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 CtdlSetConfigLong("MMnextroom", qrbuf->QRnumber);
59 CtdlGetRoom(&room, qrbuf->QRname);
61 /* Load the message list */
62 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
64 msglist = (long *) cdbfr->ptr;
65 num_msgs = cdbfr->len / sizeof(long);
67 return; /* No messages at all? No further action. */
71 for (c=0; c<num_msgs; c++) {
72 if (msglist[c] > CtdlGetConfigLong("MMhighest")) {
73 syslog(LOG_DEBUG, "control: fixing MMhighest %ld > %ld", msglist[c], CtdlGetConfigLong("MMhighest"));
74 CtdlSetConfigLong("MMhighest", msglist[c]);
84 * Callback to get highest user number.
86 void control_find_user(struct ctdluser *EachUser, void *out_data)
88 if (EachUser->usernum > CtdlGetConfigLong("MMnextuser")) {
89 syslog(LOG_DEBUG, "control: fixing MMnextuser %ld > %ld", EachUser->usernum, CtdlGetConfigLong("MMnextuser"));
90 CtdlSetConfigLong("MMnextuser", EachUser->usernum);
96 * If we have a legacy format control record on disk, import it.
98 void migrate_legacy_control_record(void)
101 struct legacy_ctrl_format c;
102 memset(&c, 0, sizeof(c));
104 fp = fopen(file_citadel_control, "rb+");
106 syslog(LOG_INFO, "control: legacy format record found -- importing to db");
107 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
109 CtdlSetConfigLong( "MMhighest", c.MMhighest);
110 CtdlSetConfigInt( "MMflags", c.MMflags);
111 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
112 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
113 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
114 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
115 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
118 if (unlink(file_citadel_control) != 0) {
119 fprintf(stderr, "Unable to remove legacy control record %s after migrating it.\n", file_citadel_control);
120 fprintf(stderr, "Exiting to prevent data corruption.\n");
121 exit(CTDLEXIT_CONFIG);
128 * check_control - check the control record has sensible values for message, user and room numbers
130 void check_control(void)
132 syslog(LOG_INFO, "control: sanity checking the recorded highest message and room numbers");
133 CtdlForEachRoom(control_find_highest, NULL);
134 syslog(LOG_INFO, "control: sanity checking the recorded highest user number");
135 ForEachUser(control_find_user, NULL);
136 syslog(LOG_INFO, "control: sanity checks complete");
141 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
143 long get_new_message_number(void)
146 begin_critical_section(S_CONTROL);
147 retval = CtdlGetConfigLong("MMhighest");
149 CtdlSetConfigLong("MMhighest", retval);
150 end_critical_section(S_CONTROL);
156 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
157 * This provides a quick way to initialise a variable that might be used to indicate
158 * messages that should not be processed. EG. a new Sieve script will use this
159 * to record determine that messages older than this should not be processed.
161 * (Why is this function here? Can't we just go straight to the config variable it fetches?)
163 long CtdlGetCurrentMessageNumber(void)
165 return CtdlGetConfigLong("MMhighest");
170 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
172 long get_new_user_number(void)
175 begin_critical_section(S_CONTROL);
176 retval = CtdlGetConfigLong("MMnextuser");
178 CtdlSetConfigLong("MMnextuser", retval);
179 end_critical_section(S_CONTROL);
185 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
187 long get_new_room_number(void)
190 begin_critical_section(S_CONTROL);
191 retval = CtdlGetConfigLong("MMnextroom");
193 CtdlSetConfigLong("MMnextroom", retval);
194 end_critical_section(S_CONTROL);
200 * Helper function for cmd_conf() to handle boolean values
202 int confbool(char *v)
204 if (IsEmptyStr(v)) return(0);
205 if (atoi(v) != 0) return(1);
211 * Get or set global configuration options
213 * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
214 * http://www.citadel.org/doku.php/documentation:appproto:system_config
217 void cmd_conf(char *argbuf)
226 if (CtdlAccessCheck(ac_aide)) return;
228 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
230 // CONF GET - retrieve system configuration in legacy format
231 // This is deprecated; please do not add fields or change their order.
232 if (!strcasecmp(cmd, "GET")) {
233 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
234 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
235 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
236 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
237 cprintf("xxx\n"); /* placeholder -- field no longer in use */
238 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
239 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
240 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
241 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
242 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
243 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
244 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
245 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
246 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
247 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
248 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
249 cprintf("xxx\n"); /* placeholder -- field no longer in use */
250 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
251 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
252 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
253 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
254 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
255 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
256 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
257 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
258 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
259 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
260 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
261 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
262 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
263 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
264 cprintf("1\n"); /* niu */
265 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
267 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
268 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
269 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
270 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
271 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
279 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
280 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
281 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
282 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
283 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
284 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
285 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
287 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
288 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
289 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
290 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
291 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
292 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
293 cprintf("%d\n", CtdlGetConfigInt("c_managesieve_port"));
294 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
299 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
300 cprintf("%s\n", CtdlGetConfigStr("c_master_user"));
301 cprintf("%s\n", CtdlGetConfigStr("c_master_pass"));
302 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
303 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
304 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
305 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
306 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
307 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
308 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
309 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
310 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
311 cprintf("%d\n", ctdluid);
312 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
313 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
317 // CONF SET - set system configuration in legacy format
318 // This is deprecated; please do not add fields or change their order.
319 else if (!strcasecmp(cmd, "SET")) {
321 cprintf("%d Send configuration...\n", SEND_LISTING);
323 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
326 CtdlSetConfigStr("c_nodename", buf);
329 CtdlSetConfigStr("c_fqdn", buf);
332 CtdlSetConfigStr("c_humannode", buf);
335 /* placeholder -- field no longer in use */
338 CtdlSetConfigInt("c_creataide", confbool(buf));
341 CtdlSetConfigInt("c_sleeping", atoi(buf));
347 CtdlSetConfigInt("c_initax", i);
350 CtdlSetConfigInt("c_regiscall", confbool(buf));
353 CtdlSetConfigInt("c_twitdetect", confbool(buf));
356 CtdlSetConfigStr("c_twitroom", buf);
359 CtdlSetConfigStr("c_moreprompt", buf);
362 CtdlSetConfigInt("c_restrict", confbool(buf));
365 CtdlSetConfigStr("c_site_location", buf);
368 CtdlSetConfigStr("c_sysadm", buf);
373 CtdlSetConfigInt("c_maxsessions", i);
376 /* placeholder -- field no longer in use */
379 CtdlSetConfigInt("c_userpurge", atoi(buf));
382 CtdlSetConfigInt("c_roompurge", atoi(buf));
385 CtdlSetConfigStr("c_logpages", buf);
391 CtdlSetConfigInt("c_createax", i);
396 CtdlSetConfigLong("c_maxmsglen", ii);
401 if (i >= 3) { // minimum value
402 CtdlSetConfigInt("c_min_workers", i);
407 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
408 CtdlSetConfigInt("c_max_workers", i);
412 CtdlSetConfigInt("c_pop3_port", atoi(buf));
415 CtdlSetConfigInt("c_smtp_port", atoi(buf));
418 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
421 CtdlSetConfigInt("c_aide_zap", confbool(buf));
424 CtdlSetConfigInt("c_imap_port", atoi(buf));
427 CtdlSetConfigLong("c_net_freq", atol(buf));
430 CtdlSetConfigInt("c_disable_newu", confbool(buf));
437 if ((i >= 0) && (i <= 23)) {
438 CtdlSetConfigInt("c_purge_hour", i);
442 CtdlSetConfigStr("c_ldap_host", buf);
445 CtdlSetConfigInt("c_ldap_port", atoi(buf));
448 CtdlSetConfigStr("c_ldap_base_dn", buf);
451 CtdlSetConfigStr("c_ldap_bind_dn", buf);
454 CtdlSetConfigStr("c_ldap_bind_pw", buf);
457 CtdlSetConfigStr("c_ip_addr", buf);
460 CtdlSetConfigInt("c_msa_port", atoi(buf));
463 CtdlSetConfigInt("c_imaps_port", atoi(buf));
466 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
469 CtdlSetConfigInt("c_smtps_port", atoi(buf));
472 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
475 CtdlSetConfigInt("c_auto_cull", confbool(buf));
481 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
484 CtdlSetConfigInt("c_journal_email", confbool(buf));
487 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
490 CtdlSetConfigStr("c_journal_dest", buf);
493 CtdlSetConfigStr("c_default_cal_zone", buf);
496 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
499 CtdlSetConfigInt("c_managesieve_port", atoi(buf));
502 CtdlSetConfigInt("c_auth_mode", atoi(buf));
517 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
520 CtdlSetConfigStr("c_master_user", buf);
523 CtdlSetConfigStr("c_master_pass", buf);
526 CtdlSetConfigStr("c_pager_program", buf);
529 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
532 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
535 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
538 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
541 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
544 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
547 CtdlSetConfigInt("c_guest_logins", confbool(buf));
550 CtdlSetConfigInt("c_port_number", atoi(buf));
556 CtdlSetConfigInt("c_nntp_port", atoi(buf));
559 CtdlSetConfigInt("c_nntps_port", atoi(buf));
564 snprintf(buf, sizeof buf,
565 "The global system configuration has been edited by %s.\n",
566 (CC->logged_in ? CC->curr_user : "an administrator")
568 CtdlAideMessage(buf, "Citadel Configuration Manager Message");
570 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
571 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
573 /* If full text indexing has been disabled, invalidate the
574 * index so it doesn't try to use it later.
576 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
577 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
581 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
582 else if (!strcasecmp(cmd, "GETSYS")) {
583 extract_token(confname, argbuf, 1, '|', sizeof confname);
584 confptr = CtdlGetSysConfig(confname);
585 if (confptr != NULL) {
588 len = strlen(confptr);
589 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
590 client_write(confptr, len);
591 if ((len > 0) && (confptr[len - 1] != 10))
592 client_write("\n", 1);
596 cprintf("%d No such configuration.\n",
597 ERROR + ILLEGAL_VALUE);
601 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
602 else if (!strcasecmp(cmd, "PUTSYS")) {
603 extract_token(confname, argbuf, 1, '|', sizeof confname);
605 cprintf("%d %s\n", SEND_LISTING, confname);
606 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
607 CtdlPutSysConfig(confname, confptr);
611 // CONF GETVAL - retrieve configuration variables from the database
612 // CONF LOADVAL - same thing but can handle variables bigger than 1 KB
613 else if ( (!strcasecmp(cmd, "GETVAL")) || (!strcasecmp(cmd, "LOADVAL")) ) {
614 extract_token(confname, argbuf, 1, '|', sizeof confname);
615 char *v = CtdlGetConfigStr(confname);
616 if ( (v) && (!strcasecmp(cmd, "GETVAL")) ) {
617 cprintf("%d %s|\n", CIT_OK, v);
619 else if ( (v) && (!strcasecmp(cmd, "LOADVAL")) ) {
620 cprintf("%d %d\n", BINARY_FOLLOWS, (int)strlen(v));
621 client_write(v, strlen(v));
624 cprintf("%d |\n", ERROR);
628 // CONF PUTVAL - store configuration variables in the database
629 else if (!strcasecmp(cmd, "PUTVAL")) {
630 if (num_tokens(argbuf, '|') < 3) {
631 cprintf("%d name and value required\n", ERROR);
634 extract_token(confname, argbuf, 1, '|', sizeof confname);
635 extract_token(buf, argbuf, 2, '|', sizeof buf);
636 CtdlSetConfigStr(confname, buf);
637 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
641 // CONF STOREVAL - store configuration variables in the database bigger than 1 KB
642 else if (!strcasecmp(cmd, "STOREVAL")) {
643 if (num_tokens(argbuf, '|') < 3) {
644 cprintf("%d name and length required\n", ERROR);
647 extract_token(confname, argbuf, 1, '|', sizeof confname);
648 int bytes = extract_int(argbuf, 2);
649 char *valbuf = malloc(bytes + 1);
650 cprintf("%d %d\n", SEND_BINARY, bytes);
651 client_read(valbuf, bytes);
653 CtdlSetConfigStr(confname, valbuf);
658 // CONF LISTVAL - list configuration variables in the database and their values
659 else if (!strcasecmp(cmd, "LISTVAL")) {
660 struct cdbdata *cdbcfg;
665 cprintf("%d all configuration variables\n", LISTING_FOLLOWS);
666 cdb_rewind(CDB_CONFIG);
667 while (cdbcfg = cdb_next_item(CDB_CONFIG), cdbcfg != NULL) {
668 if (cdbcfg->len < 1020) {
669 keylen = strlen(cdbcfg->ptr);
671 value = cdbcfg->ptr + keylen + 1;
672 cprintf("%s|%s\n", key, value);
680 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
685 typedef struct __ConfType {
690 ConfType CfgNames[] = {
691 { {HKEY("localhost") }, 0},
692 { {HKEY("directory") }, 0},
693 { {HKEY("smarthost") }, 2},
694 { {HKEY("fallbackhost") }, 2},
695 { {HKEY("rbl") }, 3},
696 { {HKEY("spamassassin") }, 3},
697 { {HKEY("masqdomain") }, 1},
698 { {HKEY("clamav") }, 3},
699 { {HKEY("notify") }, 3},
703 HashList *CfgNameHash = NULL;
704 void cmd_gvdn(char *argbuf)
706 const ConfType *pCfg;
708 long min = atol(argbuf);
709 const char *Pos = NULL;
710 const char *PPos = NULL;
721 List = NewHash(1, NULL);
722 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
723 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
726 confptr = CtdlGetSysConfig(INTERNETCFG);
727 Config = NewStrBufPlain(confptr, -1);
730 Line = NewStrBufPlain(NULL, StrLength(Config));
731 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
732 while (StrBufSipLine(Line, Config, &Pos))
735 Cfg = NewStrBufPlain(NULL, StrLength(Line));
737 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
738 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
739 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
742 pCfg = (ConfType *) vptr;
743 if (pCfg->Type <= min)
745 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
751 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
752 It = GetNewHashPos(List, 1);
753 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
764 FreeStrBuf(&CfgToken);
768 /*****************************************************************************/
769 /* MODULE INITIALIZATION STUFF */
770 /*****************************************************************************/
772 void control_cleanup(void)
774 DeleteHash(&CfgNameHash);
776 CTDL_MODULE_INIT(control)
781 CfgNameHash = NewHash(1, NULL);
782 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
783 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
785 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
786 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
787 CtdlRegisterCleanupHook(control_cleanup);
790 /* return our id for the Log */