config.c 34 KB

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