]>
git.saurik.com Git - apple/security.git/blob - SecurityServer/main.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // SecurityServer - Apple security services daemon.
22 #include "securityserver.h"
24 #include <MacYarrow/yarrowseed.h>
26 #include <Security/daemon.h>
27 #include <Security/osxsigner.h>
28 #include "authority.h"
32 #include <Security/machserver.h>
34 #include <sys/types.h>
38 // ACL subject types (their makers are instantiated here)
39 #include <Security/acl_any.h>
40 #include <Security/acl_password.h>
41 #include <Security/acl_threshold.h>
42 #include <Security/acl_codesigning.h>
43 #include <Security/acl_comment.h>
44 #include "acl_keychain.h"
51 // Program options (set by argument scan and environment)
55 } // end namespace Security
59 // Local functions of the main program driver
61 static void usage(const char *me
);
62 static void handleSIGCHLD(int);
63 static void handleSIGOther(int);
69 int main(int argc
, char *argv
[])
71 // program arguments (preset to defaults)
72 bool forceCssmInit
= false;
73 int workerTimeout
= 0;
75 const char *authorizationConfig
= "/etc/authorization";
76 const char *bootstrapName
= "SecurityServer";
78 // parse command line arguments
82 while ((arg
= getopt(argc
, argv
, "a:dfN:t:T:")) != -1) {
85 authorizationConfig
= optarg
;
94 bootstrapName
= optarg
;
97 if ((maxThreads
= atoi(optarg
)) < 0)
101 if ((workerTimeout
= atoi(optarg
)) < 0)
109 // take no non-option arguments
115 Syslog::open(argv
[0], LOG_AUTHPRIV
, LOG_PERROR
);
116 Syslog::notice("SecurityServer started in debug mode");
118 Syslog::open(argv
[0], LOG_AUTHPRIV
, LOG_CONS
);
121 // if we're not running as root in production mode, fail
122 // in debug mode, issue a warning
123 if (uid_t uid
= getuid()) {
125 Syslog::alert("Unprivileged SecurityServer aborted (uid=%d)", uid
);
126 fprintf(stderr
, "You are not allowed to run SecurityServer\n");
129 debug("SS", "Running unprivileged (uid=%d); some features may not work", uid
);
133 // turn into a properly diabolical daemon unless debugMode is on
134 if (!debugMode
&& !Daemon::incarnate())
137 // create a code signing engine
138 CodeSigning::OSXSigner signer
;
140 // create an Authorization engine
141 Authority
authority(authorizationConfig
);
143 // establish the ACL machinery
144 new AnyAclSubject::Maker();
145 new PasswordAclSubject::Maker();
146 new ThresholdAclSubject::Maker();
147 new KeychainPromptAclSubject::Maker();
148 new CommentAclSubject::Maker();
149 new CodeSignatureAclSubject::Maker(signer
);
151 // create the RootSession object
152 RootSession rootSession
;
154 // create the main server object and register it
155 Server
server(authority
, bootstrapName
);
157 // set server configuration from arguments, if specified
159 server
.timeout(workerTimeout
);
161 server
.maxThreads(maxThreads
);
163 // add the RNG seed timer to it
164 YarrowTimer
yarrow(server
);
166 // set up signal handlers
167 if (signal(SIGCHLD
, handleSIGCHLD
) == SIG_ERR
)
168 debug("SS", "Cannot ignore SIGCHLD: errno=%d", errno
);
169 if (signal(SIGINT
, handleSIGOther
) == SIG_ERR
)
170 debug("SS", "Cannot handle SIGINT: errno=%d", errno
);
171 if (signal(SIGTERM
, handleSIGOther
) == SIG_ERR
)
172 debug("SS", "Cannot handle SIGTERM: errno=%d", errno
);
174 // initialize CSSM now if requested
178 Syslog::notice("Entering service");
179 debug("SS", "Entering service run loop");
182 // fell out of runloop (should not happen)
183 Syslog::alert("Aborting");
189 // Issue usage message and die
191 static void usage(const char *me
)
193 fprintf(stderr
, "Usage: %s [-df] [-t maxthreads] [-T threadTimeout]"
194 "\t[-N bootstrapName] [-a authConfigFile]\n", me
);
200 // Handle SIGCHLD signals to reap our children (zombie cleanup)
202 static void handleSIGCHLD(int)
205 switch (pid_t pid
= waitpid(-1, &status
, WNOHANG
)) {
207 debug("SS", "Spurious SIGCHLD ignored");
210 debug("SS", "waitpid after SIGCHLD failed: errno=%d", errno
);
213 debug("SS", "Reaping child pid=%d", pid
);
220 // Handle some other signals to shut down cleanly (and with logging)
222 static void handleSIGOther(int sig
)
226 debug("SS", "Interrupt signal; terminating");
229 debug("SS", "Termination signal; terminating");