source: trunk/src/reglookup.c @ 251

Last change on this file since 251 was 251, checked in by tim, 13 years ago

simplified NTTIME storage and conversions

  • Property svn:keywords set to Id
File size: 17.7 KB
Line 
1/*
2 * A utility to read a Windows NT and later registry files.
3 *
4 * Copyright (C) 2005-2010 Timothy D. Morgan
5 * Copyright (C) 2010 Tobias Mueller (portions of '-i' code)
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 3 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 251 2011-05-06 01:34:35Z 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 "regfi.h"
31#include "void_stack.h"
32
33/* Globals, influenced by command line parameters */
34bool print_value_mtime = false;
35bool print_verbose = false;
36bool print_security = false;
37bool print_header = true;
38bool path_filter_enabled = false;
39bool type_filter_enabled = false;
40char* path_filter = NULL;
41int type_filter;
42const char* registry_file = NULL;
43
44/* Other globals */
45REGFI_FILE* f;
46
47
48/* XXX: A hack to share some functions with reglookup-recover.c.
49 *      Should move these into a proper library at some point.
50 */
51#include "common.c"
52
53
54static bool keysEqual(const REGFI_NK* x, const REGFI_NK* y)
55{
56  return (x != NULL && y != NULL && x->offset == y->offset);
57}
58
59void printValue(REGFI_ITERATOR* iter, const REGFI_VK* vk, char* prefix)
60{
61  const REGFI_NK* cur_key;
62  const REGFI_DATA* data;
63  char* quoted_value = NULL;
64  char* quoted_name = NULL;
65  char* conv_error = NULL;
66  const char* str_type = NULL;
67  char mtime[20];
68  time_t tmp_time[1];
69  struct tm* tmp_time_s = NULL;
70
71  quoted_name = get_quoted_valuename(vk);
72  if (quoted_name == NULL)
73  { /* Value names are NULL when we're looking at the "(default)" value.
74     * Currently we just return a 0-length string to try an eliminate
75     * ambiguity with a literal "(default)" value.  The data type of a line
76     * in the output allows one to differentiate between the parent key and
77     * this value.
78     */
79    quoted_name = malloc(1*sizeof(char));
80    if(quoted_name == NULL)
81      bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Could not allocate sufficient memory.\n");
82    quoted_name[0] = '\0';
83  }
84 
85  data = regfi_fetch_data(iter->f, vk);
86
87  printMsgs(iter->f);
88  if(data != NULL)
89  {
90    quoted_value = data_to_ascii(data, &conv_error);
91    if(quoted_value == NULL)
92    {
93      if(conv_error == NULL)
94        fprintf(stderr, "WARN: Could not quote value for '%s/%s'.  "
95                "Memory allocation failure likely.\n", prefix, quoted_name);
96      else
97        fprintf(stderr, "WARN: Could not quote value for '%s/%s'.  "
98                "Returned error: %s\n", prefix, quoted_name, conv_error);
99    }
100    else if(conv_error != NULL)
101      fprintf(stderr, "WARN: While quoting value for '%s/%s', "
102              "warning returned: %s\n", prefix, quoted_name, conv_error);
103    regfi_free_record(iter->f, data);
104  }
105
106  if(print_value_mtime)
107  {
108    cur_key = regfi_iterator_cur_key(iter);
109    *tmp_time = regfi_nt2unix_time(cur_key->mtime);
110    tmp_time_s = gmtime(tmp_time);
111    strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
112    regfi_free_record(iter->f, cur_key);
113  }
114  else
115    mtime[0] = '\0';
116
117  str_type = regfi_type_val2str(vk->type);
118  if(print_security)
119  {
120    if(str_type == NULL)
121      printf("%s/%s,0x%.8X,%s,%s,,,,\n", prefix, quoted_name,
122             vk->type, quoted_value, mtime);
123    else
124      printf("%s/%s,%s,%s,%s,,,,\n", prefix, quoted_name,
125             str_type, quoted_value, mtime);
126  }
127  else
128  {
129    if(str_type == NULL)
130      printf("%s/%s,0x%.8X,%s,%s\n", prefix, quoted_name,
131             vk->type, quoted_value, mtime);
132    else
133      printf("%s/%s,%s,%s,%s\n", prefix, quoted_name,
134             str_type, quoted_value, mtime);
135  }
136
137  if(quoted_value != NULL)
138    free(quoted_value);
139  if(quoted_name != NULL)
140    free(quoted_name);
141  if(conv_error != NULL)
142    free(conv_error);
143}
144
145
146char** splitPath(const char* s)
147{
148  char** ret_val;
149  const char* cur = s;
150  char* next = NULL;
151  char* copy;
152  uint32_t ret_cur = 0;
153
154  ret_val = (char**)malloc((REGFI_MAX_DEPTH+1+1)*sizeof(char**));
155  if (ret_val == NULL)
156    return NULL;
157  ret_val[0] = NULL;
158
159  /* We return a well-formed, 0-length, path even when input is icky. */
160  if (s == NULL)
161    return ret_val;
162 
163  while((next = strchr(cur, '/')) != NULL)
164  {
165    if ((next-cur) > 0)
166    {
167      copy = (char*)malloc((next-cur+1)*sizeof(char));
168      if(copy == NULL)
169        bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Memory allocation problem.\n");
170         
171      memcpy(copy, cur, next-cur);
172      copy[next-cur] = '\0';
173      ret_val[ret_cur++] = copy;
174      if(ret_cur < (REGFI_MAX_DEPTH+1+1))
175        ret_val[ret_cur] = NULL;
176      else
177        bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: Registry maximum depth exceeded.\n");
178    }
179    cur = next+1;
180  }
181
182  /* Grab last element, if path doesn't end in '/'. */
183  if(strlen(cur) > 0)
184  {
185    copy = strdup(cur);
186    ret_val[ret_cur++] = copy;
187    if(ret_cur < (REGFI_MAX_DEPTH+1+1))
188      ret_val[ret_cur] = NULL;
189    else
190      bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: Registry maximum depth exceeded.\n");
191  }
192
193  return ret_val;
194}
195
196
197void freePath(char** path)
198{
199  uint32_t i;
200
201  if(path == NULL)
202    return;
203
204  for(i=0; path[i] != NULL; i++)
205    free(path[i]);
206
207  free(path);
208}
209
210
211/* Returns a quoted path of the current iterator's position */
212char* iter2Path(REGFI_ITERATOR* i)
213{
214  const REGFI_NK** path;
215  uint32_t k;
216  uint32_t buf_left = 127;
217  uint32_t buf_len = buf_left+1;
218  uint32_t name_len = 0;
219  uint32_t grow_amt;
220  char* buf;
221  char* new_buf;
222  char* name;
223 
224  buf = (char*)malloc((buf_len)*sizeof(char));
225  if (buf == NULL)
226    return NULL;
227  buf[0] = '\0';
228
229  path = regfi_iterator_cur_path(i);
230  if(path == NULL)
231  {
232    free(buf);
233    return NULL;
234  }
235
236  for(k=0; path[k] != NULL; k++)
237  {
238    /* skip root element's name */
239    if(k == 0)
240    {
241      buf[0] = '/';
242      buf[1] = '\0';
243    }
244    else
245    {
246      name = get_quoted_keyname(path[k]);
247
248      buf[buf_len-buf_left-1] = '/';
249      buf_left -= 1;
250      name_len = strlen(name);
251      if(name_len+1 > buf_left)
252      {
253        grow_amt = (uint32_t)(buf_len/2);
254        buf_len += name_len+1+grow_amt-buf_left;
255        if((new_buf = realloc(buf, buf_len)) == NULL)
256        {
257          regfi_free_record(i->f, path);
258          free(name);
259          free(buf);
260          return NULL;
261        }
262        buf = new_buf;
263        buf_left = grow_amt + name_len + 1;
264      }
265      strncpy(buf+(buf_len-buf_left-1), name, name_len);
266      buf_left -= name_len;
267      buf[buf_len-buf_left-1] = '\0';
268      free(name);
269    }
270  }
271
272  regfi_free_record(i->f, path);
273  return buf;
274}
275
276
277void printValueList(REGFI_ITERATOR* iter, char* prefix)
278{
279  const REGFI_VK* value;
280
281  regfi_iterator_first_value(iter);
282  while((value = regfi_iterator_cur_value(iter)) != NULL)
283  {
284    if(!type_filter_enabled || (value->type == type_filter))
285      printValue(iter, value, prefix);
286    regfi_free_record(iter->f, value);
287    regfi_iterator_next_value(iter);
288    printMsgs(iter->f);
289  }
290}
291
292
293void printKey(REGFI_ITERATOR* iter, char* full_path)
294{
295  static char empty_str[1] = "";
296  char* owner = NULL;
297  char* group = NULL;
298  char* sacl = NULL;
299  char* dacl = NULL;
300  char mtime[24];
301  char* quoted_classname;
302  const REGFI_SK* sk;
303  const REGFI_NK* key = regfi_iterator_cur_key(iter);
304  const REGFI_CLASSNAME* classname;
305
306  formatTime(key->mtime, mtime);
307
308  if(print_security && (sk=regfi_fetch_sk(iter->f, key)))
309  {
310    owner = regfi_get_owner(sk->sec_desc);
311    group = regfi_get_group(sk->sec_desc);
312    sacl = regfi_get_sacl(sk->sec_desc);
313    dacl = regfi_get_dacl(sk->sec_desc);
314    regfi_free_record(iter->f, sk);
315
316    if(owner == NULL)
317      owner = empty_str;
318    if(group == NULL)
319      group = empty_str;
320    if(sacl == NULL)
321      sacl = empty_str;
322    if(dacl == NULL)
323      dacl = empty_str;
324
325    classname = regfi_fetch_classname(iter->f, key);
326    printMsgs(iter->f);
327    if(classname != NULL)
328    {
329      if(classname->interpreted == NULL)
330      {
331        fprintf(stderr, "WARN: Could not convert class name"
332                " charset for key '%s'.  Quoting raw...\n", full_path);
333        quoted_classname = quote_buffer(classname->raw, classname->size,
334                                        key_special_chars);
335      }
336      else
337        quoted_classname = quote_string(classname->interpreted, 
338                                        key_special_chars);
339
340      if(quoted_classname == NULL)
341      {
342        fprintf(stderr, "ERROR: Could not quote classname"
343                " for key '%s' due to unknown error.\n", full_path);
344        quoted_classname = empty_str;
345      }
346    }
347    else
348      quoted_classname = empty_str;
349    regfi_free_record(iter->f, classname);
350
351    printMsgs(iter->f);
352    printf("%s,KEY,,%s,%s,%s,%s,%s,%s\n", full_path, mtime, 
353           owner, group, sacl, dacl, quoted_classname);
354
355    if(owner != empty_str)
356      free(owner);
357    if(group != empty_str)
358      free(group);
359    if(sacl != empty_str)
360      free(sacl);
361    if(dacl != empty_str)
362      free(dacl);
363    if(quoted_classname != empty_str)
364      free(quoted_classname);
365  }
366  else
367    printf("%s,KEY,,%s\n", full_path, mtime);
368
369  regfi_free_record(iter->f, key);
370}
371
372
373void printKeyTree(REGFI_ITERATOR* iter)
374{
375  const REGFI_NK* root = NULL;
376  const REGFI_NK* cur = NULL;
377  const REGFI_NK* sub = NULL;
378  char* path = NULL;
379  int key_type = regfi_type_str2val("KEY");
380  bool print_this = true;
381
382  root = regfi_iterator_cur_key(iter);
383  regfi_reference_record(iter->f, root);
384  cur = root;
385  regfi_iterator_first_subkey(iter);
386  sub = regfi_iterator_cur_subkey(iter);
387  printMsgs(iter->f);
388
389  if(root == NULL)
390    bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: root cannot be NULL.\n");
391 
392  do
393  {
394    if(print_this)
395    {
396      path = iter2Path(iter);
397      if(path == NULL)
398        bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Could not construct iterator's path.\n");
399
400      if(!type_filter_enabled || (key_type == type_filter))
401        printKey(iter, path);
402      if(!type_filter_enabled || (key_type != type_filter))
403        printValueList(iter, path);
404     
405      free(path);
406    }
407   
408    if(sub == NULL)
409    {
410      if(!keysEqual(cur, root))
411      {
412        regfi_free_record(iter->f, cur);
413        cur = NULL;
414        /* We're done with this sub-tree, going up and hitting other branches. */
415        if(!regfi_iterator_up(iter))
416        {
417          printMsgs(iter->f);
418          bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: could not traverse iterator upward.\n");
419        }
420
421        cur = regfi_iterator_cur_key(iter);
422        if(cur == NULL)
423        {
424          printMsgs(iter->f);
425          bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: unexpected NULL for key.\n");
426        }
427       
428        regfi_iterator_next_subkey(iter);
429        sub = regfi_iterator_cur_subkey(iter);
430      }
431      print_this = false;
432    }
433    else
434    { /* We have unexplored sub-keys. 
435       * Let's move down and print this first sub-tree out.
436       */
437      regfi_free_record(iter->f, cur);
438      cur = NULL;
439      if(!regfi_iterator_down(iter))
440      {
441        printMsgs(iter->f);
442        bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: could not traverse iterator downward.\n");
443      }
444
445      cur = regfi_iterator_cur_key(iter);
446      regfi_free_record(iter->f, sub);
447      regfi_iterator_first_subkey(iter);
448      sub = regfi_iterator_cur_subkey(iter);
449      print_this = true;
450    }
451    printMsgs(iter->f);
452  } while(!(keysEqual(cur, root) && (sub == NULL)));
453  if(cur != NULL)
454    regfi_free_record(iter->f, cur);
455  regfi_free_record(iter->f, root);
456
457  if(print_verbose)
458    fprintf(stderr, "INFO: Finished printing key tree.\n");
459}
460
461
462/* XXX: What if there is BOTH a value AND a key with that name??
463 *      What if there are multiple keys/values with the same name??
464 */
465/*
466 * Returns 0 if path was not found.
467 * Returns 1 if path was found as value.
468 * Returns 2 if path was found as key.
469 * Returns less than 0 on other error.
470 */
471int retrievePath(REGFI_ITERATOR* iter, char** path)
472{
473  const REGFI_VK* value;
474  char* tmp_path_joined;
475  const char** tmp_path;
476  uint32_t i;
477 
478  if(path == NULL)
479    return -1;
480
481  /* One extra for any value at the end, and one more for NULL */
482  tmp_path = (const char**)malloc(sizeof(const char**)*(REGFI_MAX_DEPTH+1+1));
483  if(tmp_path == NULL)
484    return -2;
485
486  /* Strip any potential value name at end of path */
487  for(i=0; 
488      (path[i] != NULL) && (path[i+1] != NULL) && (i < REGFI_MAX_DEPTH+1);
489      i++)
490  { tmp_path[i] = path[i]; }
491  tmp_path[i] = NULL;
492
493  if(print_verbose)
494    fprintf(stderr, "INFO: Attempting to retrieve specified path: %s\n",
495            path_filter);
496
497  /* Special check for '/' path filter */
498  if(path[0] == NULL)
499  {
500    if(print_verbose)
501      fprintf(stderr, "INFO: Found final path element as root key.\n");
502    free(tmp_path);
503    return 2;
504  }
505
506  if(!regfi_iterator_walk_path(iter, tmp_path))
507  {
508    printMsgs(iter->f);
509    free(tmp_path);
510    return 0;
511  }
512
513  if(regfi_iterator_find_value(iter, path[i]))
514  {
515    if(print_verbose)
516      fprintf(stderr, "INFO: Found final path element as value.\n");
517
518    value = regfi_iterator_cur_value(iter);
519    printMsgs(iter->f);
520    tmp_path_joined = iter2Path(iter);
521
522    if((value == NULL) || (tmp_path_joined == NULL))
523      bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Unexpected error before printValue.\n");
524
525    if(!type_filter_enabled || (value->type == type_filter))
526      printValue(iter, value, tmp_path_joined);
527
528    regfi_free_record(iter->f, value);
529    free(tmp_path);
530    free(tmp_path_joined);
531    return 1;
532  }
533  else if(regfi_iterator_find_subkey(iter, path[i]))
534  {
535    printMsgs(iter->f);
536    if(print_verbose)
537      fprintf(stderr, "INFO: Found final path element as key.\n");
538
539    if(!regfi_iterator_down(iter))
540    {
541      printMsgs(iter->f);
542      bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: Unexpected error on traversing path filter key.\n");
543    }
544
545    return 2;
546  }
547  printMsgs(iter->f);
548
549  if(print_verbose)
550    fprintf(stderr, "INFO: Could not find last element of path.\n");
551
552  return 0;
553}
554
555
556static void usage(void)
557{
558  fprintf(stderr, "Usage: reglookup [-v] [-s]"
559          " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]"
560          " <REGISTRY_FILE>\n");
561  fprintf(stderr, "Version: %s\n", regfi_version());
562  fprintf(stderr, "Options:\n");
563  fprintf(stderr, "\t-v\t sets verbose mode.\n");
564  fprintf(stderr, "\t-h\t enables header row. (default)\n");
565  fprintf(stderr, "\t-H\t disables header row.\n");
566  fprintf(stderr, "\t-s\t enables security descriptor output.\n");
567  fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n");
568  fprintf(stderr, "\t-p\t restrict output to elements below this path.\n");
569  fprintf(stderr, "\t-t\t restrict results to this specific data type.\n");
570  fprintf(stderr, "\t-i\t includes parent key modification times with child values.\n");
571  fprintf(stderr, "\n");
572}
573
574
575int main(int argc, char** argv)
576{
577  char** path = NULL;
578  REGFI_ITERATOR* iter;
579  int retr_path_ret, fd;
580  uint32_t argi, arge;
581
582  /* Process command line arguments */
583  if(argc < 2)
584  {
585    usage();
586    bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: Requires at least one argument.\n");
587  }
588 
589  arge = argc-1;
590  for(argi = 1; argi < arge; argi++)
591  {
592    if (strcmp("-p", argv[argi]) == 0)
593    {
594      if(++argi >= arge)
595      {
596        usage();
597        bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: '-p' option requires parameter.\n");
598      }
599      if((path_filter = strdup(argv[argi])) == NULL)
600        bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Memory allocation problem.\n");
601
602      path_filter_enabled = true;
603    }
604    else if (strcmp("-t", argv[argi]) == 0)
605    {
606      if(++argi >= arge)
607      {
608        usage();
609        bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: '-t' option requires parameter.\n");
610      }
611      if((type_filter = regfi_type_str2val(argv[argi])) < 0)
612      {
613        fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]);
614        bailOut(REGLOOKUP_EXIT_USAGE, "");
615      }
616      type_filter_enabled = true;
617    }
618    else if (strcmp("-h", argv[argi]) == 0)
619      print_header = true;
620    else if (strcmp("-H", argv[argi]) == 0)
621      print_header = false;
622    else if (strcmp("-s", argv[argi]) == 0)
623      print_security = true;
624    else if (strcmp("-S", argv[argi]) == 0)
625      print_security = false;
626    else if (strcmp("-v", argv[argi]) == 0)
627      print_verbose = true;
628    else if (strcmp("-i", argv[argi]) == 0)
629      print_value_mtime = true;
630    else
631    {
632      usage();
633      fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
634      bailOut(REGLOOKUP_EXIT_USAGE, "");
635    }
636  }
637  registry_file = argv[argi];
638
639  if(print_verbose)
640    regfi_log_set_mask(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR);
641
642  fd = openHive(registry_file);
643  if(fd < 0)
644  {
645    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
646    bailOut(REGLOOKUP_EXIT_NOINPUT, "");
647  }
648   
649  /* XXX: add command line option to choose output encoding */
650  f = regfi_alloc(fd, REGFI_ENCODING_ASCII);
651  if(f == NULL)
652  {
653    close(fd);
654    bailOut(REGLOOKUP_EXIT_NOINPUT, "ERROR: Failed to create REGFI_FILE structure.\n");
655  }
656
657  iter = regfi_iterator_new(f);
658  if(iter == NULL)
659  {
660    printMsgs(f);
661    bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Couldn't create registry iterator.\n");
662  }
663
664  if(print_header)
665  {
666    if(print_security)
667      printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL,CLASS\n");
668    else
669      printf("PATH,TYPE,VALUE,MTIME\n");
670  }
671
672  if(path_filter_enabled && path_filter != NULL)
673    path = splitPath(path_filter);
674
675  if(path != NULL)
676  {
677    retr_path_ret = retrievePath(iter, path);
678    printMsgs(iter->f);
679    freePath(path);
680
681    if(retr_path_ret == 0)
682      fprintf(stderr, "WARN: Specified path '%s' not found.\n", path_filter);
683    else if (retr_path_ret == 2)
684      printKeyTree(iter);
685    else if(retr_path_ret < 0)
686    {
687      fprintf(stderr, "ERROR: retrievePath() returned %d.\n", 
688              retr_path_ret);
689      bailOut(REGLOOKUP_EXIT_DATAERR,
690              "ERROR: Unknown error occurred in retrieving path.\n");
691    }
692  }
693  else
694    printKeyTree(iter);
695
696  regfi_iterator_free(iter);
697  regfi_free(f);
698  close(fd);
699
700  return 0;
701}
Note: See TracBrowser for help on using the repository browser.