config.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /* enumeration of types which option values can take */
  6. #define CONFIG_TYPE_STRING 0
  7. #define CONFIG_TYPE_CHAR 1
  8. #define CONFIG_TYPE_INT 2
  9. #define CONFIG_TYPE_LONG 3
  10. #define CONFIG_TYPE_DOUBLE 4
  11. #define CONFIG_TYPE_BOOL 5
  12. #define CONFIG_TYPE_LINELIST 6
  13. #define CONFIG_LINE_T_MAXLEN 4096
  14. static FILE *config_open(const unsigned char *filename);
  15. static int config_close(FILE *f);
  16. static struct config_line_t *config_get_commandlines(int argc, char **argv);
  17. static struct config_line_t *config_get_lines(FILE *f);
  18. static void config_free_lines(struct config_line_t *front);
  19. static int config_compare(struct config_line_t *c, char *key, int type, void *arg);
  20. static int config_assign(or_options_t *options, struct config_line_t *list);
  21. /* open configuration file for reading */
  22. static FILE *config_open(const unsigned char *filename) {
  23. assert(filename);
  24. if (strspn(filename,CONFIG_LEGAL_FILENAME_CHARACTERS) != strlen(filename)) {
  25. /* filename has illegal letters */
  26. return NULL;
  27. }
  28. return fopen(filename, "r");
  29. }
  30. /* close configuration file */
  31. static int config_close(FILE *f) {
  32. assert(f);
  33. return fclose(f);
  34. }
  35. static struct config_line_t *config_get_commandlines(int argc, char **argv) {
  36. struct config_line_t *new;
  37. struct config_line_t *front = NULL;
  38. char *s;
  39. int i = 1;
  40. while(i < argc-1) {
  41. if(!strcmp(argv[i],"-f")) {
  42. // log(LOG_DEBUG,"Commandline: skipping over -f.");
  43. i+=2; /* this is the config file option. ignore it. */
  44. continue;
  45. }
  46. new = tor_malloc(sizeof(struct config_line_t));
  47. s = argv[i];
  48. while(*s == '-')
  49. s++;
  50. new->key = tor_strdup(s);
  51. new->value = tor_strdup(argv[i+1]);
  52. log(LOG_DEBUG,"Commandline: parsed keyword '%s', value '%s'",
  53. new->key, new->value);
  54. new->next = front;
  55. front = new;
  56. i += 2;
  57. }
  58. return front;
  59. }
  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. /* parse the config file and strdup into key/value strings. Return list,
  73. * or NULL if parsing the file failed.
  74. * Warn and ignore mangled lines. */
  75. static struct config_line_t *config_get_lines(FILE *f) {
  76. struct config_line_t *front = NULL;
  77. char line[CONFIG_LINE_T_MAXLEN];
  78. int result;
  79. char *key, *value;
  80. while( (result=parse_line_from_file(line,sizeof(line),f,&key,&value)) > 0) {
  81. front = config_line_prepend(front, key, value);
  82. }
  83. if(result < 0)
  84. return NULL;
  85. return front;
  86. }
  87. static void config_free_lines(struct config_line_t *front) {
  88. struct config_line_t *tmp;
  89. while(front) {
  90. tmp = front;
  91. front = tmp->next;
  92. free(tmp->key);
  93. free(tmp->value);
  94. free(tmp);
  95. }
  96. }
  97. static int config_compare(struct config_line_t *c, char *key, int type, void *arg) {
  98. int i;
  99. if(strncasecmp(c->key,key,strlen(c->key)))
  100. return 0;
  101. /* it's a match. cast and assign. */
  102. log_fn(LOG_DEBUG,"Recognized keyword '%s' as %s, using value '%s'.",c->key,key,c->value);
  103. switch(type) {
  104. case CONFIG_TYPE_INT:
  105. *(int *)arg = atoi(c->value);
  106. break;
  107. case CONFIG_TYPE_BOOL:
  108. i = atoi(c->value);
  109. if (i != 0 && i != 1) {
  110. log(LOG_WARN, "Boolean keyword '%s' expects 0 or 1", c->key);
  111. return 0;
  112. }
  113. *(int *)arg = i;
  114. break;
  115. case CONFIG_TYPE_STRING:
  116. tor_free(*(char **)arg);
  117. *(char **)arg = tor_strdup(c->value);
  118. break;
  119. case CONFIG_TYPE_DOUBLE:
  120. *(double *)arg = atof(c->value);
  121. break;
  122. case CONFIG_TYPE_LINELIST:
  123. /* Note: this reverses the order that the lines appear in. That's
  124. * just fine, since we build up the list of lines reversed in the
  125. * first place. */
  126. *(struct config_line_t**)arg =
  127. config_line_prepend(*(struct config_line_t**)arg, c->key, c->value);
  128. break;
  129. }
  130. return 1;
  131. }
  132. /* Iterate through list.
  133. * For each item, convert as appropriate and assign to 'options'.
  134. * If an item is unrecognized, return -1 immediately,
  135. * else return 0 for success. */
  136. static int config_assign(or_options_t *options, struct config_line_t *list) {
  137. while(list) {
  138. if(
  139. /* order matters here! abbreviated arguments use the first match. */
  140. /* string options */
  141. config_compare(list, "Address", CONFIG_TYPE_STRING, &options->Address) ||
  142. config_compare(list, "BandwidthRate", CONFIG_TYPE_INT, &options->BandwidthRate) ||
  143. config_compare(list, "BandwidthBurst", CONFIG_TYPE_INT, &options->BandwidthBurst) ||
  144. config_compare(list, "DebugLogFile", CONFIG_TYPE_STRING, &options->DebugLogFile) ||
  145. config_compare(list, "DataDirectory", CONFIG_TYPE_STRING, &options->DataDirectory) ||
  146. config_compare(list, "DirPort", CONFIG_TYPE_INT, &options->DirPort) ||
  147. config_compare(list, "DirBindAddress", CONFIG_TYPE_STRING, &options->DirBindAddress) ||
  148. config_compare(list, "DirFetchPostPeriod",CONFIG_TYPE_INT, &options->DirFetchPostPeriod) ||
  149. config_compare(list, "ExitNodes", CONFIG_TYPE_STRING, &options->ExitNodes) ||
  150. config_compare(list, "EntryNodes", CONFIG_TYPE_STRING, &options->EntryNodes) ||
  151. config_compare(list, "ExitPolicy", CONFIG_TYPE_STRING, &options->ExitPolicy) ||
  152. config_compare(list, "ExcludeNodes", CONFIG_TYPE_STRING, &options->ExcludeNodes) ||
  153. config_compare(list, "Group", CONFIG_TYPE_STRING, &options->Group) ||
  154. config_compare(list, "IgnoreVersion", CONFIG_TYPE_BOOL, &options->IgnoreVersion) ||
  155. config_compare(list, "KeepalivePeriod",CONFIG_TYPE_INT, &options->KeepalivePeriod) ||
  156. config_compare(list, "LogLevel", CONFIG_TYPE_STRING, &options->LogLevel) ||
  157. config_compare(list, "LogFile", CONFIG_TYPE_STRING, &options->LogFile) ||
  158. config_compare(list, "LinkPadding", CONFIG_TYPE_BOOL, &options->LinkPadding) ||
  159. config_compare(list, "MaxConn", CONFIG_TYPE_INT, &options->MaxConn) ||
  160. config_compare(list, "MaxOnionsPending",CONFIG_TYPE_INT, &options->MaxOnionsPending) ||
  161. config_compare(list, "Nickname", CONFIG_TYPE_STRING, &options->Nickname) ||
  162. config_compare(list, "NewCircuitPeriod",CONFIG_TYPE_INT, &options->NewCircuitPeriod) ||
  163. config_compare(list, "NumCpus", CONFIG_TYPE_INT, &options->NumCpus) ||
  164. config_compare(list, "ORPort", CONFIG_TYPE_INT, &options->ORPort) ||
  165. config_compare(list, "ORBindAddress", CONFIG_TYPE_STRING, &options->ORBindAddress) ||
  166. config_compare(list, "PidFile", CONFIG_TYPE_STRING, &options->PidFile) ||
  167. config_compare(list, "PathlenCoinWeight",CONFIG_TYPE_DOUBLE, &options->PathlenCoinWeight) ||
  168. config_compare(list, "RouterFile", CONFIG_TYPE_STRING, &options->RouterFile) ||
  169. config_compare(list, "RunAsDaemon", CONFIG_TYPE_BOOL, &options->RunAsDaemon) ||
  170. config_compare(list, "RecommendedVersions",CONFIG_TYPE_STRING, &options->RecommendedVersions) ||
  171. config_compare(list, "RendNodes", CONFIG_TYPE_STRING, &options->RendNodes) ||
  172. config_compare(list, "RendExcludeNodes",CONFIG_TYPE_STRING, &options->RendExcludeNodes) ||
  173. config_compare(list, "SocksPort", CONFIG_TYPE_INT, &options->SocksPort) ||
  174. config_compare(list, "SocksBindAddress",CONFIG_TYPE_STRING,&options->SocksBindAddress) ||
  175. config_compare(list, "TrafficShaping", CONFIG_TYPE_BOOL, &options->TrafficShaping) ||
  176. config_compare(list, "User", CONFIG_TYPE_STRING, &options->User) ||
  177. config_compare(list, "RunTesting", CONFIG_TYPE_BOOL, &options->RunTesting) ||
  178. config_compare(list, "HiddenServiceDir", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  179. config_compare(list, "HiddenServicePort", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  180. config_compare(list, "HiddenServiceNodes", CONFIG_TYPE_LINELIST, &options->RendConfigLines)||
  181. config_compare(list, "HiddenServiceExcludeNodes", CONFIG_TYPE_LINELIST, &options->RendConfigLines)
  182. ) {
  183. /* then we're ok. it matched something. */
  184. } else {
  185. log_fn(LOG_WARN,"Unknown keyword '%s'. Failing.",list->key);
  186. return -1;
  187. }
  188. list = list->next;
  189. }
  190. return 0;
  191. }
  192. /* XXX are there any other specifiers we want to give so making
  193. * a several-thousand-byte string is less painful? */
  194. const char default_dirservers_string[] =
  195. "router moria1 moria.mit.edu 9001 9021 9031 800000\n"
  196. "platform Tor 0.0.2pre8 on Linux moria.mit.edu 2.4.18-27.7.xbigmem #1 SMP Fri Mar 14 05:08:50 EST 2003 i686\n"
  197. "published 2003-09-30 23:14:08\n"
  198. "onion-key\n"
  199. "-----BEGIN RSA PUBLIC KEY-----\n"
  200. "MIGJAoGBANoIvHieyHUTzIacbnWOnyTyzGrLOdXqbcjz2GGMxyHEd5K1bO1ZBNHP\n"
  201. "9i5qLQpN5viFk2K2rEGuG8tFgDEzSWZEtBqv3NVfUdiumdERWMBwlaQ0MVK4C+jf\n"
  202. "y5gZ8KI3o9ZictgPS1AQF+Kk932/vIHTuRIUKb4ILTnQilNvID0NAgMBAAE=\n"
  203. "-----END RSA PUBLIC KEY-----\n"
  204. "link-key\n"
  205. "-----BEGIN RSA PUBLIC KEY-----\n"
  206. "MIGJAoGBAPt97bGDd9siVjPd7Xuq2s+amMEOLIj9961aSdP6/OT+BS1Q4TX2dNOX\n"
  207. "ZNAl63Z2fQISsR81+nfoqRLYCKxhajsD7LRvRTaRwUrWemVqFevmZ4nJrHw6FoU3\n"
  208. "xNUIHRMA8X2DZ+l5qgnWZb7JU50ohhX5OpMSyysXnik51J8hD5mBAgMBAAE=\n"
  209. "-----END RSA PUBLIC KEY-----\n"
  210. "signing-key\n"
  211. "-----BEGIN RSA PUBLIC KEY-----\n"
  212. "MIGJAoGBAMHa0ZC/jo2Q2DrwKYF/6ZbmZ27PFYG91u4gUzzmZ/VXLpZ8wNzEV3oW\n"
  213. "nt+I61048fBiC1frT1/DZ351n2bLSk9zJbB6jyGZJn0380FPRX3+cXyXS0Gq8Ril\n"
  214. "xkhMQf5XuNFUb8UmYPSOH4WErjvYjKvU+gfjbK/82Jo9SuHpYz+BAgMBAAE=\n"
  215. "-----END RSA PUBLIC KEY-----\n"
  216. "router-signature\n"
  217. "-----BEGIN SIGNATURE-----\n"
  218. "Td3zb5d6uxO8oYGlmEHGzIdLuVm9s1Afqtm29JvRnnviQ36j6FZPlzPUaMVOUayn\n"
  219. "Wtz/CbaMj7mHSufpQ68wCLb1lQrtQkn7MkAWcQPIvZjpYh3UrcWrpfm7f/D+nKeN\n"
  220. "Z7UovF36xhCacjATNHhQNHHZHH6yONwN+Rf/N4kyPHw=\n"
  221. "-----END SIGNATURE-----\n"
  222. "\n"
  223. "router moria2 moria.mit.edu 9002 9022 9032 800000\n"
  224. "platform Tor 0.0.2pre8 on Linux moria.mit.edu 2.4.18-27.7.xbigmem #1 SMP Fri Mar 14 05:08:50 EST 2003 i686\n"
  225. "published 2003-09-30 23:14:05\n"
  226. "onion-key\n"
  227. "-----BEGIN RSA PUBLIC KEY-----\n"
  228. "MIGJAoGBAM4Cc/npgYC54XrYLC+grVxJp7PDmNO2DRRJOxKttBBtvLpnR1UaueTi\n"
  229. "kyknT5kmlx+ihgZF/jmye//2dDUp2+kK/kSkpRV4xnDLXZmed+sNSQxqmm9TtZQ9\n"
  230. "/hjpxhp5J9HmUTYhntBs+4E4CUKokmrI6oRLoln4SA39AX9QLPcnAgMBAAE=\n"
  231. "-----END RSA PUBLIC KEY-----\n"
  232. "link-key\n"
  233. "-----BEGIN RSA PUBLIC KEY-----\n"
  234. "MIGJAoGBAN7JVeCIJ7+0ZJew5ScOU58rTUqjGt1Z1Rkursc7WabEb8jno45VZwIs\n"
  235. "dkjnl31i36KHyyS7kQdHgkvG5EiyZiRipFAcoTaYv3Gvf1No9cXL6IhT3y/37dJ/\n"
  236. "kFPEMb/G2wdkJCC+D8fMwHBwMuqAg0JGuhoBOz0ArCgK3fq0BLilAgMBAAE=\n"
  237. "-----END RSA PUBLIC KEY-----\n"
  238. "signing-key\n"
  239. "-----BEGIN RSA PUBLIC KEY-----\n"
  240. "MIGJAoGBAOcrht/y5rkaahfX7sMe2qnpqoPibsjTSJaDvsUtaNP/Bq0MgNDGOR48\n"
  241. "rtwfqTRff275Edkp/UYw3G3vSgKCJr76/bqOHCmkiZrnPV1zxNfrK18gNw2Cxre0\n"
  242. "nTA+fD8JQqpPtb8b0SnG9kwy75eS//sRu7TErie2PzGMxrf9LH0LAgMBAAE=\n"
  243. "-----END RSA PUBLIC KEY-----\n"
  244. "router-signature\n"
  245. "-----BEGIN SIGNATURE-----\n"
  246. "X10a9Oc0LKNYKLDVzjRTIVT3NnE0y+xncllDDHSJSXR97fz3MBHGDqhy0Vgha/fe\n"
  247. "H/Y2E59oG01lYQ73j3JN+ibsCMtkzJDx2agCpV0LmakAD9ekHrYDWm/S41Ru6kf+\n"
  248. "PsyHpXlh7cZuGEX4U1pblSDFrQZ9L1vTkpfW+COzEvI=\n"
  249. "-----END SIGNATURE-----\n"
  250. "\n"
  251. "router moria3 moria.mit.edu 9003 9023 9033 800000\n"
  252. "platform Tor 0.0.2pre8 on Linux moria.mit.edu 2.4.18-27.7.xbigmem #1 SMP Fri Mar 14 05:08:50 EST 2003 i686\n"
  253. "published 2003-09-30 23:14:07\n"
  254. "onion-key\n"
  255. "-----BEGIN RSA PUBLIC KEY-----\n"
  256. "MIGJAoGBANS6J/Er9fYo03fjUUVesc7We9Z6xIevyDJH39pYS4NUlcr5ExYgSVFJ\n"
  257. "95aLCNx1x8Rf5YtiBKYuT3plBO/+rfuX+0iAGNkz/y3SlJVGz6aeptU3wN8CkvCL\n"
  258. "zATEcnl4QSPhHX0wFB9A3t7wZ+Bat1PTI029lax/BkoS9JG5onHPAgMBAAE=\n"
  259. "-----END RSA PUBLIC KEY-----\n"
  260. "link-key\n"
  261. "-----BEGIN RSA PUBLIC KEY-----\n"
  262. "MIGJAoGBAKUMY8p+7LBu7dEJnOR9HqbfcD6c4/f9GqJt3o29uu4XJPD8z2XGVBik\n"
  263. "pZBLijhYS6U7GFg0NLR4zBlsLyB8TxHeaz5KJidJjy+BfC01jz1xwVTYDlmGVpc1\n"
  264. "0mw0Ag0ND6aOQKKhelxhTI3Bf0R9olEXuSUKEWx3EMIz2qhLd9oDAgMBAAE=\n"
  265. "-----END RSA PUBLIC KEY-----\n"
  266. "signing-key\n"
  267. "-----BEGIN RSA PUBLIC KEY-----\n"
  268. "MIGJAoGBAMqgq83cwzSid2LSvzsn2rvkD8U0tWvqF6PuQAsKP3QHFqtBO+66pnIm\n"
  269. "CbiY2e6o01tmR47t557LuUCodEc8Blggxjg3ZEzvP42hsGB9LwQbcrU7grPRk0G0\n"
  270. "IltsOF9TZ+66gCeU7LxExLdAMqT2Tx6VT4IREPJMeNxSiceEjbABAgMBAAE=\n"
  271. "-----END RSA PUBLIC KEY-----\n"
  272. "router-signature\n"
  273. "-----BEGIN SIGNATURE-----\n"
  274. "GWpK2Ux/UwDaNUHwq+Xn7denyYFGS8SIWwqiMgHyUzc5wj1t2gWubJ/rMyGL59U3\n"
  275. "o6L/9qV34aa5UyNNBHXwYkxy7ixgPURaRYpAbkQKPU3ew8BgNXG/MNLYllIUkrbb\n"
  276. "h6G5u8RGbto+Nby/OjIh9TqdgK/B1sOdwAHI/IXiDoY=\n"
  277. "-----END SIGNATURE-----\n"
  278. ;
  279. int config_assign_default_dirservers(void) {
  280. if(router_set_routerlist_from_string(default_dirservers_string) < 0) {
  281. log_fn(LOG_WARN,"Bug: the default dirservers internal string is corrupt.");
  282. return -1;
  283. }
  284. return 0;
  285. }
  286. /* Call this function when they're using the default torrc but
  287. * we can't find it. For now, just hard-code what comes in the
  288. * default torrc.
  289. */
  290. static int config_assign_default(or_options_t *options) {
  291. /* set them up as a client only */
  292. options->SocksPort = 9050;
  293. /* plus give them a dirservers file */
  294. if(config_assign_default_dirservers() < 0)
  295. return -1;
  296. return 0;
  297. }
  298. /* prints the usage of tor. */
  299. static void print_usage(void) {
  300. printf("tor -f <torrc> [args]\n"
  301. "See man page for more options. This -h is probably obsolete.\n\n"
  302. "-b <bandwidth>\t\tbytes/second rate limiting\n"
  303. "-d <file>\t\tDebug file\n"
  304. // "-m <max>\t\tMax number of connections\n"
  305. "-l <level>\t\tLog level\n"
  306. "-r <file>\t\tList of known routers\n");
  307. printf("\nClient options:\n"
  308. "-e \"nick1 nick2 ...\"\t\tExit nodes\n"
  309. "-s <IP>\t\t\tPort to bind to for Socks\n"
  310. );
  311. printf("\nServer options:\n"
  312. "-n <nick>\t\tNickname of router\n"
  313. "-o <port>\t\tOR port to bind to\n"
  314. "-p <file>\t\tPID file\n"
  315. );
  316. }
  317. static int resolve_my_address(or_options_t *options) {
  318. struct in_addr in;
  319. struct hostent *rent;
  320. char localhostname[256];
  321. int explicit_ip=1;
  322. if(!options->Address) { /* then we need to guess our address */
  323. explicit_ip = 0; /* it's implicit */
  324. if(gethostname(localhostname,sizeof(localhostname)) < 0) {
  325. log_fn(LOG_WARN,"Error obtaining local hostname");
  326. return -1;
  327. }
  328. #if 0 /* don't worry about complaining, as long as it resolves */
  329. if(!strchr(localhostname,'.')) {
  330. log_fn(LOG_WARN,"fqdn '%s' has only one element. Misconfigured machine?",address);
  331. log_fn(LOG_WARN,"Try setting the Address line in your config file.");
  332. return -1;
  333. }
  334. #endif
  335. options->Address = tor_strdup(localhostname);
  336. log_fn(LOG_DEBUG,"Guessed local host name as '%s'",options->Address);
  337. }
  338. /* now we know options->Address is set. resolve it and keep only the IP */
  339. if(tor_inet_aton(options->Address, &in) == 0) {
  340. /* then we have to resolve it */
  341. explicit_ip = 0;
  342. rent = (struct hostent *)gethostbyname(options->Address);
  343. if (!rent) {
  344. log_fn(LOG_WARN,"Could not resolve Address %s. Failing.", options->Address);
  345. return -1;
  346. }
  347. assert(rent->h_length == 4);
  348. memcpy(&in.s_addr, rent->h_addr,rent->h_length);
  349. }
  350. if(!explicit_ip && is_internal_IP(htonl(in.s_addr))) {
  351. log_fn(LOG_WARN,"Address '%s' resolves to private IP '%s'. "
  352. "Please set the Address config option to be the IP you want to use.",
  353. options->Address, inet_ntoa(in));
  354. return -1;
  355. }
  356. tor_free(options->Address);
  357. options->Address = tor_strdup(inet_ntoa(in));
  358. log_fn(LOG_DEBUG,"Resolved Address to %s.", options->Address);
  359. return 0;
  360. }
  361. static void free_options(or_options_t *options) {
  362. tor_free(options->LogLevel);
  363. tor_free(options->LogFile);
  364. tor_free(options->DebugLogFile);
  365. tor_free(options->DataDirectory);
  366. tor_free(options->RouterFile);
  367. tor_free(options->Nickname);
  368. tor_free(options->Address);
  369. tor_free(options->PidFile);
  370. tor_free(options->ExitNodes);
  371. tor_free(options->EntryNodes);
  372. tor_free(options->ExcludeNodes);
  373. tor_free(options->RendNodes);
  374. tor_free(options->RendExcludeNodes);
  375. tor_free(options->ExitPolicy);
  376. tor_free(options->SocksBindAddress);
  377. tor_free(options->ORBindAddress);
  378. tor_free(options->DirBindAddress);
  379. tor_free(options->RecommendedVersions);
  380. tor_free(options->User);
  381. tor_free(options->Group);
  382. config_free_lines(options->RendConfigLines);
  383. }
  384. static void init_options(or_options_t *options) {
  385. /* give reasonable values for each option. Defaults to zero. */
  386. memset(options,0,sizeof(or_options_t));
  387. options->LogLevel = tor_strdup("notice");
  388. options->ExitNodes = tor_strdup("");
  389. options->EntryNodes = tor_strdup("");
  390. options->ExcludeNodes = tor_strdup("");
  391. options->RendNodes = tor_strdup("");
  392. options->RendExcludeNodes = tor_strdup("");
  393. options->ExitPolicy = tor_strdup("");
  394. options->SocksBindAddress = tor_strdup("127.0.0.1");
  395. options->ORBindAddress = tor_strdup("0.0.0.0");
  396. options->DirBindAddress = tor_strdup("0.0.0.0");
  397. options->RecommendedVersions = NULL;
  398. options->loglevel = LOG_INFO;
  399. options->PidFile = NULL; // tor_strdup("tor.pid");
  400. options->DataDirectory = NULL;
  401. options->PathlenCoinWeight = 0.3;
  402. options->MaxConn = 900;
  403. options->DirFetchPostPeriod = 600;
  404. options->KeepalivePeriod = 300;
  405. options->MaxOnionsPending = 100;
  406. options->NewCircuitPeriod = 30; /* twice a minute */
  407. options->BandwidthRate = 800000; /* at most 800kB/s total sustained incoming */
  408. options->BandwidthBurst = 10000000; /* max burst on the token bucket */
  409. options->NumCpus = 1;
  410. options->RendConfigLines = NULL;
  411. }
  412. /* return 0 if success, <0 if failure. */
  413. int getconfig(int argc, char **argv, or_options_t *options) {
  414. struct config_line_t *cl;
  415. FILE *cf;
  416. char *fname;
  417. int i;
  418. int result = 0;
  419. static int first_load = 1;
  420. static char **backup_argv;
  421. static int backup_argc;
  422. char *previous_pidfile = NULL;
  423. int previous_runasdaemon = 0;
  424. int previous_orport = -1;
  425. int using_default_torrc;
  426. if(first_load) { /* first time we're called. save commandline args */
  427. backup_argv = argv;
  428. backup_argc = argc;
  429. first_load = 0;
  430. } else { /* we're reloading. need to clean up old ones first. */
  431. argv = backup_argv;
  432. argc = backup_argc;
  433. /* record some previous values, so we can fail if they change */
  434. if(options->PidFile)
  435. previous_pidfile = tor_strdup(options->PidFile);
  436. previous_runasdaemon = options->RunAsDaemon;
  437. previous_orport = options->ORPort;
  438. free_options(options);
  439. }
  440. init_options(options);
  441. if(argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1],"--help"))) {
  442. print_usage();
  443. exit(0);
  444. }
  445. if(argc > 1 && (!strcmp(argv[1],"--version"))) {
  446. printf("Tor version %s.\n",VERSION);
  447. exit(0);
  448. }
  449. /* learn config file name, get config lines, assign them */
  450. i = 1;
  451. while(i < argc-1 && strcmp(argv[i],"-f")) {
  452. i++;
  453. }
  454. if(i < argc-1) { /* we found one */
  455. fname = argv[i+1];
  456. using_default_torrc = 0;
  457. } else { /* didn't find one, try CONFDIR */
  458. fname = CONFDIR "/torrc";
  459. using_default_torrc = 1;
  460. }
  461. log(LOG_DEBUG,"Opening config file '%s'",fname);
  462. cf = config_open(fname);
  463. if(!cf) {
  464. if(using_default_torrc == 1) {
  465. log(LOG_NOTICE, "Configuration file '%s' not present, using reasonable defaults.",fname);
  466. if(config_assign_default(options) < 0)
  467. return -1;
  468. } else {
  469. log(LOG_WARN, "Unable to open configuration file '%s'.",fname);
  470. return -1;
  471. }
  472. } else { /* it opened successfully. use it. */
  473. cl = config_get_lines(cf);
  474. if(!cl) return -1;
  475. if(config_assign(options,cl) < 0)
  476. return -1;
  477. config_free_lines(cl);
  478. config_close(cf);
  479. }
  480. /* go through command-line variables too */
  481. cl = config_get_commandlines(argc,argv);
  482. if(config_assign(options,cl) < 0)
  483. return -1;
  484. config_free_lines(cl);
  485. /* Validate options */
  486. /* first check if any of the previous options have changed but aren't allowed to */
  487. if(previous_pidfile && strcmp(previous_pidfile,options->PidFile)) {
  488. log_fn(LOG_WARN,"During reload, PidFile changed from %s to %s. Failing.",
  489. previous_pidfile, options->PidFile);
  490. return -1;
  491. }
  492. tor_free(previous_pidfile);
  493. if(previous_runasdaemon && !options->RunAsDaemon) {
  494. log_fn(LOG_WARN,"During reload, change from RunAsDaemon=1 to =0 not allowed. Failing.");
  495. return -1;
  496. }
  497. if(previous_orport == 0 && options->ORPort > 0) {
  498. log_fn(LOG_WARN,"During reload, change from ORPort=0 to >0 not allowed. Failing.");
  499. return -1;
  500. }
  501. if(options->LogLevel) {
  502. if(!strcmp(options->LogLevel,"err"))
  503. options->loglevel = LOG_ERR;
  504. else if(!strcmp(options->LogLevel,"warn"))
  505. options->loglevel = LOG_WARN;
  506. else if(!strcmp(options->LogLevel,"notice"))
  507. options->loglevel = LOG_NOTICE;
  508. else if(!strcmp(options->LogLevel,"info"))
  509. options->loglevel = LOG_INFO;
  510. else if(!strcmp(options->LogLevel,"debug"))
  511. options->loglevel = LOG_DEBUG;
  512. else {
  513. log(LOG_WARN,"LogLevel must be one of err|warn|notice|info|debug.");
  514. result = -1;
  515. }
  516. }
  517. if(options->ORPort < 0) {
  518. log(LOG_WARN,"ORPort option can't be negative.");
  519. result = -1;
  520. }
  521. if(options->ORPort && options->DataDirectory == NULL) {
  522. log(LOG_WARN,"DataDirectory option required if ORPort is set, but not found.");
  523. result = -1;
  524. }
  525. if (options->ORPort) {
  526. if (options->Nickname == NULL) {
  527. log_fn(LOG_WARN,"Nickname required if ORPort is set, but not found.");
  528. result = -1;
  529. } else {
  530. if (strspn(options->Nickname, LEGAL_NICKNAME_CHARACTERS) !=
  531. strlen(options->Nickname)) {
  532. log_fn(LOG_WARN, "Nickname '%s' contains illegal characters.", options->Nickname);
  533. result = -1;
  534. }
  535. if (strlen(options->Nickname) > MAX_NICKNAME_LEN) {
  536. log_fn(LOG_WARN, "Nickname '%s' has more than %d characters.",
  537. options->Nickname, MAX_NICKNAME_LEN);
  538. result = -1;
  539. }
  540. }
  541. }
  542. if(options->ORPort) { /* get an IP for ourselves */
  543. if(resolve_my_address(options) < 0)
  544. result = -1;
  545. }
  546. if(options->SocksPort < 0) {
  547. log(LOG_WARN,"SocksPort option can't be negative.");
  548. result = -1;
  549. }
  550. if(options->SocksPort == 0 && options->ORPort == 0) {
  551. log(LOG_WARN,"SocksPort and ORPort are both undefined? Quitting.");
  552. result = -1;
  553. }
  554. if(options->DirPort < 0) {
  555. log(LOG_WARN,"DirPort option can't be negative.");
  556. result = -1;
  557. }
  558. if(options->DirPort && options->RecommendedVersions == NULL) {
  559. log(LOG_WARN,"Directory servers must configure RecommendedVersions.");
  560. result = -1;
  561. }
  562. if(options->SocksPort > 1 &&
  563. (options->PathlenCoinWeight < 0.0 || options->PathlenCoinWeight >= 1.0)) {
  564. log(LOG_WARN,"PathlenCoinWeight option must be >=0.0 and <1.0.");
  565. result = -1;
  566. }
  567. if(options->MaxConn < 1) {
  568. log(LOG_WARN,"MaxConn option must be a non-zero positive integer.");
  569. result = -1;
  570. }
  571. if(options->MaxConn >= MAXCONNECTIONS) {
  572. log(LOG_WARN,"MaxConn option must be less than %d.", MAXCONNECTIONS);
  573. result = -1;
  574. }
  575. if(options->DirFetchPostPeriod < 1) {
  576. log(LOG_WARN,"DirFetchPostPeriod option must be positive.");
  577. result = -1;
  578. }
  579. if(options->KeepalivePeriod < 1) {
  580. log(LOG_WARN,"KeepalivePeriod option must be positive.");
  581. result = -1;
  582. }
  583. /* XXX look at the various nicknamelists and make sure they're
  584. * valid and don't have hostnames that are too long.
  585. */
  586. if (rend_config_services(options) < 0) {
  587. result = -1;
  588. }
  589. return result;
  590. }
  591. /*
  592. Local Variables:
  593. mode:c
  594. indent-tabs-mode:nil
  595. c-basic-offset:2
  596. End:
  597. */