2 * Copyright (c) 2000-2004,2011-2014 Apple 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/AuthorizationPriv.h>
37 #include <Security/SecBase.h>
38 #include <security_utilities/endian.h>
39 #include <security_utilities/debugging.h>
42 // Where is the trampoline itself?
44 #if !defined(TRAMPOLINE)
45 # define TRAMPOLINE "/usr/libexec/security_authtrampoline" /* fallback */
50 // A few names for clarity's sake
53 READ
= 0, // read end of standard UNIX pipe
54 WRITE
= 1 // write end of standard UNIX pipe
59 // Local (static) functions
61 static const char **argVector(const char *trampoline
,
62 const char *tool
, const char *commFd
,
63 char *const *arguments
);
66 OSStatus
AuthorizationExecuteWithPrivileges(AuthorizationRef authorization
,
67 const char *pathToTool
,
68 AuthorizationFlags flags
,
69 char *const *arguments
,
70 FILE **communicationsPipe
)
72 // externalize the authorization
73 AuthorizationExternalForm extForm
;
74 if (OSStatus err
= AuthorizationMakeExternalForm(authorization
, &extForm
))
77 return AuthorizationExecuteWithPrivilegesExternalForm(&extForm
, pathToTool
, flags
, arguments
, communicationsPipe
);
81 // The public client API function.
83 OSStatus
AuthorizationExecuteWithPrivilegesExternalForm(const AuthorizationExternalForm
* extForm
,
84 const char *pathToTool
,
85 AuthorizationFlags flags
,
86 char *const *arguments
,
87 FILE **communicationsPipe
)
90 return errAuthorizationInvalidPointer
;
92 // report the caller to the authorities
93 aslmsg m
= asl_new(ASL_TYPE_MSG
);
94 asl_set(m
, "com.apple.message.domain", "com.apple.libsecurity_authorization.AuthorizationExecuteWithPrivileges");
95 asl_set(m
, "com.apple.message.signature", getprogname());
96 asl_log(NULL
, m
, ASL_LEVEL_NOTICE
, "AuthorizationExecuteWithPrivileges!");
99 // flags are currently reserved
101 return errAuthorizationInvalidFlags
;
103 // create the mailbox file
104 FILE *mbox
= tmpfile();
106 return errAuthorizationInternal
;
107 if (fwrite(extForm
, sizeof(*extForm
), 1, mbox
) != 1) {
109 return errAuthorizationInternal
;
113 // compute the argument vector here because we can't allocate memory once we fork.
115 // make text representation of the temp-file descriptor
117 snprintf(mboxFdText
, sizeof(mboxFdText
), "auth %d", fileno(mbox
));
119 // where is the trampoline?
121 const char *trampoline
= TRAMPOLINE
;
123 const char *trampoline
= getenv("AUTHORIZATIONTRAMPOLINE");
125 trampoline
= TRAMPOLINE
;
128 const char **argv
= argVector(trampoline
, pathToTool
, mboxFdText
, arguments
);
130 // make a notifier pipe
134 return errAuthorizationToolExecuteFailure
;
137 // make the communications pipe if requested
139 if (communicationsPipe
&& socketpair(AF_UNIX
, SOCK_STREAM
, 0, comm
)) {
140 close(notify
[READ
]); close(notify
[WRITE
]);
142 return errAuthorizationToolExecuteFailure
;
145 OSStatus status
= errSecSuccess
;
147 // do the standard forking tango...
149 for (int n
= 5;; n
--, delay
*= 2) {
152 if (errno
== EAGAIN
) {
153 // potentially recoverable resource shortage
155 secinfo("authexec", "resource shortage (EAGAIN), delaying %d seconds", delay
);
160 secinfo("authexec", "fork failed (errno=%d)", errno
);
161 close(notify
[READ
]); close(notify
[WRITE
]);
162 return errAuthorizationToolExecuteFailure
;
165 // close foreign side of pipes
166 close(notify
[WRITE
]);
167 if (communicationsPipe
)
170 // close mailbox file (child has it open now)
173 // get status notification from child
174 secinfo("authexec", "parent waiting for status");
175 ssize_t rc
= read(notify
[READ
], &status
, sizeof(status
));
176 status
= n2h(status
);
178 default: // weird result of read: post error
179 secinfo("authexec", "unexpected read return value %ld", long(rc
));
180 status
= errAuthorizationToolEnvironmentError
;
182 case sizeof(status
): // read succeeded: child reported an error
183 secinfo("authexec", "parent received status=%d", (int)status
);
185 if (communicationsPipe
) { close(comm
[READ
]); close(comm
[WRITE
]); }
187 case 0: // end of file: exec succeeded
189 if (communicationsPipe
)
190 *communicationsPipe
= fdopen(comm
[READ
], "r+");
191 secinfo("authexec", "parent resumes (no error)");
192 status
= errSecSuccess
;
199 // close foreign side of pipes
201 if (communicationsPipe
)
204 // fd 1 (stdout) holds the notify write end
205 dup2(notify
[WRITE
], 1);
206 close(notify
[WRITE
]);
208 // fd 0 (stdin) holds either the comm-link write-end or /dev/null
209 if (communicationsPipe
) {
210 dup2(comm
[WRITE
], 0);
214 open("/dev/null", O_RDWR
);
217 // okay, execute the trampoline
219 execv(trampoline
, (char *const*)argv
);
221 // execute failed - tell the parent
223 OSStatus error
= errAuthorizationToolExecuteFailure
;
225 write(1, &error
, sizeof(error
));
238 // Build an argv vector
240 static const char **argVector(const char *trampoline
, const char *pathToTool
,
241 const char *mboxFdText
, char *const *arguments
)
245 for (char *const *p
= arguments
; *p
; p
++)
248 if (const char **args
= (const char **)malloc(sizeof(const char *) * (length
+ 4))) {
249 args
[0] = trampoline
;
250 args
[1] = pathToTool
;
251 args
[2] = mboxFdText
;
253 for (int n
= 0; arguments
[n
]; n
++)
254 args
[n
+ 3] = arguments
[n
];
255 args
[length
+ 3] = NULL
;