2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009, 2011, 2014-2017 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 __SCPreferencesUsingDefaultPrefs(SCPreferencesRef prefs
)
140 Boolean isDefault
= FALSE
;
141 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
143 curPath
= prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
;
144 if (curPath
!= NULL
) {
147 defPath
= __SCPreferencesPath(NULL
,
149 (prefsPrivate
->newPath
== NULL
));
150 if (defPath
!= NULL
) {
151 if (strcmp(curPath
, defPath
) == 0) {
154 CFAllocatorDeallocate(NULL
, defPath
);
162 __SCPreferencesCreateNIPrefsFromPrefs(SCPreferencesRef prefs
)
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
);
171 newPath
= CFStringCreateMutable(NULL
, 0);
172 CFStringAppendFormat(newPath
, NULL
, CFSTR("%s"), prefsPath
);
174 CFStringFindAndReplace(newPath
,
175 PREFS_DEFAULT_CONFIG
,
176 NETWORK_INTERFACES_PREFS
,
177 CFRangeMake(0, CFStringGetLength(newPath
)),
178 kCFCompareBackwards
);
180 newURL
= CFURLCreateWithFileSystemPath(NULL
, newPath
, kCFURLPOSIXPathStyle
, FALSE
);
181 if (!CFURLResourceIsReachable(newURL
, NULL
)) {
182 ni_prefs
= __SCNetworkCreateDefaultNIPrefs(newPath
);
184 ni_prefs
= SCPreferencesCreate(NULL
, prefsPrivate
->name
, newPath
);
186 CFAllocatorDeallocate(NULL
, prefsPath
);
194 SCPreferencesGetSignature(SCPreferencesRef prefs
)
196 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
199 /* sorry, you must provide a session */
200 _SCErrorSet(kSCStatusNoPrefsSession
);
204 __SCPreferencesAccess(prefs
);
206 return prefsPrivate
->signature
;
210 __private_extern__ CF_RETURNS_RETAINED CFStringRef
211 _SCPNotificationKey(CFAllocatorRef allocator
,
218 CFStringRef storeKey
;
221 case kSCPreferencesKeyLock
:
222 keyStr
= CFSTR("lock");
224 case kSCPreferencesKeyCommit
:
225 keyStr
= CFSTR("commit");
227 case kSCPreferencesKeyApply
:
228 keyStr
= CFSTR("apply");
234 path
= __SCPreferencesPath(allocator
, prefsID
, TRUE
);
239 pathStr
= CFStringCreateWithCStringNoCopy(allocator
,
241 kCFStringEncodingASCII
,
244 storeKey
= CFStringCreateWithFormat(allocator
,
247 kSCDynamicStoreDomainPrefs
,
252 CFAllocatorDeallocate(NULL
, path
);
258 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator
,
260 SCPreferencesKeyType keyType
)
262 return _SCPNotificationKey(allocator
, prefsID
, keyType
);
266 __private_extern__
void
267 __SCPreferencesSetLimitSCNetworkConfiguration(SCPreferencesRef prefs
,
268 Boolean limit_SCNetworkConfiguration
)
270 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
275 prefsPrivate
->limit_SCNetworkConfiguration
= limit_SCNetworkConfiguration
;