config.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* Copyright 2001,2002 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 1024
  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 void 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_WARNING, "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. *(char **)arg = tor_strdup(c->value);
  114. break;
  115. case CONFIG_TYPE_DOUBLE:
  116. *(double *)arg = atof(c->value);
  117. break;
  118. }
  119. return 1;
  120. }
  121. static void config_assign(or_options_t *options, struct config_line *list) {
  122. /* iterate through list. for each item convert as appropriate and assign to 'options'. */
  123. while(list) {
  124. if(
  125. /* order matters here! abbreviated arguments use the first match. */
  126. /* string options */
  127. config_compare(list, "LogLevel", CONFIG_TYPE_STRING, &options->LogLevel) ||
  128. config_compare(list, "DataDirectory", CONFIG_TYPE_STRING, &options->DataDirectory) ||
  129. config_compare(list, "RouterFile", CONFIG_TYPE_STRING, &options->RouterFile) ||
  130. config_compare(list, "Nickname", CONFIG_TYPE_STRING, &options->Nickname) ||
  131. config_compare(list, "Address", CONFIG_TYPE_STRING, &options->Address) ||
  132. /* int options */
  133. config_compare(list, "MaxConn", CONFIG_TYPE_INT, &options->MaxConn) ||
  134. config_compare(list, "APPort", CONFIG_TYPE_INT, &options->APPort) ||
  135. config_compare(list, "ORPort", CONFIG_TYPE_INT, &options->ORPort) ||
  136. config_compare(list, "DirPort", CONFIG_TYPE_INT, &options->DirPort) ||
  137. config_compare(list, "DirFetchPostPeriod",CONFIG_TYPE_INT, &options->DirFetchPostPeriod) ||
  138. config_compare(list, "KeepalivePeriod", CONFIG_TYPE_INT, &options->KeepalivePeriod) ||
  139. config_compare(list, "MaxOnionsPending",CONFIG_TYPE_INT, &options->MaxOnionsPending) ||
  140. config_compare(list, "NewCircuitPeriod",CONFIG_TYPE_INT, &options->NewCircuitPeriod) ||
  141. config_compare(list, "TotalBandwidth", CONFIG_TYPE_INT, &options->TotalBandwidth) ||
  142. config_compare(list, "NumCpus", CONFIG_TYPE_INT, &options->NumCpus) ||
  143. config_compare(list, "OnionRouter", CONFIG_TYPE_BOOL, &options->OnionRouter) ||
  144. config_compare(list, "Daemon", CONFIG_TYPE_BOOL, &options->Daemon) ||
  145. config_compare(list, "TrafficShaping", CONFIG_TYPE_BOOL, &options->TrafficShaping) ||
  146. config_compare(list, "LinkPadding", CONFIG_TYPE_BOOL, &options->LinkPadding) ||
  147. config_compare(list, "IgnoreVersion", CONFIG_TYPE_BOOL, &options->IgnoreVersion) ||
  148. /* float options */
  149. config_compare(list, "CoinWeight", CONFIG_TYPE_DOUBLE, &options->CoinWeight)
  150. ) {
  151. /* then we're ok. it matched something. */
  152. } else {
  153. log_fn(LOG_WARNING,"Ignoring unknown keyword '%s'.",list->key);
  154. }
  155. list = list->next;
  156. }
  157. }
  158. /* return 0 if success, <0 if failure. */
  159. int getconfig(int argc, char **argv, or_options_t *options) {
  160. struct config_line *cl;
  161. FILE *cf;
  162. char *fname;
  163. int i;
  164. int result = 0;
  165. /* give reasonable values for each option. Defaults to zero. */
  166. memset(options,0,sizeof(or_options_t));
  167. options->LogLevel = "info";
  168. options->loglevel = LOG_DEBUG;
  169. options->DataDirectory = NULL;
  170. options->CoinWeight = 0.1;
  171. options->MaxConn = 900;
  172. options->DirFetchPostPeriod = 600;
  173. options->KeepalivePeriod = 300;
  174. options->MaxOnionsPending = 10;
  175. options->NewCircuitPeriod = 60; /* once a minute */
  176. options->TotalBandwidth = 800000; /* at most 800kB/s total sustained incoming */
  177. options->NumCpus = 1;
  178. /* learn config file name, get config lines, assign them */
  179. i = 1;
  180. while(i < argc-1 && strcmp(argv[i],"-f")) {
  181. i++;
  182. }
  183. if(i < argc-1) { /* we found one */
  184. fname = argv[i+1];
  185. } else { /* didn't find one, try /etc/torrc */
  186. fname = "/etc/torrc";
  187. }
  188. log(LOG_DEBUG,"Opening config file '%s'",fname);
  189. cf = config_open(fname);
  190. if(!cf) {
  191. log(LOG_WARNING, "Unable to open configuration file '%s'.",fname);
  192. return -1;
  193. }
  194. cl = config_get_lines(cf);
  195. if(!cl) return -1;
  196. config_assign(options,cl);
  197. config_free_lines(cl);
  198. config_close(cf);
  199. /* go through command-line variables too */
  200. cl = config_get_commandlines(argc,argv);
  201. config_assign(options,cl);
  202. config_free_lines(cl);
  203. /* Validate options */
  204. if(options->LogLevel) {
  205. if(!strcmp(options->LogLevel,"err"))
  206. options->loglevel = LOG_ERR;
  207. else if(!strncmp(options->LogLevel,"warn",4))
  208. options->loglevel = LOG_WARNING;
  209. else if(!strcmp(options->LogLevel,"info"))
  210. options->loglevel = LOG_INFO;
  211. else if(!strcmp(options->LogLevel,"debug"))
  212. options->loglevel = LOG_DEBUG;
  213. else {
  214. log(LOG_WARNING,"LogLevel must be one of err|warning|info|debug.");
  215. result = -1;
  216. }
  217. }
  218. if(options->RouterFile == NULL) {
  219. log(LOG_WARNING,"RouterFile option required, but not found.");
  220. result = -1;
  221. }
  222. if(options->ORPort < 0) {
  223. log(LOG_WARNING,"ORPort option can't be negative.");
  224. result = -1;
  225. }
  226. if(options->OnionRouter && options->ORPort == 0) {
  227. log(LOG_WARNING,"If OnionRouter is set, then ORPort must be positive.");
  228. result = -1;
  229. }
  230. if(options->OnionRouter && options->DataDirectory == NULL) {
  231. log(LOG_WARNING,"DataDirectory option required for OnionRouter, but not found.");
  232. result = -1;
  233. }
  234. if(options->OnionRouter && options->Nickname == NULL) {
  235. log_fn(LOG_WARNING,"Nickname required for OnionRouter, but not found.");
  236. result = -1;
  237. }
  238. if(options->APPort < 0) {
  239. log(LOG_WARNING,"APPort option can't be negative.");
  240. result = -1;
  241. }
  242. if(options->DirPort < 0) {
  243. log(LOG_WARNING,"DirPort option can't be negative.");
  244. result = -1;
  245. }
  246. if(options->APPort > 1 &&
  247. (options->CoinWeight < 0.0 || options->CoinWeight >= 1.0)) {
  248. log(LOG_WARNING,"CoinWeight option must be >=0.0 and <1.0.");
  249. result = -1;
  250. }
  251. if(options->MaxConn < 1) {
  252. log(LOG_WARNING,"MaxConn option must be a non-zero positive integer.");
  253. result = -1;
  254. }
  255. if(options->MaxConn >= MAXCONNECTIONS) {
  256. log(LOG_WARNING,"MaxConn option must be less than %d.", MAXCONNECTIONS);
  257. result = -1;
  258. }
  259. if(options->DirFetchPostPeriod < 1) {
  260. log(LOG_WARNING,"DirFetchPostPeriod option must be positive.");
  261. result = -1;
  262. }
  263. if(options->KeepalivePeriod < 1) {
  264. log(LOG_WARNING,"KeepalivePeriod option must be positive.");
  265. result = -1;
  266. }
  267. return result;
  268. }
  269. /*
  270. Local Variables:
  271. mode:c
  272. indent-tabs-mode:nil
  273. c-basic-offset:2
  274. End:
  275. */