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