2 * This module handles states which are global to the entire server.
4 * Copyright (c) 1987-2020 by the citadel.org team
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.
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.
17 #include <libcitadel.h>
19 #include "ctdl_module.h"
21 #include "citserver.h"
24 long control_highest_user = 0;
27 * This is the legacy "control record" format for the message base. If found
28 * on disk, its contents will be migrated into the system configuration. Never
31 struct legacy_ctrl_format {
32 long MMhighest; /* highest message number in file */
33 unsigned MMflags; /* Global system flags */
34 long MMnextuser; /* highest user number on system */
35 long MMnextroom; /* highest room number on system */
36 int MM_hosted_upgrade_level; /* Server-hosted upgrade level */
37 int MM_fulltext_wordbreaker; /* ID of wordbreaker in use */
38 long MMfulltext; /* highest message number indexed */
39 int MMdbversion; /* Version of Berkeley DB used on previous server run */
44 * Callback to get highest room number when rebuilding message base metadata
46 * sanity_diag_mode (can be set by -s flag at startup) may be:
47 * 0 = attempt to fix inconsistencies
48 * 1 = show inconsistencies but don't repair them, exit after complete
49 * 2 = show inconsistencies but don't repair them, continue execution
51 void control_find_highest(struct ctdlroom *qrbuf, void *data)
54 struct cdbdata *cdbfr;
59 if (qrbuf->QRnumber > CtdlGetConfigLong("MMnextroom")) {
60 syslog(LOG_DEBUG, "control: fixing MMnextroom %ld > %ld , found in %s",
61 qrbuf->QRnumber, CtdlGetConfigLong("MMnextroom"), qrbuf->QRname
63 if (!sanity_diag_mode) {
64 CtdlSetConfigLong("MMnextroom", qrbuf->QRnumber);
68 CtdlGetRoom(&room, qrbuf->QRname);
70 /* Load the message list */
71 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
73 msglist = (long *) cdbfr->ptr;
74 num_msgs = cdbfr->len / sizeof(long);
76 return; /* No messages at all? No further action. */
80 for (c=0; c<num_msgs; c++) {
81 if (msglist[c] > CtdlGetConfigLong("MMhighest")) {
82 syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld , found in %s",
83 msglist[c], CtdlGetConfigLong("MMhighest"), qrbuf->QRname
85 if (!sanity_diag_mode) {
86 CtdlSetConfigLong("MMhighest", msglist[c]);
97 * Callback to get highest user number.
99 void control_find_user(char *username, void *out_data) {
100 struct ctdluser EachUser;
102 if (CtdlGetUser(&EachUser, username) != 0) {
106 if (EachUser.usernum > CtdlGetConfigLong("MMnextuser")) {
107 syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld , found in %s",
108 EachUser.usernum, CtdlGetConfigLong("MMnextuser"), EachUser.fullname
110 if (!sanity_diag_mode) {
111 CtdlSetConfigLong("MMnextuser", EachUser.usernum);
118 * If we have a legacy format control record on disk, import it.
120 void migrate_legacy_control_record(void)
123 struct legacy_ctrl_format c;
124 memset(&c, 0, sizeof(c));
126 fp = fopen(file_citadel_control, "rb+");
128 syslog(LOG_INFO, "control: legacy format record found -- importing to db");
129 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
131 CtdlSetConfigLong( "MMhighest", c.MMhighest);
132 CtdlSetConfigInt( "MMflags", c.MMflags);
133 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
134 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
135 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
136 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
137 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
140 if (unlink(file_citadel_control) != 0) {
141 fprintf(stderr, "Unable to remove legacy control record %s after migrating it.\n", file_citadel_control);
142 fprintf(stderr, "Exiting to prevent data corruption.\n");
143 exit(CTDLEXIT_CONFIG);
150 * check_control - check the control record has sensible values for message, user and room numbers
152 void check_control(void)
154 syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
155 CtdlForEachRoom(control_find_highest, NULL);
156 syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
157 ForEachUser(control_find_user, NULL);
158 syslog(LOG_INFO, "control: sanity checks complete");
159 if (sanity_diag_mode == 1) {
160 syslog(LOG_INFO, "control: sanity check diagnostic mode is active - exiting now");
167 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
169 long get_new_message_number(void)
172 begin_critical_section(S_CONTROL);
173 retval = CtdlGetConfigLong("MMhighest");
175 CtdlSetConfigLong("MMhighest", retval);
176 end_critical_section(S_CONTROL);
182 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
183 * This provides a quick way to initialise a variable that might be used to indicate
184 * messages that should not be processed. For example, an inbox rules script will use this
185 * to record determine that messages older than this should not be processed.
187 * (Why is this function here? Can't we just go straight to the config variable it fetches?)
189 long CtdlGetCurrentMessageNumber(void)
191 return CtdlGetConfigLong("MMhighest");
196 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
198 long get_new_user_number(void)
201 begin_critical_section(S_CONTROL);
202 retval = CtdlGetConfigLong("MMnextuser");
204 CtdlSetConfigLong("MMnextuser", retval);
205 end_critical_section(S_CONTROL);
211 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
213 long get_new_room_number(void)
216 begin_critical_section(S_CONTROL);
217 retval = CtdlGetConfigLong("MMnextroom");
219 CtdlSetConfigLong("MMnextroom", retval);
220 end_critical_section(S_CONTROL);
226 * Helper function for cmd_conf() to handle boolean values
228 int confbool(char *v)
230 if (IsEmptyStr(v)) return(0);
231 if (atoi(v) != 0) return(1);
237 * Get or set global configuration options
239 void cmd_conf(char *argbuf)
248 if (CtdlAccessCheck(ac_aide)) return;
250 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
252 // CONF GET - retrieve system configuration in legacy format
253 // This is deprecated; please do not add fields or change their order.
254 if (!strcasecmp(cmd, "GET")) {
255 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
256 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
257 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
258 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
259 cprintf("xxx\n"); /* placeholder -- field no longer in use */
260 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
261 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
262 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
263 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
264 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
265 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
266 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
267 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
268 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
269 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
270 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
271 cprintf("xxx\n"); /* placeholder -- field no longer in use */
272 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
273 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
274 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
275 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
276 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
277 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
278 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
279 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
280 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
281 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
282 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
283 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
284 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
285 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
286 cprintf("1\n"); /* niu */
287 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
289 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
290 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
291 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
292 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
293 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
301 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
302 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
303 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
304 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
305 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
306 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
307 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
309 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
310 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
311 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
312 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
313 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
314 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
316 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
321 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
324 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
325 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
326 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
327 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
328 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
329 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
330 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
331 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
332 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
333 cprintf("%d\n", ctdluid);
334 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
335 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
339 // CONF SET - set system configuration in legacy format
340 // This is deprecated; please do not add fields or change their order.
341 else if (!strcasecmp(cmd, "SET")) {
343 cprintf("%d Send configuration...\n", SEND_LISTING);
345 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
348 CtdlSetConfigStr("c_nodename", buf);
351 CtdlSetConfigStr("c_fqdn", buf);
354 CtdlSetConfigStr("c_humannode", buf);
357 /* placeholder -- field no longer in use */
360 CtdlSetConfigInt("c_creataide", confbool(buf));
363 CtdlSetConfigInt("c_sleeping", atoi(buf));
369 CtdlSetConfigInt("c_initax", i);
372 CtdlSetConfigInt("c_regiscall", confbool(buf));
375 CtdlSetConfigInt("c_twitdetect", confbool(buf));
378 CtdlSetConfigStr("c_twitroom", buf);
381 CtdlSetConfigStr("c_moreprompt", buf);
384 CtdlSetConfigInt("c_restrict", confbool(buf));
387 CtdlSetConfigStr("c_site_location", buf);
390 CtdlSetConfigStr("c_sysadm", buf);
395 CtdlSetConfigInt("c_maxsessions", i);
398 /* placeholder -- field no longer in use */
401 CtdlSetConfigInt("c_userpurge", atoi(buf));
404 CtdlSetConfigInt("c_roompurge", atoi(buf));
407 CtdlSetConfigStr("c_logpages", buf);
413 CtdlSetConfigInt("c_createax", i);
418 CtdlSetConfigLong("c_maxmsglen", ii);
423 if (i >= 3) { // minimum value
424 CtdlSetConfigInt("c_min_workers", i);
429 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
430 CtdlSetConfigInt("c_max_workers", i);
434 CtdlSetConfigInt("c_pop3_port", atoi(buf));
437 CtdlSetConfigInt("c_smtp_port", atoi(buf));
440 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
443 CtdlSetConfigInt("c_aide_zap", confbool(buf));
446 CtdlSetConfigInt("c_imap_port", atoi(buf));
449 CtdlSetConfigLong("c_net_freq", atol(buf));
452 CtdlSetConfigInt("c_disable_newu", confbool(buf));
459 if ((i >= 0) && (i <= 23)) {
460 CtdlSetConfigInt("c_purge_hour", i);
464 CtdlSetConfigStr("c_ldap_host", buf);
467 CtdlSetConfigInt("c_ldap_port", atoi(buf));
470 CtdlSetConfigStr("c_ldap_base_dn", buf);
473 CtdlSetConfigStr("c_ldap_bind_dn", buf);
476 CtdlSetConfigStr("c_ldap_bind_pw", buf);
479 CtdlSetConfigStr("c_ip_addr", buf);
482 CtdlSetConfigInt("c_msa_port", atoi(buf));
485 CtdlSetConfigInt("c_imaps_port", atoi(buf));
488 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
491 CtdlSetConfigInt("c_smtps_port", atoi(buf));
494 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
497 CtdlSetConfigInt("c_auto_cull", confbool(buf));
503 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
506 CtdlSetConfigInt("c_journal_email", confbool(buf));
509 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
512 CtdlSetConfigStr("c_journal_dest", buf);
515 CtdlSetConfigStr("c_default_cal_zone", buf);
518 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
524 CtdlSetConfigInt("c_auth_mode", atoi(buf));
539 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
548 CtdlSetConfigStr("c_pager_program", buf);
551 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
554 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
557 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
560 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
563 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
566 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
569 CtdlSetConfigInt("c_guest_logins", confbool(buf));
572 CtdlSetConfigInt("c_port_number", atoi(buf));
578 CtdlSetConfigInt("c_nntp_port", atoi(buf));
581 CtdlSetConfigInt("c_nntps_port", atoi(buf));
586 snprintf(buf, sizeof buf,
587 "The global system configuration has been edited by %s.\n",
588 (CC->logged_in ? CC->curr_user : "an administrator")
590 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
592 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
593 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
595 /* If full text indexing has been disabled, invalidate the
596 * index so it doesn't try to use it later.
598 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
599 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
603 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
604 else if (!strcasecmp(cmd, "GETSYS")) {
605 extract_token(confname, argbuf, 1, '|', sizeof confname);
606 confptr = CtdlGetSysConfig(confname);
607 if (confptr != NULL) {
610 len = strlen(confptr);
611 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
612 client_write(confptr, len);
613 if ((len > 0) && (confptr[len - 1] != 10))
614 client_write("\n", 1);
618 cprintf("%d No such configuration.\n",
619 ERROR + ILLEGAL_VALUE);
623 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
624 else if (!strcasecmp(cmd, "PUTSYS")) {
625 extract_token(confname, argbuf, 1, '|', sizeof confname);
627 cprintf("%d %s\n", SEND_LISTING, confname);
628 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
629 CtdlPutSysConfig(confname, confptr);
633 // CONF GETVAL - retrieve configuration variables from the database
634 // CONF LOADVAL - same thing but can handle variables bigger than 1 KB
635 else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
636 extract_token(confname, argbuf, 1, '|', sizeof confname);
637 char *v = CtdlGetConfigStr(confname);
638 if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
639 cprintf("%d %s|\n", CIT_OK, v);
641 else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
642 cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
643 client_write(v, strlen(v));
646 cprintf("%d |\n", ERROR);
650 // CONF PUTVAL - store configuration variables in the database
651 else if (!strcasecmp(cmd, "PUTVAL")) {
652 if (num_tokens(argbuf, '|') < 3) {
653 cprintf("%d name and value required\n", ERROR);
656 extract_token(confname, argbuf, 1, '|', sizeof confname);
657 extract_token(buf, argbuf, 2, '|', sizeof buf);
658 CtdlSetConfigStr(confname, buf);
659 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
663 // CONF STOREVAL - store configuration variables in the database bigger than 1 KB
664 else if (!strcasecmp(cmd, "STOREVAL")) {
665 if (num_tokens(argbuf, '|') < 3) {
666 cprintf("%d name and length required\n", ERROR);
669 extract_token(confname, argbuf, 1, '|', sizeof confname);
670 int bytes = extract_int(argbuf, 2);
671 char *valbuf = malloc(bytes + 1);
672 cprintf("%d %d\n", SEND_BINARY, bytes);
673 client_read(valbuf, bytes);
675 CtdlSetConfigStr(confname, valbuf);
680 // CONF LISTVAL - list configuration variables in the database and their values
681 else if (!strcasecmp(cmd, "LISTVAL")) {
682 struct cdbdata *cdbcfg;
687 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
688 cdb_rewind(CDB_CONFIG);
689 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
690 if (cdbcfg->len < 1020) {
691 keylen = strlen(cdbcfg->ptr);
693 value = cdbcfg->ptr + keylen + 1;
694 cprintf("%s|%s\n", key, value);
702 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
707 typedef struct __ConfType {
712 ConfType CfgNames[] = {
713 { {HKEY("localhost") }, 0},
714 { {HKEY("directory") }, 0},
715 { {HKEY("smarthost") }, 2},
716 { {HKEY("fallbackhost") }, 2},
717 { {HKEY("rbl") }, 3},
718 { {HKEY("spamassassin") }, 3},
719 { {HKEY("masqdomain") }, 1},
720 { {HKEY("clamav") }, 3},
721 { {HKEY("notify") }, 3},
725 HashList *CfgNameHash = NULL;
726 void cmd_gvdn(char *argbuf)
728 const ConfType *pCfg;
730 long min = atol(argbuf);
731 const char *Pos = NULL;
732 const char *PPos = NULL;
743 List = NewHash(1, NULL);
744 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
745 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
748 confptr = CtdlGetSysConfig(INTERNETCFG);
749 Config = NewStrBufPlain(confptr, -1);
752 Line = NewStrBufPlain(NULL, StrLength(Config));
753 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
754 while (StrBufSipLine(Line, Config, &Pos))
757 Cfg = NewStrBufPlain(NULL, StrLength(Line));
759 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
760 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
761 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
764 pCfg = (ConfType *) vptr;
765 if (pCfg->Type <= min)
767 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
773 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
774 It = GetNewHashPos(List, 1);
775 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
786 FreeStrBuf(&CfgToken);
790 /*****************************************************************************/
791 /* MODULE INITIALIZATION STUFF */
792 /*****************************************************************************/
794 void control_cleanup(void)
796 DeleteHash(&CfgNameHash);
798 CTDL_MODULE_INIT(control)
803 CfgNameHash = NewHash(1, NULL);
804 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
805 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
807 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
808 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
809 CtdlRegisterCleanupHook(control_cleanup);
812 /* return our id for the Log */