source: trunk/src/reglookup-recover.c @ 130

Last change on this file since 130 was 130, checked in by tim, 15 years ago

fixed overlooked constant rename

  • Property svn:keywords set to Id
File size: 22.6 KB
Line 
1/*
2 * This program attempts to recover deleted data structures in a registry hive.
3 *
4 * Copyright (C) 2008 Timothy D. Morgan
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 3 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
18 *
19 * $Id: reglookup-recover.c 130 2009-01-10 17:41:26Z tim $
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <sysexits.h>
25
26#include "../include/regfi.h"
27#include "../include/range_list.h"
28#include "../include/lru_cache.h"
29
30
31/* Globals, influenced by command line parameters */
32bool print_verbose = false;
33bool print_security = false;
34bool print_header = true;
35bool print_leftover = false;
36bool print_parsedraw = false;
37char* registry_file = NULL;
38
39#include "common.c"
40
41
42char* getQuotedData(int fd, uint32 offset, uint32 length)
43{
44  uint8* buf;
45  char* quoted_buf;
46  uint32 len;
47
48  if((lseek(fd, offset, SEEK_SET)) == -1)
49    return NULL;
50
51  buf = (uint8*)malloc(length);
52  if(buf == NULL)
53    return NULL;
54
55  len = length;
56  if((regfi_read(fd, buf, &length) != 0) || length != len)
57  {
58    free(buf);
59    return NULL;
60  }
61
62  quoted_buf = quote_buffer(buf, length, common_special_chars);
63  free(buf);
64
65  return quoted_buf;
66}
67
68
69void printKey(REGF_FILE* f, REGF_NK_REC* nk, const char* prefix)
70{
71  char mtime[20];
72  time_t tmp_time[1];
73  struct tm* tmp_time_s = NULL;
74  char* quoted_name = NULL;
75  char* quoted_raw = "";
76
77  *tmp_time = nt_time_to_unix(&nk->mtime);
78  tmp_time_s = gmtime(tmp_time);
79  strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
80
81  quoted_name = quote_string(nk->keyname, key_special_chars);
82  if (quoted_name == NULL)
83  {
84    quoted_name = malloc(1*sizeof(char));
85    if(quoted_name == NULL)
86      bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n");
87    quoted_name[0] = '\0';
88
89    fprintf(stderr, "WARNING: NULL key name in NK record at offset %.8X.\n",
90            nk->offset);
91  }
92
93  if(print_parsedraw)
94    quoted_raw = getQuotedData(f->fd, nk->offset, nk->cell_size);
95
96  printf("%.8X,%.8X,KEY,%s,%s,%s,%d,,,,,,,,%s\n", nk->offset, nk->cell_size,
97         prefix, quoted_name, mtime, nk->num_values, quoted_raw);
98 
99  if(print_parsedraw)
100    free(quoted_raw);
101}
102
103
104void printValue(REGF_FILE* f, const REGF_VK_REC* vk, const char* prefix)
105{
106  char* quoted_value = NULL;
107  char* quoted_name = NULL;
108  char* quoted_raw = "";
109  char* conv_error = NULL;
110  const char* str_type = NULL;
111  uint32 size = vk->data_size;
112
113  /* Microsoft's documentation indicates that "available memory" is
114   * the limit on value sizes.  Annoying.  We limit it to 1M which
115   * should rarely be exceeded, unless the file is corrupt or
116   * malicious. For more info, see:
117   *   http://msdn2.microsoft.com/en-us/library/ms724872.aspx
118   */
119  /* XXX: Should probably do something different here for this tool.
120   *      Also, It would be really nice if this message somehow included the
121   *      name of the current value we're having trouble with, since
122   *      stderr/stdout don't always sync nicely.
123   */
124  if(size > VK_MAX_DATA_LENGTH)
125  {
126    fprintf(stderr, "WARNING: value data size %d larger than "
127            "%d, truncating...\n", size, VK_MAX_DATA_LENGTH);
128    size = VK_MAX_DATA_LENGTH;
129  }
130 
131  quoted_name = quote_string(vk->valuename, key_special_chars);
132  if (quoted_name == NULL)
133  { /* Value names are NULL when we're looking at the "(default)" value.
134     * Currently we just return a 0-length string to try an eliminate
135     * ambiguity with a literal "(default)" value.  The data type of a line
136     * in the output allows one to differentiate between the parent key and
137     * this value.
138     */
139    quoted_name = malloc(1*sizeof(char));
140    if(quoted_name == NULL)
141      bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n");
142    quoted_name[0] = '\0';
143  }
144
145  quoted_value = data_to_ascii(vk->data, size, vk->type, &conv_error);
146  if(quoted_value == NULL)
147  {
148    quoted_value = malloc(1*sizeof(char));
149    if(quoted_value == NULL)
150      bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n");
151    quoted_value[0] = '\0';
152
153    if(conv_error == NULL)
154      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
155              "Memory allocation failure likely.\n", prefix, quoted_name);
156    else if(print_verbose)
157      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
158              "Returned error: %s\n", prefix, quoted_name, conv_error);
159  }
160  /* XXX: should these always be printed? */
161  else if(conv_error != NULL && print_verbose)
162    fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', "
163            "warning returned: %s\n", prefix, quoted_name, conv_error);
164
165
166  if(print_parsedraw)
167    quoted_raw = getQuotedData(f->fd, vk->offset, vk->cell_size);
168
169  str_type = regfi_type_val2str(vk->type);
170  if(str_type == NULL)
171    printf("%.8X,%.8X,VALUE,%s,%s,,,0x%.8X,%s,%d,,,,,%s\n", 
172           vk->offset, vk->cell_size, prefix, quoted_name, 
173           vk->type, quoted_value, vk->data_size, quoted_raw);
174  else
175    printf("%.8X,%.8X,VALUE,%s,%s,,,%s,%s,%d,,,,,%s\n", 
176           vk->offset, vk->cell_size, prefix, quoted_name, 
177           str_type, quoted_value, vk->data_size, quoted_raw);
178
179  if(print_parsedraw)
180    free(quoted_raw);
181  if(quoted_value != NULL)
182    free(quoted_value);
183  if(quoted_name != NULL)
184    free(quoted_name);
185  if(conv_error != NULL)
186    free(conv_error);
187}
188
189
190void printSK(REGF_FILE* f, REGF_SK_REC* sk)
191{
192  char* quoted_raw = NULL;
193  char* empty_str = "";
194  char* owner = regfi_get_owner(sk->sec_desc);
195  char* group = regfi_get_group(sk->sec_desc);
196  char* sacl = regfi_get_sacl(sk->sec_desc);
197  char* dacl = regfi_get_dacl(sk->sec_desc);
198
199  if(print_parsedraw)
200    quoted_raw = getQuotedData(f->fd, sk->offset, sk->cell_size);
201
202  if(owner == NULL)
203    owner = empty_str;
204  if(group == NULL)
205    group = empty_str;
206  if(sacl == NULL)
207    sacl = empty_str;
208  if(dacl == NULL)
209    dacl = empty_str;
210
211  printf("%.8X,%.8X,SK,,,,,,,,%s,%s,%s,%s,%s\n", sk->offset, sk->cell_size,
212         owner, group, sacl, dacl, quoted_raw);
213 
214  if(owner != empty_str)
215    free(owner);
216  if(group != empty_str)
217    free(group);
218  if(sacl != empty_str)
219    free(sacl);
220  if(dacl != empty_str)
221    free(dacl);
222
223  if(print_parsedraw)
224    free(quoted_raw);
225}
226
227
228int printCell(REGF_FILE* f, uint32 offset)
229{
230  char* quoted_buf;
231  uint32 cell_length;
232  bool unalloc;
233
234  if(!regfi_parse_cell(f->fd, offset, NULL, 0, &cell_length, &unalloc))
235    return 1;
236
237  quoted_buf = getQuotedData(f->fd, offset, cell_length);
238  if(quoted_buf == NULL)
239    return 2;
240
241  printf("%.8X,%.8X,RAW,,,,,,,,,,,,%s\n", offset, cell_length, quoted_buf);
242
243  free(quoted_buf);
244  return 0;
245}
246
247
248/* This function returns a properly quoted parent path or partial parent
249 * path for a given key.  Returns NULL on error, "" if no path was available.
250 * Paths returned must be free()d.
251 */
252/* XXX: This is not terribly efficient, as it may reparse many keys
253 *      repeatedly.  Should try to add caching.  Also, piecing the path
254 *      together is slow and redundant.
255 */
256char* getParentPath(REGF_FILE* f, REGF_NK_REC* nk)
257{
258  void_stack* path_stack = void_stack_new(REGF_MAX_DEPTH);
259  REGF_HBIN* hbin;
260  REGF_NK_REC* cur_ancestor;
261  char* ret_val;
262  char* path_element;
263  char* tmp_str;
264  uint32 virt_offset, i, stack_size, ret_val_size, ret_val_left, element_size;
265  uint32 max_length;
266
267  /* The path_stack size limit should guarantee that we don't recurse forever. */
268  virt_offset = nk->parent_off;
269  while(virt_offset != REGF_OFFSET_NONE)
270  { 
271    hbin = regfi_lookup_hbin(f, virt_offset);
272    if(hbin == NULL)
273      virt_offset = REGF_OFFSET_NONE;
274    else
275    {
276      max_length = hbin->block_size + hbin->file_off
277        - (virt_offset+REGF_BLOCKSIZE);
278      cur_ancestor = regfi_parse_nk(f, virt_offset+REGF_BLOCKSIZE, 
279                                    max_length, true);
280      if(cur_ancestor == NULL)
281        virt_offset = REGF_OFFSET_NONE;
282      else
283      {
284        if((cur_ancestor->key_type == NK_TYPE_ROOTKEY1) 
285           || (cur_ancestor->key_type == NK_TYPE_ROOTKEY2))
286          virt_offset = REGF_OFFSET_NONE;
287        else
288          virt_offset = cur_ancestor->parent_off;
289       
290        path_element = quote_string(cur_ancestor->keyname, key_special_chars);
291        if(path_element == NULL || !void_stack_push(path_stack, path_element))
292        {
293          free(cur_ancestor->keyname);
294          free(cur_ancestor);
295          void_stack_free_deep(path_stack);
296          return NULL;
297        }
298
299        regfi_key_free(cur_ancestor);
300      }
301    }
302  }
303 
304  stack_size = void_stack_size(path_stack);
305  ret_val_size = 16*stack_size;
306  if(ret_val_size == 0)
307    ret_val_size = 1;
308  ret_val_left = ret_val_size;
309  ret_val = malloc(ret_val_size);
310  if(ret_val == NULL)
311  {
312    void_stack_free_deep(path_stack);
313    return NULL;
314  }
315  ret_val[0] = '\0';
316
317  for(i=0; i<stack_size; i++)
318  {
319    path_element = void_stack_pop(path_stack);
320    element_size = strlen(path_element);
321    if(ret_val_left < element_size+2)
322    {
323      ret_val_size += element_size+16;
324      ret_val_left += element_size+16;
325      tmp_str = (char*)realloc(ret_val, ret_val_size);
326      if(tmp_str == NULL)
327      {
328        free(ret_val);
329        void_stack_free_deep(path_stack);
330        return NULL;
331      }
332      ret_val = tmp_str;
333    }
334
335    ret_val_left -= snprintf(ret_val+ret_val_size-ret_val_left,ret_val_left, "/%s", path_element);
336    free(path_element);
337  }
338  void_stack_free(path_stack);
339
340  return ret_val;
341}
342
343
344static void usage(void)
345{
346  fprintf(stderr, "Usage: reglookup-recover [options] <REGISTRY_FILE>\n");
347  fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION);
348  fprintf(stderr, "Options:\n");
349  fprintf(stderr, "\t-v\t sets verbose mode.\n");
350  fprintf(stderr, "\t-h\t enables header row. (default)\n");
351  fprintf(stderr, "\t-H\t disables header row.\n");
352  fprintf(stderr, "\t-l\t enables leftover(raw) cell output.\n");
353  fprintf(stderr, "\t-L\t disables leftover(raw) cell output. (default)\n");
354  fprintf(stderr, "\t-r\t enables raw cell output for parsed cells.\n");
355  fprintf(stderr, "\t-R\t disables raw cell output for parsed cells. (default)\n");
356  fprintf(stderr, "\n");
357}
358
359
360bool removeRange(range_list* rl, uint32 offset, uint32 length)
361{
362  int32 rm_idx;
363  const range_list_element* cur_elem;
364
365  rm_idx = range_list_find(rl, offset);
366  if(rm_idx < 0)
367  {
368    fprintf(stderr, "DEBUG: removeRange: rm_idx < 0; (%d)\n", rm_idx);
369    return false;
370  }
371
372  cur_elem = range_list_get(rl, rm_idx);
373  if(cur_elem == NULL)
374  {
375    fprintf(stderr, "DEBUG: removeRange: cur_elem == NULL.  rm_idx=%d\n", rm_idx);
376    return false;
377  }
378
379  if(offset > cur_elem->offset)
380  {
381    if(!range_list_split_element(rl, rm_idx, offset))
382    {
383      fprintf(stderr, "DEBUG: removeRange: first split failed\n");
384      return false;
385    }
386    rm_idx++;
387    cur_elem = range_list_get(rl, rm_idx);
388    if(cur_elem == NULL)
389    {
390      fprintf(stderr, 
391              "DEBUG: removeRange: cur_elem == NULL after first split.  rm_idx=%d\n",
392              rm_idx);
393      return false;
394    }
395  }
396 
397  if(offset+length < cur_elem->offset+cur_elem->length)
398  {
399    if(!range_list_split_element(rl, rm_idx, offset+length))
400    {
401      fprintf(stderr, "DEBUG: removeRange: second split failed\n");
402      return false;
403    }
404  }
405 
406  if(!range_list_remove(rl, rm_idx))
407  {
408    fprintf(stderr, "DEBUG: removeRange: remove failed\n");
409    return false;
410  }
411
412  return true;
413}
414
415
416/* NOTE: unalloc_keys should be an empty range_list. */
417int extractKeys(REGF_FILE* f, 
418                range_list* unalloc_cells, 
419                range_list* unalloc_keys)
420{
421  const range_list_element* cur_elem;
422  REGF_NK_REC* key;
423  uint32 i, j;
424
425  for(i=0; i < range_list_size(unalloc_cells); i++)
426  {
427    cur_elem = range_list_get(unalloc_cells, i);
428    for(j=0; cur_elem->length > REGFI_NK_MIN_LENGTH
429          && j <= cur_elem->length-REGFI_NK_MIN_LENGTH; j+=8)
430    {
431      key = regfi_parse_nk(f, cur_elem->offset+j,
432                           cur_elem->length-j, false);
433      if(key != NULL)
434      {
435        if(!range_list_add(unalloc_keys, key->offset, 
436                           key->cell_size, key))
437        {
438          fprintf(stderr, "ERROR: Couldn't add key to unalloc_keys.\n");
439          return 20;
440        }
441        j+=key->cell_size-8;
442      }
443    }
444  }
445
446  for(i=0; i<range_list_size(unalloc_keys); i++)
447  {
448    cur_elem = range_list_get(unalloc_keys, i);
449    if(!removeRange(unalloc_cells, cur_elem->offset, cur_elem->length))
450      return 30;
451  }
452
453  return 0;
454}
455
456
457int extractValueLists(REGF_FILE* f,
458                      range_list* unalloc_cells,
459                      range_list* unalloc_keys)
460{
461  REGF_NK_REC* nk;
462  REGF_HBIN* hbin;
463  const range_list_element* cur_elem;
464  uint32 i, j, num_keys, off, values_length, max_length;
465
466  num_keys=range_list_size(unalloc_keys);
467  for(i=0; i<num_keys; i++)
468  {
469    cur_elem = range_list_get(unalloc_keys, i);
470    if(cur_elem == NULL)
471      return 10;
472    nk = cur_elem->data;
473
474    if(nk->num_values && (nk->values_off!=REGF_OFFSET_NONE))
475    {
476      hbin = regfi_lookup_hbin(f, nk->values_off);
477     
478      if(hbin != NULL)
479      {
480        off = nk->values_off + REGF_BLOCKSIZE;
481        max_length = hbin->block_size + hbin->file_off - off;
482        /* XXX: This is a hack.  We parse all value-lists, VK records,
483         *      and data records without regard for current allocation status. 
484         *      On the off chance that such a record correctly parsed but is
485         *      actually a reallocated structure used by something else, we
486         *      simply prune it after the fact.  Would be faster to check this
487         *      up front somehow.
488         */
489        nk->values = regfi_load_valuelist(f, off, nk->num_values, max_length,
490                                          false);
491        values_length = (nk->num_values+1)*sizeof(uint32);
492        if(values_length != (values_length & 0xFFFFFFF8))
493          values_length = (values_length & 0xFFFFFFF8) + 8;
494
495        if(nk->values != NULL)
496        {
497          if(!range_list_has_range(unalloc_cells, off, values_length))
498          { /* We've parsed a values-list which isn't in the unallocated list,
499             * so prune it.
500             */
501            for(j=0; j<nk->num_values; j++)
502            {
503              if(nk->values[j] != NULL)
504              {
505                if(nk->values[j]->data != NULL)
506                  free(nk->values[j]->data);
507                free(nk->values[j]);
508              }
509            }
510            free(nk->values);
511            nk->values = NULL;
512          }
513          else
514          { /* Values-list was recovered.  Remove from unalloc_cells and
515             * inspect values.
516             */
517            if(!removeRange(unalloc_cells, off, values_length))
518              return 20;
519
520            for(j=0; j < nk->num_values; j++)
521            {
522              if(nk->values[j] != NULL)
523              {
524                if(!range_list_has_range(unalloc_cells, nk->values[j]->offset, 
525                                         nk->values[j]->cell_size))
526                { /* We've parsed a value which isn't in the unallocated list,
527                   * so prune it.
528                   */
529                  if(nk->values[j]->data != NULL)
530                    free(nk->values[j]->data);
531                  free(nk->values[j]);
532                  nk->values[j] = NULL;
533                }
534                else
535                {
536                  /* A VK record was recovered.  Remove from unalloc_cells
537                   * and inspect data.
538                   */
539                  if(!removeRange(unalloc_cells, nk->values[j]->offset,
540                                  nk->values[j]->cell_size))
541                    return 21;
542
543                  /* Don't bother pruning or removing from unalloc_cells if
544                   * there is no data, or it is stored in the offset.
545                   */
546                  if(nk->values[j]->data != NULL && !nk->values[j]->data_in_offset)
547                  {
548                    off = nk->values[j]->data_off+REGF_BLOCKSIZE;
549                    if(!range_list_has_range(unalloc_cells, off, 
550                                             nk->values[j]->data_size))
551                    { /* We've parsed a data cell which isn't in the unallocated
552                       * list, so prune it.
553                       */
554                      free(nk->values[j]->data);
555                      nk->values[j]->data = NULL;
556                    }
557                    else
558                    { /*A data record was recovered. Remove from unalloc_cells.*/
559                      if(!removeRange(unalloc_cells, off, 
560                                      nk->values[j]->data_size))
561                        return 22;
562                    }
563                  }
564                }
565              }
566            }
567          }
568        }
569      }
570    }
571  }
572
573  return 0;
574}
575
576
577/* NOTE: unalloc_values should be an empty range_list. */
578int extractValues(REGF_FILE* f,
579                  range_list* unalloc_cells,
580                  range_list* unalloc_values)
581{
582  const range_list_element* cur_elem;
583  REGF_VK_REC* vk;
584  uint32 i, j, off;
585
586  for(i=0; i < range_list_size(unalloc_cells); i++)
587  {
588    cur_elem = range_list_get(unalloc_cells, i);
589    for(j=0; j <= cur_elem->length; j+=8)
590    {
591      vk = regfi_parse_vk(f, cur_elem->offset+j, 
592                           cur_elem->length-j, false);
593      if(vk != NULL)
594      {
595        if(!range_list_add(unalloc_values, vk->offset,
596                           vk->cell_size, vk))
597        {
598          fprintf(stderr, "ERROR: Couldn't add value to unalloc_values.\n");
599          return 20;
600        }
601        j+=vk->cell_size-8;
602      }
603    }
604  }
605 
606  /* Remove value ranges from the unalloc_cells before we continue. */
607  for(i=0; i<range_list_size(unalloc_values); i++)
608  {
609    cur_elem = range_list_get(unalloc_values, i);
610    if(!removeRange(unalloc_cells, cur_elem->offset, cur_elem->length))
611      return 30;
612  }
613
614  /* Now see if the data associated with each value is intact */
615  for(i=0; i<range_list_size(unalloc_values); i++)
616  {
617    cur_elem = range_list_get(unalloc_values, i);
618    vk = (REGF_VK_REC*)cur_elem->data;
619    if(vk == NULL)
620      return 40;
621
622    if(vk->data != NULL && !vk->data_in_offset)
623    {
624      off = vk->data_off+REGF_BLOCKSIZE;
625      if(!range_list_has_range(unalloc_cells, off, vk->data_size))
626      { /* We've parsed a data cell which isn't in the unallocated
627         * list, so prune it.
628         */
629        free(vk->data);
630        vk->data = NULL;
631      }
632      else
633      { /*A data record was recovered. Remove from unalloc_cells.*/
634        if(!removeRange(unalloc_cells, off, vk->data_size))
635          return 50;
636      }
637    }
638  }
639
640  return 0;
641}
642
643
644/* NOTE: unalloc_sks should be an empty range_list. */
645int extractSKs(REGF_FILE* f, 
646               range_list* unalloc_cells,
647               range_list* unalloc_sks)
648{
649  const range_list_element* cur_elem;
650  REGF_SK_REC* sk;
651  uint32 i, j;
652
653  for(i=0; i < range_list_size(unalloc_cells); i++)
654  {
655    cur_elem = range_list_get(unalloc_cells, i);
656    for(j=0; j <= cur_elem->length; j+=8)
657    {
658      sk = regfi_parse_sk(f, cur_elem->offset+j, 
659                          cur_elem->length-j, false);
660      if(sk != NULL)
661      {
662        if(!range_list_add(unalloc_sks, sk->offset,
663                           sk->cell_size, sk))
664        {
665          fprintf(stderr, "ERROR: Couldn't add sk to unalloc_sks.\n");
666          return 20;
667        }
668        j+=sk->cell_size-8;
669      }
670    }
671  }
672
673  for(i=0; i<range_list_size(unalloc_sks); i++)
674  {
675    cur_elem = range_list_get(unalloc_sks, i);
676    if(!removeRange(unalloc_cells, cur_elem->offset, cur_elem->length))
677      return 30;
678  }
679
680  return 0;
681}
682
683
684int main(int argc, char** argv)
685{ 
686  REGF_FILE* f;
687  const range_list_element* cur_elem;
688  range_list* unalloc_cells;
689  range_list* unalloc_keys;
690  range_list* unalloc_values;
691  range_list* unalloc_sks;
692  char** parent_paths;
693  char* tmp_name;
694  char* tmp_path;
695  REGF_NK_REC* tmp_key;
696  REGF_VK_REC* tmp_value;
697  uint32 argi, arge, i, j, ret, num_unalloc_keys;
698  /* uint32 test_offset;*/
699 
700  /* Process command line arguments */
701  if(argc < 2)
702  {
703    usage();
704    bailOut(EX_USAGE, "ERROR: Requires at least one argument.\n");
705  }
706 
707  arge = argc-1;
708  for(argi = 1; argi < arge; argi++)
709  {
710    if (strcmp("-v", argv[argi]) == 0)
711      print_verbose = true;
712    else if (strcmp("-h", argv[argi]) == 0)
713      print_header = true;
714    else if (strcmp("-H", argv[argi]) == 0)
715      print_header = false;
716    else if (strcmp("-l", argv[argi]) == 0)
717      print_leftover = true;
718    else if (strcmp("-L", argv[argi]) == 0)
719      print_leftover = false;
720    else if (strcmp("-r", argv[argi]) == 0)
721      print_parsedraw = true;
722    else if (strcmp("-R", argv[argi]) == 0)
723      print_parsedraw = false;
724    else
725    {
726      usage();
727      fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
728      bailOut(EX_USAGE, "");
729    }
730  }
731  /*test_offset = strtol(argv[argi++], NULL, 16);*/
732
733  if((registry_file = strdup(argv[argi])) == NULL)
734    bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
735
736  f = regfi_open(registry_file);
737  if(f == NULL)
738  {
739    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
740    bailOut(EX_NOINPUT, "");
741  }
742
743  if(print_header)
744    printf("OFFSET,REC_LENGTH,REC_TYPE,PATH,NAME,"
745           "NK_MTIME,NK_NVAL,VK_TYPE,VK_VALUE,VK_DATA_LEN,"
746           "SK_OWNER,SK_GROUP,SK_SACL,SK_DACL,RAW_CELL\n");
747
748  unalloc_cells = regfi_parse_unalloc_cells(f);
749  if(unalloc_cells == NULL)
750  {
751    fprintf(stderr, "ERROR: Could not obtain list of unallocated cells.\n");
752    return 1;
753  }
754
755  unalloc_keys = range_list_new();
756  if(unalloc_keys == NULL)
757    return 10;
758
759  unalloc_values = range_list_new();
760  if(unalloc_values == NULL)
761    return 10;
762
763  unalloc_sks = range_list_new();
764  if(unalloc_sks == NULL)
765    return 10;
766
767  ret = extractKeys(f, unalloc_cells, unalloc_keys);
768  if(ret != 0)
769  {
770    fprintf(stderr, "ERROR: extractKeys() failed with %d.\n", ret);
771    return ret;
772  }
773
774  ret = extractValueLists(f, unalloc_cells, unalloc_keys);
775  if(ret != 0)
776  {
777    fprintf(stderr, "ERROR: extractValueLists() failed with %d.\n", ret);
778    return ret;
779  }
780
781  /* Carve any orphan values and associated data */
782  ret = extractValues(f, unalloc_cells, unalloc_values);
783  if(ret != 0)
784  {
785    fprintf(stderr, "ERROR: extractValues() failed with %d.\n", ret);
786    return ret;
787  }
788
789  /* Carve any SK records */
790  ret = extractSKs(f, unalloc_cells, unalloc_sks);
791  if(ret != 0)
792  {
793    fprintf(stderr, "ERROR: extractSKs() failed with %d.\n", ret);
794    return ret;
795  }
796
797  /* Now that we're done carving, associate recovered keys with parents,
798   * if at all possible.
799   */
800  num_unalloc_keys = range_list_size(unalloc_keys);
801  parent_paths = (char**)malloc(sizeof(char*)*num_unalloc_keys);
802  if(parent_paths == NULL)
803    return 10;
804
805  for(i=0; i < num_unalloc_keys; i++)
806  {
807    cur_elem = range_list_get(unalloc_keys, i);
808    tmp_key = (REGF_NK_REC*)cur_elem->data;
809
810    if(tmp_key == NULL)
811      return 20;
812   
813    parent_paths[i] = getParentPath(f, tmp_key);
814    if(parent_paths[i] == NULL)
815      return 20;
816  }
817 
818  /* Now start the output */
819
820  for(i=0; i < num_unalloc_keys; i++)
821  {
822    cur_elem = range_list_get(unalloc_keys, i);
823    tmp_key = (REGF_NK_REC*)cur_elem->data;
824
825    printKey(f, tmp_key, parent_paths[i]);
826    if(tmp_key->num_values > 0 && tmp_key->values != NULL)
827    {
828      tmp_name = quote_string(tmp_key->keyname, key_special_chars);
829      tmp_path = (char*)malloc(strlen(parent_paths[i])+strlen(tmp_name)+2);
830      if(tmp_path == NULL)
831        return 10;
832      sprintf(tmp_path, "%s/%s", parent_paths[i], tmp_name);
833      for(j=0; j < tmp_key->num_values; j++)
834      {
835        tmp_value = tmp_key->values[j];
836        if(tmp_value != NULL)
837          printValue(f, tmp_value, tmp_path);
838      }
839      free(tmp_path);
840      free(tmp_name);
841      free(parent_paths[i]);
842    }
843  }
844  free(parent_paths);
845
846  /* Print out orphaned values */
847  for(i=0; i < range_list_size(unalloc_values); i++)
848  {
849    cur_elem = range_list_get(unalloc_values, i);
850    tmp_value = (REGF_VK_REC*)cur_elem->data; 
851
852    printValue(f, tmp_value, "");
853  }
854 
855  if(print_leftover)
856  {
857    for(i=0; i < range_list_size(unalloc_cells); i++)
858    {
859      cur_elem = range_list_get(unalloc_cells, i);
860      printCell(f, cur_elem->offset);
861    }
862  }
863
864  return 0;
865}
Note: See TracBrowser for help on using the repository browser.