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