2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009, 2011, 2014 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 <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCPrivate.h>
36 #include "SCPreferencesInternal.h"
37 #include "SCNetworkConfigurationInternal.h"
42 #include <sys/errno.h>
43 #include <sys/param.h>
45 __private_extern__ CF_RETURNS_RETAINED CFDataRef
46 __SCPSignatureFromStatbuf(const struct stat
*statBuf
)
48 CFMutableDataRef signature
;
49 SCPSignatureDataRef sig
;
51 signature
= CFDataCreateMutable(NULL
, sizeof(SCPSignatureData
));
52 CFDataSetLength(signature
, sizeof(SCPSignatureData
));
54 /* ALIGN: CFDataGetBytePtr aligns to at least 8 bytes */
55 sig
= (SCPSignatureDataRef
)(void *)CFDataGetBytePtr(signature
);
57 sig
->st_dev
= statBuf
->st_dev
;
58 sig
->st_ino
= statBuf
->st_ino
;
59 sig
->tv_sec
= statBuf
->st_mtimespec
.tv_sec
;
60 sig
->tv_nsec
= statBuf
->st_mtimespec
.tv_nsec
;
61 sig
->st_size
= statBuf
->st_size
;
66 __private_extern__
char *
67 __SCPreferencesPath(CFAllocatorRef allocator
,
71 CFStringRef path
= NULL
;
74 if (prefsID
== NULL
) {
75 /* default preference ID */
76 path
= CFStringCreateWithFormat(allocator
,
79 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
80 useNewPrefs
? PREFS_DEFAULT_CONFIG
: PREFS_DEFAULT_CONFIG_OLD
);
81 } else if (CFStringHasPrefix(prefsID
, CFSTR("/"))) {
82 /* if absolute path */
83 path
= CFStringCreateCopy(allocator
, prefsID
);
86 path
= CFStringCreateWithFormat(allocator
,
89 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
91 if (useNewPrefs
&& CFStringHasSuffix(path
, CFSTR(".xml"))) {
92 CFMutableStringRef newPath
;
94 newPath
= CFStringCreateMutableCopy(allocator
, 0, path
);
95 CFStringReplace(newPath
,
96 CFRangeMake(CFStringGetLength(newPath
)-4, 4),
104 * convert CFStringRef path to C-string path
106 pathStr
= _SC_cfstring_to_cstring(path
, NULL
, 0, kCFStringEncodingASCII
);
107 if (pathStr
== NULL
) {
110 pathLen
= CFStringGetMaximumSizeOfFileSystemRepresentation(path
);
111 pathStr
= CFAllocatorAllocate(NULL
, pathLen
, 0);
112 if (CFStringGetFileSystemRepresentation(path
, pathStr
, pathLen
) == FALSE
) {
113 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("could not convert path to C string"));
114 CFAllocatorDeallocate(NULL
, pathStr
);
126 __SCPreferencesGetLimitSCNetworkConfiguration(SCPreferencesRef prefs
)
128 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
133 return prefsPrivate
->limit_SCNetworkConfiguration
;
139 __SCPreferencesUsingDefaultPrefs(SCPreferencesRef prefs
)
142 Boolean isDefault
= FALSE
;
143 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
145 curPath
= prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
;
146 if (curPath
!= NULL
) {
149 defPath
= __SCPreferencesPath(NULL
,
151 (prefsPrivate
->newPath
== NULL
));
152 if (defPath
!= NULL
) {
153 if (strcmp(curPath
, defPath
) == 0) {
156 CFAllocatorDeallocate(NULL
, defPath
);
164 __SCPreferencesCreateNIPrefsFromPrefs(SCPreferencesRef prefs
)
166 CFMutableStringRef newPath
= NULL
;
167 CFURLRef newURL
= NULL
;
168 SCPreferencesRef ni_prefs
= NULL
;
169 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
170 char * prefsPath
= __SCPreferencesPath(NULL
, prefsPrivate
->prefsID
, FALSE
);
173 newPath
= CFStringCreateMutable(NULL
, 0);
174 CFStringAppendFormat(newPath
, NULL
, CFSTR("%s"), prefsPath
);
176 CFStringFindAndReplace(newPath
, PREFS_DEFAULT_CONFIG
,
177 NETWORK_INTERFACES_PREFS
,
178 CFRangeMake(0, CFStringGetLength(newPath
)),
179 kCFCompareBackwards
);
181 newURL
= CFURLCreateWithFileSystemPath(NULL
, newPath
, kCFURLPOSIXPathStyle
, FALSE
);
182 if (CFURLResourceIsReachable(newURL
, NULL
) == FALSE
) {
183 ni_prefs
= __SCNetworkCreateDefaultNIPrefs(newPath
);
186 ni_prefs
= SCPreferencesCreate(NULL
, prefsPrivate
->name
, newPath
);
188 CFAllocatorDeallocate(NULL
, prefsPath
);
196 SCPreferencesGetSignature(SCPreferencesRef prefs
)
198 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
201 /* sorry, you must provide a session */
202 _SCErrorSet(kSCStatusNoPrefsSession
);
206 __SCPreferencesAccess(prefs
);
208 return prefsPrivate
->signature
;
212 __private_extern__ CF_RETURNS_RETAINED CFStringRef
213 _SCPNotificationKey(CFAllocatorRef allocator
,
220 CFStringRef storeKey
;
223 case kSCPreferencesKeyLock
:
224 keyStr
= CFSTR("lock");
226 case kSCPreferencesKeyCommit
:
227 keyStr
= CFSTR("commit");
229 case kSCPreferencesKeyApply
:
230 keyStr
= CFSTR("apply");
236 path
= __SCPreferencesPath(allocator
, prefsID
, TRUE
);
241 pathStr
= CFStringCreateWithCStringNoCopy(allocator
,
243 kCFStringEncodingASCII
,
246 storeKey
= CFStringCreateWithFormat(allocator
,
249 kSCDynamicStoreDomainPrefs
,
254 CFAllocatorDeallocate(NULL
, path
);
260 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator
,
262 SCPreferencesKeyType keyType
)
264 return _SCPNotificationKey(allocator
, prefsID
, keyType
);
268 __private_extern__
void
269 __SCPreferencesSetLimitSCNetworkConfiguration(SCPreferencesRef prefs
,
270 Boolean limit_SCNetworkConfiguration
)
272 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
277 prefsPrivate
->limit_SCNetworkConfiguration
= limit_SCNetworkConfiguration
;