[30] | 1 | /* |
---|
[135] | 2 | * A utility to read a Windows NT and later registry files. |
---|
[30] | 3 | * |
---|
[135] | 4 | * Copyright (C) 2005-2009 Timothy D. Morgan |
---|
[42] | 5 | * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com |
---|
[30] | 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify |
---|
| 8 | * it under the terms of the GNU General Public License as published by |
---|
[111] | 9 | * the Free Software Foundation; version 3 of the License. |
---|
[30] | 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * GNU General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 19 | * |
---|
| 20 | * $Id: reglookup.c 150 2009-03-02 02:17:46Z tim $ |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | #include <stdlib.h> |
---|
| 25 | #include <stdio.h> |
---|
| 26 | #include <string.h> |
---|
[33] | 27 | #include <strings.h> |
---|
[42] | 28 | #include <time.h> |
---|
[147] | 29 | #include "regfi.h" |
---|
| 30 | #include "void_stack.h" |
---|
[30] | 31 | |
---|
[40] | 32 | /* Globals, influenced by command line parameters */ |
---|
| 33 | bool print_verbose = false; |
---|
| 34 | bool print_security = false; |
---|
[42] | 35 | bool print_header = true; |
---|
[40] | 36 | bool path_filter_enabled = false; |
---|
| 37 | bool type_filter_enabled = false; |
---|
| 38 | char* path_filter = NULL; |
---|
| 39 | int type_filter; |
---|
| 40 | char* registry_file = NULL; |
---|
| 41 | |
---|
[42] | 42 | /* Other globals */ |
---|
[135] | 43 | REGFI_FILE* f; |
---|
[66] | 44 | |
---|
[40] | 45 | |
---|
[116] | 46 | /* XXX: A hack to share some functions with reglookup-recover.c. |
---|
[125] | 47 | * Should move these into a proper library at some point. |
---|
[111] | 48 | */ |
---|
| 49 | #include "common.c" |
---|
[61] | 50 | |
---|
[38] | 51 | |
---|
[135] | 52 | void printValue(const REGFI_VK_REC* vk, char* prefix) |
---|
[41] | 53 | { |
---|
[111] | 54 | char* quoted_value = NULL; |
---|
| 55 | char* quoted_name = NULL; |
---|
| 56 | char* conv_error = NULL; |
---|
| 57 | const char* str_type = NULL; |
---|
| 58 | uint32 size = vk->data_size; |
---|
[41] | 59 | |
---|
[111] | 60 | /* Microsoft's documentation indicates that "available memory" is |
---|
| 61 | * the limit on value sizes. Annoying. We limit it to 1M which |
---|
| 62 | * should rarely be exceeded, unless the file is corrupt or |
---|
| 63 | * malicious. For more info, see: |
---|
| 64 | * http://msdn2.microsoft.com/en-us/library/ms724872.aspx |
---|
| 65 | */ |
---|
[135] | 66 | if(size > REGFI_VK_MAX_DATA_LENGTH) |
---|
[41] | 67 | { |
---|
[137] | 68 | fprintf(stderr, "WARN: value data size %d larger than " |
---|
[135] | 69 | "%d, truncating...\n", size, REGFI_VK_MAX_DATA_LENGTH); |
---|
| 70 | size = REGFI_VK_MAX_DATA_LENGTH; |
---|
[41] | 71 | } |
---|
[111] | 72 | |
---|
| 73 | quoted_name = quote_string(vk->valuename, key_special_chars); |
---|
| 74 | if (quoted_name == NULL) |
---|
| 75 | { /* Value names are NULL when we're looking at the "(default)" value. |
---|
| 76 | * Currently we just return a 0-length string to try an eliminate |
---|
| 77 | * ambiguity with a literal "(default)" value. The data type of a line |
---|
| 78 | * in the output allows one to differentiate between the parent key and |
---|
| 79 | * this value. |
---|
| 80 | */ |
---|
| 81 | quoted_name = malloc(1*sizeof(char)); |
---|
| 82 | if(quoted_name == NULL) |
---|
[143] | 83 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Could not allocate sufficient memory.\n"); |
---|
[111] | 84 | quoted_name[0] = '\0'; |
---|
| 85 | } |
---|
[41] | 86 | |
---|
[138] | 87 | if(vk->data == NULL) |
---|
[41] | 88 | { |
---|
[138] | 89 | if(print_verbose) |
---|
| 90 | fprintf(stderr, "INFO: While quoting value for '%s/%s', " |
---|
| 91 | "data pointer was NULL.\n", prefix, quoted_name); |
---|
[41] | 92 | } |
---|
[138] | 93 | else |
---|
| 94 | { |
---|
| 95 | quoted_value = data_to_ascii(vk->data, size, vk->type, &conv_error); |
---|
| 96 | if(quoted_value == NULL) |
---|
| 97 | { |
---|
| 98 | if(conv_error == NULL) |
---|
| 99 | fprintf(stderr, "WARN: Could not quote value for '%s/%s'. " |
---|
| 100 | "Memory allocation failure likely.\n", prefix, quoted_name); |
---|
| 101 | else |
---|
| 102 | fprintf(stderr, "WARN: Could not quote value for '%s/%s'. " |
---|
| 103 | "Returned error: %s\n", prefix, quoted_name, conv_error); |
---|
| 104 | } |
---|
| 105 | else if(conv_error != NULL && print_verbose) |
---|
| 106 | fprintf(stderr, "INFO: While quoting value for '%s/%s', " |
---|
| 107 | "warning returned: %s\n", prefix, quoted_name, conv_error); |
---|
| 108 | } |
---|
[41] | 109 | |
---|
[111] | 110 | str_type = regfi_type_val2str(vk->type); |
---|
| 111 | if(print_security) |
---|
[41] | 112 | { |
---|
[111] | 113 | if(str_type == NULL) |
---|
| 114 | printf("%s/%s,0x%.8X,%s,,,,,\n", prefix, quoted_name, |
---|
| 115 | vk->type, quoted_value); |
---|
[66] | 116 | else |
---|
[111] | 117 | printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name, |
---|
| 118 | str_type, quoted_value); |
---|
[71] | 119 | } |
---|
[111] | 120 | else |
---|
| 121 | { |
---|
| 122 | if(str_type == NULL) |
---|
| 123 | printf("%s/%s,0x%.8X,%s,\n", prefix, quoted_name, |
---|
| 124 | vk->type, quoted_value); |
---|
| 125 | else |
---|
| 126 | printf("%s/%s,%s,%s,\n", prefix, quoted_name, |
---|
| 127 | str_type, quoted_value); |
---|
| 128 | } |
---|
[42] | 129 | |
---|
[111] | 130 | if(quoted_value != NULL) |
---|
| 131 | free(quoted_value); |
---|
| 132 | if(quoted_name != NULL) |
---|
| 133 | free(quoted_name); |
---|
| 134 | if(conv_error != NULL) |
---|
| 135 | free(conv_error); |
---|
[41] | 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
[81] | 139 | char** splitPath(const char* s) |
---|
[30] | 140 | { |
---|
[81] | 141 | char** ret_val; |
---|
[38] | 142 | const char* cur = s; |
---|
[33] | 143 | char* next = NULL; |
---|
[38] | 144 | char* copy; |
---|
[81] | 145 | uint32 ret_cur = 0; |
---|
[38] | 146 | |
---|
[135] | 147 | ret_val = (char**)malloc((REGFI_MAX_DEPTH+1+1)*sizeof(char**)); |
---|
[81] | 148 | if (ret_val == NULL) |
---|
[38] | 149 | return NULL; |
---|
[81] | 150 | ret_val[0] = NULL; |
---|
| 151 | |
---|
| 152 | /* We return a well-formed, 0-length, path even when input is icky. */ |
---|
[37] | 153 | if (s == NULL) |
---|
[81] | 154 | return ret_val; |
---|
[38] | 155 | |
---|
| 156 | while((next = strchr(cur, '/')) != NULL) |
---|
[33] | 157 | { |
---|
[38] | 158 | if ((next-cur) > 0) |
---|
| 159 | { |
---|
| 160 | copy = (char*)malloc((next-cur+1)*sizeof(char)); |
---|
| 161 | if(copy == NULL) |
---|
[143] | 162 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Memory allocation problem.\n"); |
---|
[38] | 163 | |
---|
| 164 | memcpy(copy, cur, next-cur); |
---|
| 165 | copy[next-cur] = '\0'; |
---|
[81] | 166 | ret_val[ret_cur++] = copy; |
---|
[135] | 167 | if(ret_cur < (REGFI_MAX_DEPTH+1+1)) |
---|
[81] | 168 | ret_val[ret_cur] = NULL; |
---|
| 169 | else |
---|
[143] | 170 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: Registry maximum depth exceeded.\n"); |
---|
[38] | 171 | } |
---|
| 172 | cur = next+1; |
---|
[33] | 173 | } |
---|
[81] | 174 | |
---|
| 175 | /* Grab last element, if path doesn't end in '/'. */ |
---|
[33] | 176 | if(strlen(cur) > 0) |
---|
[38] | 177 | { |
---|
| 178 | copy = strdup(cur); |
---|
[81] | 179 | ret_val[ret_cur++] = copy; |
---|
[135] | 180 | if(ret_cur < (REGFI_MAX_DEPTH+1+1)) |
---|
[81] | 181 | ret_val[ret_cur] = NULL; |
---|
| 182 | else |
---|
[143] | 183 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: Registry maximum depth exceeded.\n"); |
---|
[38] | 184 | } |
---|
[33] | 185 | |
---|
| 186 | return ret_val; |
---|
| 187 | } |
---|
| 188 | |
---|
[81] | 189 | |
---|
[83] | 190 | void freePath(char** path) |
---|
| 191 | { |
---|
| 192 | uint32 i; |
---|
| 193 | |
---|
| 194 | if(path == NULL) |
---|
| 195 | return; |
---|
| 196 | |
---|
| 197 | for(i=0; path[i] != NULL; i++) |
---|
| 198 | free(path[i]); |
---|
| 199 | |
---|
| 200 | free(path); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | |
---|
[81] | 204 | /* Returns a quoted path from an iterator's stack */ |
---|
| 205 | char* iter2Path(REGFI_ITERATOR* i) |
---|
[33] | 206 | { |
---|
[81] | 207 | const REGFI_ITER_POSITION* cur; |
---|
[37] | 208 | uint32 buf_left = 127; |
---|
| 209 | uint32 buf_len = buf_left+1; |
---|
| 210 | uint32 name_len = 0; |
---|
| 211 | uint32 grow_amt; |
---|
[81] | 212 | char* buf; |
---|
[31] | 213 | char* new_buf; |
---|
[66] | 214 | char* name; |
---|
[81] | 215 | const char* cur_name; |
---|
[31] | 216 | void_stack_iterator* iter; |
---|
| 217 | |
---|
| 218 | buf = (char*)malloc((buf_len)*sizeof(char)); |
---|
| 219 | if (buf == NULL) |
---|
| 220 | return NULL; |
---|
[54] | 221 | buf[0] = '\0'; |
---|
[30] | 222 | |
---|
[81] | 223 | iter = void_stack_iterator_new(i->key_positions); |
---|
[31] | 224 | if (iter == NULL) |
---|
[30] | 225 | { |
---|
[31] | 226 | free(buf); |
---|
| 227 | return NULL; |
---|
[30] | 228 | } |
---|
| 229 | |
---|
[33] | 230 | /* skip root element */ |
---|
[81] | 231 | if(void_stack_size(i->key_positions) < 1) |
---|
| 232 | { |
---|
| 233 | buf[0] = '/'; |
---|
| 234 | buf[1] = '\0'; |
---|
| 235 | return buf; |
---|
| 236 | } |
---|
[33] | 237 | cur = void_stack_iterator_next(iter); |
---|
| 238 | |
---|
[81] | 239 | do |
---|
[31] | 240 | { |
---|
[81] | 241 | cur = void_stack_iterator_next(iter); |
---|
| 242 | if (cur == NULL) |
---|
| 243 | cur_name = i->cur_key->keyname; |
---|
| 244 | else |
---|
| 245 | cur_name = cur->nk->keyname; |
---|
| 246 | |
---|
[33] | 247 | buf[buf_len-buf_left-1] = '/'; |
---|
| 248 | buf_left -= 1; |
---|
[81] | 249 | name = quote_string(cur_name, key_special_chars); |
---|
[66] | 250 | name_len = strlen(name); |
---|
[31] | 251 | if(name_len+1 > buf_left) |
---|
| 252 | { |
---|
[37] | 253 | grow_amt = (uint32)(buf_len/2); |
---|
[31] | 254 | buf_len += name_len+1+grow_amt-buf_left; |
---|
| 255 | if((new_buf = realloc(buf, buf_len)) == NULL) |
---|
| 256 | { |
---|
[136] | 257 | free(name); |
---|
[31] | 258 | free(buf); |
---|
| 259 | free(iter); |
---|
| 260 | return NULL; |
---|
| 261 | } |
---|
| 262 | buf = new_buf; |
---|
| 263 | buf_left = grow_amt + name_len + 1; |
---|
| 264 | } |
---|
[66] | 265 | strncpy(buf+(buf_len-buf_left-1), name, name_len); |
---|
[31] | 266 | buf_left -= name_len; |
---|
| 267 | buf[buf_len-buf_left-1] = '\0'; |
---|
[66] | 268 | free(name); |
---|
[81] | 269 | } while(cur != NULL); |
---|
[30] | 270 | |
---|
[31] | 271 | return buf; |
---|
| 272 | } |
---|
[30] | 273 | |
---|
[31] | 274 | |
---|
[137] | 275 | void printValueList(REGFI_ITERATOR* iter, char* prefix) |
---|
[32] | 276 | { |
---|
[150] | 277 | REGFI_VK_REC* value; |
---|
[80] | 278 | |
---|
[137] | 279 | value = regfi_iterator_first_value(iter); |
---|
[80] | 280 | while(value != NULL) |
---|
[81] | 281 | { |
---|
| 282 | if(!type_filter_enabled || (value->type == type_filter)) |
---|
[80] | 283 | printValue(value, prefix); |
---|
[150] | 284 | regfi_free_value(value); |
---|
[137] | 285 | value = regfi_iterator_next_value(iter); |
---|
[138] | 286 | printMsgs(iter->f); |
---|
[81] | 287 | } |
---|
[33] | 288 | } |
---|
| 289 | |
---|
[37] | 290 | |
---|
[137] | 291 | void printKey(REGFI_ITERATOR* iter, char* full_path) |
---|
[33] | 292 | { |
---|
[43] | 293 | static char empty_str[1] = ""; |
---|
[42] | 294 | char* owner = NULL; |
---|
| 295 | char* group = NULL; |
---|
| 296 | char* sacl = NULL; |
---|
| 297 | char* dacl = NULL; |
---|
[125] | 298 | char* quoted_classname; |
---|
[126] | 299 | char* error_msg = NULL; |
---|
[42] | 300 | char mtime[20]; |
---|
| 301 | time_t tmp_time[1]; |
---|
| 302 | struct tm* tmp_time_s = NULL; |
---|
[135] | 303 | const REGFI_SK_REC* sk; |
---|
[137] | 304 | const REGFI_NK_REC* k = regfi_iterator_cur_key(iter); |
---|
[42] | 305 | |
---|
[43] | 306 | *tmp_time = nt_time_to_unix(&k->mtime); |
---|
| 307 | tmp_time_s = gmtime(tmp_time); |
---|
| 308 | strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s); |
---|
| 309 | |
---|
[137] | 310 | if(print_security && (sk=regfi_iterator_cur_sk(iter))) |
---|
[43] | 311 | { |
---|
[109] | 312 | owner = regfi_get_owner(sk->sec_desc); |
---|
| 313 | group = regfi_get_group(sk->sec_desc); |
---|
| 314 | sacl = regfi_get_sacl(sk->sec_desc); |
---|
| 315 | dacl = regfi_get_dacl(sk->sec_desc); |
---|
[43] | 316 | if(owner == NULL) |
---|
| 317 | owner = empty_str; |
---|
| 318 | if(group == NULL) |
---|
| 319 | group = empty_str; |
---|
| 320 | if(sacl == NULL) |
---|
| 321 | sacl = empty_str; |
---|
| 322 | if(dacl == NULL) |
---|
| 323 | dacl = empty_str; |
---|
| 324 | |
---|
[125] | 325 | if(k->classname != NULL) |
---|
[126] | 326 | { |
---|
| 327 | quoted_classname = quote_unicode((uint8*)k->classname, k->classname_length, |
---|
| 328 | key_special_chars, &error_msg); |
---|
| 329 | if(quoted_classname == NULL) |
---|
| 330 | { |
---|
| 331 | if(error_msg == NULL) |
---|
| 332 | fprintf(stderr, "ERROR: Could not quote classname" |
---|
| 333 | " for key '%s' due to unknown error.\n", full_path); |
---|
| 334 | else |
---|
| 335 | { |
---|
| 336 | fprintf(stderr, "ERROR: Could not quote classname" |
---|
| 337 | " for key '%s' due to error: %s\n", full_path, error_msg); |
---|
| 338 | free(error_msg); |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | else if (error_msg != NULL) |
---|
| 342 | { |
---|
| 343 | if(print_verbose) |
---|
[138] | 344 | fprintf(stderr, "INFO: While converting classname" |
---|
[126] | 345 | " for key '%s': %s.\n", full_path, error_msg); |
---|
| 346 | free(error_msg); |
---|
| 347 | } |
---|
| 348 | } |
---|
[125] | 349 | else |
---|
| 350 | quoted_classname = empty_str; |
---|
[43] | 351 | |
---|
[138] | 352 | printMsgs(iter->f); |
---|
[125] | 353 | printf("%s,KEY,,%s,%s,%s,%s,%s,%s\n", full_path, mtime, |
---|
| 354 | owner, group, sacl, dacl, quoted_classname); |
---|
| 355 | |
---|
[43] | 356 | if(owner != empty_str) |
---|
| 357 | free(owner); |
---|
| 358 | if(group != empty_str) |
---|
| 359 | free(group); |
---|
| 360 | if(sacl != empty_str) |
---|
| 361 | free(sacl); |
---|
| 362 | if(dacl != empty_str) |
---|
| 363 | free(dacl); |
---|
[125] | 364 | if(quoted_classname != empty_str) |
---|
| 365 | free(quoted_classname); |
---|
[43] | 366 | } |
---|
| 367 | else |
---|
[66] | 368 | printf("%s,KEY,,%s\n", full_path, mtime); |
---|
[43] | 369 | } |
---|
| 370 | |
---|
| 371 | |
---|
[81] | 372 | void printKeyTree(REGFI_ITERATOR* iter) |
---|
[43] | 373 | { |
---|
[135] | 374 | const REGFI_NK_REC* root = NULL; |
---|
| 375 | const REGFI_NK_REC* cur = NULL; |
---|
[150] | 376 | REGFI_NK_REC* sub = NULL; |
---|
[43] | 377 | char* path = NULL; |
---|
[78] | 378 | int key_type = regfi_type_str2val("KEY"); |
---|
[81] | 379 | bool print_this = true; |
---|
| 380 | |
---|
| 381 | root = cur = regfi_iterator_cur_key(iter); |
---|
| 382 | sub = regfi_iterator_first_subkey(iter); |
---|
[138] | 383 | printMsgs(iter->f); |
---|
[137] | 384 | |
---|
[81] | 385 | if(root == NULL) |
---|
[143] | 386 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: root cannot be NULL.\n"); |
---|
[81] | 387 | |
---|
| 388 | do |
---|
[31] | 389 | { |
---|
[81] | 390 | if(print_this) |
---|
[54] | 391 | { |
---|
[81] | 392 | path = iter2Path(iter); |
---|
| 393 | if(path == NULL) |
---|
[143] | 394 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Could not construct iterator's path.\n"); |
---|
[137] | 395 | |
---|
[81] | 396 | if(!type_filter_enabled || (key_type == type_filter)) |
---|
[109] | 397 | printKey(iter, path); |
---|
[81] | 398 | if(!type_filter_enabled || (key_type != type_filter)) |
---|
| 399 | printValueList(iter, path); |
---|
| 400 | |
---|
| 401 | free(path); |
---|
[54] | 402 | } |
---|
[66] | 403 | |
---|
[81] | 404 | if(sub == NULL) |
---|
[31] | 405 | { |
---|
[81] | 406 | if(cur != root) |
---|
[31] | 407 | { |
---|
[81] | 408 | /* We're done with this sub-tree, going up and hitting other branches. */ |
---|
| 409 | if(!regfi_iterator_up(iter)) |
---|
[137] | 410 | { |
---|
[138] | 411 | printMsgs(iter->f); |
---|
[143] | 412 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: could not traverse iterator upward.\n"); |
---|
[137] | 413 | } |
---|
| 414 | |
---|
[81] | 415 | cur = regfi_iterator_cur_key(iter); |
---|
| 416 | if(cur == NULL) |
---|
[137] | 417 | { |
---|
[138] | 418 | printMsgs(iter->f); |
---|
[143] | 419 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: unexpected NULL for key.\n"); |
---|
[137] | 420 | } |
---|
[150] | 421 | |
---|
[81] | 422 | sub = regfi_iterator_next_subkey(iter); |
---|
[66] | 423 | } |
---|
[81] | 424 | print_this = false; |
---|
[31] | 425 | } |
---|
[81] | 426 | else |
---|
| 427 | { /* We have unexplored sub-keys. |
---|
| 428 | * Let's move down and print this first sub-tree out. |
---|
| 429 | */ |
---|
| 430 | if(!regfi_iterator_down(iter)) |
---|
[137] | 431 | { |
---|
[138] | 432 | printMsgs(iter->f); |
---|
[143] | 433 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: could not traverse iterator downward.\n"); |
---|
[137] | 434 | } |
---|
[81] | 435 | |
---|
[150] | 436 | cur = regfi_iterator_cur_key(iter); |
---|
| 437 | regfi_free_key(sub); |
---|
[81] | 438 | sub = regfi_iterator_first_subkey(iter); |
---|
| 439 | print_this = true; |
---|
| 440 | } |
---|
[138] | 441 | printMsgs(iter->f); |
---|
[81] | 442 | } while(!((cur == root) && (sub == NULL))); |
---|
| 443 | |
---|
[54] | 444 | if(print_verbose) |
---|
[138] | 445 | fprintf(stderr, "INFO: Finished printing key tree.\n"); |
---|
[30] | 446 | } |
---|
| 447 | |
---|
[81] | 448 | |
---|
[140] | 449 | /* XXX: What if there is BOTH a value AND a key with that name?? |
---|
| 450 | * What if there are multiple keys/values with the same name?? |
---|
| 451 | */ |
---|
[33] | 452 | /* |
---|
[80] | 453 | * Returns 0 if path was not found. |
---|
| 454 | * Returns 1 if path was found as value. |
---|
| 455 | * Returns 2 if path was found as key. |
---|
[33] | 456 | * Returns less than 0 on other error. |
---|
| 457 | */ |
---|
[80] | 458 | int retrievePath(REGFI_ITERATOR* iter, char** path) |
---|
[33] | 459 | { |
---|
[150] | 460 | REGFI_VK_REC* value; |
---|
[81] | 461 | char* tmp_path_joined; |
---|
| 462 | const char** tmp_path; |
---|
[80] | 463 | uint32 i; |
---|
| 464 | |
---|
| 465 | if(path == NULL) |
---|
[33] | 466 | return -1; |
---|
| 467 | |
---|
[80] | 468 | /* One extra for any value at the end, and one more for NULL */ |
---|
[135] | 469 | tmp_path = (const char**)malloc(sizeof(const char**)*(REGFI_MAX_DEPTH+1+1)); |
---|
[80] | 470 | if(tmp_path == NULL) |
---|
[33] | 471 | return -2; |
---|
| 472 | |
---|
[80] | 473 | /* Strip any potential value name at end of path */ |
---|
| 474 | for(i=0; |
---|
[136] | 475 | (path[i] != NULL) && (path[i+1] != NULL) && (i < REGFI_MAX_DEPTH+1); |
---|
[80] | 476 | i++) |
---|
[136] | 477 | { tmp_path[i] = path[i]; } |
---|
[80] | 478 | tmp_path[i] = NULL; |
---|
| 479 | |
---|
[54] | 480 | if(print_verbose) |
---|
[138] | 481 | fprintf(stderr, "INFO: Attempting to retrieve specified path: %s\n", |
---|
[54] | 482 | path_filter); |
---|
| 483 | |
---|
[82] | 484 | /* Special check for '/' path filter */ |
---|
| 485 | if(path[0] == NULL) |
---|
| 486 | { |
---|
| 487 | if(print_verbose) |
---|
[138] | 488 | fprintf(stderr, "INFO: Found final path element as root key.\n"); |
---|
[136] | 489 | free(tmp_path); |
---|
[82] | 490 | return 2; |
---|
| 491 | } |
---|
| 492 | |
---|
[80] | 493 | if(!regfi_iterator_walk_path(iter, tmp_path)) |
---|
[33] | 494 | { |
---|
[138] | 495 | printMsgs(iter->f); |
---|
[80] | 496 | free(tmp_path); |
---|
| 497 | return 0; |
---|
[33] | 498 | } |
---|
| 499 | |
---|
[80] | 500 | if(regfi_iterator_find_value(iter, path[i])) |
---|
| 501 | { |
---|
| 502 | if(print_verbose) |
---|
[138] | 503 | fprintf(stderr, "INFO: Found final path element as value.\n"); |
---|
[33] | 504 | |
---|
[80] | 505 | value = regfi_iterator_cur_value(iter); |
---|
[138] | 506 | printMsgs(iter->f); |
---|
[81] | 507 | tmp_path_joined = iter2Path(iter); |
---|
[54] | 508 | |
---|
[80] | 509 | if((value == NULL) || (tmp_path_joined == NULL)) |
---|
[143] | 510 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Unexpected error before printValue.\n"); |
---|
[54] | 511 | |
---|
[121] | 512 | if(!type_filter_enabled || (value->type == type_filter)) |
---|
| 513 | printValue(value, tmp_path_joined); |
---|
[54] | 514 | |
---|
[150] | 515 | regfi_free_value(value); |
---|
[80] | 516 | free(tmp_path); |
---|
| 517 | free(tmp_path_joined); |
---|
| 518 | return 1; |
---|
[33] | 519 | } |
---|
[80] | 520 | else if(regfi_iterator_find_subkey(iter, path[i])) |
---|
[33] | 521 | { |
---|
[138] | 522 | printMsgs(iter->f); |
---|
[80] | 523 | if(print_verbose) |
---|
[138] | 524 | fprintf(stderr, "INFO: Found final path element as key.\n"); |
---|
[82] | 525 | |
---|
| 526 | if(!regfi_iterator_down(iter)) |
---|
[137] | 527 | { |
---|
[138] | 528 | printMsgs(iter->f); |
---|
[143] | 529 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: Unexpected error on traversing path filter key.\n"); |
---|
[137] | 530 | } |
---|
[82] | 531 | |
---|
[80] | 532 | return 2; |
---|
[33] | 533 | } |
---|
[138] | 534 | printMsgs(iter->f); |
---|
[33] | 535 | |
---|
[54] | 536 | if(print_verbose) |
---|
[138] | 537 | fprintf(stderr, "INFO: Could not find last element of path.\n"); |
---|
[54] | 538 | |
---|
[80] | 539 | return 0; |
---|
[33] | 540 | } |
---|
| 541 | |
---|
| 542 | |
---|
[37] | 543 | static void usage(void) |
---|
| 544 | { |
---|
[61] | 545 | fprintf(stderr, "Usage: reglookup [-v] [-s]" |
---|
[40] | 546 | " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]" |
---|
[39] | 547 | " <REGISTRY_FILE>\n"); |
---|
[111] | 548 | fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION); |
---|
[39] | 549 | fprintf(stderr, "Options:\n"); |
---|
| 550 | fprintf(stderr, "\t-v\t sets verbose mode.\n"); |
---|
[47] | 551 | fprintf(stderr, "\t-h\t enables header row. (default)\n"); |
---|
| 552 | fprintf(stderr, "\t-H\t disables header row.\n"); |
---|
[44] | 553 | fprintf(stderr, "\t-s\t enables security descriptor output.\n"); |
---|
| 554 | fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n"); |
---|
[40] | 555 | fprintf(stderr, "\t-p\t restrict output to elements below this path.\n"); |
---|
| 556 | fprintf(stderr, "\t-t\t restrict results to this specific data type.\n"); |
---|
[37] | 557 | fprintf(stderr, "\n"); |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | |
---|
[30] | 561 | int main(int argc, char** argv) |
---|
| 562 | { |
---|
[80] | 563 | char** path = NULL; |
---|
| 564 | REGFI_ITERATOR* iter; |
---|
[33] | 565 | int retr_path_ret; |
---|
[44] | 566 | uint32 argi, arge; |
---|
[31] | 567 | |
---|
[37] | 568 | /* Process command line arguments */ |
---|
[30] | 569 | if(argc < 2) |
---|
| 570 | { |
---|
[37] | 571 | usage(); |
---|
[143] | 572 | bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: Requires at least one argument.\n"); |
---|
[30] | 573 | } |
---|
[37] | 574 | |
---|
[44] | 575 | arge = argc-1; |
---|
| 576 | for(argi = 1; argi < arge; argi++) |
---|
[37] | 577 | { |
---|
[40] | 578 | if (strcmp("-p", argv[argi]) == 0) |
---|
[37] | 579 | { |
---|
[44] | 580 | if(++argi >= arge) |
---|
[37] | 581 | { |
---|
| 582 | usage(); |
---|
[143] | 583 | bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: '-p' option requires parameter.\n"); |
---|
[37] | 584 | } |
---|
[40] | 585 | if((path_filter = strdup(argv[argi])) == NULL) |
---|
[143] | 586 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Memory allocation problem.\n"); |
---|
[38] | 587 | |
---|
[40] | 588 | path_filter_enabled = true; |
---|
[37] | 589 | } |
---|
| 590 | else if (strcmp("-t", argv[argi]) == 0) |
---|
| 591 | { |
---|
[44] | 592 | if(++argi >= arge) |
---|
[37] | 593 | { |
---|
| 594 | usage(); |
---|
[143] | 595 | bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: '-t' option requires parameter.\n"); |
---|
[37] | 596 | } |
---|
[78] | 597 | if((type_filter = regfi_type_str2val(argv[argi])) < 0) |
---|
[40] | 598 | { |
---|
| 599 | fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]); |
---|
[143] | 600 | bailOut(REGLOOKUP_EXIT_USAGE, ""); |
---|
[40] | 601 | } |
---|
[37] | 602 | type_filter_enabled = true; |
---|
| 603 | } |
---|
[47] | 604 | else if (strcmp("-h", argv[argi]) == 0) |
---|
| 605 | print_header = true; |
---|
| 606 | else if (strcmp("-H", argv[argi]) == 0) |
---|
| 607 | print_header = false; |
---|
[37] | 608 | else if (strcmp("-s", argv[argi]) == 0) |
---|
| 609 | print_security = true; |
---|
[44] | 610 | else if (strcmp("-S", argv[argi]) == 0) |
---|
| 611 | print_security = false; |
---|
[37] | 612 | else if (strcmp("-v", argv[argi]) == 0) |
---|
| 613 | print_verbose = true; |
---|
[44] | 614 | else |
---|
[37] | 615 | { |
---|
[38] | 616 | usage(); |
---|
[37] | 617 | fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]); |
---|
[143] | 618 | bailOut(REGLOOKUP_EXIT_USAGE, ""); |
---|
[37] | 619 | } |
---|
| 620 | } |
---|
[44] | 621 | if((registry_file = strdup(argv[argi])) == NULL) |
---|
[143] | 622 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Memory allocation problem.\n"); |
---|
[30] | 623 | |
---|
[110] | 624 | f = regfi_open(registry_file); |
---|
[37] | 625 | if(f == NULL) |
---|
| 626 | { |
---|
| 627 | fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file); |
---|
[143] | 628 | bailOut(REGLOOKUP_EXIT_NOINPUT, ""); |
---|
[37] | 629 | } |
---|
[38] | 630 | |
---|
[138] | 631 | if(print_verbose) |
---|
| 632 | regfi_set_message_mask(f, REGFI_MSG_INFO|REGFI_MSG_WARN|REGFI_MSG_ERROR); |
---|
| 633 | |
---|
[80] | 634 | iter = regfi_iterator_new(f); |
---|
| 635 | if(iter == NULL) |
---|
[143] | 636 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Couldn't create registry iterator.\n"); |
---|
[30] | 637 | |
---|
[81] | 638 | if(print_header) |
---|
| 639 | { |
---|
| 640 | if(print_security) |
---|
[125] | 641 | printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL,CLASS\n"); |
---|
[81] | 642 | else |
---|
| 643 | printf("PATH,TYPE,VALUE,MTIME\n"); |
---|
| 644 | } |
---|
| 645 | |
---|
[80] | 646 | if(path_filter_enabled && path_filter != NULL) |
---|
| 647 | path = splitPath(path_filter); |
---|
[81] | 648 | |
---|
[80] | 649 | if(path != NULL) |
---|
[33] | 650 | { |
---|
[80] | 651 | retr_path_ret = retrievePath(iter, path); |
---|
[138] | 652 | printMsgs(iter->f); |
---|
[83] | 653 | freePath(path); |
---|
| 654 | |
---|
[80] | 655 | if(retr_path_ret == 0) |
---|
[141] | 656 | fprintf(stderr, "WARN: Specified path '%s' not found.\n", path_filter); |
---|
[80] | 657 | else if (retr_path_ret == 2) |
---|
[81] | 658 | printKeyTree(iter); |
---|
[93] | 659 | else if(retr_path_ret < 0) |
---|
| 660 | { |
---|
| 661 | fprintf(stderr, "ERROR: retrievePath() returned %d.\n", |
---|
| 662 | retr_path_ret); |
---|
[143] | 663 | bailOut(REGLOOKUP_EXIT_DATAERR, |
---|
| 664 | "ERROR: Unknown error occurred in retrieving path.\n"); |
---|
[93] | 665 | } |
---|
[33] | 666 | } |
---|
[37] | 667 | else |
---|
[81] | 668 | printKeyTree(iter); |
---|
[31] | 669 | |
---|
[80] | 670 | regfi_iterator_free(iter); |
---|
[78] | 671 | regfi_close(f); |
---|
[30] | 672 | |
---|
| 673 | return 0; |
---|
| 674 | } |
---|