2 * Copyright (c) 2000-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // securityd - Apple security services daemon.
28 #include <securityd_client/ucsp.h>
32 #include "authority.h"
34 #include "notifications.h"
35 #include "pcscmonitor.h"
38 #include <security_utilities/daemon.h>
39 #include <security_utilities/machserver.h>
40 #include <security_utilities/logging.h>
41 #include <security_utilities/ktracecodes.h>
43 #include <Security/SecKeychainPriv.h>
46 #include <sys/types.h>
51 // #define PERFORMANCE_MEASUREMENT 1
53 #ifdef PERFORMANCE_MEASUREMENT
54 #include <mach/mach_time.h>
57 // ACL subject types (their makers are instantiated here)
58 #include <security_cdsa_utilities/acl_any.h>
59 #include <security_cdsa_utilities/acl_password.h>
60 #include <security_cdsa_utilities/acl_prompted.h>
61 #include <security_cdsa_utilities/acl_protectedpw.h>
62 #include <security_cdsa_utilities/acl_threshold.h>
63 #include <security_cdsa_utilities/acl_codesigning.h>
64 #include <security_cdsa_utilities/acl_process.h>
65 #include <security_cdsa_utilities/acl_comment.h>
66 #include <security_cdsa_utilities/acl_preauth.h>
67 #include "acl_keychain.h"
71 // Local functions of the main program driver
73 static void usage(const char *me
) __attribute__((noreturn
));
74 static void handleSignals(int sig
);
75 static PCSCMonitor::ServiceLevel
scOptions(const char *optionString
);
78 static Port gMainServerPort
;
84 int main(int argc
, char *argv
[])
86 #ifdef PERFORMANCE_MEASUREMENT
87 // needed for automated timing of securityd startup
88 uint64_t startTime
= mach_absolute_time ();
91 Debug::trace (kSecTraceSecurityServerStart
);
93 // clear the umask - we know what we're doing
94 secdebug("SS", "starting umask was 0%o", ::umask(0));
97 // tell the keychain (client) layer to turn off the server interface
98 SecKeychainSetServerMode();
100 // program arguments (preset to defaults)
101 bool debugMode
= false;
102 const char *bootstrapName
= NULL
;
103 const char* messagingName
= SECURITY_MESSAGES_NAME
;
105 bool reExecute
= false;
106 int workerTimeout
= 0;
108 bool waitForClients
= false;
109 const char *authorizationConfig
= "/etc/authorization";
110 const char *tokenCacheDir
= "/var/db/TokenCache";
111 const char *entropyFile
= "/var/db/SystemEntropyCache";
112 const char *equivDbFile
= EQUIVALENCEDBPATH
;
113 const char *smartCardOptions
= getenv("SMARTCARDS");
114 uint32_t keychainAclDefault
= CSSM_ACL_KEYCHAIN_PROMPT_INVALID
| CSSM_ACL_KEYCHAIN_PROMPT_UNSIGNED
;
116 // check for the Installation-DVD environment and modify some default arguments if found
117 if (access("/etc/rc.cdrom", F_OK
) == 0) { // /etc/rc.cdrom exists
118 secdebug("SS", "configuring for installation");
119 smartCardOptions
= "off"; // needs writable directories that aren't
122 // parse command line arguments
126 while ((arg
= getopt(argc
, argv
, "a:c:de:E:fiN:s:t:T:Xuw")) != -1) {
129 authorizationConfig
= optarg
;
132 tokenCacheDir
= optarg
;
138 equivDbFile
= optarg
;
141 entropyFile
= optarg
;
144 fprintf(stderr
, "%s: the -f option is obsolete\n", argv
[0]);
147 keychainAclDefault
&= ~CSSM_ACL_KEYCHAIN_PROMPT_INVALID
;
150 bootstrapName
= optarg
;
153 smartCardOptions
= optarg
;
156 if ((maxThreads
= atoi(optarg
)) < 0)
160 if ((workerTimeout
= atoi(optarg
)) < 0)
164 waitForClients
= true;
167 keychainAclDefault
&= ~CSSM_ACL_KEYCHAIN_PROMPT_UNSIGNED
;
178 // take no non-option arguments
182 // figure out the bootstrap name
183 if (!bootstrapName
) {
184 bootstrapName
= getenv(SECURITYSERVER_BOOTSTRAP_ENV
);
187 bootstrapName
= SECURITYSERVER_BOOTSTRAP_NAME
;
191 messagingName
= bootstrapName
;
196 messagingName
= bootstrapName
;
199 // configure logging first
201 Syslog::open(bootstrapName
, LOG_AUTHPRIV
, LOG_PERROR
);
202 Syslog::notice("%s started in debug mode", argv
[0]);
204 Syslog::open(bootstrapName
, LOG_AUTHPRIV
, LOG_CONS
);
207 // if we're not running as root in production mode, fail
208 // in debug mode, issue a warning
209 if (uid_t uid
= getuid()) {
211 Syslog::alert("Tried to run securityd as user %d: aborted", uid
);
212 fprintf(stderr
, "You are not allowed to run securityd\n");
215 fprintf(stderr
, "securityd is unprivileged; some features may not work.\n");
216 secdebug("SS", "Running as user %d (you have been warned)", uid
);
220 // turn into a properly diabolical daemon unless debugMode is on
221 if (!debugMode
&& getppid() != 1) {
222 if (!Daemon::incarnate(doFork
))
223 exit(1); // can't daemonize
225 if (reExecute
&& !Daemon::executeSelf(argv
))
226 exit(1); // can't self-execute
229 // arm signal handlers; code below may generate signals we want to see
230 if (signal(SIGCHLD
, handleSignals
) == SIG_ERR
)
231 secdebug("SS", "Cannot handle SIGCHLD: errno=%d", errno
);
232 if (signal(SIGINT
, handleSignals
) == SIG_ERR
)
233 secdebug("SS", "Cannot handle SIGINT: errno=%d", errno
);
234 if (signal(SIGTERM
, handleSignals
) == SIG_ERR
)
235 secdebug("SS", "Cannot handle SIGTERM: errno=%d", errno
);
236 if (signal(SIGPIPE
, handleSignals
) == SIG_ERR
)
237 secdebug("SS", "Cannot handle SIGPIPE: errno=%d", errno
);
239 if (signal(SIGUSR1
, handleSignals
) == SIG_ERR
)
240 secdebug("SS", "Cannot handle SIGHUP: errno=%d", errno
);
243 // create the shared memory notification hub
244 new SharedMemoryListener(messagingName
, kSharedMemoryPoolSize
);
246 // create an Authorization engine
247 Authority
authority(authorizationConfig
);
249 // introduce all supported ACL subject types
250 new AnyAclSubject::Maker();
251 new PasswordAclSubject::Maker();
252 new ProtectedPasswordAclSubject::Maker();
253 new PromptedAclSubject::Maker();
254 new ThresholdAclSubject::Maker();
255 new CommentAclSubject::Maker();
256 new ProcessAclSubject::Maker();
257 new CodeSignatureAclSubject::Maker();
258 new KeychainPromptAclSubject::Maker(keychainAclDefault
);
259 new PreAuthorizationAcls::OriginMaker();
260 new PreAuthorizationAcls::SourceMaker();
262 // establish the code equivalents database
263 CodeSignatures
codeSignatures(equivDbFile
);
265 // create the main server object and register it
266 Server
server(authority
, codeSignatures
, bootstrapName
);
268 // Remember the primary service port to send signal events to
269 gMainServerPort
= server
.primaryServicePort();
271 // set server configuration from arguments, if specified
273 server
.timeout(workerTimeout
);
275 server
.maxThreads(maxThreads
);
276 server
.floatingThread(true);
277 server
.waitForClients(waitForClients
);
279 // add the RNG seed timer
281 EntropyManager
entropy(server
, entropyFile
);
283 if (getuid() == 0) new EntropyManager(server
, entropyFile
);
286 // create a token-cache interface
288 if (const char *s
= getenv("TOKENCACHE"))
292 // create a smartcard monitor to manage external token devices
293 new PCSCMonitor(server
, tokenCacheDir
, scOptions(smartCardOptions
));
295 // create the RootSession object (if -d, give it graphics and tty attributes)
296 RootSession
rootSession(server
,
297 debugMode
? (sessionHasGraphicAccess
| sessionHasTTY
) : 0);
299 // install MDS and initialize the local CSSM
302 // okay, we're ready to roll
303 Syslog::notice("Entering service");
304 secdebug("SS", "%s initialized", bootstrapName
);
305 Debug::trace (kSecTraceSecurityServerInitialized
);
307 #ifdef PERFORMANCE_MEASUREMENT
308 // needed for automated timing of securityd startup
309 uint64_t endTime
= mach_absolute_time ();
311 // compute how long it took to initialize
312 uint64_t elapsedTime
= endTime
- startTime
;
313 mach_timebase_info_data_t multiplier
;
314 mach_timebase_info (&multiplier
);
316 elapsedTime
= elapsedTime
* multiplier
.numer
/ multiplier
.denom
;
318 FILE* f
= fopen ("/var/log/startuptime.txt", "a");
321 // probably not running as root.
322 f
= fopen ("/tmp/startuptime.txt", "a");
325 fprintf (f
, "%lld\n", elapsedTime
);
332 // fell out of runloop (should not happen)
333 Syslog::alert("Aborting");
339 // Issue usage message and die
341 static void usage(const char *me
)
343 fprintf(stderr
, "Usage: %s [-dwX]"
344 "\n\t[-a authConfigFile] Authorization configuration file"
345 "\n\t[-c tokencache] smartcard token cache directory"
346 "\n\t[-e equivDatabase] path to code equivalence database"
347 "\n\t[-N serviceName] MACH service name"
348 "\n\t[-s off|on|conservative|aggressive] smartcard operation level"
349 "\n\t[-t maxthreads] [-T threadTimeout] server thread control"
356 // Translate strings (e.g. "conservative") into PCSCMonitor service levels
358 static PCSCMonitor::ServiceLevel
scOptions(const char *optionString
)
361 if (!strcmp(optionString
, "off"))
362 return PCSCMonitor::forcedOff
;
363 else if (!strcmp(optionString
, "on"))
364 return PCSCMonitor::forcedOn
;
365 else if (!strcmp(optionString
, "conservative"))
366 return PCSCMonitor::conservative
;
367 else if (!strcmp(optionString
, "aggressive"))
368 return PCSCMonitor::aggressive
;
369 else if (!strcmp(optionString
, "external"))
370 return PCSCMonitor::externalDaemon
;
374 return PCSCMonitor::aggressive
;
380 // We send ourselves a message (through the "self" service), so actual
381 // actions happen on the normal event loop path. Note that another thread
382 // may be picking up the message immediately.
384 static void handleSignals(int sig
)
386 if (kern_return_t rc
= self_client_handleSignal(gMainServerPort
, mach_task_self(), sig
))
387 Syslog::error("self-send failed (mach error %d)", rc
);