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