+int read_config_file(avl_tree_t *config_tree, const char *fname)
+{
+ int err = -2; /* Parse error */
+ FILE *fp;
+ char *buffer, *line;
+ char *variable, *value, *eol;
+ int lineno = 0;
+ int len;
+ bool ignore = false;
+ config_t *cfg;
+ size_t bufsize;
+
+ cp();
+
+ fp = fopen(fname, "r");
+
+ if(!fp) {
+ logger(LOG_ERR, _("Cannot open config file %s: %s"), fname,
+ strerror(errno));
+ return -3;
+ }
+
+ bufsize = 100;
+ buffer = xmalloc(bufsize);
+
+ for(;;) {
+ if(feof(fp)) {
+ err = 0;
+ break;
+ }
+
+ line = readline(fp, &buffer, &bufsize);
+
+ if(!line) {
+ err = -1;
+ break;
+ }
+
+ lineno++;
+
+ if(!*line || *line == '#')
+ continue;
+
+ if(ignore) {
+ if(!strncmp(line, "-----END", 8))
+ ignore = false;
+ continue;
+ }
+
+ if(!strncmp(line, "-----BEGIN", 10)) {
+ ignore = true;
+ continue;
+ }
+
+ variable = value = line;
+
+ eol = line + strlen(line);
+ while(strchr("\t ", *--eol))
+ *eol = '\0';
+
+ len = strcspn(value, "\t =");
+ value += len;
+ value += strspn(value, "\t ");
+ if(*value == '=') {
+ value++;
+ value += strspn(value, "\t ");
+ }
+ variable[len] = '\0';
+
+
+ if(!*value) {
+ logger(LOG_ERR, _("No value for variable `%s' on line %d while reading config file %s"),
+ variable, lineno, fname);
+ break;
+ }
+
+ cfg = new_config();
+ cfg->variable = xstrdup(variable);
+ cfg->value = xstrdup(value);
+ cfg->file = xstrdup(fname);
+ cfg->line = lineno;
+
+ config_add(config_tree, cfg);
+ }
+
+ free(buffer);
+ fclose(fp);
+
+ return err;
+}
+
+bool read_server_config()
+{
+ char *fname;
+ int x;
+
+ cp();
+
+ xasprintf(&fname, "%s/tinc.conf", confbase);
+ x = read_config_file(config_tree, fname);
+
+ if(x == -1) { /* System error: complain */
+ logger(LOG_ERR, _("Failed to read `%s': %s"), fname, strerror(errno));
+ }
+
+ free(fname);
+
+ return x == 0;
+}
+
+FILE *ask_and_open(const char *filename, const char *what)