source: trunk/src/reglookup.c @ 125

Last change on this file since 125 was 125, checked in by tim, 16 years ago

added early version of class name parsing. additional work still needed.

  • Property svn:keywords set to Id
File size: 15.9 KB
Line 
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-2008 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 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 125 2008-08-16 01:21:54Z tim $
22 */
23
24
25#include <stdlib.h>
26#include <sysexits.h>
27#include <stdio.h>
28#include <string.h>
29#include <strings.h>
30#include <time.h>
31#include "../include/regfi.h"
32#include "../include/void_stack.h"
33
34/* Globals, influenced by command line parameters */
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;
42char* registry_file = NULL;
43
44/* Other globals */
45REGF_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
54void printValue(const REGF_VK_REC* vk, char* prefix)
55{
56  char* quoted_value = NULL;
57  char* quoted_name = NULL;
58  char* conv_error = NULL;
59  const char* str_type = NULL;
60  uint32 size = vk->data_size;
61
62  /* Microsoft's documentation indicates that "available memory" is
63   * the limit on value sizes.  Annoying.  We limit it to 1M which
64   * should rarely be exceeded, unless the file is corrupt or
65   * malicious. For more info, see:
66   *   http://msdn2.microsoft.com/en-us/library/ms724872.aspx
67   */
68  if(size > VK_MAX_DATA_LENGTH)
69  {
70    fprintf(stderr, "WARNING: value data size %d larger than "
71            "%d, truncating...\n", size, VK_MAX_DATA_LENGTH);
72    size = VK_MAX_DATA_LENGTH;
73  }
74 
75  quoted_name = quote_string(vk->valuename, key_special_chars);
76  if (quoted_name == NULL)
77  { /* Value names are NULL when we're looking at the "(default)" value.
78     * Currently we just return a 0-length string to try an eliminate
79     * ambiguity with a literal "(default)" value.  The data type of a line
80     * in the output allows one to differentiate between the parent key and
81     * this value.
82     */
83    quoted_name = malloc(1*sizeof(char));
84    if(quoted_name == NULL)
85      bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n");
86    quoted_name[0] = '\0';
87  }
88
89  quoted_value = data_to_ascii(vk->data, size, vk->type, &conv_error);
90  if(quoted_value == NULL)
91  {
92    if(conv_error == NULL)
93      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
94              "Memory allocation failure likely.\n", prefix, quoted_name);
95    else if(print_verbose)
96      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
97              "Returned error: %s\n", prefix, quoted_name, conv_error);
98  }
99  /* XXX: should these always be printed? */
100  else if(conv_error != NULL && print_verbose)
101    fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', "
102            "warning returned: %s\n", prefix, quoted_name, conv_error);
103
104  str_type = regfi_type_val2str(vk->type);
105  if(print_security)
106  {
107    if(str_type == NULL)
108      printf("%s/%s,0x%.8X,%s,,,,,\n", prefix, quoted_name,
109             vk->type, quoted_value);
110    else
111      printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name,
112             str_type, quoted_value);
113  }
114  else
115  {
116    if(str_type == NULL)
117      printf("%s/%s,0x%.8X,%s,\n", prefix, quoted_name,
118             vk->type, quoted_value);
119    else
120      printf("%s/%s,%s,%s,\n", prefix, quoted_name,
121             str_type, quoted_value);
122  }
123
124  if(quoted_value != NULL)
125    free(quoted_value);
126  if(quoted_name != NULL)
127    free(quoted_name);
128  if(conv_error != NULL)
129    free(conv_error);
130}
131
132
133
134/* XXX: Each chunk must be unquoted after it is split out.
135 *      Quoting syntax may need to be standardized and pushed into the API
136 *      to deal with this issue and others.
137 */
138char** splitPath(const char* s)
139{
140  char** ret_val;
141  const char* cur = s;
142  char* next = NULL;
143  char* copy;
144  uint32 ret_cur = 0;
145
146  ret_val = (char**)malloc((REGF_MAX_DEPTH+1+1)*sizeof(char**));
147  if (ret_val == NULL)
148    return NULL;
149  ret_val[0] = NULL;
150
151  /* We return a well-formed, 0-length, path even when input is icky. */
152  if (s == NULL)
153    return ret_val;
154 
155  while((next = strchr(cur, '/')) != NULL)
156  {
157    if ((next-cur) > 0)
158    {
159      copy = (char*)malloc((next-cur+1)*sizeof(char));
160      if(copy == NULL)
161        bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
162         
163      memcpy(copy, cur, next-cur);
164      copy[next-cur] = '\0';
165      ret_val[ret_cur++] = copy;
166      if(ret_cur < (REGF_MAX_DEPTH+1+1))
167        ret_val[ret_cur] = NULL;
168      else
169        bailOut(EX_DATAERR, "ERROR: Registry maximum depth exceeded.\n");
170    }
171    cur = next+1;
172  }
173
174  /* Grab last element, if path doesn't end in '/'. */
175  if(strlen(cur) > 0)
176  {
177    copy = strdup(cur);
178    ret_val[ret_cur++] = copy;
179    if(ret_cur < (REGF_MAX_DEPTH+1+1))
180      ret_val[ret_cur] = NULL;
181    else
182      bailOut(EX_DATAERR, "ERROR: Registry maximum depth exceeded.\n");
183  }
184
185  return ret_val;
186}
187
188
189void freePath(char** path)
190{
191  uint32 i;
192
193  if(path == NULL)
194    return;
195
196  for(i=0; path[i] != NULL; i++)
197    free(path[i]);
198
199  free(path);
200}
201
202
203/* Returns a quoted path from an iterator's stack */
204/* XXX: Some way should be found to integrate this into regfi's API
205 *      The problem is that the escaping is sorta reglookup-specific.
206 */
207char* iter2Path(REGFI_ITERATOR* i)
208{
209  const REGFI_ITER_POSITION* cur;
210  uint32 buf_left = 127;
211  uint32 buf_len = buf_left+1;
212  uint32 name_len = 0;
213  uint32 grow_amt;
214  char* buf;
215  char* new_buf;
216  char* name;
217  const char* cur_name;
218  void_stack_iterator* iter;
219 
220  buf = (char*)malloc((buf_len)*sizeof(char));
221  if (buf == NULL)
222    return NULL;
223  buf[0] = '\0';
224
225  iter = void_stack_iterator_new(i->key_positions);
226  if (iter == NULL)
227  {
228    free(buf);
229    return NULL;
230  }
231
232  /* skip root element */
233  if(void_stack_size(i->key_positions) < 1)
234  {
235    buf[0] = '/';
236    buf[1] = '\0';
237    return buf;
238  }
239  cur = void_stack_iterator_next(iter);
240
241  do
242  {
243    cur = void_stack_iterator_next(iter);
244    if (cur == NULL)
245      cur_name = i->cur_key->keyname;
246    else
247      cur_name = cur->nk->keyname;
248
249    buf[buf_len-buf_left-1] = '/';
250    buf_left -= 1;
251    name = quote_string(cur_name, key_special_chars);
252    name_len = strlen(name);
253    if(name_len+1 > buf_left)
254    {
255      grow_amt = (uint32)(buf_len/2);
256      buf_len += name_len+1+grow_amt-buf_left;
257      if((new_buf = realloc(buf, buf_len)) == NULL)
258      {
259        free(buf);
260        free(iter);
261        return NULL;
262      }
263      buf = new_buf;
264      buf_left = grow_amt + name_len + 1;
265    }
266    strncpy(buf+(buf_len-buf_left-1), name, name_len);
267    buf_left -= name_len;
268    buf[buf_len-buf_left-1] = '\0';
269    free(name);
270  } while(cur != NULL);
271
272  return buf;
273}
274
275
276void printValueList(REGFI_ITERATOR* i, char* prefix)
277{
278  const REGF_VK_REC* value;
279
280  value = regfi_iterator_first_value(i);
281  while(value != NULL)
282  {
283    if(!type_filter_enabled || (value->type == type_filter))
284      printValue(value, prefix);
285    value = regfi_iterator_next_value(i);
286  }
287}
288
289
290void printKey(REGFI_ITERATOR* i, char* full_path)
291{
292  static char empty_str[1] = "";
293  char* owner = NULL;
294  char* group = NULL;
295  char* sacl = NULL;
296  char* dacl = NULL;
297  char* quoted_classname;
298  char mtime[20];
299  time_t tmp_time[1];
300  struct tm* tmp_time_s = NULL;
301  const REGF_SK_REC* sk;
302  const REGF_NK_REC* k = regfi_iterator_cur_key(i);
303
304  *tmp_time = nt_time_to_unix(&k->mtime);
305  tmp_time_s = gmtime(tmp_time);
306  strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
307
308  if(print_security && (sk=regfi_iterator_cur_sk(i)))
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    if(owner == NULL)
315      owner = empty_str;
316    if(group == NULL)
317      group = empty_str;
318    if(sacl == NULL)
319      sacl = empty_str;
320    if(dacl == NULL)
321      dacl = empty_str;
322
323    if(k->classname != NULL)
324      quoted_classname = quote_string(k->classname, key_special_chars);
325    else
326      quoted_classname = empty_str;
327
328    printf("%s,KEY,,%s,%s,%s,%s,%s,%s\n", full_path, mtime, 
329           owner, group, sacl, dacl, quoted_classname);
330
331    if(owner != empty_str)
332      free(owner);
333    if(group != empty_str)
334      free(group);
335    if(sacl != empty_str)
336      free(sacl);
337    if(dacl != empty_str)
338      free(dacl);
339    if(quoted_classname != empty_str)
340      free(quoted_classname);
341  }
342  else
343    printf("%s,KEY,,%s\n", full_path, mtime);
344}
345
346
347void printKeyTree(REGFI_ITERATOR* iter)
348{
349  const REGF_NK_REC* root = NULL;
350  const REGF_NK_REC* cur = NULL;
351  const REGF_NK_REC* sub = NULL;
352  char* path = NULL;
353  int key_type = regfi_type_str2val("KEY");
354  bool print_this = true;
355
356  root = cur = regfi_iterator_cur_key(iter);
357  sub = regfi_iterator_first_subkey(iter);
358 
359  if(root == NULL)
360    bailOut(EX_DATAERR, "ERROR: root cannot be NULL.\n");
361 
362  do
363  {
364    if(print_this)
365    {
366      path = iter2Path(iter);
367      if(path == NULL)
368        bailOut(EX_OSERR, "ERROR: Could not construct iterator's path.\n");
369     
370      if(!type_filter_enabled || (key_type == type_filter))
371        printKey(iter, path);
372      if(!type_filter_enabled || (key_type != type_filter))
373        printValueList(iter, path);
374     
375      free(path);
376    }
377   
378    if(sub == NULL)
379    {
380      if(cur != root)
381      {
382        /* We're done with this sub-tree, going up and hitting other branches. */
383        if(!regfi_iterator_up(iter))
384          bailOut(EX_DATAERR, "ERROR: could not traverse iterator upward.\n");
385       
386        cur = regfi_iterator_cur_key(iter);
387        if(cur == NULL)
388          bailOut(EX_DATAERR, "ERROR: unexpected NULL for key.\n");
389       
390        sub = regfi_iterator_next_subkey(iter);
391      }
392      print_this = false;
393    }
394    else
395    { /* We have unexplored sub-keys. 
396       * Let's move down and print this first sub-tree out.
397       */
398      if(!regfi_iterator_down(iter))
399        bailOut(EX_DATAERR, "ERROR: could not traverse iterator downward.\n");
400
401      cur = sub;
402      sub = regfi_iterator_first_subkey(iter);
403      print_this = true;
404    }
405  } while(!((cur == root) && (sub == NULL)));
406
407  if(print_verbose)
408    fprintf(stderr, "VERBOSE: Finished printing key tree.\n");
409}
410
411
412/* XXX: what if there is BOTH a value AND a key with that name?? */
413/*
414 * Returns 0 if path was not found.
415 * Returns 1 if path was found as value.
416 * Returns 2 if path was found as key.
417 * Returns less than 0 on other error.
418 */
419int retrievePath(REGFI_ITERATOR* iter, char** path)
420{
421  const REGF_VK_REC* value;
422  char* tmp_path_joined;
423  const char** tmp_path;
424  uint32 i;
425 
426  if(path == NULL)
427    return -1;
428
429  /* One extra for any value at the end, and one more for NULL */
430  tmp_path = (const char**)malloc(sizeof(const char**)*(REGF_MAX_DEPTH+1+1));
431  if(tmp_path == NULL)
432    return -2;
433
434  /* Strip any potential value name at end of path */
435  for(i=0; 
436      (path[i] != NULL) && (path[i+1] != NULL) 
437        && (i < REGF_MAX_DEPTH+1+1);
438      i++)
439    tmp_path[i] = path[i];
440
441  tmp_path[i] = NULL;
442
443  if(print_verbose)
444    fprintf(stderr, "VERBOSE: Attempting to retrieve specified path: %s\n",
445            path_filter);
446
447  /* Special check for '/' path filter */
448  if(path[0] == NULL)
449  {
450    if(print_verbose)
451      fprintf(stderr, "VERBOSE: Found final path element as root key.\n");
452    return 2;
453  }
454
455  if(!regfi_iterator_walk_path(iter, tmp_path))
456  {
457    free(tmp_path);
458    return 0;
459  }
460
461  if(regfi_iterator_find_value(iter, path[i]))
462  {
463    if(print_verbose)
464      fprintf(stderr, "VERBOSE: Found final path element as value.\n");
465
466    value = regfi_iterator_cur_value(iter);
467    tmp_path_joined = iter2Path(iter);
468
469    if((value == NULL) || (tmp_path_joined == NULL))
470      bailOut(EX_OSERR, "ERROR: Unexpected error before printValue.\n");
471
472    if(!type_filter_enabled || (value->type == type_filter))
473      printValue(value, tmp_path_joined);
474
475    free(tmp_path);
476    free(tmp_path_joined);
477    return 1;
478  }
479  else if(regfi_iterator_find_subkey(iter, path[i]))
480  {
481    if(print_verbose)
482      fprintf(stderr, "VERBOSE: Found final path element as key.\n");
483
484    if(!regfi_iterator_down(iter))
485      bailOut(EX_DATAERR, "ERROR: Unexpected error on traversing path filter key.\n");
486
487    return 2;
488  }
489
490  if(print_verbose)
491    fprintf(stderr, "VERBOSE: Could not find last element of path.\n");
492
493  return 0;
494}
495
496
497static void usage(void)
498{
499  fprintf(stderr, "Usage: reglookup [-v] [-s]"
500          " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]"
501          " <REGISTRY_FILE>\n");
502  fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION);
503  fprintf(stderr, "Options:\n");
504  fprintf(stderr, "\t-v\t sets verbose mode.\n");
505  fprintf(stderr, "\t-h\t enables header row. (default)\n");
506  fprintf(stderr, "\t-H\t disables header row.\n");
507  fprintf(stderr, "\t-s\t enables security descriptor output.\n");
508  fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n");
509  fprintf(stderr, "\t-p\t restrict output to elements below this path.\n");
510  fprintf(stderr, "\t-t\t restrict results to this specific data type.\n");
511  fprintf(stderr, "\n");
512}
513
514
515int main(int argc, char** argv)
516{
517  char** path = NULL;
518  REGFI_ITERATOR* iter;
519  int retr_path_ret;
520  uint32 argi, arge;
521
522  /* Process command line arguments */
523  if(argc < 2)
524  {
525    usage();
526    bailOut(EX_USAGE, "ERROR: Requires at least one argument.\n");
527  }
528 
529  arge = argc-1;
530  for(argi = 1; argi < arge; argi++)
531  {
532    if (strcmp("-p", argv[argi]) == 0)
533    {
534      if(++argi >= arge)
535      {
536        usage();
537        bailOut(EX_USAGE, "ERROR: '-p' option requires parameter.\n");
538      }
539      if((path_filter = strdup(argv[argi])) == NULL)
540        bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
541
542      path_filter_enabled = true;
543    }
544    else if (strcmp("-t", argv[argi]) == 0)
545    {
546      if(++argi >= arge)
547      {
548        usage();
549        bailOut(EX_USAGE, "ERROR: '-t' option requires parameter.\n");
550      }
551      if((type_filter = regfi_type_str2val(argv[argi])) < 0)
552      {
553        fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]);
554        bailOut(EX_USAGE, "");
555      }
556      type_filter_enabled = true;
557    }
558    else if (strcmp("-h", argv[argi]) == 0)
559      print_header = true;
560    else if (strcmp("-H", argv[argi]) == 0)
561      print_header = false;
562    else if (strcmp("-s", argv[argi]) == 0)
563      print_security = true;
564    else if (strcmp("-S", argv[argi]) == 0)
565      print_security = false;
566    else if (strcmp("-v", argv[argi]) == 0)
567      print_verbose = true;
568    else
569    {
570      usage();
571      fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
572      bailOut(EX_USAGE, "");
573    }
574  }
575  if((registry_file = strdup(argv[argi])) == NULL)
576    bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
577
578  f = regfi_open(registry_file);
579  if(f == NULL)
580  {
581    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
582    bailOut(EX_NOINPUT, "");
583  }
584
585  iter = regfi_iterator_new(f);
586  if(iter == NULL)
587    bailOut(EX_OSERR, "ERROR: Couldn't create registry iterator.\n");
588
589  if(print_header)
590  {
591    if(print_security)
592      printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL,CLASS\n");
593    else
594      printf("PATH,TYPE,VALUE,MTIME\n");
595  }
596
597  if(path_filter_enabled && path_filter != NULL)
598    path = splitPath(path_filter);
599
600  if(path != NULL)
601  {
602    retr_path_ret = retrievePath(iter, path);
603    freePath(path);
604
605    if(retr_path_ret == 0)
606      fprintf(stderr, "WARNING: specified path not found.\n");
607    else if (retr_path_ret == 2)
608      printKeyTree(iter);
609    else if(retr_path_ret < 0)
610    {
611      fprintf(stderr, "ERROR: retrievePath() returned %d.\n", 
612              retr_path_ret);
613      bailOut(EX_DATAERR,"ERROR: Unknown error occurred in retrieving path.\n");
614    }
615  }
616  else
617    printKeyTree(iter);
618
619  regfi_iterator_free(iter);
620  regfi_close(f);
621
622  return 0;
623}
Note: See TracBrowser for help on using the repository browser.