]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDConsoleUser.c
900db519c82baf000154af18b9d39500ceabde1b
[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 /*
24 * Modification History
25 *
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
28 *
29 * January 2, 2001 Allan Nathanson <ajn@apple.com>
30 * - initial revision
31 */
32
33 #include <SystemConfiguration/SystemConfiguration.h>
34 #include <SystemConfiguration/SCPrivate.h>
35 #include <SystemConfiguration/SCValidation.h>
36
37 CFStringRef
38 SCDynamicStoreKeyCreateConsoleUser(CFAllocatorRef allocator)
39 {
40 return SCDynamicStoreKeyCreate(allocator,
41 CFSTR("%@/%@/%@"),
42 kSCDynamicStoreDomainState,
43 kSCCompUsers,
44 kSCEntUsersConsoleUser);
45 }
46
47
48 CFStringRef
49 SCDynamicStoreCopyConsoleUser(SCDynamicStoreRef store,
50 uid_t *uid,
51 gid_t *gid)
52 {
53 CFStringRef consoleUser = NULL;
54 CFDictionaryRef dict = NULL;
55 CFStringRef key;
56 SCDynamicStoreRef mySession = store;
57
58 if (!store) {
59 mySession = SCDynamicStoreCreate(NULL,
60 CFSTR("SCDynamicStoreCopyConsoleUser"),
61 NULL,
62 NULL);
63 if (!mySession) {
64 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCreate() failed"));
65 return NULL;
66 }
67 }
68
69 key = SCDynamicStoreKeyCreateConsoleUser(NULL);
70 dict = SCDynamicStoreCopyValue(mySession, key);
71 CFRelease(key);
72 if (!isA_CFDictionary(dict)) {
73 _SCErrorSet(kSCStatusNoKey);
74 goto done;
75 }
76
77 consoleUser = CFDictionaryGetValue(dict, kSCPropUsersConsoleUserName);
78 consoleUser = isA_CFString(consoleUser);
79 if (!consoleUser) {
80 _SCErrorSet(kSCStatusNoKey);
81 goto done;
82 }
83
84 CFRetain(consoleUser);
85
86 if (uid) {
87 CFNumberRef num;
88 SInt32 val;
89
90 num = CFDictionaryGetValue(dict, kSCPropUsersConsoleUserUID);
91 if (isA_CFNumber(num)) {
92 if (CFNumberGetValue(num, kCFNumberSInt32Type, &val)) {
93 *uid = (uid_t)val;
94 }
95 }
96 }
97
98 if (gid) {
99 CFNumberRef num;
100 SInt32 val;
101
102 num = CFDictionaryGetValue(dict, kSCPropUsersConsoleUserGID);
103 if (isA_CFNumber(num)) {
104 if (CFNumberGetValue(num, kCFNumberSInt32Type, &val)) {
105 *gid = (gid_t)val;
106 }
107 }
108 }
109
110 done :
111
112 if (!store && mySession) CFRelease(mySession);
113 if (dict) CFRelease(dict);
114 return consoleUser;
115
116 }
117
118
119 Boolean
120 SCDynamicStoreSetConsoleUser(SCDynamicStoreRef store,
121 const char *user,
122 uid_t uid,
123 gid_t gid)
124 {
125 CFStringRef consoleUser;
126 CFMutableDictionaryRef dict = NULL;
127 CFStringRef key = SCDynamicStoreKeyCreateConsoleUser(NULL);
128 SCDynamicStoreRef mySession = store;
129 CFNumberRef num;
130 Boolean ok = TRUE;
131
132 if (!store) {
133 mySession = SCDynamicStoreCreate(NULL,
134 CFSTR("SCDynamicStoreSetConsoleUser"),
135 NULL,
136 NULL);
137 if (!mySession) {
138 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCreate() failed"));
139 return FALSE;
140 }
141 }
142
143 if (user == NULL) {
144 ok = SCDynamicStoreRemoveValue(mySession, key);
145 goto done;
146 }
147
148 dict = CFDictionaryCreateMutable(NULL,
149 0,
150 &kCFTypeDictionaryKeyCallBacks,
151 &kCFTypeDictionaryValueCallBacks);
152
153 consoleUser = CFStringCreateWithCString(NULL, user, kCFStringEncodingMacRoman);
154 CFDictionarySetValue(dict, kSCPropUsersConsoleUserName, consoleUser);
155 CFRelease(consoleUser);
156
157 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&uid);
158 CFDictionarySetValue(dict, kSCPropUsersConsoleUserUID, num);
159 CFRelease(num);
160
161 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&gid);
162 CFDictionarySetValue(dict, kSCPropUsersConsoleUserGID, num);
163 CFRelease(num);
164
165 ok = SCDynamicStoreSetValue(mySession, key, dict);
166
167 done :
168
169 if (dict) CFRelease(dict);
170 if (key) CFRelease(key);
171 if (!store && mySession) CFRelease(mySession);
172 return ok;
173 }