Avoid crashing if we call num_usable_bridges() when bridges are not enabled
[tor/appveyor.git] / src / or / statefile.c
blob97bd9cac362fd6dc90e0d3eecc640fc8300d0295
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file statefile.c
10 * \brief Handles parsing and encoding the persistent 'state' file that carries
11 * miscellaneous persistent state between Tor invocations.
13 * This 'state' file is a typed key-value store that allows multiple
14 * entries for the same key. It follows the same metaformat as described
15 * in confparse.c, and uses the same code to read and write itself.
17 * The state file is most suitable for small values that don't change too
18 * frequently. For values that become very large, we typically use a separate
19 * file -- for example, see how we handle microdescriptors, by storing them in
20 * a separate file with a journal.
22 * The current state is accessed via get_or_state(), which returns a singleton
23 * or_state_t object. Functions that change it should call
24 * or_state_mark_dirty() to ensure that it will get written to disk.
26 * The or_state_save() function additionally calls various functioens
27 * throughout Tor that might want to flush more state to the the disk,
28 * including some in rephist.c, entrynodes.c, circuitstats.c, hibernate.c.
31 #define STATEFILE_PRIVATE
32 #include "or.h"
33 #include "circuitstats.h"
34 #include "config.h"
35 #include "confparse.h"
36 #include "connection.h"
37 #include "control.h"
38 #include "entrynodes.h"
39 #include "hibernate.h"
40 #include "rephist.h"
41 #include "router.h"
42 #include "sandbox.h"
43 #include "statefile.h"
45 /** A list of state-file "abbreviations," for compatibility. */
46 static config_abbrev_t state_abbrevs_[] = {
47 { "AccountingBytesReadInterval", "AccountingBytesReadInInterval", 0, 0 },
48 { "HelperNode", "EntryGuard", 0, 0 },
49 { "HelperNodeDownSince", "EntryGuardDownSince", 0, 0 },
50 { "HelperNodeUnlistedSince", "EntryGuardUnlistedSince", 0, 0 },
51 { "EntryNode", "EntryGuard", 0, 0 },
52 { "EntryNodeDownSince", "EntryGuardDownSince", 0, 0 },
53 { "EntryNodeUnlistedSince", "EntryGuardUnlistedSince", 0, 0 },
54 { NULL, NULL, 0, 0},
57 /** dummy instance of or_state_t, used for type-checking its
58 * members with CONF_CHECK_VAR_TYPE. */
59 DUMMY_TYPECHECK_INSTANCE(or_state_t);
61 /*XXXX these next two are duplicates or near-duplicates from config.c */
62 #define VAR(name,conftype,member,initvalue) \
63 { name, CONFIG_TYPE_ ## conftype, offsetof(or_state_t, member), \
64 initvalue CONF_TEST_MEMBERS(or_state_t, conftype, member) }
65 /** As VAR, but the option name and member name are the same. */
66 #define V(member,conftype,initvalue) \
67 VAR(#member, conftype, member, initvalue)
69 /** Array of "state" variables saved to the ~/.tor/state file. */
70 static config_var_t state_vars_[] = {
71 /* Remember to document these in state-contents.txt ! */
73 V(AccountingBytesReadInInterval, MEMUNIT, NULL),
74 V(AccountingBytesWrittenInInterval, MEMUNIT, NULL),
75 V(AccountingExpectedUsage, MEMUNIT, NULL),
76 V(AccountingIntervalStart, ISOTIME, NULL),
77 V(AccountingSecondsActive, INTERVAL, NULL),
78 V(AccountingSecondsToReachSoftLimit,INTERVAL, NULL),
79 V(AccountingSoftLimitHitAt, ISOTIME, NULL),
80 V(AccountingBytesAtSoftLimit, MEMUNIT, NULL),
82 VAR("EntryGuard", LINELIST_S, EntryGuards, NULL),
83 VAR("EntryGuardDownSince", LINELIST_S, EntryGuards, NULL),
84 VAR("EntryGuardUnlistedSince", LINELIST_S, EntryGuards, NULL),
85 VAR("EntryGuardAddedBy", LINELIST_S, EntryGuards, NULL),
86 VAR("EntryGuardPathBias", LINELIST_S, EntryGuards, NULL),
87 VAR("EntryGuardPathUseBias", LINELIST_S, EntryGuards, NULL),
88 V(EntryGuards, LINELIST_V, NULL),
90 VAR("TransportProxy", LINELIST_S, TransportProxies, NULL),
91 V(TransportProxies, LINELIST_V, NULL),
93 V(HidServRevCounter, LINELIST, NULL),
95 V(BWHistoryReadEnds, ISOTIME, NULL),
96 V(BWHistoryReadInterval, UINT, "900"),
97 V(BWHistoryReadValues, CSV, ""),
98 V(BWHistoryReadMaxima, CSV, ""),
99 V(BWHistoryWriteEnds, ISOTIME, NULL),
100 V(BWHistoryWriteInterval, UINT, "900"),
101 V(BWHistoryWriteValues, CSV, ""),
102 V(BWHistoryWriteMaxima, CSV, ""),
103 V(BWHistoryDirReadEnds, ISOTIME, NULL),
104 V(BWHistoryDirReadInterval, UINT, "900"),
105 V(BWHistoryDirReadValues, CSV, ""),
106 V(BWHistoryDirReadMaxima, CSV, ""),
107 V(BWHistoryDirWriteEnds, ISOTIME, NULL),
108 V(BWHistoryDirWriteInterval, UINT, "900"),
109 V(BWHistoryDirWriteValues, CSV, ""),
110 V(BWHistoryDirWriteMaxima, CSV, ""),
112 V(Guard, LINELIST, NULL),
114 V(TorVersion, STRING, NULL),
116 V(LastRotatedOnionKey, ISOTIME, NULL),
117 V(LastWritten, ISOTIME, NULL),
119 V(TotalBuildTimes, UINT, NULL),
120 V(CircuitBuildAbandonedCount, UINT, "0"),
121 VAR("CircuitBuildTimeBin", LINELIST_S, BuildtimeHistogram, NULL),
122 VAR("BuildtimeHistogram", LINELIST_V, BuildtimeHistogram, NULL),
124 END_OF_CONFIG_VARS
127 #undef VAR
128 #undef V
130 static int or_state_validate(or_state_t *state, char **msg);
132 static int or_state_validate_cb(void *old_options, void *options,
133 void *default_options,
134 int from_setconf, char **msg);
136 /** Magic value for or_state_t. */
137 #define OR_STATE_MAGIC 0x57A73f57
139 /** "Extra" variable in the state that receives lines we can't parse. This
140 * lets us preserve options from versions of Tor newer than us. */
141 static config_var_t state_extra_var = {
142 "__extra", CONFIG_TYPE_LINELIST, offsetof(or_state_t, ExtraLines), NULL
143 CONF_TEST_MEMBERS(or_state_t, LINELIST, ExtraLines)
146 /** Configuration format for or_state_t. */
147 static const config_format_t state_format = {
148 sizeof(or_state_t),
149 OR_STATE_MAGIC,
150 offsetof(or_state_t, magic_),
151 state_abbrevs_,
152 NULL,
153 state_vars_,
154 or_state_validate_cb,
155 &state_extra_var,
158 /** Persistent serialized state. */
159 static or_state_t *global_state = NULL;
161 /** Return the persistent state struct for this Tor. */
162 MOCK_IMPL(or_state_t *,
163 get_or_state, (void))
165 tor_assert(global_state);
166 return global_state;
169 /** Return true iff we have loaded the global state for this Tor */
171 or_state_loaded(void)
173 return global_state != NULL;
176 /** Return true if <b>line</b> is a valid state TransportProxy line.
177 * Return false otherwise. */
178 static int
179 state_transport_line_is_valid(const char *line)
181 smartlist_t *items = NULL;
182 char *addrport=NULL;
183 tor_addr_t addr;
184 uint16_t port = 0;
185 int r;
187 items = smartlist_new();
188 smartlist_split_string(items, line, NULL,
189 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
191 if (smartlist_len(items) != 2) {
192 log_warn(LD_CONFIG, "state: Not enough arguments in TransportProxy line.");
193 goto err;
196 addrport = smartlist_get(items, 1);
197 if (tor_addr_port_lookup(addrport, &addr, &port) < 0) {
198 log_warn(LD_CONFIG, "state: Could not parse addrport.");
199 goto err;
202 if (!port) {
203 log_warn(LD_CONFIG, "state: Transport line did not contain port.");
204 goto err;
207 r = 1;
208 goto done;
210 err:
211 r = 0;
213 done:
214 SMARTLIST_FOREACH(items, char*, s, tor_free(s));
215 smartlist_free(items);
216 return r;
219 /** Return 0 if all TransportProxy lines in <b>state</b> are well
220 * formed. Otherwise, return -1. */
221 static int
222 validate_transports_in_state(or_state_t *state)
224 int broken = 0;
225 config_line_t *line;
227 for (line = state->TransportProxies ; line ; line = line->next) {
228 tor_assert(!strcmp(line->key, "TransportProxy"));
229 if (!state_transport_line_is_valid(line->value))
230 broken = 1;
233 if (broken)
234 log_warn(LD_CONFIG, "state: State file seems to be broken.");
236 return 0;
239 static int
240 or_state_validate_cb(void *old_state, void *state, void *default_state,
241 int from_setconf, char **msg)
243 /* We don't use these; only options do. Still, we need to match that
244 * signature. */
245 (void) from_setconf;
246 (void) default_state;
247 (void) old_state;
249 return or_state_validate(state, msg);
252 /** Return 0 if every setting in <b>state</b> is reasonable, and a
253 * permissible transition from <b>old_state</b>. Else warn and return -1.
254 * Should have no side effects, except for normalizing the contents of
255 * <b>state</b>.
257 static int
258 or_state_validate(or_state_t *state, char **msg)
260 if (entry_guards_parse_state(state, 0, msg)<0)
261 return -1;
263 if (validate_transports_in_state(state)<0)
264 return -1;
266 return 0;
269 /** Replace the current persistent state with <b>new_state</b> */
270 static int
271 or_state_set(or_state_t *new_state)
273 char *err = NULL;
274 int ret = 0;
275 tor_assert(new_state);
276 config_free(&state_format, global_state);
277 global_state = new_state;
278 if (entry_guards_parse_state(global_state, 1, &err)<0) {
279 log_warn(LD_GENERAL,"%s",err);
280 tor_free(err);
281 ret = -1;
283 if (rep_hist_load_state(global_state, &err)<0) {
284 log_warn(LD_GENERAL,"Unparseable bandwidth history state: %s",err);
285 tor_free(err);
286 ret = -1;
288 if (circuit_build_times_parse_state(
289 get_circuit_build_times_mutable(),global_state) < 0) {
290 ret = -1;
292 return ret;
296 * Save a broken state file to a backup location.
298 static void
299 or_state_save_broken(char *fname)
301 int i, res;
302 file_status_t status;
303 char *fname2 = NULL;
304 for (i = 0; i < 100; ++i) {
305 tor_asprintf(&fname2, "%s.%d", fname, i);
306 status = file_status(fname2);
307 if (status == FN_NOENT)
308 break;
309 tor_free(fname2);
311 if (i == 100) {
312 log_warn(LD_BUG, "Unable to parse state in \"%s\"; too many saved bad "
313 "state files to move aside. Discarding the old state file.",
314 fname);
315 res = unlink(fname);
316 if (res != 0) {
317 log_warn(LD_FS,
318 "Also couldn't discard old state file \"%s\" because "
319 "unlink() failed: %s",
320 fname, strerror(errno));
322 } else {
323 log_warn(LD_BUG, "Unable to parse state in \"%s\". Moving it aside "
324 "to \"%s\". This could be a bug in Tor; please tell "
325 "the developers.", fname, fname2);
326 if (tor_rename(fname, fname2) < 0) {//XXXX sandbox prohibits
327 log_warn(LD_BUG, "Weirdly, I couldn't even move the state aside. The "
328 "OS gave an error of %s", strerror(errno));
331 tor_free(fname2);
334 STATIC or_state_t *
335 or_state_new(void)
337 or_state_t *new_state = tor_malloc_zero(sizeof(or_state_t));
338 new_state->magic_ = OR_STATE_MAGIC;
339 config_init(&state_format, new_state);
341 return new_state;
344 /** Reload the persistent state from disk, generating a new state as needed.
345 * Return 0 on success, less than 0 on failure.
348 or_state_load(void)
350 or_state_t *new_state = NULL;
351 char *contents = NULL, *fname;
352 char *errmsg = NULL;
353 int r = -1, badstate = 0;
355 fname = get_datadir_fname("state");
356 switch (file_status(fname)) {
357 case FN_FILE:
358 if (!(contents = read_file_to_str(fname, 0, NULL))) {
359 log_warn(LD_FS, "Unable to read state file \"%s\"", fname);
360 goto done;
362 break;
363 /* treat empty state files as if the file doesn't exist, and generate
364 * a new state file, overwriting the empty file in or_state_save() */
365 case FN_NOENT:
366 case FN_EMPTY:
367 break;
368 case FN_ERROR:
369 case FN_DIR:
370 default:
371 log_warn(LD_GENERAL,"State file \"%s\" is not a file? Failing.", fname);
372 goto done;
374 new_state = or_state_new();
375 if (contents) {
376 config_line_t *lines=NULL;
377 int assign_retval;
378 if (config_get_lines(contents, &lines, 0)<0)
379 goto done;
380 assign_retval = config_assign(&state_format, new_state,
381 lines, 0, &errmsg);
382 config_free_lines(lines);
383 if (assign_retval<0)
384 badstate = 1;
385 if (errmsg) {
386 log_warn(LD_GENERAL, "%s", errmsg);
387 tor_free(errmsg);
391 if (!badstate && or_state_validate(new_state, &errmsg) < 0)
392 badstate = 1;
394 if (errmsg) {
395 log_warn(LD_GENERAL, "%s", errmsg);
396 tor_free(errmsg);
399 if (badstate && !contents) {
400 log_warn(LD_BUG, "Uh oh. We couldn't even validate our own default state."
401 " This is a bug in Tor.");
402 goto done;
403 } else if (badstate && contents) {
404 or_state_save_broken(fname);
406 tor_free(contents);
407 config_free(&state_format, new_state);
409 new_state = or_state_new();
410 } else if (contents) {
411 log_info(LD_GENERAL, "Loaded state from \"%s\"", fname);
412 /* Warn the user if their clock has been set backwards,
413 * they could be tricked into using old consensuses */
414 time_t apparent_skew = time(NULL) - new_state->LastWritten;
415 if (apparent_skew < 0) {
416 /* Initialize bootstrap event reporting because we might call
417 * clock_skew_warning() before the bootstrap state is
418 * initialized, causing an assertion failure. */
419 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
420 clock_skew_warning(NULL, (long)apparent_skew, 1, LD_GENERAL,
421 "local state file", fname);
423 } else {
424 log_info(LD_GENERAL, "Initialized state");
426 if (or_state_set(new_state) == -1) {
427 or_state_save_broken(fname);
429 new_state = NULL;
430 if (!contents) {
431 global_state->next_write = 0;
432 or_state_save(time(NULL));
434 r = 0;
436 done:
437 tor_free(fname);
438 tor_free(contents);
439 if (new_state)
440 config_free(&state_format, new_state);
442 return r;
445 /** Did the last time we tried to write the state file fail? If so, we
446 * should consider disabling such features as preemptive circuit generation
447 * to compute circuit-build-time. */
448 static int last_state_file_write_failed = 0;
450 /** Return whether the state file failed to write last time we tried. */
452 did_last_state_file_write_fail(void)
454 return last_state_file_write_failed;
457 /** If writing the state to disk fails, try again after this many seconds. */
458 #define STATE_WRITE_RETRY_INTERVAL 3600
460 /** If we're a relay, how often should we checkpoint our state file even
461 * if nothing else dirties it? This will checkpoint ongoing stats like
462 * bandwidth used, per-country user stats, etc. */
463 #define STATE_RELAY_CHECKPOINT_INTERVAL (12*60*60)
465 /** Write the persistent state to disk. Return 0 for success, <0 on failure. */
467 or_state_save(time_t now)
469 char *state, *contents;
470 char tbuf[ISO_TIME_LEN+1];
471 char *fname;
473 tor_assert(global_state);
475 if (global_state->next_write > now)
476 return 0;
478 /* Call everything else that might dirty the state even more, in order
479 * to avoid redundant writes. */
480 entry_guards_update_state(global_state);
481 rep_hist_update_state(global_state);
482 circuit_build_times_update_state(get_circuit_build_times(), global_state);
483 if (accounting_is_enabled(get_options()))
484 accounting_run_housekeeping(now);
486 global_state->LastWritten = now;
488 tor_free(global_state->TorVersion);
489 tor_asprintf(&global_state->TorVersion, "Tor %s", get_version());
491 state = config_dump(&state_format, NULL, global_state, 1, 0);
492 format_local_iso_time(tbuf, now);
493 tor_asprintf(&contents,
494 "# Tor state file last generated on %s local time\n"
495 "# Other times below are in UTC\n"
496 "# You *do not* need to edit this file.\n\n%s",
497 tbuf, state);
498 tor_free(state);
499 fname = get_datadir_fname("state");
500 if (write_str_to_file(fname, contents, 0)<0) {
501 log_warn(LD_FS, "Unable to write state to file \"%s\"; "
502 "will try again later", fname);
503 last_state_file_write_failed = 1;
504 tor_free(fname);
505 tor_free(contents);
506 /* Try again after STATE_WRITE_RETRY_INTERVAL (or sooner, if the state
507 * changes sooner). */
508 global_state->next_write = now + STATE_WRITE_RETRY_INTERVAL;
509 return -1;
512 last_state_file_write_failed = 0;
513 log_info(LD_GENERAL, "Saved state to \"%s\"", fname);
514 tor_free(fname);
515 tor_free(contents);
517 if (server_mode(get_options()))
518 global_state->next_write = now + STATE_RELAY_CHECKPOINT_INTERVAL;
519 else
520 global_state->next_write = TIME_MAX;
522 return 0;
525 /** Return the config line for transport <b>transport</b> in the current state.
526 * Return NULL if there is no config line for <b>transport</b>. */
527 STATIC config_line_t *
528 get_transport_in_state_by_name(const char *transport)
530 or_state_t *or_state = get_or_state();
531 config_line_t *line;
532 config_line_t *ret = NULL;
533 smartlist_t *items = NULL;
535 for (line = or_state->TransportProxies ; line ; line = line->next) {
536 tor_assert(!strcmp(line->key, "TransportProxy"));
538 items = smartlist_new();
539 smartlist_split_string(items, line->value, NULL,
540 SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
541 if (smartlist_len(items) != 2) /* broken state */
542 goto done;
544 if (!strcmp(smartlist_get(items, 0), transport)) {
545 ret = line;
546 goto done;
549 SMARTLIST_FOREACH(items, char*, s, tor_free(s));
550 smartlist_free(items);
551 items = NULL;
554 done:
555 if (items) {
556 SMARTLIST_FOREACH(items, char*, s, tor_free(s));
557 smartlist_free(items);
559 return ret;
562 /** Return string containing the address:port part of the
563 * TransportProxy <b>line</b> for transport <b>transport</b>.
564 * If the line is corrupted, return NULL. */
565 static const char *
566 get_transport_bindaddr(const char *line, const char *transport)
568 char *line_tmp = NULL;
570 if (strlen(line) < strlen(transport) + 2) {
571 goto broken_state;
572 } else {
573 /* line should start with the name of the transport and a space.
574 (for example, "obfs2 127.0.0.1:47245") */
575 tor_asprintf(&line_tmp, "%s ", transport);
576 if (strcmpstart(line, line_tmp))
577 goto broken_state;
579 tor_free(line_tmp);
580 return (line+strlen(transport)+1);
583 broken_state:
584 tor_free(line_tmp);
585 return NULL;
588 /** Return a string containing the address:port that a proxy transport
589 * should bind on. The string is stored on the heap and must be freed
590 * by the caller of this function. */
591 char *
592 get_stored_bindaddr_for_server_transport(const char *transport)
594 char *default_addrport = NULL;
595 const char *stored_bindaddr = NULL;
596 config_line_t *line = NULL;
599 /* See if the user explicitly asked for a specific listening
600 address for this transport. */
601 char *conf_bindaddr = get_transport_bindaddr_from_config(transport);
602 if (conf_bindaddr)
603 return conf_bindaddr;
606 line = get_transport_in_state_by_name(transport);
607 if (!line) /* Found no references in state for this transport. */
608 goto no_bindaddr_found;
610 stored_bindaddr = get_transport_bindaddr(line->value, transport);
611 if (stored_bindaddr) /* found stored bindaddr in state file. */
612 return tor_strdup(stored_bindaddr);
614 no_bindaddr_found:
615 /** If we didn't find references for this pluggable transport in the
616 state file, we should instruct the pluggable transport proxy to
617 listen on INADDR_ANY on a random ephemeral port. */
618 tor_asprintf(&default_addrport, "%s:%s", fmt_addr32(INADDR_ANY), "0");
619 return default_addrport;
622 /** Save <b>transport</b> listening on <b>addr</b>:<b>port</b> to
623 state */
624 void
625 save_transport_to_state(const char *transport,
626 const tor_addr_t *addr, uint16_t port)
628 or_state_t *state = get_or_state();
630 char *transport_addrport=NULL;
632 /** find where to write on the state */
633 config_line_t **next, *line;
635 /* see if this transport is already stored in state */
636 config_line_t *transport_line =
637 get_transport_in_state_by_name(transport);
639 if (transport_line) { /* if transport already exists in state... */
640 const char *prev_bindaddr = /* get its addrport... */
641 get_transport_bindaddr(transport_line->value, transport);
642 transport_addrport = tor_strdup(fmt_addrport(addr, port));
644 /* if transport in state has the same address as this one, life is good */
645 if (!strcmp(prev_bindaddr, transport_addrport)) {
646 log_info(LD_CONFIG, "Transport seems to have spawned on its usual "
647 "address:port.");
648 goto done;
649 } else { /* if addrport in state is different than the one we got */
650 log_info(LD_CONFIG, "Transport seems to have spawned on different "
651 "address:port. Let's update the state file with the new "
652 "address:port");
653 tor_free(transport_line->value); /* free the old line */
654 /* replace old addrport line with new line */
655 tor_asprintf(&transport_line->value, "%s %s", transport,
656 fmt_addrport(addr, port));
658 } else { /* never seen this one before; save it in state for next time */
659 log_info(LD_CONFIG, "It's the first time we see this transport. "
660 "Let's save its address:port");
661 next = &state->TransportProxies;
662 /* find the last TransportProxy line in the state and point 'next'
663 right after it */
664 line = state->TransportProxies;
665 while (line) {
666 next = &(line->next);
667 line = line->next;
670 /* allocate space for the new line and fill it in */
671 *next = line = tor_malloc_zero(sizeof(config_line_t));
672 line->key = tor_strdup("TransportProxy");
673 tor_asprintf(&line->value, "%s %s", transport, fmt_addrport(addr, port));
676 if (!get_options()->AvoidDiskWrites)
677 or_state_mark_dirty(state, 0);
679 done:
680 tor_free(transport_addrport);
683 STATIC void
684 or_state_free(or_state_t *state)
686 if (!state)
687 return;
689 config_free(&state_format, state);
692 void
693 or_state_free_all(void)
695 or_state_free(global_state);
696 global_state = NULL;