]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
configd-395.10.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCP.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2005, 2007-2009 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 <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCPrivate.h>
36 #include "SCPreferencesInternal.h"
37
38 #include <fcntl.h>
39 #include <pwd.h>
40 #include <unistd.h>
41 #include <sys/errno.h>
42 #include <sys/param.h>
43
44 __private_extern__ CFDataRef
45 __SCPSignatureFromStatbuf(const struct stat *statBuf)
46 {
47 CFMutableDataRef signature;
48 SCPSignatureDataRef sig;
49
50 signature = CFDataCreateMutable(NULL, sizeof(SCPSignatureData));
51 CFDataSetLength(signature, sizeof(SCPSignatureData));
52 sig = (SCPSignatureDataRef)CFDataGetBytePtr(signature);
53 sig->st_dev = statBuf->st_dev;
54 sig->st_ino = statBuf->st_ino;
55 sig->tv_sec = statBuf->st_mtimespec.tv_sec;
56 sig->tv_nsec = statBuf->st_mtimespec.tv_nsec;
57 sig->st_size = statBuf->st_size;
58 return signature;
59 }
60
61
62 __private_extern__ char *
63 __SCPreferencesPath(CFAllocatorRef allocator,
64 CFStringRef prefsID,
65 Boolean useNewPrefs)
66 {
67 CFStringRef path = NULL;
68 char *pathStr;
69
70 if (prefsID == NULL) {
71 /* default preference ID */
72 path = CFStringCreateWithFormat(allocator,
73 NULL,
74 CFSTR("%@/%@"),
75 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
76 useNewPrefs ? PREFS_DEFAULT_CONFIG : PREFS_DEFAULT_CONFIG_OLD);
77 } else if (CFStringHasPrefix(prefsID, CFSTR("/"))) {
78 /* if absolute path */
79 path = CFStringCreateCopy(allocator, prefsID);
80 } else {
81 /* relative path */
82 path = CFStringCreateWithFormat(allocator,
83 NULL,
84 CFSTR("%@/%@"),
85 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
86 prefsID);
87 if (useNewPrefs && CFStringHasSuffix(path, CFSTR(".xml"))) {
88 CFMutableStringRef newPath;
89
90 newPath = CFStringCreateMutableCopy(allocator, 0, path);
91 CFStringReplace(newPath,
92 CFRangeMake(CFStringGetLength(newPath)-4, 4),
93 CFSTR(".plist"));
94 CFRelease(path);
95 path = newPath;
96 }
97 }
98
99 /*
100 * convert CFStringRef path to C-string path
101 */
102 pathStr = _SC_cfstring_to_cstring(path, NULL, 0, kCFStringEncodingASCII);
103 if (pathStr == NULL) {
104 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("could not convert path to C string"));
105 }
106
107 CFRelease(path);
108 return pathStr;
109 }
110
111
112 CFDataRef
113 SCPreferencesGetSignature(SCPreferencesRef prefs)
114 {
115 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
116
117 if (prefs == NULL) {
118 /* sorry, you must provide a session */
119 _SCErrorSet(kSCStatusNoPrefsSession);
120 return NULL;
121 }
122
123 __SCPreferencesAccess(prefs);
124
125 return prefsPrivate->signature;
126 }
127
128
129 __private_extern__ CFStringRef
130 _SCPNotificationKey(CFAllocatorRef allocator,
131 CFStringRef prefsID,
132 int keyType)
133 {
134 CFStringRef keyStr;
135 char *path;
136 CFStringRef pathStr;
137 CFStringRef storeKey;
138
139 switch (keyType) {
140 case kSCPreferencesKeyLock :
141 keyStr = CFSTR("lock");
142 break;
143 case kSCPreferencesKeyCommit :
144 keyStr = CFSTR("commit");
145 break;
146 case kSCPreferencesKeyApply :
147 keyStr = CFSTR("apply");
148 break;
149 default :
150 return NULL;
151 }
152
153 path = __SCPreferencesPath(allocator, prefsID, TRUE);
154 if (path == NULL) {
155 return NULL;
156 }
157
158 pathStr = CFStringCreateWithCStringNoCopy(allocator,
159 path,
160 kCFStringEncodingASCII,
161 kCFAllocatorNull);
162
163 storeKey = CFStringCreateWithFormat(allocator,
164 NULL,
165 CFSTR("%@%@:%@"),
166 kSCDynamicStoreDomainPrefs,
167 keyStr,
168 pathStr);
169
170 CFRelease(pathStr);
171 CFAllocatorDeallocate(NULL, path);
172 return storeKey;
173 }
174
175
176 CFStringRef
177 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
178 CFStringRef prefsID,
179 SCPreferencesKeyType keyType)
180 {
181 return _SCPNotificationKey(allocator, prefsID, keyType);
182 }