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