1 | /*␊ |
2 | * property_list.cpp␊ |
3 | *␊ |
4 | * Created by Rekursor on 1/17/10.␊ |
5 | *␊ |
6 | */␊ |
7 | ␊ |
8 | #import "ChameleonPropertyList.h"␊ |
9 | #include "file_util.h"␊ |
10 | #include <string.h>␊ |
11 | #include <sys/stat.h>␊ |
12 | ␊ |
13 | ␊ |
14 | //--------------------------------------------------------------------------␊ |
15 | // Simple parsing and splitting args function, does handle env. vars ␊ |
16 | //--------------------------------------------------------------------------␊ |
17 | static void parseArgs(const char * args, const char* argv[], int argvlen)␊ |
18 | {␊ |
19 | ␉// Create a list of args fron the space delimited args parameters␊ |
20 | ␉int n=0;␊ |
21 | ␉if (args && *args) ␊ |
22 | ␉{␊ |
23 | ␉␉bool skipSpaces = true;␊ |
24 | ␉␉bool prevCharwasDelim =false;␊ |
25 | ␉␉char * buf = strdup(args);␊ |
26 | ␉␉char lastPriorityDelim=' ';␊ |
27 | ␉␉␊ |
28 | ␉␉printf("Parsing <%s>\n", args);␊ |
29 | ␉␉␊ |
30 | ␉␉int k=0;␊ |
31 | ␉␉for (int i=0; i< strlen(args); i++)␊ |
32 | ␉␉{␊ |
33 | ␉␉␉switch (args[i]) ␊ |
34 | ␉␉␉{␊ |
35 | ␉␉␉␉case ' ': case '\t':␊ |
36 | ␉␉␉␉␉if(!skipSpaces) {␊ |
37 | ␉␉␉␉␉␉buf[k++]=args[i];␊ |
38 | ␉␉␉␉␉}␊ |
39 | ␉␉␉␉␉else {␊ |
40 | ␉␉␉␉␉␉prevCharwasDelim = true; // // keep a track of delim to further split tokens␊ |
41 | ␉␉␉␉␉}␊ |
42 | ␉␉␉␉␉break;␊ |
43 | ␉␉␉␉case '\0':␊ |
44 | ␉␉␉␉case '\'': ␊ |
45 | ␉␉␉␉case '"': ␊ |
46 | ␉␉␉␉␉if (!skipSpaces &&lastPriorityDelim!=args[i])␊ |
47 | ␉␉␉␉␉{ // consider the imbricated delim as a simple char␊ |
48 | ␉␉␉␉␉␉buf[k++]=args[i];␊ |
49 | ␉␉␉␉␉␉break;␊ |
50 | ␉␉␉␉␉}␊ |
51 | ␉␉␉␉␉␊ |
52 | ␉␉␉␉␉if (!skipSpaces) // end of priority substr␊ |
53 | ␉␉␉␉␉{␊ |
54 | ␉␉␉␉␉␉skipSpaces = true;␊ |
55 | ␉␉␉␉␉␉lastPriorityDelim=' ';␊ |
56 | ␉␉␉␉␉␉buf[k]='\0'; ␊ |
57 | ␉␉␉␉␉␉argv[n++]=strdup(buf); // copy the priority substr␊ |
58 | ␉␉␉␉␉␉k=0; // reset the buf index for next token␊ |
59 | ␉␉␉␉␉␉buf[0]='\0';␊ |
60 | ␉␉␉␉␉}␊ |
61 | ␉␉␉␉␉else␊ |
62 | ␉␉␉␉␉{ // start of priority substr␊ |
63 | ␉␉␉␉␉␉skipSpaces=false;␊ |
64 | ␉␉␉␉␉␉lastPriorityDelim=args[i];␊ |
65 | ␉␉␉␉␉␉if(k>0) // previous string to store first␊ |
66 | ␉␉␉␉␉␉{␊ |
67 | ␉␉␉␉␉␉␉buf[k++]='\0'; // finish the substr␊ |
68 | ␉␉␉␉␉␉␉argv[n++]=strdup(buf); // copy the substr␊ |
69 | ␉␉␉␉␉␉␉k=0; // rest the index for next token␊ |
70 | ␉␉␉␉␉␉␉buf[0]='\0';␊ |
71 | ␉␉␉␉␉␉}␊ |
72 | ␉␉␉␉␉␉//if (args[i] !='\0') buf[k++] = args[i]; // start the new priority substr␊ |
73 | ␉␉␉␉␉}␊ |
74 | ␉␉␉␉␉break;␊ |
75 | ␉␉␉␉default: // non delimiters chars␊ |
76 | ␉␉␉␉␉if(skipSpaces && prevCharwasDelim)␊ |
77 | ␉␉␉␉␉{␊ |
78 | ␉␉␉␉␉␉if(k>0) // a previous substr was before the space␊ |
79 | ␉␉␉␉␉␉{ // store previous token␊ |
80 | ␉␉␉␉␉␉␉buf[k++]='\0'; // finish the substr␊ |
81 | ␉␉␉␉␉␉␉argv[n++]=strdup(buf); // copy the substr␊ |
82 | ␉␉␉␉␉␉␉k=0; // rest the index for next token␊ |
83 | ␉␉␉␉␉␉␉buf[0]='\0';␊ |
84 | ␉␉␉␉␉␉}␊ |
85 | ␉␉␉␉␉}␊ |
86 | ␉␉␉␉␉prevCharwasDelim = false;␊ |
87 | ␉␉␉␉␉buf[k++]=args[i];␊ |
88 | ␉␉␉␉␉break;␊ |
89 | ␉␉␉}␊ |
90 | ␉␉}␊ |
91 | ␉␉␊ |
92 | ␉␉if(*buf) ␊ |
93 | ␉␉{␊ |
94 | ␉␉␉buf[k]='\0';␊ |
95 | ␉␉␉argv[n++]=strdup(buf); // copy the last buffer␉␊ |
96 | ␉␉}␊ |
97 | ␉␉free((void*) buf);␊ |
98 | ␉}␊ |
99 | ␉argv[n]=NULL;␊ |
100 | }␊ |
101 | ␊ |
102 | //--------------------------------------------------------------------------␊ |
103 | // Execute a priviledge shell command ␊ |
104 | //--------------------------------------------------------------------------␊ |
105 | bool executePrivilegedCmd(AuthorizationRef auth, ␊ |
106 | ␉␉␉␉␉␉␉␉ const char* pathToTool, ␊ |
107 | ␉␉␉␉␉␉␉␉ const char* args, ␊ |
108 | ␉␉␉␉␉␉␉␉ AuthorizationFlags flags)␊ |
109 | {␊ |
110 | ␉const char * argv[16];␊ |
111 | ␊ |
112 | ␉parseArgs(args, argv, sizeof(argv)/sizeof(const char*));␊ |
113 | ␊ |
114 | ␉// Execute Cmd␊ |
115 | ␉OSStatus status = AuthorizationExecuteWithPrivileges(auth, pathToTool,flags, (char**)argv, NULL);␊ |
116 | ␉␊ |
117 | ␉␊ |
118 | ␉// Cleanup mem␊ |
119 | ␉for (int i=0; argv[i]; i++) free ((void*) argv[i]);␊ |
120 | ␊ |
121 | return status ? false : true;␊ |
122 | }␊ |
123 | ␊ |
124 | //--------------------------------------------------------------------------␊ |
125 | // Execute priviledged chmod for accessing system files␊ |
126 | //--------------------------------------------------------------------------␊ |
127 | bool PropertyList::chmodFile(const char * path, const char * chmodMask,␊ |
128 | ␉␉␉␉␉␉␉ AuthorizationRef auth, AuthorizationFlags flags)␊ |
129 | {␊ |
130 | ␉if (!path || !*path) return false;␊ |
131 | ␉␊ |
132 | ␉if (auth)␊ |
133 | ␉{ // give write temporary write rights to the file␊ |
134 | ␉␉std::string args = chmodMask;␊ |
135 | ␉␉args += ' ';␊ |
136 | ␉␉args += path;␊ |
137 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␉␊ |
138 | ␉␉␉return false;␊ |
139 | ␉}␊ |
140 | ␉return true;␊ |
141 | ␉␊ |
142 | }␊ |
143 | ␊ |
144 | //--------------------------------------------------------------------------␊ |
145 | // Write a property list from a file␊ |
146 | //--------------------------------------------------------------------------␊ |
147 | bool WritePropertyListToFile( CFPropertyListRef propertyList, CFURLRef fileURL )␊ |
148 | {␊ |
149 | ␉CFDataRef xmlData;␊ |
150 | ␉Boolean status;␊ |
151 | ␉SInt32 errorCode;␊ |
152 | ␉␊ |
153 | ␉// Convert the property list into XML data.␊ |
154 | ␉xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, propertyList );␊ |
155 | ␉␊ |
156 | ␉// Write the XML data to the file.␊ |
157 | ␉status = CFURLWriteDataAndPropertiesToResource (␊ |
158 | ␉␉␉␉␉␉␉␉␉␉␉␉␉fileURL, // URL to use␊ |
159 | ␉␉␉␉␉␉␉␉␉␉␉␉␉xmlData, // data to write␊ |
160 | ␉␉␉␉␉␉␉␉␉␉␉␉␉NULL,␊ |
161 | ␉␉␉␉␉␉␉␉␉␉␉␉␉&errorCode);␊ |
162 | ␉CFRelease(xmlData);␊ |
163 | ␉␊ |
164 | ␉return status ? true: false;␊ |
165 | }␊ |
166 | ␊ |
167 | //--------------------------------------------------------------------------␊ |
168 | // Read a property list from a file␊ |
169 | //--------------------------------------------------------------------------␊ |
170 | static CFPropertyListRef CreatePropertyListFromFile( CFURLRef fileURL, ␊ |
171 | ␉␉␉␉␉␉␉␉␉␉␉␉␉const std::string& filePath,␊ |
172 | ␉␉␉␉␉␉␉␉␉␉␉␉␉CFStringRef * errorString, ␊ |
173 | ␉␉␉␉␉␉␉␉␉␉␉␉␉bool createIfNotExist) {␊ |
174 | ␉CFPropertyListRef propertyList;␊ |
175 | ␉CFDataRef resourceData;␊ |
176 | ␉Boolean status;␊ |
177 | ␉SInt32 errorCode;␊ |
178 | ␉// ␊ |
179 | ␉if (!fileExists(filePath))␊ |
180 | ␉{␊ |
181 | ␉␉if (createIfNotExist)␊ |
182 | ␉␉{␊ |
183 | ␉␉␉CFMutableDictionaryRef dict = ␊ |
184 | ␉␉␉ CFDictionaryCreateMutable( kCFAllocatorDefault, 0,␊ |
185 | ␉␉␉␉␉␉␉␉␉␉ &kCFTypeDictionaryKeyCallBacks,␊ |
186 | ␉␉␉␉␉␉␉␉␉␉ &kCFTypeDictionaryValueCallBacks );␊ |
187 | ␉␉␉WritePropertyListToFile(dict, fileURL);␊ |
188 | ␉␉}␊ |
189 | ␉␉␊ |
190 | ␉}␊ |
191 | ␉// Read the XML file.␊ |
192 | ␉status = CFURLCreateDataAndPropertiesFromResource(␊ |
193 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ kCFAllocatorDefault,␊ |
194 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ fileURL,␊ |
195 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ &resourceData, // place to put file data␊ |
196 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ NULL,␊ |
197 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ NULL,␊ |
198 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ &errorCode);␊ |
199 | ␉if (!status) return NULL;␊ |
200 | ␉␊ |
201 | ␉// Reconstitute the dictionary using the XML data.␊ |
202 | ␉propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,␊ |
203 | ␉␉␉␉␉␉␉␉␉␉␉␉ resourceData,␊ |
204 | ␉␉␉␉␉␉␉␉␉␉␉␉ kCFPropertyListMutableContainersAndLeaves,␊ |
205 | ␉␉␉␉␉␉␉␉␉␉␉␉ errorString);␊ |
206 | ␊ |
207 | ␉␉␉␉␉␉␊ |
208 | ␉CFRelease( resourceData );␊ |
209 | ␉return propertyList;␊ |
210 | }␊ |
211 | ␊ |
212 | //--------------------------------------------------------------------------␊ |
213 | // Always return a valid static const string, ␊ |
214 | // if the CFSTR is not valid, then we return ""␊ |
215 | //--------------------------------------------------------------------------␊ |
216 | static const char * CfsToCs(CFStringRef inRef)␊ |
217 | {␊ |
218 | ␉static char buffer[512]="";␊ |
219 | ␉if(CFStringGetFileSystemRepresentation (inRef, buffer, sizeof(buffer)))␊ |
220 | ␉ return buffer;␊ |
221 | ␉return "";␊ |
222 | }␊ |
223 | ␉␊ |
224 | //--------------------------------------------------------------------------␊ |
225 | // Destruct a PropertyList Instance␊ |
226 | //--------------------------------------------------------------------------␊ |
227 | PropertyList::~PropertyList()␊ |
228 | {␊ |
229 | ␉if (_CFURLRef) CFRelease(_CFURLRef);␊ |
230 | ␉if (_proplistRef) CFRelease(_proplistRef);␊ |
231 | ␊ |
232 | }␊ |
233 | ␊ |
234 | //--------------------------------------------------------------------------␊ |
235 | // Open a property list from a path name with auth␊ |
236 | //--------------------------------------------------------------------------␊ |
237 | bool PropertyList::open(const char * path, bool createIfNotExist, AuthorizationRef auth, AuthorizationFlags flags)␊ |
238 | {␊ |
239 | ␉bool ret = false;␊ |
240 | ␉␊ |
241 | ␉if (!path || !*path) return false;␊ |
242 | ␉struct stat st;␊ |
243 | ␉if(stat(path,&st) != 0) return false; // file does not exist;␊ |
244 | ␊ |
245 | ␉_propFilePath = path;␊ |
246 | ␊ |
247 | ␉CFStringRef cfPath = CFStringCreateWithCString (kCFAllocatorDefault, path, kCFStringEncodingUTF8);␊ |
248 | ␉CFURLRef inURL = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,␊ |
249 | ␉␉␉␉␉␉␉␉␉␉␉cfPath, ␊ |
250 | ␉␉␉␉␉␉␉␉␉␉␉kCFURLPOSIXPathStyle, // interpret as POSIX path␊ |
251 | ␉␉␉␉␉␉␉␉␉␉␉false ); // is it a directory?␊ |
252 | ␉CFRelease(cfPath);␊ |
253 | ␉if (_CFURLRef) CFRelease(_CFURLRef);␊ |
254 | ␉_CFURLRef = inURL;␊ |
255 | ␉␊ |
256 | ␉if (_CFURLRef) ␊ |
257 | ␉{␊ |
258 | ␉␉CFRetain(_CFURLRef);␊ |
259 | ␊ |
260 | ␉␉// give write temporary write rights to the file␊ |
261 | ␉␉if (!chmodFile(path,"0777", auth, flags)) return false;␊ |
262 | ␉␉␊ |
263 | ␉␉_proplistRef = CreatePropertyListFromFile(inURL, _propFilePath, &_errorString, createIfNotExist);␊ |
264 | ␉␉␊ |
265 | ␉␉// restore rights␊ |
266 | ␉␉ret= chmodFile(path,"0644", auth, flags);␊ |
267 | ␊ |
268 | ␉␉if (_proplistRef) {␊ |
269 | ␉␉␉CFRetain(_proplistRef);␊ |
270 | ␉␉␉ret= true;␊ |
271 | ␉␉}␊ |
272 | ␉}␊ |
273 | ␉␊ |
274 | ␉return ret;␊ |
275 | }␊ |
276 | ␊ |
277 | //--------------------------------------------------------------------------␊ |
278 | // Save the current property list␊ |
279 | //--------------------------------------------------------------------------␊ |
280 | bool PropertyList::save(AuthorizationRef auth, AuthorizationFlags flags)␊ |
281 | {␊ |
282 | ␉bool ret=false;␊ |
283 | ␉if (auth)␊ |
284 | ␉{ // give write temporary write rights to the file␊ |
285 | ␉␉std::string args = " 0777 "+ _propFilePath;␊ |
286 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
287 | ␉␉␉return false;␊ |
288 | ␉}␊ |
289 | ␉if (_proplistRef && _CFURLRef) ␊ |
290 | ␉␉ret= WritePropertyListToFile(_proplistRef,_CFURLRef);␊ |
291 | ␉if (auth)␊ |
292 | ␉{ // give write temporary write rights to the file␊ |
293 | ␉␉std::string args = " 0644 "+ _propFilePath;␊ |
294 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
295 | ␉␉␉return false;␊ |
296 | ␉}␊ |
297 | ␉return ret;␊ |
298 | }␊ |
299 | ␊ |
300 | //--------------------------------------------------------------------------␊ |
301 | // Get a string value from a property list key, ␊ |
302 | // value returned is not owned by caller, copy it necessary.␊ |
303 | //--------------------------------------------------------------------------␊ |
304 | const char * PropertyList::getStringForKey(const char *key)␊ |
305 | {␊ |
306 | ␉if (!_proplistRef) ␉return NULL;␊ |
307 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
308 | ␉CFStringRef myString;␊ |
309 | ␉␊ |
310 | ␉if (_proplistRef && CFDictionaryGetValueIfPresent((CFMutableDictionaryRef)_proplistRef, ␊ |
311 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ cfKey, (const void **) &myString))␊ |
312 | ␉{␊ |
313 | ␉␉CFRelease(cfKey);␊ |
314 | ␉␉return CfsToCs(myString);␊ |
315 | ␉}␊ |
316 | ␉␊ |
317 | ␉if(cfKey) CFRelease(cfKey);␊ |
318 | ␉return NULL;␊ |
319 | }␊ |
320 | ␊ |
321 | //--------------------------------------------------------------------------␊ |
322 | // Replace or add a string value from a property list key, ␊ |
323 | // value returned is not owned by caller, copy it necessary.␊ |
324 | //--------------------------------------------------------------------------␊ |
325 | bool PropertyList::setStringForKey(const char *key, const char * value)␊ |
326 | {␊ |
327 | ␉if (!_proplistRef) ␉return false;␊ |
328 | ␉bool ret = false;␊ |
329 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
330 | ␉CFStringRef myString = CFStringCreateWithCString (kCFAllocatorDefault, value, kCFStringEncodingUTF8);␊ |
331 | ␉CFMutableDictionaryRef myDict = (CFMutableDictionaryRef) _proplistRef;␊ |
332 | ␉␊ |
333 | ␉if (myDict) ␊ |
334 | ␉{␊ |
335 | ␉␉CFDictionarySetValue(myDict, cfKey, myString);␊ |
336 | ␉␉ret=true;␉␊ |
337 | ␉} ␊ |
338 | ␉if(cfKey) CFRelease(cfKey);␊ |
339 | ␉//if(myString) CFRelease(myString);␊ |
340 | ␉return ret;␊ |
341 | }␊ |
342 | ␊ |
343 | //--------------------------------------------------------------------------␊ |
344 | // Delete a key and its value from the dictionary␊ |
345 | //--------------------------------------------------------------------------␊ |
346 | bool PropertyList::removeKeyAndValue(const char *key)␊ |
347 | {␊ |
348 | ␉if (!_proplistRef) ␉return false;␊ |
349 | ␉bool ret = false;␊ |
350 | ␉␊ |
351 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
352 | ␉␊ |
353 | ␉if (_proplistRef) {␊ |
354 | ␉␉CFDictionaryRemoveValue((CFMutableDictionaryRef) _proplistRef, cfKey);␊ |
355 | ␉␉ret=true;␉␊ |
356 | ␉} ␊ |
357 | ␉if(cfKey) CFRelease(cfKey);␊ |
358 | ␉return ret;␊ |
359 | }␊ |
360 | ␊ |
361 | |