]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDConsoleUser.c
configd-130.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDConsoleUser.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 * May 1, 2003 Allan Nathanson <ajn@apple.com>
28 * - add console [session] information SPIs
29 *
30 * June 1, 2001 Allan Nathanson <ajn@apple.com>
31 * - public API conversion
32 *
33 * January 2, 2001 Allan Nathanson <ajn@apple.com>
34 * - initial revision
35 */
36
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCValidation.h>
39 #include <SystemConfiguration/SCPrivate.h>
40
41
42 #ifndef kSCPropUsersConsoleUserName
43 #define kSCPropUsersConsoleUserName CFSTR("Name")
44 #endif
45
46 #ifndef kSCPropUsersConsoleUserUID
47 #define kSCPropUsersConsoleUserUID CFSTR("UID")
48 #endif
49
50 #ifndef kSCPropUsersConsoleUserGID
51 #define kSCPropUsersConsoleUserGID CFSTR("GID")
52 #endif
53
54 #ifndef kSCPropUsersConsoleSessionInfo
55 #define kSCPropUsersConsoleSessionInfo CFSTR("SessionInfo")
56 #endif
57
58
59 // from CoreGraphics (CGSession.h)
60 const CFStringRef kSCConsoleSessionUserName = CFSTR("kCGSSessionUserNameKey"); /* value is CFString */
61 const CFStringRef kSCConsoleSessionUID = CFSTR("kCGSSessionUserIDKey"); /* value is CFNumber (a uid_t) */
62 const CFStringRef kSCConsoleSessionConsoleSet = CFSTR("kCGSSessionConsoleSetKey"); /* value is CFNumber */
63 const CFStringRef kSCConsoleSessionOnConsole = CFSTR("kCGSSessionOnConsoleKey"); /* value is CFBoolean */
64 const CFStringRef kSCConsoleSessionLoginDone = CFSTR("kCGSessionLoginDoneKey"); /* value is CFBoolean */
65
66 // from CoreGraphics (CGSSession.h)
67 const CFStringRef kSCConsoleSessionID = CFSTR("kCGSSessionIDKey"); /* value is CFNumber */
68
69 // from loginwindow
70 const CFStringRef kSCConsoleSessionSystemSafeBoot = CFSTR("kCGSSessionSystemSafeBoot"); /* value is CFBoolean */
71 const CFStringRef kSCConsoleSessionLoginwindowSafeLogin = CFSTR("kCGSSessionLoginwindowSafeLogin"); /* value is CFBoolean */
72
73
74 CFStringRef
75 SCDynamicStoreKeyCreateConsoleUser(CFAllocatorRef allocator)
76 {
77 return SCDynamicStoreKeyCreate(allocator,
78 CFSTR("%@/%@/%@"),
79 kSCDynamicStoreDomainState,
80 kSCCompUsers,
81 kSCEntUsersConsoleUser);
82 }
83
84
85 CFStringRef
86 SCDynamicStoreCopyConsoleUser(SCDynamicStoreRef store,
87 uid_t *uid,
88 gid_t *gid)
89 {
90 CFStringRef consoleUser = NULL;
91 CFDictionaryRef dict = NULL;
92 CFStringRef key;
93 Boolean tempSession = FALSE;
94
95 if (store == NULL) {
96 store = SCDynamicStoreCreate(NULL,
97 CFSTR("SCDynamicStoreCopyConsoleUser"),
98 NULL,
99 NULL);
100 if (store == NULL) {
101 return NULL;
102 }
103 tempSession = TRUE;
104 }
105
106 key = SCDynamicStoreKeyCreateConsoleUser(NULL);
107 dict = SCDynamicStoreCopyValue(store, key);
108 CFRelease(key);
109 if (!isA_CFDictionary(dict)) {
110 _SCErrorSet(kSCStatusNoKey);
111 goto done;
112 }
113
114 consoleUser = CFDictionaryGetValue(dict, kSCPropUsersConsoleUserName);
115 consoleUser = isA_CFString(consoleUser);
116 if (!consoleUser) {
117 _SCErrorSet(kSCStatusNoKey);
118 goto done;
119 }
120
121 CFRetain(consoleUser);
122
123 if (uid) {
124 CFNumberRef num;
125 SInt32 val;
126
127 num = CFDictionaryGetValue(dict, kSCPropUsersConsoleUserUID);
128 if (isA_CFNumber(num)) {
129 if (CFNumberGetValue(num, kCFNumberSInt32Type, &val)) {
130 *uid = (uid_t)val;
131 }
132 }
133 }
134
135 if (gid) {
136 CFNumberRef num;
137 SInt32 val;
138
139 num = CFDictionaryGetValue(dict, kSCPropUsersConsoleUserGID);
140 if (isA_CFNumber(num)) {
141 if (CFNumberGetValue(num, kCFNumberSInt32Type, &val)) {
142 *gid = (gid_t)val;
143 }
144 }
145 }
146
147 done :
148
149 if (tempSession) CFRelease(store);
150 if (dict) CFRelease(dict);
151 return consoleUser;
152 }
153
154
155 CFArrayRef
156 SCDynamicStoreCopyConsoleInformation(SCDynamicStoreRef store)
157 {
158 CFDictionaryRef dict = NULL;
159 CFArrayRef info = NULL;
160 CFStringRef key;
161 Boolean tempSession = FALSE;
162
163 if (store == NULL) {
164 store = SCDynamicStoreCreate(NULL,
165 CFSTR("SCDynamicStoreCopyConsoleUser"),
166 NULL,
167 NULL);
168 if (store == NULL) {
169 return NULL;
170 }
171 tempSession = TRUE;
172 }
173
174 key = SCDynamicStoreKeyCreateConsoleUser(NULL);
175 dict = SCDynamicStoreCopyValue(store, key);
176 CFRelease(key);
177 if (!isA_CFDictionary(dict)) {
178 _SCErrorSet(kSCStatusNoKey);
179 goto done;
180 }
181
182 info = CFDictionaryGetValue(dict, kSCPropUsersConsoleSessionInfo);
183 info = isA_CFArray(info);
184 if (!info) {
185 _SCErrorSet(kSCStatusNoKey);
186 goto done;
187 }
188
189 CFRetain(info);
190
191 done :
192
193 if (tempSession) CFRelease(store);
194 if (dict) CFRelease(dict);
195 return info;
196 }
197
198
199 Boolean
200 SCDynamicStoreSetConsoleInformation(SCDynamicStoreRef store,
201 const char *user,
202 uid_t uid,
203 gid_t gid,
204 CFArrayRef sessions)
205 {
206 CFStringRef consoleUser;
207 CFMutableDictionaryRef dict = NULL;
208 CFStringRef key = SCDynamicStoreKeyCreateConsoleUser(NULL);
209 Boolean ok = TRUE;
210 Boolean tempSession = FALSE;
211
212 if (store == NULL) {
213 store = SCDynamicStoreCreate(NULL,
214 CFSTR("SCDynamicStoreSetConsoleUser"),
215 NULL,
216 NULL);
217 if (store == NULL) {
218 return FALSE;
219 }
220 tempSession = TRUE;
221 }
222
223 if ((user == NULL) && (sessions == NULL)) {
224 (void) SCDynamicStoreRemoveValue(store, key);
225 goto done;
226 }
227
228 dict = CFDictionaryCreateMutable(NULL,
229 0,
230 &kCFTypeDictionaryKeyCallBacks,
231 &kCFTypeDictionaryValueCallBacks);
232
233 if (user != NULL) {
234 CFNumberRef num;
235
236 consoleUser = CFStringCreateWithCString(NULL, user, kCFStringEncodingMacRoman);
237 CFDictionarySetValue(dict, kSCPropUsersConsoleUserName, consoleUser);
238 CFRelease(consoleUser);
239
240 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&uid);
241 CFDictionarySetValue(dict, kSCPropUsersConsoleUserUID, num);
242 CFRelease(num);
243
244 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&gid);
245 CFDictionarySetValue(dict, kSCPropUsersConsoleUserGID, num);
246 CFRelease(num);
247 }
248
249 if (sessions != NULL) {
250 CFDictionarySetValue(dict, kSCPropUsersConsoleSessionInfo, sessions);
251 }
252
253 ok = SCDynamicStoreSetValue(store, key, dict);
254
255 done :
256
257 if (dict) CFRelease(dict);
258 if (key) CFRelease(key);
259 if (tempSession) CFRelease(store);
260 return ok;
261 }
262
263
264 Boolean
265 SCDynamicStoreSetConsoleUser(SCDynamicStoreRef store,
266 const char *user,
267 uid_t uid,
268 gid_t gid)
269 {
270 CFStringRef consoleUser;
271 CFMutableDictionaryRef dict = NULL;
272 CFStringRef key = SCDynamicStoreKeyCreateConsoleUser(NULL);
273 CFNumberRef num;
274 Boolean ok = TRUE;
275 Boolean tempSession = FALSE;
276
277 if (store == NULL) {
278 store = SCDynamicStoreCreate(NULL,
279 CFSTR("SCDynamicStoreSetConsoleUser"),
280 NULL,
281 NULL);
282 if (store == NULL) {
283 return FALSE;
284 }
285 tempSession = TRUE;
286 }
287
288 if (user == NULL) {
289 (void) SCDynamicStoreRemoveValue(store, key);
290 goto done;
291 }
292
293 dict = CFDictionaryCreateMutable(NULL,
294 0,
295 &kCFTypeDictionaryKeyCallBacks,
296 &kCFTypeDictionaryValueCallBacks);
297
298 consoleUser = CFStringCreateWithCString(NULL, user, kCFStringEncodingMacRoman);
299 CFDictionarySetValue(dict, kSCPropUsersConsoleUserName, consoleUser);
300 CFRelease(consoleUser);
301
302 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&uid);
303 CFDictionarySetValue(dict, kSCPropUsersConsoleUserUID, num);
304 CFRelease(num);
305
306 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&gid);
307 CFDictionarySetValue(dict, kSCPropUsersConsoleUserGID, num);
308 CFRelease(num);
309
310 ok = SCDynamicStoreSetValue(store, key, dict);
311
312 done :
313
314 if (dict) CFRelease(dict);
315 if (key) CFRelease(key);
316 if (tempSession) CFRelease(store);
317 return ok;
318 }