+bool read_config_file(splay_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(DEBUG_ALWAYS, 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;
+}
+
+bool read_server_config(void) {
+ char *fname;
+ bool x;
+
+ xasprintf(&fname, "%s" SLASH "tinc.conf", mesh->confbase);
+ errno = 0;
+ x = read_config_file(mesh->config, fname);
+
+ if(!x && errno)
+ logger(DEBUG_ALWAYS, LOG_ERR, "Failed to read `%s': %s", fname, strerror(errno));
+
+ free(fname);
+
+ return x;
+}
+
+bool read_host_config(splay_tree_t *config_tree, const char *name) {
+ char *fname;
+ bool x;
+
+ xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
+ x = read_config_file(config_tree, fname);
+ free(fname);
+
+ return x;
+}
+
+bool append_config_file(const char *name, const char *key, const char *value) {
+ char *fname;
+ xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
+
+ FILE *fp = fopen(fname, "a");
+
+ if(!fp) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Cannot open config file %s: %s", fname, strerror(errno));
+ } else {
+ fprintf(fp, "\n# The following line was automatically added by tinc\n%s = %s\n", key, value);
+ fclose(fp);
+ }