2 File: PrivilegedOperations.c
4 Abstract: Interface to "ddnswriteconfig" setuid root tool.
6 Copyright: (c) Copyright 2005 Apple Computer, Inc. All rights reserved.
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.
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.
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.
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.
43 Change History (most recent first):
45 $Log: PrivilegedOperations.c,v $
46 Revision 1.6 2006/08/14 23:15:47 cheshire
47 Tidy up Change History comment
49 Revision 1.5 2006/06/10 02:07:11 mkrochma
50 Whoa. Make sure code compiles before checking it in.
52 Revision 1.4 2006/05/27 02:32:38 mkrochma
53 Wait for installer script to exit before returning result
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
59 Revision 1.2 2005/02/10 22:35:20 cheshire
60 <rdar://problem/3727944> Update name
62 Revision 1.1 2005/02/05 01:59:19 cheshire
63 Add Preference Pane to facilitate testing of DDNS & wide-area features
67 #include "PrivilegedOperations.h"
68 #include "ConfigurationAuthority.h"
69 #include <CoreFoundation/CoreFoundation.h>
70 #include <SystemConfiguration/SystemConfiguration.h>
76 #include <AssertMacros.h>
77 #include <Security/Security.h>
79 Boolean gToolApproved
= false;
81 pid_t
execTool(const char *args
[])
82 // fork/exec and return new pid
89 execv(args
[0], (char *const *)args
);
90 printf("exec of %s failed; errno = %d\n", args
[0], errno
);
91 _exit(-1); // exec failed
97 OSStatus
EnsureToolInstalled(void)
98 // Make sure that the tool is installed in the right place, with the right privs, and the right version.
103 OSStatus err
= noErr
;
104 const char *args
[] = { kToolPath
, "0", "V", NULL
};
105 char toolSourcePath
[PATH_MAX
] = {};
106 char toolInstallerPath
[PATH_MAX
] = {};
111 // Check version of installed tool
112 toolPID
= execTool(args
);
115 waitpid(toolPID
, &status
, 0);
116 if (WIFEXITED(status
) && WEXITSTATUS(status
) == PRIV_OP_TOOL_VERS
)
120 // Locate our in-bundle copy of privop tool
121 bundleURL
= CFBundleCopyBundleURL(CFBundleGetBundleWithIdentifier(CFSTR("com.apple.preference.bonjour")) );
122 if (bundleURL
!= NULL
)
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);
130 return coreFoundationUnknownErr
;
132 // Obtain authorization and run in-bundle copy as root to install it
134 AuthorizationItem aewpRight
= { kAuthorizationRightExecute
, strlen(toolInstallerPath
), toolInstallerPath
, 0 };
135 AuthorizationItemSet rights
= { 1, &aewpRight
};
136 AuthorizationRef authRef
;
138 err
= AuthorizationCreate(&rights
, (AuthorizationEnvironment
*) NULL
,
139 kAuthorizationFlagInteractionAllowed
| kAuthorizationFlagExtendRights
|
140 kAuthorizationFlagPreAuthorize
, &authRef
);
143 char *installerargs
[] = { toolSourcePath
, NULL
};
144 err
= AuthorizationExecuteWithPrivileges(authRef
, toolInstallerPath
, 0, installerargs
, (FILE**) NULL
);
147 int pid
= wait(&status
);
148 if (pid
> 0 && WIFEXITED(status
)) {
149 err
= WEXITSTATUS(status
);
151 gToolApproved
= true;
157 (void) AuthorizationFree(authRef
, kAuthorizationFlagDestroyRights
);
165 static OSStatus
ExecWithCmdAndParam(const char *subCmd
, CFDataRef paramData
)
166 // Execute our privop tool with the supplied subCmd and parameter
168 OSStatus err
= noErr
;
174 const char *args
[] = { kToolPath
, NULL
, "A", NULL
, NULL
};
175 AuthorizationExternalForm authExt
;
177 err
= ExternalizeAuthority(&authExt
);
178 require_noerr(err
, AuthFailed
);
180 dataLen
= CFDataGetLength(paramData
);
181 buff
= (UInt8
*) malloc(dataLen
* sizeof(UInt8
));
182 require_action(buff
!= NULL
, AllocBuffFailed
, err
=memFullErr
;);
184 CFRange all
= { 0, dataLen
};
185 CFDataGetBytes(paramData
, all
, buff
);
188 commFD
= fileno(tmpfile());
189 sprintf(fileNum
, "%d", commFD
);
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
);
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
);
207 child
= execTool(args
);
210 waitpid(child
, &status
, 0);
211 if (WIFEXITED(status
))
212 err
= WEXITSTATUS(status
);
213 //fprintf(stderr, "child exited; status = %d (%ld)\n", status, err);
225 WriteBrowseDomain(CFDataRef domainArrayData
)
227 if (!CurrentlyAuthorized())
229 return ExecWithCmdAndParam("Wb", domainArrayData
);
233 WriteRegistrationDomain(CFDataRef domainArrayData
)
235 if (!CurrentlyAuthorized())
237 return ExecWithCmdAndParam("Wd", domainArrayData
);
241 WriteHostname(CFDataRef domainArrayData
)
243 if (!CurrentlyAuthorized())
245 return ExecWithCmdAndParam("Wh", domainArrayData
);
249 SetKeyForDomain(CFDataRef secretData
)
251 if (!CurrentlyAuthorized())
253 return ExecWithCmdAndParam("Wk", secretData
);