conf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. conf.c -- configuration code
  3. Copyright (C) 1998 Robert van der Meulen
  4. 1998-2005 Ivo Timmermans
  5. 2000-2014 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. }
  42. /* give priority to command line options */
  43. result = !b->file - !a->file;
  44. if(result) {
  45. return result;
  46. }
  47. result = a->line - b->line;
  48. if(result) {
  49. return result;
  50. } else {
  51. return a->file ? strcmp(a->file, b->file) : 0;
  52. }
  53. }
  54. void init_configuration(avl_tree_t **config_tree) {
  55. *config_tree = avl_alloc_tree((avl_compare_t) config_compare, (avl_action_t) free_config);
  56. }
  57. void exit_configuration(avl_tree_t **config_tree) {
  58. avl_delete_tree(*config_tree);
  59. *config_tree = NULL;
  60. }
  61. config_t *new_config(void) {
  62. return xmalloc_and_zero(sizeof(config_t));
  63. }
  64. void free_config(config_t *cfg) {
  65. if(cfg->variable) {
  66. free(cfg->variable);
  67. }
  68. if(cfg->value) {
  69. free(cfg->value);
  70. }
  71. if(cfg->file) {
  72. free(cfg->file);
  73. }
  74. free(cfg);
  75. }
  76. void config_add(avl_tree_t *config_tree, config_t *cfg) {
  77. avl_insert(config_tree, cfg);
  78. }
  79. config_t *lookup_config(const avl_tree_t *config_tree, char *variable) {
  80. config_t cfg, *found;
  81. cfg.variable = variable;
  82. cfg.file = NULL;
  83. cfg.line = 0;
  84. found = avl_search_closest_greater(config_tree, &cfg);
  85. if(!found) {
  86. return NULL;
  87. }
  88. if(strcasecmp(found->variable, variable)) {
  89. return NULL;
  90. }
  91. return found;
  92. }
  93. config_t *lookup_config_next(const avl_tree_t *config_tree, const config_t *cfg) {
  94. avl_node_t *node;
  95. config_t *found;
  96. node = avl_search_node(config_tree, cfg);
  97. if(node) {
  98. if(node->next) {
  99. found = node->next->data;
  100. if(!strcasecmp(found->variable, cfg->variable)) {
  101. return found;
  102. }
  103. }
  104. }
  105. return NULL;
  106. }
  107. bool get_config_bool(const config_t *cfg, bool *result) {
  108. if(!cfg) {
  109. return false;
  110. }
  111. if(!strcasecmp(cfg->value, "yes")) {
  112. *result = true;
  113. return true;
  114. } else if(!strcasecmp(cfg->value, "no")) {
  115. *result = false;
  116. return true;
  117. }
  118. logger(LOG_ERR, "\"yes\" or \"no\" expected for configuration variable %s in %s line %d",
  119. cfg->variable, cfg->file, cfg->line);
  120. return false;
  121. }
  122. bool get_config_int(const config_t *cfg, int *result) {
  123. if(!cfg) {
  124. return false;
  125. }
  126. if(sscanf(cfg->value, "%d", result) == 1) {
  127. return true;
  128. }
  129. logger(LOG_ERR, "Integer expected for configuration variable %s in %s line %d",
  130. cfg->variable, cfg->file, cfg->line);
  131. return false;
  132. }
  133. bool get_config_string(const config_t *cfg, char **result) {
  134. if(!cfg) {
  135. return false;
  136. }
  137. *result = xstrdup(cfg->value);
  138. return true;
  139. }
  140. bool get_config_address(const config_t *cfg, struct addrinfo **result) {
  141. struct addrinfo *ai;
  142. if(!cfg) {
  143. return false;
  144. }
  145. ai = str2addrinfo(cfg->value, NULL, 0);
  146. if(ai) {
  147. *result = ai;
  148. return true;
  149. }
  150. logger(LOG_ERR, "Hostname or IP address expected for configuration variable %s in %s line %d",
  151. cfg->variable, cfg->file, cfg->line);
  152. return false;
  153. }
  154. bool get_config_subnet(const config_t *cfg, subnet_t **result) {
  155. subnet_t subnet = {};
  156. if(!cfg) {
  157. return false;
  158. }
  159. if(!str2net(&subnet, cfg->value)) {
  160. logger(LOG_ERR, "Subnet expected for configuration variable %s in %s line %d",
  161. cfg->variable, cfg->file, cfg->line);
  162. return false;
  163. }
  164. /* Teach newbies what subnets are... */
  165. if(((subnet.type == SUBNET_IPV4)
  166. && !maskcheck(&subnet.net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t)))
  167. || ((subnet.type == SUBNET_IPV6)
  168. && !maskcheck(&subnet.net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t)))) {
  169. logger(LOG_ERR, "Network address and prefix length do not match for configuration variable %s in %s line %d",
  170. cfg->variable, cfg->file, cfg->line);
  171. return false;
  172. }
  173. *(*result = new_subnet()) = subnet;
  174. return true;
  175. }
  176. /*
  177. Read exactly one line and strip the trailing newline if any.
  178. */
  179. static char *readline(FILE *fp, char *buf, size_t buflen) {
  180. char *newline = NULL;
  181. char *p;
  182. if(feof(fp)) {
  183. return NULL;
  184. }
  185. p = fgets(buf, buflen, fp);
  186. if(!p) {
  187. return NULL;
  188. }
  189. newline = strchr(p, '\n');
  190. if(!newline) {
  191. return buf;
  192. }
  193. *newline = '\0'; /* kill newline */
  194. if(newline > p && newline[-1] == '\r') { /* and carriage return if necessary */
  195. newline[-1] = '\0';
  196. }
  197. return buf;
  198. }
  199. config_t *parse_config_line(char *line, const char *fname, int lineno) {
  200. config_t *cfg;
  201. int len;
  202. char *variable, *value, *eol;
  203. variable = value = line;
  204. eol = line + strlen(line);
  205. while(strchr("\t ", *--eol)) {
  206. *eol = '\0';
  207. }
  208. len = strcspn(value, "\t =");
  209. value += len;
  210. value += strspn(value, "\t ");
  211. if(*value == '=') {
  212. value++;
  213. value += strspn(value, "\t ");
  214. }
  215. variable[len] = '\0';
  216. if(!*value) {
  217. const char err[] = "No value for variable";
  218. if(fname)
  219. logger(LOG_ERR, "%s `%s' on line %d while reading config file %s",
  220. err, variable, lineno, fname);
  221. else
  222. logger(LOG_ERR, "%s `%s' in command line option %d",
  223. err, variable, lineno);
  224. return NULL;
  225. }
  226. cfg = new_config();
  227. cfg->variable = xstrdup(variable);
  228. cfg->value = xstrdup(value);
  229. cfg->file = fname ? xstrdup(fname) : NULL;
  230. cfg->line = lineno;
  231. return cfg;
  232. }
  233. /*
  234. Parse a configuration file and put the results in the configuration tree
  235. starting at *base.
  236. */
  237. bool read_config_file(avl_tree_t *config_tree, const char *fname) {
  238. FILE *fp;
  239. char buffer[MAX_STRING_SIZE];
  240. char *line;
  241. int lineno = 0;
  242. bool ignore = false;
  243. config_t *cfg;
  244. bool result = false;
  245. fp = fopen(fname, "r");
  246. if(!fp) {
  247. logger(LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
  248. return false;
  249. }
  250. for(;;) {
  251. line = readline(fp, buffer, sizeof(buffer));
  252. if(!line) {
  253. if(feof(fp)) {
  254. result = true;
  255. }
  256. break;
  257. }
  258. lineno++;
  259. if(!*line || *line == '#') {
  260. continue;
  261. }
  262. if(ignore) {
  263. if(!strncmp(line, "-----END", 8)) {
  264. ignore = false;
  265. }
  266. continue;
  267. }
  268. if(!strncmp(line, "-----BEGIN", 10)) {
  269. ignore = true;
  270. continue;
  271. }
  272. cfg = parse_config_line(line, fname, lineno);
  273. if(!cfg) {
  274. break;
  275. }
  276. config_add(config_tree, cfg);
  277. }
  278. fclose(fp);
  279. return result;
  280. }
  281. void read_config_options(avl_tree_t *config_tree, const char *prefix) {
  282. size_t prefix_len = prefix ? strlen(prefix) : 0;
  283. for(const list_node_t *node = cmdline_conf->tail; node; node = node->prev) {
  284. const config_t *cfg = node->data;
  285. if(!prefix) {
  286. if(strchr(cfg->variable, '.')) {
  287. continue;
  288. }
  289. } else {
  290. if(strncmp(prefix, cfg->variable, prefix_len) ||
  291. cfg->variable[prefix_len] != '.') {
  292. continue;
  293. }
  294. }
  295. config_t *new = new_config();
  296. if(prefix) {
  297. new->variable = xstrdup(cfg->variable + prefix_len + 1);
  298. } else {
  299. new->variable = xstrdup(cfg->variable);
  300. }
  301. new->value = xstrdup(cfg->value);
  302. new->file = NULL;
  303. new->line = cfg->line;
  304. config_add(config_tree, new);
  305. }
  306. }
  307. bool read_server_config(void) {
  308. char fname[PATH_MAX];
  309. bool x;
  310. read_config_options(config_tree, NULL);
  311. snprintf(fname, sizeof(fname), "%s/tinc.conf", confbase);
  312. errno = 0;
  313. x = read_config_file(config_tree, fname);
  314. // We will try to read the conf files in the "conf.d" dir
  315. if(x) {
  316. char dname[PATH_MAX];
  317. snprintf(dname, sizeof(dname), "%s/conf.d", confbase);
  318. DIR *dir = opendir(dname);
  319. // If we can find this dir
  320. if(dir) {
  321. struct dirent *ep;
  322. // We list all the files in it
  323. while(x && (ep = readdir(dir))) {
  324. size_t l = strlen(ep->d_name);
  325. // And we try to read the ones that end with ".conf"
  326. if(l > 5 && !strcmp(".conf", & ep->d_name[ l - 5 ])) {
  327. snprintf(fname, sizeof(fname), "%s/%s", dname, ep->d_name);
  328. x = read_config_file(config_tree, fname);
  329. }
  330. }
  331. closedir(dir);
  332. }
  333. }
  334. if(!x && errno) {
  335. logger(LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
  336. }
  337. return x;
  338. }
  339. bool read_connection_config(connection_t *c) {
  340. char fname[PATH_MAX];
  341. bool x;
  342. read_config_options(c->config_tree, c->name);
  343. snprintf(fname, sizeof(fname), "%s/hosts/%s", confbase, c->name);
  344. x = read_config_file(c->config_tree, fname);
  345. return x;
  346. }
  347. static void disable_old_keys(const char *filename) {
  348. char tmpfile[PATH_MAX] = "";
  349. char buf[1024];
  350. bool disabled = false;
  351. FILE *r, *w;
  352. r = fopen(filename, "r");
  353. if(!r) {
  354. return;
  355. }
  356. snprintf(tmpfile, sizeof(tmpfile), "%s.tmp", filename);
  357. w = fopen(tmpfile, "w");
  358. while(fgets(buf, sizeof(buf), r)) {
  359. if(!strncmp(buf, "-----BEGIN RSA", 14)) {
  360. buf[11] = 'O';
  361. buf[12] = 'L';
  362. buf[13] = 'D';
  363. disabled = true;
  364. } else if(!strncmp(buf, "-----END RSA", 12)) {
  365. buf[ 9] = 'O';
  366. buf[10] = 'L';
  367. buf[11] = 'D';
  368. disabled = true;
  369. }
  370. if(w && fputs(buf, w) < 0) {
  371. disabled = false;
  372. break;
  373. }
  374. }
  375. if(w) {
  376. fclose(w);
  377. }
  378. fclose(r);
  379. if(!w && disabled) {
  380. fprintf(stderr, "Warning: old key(s) found, remove them by hand!\n");
  381. return;
  382. }
  383. if(disabled) {
  384. #ifdef HAVE_MINGW
  385. // We cannot atomically replace files on Windows.
  386. char bakfile[PATH_MAX] = "";
  387. snprintf(bakfile, sizeof(bakfile), "%s.bak", filename);
  388. if(rename(filename, bakfile) || rename(tmpfile, filename)) {
  389. rename(bakfile, filename);
  390. #else
  391. if(rename(tmpfile, filename)) {
  392. #endif
  393. fprintf(stderr, "Warning: old key(s) found, remove them by hand!\n");
  394. } else {
  395. #ifdef HAVE_MINGW
  396. unlink(bakfile);
  397. #endif
  398. fprintf(stderr, "Warning: old key(s) found and disabled.\n");
  399. }
  400. }
  401. unlink(tmpfile);
  402. }
  403. FILE *ask_and_open(const char *filename, const char *what) {
  404. FILE *r;
  405. char directory[PATH_MAX];
  406. char line[PATH_MAX];
  407. char abspath[PATH_MAX];
  408. const char *fn;
  409. /* Check stdin and stdout */
  410. if(!isatty(0) || !isatty(1)) {
  411. /* Argh, they are running us from a script or something. Write
  412. the files to the current directory and let them burn in hell
  413. for ever. */
  414. fn = filename;
  415. } else {
  416. /* Ask for a file and/or directory name. */
  417. fprintf(stdout, "Please enter a file to save %s to [%s]: ",
  418. what, filename);
  419. fflush(stdout);
  420. fn = readline(stdin, line, sizeof(line));
  421. if(!fn) {
  422. fprintf(stderr, "Error while reading stdin: %s\n",
  423. strerror(errno));
  424. return NULL;
  425. }
  426. if(!strlen(fn))
  427. /* User just pressed enter. */
  428. {
  429. fn = filename;
  430. }
  431. }
  432. #ifdef HAVE_MINGW
  433. if(fn[0] != '\\' && fn[0] != '/' && !strchr(fn, ':')) {
  434. #else
  435. if(fn[0] != '/') {
  436. #endif
  437. /* The directory is a relative path or a filename. */
  438. getcwd(directory, sizeof(directory));
  439. snprintf(abspath, sizeof(abspath), "%s/%s", directory, fn);
  440. fn = abspath;
  441. }
  442. umask(0077); /* Disallow everything for group and other */
  443. disable_old_keys(fn);
  444. /* Open it first to keep the inode busy */
  445. r = fopen(fn, "a");
  446. if(!r) {
  447. fprintf(stderr, "Error opening file `%s': %s\n",
  448. fn, strerror(errno));
  449. return NULL;
  450. }
  451. return r;
  452. }