2 // This module handles states which are global to the entire server.
4 // Copyright (c) 1987-2023 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.
11 #include <libcitadel.h>
13 #include "ctdl_module.h"
15 #include "citserver.h"
18 long control_highest_user = 0;
20 // This is the legacy "control record" format for the message base. If found
21 // on disk, its contents will be migrated into the system configuration. Never
23 struct legacy_ctrl_format {
24 long MMhighest; // highest message number in file
25 unsigned MMflags; // Global system flags
26 long MMnextuser; // highest user number on system
27 long MMnextroom; // highest room number on system
28 int MM_hosted_upgrade_level; // Server-hosted upgrade level
29 int MM_fulltext_wordbreaker; // ID of wordbreaker in use
30 long MMfulltext; // highest message number indexed
31 int MMdbversion; // Version of Berkeley DB used on previous server run
35 // data that gets passed back and forth between control_find_highest() and its caller
37 long highest_roomnum_found;
38 long highest_msgnum_found;
42 // Callback to get highest room number when rebuilding message base metadata
44 // sanity_diag_mode (can be set by -s flag at startup) may be:
45 // 0 = attempt to fix inconsistencies
46 // 1 = show inconsistencies but don't repair them, exit after complete
47 // 2 = show inconsistencies but don't repair them, continue execution
48 void control_find_highest(struct ctdlroom *qrbuf, void *data) {
49 struct cfh *cfh = (struct cfh *)data;
50 struct cdbdata *cdbfr;
55 if (qrbuf->QRnumber > cfh->highest_roomnum_found) {
56 cfh->highest_roomnum_found = qrbuf->QRnumber;
59 // Load the message list
60 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
62 msglist = (long *) cdbfr->ptr;
63 num_msgs = cdbfr->len / sizeof(long);
66 return; // No messages at all? No further action.
70 for (c=0; c<num_msgs; c++) {
71 if (msglist[c] > cfh->highest_msgnum_found) {
72 cfh->highest_msgnum_found = msglist[c];
81 // Callback to get highest user number.
82 void control_find_user(char *username, void *out_data) {
83 struct ctdluser EachUser;
85 if (CtdlGetUser(&EachUser, username) != 0) {
89 if (EachUser.usernum > CtdlGetConfigLong("MMnextuser")) {
90 syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld , found in %s",
91 EachUser.usernum, CtdlGetConfigLong("MMnextuser"), EachUser.fullname
93 if (!sanity_diag_mode) {
94 CtdlSetConfigLong("MMnextuser", EachUser.usernum);
100 // If we have a legacy format control record on disk, import it.
101 void migrate_legacy_control_record(void) {
103 struct legacy_ctrl_format c;
104 memset(&c, 0, sizeof(c));
106 fp = fopen("citadel.control", "rb+");
108 syslog(LOG_INFO, "control: legacy format record found -- importing to db");
109 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
111 CtdlSetConfigLong( "MMhighest", c.MMhighest);
112 CtdlSetConfigInt( "MMflags", c.MMflags);
113 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
114 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
115 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
116 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
117 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
120 if (unlink("citadel.control") != 0) {
121 fprintf(stderr, "Unable to remove legacy control record after migrating it.\n");
122 fprintf(stderr, "Exiting to prevent data corruption.\n");
123 exit(CTDLEXIT_CONFIG);
129 // check_control - check the control record has sensible values for message, user and room numbers
130 void check_control(void) {
132 syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
134 memset(&cfh, 0, sizeof(struct cfh));
135 CtdlForEachRoom(control_find_highest, &cfh);
137 if (cfh.highest_roomnum_found > CtdlGetConfigLong("MMnextroom")) {
138 syslog(LOG_DEBUG, "control: fixing MMnextroom %ld > %ld", cfh.highest_roomnum_found, CtdlGetConfigLong("MMnextroom"));
139 if (!sanity_diag_mode) {
140 CtdlSetConfigLong("MMnextroom", cfh.highest_roomnum_found);
144 if (cfh.highest_msgnum_found > CtdlGetConfigLong("MMhighest")) {
145 syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld", cfh.highest_msgnum_found, CtdlGetConfigLong("MMhighest"));
146 if (!sanity_diag_mode) {
147 CtdlSetConfigLong("MMhighest", cfh.highest_msgnum_found);
151 syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
152 ForEachUser(control_find_user, NULL);
154 syslog(LOG_INFO, "control: sanity checks complete");
156 if (sanity_diag_mode == 1) {
157 syslog(LOG_INFO, "control: sanity check diagnostic mode is active - exiting now");
158 exit(CTDLEXIT_SANITY);
163 // get_new_message_number() - Obtain a new, unique ID to be used for a message.
164 long get_new_message_number(void) {
166 begin_critical_section(S_CONTROL);
167 retval = CtdlGetConfigLong("MMhighest");
169 CtdlSetConfigLong("MMhighest", retval);
170 end_critical_section(S_CONTROL);
175 // CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
176 // This provides a quick way to initialize a variable that might be used to indicate
177 // messages that should not be processed. For example, an inbox rules script will use this
178 // to record determine that messages older than this should not be processed.
180 // (Why is this function here? Can't we just go straight to the config variable it fetches?)
181 long CtdlGetCurrentMessageNumber(void) {
182 return CtdlGetConfigLong("MMhighest");
186 // get_new_user_number() - Obtain a new, unique ID to be used for a user.
187 long get_new_user_number(void) {
189 begin_critical_section(S_CONTROL);
190 retval = CtdlGetConfigLong("MMnextuser");
192 CtdlSetConfigLong("MMnextuser", retval);
193 end_critical_section(S_CONTROL);
198 // get_new_room_number() - Obtain a new, unique ID to be used for a room.
199 long get_new_room_number(void) {
201 begin_critical_section(S_CONTROL);
202 retval = CtdlGetConfigLong("MMnextroom");
204 CtdlSetConfigLong("MMnextroom", retval);
205 end_critical_section(S_CONTROL);
210 // Helper function for cmd_conf() to handle boolean values
211 int confbool(char *v) {
212 if (IsEmptyStr(v)) return(0);
213 if (atoi(v) != 0) return(1);
218 // Get or set global configuration options
219 void cmd_conf(char *argbuf) {
227 if (CtdlAccessCheck(ac_aide)) return;
229 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
231 // CONF GET - retrieve system configuration in legacy format
232 // This is deprecated; please do not add fields or change their order.
233 if (!strcasecmp(cmd, "GET")) {
234 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
235 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
236 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
237 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
238 cprintf("xxx\n"); // placeholder -- field no longer in use
239 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
240 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
241 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
242 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
243 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
244 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
245 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
246 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
247 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
248 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
249 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
250 cprintf("xxx\n"); // placeholder -- field no longer in use
251 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
252 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
253 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
254 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
255 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
256 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
257 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
258 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
259 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
260 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
261 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
262 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
263 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
264 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
265 cprintf("1\n"); // niu
266 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
267 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
268 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
269 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
270 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
271 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
272 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
273 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
274 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
275 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
276 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
277 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
278 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
280 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
281 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
282 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
283 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
284 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
285 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
287 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
292 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
295 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
296 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
297 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
298 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
299 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
300 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
301 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
302 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
303 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
304 cprintf("%d\n", ctdluid);
305 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
306 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
310 // CONF SET - set system configuration in legacy format
311 // This is deprecated; please do not add fields or change their order.
312 else if (!strcasecmp(cmd, "SET")) {
314 cprintf("%d Send configuration...\n", SEND_LISTING);
316 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
319 CtdlSetConfigStr("c_nodename", buf);
322 CtdlSetConfigStr("c_fqdn", buf);
325 CtdlSetConfigStr("c_humannode", buf);
328 // placeholder -- field no longer in use
331 CtdlSetConfigInt("c_creataide", confbool(buf));
334 CtdlSetConfigInt("c_sleeping", atoi(buf));
340 CtdlSetConfigInt("c_initax", i);
343 CtdlSetConfigInt("c_regiscall", confbool(buf));
346 CtdlSetConfigInt("c_twitdetect", confbool(buf));
349 CtdlSetConfigStr("c_twitroom", buf);
352 CtdlSetConfigStr("c_moreprompt", buf);
355 CtdlSetConfigInt("c_restrict", confbool(buf));
358 CtdlSetConfigStr("c_site_location", buf);
361 CtdlSetConfigStr("c_sysadm", buf);
366 CtdlSetConfigInt("c_maxsessions", i);
369 // placeholder -- field no longer in use
372 CtdlSetConfigInt("c_userpurge", atoi(buf));
375 CtdlSetConfigInt("c_roompurge", atoi(buf));
378 CtdlSetConfigStr("c_logpages", buf);
384 CtdlSetConfigInt("c_createax", i);
389 CtdlSetConfigLong("c_maxmsglen", ii);
394 if (i >= 3) { // minimum value
395 CtdlSetConfigInt("c_min_workers", i);
400 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
401 CtdlSetConfigInt("c_max_workers", i);
405 CtdlSetConfigInt("c_pop3_port", atoi(buf));
408 CtdlSetConfigInt("c_smtp_port", atoi(buf));
411 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
414 CtdlSetConfigInt("c_aide_zap", confbool(buf));
417 CtdlSetConfigInt("c_imap_port", atoi(buf));
420 CtdlSetConfigLong("c_net_freq", atol(buf));
423 CtdlSetConfigInt("c_disable_newu", confbool(buf));
430 if ((i >= 0) && (i <= 23)) {
431 CtdlSetConfigInt("c_purge_hour", i);
435 CtdlSetConfigStr("c_ldap_host", buf);
438 CtdlSetConfigInt("c_ldap_port", atoi(buf));
441 CtdlSetConfigStr("c_ldap_base_dn", buf);
444 CtdlSetConfigStr("c_ldap_bind_dn", buf);
447 CtdlSetConfigStr("c_ldap_bind_pw", buf);
450 CtdlSetConfigStr("c_ip_addr", buf);
453 CtdlSetConfigInt("c_msa_port", atoi(buf));
456 CtdlSetConfigInt("c_imaps_port", atoi(buf));
459 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
462 CtdlSetConfigInt("c_smtps_port", atoi(buf));
465 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
468 CtdlSetConfigInt("c_auto_cull", confbool(buf));
474 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
477 CtdlSetConfigInt("c_journal_email", confbool(buf));
480 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
483 CtdlSetConfigStr("c_journal_dest", buf);
486 CtdlSetConfigStr("c_default_cal_zone", buf);
489 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
495 CtdlSetConfigInt("c_auth_mode", atoi(buf));
510 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
519 CtdlSetConfigStr("c_pager_program", buf);
522 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
525 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
528 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
531 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
534 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
537 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
540 CtdlSetConfigInt("c_guest_logins", confbool(buf));
543 CtdlSetConfigInt("c_port_number", atoi(buf));
549 CtdlSetConfigInt("c_nntp_port", atoi(buf));
552 CtdlSetConfigInt("c_nntps_port", atoi(buf));
557 snprintf(buf, sizeof buf,
558 "The global system configuration has been edited by %s.\n",
559 (CC->logged_in ? CC->curr_user : "an administrator")
561 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
563 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
564 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
566 // If full text indexing has been disabled, invalidate the index so it doesn't try to use it later.
567 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
568 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
572 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
573 else if (!strcasecmp(cmd, "GETSYS")) {
574 extract_token(confname, argbuf, 1, '|', sizeof confname);
575 confptr = CtdlGetSysConfig(confname);
576 if (confptr != NULL) {
579 len = strlen(confptr);
580 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
581 client_write(confptr, len);
582 if ((len > 0) && (confptr[len - 1] != 10))
583 client_write("\n", 1);
587 cprintf("%d No such configuration.\n",
588 ERROR + ILLEGAL_VALUE);
592 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
593 else if (!strcasecmp(cmd, "PUTSYS")) {
594 extract_token(confname, argbuf, 1, '|', sizeof confname);
596 cprintf("%d %s\n", SEND_LISTING, confname);
597 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
598 CtdlPutSysConfig(confname, confptr);
602 // CONF GETVAL - retrieve configuration variables from the database
603 // CONF LOADVAL - same thing but can handle variables bigger than 1 KB (deprecated and probably safe to remove)
604 else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
605 extract_token(confname, argbuf, 1, '|', sizeof confname);
606 char *v = CtdlGetConfigStr(confname);
607 if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
608 cprintf("%d %s|\n", CIT_OK, v);
610 else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
611 cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
612 client_write(v, strlen(v));
615 cprintf("%d |\n", ERROR);
619 // CONF PUTVAL - store configuration variables in the database
620 else if (!strcasecmp(cmd, "PUTVAL")) {
621 if (num_tokens(argbuf, '|') < 3) {
622 cprintf("%d name and value required\n", ERROR);
625 extract_token(confname, argbuf, 1, '|', sizeof confname);
626 extract_token(buf, argbuf, 2, '|', sizeof buf);
627 CtdlSetConfigStr(confname, buf);
628 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
632 // CONF STOREVAL - store configuration variables in the database bigger than 1 KB (deprecated and probably safe to remove)
633 else if (!strcasecmp(cmd, "STOREVAL")) {
634 if (num_tokens(argbuf, '|') < 3) {
635 cprintf("%d name and length required\n", ERROR);
638 extract_token(confname, argbuf, 1, '|', sizeof confname);
639 int bytes = extract_int(argbuf, 2);
640 char *valbuf = malloc(bytes + 1);
641 cprintf("%d %d\n", SEND_BINARY, bytes);
642 client_read(valbuf, bytes);
644 CtdlSetConfigStr(confname, valbuf);
649 // CONF LISTVAL - list configuration variables in the database and their values
650 else if (!strcasecmp(cmd, "LISTVAL")) {
651 struct cdbdata *cdbcfg;
656 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
657 cdb_rewind(CDB_CONFIG);
658 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
659 if (cdbcfg->len < 1020) {
660 keylen = strlen(cdbcfg->ptr);
662 value = cdbcfg->ptr + keylen + 1;
663 cprintf("%s|%s\n", key, value);
671 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
676 typedef struct __ConfType {
681 ConfType CfgNames[] = {
682 { {HKEY("localhost") }, 0},
683 { {HKEY("directory") }, 0},
684 { {HKEY("smarthost") }, 2},
685 { {HKEY("fallbackhost") }, 2},
686 { {HKEY("rbl") }, 3},
687 { {HKEY("spamassassin") }, 3},
688 { {HKEY("masqdomain") }, 1},
689 { {HKEY("clamav") }, 3},
690 { {HKEY("notify") }, 3},
694 HashList *CfgNameHash = NULL;
695 void cmd_gvdn(char *argbuf) {
696 const ConfType *pCfg;
698 long min = atol(argbuf);
699 const char *Pos = NULL;
700 const char *PPos = NULL;
711 List = NewHash(1, NULL);
712 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
713 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
716 confptr = CtdlGetSysConfig(INTERNETCFG);
717 Config = NewStrBufPlain(confptr, -1);
720 Line = NewStrBufPlain(NULL, StrLength(Config));
721 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
722 while (StrBufSipLine(Line, Config, &Pos)) {
724 Cfg = NewStrBufPlain(NULL, StrLength(Line));
726 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
727 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
728 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) && (vptr != NULL)) {
729 pCfg = (ConfType *) vptr;
730 if (pCfg->Type <= min)
732 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
738 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
739 It = GetNewHashPos(List, 1);
740 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr)) {
750 FreeStrBuf(&CfgToken);
755 // MODULE INITIALIZATION STUFF
756 char *ctdl_module_init_control(void) {
760 CfgNameHash = NewHash(1, NULL);
761 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
762 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
764 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
765 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
767 // return our id for the log