+bool read_config_file(avl_tree_t *config_tree, const char *fname) {
+ FILE *fp;
+ char buffer[MAX_STRING_SIZE];
+ char *line;
+ int lineno = 0;
+ bool ignore = false;
+ config_t *cfg;
+ bool result = false;
+
+ fp = fopen(fname, "r");
+
+ if(!fp) {
+ logger(LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
+ return false;
+ }
+
+ for(;;) {
+ line = readline(fp, buffer, sizeof buffer);
+
+ if(!line) {
+ if(feof(fp))
+ result = true;
+ break;
+ }
+
+ lineno++;
+
+ if(!*line || *line == '#')
+ continue;
+
+ if(ignore) {
+ if(!strncmp(line, "-----END", 8))
+ ignore = false;
+ continue;
+ }
+
+ if(!strncmp(line, "-----BEGIN", 10)) {
+ ignore = true;
+ continue;
+ }
+
+ cfg = parse_config_line(line, fname, lineno);
+ if (!cfg)
+ break;
+ config_add(config_tree, cfg);
+ }
+
+ fclose(fp);
+
+ return result;
+}
+
+void read_config_options(avl_tree_t *config_tree, const char *prefix) {
+ list_node_t *node, *next;
+ size_t prefix_len = prefix ? strlen(prefix) : 0;
+
+ for(node = cmdline_conf->tail; node; node = next) {
+ config_t *orig_cfg, *cfg = (config_t *)node->data;
+ next = node->prev;
+
+ if(!prefix) {
+ if(strchr(cfg->variable, '.'))
+ continue;
+ node->data = NULL;
+ list_unlink_node(cmdline_conf, node);
+ } else {
+ if(strncmp(prefix, cfg->variable, prefix_len) ||
+ cfg->variable[prefix_len] != '.')
+ continue;
+ /* Because host configuration is parsed again when
+ reconnecting, nodes must not be freed when a prefix
+ is given. */
+ orig_cfg = cfg;
+ cfg = new_config();
+ cfg->variable = xstrdup(orig_cfg->variable + prefix_len + 1);
+ cfg->value = xstrdup(orig_cfg->value);
+ cfg->file = NULL;
+ cfg->line = orig_cfg->line;
+ }
+ config_add(config_tree, cfg);
+ }
+}
+
+bool read_server_config(void) {
+ char *fname;
+ bool x;
+
+ read_config_options(config_tree, NULL);
+
+ xasprintf(&fname, "%s/tinc.conf", confbase);
+ x = read_config_file(config_tree, fname);
+
+ if(!x) { /* System error: complain */
+ logger(LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
+ }
+
+ free(fname);
+
+ return x;
+}
+
+bool read_connection_config(connection_t *c) {
+ char *fname;
+ bool x;
+
+ read_config_options(c->config_tree, c->name);
+
+ xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
+ x = read_config_file(c->config_tree, fname);
+ free(fname);
+
+ return x;
+}
+
+FILE *ask_and_open(const char *filename, const char *what) {
+ FILE *r;
+ char *directory;
+ char line[PATH_MAX];
+ const char *fn;
+
+ /* Check stdin and stdout */
+ if(!isatty(0) || !isatty(1)) {
+ /* Argh, they are running us from a script or something. Write
+ the files to the current directory and let them burn in hell
+ for ever. */
+ fn = filename;
+ } else {
+ /* Ask for a file and/or directory name. */
+ fprintf(stdout, "Please enter a file to save %s to [%s]: ",
+ what, filename);
+ fflush(stdout);
+
+ fn = readline(stdin, line, sizeof line);
+
+ if(!fn) {
+ fprintf(stderr, "Error while reading stdin: %s\n",
+ strerror(errno));
+ return NULL;
+ }
+
+ if(!strlen(fn))
+ /* User just pressed enter. */
+ fn = filename;
+ }
+
+#ifdef HAVE_MINGW
+ if(fn[0] != '\\' && fn[0] != '/' && !strchr(fn, ':')) {
+#else
+ if(fn[0] != '/') {
+#endif
+ /* The directory is a relative path or a filename. */
+ char *p;
+
+ directory = get_current_dir_name();
+ xasprintf(&p, "%s/%s", directory, fn);
+ free(directory);
+ fn = p;
+ }
+
+ umask(0077); /* Disallow everything for group and other */
+
+ /* Open it first to keep the inode busy */
+
+ r = fopen(fn, "r+") ?: fopen(fn, "w+");
+
+ if(!r) {
+ fprintf(stderr, "Error opening file `%s': %s\n",
+ fn, strerror(errno));
+ return NULL;
+ }
+
+ return r;
+}
+
+bool disable_old_keys(FILE *f) {
+ char buf[100];
+ long pos;
+ bool disabled = false;
+
+ rewind(f);
+ pos = ftell(f);
+
+ if(pos < 0)
+ return false;
+
+ while(fgets(buf, sizeof buf, f)) {
+ if(!strncmp(buf, "-----BEGIN RSA", 14)) {
+ buf[11] = 'O';
+ buf[12] = 'L';
+ buf[13] = 'D';
+ if(fseek(f, pos, SEEK_SET))
+ break;
+ if(fputs(buf, f) <= 0)
+ break;
+ disabled = true;
+ }
+ else if(!strncmp(buf, "-----END RSA", 12)) {
+ buf[ 9] = 'O';
+ buf[10] = 'L';
+ buf[11] = 'D';
+ if(fseek(f, pos, SEEK_SET))
+ break;
+ if(fputs(buf, f) <= 0)
+ break;
+ disabled = true;
+ }
+ pos = ftell(f);
+ if(pos < 0)
+ break;
+ }
+
+ return disabled;