2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // Exectest - privileged-execution test driver
22 #include <Security/Authorization.h>
27 void doLoopback(int argc
, char *argv
[]);
30 int main(int argc
, char **argv
)
32 const char *path
= "/usr/bin/id";
33 bool writeToPipe
= false;
34 bool loopback
= false;
39 while ((arg
= getopt(argc
, argv
, "f:lLw")) != -1) {
48 doLoopback(argc
, argv
);
58 AuthorizationItem right
= { "system.privilege.admin", 0, NULL
, 0 };
59 AuthorizationRights rights
= { 1, &right
};
61 AuthorizationRef auth
;
62 if (OSStatus error
= AuthorizationCreate(&rights
, NULL
/*env*/,
63 kAuthorizationFlagInteractionAllowed
|
64 kAuthorizationFlagExtendRights
|
65 kAuthorizationFlagPreAuthorize
,
67 printf("create error %ld\n", error
);
73 argv
[--optind
] = "-L"; // backing over existing array element
77 if (OSStatus error
= AuthorizationExecuteWithPrivileges(auth
,
78 path
, 0, argv
+ optind
, &f
)) {
79 printf("exec error %ld\n", error
);
82 printf("--- execute successful ---\n");
85 while (fgets(buffer
, sizeof(buffer
), stdin
))
86 fprintf(f
, "%s", buffer
);
89 while (fgets(buffer
, sizeof(buffer
), f
))
92 printf("--- end of output ---\n");
97 void doLoopback(int argc
, char *argv
[])
100 printf("Authorization Execution Loopback Test\n");
101 printf("Invoked as");
102 for (int n
= 0; argv
[n
]; n
++)
103 printf(" %s", argv
[n
]);
106 // recover the authorization handle
107 AuthorizationRef auth
;
108 if (OSStatus err
= AuthorizationCopyPrivilegedReference(&auth
, 0)) {
109 printf("Cannot recover AuthorizationRef: error=%ld\n", err
);
113 printf("AuthorizationRef recovered.\n");