#import <Cocoa/Cocoa.h>
#import <Security/Security.h>

int main(int argc, char *argv[])
{
#if 0
	printf("TEST de pre autorisation");
	printf("1");
	OSStatus err = AuthorizationCreate(NULL,
	NULL,
	kAuthorizationFlagDefaults,
	NULL);
	printf("resutl:%i\nMore rights?",err);
	|kAuthorizationFlagExtendRights|kAuthorizationFlagPreAuthorize
	return 0;
#elif 1
	printf("TEST de pre autorisation\nstep 1\n");
	OSStatus myStatus;
    AuthorizationFlags myFlags = kAuthorizationFlagDefaults;              // 1
    AuthorizationRef myAuthorizationRef;                                  // 2
 
    myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,  // 3
                myFlags, &myAuthorizationRef);
	printf("myStatus:%i\n",myStatus);
    if (myStatus != errAuthorizationSuccess)
        return myStatus;
	printf("step 2\n");

	AuthorizationItem myItems = {kAuthorizationRightExecute, 0,    // 4
			NULL, 0};
	AuthorizationRights myRights = {1, &myItems};                  // 5

	myFlags = kAuthorizationFlagDefaults |
			kAuthorizationFlagPreAuthorize |
			kAuthorizationFlagExtendRights |
			kAuthorizationFlagInteractionAllowed;
	myStatus = AuthorizationCopyRights (myAuthorizationRef,       // 7
								 &myRights, NULL, myFlags, NULL );
	
	printf("myStatus:%i\n",myStatus);
	if (myStatus == errAuthorizationInteractionNotAllowed)
	{
		printf("Interaction REQUIRES\n");
        return myStatus;
	}
	if (myStatus != errAuthorizationSuccess)
        return myStatus;
	printf("step 3\n");
	return 0;
#else
 
    do
    {
        {
            AuthorizationItem myItems = {kAuthorizationRightExecute, 0,    // 4
                    NULL, 0};
            AuthorizationRights myRights = {1, &myItems};                  // 5
 
            myFlags = kAuthorizationFlagDefaults |                         // 6
                    kAuthorizationFlagInteractionAllowed |
                    kAuthorizationFlagPreAuthorize |
                    kAuthorizationFlagExtendRights;
            myStatus = AuthorizationCopyRights (myAuthorizationRef,       // 7
                                         &myRights, NULL, myFlags, NULL );
        }
 
        if (myStatus != errAuthorizationSuccess) break;
 
        {
            char myToolPath[] = "/usr/bin/id";
            char *myArguments[] = { "-un", NULL };
            FILE *myCommunicationsPipe = NULL;
            char myReadBuffer[128];
 
            myFlags = kAuthorizationFlagDefaults;                          // 8
            myStatus = AuthorizationExecuteWithPrivileges                  // 9
                    (myAuthorizationRef, myToolPath, myFlags, myArguments,
                    &myCommunicationsPipe);
 
            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);    // 10
 
    if (myStatus) printf("Status: %ld\n", myStatus);
    return myStatus;
#endif
}