2 * Copyright (c) 2000-2003 Apple Computer, 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 #ifndef kSCPropUsersConsoleUserName
43 #define kSCPropUsersConsoleUserName CFSTR("Name")
46 #ifndef kSCPropUsersConsoleUserUID
47 #define kSCPropUsersConsoleUserUID CFSTR("UID")
50 #ifndef kSCPropUsersConsoleUserGID
51 #define kSCPropUsersConsoleUserGID CFSTR("GID")
54 #ifndef kSCPropUsersConsoleSessionInfo
55 #define kSCPropUsersConsoleSessionInfo CFSTR("SessionInfo")
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 */
67 SCDynamicStoreKeyCreateConsoleUser(CFAllocatorRef allocator
)
69 return SCDynamicStoreKeyCreate(allocator
,
71 kSCDynamicStoreDomainState
,
73 kSCEntUsersConsoleUser
);
78 SCDynamicStoreCopyConsoleUser(SCDynamicStoreRef store
,
82 CFStringRef consoleUser
= NULL
;
83 CFDictionaryRef dict
= NULL
;
85 Boolean tempSession
= FALSE
;
88 store
= SCDynamicStoreCreate(NULL
,
89 CFSTR("SCDynamicStoreCopyConsoleUser"),
93 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
99 key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
100 dict
= SCDynamicStoreCopyValue(store
, key
);
102 if (!isA_CFDictionary(dict
)) {
103 _SCErrorSet(kSCStatusNoKey
);
107 consoleUser
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserName
);
108 consoleUser
= isA_CFString(consoleUser
);
110 _SCErrorSet(kSCStatusNoKey
);
114 CFRetain(consoleUser
);
120 num
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserUID
);
121 if (isA_CFNumber(num
)) {
122 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
132 num
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserGID
);
133 if (isA_CFNumber(num
)) {
134 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
142 if (tempSession
) CFRelease(store
);
143 if (dict
) CFRelease(dict
);
149 SCDynamicStoreCopyConsoleInformation(SCDynamicStoreRef store
)
151 CFDictionaryRef dict
= NULL
;
152 CFArrayRef info
= NULL
;
154 Boolean tempSession
= FALSE
;
157 store
= SCDynamicStoreCreate(NULL
,
158 CFSTR("SCDynamicStoreCopyConsoleUser"),
162 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
168 key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
169 dict
= SCDynamicStoreCopyValue(store
, key
);
171 if (!isA_CFDictionary(dict
)) {
172 _SCErrorSet(kSCStatusNoKey
);
176 info
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleSessionInfo
);
177 info
= isA_CFArray(info
);
179 _SCErrorSet(kSCStatusNoKey
);
187 if (tempSession
) CFRelease(store
);
188 if (dict
) CFRelease(dict
);
194 SCDynamicStoreSetConsoleInformation(SCDynamicStoreRef store
,
200 CFStringRef consoleUser
;
201 CFMutableDictionaryRef dict
= NULL
;
202 CFStringRef key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
205 Boolean tempSession
= FALSE
;
208 store
= SCDynamicStoreCreate(NULL
,
209 CFSTR("SCDynamicStoreSetConsoleUser"),
213 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
220 ok
= SCDynamicStoreRemoveValue(store
, key
);
224 dict
= CFDictionaryCreateMutable(NULL
,
226 &kCFTypeDictionaryKeyCallBacks
,
227 &kCFTypeDictionaryValueCallBacks
);
229 consoleUser
= CFStringCreateWithCString(NULL
, user
, kCFStringEncodingMacRoman
);
230 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserName
, consoleUser
);
231 CFRelease(consoleUser
);
233 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&uid
);
234 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserUID
, num
);
237 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&gid
);
238 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserGID
, num
);
241 CFDictionarySetValue(dict
, kSCPropUsersConsoleSessionInfo
, sessions
);
243 ok
= SCDynamicStoreSetValue(store
, key
, dict
);
247 if (dict
) CFRelease(dict
);
248 if (key
) CFRelease(key
);
249 if (tempSession
) CFRelease(store
);
255 SCDynamicStoreSetConsoleUser(SCDynamicStoreRef store
,
260 CFStringRef consoleUser
;
261 CFMutableDictionaryRef dict
= NULL
;
262 CFStringRef key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
265 Boolean tempSession
= FALSE
;
268 store
= SCDynamicStoreCreate(NULL
,
269 CFSTR("SCDynamicStoreSetConsoleUser"),
273 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
280 ok
= SCDynamicStoreRemoveValue(store
, key
);
284 dict
= CFDictionaryCreateMutable(NULL
,
286 &kCFTypeDictionaryKeyCallBacks
,
287 &kCFTypeDictionaryValueCallBacks
);
289 consoleUser
= CFStringCreateWithCString(NULL
, user
, kCFStringEncodingMacRoman
);
290 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserName
, consoleUser
);
291 CFRelease(consoleUser
);
293 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&uid
);
294 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserUID
, num
);
297 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&gid
);
298 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserGID
, num
);
301 ok
= SCDynamicStoreSetValue(store
, key
, dict
);
305 if (dict
) CFRelease(dict
);
306 if (key
) CFRelease(key
);
307 if (tempSession
) CFRelease(store
);