]> the.earth.li Git - onak.git/blob - onak-conf.c
0af6a45458e2c708a87d8822c3286f48aec25f8d
[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("www_port ", curline, 9)) {
185                         /*
186                          * Not applicable; ignored for compatibility with pksd.
187                          */
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],
200                                                 config.use_keyd);
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);
206                 } else {
207                         logthing(LOGTHING_ERROR,
208                                 "Unknown config line: %s", curline);
209                 }
210
211                         if (!fgets(curline, 1023, conffile) &&
212                                         !feof(conffile)) {
213                                 logthing(LOGTHING_CRITICAL,
214                                         "Problem reading configuration file.");
215                                 break;
216                         }
217                 }
218                 fclose(conffile);
219         } else {
220                 logthing(LOGTHING_NOTICE,
221                                 "Couldn't open config file; using defaults.");
222         }
223 }
224
225 void cleanupdbconfig(void *object)
226 {
227         struct onak_db_config *dbconfig = (struct onak_db_config *) object;
228
229         if (dbconfig->name != NULL) {
230                 free(dbconfig->name);
231                 dbconfig->name = NULL;
232         }
233         if (dbconfig->type != NULL) {
234                 free(dbconfig->type);
235                 dbconfig->type = NULL;
236         }
237         if (dbconfig->location != NULL) {
238                 free(dbconfig->location);
239                 dbconfig->location = NULL;
240         }
241         if (dbconfig->hostname != NULL) {
242                 free(dbconfig->hostname);
243                 dbconfig->hostname = NULL;
244         }
245         if (dbconfig->username != NULL) {
246                 free(dbconfig->username);
247                 dbconfig->username = NULL;
248         }
249         if (dbconfig->password != NULL) {
250                 free(dbconfig->password);
251                 dbconfig->password = NULL;
252         }
253 }
254
255 void cleanupconfig(void) {
256         /* Free any defined DB backend configuration first */
257         llfree(config.backends, cleanupdbconfig);
258         config.backends = NULL;
259
260         if (config.thissite != NULL) {
261                 free(config.thissite);
262                 config.thissite = NULL;
263         }
264         if (config.adminemail != NULL) {
265                 free(config.adminemail);
266                 config.adminemail = NULL;
267         }
268         if (config.mta != NULL) {
269                 free(config.mta);
270                 config.mta = NULL;
271         }
272         if (config.syncsites != NULL) {
273                 llfree(config.syncsites, free);
274                 config.syncsites = NULL;
275         }
276         if (config.logfile != NULL) {
277                 free(config.logfile);
278                 config.logfile = NULL;
279         }
280         if (config.db_backend != NULL) {
281                 free(config.db_backend);
282                 config.db_backend = NULL;
283         }
284         if (config.backends_dir != NULL) {
285                 free(config.backends_dir);
286                 config.backends_dir = NULL;
287         }
288         if (config.bin_dir != NULL) {
289                 free(config.bin_dir);
290                 config.bin_dir = NULL;
291         }
292         if (config.mail_dir != NULL) {
293                 free(config.mail_dir);
294                 config.mail_dir = NULL;
295         }
296 }