93a9f10669382c09f4d7712c16852eb07086fb4c
[citadel.git] / citadel / config.c
1 /*
2  * Read and write the citadel.config file
3  *
4  * Copyright (c) 1987-2015 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "sysdep.h"
16 #include <stdio.h>
17 #include <sys/utsname.h>
18 #include <libcitadel.h>
19 #include "config.h"
20 #include "ctdl_module.h"
21
22 struct config config;           // legacy configuration
23 HashList *ctdlconfig = NULL;    // new configuration
24
25 #define STR_NOT_EMPTY(CFG_FIELDNAME) if (IsEmptyStr(config.CFG_FIELDNAME)) \
26                 syslog(LOG_EMERG, "configuration setting "#CFG_FIELDNAME" is empty, but must not - check your config!");
27
28 #define TEST_PORT(CFG_PORT, DEFAULTPORT)                        \
29         if ((config.CFG_PORT < -1) ||           \
30             (config.CFG_PORT == 0) ||           \
31             (config.CFG_PORT > UINT16_MAX))     \
32                 syslog(LOG_EMERG, "configuration setting "#CFG_PORT" is not -1 (disabled) or a valid TCP-Port - check your config! Default setting is: "#DEFAULTPORT);
33                         
34
35 void validate_config(void) {
36 /* these shouldn't be empty: */
37         STR_NOT_EMPTY(c_fqdn);
38
39         STR_NOT_EMPTY(c_baseroom);
40         STR_NOT_EMPTY(c_aideroom);
41         STR_NOT_EMPTY(c_twitroom);
42         STR_NOT_EMPTY(c_nodename);
43         STR_NOT_EMPTY(c_default_cal_zone);
44
45 /* we bind a lot of ports: */
46         TEST_PORT(c_smtp_port, 25);
47         TEST_PORT(c_pop3_port, 110);
48         TEST_PORT(c_imap_port, 143);
49         TEST_PORT(c_msa_port, 587);
50         TEST_PORT(c_port_number, 504);
51         TEST_PORT(c_smtps_port, 465);
52         TEST_PORT(c_pop3s_port, 995);
53         TEST_PORT(c_imaps_port, 993);
54         TEST_PORT(c_pftcpdict_port, -1);
55         TEST_PORT(c_managesieve_port, 2020);
56         TEST_PORT(c_xmpp_c2s_port, 5222);
57         TEST_PORT(c_xmpp_s2s_port, 5269);
58         TEST_PORT(c_nntp_port, 119);
59         TEST_PORT(c_nntps_port, 563);
60
61         if (getpwuid(ctdluid) == NULL) {
62                 syslog(LOG_EMERG, "The UID (%d) citadel is configured to use is not defined in your system (/etc/passwd?)!", ctdluid);
63         }
64         
65 }
66
67 /*
68  * Put some sane default values into our configuration.  Some will be overridden when we run setup.
69  */
70 void brand_new_installation_set_defaults(void) {
71
72         struct utsname my_utsname;
73         struct hostent *he;
74
75         /* Determine our host name, in case we need to use it as a default */
76         uname(&my_utsname);
77
78         /* set some sample/default values in place of blanks... */
79         extract_token(config.c_nodename, my_utsname.nodename, 0, '.', sizeof config.c_nodename);
80         if (IsEmptyStr(config.c_fqdn) ) {
81                 if ((he = gethostbyname(my_utsname.nodename)) != NULL) {
82                         safestrncpy(config.c_fqdn, he->h_name, sizeof config.c_fqdn);
83                 }
84                 else {
85                         safestrncpy(config.c_fqdn, my_utsname.nodename, sizeof config.c_fqdn);
86                 }
87         }
88
89         safestrncpy(config.c_humannode, "Citadel Server", sizeof config.c_humannode);
90         safestrncpy(config.c_phonenum, "US 800 555 1212", sizeof config.c_phonenum);
91         config.c_initax = 4;
92         safestrncpy(config.c_moreprompt, "<more>", sizeof config.c_moreprompt);
93         safestrncpy(config.c_twitroom, "Trashcan", sizeof config.c_twitroom);
94         safestrncpy(config.c_baseroom, BASEROOM, sizeof config.c_baseroom);
95         safestrncpy(config.c_aideroom, "Aide", sizeof config.c_aideroom);
96         config.c_port_number = 504;
97         config.c_sleeping = 900;
98
99         if (config.c_createax == 0) {
100                 config.c_createax = 3;
101         }
102
103         /*
104          * Default port numbers for various services
105          */
106         config.c_smtp_port = 25;
107         config.c_pop3_port = 110;
108         config.c_imap_port = 143;
109         config.c_msa_port = 587;
110         config.c_smtps_port = 465;
111         config.c_pop3s_port = 995;
112         config.c_imaps_port = 993;
113         config.c_pftcpdict_port = -1 ;
114         config.c_managesieve_port = 2020;
115         config.c_xmpp_c2s_port = 5222;
116         config.c_xmpp_s2s_port = 5269;
117         config.c_nntp_port = 119;
118         config.c_nntps_port = 563;
119 }
120
121
122
123 /*
124  * Called during the initialization of Citadel server.
125  * It verifies the system's integrity and reads citadel.config into memory.
126  */
127 void initialize_config_system(void) {
128         FILE *cfp;
129         int rv;
130         ctdlconfig = NewHash(1, NULL);
131
132         if (chdir(ctdl_bbsbase_dir) != 0) {
133                 fprintf(stderr,
134                         "This program could not be started.\nUnable to change directory to %s\nError: %s\n",
135                         ctdl_bbsbase_dir,
136                         strerror(errno)
137                 );
138                 exit(CTDLEXIT_HOME);
139         }
140
141         memset(&config, 0, sizeof(struct config));
142         cfp = fopen(file_citadel_config, "rb");
143         if (cfp != NULL) {
144                 rv = fread((char *) &config, sizeof(struct config), 1, cfp);
145                 if (rv != 1)
146                 {
147                         fprintf(stderr, 
148                                 "Warning: The config file %s has unexpected size. \n",
149                                 file_citadel_config
150                         );
151                 }
152                 fclose(cfp);
153         }
154         else {
155                 brand_new_installation_set_defaults();
156         }
157
158         /* Ensure that we are linked to the correct version of libcitadel */
159         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
160                 fprintf(stderr, "    You are running libcitadel version %d.%02d\n",
161                         (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
162                 fprintf(stderr, "citserver was compiled against version %d.%02d\n",
163                         (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
164                 exit(CTDLEXIT_LIBCITADEL);
165         }
166
167         /* Only allow LDAP auth mode if we actually have LDAP support */
168 #ifndef HAVE_LDAP
169         if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
170                 fprintf(stderr, "Your system is configured for LDAP authentication,\n"
171                                 "but you are running a server built without OpenLDAP support.\n");
172                 exit(CTDL_EXIT_UNSUP_AUTH);
173         }
174 #endif
175
176         /* Default maximum message length is 10 megabytes.  This is site
177          * configurable.  Also check to make sure the limit has not been
178          * set below 8192 bytes.
179          */
180         if (config.c_maxmsglen <= 0)
181                 config.c_maxmsglen = 10485760;
182         if (config.c_maxmsglen < 8192)
183                 config.c_maxmsglen = 8192;
184
185         /* Default lower and upper limits on number of worker threads */
186
187         if (config.c_min_workers < 3)           /* no less than 3 */
188                 config.c_min_workers = 5;
189
190         if (config.c_max_workers == 0)                  /* default maximum */
191                 config.c_max_workers = 256;
192
193         if (config.c_max_workers < config.c_min_workers)   /* max >= min */
194                 config.c_max_workers = config.c_min_workers;
195
196         /* Networking more than once every five minutes just isn't sane */
197         if (config.c_net_freq == 0L)
198                 config.c_net_freq = 3600L;      /* once per hour default */
199         if (config.c_net_freq < 300L) 
200                 config.c_net_freq = 300L;
201
202         /* Same goes for POP3 */
203         if (config.c_pop3_fetch == 0L)
204                 config.c_pop3_fetch = 3600L;    /* once per hour default */
205         if (config.c_pop3_fetch < 300L) 
206                 config.c_pop3_fetch = 300L;
207         if (config.c_pop3_fastest == 0L)
208                 config.c_pop3_fastest = 3600L;  /* once per hour default */
209         if (config.c_pop3_fastest < 300L) 
210                 config.c_pop3_fastest = 300L;
211
212         /* "create new user" only works with native authentication mode */
213         if (config.c_auth_mode != AUTHMODE_NATIVE) {
214                 config.c_disable_newu = 1;
215         }
216 }
217
218 long config_msgnum = 0;
219
220 /*
221  * Occasionally, we will need to write the config file, because some operations
222  * change site-wide parameters.
223  */
224 void put_config(void)
225 {
226         FILE *cfp;
227         int blocks_written = 0;
228
229         cfp = fopen(file_citadel_config, "w");
230         if (cfp != NULL) {
231                 blocks_written = fwrite((char *) &config, sizeof(struct config), 1, cfp);
232                 if (blocks_written == 1) {
233                         chown(file_citadel_config, CTDLUID, (-1));
234                         chmod(file_citadel_config, 0600);
235                         fclose(cfp);
236                         return;
237                 }
238                 fclose(cfp);
239         }
240         syslog(LOG_EMERG, "%s: %s", file_citadel_config, strerror(errno));
241 }
242
243
244
245 /*
246  * Called when Citadel server is shutting down.
247  * Clears out the config hash table.
248  */
249 void shutdown_config_system(void) 
250 {
251         DeleteHash(&ctdlconfig);
252 }
253
254
255
256 /*
257  * Set a system config value.  Simple key/value here.
258  */
259 void CtdlSetConfigStr(char *key, char *value)
260 {
261         int key_len = strlen(key);
262         int value_len = strlen(value);
263
264         /* Save it in memory */
265         Put(ctdlconfig, key, key_len, strdup(value), NULL);
266
267         /* Also write it to the config database */
268
269         int dbv_size = key_len + value_len + 2;
270         char *dbv = malloc(dbv_size);
271         strcpy(dbv, key);
272         strcpy(&dbv[key_len + 1], value);
273         cdb_store(CDB_CONFIG, key, key_len, dbv, dbv_size);
274         free(dbv);
275 }
276
277
278 /*
279  * Set a numeric system config value (long integer)
280  */
281 void CtdlSetConfigLong(char *key, long value)
282 {
283         char longstr[256];
284         sprintf(longstr, "%ld", value);
285         CtdlSetConfigStr(key, longstr);
286 }
287
288
289 /*
290  * Set a numeric system config value (integer)
291  */
292 void CtdlSetConfigInt(char *key, int value)
293 {
294         char intstr[256];
295         sprintf(intstr, "%d", value);
296         CtdlSetConfigStr(key, intstr);
297 }
298
299
300 /*
301  * Fetch a system config value.  Caller does *not* own the returned value and may not alter it.
302  */
303 char *CtdlGetConfigStr(char *key)
304 {
305         char *value = NULL;
306         struct cdbdata *cdb;
307         int key_len = strlen(key);
308
309         /* First look in memory */
310         if (GetHash(ctdlconfig, key, key_len, (void *)&value))
311         {
312                 return value;
313         }
314
315         /* Then look in the database. */
316
317         cdb = cdb_fetch(CDB_CONFIG, key, key_len);
318
319         if (cdb == NULL) {      /* nope, not there either. */
320                 return(NULL);
321         }
322
323         /* Got it.  Save it in memory for the next fetch. */
324         value = strdup(cdb->ptr + key_len + 1);         /* The key was stored there too; skip past it */
325         cdb_free(cdb);
326         Put(ctdlconfig, key, key_len, value, NULL);
327         return value;
328 }
329
330
331
332
333
334 /**********************************************************************/
335
336
337
338
339
340
341
342
343
344
345 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
346         config_msgnum = msgnum;
347 }
348
349
350 char *CtdlGetSysConfig(char *sysconfname) {
351         char hold_rm[ROOMNAMELEN];
352         long msgnum;
353         char *conf;
354         struct CtdlMessage *msg;
355         char buf[SIZ];
356         
357         strcpy(hold_rm, CC->room.QRname);
358         if (CtdlGetRoom(&CC->room, SYSCONFIGROOM) != 0) {
359                 CtdlGetRoom(&CC->room, hold_rm);
360                 return NULL;
361         }
362
363
364         /* We want the last (and probably only) config in this room */
365         begin_critical_section(S_CONFIG);
366         config_msgnum = (-1L);
367         CtdlForEachMessage(MSGS_LAST, 1, NULL, sysconfname, NULL,
368                            CtdlGetSysConfigBackend, NULL);
369         msgnum = config_msgnum;
370         end_critical_section(S_CONFIG);
371
372         if (msgnum < 0L) {
373                 conf = NULL;
374         }
375         else {
376                 msg = CtdlFetchMessage(msgnum, 1);
377                 if (msg != NULL) {
378                         conf = strdup(msg->cm_fields[eMesageText]);
379                         CM_Free(msg);
380                 }
381                 else {
382                         conf = NULL;
383                 }
384         }
385
386         CtdlGetRoom(&CC->room, hold_rm);
387
388         if (conf != NULL) do {
389                         extract_token(buf, conf, 0, '\n', sizeof buf);
390                         strcpy(conf, &conf[strlen(buf)+1]);
391                 } while ( (!IsEmptyStr(conf)) && (!IsEmptyStr(buf)) );
392
393         return(conf);
394 }
395
396
397 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
398         CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 1, 0);
399 }
400