Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting privilege to write a file to \Library\ColorSync\Profiles\ in a mac application

From my mac application, I need to write a file to \Library\ColorSync\Profiles. For this, the app needs admin privilege to read write to the folder. Is possible to get the same with in the application? Any help will be appreciated.

I am able to pop up the permission dialog with the following code snippet

 NSSavePanel *tvarNSSavePanelObj    = [NSSavePanel savePanel];
[tvarNSSavePanelObj setDirectoryURL:[NSURL URLWithString:@"/Library/ColorSync/Profiles"]];

__block NSString *filePathexn  = nil;

[tvarNSSavePanelObj beginSheetModalForWindow:[NSApplication sharedApplication].mainWindow completionHandler:^(NSInteger tvarInt) {
    if(tvarInt == NSModalResponseOK){
        NSURL* tvarUrl = [tvarNSSavePanelObj URL];
        NSLog(@"doSaveAs filename = %@",[tvarUrl path]);
        NSString *filePath = [tvarUrl path];
        filePathexn = [filePath stringByAppendingPathExtension:@"rtf"];
        OSStatus status;
        if(![[NSFileManager defaultManager]isWritableFileAtPath:filePath]){
            NSLog(@"Not Writable at path");
            AuthorizationRef authRef;
            AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
            AuthorizationRights rights = {1, &right};
            status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagPreAuthorize, &authRef);
            AuthorizationFlags authFlags = kAuthorizationFlagDefaults | kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize;
            status = AuthorizationCopyRights(authRef, &rights, kAuthorizationEmptyEnvironment, authFlags, NULL);
        }
       BOOL status1 = [[NSFileManager defaultManager]createFileAtPath:filePathexn contents:nil attributes:nil];

    } else if(tvarInt == NSModalResponseCancel) {
        return;
    } else {
        NSLog(@"doSave As tvarInt not equal 1 or zero = %3ld",(long)tvarInt);
        return;
    }
}];

I need to know how the file write could be done. Still file is not written to the path. Is any tool name need to be specified? Will it be possible with SMJobless() ? Kindly advise a solution!!

like image 304
Govind Avatar asked Jan 11 '16 08:01

Govind


1 Answers

If it is saving a ColorSync profile you are after, ColorSyncProfileInstall will do the heavy lifting for you, like prompting the user for permission & authentication. 🏖️

It is also necessary to include the code-signing COLORSYNC_PROFILE_INSTALL_ENTITLEMENT entitlement:

<key>com.apple.developer.ColorSync.profile.install</key>
<true/>

The documentation for the aforementioned symbol can be found in the ColorSyncProfile.h header—partially reproduced here for convenience.

CSEXTERN bool ColorSyncProfileInstall(ColorSyncProfileRef profile, CFStringRef domain, CFStringRef subpath, CFErrorRef* error);
   /*
    * profile   - profile to be installed
    * domain    - either kColorSyncProfileComputerDomain or kColorSyncProfileUserDomain.
    *             kColorSyncProfileComputerDomain is for sharing the profiles (from /Library/ColorSync/Profiles).
    *             kColorSyncProfileUserDomain is for user custom profiles (installed under home directory, i.e. in 
    *             ~/Library/ColorSync/Profiles.
    *             NULL is the same as kColorSyncProfileUserDomain.
    * subpath   - CFString created from the file system representation of the path of the file to contain the installed
    *             profile. The last component of the path is interpreted as a file name if it ends with the extension ".icc".
    *             Otherwise, the subpath is interpreted as the directory path and file name will be created from the 
    *             profile description tag, appended with the ".icc" extension.
    * error     - (optional) pointer to the error which will be returned in case of failure.
    *
    *             bool value true is returned if success or false in case of error.
    */
like image 161
Chris Zielinski Avatar answered Oct 23 '22 22:10

Chris Zielinski