]> git.sommitrealweird.co.uk Git - onak.git/blob - onak-conf.c
6d72e6fd4e7e3d53ac86ab30760c45ffb7020d04
[onak.git] / onak-conf.c
1 /*
2  * onak-conf.c - Routines related to runtime config.
3  *
4  * Copyright 2002 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 dbfuncs DBFUNCS;
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         &DBFUNCS,               /* Default dbfuncs struct */
69 };
70
71 bool parsebool(char *str, bool fallback)
72 {
73         if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
74                         !strcasecmp(str, "0")) {
75                 return false;
76         } else if (!strcasecmp(str, "true") || !strcasecmp(str, "yes") ||
77                         !strcasecmp(str, "1")) {
78                 return true;
79         } else {
80                 logthing(LOGTHING_CRITICAL,
81                         "Couldn't parse %s as a boolean config variable, "
82                         "returning fallback of '%s'.",
83                         str,
84                         fallback ? "true" : "false");
85                 return fallback;
86         }
87 }
88
89 void readconfig(const char *configfile) {
90         FILE *conffile;
91         char  curline[1024];
92         int   i;
93
94         curline[1023] = 0;
95         if (configfile == NULL) {
96                 conffile = fopen(CONFIGFILE, "r");
97         } else {
98                 conffile = fopen(configfile, "r");
99         }
100         if (conffile != NULL) {
101                 fgets(curline, 1023, conffile);
102
103                 while (!feof(conffile)) {
104                         for (i = strlen(curline) - 1;
105                                         i >= 0 && isspace(curline[i]);
106                                         i--) {
107                                 curline[i] = 0;
108                         }
109
110                 if (curline[0] == '#' || curline[0] == 0) {
111                         /*
112                          * Comment line, ignore.
113                          */
114                 } else if (!strncmp("db_dir ", curline, 7)) {
115                         config.db_dir = strdup(&curline[7]);
116                 } else if (!strncmp("debug ", curline, 6)) {
117                         /*
118                          * Not supported yet; ignore for compatibility with
119                          * pksd.
120                          */
121                 } else if (!strncmp("default_language ", curline, 17)) {
122                         /*
123                          * Not supported yet; ignore for compatibility with
124                          * pksd.
125                          */
126                 } else if (!strncmp("mail_delivery_client ", curline, 21)) {
127                         config.mta = strdup(&curline[21]);
128                 } else if (!strncmp("maintainer_email ", curline, 17)) {
129                         config.adminemail = strdup(&curline[17]);
130                 } else if (!strncmp("mail_intro_file ", curline, 16)) {
131                         /*
132                          * Not supported yet; ignore for compatibility with
133                          * pksd.
134                          */
135                 } else if (!strncmp("help_dir ", curline, 9)) {
136                         /*
137                          * Not supported yet; ignore for compatibility with
138                          * pksd.
139                          */
140                 } else if (!strncmp("max_last ", curline, 9)) {
141                         /*
142                          * Not supported yet; ignore for compatibility with
143                          * pksd.
144                          */
145                 } else if (!strncmp("max_reply_keys ", curline, 15)) {
146                         config.maxkeys = atoi(&curline[15]);
147                 } else if (!strncmp("pg_dbhost ", curline, 10)) {
148                         config.pg_dbhost = strdup(&curline[10]);
149                 } else if (!strncmp("pg_dbname ", curline, 10)) {
150                         config.pg_dbname = strdup(&curline[10]);
151                 } else if (!strncmp("pg_dbuser ", curline, 10)) {
152                         config.pg_dbuser = strdup(&curline[10]);
153                 } else if (!strncmp("pg_dbpass ", curline, 10)) {
154                         config.pg_dbpass = strdup(&curline[10]);
155                 } else if (!strncmp("syncsite ", curline, 9)) {
156                         config.syncsites =
157                                 lladd(config.syncsites, strdup(&curline[9]));
158                 } else if (!strncmp("logfile ", curline, 8)) {
159                         config.logfile = strdup(&curline[8]);
160                 } else if (!strncmp("loglevel ", curline, 9)) {
161                         setlogthreshold(atoi(&curline[9]));
162                 } else if (!strncmp("this_site ", curline, 10)) {
163                         config.thissite = strdup(&curline[10]);
164                 } else if (!strncmp("socket_name ", curline, 12) ||
165                                 !strncmp("pks_bin_dir ", curline, 12) ||
166                                 !strncmp("mail_dir ", curline, 9) ||
167                                 !strncmp("www_port ", curline, 9)) {
168                         /*
169                          * Not applicable; ignored for compatibility with pksd.
170                          */
171                 } else if (!strncmp("db_backend ", curline, 11)) {
172                         config.db_backend = strdup(&curline[11]);
173                 } else if (!strncmp("backends_dir ", curline, 13)) {
174                         config.backends_dir = strdup(&curline[13]);
175                 } else if (!strncmp("use_keyd ", curline, 9)) {
176                         config.use_keyd = parsebool(&curline[9],
177                                                 config.use_keyd);
178                 } else {
179                         logthing(LOGTHING_ERROR,
180                                 "Unknown config line: %s", curline);
181                 }
182
183                         fgets(curline, 1023, conffile);
184                 }
185                 fclose(conffile);
186         } else {
187                 logthing(LOGTHING_NOTICE,
188                                 "Couldn't open config file; using defaults.");
189         }
190 }
191
192 void cleanupconfig(void) {
193         if (config.thissite != NULL) {
194                 free(config.thissite);
195                 config.thissite = NULL;
196         }
197         if (config.adminemail != NULL) {
198                 free(config.adminemail);
199                 config.adminemail = NULL;
200         }
201         if (config.mta != NULL) {
202                 free(config.mta);
203                 config.mta = NULL;
204         }
205         if (config.db_dir != NULL) {
206                 free(config.db_dir);
207                 config.db_dir = NULL;
208         }
209         if (config.pg_dbhost != NULL) {
210                 free(config.pg_dbhost);
211                 config.pg_dbhost = NULL;
212         }
213         if (config.pg_dbname != NULL) {
214                 free(config.pg_dbname);
215                 config.pg_dbname = NULL;
216         }
217         if (config.pg_dbuser != NULL) {
218                 free(config.pg_dbuser);
219                 config.pg_dbuser = NULL;
220         }
221         if (config.pg_dbpass != NULL) {
222                 free(config.pg_dbpass);
223                 config.pg_dbpass = NULL;
224         }
225         if (config.syncsites != NULL) {
226                 llfree(config.syncsites, free);
227                 config.syncsites = NULL;
228         }
229         if (config.logfile != NULL) {
230                 free(config.logfile);
231                 config.logfile = NULL;
232         }
233         if (config.db_backend != NULL) {
234                 free(config.db_backend);
235                 config.db_backend = NULL;
236         }
237         if (config.backends_dir != NULL) {
238                 free(config.backends_dir);
239                 config.backends_dir = NULL;
240         }
241 }