]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
configd-1109.101.1.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCP.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009, 2011, 2014-2021 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__
65 uint32_t
66 __SCPreferencesGetNetworkConfigurationFlags(SCPreferencesRef prefs)
67 {
68 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
69
70 return (prefs != NULL) ? prefsPrivate->nc_flags : 0;
71 }
72
73 __private_extern__
74 void
75 __SCPreferencesSetNetworkConfigurationFlags(SCPreferencesRef prefs, uint32_t nc_flags)
76 {
77 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
78
79 if (prefs != NULL) {
80 prefsPrivate->nc_flags = nc_flags;
81 }
82
83 return;
84 }
85
86
87 __private_extern__ char *
88 __SCPreferencesPath(CFAllocatorRef allocator,
89 CFStringRef prefsID,
90 Boolean useNewPrefs)
91 {
92 CFStringRef path = NULL;
93 char *pathStr;
94
95 if (prefsID == NULL) {
96 /* default preference ID */
97 path = CFStringCreateWithFormat(allocator,
98 NULL,
99 CFSTR("%@/%@"),
100 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
101 useNewPrefs ? PREFS_DEFAULT_CONFIG : PREFS_DEFAULT_CONFIG_OLD);
102 } else if (CFStringHasPrefix(prefsID, CFSTR("/"))) {
103 /* if absolute path */
104 path = CFStringCreateCopy(allocator, prefsID);
105 } else {
106 /* relative path */
107 path = CFStringCreateWithFormat(allocator,
108 NULL,
109 CFSTR("%@/%@"),
110 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
111 prefsID);
112 if (useNewPrefs && CFStringHasSuffix(path, CFSTR(".xml"))) {
113 CFMutableStringRef newPath;
114
115 newPath = CFStringCreateMutableCopy(allocator, 0, path);
116 CFStringReplace(newPath,
117 CFRangeMake(CFStringGetLength(newPath)-4, 4),
118 CFSTR(".plist"));
119 CFRelease(path);
120 path = newPath;
121 }
122 }
123
124 /*
125 * convert CFStringRef path to C-string path
126 */
127 pathStr = _SC_cfstring_to_cstring(path, NULL, 0, kCFStringEncodingASCII);
128 if (pathStr == NULL) {
129 CFIndex pathLen;
130
131 pathLen = CFStringGetMaximumSizeOfFileSystemRepresentation(path);
132 pathStr = CFAllocatorAllocate(NULL, pathLen, 0);
133 if (!CFStringGetFileSystemRepresentation(path, pathStr, pathLen)) {
134 SC_log(LOG_INFO, "could not convert path to C string");
135 CFAllocatorDeallocate(NULL, pathStr);
136 pathStr = NULL;
137 }
138 }
139
140 CFRelease(path);
141 return pathStr;
142 }
143
144
145 __private_extern__
146 Boolean
147 __SCPreferencesIsEmpty(SCPreferencesRef prefs)
148 {
149 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
150
151 assert(prefs != NULL);
152 __SCPreferencesAccess(prefs);
153
154 if ((prefsPrivate->prefs == NULL) ||
155 (CFDictionaryGetCount(prefsPrivate->prefs) == 0)) {
156 return TRUE;
157 }
158
159 return FALSE;
160 }
161
162
163 __private_extern__
164 off_t
165 __SCPreferencesPrefsSize(SCPreferencesRef prefs)
166 {
167 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
168 SCPSignatureDataRef sig;
169 CFDataRef signature;
170
171 signature = prefsPrivate->signature;
172 if (signature == NULL) {
173 return 0;
174 }
175
176 sig = (SCPSignatureDataRef)(void *)CFDataGetBytePtr(signature);
177 return sig->st_size;
178 }
179
180
181 __private_extern__
182 Boolean
183 __SCPreferencesUsingDefaultPrefs(SCPreferencesRef prefs)
184 {
185 char *curPath;
186 Boolean isDefault = FALSE;
187 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
188
189 if (prefs == NULL) {
190 // if no prefs, assume that we are using the "default" prefs
191 return TRUE;
192 }
193
194 curPath = prefsPrivate->newPath ? prefsPrivate->newPath : prefsPrivate->path;
195 if (curPath != NULL) {
196 char* defPath;
197
198 defPath = __SCPreferencesPath(NULL,
199 NULL,
200 (prefsPrivate->newPath == NULL));
201 if (defPath != NULL) {
202 if (strcmp(curPath, defPath) == 0) {
203 isDefault = TRUE;
204 }
205 CFAllocatorDeallocate(NULL, defPath);
206 }
207 }
208 return isDefault;
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