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-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief Invocation module. Initializes subsystems and runs the main loop.
12 #include "core/or/or.h"
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"
21 #include "core/mainloop/connection.h"
22 #include "core/mainloop/cpuworker.h"
23 #include "core/mainloop/mainloop.h"
24 #include "core/mainloop/mainloop_pubsub.h"
25 #include "core/mainloop/netstatus.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"
35 #include "feature/api/tor_api_internal.h"
36 #include "feature/client/addressmap.h"
37 #include "feature/control/control.h"
38 #include "feature/control/control_auth.h"
39 #include "feature/control/control_events.h"
40 #include "feature/dirauth/keypin.h"
41 #include "feature/dirauth/process_descs.h"
42 #include "feature/dircache/consdiffmgr.h"
43 #include "feature/dirparse/routerparse.h"
44 #include "feature/hibernate/hibernate.h"
45 #include "feature/hs/hs_dos.h"
46 #include "feature/nodelist/authcert.h"
47 #include "feature/nodelist/networkstatus.h"
48 #include "feature/nodelist/routerlist.h"
49 #include "feature/relay/dns.h"
50 #include "feature/relay/ext_orport.h"
51 #include "feature/relay/routerkeys.h"
52 #include "feature/relay/routermode.h"
53 #include "feature/rend/rendcache.h"
54 #include "feature/rend/rendservice.h"
55 #include "feature/stats/predict_ports.h"
56 #include "feature/stats/rephist.h"
57 #include "lib/compress/compress.h"
58 #include "lib/buf/buffers.h"
59 #include "lib/crypt_ops/crypto_rand.h"
60 #include "lib/crypt_ops/crypto_s2k.h"
61 #include "lib/net/resolve.h"
63 #include "lib/process/waitpid.h"
64 #include "lib/pubsub/pubsub_build.h"
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/tls/tortls.h"
71 #include "lib/evloop/compat_libevent.h"
72 #include "lib/encoding/confline.h"
73 #include "lib/evloop/timers.h"
74 #include "lib/crypt_ops/crypto_init.h"
75 #include "lib/version/torversion.h"
77 #include <event2/event.h>
79 #include "feature/dirauth/authmode.h"
80 #include "feature/dirauth/shared_random.h"
82 #include "core/or/or_connection_st.h"
83 #include "core/or/port_cfg_st.h"
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.
94 # define __INCLUDE_LEVEL__ 2
95 #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
96 #include <systemd/sd-daemon.h>
97 #endif /* defined(HAVE_SYSTEMD) */
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);
105 /********* PROTOTYPES **********/
107 static void dumpmemusage(int severity
);
108 static void dumpstats(int severity
); /* log stats */
109 static void process_signal(int sig
);
111 /** Called when we get a SIGHUP: reload configuration files and keys,
112 * retry all connections, and so on. */
116 const or_options_t
*options
= get_options();
118 log_notice(LD_GENERAL
,"Received reload signal (hup). Reloading config and "
119 "resetting internal state.");
120 if (accounting_is_enabled(options
))
121 accounting_record_bandwidth_usage(time(NULL
), get_or_state());
123 router_reset_warnings();
124 routerlist_reset_warnings();
125 /* first, reload config variables, in case they've changed */
126 if (options
->ReloadTorrcOnSIGHUP
) {
127 /* no need to provide argc/v, they've been cached in init_from_config */
128 int init_rv
= options_init_from_torrc(0, NULL
);
130 log_err(LD_CONFIG
,"Reading config failed--see warnings above. "
131 "For usage, try -h.");
133 } else if (BUG(init_rv
> 0)) {
135 /* This should be impossible: the only "return 1" cases in
136 * options_init_from_torrc are ones caused by command-line arguments;
137 * but they can't change while Tor is running. */
141 options
= get_options(); /* they have changed now */
142 /* Logs are only truncated the first time they are opened, but were
143 probably intended to be cleaned up on signal. */
144 if (options
->TruncateLogFile
)
148 log_notice(LD_GENERAL
, "Not reloading config file: the controller told "
150 /* Make stuff get rescanned, reloaded, etc. */
151 if (set_options((or_options_t
*)options
, &msg
) < 0) {
153 msg
= tor_strdup("Unknown error");
154 log_warn(LD_GENERAL
, "Unable to re-set previous options: %s", msg
);
158 if (authdir_mode(options
)) {
159 /* reload the approved-routers file */
160 if (dirserv_load_fingerprint_file() < 0) {
161 /* warnings are logged from dirserv_load_fingerprint_file() directly */
162 log_info(LD_GENERAL
, "Error reloading fingerprints. "
163 "Continuing with old list.");
167 /* Rotate away from the old dirty circuits. This has to be done
168 * after we've read the new options, but before we start using
169 * circuits for directory fetches. */
170 circuit_mark_all_dirty_circs_as_unusable();
172 /* retry appropriate downloads */
173 router_reset_status_download_failures();
174 router_reset_descriptor_download_failures();
175 if (!net_is_disabled())
176 update_networkstatus_downloads(time(NULL
));
178 /* We'll retry routerstatus downloads in about 10 seconds; no need to
179 * force a retry there. */
181 if (server_mode(options
)) {
182 /* Maybe we've been given a new ed25519 key or certificate?
184 time_t now
= approx_time();
185 int new_signing_key
= load_ed_keys(options
, now
);
186 if (new_signing_key
< 0 ||
187 generate_ed_link_cert(options
, now
, new_signing_key
> 0)) {
188 log_warn(LD_OR
, "Problem reloading Ed25519 keys; still using old keys.");
191 /* Update cpuworker and dnsworker processes, so they get up-to-date
192 * configuration options. */
193 cpuworkers_rotate_keyinfo();
199 /** Libevent callback: invoked when we get a signal.
202 signal_callback(evutil_socket_t fd
, short events
, void *arg
)
204 const int *sigptr
= arg
;
205 const int sig
= *sigptr
;
209 update_current_time(time(NULL
));
213 /** Do the work of acting on a signal received in <b>sig</b> */
215 process_signal(int sig
)
220 log_notice(LD_GENERAL
,"Catching signal TERM, exiting cleanly.");
221 tor_shutdown_event_loop_and_exit(0);
224 if (!server_mode(get_options())) { /* do it now */
225 log_notice(LD_GENERAL
,"Interrupt: exiting cleanly.");
226 tor_shutdown_event_loop_and_exit(0);
230 sd_notify(0, "STOPPING=1");
232 hibernate_begin_shutdown();
236 log_debug(LD_GENERAL
,"Caught SIGPIPE. Ignoring.");
240 /* prefer to log it at INFO, but make sure we always see it */
241 dumpstats(get_min_log_level()<LOG_INFO
? get_min_log_level() : LOG_INFO
);
242 control_event_signal(sig
);
246 log_debug(LD_GENERAL
,"Caught USR2, going to loglevel debug. "
247 "Send HUP to change back.");
248 control_event_signal(sig
);
252 sd_notify(0, "RELOADING=1");
255 log_warn(LD_CONFIG
,"Restart failed (config error?). Exiting.");
256 tor_shutdown_event_loop_and_exit(1);
260 sd_notify(0, "READY=1");
262 control_event_signal(sig
);
266 notify_pending_waitpid_callbacks();
270 do_signewnym(time(NULL
));
273 case SIGCLEARDNSCACHE
:
274 addressmap_clear_transient();
275 control_event_signal(sig
);
278 log_heartbeat(time(NULL
));
279 control_event_signal(sig
);
282 /* "SIGACTIVE" counts as ersatz user activity. */
283 note_user_activity(approx_time());
284 control_event_signal(sig
);
287 /* "SIGDORMANT" means to ignore past user activity */
288 log_notice(LD_GENERAL
, "Going dormant because of controller request.");
289 reset_user_activity(0);
290 set_network_participation(false);
291 schedule_rescan_periodic_events();
292 control_event_signal(sig
);
298 /** Activate SIGINT on reciving a control signal in console */
300 process_win32_console_ctrl(DWORD ctrl_type
)
302 /* Ignore type of the ctrl signal */
305 activate_signal(SIGINT
);
311 * Write current memory usage information to the log.
314 dumpmemusage(int severity
)
316 connection_dump_buffer_mem_stats(severity
);
317 tor_log(severity
, LD_GENERAL
, "In rephist: %"PRIu64
" used by %d Tors.",
318 (rephist_total_alloc
), rephist_total_num
);
319 dump_routerlist_mem_usage(severity
);
320 dump_cell_pool_usage(severity
);
321 dump_dns_mem_usage(severity
);
322 tor_log_mallinfo(severity
);
325 /** Write all statistics to the log, with log level <b>severity</b>. Called
326 * in response to a SIGUSR1. */
328 dumpstats(int severity
)
330 time_t now
= time(NULL
);
332 size_t rbuf_cap
, wbuf_cap
, rbuf_len
, wbuf_len
;
334 tor_log(severity
, LD_GENERAL
, "Dumping stats:");
336 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t
*, conn
) {
338 tor_log(severity
, LD_GENERAL
,
339 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
340 i
, (int)conn
->s
, conn
->type
, conn_type_to_string(conn
->type
),
341 conn
->state
, conn_state_to_string(conn
->type
, conn
->state
),
342 (int)(now
- conn
->timestamp_created
));
343 if (!connection_is_listener(conn
)) {
344 tor_log(severity
,LD_GENERAL
,
345 "Conn %d is to %s:%d.", i
,
346 safe_str_client(conn
->address
),
348 tor_log(severity
,LD_GENERAL
,
349 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
351 (int)connection_get_inbuf_len(conn
),
352 (int)buf_allocation(conn
->inbuf
),
353 (int)(now
- conn
->timestamp_last_read_allowed
));
354 tor_log(severity
,LD_GENERAL
,
355 "Conn %d: %d bytes waiting on outbuf "
356 "(len %d, last written %d secs ago)",i
,
357 (int)connection_get_outbuf_len(conn
),
358 (int)buf_allocation(conn
->outbuf
),
359 (int)(now
- conn
->timestamp_last_write_allowed
));
360 if (conn
->type
== CONN_TYPE_OR
) {
361 or_connection_t
*or_conn
= TO_OR_CONN(conn
);
363 if (tor_tls_get_buffer_sizes(or_conn
->tls
, &rbuf_cap
, &rbuf_len
,
364 &wbuf_cap
, &wbuf_len
) == 0) {
365 tor_log(severity
, LD_GENERAL
,
366 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
367 "%d/%d bytes used on write buffer.",
368 i
, (int)rbuf_len
, (int)rbuf_cap
, (int)wbuf_len
, (int)wbuf_cap
);
373 circuit_dump_by_conn(conn
, severity
); /* dump info about all the circuits
375 } SMARTLIST_FOREACH_END(conn
);
377 channel_dumpstats(severity
);
378 channel_listener_dumpstats(severity
);
380 tor_log(severity
, LD_NET
,
381 "Cells processed: %"PRIu64
" padding\n"
382 " %"PRIu64
" create\n"
383 " %"PRIu64
" created\n"
385 " (%"PRIu64
" relayed)\n"
386 " (%"PRIu64
" delivered)\n"
387 " %"PRIu64
" destroy",
388 (stats_n_padding_cells_processed
),
389 (stats_n_create_cells_processed
),
390 (stats_n_created_cells_processed
),
391 (stats_n_relay_cells_processed
),
392 (stats_n_relay_cells_relayed
),
393 (stats_n_relay_cells_delivered
),
394 (stats_n_destroy_cells_processed
));
395 if (stats_n_data_cells_packaged
)
396 tor_log(severity
,LD_NET
,"Average packaged cell fullness: %2.3f%%",
397 100*(((double)stats_n_data_bytes_packaged
) /
398 ((double)stats_n_data_cells_packaged
*RELAY_PAYLOAD_SIZE
)) );
399 if (stats_n_data_cells_received
)
400 tor_log(severity
,LD_NET
,"Average delivered cell fullness: %2.3f%%",
401 100*(((double)stats_n_data_bytes_received
) /
402 ((double)stats_n_data_cells_received
*RELAY_PAYLOAD_SIZE
)) );
404 cpuworker_log_onionskin_overhead(severity
, ONION_HANDSHAKE_TYPE_TAP
, "TAP");
405 cpuworker_log_onionskin_overhead(severity
, ONION_HANDSHAKE_TYPE_NTOR
,"ntor");
407 if (now
- time_of_process_start
>= 0)
408 elapsed
= now
- time_of_process_start
;
413 tor_log(severity
, LD_NET
,
414 "Average bandwidth: %"PRIu64
"/%d = %d bytes/sec reading",
417 (int) (get_bytes_read()/elapsed
));
418 tor_log(severity
, LD_NET
,
419 "Average bandwidth: %"PRIu64
"/%d = %d bytes/sec writing",
420 (get_bytes_written()),
422 (int) (get_bytes_written()/elapsed
));
425 tor_log(severity
, LD_NET
, "--------------- Dumping memory information:");
426 dumpmemusage(severity
);
428 rep_hist_dump_stats(now
,severity
);
429 rend_service_dump_stats(severity
);
430 hs_service_dump_stats(severity
);
440 /** A numeric code for this signal. Must match the signal value if
441 * try_to_register is true. */
443 /** True if we should try to register this signal with libevent and catch
444 * corresponding posix signals. False otherwise. */
446 /** Pointer to hold the event object constructed for this signal. */
447 struct event
*signal_event
;
448 } signal_handlers
[] = {
450 { SIGINT
, UNIX_ONLY
, NULL
}, /* do a controlled slow shutdown */
453 { SIGTERM
, UNIX_ONLY
, NULL
}, /* to terminate now */
456 { SIGPIPE
, UNIX_ONLY
, NULL
}, /* otherwise SIGPIPE kills us */
459 { SIGUSR1
, UNIX_ONLY
, NULL
}, /* dump stats */
462 { SIGUSR2
, UNIX_ONLY
, NULL
}, /* go to loglevel debug */
465 { SIGHUP
, UNIX_ONLY
, NULL
}, /* to reload config, retry conns, etc */
468 { SIGXFSZ
, UNIX_ONLY
, NULL
}, /* handle file-too-big resource exhaustion */
471 { SIGCHLD
, UNIX_ONLY
, NULL
}, /* handle dns/cpu workers that exit */
473 /* These are controller-only */
474 { SIGNEWNYM
, 0, NULL
},
475 { SIGCLEARDNSCACHE
, 0, NULL
},
476 { SIGHEARTBEAT
, 0, NULL
},
477 { SIGACTIVE
, 0, NULL
},
478 { SIGDORMANT
, 0, NULL
},
482 /** Set up the signal handler events for this process, and register them
483 * with libevent if appropriate. */
488 const int enabled
= !get_options()->DisableSignalHandlers
;
490 for (i
= 0; signal_handlers
[i
].signal_value
>= 0; ++i
) {
491 /* Signal handlers are only registered with libevent if they need to catch
492 * real POSIX signals. We construct these signal handler events in either
493 * case, though, so that controllers can activate them with the SIGNAL
496 if (enabled
&& signal_handlers
[i
].try_to_register
) {
497 signal_handlers
[i
].signal_event
=
498 tor_evsignal_new(tor_libevent_get_base(),
499 signal_handlers
[i
].signal_value
,
501 &signal_handlers
[i
].signal_value
);
502 if (event_add(signal_handlers
[i
].signal_event
, NULL
))
503 log_warn(LD_BUG
, "Error from libevent when adding "
504 "event for signal %d",
505 signal_handlers
[i
].signal_value
);
507 signal_handlers
[i
].signal_event
=
508 tor_event_new(tor_libevent_get_base(), -1,
509 EV_SIGNAL
, signal_callback
,
510 &signal_handlers
[i
].signal_value
);
515 /* Windows lacks traditional POSIX signals but WinAPI provides a function
516 * to handle control signals like Ctrl+C in the console, we can use this to
517 * simulate the SIGINT signal */
518 if (enabled
) SetConsoleCtrlHandler(process_win32_console_ctrl
, TRUE
);
522 /* Cause the signal handler for signal_num to be called in the event loop. */
524 activate_signal(int signal_num
)
527 for (i
= 0; signal_handlers
[i
].signal_value
>= 0; ++i
) {
528 if (signal_handlers
[i
].signal_value
== signal_num
) {
529 event_active(signal_handlers
[i
].signal_event
, EV_SIGNAL
, 1);
535 /** Main entry point for the Tor command-line client. Return 0 on "success",
536 * negative on "failure", and positive on "success and exit".
539 tor_init(int argc
, char *argv
[])
542 quiet_level_t quiet
= QUIET_NONE
;
544 time_of_process_start
= time(NULL
);
545 tor_init_connection_lists();
546 /* Have the log set up with our application name. */
547 tor_snprintf(progname
, sizeof(progname
), "Tor %s", get_version());
548 log_set_application_name(progname
);
550 /* Initialize the history structures. */
552 /* Initialize the service cache. */
554 addressmap_init(); /* Init the client dns cache. Do it always, since it's
557 /* Initialize the HS subsystem. */
561 /* We check for the "quiet"/"hush" settings first, since they decide
562 whether we log anything at all to stdout. */
563 parsed_cmdline_t
*cmdline
;
564 cmdline
= config_parse_commandline(argc
, argv
, 1);
566 quiet
= cmdline
->quiet_level
;
567 parsed_cmdline_free(cmdline
);
570 /* give it somewhere to log to initially */
571 add_default_log_for_quiet_level(quiet
);
575 const char *version
= get_version();
577 log_notice(LD_GENERAL
, "Tor %s running on %s with Libevent %s, "
578 "%s %s, Zlib %s, Liblzma %s, and Libzstd %s.", version
,
580 tor_libevent_get_version_str(),
581 crypto_get_library_name(),
582 crypto_get_library_version_string(),
583 tor_compress_supports_method(ZLIB_METHOD
) ?
584 tor_compress_version_str(ZLIB_METHOD
) : "N/A",
585 tor_compress_supports_method(LZMA_METHOD
) ?
586 tor_compress_version_str(LZMA_METHOD
) : "N/A",
587 tor_compress_supports_method(ZSTD_METHOD
) ?
588 tor_compress_version_str(ZSTD_METHOD
) : "N/A");
590 log_notice(LD_GENERAL
, "Tor can't help you if you use it wrong! "
591 "Learn how to be safe at "
592 "https://www.torproject.org/download/download#warning");
594 if (strstr(version
, "alpha") || strstr(version
, "beta"))
595 log_notice(LD_GENERAL
, "This version is not a stable Tor release. "
596 "Expect more bugs than usual.");
598 tor_compress_log_init_warnings();
602 rust_log_welcome_string();
603 #endif /* defined(HAVE_RUST) */
605 int init_rv
= options_init_from_torrc(argc
,argv
);
607 log_err(LD_CONFIG
,"Reading config failed--see warnings above.");
609 } else if (init_rv
> 0) {
610 // We succeeded, and should exit anyway -- probably the user just said
611 // "--version" or something like that.
615 /* Initialize channelpadding and circpad parameters to defaults
616 * until we get a consensus */
617 channelpadding_new_consensus_params(NULL
);
618 circpad_new_consensus_params(NULL
);
620 /* Initialize circuit padding to defaults+torrc until we get a consensus */
621 circpad_machines_init();
623 /* Initialize hidden service DoS subsystem. We need to do this once the
624 * configuration object has been set because it can be accessed. */
627 /* Initialize predicted ports list after loading options */
628 predicted_ports_init();
632 log_warn(LD_GENERAL
,"You are running Tor as root. You don't need to, "
633 "and you probably shouldn't.");
636 /* Scan/clean unparseable descriptors; after reading config */
642 /** A lockfile structure, used to prevent two Tors from messing with the
643 * data directory at once. If this variable is non-NULL, we're holding
645 static tor_lockfile_t
*lockfile
= NULL
;
647 /** Try to grab the lock file described in <b>options</b>, if we do not
648 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
649 * holding the lock, and exit if we can't get it after waiting. Otherwise,
650 * return -1 if we can't get the lockfile. Return 0 on success.
653 try_locking(const or_options_t
*options
, int err_if_locked
)
658 char *fname
= options_get_datadir_fname(options
, "lock");
659 int already_locked
= 0;
660 tor_lockfile_t
*lf
= tor_lockfile_lock(fname
, 0, &already_locked
);
663 if (err_if_locked
&& already_locked
) {
665 log_warn(LD_GENERAL
, "It looks like another Tor process is running "
666 "with the same data directory. Waiting 5 seconds to see "
673 r
= try_locking(options
, 0);
675 log_err(LD_GENERAL
, "No, it's still there. Exiting.");
687 /** Return true iff we've successfully acquired the lock file. */
691 return lockfile
!= NULL
;
694 /** If we have successfully acquired the lock file, release it. */
696 release_lockfile(void)
699 tor_lockfile_unlock(lockfile
);
705 * Remove the specified file, and log a warning if the operation fails for
706 * any reason other than the file not existing. Ignores NULL filenames.
709 tor_remove_file(const char *filename
)
711 if (filename
&& tor_unlink(filename
) != 0 && errno
!= ENOENT
) {
712 log_warn(LD_FS
, "Couldn't unlink %s: %s",
713 filename
, strerror(errno
));
717 /** Read/create keys as needed, and echo our fingerprint to stdout. */
719 do_list_fingerprint(void)
721 char buf
[FINGERPRINT_LEN
+1];
723 const char *nickname
= get_options()->Nickname
;
724 sandbox_disable_getaddrinfo_cache();
725 if (!server_mode(get_options())) {
727 "Clients don't have long-term identity keys. Exiting.");
730 tor_assert(nickname
);
731 if (init_keys() < 0) {
732 log_err(LD_GENERAL
,"Error initializing keys; exiting.");
735 if (!(k
= get_server_identity_key())) {
736 log_err(LD_GENERAL
,"Error: missing identity key.");
739 if (crypto_pk_get_fingerprint(k
, buf
, 1)<0) {
740 log_err(LD_BUG
, "Error computing fingerprint");
743 printf("%s %s\n", nickname
, buf
);
747 /** Entry point for password hashing: take the desired password from
748 * the command line, and print its salted hash to stdout. **/
750 do_hash_password(void)
754 char key
[S2K_RFC2440_SPECIFIER_LEN
+DIGEST_LEN
];
756 crypto_rand(key
, S2K_RFC2440_SPECIFIER_LEN
-1);
757 key
[S2K_RFC2440_SPECIFIER_LEN
-1] = (uint8_t)96; /* Hash 64 K of data. */
758 secret_to_key_rfc2440(key
+S2K_RFC2440_SPECIFIER_LEN
, DIGEST_LEN
,
759 get_options()->command_arg
, strlen(get_options()->command_arg
),
761 base16_encode(output
, sizeof(output
), key
, sizeof(key
));
762 printf("16:%s\n",output
);
765 /** Entry point for configuration dumping: write the configuration to
770 const or_options_t
*options
= get_options();
771 const char *arg
= options
->command_arg
;
775 if (!strcmp(arg
, "short")) {
776 how
= OPTIONS_DUMP_MINIMAL
;
777 } else if (!strcmp(arg
, "non-builtin")) {
778 how
= OPTIONS_DUMP_DEFAULTS
;
779 } else if (!strcmp(arg
, "full")) {
780 how
= OPTIONS_DUMP_ALL
;
782 fprintf(stderr
, "No valid argument to --dump-config found!\n");
783 fprintf(stderr
, "Please select 'short', 'non-builtin', or 'full'.\n");
788 opts
= options_dump(options
, how
);
798 if (! server_mode(get_options()) ||
799 (get_options()->Address
&& strlen(get_options()->Address
) > 0)) {
800 /* We don't need to seed our own hostname, because we won't be calling
801 * resolve_my_address on it.
807 // host name to sandbox
808 gethostname(hname
, sizeof(hname
));
809 tor_add_addrinfo(hname
);
812 static sandbox_cfg_t
*
813 sandbox_init_filter(void)
815 const or_options_t
*options
= get_options();
816 sandbox_cfg_t
*cfg
= sandbox_cfg_new();
819 sandbox_cfg_allow_openat_filename(&cfg
,
820 get_cachedir_fname("cached-status"));
823 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(name))
825 #define OPENDIR(dir) \
826 sandbox_cfg_allow_opendir_dirname(&cfg, tor_strdup(dir))
828 #define OPEN_DATADIR(name) \
829 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname(name))
831 #define OPEN_DATADIR2(name, name2) \
832 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname2((name), (name2)))
834 #define OPEN_DATADIR_SUFFIX(name, suffix) do { \
835 OPEN_DATADIR(name); \
836 OPEN_DATADIR(name suffix); \
839 #define OPEN_DATADIR2_SUFFIX(name, name2, suffix) do { \
840 OPEN_DATADIR2(name, name2); \
841 OPEN_DATADIR2(name, name2 suffix); \
844 // KeyDirectory is a directory, but it is only opened in check_private_dir
845 // which calls open instead of opendir
846 #define OPEN_KEY_DIRECTORY() \
847 OPEN(options->KeyDirectory)
848 #define OPEN_CACHEDIR(name) \
849 sandbox_cfg_allow_open_filename(&cfg, get_cachedir_fname(name))
850 #define OPEN_CACHEDIR_SUFFIX(name, suffix) do { \
851 OPEN_CACHEDIR(name); \
852 OPEN_CACHEDIR(name suffix); \
854 #define OPEN_KEYDIR(name) \
855 sandbox_cfg_allow_open_filename(&cfg, get_keydir_fname(name))
856 #define OPEN_KEYDIR_SUFFIX(name, suffix) do { \
858 OPEN_KEYDIR(name suffix); \
861 // DataDirectory is a directory, but it is only opened in check_private_dir
862 // which calls open instead of opendir
863 OPEN(options
->DataDirectory
);
864 OPEN_KEY_DIRECTORY();
866 OPEN_CACHEDIR_SUFFIX("cached-certs", ".tmp");
867 OPEN_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
868 OPEN_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
869 OPEN_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
870 OPEN_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
871 OPEN_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
872 OPEN_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
873 OPEN_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
874 OPEN_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
875 OPEN_CACHEDIR("cached-descriptors.tmp.tmp");
876 OPEN_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
877 OPEN_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
878 OPEN_CACHEDIR("cached-extrainfo.tmp.tmp");
880 OPEN_DATADIR_SUFFIX("state", ".tmp");
881 OPEN_DATADIR_SUFFIX("sr-state", ".tmp");
882 OPEN_DATADIR_SUFFIX("unparseable-desc", ".tmp");
883 OPEN_DATADIR_SUFFIX("v3-status-votes", ".tmp");
884 OPEN_DATADIR("key-pinning-journal");
885 OPEN("/dev/srandom");
886 OPEN("/dev/urandom");
889 OPEN("/proc/meminfo");
891 if (options
->BridgeAuthoritativeDir
)
892 OPEN_DATADIR_SUFFIX("networkstatus-bridges", ".tmp");
894 if (authdir_mode(options
))
895 OPEN_DATADIR("approved-routers");
897 if (options
->ServerDNSResolvConfFile
)
898 sandbox_cfg_allow_open_filename(&cfg
,
899 tor_strdup(options
->ServerDNSResolvConfFile
));
901 sandbox_cfg_allow_open_filename(&cfg
, tor_strdup("/etc/resolv.conf"));
903 for (i
= 0; i
< 2; ++i
) {
904 if (get_torrc_fname(i
)) {
905 sandbox_cfg_allow_open_filename(&cfg
, tor_strdup(get_torrc_fname(i
)));
909 SMARTLIST_FOREACH(options
->FilesOpenedByIncludes
, char *, f
, {
910 if (file_status(f
) == FN_DIR
) {
917 #define RENAME_SUFFIX(name, suffix) \
918 sandbox_cfg_allow_rename(&cfg, \
919 get_datadir_fname(name suffix), \
920 get_datadir_fname(name))
922 #define RENAME_SUFFIX2(prefix, name, suffix) \
923 sandbox_cfg_allow_rename(&cfg, \
924 get_datadir_fname2(prefix, name suffix), \
925 get_datadir_fname2(prefix, name))
927 #define RENAME_CACHEDIR_SUFFIX(name, suffix) \
928 sandbox_cfg_allow_rename(&cfg, \
929 get_cachedir_fname(name suffix), \
930 get_cachedir_fname(name))
932 #define RENAME_KEYDIR_SUFFIX(name, suffix) \
933 sandbox_cfg_allow_rename(&cfg, \
934 get_keydir_fname(name suffix), \
935 get_keydir_fname(name))
937 RENAME_CACHEDIR_SUFFIX("cached-certs", ".tmp");
938 RENAME_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
939 RENAME_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
940 RENAME_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
941 RENAME_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
942 RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
943 RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".new");
944 RENAME_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
945 RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
946 RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".new");
947 RENAME_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
948 RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
949 RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".new");
950 RENAME_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
952 RENAME_SUFFIX("state", ".tmp");
953 RENAME_SUFFIX("sr-state", ".tmp");
954 RENAME_SUFFIX("unparseable-desc", ".tmp");
955 RENAME_SUFFIX("v3-status-votes", ".tmp");
957 if (options
->BridgeAuthoritativeDir
)
958 RENAME_SUFFIX("networkstatus-bridges", ".tmp");
960 #define STAT_DATADIR(name) \
961 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname(name))
963 #define STAT_CACHEDIR(name) \
964 sandbox_cfg_allow_stat_filename(&cfg, get_cachedir_fname(name))
966 #define STAT_DATADIR2(name, name2) \
967 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname2((name), (name2)))
969 #define STAT_KEY_DIRECTORY() \
970 sandbox_cfg_allow_stat_filename(&cfg, tor_strdup(options->KeyDirectory))
973 STAT_DATADIR("lock");
974 STAT_DATADIR("state");
975 STAT_DATADIR("router-stability");
977 STAT_CACHEDIR("cached-extrainfo.new");
980 smartlist_t
*files
= smartlist_new();
981 tor_log_get_logfile_names(files
);
982 SMARTLIST_FOREACH(files
, char *, file_name
, {
983 /* steals reference */
984 sandbox_cfg_allow_open_filename(&cfg
, file_name
);
986 smartlist_free(files
);
990 smartlist_t
*files
= smartlist_new();
991 smartlist_t
*dirs
= smartlist_new();
992 hs_service_lists_fnames_for_sandbox(files
, dirs
);
993 SMARTLIST_FOREACH(files
, char *, file_name
, {
994 char *tmp_name
= NULL
;
995 tor_asprintf(&tmp_name
, "%s.tmp", file_name
);
996 sandbox_cfg_allow_rename(&cfg
,
997 tor_strdup(tmp_name
), tor_strdup(file_name
));
998 /* steals references */
999 sandbox_cfg_allow_open_filename(&cfg
, file_name
);
1000 sandbox_cfg_allow_open_filename(&cfg
, tmp_name
);
1002 SMARTLIST_FOREACH(dirs
, char *, dir
, {
1003 /* steals reference */
1004 sandbox_cfg_allow_stat_filename(&cfg
, dir
);
1006 smartlist_free(files
);
1007 smartlist_free(dirs
);
1012 if ((fname
= get_controller_cookie_file_name())) {
1013 sandbox_cfg_allow_open_filename(&cfg
, fname
);
1015 if ((fname
= get_ext_or_auth_cookie_file_name())) {
1016 sandbox_cfg_allow_open_filename(&cfg
, fname
);
1020 SMARTLIST_FOREACH_BEGIN(get_configured_ports(), port_cfg_t
*, port
) {
1021 if (!port
->is_unix_addr
)
1023 /* When we open an AF_UNIX address, we want permission to open the
1024 * directory that holds it. */
1025 char *dirname
= tor_strdup(port
->unix_addr
);
1026 if (get_parent_directory(dirname
) == 0) {
1030 sandbox_cfg_allow_chmod_filename(&cfg
, tor_strdup(port
->unix_addr
));
1031 sandbox_cfg_allow_chown_filename(&cfg
, tor_strdup(port
->unix_addr
));
1032 } SMARTLIST_FOREACH_END(port
);
1034 if (options
->DirPortFrontPage
) {
1035 sandbox_cfg_allow_open_filename(&cfg
,
1036 tor_strdup(options
->DirPortFrontPage
));
1040 if (server_mode(get_options())) {
1042 OPEN_KEYDIR_SUFFIX("secret_id_key", ".tmp");
1043 OPEN_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
1044 OPEN_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
1045 OPEN_KEYDIR("secret_id_key.old");
1046 OPEN_KEYDIR("secret_onion_key.old");
1047 OPEN_KEYDIR("secret_onion_key_ntor.old");
1049 OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
1050 OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
1051 OPEN_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
1052 OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
1053 OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key_encrypted", ".tmp");
1054 OPEN_KEYDIR_SUFFIX("ed25519_signing_public_key", ".tmp");
1055 OPEN_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
1057 OPEN_DATADIR2_SUFFIX("stats", "bridge-stats", ".tmp");
1058 OPEN_DATADIR2_SUFFIX("stats", "dirreq-stats", ".tmp");
1060 OPEN_DATADIR2_SUFFIX("stats", "entry-stats", ".tmp");
1061 OPEN_DATADIR2_SUFFIX("stats", "exit-stats", ".tmp");
1062 OPEN_DATADIR2_SUFFIX("stats", "buffer-stats", ".tmp");
1063 OPEN_DATADIR2_SUFFIX("stats", "conn-stats", ".tmp");
1064 OPEN_DATADIR2_SUFFIX("stats", "hidserv-stats", ".tmp");
1066 OPEN_DATADIR("approved-routers");
1067 OPEN_DATADIR_SUFFIX("fingerprint", ".tmp");
1068 OPEN_DATADIR_SUFFIX("hashed-fingerprint", ".tmp");
1069 OPEN_DATADIR_SUFFIX("router-stability", ".tmp");
1071 OPEN("/etc/resolv.conf");
1073 RENAME_SUFFIX("fingerprint", ".tmp");
1074 RENAME_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
1076 RENAME_KEYDIR_SUFFIX("secret_id_key", ".tmp");
1077 RENAME_KEYDIR_SUFFIX("secret_id_key.old", ".tmp");
1078 RENAME_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
1079 RENAME_KEYDIR_SUFFIX("secret_onion_key.old", ".tmp");
1081 RENAME_SUFFIX2("stats", "bridge-stats", ".tmp");
1082 RENAME_SUFFIX2("stats", "dirreq-stats", ".tmp");
1083 RENAME_SUFFIX2("stats", "entry-stats", ".tmp");
1084 RENAME_SUFFIX2("stats", "exit-stats", ".tmp");
1085 RENAME_SUFFIX2("stats", "buffer-stats", ".tmp");
1086 RENAME_SUFFIX2("stats", "conn-stats", ".tmp");
1087 RENAME_SUFFIX2("stats", "hidserv-stats", ".tmp");
1088 RENAME_SUFFIX("hashed-fingerprint", ".tmp");
1089 RENAME_SUFFIX("router-stability", ".tmp");
1091 RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
1092 RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
1093 RENAME_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
1094 RENAME_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
1095 RENAME_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
1097 sandbox_cfg_allow_rename(&cfg
,
1098 get_keydir_fname("secret_onion_key"),
1099 get_keydir_fname("secret_onion_key.old"));
1100 sandbox_cfg_allow_rename(&cfg
,
1101 get_keydir_fname("secret_onion_key_ntor"),
1102 get_keydir_fname("secret_onion_key_ntor.old"));
1104 STAT_KEY_DIRECTORY();
1105 OPEN_DATADIR("stats");
1106 STAT_DATADIR("stats");
1107 STAT_DATADIR2("stats", "dirreq-stats");
1109 consdiffmgr_register_with_sandbox(&cfg
);
1118 run_tor_main_loop(void)
1121 timers_initialize();
1122 initialize_mainloop_events();
1124 /* load the private keys, if we're supposed to have them, and set up the
1126 if (! client_identity_key_is_set()) {
1127 if (init_keys() < 0) {
1128 log_err(LD_OR
, "Error initializing keys; exiting");
1133 /* Set up our buckets */
1134 connection_bucket_init();
1136 /* initialize the bootstrap status events to know we're starting up */
1137 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING
, 0);
1139 /* Initialize the keypinning log. */
1140 if (authdir_mode_v3(get_options())) {
1141 char *fname
= get_datadir_fname("key-pinning-journal");
1143 if (keypin_load_journal(fname
)<0) {
1144 log_err(LD_DIR
, "Error loading key-pinning journal: %s",strerror(errno
));
1147 if (keypin_open_journal(fname
)<0) {
1148 log_err(LD_DIR
, "Error opening key-pinning journal: %s",strerror(errno
));
1156 /* This is the old name for key-pinning-journal. These got corrupted
1157 * in a couple of cases by #16530, so we started over. See #16580 for
1158 * the rationale and for other options we didn't take. We can remove
1159 * this code once all the authorities that ran 0.2.7.1-alpha-dev are
1162 char *fname
= get_datadir_fname("key-pinning-entries");
1167 if (trusted_dirs_reload_certs()) {
1169 "Couldn't load all cached v3 certificates. Starting anyway.");
1171 if (router_reload_consensus_networkstatus()) {
1174 /* load the routers file, or assign the defaults. */
1175 if (router_reload_router_list()) {
1178 /* load the networkstatuses. (This launches a download for new routers as
1181 const time_t now
= time(NULL
);
1182 directory_info_has_arrived(now
, 1, 0);
1184 if (server_mode(get_options()) || dir_server_mode(get_options())) {
1185 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
1188 consdiffmgr_enable_background_compression();
1190 /* Setup shared random protocol subsystem. */
1191 if (authdir_mode_v3(get_options())) {
1192 if (sr_init(1) < 0) {
1197 /* initialize dns resolve map, spawn workers if needed */
1198 if (dns_init() < 0) {
1199 if (get_options()->ServerDNSAllowBrokenConfig
)
1200 log_warn(LD_GENERAL
, "Couldn't set up any working nameservers. "
1201 "Network not up yet? Will try again soon.");
1203 log_err(LD_GENERAL
,"Error initializing dns subsystem; exiting. To "
1204 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
1210 const int r
= sd_notify(0, "READY=1");
1212 log_warn(LD_GENERAL
, "Unable to send readiness to systemd: %s",
1215 log_notice(LD_GENERAL
, "Signaled readiness to systemd");
1217 log_info(LD_GENERAL
, "Systemd NOTIFY_SOCKET not present.");
1220 #endif /* defined(HAVE_SYSTEMD) */
1222 return do_main_loop();
1225 /** Install the publish/subscribe relationships for all the subsystems. */
1227 pubsub_install(void)
1229 pubsub_builder_t
*builder
= pubsub_builder_new();
1230 int r
= subsystems_add_pubsub(builder
);
1232 r
= tor_mainloop_connect_pubsub(builder
); // consumes builder
1236 /** Connect the mainloop to its publish/subscribe message delivery events if
1237 * appropriate, and configure the global channels appropriately. */
1239 pubsub_connect(void)
1241 if (get_options()->command
== CMD_RUN_TOR
) {
1242 tor_mainloop_connect_pubsub_events();
1243 /* XXXX For each pubsub channel, its delivery strategy should be set at
1244 * this XXXX point, using tor_mainloop_set_delivery_strategy().
1246 tor_mainloop_set_delivery_strategy("orconn", DELIV_IMMEDIATE
);
1247 tor_mainloop_set_delivery_strategy("ocirc", DELIV_IMMEDIATE
);
1251 /* Main entry point for the Tor process. Called from tor_main(), and by
1252 * anybody embedding Tor. */
1254 tor_run_main(const tor_main_configuration_t
*tor_cfg
)
1258 #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
1259 event_set_mem_functions(tor_malloc_
, tor_realloc_
, tor_free_
);
1264 init_protocol_warning_severity_level();
1266 int argc
= tor_cfg
->argc
+ tor_cfg
->argc_owned
;
1267 char **argv
= tor_calloc(argc
, sizeof(char*));
1268 memcpy(argv
, tor_cfg
->argv
, tor_cfg
->argc
*sizeof(char*));
1269 if (tor_cfg
->argc_owned
)
1270 memcpy(argv
+ tor_cfg
->argc
, tor_cfg
->argv_owned
,
1271 tor_cfg
->argc_owned
*sizeof(char*));
1274 result
= nt_service_parse_options(argc
, argv
, &done
);
1281 int init_rv
= tor_init(argc
, argv
);
1284 result
= (init_rv
< 0) ? -1 : 0;
1291 if (get_options()->Sandbox
&& get_options()->command
== CMD_RUN_TOR
) {
1292 sandbox_cfg_t
* cfg
= sandbox_init_filter();
1294 if (sandbox_init(cfg
)) {
1296 log_err(LD_BUG
,"Failed to create syscall sandbox filter");
1300 tor_make_getaddrinfo_cache_active();
1302 // registering libevent rng
1303 #ifdef HAVE_EVUTIL_SECURE_RNG_SET_URANDOM_DEVICE_FILE
1304 evutil_secure_rng_set_urandom_device_file(
1305 (char*) sandbox_intern_string("/dev/urandom"));
1309 switch (get_options()->command
) {
1311 nt_service_set_state(SERVICE_RUNNING
);
1312 result
= run_tor_main_loop();
1315 result
= load_ed_keys(get_options(), time(NULL
)) < 0;
1317 case CMD_KEY_EXPIRATION
:
1319 result
= log_cert_expiration();
1321 case CMD_LIST_FINGERPRINT
:
1322 result
= do_list_fingerprint();
1324 case CMD_HASH_PASSWORD
:
1328 case CMD_VERIFY_CONFIG
:
1329 if (quiet_level
== QUIET_NONE
)
1330 printf("Configuration was valid\n");
1333 case CMD_DUMP_CONFIG
:
1334 result
= do_dump_config();
1336 case CMD_RUN_UNITTESTS
: /* only set by test.c */
1337 case CMD_IMMEDIATE
: /* Handled in config.c */
1339 log_warn(LD_BUG
,"Illegal command number %d: internal error.",
1340 get_options()->command
);