]> git.saurik.com Git - apple/security.git/blob - SecurityServer/Authorization/trampolineClient.cpp
Security-28.tar.gz
[apple/security.git] / SecurityServer / Authorization / trampolineClient.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // trampolineClient - Authorization trampoline client-side implementation
21 //
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <sys/socket.h>
28 #include <Security/Authorization.h>
29 #include <Security/debugging.h>
30
31 //
32 // Where is the trampoline itself?
33 //
34 #if !defined(TRAMPOLINE)
35 # define TRAMPOLINE "/System/Library/CoreServices/AuthorizationTrampoline" /* fallback */
36 #endif
37
38
39 //
40 // A few names for clarity's sake
41 //
42 enum {
43 READ = 0, // read end of standard UNIX pipe
44 WRITE = 1 // write end of standard UNIX pipe
45 };
46
47
48 //
49 // Local (static) functions
50 //
51 static const char **argVector(const char *trampoline,
52 const char *tool, const char *commFd,
53 char *const *arguments);
54
55
56 //
57 // The public client API function.
58 //
59 OSStatus AuthorizationExecuteWithPrivileges(AuthorizationRef authorization,
60 const char *pathToTool,
61 unsigned long flags,
62 char *const *arguments,
63 FILE **communicationsPipe)
64 {
65 // flags are currently reserved
66 if (flags != 0)
67 return errAuthorizationInvalidFlags;
68
69 // externalize the authorization
70 AuthorizationExternalForm extForm;
71 if (OSStatus err = AuthorizationMakeExternalForm(authorization, &extForm))
72 return err;
73
74 // create the mailbox file
75 FILE *mbox = tmpfile();
76 if (!mbox)
77 return errAuthorizationInternal;
78 if (fwrite(&extForm, sizeof(extForm), 1, mbox) != 1) {
79 fclose(mbox);
80 return errAuthorizationInternal;
81 }
82
83 // make text representation of the temp-file descriptor
84 char mboxFdText[20];
85 snprintf(mboxFdText, sizeof(mboxFdText), "auth %d", fileno(mbox));
86
87 // make a notifier pipe
88 int notify[2];
89 if (pipe(notify)) {
90 fclose(mbox);
91 return errAuthorizationToolExecuteFailure;
92 }
93
94 // make the communications pipe if requested
95 int comm[2];
96 if (communicationsPipe && socketpair(AF_UNIX, SOCK_STREAM, 0, comm)) {
97 close(notify[READ]); close(notify[WRITE]);
98 fclose(mbox);
99 return errAuthorizationToolExecuteFailure;
100 }
101
102 // do the standard forking tango...
103 int delay = 1;
104 for (int n = 5;; n--, delay *= 2) {
105 switch (pid_t pid = fork()) {
106 case -1: // error
107 if (errno == EAGAIN) {
108 // potentially recoverable resource shortage
109 if (n > 0) {
110 debug("authexec", "resource shortage (EAGAIN), delaying %d seconds", delay);
111 sleep(delay);
112 continue;
113 }
114 }
115 debug("authexec", "fork failed (errno=%d)", errno);
116 close(notify[READ]); close(notify[WRITE]);
117 return errAuthorizationToolExecuteFailure;
118
119 default: // parent
120 // close foreign side of pipes
121 close(notify[WRITE]);
122 if (communicationsPipe)
123 close(comm[WRITE]);
124
125 // close mailbox file (child has it open now)
126 fclose(mbox);
127
128 // get status notification from child
129 OSStatus status;
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;
135 // fall through
136 case sizeof(status): // read succeeded: child reported an error
137 debug("authexec", "parent received status=%ld", status);
138 close(notify[READ]);
139 if (communicationsPipe) { close(comm[READ]); close(comm[WRITE]); }
140 return status;
141 case 0: // end of file: exec succeeded
142 close(notify[READ]);
143 if (communicationsPipe)
144 *communicationsPipe = fdopen(comm[READ], "r+");
145 debug("authexec", "parent resumes (no error)");
146 return noErr;
147 }
148
149 case 0: // child
150 // close foreign side of pipes
151 close(notify[READ]);
152 if (communicationsPipe)
153 close(comm[READ]);
154
155 // fd 1 (stdout) holds the notify write end
156 dup2(notify[WRITE], 1);
157 close(notify[WRITE]);
158
159 // fd 0 (stdin) holds either the comm-link write-end or /dev/null
160 if (communicationsPipe) {
161 dup2(comm[WRITE], 0);
162 close(comm[WRITE]);
163 } else {
164 close(0);
165 open("/dev/null", O_RDWR);
166 }
167
168 // where is the trampoline?
169 #if defined(NDEBUG)
170 const char *trampoline = TRAMPOLINE;
171 #else //!NDEBUG
172 const char *trampoline = getenv("AUTHORIZATIONTRAMPOLINE");
173 if (!trampoline)
174 trampoline = TRAMPOLINE;
175 #endif //NDEBUG
176
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);
183
184 // execute failed - tell the parent
185 {
186 OSStatus error = errAuthorizationToolExecuteFailure;
187 write(1, &error, sizeof(error));
188 _exit(1);
189 }
190 }
191 }
192 }
193
194
195 //
196 // Build an argv vector
197 //
198 static const char **argVector(const char *trampoline, const char *pathToTool,
199 const char *mboxFdText, char *const *arguments)
200 {
201 int length = 0;
202 if (arguments) {
203 for (char *const *p = arguments; *p; p++)
204 length++;
205 }
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;
210 if (arguments)
211 for (int n = 0; arguments[n]; n++)
212 args[n + 3] = arguments[n];
213 args[length + 3] = NULL;
214 return args;
215 }
216 return NULL;
217 }