2 // This module handles states which are global to the entire server.
4 // Copyright (c) 1987-2024 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 for (c=0; c<num_msgs; c++) {
63 if (msglist[c] > cfh->highest_msgnum_found) {
64 cfh->highest_msgnum_found = msglist[c];
72 // Callback to get highest user number.
73 void control_find_user(char *username, void *out_data) {
74 struct ctdluser EachUser;
76 if (CtdlGetUser(&EachUser, username) != 0) {
80 if (EachUser.usernum > CtdlGetConfigLong("MMnextuser")) {
81 syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld , found in %s",
82 EachUser.usernum, CtdlGetConfigLong("MMnextuser"), EachUser.fullname
84 if (!sanity_diag_mode) {
85 CtdlSetConfigLong("MMnextuser", EachUser.usernum);
91 // If we have a legacy format control record on disk, import it.
92 void migrate_legacy_control_record(void) {
94 struct legacy_ctrl_format c;
95 memset(&c, 0, sizeof(c));
97 fp = fopen("citadel.control", "rb+");
99 syslog(LOG_INFO, "control: legacy format record found -- importing to db");
100 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
102 CtdlSetConfigLong( "MMhighest", c.MMhighest);
103 CtdlSetConfigInt( "MMflags", c.MMflags);
104 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
105 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
106 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
107 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
108 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
111 if (unlink("citadel.control") != 0) {
112 fprintf(stderr, "Unable to remove legacy control record after migrating it.\n");
113 fprintf(stderr, "Exiting to prevent data corruption.\n");
114 exit(CTDLEXIT_CONFIG);
120 // check_control - check the control record has sensible values for message, user and room numbers
121 void check_control(void) {
123 syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
125 memset(&cfh, 0, sizeof(struct cfh));
126 CtdlForEachRoom(control_find_highest, &cfh);
128 if (cfh.highest_roomnum_found > CtdlGetConfigLong("MMnextroom")) {
129 syslog(LOG_DEBUG, "control: fixing MMnextroom %ld > %ld", cfh.highest_roomnum_found, CtdlGetConfigLong("MMnextroom"));
130 if (!sanity_diag_mode) {
131 CtdlSetConfigLong("MMnextroom", cfh.highest_roomnum_found);
135 if (cfh.highest_msgnum_found > CtdlGetConfigLong("MMhighest")) {
136 syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld", cfh.highest_msgnum_found, CtdlGetConfigLong("MMhighest"));
137 if (!sanity_diag_mode) {
138 CtdlSetConfigLong("MMhighest", cfh.highest_msgnum_found);
142 syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
143 ForEachUser(control_find_user, NULL);
145 syslog(LOG_INFO, "control: sanity checks complete");
147 if (sanity_diag_mode == 1) {
148 syslog(LOG_INFO, "control: sanity check diagnostic mode is active - exiting now");
149 exit(CTDLEXIT_SANITY);
154 // get_new_message_number() - Obtain a new, unique ID to be used for a message.
155 long get_new_message_number(void) {
157 begin_critical_section(S_CONTROL);
158 retval = CtdlGetConfigLong("MMhighest");
160 CtdlSetConfigLong("MMhighest", retval);
161 end_critical_section(S_CONTROL);
166 // CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
167 // This provides a quick way to initialize a variable that might be used to indicate
168 // messages that should not be processed. For example, an inbox rules script will use this
169 // to record determine that messages older than this should not be processed.
171 // (Why is this function here? Can't we just go straight to the config variable it fetches?)
172 long CtdlGetCurrentMessageNumber(void) {
173 return CtdlGetConfigLong("MMhighest");
177 // get_new_user_number() - Obtain a new, unique ID to be used for a user.
178 long get_new_user_number(void) {
180 begin_critical_section(S_CONTROL);
181 retval = CtdlGetConfigLong("MMnextuser");
183 CtdlSetConfigLong("MMnextuser", retval);
184 end_critical_section(S_CONTROL);
189 // get_new_room_number() - Obtain a new, unique ID to be used for a room.
190 long get_new_room_number(void) {
192 begin_critical_section(S_CONTROL);
193 retval = CtdlGetConfigLong("MMnextroom");
195 CtdlSetConfigLong("MMnextroom", retval);
196 end_critical_section(S_CONTROL);
201 // Helper function for cmd_conf() to handle boolean values
202 int confbool(char *v) {
203 if (IsEmptyStr(v)) return(0);
204 if (atoi(v) != 0) return(1);
209 // Get or set global configuration options
210 void cmd_conf(char *argbuf) {
218 if (CtdlAccessCheck(ac_aide)) return;
220 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
222 // CONF GET - retrieve system configuration in legacy format
223 // This is deprecated; please do not add fields or change their order.
224 if (!strcasecmp(cmd, "GET")) {
225 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
226 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
227 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
228 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
230 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
231 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
232 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
233 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
234 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
235 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
236 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
237 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
238 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
239 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
240 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
242 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
243 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
244 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
245 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
246 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
247 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
248 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
249 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
250 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
251 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
252 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
253 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
254 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
255 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
257 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
258 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
259 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
260 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
261 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
262 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
263 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
264 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
265 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
266 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
267 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
268 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
271 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
272 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
273 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
274 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
275 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
276 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
278 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
283 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
286 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
287 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
288 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
289 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
290 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
291 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
292 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
293 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
294 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
295 cprintf("%d\n", ctdluid);
296 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
297 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
298 cprintf("%d\n", CtdlGetConfigInt("smtp_advertise_starttls"));
302 // CONF SET - set system configuration in legacy format
303 // This is deprecated; please do not add fields or change their order.
304 else if (!strcasecmp(cmd, "SET")) {
306 cprintf("%d Send configuration...\n", SEND_LISTING);
308 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
311 CtdlSetConfigStr("c_nodename", buf);
314 CtdlSetConfigStr("c_fqdn", buf);
317 CtdlSetConfigStr("c_humannode", buf);
320 // placeholder -- field no longer in use
323 CtdlSetConfigInt("c_creataide", confbool(buf));
326 CtdlSetConfigInt("c_sleeping", atoi(buf));
332 CtdlSetConfigInt("c_initax", i);
335 CtdlSetConfigInt("c_regiscall", confbool(buf));
338 CtdlSetConfigInt("c_twitdetect", confbool(buf));
341 CtdlSetConfigStr("c_twitroom", buf);
344 CtdlSetConfigStr("c_moreprompt", buf);
347 CtdlSetConfigInt("c_restrict", confbool(buf));
350 CtdlSetConfigStr("c_site_location", buf);
353 CtdlSetConfigStr("c_sysadm", buf);
358 CtdlSetConfigInt("c_maxsessions", i);
361 // placeholder -- field no longer in use
364 CtdlSetConfigInt("c_userpurge", atoi(buf));
367 CtdlSetConfigInt("c_roompurge", atoi(buf));
370 CtdlSetConfigStr("c_logpages", buf);
376 CtdlSetConfigInt("c_createax", i);
381 CtdlSetConfigLong("c_maxmsglen", ii);
386 if (i >= 3) { // minimum value
387 CtdlSetConfigInt("c_min_workers", i);
392 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
393 CtdlSetConfigInt("c_max_workers", i);
397 CtdlSetConfigInt("c_pop3_port", atoi(buf));
400 CtdlSetConfigInt("c_smtp_port", atoi(buf));
403 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
406 CtdlSetConfigInt("c_aide_zap", confbool(buf));
409 CtdlSetConfigInt("c_imap_port", atoi(buf));
412 CtdlSetConfigLong("c_net_freq", atol(buf));
415 CtdlSetConfigInt("c_disable_newu", confbool(buf));
422 if ((i >= 0) && (i <= 23)) {
423 CtdlSetConfigInt("c_purge_hour", i);
427 CtdlSetConfigStr("c_ldap_host", buf);
430 CtdlSetConfigInt("c_ldap_port", atoi(buf));
433 CtdlSetConfigStr("c_ldap_base_dn", buf);
436 CtdlSetConfigStr("c_ldap_bind_dn", buf);
439 CtdlSetConfigStr("c_ldap_bind_pw", buf);
442 CtdlSetConfigStr("c_ip_addr", buf);
445 CtdlSetConfigInt("c_msa_port", atoi(buf));
448 CtdlSetConfigInt("c_imaps_port", atoi(buf));
451 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
454 CtdlSetConfigInt("c_smtps_port", atoi(buf));
457 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
466 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
469 CtdlSetConfigInt("c_journal_email", confbool(buf));
472 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
475 CtdlSetConfigStr("c_journal_dest", buf);
478 CtdlSetConfigStr("c_default_cal_zone", buf);
481 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
487 CtdlSetConfigInt("c_auth_mode", atoi(buf));
502 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
511 CtdlSetConfigStr("c_pager_program", buf);
514 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
517 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
520 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
523 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
526 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
529 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
532 CtdlSetConfigInt("c_guest_logins", confbool(buf));
535 CtdlSetConfigInt("c_port_number", atoi(buf));
541 CtdlSetConfigInt("c_nntp_port", atoi(buf));
544 CtdlSetConfigInt("c_nntps_port", atoi(buf));
547 CtdlSetConfigInt("smtp_advertise_starttls", atoi(buf));
552 snprintf(buf, sizeof buf,
553 "The global system configuration has been edited by %s.\n",
554 (CC->logged_in ? CC->curr_user : "an administrator")
556 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
558 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
559 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
561 // If full text indexing has been disabled, invalidate the index so it doesn't try to use it later.
562 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
563 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
567 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
568 else if (!strcasecmp(cmd, "GETSYS")) {
569 extract_token(confname, argbuf, 1, '|', sizeof confname);
570 confptr = CtdlGetSysConfig(confname);
571 if (confptr != NULL) {
574 len = strlen(confptr);
575 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
576 client_write(confptr, len);
577 if ((len > 0) && (confptr[len - 1] != 10)) {
578 client_write("\n", 1);
584 cprintf("%d No such configuration.\n", 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 cdbkeyval cdbcfg;
652 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
653 cdb_rewind(CDB_CONFIG);
654 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg.val.ptr!=NULL) { // MUST read to the end
655 if (cdbcfg.val.len < 1020) {
656 keylen = strlen(cdbcfg.val.ptr);
657 key = cdbcfg.val.ptr;
658 value = cdbcfg.val.ptr + keylen + 1;
659 cprintf("%s|%s\n", key, value);
666 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
671 typedef struct __ConfType {
676 ConfType CfgNames[] = {
677 { {HKEY("localhost") }, 0},
678 { {HKEY("directory") }, 0},
679 { {HKEY("smarthost") }, 2},
680 { {HKEY("fallbackhost") }, 2},
681 { {HKEY("rbl") }, 3},
682 { {HKEY("spamassassin") }, 3},
683 { {HKEY("masqdomain") }, 1},
684 { {HKEY("clamav") }, 3},
685 { {HKEY("notify") }, 3},
689 HashList *CfgNameHash = NULL;
690 void cmd_gvdn(char *argbuf) {
691 const ConfType *pCfg;
693 long min = atol(argbuf);
694 const char *Pos = NULL;
695 const char *PPos = NULL;
706 List = NewHash(1, NULL);
707 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
708 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
711 confptr = CtdlGetSysConfig(INTERNETCFG);
712 Config = NewStrBufPlain(confptr, -1);
715 Line = NewStrBufPlain(NULL, StrLength(Config));
716 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
717 while (StrBufSipLine(Line, Config, &Pos)) {
719 Cfg = NewStrBufPlain(NULL, StrLength(Line));
721 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
722 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
723 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) && (vptr != NULL)) {
724 pCfg = (ConfType *) vptr;
725 if (pCfg->Type <= min) {
726 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
732 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
733 It = GetNewHashPos(List, 1);
734 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr)) {
744 FreeStrBuf(&CfgToken);
749 // MODULE INITIALIZATION STUFF
750 char *ctdl_module_init_control(void) {
754 CfgNameHash = NewHash(1, NULL);
755 for (i = 0; CfgNames[i].Name.Key != NULL; i++) {
756 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
759 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
760 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
762 // return our id for the log