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