static IOReturn updateRepeatEventsOnDisk(SCPreferencesRef prefs) { IOReturn ret = kIOReturnSuccess; if (repeatingPowerOn) { if(!SCPreferencesSetValue(prefs, CFSTR(kIOPMRepeatingPowerOnKey), repeatingPowerOn)) { ret = kIOReturnError; goto exit; } } else SCPreferencesRemoveValue(prefs, CFSTR(kIOPMRepeatingPowerOnKey)); if (repeatingPowerOff) { if(!SCPreferencesSetValue(prefs, CFSTR(kIOPMRepeatingPowerOffKey), repeatingPowerOff)) { ret = kIOReturnError; goto exit; } } else SCPreferencesRemoveValue(prefs, CFSTR(kIOPMRepeatingPowerOffKey)); if(!SCPreferencesCommitChanges(prefs)) { ret = kIOReturnError; goto exit; } exit: return ret; }
Boolean SCNetworkSetSetCurrent(SCNetworkSetRef set) { Boolean ok; CFStringRef path; SCNetworkSetPrivateRef setPrivate = (SCNetworkSetPrivateRef)set; if (!isA_SCNetworkSet(set)) { _SCErrorSet(kSCStatusInvalidArgument); return FALSE; } path = SCPreferencesPathKeyCreateSet(NULL, setPrivate->setID); ok = SCPreferencesSetValue(setPrivate->prefs, kSCPrefCurrentSet, path); CFRelease(path); return ok; }
static void update_pref(SCPreferencesRef prefs, CFStringRef key, CFTypeRef newVal, Boolean *changed) { CFTypeRef curVal; curVal = SCPreferencesGetValue(prefs, key); if (!_SC_CFEqual(curVal, newVal)) { if (newVal != NULL) { SCPreferencesSetValue(prefs, key, newVal); } else { SCPreferencesRemoveValue(prefs, key); } *changed = TRUE; } return; }
int main(int argc, char **argv) { const char *command = argv[0]; extern int optind; int opt; CFStringRef current = NULL; int currentMatched = 0; CFStringRef newSet = NULL; /* set key */ CFStringRef newSetUDN = NULL; /* user defined name */ CFStringRef prefix; SCPreferencesRef prefs; CFDictionaryRef sets; CFIndex nSets; const void **setKeys = NULL; const void **setVals = NULL; CFIndex i; #if !TARGET_OS_IPHONE AuthorizationRef authorization = NULL; AuthorizationFlags flags = kAuthorizationFlagDefaults; CFMutableDictionaryRef options; OSStatus status; #endif // !TARGET_OS_IPHONE /* process any arguments */ while ((opt = getopt_long(argc, argv, "dvn", longopts, NULL)) != -1) { switch(opt) { case 'd': _sc_debug = TRUE; _sc_log = FALSE; /* enable framework logging */ break; case 'v': _sc_verbose = TRUE; break; case 'n': apply = FALSE; break; case '?': default : usage(command); } } argc -= optind; argv += optind; prefix = CFStringCreateWithFormat(NULL, NULL, CFSTR("/%@/"), kSCPrefSets); if (argc == 1) { newSet = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman); /* check if a full path to the new "set" was specified */ if ((CFStringGetLength(newSet) > 0) && CFStringHasPrefix(newSet, prefix)) { CFRange range; CFMutableStringRef str; str = CFStringCreateMutableCopy(NULL, 0, newSet); CFRelease(newSet); CFStringDelete(str, CFRangeMake(0, CFStringGetLength(prefix))); newSet = CFStringCreateCopy(NULL, newSet); CFRelease(str); range = CFStringFind(newSet, CFSTR("/"), 0); if (range.location != kCFNotFound) { SCPrint(TRUE, stderr, CFSTR("Set \"%@\" not available\n."), newSet); exit (1); } } } else { newSet = CFRetain(CFSTR("")); } #if !TARGET_OS_IPHONE status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, flags, &authorization); if (status != errAuthorizationSuccess) { SCPrint(TRUE, stderr, CFSTR("AuthorizationCreate() failed: status = %d\n"), (int)status); exit (1); } options = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(options, kSCPreferencesOptionChangeNetworkSet, kCFBooleanTrue); prefs = SCPreferencesCreateWithOptions(NULL, CFSTR("scselect"), NULL, authorization, options); CFRelease(options); if (prefs == NULL) { SCPrint(TRUE, stderr, CFSTR("SCPreferencesCreate() failed\n")); exit (1); } #else // !TARGET_OS_IPHONE prefs = SCPreferencesCreate(NULL, CFSTR("scselect"), NULL); if (prefs == NULL) { SCPrint(TRUE, stderr, CFSTR("SCPreferencesCreate() failed\n")); exit (1); } #endif // !TARGET_OS_IPHONE sets = SCPreferencesGetValue(prefs, kSCPrefSets); if (sets == NULL) { SCPrint(TRUE, stderr, CFSTR("No network sets defined.\n")); exit (1); } current = SCPreferencesGetValue(prefs, kSCPrefCurrentSet); if (current != NULL) { if (CFStringHasPrefix(current, prefix)) { CFMutableStringRef tmp; tmp = CFStringCreateMutableCopy(NULL, 0, current); CFStringDelete(tmp, CFRangeMake(0, CFStringGetLength(prefix))); current = tmp; } else { CFRetain(current); currentMatched = -1; /* not prefixed */ } } else { current = CFRetain(CFSTR("")); currentMatched = -2; /* not defined */ } nSets = CFDictionaryGetCount(sets); if (nSets > 0) { setKeys = CFAllocatorAllocate(NULL, nSets * sizeof(CFStringRef), 0); setVals = CFAllocatorAllocate(NULL, nSets * sizeof(CFDictionaryRef), 0); CFDictionaryGetKeysAndValues(sets, setKeys, setVals); } /* check for set with matching name */ for (i = 0; i < nSets; i++) { CFStringRef key = (CFStringRef) setKeys[i]; CFDictionaryRef dict = (CFDictionaryRef)setVals[i]; if ((currentMatched >= 0) && CFEqual(key, current)) { currentMatched++; } if (CFEqual(newSet, key)) { newSetUDN = CFDictionaryGetValue(dict, kSCPropUserDefinedName); if (newSetUDN != NULL) CFRetain(newSetUDN); goto found; } } /* check for set with matching user-defined name */ for (i = 0; i < nSets; i++) { CFStringRef key = (CFStringRef) setKeys[i]; CFDictionaryRef dict = (CFDictionaryRef)setVals[i]; newSetUDN = CFDictionaryGetValue(dict, kSCPropUserDefinedName); if ((newSetUDN != NULL) && CFEqual(newSet, newSetUDN)) { CFRelease(newSet); newSet = CFRetain(key); CFRetain(newSetUDN); goto found; } } if (argc == 1) { SCPrint(TRUE, stderr, CFSTR("Set \"%@\" not available.\n"), newSet); exit(1); } SCPrint(TRUE, stdout, CFSTR("Defined sets include:%s\n"), (currentMatched > 0) ? " (* == current set)" : ""); for (i = 0; i < nSets; i++) { CFStringRef key = (CFStringRef) setKeys[i]; CFDictionaryRef dict = (CFDictionaryRef)setVals[i]; CFStringRef udn = CFDictionaryGetValue(dict, kSCPropUserDefinedName); SCPrint(TRUE, stdout, CFSTR(" %s %@\t(%@)\n"), ((currentMatched > 0) && CFEqual(key, current)) ? "*" : " ", key, udn ? udn : CFSTR("")); } switch (currentMatched) { case -2 : SCPrint(TRUE, stdout, CFSTR("\nCurrent set not defined.\n")); break; case -1 : SCPrint(TRUE, stdout, CFSTR("\nCurrent set \"%@\" may not be valid\n"), current); break; case 0 : SCPrint(TRUE, stdout, CFSTR("\nCurrent set \"%@\" not valid\n"), current); break; default : break; } CFRelease(prefix); exit (0); found : CFRelease(current); current = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@"), prefix, newSet); if (!SCPreferencesSetValue(prefs, kSCPrefCurrentSet, current)) { SCPrint(TRUE, stderr, CFSTR("SCPreferencesSetValue(...,%@,%@) failed: %s\n"), kSCPrefCurrentSet, current, SCErrorString(SCError())); exit (1); } if (!SCPreferencesCommitChanges(prefs)) { int sc_status = SCError(); if (sc_status == kSCStatusAccessError) { SCPrint(TRUE, stderr, CFSTR("Only local console users and administrators can change locations\n")); exit (EX_NOPERM); } else { SCPrint(TRUE, stderr, CFSTR("SCPreferencesCommitChanges() failed: %s\n"), SCErrorString(sc_status)); exit (1); } } if (apply) { if (!SCPreferencesApplyChanges(prefs)) { SCPrint(TRUE, stderr, CFSTR("SCPreferencesApplyChanges() failed %s\n"), SCErrorString(SCError())); exit (1); } } SCPrint(TRUE, stdout, CFSTR("%@ updated to %@ (%@)\n"), kSCPrefCurrentSet, newSet, newSetUDN ? newSetUDN : CFSTR("")); CFRelease(current); CFRelease(newSet); if (newSetUDN != NULL) CFRelease(newSetUDN); CFRelease(prefix); CFRelease(prefs); #if !TARGET_OS_IPHONE AuthorizationFree(authorization, kAuthorizationFlagDefaults); // AuthorizationFree(authorization, kAuthorizationFlagDestroyRights); #endif /* !TARGET_OS_IPHONE */ exit (0); return 0; }
static void nc_set_application_url(CFStringRef subtype, CFStringRef directory) { CFURLRef directory_url = NULL; CFDataRef directory_url_data = NULL; CFStringRef vpnprefpath = NULL; char *path = NULL; CFIndex path_len = 0; if (subtype == NULL || directory == NULL) { goto done; } directory_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, directory, kCFURLPOSIXPathStyle, FALSE); if (directory_url == NULL) { SCPrint(TRUE, stderr, CFSTR("CFURLCreateWithFileSystemPath failed\n")); goto done; } directory_url_data = CFURLCreateBookmarkData(NULL, directory_url, 0, 0, 0, 0); if (directory_url_data == NULL) { SCPrint(TRUE, stderr, CFSTR("CFURLCreateBookmarkData failed\n")); goto done; } vpnprefpath = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@%@"), PREF_PREFIX, subtype, PREF_SUFFIX ); if (vpnprefpath == NULL) { SCPrint(TRUE, stderr, CFSTR("CFStringCreateWithFormat failed\n")); goto done; } path_len = CFStringGetLength(vpnprefpath) + 1; path = malloc(path_len); if (path == NULL) { goto done; } if (!CFStringGetCString(vpnprefpath, path, path_len, kCFStringEncodingASCII)) { SCPrint(TRUE, stderr, CFSTR("CFStringGetCString failed\n")); goto done; } do_prefs_init(); /* initialization */ do_prefs_open(1, &path); /* open prefs */ if (!SCPreferencesSetValue(prefs, CFSTR("ApplicationURL"), directory_url_data)) { SCPrint(TRUE, stderr, CFSTR("SCPreferencesSetValue ApplicationURL failed, %s\n"), SCErrorString(SCError())); goto done; } _prefs_save(); done: my_CFRelease(&directory_url); my_CFRelease(&directory_url_data); my_CFRelease(&vpnprefpath); if (path) { free(path); } _prefs_close(); exit(0); }
static Boolean establishNewPreferences() { CFBundleRef bundle; SCNetworkSetRef current = NULL; CFStringRef new_model; Boolean ok = FALSE; int sc_status = kSCStatusFailed; SCNetworkSetRef set = NULL; CFStringRef setName = NULL; Boolean updated = FALSE; while (TRUE) { ok = SCPreferencesLock(prefs, TRUE); if (ok) { break; } sc_status = SCError(); if (sc_status == kSCStatusStale) { SCPreferencesSynchronize(prefs); } else { SCLog(TRUE, LOG_ERR, CFSTR("Could not acquire network configuration lock: %s"), SCErrorString(sc_status)); return FALSE; } } /* Ensure that the preferences has the new model */ new_model = _SC_hw_model(FALSE); /* Need to regenerate the new configuration for new model */ if (new_model != NULL) { CFStringRef old_model; old_model = SCPreferencesGetValue(prefs, MODEL); if ((old_model != NULL) && !_SC_CFEqual(old_model, new_model)) { CFIndex count; CFIndex index; CFArrayRef keys; keys = SCPreferencesCopyKeyList(prefs); count = (keys != NULL) ? CFArrayGetCount(keys) : 0; // if new hardware for (index = 0; index < count; index++) { CFStringRef existing_key; existing_key = CFArrayGetValueAtIndex(keys, index); if (isA_CFString(existing_key) != NULL) { CFStringRef new_key; CFPropertyListRef value; /* If it already contains a Model or if it already contains a MODEL:KEY key skip it*/ if (CFEqual(existing_key, MODEL) || CFStringFind(existing_key, CFSTR(":"), 0).location != kCFNotFound) { continue; } value = SCPreferencesGetValue(prefs, existing_key); /* Create a new key as OLD_MODEL:OLD_KEY */ new_key = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@:%@"), old_model, existing_key); SCPreferencesSetValue(prefs, new_key, value); if (!CFEqual(existing_key, kSCPrefSystem)) { /* preserve existing host names */ SCPreferencesRemoveValue(prefs, existing_key); } CFRelease(new_key); } } if (keys != NULL) { CFRelease(keys); } } /* Set the new model */ SCPreferencesSetValue(prefs, MODEL, new_model); } current = SCNetworkSetCopyCurrent(prefs); if (current != NULL) { set = current; } if (set == NULL) { set = SCNetworkSetCreate(prefs); if (set == NULL) { ok = FALSE; sc_status = SCError(); goto done; } bundle = _SC_CFBundleGet(); if (bundle != NULL) { setName = CFBundleCopyLocalizedString(bundle, CFSTR("DEFAULT_SET_NAME"), CFSTR("Automatic"), NULL); } ok = SCNetworkSetSetName(set, (setName != NULL) ? setName : CFSTR("Automatic")); if (!ok) { sc_status = SCError(); goto done; } ok = SCNetworkSetSetCurrent(set); if (!ok) { sc_status = SCError(); goto done; } } ok = SCNetworkSetEstablishDefaultConfiguration(set); if (!ok) { sc_status = SCError(); goto done; } done : if (ok) { ok = SCPreferencesCommitChanges(prefs); if (ok) { SCLog(TRUE, LOG_NOTICE, CFSTR("New network configuration saved")); updated = TRUE; } else { sc_status = SCError(); if (sc_status == EROFS) { /* a read-only fileysstem is OK */ ok = TRUE; /* ... but we don't want to synchronize */ rofs = TRUE; } } /* apply (committed or temporary/read-only) changes */ (void) SCPreferencesApplyChanges(prefs); } else if ((current == NULL) && (set != NULL)) { (void) SCNetworkSetRemove(set); } if (!ok) { SCLog(TRUE, LOG_ERR, CFSTR("Could not establish network configuration: %s"), SCErrorString(sc_status)); } (void)SCPreferencesUnlock(prefs); if (setName != NULL) CFRelease(setName); if (set != NULL) CFRelease(set); return updated; }
/* * Function: EAPOLClientConfigurationSave * * Purpose: * Write the configuration to persistent storage. * * Returns: * TRUE if successfully written, FALSE otherwise. */ Boolean EAPOLClientConfigurationSave(EAPOLClientConfigurationRef cfg) { Boolean changed = FALSE; CFDictionaryRef existing_prefs_dict; CFDictionaryRef prefs_dict; Boolean ret = FALSE; /* save the 802.1X prefs */ prefs_dict = export_profiles(cfg); if (prefs_dict == NULL) { EAPLOG(LOG_NOTICE, "EAPOLClientConfigurationSave export_profiles() failed"); goto done; } existing_prefs_dict = SCPreferencesGetValue(cfg->eap_prefs, kConfigurationKeyProfiles); if (cfg->def_auth_props_changed == FALSE && my_CFEqual(existing_prefs_dict, prefs_dict)) { /* configuration is the same, no need to save */ } else { if (cfg->def_auth_props_changed) { ret = SCPreferencesSetValue(cfg->eap_prefs, kConfigurationKeyDefaultAuthenticationProperties, cfg->def_auth_props); if (ret == FALSE) { EAPLOG(LOG_NOTICE, "EAPOLClientConfigurationSave SCPreferencesSetValue" " failed %s", SCErrorString(SCError())); goto done; } } ret = SCPreferencesSetValue(cfg->eap_prefs, kConfigurationKeyProfiles, prefs_dict); if (ret == FALSE) { EAPLOG(LOG_NOTICE, "EAPOLClientConfigurationSave SCPreferencesSetValue" " failed %s", SCErrorString(SCError())); goto done; } ret = SCPreferencesCommitChanges(cfg->eap_prefs); if (ret == FALSE) { EAPLOG(LOG_NOTICE, "EAPOLClientConfigurationSave SCPreferencesCommitChanges" " failed %s", SCErrorString(SCError())); return (FALSE); } cfg->def_auth_props_changed = FALSE; SCPreferencesApplyChanges(cfg->eap_prefs); changed = TRUE; } /* save the network prefs */ { Boolean this_changed = FALSE; ret = saveInterfaceEAPOLConfiguration(cfg, &this_changed); if (ret == FALSE) { goto done; } if (this_changed) { changed = TRUE; } } my_CFRelease(&cfg->sc_prefs); /* force a refresh */ done: my_CFRelease(&prefs_dict); if (changed) { notify_post(kEAPOLClientConfigurationChangedNotifyKey); } return (ret); }