2 * Copyright (c) 2000-2005, 2009, 2011, 2015 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
;
90 key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
91 dict
= SCDynamicStoreCopyValue(store
, key
);
93 if (!isA_CFDictionary(dict
)) {
94 _SCErrorSet(kSCStatusNoKey
);
98 consoleUser
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserName
);
99 consoleUser
= isA_CFString(consoleUser
);
101 _SCErrorSet(kSCStatusNoKey
);
105 CFRetain(consoleUser
);
111 num
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserUID
);
112 if (isA_CFNumber(num
)) {
113 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
123 num
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserGID
);
124 if (isA_CFNumber(num
)) {
125 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
133 if (dict
) CFRelease(dict
);
139 SCDynamicStoreCopyConsoleInformation(SCDynamicStoreRef store
)
141 CFDictionaryRef dict
= NULL
;
142 CFArrayRef info
= NULL
;
145 key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
146 dict
= SCDynamicStoreCopyValue(store
, key
);
148 if (!isA_CFDictionary(dict
)) {
149 _SCErrorSet(kSCStatusNoKey
);
153 info
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleSessionInfo
);
154 info
= isA_CFArray(info
);
156 _SCErrorSet(kSCStatusNoKey
);
164 if (dict
) CFRelease(dict
);
170 SCDynamicStoreSetConsoleInformation(SCDynamicStoreRef store
,
176 CFStringRef consoleUser
;
177 CFMutableDictionaryRef dict
= NULL
;
178 CFStringRef key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
181 if ((user
== NULL
) && (sessions
== NULL
)) {
182 ok
= SCDynamicStoreRemoveValue(store
, key
);
186 dict
= CFDictionaryCreateMutable(NULL
,
188 &kCFTypeDictionaryKeyCallBacks
,
189 &kCFTypeDictionaryValueCallBacks
);
194 consoleUser
= CFStringCreateWithCString(NULL
, user
, kCFStringEncodingMacRoman
);
195 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserName
, consoleUser
);
196 CFRelease(consoleUser
);
198 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&uid
);
199 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserUID
, num
);
202 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&gid
);
203 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserGID
, num
);
207 if (sessions
!= NULL
) {
208 CFDictionarySetValue(dict
, kSCPropUsersConsoleSessionInfo
, sessions
);
211 ok
= SCDynamicStoreSetValue(store
, key
, dict
);
215 if (dict
) CFRelease(dict
);
216 if (key
) CFRelease(key
);
222 SCDynamicStoreSetConsoleUser(SCDynamicStoreRef store
,
227 CFStringRef consoleUser
;
228 CFMutableDictionaryRef dict
= NULL
;
229 CFStringRef key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
234 ok
= SCDynamicStoreRemoveValue(store
, key
);
238 dict
= CFDictionaryCreateMutable(NULL
,
240 &kCFTypeDictionaryKeyCallBacks
,
241 &kCFTypeDictionaryValueCallBacks
);
243 consoleUser
= CFStringCreateWithCString(NULL
, user
, kCFStringEncodingMacRoman
);
244 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserName
, consoleUser
);
245 CFRelease(consoleUser
);
247 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&uid
);
248 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserUID
, num
);
251 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&gid
);
252 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserGID
, num
);
255 ok
= SCDynamicStoreSetValue(store
, key
, dict
);
259 if (dict
) CFRelease(dict
);
260 if (key
) CFRelease(key
);