2  * Copyright (c) 2000-2001,2004 Apple Computer, Inc. All Rights Reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * This file contains Original Code and/or Modifications of Original Code 
   7  * as defined in and that are subject to the Apple Public Source License 
   8  * Version 2.0 (the 'License'). You may not use this file except in 
   9  * compliance with the License. Please obtain a copy of the License at 
  10  * http://www.opensource.apple.com/apsl/ and read it before using this 
  13  * The Original Code and all software distributed under the License are 
  14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  18  * Please see the License for the specific language governing rights and 
  19  * limitations under the License. 
  21  * @APPLE_LICENSE_HEADER_END@ 
  26 // Exectest - privileged-execution test driver 
  28 #include <Security/Authorization.h> 
  33 void doLoopback(int argc
, char *argv
[]); 
  36 int main(int argc
, char **argv
) 
  38         const char *path 
= "/usr/bin/id"; 
  39         bool writeToPipe 
= false; 
  40         bool loopback 
= false; 
  45         while ((arg 
= getopt(argc
, argv
, "f:lLw")) != -1) { 
  54                         doLoopback(argc
, argv
); 
  64         AuthorizationItem right 
= { "system.privilege.admin", 0, NULL
, 0 }; 
  65         AuthorizationRights rights 
= { 1, &right 
}; 
  67         AuthorizationRef auth
; 
  68         if (OSStatus error 
= AuthorizationCreate(&rights
, NULL 
/*env*/, 
  69                 kAuthorizationFlagInteractionAllowed 
| 
  70                 kAuthorizationFlagExtendRights 
| 
  71                 kAuthorizationFlagPreAuthorize
, 
  73                 printf("create error %ld\n", error
); 
  79                 argv
[--optind
] = "-L";  // backing over existing array element 
  83         if (OSStatus error 
= AuthorizationExecuteWithPrivileges(auth
, 
  84                 path
, 0, argv 
+ optind
, &f
)) { 
  85                 printf("exec error %ld\n", error
); 
  88         printf("--- execute successful ---\n"); 
  91                 while (fgets(buffer
, sizeof(buffer
), stdin
)) 
  92                         fprintf(f
, "%s", buffer
); 
  95                 while (fgets(buffer
, sizeof(buffer
), f
)) 
  98         printf("--- end of output ---\n"); 
 103 void doLoopback(int argc
, char *argv
[]) 
 106         printf("Authorization Execution Loopback Test\n"); 
 107         printf("Invoked as"); 
 108         for (int n 
= 0; argv
[n
]; n
++) 
 109                 printf(" %s", argv
[n
]); 
 112         // recover the authorization handle 
 113         AuthorizationRef auth
; 
 114         if (OSStatus err 
= AuthorizationCopyPrivilegedReference(&auth
, 0)) { 
 115                 printf("Cannot recover AuthorizationRef: error=%ld\n", err
); 
 119         printf("AuthorizationRef recovered.\n");