]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDHostName.c
configd-135.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDHostName.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * January 8, 2001 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCValidation.h>
36 #include <SystemConfiguration/SCPrivate.h>
37
38 CFStringRef
39 SCDynamicStoreKeyCreateComputerName(CFAllocatorRef allocator)
40 {
41 return SCDynamicStoreKeyCreate(allocator,
42 CFSTR("%@/%@"),
43 kSCDynamicStoreDomainSetup,
44 kSCCompSystem);
45 }
46
47
48 CFStringRef
49 SCDynamicStoreCopyComputerName(SCDynamicStoreRef store,
50 CFStringEncoding *nameEncoding)
51 {
52 CFDictionaryRef dict;
53 CFStringRef key;
54 CFStringRef name = NULL;
55 Boolean tempSession = FALSE;
56
57 if (store == NULL) {
58 store = SCDynamicStoreCreate(NULL,
59 CFSTR("SCDynamicStoreCopyComputerName"),
60 NULL,
61 NULL);
62 if (store == NULL) {
63 return NULL;
64 }
65 tempSession = TRUE;
66 }
67
68 key = SCDynamicStoreKeyCreateComputerName(NULL);
69 dict = SCDynamicStoreCopyValue(store, key);
70 CFRelease(key);
71 if (dict == NULL) {
72 goto done;
73 }
74 if (!isA_CFDictionary(dict)) {
75 _SCErrorSet(kSCStatusNoKey);
76 goto done;
77 }
78
79 name = isA_CFString(CFDictionaryGetValue(dict, kSCPropSystemComputerName));
80 if (name == NULL) {
81 _SCErrorSet(kSCStatusNoKey);
82 goto done;
83 }
84 CFRetain(name);
85
86 if (nameEncoding != NULL) {
87 CFNumberRef num;
88
89 num = CFDictionaryGetValue(dict,
90 kSCPropSystemComputerNameEncoding);
91 if (isA_CFNumber(num)) {
92 CFNumberGetValue(num, kCFNumberIntType, nameEncoding);
93 } else {
94 *nameEncoding = CFStringGetSystemEncoding();
95 }
96 }
97
98 done :
99
100 if (tempSession) CFRelease(store);
101 if (dict != NULL) CFRelease(dict);
102 return name;
103 }
104
105
106 Boolean
107 SCPreferencesSetComputerName(SCPreferencesRef prefs,
108 CFStringRef name,
109 CFStringEncoding encoding)
110 {
111 CFDictionaryRef dict;
112 CFMutableDictionaryRef newDict;
113 CFNumberRef num;
114 Boolean ok;
115 CFStringRef path;
116
117 if (!isA_CFString(name)) {
118 _SCErrorSet(kSCStatusInvalidArgument);
119 return FALSE;
120 }
121
122 path = CFStringCreateWithFormat(NULL,
123 NULL,
124 CFSTR("/%@/%@"),
125 kSCPrefSystem,
126 kSCCompSystem);
127
128 dict = SCPreferencesPathGetValue(prefs, path);
129 if (dict != NULL) {
130 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
131 } else {
132 newDict = CFDictionaryCreateMutable(NULL,
133 0,
134 &kCFTypeDictionaryKeyCallBacks,
135 &kCFTypeDictionaryValueCallBacks);
136 }
137
138 CFDictionarySetValue(newDict, kSCPropSystemComputerName, name);
139
140 num = CFNumberCreate(NULL, kCFNumberIntType, &encoding);
141 CFDictionarySetValue(newDict, kSCPropSystemComputerNameEncoding, num);
142 CFRelease(num);
143
144 ok = SCPreferencesPathSetValue(prefs, path, newDict);
145
146 CFRelease(path);
147 CFRelease(newDict);
148
149 return ok;
150 }
151
152
153 #ifndef kSCPropNetHostName
154 #define kSCPropNetHostName CFSTR("HostName")
155 #endif
156
157
158 CFStringRef
159 SCPreferencesGetHostName(SCPreferencesRef prefs)
160 {
161 CFDictionaryRef dict;
162 CFStringRef name;
163 CFStringRef path;
164
165 path = CFStringCreateWithFormat(NULL,
166 NULL,
167 CFSTR("/%@/%@"),
168 kSCPrefSystem,
169 kSCCompSystem);
170 dict = SCPreferencesPathGetValue(prefs, path);
171 CFRelease(path);
172
173 if (!isA_CFDictionary(dict)) {
174 _SCErrorSet(kSCStatusNoKey);
175 return NULL;
176 }
177
178 name = isA_CFString(CFDictionaryGetValue(dict, kSCPropNetHostName));
179 if (name == NULL) {
180 _SCErrorSet(kSCStatusNoKey);
181 return NULL;
182 }
183
184 return name;
185 }
186
187
188 Boolean
189 SCPreferencesSetHostName(SCPreferencesRef prefs,
190 CFStringRef name)
191 {
192 CFDictionaryRef dict;
193 CFMutableDictionaryRef newDict;
194 Boolean ok;
195 CFStringRef path;
196
197 if (name != NULL) {
198 CFIndex len;
199
200 if (!isA_CFString(name)) {
201 _SCErrorSet(kSCStatusInvalidArgument);
202 return FALSE;
203 }
204
205 len = CFStringGetLength(name);
206 if (len == 0) {
207 name = NULL;
208 }
209 }
210
211 path = CFStringCreateWithFormat(NULL,
212 NULL,
213 CFSTR("/%@/%@"),
214 kSCPrefSystem,
215 kSCCompSystem);
216
217 dict = SCPreferencesPathGetValue(prefs, path);
218 if (dict != NULL) {
219 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
220 } else {
221 newDict = CFDictionaryCreateMutable(NULL,
222 0,
223 &kCFTypeDictionaryKeyCallBacks,
224 &kCFTypeDictionaryValueCallBacks);
225 }
226
227 if (name != NULL) {
228 CFDictionarySetValue(newDict, kSCPropNetHostName, name);
229 } else {
230 CFDictionaryRemoveValue(newDict, kSCPropNetHostName);
231 }
232
233 if (CFDictionaryGetCount(newDict) > 0) {
234 ok = SCPreferencesPathSetValue(prefs, path, newDict);
235 } else {
236 ok = SCPreferencesPathRemoveValue(prefs, path);
237 }
238
239 CFRelease(path);
240 CFRelease(newDict);
241
242 return ok;
243 }
244
245
246 CFStringRef
247 SCDynamicStoreKeyCreateHostNames(CFAllocatorRef allocator)
248 {
249 return SCDynamicStoreKeyCreate(allocator,
250 CFSTR("%@/%@/%@"),
251 kSCDynamicStoreDomainSetup,
252 kSCCompNetwork,
253 kSCCompHostNames);
254 }
255
256
257 CFStringRef
258 SCDynamicStoreCopyLocalHostName(SCDynamicStoreRef store)
259 {
260 CFDictionaryRef dict;
261 CFStringRef key;
262 CFStringRef name = NULL;
263 Boolean tempSession = FALSE;
264
265 if (store == NULL) {
266 store = SCDynamicStoreCreate(NULL,
267 CFSTR("SCDynamicStoreCopyLocalHostName"),
268 NULL,
269 NULL);
270 if (store == NULL) {
271 return NULL;
272 }
273 tempSession = TRUE;
274 }
275
276 key = SCDynamicStoreKeyCreateHostNames(NULL);
277 dict = SCDynamicStoreCopyValue(store, key);
278 CFRelease(key);
279 if (dict == NULL) {
280 goto done;
281 }
282 if (!isA_CFDictionary(dict)) {
283 _SCErrorSet(kSCStatusNoKey);
284 goto done;
285 }
286
287 name = isA_CFString(CFDictionaryGetValue(dict, kSCPropNetLocalHostName));
288 if (name == NULL) {
289 _SCErrorSet(kSCStatusNoKey);
290 goto done;
291 }
292 CFRetain(name);
293
294 done :
295
296 if (tempSession) CFRelease(store);
297 if (dict != NULL) CFRelease(dict);
298 return name;
299 }
300
301
302 Boolean
303 _SC_stringIsValidDNSName(const char *name)
304 {
305 int i;
306 int len = strlen(name);
307 char prev = '\0';
308 const char *scan;
309
310 if (len == 0) {
311 return FALSE;
312 }
313
314 for (scan = name, i = 0; i < len; i++, scan++) {
315 char ch = *scan;
316 char next = *(scan + 1);
317
318 if (prev == '.' || prev == '\0') {
319 if (isalnum(ch) == 0) {
320 /* a label must begin with a letter or digit */
321 return FALSE;
322 }
323 } else if (next == '\0' || next == '.') {
324 if (isalnum(ch) == 0) {
325 /* a label must end with a letter or digit */
326 return FALSE;
327 }
328 } else if (isalnum(ch) == 0) {
329 switch (ch) {
330 case '.':
331 case '-':
332 if (prev == '.' || prev == '-') {
333 /* a label cannot begin or end with a hyphen */
334 return FALSE;
335 }
336 break;
337 default:
338 /* an invalid character */
339 return FALSE;
340 break;
341 }
342 }
343 prev = ch;
344 }
345
346 return TRUE;
347 }
348
349 Boolean
350 _SC_CFStringIsValidDNSName(CFStringRef name)
351 {
352 Boolean clean = FALSE;
353 char *str = NULL;
354
355 if (!isA_CFString(name)) {
356 return FALSE;
357 }
358
359 str = _SC_cfstring_to_cstring(name, NULL, 0, kCFStringEncodingASCII);
360 if (str == NULL) {
361 return FALSE;
362 }
363
364 clean = _SC_stringIsValidDNSName(str);
365
366 if (str != NULL) CFAllocatorDeallocate(NULL, str);
367 return clean;
368 }
369
370
371 Boolean
372 SCPreferencesSetLocalHostName(SCPreferencesRef prefs,
373 CFStringRef name)
374 {
375 CFDictionaryRef dict;
376 CFMutableDictionaryRef newDict;
377 Boolean ok;
378 CFStringRef path;
379
380 if (name != NULL) {
381 CFIndex len;
382
383 if (!isA_CFString(name)) {
384 _SCErrorSet(kSCStatusInvalidArgument);
385 return FALSE;
386 }
387
388 len = CFStringGetLength(name);
389 if (len > 0) {
390 if (!_SC_CFStringIsValidDNSName(name)) {
391 _SCErrorSet(kSCStatusInvalidArgument);
392 return FALSE;
393 }
394
395 if (CFStringFindWithOptions(name, CFSTR("."), CFRangeMake(0, len), 0, NULL)) {
396 _SCErrorSet(kSCStatusInvalidArgument);
397 return FALSE;
398 }
399 } else {
400 name = NULL;
401 }
402 }
403
404 path = CFStringCreateWithFormat(NULL,
405 NULL,
406 CFSTR("/%@/%@/%@"),
407 kSCPrefSystem,
408 kSCCompNetwork,
409 kSCCompHostNames);
410
411 dict = SCPreferencesPathGetValue(prefs, path);
412 if (dict != NULL) {
413 newDict = CFDictionaryCreateMutableCopy(NULL, 0, dict);
414 } else {
415 newDict = CFDictionaryCreateMutable(NULL,
416 0,
417 &kCFTypeDictionaryKeyCallBacks,
418 &kCFTypeDictionaryValueCallBacks);
419 }
420
421 if (name != NULL) {
422 CFDictionarySetValue(newDict, kSCPropNetLocalHostName, name);
423 } else {
424 CFDictionaryRemoveValue(newDict, kSCPropNetLocalHostName);
425 }
426
427 if (CFDictionaryGetCount(newDict) > 0) {
428 ok = SCPreferencesPathSetValue(prefs, path, newDict);
429 } else {
430 ok = SCPreferencesPathRemoveValue(prefs, path);
431 }
432
433 CFRelease(path);
434 CFRelease(newDict);
435
436 return ok;
437 }