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