]> git.sommitrealweird.co.uk Git - onak.git/blob - onak-conf.c
5f07e02426e25b22594e6eb2f7da74263fe6dc31
[onak.git] / onak-conf.c
1 /*
2  * onak-conf.c - Routines related to runtime config.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include "config.h"
10
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "ll.h"
17 #include "log.h"
18 #include "onak-conf.h"
19
20 extern struct dbfuncs DBFUNCS;
21
22 /*
23  *      config - Runtime configuration for onak.
24  *
25  *      This is the default config; normally overridden with values from the
26  *      config file.
27  */
28 struct onak_config config = {
29         128,                    /* maxkeys */
30         NULL,                   /* thissite */
31         NULL,                   /* adminemail */
32         NULL,                   /* mta */
33         NULL,                   /* syncsites */
34         NULL,                   /* logfile */
35
36         false,                  /* use_keyd */
37
38         /*
39          * Options for directory backends.
40          */
41         NULL,                   /* db_dir */
42
43         /*
44          * Options for the Postgres backend.
45          */
46         NULL,                   /* pg_dbhost */
47         NULL,                   /* pg_dbname */
48         NULL,                   /* pg_dbuser */
49         NULL,                   /* pg_dbpass */
50
51         /*
52          * Options for dynamic backends.
53          */
54         NULL,                   /* db_backend */
55         NULL,                   /* backends_dir */
56
57         &DBFUNCS,               /* Default dbfuncs struct */
58 };
59
60 bool parsebool(char *str, bool fallback)
61 {
62         if (!strcasecmp(str, "false") || !strcasecmp(str, "no") ||
63                         !strcasecmp(str, "0")) {
64                 return false;
65         } else if (!strcasecmp(str, "true") || !strcasecmp(str, "yes") ||
66                         !strcasecmp(str, "1")) {
67                 return true;
68         } else {
69                 logthing(LOGTHING_CRITICAL,
70                         "Couldn't parse %s as a boolean config variable, "
71                         "returning fallback of '%s'.",
72                         str,
73                         fallback ? "true" : "false");
74                 return fallback;
75         }
76 }
77
78 void readconfig(const char *configfile) {
79         FILE *conffile;
80         char  curline[1024];
81         int   i;
82
83         curline[1023] = 0;
84         if (configfile == NULL) {
85                 conffile = fopen(CONFIGFILE, "r");
86         } else {
87                 conffile = fopen(configfile, "r");
88         }
89         if (conffile != NULL) {
90                 fgets(curline, 1023, conffile);
91
92                 while (!feof(conffile)) {
93                         for (i = strlen(curline) - 1;
94                                         i >= 0 && isspace(curline[i]);
95                                         i--) {
96                                 curline[i] = 0;
97                         }
98
99                 if (curline[0] == '#' || curline[0] == 0) {
100                         /*
101                          * Comment line, ignore.
102                          */
103                 } else if (!strncmp("db_dir ", curline, 7)) {
104                         config.db_dir = strdup(&curline[7]);
105                 } else if (!strncmp("debug ", curline, 6)) {
106                         /*
107                          * Not supported yet; ignore for compatibility with
108                          * pksd.
109                          */
110                 } else if (!strncmp("default_language ", curline, 17)) {
111                         /*
112                          * Not supported yet; ignore for compatibility with
113                          * pksd.
114                          */
115                 } else if (!strncmp("mail_delivery_client ", curline, 21)) {
116                         config.mta = strdup(&curline[21]);
117                 } else if (!strncmp("maintainer_email ", curline, 17)) {
118                         config.adminemail = strdup(&curline[17]);
119                 } else if (!strncmp("mail_intro_file ", curline, 16)) {
120                         /*
121                          * Not supported yet; ignore for compatibility with
122                          * pksd.
123                          */
124                 } else if (!strncmp("help_dir ", curline, 9)) {
125                         /*
126                          * Not supported yet; ignore for compatibility with
127                          * pksd.
128                          */
129                 } else if (!strncmp("max_last ", curline, 9)) {
130                         /*
131                          * Not supported yet; ignore for compatibility with
132                          * pksd.
133                          */
134                 } else if (!strncmp("max_reply_keys ", curline, 15)) {
135                         config.maxkeys = atoi(&curline[15]);
136                 } else if (!strncmp("pg_dbhost ", curline, 10)) {
137                         config.pg_dbhost = strdup(&curline[10]);
138                 } else if (!strncmp("pg_dbname ", curline, 10)) {
139                         config.pg_dbname = strdup(&curline[10]);
140                 } else if (!strncmp("pg_dbuser ", curline, 10)) {
141                         config.pg_dbuser = strdup(&curline[10]);
142                 } else if (!strncmp("pg_dbpass ", curline, 10)) {
143                         config.pg_dbpass = strdup(&curline[10]);
144                 } else if (!strncmp("syncsite ", curline, 9)) {
145                         config.syncsites =
146                                 lladd(config.syncsites, strdup(&curline[9]));
147                 } else if (!strncmp("logfile ", curline, 8)) {
148                         config.logfile = strdup(&curline[8]);
149                 } else if (!strncmp("loglevel ", curline, 9)) {
150                         setlogthreshold(atoi(&curline[9]));
151                 } else if (!strncmp("this_site ", curline, 10)) {
152                         config.thissite = strdup(&curline[10]);
153                 } else if (!strncmp("socket_name ", curline, 12) ||
154                                 !strncmp("pks_bin_dir ", curline, 12) ||
155                                 !strncmp("mail_dir ", curline, 9) ||
156                                 !strncmp("www_port ", curline, 9)) {
157                         /*
158                          * Not applicable; ignored for compatibility with pksd.
159                          */
160                 } else if (!strncmp("db_backend ", curline, 11)) {
161                         config.db_backend = strdup(&curline[11]);
162                 } else if (!strncmp("backends_dir ", curline, 13)) {
163                         config.backends_dir = strdup(&curline[13]);
164                 } else if (!strncmp("use_keyd ", curline, 9)) {
165                         config.use_keyd = parsebool(&curline[9],
166                                                 config.use_keyd);
167                 } else {
168                         logthing(LOGTHING_ERROR,
169                                 "Unknown config line: %s", curline);
170                 }
171
172                         fgets(curline, 1023, conffile);
173                 }
174                 fclose(conffile);
175         } else {
176                 logthing(LOGTHING_NOTICE,
177                                 "Couldn't open config file; using defaults.");
178         }
179 }
180
181 void cleanupconfig(void) {
182         if (config.thissite != NULL) {
183                 free(config.thissite);
184                 config.thissite = NULL;
185         }
186         if (config.adminemail != NULL) {
187                 free(config.adminemail);
188                 config.adminemail = NULL;
189         }
190         if (config.mta != NULL) {
191                 free(config.mta);
192                 config.mta = NULL;
193         }
194         if (config.db_dir != NULL) {
195                 free(config.db_dir);
196                 config.db_dir = NULL;
197         }
198         if (config.pg_dbhost != NULL) {
199                 free(config.pg_dbhost);
200                 config.pg_dbhost = NULL;
201         }
202         if (config.pg_dbname != NULL) {
203                 free(config.pg_dbname);
204                 config.pg_dbname = NULL;
205         }
206         if (config.pg_dbuser != NULL) {
207                 free(config.pg_dbuser);
208                 config.pg_dbuser = NULL;
209         }
210         if (config.pg_dbpass != NULL) {
211                 free(config.pg_dbpass);
212                 config.pg_dbpass = NULL;
213         }
214         if (config.syncsites != NULL) {
215                 llfree(config.syncsites, free);
216                 config.syncsites = NULL;
217         }
218         if (config.logfile != NULL) {
219                 free(config.logfile);
220                 config.logfile = NULL;
221         }
222         if (config.db_backend != NULL) {
223                 free(config.db_backend);
224                 config.db_backend = NULL;
225         }
226         if (config.backends_dir != NULL) {
227                 free(config.backends_dir);
228                 config.backends_dir = NULL;
229         }
230 }