]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
configd-1061.101.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 off_t
147 __SCPreferencesPrefsSize(SCPreferencesRef prefs)
148 {
149 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
150 SCPSignatureDataRef sig;
151 CFDataRef signature;
152
153 signature = prefsPrivate->signature;
154 if (signature == NULL) {
155 return 0;
156 }
157
158 sig = (SCPSignatureDataRef)(void *)CFDataGetBytePtr(signature);
159 return sig->st_size;
160 }
161
162
163 __private_extern__
164 Boolean
165 __SCPreferencesUsingDefaultPrefs(SCPreferencesRef prefs)
166 {
167 char *curPath;
168 Boolean isDefault = FALSE;
169 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
170
171 curPath = prefsPrivate->newPath ? prefsPrivate->newPath : prefsPrivate->path;
172 if (curPath != NULL) {
173 char* defPath;
174
175 defPath = __SCPreferencesPath(NULL,
176 NULL,
177 (prefsPrivate->newPath == NULL));
178 if (defPath != NULL) {
179 if (strcmp(curPath, defPath) == 0) {
180 isDefault = TRUE;
181 }
182 CFAllocatorDeallocate(NULL, defPath);
183 }
184 }
185 return isDefault;
186 }
187
188 __private_extern__
189 SCPreferencesRef
190 __SCPreferencesCreateNIPrefsFromPrefs(SCPreferencesRef prefs)
191 {
192 SCPreferencesRef ni_prefs;
193
194 // open [companion] NetworkInterfaces.plist
195 ni_prefs = SCPreferencesCreateCompanion(prefs, INTERFACES_DEFAULT_CONFIG);
196
197 // if needed, populate
198 __SCNetworkPopulateDefaultNIPrefs(ni_prefs);
199
200 return ni_prefs;
201 }
202
203 CFDataRef
204 SCPreferencesGetSignature(SCPreferencesRef prefs)
205 {
206 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
207
208 if (prefs == NULL) {
209 /* sorry, you must provide a session */
210 _SCErrorSet(kSCStatusNoPrefsSession);
211 return NULL;
212 }
213
214 __SCPreferencesAccess(prefs);
215
216 return prefsPrivate->signature;
217 }
218
219
220 __private_extern__ CF_RETURNS_RETAINED CFStringRef
221 _SCPNotificationKey(CFAllocatorRef allocator,
222 CFStringRef prefsID,
223 int keyType)
224 {
225 CFStringRef keyStr;
226 char *path;
227 CFStringRef pathStr;
228 CFStringRef storeKey;
229
230 switch (keyType) {
231 case kSCPreferencesKeyLock :
232 keyStr = CFSTR("lock");
233 break;
234 case kSCPreferencesKeyCommit :
235 keyStr = CFSTR("commit");
236 break;
237 case kSCPreferencesKeyApply :
238 keyStr = CFSTR("apply");
239 break;
240 default :
241 return NULL;
242 }
243
244 path = __SCPreferencesPath(allocator, prefsID, TRUE);
245 if (path == NULL) {
246 return NULL;
247 }
248
249 pathStr = CFStringCreateWithCStringNoCopy(allocator,
250 path,
251 kCFStringEncodingASCII,
252 kCFAllocatorNull);
253
254 storeKey = CFStringCreateWithFormat(allocator,
255 NULL,
256 CFSTR("%@%@:%@"),
257 kSCDynamicStoreDomainPrefs,
258 keyStr,
259 pathStr);
260
261 CFRelease(pathStr);
262 CFAllocatorDeallocate(NULL, path);
263 return storeKey;
264 }
265
266
267 CFStringRef
268 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
269 CFStringRef prefsID,
270 SCPreferencesKeyType keyType)
271 {
272 return _SCPNotificationKey(allocator, prefsID, keyType);
273 }
274