无法使用root授权删除网络服务

时间:2018-02-14 12:14:08

标签: objective-c macos networking system-configuration

我正在尝试从我的偏好中删除特定的服务名称,但遗憾的是,在尝试使用以下功能时,我收到1003错误:SCPreferencesLock,SCPreferencesCommitChanges,SCPreferencesApplyChanges。

它特定于kSCStatusAccessError的1003代码,这意味着即使AuthorizationRef是root,我也没有root权限。

    // Create AuthorizationRef
AuthorizationRef auth = NULL;
OSStatus status;

AuthorizationFlags rootFlags =  kAuthorizationFlagDefaults |
                                kAuthorizationFlagInteractionAllowed |
                                kAuthorizationFlagPreAuthorize |
                                kAuthorizationFlagExtendRights;

// Get default authorization
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, rootFlags, &auth);

SCPreferencesRef preferences;

if (status == noErr) {
    preferences = SCPreferencesCreateWithAuthorization(NULL, CFSTR("personal.configuration"), NULL, auth);
    NSLog(@"Root autehntication");
} else {
    preferences = SCPreferencesCreate(NULL, CFSTR("personal.configuration"), NULL);
    NSLog(@"Default autehntication");
}


if(preferences == NULL) {
    NSLog(@"Could not create preferences");
}

CFArrayRef servicesArray = SCNetworkServiceCopyAll(preferences);
if (servicesArray == NULL) {
    NSLog(@"No network services");
}

// Get list of available services
SCNetworkServiceRef service;

for (int i = 0; i < CFArrayGetCount(servicesArray); i++) {
    // Get service reference
    service = (SCNetworkServiceRef)CFArrayGetValueAtIndex(servicesArray, i);
    // Get service anme
    CFStringRef serviceName = SCNetworkServiceGetName(service);
    NSString* strServiceName= (__bridge NSString *)(serviceName);

    NSLog(@"New network serivice found: %@", strServiceName);

    if ([strServiceName isEqualToString:@"Specific name"]) {

        if (!SCPreferencesLock(preferences, TRUE)){
            NSLog(@"\tFailed to call SCPreferencesLock: %d", SCError());
        }

        if(SCNetworkServiceRemove(service)) {
            NSLog(@"\tInternet Service successfully removed!");
        }

        if (!SCPreferencesCommitChanges(preferences)) {
            NSLog(@"\tFailed to commit preferences changes: %d", SCError());
        }

        if (!SCPreferencesApplyChanges(preferences)) {
            NSLog(@"\tFailed to apply preferences changes: %d", SCError());
        }

        SCPreferencesSynchronize(preferences);

        if (!SCPreferencesUnlock(preferences)) {
            NSLog(@"\tFailed to unlock preferences: %d", SCError());
        }
    }
}

CFRelease(servicesArray);

1 个答案:

答案 0 :(得分:1)

根据评论,问题是应用程序正在使用Cocoa沙箱。

  

使用App Sandbox,您的应用无法修改系统的网络配置(无论是使用系统配置框架,CoreWLAN框架还是其他类似的API),因为这样做需要管理员权限。

如果要修改系统配置框架,则无法使用沙箱。

您可以转到应用程序权利文件并将条目App Sandbox更改为NO来禁用沙箱,然后会出现安全对话框。