]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
e81b75f7d95774cc7a2563606377fb4ce6d3f797
[apple/configd.git] / SystemConfiguration.fproj / SCP.c
1 /*
2 * Copyright(c) 2000-2005 Apple Computer, 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->st_mtimespec = statBuf->st_mtimespec;
56 sig->st_size = statBuf->st_size;
57 return signature;
58 }
59
60
61 __private_extern__ char *
62 __SCPreferencesPath(CFAllocatorRef allocator,
63 CFStringRef prefsID,
64 Boolean perUser,
65 CFStringRef user,
66 Boolean useNewPrefs)
67 {
68 CFStringRef path = NULL;
69 char *pathStr;
70
71 if (perUser) {
72 if (prefsID == NULL) {
73 /* no user prefsID specified */
74 return NULL;
75 } else if (CFStringHasPrefix(prefsID, CFSTR("/"))) {
76 /* if absolute path */
77 path = CFStringCreateCopy(allocator, prefsID);
78 } else {
79 /*
80 * relative (to the user's preferences) path
81 */
82 char login[MAXLOGNAME+1];
83 struct passwd *pwd;
84
85 bzero(&login, sizeof(login));
86 if (user == NULL) {
87 CFStringRef u;
88
89 /* get current console user */
90 u = SCDynamicStoreCopyConsoleUser(NULL, NULL, NULL);
91 if (!u) {
92 /* if could not get console user */
93 return NULL;
94 }
95
96 (void)_SC_cfstring_to_cstring(u, login, sizeof(login), kCFStringEncodingASCII);
97 CFRelease(u);
98 } else {
99 /* use specified user */
100 (void)_SC_cfstring_to_cstring(user, login, sizeof(login), kCFStringEncodingASCII);
101 }
102
103 /* get password entry for user */
104 pwd = getpwnam(login);
105 if (pwd == NULL) {
106 /* if no home directory */
107 return NULL;
108 }
109
110 /* create prefs ID */
111 path = CFStringCreateWithFormat(allocator,
112 NULL,
113 CFSTR("%s/%@/%@"),
114 pwd->pw_dir,
115 PREFS_DEFAULT_USER_DIR,
116 prefsID);
117 }
118 } else {
119 if (prefsID == NULL) {
120 /* default preference ID */
121 path = CFStringCreateWithFormat(allocator,
122 NULL,
123 CFSTR("%@/%@"),
124 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
125 useNewPrefs ? PREFS_DEFAULT_CONFIG : PREFS_DEFAULT_CONFIG_OLD);
126 } else if (CFStringHasPrefix(prefsID, CFSTR("/"))) {
127 /* if absolute path */
128 path = CFStringCreateCopy(allocator, prefsID);
129 } else {
130 /* relative path */
131 path = CFStringCreateWithFormat(allocator,
132 NULL,
133 CFSTR("%@/%@"),
134 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
135 prefsID);
136 if (useNewPrefs && CFStringHasSuffix(path, CFSTR(".xml"))) {
137 CFMutableStringRef newPath;
138
139 newPath = CFStringCreateMutableCopy(allocator, 0, path);
140 CFStringReplace(newPath,
141 CFRangeMake(CFStringGetLength(newPath)-4, 4),
142 CFSTR(".plist"));
143 CFRelease(path);
144 path = newPath;
145 }
146 }
147 }
148
149 /*
150 * convert CFStringRef path to C-string path
151 */
152 pathStr = _SC_cfstring_to_cstring(path, NULL, 0, kCFStringEncodingASCII);
153 if (pathStr == NULL) {
154 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("could not convert path to C string"));
155 }
156
157 CFRelease(path);
158 return pathStr;
159 }
160
161
162 CFDataRef
163 SCPreferencesGetSignature(SCPreferencesRef prefs)
164 {
165 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
166
167 if (prefs == NULL) {
168 /* sorry, you must provide a session */
169 _SCErrorSet(kSCStatusNoPrefsSession);
170 return NULL;
171 }
172
173 __SCPreferencesAccess(prefs);
174
175 return prefsPrivate->signature;
176 }
177
178
179 __private_extern__ CFStringRef
180 _SCPNotificationKey(CFAllocatorRef allocator,
181 CFStringRef prefsID,
182 Boolean perUser,
183 CFStringRef user,
184 int keyType)
185 {
186 CFStringRef keyStr;
187 char *path;
188 CFStringRef pathStr;
189 CFStringRef storeKey;
190
191 switch (keyType) {
192 case kSCPreferencesKeyLock :
193 keyStr = CFSTR("lock");
194 break;
195 case kSCPreferencesKeyCommit :
196 keyStr = CFSTR("commit");
197 break;
198 case kSCPreferencesKeyApply :
199 keyStr = CFSTR("apply");
200 break;
201 default :
202 return NULL;
203 }
204
205 path = __SCPreferencesPath(allocator, prefsID, perUser, user, TRUE);
206 if (path == NULL) {
207 return NULL;
208 }
209
210 pathStr = CFStringCreateWithCStringNoCopy(allocator,
211 path,
212 kCFStringEncodingASCII,
213 kCFAllocatorNull);
214
215 storeKey = CFStringCreateWithFormat(allocator,
216 NULL,
217 CFSTR("%@%@:%@"),
218 kSCDynamicStoreDomainPrefs,
219 keyStr,
220 pathStr);
221
222 CFRelease(pathStr);
223 CFAllocatorDeallocate(NULL, path);
224 return storeKey;
225 }
226
227
228 CFStringRef
229 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
230 CFStringRef prefsID,
231 SCPreferencesKeyType keyType)
232 {
233 return _SCPNotificationKey(allocator, prefsID, FALSE, NULL, keyType);
234 }
235
236
237 CFStringRef
238 SCDynamicStoreKeyCreateUserPreferences(CFAllocatorRef allocator,
239 CFStringRef prefsID,
240 CFStringRef user,
241 SCPreferencesKeyType keyType)
242 {
243 return _SCPNotificationKey(allocator, prefsID, TRUE, user, keyType);
244 }