]> the.earth.li Git - onak.git/blob - onak-conf.c
Properly isolate database backend configuration
[onak.git] / onak-conf.c
1 /*
2  * onak-conf.c - Routines related to runtime config.
3  *
4  * Copyright 2002,2012 Jonathan McDowell <noodles@earth.li>
5  *
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.
9  *
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
13  * more details.
14  *
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.
18  */
19
20 #include "config.h"
21
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "ll.h"
28 #include "log.h"
29 #include "onak-conf.h"
30
31 extern struct onak_dbctx *DBINIT(struct onak_db_config *dbcfg, bool readonly);
32
33 /*
34  *      config - Runtime configuration for onak.
35  *
36  *      This is the default config; normally overridden with values from the
37  *      config file.
38  */
39 struct onak_config config = {
40         .maxkeys = 128,
41         .thissite = NULL,
42         .adminemail = NULL,
43         .mta = NULL,
44         .syncsites = NULL,
45         .logfile = NULL,
46
47         .use_keyd = false,
48         .sock_dir = ".",
49
50         .backends = NULL,
51         .backends_dir = NULL,
52
53         .dbinit = DBINIT,
54
55         .check_sighash = true,
56
57         .bin_dir = NULL,
58         .mail_dir = NULL,
59 };
60
61 bool parsebool(char *str, bool fallback)
62 {
63         if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
64                         !strcasecmp(str, "0")) {
65                 return false;
66         } else if (!strcasecmp(str, "true") || !strcasecmp(str, "yes") ||
67                         !strcasecmp(str, "1")) {
68                 return true;
69         } else {
70                 logthing(LOGTHING_CRITICAL,
71                         "Couldn't parse %s as a boolean config variable, "
72                         "returning fallback of '%s'.",
73                         str,
74                         fallback ? "true" : "false");
75                 return fallback;
76         }
77 }
78
79 void readconfig(const char *configfile) {
80         FILE *conffile;
81         char  curline[1024];
82         int   i;
83         char *dir, *conf;
84         size_t len;
85         struct onak_db_config *backend;
86
87         curline[1023] = 0;
88         if (configfile == NULL) {
89                 conffile = NULL;
90                 if ((dir = getenv("XDG_CONFIG_HOME")) != NULL) {
91                         len = strlen(dir) + 1 + 9 + 1; /* dir + / + onak.conf + NUL */
92                         conf = malloc(len);
93                         snprintf(conf, len, "%s/onak.conf", dir);
94                         conffile = fopen(conf, "r");
95                         free(conf);
96                 } else if ((dir = getenv("HOME")) != NULL) {
97                         len = strlen(dir) + 18 + 1; /* dir + /.config/onak.conf + NUL */
98                         conf = malloc(len);
99                         snprintf(conf, len, "%s/.config/onak.conf", dir);
100                         conffile = fopen(conf, "r");
101                         free(conf);
102                 }
103                 if (conffile == NULL) {
104                         conffile = fopen(CONFIGFILE, "r");
105                 }
106         } else {
107                 conffile = fopen(configfile, "r");
108         }
109         if (conffile != NULL) {
110                 if (!fgets(curline, 1023, conffile)) {
111                         logthing(LOGTHING_CRITICAL,
112                                 "Problem reading configuration file.");
113                         fclose(conffile);
114                         return;
115                 }
116
117                 /* Add a single DB configuration */
118                 backend = calloc(1, sizeof(*backend));
119                 config.backend = backend;
120                 config.backends = lladd(NULL, backend);
121
122                 while (!feof(conffile)) {
123                         for (i = strlen(curline) - 1;
124                                         i >= 0 && isspace(curline[i]);
125                                         i--) {
126                                 curline[i] = 0;
127                         }
128
129                 if (curline[0] == '#' || curline[0] == 0) {
130                         /*
131                          * Comment line, ignore.
132                          */
133                 } else if (!strncmp("db_dir ", curline, 7)) {
134                         backend->location = strdup(&curline[7]);
135                 } else if (!strncmp("debug ", curline, 6)) {
136                         /*
137                          * Not supported yet; ignore for compatibility with
138                          * pksd.
139                          */
140                 } else if (!strncmp("default_language ", curline, 17)) {
141                         /*
142                          * Not supported yet; ignore for compatibility with
143                          * pksd.
144                          */
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)) {
150                         /*
151                          * Not supported yet; ignore for compatibility with
152                          * pksd.
153                          */
154                 } else if (!strncmp("help_dir ", curline, 9)) {
155                         /*
156                          * Not supported yet; ignore for compatibility with
157                          * pksd.
158                          */
159                 } else if (!strncmp("max_last ", curline, 9)) {
160                         /*
161                          * Not supported yet; ignore for compatibility with
162                          * pksd.
163                          */
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)) {
175                         config.syncsites =
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("pks_bin_dir ", curline, 12) ||
185                                 !strncmp("mail_dir ", curline, 9) ||
186                                 !strncmp("www_port ", curline, 9)) {
187                         /*
188                          * Not applicable; ignored for compatibility with pksd.
189                          */
190                 } else if (!strncmp("db_backend ", curline, 11)) {
191                         backend->type = strdup(&curline[11]);
192                         backend->name = strdup(&curline[11]);
193                         config.db_backend = strdup(&curline[11]);
194                 } else if (!strncmp("backends_dir ", curline, 13)) {
195                         config.backends_dir = strdup(&curline[13]);
196                 } else if (!strncmp("use_keyd ", curline, 9)) {
197                         config.use_keyd = parsebool(&curline[9],
198                                                 config.use_keyd);
199                 } else if (!strncmp("sock_dir ", curline, 9)) {
200                         config.sock_dir = strdup(&curline[9]);
201                 } else if (!strncmp("check_sighash ", curline, 9)) {
202                         config.check_sighash = parsebool(&curline[9],
203                                                 config.check_sighash);
204                 } else {
205                         logthing(LOGTHING_ERROR,
206                                 "Unknown config line: %s", curline);
207                 }
208
209                         if (!fgets(curline, 1023, conffile) &&
210                                         !feof(conffile)) {
211                                 logthing(LOGTHING_CRITICAL,
212                                         "Problem reading configuration file.");
213                                 break;
214                         }
215                 }
216                 fclose(conffile);
217         } else {
218                 logthing(LOGTHING_NOTICE,
219                                 "Couldn't open config file; using defaults.");
220         }
221 }
222
223 void cleanupdbconfig(void *object)
224 {
225         struct onak_db_config *dbconfig = (struct onak_db_config *) object;
226
227         if (dbconfig->name != NULL) {
228                 free(dbconfig->name);
229                 dbconfig->name = NULL;
230         }
231         if (dbconfig->type != NULL) {
232                 free(dbconfig->type);
233                 dbconfig->type = NULL;
234         }
235         if (dbconfig->location != NULL) {
236                 free(dbconfig->location);
237                 dbconfig->location = NULL;
238         }
239         if (dbconfig->hostname != NULL) {
240                 free(dbconfig->hostname);
241                 dbconfig->hostname = NULL;
242         }
243         if (dbconfig->username != NULL) {
244                 free(dbconfig->username);
245                 dbconfig->username = NULL;
246         }
247         if (dbconfig->password != NULL) {
248                 free(dbconfig->password);
249                 dbconfig->password = NULL;
250         }
251 }
252
253 void cleanupconfig(void) {
254         /* Free any defined DB backend configuration first */
255         llfree(config.backends, cleanupdbconfig);
256         config.backends = NULL;
257
258         if (config.thissite != NULL) {
259                 free(config.thissite);
260                 config.thissite = NULL;
261         }
262         if (config.adminemail != NULL) {
263                 free(config.adminemail);
264                 config.adminemail = NULL;
265         }
266         if (config.mta != NULL) {
267                 free(config.mta);
268                 config.mta = NULL;
269         }
270         if (config.syncsites != NULL) {
271                 llfree(config.syncsites, free);
272                 config.syncsites = NULL;
273         }
274         if (config.logfile != NULL) {
275                 free(config.logfile);
276                 config.logfile = NULL;
277         }
278         if (config.db_backend != NULL) {
279                 free(config.db_backend);
280                 config.db_backend = NULL;
281         }
282         if (config.backends_dir != NULL) {
283                 free(config.backends_dir);
284                 config.backends_dir = NULL;
285         }
286 }