Changeset 66


Ignore:
Timestamp:
07/23/06 18:46:15 (18 years ago)
Author:
tim
Message:

fixed quoting bug in key names

fixed unicode decoding and string display bugs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/reglookup.c

    r61 r66  
    4343
    4444/* Other globals */
    45 const char* special_chars = ",\"\\";
     45const char* key_special_chars = ",\"\\/";
     46const char* subfield_special_chars = ",\"\\|";
     47const char* common_special_chars = ",\"\\";
     48
    4649iconv_t conv_desc;
    4750
     
    150153  {
    151154    iconv_close(conv_desc);
    152     return -1;
    153   }
     155    return -errno;
     156  }
     157  *outbuf = '\0';
    154158
    155159  iconv_close(conv_desc); 
     
    173177  unsigned int ascii_max, cur_str_max;
    174178  unsigned int str_rem, cur_str_rem, alen;
     179  int ret_err;
    175180
    176181  switch (type)
    177182  {
     183  case REG_SZ:
     184  case REG_EXPAND_SZ:
    178185    /* REG_LINK is a symbolic link, stored as a unicode string. */
    179186  case REG_LINK:
    180   case REG_SZ:
    181187    ascii_max = sizeof(char)*len;
    182188    ascii = malloc(ascii_max+4);
     
    184190      return NULL;
    185191   
    186     if(uni_to_ascii(datap, ascii, len, ascii_max) < 0)
    187     {
    188       free(ascii);
    189       return NULL;
    190     }
    191     cur_quoted = quote_string(ascii, special_chars);
    192     free(ascii);
    193     return cur_quoted;
    194     break;
    195 
    196     /* XXX: This could be Unicode or ASCII.  Are we processing it
    197      *      correctly in both cases?
     192    /* Sometimes values have binary stored in them.  If the unicode
     193     * conversion fails, just quote it raw.
    198194     */
    199   case REG_EXPAND_SZ:
    200     ascii_max = sizeof(char)*len;
    201     ascii = malloc(ascii_max+2);
    202     if(ascii == NULL)
    203       return NULL;
    204 
    205     if(uni_to_ascii(datap, ascii, len, ascii_max) < 0)
    206     {
    207       free(ascii);
    208       return NULL;
    209     }
    210     cur_quoted = quote_string(ascii, special_chars);
     195   
     196    ret_err = uni_to_ascii(datap, ascii, len, ascii_max);
     197    if(ret_err < 0)
     198    {
     199      /* XXX: It would be nice if somehow we could include the name of this
     200       *      value in the error message.
     201       */
     202      if(print_verbose)
     203        fprintf(stderr, "VERBOSE: Unicode conversion failed; "
     204                "printing as binary.  Error: %s\n", strerror(-ret_err));
     205
     206      cur_quoted = quote_buffer(datap, len, common_special_chars);
     207    }
     208    else
     209      cur_quoted = quote_string(ascii, common_special_chars);
    211210    free(ascii);
    212211    return cur_quoted;
     
    269268      if(num_nulls == 2)
    270269      {
    271         if(uni_to_ascii(cur_str, cur_ascii, cur_str_max, cur_str_max) < 0)
     270        ret_err = uni_to_ascii(cur_str, cur_ascii, cur_str_len-1, cur_str_max);
     271        if(ret_err < 0)
    272272        {
    273           free(ascii);
    274           free(cur_ascii);
    275           free(cur_str);
    276           return NULL;
     273          /* XXX: It would be nice if somehow we could include the name of this
     274           *      value in the error message.
     275           */
     276          if(print_verbose)
     277            fprintf(stderr, "VERBOSE: Unicode conversion failed on sub-field; "
     278                    "printing as binary.  Error: %s\n", strerror(-ret_err));
     279         
     280          cur_quoted = quote_buffer(cur_str, cur_str_len-1,
     281                                    subfield_special_chars);
    277282        }
    278 
    279         cur_quoted = quote_string(cur_ascii, ",|\"\\");
     283        else
     284          cur_quoted = quote_string(cur_ascii, subfield_special_chars);
     285
    280286        alen = snprintf(asciip, str_rem, "%s", cur_quoted);
    281287        asciip += alen;
     
    315321
    316322  case REG_BINARY:
    317     return quote_buffer(datap, len, special_chars);
     323    return quote_buffer(datap, len, common_special_chars);
    318324    break;
    319325
     
    366372}
    367373
    368 
     374/* Returns a quoted path from an nk_stack */
    369375char* stack2Path(void_stack* nk_stack)
    370376{
     
    376382  char* buf;
    377383  char* new_buf;
     384  char* name;
    378385  void_stack_iterator* iter;
    379386 
     
    397404    buf[buf_len-buf_left-1] = '/';
    398405    buf_left -= 1;
    399     name_len = strlen(cur->keyname);
     406    name = quote_string(cur->keyname, key_special_chars);
     407    name_len = strlen(name);
    400408    if(name_len+1 > buf_left)
    401409    {
     
    411419      buf_left = grow_amt + name_len + 1;
    412420    }
    413     strncpy(buf+(buf_len-buf_left-1), cur->keyname, name_len);
     421    strncpy(buf+(buf_len-buf_left-1), name, name_len);
    414422    buf_left -= name_len;
    415423    buf[buf_len-buf_left-1] = '\0';
     424    free(name);
    416425  }
    417426
     
    424433  uint32 size;
    425434  uint8 tmp_buf[4];
    426   char* quoted_value;
    427   char* quoted_prefix;
    428   char* quoted_name;
     435  char* quoted_value = NULL;
     436  char* quoted_name = NULL;
    429437
    430438  /* Thanks Microsoft for making this process so straight-forward!!! */
     
    460468   *      for that condition.
    461469   */
    462   quoted_prefix = quote_string(prefix, special_chars);
    463   quoted_name = quote_string(vk->valuename, special_chars);
    464  
     470  quoted_name = quote_string(vk->valuename, common_special_chars);
    465471  if(print_security)
    466     printf("%s/%s,%s,%s,,,,,\n", quoted_prefix, quoted_name,
     472    printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name,
    467473           regfio_type_val2str(vk->type), quoted_value);
    468474  else
    469     printf("%s/%s,%s,%s,\n", quoted_prefix, quoted_name,
     475    printf("%s/%s,%s,%s,\n", prefix, quoted_name,
    470476           regfio_type_val2str(vk->type), quoted_value);
    471477 
    472478  if(quoted_value != NULL)
    473479    free(quoted_value);
    474   if(quoted_prefix != NULL)
    475     free(quoted_prefix);
    476480  if(quoted_name != NULL)
    477481    free(quoted_name);
     
    485489  for(i=0; i < nk->num_values; i++)
    486490    if(!type_filter_enabled || (nk->values[i].type == type_filter))
    487       printValue(&nk->values[i], prefix);
     491      printValue(nk->values+i, prefix);
    488492}
    489493
     
    499503  time_t tmp_time[1];
    500504  struct tm* tmp_time_s = NULL;
    501   char* quoted_path = NULL;
    502 
    503   /* XXX: it would be faster to always pass a quoted string to this
    504    *      function, instead of re-quoting it every call. */
    505   /* XXX: check return of quote_string */
    506   quoted_path = quote_string(full_path, special_chars);
     505
    507506  *tmp_time = nt_time_to_unix(&k->mtime);
    508507  tmp_time_s = gmtime(tmp_time);
     
    524523      dacl = empty_str;
    525524
    526     printf("%s,KEY,,%s,%s,%s,%s,%s\n", quoted_path, mtime,
     525    printf("%s,KEY,,%s,%s,%s,%s,%s\n", full_path, mtime,
    527526           owner, group, sacl, dacl);
    528527
     
    537536  }
    538537  else
    539     printf("%s,KEY,,%s\n", quoted_path, mtime);
    540 
    541   free(quoted_path);
    542 }
    543 
    544 
    545 /* XXX: this function is awful.  Needs to be re-designed. */
    546 void printKeyTree(REGF_FILE* f, void_stack* nk_stack, char* prefix)
     538    printf("%s,KEY,,%s\n", full_path, mtime);
     539}
     540
     541
     542void printKeyTree(REGF_FILE* f, void_stack* nk_stack, const char* prefix)
    547543{
    548544  REGF_NK_REC* cur = NULL;
     
    550546  char* path = NULL;
    551547  char* val_path = NULL;
     548  uint32 val_path_len = 0;
     549  uint32 path_len = 0;
     550  uint32 prefix_len = strlen(prefix);
    552551  int key_type = regfio_type_str2val("KEY");
    553552 
     
    566565    }
    567566
    568     val_path = (char*)malloc(strlen(prefix)+strlen(path)+1);
    569     sprintf(val_path, "%s%s", prefix, path);
     567    path_len = strlen(path);
     568    val_path_len = prefix_len+path_len;
     569    val_path = (char*)malloc(val_path_len+1+1);
     570    if(val_path == NULL)
     571      bailOut(2, "ERROR: Could not allocate val_path.\n");
     572
     573    strcpy(val_path, prefix);
     574    strcpy(val_path+prefix_len, path);
    570575    if(val_path[0] == '\0')
    571576    {
     
    573578      val_path[1] = '\0';
    574579    }
    575 
    576580    if(!type_filter_enabled || (key_type == type_filter))
    577581      printKey(cur, val_path);
    578582    if(!type_filter_enabled || (key_type != type_filter))
    579583      printValueList(cur, val_path);
    580     if(val_path != NULL)
    581       free(val_path);
     584   
    582585    while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)
    583586    {
    584       if((sub = regfio_fetch_subkey(f, cur)) != NULL)
     587      if((sub = regfio_fetch_subkey(f, cur)) == NULL)
     588      {
     589        sub = void_stack_pop(nk_stack);
     590        /* XXX: This is just a shallow free.  Need to write deep free
     591         * routines to replace the Samba code for this.
     592         */
     593        if(sub != NULL)
     594          free(sub);
     595      }
     596      else
    585597      {
    586598        sub->subkey_index = 0;
     
    589601        if(path != NULL)
    590602        {
    591           val_path = (char*)malloc(strlen(prefix)+strlen(path)+1);
    592           sprintf(val_path, "%s%s", prefix, path);
     603          path_len = strlen(path);
     604          if(val_path_len < prefix_len+path_len)
     605          {
     606            val_path_len = prefix_len+path_len;
     607            val_path = (char*)realloc(val_path, val_path_len+1);
     608            if(val_path == NULL)
     609              bailOut(2, "ERROR: Could not reallocate val_path.\n");
     610          }
     611          strcpy(val_path, prefix);
     612          strcpy(val_path+prefix_len, path);
    593613          if(!type_filter_enabled || (key_type == type_filter))
    594614            printKey(sub, val_path);
    595615          if(!type_filter_enabled || (key_type != type_filter))
    596616            printValueList(sub, val_path);
    597           if(val_path != NULL)
    598             free(val_path);
    599617        }
    600618      }
    601       else
    602       {
    603         cur = void_stack_pop(nk_stack);
    604         /* XXX: This is just a shallow free.  Need to write deep free
    605          * routines to replace the Samba code for this.
    606          */
    607         if(cur != NULL)
    608           free(cur);
    609       }
    610     }
    611   }
     619    }
     620  }
     621  if(val_path != NULL)
     622    free(val_path);
    612623  if(print_verbose)
    613624    fprintf(stderr, "VERBOSE: Finished printing key tree.\n");
     
    627638  void_stack* sub_nk_stack;
    628639  char* prefix;
    629   uint32 prefix_len;
    630640  char* cur_str = NULL;
    631641  char* path = NULL;
     642  char* name;
     643  uint16 path_depth;
     644  uint32 i, prefix_len;
    632645  bool found_cur = true;
    633   uint32 i;
    634   uint16 path_depth;
    635646  if(path_stack == NULL)
    636647    return -1;
     
    715726      if(prefix == NULL)
    716727        return -1;
     728      name = quote_string(sub->keyname, key_special_chars);
    717729      strcat(prefix, "/");
    718       strcat(prefix, sub->keyname);
     730      strcat(prefix, name);
     731      free(name);
    719732
    720733      if(print_verbose)
Note: See TracChangeset for help on using the changeset viewer.