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

Could not remove Network Service with root authorization

我试图从我的首选项中删除特定的服务名称,但不幸的是,我在尝试使用以下函数时收到 1003 错误:SCPreferencesLock、SCPreferencesCommitChanges、SCPreferencesApplyChanges。

1003 代码是针对 kSCStatusAccessError 的,这意味着我没有 root 权限,即使 AuthorizationRef 是 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);

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

With App Sandbox, your app cannot modify the system’s network configuration (whether with the System Configuration framework, the CoreWLAN framework, or other similar APIs) because doing so requires administrator privileges.

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

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