File: common.c

package info (click to toggle)
reglookup 1.0.1+svn287-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 952 kB
  • sloc: ansic: 6,676; python: 3,762; makefile: 40; sh: 27
file content (375 lines) | stat: -rw-r--r-- 9,553 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * This file stores code common to the command line tools.
 * XXX: This should be converted to a proper library.
 *
 * Copyright (C) 2005-2008,2011 Timothy D. Morgan
 * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
 *
 * $Id$
 */

#include <iconv.h>
iconv_t conv_desc;

const char* key_special_chars = ",\"/";
const char* subfield_special_chars = ",\"|";
const char* common_special_chars = ",\"";

#define REGLOOKUP_EXIT_OK       0
#define REGLOOKUP_EXIT_OSERR   71
#define REGLOOKUP_EXIT_USAGE   64
#define REGLOOKUP_EXIT_DATAERR 65
#define REGLOOKUP_EXIT_NOINPUT 66


/* Windows is lame */
#ifdef O_BINARY
#define REGLOOKUP_OPEN_FLAGS O_RDONLY|O_BINARY
#else
#define REGLOOKUP_OPEN_FLAGS O_RDONLY
#endif


void bailOut(int code, char* message)
{
  fprintf(stderr, "%s", message);
  exit(code);
}

void printMsgs()
{
  char* msgs = regfi_log_get_str();
  if(msgs != NULL)
  {
    fprintf(stderr, "%s", msgs);
    free(msgs);
  }
}

void clearMsgs()
{
  char* msgs = regfi_log_get_str();
  if(msgs != NULL)
    free(msgs);
}


/* Returns a newly malloc()ed string which contains original buffer,
 * except for non-printable or special characters are quoted in hex
 * with the syntax '%QQ' where QQ is the hex ascii value of the quoted
 * character.  A null terminator is added, since only ascii, not binary,
 * is returned.
 */
static char* quote_buffer(const unsigned char* str, 
			  unsigned int len, const char* special)
{
  unsigned int i, added_len;
  unsigned int num_written = 0;

  unsigned int buf_len = sizeof(char)*(len+1);
  char* ret_val = NULL; 
  char* tmp_buf;

  if(buf_len > 0) 
    ret_val = malloc(buf_len);
  if(ret_val == NULL)
    return NULL;

  for(i=0; i<len; i++)
  {
    if(buf_len <= (num_written+5))
    {
      /* Expand the buffer by the memory consumption rate seen so far 
       * times the amount of input left to process.  The expansion is bounded 
       * below by a minimum safety increase, and above by the maximum possible 
       * output string length.  This should minimize both the number of 
       * reallocs() and the amount of wasted memory.
       */
      added_len = (len-i)*num_written/(i+1);
      if((buf_len+added_len) > (len*4+1))
	buf_len = len*4+1;
      else
      {
	if (added_len < 5)
	  buf_len += 5;
	else
	  buf_len += added_len;
      }

      tmp_buf = realloc(ret_val, buf_len);
      if(tmp_buf == NULL)
      {
	free(ret_val);
	return NULL;
      }
      ret_val = tmp_buf;
    }
    
    if(str[i] < 32 || str[i] > 126 
       || str[i] == '%' || strchr(special, str[i]) != NULL)
    {
      num_written += snprintf(ret_val + num_written, buf_len - num_written,
			      "%%%.2X", str[i]);
    }
    else
      ret_val[num_written++] = str[i];
  }
  ret_val[num_written] = '\0';

  return ret_val;
}


/* Returns a newly malloc()ed string which contains original string, 
 * except for non-printable or special characters are quoted in hex
 * with the syntax '%QQ' where QQ is the hex ascii value of the quoted
 * character.
 */
static char* quote_string(const char* str, const char* special)
{
  unsigned int len;

  if(str == NULL)
    return NULL;

  len = strlen(str);
  return quote_buffer((const unsigned char*)str, len, special);
}


/*
 * Convert a data value to a string for display.  Returns NULL on error,
 * and the string to display if there is no error, or a non-fatal
 * error.  On any error (fatal or non-fatal) occurs, (*error_msg) will
 * be set to a newly allocated string, containing an error message.  If
 * a memory allocation failure occurs while generating the error
 * message, both the return value and (*error_msg) will be NULL.  It
 * is the responsibility of the caller to free both a non-NULL return
 * value, and a non-NULL (*error_msg).
 */
