]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDConsoleUser.c
1eb518a44d707a30f63ffeb3d7f7b8d91b57aadf
[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/SCValidation.h>
35 #include <SystemConfiguration/SCPrivate.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 Boolean tempSession = FALSE;
57
58 if (!store) {
59 store = SCDynamicStoreCreate(NULL,
60 CFSTR("SCDynamicStoreCopyConsoleUser"),
61 NULL,
62 NULL);
63 if (!store) {
64 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCreate() failed"));
65 return NULL;
66 }
67 tempSession = TRUE;
68 }
69
70 key = SCDynamicStoreKeyCreateConsoleUser(NULL);
71 dict = SCDynamicStoreCopyValue(store, key);
72 CFRelease(key);
73 if (!isA_CFDictionary(dict)) {
74 _SCErrorSet(kSCStatusNoKey);
75 goto done;
76 }
77
78 consoleUser = CFDictionaryGetValue(dict, kSCPropUsersConsoleUserName);
79 consoleUser = isA_CFString(consoleUser);
80 if (!consoleUser) {
81 _SCErrorSet(kSCStatusNoKey);
82 goto done;
83 }
84
85 CFRetain(consoleUser);
86
87 if (uid) {
88 CFNumberRef num;
89 SInt32 val;
90
91 num = CFDictionaryGetValue(dict, kSCPropUsersConsoleUserUID);
92 if (isA_CFNumber(num)) {
93 if (CFNumberGetValue(num, kCFNumberSInt32Type, &val)) {
94 *uid = (uid_t)val;
95 }
96 }
97 }
98
99 if (gid) {
100 CFNumberRef num;
101 SInt32 val;
102
103 num = CFDictionaryGetValue(dict, kSCPropUsersConsoleUserGID);
104 if (isA_CFNumber(num)) {
105 if (CFNumberGetValue(num, kCFNumberSInt32Type, &val)) {
106 *gid = (gid_t)val;
107 }
108 }
109 }
110
111 done :
112
113 if (tempSession) CFRelease(store);
114 if (dict) CFRelease(dict);
115 return consoleUser;
116
117 }
118
119
120 Boolean
121 SCDynamicStoreSetConsoleUser(SCDynamicStoreRef store,
122 const char *user,
123 uid_t uid,
124 gid_t gid)
125 {
126 CFStringRef consoleUser;
127 CFMutableDictionaryRef dict = NULL;
128 CFStringRef key = SCDynamicStoreKeyCreateConsoleUser(NULL);
129 CFNumberRef num;
130 Boolean ok = TRUE;
131 Boolean tempSession = FALSE;
132
133 if (!store) {
134 store = SCDynamicStoreCreate(NULL,
135 CFSTR("SCDynamicStoreSetConsoleUser"),
136 NULL,
137 NULL);
138 if (!store) {
139 SCLog(_sc_verbose, LOG_INFO, CFSTR("SCDynamicStoreCreate() failed"));
140 return FALSE;
141 }
142 tempSession = TRUE;
143 }
144
145 if (user == NULL) {
146 ok = SCDynamicStoreRemoveValue(store, key);
147 goto done;
148 }
149
150 dict = CFDictionaryCreateMutable(NULL,
151 0,
152 &kCFTypeDictionaryKeyCallBacks,
153 &kCFTypeDictionaryValueCallBacks);
154
155 consoleUser = CFStringCreateWithCString(NULL, user, kCFStringEncodingMacRoman);
156 CFDictionarySetValue(dict, kSCPropUsersConsoleUserName, consoleUser);
157 CFRelease(consoleUser);
158
159 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&uid);
160 CFDictionarySetValue(dict, kSCPropUsersConsoleUserUID, num);
161 CFRelease(num);
162
163 num = CFNumberCreate(NULL, kCFNumberSInt32Type, (SInt32 *)&gid);
164 CFDictionarySetValue(dict, kSCPropUsersConsoleUserGID, num);
165 CFRelease(num);
166
167 ok = SCDynamicStoreSetValue(store, key, dict);
168
169 done :
170
171 if (dict) CFRelease(dict);
172 if (key) CFRelease(key);
173 if (tempSession) CFRelease(store);
174 return ok;
175 }