[30] | 1 | /* |
---|
[135] | 2 | * A utility to read a Windows NT and later registry files. |
---|
[30] | 3 | * |
---|
[261] | 4 | * Copyright (C) 2005-2011 Timothy D. Morgan |
---|
[170] | 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 261 2011-06-17 00:55:49Z 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); |
---|
[251] | 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 | |
---|
[252] | 229 | path = regfi_iterator_ancestry(i); |
---|
[249] | 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 | |
---|
[251] | 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); |
---|
[252] | 383 | cur = root = regfi_reference_record(iter->f, root); |
---|
[199] | 384 | regfi_iterator_first_subkey(iter); |
---|
| 385 | sub = regfi_iterator_cur_subkey(iter); |
---|
[138] | 386 | printMsgs(iter->f); |
---|
[137] | 387 | |
---|
[81] | 388 | if(root == NULL) |
---|
[143] | 389 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: root cannot be NULL.\n"); |
---|
[81] | 390 | |
---|
| 391 | do |
---|
[31] | 392 | { |
---|
[81] | 393 | if(print_this) |
---|
[54] | 394 | { |
---|
[81] | 395 | path = iter2Path(iter); |
---|
| 396 | if(path == NULL) |
---|
[143] | 397 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Could not construct iterator's path.\n"); |
---|
[137] | 398 | |
---|
[81] | 399 | if(!type_filter_enabled || (key_type == type_filter)) |
---|
[109] | 400 | printKey(iter, path); |
---|
[81] | 401 | if(!type_filter_enabled || (key_type != type_filter)) |
---|
| 402 | printValueList(iter, path); |
---|
| 403 | |
---|
| 404 | free(path); |
---|
[54] | 405 | } |
---|
[66] | 406 | |
---|
[81] | 407 | if(sub == NULL) |
---|
[31] | 408 | { |
---|
[249] | 409 | if(!keysEqual(cur, root)) |
---|
[31] | 410 | { |
---|
[228] | 411 | regfi_free_record(iter->f, cur); |
---|
[249] | 412 | cur = NULL; |
---|
[81] | 413 | /* We're done with this sub-tree, going up and hitting other branches. */ |
---|
| 414 | if(!regfi_iterator_up(iter)) |
---|
[137] | 415 | { |
---|
[138] | 416 | printMsgs(iter->f); |
---|
[143] | 417 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: could not traverse iterator upward.\n"); |
---|
[137] | 418 | } |
---|
| 419 | |
---|
[81] | 420 | cur = regfi_iterator_cur_key(iter); |
---|
| 421 | if(cur == NULL) |
---|
[137] | 422 | { |
---|
[138] | 423 | printMsgs(iter->f); |
---|
[143] | 424 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: unexpected NULL for key.\n"); |
---|
[137] | 425 | } |
---|
[150] | 426 | |
---|
[199] | 427 | regfi_iterator_next_subkey(iter); |
---|
| 428 | sub = regfi_iterator_cur_subkey(iter); |
---|
[66] | 429 | } |
---|
[81] | 430 | print_this = false; |
---|
[31] | 431 | } |
---|
[81] | 432 | else |
---|
| 433 | { /* We have unexplored sub-keys. |
---|
| 434 | * Let's move down and print this first sub-tree out. |
---|
| 435 | */ |
---|
[228] | 436 | regfi_free_record(iter->f, cur); |
---|
[249] | 437 | cur = NULL; |
---|
[81] | 438 | if(!regfi_iterator_down(iter)) |
---|
[137] | 439 | { |
---|
[138] | 440 | printMsgs(iter->f); |
---|
[143] | 441 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: could not traverse iterator downward.\n"); |
---|
[137] | 442 | } |
---|
[81] | 443 | |
---|
[150] | 444 | cur = regfi_iterator_cur_key(iter); |
---|
[228] | 445 | regfi_free_record(iter->f, sub); |
---|
[199] | 446 | regfi_iterator_first_subkey(iter); |
---|
| 447 | sub = regfi_iterator_cur_subkey(iter); |
---|
[81] | 448 | print_this = true; |
---|
| 449 | } |
---|
[138] | 450 | printMsgs(iter->f); |
---|
[249] | 451 | } while(!(keysEqual(cur, root) && (sub == NULL))); |
---|
| 452 | if(cur != NULL) |
---|
| 453 | regfi_free_record(iter->f, cur); |
---|
[228] | 454 | regfi_free_record(iter->f, root); |
---|
[81] | 455 | |
---|
[54] | 456 | if(print_verbose) |
---|
[138] | 457 | fprintf(stderr, "INFO: Finished printing key tree.\n"); |
---|
[30] | 458 | } |
---|
| 459 | |
---|
[81] | 460 | |
---|
[140] | 461 | /* XXX: What if there is BOTH a value AND a key with that name?? |
---|
| 462 | * What if there are multiple keys/values with the same name?? |
---|
| 463 | */ |
---|
[33] | 464 | /* |
---|
[80] | 465 | * Returns 0 if path was not found. |
---|
| 466 | * Returns 1 if path was found as value. |
---|
| 467 | * Returns 2 if path was found as key. |
---|
[33] | 468 | * Returns less than 0 on other error. |
---|
| 469 | */ |
---|
[80] | 470 | int retrievePath(REGFI_ITERATOR* iter, char** path) |
---|
[33] | 471 | { |
---|
[203] | 472 | const REGFI_VK* value; |
---|
[81] | 473 | char* tmp_path_joined; |
---|
| 474 | const char** tmp_path; |
---|
[168] | 475 | uint32_t i; |
---|
[80] | 476 | |
---|
| 477 | if(path == NULL) |
---|
[33] | 478 | return -1; |
---|
| 479 | |
---|
[80] | 480 | /* One extra for any value at the end, and one more for NULL */ |
---|
[135] | 481 | tmp_path = (const char**)malloc(sizeof(const char**)*(REGFI_MAX_DEPTH+1+1)); |
---|
[80] | 482 | if(tmp_path == NULL) |
---|
[33] | 483 | return -2; |
---|
| 484 | |
---|
[80] | 485 | /* Strip any potential value name at end of path */ |
---|
| 486 | for(i=0; |
---|
[136] | 487 | (path[i] != NULL) && (path[i+1] != NULL) && (i < REGFI_MAX_DEPTH+1); |
---|
[80] | 488 | i++) |
---|
[136] | 489 | { tmp_path[i] = path[i]; } |
---|
[80] | 490 | tmp_path[i] = NULL; |
---|
| 491 | |
---|
[54] | 492 | if(print_verbose) |
---|
[138] | 493 | fprintf(stderr, "INFO: Attempting to retrieve specified path: %s\n", |
---|
[54] | 494 | path_filter); |
---|
| 495 | |
---|
[82] | 496 | /* Special check for '/' path filter */ |
---|
| 497 | if(path[0] == NULL) |
---|
| 498 | { |
---|
| 499 | if(print_verbose) |
---|
[138] | 500 | fprintf(stderr, "INFO: Found final path element as root key.\n"); |
---|
[136] | 501 | free(tmp_path); |
---|
[82] | 502 | return 2; |
---|
| 503 | } |
---|
| 504 | |
---|
[252] | 505 | if(!regfi_iterator_descend(iter, tmp_path)) |
---|
[33] | 506 | { |
---|
[138] | 507 | printMsgs(iter->f); |
---|
[80] | 508 | free(tmp_path); |
---|
| 509 | return 0; |
---|
[33] | 510 | } |
---|
| 511 | |
---|
[80] | 512 | if(regfi_iterator_find_value(iter, path[i])) |
---|
| 513 | { |
---|
| 514 | if(print_verbose) |
---|
[138] | 515 | fprintf(stderr, "INFO: Found final path element as value.\n"); |
---|
[33] | 516 | |
---|
[80] | 517 | value = regfi_iterator_cur_value(iter); |
---|
[138] | 518 | printMsgs(iter->f); |
---|
[81] | 519 | tmp_path_joined = iter2Path(iter); |
---|
[54] | 520 | |
---|
[80] | 521 | if((value == NULL) || (tmp_path_joined == NULL)) |
---|
[143] | 522 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Unexpected error before printValue.\n"); |
---|
[54] | 523 | |
---|
[121] | 524 | if(!type_filter_enabled || (value->type == type_filter)) |
---|
[159] | 525 | printValue(iter, value, tmp_path_joined); |
---|
[54] | 526 | |
---|
[228] | 527 | regfi_free_record(iter->f, value); |
---|
[80] | 528 | free(tmp_path); |
---|
| 529 | free(tmp_path_joined); |
---|
| 530 | return 1; |
---|
[33] | 531 | } |
---|
[80] | 532 | else if(regfi_iterator_find_subkey(iter, path[i])) |
---|
[33] | 533 | { |
---|
[138] | 534 | printMsgs(iter->f); |
---|
[80] | 535 | if(print_verbose) |
---|
[138] | 536 | fprintf(stderr, "INFO: Found final path element as key.\n"); |
---|
[82] | 537 | |
---|
| 538 | if(!regfi_iterator_down(iter)) |
---|
[137] | 539 | { |
---|
[138] | 540 | printMsgs(iter->f); |
---|
[143] | 541 | bailOut(REGLOOKUP_EXIT_DATAERR, "ERROR: Unexpected error on traversing path filter key.\n"); |
---|
[137] | 542 | } |
---|
[82] | 543 | |
---|
[80] | 544 | return 2; |
---|
[33] | 545 | } |
---|
[138] | 546 | printMsgs(iter->f); |
---|
[33] | 547 | |
---|
[54] | 548 | if(print_verbose) |
---|
[138] | 549 | fprintf(stderr, "INFO: Could not find last element of path.\n"); |
---|
[54] | 550 | |
---|
[80] | 551 | return 0; |
---|
[33] | 552 | } |
---|
| 553 | |
---|
| 554 | |
---|
[37] | 555 | static void usage(void) |
---|
| 556 | { |
---|
[61] | 557 | fprintf(stderr, "Usage: reglookup [-v] [-s]" |
---|
[40] | 558 | " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]" |
---|
[39] | 559 | " <REGISTRY_FILE>\n"); |
---|
[233] | 560 | fprintf(stderr, "Version: %s\n", regfi_version()); |
---|
[39] | 561 | fprintf(stderr, "Options:\n"); |
---|
| 562 | fprintf(stderr, "\t-v\t sets verbose mode.\n"); |
---|
[47] | 563 | fprintf(stderr, "\t-h\t enables header row. (default)\n"); |
---|
| 564 | fprintf(stderr, "\t-H\t disables header row.\n"); |
---|
[44] | 565 | fprintf(stderr, "\t-s\t enables security descriptor output.\n"); |
---|
| 566 | fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n"); |
---|
[40] | 567 | fprintf(stderr, "\t-p\t restrict output to elements below this path.\n"); |
---|
| 568 | fprintf(stderr, "\t-t\t restrict results to this specific data type.\n"); |
---|
[170] | 569 | fprintf(stderr, "\t-i\t includes parent key modification times with child values.\n"); |
---|
[37] | 570 | fprintf(stderr, "\n"); |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | |
---|
[30] | 574 | int main(int argc, char** argv) |
---|
| 575 | { |
---|
[80] | 576 | char** path = NULL; |
---|
| 577 | REGFI_ITERATOR* iter; |
---|
[178] | 578 | int retr_path_ret, fd; |
---|
[168] | 579 | uint32_t argi, arge; |
---|
[31] | 580 | |
---|
[37] | 581 | /* Process command line arguments */ |
---|
[30] | 582 | if(argc < 2) |
---|
| 583 | { |
---|
[37] | 584 | usage(); |
---|
[143] | 585 | bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: Requires at least one argument.\n"); |
---|
[30] | 586 | } |
---|
[37] | 587 | |
---|
[44] | 588 | arge = argc-1; |
---|
| 589 | for(argi = 1; argi < arge; argi++) |
---|
[37] | 590 | { |
---|
[40] | 591 | if (strcmp("-p", argv[argi]) == 0) |
---|
[37] | 592 | { |
---|
[44] | 593 | if(++argi >= arge) |
---|
[37] | 594 | { |
---|
| 595 | usage(); |
---|
[143] | 596 | bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: '-p' option requires parameter.\n"); |
---|
[37] | 597 | } |
---|
[40] | 598 | if((path_filter = strdup(argv[argi])) == NULL) |
---|
[143] | 599 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Memory allocation problem.\n"); |
---|
[38] | 600 | |
---|
[40] | 601 | path_filter_enabled = true; |
---|
[37] | 602 | } |
---|
| 603 | else if (strcmp("-t", argv[argi]) == 0) |
---|
| 604 | { |
---|
[44] | 605 | if(++argi >= arge) |
---|
[37] | 606 | { |
---|
| 607 | usage(); |
---|
[143] | 608 | bailOut(REGLOOKUP_EXIT_USAGE, "ERROR: '-t' option requires parameter.\n"); |
---|
[37] | 609 | } |
---|
[78] | 610 | if((type_filter = regfi_type_str2val(argv[argi])) < 0) |
---|
[40] | 611 | { |
---|
| 612 | fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]); |
---|
[143] | 613 | bailOut(REGLOOKUP_EXIT_USAGE, ""); |
---|
[40] | 614 | } |
---|
[37] | 615 | type_filter_enabled = true; |
---|
| 616 | } |
---|
[47] | 617 | else if (strcmp("-h", argv[argi]) == 0) |
---|
| 618 | print_header = true; |
---|
| 619 | else if (strcmp("-H", argv[argi]) == 0) |
---|
| 620 | print_header = false; |
---|
[37] | 621 | else if (strcmp("-s", argv[argi]) == 0) |
---|
| 622 | print_security = true; |
---|
[44] | 623 | else if (strcmp("-S", argv[argi]) == 0) |
---|
| 624 | print_security = false; |
---|
[37] | 625 | else if (strcmp("-v", argv[argi]) == 0) |
---|
| 626 | print_verbose = true; |
---|
[170] | 627 | else if (strcmp("-i", argv[argi]) == 0) |
---|
| 628 | print_value_mtime = true; |
---|
[44] | 629 | else |
---|
[37] | 630 | { |
---|
[38] | 631 | usage(); |
---|
[37] | 632 | fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]); |
---|
[143] | 633 | bailOut(REGLOOKUP_EXIT_USAGE, ""); |
---|
[37] | 634 | } |
---|
| 635 | } |
---|
[181] | 636 | registry_file = argv[argi]; |
---|
[30] | 637 | |
---|
[182] | 638 | if(print_verbose) |
---|
[185] | 639 | regfi_log_set_mask(REGFI_LOG_INFO|REGFI_LOG_WARN|REGFI_LOG_ERROR); |
---|
[182] | 640 | |
---|
[178] | 641 | fd = openHive(registry_file); |
---|
| 642 | if(fd < 0) |
---|
[37] | 643 | { |
---|
| 644 | fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file); |
---|
[143] | 645 | bailOut(REGLOOKUP_EXIT_NOINPUT, ""); |
---|
[37] | 646 | } |
---|
[182] | 647 | |
---|
[206] | 648 | /* XXX: add command line option to choose output encoding */ |
---|
| 649 | f = regfi_alloc(fd, REGFI_ENCODING_ASCII); |
---|
[178] | 650 | if(f == NULL) |
---|
| 651 | { |
---|
| 652 | close(fd); |
---|
| 653 | bailOut(REGLOOKUP_EXIT_NOINPUT, "ERROR: Failed to create REGFI_FILE structure.\n"); |
---|
| 654 | } |
---|
| 655 | |
---|
[206] | 656 | iter = regfi_iterator_new(f); |
---|
[80] | 657 | if(iter == NULL) |
---|
[158] | 658 | { |
---|
| 659 | printMsgs(f); |
---|
[143] | 660 | bailOut(REGLOOKUP_EXIT_OSERR, "ERROR: Couldn't create registry iterator.\n"); |
---|
[158] | 661 | } |
---|
[30] | 662 | |
---|
[81] | 663 | if(print_header) |
---|
| 664 | { |
---|
| 665 | if(print_security) |
---|
[125] | 666 | printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL,CLASS\n"); |
---|
[81] | 667 | else |
---|
| 668 | printf("PATH,TYPE,VALUE,MTIME\n"); |
---|
| 669 | } |
---|
| 670 | |
---|
[80] | 671 | if(path_filter_enabled && path_filter != NULL) |
---|
| 672 | path = splitPath(path_filter); |
---|
[81] | 673 | |
---|
[80] | 674 | if(path != NULL) |
---|
[33] | 675 | { |
---|
[80] | 676 | retr_path_ret = retrievePath(iter, path); |
---|
[138] | 677 | printMsgs(iter->f); |
---|
[83] | 678 | freePath(path); |
---|
| 679 | |
---|
[80] | 680 | if(retr_path_ret == 0) |
---|
[141] | 681 | fprintf(stderr, "WARN: Specified path '%s' not found.\n", path_filter); |
---|
[80] | 682 | else if (retr_path_ret == 2) |
---|
[81] | 683 | printKeyTree(iter); |
---|
[93] | 684 | else if(retr_path_ret < 0) |
---|
| 685 | { |
---|
| 686 | fprintf(stderr, "ERROR: retrievePath() returned %d.\n", |
---|
| 687 | retr_path_ret); |
---|
[143] | 688 | bailOut(REGLOOKUP_EXIT_DATAERR, |
---|
| 689 | "ERROR: Unknown error occurred in retrieving path.\n"); |
---|
[93] | 690 | } |
---|
[33] | 691 | } |
---|
[37] | 692 | else |
---|
[81] | 693 | printKeyTree(iter); |
---|
[31] | 694 | |
---|
[80] | 695 | regfi_iterator_free(iter); |
---|
[178] | 696 | regfi_free(f); |
---|
| 697 | close(fd); |
---|
[30] | 698 | |
---|
| 699 | return 0; |
---|
| 700 | } |
---|