config.c 20 KB

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