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