Tor  0.4.3.0-alpha-dev
main.c
Go to the documentation of this file.
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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file main.c
9  * \brief Invocation module. Initializes subsystems and runs the main loop.
10  **/
11 
12 #include "core/or/or.h"
13 
14 #include "app/config/config.h"
15 #include "app/config/statefile.h"
16 #include "app/config/quiet_level.h"
17 #include "app/main/main.h"
18 #include "app/main/ntmain.h"
19 #include "app/main/shutdown.h"
20 #include "app/main/subsysmgr.h"
23 #include "core/mainloop/mainloop.h"
26 #include "core/or/channel.h"
27 #include "core/or/channelpadding.h"
28 #include "core/or/circuitpadding.h"
29 #include "core/or/circuitlist.h"
30 #include "core/or/command.h"
31 #include "core/or/connection_or.h"
32 #include "core/or/relay.h"
33 #include "core/or/status.h"
34 #include "feature/api/tor_api.h"
40 #include "feature/dirauth/keypin.h"
45 #include "feature/hs/hs_dos.h"
49 #include "feature/relay/dns.h"
53 #include "feature/rend/rendcache.h"
56 #include "feature/stats/rephist.h"
57 #include "lib/compress/compress.h"
58 #include "lib/buf/buffers.h"
61 #include "lib/net/resolve.h"
62 
63 #include "lib/process/waitpid.h"
65 
66 #include "lib/meminfo/meminfo.h"
67 #include "lib/osinfo/uname.h"
68 #include "lib/sandbox/sandbox.h"
69 #include "lib/fs/lockfile.h"
70 #include "lib/net/resolve.h"
71 #include "lib/tls/tortls.h"
73 #include "lib/encoding/confline.h"
74 #include "lib/evloop/timers.h"
76 #include "lib/version/torversion.h"
77 
78 #include <event2/event.h>
79 
82 
84 #include "core/or/port_cfg_st.h"
85 
86 #ifdef HAVE_UNISTD_H
87 #include <unistd.h>
88 #endif
89 
90 #ifdef HAVE_SYSTEMD
91 # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
92 /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
93  * Coverity. Here's a kludge to unconfuse it.
94  */
95 # define __INCLUDE_LEVEL__ 2
96 #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
97 #include <systemd/sd-daemon.h>
98 #endif /* defined(HAVE_SYSTEMD) */
99 
100 #ifdef HAVE_RUST
101 // helper function defined in Rust to output a log message indicating if tor is
102 // running with Rust enabled. See src/rust/tor_util
103 void rust_log_welcome_string(void);
104 #endif
105 
106 /********* PROTOTYPES **********/
107 
108 static void dumpmemusage(int severity);
109 static void dumpstats(int severity); /* log stats */
110 static void process_signal(int sig);
111 
112 /** Called when we get a SIGHUP: reload configuration files and keys,
113  * retry all connections, and so on. */
114 static int
115 do_hup(void)
116 {
117  const or_options_t *options = get_options();
118 
119  log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
120  "resetting internal state.");
121  if (accounting_is_enabled(options))
123 
126  /* first, reload config variables, in case they've changed */
127  if (options->ReloadTorrcOnSIGHUP) {
128  /* no need to provide argc/v, they've been cached in init_from_config */
129  int init_rv = options_init_from_torrc(0, NULL);
130  if (init_rv < 0) {
131  log_err(LD_CONFIG,"Reading config failed--see warnings above. "
132  "For usage, try -h.");
133  return -1;
134  } else if (BUG(init_rv > 0)) {
135  // LCOV_EXCL_START
136  /* This should be impossible: the only "return 1" cases in
137  * options_init_from_torrc are ones caused by command-line arguments;
138  * but they can't change while Tor is running. */
139  return -1;
140  // LCOV_EXCL_STOP
141  }
142  options = get_options(); /* they have changed now */
143  /* Logs are only truncated the first time they are opened, but were
144  probably intended to be cleaned up on signal. */
145  if (options->TruncateLogFile)
146  truncate_logs();
147  } else {
148  char *msg = NULL;
149  log_notice(LD_GENERAL, "Not reloading config file: the controller told "
150  "us not to.");
151  /* Make stuff get rescanned, reloaded, etc. */
152  if (set_options((or_options_t*)options, &msg) < 0) {
153  if (!msg)
154  msg = tor_strdup("Unknown error");
155  log_warn(LD_GENERAL, "Unable to re-set previous options: %s", msg);
156  tor_free(msg);
157  }
158  }
159  if (authdir_mode(options)) {
160  /* reload the approved-routers file */
161  if (dirserv_load_fingerprint_file() < 0) {
162  /* warnings are logged from dirserv_load_fingerprint_file() directly */
163  log_info(LD_GENERAL, "Error reloading fingerprints. "
164  "Continuing with old list.");
165  }
166  }
167 
168  /* Rotate away from the old dirty circuits. This has to be done
169  * after we've read the new options, but before we start using
170  * circuits for directory fetches. */
172 
173  /* retry appropriate downloads */
176  if (!net_is_disabled())
177  update_networkstatus_downloads(time(NULL));
178 
179  /* We'll retry routerstatus downloads in about 10 seconds; no need to
180  * force a retry there. */
181 
182  if (server_mode(options)) {
183  /* Maybe we've been given a new ed25519 key or certificate?
184  */
185  time_t now = approx_time();
186  int new_signing_key = load_ed_keys(options, now);
187  if (new_signing_key < 0 ||
188  generate_ed_link_cert(options, now, new_signing_key > 0)) {
189  log_warn(LD_OR, "Problem reloading Ed25519 keys; still using old keys.");
190  }
191 
192  /* Update cpuworker and dnsworker processes, so they get up-to-date
193  * configuration options. */
195  dns_reset();
196  }
197  return 0;
198 }
199 
200 /** Libevent callback: invoked when we get a signal.
201  */
202 static void
203 signal_callback(evutil_socket_t fd, short events, void *arg)
204 {
205  const int *sigptr = arg;
206  const int sig = *sigptr;
207  (void)fd;
208  (void)events;
209 
210  update_current_time(time(NULL));
211  process_signal(sig);
212 }
213 
214 /** Do the work of acting on a signal received in <b>sig</b> */
215 static void
217 {
218  switch (sig)
219  {
220  case SIGTERM:
221  log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
223  break;
224  case SIGINT:
225  if (!server_mode(get_options())) { /* do it now */
226  log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
228  return;
229  }
230 #ifdef HAVE_SYSTEMD
231  sd_notify(0, "STOPPING=1");
232 #endif
234  break;
235 #ifdef SIGPIPE
236  case SIGPIPE:
237  log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
238  break;
239 #endif
240  case SIGUSR1:
241  /* prefer to log it at INFO, but make sure we always see it */
244  break;
245  case SIGUSR2:
247  log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
248  "Send HUP to change back.");
250  break;
251  case SIGHUP:
252 #ifdef HAVE_SYSTEMD
253  sd_notify(0, "RELOADING=1");
254 #endif
255  if (do_hup() < 0) {
256  log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
258  return;
259  }
260 #ifdef HAVE_SYSTEMD
261  sd_notify(0, "READY=1");
262 #endif
264  break;
265 #ifdef SIGCHLD
266  case SIGCHLD:
268  break;
269 #endif
270  case SIGNEWNYM: {
271  do_signewnym(time(NULL));
272  break;
273  }
274  case SIGCLEARDNSCACHE:
277  break;
278  case SIGHEARTBEAT:
279  log_heartbeat(time(NULL));
281  break;
282  case SIGACTIVE:
283  /* "SIGACTIVE" counts as ersatz user activity. */
286  break;
287  case SIGDORMANT:
288  /* "SIGDORMANT" means to ignore past user activity */
289  log_notice(LD_GENERAL, "Going dormant because of controller request.");
294  break;
295  }
296 }
297 
298 /**
299  * Write current memory usage information to the log.
300  */
301 static void
302 dumpmemusage(int severity)
303 {
305  tor_log(severity, LD_GENERAL, "In rephist: %"PRIu64" used by %d Tors.",
307  dump_routerlist_mem_usage(severity);
308  dump_cell_pool_usage(severity);
309  dump_dns_mem_usage(severity);
310  tor_log_mallinfo(severity);
311 }
312 
313 /** Write all statistics to the log, with log level <b>severity</b>. Called
314  * in response to a SIGUSR1. */
315 static void
316 dumpstats(int severity)
317 {
318  time_t now = time(NULL);
319  time_t elapsed;
320  size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
321 
322  tor_log(severity, LD_GENERAL, "Dumping stats:");
323 
325  int i = conn_sl_idx;
326  tor_log(severity, LD_GENERAL,
327  "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
328  i, (int)conn->s, conn->type, conn_type_to_string(conn->type),
329  conn->state, conn_state_to_string(conn->type, conn->state),
330  (int)(now - conn->timestamp_created));
331  if (!connection_is_listener(conn)) {
332  tor_log(severity,LD_GENERAL,
333  "Conn %d is to %s:%d.", i,
334  safe_str_client(conn->address),
335  conn->port);
336  tor_log(severity,LD_GENERAL,
337  "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
338  i,
339  (int)connection_get_inbuf_len(conn),
340  (int)buf_allocation(conn->inbuf),
341  (int)(now - conn->timestamp_last_read_allowed));
342  tor_log(severity,LD_GENERAL,
343  "Conn %d: %d bytes waiting on outbuf "
344  "(len %d, last written %d secs ago)",i,
345  (int)connection_get_outbuf_len(conn),
346  (int)buf_allocation(conn->outbuf),
347  (int)(now - conn->timestamp_last_write_allowed));
348  if (conn->type == CONN_TYPE_OR) {
349  or_connection_t *or_conn = TO_OR_CONN(conn);
350  if (or_conn->tls) {
351  if (tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
352  &wbuf_cap, &wbuf_len) == 0) {
353  tor_log(severity, LD_GENERAL,
354  "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
355  "%d/%d bytes used on write buffer.",
356  i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
357  }
358  }
359  }
360  }
361  circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
362  * using this conn */
363  } SMARTLIST_FOREACH_END(conn);
364 
365  channel_dumpstats(severity);
366  channel_listener_dumpstats(severity);
367 
368  tor_log(severity, LD_NET,
369  "Cells processed: %"PRIu64" padding\n"
370  " %"PRIu64" create\n"
371  " %"PRIu64" created\n"
372  " %"PRIu64" relay\n"
373  " (%"PRIu64" relayed)\n"
374  " (%"PRIu64" delivered)\n"
375  " %"PRIu64" destroy",
384  tor_log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
385  100*(((double)stats_n_data_bytes_packaged) /
388  tor_log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
389  100*(((double)stats_n_data_bytes_received) /
391 
392  cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_TAP, "TAP");
393  cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_NTOR,"ntor");
394 
395  if (now - time_of_process_start >= 0)
396  elapsed = now - time_of_process_start;
397  else
398  elapsed = 0;
399 
400  if (elapsed) {
401  tor_log(severity, LD_NET,
402  "Average bandwidth: %"PRIu64"/%d = %d bytes/sec reading",
403  (get_bytes_read()),
404  (int)elapsed,
405  (int) (get_bytes_read()/elapsed));
406  tor_log(severity, LD_NET,
407  "Average bandwidth: %"PRIu64"/%d = %d bytes/sec writing",
408  (get_bytes_written()),
409  (int)elapsed,
410  (int) (get_bytes_written()/elapsed));
411  }
412 
413  tor_log(severity, LD_NET, "--------------- Dumping memory information:");
414  dumpmemusage(severity);
415 
416  rep_hist_dump_stats(now,severity);
417  rend_service_dump_stats(severity);
418 }
419 
420 #ifdef _WIN32
421 #define UNIX_ONLY 0
422 #else
423 #define UNIX_ONLY 1
424 #endif
425 
426 static struct {
427  /** A numeric code for this signal. Must match the signal value if
428  * try_to_register is true. */
430  /** True if we should try to register this signal with libevent and catch
431  * corresponding posix signals. False otherwise. */
433  /** Pointer to hold the event object constructed for this signal. */
434  struct event *signal_event;
435 } signal_handlers[] = {
436 #ifdef SIGINT
437  { SIGINT, UNIX_ONLY, NULL }, /* do a controlled slow shutdown */
438 #endif
439 #ifdef SIGTERM
440  { SIGTERM, UNIX_ONLY, NULL }, /* to terminate now */
441 #endif
442 #ifdef SIGPIPE
443  { SIGPIPE, UNIX_ONLY, NULL }, /* otherwise SIGPIPE kills us */
444 #endif
445 #ifdef SIGUSR1
446  { SIGUSR1, UNIX_ONLY, NULL }, /* dump stats */
447 #endif
448 #ifdef SIGUSR2
449  { SIGUSR2, UNIX_ONLY, NULL }, /* go to loglevel debug */
450 #endif
451 #ifdef SIGHUP
452  { SIGHUP, UNIX_ONLY, NULL }, /* to reload config, retry conns, etc */
453 #endif
454 #ifdef SIGXFSZ
455  { SIGXFSZ, UNIX_ONLY, NULL }, /* handle file-too-big resource exhaustion */
456 #endif
457 #ifdef SIGCHLD
458  { SIGCHLD, UNIX_ONLY, NULL }, /* handle dns/cpu workers that exit */
459 #endif
460  /* These are controller-only */
461  { SIGNEWNYM, 0, NULL },
462  { SIGCLEARDNSCACHE, 0, NULL },
463  { SIGHEARTBEAT, 0, NULL },
464  { SIGACTIVE, 0, NULL },
465  { SIGDORMANT, 0, NULL },
466  { -1, -1, NULL }
467 };
468 
469 /** Set up the signal handler events for this process, and register them
470  * with libevent if appropriate. */
471 void
473 {
474  int i;
475  const int enabled = !get_options()->DisableSignalHandlers;
476 
477  for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
478  /* Signal handlers are only registered with libevent if they need to catch
479  * real POSIX signals. We construct these signal handler events in either
480  * case, though, so that controllers can activate them with the SIGNAL
481  * command.
482  */
483  if (enabled && signal_handlers[i].try_to_register) {
484  signal_handlers[i].signal_event =
485  tor_evsignal_new(tor_libevent_get_base(),
486  signal_handlers[i].signal_value,
488  &signal_handlers[i].signal_value);
489  if (event_add(signal_handlers[i].signal_event, NULL))
490  log_warn(LD_BUG, "Error from libevent when adding "
491  "event for signal %d",
492  signal_handlers[i].signal_value);
493  } else {
494  signal_handlers[i].signal_event =
495  tor_event_new(tor_libevent_get_base(), -1,
496  EV_SIGNAL, signal_callback,
497  &signal_handlers[i].signal_value);
498  }
499  }
500 }
501 
502 /* Cause the signal handler for signal_num to be called in the event loop. */
503 void
504 activate_signal(int signal_num)
505 {
506  int i;
507  for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
508  if (signal_handlers[i].signal_value == signal_num) {
509  event_active(signal_handlers[i].signal_event, EV_SIGNAL, 1);
510  return;
511  }
512  }
513 }
514 
515 /** Main entry point for the Tor command-line client. Return 0 on "success",
516  * negative on "failure", and positive on "success and exit".
517  */
518 int
519 tor_init(int argc, char *argv[])
520 {
521  char progname[256];
523 
524  time_of_process_start = time(NULL);
526  /* Have the log set up with our application name. */
527  tor_snprintf(progname, sizeof(progname), "Tor %s", get_version());
528  log_set_application_name(progname);
529 
530  /* Initialize the history structures. */
531  rep_hist_init();
532  /* Initialize the service cache. */
533  rend_cache_init();
534  addressmap_init(); /* Init the client dns cache. Do it always, since it's
535  * cheap. */
536 
537  /* Initialize the HS subsystem. */
538  hs_init();
539 
540  {
541  /* We check for the "quiet"/"hush" settings first, since they decide
542  whether we log anything at all to stdout. */
543  parsed_cmdline_t *cmdline;
544  cmdline = config_parse_commandline(argc, argv, 1);
545  if (cmdline)
546  quiet = cmdline->quiet_level;
547  parsed_cmdline_free(cmdline);
548  }
549 
550  /* give it somewhere to log to initially */
552  quiet_level = quiet;
553 
554  {
555  const char *version = get_version();
556 
557  log_notice(LD_GENERAL, "Tor %s running on %s with Libevent %s, "
558  "%s %s, Zlib %s, Liblzma %s, and Libzstd %s.", version,
559  get_uname(),
563  tor_compress_supports_method(ZLIB_METHOD) ?
564  tor_compress_version_str(ZLIB_METHOD) : "N/A",
565  tor_compress_supports_method(LZMA_METHOD) ?
566  tor_compress_version_str(LZMA_METHOD) : "N/A",
567  tor_compress_supports_method(ZSTD_METHOD) ?
568  tor_compress_version_str(ZSTD_METHOD) : "N/A");
569 
570  log_notice(LD_GENERAL, "Tor can't help you if you use it wrong! "
571  "Learn how to be safe at "
572  "https://www.torproject.org/download/download#warning");
573 
574  if (strstr(version, "alpha") || strstr(version, "beta"))
575  log_notice(LD_GENERAL, "This version is not a stable Tor release. "
576  "Expect more bugs than usual.");
577 
579  }
580 
581 #ifdef HAVE_RUST
582  rust_log_welcome_string();
583 #endif /* defined(HAVE_RUST) */
584 
585  int init_rv = options_init_from_torrc(argc,argv);
586  if (init_rv < 0) {
587  log_err(LD_CONFIG,"Reading config failed--see warnings above.");
588  return -1;
589  } else if (init_rv > 0) {
590  // We succeeded, and should exit anyway -- probably the user just said
591  // "--version" or something like that.
592  return 1;
593  }
594 
595  /* Initialize channelpadding and circpad parameters to defaults
596  * until we get a consensus */
599 
600  /* Initialize circuit padding to defaults+torrc until we get a consensus */
602 
603  /* Initialize hidden service DoS subsystem. We need to do this once the
604  * configuration object has been set because it can be accessed. */
605  hs_dos_init();
606 
607  /* Initialize predicted ports list after loading options */
608  predicted_ports_init();
609 
610 #ifndef _WIN32
611  if (geteuid()==0)
612  log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
613  "and you probably shouldn't.");
614 #endif
615 
616  /* Scan/clean unparseable descriptors; after reading config */
618 
619  return 0;
620 }
621 
622 /** A lockfile structure, used to prevent two Tors from messing with the
623  * data directory at once. If this variable is non-NULL, we're holding
624  * the lockfile. */
625 static tor_lockfile_t *lockfile = NULL;
626 
627 /** Try to grab the lock file described in <b>options</b>, if we do not
628  * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
629  * holding the lock, and exit if we can't get it after waiting. Otherwise,
630  * return -1 if we can't get the lockfile. Return 0 on success.
631  */
632 int
633 try_locking(const or_options_t *options, int err_if_locked)
634 {
635  if (lockfile)
636  return 0;
637  else {
638  char *fname = options_get_datadir_fname(options, "lock");
639  int already_locked = 0;
640  tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
641  tor_free(fname);
642  if (!lf) {
643  if (err_if_locked && already_locked) {
644  int r;
645  log_warn(LD_GENERAL, "It looks like another Tor process is running "
646  "with the same data directory. Waiting 5 seconds to see "
647  "if it goes away.");
648 #ifndef _WIN32
649  sleep(5);
650 #else
651  Sleep(5000);
652 #endif
653  r = try_locking(options, 0);
654  if (r<0) {
655  log_err(LD_GENERAL, "No, it's still there. Exiting.");
656  return -1;
657  }
658  return r;
659  }
660  return -1;
661  }
662  lockfile = lf;
663  return 0;
664  }
665 }
666 
667 /** Return true iff we've successfully acquired the lock file. */
668 int
670 {
671  return lockfile != NULL;
672 }
673 
674 /** If we have successfully acquired the lock file, release it. */
675 void
677 {
678  if (lockfile) {
680  lockfile = NULL;
681  }
682 }
683 
684 /**
685  * Remove the specified file, and log a warning if the operation fails for
686  * any reason other than the file not existing. Ignores NULL filenames.
687  */
688 void
689 tor_remove_file(const char *filename)
690 {
691  if (filename && tor_unlink(filename) != 0 && errno != ENOENT) {
692  log_warn(LD_FS, "Couldn't unlink %s: %s",
693  filename, strerror(errno));
694  }
695 }
696 
697 /** Read/create keys as needed, and echo our fingerprint to stdout. */
698 static int
700 {
701  char buf[FINGERPRINT_LEN+1];
702  crypto_pk_t *k;
703  const char *nickname = get_options()->Nickname;
704  sandbox_disable_getaddrinfo_cache();
705  if (!server_mode(get_options())) {
706  log_err(LD_GENERAL,
707  "Clients don't have long-term identity keys. Exiting.");
708  return -1;
709  }
710  tor_assert(nickname);
711  if (init_keys() < 0) {
712  log_err(LD_GENERAL,"Error initializing keys; exiting.");
713  return -1;
714  }
715  if (!(k = get_server_identity_key())) {
716  log_err(LD_GENERAL,"Error: missing identity key.");
717  return -1;
718  }
719  if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
720  log_err(LD_BUG, "Error computing fingerprint");
721  return -1;
722  }
723  printf("%s %s\n", nickname, buf);
724  return 0;
725 }
726 
727 /** Entry point for password hashing: take the desired password from
728  * the command line, and print its salted hash to stdout. **/
729 static void
731 {
732 
733  char output[256];
735 
737  key[S2K_RFC2440_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
739  get_options()->command_arg, strlen(get_options()->command_arg),
740  key);
741  base16_encode(output, sizeof(output), key, sizeof(key));
742  printf("16:%s\n",output);
743 }
744 
745 /** Entry point for configuration dumping: write the configuration to
746  * stdout. */
747 static int
749 {
750  const or_options_t *options = get_options();
751  const char *arg = options->command_arg;
752  int how;
753  char *opts;
754 
755  if (!strcmp(arg, "short")) {
756  how = OPTIONS_DUMP_MINIMAL;
757  } else if (!strcmp(arg, "non-builtin")) {
758  how = OPTIONS_DUMP_DEFAULTS;
759  } else if (!strcmp(arg, "full")) {
760  how = OPTIONS_DUMP_ALL;
761  } else {
762  fprintf(stderr, "No valid argument to --dump-config found!\n");
763  fprintf(stderr, "Please select 'short', 'non-builtin', or 'full'.\n");
764 
765  return -1;
766  }
767 
768  opts = options_dump(options, how);
769  printf("%s", opts);
770  tor_free(opts);
771 
772  return 0;
773 }
774 
775 static void
776 init_addrinfo(void)
777 {
778  if (! server_mode(get_options()) ||
779  (get_options()->Address && strlen(get_options()->Address) > 0)) {
780  /* We don't need to seed our own hostname, because we won't be calling
781  * resolve_my_address on it.
782  */
783  return;
784  }
785  char hname[256];
786 
787  // host name to sandbox
788  gethostname(hname, sizeof(hname));
789  tor_add_addrinfo(hname);
790 }
791 
792 static sandbox_cfg_t*
793 sandbox_init_filter(void)
794 {
795  const or_options_t *options = get_options();
797  int i;
798 
800  get_cachedir_fname("cached-status"));
801 
802 #define OPEN(name) \
803  sandbox_cfg_allow_open_filename(&cfg, tor_strdup(name))
804 
805 #define OPEN_DATADIR(name) \
806  sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname(name))
807 
808 #define OPEN_DATADIR2(name, name2) \
809  sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname2((name), (name2)))
810 
811 #define OPEN_DATADIR_SUFFIX(name, suffix) do { \
812  OPEN_DATADIR(name); \
813  OPEN_DATADIR(name suffix); \
814  } while (0)
815 
816 #define OPEN_DATADIR2_SUFFIX(name, name2, suffix) do { \
817  OPEN_DATADIR2(name, name2); \
818  OPEN_DATADIR2(name, name2 suffix); \
819  } while (0)
820 
821 #define OPEN_KEY_DIRECTORY() \
822  sandbox_cfg_allow_open_filename(&cfg, tor_strdup(options->KeyDirectory))
823 #define OPEN_CACHEDIR(name) \
824  sandbox_cfg_allow_open_filename(&cfg, get_cachedir_fname(name))
825 #define OPEN_CACHEDIR_SUFFIX(name, suffix) do { \
826  OPEN_CACHEDIR(name); \
827  OPEN_CACHEDIR(name suffix); \
828  } while (0)
829 #define OPEN_KEYDIR(name) \
830  sandbox_cfg_allow_open_filename(&cfg, get_keydir_fname(name))
831 #define OPEN_KEYDIR_SUFFIX(name, suffix) do { \
832  OPEN_KEYDIR(name); \
833  OPEN_KEYDIR(name suffix); \
834  } while (0)
835 
836  OPEN(options->DataDirectory);
837  OPEN_KEY_DIRECTORY();
838 
839  OPEN_CACHEDIR_SUFFIX("cached-certs", ".tmp");
840  OPEN_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
841  OPEN_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
842  OPEN_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
843  OPEN_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
844  OPEN_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
845  OPEN_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
846  OPEN_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
847  OPEN_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
848  OPEN_CACHEDIR("cached-descriptors.tmp.tmp");
849  OPEN_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
850  OPEN_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
851  OPEN_CACHEDIR("cached-extrainfo.tmp.tmp");
852 
853  OPEN_DATADIR_SUFFIX("state", ".tmp");
854  OPEN_DATADIR_SUFFIX("sr-state", ".tmp");
855  OPEN_DATADIR_SUFFIX("unparseable-desc", ".tmp");
856  OPEN_DATADIR_SUFFIX("v3-status-votes", ".tmp");
857  OPEN_DATADIR("key-pinning-journal");
858  OPEN("/dev/srandom");
859  OPEN("/dev/urandom");
860  OPEN("/dev/random");
861  OPEN("/etc/hosts");
862  OPEN("/proc/meminfo");
863 
864  if (options->BridgeAuthoritativeDir)
865  OPEN_DATADIR_SUFFIX("networkstatus-bridges", ".tmp");
866 
867  if (authdir_mode(options))
868  OPEN_DATADIR("approved-routers");
869 
870  if (options->ServerDNSResolvConfFile)
872  tor_strdup(options->ServerDNSResolvConfFile));
873  else
874  sandbox_cfg_allow_open_filename(&cfg, tor_strdup("/etc/resolv.conf"));
875 
876  for (i = 0; i < 2; ++i) {
877  if (get_torrc_fname(i)) {
878  sandbox_cfg_allow_open_filename(&cfg, tor_strdup(get_torrc_fname(i)));
879  }
880  }
881 
882  SMARTLIST_FOREACH(options->FilesOpenedByIncludes, char *, f, {
883  OPEN(f);
884  });
885 
886 #define RENAME_SUFFIX(name, suffix) \
887  sandbox_cfg_allow_rename(&cfg, \
888  get_datadir_fname(name suffix), \
889  get_datadir_fname(name))
890 
891 #define RENAME_SUFFIX2(prefix, name, suffix) \
892  sandbox_cfg_allow_rename(&cfg, \
893  get_datadir_fname2(prefix, name suffix), \
894  get_datadir_fname2(prefix, name))
895 
896 #define RENAME_CACHEDIR_SUFFIX(name, suffix) \
897  sandbox_cfg_allow_rename(&cfg, \
898  get_cachedir_fname(name suffix), \
899  get_cachedir_fname(name))
900 
901 #define RENAME_KEYDIR_SUFFIX(name, suffix) \
902  sandbox_cfg_allow_rename(&cfg, \
903  get_keydir_fname(name suffix), \
904  get_keydir_fname(name))
905 
906  RENAME_CACHEDIR_SUFFIX("cached-certs", ".tmp");
907  RENAME_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
908  RENAME_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
909  RENAME_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
910  RENAME_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
911  RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
912  RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".new");
913  RENAME_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
914  RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
915  RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".new");
916  RENAME_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
917  RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
918  RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".new");
919  RENAME_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
920 
921  RENAME_SUFFIX("state", ".tmp");
922  RENAME_SUFFIX("sr-state", ".tmp");
923  RENAME_SUFFIX("unparseable-desc", ".tmp");
924  RENAME_SUFFIX("v3-status-votes", ".tmp");
925 
926  if (options->BridgeAuthoritativeDir)
927  RENAME_SUFFIX("networkstatus-bridges", ".tmp");
928 
929 #define STAT_DATADIR(name) \
930  sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname(name))
931 
932 #define STAT_CACHEDIR(name) \
933  sandbox_cfg_allow_stat_filename(&cfg, get_cachedir_fname(name))
934 
935 #define STAT_DATADIR2(name, name2) \
936  sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname2((name), (name2)))
937 
938 #define STAT_KEY_DIRECTORY() \
939  sandbox_cfg_allow_stat_filename(&cfg, tor_strdup(options->KeyDirectory))
940 
941  STAT_DATADIR(NULL);
942  STAT_DATADIR("lock");
943  STAT_DATADIR("state");
944  STAT_DATADIR("router-stability");
945 
946  STAT_CACHEDIR("cached-extrainfo.new");
947 
948  {
949  smartlist_t *files = smartlist_new();
951  SMARTLIST_FOREACH(files, char *, file_name, {
952  /* steals reference */
953  sandbox_cfg_allow_open_filename(&cfg, file_name);
954  });
955  smartlist_free(files);
956  }
957 
958  {
959  smartlist_t *files = smartlist_new();
960  smartlist_t *dirs = smartlist_new();
962  SMARTLIST_FOREACH(files, char *, file_name, {
963  char *tmp_name = NULL;
964  tor_asprintf(&tmp_name, "%s.tmp", file_name);
965  sandbox_cfg_allow_rename(&cfg,
966  tor_strdup(tmp_name), tor_strdup(file_name));
967  /* steals references */
968  sandbox_cfg_allow_open_filename(&cfg, file_name);
969  sandbox_cfg_allow_open_filename(&cfg, tmp_name);
970  });
971  SMARTLIST_FOREACH(dirs, char *, dir, {
972  /* steals reference */
974  });
975  smartlist_free(files);
976  smartlist_free(dirs);
977  }
978 
979  {
980  char *fname;
981  if ((fname = get_controller_cookie_file_name())) {
982  sandbox_cfg_allow_open_filename(&cfg, fname);
983  }
984  if ((fname = get_ext_or_auth_cookie_file_name())) {
985  sandbox_cfg_allow_open_filename(&cfg, fname);
986  }
987  }
988 
990  if (!port->is_unix_addr)
991  continue;
992  /* When we open an AF_UNIX address, we want permission to open the
993  * directory that holds it. */
994  char *dirname = tor_strdup(port->unix_addr);
995  if (get_parent_directory(dirname) == 0) {
996  OPEN(dirname);
997  }
998  tor_free(dirname);
999  sandbox_cfg_allow_chmod_filename(&cfg, tor_strdup(port->unix_addr));
1000  sandbox_cfg_allow_chown_filename(&cfg, tor_strdup(port->unix_addr));
1001  } SMARTLIST_FOREACH_END(port);
1002 
1003  if (options->DirPortFrontPage) {
1005  tor_strdup(options->DirPortFrontPage));
1006  }
1007 
1008  // orport
1009  if (server_mode(get_options())) {
1010 
1011  OPEN_KEYDIR_SUFFIX("secret_id_key", ".tmp");
1012  OPEN_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
1013  OPEN_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
1014  OPEN_KEYDIR("secret_id_key.old");
1015  OPEN_KEYDIR("secret_onion_key.old");
1016  OPEN_KEYDIR("secret_onion_key_ntor.old");
1017 
1018  OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
1019  OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
1020  OPEN_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
1021  OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
1022  OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key_encrypted", ".tmp");
1023  OPEN_KEYDIR_SUFFIX("ed25519_signing_public_key", ".tmp");
1024  OPEN_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
1025 
1026  OPEN_DATADIR2_SUFFIX("stats", "bridge-stats", ".tmp");
1027  OPEN_DATADIR2_SUFFIX("stats", "dirreq-stats", ".tmp");
1028 
1029  OPEN_DATADIR2_SUFFIX("stats", "entry-stats", ".tmp");
1030  OPEN_DATADIR2_SUFFIX("stats", "exit-stats", ".tmp");
1031  OPEN_DATADIR2_SUFFIX("stats", "buffer-stats", ".tmp");
1032  OPEN_DATADIR2_SUFFIX("stats", "conn-stats", ".tmp");
1033  OPEN_DATADIR2_SUFFIX("stats", "hidserv-stats", ".tmp");
1034 
1035  OPEN_DATADIR("approved-routers");
1036  OPEN_DATADIR_SUFFIX("fingerprint", ".tmp");
1037  OPEN_DATADIR_SUFFIX("hashed-fingerprint", ".tmp");
1038  OPEN_DATADIR_SUFFIX("router-stability", ".tmp");
1039 
1040  OPEN("/etc/resolv.conf");
1041 
1042  RENAME_SUFFIX("fingerprint", ".tmp");
1043  RENAME_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
1044 
1045  RENAME_KEYDIR_SUFFIX("secret_id_key", ".tmp");
1046  RENAME_KEYDIR_SUFFIX("secret_id_key.old", ".tmp");
1047  RENAME_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
1048  RENAME_KEYDIR_SUFFIX("secret_onion_key.old", ".tmp");
1049 
1050  RENAME_SUFFIX2("stats", "bridge-stats", ".tmp");
1051  RENAME_SUFFIX2("stats", "dirreq-stats", ".tmp");
1052  RENAME_SUFFIX2("stats", "entry-stats", ".tmp");
1053  RENAME_SUFFIX2("stats", "exit-stats", ".tmp");
1054  RENAME_SUFFIX2("stats", "buffer-stats", ".tmp");
1055  RENAME_SUFFIX2("stats", "conn-stats", ".tmp");
1056  RENAME_SUFFIX2("stats", "hidserv-stats", ".tmp");
1057  RENAME_SUFFIX("hashed-fingerprint", ".tmp");
1058  RENAME_SUFFIX("router-stability", ".tmp");
1059 
1060  RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
1061  RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
1062  RENAME_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
1063  RENAME_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
1064  RENAME_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
1065 
1066  sandbox_cfg_allow_rename(&cfg,
1067  get_keydir_fname("secret_onion_key"),
1068  get_keydir_fname("secret_onion_key.old"));
1069  sandbox_cfg_allow_rename(&cfg,
1070  get_keydir_fname("secret_onion_key_ntor"),
1071  get_keydir_fname("secret_onion_key_ntor.old"));
1072 
1073  STAT_KEY_DIRECTORY();
1074  OPEN_DATADIR("stats");
1075  STAT_DATADIR("stats");
1076  STAT_DATADIR2("stats", "dirreq-stats");
1077 
1079  }
1080 
1081  init_addrinfo();
1082 
1083  return cfg;
1084 }
1085 
1086 int
1087 run_tor_main_loop(void)
1088 {
1089  handle_signals();
1092 
1093  /* load the private keys, if we're supposed to have them, and set up the
1094  * TLS context. */
1095  if (! client_identity_key_is_set()) {
1096  if (init_keys() < 0) {
1097  log_err(LD_OR, "Error initializing keys; exiting");
1098  return -1;
1099  }
1100  }
1101 
1102  /* Set up our buckets */
1104 
1105  /* initialize the bootstrap status events to know we're starting up */
1106  control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
1107 
1108  /* Initialize the keypinning log. */
1109  if (authdir_mode_v3(get_options())) {
1110  char *fname = get_datadir_fname("key-pinning-journal");
1111  int r = 0;
1112  if (keypin_load_journal(fname)<0) {
1113  log_err(LD_DIR, "Error loading key-pinning journal: %s",strerror(errno));
1114  r = -1;
1115  }
1116  if (keypin_open_journal(fname)<0) {
1117  log_err(LD_DIR, "Error opening key-pinning journal: %s",strerror(errno));
1118  r = -1;
1119  }
1120  tor_free(fname);
1121  if (r)
1122  return r;
1123  }
1124  {
1125  /* This is the old name for key-pinning-journal. These got corrupted
1126  * in a couple of cases by #16530, so we started over. See #16580 for
1127  * the rationale and for other options we didn't take. We can remove
1128  * this code once all the authorities that ran 0.2.7.1-alpha-dev are
1129  * upgraded.
1130  */
1131  char *fname = get_datadir_fname("key-pinning-entries");
1132  unlink(fname);
1133  tor_free(fname);
1134  }
1135 
1136  if (trusted_dirs_reload_certs()) {
1137  log_warn(LD_DIR,
1138  "Couldn't load all cached v3 certificates. Starting anyway.");
1139  }
1141  return -1;
1142  }
1143  /* load the routers file, or assign the defaults. */
1144  if (router_reload_router_list()) {
1145  return -1;
1146  }
1147  /* load the networkstatuses. (This launches a download for new routers as
1148  * appropriate.)
1149  */
1150  const time_t now = time(NULL);
1151  directory_info_has_arrived(now, 1, 0);
1152 
1154  /* launch cpuworkers. Need to do this *after* we've read the onion key. */
1155  cpu_init();
1156  }
1158 
1159  /* Setup shared random protocol subsystem. */
1160  if (authdir_mode_v3(get_options())) {
1161  if (sr_init(1) < 0) {
1162  return -1;
1163  }
1164  }
1165 
1166  /* initialize dns resolve map, spawn workers if needed */
1167  if (dns_init() < 0) {
1168  if (get_options()->ServerDNSAllowBrokenConfig)
1169  log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
1170  "Network not up yet? Will try again soon.");
1171  else {
1172  log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
1173  "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
1174  }
1175  }
1176 
1177 #ifdef HAVE_SYSTEMD
1178  {
1179  const int r = sd_notify(0, "READY=1");
1180  if (r < 0) {
1181  log_warn(LD_GENERAL, "Unable to send readiness to systemd: %s",
1182  strerror(r));
1183  } else if (r > 0) {
1184  log_notice(LD_GENERAL, "Signaled readiness to systemd");
1185  } else {
1186  log_info(LD_GENERAL, "Systemd NOTIFY_SOCKET not present.");
1187  }
1188  }
1189 #endif /* defined(HAVE_SYSTEMD) */
1190 
1191  return do_main_loop();
1192 }
1193 
1194 /** Install the publish/subscribe relationships for all the subsystems. */
1195 static void
1197 {
1198  pubsub_builder_t *builder = pubsub_builder_new();
1199  int r = subsystems_add_pubsub(builder);
1200  tor_assert(r == 0);
1201  r = tor_mainloop_connect_pubsub(builder); // consumes builder
1202  tor_assert(r == 0);
1203 }
1204 
1205 /** Connect the mainloop to its publish/subscribe message delivery events if
1206  * appropriate, and configure the global channels appropriately. */
1207 static void
1209 {
1210  if (get_options()->command == CMD_RUN_TOR) {
1212  /* XXXX For each pubsub channel, its delivery strategy should be set at
1213  * this XXXX point, using tor_mainloop_set_delivery_strategy().
1214  */
1215  tor_mainloop_set_delivery_strategy("orconn", DELIV_IMMEDIATE);
1216  tor_mainloop_set_delivery_strategy("ocirc", DELIV_IMMEDIATE);
1217  }
1218 }
1219 
1220 /* Main entry point for the Tor process. Called from tor_main(), and by
1221  * anybody embedding Tor. */
1222 int
1224 {
1225  int result = 0;
1226 
1227 #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
1228  event_set_mem_functions(tor_malloc_, tor_realloc_, tor_free_);
1229 #endif
1230 
1231  subsystems_init();
1232 
1234 
1235  int argc = tor_cfg->argc + tor_cfg->argc_owned;
1236  char **argv = tor_calloc(argc, sizeof(char*));
1237  memcpy(argv, tor_cfg->argv, tor_cfg->argc*sizeof(char*));
1238  if (tor_cfg->argc_owned)
1239  memcpy(argv + tor_cfg->argc, tor_cfg->argv_owned,
1240  tor_cfg->argc_owned*sizeof(char*));
1241 
1242 #ifdef NT_SERVICE
1243  {
1244  int done = 0;
1245  result = nt_service_parse_options(argc, argv, &done);
1246  if (done) {
1247  goto done;
1248  }
1249  }
1250 #endif /* defined(NT_SERVICE) */
1251 
1252  pubsub_install();
1253 
1254  {
1255  int init_rv = tor_init(argc, argv);
1256  if (init_rv) {
1257  tor_free_all(0);
1258  result = (init_rv < 0) ? -1 : 0;
1259  goto done;
1260  }
1261  }
1262 
1263  pubsub_connect();
1264 
1265  if (get_options()->Sandbox && get_options()->command == CMD_RUN_TOR) {
1266  sandbox_cfg_t* cfg = sandbox_init_filter();
1267 
1268  if (sandbox_init(cfg)) {
1269  tor_free(argv);
1270  log_err(LD_BUG,"Failed to create syscall sandbox filter");
1271  tor_free_all(0);
1272  return -1;
1273  }
1274  tor_make_getaddrinfo_cache_active();
1275 
1276  // registering libevent rng
1277 #ifdef HAVE_EVUTIL_SECURE_RNG_SET_URANDOM_DEVICE_FILE
1278  evutil_secure_rng_set_urandom_device_file(
1279  (char*) sandbox_intern_string("/dev/urandom"));
1280 #endif
1281  }
1282 
1283  switch (get_options()->command) {
1284  case CMD_RUN_TOR:
1285 #ifdef NT_SERVICE
1286  nt_service_set_state(SERVICE_RUNNING);
1287 #endif
1288  result = run_tor_main_loop();
1289  break;
1290  case CMD_KEYGEN:
1291  result = load_ed_keys(get_options(), time(NULL)) < 0;
1292  break;
1293  case CMD_KEY_EXPIRATION:
1294  init_keys();
1295  result = log_cert_expiration();
1296  break;
1297  case CMD_LIST_FINGERPRINT:
1298  result = do_list_fingerprint();
1299  break;
1300  case CMD_HASH_PASSWORD:
1301  do_hash_password();
1302  result = 0;
1303  break;
1304  case CMD_VERIFY_CONFIG:
1305  if (quiet_level == QUIET_NONE)
1306  printf("Configuration was valid\n");
1307  result = 0;
1308  break;
1309  case CMD_DUMP_CONFIG:
1310  result = do_dump_config();
1311  break;
1312  case CMD_RUN_UNITTESTS: /* only set by test.c */
1313  case CMD_IMMEDIATE: /* Handled in config.c */
1314  default:
1315  log_warn(LD_BUG,"Illegal command number %d: internal error.",
1316  get_options()->command);
1317  result = -1;
1318  }
1319  tor_cleanup();
1320  done:
1321  tor_free(argv);
1322  return result;
1323 }
void timers_initialize(void)
Definition: timers.c:205
#define RELAY_PAYLOAD_SIZE
Definition: or.h:605
Header file containing denial of service defenses for the HS subsystem for all versions.
uint64_t stats_n_data_cells_received
Definition: relay.c:2048
smartlist_t * get_connection_array(void)
Definition: mainloop.c:452
void router_reset_descriptor_download_failures(void)
Definition: routerlist.c:2829
Header for statefile.c.
char * DirPortFrontPage
Declare the quiet_level enumeration and global.
time_t time_of_process_start
Definition: mainloop.c:143
uint64_t rephist_total_alloc
Definition: rephist.c:104
void crypto_rand(char *to, size_t n)
Definition: crypto_rand.c:477
Header for uname.c.
Header for confline.c.
struct sandbox_cfg_elem sandbox_cfg_t
Definition: sandbox.h:35
void add_default_log_for_quiet_level(quiet_level_t quiet)
Definition: quiet_level.c:24
tor_lockfile_t * tor_lockfile_lock(const char *filename, int blocking, int *locked_out)
Definition: lockfile.c:63
Common functions for using (pseudo-)random number generators.
void init_protocol_warning_severity_level(void)
Definition: config.c:1189
int have_lockfile(void)
Definition: main.c:669
Header file for control_auth.c.
void notify_pending_waitpid_callbacks(void)
Definition: waitpid.c:141
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
uint64_t stats_n_relay_cells_relayed
Definition: relay.c:125
Header file for command.c.
Header for addressmap.c.
Headers for waitpid.c.
void update_current_time(time_t now)
Definition: mainloop.c:2195
void tor_cleanup(void)
Definition: shutdown.c:60
int options_init_from_torrc(int argc, char **argv)
Definition: config.c:4327
struct pubsub_builder_t pubsub_builder_t
Definition: pubsub_build.h:28
static void signal_callback(evutil_socket_t fd, short events, void *arg)
Definition: main.c:203
void hs_init(void)
Definition: hs_common.c:1794
void dump_routerlist_mem_usage(int severity)
Definition: routerlist.c:974
Header file for connection.c.
int subsystems_init(void)
Definition: subsysmgr.c:114
#define LD_GENERAL
Definition: log.h:62
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
Headers for compress.c.
#define LOG_INFO
Definition: log.h:45
void channel_dumpstats(int severity)
Definition: channel.c:2068
int tor_compress_supports_method(compress_method_t method)
Definition: compress.c:304
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:628
int dns_init(void)
Definition: dns.c:224
Header file for shutdown.c.
Header file for ntmain.c.
int connection_is_listener(connection_t *conn)
Definition: connection.c:4629
int accounting_is_enabled(const or_options_t *options)
Definition: hibernate.c:305
int tor_tls_get_buffer_sizes(tor_tls_t *tls, size_t *rbuf_capacity, size_t *rbuf_bytes, size_t *wbuf_capacity, size_t *wbuf_bytes)
Definition: tortls_nss.c:674
int try_to_register
Definition: main.c:432
int get_min_log_level(void)
Definition: log.c:1517
OR connection structure.
void tor_log_get_logfile_names(smartlist_t *out)
Definition: log.c:741
Header for mainloop_pubsub.c.
Header file for config.c.
Header file for authcert.c.
#define CONN_TYPE_OR
Definition: connection.h:24
const char * conn_type_to_string(int type)
Definition: connection.c:243
const or_options_t * get_options(void)
Definition: config.c:941
#define tor_assert(expr)
Definition: util_bug.h:102
struct tor_tls_t * tls
void tor_mainloop_connect_pubsub_events(void)
int sandbox_init(sandbox_cfg_t *cfg)
Definition: sandbox.c:1737
Header file for cpuworker.c.
Header for keypin.c.
int control_event_signal(uintptr_t signal_num)
uint64_t get_bytes_written(void)
Definition: mainloop.c:474
static int do_list_fingerprint(void)
Definition: main.c:699
#define tor_free(p)
Definition: malloc.h:52
int do_main_loop(void)
Definition: mainloop.c:2340
void tor_shutdown_event_loop_and_exit(int exitcode)
Definition: mainloop.c:765
void initialize_mainloop_events(void)
Definition: mainloop.c:2326
void circpad_new_consensus_params(const networkstatus_t *ns)
struct event * signal_event
Definition: main.c:434
uint32_t rephist_total_num
Definition: rephist.c:106
parsed_cmdline_t * config_parse_commandline(int argc, char **argv, int ignore_errors)
Definition: config.c:2283
const char * conn_state_to_string(int type, int state)
Definition: connection.c:275
Header file for mainloop.c.
void channelpadding_new_consensus_params(networkstatus_t *ns)
smartlist_t * smartlist_new(void)
static int do_dump_config(void)
Definition: main.c:748
char * options_dump(const or_options_t *options, int how_to_dump)
Definition: config.c:2818
int sandbox_cfg_allow_open_filename(sandbox_cfg_t **cfg, char *file)
Definition: sandbox.c:1761
Header for meminfo.c.
static void do_hash_password(void)
Definition: main.c:730
pubsub_builder_t * pubsub_builder_new(void)
Definition: pubsub_build.c:56
void set_network_participation(bool participation)
Definition: netstatus.c:101
int dir_server_mode(const or_options_t *options)
Definition: routermode.c:23
Header for ext_orport.c.
#define sandbox_intern_string(s)
Definition: sandbox.h:112
char * get_controller_cookie_file_name(void)
Definition: control_auth.c:48
static tor_lockfile_t * lockfile
Definition: main.c:625
quiet_level_t quiet_level
Definition: quiet_level.c:20
Header file for main.c.
int keypin_load_journal(const char *fname)
Definition: keypin.c:452
void connection_dump_buffer_mem_stats(int severity)
Definition: connection.c:5208
or_connection_t * TO_OR_CONN(connection_t *c)
quiet_level_t quiet_level
Definition: config.h:207
void tor_lockfile_unlock(tor_lockfile_t *lockfile)
Definition: lockfile.c:121
Header file for directory authority mode.
void circpad_machines_init(void)
int try_locking(const or_options_t *options, int err_if_locked)
Definition: main.c:633
void control_event_bootstrap(bootstrap_status_t status, int progress)
Header file for hibernate.c.
uint64_t stats_n_data_bytes_received
Definition: relay.c:2052
void handle_signals(void)
Definition: main.c:472
int dirserv_load_fingerprint_file(void)
void router_reset_warnings(void)
Definition: router.c:3445
uint64_t stats_n_relay_cells_processed
Definition: command.c:71
int client_identity_key_is_set(void)
Definition: router.c:422
Header file for channel.c.
const char * crypto_get_library_version_string(void)
Definition: crypto_init.c:191
void directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
Definition: mainloop.c:1121
uint64_t stats_n_created_cells_processed
Definition: command.c:69
int init_keys(void)
Definition: router.c:920
void routerlist_reset_warnings(void)
Definition: routerlist.c:1443
const char * get_uname(void)
Definition: uname.c:33
void log_set_application_name(const char *name)
Definition: log.c:244
const smartlist_t * get_configured_ports(void)
Definition: config.c:6403
int trusted_dirs_reload_certs(void)
Definition: authcert.c:324
void addressmap_init(void)
Definition: addressmap.c:91
void tor_compress_log_init_warnings(void)
Definition: compress.c:682
int ReloadTorrcOnSIGHUP
Header file for routermode.c.
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
uint64_t stats_n_data_bytes_packaged
Definition: relay.c:2046
void secret_to_key_rfc2440(char *key_out, size_t key_out_len, const char *secret, size_t secret_len, const char *s2k_specifier)
Definition: crypto_s2k.c:203
void cpu_init(void)
Definition: cpuworker.c:83
Header file for circuitpadding.c.
#define DIGEST_LEN
Definition: digest_sizes.h:20
Header for version.c.
int load_ed_keys(const or_options_t *options, time_t now)
Definition: routerkeys.c:55
Header file for sandbox.c.
Header file for rendcache.c.
int log_heartbeat(time_t now)
Definition: status.c:119
int net_is_disabled(void)
Definition: netstatus.c:25
Public C API for the Tor network service.
Master header file for Tor-specific functionality.
int dns_reset(void)
Definition: dns.c:238
void rep_hist_dump_stats(time_t now, int severity)
Definition: rephist.c:554
void hs_dos_init(void)
Definition: hs_dos.c:218
uint64_t stats_n_padding_cells_processed
Definition: channeltls.c:78
uint64_t stats_n_relay_cells_delivered
Definition: relay.c:129
int keypin_open_journal(const char *fname)
Definition: keypin.c:305
void release_lockfile(void)
Definition: main.c:676
Header for crypto_s2k.c.
static void dumpmemusage(int severity)
Definition: main.c:302
int DisableSignalHandlers
void routerparse_init(void)
Definition: routerparse.c:1228
Header file for rephist.c.
Header for routerkeys.c.
const char * tor_libevent_get_version_str(void)
struct event_base * tor_libevent_get_base(void)
Header for netstatus.c.
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
int router_reload_consensus_networkstatus(void)
int quiet
Definition: config.c:2217
void tor_free_(void *mem)
Definition: malloc.c:227
void rep_hist_init(void)
Definition: rephist.c:232
void do_signewnym(time_t now)
Definition: mainloop.c:1322
Header file for circuitlist.c.
int tor_run_main(const tor_main_configuration_t *tor_cfg)
Definition: main.c:1223
Header file for rendservice.c.
Header file for process_descs.c.
uint64_t get_bytes_read(void)
Definition: mainloop.c:464
void schedule_rescan_periodic_events(void)
Definition: mainloop.c:1578
void cpuworkers_rotate_keyinfo(void)
Definition: cpuworker.c:188
void truncate_logs(void)
Definition: log.c:1547
#define LD_OR
Definition: log.h:92
void hibernate_begin_shutdown(void)
Definition: hibernate.c:927
static void process_signal(int sig)
Definition: main.c:216
void circuit_dump_by_conn(connection_t *conn, int severity)
Definition: circuitlist.c:1384
void hs_service_lists_fnames_for_sandbox(smartlist_t *file_list, smartlist_t *dir_list)
Definition: hs_service.c:3827
static void dumpstats(int severity)
Definition: main.c:316
void reset_user_activity(time_t now)
Definition: netstatus.c:82
#define LD_FS
Definition: log.h:70
#define LD_DIR
Definition: log.h:88
static void pubsub_install(void)
Definition: main.c:1196
int generate_ed_link_cert(const or_options_t *options, time_t now, int force)
Definition: routerkeys.c:365
void router_reset_status_download_failures(void)
Definition: dirlist.c:103
int tor_mainloop_set_delivery_strategy(const char *msg_channel_name, deliv_strategy_t strategy)
char * Nickname
Definition: or_options_st.h:65
int sandbox_cfg_allow_stat_filename(sandbox_cfg_t **cfg, char *file)
Definition: sandbox.c:1775
uint64_t stats_n_data_cells_packaged
Definition: relay.c:2042
void note_user_activity(time_t now)
Definition: netstatus.c:63
struct smartlist_t * FilesOpenedByIncludes
int crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out, int add_space)
Definition: crypto_rsa.c:229
Header for status.c.
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
int tor_init(int argc, char *argv[])
Definition: main.c:519
size_t buf_allocation(const buf_t *buf)
Definition: buffers.c:401
void channel_listener_dumpstats(int severity)
Definition: channel.c:2099
#define S2K_RFC2440_SPECIFIER_LEN
Definition: crypto_s2k.h:21
void circuit_mark_all_dirty_circs_as_unusable(void)
Definition: circuitlist.c:2112
Header for timers.c.
int BridgeAuthoritativeDir
Header file for relay.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
#define FINGERPRINT_LEN
Definition: crypto_rsa.h:34
uint64_t stats_n_create_cells_processed
Definition: command.c:67
void * tor_malloc_(size_t size)
Definition: malloc.c:32
uint64_t stats_n_destroy_cells_processed
Definition: command.c:73
void consdiffmgr_enable_background_compression(void)
Definition: consdiffmgr.c:1908
void update_networkstatus_downloads(time_t now)
quiet_level_t
Definition: quiet_level.h:16
void switch_logs_debug(void)
Definition: log.c:1532
int set_options(or_options_t *new_val, char **msg)
Definition: config.c:977
int subsystems_add_pubsub(pubsub_builder_t *builder)
Definition: subsysmgr.c:195
void cpuworker_log_onionskin_overhead(int severity, int onionskin_type, const char *onionskin_type_name)
Definition: cpuworker.c:292
void dump_dns_mem_usage(int severity)
Definition: dns.c:2152
Headers for tortls.c.
Header file for control.c.
Header for resolve.c.
void rend_cache_init(void)
Definition: rendcache.c:68
time_t approx_time(void)
Definition: approx_time.c:32
int tor_unlink(const char *pathname)
Definition: files.c:154
int router_reload_router_list(void)
Definition: routerlist.c:457
tor_cmdline_mode_t command
Definition: config.c:2215
void tor_log_mallinfo(int severity)
Definition: meminfo.c:44
int tor_mainloop_connect_pubsub(struct pubsub_builder_t *builder)
static void pubsub_connect(void)
Definition: main.c:1208
char * get_ext_or_auth_cookie_file_name(void)
Definition: ext_orport.c:127
void * tor_realloc_(void *ptr, size_t size)
Definition: malloc.c:118
Header file for buffers.c.
void connection_bucket_init(void)
Definition: connection.c:3422
int accounting_record_bandwidth_usage(time_t now, or_state_t *state)
Definition: hibernate.c:705
Header file for connection_or.c.
char * ServerDNSResolvConfFile
char * DataDirectory
Definition: or_options_st.h:52
void rend_service_dump_stats(int severity)
Definition: rendservice.c:4370
Header for subsysmgr.c.
#define LD_NET
Definition: log.h:66
int signal_value
Definition: main.c:429
const char * get_torrc_fname(int defaults_fname)
Definition: config.c:4637
or_state_t * get_or_state(void)
Definition: statefile.c:198
const char * get_version(void)
Definition: version.c:38
void addressmap_clear_transient(void)
Definition: addressmap.c:312
char * command_arg
Definition: or_options_st.h:36
int log_cert_expiration(void)
Definition: routerkeys.c:593
Header for compat_libevent.c.
int server_mode(const or_options_t *options)
Definition: routermode.c:34
Header file for control_events.c.
void tor_init_connection_lists(void)
Definition: mainloop.c:413
Header used for constructing the OO publish-subscribe facility.
Header for consdiffmgr.c.
const char * tor_compress_version_str(compress_method_t method)
Definition: compress.c:417
void tor_remove_file(const char *filename)
Definition: main.c:689
This file contains ABI/API of the shared random protocol defined in proposal #250....
int sr_init(int save_to_disk)
int get_parent_directory(char *fname)
Definition: path.c:176
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:86
Header file for dns.c.
Headers for crypto_init.c.
Header file for predict_ports.c.
#define LD_CONFIG
Definition: log.h:68
void tor_free_all(int postfork)
Definition: shutdown.c:110
Header file for routerlist.c.
void dump_cell_pool_usage(int severity)
Definition: relay.c:2521
Header file for routerparse.c.
static int do_hup(void)
Definition: main.c:115
int sandbox_cfg_allow_openat_filename(sandbox_cfg_t **cfg, char *file)
Definition: sandbox.c:1768
sandbox_cfg_t * sandbox_cfg_new(void)
Definition: sandbox.c:1731
Listener port configuration structure.
const char * crypto_get_library_name(void)
Definition: crypto_init.c:178
Internal declarations for in-process Tor API.
int consdiffmgr_register_with_sandbox(struct sandbox_cfg_elem **cfg)
Definition: consdiffmgr.c:847
Header for lockfile.c.