conf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. conf.c -- configuration code
  3. Copyright (C) 1998 Robert van der Meulen
  4. 1998-2005 Ivo Timmermans
  5. 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
  6. 2010-2011 Julien Muchembled <jm@jmuchemb.eu>
  7. 2000 Cris van Pelt
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License along
  17. with this program; if not, write to the Free Software Foundation, Inc.,
  18. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "system.h"
  21. #include "avl_tree.h"
  22. #include "connection.h"
  23. #include "conf.h"
  24. #include "list.h"
  25. #include "logger.h"
  26. #include "netutl.h" /* for str2address */
  27. #include "protocol.h"
  28. #include "utils.h" /* for cp */
  29. #include "xalloc.h"
  30. avl_tree_t *config_tree;
  31. int pinginterval = 0; /* seconds between pings */
  32. int pingtimeout = 0; /* seconds to wait for response */
  33. char *confbase = NULL; /* directory in which all config files are */
  34. char *netname = NULL; /* name of the vpn network */
  35. list_t *cmdline_conf = NULL; /* global/host configuration values given at the command line */
  36. static int config_compare(const config_t *a, const config_t *b) {
  37. int result;
  38. result = strcasecmp(a->variable, b->variable);
  39. if(result)
  40. return result;
  41. /* give priority to command line options */
  42. result = !b->file - !a->file;
  43. if (result)
  44. return result;
  45. result = a->line - b->line;
  46. if(result)
  47. return result;
  48. else
  49. return a->file ? strcmp(a->file, b->file) : 0;
  50. }
  51. void init_configuration(avl_tree_t ** config_tree) {
  52. *config_tree = avl_alloc_tree((avl_compare_t) config_compare, (avl_action_t) free_config);
  53. }
  54. void exit_configuration(avl_tree_t ** config_tree) {
  55. avl_delete_tree(*config_tree);
  56. *config_tree = NULL;
  57. }
  58. config_t *new_config(void) {
  59. return xmalloc_and_zero(sizeof(config_t));
  60. }
  61. void free_config(config_t *cfg) {
  62. if(cfg->variable)
  63. free(cfg->variable);
  64. if(cfg->value)
  65. free(cfg->value);
  66. if(cfg->file)
  67. free(cfg->file);
  68. free(cfg);
  69. }
  70. void config_add(avl_tree_t *config_tree, config_t *cfg) {
  71. avl_insert(config_tree, cfg);
  72. }
  73. config_t *lookup_config(const avl_tree_t *config_tree, char *variable) {
  74. config_t cfg, *found;
  75. cfg.variable = variable;
  76. cfg.file = NULL;
  77. cfg.line = 0;
  78. found = avl_search_closest_greater(config_tree, &cfg);
  79. if(!found)
  80. return NULL;
  81. if(strcasecmp(found->variable, variable))
  82. return NULL;
  83. return found;
  84. }
  85. config_t *lookup_config_next(const avl_tree_t *config_tree, const config_t *cfg) {
  86. avl_node_t *node;
  87. config_t *found;
  88. node = avl_search_node(config_tree, cfg);
  89. if(node) {
  90. if(node->next) {
  91. found = node->next->data;
  92. if(!strcasecmp(found->variable, cfg->variable))
  93. return found;
  94. }
  95. }
  96. return NULL;
  97. }
  98. bool get_config_bool(const config_t *cfg, bool *result) {
  99. if(!cfg)
  100. return false;
  101. if(!strcasecmp(cfg->value, "yes")) {
  102. *result = true;
  103. return true;
  104. } else if(!strcasecmp(cfg->value, "no")) {
  105. *result = false;
  106. return true;
  107. }
  108. logger(LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
  109. cfg->variable, cfg->file, cfg->line);
  110. return false;
  111. }
  112. bool get_config_int(const config_t *cfg, int *result) {
  113. if(!cfg)
  114. return false;
  115. if(sscanf(cfg->value, "%d", result) == 1)
  116. return true;
  117. logger(LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
  118. cfg->variable, cfg->file, cfg->line);
  119. return false;
  120. }
  121. bool get_config_string(const config_t *cfg, char **result) {
  122. if(!cfg)
  123. return false;
  124. *result = xstrdup(cfg->value);
  125. return true;
  126. }
  127. bool get_config_address(const config_t *cfg, struct addrinfo **result) {
  128. struct addrinfo *ai;
  129. if(!cfg)
  130. return false;
  131. ai = str2addrinfo(cfg->value, NULL, 0);
  132. if(ai) {
  133. *result = ai;
  134. return true;
  135. }
  136. logger(LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
  137. cfg->variable, cfg->file, cfg->line);
  138. return false;
  139. }
  140. bool get_config_subnet(const config_t *cfg, subnet_t ** result) {
  141. subnet_t subnet = {NULL};
  142. if(!cfg)
  143. return false;
  144. if(!str2net(&subnet, cfg->value)) {
  145. logger(LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
  146. cfg->variable, cfg->file, cfg->line);
  147. return false;
  148. }
  149. /* Teach newbies what subnets are... */
  150. if(((subnet.type == SUBNET_IPV4)
  151. && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t)))
  152. || ((subnet.type == SUBNET_IPV6)
  153. && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t)))) {
  154. logger(LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
  155. cfg->variable, cfg->file, cfg->line);
  156. return false;
  157. }
  158. *(*result = new_subnet()) = subnet;
  159. return true;
  160. }
  161. /*
  162. Read exactly one line and strip the trailing newline if any.
  163. */
  164. static char *readline(FILE * fp, char *buf, size_t buflen) {
  165. char *newline = NULL;
  166. char *p;
  167. if(feof(fp))
  168. return NULL;
  169. p = fgets(buf, buflen, fp);
  170. if(!p)
  171. return NULL;
  172. newline = strchr(p, '\n');
  173. if(!newline)
  174. return buf;
  175. *newline = '\0'; /* kill newline */
  176. if(newline > p && newline[-1] == '\r') /* and carriage return if necessary */
  177. newline[-1] = '\0';
  178. return buf;
  179. }
  180. config_t *parse_config_line(char *line, const char *fname, int lineno) {
  181. config_t *cfg;
  182. int len;
  183. char *variable, *value, *eol;
  184. variable = value = line;
  185. eol = line + strlen(line);
  186. while(strchr("\t ", *--eol))
  187. *eol = '\0';
  188. len = strcspn(value, "\t =");
  189. value += len;
  190. value += strspn(value, "\t ");
  191. if(*value == '=') {
  192. value++;
  193. value += strspn(value, "\t ");
  194. }
  195. variable[len] = '\0';
  196. if(!*value) {
  197. const char err[] = "No value for variable";
  198. if (fname)
  199. logger(LOG_ERR, "%s `%s' on line %d while reading config file %s",
  200. err, variable, lineno, fname);
  201. else
  202. logger(LOG_ERR, "%s `%s' in command line option %d",
  203. err, variable, lineno);
  204. return NULL;
  205. }
  206. cfg = new_config();
  207. cfg->variable = xstrdup(variable);
  208. cfg->value = xstrdup(value);
  209. cfg->file = fname ? xstrdup(fname) : NULL;
  210. cfg->line = lineno;
  211. return cfg;
  212. }
  213. /*
  214. Parse a configuration file and put the results in the configuration tree
  215. starting at *base.
  216. */
  217. bool read_config_file(avl_tree_t *config_tree, const char *fname) {
  218. FILE *fp;
  219. char buffer[MAX_STRING_SIZE];
  220. char *line;
  221. int lineno = 0;
  222. bool ignore = false;
  223. config_t *cfg;
  224. bool result = false;
  225. fp = fopen(fname, "r");
  226. if(!fp) {
  227. logger(LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
  228. return false;
  229. }
  230. for(;;) {
  231. line = readline(fp, buffer, sizeof buffer);
  232. if(!line) {
  233. if(feof(fp))
  234. result = true;
  235. break;
  236. }
  237. lineno++;
  238. if(!*line || *line == '#')
  239. continue;
  240. if(ignore) {
  241. if(!strncmp(line, "-----END", 8))
  242. ignore = false;
  243. continue;
  244. }
  245. if(!strncmp(line, "-----BEGIN", 10)) {
  246. ignore = true;
  247. continue;
  248. }
  249. cfg = parse_config_line(line, fname, lineno);
  250. if (!cfg)
  251. break;
  252. config_add(config_tree, cfg);
  253. }
  254. fclose(fp);
  255. return result;
  256. }
  257. void read_config_options(avl_tree_t *config_tree, const char *prefix) {
  258. list_node_t *node, *next;
  259. size_t prefix_len = prefix ? strlen(prefix) : 0;
  260. for(node = cmdline_conf->tail; node; node = next) {
  261. config_t *orig_cfg, *cfg = (config_t *)node->data;
  262. next = node->prev;
  263. if(!prefix) {
  264. if(strchr(cfg->variable, '.'))
  265. continue;
  266. node->data = NULL;
  267. list_unlink_node(cmdline_conf, node);
  268. } else {
  269. if(strncmp(prefix, cfg->variable, prefix_len) ||
  270. cfg->variable[prefix_len] != '.')
  271. continue;
  272. /* Because host configuration is parsed again when
  273. reconnecting, nodes must not be freed when a prefix
  274. is given. */
  275. orig_cfg = cfg;
  276. cfg = new_config();
  277. cfg->variable = xstrdup(orig_cfg->variable + prefix_len + 1);
  278. cfg->value = xstrdup(orig_cfg->value);
  279. cfg->file = NULL;
  280. cfg->line = orig_cfg->line;
  281. }
  282. config_add(config_tree, cfg);
  283. }
  284. }
  285. bool read_server_config(void) {
  286. char *fname;
  287. bool x;
  288. read_config_options(config_tree, NULL);
  289. xasprintf(&fname, "%s/tinc.conf", confbase);
  290. x = read_config_file(config_tree, fname);
  291. if(!x) { /* System error: complain */
  292. logger(LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
  293. }
  294. free(fname);
  295. return x;
  296. }
  297. bool read_connection_config(connection_t *c) {
  298. char *fname;
  299. bool x;
  300. read_config_options(c->config_tree, c->name);
  301. xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
  302. x = read_config_file(c->config_tree, fname);
  303. free(fname);
  304. return x;
  305. }
  306. static void disable_old_keys(const char *filename) {
  307. char tmpfile[PATH_MAX] = "";
  308. char buf[1024];
  309. bool disabled = false;
  310. FILE *r, *w;
  311. r = fopen(filename, "r");
  312. if(!r)
  313. return;
  314. snprintf(tmpfile, sizeof tmpfile, "%s.tmp", filename);
  315. w = fopen(tmpfile, "w");
  316. while(fgets(buf, sizeof buf, r)) {
  317. if(!strncmp(buf, "-----BEGIN RSA", 14)) {
  318. buf[11] = 'O';
  319. buf[12] = 'L';
  320. buf[13] = 'D';
  321. disabled = true;
  322. }
  323. else if(!strncmp(buf, "-----END RSA", 12)) {
  324. buf[ 9] = 'O';
  325. buf[10] = 'L';
  326. buf[11] = 'D';
  327. disabled = true;
  328. }
  329. if(w && fputs(buf, w) < 0) {
  330. disabled = false;
  331. break;
  332. }
  333. }
  334. if(w)
  335. fclose(w);
  336. fclose(r);
  337. if(!w && disabled) {
  338. fprintf(stderr, "Warning: old key(s) found, remove them by hand!\n");
  339. return;
  340. }
  341. if(disabled) {
  342. #ifdef HAVE_MINGW
  343. // We cannot atomically replace files on Windows.
  344. char bakfile[PATH_MAX] = "";
  345. snprintf(bakfile, sizeof bakfile, "%s.bak", filename);
  346. if(rename(filename, bakfile) || rename(tmpfile, filename)) {
  347. rename(bakfile, filename);
  348. #else
  349. if(rename(tmpfile, filename)) {
  350. #endif
  351. fprintf(stderr, "Warning: old key(s) found, remove them by hand!\n");
  352. } else {
  353. #ifdef HAVE_MINGW
  354. unlink(bakfile);
  355. #endif
  356. fprintf(stderr, "Warning: old key(s) found and disabled.\n");
  357. }
  358. }
  359. unlink(tmpfile);
  360. }
  361. FILE *ask_and_open(const char *filename, const char *what) {
  362. FILE *r;
  363. char *directory;
  364. char line[PATH_MAX];
  365. const char *fn;
  366. /* Check stdin and stdout */
  367. if(!isatty(0) || !isatty(1)) {
  368. /* Argh, they are running us from a script or something. Write
  369. the files to the current directory and let them burn in hell
  370. for ever. */
  371. fn = filename;
  372. } else {
  373. /* Ask for a file and/or directory name. */
  374. fprintf(stdout, "Please enter a file to save %s to [%s]: ",
  375. what, filename);
  376. fflush(stdout);
  377. fn = readline(stdin, line, sizeof line);
  378. if(!fn) {
  379. fprintf(stderr, "Error while reading stdin: %s\n",
  380. strerror(errno));
  381. return NULL;
  382. }
  383. if(!strlen(fn))
  384. /* User just pressed enter. */
  385. fn = filename;
  386. }
  387. #ifdef HAVE_MINGW
  388. if(fn[0] != '\\' && fn[0] != '/' && !strchr(fn, ':')) {
  389. #else
  390. if(fn[0] != '/') {
  391. #endif
  392. /* The directory is a relative path or a filename. */
  393. char *p;
  394. directory = get_current_dir_name();
  395. xasprintf(&p, "%s/%s", directory, fn);
  396. free(directory);
  397. fn = p;
  398. }
  399. umask(0077); /* Disallow everything for group and other */
  400. disable_old_keys(fn);
  401. /* Open it first to keep the inode busy */
  402. r = fopen(fn, "a");
  403. if(!r) {
  404. fprintf(stderr, "Error opening file `%s': %s\n",
  405. fn, strerror(errno));
  406. return NULL;
  407. }
  408. return r;
  409. }