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