2 * Copyright (c) 2005-2016 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@
29 #include <bsm/libbsm.h>
30 #include <servers/bootstrap.h>
31 #include <sys/types.h>
34 //#define DEBUG_MACH_PORT_ALLOCATIONS
36 #include <CoreFoundation/CoreFoundation.h>
37 #include <CoreFoundation/CFRuntime.h>
38 #include <Security/Security.h>
39 #include <Security/SecTask.h>
41 #define SC_LOG_HANDLE __log_SCHelper()
42 #include "SCPreferencesInternal.h"
43 #include "SCHelper_client.h"
44 #include "helper_types.h"
47 #pragma mark SCHelper session managment
51 // entitlement used to control read (or write) access to a given "prefsID"
53 #define kSCReadEntitlementName CFSTR("com.apple.SystemConfiguration.SCPreferences-read-access")
54 #define kSCWriteEntitlementName CFSTR("com.apple.SystemConfiguration.SCPreferences-write-access")
57 // entitlement used to allow limited [VPN configuration] write access to the "preferences.plist"
59 #define kSCVPNFilterEntitlementName CFSTR("com.apple.networking.vpn.configuration")
61 typedef enum { NO
= 0, YES
, UNKNOWN
} lazyBoolean
;
63 typedef const struct __SCHelperSession
* SCHelperSessionRef
;
67 // base CFType information
74 AuthorizationRef authorization
;
75 Boolean use_entitlement
;
81 // Mach security audit trailer for evaluating credentials
82 audit_token_t auditToken
;
84 // write access entitlement associated with this session
85 lazyBoolean callerReadAccess
;
86 lazyBoolean callerWriteAccess
;
88 // configuration filtering
89 lazyBoolean isSetChange
; // only network "set" changes
90 lazyBoolean isVPNChange
; // only VPN configuration changes
94 SCPreferencesRef prefs
;
97 CFMutableSetRef backtraces
;
99 } SCHelperSessionPrivate
, *SCHelperSessionPrivateRef
;
102 static CFStringRef
__SCHelperSessionCopyDescription (CFTypeRef cf
);
103 static void __SCHelperSessionDeallocate (CFTypeRef cf
);
106 static void __SCHelperSessionLogBacktrace (const void *value
, void *context
);
109 static CFTypeID __kSCHelperSessionTypeID
= _kCFRuntimeNotATypeID
;
110 static Boolean debug
= FALSE
;
111 static pthread_once_t initialized
= PTHREAD_ONCE_INIT
;
112 static CFRunLoopRef main_runLoop
= NULL
;
113 static CFMutableSetRef sessions
= NULL
;
114 static int sessions_closed
= 0; // count of sessions recently closed
115 static int sessions_generation
= 0;
116 static pthread_mutex_t sessions_lock
= PTHREAD_MUTEX_INITIALIZER
;
126 static os_log_t log
= NULL
;
129 log
= os_log_create("com.apple.SystemConfiguration", "SCPreferences");
137 #pragma mark Helper session management
140 #if !TARGET_OS_IPHONE
142 __SCHelperSessionUseEntitlement(SCHelperSessionRef session
)
144 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
146 return sessionPrivate
->use_entitlement
;
148 #endif //!TARGET_OS_IPHONE
151 static AuthorizationRef
152 __SCHelperSessionGetAuthorization(SCHelperSessionRef session
)
154 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
156 return sessionPrivate
->authorization
;
161 __SCHelperSessionSetAuthorization(SCHelperSessionRef session
, CFTypeRef authorizationData
)
164 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
167 * On OSX, the authorizationData can either be a CFData-wrapped/externalized
168 * "authorization" or a CFString indicating that we should check entitlements.
170 * On iOS, the authorizationData is a CFString indicating that we should
171 * check entitlements.
173 pthread_mutex_lock(&sessionPrivate
->lock
);
175 if (sessionPrivate
->authorization
!= NULL
) {
176 #if !TARGET_OS_IPHONE
177 if (!__SCHelperSessionUseEntitlement(session
)) {
180 status
= AuthorizationFree(sessionPrivate
->authorization
, kAuthorizationFlagDefaults
);
181 // status = AuthorizationFree(sessionPrivate->authorization, kAuthorizationFlagDestroyRights);
182 if (status
!= errAuthorizationSuccess
) {
183 SC_log(LOG_DEBUG
, "AuthorizationFree() failed: status = %d",
187 CFRelease(sessionPrivate
->authorization
);
189 #else //!TARGET_OS_IPHONE
190 CFRelease(sessionPrivate
->authorization
);
191 #endif //!TARGET_OS_IPHONE
192 sessionPrivate
->authorization
= NULL
;
193 sessionPrivate
->use_entitlement
= FALSE
;
196 #if !TARGET_OS_IPHONE
197 if (isA_CFData(authorizationData
)) {
198 AuthorizationExternalForm extForm
;
200 if (CFDataGetLength(authorizationData
) == sizeof(extForm
.bytes
)) {
203 bcopy(CFDataGetBytePtr(authorizationData
), extForm
.bytes
, sizeof(extForm
.bytes
));
204 status
= AuthorizationCreateFromExternalForm(&extForm
,
205 &sessionPrivate
->authorization
);
206 if (status
!= errAuthorizationSuccess
) {
207 SC_log(LOG_NOTICE
, "AuthorizationCreateFromExternalForm() failed: status = %d",
209 sessionPrivate
->authorization
= NULL
;
213 } else if (isA_CFString(authorizationData
)) {
214 sessionPrivate
->authorization
= (void *)CFRetain(authorizationData
);
215 sessionPrivate
->use_entitlement
= TRUE
;
217 #else //!TARGET_OS_IPHONE
218 if (isA_CFString(authorizationData
)) {
219 sessionPrivate
->authorization
= (void *)CFRetain(authorizationData
);
220 sessionPrivate
->use_entitlement
= TRUE
;
222 #endif //!TARGET_OS_IPHONE
224 pthread_mutex_unlock(&sessionPrivate
->lock
);
230 static SCPreferencesRef
231 __SCHelperSessionGetPreferences(SCHelperSessionRef session
)
233 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
235 return sessionPrivate
->prefs
;
240 __SCHelperSessionSetThreadName(SCHelperSessionRef session
)
246 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
248 if (sessionPrivate
->mp
== NULL
) {
252 if (sessionPrivate
->prefs
!= NULL
) {
253 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)sessionPrivate
->prefs
;
255 if (prefsPrivate
->name
!= NULL
) {
256 caller
= _SC_cfstring_to_cstring(prefsPrivate
->name
,
259 kCFStringEncodingUTF8
);
262 path
= (prefsPrivate
->newPath
!= NULL
) ? prefsPrivate
->newPath
: prefsPrivate
->path
;
264 path_s
= strrchr(path
, '/');
265 if (path_s
!= NULL
) {
271 if (caller
!= NULL
) {
272 snprintf(name
, sizeof(name
), "SESSION|%p|%s|%s%s",
273 (void *)(uintptr_t)CFMachPortGetPort(sessionPrivate
->mp
),
275 (path_s
!= NULL
) ? "*/" : "",
276 (path
!= NULL
) ? path
: "?");
277 CFAllocatorDeallocate(NULL
, caller
);
279 snprintf(name
, sizeof(name
), "SESSION|%p",
280 (void *)(uintptr_t)CFMachPortGetPort(sessionPrivate
->mp
));
283 pthread_setname_np(name
);
290 __SCHelperSessionSetPreferences(SCHelperSessionRef session
, SCPreferencesRef prefs
)
292 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
294 pthread_mutex_lock(&sessionPrivate
->lock
);
299 if (sessionPrivate
->prefs
!= NULL
) {
300 SC_log(LOG_INFO
, "%p : close", session
);
301 CFRelease(sessionPrivate
->prefs
);
304 SC_log(LOG_INFO
, "%p : open, prefs = %@", session
, prefs
);
306 sessionPrivate
->prefs
= prefs
;
308 __SCHelperSessionSetThreadName(session
);
310 pthread_mutex_unlock(&sessionPrivate
->lock
);
317 __SCHelperSessionSetNetworkSetFilter(SCHelperSessionRef session
, Boolean setChange
)
319 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
321 pthread_mutex_lock(&sessionPrivate
->lock
);
323 sessionPrivate
->isSetChange
= setChange
? YES
: NO
;
325 pthread_mutex_unlock(&sessionPrivate
->lock
);
332 __SCHelperSessionUseNetworkSetFilter(SCHelperSessionRef session
)
334 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
336 return (sessionPrivate
->isSetChange
== YES
) ? TRUE
: FALSE
;
341 __SCHelperSessionSetVPNFilter(SCHelperSessionRef session
, Boolean vpnChange
, CFArrayRef vpnTypes
)
343 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
345 pthread_mutex_lock(&sessionPrivate
->lock
);
347 sessionPrivate
->isVPNChange
= vpnChange
? YES
: NO
;
349 if (vpnTypes
!= NULL
) {
352 if (sessionPrivate
->vpnTypes
!= NULL
) {
353 CFRelease(sessionPrivate
->vpnTypes
);
355 sessionPrivate
->vpnTypes
= vpnTypes
;
357 pthread_mutex_unlock(&sessionPrivate
->lock
);
364 __SCHelperSessionUseVPNFilter(SCHelperSessionRef session
, CFArrayRef
*vpnTypes
)
366 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
368 *vpnTypes
= sessionPrivate
->vpnTypes
;
369 return (sessionPrivate
->vpnTypes
!= NULL
) ? TRUE
: FALSE
;
374 __SCHelperSessionLog(const void *value
, void *context
)
376 SCHelperSessionRef session
= (SCHelperSessionRef
)value
;
377 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
378 FILE **logFile
= (FILE **)context
;
380 pthread_mutex_lock(&sessionPrivate
->lock
);
382 if ((sessionPrivate
->mp
!= NULL
) && (sessionPrivate
->prefs
!= NULL
)) {
383 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)sessionPrivate
->prefs
;
385 SC_log(LOG_INFO
, " %p {port = %p, caller = %@, path = %s%s}",
387 (void *)(uintptr_t)CFMachPortGetPort(sessionPrivate
->mp
),
389 prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
,
390 prefsPrivate
->locked
? ", locked" : "");
392 if ((sessionPrivate
->backtraces
!= NULL
) &&
393 (CFSetGetCount(sessionPrivate
->backtraces
) > 0)) {
394 // log/report all collected backtraces
395 CFSetApplyFunction(sessionPrivate
->backtraces
,
396 __SCHelperSessionLogBacktrace
,
399 // to ensure that we don't log the same backtraces multiple
400 // times we remove any reported traces
401 CFRelease(sessionPrivate
->backtraces
);
402 sessionPrivate
->backtraces
= NULL
;
406 pthread_mutex_unlock(&sessionPrivate
->lock
);
415 static const CFRuntimeClass __SCHelperSessionClass
= {
417 "SCHelperSession", // className
420 __SCHelperSessionDeallocate
, // dealloc
423 NULL
, // copyFormattingDesc
424 __SCHelperSessionCopyDescription
// copyDebugDesc
429 __SCHelperSessionCopyDescription(CFTypeRef cf
)
431 CFAllocatorRef allocator
= CFGetAllocator(cf
);
432 CFMutableStringRef result
;
433 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)cf
;
435 pthread_mutex_lock(&sessionPrivate
->lock
);
437 result
= CFStringCreateMutable(allocator
, 0);
438 CFStringAppendFormat(result
, NULL
, CFSTR("<SCHelperSession %p [%p]> {"), cf
, allocator
);
439 CFStringAppendFormat(result
, NULL
, CFSTR("authorization = %p"), sessionPrivate
->authorization
);
440 if (sessionPrivate
->mp
!= NULL
) {
441 CFStringAppendFormat(result
, NULL
,
442 CFSTR(", mp = %p (port = 0x%x)"),
444 CFMachPortGetPort(sessionPrivate
->mp
));
446 if (sessionPrivate
->prefs
!= NULL
) {
447 CFStringAppendFormat(result
, NULL
, CFSTR(", prefs = %@"), sessionPrivate
->prefs
);
449 CFStringAppendFormat(result
, NULL
, CFSTR("}"));
451 pthread_mutex_unlock(&sessionPrivate
->lock
);
458 __SCHelperSessionDeallocate(CFTypeRef cf
)
460 SCHelperSessionRef session
= (SCHelperSessionRef
)cf
;
461 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
463 // we're releasing "a" session so take the global lock...
464 pthread_mutex_lock(&sessions_lock
);
467 __SCHelperSessionSetAuthorization(session
, NULL
);
468 __SCHelperSessionSetPreferences(session
, NULL
);
469 __SCHelperSessionSetNetworkSetFilter(session
, FALSE
);
470 __SCHelperSessionSetVPNFilter(session
, FALSE
, NULL
);
471 pthread_mutex_destroy(&sessionPrivate
->lock
);
472 if (sessionPrivate
->backtraces
!= NULL
) {
473 CFRelease(sessionPrivate
->backtraces
);
476 // we no longer need/want to track this session
477 CFSetRemoveValue(sessions
, sessionPrivate
);
478 sessions_generation
++;
481 // release the global lock, wake up the main runloop, all done
482 pthread_mutex_unlock(&sessions_lock
);
483 CFRunLoopWakeUp(main_runLoop
);
490 __SCHelperSessionInitialize(void)
492 __kSCHelperSessionTypeID
= _CFRuntimeRegisterClass(&__SCHelperSessionClass
);
497 static SCHelperSessionRef
498 __SCHelperSessionCreate(CFAllocatorRef allocator
)
500 SCHelperSessionPrivateRef sessionPrivate
;
503 // initialize runtime
504 pthread_once(&initialized
, __SCHelperSessionInitialize
);
507 size
= sizeof(SCHelperSessionPrivate
) - sizeof(CFRuntimeBase
);
508 sessionPrivate
= (SCHelperSessionPrivateRef
)_CFRuntimeCreateInstance(allocator
,
509 __kSCHelperSessionTypeID
,
512 if (sessionPrivate
== NULL
) {
516 /* initialize non-zero/NULL members */
517 if (pthread_mutex_init(&sessionPrivate
->lock
, NULL
) != 0) {
518 SC_log(LOG_NOTICE
, "pthread_mutex_init(): failure to initialize per session lock");
519 CFRelease(sessionPrivate
);
522 sessionPrivate
->callerReadAccess
= UNKNOWN
;
523 sessionPrivate
->callerWriteAccess
= UNKNOWN
;
524 sessionPrivate
->isSetChange
= UNKNOWN
;
525 sessionPrivate
->isVPNChange
= UNKNOWN
;
527 // keep track this session
528 pthread_mutex_lock(&sessions_lock
);
529 if (sessions
== NULL
) {
530 const CFSetCallBacks mySetCallBacks
= { 0, NULL
, NULL
, CFCopyDescription
, CFEqual
, CFHash
};
532 // create a non-retaining set
533 sessions
= CFSetCreateMutable(NULL
, 0, &mySetCallBacks
);
535 CFSetAddValue(sessions
, sessionPrivate
);
536 sessions_generation
++;
537 pthread_mutex_unlock(&sessions_lock
);
539 return (SCHelperSessionRef
)sessionPrivate
;
546 static SCHelperSessionRef
547 __SCHelperSessionFindWithPort(mach_port_t port
)
549 SCHelperSessionRef session
= NULL
;
551 // keep track this session
552 pthread_mutex_lock(&sessions_lock
);
553 if (sessions
!= NULL
) {
555 CFIndex n
= CFSetGetCount(sessions
);
556 const void * vals_q
[16];
557 const void ** vals
= vals_q
;
559 if (n
> (CFIndex
)(sizeof(vals_q
) / sizeof(SCHelperSessionRef
)))
560 vals
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
561 CFSetGetValues(sessions
, vals
);
562 for (i
= 0; i
< n
; i
++) {
563 SCHelperSessionPrivateRef sessionPrivate
;
565 sessionPrivate
= (SCHelperSessionPrivateRef
)vals
[i
];
566 if (sessionPrivate
->port
== port
) {
567 session
= (SCHelperSessionRef
)sessionPrivate
;
572 CFAllocatorDeallocate(NULL
, vals
);
574 pthread_mutex_unlock(&sessions_lock
);
581 #pragma mark Session backtrace logging
585 __SCHelperSessionAddBacktrace(SCHelperSessionRef session
, CFStringRef backtrace
, const char * command
)
587 CFStringRef logEntry
;
588 SCPreferencesRef prefs
;
589 SCPreferencesPrivateRef prefsPrivate
;
590 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
592 prefs
= __SCHelperSessionGetPreferences((SCHelperSessionRef
)sessionPrivate
);
597 prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
599 logEntry
= CFStringCreateWithFormat(NULL
, NULL
,
600 CFSTR("%@ [%s]: %s\n\n%@"),
602 prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
,
606 pthread_mutex_lock(&sessionPrivate
->lock
);
608 if (sessionPrivate
->backtraces
== NULL
) {
609 sessionPrivate
->backtraces
= CFSetCreateMutable(NULL
, 0, &kCFTypeSetCallBacks
);
611 CFSetSetValue(sessionPrivate
->backtraces
, logEntry
);
613 pthread_mutex_unlock(&sessionPrivate
->lock
);
621 __SCHelperSessionLogBacktrace(const void *value
, void *context
)
623 CFSetRef backtrace
= (CFSetRef
)value
;
624 FILE **logFile
= (FILE **)context
;
626 if (*logFile
== NULL
) {
629 struct timeval tv_now
;
631 (void)gettimeofday(&tv_now
, NULL
);
632 (void)localtime_r(&tv_now
.tv_sec
, &tm_now
);
636 "/Library/Logs/CrashReporter/SCHelper-%4d-%02d-%02d-%02d%02d%02d.log",
637 tm_now
.tm_year
+ 1900,
644 *logFile
= fopen(path
, "a");
645 if (*logFile
== NULL
) {
646 // if log file could not be created
650 SC_log(LOG_INFO
, "created backtrace log: %s", path
);
653 SCPrint(TRUE
, *logFile
, CFSTR("%@\n"), backtrace
);
665 * (out) status = SCError()
669 do_Exit(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
678 * (in) data = authorizationDict (in 2 flavors)
679 * kSCHelperAuthAuthorization - use provided AuthorizationExternalForm
680 * kSCHelperAuthCallerInfo - use entitlement
681 * (out) status = OSStatus
685 do_Auth(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
687 CFDictionaryRef authorizationDict
;
688 #if !TARGET_OS_IPHONE
689 CFDataRef authorizationData
= NULL
;
693 if (!_SCUnserialize((CFPropertyListRef
*)&authorizationDict
, data
, NULL
, 0)) {
697 if (authorizationDict
== NULL
) {
701 if (!isA_CFDictionary(authorizationDict
)) {
702 CFRelease(authorizationDict
);
706 #if !TARGET_OS_IPHONE
707 authorizationData
= CFDictionaryGetValue(authorizationDict
, kSCHelperAuthAuthorization
);
708 if (authorizationData
!= NULL
&& isA_CFData(authorizationData
)) {
709 ok
= __SCHelperSessionSetAuthorization(session
, authorizationData
);
713 CFStringRef authorizationInfo
;
715 authorizationInfo
= CFDictionaryGetValue(authorizationDict
, kSCHelperAuthCallerInfo
);
716 if (authorizationInfo
!= NULL
&& isA_CFString(authorizationInfo
)) {
717 ok
= __SCHelperSessionSetAuthorization(session
, authorizationInfo
);
721 CFRelease(authorizationDict
);
722 *status
= ok
? 0 : 1;
727 #if !TARGET_OS_IPHONE
731 * SCHELPER_MSG_KEYCHAIN_COPY
732 * (in) data = unique_id
733 * (out) status = SCError()
734 * (out) reply = password
737 do_keychain_copy(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
740 SCPreferencesRef prefs
;
741 CFStringRef unique_id
= NULL
;
743 if ((data
!= NULL
) && !_SCUnserializeString(&unique_id
, data
, NULL
, 0)) {
747 if (unique_id
!= NULL
) {
748 if (isA_CFString(unique_id
)) {
749 prefs
= __SCHelperSessionGetPreferences(session
);
750 *reply
= _SCPreferencesSystemKeychainPasswordItemCopy(prefs
, unique_id
);
751 if (*reply
== NULL
) {
757 CFRelease(unique_id
);
765 * SCHELPER_MSG_KEYCHAIN_EXISTS
766 * (in) data = unique_id
767 * (out) status = SCError()
771 do_keychain_exists(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
774 SCPreferencesRef prefs
;
775 CFStringRef unique_id
= NULL
;
777 if ((data
!= NULL
) && !_SCUnserializeString(&unique_id
, data
, NULL
, 0)) {
781 if (unique_id
!= NULL
) {
782 if (isA_CFString(unique_id
)) {
783 prefs
= __SCHelperSessionGetPreferences(session
);
784 ok
= _SCPreferencesSystemKeychainPasswordItemExists(prefs
, unique_id
);
790 CFRelease(unique_id
);
798 * SCHELPER_MSG_KEYCHAIN_REMOVE
799 * (in) data = unique_id
800 * (out) status = SCError()
804 do_keychain_remove(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
807 SCPreferencesRef prefs
;
808 CFStringRef unique_id
= NULL
;
810 if ((data
!= NULL
) && !_SCUnserializeString(&unique_id
, data
, NULL
, 0)) {
814 if (unique_id
!= NULL
) {
815 if (isA_CFString(unique_id
)) {
816 prefs
= __SCHelperSessionGetPreferences(session
);
817 ok
= _SCPreferencesSystemKeychainPasswordItemRemove(prefs
, unique_id
);
823 CFRelease(unique_id
);
831 * SCHELPER_MSG_KEYCHAIN_SET
832 * (in) data = options dictionary
833 * (out) status = SCError()
837 do_keychain_set(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
840 CFStringRef description
;
841 CFArrayRef executablePaths
= NULL
;
844 CFDictionaryRef options
= NULL
;
846 SCPreferencesRef prefs
;
847 CFStringRef unique_id
;
849 if ((data
!= NULL
) && !_SCUnserialize((CFPropertyListRef
*)&options
, data
, NULL
, 0)) {
853 if (options
!= NULL
) {
854 if (!isA_CFDictionary(options
)) {
862 if (CFDictionaryGetValueIfPresent(options
,
863 kSCKeychainOptionsAllowedExecutables
,
864 (const void **)&executablePaths
)) {
865 CFMutableArrayRef executableURLs
;
868 CFMutableDictionaryRef newOptions
;
870 executableURLs
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
871 n
= CFArrayGetCount(executablePaths
);
872 for (i
= 0; i
< n
; i
++) {
876 path
= CFArrayGetValueAtIndex(executablePaths
, i
);
877 url
= CFURLCreateFromFileSystemRepresentation(NULL
,
878 CFDataGetBytePtr(path
),
879 CFDataGetLength(path
),
882 CFArrayAppendValue(executableURLs
, url
);
887 newOptions
= CFDictionaryCreateMutableCopy(NULL
, 0, options
);
888 CFDictionarySetValue(newOptions
, kSCKeychainOptionsAllowedExecutables
, executableURLs
);
889 CFRelease(executableURLs
);
892 options
= newOptions
;
895 unique_id
= CFDictionaryGetValue(options
, kSCKeychainOptionsUniqueID
);
896 label
= CFDictionaryGetValue(options
, kSCKeychainOptionsLabel
);
897 description
= CFDictionaryGetValue(options
, kSCKeychainOptionsDescription
);
898 account
= CFDictionaryGetValue(options
, kSCKeychainOptionsAccount
);
899 password
= CFDictionaryGetValue(options
, kSCKeychainOptionsPassword
);
901 prefs
= __SCHelperSessionGetPreferences(session
);
902 ok
= _SCPreferencesSystemKeychainPasswordItemSet(prefs
,
918 #endif // !TARGET_OS_IPHONE
922 * SCHELPER_MSG_INTERFACE_REFRESH
924 * (out) status = SCError()
928 do_interface_refresh(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
930 CFStringRef ifName
= NULL
;
933 if ((data
!= NULL
) && !_SCUnserializeString(&ifName
, data
, NULL
, 0)) {
934 *status
= kSCStatusInvalidArgument
;
935 SC_log(LOG_NOTICE
, "interface name not valid");
939 if (ifName
== NULL
) {
940 *status
= kSCStatusInvalidArgument
;
941 SC_log(LOG_NOTICE
, "interface name not valid");
945 if (isA_CFString(ifName
)) {
946 ok
= _SCNetworkInterfaceForceConfigurationRefresh(ifName
);
949 SC_log(LOG_NOTICE
, "interface \"%@\" not refreshed: %s",
951 SCErrorString(*status
));
954 *status
= kSCStatusInvalidArgument
;
955 SC_log(LOG_NOTICE
, "interface name not valid");
966 * (in) data = prefsID
967 * (out) status = SCError()
971 do_prefs_Open(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
974 CFDictionaryRef options
;
976 SCPreferencesRef prefs
= __SCHelperSessionGetPreferences(session
);
977 CFDictionaryRef prefsInfo
= NULL
;
979 CFStringRef prefsName
;
980 CFStringRef proc_name
;
986 if ((data
!= NULL
) && !_SCUnserialize((CFPropertyListRef
*)&prefsInfo
, data
, NULL
, 0)) {
987 SC_log(LOG_NOTICE
, "data not valid, %@", data
);
991 if ((prefsInfo
== NULL
) || !isA_CFDictionary(prefsInfo
)) {
992 SC_log(LOG_NOTICE
, "info not valid");
993 if (prefsInfo
!= NULL
) CFRelease(prefsInfo
);
997 // get [optional] prefsID
998 prefsID
= CFDictionaryGetValue(prefsInfo
, CFSTR("prefsID"));
999 prefsID
= isA_CFString(prefsID
);
1000 if (prefsID
!= NULL
) {
1001 if (CFStringHasPrefix(prefsID
, CFSTR("/")) ||
1002 CFStringHasPrefix(prefsID
, CFSTR("../")) ||
1003 CFStringHasSuffix(prefsID
, CFSTR("/..")) ||
1004 (CFStringFind(prefsID
, CFSTR("/../"), 0).location
!= kCFNotFound
)) {
1005 // if we're trying to escape from the preferences directory
1006 SC_log(LOG_NOTICE
, "prefsID (%@) not valid", prefsID
);
1007 CFRelease(prefsInfo
);
1008 *status
= kSCStatusInvalidArgument
;
1013 // get [optional] options
1014 options
= CFDictionaryGetValue(prefsInfo
, CFSTR("options"));
1015 options
= isA_CFDictionary(options
);
1017 // get preferences session "name"
1018 name
= CFDictionaryGetValue(prefsInfo
, CFSTR("name"));
1019 if (!isA_CFString(name
)) {
1020 SC_log(LOG_NOTICE
, "session \"name\" not valid");
1021 CFRelease(prefsInfo
);
1025 // get PID of caller
1026 pid
= CFDictionaryGetValue(prefsInfo
, CFSTR("PID"));
1027 if (!isA_CFNumber(pid
)) {
1028 SC_log(LOG_NOTICE
, "PID not valid");
1029 CFRelease(prefsInfo
);
1033 // get process name of caller
1034 proc_name
= CFDictionaryGetValue(prefsInfo
, CFSTR("PROC_NAME"));
1035 if (!isA_CFString(proc_name
)) {
1036 SC_log(LOG_NOTICE
, "process name not valid");
1037 CFRelease(prefsInfo
);
1041 // build [helper] preferences "name" (used for debugging) and estabish
1042 // a preferences session.
1043 prefsName
= CFStringCreateWithFormat(NULL
, NULL
,
1049 prefs
= SCPreferencesCreateWithOptions(NULL
, prefsName
, prefsID
, NULL
, options
);
1050 CFRelease(prefsName
);
1051 CFRelease(prefsInfo
);
1053 __SCHelperSessionSetPreferences(session
, prefs
);
1055 if (prefs
!= NULL
) {
1058 CFRunLoopRef rl
= CFRunLoopGetCurrent();
1060 // [temporarily] schedule notifications to ensure that we can use
1061 // the SCDynamicStore to track helper sessions
1062 ok
= SCPreferencesScheduleWithRunLoop(prefs
, rl
, kCFRunLoopDefaultMode
);
1064 (void)SCPreferencesUnscheduleFromRunLoop(prefs
, rl
, kCFRunLoopDefaultMode
);
1068 // the session now holds a references to the SCPreferencesRef
1071 *status
= SCError();
1081 * (out) status = SCError()
1082 * (out) reply = current signature + current preferences
1085 do_prefs_Access(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
1088 SCPreferencesRef prefs
= __SCHelperSessionGetPreferences(session
);
1089 CFDataRef signature
;
1091 if (prefs
== NULL
) {
1095 signature
= SCPreferencesGetSignature(prefs
);
1096 if (signature
!= NULL
) {
1097 const void * dictKeys
[2];
1098 const void * dictVals
[2];
1099 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1100 CFDictionaryRef replyDict
;
1102 dictKeys
[0] = CFSTR("signature");
1103 dictVals
[0] = signature
;
1105 dictKeys
[1] = CFSTR("preferences");
1106 dictVals
[1] = prefsPrivate
->prefs
;
1108 replyDict
= CFDictionaryCreate(NULL
,
1109 (const void **)&dictKeys
,
1110 (const void **)&dictVals
,
1111 sizeof(dictKeys
)/sizeof(dictKeys
[0]),
1112 &kCFTypeDictionaryKeyCallBacks
,
1113 &kCFTypeDictionaryValueCallBacks
);
1115 ok
= _SCSerialize(replyDict
, reply
, NULL
, NULL
);
1116 CFRelease(replyDict
);
1121 *status
= SCError();
1130 * (in) data = client prefs signature (NULL if check not needed)
1131 * (out) status = SCError()
1135 do_prefs_Lock(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
1137 CFDataRef clientSignature
= (CFDataRef
)data
;
1139 SCPreferencesRef prefs
= __SCHelperSessionGetPreferences(session
);
1140 Boolean wait
= (info
== (void *)FALSE
) ? FALSE
: TRUE
;
1142 if (prefs
== NULL
) {
1146 ok
= SCPreferencesLock(prefs
, wait
);
1148 *status
= SCError();
1152 if (clientSignature
!= NULL
) {
1153 CFDataRef serverSignature
;
1155 serverSignature
= SCPreferencesGetSignature(prefs
);
1156 if (!CFEqual(clientSignature
, serverSignature
)) {
1157 (void)SCPreferencesUnlock(prefs
);
1158 *status
= kSCStatusStale
;
1168 * (in) data = new preferences (NULL if commit w/no changes)
1169 * (out) status = SCError()
1170 * (out) reply = new signature
1173 do_prefs_Commit(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
1176 SCPreferencesRef prefs
= __SCHelperSessionGetPreferences(session
);
1177 CFPropertyListRef prefsData
= NULL
;
1178 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1179 Boolean saveAccessed
;
1180 Boolean saveChanged
;
1181 CFMutableDictionaryRef savePrefs
= NULL
;
1182 Boolean saveValid
= FALSE
;
1183 Boolean useSetFilter
;
1184 Boolean useVPNFilter
;
1185 CFArrayRef vpnTypes
= NULL
;
1187 if (prefs
== NULL
) {
1192 // if commit with no changes
1196 ok
= _SCUnserialize(&prefsData
, data
, NULL
, 0);
1201 if (!isA_CFDictionary(prefsData
)) {
1202 *status
= kSCStatusFailed
;
1207 useSetFilter
= __SCHelperSessionUseNetworkSetFilter(session
);
1208 useVPNFilter
= __SCHelperSessionUseVPNFilter(session
, &vpnTypes
);
1209 if (useSetFilter
|| useVPNFilter
) {
1212 if (prefsPrivate
->prefs
!= NULL
) {
1214 CFMutableDictionaryRef prefsNew
= NULL
;
1215 CFMutableDictionaryRef prefsOld
= NULL
;
1216 CFMutableDictionaryRef prefsSave
= prefsPrivate
->prefs
;
1218 for (c
= 0; c
< 2; c
++) {
1222 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(NULL
, 0, prefsSave
);
1225 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(NULL
, 0, prefsData
);
1230 // filter out current network set selection
1231 (void) SCPreferencesRemoveValue(prefs
, kSCPrefCurrentSet
);
1232 } else if (useVPNFilter
) {
1233 CFRange range
= CFRangeMake(0, CFArrayGetCount(vpnTypes
));
1234 CFArrayRef services
;
1236 // filter out VPN services of the specified type
1237 services
= SCNetworkServiceCopyAll(prefs
);
1238 if (services
!= NULL
) {
1240 CFIndex n
= CFArrayGetCount(services
);
1242 for (i
= 0; i
< n
; i
++) {
1243 SCNetworkServiceRef service
;
1245 service
= CFArrayGetValueAtIndex(services
, i
);
1246 if (_SCNetworkServiceIsVPN(service
)) {
1247 SCNetworkInterfaceRef child
;
1248 CFStringRef childType
= NULL
;
1249 SCNetworkInterfaceRef interface
;
1250 CFStringRef interfaceType
;
1252 interface
= SCNetworkServiceGetInterface(service
);
1253 interfaceType
= SCNetworkInterfaceGetInterfaceType(interface
);
1254 child
= SCNetworkInterfaceGetInterface(interface
);
1255 if (child
!= NULL
) {
1256 childType
= SCNetworkInterfaceGetInterfaceType(child
);
1258 if (CFEqual(interfaceType
, kSCNetworkInterfaceTypeVPN
) &&
1259 (childType
!= NULL
) &&
1260 CFArrayContainsValue(vpnTypes
, range
, childType
)) {
1261 // filter out VPN service
1262 (void) SCNetworkServiceRemove(service
);
1264 // mark all other VPN services "enabled"
1265 (void) SCNetworkServiceSetEnabled(service
, TRUE
);
1270 CFRelease(services
);
1276 prefsOld
= prefsPrivate
->prefs
;
1279 prefsNew
= prefsPrivate
->prefs
;
1284 // compare the filtered configurations
1285 ok
= _SC_CFEqual(prefsOld
, prefsNew
);
1288 if (prefsOld
!= NULL
) CFRelease(prefsOld
);
1289 if (prefsNew
!= NULL
) CFRelease(prefsNew
);
1290 prefsPrivate
->prefs
= prefsSave
;
1294 *status
= kSCStatusAccessError
;
1299 /* Take a backup of prefs, accessed bit, changed bit to
1300 restore them IFF the commit fails. Pretend as if the
1301 commit never happened!
1303 savePrefs
= prefsPrivate
->prefs
;
1304 saveAccessed
= prefsPrivate
->accessed
;
1305 saveChanged
= prefsPrivate
->changed
;
1307 prefsPrivate
->prefs
= CFDictionaryCreateMutableCopy(NULL
, 0, prefsData
);
1308 prefsPrivate
->accessed
= TRUE
;
1309 prefsPrivate
->changed
= TRUE
;
1314 ok
= SCPreferencesCommitChanges(prefs
);
1316 if (savePrefs
!= NULL
) {
1317 CFRelease(savePrefs
);
1319 *reply
= SCPreferencesGetSignature(prefs
);
1322 /* Restore the backup we took earlier */
1324 if (prefsPrivate
->prefs
!= NULL
) {
1325 CFRelease(prefsPrivate
->prefs
);
1328 prefsPrivate
->prefs
= savePrefs
;
1329 prefsPrivate
->accessed
= saveAccessed
;
1330 prefsPrivate
->changed
= saveChanged
;
1332 *status
= SCError();
1337 if (prefsData
!= NULL
) CFRelease(prefsData
);
1345 * (out) status = SCError()
1349 do_prefs_Apply(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
1352 SCPreferencesRef prefs
= __SCHelperSessionGetPreferences(session
);
1354 if (prefs
== NULL
) {
1358 ok
= SCPreferencesApplyChanges(prefs
);
1360 *status
= SCError();
1370 * (out) status = SCError()
1374 do_prefs_Unlock(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
1377 SCPreferencesRef prefs
= __SCHelperSessionGetPreferences(session
);
1379 if (prefs
== NULL
) {
1383 ok
= SCPreferencesUnlock(prefs
);
1385 *status
= SCError();
1395 * (out) status = SCError()
1399 do_prefs_Close(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
1401 SCPreferencesRef prefs
= __SCHelperSessionGetPreferences(session
);
1403 if (prefs
== NULL
) {
1407 __SCHelperSessionSetPreferences(session
, NULL
);
1416 * (out) status = kSCStatusOK
1420 do_prefs_Synchronize(SCHelperSessionRef session
, void *info
, CFDataRef data
, uint32_t *status
, CFDataRef
*reply
)
1422 SCPreferencesRef prefs
= __SCHelperSessionGetPreferences(session
);
1424 if (prefs
== NULL
) {
1428 SCPreferencesSynchronize(prefs
);
1429 *status
= kSCStatusOK
;
1435 #pragma mark Process commands
1439 sessionName(SCHelperSessionRef session
)
1441 CFStringRef name
= NULL
;
1442 SCPreferencesRef prefs
;
1444 prefs
= __SCHelperSessionGetPreferences(session
);
1445 if (prefs
!= NULL
) {
1446 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1448 name
= prefsPrivate
->name
;
1451 return (name
!= NULL
) ? name
: CFSTR("???");
1456 sessionPrefsID(SCHelperSessionRef session
)
1458 CFStringRef prefsID
;
1459 SCPreferencesPrivateRef prefsPrivate
;
1460 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
1462 prefsPrivate
= (SCPreferencesPrivateRef
)sessionPrivate
->prefs
;
1463 if ((prefsPrivate
!= NULL
) && (prefsPrivate
->prefsID
!= NULL
)) {
1464 prefsID
= prefsPrivate
->prefsID
;
1466 prefsID
= PREFS_DEFAULT_CONFIG
;
1474 copyEntitlement(SCHelperSessionRef session
, CFStringRef entitlement
)
1476 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
1478 CFTypeRef value
= NULL
;
1480 // Create the security task from the audit token
1481 task
= SecTaskCreateWithAuditToken(NULL
, sessionPrivate
->auditToken
);
1483 CFErrorRef error
= NULL
;
1485 // Get the value for the entitlement
1486 value
= SecTaskCopyValueForEntitlement(task
, entitlement
, &error
);
1487 if ((value
== NULL
) && (error
!= NULL
)) {
1488 CFIndex code
= CFErrorGetCode(error
);
1489 CFStringRef domain
= CFErrorGetDomain(error
);
1491 if (!CFEqual(domain
, kCFErrorDomainMach
) ||
1492 ((code
!= kIOReturnInvalid
) && (code
!= kIOReturnNotFound
))) {
1493 // if unexpected error
1494 SC_log(LOG_NOTICE
, "SecTaskCopyValueForEntitlement(,\"%@\",) failed, error = %@ : %@",
1497 sessionName(session
));
1504 SC_log(LOG_NOTICE
, "SecTaskCreateWithAuditToken() failed: %@",
1505 sessionName(session
));
1512 #if !TARGET_OS_IPHONE
1514 isSetChange(SCHelperSessionRef session
)
1516 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
1518 if (sessionPrivate
->isSetChange
== UNKNOWN
) {
1519 CFBooleanRef bVal
= NULL
;
1520 CFStringRef prefsID
;
1521 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)sessionPrivate
->prefs
;
1522 Boolean setFilter
= FALSE
;
1524 prefsID
= sessionPrefsID(session
);
1525 if (CFEqual(prefsID
, PREFS_DEFAULT_CONFIG
) &&
1526 isA_CFDictionary(prefsPrivate
->options
) &&
1527 CFDictionaryGetValueIfPresent(prefsPrivate
->options
,
1528 kSCPreferencesOptionChangeNetworkSet
,
1529 (const void **)&bVal
) &&
1530 isA_CFBoolean(bVal
) &&
1531 CFBooleanGetValue(bVal
)) {
1535 // establish network set (location) filter
1536 __SCHelperSessionSetNetworkSetFilter(session
, setFilter
);
1539 return (sessionPrivate
->isSetChange
== YES
) ? TRUE
: FALSE
;
1541 #endif // !TARGET_OS_IPHONE
1545 isVPNChange(SCHelperSessionRef session
)
1547 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
1549 if (sessionPrivate
->isVPNChange
== UNKNOWN
) {
1550 CFArrayRef entitlement
;
1551 Boolean vpnChange
= FALSE
;
1552 CFArrayRef vpnTypes
= NULL
;
1554 entitlement
= copyEntitlement(session
, kSCVPNFilterEntitlementName
);
1555 if (entitlement
!= NULL
) {
1556 if (isA_CFArray(entitlement
)) {
1557 CFStringRef prefsID
;
1559 prefsID
= sessionPrefsID(session
);
1560 if (CFEqual(prefsID
, PREFS_DEFAULT_CONFIG
)) {
1561 // save the VPN type identifiers
1562 vpnTypes
= CFRetain(entitlement
);
1564 // grant an exception
1566 } else if (CFStringHasPrefix(prefsID
, CFSTR("VPN-")) &&
1567 CFStringHasSuffix(prefsID
, CFSTR(".plist"))) {
1571 range
.location
= sizeof("VPN-") - 1;
1572 range
.length
= CFStringGetLength(prefsID
)
1573 - (sizeof("VPN-") - 1) // trim VPN-
1574 - (sizeof(".plist") - 1); // trim .plist
1575 vpnID
= CFStringCreateWithSubstring(NULL
, prefsID
, range
);
1576 if (CFArrayContainsValue(entitlement
,
1577 CFRangeMake(0, CFArrayGetCount(entitlement
)),
1579 // grant an exception
1586 CFRelease(entitlement
);
1589 __SCHelperSessionSetVPNFilter(session
, vpnChange
, vpnTypes
);
1590 if (vpnTypes
!= NULL
) {
1591 CFRelease(vpnTypes
);
1595 return (sessionPrivate
->isVPNChange
== YES
) ? TRUE
: FALSE
;
1600 checkEntitlement(SCHelperSessionRef session
, CFStringRef prefsID
, CFStringRef entitlement_name
)
1602 CFArrayRef entitlement
;
1603 Boolean hasEntitlement
= FALSE
;
1605 entitlement
= copyEntitlement(session
, entitlement_name
);
1606 if (entitlement
!= NULL
) {
1607 if (isA_CFArray(entitlement
)) {
1608 if (CFArrayContainsValue(entitlement
,
1609 CFRangeMake(0, CFArrayGetCount(entitlement
)),
1611 // if client DOES have entitlement
1612 hasEntitlement
= TRUE
;
1615 SC_log(LOG_NOTICE
, "hasAuthorization() session=%@: entitlement=%@: not valid",
1616 sessionName(session
),
1620 CFRelease(entitlement
);
1623 #if TARGET_OS_IPHONE
1624 // make an exception for VPN configuration management
1625 if (!hasEntitlement
) {
1626 if (isVPNChange(session
)) {
1627 // grant a "filtered" exception
1628 hasEntitlement
= TRUE
;
1631 #endif // TARGET_OS_IPHONE
1633 return hasEntitlement
;
1638 hasAuthorization(SCHelperSessionRef session
, Boolean needWrite
)
1640 AuthorizationRef authorization
= __SCHelperSessionGetAuthorization(session
);
1641 CFStringRef prefsID
;
1642 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
1644 if (authorization
== NULL
) {
1648 #if !TARGET_OS_IPHONE
1649 if (!__SCHelperSessionUseEntitlement(session
)) {
1650 AuthorizationFlags flags
;
1651 AuthorizationItem items
[1];
1652 AuthorizationRights rights
;
1655 if (isSetChange(session
)) {
1656 items
[0].name
= kSCPreferencesAuthorizationRight_network_set
;
1657 items
[0].value
= NULL
;
1658 items
[0].valueLength
= 0;
1660 } else if (isVPNChange(session
)) {
1661 items
[0].name
= kSCPreferencesAuthorizationRight_write
;
1662 items
[0].value
= NULL
;
1663 items
[0].valueLength
= 0;
1666 items
[0].name
= kSCPreferencesAuthorizationRight_write
;
1667 items
[0].value
= NULL
;
1668 items
[0].valueLength
= 0;
1672 rights
.count
= sizeof(items
) / sizeof(items
[0]);
1673 rights
.items
= items
;
1675 flags
= kAuthorizationFlagDefaults
;
1676 flags
|= kAuthorizationFlagInteractionAllowed
;
1677 flags
|= kAuthorizationFlagExtendRights
;
1678 // flags |= kAuthorizationFlagPartialRights;
1679 // flags |= kAuthorizationFlagPreAuthorize;
1681 status
= AuthorizationCopyRights(authorization
,
1683 kAuthorizationEmptyEnvironment
,
1686 if (status
!= errAuthorizationSuccess
) {
1687 SC_log(LOG_INFO
, "AuthorizationCopyRights() failed: status = %d",
1694 #endif // !TARGET_OS_IPHONE
1696 prefsID
= sessionPrefsID(session
);
1698 if (sessionPrivate
->callerWriteAccess
== UNKNOWN
) {
1699 if (checkEntitlement(session
, prefsID
, kSCWriteEntitlementName
)) {
1700 sessionPrivate
->callerWriteAccess
= YES
;
1701 sessionPrivate
->callerReadAccess
= YES
; // implied
1703 sessionPrivate
->callerWriteAccess
= NO
;
1708 if (sessionPrivate
->callerWriteAccess
== YES
) {
1711 SC_log(LOG_NOTICE
, "SCPreferences write access to \"%@\" denied, no entitlement for \"%@\"",
1713 sessionName(session
));
1718 if (sessionPrivate
->callerReadAccess
== UNKNOWN
) {
1719 if (checkEntitlement(session
, prefsID
, kSCReadEntitlementName
)) {
1720 sessionPrivate
->callerReadAccess
= YES
;
1722 sessionPrivate
->callerWriteAccess
= NO
;
1726 if (sessionPrivate
->callerReadAccess
== YES
) {
1730 SC_log(LOG_NOTICE
, "SCPreferences access to \"%@\" denied, no entitlement for \"%@\"",
1732 sessionName(session
));
1737 typedef Boolean (*helperFunction
) (SCHelperSessionRef session
,
1744 static const struct helper
{
1746 const char *commandName
;
1747 Boolean needsAuthorization
;
1749 helperFunction func
;
1752 { SCHELPER_MSG_AUTH
, "AUTH", FALSE
, FALSE
, do_Auth
, NULL
},
1754 { SCHELPER_MSG_PREFS_OPEN
, "PREFS open", FALSE
, FALSE
, do_prefs_Open
, NULL
},
1755 { SCHELPER_MSG_PREFS_ACCESS
, "PREFS access", TRUE
, FALSE
, do_prefs_Access
, NULL
},
1756 { SCHELPER_MSG_PREFS_LOCK
, "PREFS lock", TRUE
, TRUE
, do_prefs_Lock
, (void *)FALSE
},
1757 { SCHELPER_MSG_PREFS_LOCKWAIT
, "PREFS lock/wait", TRUE
, TRUE
, do_prefs_Lock
, (void *)TRUE
},
1758 { SCHELPER_MSG_PREFS_COMMIT
, "PREFS commit", TRUE
, TRUE
, do_prefs_Commit
, NULL
},
1759 { SCHELPER_MSG_PREFS_APPLY
, "PREFS apply", TRUE
, TRUE
, do_prefs_Apply
, NULL
},
1760 { SCHELPER_MSG_PREFS_UNLOCK
, "PREFS unlock", FALSE
, TRUE
, do_prefs_Unlock
, NULL
},
1761 { SCHELPER_MSG_PREFS_CLOSE
, "PREFS close", FALSE
, FALSE
, do_prefs_Close
, NULL
},
1762 { SCHELPER_MSG_PREFS_SYNCHRONIZE
, "PREFS synchronize", FALSE
, FALSE
, do_prefs_Synchronize
, NULL
},
1764 { SCHELPER_MSG_INTERFACE_REFRESH
, "INTERFACE refresh", TRUE
, TRUE
, do_interface_refresh
, NULL
},
1766 #if !TARGET_OS_IPHONE
1767 { SCHELPER_MSG_KEYCHAIN_COPY
, "KEYCHAIN copy", TRUE
, FALSE
, do_keychain_copy
, NULL
},
1768 { SCHELPER_MSG_KEYCHAIN_EXISTS
, "KEYCHAIN exists", TRUE
, FALSE
, do_keychain_exists
, NULL
},
1769 { SCHELPER_MSG_KEYCHAIN_REMOVE
, "KEYCHAIN remove", TRUE
, TRUE
, do_keychain_remove
, NULL
},
1770 { SCHELPER_MSG_KEYCHAIN_SET
, "KEYCHAIN set", TRUE
, TRUE
, do_keychain_set
, NULL
},
1771 #endif // !TARGET_OS_IPHONE
1773 { SCHELPER_MSG_EXIT
, "EXIT", FALSE
, FALSE
, do_Exit
, NULL
}
1775 #define nHELPERS (sizeof(helpers)/sizeof(struct helper))
1779 findCommand(uint32_t command
)
1783 for (i
= 0; i
< (int)nHELPERS
; i
++) {
1784 if (helpers
[i
].command
== command
) {
1794 newHelper(void *arg
)
1796 CFRunLoopSourceRef rls
= NULL
;
1797 SCHelperSessionRef session
= (SCHelperSessionRef
)arg
;
1798 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
1800 assert(session
!= NULL
);
1801 assert(sessionPrivate
->mp
!= NULL
);
1803 __SCHelperSessionSetThreadName(session
);
1805 rls
= CFMachPortCreateRunLoopSource(NULL
, sessionPrivate
->mp
, 0);
1806 CFRelease(sessionPrivate
->mp
);
1809 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
1812 SC_log(LOG_INFO
, "%p : start", session
);
1814 SC_log(LOG_INFO
, "%p : stop", session
);
1822 #pragma mark Main loop
1825 // MiG generated externals and functions
1826 extern struct mig_subsystem _helper_subsystem
;
1827 extern boolean_t
helper_server(mach_msg_header_t
*, mach_msg_header_t
*);
1832 notify_server(mach_msg_header_t
*request
, mach_msg_header_t
*reply
)
1834 mach_no_senders_notification_t
*Request
= (mach_no_senders_notification_t
*)request
;
1835 mig_reply_error_t
*Reply
= (mig_reply_error_t
*)reply
;
1837 reply
->msgh_bits
= MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(request
->msgh_bits
), 0);
1838 reply
->msgh_remote_port
= request
->msgh_remote_port
;
1839 reply
->msgh_size
= sizeof(mig_reply_error_t
); /* Minimal size: update as needed */
1840 reply
->msgh_local_port
= MACH_PORT_NULL
;
1841 reply
->msgh_id
= request
->msgh_id
+ 100;
1843 if ((Request
->not_header
.msgh_id
> MACH_NOTIFY_LAST
) ||
1844 (Request
->not_header
.msgh_id
< MACH_NOTIFY_FIRST
)) {
1845 Reply
->NDR
= NDR_record
;
1846 Reply
->RetCode
= MIG_BAD_ID
;
1847 return FALSE
; /* if this is not a notification message */
1850 switch (Request
->not_header
.msgh_id
) {
1851 case MACH_NOTIFY_NO_SENDERS
: {
1852 SCHelperSessionRef session
;
1854 __MACH_PORT_DEBUG(TRUE
, "*** notify_server MACH_NOTIFY_NO_SENDERS", Request
->not_header
.msgh_local_port
);
1857 session
= __SCHelperSessionFindWithPort(Request
->not_header
.msgh_local_port
);
1858 if (session
!= NULL
) {
1859 SCHelperSessionPrivateRef sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
1861 // release CFMachPort *and* SCHelperSession
1862 CFMachPortInvalidate(sessionPrivate
->mp
);
1865 __MACH_PORT_DEBUG(TRUE
, "*** notify_server after invalidate", Request
->not_header
.msgh_local_port
);
1867 // and, lastly, remove our receive right.
1868 (void) mach_port_mod_refs(mach_task_self(),
1869 Request
->not_header
.msgh_local_port
,
1870 MACH_PORT_RIGHT_RECEIVE
, -1);
1872 Reply
->Head
.msgh_bits
= 0;
1873 Reply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
1874 Reply
->RetCode
= KERN_SUCCESS
;
1882 SC_log(LOG_NOTICE
, "HELP!, Received notification: port=%d, msgh_id=%d",
1883 Request
->not_header
.msgh_local_port
,
1884 Request
->not_header
.msgh_id
);
1886 Reply
->NDR
= NDR_record
;
1887 Reply
->RetCode
= MIG_BAD_ID
;
1888 return FALSE
; /* if this is not a notification we are handling */
1894 helper_demux(mach_msg_header_t
*request
, mach_msg_header_t
*reply
)
1896 Boolean processed
= FALSE
;
1899 * (attempt to) process SCHelper requests.
1901 processed
= helper_server(request
, reply
);
1907 * (attempt to) process (NO MORE SENDERS) notification messages.
1909 processed
= notify_server(request
, reply
);
1915 * unknown message ID, log and return an error.
1917 SC_log(LOG_NOTICE
, "helper_demux(): unknown message ID (%d) received", request
->msgh_id
);
1918 reply
->msgh_bits
= MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(request
->msgh_bits
), 0);
1919 reply
->msgh_remote_port
= request
->msgh_remote_port
;
1920 reply
->msgh_size
= sizeof(mig_reply_error_t
); /* Minimal size */
1921 reply
->msgh_local_port
= MACH_PORT_NULL
;
1922 reply
->msgh_id
= request
->msgh_id
+ 100;
1923 ((mig_reply_error_t
*)reply
)->NDR
= NDR_record
;
1924 ((mig_reply_error_t
*)reply
)->RetCode
= MIG_BAD_ID
;
1930 #define MACH_MSG_BUFFER_SIZE 128
1934 helperCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
1936 mig_reply_error_t
* bufRequest
= msg
;
1937 uint32_t bufReply_q
[MACH_MSG_BUFFER_SIZE
/sizeof(uint32_t)];
1938 mig_reply_error_t
* bufReply
= (mig_reply_error_t
*)bufReply_q
;
1939 static CFIndex bufSize
= 0;
1940 mach_msg_return_t mr
;
1944 // get max size for MiG reply buffers
1945 bufSize
= _helper_subsystem
.maxsize
;
1947 // check if our on-the-stack reply buffer will be big enough
1948 if (bufSize
> sizeof(bufReply_q
)) {
1949 SC_log(LOG_NOTICE
, "buffer size should be increased > %d",
1950 _helper_subsystem
.maxsize
);
1954 if (bufSize
> sizeof(bufReply_q
)) {
1955 bufReply
= CFAllocatorAllocate(NULL
, _helper_subsystem
.maxsize
, 0);
1957 bufReply
->RetCode
= 0;
1959 /* we have a request message */
1960 (void) helper_demux(&bufRequest
->Head
, &bufReply
->Head
);
1962 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
1963 if (bufReply
->RetCode
== MIG_NO_REPLY
) {
1964 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
1965 } else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
1966 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
1968 * destroy the request - but not the reply port
1970 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
1971 mach_msg_destroy(&bufRequest
->Head
);
1975 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
1979 * We don't want to block indefinitely because the client
1980 * isn't receiving messages from the reply port.
1981 * If we have a send-once right for the reply port, then
1982 * this isn't a concern because the send won't block.
1983 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
1984 * To avoid falling off the kernel's fast RPC path unnecessarily,
1985 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
1988 options
= MACH_SEND_MSG
;
1989 if (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) != MACH_MSG_TYPE_MOVE_SEND_ONCE
) {
1990 options
|= MACH_SEND_TIMEOUT
;
1992 mr
= mach_msg(&bufReply
->Head
, /* msg */
1993 options
, /* option */
1994 bufReply
->Head
.msgh_size
, /* send_size */
1996 MACH_PORT_NULL
, /* rcv_name */
1997 MACH_MSG_TIMEOUT_NONE
, /* timeout */
1998 MACH_PORT_NULL
); /* notify */
2000 /* Has a message error occurred? */
2002 case MACH_SEND_INVALID_DEST
:
2003 case MACH_SEND_TIMED_OUT
:
2006 /* Includes success case. */
2011 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
2012 mach_msg_destroy(&bufReply
->Head
);
2017 if (bufReply
!= (mig_reply_error_t
*)bufReply_q
)
2018 CFAllocatorDeallocate(NULL
, bufReply
);
2025 initMPCopyDescription(const void *info
)
2027 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<SCHelper MP>"));
2033 _helperinit(mach_port_t server
,
2034 mach_port_t
*newSession
,
2036 audit_token_t audit_token
)
2038 CFMachPortContext context
= { 0
2042 , initMPCopyDescription
2045 mach_port_t oldNotify
;
2046 SCHelperSessionRef session
;
2047 SCHelperSessionPrivateRef sessionPrivate
;
2048 pthread_attr_t tattr
;
2051 session
= __SCHelperSessionFindWithPort(server
);
2052 if (session
!= NULL
) {
2054 SC_log(LOG_DEBUG
, "session is already open");
2056 *status
= kSCStatusFailed
; /* you can't re-open an "open" session */
2057 return KERN_SUCCESS
;
2060 session
= __SCHelperSessionCreate(NULL
);
2061 assert(session
!= NULL
);
2062 sessionPrivate
= (SCHelperSessionPrivateRef
)session
;
2064 // create per-session port
2065 kr
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &sessionPrivate
->port
);
2066 if (kr
!= KERN_SUCCESS
) {
2067 SC_log(LOG_ERR
, "mach_port_allocate() failed: %s", mach_error_string(kr
));
2072 *newSession
= sessionPrivate
->port
;
2074 (void) mach_port_set_attributes(mach_task_self(),
2076 MACH_PORT_IMPORTANCE_RECEIVER
,
2081 // Note: we create the CFMachPort *before* we insert a send
2082 // right present to ensure that CF does not establish
2083 // its dead name notification.
2085 context
.info
= (void *)session
;
2086 sessionPrivate
->mp
= _SC_CFMachPortCreateWithPort("SCHelper/session",
2091 /* Request a notification when/if the client dies */
2092 kr
= mach_port_request_notification(mach_task_self(),
2094 MACH_NOTIFY_NO_SENDERS
,
2097 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
2099 if (kr
!= KERN_SUCCESS
) {
2100 SC_log(LOG_NOTICE
, "mach_port_request_notification() failed: %s", mach_error_string(kr
));
2102 // clean up CFMachPort, mach port rights
2103 CFMachPortInvalidate(sessionPrivate
->mp
);
2104 CFRelease(sessionPrivate
->mp
);
2105 sessionPrivate
->mp
= NULL
;
2106 (void) mach_port_mod_refs(mach_task_self(), *newSession
, MACH_PORT_RIGHT_RECEIVE
, -1);
2107 *newSession
= MACH_PORT_NULL
;
2108 *status
= kSCStatusFailed
;
2112 if (oldNotify
!= MACH_PORT_NULL
) {
2113 SC_log(LOG_NOTICE
, "oldNotify != MACH_PORT_NULL");
2116 // add send right (that will be passed back to the client)
2117 (void) mach_port_insert_right(mach_task_self(),
2120 MACH_MSG_TYPE_MAKE_SEND
);
2123 sessionPrivate
->auditToken
= audit_token
;
2126 // Note: at this time we should be holding ONE send right and
2127 // ONE receive right to the server. The send right is
2128 // moved to the caller.
2131 // start per-session thread
2132 pthread_attr_init(&tattr
);
2133 pthread_attr_setscope(&tattr
, PTHREAD_SCOPE_SYSTEM
);
2134 pthread_attr_setdetachstate(&tattr
, PTHREAD_CREATE_DETACHED
);
2135 pthread_attr_setstacksize(&tattr
, 96 * 1024); // each thread gets a 96K stack
2136 pthread_create(&tid
, &tattr
, newHelper
, (void *)session
);
2137 pthread_attr_destroy(&tattr
);
2139 *status
= kSCStatusOK
;
2144 return KERN_SUCCESS
;
2150 _helperexec(mach_port_t server
,
2152 xmlData_t dataRef
, /* raw XML bytes */
2153 mach_msg_type_number_t dataLen
,
2154 xmlData_t traceRef
, /* raw XML bytes */
2155 mach_msg_type_number_t traceLen
,
2157 xmlDataOut_t
*replyRef
, /* raw XML bytes */
2158 mach_msg_type_number_t
*replyLen
)
2160 CFStringRef backtrace
= NULL
;
2161 CFDataRef data
= NULL
;
2163 CFDataRef reply
= NULL
;
2164 SCHelperSessionRef session
;
2166 *status
= kSCStatusOK
;
2170 if ((dataRef
!= NULL
) && (dataLen
> 0)) {
2171 if (!_SCUnserializeData(&data
, (void *)dataRef
, dataLen
)) {
2172 *status
= SCError();
2176 if ((traceRef
!= NULL
) && (traceLen
> 0)) {
2177 if (!_SCUnserializeString(&backtrace
, NULL
, (void *)traceRef
, traceLen
)) {
2178 *status
= SCError();
2182 if (*status
!= kSCStatusOK
) {
2186 session
= __SCHelperSessionFindWithPort(server
);
2187 if (session
== NULL
) {
2188 *status
= kSCStatusFailed
; /* you must have an open session to play */
2192 i
= findCommand(msgID
);
2194 SC_log(LOG_NOTICE
, "received unknown command : %u", msgID
);
2195 *status
= kSCStatusInvalidArgument
;
2199 SC_log(LOG_INFO
, "%p : processing command \"%s\"%s",
2201 helpers
[i
].commandName
,
2202 (data
!= NULL
) ? " w/data" : "");
2204 if (helpers
[i
].needsAuthorization
&&
2205 !hasAuthorization(session
, helpers
[i
].needsWrite
)) {
2206 SC_log(LOG_INFO
, "%p : command \"%s\" : not authorized",
2208 helpers
[i
].commandName
);
2209 *status
= kSCStatusAccessError
;
2212 if (*status
== kSCStatusOK
) {
2213 if (backtrace
!= NULL
) {
2214 __SCHelperSessionAddBacktrace(session
, backtrace
, helpers
[i
].commandName
);
2216 (*helpers
[i
].func
)(session
, helpers
[i
].info
, data
, status
, &reply
);
2219 if ((*status
!= -1) || (reply
!= NULL
)) {
2222 SC_log(LOG_INFO
, "%p : sending status %u%s",
2225 (reply
!= NULL
) ? " w/reply" : "");
2227 /* serialize the data */
2228 if (reply
!= NULL
) {
2231 ok
= _SCSerializeData(reply
, (void **)replyRef
, &len
);
2232 *replyLen
= (mach_msg_type_number_t
)len
;
2234 *status
= SCError();
2242 if (data
!= NULL
) CFRelease(data
);
2243 if (backtrace
!= NULL
) CFRelease(backtrace
);
2244 if (reply
!= NULL
) CFRelease(reply
);
2245 return KERN_SUCCESS
;
2250 helperMPCopyDescription(const void *info
)
2252 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<main SCHelper MP>"));
2257 init_MiG(const char *service_name
, int *n_listeners
)
2259 CFMachPortContext context
= { 0
2263 , helperMPCopyDescription
2267 CFRunLoopSourceRef rls
;
2268 mach_port_t service_port
= MACH_PORT_NULL
;
2270 kr
= bootstrap_check_in(bootstrap_port
, service_name
, &service_port
);
2271 if (kr
!= BOOTSTRAP_SUCCESS
) {
2272 SC_log(LOG_NOTICE
, "bootstrap_check_in() failed: %s",
2273 bootstrap_strerror(kr
));
2277 // add a run loop source to listen for new requests
2278 mp
= _SC_CFMachPortCreateWithPort("SCHelper/server",
2282 rls
= CFMachPortCreateRunLoopSource(NULL
, mp
, 0);
2284 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
2287 *n_listeners
= *n_listeners
+ 1;
2297 static const struct option longopts
[] = {
2298 { "debug", no_argument
, 0, 'd' },
2304 main(int argc
, char **argv
)
2306 Boolean done
= FALSE
;
2308 int gen_reported
= 0;
2310 int n_listeners
= 0;
2311 // extern int optind;
2315 openlog("SCHelper", LOG_CONS
|LOG_PID
, LOG_DAEMON
);
2317 // process any arguments
2318 while ((opt
= getopt_long(argc
, argv
, "d", longopts
, &opti
)) != -1) {
2324 // if (strcmp(longopts[opti].name, "debug") == 1) {
2329 SC_log(LOG_NOTICE
, "ignoring unknown or ambiguous command line option");
2336 if (geteuid() != 0) {
2337 SC_log(LOG_NOTICE
, "%s", strerror(EACCES
));
2341 main_runLoop
= CFRunLoopGetCurrent();
2343 err
= init_MiG("com.apple.SystemConfiguration.helper", &n_listeners
);
2344 if ((err
!= 0) || (n_listeners
== 0)) {
2348 pthread_setname_np("SCHelper main thread");
2354 rlStatus
= CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 15.0, TRUE
);
2356 pthread_mutex_lock(&sessions_lock
);
2358 if (sessions
!= NULL
) {
2359 if (rlStatus
== kCFRunLoopRunTimedOut
) {
2362 if ((CFSetGetCount(sessions
) == 0) && (sessions_closed
== 0)) {
2363 // if we don't have any open sessions and no
2364 // sessions have recently been closed
2371 gen_current
= sessions_generation
;
2372 sessions_closed
= 0;
2374 if (!done
&& (idle
>= (2 * 60 / 15))) {
2375 if (gen_reported
!= gen_current
) {
2376 FILE *logFile
= NULL
;
2378 SC_log(LOG_INFO
, "active (but IDLE) sessions");
2379 CFSetApplyFunction(sessions
, __SCHelperSessionLog
, (void *)&logFile
);
2380 gen_reported
= gen_current
;
2382 if (logFile
!= NULL
) {
2383 (void) fclose(logFile
);
2389 pthread_mutex_unlock(&sessions_lock
);