Changeset 182 for trunk/lib


Ignore:
Timestamp:
03/17/10 02:41:17 (15 years ago)
Author:
tim
Message:

redesigned regfi logging API to utilize thread-local storage

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/regfi.c

    r181 r182  
    4545
    4646
     47
    4748/******************************************************************************
    4849 ******************************************************************************/
    49 void regfi_add_message(REGFI_FILE* file, uint16_t msg_type, const char* fmt, ...)
    50 {
    51   /* XXX: This function is not particularly efficient,
    52    *      but then it is mostly used during errors.
     50void regfi_log_start(uint16_t msg_mask)
     51{
     52  REGFI_LOG* log_info = talloc(NULL, REGFI_LOG);
     53  if(log_info == NULL)
     54    return;
     55
     56  log_info->msg_mask = msg_mask;
     57  log_info->messages = NULL;
     58
     59  /* XXX: Should we bother with a destructor here? */
     60  if(pthread_key_create(&REGFI_LOG_KEY, NULL) != 0)
     61  {
     62    fprintf(stderr, "ERROR: key_create\n");
     63    goto fail;
     64  }
     65
     66  if(pthread_setspecific(REGFI_LOG_KEY, log_info) != 0)
     67  {
     68    fprintf(stderr, "ERROR: setspecific\n");
     69    goto fail;
     70  }
     71
     72  return;
     73
     74 fail:
     75  talloc_free(log_info);
     76}
     77
     78
     79/******************************************************************************
     80 ******************************************************************************/
     81void regfi_log_add(uint16_t msg_type, const char* fmt, ...)
     82{
     83  /* XXX: Switch internal storage over to a linked list or stack.
     84   *      Then add a regfi_log_get function that returns the list in some
     85   *      convenient, user-friendly data structure.  regfi_log_get_str should
     86   *      stick around and will simply smush the list into a big string when
     87   *      it's called, rather than having messages smushed when they're first
     88   *      written to the log.
    5389   */
    5490  uint32_t buf_size, buf_used;
    5591  char* new_msg;
     92  REGFI_LOG* log_info;
    5693  va_list args;
    5794
    58   if((file->msg_mask & msg_type) != 0)
    59   {
    60     if(file->last_message == NULL)
    61       buf_used = 0;
    62     else
    63       buf_used = strlen(file->last_message);
    64    
    65     buf_size = buf_used+strlen(fmt)+160;
    66     new_msg = realloc(file->last_message, buf_size);
    67     if(new_msg == NULL)
    68       /* XXX: should we report this? */
    69       return;
    70 
    71     switch (msg_type)
    72     {
    73     case REGFI_MSG_INFO:
    74       strcpy(new_msg+buf_used, "INFO: ");
    75       buf_used += 6;
    76       break;
    77     case REGFI_MSG_WARN:
    78       strcpy(new_msg+buf_used, "WARN: ");
    79       buf_used += 6;
    80       break;
    81     case REGFI_MSG_ERROR:
    82       strcpy(new_msg+buf_used, "ERROR: ");
    83       buf_used += 7;
    84       break;
    85     }
    86 
    87     va_start(args, fmt);
    88     vsnprintf(new_msg+buf_used, buf_size-buf_used, fmt, args);
    89     va_end(args);
    90     strncat(new_msg, "\n", buf_size-1);
    91    
    92     file->last_message = new_msg;
    93   }
     95  log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
     96  if(log_info == NULL || (log_info->msg_mask & msg_type) == 0)
     97    return;
     98
     99  if(log_info->messages == NULL)
     100    buf_used = 0;
     101  else
     102    buf_used = strlen(log_info->messages);
     103 
     104  buf_size = buf_used+strlen(fmt)+160;
     105  new_msg = realloc(log_info->messages, buf_size);
     106  if(new_msg == NULL)
     107    /* XXX: should we report this? */
     108    return;
     109 
     110  switch (msg_type)
     111  {
     112  case REGFI_LOG_INFO:
     113    strcpy(new_msg+buf_used, "INFO: ");
     114    buf_used += 6;
     115    break;
     116  case REGFI_LOG_WARN:
     117    strcpy(new_msg+buf_used, "WARN: ");
     118    buf_used += 6;
     119    break;
     120  case REGFI_LOG_ERROR:
     121    strcpy(new_msg+buf_used, "ERROR: ");
     122    buf_used += 7;
     123    break;
     124  }
     125 
     126  va_start(args, fmt);
     127  vsnprintf(new_msg+buf_used, buf_size-buf_used, fmt, args);
     128  va_end(args);
     129  strncat(new_msg, "\n", buf_size-1);
     130 
     131  log_info->messages = new_msg;
    94132}
    95133
     
    97135/******************************************************************************
    98136 ******************************************************************************/
    99 char* regfi_get_messages(REGFI_FILE* file)
    100 {
    101   char* ret_val = file->last_message;
    102   file->last_message = NULL;
     137char* regfi_log_get_str()
     138{
     139  char* ret_val;
     140  REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
     141  if(log_info == NULL)
     142    return NULL;
     143
     144  ret_val = log_info->messages;
     145  log_info->messages = NULL;
    103146
    104147  return ret_val;
     
    106149
    107150
    108 void regfi_set_message_mask(REGFI_FILE* file, uint16_t mask)
    109 {
    110   file->msg_mask = mask;
     151/******************************************************************************
     152 ******************************************************************************/
     153void regfi_log_set_mask(uint16_t msg_mask)
     154{
     155  REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
     156  if(log_info == NULL)
     157    return;
     158
     159  log_info->msg_mask = msg_mask;
     160}
     161
     162
     163/******************************************************************************
     164 ******************************************************************************/
     165void regfi_log_stop()
     166{
     167  REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
     168  if(log_info == NULL)
     169    return;
     170 
     171  if(log_info->messages != NULL)
     172    free(log_info->messages);
     173
     174  pthread_key_delete(REGFI_LOG_KEY);
     175  talloc_free(log_info);
    111176}
    112177
     
    427492  if(lock_ret != 0)
    428493  {
    429     regfi_add_message(file, REGFI_MSG_ERROR, "Error obtaining read lock in"
     494    regfi_log_add(REGFI_LOG_ERROR, "Error obtaining read lock in"
    430495                      "%s due to: %s\n", context, strerror(lock_ret));
    431496    return false;
     
    441506  if(lock_ret != 0)
    442507  {
    443     regfi_add_message(file, REGFI_MSG_ERROR, "Error obtaining write lock in"
     508    regfi_log_add(REGFI_LOG_ERROR, "Error obtaining write lock in"
    444509                      "%s due to: %s\n", context, strerror(lock_ret));
    445510    return false;
     
    455520  if(lock_ret != 0)
    456521  {
    457     regfi_add_message(file, REGFI_MSG_ERROR, "Error releasing lock in"
     522    regfi_log_add(REGFI_LOG_ERROR, "Error releasing lock in"
    458523                      "%s due to: %s\n", context, strerror(lock_ret));
    459524    return false;
     
    469534  if(lock_ret != 0)
    470535  {
    471     regfi_add_message(file, REGFI_MSG_ERROR, "Error obtaining mutex lock in"
     536    regfi_log_add(REGFI_LOG_ERROR, "Error obtaining mutex lock in"
    472537                      "%s due to: %s\n", context, strerror(lock_ret));
    473538    return false;
     
    483548  if(lock_ret != 0)
    484549  {
    485     regfi_add_message(file, REGFI_MSG_ERROR, "Error releasing mutex lock in"
     550    regfi_log_add(REGFI_LOG_ERROR, "Error releasing mutex lock in"
    486551                      "%s due to: %s\n", context, strerror(lock_ret));
    487552    return false;
     
    644709  if(ret_val == NULL)
    645710  {
    646     regfi_add_message(file, REGFI_MSG_WARN, "Failed to load subkey list at"
     711    regfi_log_add(REGFI_LOG_WARN, "Failed to load subkey list at"
    647712                      " offset 0x%.8X.", offset);
    648713    return NULL;
     
    655720     *  now if they don't match.
    656721     */
    657     regfi_add_message(file, REGFI_MSG_WARN, "Number of subkeys listed in parent"
     722    regfi_log_add(REGFI_LOG_WARN, "Number of subkeys listed in parent"
    658723                      " (%d) did not match number found in subkey list/tree (%d)"
    659724                      " while parsing subkey list/tree at offset 0x%.8X.",
     
    678743  if(depth_left == 0)
    679744  {
    680     regfi_add_message(file, REGFI_MSG_WARN, "Maximum depth reached"
     745    regfi_log_add(REGFI_LOG_WARN, "Maximum depth reached"
    681746                      " while parsing subkey list/tree at offset 0x%.8X.",
    682747                      offset);
     
    731796                       &cell_length, &unalloc))
    732797  {
    733     regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while "
     798    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while "
    734799                      "parsing subkey-list at offset 0x%.8X.", offset);
    735800    goto fail_locked;
     
    738803  if(cell_length > max_size)
    739804  {
    740     regfi_add_message(file, REGFI_MSG_WARN, "Cell size longer than max_size"
     805    regfi_log_add(REGFI_LOG_WARN, "Cell size longer than max_size"
    741806                      " while parsing subkey-list at offset 0x%.8X.", offset);
    742807    if(strict)
     
    757822  else
    758823  {
    759     regfi_add_message(file, REGFI_MSG_ERROR, "Unknown magic number"
     824    regfi_log_add(REGFI_LOG_ERROR, "Unknown magic number"
    760825                      " (0x%.2X, 0x%.2X) encountered while parsing"
    761826                      " subkey-list at offset 0x%.8X.", buf[0], buf[1], offset);
     
    780845  if(cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32_t) < length)
    781846  {
    782     regfi_add_message(file, REGFI_MSG_WARN, "Number of elements too large for"
     847    regfi_log_add(REGFI_LOG_WARN, "Number of elements too large for"
    783848                      " cell while parsing subkey-list at offset 0x%.8X.",
    784849                      offset);
     
    907972                       &cell_length, &unalloc))
    908973  {
    909     regfi_add_message(file, REGFI_MSG_WARN, "Could not parse SK record cell"
     974    regfi_log_add(REGFI_LOG_WARN, "Could not parse SK record cell"
    910975                      " at offset 0x%.8X.", offset);
    911976    goto fail_locked;
     
    914979  if(sk_header[0] != 's' || sk_header[1] != 'k')
    915980  {
    916     regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing"
     981    regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch in parsing"
    917982                      " SK record at offset 0x%.8X.", offset);
    918983    goto fail_locked;
     
    934999     || (strict && (ret_val->cell_size & 0x00000007) != 0))
    9351000  {
    936     regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size found while"
     1001    regfi_log_add(REGFI_LOG_WARN, "Invalid cell size found while"
    9371002                      " parsing SK record at offset 0x%.8X.", offset);
    9381003    goto fail_locked;
     
    9511016     || (ret_val->next_sk_off & 0x00000007) != 0)
    9521017  {
    953     regfi_add_message(file, REGFI_MSG_WARN, "SK record's next/previous offsets"
     1018    regfi_log_add(REGFI_LOG_WARN, "SK record's next/previous offsets"
    9541019                      " are not a multiple of 8 while parsing SK record at"
    9551020                      " offset 0x%.8X.", offset);
     
    9591024  if(ret_val->desc_size + REGFI_SK_MIN_LENGTH > ret_val->cell_size)
    9601025  {
    961     regfi_add_message(file, REGFI_MSG_WARN, "Security descriptor too large for"
     1026    regfi_log_add(REGFI_LOG_WARN, "Security descriptor too large for"
    9621027                      " cell while parsing SK record at offset 0x%.8X.",
    9631028                      offset);
     
    9731038     || length != ret_val->desc_size)
    9741039  {
    975     regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read security"
     1040    regfi_log_add(REGFI_LOG_ERROR, "Failed to read security"
    9761041                      " descriptor while parsing SK record at offset 0x%.8X.",
    9771042                      offset);
     
    9851050                                                   ret_val->desc_size)))
    9861051  {
    987     regfi_add_message(file, REGFI_MSG_ERROR, "Failed to parse security"
     1052    regfi_log_add(REGFI_LOG_ERROR, "Failed to parse security"
    9881053                      " descriptor while parsing SK record at offset 0x%.8X.",
    9891054                      offset);
     
    10161081  if(!regfi_parse_cell(file->cb, offset, NULL, 0, &cell_length, &unalloc))
    10171082  {
    1018     regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read cell header"
     1083    regfi_log_add(REGFI_LOG_ERROR, "Failed to read cell header"
    10191084                      " while parsing value list at offset 0x%.8X.", offset);
    10201085    goto fail_locked;
     
    10231088  if((cell_length & 0x00000007) != 0)
    10241089  {
    1025     regfi_add_message(file, REGFI_MSG_WARN, "Cell length not a multiple of 8"
     1090    regfi_log_add(REGFI_LOG_WARN, "Cell length not a multiple of 8"
    10261091                      " while parsing value list at offset 0x%.8X.", offset);
    10271092    if(strict)
     
    10321097  if((num_values * sizeof(uint32_t)) > cell_length-sizeof(uint32_t))
    10331098  {
    1034     regfi_add_message(file, REGFI_MSG_WARN, "Too many values found"
     1099    regfi_log_add(REGFI_LOG_WARN, "Too many values found"
    10351100                      " while parsing value list at offset 0x%.8X.", offset);
    10361101    if(strict)
     
    10541119     || length != read_len)
    10551120  {
    1056     regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read value pointers"
     1121    regfi_log_add(REGFI_LOG_ERROR, "Failed to read value pointers"
    10571122                      " while parsing value list at offset 0x%.8X.", offset);
    10581123    goto fail_locked;
     
    10751140         || ((ret_val->elements[i] & 0x00000007) != 0))
    10761141      {
    1077         regfi_add_message(file, REGFI_MSG_WARN, "Invalid value pointer"
     1142        regfi_log_add(REGFI_LOG_WARN, "Invalid value pointer"
    10781143                          " (0x%.8X) found while parsing value list at offset"
    10791144                          " 0x%.8X.", ret_val->elements[i], offset);
     
    11301195    if(tmp_size < 0)
    11311196    {
    1132       regfi_add_message(file, REGFI_MSG_WARN, "Error occurred while converting"
     1197      regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting"
    11331198                        " valuename to encoding %s.  Error message: %s",
    11341199                        regfi_encoding_int2str(output_encoding),
     
    11741239  if((num_values+1) * sizeof(uint32_t) > max_size)
    11751240  {
    1176     regfi_add_message(file, REGFI_MSG_WARN, "Number of values indicated by"
     1241    regfi_log_add(REGFI_LOG_WARN, "Number of values indicated by"
    11771242                      " parent key (%d) would cause cell to straddle HBIN"
    11781243                      " boundary while loading value list at offset"
     
    12241289    if(tmp_size < 0)
    12251290    {
    1226       regfi_add_message(file, REGFI_MSG_WARN, "Error occurred while converting"
     1291      regfi_log_add(REGFI_LOG_WARN, "Error occurred while converting"
    12271292                        " keyname to encoding %s.  Error message: %s",
    12281293                        regfi_encoding_int2str(output_encoding),
     
    12521317  if((nk = regfi_parse_nk(file, offset, max_size, true)) == NULL)
    12531318  {
    1254     regfi_add_message(file, REGFI_MSG_ERROR, "Could not load NK record at"
    1255                       " offset 0x%.8X.", offset);
     1319    regfi_log_add(REGFI_LOG_ERROR, "Could not load NK record at"
     1320                  " offset 0x%.8X.", offset);
    12561321    return NULL;
    12571322  }
     
    12811346      if(nk->values == NULL)
    12821347      {
    1283         regfi_add_message(file, REGFI_MSG_WARN, "Could not load value list"
    1284                           " for NK record at offset 0x%.8X.", offset);
     1348        regfi_log_add(REGFI_LOG_WARN, "Could not load value list"
     1349                      " for NK record at offset 0x%.8X.", offset);
    12851350        if(strict)
    12861351        {
     
    13151380      if(nk->subkeys == NULL)
    13161381      {
    1317         regfi_add_message(file, REGFI_MSG_WARN, "Could not load subkey list"
    1318                           " while parsing NK record at offset 0x%.8X.", offset);
     1382        regfi_log_add(REGFI_LOG_WARN, "Could not load subkey list"
     1383                      " while parsing NK record at offset 0x%.8X.", offset);
    13191384        nk->num_subkeys = 0;
    13201385      }
     
    13921457    if(!regfi_parse_cell(file->cb, cur_offset, NULL, 0, &cell_length, &unalloc))
    13931458    {
    1394       regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell at offset"
    1395                         " 0x%.8X while searching for root key.", cur_offset);
     1459      regfi_log_add(REGFI_LOG_WARN, "Could not parse cell at offset"
     1460                    " 0x%.8X while searching for root key.", cur_offset);
    13961461      return NULL;
    13971462    }
     
    14651530  file_length = file_cb->seek(file_cb, 0, SEEK_END);
    14661531  if(file_length < REGFI_REGF_SIZE+REGFI_HBIN_ALLOC)
    1467     return NULL;
     1532  {
     1533    regfi_log_add(REGFI_LOG_ERROR, "File length (%d) too short to contain a"
     1534                  " header and at least one HBIN.", file_length);
     1535    return NULL;
     1536  }
    14681537  file_cb->seek(file_cb, 0, SEEK_SET);
    14691538
    14701539  /* Read file header */
    1471   if ((rb = regfi_parse_regf(file_cb, true)) == NULL)
    1472   {
    1473     /* fprintf(stderr, "regfi_alloc_cb: Failed to read initial REGF block\n");*/
     1540  if ((rb = regfi_parse_regf(file_cb, false)) == NULL)
     1541  {
     1542    regfi_log_add(REGFI_LOG_ERROR, "Failed to read REGF block.");
    14741543    return NULL;
    14751544  }
     
    14821551  rb->cb_lock = talloc(rb, pthread_mutex_t);
    14831552  if(rb->cb_lock == NULL || pthread_mutex_init(rb->cb_lock, NULL) != 0)
    1484     goto fail;
     1553  {
     1554    regfi_log_add(REGFI_LOG_ERROR, "Failed to create cb_lock mutex.");
     1555    goto fail;
     1556  }
    14851557
    14861558  rb->hbins_lock = talloc(rb, pthread_rwlock_t);
    14871559  if(rb->hbins_lock == NULL || pthread_rwlock_init(rb->hbins_lock, NULL) != 0)
    1488     goto fail;
     1560  {
     1561    regfi_log_add(REGFI_LOG_ERROR, "Failed to create hbins_lock rwlock.");
     1562    goto fail;
     1563  }
    14891564
    14901565  rb->sk_lock = talloc(rb, pthread_mutex_t);
    14911566  if(rb->sk_lock == NULL || pthread_mutex_init(rb->sk_lock, NULL) != 0)
    1492     goto fail;
     1567  {
     1568    regfi_log_add(REGFI_LOG_ERROR, "Failed to create sk_lock mutex.");
     1569    goto fail;
     1570  }
    14931571
    14941572  rb->hbins = range_list_new();
    14951573  if(rb->hbins == NULL)
    1496     goto fail;
     1574  {
     1575    regfi_log_add(REGFI_LOG_ERROR, "Failed to create HBIN range_list.");
     1576    goto fail;
     1577  }
    14971578  talloc_steal(rb, rb->hbins);
    14981579
     
    15181599  /* Cache an unlimited number of SK records.  Typically there are very few. */
    15191600  rb->sk_cache = lru_cache_create_ctx(rb, 0, cache_secret, true);
    1520 
    1521   /* Default message mask */
    1522   rb->msg_mask = REGFI_MSG_ERROR|REGFI_MSG_WARN;
    15231601
    15241602  /* success */
     
    15451623void regfi_free(REGFI_FILE *file)
    15461624{
    1547   if(file->last_message != NULL)
    1548     free(file->last_message);
    1549 
    15501625  pthread_mutex_destroy(file->cb_lock);
    15511626  pthread_rwlock_destroy(file->hbins_lock);
     
    15761651  }
    15771652
    1578   regfi_add_message(file, REGFI_MSG_WARN, "File header indicated root key at"
    1579                     " location 0x%.8X, but no root key found."
    1580                     " Searching rest of file...", root_offset);
     1653  regfi_log_add(REGFI_LOG_WARN, "File header indicated root key at"
     1654                " location 0x%.8X, but no root key found."
     1655                " Searching rest of file...", root_offset);
    15811656 
    15821657  /* If the file header gives bad info, scan through the file one HBIN
     
    16401715     && output_encoding != REGFI_ENCODING_ASCII)
    16411716  {
    1642     regfi_add_message(file, REGFI_MSG_ERROR, "Invalid output_encoding supplied"
    1643                       " in creation of regfi iterator.");
     1717    regfi_log_add(REGFI_LOG_ERROR, "Invalid output_encoding supplied"
     1718                  " in creation of regfi iterator.");
    16441719    return NULL;
    16451720  }
     
    19842059  if(raw == NULL)
    19852060  {
    1986     regfi_add_message(i->f, REGFI_MSG_WARN, "Could not parse class"
    1987                       " name at offset 0x%.8X for key record at offset 0x%.8X.",
    1988                       offset, key->offset);
     2061    regfi_log_add(REGFI_LOG_WARN, "Could not parse class"
     2062                  " name at offset 0x%.8X for key record at offset 0x%.8X.",
     2063                  offset, key->offset);
    19892064    return NULL;
    19902065  }
     
    20062081  if(conv_size < 0)
    20072082  {
    2008     regfi_add_message(i->f, REGFI_MSG_WARN, "Error occurred while"
    2009                       " converting classname to charset %s.  Error message: %s",
    2010                       i->string_encoding, strerror(-conv_size));
     2083    regfi_log_add(REGFI_LOG_WARN, "Error occurred while"
     2084                  " converting classname to charset %s.  Error message: %s",
     2085                  i->string_encoding, strerror(-conv_size));
    20112086    talloc_free(interpreted);
    20122087    ret_val->interpreted = NULL;
     
    20372112    if(raw_data.buf == NULL)
    20382113    {
    2039       regfi_add_message(i->f, REGFI_MSG_WARN, "Could not parse data record"
    2040                         " while parsing VK record at offset 0x%.8X.",
    2041                         value->offset);
     2114      regfi_log_add(REGFI_LOG_WARN, "Could not parse data record"
     2115                    " while parsing VK record at offset 0x%.8X.",
     2116                    value->offset);
    20422117    }
    20432118    else
     
    20472122      if(ret_val == NULL)
    20482123      {
    2049         regfi_add_message(i->f, REGFI_MSG_WARN, "Error occurred in converting"
    2050                           " data buffer to data structure while interpreting "
    2051                           "data for VK record at offset 0x%.8X.",
    2052                           value->offset);
     2124        regfi_log_add(REGFI_LOG_WARN, "Error occurred in converting"
     2125                      " data buffer to data structure while interpreting "
     2126                      "data for VK record at offset 0x%.8X.",
     2127                      value->offset);
    20532128        talloc_free(raw_data.buf);
    20542129        return NULL;
     
    20572132      if(!regfi_interpret_data(i->f, i->string_encoding, value->type, ret_val))
    20582133      {
    2059         regfi_add_message(i->f, REGFI_MSG_INFO, "Error occurred while"
    2060                           " interpreting data for VK record at offset 0x%.8X.",
    2061                           value->offset);
     2134        regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
     2135                      " interpreting data for VK record at offset 0x%.8X.",
     2136                      value->offset);
    20622137      }
    20632138    }
     
    21392214    if(tmp_size < 0)
    21402215    {
    2141       regfi_add_message(file, REGFI_MSG_INFO, "Error occurred while"
    2142                         " converting data of type %d to %s.  Error message: %s",
    2143                         type, string_encoding, strerror(-tmp_size));
     2216      regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
     2217                    " converting data of type %d to %s.  Error message: %s",
     2218                    type, string_encoding, strerror(-tmp_size));
    21442219      talloc_free(tmp_str);
    21452220      data->interpreted.string = NULL;
     
    22062281    if(tmp_size < 0)
    22072282    {
    2208       regfi_add_message(file, REGFI_MSG_INFO, "Error occurred while"
    2209                         " converting data of type %d to %s.  Error message: %s",
    2210                         type, string_encoding, strerror(-tmp_size));
     2283      regfi_log_add(REGFI_LOG_INFO, "Error occurred while"
     2284                    " converting data of type %d to %s.  Error message: %s",
     2285                    type, string_encoding, strerror(-tmp_size));
    22112286      talloc_free(tmp_str);
    22122287      data->interpreted.multiple_string = NULL;
     
    23372412
    23382413/*******************************************************************
    2339  * XXX: Add way to return more detailed error information.
    23402414 *******************************************************************/
    23412415REGFI_FILE* regfi_parse_regf(REGFI_RAW_FILE* file_cb, bool strict)
     
    23502424
    23512425  ret_val->sk_cache = NULL;
    2352   ret_val->last_message = NULL;
    23532426  ret_val->hbins = NULL;
    23542427
     
    23562429  if((regfi_read(file_cb, file_header, &length)) != 0
    23572430     || length != REGFI_REGF_SIZE)
    2358     goto fail;
    2359  
     2431  {
     2432    regfi_log_add(REGFI_LOG_WARN, "Read failed while parsing REGF structure.");
     2433    goto fail;
     2434  }
     2435
    23602436  ret_val->checksum = IVAL(file_header, 0x1FC);
    23612437  ret_val->computed_checksum = regfi_compute_header_checksum(file_header);
    23622438  if (strict && (ret_val->checksum != ret_val->computed_checksum))
    2363     goto fail;
     2439  {
     2440    regfi_log_add(REGFI_LOG_WARN, "Stored header checksum (%.8X) did not equal"
     2441                  " computed checksum (%.8X).",
     2442                  ret_val->checksum, ret_val->computed_checksum);
     2443    if(strict)
     2444      goto fail;
     2445  }
    23642446
    23652447  memcpy(ret_val->magic, file_header, REGFI_REGF_MAGIC_SIZE);
    23662448  if(memcmp(ret_val->magic, "regf", REGFI_REGF_MAGIC_SIZE) != 0)
    23672449  {
    2368     if(strict)
    2369       goto fail;
    2370     regfi_add_message(ret_val, REGFI_MSG_WARN, "Magic number mismatch "
    2371                       "(%.2X %.2X %.2X %.2X) while parsing hive header",
    2372                       ret_val->magic[0], ret_val->magic[1],
    2373                       ret_val->magic[2], ret_val->magic[3]);
     2450    regfi_log_add(REGFI_LOG_ERROR, "Magic number mismatch "
     2451                  "(%.2X %.2X %.2X %.2X) while parsing hive header",
     2452                  ret_val->magic[0], ret_val->magic[1],
     2453                  ret_val->magic[2], ret_val->magic[3]);
     2454    goto fail;
    23742455  }
    23752456
     
    23842465  ret_val->root_cell = IVAL(file_header, 0x24);
    23852466  ret_val->last_block = IVAL(file_header, 0x28);
    2386 
    23872467  ret_val->cluster = IVAL(file_header, 0x2C);
    23882468
     
    24322512  if(regfi_seek(file->cb, offset, SEEK_SET) == -1)
    24332513  {
    2434     regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed"
    2435                       " while parsing hbin at offset 0x%.8X.", offset);
     2514    regfi_log_add(REGFI_LOG_ERROR, "Seek failed"
     2515                  " while parsing hbin at offset 0x%.8X.", offset);
    24362516    goto fail_locked;
    24372517  }
     
    24402520  if((regfi_read(file->cb, hbin_header, &length) != 0)
    24412521     || length != REGFI_HBIN_HEADER_SIZE)
     2522  {
     2523    regfi_log_add(REGFI_LOG_ERROR, "Read failed"
     2524                  " while parsing hbin at offset 0x%.8X.", offset);
    24422525    goto fail_locked;
     2526  }
    24432527
    24442528  if(!regfi_unlock(file, file->cb_lock, "regfi_parse_hbin"))
     
    24532537  if(strict && (memcmp(hbin->magic, "hbin", 4) != 0))
    24542538  {
    2455     regfi_add_message(file, REGFI_MSG_INFO, "Magic number mismatch "
    2456                       "(%.2X %.2X %.2X %.2X) while parsing hbin at offset"
    2457                       " 0x%.8X.", hbin->magic[0], hbin->magic[1],
    2458                       hbin->magic[2], hbin->magic[3], offset);
     2539    /* This always seems to happen at the end of a file, so we make it an INFO
     2540     * message, rather than something more serious.
     2541     */
     2542    regfi_log_add(REGFI_LOG_INFO, "Magic number mismatch "
     2543                  "(%.2X %.2X %.2X %.2X) while parsing hbin at offset"
     2544                  " 0x%.8X.", hbin->magic[0], hbin->magic[1],
     2545                  hbin->magic[2], hbin->magic[3], offset);
    24592546    goto fail;
    24602547  }
     
    24622549  hbin->first_hbin_off = IVAL(hbin_header, 0x4);
    24632550  hbin->block_size = IVAL(hbin_header, 0x8);
    2464   /* this should be the same thing as hbin->block_size but just in case */
     2551  /* this should be the same thing as hbin->block_size, but just in case */
    24652552  hbin->next_block = IVAL(hbin_header, 0x1C);
    24662553
     
    24752562     || (hbin->block_size & 0xFFFFF000) != hbin->block_size)
    24762563  {
    2477     regfi_add_message(file, REGFI_MSG_ERROR, "The hbin offset is not aligned"
    2478                       " or runs off the end of the file"
    2479                       " while parsing hbin at offset 0x%.8X.", offset);
     2564    regfi_log_add(REGFI_LOG_ERROR, "The hbin offset is not aligned"
     2565                  " or runs off the end of the file"
     2566                  " while parsing hbin at offset 0x%.8X.", offset);
    24802567    goto fail;
    24812568  }
     
    25042591  if(ret_val == NULL)
    25052592  {
    2506     regfi_add_message(file, REGFI_MSG_ERROR, "Failed to allocate memory while"
    2507                       " parsing NK record at offset 0x%.8X.", offset);
     2593    regfi_log_add(REGFI_LOG_ERROR, "Failed to allocate memory while"
     2594                  " parsing NK record at offset 0x%.8X.", offset);
    25082595    goto fail;
    25092596  }
     
    25152602                       &cell_length, &unalloc))
    25162603  {
    2517     regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header"
    2518                       " while parsing NK record at offset 0x%.8X.", offset);
     2604    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header"
     2605                  " while parsing NK record at offset 0x%.8X.", offset);
    25192606    goto fail_locked;
    25202607  }
     
    25222609  if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k'))
    25232610  {
    2524     regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing"
    2525                       " NK record at offset 0x%.8X.", offset);
     2611    regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch in parsing"
     2612                  " NK record at offset 0x%.8X.", offset);
    25262613    goto fail_locked;
    25272614  }
     
    25372624     || (strict && (ret_val->cell_size & 0x00000007) != 0))
    25382625  {
    2539     regfi_add_message(file, REGFI_MSG_WARN, "A length check failed while"
    2540                       " parsing NK record at offset 0x%.8X.", offset);
     2626    regfi_log_add(REGFI_LOG_WARN, "A length check failed while"
     2627                  " parsing NK record at offset 0x%.8X.", offset);
    25412628    goto fail_locked;
    25422629  }
     
    25482635  if((ret_val->flags & ~REGFI_NK_KNOWN_FLAGS) != 0)
    25492636  {
    2550     regfi_add_message(file, REGFI_MSG_WARN, "Unknown key flags (0x%.4X) while"
    2551                       " parsing NK record at offset 0x%.8X.",
    2552                       (ret_val->flags & ~REGFI_NK_KNOWN_FLAGS), offset);
     2637    regfi_log_add(REGFI_LOG_WARN, "Unknown key flags (0x%.4X) while"
     2638                  " parsing NK record at offset 0x%.8X.",
     2639                  (ret_val->flags & ~REGFI_NK_KNOWN_FLAGS), offset);
    25532640  }
    25542641
     
    25892676    if(strict)
    25902677    {
    2591       regfi_add_message(file, REGFI_MSG_ERROR, "Contents too large for cell"
    2592                         " while parsing NK record at offset 0x%.8X.", offset);
     2678      regfi_log_add(REGFI_LOG_ERROR, "Contents too large for cell"
     2679                    " while parsing NK record at offset 0x%.8X.", offset);
    25932680      goto fail_locked;
    25942681    }
     
    26172704     || length != ret_val->name_length)
    26182705  {
    2619     regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read key name"
    2620                       " while parsing NK record at offset 0x%.8X.", offset);
     2706    regfi_log_add(REGFI_LOG_ERROR, "Failed to read key name"
     2707                  " while parsing NK record at offset 0x%.8X.", offset);
    26212708    goto fail_locked;
    26222709  }
     
    26522739  if(!regfi_parse_cell(file->cb, offset, NULL, 0, &cell_length, &unalloc))
    26532740  {
    2654     regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header"
    2655                       " while parsing class name at offset 0x%.8X.", offset);
     2741    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header"
     2742                  " while parsing class name at offset 0x%.8X.", offset);
    26562743    goto fail_locked;
    26572744  }
     
    26592746  if((cell_length & 0x0000007) != 0)
    26602747  {
    2661     regfi_add_message(file, REGFI_MSG_ERROR, "Cell length not a multiple of 8"
    2662                       " while parsing class name at offset 0x%.8X.", offset);
     2748    regfi_log_add(REGFI_LOG_ERROR, "Cell length not a multiple of 8"
     2749                  " while parsing class name at offset 0x%.8X.", offset);
    26632750    goto fail_locked;
    26642751  }
     
    26662753  if(cell_length > max_size)
    26672754  {
    2668     regfi_add_message(file, REGFI_MSG_WARN, "Cell stretches past hbin "
    2669                       "boundary while parsing class name at offset 0x%.8X.",
    2670                       offset);
     2755    regfi_log_add(REGFI_LOG_WARN, "Cell stretches past hbin "
     2756                  "boundary while parsing class name at offset 0x%.8X.",
     2757                  offset);
    26712758    if(strict)
    26722759      goto fail_locked;
     
    26762763  if((cell_length - 4) < *name_length)
    26772764  {
    2678     regfi_add_message(file, REGFI_MSG_WARN, "Class name is larger than"
    2679                       " cell_length while parsing class name at offset"
    2680                       " 0x%.8X.", offset);
     2765    regfi_log_add(REGFI_LOG_WARN, "Class name is larger than"
     2766                  " cell_length while parsing class name at offset"
     2767                  " 0x%.8X.", offset);
    26812768    if(strict)
    26822769      goto fail_locked;
     
    26912778       || length != *name_length)
    26922779    {
    2693       regfi_add_message(file, REGFI_MSG_ERROR, "Could not read class name"
    2694                         " while parsing class name at offset 0x%.8X.", offset);
     2780      regfi_log_add(REGFI_LOG_ERROR, "Could not read class name"
     2781                    " while parsing class name at offset 0x%.8X.", offset);
    26952782      goto fail_locked;
    26962783    }
     
    27302817                       &cell_length, &unalloc))
    27312818  {
    2732     regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header"
    2733                       " while parsing VK record at offset 0x%.8X.", offset);
     2819    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell header"
     2820                  " while parsing VK record at offset 0x%.8X.", offset);
    27342821    goto fail_locked;
    27352822  }
     
    27452832     || (ret_val->cell_size & 0x00000007) != 0)
    27462833  {
    2747     regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size encountered"
    2748                       " while parsing VK record at offset 0x%.8X.", offset);
     2834    regfi_log_add(REGFI_LOG_WARN, "Invalid cell size encountered"
     2835                  " while parsing VK record at offset 0x%.8X.", offset);
    27492836    goto fail_locked;
    27502837  }
     
    27582845     *      0xFFFF.
    27592846     */
    2760     regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch"
    2761                       " while parsing VK record at offset 0x%.8X.", offset);
     2847    regfi_log_add(REGFI_LOG_WARN, "Magic number mismatch"
     2848                  " while parsing VK record at offset 0x%.8X.", offset);
    27622849    goto fail_locked;
    27632850  }
     
    27792866    if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size)
    27802867    {
    2781       regfi_add_message(file, REGFI_MSG_WARN, "Name too long for remaining cell"
    2782                         " space while parsing VK record at offset 0x%.8X.",
    2783                         offset);
     2868      regfi_log_add(REGFI_LOG_WARN, "Name too long for remaining cell"
     2869                    " space while parsing VK record at offset 0x%.8X.",
     2870                    offset);
    27842871      if(strict)
    27852872        goto fail_locked;
     
    28012888       || length != ret_val->name_length)
    28022889    {
    2803       regfi_add_message(file, REGFI_MSG_ERROR, "Could not read value name"
    2804                         " while parsing VK record at offset 0x%.8X.", offset);
     2890      regfi_log_add(REGFI_LOG_ERROR, "Could not read value name"
     2891                    " while parsing VK record at offset 0x%.8X.", offset);
    28052892      goto fail_locked;
    28062893    }
     
    28562943  if(length > REGFI_VK_MAX_DATA_LENGTH)
    28572944  {
    2858     regfi_add_message(file, REGFI_MSG_WARN, "Value data size %d larger than "
    2859                       "%d, truncating...", length, REGFI_VK_MAX_DATA_LENGTH);
     2945    regfi_log_add(REGFI_LOG_WARN, "Value data size %d larger than "
     2946                  "%d, truncating...", length, REGFI_VK_MAX_DATA_LENGTH);
    28602947    length = REGFI_VK_MAX_DATA_LENGTH;
    28612948  }
     
    28692956    if(max_size < 0)
    28702957    {
    2871       regfi_add_message(file, REGFI_MSG_WARN, "Could not find HBIN for data"
    2872                         " at offset 0x%.8X.", offset);
     2958      regfi_log_add(REGFI_LOG_WARN, "Could not find HBIN for data"
     2959                    " at offset 0x%.8X.", offset);
    28732960      goto fail;
    28742961    }
     
    28802967                         &cell_length, &unalloc))
    28812968    {
    2882       regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while"
    2883                         " parsing data record at offset 0x%.8X.", offset);
     2969      regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
     2970                    " parsing data record at offset 0x%.8X.", offset);
    28842971      goto fail_locked;
    28852972    }
     
    28902977    if((cell_length & 0x00000007) != 0)
    28912978    {
    2892       regfi_add_message(file, REGFI_MSG_WARN, "Cell length not multiple of 8"
    2893                         " while parsing data record at offset 0x%.8X.",
    2894                         offset);
     2979      regfi_log_add(REGFI_LOG_WARN, "Cell length not multiple of 8"
     2980                    " while parsing data record at offset 0x%.8X.",
     2981                    offset);
    28952982      goto fail;
    28962983    }
     
    28982985    if(cell_length > max_size)
    28992986    {
    2900       regfi_add_message(file, REGFI_MSG_WARN, "Cell extends past HBIN boundary"
    2901                         " while parsing data record at offset 0x%.8X.",
    2902                         offset);
     2987      regfi_log_add(REGFI_LOG_WARN, "Cell extends past HBIN boundary"
     2988                    " while parsing data record at offset 0x%.8X.",
     2989                    offset);
    29032990      goto fail;
    29042991    }
     
    29183005      else
    29193006      {
    2920         regfi_add_message(file, REGFI_MSG_WARN, "Data length (0x%.8X) larger than"
    2921                           " remaining cell length (0x%.8X)"
    2922                           " while parsing data record at offset 0x%.8X.",
    2923                           length, cell_length - 4, offset);
     3007        regfi_log_add(REGFI_LOG_WARN, "Data length (0x%.8X) larger than"
     3008                      " remaining cell length (0x%.8X)"
     3009                      " while parsing data record at offset 0x%.8X.",
     3010                      length, cell_length - 4, offset);
    29243011        if(strict)
    29253012          goto fail;
     
    29643051  if(regfi_seek(file->cb, offset+4, SEEK_SET) == -1)
    29653052  {
    2966     regfi_add_message(file, REGFI_MSG_WARN, "Could not seek while "
    2967                       "reading data at offset 0x%.8X.", offset);
     3053    regfi_log_add(REGFI_LOG_WARN, "Could not seek while "
     3054                  "reading data at offset 0x%.8X.", offset);
    29683055    goto fail_locked;
    29693056  }
     
    29733060     || read_length != length)
    29743061  {
    2975     regfi_add_message(file, REGFI_MSG_ERROR, "Could not read data block while"
    2976                       " parsing data record at offset 0x%.8X.", offset);
     3062    regfi_log_add(REGFI_LOG_ERROR, "Could not read data block while"
     3063                  " parsing data record at offset 0x%.8X.", offset);
    29773064    goto fail_locked;
    29783065  }
     
    30083095  if(length > 4)
    30093096  {
    3010     regfi_add_message(file, REGFI_MSG_ERROR, "Data in offset but length > 4"
    3011                       " while parsing data record. (voffset=0x%.8X, length=%d)",
    3012                       voffset, length);
     3097    regfi_log_add(REGFI_LOG_ERROR, "Data in offset but length > 4"
     3098                  " while parsing data record. (voffset=0x%.8X, length=%d)",
     3099                  voffset, length);
    30133100    return ret_val;
    30143101  }
     
    30403127  if(REGFI_BIG_DATA_MIN_LENGTH > max_size)
    30413128  {
    3042     regfi_add_message(file, REGFI_MSG_WARN, "Big data header exceeded max_size "
    3043                       "while parsing big data header at offset 0x%.8X.",offset);
     3129    regfi_log_add(REGFI_LOG_WARN, "Big data header exceeded max_size "
     3130                  "while parsing big data header at offset 0x%.8X.",offset);
    30443131    goto fail;
    30453132  }
     
    30523139                       &cell_length, &unalloc))
    30533140  {
    3054     regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while"
    3055                       " parsing big data header at offset 0x%.8X.", offset);
     3141    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
     3142                  " parsing big data header at offset 0x%.8X.", offset);
    30563143    goto fail_locked;
    30573144  }
     
    30623149  if((ret_val.buf[0] != 'd') || (ret_val.buf[1] != 'b'))
    30633150  {
    3064     regfi_add_message(file, REGFI_MSG_WARN, "Unknown magic number"
    3065                       " (0x%.2X, 0x%.2X) encountered while parsing"
    3066                       " big data header at offset 0x%.8X.",
    3067                       ret_val.buf[0], ret_val.buf[1], offset);
     3151    regfi_log_add(REGFI_LOG_WARN, "Unknown magic number"
     3152                  " (0x%.2X, 0x%.2X) encountered while parsing"
     3153                  " big data header at offset 0x%.8X.",
     3154                  ret_val.buf[0], ret_val.buf[1], offset);
    30683155    goto fail;
    30693156  }
     
    31123199                       &indirect_length, &unalloc))
    31133200  {
    3114     regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while"
    3115                       " parsing big data indirect record at offset 0x%.8X.",
    3116                       offset);
     3201    regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
     3202                  " parsing big data indirect record at offset 0x%.8X.",
     3203                  offset);
    31173204    goto fail_locked;
    31183205  }
     
    31733260                         &cell_length, &unalloc))
    31743261    {
    3175       regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while"
    3176                         " parsing big data chunk at offset 0x%.8X.",
    3177                         chunk_offset);
     3262      regfi_log_add(REGFI_LOG_WARN, "Could not parse cell while"
     3263                    " parsing big data chunk at offset 0x%.8X.",
     3264                    chunk_offset);
    31783265      goto fail_locked;
    31793266    }
     
    32653352      if(i+1 != num_chunks)
    32663353      {
    3267         regfi_add_message(file, REGFI_MSG_WARN, "Left over chunks detected "
    3268                           "while constructing big data at offset 0x%.8X "
    3269                           "(chunk offset 0x%.8X).", offset, cell_info->offset);
     3354        regfi_log_add(REGFI_LOG_WARN, "Left over chunks detected "
     3355                      "while constructing big data at offset 0x%.8X "
     3356                      "(chunk offset 0x%.8X).", offset, cell_info->offset);
    32703357      }
    32713358      read_length = data_left;
     
    32773364    if(read_length > regfi_calc_maxsize(file, cell_info->offset))
    32783365    {
    3279       regfi_add_message(file, REGFI_MSG_WARN, "A chunk exceeded the maxsize "
    3280                         "while constructing big data at offset 0x%.8X "
    3281                         "(chunk offset 0x%.8X).", offset, cell_info->offset);
     3366      regfi_log_add(REGFI_LOG_WARN, "A chunk exceeded the maxsize "
     3367                    "while constructing big data at offset 0x%.8X "
     3368                    "(chunk offset 0x%.8X).", offset, cell_info->offset);
    32823369      goto fail;
    32833370    }
     
    32883375    if(regfi_seek(file->cb, cell_info->offset+sizeof(uint32_t), SEEK_SET) == -1)
    32893376    {
    3290       regfi_add_message(file, REGFI_MSG_WARN, "Could not seek to chunk while "
    3291                         "constructing big data at offset 0x%.8X "
    3292                         "(chunk offset 0x%.8X).", offset, cell_info->offset);
     3377      regfi_log_add(REGFI_LOG_WARN, "Could not seek to chunk while "
     3378                    "constructing big data at offset 0x%.8X "
     3379                    "(chunk offset 0x%.8X).", offset, cell_info->offset);
    32933380      goto fail_locked;
    32943381    }
     
    32983385                  &read_length) != 0 || (read_length != tmp_len))
    32993386    {
    3300       regfi_add_message(file, REGFI_MSG_WARN, "Could not read data chunk while"
    3301                         " constructing big data at offset 0x%.8X"
    3302                         " (chunk offset 0x%.8X).", offset, cell_info->offset);
     3387      regfi_log_add(REGFI_LOG_WARN, "Could not read data chunk while"
     3388                    " constructing big data at offset 0x%.8X"
     3389                    " (chunk offset 0x%.8X).", offset, cell_info->offset);
    33033390      goto fail_locked;
    33043391    }
     
    33753462      if((cell_len == 0) || ((cell_len & 0x00000007) != 0))
    33763463      {
    3377         regfi_add_message(file, REGFI_MSG_ERROR, "Bad cell length encountered"
    3378                           " while parsing unallocated cells at offset 0x%.8X.",
    3379                           hbin->file_off+curr_off);
     3464        regfi_log_add(REGFI_LOG_ERROR, "Bad cell length encountered"
     3465                      " while parsing unallocated cells at offset 0x%.8X.",
     3466                      hbin->file_off+curr_off);
    33803467        break;
    33813468      }
Note: See TracChangeset for help on using the changeset viewer.