]> the.earth.li Git - onak.git/blob - onak-conf.c
Move to CMake over autoconf
[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, see <https://www.gnu.org/licenses/>.
17  */
18 #include <ctype.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <strings.h>
23
24 #include "build-config.h"
25
26 #include "cleankey.h"
27 #include "ll.h"
28 #include "log.h"
29 #include "onak-conf.h"
30
31 #ifdef DBINIT
32 extern struct onak_dbctx *DBINIT(struct onak_db_config *dbcfg, bool readonly);
33 #endif
34
35 /*
36  *      config - Runtime configuration for onak.
37  *
38  *      This is the default config; normally overridden with values from the
39  *      config file.
40  */
41 struct onak_config config = {
42         .maxkeys = 128,
43         .thissite = NULL,
44         .adminemail = NULL,
45         .mta = NULL,
46         .syncsites = NULL,
47         .logfile = NULL,
48
49         .use_keyd = false,
50         .sock_dir = NULL,
51
52         .backends = NULL,
53         .backends_dir = NULL,
54
55 #ifdef DBINIT
56         .dbinit = DBINIT,
57 #else
58         .dbinit = NULL,
59 #endif
60
61         .clean_policies = ONAK_CLEAN_CHECK_SIGHASH,
62
63         .bin_dir = NULL,
64         .mail_dir = NULL,
65 };
66
67 struct onak_db_config *find_db_backend_config(struct ll *backends, char *name)
68 {
69         struct ll *cur;
70
71         cur = backends;
72         while (cur != NULL && strcmp(name,
73                         ((struct onak_db_config *) cur->object)->name)) {
74                 cur = cur->next;
75         }
76
77         return (cur != NULL) ? (struct onak_db_config *) cur->object : NULL;
78 }
79
80 bool parsebool(char *str, bool fallback)
81 {
82         if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
83                         !strcasecmp(str, "0")) {
84                 return false;
85         } else if (!strcasecmp(str, "true") || !strcasecmp(str, "yes") ||
86                         !strcasecmp(str, "1")) {
87                 return true;
88         } else {
89                 logthing(LOGTHING_CRITICAL,
90                         "Couldn't parse %s as a boolean config variable, "
91                         "returning fallback of '%s'.",
92                         str,
93                         fallback ? "true" : "false");
94                 return fallback;
95         }
96 }
97
98 /*
99  * Parse an old pksd style config line, as used in onak 0.4.6 and earlier.
100  */
101 static bool parseoldconfigline(char *line)
102 {
103         if (line[0] == '#' || line[0] == 0) {
104                 /*
105                  * Comment line, ignore.
106                  */
107         } else if (!strncmp("db_dir ", line, 7)) {
108                 config.backend->location = strdup(&line[7]);
109         } else if (!strncmp("debug ", line, 6)) {
110                 /*
111                  * Not supported yet; ignore for compatibility with
112                  * pksd.
113                  */
114         } else if (!strncmp("default_language ", line, 17)) {
115                 /*
116                  * Not supported yet; ignore for compatibility with
117                  * pksd.
118                  */
119         } else if (!strncmp("mail_delivery_client ", line, 21)) {
120                 config.mta = strdup(&line[21]);
121         } else if (!strncmp("maintainer_email ", line, 17)) {
122                 config.adminemail = strdup(&line[17]);
123         } else if (!strncmp("mail_intro_file ", line, 16)) {
124                 /*
125                  * Not supported yet; ignore for compatibility with
126                  * pksd.
127                  */
128         } else if (!strncmp("help_dir ", line, 9)) {
129                 /*
130                  * Not supported yet; ignore for compatibility with
131                  * pksd.
132                  */
133         } else if (!strncmp("max_last ", line, 9)) {
134                 /*
135                  * Not supported yet; ignore for compatibility with
136                  * pksd.
137                  */
138         } else if (!strncmp("max_reply_keys ", line, 15)) {
139                 config.maxkeys = atoi(&line[15]);
140         } else if (!strncmp("pg_dbhost ", line, 10)) {
141                 config.backend->hostname = strdup(&line[10]);
142         } else if (!strncmp("pg_dbname ", line, 10)) {
143                 config.backend->location = strdup(&line[10]);
144         } else if (!strncmp("pg_dbuser ", line, 10)) {
145                 config.backend->username = strdup(&line[10]);
146         } else if (!strncmp("pg_dbpass ", line, 10)) {
147                 config.backend->password = strdup(&line[10]);
148         } else if (!strncmp("syncsite ", line, 9)) {
149                 config.syncsites =
150                         lladd(config.syncsites, strdup(&line[9]));
151         } else if (!strncmp("logfile ", line, 8)) {
152                 config.logfile = strdup(&line[8]);
153         } else if (!strncmp("loglevel ", line, 9)) {
154                 setlogthreshold(atoi(&line[9]));
155         } else if (!strncmp("this_site ", line, 10)) {
156                 config.thissite = strdup(&line[10]);
157         } else if (!strncmp("socket_name ", line, 12) ||
158                         !strncmp("www_port ", line, 9)) {
159                 /*
160                  * Not applicable; ignored for compatibility with pksd.
161                  */
162         } else if (!strncmp("pks_bin_dir ", line, 12)) {
163                 config.bin_dir = strdup(&line[12]);
164         } else if (!strncmp("mail_dir ", line, 9)) {
165                 config.mail_dir = strdup(&line[9]);
166         } else if (!strncmp("db_backend ", line, 11)) {
167                 config.backend->type = strdup(&line[11]);
168                 config.backend->name = strdup(&line[11]);
169                 config.db_backend = strdup(&line[11]);
170         } else if (!strncmp("backends_dir ", line, 13)) {
171                 config.backends_dir = strdup(&line[13]);
172         } else if (!strncmp("use_keyd ", line, 9)) {
173                 config.use_keyd = parsebool(&line[9],
174                                         config.use_keyd);
175         } else if (!strncmp("sock_dir ", line, 9)) {
176                 config.sock_dir = strdup(&line[9]);
177         } else if (!strncmp("check_sighash ", line, 9)) {
178                 if (parsebool(&line[9], config.clean_policies &
179                                         ONAK_CLEAN_CHECK_SIGHASH)) {
180                         config.clean_policies |=
181                                 ONAK_CLEAN_CHECK_SIGHASH;
182                 } else {
183                         config.clean_policies &=
184                                 ~ONAK_CLEAN_CHECK_SIGHASH;
185                 }
186         } else {
187                 return false;
188         }
189
190         return true;
191 }
192
193 /*
194  * Parse a new style .ini config line, supporting [sections] and name=value
195  * format.
196  */
197 static bool parseconfigline(char *line)
198 {
199         /* Yes, this means we're not re-entrant. */
200         static char section[32] = "";
201         struct onak_db_config *backend;
202         size_t len;
203         char *name, *value;
204
205         if (line[0] == '#' || line[0] == ';' ||
206                         line[0] == 0) {
207                 /*
208                  * Comment line, ignore.
209                  */
210         } else if (line[0] == '[') {
211                 /* Section name */
212                 len = strlen(line);
213                 if (line[len - 1] != ']') {
214                         logthing(LOGTHING_CRITICAL,
215                                 "Malformed section header '%s' in "
216                                 "config file.", line);
217                         return false;
218                 }
219                 if (len > sizeof(section)) {
220                         logthing(LOGTHING_CRITICAL,
221                                 "Section header '%s' is too long in "
222                                 "config file.", line);
223                         return false;
224                 }
225                 line[len - 1] = 0;
226                 strncpy(section, &line[1], len);
227         } else if ((value = strchr(line, '=')) != NULL) {
228                 name = line;
229                 *value++ = 0;
230
231                 /* We can have multiple backend: sections */
232                 if (!strncmp(section, "backend:", 8)) {
233                         backend = find_db_backend_config(
234                                 config.backends, &section[8]);
235                         if (backend == NULL) {
236                                 backend = calloc(1,
237                                         sizeof(struct onak_db_config));
238                                 backend->name = strdup(&section[8]);
239                                 config.backends = lladd(config.backends,
240                                                         backend);
241                         }
242
243                         if (!strcmp(name, "type")) {
244                                 backend->type = strdup(value);
245                         } else if (!strcmp(name, "location")) {
246                                 backend->location = strdup(value);
247                         } else if (!strcmp(name, "hostname")) {
248                                 backend->location = strdup(value);
249                         } else if (!strcmp(name, "username")) {
250                                 backend->location = strdup(value);
251                         } else if (!strcmp(name, "password")) {
252                                 backend->location = strdup(value);
253                         }
254
255 #define MATCH(s, n) !strcmp(section, s) && !strcmp(name, n)
256                 /* [main] section */
257                 } else if (MATCH("main", "backend")) {
258                         config.db_backend = strdup(value);
259                 } else if (MATCH("main", "backends_dir")) {
260                         config.backends_dir = strdup(value);
261                 } else if (MATCH("main", "logfile")) {
262                         config.logfile = strdup(value);
263                 } else if (MATCH("main", "loglevel")) {
264                         setlogthreshold(atoi(value));
265                 } else if (MATCH("main", "use_keyd")) {
266                         config.use_keyd = parsebool(value,
267                                         config.use_keyd);
268                 } else if (MATCH("main", "sock_dir")) {
269                         config.sock_dir = strdup(value);
270                 } else if (MATCH("main", "max_reply_keys")) {
271                         config.maxkeys = atoi(value);
272                 /* [mail] section */
273                 } else if (MATCH("mail", "maintainer_email")) {
274                         config.adminemail = strdup(value);
275                 } else if (MATCH("mail", "mail_dir")) {
276                 config.mail_dir = strdup(value);
277                 } else if (MATCH("mail", "mta")) {
278                         config.mta = strdup(value);
279                 } else if (MATCH("mail", "bin_dir")) {
280                         config.bin_dir = strdup(value);
281                 } else if (MATCH("mail", "this_site")) {
282                         config.thissite = strdup(value);
283                 } else if (MATCH("mail", "syncsite")) {
284                         config.syncsites = lladd(config.syncsites,
285                                 strdup(value));
286                 /* [verification] section */
287                 } else if (MATCH("verification", "check_sighash")) {
288                         if (parsebool(value, config.clean_policies &
289                                         ONAK_CLEAN_CHECK_SIGHASH)) {
290                                 config.clean_policies |=
291                                         ONAK_CLEAN_CHECK_SIGHASH;
292                         } else {
293                                 config.clean_policies &=
294                                         ~ONAK_CLEAN_CHECK_SIGHASH;
295                         }
296                 } else if (MATCH("verification", "check_packet_size")) {
297                         if (parsebool(value, config.clean_policies &
298                                         ONAK_CLEAN_LARGE_PACKETS)) {
299                                 config.clean_policies |=
300                                         ONAK_CLEAN_LARGE_PACKETS;
301                         } else {
302                                 config.clean_policies &=
303                                         ~ONAK_CLEAN_LARGE_PACKETS;
304                         }
305                 } else {
306                         return false;
307                 }
308         } else {
309                 return false;
310         }
311
312         return true;
313 }
314
315 void readconfig(const char *configfile) {
316         FILE *conffile;
317         char  curline[1024];
318         int   i;
319         char *dir, *conf;
320         size_t len;
321         struct onak_db_config *backend;
322         bool oldstyle = false;
323         bool res;
324
325         curline[1023] = 0;
326
327         /*
328          * Try to find a config file to use. If one is explicitly provided,
329          * use that. Otherwise look in $XDG_CONFIG_HOME, $HOME and finally
330          * the build in configuration directory. We try an old style onak.conf
331          * first and then the new style onak.ini if that wasn't found - this
332          * is to prevent breaking existing installs on upgrade.
333          */
334         if (configfile == NULL) {
335                 conffile = NULL;
336                 if ((dir = getenv("XDG_CONFIG_HOME")) != NULL) {
337                         /* dir + / + onak.conf + NUL */
338                         len = strlen(dir) + 1 + 9 + 1;
339                         conf = malloc(len);
340                         snprintf(conf, len, "%s/onak.conf", dir);
341                         conffile = fopen(conf, "r");
342                         if (conffile == NULL) {
343                                 /* Conveniently .ini is shorter than .conf */
344                                 snprintf(conf, len, "%s/onak.ini", dir);
345                                 conffile = fopen(conf, "r");
346                         } else {
347                                 oldstyle = true;
348                         }
349                         free(conf);
350                 } else if ((dir = getenv("HOME")) != NULL) {
351                         /* dir + /.config/onak.conf + NUL */
352                         len = strlen(dir) + 18 + 1;
353                         conf = malloc(len);
354                         snprintf(conf, len, "%s/.config/onak.conf", dir);
355                         conffile = fopen(conf, "r");
356                         if (conffile == NULL) {
357                                 /* Conveniently .ini is shorter than .conf */
358                                 snprintf(conf, len, "%s/onak.ini", dir);
359                                 conffile = fopen(conf, "r");
360                         } else {
361                                 oldstyle = true;
362                         }
363                         free(conf);
364                 }
365                 if (conffile == NULL) {
366                         conffile = fopen(CONFIGDIR "/onak.conf", "r");
367                         if (conffile == NULL) {
368                                 conffile = fopen(CONFIGDIR "/onak.ini", "r");
369                         } else {
370                                 oldstyle = true;
371                         }
372                 }
373         } else {
374                 /*
375                  * Explicitly provided config file; if the filename ends .conf
376                  * assume it's old style.
377                  */
378                 len = strlen(configfile);
379                 if (!strcmp(&configfile[len - 5], ".conf")) {
380                         oldstyle = true;
381                 }
382                 conffile = fopen(configfile, "r");
383         }
384
385         if (conffile != NULL) {
386                 if (!fgets(curline, 1023, conffile)) {
387                         logthing(LOGTHING_CRITICAL,
388                                 "Problem reading configuration file.");
389                         fclose(conffile);
390                         return;
391                 }
392
393                 if (oldstyle) {
394                         /* Add a single DB configuration */
395                         backend = calloc(1, sizeof(*backend));
396                         config.backend = backend;
397                         config.backends = lladd(NULL, backend);
398                 }
399
400                 while (!feof(conffile)) {
401                         /* Strip any trailing white space */
402                         for (i = strlen(curline) - 1;
403                                         i >= 0 && isspace(curline[i]);
404                                         i--) {
405                                 curline[i] = 0;
406                         }
407
408                         /* Strip any leading white space */
409                         i = 0;
410                         while (curline[i] != 0 && isspace(curline[i])) {
411                                 i++;
412                         }
413
414                         if (oldstyle) {
415                                 res = parseoldconfigline(&curline[i]);
416                         } else {
417                                 res = parseconfigline(&curline[i]);
418                         }
419                         if (!res) {
420                                 logthing(LOGTHING_ERROR,
421                                         "Unknown config line: %s", curline);
422                         }
423
424                         if (!fgets(curline, 1023, conffile) &&
425                                         !feof(conffile)) {
426                                 logthing(LOGTHING_CRITICAL,
427                                         "Problem reading configuration file.");
428                                 break;
429                         }
430                 }
431                 fclose(conffile);
432
433                 if (config.db_backend == NULL) {
434                         logthing(LOGTHING_CRITICAL,
435                                 "No database backend configured.");
436                 } else if (!oldstyle) {
437                         config.backend = find_db_backend_config(
438                                 config.backends, config.db_backend);
439                         if (config.backend == NULL) {
440                                 logthing(LOGTHING_NOTICE,
441                                         "Couldn't find configuration for %s "
442                                         "backend.", config.db_backend);
443                         }
444                 }
445         } else {
446                 logthing(LOGTHING_NOTICE,
447                                 "Couldn't open config file; using defaults.");
448         }
449 }
450
451 void writeconfig(const char *configfile)
452 {
453         FILE *conffile;
454         struct ll *cur;
455
456         if (configfile) {
457                 conffile = fopen(configfile, "w");
458         } else {
459                 conffile = stdout;
460         }
461
462 #define WRITE_IF_NOT_NULL(c, s) if (c != NULL) { \
463         fprintf(conffile, s "=%s\n", c); \
464 }
465 #define WRITE_BOOL(c, s) fprintf(conffile, s "=%s\n", s ? "true" : "false");
466
467         fprintf(conffile, "[main]\n");
468         WRITE_IF_NOT_NULL(config.backend->name, "backend");
469         WRITE_IF_NOT_NULL(config.backends_dir, "backends_dir");
470         WRITE_IF_NOT_NULL(config.logfile, "logfile");
471         fprintf(conffile, "loglevel=%d\n", getlogthreshold());
472         WRITE_BOOL(config.use_keyd, "use_keyd");
473         WRITE_IF_NOT_NULL(config.sock_dir, "sock_dir");
474         fprintf(conffile, "max_reply_keys=%d\n", config.maxkeys);
475         fprintf(conffile, "\n");
476
477         fprintf(conffile, "[verification]\n");
478         WRITE_BOOL(config.clean_policies & ONAK_CLEAN_CHECK_SIGHASH,
479                         "check_sighash");
480         fprintf(conffile, "\n");
481
482         fprintf(conffile, "[mail]\n");
483         WRITE_IF_NOT_NULL(config.adminemail, "maintainer_email");
484         WRITE_IF_NOT_NULL(config.mail_dir, "mail_dir");
485         WRITE_IF_NOT_NULL(config.mta, "mta");
486         WRITE_IF_NOT_NULL(config.bin_dir, "bin_dir");
487         WRITE_IF_NOT_NULL(config.thissite, "this_site");
488
489         cur = config.syncsites;
490         while (cur != NULL) {
491                 fprintf(conffile, "syncsite=%s\n", (char *) cur->object);
492                 cur = cur->next;
493         }
494
495         cur = config.backends;
496         while (cur != NULL) {
497                 struct onak_db_config *backend =
498                         (struct onak_db_config *) cur->object;
499                 fprintf(conffile, "\n[backend:%s]\n", backend->name);
500                 WRITE_IF_NOT_NULL(backend->type, "type");
501                 WRITE_IF_NOT_NULL(backend->location, "location");
502                 WRITE_IF_NOT_NULL(backend->hostname, "hostname");
503                 WRITE_IF_NOT_NULL(backend->username, "username");
504                 WRITE_IF_NOT_NULL(backend->password, "password");
505                 cur = cur->next;
506         }
507
508         if (configfile) {
509                 fclose(conffile);
510         }
511 }
512
513 void cleanupdbconfig(void *object)
514 {
515         struct onak_db_config *dbconfig = (struct onak_db_config *) object;
516
517         if (dbconfig->name != NULL) {
518                 free(dbconfig->name);
519                 dbconfig->name = NULL;
520         }
521         if (dbconfig->type != NULL) {
522                 free(dbconfig->type);
523                 dbconfig->type = NULL;
524         }
525         if (dbconfig->location != NULL) {
526                 free(dbconfig->location);
527                 dbconfig->location = NULL;
528         }
529         if (dbconfig->hostname != NULL) {
530                 free(dbconfig->hostname);
531                 dbconfig->hostname = NULL;
532         }
533         if (dbconfig->username != NULL) {
534                 free(dbconfig->username);
535                 dbconfig->username = NULL;
536         }
537         if (dbconfig->password != NULL) {
538                 free(dbconfig->password);
539                 dbconfig->password = NULL;
540         }
541
542         free(dbconfig);
543 }
544
545 void cleanupconfig(void) {
546         /* Free any defined DB backend configuration first */
547         llfree(config.backends, cleanupdbconfig);
548         config.backends = NULL;
549
550         if (config.thissite != NULL) {
551                 free(config.thissite);
552                 config.thissite = NULL;
553         }
554         if (config.adminemail != NULL) {
555                 free(config.adminemail);
556                 config.adminemail = NULL;
557         }
558         if (config.mta != NULL) {
559                 free(config.mta);
560                 config.mta = NULL;
561         }
562         if (config.syncsites != NULL) {
563                 llfree(config.syncsites, free);
564                 config.syncsites = NULL;
565         }
566         if (config.logfile != NULL) {
567                 free(config.logfile);
568                 config.logfile = NULL;
569         }
570         if (config.db_backend != NULL) {
571                 free(config.db_backend);
572                 config.db_backend = NULL;
573         }
574         if (config.backends_dir != NULL) {
575                 free(config.backends_dir);
576                 config.backends_dir = NULL;
577         }
578         if (config.sock_dir != NULL) {
579                 free(config.sock_dir);
580                 config.sock_dir = NULL;
581         }
582         if (config.bin_dir != NULL) {
583                 free(config.bin_dir);
584                 config.bin_dir = NULL;
585         }
586         if (config.mail_dir != NULL) {
587                 free(config.mail_dir);
588                 config.mail_dir = NULL;
589         }
590 }