1 | /*
|
---|
2 | * A utility to read a Windows NT/2K/XP/2K3 registry file, using
|
---|
3 | * Gerald Carter''s regfio interface.
|
---|
4 | *
|
---|
5 | * Copyright (C) 2005 Timothy D. Morgan
|
---|
6 | * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com
|
---|
7 | *
|
---|
8 | * This program is free software; you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation; version 2 of the License.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | *
|
---|
21 | * $Id: reglookup.c 56 2005-09-05 01:54:18Z tim $
|
---|
22 | */
|
---|
23 |
|
---|
24 |
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <strings.h>
|
---|
29 | #include <time.h>
|
---|
30 | #include "../include/regfio.h"
|
---|
31 | #include "../include/void_stack.h"
|
---|
32 |
|
---|
33 | /* Globals, influenced by command line parameters */
|
---|
34 | bool print_verbose = false;
|
---|
35 | bool print_security = false;
|
---|
36 | bool print_header = true;
|
---|
37 | bool path_filter_enabled = false;
|
---|
38 | bool type_filter_enabled = false;
|
---|
39 | char* path_filter = NULL;
|
---|
40 | int type_filter;
|
---|
41 | char* registry_file = NULL;
|
---|
42 |
|
---|
43 | /* Other globals */
|
---|
44 | const char* special_chars = ",\"\\";
|
---|
45 |
|
---|
46 | void bailOut(int code, char* message)
|
---|
47 | {
|
---|
48 | fprintf(stderr, message);
|
---|
49 | exit(code);
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /* Returns a newly malloc()ed string which contains original buffer,
|
---|
54 | * except for non-printable or special characters are quoted in hex
|
---|
55 | * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted
|
---|
56 | * character. A null terminator is added, as only ascii, not binary,
|
---|
57 | * is returned.
|
---|
58 | */
|
---|
59 | static char* quote_buffer(const unsigned char* str,
|
---|
60 | unsigned int len, const char* special)
|
---|
61 | {
|
---|
62 | unsigned int i;
|
---|
63 | unsigned int num_written=0;
|
---|
64 | unsigned int out_len = sizeof(char)*len+1;
|
---|
65 | char* ret_val = malloc(out_len);
|
---|
66 |
|
---|
67 | if(ret_val == NULL)
|
---|
68 | return NULL;
|
---|
69 |
|
---|
70 | for(i=0; i<len; i++)
|
---|
71 | {
|
---|
72 | if(str[i] < 32 || str[i] > 126 || strchr(special, str[i]) != NULL)
|
---|
73 | {
|
---|
74 | out_len += 3;
|
---|
75 | /* XXX: may not be the most efficient way of getting enough memory. */
|
---|
76 | ret_val = realloc(ret_val, out_len);
|
---|
77 | if(ret_val == NULL)
|
---|
78 | break;
|
---|
79 | num_written += snprintf(ret_val+num_written, (out_len)-num_written,
|
---|
80 | "\\x%.2X", str[i]);
|
---|
81 | }
|
---|
82 | else
|
---|
83 | ret_val[num_written++] = str[i];
|
---|
84 | }
|
---|
85 | ret_val[num_written] = '\0';
|
---|
86 |
|
---|
87 | return ret_val;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | /* Returns a newly malloc()ed string which contains original string,
|
---|
92 | * except for non-printable or special characters are quoted in hex
|
---|
93 | * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted
|
---|
94 | * character.
|
---|
95 | */
|
---|
96 | static char* quote_string(const char* str, const char* special)
|
---|
97 | {
|
---|
98 | unsigned int len;
|
---|
99 |
|
---|
100 | if(str == NULL)
|
---|
101 | return NULL;
|
---|
102 |
|
---|
103 | len = strlen(str);
|
---|
104 | return quote_buffer((const unsigned char*)str, len, special);
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Convert from UniCode to Ascii ... Does not take into account other lang
|
---|
110 | * Restrict by ascii_max if > 0
|
---|
111 | */
|
---|
112 | static int uni_to_ascii(unsigned char *uni, unsigned char *ascii,
|
---|
113 | int ascii_max, int uni_max)
|
---|
114 | {
|
---|
115 | int i = 0;
|
---|
116 |
|
---|
117 | while (i < ascii_max && (uni[i*2] || uni[i*2+1]))
|
---|
118 | {
|
---|
119 | if (uni_max > 0 && (i*2) >= uni_max) break;
|
---|
120 | ascii[i] = uni[i*2];
|
---|
121 | i++;
|
---|
122 | }
|
---|
123 | ascii[i] = '\0';
|
---|
124 |
|
---|
125 | return i;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Convert a data value to a string for display
|
---|
131 | */
|
---|
132 | static unsigned char* data_to_ascii(unsigned char *datap, int len, int type)
|
---|
133 | {
|
---|
134 | unsigned char *asciip;
|
---|
135 | unsigned int i;
|
---|
136 | unsigned short num_nulls;
|
---|
137 | unsigned char* ascii;
|
---|
138 | unsigned char* cur_str;
|
---|
139 | unsigned char* cur_ascii;
|
---|
140 | char* cur_quoted;
|
---|
141 | unsigned int cur_str_len;
|
---|
142 | unsigned int ascii_max, cur_str_max;
|
---|
143 | unsigned int str_rem, cur_str_rem, alen;
|
---|
144 |
|
---|
145 | switch (type)
|
---|
146 | {
|
---|
147 | case REG_SZ:
|
---|
148 | ascii_max = sizeof(char)*len;
|
---|
149 | ascii = malloc(ascii_max+4);
|
---|
150 | if(ascii == NULL)
|
---|
151 | return NULL;
|
---|
152 |
|
---|
153 | /* XXX: This has to be fixed. It has to be UNICODE */
|
---|
154 | uni_to_ascii(datap, ascii, len, ascii_max);
|
---|
155 | cur_quoted = quote_string((char*)ascii, special_chars);
|
---|
156 | free(ascii);
|
---|
157 | return (unsigned char*)cur_quoted;
|
---|
158 | break;
|
---|
159 |
|
---|
160 | case REG_EXPAND_SZ:
|
---|
161 | ascii_max = sizeof(char)*len;
|
---|
162 | ascii = malloc(ascii_max+2);
|
---|
163 | if(ascii == NULL)
|
---|
164 | return NULL;
|
---|
165 |
|
---|
166 | uni_to_ascii(datap, ascii, len, ascii_max);
|
---|
167 | cur_quoted = quote_string((char*)ascii, special_chars);
|
---|
168 | free(ascii);
|
---|
169 | return (unsigned char*)cur_quoted;
|
---|
170 | break;
|
---|
171 |
|
---|
172 | case REG_DWORD:
|
---|
173 | ascii_max = sizeof(char)*10;
|
---|
174 | ascii = malloc(ascii_max+1);
|
---|
175 | if(ascii == NULL)
|
---|
176 | return NULL;
|
---|
177 |
|
---|
178 | if (*(int *)datap == 0)
|
---|
179 | snprintf((char*)ascii, ascii_max, "0");
|
---|
180 | else
|
---|
181 | snprintf((char*)ascii, ascii_max, "0x%x", *(int *)datap);
|
---|
182 | return ascii;
|
---|
183 | break;
|
---|
184 |
|
---|
185 | /* XXX: this MULTI_SZ parser is pretty inefficient. Should be
|
---|
186 | * redone with fewer malloc calls and better string concatenation.
|
---|
187 | */
|
---|
188 | case REG_MULTI_SZ:
|
---|
189 | ascii_max = sizeof(char)*len*4;
|
---|
190 | cur_str_max = sizeof(char)*len+1;
|
---|
191 | cur_str = malloc(cur_str_max);
|
---|
192 | cur_ascii = malloc(cur_str_max);
|
---|
193 | ascii = malloc(ascii_max+4);
|
---|
194 | if(ascii == NULL || cur_str == NULL || cur_ascii == NULL)
|
---|
195 | return NULL;
|
---|
196 |
|
---|
197 | /* Reads until it reaches 4 consecutive NULLs,
|
---|
198 | * which is two nulls in unicode, or until it reaches len, or until we
|
---|
199 | * run out of buffer. The latter should never happen, but we shouldn't
|
---|
200 | * trust our file to have the right lengths/delimiters.
|
---|
201 | */
|
---|
202 | asciip = ascii;
|
---|
203 | num_nulls = 0;
|
---|
204 | str_rem = ascii_max;
|
---|
205 | cur_str_rem = cur_str_max;
|
---|
206 | cur_str_len = 0;
|
---|
207 |
|
---|
208 | for(i=0; (i < len) && str_rem > 0; i++)
|
---|
209 | {
|
---|
210 | *(cur_str+cur_str_len) = *(datap+i);
|
---|
211 | if(*(cur_str+cur_str_len) == 0)
|
---|
212 | num_nulls++;
|
---|
213 | else
|
---|
214 | num_nulls = 0;
|
---|
215 | cur_str_len++;
|
---|
216 |
|
---|
217 | if(num_nulls == 2)
|
---|
218 | {
|
---|
219 | uni_to_ascii(cur_str, cur_ascii, cur_str_max, 0);
|
---|
220 | cur_quoted = quote_string((char*)cur_ascii, ",|\"\\");
|
---|
221 | alen = snprintf((char*)asciip, str_rem, "%s", cur_quoted);
|
---|
222 | asciip += alen;
|
---|
223 | str_rem -= alen;
|
---|
224 | free(cur_quoted);
|
---|
225 |
|
---|
226 | if(*(datap+i+1) == 0 && *(datap+i+2) == 0)
|
---|
227 | break;
|
---|
228 | else
|
---|
229 | {
|
---|
230 | alen = snprintf((char*)asciip, str_rem, "%c", '|');
|
---|
231 | asciip += alen;
|
---|
232 | str_rem -= alen;
|
---|
233 | memset(cur_str, 0, cur_str_max);
|
---|
234 | cur_str_len = 0;
|
---|
235 | num_nulls = 0;
|
---|
236 | /* To eliminate leading nulls in subsequent strings. */
|
---|
237 | i++;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 | *asciip = 0;
|
---|
242 | free(cur_str);
|
---|
243 | free(cur_ascii);
|
---|
244 | return ascii;
|
---|
245 | break;
|
---|
246 |
|
---|
247 | /* XXX: Dont know what to do with these yet, just print as binary... */
|
---|
248 | case REG_RESOURCE_LIST:
|
---|
249 | case REG_FULL_RESOURCE_DESCRIPTOR:
|
---|
250 | case REG_RESOURCE_REQUIREMENTS_LIST:
|
---|
251 |
|
---|
252 | case REG_BINARY:
|
---|
253 | return (unsigned char*)quote_buffer(datap, len, special_chars);
|
---|
254 | break;
|
---|
255 |
|
---|
256 | default:
|
---|
257 | return NULL;
|
---|
258 | break;
|
---|
259 | }
|
---|
260 |
|
---|
261 | return NULL;
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | void_stack* path2Stack(const char* s)
|
---|
266 | {
|
---|
267 | void_stack* ret_val;
|
---|
268 | void_stack* rev_ret = void_stack_new(1024);
|
---|
269 | const char* cur = s;
|
---|
270 | char* next = NULL;
|
---|
271 | char* copy;
|
---|
272 |
|
---|
273 | if (rev_ret == NULL)
|
---|
274 | return NULL;
|
---|
275 | if (s == NULL)
|
---|
276 | return rev_ret;
|
---|
277 |
|
---|
278 | while((next = strchr(cur, '/')) != NULL)
|
---|
279 | {
|
---|
280 | if ((next-cur) > 0)
|
---|
281 | {
|
---|
282 | copy = (char*)malloc((next-cur+1)*sizeof(char));
|
---|
283 | if(copy == NULL)
|
---|
284 | bailOut(2, "ERROR: Memory allocation problem.\n");
|
---|
285 |
|
---|
286 | memcpy(copy, cur, next-cur);
|
---|
287 | copy[next-cur] = '\0';
|
---|
288 | void_stack_push(rev_ret, copy);
|
---|
289 | }
|
---|
290 | cur = next+1;
|
---|
291 | }
|
---|
292 | if(strlen(cur) > 0)
|
---|
293 | {
|
---|
294 | copy = strdup(cur);
|
---|
295 | void_stack_push(rev_ret, copy);
|
---|
296 | }
|
---|
297 |
|
---|
298 | ret_val = void_stack_copy_reverse(rev_ret);
|
---|
299 | void_stack_destroy(rev_ret);
|
---|
300 |
|
---|
301 | return ret_val;
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | char* stack2Path(void_stack* nk_stack)
|
---|
306 | {
|
---|
307 | const REGF_NK_REC* cur;
|
---|
308 | uint32 buf_left = 127;
|
---|
309 | uint32 buf_len = buf_left+1;
|
---|
310 | uint32 name_len = 0;
|
---|
311 | uint32 grow_amt;
|
---|
312 | char* buf;
|
---|
313 | char* new_buf;
|
---|
314 | void_stack_iterator* iter;
|
---|
315 |
|
---|
316 | buf = (char*)malloc((buf_len)*sizeof(char));
|
---|
317 | if (buf == NULL)
|
---|
318 | return NULL;
|
---|
319 | buf[0] = '\0';
|
---|
320 |
|
---|
321 | iter = void_stack_iterator_new(nk_stack);
|
---|
322 | if (iter == NULL)
|
---|
323 | {
|
---|
324 | free(buf);
|
---|
325 | return NULL;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /* skip root element */
|
---|
329 | cur = void_stack_iterator_next(iter);
|
---|
330 |
|
---|
331 | while((cur = void_stack_iterator_next(iter)) != NULL)
|
---|
332 | {
|
---|
333 | buf[buf_len-buf_left-1] = '/';
|
---|
334 | buf_left -= 1;
|
---|
335 | name_len = strlen(cur->keyname);
|
---|
336 | if(name_len+1 > buf_left)
|
---|
337 | {
|
---|
338 | grow_amt = (uint32)(buf_len/2);
|
---|
339 | buf_len += name_len+1+grow_amt-buf_left;
|
---|
340 | if((new_buf = realloc(buf, buf_len)) == NULL)
|
---|
341 | {
|
---|
342 | free(buf);
|
---|
343 | free(iter);
|
---|
344 | return NULL;
|
---|
345 | }
|
---|
346 | buf = new_buf;
|
---|
347 | buf_left = grow_amt + name_len + 1;
|
---|
348 | }
|
---|
349 | strncpy(buf+(buf_len-buf_left-1), cur->keyname, name_len);
|
---|
350 | buf_left -= name_len;
|
---|
351 | buf[buf_len-buf_left-1] = '\0';
|
---|
352 | }
|
---|
353 |
|
---|
354 | return buf;
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | void printValue(REGF_VK_REC* vk, char* prefix)
|
---|
359 | {
|
---|
360 | uint32 size;
|
---|
361 | uint8 tmp_buf[4];
|
---|
362 | unsigned char* quoted_value;
|
---|
363 | char* quoted_prefix;
|
---|
364 | char* quoted_name;
|
---|
365 |
|
---|
366 | /* Thanks Microsoft for making this process so straight-forward!!! */
|
---|
367 | size = (vk->data_size & ~VK_DATA_IN_OFFSET);
|
---|
368 | if(vk->data_size & VK_DATA_IN_OFFSET)
|
---|
369 | {
|
---|
370 | tmp_buf[0] = (uint8)((vk->data_off >> 3) & 0xFF);
|
---|
371 | tmp_buf[1] = (uint8)((vk->data_off >> 2) & 0xFF);
|
---|
372 | tmp_buf[2] = (uint8)((vk->data_off >> 1) & 0xFF);
|
---|
373 | tmp_buf[3] = (uint8)(vk->data_off & 0xFF);
|
---|
374 | if(size > 4)
|
---|
375 | size = 4;
|
---|
376 | quoted_value = data_to_ascii(tmp_buf, 4, vk->type);
|
---|
377 | }
|
---|
378 | else
|
---|
379 | {
|
---|
380 | /* XXX: This is a safety hack. No data fields have yet been found
|
---|
381 | * larger, but length limits are probably better got from fields
|
---|
382 | * in the registry itself, within reason.
|
---|
383 | */
|
---|
384 | if(size > 16384)
|
---|
385 | {
|
---|
386 | fprintf(stderr, "WARNING: key size %d larger than "
|
---|
387 | "16384, truncating...\n", size);
|
---|
388 | size = 16384;
|
---|
389 | }
|
---|
390 | quoted_value = data_to_ascii(vk->data, vk->data_size, vk->type);
|
---|
391 | }
|
---|
392 |
|
---|
393 | /* XXX: Sometimes value names can be NULL in registry. Need to
|
---|
394 | * figure out why and when, and generate the appropriate output
|
---|
395 | * for that condition.
|
---|
396 | */
|
---|
397 | quoted_prefix = quote_string(prefix, special_chars);
|
---|
398 | quoted_name = quote_string(vk->valuename, special_chars);
|
---|
399 |
|
---|
400 | if(print_security)
|
---|
401 | printf("%s/%s,%s,%s,,,,,\n", quoted_prefix, quoted_name,
|
---|
402 | regfio_type_val2str(vk->type), quoted_value);
|
---|
403 | else
|
---|
404 | printf("%s/%s,%s,%s,\n", quoted_prefix, quoted_name,
|
---|
405 | regfio_type_val2str(vk->type), quoted_value);
|
---|
406 |
|
---|
407 | if(quoted_value != NULL)
|
---|
408 | free(quoted_value);
|
---|
409 | if(quoted_prefix != NULL)
|
---|
410 | free(quoted_prefix);
|
---|
411 | if(quoted_name != NULL)
|
---|
412 | free(quoted_name);
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | void printValueList(REGF_NK_REC* nk, char* prefix)
|
---|
417 | {
|
---|
418 | uint32 i;
|
---|
419 |
|
---|
420 | for(i=0; i < nk->num_values; i++)
|
---|
421 | if(!type_filter_enabled || (nk->values[i].type == type_filter))
|
---|
422 | printValue(&nk->values[i], prefix);
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | void printKey(REGF_NK_REC* k, char* full_path)
|
---|
427 | {
|
---|
428 | static char empty_str[1] = "";
|
---|
429 | char* owner = NULL;
|
---|
430 | char* group = NULL;
|
---|
431 | char* sacl = NULL;
|
---|
432 | char* dacl = NULL;
|
---|
433 | char mtime[20];
|
---|
434 | time_t tmp_time[1];
|
---|
435 | struct tm* tmp_time_s = NULL;
|
---|
436 |
|
---|
437 | *tmp_time = nt_time_to_unix(&k->mtime);
|
---|
438 | tmp_time_s = gmtime(tmp_time);
|
---|
439 | strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
|
---|
440 |
|
---|
441 | if(print_security)
|
---|
442 | {
|
---|
443 | owner = regfio_get_owner(k->sec_desc->sec_desc);
|
---|
444 | group = regfio_get_group(k->sec_desc->sec_desc);
|
---|
445 | sacl = regfio_get_sacl(k->sec_desc->sec_desc);
|
---|
446 | dacl = regfio_get_dacl(k->sec_desc->sec_desc);
|
---|
447 | if(owner == NULL)
|
---|
448 | owner = empty_str;
|
---|
449 | if(group == NULL)
|
---|
450 | group = empty_str;
|
---|
451 | if(sacl == NULL)
|
---|
452 | sacl = empty_str;
|
---|
453 | if(dacl == NULL)
|
---|
454 | dacl = empty_str;
|
---|
455 |
|
---|
456 | printf("%s,KEY,,%s,%s,%s,%s,%s\n", full_path, mtime,
|
---|
457 | owner, group, sacl, dacl);
|
---|
458 |
|
---|
459 | if(owner != empty_str)
|
---|
460 | free(owner);
|
---|
461 | if(group != empty_str)
|
---|
462 | free(group);
|
---|
463 | if(sacl != empty_str)
|
---|
464 | free(sacl);
|
---|
465 | if(dacl != empty_str)
|
---|
466 | free(dacl);
|
---|
467 | }
|
---|
468 | else
|
---|
469 | printf("%s,KEY,,%s\n", full_path, mtime);
|
---|
470 | }
|
---|
471 |
|
---|
472 |
|
---|
473 | /* XXX: this function is awful. Needs to be re-designed. */
|
---|
474 | void printKeyTree(REGF_FILE* f, void_stack* nk_stack, char* prefix)
|
---|
475 | {
|
---|
476 | REGF_NK_REC* cur = NULL;
|
---|
477 | REGF_NK_REC* sub = NULL;
|
---|
478 | char* path = NULL;
|
---|
479 | char* val_path = NULL;
|
---|
480 | int key_type = regfio_type_str2val("KEY");
|
---|
481 |
|
---|
482 | if((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
|
---|
483 | {
|
---|
484 | cur->subkey_index = 0;
|
---|
485 | path = stack2Path(nk_stack);
|
---|
486 |
|
---|
487 | if(print_verbose)
|
---|
488 | {
|
---|
489 | if(prefix[0] == '\0')
|
---|
490 | fprintf(stderr, "VERBOSE: Printing key tree under path: /\n");
|
---|
491 | else
|
---|
492 | fprintf(stderr, "VERBOSE: Printing key tree under path: %s\n",
|
---|
493 | prefix);
|
---|
494 | }
|
---|
495 |
|
---|
496 | val_path = (char*)malloc(strlen(prefix)+strlen(path)+1);
|
---|
497 | sprintf(val_path, "%s%s", prefix, path);
|
---|
498 | if(val_path[0] == '\0')
|
---|
499 | {
|
---|
500 | val_path[0] = '/';
|
---|
501 | val_path[1] = '\0';
|
---|
502 | }
|
---|
503 |
|
---|
504 | if(!type_filter_enabled || (key_type == type_filter))
|
---|
505 | printKey(cur, val_path);
|
---|
506 | if(!type_filter_enabled || (key_type != type_filter))
|
---|
507 | printValueList(cur, val_path);
|
---|
508 | if(val_path != NULL)
|
---|
509 | free(val_path);
|
---|
510 | while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
|
---|
511 | {
|
---|
512 | if((sub = regfio_fetch_subkey(f, cur)) != NULL)
|
---|
513 | {
|
---|
514 | sub->subkey_index = 0;
|
---|
515 | void_stack_push(nk_stack, sub);
|
---|
516 | path = stack2Path(nk_stack);
|
---|
517 | if(path != NULL)
|
---|
518 | {
|
---|
519 | val_path = (char*)malloc(strlen(prefix)+strlen(path)+1);
|
---|
520 | sprintf(val_path, "%s%s", prefix, path);
|
---|
521 | if(!type_filter_enabled || (key_type == type_filter))
|
---|
522 | printKey(sub, val_path);
|
---|
523 | if(!type_filter_enabled || (key_type != type_filter))
|
---|
524 | printValueList(sub, val_path);
|
---|
525 | if(val_path != NULL)
|
---|
526 | free(val_path);
|
---|
527 | }
|
---|
528 | }
|
---|
529 | else
|
---|
530 | {
|
---|
531 | cur = void_stack_pop(nk_stack);
|
---|
532 | /* XXX: This is just a shallow free. Need to write deep free
|
---|
533 | * routines to replace the Samba code for this.
|
---|
534 | */
|
---|
535 | if(cur != NULL)
|
---|
536 | free(cur);
|
---|
537 | }
|
---|
538 | }
|
---|
539 | }
|
---|
540 | if(print_verbose)
|
---|
541 | fprintf(stderr, "VERBOSE: Finished printing key tree.\n");
|
---|
542 | }
|
---|
543 |
|
---|
544 |
|
---|
545 | /*
|
---|
546 | * Returns 0 if path was found.
|
---|
547 | * Returns 1 if path was not found.
|
---|
548 | * Returns less than 0 on other error.
|
---|
549 | */
|
---|
550 | int retrievePath(REGF_FILE* f, void_stack* nk_stack,
|
---|
551 | void_stack* path_stack)
|
---|
552 | {
|
---|
553 | REGF_NK_REC* sub = NULL;
|
---|
554 | REGF_NK_REC* cur = NULL;
|
---|
555 | void_stack* sub_nk_stack;
|
---|
556 | char* prefix;
|
---|
557 | uint32 prefix_len;
|
---|
558 | char* cur_str = NULL;
|
---|
559 | char* path = NULL;
|
---|
560 | bool found_cur = true;
|
---|
561 | uint32 i;
|
---|
562 | uint16 path_depth;
|
---|
563 | if(path_stack == NULL)
|
---|
564 | return -1;
|
---|
565 |
|
---|
566 | path_depth = void_stack_size(path_stack);
|
---|
567 | if(path_depth < 1)
|
---|
568 | return -2;
|
---|
569 |
|
---|
570 | if(void_stack_size(nk_stack) < 1)
|
---|
571 | return -3;
|
---|
572 | cur = (REGF_NK_REC*)void_stack_cur(nk_stack);
|
---|
573 |
|
---|
574 | if(print_verbose)
|
---|
575 | fprintf(stderr, "VERBOSE: Beginning retrieval of specified path: %s\n",
|
---|
576 | path_filter);
|
---|
577 |
|
---|
578 | while(void_stack_size(path_stack) > 1)
|
---|
579 | {
|
---|
580 | /* Search key records only */
|
---|
581 | cur_str = (char*)void_stack_pop(path_stack);
|
---|
582 |
|
---|
583 | found_cur = false;
|
---|
584 | while(!found_cur &&
|
---|
585 | (sub = regfio_fetch_subkey(f, cur)) != NULL)
|
---|
586 | {
|
---|
587 | if(strcasecmp(sub->keyname, cur_str) == 0)
|
---|
588 | {
|
---|
589 | cur = sub;
|
---|
590 | void_stack_push(nk_stack, sub);
|
---|
591 | found_cur = true;
|
---|
592 | }
|
---|
593 | }
|
---|
594 | if(print_verbose && !found_cur)
|
---|
595 | fprintf(stderr, "VERBOSE: Could not find KEY '%s' in specified path.\n",
|
---|
596 | cur_str);
|
---|
597 |
|
---|
598 | free(cur_str);
|
---|
599 | if(!found_cur)
|
---|
600 | return 1;
|
---|
601 | }
|
---|
602 |
|
---|
603 | /* Last round, search value and key records */
|
---|
604 | cur_str = (char*)void_stack_pop(path_stack);
|
---|
605 |
|
---|
606 | if(print_verbose)
|
---|
607 | fprintf(stderr, "VERBOSE: Searching values for last component"
|
---|
608 | " of specified path.\n");
|
---|
609 |
|
---|
610 | for(i=0; (i < cur->num_values); i++)
|
---|
611 | {
|
---|
612 | /* XXX: Not sure when/why this can be NULL, but it's happened. */
|
---|
613 | if(sub->values[i].valuename != NULL
|
---|
614 | && strcasecmp(sub->values[i].valuename, cur_str) == 0)
|
---|
615 | {
|
---|
616 | path = stack2Path(nk_stack);
|
---|
617 |
|
---|
618 | if(print_verbose)
|
---|
619 | fprintf(stderr, "VERBOSE: Found final path element as value.\n");
|
---|
620 |
|
---|
621 | if(!type_filter_enabled || (sub->values[i].type == type_filter))
|
---|
622 | printValue(&sub->values[i], path);
|
---|
623 | if(path != NULL)
|
---|
624 | free(path);
|
---|
625 |
|
---|
626 | return 0;
|
---|
627 | }
|
---|
628 | }
|
---|
629 |
|
---|
630 | if(print_verbose)
|
---|
631 | fprintf(stderr, "VERBOSE: Searching keys for last component"
|
---|
632 | " of specified path.\n");
|
---|
633 |
|
---|
634 | while((sub = regfio_fetch_subkey(f, cur)) != NULL)
|
---|
635 | {
|
---|
636 | if(strcasecmp(sub->keyname, cur_str) == 0)
|
---|
637 | {
|
---|
638 | sub_nk_stack = void_stack_new(1024);
|
---|
639 | void_stack_push(sub_nk_stack, sub);
|
---|
640 | prefix = stack2Path(nk_stack);
|
---|
641 | prefix_len = strlen(prefix);
|
---|
642 | prefix = realloc(prefix, prefix_len+strlen(sub->keyname)+2);
|
---|
643 | if(prefix == NULL)
|
---|
644 | return -1;
|
---|
645 | strcat(prefix, "/");
|
---|
646 | strcat(prefix, sub->keyname);
|
---|
647 |
|
---|
648 | if(print_verbose)
|
---|
649 | fprintf(stderr, "VERBOSE: Found final path element as key.\n");
|
---|
650 |
|
---|
651 | printKeyTree(f, sub_nk_stack, prefix);
|
---|
652 |
|
---|
653 | return 0;
|
---|
654 | }
|
---|
655 | }
|
---|
656 |
|
---|
657 | if(print_verbose)
|
---|
658 | fprintf(stderr, "VERBOSE: Could not find last element of path.\n");
|
---|
659 |
|
---|
660 | return 1;
|
---|
661 | }
|
---|
662 |
|
---|
663 |
|
---|
664 | static void usage(void)
|
---|
665 | {
|
---|
666 | fprintf(stderr, "Usage: readreg [-v] [-s]"
|
---|
667 | " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]"
|
---|
668 | " <REGISTRY_FILE>\n");
|
---|
669 | /* XXX: replace version string with Subversion property? */
|
---|
670 | fprintf(stderr, "Version: 0.2.1\n");
|
---|
671 | fprintf(stderr, "Options:\n");
|
---|
672 | fprintf(stderr, "\t-v\t sets verbose mode.\n");
|
---|
673 | fprintf(stderr, "\t-h\t enables header row. (default)\n");
|
---|
674 | fprintf(stderr, "\t-H\t disables header row.\n");
|
---|
675 | fprintf(stderr, "\t-s\t enables security descriptor output.\n");
|
---|
676 | fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n");
|
---|
677 | fprintf(stderr, "\t-p\t restrict output to elements below this path.\n");
|
---|
678 | fprintf(stderr, "\t-t\t restrict results to this specific data type.\n");
|
---|
679 | fprintf(stderr, "\n");
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | int main(int argc, char** argv)
|
---|
684 | {
|
---|
685 | void_stack* nk_stack;
|
---|
686 | void_stack* path_stack;
|
---|
687 | REGF_FILE* f;
|
---|
688 | REGF_NK_REC* root;
|
---|
689 | int retr_path_ret;
|
---|
690 | uint32 argi, arge;
|
---|
691 |
|
---|
692 | /* Process command line arguments */
|
---|
693 | if(argc < 2)
|
---|
694 | {
|
---|
695 | usage();
|
---|
696 | bailOut(1, "ERROR: Requires at least one argument.\n");
|
---|
697 | }
|
---|
698 |
|
---|
699 | arge = argc-1;
|
---|
700 | for(argi = 1; argi < arge; argi++)
|
---|
701 | {
|
---|
702 | if (strcmp("-p", argv[argi]) == 0)
|
---|
703 | {
|
---|
704 | if(++argi >= arge)
|
---|
705 | {
|
---|
706 | usage();
|
---|
707 | bailOut(1, "ERROR: '-p' option requires parameter.\n");
|
---|
708 | }
|
---|
709 | if((path_filter = strdup(argv[argi])) == NULL)
|
---|
710 | bailOut(2, "ERROR: Memory allocation problem.\n");
|
---|
711 |
|
---|
712 | path_filter_enabled = true;
|
---|
713 | }
|
---|
714 | else if (strcmp("-t", argv[argi]) == 0)
|
---|
715 | {
|
---|
716 | if(++argi >= arge)
|
---|
717 | {
|
---|
718 | usage();
|
---|
719 | bailOut(1, "ERROR: '-t' option requires parameter.\n");
|
---|
720 | }
|
---|
721 | if((type_filter = regfio_type_str2val(argv[argi])) == 0)
|
---|
722 | {
|
---|
723 | fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]);
|
---|
724 | bailOut(1, "");
|
---|
725 | }
|
---|
726 |
|
---|
727 | type_filter_enabled = true;
|
---|
728 | }
|
---|
729 | else if (strcmp("-h", argv[argi]) == 0)
|
---|
730 | print_header = true;
|
---|
731 | else if (strcmp("-H", argv[argi]) == 0)
|
---|
732 | print_header = false;
|
---|
733 | else if (strcmp("-s", argv[argi]) == 0)
|
---|
734 | print_security = true;
|
---|
735 | else if (strcmp("-S", argv[argi]) == 0)
|
---|
736 | print_security = false;
|
---|
737 | else if (strcmp("-v", argv[argi]) == 0)
|
---|
738 | print_verbose = true;
|
---|
739 | else
|
---|
740 | {
|
---|
741 | usage();
|
---|
742 | fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
|
---|
743 | bailOut(1, "");
|
---|
744 | }
|
---|
745 | }
|
---|
746 | if((registry_file = strdup(argv[argi])) == NULL)
|
---|
747 | bailOut(2, "ERROR: Memory allocation problem.\n");
|
---|
748 |
|
---|
749 | f = regfio_open(registry_file);
|
---|
750 | if(f == NULL)
|
---|
751 | {
|
---|
752 | fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
|
---|
753 | bailOut(3, "");
|
---|
754 | }
|
---|
755 |
|
---|
756 | root = regfio_rootkey(f);
|
---|
757 | nk_stack = void_stack_new(1024);
|
---|
758 |
|
---|
759 | if(void_stack_push(nk_stack, root))
|
---|
760 | {
|
---|
761 | if(print_header)
|
---|
762 | {
|
---|
763 | if(print_security)
|
---|
764 | printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL\n");
|
---|
765 | else
|
---|
766 | printf("PATH,TYPE,VALUE,MTIME\n");
|
---|
767 | }
|
---|
768 |
|
---|
769 | path_stack = path2Stack(path_filter);
|
---|
770 | if(void_stack_size(path_stack) < 1)
|
---|
771 | printKeyTree(f, nk_stack, "");
|
---|
772 | else
|
---|
773 | {
|
---|
774 | retr_path_ret = retrievePath(f, nk_stack, path_stack);
|
---|
775 | if(retr_path_ret == 1)
|
---|
776 | fprintf(stderr, "WARNING: specified path not found.\n");
|
---|
777 | else if(retr_path_ret != 0)
|
---|
778 | bailOut(4, "ERROR:\n");
|
---|
779 | }
|
---|
780 | }
|
---|
781 | else
|
---|
782 | bailOut(2, "ERROR: Memory allocation problem.\n");
|
---|
783 |
|
---|
784 | void_stack_destroy_deep(nk_stack);
|
---|
785 | regfio_close(f);
|
---|
786 |
|
---|
787 | return 0;
|
---|
788 | }
|
---|