]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCPCommit.c
configd-24.1.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCPCommit.c
1 /*
2 * Copyright(c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1(the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <SystemConfiguration/SCP.h>
24 #include "SCPPrivate.h"
25
26 #include <SystemConfiguration/SCD.h>
27
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/errno.h>
31
32
33 SCPStatus
34 SCPCommit(SCPSessionRef session)
35 {
36 SCPSessionPrivateRef sessionPrivate;
37 SCPStatus scp_status = SCP_OK;
38 SCDStatus scd_status;
39 boolean_t wasLocked;
40
41 if (session == NULL) {
42 return SCP_FAILED; /* you can't do anything with a closed session */
43 }
44 sessionPrivate = (SCPSessionPrivateRef)session;
45
46 /*
47 * Determine if the we have exclusive access to the preferences
48 * and acquire the lock if necessary.
49 */
50 wasLocked = sessionPrivate->locked;
51 if (!wasLocked) {
52 scp_status = SCPLock(session, TRUE);
53 if (scp_status != SCD_OK) {
54 SCDLog(LOG_DEBUG, CFSTR(" SCPLock(): %s"), SCPError(scp_status));
55 return scp_status;
56 }
57 }
58
59 /*
60 * if necessary, apply changes
61 */
62 if (sessionPrivate->changed) {
63 struct stat statBuf;
64 int pathLen;
65 char *newPath;
66 int fd;
67 CFDataRef newPrefs;
68
69 if (stat(sessionPrivate->path, &statBuf) == -1) {
70 if (errno == ENOENT) {
71 bzero(&statBuf, sizeof(statBuf));
72 statBuf.st_mode = 0644;
73 statBuf.st_uid = geteuid();
74 statBuf.st_gid = getegid();
75 } else {
76 SCDLog(LOG_DEBUG, CFSTR("stat() failed: %s"), strerror(errno));
77 scp_status = SCP_FAILED;
78 goto done;
79 }
80 }
81
82 /* create the (new) preferences file */
83 pathLen = strlen(sessionPrivate->path) + sizeof("-new");
84 newPath = CFAllocatorAllocate(NULL, pathLen, 0);
85 snprintf(newPath, pathLen, "%s-new", sessionPrivate->path);
86
87 /* open the (new) preferences file */
88 reopen :
89 fd = open(newPath, O_WRONLY|O_CREAT, statBuf.st_mode);
90 if (fd == -1) {
91 if ((errno == ENOENT) &&
92 ((sessionPrivate->prefsID == NULL) || !CFStringHasPrefix(sessionPrivate->prefsID, CFSTR("/")))) {
93 char *ch;
94
95 ch = strrchr(newPath, '/');
96 if (ch != NULL) {
97 int status;
98
99 *ch = '\0';
100 status = mkdir(newPath, 0755);
101 *ch = '/';
102 if (status == 0) {
103 goto reopen;
104 }
105 }
106 }
107 SCDLog(LOG_DEBUG, CFSTR("SCPCommit open() failed: %s"), strerror(errno));
108 CFAllocatorDeallocate(NULL, newPath);
109 scp_status = SCP_FAILED;
110 goto done;
111 }
112
113 /* preserve permissions */
114 (void)fchown(fd, statBuf.st_uid, statBuf.st_gid);
115
116 /* write the new preferences */
117 newPrefs = CFPropertyListCreateXMLData(NULL, sessionPrivate->prefs);
118 (void) write(fd, CFDataGetBytePtr(newPrefs), CFDataGetLength(newPrefs));
119 (void) close(fd);
120 CFRelease(newPrefs);
121
122 /* rename new->old */
123 if (rename(newPath, sessionPrivate->path) == -1) {
124 SCDLog(LOG_DEBUG, CFSTR("rename() failed: %s"), strerror(errno));
125 CFAllocatorDeallocate(NULL, newPath);
126 scp_status = SCP_FAILED;
127 goto done;
128 }
129 CFAllocatorDeallocate(NULL, newPath);
130
131 /* update signature */
132 if (stat(sessionPrivate->path, &statBuf) == -1) {
133 SCDLog(LOG_DEBUG, CFSTR("stat() failed: %s"), strerror(errno));
134 scp_status = SCP_FAILED;
135 goto done;
136 }
137 CFRelease(sessionPrivate->signature);
138 sessionPrivate->signature = _SCPSignatureFromStatbuf(&statBuf);
139 }
140
141 if (!sessionPrivate->isRoot) {
142 /* CONFIGD REALLY NEEDS NON-ROOT WRITE ACCESS */
143 goto done;
144 }
145
146 /* if necessary, create the session "commit" key */
147 if (sessionPrivate->sessionKeyCommit == NULL) {
148 sessionPrivate->sessionKeyCommit = _SCPNotificationKey(sessionPrivate->prefsID,
149 sessionPrivate->perUser,
150 sessionPrivate->user,
151 kSCPKeyCommit);
152 }
153
154 /* post notification */
155 scd_status = SCDLock(sessionPrivate->session);
156 if (scd_status == SCD_OK) {
157 (void) SCDTouch (sessionPrivate->session, sessionPrivate->sessionKeyCommit);
158 (void) SCDRemove(sessionPrivate->session, sessionPrivate->sessionKeyCommit);
159 (void) SCDUnlock(sessionPrivate->session);
160 } else {
161 SCDLog(LOG_DEBUG, CFSTR(" SCDLock(): %s"), SCDError(scd_status));
162 scp_status = SCP_FAILED;
163 }
164
165 done :
166
167 if (!wasLocked)
168 (void) SCPUnlock(session);
169
170 sessionPrivate->changed = FALSE;
171
172 return scp_status;
173 }