]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
configd-1061.141.1.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCP.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009, 2011, 2014-2020 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 curPath = prefsPrivate->newPath ? prefsPrivate->newPath : prefsPrivate->path;
190 if (curPath != NULL) {
191 char* defPath;
192
193 defPath = __SCPreferencesPath(NULL,
194 NULL,
195 (prefsPrivate->newPath == NULL));
196 if (defPath != NULL) {
197 if (strcmp(curPath, defPath) == 0) {
198 isDefault = TRUE;
199 }
200 CFAllocatorDeallocate(NULL, defPath);
201 }
202 }
203 return isDefault;
204 }
205
206 CFDataRef
207 SCPreferencesGetSignature(SCPreferencesRef prefs)
208 {
209 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
210
211 if (prefs == NULL) {
212 /* sorry, you must provide a session */
213 _SCErrorSet(kSCStatusNoPrefsSession);
214 return NULL;
215 }
216
217 __SCPreferencesAccess(prefs);
218
219 return prefsPrivate->signature;
220 }
221
222
223 __private_extern__ CF_RETURNS_RETAINED CFStringRef
224 _SCPNotificationKey(CFAllocatorRef allocator,
225 CFStringRef prefsID,
226 int keyType)
227 {
228 CFStringRef keyStr;
229 char *path;
230 CFStringRef pathStr;
231 CFStringRef storeKey;
232
233 switch (keyType) {
234 case kSCPreferencesKeyLock :
235 keyStr = CFSTR("lock");
236 break;
237 case kSCPreferencesKeyCommit :
238 keyStr = CFSTR("commit");
239 break;
240 case kSCPreferencesKeyApply :
241 keyStr = CFSTR("apply");
242 break;
243 default :
244 return NULL;
245 }
246
247 path = __SCPreferencesPath(allocator, prefsID, TRUE);
248 if (path == NULL) {
249 return NULL;
250 }
251
252 pathStr = CFStringCreateWithCStringNoCopy(allocator,
253 path,
254 kCFStringEncodingASCII,
255 kCFAllocatorNull);
256
257 storeKey = CFStringCreateWithFormat(allocator,
258 NULL,
259 CFSTR("%@%@:%@"),
260 kSCDynamicStoreDomainPrefs,
261 keyStr,
262 pathStr);
263
264 CFRelease(pathStr);
265 CFAllocatorDeallocate(NULL, path);
266 return storeKey;
267 }
268
269
270 CFStringRef
271 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
272 CFStringRef prefsID,
273 SCPreferencesKeyType keyType)
274 {
275 return _SCPNotificationKey(allocator, prefsID, keyType);
276 }
277