[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 136 2009-01-23 17:29:51Z tim $ |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | #include <stdlib.h> |
---|
[88] | 25 | #include <sysexits.h> |
---|
[30] | 26 | #include <stdio.h> |
---|
| 27 | #include <string.h> |
---|
[33] | 28 | #include <strings.h> |
---|
[42] | 29 | #include <time.h> |
---|
[79] | 30 | #include "../include/regfi.h" |
---|
[31] | 31 | #include "../include/void_stack.h" |
---|
[30] | 32 | |
---|
[40] | 33 | /* Globals, influenced by command line parameters */ |
---|
| 34 | bool print_verbose = false; |
---|
| 35 | bool print_security = false; |
---|
[42] | 36 | bool print_header = true; |
---|
[40] | 37 | bool path_filter_enabled = false; |
---|
| 38 | bool type_filter_enabled = false; |
---|
| 39 | char* path_filter = NULL; |
---|
| 40 | int type_filter; |
---|
| 41 | char* registry_file = NULL; |
---|
| 42 | |
---|
[42] | 43 | /* Other globals */ |
---|
[135] | 44 | REGFI_FILE* f; |
---|
[66] | 45 | |
---|
[40] | 46 | |
---|
[116] | 47 | /* XXX: A hack to share some functions with reglookup-recover.c. |
---|
[125] | 48 | * Should move these into a proper library at some point. |
---|
[111] | 49 | */ |
---|
| 50 | #include "common.c" |
---|
[61] | 51 | |
---|
[38] | 52 | |
---|
[135] | 53 | void printValue(const REGFI_VK_REC* vk, char* prefix) |
---|
[41] | 54 | { |
---|
[111] | 55 | char* quoted_value = NULL; |
---|
| 56 | char* quoted_name = NULL; |
---|
| 57 | char* conv_error = NULL; |
---|
| 58 | const char* str_type = NULL; |
---|
| 59 | uint32 size = vk->data_size; |
---|
[41] | 60 | |
---|
[111] | 61 | /* Microsoft's documentation indicates that "available memory" is |
---|
| 62 | * the limit on value sizes. Annoying. We limit it to 1M which |
---|
| 63 | * should rarely be exceeded, unless the file is corrupt or |
---|
| 64 | * malicious. For more info, see: |
---|
| 65 | * http://msdn2.microsoft.com/en-us/library/ms724872.aspx |
---|
| 66 | */ |
---|
[135] | 67 | if(size > REGFI_VK_MAX_DATA_LENGTH) |
---|
[41] | 68 | { |
---|
[111] | 69 | fprintf(stderr, "WARNING: value data size %d larger than " |
---|
[135] | 70 | "%d, truncating...\n", size, REGFI_VK_MAX_DATA_LENGTH); |
---|
| 71 | size = REGFI_VK_MAX_DATA_LENGTH; |
---|
[41] | 72 | } |
---|
[111] | 73 | |
---|
| 74 | quoted_name = quote_string(vk->valuename, key_special_chars); |
---|
| 75 | if (quoted_name == NULL) |
---|
| 76 | { /* Value names are NULL when we're looking at the "(default)" value. |
---|
| 77 | * Currently we just return a 0-length string to try an eliminate |
---|
| 78 | * ambiguity with a literal "(default)" value. The data type of a line |
---|
| 79 | * in the output allows one to differentiate between the parent key and |
---|
| 80 | * this value. |
---|
| 81 | */ |
---|
| 82 | quoted_name = malloc(1*sizeof(char)); |
---|
| 83 | if(quoted_name == NULL) |
---|
| 84 | bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n"); |
---|
| 85 | quoted_name[0] = '\0'; |
---|
| 86 | } |
---|
[41] | 87 | |
---|
[111] | 88 | quoted_value = data_to_ascii(vk->data, size, vk->type, &conv_error); |
---|
| 89 | if(quoted_value == NULL) |
---|
[41] | 90 | { |
---|
[111] | 91 | if(conv_error == NULL) |
---|
| 92 | fprintf(stderr, "WARNING: Could not quote value for '%s/%s'. " |
---|
| 93 | "Memory allocation failure likely.\n", prefix, quoted_name); |
---|
| 94 | else if(print_verbose) |
---|
| 95 | fprintf(stderr, "WARNING: Could not quote value for '%s/%s'. " |
---|
| 96 | "Returned error: %s\n", prefix, quoted_name, conv_error); |
---|
[41] | 97 | } |
---|
[111] | 98 | else if(conv_error != NULL && print_verbose) |
---|
| 99 | fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', " |
---|
| 100 | "warning returned: %s\n", prefix, quoted_name, conv_error); |
---|
[41] | 101 | |
---|
[111] | 102 | str_type = regfi_type_val2str(vk->type); |
---|
| 103 | if(print_security) |
---|
[41] | 104 | { |
---|
[111] | 105 | if(str_type == NULL) |
---|
| 106 | printf("%s/%s,0x%.8X,%s,,,,,\n", prefix, quoted_name, |
---|
| 107 | vk->type, quoted_value); |
---|
[66] | 108 | else |
---|
[111] | 109 | printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name, |
---|
| 110 | str_type, quoted_value); |
---|
[71] | 111 | } |
---|
[111] | 112 | else |
---|
| 113 | { |
---|
| 114 | if(str_type == NULL) |
---|
| 115 | printf("%s/%s,0x%.8X,%s,\n", prefix, quoted_name, |
---|
| 116 | vk->type, quoted_value); |
---|
| 117 | else |
---|
| 118 | printf("%s/%s,%s,%s,\n", prefix, quoted_name, |
---|
| 119 | str_type, quoted_value); |
---|
| 120 | } |
---|
[42] | 121 | |
---|
[111] | 122 | if(quoted_value != NULL) |
---|
| 123 | free(quoted_value); |
---|
| 124 | if(quoted_name != NULL) |
---|
| 125 | free(quoted_name); |
---|
| 126 | if(conv_error != NULL) |
---|
| 127 | free(conv_error); |
---|
[41] | 128 | } |
---|
| 129 | |
---|
| 130 | |
---|
[111] | 131 | |
---|
[83] | 132 | /* XXX: Each chunk must be unquoted after it is split out. |
---|
| 133 | * Quoting syntax may need to be standardized and pushed into the API |
---|
| 134 | * to deal with this issue and others. |
---|
| 135 | */ |
---|
[81] | 136 | char** splitPath(const char* s) |
---|
[30] | 137 | { |
---|
[81] | 138 | char** ret_val; |
---|
[38] | 139 | const char* cur = s; |
---|
[33] | 140 | char* next = NULL; |
---|
[38] | 141 | char* copy; |
---|
[81] | 142 | uint32 ret_cur = 0; |
---|
[38] | 143 | |
---|
[135] | 144 | ret_val = (char**)malloc((REGFI_MAX_DEPTH+1+1)*sizeof(char**)); |
---|
[81] | 145 | if (ret_val == NULL) |
---|
[38] | 146 | return NULL; |
---|
[81] | 147 | ret_val[0] = NULL; |
---|
| 148 | |
---|
| 149 | /* We return a well-formed, 0-length, path even when input is icky. */ |
---|
[37] | 150 | if (s == NULL) |
---|
[81] | 151 | return ret_val; |
---|
[38] | 152 | |
---|
| 153 | while((next = strchr(cur, '/')) != NULL) |
---|
[33] | 154 | { |
---|
[38] | 155 | if ((next-cur) > 0) |
---|
| 156 | { |
---|
| 157 | copy = (char*)malloc((next-cur+1)*sizeof(char)); |
---|
| 158 | if(copy == NULL) |
---|
[88] | 159 | bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n"); |
---|
[38] | 160 | |
---|
| 161 | memcpy(copy, cur, next-cur); |
---|
| 162 | copy[next-cur] = '\0'; |
---|
[81] | 163 | ret_val[ret_cur++] = copy; |
---|
[135] | 164 | if(ret_cur < (REGFI_MAX_DEPTH+1+1)) |
---|
[81] | 165 | ret_val[ret_cur] = NULL; |
---|
| 166 | else |
---|
[88] | 167 | bailOut(EX_DATAERR, "ERROR: Registry maximum depth exceeded.\n"); |
---|
[38] | 168 | } |
---|
| 169 | cur = next+1; |
---|
[33] | 170 | } |
---|
[81] | 171 | |
---|
| 172 | /* Grab last element, if path doesn't end in '/'. */ |
---|
[33] | 173 | if(strlen(cur) > 0) |
---|
[38] | 174 | { |
---|
| 175 | copy = strdup(cur); |
---|
[81] | 176 | ret_val[ret_cur++] = copy; |
---|
[135] | 177 | if(ret_cur < (REGFI_MAX_DEPTH+1+1)) |
---|
[81] | 178 | ret_val[ret_cur] = NULL; |
---|
| 179 | else |
---|
[88] | 180 | bailOut(EX_DATAERR, "ERROR: Registry maximum depth exceeded.\n"); |
---|
[38] | 181 | } |
---|
[33] | 182 | |
---|
| 183 | return ret_val; |
---|
| 184 | } |
---|
| 185 | |
---|
[81] | 186 | |
---|
[83] | 187 | void freePath(char** path) |
---|
| 188 | { |
---|
| 189 | uint32 i; |
---|
| 190 | |
---|
| 191 | if(path == NULL) |
---|
| 192 | return; |
---|
| 193 | |
---|
| 194 | for(i=0; path[i] != NULL; i++) |
---|
| 195 | free(path[i]); |
---|
| 196 | |
---|
| 197 | free(path); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | |
---|
[81] | 201 | /* Returns a quoted path from an iterator's stack */ |
---|
| 202 | /* XXX: Some way should be found to integrate this into regfi's API |
---|
| 203 | * The problem is that the escaping is sorta reglookup-specific. |
---|
| 204 | */ |
---|
| 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 | |
---|
[81] | 275 | void printValueList(REGFI_ITERATOR* i, char* prefix) |
---|
[32] | 276 | { |
---|
[135] | 277 | const REGFI_VK_REC* value; |
---|
[80] | 278 | |
---|
| 279 | value = regfi_iterator_first_value(i); |
---|
| 280 | while(value != NULL) |
---|
[81] | 281 | { |
---|
| 282 | if(!type_filter_enabled || (value->type == type_filter)) |
---|
[80] | 283 | printValue(value, prefix); |
---|
[81] | 284 | value = regfi_iterator_next_value(i); |
---|
| 285 | } |
---|
[33] | 286 | } |
---|
| 287 | |
---|
[37] | 288 | |
---|
[109] | 289 | void printKey(REGFI_ITERATOR* i, char* full_path) |
---|
[33] | 290 | { |
---|
[43] | 291 | static char empty_str[1] = ""; |
---|
[42] | 292 | char* owner = NULL; |
---|
| 293 | char* group = NULL; |
---|
| 294 | char* sacl = NULL; |
---|
| 295 | char* dacl = NULL; |
---|
[125] | 296 | char* quoted_classname; |
---|
[126] | 297 | char* error_msg = NULL; |
---|
[42] | 298 | char mtime[20]; |
---|
| 299 | time_t tmp_time[1]; |
---|
| 300 | struct tm* tmp_time_s = NULL; |
---|
[135] | 301 | const REGFI_SK_REC* sk; |
---|
| 302 | const REGFI_NK_REC* k = regfi_iterator_cur_key(i); |
---|
[42] | 303 | |
---|
[43] | 304 | *tmp_time = nt_time_to_unix(&k->mtime); |
---|
| 305 | tmp_time_s = gmtime(tmp_time); |
---|
| 306 | strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s); |
---|
| 307 | |
---|
[109] | 308 | if(print_security && (sk=regfi_iterator_cur_sk(i))) |
---|
[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); |
---|
[43] | 314 | if(owner == NULL) |
---|
| 315 | owner = empty_str; |
---|
| 316 | if(group == NULL) |
---|
| 317 | group = empty_str; |
---|
| 318 | if(sacl == NULL) |
---|
| 319 | sacl = empty_str; |
---|
| 320 | if(dacl == NULL) |
---|
| 321 | dacl = empty_str; |
---|
| 322 | |
---|
[125] | 323 | if(k->classname != NULL) |
---|
[126] | 324 | { |
---|
| 325 | quoted_classname = quote_unicode((uint8*)k->classname, k->classname_length, |
---|
| 326 | key_special_chars, &error_msg); |
---|
| 327 | if(quoted_classname == NULL) |
---|
| 328 | { |
---|
| 329 | if(error_msg == NULL) |
---|
| 330 | fprintf(stderr, "ERROR: Could not quote classname" |
---|
| 331 | " for key '%s' due to unknown error.\n", full_path); |
---|
| 332 | else |
---|
| 333 | { |
---|
| 334 | fprintf(stderr, "ERROR: Could not quote classname" |
---|
| 335 | " for key '%s' due to error: %s\n", full_path, error_msg); |
---|
| 336 | free(error_msg); |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | else if (error_msg != NULL) |
---|
| 340 | { |
---|
| 341 | if(print_verbose) |
---|
| 342 | fprintf(stderr, "WARNING: While converting classname" |
---|
| 343 | " for key '%s': %s.\n", full_path, error_msg); |
---|
| 344 | free(error_msg); |
---|
| 345 | } |
---|
| 346 | } |
---|
[125] | 347 | else |
---|
| 348 | quoted_classname = empty_str; |
---|
[43] | 349 | |
---|
[125] | 350 | printf("%s,KEY,,%s,%s,%s,%s,%s,%s\n", full_path, mtime, |
---|
| 351 | owner, group, sacl, dacl, quoted_classname); |
---|
| 352 | |
---|
[43] | 353 | if(owner != empty_str) |
---|
| 354 | free(owner); |
---|
| 355 | if(group != empty_str) |
---|
| 356 | free(group); |
---|
| 357 | if(sacl != empty_str) |
---|
| 358 | free(sacl); |
---|
| 359 | if(dacl != empty_str) |
---|
| 360 | free(dacl); |
---|
[125] | 361 | if(quoted_classname != empty_str) |
---|
| 362 | free(quoted_classname); |
---|
[43] | 363 | } |
---|
| 364 | else |
---|
[66] | 365 | printf("%s,KEY,,%s\n", full_path, mtime); |
---|
[43] | 366 | } |
---|
| 367 | |
---|
| 368 | |
---|
[81] | 369 | void printKeyTree(REGFI_ITERATOR* iter) |
---|
[43] | 370 | { |
---|
[135] | 371 | const REGFI_NK_REC* root = NULL; |
---|
| 372 | const REGFI_NK_REC* cur = NULL; |
---|
| 373 | const REGFI_NK_REC* sub = NULL; |
---|
[43] | 374 | char* path = NULL; |
---|
[136] | 375 | char* msgs = NULL; |
---|
[78] | 376 | int key_type = regfi_type_str2val("KEY"); |
---|
[81] | 377 | bool print_this = true; |
---|
| 378 | |
---|
| 379 | root = cur = regfi_iterator_cur_key(iter); |
---|
| 380 | sub = regfi_iterator_first_subkey(iter); |
---|
[136] | 381 | msgs = regfi_get_messages(iter->f); |
---|
| 382 | if(msgs != NULL) |
---|
| 383 | fprintf(stderr, "%s", msgs); |
---|
[81] | 384 | if(root == NULL) |
---|
[88] | 385 | bailOut(EX_DATAERR, "ERROR: root cannot be NULL.\n"); |
---|
[81] | 386 | |
---|
| 387 | do |
---|
[31] | 388 | { |
---|
[81] | 389 | if(print_this) |
---|
[54] | 390 | { |
---|
[81] | 391 | path = iter2Path(iter); |
---|
| 392 | if(path == NULL) |
---|
[88] | 393 | bailOut(EX_OSERR, "ERROR: Could not construct iterator's path.\n"); |
---|
[81] | 394 | |
---|
| 395 | if(!type_filter_enabled || (key_type == type_filter)) |
---|
[109] | 396 | printKey(iter, path); |
---|
[81] | 397 | if(!type_filter_enabled || (key_type != type_filter)) |
---|
| 398 | printValueList(iter, path); |
---|
| 399 | |
---|
| 400 | free(path); |
---|
[54] | 401 | } |
---|
[66] | 402 | |
---|
[81] | 403 | if(sub == NULL) |
---|
[31] | 404 | { |
---|
[81] | 405 | if(cur != root) |
---|
[31] | 406 | { |
---|
[81] | 407 | /* We're done with this sub-tree, going up and hitting other branches. */ |
---|
| 408 | if(!regfi_iterator_up(iter)) |
---|
[88] | 409 | bailOut(EX_DATAERR, "ERROR: could not traverse iterator upward.\n"); |
---|
[81] | 410 | |
---|
| 411 | cur = regfi_iterator_cur_key(iter); |
---|
| 412 | if(cur == NULL) |
---|
[88] | 413 | bailOut(EX_DATAERR, "ERROR: unexpected NULL for key.\n"); |
---|
[81] | 414 | |
---|
| 415 | sub = regfi_iterator_next_subkey(iter); |
---|
[66] | 416 | } |
---|
[81] | 417 | print_this = false; |
---|
[31] | 418 | } |
---|
[81] | 419 | else |
---|
| 420 | { /* We have unexplored sub-keys. |
---|
| 421 | * Let's move down and print this first sub-tree out. |
---|
| 422 | */ |
---|
| 423 | if(!regfi_iterator_down(iter)) |
---|
[88] | 424 | bailOut(EX_DATAERR, "ERROR: could not traverse iterator downward.\n"); |
---|
[81] | 425 | |
---|
| 426 | cur = sub; |
---|
| 427 | sub = regfi_iterator_first_subkey(iter); |
---|
| 428 | print_this = true; |
---|
| 429 | } |
---|
| 430 | } while(!((cur == root) && (sub == NULL))); |
---|
| 431 | |
---|
[54] | 432 | if(print_verbose) |
---|
| 433 | fprintf(stderr, "VERBOSE: Finished printing key tree.\n"); |
---|
[30] | 434 | } |
---|
| 435 | |
---|
[81] | 436 | |
---|
[97] | 437 | /* XXX: what if there is BOTH a value AND a key with that name?? */ |
---|
[33] | 438 | /* |
---|
[80] | 439 | * Returns 0 if path was not found. |
---|
| 440 | * Returns 1 if path was found as value. |
---|
| 441 | * Returns 2 if path was found as key. |
---|
[33] | 442 | * Returns less than 0 on other error. |
---|
| 443 | */ |
---|
[80] | 444 | int retrievePath(REGFI_ITERATOR* iter, char** path) |
---|
[33] | 445 | { |
---|
[135] | 446 | const REGFI_VK_REC* value; |
---|
[81] | 447 | char* tmp_path_joined; |
---|
| 448 | const char** tmp_path; |
---|
[80] | 449 | uint32 i; |
---|
| 450 | |
---|
| 451 | if(path == NULL) |
---|
[33] | 452 | return -1; |
---|
| 453 | |
---|
[80] | 454 | /* One extra for any value at the end, and one more for NULL */ |
---|
[135] | 455 | tmp_path = (const char**)malloc(sizeof(const char**)*(REGFI_MAX_DEPTH+1+1)); |
---|
[80] | 456 | if(tmp_path == NULL) |
---|
[33] | 457 | return -2; |
---|
| 458 | |
---|
[80] | 459 | /* Strip any potential value name at end of path */ |
---|
| 460 | for(i=0; |
---|
[136] | 461 | (path[i] != NULL) && (path[i+1] != NULL) && (i < REGFI_MAX_DEPTH+1); |
---|
[80] | 462 | i++) |
---|
[136] | 463 | { tmp_path[i] = path[i]; } |
---|
[80] | 464 | tmp_path[i] = NULL; |
---|
| 465 | |
---|
[54] | 466 | if(print_verbose) |
---|
[80] | 467 | fprintf(stderr, "VERBOSE: Attempting to retrieve specified path: %s\n", |
---|
[54] | 468 | path_filter); |
---|
| 469 | |
---|
[82] | 470 | /* Special check for '/' path filter */ |
---|
| 471 | if(path[0] == NULL) |
---|
| 472 | { |
---|
| 473 | if(print_verbose) |
---|
| 474 | fprintf(stderr, "VERBOSE: Found final path element as root key.\n"); |
---|
[136] | 475 | free(tmp_path); |
---|
[82] | 476 | return 2; |
---|
| 477 | } |
---|
| 478 | |
---|
[80] | 479 | if(!regfi_iterator_walk_path(iter, tmp_path)) |
---|
[33] | 480 | { |
---|
[80] | 481 | free(tmp_path); |
---|
| 482 | return 0; |
---|
[33] | 483 | } |
---|
| 484 | |
---|
[80] | 485 | if(regfi_iterator_find_value(iter, path[i])) |
---|
| 486 | { |
---|
| 487 | if(print_verbose) |
---|
| 488 | fprintf(stderr, "VERBOSE: Found final path element as value.\n"); |
---|
[33] | 489 | |
---|
[80] | 490 | value = regfi_iterator_cur_value(iter); |
---|
[81] | 491 | tmp_path_joined = iter2Path(iter); |
---|
[54] | 492 | |
---|
[80] | 493 | if((value == NULL) || (tmp_path_joined == NULL)) |
---|
[88] | 494 | bailOut(EX_OSERR, "ERROR: Unexpected error before printValue.\n"); |
---|
[54] | 495 | |
---|
[121] | 496 | if(!type_filter_enabled || (value->type == type_filter)) |
---|
| 497 | printValue(value, tmp_path_joined); |
---|
[54] | 498 | |
---|
[80] | 499 | free(tmp_path); |
---|
| 500 | free(tmp_path_joined); |
---|
| 501 | return 1; |
---|
[33] | 502 | } |
---|
[80] | 503 | else if(regfi_iterator_find_subkey(iter, path[i])) |
---|
[33] | 504 | { |
---|
[80] | 505 | if(print_verbose) |
---|
| 506 | fprintf(stderr, "VERBOSE: Found final path element as key.\n"); |
---|
[82] | 507 | |
---|
| 508 | if(!regfi_iterator_down(iter)) |
---|
[88] | 509 | bailOut(EX_DATAERR, "ERROR: Unexpected error on traversing path filter key.\n"); |
---|
[82] | 510 | |
---|
[80] | 511 | return 2; |
---|
[33] | 512 | } |
---|
| 513 | |
---|
[54] | 514 | if(print_verbose) |
---|
| 515 | fprintf(stderr, "VERBOSE: Could not find last element of path.\n"); |
---|
| 516 | |
---|
[80] | 517 | return 0; |
---|
[33] | 518 | } |
---|
| 519 | |
---|
| 520 | |
---|
[37] | 521 | static void usage(void) |
---|
| 522 | { |
---|
[61] | 523 | fprintf(stderr, "Usage: reglookup [-v] [-s]" |
---|
[40] | 524 | " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]" |
---|
[39] | 525 | " <REGISTRY_FILE>\n"); |
---|
[111] | 526 | fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION); |
---|
[39] | 527 | fprintf(stderr, "Options:\n"); |
---|
| 528 | fprintf(stderr, "\t-v\t sets verbose mode.\n"); |
---|
[47] | 529 | fprintf(stderr, "\t-h\t enables header row. (default)\n"); |
---|
| 530 | fprintf(stderr, "\t-H\t disables header row.\n"); |
---|
[44] | 531 | fprintf(stderr, "\t-s\t enables security descriptor output.\n"); |
---|
| 532 | fprintf(stderr, "\t-S\t disables security descriptor output. (default)\n"); |
---|
[40] | 533 | fprintf(stderr, "\t-p\t restrict output to elements below this path.\n"); |
---|
| 534 | fprintf(stderr, "\t-t\t restrict results to this specific data type.\n"); |
---|
[37] | 535 | fprintf(stderr, "\n"); |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | |
---|
[30] | 539 | int main(int argc, char** argv) |
---|
| 540 | { |
---|
[80] | 541 | char** path = NULL; |
---|
| 542 | REGFI_ITERATOR* iter; |
---|
[33] | 543 | int retr_path_ret; |
---|
[44] | 544 | uint32 argi, arge; |
---|
[31] | 545 | |
---|
[37] | 546 | /* Process command line arguments */ |
---|
[30] | 547 | if(argc < 2) |
---|
| 548 | { |
---|
[37] | 549 | usage(); |
---|
[88] | 550 | bailOut(EX_USAGE, "ERROR: Requires at least one argument.\n"); |
---|
[30] | 551 | } |
---|
[37] | 552 | |
---|
[44] | 553 | arge = argc-1; |
---|
| 554 | for(argi = 1; argi < arge; argi++) |
---|
[37] | 555 | { |
---|
[40] | 556 | if (strcmp("-p", argv[argi]) == 0) |
---|
[37] | 557 | { |
---|
[44] | 558 | if(++argi >= arge) |
---|
[37] | 559 | { |
---|
| 560 | usage(); |
---|
[88] | 561 | bailOut(EX_USAGE, "ERROR: '-p' option requires parameter.\n"); |
---|
[37] | 562 | } |
---|
[40] | 563 | if((path_filter = strdup(argv[argi])) == NULL) |
---|
[88] | 564 | bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n"); |
---|
[38] | 565 | |
---|
[40] | 566 | path_filter_enabled = true; |
---|
[37] | 567 | } |
---|
| 568 | else if (strcmp("-t", argv[argi]) == 0) |
---|
| 569 | { |
---|
[44] | 570 | if(++argi >= arge) |
---|
[37] | 571 | { |
---|
| 572 | usage(); |
---|
[88] | 573 | bailOut(EX_USAGE, "ERROR: '-t' option requires parameter.\n"); |
---|
[37] | 574 | } |
---|
[78] | 575 | if((type_filter = regfi_type_str2val(argv[argi])) < 0) |
---|
[40] | 576 | { |
---|
| 577 | fprintf(stderr, "ERROR: Invalid type specified: %s.\n", argv[argi]); |
---|
[88] | 578 | bailOut(EX_USAGE, ""); |
---|
[40] | 579 | } |
---|
[37] | 580 | type_filter_enabled = true; |
---|
| 581 | } |
---|
[47] | 582 | else if (strcmp("-h", argv[argi]) == 0) |
---|
| 583 | print_header = true; |
---|
| 584 | else if (strcmp("-H", argv[argi]) == 0) |
---|
| 585 | print_header = false; |
---|
[37] | 586 | else if (strcmp("-s", argv[argi]) == 0) |
---|
| 587 | print_security = true; |
---|
[44] | 588 | else if (strcmp("-S", argv[argi]) == 0) |
---|
| 589 | print_security = false; |
---|
[37] | 590 | else if (strcmp("-v", argv[argi]) == 0) |
---|
| 591 | print_verbose = true; |
---|
[44] | 592 | else |
---|
[37] | 593 | { |
---|
[38] | 594 | usage(); |
---|
[37] | 595 | fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]); |
---|
[88] | 596 | bailOut(EX_USAGE, ""); |
---|
[37] | 597 | } |
---|
| 598 | } |
---|
[44] | 599 | if((registry_file = strdup(argv[argi])) == NULL) |
---|
[88] | 600 | bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n"); |
---|
[30] | 601 | |
---|
[110] | 602 | f = regfi_open(registry_file); |
---|
[37] | 603 | if(f == NULL) |
---|
| 604 | { |
---|
| 605 | fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file); |
---|
[88] | 606 | bailOut(EX_NOINPUT, ""); |
---|
[37] | 607 | } |
---|
[38] | 608 | |
---|
[80] | 609 | iter = regfi_iterator_new(f); |
---|
| 610 | if(iter == NULL) |
---|
[88] | 611 | bailOut(EX_OSERR, "ERROR: Couldn't create registry iterator.\n"); |
---|
[30] | 612 | |
---|
[81] | 613 | if(print_header) |
---|
| 614 | { |
---|
| 615 | if(print_security) |
---|
[125] | 616 | printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL,CLASS\n"); |
---|
[81] | 617 | else |
---|
| 618 | printf("PATH,TYPE,VALUE,MTIME\n"); |
---|
| 619 | } |
---|
| 620 | |
---|
[80] | 621 | if(path_filter_enabled && path_filter != NULL) |
---|
| 622 | path = splitPath(path_filter); |
---|
[81] | 623 | |
---|
[80] | 624 | if(path != NULL) |
---|
[33] | 625 | { |
---|
[80] | 626 | retr_path_ret = retrievePath(iter, path); |
---|
[83] | 627 | freePath(path); |
---|
| 628 | |
---|
[80] | 629 | if(retr_path_ret == 0) |
---|
| 630 | fprintf(stderr, "WARNING: specified path not found.\n"); |
---|
| 631 | else if (retr_path_ret == 2) |
---|
[81] | 632 | printKeyTree(iter); |
---|
[93] | 633 | else if(retr_path_ret < 0) |
---|
| 634 | { |
---|
| 635 | fprintf(stderr, "ERROR: retrievePath() returned %d.\n", |
---|
| 636 | retr_path_ret); |
---|
| 637 | bailOut(EX_DATAERR,"ERROR: Unknown error occurred in retrieving path.\n"); |
---|
| 638 | } |
---|
[33] | 639 | } |
---|
[37] | 640 | else |
---|
[81] | 641 | printKeyTree(iter); |
---|
[31] | 642 | |
---|
[80] | 643 | regfi_iterator_free(iter); |
---|
[78] | 644 | regfi_close(f); |
---|
[30] | 645 | |
---|
| 646 | return 0; |
---|
| 647 | } |
---|