2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009, 2011, 2014-2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * November 9, 2000 Allan Nathanson <ajn@apple.com>
34 #include "SCPreferencesInternal.h"
35 #include "SCNetworkConfigurationInternal.h"
40 #include <sys/errno.h>
41 #include <sys/param.h>
43 __private_extern__ CF_RETURNS_RETAINED CFDataRef
44 __SCPSignatureFromStatbuf(const struct stat
*statBuf
)
46 CFMutableDataRef signature
;
47 SCPSignatureDataRef sig
;
49 signature
= CFDataCreateMutable(NULL
, sizeof(SCPSignatureData
));
50 CFDataSetLength(signature
, sizeof(SCPSignatureData
));
52 /* ALIGN: CFDataGetBytePtr aligns to at least 8 bytes */
53 sig
= (SCPSignatureDataRef
)(void *)CFDataGetBytePtr(signature
);
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
;
64 __private_extern__
char *
65 __SCPreferencesPath(CFAllocatorRef allocator
,
69 CFStringRef path
= NULL
;
72 if (prefsID
== NULL
) {
73 /* default preference ID */
74 path
= CFStringCreateWithFormat(allocator
,
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
);
84 path
= CFStringCreateWithFormat(allocator
,
87 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
89 if (useNewPrefs
&& CFStringHasSuffix(path
, CFSTR(".xml"))) {
90 CFMutableStringRef newPath
;
92 newPath
= CFStringCreateMutableCopy(allocator
, 0, path
);
93 CFStringReplace(newPath
,
94 CFRangeMake(CFStringGetLength(newPath
)-4, 4),
102 * convert CFStringRef path to C-string path
104 pathStr
= _SC_cfstring_to_cstring(path
, NULL
, 0, kCFStringEncodingASCII
);
105 if (pathStr
== NULL
) {
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
);
124 __SCPreferencesGetLimitSCNetworkConfiguration(SCPreferencesRef prefs
)
126 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
131 return prefsPrivate
->limit_SCNetworkConfiguration
;
137 __SCPreferencesPrefsSize(SCPreferencesRef prefs
)
139 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
140 SCPSignatureDataRef sig
;
143 signature
= prefsPrivate
->signature
;
144 if (signature
== NULL
) {
148 sig
= (SCPSignatureDataRef
)(void *)CFDataGetBytePtr(signature
);
155 __SCPreferencesUsingDefaultPrefs(SCPreferencesRef prefs
)
158 Boolean isDefault
= FALSE
;
159 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
161 curPath
= prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
;
162 if (curPath
!= NULL
) {
165 defPath
= __SCPreferencesPath(NULL
,
167 (prefsPrivate
->newPath
== NULL
));
168 if (defPath
!= NULL
) {
169 if (strcmp(curPath
, defPath
) == 0) {
172 CFAllocatorDeallocate(NULL
, defPath
);
180 __SCPreferencesCreateNIPrefsFromPrefs(SCPreferencesRef prefs
)
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
);
189 newPath
= CFStringCreateMutable(NULL
, 0);
190 CFStringAppendFormat(newPath
, NULL
, CFSTR("%s"), prefsPath
);
192 CFStringFindAndReplace(newPath
,
193 PREFS_DEFAULT_CONFIG
,
194 NETWORK_INTERFACES_PREFS
,
195 CFRangeMake(0, CFStringGetLength(newPath
)),
196 kCFCompareBackwards
);
198 newURL
= CFURLCreateWithFileSystemPath(NULL
, newPath
, kCFURLPOSIXPathStyle
, FALSE
);
199 if (!CFURLResourceIsReachable(newURL
, NULL
)) {
200 ni_prefs
= __SCNetworkCreateDefaultNIPrefs(newPath
);
202 ni_prefs
= SCPreferencesCreate(NULL
, prefsPrivate
->name
, newPath
);
204 CFAllocatorDeallocate(NULL
, prefsPath
);
212 SCPreferencesGetSignature(SCPreferencesRef prefs
)
214 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
217 /* sorry, you must provide a session */
218 _SCErrorSet(kSCStatusNoPrefsSession
);
222 __SCPreferencesAccess(prefs
);
224 return prefsPrivate
->signature
;
228 __private_extern__ CF_RETURNS_RETAINED CFStringRef
229 _SCPNotificationKey(CFAllocatorRef allocator
,
236 CFStringRef storeKey
;
239 case kSCPreferencesKeyLock
:
240 keyStr
= CFSTR("lock");
242 case kSCPreferencesKeyCommit
:
243 keyStr
= CFSTR("commit");
245 case kSCPreferencesKeyApply
:
246 keyStr
= CFSTR("apply");
252 path
= __SCPreferencesPath(allocator
, prefsID
, TRUE
);
257 pathStr
= CFStringCreateWithCStringNoCopy(allocator
,
259 kCFStringEncodingASCII
,
262 storeKey
= CFStringCreateWithFormat(allocator
,
265 kSCDynamicStoreDomainPrefs
,
270 CFAllocatorDeallocate(NULL
, path
);
276 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator
,
278 SCPreferencesKeyType keyType
)
280 return _SCPNotificationKey(allocator
, prefsID
, keyType
);
284 __private_extern__
void
285 __SCPreferencesSetLimitSCNetworkConfiguration(SCPreferencesRef prefs
,
286 Boolean limit_SCNetworkConfiguration
)
288 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
293 prefsPrivate
->limit_SCNetworkConfiguration
= limit_SCNetworkConfiguration
;