- Timestamp:
- 01/17/07 11:47:39 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/reglookup.c
r80 r81 3 3 * Gerald Carter''s regfio interface. 4 4 * 5 * Copyright (C) 2005-200 6Timothy D. Morgan5 * Copyright (C) 2005-2007 Timothy D. Morgan 6 6 * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com 7 7 * … … 382 382 383 383 384 void_stack* path2Stack(const char* s) 385 { 386 void_stack* ret_val; 387 void_stack* rev_ret = void_stack_new(REGF_MAX_DEPTH); 384 char** splitPath(const char* s) 385 { 386 char** ret_val; 388 387 const char* cur = s; 389 388 char* next = NULL; 390 389 char* copy; 391 392 if (rev_ret == NULL) 390 uint32 ret_cur = 0; 391 392 ret_val = (char**)malloc((REGF_MAX_DEPTH+1+1)*sizeof(char**)); 393 if (ret_val == NULL) 393 394 return NULL; 395 ret_val[0] = NULL; 396 397 /* We return a well-formed, 0-length, path even when input is icky. */ 394 398 if (s == NULL) 395 return re v_ret;399 return ret_val; 396 400 397 401 while((next = strchr(cur, '/')) != NULL) … … 405 409 memcpy(copy, cur, next-cur); 406 410 copy[next-cur] = '\0'; 407 if(!void_stack_push(rev_ret, copy)) 411 ret_val[ret_cur++] = copy; 412 if(ret_cur < (REGF_MAX_DEPTH+1+1)) 413 ret_val[ret_cur] = NULL; 414 else 408 415 bailOut(2, "ERROR: Registry maximum depth exceeded.\n"); 409 416 } 410 417 cur = next+1; 411 418 } 419 420 /* Grab last element, if path doesn't end in '/'. */ 412 421 if(strlen(cur) > 0) 413 422 { 414 423 copy = strdup(cur); 415 if(!void_stack_push(rev_ret, copy)) 424 ret_val[ret_cur++] = copy; 425 if(ret_cur < (REGF_MAX_DEPTH+1+1)) 426 ret_val[ret_cur] = NULL; 427 else 416 428 bailOut(2, "ERROR: Registry maximum depth exceeded.\n"); 417 429 } 418 430 419 ret_val = void_stack_copy_reverse(rev_ret);420 void_stack_free(rev_ret);421 422 431 return ret_val; 423 432 } 424 433 425 /* Returns a quoted path from an nk_stack */ 426 char* stack2Path(void_stack* nk_stack) 427 { 428 const REGF_NK_REC* cur; 434 435 /* Returns a quoted path from an iterator's stack */ 436 /* XXX: Some way should be found to integrate this into regfi's API 437 * The problem is that the escaping is sorta reglookup-specific. 438 */ 439 char* iter2Path(REGFI_ITERATOR* i) 440 { 441 const REGFI_ITER_POSITION* cur; 429 442 uint32 buf_left = 127; 430 443 uint32 buf_len = buf_left+1; 431 444 uint32 name_len = 0; 432 445 uint32 grow_amt; 433 char* buf; 446 char* buf; 434 447 char* new_buf; 435 448 char* name; 449 const char* cur_name; 436 450 void_stack_iterator* iter; 437 451 … … 441 455 buf[0] = '\0'; 442 456 443 iter = void_stack_iterator_new( nk_stack);457 iter = void_stack_iterator_new(i->key_positions); 444 458 if (iter == NULL) 445 459 { … … 449 463 450 464 /* skip root element */ 465 if(void_stack_size(i->key_positions) < 1) 466 { 467 buf[0] = '/'; 468 buf[1] = '\0'; 469 return buf; 470 } 451 471 cur = void_stack_iterator_next(iter); 452 472 453 while((cur = void_stack_iterator_next(iter)) != NULL) 454 { 473 do 474 { 475 cur = void_stack_iterator_next(iter); 476 if (cur == NULL) 477 cur_name = i->cur_key->keyname; 478 else 479 cur_name = cur->nk->keyname; 480 455 481 buf[buf_len-buf_left-1] = '/'; 456 482 buf_left -= 1; 457 name = quote_string(cur ->keyname, key_special_chars);483 name = quote_string(cur_name, key_special_chars); 458 484 name_len = strlen(name); 459 485 if(name_len+1 > buf_left) … … 474 500 buf[buf_len-buf_left-1] = '\0'; 475 501 free(name); 476 } 502 } while(cur != NULL); 477 503 478 504 return buf; … … 572 598 573 599 574 void printValueList(REGFI_ITERATOR i, char* prefix)600 void printValueList(REGFI_ITERATOR* i, char* prefix) 575 601 { 576 602 REGF_VK_REC* value; … … 578 604 value = regfi_iterator_first_value(i); 579 605 while(value != NULL) 580 if(!type_filter_enabled || (value.type == type_filter)) 606 { 607 if(!type_filter_enabled || (value->type == type_filter)) 581 608 printValue(value, prefix); 609 value = regfi_iterator_next_value(i); 610 } 582 611 } 583 612 … … 630 659 631 660 632 void printKeyTree(REGF_FILE* f, void_stack* nk_stack, const char* prefix) 633 { 661 void printKeyTree(REGFI_ITERATOR* iter) 662 { 663 REGF_NK_REC* root = NULL; 634 664 REGF_NK_REC* cur = NULL; 635 665 REGF_NK_REC* sub = NULL; 636 666 char* path = NULL; 637 char* val_path = NULL;638 uint32 val_path_len = 0;639 uint32 path_len = 0;640 uint32 prefix_len = strlen(prefix);641 667 int key_type = regfi_type_str2val("KEY"); 668 bool print_this = true; 669 670 root = cur = regfi_iterator_cur_key(iter); 671 sub = regfi_iterator_first_subkey(iter); 642 672 643 if((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL) 644 { 645 cur->subkey_index = 0; 646 path = stack2Path(nk_stack); 647 648 if(print_verbose) 649 { 650 if(prefix[0] == '\0') 651 fprintf(stderr, "VERBOSE: Printing key tree under path: /\n"); 652 else 653 fprintf(stderr, "VERBOSE: Printing key tree under path: %s\n", 654 prefix); 655 } 656 657 path_len = strlen(path); 658 val_path_len = prefix_len+path_len; 659 val_path = (char*)malloc(val_path_len+1+1); 660 if(val_path == NULL) 661 bailOut(2, "ERROR: Could not allocate val_path.\n"); 662 663 strcpy(val_path, prefix); 664 strcpy(val_path+prefix_len, path); 665 if(val_path[0] == '\0') 666 { 667 val_path[0] = '/'; 668 val_path[1] = '\0'; 669 } 670 if(!type_filter_enabled || (key_type == type_filter)) 671 printKey(cur, val_path); 672 if(!type_filter_enabled || (key_type != type_filter)) 673 printValueList(cur, val_path); 673 if(root == NULL) 674 bailOut(3, "ERROR: root cannot be NULL.\n"); 675 676 do 677 { 678 if(print_this) 679 { 680 path = iter2Path(iter); 681 if(path == NULL) 682 bailOut(2, "ERROR: Could not construct iterator's path.\n"); 683 684 if(!type_filter_enabled || (key_type == type_filter)) 685 printKey(cur, path); 686 if(!type_filter_enabled || (key_type != type_filter)) 687 printValueList(iter, path); 688 689 free(path); 690 } 674 691 675 while((cur = (REGF_NK_REC*)void_stack_cur(nk_stack)) != NULL)676 { 677 if( (sub = regfi_fetch_subkey(f, cur)) == NULL)692 if(sub == NULL) 693 { 694 if(cur != root) 678 695 { 679 sub = void_stack_pop(nk_stack); 680 /* XXX: This is just a shallow free. Need to write deep free 681 * routines to replace the Samba code for this. 682 */ 683 if(sub != NULL) 684 free(sub); 696 /* We're done with this sub-tree, going up and hitting other branches. */ 697 if(!regfi_iterator_up(iter)) 698 bailOut(3, "ERROR: could not traverse iterator upward.\n"); 699 700 cur = regfi_iterator_cur_key(iter); 701 if(cur == NULL) 702 bailOut(3, "ERROR: unexpected NULL for key.\n"); 703 704 sub = regfi_iterator_next_subkey(iter); 685 705 } 686 else 687 { 688 sub->subkey_index = 0; 689 if(!void_stack_push(nk_stack, sub)) 690 bailOut(2, "ERROR: Registry maximum depth exceeded.\n"); 691 path = stack2Path(nk_stack); 692 if(path != NULL) 693 { 694 path_len = strlen(path); 695 if(val_path_len < prefix_len+path_len) 696 { 697 val_path_len = prefix_len+path_len; 698 val_path = (char*)realloc(val_path, val_path_len+1); 699 if(val_path == NULL) 700 bailOut(2, "ERROR: Could not reallocate val_path.\n"); 701 } 702 strcpy(val_path, prefix); 703 strcpy(val_path+prefix_len, path); 704 if(!type_filter_enabled || (key_type == type_filter)) 705 printKey(sub, val_path); 706 if(!type_filter_enabled || (key_type != type_filter)) 707 printValueList(sub, val_path); 708 } 709 } 710 } 711 } 712 if(val_path != NULL) 713 free(val_path); 706 print_this = false; 707 } 708 else 709 { /* We have unexplored sub-keys. 710 * Let's move down and print this first sub-tree out. 711 */ 712 if(!regfi_iterator_down(iter)) 713 bailOut(3, "ERROR: could not traverse iterator downward.\n"); 714 715 cur = sub; 716 sub = regfi_iterator_first_subkey(iter); 717 print_this = true; 718 } 719 } while(!((cur == root) && (sub == NULL))); 720 714 721 if(print_verbose) 715 722 fprintf(stderr, "VERBOSE: Finished printing key tree.\n"); 716 723 } 724 717 725 718 726 /* … … 724 732 int retrievePath(REGFI_ITERATOR* iter, char** path) 725 733 { 726 REG_VK_REC* value; 734 REGF_VK_REC* value; 735 char* tmp_path_joined; 736 const char** tmp_path; 727 737 uint32 i; 728 char* p;729 char* tmp_path_joined;730 char** tmp_path;731 738 732 739 if(path == NULL) … … 734 741 735 742 /* One extra for any value at the end, and one more for NULL */ 736 tmp_path = (c har**)malloc(sizeof(char**)*(REGF_MAX_DEPTH+1+1));743 tmp_path = (const char**)malloc(sizeof(const char**)*(REGF_MAX_DEPTH+1+1)); 737 744 if(tmp_path == NULL) 738 745 return -2; … … 763 770 764 771 value = regfi_iterator_cur_value(iter); 765 tmp_path_joined = joinPath(tmp_path);772 tmp_path_joined = iter2Path(iter); 766 773 767 774 if((value == NULL) || (tmp_path_joined == NULL)) … … 808 815 int main(int argc, char** argv) 809 816 { 810 void_stack* path_stack;811 817 char** path = NULL; 812 818 REGF_FILE* f; 813 REGF_NK_REC* root;814 819 REGFI_ITERATOR* iter; 815 820 int retr_path_ret; … … 883 888 bailOut(3, "ERROR: Couldn't create registry iterator.\n"); 884 889 890 if(print_header) 891 { 892 if(print_security) 893 printf("PATH,TYPE,VALUE,MTIME,OWNER,GROUP,SACL,DACL\n"); 894 else 895 printf("PATH,TYPE,VALUE,MTIME\n"); 896 } 897 885 898 if(path_filter_enabled && path_filter != NULL) 886 899 path = splitPath(path_filter); 887 900 888 901 if(path != NULL) 889 902 { … … 892 905 fprintf(stderr, "WARNING: specified path not found.\n"); 893 906 else if (retr_path_ret == 2) 894 printKeyTree(iter , path_filter);907 printKeyTree(iter); 895 908 else if(retr_path_ret != 0) 896 909 bailOut(4, "ERROR: Unknown error occurred in retrieving path.\n"); 897 910 } 898 911 else 899 printKeyTree(iter , "");912 printKeyTree(iter); 900 913 901 914 regfi_iterator_free(iter);
Note: See TracChangeset
for help on using the changeset viewer.