config.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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. /* An abbreviation for a configuration option allowed on the command line */
  26. typedef struct config_abbrev_t {
  27. char *abbreviated;
  28. char *full;
  29. int commandline_only;
  30. } config_abbrev_t;
  31. /* Handy macro for declaring "In the config file or on the command line,
  32. * you can abbreviate <b>tok</b>s as <b>tok</b>". */
  33. #define PLURAL(tok) { #tok, #tok "s", 0 }
  34. /* A list of command-line abbreviations. */
  35. static config_abbrev_t config_abbrevs[] = {
  36. PLURAL(ExitNode),
  37. PLURAL(EntryNodes),
  38. PLURAL(ExcludeNode),
  39. PLURAL(FirewallPort),
  40. PLURAL(HiddenServiceNode),
  41. PLURAL(HiddenServiceExcludeNode),
  42. PLURAL(RendNode),
  43. PLURAL(RendExcludeNode),
  44. { "l", "LogLevel" , 1},
  45. { NULL, NULL },
  46. };
  47. #undef PLURAL
  48. /* A variable allowed in the configuration file or on the command line */
  49. typedef struct config_var_t {
  50. char *name; /**< The full keyword (case insensitive) */
  51. config_type_t type; /**< How to interpret the type and turn it into a value */
  52. off_t var_offset; /**< Offset of the corresponding member of or_options_t */
  53. } config_var_t;
  54. /** Return the offset of <b>member</b> within the type <b>tp</b>, in bytes */
  55. #define STRUCT_OFFSET(tp, member) ((off_t) &(((tp*)0)->member))
  56. /** An entry for config_vars: "The option <b>name</b> has type
  57. * CONFIG_TYPE_<b>conftype</b>, and corresponds to
  58. * or_options_t.<b>member</b>"
  59. */
  60. #define VAR(name,conftype,member) \
  61. { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(or_options_t, member) }
  62. /** An entry for config_vars: "The option <b>name</b> is obsolete." */
  63. #define OBSOLETE(name) { name, CONFIG_TYPE_OBSOLETE, 0 }
  64. /** Array of configuration options. Until we disallow nonstandard
  65. * abbreviations, order is significant, since the first matching option will
  66. * be chosen first.
  67. */
  68. static config_var_t config_vars[] = {
  69. VAR("Address", STRING, Address),
  70. VAR("AllowUnverifiedNodes",CSV, AllowUnverifiedNodes),
  71. VAR("AuthoritativeDirectory",BOOL, AuthoritativeDir),
  72. VAR("BandwidthRate", UINT, BandwidthRate),
  73. VAR("BandwidthBurst", UINT, BandwidthBurst),
  74. VAR("ClientOnly", BOOL, ClientOnly),
  75. VAR("ContactInfo", STRING, ContactInfo),
  76. VAR("DebugLogFile", STRING, DebugLogFile),
  77. VAR("DataDirectory", STRING, DataDirectory),
  78. VAR("DirPort", UINT, DirPort),
  79. VAR("DirBindAddress", LINELIST, DirBindAddress),
  80. VAR("DirFetchPostPeriod", UINT, DirFetchPostPeriod),
  81. VAR("DirPolicy", LINELIST, DirPolicy),
  82. VAR("DirServer", LINELIST, DirServers),
  83. VAR("ExitNodes", STRING, ExitNodes),
  84. VAR("EntryNodes", STRING, EntryNodes),
  85. VAR("StrictExitNodes", BOOL, StrictExitNodes),
  86. VAR("StrictEntryNodes", BOOL, StrictEntryNodes),
  87. VAR("ExitPolicy", LINELIST, ExitPolicy),
  88. VAR("ExcludeNodes", STRING, ExcludeNodes),
  89. VAR("FascistFirewall", BOOL, FascistFirewall),
  90. VAR("FirewallPorts", CSV, FirewallPorts),
  91. VAR("MyFamily", STRING, MyFamily),
  92. VAR("NodeFamily", LINELIST, NodeFamilies),
  93. VAR("Group", STRING, Group),
  94. VAR("HttpProxy", STRING, HttpProxy),
  95. VAR("HiddenServiceDir", LINELIST, RendConfigLines),
  96. VAR("HiddenServicePort", LINELIST, RendConfigLines),
  97. VAR("HiddenServiceNodes", LINELIST, RendConfigLines),
  98. VAR("HiddenServiceExcludeNodes", LINELIST, RendConfigLines),
  99. VAR("IgnoreVersion", BOOL, IgnoreVersion),
  100. VAR("KeepalivePeriod", UINT, KeepalivePeriod),
  101. VAR("LogLevel", LINELIST, LogOptions),
  102. VAR("LogFile", LINELIST, LogOptions),
  103. OBSOLETE("LinkPadding"),
  104. VAR("MaxConn", UINT, MaxConn),
  105. VAR("MaxOnionsPending", UINT, MaxOnionsPending),
  106. VAR("Nickname", STRING, Nickname),
  107. VAR("NewCircuitPeriod", UINT, NewCircuitPeriod),
  108. VAR("NumCpus", UINT, NumCpus),
  109. VAR("ORPort", UINT, ORPort),
  110. VAR("ORBindAddress", LINELIST, ORBindAddress),
  111. VAR("OutboundBindAddress", STRING, OutboundBindAddress),
  112. VAR("PidFile", STRING, PidFile),
  113. VAR("PathlenCoinWeight", DOUBLE, PathlenCoinWeight),
  114. VAR("RedirectExit", LINELIST, RedirectExit),
  115. OBSOLETE("RouterFile"),
  116. VAR("RunAsDaemon", BOOL, RunAsDaemon),
  117. VAR("RunTesting", BOOL, RunTesting),
  118. VAR("RecommendedVersions", LINELIST, RecommendedVersions),
  119. VAR("RendNodes", STRING, RendNodes),
  120. VAR("RendExcludeNodes", STRING, RendExcludeNodes),
  121. VAR("SocksPort", UINT, SocksPort),
  122. VAR("SocksBindAddress", LINELIST, SocksBindAddress),
  123. VAR("SocksPolicy", LINELIST, SocksPolicy),
  124. VAR("SysLog", LINELIST, LogOptions),
  125. OBSOLETE("TrafficShaping"),
  126. VAR("User", STRING, User),
  127. { NULL, CONFIG_TYPE_OBSOLETE, 0 }
  128. };
  129. #undef VAR
  130. #undef OBSOLETE
  131. /** Largest allowed config line */
  132. #define CONFIG_LINE_T_MAXLEN 4096
  133. static struct config_line_t *config_get_commandlines(int argc, char **argv);
  134. static int config_get_lines(FILE *f, struct config_line_t **result);
  135. static void config_free_lines(struct config_line_t *front);
  136. static int config_assign_line(or_options_t *options, struct config_line_t *c);
  137. static int config_assign(or_options_t *options, struct config_line_t *list);
  138. static int parse_dir_server_line(const char *line);
  139. static int parse_redirect_line(or_options_t *options,
  140. struct config_line_t *line);
  141. static const char *expand_abbrev(const char *option, int commandline_only);
  142. static config_var_t *config_find_option(const char *key);
  143. /** If <b>option</b> is an official abbreviation for a longer option,
  144. * return the longer option. Otherwise return <b>option</b>.
  145. * If <b>command_line</b> is set, apply all abbreviations. Otherwise, only
  146. * apply abbreviations that work for the config file and the command line. */
  147. static const char *
  148. expand_abbrev(const char *option, int command_line)
  149. {
  150. int i;
  151. for (i=0; config_abbrevs[i].abbreviated; ++i) {
  152. /* Abbreviations aren't casei. */
  153. if (!strcmp(option,config_abbrevs[i].abbreviated) &&
  154. (command_line || !config_abbrevs[i].commandline_only)) {
  155. return config_abbrevs[i].full;
  156. }
  157. }
  158. return option;
  159. }
  160. /** Helper: Read a list of configuration options from the command line. */
  161. static struct config_line_t *
  162. config_get_commandlines(int argc, char **argv)
  163. {
  164. struct config_line_t *new;
  165. struct config_line_t *front = NULL;
  166. char *s;
  167. int i = 1;
  168. while (i < argc-1) {
  169. if (!strcmp(argv[i],"-f")) {
  170. // log(LOG_DEBUG,"Commandline: skipping over -f.");
  171. i += 2; /* this is the config file option. ignore it. */
  172. continue;
  173. }
  174. new = tor_malloc(sizeof(struct config_line_t));
  175. s = argv[i];
  176. while(*s == '-')
  177. s++;
  178. new->key = tor_strdup(expand_abbrev(s, 1));
  179. new->value = tor_strdup(argv[i+1]);
  180. log(LOG_DEBUG,"Commandline: parsed keyword '%s', value '%s'",
  181. new->key, new->value);
  182. new->next = front;
  183. front = new;
  184. i += 2;
  185. }
  186. return front;
  187. }
  188. /** Helper: allocate a new configuration option mapping 'key' to 'val',
  189. * prepend it to 'front', and return the newly allocated config_line_t */
  190. static struct config_line_t *
  191. config_line_prepend(struct config_line_t *front,
  192. const char *key,
  193. const char *val)
  194. {
  195. struct config_line_t *newline;
  196. newline = tor_malloc(sizeof(struct config_line_t));
  197. newline->key = tor_strdup(key);
  198. newline->value = tor_strdup(val);
  199. newline->next = front;
  200. return newline;
  201. }
  202. /** Helper: parse the config file and strdup into key/value
  203. * strings. Set *result to the list, or NULL if parsing the file
  204. * failed. Return 0 on success, -1 on failure. Warn and ignore any
  205. * misformatted lines. */
  206. static int
  207. config_get_lines(FILE *f, struct config_line_t **result)
  208. {
  209. struct config_line_t *front = NULL;
  210. char line[CONFIG_LINE_T_MAXLEN];
  211. int r;
  212. char *key, *value;
  213. while ((r = parse_line_from_file(line, sizeof(line), f, &key, &value)) > 0) {
  214. front = config_line_prepend(front, key, value);
  215. }
  216. if (r < 0) {
  217. *result = NULL;
  218. return -1;
  219. } else {
  220. *result = front;
  221. return 0;
  222. }
  223. }
  224. /**
  225. * Free all the configuration lines on the linked list <b>front</b>.
  226. */
  227. static void
  228. config_free_lines(struct config_line_t *front)
  229. {
  230. struct config_line_t *tmp;
  231. while (front) {
  232. tmp = front;
  233. front = tmp->next;
  234. tor_free(tmp->key);
  235. tor_free(tmp->value);
  236. tor_free(tmp);
  237. }
  238. }
  239. /** If <b>key</b> is a configuration option, return the corresponding
  240. * config_var_t. Otherwise, if <b>key</b> is a non-standard abbreviation,
  241. * warn, and return the corresponding config_var_t. Otherwise return NULL.
  242. */
  243. static config_var_t *config_find_option(const char *key)
  244. {
  245. int i;
  246. /* First, check for an exact (case-insensitive) match */
  247. for (i=0; config_vars[i].name; ++i) {
  248. if (!strcasecmp(key, config_vars[i].name))
  249. return &config_vars[i];
  250. }
  251. /* If none, check for an abbreviated match */
  252. for (i=0; config_vars[i].name; ++i) {
  253. if (!strncasecmp(key, config_vars[i].name, strlen(key))) {
  254. log_fn(LOG_WARN, "The abbreviation '%s' is deprecated. "
  255. "Tell Nick and Roger to make it official, or just use '%s' instead",
  256. key, config_vars[i].name);
  257. return &config_vars[i];
  258. }
  259. }
  260. /* Okay, unrecogized options */
  261. return NULL;
  262. }
  263. /** If <b>c</b> is a syntactically valid configuration line, update
  264. * <b>options</b> with its value and return 0. Otherwise return -1. */
  265. static int
  266. config_assign_line(or_options_t *options, struct config_line_t *c)
  267. {
  268. int i, ok;
  269. config_var_t *var;
  270. void *lvalue;
  271. var = config_find_option(c->key);
  272. if (!var) {
  273. log_fn(LOG_WARN, "Unknown option '%s'. Failing.", c->key);
  274. return -1;
  275. }
  276. /* Put keyword into canonical case. */
  277. if (strcmp(var->name, c->key)) {
  278. tor_free(c->key);
  279. c->key = tor_strdup(var->name);
  280. }
  281. lvalue = ((void*)options) + var->var_offset;
  282. switch(var->type) {
  283. case CONFIG_TYPE_UINT:
  284. i = tor_parse_long(c->value, 10, 0, INT_MAX, &ok, NULL);
  285. if (!ok) {
  286. log(LOG_WARN, "Int keyword '%s %s' is malformed or out of bounds. Skipping.",
  287. c->key,c->value);
  288. return 0;
  289. }
  290. *(int *)lvalue = i;
  291. break;
  292. case CONFIG_TYPE_BOOL:
  293. i = tor_parse_long(c->value, 10, 0, 1, &ok, NULL);
  294. if (!ok) {
  295. log(LOG_WARN, "Boolean keyword '%s' expects 0 or 1. Skipping.", c->key);
  296. return 0;
  297. }
  298. *(int *)lvalue = i;
  299. break;
  300. case CONFIG_TYPE_STRING:
  301. tor_free(*(char **)lvalue);
  302. *(char **)lvalue = tor_strdup(c->value);
  303. break;
  304. case CONFIG_TYPE_DOUBLE:
  305. *(double *)lvalue = atof(c->value);
  306. break;
  307. case CONFIG_TYPE_CSV:
  308. if (*(smartlist_t**)lvalue == NULL)
  309. *(smartlist_t**)lvalue = smartlist_create();
  310. smartlist_split_string(*(smartlist_t**)lvalue, c->value, ",",
  311. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  312. break;
  313. case CONFIG_TYPE_LINELIST:
  314. /* Note: this reverses the order that the lines appear in. That's
  315. * just fine, since we build up the list of lines reversed in the
  316. * first place. */
  317. *(struct config_line_t**)lvalue =
  318. config_line_prepend(*(struct config_line_t**)lvalue, c->key, c->value);
  319. break;
  320. case CONFIG_TYPE_OBSOLETE:
  321. log_fn(LOG_WARN, "Skipping obsolete configuration option '%s'", c->key);
  322. break;
  323. }
  324. return 0;
  325. }
  326. /** Iterate through the linked list of options <b>list</b>.
  327. * For each item, convert as appropriate and assign to <b>options</b>.
  328. * If an item is unrecognized, return -1 immediately,
  329. * else return 0 for success. */
  330. static int
  331. config_assign(or_options_t *options, struct config_line_t *list)
  332. {
  333. while (list) {
  334. const char *full = expand_abbrev(list->key, 0);
  335. if (strcmp(full,list->key)) {
  336. tor_free(list->key);
  337. list->key = tor_strdup(full);
  338. }
  339. if (config_assign_line(options, list))
  340. return -1;
  341. list = list->next;
  342. }
  343. return 0;
  344. }
  345. static void
  346. add_default_trusted_dirservers(void)
  347. {
  348. /* moria1 */
  349. parse_dir_server_line("18.244.0.188:9031 "
  350. "FFCB 46DB 1339 DA84 674C 70D7 CB58 6434 C437 0441");
  351. /* moria2 */
  352. parse_dir_server_line("18.244.0.114:80 "
  353. "719B E45D E224 B607 C537 07D0 E214 3E2D 423E 74CF");
  354. /* tor26 */
  355. parse_dir_server_line("62.116.124.106:9030 "
  356. "847B 1F85 0344 D787 6491 A548 92F9 0493 4E4E B85D");
  357. }
  358. /** Set <b>options</b> to a reasonable default.
  359. *
  360. * Call this function before we parse the torrc file.
  361. */
  362. static int
  363. config_assign_defaults(or_options_t *options)
  364. {
  365. /* set them up as a client only */
  366. options->SocksPort = 9050;
  367. options->AllowUnverifiedNodes = smartlist_create();
  368. smartlist_add(options->AllowUnverifiedNodes, "middle");
  369. smartlist_add(options->AllowUnverifiedNodes, "rendezvous");
  370. config_free_lines(options->ExitPolicy);
  371. options->ExitPolicy = NULL;
  372. return 0;
  373. }
  374. /** Print a usage message for tor. */
  375. static void
  376. print_usage(void)
  377. {
  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. printf("\nServer options:\n"
  389. "-n <nick>\t\tNickname of router\n"
  390. "-o <port>\t\tOR port to bind to\n"
  391. "-p <file>\t\tPID file\n");
  392. }
  393. /**
  394. * Based on <b>address</b>, guess our public IP address and put it
  395. * in <b>addr</b>.
  396. */
  397. int
  398. resolve_my_address(const char *address, uint32_t *addr)
  399. {
  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 *
  438. get_default_nickname(void)
  439. {
  440. char localhostname[256];
  441. char *cp, *out, *outp;
  442. if (gethostname(localhostname, sizeof(localhostname)) < 0) {
  443. log_fn(LOG_WARN,"Error obtaining local hostname");
  444. return NULL;
  445. }
  446. /* Put it in lowercase; stop at the first dot. */
  447. for (cp = localhostname; *cp; ++cp) {
  448. if (*cp == '.') {
  449. *cp = '\0';
  450. break;
  451. }
  452. *cp = tolower(*cp);
  453. }
  454. /* Strip invalid characters. */
  455. cp = localhostname;
  456. out = outp = tor_malloc(strlen(localhostname) + 1);
  457. while (*cp) {
  458. if (strchr(LEGAL_NICKNAME_CHARACTERS, *cp))
  459. *outp++ = *cp++;
  460. else
  461. cp++;
  462. }
  463. *outp = '\0';
  464. /* Enforce length. */
  465. if (strlen(out) > MAX_NICKNAME_LEN)
  466. out[MAX_NICKNAME_LEN]='\0';
  467. return out;
  468. }
  469. /** Release storage held by <b>options</b> */
  470. static void
  471. free_options(or_options_t *options)
  472. {
  473. config_free_lines(options->LogOptions);
  474. tor_free(options->ContactInfo);
  475. tor_free(options->DebugLogFile);
  476. tor_free(options->DataDirectory);
  477. tor_free(options->Nickname);
  478. tor_free(options->Address);
  479. tor_free(options->PidFile);
  480. tor_free(options->ExitNodes);
  481. tor_free(options->EntryNodes);
  482. tor_free(options->ExcludeNodes);
  483. tor_free(options->RendNodes);
  484. tor_free(options->RendExcludeNodes);
  485. tor_free(options->OutboundBindAddress);
  486. tor_free(options->User);
  487. tor_free(options->Group);
  488. tor_free(options->HttpProxy);
  489. config_free_lines(options->RendConfigLines);
  490. config_free_lines(options->SocksBindAddress);
  491. config_free_lines(options->ORBindAddress);
  492. config_free_lines(options->DirBindAddress);
  493. config_free_lines(options->ExitPolicy);
  494. config_free_lines(options->SocksPolicy);
  495. config_free_lines(options->DirPolicy);
  496. config_free_lines(options->DirServers);
  497. config_free_lines(options->RecommendedVersions);
  498. config_free_lines(options->NodeFamilies);
  499. config_free_lines(options->RedirectExit);
  500. if (options->RedirectExitList) {
  501. SMARTLIST_FOREACH(options->RedirectExitList,
  502. exit_redirect_t *, p, tor_free(p));
  503. smartlist_free(options->RedirectExitList);
  504. options->RedirectExitList = NULL;
  505. }
  506. if (options->FirewallPorts) {
  507. SMARTLIST_FOREACH(options->FirewallPorts, char *, cp, tor_free(cp));
  508. smartlist_free(options->FirewallPorts);
  509. options->FirewallPorts = NULL;
  510. }
  511. }
  512. /** Set <b>options</b> to hold reasonable defaults for most options.
  513. * Each option defaults to zero. */
  514. static void
  515. init_options(or_options_t *options)
  516. {
  517. memset(options,0,sizeof(or_options_t));
  518. options->ExitNodes = tor_strdup("");
  519. options->EntryNodes = tor_strdup("");
  520. options->StrictEntryNodes = options->StrictExitNodes = 0;
  521. options->ExcludeNodes = tor_strdup("");
  522. options->RendNodes = tor_strdup("");
  523. options->RendExcludeNodes = tor_strdup("");
  524. /* options->PidFile = tor_strdup("tor.pid"); */
  525. options->PathlenCoinWeight = 0.3;
  526. options->MaxConn = 900;
  527. options->DirFetchPostPeriod = 600;
  528. options->KeepalivePeriod = 300;
  529. options->MaxOnionsPending = 100;
  530. options->NewCircuitPeriod = 30; /* twice a minute */
  531. options->BandwidthRate = 800000; /* at most 800kB/s total sustained incoming */
  532. options->BandwidthBurst = 10000000; /* max burst on the token bucket */
  533. options->NumCpus = 1;
  534. }
  535. #ifdef MS_WINDOWS
  536. static char *get_windows_conf_root(void)
  537. {
  538. static int is_set = 0;
  539. static char path[MAX_PATH+1];
  540. LPITEMIDLIST idl;
  541. IMalloc *m;
  542. HRESULT result;
  543. if (is_set)
  544. return path;
  545. /* Find X:\documents and settings\username\applicatation data\ .
  546. * We would use SHGetSpecialFolder path, but that wasn't added until IE4.
  547. */
  548. if (!SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA,
  549. &idl))) {
  550. GetCurrentDirectory(MAX_PATH, path);
  551. is_set = 1;
  552. log_fn(LOG_WARN, "I couldn't find your application data folder: are you running an ancient version of Windows 95? Defaulting to '%s'", path);
  553. return path;
  554. }
  555. /* Convert the path from an "ID List" (whatever that is!) to a path. */
  556. result = SHGetPathFromIDList(idl, path);
  557. /* Now we need to free the */
  558. SHGetMalloc(&m);
  559. if (m) {
  560. m->lpVtbl->Free(m, idl);
  561. m->lpVtbl->Release(m);
  562. }
  563. if (!SUCCEEDED(result)) {
  564. return NULL;
  565. }
  566. strlcat(path,"\\tor",MAX_PATH);
  567. is_set = 1;
  568. return path;
  569. }
  570. #endif
  571. static char *
  572. get_default_conf_file(void)
  573. {
  574. #ifdef MS_WINDOWS
  575. char *path = tor_malloc(MAX_PATH);
  576. strlcpy(path, get_windows_conf_root(), MAX_PATH);
  577. strlcat(path,"\\torrc",MAX_PATH);
  578. return path;
  579. #else
  580. return tor_strdup(CONFDIR "/torrc");
  581. #endif
  582. }
  583. /** Verify whether lst is a string containing valid-looking space-separated
  584. * nicknames, or NULL. Return 0 on success. Warn and return -1 on failure.
  585. */
  586. static int check_nickname_list(const char *lst, const char *name)
  587. {
  588. int r = 0;
  589. smartlist_t *sl;
  590. if (!lst)
  591. return 0;
  592. sl = smartlist_create();
  593. smartlist_split_string(sl, lst, ",", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  594. SMARTLIST_FOREACH(sl, const char *, s,
  595. {
  596. if (!is_legal_nickname_or_hexdigest(s)) {
  597. log_fn(LOG_WARN, "Invalid nickname '%s' in %s line", s, name);
  598. r = -1;
  599. }
  600. });
  601. SMARTLIST_FOREACH(sl, char *, s, tor_free(s));
  602. smartlist_free(sl);
  603. return r;
  604. }
  605. /** Read a configuration file into <b>options</b>, finding the configuration
  606. * file location based on the command line. After loading the options,
  607. * validate them for consistency. Return 0 if success, <0 if failure. */
  608. int
  609. getconfig(int argc, char **argv, or_options_t *options)
  610. {
  611. struct config_line_t *cl;
  612. FILE *cf;
  613. char *fname;
  614. int i;
  615. int result = 0;
  616. static int first_load = 1;
  617. static char **backup_argv;
  618. static int backup_argc;
  619. char *previous_pidfile = NULL;
  620. int previous_runasdaemon = 0;
  621. int previous_orport = -1;
  622. int using_default_torrc;
  623. if (first_load) { /* first time we're called. save commandline args */
  624. backup_argv = argv;
  625. backup_argc = argc;
  626. first_load = 0;
  627. } else { /* we're reloading. need to clean up old ones first. */
  628. argv = backup_argv;
  629. argc = backup_argc;
  630. /* record some previous values, so we can fail if they change */
  631. if (options->PidFile)
  632. previous_pidfile = tor_strdup(options->PidFile);
  633. previous_runasdaemon = options->RunAsDaemon;
  634. previous_orport = options->ORPort;
  635. free_options(options);
  636. }
  637. init_options(options);
  638. if (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1],"--help"))) {
  639. print_usage();
  640. exit(0);
  641. }
  642. if (argc > 1 && (!strcmp(argv[1],"--version"))) {
  643. printf("Tor version %s.\n",VERSION);
  644. exit(0);
  645. }
  646. /* learn config file name, get config lines, assign them */
  647. i = 1;
  648. while (i < argc-1 && strcmp(argv[i],"-f")) {
  649. i++;
  650. }
  651. if (i < argc-1) { /* we found one */
  652. fname = tor_strdup(argv[i+1]);
  653. using_default_torrc = 0;
  654. } else {
  655. /* didn't find one, try CONFDIR */
  656. char *fn;
  657. using_default_torrc = 1;
  658. fn = get_default_conf_file();
  659. if (fn && file_status(fn) == FN_FILE) {
  660. fname = fn;
  661. } else {
  662. tor_free(fn);
  663. fn = expand_filename("~/.torrc");
  664. if (fn && file_status(fn) == FN_FILE) {
  665. fname = fn;
  666. } else {
  667. tor_free(fn);
  668. fname = get_default_conf_file();
  669. }
  670. }
  671. }
  672. tor_assert(fname);
  673. log(LOG_DEBUG, "Opening config file '%s'", fname);
  674. if (config_assign_defaults(options) < 0) {
  675. return -1;
  676. }
  677. cf = fopen(fname, "r");
  678. if (!cf) {
  679. if (using_default_torrc == 1) {
  680. log(LOG_NOTICE, "Configuration file '%s' not present, "
  681. "using reasonable defaults.", fname);
  682. tor_free(fname);
  683. } else {
  684. log(LOG_WARN, "Unable to open configuration file '%s'.", fname);
  685. tor_free(fname);
  686. return -1;
  687. }
  688. } else { /* it opened successfully. use it. */
  689. tor_free(fname);
  690. if (config_get_lines(cf, &cl)<0)
  691. return -1;
  692. if (config_assign(options,cl) < 0)
  693. return -1;
  694. config_free_lines(cl);
  695. fclose(cf);
  696. }
  697. /* go through command-line variables too */
  698. cl = config_get_commandlines(argc,argv);
  699. if (config_assign(options,cl) < 0)
  700. return -1;
  701. config_free_lines(cl);
  702. /* Validate options */
  703. /* first check if any of the previous options have changed but aren't allowed to */
  704. if (previous_pidfile && strcmp(previous_pidfile,options->PidFile)) {
  705. log_fn(LOG_WARN,"During reload, PidFile changed from %s to %s. Failing.",
  706. previous_pidfile, options->PidFile);
  707. return -1;
  708. }
  709. tor_free(previous_pidfile);
  710. if (previous_runasdaemon && !options->RunAsDaemon) {
  711. log_fn(LOG_WARN,"During reload, change from RunAsDaemon=1 to =0 not allowed. Failing.");
  712. return -1;
  713. }
  714. if (previous_orport == 0 && options->ORPort > 0) {
  715. log_fn(LOG_WARN,"During reload, change from ORPort=0 to >0 not allowed. Failing.");
  716. return -1;
  717. }
  718. if (options->ORPort < 0 || options->ORPort > 65535) {
  719. log(LOG_WARN, "ORPort option out of bounds.");
  720. result = -1;
  721. }
  722. if (options->Nickname == NULL) {
  723. if (server_mode()) {
  724. if (!(options->Nickname = get_default_nickname()))
  725. return -1;
  726. log_fn(LOG_NOTICE, "Choosing default nickname %s", options->Nickname);
  727. }
  728. } else {
  729. if (strspn(options->Nickname, LEGAL_NICKNAME_CHARACTERS) !=
  730. strlen(options->Nickname)) {
  731. log_fn(LOG_WARN, "Nickname '%s' contains illegal characters.", options->Nickname);
  732. result = -1;
  733. }
  734. if (strlen(options->Nickname) == 0) {
  735. log_fn(LOG_WARN, "Nickname must have at least one character");
  736. result = -1;
  737. }
  738. if (strlen(options->Nickname) > MAX_NICKNAME_LEN) {
  739. log_fn(LOG_WARN, "Nickname '%s' has more than %d characters.",
  740. options->Nickname, MAX_NICKNAME_LEN);
  741. result = -1;
  742. }
  743. }
  744. if (server_mode()) {
  745. /* confirm that our address isn't broken, so we can complain now */
  746. uint32_t tmp;
  747. if (resolve_my_address(options->Address, &tmp) < 0)
  748. result = -1;
  749. }
  750. if (options->SocksPort < 0 || options->SocksPort > 65535) {
  751. log(LOG_WARN, "SocksPort option out of bounds.");
  752. result = -1;
  753. }
  754. if (options->SocksPort == 0 && options->ORPort == 0) {
  755. log(LOG_WARN, "SocksPort and ORPort are both undefined? Quitting.");
  756. result = -1;
  757. }
  758. if (options->DirPort < 0 || options->DirPort > 65535) {
  759. log(LOG_WARN, "DirPort option out of bounds.");
  760. result = -1;
  761. }
  762. if (options->StrictExitNodes && !strlen(options->ExitNodes)) {
  763. log(LOG_WARN, "StrictExitNodes set, but no ExitNodes listed.");
  764. }
  765. if (options->StrictEntryNodes && !strlen(options->EntryNodes)) {
  766. log(LOG_WARN, "StrictEntryNodes set, but no EntryNodes listed.");
  767. }
  768. if (options->AuthoritativeDir && options->RecommendedVersions == NULL) {
  769. log(LOG_WARN, "Directory servers must configure RecommendedVersions.");
  770. result = -1;
  771. }
  772. if (options->AuthoritativeDir && !options->DirPort) {
  773. log(LOG_WARN, "Running as authoritative directory, but no DirPort set.");
  774. result = -1;
  775. }
  776. if (options->AuthoritativeDir && !options->ORPort) {
  777. log(LOG_WARN, "Running as authoritative directory, but no ORPort set.");
  778. result = -1;
  779. }
  780. if (options->AuthoritativeDir && options->ClientOnly) {
  781. log(LOG_WARN, "Running as authoritative directory, but ClientOnly also set.");
  782. result = -1;
  783. }
  784. if (options->FascistFirewall && !options->FirewallPorts) {
  785. options->FirewallPorts = smartlist_create();
  786. smartlist_add(options->FirewallPorts, tor_strdup("80"));
  787. smartlist_add(options->FirewallPorts, tor_strdup("443"));
  788. }
  789. if (options->FirewallPorts) {
  790. SMARTLIST_FOREACH(options->FirewallPorts, const char *, cp,
  791. {
  792. i = atoi(cp);
  793. if (i < 1 || i > 65535) {
  794. log(LOG_WARN, "Port '%s' out of range in FirewallPorts", cp);
  795. result=-1;
  796. }
  797. });
  798. }
  799. options->_AllowUnverified = 0;
  800. if (options->AllowUnverifiedNodes) {
  801. SMARTLIST_FOREACH(options->AllowUnverifiedNodes, const char *, cp, {
  802. if (!strcasecmp(cp, "entry"))
  803. options->_AllowUnverified |= ALLOW_UNVERIFIED_ENTRY;
  804. else if (!strcasecmp(cp, "exit"))
  805. options->_AllowUnverified |= ALLOW_UNVERIFIED_EXIT;
  806. else if (!strcasecmp(cp, "middle"))
  807. options->_AllowUnverified |= ALLOW_UNVERIFIED_MIDDLE;
  808. else if (!strcasecmp(cp, "introduction"))
  809. options->_AllowUnverified |= ALLOW_UNVERIFIED_INTRODUCTION;
  810. else if (!strcasecmp(cp, "rendezvous"))
  811. options->_AllowUnverified |= ALLOW_UNVERIFIED_RENDEZVOUS;
  812. else {
  813. log(LOG_WARN, "Unrecognized value '%s' in AllowUnverifiedNodes",
  814. cp);
  815. result=-1;
  816. }
  817. });
  818. }
  819. if (options->SocksPort >= 1 &&
  820. (options->PathlenCoinWeight < 0.0 || options->PathlenCoinWeight >= 1.0)) {
  821. log(LOG_WARN, "PathlenCoinWeight option must be >=0.0 and <1.0.");
  822. result = -1;
  823. }
  824. if (options->MaxConn < 1) {
  825. log(LOG_WARN, "MaxConn option must be a non-zero positive integer.");
  826. result = -1;
  827. }
  828. if (options->MaxConn >= MAXCONNECTIONS) {
  829. log(LOG_WARN, "MaxConn option must be less than %d.", MAXCONNECTIONS);
  830. result = -1;
  831. }
  832. #define MIN_DIRFETCHPOSTPERIOD 60
  833. if (options->DirFetchPostPeriod < MIN_DIRFETCHPOSTPERIOD) {
  834. log(LOG_WARN, "DirFetchPostPeriod option must be at least %d.", MIN_DIRFETCHPOSTPERIOD);
  835. result = -1;
  836. }
  837. if (options->DirFetchPostPeriod > MIN_ONION_KEY_LIFETIME / 2) {
  838. log(LOG_WARN, "DirFetchPostPeriod is too large; clipping.");
  839. options->DirFetchPostPeriod = MIN_ONION_KEY_LIFETIME / 2;
  840. }
  841. if (options->KeepalivePeriod < 1) {
  842. log(LOG_WARN,"KeepalivePeriod option must be positive.");
  843. result = -1;
  844. }
  845. if (options->HttpProxy) { /* parse it now */
  846. if (parse_addr_port(options->HttpProxy, NULL,
  847. &options->HttpProxyAddr, &options->HttpProxyPort) < 0) {
  848. log(LOG_WARN,"HttpProxy failed to parse or resolve. Please fix.");
  849. result = -1;
  850. }
  851. if (options->HttpProxyPort == 0) { /* give it a default */
  852. options->HttpProxyPort = 80;
  853. }
  854. }
  855. if (check_nickname_list(options->ExitNodes, "ExitNodes"))
  856. return -1;
  857. if (check_nickname_list(options->EntryNodes, "EntryNodes"))
  858. return -1;
  859. if (check_nickname_list(options->ExcludeNodes, "ExcludeNodes"))
  860. return -1;
  861. if (check_nickname_list(options->RendNodes, "RendNodes"))
  862. return -1;
  863. if (check_nickname_list(options->RendNodes, "RendExcludeNodes"))
  864. return -1;
  865. if (check_nickname_list(options->MyFamily, "MyFamily"))
  866. return -1;
  867. for (cl = options->NodeFamilies; cl; cl = cl->next) {
  868. if (check_nickname_list(cl->value, "NodeFamily"))
  869. return -1;
  870. }
  871. if (!options->RedirectExitList)
  872. options->RedirectExitList = smartlist_create();
  873. for (cl = options->RedirectExit; cl; cl = cl->next) {
  874. if (parse_redirect_line(options, cl)<0)
  875. return -1;
  876. }
  877. clear_trusted_dir_servers();
  878. if (!options->DirServers) {
  879. add_default_trusted_dirservers();
  880. } else {
  881. for (cl = options->DirServers; cl; cl = cl->next) {
  882. if (parse_dir_server_line(cl->value)<0)
  883. return -1;
  884. }
  885. }
  886. if (rend_config_services(options) < 0) {
  887. result = -1;
  888. }
  889. return result;
  890. }
  891. static int
  892. add_single_log(struct config_line_t *level_opt,
  893. struct config_line_t *file_opt, int isDaemon)
  894. {
  895. int levelMin = -1, levelMax = -1;
  896. char *cp, *tmp_sev;
  897. if (level_opt) {
  898. cp = strchr(level_opt->value, '-');
  899. if (cp) {
  900. tmp_sev = tor_strndup(level_opt->value, cp - level_opt->value);
  901. levelMin = parse_log_level(tmp_sev);
  902. if (levelMin < 0) {
  903. log_fn(LOG_WARN, "Unrecognized log severity '%s': must be one of "
  904. "err|warn|notice|info|debug", tmp_sev);
  905. return -1;
  906. }
  907. tor_free(tmp_sev);
  908. levelMax = parse_log_level(cp+1);
  909. if (levelMax < 0) {
  910. log_fn(LOG_WARN, "Unrecognized log severity '%s': must be one of "
  911. "err|warn|notice|info|debug", cp+1);
  912. return -1;
  913. }
  914. } else {
  915. levelMin = parse_log_level(level_opt->value);
  916. if (levelMin < 0) {
  917. log_fn(LOG_WARN, "Unrecognized log severity '%s': must be one of "
  918. "err|warn|notice|info|debug", level_opt->value);
  919. return -1;
  920. }
  921. }
  922. }
  923. if (levelMin < 0 && levelMax < 0) {
  924. levelMin = LOG_NOTICE;
  925. levelMax = LOG_ERR;
  926. } else if (levelMin < 0) {
  927. levelMin = levelMax;
  928. } else {
  929. levelMax = LOG_ERR;
  930. }
  931. if (file_opt && !strcasecmp(file_opt->key, "LogFile")) {
  932. if (add_file_log(levelMin, levelMax, file_opt->value) < 0) {
  933. log_fn(LOG_WARN, "Cannot write to LogFile '%s': %s.", file_opt->value,
  934. strerror(errno));
  935. return -1;
  936. }
  937. log_fn(LOG_NOTICE, "Successfully opened LogFile '%s', redirecting output.",
  938. file_opt->value);
  939. } else if (file_opt && !strcasecmp(file_opt->key, "SysLog")) {
  940. #ifdef HAVE_SYSLOG_H
  941. if (add_syslog_log(levelMin, levelMax) < 0) {
  942. log_fn(LOG_WARN, "Cannot open system log facility");
  943. return -1;
  944. }
  945. log_fn(LOG_NOTICE, "Successfully opened system log, redirecting output.");
  946. #else
  947. log_fn(LOG_WARN, "Tor was compiled without system logging enabled; can't enable SysLog.");
  948. #endif
  949. } else if (!isDaemon) {
  950. add_stream_log(levelMin, levelMax, "<stdout>", stdout);
  951. close_temp_logs();
  952. }
  953. return 0;
  954. }
  955. /**
  956. * Initialize the logs based on the configuration file.
  957. */
  958. int
  959. config_init_logs(or_options_t *options)
  960. {
  961. /* The order of options is: Level? (File Level?)+
  962. */
  963. struct config_line_t *opt = options->LogOptions;
  964. /* Special case if no options are given. */
  965. if (!opt) {
  966. add_stream_log(LOG_NOTICE, LOG_ERR, "<stdout>", stdout);
  967. close_temp_logs();
  968. /* don't return yet, in case we want to do a debuglogfile below */
  969. }
  970. /* Special case for if first option is LogLevel. */
  971. if (opt && !strcasecmp(opt->key, "LogLevel")) {
  972. if (opt->next && (!strcasecmp(opt->next->key, "LogFile") ||
  973. !strcasecmp(opt->next->key, "SysLog"))) {
  974. if (add_single_log(opt, opt->next, options->RunAsDaemon) < 0)
  975. return -1;
  976. opt = opt->next->next;
  977. } else if (!opt->next) {
  978. if (add_single_log(opt, NULL, options->RunAsDaemon) < 0)
  979. return -1;
  980. opt = opt->next;
  981. } else {
  982. ; /* give warning below */
  983. }
  984. }
  985. while (opt) {
  986. if (!strcasecmp(opt->key, "LogLevel")) {
  987. log_fn(LOG_WARN, "Two LogLevel options in a row without intervening LogFile or SysLog");
  988. opt = opt->next;
  989. } else {
  990. tor_assert(!strcasecmp(opt->key, "LogFile") ||
  991. !strcasecmp(opt->key, "SysLog"));
  992. if (opt->next && !strcasecmp(opt->next->key, "LogLevel")) {
  993. /* LogFile/SysLog followed by LogLevel */
  994. if (add_single_log(opt->next, opt, options->RunAsDaemon) < 0)
  995. return -1;
  996. opt = opt->next->next;
  997. } else {
  998. /* LogFile/SysLog followed by LogFile/SysLog or end of list. */
  999. if (add_single_log(NULL, opt, options->RunAsDaemon) < 0)
  1000. return -1;
  1001. opt = opt->next;
  1002. }
  1003. }
  1004. }
  1005. if (options->DebugLogFile) {
  1006. log_fn(LOG_WARN, "DebugLogFile is deprecated; use LogFile and LogLevel instead");
  1007. if (add_file_log(LOG_DEBUG, LOG_ERR, options->DebugLogFile) < 0)
  1008. return -1;
  1009. }
  1010. return 0;
  1011. }
  1012. /**
  1013. * Given a linked list of config lines containing "allow" and "deny" tokens,
  1014. * parse them and place the result in <b>dest</b>. Skip malformed lines.
  1015. */
  1016. void
  1017. config_parse_exit_policy(struct config_line_t *cfg,
  1018. struct exit_policy_t **dest)
  1019. {
  1020. struct exit_policy_t **nextp;
  1021. smartlist_t *entries;
  1022. if (!cfg)
  1023. return;
  1024. nextp = dest;
  1025. while (*nextp)
  1026. nextp = &((*nextp)->next);
  1027. entries = smartlist_create();
  1028. for (; cfg; cfg = cfg->next) {
  1029. smartlist_split_string(entries, cfg->value, ",", SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1030. SMARTLIST_FOREACH(entries, const char *, ent,
  1031. {
  1032. log_fn(LOG_DEBUG,"Adding new entry '%s'",ent);
  1033. *nextp = router_parse_exit_policy_from_string(ent);
  1034. if (*nextp) {
  1035. nextp = &((*nextp)->next);
  1036. } else {
  1037. log_fn(LOG_WARN,"Malformed exit policy %s; skipping.", ent);
  1038. }
  1039. });
  1040. SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
  1041. smartlist_clear(entries);
  1042. }
  1043. smartlist_free(entries);
  1044. }
  1045. void exit_policy_free(struct exit_policy_t *p) {
  1046. struct exit_policy_t *e;
  1047. while (p) {
  1048. e = p;
  1049. p = p->next;
  1050. tor_free(e->string);
  1051. tor_free(e);
  1052. }
  1053. }
  1054. static int parse_redirect_line(or_options_t *options,
  1055. struct config_line_t *line)
  1056. {
  1057. smartlist_t *elements = NULL;
  1058. exit_redirect_t *r;
  1059. tor_assert(options);
  1060. tor_assert(options->RedirectExitList);
  1061. tor_assert(line);
  1062. r = tor_malloc_zero(sizeof(exit_redirect_t));
  1063. elements = smartlist_create();
  1064. smartlist_split_string(elements, line->value, " ",
  1065. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  1066. if (smartlist_len(elements) != 2) {
  1067. log_fn(LOG_WARN, "Wrong number of elements in RedirectExit line");
  1068. goto err;
  1069. }
  1070. if (parse_addr_and_port_range(smartlist_get(elements,0),&r->addr,&r->mask,
  1071. &r->port_min,&r->port_max)) {
  1072. log_fn(LOG_WARN, "Error parsing source address in RedirectExit line");
  1073. goto err;
  1074. }
  1075. if (0==strcasecmp(smartlist_get(elements,1), "pass")) {
  1076. r->is_redirect = 0;
  1077. } else {
  1078. if (parse_addr_port(smartlist_get(elements,1),NULL,&r->addr_dest,
  1079. &r->port_dest)) {
  1080. log_fn(LOG_WARN, "Error parseing dest address in RedirectExit line");
  1081. goto err;
  1082. }
  1083. r->is_redirect = 1;
  1084. }
  1085. goto done;
  1086. err:
  1087. tor_free(r);
  1088. done:
  1089. SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
  1090. smartlist_free(elements);
  1091. if (r) {
  1092. smartlist_add(options->RedirectExitList, r);
  1093. return 0;
  1094. } else {
  1095. return -1;
  1096. }
  1097. }
  1098. static int parse_dir_server_line(const char *line)
  1099. {
  1100. smartlist_t *items = NULL;
  1101. int r;
  1102. char *addrport, *address=NULL;
  1103. uint16_t port;
  1104. char digest[DIGEST_LEN];
  1105. items = smartlist_create();
  1106. smartlist_split_string(items, line, " ",
  1107. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
  1108. if (smartlist_len(items) < 2) {
  1109. log_fn(LOG_WARN, "Too few arguments to DirServer line.");
  1110. goto err;
  1111. }
  1112. addrport = smartlist_get(items, 0);
  1113. if (parse_addr_port(addrport, &address, NULL, &port)<0) {
  1114. log_fn(LOG_WARN, "Error parsing DirServer address '%s'", addrport);
  1115. goto err;
  1116. }
  1117. if (!port) {
  1118. log_fn(LOG_WARN, "Missing port in DirServe address '%s'",addrport);
  1119. goto err;
  1120. }
  1121. tor_strstrip(smartlist_get(items, 1), " ");
  1122. if (strlen(smartlist_get(items, 1)) != HEX_DIGEST_LEN) {
  1123. log_fn(LOG_WARN, "Key digest for DirServer is wrong length.");
  1124. goto err;
  1125. }
  1126. if (base16_decode(digest, DIGEST_LEN,
  1127. smartlist_get(items,1), HEX_DIGEST_LEN)<0) {
  1128. log_fn(LOG_WARN, "Unable to decode DirServer key digest.");
  1129. goto err;
  1130. }
  1131. log_fn(LOG_DEBUG, "Trusted dirserver at %s:%d (%s)", address, (int)port,
  1132. (char*)smartlist_get(items,1));
  1133. add_trusted_dir_server(address, port, digest);
  1134. r = 0;
  1135. goto done;
  1136. err:
  1137. r = -1;
  1138. done:
  1139. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  1140. smartlist_free(items);
  1141. if (address)
  1142. tor_free(address);
  1143. return r;
  1144. }
  1145. const char *
  1146. get_data_directory(or_options_t *options)
  1147. {
  1148. const char *d;
  1149. if (options->DataDirectory) {
  1150. d = options->DataDirectory;
  1151. } else {
  1152. #ifdef MS_WINDOWS
  1153. char *p;
  1154. p = tor_malloc(MAX_PATH);
  1155. strlcpy(p,get_windows_conf_root(),MAX_PATH);
  1156. options->DataDirectory = p;
  1157. return p;
  1158. #else
  1159. d = "~/.tor";
  1160. #endif
  1161. }
  1162. if (d && strncmp(d,"~/",2) == 0) {
  1163. char *fn = expand_filename(d);
  1164. if (!fn) {
  1165. log_fn(LOG_ERR,"Failed to expand filename '%s'. Exiting.", d);
  1166. exit(1);
  1167. }
  1168. tor_free(options->DataDirectory);
  1169. options->DataDirectory = fn;
  1170. }
  1171. return options->DataDirectory;
  1172. }
  1173. /*
  1174. Local Variables:
  1175. mode:c
  1176. indent-tabs-mode:nil
  1177. c-basic-offset:2
  1178. End:
  1179. */