2 // This module handles states which are global to the entire server.
4 // Copyright (c) 1987-2021 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.
8 // The program is distributed without any warranty, expressed or implied.
12 #include <libcitadel.h>
14 #include "ctdl_module.h"
16 #include "citserver.h"
19 long control_highest_user = 0;
22 * This is the legacy "control record" format for the message base. If found
23 * on disk, its contents will be migrated into the system configuration. Never
26 struct legacy_ctrl_format {
27 long MMhighest; /* highest message number in file */
28 unsigned MMflags; /* Global system flags */
29 long MMnextuser; /* highest user number on system */
30 long MMnextroom; /* highest room number on system */
31 int MM_hosted_upgrade_level; /* Server-hosted upgrade level */
32 int MM_fulltext_wordbreaker; /* ID of wordbreaker in use */
33 long MMfulltext; /* highest message number indexed */
34 int MMdbversion; /* Version of Berkeley DB used on previous server run */
39 * data that gets passed back and forth between control_find_highest() and its caller
42 long highest_roomnum_found;
43 long highest_msgnum_found;
48 * Callback to get highest room number when rebuilding message base metadata
50 * sanity_diag_mode (can be set by -s flag at startup) may be:
51 * 0 = attempt to fix inconsistencies
52 * 1 = show inconsistencies but don't repair them, exit after complete
53 * 2 = show inconsistencies but don't repair them, continue execution
55 void control_find_highest(struct ctdlroom *qrbuf, void *data) {
56 struct cfh *cfh = (struct cfh *)data;
57 struct cdbdata *cdbfr;
62 if (qrbuf->QRnumber > cfh->highest_roomnum_found) {
63 cfh->highest_roomnum_found = qrbuf->QRnumber;
66 /* Load the message list */
67 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
69 msglist = (long *) cdbfr->ptr;
70 num_msgs = cdbfr->len / sizeof(long);
73 return; /* No messages at all? No further action. */
77 for (c=0; c<num_msgs; c++) {
78 if (msglist[c] > cfh->highest_msgnum_found) {
79 cfh->highest_msgnum_found = msglist[c];
89 * Callback to get highest user number.
91 void control_find_user(char *username, void *out_data) {
92 struct ctdluser EachUser;
94 if (CtdlGetUser(&EachUser, username) != 0) {
98 if (EachUser.usernum > CtdlGetConfigLong("MMnextuser")) {
99 syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld , found in %s",
100 EachUser.usernum, CtdlGetConfigLong("MMnextuser"), EachUser.fullname
102 if (!sanity_diag_mode) {
103 CtdlSetConfigLong("MMnextuser", EachUser.usernum);
110 * If we have a legacy format control record on disk, import it.
112 void migrate_legacy_control_record(void) {
114 struct legacy_ctrl_format c;
115 memset(&c, 0, sizeof(c));
117 fp = fopen("citadel.control", "rb+");
119 syslog(LOG_INFO, "control: legacy format record found -- importing to db");
120 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
122 CtdlSetConfigLong( "MMhighest", c.MMhighest);
123 CtdlSetConfigInt( "MMflags", c.MMflags);
124 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
125 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
126 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
127 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
128 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
131 if (unlink("citadel.control") != 0) {
132 fprintf(stderr, "Unable to remove legacy control record after migrating it.\n");
133 fprintf(stderr, "Exiting to prevent data corruption.\n");
134 exit(CTDLEXIT_CONFIG);
141 * check_control - check the control record has sensible values for message, user and room numbers
143 void check_control(void) {
145 syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
147 memset(&cfh, 0, sizeof(struct cfh));
148 CtdlForEachRoom(control_find_highest, &cfh);
150 if (cfh.highest_roomnum_found > CtdlGetConfigLong("MMnextroom")) {
151 syslog(LOG_DEBUG, "control: fixing MMnextroom %ld > %ld", cfh.highest_roomnum_found, CtdlGetConfigLong("MMnextroom"));
152 if (!sanity_diag_mode) {
153 CtdlSetConfigLong("MMnextroom", cfh.highest_roomnum_found);
157 if (cfh.highest_msgnum_found > CtdlGetConfigLong("MMhighest")) {
158 syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld", cfh.highest_msgnum_found, CtdlGetConfigLong("MMhighest"));
159 if (!sanity_diag_mode) {
160 CtdlSetConfigLong("MMhighest", cfh.highest_msgnum_found);
164 syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
165 ForEachUser(control_find_user, NULL);
167 syslog(LOG_INFO, "control: sanity checks complete");
169 if (sanity_diag_mode == 1) {
170 syslog(LOG_INFO, "control: sanity check diagnostic mode is active - exiting now");
177 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
179 long get_new_message_number(void)
182 begin_critical_section(S_CONTROL);
183 retval = CtdlGetConfigLong("MMhighest");
185 CtdlSetConfigLong("MMhighest", retval);
186 end_critical_section(S_CONTROL);
192 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
193 * This provides a quick way to initialise a variable that might be used to indicate
194 * messages that should not be processed. For example, an inbox rules script will use this
195 * to record determine that messages older than this should not be processed.
197 * (Why is this function here? Can't we just go straight to the config variable it fetches?)
199 long CtdlGetCurrentMessageNumber(void)
201 return CtdlGetConfigLong("MMhighest");
206 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
208 long get_new_user_number(void)
211 begin_critical_section(S_CONTROL);
212 retval = CtdlGetConfigLong("MMnextuser");
214 CtdlSetConfigLong("MMnextuser", retval);
215 end_critical_section(S_CONTROL);
221 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
223 long get_new_room_number(void)
226 begin_critical_section(S_CONTROL);
227 retval = CtdlGetConfigLong("MMnextroom");
229 CtdlSetConfigLong("MMnextroom", retval);
230 end_critical_section(S_CONTROL);
236 * Helper function for cmd_conf() to handle boolean values
238 int confbool(char *v)
240 if (IsEmptyStr(v)) return(0);
241 if (atoi(v) != 0) return(1);
247 * Get or set global configuration options
249 void cmd_conf(char *argbuf)
258 if (CtdlAccessCheck(ac_aide)) return;
260 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
262 // CONF GET - retrieve system configuration in legacy format
263 // This is deprecated; please do not add fields or change their order.
264 if (!strcasecmp(cmd, "GET")) {
265 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
266 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
267 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
268 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
269 cprintf("xxx\n"); /* placeholder -- field no longer in use */
270 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
271 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
272 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
273 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
274 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
275 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
276 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
277 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
278 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
279 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
280 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
281 cprintf("xxx\n"); /* placeholder -- field no longer in use */
282 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
283 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
284 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
285 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
286 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
287 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
288 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
289 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
290 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
291 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
292 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
293 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
294 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
295 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
296 cprintf("1\n"); /* niu */
297 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
299 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
300 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
301 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
302 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
303 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
311 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
312 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
313 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
314 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
315 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
316 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
317 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
319 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
320 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
321 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
322 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
323 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
324 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
326 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
331 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
334 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
335 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
336 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
337 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
338 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
339 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
340 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
341 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
342 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
343 cprintf("%d\n", ctdluid);
344 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
345 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
349 // CONF SET - set system configuration in legacy format
350 // This is deprecated; please do not add fields or change their order.
351 else if (!strcasecmp(cmd, "SET")) {
353 cprintf("%d Send configuration...\n", SEND_LISTING);
355 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
358 CtdlSetConfigStr("c_nodename", buf);
361 CtdlSetConfigStr("c_fqdn", buf);
364 CtdlSetConfigStr("c_humannode", buf);
367 /* placeholder -- field no longer in use */
370 CtdlSetConfigInt("c_creataide", confbool(buf));
373 CtdlSetConfigInt("c_sleeping", atoi(buf));
379 CtdlSetConfigInt("c_initax", i);
382 CtdlSetConfigInt("c_regiscall", confbool(buf));
385 CtdlSetConfigInt("c_twitdetect", confbool(buf));
388 CtdlSetConfigStr("c_twitroom", buf);
391 CtdlSetConfigStr("c_moreprompt", buf);
394 CtdlSetConfigInt("c_restrict", confbool(buf));
397 CtdlSetConfigStr("c_site_location", buf);
400 CtdlSetConfigStr("c_sysadm", buf);
405 CtdlSetConfigInt("c_maxsessions", i);
408 /* placeholder -- field no longer in use */
411 CtdlSetConfigInt("c_userpurge", atoi(buf));
414 CtdlSetConfigInt("c_roompurge", atoi(buf));
417 CtdlSetConfigStr("c_logpages", buf);
423 CtdlSetConfigInt("c_createax", i);
428 CtdlSetConfigLong("c_maxmsglen", ii);
433 if (i >= 3) { // minimum value
434 CtdlSetConfigInt("c_min_workers", i);
439 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
440 CtdlSetConfigInt("c_max_workers", i);
444 CtdlSetConfigInt("c_pop3_port", atoi(buf));
447 CtdlSetConfigInt("c_smtp_port", atoi(buf));
450 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
453 CtdlSetConfigInt("c_aide_zap", confbool(buf));
456 CtdlSetConfigInt("c_imap_port", atoi(buf));
459 CtdlSetConfigLong("c_net_freq", atol(buf));
462 CtdlSetConfigInt("c_disable_newu", confbool(buf));
469 if ((i >= 0) && (i <= 23)) {
470 CtdlSetConfigInt("c_purge_hour", i);
474 CtdlSetConfigStr("c_ldap_host", buf);
477 CtdlSetConfigInt("c_ldap_port", atoi(buf));
480 CtdlSetConfigStr("c_ldap_base_dn", buf);
483 CtdlSetConfigStr("c_ldap_bind_dn", buf);
486 CtdlSetConfigStr("c_ldap_bind_pw", buf);
489 CtdlSetConfigStr("c_ip_addr", buf);
492 CtdlSetConfigInt("c_msa_port", atoi(buf));
495 CtdlSetConfigInt("c_imaps_port", atoi(buf));
498 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
501 CtdlSetConfigInt("c_smtps_port", atoi(buf));
504 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
507 CtdlSetConfigInt("c_auto_cull", confbool(buf));
513 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
516 CtdlSetConfigInt("c_journal_email", confbool(buf));
519 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
522 CtdlSetConfigStr("c_journal_dest", buf);
525 CtdlSetConfigStr("c_default_cal_zone", buf);
528 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
534 CtdlSetConfigInt("c_auth_mode", atoi(buf));
549 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
558 CtdlSetConfigStr("c_pager_program", buf);
561 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
564 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
567 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
570 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
573 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
576 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
579 CtdlSetConfigInt("c_guest_logins", confbool(buf));
582 CtdlSetConfigInt("c_port_number", atoi(buf));
588 CtdlSetConfigInt("c_nntp_port", atoi(buf));
591 CtdlSetConfigInt("c_nntps_port", atoi(buf));
596 snprintf(buf, sizeof buf,
597 "The global system configuration has been edited by %s.\n",
598 (CC->logged_in ? CC->curr_user : "an administrator")
600 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
602 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
603 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
605 /* If full text indexing has been disabled, invalidate the
606 * index so it doesn't try to use it later.
608 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
609 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
613 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
614 else if (!strcasecmp(cmd, "GETSYS")) {
615 extract_token(confname, argbuf, 1, '|', sizeof confname);
616 confptr = CtdlGetSysConfig(confname);
617 if (confptr != NULL) {
620 len = strlen(confptr);
621 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
622 client_write(confptr, len);
623 if ((len > 0) && (confptr[len - 1] != 10))
624 client_write("\n", 1);
628 cprintf("%d No such configuration.\n",
629 ERROR + ILLEGAL_VALUE);
633 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
634 else if (!strcasecmp(cmd, "PUTSYS")) {
635 extract_token(confname, argbuf, 1, '|', sizeof confname);
637 cprintf("%d %s\n", SEND_LISTING, confname);
638 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
639 CtdlPutSysConfig(confname, confptr);
643 // CONF GETVAL - retrieve configuration variables from the database
644 // CONF LOADVAL - same thing but can handle variables bigger than 1 KB
645 else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
646 extract_token(confname, argbuf, 1, '|', sizeof confname);
647 char *v = CtdlGetConfigStr(confname);
648 if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
649 cprintf("%d %s|\n", CIT_OK, v);
651 else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
652 cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
653 client_write(v, strlen(v));
656 cprintf("%d |\n", ERROR);
660 // CONF PUTVAL - store configuration variables in the database
661 else if (!strcasecmp(cmd, "PUTVAL")) {
662 if (num_tokens(argbuf, '|') < 3) {
663 cprintf("%d name and value required\n", ERROR);
666 extract_token(confname, argbuf, 1, '|', sizeof confname);
667 extract_token(buf, argbuf, 2, '|', sizeof buf);
668 CtdlSetConfigStr(confname, buf);
669 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
673 // CONF STOREVAL - store configuration variables in the database bigger than 1 KB
674 else if (!strcasecmp(cmd, "STOREVAL")) {
675 if (num_tokens(argbuf, '|') < 3) {
676 cprintf("%d name and length required\n", ERROR);
679 extract_token(confname, argbuf, 1, '|', sizeof confname);
680 int bytes = extract_int(argbuf, 2);
681 char *valbuf = malloc(bytes + 1);
682 cprintf("%d %d\n", SEND_BINARY, bytes);
683 client_read(valbuf, bytes);
685 CtdlSetConfigStr(confname, valbuf);
690 // CONF LISTVAL - list configuration variables in the database and their values
691 else if (!strcasecmp(cmd, "LISTVAL")) {
692 struct cdbdata *cdbcfg;
697 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
698 cdb_rewind(CDB_CONFIG);
699 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
700 if (cdbcfg->len < 1020) {
701 keylen = strlen(cdbcfg->ptr);
703 value = cdbcfg->ptr + keylen + 1;
704 cprintf("%s|%s\n", key, value);
712 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
717 typedef struct __ConfType {
722 ConfType CfgNames[] = {
723 { {HKEY("localhost") }, 0},
724 { {HKEY("directory") }, 0},
725 { {HKEY("smarthost") }, 2},
726 { {HKEY("fallbackhost") }, 2},
727 { {HKEY("rbl") }, 3},
728 { {HKEY("spamassassin") }, 3},
729 { {HKEY("masqdomain") }, 1},
730 { {HKEY("clamav") }, 3},
731 { {HKEY("notify") }, 3},
735 HashList *CfgNameHash = NULL;
736 void cmd_gvdn(char *argbuf)
738 const ConfType *pCfg;
740 long min = atol(argbuf);
741 const char *Pos = NULL;
742 const char *PPos = NULL;
753 List = NewHash(1, NULL);
754 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
755 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
758 confptr = CtdlGetSysConfig(INTERNETCFG);
759 Config = NewStrBufPlain(confptr, -1);
762 Line = NewStrBufPlain(NULL, StrLength(Config));
763 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
764 while (StrBufSipLine(Line, Config, &Pos))
767 Cfg = NewStrBufPlain(NULL, StrLength(Line));
769 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
770 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
771 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
774 pCfg = (ConfType *) vptr;
775 if (pCfg->Type <= min)
777 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
783 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
784 It = GetNewHashPos(List, 1);
785 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
796 FreeStrBuf(&CfgToken);
800 /*****************************************************************************/
801 /* MODULE INITIALIZATION STUFF */
802 /*****************************************************************************/
804 CTDL_MODULE_INIT(control)
809 CfgNameHash = NewHash(1, NULL);
810 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
811 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
813 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
814 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
816 /* return our id for the Log */