2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * May 1, 2003 Allan Nathanson <ajn@apple.com>
30 * - add console [session] information SPIs
32 * June 1, 2001 Allan Nathanson <ajn@apple.com>
33 * - public API conversion
35 * January 2, 2001 Allan Nathanson <ajn@apple.com>
39 #include <SystemConfiguration/SystemConfiguration.h>
40 #include <SystemConfiguration/SCValidation.h>
41 #include <SystemConfiguration/SCPrivate.h>
44 #ifndef kSCPropUsersConsoleUserName
45 #define kSCPropUsersConsoleUserName CFSTR("Name")
48 #ifndef kSCPropUsersConsoleUserUID
49 #define kSCPropUsersConsoleUserUID CFSTR("UID")
52 #ifndef kSCPropUsersConsoleUserGID
53 #define kSCPropUsersConsoleUserGID CFSTR("GID")
56 #ifndef kSCPropUsersConsoleSessionInfo
57 #define kSCPropUsersConsoleSessionInfo CFSTR("SessionInfo")
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 */
69 SCDynamicStoreKeyCreateConsoleUser(CFAllocatorRef allocator
)
71 return SCDynamicStoreKeyCreate(allocator
,
73 kSCDynamicStoreDomainState
,
75 kSCEntUsersConsoleUser
);
80 SCDynamicStoreCopyConsoleUser(SCDynamicStoreRef store
,
84 CFStringRef consoleUser
= NULL
;
85 CFDictionaryRef dict
= NULL
;
87 Boolean tempSession
= FALSE
;
90 store
= SCDynamicStoreCreate(NULL
,
91 CFSTR("SCDynamicStoreCopyConsoleUser"),
95 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
101 key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
102 dict
= SCDynamicStoreCopyValue(store
, key
);
104 if (!isA_CFDictionary(dict
)) {
105 _SCErrorSet(kSCStatusNoKey
);
109 consoleUser
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserName
);
110 consoleUser
= isA_CFString(consoleUser
);
112 _SCErrorSet(kSCStatusNoKey
);
116 CFRetain(consoleUser
);
122 num
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserUID
);
123 if (isA_CFNumber(num
)) {
124 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
134 num
= CFDictionaryGetValue(dict
, kSCPropUsersConsoleUserGID
);
135 if (isA_CFNumber(num
)) {
136 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
144 if (tempSession
) CFRelease(store
);
145 if (dict
) CFRelease(dict
);
151 SCDynamicStoreCopyConsoleInformation(SCDynamicStoreRef store
)
153 CFDictionaryRef dict
= NULL
;
154 CFArrayRef info
= NULL
;
156 Boolean tempSession
= FALSE
;
159 store
= SCDynamicStoreCreate(NULL
,
160 CFSTR("SCDynamicStoreCopyConsoleUser"),
164 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
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
);
207 Boolean tempSession
= FALSE
;
210 store
= SCDynamicStoreCreate(NULL
,
211 CFSTR("SCDynamicStoreSetConsoleUser"),
215 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
222 ok
= SCDynamicStoreRemoveValue(store
, key
);
226 dict
= CFDictionaryCreateMutable(NULL
,
228 &kCFTypeDictionaryKeyCallBacks
,
229 &kCFTypeDictionaryValueCallBacks
);
231 consoleUser
= CFStringCreateWithCString(NULL
, user
, kCFStringEncodingMacRoman
);
232 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserName
, consoleUser
);
233 CFRelease(consoleUser
);
235 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&uid
);
236 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserUID
, num
);
239 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&gid
);
240 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserGID
, num
);
243 CFDictionarySetValue(dict
, kSCPropUsersConsoleSessionInfo
, sessions
);
245 ok
= SCDynamicStoreSetValue(store
, key
, dict
);
249 if (dict
) CFRelease(dict
);
250 if (key
) CFRelease(key
);
251 if (tempSession
) CFRelease(store
);
257 SCDynamicStoreSetConsoleUser(SCDynamicStoreRef store
,
262 CFStringRef consoleUser
;
263 CFMutableDictionaryRef dict
= NULL
;
264 CFStringRef key
= SCDynamicStoreKeyCreateConsoleUser(NULL
);
267 Boolean tempSession
= FALSE
;
270 store
= SCDynamicStoreCreate(NULL
,
271 CFSTR("SCDynamicStoreSetConsoleUser"),
275 SCLog(_sc_verbose
, LOG_INFO
, CFSTR("SCDynamicStoreCreate() failed"));
282 ok
= SCDynamicStoreRemoveValue(store
, key
);
286 dict
= CFDictionaryCreateMutable(NULL
,
288 &kCFTypeDictionaryKeyCallBacks
,
289 &kCFTypeDictionaryValueCallBacks
);
291 consoleUser
= CFStringCreateWithCString(NULL
, user
, kCFStringEncodingMacRoman
);
292 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserName
, consoleUser
);
293 CFRelease(consoleUser
);
295 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&uid
);
296 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserUID
, num
);
299 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&gid
);
300 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserGID
, num
);
303 ok
= SCDynamicStoreSetValue(store
, key
, dict
);
307 if (dict
) CFRelease(dict
);
308 if (key
) CFRelease(key
);
309 if (tempSession
) CFRelease(store
);