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