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 // trampolineClient - Authorization trampoline client-side implementation
22 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <Security/Authorization.h>
29 #include <Security/debugging.h>
32 // Where is the trampoline itself?
34 #if !defined(TRAMPOLINE)
35 # define TRAMPOLINE "/System/Library/CoreServices/AuthorizationTrampoline" /* fallback */
40 // A few names for clarity's sake
43 READ
= 0, // read end of standard UNIX pipe
44 WRITE
= 1 // write end of standard UNIX pipe
49 // Local (static) functions
51 static const char **argVector(const char *trampoline
,
52 const char *tool
, const char *commFd
,
53 char *const *arguments
);
57 // The public client API function.
59 OSStatus
AuthorizationExecuteWithPrivileges(AuthorizationRef authorization
,
60 const char *pathToTool
,
62 char *const *arguments
,
63 FILE **communicationsPipe
)
65 // flags are currently reserved
67 return errAuthorizationInvalidFlags
;
69 // externalize the authorization
70 AuthorizationExternalForm extForm
;
71 if (OSStatus err
= AuthorizationMakeExternalForm(authorization
, &extForm
))
74 // create the mailbox file
75 FILE *mbox
= tmpfile();
77 return errAuthorizationInternal
;
78 if (fwrite(&extForm
, sizeof(extForm
), 1, mbox
) != 1) {
80 return errAuthorizationInternal
;
83 // make text representation of the temp-file descriptor
85 snprintf(mboxFdText
, sizeof(mboxFdText
), "auth %d", fileno(mbox
));
87 // make a notifier pipe
91 return errAuthorizationToolExecuteFailure
;
94 // make the communications pipe if requested
96 if (communicationsPipe
&& socketpair(AF_UNIX
, SOCK_STREAM
, 0, comm
)) {
97 close(notify
[READ
]); close(notify
[WRITE
]);
99 return errAuthorizationToolExecuteFailure
;
102 // do the standard forking tango...
104 for (int n
= 5;; n
--, delay
*= 2) {
105 switch (pid_t pid
= fork()) {
107 if (errno
== EAGAIN
) {
108 // potentially recoverable resource shortage
110 debug("authexec", "resource shortage (EAGAIN), delaying %d seconds", delay
);
115 debug("authexec", "fork failed (errno=%d)", errno
);
116 close(notify
[READ
]); close(notify
[WRITE
]);
117 return errAuthorizationToolExecuteFailure
;
120 // close foreign side of pipes
121 close(notify
[WRITE
]);
122 if (communicationsPipe
)
125 // close mailbox file (child has it open now)
128 // get status notification from child
130 debug("authexec", "parent waiting for status");
131 switch (ssize_t rc
= read(notify
[READ
], &status
, sizeof(status
))) {
132 default: // weird result of read: post error
133 debug("authexec", "unexpected read return value %ld", long(rc
));
134 status
= errAuthorizationToolEnvironmentError
;
136 case sizeof(status
): // read succeeded: child reported an error
137 debug("authexec", "parent received status=%ld", status
);
139 if (communicationsPipe
) { close(comm
[READ
]); close(comm
[WRITE
]); }
141 case 0: // end of file: exec succeeded
143 if (communicationsPipe
)
144 *communicationsPipe
= fdopen(comm
[READ
], "r+");
145 debug("authexec", "parent resumes (no error)");
150 // close foreign side of pipes
152 if (communicationsPipe
)
155 // fd 1 (stdout) holds the notify write end
156 dup2(notify
[WRITE
], 1);
157 close(notify
[WRITE
]);
159 // fd 0 (stdin) holds either the comm-link write-end or /dev/null
160 if (communicationsPipe
) {
161 dup2(comm
[WRITE
], 0);
165 open("/dev/null", O_RDWR
);
168 // where is the trampoline?
170 const char *trampoline
= TRAMPOLINE
;
172 const char *trampoline
= getenv("AUTHORIZATIONTRAMPOLINE");
174 trampoline
= TRAMPOLINE
;
177 // okay, execute the trampoline
178 debug("authexec", "child exec(%s:%s)",
179 trampoline
, pathToTool
);
180 if (const char **argv
= argVector(trampoline
, pathToTool
, mboxFdText
, arguments
))
181 execv(trampoline
, (char *const[])argv
);
182 debug("authexec", "trampoline exec failed (errno=%d)", errno
);
184 // execute failed - tell the parent
186 OSStatus error
= errAuthorizationToolExecuteFailure
;
187 write(1, &error
, sizeof(error
));
196 // Build an argv vector
198 static const char **argVector(const char *trampoline
, const char *pathToTool
,
199 const char *mboxFdText
, char *const *arguments
)
203 for (char *const *p
= arguments
; *p
; p
++)
206 if (const char **args
= (const char **)malloc(sizeof(const char *) * (length
+ 4))) {
207 args
[0] = trampoline
;
208 args
[1] = pathToTool
;
209 args
[2] = mboxFdText
;
211 for (int n
= 0; arguments
[n
]; n
++)
212 args
[n
+ 3] = arguments
[n
];
213 args
[length
+ 3] = NULL
;