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