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
RevLine 
[30]1/*
[42]2 * A utility to read a Windows NT/2K/XP/2K3 registry file, using
3 * Gerald Carter''s regfio interface.
[30]4 *
[111]5 * Copyright (C) 2005-2008 Timothy D. Morgan
[42]6 * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com
[30]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
[111]10 * the Free Software Foundation; version 3 of the License.
[30]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>
[88]26#include <sysexits.h>
[30]27#include <stdio.h>
28#include <string.h>
[33]29#include <strings.h>
[42]30#include <time.h>
[79]31#include "../include/regfi.h"
[31]32#include "../include/void_stack.h"
[30]33
[40]34/* Globals, influenced by command line parameters */
35bool print_verbose = false;
36bool print_security = false;
[42]37bool print_header = true;
[40]38bool path_filter_enabled = false;
39bool type_filter_enabled = false;
40char* path_filter = NULL;
41int type_filter;
42char* registry_file = NULL;
43
[42]44/* Other globals */
[109]45REGF_FILE* f;
[66]46
[40]47
[116]48/* XXX: A hack to share some functions with reglookup-recover.c.
[125]49 *      Should move these into a proper library at some point.
[111]50 */
51#include "common.c"
[61]52
[38]53
[111]54void printValue(const REGF_VK_REC* vk, char* prefix)
[41]55{
[111]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;
[41]61
[111]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)
[41]69  {
[111]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;
[41]73  }
[111]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  }
[41]88
[111]89  quoted_value = data_to_ascii(vk->data, size, vk->type, &conv_error);
90  if(quoted_value == NULL)
[41]91  {
[111]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);
[41]98  }
[111]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);
[41]103
[111]104  str_type = regfi_type_val2str(vk->type);
105  if(print_security)
[41]106  {
[111]107    if(str_type == NULL)
108      printf("%s/%s,0x%.8X,%s,,,,,\n", prefix, quoted_name,
109             vk->type, quoted_value);
[66]110    else
[111]111      printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name,
112             str_type, quoted_value);
[71]113  }
[111]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  }
[42]123
[111]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);
[41]130}
131
132
[111]133
[83]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 */
[81]138char** splitPath(const char* s)
[30]139{
[81]140  char** ret_val;
[38]141  const char* cur = s;
[33]142  char* next = NULL;
[38]143  char* copy;
[81]144  uint32 ret_cur = 0;
[38]145
[81]146  ret_val = (char**)malloc((REGF_MAX_DEPTH+1+1)*sizeof(char**));
147  if (ret_val == NULL)
[38]148    return NULL;
[81]149  ret_val[0] = NULL;
150
151  /* We return a well-formed, 0-length, path even when input is icky. */
[37]152  if (s == NULL)
[81]153    return ret_val;
[38]154 
155  while((next = strchr(cur, '/')) != NULL)
[33]156  {
[38]157    if ((next-cur) > 0)
158    {
159      copy = (char*)malloc((next-cur+1)*sizeof(char));
160      if(copy == NULL)
[88]161        bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
[38]162         
163      memcpy(copy, cur, next-cur);
164      copy[next-cur] = '\0';
[81]165      ret_val[ret_cur++] = copy;
166      if(ret_cur < (REGF_MAX_DEPTH+1+1))
167        ret_val[ret_cur] = NULL;
168      else
[88]169        bailOut(EX_DATAERR, "ERROR: Registry maximum depth exceeded.\n");
[38]170    }
171    cur = next+1;
[33]172  }
[81]173
174  /* Grab last element, if path doesn't end in '/'. */
[33]175  if(strlen(cur) > 0)
[38]176  {
177    copy = strdup(cur);
[81]178    ret_val[ret_cur++] = copy;
179    if(ret_cur < (REGF_MAX_DEPTH+1+1))
180      ret_val[ret_cur] = NULL;
181    else
[88]182      bailOut(EX_DATAERR, "ERROR: Registry maximum depth exceeded.\n");
[38]183  }
[33]184
185  return ret_val;
186}
187
[81]188
[83]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
[81]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)
[33]208{
[81]209  const REGFI_ITER_POSITION* cur;
[37]210  uint32 buf_left = 127;
211  uint32 buf_len = buf_left+1;
212  uint32 name_len = 0;
213  uint32 grow_amt;
[81]214  char* buf;
[31]215  char* new_buf;
[66]216  char* name;
[81]217  const char* cur_name;
[31]218  void_stack_iterator* iter;
219 
220  buf = (char*)malloc((buf_len)*sizeof(char));
221  if (buf == NULL)
222    return NULL;
[54]223  buf[0] = '\0';
[30]224
[81]225  iter = void_stack_iterator_new(i->key_positions);
[31]226  if (iter == NULL)
[30]227  {
[31]228    free(buf);
229    return NULL;
[30]230  }
231
[33]232  /* skip root element */
[81]233  if(void_stack_size(i->key_positions) < 1)
234  {
235    buf[0] = '/';
236    buf[1] = '\0';
237    return buf;
238  }
[33]239  cur = void_stack_iterator_next(iter);
240
[81]241  do
[31]242  {
[81]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
[33]249    buf[buf_len-buf_left-1] = '/';
250    buf_left -= 1;
[81]251    name = quote_string(cur_name, key_special_chars);
[66]252    name_len = strlen(name);
[31]253    if(name_len+1 > buf_left)
254    {
[37]255      grow_amt = (uint32)(buf_len/2);
[31]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    }
[66]266    strncpy(buf+(buf_len-buf_left-1), name, name_len);
[31]267    buf_left -= name_len;
268    buf[buf_len-buf_left-1] = '\0';
[66]269    free(name);
[81]270  } while(cur != NULL);
[30]271
[31]272  return buf;
273}
[30]274
[31]275
[81]276void printValueList(REGFI_ITERATOR* i, char* prefix)
[32]277{
[84]278  const REGF_VK_REC* value;
[80]279
280  value = regfi_iterator_first_value(i);
281  while(value != NULL)
[81]282  {
283    if(!type_filter_enabled || (value->type == type_filter))
[80]284      printValue(value, prefix);
[81]285    value = regfi_iterator_next_value(i);
286  }
[33]287}
288
[37]289
[109]290void printKey(REGFI_ITERATOR* i, char* full_path)
[33]291{
[43]292  static char empty_str[1] = "";
[42]293  char* owner = NULL;
294  char* group = NULL;
295  char* sacl = NULL;
296  char* dacl = NULL;
[125]297  char* quoted_classname;
[42]298  char mtime[20];
299  time_t tmp_time[1];
300  struct tm* tmp_time_s = NULL;
[109]301  const REGF_SK_REC* sk;
302  const REGF_NK_REC* k = regfi_iterator_cur_key(i);
[42]303
[43]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
[109]308  if(print_security && (sk=regfi_iterator_cur_sk(i)))
[43]309  {
[109]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);
[43]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
[125]323    if(k->classname != NULL)
324      quoted_classname = quote_string(k->classname, key_special_chars);
325    else
326      quoted_classname = empty_str;
[43]327
[125]328    printf("%s,KEY,,%s,%s,%s,%s,%s,%s\n", full_path, mtime, 
329           owner, group, sacl, dacl, quoted_classname);
330
[43]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);
[125]339    if(quoted_classname != empty_str)
340      free(quoted_classname);
[43]341  }
342  else
[66]343    printf("%s,KEY,,%s\n", full_path, mtime);
[43]344}
345
346
[81]347void printKeyTree(REGFI_ITERATOR* iter)
[43]348{
[84]349  const REGF_NK_REC* root = NULL;
350  const REGF_NK_REC* cur = NULL;
351  const REGF_NK_REC* sub = NULL;
[43]352  char* path = NULL;
[78]353  int key_type = regfi_type_str2val("KEY");
[81]354  bool print_this = true;
355
356  root = cur = regfi_iterator_cur_key(iter);
357  sub = regfi_iterator_first_subkey(iter);
[43]358 
[81]359  if(root == NULL)
[88]360    bailOut(EX_DATAERR, "ERROR: root cannot be NULL.\n");
[81]361 
362  do
[31]363  {
[81]364    if(print_this)
[54]365    {
[81]366      path = iter2Path(iter);
367      if(path == NULL)
[88]368        bailOut(EX_OSERR, "ERROR: Could not construct iterator's path.\n");
[81]369     
370      if(!type_filter_enabled || (key_type == type_filter))
[109]371        printKey(iter, path);
[81]372      if(!type_filter_enabled || (key_type != type_filter))
373        printValueList(iter, path);
374     
375      free(path);
[54]376    }
[66]377   
[81]378    if(sub == NULL)
[31]379    {
[81]380      if(cur != root)
[31]381      {
[81]382        /* We're done with this sub-tree, going up and hitting other branches. */
383        if(!regfi_iterator_up(iter))
[88]384          bailOut(EX_DATAERR, "ERROR: could not traverse iterator upward.\n");
[81]385       
386        cur = regfi_iterator_cur_key(iter);
387        if(cur == NULL)
[88]388          bailOut(EX_DATAERR, "ERROR: unexpected NULL for key.\n");
[81]389       
390        sub = regfi_iterator_next_subkey(iter);
[66]391      }
[81]392      print_this = false;
[31]393    }
[81]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))
[88]399        bailOut(EX_DATAERR, "ERROR: could not traverse iterator downward.\n");
[81]400
401      cur = sub;
402      sub = regfi_iterator_first_subkey(iter);
403      print_this = true;
404    }
405  } while(!((cur == root) && (sub == NULL)));
406
[54]407  if(print_verbose)
408    fprintf(stderr, "VERBOSE: Finished printing key tree.\n");
[30]409}
410
[81]411
[97]412/* XXX: what if there is BOTH a value AND a key with that name?? */
[33]413/*
[80]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.
[33]417 * Returns less than 0 on other error.
418 */
[80]419int retrievePath(REGFI_ITERATOR* iter, char** path)
[33]420{
[84]421  const REGF_VK_REC* value;
[81]422  char* tmp_path_joined;
423  const char** tmp_path;
[80]424  uint32 i;
425 
426  if(path == NULL)
[33]427    return -1;
428
[80]429  /* One extra for any value at the end, and one more for NULL */
[81]430  tmp_path = (const char**)malloc(sizeof(const char**)*(REGF_MAX_DEPTH+1+1));
[80]431  if(tmp_path == NULL)
[33]432    return -2;
433
[80]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];
[33]440
[80]441  tmp_path[i] = NULL;
442
[54]443  if(print_verbose)
[80]444    fprintf(stderr, "VERBOSE: Attempting to retrieve specified path: %s\n",
[54]445            path_filter);
446
[82]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
[80]455  if(!regfi_iterator_walk_path(iter, tmp_path))
[33]456  {
[80]457    free(tmp_path);
458    return 0;
[33]459  }
460
[80]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");
[33]465
[80]466    value = regfi_iterator_cur_value(iter);
[81]467    tmp_path_joined = iter2Path(iter);
[54]468
[80]469    if((value == NULL) || (tmp_path_joined == NULL))
[88]470      bailOut(EX_OSERR, "ERROR: Unexpected error before printValue.\n");
[54]471
[121]472    if(!type_filter_enabled || (value->type == type_filter))
473      printValue(value, tmp_path_joined);
[54]474
[80]475    free(tmp_path);
476    free(tmp_path_joined);
477    return 1;
[33]478  }
[80]479  else if(regfi_iterator_find_subkey(iter, path[i]))
[33]480  {
[80]481    if(print_verbose)
482      fprintf(stderr, "VERBOSE: Found final path element as key.\n");
[82]483
484    if(!regfi_iterator_down(iter))
[88]485      bailOut(EX_DATAERR, "ERROR: Unexpected error on traversing path filter key.\n");
[82]486
[80]487    return 2;
[33]488  }
489
[54]490  if(print_verbose)
491    fprintf(stderr, "VERBOSE: Could not find last element of path.\n");
492
[80]493  return 0;
[33]494}
495
496
[37]497static void usage(void)
498{
[61]499  fprintf(stderr, "Usage: reglookup [-v] [-s]"
[40]500          " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]"
[39]501          " <REGISTRY_FILE>\n");
[111]502  fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION);
[39]503  fprintf(stderr, "Options:\n");
504  fprintf(stderr, "\t-v\t sets verbose mode.\n");
[47]505  fprintf(stderr, "\t-h\t enables header row. (default)\n");
506  fprintf(stderr, "\t-H\t disables header row.\n");
[44]507  fprintf(stderr, "\t-s\t enables security descriptor output.\n");
508  fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n");
[40]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");
[37]511  fprintf(stderr, "\n");
512}
513
514
[30]515int main(int argc, char** argv)
516{
[80]517  char** path = NULL;
518  REGFI_ITERATOR* iter;
[33]519  int retr_path_ret;
[44]520  uint32 argi, arge;
[31]521
[37]522  /* Process command line arguments */
[30]523  if(argc < 2)
524  {
[37]525    usage();
[88]526    bailOut(EX_USAGE, "ERROR: Requires at least one argument.\n");
[30]527  }
[37]528 
[44]529  arge = argc-1;
530  for(argi = 1; argi < arge; argi++)
[37]531  {
[40]532    if (strcmp("-p", argv[argi]) == 0)
[37]533    {
[44]534      if(++argi >= arge)
[37]535      {
536        usage();
[88]537        bailOut(EX_USAGE, "ERROR: '-p' option requires parameter.\n");
[37]538      }
[40]539      if((path_filter = strdup(argv[argi])) == NULL)
[88]540        bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
[38]541
[40]542      path_filter_enabled = true;
[37]543    }
544    else if (strcmp("-t", argv[argi]) == 0)
545    {
[44]546      if(++argi >= arge)
[37]547      {
548        usage();
[88]549        bailOut(EX_USAGE, "ERROR: '-t' option requires parameter.\n");
[37]550      }
[78]551      if((type_filter = regfi_type_str2val(argv[argi])) < 0)
[40]552      {
553        fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]);
[88]554        bailOut(EX_USAGE, "");
[40]555      }
[37]556      type_filter_enabled = true;
557    }
[47]558    else if (strcmp("-h", argv[argi]) == 0)
559      print_header = true;
560    else if (strcmp("-H", argv[argi]) == 0)
561      print_header = false;
[37]562    else if (strcmp("-s", argv[argi]) == 0)
563      print_security = true;
[44]564    else if (strcmp("-S", argv[argi]) == 0)
565      print_security = false;
[37]566    else if (strcmp("-v", argv[argi]) == 0)
567      print_verbose = true;
[44]568    else
[37]569    {
[38]570      usage();
[37]571      fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
[88]572      bailOut(EX_USAGE, "");
[37]573    }
574  }
[44]575  if((registry_file = strdup(argv[argi])) == NULL)
[88]576    bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
[30]577
[110]578  f = regfi_open(registry_file);
[37]579  if(f == NULL)
580  {
581    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
[88]582    bailOut(EX_NOINPUT, "");
[37]583  }
[38]584
[80]585  iter = regfi_iterator_new(f);
586  if(iter == NULL)
[88]587    bailOut(EX_OSERR, "ERROR: Couldn't create registry iterator.\n");
[30]588
[81]589  if(print_header)
590  {
591    if(print_security)
[125]592      printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL,CLASS\n");
[81]593    else
594      printf("PATH,TYPE,VALUE,MTIME\n");
595  }
596
[80]597  if(path_filter_enabled && path_filter != NULL)
598    path = splitPath(path_filter);
[81]599
[80]600  if(path != NULL)
[33]601  {
[80]602    retr_path_ret = retrievePath(iter, path);
[83]603    freePath(path);
604
[80]605    if(retr_path_ret == 0)
606      fprintf(stderr, "WARNING: specified path not found.\n");
607    else if (retr_path_ret == 2)
[81]608      printKeyTree(iter);
[93]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    }
[33]615  }
[37]616  else
[81]617    printKeyTree(iter);
[31]618
[80]619  regfi_iterator_free(iter);
[78]620  regfi_close(f);
[30]621
622  return 0;
623}
Note: See TracBrowser for help on using the repository browser.