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"
19 long control_highest_user = 0;
21 // This is the legacy "control record" format for the message base. If found
22 // on disk, its contents will be migrated into the system configuration. Never
24 struct legacy_ctrl_format {
25 long MMhighest; // highest message number in file
26 unsigned MMflags; // Global system flags
27 long MMnextuser; // highest user number on system
28 long MMnextroom; // highest room number on system
29 int MM_hosted_upgrade_level; // Server-hosted upgrade level
30 int MM_fulltext_wordbreaker; // ID of wordbreaker in use
31 long MMfulltext; // highest message number indexed
32 int MMdbversion; // Version of Berkeley DB used on previous server run
36 // data that gets passed back and forth between control_find_highest() and its caller
38 long highest_roomnum_found;
39 long highest_msgnum_found;
43 // Callback to get highest room number when rebuilding message base metadata
45 // sanity_diag_mode (can be set by -s flag at startup) may be:
46 // 0 = attempt to fix inconsistencies
47 // 1 = show inconsistencies but don't repair them, exit after complete
48 // 2 = show inconsistencies but don't repair them, continue execution
49 void control_find_highest(struct ctdlroom *qrbuf, void *data) {
50 struct cfh *cfh = (struct cfh *)data;
55 if (qrbuf->QRnumber > cfh->highest_roomnum_found) {
56 cfh->highest_roomnum_found = qrbuf->QRnumber;
59 // Load the message list
60 num_msgs = CtdlFetchMsgList(qrbuf->QRnumber, &msglist);
62 return; // No msglists record? No further action.
66 for (c=0; c<num_msgs; c++) {
67 if (msglist[c] > cfh->highest_msgnum_found) {
68 cfh->highest_msgnum_found = msglist[c];
77 // Callback to get highest user number.
78 void control_find_user(char *username, void *out_data) {
79 struct ctdluser EachUser;
81 if (CtdlGetUser(&EachUser, username) != 0) {
85 if (EachUser.usernum > CtdlGetConfigLong("MMnextuser")) {
86 syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld , found in %s",
87 EachUser.usernum, CtdlGetConfigLong("MMnextuser"), EachUser.fullname
89 if (!sanity_diag_mode) {
90 CtdlSetConfigLong("MMnextuser", EachUser.usernum);
96 // If we have a legacy format control record on disk, import it.
97 void migrate_legacy_control_record(void) {
99 struct legacy_ctrl_format c;
100 memset(&c, 0, sizeof(c));
102 fp = fopen("citadel.control", "rb+");
104 syslog(LOG_INFO, "control: legacy format record found -- importing to db");
105 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
107 CtdlSetConfigLong( "MMhighest", c.MMhighest);
108 CtdlSetConfigInt( "MMflags", c.MMflags);
109 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
110 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
111 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
112 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
113 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
116 if (unlink("citadel.control") != 0) {
117 fprintf(stderr, "Unable to remove legacy control record after migrating it.\n");
118 fprintf(stderr, "Exiting to prevent data corruption.\n");
119 exit(CTDLEXIT_CONFIG);
125 // check_control - check the control record has sensible values for message, user and room numbers
126 void check_control(void) {
128 syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
130 memset(&cfh, 0, sizeof(struct cfh));
131 CtdlForEachRoom(control_find_highest, &cfh);
133 if (cfh.highest_roomnum_found > CtdlGetConfigLong("MMnextroom")) {
134 syslog(LOG_DEBUG, "control: fixing MMnextroom %ld > %ld", cfh.highest_roomnum_found, CtdlGetConfigLong("MMnextroom"));
135 if (!sanity_diag_mode) {
136 CtdlSetConfigLong("MMnextroom", cfh.highest_roomnum_found);
140 if (cfh.highest_msgnum_found > CtdlGetConfigLong("MMhighest")) {
141 syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld", cfh.highest_msgnum_found, CtdlGetConfigLong("MMhighest"));
142 if (!sanity_diag_mode) {
143 CtdlSetConfigLong("MMhighest", cfh.highest_msgnum_found);
147 syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
148 ForEachUser(control_find_user, NULL);
150 syslog(LOG_INFO, "control: sanity checks complete");
152 if (sanity_diag_mode == 1) {
153 syslog(LOG_INFO, "control: sanity check diagnostic mode is active - exiting now");
154 exit(CTDLEXIT_SANITY);
159 // get_new_message_number() - Obtain a new, unique ID to be used for a message.
160 long get_new_message_number(void) {
162 begin_critical_section(S_CONTROL);
163 retval = CtdlGetConfigLong("MMhighest");
165 CtdlSetConfigLong("MMhighest", retval);
166 end_critical_section(S_CONTROL);
171 // CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
172 // This provides a quick way to initialize a variable that might be used to indicate
173 // messages that should not be processed. For example, an inbox rules script will use this
174 // to record determine that messages older than this should not be processed.
176 // (Why is this function here? Can't we just go straight to the config variable it fetches?)
177 long CtdlGetCurrentMessageNumber(void) {
178 return CtdlGetConfigLong("MMhighest");
182 // get_new_user_number() - Obtain a new, unique ID to be used for a user.
183 long get_new_user_number(void) {
185 begin_critical_section(S_CONTROL);
186 retval = CtdlGetConfigLong("MMnextuser");
188 CtdlSetConfigLong("MMnextuser", retval);
189 end_critical_section(S_CONTROL);
194 // get_new_room_number() - Obtain a new, unique ID to be used for a room.
195 long get_new_room_number(void) {
197 begin_critical_section(S_CONTROL);
198 retval = CtdlGetConfigLong("MMnextroom");
200 CtdlSetConfigLong("MMnextroom", retval);
201 end_critical_section(S_CONTROL);
206 // Helper function for cmd_conf() to handle boolean values
207 int confbool(char *v) {
208 if (IsEmptyStr(v)) return(0);
209 if (atoi(v) != 0) return(1);
214 // Get or set global configuration options
215 void cmd_conf(char *argbuf) {
223 if (CtdlAccessCheck(ac_aide)) return;
225 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
227 // CONF GET - retrieve system configuration in legacy format
228 // This is deprecated; please do not add fields or change their order.
229 if (!strcasecmp(cmd, "GET")) {
230 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
231 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
232 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
233 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
234 cprintf("xxx\n"); // placeholder -- field no longer in use
235 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
236 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
237 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
238 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
239 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
240 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
241 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
242 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
243 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
244 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
245 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
246 cprintf("xxx\n"); // placeholder -- field no longer in use
247 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
248 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
249 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
250 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
251 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
252 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
253 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
254 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
255 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
256 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
257 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
258 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
259 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
260 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
261 cprintf("1\n"); // niu
262 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
263 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
264 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
265 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
266 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
267 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
268 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
269 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
270 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
271 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
272 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
273 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
274 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
276 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
277 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
278 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
279 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
280 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
281 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
283 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
288 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
291 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
292 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
293 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
294 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
295 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
296 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
297 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
298 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
299 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
300 cprintf("%d\n", ctdluid);
301 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
302 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
306 // CONF SET - set system configuration in legacy format
307 // This is deprecated; please do not add fields or change their order.
308 else if (!strcasecmp(cmd, "SET")) {
310 cprintf("%d Send configuration...\n", SEND_LISTING);
312 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
315 CtdlSetConfigStr("c_nodename", buf);
318 CtdlSetConfigStr("c_fqdn", buf);
321 CtdlSetConfigStr("c_humannode", buf);
324 // placeholder -- field no longer in use
327 CtdlSetConfigInt("c_creataide", confbool(buf));
330 CtdlSetConfigInt("c_sleeping", atoi(buf));
336 CtdlSetConfigInt("c_initax", i);
339 CtdlSetConfigInt("c_regiscall", confbool(buf));
342 CtdlSetConfigInt("c_twitdetect", confbool(buf));
345 CtdlSetConfigStr("c_twitroom", buf);
348 CtdlSetConfigStr("c_moreprompt", buf);
351 CtdlSetConfigInt("c_restrict", confbool(buf));
354 CtdlSetConfigStr("c_site_location", buf);
357 CtdlSetConfigStr("c_sysadm", buf);
362 CtdlSetConfigInt("c_maxsessions", i);
365 // placeholder -- field no longer in use
368 CtdlSetConfigInt("c_userpurge", atoi(buf));
371 CtdlSetConfigInt("c_roompurge", atoi(buf));
374 CtdlSetConfigStr("c_logpages", buf);
380 CtdlSetConfigInt("c_createax", i);
385 CtdlSetConfigLong("c_maxmsglen", ii);
390 if (i >= 3) { // minimum value
391 CtdlSetConfigInt("c_min_workers", i);
396 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
397 CtdlSetConfigInt("c_max_workers", i);
401 CtdlSetConfigInt("c_pop3_port", atoi(buf));
404 CtdlSetConfigInt("c_smtp_port", atoi(buf));
407 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
410 CtdlSetConfigInt("c_aide_zap", confbool(buf));
413 CtdlSetConfigInt("c_imap_port", atoi(buf));
416 CtdlSetConfigLong("c_net_freq", atol(buf));
419 CtdlSetConfigInt("c_disable_newu", confbool(buf));
426 if ((i >= 0) && (i <= 23)) {
427 CtdlSetConfigInt("c_purge_hour", i);
431 CtdlSetConfigStr("c_ldap_host", buf);
434 CtdlSetConfigInt("c_ldap_port", atoi(buf));
437 CtdlSetConfigStr("c_ldap_base_dn", buf);
440 CtdlSetConfigStr("c_ldap_bind_dn", buf);
443 CtdlSetConfigStr("c_ldap_bind_pw", buf);
446 CtdlSetConfigStr("c_ip_addr", buf);
449 CtdlSetConfigInt("c_msa_port", atoi(buf));
452 CtdlSetConfigInt("c_imaps_port", atoi(buf));
455 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
458 CtdlSetConfigInt("c_smtps_port", atoi(buf));
461 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
464 CtdlSetConfigInt("c_auto_cull", confbool(buf));
470 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
473 CtdlSetConfigInt("c_journal_email", confbool(buf));
476 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
479 CtdlSetConfigStr("c_journal_dest", buf);
482 CtdlSetConfigStr("c_default_cal_zone", buf);
485 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
491 CtdlSetConfigInt("c_auth_mode", atoi(buf));
506 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
515 CtdlSetConfigStr("c_pager_program", buf);
518 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
521 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
524 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
527 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
530 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
533 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
536 CtdlSetConfigInt("c_guest_logins", confbool(buf));
539 CtdlSetConfigInt("c_port_number", atoi(buf));
545 CtdlSetConfigInt("c_nntp_port", atoi(buf));
548 CtdlSetConfigInt("c_nntps_port", atoi(buf));
553 snprintf(buf, sizeof buf,
554 "The global system configuration has been edited by %s.\n",
555 (CC->logged_in ? CC->curr_user : "an administrator")
557 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
559 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
560 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
562 // If full text indexing has been disabled, invalidate the index so it doesn't try to use it later.
563 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
564 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
568 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
569 else if (!strcasecmp(cmd, "GETSYS")) {
570 extract_token(confname, argbuf, 1, '|', sizeof confname);
571 confptr = CtdlGetSysConfig(confname);
572 if (confptr != NULL) {
575 len = strlen(confptr);
576 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
577 client_write(confptr, len);
578 if ((len > 0) && (confptr[len - 1] != 10))
579 client_write("\n", 1);
583 cprintf("%d No such configuration.\n",
584 ERROR + ILLEGAL_VALUE);
588 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
589 else if (!strcasecmp(cmd, "PUTSYS")) {
590 extract_token(confname, argbuf, 1, '|', sizeof confname);
592 cprintf("%d %s\n", SEND_LISTING, confname);
593 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
594 CtdlPutSysConfig(confname, confptr);
598 // CONF GETVAL - retrieve configuration variables from the database
599 // CONF LOADVAL - same thing but can handle variables bigger than 1 KB (deprecated and probably safe to remove)
600 else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
601 extract_token(confname, argbuf, 1, '|', sizeof confname);
602 char *v = CtdlGetConfigStr(confname);
603 if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
604 cprintf("%d %s|\n", CIT_OK, v);
606 else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
607 cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
608 client_write(v, strlen(v));
611 cprintf("%d |\n", ERROR);
615 // CONF PUTVAL - store configuration variables in the database
616 else if (!strcasecmp(cmd, "PUTVAL")) {
617 if (num_tokens(argbuf, '|') < 3) {
618 cprintf("%d name and value required\n", ERROR);
621 extract_token(confname, argbuf, 1, '|', sizeof confname);
622 extract_token(buf, argbuf, 2, '|', sizeof buf);
623 CtdlSetConfigStr(confname, buf);
624 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
628 // CONF STOREVAL - store configuration variables in the database bigger than 1 KB (deprecated and probably safe to remove)
629 else if (!strcasecmp(cmd, "STOREVAL")) {
630 if (num_tokens(argbuf, '|') < 3) {
631 cprintf("%d name and length required\n", ERROR);
634 extract_token(confname, argbuf, 1, '|', sizeof confname);
635 int bytes = extract_int(argbuf, 2);
636 char *valbuf = malloc(bytes + 1);
637 cprintf("%d %d\n", SEND_BINARY, bytes);
638 client_read(valbuf, bytes);
640 CtdlSetConfigStr(confname, valbuf);
645 // CONF LISTVAL - list configuration variables in the database and their values
646 else if (!strcasecmp(cmd, "LISTVAL")) {
647 struct cdbdata *cdbcfg;
652 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
653 cdb_rewind(CDB_CONFIG);
654 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
655 if (cdbcfg->len < 1020) {
656 keylen = strlen(cdbcfg->ptr);
658 value = cdbcfg->ptr + keylen + 1;
659 cprintf("%s|%s\n", key, value);
667 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
672 typedef struct __ConfType {
677 ConfType CfgNames[] = {
678 { {HKEY("localhost") }, 0},
679 { {HKEY("directory") }, 0},
680 { {HKEY("smarthost") }, 2},
681 { {HKEY("fallbackhost") }, 2},
682 { {HKEY("rbl") }, 3},
683 { {HKEY("spamassassin") }, 3},
684 { {HKEY("masqdomain") }, 1},
685 { {HKEY("clamav") }, 3},
686 { {HKEY("notify") }, 3},
690 HashList *CfgNameHash = NULL;
691 void cmd_gvdn(char *argbuf) {
692 const ConfType *pCfg;
694 long min = atol(argbuf);
695 const char *Pos = NULL;
696 const char *PPos = NULL;
707 List = NewHash(1, NULL);
708 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
709 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
712 confptr = CtdlGetSysConfig(INTERNETCFG);
713 Config = NewStrBufPlain(confptr, -1);
716 Line = NewStrBufPlain(NULL, StrLength(Config));
717 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
718 while (StrBufSipLine(Line, Config, &Pos)) {
720 Cfg = NewStrBufPlain(NULL, StrLength(Line));
722 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
723 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
724 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) && (vptr != NULL)) {
725 pCfg = (ConfType *) vptr;
726 if (pCfg->Type <= min)
728 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
734 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
735 It = GetNewHashPos(List, 1);
736 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr)) {
746 FreeStrBuf(&CfgToken);
751 // MODULE INITIALIZATION STUFF
752 char *ctdl_module_init_control(void) {
756 CfgNameHash = NewHash(1, NULL);
757 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
758 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
760 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
761 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
763 // return our id for the log