]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
0d6474354c639a9075fc3d36f94990f040137488
[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 * 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 session)
164 {
165 SCPreferencesPrivateRef sessionPrivate = (SCPreferencesPrivateRef)session;
166
167 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCPreferencesGetSignature:"));
168
169 sessionPrivate->accessed = TRUE;
170 return sessionPrivate->signature;
171 }
172
173
174 __private_extern__ CFStringRef
175 _SCPNotificationKey(CFAllocatorRef allocator,
176 CFStringRef prefsID,
177 Boolean perUser,
178 CFStringRef user,
179 int keyType)
180 {
181 CFStringRef key = NULL;
182 char *pathStr;
183 char *typeStr;
184
185 pathStr = __SCPreferencesPath(allocator, prefsID, perUser, user, TRUE);
186 if (pathStr == NULL) {
187 return NULL;
188 }
189
190 /* create notification key */
191 switch (keyType) {
192 case kSCPreferencesKeyLock :
193 typeStr = "lock";
194 break;
195 case kSCPreferencesKeyCommit :
196 typeStr = "commit";
197 break;
198 case kSCPreferencesKeyApply :
199 typeStr = "apply";
200 break;
201 default :
202 typeStr = "?";
203 }
204
205 key = CFStringCreateWithFormat(allocator,
206 NULL,
207 CFSTR("%@%s:%s"),
208 kSCDynamicStoreDomainPrefs,
209 typeStr,
210 pathStr);
211
212 CFAllocatorDeallocate(NULL, pathStr);
213 return key;
214 }
215
216
217 CFStringRef
218 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
219 CFStringRef prefsID,
220 SCPreferencesKeyType keyType)
221 {
222 return _SCPNotificationKey(allocator, prefsID, FALSE, NULL, keyType);
223 }
224
225
226 CFStringRef
227 SCDynamicStoreKeyCreateUserPreferences(CFAllocatorRef allocator,
228 CFStringRef prefsID,
229 CFStringRef user,
230 SCPreferencesKeyType keyType)
231 {
232 return _SCPNotificationKey(allocator, prefsID, TRUE, user, keyType);
233 }