]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
configd-1061.0.2.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCP.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009, 2011, 2014-2018 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 "SCPreferencesInternal.h"
35 #include "SCNetworkConfigurationInternal.h"
36
37 #include <fcntl.h>
38 #include <pwd.h>
39 #include <unistd.h>
40 #include <sys/errno.h>
41 #include <sys/param.h>
42
43 __private_extern__ CF_RETURNS_RETAINED CFDataRef
44 __SCPSignatureFromStatbuf(const struct stat *statBuf)
45 {
46 CFMutableDataRef signature;
47 SCPSignatureDataRef sig;
48
49 signature = CFDataCreateMutable(NULL, sizeof(SCPSignatureData));
50 CFDataSetLength(signature, sizeof(SCPSignatureData));
51
52 /* ALIGN: CFDataGetBytePtr aligns to at least 8 bytes */
53 sig = (SCPSignatureDataRef)(void *)CFDataGetBytePtr(signature);
54
55 sig->st_dev = statBuf->st_dev;
56 sig->st_ino = statBuf->st_ino;
57 sig->tv_sec = statBuf->st_mtimespec.tv_sec;
58 sig->tv_nsec = statBuf->st_mtimespec.tv_nsec;
59 sig->st_size = statBuf->st_size;
60 return signature;
61 }
62
63
64 __private_extern__ char *
65 __SCPreferencesPath(CFAllocatorRef allocator,
66 CFStringRef prefsID,
67 Boolean useNewPrefs)
68 {
69 CFStringRef path = NULL;
70 char *pathStr;
71
72 if (prefsID == NULL) {
73 /* default preference ID */
74 path = CFStringCreateWithFormat(allocator,
75 NULL,
76 CFSTR("%@/%@"),
77 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
78 useNewPrefs ? PREFS_DEFAULT_CONFIG : PREFS_DEFAULT_CONFIG_OLD);
79 } else if (CFStringHasPrefix(prefsID, CFSTR("/"))) {
80 /* if absolute path */
81 path = CFStringCreateCopy(allocator, prefsID);
82 } else {
83 /* relative path */
84 path = CFStringCreateWithFormat(allocator,
85 NULL,
86 CFSTR("%@/%@"),
87 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
88 prefsID);
89 if (useNewPrefs && CFStringHasSuffix(path, CFSTR(".xml"))) {
90 CFMutableStringRef newPath;
91
92 newPath = CFStringCreateMutableCopy(allocator, 0, path);
93 CFStringReplace(newPath,
94 CFRangeMake(CFStringGetLength(newPath)-4, 4),
95 CFSTR(".plist"));
96 CFRelease(path);
97 path = newPath;
98 }
99 }
100
101 /*
102 * convert CFStringRef path to C-string path
103 */
104 pathStr = _SC_cfstring_to_cstring(path, NULL, 0, kCFStringEncodingASCII);
105 if (pathStr == NULL) {
106 CFIndex pathLen;
107
108 pathLen = CFStringGetMaximumSizeOfFileSystemRepresentation(path);
109 pathStr = CFAllocatorAllocate(NULL, pathLen, 0);
110 if (!CFStringGetFileSystemRepresentation(path, pathStr, pathLen)) {
111 SC_log(LOG_INFO, "could not convert path to C string");
112 CFAllocatorDeallocate(NULL, pathStr);
113 pathStr = NULL;
114 }
115 }
116
117 CFRelease(path);
118 return pathStr;
119 }
120
121
122 __private_extern__
123 Boolean
124 __SCPreferencesGetLimitSCNetworkConfiguration(SCPreferencesRef prefs)
125 {
126 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
127
128 if (prefs == NULL) {
129 return FALSE;
130 }
131 return prefsPrivate->limit_SCNetworkConfiguration;
132 }
133
134
135 __private_extern__
136 off_t
137 __SCPreferencesPrefsSize(SCPreferencesRef prefs)
138 {
139 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
140 SCPSignatureDataRef sig;
141 CFDataRef signature;
142
143 signature = prefsPrivate->signature;
144 if (signature == NULL) {
145 return 0;
146 }
147
148 sig = (SCPSignatureDataRef)(void *)CFDataGetBytePtr(signature);
149 return sig->st_size;
150 }
151
152
153 __private_extern__
154 Boolean
155 __SCPreferencesUsingDefaultPrefs(SCPreferencesRef prefs)
156 {
157 char *curPath;
158 Boolean isDefault = FALSE;
159 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
160
161 curPath = prefsPrivate->newPath ? prefsPrivate->newPath : prefsPrivate->path;
162 if (curPath != NULL) {
163 char* defPath;
164
165 defPath = __SCPreferencesPath(NULL,
166 NULL,
167 (prefsPrivate->newPath == NULL));
168 if (defPath != NULL) {
169 if (strcmp(curPath, defPath) == 0) {
170 isDefault = TRUE;
171 }
172 CFAllocatorDeallocate(NULL, defPath);
173 }
174 }
175 return isDefault;
176 }
177
178 __private_extern__
179 SCPreferencesRef
180 __SCPreferencesCreateNIPrefsFromPrefs(SCPreferencesRef prefs)
181 {
182 CFMutableStringRef newPath = NULL;
183 CFURLRef newURL = NULL;
184 SCPreferencesRef ni_prefs = NULL;
185 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
186 char * prefsPath = __SCPreferencesPath(NULL, prefsPrivate->prefsID, FALSE);
187
188
189 newPath = CFStringCreateMutable(NULL, 0);
190 CFStringAppendFormat(newPath, NULL, CFSTR("%s"), prefsPath);
191
192 CFStringFindAndReplace(newPath,
193 PREFS_DEFAULT_CONFIG,
194 NETWORK_INTERFACES_PREFS,
195 CFRangeMake(0, CFStringGetLength(newPath)),
196 kCFCompareBackwards);
197
198 newURL = CFURLCreateWithFileSystemPath(NULL, newPath, kCFURLPOSIXPathStyle, FALSE);
199 if (!CFURLResourceIsReachable(newURL, NULL)) {
200 ni_prefs = __SCNetworkCreateDefaultNIPrefs(newPath);
201 } else {
202 ni_prefs = SCPreferencesCreate(NULL, prefsPrivate->name, newPath);
203 }
204 CFAllocatorDeallocate(NULL, prefsPath);
205 CFRelease(newPath);
206 CFRelease(newURL);
207
208 return ni_prefs;
209 }
210
211 CFDataRef
212 SCPreferencesGetSignature(SCPreferencesRef prefs)
213 {
214 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
215
216 if (prefs == NULL) {
217 /* sorry, you must provide a session */
218 _SCErrorSet(kSCStatusNoPrefsSession);
219 return NULL;
220 }
221
222 __SCPreferencesAccess(prefs);
223
224 return prefsPrivate->signature;
225 }
226
227
228 __private_extern__ CF_RETURNS_RETAINED CFStringRef
229 _SCPNotificationKey(CFAllocatorRef allocator,
230 CFStringRef prefsID,
231 int keyType)
232 {
233 CFStringRef keyStr;
234 char *path;
235 CFStringRef pathStr;
236 CFStringRef storeKey;
237
238 switch (keyType) {
239 case kSCPreferencesKeyLock :
240 keyStr = CFSTR("lock");
241 break;
242 case kSCPreferencesKeyCommit :
243 keyStr = CFSTR("commit");
244 break;
245 case kSCPreferencesKeyApply :
246 keyStr = CFSTR("apply");
247 break;
248 default :
249 return NULL;
250 }
251
252 path = __SCPreferencesPath(allocator, prefsID, TRUE);
253 if (path == NULL) {
254 return NULL;
255 }
256
257 pathStr = CFStringCreateWithCStringNoCopy(allocator,
258 path,
259 kCFStringEncodingASCII,
260 kCFAllocatorNull);
261
262 storeKey = CFStringCreateWithFormat(allocator,
263 NULL,
264 CFSTR("%@%@:%@"),
265 kSCDynamicStoreDomainPrefs,
266 keyStr,
267 pathStr);
268
269 CFRelease(pathStr);
270 CFAllocatorDeallocate(NULL, path);
271 return storeKey;
272 }
273
274
275 CFStringRef
276 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
277 CFStringRef prefsID,
278 SCPreferencesKeyType keyType)
279 {
280 return _SCPNotificationKey(allocator, prefsID, keyType);
281 }
282
283
284 __private_extern__ void
285 __SCPreferencesSetLimitSCNetworkConfiguration(SCPreferencesRef prefs,
286 Boolean limit_SCNetworkConfiguration)
287 {
288 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
289
290 if (prefs == NULL) {
291 return;
292 }
293 prefsPrivate->limit_SCNetworkConfiguration = limit_SCNetworkConfiguration;
294 }