Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Calling a Privileged Installer

Occasionally, an installer must install files in directories that are not owned by the user running the installer. This should be a rare case and you should avoid it if at all possible. In the event that it can’t be avoided, the code in Listing 2-17 shows a tool that runs the /usr/bin/id utility with optional flag -un. By replacing the utility path and including your own flags, you can use this sample code to call your installer with root privileges. Your installer will then be able to perform any privileged operations it requires.

Listing 2-17  Calling a privileged installer

#include <Security/Authorization.h>
#include <Security/AuthorizationTags.h>
 
int read (long,StringPtr,int);
int write (long,StringPtr,int);
 
int main() {
 
    OSStatus myStatus;
    AuthorizationFlags myFlags = kAuthorizationFlagDefaults;// 1
    AuthorizationRef myAuthorizationRef;// 2
 
    myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,// 3
                myFlags, &myAuthorizationRef);// 4
    if (myStatus != errAuthorizationSuccess)
        return myStatus;
 
    do
    {
        {
            AuthorizationItem myItems = {kAuthorizationRightExecute, 0,// 5
                    NULL, 0};// 6
            AuthorizationRights myRights = {1, &myItems};// 7
 
            myFlags = kAuthorizationFlagDefaults |// 8
                    kAuthorizationFlagInteractionAllowed |// 9
                    kAuthorizationFlagPreAuthorize |// 10
                    kAuthorizationFlagExtendRights;// 11
            myStatus = AuthorizationCopyRights (myAuthorizationRef,                     &myRights, NULL, myFlags, NULL );// 12
        }
 
        if (myStatus != errAuthorizationSuccess) break;
 
        {
            char myToolPath[] = "/usr/bin/id";
            char *myArguments[] = { "-un", NULL };
            FILE *myCommunicationsPipe = NULL;
            char myReadBuffer[128];
 
            myFlags = kAuthorizationFlagDefaults;// 13
            myStatus = AuthorizationExecuteWithPrivileges// 14
                    (myAuthorizationRef, myToolPath, myFlags, myArguments,// 15
                    &myCommunicationsPipe);// 16
 
            if (myStatus == errAuthorizationSuccess)
                for(;;)
                {
                    int bytesRead = read (fileno (myCommunicationsPipe),
                            myReadBuffer, sizeof (myReadBuffer));
                    if (bytesRead < 1) break;
                write (fileno (stdout), myReadBuffer, bytesRead);
                }
        }
    } while (0);
 
    AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDefaults);// 17
 
    if (myStatus) printf("Status: %ld\n", myStatus);
    return myStatus;
}

Here are explanations of the numbered lines of code in Listing 2-17:

  1. Declare a variable to store authorization options.

  2. Declare an authorization reference.

  3. Use the AuthorizationCreate function to initialize the authorization reference. See “Creating an Authorization Reference Without Rights” for more information.

  4. Create an authorization item array. The user must have the right to execute to use the AuthorizationExecuteWithPrivileges function. To create a right to execute authorization item, set the name field to kAuthorizationRightExecute, the value fields to NULL, the valueLength and flags fields to 0. See “Creating an Authorization Rights Set” for more information.

  5. Create an authorization rights set. Set the count field to the number of items in the authorization item array, and set the items field to point to the authorization item array. See “Creating an Authorization Rights Set” for more information.

  6. Set the authorization options to preauthorize the rights. See “Specifying Authorization Options for Preauthorization” for more information.

  7. Use the AuthorizationCopyRights function to preauthorize the right to execute your installer as root. In this case, there is no reason to continue if the user can’t preauthorize. See “Authorizing” for more information.

  8. Set the authorization options for the AuthorizationExecuteWithPrivileges function to kAuthorizationFlagDefaults. Other authorization options, such as that specified by the kAuthorizationFlagInteractionAllowed constant, are not necessary because the AuthorizationExecuteWithPrivileges function interacts with the user whether you specify the option or not.

  9. Use the AuthorizationExecuteWithPrivileges function to invoke your installer. Pass the authorization reference in the first parameter. Pass the installer’s full POSIX pathname in the second parameter. Pass the authorization options default in the third parameter. Pass any arguments for the installer in the fourth parameter. A communications pipe to the tool may be set up through the fifth parameter. See “Calling a Helper Tool as Root” for more information about the AuthorizationExecuteWithPrivileges function.

  10. Release the authorization reference using the AuthorizationFree function. See “Releasing an Authorization Reference” for more details.



< Previous PageNext Page > Hide TOC


Last updated: 2004-02-01




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice