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(struct onak_db_config *dbcfg, 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 = {
55 .check_sighash = true,
61 bool parsebool(char *str, bool fallback)
63 if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
64 !strcasecmp(str, "0")) {
66 } else if (!strcasecmp(str, "true") || !strcasecmp(str, "yes") ||
67 !strcasecmp(str, "1")) {
70 logthing(LOGTHING_CRITICAL,
71 "Couldn't parse %s as a boolean config variable, "
72 "returning fallback of '%s'.",
74 fallback ? "true" : "false");
79 void readconfig(const char *configfile) {
85 struct onak_db_config *backend;
88 if (configfile == NULL) {
90 if ((dir = getenv("XDG_CONFIG_HOME")) != NULL) {
91 len = strlen(dir) + 1 + 9 + 1; /* dir + / + onak.conf + NUL */
93 snprintf(conf, len, "%s/onak.conf", dir);
94 conffile = fopen(conf, "r");
96 } else if ((dir = getenv("HOME")) != NULL) {
97 len = strlen(dir) + 18 + 1; /* dir + /.config/onak.conf + NUL */
99 snprintf(conf, len, "%s/.config/onak.conf", dir);
100 conffile = fopen(conf, "r");
103 if (conffile == NULL) {
104 conffile = fopen(CONFIGFILE, "r");
107 conffile = fopen(configfile, "r");
109 if (conffile != NULL) {
110 if (!fgets(curline, 1023, conffile)) {
111 logthing(LOGTHING_CRITICAL,
112 "Problem reading configuration file.");
117 /* Add a single DB configuration */
118 backend = calloc(1, sizeof(*backend));
119 config.backend = backend;
120 config.backends = lladd(NULL, backend);
122 while (!feof(conffile)) {
123 for (i = strlen(curline) - 1;
124 i >= 0 && isspace(curline[i]);
129 if (curline[0] == '#' || curline[0] == 0) {
131 * Comment line, ignore.
133 } else if (!strncmp("db_dir ", curline, 7)) {
134 backend->location = strdup(&curline[7]);
135 } else if (!strncmp("debug ", curline, 6)) {
137 * Not supported yet; ignore for compatibility with
140 } else if (!strncmp("default_language ", curline, 17)) {
142 * Not supported yet; ignore for compatibility with
145 } else if (!strncmp("mail_delivery_client ", curline, 21)) {
146 config.mta = strdup(&curline[21]);
147 } else if (!strncmp("maintainer_email ", curline, 17)) {
148 config.adminemail = strdup(&curline[17]);
149 } else if (!strncmp("mail_intro_file ", curline, 16)) {
151 * Not supported yet; ignore for compatibility with
154 } else if (!strncmp("help_dir ", curline, 9)) {
156 * Not supported yet; ignore for compatibility with
159 } else if (!strncmp("max_last ", curline, 9)) {
161 * Not supported yet; ignore for compatibility with
164 } else if (!strncmp("max_reply_keys ", curline, 15)) {
165 config.maxkeys = atoi(&curline[15]);
166 } else if (!strncmp("pg_dbhost ", curline, 10)) {
167 backend->hostname = strdup(&curline[10]);
168 } else if (!strncmp("pg_dbname ", curline, 10)) {
169 backend->location = strdup(&curline[10]);
170 } else if (!strncmp("pg_dbuser ", curline, 10)) {
171 backend->username = strdup(&curline[10]);
172 } else if (!strncmp("pg_dbpass ", curline, 10)) {
173 backend->password = strdup(&curline[10]);
174 } else if (!strncmp("syncsite ", curline, 9)) {
176 lladd(config.syncsites, strdup(&curline[9]));
177 } else if (!strncmp("logfile ", curline, 8)) {
178 config.logfile = strdup(&curline[8]);
179 } else if (!strncmp("loglevel ", curline, 9)) {
180 setlogthreshold(atoi(&curline[9]));
181 } else if (!strncmp("this_site ", curline, 10)) {
182 config.thissite = strdup(&curline[10]);
183 } else if (!strncmp("socket_name ", curline, 12) ||
184 !strncmp("www_port ", curline, 9)) {
186 * Not applicable; ignored for compatibility with pksd.
188 } else if (!strncmp("pks_bin_dir ", curline, 12)) {
189 config.bin_dir = strdup(&curline[12]);
190 } else if (!strncmp("mail_dir ", curline, 9)) {
191 config.mail_dir = strdup(&curline[9]);
192 } else if (!strncmp("db_backend ", curline, 11)) {
193 backend->type = strdup(&curline[11]);
194 backend->name = strdup(&curline[11]);
195 config.db_backend = strdup(&curline[11]);
196 } else if (!strncmp("backends_dir ", curline, 13)) {
197 config.backends_dir = strdup(&curline[13]);
198 } else if (!strncmp("use_keyd ", curline, 9)) {
199 config.use_keyd = parsebool(&curline[9],
201 } else if (!strncmp("sock_dir ", curline, 9)) {
202 config.sock_dir = strdup(&curline[9]);
203 } else if (!strncmp("check_sighash ", curline, 9)) {
204 config.check_sighash = parsebool(&curline[9],
205 config.check_sighash);
207 logthing(LOGTHING_ERROR,
208 "Unknown config line: %s", curline);
211 if (!fgets(curline, 1023, conffile) &&
213 logthing(LOGTHING_CRITICAL,
214 "Problem reading configuration file.");
220 logthing(LOGTHING_NOTICE,
221 "Couldn't open config file; using defaults.");
225 void cleanupdbconfig(void *object)
227 struct onak_db_config *dbconfig = (struct onak_db_config *) object;
229 if (dbconfig->name != NULL) {
230 free(dbconfig->name);
231 dbconfig->name = NULL;
233 if (dbconfig->type != NULL) {
234 free(dbconfig->type);
235 dbconfig->type = NULL;
237 if (dbconfig->location != NULL) {
238 free(dbconfig->location);
239 dbconfig->location = NULL;
241 if (dbconfig->hostname != NULL) {
242 free(dbconfig->hostname);
243 dbconfig->hostname = NULL;
245 if (dbconfig->username != NULL) {
246 free(dbconfig->username);
247 dbconfig->username = NULL;
249 if (dbconfig->password != NULL) {
250 free(dbconfig->password);
251 dbconfig->password = NULL;
255 void cleanupconfig(void) {
256 /* Free any defined DB backend configuration first */
257 llfree(config.backends, cleanupdbconfig);
258 config.backends = NULL;
260 if (config.thissite != NULL) {
261 free(config.thissite);
262 config.thissite = NULL;
264 if (config.adminemail != NULL) {
265 free(config.adminemail);
266 config.adminemail = NULL;
268 if (config.mta != NULL) {
272 if (config.syncsites != NULL) {
273 llfree(config.syncsites, free);
274 config.syncsites = NULL;
276 if (config.logfile != NULL) {
277 free(config.logfile);
278 config.logfile = NULL;
280 if (config.db_backend != NULL) {
281 free(config.db_backend);
282 config.db_backend = NULL;
284 if (config.backends_dir != NULL) {
285 free(config.backends_dir);
286 config.backends_dir = NULL;
288 if (config.bin_dir != NULL) {
289 free(config.bin_dir);
290 config.bin_dir = NULL;
292 if (config.mail_dir != NULL) {
293 free(config.mail_dir);
294 config.mail_dir = NULL;