]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/PreferencePane/PrivilegedOperations.c
0ca00dbb2e7b11737755a1580b40f78d2b131ebb
[apple/mdnsresponder.git] / mDNSMacOSX / PreferencePane / PrivilegedOperations.c
1 /*
2 File: PrivilegedOperations.c
3
4 Abstract: Interface to "ddnswriteconfig" setuid root tool.
5
6 Copyright: (c) Copyright 2005 Apple Computer, Inc. All rights reserved.
7
8 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
9 ("Apple") in consideration of your agreement to the following terms, and your
10 use, installation, modification or redistribution of this Apple software
11 constitutes acceptance of these terms. If you do not agree with these terms,
12 please do not use, install, modify or redistribute this Apple software.
13
14 In consideration of your agreement to abide by the following terms, and subject
15 to these terms, Apple grants you a personal, non-exclusive license, under Apple's
16 copyrights in this original Apple software (the "Apple Software"), to use,
17 reproduce, modify and redistribute the Apple Software, with or without
18 modifications, in source and/or binary forms; provided that if you redistribute
19 the Apple Software in its entirety and without modifications, you must retain
20 this notice and the following text and disclaimers in all such redistributions of
21 the Apple Software. Neither the name, trademarks, service marks or logos of
22 Apple Computer, Inc. may be used to endorse or promote products derived from the
23 Apple Software without specific prior written permission from Apple. Except as
24 expressly stated in this notice, no other rights or licenses, express or implied,
25 are granted by Apple herein, including but not limited to any patent rights that
26 may be infringed by your derivative works or by other works in which the Apple
27 Software may be incorporated.
28
29 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
30 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
31 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
33 COMBINATION WITH YOUR PRODUCTS.
34
35 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
36 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
39 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
40 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
41 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43 Change History (most recent first):
44
45 $Log: PrivilegedOperations.c,v $
46 Revision 1.6 2006/08/14 23:15:47 cheshire
47 Tidy up Change History comment
48
49 Revision 1.5 2006/06/10 02:07:11 mkrochma
50 Whoa. Make sure code compiles before checking it in.
51
52 Revision 1.4 2006/05/27 02:32:38 mkrochma
53 Wait for installer script to exit before returning result
54
55 Revision 1.3 2005/06/04 04:50:00 cheshire
56 <rdar://problem/4138070> ddnswriteconfig (Bonjour PreferencePane) vulnerability
57 Use installtool instead of requiring ddnswriteconfig to self-install
58
59 Revision 1.2 2005/02/10 22:35:20 cheshire
60 <rdar://problem/3727944> Update name
61
62 Revision 1.1 2005/02/05 01:59:19 cheshire
63 Add Preference Pane to facilitate testing of DDNS & wide-area features
64
65 */
66
67 #include "PrivilegedOperations.h"
68 #include "ConfigurationAuthority.h"
69 #include <CoreFoundation/CoreFoundation.h>
70 #include <SystemConfiguration/SystemConfiguration.h>
71 #include <stdio.h>
72 #include <stdint.h>
73 #include <stdlib.h>
74 #include <unistd.h>
75 #include <sys/wait.h>
76 #include <AssertMacros.h>
77 #include <Security/Security.h>
78
79 Boolean gToolApproved = false;
80
81 pid_t execTool(const char *args[])
82 // fork/exec and return new pid
83 {
84 pid_t child;
85
86 child = vfork();
87 if (child == 0)
88 {
89 execv(args[0], (char *const *)args);
90 printf("exec of %s failed; errno = %d\n", args[0], errno);
91 _exit(-1); // exec failed
92 }
93 else
94 return child;
95 }
96
97 OSStatus EnsureToolInstalled(void)
98 // Make sure that the tool is installed in the right place, with the right privs, and the right version.
99 {
100 CFURLRef bundleURL;
101 pid_t toolPID;
102 int status;
103 OSStatus err = noErr;
104 const char *args[] = { kToolPath, "0", "V", NULL };
105 char toolSourcePath[PATH_MAX] = {};
106 char toolInstallerPath[PATH_MAX] = {};
107
108 if (gToolApproved)
109 return noErr;
110
111 // Check version of installed tool
112 toolPID = execTool(args);
113 if (toolPID > 0)
114 {
115 waitpid(toolPID, &status, 0);
116 if (WIFEXITED(status) && WEXITSTATUS(status) == PRIV_OP_TOOL_VERS)
117 return noErr;
118 }
119
120 // Locate our in-bundle copy of privop tool
121 bundleURL = CFBundleCopyBundleURL(CFBundleGetBundleWithIdentifier(CFSTR("com.apple.preference.bonjour")) );
122 if (bundleURL != NULL)
123 {
124 CFURLGetFileSystemRepresentation(bundleURL, false, (UInt8*) toolSourcePath, sizeof toolSourcePath);
125 if (strlcat(toolSourcePath, "/Contents/Resources/" kToolName, sizeof toolSourcePath ) >= sizeof toolSourcePath ) return(-1);
126 CFURLGetFileSystemRepresentation(bundleURL, false, (UInt8*) toolInstallerPath, sizeof toolInstallerPath);
127 if (strlcat(toolInstallerPath, "/Contents/Resources/" kToolInstaller, sizeof toolInstallerPath) >= sizeof toolInstallerPath) return(-1);
128 }
129 else
130 return coreFoundationUnknownErr;
131
132 // Obtain authorization and run in-bundle copy as root to install it
133 {
134 AuthorizationItem aewpRight = { kAuthorizationRightExecute, strlen(toolInstallerPath), toolInstallerPath, 0 };
135 AuthorizationItemSet rights = { 1, &aewpRight };
136 AuthorizationRef authRef;
137
138 err = AuthorizationCreate(&rights, (AuthorizationEnvironment*) NULL,
139 kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights |
140 kAuthorizationFlagPreAuthorize, &authRef);
141 if (err == noErr)
142 {
143 char *installerargs[] = { toolSourcePath, NULL };
144 err = AuthorizationExecuteWithPrivileges(authRef, toolInstallerPath, 0, installerargs, (FILE**) NULL);
145 if (err == noErr) {
146 int status;
147 int pid = wait(&status);
148 if (pid > 0 && WIFEXITED(status)) {
149 err = WEXITSTATUS(status);
150 if (err == noErr) {
151 gToolApproved = true;
152 }
153 } else {
154 err = -1;
155 }
156 }
157 (void) AuthorizationFree(authRef, kAuthorizationFlagDestroyRights);
158 }
159 }
160
161 return err;
162 }
163
164
165 static OSStatus ExecWithCmdAndParam(const char *subCmd, CFDataRef paramData)
166 // Execute our privop tool with the supplied subCmd and parameter
167 {
168 OSStatus err = noErr;
169 int commFD, dataLen;
170 u_int32_t len;
171 pid_t child;
172 char fileNum[16];
173 UInt8 *buff;
174 const char *args[] = { kToolPath, NULL, "A", NULL, NULL };
175 AuthorizationExternalForm authExt;
176
177 err = ExternalizeAuthority(&authExt);
178 require_noerr(err, AuthFailed);
179
180 dataLen = CFDataGetLength(paramData);
181 buff = (UInt8*) malloc(dataLen * sizeof(UInt8));
182 require_action(buff != NULL, AllocBuffFailed, err=memFullErr;);
183 {
184 CFRange all = { 0, dataLen };
185 CFDataGetBytes(paramData, all, buff);
186 }
187
188 commFD = fileno(tmpfile());
189 sprintf(fileNum, "%d", commFD);
190 args[1] = fileNum;
191 args[3] = subCmd;
192
193 // write authority to pipe
194 len = 0; // tag, unused
195 write(commFD, &len, sizeof len);
196 len = sizeof authExt; // len
197 write(commFD, &len, sizeof len);
198 write(commFD, &authExt, len);
199
200 // write parameter to pipe
201 len = 0; // tag, unused
202 write(commFD, &len, sizeof len);
203 len = dataLen; // len
204 write(commFD, &len, sizeof len);
205 write(commFD, buff, len);
206
207 child = execTool(args);
208 if (child > 0) {
209 int status;
210 waitpid(child, &status, 0);
211 if (WIFEXITED(status))
212 err = WEXITSTATUS(status);
213 //fprintf(stderr, "child exited; status = %d (%ld)\n", status, err);
214 }
215
216 close(commFD);
217
218 free(buff);
219 AllocBuffFailed:
220 AuthFailed:
221 return err;
222 }
223
224 OSStatus
225 WriteBrowseDomain(CFDataRef domainArrayData)
226 {
227 if (!CurrentlyAuthorized())
228 return authFailErr;
229 return ExecWithCmdAndParam("Wb", domainArrayData);
230 }
231
232 OSStatus
233 WriteRegistrationDomain(CFDataRef domainArrayData)
234 {
235 if (!CurrentlyAuthorized())
236 return authFailErr;
237 return ExecWithCmdAndParam("Wd", domainArrayData);
238 }
239
240 OSStatus
241 WriteHostname(CFDataRef domainArrayData)
242 {
243 if (!CurrentlyAuthorized())
244 return authFailErr;
245 return ExecWithCmdAndParam("Wh", domainArrayData);
246 }
247
248 OSStatus
249 SetKeyForDomain(CFDataRef secretData)
250 {
251 if (!CurrentlyAuthorized())
252 return authFailErr;
253 return ExecWithCmdAndParam("Wk", secretData);
254 }