]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDConsoleUser.c
configd-24.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDConsoleUser.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <SystemConfiguration/SystemConfiguration.h>
24
25
26 CFStringRef
27 SCDKeyCreateConsoleUser()
28 {
29 return SCDKeyCreate(CFSTR("%@/%@/%@"),
30 CFSTR("state:"), // FIXME!!! (should be kSCCacheDomainState)
31 kSCCompUsers,
32 kSCEntUsersConsoleUser);
33 }
34
35
36 SCDStatus
37 SCDConsoleUserGet(char *user, int userlen, uid_t *uid, gid_t *gid)
38 {
39 CFDictionaryRef dict;
40 SCDHandleRef handle = NULL;
41 CFStringRef key;
42 SCDSessionRef session = NULL;
43 SCDStatus status;
44
45 /* get current user */
46 status = SCDOpen(&session, CFSTR("SCDConsoleUserGet"));
47 if (status != SCD_OK) {
48 goto done;
49 }
50
51 key = SCDKeyCreateConsoleUser();
52 status = SCDGet(session, key, &handle);
53 CFRelease(key);
54 if (status != SCD_OK) {
55 goto done;
56 }
57
58 dict = SCDHandleGetData(handle);
59
60 if (user && (userlen > 0)) {
61 CFStringRef consoleUser;
62
63 bzero(user, userlen);
64 if (CFDictionaryGetValueIfPresent(dict,
65 kSCPropUsersConsoleUserName,
66 (void **)&consoleUser)) {
67 CFIndex len;
68 CFRange range;
69
70 range = CFRangeMake(0, CFStringGetLength(consoleUser));
71 (void)CFStringGetBytes(consoleUser,
72 range,
73 kCFStringEncodingMacRoman,
74 0,
75 FALSE,
76 user,
77 userlen,
78 &len);
79 }
80 }
81
82 if (uid) {
83 CFNumberRef num;
84 SInt32 val;
85
86 if (CFDictionaryGetValueIfPresent(dict,
87 kSCPropUsersConsoleUserUID,
88 (void **)&num)) {
89 if (CFNumberGetValue(num, kCFNumberSInt32Type, &val)) {
90 *uid = (uid_t)val;
91 }
92 }
93 }
94
95 if (gid) {
96 CFNumberRef num;
97 SInt32 val;
98
99 if (CFDictionaryGetValueIfPresent(dict,
100 kSCPropUsersConsoleUserGID,
101 (void **)&num)) {
102 if (CFNumberGetValue(num, kCFNumberSInt32Type, &val)) {
103 *gid = (gid_t)val;
104 }
105 }
106 }
107
108 done :
109
110 if (handle) SCDHandleRelease(handle);
111 if (session) (void) SCDClose(&session);
112 return status;
113 }
114
115
116 SCDStatus
117 SCDConsoleUserSet(const char *user, uid_t uid, gid_t gid)
118 {
119 CFStringRef consoleUser;
120 CFMutableDictionaryRef dict = NULL;
121 SCDHandleRef handle = NULL;
122 CFStringRef key = SCDKeyCreateConsoleUser();
123 CFNumberRef num;
124 SCDSessionRef session = NULL;
125 SCDStatus status;
126
127 status = SCDOpen(&session, CFSTR("SCDConsoleUserSet"));
128 if (status != SCD_OK) {
129 goto done;
130 }
131
132 if (user == NULL) {
133 (void)SCDRemove(session, key);
134 goto done;
135 }
136
137 dict = CFDictionaryCreateMutable(NULL,
138 0,
139 &kCFTypeDictionaryKeyCallBacks,
140 &kCFTypeDictionaryValueCallBacks);
141
142 consoleUser = CFStringCreateWithCString(NULL, user, kCFStringEncodingMacRoman);
143 CFDictionarySetValue(dict, kSCPropUsersConsoleUserName, consoleUser);
144 CFRelease(consoleUser);
145
146 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&uid);
147 CFDictionarySetValue(dict, kSCPropUsersConsoleUserUID, num);
148 CFRelease(num);
149
150 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&gid);
151 CFDictionarySetValue(dict, kSCPropUsersConsoleUserGID, num);
152 CFRelease(num);
153
154 handle = SCDHandleInit();
155 SCDHandleSetData(handle, dict);
156
157 status = SCDLock(session);
158 if (status != SCD_OK) {
159 goto done;
160 }
161 (void)SCDRemove(session, key);
162 (void)SCDAdd (session, key, handle);
163 status = SCDUnlock(session);
164
165 done :
166
167 if (dict) CFRelease(dict);
168 if (handle) SCDHandleRelease(handle);
169 if (key) CFRelease(key);
170 if (session) (void) SCDClose(&session);
171 return status;
172 }