]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
configd-963.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCP.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009, 2011, 2014-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 "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 Boolean
137 __SCPreferencesUsingDefaultPrefs(SCPreferencesRef prefs)
138 {
139 char *curPath;
140 Boolean isDefault = FALSE;
141 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
142
143 curPath = prefsPrivate->newPath ? prefsPrivate->newPath : prefsPrivate->path;
144 if (curPath != NULL) {
145 char* defPath;
146
147 defPath = __SCPreferencesPath(NULL,
148 NULL,
149 (prefsPrivate->newPath == NULL));
150 if (defPath != NULL) {
151 if (strcmp(curPath, defPath) == 0) {
152 isDefault = TRUE;
153 }
154 CFAllocatorDeallocate(NULL, defPath);
155 }
156 }
157 return isDefault;
158 }
159
160 __private_extern__
161 SCPreferencesRef
162 __SCPreferencesCreateNIPrefsFromPrefs(SCPreferencesRef prefs)
163 {
164 CFMutableStringRef newPath = NULL;
165 CFURLRef newURL = NULL;
166 SCPreferencesRef ni_prefs = NULL;
167 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
168 char * prefsPath = __SCPreferencesPath(NULL, prefsPrivate->prefsID, FALSE);
169
170
171 newPath = CFStringCreateMutable(NULL, 0);
172 CFStringAppendFormat(newPath, NULL, CFSTR("%s"), prefsPath);
173
174 CFStringFindAndReplace(newPath,
175 PREFS_DEFAULT_CONFIG,
176 NETWORK_INTERFACES_PREFS,
177 CFRangeMake(0, CFStringGetLength(newPath)),
178 kCFCompareBackwards);
179
180 newURL = CFURLCreateWithFileSystemPath(NULL, newPath, kCFURLPOSIXPathStyle, FALSE);
181 if (!CFURLResourceIsReachable(newURL, NULL)) {
182 ni_prefs = __SCNetworkCreateDefaultNIPrefs(newPath);
183 } else {
184 ni_prefs = SCPreferencesCreate(NULL, prefsPrivate->name, newPath);
185 }
186 CFAllocatorDeallocate(NULL, prefsPath);
187 CFRelease(newPath);
188 CFRelease(newURL);
189
190 return ni_prefs;
191 }
192
193 CFDataRef
194 SCPreferencesGetSignature(SCPreferencesRef prefs)
195 {
196 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
197
198 if (prefs == NULL) {
199 /* sorry, you must provide a session */
200 _SCErrorSet(kSCStatusNoPrefsSession);
201 return NULL;
202 }
203
204 __SCPreferencesAccess(prefs);
205
206 return prefsPrivate->signature;
207 }
208
209
210 __private_extern__ CF_RETURNS_RETAINED CFStringRef
211 _SCPNotificationKey(CFAllocatorRef allocator,
212 CFStringRef prefsID,
213 int keyType)
214 {
215 CFStringRef keyStr;
216 char *path;
217 CFStringRef pathStr;
218 CFStringRef storeKey;
219
220 switch (keyType) {
221 case kSCPreferencesKeyLock :
222 keyStr = CFSTR("lock");
223 break;
224 case kSCPreferencesKeyCommit :
225 keyStr = CFSTR("commit");
226 break;
227 case kSCPreferencesKeyApply :
228 keyStr = CFSTR("apply");
229 break;
230 default :
231 return NULL;
232 }
233
234 path = __SCPreferencesPath(allocator, prefsID, TRUE);
235 if (path == NULL) {
236 return NULL;
237 }
238
239 pathStr = CFStringCreateWithCStringNoCopy(allocator,
240 path,
241 kCFStringEncodingASCII,
242 kCFAllocatorNull);
243
244 storeKey = CFStringCreateWithFormat(allocator,
245 NULL,
246 CFSTR("%@%@:%@"),
247 kSCDynamicStoreDomainPrefs,
248 keyStr,
249 pathStr);
250
251 CFRelease(pathStr);
252 CFAllocatorDeallocate(NULL, path);
253 return storeKey;
254 }
255
256
257 CFStringRef
258 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
259 CFStringRef prefsID,
260 SCPreferencesKeyType keyType)
261 {
262 return _SCPNotificationKey(allocator, prefsID, keyType);
263 }
264
265
266 __private_extern__ void
267 __SCPreferencesSetLimitSCNetworkConfiguration(SCPreferencesRef prefs,
268 Boolean limit_SCNetworkConfiguration)
269 {
270 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
271
272 if (prefs == NULL) {
273 return;
274 }
275 prefsPrivate->limit_SCNetworkConfiguration = limit_SCNetworkConfiguration;
276 }