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 onak_dbctx *DBINIT(bool readonly);
34 * config - Runtime configuration for onak.
36 * This is the default config; normally overridden with values from the
39 struct onak_config config = {
51 * Options for directory backends.
56 * Options for the Postgres backend.
64 * Options for dynamic backends.
71 .check_sighash = true,
74 bool parsebool(char *str, bool fallback)
76 if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
77 !strcasecmp(str, "0")) {
79 } else if (!strcasecmp(str, "true") || !strcasecmp(str, "yes") ||
80 !strcasecmp(str, "1")) {
83 logthing(LOGTHING_CRITICAL,
84 "Couldn't parse %s as a boolean config variable, "
85 "returning fallback of '%s'.",
87 fallback ? "true" : "false");
92 void readconfig(const char *configfile) {
100 if (configfile == NULL) {
102 if ((dir = getenv("XDG_CONFIG_HOME")) != NULL) {
103 len = strlen(dir) + 1 + 9 + 1; /* dir + / + onak.conf + NUL */
105 snprintf(conf, len, "%s/onak.conf", dir);
106 conffile = fopen(conf, "r");
108 } else if ((dir = getenv("HOME")) != NULL) {
109 len = strlen(dir) + 18 + 1; /* dir + /.config/onak.conf + NUL */
111 snprintf(conf, len, "%s/.config/onak.conf", dir);
112 conffile = fopen(conf, "r");
115 if (conffile == NULL) {
116 conffile = fopen(CONFIGFILE, "r");
119 conffile = fopen(configfile, "r");
121 if (conffile != NULL) {
122 if (!fgets(curline, 1023, conffile)) {
123 logthing(LOGTHING_CRITICAL,
124 "Problem reading configuration file.");
129 while (!feof(conffile)) {
130 for (i = strlen(curline) - 1;
131 i >= 0 && isspace(curline[i]);
136 if (curline[0] == '#' || curline[0] == 0) {
138 * Comment line, ignore.
140 } else if (!strncmp("db_dir ", curline, 7)) {
141 config.db_dir = strdup(&curline[7]);
142 } else if (!strncmp("debug ", curline, 6)) {
144 * Not supported yet; ignore for compatibility with
147 } else if (!strncmp("default_language ", curline, 17)) {
149 * Not supported yet; ignore for compatibility with
152 } else if (!strncmp("mail_delivery_client ", curline, 21)) {
153 config.mta = strdup(&curline[21]);
154 } else if (!strncmp("maintainer_email ", curline, 17)) {
155 config.adminemail = strdup(&curline[17]);
156 } else if (!strncmp("mail_intro_file ", curline, 16)) {
158 * Not supported yet; ignore for compatibility with
161 } else if (!strncmp("help_dir ", curline, 9)) {
163 * Not supported yet; ignore for compatibility with
166 } else if (!strncmp("max_last ", curline, 9)) {
168 * Not supported yet; ignore for compatibility with
171 } else if (!strncmp("max_reply_keys ", curline, 15)) {
172 config.maxkeys = atoi(&curline[15]);
173 } else if (!strncmp("pg_dbhost ", curline, 10)) {
174 config.pg_dbhost = strdup(&curline[10]);
175 } else if (!strncmp("pg_dbname ", curline, 10)) {
176 config.pg_dbname = strdup(&curline[10]);
177 } else if (!strncmp("pg_dbuser ", curline, 10)) {
178 config.pg_dbuser = strdup(&curline[10]);
179 } else if (!strncmp("pg_dbpass ", curline, 10)) {
180 config.pg_dbpass = strdup(&curline[10]);
181 } else if (!strncmp("syncsite ", curline, 9)) {
183 lladd(config.syncsites, strdup(&curline[9]));
184 } else if (!strncmp("logfile ", curline, 8)) {
185 config.logfile = strdup(&curline[8]);
186 } else if (!strncmp("loglevel ", curline, 9)) {
187 setlogthreshold(atoi(&curline[9]));
188 } else if (!strncmp("this_site ", curline, 10)) {
189 config.thissite = strdup(&curline[10]);
190 } else if (!strncmp("socket_name ", curline, 12) ||
191 !strncmp("pks_bin_dir ", curline, 12) ||
192 !strncmp("mail_dir ", curline, 9) ||
193 !strncmp("www_port ", curline, 9)) {
195 * Not applicable; ignored for compatibility with pksd.
197 } else if (!strncmp("db_backend ", curline, 11)) {
198 config.db_backend = strdup(&curline[11]);
199 } else if (!strncmp("backends_dir ", curline, 13)) {
200 config.backends_dir = strdup(&curline[13]);
201 } else if (!strncmp("use_keyd ", curline, 9)) {
202 config.use_keyd = parsebool(&curline[9],
204 } else if (!strncmp("sock_dir ", curline, 9)) {
205 config.sock_dir = strdup(&curline[9]);
206 } else if (!strncmp("check_sighash ", curline, 9)) {
207 config.check_sighash = parsebool(&curline[9],
208 config.check_sighash);
210 logthing(LOGTHING_ERROR,
211 "Unknown config line: %s", curline);
214 if (!fgets(curline, 1023, conffile) &&
216 logthing(LOGTHING_CRITICAL,
217 "Problem reading configuration file.");
223 logthing(LOGTHING_NOTICE,
224 "Couldn't open config file; using defaults.");
228 void cleanupconfig(void) {
229 if (config.thissite != NULL) {
230 free(config.thissite);
231 config.thissite = NULL;
233 if (config.adminemail != NULL) {
234 free(config.adminemail);
235 config.adminemail = NULL;
237 if (config.mta != NULL) {
241 if (config.db_dir != NULL) {
243 config.db_dir = NULL;
245 if (config.pg_dbhost != NULL) {
246 free(config.pg_dbhost);
247 config.pg_dbhost = NULL;
249 if (config.pg_dbname != NULL) {
250 free(config.pg_dbname);
251 config.pg_dbname = NULL;
253 if (config.pg_dbuser != NULL) {
254 free(config.pg_dbuser);
255 config.pg_dbuser = NULL;
257 if (config.pg_dbpass != NULL) {
258 free(config.pg_dbpass);
259 config.pg_dbpass = NULL;
261 if (config.syncsites != NULL) {
262 llfree(config.syncsites, free);
263 config.syncsites = NULL;
265 if (config.logfile != NULL) {
266 free(config.logfile);
267 config.logfile = NULL;
269 if (config.db_backend != NULL) {
270 free(config.db_backend);
271 config.db_backend = NULL;
273 if (config.backends_dir != NULL) {
274 free(config.backends_dir);
275 config.backends_dir = NULL;