]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/PreferencePane/PrivilegedOperations.c
mDNSResponder-170.tar.gz
[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.8 2007/11/30 23:42:33 cheshire
47 Fixed compile warning: declaration of 'status' shadows a previous local
48
49 Revision 1.7 2007/02/09 00:39:06 cheshire
50 Fix compile warnings
51
52 Revision 1.6 2006/08/14 23:15:47 cheshire
53 Tidy up Change History comment
54
55 Revision 1.5 2006/06/10 02:07:11 mkrochma
56 Whoa. Make sure code compiles before checking it in.
57
58 Revision 1.4 2006/05/27 02:32:38 mkrochma
59 Wait for installer script to exit before returning result
60
61 Revision 1.3 2005/06/04 04:50:00 cheshire
62 <rdar://problem/4138070> ddnswriteconfig (Bonjour PreferencePane) vulnerability
63 Use installtool instead of requiring ddnswriteconfig to self-install
64
65 Revision 1.2 2005/02/10 22:35:20 cheshire
66 <rdar://problem/3727944> Update name
67
68 Revision 1.1 2005/02/05 01:59:19 cheshire
69 Add Preference Pane to facilitate testing of DDNS & wide-area features
70
71 */
72
73 #include "PrivilegedOperations.h"
74 #include "ConfigurationAuthority.h"
75 #include <CoreFoundation/CoreFoundation.h>
76 #include <SystemConfiguration/SystemConfiguration.h>
77 #include <stdio.h>
78 #include <stdint.h>
79 #include <stdlib.h>
80 #include <unistd.h>
81 #include <sys/wait.h>
82 #include <AssertMacros.h>
83 #include <Security/Security.h>
84
85 Boolean gToolApproved = false;
86
87 static pid_t execTool(const char *args[])
88 // fork/exec and return new pid
89 {
90 pid_t child;
91
92 child = vfork();
93 if (child == 0)
94 {
95 execv(args[0], (char *const *)args);
96 printf("exec of %s failed; errno = %d\n", args[0], errno);
97 _exit(-1); // exec failed
98 }
99 else
100 return child;
101 }
102
103 OSStatus EnsureToolInstalled(void)
104 // Make sure that the tool is installed in the right place, with the right privs, and the right version.
105 {
106 CFURLRef bundleURL;
107 pid_t toolPID;
108 int status;
109 OSStatus err = noErr;
110 const char *args[] = { kToolPath, "0", "V", NULL };
111 char toolSourcePath[PATH_MAX] = {};
112 char toolInstallerPath[PATH_MAX] = {};
113
114 if (gToolApproved)
115 return noErr;
116
117 // Check version of installed tool
118 toolPID = execTool(args);
119 if (toolPID > 0)
120 {
121 waitpid(toolPID, &status, 0);
122 if (WIFEXITED(status) && WEXITSTATUS(status) == PRIV_OP_TOOL_VERS)
123 return noErr;
124 }
125
126 // Locate our in-bundle copy of privop tool
127 bundleURL = CFBundleCopyBundleURL(CFBundleGetBundleWithIdentifier(CFSTR("com.apple.preference.bonjour")) );
128 if (bundleURL != NULL)
129 {
130 CFURLGetFileSystemRepresentation(bundleURL, false, (UInt8*) toolSourcePath, sizeof toolSourcePath);
131 if (strlcat(toolSourcePath, "/Contents/Resources/" kToolName, sizeof toolSourcePath ) >= sizeof toolSourcePath ) return(-1);
132 CFURLGetFileSystemRepresentation(bundleURL, false, (UInt8*) toolInstallerPath, sizeof toolInstallerPath);
133 if (strlcat(toolInstallerPath, "/Contents/Resources/" kToolInstaller, sizeof toolInstallerPath) >= sizeof toolInstallerPath) return(-1);
134 }
135 else
136 return coreFoundationUnknownErr;
137
138 // Obtain authorization and run in-bundle copy as root to install it
139 {
140 AuthorizationItem aewpRight = { kAuthorizationRightExecute, strlen(toolInstallerPath), toolInstallerPath, 0 };
141 AuthorizationItemSet rights = { 1, &aewpRight };
142 AuthorizationRef authRef;
143
144 err = AuthorizationCreate(&rights, (AuthorizationEnvironment*) NULL,
145 kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights |
146 kAuthorizationFlagPreAuthorize, &authRef);
147 if (err == noErr)
148 {
149 char *installerargs[] = { toolSourcePath, NULL };
150 err = AuthorizationExecuteWithPrivileges(authRef, toolInstallerPath, 0, installerargs, (FILE**) NULL);
151 if (err == noErr) {
152 int pid = wait(&status);
153 if (pid > 0 && WIFEXITED(status)) {
154 err = WEXITSTATUS(status);
155 if (err == noErr) {
156 gToolApproved = true;
157 }
158 } else {
159 err = -1;
160 }
161 }
162 (void) AuthorizationFree(authRef, kAuthorizationFlagDestroyRights);
163 }
164 }
165
166 return err;
167 }
168
169
170 static OSStatus ExecWithCmdAndParam(const char *subCmd, CFDataRef paramData)
171 // Execute our privop tool with the supplied subCmd and parameter
172 {
173 OSStatus err = noErr;
174 int commFD, dataLen;
175 u_int32_t len;
176 pid_t child;
177 char fileNum[16];
178 UInt8 *buff;
179 const char *args[] = { kToolPath, NULL, "A", NULL, NULL };
180 AuthorizationExternalForm authExt;
181
182 err = ExternalizeAuthority(&authExt);
183 require_noerr(err, AuthFailed);
184
185 dataLen = CFDataGetLength(paramData);
186 buff = (UInt8*) malloc(dataLen * sizeof(UInt8));
187 require_action(buff != NULL, AllocBuffFailed, err=memFullErr;);
188 {
189 CFRange all = { 0, dataLen };
190 CFDataGetBytes(paramData, all, buff);
191 }
192
193 commFD = fileno(tmpfile());
194 sprintf(fileNum, "%d", commFD);
195 args[1] = fileNum;
196 args[3] = subCmd;
197
198 // write authority to pipe
199 len = 0; // tag, unused
200 write(commFD, &len, sizeof len);
201 len = sizeof authExt; // len
202 write(commFD, &len, sizeof len);
203 write(commFD, &authExt, len);
204
205 // write parameter to pipe
206 len = 0; // tag, unused
207 write(commFD, &len, sizeof len);
208 len = dataLen; // len
209 write(commFD, &len, sizeof len);
210 write(commFD, buff, len);
211
212 child = execTool(args);
213 if (child > 0) {
214 int status;
215 waitpid(child, &status, 0);
216 if (WIFEXITED(status))
217 err = WEXITSTATUS(status);
218 //fprintf(stderr, "child exited; status = %d (%ld)\n", status, err);
219 }
220
221 close(commFD);
222
223 free(buff);
224 AllocBuffFailed:
225 AuthFailed:
226 return err;
227 }
228
229 OSStatus
230 WriteBrowseDomain(CFDataRef domainArrayData)
231 {
232 if (!CurrentlyAuthorized())
233 return authFailErr;
234 return ExecWithCmdAndParam("Wb", domainArrayData);
235 }
236
237 OSStatus
238 WriteRegistrationDomain(CFDataRef domainArrayData)
239 {
240 if (!CurrentlyAuthorized())
241 return authFailErr;
242 return ExecWithCmdAndParam("Wd", domainArrayData);
243 }
244
245 OSStatus
246 WriteHostname(CFDataRef domainArrayData)
247 {
248 if (!CurrentlyAuthorized())
249 return authFailErr;
250 return ExecWithCmdAndParam("Wh", domainArrayData);
251 }
252
253 OSStatus
254 SetKeyForDomain(CFDataRef secretData)
255 {
256 if (!CurrentlyAuthorized())
257 return authFailErr;
258 return ExecWithCmdAndParam("Wk", secretData);
259 }