12 #define UNPARSEABLE_PRIVATE
19 #ifdef HAVE_SYS_STAT_H
31 static int problem_with_dump_desc_dir = 0;
33 #define DESC_DUMP_DATADIR_SUBDIR "unparseable-descs"
34 #define DESC_DUMP_BASE_FILENAME "unparseable-desc"
42 dump_desc_dir = get_datadir_fname(DESC_DUMP_DATADIR_SUBDIR);
51 "Doesn't look like we'll be able to create descriptor dump "
52 "directory %s; dumps will be disabled.",
54 problem_with_dump_desc_dir = 1;
72 "Couldn't check whether descriptor dump directory %s already"
74 dump_desc_dir, strerror(errno));
75 problem_with_dump_desc_dir = 1;
82 "Descriptor dump directory %s already exists and isn't a "
85 problem_with_dump_desc_dir = 1;
102 if (problem_with_dump_desc_dir)
return;
106 dump_desc_dir = get_datadir_fname(DESC_DUMP_DATADIR_SUBDIR);
111 "Failed to create descriptor dump directory %s",
113 problem_with_dump_desc_dir = 1;
133 dumped_desc_t *ent = NULL, *tmp;
147 ent = tor_malloc_zero(
sizeof(*ent));
148 ent->filename = filename;
150 ent->when = time(NULL);
159 tmp = (dumped_desc_t *)(smartlist_get(
descs_dumped, 0));
165 if (strcmp(tmp->filename, filename) != 0) {
171 "Deleting old unparseable descriptor dump %s due to "
182 "Replacing old descriptor dump %s with new identical one",
203 dumped_desc_t *match = NULL;
220 } SMARTLIST_FOREACH_END(ent);
224 match->when = time(NULL);
247 } SMARTLIST_FOREACH_END(ent);
262 dumped_desc_t *ent = NULL;
263 char *path = NULL, *desc = NULL;
264 const char *digest_str;
267 const char *f_pfx = DESC_DUMP_BASE_FILENAME
".";
279 tor_asprintf(&path,
"%s" PATH_SEPARATOR
"%s", dirname, f);
285 digest_str = f + strlen(f_pfx);
299 "Removing unrecognized filename %s from unparseable "
300 "descriptors directory", f);
317 "Failed to read %s from unparseable descriptors directory; "
318 "attempting to remove it.", f);
324 #if SIZE_MAX > UINT64_MAX
325 if (BUG((uint64_t)st.st_size > (uint64_t)SIZE_MAX)) {
333 if (BUG(st.st_size < 0)) {
346 DIGEST_SHA256) < 0) {
349 "Unable to hash content of %s from unparseable descriptors "
360 "Hash of %s from unparseable descriptors directory didn't "
361 "match its filename; removing it", f);
368 ent = tor_malloc_zero(
sizeof(dumped_desc_t));
369 ent->filename = path;
371 ent->len = (size_t) st.st_size;
372 ent->when = st.st_mtime;
391 const dumped_desc_t **a = (
const dumped_desc_t **)a_v;
392 const dumped_desc_t **b = (
const dumped_desc_t **)b_v;
394 if ((a != NULL) && (*a != NULL)) {
395 if ((b != NULL) && (*b != NULL)) {
397 if ((*a)->when < (*b)->when) {
399 }
else if ((*a)->when == (*b)->when) {
429 dumped_desc_t *ent = NULL;
437 "Unable to get contents of unparseable descriptor dump "
472 } SMARTLIST_FOREACH_END(f);
481 "Reloaded unparseable descriptor dump FIFO with %d dump(s) "
482 "totaling %"PRIu64
" bytes",
488 smartlist_free(files);
500 #ifndef TOR_UNIT_TESTS
514 char *debugfile, *debugfile_base;
519 DIGEST_SHA256) < 0) {
521 "Unable to parse descriptor of type %s, and unable to even hash"
527 (
const char *)digest_sha256,
sizeof(digest_sha256));
534 DESC_DUMP_BASE_FILENAME
".%s", digest_sha256_hex);
535 debugfile = get_datadir_fname2(DESC_DUMP_DATADIR_SUBDIR, debugfile_base);
542 if (len <=
get_options()->MaxUnparseableDescSizeToLog) {
551 "Unable to parse descriptor of type %s with hash %s and "
552 "length %lu. See file %s in data directory for details.",
553 type, digest_sha256_hex, (
unsigned long)len,
561 "Unable to parse descriptor of type %s with hash %s and "
562 "length %lu. Descriptor not dumped because we had a "
563 "problem creating the " DESC_DUMP_DATADIR_SUBDIR
565 type, digest_sha256_hex, (
unsigned long)len);
571 "Unable to parse descriptor of type %s with hash %s and "
572 "length %lu. Descriptor not dumped because one with that "
573 "hash has already been dumped.",
574 type, digest_sha256_hex, (
unsigned long)len);
580 "Unable to parse descriptor of type %s with hash %s and "
581 "length %lu. Descriptor not dumped because it exceeds maximum"
582 " log size all by itself.",
583 type, digest_sha256_hex, (
unsigned long)len);
594 "Unable to parse descriptor of type %s with hash %s and "
595 "length %lu. Descriptor not dumped because the sandbox is "
597 type, digest_sha256_hex, (
unsigned long)len);
int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen)
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
const or_options_t * get_options(void)
Header file for config.c.
#define HEX_DIGEST256_LEN
int crypto_digest256(char *digest, const char *m, size_t len, digest_algorithm_t algorithm)
int tor_memeq(const void *a, const void *b, size_t sz)
#define tor_memneq(a, b, sz)
smartlist_t * tor_listdir(const char *dirname)
int check_private_dir(const char *dirname, cpd_check_t check, const char *effective_user)
int write_str_to_file(const char *fname, const char *str, int bin)
int tor_unlink(const char *pathname)
#define RFTS_IGNORE_MISSING
file_status_t file_status(const char *filename)
Master header file for Tor-specific functionality.
int tor_asprintf(char **strp, const char *fmt,...)
int sandbox_is_active(void)
Header file for sandbox.c.
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_del_keeporder(smartlist_t *sl, int idx)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
#define SMARTLIST_DEL_CURRENT_KEEPORDER(sl, var)
uint64_t MaxUnparseableDescSizeToLog
#define MOCK_IMPL(rv, funcname, arglist)
STATIC void dump_desc_populate_fifo_from_directory(const char *dirname)
static void dump_desc_fifo_add_and_clean(char *filename, const uint8_t *digest_sha256, size_t len)
STATIC smartlist_t * descs_dumped
STATIC dumped_desc_t * dump_desc_populate_one_file(const char *dirname, const char *f)
void dump_desc_fifo_cleanup(void)
static void dump_desc_create_dir(void)
static int have_dump_desc_dir
void dump_desc(const char *desc, const char *type)
static int dump_desc_compare_fifo_entries(const void **a_v, const void **b_v)
STATIC uint64_t len_descs_dumped
static int dump_desc_fifo_bump_hash(const uint8_t *digest_sha256)
void dump_desc_init(void)
Header file for unparseable.c.
int strcmpstart(const char *s1, const char *s2)