2 * This module handles states which are global to the entire server.
4 * Copyright (c) 1987-2018 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 #include <libcitadel.h>
19 #include "ctdl_module.h"
21 #include "citserver.h"
24 long control_highest_user = 0;
27 * This is the legacy "control record" format for the message base. If found
28 * on disk, its contents will be migrated into the system configuration. Never
31 struct legacy_ctrl_format {
32 long MMhighest; /* highest message number in file */
33 unsigned MMflags; /* Global system flags */
34 long MMnextuser; /* highest user number on system */
35 long MMnextroom; /* highest room number on system */
36 int MM_hosted_upgrade_level; /* Server-hosted upgrade level */
37 int MM_fulltext_wordbreaker; /* ID of wordbreaker in use */
38 long MMfulltext; /* highest message number indexed */
39 int MMdbversion; /* Version of Berkeley DB used on previous server run */
44 * Callback to get highest room number when rebuilding message base metadata
46 void control_find_highest(struct ctdlroom *qrbuf, void *data)
49 struct cdbdata *cdbfr;
54 if (qrbuf->QRnumber > CtdlGetConfigLong("MMnextroom")) {
55 syslog(LOG_DEBUG, "control: fixing MMnextroom %ld > %ld", qrbuf->QRnumber, CtdlGetConfigLong("MMnextroom"));
56 if (!sanity_diag_mode) {
57 CtdlSetConfigLong("MMnextroom", qrbuf->QRnumber);
61 CtdlGetRoom(&room, qrbuf->QRname);
63 /* Load the message list */
64 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
66 msglist = (long *) cdbfr->ptr;
67 num_msgs = cdbfr->len / sizeof(long);
69 return; /* No messages at all? No further action. */
73 for (c=0; c<num_msgs; c++) {
74 if (msglist[c] > CtdlGetConfigLong("MMhighest")) {
75 syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld", msglist[c], CtdlGetConfigLong("MMhighest"));
76 if (!sanity_diag_mode) {
77 CtdlSetConfigLong("MMhighest", msglist[c]);
88 * Callback to get highest user number.
90 void control_find_user(struct ctdluser *EachUser, void *out_data)
92 if (EachUser->usernum > CtdlGetConfigLong("MMnextuser")) {
93 syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld", EachUser->usernum, CtdlGetConfigLong("MMnextuser"));
94 if (!sanity_diag_mode) {
95 CtdlSetConfigLong("MMnextuser", EachUser->usernum);
102 * If we have a legacy format control record on disk, import it.
104 void migrate_legacy_control_record(void)
107 struct legacy_ctrl_format c;
108 memset(&c, 0, sizeof(c));
110 fp = fopen(file_citadel_control, "rb+");
112 syslog(LOG_INFO, "control: legacy format record found -- importing to db");
113 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
115 CtdlSetConfigLong( "MMhighest", c.MMhighest);
116 CtdlSetConfigInt( "MMflags", c.MMflags);
117 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
118 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
119 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
120 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
121 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
124 if (unlink(file_citadel_control) != 0) {
125 fprintf(stderr, "Unable to remove legacy control record %s after migrating it.\n", file_citadel_control);
126 fprintf(stderr, "Exiting to prevent data corruption.\n");
127 exit(CTDLEXIT_CONFIG);
134 * check_control - check the control record has sensible values for message, user and room numbers
136 void check_control(void)
138 syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
139 CtdlForEachRoom(control_find_highest, NULL);
140 syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
141 ForEachUser(control_find_user, NULL);
142 syslog(LOG_INFO, "control: sanity checks complete");
143 if (sanity_diag_mode) {
144 syslog(LOG_INFO, "control: sanity check diagnostic mode is active - exiting now");
151 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
153 long get_new_message_number(void)
156 begin_critical_section(S_CONTROL);
157 retval = CtdlGetConfigLong("MMhighest");
159 CtdlSetConfigLong("MMhighest", retval);
160 end_critical_section(S_CONTROL);
166 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
167 * This provides a quick way to initialise a variable that might be used to indicate
168 * messages that should not be processed. EG. a new Sieve 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?)
173 long CtdlGetCurrentMessageNumber(void)
175 return CtdlGetConfigLong("MMhighest");
180 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
182 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);
195 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
197 long get_new_room_number(void)
200 begin_critical_section(S_CONTROL);
201 retval = CtdlGetConfigLong("MMnextroom");
203 CtdlSetConfigLong("MMnextroom", retval);
204 end_critical_section(S_CONTROL);
210 * Helper function for cmd_conf() to handle boolean values
212 int confbool(char *v)
214 if (IsEmptyStr(v)) return(0);
215 if (atoi(v) != 0) return(1);
221 * Get or set global configuration options
223 * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
224 * http://www.citadel.org/doku.php/documentation:appproto:system_config
227 void cmd_conf(char *argbuf)
236 if (CtdlAccessCheck(ac_aide)) return;
238 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
240 // CONF GET - retrieve system configuration in legacy format
241 // This is deprecated; please do not add fields or change their order.
242 if (!strcasecmp(cmd, "GET")) {
243 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
244 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
245 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
246 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
247 cprintf("xxx\n"); /* placeholder -- field no longer in use */
248 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
249 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
250 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
251 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
252 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
253 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
254 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
255 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
256 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
257 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
258 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
259 cprintf("xxx\n"); /* placeholder -- field no longer in use */
260 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
261 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
262 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
263 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
264 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
265 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
266 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
267 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
268 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
269 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
270 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
271 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
272 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
273 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
274 cprintf("1\n"); /* niu */
275 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
277 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
278 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
279 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
280 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
281 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
289 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
290 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
291 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
292 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
293 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
294 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
295 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
297 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
298 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
299 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
300 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
301 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
302 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
303 cprintf("%d\n", CtdlGetConfigInt("c_managesieve_port"));
304 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
309 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
310 cprintf("%s\n", CtdlGetConfigStr("c_master_user"));
311 cprintf("%s\n", CtdlGetConfigStr("c_master_pass"));
312 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
313 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
314 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
315 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
316 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
317 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
318 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
319 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
320 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
321 cprintf("%d\n", ctdluid);
322 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
323 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
327 // CONF SET - set system configuration in legacy format
328 // This is deprecated; please do not add fields or change their order.
329 else if (!strcasecmp(cmd, "SET")) {
331 cprintf("%d Send configuration...\n", SEND_LISTING);
333 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
336 CtdlSetConfigStr("c_nodename", buf);
339 CtdlSetConfigStr("c_fqdn", buf);
342 CtdlSetConfigStr("c_humannode", buf);
345 /* placeholder -- field no longer in use */
348 CtdlSetConfigInt("c_creataide", confbool(buf));
351 CtdlSetConfigInt("c_sleeping", atoi(buf));
357 CtdlSetConfigInt("c_initax", i);
360 CtdlSetConfigInt("c_regiscall", confbool(buf));
363 CtdlSetConfigInt("c_twitdetect", confbool(buf));
366 CtdlSetConfigStr("c_twitroom", buf);
369 CtdlSetConfigStr("c_moreprompt", buf);
372 CtdlSetConfigInt("c_restrict", confbool(buf));
375 CtdlSetConfigStr("c_site_location", buf);
378 CtdlSetConfigStr("c_sysadm", buf);
383 CtdlSetConfigInt("c_maxsessions", i);
386 /* placeholder -- field no longer in use */
389 CtdlSetConfigInt("c_userpurge", atoi(buf));
392 CtdlSetConfigInt("c_roompurge", atoi(buf));
395 CtdlSetConfigStr("c_logpages", buf);
401 CtdlSetConfigInt("c_createax", i);
406 CtdlSetConfigLong("c_maxmsglen", ii);
411 if (i >= 3) { // minimum value
412 CtdlSetConfigInt("c_min_workers", i);
417 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
418 CtdlSetConfigInt("c_max_workers", i);
422 CtdlSetConfigInt("c_pop3_port", atoi(buf));
425 CtdlSetConfigInt("c_smtp_port", atoi(buf));
428 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
431 CtdlSetConfigInt("c_aide_zap", confbool(buf));
434 CtdlSetConfigInt("c_imap_port", atoi(buf));
437 CtdlSetConfigLong("c_net_freq", atol(buf));
440 CtdlSetConfigInt("c_disable_newu", confbool(buf));
447 if ((i >= 0) && (i <= 23)) {
448 CtdlSetConfigInt("c_purge_hour", i);
452 CtdlSetConfigStr("c_ldap_host", buf);
455 CtdlSetConfigInt("c_ldap_port", atoi(buf));
458 CtdlSetConfigStr("c_ldap_base_dn", buf);
461 CtdlSetConfigStr("c_ldap_bind_dn", buf);
464 CtdlSetConfigStr("c_ldap_bind_pw", buf);
467 CtdlSetConfigStr("c_ip_addr", buf);
470 CtdlSetConfigInt("c_msa_port", atoi(buf));
473 CtdlSetConfigInt("c_imaps_port", atoi(buf));
476 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
479 CtdlSetConfigInt("c_smtps_port", atoi(buf));
482 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
485 CtdlSetConfigInt("c_auto_cull", confbool(buf));
491 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
494 CtdlSetConfigInt("c_journal_email", confbool(buf));
497 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
500 CtdlSetConfigStr("c_journal_dest", buf);
503 CtdlSetConfigStr("c_default_cal_zone", buf);
506 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
509 CtdlSetConfigInt("c_managesieve_port", atoi(buf));
512 CtdlSetConfigInt("c_auth_mode", atoi(buf));
527 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
530 CtdlSetConfigStr("c_master_user", buf);
533 CtdlSetConfigStr("c_master_pass", buf);
536 CtdlSetConfigStr("c_pager_program", buf);
539 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
542 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
545 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
548 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
551 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
554 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
557 CtdlSetConfigInt("c_guest_logins", confbool(buf));
560 CtdlSetConfigInt("c_port_number", atoi(buf));
566 CtdlSetConfigInt("c_nntp_port", atoi(buf));
569 CtdlSetConfigInt("c_nntps_port", atoi(buf));
574 snprintf(buf, sizeof buf,
575 "The global system configuration has been edited by %s.\n",
576 (CC->logged_in ? CC->curr_user : "an administrator")
578 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
580 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
581 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
583 /* If full text indexing has been disabled, invalidate the
584 * index so it doesn't try to use it later.
586 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
587 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
591 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
592 else if (!strcasecmp(cmd, "GETSYS")) {
593 extract_token(confname, argbuf, 1, '|', sizeof confname);
594 confptr = CtdlGetSysConfig(confname);
595 if (confptr != NULL) {
598 len = strlen(confptr);
599 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
600 client_write(confptr, len);
601 if ((len > 0) && (confptr[len - 1] != 10))
602 client_write("\n", 1);
606 cprintf("%d No such configuration.\n",
607 ERROR + ILLEGAL_VALUE);
611 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
612 else if (!strcasecmp(cmd, "PUTSYS")) {
613 extract_token(confname, argbuf, 1, '|', sizeof confname);
615 cprintf("%d %s\n", SEND_LISTING, confname);
616 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
617 CtdlPutSysConfig(confname, confptr);
621 // CONF GETVAL - retrieve configuration variables from the database
622 // CONF LOADVAL - same thing but can handle variables bigger than 1 KB
623 else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
624 extract_token(confname, argbuf, 1, '|', sizeof confname);
625 char *v = CtdlGetConfigStr(confname);
626 if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
627 cprintf("%d %s|\n", CIT_OK, v);
629 else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
630 cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
631 client_write(v, strlen(v));
634 cprintf("%d |\n", ERROR);
638 // CONF PUTVAL - store configuration variables in the database
639 else if (!strcasecmp(cmd, "PUTVAL")) {
640 if (num_tokens(argbuf, '|') < 3) {
641 cprintf("%d name and value required\n", ERROR);
644 extract_token(confname, argbuf, 1, '|', sizeof confname);
645 extract_token(buf, argbuf, 2, '|', sizeof buf);
646 CtdlSetConfigStr(confname, buf);
647 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
651 // CONF STOREVAL - store configuration variables in the database bigger than 1 KB
652 else if (!strcasecmp(cmd, "STOREVAL")) {
653 if (num_tokens(argbuf, '|') < 3) {
654 cprintf("%d name and length required\n", ERROR);
657 extract_token(confname, argbuf, 1, '|', sizeof confname);
658 int bytes = extract_int(argbuf, 2);
659 char *valbuf = malloc(bytes + 1);
660 cprintf("%d %d\n", SEND_BINARY, bytes);
661 client_read(valbuf, bytes);
663 CtdlSetConfigStr(confname, valbuf);
668 // CONF LISTVAL - list configuration variables in the database and their values
669 else if (!strcasecmp(cmd, "LISTVAL")) {
670 struct cdbdata *cdbcfg;
675 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
676 cdb_rewind(CDB_CONFIG);
677 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
678 if (cdbcfg->len < 1020) {
679 keylen = strlen(cdbcfg->ptr);
681 value = cdbcfg->ptr + keylen + 1;
682 cprintf("%s|%s\n", key, value);
690 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
695 typedef struct __ConfType {
700 ConfType CfgNames[] = {
701 { {HKEY("localhost") }, 0},
702 { {HKEY("directory") }, 0},
703 { {HKEY("smarthost") }, 2},
704 { {HKEY("fallbackhost") }, 2},
705 { {HKEY("rbl") }, 3},
706 { {HKEY("spamassassin") }, 3},
707 { {HKEY("masqdomain") }, 1},
708 { {HKEY("clamav") }, 3},
709 { {HKEY("notify") }, 3},
713 HashList *CfgNameHash = NULL;
714 void cmd_gvdn(char *argbuf)
716 const ConfType *pCfg;
718 long min = atol(argbuf);
719 const char *Pos = NULL;
720 const char *PPos = NULL;
731 List = NewHash(1, NULL);
732 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
733 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
736 confptr = CtdlGetSysConfig(INTERNETCFG);
737 Config = NewStrBufPlain(confptr, -1);
740 Line = NewStrBufPlain(NULL, StrLength(Config));
741 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
742 while (StrBufSipLine(Line, Config, &Pos))
745 Cfg = NewStrBufPlain(NULL, StrLength(Line));
747 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
748 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
749 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
752 pCfg = (ConfType *) vptr;
753 if (pCfg->Type <= min)
755 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
761 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
762 It = GetNewHashPos(List, 1);
763 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
774 FreeStrBuf(&CfgToken);
778 /*****************************************************************************/
779 /* MODULE INITIALIZATION STUFF */
780 /*****************************************************************************/
782 void control_cleanup(void)
784 DeleteHash(&CfgNameHash);
786 CTDL_MODULE_INIT(control)
791 CfgNameHash = NewHash(1, NULL);
792 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
793 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
795 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
796 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
797 CtdlRegisterCleanupHook(control_cleanup);
800 /* return our id for the Log */