Something went wrong. Try again.
Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039/* * Copyright (c) 2006, 2007, 2010, 2014, 2016-2018 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */
/* * Modification History * * May 24, 2006 Allan Nathanson (ajn@apple.com) * - adapted (for SystemConfiguration) * * May 10, 2006 Dieter Siegmund (dieter@apple.com) * - created (for EAP) */
#include <os/availability.h>#include <TargetConditionals.h>#include <sys/param.h>#include <CoreFoundation/CoreFoundation.h>#include <CoreFoundation/CFBundlePriv.h> // for _CFBundleCopyMainBundleExecutableURL#include "dy_framework.h"
#include "SCPreferencesInternal.h"
#if !TARGET_OS_IPHONEstatic CFDataRefcopyMyExecutablePath(void){ char fspath[MAXPATHLEN]; Boolean isBundle = FALSE; Boolean ok; CFDataRef path = NULL; CFURLRef url;
url = _CFBundleCopyMainBundleExecutableURL(&isBundle); if (url == NULL) { return NULL; }
ok = CFURLGetFileSystemRepresentation(url, TRUE, (UInt8 *)fspath, sizeof(fspath)); CFRelease(url); if (!ok) { return NULL; } fspath[sizeof(fspath) - 1] = '\0';
if (isBundle) { const char *slash;
slash = strrchr(fspath, '/'); if (slash != NULL) { const char *contents;
contents = strstr(fspath, "/Contents/MacOS/"); if ((contents != NULL) && ((contents + sizeof("/Contents/MacOS/") - 1) == slash)) { path = CFDataCreate(NULL, (UInt8 *)fspath, contents - fspath); goto done; } } }
path = CFDataCreate(NULL, (UInt8 *)fspath, strlen(fspath));
done :
return path;}
#pragma mark -#pragma mark Keychain helper APIs
#if (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070)/* * Create a SecAccessRef with a custom form. * * Both the owner and the ACL set allow free access to root, * but nothing to anyone else. * * NOTE: This is not the easiest way to build up CSSM data structures * but it is a way that does not depend on any outside software * layers (other than CSSM and Security's Sec* layer, of course). */static SecAccessRef_SCSecAccessCreateForUID(uid_t uid){ SecAccessRef access = NULL; OSStatus status;
// make the "uid/gid" ACL subject // this is a CSSM_LIST_ELEMENT chain
CSSM_ACL_PROCESS_SUBJECT_SELECTOR selector = { CSSM_ACL_PROCESS_SELECTOR_CURRENT_VERSION, // version CSSM_ACL_MATCH_UID, // active fields mask: match uids (only) uid, // effective user id to match 0 // effective group id to match };
CSSM_LIST_ELEMENT subject2 = { NULL, // NextElement 0 // WordID // rest is defaulted };
subject2.Element.Word.Data = (UInt8 *)&selector; subject2.Element.Word.Length = sizeof(selector);
CSSM_LIST_ELEMENT subject1 = { &subject2, // NextElement CSSM_ACL_SUBJECT_TYPE_PROCESS, // WordID CSSM_LIST_ELEMENT_WORDID // ElementType // rest is defaulted };
// rights granted (replace with individual list if desired) CSSM_ACL_AUTHORIZATION_TAG rights[] = { CSSM_ACL_AUTHORIZATION_ANY // everything };
// owner component (right to change ACL) CSSM_ACL_OWNER_PROTOTYPE owner = { { // TypedSubject CSSM_LIST_TYPE_UNKNOWN, // type of this list &subject1, // head of the list &subject2 // tail of the list }, FALSE // Delegate };
// ACL entries (any number, just one here) CSSM_ACL_ENTRY_INFO acls[] = { { { // EntryPublicInfo { // TypedSubject CSSM_LIST_TYPE_UNKNOWN, // type of this list &subject1, // head of the list &subject2 // tail of the list }, FALSE, // Delegate { // Authorization sizeof(rights) / sizeof(rights[0]), // NumberOfAuthTags rights // AuthTags }, { // TimeRange }, { // EntryTag } }, 0 // EntryHandle } };
status = SecAccessCreateFromOwnerAndACL(&owner, sizeof(acls) / sizeof(acls[0]), acls, &access); if (status != noErr) { _SCErrorSet(status); }
return access;}#endif // (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070)
// one example would be to pass a URL for "/System/Library/CoreServices/SystemUIServer.app"static SecAccessRef_SCSecAccessCreateForExecutables(CFStringRef label, CFArrayRef executableURLs){ SecAccessRef access = NULL; CFIndex i; CFIndex n; OSStatus status; SecTrustedApplicationRef trustedApplication; CFMutableArrayRef trustedApplications;
trustedApplications = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
// Use default access ("confirm access")
// Next, we make an exception list of applications you want to trust. // These applications will be allowed to access the item without requiring // user confirmation.
// Trust the calling application status = SecTrustedApplicationCreateFromPath(NULL, &trustedApplication); if (status == noErr) { CFArrayAppendValue(trustedApplications, trustedApplication); CFRelease(trustedApplication); }
n = (executableURLs != NULL) ? CFArrayGetCount(executableURLs) : 0; for (i = 0; i < n; i++) { Boolean ok; char path[MAXPATHLEN]; CFURLRef url;
url = CFArrayGetValueAtIndex(executableURLs, i); ok = CFURLGetFileSystemRepresentation(url, TRUE, (UInt8 *)path, sizeof(path)); if (!ok) { continue; }
status = SecTrustedApplicationCreateFromPath(path, &trustedApplication); if (status == noErr) { CFArrayAppendValue(trustedApplications, trustedApplication); CFRelease(trustedApplication); } }
status = SecAccessCreate(label, trustedApplications, &access); if (status != noErr) { goto done; }
done :
CFRelease(trustedApplications);
return access;}#endif // !TARGET_OS_IPHONE
SecKeychainRef_SCSecKeychainCopySystemKeychain(void){#if !TARGET_OS_IPHONE SecKeychainRef keychain = NULL; OSStatus status;
status = SecKeychainCopyDomainDefault(kSecPreferencesDomainSystem, &keychain); if (status != noErr) { _SCErrorSet(status); if (keychain != NULL) CFRelease(keychain); return NULL; }
return keychain;#else // !TARGET_OS_IPHONE _SCErrorSet(kSCStatusAccessError); return NULL;#endif // !TARGET_OS_IPHONE}
#if !TARGET_OS_IPHONEstatic OSStatusfindKeychainItem(SecKeychainRef keychain, CFStringRef unique_id, SecKeychainItemRef *item){ CFMutableDictionaryRef query; OSStatus status;
query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); if (keychain != NULL) { CFArrayRef keychains;
keychains = CFArrayCreate(NULL, (const void **)&keychain, 1, &kCFTypeArrayCallBacks); CFDictionarySetValue(query, kSecMatchSearchList, keychains); CFRelease(keychains); } CFDictionarySetValue(query, kSecClass , kSecClassGenericPassword); CFDictionarySetValue(query, kSecAttrService, unique_id); CFDictionarySetValue(query, kSecReturnRef , kCFBooleanTrue); status = SecItemCopyMatching(query, (CFTypeRef *)item); CFRelease(query);
return status;}#endif // !TARGET_OS_IPHONE
CFDataRef_SCSecKeychainPasswordItemCopy(SecKeychainRef keychain, CFStringRef unique_id){#if !TARGET_OS_IPHONE SecKeychainItemRef item = NULL; CFDataRef keychain_password = NULL; OSStatus status;
status = findKeychainItem(keychain, unique_id, &item); if (status == noErr) { void * pw = NULL; UInt32 pw_len = 0;
status = SecKeychainItemCopyContent(item, NULL, NULL, &pw_len, &pw); if (status == noErr) { keychain_password = CFDataCreate(NULL, pw, pw_len); status = SecKeychainItemFreeContent(NULL, pw); } } if (item != NULL) CFRelease(item); if (status != noErr) { _SCErrorSet(status); }
return keychain_password;#else // !TARGET_OS_IPHONE#pragma unused(keychain)#pragma unused(unique_id) _SCErrorSet(kSCStatusAccessError); return NULL;#endif // !TARGET_OS_IPHONE}
Boolean_SCSecKeychainPasswordItemExists(SecKeychainRef keychain, CFStringRef unique_id){#if !TARGET_OS_IPHONE SecKeychainItemRef item; OSStatus status;
status = findKeychainItem(keychain, unique_id, &item); if (status != noErr) { _SCErrorSet(status); return FALSE; }
CFRelease(item); return TRUE;#else // !TARGET_OS_IPHONE#pragma unused(keychain)#pragma unused(unique_id) _SCErrorSet(kSCStatusAccessError); return FALSE;#endif // !TARGET_OS_IPHONE}
Boolean_SCSecKeychainPasswordItemRemove(SecKeychainRef keychain, CFStringRef unique_id){#if !TARGET_OS_IPHONE SecKeychainItemRef item; OSStatus status;
status = findKeychainItem(keychain, unique_id, &item); if (status != noErr) { _SCErrorSet(status); return FALSE; }
status = SecKeychainItemDelete(item); CFRelease(item); if (status != noErr) { _SCErrorSet(status); return FALSE; }
return TRUE;#else // !TARGET_OS_IPHONE#pragma unused(keychain)#pragma unused(unique_id) _SCErrorSet(kSCStatusAccessError); return FALSE;#endif // !TARGET_OS_IPHONE}
Boolean_SCSecKeychainPasswordItemSet(SecKeychainRef keychain, CFStringRef unique_id, CFStringRef label, CFStringRef description, CFStringRef account, CFDataRef password, CFDictionaryRef options){#if !TARGET_OS_IPHONE SecAccessRef access = NULL; CFBooleanRef allowRoot = NULL; CFArrayRef allowedExecutables = NULL; SecKeychainAttribute attributes[4]; SecKeychainAttributeList attributeList = { 0, attributes }; CFIndex i; SecKeychainItemRef item = NULL; CFIndex n = 0; OSStatus status;
if (options != NULL) { if (isA_CFDictionary(options)) { allowRoot = CFDictionaryGetValue(options, kSCKeychainOptionsAllowRoot); allowedExecutables = CFDictionaryGetValue(options, kSCKeychainOptionsAllowedExecutables); } else { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; } }
if (!isA_CFString(unique_id) || ((label != NULL) && !isA_CFString (label )) || ((description != NULL) && !isA_CFString (description )) || ((account != NULL) && !isA_CFString (account )) || ((password != NULL) && !isA_CFData (password )) || ((allowRoot != NULL) && !isA_CFBoolean(allowRoot )) || ((allowedExecutables != NULL) && !isA_CFArray (allowedExecutables)) || ((allowRoot != NULL) && (allowedExecutables != NULL))) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
if ((allowRoot != NULL) && CFBooleanGetValue(allowRoot)) {#if (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070) access = _SCSecAccessCreateForUID(0); if (access == NULL) { return FALSE; }#else // (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070) CFErrorRef error = NULL;
access = SecAccessCreateWithOwnerAndACL(0, 0, kSecUseOnlyUID, NULL, &error); if (access == NULL) { CFIndex code = kSCStatusAccessError;
if (error != NULL) {
code = CFErrorGetCode(error); CFRelease(error); } _SCErrorSet((int)code); return FALSE; }#endif // (__MAC_OS_X_VERSION_MIN_REQUIRED < 1070) } else if (allowedExecutables != NULL) { access = _SCSecAccessCreateForExecutables(label, allowedExecutables); if (access == NULL) { return FALSE; } }
for (i = 0; i < 4; i++) { CFStringRef str = NULL; SecKeychainAttrType tag = 0;
switch (i) { case 0 : str = unique_id; tag = kSecServiceItemAttr; break; case 1 : str = label; tag = kSecLabelItemAttr; break; case 2 : str = description; tag = kSecDescriptionItemAttr; break; case 3 : str = account; tag = kSecAccountItemAttr; break; }
if (str == NULL) { continue; }
attributes[n].tag = tag; attributes[n].data = _SC_cfstring_to_cstring(str, NULL, 0, kCFStringEncodingUTF8); attributes[n].length = (UInt32)strlen(attributes[n].data); n++; }
status = findKeychainItem(keychain, unique_id, &item); switch (status) { case noErr : { const void *pw = NULL; UInt32 pw_len = 0;
// keychain item exists if (password != NULL) { pw = CFDataGetBytePtr(password); pw_len = (UInt32)CFDataGetLength(password); }
attributeList.count = (UInt32)n; status = SecKeychainItemModifyContent(item, &attributeList, pw_len, pw); break; }
case errSecItemNotFound : { // no keychain item if (password == NULL) { // creating new keychain item and password not specified status = kSCStatusInvalidArgument; goto done; }
attributeList.count = (UInt32)n; status = SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &attributeList, (UInt32)CFDataGetLength(password), CFDataGetBytePtr(password), keychain, access, NULL); break; }
// some other error default : break; }
done :
if (access != NULL) CFRelease(access); if (item != NULL) CFRelease(item);
for (i = 0; i < n; i++) { CFAllocatorDeallocate(NULL, attributes[i].data); }
if (status != noErr) { _SCErrorSet(status); return FALSE; }
return TRUE;#else // !TARGET_OS_IPHONE#pragma unused(keychain)#pragma unused(unique_id)#pragma unused(label)#pragma unused(description)#pragma unused(account)#pragma unused(password)#pragma unused(options) _SCErrorSet(kSCStatusAccessError); return FALSE;#endif // !TARGET_OS_IPHONE}
#pragma mark -#pragma mark "System" Keychain APIs (w/SCPreferences)
#include "SCHelper_client.h"
#include <fcntl.h>#include <unistd.h>#include <sys/errno.h>
#if !TARGET_OS_IPHONEstatic CFDataRef__SCPreferencesSystemKeychainPasswordItemCopy_helper(SCPreferencesRef prefs, CFStringRef unique_id){ CFDataRef data = NULL; Boolean ok; SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs; uint32_t status = kSCStatusOK; CFDataRef reply = NULL;
if (prefsPrivate->helper_port == MACH_PORT_NULL) { ok = __SCPreferencesCreate_helper(prefs); if (!ok) { return FALSE; } }
ok = _SCSerializeString(unique_id, &data, NULL, NULL); if (!ok) { goto fail; }
// have the helper set the "System" Keychain password ok = _SCHelperExec(prefsPrivate->helper_port, SCHELPER_MSG_KEYCHAIN_COPY, data, &status, &reply); if (data != NULL) CFRelease(data); if (!ok) { goto fail; }
if (status != kSCStatusOK) { goto error; }
return reply;
fail :
// close helper if (prefsPrivate->helper_port != MACH_PORT_NULL) { _SCHelperClose(&prefsPrivate->helper_port); }
status = kSCStatusAccessError;
error :
// return error if (reply != NULL) CFRelease(reply); _SCErrorSet(status); return NULL;}#endif // !TARGET_OS_IPHONE
CFDataRef_SCPreferencesSystemKeychainPasswordItemCopy(SCPreferencesRef prefs, CFStringRef unique_id){#if !TARGET_OS_IPHONE SecKeychainRef keychain = NULL; CFDataRef password = NULL; SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
if (prefs == NULL) { /* sorry, you must provide a session */ _SCErrorSet(kSCStatusNoPrefsSession); return NULL; }
if (!isA_CFString(unique_id)) { _SCErrorSet(kSCStatusInvalidArgument); return NULL; }
if (prefsPrivate->authorizationData != NULL) { password = __SCPreferencesSystemKeychainPasswordItemCopy_helper(prefs, unique_id); goto done; }
keychain = _SCSecKeychainCopySystemKeychain(); if (keychain == NULL) { goto done; }
password = _SCSecKeychainPasswordItemCopy(keychain, unique_id);
done :
if (keychain != NULL) CFRelease(keychain); return password;#else // !TARGET_OS_IPHONE#pragma unused(prefs)#pragma unused(unique_id) _SCErrorSet(kSCStatusAccessError); return NULL;#endif // !TARGET_OS_IPHONE}
Boolean_SCPreferencesSystemKeychainPasswordItemExists(SCPreferencesRef prefs, CFStringRef unique_id){#if !TARGET_OS_IPHONE SecKeychainRef keychain = NULL; Boolean ok = FALSE;// SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
if (prefs == NULL) { /* sorry, you must provide a session */ _SCErrorSet(kSCStatusNoPrefsSession); return FALSE; }
if (!isA_CFString(unique_id)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
// if (prefsPrivate->authorizationData != NULL) {// ok = __SCPreferencesSystemKeychainPasswordItemExists_helper(prefs, unique_id);// goto done;// }
keychain = _SCSecKeychainCopySystemKeychain(); if (keychain == NULL) { goto done; }
ok = _SCSecKeychainPasswordItemExists(keychain, unique_id);
done :
if (keychain != NULL) CFRelease(keychain); return ok;#else // !TARGET_OS_IPHONE#pragma unused(prefs)#pragma unused(unique_id) _SCErrorSet(kSCStatusAccessError); return FALSE;#endif // !TARGET_OS_IPHONE}
#if !TARGET_OS_IPHONEstatic Boolean__SCPreferencesSystemKeychainPasswordItemRemove_helper(SCPreferencesRef prefs, CFStringRef unique_id){ CFDataRef data = NULL; Boolean ok; SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs; uint32_t status = kSCStatusOK; CFDataRef reply = NULL;
if (prefsPrivate->helper_port == MACH_PORT_NULL) { ok = __SCPreferencesCreate_helper(prefs); if (!ok) { return FALSE; } }
ok = _SCSerializeString(unique_id, &data, NULL, NULL); if (!ok) { goto fail; }
// have the helper set the "System" Keychain password ok = _SCHelperExec(prefsPrivate->helper_port, SCHELPER_MSG_KEYCHAIN_REMOVE, data, &status, &reply); if (data != NULL) CFRelease(data); if (!ok) { goto fail; }
if (status != kSCStatusOK) { goto error; }
return TRUE;
fail :
// close helper if (prefsPrivate->helper_port != MACH_PORT_NULL) { _SCHelperClose(&prefsPrivate->helper_port); }
status = kSCStatusAccessError;
error :
// return error if (reply != NULL) CFRelease(reply); _SCErrorSet(status); return FALSE;}#endif // !TARGET_OS_IPHONE
Boolean_SCPreferencesSystemKeychainPasswordItemRemove(SCPreferencesRef prefs, CFStringRef unique_id){#if !TARGET_OS_IPHONE SecKeychainRef keychain = NULL; Boolean ok = FALSE; SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
if (prefs == NULL) { /* sorry, you must provide a session */ _SCErrorSet(kSCStatusNoPrefsSession); return FALSE; }
if (!isA_CFString(unique_id)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
if (prefsPrivate->authorizationData != NULL) { ok = __SCPreferencesSystemKeychainPasswordItemRemove_helper(prefs, unique_id); goto done; }
keychain = _SCSecKeychainCopySystemKeychain(); if (keychain == NULL) { goto done; }
ok = _SCSecKeychainPasswordItemRemove(keychain, unique_id);
done :
if (keychain != NULL) CFRelease(keychain); return ok;#else // !TARGET_OS_IPHONE#pragma unused(prefs)#pragma unused(unique_id) _SCErrorSet(kSCStatusAccessError); return FALSE;#endif // !TARGET_OS_IPHONE}
#if !TARGET_OS_IPHONEstatic Boolean__SCPreferencesSystemKeychainPasswordItemSet_helper(SCPreferencesRef prefs, CFStringRef unique_id, CFStringRef label, CFStringRef description, CFStringRef account, CFDataRef password, CFDictionaryRef options){ CFDataRef data = NULL; CFMutableDictionaryRef newOptions = NULL; Boolean ok; SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs; uint32_t status = kSCStatusOK; CFDataRef reply = NULL;
if (prefsPrivate->helper_port == MACH_PORT_NULL) { ok = __SCPreferencesCreate_helper(prefs); if (!ok) { return FALSE; } }
if (isA_CFDictionary(options)) { CFArrayRef executableURLs = NULL;
newOptions = CFDictionaryCreateMutableCopy(NULL, 0, options);
if (CFDictionaryGetValueIfPresent(newOptions, kSCKeychainOptionsAllowedExecutables, (const void **)&executableURLs)) { CFMutableArrayRef executablePaths; CFIndex i; CFIndex n; CFDataRef path;
executablePaths = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
path = copyMyExecutablePath(); if (path != NULL) { CFArrayAppendValue(executablePaths, path); CFRelease(path); }
n = CFArrayGetCount(executableURLs); for (i = 0; i < n; i++) { char fspath[MAXPATHLEN]; CFURLRef url;
url = CFArrayGetValueAtIndex(executableURLs, i); ok = CFURLGetFileSystemRepresentation(url, TRUE, (UInt8 *)fspath, sizeof(fspath)); if (!ok) { continue; } fspath[sizeof(fspath) - 1] = '\0'; path = CFDataCreate(NULL, (UInt8 *)fspath, strlen(fspath)); CFArrayAppendValue(executablePaths, path); CFRelease(path); }
CFDictionarySetValue(newOptions, kSCKeychainOptionsAllowedExecutables, executablePaths); CFRelease(executablePaths); } } else { newOptions = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); }
if (unique_id != NULL) CFDictionarySetValue(newOptions, kSCKeychainOptionsUniqueID , unique_id); if (label != NULL) CFDictionarySetValue(newOptions, kSCKeychainOptionsLabel , label); if (description != NULL) CFDictionarySetValue(newOptions, kSCKeychainOptionsDescription, description); if (account != NULL) CFDictionarySetValue(newOptions, kSCKeychainOptionsAccount , account); if (password != NULL) CFDictionarySetValue(newOptions, kSCKeychainOptionsPassword , password);
// // if not AllowRoot and a list of executables was not provided than // pass the current executable // if (!CFDictionaryContainsKey(newOptions, kSCKeychainOptionsAllowRoot) && !CFDictionaryContainsKey(newOptions, kSCKeychainOptionsAllowedExecutables)) { CFDataRef path;
path = copyMyExecutablePath(); if (path != NULL) { CFArrayRef executablePaths;
executablePaths = CFArrayCreate(NULL, (const void **)&path, 1, &kCFTypeArrayCallBacks); CFRelease(path); CFDictionarySetValue(newOptions, kSCKeychainOptionsAllowedExecutables, executablePaths); CFRelease(executablePaths); } }
ok = _SCSerialize(newOptions, &data, NULL, NULL); CFRelease(newOptions); if (!ok) { goto fail; }
// have the helper create the "System" Keychain password ok = _SCHelperExec(prefsPrivate->helper_port, SCHELPER_MSG_KEYCHAIN_SET, data, &status, &reply); if (data != NULL) CFRelease(data); if (!ok) { goto fail; }
if (status != kSCStatusOK) { goto error; }
return TRUE;
fail :
// close helper if (prefsPrivate->helper_port != MACH_PORT_NULL) { _SCHelperClose(&prefsPrivate->helper_port); }
status = kSCStatusAccessError;
error :
// return error if (reply != NULL) CFRelease(reply); _SCErrorSet(status); return FALSE;}#endif // !TARGET_OS_IPHONE
Boolean_SCPreferencesSystemKeychainPasswordItemSet(SCPreferencesRef prefs, CFStringRef unique_id, CFStringRef label, CFStringRef description, CFStringRef account, CFDataRef password, CFDictionaryRef options){#if !TARGET_OS_IPHONE SecKeychainRef keychain = NULL; Boolean ok = FALSE; SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
if (prefs == NULL) { /* sorry, you must provide a session */ _SCErrorSet(kSCStatusNoPrefsSession); return FALSE; }
if (!isA_CFString(unique_id) || ((label != NULL) && !isA_CFString (label )) || ((description != NULL) && !isA_CFString (description)) || ((account != NULL) && !isA_CFString (account )) || ((password != NULL) && !isA_CFData (password )) || ((options != NULL) && !isA_CFDictionary(options ))) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; }
if (prefsPrivate->authorizationData != NULL) { ok = __SCPreferencesSystemKeychainPasswordItemSet_helper(prefs, unique_id, label, description, account, password, options); goto done; }
keychain = _SCSecKeychainCopySystemKeychain(); if (keychain == NULL) { goto done; }
ok = _SCSecKeychainPasswordItemSet(keychain, unique_id, label, description, account, password, options);
done :
if (keychain != NULL) CFRelease(keychain); return ok;#else // !TARGET_OS_IPHONE#pragma unused(prefs)#pragma unused(unique_id)#pragma unused(label)#pragma unused(description)#pragma unused(account)#pragma unused(password)#pragma unused(options) _SCErrorSet(kSCStatusAccessError); return FALSE;#endif // !TARGET_OS_IPHONE}