]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCP.c
2f8d071e49ef8e42c293ba222b29db2fc442ed1e
[apple/configd.git] / SystemConfiguration.fproj / SCP.c
1 /*
2 * Copyright (c) 2000, 2001, 2003-2005, 2007 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->st_mtimespec = statBuf->st_mtimespec;
56 sig->st_size = statBuf->st_size;
57 return signature;
58 }
59
60
61 __private_extern__ char *
62 __SCPreferencesPath(CFAllocatorRef allocator,
63 CFStringRef prefsID,
64 Boolean useNewPrefs)
65 {
66 CFStringRef path = NULL;
67 char *pathStr;
68
69 if (prefsID == NULL) {
70 /* default preference ID */
71 path = CFStringCreateWithFormat(allocator,
72 NULL,
73 CFSTR("%@/%@"),
74 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
75 useNewPrefs ? PREFS_DEFAULT_CONFIG : PREFS_DEFAULT_CONFIG_OLD);
76 } else if (CFStringHasPrefix(prefsID, CFSTR("/"))) {
77 /* if absolute path */
78 path = CFStringCreateCopy(allocator, prefsID);
79 } else {
80 /* relative path */
81 path = CFStringCreateWithFormat(allocator,
82 NULL,
83 CFSTR("%@/%@"),
84 useNewPrefs ? PREFS_DEFAULT_DIR : PREFS_DEFAULT_DIR_OLD,
85 prefsID);
86 if (useNewPrefs && CFStringHasSuffix(path, CFSTR(".xml"))) {
87 CFMutableStringRef newPath;
88
89 newPath = CFStringCreateMutableCopy(allocator, 0, path);
90 CFStringReplace(newPath,
91 CFRangeMake(CFStringGetLength(newPath)-4, 4),
92 CFSTR(".plist"));
93 CFRelease(path);
94 path = newPath;
95 }
96 }
97
98 /*
99 * convert CFStringRef path to C-string path
100 */
101 pathStr = _SC_cfstring_to_cstring(path, NULL, 0, kCFStringEncodingASCII);
102 if (pathStr == NULL) {
103 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("could not convert path to C string"));
104 }
105
106 CFRelease(path);
107 return pathStr;
108 }
109
110
111 CFDataRef
112 SCPreferencesGetSignature(SCPreferencesRef prefs)
113 {
114 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
115
116 if (prefs == NULL) {
117 /* sorry, you must provide a session */
118 _SCErrorSet(kSCStatusNoPrefsSession);
119 return NULL;
120 }
121
122 __SCPreferencesAccess(prefs);
123
124 return prefsPrivate->signature;
125 }
126
127
128 __private_extern__ CFStringRef
129 _SCPNotificationKey(CFAllocatorRef allocator,
130 CFStringRef prefsID,
131 int keyType)
132 {
133 CFStringRef keyStr;
134 char *path;
135 CFStringRef pathStr;
136 CFStringRef storeKey;
137
138 switch (keyType) {
139 case kSCPreferencesKeyCommit :
140 keyStr = CFSTR("commit");
141 break;
142 case kSCPreferencesKeyApply :
143 keyStr = CFSTR("apply");
144 break;
145 default :
146 return NULL;
147 }
148
149 path = __SCPreferencesPath(allocator, prefsID, TRUE);
150 if (path == NULL) {
151 return NULL;
152 }
153
154 pathStr = CFStringCreateWithCStringNoCopy(allocator,
155 path,
156 kCFStringEncodingASCII,
157 kCFAllocatorNull);
158
159 storeKey = CFStringCreateWithFormat(allocator,
160 NULL,
161 CFSTR("%@%@:%@"),
162 kSCDynamicStoreDomainPrefs,
163 keyStr,
164 pathStr);
165
166 CFRelease(pathStr);
167 CFAllocatorDeallocate(NULL, path);
168 return storeKey;
169 }
170
171
172 CFStringRef
173 SCDynamicStoreKeyCreatePreferences(CFAllocatorRef allocator,
174 CFStringRef prefsID,
175 SCPreferencesKeyType keyType)
176 {
177 return _SCPNotificationKey(allocator, prefsID, keyType);
178 }