[111] | 1 | /* |
---|
[121] | 2 | * This file stores code common to the command line tools. |
---|
| 3 | * XXX: This should be converted to a proper library. |
---|
[111] | 4 | * |
---|
[232] | 5 | * Copyright (C) 2005-2008,2011 Timothy D. Morgan |
---|
[111] | 6 | * Copyright (C) 2002 Richard Sharpe, rsharpe@richardsharpe.com |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or modify |
---|
| 9 | * it under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation; version 3 of the License. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 20 | * |
---|
[121] | 21 | * $Id: common.c 251 2011-05-06 01:34:35Z tim $ |
---|
[111] | 22 | */ |
---|
| 23 | |
---|
| 24 | #include <iconv.h> |
---|
| 25 | iconv_t conv_desc; |
---|
| 26 | |
---|
| 27 | const char* key_special_chars = ",\"\\/"; |
---|
| 28 | const char* subfield_special_chars = ",\"\\|"; |
---|
| 29 | const char* common_special_chars = ",\"\\"; |
---|
| 30 | |
---|
[143] | 31 | #define REGLOOKUP_EXIT_OK 0 |
---|
| 32 | #define REGLOOKUP_EXIT_OSERR 71 |
---|
| 33 | #define REGLOOKUP_EXIT_USAGE 64 |
---|
| 34 | #define REGLOOKUP_EXIT_DATAERR 65 |
---|
| 35 | #define REGLOOKUP_EXIT_NOINPUT 66 |
---|
[111] | 36 | |
---|
[143] | 37 | |
---|
[178] | 38 | /* Windows is lame */ |
---|
| 39 | #ifdef O_BINARY |
---|
| 40 | #define REGLOOKUP_OPEN_FLAGS O_RDONLY|O_BINARY |
---|
| 41 | #else |
---|
| 42 | #define REGLOOKUP_OPEN_FLAGS O_RDONLY |
---|
| 43 | #endif |
---|
| 44 | |
---|
| 45 | |
---|
[111] | 46 | void bailOut(int code, char* message) |
---|
| 47 | { |
---|
[186] | 48 | fprintf(stderr, "%s", message); |
---|
[111] | 49 | exit(code); |
---|
| 50 | } |
---|
| 51 | |
---|
[182] | 52 | void printMsgs() |
---|
[137] | 53 | { |
---|
[182] | 54 | char* msgs = regfi_log_get_str(); |
---|
[137] | 55 | if(msgs != NULL) |
---|
| 56 | { |
---|
| 57 | fprintf(stderr, "%s", msgs); |
---|
| 58 | free(msgs); |
---|
| 59 | } |
---|
| 60 | } |
---|
[111] | 61 | |
---|
[182] | 62 | void clearMsgs() |
---|
[138] | 63 | { |
---|
[182] | 64 | char* msgs = regfi_log_get_str(); |
---|
[138] | 65 | if(msgs != NULL) |
---|
| 66 | free(msgs); |
---|
| 67 | } |
---|
[137] | 68 | |
---|
[138] | 69 | |
---|
[111] | 70 | /* Returns a newly malloc()ed string which contains original buffer, |
---|
| 71 | * except for non-printable or special characters are quoted in hex |
---|
| 72 | * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted |
---|
| 73 | * character. A null terminator is added, since only ascii, not binary, |
---|
| 74 | * is returned. |
---|
| 75 | */ |
---|
| 76 | static char* quote_buffer(const unsigned char* str, |
---|
| 77 | unsigned int len, const char* special) |
---|
| 78 | { |
---|
| 79 | unsigned int i, added_len; |
---|
| 80 | unsigned int num_written = 0; |
---|
| 81 | |
---|
| 82 | unsigned int buf_len = sizeof(char)*(len+1); |
---|
[136] | 83 | char* ret_val = NULL; |
---|
[111] | 84 | char* tmp_buf; |
---|
| 85 | |
---|
[136] | 86 | if(buf_len > 0) |
---|
| 87 | ret_val = malloc(buf_len); |
---|
[111] | 88 | if(ret_val == NULL) |
---|
| 89 | return NULL; |
---|
| 90 | |
---|
| 91 | for(i=0; i<len; i++) |
---|
| 92 | { |
---|
| 93 | if(buf_len <= (num_written+5)) |
---|
| 94 | { |
---|
| 95 | /* Expand the buffer by the memory consumption rate seen so far |
---|
| 96 | * times the amount of input left to process. The expansion is bounded |
---|
| 97 | * below by a minimum safety increase, and above by the maximum possible |
---|
| 98 | * output string length. This should minimize both the number of |
---|
| 99 | * reallocs() and the amount of wasted memory. |
---|
| 100 | */ |
---|
| 101 | added_len = (len-i)*num_written/(i+1); |
---|
| 102 | if((buf_len+added_len) > (len*4+1)) |
---|
| 103 | buf_len = len*4+1; |
---|
| 104 | else |
---|
| 105 | { |
---|
| 106 | if (added_len < 5) |
---|
| 107 | buf_len += 5; |
---|
| 108 | else |
---|
| 109 | buf_len += added_len; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | tmp_buf = realloc(ret_val, buf_len); |
---|
| 113 | if(tmp_buf == NULL) |
---|
| 114 | { |
---|
| 115 | free(ret_val); |
---|
| 116 | return NULL; |
---|
| 117 | } |
---|
| 118 | ret_val = tmp_buf; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | if(str[i] < 32 || str[i] > 126 || strchr(special, str[i]) != NULL) |
---|
| 122 | { |
---|
| 123 | num_written += snprintf(ret_val + num_written, buf_len - num_written, |
---|
| 124 | "\\x%.2X", str[i]); |
---|
| 125 | } |
---|
| 126 | else |
---|
| 127 | ret_val[num_written++] = str[i]; |
---|
| 128 | } |
---|
| 129 | ret_val[num_written] = '\0'; |
---|
| 130 | |
---|
| 131 | return ret_val; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | /* Returns a newly malloc()ed string which contains original string, |
---|
| 136 | * except for non-printable or special characters are quoted in hex |
---|
| 137 | * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted |
---|
| 138 | * character. |
---|
| 139 | */ |
---|
| 140 | static char* quote_string(const char* str, const char* special) |
---|
| 141 | { |
---|
| 142 | unsigned int len; |
---|
| 143 | |
---|
| 144 | if(str == NULL) |
---|
| 145 | return NULL; |
---|
| 146 | |
---|
| 147 | len = strlen(str); |
---|
| 148 | return quote_buffer((const unsigned char*)str, len, special); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | /* |
---|
| 153 | * Convert a data value to a string for display. Returns NULL on error, |
---|
| 154 | * and the string to display if there is no error, or a non-fatal |
---|
| 155 | * error. On any error (fatal or non-fatal) occurs, (*error_msg) will |
---|
| 156 | * be set to a newly allocated string, containing an error message. If |
---|
| 157 | * a memory allocation failure occurs while generating the error |
---|
| 158 | * message, both the return value and (*error_msg) will be NULL. It |
---|
| 159 | * is the responsibility of the caller to free both a non-NULL return |
---|
| 160 | * value, and a non-NULL (*error_msg). |
---|
| 161 | */ |
---|
[184] | 162 | static char* data_to_ascii(const REGFI_DATA* data, char** error_msg) |
---|
[111] | 163 | { |
---|
[159] | 164 | char* ret_val; |
---|
[111] | 165 | char* cur_quoted; |
---|
[159] | 166 | char* tmp_ptr; |
---|
| 167 | char* delim; |
---|
[168] | 168 | uint32_t ret_val_left, i, tmp_len; |
---|
[111] | 169 | |
---|
[159] | 170 | if(data == NULL || data->size == 0) |
---|
[111] | 171 | { |
---|
[159] | 172 | *error_msg = (char*)malloc(37); |
---|
[111] | 173 | if(*error_msg == NULL) |
---|
| 174 | return NULL; |
---|
[159] | 175 | strcpy(*error_msg, "Data pointer was NULL or size was 0."); |
---|
[111] | 176 | return NULL; |
---|
| 177 | } |
---|
| 178 | *error_msg = NULL; |
---|
| 179 | |
---|
[159] | 180 | |
---|
| 181 | if(data->interpreted_size == 0) |
---|
[111] | 182 | { |
---|
[159] | 183 | *error_msg = (char*)malloc(51); |
---|
| 184 | if(*error_msg == NULL) |
---|
| 185 | return NULL; |
---|
| 186 | strcpy(*error_msg, "Data could not be interpreted, quoting raw buffer."); |
---|
[171] | 187 | return quote_buffer(data->raw, data->size, subfield_special_chars); |
---|
[159] | 188 | } |
---|
| 189 | |
---|
| 190 | switch (data->type) |
---|
| 191 | { |
---|
[111] | 192 | case REG_SZ: |
---|
[159] | 193 | ret_val = quote_string((char*)data->interpreted.string, common_special_chars); |
---|
| 194 | if(ret_val == NULL && (*error_msg = (char*)malloc(49)) != NULL) |
---|
| 195 | strcpy(*error_msg, "Buffer could not be quoted due to unknown error."); |
---|
| 196 | |
---|
| 197 | return ret_val; |
---|
| 198 | break; |
---|
| 199 | |
---|
| 200 | |
---|
[111] | 201 | case REG_EXPAND_SZ: |
---|
[159] | 202 | ret_val = quote_string((char*)data->interpreted.expand_string, |
---|
| 203 | common_special_chars); |
---|
| 204 | if(ret_val == NULL && (*error_msg = (char*)malloc(49)) != NULL) |
---|
| 205 | strcpy(*error_msg, "Buffer could not be quoted due to unknown error."); |
---|
| 206 | |
---|
| 207 | return ret_val; |
---|
| 208 | break; |
---|
| 209 | |
---|
[111] | 210 | case REG_LINK: |
---|
[159] | 211 | ret_val = quote_string((char*)data->interpreted.link, common_special_chars); |
---|
| 212 | if(ret_val == NULL && (*error_msg = (char*)malloc(49)) != NULL) |
---|
[126] | 213 | strcpy(*error_msg, "Buffer could not be quoted due to unknown error."); |
---|
[159] | 214 | |
---|
| 215 | return ret_val; |
---|
[111] | 216 | break; |
---|
| 217 | |
---|
| 218 | case REG_DWORD: |
---|
[159] | 219 | ret_val = malloc(sizeof(char)*(8+2+1)); |
---|
| 220 | if(ret_val == NULL) |
---|
[111] | 221 | return NULL; |
---|
| 222 | |
---|
[159] | 223 | sprintf(ret_val, "0x%.8X", data->interpreted.dword); |
---|
| 224 | return ret_val; |
---|
[111] | 225 | break; |
---|
| 226 | |
---|
| 227 | case REG_DWORD_BE: |
---|
[159] | 228 | ret_val = malloc(sizeof(char)*(8+2+1)); |
---|
| 229 | if(ret_val == NULL) |
---|
[111] | 230 | return NULL; |
---|
| 231 | |
---|
[159] | 232 | sprintf(ret_val, "0x%.8X", data->interpreted.dword_be); |
---|
| 233 | return ret_val; |
---|
[111] | 234 | break; |
---|
| 235 | |
---|
| 236 | case REG_QWORD: |
---|
[159] | 237 | ret_val = malloc(sizeof(char)*(16+2+1)); |
---|
| 238 | if(ret_val == NULL) |
---|
[111] | 239 | return NULL; |
---|
| 240 | |
---|
[159] | 241 | sprintf(ret_val, "0x%.16llX", |
---|
| 242 | (long long unsigned int)data->interpreted.qword); |
---|
| 243 | return ret_val; |
---|
[111] | 244 | break; |
---|
[159] | 245 | |
---|
[111] | 246 | case REG_MULTI_SZ: |
---|
[159] | 247 | ret_val_left = data->interpreted_size*4+1; |
---|
| 248 | ret_val = malloc(ret_val_left); |
---|
| 249 | if(ret_val == NULL) |
---|
[111] | 250 | return NULL; |
---|
| 251 | |
---|
[159] | 252 | tmp_ptr = ret_val; |
---|
| 253 | tmp_ptr[0] = '\0'; |
---|
| 254 | delim = ""; |
---|
| 255 | for(i=0; data->interpreted.multiple_string[i] != NULL; i++) |
---|
[111] | 256 | { |
---|
[159] | 257 | cur_quoted = quote_string((char*)data->interpreted.multiple_string[i], |
---|
| 258 | subfield_special_chars); |
---|
[209] | 259 | if(cur_quoted != NULL) |
---|
[136] | 260 | { |
---|
[159] | 261 | tmp_len = snprintf(tmp_ptr, ret_val_left, "%s%s",delim, cur_quoted); |
---|
| 262 | tmp_ptr += tmp_len; |
---|
| 263 | ret_val_left -= tmp_len; |
---|
| 264 | free(cur_quoted); |
---|
[136] | 265 | } |
---|
[159] | 266 | delim = "|"; |
---|
[126] | 267 | } |
---|
| 268 | |
---|
[159] | 269 | return ret_val; |
---|
[111] | 270 | break; |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | case REG_NONE: |
---|
[159] | 274 | return quote_buffer(data->interpreted.none, data->interpreted_size, |
---|
| 275 | common_special_chars); |
---|
| 276 | |
---|
| 277 | break; |
---|
| 278 | |
---|
[111] | 279 | case REG_RESOURCE_LIST: |
---|
[159] | 280 | return quote_buffer(data->interpreted.resource_list, data->interpreted_size, |
---|
| 281 | common_special_chars); |
---|
| 282 | |
---|
| 283 | break; |
---|
| 284 | |
---|
[111] | 285 | case REG_FULL_RESOURCE_DESCRIPTOR: |
---|
[159] | 286 | return quote_buffer(data->interpreted.full_resource_descriptor, |
---|
| 287 | data->interpreted_size, common_special_chars); |
---|
| 288 | |
---|
| 289 | break; |
---|
| 290 | |
---|
[111] | 291 | case REG_RESOURCE_REQUIREMENTS_LIST: |
---|
[159] | 292 | return quote_buffer(data->interpreted.resource_requirements_list, |
---|
| 293 | data->interpreted_size, common_special_chars); |
---|
[111] | 294 | |
---|
[159] | 295 | break; |
---|
| 296 | |
---|
[111] | 297 | case REG_BINARY: |
---|
[159] | 298 | return quote_buffer(data->interpreted.binary, data->interpreted_size, |
---|
| 299 | common_special_chars); |
---|
| 300 | |
---|
[111] | 301 | break; |
---|
[159] | 302 | |
---|
| 303 | default: |
---|
| 304 | /* This shouldn't happen, since the regfi routines won't interpret |
---|
| 305 | * unknown types, but just as a safety measure against library changes... |
---|
| 306 | */ |
---|
| 307 | *error_msg = (char*)malloc(65); |
---|
| 308 | if(*error_msg == NULL) |
---|
| 309 | return NULL; |
---|
| 310 | sprintf(*error_msg, |
---|
| 311 | "Unrecognized registry data type (0x%.8X); quoting as binary.", |
---|
| 312 | data->type); |
---|
| 313 | return quote_buffer(data->raw, data->size, common_special_chars); |
---|
[111] | 314 | } |
---|
[159] | 315 | |
---|
[111] | 316 | return NULL; |
---|
| 317 | } |
---|
[172] | 318 | |
---|
| 319 | |
---|
[203] | 320 | static char* get_quoted_keyname(const REGFI_NK* nk) |
---|
[172] | 321 | { |
---|
| 322 | char* ret_val; |
---|
| 323 | |
---|
[206] | 324 | if(nk->name == NULL) |
---|
| 325 | ret_val = quote_buffer(nk->name_raw, nk->name_length, key_special_chars); |
---|
[172] | 326 | else |
---|
[206] | 327 | ret_val = quote_string(nk->name, key_special_chars); |
---|
[172] | 328 | |
---|
| 329 | return ret_val; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | |
---|
[203] | 333 | static char* get_quoted_valuename(const REGFI_VK* vk) |
---|
[172] | 334 | { |
---|
| 335 | char* ret_val; |
---|
| 336 | |
---|
[206] | 337 | if(vk->name == NULL) |
---|
| 338 | ret_val = quote_buffer(vk->name_raw, vk->name_length, |
---|
[172] | 339 | key_special_chars); |
---|
| 340 | else |
---|
[206] | 341 | ret_val = quote_string(vk->name, key_special_chars); |
---|
[172] | 342 | |
---|
| 343 | return ret_val; |
---|
| 344 | } |
---|
[178] | 345 | |
---|
| 346 | |
---|
| 347 | int openHive(const char* filename) |
---|
| 348 | { |
---|
| 349 | int ret_val; |
---|
| 350 | |
---|
| 351 | /* open an existing file */ |
---|
| 352 | if ((ret_val = open(filename, REGLOOKUP_OPEN_FLAGS)) == -1) |
---|
| 353 | { |
---|
| 354 | fprintf(stderr, "ERROR: Failed to open hive. Error returned: %s\n", |
---|
| 355 | strerror(errno)); |
---|
| 356 | return -1; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | return ret_val; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | |
---|
[251] | 363 | void formatTime(REGFI_NTTIME nttime, char* output) |
---|
[178] | 364 | { |
---|
| 365 | time_t tmp_time[1]; |
---|
| 366 | struct tm* tmp_time_s = NULL; |
---|
| 367 | |
---|
[219] | 368 | *tmp_time = (time_t)regfi_nt2unix_time(nttime); |
---|
[178] | 369 | tmp_time_s = gmtime(tmp_time); |
---|
| 370 | strftime(output, |
---|
| 371 | (4+1+2+1+2)+1+(2+1+2+1+2)+1, |
---|
| 372 | "%Y-%m-%d %H:%M:%S", |
---|
| 373 | tmp_time_s); |
---|
| 374 | } |
---|