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"));
301 // CONF SET - set system configuration in legacy format
302 // This is deprecated; please do not add fields or change their order.
303 else if (!strcasecmp(cmd, "SET")) {
305 cprintf("%d Send configuration...\n", SEND_LISTING);
307 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
310 CtdlSetConfigStr("c_nodename", buf);
313 CtdlSetConfigStr("c_fqdn", buf);
316 CtdlSetConfigStr("c_humannode", buf);
319 // placeholder -- field no longer in use
322 CtdlSetConfigInt("c_creataide", confbool(buf));
325 CtdlSetConfigInt("c_sleeping", atoi(buf));
331 CtdlSetConfigInt("c_initax", i);
334 CtdlSetConfigInt("c_regiscall", confbool(buf));
337 CtdlSetConfigInt("c_twitdetect", confbool(buf));
340 CtdlSetConfigStr("c_twitroom", buf);
343 CtdlSetConfigStr("c_moreprompt", buf);
346 CtdlSetConfigInt("c_restrict", confbool(buf));
349 CtdlSetConfigStr("c_site_location", buf);
352 CtdlSetConfigStr("c_sysadm", buf);
357 CtdlSetConfigInt("c_maxsessions", i);
360 // placeholder -- field no longer in use
363 CtdlSetConfigInt("c_userpurge", atoi(buf));
366 CtdlSetConfigInt("c_roompurge", atoi(buf));
369 CtdlSetConfigStr("c_logpages", buf);
375 CtdlSetConfigInt("c_createax", i);
380 CtdlSetConfigLong("c_maxmsglen", ii);
385 if (i >= 3) { // minimum value
386 CtdlSetConfigInt("c_min_workers", i);
391 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
392 CtdlSetConfigInt("c_max_workers", i);
396 CtdlSetConfigInt("c_pop3_port", atoi(buf));
399 CtdlSetConfigInt("c_smtp_port", atoi(buf));
402 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
405 CtdlSetConfigInt("c_aide_zap", confbool(buf));
408 CtdlSetConfigInt("c_imap_port", atoi(buf));
411 CtdlSetConfigLong("c_net_freq", atol(buf));
414 CtdlSetConfigInt("c_disable_newu", confbool(buf));
421 if ((i >= 0) && (i <= 23)) {
422 CtdlSetConfigInt("c_purge_hour", i);
426 CtdlSetConfigStr("c_ldap_host", buf);
429 CtdlSetConfigInt("c_ldap_port", atoi(buf));
432 CtdlSetConfigStr("c_ldap_base_dn", buf);
435 CtdlSetConfigStr("c_ldap_bind_dn", buf);
438 CtdlSetConfigStr("c_ldap_bind_pw", buf);
441 CtdlSetConfigStr("c_ip_addr", buf);
444 CtdlSetConfigInt("c_msa_port", atoi(buf));
447 CtdlSetConfigInt("c_imaps_port", atoi(buf));
450 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
453 CtdlSetConfigInt("c_smtps_port", atoi(buf));
456 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
465 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
468 CtdlSetConfigInt("c_journal_email", confbool(buf));
471 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
474 CtdlSetConfigStr("c_journal_dest", buf);
477 CtdlSetConfigStr("c_default_cal_zone", buf);
480 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
486 CtdlSetConfigInt("c_auth_mode", atoi(buf));
501 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
510 CtdlSetConfigStr("c_pager_program", buf);
513 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
516 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
519 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
522 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
525 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
528 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
531 CtdlSetConfigInt("c_guest_logins", confbool(buf));
534 CtdlSetConfigInt("c_port_number", atoi(buf));
540 CtdlSetConfigInt("c_nntp_port", atoi(buf));
543 CtdlSetConfigInt("c_nntps_port", atoi(buf));
548 snprintf(buf, sizeof buf,
549 "The global system configuration has been edited by %s.\n",
550 (CC->logged_in ? CC->curr_user : "an administrator")
552 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
554 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
555 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
557 // If full text indexing has been disabled, invalidate the index so it doesn't try to use it later.
558 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
559 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
563 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
564 else if (!strcasecmp(cmd, "GETSYS")) {
565 extract_token(confname, argbuf, 1, '|', sizeof confname);
566 confptr = CtdlGetSysConfig(confname);
567 if (confptr != NULL) {
570 len = strlen(confptr);
571 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
572 client_write(confptr, len);
573 if ((len > 0) && (confptr[len - 1] != 10)) {
574 client_write("\n", 1);
580 cprintf("%d No such configuration.\n", ERROR + ILLEGAL_VALUE);
584 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
585 else if (!strcasecmp(cmd, "PUTSYS")) {
586 extract_token(confname, argbuf, 1, '|', sizeof confname);
588 cprintf("%d %s\n", SEND_LISTING, confname);
589 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
590 CtdlPutSysConfig(confname, confptr);
594 // CONF GETVAL - retrieve configuration variables from the database
595 // CONF LOADVAL - same thing but can handle variables bigger than 1 KB (deprecated and probably safe to remove)
596 else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
597 extract_token(confname, argbuf, 1, '|', sizeof confname);
598 char *v = CtdlGetConfigStr(confname);
599 if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
600 cprintf("%d %s|\n", CIT_OK, v);
602 else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
603 cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
604 client_write(v, strlen(v));
607 cprintf("%d |\n", ERROR);
611 // CONF PUTVAL - store configuration variables in the database
612 else if (!strcasecmp(cmd, "PUTVAL")) {
613 if (num_tokens(argbuf, '|') < 3) {
614 cprintf("%d name and value required\n", ERROR);
617 extract_token(confname, argbuf, 1, '|', sizeof confname);
618 extract_token(buf, argbuf, 2, '|', sizeof buf);
619 CtdlSetConfigStr(confname, buf);
620 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
624 // CONF STOREVAL - store configuration variables in the database bigger than 1 KB (deprecated and probably safe to remove)
625 else if (!strcasecmp(cmd, "STOREVAL")) {
626 if (num_tokens(argbuf, '|') < 3) {
627 cprintf("%d name and length required\n", ERROR);
630 extract_token(confname, argbuf, 1, '|', sizeof confname);
631 int bytes = extract_int(argbuf, 2);
632 char *valbuf = malloc(bytes + 1);
633 cprintf("%d %d\n", SEND_BINARY, bytes);
634 client_read(valbuf, bytes);
636 CtdlSetConfigStr(confname, valbuf);
641 // CONF LISTVAL - list configuration variables in the database and their values
642 else if (!strcasecmp(cmd, "LISTVAL")) {
643 struct cdbkeyval cdbcfg;
648 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
649 cdb_rewind(CDB_CONFIG);
650 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg.val.ptr!=NULL) { // MUST read to the end
651 if (cdbcfg.val.len < 1020) {
652 keylen = strlen(cdbcfg.val.ptr);
653 key = cdbcfg.val.ptr;
654 value = cdbcfg.val.ptr + keylen + 1;
655 cprintf("%s|%s\n", key, value);
662 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
667 typedef struct __ConfType {
672 ConfType CfgNames[] = {
673 { {HKEY("localhost") }, 0},
674 { {HKEY("directory") }, 0},
675 { {HKEY("smarthost") }, 2},
676 { {HKEY("fallbackhost") }, 2},
677 { {HKEY("rbl") }, 3},
678 { {HKEY("spamassassin") }, 3},
679 { {HKEY("masqdomain") }, 1},
680 { {HKEY("clamav") }, 3},
681 { {HKEY("notify") }, 3},
685 HashList *CfgNameHash = NULL;
686 void cmd_gvdn(char *argbuf) {
687 const ConfType *pCfg;
689 long min = atol(argbuf);
690 const char *Pos = NULL;
691 const char *PPos = NULL;
702 List = NewHash(1, NULL);
703 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
704 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
707 confptr = CtdlGetSysConfig(INTERNETCFG);
708 Config = NewStrBufPlain(confptr, -1);
711 Line = NewStrBufPlain(NULL, StrLength(Config));
712 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
713 while (StrBufSipLine(Line, Config, &Pos)) {
715 Cfg = NewStrBufPlain(NULL, StrLength(Line));
717 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
718 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
719 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) && (vptr != NULL)) {
720 pCfg = (ConfType *) vptr;
721 if (pCfg->Type <= min) {
722 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
728 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
729 It = GetNewHashPos(List, 1);
730 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr)) {
740 FreeStrBuf(&CfgToken);
745 // MODULE INITIALIZATION STUFF
746 char *ctdl_module_init_control(void) {
750 CfgNameHash = NewHash(1, NULL);
751 for (i = 0; CfgNames[i].Name.Key != NULL; i++) {
752 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
755 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
756 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
758 // return our id for the log