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