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 : will wait for execution termination␊ |
104 | // by calling wait()␊ |
105 | //--------------------------------------------------------------------------␊ |
106 | bool executePrivilegedCmd(AuthorizationRef auth, ␊ |
107 | ␉␉␉␉␉␉␉␉ const char* pathToTool, ␊ |
108 | ␉␉␉␉␉␉␉␉ const char* args, ␊ |
109 | ␉␉␉␉␉␉␉␉ AuthorizationFlags flags)␊ |
110 | {␊ |
111 | ␉const char * argv[16];␊ |
112 | ␊ |
113 | ␉parseArgs(args, argv, sizeof(argv)/sizeof(const char*));␊ |
114 | ␊ |
115 | ␉// Execute Cmd␊ |
116 | ␉OSStatus status = AuthorizationExecuteWithPrivileges(auth, pathToTool,flags, (char**)argv, NULL);␊ |
117 | ␉// Wait for the child process of AEWP to terminate:␊ |
118 | ␉int wait_status=0;␊ |
119 | ␉/* pid_t pid = */␊ |
120 | ␉wait(&wait_status);␊ |
121 | ␉// usleep(10000); // 10 ms␊ |
122 | ␉␉// Cleanup mem␊ |
123 | ␉for (int i=0; argv[i]; i++) free ((void*) argv[i]);␊ |
124 | ␊ |
125 | return status ? false : true;␊ |
126 | }␊ |
127 | ␊ |
128 | //--------------------------------------------------------------------------␊ |
129 | // Execute priviledged chmod for accessing system files␊ |
130 | //--------------------------------------------------------------------------␊ |
131 | bool PropertyList::chmodFile(const char * path, const char * chmodMask,␊ |
132 | ␉␉␉␉␉␉␉ AuthorizationRef auth, AuthorizationFlags flags)␊ |
133 | {␊ |
134 | ␉if (!path || !*path) return false;␊ |
135 | ␉␊ |
136 | ␉if (auth)␊ |
137 | ␉{ // give write temporary write rights to the file␊ |
138 | ␉␉std::string args = chmodMask;␊ |
139 | ␉␉args += ' ';␊ |
140 | ␉␉args += path;␊ |
141 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␉␊ |
142 | ␉␉␉return false;␊ |
143 | ␉}␊ |
144 | ␉return true;␊ |
145 | ␉␊ |
146 | }␊ |
147 | ␊ |
148 | //--------------------------------------------------------------------------␊ |
149 | // Write a property list from a file␊ |
150 | //--------------------------------------------------------------------------␊ |
151 | bool WritePropertyListToFile( CFPropertyListRef propertyList, CFURLRef fileURL )␊ |
152 | {␊ |
153 | ␉CFDataRef xmlData;␊ |
154 | ␉Boolean status;␊ |
155 | ␉SInt32 errorCode;␊ |
156 | ␉␊ |
157 | ␉// Convert the property list into XML data.␊ |
158 | ␉xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, propertyList );␊ |
159 | ␉␊ |
160 | ␉// Write the XML data to the file.␊ |
161 | ␉status = CFURLWriteDataAndPropertiesToResource (␊ |
162 | ␉␉␉␉␉␉␉␉␉␉␉␉␉fileURL, // URL to use␊ |
163 | ␉␉␉␉␉␉␉␉␉␉␉␉␉xmlData, // data to write␊ |
164 | ␉␉␉␉␉␉␉␉␉␉␉␉␉NULL,␊ |
165 | ␉␉␉␉␉␉␉␉␉␉␉␉␉&errorCode);␊ |
166 | ␉CFRelease(xmlData);␊ |
167 | ␉␊ |
168 | ␉return status ? true: false;␊ |
169 | }␊ |
170 | ␊ |
171 | //--------------------------------------------------------------------------␊ |
172 | // Read a property list from a file␊ |
173 | //--------------------------------------------------------------------------␊ |
174 | static CFPropertyListRef CreatePropertyListFromFile( CFURLRef fileURL, ␊ |
175 | ␉␉␉␉␉␉␉␉␉␉␉␉␉const std::string& filePath,␊ |
176 | ␉␉␉␉␉␉␉␉␉␉␉␉␉CFStringRef * errorString, ␊ |
177 | ␉␉␉␉␉␉␉␉␉␉␉␉␉bool createIfNotExist) {␊ |
178 | ␉CFPropertyListRef propertyList;␊ |
179 | ␉CFDataRef resourceData;␊ |
180 | ␉Boolean status;␊ |
181 | ␉SInt32 errorCode;␊ |
182 | ␉// ␊ |
183 | ␉if (!fileExists(filePath))␊ |
184 | ␉{␊ |
185 | ␉␉if (createIfNotExist)␊ |
186 | ␉␉{␊ |
187 | ␉␉␉CFMutableDictionaryRef dict = ␊ |
188 | ␉␉␉ CFDictionaryCreateMutable( kCFAllocatorDefault, 0,␊ |
189 | ␉␉␉␉␉␉␉␉␉␉ &kCFTypeDictionaryKeyCallBacks,␊ |
190 | ␉␉␉␉␉␉␉␉␉␉ &kCFTypeDictionaryValueCallBacks );␊ |
191 | ␉␉␉WritePropertyListToFile(dict, fileURL);␊ |
192 | ␉␉}␊ |
193 | ␉␉␊ |
194 | ␉}␊ |
195 | ␉// Read the XML file.␊ |
196 | ␉status = CFURLCreateDataAndPropertiesFromResource(␊ |
197 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ kCFAllocatorDefault,␊ |
198 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ fileURL,␊ |
199 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ &resourceData, // place to put file data␊ |
200 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ NULL,␊ |
201 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ NULL,␊ |
202 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ &errorCode);␊ |
203 | ␉if (!status) return NULL;␊ |
204 | ␉␊ |
205 | ␉// Reconstitute the dictionary using the XML data.␊ |
206 | ␉propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,␊ |
207 | ␉␉␉␉␉␉␉␉␉␉␉␉ resourceData,␊ |
208 | ␉␉␉␉␉␉␉␉␉␉␉␉ kCFPropertyListMutableContainersAndLeaves,␊ |
209 | ␉␉␉␉␉␉␉␉␉␉␉␉ errorString);␊ |
210 | ␊ |
211 | ␉␉␉␉␉␉␊ |
212 | ␉CFRelease( resourceData );␊ |
213 | ␉return propertyList;␊ |
214 | }␊ |
215 | ␊ |
216 | //--------------------------------------------------------------------------␊ |
217 | // Always return a valid static const string, ␊ |
218 | // if the CFSTR is not valid, then we return ""␊ |
219 | //--------------------------------------------------------------------------␊ |
220 | static const char * CfsToCs(CFStringRef inRef)␊ |
221 | {␊ |
222 | ␉static char *buffer=NULL;␊ |
223 | ␉CFIndex len = CFStringGetLength(inRef);␊ |
224 | ␉if (buffer) {␊ |
225 | ␉␉delete[] buffer;␉␊ |
226 | ␉}␊ |
227 | ␉buffer = new char[len+1];␊ |
228 | ␉if(CFStringGetFileSystemRepresentation (inRef, buffer, len+1))␊ |
229 | ␉␉return buffer;␊ |
230 | ␉return "";␊ |
231 | }␊ |
232 | ␉␊ |
233 | //--------------------------------------------------------------------------␊ |
234 | // Destruct a PropertyList Instance␊ |
235 | //--------------------------------------------------------------------------␊ |
236 | PropertyList::~PropertyList()␊ |
237 | {␊ |
238 | ␉if (_CFURLRef) CFRelease(_CFURLRef);␊ |
239 | ␉if (_proplistRef) CFRelease(_proplistRef);␊ |
240 | ␊ |
241 | }␊ |
242 | ␊ |
243 | //--------------------------------------------------------------------------␊ |
244 | // Open a property list from a path name with auth␊ |
245 | //--------------------------------------------------------------------------␊ |
246 | bool PropertyList::open(const char * path, bool createIfNotExist, AuthorizationRef auth, AuthorizationFlags flags)␊ |
247 | {␊ |
248 | ␉bool ret = false;␊ |
249 | ␉␊ |
250 | ␉if (!path || !*path) return false;␊ |
251 | ␉struct stat st;␊ |
252 | ␉if(stat(path,&st) != 0) return false; // file does not exist;␊ |
253 | ␊ |
254 | ␉_propFilePath = path;␊ |
255 | ␊ |
256 | ␉CFStringRef cfPath = CFStringCreateWithCString (kCFAllocatorDefault, path, kCFStringEncodingUTF8);␊ |
257 | ␉CFURLRef inURL = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,␊ |
258 | ␉␉␉␉␉␉␉␉␉␉␉cfPath, ␊ |
259 | ␉␉␉␉␉␉␉␉␉␉␉kCFURLPOSIXPathStyle, // interpret as POSIX path␊ |
260 | ␉␉␉␉␉␉␉␉␉␉␉false ); // is it a directory?␊ |
261 | ␉CFRelease(cfPath);␊ |
262 | ␉if (_CFURLRef) CFRelease(_CFURLRef);␊ |
263 | ␉_CFURLRef = inURL;␊ |
264 | ␉␊ |
265 | ␉if (_CFURLRef) ␊ |
266 | ␉{␊ |
267 | ␉␉CFRetain(_CFURLRef);␊ |
268 | ␊ |
269 | ␉␉// give write temporary write rights to the file␊ |
270 | ␉␉if (!chmodFile(path,"0777", auth, flags)) return false;␊ |
271 | ␉␉␊ |
272 | ␉␉_proplistRef = CreatePropertyListFromFile(inURL, _propFilePath, &_errorString, createIfNotExist);␊ |
273 | ␉␉␊ |
274 | ␉␉// restore rights␊ |
275 | ␉␉ret= chmodFile(path,"0644", auth, flags);␊ |
276 | ␊ |
277 | ␉␉if (_proplistRef) {␊ |
278 | ␉␉␉CFRetain(_proplistRef);␊ |
279 | ␉␉␉ret= true;␊ |
280 | ␉␉}␊ |
281 | ␉}␊ |
282 | ␉␊ |
283 | ␉return ret;␊ |
284 | }␊ |
285 | ␊ |
286 | //--------------------------------------------------------------------------␊ |
287 | // Save the current property list␊ |
288 | //--------------------------------------------------------------------------␊ |
289 | bool PropertyList::save(AuthorizationRef auth, AuthorizationFlags flags)␊ |
290 | {␊ |
291 | ␉bool ret=false;␊ |
292 | ␉if (auth)␊ |
293 | ␉{ // give write temporary write rights to the file␊ |
294 | ␉␉std::string args = " 0777 "+ _propFilePath;␊ |
295 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
296 | ␉␉␉return false;␊ |
297 | ␉}␊ |
298 | ␉if (_proplistRef && _CFURLRef) ␊ |
299 | ␉␉ret= WritePropertyListToFile(_proplistRef,_CFURLRef);␊ |
300 | ␉if (auth)␊ |
301 | ␉{ // give write temporary write rights to the file␊ |
302 | ␉␉std::string args = " 0644 "+ _propFilePath;␊ |
303 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
304 | ␉␉␉return false;␊ |
305 | ␉}␊ |
306 | ␉return ret;␊ |
307 | }␊ |
308 | ␊ |
309 | //--------------------------------------------------------------------------␊ |
310 | // Get a string value from a property list key, ␊ |
311 | // value returned is not owned by caller, copy it necessary.␊ |
312 | //--------------------------------------------------------------------------␊ |
313 | const char * PropertyList::getStringForKey(const char *key)␊ |
314 | {␊ |
315 | ␉if (!_proplistRef) ␉return NULL;␊ |
316 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
317 | ␉CFStringRef myString;␊ |
318 | ␉␊ |
319 | ␉if (_proplistRef && CFDictionaryGetValueIfPresent((CFMutableDictionaryRef)_proplistRef, ␊ |
320 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ cfKey, (const void **) &myString))␊ |
321 | ␉{␊ |
322 | ␉␉CFRelease(cfKey);␊ |
323 | ␉␉return CfsToCs(myString);␊ |
324 | ␉}␊ |
325 | ␉␊ |
326 | ␉if(cfKey) CFRelease(cfKey);␊ |
327 | ␉return NULL;␊ |
328 | }␊ |
329 | ␊ |
330 | //--------------------------------------------------------------------------␊ |
331 | // Replace or add a string value from a property list key, ␊ |
332 | // value returned is not owned by caller, copy it necessary.␊ |
333 | //--------------------------------------------------------------------------␊ |
334 | bool PropertyList::setStringForKey(const char *key, const char * value)␊ |
335 | {␊ |
336 | ␉if (!_proplistRef) ␉return false;␊ |
337 | ␉bool ret = false;␊ |
338 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
339 | ␉CFStringRef myString = CFStringCreateWithCString (kCFAllocatorDefault, value, kCFStringEncodingUTF8);␊ |
340 | ␉CFMutableDictionaryRef myDict = (CFMutableDictionaryRef) _proplistRef;␊ |
341 | ␉␊ |
342 | ␉if (myDict) ␊ |
343 | ␉{␊ |
344 | ␉␉CFDictionarySetValue(myDict, cfKey, myString);␊ |
345 | ␉␉ret=true;␉␊ |
346 | ␉} ␊ |
347 | ␉if(cfKey) CFRelease(cfKey);␊ |
348 | ␉if(myString) CFRelease(myString);␊ |
349 | ␉return ret;␊ |
350 | }␊ |
351 | ␊ |
352 | //--------------------------------------------------------------------------␊ |
353 | // Delete a key and its value from the dictionary␊ |
354 | //--------------------------------------------------------------------------␊ |
355 | bool PropertyList::removeKeyAndValue(const char *key)␊ |
356 | {␊ |
357 | ␉if (!_proplistRef) ␉return false;␊ |
358 | ␉bool ret = false;␊ |
359 | ␉␊ |
360 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
361 | ␉␊ |
362 | ␉if (_proplistRef) {␊ |
363 | ␉␉CFDictionaryRemoveValue((CFMutableDictionaryRef) _proplistRef, cfKey);␊ |
364 | ␉␉ret=true;␉␊ |
365 | ␉} ␊ |
366 | ␉if(cfKey) CFRelease(cfKey);␊ |
367 | ␉return ret;␊ |
368 | }␊ |
369 | ␊ |
370 | |