2 * Copyright (c) 2000-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 // trampolineClient - Authorization trampoline client-side implementation
29 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <Security/Authorization.h>
36 #include <security_utilities/endian.h>
37 #include <security_utilities/debugging.h>
40 // Where is the trampoline itself?
42 #if !defined(TRAMPOLINE)
43 # define TRAMPOLINE "/usr/libexec/security_authtrampoline" /* fallback */
48 // A few names for clarity's sake
51 READ
= 0, // read end of standard UNIX pipe
52 WRITE
= 1 // write end of standard UNIX pipe
57 // Local (static) functions
59 static const char **argVector(const char *trampoline
,
60 const char *tool
, const char *commFd
,
61 char *const *arguments
);
65 // The public client API function.
67 OSStatus
AuthorizationExecuteWithPrivileges(AuthorizationRef authorization
,
68 const char *pathToTool
,
69 AuthorizationFlags flags
,
70 char *const *arguments
,
71 FILE **communicationsPipe
)
73 // report the caller to the authorities
74 aslmsg m
= asl_new(ASL_TYPE_MSG
);
75 asl_set(m
, "com.apple.message.domain", "com.apple.libsecurity_authorization.AuthorizationExecuteWithPrivileges");
76 asl_set(m
, "com.apple.message.signature", getprogname());
77 asl_log(NULL
, m
, ASL_LEVEL_NOTICE
, "AuthorizationExecuteWithPrivileges!");
80 // flags are currently reserved
82 return errAuthorizationInvalidFlags
;
84 // externalize the authorization
85 AuthorizationExternalForm extForm
;
86 if (OSStatus err
= AuthorizationMakeExternalForm(authorization
, &extForm
))
89 // create the mailbox file
90 FILE *mbox
= tmpfile();
92 return errAuthorizationInternal
;
93 if (fwrite(&extForm
, sizeof(extForm
), 1, mbox
) != 1) {
95 return errAuthorizationInternal
;
99 // make text representation of the temp-file descriptor
101 snprintf(mboxFdText
, sizeof(mboxFdText
), "auth %d", fileno(mbox
));
103 // make a notifier pipe
107 return errAuthorizationToolExecuteFailure
;
110 // make the communications pipe if requested
112 if (communicationsPipe
&& socketpair(AF_UNIX
, SOCK_STREAM
, 0, comm
)) {
113 close(notify
[READ
]); close(notify
[WRITE
]);
115 return errAuthorizationToolExecuteFailure
;
118 // do the standard forking tango...
120 for (int n
= 5;; n
--, delay
*= 2) {
123 if (errno
== EAGAIN
) {
124 // potentially recoverable resource shortage
126 secdebug("authexec", "resource shortage (EAGAIN), delaying %d seconds", delay
);
131 secdebug("authexec", "fork failed (errno=%d)", errno
);
132 close(notify
[READ
]); close(notify
[WRITE
]);
133 return errAuthorizationToolExecuteFailure
;
136 // close foreign side of pipes
137 close(notify
[WRITE
]);
138 if (communicationsPipe
)
141 // close mailbox file (child has it open now)
144 // get status notification from child
146 secdebug("authexec", "parent waiting for status");
147 ssize_t rc
= read(notify
[READ
], &status
, sizeof(status
));
148 status
= n2h(status
);
150 default: // weird result of read: post error
151 secdebug("authexec", "unexpected read return value %ld", long(rc
));
152 status
= errAuthorizationToolEnvironmentError
;
154 case sizeof(status
): // read succeeded: child reported an error
155 secdebug("authexec", "parent received status=%d", (int)status
);
157 if (communicationsPipe
) { close(comm
[READ
]); close(comm
[WRITE
]); }
159 case 0: // end of file: exec succeeded
161 if (communicationsPipe
)
162 *communicationsPipe
= fdopen(comm
[READ
], "r+");
163 secdebug("authexec", "parent resumes (no error)");
169 // close foreign side of pipes
171 if (communicationsPipe
)
174 // fd 1 (stdout) holds the notify write end
175 dup2(notify
[WRITE
], 1);
176 close(notify
[WRITE
]);
178 // fd 0 (stdin) holds either the comm-link write-end or /dev/null
179 if (communicationsPipe
) {
180 dup2(comm
[WRITE
], 0);
184 open("/dev/null", O_RDWR
);
187 // where is the trampoline?
189 const char *trampoline
= TRAMPOLINE
;
191 const char *trampoline
= getenv("AUTHORIZATIONTRAMPOLINE");
193 trampoline
= TRAMPOLINE
;
196 // okay, execute the trampoline
197 secdebug("authexec", "child exec(%s:%s)",
198 trampoline
, pathToTool
);
199 if (const char **argv
= argVector(trampoline
, pathToTool
, mboxFdText
, arguments
))
200 execv(trampoline
, (char *const*)argv
);
201 secdebug("authexec", "trampoline exec failed (errno=%d)", errno
);
203 // execute failed - tell the parent
205 OSStatus error
= errAuthorizationToolExecuteFailure
;
207 write(1, &error
, sizeof(error
));
216 // Build an argv vector
218 static const char **argVector(const char *trampoline
, const char *pathToTool
,
219 const char *mboxFdText
, char *const *arguments
)
223 for (char *const *p
= arguments
; *p
; p
++)
226 if (const char **args
= (const char **)malloc(sizeof(const char *) * (length
+ 4))) {
227 args
[0] = trampoline
;
228 args
[1] = pathToTool
;
229 args
[2] = mboxFdText
;
231 for (int n
= 0; arguments
[n
]; n
++)
232 args
[n
+ 3] = arguments
[n
];
233 args
[length
+ 3] = NULL
;