config.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. #ifdef MS_WINDOWS
  12. #include <shlobj.h>
  13. #endif
  14. /** Enumeration of types which option values can take */
  15. typedef enum config_type_t {
  16. CONFIG_TYPE_STRING = 0, /**< An arbitrary string. */
  17. CONFIG_TYPE_UINT, /**< A non-negative integer less than MAX_INT */
  18. CONFIG_TYPE_DOUBLE, /**< A floating-point value */
  19. CONFIG_TYPE_BOOL, /**< A boolean value, expressed as 0 or 1. */
  20. CONFIG_TYPE_CSV, /**< A list of strings, separated by commas and optional
  21. * whitespace. */
  22. CONFIG_TYPE_LINELIST, /**< Uninterpreted config lines */
  23. CONFIG_TYPE_OBSOLETE, /**< Obsolete (ignored) option. */
  24. } config_type_t;
  25. /** Largest allowed config line */
  26. #define CONFIG_LINE_T_MAXLEN 4096
  27. static struct config_line_t *config_get_commandlines(int argc, char **argv);
  28. static int config_get_lines(FILE *f, struct config_line_t **result);
  29. static void config_free_lines(struct config_line_t *front);
  30. static int config_compare(struct config_line_t *c, const char *key, config_type_t type, void *arg);
  31. static int config_assign(or_options_t *options, struct config_line_t *list);
  32. static int parse_dir_server_line(const char *line);
  33. /** Helper: Read a list of configuration options from the command line. */
  34. static struct config_line_t *config_get_commandlines(int argc, char **argv) {
  35. struct config_line_t *new;
  36. struct config_line_t *front = NULL;
  37. char *s;
  38. int i = 1;
  39. while(i < argc-1) {
  40. if(!strcmp(argv[i],"-f")) {
  41. // log(LOG_DEBUG,"Commandline: skipping over -f.");
  42. i+=2; /* this is the config file option. ignore it. */
  43. continue;
  44. }
  45. new = tor_malloc(sizeof(struct config_line_t));
  46. s = argv[i];
  47. while(*s == '-')
  48. s++;
  49. new->key = tor_strdup(s);
  50. new->value = tor_strdup(argv[i+1]);
  51. log(LOG_DEBUG,"Commandline: parsed keyword '%s', value '%s'",
  52. new->key, new->value);
  53. new->next = front;
  54. front = new;
  55. i += 2;
  56. }
  57. return front;
  58. }
  59. /** Helper: allocate a new configuration option mapping 'key' to 'val',
  60. * prepend it to 'front', and return the newly allocated config_line_t */
  61. static struct config_line_t *
  62. config_line_prepend(struct config_line_t *front,
  63. const char *key,
  64. const char *val)
  65. {
  66. struct config_line_t *newline;
  67. newline = tor_malloc(sizeof(struct config_line_t));
  68. newline->key = tor_strdup(key);
  69. newline->value = tor_strdup(val);
  70. newline->next = front;
  71. return newline;
  72. }
  73. /** Helper: parse the config file and strdup into key/value
  74. * strings. Set *result to the list, or NULL if parsing the file
  75. * failed. Return 0 on success, -1 on failure. Warn and ignore any
  76. * misformatted lines. */
  77. static int config_get_lines(FILE *f,
  78. struct config_line_t **result) {
  79. struct config_line_t *front = NULL;
  80. char line[CONFIG_LINE_T_MAXLEN];
  81. int r;
  82. char *key, *value;
  83. while( (r=parse_line_from_file(line,sizeof(line),f,&key,&value)) > 0) {
  84. front = config_line_prepend(front, key, value);
  85. }
  86. if(r < 0) {
  87. *result = NULL;
  88. return -1;
  89. } else {
  90. *result = front;
  91. return 0;
  92. }
  93. }
  94. /**
  95. * Free all the configuration lines on the linked list <b>front</b>.
  96. */
  97. static void config_free_lines(struct config_line_t *front) {
  98. struct config_line_t *tmp;
  99. while(front) {
  100. tmp = front;
  101. front = tmp->next;
  102. tor_free(tmp->key);
  103. tor_free(tmp->value);
  104. tor_free(tmp);
  105. }
  106. }
  107. /** Search the linked list <b>c</b> for any option whose key is <b>key</b>.
  108. * If such an option is found, interpret it as of type <b>type</b>, and store
  109. * the result in <b>arg</b>. If the option is misformatted, log a warning and
  110. * skip it.
  111. */
  112. static int config_compare(struct config_line_t *c, const char *key, config_type_t type, void *arg) {
  113. int i, ok;
  114. if(strncasecmp(c->key,key,strlen(c->key)))
  115. return 0;
  116. if(strcasecmp(c->key,key)) {
  117. tor_free(c->key);
  118. c->key = tor_strdup(key);
  119. }
  120. /* it's a match. cast and assign. */
  121. log_fn(LOG_DEBUG,"Recognized keyword '%s' as %s, using value '%s'.",c->key,key,c->value);
  122. switch(type) {
  123. case CONFIG_TYPE_UINT:
  124. i = tor_parse_long(c->value,10,0,INT_MAX,&ok,NULL);
  125. if(!ok) {
  126. log(LOG_WARN, "Int keyword '%s %s' is malformed or out of bounds. Skipping.",
  127. c->key,c->value);
  128. return 0;
  129. }
  130. *(int *)arg = i;
  131. break;
  132. case CONFIG_TYPE_BOOL:
  133. i = tor_parse_long(c->value,10,0,1,&ok,NULL);
  134. if (!ok) {
  135. log(LOG_WARN, "Boolean keyword '%s' expects 0 or 1. Skipping.", c->key);
  136. return 0;
  137. }
  138. *(int *)arg = i;
  139. break;
  140. case CONFIG_TYPE_STRING:
  141. tor_free(*(char **)arg);
  142. *(char **)arg = tor_strdup(c->value);
  143. break;
  144. case CONFIG_TYPE_DOUBLE:
  145. *(double *)arg = atof(c->value);
  146. break;
  147. case CONFIG_TYPE_CSV:
  148. if(*(smartlist_t**)arg == NULL)
  149. *(smartlist_t**)arg = smartlist_create();
  150. smartlist_split_string(*(smartlist_t**)arg, c->value, ",",
  151. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  152. break;
  153. case CONFIG_TYPE_LINELIST:
  154. /* Note: this reverses the order that the lines appear in. That's
  155. * just fine, since we build up the list of lines reversed in the
  156. * first place. */
  157. *(struct config_line_t**)arg =
  158. config_line_prepend(*(struct config_line_t**)arg, c->key, c->value);
  159. break;
  160. case CONFIG_TYPE_OBSOLETE:
  161. log_fn(LOG_WARN, "Skipping obsolete configuration option '%s'", c->key);
  162. break;
  163. }
  164. return 1;
  165. }
  166. /** Iterate through the linked list of options <b>list</b>.
  167. * For each item, convert as appropriate and assign to <b>options</b>.
  168. * If an item is unrecognized, return -1 immediately,
  169. * else return 0 for success. */
  170. static int config_assign(or_options_t *options, struct config_line_t *list) {
  171. while(list) {
  172. if(
  173. /* order matters here! abbreviated arguments use the first match. */
  174. /* string options */
  175. config_compare(list, "Address", CONFIG_TYPE_STRING, &options->Address) ||
  176. config_compare(list, "AllowUnverifiedNodes", CONFIG_TYPE_CSV, &options->AllowUnverifiedNodes) ||
  177. config_compare(list, "AuthoritativeDirectory",CONFIG_TYPE_BOOL, &options->AuthoritativeDir) ||
  178. config_compare(list, "BandwidthRate", CONFIG_TYPE_UINT, &options->BandwidthRate) ||
  179. config_compare(list, "BandwidthBurst", CONFIG_TYPE_UINT, &options->BandwidthBurst) ||
  180. config_compare(list, "ClientOnly", CONFIG_TYPE_BOOL, &options->ClientOnly) ||
  181. config_compare(list, "ContactInfo", CONFIG_TYPE_STRING, &options->ContactInfo) ||
  182. config_compare(list, "DebugLogFile", CONFIG_TYPE_STRING, &options->DebugLogFile) ||
  183. config_compare(list, "DataDirectory", CONFIG_TYPE_STRING, &options->DataDirectory) ||
  184. config_compare(list, "DirPort", CONFIG_TYPE_UINT, &options->DirPort) ||
  185. config_compare(list, "DirBindAddress", CONFIG_TYPE_LINELIST, &options->DirBindAddress) ||
  186. config_compare(list, "DirFetchPostPeriod",CONFIG_TYPE_UINT, &options->DirFetchPostPeriod) ||
  187. config_compare(list, "DirServer", CONFIG_TYPE_LINELIST, &options->DirServers) ||
  188. config_compare(list, "ExitNodes", CONFIG_TYPE_STRING, &options->ExitNodes) ||
  189. config_compare(list, "EntryNodes", CONFIG_TYPE_STRING, &options->EntryNodes) ||
  190. config_compare(list, "StrictExitNodes", CONFIG_TYPE_BOOL, &options->StrictExitNodes) ||
  191. config_compare(list, "StrictEntryNodes", CONFIG_TYPE_BOOL, &options->StrictEntryNodes) ||
  192. config_compare(list, "ExitPolicy", CONFIG_TYPE_LINELIST, &options->ExitPolicy) ||
  193. config_compare(list, "ExcludeNodes", CONFIG_TYPE_STRING, &options->ExcludeNodes) ||
  194. config_compare(list, "FascistFirewall",CONFIG_TYPE_BOOL, &options->FascistFirewall) ||
  195. config_compare(list, "FirewallPorts",CONFIG_TYPE_CSV, &options->FirewallPorts) ||
  196. config_compare(list, "Group", CONFIG_TYPE_STRING, &options->Group) ||
  197. config_compare(list, "HttpProxy", CONFIG_TYPE_STRING, &options->HttpProxy) ||
  198. config_compare(list, "HiddenServiceDir", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  199. config_compare(list, "HiddenServicePort", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  200. config_compare(list, "HiddenServiceNodes", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  201. config_compare(list, "HiddenServiceExcludeNodes", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  202. config_compare(list, "IgnoreVersion", CONFIG_TYPE_BOOL, &options->IgnoreVersion) ||
  203. config_compare(list, "KeepalivePeriod",CONFIG_TYPE_UINT, &options->KeepalivePeriod) ||
  204. config_compare(list, "LogLevel", CONFIG_TYPE_LINELIST, &options->LogOptions) ||
  205. config_compare(list, "LogFile", CONFIG_TYPE_LINELIST, &options->LogOptions) ||
  206. config_compare(list, "LinkPadding", CONFIG_TYPE_OBSOLETE, NULL) ||
  207. config_compare(list, "MaxConn", CONFIG_TYPE_UINT, &options->MaxConn) ||
  208. config_compare(list, "MaxOnionsPending",CONFIG_TYPE_UINT, &options->MaxOnionsPending) ||
  209. config_compare(list, "Nickname", CONFIG_TYPE_STRING, &options->Nickname) ||
  210. config_compare(list, "NewCircuitPeriod",CONFIG_TYPE_UINT, &options->NewCircuitPeriod) ||
  211. config_compare(list, "NumCpus", CONFIG_TYPE_UINT, &options->NumCpus) ||
  212. config_compare(list, "ORPort", CONFIG_TYPE_UINT, &options->ORPort) ||
  213. config_compare(list, "ORBindAddress", CONFIG_TYPE_LINELIST, &options->ORBindAddress) ||
  214. config_compare(list, "OutboundBindAddress",CONFIG_TYPE_STRING, &options->OutboundBindAddress) ||
  215. config_compare(list, "PidFile", CONFIG_TYPE_STRING, &options->PidFile) ||
  216. config_compare(list, "PathlenCoinWeight",CONFIG_TYPE_DOUBLE, &options->PathlenCoinWeight) ||
  217. config_compare(list, "RouterFile", CONFIG_TYPE_STRING, &options->RouterFile) ||
  218. config_compare(list, "RunAsDaemon", CONFIG_TYPE_BOOL, &options->RunAsDaemon) ||
  219. config_compare(list, "RunTesting", CONFIG_TYPE_BOOL, &options->RunTesting) ||
  220. config_compare(list, "RecommendedVersions",CONFIG_TYPE_STRING, &options->RecommendedVersions) ||
  221. config_compare(list, "RendNodes", CONFIG_TYPE_STRING, &options->RendNodes) ||
  222. config_compare(list, "RendExcludeNodes",CONFIG_TYPE_STRING, &options->RendExcludeNodes) ||
  223. config_compare(list, "SocksPort", CONFIG_TYPE_UINT, &options->SocksPort) ||
  224. config_compare(list, "SocksBindAddress",CONFIG_TYPE_LINELIST,&options->SocksBindAddress) ||
  225. config_compare(list, "SocksPolicy", CONFIG_TYPE_LINELIST,&options->SocksPolicy) ||
  226. config_compare(list, "TrafficShaping", CONFIG_TYPE_OBSOLETE, NULL) ||
  227. config_compare(list, "User", CONFIG_TYPE_STRING, &options->User)
  228. ) {
  229. /* then we're ok. it matched something. */
  230. } else {
  231. log_fn(LOG_WARN,"Unknown keyword '%s'. Failing.",list->key);
  232. return -1;
  233. }
  234. list = list->next;
  235. }
  236. return 0;
  237. }
  238. const char default_dirservers_string[] =
  239. "router moria1 18.244.0.188 9001 9021 9031\n"
  240. "platform Tor 0.0.6rc1 on Linux moria.mit.edu i686\n"
  241. "published 2004-04-25 21:54:28\n"
  242. "bandwidth 800000 10000000\n"
  243. "onion-key\n"
  244. "-----BEGIN RSA PUBLIC KEY-----\n"
  245. "MIGJAoGBANoIvHieyHUTzIacbnWOnyTyzGrLOdXqbcjz2GGMxyHEd5K1bO1ZBNHP\n"
  246. "9i5qLQpN5viFk2K2rEGuG8tFgDEzSWZEtBqv3NVfUdiumdERWMBwlaQ0MVK4C+jf\n"
  247. "y5gZ8KI3o9ZictgPS1AQF+Kk932/vIHTuRIUKb4ILTnQilNvID0NAgMBAAE=\n"
  248. "-----END RSA PUBLIC KEY-----\n"
  249. "signing-key\n"
  250. "-----BEGIN RSA PUBLIC KEY-----\n"
  251. "MIGJAoGBAMHa0ZC/jo2Q2DrwKYF/6ZbmZ27PFYG91u4gUzzmZ/VXLpZ8wNzEV3oW\n"
  252. "nt+I61048fBiC1frT1/DZ351n2bLSk9zJbB6jyGZJn0380FPRX3+cXyXS0Gq8Ril\n"
  253. "xkhMQf5XuNFUb8UmYPSOH4WErjvYjKvU+gfjbK/82Jo9SuHpYz+BAgMBAAE=\n"
  254. "-----END RSA PUBLIC KEY-----\n"
  255. "reject 0.0.0.0/255.0.0.0:*\n"
  256. "reject 169.254.0.0/255.255.0.0:*\n"
  257. "reject 127.0.0.0/255.0.0.0:*\n"
  258. "reject 192.168.0.0/255.255.0.0:*\n"
  259. "reject 10.0.0.0/255.0.0.0:*\n"
  260. "reject 172.16.0.0/255.240.0.0:*\n"
  261. "accept *:20-22\n"
  262. "accept *:53\n"
  263. "accept *:79-80\n"
  264. "accept *:110\n"
  265. "accept *:143\n"
  266. "accept *:443\n"
  267. "accept *:873\n"
  268. "accept *:993\n"
  269. "accept *:995\n"
  270. "accept *:1024-65535\n"
  271. "reject *:*\n"
  272. "router-signature\n"
  273. "-----BEGIN SIGNATURE-----\n"
  274. "o1eAoRHDAEAXsnh5wN++vIwrupd+DbAJ2p3wxHDrmqxTpygzxxCnyQyhMfX03ua2\n"
  275. "4iplyNlwyFwzWcw0sk31otlO2HBYXT1V9G0YxGtKMOeOBMHjfGbUjGvEALHzWi4z\n"
  276. "8DXGJp13zgnUyP4ZA6xaGROwcT6oB5e7UlztvvpGxTg=\n"
  277. "-----END SIGNATURE-----\n"
  278. "\n"
  279. "router moria2 18.244.0.188 9002 9022 9032\n"
  280. "platform Tor 0.0.6rc1 on Linux moria.mit.edu i686\n"
  281. "published 2004-04-25 21:54:30\n"
  282. "bandwidth 800000 10000000\n"
  283. "onion-key\n"
  284. "-----BEGIN RSA PUBLIC KEY-----\n"
  285. "MIGJAoGBAM4Cc/npgYC54XrYLC+grVxJp7PDmNO2DRRJOxKttBBtvLpnR1UaueTi\n"
  286. "kyknT5kmlx+ihgZF/jmye//2dDUp2+kK/kSkpRV4xnDLXZmed+sNSQxqmm9TtZQ9\n"
  287. "/hjpxhp5J9HmUTYhntBs+4E4CUKokmrI6oRLoln4SA39AX9QLPcnAgMBAAE=\n"
  288. "-----END RSA PUBLIC KEY-----\n"
  289. "signing-key\n"
  290. "-----BEGIN RSA PUBLIC KEY-----\n"
  291. "MIGJAoGBAOcrht/y5rkaahfX7sMe2qnpqoPibsjTSJaDvsUtaNP/Bq0MgNDGOR48\n"
  292. "rtwfqTRff275Edkp/UYw3G3vSgKCJr76/bqOHCmkiZrnPV1zxNfrK18gNw2Cxre0\n"
  293. "nTA+fD8JQqpPtb8b0SnG9kwy75eS//sRu7TErie2PzGMxrf9LH0LAgMBAAE=\n"
  294. "-----END RSA PUBLIC KEY-----\n"
  295. "reject 0.0.0.0/255.0.0.0:*\n"
  296. "reject 169.254.0.0/255.255.0.0:*\n"
  297. "reject 127.0.0.0/255.0.0.0:*\n"
  298. "reject 192.168.0.0/255.255.0.0:*\n"
  299. "reject 10.0.0.0/255.0.0.0:*\n"
  300. "reject 172.16.0.0/255.240.0.0:*\n"
  301. "accept *:20-22\n"
  302. "accept *:53\n"
  303. "accept *:79-80\n"
  304. "accept *:110\n"
  305. "accept *:143\n"
  306. "accept *:443\n"
  307. "accept *:873\n"
  308. "accept *:993\n"
  309. "accept *:995\n"
  310. "accept *:1024-65535\n"
  311. "reject *:*\n"
  312. "router-signature\n"
  313. "-----BEGIN SIGNATURE-----\n"
  314. "RKROLwP1ExjTZeg6wuN0pzYqed9IJUd5lAe9hp4ritbnmJAgS6qfww6jgx61CfUR\n"
  315. "6SElhOLE7Q77jAdoL45Ji5pn/Y+Q+E+5lJm1E/ed9ha+YsOPaOc7z6GQ7E4mihCL\n"
  316. "gI1vsw92+P1Ty4RHj6fyD9DhbV19nh2Qs+pvGJOS2FY=\n"
  317. "-----END SIGNATURE-----\n"
  318. "\n"
  319. "router tor26 62.116.124.106 9001 9050 9030\n"
  320. "platform Tor 0.0.6 on Linux seppia i686\n"
  321. "published 2004-05-06 21:33:23\n"
  322. "bandwidth 500000 10000000\n"
  323. "onion-key\n"
  324. "-----BEGIN RSA PUBLIC KEY-----\n"
  325. "MIGJAoGBAMEHdDnpj3ik1AF1xe/VqjoguH2DbANifYqXXfempu0fS+tU9FGo6dU/\n"
  326. "fnVHAZwL9Ek9k2rMzumShi1RduK9p035R/Gk+PBBcLfvwYJ/Nat+ZO/L8jn/3bZe\n"
  327. "ieQd9CKj2LjNGKpRNry37vkwMGIOIlegwK+2us8aXJ7sIvlNts0TAgMBAAE=\n"
  328. "-----END RSA PUBLIC KEY-----\n"
  329. "signing-key\n"
  330. "-----BEGIN RSA PUBLIC KEY-----\n"
  331. "MIGJAoGBAMQgV2gXLbXgesWgeAsj8P1Uvm/zibrFXqwDq27lLKNgWGYGX2ax3LyT\n"
  332. "3nzI1Y5oLs4kPKTsMM5ft9aokwf417lKoCRlZc9ptfRbgxDx90c9GtWVmkrmDvCK\n"
  333. "ae59TMoXIiGfZiwWT6KKq5Zm9/Fu2Il3B2vHGkKJYKixmiBJRKp/AgMBAAE=\n"
  334. "-----END RSA PUBLIC KEY-----\n"
  335. "accept 62.245.184.24:25\n"
  336. "accept 62.116.124.106:6666-6670\n"
  337. "accept *:48099\n"
  338. "reject *:*\n"
  339. "router-signature\n"
  340. "-----BEGIN SIGNATURE-----\n"
  341. "qh/xRoqfLNFzPaB8VdpbdMAwRyuk5qjx4LeLVQ2pDwTZ55PqmG99+VKUNte2WTTD\n"
  342. "7dZEA7um2rueohGe4nYmvbhJWr20/I0ZxmWDRDvFy0b5nwzDMGvLvDw95Zu/XJQ2\n"
  343. "md32NE3y9VZCfbCN+GlvETX3fdR3Svzcm8Kzesg2/s4=\n"
  344. "-----END SIGNATURE-----\n"
  345. ;
  346. int config_assign_default_dirservers(void) {
  347. if(router_load_routerlist_from_string(default_dirservers_string, 1) < 0) {
  348. log_fn(LOG_WARN,"Bug: the default dirservers internal string is corrupt.");
  349. return -1;
  350. }
  351. /* moria1 */
  352. parse_dir_server_line("18.244.0.188:9031 "
  353. "FFCB 46DB 1339 DA84 674C 70D7 CB58 6434 C437 0441");
  354. /* moria2 */
  355. parse_dir_server_line("18.244.0.188:9032 "
  356. "719B E45D E224 B607 C537 07D0 E214 3E2D 423E 74CF");
  357. /* tor26 */
  358. parse_dir_server_line("62.116.124.106:9030 "
  359. "847B 1F85 0344 D787 6491 A548 92F9 0493 4E4E B85D");
  360. return 0;
  361. }
  362. /** Set <b>options</b> to a reasonable default.
  363. *
  364. * Call this function when we can't find any torrc config file.
  365. */
  366. static int config_assign_defaults(or_options_t *options) {
  367. /* set them up as a client only */
  368. options->SocksPort = 9050;
  369. options->AllowUnverifiedNodes = smartlist_create();
  370. smartlist_add(options->AllowUnverifiedNodes, "middle");
  371. smartlist_add(options->AllowUnverifiedNodes, "rendezvous");
  372. config_free_lines(options->ExitPolicy);
  373. options->ExitPolicy = config_line_prepend(NULL, "ExitPolicy", "reject *:*");
  374. return 0;
  375. }
  376. /** Print a usage message for tor. */
  377. static void print_usage(void) {
  378. printf("tor -f <torrc> [args]\n"
  379. "See man page for more options. This -h is probably obsolete.\n\n"
  380. "-b <bandwidth>\t\tbytes/second rate limiting\n"
  381. "-d <file>\t\tDebug file\n"
  382. // "-m <max>\t\tMax number of connections\n"
  383. "-l <level>\t\tLog level\n"
  384. "-r <file>\t\tList of known routers\n");
  385. printf("\nClient options:\n"
  386. "-e \"nick1 nick2 ...\"\t\tExit nodes\n"
  387. "-s <IP>\t\t\tPort to bind to for Socks\n"
  388. );
  389. printf("\nServer options:\n"
  390. "-n <nick>\t\tNickname of router\n"
  391. "-o <port>\t\tOR port to bind to\n"
  392. "-p <file>\t\tPID file\n"
  393. );
  394. }
  395. /**
  396. * Based on <b>address</b>, guess our public IP address and put it
  397. * in <b>addr</b>.
  398. */
  399. int resolve_my_address(const char *address, uint32_t *addr) {
  400. struct in_addr in;
  401. struct hostent *rent;
  402. char hostname[256];
  403. int explicit_ip=1;
  404. tor_assert(addr);
  405. if(address) {
  406. strlcpy(hostname,address,sizeof(hostname));
  407. } else { /* then we need to guess our address */
  408. explicit_ip = 0; /* it's implicit */
  409. if(gethostname(hostname,sizeof(hostname)) < 0) {
  410. log_fn(LOG_WARN,"Error obtaining local hostname");
  411. return -1;
  412. }
  413. log_fn(LOG_DEBUG,"Guessed local host name as '%s'",hostname);
  414. }
  415. /* now we know hostname. resolve it and keep only the IP */
  416. if(tor_inet_aton(hostname, &in) == 0) {
  417. /* then we have to resolve it */
  418. explicit_ip = 0;
  419. rent = (struct hostent *)gethostbyname(hostname);
  420. if (!rent) {
  421. log_fn(LOG_WARN,"Could not resolve local Address %s. Failing.", hostname);
  422. return -1;
  423. }
  424. tor_assert(rent->h_length == 4);
  425. memcpy(&in.s_addr, rent->h_addr,rent->h_length);
  426. }
  427. if(!explicit_ip && is_internal_IP(htonl(in.s_addr))) {
  428. log_fn(LOG_WARN,"Address '%s' resolves to private IP '%s'. "
  429. "Please set the Address config option to be the IP you want to use.",
  430. hostname, inet_ntoa(in));
  431. return -1;
  432. }
  433. log_fn(LOG_DEBUG,"Resolved Address to %s.", inet_ntoa(in));
  434. *addr = ntohl(in.s_addr);
  435. return 0;
  436. }
  437. static char *get_default_nickname(void)
  438. {
  439. char localhostname[256];
  440. char *cp, *out, *outp;
  441. if(gethostname(localhostname,sizeof(localhostname)) < 0) {
  442. log_fn(LOG_WARN,"Error obtaining local hostname");
  443. return NULL;
  444. }
  445. /* Put it in lowercase; stop at the first dot. */
  446. for(cp = localhostname; *cp; ++cp) {
  447. if (*cp == '.') {
  448. *cp = '\0';
  449. break;
  450. }
  451. *cp = tolower(*cp);
  452. }
  453. /* Strip invalid characters. */
  454. cp = localhostname;
  455. out = outp = tor_malloc(strlen(localhostname)+1);
  456. while (*cp) {
  457. if (strchr(LEGAL_NICKNAME_CHARACTERS, *cp))
  458. *outp++ = *cp++;
  459. else
  460. cp++;
  461. }
  462. *outp = '\0';
  463. /* Enforce length. */
  464. if (strlen(out) > MAX_NICKNAME_LEN)
  465. out[MAX_NICKNAME_LEN]='\0';
  466. return out;
  467. }
  468. /** Release storage held by <b>options</b> */
  469. static void free_options(or_options_t *options) {
  470. config_free_lines(options->LogOptions);
  471. tor_free(options->ContactInfo);
  472. tor_free(options->DebugLogFile);
  473. tor_free(options->DataDirectory);
  474. tor_free(options->RouterFile);
  475. tor_free(options->Nickname);
  476. tor_free(options->Address);
  477. tor_free(options->PidFile);
  478. tor_free(options->ExitNodes);
  479. tor_free(options->EntryNodes);
  480. tor_free(options->ExcludeNodes);
  481. tor_free(options->RendNodes);
  482. tor_free(options->RendExcludeNodes);
  483. tor_free(options->OutboundBindAddress);
  484. tor_free(options->RecommendedVersions);
  485. tor_free(options->User);
  486. tor_free(options->Group);
  487. tor_free(options->HttpProxy);
  488. config_free_lines(options->RendConfigLines);
  489. config_free_lines(options->SocksBindAddress);
  490. config_free_lines(options->ORBindAddress);
  491. config_free_lines(options->DirBindAddress);
  492. config_free_lines(options->ExitPolicy);
  493. config_free_lines(options->SocksPolicy);
  494. config_free_lines(options->DirServers);
  495. if (options->FirewallPorts) {
  496. SMARTLIST_FOREACH(options->FirewallPorts, char *, cp, tor_free(cp));
  497. smartlist_free(options->FirewallPorts);
  498. options->FirewallPorts = NULL;
  499. }
  500. }
  501. /** Set <b>options</b> to hold reasonable defaults for most options. */
  502. static void init_options(or_options_t *options) {
  503. /* give reasonable values for each option. Defaults to zero. */
  504. memset(options,0,sizeof(or_options_t));
  505. options->LogOptions = NULL;
  506. options->ExitNodes = tor_strdup("");
  507. options->EntryNodes = tor_strdup("");
  508. options->StrictEntryNodes = options->StrictExitNodes = 0;
  509. options->ExcludeNodes = tor_strdup("");
  510. options->RendNodes = tor_strdup("");
  511. options->RendExcludeNodes = tor_strdup("");
  512. options->ExitPolicy = NULL;
  513. options->SocksPolicy = NULL;
  514. options->SocksBindAddress = NULL;
  515. options->ORBindAddress = NULL;
  516. options->DirBindAddress = NULL;
  517. options->OutboundBindAddress = NULL;
  518. options->RecommendedVersions = NULL;
  519. options->PidFile = NULL; // tor_strdup("tor.pid");
  520. options->DataDirectory = NULL;
  521. options->PathlenCoinWeight = 0.3;
  522. options->MaxConn = 900;
  523. options->DirFetchPostPeriod = 600;
  524. options->KeepalivePeriod = 300;
  525. options->MaxOnionsPending = 100;
  526. options->NewCircuitPeriod = 30; /* twice a minute */
  527. options->BandwidthRate = 800000; /* at most 800kB/s total sustained incoming */
  528. options->BandwidthBurst = 10000000; /* max burst on the token bucket */
  529. options->NumCpus = 1;
  530. options->RendConfigLines = NULL;
  531. options->FirewallPorts = NULL;
  532. options->DirServers = NULL;
  533. }
  534. static char *get_default_conf_file(void)
  535. {
  536. #ifdef MS_WINDOWS
  537. char *path = tor_malloc(MAX_PATH);
  538. if (!SUCCEEDED(SHGetSpecialFolderPath(NULL, path, CSIDL_APPDATA, 1))) {
  539. tor_free(path);
  540. return NULL;
  541. }
  542. strlcat(path,"\\tor\\torrc",MAX_PATH);
  543. return path;
  544. #else
  545. return tor_strdup(CONFDIR "/torrc");
  546. #endif
  547. }
  548. /** Read a configuration file into <b>options</b>, finding the configuration
  549. * file location based on the command line. After loading the options,
  550. * validate them for consistency. Return 0 if success, <0 if failure. */
  551. int getconfig(int argc, char **argv, or_options_t *options) {
  552. struct config_line_t *cl;
  553. FILE *cf;
  554. char *fname;
  555. int i;
  556. int result = 0;
  557. static int first_load = 1;
  558. static char **backup_argv;
  559. static int backup_argc;
  560. char *previous_pidfile = NULL;
  561. int previous_runasdaemon = 0;
  562. int previous_orport = -1;
  563. int using_default_torrc;
  564. if(first_load) { /* first time we're called. save commandline args */
  565. backup_argv = argv;
  566. backup_argc = argc;
  567. first_load = 0;
  568. } else { /* we're reloading. need to clean up old ones first. */
  569. argv = backup_argv;
  570. argc = backup_argc;
  571. /* record some previous values, so we can fail if they change */
  572. if(options->PidFile)
  573. previous_pidfile = tor_strdup(options->PidFile);
  574. previous_runasdaemon = options->RunAsDaemon;
  575. previous_orport = options->ORPort;
  576. free_options(options);
  577. }
  578. init_options(options);
  579. if(argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1],"--help"))) {
  580. print_usage();
  581. exit(0);
  582. }
  583. if(argc > 1 && (!strcmp(argv[1],"--version"))) {
  584. printf("Tor version %s.\n",VERSION);
  585. exit(0);
  586. }
  587. /* learn config file name, get config lines, assign them */
  588. i = 1;
  589. while(i < argc-1 && strcmp(argv[i],"-f")) {
  590. i++;
  591. }
  592. if(i < argc-1) { /* we found one */
  593. fname = tor_strdup(argv[i+1]);
  594. using_default_torrc = 0;
  595. } else {
  596. /* didn't find one, try CONFDIR */
  597. char *fn;
  598. using_default_torrc = 1;
  599. fn = get_default_conf_file();
  600. if (fn && file_status(fn)==FN_FILE) {
  601. fname = fn;
  602. } else {
  603. tor_free(fn);
  604. fn = expand_filename("~/.torrc");
  605. if (fn && file_status(fn)==FN_FILE) {
  606. fname = fn;
  607. } else {
  608. tor_free(fn);
  609. fname = get_default_conf_file();
  610. }
  611. }
  612. }
  613. tor_assert(fname);
  614. log(LOG_DEBUG,"Opening config file '%s'",fname);
  615. if(config_assign_defaults(options) < 0) {
  616. return -1;
  617. }
  618. cf = fopen(fname, "r");
  619. if(!cf) {
  620. if(using_default_torrc == 1) {
  621. log(LOG_NOTICE, "Configuration file '%s' not present, using reasonable defaults.",fname);
  622. tor_free(fname);
  623. } else {
  624. log(LOG_WARN, "Unable to open configuration file '%s'.",fname);
  625. tor_free(fname);
  626. return -1;
  627. }
  628. } else { /* it opened successfully. use it. */
  629. tor_free(fname);
  630. if (config_get_lines(cf, &cl)<0)
  631. return -1;
  632. if(config_assign(options,cl) < 0)
  633. return -1;
  634. config_free_lines(cl);
  635. fclose(cf);
  636. }
  637. /* go through command-line variables too */
  638. cl = config_get_commandlines(argc,argv);
  639. if(config_assign(options,cl) < 0)
  640. return -1;
  641. config_free_lines(cl);
  642. /* Validate options */
  643. /* first check if any of the previous options have changed but aren't allowed to */
  644. if(previous_pidfile && strcmp(previous_pidfile,options->PidFile)) {
  645. log_fn(LOG_WARN,"During reload, PidFile changed from %s to %s. Failing.",
  646. previous_pidfile, options->PidFile);
  647. return -1;
  648. }
  649. tor_free(previous_pidfile);
  650. if(previous_runasdaemon && !options->RunAsDaemon) {
  651. log_fn(LOG_WARN,"During reload, change from RunAsDaemon=1 to =0 not allowed. Failing.");
  652. return -1;
  653. }
  654. if(previous_orport == 0 && options->ORPort > 0) {
  655. log_fn(LOG_WARN,"During reload, change from ORPort=0 to >0 not allowed. Failing.");
  656. return -1;
  657. }
  658. if(options->ORPort < 0 || options->ORPort > 65535) {
  659. log(LOG_WARN,"ORPort option out of bounds.");
  660. result = -1;
  661. }
  662. if (options->Nickname == NULL) {
  663. if(server_mode()) {
  664. if (!(options->Nickname = get_default_nickname()))
  665. return -1;
  666. log_fn(LOG_NOTICE, "Choosing default nickname %s", options->Nickname);
  667. }
  668. } else {
  669. if (strspn(options->Nickname, LEGAL_NICKNAME_CHARACTERS) !=
  670. strlen(options->Nickname)) {
  671. log_fn(LOG_WARN, "Nickname '%s' contains illegal characters.", options->Nickname);
  672. result = -1;
  673. }
  674. if (strlen(options->Nickname) == 0) {
  675. log_fn(LOG_WARN, "Nickname must have at least one character");
  676. result = -1;
  677. }
  678. if (strlen(options->Nickname) > MAX_NICKNAME_LEN) {
  679. log_fn(LOG_WARN, "Nickname '%s' has more than %d characters.",
  680. options->Nickname, MAX_NICKNAME_LEN);
  681. result = -1;
  682. }
  683. }
  684. if(server_mode()) {
  685. /* confirm that our address isn't broken, so we can complain now */
  686. uint32_t tmp;
  687. if(resolve_my_address(options->Address, &tmp) < 0)
  688. result = -1;
  689. }
  690. if(options->SocksPort < 0 || options->SocksPort > 65535) {
  691. log(LOG_WARN,"SocksPort option out of bounds.");
  692. result = -1;
  693. }
  694. if(options->SocksPort == 0 && options->ORPort == 0) {
  695. log(LOG_WARN,"SocksPort and ORPort are both undefined? Quitting.");
  696. result = -1;
  697. }
  698. if(options->DirPort < 0 || options->DirPort > 65535) {
  699. log(LOG_WARN,"DirPort option out of bounds.");
  700. result = -1;
  701. }
  702. if(options->StrictExitNodes && !strlen(options->ExitNodes)) {
  703. log(LOG_WARN,"StrictExitNodes set, but no ExitNodes listed.");
  704. }
  705. if(options->StrictEntryNodes && !strlen(options->EntryNodes)) {
  706. log(LOG_WARN,"StrictEntryNodes set, but no EntryNodes listed.");
  707. }
  708. if(options->AuthoritativeDir && options->RecommendedVersions == NULL) {
  709. log(LOG_WARN,"Directory servers must configure RecommendedVersions.");
  710. result = -1;
  711. }
  712. if(options->AuthoritativeDir && !options->DirPort) {
  713. log(LOG_WARN,"Running as authoritative directory, but no DirPort set.");
  714. result = -1;
  715. }
  716. if(options->AuthoritativeDir && !options->ORPort) {
  717. log(LOG_WARN,"Running as authoritative directory, but no ORPort set.");
  718. result = -1;
  719. }
  720. if(options->AuthoritativeDir && options->ClientOnly) {
  721. log(LOG_WARN,"Running as authoritative directory, but ClientOnly also set.");
  722. result = -1;
  723. }
  724. if(options->FascistFirewall && !options->FirewallPorts) {
  725. options->FirewallPorts = smartlist_create();
  726. smartlist_add(options->FirewallPorts, tor_strdup("80"));
  727. smartlist_add(options->FirewallPorts, tor_strdup("443"));
  728. }
  729. if(options->FirewallPorts) {
  730. SMARTLIST_FOREACH(options->FirewallPorts, const char *, cp,
  731. { i = atoi(cp);
  732. if (i < 1 || i > 65535) {
  733. log(LOG_WARN, "Port '%s' out of range in FirewallPorts", cp);
  734. result=-1;
  735. }
  736. });
  737. }
  738. options->_AllowUnverified = 0;
  739. if(options->AllowUnverifiedNodes) {
  740. SMARTLIST_FOREACH(options->AllowUnverifiedNodes, const char *, cp,
  741. { if (!strcasecmp(cp, "entry"))
  742. options->_AllowUnverified |= ALLOW_UNVERIFIED_ENTRY;
  743. else if (!strcasecmp(cp, "exit"))
  744. options->_AllowUnverified |= ALLOW_UNVERIFIED_EXIT;
  745. else if (!strcasecmp(cp, "middle"))
  746. options->_AllowUnverified |= ALLOW_UNVERIFIED_MIDDLE;
  747. else if (!strcasecmp(cp, "introduction"))
  748. options->_AllowUnverified |= ALLOW_UNVERIFIED_INTRODUCTION;
  749. else if (!strcasecmp(cp, "rendezvous"))
  750. options->_AllowUnverified |= ALLOW_UNVERIFIED_RENDEZVOUS;
  751. else {
  752. log(LOG_WARN, "Unrecognized value '%s' in AllowUnverifiedNodes",
  753. cp);
  754. result=-1;
  755. }
  756. });
  757. }
  758. if(options->SocksPort >= 1 &&
  759. (options->PathlenCoinWeight < 0.0 || options->PathlenCoinWeight >= 1.0)) {
  760. log(LOG_WARN,"PathlenCoinWeight option must be >=0.0 and <1.0.");
  761. result = -1;
  762. }
  763. if(options->MaxConn < 1) {
  764. log(LOG_WARN,"MaxConn option must be a non-zero positive integer.");
  765. result = -1;
  766. }
  767. if(options->MaxConn >= MAXCONNECTIONS) {
  768. log(LOG_WARN,"MaxConn option must be less than %d.", MAXCONNECTIONS);
  769. result = -1;
  770. }
  771. #define MIN_DIRFETCHPOSTPERIOD 60
  772. if(options->DirFetchPostPeriod < MIN_DIRFETCHPOSTPERIOD) {
  773. log(LOG_WARN,"DirFetchPostPeriod option must be at least %d.", MIN_DIRFETCHPOSTPERIOD);
  774. result = -1;
  775. }
  776. if(options->DirFetchPostPeriod > MIN_ONION_KEY_LIFETIME/2) {
  777. log(LOG_WARN,"DirFetchPostPeriod is too large; clipping.");
  778. options->DirFetchPostPeriod = MIN_ONION_KEY_LIFETIME/2;
  779. }
  780. if(options->KeepalivePeriod < 1) {
  781. log(LOG_WARN,"KeepalivePeriod option must be positive.");
  782. result = -1;
  783. }
  784. if(options->HttpProxy) { /* parse it now */
  785. if(parse_addr_port(options->HttpProxy, NULL,
  786. &options->HttpProxyAddr, &options->HttpProxyPort) < 0) {
  787. log(LOG_WARN,"HttpProxy failed to parse or resolve. Please fix.");
  788. result = -1;
  789. }
  790. options->HttpProxyAddr = ntohl(options->HttpProxyAddr); /* switch to host-order */
  791. if(options->HttpProxyPort == 0) { /* give it a default */
  792. options->HttpProxyPort = 80;
  793. }
  794. }
  795. for (cl = options->DirServers; cl; cl = cl->next) {
  796. if (parse_dir_server_line(cl->value)<0)
  797. return -1;
  798. }
  799. /* XXX look at the various nicknamelists and make sure they're
  800. * valid and don't have hostnames that are too long.
  801. */
  802. if (rend_config_services(options) < 0) {
  803. result = -1;
  804. }
  805. return result;
  806. }
  807. static int add_single_log(struct config_line_t *level_opt,
  808. struct config_line_t *file_opt,
  809. int isDaemon)
  810. {
  811. int levelMin=-1, levelMax=-1;
  812. char *cp, *tmp_sev;
  813. if (level_opt) {
  814. cp = strchr(level_opt->value, '-');
  815. if (cp) {
  816. tmp_sev = tor_strndup(level_opt->value, cp - level_opt->value);
  817. levelMin = parse_log_level(tmp_sev);
  818. if (levelMin<0) {
  819. log_fn(LOG_WARN, "Unrecognized log severity '%s': must be one of err|warn|notice|info|debug", tmp_sev);
  820. return -1;
  821. }
  822. tor_free(tmp_sev);
  823. levelMax = parse_log_level(cp+1);
  824. if (levelMax<0) {
  825. log_fn(LOG_WARN, "Unrecognized log severity '%s': must be one of err|warn|notice|info|debug", cp+1);
  826. return -1;
  827. }
  828. } else {
  829. levelMin = parse_log_level(level_opt->value);
  830. if (levelMin<0) {
  831. log_fn(LOG_WARN, "Unrecognized log severity '%s': must be one of err|warn|notice|info|debug", level_opt->value);
  832. return -1;
  833. }
  834. }
  835. }
  836. if (levelMin < 0 && levelMax < 0) {
  837. levelMin = LOG_NOTICE;
  838. levelMax = LOG_ERR;
  839. } else if (levelMin < 0) {
  840. levelMin = levelMax;
  841. } else {
  842. levelMax = LOG_ERR;
  843. }
  844. if (file_opt) {
  845. if (add_file_log(levelMin, levelMax, file_opt->value) < 0) {
  846. log_fn(LOG_WARN, "Cannot write to LogFile '%s': %s.", file_opt->value,
  847. strerror(errno));
  848. return -1;
  849. }
  850. log_fn(LOG_NOTICE, "Successfully opened LogFile '%s', redirecting output.",
  851. file_opt->value);
  852. } else if (!isDaemon) {
  853. add_stream_log(levelMin, levelMax, "<stdout>", stdout);
  854. close_temp_logs();
  855. }
  856. return 0;
  857. }
  858. /**
  859. * Initialize the logs based on the configuration file.
  860. */
  861. int config_init_logs(or_options_t *options)
  862. {
  863. /* The order of options is: Level? (File Level?)+
  864. */
  865. struct config_line_t *opt = options->LogOptions;
  866. /* Special case if no options are given. */
  867. if (!opt) {
  868. add_stream_log(LOG_NOTICE, LOG_ERR, "<stdout>", stdout);
  869. close_temp_logs();
  870. /* don't return yet, in case we want to do a debuglogfile below */
  871. }
  872. /* Special case for if first option is LogLevel. */
  873. if (opt && !strcasecmp(opt->key, "LogLevel")) {
  874. if (opt->next && !strcasecmp(opt->next->key, "LogFile")) {
  875. if (add_single_log(opt, opt->next, options->RunAsDaemon)<0)
  876. return -1;
  877. opt = opt->next->next;
  878. } else if (!opt->next) {
  879. if (add_single_log(opt, NULL, options->RunAsDaemon)<0)
  880. return -1;
  881. opt = opt->next;
  882. } else {
  883. ; /* give warning below */
  884. }
  885. }
  886. while (opt) {
  887. if (!strcasecmp(opt->key, "LogLevel")) {
  888. log_fn(LOG_WARN, "Two LogLevel options in a row without intervening LogFile");
  889. opt = opt->next;
  890. } else {
  891. tor_assert(!strcasecmp(opt->key, "LogFile"));
  892. if (opt->next && !strcasecmp(opt->next->key, "LogLevel")) {
  893. /* LogFile followed by LogLevel */
  894. if (add_single_log(opt->next, opt, options->RunAsDaemon)<0)
  895. return -1;
  896. opt = opt->next->next;
  897. } else {
  898. /* LogFile followed by LogFile or end of list. */
  899. if (add_single_log(NULL, opt, options->RunAsDaemon)<0)
  900. return -1;
  901. opt = opt->next;
  902. }
  903. }
  904. }
  905. if (options->DebugLogFile) {
  906. log_fn(LOG_WARN, "DebugLogFile is deprecated; use LogFile and LogLevel instead");
  907. if (add_file_log(LOG_DEBUG, LOG_ERR, options->DebugLogFile)<0)
  908. return -1;
  909. }
  910. return 0;
  911. }
  912. /**
  913. * Given a linked list of config lines containing "allow" and "deny" tokens,
  914. * parse them and place the result in <b>dest</b>. Skip malformed lines.
  915. */
  916. void
  917. config_parse_exit_policy(struct config_line_t *cfg,
  918. struct exit_policy_t **dest)
  919. {
  920. struct exit_policy_t **nextp;
  921. smartlist_t *entries;
  922. if (!cfg)
  923. return;
  924. nextp = dest;
  925. while (*nextp)
  926. nextp = &((*nextp)->next);
  927. entries = smartlist_create();
  928. for (; cfg; cfg = cfg->next) {
  929. smartlist_split_string(entries,cfg->value,",",SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK,0);
  930. SMARTLIST_FOREACH(entries, const char *, ent, {
  931. log_fn(LOG_DEBUG,"Adding new entry '%s'",ent);
  932. *nextp = router_parse_exit_policy_from_string(ent);
  933. if(*nextp) {
  934. nextp = &((*nextp)->next);
  935. } else {
  936. log_fn(LOG_WARN,"Malformed exit policy %s; skipping.", ent);
  937. }
  938. });
  939. SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
  940. smartlist_clear(entries);
  941. }
  942. smartlist_free(entries);
  943. }
  944. void exit_policy_free(struct exit_policy_t *p) {
  945. struct exit_policy_t *e;
  946. while (p) {
  947. e = p;
  948. p = p->next;
  949. tor_free(e->string);
  950. tor_free(e);
  951. }
  952. }
  953. static int parse_dir_server_line(const char *line)
  954. {
  955. smartlist_t *items = NULL;
  956. int r;
  957. char *addrport, *address=NULL;
  958. uint16_t port;
  959. char digest[DIGEST_LEN];
  960. items = smartlist_create();
  961. smartlist_split_string(items, line, " ",
  962. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  963. if (smartlist_len(items) < 2) {
  964. log_fn(LOG_WARN, "Too few arguments to DirServer line."); goto err;
  965. }
  966. addrport = smartlist_get(items, 0);
  967. if (parse_addr_port(addrport, &address, NULL, &port)<0) {
  968. log_fn(LOG_WARN, "Error parsing DirServer address '%s'", addrport);goto err;
  969. }
  970. if (!port) {
  971. log_fn(LOG_WARN, "Missing port in DirServe address '%s'",addrport);goto err;
  972. }
  973. tor_strstrip(smartlist_get(items, 1), " ");
  974. if (strlen(smartlist_get(items, 1)) != HEX_DIGEST_LEN) {
  975. log_fn(LOG_WARN, "Key digest for DirServer is wrong length."); goto err;
  976. }
  977. if (base16_decode(digest, DIGEST_LEN,
  978. smartlist_get(items,1), HEX_DIGEST_LEN)<0) {
  979. log_fn(LOG_WARN, "Unable to decode DirServer key digest."); goto err;
  980. }
  981. log_fn(LOG_DEBUG, "Trusted dirserver at %s:%d (%s)", address,(int)port,
  982. (char*)smartlist_get(items,1));
  983. add_trusted_dir_server(address, port, digest);
  984. r = 0;
  985. goto done;
  986. err:
  987. r = -1;
  988. done:
  989. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  990. smartlist_free(items);
  991. if (address) tor_free(address);
  992. return r;
  993. }
  994. const char *get_data_directory(or_options_t *options) {
  995. const char *d;
  996. if (options->DataDirectory)
  997. d = options->DataDirectory;
  998. else {
  999. #ifdef MS_WINDOWS
  1000. char *p;
  1001. p = tor_malloc(MAX_PATH);
  1002. if (!SUCCEEDED(SHGetSpecialFolderPath(NULL, p, CSIDL_APPDATA, 1))) {
  1003. strlcpy(p,CONFDIR, MAX_PATH);
  1004. }
  1005. strlcat(p,"\\tor",MAX_PATH);
  1006. options->DataDirectory = p;
  1007. return p;
  1008. #else
  1009. d = "~/.tor";
  1010. #endif
  1011. }
  1012. if (d && strncmp(d,"~/",2)==0) {
  1013. char *fn = expand_filename(d);
  1014. if(!fn) {
  1015. log_fn(LOG_ERR,"Failed to expand filename '%s'. Exiting.",d);
  1016. exit(1);
  1017. }
  1018. tor_free(options->DataDirectory);
  1019. options->DataDirectory = fn;
  1020. }
  1021. return options->DataDirectory;
  1022. }
  1023. /*
  1024. Local Variables:
  1025. mode:c
  1026. indent-tabs-mode:nil
  1027. c-basic-offset:2
  1028. End:
  1029. */