- Timestamp:
- 02/08/09 14:53:48 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/regfi.c
r137 r138 37 37 /****************************************************************************** 38 38 ******************************************************************************/ 39 void regfi_add_message(REGFI_FILE* file, const char* fmt, ...)39 void regfi_add_message(REGFI_FILE* file, uint16 msg_type, const char* fmt, ...) 40 40 { 41 41 /* XXX: This function is not particularly efficient, 42 42 * but then it is mostly used during errors. 43 43 */ 44 /* XXX: Should we add support for filtering by levels of severity? */45 44 uint32 buf_size, buf_used; 46 45 char* new_msg; 47 46 va_list args; 48 47 49 if(file->last_message == NULL) 50 buf_used = 0; 51 else 52 buf_used = strlen(file->last_message); 53 54 buf_size = buf_used+strlen(fmt)+2+128; 55 new_msg = realloc(file->last_message, buf_size); 56 if(new_msg == NULL) 57 /* XXX: should we report this? */ 58 return; 59 60 va_start(args, fmt); 61 vsnprintf(new_msg+buf_used, buf_size-buf_used, fmt, args); 62 va_end(args); 63 strncat(new_msg, "\n", buf_size-1); 64 65 file->last_message = new_msg; 48 if((file->msg_mask & msg_type) != 0) 49 { 50 if(file->last_message == NULL) 51 buf_used = 0; 52 else 53 buf_used = strlen(file->last_message); 54 55 buf_size = buf_used+strlen(fmt)+160; 56 new_msg = realloc(file->last_message, buf_size); 57 if(new_msg == NULL) 58 /* XXX: should we report this? */ 59 return; 60 61 switch (msg_type) 62 { 63 case REGFI_MSG_INFO: 64 strcpy(new_msg+buf_used, "INFO: "); 65 buf_used += 6; 66 break; 67 case REGFI_MSG_WARN: 68 strcpy(new_msg+buf_used, "WARN: "); 69 buf_used += 6; 70 break; 71 case REGFI_MSG_ERROR: 72 strcpy(new_msg+buf_used, "ERROR: "); 73 buf_used += 7; 74 break; 75 } 76 77 va_start(args, fmt); 78 vsnprintf(new_msg+buf_used, buf_size-buf_used, fmt, args); 79 va_end(args); 80 strncat(new_msg, "\n", buf_size-1); 81 82 file->last_message = new_msg; 83 } 66 84 } 67 85 … … 75 93 76 94 return ret_val; 95 } 96 97 98 void regfi_set_message_mask(REGFI_FILE* file, uint16 mask) 99 { 100 file->msg_mask = mask; 77 101 } 78 102 … … 702 726 &cell_length, &unalloc)) 703 727 { 704 regfi_add_message(file, "ERROR:Could not parse SK record cell"728 regfi_add_message(file, REGFI_MSG_WARN, "Could not parse SK record cell" 705 729 " at offset 0x%.8X.", offset); 706 730 return NULL; … … 709 733 if(sk_header[0] != 's' || sk_header[1] != 'k') 710 734 { 711 regfi_add_message(file, "ERROR: Magic number mismatch in parsing SK record"712 " at offset 0x%.8X.", offset);735 regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" 736 " SK record at offset 0x%.8X.", offset); 713 737 return NULL; 714 738 } … … 729 753 || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) 730 754 { 731 regfi_add_message(file, "ERROR: Invalid cell size found while parsing SK"732 " record at offset 0x%.8X.", offset);755 regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size found while" 756 " parsing SK record at offset 0x%.8X.", offset); 733 757 free(ret_val); 734 758 return NULL; … … 747 771 if(ret_val->desc_size + REGFI_SK_MIN_LENGTH > ret_val->cell_size) 748 772 { 749 regfi_add_message(file, "ERROR: Security descriptor too large for cell" 750 " while parsing SK record at offset 0x%.8X.", offset); 773 regfi_add_message(file, REGFI_MSG_ERROR, "Security descriptor too large for" 774 " cell while parsing SK record at offset 0x%.8X.", 775 offset); 751 776 free(ret_val); 752 777 return NULL; … … 764 789 || length != ret_val->desc_size) 765 790 { 766 regfi_add_message(file, "ERROR: Failed to read security descriptor" 767 " while parsing SK record at offset 0x%.8X.", offset); 791 regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read security" 792 " descriptor while parsing SK record at offset 0x%.8X.", 793 offset); 768 794 free(ret_val); 769 795 return NULL; … … 772 798 if(!(ret_val->sec_desc = winsec_parse_desc(sec_desc_buf, ret_val->desc_size))) 773 799 { 774 regfi_add_message(file, "ERROR: Failed to parse security descriptor" 775 " while parsing SK record at offset 0x%.8X.", offset); 800 regfi_add_message(file, REGFI_MSG_ERROR, "Failed to parse security" 801 " descriptor while parsing SK record at offset 0x%.8X.", 802 offset); 776 803 free(sec_desc_buf); 777 804 free(ret_val); … … 794 821 if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) 795 822 { 796 regfi_add_message(file, "ERROR:Failed to read cell header"823 regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read cell header" 797 824 " while parsing value list at offset 0x%.8X.", offset); 798 825 return NULL; … … 807 834 if((num_values * sizeof(uint32)) > cell_length-sizeof(uint32)) 808 835 { 809 regfi_add_message(file, "ERROR:Too many values found"836 regfi_add_message(file, REGFI_MSG_ERROR, "Too many values found" 810 837 " while parsing value list at offset 0x%.8X.", offset); 811 838 return NULL; … … 820 847 if((regfi_read(file->fd, (uint8*)ret_val, &length) != 0) || length != read_len) 821 848 { 822 regfi_add_message(file, "ERROR:Failed to read value pointers"849 regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read value pointers" 823 850 " while parsing value list at offset 0x%.8X.", offset); 824 851 free(ret_val); … … 837 864 || ((ret_val[i] & 0xFFFFFFF8) != ret_val[i])) 838 865 { 839 regfi_add_message(file, "ERROR: Invalid value pointer (0x%.8X) found"840 " while parsing value list at offset 0x%.8X.",841 ret_val[i], offset);866 regfi_add_message(file, REGFI_MSG_ERROR, "Invalid value pointer" 867 " (0x%.8X) found while parsing value list at offset" 868 " 0x%.8X.", ret_val[i], offset); 842 869 free(ret_val); 843 870 return NULL; … … 934 961 if ((nk = regfi_parse_nk(file, offset, max_length, true)) == NULL) 935 962 { 936 regfi_add_message(file, "ERROR:Could not load NK record at"963 regfi_add_message(file, REGFI_MSG_ERROR, "Could not load NK record at" 937 964 " offset 0x%.8X.", offset); 938 965 return NULL; … … 965 992 if(strict && nk->values == NULL) 966 993 { 967 regfi_add_message(file, "ERROR:Could not load value list"994 regfi_add_message(file, REGFI_MSG_ERROR, "Could not load value list" 968 995 " for NK record at offset 0x%.8X.", 969 996 offset); … … 1111 1138 rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin); 1112 1139 hbin_off = hbin->file_off + hbin->block_size; 1113 /*fprintf(stderr, "file_length=%.8X,hbin_off=%.8X,hbin->block_size=%.8X,hbin->next_block=%.8X\n",1114 file_length, hbin_off, hbin->block_size, hbin->next_block);*/1115 1140 hbin = regfi_parse_hbin(rb, hbin_off, true); 1116 1141 } 1142 1143 /* Default message mask */ 1144 rb->msg_mask = REGFI_MSG_ERROR|REGFI_MSG_WARN; 1117 1145 1118 1146 /* success */ … … 1661 1689 if(lseek(file->fd, offset, SEEK_SET) == -1) 1662 1690 { 1663 regfi_add_message(file, "ERROR:Seek failed"1691 regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" 1664 1692 " while parsing hbin at offset 0x%.8X.", offset); 1665 1693 return NULL; … … 1673 1701 if(lseek(file->fd, offset, SEEK_SET) == -1) 1674 1702 { 1675 regfi_add_message(file, "ERROR:Seek failed"1703 regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" 1676 1704 " while parsing hbin at offset 0x%.8X.", offset); 1677 1705 return NULL; … … 1685 1713 if(strict && (memcmp(hbin->magic, "hbin", 4) != 0)) 1686 1714 { 1687 /* XXX: add this back in when we have configurable verbosity. */1688 /* regfi_add_message(file, "INFO: Magic number mismatch (%.2X %.2X %.2X %.2X)"1689 " while parsing hbin at offset 0x%.8X.", hbin->magic[0],1690 hbin->magic[ 1], hbin->magic[2], hbin->magic[3], offset); */1715 regfi_add_message(file, REGFI_MSG_INFO, "Magic number mismatch " 1716 "(%.2X %.2X %.2X %.2X) while parsing hbin at offset" 1717 " 0x%.8X.", hbin->magic[0], hbin->magic[1], 1718 hbin->magic[2], hbin->magic[3], offset); 1691 1719 free(hbin); 1692 1720 return NULL; … … 1695 1723 hbin->first_hbin_off = IVAL(hbin_header, 0x4); 1696 1724 hbin->block_size = IVAL(hbin_header, 0x8); 1697 /*fprintf(stderr, "hbin->block_size field => %.8X\n", IVAL(hbin_header, 0x8));*/1698 /* hbin->block_size = IVAL(hbin_header, 0x8);*/1699 1725 /* this should be the same thing as hbin->block_size but just in case */ 1700 1726 hbin->next_block = IVAL(hbin_header, 0x1C); … … 1710 1736 || (hbin->block_size & 0xFFFFF000) != hbin->block_size) 1711 1737 { 1712 regfi_add_message(file, "ERROR:The hbin offset is not aligned"1738 regfi_add_message(file, REGFI_MSG_ERROR, "The hbin offset is not aligned" 1713 1739 " or runs off the end of the file" 1714 1740 " while parsing hbin at offset 0x%.8X.", offset); … … 1736 1762 &cell_length, &unalloc)) 1737 1763 { 1738 regfi_add_message(file, "ERROR:Could not parse cell header"1764 regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" 1739 1765 " while parsing NK record at offset 0x%.8X.", offset); 1740 1766 return NULL; … … 1744 1770 if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k')) 1745 1771 { 1746 regfi_add_message(file, "ERROR: Magic number mismatch in parsing NK record"1747 " at offset 0x%.8X.", offset);1772 regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" 1773 " NK record at offset 0x%.8X.", offset); 1748 1774 return NULL; 1749 1775 } … … 1752 1778 if(ret_val == NULL) 1753 1779 { 1754 regfi_add_message(file, "ERROR:Failed to allocate memory while"1780 regfi_add_message(file, REGFI_MSG_ERROR, "Failed to allocate memory while" 1755 1781 " parsing NK record at offset 0x%.8X.", offset); 1756 1782 return NULL; … … 1765 1791 || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) 1766 1792 { 1767 regfi_add_message(file, "ERROR: A length check failed while parsing"1768 " NK record at offset 0x%.8X.", offset);1793 regfi_add_message(file, REGFI_MSG_ERROR, "A length check failed while" 1794 " parsing NK record at offset 0x%.8X.", offset); 1769 1795 free(ret_val); 1770 1796 return NULL; … … 1782 1808 && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN3)) 1783 1809 { 1784 regfi_add_message(file, "WARN: Unknown key type (0x%.4X) while parsing"1785 " NK record at offset 0x%.8X.", ret_val->key_type,1786 offset);1810 regfi_add_message(file, REGFI_MSG_WARN, "Unknown key type (0x%.4X) while" 1811 " parsing NK record at offset 0x%.8X.", 1812 ret_val->key_type, offset); 1787 1813 } 1788 1814 … … 1825 1851 if(strict) 1826 1852 { 1827 regfi_add_message(file, "ERROR:Contents too large for cell"1853 regfi_add_message(file, REGFI_MSG_ERROR, "Contents too large for cell" 1828 1854 " while parsing NK record at offset 0x%.8X.", offset); 1829 1855 free(ret_val); … … 1857 1883 || length != ret_val->name_length) 1858 1884 { 1859 regfi_add_message(file, "ERROR:Failed to read key name"1885 regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read key name" 1860 1886 " while parsing NK record at offset 0x%.8X.", offset); 1861 1887 free(ret_val->keyname); … … 1879 1905 { 1880 1906 ret_val->classname = NULL; 1881 regfi_add_message(file, "WARN: Could not find hbin for class name" 1882 " while parsing NK record at offset 0x%.8X.", offset); 1907 regfi_add_message(file, REGFI_MSG_WARN, "Could not find hbin for class" 1908 " name while parsing NK record at offset 0x%.8X.", 1909 offset); 1883 1910 } 1884 1911 /* XXX: Should add this back and make it more strict? … … 1887 1914 */ 1888 1915 } 1889 /* 1890 if(ret_val->key_type == 0x0000 || ret_val->key_type == 0x4020) 1891 { 1892 fprintf(stderr, "INFO: keyname=%s,classname=%s,unalloc=%d,num_subkeys=%d,num_values=%d\n", 1893 ret_val->keyname,ret_val->classname,unalloc,ret_val->num_subkeys,ret_val->num_values); 1894 } 1895 */ 1916 1896 1917 return ret_val; 1897 1918 } … … 1911 1932 if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) 1912 1933 { 1913 regfi_add_message(file, "ERROR:Could not parse cell header"1934 regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" 1914 1935 " while parsing class name at offset 0x%.8X.", offset); 1915 1936 return NULL; … … 1918 1939 if((cell_length & 0xFFFFFFF8) != cell_length) 1919 1940 { 1920 regfi_add_message(file, "ERROR:Cell length not a multiple of 8"1941 regfi_add_message(file, REGFI_MSG_ERROR, "Cell length not a multiple of 8" 1921 1942 " while parsing class name at offset 0x%.8X.", offset); 1922 1943 return NULL; … … 1925 1946 if(cell_length > max_size) 1926 1947 { 1927 regfi_add_message(file, "WARN: Cell stretches past hbin boundary" 1928 " while parsing class name at offset 0x%.8X.", offset); 1948 regfi_add_message(file, REGFI_MSG_WARN, "Cell stretches past hbin " 1949 "boundary while parsing class name at offset 0x%.8X.", 1950 offset); 1929 1951 if(strict) 1930 1952 return NULL; … … 1934 1956 if((cell_length - 4) < *name_length) 1935 1957 { 1936 regfi_add_message(file, "WARN: Class name is larger than cell_length" 1937 " while parsing class name at offset 0x%.8X.", offset); 1958 regfi_add_message(file, REGFI_MSG_WARN, "Class name is larger than" 1959 " cell_length while parsing class name at offset" 1960 " 0x%.8X.", offset); 1938 1961 if(strict) 1939 1962 return NULL; … … 1948 1971 || length != *name_length) 1949 1972 { 1950 regfi_add_message(file, "ERROR:Could not read class name"1973 regfi_add_message(file, REGFI_MSG_ERROR, "Could not read class name" 1951 1974 " while parsing class name at offset 0x%.8X.", offset); 1952 1975 free(ret_val); … … 1975 1998 &cell_length, &unalloc)) 1976 1999 { 1977 regfi_add_message(file, "ERROR:Could not parse cell header"2000 regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" 1978 2001 " while parsing VK record at offset 0x%.8X.", offset); 1979 2002 return NULL; … … 1992 2015 || ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8)) 1993 2016 { 1994 regfi_add_message(file, "ERROR:Invalid cell size encountered"2017 regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size encountered" 1995 2018 " while parsing VK record at offset 0x%.8X.", offset); 1996 2019 free(ret_val); … … 2006 2029 * 0xFFFF. 2007 2030 */ 2008 regfi_add_message(file, "ERROR:Magic number mismatch"2031 regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch" 2009 2032 " while parsing VK record at offset 0x%.8X.", offset); 2010 2033 free(ret_val); … … 2025 2048 if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size) 2026 2049 { 2027 regfi_add_message(file, "WARN: Name too long for remaining cell space" 2028 " while parsing VK record at offset 0x%.8X.", offset); 2050 regfi_add_message(file, REGFI_MSG_WARN, "Name too long for remaining cell" 2051 " space while parsing VK record at offset 0x%.8X.", 2052 offset); 2029 2053 if(strict) 2030 2054 { … … 2052 2076 || length != ret_val->name_length) 2053 2077 { 2054 regfi_add_message(file, "ERROR:Could not read value name"2078 regfi_add_message(file, REGFI_MSG_ERROR, "Could not read value name" 2055 2079 " while parsing VK record at offset 0x%.8X.", offset); 2056 2080 free(ret_val->valuename); … … 2093 2117 else 2094 2118 { 2095 regfi_add_message(file, "WARN:Could not find hbin for data"2119 regfi_add_message(file, REGFI_MSG_WARN, "Could not find hbin for data" 2096 2120 " while parsing VK record at offset 0x%.8X.", offset); 2097 2121 ret_val->data = NULL; … … 2101 2125 if(ret_val->data == NULL) 2102 2126 { 2103 regfi_add_message(file, "WARN:Could not parse data record"2127 regfi_add_message(file, REGFI_MSG_WARN, "Could not parse data record" 2104 2128 " while parsing VK record at offset 0x%.8X.", offset); 2105 2129 } … … 2124 2148 if(length > 4) 2125 2149 { 2126 regfi_add_message(file, "ERROR:Data in offset but length > 4"2150 regfi_add_message(file, REGFI_MSG_ERROR, "Data in offset but length > 4" 2127 2151 " while parsing data record at offset 0x%.8X.", 2128 2152 offset); … … 2141 2165 &cell_length, &unalloc)) 2142 2166 { 2143 regfi_add_message(file, "ERROR:Could not parse cell while"2167 regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while" 2144 2168 " parsing data record at offset 0x%.8X.", offset); 2145 2169 return NULL; … … 2148 2172 if((cell_length & 0xFFFFFFF8) != cell_length) 2149 2173 { 2150 regfi_add_message(file, "ERROR:Cell length not multiple of 8"2174 regfi_add_message(file, REGFI_MSG_WARN, "Cell length not multiple of 8" 2151 2175 " while parsing data record at offset 0x%.8X.", 2152 2176 offset); … … 2156 2180 if(cell_length > max_size) 2157 2181 { 2158 regfi_add_message(file, "WARN:Cell extends past hbin boundary"2182 regfi_add_message(file, REGFI_MSG_WARN, "Cell extends past hbin boundary" 2159 2183 " while parsing data record at offset 0x%.8X.", 2160 2184 offset); … … 2171 2195 * such as 53392. 2172 2196 */ 2173 regfi_add_message(file, "WARN:Data length (0x%.8X) larger than"2197 regfi_add_message(file, REGFI_MSG_WARN, "Data length (0x%.8X) larger than" 2174 2198 " remaining cell length (0x%.8X)" 2175 2199 " while parsing data record at offset 0x%.8X.", … … 2188 2212 || read_length != length) 2189 2213 { 2190 regfi_add_message(file, "ERROR:Could not read data block while"2214 regfi_add_message(file, REGFI_MSG_ERROR, "Could not read data block while" 2191 2215 " parsing data record at offset 0x%.8X.", offset); 2192 2216 free(ret_val);
Note: See TracChangeset
for help on using the changeset viewer.