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