]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
configd-84.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCP.c
1 /*
2 * Copyright(c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * November 9, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include <SystemConfiguration/SystemConfiguration.h>
37 #include <SystemConfiguration/SCPrivate.h>
38 #include "SCPreferencesInternal.h"
39
40 #include <fcntl.h>
41 #include <pwd.h>
42 #include <unistd.h>
43 #include <sys/errno.h>
44 #include <sys/param.h>
45
46 __private_extern__ CFDataRef
47 __SCPSignatureFromStatbuf(const struct stat *statBuf)
48 {
49 CFMutableDataRef signature;
50 SCPSignatureDataRef sig;
51
52 signature = CFDataCreateMutable(NULL, sizeof(SCPSignatureData));
53 CFDataSetLength(signature, sizeof(SCPSignatureData));
54 sig = (SCPSignatureDataRef)CFDataGetBytePtr(signature);
55 sig->st_dev = statBuf->st_dev;
56 sig->st_ino = statBuf->st_ino;
57 sig->st_mtimespec = statBuf->st_mtimespec;
58 sig->st_size = statBuf->st_size;
59 return signature;
60 }
61
62
63 __private_extern__ char *
64 __SCPreferencesPath(CFAllocatorRef allocator,
65 CFStringRef prefsID,
66 Boolean perUser,
67 CFStringRef user,
68 Boolean useNewPrefs)
69 {
70 CFStringRef path = NULL;
71 char *pathStr;
72
73 if (perUser) {
74 if (prefsID == NULL) {
75 /* no user prefsID specified */
76 return NULL;
77 } else if (CFStringHasPrefix(prefsID, CFSTR("/"))) {
78 /* if absolute path */
79 path = CFStringCreateCopy(allocator, prefsID);
80 } else {
81 /*
82 * relative (to the user's preferences) path
83 */
84 char login[MAXLOGNAME+1];
85 struct passwd *pwd;
86
87 bzero(&login, sizeof(login));
88 if (user == NULL) {
89 CFStringRef u;
90
91 /* get current console user */
92 u = SCDynamicStoreCopyConsoleUser(NULL, NULL, NULL);
93 if (!u) {
94 /* if could not get console user */
95 return NULL;
96 }
97
98 (void)_SC_cfstring_to_cstring(u, login, sizeof(login), kCFStringEncodingASCII);
99 CFRelease(u);
100 } else {
101 /* use specified user */
102 (void)_SC_cfstring_to_cstring(user, login, sizeof(login), kCFStringEncodingASCII);
103 }
104
105 /* get password entry for user */
106 pwd = getpwnam(login);
107 if (pwd == NULL) {
108 /* if no home directory */
109 return NULL;
110 }
111
112 /* create prefs ID */
113 path = CFStringCreateWithFormat(allocator,
114 NULL,
115 CFSTR("%s/%@/%@"),
116 pwd->pw_dir,
117 PREFS_DEFAULT_USER_DIR,
118 prefsID);
119 }
120 } else {
121 if (prefsID == NULL) {
122 /* default preference ID */
123 path = CFStringCreateWithFormat(allocator,
124 NULL,
125 CFSTR("%@/%@"),
126 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
127 useNewPrefs ? PREFS_DEFAULT_CONFIG : PREFS_DEFAULT_CONFIG_OLD);
128 } else if (CFStringHasPrefix(prefsID, CFSTR("/"))) {
129 /* if absolute path */
130 path = CFStringCreateCopy(allocator, prefsID);
131 } else {
132 /* relative path */
133 path = CFStringCreateWithFormat(allocator,
134 NULL,
135 CFSTR("%@/%@"),
136 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
137 prefsID);
138 if (useNewPrefs && CFStringHasSuffix(path, CFSTR(".xml"))) {
139 CFMutableStringRef newPath;
140
141 newPath = CFStringCreateMutableCopy(allocator, 0, path);
142 CFStringReplace(newPath,
143 CFRangeMake(CFStringGetLength(newPath)-4, 4),
144 CFSTR(".plist"));
145 CFRelease(path);
146 path = newPath;
147 }
148 }
149 }
150
151 /*
152 * convert CFStringRef path to C-string path
153 */
154 pathStr = _SC_cfstring_to_cstring(path, NULL, 0, kCFStringEncodingASCII);
155 if (pathStr == NULL) {
156 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("could not convert path to C string"));
157 }
158
159 CFRelease(path);
160 return pathStr;
161 }
162
163
164 CFDataRef
165 SCPreferencesGetSignature(SCPreferencesRef session)
166 {
167 SCPreferencesPrivateRef sessionPrivate = (SCPreferencesPrivateRef)session;
168
169 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCPreferencesGetSignature:"));
170
171 sessionPrivate->accessed = TRUE;
172 return sessionPrivate->signature;
173 }
174
175
176 __private_extern__ CFStringRef
177 _SCPNotificationKey(CFAllocatorRef allocator,
178 CFStringRef prefsID,
179 Boolean perUser,
180 CFStringRef user,
181 int keyType)
182 {
183 CFStringRef key = NULL;
184 char *pathStr;
185 char *typeStr;
186
187 pathStr = __SCPreferencesPath(allocator, prefsID, perUser, user, TRUE);
188 if (pathStr == NULL) {
189 return NULL;
190 }
191
192 /* create notification key */
193 switch (keyType) {
194 case kSCPreferencesKeyLock :
195 typeStr = "lock";
196 break;
197 case kSCPreferencesKeyCommit :
198 typeStr = "commit";
199 break;
200 case kSCPreferencesKeyApply :
201 typeStr = "apply";
202 break;
203 default :
204 typeStr = "?";
205 }
206
207 key = CFStringCreateWithFormat(allocator,
208 NULL,
209 CFSTR("%@%s:%s"),
210 kSCDynamicStoreDomainPrefs,
211 typeStr,
212 pathStr);
213
214 CFAllocatorDeallocate(NULL, pathStr);
215 return key;
216 }
217
218
219 CFStringRef
220 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
221 CFStringRef prefsID,
222 SCPreferencesKeyType keyType)
223 {
224 return _SCPNotificationKey(allocator, prefsID, FALSE, NULL, keyType);
225 }
226
227
228 CFStringRef
229 SCDynamicStoreKeyCreateUserPreferences(CFAllocatorRef allocator,
230 CFStringRef prefsID,
231 CFStringRef user,
232 SCPreferencesKeyType keyType)
233 {
234 return _SCPNotificationKey(allocator, prefsID, TRUE, user, keyType);
235 }