static char* data_to_ascii(const REGFI_DATA* data, char** error_msg)
{
  char* ret_val;
  char* cur_quoted;
  char* tmp_ptr;
  char* delim;
  uint32_t ret_val_left, i, tmp_len;

  if(data == NULL || data->size == 0)
  {
    *error_msg = (char*)malloc(37);
    if(*error_msg == NULL)
      return NULL;
    strcpy(*error_msg, "Data pointer was NULL or size was 0.");
    return NULL;
  }
  *error_msg = NULL;


  if(data->interpreted_size == 0)
  {
    *error_msg = (char*)malloc(51);
    if(*error_msg == NULL)
      return NULL;
    strcpy(*error_msg, "Data could not be interpreted, quoting raw buffer.");
    return quote_buffer(data->raw, data->size, subfield_special_chars);
  }

  switch (data->type) 
  {
  case REG_SZ:
    ret_val = quote_string((char*)data->interpreted.string, common_special_chars);
    if(ret_val == NULL && (*error_msg = (char*)malloc(49)) != NULL)
	strcpy(*error_msg, "Buffer could not be quoted due to unknown error.");

    return ret_val;
    break;

    
  case REG_EXPAND_SZ:
    ret_val = quote_string((char*)data->interpreted.expand_string, 
			   common_special_chars);
    if(ret_val == NULL && (*error_msg = (char*)malloc(49)) != NULL)
	strcpy(*error_msg, "Buffer could not be quoted due to unknown error.");

    return ret_val;
    break;

  case REG_LINK:
    ret_val = quote_string((char*)data->interpreted.link, common_special_chars);
    if(ret_val == NULL && (*error_msg = (char*)malloc(49)) != NULL)
	strcpy(*error_msg, "Buffer could not be quoted due to unknown error.");

    return ret_val;
    break;

  case REG_DWORD:
    ret_val = malloc(sizeof(char)*(8+2+1));
    if(ret_val == NULL)
      return NULL;

    sprintf(ret_val, "0x%.8X", data->interpreted.dword);
    return ret_val;
    break;

  case REG_DWORD_BE:
    ret_val = malloc(sizeof(char)*(8+2+1));
    if(ret_val == NULL)
      return NULL;

    sprintf(ret_val, "0x%.8X", data->interpreted.dword_be);
    return ret_val;
    break;

  case REG_QWORD:
    ret_val = malloc(sizeof(char)*(16+2+1));
    if(ret_val == NULL)
      return NULL;

    sprintf(ret_val, "0x%.16llX", 
	    (long long unsigned int)data->interpreted.qword);
    return ret_val;
    break;

  case REG_MULTI_SZ:
    ret_val_left = data->interpreted_size*4+1;
    ret_val = malloc(ret_val_left);
    if(ret_val == NULL)
      return NULL;

    tmp_ptr = ret_val;
    tmp_ptr[0] = '\0';
    delim = "";
    for(i=0; data->interpreted.multiple_string[i] != NULL; i++)
    {
      cur_quoted = quote_string((char*)data->interpreted.multiple_string[i],
				subfield_special_chars);
      if(cur_quoted != NULL)
      {
	tmp_len = snprintf(tmp_ptr, ret_val_left, "%s%s",delim, cur_quoted);
	tmp_ptr += tmp_len;
	ret_val_left -= tmp_len;
	free(cur_quoted);
      }
      delim = "|";
    }

    return ret_val;
    break;

    
  case REG_NONE:
    return quote_buffer(data->interpreted.none, data->interpreted_size,
			common_special_chars);

    break;

  case REG_RESOURCE_LIST:
    return quote_buffer(data->interpreted.resource_list, data->interpreted_size,
			common_special_chars);

    break;

  case REG_FULL_RESOURCE_DESCRIPTOR:
    return quote_buffer(data->interpreted.full_resource_descriptor, 
			data->interpreted_size, common_special_chars);

    break;

  case REG_RESOURCE_REQUIREMENTS_LIST:
    return quote_buffer(data->interpreted.resource_requirements_list,
			data->interpreted_size, common_special_chars);

    break;

  case REG_BINARY:
    return quote_buffer(data->interpreted.binary, data->interpreted_size,
			common_special_chars);

    break;

  default:
    /* This shouldn't happen, since the regfi routines won't interpret 
     * unknown types, but just as a safety measure against library changes... 
     */
    *error_msg = (char*)malloc(65);
    if(*error_msg == NULL)
      return NULL;
    sprintf(*error_msg,
	    "Unrecognized registry data type (0x%.8X); quoting as binary.",
	    data->type);
    return quote_buffer(data->raw, data->size, common_special_chars);
  }
    
  return NULL;
}


static char* get_quoted_keyname(const REGFI_NK* nk)
{
  char* ret_val;

  if(nk->name == NULL)
    ret_val = quote_buffer(nk->name_raw, nk->name_length, key_special_chars);
  else
    ret_val = quote_string(nk->name, key_special_chars);

  return ret_val;
}


static char* get_quoted_valuename(const REGFI_VK* vk)
{
  char* ret_val;

  if(vk->name == NULL)
    ret_val = quote_buffer(vk->name_raw, vk->name_length, 
			   key_special_chars);
  else
    ret_val = quote_string(vk->name, key_special_chars);

  return ret_val;
}


int openHive(const char* filename)
{
  int ret_val;

  /* open an existing file */
  if ((ret_val = open(filename, REGLOOKUP_OPEN_FLAGS)) == -1)
  {
    fprintf(stderr, "ERROR: Failed to open hive.  Error returned: %s\n", 
	    strerror(errno));
    return -1;
  }

  return ret_val;
}


void formatTime(REGFI_NTTIME nttime, char* output)
{
  time_t tmp_time[1];
  struct tm* tmp_time_s = NULL;

  *tmp_time = (time_t)regfi_nt2unix_time(nttime);
  tmp_time_s = gmtime(tmp_time);
  strftime(output, 
	   (4+1+2+1+2)+1+(2+1+2+1+2)+1, 
              "%Y-%m-%d %H:%M:%S", 
	   tmp_time_s);
}