Tor  0.4.4.0-alpha-dev
relay_config.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-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * @file relay_config.c
9  * @brief Code to interpret the user's configuration of Tor's relay module.
10  **/
11 
12 #include "orconfig.h"
13 #define RELAY_CONFIG_PRIVATE
15 
16 #include "lib/encoding/confline.h"
17 #include "lib/confmgt/confmgt.h"
18 
20 #include "lib/geoip/geoip.h"
21 #include "lib/meminfo/meminfo.h"
22 #include "lib/osinfo/uname.h"
23 #include "lib/process/setuid.h"
24 
25 /* Required for dirinfo_type_t in or_options_t */
26 #include "core/or/or.h"
27 #include "app/config/config.h"
28 
31 #include "core/mainloop/mainloop.h"
32 #include "core/or/circuitbuild.h"
33 #include "core/or/connection_or.h"
34 #include "core/or/port_cfg_st.h"
35 
40 #include "feature/stats/rephist.h"
41 
43 
45 #include "feature/relay/dns.h"
47 
48 /** Contents of most recently read DirPortFrontPage file. */
49 static char *global_dirfrontpagecontents = NULL;
50 
51 /* Copied from config.c, we will refactor later in 29211. */
52 #define REJECT(arg) \
53  STMT_BEGIN *msg = tor_strdup(arg); return -1; STMT_END
54 #if defined(__GNUC__) && __GNUC__ <= 3
55 #define COMPLAIN(args...) \
56  STMT_BEGIN log_warn(LD_CONFIG, args); STMT_END
57 #else
58 #define COMPLAIN(args, ...) \
59  STMT_BEGIN log_warn(LD_CONFIG, args, ##__VA_ARGS__); STMT_END
60 #endif /* defined(__GNUC__) && __GNUC__ <= 3 */
61 
62 /* Used in the various options_transition_affects* functions. */
63 #define YES_IF_CHANGED_BOOL(opt) \
64  if (!CFG_EQ_BOOL(old_options, new_options, opt)) return 1;
65 #define YES_IF_CHANGED_INT(opt) \
66  if (!CFG_EQ_INT(old_options, new_options, opt)) return 1;
67 #define YES_IF_CHANGED_STRING(opt) \
68  if (!CFG_EQ_STRING(old_options, new_options, opt)) return 1;
69 #define YES_IF_CHANGED_LINELIST(opt) \
70  if (!CFG_EQ_LINELIST(old_options, new_options, opt)) return 1;
71 
72 /** Return the contents of our frontpage string, or NULL if not configured. */
73 MOCK_IMPL(const char*,
75 {
77 }
78 
79 /** Release all memory and resources held by global relay configuration
80  * structures.
81  */
82 void
84 {
86 }
87 
88 /** Return the bandwidthrate that we are going to report to the authorities
89  * based on the config options. */
90 uint32_t
92 {
93  uint64_t bw = options->BandwidthRate;
94  if (bw > options->MaxAdvertisedBandwidth)
95  bw = options->MaxAdvertisedBandwidth;
96  if (options->RelayBandwidthRate > 0 && bw > options->RelayBandwidthRate)
97  bw = options->RelayBandwidthRate;
98  /* config_ensure_bandwidth_cap() makes sure that this cast can't overflow. */
99  return (uint32_t)bw;
100 }
101 
102 /** Return the bandwidthburst that we are going to report to the authorities
103  * based on the config options. */
104 uint32_t
106 {
107  uint64_t bw = options->BandwidthBurst;
108  if (options->RelayBandwidthBurst > 0 && bw > options->RelayBandwidthBurst)
109  bw = options->RelayBandwidthBurst;
110  /* config_ensure_bandwidth_cap() makes sure that this cast can't overflow. */
111  return (uint32_t)bw;
112 }
113 
114 /** Warn for every Extended ORPort port in <b>ports</b> that is on a
115  * publicly routable address. */
116 void
117 port_warn_nonlocal_ext_orports(const smartlist_t *ports, const char *portname)
118 {
119  SMARTLIST_FOREACH_BEGIN(ports, const port_cfg_t *, port) {
120  if (port->type != CONN_TYPE_EXT_OR_LISTENER)
121  continue;
122  if (port->is_unix_addr)
123  continue;
124  /* XXX maybe warn even if address is RFC1918? */
125  if (!tor_addr_is_internal(&port->addr, 1)) {
126  log_warn(LD_CONFIG, "You specified a public address '%s' for %sPort. "
127  "This is not advised; this address is supposed to only be "
128  "exposed on localhost so that your pluggable transport "
129  "proxies can connect to it.",
130  fmt_addrport(&port->addr, port->port), portname);
131  }
132  } SMARTLIST_FOREACH_END(port);
133 }
134 
135 /** Given a list of <b>port_cfg_t</b> in <b>ports</b>, check them for internal
136  * consistency and warn as appropriate. On Unix-based OSes, set
137  * *<b>n_low_ports_out</b> to the number of sub-1024 ports we will be
138  * binding, and warn if we may be unable to re-bind after hibernation. */
139 static int
141  const or_options_t *options,
142  int *n_low_ports_out)
143 {
144  if (BUG(!ports))
145  return -1;
146 
147  if (BUG(!options))
148  return -1;
149 
150  if (BUG(!n_low_ports_out))
151  return -1;
152 
153  int n_orport_advertised = 0;
154  int n_orport_advertised_ipv4 = 0;
155  int n_orport_listeners = 0;
156  int n_dirport_advertised = 0;
157  int n_dirport_listeners = 0;
158  int n_low_port = 0;
159  int r = 0;
160 
161  SMARTLIST_FOREACH_BEGIN(ports, const port_cfg_t *, port) {
162  if (port->type == CONN_TYPE_DIR_LISTENER) {
163  if (! port->server_cfg.no_advertise)
164  ++n_dirport_advertised;
165  if (! port->server_cfg.no_listen)
166  ++n_dirport_listeners;
167  } else if (port->type == CONN_TYPE_OR_LISTENER) {
168  if (! port->server_cfg.no_advertise) {
169  ++n_orport_advertised;
170  if (port_binds_ipv4(port))
171  ++n_orport_advertised_ipv4;
172  }
173  if (! port->server_cfg.no_listen)
174  ++n_orport_listeners;
175  } else {
176  continue;
177  }
178 #ifndef _WIN32
179  if (!port->server_cfg.no_listen && port->port < 1024)
180  ++n_low_port;
181 #endif
182  } SMARTLIST_FOREACH_END(port);
183 
184  if (n_orport_advertised && !n_orport_listeners) {
185  log_warn(LD_CONFIG, "We are advertising an ORPort, but not actually "
186  "listening on one.");
187  r = -1;
188  }
189  if (n_orport_listeners && !n_orport_advertised) {
190  log_warn(LD_CONFIG, "We are listening on an ORPort, but not advertising "
191  "any ORPorts. This will keep us from building a %s "
192  "descriptor, and make us impossible to use.",
193  options->BridgeRelay ? "bridge" : "router");
194  r = -1;
195  }
196  if (n_dirport_advertised && !n_dirport_listeners) {
197  log_warn(LD_CONFIG, "We are advertising a DirPort, but not actually "
198  "listening on one.");
199  r = -1;
200  }
201  if (n_dirport_advertised > 1) {
202  log_warn(LD_CONFIG, "Can't advertise more than one DirPort.");
203  r = -1;
204  }
205  if (n_orport_advertised && !n_orport_advertised_ipv4 &&
206  !options->BridgeRelay) {
207  log_warn(LD_CONFIG, "Configured public relay to listen only on an IPv6 "
208  "address. Tor needs to listen on an IPv4 address too.");
209  r = -1;
210  }
211 
212  if (n_low_port && options->AccountingMax &&
213  (!have_capability_support() || options->KeepBindCapabilities == 0)) {
214  const char *extra = "";
215  if (options->KeepBindCapabilities == 0 && have_capability_support())
216  extra = ", and you have disabled KeepBindCapabilities.";
217  log_warn(LD_CONFIG,
218  "You have set AccountingMax to use hibernation. You have also "
219  "chosen a low DirPort or OrPort%s."
220  "This combination can make Tor stop "
221  "working when it tries to re-attach the port after a period of "
222  "hibernation. Please choose a different port or turn off "
223  "hibernation unless you know this combination will work on your "
224  "platform.", extra);
225  }
226 
227  if (n_low_ports_out)
228  *n_low_ports_out = n_low_port;
229 
230  return r;
231 }
232 
233 /** Parse all relay ports from <b>options</b>. On success, add parsed ports to
234  * <b>ports</b>, and return 0. On failure, set *<b>msg</b> to a description
235  * of the problem and return -1.
236  **/
237 int
239  char **msg,
240  smartlist_t *ports_out,
241  int *have_low_ports_out)
242 {
243  int retval = -1;
244  smartlist_t *ports = smartlist_new();
245  int n_low_ports = 0;
246 
247  if (BUG(!options))
248  goto err;
249 
250  if (BUG(!msg))
251  goto err;
252 
253  if (BUG(!ports_out))
254  goto err;
255 
256  if (BUG(!have_low_ports_out))
257  goto err;
258 
259  if (options->ClientOnly) {
260  retval = 0;
261  goto err;
262  }
263 
264  if (port_parse_config(ports,
265  options->ORPort_lines,
266  "OR", CONN_TYPE_OR_LISTENER,
267  "0.0.0.0", 0,
268  CL_PORT_SERVER_OPTIONS) < 0) {
269  *msg = tor_strdup("Invalid ORPort configuration");
270  goto err;
271  }
272  if (port_parse_config(ports,
273  options->ExtORPort_lines,
274  "ExtOR", CONN_TYPE_EXT_OR_LISTENER,
275  "127.0.0.1", 0,
276  CL_PORT_SERVER_OPTIONS|CL_PORT_WARN_NONLOCAL) < 0) {
277  *msg = tor_strdup("Invalid ExtORPort configuration");
278  goto err;
279  }
280  if (port_parse_config(ports,
281  options->DirPort_lines,
282  "Dir", CONN_TYPE_DIR_LISTENER,
283  "0.0.0.0", 0,
284  CL_PORT_SERVER_OPTIONS) < 0) {
285  *msg = tor_strdup("Invalid DirPort configuration");
286  goto err;
287  }
288 
289  if (check_server_ports(ports, options, &n_low_ports) < 0) {
290  *msg = tor_strdup("Misconfigured server ports");
291  goto err;
292  }
293 
294  smartlist_add_all(ports_out, ports);
295  smartlist_free(ports);
296  ports = NULL;
297  retval = 0;
298 
299  err:
300  if (*have_low_ports_out < 0)
301  *have_low_ports_out = (n_low_ports > 0);
302  if (ports) {
303  SMARTLIST_FOREACH(ports, port_cfg_t *, p, port_cfg_free(p));
304  smartlist_free(ports);
305  }
306  return retval;
307 }
308 
309 /** Update the relay *Port_set values in <b>options</b> from <b>ports</b>. */
310 void
312  const smartlist_t *ports)
313 {
314  if (BUG(!options))
315  return;
316 
317  if (BUG(!ports))
318  return;
319 
320  if (options->ClientOnly)
321  return;
322 
323  /* Update the relay *Port_set options. The !! here is to force a boolean
324  * out of an integer. */
325  options->ORPort_set =
327  options->DirPort_set =
329  options->ExtORPort_set =
331 }
332 
333 /**
334  * Legacy validation function, which checks that the current OS is usable in
335  * relay mode, if options is set to a relay mode.
336  *
337  * Warns about OSes with potential issues. Always returns 0.
338  */
339 int
341  or_options_t *options,
342  char **msg)
343 {
344  (void)old_options;
345 
346  if (BUG(!options))
347  return -1;
348 
349  if (BUG(!msg))
350  return -1;
351 
352  if (!server_mode(options))
353  return 0;
354 
355  const char *uname = get_uname();
356 
357  if (!strcmpstart(uname, "Windows 95") ||
358  !strcmpstart(uname, "Windows 98") ||
359  !strcmpstart(uname, "Windows Me")) {
360  log_warn(LD_CONFIG, "Tor is running as a server, but you are "
361  "running %s; this probably won't work. See "
362  "https://www.torproject.org/docs/faq.html#BestOSForRelay "
363  "for details.", uname);
364  }
365 
366  return 0;
367 }
368 
369 /**
370  * Legacy validation/normalization function for the relay info options.
371  * Uses old_options as the previous options.
372  *
373  * Returns 0 on success, returns -1 and sets *msg to a newly allocated string
374  * on error.
375  */
376 int
378  or_options_t *options,
379  char **msg)
380 {
381  (void)old_options;
382 
383  if (BUG(!options))
384  return -1;
385 
386  if (BUG(!msg))
387  return -1;
388 
389  if (options->Nickname == NULL) {
390  if (server_mode(options)) {
391  options->Nickname = tor_strdup(UNNAMED_ROUTER_NICKNAME);
392  }
393  } else {
394  if (!is_legal_nickname(options->Nickname)) {
395  tor_asprintf(msg,
396  "Nickname '%s', nicknames must be between 1 and 19 characters "
397  "inclusive, and must contain only the characters [a-zA-Z0-9].",
398  options->Nickname);
399  return -1;
400  }
401  }
402 
403  if (server_mode(options) && !options->ContactInfo)
404  log_notice(LD_CONFIG, "Your ContactInfo config option is not set. "
405  "Please consider setting it, so we can contact you if your server is "
406  "misconfigured or something else goes wrong.");
407 
408  const char *ContactInfo = options->ContactInfo;
409  if (ContactInfo && !string_is_utf8(ContactInfo, strlen(ContactInfo)))
410  REJECT("ContactInfo config option must be UTF-8.");
411 
412  return 0;
413 }
414 
415 /** Parse an authority type from <b>options</b>->PublishServerDescriptor
416  * and write it to <b>options</b>->PublishServerDescriptor_. Treat "1"
417  * as "v3" unless BridgeRelay is 1, in which case treat it as "bridge".
418  * Treat "0" as "".
419  * Return 0 on success or -1 if not a recognized authority type (in which
420  * case the value of PublishServerDescriptor_ is undefined). */
421 static int
423 {
424  smartlist_t *list = options->PublishServerDescriptor;
425  dirinfo_type_t *auth = &options->PublishServerDescriptor_;
426  *auth = NO_DIRINFO;
427  if (!list) /* empty list, answer is none */
428  return 0;
429  SMARTLIST_FOREACH_BEGIN(list, const char *, string) {
430  if (!strcasecmp(string, "v1"))
431  log_warn(LD_CONFIG, "PublishServerDescriptor v1 has no effect, because "
432  "there are no v1 directory authorities anymore.");
433  else if (!strcmp(string, "1"))
434  if (options->BridgeRelay)
435  *auth |= BRIDGE_DIRINFO;
436  else
437  *auth |= V3_DIRINFO;
438  else if (!strcasecmp(string, "v2"))
439  log_warn(LD_CONFIG, "PublishServerDescriptor v2 has no effect, because "
440  "there are no v2 directory authorities anymore.");
441  else if (!strcasecmp(string, "v3"))
442  *auth |= V3_DIRINFO;
443  else if (!strcasecmp(string, "bridge"))
444  *auth |= BRIDGE_DIRINFO;
445  else if (!strcasecmp(string, "hidserv"))
446  log_warn(LD_CONFIG,
447  "PublishServerDescriptor hidserv is invalid. See "
448  "PublishHidServDescriptors.");
449  else if (!strcasecmp(string, "") || !strcmp(string, "0"))
450  /* no authority */;
451  else
452  return -1;
453  } SMARTLIST_FOREACH_END(string);
454  return 0;
455 }
456 
457 /**
458  * Validate the configured bridge distribution method from a BridgeDistribution
459  * config line.
460  *
461  * The input <b>bd</b>, is a string taken from the BridgeDistribution config
462  * line (if present). If the option wasn't set, return 0 immediately. The
463  * BridgeDistribution option is then validated. Currently valid, recognised
464  * options are:
465  *
466  * - "none"
467  * - "any"
468  * - "https"
469  * - "email"
470  * - "moat"
471  *
472  * If the option string is unrecognised, a warning will be logged and 0 is
473  * returned. If the option string contains an invalid character, -1 is
474  * returned.
475  **/
476 STATIC int
478 {
479  if (bd == NULL)
480  return 0;
481 
482  const char *RECOGNIZED[] = {
483  "none", "any", "https", "email", "moat"
484  };
485  unsigned i;
486  for (i = 0; i < ARRAY_LENGTH(RECOGNIZED); ++i) {
487  if (!strcasecmp(bd, RECOGNIZED[i]))
488  return 0;
489  }
490 
491  const char *cp = bd;
492  // Method = (KeywordChar | "_") +
493  while (TOR_ISALNUM(*cp) || *cp == '-' || *cp == '_')
494  ++cp;
495 
496  if (*cp == 0) {
497  log_warn(LD_CONFIG, "Unrecognized BridgeDistribution value %s. I'll "
498  "assume you know what you are doing...", escaped(bd));
499  return 0; // we reached the end of the string; all is well
500  } else {
501  return -1; // we found a bad character in the string.
502  }
503 }
504 
505 /**
506  * Legacy validation/normalization function for the bridge relay options.
507  * Uses old_options as the previous options.
508  *
509  * Returns 0 on success, returns -1 and sets *msg to a newly allocated string
510  * on error.
511  */
512 int
514  or_options_t *options,
515  char **msg)
516 {
517  (void)old_options;
518 
519  if (BUG(!options))
520  return -1;
521 
522  if (BUG(!msg))
523  return -1;
524 
525  if (compute_publishserverdescriptor(options) < 0) {
526  tor_asprintf(msg, "Unrecognized value in PublishServerDescriptor");
527  return -1;
528  }
529 
530  if ((options->BridgeRelay
532  && (options->PublishServerDescriptor_ & V3_DIRINFO)) {
533  REJECT("Bridges are not supposed to publish router descriptors to the "
534  "directory authorities. Please correct your "
535  "PublishServerDescriptor line.");
536  }
537 
538  if (options->BridgeDistribution) {
539  if (!options->BridgeRelay) {
540  REJECT("You set BridgeDistribution, but you didn't set BridgeRelay!");
541  }
543  REJECT("Invalid BridgeDistribution value.");
544  }
545  }
546 
547  if (options->PublishServerDescriptor)
548  SMARTLIST_FOREACH(options->PublishServerDescriptor, const char *, pubdes, {
549  if (!strcmp(pubdes, "1") || !strcmp(pubdes, "0"))
550  if (smartlist_len(options->PublishServerDescriptor) > 1) {
551  COMPLAIN("You have passed a list of multiple arguments to the "
552  "PublishServerDescriptor option that includes 0 or 1. "
553  "0 or 1 should only be used as the sole argument. "
554  "This configuration will be rejected in a future release.");
555  break;
556  }
557  });
558 
559  return 0;
560 }
561 
562 /**
563  * Legacy validation/normalization function for the relay padding options.
564  * Uses old_options as the previous options.
565  *
566  * Returns 0 on success, returns -1 and sets *msg to a newly allocated string
567  * on error.
568  */
569 int
571  or_options_t *options,
572  char **msg)
573 {
574  (void)old_options;
575 
576  if (BUG(!options))
577  return -1;
578 
579  if (BUG(!msg))
580  return -1;
581 
582  if (!server_mode(options))
583  return 0;
584 
585  if (options->ConnectionPadding != -1) {
586  REJECT("Relays must use 'auto' for the ConnectionPadding setting.");
587  }
588 
589  if (options->ReducedConnectionPadding != 0) {
590  REJECT("Relays cannot set ReducedConnectionPadding. ");
591  }
592 
593  if (options->CircuitPadding == 0) {
594  REJECT("Relays cannot set CircuitPadding to 0. ");
595  }
596 
597  if (options->ReducedCircuitPadding == 1) {
598  REJECT("Relays cannot set ReducedCircuitPadding. ");
599  }
600 
601  return 0;
602 }
603 
604 /**
605  * Legacy validation/normalization function for the relay bandwidth options.
606  * Uses old_options as the previous options.
607  *
608  * Returns 0 on success, returns -1 and sets *msg to a newly allocated string
609  * on error.
610  */
611 int
613  or_options_t *options,
614  char **msg)
615 {
616  (void)old_options;
617 
618  if (BUG(!options))
619  return -1;
620 
621  if (BUG(!msg))
622  return -1;
623 
624  /* 31851: the tests expect us to validate bandwidths, even when we are not
625  * in relay mode. */
627  "MaxAdvertisedBandwidth", msg) < 0)
628  return -1;
630  "RelayBandwidthRate", msg) < 0)
631  return -1;
633  "RelayBandwidthBurst", msg) < 0)
634  return -1;
636  "PerConnBWRate", msg) < 0)
637  return -1;
639  "PerConnBWBurst", msg) < 0)
640  return -1;
641 
642  if (options->RelayBandwidthRate && !options->RelayBandwidthBurst)
643  options->RelayBandwidthBurst = options->RelayBandwidthRate;
644  if (options->RelayBandwidthBurst && !options->RelayBandwidthRate)
645  options->RelayBandwidthRate = options->RelayBandwidthBurst;
646 
647  if (server_mode(options)) {
648  const unsigned required_min_bw =
649  public_server_mode(options) ?
650  RELAY_REQUIRED_MIN_BANDWIDTH : BRIDGE_REQUIRED_MIN_BANDWIDTH;
651  const char * const optbridge =
652  public_server_mode(options) ? "" : "bridge ";
653  if (options->BandwidthRate < required_min_bw) {
654  tor_asprintf(msg,
655  "BandwidthRate is set to %d bytes/second. "
656  "For %sservers, it must be at least %u.",
657  (int)options->BandwidthRate, optbridge,
658  required_min_bw);
659  return -1;
660  } else if (options->MaxAdvertisedBandwidth <
661  required_min_bw/2) {
662  tor_asprintf(msg,
663  "MaxAdvertisedBandwidth is set to %d bytes/second. "
664  "For %sservers, it must be at least %u.",
665  (int)options->MaxAdvertisedBandwidth, optbridge,
666  required_min_bw/2);
667  return -1;
668  }
669  if (options->RelayBandwidthRate &&
670  options->RelayBandwidthRate < required_min_bw) {
671  tor_asprintf(msg,
672  "RelayBandwidthRate is set to %d bytes/second. "
673  "For %sservers, it must be at least %u.",
674  (int)options->RelayBandwidthRate, optbridge,
675  required_min_bw);
676  return -1;
677  }
678  }
679 
680  /* 31851: the tests expect us to validate bandwidths, even when we are not
681  * in relay mode. */
682  if (options->RelayBandwidthRate > options->RelayBandwidthBurst)
683  REJECT("RelayBandwidthBurst must be at least equal "
684  "to RelayBandwidthRate.");
685 
686  /* if they set relaybandwidth* really high but left bandwidth*
687  * at the default, raise the defaults. */
688  if (options->RelayBandwidthRate > options->BandwidthRate)
689  options->BandwidthRate = options->RelayBandwidthRate;
690  if (options->RelayBandwidthBurst > options->BandwidthBurst)
691  options->BandwidthBurst = options->RelayBandwidthBurst;
692 
693  return 0;
694 }
695 
696 /**
697  * Legacy validation/normalization function for the relay bandwidth accounting
698  * options. Uses old_options as the previous options.
699  *
700  * Returns 0 on success, returns -1 and sets *msg to a newly allocated string
701  * on error.
702  */
703 int
705  or_options_t *options,
706  char **msg)
707 {
708  (void)old_options;
709 
710  if (BUG(!options))
711  return -1;
712 
713  if (BUG(!msg))
714  return -1;
715 
716  /* 31851: the tests expect us to validate accounting, even when we are not
717  * in relay mode. */
718  if (accounting_parse_options(options, 1)<0)
719  REJECT("Failed to parse accounting options. See logs for details.");
720 
721  if (options->AccountingMax) {
722  if (options->RendConfigLines && server_mode(options)) {
723  log_warn(LD_CONFIG, "Using accounting with a hidden service and an "
724  "ORPort is risky: your hidden service(s) and your public "
725  "address will all turn off at the same time, which may alert "
726  "observers that they are being run by the same party.");
727  } else if (config_count_key(options->RendConfigLines,
728  "HiddenServiceDir") > 1) {
729  log_warn(LD_CONFIG, "Using accounting with multiple hidden services is "
730  "risky: they will all turn off at the same time, which may "
731  "alert observers that they are being run by the same party.");
732  }
733  }
734 
735  options->AccountingRule = ACCT_MAX;
736  if (options->AccountingRule_option) {
737  if (!strcmp(options->AccountingRule_option, "sum"))
738  options->AccountingRule = ACCT_SUM;
739  else if (!strcmp(options->AccountingRule_option, "max"))
740  options->AccountingRule = ACCT_MAX;
741  else if (!strcmp(options->AccountingRule_option, "in"))
742  options->AccountingRule = ACCT_IN;
743  else if (!strcmp(options->AccountingRule_option, "out"))
744  options->AccountingRule = ACCT_OUT;
745  else
746  REJECT("AccountingRule must be 'sum', 'max', 'in', or 'out'");
747  }
748 
749  return 0;
750 }
751 
752 /** Verify whether lst is a list of strings containing valid-looking
753  * comma-separated nicknames, or NULL. Will normalise <b>lst</b> to prefix '$'
754  * to any nickname or fingerprint that needs it. Also splits comma-separated
755  * list elements into multiple elements. Return 0 on success.
756  * Warn and return -1 on failure.
757  */
758 static int
760  const config_line_t *lst, const char *name,
761  char **msg)
762 {
763  if (!lst)
764  return 0;
765 
766  config_line_t *new_nicknames = NULL;
767  config_line_t **new_nicknames_next = &new_nicknames;
768 
769  const config_line_t *cl;
770  for (cl = lst; cl; cl = cl->next) {
771  const char *line = cl->value;
772  if (!line)
773  continue;
774 
775  int valid_line = 1;
776  smartlist_t *sl = smartlist_new();
777  smartlist_split_string(sl, line, ",",
778  SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK|SPLIT_STRIP_SPACE, 0);
779  SMARTLIST_FOREACH_BEGIN(sl, char *, s)
780  {
781  char *normalized = NULL;
783  // check if first char is dollar
784  if (s[0] != '$') {
785  // Try again but with a dollar symbol prepended
786  char *prepended;
787  tor_asprintf(&prepended, "$%s", s);
788 
789  if (is_legal_nickname_or_hexdigest(prepended)) {
790  // The nickname is valid when it's prepended, set it as the
791  // normalized version
792  normalized = prepended;
793  } else {
794  // Still not valid, free and fallback to error message
795  tor_free(prepended);
796  }
797  }
798 
799  if (!normalized) {
800  tor_asprintf(msg, "Invalid nickname '%s' in %s line", s, name);
801  valid_line = 0;
802  break;
803  }
804  } else {
805  normalized = tor_strdup(s);
806  }
807 
808  config_line_t *next = tor_malloc_zero(sizeof(*next));
809  next->key = tor_strdup(cl->key);
810  next->value = normalized;
811  next->next = NULL;
812 
813  *new_nicknames_next = next;
814  new_nicknames_next = &next->next;
815  } SMARTLIST_FOREACH_END(s);
816 
817  SMARTLIST_FOREACH(sl, char *, s, tor_free(s));
818  smartlist_free(sl);
819 
820  if (!valid_line) {
821  config_free_lines(new_nicknames);
822  return -1;
823  }
824  }
825 
826  *normalized_out = new_nicknames;
827 
828  return 0;
829 }
830 
831 #define ONE_MEGABYTE (UINT64_C(1) << 20)
832 
833 /* If we have less than 300 MB suggest disabling dircache */
834 #define DIRCACHE_MIN_MEM_MB 300
835 #define DIRCACHE_MIN_MEM_BYTES (DIRCACHE_MIN_MEM_MB*ONE_MEGABYTE)
836 #define STRINGIFY(val) #val
837 
838 /** Create a warning message for emitting if we are a dircache but may not have
839  * enough system memory, or if we are not a dircache but probably should be.
840  * Return -1 when a message is returned in *msg*, else return 0. */
841 STATIC int
842 have_enough_mem_for_dircache(const or_options_t *options, size_t total_mem,
843  char **msg)
844 {
845  *msg = NULL;
846  /* XXX We should possibly be looking at MaxMemInQueues here
847  * unconditionally. Or we should believe total_mem unconditionally. */
848  if (total_mem == 0) {
849  if (get_total_system_memory(&total_mem) < 0) {
850  total_mem = options->MaxMemInQueues >= SIZE_MAX ?
851  SIZE_MAX : (size_t)options->MaxMemInQueues;
852  }
853  }
854  if (options->DirCache) {
855  if (total_mem < DIRCACHE_MIN_MEM_BYTES) {
856  if (options->BridgeRelay) {
857  tor_asprintf(msg, "Running a Bridge with less than %d MB of memory "
858  "is not recommended.", DIRCACHE_MIN_MEM_MB);
859  } else {
860  tor_asprintf(msg, "Being a directory cache (default) with less than "
861  "%d MB of memory is not recommended and may consume "
862  "most of the available resources. Consider disabling "
863  "this functionality by setting the DirCache option "
864  "to 0.", DIRCACHE_MIN_MEM_MB);
865  }
866  }
867  } else {
868  if (total_mem >= DIRCACHE_MIN_MEM_BYTES) {
869  *msg = tor_strdup("DirCache is disabled and we are configured as a "
870  "relay. We will not become a Guard.");
871  }
872  }
873  return *msg == NULL ? 0 : -1;
874 }
875 #undef STRINGIFY
876 
877 /**
878  * Legacy validation/normalization function for the relay mode options.
879  * Uses old_options as the previous options.
880  *
881  * Returns 0 on success, returns -1 and sets *msg to a newly allocated string
882  * on error.
883  */
884 int
886  or_options_t *options,
887  char **msg)
888 {
889  (void)old_options;
890 
891  if (BUG(!options))
892  return -1;
893 
894  if (BUG(!msg))
895  return -1;
896 
897  if (server_mode(options) && options->RendConfigLines)
898  log_warn(LD_CONFIG,
899  "Tor is currently configured as a relay and a hidden service. "
900  "That's not very secure: you should probably run your hidden service "
901  "in a separate Tor process, at least -- see "
902  "https://trac.torproject.org/8742");
903 
904  if (options->BridgeRelay && options->DirPort_set) {
905  log_warn(LD_CONFIG, "Can't set a DirPort on a bridge relay; disabling "
906  "DirPort");
907  config_free_lines(options->DirPort_lines);
908  options->DirPort_lines = NULL;
909  options->DirPort_set = 0;
910  }
911 
912  if (options->DirPort_set && !options->DirCache) {
913  REJECT("DirPort configured but DirCache disabled. DirPort requires "
914  "DirCache.");
915  }
916 
917  if (options->BridgeRelay && !options->DirCache) {
918  REJECT("We're a bridge but DirCache is disabled. BridgeRelay requires "
919  "DirCache.");
920  }
921 
922  if (options->BridgeRelay == 1 && ! options->ORPort_set)
923  REJECT("BridgeRelay is 1, ORPort is not set. This is an invalid "
924  "combination.");
925 
926  if (server_mode(options)) {
927  char *dircache_msg = NULL;
928  if (have_enough_mem_for_dircache(options, 0, &dircache_msg)) {
929  log_warn(LD_CONFIG, "%s", dircache_msg);
930  tor_free(dircache_msg);
931  }
932  }
933 
934  if (options->MyFamily_lines && options->BridgeRelay) {
935  log_warn(LD_CONFIG, "Listing a family for a bridge relay is not "
936  "supported: it can reveal bridge fingerprints to censors. "
937  "You should also make sure you aren't listing this bridge's "
938  "fingerprint in any other MyFamily.");
939  }
940  if (options->MyFamily_lines && !options->ContactInfo) {
941  log_warn(LD_CONFIG, "MyFamily is set but ContactInfo is not configured. "
942  "ContactInfo should always be set when MyFamily option is too.");
943  }
944  if (normalize_nickname_list(&options->MyFamily,
945  options->MyFamily_lines, "MyFamily", msg))
946  return -1;
947 
948  if (options->ConstrainedSockets) {
949  if (options->DirPort_set) {
950  /* Providing cached directory entries while system TCP buffers are scarce
951  * will exacerbate the socket errors. Suggest that this be disabled. */
952  COMPLAIN("You have requested constrained socket buffers while also "
953  "serving directory entries via DirPort. It is strongly "
954  "suggested that you disable serving directory requests when "
955  "system TCP buffer resources are scarce.");
956  }
957  }
958 
959  return 0;
960 }
961 
962 /**
963  * Legacy validation/normalization function for the relay testing options
964  * in options. Uses old_options as the previous options.
965  *
966  * Returns 0 on success, returns -1 and sets *msg to a newly allocated string
967  * on error.
968  */
969 int
971  or_options_t *options,
972  char **msg)
973 {
974  (void)old_options;
975 
976  if (BUG(!options))
977  return -1;
978 
979  if (BUG(!msg))
980  return -1;
981 
982  if (options->SigningKeyLifetime < options->TestingSigningKeySlop*2)
983  REJECT("SigningKeyLifetime is too short.");
984  if (options->TestingLinkCertLifetime < options->TestingAuthKeySlop*2)
985  REJECT("LinkCertLifetime is too short.");
986  if (options->TestingAuthKeyLifetime < options->TestingLinkKeySlop*2)
987  REJECT("TestingAuthKeyLifetime is too short.");
988 
989  return 0;
990 }
991 
992 /** Return 1 if any change from <b>old_options</b> to <b>new_options</b>
993  * will require us to rotate the CPU and DNS workers; else return 0. */
994 static int
996  const or_options_t *new_options)
997 {
998  YES_IF_CHANGED_STRING(DataDirectory);
999  YES_IF_CHANGED_INT(NumCPUs);
1000  YES_IF_CHANGED_LINELIST(ORPort_lines);
1001  YES_IF_CHANGED_BOOL(ServerDNSSearchDomains);
1002  YES_IF_CHANGED_BOOL(SafeLogging_);
1003  YES_IF_CHANGED_BOOL(ClientOnly);
1004  YES_IF_CHANGED_BOOL(LogMessageDomains);
1005  YES_IF_CHANGED_LINELIST(Logs);
1006 
1007  if (server_mode(old_options) != server_mode(new_options) ||
1008  public_server_mode(old_options) != public_server_mode(new_options) ||
1009  dir_server_mode(old_options) != dir_server_mode(new_options))
1010  return 1;
1011 
1012  /* Nothing that changed matters. */
1013  return 0;
1014 }
1015 
1016 /** Return 1 if any change from <b>old_options</b> to <b>new_options</b>
1017  * will require us to generate a new descriptor; else return 0. */
1018 static int
1020  const or_options_t *new_options)
1021 {
1022  /* XXX We can be smarter here. If your DirPort isn't being
1023  * published and you just turned it off, no need to republish. Etc. */
1024 
1025  YES_IF_CHANGED_STRING(DataDirectory);
1026  YES_IF_CHANGED_STRING(Nickname);
1027  YES_IF_CHANGED_STRING(Address);
1028  YES_IF_CHANGED_LINELIST(ExitPolicy);
1029  YES_IF_CHANGED_BOOL(ExitRelay);
1030  YES_IF_CHANGED_BOOL(ExitPolicyRejectPrivate);
1031  YES_IF_CHANGED_BOOL(ExitPolicyRejectLocalInterfaces);
1032  YES_IF_CHANGED_BOOL(IPv6Exit);
1033  YES_IF_CHANGED_LINELIST(ORPort_lines);
1034  YES_IF_CHANGED_LINELIST(DirPort_lines);
1035  YES_IF_CHANGED_LINELIST(DirPort_lines);
1036  YES_IF_CHANGED_BOOL(ClientOnly);
1037  YES_IF_CHANGED_BOOL(DisableNetwork);
1038  YES_IF_CHANGED_BOOL(PublishServerDescriptor_);
1039  YES_IF_CHANGED_STRING(ContactInfo);
1040  YES_IF_CHANGED_STRING(BridgeDistribution);
1041  YES_IF_CHANGED_LINELIST(MyFamily);
1042  YES_IF_CHANGED_STRING(AccountingStart);
1043  YES_IF_CHANGED_INT(AccountingMax);
1044  YES_IF_CHANGED_INT(AccountingRule);
1045  YES_IF_CHANGED_BOOL(DirCache);
1046  YES_IF_CHANGED_BOOL(AssumeReachable);
1047 
1048  if (relay_get_effective_bwrate(old_options) !=
1049  relay_get_effective_bwrate(new_options) ||
1050  relay_get_effective_bwburst(old_options) !=
1051  relay_get_effective_bwburst(new_options) ||
1052  public_server_mode(old_options) != public_server_mode(new_options))
1053  return 1;
1054 
1055  return 0;
1056 }
1057 
1058 /** Fetch the active option list, and take relay actions based on it. All of
1059  * the things we do should survive being done repeatedly. If present,
1060  * <b>old_options</b> contains the previous value of the options.
1061  *
1062  * Return 0 if all goes well, return -1 if it's time to die.
1063  *
1064  * Note: We haven't moved all the "act on new configuration" logic
1065  * into the options_act* functions yet. Some is still in do_hup() and other
1066  * places.
1067  */
1068 int
1069 options_act_relay(const or_options_t *old_options)
1070 {
1071  const or_options_t *options = get_options();
1072 
1073  const int transition_affects_workers =
1074  old_options && options_transition_affects_workers(old_options, options);
1075 
1076  /* We want to reinit keys as needed before we do much of anything else:
1077  keys are important, and other things can depend on them. */
1078  if (transition_affects_workers ||
1079  (authdir_mode_v3(options) && (!old_options ||
1080  !authdir_mode_v3(old_options)))) {
1081  if (init_keys() < 0) {
1082  log_warn(LD_BUG,"Error initializing keys; exiting");
1083  return -1;
1084  }
1085  }
1086 
1087  if (server_mode(options)) {
1088  static int cdm_initialized = 0;
1089  if (cdm_initialized == 0) {
1090  cdm_initialized = 1;
1091  consdiffmgr_configure(NULL);
1093  }
1094  }
1095 
1096  /* Check for transitions that need action. */
1097  if (old_options) {
1098  if (transition_affects_workers) {
1099  log_info(LD_GENERAL,
1100  "Worker-related options changed. Rotating workers.");
1101  const int server_mode_turned_on =
1102  server_mode(options) && !server_mode(old_options);
1103  const int dir_server_mode_turned_on =
1104  dir_server_mode(options) && !dir_server_mode(old_options);
1105 
1106  if (server_mode_turned_on || dir_server_mode_turned_on) {
1107  cpu_init();
1108  }
1109 
1110  if (server_mode_turned_on) {
1111  ip_address_changed(0);
1112  if (have_completed_a_circuit() || !any_predicted_circuits(time(NULL)))
1114  }
1116  }
1117  }
1118 
1119  return 0;
1120 }
1121 
1122 /** Fetch the active option list, and take relay accounting actions based on
1123  * it. All of the things we do should survive being done repeatedly. If
1124  * present, <b>old_options</b> contains the previous value of the options.
1125  *
1126  * Return 0 if all goes well, return -1 if it's time to die.
1127  *
1128  * Note: We haven't moved all the "act on new configuration" logic
1129  * into the options_act* functions yet. Some is still in do_hup() and other
1130  * places.
1131  */
1132 int
1134 {
1135  (void)old_options;
1136 
1137  const or_options_t *options = get_options();
1138 
1139  /* Set up accounting */
1140  if (accounting_parse_options(options, 0)<0) {
1141  // LCOV_EXCL_START
1142  log_warn(LD_BUG,"Error in previously validated accounting options");
1143  return -1;
1144  // LCOV_EXCL_STOP
1145  }
1146  if (accounting_is_enabled(options))
1147  configure_accounting(time(NULL));
1148 
1149  return 0;
1150 }
1151 
1152 /** Fetch the active option list, and take relay bandwidth actions based on
1153  * it. All of the things we do should survive being done repeatedly. If
1154  * present, <b>old_options</b> contains the previous value of the options.
1155  *
1156  * Return 0 if all goes well, return -1 if it's time to die.
1157  *
1158  * Note: We haven't moved all the "act on new configuration" logic
1159  * into the options_act* functions yet. Some is still in do_hup() and other
1160  * places.
1161  */
1162 int
1164 {
1165  const or_options_t *options = get_options();
1166 
1167  /* Check for transitions that need action. */
1168  if (old_options) {
1169  if (options->PerConnBWRate != old_options->PerConnBWRate ||
1170  options->PerConnBWBurst != old_options->PerConnBWBurst)
1172 
1173  if (options->RelayBandwidthRate != old_options->RelayBandwidthRate ||
1174  options->RelayBandwidthBurst != old_options->RelayBandwidthBurst)
1175  connection_bucket_adjust(options);
1176  }
1177 
1178  return 0;
1179 }
1180 
1181 /** Fetch the active option list, and take bridge statistics actions based on
1182  * it. All of the things we do should survive being done repeatedly. If
1183  * present, <b>old_options</b> contains the previous value of the options.
1184  *
1185  * Return 0 if all goes well, return -1 if it's time to die.
1186  *
1187  * Note: We haven't moved all the "act on new configuration" logic
1188  * into the options_act* functions yet. Some is still in do_hup() and other
1189  * places.
1190  */
1191 int
1193 {
1194  const or_options_t *options = get_options();
1195 
1196 /* How long should we delay counting bridge stats after becoming a bridge?
1197  * We use this so we don't count clients who used our bridge thinking it is
1198  * a relay. If you change this, don't forget to change the log message
1199  * below. It's 4 hours (the time it takes to stop being used by clients)
1200  * plus some extra time for clock skew. */
1201 #define RELAY_BRIDGE_STATS_DELAY (6 * 60 * 60)
1202 
1203  /* Check for transitions that need action. */
1204  if (old_options) {
1205  if (! bool_eq(options->BridgeRelay, old_options->BridgeRelay)) {
1206  int was_relay = 0;
1207  if (options->BridgeRelay) {
1208  time_t int_start = time(NULL);
1209  if (config_lines_eq(old_options->ORPort_lines,options->ORPort_lines)) {
1210  int_start += RELAY_BRIDGE_STATS_DELAY;
1211  was_relay = 1;
1212  }
1213  geoip_bridge_stats_init(int_start);
1214  log_info(LD_CONFIG, "We are acting as a bridge now. Starting new "
1215  "GeoIP stats interval%s.", was_relay ? " in 6 "
1216  "hours from now" : "");
1217  } else {
1219  log_info(LD_GENERAL, "We are no longer acting as a bridge. "
1220  "Forgetting GeoIP stats.");
1221  }
1222  }
1223  }
1224 
1225  return 0;
1226 }
1227 
1228 /** Fetch the active option list, and take relay statistics actions based on
1229  * it. All of the things we do should survive being done repeatedly. If
1230  * present, <b>old_options</b> contains the previous value of the options.
1231  *
1232  * Sets <b>*print_notice_out</b> if we enabled stats, and need to print
1233  * a stats log using options_act_relay_stats_msg().
1234  *
1235  * If loading the GeoIP file failed, sets DirReqStatistics and
1236  * EntryStatistics to 0. This breaks the normalization/act ordering
1237  * introduced in 29211.
1238  *
1239  * Return 0 if all goes well, return -1 if it's time to die.
1240  *
1241  * Note: We haven't moved all the "act on new configuration" logic
1242  * into the options_act* functions yet. Some is still in do_hup() and other
1243  * places.
1244  */
1245 int
1247  bool *print_notice_out)
1248 {
1249  if (BUG(!print_notice_out))
1250  return -1;
1251 
1252  or_options_t *options = get_options_mutable();
1253 
1254  if (options->CellStatistics || options->DirReqStatistics ||
1255  options->EntryStatistics || options->ExitPortStatistics ||
1256  options->ConnDirectionStatistics ||
1257  options->HiddenServiceStatistics) {
1258  time_t now = time(NULL);
1259  int print_notice = 0;
1260 
1261  if ((!old_options || !old_options->CellStatistics) &&
1262  options->CellStatistics) {
1264  print_notice = 1;
1265  }
1266  if ((!old_options || !old_options->DirReqStatistics) &&
1267  options->DirReqStatistics) {
1268  if (geoip_is_loaded(AF_INET)) {
1270  print_notice = 1;
1271  } else {
1272  /* disable statistics collection since we have no geoip file */
1273  /* 29211: refactor to avoid the normalisation/act inversion */
1274  options->DirReqStatistics = 0;
1275  if (options->ORPort_set)
1276  log_notice(LD_CONFIG, "Configured to measure directory request "
1277  "statistics, but no GeoIP database found. "
1278  "Please specify a GeoIP database using the "
1279  "GeoIPFile option.");
1280  }
1281  }
1282  if ((!old_options || !old_options->EntryStatistics) &&
1283  options->EntryStatistics && !should_record_bridge_info(options)) {
1284  /* If we get here, we've started recording bridge info when we didn't
1285  * do so before. Note that "should_record_bridge_info()" will
1286  * always be false at this point, because of the earlier block
1287  * that cleared EntryStatistics when public_server_mode() was false.
1288  * We're leaving it in as defensive programming. */
1289  if (geoip_is_loaded(AF_INET) || geoip_is_loaded(AF_INET6)) {
1291  print_notice = 1;
1292  } else {
1293  options->EntryStatistics = 0;
1294  log_notice(LD_CONFIG, "Configured to measure entry node "
1295  "statistics, but no GeoIP database found. "
1296  "Please specify a GeoIP database using the "
1297  "GeoIPFile option.");
1298  }
1299  }
1300  if ((!old_options || !old_options->ExitPortStatistics) &&
1301  options->ExitPortStatistics) {
1303  print_notice = 1;
1304  }
1305  if ((!old_options || !old_options->ConnDirectionStatistics) &&
1306  options->ConnDirectionStatistics) {
1308  }
1309  if ((!old_options || !old_options->HiddenServiceStatistics) &&
1310  options->HiddenServiceStatistics) {
1311  log_info(LD_CONFIG, "Configured to measure hidden service statistics.");
1313  }
1314  if (print_notice)
1315  *print_notice_out = 1;
1316  }
1317 
1318  /* If we used to have statistics enabled but we just disabled them,
1319  stop gathering them. */
1320  if (old_options && old_options->CellStatistics &&
1321  !options->CellStatistics)
1323  if (old_options && old_options->DirReqStatistics &&
1324  !options->DirReqStatistics)
1326  if (old_options && old_options->EntryStatistics &&
1327  !options->EntryStatistics)
1329  if (old_options && old_options->HiddenServiceStatistics &&
1330  !options->HiddenServiceStatistics)
1332  if (old_options && old_options->ExitPortStatistics &&
1333  !options->ExitPortStatistics)
1335  if (old_options && old_options->ConnDirectionStatistics &&
1336  !options->ConnDirectionStatistics)
1338 
1339  return 0;
1340 }
1341 
1342 /** Print a notice about relay/dirauth stats being enabled. */
1343 void
1345 {
1346  log_notice(LD_CONFIG, "Configured to measure statistics. Look for "
1347  "the *-stats files that will first be written to the "
1348  "data directory in 24 hours from now.");
1349 }
1350 
1351 /** Fetch the active option list, and take relay descriptor actions based on
1352  * it. All of the things we do should survive being done repeatedly. If
1353  * present, <b>old_options</b> contains the previous value of the options.
1354  *
1355  * Return 0 if all goes well, return -1 if it's time to die.
1356  *
1357  * Note: We haven't moved all the "act on new configuration" logic
1358  * into the options_act* functions yet. Some is still in do_hup() and other
1359  * places.
1360  */
1361 int
1363 {
1364  const or_options_t *options = get_options();
1365 
1366  /* Since our options changed, we might need to regenerate and upload our
1367  * server descriptor.
1368  */
1369  if (!old_options ||
1370  options_transition_affects_descriptor(old_options, options))
1371  mark_my_descriptor_dirty("config change");
1372 
1373  return 0;
1374 }
1375 
1376 /** Fetch the active option list, and take relay DoS actions based on
1377  * it. All of the things we do should survive being done repeatedly. If
1378  * present, <b>old_options</b> contains the previous value of the options.
1379  *
1380  * Return 0 if all goes well, return -1 if it's time to die.
1381  *
1382  * Note: We haven't moved all the "act on new configuration" logic
1383  * into the options_act* functions yet. Some is still in do_hup() and other
1384  * places.
1385  */
1386 int
1388 {
1389  const or_options_t *options = get_options();
1390 
1391  /* DoS mitigation subsystem only applies to public relay. */
1392  if (public_server_mode(options)) {
1393  /* If we are configured as a relay, initialize the subsystem. Even on HUP,
1394  * this is safe to call as it will load data from the current options
1395  * or/and the consensus. */
1396  dos_init();
1397  } else if (old_options && public_server_mode(old_options)) {
1398  /* Going from relay to non relay, clean it up. */
1399  dos_free_all();
1400  }
1401 
1402  return 0;
1403 }
1404 
1405 /** Fetch the active option list, and take dirport actions based on
1406  * it. All of the things we do should survive being done repeatedly. If
1407  * present, <b>old_options</b> contains the previous value of the options.
1408  *
1409  * Return 0 if all goes well, return -1 if it's time to die.
1410  *
1411  * Note: We haven't moved all the "act on new configuration" logic
1412  * into the options_act* functions yet. Some is still in do_hup() and other
1413  * places.
1414  */
1415 int
1417 {
1418  (void)old_options;
1419 
1420  const or_options_t *options = get_options();
1421 
1422  if (!public_server_mode(options))
1423  return 0;
1424 
1425  /* Load the webpage we're going to serve every time someone asks for '/' on
1426  our DirPort. */
1428  if (options->DirPortFrontPage) {
1430  read_file_to_str(options->DirPortFrontPage, 0, NULL);
1432  log_warn(LD_CONFIG,
1433  "DirPortFrontPage file '%s' not found. Continuing anyway.",
1434  options->DirPortFrontPage);
1435  }
1436  }
1437 
1438  return 0;
1439 }
int config_ensure_bandwidth_cap(uint64_t *value, const char *desc, char **msg)
Definition: config.c:3104
#define CONN_TYPE_DIR_LISTENER
Definition: connection.h:51
smartlist_t * get_connection_array(void)
Definition: mainloop.c:452
static int options_transition_affects_workers(const or_options_t *old_options, const or_options_t *new_options)
Definition: relay_config.c:995
char * DirPortFrontPage
static int compute_publishserverdescriptor(or_options_t *options)
Definition: relay_config.c:422
Header for feature/relay/relay_config.c.
char * AccountingRule_option
int TestingLinkKeySlop
Header for uname.c.
Header for confline.c.
int options_act_relay_accounting(const or_options_t *old_options)
Header for smartlist.c.
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
void rep_hist_exit_stats_term(void)
Definition: rephist.c:1579
Header file for geoip_stats.c.
void consdiffmgr_configure(const consdiff_cfg_t *cfg)
Definition: consdiffmgr.c:834
int config_lines_eq(const config_line_t *a, const config_line_t *b)
Definition: confline.c:259
uint64_t PerConnBWRate
char * ContactInfo
int ReducedConnectionPadding
int port_count_real_listeners(const smartlist_t *ports, int listenertype, int count_sockets)
Definition: config.c:6536
Header file for connection.c.
int options_act_relay_stats(const or_options_t *old_options, bool *print_notice_out)
#define LD_GENERAL
Definition: log.h:62
int ExitPortStatistics
void rep_hist_conn_stats_term(void)
Definition: rephist.c:2320
uint64_t MaxMemInQueues
struct config_line_t * RendConfigLines
int any_predicted_circuits(time_t now)
int accounting_is_enabled(const or_options_t *options)
Definition: hibernate.c:305
uint64_t RelayBandwidthRate
int options_act_relay_bandwidth(const or_options_t *old_options)
dirinfo_type_t PublishServerDescriptor_
#define CONN_TYPE_OR_LISTENER
Definition: connection.h:39
Header file for config.c.
Header file for nickname.c.
void rep_hist_buffer_stats_init(time_t now)
Definition: rephist.c:1815
int ConnDirectionStatistics
struct config_line_t * DirPort_lines
const or_options_t * get_options(void)
Definition: config.c:926
Header file for cpuworker.c.
void geoip_dirreq_stats_init(time_t now)
Definition: geoip_stats.c:918
int should_record_bridge_info(const or_options_t *options)
Definition: geoip_stats.c:112
int strcmpstart(const char *s1, const char *s2)
Definition: util_string.c:206
static int check_server_ports(const smartlist_t *ports, const or_options_t *options, int *n_low_ports_out)
Definition: relay_config.c:140
#define tor_free(p)
Definition: malloc.h:52
void rep_hist_buffer_stats_term(void)
Definition: rephist.c:1907
void rep_hist_exit_stats_init(time_t now)
Definition: rephist.c:1558
int is_legal_nickname_or_hexdigest(const char *s)
Definition: nickname.c:31
int geoip_is_loaded(sa_family_t family)
Definition: geoip.c:458
const char * name
Definition: config.c:2441
void relay_config_free_all(void)
Definition: relay_config.c:83
Header file for mainloop.c.
dirinfo_type_t
Definition: or.h:891
int config_count_key(const config_line_t *a, const char *key)
Definition: confline.c:274
smartlist_t * smartlist_new(void)
Header for meminfo.c.
STATIC int check_bridge_distribution_setting(const char *bd)
Definition: relay_config.c:477
#define bool_eq(a, b)
Definition: logic.h:16
int dir_server_mode(const or_options_t *options)
Definition: routermode.c:23
Header file for geoip.c.
void options_act_relay_stats_msg(void)
#define STATIC
Definition: testsupport.h:32
int options_validate_relay_info(const or_options_t *old_options, or_options_t *options, char **msg)
Definition: relay_config.c:377
int options_act_bridge_stats(const or_options_t *old_options)
void geoip_dirreq_stats_term(void)
Definition: geoip_stats.c:957
int options_act_relay_dir(const or_options_t *old_options)
Header file for directory authority mode.
int options_validate_publish_server(const or_options_t *old_options, or_options_t *options, char **msg)
Definition: relay_config.c:513
void geoip_entry_stats_init(time_t now)
Definition: geoip_stats.c:1320
Header file for hibernate.c.
int SigningKeyLifetime
int options_validate_relay_padding(const or_options_t *old_options, or_options_t *options, char **msg)
Definition: relay_config.c:570
struct config_line_t * MyFamily
int options_act_relay_desc(const or_options_t *old_options)
struct config_line_t * ExtORPort_lines
int TestingAuthKeySlop
Header for setuid.c.
void configure_accounting(time_t now)
Definition: hibernate.c:430
int init_keys(void)
Definition: router.c:920
int TestingAuthKeyLifetime
const char * get_uname(void)
Definition: uname.c:33
uint32_t relay_get_effective_bwburst(const or_options_t *options)
Definition: relay_config.c:105
uint64_t BandwidthRate
Header file for routermode.c.
int options_validate_relay_mode(const or_options_t *old_options, or_options_t *options, char **msg)
Definition: relay_config.c:885
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
const char * fmt_addrport(const tor_addr_t *addr, uint16_t port)
Definition: address.c:1169
void cpu_init(void)
Definition: cpuworker.c:83
int options_validate_relay_os(const or_options_t *old_options, or_options_t *options, char **msg)
Definition: relay_config.c:340
int string_is_utf8(const char *str, size_t len)
Definition: util_string.c:507
Master header file for Tor-specific functionality.
uint64_t MaxAdvertisedBandwidth
int accounting_parse_options(const or_options_t *options, int validate_only)
Definition: hibernate.c:190
Header file for circuitbuild.c.
int port_parse_ports_relay(or_options_t *options, char **msg, smartlist_t *ports_out, int *have_low_ports_out)
Definition: relay_config.c:238
int options_act_relay(const or_options_t *old_options)
Header file for rephist.c.
int port_parse_config(smartlist_t *out, const config_line_t *ports, const char *portname, int listener_type, const char *defaultaddr, int defaultport, const unsigned flags)
Definition: config.c:6074
int TestingLinkCertLifetime
int consdiffmgr_validate(void)
Definition: consdiffmgr.c:858
void rep_hist_hs_stats_term(void)
Definition: rephist.c:2549
void rep_hist_hs_stats_init(time_t now)
Definition: rephist.c:2520
void mark_my_descriptor_dirty(const char *reason)
Definition: router.c:2422
int options_validate_relay_accounting(const or_options_t *old_options, or_options_t *options, char **msg)
Definition: relay_config.c:704
void ip_address_changed(int on_client_conn)
Definition: mainloop.c:2286
void cpuworkers_rotate_keyinfo(void)
Definition: cpuworker.c:188
void port_warn_nonlocal_ext_orports(const smartlist_t *ports, const char *portname)
Definition: relay_config.c:117
uint64_t AccountingMax
char * BridgeDistribution
int ConstrainedSockets
int is_legal_nickname(const char *s)
Definition: nickname.c:19
#define UNNAMED_ROUTER_NICKNAME
Definition: or.h:560
int options_act_relay_dos(const or_options_t *old_options)
int have_capability_support(void)
Definition: setuid.c:149
char * Nickname
Definition: or_options_st.h:73
uint64_t PerConnBWBurst
void geoip_bridge_stats_init(time_t now)
Definition: geoip_stats.c:1064
void rep_hist_conn_stats_init(time_t now)
Definition: rephist.c:2222
void geoip_entry_stats_term(void)
Definition: geoip_stats.c:1336
#define SMARTLIST_FOREACH(sl, type, var, cmd)
static int normalize_nickname_list(config_line_t **normalized_out, const config_line_t *lst, const char *name, char **msg)
Definition: relay_config.c:759
const char * escaped(const char *s)
Definition: escape.c:126
uint64_t RelayBandwidthBurst
#define ARRAY_LENGTH(x)
int get_total_system_memory(size_t *mem_out)
Definition: meminfo.c:151
uint64_t BandwidthBurst
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
int ReducedCircuitPadding
const char * relay_get_dirportfrontpage(void)
Definition: relay_config.c:74
Definition: or.h:894
struct config_line_t * ORPort_lines
Header for confmgt.c.
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43
void connection_or_update_token_buckets(smartlist_t *conns, const or_options_t *options)
void connection_bucket_adjust(const or_options_t *options)
Definition: connection.c:3544
void geoip_bridge_stats_term(void)
Definition: geoip_stats.c:1072
Header file for connection_or.c.
int options_validate_relay_bandwidth(const or_options_t *old_options, or_options_t *options, char **msg)
Definition: relay_config.c:612
STATIC int have_enough_mem_for_dircache(const or_options_t *options, size_t total_mem, char **msg)
Definition: relay_config.c:842
int HiddenServiceStatistics
static int options_transition_affects_descriptor(const or_options_t *old_options, const or_options_t *new_options)
or_options_t * get_options_mutable(void)
Definition: config.c:917
int server_mode(const or_options_t *options)
Definition: routermode.c:34
void port_update_port_set_relay(or_options_t *options, const smartlist_t *ports)
Definition: relay_config.c:311
struct smartlist_t * PublishServerDescriptor
uint32_t relay_get_effective_bwrate(const or_options_t *options)
Definition: relay_config.c:91
#define CONN_TYPE_EXT_OR_LISTENER
Definition: connection.h:71
Header for consdiffmgr.c.
#define LD_BUG
Definition: log.h:86
Header file for dns.c.
Header file for predict_ports.c.
struct config_line_t * MyFamily_lines
#define LD_CONFIG
Definition: log.h:68
int inform_testing_reachability(void)
Definition: circuitbuild.c:774
int options_validate_relay_testing(const or_options_t *old_options, or_options_t *options, char **msg)
Definition: relay_config.c:970
int have_completed_a_circuit(void)
Definition: mainloop.c:219
Listener port configuration structure.
int TestingSigningKeySlop
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)
int KeepBindCapabilities
static char * global_dirfrontpagecontents
Definition: relay_config.c:49