config.c 40 KB

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