views:

223

answers:

4

I've been searching and experimenting for nearly four hours now, so I'm gonna just ask straight up:

How can I correctly use the Authorization Services API to show the user a system-level authorization window, the same one you see when you click a lock icon in System Preferences?

From what I can tell, there is no way to do it using Cocoa if you want to do it programmatically, and if your goal is to call an executable that normally needs to be called via sudo (in my case, /usr/bin/pmset) you're up a creek without a paddle.

I challenge you, I implore you: Please, enlighten me.

Thank you. :)

+1  A: 

http://cocoawithlove.com/2009/05/invoking-other-processes-in-cocoa.html http://developer.apple.com/mac/library/samplecode/BetterAuthorizationSample/index.html

tedge
Thanks! The first link had just what I needed in the sample project.
abrahamvegh
A: 

Obviously you should do real error handling and such, but here is an example to get you started.

AuthorizationRef auth = NULL;
OSStatus err;
err = AuthorizationCreate(NULL,
            NULL, 
            kAuthorizationFlagExtendRights|kAuthorizationFlagInteractionAllowed,
            &auth);
if( err != errAuthorizationSuccess ) {
    fprintf(stderr, "oops: %ld\n", (long int)err);
    exit(-1);
}
char *opts[] = { "some", "parameters", "to", "pm", NULL };
err = AuthorizationExecuteWithPrivileges(
    auth,
    "/usr/bin/pmset",
    kAuthorizationFlagDefaults,
    opts,
    NULL);
AuthorizationFree(auth, kAuthorizationFlagDefaults);
if( err != errAuthorizationSuccess ) {
    fprintf(stderr, "oops: %ld\n", (long int)err);
    exit(-1);
}
Jason Coco
A: 

This example page helped me understand how to set up a Authorization.

www.bdunagan.com system-preferences-pane-lock

markhunte