config.c 34 KB

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