2 * onak-conf.c - Routines related to runtime config.
4 * Copyright 2002,2012 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include "onak-conf.h"
31 extern struct dbfuncs DBFUNCS;
34 * config - Runtime configuration for onak.
36 * This is the default config; normally overridden with values from the
39 struct onak_config config = {
42 NULL, /* adminemail */
50 * Options for directory backends.
55 * Options for the Postgres backend.
63 * Options for dynamic backends.
65 NULL, /* db_backend */
66 NULL, /* backends_dir */
68 &DBFUNCS, /* Default dbfuncs struct */
70 true, /* Check packet sig hashes */
73 bool parsebool(char *str, bool fallback)
75 if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
76 !strcasecmp(str, "0")) {
78 } else if (!strcasecmp(str, "true") || !strcasecmp(str, "yes") ||
79 !strcasecmp(str, "1")) {
82 logthing(LOGTHING_CRITICAL,
83 "Couldn't parse %s as a boolean config variable, "
84 "returning fallback of '%s'.",
86 fallback ? "true" : "false");
91 void readconfig(const char *configfile) {
99 if (configfile == NULL) {
101 if ((dir = getenv("XDG_CONFIG_HOME")) != NULL) {
102 len = strlen(dir) + 1 + 9 + 1; /* dir + / + onak.conf + NUL */
104 snprintf(conf, len, "%s/onak.conf", dir);
105 conffile = fopen(conf, "r");
107 } else if ((dir = getenv("HOME")) != NULL) {
108 len = strlen(dir) + 18 + 1; /* dir + /.config/onak.conf + NUL */
110 snprintf(conf, len, "%s/.config/onak.conf", dir);
111 conffile = fopen(conf, "r");
114 if (conffile == NULL) {
115 conffile = fopen(CONFIGFILE, "r");
118 conffile = fopen(configfile, "r");
120 if (conffile != NULL) {
121 fgets(curline, 1023, conffile);
123 while (!feof(conffile)) {
124 for (i = strlen(curline) - 1;
125 i >= 0 && isspace(curline[i]);
130 if (curline[0] == '#' || curline[0] == 0) {
132 * Comment line, ignore.
134 } else if (!strncmp("db_dir ", curline, 7)) {
135 config.db_dir = strdup(&curline[7]);
136 } else if (!strncmp("debug ", curline, 6)) {
138 * Not supported yet; ignore for compatibility with
141 } else if (!strncmp("default_language ", curline, 17)) {
143 * Not supported yet; ignore for compatibility with
146 } else if (!strncmp("mail_delivery_client ", curline, 21)) {
147 config.mta = strdup(&curline[21]);
148 } else if (!strncmp("maintainer_email ", curline, 17)) {
149 config.adminemail = strdup(&curline[17]);
150 } else if (!strncmp("mail_intro_file ", curline, 16)) {
152 * Not supported yet; ignore for compatibility with
155 } else if (!strncmp("help_dir ", curline, 9)) {
157 * Not supported yet; ignore for compatibility with
160 } else if (!strncmp("max_last ", curline, 9)) {
162 * Not supported yet; ignore for compatibility with
165 } else if (!strncmp("max_reply_keys ", curline, 15)) {
166 config.maxkeys = atoi(&curline[15]);
167 } else if (!strncmp("pg_dbhost ", curline, 10)) {
168 config.pg_dbhost = strdup(&curline[10]);
169 } else if (!strncmp("pg_dbname ", curline, 10)) {
170 config.pg_dbname = strdup(&curline[10]);
171 } else if (!strncmp("pg_dbuser ", curline, 10)) {
172 config.pg_dbuser = strdup(&curline[10]);
173 } else if (!strncmp("pg_dbpass ", curline, 10)) {
174 config.pg_dbpass = strdup(&curline[10]);
175 } else if (!strncmp("syncsite ", curline, 9)) {
177 lladd(config.syncsites, strdup(&curline[9]));
178 } else if (!strncmp("logfile ", curline, 8)) {
179 config.logfile = strdup(&curline[8]);
180 } else if (!strncmp("loglevel ", curline, 9)) {
181 setlogthreshold(atoi(&curline[9]));
182 } else if (!strncmp("this_site ", curline, 10)) {
183 config.thissite = strdup(&curline[10]);
184 } else if (!strncmp("socket_name ", curline, 12) ||
185 !strncmp("pks_bin_dir ", curline, 12) ||
186 !strncmp("mail_dir ", curline, 9) ||
187 !strncmp("www_port ", curline, 9)) {
189 * Not applicable; ignored for compatibility with pksd.
191 } else if (!strncmp("db_backend ", curline, 11)) {
192 config.db_backend = strdup(&curline[11]);
193 } else if (!strncmp("backends_dir ", curline, 13)) {
194 config.backends_dir = strdup(&curline[13]);
195 } else if (!strncmp("use_keyd ", curline, 9)) {
196 config.use_keyd = parsebool(&curline[9],
198 } else if (!strncmp("check_sighash ", curline, 9)) {
199 config.check_sighash = parsebool(&curline[9],
200 config.check_sighash);
202 logthing(LOGTHING_ERROR,
203 "Unknown config line: %s", curline);
206 fgets(curline, 1023, conffile);
210 logthing(LOGTHING_NOTICE,
211 "Couldn't open config file; using defaults.");
215 void cleanupconfig(void) {
216 if (config.thissite != NULL) {
217 free(config.thissite);
218 config.thissite = NULL;
220 if (config.adminemail != NULL) {
221 free(config.adminemail);
222 config.adminemail = NULL;
224 if (config.mta != NULL) {
228 if (config.db_dir != NULL) {
230 config.db_dir = NULL;
232 if (config.pg_dbhost != NULL) {
233 free(config.pg_dbhost);
234 config.pg_dbhost = NULL;
236 if (config.pg_dbname != NULL) {
237 free(config.pg_dbname);
238 config.pg_dbname = NULL;
240 if (config.pg_dbuser != NULL) {
241 free(config.pg_dbuser);
242 config.pg_dbuser = NULL;
244 if (config.pg_dbpass != NULL) {
245 free(config.pg_dbpass);
246 config.pg_dbpass = NULL;
248 if (config.syncsites != NULL) {
249 llfree(config.syncsites, free);
250 config.syncsites = NULL;
252 if (config.logfile != NULL) {
253 free(config.logfile);
254 config.logfile = NULL;
256 if (config.db_backend != NULL) {
257 free(config.db_backend);
258 config.db_backend = NULL;
260 if (config.backends_dir != NULL) {
261 free(config.backends_dir);
262 config.backends_dir = NULL;