2 * Copyright (c) 2000-2005, 2009 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * May 1, 2003 Allan Nathanson <ajn@apple.com>
28 * - add console [session] information SPIs
30 * June 1, 2001 Allan Nathanson <ajn@apple.com>
31 * - public API conversion
33 * January 2, 2001 Allan Nathanson <ajn@apple.com>
37 #include <SystemConfiguration/SystemConfiguration.h>
38 #include <SystemConfiguration/SCValidation.h>
39 #include <SystemConfiguration/SCPrivate.h>
42 #undef kSCPropUsersConsoleUserName
43 #define kSCPropUsersConsoleUserName CFSTR("Name")
45 #undef kSCPropUsersConsoleUserUID
46 #define kSCPropUsersConsoleUserUID CFSTR("UID")
48 #undef kSCPropUsersConsoleUserGID
49 #define kSCPropUsersConsoleUserGID CFSTR("GID")
51 #undef kSCPropUsersConsoleSessionInfo
52 #define kSCPropUsersConsoleSessionInfo CFSTR("SessionInfo")
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 */
62 // from CoreGraphics (CGSSession.h)
63 const CFStringRef kSCConsoleSessionID
= CFSTR("kCGSSessionIDKey"); /* value is CFNumber */
66 const CFStringRef kSCConsoleSessionSystemSafeBoot
= CFSTR("kCGSSessionSystemSafeBoot"); /* value is CFBoolean */
67 const CFStringRef kSCConsoleSessionLoginwindowSafeLogin
= CFSTR("kCGSSessionLoginwindowSafeLogin"); /* value is CFBoolean */
71 SCDynamicStoreKeyCreateConsoleUser(CFAllocatorRef allocator
)
73 return SCDynamicStoreKeyCreate(allocator
,
75 kSCDynamicStoreDomainState
,
77 kSCEntUsersConsoleUser
);
82 SCDynamicStoreCopyConsoleUser(SCDynamicStoreRef store
,
86 CFStringRef consoleUser
= NULL
;
87 CFDictionaryRef dict
= NULL
;
89 Boolean tempSession
= FALSE
;
92 store
= SCDynamicStoreCreate(NULL
,
93 CFSTR("SCDynamicStoreCopyConsoleUser"),
102 key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
103 dict
= SCDynamicStoreCopyValue(store
, key
);
105 if (!isA_CFDictionary(dict
)) {
106 _SCErrorSet(kSCStatusNoKey
);
110 consoleUser
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserName
);
111 consoleUser
= isA_CFString(consoleUser
);
113 _SCErrorSet(kSCStatusNoKey
);
117 CFRetain(consoleUser
);
123 num
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserUID
);
124 if (isA_CFNumber(num
)) {
125 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
135 num
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserGID
);
136 if (isA_CFNumber(num
)) {
137 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
145 if (tempSession
) CFRelease(store
);
146 if (dict
) CFRelease(dict
);
152 SCDynamicStoreCopyConsoleInformation(SCDynamicStoreRef store
)
154 CFDictionaryRef dict
= NULL
;
155 CFArrayRef info
= NULL
;
157 Boolean tempSession
= FALSE
;
160 store
= SCDynamicStoreCreate(NULL
,
161 CFSTR("SCDynamicStoreCopyConsoleUser"),
170 key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
171 dict
= SCDynamicStoreCopyValue(store
, key
);
173 if (!isA_CFDictionary(dict
)) {
174 _SCErrorSet(kSCStatusNoKey
);
178 info
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleSessionInfo
);
179 info
= isA_CFArray(info
);
181 _SCErrorSet(kSCStatusNoKey
);
189 if (tempSession
) CFRelease(store
);
190 if (dict
) CFRelease(dict
);
196 SCDynamicStoreSetConsoleInformation(SCDynamicStoreRef store
,
202 CFStringRef consoleUser
;
203 CFMutableDictionaryRef dict
= NULL
;
204 CFStringRef key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
206 Boolean tempSession
= FALSE
;
209 store
= SCDynamicStoreCreate(NULL
,
210 CFSTR("SCDynamicStoreSetConsoleUser"),
219 if ((user
== NULL
) && (sessions
== NULL
)) {
220 ok
= SCDynamicStoreRemoveValue(store
, key
);
224 dict
= CFDictionaryCreateMutable(NULL
,
226 &kCFTypeDictionaryKeyCallBacks
,
227 &kCFTypeDictionaryValueCallBacks
);
232 consoleUser
= CFStringCreateWithCString(NULL
, user
, kCFStringEncodingMacRoman
);
233 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserName
, consoleUser
);
234 CFRelease(consoleUser
);
236 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&uid
);
237 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserUID
, num
);
240 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&gid
);
241 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserGID
, num
);
245 if (sessions
!= NULL
) {
246 CFDictionarySetValue(dict
, kSCPropUsersConsoleSessionInfo
, sessions
);
249 ok
= SCDynamicStoreSetValue(store
, key
, dict
);
253 if (dict
) CFRelease(dict
);
254 if (key
) CFRelease(key
);
255 if (tempSession
) CFRelease(store
);
261 SCDynamicStoreSetConsoleUser(SCDynamicStoreRef store
,
266 CFStringRef consoleUser
;
267 CFMutableDictionaryRef dict
= NULL
;
268 CFStringRef key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
271 Boolean tempSession
= FALSE
;
274 store
= SCDynamicStoreCreate(NULL
,
275 CFSTR("SCDynamicStoreSetConsoleUser"),
285 ok
= SCDynamicStoreRemoveValue(store
, key
);
289 dict
= CFDictionaryCreateMutable(NULL
,
291 &kCFTypeDictionaryKeyCallBacks
,
292 &kCFTypeDictionaryValueCallBacks
);
294 consoleUser
= CFStringCreateWithCString(NULL
, user
, kCFStringEncodingMacRoman
);
295 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserName
, consoleUser
);
296 CFRelease(consoleUser
);
298 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&uid
);
299 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserUID
, num
);
302 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&gid
);
303 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserGID
, num
);
306 ok
= SCDynamicStoreSetValue(store
, key
, dict
);
310 if (dict
) CFRelease(dict
);
311 if (key
) CFRelease(key
);
312 if (tempSession
) CFRelease(store
);