+static Boolean
+restorePreferences()
+{
+ Boolean ok = FALSE;
+ CFStringRef currentModel = NULL;
+ CFMutableStringRef modelPrefixStr = NULL;
+ CFArrayRef keyList = NULL;
+ CFIndex keyListCount;
+ CFIndex idx;
+ Boolean modified = FALSE;
+ int sc_status = kSCStatusFailed;
+
+ while (TRUE) {
+ ok = SCPreferencesLock(prefs, TRUE);
+ if (ok) {
+ break;
+ }
+
+ sc_status = SCError();
+ if (sc_status == kSCStatusStale) {
+ SCPreferencesSynchronize(prefs);
+ } else {
+ SC_log(LOG_NOTICE, "Could not acquire network configuration lock: %s",
+ SCErrorString(sc_status));
+ return FALSE;
+ }
+ }
+
+ keyList = SCPreferencesCopyKeyList(prefs);
+ if (keyList == NULL) {
+ goto error;
+ }
+
+ currentModel = _SC_hw_model(FALSE);
+ if (currentModel == NULL) {
+ goto error;
+ }
+
+ /* Create "model:" string for prefix-check */
+ modelPrefixStr = CFStringCreateMutableCopy(NULL, 0, currentModel);
+ CFStringAppend(modelPrefixStr, CFSTR(":"));
+
+ keyListCount = CFArrayGetCount(keyList);
+ for (idx = 0; idx < keyListCount; idx++) {
+ CFStringRef existingKey = CFArrayGetValueAtIndex(keyList, idx);
+ CFStringRef key;
+ CFArrayRef splitKey = NULL;
+ CFPropertyListRef value;
+
+ if (isA_CFString(existingKey) == NULL) {
+ continue;
+ }
+
+ if (CFStringHasPrefix(existingKey, modelPrefixStr) == FALSE) {
+ continue;
+ }
+
+ splitKey = CFStringCreateArrayBySeparatingStrings(NULL, existingKey, CFSTR(":"));
+ key = CFArrayGetValueAtIndex(splitKey, 1);
+ value = SCPreferencesGetValue(prefs, existingKey);
+ SCPreferencesSetValue(prefs, key, value);
+ SCPreferencesRemoveValue(prefs, existingKey);
+ modified = TRUE;
+ CFRelease(splitKey);
+ }
+
+ if (modified == TRUE) {
+ SCPreferencesRef ni_prefs = NULL;
+ ni_prefs = SCPreferencesCreate(NULL, MY_PLUGIN_ID, CFSTR("NetworkInterfaces.plist"));
+ if (ni_prefs == NULL) {
+ goto error;
+ }
+
+ ok = _SCNetworkConfigurationCheckValidityWithPreferences(prefs, ni_prefs, NULL);
+ CFRelease(ni_prefs);
+
+ //Commit the changes only if prefs files valid
+ if (ok == TRUE) {
+ if (!SCPreferencesCommitChanges(prefs)) {
+ if (SCError() != EROFS) {
+ SC_log(LOG_NOTICE, "SCPreferencesCommitChanges() failed: %s",
+ SCErrorString(SCError()));
+ }
+ goto error;
+
+ }
+
+ (void) SCPreferencesApplyChanges(prefs);
+ }
+ }
+
+error:
+ (void) SCPreferencesUnlock(prefs);
+
+ if (keyList != NULL) {
+ CFRelease(keyList);
+ }
+ if (modelPrefixStr != NULL) {
+ CFRelease(modelPrefixStr);
+ }
+
+ return modified;
+}
+
+static Boolean