Creating local group on MacOS Catalina using Core Services and Objective-C

102 Views Asked by At

How do I create a local group using Core Services.? Documentation for Core Services says "The Core Services Identity Reference allows developers to support user and group creation.." but there are no examples on how to do it.

Update. This is the code I have so far but It doesn't work and ErrorCode return -2, error description is null. Really struggling to find any documentation that explains how to do it. 0 information on error codes as well.


    CFStringRef realName = CFStringCreateWithCString(NULL, "newGroupTest",
                                              kCFStringEncodingMacRoman);

    CFStringRef posixName = CFStringCreateWithCString(NULL, "newgrptst1", 
                                              kCFStringEncodingMacRoman);

    AuthorizationRef auth;
    OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, 
                                                kAuthorizationFlagDefaults, 
                                                &auth);

    CSIdentityAuthorityRef authority = CSGetDefaultIdentityAuthority();
    CSIdentityRef identity = CSIdentityCreate(NULL, kCSIdentityClassGroup, realName,
                                       posixName, kCSIdentityFlagNone, authority);

    CFErrorRef error;
    BOOL success = CSIdentityCommit(Identity, auth, &error);

    if(!success)
    {
        CFIndex index = CFErrorGetCode(error);
        CFStringRef desc = CFErrorCopyDescription(error);
        const char* cDesc = CFStringGetCStringPtr(desc, CFStringGetSystemEncoding());  
    }
1

There are 1 best solutions below

0
On BEST ANSWER

Found what was the problem. I wasn't using correct identity authority. To create a local group you need to use CSGetLocaldentityAuthority() that get a local identity authority that stores the identities local to the system, instead of CSGetDefaultIdentityAuthority() that represents the network-bound authorities.