config.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * /file config.c
  6. *
  7. * /brief Code to parse and interpret configuration files.
  8. *
  9. **/
  10. #include "or.h"
  11. /** Enumeration of types which option values can take */
  12. typedef enum config_type_t {
  13. CONFIG_TYPE_STRING = 0, /**< An arbitrary string. */
  14. CONFIG_TYPE_INT, /**< An integer */
  15. CONFIG_TYPE_DOUBLE, /**< A floating-point value */
  16. CONFIG_TYPE_BOOL, /**< A boolean value, expressed as 0 or 1. */
  17. CONFIG_TYPE_LINELIST, /**< Uninterpreted config lines */
  18. } config_type_t;
  19. /** Largest allowed config line */
  20. #define CONFIG_LINE_T_MAXLEN 4096
  21. static FILE *config_open(const unsigned char *filename);
  22. static int config_close(FILE *f);
  23. static struct config_line_t *config_get_commandlines(int argc, char **argv);
  24. static struct config_line_t *config_get_lines(FILE *f);
  25. static void config_free_lines(struct config_line_t *front);
  26. static int config_compare(struct config_line_t *c, const char *key, config_type_t type, void *arg);
  27. static int config_assign(or_options_t *options, struct config_line_t *list);
  28. /** Open a configuration file for reading */
  29. static FILE *config_open(const unsigned char *filename) {
  30. tor_assert(filename);
  31. if (strspn(filename,CONFIG_LEGAL_FILENAME_CHARACTERS) != strlen(filename)) {
  32. /* filename has illegal letters */
  33. return NULL;
  34. }
  35. return fopen(filename, "r");
  36. }
  37. /** Close the configuration file */
  38. static int config_close(FILE *f) {
  39. tor_assert(f);
  40. return fclose(f);
  41. }
  42. /** Helper: Read a list of configuration options from the command line. */
  43. static struct config_line_t *config_get_commandlines(int argc, char **argv) {
  44. struct config_line_t *new;
  45. struct config_line_t *front = NULL;
  46. char *s;
  47. int i = 1;
  48. while(i < argc-1) {
  49. if(!strcmp(argv[i],"-f")) {
  50. // log(LOG_DEBUG,"Commandline: skipping over -f.");
  51. i+=2; /* this is the config file option. ignore it. */
  52. continue;
  53. }
  54. new = tor_malloc(sizeof(struct config_line_t));
  55. s = argv[i];
  56. while(*s == '-')
  57. s++;
  58. new->key = tor_strdup(s);
  59. new->value = tor_strdup(argv[i+1]);
  60. log(LOG_DEBUG,"Commandline: parsed keyword '%s', value '%s'",
  61. new->key, new->value);
  62. new->next = front;
  63. front = new;
  64. i += 2;
  65. }
  66. return front;
  67. }
  68. /** Helper: allocate a new configuration option mapping 'key' to 'val',
  69. * prepend it to 'front', and return the newly allocated config_line_t */
  70. static struct config_line_t *
  71. config_line_prepend(struct config_line_t *front,
  72. const char *key,
  73. const char *val)
  74. {
  75. struct config_line_t *newline;
  76. newline = tor_malloc(sizeof(struct config_line_t));
  77. newline->key = tor_strdup(key);
  78. newline->value = tor_strdup(val);
  79. newline->next = front;
  80. return newline;
  81. }
  82. /** Helper: parse the config file and strdup into key/value
  83. * strings. Return list, or NULL if parsing the file failed. Warn and
  84. * ignore any misformatted lines. */
  85. static struct config_line_t *config_get_lines(FILE *f) {
  86. struct config_line_t *front = NULL;
  87. char line[CONFIG_LINE_T_MAXLEN];
  88. int result;
  89. char *key, *value;
  90. while( (result=parse_line_from_file(line,sizeof(line),f,&key,&value)) > 0) {
  91. front = config_line_prepend(front, key, value);
  92. }
  93. if(result < 0)
  94. return NULL;
  95. return front;
  96. }
  97. /**
  98. * Free all the configuration lines on the linked list <b>front</b>.
  99. */
  100. static void config_free_lines(struct config_line_t *front) {
  101. struct config_line_t *tmp;
  102. while(front) {
  103. tmp = front;
  104. front = tmp->next;
  105. free(tmp->key);
  106. free(tmp->value);
  107. free(tmp);
  108. }
  109. }
  110. /** Search the linked list <b>c</b> for any option whose key is <b>key</b>.
  111. * If such an option is found, interpret it as of type <b>type</b>, and store
  112. * the result in <b>arg</b>. If the option is misformatted, log a warning and
  113. * skip it.
  114. */
  115. static int config_compare(struct config_line_t *c, const char *key, config_type_t type, void *arg) {
  116. int i;
  117. if(strncasecmp(c->key,key,strlen(c->key)))
  118. return 0;
  119. if(strcasecmp(c->key,key)) {
  120. tor_free(c->key);
  121. c->key = tor_strdup(key);
  122. }
  123. /* it's a match. cast and assign. */
  124. log_fn(LOG_DEBUG,"Recognized keyword '%s' as %s, using value '%s'.",c->key,key,c->value);
  125. switch(type) {
  126. case CONFIG_TYPE_INT:
  127. *(int *)arg = atoi(c->value);
  128. break;
  129. case CONFIG_TYPE_BOOL:
  130. i = atoi(c->value);
  131. if (i != 0 && i != 1) {
  132. log(LOG_WARN, "Boolean keyword '%s' expects 0 or 1", c->key);
  133. return 0;
  134. }
  135. *(int *)arg = i;
  136. break;
  137. case CONFIG_TYPE_STRING:
  138. tor_free(*(char **)arg);
  139. *(char **)arg = tor_strdup(c->value);
  140. break;
  141. case CONFIG_TYPE_DOUBLE:
  142. *(double *)arg = atof(c->value);
  143. break;
  144. case CONFIG_TYPE_LINELIST:
  145. /* Note: this reverses the order that the lines appear in. That's
  146. * just fine, since we build up the list of lines reversed in the
  147. * first place. */
  148. *(struct config_line_t**)arg =
  149. config_line_prepend(*(struct config_line_t**)arg, c->key, c->value);
  150. break;
  151. }
  152. return 1;
  153. }
  154. /** Iterate through the linked list of options <b>list</b>.
  155. * For each item, convert as appropriate and assign to <b>options</b>.
  156. * If an item is unrecognized, return -1 immediately,
  157. * else return 0 for success. */
  158. static int config_assign(or_options_t *options, struct config_line_t *list) {
  159. while(list) {
  160. if(
  161. /* order matters here! abbreviated arguments use the first match. */
  162. /* string options */
  163. config_compare(list, "Address", CONFIG_TYPE_STRING, &options->Address) ||
  164. config_compare(list, "BandwidthRate", CONFIG_TYPE_INT, &options->BandwidthRate) ||
  165. config_compare(list, "BandwidthBurst", CONFIG_TYPE_INT, &options->BandwidthBurst) ||
  166. config_compare(list, "DebugLogFile", CONFIG_TYPE_STRING, &options->DebugLogFile) ||
  167. config_compare(list, "DataDirectory", CONFIG_TYPE_STRING, &options->DataDirectory) ||
  168. config_compare(list, "DirPort", CONFIG_TYPE_INT, &options->DirPort) ||
  169. config_compare(list, "DirBindAddress", CONFIG_TYPE_STRING, &options->DirBindAddress) ||
  170. config_compare(list, "DirFetchPostPeriod",CONFIG_TYPE_INT, &options->DirFetchPostPeriod) ||
  171. config_compare(list, "ExitNodes", CONFIG_TYPE_STRING, &options->ExitNodes) ||
  172. config_compare(list, "EntryNodes", CONFIG_TYPE_STRING, &options->EntryNodes) ||
  173. config_compare(list, "ExitPolicy", CONFIG_TYPE_STRING, &options->ExitPolicy) ||
  174. config_compare(list, "ExcludeNodes", CONFIG_TYPE_STRING, &options->ExcludeNodes) ||
  175. config_compare(list, "Group", CONFIG_TYPE_STRING, &options->Group) ||
  176. config_compare(list, "IgnoreVersion", CONFIG_TYPE_BOOL, &options->IgnoreVersion) ||
  177. config_compare(list, "KeepalivePeriod",CONFIG_TYPE_INT, &options->KeepalivePeriod) ||
  178. config_compare(list, "LogLevel", CONFIG_TYPE_LINELIST, &options->LogOptions) ||
  179. config_compare(list, "LogFile", CONFIG_TYPE_LINELIST, &options->LogOptions) ||
  180. config_compare(list, "LinkPadding", CONFIG_TYPE_BOOL, &options->LinkPadding) ||
  181. config_compare(list, "MaxConn", CONFIG_TYPE_INT, &options->MaxConn) ||
  182. config_compare(list, "MaxOnionsPending",CONFIG_TYPE_INT, &options->MaxOnionsPending) ||
  183. config_compare(list, "Nickname", CONFIG_TYPE_STRING, &options->Nickname) ||
  184. config_compare(list, "NewCircuitPeriod",CONFIG_TYPE_INT, &options->NewCircuitPeriod) ||
  185. config_compare(list, "NumCpus", CONFIG_TYPE_INT, &options->NumCpus) ||
  186. config_compare(list, "ORPort", CONFIG_TYPE_INT, &options->ORPort) ||
  187. config_compare(list, "ORBindAddress", CONFIG_TYPE_STRING, &options->ORBindAddress) ||
  188. config_compare(list, "PidFile", CONFIG_TYPE_STRING, &options->PidFile) ||
  189. config_compare(list, "PathlenCoinWeight",CONFIG_TYPE_DOUBLE, &options->PathlenCoinWeight) ||
  190. config_compare(list, "RouterFile", CONFIG_TYPE_STRING, &options->RouterFile) ||
  191. config_compare(list, "RunAsDaemon", CONFIG_TYPE_BOOL, &options->RunAsDaemon) ||
  192. config_compare(list, "RecommendedVersions",CONFIG_TYPE_STRING, &options->RecommendedVersions) ||
  193. config_compare(list, "RendNodes", CONFIG_TYPE_STRING, &options->RendNodes) ||
  194. config_compare(list, "RendExcludeNodes",CONFIG_TYPE_STRING, &options->RendExcludeNodes) ||
  195. config_compare(list, "SocksPort", CONFIG_TYPE_INT, &options->SocksPort) ||
  196. config_compare(list, "SocksBindAddress",CONFIG_TYPE_STRING,&options->SocksBindAddress) ||
  197. config_compare(list, "TrafficShaping", CONFIG_TYPE_BOOL, &options->TrafficShaping) ||
  198. config_compare(list, "User", CONFIG_TYPE_STRING, &options->User) ||
  199. config_compare(list, "RunTesting", CONFIG_TYPE_BOOL, &options->RunTesting) ||
  200. config_compare(list, "HiddenServiceDir", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  201. config_compare(list, "HiddenServicePort", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  202. config_compare(list, "HiddenServiceNodes", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  203. config_compare(list, "HiddenServiceExcludeNodes", CONFIG_TYPE_LINELIST, &options->RendConfigLines)
  204. ) {
  205. /* then we're ok. it matched something. */
  206. } else {
  207. log_fn(LOG_WARN,"Unknown keyword '%s'. Failing.",list->key);
  208. return -1;
  209. }
  210. list = list->next;
  211. }
  212. return 0;
  213. }
  214. /* XXX are there any other specifiers we want to give so making
  215. * a several-thousand-byte string is less painful? */
  216. const char default_dirservers_string[] =
  217. "router moria1 18.244.0.188 9001 9021 9031\n"
  218. "platform Tor 0.0.6rc1 on Linux moria.mit.edu i686\n"
  219. "published 2004-04-25 21:54:28\n"
  220. "bandwidth 800000 10000000\n"
  221. "onion-key\n"
  222. "-----BEGIN RSA PUBLIC KEY-----\n"
  223. "MIGJAoGBANoIvHieyHUTzIacbnWOnyTyzGrLOdXqbcjz2GGMxyHEd5K1bO1ZBNHP\n"
  224. "9i5qLQpN5viFk2K2rEGuG8tFgDEzSWZEtBqv3NVfUdiumdERWMBwlaQ0MVK4C+jf\n"
  225. "y5gZ8KI3o9ZictgPS1AQF+Kk932/vIHTuRIUKb4ILTnQilNvID0NAgMBAAE=\n"
  226. "-----END RSA PUBLIC KEY-----\n"
  227. "signing-key\n"
  228. "-----BEGIN RSA PUBLIC KEY-----\n"
  229. "MIGJAoGBAMHa0ZC/jo2Q2DrwKYF/6ZbmZ27PFYG91u4gUzzmZ/VXLpZ8wNzEV3oW\n"
  230. "nt+I61048fBiC1frT1/DZ351n2bLSk9zJbB6jyGZJn0380FPRX3+cXyXS0Gq8Ril\n"
  231. "xkhMQf5XuNFUb8UmYPSOH4WErjvYjKvU+gfjbK/82Jo9SuHpYz+BAgMBAAE=\n"
  232. "-----END RSA PUBLIC KEY-----\n"
  233. "reject 0.0.0.0/255.0.0.0:*\n"
  234. "reject 169.254.0.0/255.255.0.0:*\n"
  235. "reject 127.0.0.0/255.0.0.0:*\n"
  236. "reject 192.168.0.0/255.255.0.0:*\n"
  237. "reject 10.0.0.0/255.0.0.0:*\n"
  238. "reject 172.16.0.0/255.240.0.0:*\n"
  239. "accept *:20-22\n"
  240. "accept *:53\n"
  241. "accept *:79-80\n"
  242. "accept *:110\n"
  243. "accept *:143\n"
  244. "accept *:443\n"
  245. "accept *:873\n"
  246. "accept *:993\n"
  247. "accept *:995\n"
  248. "accept *:1024-65535\n"
  249. "reject *:*\n"
  250. "router-signature\n"
  251. "-----BEGIN SIGNATURE-----\n"
  252. "o1eAoRHDAEAXsnh5wN++vIwrupd+DbAJ2p3wxHDrmqxTpygzxxCnyQyhMfX03ua2\n"
  253. "4iplyNlwyFwzWcw0sk31otlO2HBYXT1V9G0YxGtKMOeOBMHjfGbUjGvEALHzWi4z\n"
  254. "8DXGJp13zgnUyP4ZA6xaGROwcT6oB5e7UlztvvpGxTg=\n"
  255. "-----END SIGNATURE-----\n"
  256. "\n"
  257. "router moria2 18.244.0.188 9002 9022 9032\n"
  258. "platform Tor 0.0.6rc1 on Linux moria.mit.edu i686\n"
  259. "published 2004-04-25 21:54:30\n"
  260. "bandwidth 800000 10000000\n"
  261. "onion-key\n"
  262. "-----BEGIN RSA PUBLIC KEY-----\n"
  263. "MIGJAoGBAM4Cc/npgYC54XrYLC+grVxJp7PDmNO2DRRJOxKttBBtvLpnR1UaueTi\n"
  264. "kyknT5kmlx+ihgZF/jmye//2dDUp2+kK/kSkpRV4xnDLXZmed+sNSQxqmm9TtZQ9\n"
  265. "/hjpxhp5J9HmUTYhntBs+4E4CUKokmrI6oRLoln4SA39AX9QLPcnAgMBAAE=\n"
  266. "-----END RSA PUBLIC KEY-----\n"
  267. "signing-key\n"
  268. "-----BEGIN RSA PUBLIC KEY-----\n"
  269. "MIGJAoGBAOcrht/y5rkaahfX7sMe2qnpqoPibsjTSJaDvsUtaNP/Bq0MgNDGOR48\n"
  270. "rtwfqTRff275Edkp/UYw3G3vSgKCJr76/bqOHCmkiZrnPV1zxNfrK18gNw2Cxre0\n"
  271. "nTA+fD8JQqpPtb8b0SnG9kwy75eS//sRu7TErie2PzGMxrf9LH0LAgMBAAE=\n"
  272. "-----END RSA PUBLIC KEY-----\n"
  273. "reject 0.0.0.0/255.0.0.0:*\n"
  274. "reject 169.254.0.0/255.255.0.0:*\n"
  275. "reject 127.0.0.0/255.0.0.0:*\n"
  276. "reject 192.168.0.0/255.255.0.0:*\n"
  277. "reject 10.0.0.0/255.0.0.0:*\n"
  278. "reject 172.16.0.0/255.240.0.0:*\n"
  279. "accept *:20-22\n"
  280. "accept *:53\n"
  281. "accept *:79-80\n"
  282. "accept *:110\n"
  283. "accept *:143\n"
  284. "accept *:443\n"
  285. "accept *:873\n"
  286. "accept *:993\n"
  287. "accept *:995\n"
  288. "accept *:1024-65535\n"
  289. "reject *:*\n"
  290. "router-signature\n"
  291. "-----BEGIN SIGNATURE-----\n"
  292. "RKROLwP1ExjTZeg6wuN0pzYqed9IJUd5lAe9hp4ritbnmJAgS6qfww6jgx61CfUR\n"
  293. "6SElhOLE7Q77jAdoL45Ji5pn/Y+Q+E+5lJm1E/ed9ha+YsOPaOc7z6GQ7E4mihCL\n"
  294. "gI1vsw92+P1Ty4RHj6fyD9DhbV19nh2Qs+pvGJOS2FY=\n"
  295. "-----END SIGNATURE-----\n"
  296. "\n"
  297. "router tor26 62.116.124.106 9001 9050 9030\n"
  298. "platform Tor 0.0.6 on Linux seppia i686\n"
  299. "published 2004-05-06 21:33:23\n"
  300. "bandwidth 500000 10000000\n"
  301. "onion-key\n"
  302. "-----BEGIN RSA PUBLIC KEY-----\n"
  303. "MIGJAoGBAMEHdDnpj3ik1AF1xe/VqjoguH2DbANifYqXXfempu0fS+tU9FGo6dU/\n"
  304. "fnVHAZwL9Ek9k2rMzumShi1RduK9p035R/Gk+PBBcLfvwYJ/Nat+ZO/L8jn/3bZe\n"
  305. "ieQd9CKj2LjNGKpRNry37vkwMGIOIlegwK+2us8aXJ7sIvlNts0TAgMBAAE=\n"
  306. "-----END RSA PUBLIC KEY-----\n"
  307. "signing-key\n"
  308. "-----BEGIN RSA PUBLIC KEY-----\n"
  309. "MIGJAoGBAMQgV2gXLbXgesWgeAsj8P1Uvm/zibrFXqwDq27lLKNgWGYGX2ax3LyT\n"
  310. "3nzI1Y5oLs4kPKTsMM5ft9aokwf417lKoCRlZc9ptfRbgxDx90c9GtWVmkrmDvCK\n"
  311. "ae59TMoXIiGfZiwWT6KKq5Zm9/Fu2Il3B2vHGkKJYKixmiBJRKp/AgMBAAE=\n"
  312. "-----END RSA PUBLIC KEY-----\n"
  313. "accept 62.245.184.24:25\n"
  314. "accept 62.116.124.106:6666-6670\n"
  315. "accept *:48099\n"
  316. "reject *:*\n"
  317. "router-signature\n"
  318. "-----BEGIN SIGNATURE-----\n"
  319. "qh/xRoqfLNFzPaB8VdpbdMAwRyuk5qjx4LeLVQ2pDwTZ55PqmG99+VKUNte2WTTD\n"
  320. "7dZEA7um2rueohGe4nYmvbhJWr20/I0ZxmWDRDvFy0b5nwzDMGvLvDw95Zu/XJQ2\n"
  321. "md32NE3y9VZCfbCN+GlvETX3fdR3Svzcm8Kzesg2/s4=\n"
  322. "-----END SIGNATURE-----\n"
  323. ;
  324. int config_assign_default_dirservers(void) {
  325. if(router_load_routerlist_from_string(default_dirservers_string, 1) < 0) {
  326. log_fn(LOG_WARN,"Bug: the default dirservers internal string is corrupt.");
  327. return -1;
  328. }
  329. return 0;
  330. }
  331. /** Set <b>options</b> to a reasonable default.
  332. *
  333. * Call this function when they're using the default torrc but
  334. * we can't find it. For now, we just hard-code what comes in the
  335. * default torrc.
  336. */
  337. static int config_assign_default(or_options_t *options) {
  338. /* set them up as a client only */
  339. options->SocksPort = 9050;
  340. /* plus give them a dirservers file */
  341. if(config_assign_default_dirservers() < 0)
  342. return -1;
  343. return 0;
  344. }
  345. /** Print a usage message for tor. */
  346. static void print_usage(void) {
  347. printf("tor -f <torrc> [args]\n"
  348. "See man page for more options. This -h is probably obsolete.\n\n"
  349. "-b <bandwidth>\t\tbytes/second rate limiting\n"
  350. "-d <file>\t\tDebug file\n"
  351. // "-m <max>\t\tMax number of connections\n"
  352. "-l <level>\t\tLog level\n"
  353. "-r <file>\t\tList of known routers\n");
  354. printf("\nClient options:\n"
  355. "-e \"nick1 nick2 ...\"\t\tExit nodes\n"
  356. "-s <IP>\t\t\tPort to bind to for Socks\n"
  357. );
  358. printf("\nServer options:\n"
  359. "-n <nick>\t\tNickname of router\n"
  360. "-o <port>\t\tOR port to bind to\n"
  361. "-p <file>\t\tPID file\n"
  362. );
  363. }
  364. /**
  365. * Adjust <b>options</b> to contain a reasonable value for Address.
  366. */
  367. static int resolve_my_address(or_options_t *options) {
  368. struct in_addr in;
  369. struct hostent *rent;
  370. char localhostname[256];
  371. int explicit_ip=1;
  372. if(!options->Address) { /* then we need to guess our address */
  373. explicit_ip = 0; /* it's implicit */
  374. if(gethostname(localhostname,sizeof(localhostname)) < 0) {
  375. log_fn(LOG_WARN,"Error obtaining local hostname");
  376. return -1;
  377. }
  378. #if 0 /* don't worry about complaining, as long as it resolves */
  379. if(!strchr(localhostname,'.')) {
  380. log_fn(LOG_WARN,"fqdn '%s' has only one element. Misconfigured machine?",address);
  381. log_fn(LOG_WARN,"Try setting the Address line in your config file.");
  382. return -1;
  383. }
  384. #endif
  385. options->Address = tor_strdup(localhostname);
  386. log_fn(LOG_DEBUG,"Guessed local host name as '%s'",options->Address);
  387. }
  388. /* now we know options->Address is set. resolve it and keep only the IP */
  389. if(tor_inet_aton(options->Address, &in) == 0) {
  390. /* then we have to resolve it */
  391. explicit_ip = 0;
  392. rent = (struct hostent *)gethostbyname(options->Address);
  393. if (!rent) {
  394. log_fn(LOG_WARN,"Could not resolve Address %s. Failing.", options->Address);
  395. return -1;
  396. }
  397. tor_assert(rent->h_length == 4);
  398. memcpy(&in.s_addr, rent->h_addr,rent->h_length);
  399. }
  400. if(!explicit_ip && is_internal_IP(htonl(in.s_addr))) {
  401. log_fn(LOG_WARN,"Address '%s' resolves to private IP '%s'. "
  402. "Please set the Address config option to be the IP you want to use.",
  403. options->Address, inet_ntoa(in));
  404. return -1;
  405. }
  406. tor_free(options->Address);
  407. options->Address = tor_strdup(inet_ntoa(in));
  408. log_fn(LOG_DEBUG,"Resolved Address to %s.", options->Address);
  409. return 0;
  410. }
  411. /** Release storage held by <b>options</b> */
  412. static void free_options(or_options_t *options) {
  413. config_free_lines(options->LogOptions);
  414. tor_free(options->DebugLogFile);
  415. tor_free(options->DataDirectory);
  416. tor_free(options->RouterFile);
  417. tor_free(options->Nickname);
  418. tor_free(options->Address);
  419. tor_free(options->PidFile);
  420. tor_free(options->ExitNodes);
  421. tor_free(options->EntryNodes);
  422. tor_free(options->ExcludeNodes);
  423. tor_free(options->RendNodes);
  424. tor_free(options->RendExcludeNodes);
  425. tor_free(options->ExitPolicy);
  426. tor_free(options->SocksBindAddress);
  427. tor_free(options->ORBindAddress);
  428. tor_free(options->DirBindAddress);
  429. tor_free(options->RecommendedVersions);
  430. tor_free(options->User);
  431. tor_free(options->Group);
  432. config_free_lines(options->RendConfigLines);
  433. }
  434. /** Set <b>options</b> to hold reasonable defaults for most options. */
  435. static void init_options(or_options_t *options) {
  436. /* give reasonable values for each option. Defaults to zero. */
  437. memset(options,0,sizeof(or_options_t));
  438. options->LogOptions = NULL;
  439. options->ExitNodes = tor_strdup("");
  440. options->EntryNodes = tor_strdup("");
  441. options->ExcludeNodes = tor_strdup("");
  442. options->RendNodes = tor_strdup("");
  443. options->RendExcludeNodes = tor_strdup("");
  444. options->ExitPolicy = tor_strdup("");
  445. options->SocksBindAddress = tor_strdup("127.0.0.1");
  446. options->ORBindAddress = tor_strdup("0.0.0.0");
  447. options->DirBindAddress = tor_strdup("0.0.0.0");
  448. options->RecommendedVersions = NULL;
  449. options->PidFile = NULL; // tor_strdup("tor.pid");
  450. options->DataDirectory = NULL;
  451. options->PathlenCoinWeight = 0.3;
  452. options->MaxConn = 900;
  453. options->DirFetchPostPeriod = 600;
  454. options->KeepalivePeriod = 300;
  455. options->MaxOnionsPending = 100;
  456. options->NewCircuitPeriod = 30; /* twice a minute */
  457. options->BandwidthRate = 800000; /* at most 800kB/s total sustained incoming */
  458. options->BandwidthBurst = 10000000; /* max burst on the token bucket */
  459. options->NumCpus = 1;
  460. options->RendConfigLines = NULL;
  461. }
  462. /** Read a configuration file into <b>options</b>, finding the configuration
  463. * file location based on the command line. After loading the options,
  464. * validate them for consistency. Return 0 if success, <0 if failure. */
  465. int getconfig(int argc, char **argv, or_options_t *options) {
  466. struct config_line_t *cl;
  467. FILE *cf;
  468. char *fname;
  469. int i;
  470. int result = 0;
  471. static int first_load = 1;
  472. static char **backup_argv;
  473. static int backup_argc;
  474. char *previous_pidfile = NULL;
  475. int previous_runasdaemon = 0;
  476. int previous_orport = -1;
  477. int using_default_torrc;
  478. if(first_load) { /* first time we're called. save commandline args */
  479. backup_argv = argv;
  480. backup_argc = argc;
  481. first_load = 0;
  482. } else { /* we're reloading. need to clean up old ones first. */
  483. argv = backup_argv;
  484. argc = backup_argc;
  485. /* record some previous values, so we can fail if they change */
  486. if(options->PidFile)
  487. previous_pidfile = tor_strdup(options->PidFile);
  488. previous_runasdaemon = options->RunAsDaemon;
  489. previous_orport = options->ORPort;
  490. free_options(options);
  491. }
  492. init_options(options);
  493. if(argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1],"--help"))) {
  494. print_usage();
  495. exit(0);
  496. }
  497. if(argc > 1 && (!strcmp(argv[1],"--version"))) {
  498. printf("Tor version %s.\n",VERSION);
  499. exit(0);
  500. }
  501. /* learn config file name, get config lines, assign them */
  502. i = 1;
  503. while(i < argc-1 && strcmp(argv[i],"-f")) {
  504. i++;
  505. }
  506. if(i < argc-1) { /* we found one */
  507. fname = argv[i+1];
  508. using_default_torrc = 0;
  509. } else { /* didn't find one, try CONFDIR */
  510. fname = CONFDIR "/torrc";
  511. using_default_torrc = 1;
  512. }
  513. log(LOG_DEBUG,"Opening config file '%s'",fname);
  514. cf = config_open(fname);
  515. if(!cf) {
  516. if(using_default_torrc == 1) {
  517. log(LOG_NOTICE, "Configuration file '%s' not present, using reasonable defaults.",fname);
  518. if(config_assign_default(options) < 0)
  519. return -1;
  520. } else {
  521. log(LOG_WARN, "Unable to open configuration file '%s'.",fname);
  522. return -1;
  523. }
  524. } else { /* it opened successfully. use it. */
  525. cl = config_get_lines(cf);
  526. if(!cl) return -1;
  527. if(config_assign(options,cl) < 0)
  528. return -1;
  529. config_free_lines(cl);
  530. config_close(cf);
  531. }
  532. /* go through command-line variables too */
  533. cl = config_get_commandlines(argc,argv);
  534. if(config_assign(options,cl) < 0)
  535. return -1;
  536. config_free_lines(cl);
  537. /* Validate options */
  538. /* first check if any of the previous options have changed but aren't allowed to */
  539. if(previous_pidfile && strcmp(previous_pidfile,options->PidFile)) {
  540. log_fn(LOG_WARN,"During reload, PidFile changed from %s to %s. Failing.",
  541. previous_pidfile, options->PidFile);
  542. return -1;
  543. }
  544. tor_free(previous_pidfile);
  545. if(previous_runasdaemon && !options->RunAsDaemon) {
  546. log_fn(LOG_WARN,"During reload, change from RunAsDaemon=1 to =0 not allowed. Failing.");
  547. return -1;
  548. }
  549. if(previous_orport == 0 && options->ORPort > 0) {
  550. log_fn(LOG_WARN,"During reload, change from ORPort=0 to >0 not allowed. Failing.");
  551. return -1;
  552. }
  553. if(options->ORPort < 0) {
  554. log(LOG_WARN,"ORPort option can't be negative.");
  555. result = -1;
  556. }
  557. if(options->ORPort && options->DataDirectory == NULL) {
  558. log(LOG_WARN,"DataDirectory option required if ORPort is set, but not found.");
  559. result = -1;
  560. }
  561. if (options->ORPort) {
  562. if (options->Nickname == NULL) {
  563. log_fn(LOG_WARN,"Nickname required if ORPort is set, but not found.");
  564. result = -1;
  565. } else {
  566. if (strspn(options->Nickname, LEGAL_NICKNAME_CHARACTERS) !=
  567. strlen(options->Nickname)) {
  568. log_fn(LOG_WARN, "Nickname '%s' contains illegal characters.", options->Nickname);
  569. result = -1;
  570. }
  571. if (strlen(options->Nickname) > MAX_NICKNAME_LEN) {
  572. log_fn(LOG_WARN, "Nickname '%s' has more than %d characters.",
  573. options->Nickname, MAX_NICKNAME_LEN);
  574. result = -1;
  575. }
  576. }
  577. }
  578. if(options->ORPort) { /* get an IP for ourselves */
  579. if(resolve_my_address(options) < 0)
  580. result = -1;
  581. }
  582. if(options->SocksPort < 0) {
  583. log(LOG_WARN,"SocksPort option can't be negative.");
  584. result = -1;
  585. }
  586. if(options->SocksPort == 0 && options->ORPort == 0) {
  587. log(LOG_WARN,"SocksPort and ORPort are both undefined? Quitting.");
  588. result = -1;
  589. }
  590. if(options->DirPort < 0) {
  591. log(LOG_WARN,"DirPort option can't be negative.");
  592. result = -1;
  593. }
  594. if(options->DirPort && options->RecommendedVersions == NULL) {
  595. log(LOG_WARN,"Directory servers must configure RecommendedVersions.");
  596. result = -1;
  597. }
  598. if(options->SocksPort > 1 &&
  599. (options->PathlenCoinWeight < 0.0 || options->PathlenCoinWeight >= 1.0)) {
  600. log(LOG_WARN,"PathlenCoinWeight option must be >=0.0 and <1.0.");
  601. result = -1;
  602. }
  603. if(options->MaxConn < 1) {
  604. log(LOG_WARN,"MaxConn option must be a non-zero positive integer.");
  605. result = -1;
  606. }
  607. if(options->MaxConn >= MAXCONNECTIONS) {
  608. log(LOG_WARN,"MaxConn option must be less than %d.", MAXCONNECTIONS);
  609. result = -1;
  610. }
  611. if(options->DirFetchPostPeriod < 1) {
  612. log(LOG_WARN,"DirFetchPostPeriod option must be positive.");
  613. result = -1;
  614. }
  615. if(options->DirFetchPostPeriod > MIN_ONION_KEY_LIFETIME/2) {
  616. log(LOG_WARN,"DirFetchPostPeriod is too large; clipping.");
  617. options->DirFetchPostPeriod = MIN_ONION_KEY_LIFETIME/2;
  618. }
  619. if(options->KeepalivePeriod < 1) {
  620. log(LOG_WARN,"KeepalivePeriod option must be positive.");
  621. result = -1;
  622. }
  623. /* XXX look at the various nicknamelists and make sure they're
  624. * valid and don't have hostnames that are too long.
  625. */
  626. if (rend_config_services(options) < 0) {
  627. result = -1;
  628. }
  629. return result;
  630. }
  631. static void add_single_log(struct config_line_t *level_opt,
  632. struct config_line_t *file_opt,
  633. int isDaemon)
  634. {
  635. int levelMin=-1, levelMax=-1;
  636. char *cp, *tmp_sev;
  637. if (level_opt) {
  638. cp = strchr(level_opt->value, '-');
  639. if (cp) {
  640. tmp_sev = tor_strndup(level_opt->value, cp - level_opt->value);
  641. levelMin = parse_log_level(tmp_sev);
  642. if (levelMin<0) {
  643. log_fn(LOG_WARN, "Unrecognized log severity %s: must be one of err|warn|notice|info|debug", tmp_sev);
  644. }
  645. tor_free(tmp_sev);
  646. levelMax = parse_log_level(cp+1);
  647. if (levelMax<0) {
  648. log_fn(LOG_WARN, "Unrecognized log severity %s: must be one of err|warn|notice|info|debug", cp+1);
  649. }
  650. } else {
  651. levelMin = parse_log_level(level_opt->value);
  652. if (levelMin<0) {
  653. log_fn(LOG_WARN, "Unrecognized log severity %s: must be one of err|warn|notice|info|debug", level_opt->value);
  654. }
  655. }
  656. }
  657. if (levelMin < 0 && levelMax < 0) {
  658. levelMin = LOG_NOTICE;
  659. levelMax = LOG_ERR;
  660. } else if (levelMin < 0) {
  661. levelMin = levelMax;
  662. } else {
  663. levelMax = LOG_ERR;
  664. }
  665. if (file_opt) {
  666. if (add_file_log(levelMin, levelMax, file_opt->value) < 0) {
  667. /* opening the log file failed! Use stderr and log a warning */
  668. add_stream_log(levelMin, levelMax, "<stderr>", stderr);
  669. log_fn(LOG_WARN, "Cannot write to LogFile '%s': %s.", file_opt->value,
  670. strerror(errno));
  671. }
  672. log_fn(LOG_NOTICE, "Successfully opened LogFile '%s', redirecting output.",
  673. file_opt->value);
  674. } else if (!isDaemon) {
  675. add_stream_log(levelMin, levelMax, "<stderr>", stderr);
  676. }
  677. }
  678. /**
  679. * Initialize the logs based on the configuration file.
  680. */
  681. void config_init_logs(or_options_t *options)
  682. {
  683. /* The order of options is: Level? (File Level?)+
  684. */
  685. struct config_line_t *opt = options->LogOptions;
  686. /* Special case for if first option is LogLevel. */
  687. if (opt && !strcasecmp(opt->key, "LogLevel")) {
  688. if (opt->next && !strcasecmp(opt->next->key, "LogFile")) {
  689. add_single_log(opt, opt->next, options->RunAsDaemon);
  690. opt = opt->next->next;
  691. } else if (!opt->next) {
  692. add_single_log(opt, NULL, options->RunAsDaemon);
  693. return;
  694. } else {
  695. opt = opt->next;
  696. }
  697. }
  698. while (opt) {
  699. if (!strcasecmp(opt->key, "LogLevel")) {
  700. log_fn(LOG_WARN, "Two LogLevel options in a row without intervening LogFile");
  701. opt = opt->next;
  702. } else {
  703. assert(!strcasecmp(opt->key, "LogFile"));
  704. if (opt->next && !strcasecmp(opt->next->key, "LogLevel")) {
  705. /* LogFile followed by LogLevel */
  706. add_single_log(opt->next, opt, options->RunAsDaemon);
  707. opt = opt->next->next;
  708. } else {
  709. /* LogFile followed by LogFile or end of list. */
  710. add_single_log(NULL, opt, options->RunAsDaemon);
  711. opt = opt->next;
  712. }
  713. }
  714. }
  715. if (options->DebugLogFile) {
  716. log_fn(LOG_WARN, "DebugLogFile is deprecated; use LogFile and LogLevel instead");
  717. add_file_log(LOG_DEBUG, LOG_ERR, options->DebugLogFile);
  718. }
  719. }
  720. /*
  721. Local Variables:
  722. mode:c
  723. indent-tabs-mode:nil
  724. c-basic-offset:2
  725. End:
  726. */