f3357d9d7b1bbc771879d007213a7a8f583e76d2
[citadel.git] / citadel / config.c
1 /*
2  * Read and write the citadel.config file
3  *
4  * Copyright (c) 1987-2021 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 <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <netdb.h>
20 #include <sys/utsname.h>
21 #include <libcitadel.h>
22 #include <assert.h>
23 #include "config.h"
24 #include "ctdl_module.h"
25
26 long config_msgnum = 0;
27 HashList *ctdlconfig = NULL;    // new configuration
28
29
30 void config_warn_if_port_unset(char *key, int default_port)
31 {
32         int p = CtdlGetConfigInt(key);
33         if ((p < -1) || (p == 0) || (p > UINT16_MAX))
34         {
35                 syslog(LOG_ERR, "config: setting %s is not -1 (disabled) or a valid TCP port - setting to default %d", key, default_port);
36                 CtdlSetConfigInt(key, default_port);
37         }
38 }
39
40
41 void config_warn_if_empty(char *key)
42 {
43         if (IsEmptyStr(CtdlGetConfigStr(key)))
44         {
45                 syslog(LOG_ERR, "config: setting %s is empty, but must not - check your config!", key);
46         }
47 }
48
49
50 void validate_config(void) {
51
52         /*
53          * these shouldn't be empty
54          */
55         config_warn_if_empty("c_fqdn");
56         config_warn_if_empty("c_baseroom");
57         config_warn_if_empty("c_aideroom");
58         config_warn_if_empty("c_twitroom");
59         config_warn_if_empty("c_nodename");
60
61         /*
62          * Sanity check for port bindings
63          */
64         config_warn_if_port_unset("c_smtp_port",        25);
65         config_warn_if_port_unset("c_pop3_port",        110);
66         config_warn_if_port_unset("c_imap_port",        143);
67         config_warn_if_port_unset("c_msa_port",         587);
68         config_warn_if_port_unset("c_port_number",      504);
69         config_warn_if_port_unset("c_smtps_port",       465);
70         config_warn_if_port_unset("c_pop3s_port",       995);
71         config_warn_if_port_unset("c_imaps_port",       993);
72         config_warn_if_port_unset("c_pftcpdict_port",   -1);
73         config_warn_if_port_unset("c_xmpp_c2s_port",    5222);
74         config_warn_if_port_unset("c_xmpp_s2s_port",    5269);
75         config_warn_if_port_unset("c_nntp_port",        119);
76         config_warn_if_port_unset("c_nntps_port",       563);
77
78         if (getpwuid(ctdluid) == NULL) {
79                 syslog(LOG_ERR, "config: uid (%d) does not exist ... citserver will run as root", ctdluid);
80         }
81 }
82
83
84 /*
85  * Put some sane default values into our configuration.  Some will be overridden when we run setup.
86  */
87 void brand_new_installation_set_defaults(void) {
88
89         struct utsname my_utsname;
90         struct hostent *he;
91         char detected_hostname[256];
92
93         /* Determine our host name, in case we need to use it as a default */
94         uname(&my_utsname);
95
96         /* set some sample/default values in place of blanks... */
97         extract_token(detected_hostname, my_utsname.nodename, 0, '.', sizeof detected_hostname);
98         CtdlSetConfigStr("c_nodename", detected_hostname);
99
100         if ((he = gethostbyname(my_utsname.nodename)) != NULL) {
101                 CtdlSetConfigStr("c_fqdn", he->h_name);
102         }
103         else {
104                 CtdlSetConfigStr("c_fqdn", my_utsname.nodename);
105         }
106
107         CtdlSetConfigStr("c_humannode",         "Citadel Server");
108         CtdlSetConfigInt("c_initax",            4);
109         CtdlSetConfigStr("c_moreprompt",        "<more>");
110         CtdlSetConfigStr("c_twitroom",          "Trashcan");
111         CtdlSetConfigStr("c_baseroom",          BASEROOM);
112         CtdlSetConfigStr("c_aideroom",          "Aide");
113         CtdlSetConfigInt("c_sleeping",          900);
114
115         if (CtdlGetConfigInt("c_createax") == 0) {
116                 CtdlSetConfigInt("c_createax", 3);
117         }
118
119         /*
120          * Default port numbers for various services
121          */
122         CtdlSetConfigInt("c_port_number",       504);
123         CtdlSetConfigInt("c_smtp_port",         25);
124         CtdlSetConfigInt("c_pop3_port",         110);
125         CtdlSetConfigInt("c_imap_port",         143);
126         CtdlSetConfigInt("c_msa_port",          587);
127         CtdlSetConfigInt("c_smtps_port",        465);
128         CtdlSetConfigInt("c_pop3s_port",        995);
129         CtdlSetConfigInt("c_imaps_port",        993);
130         CtdlSetConfigInt("c_pftcpdict_port",    -1);
131         CtdlSetConfigInt("c_xmpp_c2s_port",     5222);
132         CtdlSetConfigInt("c_xmpp_s2s_port",     5269);
133         CtdlSetConfigInt("c_nntp_port",         119);
134         CtdlSetConfigInt("c_nntps_port",        563);
135
136         /*
137          * Prevent the "new installation, set defaults" behavior from occurring again
138          */
139         CtdlSetConfigLong("c_config_created_or_migrated", (long)time(NULL));
140 }
141
142
143 /*
144  * Migrate a supplied legacy configuration to the new in-db format.
145  * No individual site should ever have to do this more than once.
146  */
147 void migrate_legacy_config(struct legacy_config *lconfig)
148 {
149         CtdlSetConfigStr(       "c_nodename"            ,       lconfig->c_nodename             );
150         CtdlSetConfigStr(       "c_fqdn"                ,       lconfig->c_fqdn                 );
151         CtdlSetConfigStr(       "c_humannode"           ,       lconfig->c_humannode            );
152         CtdlSetConfigInt(       "c_creataide"           ,       lconfig->c_creataide            );
153         CtdlSetConfigInt(       "c_sleeping"            ,       lconfig->c_sleeping             );
154         CtdlSetConfigInt(       "c_initax"              ,       lconfig->c_initax               );
155         CtdlSetConfigInt(       "c_regiscall"           ,       lconfig->c_regiscall            );
156         CtdlSetConfigInt(       "c_twitdetect"          ,       lconfig->c_twitdetect           );
157         CtdlSetConfigStr(       "c_twitroom"            ,       lconfig->c_twitroom             );
158         CtdlSetConfigStr(       "c_moreprompt"          ,       lconfig->c_moreprompt           );
159         CtdlSetConfigInt(       "c_restrict"            ,       lconfig->c_restrict             );
160         CtdlSetConfigStr(       "c_site_location"       ,       lconfig->c_site_location        );
161         CtdlSetConfigStr(       "c_sysadm"              ,       lconfig->c_sysadm               );
162         CtdlSetConfigInt(       "c_maxsessions"         ,       lconfig->c_maxsessions          );
163         CtdlSetConfigStr(       "c_ip_addr"             ,       lconfig->c_ip_addr              );
164         CtdlSetConfigInt(       "c_port_number"         ,       lconfig->c_port_number          );
165         CtdlSetConfigInt(       "c_ep_mode"             ,       lconfig->c_ep.expire_mode       );
166         CtdlSetConfigInt(       "c_ep_value"            ,       lconfig->c_ep.expire_value      );
167         CtdlSetConfigInt(       "c_userpurge"           ,       lconfig->c_userpurge            );
168         CtdlSetConfigInt(       "c_roompurge"           ,       lconfig->c_roompurge            );
169         CtdlSetConfigStr(       "c_logpages"            ,       lconfig->c_logpages             );
170         CtdlSetConfigInt(       "c_createax"            ,       lconfig->c_createax             );
171         CtdlSetConfigLong(      "c_maxmsglen"           ,       lconfig->c_maxmsglen            );
172         CtdlSetConfigInt(       "c_min_workers"         ,       lconfig->c_min_workers          );
173         CtdlSetConfigInt(       "c_max_workers"         ,       lconfig->c_max_workers          );
174         CtdlSetConfigInt(       "c_pop3_port"           ,       lconfig->c_pop3_port            );
175         CtdlSetConfigInt(       "c_smtp_port"           ,       lconfig->c_smtp_port            );
176         CtdlSetConfigInt(       "c_rfc822_strict_from"  ,       lconfig->c_rfc822_strict_from   );
177         CtdlSetConfigInt(       "c_aide_zap"            ,       lconfig->c_aide_zap             );
178         CtdlSetConfigInt(       "c_imap_port"           ,       lconfig->c_imap_port            );
179         CtdlSetConfigLong(      "c_net_freq"            ,       lconfig->c_net_freq             );
180         CtdlSetConfigInt(       "c_disable_newu"        ,       lconfig->c_disable_newu         );
181         CtdlSetConfigInt(       "c_enable_fulltext"     ,       lconfig->c_enable_fulltext      );
182         CtdlSetConfigStr(       "c_baseroom"            ,       lconfig->c_baseroom             );
183         CtdlSetConfigStr(       "c_aideroom"            ,       lconfig->c_aideroom             );
184         CtdlSetConfigInt(       "c_purge_hour"          ,       lconfig->c_purge_hour           );
185         CtdlSetConfigInt(       "c_mbxep_mode"          ,       lconfig->c_mbxep.expire_mode    );
186         CtdlSetConfigInt(       "c_mbxep_value"         ,       lconfig->c_mbxep.expire_value   );
187         CtdlSetConfigStr(       "c_ldap_host"           ,       lconfig->c_ldap_host            );
188         CtdlSetConfigInt(       "c_ldap_port"           ,       lconfig->c_ldap_port            );
189         CtdlSetConfigStr(       "c_ldap_base_dn"        ,       lconfig->c_ldap_base_dn         );
190         CtdlSetConfigStr(       "c_ldap_bind_dn"        ,       lconfig->c_ldap_bind_dn         );
191         CtdlSetConfigStr(       "c_ldap_bind_pw"        ,       lconfig->c_ldap_bind_pw         );
192         CtdlSetConfigInt(       "c_msa_port"            ,       lconfig->c_msa_port             );
193         CtdlSetConfigInt(       "c_imaps_port"          ,       lconfig->c_imaps_port           );
194         CtdlSetConfigInt(       "c_pop3s_port"          ,       lconfig->c_pop3s_port           );
195         CtdlSetConfigInt(       "c_smtps_port"          ,       lconfig->c_smtps_port           );
196         CtdlSetConfigInt(       "c_auto_cull"           ,       lconfig->c_auto_cull            );
197         CtdlSetConfigInt(       "c_allow_spoofing"      ,       lconfig->c_allow_spoofing       );
198         CtdlSetConfigInt(       "c_journal_email"       ,       lconfig->c_journal_email        );
199         CtdlSetConfigInt(       "c_journal_pubmsgs"     ,       lconfig->c_journal_pubmsgs      );
200         CtdlSetConfigStr(       "c_journal_dest"        ,       lconfig->c_journal_dest         );
201         CtdlSetConfigStr(       "c_default_cal_zone"    ,       lconfig->c_default_cal_zone     );
202         CtdlSetConfigInt(       "c_pftcpdict_port"      ,       lconfig->c_pftcpdict_port       );
203         CtdlSetConfigInt(       "c_auth_mode"           ,       lconfig->c_auth_mode            );
204         CtdlSetConfigInt(       "c_rbl_at_greeting"     ,       lconfig->c_rbl_at_greeting      );
205         CtdlSetConfigStr(       "c_pager_program"       ,       lconfig->c_pager_program        );
206         CtdlSetConfigInt(       "c_imap_keep_from"      ,       lconfig->c_imap_keep_from       );
207         CtdlSetConfigInt(       "c_xmpp_c2s_port"       ,       lconfig->c_xmpp_c2s_port        );
208         CtdlSetConfigInt(       "c_xmpp_s2s_port"       ,       lconfig->c_xmpp_s2s_port        );
209         CtdlSetConfigLong(      "c_pop3_fetch"          ,       lconfig->c_pop3_fetch           );
210         CtdlSetConfigLong(      "c_pop3_fastest"        ,       lconfig->c_pop3_fastest         );
211         CtdlSetConfigInt(       "c_spam_flag_only"      ,       lconfig->c_spam_flag_only       );
212         CtdlSetConfigInt(       "c_guest_logins"        ,       lconfig->c_guest_logins         );
213         CtdlSetConfigInt(       "c_nntp_port"           ,       lconfig->c_nntp_port            );
214         CtdlSetConfigInt(       "c_nntps_port"          ,       lconfig->c_nntps_port           );
215 }
216
217
218 /*
219  * Called during the initialization of Citadel server.
220  * It verifies the system's integrity and reads citadel.config into memory.
221  */
222 void initialize_config_system(void) {
223         FILE *cfp;
224         int rv;
225         struct legacy_config lconfig;   // legacy configuration
226         ctdlconfig = NewHash(1, NULL);  // set up the real config system
227
228         /* Ensure that we are linked to the correct version of libcitadel */
229         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
230                 fprintf(stderr, "You are running libcitadel version %d\n", libcitadel_version_number());
231                 fprintf(stderr, "citserver was compiled against version %d\n", LIBCITADEL_VERSION_NUMBER);
232                 exit(CTDLEXIT_LIBCITADEL);
233         }
234
235         memset(&lconfig, 0, sizeof(struct legacy_config));
236         cfp = fopen(file_citadel_config, "rb");
237         if (cfp != NULL) {
238                 if (CtdlGetConfigLong("c_config_created_or_migrated") > 0) {
239                         fprintf(stderr, "Citadel Server found BOTH legacy and new configurations present.\n");
240                         fprintf(stderr, "Exiting to prevent data corruption.\n");
241                         exit(CTDLEXIT_CONFIG);
242                 }
243                 rv = fread((char *) &lconfig, sizeof(struct legacy_config), 1, cfp);
244                 if (rv != 1)
245                 {
246                         fprintf(stderr, 
247                                 "Warning: Found a legacy config file %s has unexpected size. \n",
248                                 file_citadel_config
249                         );
250                 }
251
252                 migrate_legacy_config(&lconfig);
253
254                 fclose(cfp);
255                 if (unlink(file_citadel_config) != 0) {
256                         fprintf(stderr, "Unable to remove legacy config file %s after migrating it.\n", file_citadel_config);
257                         fprintf(stderr, "Exiting to prevent data corruption.\n");
258                         exit(CTDLEXIT_CONFIG);
259                 }
260
261                 /*
262                  * Prevent migration/initialization from happening again.
263                  */
264                 CtdlSetConfigLong("c_config_created_or_migrated", (long)time(NULL));
265
266         }
267
268         /* New installation?  Set up configuration */
269         if (CtdlGetConfigLong("c_config_created_or_migrated") <= 0) {
270                 brand_new_installation_set_defaults();
271         }
272
273         /* Only allow LDAP auth mode if we actually have LDAP support */
274 #ifndef HAVE_LDAP
275         if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP) || (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_LDAP_AD)) {
276                 fprintf(stderr, "Your system is configured for LDAP authentication,\n"
277                                 "but you are running a server built without OpenLDAP support.\n");
278                 exit(CTDL_EXIT_UNSUP_AUTH);
279         }
280 #endif
281
282         /* Default maximum message length is 10 megabytes.  This is site
283          * configurable.  Also check to make sure the limit has not been
284          * set below 8192 bytes.
285          */
286         if (CtdlGetConfigLong("c_maxmsglen") <= 0)      CtdlSetConfigLong("c_maxmsglen", 10485760);
287         if (CtdlGetConfigLong("c_maxmsglen") < 8192)    CtdlSetConfigLong("c_maxmsglen", 8192);
288
289         /*
290          * Default lower and upper limits on number of worker threads
291          */
292         if (CtdlGetConfigInt("c_min_workers") < 5)      CtdlSetConfigInt("c_min_workers", 5);   // min
293         if (CtdlGetConfigInt("c_max_workers") == 0)     CtdlSetConfigInt("c_max_workers", 256); // default max
294         if (CtdlGetConfigInt("c_max_workers") < CtdlGetConfigInt("c_min_workers")) {
295                 CtdlSetConfigInt("c_max_workers", CtdlGetConfigInt("c_min_workers"));           // max >= min
296         }
297
298         /* Networking more than once every five minutes just isn't sane */
299         if (CtdlGetConfigLong("c_net_freq") == 0)       CtdlSetConfigLong("c_net_freq", 3600);  // once per hour default
300         if (CtdlGetConfigLong("c_net_freq") < 300)      CtdlSetConfigLong("c_net_freq", 300);   // minimum 5 minutes
301
302         /* Same goes for POP3 */
303         if (CtdlGetConfigLong("c_pop3_fetch") == 0)     CtdlSetConfigLong("c_pop3_fetch", 3600);        // once per hour default
304         if (CtdlGetConfigLong("c_pop3_fetch") < 300)    CtdlSetConfigLong("c_pop3_fetch", 300);         // 5 minutes min
305         if (CtdlGetConfigLong("c_pop3_fastest") == 0)   CtdlSetConfigLong("c_pop3_fastest", 3600);      // once per hour default
306         if (CtdlGetConfigLong("c_pop3_fastest") < 300)  CtdlSetConfigLong("c_pop3_fastest", 300);       // 5 minutes min
307
308         /* LDAP sync frequency */
309         if (CtdlGetConfigLong("c_ldap_sync_freq") == 0) CtdlSetConfigLong("c_ldap_sync_freq", 300);     // every 5 minutes default
310
311         /* "create new user" only works with native authentication mode */
312         if (CtdlGetConfigInt("c_auth_mode") != AUTHMODE_NATIVE) {
313                 CtdlSetConfigInt("c_disable_newu", 1);
314         }
315 }
316
317
318 /*
319  * Called when Citadel server is shutting down.
320  * Clears out the config hash table.
321  */
322 void shutdown_config_system(void) 
323 {
324         DeleteHash(&ctdlconfig);
325 }
326
327
328 /*
329  * Set a system config value.  Simple key/value here.
330  */
331 void CtdlSetConfigStr(char *key, char *value)
332 {
333         int key_len = strlen(key);
334         int value_len = strlen(value);
335
336         /* Save it in memory */
337         Put(ctdlconfig, key, key_len, strdup(value), NULL);
338
339         /* Also write it to the config database */
340
341         int dbv_size = key_len + value_len + 2;
342         char *dbv = malloc(dbv_size);
343         strcpy(dbv, key);
344         strcpy(&dbv[key_len + 1], value);
345         cdb_store(CDB_CONFIG, key, key_len, dbv, dbv_size);
346         free(dbv);
347 }
348
349
350 /*
351  * Set a numeric system config value (long integer)
352  */
353 void CtdlSetConfigLong(char *key, long value)
354 {
355         char longstr[256];
356         sprintf(longstr, "%ld", value);
357         CtdlSetConfigStr(key, longstr);
358 }
359
360
361 /*
362  * Set a numeric system config value (integer)
363  */
364 void CtdlSetConfigInt(char *key, int value)
365 {
366         char intstr[256];
367         sprintf(intstr, "%d", value);
368         CtdlSetConfigStr(key, intstr);
369 }
370
371
372 /*
373  * Delete a system config value.
374  */
375 void CtdlDelConfig(char *key)
376 {
377         int key_len = strlen(key);
378
379         if (IsEmptyStr(key)) return;
380
381         /* Delete from the database. */
382         cdb_delete(CDB_CONFIG, key, key_len);
383
384         /* Delete from the in-memory cache */
385         HashPos *Pos = GetNewHashPos(ctdlconfig, 1);
386         if (GetHashPosFromKey(ctdlconfig, key, key_len, Pos)) {
387                 DeleteEntryFromHash(ctdlconfig, Pos);
388         }
389         DeleteHashPos(&Pos);
390
391         assert(Pos == NULL);    // no memory leaks allowed
392 }
393
394
395 /*
396  * Fetch a system config value.  Caller does *not* own the returned value and may not alter it.
397  */
398 char *CtdlGetConfigStr(char *key)
399 {
400         char *value = NULL;
401         struct cdbdata *cdb;
402         int key_len = strlen(key);
403
404         if (IsEmptyStr(key)) return(NULL);
405
406         /* First look in memory */
407         if (GetHash(ctdlconfig, key, key_len, (void *)&value))
408         {
409                 return value;
410         }
411
412         /* Then look in the database. */
413
414         cdb = cdb_fetch(CDB_CONFIG, key, key_len);
415
416         if (cdb == NULL) {      /* nope, not there either. */
417                 return(NULL);
418         }
419
420         /* Got it.  Save it in memory for the next fetch. */
421         value = strdup(cdb->ptr + key_len + 1);         /* The key was stored there too; skip past it */
422         cdb_free(cdb);
423         Put(ctdlconfig, key, key_len, value, NULL);
424         return value;
425 }
426
427
428 /*
429  * Fetch a system config value - integer
430  */
431 int CtdlGetConfigInt(char *key)
432 {
433         char *s = CtdlGetConfigStr(key);
434         if (s) return atoi(s);
435         return 0;
436 }
437
438
439 /*
440  * Fetch a system config value - long integer
441  */
442 long CtdlGetConfigLong(char *key)
443 {
444         char *s = CtdlGetConfigStr(key);
445         if (s) return atol(s);
446         return 0;
447 }
448
449
450 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
451         config_msgnum = msgnum;
452 }
453
454
455 char *CtdlGetSysConfig(char *sysconfname) {
456         char hold_rm[ROOMNAMELEN];
457         long msgnum;
458         char *conf;
459         struct CtdlMessage *msg;
460         char buf[SIZ];
461         
462         strcpy(hold_rm, CC->room.QRname);
463         if (CtdlGetRoom(&CC->room, SYSCONFIGROOM) != 0) {
464                 CtdlGetRoom(&CC->room, hold_rm);
465                 return NULL;
466         }
467
468         /* We want the last (and probably only) config in this room */
469         begin_critical_section(S_CONFIG);
470         config_msgnum = (-1L);
471         CtdlForEachMessage(MSGS_LAST, 1, NULL, sysconfname, NULL, CtdlGetSysConfigBackend, NULL);
472         msgnum = config_msgnum;
473         end_critical_section(S_CONFIG);
474
475         if (msgnum < 0L) {
476                 conf = NULL;
477         }
478         else {
479                 msg = CtdlFetchMessage(msgnum, 1);
480                 if (msg != NULL) {
481                         conf = strdup(msg->cm_fields[eMesageText]);
482                         CM_Free(msg);
483                 }
484                 else {
485                         conf = NULL;
486                 }
487         }
488
489         CtdlGetRoom(&CC->room, hold_rm);
490
491         if (conf != NULL) {
492                 do {
493                         extract_token(buf, conf, 0, '\n', sizeof buf);
494                         strcpy(conf, &conf[strlen(buf)+1]);
495                 } while ( (!IsEmptyStr(conf)) && (!IsEmptyStr(buf)) );
496         }
497
498         return(conf);
499 }
500
501
502 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
503         CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 1, 0);
504 }