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[512]="";␊ |
223 | ␉if(CFStringGetFileSystemRepresentation (inRef, buffer, sizeof(buffer)))␊ |
224 | ␉ return buffer;␊ |
225 | ␉return "";␊ |
226 | }␊ |
227 | ␉␊ |
228 | //--------------------------------------------------------------------------␊ |
229 | // Destruct a PropertyList Instance␊ |
230 | //--------------------------------------------------------------------------␊ |
231 | PropertyList::~PropertyList()␊ |
232 | {␊ |
233 | ␉if (_CFURLRef) CFRelease(_CFURLRef);␊ |
234 | ␉if (_proplistRef) CFRelease(_proplistRef);␊ |
235 | ␊ |
236 | }␊ |
237 | ␊ |
238 | //--------------------------------------------------------------------------␊ |
239 | // Open a property list from a path name with auth␊ |
240 | //--------------------------------------------------------------------------␊ |
241 | bool PropertyList::open(const char * path, bool createIfNotExist, AuthorizationRef auth, AuthorizationFlags flags)␊ |
242 | {␊ |
243 | ␉bool ret = false;␊ |
244 | ␉␊ |
245 | ␉if (!path || !*path) return false;␊ |
246 | ␉struct stat st;␊ |
247 | ␉if(stat(path,&st) != 0) return false; // file does not exist;␊ |
248 | ␊ |
249 | ␉_propFilePath = path;␊ |
250 | ␊ |
251 | ␉CFStringRef cfPath = CFStringCreateWithCString (kCFAllocatorDefault, path, kCFStringEncodingUTF8);␊ |
252 | ␉CFURLRef inURL = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,␊ |
253 | ␉␉␉␉␉␉␉␉␉␉␉cfPath, ␊ |
254 | ␉␉␉␉␉␉␉␉␉␉␉kCFURLPOSIXPathStyle, // interpret as POSIX path␊ |
255 | ␉␉␉␉␉␉␉␉␉␉␉false ); // is it a directory?␊ |
256 | ␉CFRelease(cfPath);␊ |
257 | ␉if (_CFURLRef) CFRelease(_CFURLRef);␊ |
258 | ␉_CFURLRef = inURL;␊ |
259 | ␉␊ |
260 | ␉if (_CFURLRef) ␊ |
261 | ␉{␊ |
262 | ␉␉CFRetain(_CFURLRef);␊ |
263 | ␊ |
264 | ␉␉// give write temporary write rights to the file␊ |
265 | ␉␉if (!chmodFile(path,"0777", auth, flags)) return false;␊ |
266 | ␉␉␊ |
267 | ␉␉_proplistRef = CreatePropertyListFromFile(inURL, _propFilePath, &_errorString, createIfNotExist);␊ |
268 | ␉␉␊ |
269 | ␉␉// restore rights␊ |
270 | ␉␉ret= chmodFile(path,"0644", auth, flags);␊ |
271 | ␊ |
272 | ␉␉if (_proplistRef) {␊ |
273 | ␉␉␉CFRetain(_proplistRef);␊ |
274 | ␉␉␉ret= true;␊ |
275 | ␉␉}␊ |
276 | ␉}␊ |
277 | ␉␊ |
278 | ␉return ret;␊ |
279 | }␊ |
280 | ␊ |
281 | //--------------------------------------------------------------------------␊ |
282 | // Save the current property list␊ |
283 | //--------------------------------------------------------------------------␊ |
284 | bool PropertyList::save(AuthorizationRef auth, AuthorizationFlags flags)␊ |
285 | {␊ |
286 | ␉bool ret=false;␊ |
287 | ␉if (auth)␊ |
288 | ␉{ // give write temporary write rights to the file␊ |
289 | ␉␉std::string args = " 0777 "+ _propFilePath;␊ |
290 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
291 | ␉␉␉return false;␊ |
292 | ␉}␊ |
293 | ␉if (_proplistRef && _CFURLRef) ␊ |
294 | ␉␉ret= WritePropertyListToFile(_proplistRef,_CFURLRef);␊ |
295 | ␉if (auth)␊ |
296 | ␉{ // give write temporary write rights to the file␊ |
297 | ␉␉std::string args = " 0644 "+ _propFilePath;␊ |
298 | ␉␉if(!executePrivilegedCmd(auth, "/bin/chmod", args.c_str(), flags))␊ |
299 | ␉␉␉return false;␊ |
300 | ␉}␊ |
301 | ␉return ret;␊ |
302 | }␊ |
303 | ␊ |
304 | //--------------------------------------------------------------------------␊ |
305 | // Get a string value from a property list key, ␊ |
306 | // value returned is not owned by caller, copy it necessary.␊ |
307 | //--------------------------------------------------------------------------␊ |
308 | const char * PropertyList::getStringForKey(const char *key)␊ |
309 | {␊ |
310 | ␉if (!_proplistRef) ␉return NULL;␊ |
311 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
312 | ␉CFStringRef myString;␊ |
313 | ␉␊ |
314 | ␉if (_proplistRef && CFDictionaryGetValueIfPresent((CFMutableDictionaryRef)_proplistRef, ␊ |
315 | ␉␉␉␉␉␉␉␉␉␉␉␉␉ cfKey, (const void **) &myString))␊ |
316 | ␉{␊ |
317 | ␉␉CFRelease(cfKey);␊ |
318 | ␉␉return CfsToCs(myString);␊ |
319 | ␉}␊ |
320 | ␉␊ |
321 | ␉if(cfKey) CFRelease(cfKey);␊ |
322 | ␉return NULL;␊ |
323 | }␊ |
324 | ␊ |
325 | //--------------------------------------------------------------------------␊ |
326 | // Replace or add a string value from a property list key, ␊ |
327 | // value returned is not owned by caller, copy it necessary.␊ |
328 | //--------------------------------------------------------------------------␊ |
329 | bool PropertyList::setStringForKey(const char *key, const char * value)␊ |
330 | {␊ |
331 | ␉if (!_proplistRef) ␉return false;␊ |
332 | ␉bool ret = false;␊ |
333 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
334 | ␉CFStringRef myString = CFStringCreateWithCString (kCFAllocatorDefault, value, kCFStringEncodingUTF8);␊ |
335 | ␉CFMutableDictionaryRef myDict = (CFMutableDictionaryRef) _proplistRef;␊ |
336 | ␉␊ |
337 | ␉if (myDict) ␊ |
338 | ␉{␊ |
339 | ␉␉CFDictionarySetValue(myDict, cfKey, myString);␊ |
340 | ␉␉ret=true;␉␊ |
341 | ␉} ␊ |
342 | ␉if(cfKey) CFRelease(cfKey);␊ |
343 | ␉//if(myString) CFRelease(myString);␊ |
344 | ␉return ret;␊ |
345 | }␊ |
346 | ␊ |
347 | //--------------------------------------------------------------------------␊ |
348 | // Delete a key and its value from the dictionary␊ |
349 | //--------------------------------------------------------------------------␊ |
350 | bool PropertyList::removeKeyAndValue(const char *key)␊ |
351 | {␊ |
352 | ␉if (!_proplistRef) ␉return false;␊ |
353 | ␉bool ret = false;␊ |
354 | ␉␊ |
355 | ␉CFStringRef cfKey = CFStringCreateWithCString (kCFAllocatorDefault, key, kCFStringEncodingUTF8);␊ |
356 | ␉␊ |
357 | ␉if (_proplistRef) {␊ |
358 | ␉␉CFDictionaryRemoveValue((CFMutableDictionaryRef) _proplistRef, cfKey);␊ |
359 | ␉␉ret=true;␉␊ |
360 | ␉} ␊ |
361 | ␉if(cfKey) CFRelease(cfKey);␊ |
362 | ␉return ret;␊ |
363 | }␊ |
364 | ␊ |
365 | |