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