[30] | 1 | /* |
---|
[42] | 2 | * A utility to read a Windows NT/2K/XP/2K3 registry file, using |
---|
| 3 | * Gerald Carter''s regfio interface. |
---|
[30] | 4 | * |
---|
[61] | 5 | * Copyright (C) 2005-2006 Timothy D. Morgan |
---|
[42] | 6 | * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com |
---|
[30] | 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or modify |
---|
| 9 | * it under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation; version 2 of the License. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 20 | * |
---|
| 21 | * $Id: reglookup.c 69 2006-07-30 17:57:45Z tim $ |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <stdio.h> |
---|
| 27 | #include <string.h> |
---|
[33] | 28 | #include <strings.h> |
---|
[42] | 29 | #include <time.h> |
---|
[61] | 30 | #include <iconv.h> |
---|
[30] | 31 | #include "../include/regfio.h" |
---|
[31] | 32 | #include "../include/void_stack.h" |
---|
[30] | 33 | |
---|
[40] | 34 | /* Globals, influenced by command line parameters */ |
---|
| 35 | bool print_verbose = false; |
---|
| 36 | bool print_security = false; |
---|
[42] | 37 | bool print_header = true; |
---|
[40] | 38 | bool path_filter_enabled = false; |
---|
| 39 | bool type_filter_enabled = false; |
---|
| 40 | char* path_filter = NULL; |
---|
| 41 | int type_filter; |
---|
| 42 | char* registry_file = NULL; |
---|
| 43 | |
---|
[42] | 44 | /* Other globals */ |
---|
[66] | 45 | const char* key_special_chars = ",\"\\/"; |
---|
| 46 | const char* subfield_special_chars = ",\"\\|"; |
---|
| 47 | const char* common_special_chars = ",\"\\"; |
---|
| 48 | |
---|
[61] | 49 | iconv_t conv_desc; |
---|
[40] | 50 | |
---|
[61] | 51 | |
---|
[38] | 52 | void bailOut(int code, char* message) |
---|
| 53 | { |
---|
| 54 | fprintf(stderr, message); |
---|
| 55 | exit(code); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | |
---|
[41] | 59 | /* Returns a newly malloc()ed string which contains original buffer, |
---|
| 60 | * except for non-printable or special characters are quoted in hex |
---|
| 61 | * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted |
---|
[61] | 62 | * character. A null terminator is added, since only ascii, not binary, |
---|
[41] | 63 | * is returned. |
---|
| 64 | */ |
---|
| 65 | static char* quote_buffer(const unsigned char* str, |
---|
[44] | 66 | unsigned int len, const char* special) |
---|
[41] | 67 | { |
---|
[61] | 68 | unsigned int i, added_len; |
---|
| 69 | unsigned int num_written = 0; |
---|
[41] | 70 | |
---|
[61] | 71 | unsigned int buf_len = sizeof(char)*(len+1); |
---|
| 72 | char* ret_val = malloc(buf_len); |
---|
| 73 | char* tmp_buf; |
---|
| 74 | |
---|
[41] | 75 | if(ret_val == NULL) |
---|
| 76 | return NULL; |
---|
| 77 | |
---|
| 78 | for(i=0; i<len; i++) |
---|
| 79 | { |
---|
[61] | 80 | if(buf_len <= (num_written+5)) |
---|
| 81 | { |
---|
| 82 | /* Expand the buffer by the memory consumption rate seen so far |
---|
| 83 | * times the amount of input left to process. The expansion is bounded |
---|
| 84 | * below by a minimum safety increase, and above by the maximum possible |
---|
[69] | 85 | * output string length. This should minimize both the number of |
---|
| 86 | * reallocs() and the amount of wasted memory. |
---|
[61] | 87 | */ |
---|
| 88 | added_len = (len-i)*num_written/(i+1); |
---|
| 89 | if((buf_len+added_len) > (len*4+1)) |
---|
| 90 | buf_len = len*4+1; |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | if (added_len < 5) |
---|
| 94 | buf_len += 5; |
---|
| 95 | else |
---|
| 96 | buf_len += added_len; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | tmp_buf = realloc(ret_val, buf_len); |
---|
| 100 | if(tmp_buf == NULL) |
---|
| 101 | { |
---|
| 102 | free(ret_val); |
---|
| 103 | return NULL; |
---|
| 104 | } |
---|
| 105 | ret_val = tmp_buf; |
---|
| 106 | } |
---|
| 107 | |
---|
[41] | 108 | if(str[i] < 32 || str[i] > 126 || strchr(special, str[i]) != NULL) |
---|
| 109 | { |
---|
[61] | 110 | num_written += snprintf(ret_val + num_written, buf_len - num_written, |
---|
[41] | 111 | "\\x%.2X", str[i]); |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | ret_val[num_written++] = str[i]; |
---|
| 115 | } |
---|
| 116 | ret_val[num_written] = '\0'; |
---|
| 117 | |
---|
| 118 | return ret_val; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | /* Returns a newly malloc()ed string which contains original string, |
---|
| 123 | * except for non-printable or special characters are quoted in hex |
---|
| 124 | * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted |
---|
| 125 | * character. |
---|
| 126 | */ |
---|
[44] | 127 | static char* quote_string(const char* str, const char* special) |
---|
[41] | 128 | { |
---|
[42] | 129 | unsigned int len; |
---|
[41] | 130 | |
---|
[42] | 131 | if(str == NULL) |
---|
| 132 | return NULL; |
---|
| 133 | |
---|
| 134 | len = strlen(str); |
---|
| 135 | return quote_buffer((const unsigned char*)str, len, special); |
---|
[41] | 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | /* |
---|
[69] | 140 | * Convert from UTF-16LE to ASCII. Accepts a Unicode buffer, uni, and |
---|
| 141 | * it's length, uni_max. Writes ASCII to the buffer ascii, whose size |
---|
| 142 | * is ascii_max. Writes at most (ascii_max-1) bytes to ascii, and null |
---|
| 143 | * terminates the string. Returns the length of the string stored in |
---|
| 144 | * ascii. On error, returns a negative errno code. |
---|
[41] | 145 | */ |
---|
[61] | 146 | static int uni_to_ascii(unsigned char* uni, char* ascii, |
---|
| 147 | unsigned int uni_max, unsigned int ascii_max) |
---|
[41] | 148 | { |
---|
[61] | 149 | char* inbuf = (char*)uni; |
---|
| 150 | char* outbuf = ascii; |
---|
[69] | 151 | unsigned int out_len = ascii_max-1; |
---|
[61] | 152 | int ret; |
---|
[41] | 153 | |
---|
[61] | 154 | /* Set up conversion descriptor. */ |
---|
| 155 | conv_desc = iconv_open("US-ASCII", "UTF-16LE"); |
---|
| 156 | |
---|
[69] | 157 | ret = iconv(conv_desc, &inbuf, &uni_max, &outbuf, &out_len); |
---|
[61] | 158 | if(ret == -1) |
---|
[41] | 159 | { |
---|
[61] | 160 | iconv_close(conv_desc); |
---|
[66] | 161 | return -errno; |
---|
[41] | 162 | } |
---|
[66] | 163 | *outbuf = '\0'; |
---|
[41] | 164 | |
---|
[61] | 165 | iconv_close(conv_desc); |
---|
| 166 | return strlen(ascii); |
---|
[41] | 167 | } |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | /* |
---|
[69] | 171 | * Convert a data value to a string for display. Returns NULL on error, |
---|
| 172 | * and the string to display if there is no error, or a non-fatal |
---|
| 173 | * error. On any error (fatal or non-fatal) occurs, (*error_msg) will |
---|
| 174 | * be set to a newly allocated string, containing an error message. If |
---|
| 175 | * a memory allocation failure occurs while generating the error |
---|
| 176 | * message, both the return value and (*error_msg) will be NULL. It |
---|
| 177 | * is the responsibility of the caller to free both a non-NULL return |
---|
| 178 | * value, and a non-NULL (*error_msg). |
---|
[41] | 179 | */ |
---|
[69] | 180 | static char* data_to_ascii(unsigned char *datap, int len, int type, |
---|
| 181 | char** error_msg) |
---|
[41] | 182 | { |
---|
[61] | 183 | char* asciip; |
---|
| 184 | char* ascii; |
---|
[41] | 185 | unsigned char* cur_str; |
---|
[61] | 186 | char* cur_ascii; |
---|
[41] | 187 | char* cur_quoted; |
---|
[69] | 188 | char* tmp_err; |
---|
| 189 | const char* str_type; |
---|
| 190 | unsigned int i; |
---|
[41] | 191 | unsigned int cur_str_len; |
---|
| 192 | unsigned int ascii_max, cur_str_max; |
---|
| 193 | unsigned int str_rem, cur_str_rem, alen; |
---|
[66] | 194 | int ret_err; |
---|
[69] | 195 | unsigned short num_nulls; |
---|
[41] | 196 | |
---|
[69] | 197 | *error_msg = NULL; |
---|
| 198 | |
---|
[41] | 199 | switch (type) |
---|
| 200 | { |
---|
[66] | 201 | case REG_SZ: |
---|
| 202 | case REG_EXPAND_SZ: |
---|
[61] | 203 | /* REG_LINK is a symbolic link, stored as a unicode string. */ |
---|
| 204 | case REG_LINK: |
---|
[69] | 205 | ascii_max = sizeof(char)*(len+1); |
---|
| 206 | ascii = malloc(ascii_max); |
---|
[41] | 207 | if(ascii == NULL) |
---|
| 208 | return NULL; |
---|
| 209 | |
---|
[66] | 210 | /* Sometimes values have binary stored in them. If the unicode |
---|
| 211 | * conversion fails, just quote it raw. |
---|
| 212 | */ |
---|
| 213 | ret_err = uni_to_ascii(datap, ascii, len, ascii_max); |
---|
| 214 | if(ret_err < 0) |
---|
[61] | 215 | { |
---|
[69] | 216 | tmp_err = strerror(-ret_err); |
---|
| 217 | str_type = regfio_type_val2str(type); |
---|
| 218 | *error_msg = (char*)malloc(65+strlen(str_type)+strlen(tmp_err)+1); |
---|
| 219 | if(*error_msg == NULL) |
---|
| 220 | return NULL; |
---|
| 221 | sprintf(*error_msg, "Unicode conversion failed on %s field; " |
---|
| 222 | "printing as binary. Error: %s", str_type, tmp_err); |
---|
| 223 | |
---|
[66] | 224 | cur_quoted = quote_buffer(datap, len, common_special_chars); |
---|
[61] | 225 | } |
---|
[66] | 226 | else |
---|
| 227 | cur_quoted = quote_string(ascii, common_special_chars); |
---|
[42] | 228 | free(ascii); |
---|
[61] | 229 | return cur_quoted; |
---|
[41] | 230 | break; |
---|
| 231 | |
---|
| 232 | case REG_DWORD: |
---|
[58] | 233 | ascii_max = sizeof(char)*11; |
---|
| 234 | ascii = malloc(ascii_max); |
---|
[41] | 235 | if(ascii == NULL) |
---|
| 236 | return NULL; |
---|
| 237 | |
---|
[61] | 238 | snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", |
---|
[58] | 239 | datap[0], datap[1], datap[2], datap[3]); |
---|
[41] | 240 | return ascii; |
---|
| 241 | break; |
---|
| 242 | |
---|
[58] | 243 | case REG_DWORD_BE: |
---|
| 244 | ascii_max = sizeof(char)*11; |
---|
| 245 | ascii = malloc(ascii_max); |
---|
| 246 | if(ascii == NULL) |
---|
| 247 | return NULL; |
---|
| 248 | |
---|
[61] | 249 | snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", |
---|
[58] | 250 | datap[3], datap[2], datap[1], datap[0]); |
---|
| 251 | return ascii; |
---|
| 252 | break; |
---|
| 253 | |
---|
[42] | 254 | /* XXX: this MULTI_SZ parser is pretty inefficient. Should be |
---|
[54] | 255 | * redone with fewer malloc calls and better string concatenation. |
---|
[42] | 256 | */ |
---|
[41] | 257 | case REG_MULTI_SZ: |
---|
[69] | 258 | ascii_max = sizeof(char)*(len*4+1); |
---|
| 259 | cur_str_max = sizeof(char)*(len+1); |
---|
[41] | 260 | cur_str = malloc(cur_str_max); |
---|
| 261 | cur_ascii = malloc(cur_str_max); |
---|
[69] | 262 | ascii = malloc(ascii_max); |
---|
[42] | 263 | if(ascii == NULL || cur_str == NULL || cur_ascii == NULL) |
---|
[41] | 264 | return NULL; |
---|
| 265 | |
---|
| 266 | /* Reads until it reaches 4 consecutive NULLs, |
---|
| 267 | * which is two nulls in unicode, or until it reaches len, or until we |
---|
| 268 | * run out of buffer. The latter should never happen, but we shouldn't |
---|
| 269 | * trust our file to have the right lengths/delimiters. |
---|
| 270 | */ |
---|
| 271 | asciip = ascii; |
---|
| 272 | num_nulls = 0; |
---|
| 273 | str_rem = ascii_max; |
---|
| 274 | cur_str_rem = cur_str_max; |
---|
| 275 | cur_str_len = 0; |
---|
| 276 | |
---|
| 277 | for(i=0; (i < len) && str_rem > 0; i++) |
---|
| 278 | { |
---|
| 279 | *(cur_str+cur_str_len) = *(datap+i); |
---|
| 280 | if(*(cur_str+cur_str_len) == 0) |
---|
| 281 | num_nulls++; |
---|
| 282 | else |
---|
| 283 | num_nulls = 0; |
---|
| 284 | cur_str_len++; |
---|
| 285 | |
---|
| 286 | if(num_nulls == 2) |
---|
| 287 | { |
---|
[66] | 288 | ret_err = uni_to_ascii(cur_str, cur_ascii, cur_str_len-1, cur_str_max); |
---|
| 289 | if(ret_err < 0) |
---|
[61] | 290 | { |
---|
[69] | 291 | /* XXX: should every sub-field error be enumerated? */ |
---|
| 292 | if(*error_msg == NULL) |
---|
| 293 | { |
---|
| 294 | tmp_err = strerror(-ret_err); |
---|
| 295 | *error_msg = (char*)malloc(90+strlen(tmp_err)+1); |
---|
| 296 | if(*error_msg == NULL) |
---|
| 297 | return NULL; |
---|
| 298 | sprintf(*error_msg, "Unicode conversion failed on at least one " |
---|
| 299 | "MULTI_SZ sub-field; printing as binary. Error: %s", |
---|
| 300 | tmp_err); |
---|
| 301 | } |
---|
[66] | 302 | cur_quoted = quote_buffer(cur_str, cur_str_len-1, |
---|
| 303 | subfield_special_chars); |
---|
[61] | 304 | } |
---|
[66] | 305 | else |
---|
| 306 | cur_quoted = quote_string(cur_ascii, subfield_special_chars); |
---|
[61] | 307 | |
---|
| 308 | alen = snprintf(asciip, str_rem, "%s", cur_quoted); |
---|
[41] | 309 | asciip += alen; |
---|
| 310 | str_rem -= alen; |
---|
| 311 | free(cur_quoted); |
---|
| 312 | |
---|
| 313 | if(*(datap+i+1) == 0 && *(datap+i+2) == 0) |
---|
| 314 | break; |
---|
| 315 | else |
---|
| 316 | { |
---|
[61] | 317 | if(str_rem > 0) |
---|
| 318 | { |
---|
| 319 | asciip[0] = '|'; |
---|
| 320 | asciip[1] = '\0'; |
---|
| 321 | asciip++; |
---|
| 322 | str_rem--; |
---|
| 323 | } |
---|
[41] | 324 | memset(cur_str, 0, cur_str_max); |
---|
| 325 | cur_str_len = 0; |
---|
| 326 | num_nulls = 0; |
---|
| 327 | /* To eliminate leading nulls in subsequent strings. */ |
---|
| 328 | i++; |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | *asciip = 0; |
---|
[42] | 333 | free(cur_str); |
---|
| 334 | free(cur_ascii); |
---|
[41] | 335 | return ascii; |
---|
| 336 | break; |
---|
| 337 | |
---|
[42] | 338 | /* XXX: Dont know what to do with these yet, just print as binary... */ |
---|
[61] | 339 | case REG_NONE: |
---|
[42] | 340 | case REG_RESOURCE_LIST: |
---|
| 341 | case REG_FULL_RESOURCE_DESCRIPTOR: |
---|
| 342 | case REG_RESOURCE_REQUIREMENTS_LIST: |
---|
| 343 | |
---|
| 344 | case REG_BINARY: |
---|
[66] | 345 | return quote_buffer(datap, len, common_special_chars); |
---|
[42] | 346 | break; |
---|
| 347 | |
---|
[41] | 348 | default: |
---|
| 349 | return NULL; |
---|
| 350 | break; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | return NULL; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | |
---|
[33] | 357 | void_stack* path2Stack(const char* s) |
---|
[30] | 358 | { |
---|
[38] | 359 | void_stack* ret_val; |
---|
| 360 | void_stack* rev_ret = void_stack_new(1024); |
---|
| 361 | const char* cur = s; |
---|
[33] | 362 | char* next = NULL; |
---|
[38] | 363 | char* copy; |
---|
| 364 | |
---|
| 365 | if (rev_ret == NULL) |
---|
| 366 | return NULL; |
---|
[37] | 367 | if (s == NULL) |
---|
[38] | 368 | return rev_ret; |
---|
| 369 | |
---|
| 370 | while((next = strchr(cur, '/')) != NULL) |
---|
[33] | 371 | { |
---|
[38] | 372 | if ((next-cur) > 0) |
---|
| 373 | { |
---|
| 374 | copy = (char*)malloc((next-cur+1)*sizeof(char)); |
---|
| 375 | if(copy == NULL) |
---|
| 376 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
| 377 | |
---|
| 378 | memcpy(copy, cur, next-cur); |
---|
| 379 | copy[next-cur] = '\0'; |
---|
| 380 | void_stack_push(rev_ret, copy); |
---|
| 381 | } |
---|
| 382 | cur = next+1; |
---|
[33] | 383 | } |
---|
| 384 | if(strlen(cur) > 0) |
---|
[38] | 385 | { |
---|
| 386 | copy = strdup(cur); |
---|
| 387 | void_stack_push(rev_ret, copy); |
---|
| 388 | } |
---|
[33] | 389 | |
---|
[38] | 390 | ret_val = void_stack_copy_reverse(rev_ret); |
---|
| 391 | void_stack_destroy(rev_ret); |
---|
| 392 | |
---|
[33] | 393 | return ret_val; |
---|
| 394 | } |
---|
| 395 | |
---|
[66] | 396 | /* Returns a quoted path from an nk_stack */ |
---|
[33] | 397 | char* stack2Path(void_stack* nk_stack) |
---|
| 398 | { |
---|
| 399 | const REGF_NK_REC* cur; |
---|
[37] | 400 | uint32 buf_left = 127; |
---|
| 401 | uint32 buf_len = buf_left+1; |
---|
| 402 | uint32 name_len = 0; |
---|
| 403 | uint32 grow_amt; |
---|
[31] | 404 | char* buf; |
---|
| 405 | char* new_buf; |
---|
[66] | 406 | char* name; |
---|
[31] | 407 | void_stack_iterator* iter; |
---|
| 408 | |
---|
| 409 | buf = (char*)malloc((buf_len)*sizeof(char)); |
---|
| 410 | if (buf == NULL) |
---|
| 411 | return NULL; |
---|
[54] | 412 | buf[0] = '\0'; |
---|
[30] | 413 | |
---|
[31] | 414 | iter = void_stack_iterator_new(nk_stack); |
---|
| 415 | if (iter == NULL) |
---|
[30] | 416 | { |
---|
[31] | 417 | free(buf); |
---|
| 418 | return NULL; |
---|
[30] | 419 | } |
---|
| 420 | |
---|
[33] | 421 | /* skip root element */ |
---|
| 422 | cur = void_stack_iterator_next(iter); |
---|
| 423 | |
---|
[31] | 424 | while((cur = void_stack_iterator_next(iter)) != NULL) |
---|
| 425 | { |
---|
[33] | 426 | buf[buf_len-buf_left-1] = '/'; |
---|
| 427 | buf_left -= 1; |
---|
[66] | 428 | name = quote_string(cur->keyname, key_special_chars); |
---|
| 429 | name_len = strlen(name); |
---|
[31] | 430 | if(name_len+1 > buf_left) |
---|
| 431 | { |
---|
[37] | 432 | grow_amt = (uint32)(buf_len/2); |
---|
[31] | 433 | buf_len += name_len+1+grow_amt-buf_left; |
---|
| 434 | if((new_buf = realloc(buf, buf_len)) == NULL) |
---|
| 435 | { |
---|
| 436 | free(buf); |
---|
| 437 | free(iter); |
---|
| 438 | return NULL; |
---|
| 439 | } |
---|
| 440 | buf = new_buf; |
---|
| 441 | buf_left = grow_amt + name_len + 1; |
---|
| 442 | } |
---|
[66] | 443 | strncpy(buf+(buf_len-buf_left-1), name, name_len); |
---|
[31] | 444 | buf_left -= name_len; |
---|
| 445 | buf[buf_len-buf_left-1] = '\0'; |
---|
[66] | 446 | free(name); |
---|
[31] | 447 | } |
---|
[30] | 448 | |
---|
[31] | 449 | return buf; |
---|
| 450 | } |
---|
[30] | 451 | |
---|
[31] | 452 | |
---|
[33] | 453 | void printValue(REGF_VK_REC* vk, char* prefix) |
---|
[31] | 454 | { |
---|
[41] | 455 | uint32 size; |
---|
| 456 | uint8 tmp_buf[4]; |
---|
[66] | 457 | char* quoted_value = NULL; |
---|
| 458 | char* quoted_name = NULL; |
---|
[69] | 459 | char* conv_error = NULL; |
---|
[41] | 460 | |
---|
[43] | 461 | /* Thanks Microsoft for making this process so straight-forward!!! */ |
---|
| 462 | size = (vk->data_size & ~VK_DATA_IN_OFFSET); |
---|
| 463 | if(vk->data_size & VK_DATA_IN_OFFSET) |
---|
[41] | 464 | { |
---|
[43] | 465 | tmp_buf[0] = (uint8)((vk->data_off >> 3) & 0xFF); |
---|
| 466 | tmp_buf[1] = (uint8)((vk->data_off >> 2) & 0xFF); |
---|
| 467 | tmp_buf[2] = (uint8)((vk->data_off >> 1) & 0xFF); |
---|
| 468 | tmp_buf[3] = (uint8)(vk->data_off & 0xFF); |
---|
| 469 | if(size > 4) |
---|
| 470 | size = 4; |
---|
[69] | 471 | quoted_value = data_to_ascii(tmp_buf, 4, vk->type, &conv_error); |
---|
[43] | 472 | } |
---|
| 473 | else |
---|
| 474 | { |
---|
| 475 | /* XXX: This is a safety hack. No data fields have yet been found |
---|
| 476 | * larger, but length limits are probably better got from fields |
---|
| 477 | * in the registry itself, within reason. |
---|
| 478 | */ |
---|
| 479 | if(size > 16384) |
---|
[41] | 480 | { |
---|
[43] | 481 | fprintf(stderr, "WARNING: key size %d larger than " |
---|
| 482 | "16384, truncating...\n", size); |
---|
| 483 | size = 16384; |
---|
[41] | 484 | } |
---|
[61] | 485 | |
---|
[69] | 486 | quoted_value = data_to_ascii(vk->data, vk->data_size, |
---|
| 487 | vk->type, &conv_error); |
---|
[43] | 488 | } |
---|
| 489 | |
---|
| 490 | /* XXX: Sometimes value names can be NULL in registry. Need to |
---|
| 491 | * figure out why and when, and generate the appropriate output |
---|
| 492 | * for that condition. |
---|
| 493 | */ |
---|
[66] | 494 | quoted_name = quote_string(vk->valuename, common_special_chars); |
---|
[69] | 495 | |
---|
| 496 | if(quoted_value == NULL) |
---|
| 497 | { |
---|
| 498 | if(conv_error == NULL) |
---|
| 499 | fprintf(stderr, "ERROR: Could not quote value for '%s/%s'. " |
---|
| 500 | "Memory allocation failure likely.\n", prefix, quoted_name); |
---|
| 501 | else |
---|
| 502 | fprintf(stderr, "ERROR: Could not quote value for '%s/%s'. " |
---|
| 503 | "Returned error: %s\n", prefix, quoted_name, conv_error); |
---|
| 504 | } |
---|
| 505 | /* XXX: should these always be printed? */ |
---|
| 506 | else if(conv_error != NULL && print_verbose) |
---|
| 507 | fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', " |
---|
| 508 | "warning returned: %s\n", prefix, quoted_name, conv_error); |
---|
| 509 | |
---|
[43] | 510 | if(print_security) |
---|
[66] | 511 | printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name, |
---|
[41] | 512 | regfio_type_val2str(vk->type), quoted_value); |
---|
[43] | 513 | else |
---|
[66] | 514 | printf("%s/%s,%s,%s,\n", prefix, quoted_name, |
---|
[43] | 515 | regfio_type_val2str(vk->type), quoted_value); |
---|
| 516 | |
---|
| 517 | if(quoted_value != NULL) |
---|
| 518 | free(quoted_value); |
---|
| 519 | if(quoted_name != NULL) |
---|
| 520 | free(quoted_name); |
---|
[69] | 521 | if(conv_error != NULL) |
---|
| 522 | free(conv_error); |
---|
[32] | 523 | } |
---|
| 524 | |
---|
| 525 | |
---|
[33] | 526 | void printValueList(REGF_NK_REC* nk, char* prefix) |
---|
[32] | 527 | { |
---|
[37] | 528 | uint32 i; |
---|
[33] | 529 | |
---|
| 530 | for(i=0; i < nk->num_values; i++) |
---|
[43] | 531 | if(!type_filter_enabled || (nk->values[i].type == type_filter)) |
---|
[66] | 532 | printValue(nk->values+i, prefix); |
---|
[33] | 533 | } |
---|
| 534 | |
---|
[37] | 535 | |
---|
[43] | 536 | void printKey(REGF_NK_REC* k, char* full_path) |
---|
[33] | 537 | { |
---|
[43] | 538 | static char empty_str[1] = ""; |
---|
[42] | 539 | char* owner = NULL; |
---|
| 540 | char* group = NULL; |
---|
| 541 | char* sacl = NULL; |
---|
| 542 | char* dacl = NULL; |
---|
| 543 | char mtime[20]; |
---|
| 544 | time_t tmp_time[1]; |
---|
| 545 | struct tm* tmp_time_s = NULL; |
---|
| 546 | |
---|
[43] | 547 | *tmp_time = nt_time_to_unix(&k->mtime); |
---|
| 548 | tmp_time_s = gmtime(tmp_time); |
---|
| 549 | strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s); |
---|
| 550 | |
---|
| 551 | if(print_security) |
---|
| 552 | { |
---|
[53] | 553 | owner = regfio_get_owner(k->sec_desc->sec_desc); |
---|
| 554 | group = regfio_get_group(k->sec_desc->sec_desc); |
---|
| 555 | sacl = regfio_get_sacl(k->sec_desc->sec_desc); |
---|
| 556 | dacl = regfio_get_dacl(k->sec_desc->sec_desc); |
---|
[43] | 557 | if(owner == NULL) |
---|
| 558 | owner = empty_str; |
---|
| 559 | if(group == NULL) |
---|
| 560 | group = empty_str; |
---|
| 561 | if(sacl == NULL) |
---|
| 562 | sacl = empty_str; |
---|
| 563 | if(dacl == NULL) |
---|
| 564 | dacl = empty_str; |
---|
| 565 | |
---|
[66] | 566 | printf("%s,KEY,,%s,%s,%s,%s,%s\n", full_path, mtime, |
---|
[43] | 567 | owner, group, sacl, dacl); |
---|
| 568 | |
---|
| 569 | if(owner != empty_str) |
---|
| 570 | free(owner); |
---|
| 571 | if(group != empty_str) |
---|
| 572 | free(group); |
---|
| 573 | if(sacl != empty_str) |
---|
| 574 | free(sacl); |
---|
| 575 | if(dacl != empty_str) |
---|
| 576 | free(dacl); |
---|
| 577 | } |
---|
| 578 | else |
---|
[66] | 579 | printf("%s,KEY,,%s\n", full_path, mtime); |
---|
[43] | 580 | } |
---|
| 581 | |
---|
| 582 | |
---|
[66] | 583 | void printKeyTree(REGF_FILE* f, void_stack* nk_stack, const char* prefix) |
---|
[43] | 584 | { |
---|
[52] | 585 | REGF_NK_REC* cur = NULL; |
---|
| 586 | REGF_NK_REC* sub = NULL; |
---|
[43] | 587 | char* path = NULL; |
---|
| 588 | char* val_path = NULL; |
---|
[66] | 589 | uint32 val_path_len = 0; |
---|
| 590 | uint32 path_len = 0; |
---|
| 591 | uint32 prefix_len = strlen(prefix); |
---|
[41] | 592 | int key_type = regfio_type_str2val("KEY"); |
---|
[43] | 593 | |
---|
[32] | 594 | if((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL) |
---|
[31] | 595 | { |
---|
[33] | 596 | cur->subkey_index = 0; |
---|
| 597 | path = stack2Path(nk_stack); |
---|
[43] | 598 | |
---|
[54] | 599 | if(print_verbose) |
---|
| 600 | { |
---|
| 601 | if(prefix[0] == '\0') |
---|
| 602 | fprintf(stderr, "VERBOSE: Printing key tree under path: /\n"); |
---|
| 603 | else |
---|
| 604 | fprintf(stderr, "VERBOSE: Printing key tree under path: %s\n", |
---|
| 605 | prefix); |
---|
| 606 | } |
---|
| 607 | |
---|
[66] | 608 | path_len = strlen(path); |
---|
| 609 | val_path_len = prefix_len+path_len; |
---|
| 610 | val_path = (char*)malloc(val_path_len+1+1); |
---|
| 611 | if(val_path == NULL) |
---|
| 612 | bailOut(2, "ERROR: Could not allocate val_path.\n"); |
---|
| 613 | |
---|
| 614 | strcpy(val_path, prefix); |
---|
| 615 | strcpy(val_path+prefix_len, path); |
---|
[54] | 616 | if(val_path[0] == '\0') |
---|
| 617 | { |
---|
| 618 | val_path[0] = '/'; |
---|
| 619 | val_path[1] = '\0'; |
---|
| 620 | } |
---|
[43] | 621 | if(!type_filter_enabled || (key_type == type_filter)) |
---|
| 622 | printKey(cur, val_path); |
---|
[40] | 623 | if(!type_filter_enabled || (key_type != type_filter)) |
---|
[43] | 624 | printValueList(cur, val_path); |
---|
[66] | 625 | |
---|
[32] | 626 | while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL) |
---|
[31] | 627 | { |
---|
[66] | 628 | if((sub = regfio_fetch_subkey(f, cur)) == NULL) |
---|
[31] | 629 | { |
---|
[66] | 630 | sub = void_stack_pop(nk_stack); |
---|
| 631 | /* XXX: This is just a shallow free. Need to write deep free |
---|
| 632 | * routines to replace the Samba code for this. |
---|
| 633 | */ |
---|
| 634 | if(sub != NULL) |
---|
| 635 | free(sub); |
---|
| 636 | } |
---|
| 637 | else |
---|
| 638 | { |
---|
[33] | 639 | sub->subkey_index = 0; |
---|
[32] | 640 | void_stack_push(nk_stack, sub); |
---|
[33] | 641 | path = stack2Path(nk_stack); |
---|
[32] | 642 | if(path != NULL) |
---|
| 643 | { |
---|
[66] | 644 | path_len = strlen(path); |
---|
| 645 | if(val_path_len < prefix_len+path_len) |
---|
| 646 | { |
---|
| 647 | val_path_len = prefix_len+path_len; |
---|
| 648 | val_path = (char*)realloc(val_path, val_path_len+1); |
---|
| 649 | if(val_path == NULL) |
---|
| 650 | bailOut(2, "ERROR: Could not reallocate val_path.\n"); |
---|
| 651 | } |
---|
| 652 | strcpy(val_path, prefix); |
---|
| 653 | strcpy(val_path+prefix_len, path); |
---|
[40] | 654 | if(!type_filter_enabled || (key_type == type_filter)) |
---|
[43] | 655 | printKey(sub, val_path); |
---|
[40] | 656 | if(!type_filter_enabled || (key_type != type_filter)) |
---|
| 657 | printValueList(sub, val_path); |
---|
[32] | 658 | } |
---|
[31] | 659 | } |
---|
| 660 | } |
---|
| 661 | } |
---|
[66] | 662 | if(val_path != NULL) |
---|
| 663 | free(val_path); |
---|
[54] | 664 | if(print_verbose) |
---|
| 665 | fprintf(stderr, "VERBOSE: Finished printing key tree.\n"); |
---|
[30] | 666 | } |
---|
| 667 | |
---|
| 668 | |
---|
[33] | 669 | /* |
---|
| 670 | * Returns 0 if path was found. |
---|
| 671 | * Returns 1 if path was not found. |
---|
| 672 | * Returns less than 0 on other error. |
---|
| 673 | */ |
---|
| 674 | int retrievePath(REGF_FILE* f, void_stack* nk_stack, |
---|
| 675 | void_stack* path_stack) |
---|
| 676 | { |
---|
[52] | 677 | REGF_NK_REC* sub = NULL; |
---|
| 678 | REGF_NK_REC* cur = NULL; |
---|
[33] | 679 | void_stack* sub_nk_stack; |
---|
| 680 | char* prefix; |
---|
| 681 | char* cur_str = NULL; |
---|
[53] | 682 | char* path = NULL; |
---|
[66] | 683 | char* name; |
---|
| 684 | uint16 path_depth; |
---|
| 685 | uint32 i, prefix_len; |
---|
[33] | 686 | bool found_cur = true; |
---|
| 687 | if(path_stack == NULL) |
---|
| 688 | return -1; |
---|
| 689 | |
---|
| 690 | path_depth = void_stack_size(path_stack); |
---|
| 691 | if(path_depth < 1) |
---|
| 692 | return -2; |
---|
| 693 | |
---|
| 694 | if(void_stack_size(nk_stack) < 1) |
---|
| 695 | return -3; |
---|
| 696 | cur = (REGF_NK_REC*)void_stack_cur(nk_stack); |
---|
| 697 | |
---|
[54] | 698 | if(print_verbose) |
---|
| 699 | fprintf(stderr, "VERBOSE: Beginning retrieval of specified path: %s\n", |
---|
| 700 | path_filter); |
---|
| 701 | |
---|
[33] | 702 | while(void_stack_size(path_stack) > 1) |
---|
| 703 | { |
---|
| 704 | /* Search key records only */ |
---|
| 705 | cur_str = (char*)void_stack_pop(path_stack); |
---|
| 706 | |
---|
| 707 | found_cur = false; |
---|
| 708 | while(!found_cur && |
---|
| 709 | (sub = regfio_fetch_subkey(f, cur)) != NULL) |
---|
| 710 | { |
---|
| 711 | if(strcasecmp(sub->keyname, cur_str) == 0) |
---|
| 712 | { |
---|
| 713 | cur = sub; |
---|
| 714 | void_stack_push(nk_stack, sub); |
---|
| 715 | found_cur = true; |
---|
| 716 | } |
---|
| 717 | } |
---|
[54] | 718 | if(print_verbose && !found_cur) |
---|
| 719 | fprintf(stderr, "VERBOSE: Could not find KEY '%s' in specified path.\n", |
---|
| 720 | cur_str); |
---|
| 721 | |
---|
[39] | 722 | free(cur_str); |
---|
[33] | 723 | if(!found_cur) |
---|
[37] | 724 | return 1; |
---|
[33] | 725 | } |
---|
| 726 | |
---|
| 727 | /* Last round, search value and key records */ |
---|
| 728 | cur_str = (char*)void_stack_pop(path_stack); |
---|
| 729 | |
---|
[54] | 730 | if(print_verbose) |
---|
| 731 | fprintf(stderr, "VERBOSE: Searching values for last component" |
---|
| 732 | " of specified path.\n"); |
---|
| 733 | |
---|
[33] | 734 | for(i=0; (i < cur->num_values); i++) |
---|
| 735 | { |
---|
[48] | 736 | /* XXX: Not sure when/why this can be NULL, but it's happened. */ |
---|
| 737 | if(sub->values[i].valuename != NULL |
---|
| 738 | && strcasecmp(sub->values[i].valuename, cur_str) == 0) |
---|
[33] | 739 | { |
---|
[53] | 740 | path = stack2Path(nk_stack); |
---|
[54] | 741 | |
---|
| 742 | if(print_verbose) |
---|
| 743 | fprintf(stderr, "VERBOSE: Found final path element as value.\n"); |
---|
| 744 | |
---|
[56] | 745 | if(!type_filter_enabled || (sub->values[i].type == type_filter)) |
---|
[55] | 746 | printValue(&sub->values[i], path); |
---|
| 747 | if(path != NULL) |
---|
| 748 | free(path); |
---|
| 749 | |
---|
[33] | 750 | return 0; |
---|
| 751 | } |
---|
| 752 | } |
---|
| 753 | |
---|
[54] | 754 | if(print_verbose) |
---|
| 755 | fprintf(stderr, "VERBOSE: Searching keys for last component" |
---|
| 756 | " of specified path.\n"); |
---|
| 757 | |
---|
[33] | 758 | while((sub = regfio_fetch_subkey(f, cur)) != NULL) |
---|
| 759 | { |
---|
| 760 | if(strcasecmp(sub->keyname, cur_str) == 0) |
---|
| 761 | { |
---|
| 762 | sub_nk_stack = void_stack_new(1024); |
---|
| 763 | void_stack_push(sub_nk_stack, sub); |
---|
| 764 | prefix = stack2Path(nk_stack); |
---|
[52] | 765 | prefix_len = strlen(prefix); |
---|
| 766 | prefix = realloc(prefix, prefix_len+strlen(sub->keyname)+2); |
---|
| 767 | if(prefix == NULL) |
---|
| 768 | return -1; |
---|
[66] | 769 | name = quote_string(sub->keyname, key_special_chars); |
---|
[52] | 770 | strcat(prefix, "/"); |
---|
[66] | 771 | strcat(prefix, name); |
---|
| 772 | free(name); |
---|
[54] | 773 | |
---|
| 774 | if(print_verbose) |
---|
| 775 | fprintf(stderr, "VERBOSE: Found final path element as key.\n"); |
---|
| 776 | |
---|
[33] | 777 | printKeyTree(f, sub_nk_stack, prefix); |
---|
[54] | 778 | |
---|
[33] | 779 | return 0; |
---|
| 780 | } |
---|
| 781 | } |
---|
| 782 | |
---|
[54] | 783 | if(print_verbose) |
---|
| 784 | fprintf(stderr, "VERBOSE: Could not find last element of path.\n"); |
---|
| 785 | |
---|
[33] | 786 | return 1; |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | |
---|
[37] | 790 | static void usage(void) |
---|
| 791 | { |
---|
[61] | 792 | fprintf(stderr, "Usage: reglookup [-v] [-s]" |
---|
[40] | 793 | " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]" |
---|
[39] | 794 | " <REGISTRY_FILE>\n"); |
---|
[61] | 795 | fprintf(stderr, "Version: 0.3.0\n"); |
---|
[39] | 796 | fprintf(stderr, "Options:\n"); |
---|
| 797 | fprintf(stderr, "\t-v\t sets verbose mode.\n"); |
---|
[47] | 798 | fprintf(stderr, "\t-h\t enables header row. (default)\n"); |
---|
| 799 | fprintf(stderr, "\t-H\t disables header row.\n"); |
---|
[44] | 800 | fprintf(stderr, "\t-s\t enables security descriptor output.\n"); |
---|
| 801 | fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n"); |
---|
[40] | 802 | fprintf(stderr, "\t-p\t restrict output to elements below this path.\n"); |
---|
| 803 | fprintf(stderr, "\t-t\t restrict results to this specific data type.\n"); |
---|
[37] | 804 | fprintf(stderr, "\n"); |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | |
---|
[30] | 808 | int main(int argc, char** argv) |
---|
| 809 | { |
---|
[31] | 810 | void_stack* nk_stack; |
---|
[33] | 811 | void_stack* path_stack; |
---|
[31] | 812 | REGF_FILE* f; |
---|
| 813 | REGF_NK_REC* root; |
---|
[33] | 814 | int retr_path_ret; |
---|
[44] | 815 | uint32 argi, arge; |
---|
[31] | 816 | |
---|
[37] | 817 | /* Process command line arguments */ |
---|
[30] | 818 | if(argc < 2) |
---|
| 819 | { |
---|
[37] | 820 | usage(); |
---|
[44] | 821 | bailOut(1, "ERROR: Requires at least one argument.\n"); |
---|
[30] | 822 | } |
---|
[37] | 823 | |
---|
[44] | 824 | arge = argc-1; |
---|
| 825 | for(argi = 1; argi < arge; argi++) |
---|
[37] | 826 | { |
---|
[40] | 827 | if (strcmp("-p", argv[argi]) == 0) |
---|
[37] | 828 | { |
---|
[44] | 829 | if(++argi >= arge) |
---|
[37] | 830 | { |
---|
| 831 | usage(); |
---|
[40] | 832 | bailOut(1, "ERROR: '-p' option requires parameter.\n"); |
---|
[37] | 833 | } |
---|
[40] | 834 | if((path_filter = strdup(argv[argi])) == NULL) |
---|
[38] | 835 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
| 836 | |
---|
[40] | 837 | path_filter_enabled = true; |
---|
[37] | 838 | } |
---|
| 839 | else if (strcmp("-t", argv[argi]) == 0) |
---|
| 840 | { |
---|
[44] | 841 | if(++argi >= arge) |
---|
[37] | 842 | { |
---|
| 843 | usage(); |
---|
[38] | 844 | bailOut(1, "ERROR: '-t' option requires parameter.\n"); |
---|
[37] | 845 | } |
---|
[61] | 846 | if((type_filter = regfio_type_str2val(argv[argi])) < 0) |
---|
[40] | 847 | { |
---|
| 848 | fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]); |
---|
| 849 | bailOut(1, ""); |
---|
| 850 | } |
---|
[37] | 851 | type_filter_enabled = true; |
---|
| 852 | } |
---|
[47] | 853 | else if (strcmp("-h", argv[argi]) == 0) |
---|
| 854 | print_header = true; |
---|
| 855 | else if (strcmp("-H", argv[argi]) == 0) |
---|
| 856 | print_header = false; |
---|
[37] | 857 | else if (strcmp("-s", argv[argi]) == 0) |
---|
| 858 | print_security = true; |
---|
[44] | 859 | else if (strcmp("-S", argv[argi]) == 0) |
---|
| 860 | print_security = false; |
---|
[37] | 861 | else if (strcmp("-v", argv[argi]) == 0) |
---|
| 862 | print_verbose = true; |
---|
[44] | 863 | else |
---|
[37] | 864 | { |
---|
[38] | 865 | usage(); |
---|
[37] | 866 | fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]); |
---|
[38] | 867 | bailOut(1, ""); |
---|
[37] | 868 | } |
---|
| 869 | } |
---|
[44] | 870 | if((registry_file = strdup(argv[argi])) == NULL) |
---|
| 871 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
[30] | 872 | |
---|
[37] | 873 | f = regfio_open(registry_file); |
---|
| 874 | if(f == NULL) |
---|
| 875 | { |
---|
| 876 | fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file); |
---|
[38] | 877 | bailOut(3, ""); |
---|
[37] | 878 | } |
---|
[38] | 879 | |
---|
[31] | 880 | root = regfio_rootkey(f); |
---|
[37] | 881 | nk_stack = void_stack_new(1024); |
---|
[30] | 882 | |
---|
[31] | 883 | if(void_stack_push(nk_stack, root)) |
---|
[33] | 884 | { |
---|
[42] | 885 | if(print_header) |
---|
[43] | 886 | { |
---|
| 887 | if(print_security) |
---|
| 888 | printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL\n"); |
---|
| 889 | else |
---|
| 890 | printf("PATH,TYPE,VALUE,MTIME\n"); |
---|
| 891 | } |
---|
[42] | 892 | |
---|
[40] | 893 | path_stack = path2Stack(path_filter); |
---|
[33] | 894 | if(void_stack_size(path_stack) < 1) |
---|
| 895 | printKeyTree(f, nk_stack, ""); |
---|
| 896 | else |
---|
| 897 | { |
---|
[37] | 898 | retr_path_ret = retrievePath(f, nk_stack, path_stack); |
---|
[33] | 899 | if(retr_path_ret == 1) |
---|
[37] | 900 | fprintf(stderr, "WARNING: specified path not found.\n"); |
---|
[33] | 901 | else if(retr_path_ret != 0) |
---|
[38] | 902 | bailOut(4, "ERROR:\n"); |
---|
[33] | 903 | } |
---|
| 904 | } |
---|
[37] | 905 | else |
---|
[38] | 906 | bailOut(2, "ERROR: Memory allocation problem.\n"); |
---|
[31] | 907 | |
---|
[38] | 908 | void_stack_destroy_deep(nk_stack); |
---|
[30] | 909 | regfio_close(f); |
---|
| 910 | |
---|
| 911 | return 0; |
---|
| 912 | } |
---|