source: trunk/src/reglookup.c @ 118

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

fixed major bug in reglookup-recover; now recovers much more data
rolled back release version to 0.9.0
added date range checking in regfi's NK parsing for deleted records

  • Property svn:keywords set to Id
File size: 15.6 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 116 2008-08-03 19:34:27Z 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.
49 *      Should move these into a properly 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;
297  char mtime[20];
298  time_t tmp_time[1];
299  struct tm* tmp_time_s = NULL;
[109]300  const REGF_SK_REC* sk;
301  const REGF_NK_REC* k = regfi_iterator_cur_key(i);
[42]302
[43]303  *tmp_time = nt_time_to_unix(&k->mtime);
304  tmp_time_s = gmtime(tmp_time);
305  strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
306
[109]307  if(print_security && (sk=regfi_iterator_cur_sk(i)))
[43]308  {
[109]309    owner = regfi_get_owner(sk->sec_desc);
310    group = regfi_get_group(sk->sec_desc);
311    sacl = regfi_get_sacl(sk->sec_desc);
312    dacl = regfi_get_dacl(sk->sec_desc);
[43]313    if(owner == NULL)
314      owner = empty_str;
315    if(group == NULL)
316      group = empty_str;
317    if(sacl == NULL)
318      sacl = empty_str;
319    if(dacl == NULL)
320      dacl = empty_str;
321
[66]322    printf("%s,KEY,,%s,%s,%s,%s,%s\n", full_path, mtime, 
[43]323           owner, group, sacl, dacl);
324
325    if(owner != empty_str)
326      free(owner);
327    if(group != empty_str)
328      free(group);
329    if(sacl != empty_str)
330      free(sacl);
331    if(dacl != empty_str)
332      free(dacl);
333  }
334  else
[66]335    printf("%s,KEY,,%s\n", full_path, mtime);
[43]336}
337
338
[81]339void printKeyTree(REGFI_ITERATOR* iter)
[43]340{
[84]341  const REGF_NK_REC* root = NULL;
342  const REGF_NK_REC* cur = NULL;
343  const REGF_NK_REC* sub = NULL;
[43]344  char* path = NULL;
[78]345  int key_type = regfi_type_str2val("KEY");
[81]346  bool print_this = true;
347
348  root = cur = regfi_iterator_cur_key(iter);
349  sub = regfi_iterator_first_subkey(iter);
[43]350 
[81]351  if(root == NULL)
[88]352    bailOut(EX_DATAERR, "ERROR: root cannot be NULL.\n");
[81]353 
354  do
[31]355  {
[81]356    if(print_this)
[54]357    {
[81]358      path = iter2Path(iter);
359      if(path == NULL)
[88]360        bailOut(EX_OSERR, "ERROR: Could not construct iterator's path.\n");
[81]361     
362      if(!type_filter_enabled || (key_type == type_filter))
[109]363        printKey(iter, path);
[81]364      if(!type_filter_enabled || (key_type != type_filter))
365        printValueList(iter, path);
366     
367      free(path);
[54]368    }
[66]369   
[81]370    if(sub == NULL)
[31]371    {
[81]372      if(cur != root)
[31]373      {
[81]374        /* We're done with this sub-tree, going up and hitting other branches. */
375        if(!regfi_iterator_up(iter))
[88]376          bailOut(EX_DATAERR, "ERROR: could not traverse iterator upward.\n");
[81]377       
378        cur = regfi_iterator_cur_key(iter);
379        if(cur == NULL)
[88]380          bailOut(EX_DATAERR, "ERROR: unexpected NULL for key.\n");
[81]381       
382        sub = regfi_iterator_next_subkey(iter);
[66]383      }
[81]384      print_this = false;
[31]385    }
[81]386    else
387    { /* We have unexplored sub-keys. 
388       * Let's move down and print this first sub-tree out.
389       */
390      if(!regfi_iterator_down(iter))
[88]391        bailOut(EX_DATAERR, "ERROR: could not traverse iterator downward.\n");
[81]392
393      cur = sub;
394      sub = regfi_iterator_first_subkey(iter);
395      print_this = true;
396    }
397  } while(!((cur == root) && (sub == NULL)));
398
[54]399  if(print_verbose)
400    fprintf(stderr, "VERBOSE: Finished printing key tree.\n");
[30]401}
402
[81]403
[97]404/* XXX: what if there is BOTH a value AND a key with that name?? */
[33]405/*
[80]406 * Returns 0 if path was not found.
407 * Returns 1 if path was found as value.
408 * Returns 2 if path was found as key.
[33]409 * Returns less than 0 on other error.
410 */
[80]411int retrievePath(REGFI_ITERATOR* iter, char** path)
[33]412{
[84]413  const REGF_VK_REC* value;
[81]414  char* tmp_path_joined;
415  const char** tmp_path;
[80]416  uint32 i;
417 
418  if(path == NULL)
[33]419    return -1;
420
[80]421  /* One extra for any value at the end, and one more for NULL */
[81]422  tmp_path = (const char**)malloc(sizeof(const char**)*(REGF_MAX_DEPTH+1+1));
[80]423  if(tmp_path == NULL)
[33]424    return -2;
425
[80]426  /* Strip any potential value name at end of path */
427  for(i=0; 
428      (path[i] != NULL) && (path[i+1] != NULL) 
429        && (i < REGF_MAX_DEPTH+1+1);
430      i++)
431    tmp_path[i] = path[i];
[33]432
[80]433  tmp_path[i] = NULL;
434
[54]435  if(print_verbose)
[80]436    fprintf(stderr, "VERBOSE: Attempting to retrieve specified path: %s\n",
[54]437            path_filter);
438
[82]439  /* Special check for '/' path filter */
440  if(path[0] == NULL)
441  {
442    if(print_verbose)
443      fprintf(stderr, "VERBOSE: Found final path element as root key.\n");
444    return 2;
445  }
446
[80]447  if(!regfi_iterator_walk_path(iter, tmp_path))
[33]448  {
[80]449    free(tmp_path);
450    return 0;
[33]451  }
452
[80]453  if(regfi_iterator_find_value(iter, path[i]))
454  {
455    if(print_verbose)
456      fprintf(stderr, "VERBOSE: Found final path element as value.\n");
[33]457
[80]458    value = regfi_iterator_cur_value(iter);
[81]459    tmp_path_joined = iter2Path(iter);
[54]460
[80]461    if((value == NULL) || (tmp_path_joined == NULL))
[88]462      bailOut(EX_OSERR, "ERROR: Unexpected error before printValue.\n");
[54]463
[80]464    printValue(value, tmp_path_joined);
[54]465
[80]466    free(tmp_path);
467    free(tmp_path_joined);
468    return 1;
[33]469  }
[80]470  else if(regfi_iterator_find_subkey(iter, path[i]))
[33]471  {
[80]472    if(print_verbose)
473      fprintf(stderr, "VERBOSE: Found final path element as key.\n");
[82]474
475    if(!regfi_iterator_down(iter))
[88]476      bailOut(EX_DATAERR, "ERROR: Unexpected error on traversing path filter key.\n");
[82]477
[80]478    return 2;
[33]479  }
480
[54]481  if(print_verbose)
482    fprintf(stderr, "VERBOSE: Could not find last element of path.\n");
483
[80]484  return 0;
[33]485}
486
487
[37]488static void usage(void)
489{
[61]490  fprintf(stderr, "Usage: reglookup [-v] [-s]"
[40]491          " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]"
[39]492          " <REGISTRY_FILE>\n");
[111]493  fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION);
[39]494  fprintf(stderr, "Options:\n");
495  fprintf(stderr, "\t-v\t sets verbose mode.\n");
[47]496  fprintf(stderr, "\t-h\t enables header row. (default)\n");
497  fprintf(stderr, "\t-H\t disables header row.\n");
[44]498  fprintf(stderr, "\t-s\t enables security descriptor output.\n");
499  fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n");
[40]500  fprintf(stderr, "\t-p\t restrict output to elements below this path.\n");
501  fprintf(stderr, "\t-t\t restrict results to this specific data type.\n");
[37]502  fprintf(stderr, "\n");
503}
504
505
[30]506int main(int argc, char** argv)
507{
[80]508  char** path = NULL;
509  REGFI_ITERATOR* iter;
[33]510  int retr_path_ret;
[44]511  uint32 argi, arge;
[31]512
[37]513  /* Process command line arguments */
[30]514  if(argc < 2)
515  {
[37]516    usage();
[88]517    bailOut(EX_USAGE, "ERROR: Requires at least one argument.\n");
[30]518  }
[37]519 
[44]520  arge = argc-1;
521  for(argi = 1; argi < arge; argi++)
[37]522  {
[40]523    if (strcmp("-p", argv[argi]) == 0)
[37]524    {
[44]525      if(++argi >= arge)
[37]526      {
527        usage();
[88]528        bailOut(EX_USAGE, "ERROR: '-p' option requires parameter.\n");
[37]529      }
[40]530      if((path_filter = strdup(argv[argi])) == NULL)
[88]531        bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
[38]532
[40]533      path_filter_enabled = true;
[37]534    }
535    else if (strcmp("-t", argv[argi]) == 0)
536    {
[44]537      if(++argi >= arge)
[37]538      {
539        usage();
[88]540        bailOut(EX_USAGE, "ERROR: '-t' option requires parameter.\n");
[37]541      }
[78]542      if((type_filter = regfi_type_str2val(argv[argi])) < 0)
[40]543      {
544        fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]);
[88]545        bailOut(EX_USAGE, "");
[40]546      }
[37]547      type_filter_enabled = true;
548    }
[47]549    else if (strcmp("-h", argv[argi]) == 0)
550      print_header = true;
551    else if (strcmp("-H", argv[argi]) == 0)
552      print_header = false;
[37]553    else if (strcmp("-s", argv[argi]) == 0)
554      print_security = true;
[44]555    else if (strcmp("-S", argv[argi]) == 0)
556      print_security = false;
[37]557    else if (strcmp("-v", argv[argi]) == 0)
558      print_verbose = true;
[44]559    else
[37]560    {
[38]561      usage();
[37]562      fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
[88]563      bailOut(EX_USAGE, "");
[37]564    }
565  }
[44]566  if((registry_file = strdup(argv[argi])) == NULL)
[88]567    bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
[30]568
[110]569  f = regfi_open(registry_file);
[37]570  if(f == NULL)
571  {
572    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
[88]573    bailOut(EX_NOINPUT, "");
[37]574  }
[38]575
[80]576  iter = regfi_iterator_new(f);
577  if(iter == NULL)
[88]578    bailOut(EX_OSERR, "ERROR: Couldn't create registry iterator.\n");
[30]579
[81]580  if(print_header)
581  {
582    if(print_security)
583      printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL\n");
584    else
585      printf("PATH,TYPE,VALUE,MTIME\n");
586  }
587
[80]588  if(path_filter_enabled && path_filter != NULL)
589    path = splitPath(path_filter);
[81]590
[80]591  if(path != NULL)
[33]592  {
[80]593    retr_path_ret = retrievePath(iter, path);
[83]594    freePath(path);
595
[80]596    if(retr_path_ret == 0)
597      fprintf(stderr, "WARNING: specified path not found.\n");
598    else if (retr_path_ret == 2)
[81]599      printKeyTree(iter);
[93]600    else if(retr_path_ret < 0)
601    {
602      fprintf(stderr, "ERROR: retrievePath() returned %d.\n", 
603              retr_path_ret);
604      bailOut(EX_DATAERR,"ERROR: Unknown error occurred in retrieving path.\n");
605    }
[33]606  }
[37]607  else
[81]608    printKeyTree(iter);
[31]609
[80]610  regfi_iterator_free(iter);
[78]611  regfi_close(f);
[30]612
613  return 0;
614}
Note: See TracBrowser for help on using the repository browser.