Changeset 81 for trunk/src


Ignore:
Timestamp:
01/17/07 11:47:39 (18 years ago)
Author:
tim
Message:

Finished incorporating changes to reglookup to work with new regfi interface.

Compiles now, but is minimally tested.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/reglookup.c

    r80 r81  
    33 * Gerald Carter''s regfio interface.
    44 *
    5  * Copyright (C) 2005-2006 Timothy D. Morgan
     5 * Copyright (C) 2005-2007 Timothy D. Morgan
    66 * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com
    77 *
     
    382382
    383383
    384 void_stack* path2Stack(const char* s)
    385 {
    386   void_stack* ret_val;
    387   void_stack* rev_ret = void_stack_new(REGF_MAX_DEPTH);
     384char** splitPath(const char* s)
     385{
     386  char** ret_val;
    388387  const char* cur = s;
    389388  char* next = NULL;
    390389  char* copy;
    391 
    392   if (rev_ret == NULL)
     390  uint32 ret_cur = 0;
     391
     392  ret_val = (char**)malloc((REGF_MAX_DEPTH+1+1)*sizeof(char**));
     393  if (ret_val == NULL)
    393394    return NULL;
     395  ret_val[0] = NULL;
     396
     397  /* We return a well-formed, 0-length, path even when input is icky. */
    394398  if (s == NULL)
    395     return rev_ret;
     399    return ret_val;
    396400 
    397401  while((next = strchr(cur, '/')) != NULL)
     
    405409      memcpy(copy, cur, next-cur);
    406410      copy[next-cur] = '\0';
    407       if(!void_stack_push(rev_ret, copy))
     411      ret_val[ret_cur++] = copy;
     412      if(ret_cur < (REGF_MAX_DEPTH+1+1))
     413        ret_val[ret_cur] = NULL;
     414      else
    408415        bailOut(2, "ERROR: Registry maximum depth exceeded.\n");
    409416    }
    410417    cur = next+1;
    411418  }
     419
     420  /* Grab last element, if path doesn't end in '/'. */
    412421  if(strlen(cur) > 0)
    413422  {
    414423    copy = strdup(cur);
    415     if(!void_stack_push(rev_ret, copy))
     424    ret_val[ret_cur++] = copy;
     425    if(ret_cur < (REGF_MAX_DEPTH+1+1))
     426      ret_val[ret_cur] = NULL;
     427    else
    416428      bailOut(2, "ERROR: Registry maximum depth exceeded.\n");
    417429  }
    418430
    419   ret_val = void_stack_copy_reverse(rev_ret);
    420   void_stack_free(rev_ret);
    421 
    422431  return ret_val;
    423432}
    424433
    425 /* Returns a quoted path from an nk_stack */
    426 char* stack2Path(void_stack* nk_stack)
    427 {
    428   const REGF_NK_REC* cur;
     434
     435/* Returns a quoted path from an iterator's stack */
     436/* XXX: Some way should be found to integrate this into regfi's API
     437 *      The problem is that the escaping is sorta reglookup-specific.
     438 */
     439char* iter2Path(REGFI_ITERATOR* i)
     440{
     441  const REGFI_ITER_POSITION* cur;
    429442  uint32 buf_left = 127;
    430443  uint32 buf_len = buf_left+1;
    431444  uint32 name_len = 0;
    432445  uint32 grow_amt;
    433   char* buf; 
     446  char* buf;
    434447  char* new_buf;
    435448  char* name;
     449  const char* cur_name;
    436450  void_stack_iterator* iter;
    437451 
     
    441455  buf[0] = '\0';
    442456
    443   iter = void_stack_iterator_new(nk_stack);
     457  iter = void_stack_iterator_new(i->key_positions);
    444458  if (iter == NULL)
    445459  {
     
    449463
    450464  /* skip root element */
     465  if(void_stack_size(i->key_positions) < 1)
     466  {
     467    buf[0] = '/';
     468    buf[1] = '\0';
     469    return buf;
     470  }
    451471  cur = void_stack_iterator_next(iter);
    452472
    453   while((cur = void_stack_iterator_next(iter)) != NULL)
    454   {
     473  do
     474  {
     475    cur = void_stack_iterator_next(iter);
     476    if (cur == NULL)
     477      cur_name = i->cur_key->keyname;
     478    else
     479      cur_name = cur->nk->keyname;
     480
    455481    buf[buf_len-buf_left-1] = '/';
    456482    buf_left -= 1;
    457     name = quote_string(cur->keyname, key_special_chars);
     483    name = quote_string(cur_name, key_special_chars);
    458484    name_len = strlen(name);
    459485    if(name_len+1 > buf_left)
     
    474500    buf[buf_len-buf_left-1] = '\0';
    475501    free(name);
    476   }
     502  } while(cur != NULL);
    477503
    478504  return buf;
     
    572598
    573599
    574 void printValueList(REGFI_ITERATOR i, char* prefix)
     600void printValueList(REGFI_ITERATOR* i, char* prefix)
    575601{
    576602  REGF_VK_REC* value;
     
    578604  value = regfi_iterator_first_value(i);
    579605  while(value != NULL)
    580     if(!type_filter_enabled || (value.type == type_filter))
     606  {
     607    if(!type_filter_enabled || (value->type == type_filter))
    581608      printValue(value, prefix);
     609    value = regfi_iterator_next_value(i);
     610  }
    582611}
    583612
     
    630659
    631660
    632 void printKeyTree(REGF_FILE* f, void_stack* nk_stack, const char* prefix)
    633 {
     661void printKeyTree(REGFI_ITERATOR* iter)
     662{
     663  REGF_NK_REC* root = NULL;
    634664  REGF_NK_REC* cur = NULL;
    635665  REGF_NK_REC* sub = NULL;
    636666  char* path = NULL;
    637   char* val_path = NULL;
    638   uint32 val_path_len = 0;
    639   uint32 path_len = 0;
    640   uint32 prefix_len = strlen(prefix);
    641667  int key_type = regfi_type_str2val("KEY");
     668  bool print_this = true;
     669
     670  root = cur = regfi_iterator_cur_key(iter);
     671  sub = regfi_iterator_first_subkey(iter);
    642672 
    643   if((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
    644   {
    645     cur->subkey_index = 0;
    646     path = stack2Path(nk_stack);
    647 
    648     if(print_verbose)
    649     {
    650       if(prefix[0] == '\0')
    651         fprintf(stderr, "VERBOSE: Printing key tree under path: /\n");
    652       else
    653         fprintf(stderr, "VERBOSE: Printing key tree under path: %s\n",
    654                 prefix);
    655     }
    656 
    657     path_len = strlen(path);
    658     val_path_len = prefix_len+path_len;
    659     val_path = (char*)malloc(val_path_len+1+1);
    660     if(val_path == NULL)
    661       bailOut(2, "ERROR: Could not allocate val_path.\n");
    662 
    663     strcpy(val_path, prefix);
    664     strcpy(val_path+prefix_len, path);
    665     if(val_path[0] == '\0')
    666     {
    667       val_path[0] = '/';
    668       val_path[1] = '\0';
    669     }
    670     if(!type_filter_enabled || (key_type == type_filter))
    671       printKey(cur, val_path);
    672     if(!type_filter_enabled || (key_type != type_filter))
    673       printValueList(cur, val_path);
     673  if(root == NULL)
     674    bailOut(3, "ERROR: root cannot be NULL.\n");
     675 
     676  do
     677  {
     678    if(print_this)
     679    {
     680      path = iter2Path(iter);
     681      if(path == NULL)
     682        bailOut(2, "ERROR: Could not construct iterator's path.\n");
     683     
     684      if(!type_filter_enabled || (key_type == type_filter))
     685        printKey(cur, path);
     686      if(!type_filter_enabled || (key_type != type_filter))
     687        printValueList(iter, path);
     688     
     689      free(path);
     690    }
    674691   
    675     while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
    676     {
    677       if((sub = regfi_fetch_subkey(f, cur)) == NULL)
     692    if(sub == NULL)
     693    {
     694      if(cur != root)
    678695      {
    679         sub = void_stack_pop(nk_stack);
    680         /* XXX: This is just a shallow free.  Need to write deep free
    681          * routines to replace the Samba code for this.
    682          */
    683         if(sub != NULL)
    684           free(sub);
     696        /* We're done with this sub-tree, going up and hitting other branches. */
     697        if(!regfi_iterator_up(iter))
     698          bailOut(3, "ERROR: could not traverse iterator upward.\n");
     699       
     700        cur = regfi_iterator_cur_key(iter);
     701        if(cur == NULL)
     702          bailOut(3, "ERROR: unexpected NULL for key.\n");
     703       
     704        sub = regfi_iterator_next_subkey(iter);
    685705      }
    686       else
    687       {
    688         sub->subkey_index = 0;
    689         if(!void_stack_push(nk_stack, sub))
    690           bailOut(2, "ERROR: Registry maximum depth exceeded.\n");
    691         path = stack2Path(nk_stack);
    692         if(path != NULL)
    693         {
    694           path_len = strlen(path);
    695           if(val_path_len < prefix_len+path_len)
    696           {
    697             val_path_len = prefix_len+path_len;
    698             val_path = (char*)realloc(val_path, val_path_len+1);
    699             if(val_path == NULL)
    700               bailOut(2, "ERROR: Could not reallocate val_path.\n");
    701           }
    702           strcpy(val_path, prefix);
    703           strcpy(val_path+prefix_len, path);
    704           if(!type_filter_enabled || (key_type == type_filter))
    705             printKey(sub, val_path);
    706           if(!type_filter_enabled || (key_type != type_filter))
    707             printValueList(sub, val_path);
    708         }
    709       }
    710     }
    711   }
    712   if(val_path != NULL)
    713     free(val_path);
     706      print_this = false;
     707    }
     708    else
     709    { /* We have unexplored sub-keys. 
     710       * Let's move down and print this first sub-tree out.
     711       */
     712      if(!regfi_iterator_down(iter))
     713        bailOut(3, "ERROR: could not traverse iterator downward.\n");
     714
     715      cur = sub;
     716      sub = regfi_iterator_first_subkey(iter);
     717      print_this = true;
     718    }
     719  } while(!((cur == root) && (sub == NULL)));
     720
    714721  if(print_verbose)
    715722    fprintf(stderr, "VERBOSE: Finished printing key tree.\n");
    716723}
     724
    717725
    718726/*
     
    724732int retrievePath(REGFI_ITERATOR* iter, char** path)
    725733{
    726   REG_VK_REC* value;
     734  REGF_VK_REC* value;
     735  char* tmp_path_joined;
     736  const char** tmp_path;
    727737  uint32 i;
    728   char* p;
    729   char* tmp_path_joined;
    730   char** tmp_path;
    731738 
    732739  if(path == NULL)
     
    734741
    735742  /* One extra for any value at the end, and one more for NULL */
    736   tmp_path = (char**)malloc(sizeof(char**)*(REGF_MAX_DEPTH+1+1));
     743  tmp_path = (const char**)malloc(sizeof(const char**)*(REGF_MAX_DEPTH+1+1));
    737744  if(tmp_path == NULL)
    738745    return -2;
     
    763770
    764771    value = regfi_iterator_cur_value(iter);
    765     tmp_path_joined = joinPath(tmp_path);
     772    tmp_path_joined = iter2Path(iter);
    766773
    767774    if((value == NULL) || (tmp_path_joined == NULL))
     
    808815int main(int argc, char** argv)
    809816{
    810   void_stack* path_stack;
    811817  char** path = NULL;
    812818  REGF_FILE* f;
    813   REGF_NK_REC* root;
    814819  REGFI_ITERATOR* iter;
    815820  int retr_path_ret;
     
    883888    bailOut(3, "ERROR: Couldn't create registry iterator.\n");
    884889
     890  if(print_header)
     891  {
     892    if(print_security)
     893      printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL\n");
     894    else
     895      printf("PATH,TYPE,VALUE,MTIME\n");
     896  }
     897
    885898  if(path_filter_enabled && path_filter != NULL)
    886899    path = splitPath(path_filter);
    887  
     900
    888901  if(path != NULL)
    889902  {
     
    892905      fprintf(stderr, "WARNING: specified path not found.\n");
    893906    else if (retr_path_ret == 2)
    894       printKeyTree(iter, path_filter);
     907      printKeyTree(iter);
    895908    else if(retr_path_ret != 0)
    896909      bailOut(4, "ERROR: Unknown error occurred in retrieving path.\n");
    897910  }
    898911  else
    899     printKeyTree(iter, "");
     912    printKeyTree(iter);
    900913
    901914  regfi_iterator_free(iter);
Note: See TracChangeset for help on using the changeset viewer.