2 // This module handles states which are global to the entire server.
4 // Copyright (c) 1987-2021 by the citadel.org team
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.
12 #include <libcitadel.h>
14 #include "ctdl_module.h"
16 #include "citserver.h"
19 long control_highest_user = 0;
22 * This is the legacy "control record" format for the message base. If found
23 * on disk, its contents will be migrated into the system configuration. Never
26 struct legacy_ctrl_format {
27 long MMhighest; /* highest message number in file */
28 unsigned MMflags; /* Global system flags */
29 long MMnextuser; /* highest user number on system */
30 long MMnextroom; /* highest room number on system */
31 int MM_hosted_upgrade_level; /* Server-hosted upgrade level */
32 int MM_fulltext_wordbreaker; /* ID of wordbreaker in use */
33 long MMfulltext; /* highest message number indexed */
34 int MMdbversion; /* Version of Berkeley DB used on previous server run */
39 * data that gets passed back and forth between control_find_highest() and its caller
42 long highest_roomnum_found;
43 long highest_msgnum_found;
48 * Callback to get highest room number when rebuilding message base metadata
50 * sanity_diag_mode (can be set by -s flag at startup) may be:
51 * 0 = attempt to fix inconsistencies
52 * 1 = show inconsistencies but don't repair them, exit after complete
53 * 2 = show inconsistencies but don't repair them, continue execution
55 void control_find_highest(struct ctdlroom *qrbuf, void *data) {
56 struct cfh *cfh = (struct cfh *)data;
57 struct cdbdata *cdbfr;
62 if (qrbuf->QRnumber > cfh->highest_roomnum_found) {
63 cfh->highest_roomnum_found = qrbuf->QRnumber;
66 /* Load the message list */
67 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
69 msglist = (long *) cdbfr->ptr;
70 num_msgs = cdbfr->len / sizeof(long);
73 return; /* No messages at all? No further action. */
77 for (c=0; c<num_msgs; c++) {
78 if (msglist[c] > cfh->highest_msgnum_found) {
79 cfh->highest_msgnum_found = msglist[c];
89 * Callback to get highest user number.
91 void control_find_user(char *username, void *out_data) {
92 struct ctdluser EachUser;
94 if (CtdlGetUser(&EachUser, username) != 0) {
98 if (EachUser.usernum > CtdlGetConfigLong("MMnextuser")) {
99 syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld , found in %s",
100 EachUser.usernum, CtdlGetConfigLong("MMnextuser"), EachUser.fullname
102 if (!sanity_diag_mode) {
103 CtdlSetConfigLong("MMnextuser", EachUser.usernum);
110 * If we have a legacy format control record on disk, import it.
112 void migrate_legacy_control_record(void) {
114 struct legacy_ctrl_format c;
115 memset(&c, 0, sizeof(c));
117 fp = fopen("citadel.control", "rb+");
119 syslog(LOG_INFO, "control: legacy format record found -- importing to db");
120 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
122 CtdlSetConfigLong( "MMhighest", c.MMhighest);
123 CtdlSetConfigInt( "MMflags", c.MMflags);
124 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
125 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
126 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
127 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
128 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
131 if (unlink("citadel.control") != 0) {
132 fprintf(stderr, "Unable to remove legacy control record after migrating it.\n");
133 fprintf(stderr, "Exiting to prevent data corruption.\n");
134 exit(CTDLEXIT_CONFIG);
141 * check_control - check the control record has sensible values for message, user and room numbers
143 void check_control(void) {
145 syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
147 memset(&cfh, 0, sizeof(struct cfh));
148 CtdlForEachRoom(control_find_highest, &cfh);
150 if (cfh.highest_roomnum_found > CtdlGetConfigLong("MMnextroom")) {
151 syslog(LOG_DEBUG, "control: fixing MMnextroom %ld > %ld", cfh.highest_roomnum_found, CtdlGetConfigLong("MMnextroom"));
152 if (!sanity_diag_mode) {
153 CtdlSetConfigLong("MMnextroom", cfh.highest_roomnum_found);
157 if (cfh.highest_msgnum_found > CtdlGetConfigLong("MMhighest")) {
158 syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld", cfh.highest_msgnum_found, CtdlGetConfigLong("MMhighest"));
159 if (!sanity_diag_mode) {
160 CtdlSetConfigLong("MMhighest", cfh.highest_msgnum_found);
164 syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
165 ForEachUser(control_find_user, NULL);
167 syslog(LOG_INFO, "control: sanity checks complete");
169 if (sanity_diag_mode == 1) {
170 syslog(LOG_INFO, "control: sanity check diagnostic mode is active - exiting now");
177 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
179 long get_new_message_number(void)
182 begin_critical_section(S_CONTROL);
183 retval = CtdlGetConfigLong("MMhighest");
185 CtdlSetConfigLong("MMhighest", retval);
186 end_critical_section(S_CONTROL);
192 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
193 * This provides a quick way to initialise a variable that might be used to indicate
194 * messages that should not be processed. For example, an inbox rules script will use this
195 * to record determine that messages older than this should not be processed.
197 * (Why is this function here? Can't we just go straight to the config variable it fetches?)
199 long CtdlGetCurrentMessageNumber(void)
201 return CtdlGetConfigLong("MMhighest");
206 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
208 long get_new_user_number(void)
211 begin_critical_section(S_CONTROL);
212 retval = CtdlGetConfigLong("MMnextuser");
214 CtdlSetConfigLong("MMnextuser", retval);
215 end_critical_section(S_CONTROL);
221 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
223 long get_new_room_number(void)
226 begin_critical_section(S_CONTROL);
227 retval = CtdlGetConfigLong("MMnextroom");
229 CtdlSetConfigLong("MMnextroom", retval);
230 end_critical_section(S_CONTROL);
236 * Helper function for cmd_conf() to handle boolean values
238 int confbool(char *v)
240 if (IsEmptyStr(v)) return(0);
241 if (atoi(v) != 0) return(1);
247 * Get or set global configuration options
249 void cmd_conf(char *argbuf)
258 if (CtdlAccessCheck(ac_aide)) return;
260 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
262 // CONF GET - retrieve system configuration in legacy format
263 // This is deprecated; please do not add fields or change their order.
264 if (!strcasecmp(cmd, "GET")) {
265 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
266 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
267 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
268 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
269 cprintf("xxx\n"); /* placeholder -- field no longer in use */
270 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
271 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
272 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
273 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
274 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
275 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
276 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
277 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
278 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
279 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
280 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
281 cprintf("xxx\n"); /* placeholder -- field no longer in use */
282 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
283 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
284 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
285 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
286 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
287 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
288 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
289 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
290 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
291 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
292 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
293 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
294 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
295 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
296 cprintf("1\n"); /* niu */
297 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
298 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
299 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
300 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
301 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
302 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
303 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
304 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
305 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
306 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
307 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
308 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
309 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
311 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
312 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
313 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
314 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
315 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
316 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
318 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
323 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
326 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
327 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
328 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
329 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
330 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
331 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
332 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
333 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
334 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
335 cprintf("%d\n", ctdluid);
336 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
337 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
341 // CONF SET - set system configuration in legacy format
342 // This is deprecated; please do not add fields or change their order.
343 else if (!strcasecmp(cmd, "SET")) {
345 cprintf("%d Send configuration...\n", SEND_LISTING);
347 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
350 CtdlSetConfigStr("c_nodename", buf);
353 CtdlSetConfigStr("c_fqdn", buf);
356 CtdlSetConfigStr("c_humannode", buf);
359 /* placeholder -- field no longer in use */
362 CtdlSetConfigInt("c_creataide", confbool(buf));
365 CtdlSetConfigInt("c_sleeping", atoi(buf));
371 CtdlSetConfigInt("c_initax", i);
374 CtdlSetConfigInt("c_regiscall", confbool(buf));
377 CtdlSetConfigInt("c_twitdetect", confbool(buf));
380 CtdlSetConfigStr("c_twitroom", buf);
383 CtdlSetConfigStr("c_moreprompt", buf);
386 CtdlSetConfigInt("c_restrict", confbool(buf));
389 CtdlSetConfigStr("c_site_location", buf);
392 CtdlSetConfigStr("c_sysadm", buf);
397 CtdlSetConfigInt("c_maxsessions", i);
400 /* placeholder -- field no longer in use */
403 CtdlSetConfigInt("c_userpurge", atoi(buf));
406 CtdlSetConfigInt("c_roompurge", atoi(buf));
409 CtdlSetConfigStr("c_logpages", buf);
415 CtdlSetConfigInt("c_createax", i);
420 CtdlSetConfigLong("c_maxmsglen", ii);
425 if (i >= 3) { // minimum value
426 CtdlSetConfigInt("c_min_workers", i);
431 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
432 CtdlSetConfigInt("c_max_workers", i);
436 CtdlSetConfigInt("c_pop3_port", atoi(buf));
439 CtdlSetConfigInt("c_smtp_port", atoi(buf));
442 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
445 CtdlSetConfigInt("c_aide_zap", confbool(buf));
448 CtdlSetConfigInt("c_imap_port", atoi(buf));
451 CtdlSetConfigLong("c_net_freq", atol(buf));
454 CtdlSetConfigInt("c_disable_newu", confbool(buf));
461 if ((i >= 0) && (i <= 23)) {
462 CtdlSetConfigInt("c_purge_hour", i);
466 CtdlSetConfigStr("c_ldap_host", buf);
469 CtdlSetConfigInt("c_ldap_port", atoi(buf));
472 CtdlSetConfigStr("c_ldap_base_dn", buf);
475 CtdlSetConfigStr("c_ldap_bind_dn", buf);
478 CtdlSetConfigStr("c_ldap_bind_pw", buf);
481 CtdlSetConfigStr("c_ip_addr", buf);
484 CtdlSetConfigInt("c_msa_port", atoi(buf));
487 CtdlSetConfigInt("c_imaps_port", atoi(buf));
490 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
493 CtdlSetConfigInt("c_smtps_port", atoi(buf));
496 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
499 CtdlSetConfigInt("c_auto_cull", confbool(buf));
505 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
508 CtdlSetConfigInt("c_journal_email", confbool(buf));
511 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
514 CtdlSetConfigStr("c_journal_dest", buf);
517 CtdlSetConfigStr("c_default_cal_zone", buf);
520 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
526 CtdlSetConfigInt("c_auth_mode", atoi(buf));
541 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
550 CtdlSetConfigStr("c_pager_program", buf);
553 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
556 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
559 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
562 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
565 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
568 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
571 CtdlSetConfigInt("c_guest_logins", confbool(buf));
574 CtdlSetConfigInt("c_port_number", atoi(buf));
580 CtdlSetConfigInt("c_nntp_port", atoi(buf));
583 CtdlSetConfigInt("c_nntps_port", atoi(buf));
588 snprintf(buf, sizeof buf,
589 "The global system configuration has been edited by %s.\n",
590 (CC->logged_in ? CC->curr_user : "an administrator")
592 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
594 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
595 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
597 /* If full text indexing has been disabled, invalidate the
598 * index so it doesn't try to use it later.
600 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
601 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
605 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
606 else if (!strcasecmp(cmd, "GETSYS")) {
607 extract_token(confname, argbuf, 1, '|', sizeof confname);
608 confptr = CtdlGetSysConfig(confname);
609 if (confptr != NULL) {
612 len = strlen(confptr);
613 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
614 client_write(confptr, len);
615 if ((len > 0) && (confptr[len - 1] != 10))
616 client_write("\n", 1);
620 cprintf("%d No such configuration.\n",
621 ERROR + ILLEGAL_VALUE);
625 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
626 else if (!strcasecmp(cmd, "PUTSYS")) {
627 extract_token(confname, argbuf, 1, '|', sizeof confname);
629 cprintf("%d %s\n", SEND_LISTING, confname);
630 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
631 CtdlPutSysConfig(confname, confptr);
635 // CONF GETVAL - retrieve configuration variables from the database
636 // CONF LOADVAL - same thing but can handle variables bigger than 1 KB (deprecated and probably safe to remove)
637 else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
638 extract_token(confname, argbuf, 1, '|', sizeof confname);
639 char *v = CtdlGetConfigStr(confname);
640 if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
641 cprintf("%d %s|\n", CIT_OK, v);
643 else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
644 cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
645 client_write(v, strlen(v));
648 cprintf("%d |\n", ERROR);
652 // CONF PUTVAL - store configuration variables in the database
653 else if (!strcasecmp(cmd, "PUTVAL")) {
654 if (num_tokens(argbuf, '|') < 3) {
655 cprintf("%d name and value required\n", ERROR);
658 extract_token(confname, argbuf, 1, '|', sizeof confname);
659 extract_token(buf, argbuf, 2, '|', sizeof buf);
660 CtdlSetConfigStr(confname, buf);
661 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
665 // CONF STOREVAL - store configuration variables in the database bigger than 1 KB (deprecated and probably safe to remove)
666 else if (!strcasecmp(cmd, "STOREVAL")) {
667 if (num_tokens(argbuf, '|') < 3) {
668 cprintf("%d name and length required\n", ERROR);
671 extract_token(confname, argbuf, 1, '|', sizeof confname);
672 int bytes = extract_int(argbuf, 2);
673 char *valbuf = malloc(bytes + 1);
674 cprintf("%d %d\n", SEND_BINARY, bytes);
675 client_read(valbuf, bytes);
677 CtdlSetConfigStr(confname, valbuf);
682 // CONF LISTVAL - list configuration variables in the database and their values
683 else if (!strcasecmp(cmd, "LISTVAL")) {
684 struct cdbdata *cdbcfg;
689 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
690 cdb_rewind(CDB_CONFIG);
691 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
692 if (cdbcfg->len < 1020) {
693 keylen = strlen(cdbcfg->ptr);
695 value = cdbcfg->ptr + keylen + 1;
696 cprintf("%s|%s\n", key, value);
704 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
709 typedef struct __ConfType {
714 ConfType CfgNames[] = {
715 { {HKEY("localhost") }, 0},
716 { {HKEY("directory") }, 0},
717 { {HKEY("smarthost") }, 2},
718 { {HKEY("fallbackhost") }, 2},
719 { {HKEY("rbl") }, 3},
720 { {HKEY("spamassassin") }, 3},
721 { {HKEY("masqdomain") }, 1},
722 { {HKEY("clamav") }, 3},
723 { {HKEY("notify") }, 3},
727 HashList *CfgNameHash = NULL;
728 void cmd_gvdn(char *argbuf)
730 const ConfType *pCfg;
732 long min = atol(argbuf);
733 const char *Pos = NULL;
734 const char *PPos = NULL;
745 List = NewHash(1, NULL);
746 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
747 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
750 confptr = CtdlGetSysConfig(INTERNETCFG);
751 Config = NewStrBufPlain(confptr, -1);
754 Line = NewStrBufPlain(NULL, StrLength(Config));
755 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
756 while (StrBufSipLine(Line, Config, &Pos))
759 Cfg = NewStrBufPlain(NULL, StrLength(Line));
761 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
762 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
763 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
766 pCfg = (ConfType *) vptr;
767 if (pCfg->Type <= min)
769 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
775 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
776 It = GetNewHashPos(List, 1);
777 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
788 FreeStrBuf(&CfgToken);
792 /*****************************************************************************/
793 /* MODULE INITIALIZATION STUFF */
794 /*****************************************************************************/
796 CTDL_MODULE_INIT(control)
801 CfgNameHash = NewHash(1, NULL);
802 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
803 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
805 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
806 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
808 /* return our id for the Log */