Add dynamic loading of backends.
[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 /*
21  *      config - Runtime configuration for onak.
22  *
23  *      This is the default config; normally overridden with values from the
24  *      config file.
25  */
26 struct onak_config config = {
27         128,                    /* maxkeys */
28         NULL,                   /* thissite */
29         NULL,                   /* adminemail */
30         NULL,                   /* mta */
31         NULL,                   /* syncsites */
32         NULL,                   /* logfile */
33
34         /*
35          * Options for directory backends.
36          */
37         NULL,                   /* db_dir */
38
39         /*
40          * Options for the Postgres backend.
41          */
42         NULL,                   /* pg_dbhost */
43         NULL,                   /* pg_dbname */
44         NULL,                   /* pg_dbuser */
45         NULL,                   /* pg_dbpass */
46
47         /*
48          * Options for dynamic backends.
49          */
50         NULL,                   /* db_backend */
51         NULL,                   /* backends_dir */
52 };
53
54 void readconfig(const char *configfile) {
55         FILE *conffile;
56         char  curline[1024];
57         int   i;
58
59         curline[1023] = 0;
60         if (configfile == NULL) {
61                 conffile = fopen(CONFIGFILE, "r");
62         } else {
63                 conffile = fopen(configfile, "r");
64         }
65         if (conffile != NULL) {
66                 fgets(curline, 1023, conffile);
67
68                 while (!feof(conffile)) {
69                         for (i = strlen(curline) - 1;
70                                         i >= 0 && isspace(curline[i]);
71                                         i--) {
72                                 curline[i] = 0;
73                         }
74
75                 if (curline[0] == '#' || curline[0] == 0) {
76                         /*
77                          * Comment line, ignore.
78                          */
79                 } else if (!strncmp("db_dir ", curline, 7)) {
80                         config.db_dir = strdup(&curline[7]);
81                 } else if (!strncmp("debug ", curline, 6)) {
82                         /*
83                          * Not supported yet; ignore for compatibility with
84                          * pksd.
85                          */
86                 } else if (!strncmp("default_language ", curline, 17)) {
87                         /*
88                          * Not supported yet; ignore for compatibility with
89                          * pksd.
90                          */
91                 } else if (!strncmp("mail_delivery_client ", curline, 21)) {
92                         config.mta = strdup(&curline[21]);
93                 } else if (!strncmp("maintainer_email ", curline, 17)) {
94                         config.adminemail = strdup(&curline[17]);
95                 } else if (!strncmp("mail_intro_file ", curline, 16)) {
96                         /*
97                          * Not supported yet; ignore for compatibility with
98                          * pksd.
99                          */
100                 } else if (!strncmp("help_dir ", curline, 9)) {
101                         /*
102                          * Not supported yet; ignore for compatibility with
103                          * pksd.
104                          */
105                 } else if (!strncmp("max_last ", curline, 9)) {
106                         /*
107                          * Not supported yet; ignore for compatibility with
108                          * pksd.
109                          */
110                 } else if (!strncmp("max_reply_keys ", curline, 15)) {
111                         config.maxkeys = atoi(&curline[15]);
112                 } else if (!strncmp("pg_dbhost ", curline, 10)) {
113                         config.pg_dbhost = strdup(&curline[10]);
114                 } else if (!strncmp("pg_dbname ", curline, 10)) {
115                         config.pg_dbname = strdup(&curline[10]);
116                 } else if (!strncmp("pg_dbuser ", curline, 10)) {
117                         config.pg_dbuser = strdup(&curline[10]);
118                 } else if (!strncmp("pg_dbpass ", curline, 10)) {
119                         config.pg_dbpass = strdup(&curline[10]);
120                 } else if (!strncmp("syncsite ", curline, 9)) {
121                         config.syncsites =
122                                 lladd(config.syncsites, strdup(&curline[9]));
123                 } else if (!strncmp("logfile ", curline, 8)) {
124                         config.logfile = strdup(&curline[8]);
125                 } else if (!strncmp("loglevel ", curline, 9)) {
126                         setlogthreshold(atoi(&curline[9]));
127                 } else if (!strncmp("this_site ", curline, 10)) {
128                         config.thissite = strdup(&curline[10]);
129                 } else if (!strncmp("socket_name ", curline, 12) ||
130                                 !strncmp("pks_bin_dir ", curline, 12) ||
131                                 !strncmp("mail_dir ", curline, 9) ||
132                                 !strncmp("www_port ", curline, 9)) {
133                         /*
134                          * Not applicable; ignored for compatibility with pksd.
135                          */
136                 } else if (!strncmp("db_backend ", curline, 11)) {
137                         config.db_backend = strdup(&curline[11]);
138                 } else if (!strncmp("backends_dir ", curline, 13)) {
139                         config.backends_dir = strdup(&curline[13]);
140                 } else {
141                         logthing(LOGTHING_ERROR,
142                                 "Unknown config line: %s", curline);
143                 }
144
145                         fgets(curline, 1023, conffile);
146                 }
147                 fclose(conffile);
148         } else {
149                 logthing(LOGTHING_NOTICE,
150                                 "Couldn't open config file; using defaults.");
151         }
152 }
153
154 void cleanupconfig(void) {
155         if (config.thissite != NULL) {
156                 free(config.thissite);
157                 config.thissite = NULL;
158         }
159         if (config.adminemail != NULL) {
160                 free(config.adminemail);
161                 config.adminemail = NULL;
162         }
163         if (config.mta != NULL) {
164                 free(config.mta);
165                 config.mta = NULL;
166         }
167         if (config.db_dir != NULL) {
168                 free(config.db_dir);
169                 config.db_dir = NULL;
170         }
171         if (config.pg_dbhost != NULL) {
172                 free(config.pg_dbhost);
173                 config.pg_dbhost = NULL;
174         }
175         if (config.pg_dbname != NULL) {
176                 free(config.pg_dbname);
177                 config.pg_dbname = NULL;
178         }
179         if (config.pg_dbuser != NULL) {
180                 free(config.pg_dbuser);
181                 config.pg_dbuser = NULL;
182         }
183         if (config.pg_dbpass != NULL) {
184                 free(config.pg_dbpass);
185                 config.pg_dbpass = NULL;
186         }
187         if (config.syncsites != NULL) {
188                 llfree(config.syncsites, free);
189                 config.syncsites = NULL;
190         }
191         if (config.logfile != NULL) {
192                 free(config.logfile);
193                 config.logfile = NULL;
194         }
195         if (config.db_backend != NULL) {
196                 free(config.db_backend);
197                 config.db_backend = NULL;
198         }
199         if (config.backends_dir != NULL) {
200                 free(config.backends_dir);
201                 config.backends_dir = NULL;
202         }
203 }