2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009, 2011, 2014-2020 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
;
66 __SCPreferencesGetNetworkConfigurationFlags(SCPreferencesRef prefs
)
68 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
70 return (prefs
!= NULL
) ? prefsPrivate
->nc_flags
: 0;
75 __SCPreferencesSetNetworkConfigurationFlags(SCPreferencesRef prefs
, uint32_t nc_flags
)
77 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
80 prefsPrivate
->nc_flags
= nc_flags
;
87 __private_extern__
char *
88 __SCPreferencesPath(CFAllocatorRef allocator
,
92 CFStringRef path
= NULL
;
95 if (prefsID
== NULL
) {
96 /* default preference ID */
97 path
= CFStringCreateWithFormat(allocator
,
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
);
107 path
= CFStringCreateWithFormat(allocator
,
110 useNewPrefs
? PREFS_DEFAULT_DIR
: PREFS_DEFAULT_DIR_OLD
,
112 if (useNewPrefs
&& CFStringHasSuffix(path
, CFSTR(".xml"))) {
113 CFMutableStringRef newPath
;
115 newPath
= CFStringCreateMutableCopy(allocator
, 0, path
);
116 CFStringReplace(newPath
,
117 CFRangeMake(CFStringGetLength(newPath
)-4, 4),
125 * convert CFStringRef path to C-string path
127 pathStr
= _SC_cfstring_to_cstring(path
, NULL
, 0, kCFStringEncodingASCII
);
128 if (pathStr
== NULL
) {
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
);
147 __SCPreferencesPrefsSize(SCPreferencesRef prefs
)
149 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
150 SCPSignatureDataRef sig
;
153 signature
= prefsPrivate
->signature
;
154 if (signature
== NULL
) {
158 sig
= (SCPSignatureDataRef
)(void *)CFDataGetBytePtr(signature
);
165 __SCPreferencesUsingDefaultPrefs(SCPreferencesRef prefs
)
168 Boolean isDefault
= FALSE
;
169 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
171 curPath
= prefsPrivate
->newPath
? prefsPrivate
->newPath
: prefsPrivate
->path
;
172 if (curPath
!= NULL
) {
175 defPath
= __SCPreferencesPath(NULL
,
177 (prefsPrivate
->newPath
== NULL
));
178 if (defPath
!= NULL
) {
179 if (strcmp(curPath
, defPath
) == 0) {
182 CFAllocatorDeallocate(NULL
, defPath
);
190 __SCPreferencesCreateNIPrefsFromPrefs(SCPreferencesRef prefs
)
192 SCPreferencesRef ni_prefs
;
194 // open [companion] NetworkInterfaces.plist
195 ni_prefs
= SCPreferencesCreateCompanion(prefs
, INTERFACES_DEFAULT_CONFIG
);
197 // if needed, populate
198 __SCNetworkPopulateDefaultNIPrefs(ni_prefs
);
204 SCPreferencesGetSignature(SCPreferencesRef prefs
)
206 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
209 /* sorry, you must provide a session */
210 _SCErrorSet(kSCStatusNoPrefsSession
);
214 __SCPreferencesAccess(prefs
);
216 return prefsPrivate
->signature
;
220 __private_extern__ CF_RETURNS_RETAINED CFStringRef
221 _SCPNotificationKey(CFAllocatorRef allocator
,
228 CFStringRef storeKey
;
231 case kSCPreferencesKeyLock
:
232 keyStr
= CFSTR("lock");
234 case kSCPreferencesKeyCommit
:
235 keyStr
= CFSTR("commit");
237 case kSCPreferencesKeyApply
:
238 keyStr
= CFSTR("apply");
244 path
= __SCPreferencesPath(allocator
, prefsID
, TRUE
);
249 pathStr
= CFStringCreateWithCStringNoCopy(allocator
,
251 kCFStringEncodingASCII
,
254 storeKey
= CFStringCreateWithFormat(allocator
,
257 kSCDynamicStoreDomainPrefs
,
262 CFAllocatorDeallocate(NULL
, path
);
268 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator
,
270 SCPreferencesKeyType keyType
)
272 return _SCPNotificationKey(allocator
, prefsID
, keyType
);