]> the.earth.li Git - onak.git/blob - onak-conf.c
Fix issue with pre-seeding key database on Debian install
[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(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         128,                    /* maxkeys */
41         NULL,                   /* thissite */
42         NULL,                   /* adminemail */
43         NULL,                   /* mta */
44         NULL,                   /* syncsites */
45         NULL,                   /* logfile */
46
47         false,                  /* use_keyd */
48
49         /*
50          * Options for directory backends.
51          */
52         NULL,                   /* db_dir */
53
54         /*
55          * Options for the Postgres backend.
56          */
57         NULL,                   /* pg_dbhost */
58         NULL,                   /* pg_dbname */
59         NULL,                   /* pg_dbuser */
60         NULL,                   /* pg_dbpass */
61
62         /*
63          * Options for dynamic backends.
64          */
65         NULL,                   /* db_backend */
66         NULL,                   /* backends_dir */
67
68         DBINIT,                 /* Default db initialisation function */
69
70         true,                   /* Check packet sig hashes */
71 };
72
73 bool parsebool(char *str, bool fallback)
74 {
75         if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
76                         !strcasecmp(str, "0")) {
77                 return false;
78         } else if (!strcasecmp(str, "true") || !strcasecmp(str, "yes") ||
79                         !strcasecmp(str, "1")) {
80                 return true;
81         } else {
82                 logthing(LOGTHING_CRITICAL,
83                         "Couldn't parse %s as a boolean config variable, "
84                         "returning fallback of '%s'.",
85                         str,
86                         fallback ? "true" : "false");
87                 return fallback;
88         }
89 }
90
91 void readconfig(const char *configfile) {
92         FILE *conffile;
93         char  curline[1024];
94         int   i;
95         char *dir, *conf;
96         size_t len;
97
98         curline[1023] = 0;
99         if (configfile == NULL) {
100                 conffile = NULL;
101                 if ((dir = getenv("XDG_CONFIG_HOME")) != NULL) {
102                         len = strlen(dir) + 1 + 9 + 1; /* dir + / + onak.conf + NUL */
103                         conf = malloc(len);
104                         snprintf(conf, len, "%s/onak.conf", dir);
105                         conffile = fopen(conf, "r");
106                         free(conf);
107                 } else if ((dir = getenv("HOME")) != NULL) {
108                         len = strlen(dir) + 18 + 1; /* dir + /.config/onak.conf + NUL */
109                         conf = malloc(len);
110                         snprintf(conf, len, "%s/.config/onak.conf", dir);
111                         conffile = fopen(conf, "r");
112                         free(conf);
113                 }
114                 if (conffile == NULL) {
115                         conffile = fopen(CONFIGFILE, "r");
116                 }
117         } else {
118                 conffile = fopen(configfile, "r");
119         }
120         if (conffile != NULL) {
121                 if (!fgets(curline, 1023, conffile)) {
122                         logthing(LOGTHING_CRITICAL,
123                                 "Problem reading configuration file.");
124                         fclose(conffile);
125                         return;
126                 }
127
128                 while (!feof(conffile)) {
129                         for (i = strlen(curline) - 1;
130                                         i >= 0 && isspace(curline[i]);
131                                         i--) {
132                                 curline[i] = 0;
133                         }
134
135                 if (curline[0] == '#' || curline[0] == 0) {
136                         /*
137                          * Comment line, ignore.
138                          */
139                 } else if (!strncmp("db_dir ", curline, 7)) {
140                         config.db_dir = strdup(&curline[7]);
141                 } else if (!strncmp("debug ", curline, 6)) {
142                         /*
143                          * Not supported yet; ignore for compatibility with
144                          * pksd.
145                          */
146                 } else if (!strncmp("default_language ", curline, 17)) {
147                         /*
148                          * Not supported yet; ignore for compatibility with
149                          * pksd.
150                          */
151                 } else if (!strncmp("mail_delivery_client ", curline, 21)) {
152                         config.mta = strdup(&curline[21]);
153                 } else if (!strncmp("maintainer_email ", curline, 17)) {
154                         config.adminemail = strdup(&curline[17]);
155                 } else if (!strncmp("mail_intro_file ", curline, 16)) {
156                         /*
157                          * Not supported yet; ignore for compatibility with
158                          * pksd.
159                          */
160                 } else if (!strncmp("help_dir ", curline, 9)) {
161                         /*
162                          * Not supported yet; ignore for compatibility with
163                          * pksd.
164                          */
165                 } else if (!strncmp("max_last ", curline, 9)) {
166                         /*
167                          * Not supported yet; ignore for compatibility with
168                          * pksd.
169                          */
170                 } else if (!strncmp("max_reply_keys ", curline, 15)) {
171                         config.maxkeys = atoi(&curline[15]);
172                 } else if (!strncmp("pg_dbhost ", curline, 10)) {
173                         config.pg_dbhost = strdup(&curline[10]);
174                 } else if (!strncmp("pg_dbname ", curline, 10)) {
175                         config.pg_dbname = strdup(&curline[10]);
176                 } else if (!strncmp("pg_dbuser ", curline, 10)) {
177                         config.pg_dbuser = strdup(&curline[10]);
178                 } else if (!strncmp("pg_dbpass ", curline, 10)) {
179                         config.pg_dbpass = strdup(&curline[10]);
180                 } else if (!strncmp("syncsite ", curline, 9)) {
181                         config.syncsites =
182                                 lladd(config.syncsites, strdup(&curline[9]));
183                 } else if (!strncmp("logfile ", curline, 8)) {
184                         config.logfile = strdup(&curline[8]);
185                 } else if (!strncmp("loglevel ", curline, 9)) {
186                         setlogthreshold(atoi(&curline[9]));
187                 } else if (!strncmp("this_site ", curline, 10)) {
188                         config.thissite = strdup(&curline[10]);
189                 } else if (!strncmp("socket_name ", curline, 12) ||
190                                 !strncmp("pks_bin_dir ", curline, 12) ||
191                                 !strncmp("mail_dir ", curline, 9) ||
192                                 !strncmp("www_port ", curline, 9)) {
193                         /*
194                          * Not applicable; ignored for compatibility with pksd.
195                          */
196                 } else if (!strncmp("db_backend ", curline, 11)) {
197                         config.db_backend = strdup(&curline[11]);
198                 } else if (!strncmp("backends_dir ", curline, 13)) {
199                         config.backends_dir = strdup(&curline[13]);
200                 } else if (!strncmp("use_keyd ", curline, 9)) {
201                         config.use_keyd = parsebool(&curline[9],
202                                                 config.use_keyd);
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 cleanupconfig(void) {
226         if (config.thissite != NULL) {
227                 free(config.thissite);
228                 config.thissite = NULL;
229         }
230         if (config.adminemail != NULL) {
231                 free(config.adminemail);
232                 config.adminemail = NULL;
233         }
234         if (config.mta != NULL) {
235                 free(config.mta);
236                 config.mta = NULL;
237         }
238         if (config.db_dir != NULL) {
239                 free(config.db_dir);
240                 config.db_dir = NULL;
241         }
242         if (config.pg_dbhost != NULL) {
243                 free(config.pg_dbhost);
244                 config.pg_dbhost = NULL;
245         }
246         if (config.pg_dbname != NULL) {
247                 free(config.pg_dbname);
248                 config.pg_dbname = NULL;
249         }
250         if (config.pg_dbuser != NULL) {
251                 free(config.pg_dbuser);
252                 config.pg_dbuser = NULL;
253         }
254         if (config.pg_dbpass != NULL) {
255                 free(config.pg_dbpass);
256                 config.pg_dbpass = NULL;
257         }
258         if (config.syncsites != NULL) {
259                 llfree(config.syncsites, free);
260                 config.syncsites = NULL;
261         }
262         if (config.logfile != NULL) {
263                 free(config.logfile);
264                 config.logfile = NULL;
265         }
266         if (config.db_backend != NULL) {
267                 free(config.db_backend);
268                 config.db_backend = NULL;
269         }
270         if (config.backends_dir != NULL) {
271                 free(config.backends_dir);
272                 config.backends_dir = NULL;
273         }
274 }