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