2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <SystemConfiguration/SystemConfiguration.h>
27 SCDKeyCreateConsoleUser()
29 return SCDKeyCreate(CFSTR("%@/%@/%@"),
30 CFSTR("state:"), // FIXME!!! (should be kSCCacheDomainState)
32 kSCEntUsersConsoleUser
);
37 SCDConsoleUserGet(char *user
, int userlen
, uid_t
*uid
, gid_t
*gid
)
40 SCDHandleRef handle
= NULL
;
42 SCDSessionRef session
= NULL
;
45 /* get current user */
46 status
= SCDOpen(&session
, CFSTR("SCDConsoleUserGet"));
47 if (status
!= SCD_OK
) {
51 key
= SCDKeyCreateConsoleUser();
52 status
= SCDGet(session
, key
, &handle
);
54 if (status
!= SCD_OK
) {
58 dict
= SCDHandleGetData(handle
);
60 if (user
&& (userlen
> 0)) {
61 CFStringRef consoleUser
;
64 if (CFDictionaryGetValueIfPresent(dict
,
65 kSCPropUsersConsoleUserName
,
66 (void **)&consoleUser
)) {
70 range
= CFRangeMake(0, CFStringGetLength(consoleUser
));
71 (void)CFStringGetBytes(consoleUser
,
73 kCFStringEncodingMacRoman
,
86 if (CFDictionaryGetValueIfPresent(dict
,
87 kSCPropUsersConsoleUserUID
,
89 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
99 if (CFDictionaryGetValueIfPresent(dict
,
100 kSCPropUsersConsoleUserGID
,
102 if (CFNumberGetValue(num
, kCFNumberSInt32Type
, &val
)) {
110 if (handle
) SCDHandleRelease(handle
);
111 if (session
) (void) SCDClose(&session
);
117 SCDConsoleUserSet(const char *user
, uid_t uid
, gid_t gid
)
119 CFStringRef consoleUser
;
120 CFMutableDictionaryRef dict
= NULL
;
121 SCDHandleRef handle
= NULL
;
122 CFStringRef key
= SCDKeyCreateConsoleUser();
124 SCDSessionRef session
= NULL
;
127 status
= SCDOpen(&session
, CFSTR("SCDConsoleUserSet"));
128 if (status
!= SCD_OK
) {
133 (void)SCDRemove(session
, key
);
137 dict
= CFDictionaryCreateMutable(NULL
,
139 &kCFTypeDictionaryKeyCallBacks
,
140 &kCFTypeDictionaryValueCallBacks
);
142 consoleUser
= CFStringCreateWithCString(NULL
, user
, kCFStringEncodingMacRoman
);
143 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserName
, consoleUser
);
144 CFRelease(consoleUser
);
146 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&uid
);
147 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserUID
, num
);
150 num
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, (SInt32
*)&gid
);
151 CFDictionarySetValue(dict
, kSCPropUsersConsoleUserGID
, num
);
154 handle
= SCDHandleInit();
155 SCDHandleSetData(handle
, dict
);
157 status
= SCDLock(session
);
158 if (status
!= SCD_OK
) {
161 (void)SCDRemove(session
, key
);
162 (void)SCDAdd (session
, key
, handle
);
163 status
= SCDUnlock(session
);
167 if (dict
) CFRelease(dict
);
168 if (handle
) SCDHandleRelease(handle
);
169 if (key
) CFRelease(key
);
170 if (session
) (void) SCDClose(&session
);