long OSCollector::Collect(NVDataItem **ReturnItem) { auto_ptr<NVDataItem> DataItems (new NVDataItem(OSCL_TAG)); auto_ptr<NVDataItem> OSItem (new NVDataItem(OSCI_TAG)); OSItem->AddNVItem(OSCL_MFR, "Apple Computer, Inc."); CFAuto<CFStringRef> PrefCFStringVal; CFAuto<SCDynamicStoreRef> DSObj; DSObj.Attach( SCDynamicStoreCreate( kCFAllocatorDefault, kProgramID, NULL, NULL)); std::string PrefStringVal; CFAuto<SCPreferencesRef> SysVersion; SysVersion.Attach( SCPreferencesCreate ( kCFAllocatorDefault, kProgramID, CFSTR("/System/Library/CoreServices/SystemVersion.plist"))); CFPropertyListRef SCPropVal; SCPropVal = SCPreferencesGetValue(SysVersion, CFSTR("ProductName")); PrefCFStringVal = reinterpret_cast<CFStringRef>(SCPropVal); PrefCFStringVal.GetCString(PrefStringVal); OSItem->AddNVItem(OSCL_NAME, PrefStringVal.c_str()); SCPropVal = SCPreferencesGetValue(SysVersion, CFSTR("ProductUserVisibleVersion")); PrefCFStringVal = reinterpret_cast<CFStringRef>(SCPropVal); PrefCFStringVal.GetCString(PrefStringVal); OSItem->AddNVItem(OSCL_VERSION, PrefStringVal.c_str()); SCPropVal = SCPreferencesGetValue(SysVersion, CFSTR("ProductBuildVersion")); PrefCFStringVal = reinterpret_cast<CFStringRef>(SCPropVal); PrefCFStringVal.GetCString(PrefStringVal); OSItem->AddNVItem(OSCL_BUILD, PrefStringVal.c_str()); DataItems->AddSubItem(OSItem.release()); *ReturnItem = DataItems.release(); #ifdef TRACE printf("OS Complete\n"); #endif return ERROR_SUCCESS; }
CFArrayRef SCVLANInterfaceCopyAvailablePhysicalInterfaces() { CFMutableArrayRef available; CFArrayRef bond_interfaces = NULL; CFArrayRef bridge_interfaces = NULL; CFMutableSetRef excluded = NULL; CFArrayRef interfaces; SCPreferencesRef prefs; available = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); prefs = SCPreferencesCreate(NULL, CFSTR("SCVLANInterfaceCopyAvailablePhysicalInterfaces"), NULL); if (prefs != NULL) { #if !TARGET_OS_IPHONE bond_interfaces = SCBondInterfaceCopyAll(prefs); if (bond_interfaces != NULL) { excluded = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks); __SCBondInterfaceListCollectMembers(bond_interfaces, excluded); } #endif // !TARGET_OS_IPHONE bridge_interfaces = SCBridgeInterfaceCopyAll(prefs); if (bridge_interfaces != NULL) { if (excluded == NULL) { excluded = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks); } __SCBridgeInterfaceListCollectMembers(bridge_interfaces, excluded); } CFRelease(prefs); } // add real interfaces that aren't part of a bond or bridge interfaces = __SCNetworkInterfaceCopyAll_IONetworkInterface(); if (interfaces != NULL) { addAvailableInterfaces(available, interfaces, excluded); CFRelease(interfaces); } // add bond interfaces if (bond_interfaces != NULL) { addAvailableInterfaces(available, bond_interfaces, NULL); CFRelease(bond_interfaces); } // add bridge interfaces if (bridge_interfaces != NULL) { addAvailableInterfaces(available, bridge_interfaces, NULL); CFRelease(bridge_interfaces); } if (excluded != NULL) { CFRelease(excluded); } return available; }
STATIC SCPreferencesRef get_sc_prefs(EAPOLClientConfigurationRef cfg) { if (cfg->sc_prefs == NULL) { cfg->sc_prefs = SCPreferencesCreate(NULL, kPrefsName, NULL); if (cfg->sc_prefs == NULL) { EAPLOG(LOG_NOTICE, "EAPOLClientConfiguration: SCPreferencesCreate failed, %s", SCErrorString(SCError())); } } return (cfg->sc_prefs); }
EAPOLClientConfigurationRef EAPOLClientConfigurationCreateInternal(CFAllocatorRef allocator, AuthorizationRef auth) { EAPOLClientConfigurationRef cfg; /* allocate/return an EAPOLClientConfigurationRef */ cfg = __EAPOLClientConfigurationAllocate(allocator); if (cfg == NULL) { return (NULL); } if (auth != NULL) { cfg->eap_prefs = SCPreferencesCreateWithAuthorization(allocator, kPrefsName, kEAPOLClientConfigurationPrefsID, auth); } else { cfg->eap_prefs = SCPreferencesCreate(allocator, kPrefsName, kEAPOLClientConfigurationPrefsID); } if (cfg->eap_prefs == NULL) { goto failed; } if (auth != NULL) { AuthorizationExternalForm * auth_ext_p; OSStatus status; auth_ext_p = malloc(sizeof(*auth_ext_p)); status = AuthorizationMakeExternalForm(auth, auth_ext_p); if (status != errAuthorizationSuccess) { free(auth_ext_p); goto failed; } cfg->auth_ext_p = auth_ext_p; } import_profiles(cfg); cfg->def_auth_props = copy_def_auth_props(cfg->eap_prefs); return (cfg); failed: my_CFRelease(&cfg); return (NULL); }
vector<InterfaceInfo> OSXPlatform::interfaces() { vector<InterfaceInfo> result; CFStringRef name = CFSTR("com.codebutler.firesheep.backend"); SCPreferencesRef prefs = SCPreferencesCreate(NULL, name, NULL); SCNetworkSetRef set = SCNetworkSetCopyCurrent(prefs); CFArrayRef services = SCNetworkSetCopyServices(set); int arraySize = CFArrayGetCount(services); for (int i = 0; i < arraySize; i++) { SCNetworkServiceRef service = (SCNetworkServiceRef) CFArrayGetValueAtIndex(services, i); if (SCNetworkServiceGetEnabled(service)) { SCNetworkInterfaceRef iface = SCNetworkServiceGetInterface(service); CFStringRef serviceName = SCNetworkServiceGetName(service); char cServiceName[(CFStringGetLength(serviceName) * 4) + 1]; CFStringGetCString(serviceName, cServiceName, sizeof(cServiceName), kCFStringEncodingUTF8); CFStringRef type = SCNetworkInterfaceGetInterfaceType(iface); if (CFStringCompare(type, CFSTR("Ethernet"), 0) == kCFCompareEqualTo || CFStringCompare(type, CFSTR("IEEE80211"), 0) == kCFCompareEqualTo) { char cType[(CFStringGetLength(type) * 4) + 1]; CFStringGetCString(type, cType, sizeof(cType), kCFStringEncodingUTF8); CFStringRef bsdName = SCNetworkInterfaceGetBSDName(iface); char cBsdName[(CFStringGetLength(bsdName) * 4) + 1]; CFStringGetCString(bsdName, cBsdName, sizeof(cBsdName), kCFStringEncodingUTF8); InterfaceInfo info((string(cBsdName)), (string(cServiceName)), (string(cType))); result.push_back(info); } } } CFRelease(services); CFRelease(set); CFRelease(prefs); return result; }
/* * * copySchedulePowerChangeArrays * */ static void copyScheduledPowerChangeArrays(void) { #if !TARGET_OS_EMBEDDED CFArrayRef tmp; SCPreferencesRef prefs; PowerEventBehavior *this_behavior; int i; prefs = SCPreferencesCreate(0, CFSTR("PM-configd-AutoWake"), CFSTR(kIOPMAutoWakePrefsPath)); if(!prefs) return; activeEventCnt = 0; // Loop through all sleep, wake, shutdown powerbehaviors for(i=0; i<kBehaviorsCount; i++) { this_behavior = behaviors[i]; if(this_behavior->array) { CFRelease(this_behavior->array); this_behavior->array = NULL; } tmp = isA_CFArray(SCPreferencesGetValue(prefs, this_behavior->title)); if(tmp && (0 < CFArrayGetCount(tmp))) { this_behavior->array = CFArrayCreateMutableCopy(0, 0, tmp); activeEventCnt += CFArrayGetCount(tmp); } else { this_behavior->array = NULL; } } CFRelease(prefs); #endif }
//---------------------------------------------------------------------- // dsauth_get_admin_acct //---------------------------------------------------------------------- static void dsauth_get_admin_acct(u_int32_t *acctNameSize, char** acctName, u_int32_t *passwordSize, char **password) { SCPreferencesRef prefs; CFPropertyListRef globals; CFStringRef acctNameRef; char namestr[256]; u_int32_t namelen; *passwordSize = 0; *password = 0; *acctNameSize = 0; *acctName = 0; // // get the acct name from the plist // if ((prefs = SCPreferencesCreate(0, CFSTR("pppd"), kRASServerPrefsFileName)) != 0) { // get globals dict from the plist if ((globals = SCPreferencesGetValue(prefs, kRASGlobals)) != 0) { // retrieve the password server account id if ((acctNameRef = CFDictionaryGetValue(globals, KRASGlobPSKeyAccount)) != 0) { namestr[0] = 0; CFStringGetCString(acctNameRef, namestr, 256, kCFStringEncodingMacRoman); if (namestr[0]) { namelen = strlen(namestr); if (dsauth_get_admin_password(namelen, namestr, passwordSize, password) == 0) { *acctNameSize = namelen; *acctName = malloc(namelen + 1); if (acctName != 0) memcpy(*acctName, namestr, namelen + 1); } } else error("DSAuth plugin: Key access user name not valid.\n"); } } CFRelease(prefs); } }
// ---------------------------------------------------------------------------- // get_active_server // ---------------------------------------------------------------------------- CFArrayRef get_active_servers(struct vpn_params *params) { SCPreferencesRef prefs = 0; CFPropertyListRef active_servers; CFArrayRef arrayCopy = 0; char pathStr[MAXPATHLEN]; // open the prefs file prefs = SCPreferencesCreate(0, CFSTR("vpnd"), kRASServerPrefsFileName); if (prefs == NULL) { CFStringGetCString(kRASServerPrefsFileName, pathStr, MAXPATHLEN, kCFStringEncodingMacRoman); vpnlog(LOG_ERR, "Unable to read vpnd prefs file '%s'\n", pathStr); return 0; } // get active servers list from the plist active_servers = SCPreferencesGetValue(prefs, kRASActiveServers); if (active_servers && isArray(active_servers)) if (CFArrayGetCount(active_servers) > 0) arrayCopy = CFArrayCreateCopy(0, active_servers); CFRelease(prefs); return arrayCopy; }
/* Initialise a Preferences object from the given pspec. This might be an * SCPreferences AppID or it might be the path to a plist file. */ Preferences::Preferences(const char * pspec) : m_pspec(pspec), m_plist(NULL), m_scpref(NULL) { /* Try to load this as a plist file first. The SCPreferences API does a * bunch of work if it can't find the file, and we should avoid that unless * we have a good idea that it's necessary. */ this->m_plist = load_plist_from_path(this->m_pspec.c_str()); if (this->m_plist) { /* Loading a plist should always give you back a dictionary. */ ASSERT(CFDictionaryGetTypeID() == CFGetTypeID(this->m_plist)); VERBOSE("loaded plist %s\n", pspec); return; } cf_typeref<CFStringRef> appname(cfstring_wrap(getprogname())); cf_typeref<CFStringRef> appid(cfstring_wrap(this->m_pspec.c_str())); this->m_scpref = SCPreferencesCreate(kCFAllocatorDefault, appname, appid); if (this->m_scpref == NULL) { return; } /* If there was no existing preferences file, SCError() should return * kSCStatusNoConfigFile. We are only interested in reading preferences, so * we want to fail if there's no existing config. */ if (SCError() != kSCStatusOK) { safe_release(this->m_scpref); } if (this->m_scpref) { VERBOSE("loaded SC preferences %s\n", pspec); } }
/* * Copy Events from on-disk file. We should be doing this only * once, at start of the powerd. */ static void copyScheduledRepeatPowerEvents(void) { SCPreferencesRef prefs; CFDictionaryRef tmp; prefs = SCPreferencesCreate(0, CFSTR("PM-configd-AutoWake"), CFSTR(kIOPMAutoWakePrefsPath)); if(!prefs) return; if (repeatingPowerOff) CFRelease(repeatingPowerOff); if (repeatingPowerOn) CFRelease(repeatingPowerOn); tmp = (CFDictionaryRef)SCPreferencesGetValue(prefs, CFSTR(kIOPMRepeatingPowerOffKey)); if (tmp && isA_CFDictionary(tmp)) repeatingPowerOff = CFDictionaryCreateMutableCopy(0,0,tmp); tmp = (CFDictionaryRef)SCPreferencesGetValue(prefs, CFSTR(kIOPMRepeatingPowerOnKey)); if (tmp && isA_CFDictionary(tmp)) repeatingPowerOn = CFDictionaryCreateMutableCopy(0,0,tmp); CFRelease(prefs); }
/* * Function: saveInterfaceEAPOLConfiguration * Purpose: * Save the SCNetworkInterface EAPOL information for System and LoginWindow * modes. * * Iterate over the changed SCNetworkInterfaceRef list cfg->sc_changed_if, * and for each interface, grab the EAPOL extended information from * cfg->sc_prefs. Then set the corresponding value in the new * freshly created SCPreferencesRef. * * All this done to avoid a writer getting Stale Object errors * when it has its own SCPreferencesRef object that it manipulates while * having an EAPOLClientConfigurationRef open. */ STATIC Boolean saveInterfaceEAPOLConfiguration(EAPOLClientConfigurationRef cfg, Boolean * changed_p) { AuthorizationExternalForm * auth_ext_p; int count; int i; SCPreferencesRef prefs = NULL; Boolean ret = FALSE; *changed_p = FALSE; if (cfg->sc_changed_if == NULL) { return (TRUE); } auth_ext_p = EAPOLClientConfigurationGetAuthorizationExternalForm(cfg); if (auth_ext_p != NULL) { AuthorizationRef auth; OSStatus status; status = AuthorizationCreateFromExternalForm(auth_ext_p, &auth); if (status != errAuthorizationSuccess) { EAPLOG(LOG_ERR, "EAPOLClientConfiguration: can't allocate Authorization, %d", (int)status); goto done; } prefs = SCPreferencesCreateWithAuthorization(NULL, kPrefsName, NULL, auth); AuthorizationFree(auth, kAuthorizationFlagDefaults); } else { prefs = SCPreferencesCreate(NULL, kPrefsName, NULL); } count = CFArrayGetCount(cfg->sc_changed_if); for (i = 0; i < count; i++) { CFDictionaryRef dict; CFStringRef if_name; SCNetworkInterfaceRef net_if; net_if = (SCNetworkInterfaceRef) CFArrayGetValueAtIndex(cfg->sc_changed_if, i); if_name = SCNetworkInterfaceGetBSDName(net_if); if (if_name == NULL) { /* should not happen */ EAPLOG(LOG_ERR, "EAPOLClientConfiguration: missing BSD name"); continue; } dict = SCNetworkInterfaceGetExtendedConfiguration(net_if, kEAPOL); /* find the same interface in the saving prefs */ if (set_eapol_configuration(prefs, if_name, dict) == FALSE) { continue; } } ret = SCPreferencesCommitChanges(prefs); if (ret == FALSE) { EAPLOG(LOG_NOTICE, "EAPOLClientConfigurationSave SCPreferencesCommitChanges" " failed %s", SCErrorString(SCError())); goto done; } SCPreferencesApplyChanges(prefs); *changed_p = TRUE; done: my_CFRelease(&cfg->sc_changed_if); my_CFRelease(&prefs); return (ret); }
static void smb_set_configuration(SCDynamicStoreRef store, CFDictionaryRef dict) { CFArrayRef array; Boolean changed = FALSE; UInt32 dosCodepage = 0; CFStringEncoding dosEncoding = 0; CFStringEncoding macEncoding = kCFStringEncodingMacRoman; uint32_t macRegion = 0; Boolean ok; SCPreferencesRef prefs; CFStringRef str; prefs = SCPreferencesCreate(NULL, CFSTR("smb-configuration"), CFSTR(kSMBPreferencesAppID)); if (prefs == NULL) { my_log(LOG_ERR, "smb_set_configuration: SCPreferencesCreate() failed: %s", SCErrorString(SCError())); return; } ok = SCPreferencesLock(prefs, TRUE); if (!ok) { my_log(LOG_ERR, "smb_set_configuration: SCPreferencesLock() failed: %s", SCErrorString(SCError())); goto done; } // Server description str = SCDynamicStoreCopyComputerName(store, &macEncoding); update_pref(prefs, CFSTR(kSMBPrefServerDescription), str, &changed); // DOS code page if (str != NULL) { if (macEncoding == kCFStringEncodingMacRoman) { CFStringRef key; CFDictionaryRef dict; // get region key = SCDynamicStoreKeyCreateComputerName(NULL); dict = SCDynamicStoreCopyValue(store, key); CFRelease(key); if (dict != NULL) { if (isA_CFDictionary(dict)) { CFNumberRef num; SInt32 val; num = CFDictionaryGetValue(dict, kSCPropSystemComputerNameRegion); if (isA_CFNumber(num) && CFNumberGetValue(num, kCFNumberSInt32Type, &val)) { macRegion = (uint32_t)val; } } CFRelease(dict); } } CFRelease(str); } else { // Important: must have root acccess (eUID==0) to access the config file! __CFStringGetInstallationEncodingAndRegion((uint32_t *)&macEncoding, &macRegion); } _SC_dos_encoding_and_codepage(macEncoding, macRegion, &dosEncoding, &dosCodepage); str = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), (unsigned int)dosCodepage); assert(str != NULL); update_pref(prefs, CFSTR(kSMBPrefDOSCodePage), str, &changed); CFRelease(str); // NetBIOS name str = CFDictionaryGetValue(dict, kSCPropNetSMBNetBIOSName); str = isA_CFString(str); update_pref(prefs, CFSTR(kSMBPrefNetBIOSName), str, &changed); // NetBIOS node type str = CFDictionaryGetValue(dict, kSCPropNetSMBNetBIOSNodeType); str = isA_CFString(str); if (str != NULL) { if (CFEqual(str, kSCValNetSMBNetBIOSNodeTypeBroadcast)) { // B-node str = CFSTR(kSMBPrefNetBIOSNodeBroadcast); } else if (CFEqual(str, kSCValNetSMBNetBIOSNodeTypePeer)) { // P-node str = CFSTR(kSMBPrefNetBIOSNodePeer); } else if (CFEqual(str, kSCValNetSMBNetBIOSNodeTypeMixed)) { // M-node str = CFSTR(kSMBPrefNetBIOSNodeMixed); } else if (CFEqual(str, kSCValNetSMBNetBIOSNodeTypeHybrid)) { // H-node str = CFSTR(kSMBPrefNetBIOSNodeHybrid); } else { str = NULL; } } update_pref(prefs, CFSTR(kSMBPrefNetBIOSNodeType), str, &changed); #ifdef ADD_NETBIOS_SCOPE // NetBIOS scope str = CFDictionaryGetValue(dict, kSCPropNetSMBNetBIOSScope); str = isA_CFString(str); update_pref(prefs, CFSTR(kSMBPrefNetBIOSScope), str, &changed); #endif // ADD_NETBIOS_SCOPE // WINS addresses array = CFDictionaryGetValue(dict, kSCPropNetSMBWINSAddresses); array = isA_CFArray(array); update_pref(prefs, CFSTR(kSMBPrefWINSServerAddressList), array, &changed); // Workgroup (or domain) str = CFDictionaryGetValue(dict, kSCPropNetSMBWorkgroup); str = isA_CFString(str); update_pref(prefs, CFSTR(kSMBPrefWorkgroup), str, &changed); if (changed) { ok = SCPreferencesCommitChanges(prefs); if (!ok) { if ((SCError() != EROFS)) { my_log(LOG_ERR, "smb_set_configuration: SCPreferencesCommitChanges() failed: %s", SCErrorString(SCError())); } goto done; } ok = SCPreferencesApplyChanges(prefs); if (!ok) { my_log(LOG_ERR, "smb_set_configuration: SCPreferencesApplyChanges() failed: %s", SCErrorString(SCError())); goto done; } } done : (void) SCPreferencesUnlock(prefs); CFRelease(prefs); return; }
void DAPreferenceListRefresh( void ) { struct stat status1; struct stat status2; /* * Determine whether the preference list is up-to-date. */ if ( stat( ___PREFS_DEFAULT_DIR "/" "autodiskmount.plist", &status1 ) ) { __gDAPreferenceListTime1.tv_sec = 0; __gDAPreferenceListTime1.tv_nsec = 0; } if ( stat( ___PREFS_DEFAULT_DIR "/" _kDADaemonName ".plist", &status2 ) ) { __gDAPreferenceListTime2.tv_sec = 0; __gDAPreferenceListTime2.tv_nsec = 0; } if ( __gDAPreferenceListTime1.tv_sec != status1.st_mtimespec.tv_sec || __gDAPreferenceListTime1.tv_nsec != status1.st_mtimespec.tv_nsec || __gDAPreferenceListTime2.tv_sec != status2.st_mtimespec.tv_sec || __gDAPreferenceListTime2.tv_nsec != status2.st_mtimespec.tv_nsec ) { SCPreferencesRef preferences; __gDAPreferenceListTime1.tv_sec = status1.st_mtimespec.tv_sec; __gDAPreferenceListTime1.tv_nsec = status1.st_mtimespec.tv_nsec; __gDAPreferenceListTime2.tv_sec = status2.st_mtimespec.tv_sec; __gDAPreferenceListTime2.tv_nsec = status2.st_mtimespec.tv_nsec; /* * Clear the preference list. */ CFDictionaryRemoveAllValues( gDAPreferenceList ); /* * Build the preference list. */ preferences = SCPreferencesCreate( kCFAllocatorDefault, CFSTR( "autodiskmount" ), CFSTR( "autodiskmount.plist" ) ); if ( preferences ) { CFTypeRef value; value = SCPreferencesGetValue( preferences, CFSTR( "AutomountDisksWithoutUserLogin" ) ); if ( value == kCFBooleanTrue ) { CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountDeferExternalKey, kCFBooleanFalse ); CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountDeferRemovableKey, kCFBooleanFalse ); CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountTrustExternalKey, kCFBooleanTrue ); } else if ( value == kCFBooleanFalse ) { CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountDeferExternalKey, kCFBooleanFalse ); CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountDeferRemovableKey, kCFBooleanTrue ); CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountTrustExternalKey, kCFBooleanTrue ); } CFRelease( preferences ); } preferences = SCPreferencesCreate( kCFAllocatorDefault, CFSTR( _kDADaemonName ), CFSTR( _kDADaemonName ".plist" ) ); if ( preferences ) { CFTypeRef value; value = SCPreferencesGetValue( preferences, kDAPreferenceMountDeferExternalKey ); if ( value ) { if ( CFGetTypeID( value ) == CFBooleanGetTypeID( ) ) { CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountDeferExternalKey, value ); } } value = SCPreferencesGetValue( preferences, kDAPreferenceMountDeferInternalKey ); if ( value ) { if ( CFGetTypeID( value ) == CFBooleanGetTypeID( ) ) { CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountDeferInternalKey, value ); } } value = SCPreferencesGetValue( preferences, kDAPreferenceMountDeferRemovableKey ); if ( value ) { if ( CFGetTypeID( value ) == CFBooleanGetTypeID( ) ) { CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountDeferRemovableKey, value ); } } value = SCPreferencesGetValue( preferences, kDAPreferenceMountTrustExternalKey ); if ( value ) { if ( CFGetTypeID( value ) == CFBooleanGetTypeID( ) ) { CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountTrustExternalKey, value ); } } value = SCPreferencesGetValue( preferences, kDAPreferenceMountTrustInternalKey ); if ( value ) { if ( CFGetTypeID( value ) == CFBooleanGetTypeID( ) ) { CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountTrustInternalKey, value ); } } value = SCPreferencesGetValue( preferences, kDAPreferenceMountTrustRemovableKey ); if ( value ) { if ( CFGetTypeID( value ) == CFBooleanGetTypeID( ) ) { CFDictionarySetValue( gDAPreferenceList, kDAPreferenceMountTrustRemovableKey, value ); } } CFRelease( preferences ); } } }
// ---------------------------------------------------------------------------- // process_prefs // ---------------------------------------------------------------------------- int process_prefs(struct vpn_params *params) { char pathStr[MAXPATHLEN]; SCPreferencesRef prefs = 0; CFPropertyListRef servers_list; char text[512] = ""; // open the prefs file prefs = SCPreferencesCreate(0, CFSTR("vpnd"), kRASServerPrefsFileName); if (prefs == NULL) { CFStringGetCString(kRASServerPrefsFileName, pathStr, MAXPATHLEN, kCFStringEncodingMacRoman); snprintf(text, sizeof(text), "Unable to read vpnd prefs file '%s'\n", pathStr); goto fail; } // get servers list from the plist servers_list = SCPreferencesGetValue(prefs, kRASServers); if (servers_list == NULL) { snprintf(text, sizeof(text), "Could not get servers dictionary\n"); goto fail; } // retrieve the information for the given Server ID params->serverIDRef = CFStringCreateWithCString(0, params->server_id, kCFStringEncodingMacRoman); if (params->serverIDRef == NULL) { snprintf(text, sizeof(text), "Could not create CFString for server ID\n"); goto fail; } params->serverRef = CFDictionaryGetValue(servers_list, params->serverIDRef); if (params->serverRef == NULL || isDictionary(params->serverRef) == 0) { snprintf(text, sizeof(text), "Server ID '%.64s' invalid\n", params->server_id); params->serverRef = 0; goto fail; } CFRetain(params->serverRef); CFRelease(prefs); prefs = 0; // process the dictionaries if (process_server_prefs(params)) goto fail; if (process_interface_prefs(params)) goto fail; switch (params->server_type) { case SERVER_TYPE_PPP: if (ppp_process_prefs(params)) { snprintf(text, sizeof(text), "Error while reading PPP preferences\n"); goto fail; } break; case SERVER_TYPE_IPSEC: if (ipsec_process_prefs(params)) { snprintf(text, sizeof(text), "Error while reading IPSec preferences\n"); goto fail; } break; } return 0; fail: vpnlog(LOG_ERR, text[0] ? text : "Error while reading preferences\n"); if (params->serverIDRef) { CFRelease(params->serverIDRef); params->serverIDRef = 0; } if (params->serverRef) { CFRelease(params->serverRef); params->serverRef = 0; } if (prefs) CFRelease(prefs); return -1; }
__private_extern__ void load_PreferencesMonitor(CFBundleRef bundle, Boolean bundleVerbose) { Boolean initPrefs = TRUE; if (bundleVerbose) { _verbose = TRUE; } SCLog(_verbose, LOG_DEBUG, CFSTR("load() called")); SCLog(_verbose, LOG_DEBUG, CFSTR(" bundle ID = %@"), CFBundleGetIdentifier(bundle)); /* open a SCDynamicStore session to allow cache updates */ store = SCDynamicStoreCreate(NULL, CFSTR("PreferencesMonitor.bundle"), watchQuietCallback, NULL); if (store == NULL) { SCLog(TRUE, LOG_ERR, CFSTR("SCDynamicStoreCreate() failed: %s"), SCErrorString(SCError())); goto error; } /* open a SCPreferences session */ #ifndef MAIN prefs = SCPreferencesCreate(NULL, CFSTR("PreferencesMonitor.bundle"), NULL); #else // !MAIN prefs = SCPreferencesCreate(NULL, CFSTR("PreferencesMonitor.bundle"), CFSTR("/tmp/preferences.plist")); #endif // !MAIN if (prefs != NULL) { Boolean need_update = FALSE; CFStringRef 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)) { // if new hardware need_update = TRUE; } } if (need_update == FALSE) { SCNetworkSetRef current; current = SCNetworkSetCopyCurrent(prefs); if (current != NULL) { /* network configuration available, disable template creation */ initPrefs = FALSE; CFRelease(current); } } } else { SCLog(TRUE, LOG_ERR, CFSTR("SCPreferencesCreate() failed: %s"), SCErrorString(SCError())); goto error; } /* * register for change notifications. */ if (!SCPreferencesSetCallback(prefs, updateConfiguration, NULL)) { SCLog(TRUE, LOG_ERR, CFSTR("SCPreferencesSetCallBack() failed: %s"), SCErrorString(SCError())); goto error; } if (!SCPreferencesScheduleWithRunLoop(prefs, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) { SCLog(TRUE, LOG_ERR, CFSTR("SCPreferencesScheduleWithRunLoop() failed: %s"), SCErrorString(SCError())); goto error; } /* * if no preferences, initialize with a template (now or * when IOKit has quiesced). */ if (initPrefs) { watchQuietEnable(); watchQuietCallback(store, NULL, NULL); } return; error : watchQuietDisable(); if (store != NULL) CFRelease(store); if (prefs != NULL) CFRelease(prefs); 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; }
int set_global_proxy(int enable, const char* host, int port) { printf("set global proxy %d %s %d\n", enable, host, port); SCPreferencesRef pref = SCPreferencesCreate(kCFAllocatorSystemDefault, CFSTR("setproxy"), 0); if(!pref){ printf("Failed to open preference.\n"); return 1; } CFDictionaryRef set = SCPreferencesPathGetValue(pref, CFSTR("/NetworkServices/")); if(!set){ printf("Failed to get network services.\n"); } CFMutableDictionaryRef mset = CFDictionaryCreateMutableCopy(0, 0, set); SCDynamicStoreRef theDynamicStore = SCDynamicStoreCreate(nil, CFSTR("setproxy"), nil, nil); CFDictionaryRef returnedPList; returnedPList = (CFDictionaryRef)SCDynamicStoreCopyValue(theDynamicStore, CFSTR("State:/Network/Global/IPv4")); CFStringRef primaryService = NULL; if(returnedPList){ primaryService = (CFStringRef)CFDictionaryGetValue(returnedPList, CFSTR("PrimaryService")); } size_t size = CFDictionaryGetCount(set); CFTypeRef *keysTypeRef = (CFTypeRef *) malloc( size * sizeof(CFTypeRef) ); CFTypeRef *valuesTypeRef = (CFTypeRef *) malloc( size * sizeof(CFTypeRef) ); CFDictionaryGetKeysAndValues(set, (const void **) keysTypeRef, (const void**)valuesTypeRef); const void **keys = (const void **) keysTypeRef; printf("Number of interfaces = %ld\n", size); int i; for(i=0; i<size && keysTypeRef[i]; i++){ Boolean success; CFStringRef service = (CFStringRef)keysTypeRef[i]; printf("Setting interface %d\n", i); if(enable == 1 && primaryService && CFStringCompare(service, primaryService, kCFCompareCaseInsensitive) != 0){ continue; } CFTypeRef value = valuesTypeRef[i]; if(!value){ continue; } CFDictionaryRef face = (CFDictionaryRef)value; CFMutableDictionaryRef mface = CFDictionaryCreateMutableCopy(0, 0, face); CFMutableDictionaryRef mproxy = NULL; CFDictionaryRef proxy = (CFDictionaryRef)CFDictionaryGetValue(mface, CFSTR("Proxies")); if(NULL == proxy){ if(enable == 0){ CFRelease(mface); continue; } printf("proxy = %p, try to create it\n", proxy); mproxy = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); }else{ mproxy = CFDictionaryCreateMutableCopy(0, 0, proxy); if(mproxy == NULL) return 4; } if(enable){ CFStringRef cfHost = CFStringCreateWithCString(0, host, kCFStringEncodingASCII); CFDictionarySetValue(mproxy, CFSTR("HTTPEnable"), CFNumberCreate(0, kCFNumberIntType, &enable)); CFDictionarySetValue(mproxy, CFSTR("HTTPProxy"), cfHost); CFDictionarySetValue(mproxy, CFSTR("HTTPPort"), CFNumberCreate(0, kCFNumberIntType, &port)); CFDictionarySetValue(mproxy, CFSTR("HTTPSEnable"), CFNumberCreate(0, kCFNumberIntType, &enable)); CFDictionarySetValue(mproxy, CFSTR("HTTPSProxy"), cfHost); CFDictionarySetValue(mproxy, CFSTR("HTTPSPort"), CFNumberCreate(0, kCFNumberIntType, &port)); CFRelease(cfHost); }else{ CFDictionaryRemoveValue(mproxy, CFSTR("HTTPEnable")); CFDictionaryRemoveValue(mproxy, CFSTR("HTTPProxy")); CFDictionaryRemoveValue(mproxy, CFSTR("HTTPPort")); CFDictionaryRemoveValue(mproxy, CFSTR("HTTPSEnable")); CFDictionaryRemoveValue(mproxy, CFSTR("HTTPSProxy")); CFDictionaryRemoveValue(mproxy, CFSTR("HTTPSPort")); } CFDictionarySetValue(mface, CFSTR("Proxies"), mproxy); CFDictionarySetValue(mset, service, mface); SCPreferencesPathSetValue(pref, CFSTR("/NetworkServices/"), mset); success = SCPreferencesCommitChanges(pref); printf("success: %d\n", success); success = SCPreferencesApplyChanges(pref); printf("success: %d\n", success); CFRelease(mface); CFRelease(mproxy); } CFRelease(mset); CFRelease(pref); free(keysTypeRef); free(valuesTypeRef); }