]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCPCommit.c
configd-963.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCPCommit.c
1 /*
2 * Copyright (c) 2000-2008, 2010-2013, 2015-2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <TargetConditionals.h>
35 #include "SCPreferencesInternal.h"
36 #include "SCHelper_client.h"
37
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <sys/errno.h>
41
42 static Boolean
43 __SCPreferencesCommitChanges_helper(SCPreferencesRef prefs)
44 {
45 CFDataRef data = NULL;
46 Boolean ok;
47 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
48 uint32_t status = kSCStatusOK;
49 CFDataRef reply = NULL;
50
51 if (prefsPrivate->helper_port == MACH_PORT_NULL) {
52 // if no helper
53 status = kSCStatusAccessError;
54 goto fail;
55 }
56
57 if (prefsPrivate->changed) {
58 ok = _SCSerialize(prefsPrivate->prefs, &data, NULL, NULL);
59 if (!ok) {
60 status = kSCStatusFailed;
61 if (_sc_verbose) {
62 SC_log(LOG_NOTICE, "_SCSerialize() failed");
63 SC_log(LOG_NOTICE, " prefs = %s",
64 prefsPrivate->newPath ? prefsPrivate->newPath : prefsPrivate->path);
65 }
66 goto error;
67 }
68 }
69
70 // have the helper "commit" the prefs
71 // status = kSCStatusOK;
72 // reply = NULL;
73 ok = _SCHelperExec(prefsPrivate->helper_port,
74 SCHELPER_MSG_PREFS_COMMIT,
75 data,
76 &status,
77 &reply);
78 if (data != NULL) CFRelease(data);
79 if (!ok) {
80 goto fail;
81 }
82
83 if (status != kSCStatusOK) {
84 goto error;
85 }
86
87 if (prefsPrivate->changed) {
88 if (prefsPrivate->signature != NULL) CFRelease(prefsPrivate->signature);
89 prefsPrivate->signature = reply;
90 } else {
91 if (reply != NULL) CFRelease(reply);
92 }
93
94 prefsPrivate->changed = FALSE;
95 return TRUE;
96
97 fail :
98
99 // close helper
100 if (prefsPrivate->helper_port != MACH_PORT_NULL) {
101 _SCHelperClose(&prefsPrivate->helper_port);
102 }
103
104 error :
105
106 // return error
107 if (reply != NULL) CFRelease(reply);
108 _SCErrorSet(status);
109 return FALSE;
110 }
111
112
113 static ssize_t
114 writen(int ref, const void *data, size_t len)
115 {
116 size_t left = len;
117 ssize_t n;
118 const void *p = data;
119
120 while (left > 0) {
121 if ((n = write(ref, p, left)) == -1) {
122 if (errno != EINTR) {
123 return -1;
124 }
125 n = 0;
126 }
127 left -= n;
128 p += n;
129 }
130 return len;
131 }
132
133
134 Boolean
135 SCPreferencesCommitChanges(SCPreferencesRef prefs)
136 {
137 Boolean ok = FALSE;
138 char * path;
139 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
140 Boolean save = TRUE;
141 struct stat statBuf;
142 Boolean wasLocked;
143
144 if (prefs == NULL) {
145 /* sorry, you must provide a session */
146 _SCErrorSet(kSCStatusNoPrefsSession);
147 return FALSE;
148 }
149
150 /*
151 * Determine if the we have exclusive access to the preferences
152 * and acquire the lock if necessary.
153 */
154 wasLocked = prefsPrivate->locked;
155 if (!wasLocked) {
156 if (!SCPreferencesLock(prefs, TRUE)) {
157 SC_log(LOG_INFO, "SCPreferencesLock() failed");
158 return FALSE;
159 }
160 }
161
162 if (prefsPrivate->authorizationData != NULL) {
163 ok = __SCPreferencesCommitChanges_helper(prefs);
164 if (ok) {
165 prefsPrivate->changed = FALSE;
166 }
167 goto done;
168 }
169
170 /*
171 * if necessary, apply changes
172 */
173 if (!prefsPrivate->changed) {
174 goto committed;
175 }
176
177 /*
178 * check if the preferences should be removed
179 */
180 if (CFDictionaryGetCount(prefsPrivate->prefs) == 0) {
181 CFBooleanRef val;
182
183 /* if empty */
184 if ((prefsPrivate->options != NULL) &&
185 CFDictionaryGetValueIfPresent(prefsPrivate->options,
186 kSCPreferencesOptionRemoveWhenEmpty,
187 (const void **)&val) &&
188 isA_CFBoolean(val) &&
189 CFBooleanGetValue(val)) {
190 /* if we've been asked to remove empty .plists */
191 save = FALSE;
192 }
193 }
194
195 path = prefsPrivate->newPath ? prefsPrivate->newPath : prefsPrivate->path;
196 if (save) {
197 int fd;
198 CFDataRef newPrefs;
199 CFIndex pathLen;
200 #if TARGET_OS_EMBEDDED
201 CFStringRef protectionClass;
202 #endif // TARGET_OS_EMBEDDED
203 char * thePath;
204
205 if (stat(prefsPrivate->path, &statBuf) == -1) {
206 if (errno == ENOENT) {
207 bzero(&statBuf, sizeof(statBuf));
208 statBuf.st_mode = 0644;
209 statBuf.st_uid = geteuid();
210 statBuf.st_gid = getegid();
211 } else {
212 _SCErrorSet(errno);
213 SC_log(LOG_INFO, "stat() failed: %s", strerror(errno));
214 goto done;
215 }
216 }
217
218 /* create the (new) preferences file */
219 pathLen = strlen(path) + sizeof("-new");
220 thePath = CFAllocatorAllocate(NULL, pathLen, 0);
221 snprintf(thePath, pathLen, "%s-new", path);
222
223 #if TARGET_OS_EMBEDDED
224 if ((prefsPrivate->options != NULL) &&
225 CFDictionaryGetValueIfPresent(prefsPrivate->options,
226 kSCPreferencesOptionProtectionClass,
227 (const void **)&protectionClass)) {
228 int pc;
229 const char *str;
230
231 if (!isA_CFString(protectionClass) ||
232 (CFStringGetLength(protectionClass) != 1) ||
233 ((str = CFStringGetCStringPtr(protectionClass, kCFStringEncodingASCII)) == NULL) ||
234 (str[0] < 'A') || (str[0] > 'F')
235 ) {
236 _SCErrorSet(kSCStatusInvalidArgument);
237 goto done;
238 }
239
240 pc = str[0] - 'A' + 1; // PROTECTION_CLASS_[ABCDEF]
241 fd = open_dprotected_np(thePath, O_WRONLY|O_CREAT, pc, 0, statBuf.st_mode);
242 } else
243 #endif // TARGET_OS_EMBEDDED
244 fd = open(thePath, O_WRONLY|O_CREAT, statBuf.st_mode);
245
246 if (fd == -1) {
247 _SCErrorSet(errno);
248 SC_log(LOG_INFO, "open() failed: %s", strerror(errno));
249 CFAllocatorDeallocate(NULL, thePath);
250 goto done;
251 }
252
253 /* preserve permissions */
254 (void) fchown(fd, statBuf.st_uid, statBuf.st_gid);
255 (void) fchmod(fd, statBuf.st_mode);
256
257 /* write the new preferences */
258 newPrefs = CFPropertyListCreateData(NULL,
259 prefsPrivate->prefs,
260 #if TARGET_OS_IPHONE
261 kCFPropertyListBinaryFormat_v1_0,
262 #else // TARGET_OS_IPHONE
263 kCFPropertyListXMLFormat_v1_0,
264 #endif // TARGET_OS_IPHONE
265 0,
266 NULL);
267 if (!newPrefs) {
268 _SCErrorSet(kSCStatusFailed);
269 SC_log(LOG_INFO, "CFPropertyListCreateData() failed");
270 SC_log(LOG_INFO, " prefs = %s", path);
271 CFAllocatorDeallocate(NULL, thePath);
272 (void) close(fd);
273 goto done;
274 }
275 if (writen(fd, (const void *)CFDataGetBytePtr(newPrefs), CFDataGetLength(newPrefs)) == -1) {
276 _SCErrorSet(errno);
277 SC_log(LOG_INFO, "writen() failed: %s", strerror(errno));
278 SC_log(LOG_INFO, " path = %s", thePath);
279 (void) unlink(thePath);
280 CFAllocatorDeallocate(NULL, thePath);
281 (void) close(fd);
282 CFRelease(newPrefs);
283 goto done;
284 }
285
286 /* new preferences have been written */
287 if (close(fd) == -1) {
288 _SCErrorSet(errno);
289 SC_log(LOG_INFO, "close() failed: %s", strerror(errno));
290 SC_log(LOG_INFO, " path = %s", thePath);
291 (void) unlink(thePath);
292 CFAllocatorDeallocate(NULL, thePath);
293 CFRelease(newPrefs);
294 goto done;
295 }
296 CFRelease(newPrefs);
297
298 /* rename new->old */
299 if (rename(thePath, path) == -1) {
300 _SCErrorSet(errno);
301 SC_log(LOG_INFO, "rename() failed: %s", strerror(errno));
302 SC_log(LOG_INFO, " path = %s --> %s", thePath, path);
303 CFAllocatorDeallocate(NULL, thePath);
304 goto done;
305 }
306 CFAllocatorDeallocate(NULL, thePath);
307
308 if (prefsPrivate->newPath) {
309 /* prefs file saved in "new" directory */
310 (void) unlink(prefsPrivate->path);
311 (void) symlink(prefsPrivate->newPath, prefsPrivate->path);
312 CFAllocatorDeallocate(NULL, prefsPrivate->path);
313 prefsPrivate->path = path;
314 prefsPrivate->newPath = NULL;
315 }
316
317 /* grab the new signature */
318 if (stat(path, &statBuf) == -1) {
319 _SCErrorSet(errno);
320 SC_log(LOG_INFO, "stat() failed: %s", strerror(errno));
321 SC_log(LOG_INFO, " path = %s", thePath);
322 goto done;
323 }
324 } else {
325 /* remove the empty .plist */
326 unlink(path);
327
328 /* init the new signature */
329 bzero(&statBuf, sizeof(statBuf));
330 }
331
332 /* update signature */
333 if (prefsPrivate->signature != NULL) CFRelease(prefsPrivate->signature);
334 prefsPrivate->signature = __SCPSignatureFromStatbuf(&statBuf);
335
336 committed :
337
338 SC_log(LOG_INFO, "SCPreferences() commit: %s",
339 prefsPrivate->newPath ? prefsPrivate->newPath : prefsPrivate->path);
340
341 /* post notification */
342 ok = SCDynamicStoreNotifyValue(NULL, prefsPrivate->sessionKeyCommit);
343 if (!ok) {
344 SC_log(LOG_INFO, "SCDynamicStoreNotifyValue() failed");
345 _SCErrorSet(kSCStatusFailed);
346 goto done;
347 }
348
349 prefsPrivate->changed = FALSE;
350
351 done :
352
353 if (!wasLocked) {
354 uint32_t status;
355
356 status = SCError(); // preserve status across unlock
357 (void) SCPreferencesUnlock(prefs);
358 _SCErrorSet(status);
359 }
360 return ok;
361 }