]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
configd-293.4.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCP.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2005, 2007, 2008 Apple 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 * November 9, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <SystemConfiguration/SystemConfiguration.h>
35 #include <SystemConfiguration/SCPrivate.h>
36 #include "SCPreferencesInternal.h"
37
38 #include <fcntl.h>
39 #include <pwd.h>
40 #include <unistd.h>
41 #include <sys/errno.h>
42 #include <sys/param.h>
43
44 __private_extern__ CFDataRef
45 __SCPSignatureFromStatbuf(const struct stat *statBuf)
46 {
47 CFMutableDataRef signature;
48 SCPSignatureDataRef sig;
49
50 signature = CFDataCreateMutable(NULL, sizeof(SCPSignatureData));
51 CFDataSetLength(signature, sizeof(SCPSignatureData));
52 sig = (SCPSignatureDataRef)CFDataGetBytePtr(signature);
53 sig->st_dev = statBuf->st_dev;
54 sig->st_ino = statBuf->st_ino;
55 sig->tv_sec = statBuf->st_mtimespec.tv_sec;
56 sig->tv_nsec = statBuf->st_mtimespec.tv_nsec;
57 sig->st_size = statBuf->st_size;
58 return signature;
59 }
60
61
62 __private_extern__ char *
63 __SCPreferencesPath(CFAllocatorRef allocator,
64 CFStringRef prefsID,
65 Boolean useNewPrefs)
66 {
67 CFStringRef path = NULL;
68 char *pathStr;
69
70 if (prefsID == NULL) {
71 /* default preference ID */
72 path = CFStringCreateWithFormat(allocator,
73 NULL,
74 CFSTR("%@/%@"),
75 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
76 useNewPrefs ? PREFS_DEFAULT_CONFIG : PREFS_DEFAULT_CONFIG_OLD);
77 } else if (CFStringHasPrefix(prefsID, CFSTR("/"))) {
78 /* if absolute path */
79 path = CFStringCreateCopy(allocator, prefsID);
80 } else {
81 /* relative path */
82 path = CFStringCreateWithFormat(allocator,
83 NULL,
84 CFSTR("%@/%@"),
85 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
86 prefsID);
87 if (useNewPrefs && CFStringHasSuffix(path, CFSTR(".xml"))) {
88 CFMutableStringRef newPath;
89
90 newPath = CFStringCreateMutableCopy(allocator, 0, path);
91 CFStringReplace(newPath,
92 CFRangeMake(CFStringGetLength(newPath)-4, 4),
93 CFSTR(".plist"));
94 CFRelease(path);
95 path = newPath;
96 }
97 }
98
99 /*
100 * convert CFStringRef path to C-string path
101 */
102 pathStr = _SC_cfstring_to_cstring(path, NULL, 0, kCFStringEncodingASCII);
103 if (pathStr == NULL) {
104 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("could not convert path to C string"));
105 }
106
107 CFRelease(path);
108 return pathStr;
109 }
110
111
112 CFDataRef
113 SCPreferencesGetSignature(SCPreferencesRef prefs)
114 {
115 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
116
117 if (prefs == NULL) {
118 /* sorry, you must provide a session */
119 _SCErrorSet(kSCStatusNoPrefsSession);
120 return NULL;
121 }
122
123 __SCPreferencesAccess(prefs);
124
125 return prefsPrivate->signature;
126 }
127
128
129 __private_extern__ CFStringRef
130 _SCPNotificationKey(CFAllocatorRef allocator,
131 CFStringRef prefsID,
132 int keyType)
133 {
134 CFStringRef keyStr;
135 char *path;
136 CFStringRef pathStr;
137 CFStringRef storeKey;
138
139 switch (keyType) {
140 case kSCPreferencesKeyCommit :
141 keyStr = CFSTR("commit");
142 break;
143 case kSCPreferencesKeyApply :
144 keyStr = CFSTR("apply");
145 break;
146 default :
147 return NULL;
148 }
149
150 path = __SCPreferencesPath(allocator, prefsID, TRUE);
151 if (path == NULL) {
152 return NULL;
153 }
154
155 pathStr = CFStringCreateWithCStringNoCopy(allocator,
156 path,
157 kCFStringEncodingASCII,
158 kCFAllocatorNull);
159
160 storeKey = CFStringCreateWithFormat(allocator,
161 NULL,
162 CFSTR("%@%@:%@"),
163 kSCDynamicStoreDomainPrefs,
164 keyStr,
165 pathStr);
166
167 CFRelease(pathStr);
168 CFAllocatorDeallocate(NULL, path);
169 return storeKey;
170 }
171
172
173 CFStringRef
174 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
175 CFStringRef prefsID,
176 SCPreferencesKeyType keyType)
177 {
178 return _SCPNotificationKey(allocator, prefsID, keyType);
179 }