2 // This module handles states which are global to the entire server.
4 // Copyright (c) 1987-2022 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;
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
25 struct legacy_ctrl_format {
26 long MMhighest; /* highest message number in file */
27 unsigned MMflags; /* Global system flags */
28 long MMnextuser; /* highest user number on system */
29 long MMnextroom; /* highest room number on system */
30 int MM_hosted_upgrade_level; /* Server-hosted upgrade level */
31 int MM_fulltext_wordbreaker; /* ID of wordbreaker in use */
32 long MMfulltext; /* highest message number indexed */
33 int MMdbversion; /* Version of Berkeley DB used on previous server run */
38 * data that gets passed back and forth between control_find_highest() and its caller
41 long highest_roomnum_found;
42 long highest_msgnum_found;
47 * Callback to get highest room number when rebuilding message base metadata
49 * sanity_diag_mode (can be set by -s flag at startup) may be:
50 * 0 = attempt to fix inconsistencies
51 * 1 = show inconsistencies but don't repair them, exit after complete
52 * 2 = show inconsistencies but don't repair them, continue execution
54 void control_find_highest(struct ctdlroom *qrbuf, void *data) {
55 struct cfh *cfh = (struct cfh *)data;
56 struct cdbdata *cdbfr;
61 if (qrbuf->QRnumber > cfh->highest_roomnum_found) {
62 cfh->highest_roomnum_found = qrbuf->QRnumber;
65 /* Load the message list */
66 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf->QRnumber, sizeof(long));
68 msglist = (long *) cdbfr->ptr;
69 num_msgs = cdbfr->len / sizeof(long);
72 return; /* No messages at all? No further action. */
76 for (c=0; c<num_msgs; c++) {
77 if (msglist[c] > cfh->highest_msgnum_found) {
78 cfh->highest_msgnum_found = msglist[c];
88 * Callback to get highest user number.
90 void control_find_user(char *username, void *out_data) {
91 struct ctdluser EachUser;
93 if (CtdlGetUser(&EachUser, username) != 0) {
97 if (EachUser.usernum > CtdlGetConfigLong("MMnextuser")) {
98 syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld , found in %s",
99 EachUser.usernum, CtdlGetConfigLong("MMnextuser"), EachUser.fullname
101 if (!sanity_diag_mode) {
102 CtdlSetConfigLong("MMnextuser", EachUser.usernum);
109 * If we have a legacy format control record on disk, import it.
111 void migrate_legacy_control_record(void) {
113 struct legacy_ctrl_format c;
114 memset(&c, 0, sizeof(c));
116 fp = fopen("citadel.control", "rb+");
118 syslog(LOG_INFO, "control: legacy format record found -- importing to db");
119 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
121 CtdlSetConfigLong( "MMhighest", c.MMhighest);
122 CtdlSetConfigInt( "MMflags", c.MMflags);
123 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
124 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
125 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
126 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
127 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
130 if (unlink("citadel.control") != 0) {
131 fprintf(stderr, "Unable to remove legacy control record after migrating it.\n");
132 fprintf(stderr, "Exiting to prevent data corruption.\n");
133 exit(CTDLEXIT_CONFIG);
140 * check_control - check the control record has sensible values for message, user and room numbers
142 void check_control(void) {
144 syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
146 memset(&cfh, 0, sizeof(struct cfh));
147 CtdlForEachRoom(control_find_highest, &cfh);
149 if (cfh.highest_roomnum_found > CtdlGetConfigLong("MMnextroom")) {
150 syslog(LOG_DEBUG, "control: fixing MMnextroom %ld > %ld", cfh.highest_roomnum_found, CtdlGetConfigLong("MMnextroom"));
151 if (!sanity_diag_mode) {
152 CtdlSetConfigLong("MMnextroom", cfh.highest_roomnum_found);
156 if (cfh.highest_msgnum_found > CtdlGetConfigLong("MMhighest")) {
157 syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld", cfh.highest_msgnum_found, CtdlGetConfigLong("MMhighest"));
158 if (!sanity_diag_mode) {
159 CtdlSetConfigLong("MMhighest", cfh.highest_msgnum_found);
163 syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
164 ForEachUser(control_find_user, NULL);
166 syslog(LOG_INFO, "control: sanity checks complete");
168 if (sanity_diag_mode == 1) {
169 syslog(LOG_INFO, "control: sanity check diagnostic mode is active - exiting now");
176 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
178 long get_new_message_number(void)
181 begin_critical_section(S_CONTROL);
182 retval = CtdlGetConfigLong("MMhighest");
184 CtdlSetConfigLong("MMhighest", retval);
185 end_critical_section(S_CONTROL);
191 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
192 * This provides a quick way to initialize a variable that might be used to indicate
193 * messages that should not be processed. For example, an inbox rules script will use this
194 * to record determine that messages older than this should not be processed.
196 * (Why is this function here? Can't we just go straight to the config variable it fetches?)
198 long CtdlGetCurrentMessageNumber(void)
200 return CtdlGetConfigLong("MMhighest");
205 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
207 long get_new_user_number(void)
210 begin_critical_section(S_CONTROL);
211 retval = CtdlGetConfigLong("MMnextuser");
213 CtdlSetConfigLong("MMnextuser", retval);
214 end_critical_section(S_CONTROL);
220 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
222 long get_new_room_number(void)
225 begin_critical_section(S_CONTROL);
226 retval = CtdlGetConfigLong("MMnextroom");
228 CtdlSetConfigLong("MMnextroom", retval);
229 end_critical_section(S_CONTROL);
235 * Helper function for cmd_conf() to handle boolean values
237 int confbool(char *v)
239 if (IsEmptyStr(v)) return(0);
240 if (atoi(v) != 0) return(1);
246 * Get or set global configuration options
248 void cmd_conf(char *argbuf)
257 if (CtdlAccessCheck(ac_aide)) return;
259 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
261 // CONF GET - retrieve system configuration in legacy format
262 // This is deprecated; please do not add fields or change their order.
263 if (!strcasecmp(cmd, "GET")) {
264 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
265 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
266 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
267 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
268 cprintf("xxx\n"); /* placeholder -- field no longer in use */
269 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
270 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
271 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
272 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
273 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
274 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
275 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
276 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
277 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
278 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
279 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
280 cprintf("xxx\n"); /* placeholder -- field no longer in use */
281 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
282 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
283 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
284 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
285 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
286 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
287 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
288 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
289 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
290 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
291 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
292 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
293 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
294 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
295 cprintf("1\n"); /* niu */
296 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
297 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
298 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
299 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
300 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
301 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
302 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
303 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
304 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
305 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
306 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
307 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
308 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
310 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
311 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
312 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
313 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
314 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
315 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
317 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
322 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
325 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
326 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
327 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
328 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
329 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
330 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
331 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
332 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
333 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
334 cprintf("%d\n", ctdluid);
335 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
336 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
340 // CONF SET - set system configuration in legacy format
341 // This is deprecated; please do not add fields or change their order.
342 else if (!strcasecmp(cmd, "SET")) {
344 cprintf("%d Send configuration...\n", SEND_LISTING);
346 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
349 CtdlSetConfigStr("c_nodename", buf);
352 CtdlSetConfigStr("c_fqdn", buf);
355 CtdlSetConfigStr("c_humannode", buf);
358 /* placeholder -- field no longer in use */
361 CtdlSetConfigInt("c_creataide", confbool(buf));
364 CtdlSetConfigInt("c_sleeping", atoi(buf));
370 CtdlSetConfigInt("c_initax", i);
373 CtdlSetConfigInt("c_regiscall", confbool(buf));
376 CtdlSetConfigInt("c_twitdetect", confbool(buf));
379 CtdlSetConfigStr("c_twitroom", buf);
382 CtdlSetConfigStr("c_moreprompt", buf);
385 CtdlSetConfigInt("c_restrict", confbool(buf));
388 CtdlSetConfigStr("c_site_location", buf);
391 CtdlSetConfigStr("c_sysadm", buf);
396 CtdlSetConfigInt("c_maxsessions", i);
399 /* placeholder -- field no longer in use */
402 CtdlSetConfigInt("c_userpurge", atoi(buf));
405 CtdlSetConfigInt("c_roompurge", atoi(buf));
408 CtdlSetConfigStr("c_logpages", buf);
414 CtdlSetConfigInt("c_createax", i);
419 CtdlSetConfigLong("c_maxmsglen", ii);
424 if (i >= 3) { // minimum value
425 CtdlSetConfigInt("c_min_workers", i);
430 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
431 CtdlSetConfigInt("c_max_workers", i);
435 CtdlSetConfigInt("c_pop3_port", atoi(buf));
438 CtdlSetConfigInt("c_smtp_port", atoi(buf));
441 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
444 CtdlSetConfigInt("c_aide_zap", confbool(buf));
447 CtdlSetConfigInt("c_imap_port", atoi(buf));
450 CtdlSetConfigLong("c_net_freq", atol(buf));
453 CtdlSetConfigInt("c_disable_newu", confbool(buf));
460 if ((i >= 0) && (i <= 23)) {
461 CtdlSetConfigInt("c_purge_hour", i);
465 CtdlSetConfigStr("c_ldap_host", buf);
468 CtdlSetConfigInt("c_ldap_port", atoi(buf));
471 CtdlSetConfigStr("c_ldap_base_dn", buf);
474 CtdlSetConfigStr("c_ldap_bind_dn", buf);
477 CtdlSetConfigStr("c_ldap_bind_pw", buf);
480 CtdlSetConfigStr("c_ip_addr", buf);
483 CtdlSetConfigInt("c_msa_port", atoi(buf));
486 CtdlSetConfigInt("c_imaps_port", atoi(buf));
489 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
492 CtdlSetConfigInt("c_smtps_port", atoi(buf));
495 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
498 CtdlSetConfigInt("c_auto_cull", confbool(buf));
504 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
507 CtdlSetConfigInt("c_journal_email", confbool(buf));
510 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
513 CtdlSetConfigStr("c_journal_dest", buf);
516 CtdlSetConfigStr("c_default_cal_zone", buf);
519 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
525 CtdlSetConfigInt("c_auth_mode", atoi(buf));
540 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
549 CtdlSetConfigStr("c_pager_program", buf);
552 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
555 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
558 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
561 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
564 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
567 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
570 CtdlSetConfigInt("c_guest_logins", confbool(buf));
573 CtdlSetConfigInt("c_port_number", atoi(buf));
579 CtdlSetConfigInt("c_nntp_port", atoi(buf));
582 CtdlSetConfigInt("c_nntps_port", atoi(buf));
587 snprintf(buf, sizeof buf,
588 "The global system configuration has been edited by %s.\n",
589 (CC->logged_in ? CC->curr_user : "an administrator")
591 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
593 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
594 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
596 /* If full text indexing has been disabled, invalidate the
597 * index so it doesn't try to use it later.
599 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
600 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
604 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
605 else if (!strcasecmp(cmd, "GETSYS")) {
606 extract_token(confname, argbuf, 1, '|', sizeof confname);
607 confptr = CtdlGetSysConfig(confname);
608 if (confptr != NULL) {
611 len = strlen(confptr);
612 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
613 client_write(confptr, len);
614 if ((len > 0) && (confptr[len - 1] != 10))
615 client_write("\n", 1);
619 cprintf("%d No such configuration.\n",
620 ERROR + ILLEGAL_VALUE);
624 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
625 else if (!strcasecmp(cmd, "PUTSYS")) {
626 extract_token(confname, argbuf, 1, '|', sizeof confname);
628 cprintf("%d %s\n", SEND_LISTING, confname);
629 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
630 CtdlPutSysConfig(confname, confptr);
634 // CONF GETVAL - retrieve configuration variables from the database
635 // CONF LOADVAL - same thing but can handle variables bigger than 1 KB (deprecated and probably safe to remove)
636 else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
637 extract_token(confname, argbuf, 1, '|', sizeof confname);
638 char *v = CtdlGetConfigStr(confname);
639 if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
640 cprintf("%d %s|\n", CIT_OK, v);
642 else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
643 cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
644 client_write(v, strlen(v));
647 cprintf("%d |\n", ERROR);
651 // CONF PUTVAL - store configuration variables in the database
652 else if (!strcasecmp(cmd, "PUTVAL")) {
653 if (num_tokens(argbuf, '|') < 3) {
654 cprintf("%d name and value required\n", ERROR);
657 extract_token(confname, argbuf, 1, '|', sizeof confname);
658 extract_token(buf, argbuf, 2, '|', sizeof buf);
659 CtdlSetConfigStr(confname, buf);
660 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
664 // CONF STOREVAL - store configuration variables in the database bigger than 1 KB (deprecated and probably safe to remove)
665 else if (!strcasecmp(cmd, "STOREVAL")) {
666 if (num_tokens(argbuf, '|') < 3) {
667 cprintf("%d name and length required\n", ERROR);
670 extract_token(confname, argbuf, 1, '|', sizeof confname);
671 int bytes = extract_int(argbuf, 2);
672 char *valbuf = malloc(bytes + 1);
673 cprintf("%d %d\n", SEND_BINARY, bytes);
674 client_read(valbuf, bytes);
676 CtdlSetConfigStr(confname, valbuf);
681 // CONF LISTVAL - list configuration variables in the database and their values
682 else if (!strcasecmp(cmd, "LISTVAL")) {
683 struct cdbdata *cdbcfg;
688 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
689 cdb_rewind(CDB_CONFIG);
690 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
691 if (cdbcfg->len < 1020) {
692 keylen = strlen(cdbcfg->ptr);
694 value = cdbcfg->ptr + keylen + 1;
695 cprintf("%s|%s\n", key, value);
703 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
708 typedef struct __ConfType {
713 ConfType CfgNames[] = {
714 { {HKEY("localhost") }, 0},
715 { {HKEY("directory") }, 0},
716 { {HKEY("smarthost") }, 2},
717 { {HKEY("fallbackhost") }, 2},
718 { {HKEY("rbl") }, 3},
719 { {HKEY("spamassassin") }, 3},
720 { {HKEY("masqdomain") }, 1},
721 { {HKEY("clamav") }, 3},
722 { {HKEY("notify") }, 3},
726 HashList *CfgNameHash = NULL;
727 void cmd_gvdn(char *argbuf)
729 const ConfType *pCfg;
731 long min = atol(argbuf);
732 const char *Pos = NULL;
733 const char *PPos = NULL;
744 List = NewHash(1, NULL);
745 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
746 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
749 confptr = CtdlGetSysConfig(INTERNETCFG);
750 Config = NewStrBufPlain(confptr, -1);
753 Line = NewStrBufPlain(NULL, StrLength(Config));
754 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
755 while (StrBufSipLine(Line, Config, &Pos))
758 Cfg = NewStrBufPlain(NULL, StrLength(Line));
760 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
761 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
762 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
765 pCfg = (ConfType *) vptr;
766 if (pCfg->Type <= min)
768 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
774 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
775 It = GetNewHashPos(List, 1);
776 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
787 FreeStrBuf(&CfgToken);
791 /*****************************************************************************/
792 /* MODULE INITIALIZATION STUFF */
793 /*****************************************************************************/
795 char *ctdl_module_init_control(void) {
799 CfgNameHash = NewHash(1, NULL);
800 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
801 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
803 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
804 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
806 /* return our id for the log */