2 * Copyright (c) 2000-2009,2014 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 "notifications.h"
33 #include "pcscmonitor.h"
34 #include "auditevents.h"
38 #include <security_utilities/daemon.h>
39 #include <security_utilities/machserver.h>
40 #include <security_utilities/logging.h>
42 #include <Security/SecKeychainPriv.h>
45 #include <sys/types.h>
49 // ACL subject types (their makers are instantiated here)
50 #include <security_cdsa_utilities/acl_any.h>
51 #include <security_cdsa_utilities/acl_password.h>
52 #include <security_cdsa_utilities/acl_prompted.h>
53 #include <security_cdsa_utilities/acl_protectedpw.h>
54 #include <security_cdsa_utilities/acl_threshold.h>
55 #include <security_cdsa_utilities/acl_codesigning.h>
56 #include <security_cdsa_utilities/acl_process.h>
57 #include <security_cdsa_utilities/acl_comment.h>
58 #include <security_cdsa_utilities/acl_preauth.h>
59 #include "acl_keychain.h"
60 #include "acl_partition.h"
65 // Local functions of the main program driver
67 static void usage(const char *me
) __attribute__((noreturn
));
68 static void handleSignals(int sig
);
69 static PCSCMonitor::ServiceLevel
scOptions(const char *optionString
);
70 static bool legacyTokensEnabled(void);
72 static Port gMainServerPort
;
79 int main(int argc
, char *argv
[])
81 DisableLocalization();
83 // clear the umask - we know what we're doing
84 secnotice("SecServer", "starting umask was 0%o", ::umask(0));
87 // tell the keychain (client) layer to turn off the server interface
88 SecKeychainSetServerMode();
90 const char *params
[] = {"LEGACY_TOKENS_ENABLED", legacyTokensEnabled() ? "YES" : "NO", NULL
};
91 char* errorbuf
= NULL
;
92 if (sandbox_init_with_parameters("com.apple.securityd", SANDBOX_NAMED
, params
, &errorbuf
)) {
93 seccritical("SecServer: unable to enter sandbox: %{public}s", errorbuf
);
95 sandbox_free_error(errorbuf
);
99 secnotice("SecServer", "entered sandbox");
102 // program arguments (preset to defaults)
103 bool debugMode
= false;
105 bool reExecute
= false;
106 int workerTimeout
= 0;
108 bool waitForClients
= true;
109 bool mdsIsInstalled
= false;
110 const char *tokenCacheDir
= "/var/db/TokenCache";
111 const char *smartCardOptions
= getenv("SMARTCARDS");
112 uint32_t keychainAclDefault
= CSSM_ACL_KEYCHAIN_PROMPT_INVALID
| CSSM_ACL_KEYCHAIN_PROMPT_UNSIGNED
;
113 unsigned int verbose
= 0;
115 // check for the Installation-DVD environment and modify some default arguments if found
116 if (access("/etc/rc.cdrom", F_OK
) == 0) { // /etc/rc.cdrom exists
117 secnotice("SecServer", "starting in installmode");
118 smartCardOptions
= "off"; // needs writable directories that aren't
121 // parse command line arguments
125 while ((arg
= getopt(argc
, argv
, "c:dE:ims:t:T:uvWX")) != -1) {
128 tokenCacheDir
= optarg
;
134 /* was entropyFile, kept to preserve ABI */
137 keychainAclDefault
&= ~CSSM_ACL_KEYCHAIN_PROMPT_INVALID
;
140 mdsIsInstalled
= true;
143 smartCardOptions
= optarg
;
146 if ((maxThreads
= atoi(optarg
)) < 0)
150 if ((workerTimeout
= atoi(optarg
)) < 0)
154 waitForClients
= false;
157 keychainAclDefault
&= ~CSSM_ACL_KEYCHAIN_PROMPT_UNSIGNED
;
171 // take no non-option arguments
176 const char *bootstrapName
= SECURITYSERVER_BOOTSTRAP_NAME
;
177 const char* messagingName
= SharedMemoryCommon::kDefaultSecurityMessagesName
;
179 // configure logging first
181 Syslog::open(bootstrapName
, LOG_AUTHPRIV
, LOG_PERROR
);
182 Syslog::notice("%s started in debug mode", argv
[0]);
184 Syslog::open(bootstrapName
, LOG_AUTHPRIV
, LOG_CONS
);
187 // if we're not running as root in production mode, fail
188 // in debug mode, issue a warning
189 if (uid_t uid
= getuid()) {
191 Syslog::alert("Tried to run securityd as user %d: aborted", uid
);
192 fprintf(stderr
, "You are not allowed to run securityd\n");
195 fprintf(stderr
, "securityd is unprivileged (uid=%d); some features may not work.\n", uid
);
199 // turn into a properly diabolical daemon unless debugMode is on
200 if (!debugMode
&& getppid() != 1) {
201 if (!Daemon::incarnate(doFork
))
202 exit(1); // can't daemonize
204 if (reExecute
&& !Daemon::executeSelf(argv
))
205 exit(1); // can't self-execute
208 // arm signal handlers; code below may generate signals we want to see
209 if (signal(SIGCHLD
, handleSignals
) == SIG_ERR
210 || signal(SIGINT
, handleSignals
) == SIG_ERR
211 || signal(SIGTERM
, handleSignals
) == SIG_ERR
212 || signal(SIGPIPE
, handleSignals
) == SIG_ERR
214 || signal(SIGUSR1
, handleSignals
) == SIG_ERR
216 || signal(SIGUSR2
, handleSignals
) == SIG_ERR
) {
221 // The clang static analyzer isn't a big fan of our "object creation hooks object into global pointer graph" model.
222 // Tell it not to worry.
223 #ifndef __clang_analyzer__
224 // introduce all supported ACL subject types
225 new AnyAclSubject::Maker();
226 new PasswordAclSubject::Maker();
227 new ProtectedPasswordAclSubject::Maker();
228 new PromptedAclSubject::Maker();
229 new ThresholdAclSubject::Maker();
230 new CommentAclSubject::Maker();
231 new ProcessAclSubject::Maker();
232 new CodeSignatureAclSubject::Maker();
233 new KeychainPromptAclSubject::Maker(keychainAclDefault
);
234 new PartitionAclSubject::Maker();
235 new PreAuthorizationAcls::OriginMaker();
236 new PreAuthorizationAcls::SourceMaker();
238 // establish the code equivalents database
239 CodeSignatures codeSignatures
;
242 // create the main server object and register it
243 Server
server(codeSignatures
, bootstrapName
);
245 // Remember the primary service port to send signal events to
246 gMainServerPort
= server
.primaryServicePort();
248 // set server configuration from arguments, if specified
250 server
.timeout(workerTimeout
);
252 server
.maxThreads(maxThreads
);
253 server
.floatingThread(true);
254 server
.waitForClients(waitForClients
);
255 server
.verbosity(verbose
);
257 // create a smartcard monitor to manage external token devices
258 gPCSC
= new PCSCMonitor(server
, tokenCacheDir
, scOptions(smartCardOptions
));
260 // create the RootSession object (if -d, give it graphics and tty attributes)
261 RootSession
rootSession(debugMode
? (sessionHasGraphicAccess
| sessionHasTTY
) : 0, server
);
263 // create a monitor thread to watch for audit session events
264 AuditMonitor
audits(gMainServerPort
);
267 // install MDS (if needed) and initialize the local CSSM
268 server
.loadCssm(mdsIsInstalled
);
270 #ifndef __clang_analyzer__
271 // create the shared memory notification hub
272 new SharedMemoryListener(messagingName
, kSharedMemoryPoolSize
);
276 // okay, we're ready to roll
277 secnotice("SecServer", "Entering service as %s", (char*)bootstrapName
);
278 Syslog::notice("Entering service");
283 // fell out of runloop (should not happen)
284 Syslog::alert("Aborting");
290 // Issue usage message and die
292 static void usage(const char *me
)
294 fprintf(stderr
, "Usage: %s [-dwX]"
295 "\n\t[-c tokencache] smartcard token cache directory"
296 "\n\t[-e equivDatabase] path to code equivalence database"
297 "\n\t[-s off|on|conservative|aggressive] smartcard operation level"
298 "\n\t[-t maxthreads] [-T threadTimeout] server thread control"
303 const CFStringRef kTKSmartCardPreferencesDomain
= CFSTR("com.apple.security.smartcard");
304 const CFStringRef kTKLegacyTokendPreferencesKey
= CFSTR("Legacy");
306 static bool legacyTokensEnabled() {
308 CFPropertyListRef value
= CFPreferencesCopyValue(kTKLegacyTokendPreferencesKey
, kTKSmartCardPreferencesDomain
, kCFPreferencesAnyUser
, kCFPreferencesCurrentHost
);
310 if (CFEqual(value
, kCFBooleanTrue
)) {
319 // Translate strings (e.g. "conservative") into PCSCMonitor service levels
321 static PCSCMonitor::ServiceLevel
scOptions(const char *optionString
)
323 if (!legacyTokensEnabled())
324 return PCSCMonitor::forcedOff
;
327 if (!strcmp(optionString
, "off"))
328 return PCSCMonitor::forcedOff
;
329 else if (!strcmp(optionString
, "on"))
330 return PCSCMonitor::externalDaemon
;
331 else if (!strcmp(optionString
, "conservative"))
332 return PCSCMonitor::externalDaemon
;
333 else if (!strcmp(optionString
, "aggressive"))
334 return PCSCMonitor::externalDaemon
;
335 else if (!strcmp(optionString
, "external"))
336 return PCSCMonitor::externalDaemon
;
340 return PCSCMonitor::externalDaemon
;
346 // We send ourselves a message (through the "self" service), so actual
347 // actions happen on the normal event loop path. Note that another thread
348 // may be picking up the message immediately.
350 static void handleSignals(int sig
)
352 (void)self_client_handleSignal(gMainServerPort
, mach_task_self(), sig
);