2 * This module handles states which are global to the entire server.
4 * Copyright (c) 1987-2015 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 control record for the message base...
29 struct legacy_ctrl_format {
30 long MMhighest; /* highest message number in file */
31 unsigned MMflags; /* Global system flags */
32 long MMnextuser; /* highest user number on system */
33 long MMnextroom; /* highest room number on system */
34 int MM_hosted_upgrade_level; /* Server-hosted upgrade level */
35 int MM_fulltext_wordbreaker; /* ID of wordbreaker in use */
36 long MMfulltext; /* highest message number indexed */
37 int MMdbversion; /* Version of Berkeley DB used on previous server run */
43 * callback to get highest room number when rebuilding control file
45 void control_find_highest(struct ctdlroom *qrbuf, void *data)
48 struct cdbdata *cdbfr;
53 int message_fixed = 0;
55 if (qrbuf->QRnumber > CtdlGetConfigLong("MMnextroom"))
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. */
74 for (c=0; c<num_msgs; c++)
76 if (msglist[c] > CtdlGetConfigLong("MMhighest"))
78 CtdlSetConfigLong("MMhighest", msglist[c]);
85 syslog(LOG_INFO, "Control record checking....Fixed room counter\n");
88 syslog(LOG_INFO, "Control record checking....Fixed message count\n");
95 * Callback to get highest user number.
98 void control_find_user (struct ctdluser *EachUser, void *out_data)
102 if (EachUser->usernum > CtdlGetConfigLong("MMnextuser"))
104 CtdlSetConfigLong("MMnextuser", EachUser->usernum);
108 syslog(LOG_INFO, "Control record checking....Fixed user count\n");
113 * If we have a legacy format control record on disk, import it.
115 void migrate_legacy_control_record(void)
118 struct legacy_ctrl_format c;
119 memset(&c, 0, sizeof(c));
121 fp = fopen(file_citadel_control, "rb+");
123 syslog(LOG_INFO, "Legacy format control record found -- importing to db");
124 fread(&c, sizeof(struct legacy_ctrl_format), 1, fp);
126 CtdlSetConfigLong( "MMhighest", c.MMhighest);
127 CtdlSetConfigInt( "MMflags", c.MMflags);
128 CtdlSetConfigLong( "MMnextuser", c.MMnextuser);
129 CtdlSetConfigLong( "MMnextroom", c.MMnextroom);
130 CtdlSetConfigInt( "MM_hosted_upgrade_level", c.MM_hosted_upgrade_level);
131 CtdlSetConfigInt( "MM_fulltext_wordbreaker", c.MM_fulltext_wordbreaker);
132 CtdlSetConfigLong( "MMfulltext", c.MMfulltext);
135 if (unlink(file_citadel_control) != 0) {
136 fprintf(stderr, "Unable to remove legacy control record %s after migrating it.\n", file_citadel_control);
137 fprintf(stderr, "Exiting to prevent data corruption.\n");
138 exit(CTDLEXIT_CONFIG);
145 * check_control - check the control record has sensible values for message, user and room numbers
147 void check_control(void)
149 syslog(LOG_INFO, "Sanity checking the recorded highest message, user, and room numbers\n");
150 CtdlForEachRoom(control_find_highest, NULL);
151 ForEachUser(control_find_user, NULL);
157 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
159 long get_new_message_number(void)
162 begin_critical_section(S_CONTROL);
163 retval = CtdlGetConfigLong("MMhighest");
165 CtdlSetConfigLong("MMhighest", retval);
166 end_critical_section(S_CONTROL);
172 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
173 * This provides a quick way to initialise a variable that might be used to indicate
174 * messages that should not be processed. EG. a new Sieve script will use this
175 * to record determine that messages older than this should not be processed.
177 * (Why is this function here? Can't we just go straight to the config variable it fetches?)
179 long CtdlGetCurrentMessageNumber(void)
181 return CtdlGetConfigLong("MMhighest");
186 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
188 long get_new_user_number(void)
191 begin_critical_section(S_CONTROL);
192 retval = CtdlGetConfigLong("MMnextuser");
194 CtdlSetConfigLong("MMnextuser", retval);
195 end_critical_section(S_CONTROL);
202 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
204 long get_new_room_number(void)
207 begin_critical_section(S_CONTROL);
208 retval = CtdlGetConfigLong("MMnextroom");
210 CtdlSetConfigLong("MMnextroom", retval);
211 end_critical_section(S_CONTROL);
218 * Helper function for cmd_conf() to handle boolean values
220 int confbool(char *v)
222 if (IsEmptyStr(v)) return(0);
223 if (atoi(v) != 0) return(1);
229 * Get or set global configuration options
231 * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
232 * http://www.citadel.org/doku.php/documentation:appproto:system_config
235 void cmd_conf(char *argbuf)
244 if (CtdlAccessCheck(ac_aide)) return;
246 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
248 // CONF GET - retrieve system configuration in legacy format (deprecated)
249 if (!strcasecmp(cmd, "GET")) {
250 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
251 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
252 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
253 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
254 cprintf("xxx\n"); /* placeholder -- field no longer in use */
255 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
256 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
257 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
258 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
259 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
260 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
261 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
262 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
263 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
264 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
265 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
266 cprintf("xxx\n"); /* placeholder -- field no longer in use */
267 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
268 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
269 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
270 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
271 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
272 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
273 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
274 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
275 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
276 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
277 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
278 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
279 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
280 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
281 cprintf("1\n"); /* niu */
282 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
284 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
285 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
286 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
287 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
288 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
296 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
297 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
298 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
299 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
300 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
301 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
302 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
304 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
305 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
306 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
307 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
308 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
309 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
310 cprintf("%d\n", CtdlGetConfigInt("c_managesieve_port"));
311 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
312 cprintf("%s\n", CtdlGetConfigStr("c_funambol_host"));
313 cprintf("%d\n", CtdlGetConfigInt("c_funambol_port"));
314 cprintf("%s\n", CtdlGetConfigStr("c_funambol_source"));
315 cprintf("%s\n", CtdlGetConfigStr("c_funambol_auth"));
316 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
317 cprintf("%s\n", CtdlGetConfigStr("c_master_user"));
318 cprintf("%s\n", CtdlGetConfigStr("c_master_pass"));
319 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
320 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
321 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
322 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
323 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
324 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
325 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
326 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
327 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
328 cprintf("%d\n", ctdluid);
329 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
330 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
334 // CONF SET - set system configuration in legacy format (really deprecated)
335 else if (!strcasecmp(cmd, "SET")) {
337 cprintf("%d Send configuration...\n", SEND_LISTING);
339 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
342 CtdlSetConfigStr("c_nodename", buf);
345 CtdlSetConfigStr("c_fqdn", buf);
348 CtdlSetConfigStr("c_humannode", buf);
351 /* placeholder -- field no longer in use */
354 CtdlSetConfigInt("c_creataide", atoi(buf));
357 CtdlSetConfigInt("c_sleeping", atoi(buf));
363 CtdlSetConfigInt("c_initax", i);
366 CtdlSetConfigInt("c_regiscall", confbool(buf));
369 CtdlSetConfigInt("c_twitdetect", confbool(buf));
372 CtdlSetConfigStr("c_twitroom", buf);
375 CtdlSetConfigStr("c_moreprompt", buf);
378 CtdlSetConfigInt("c_restrict", confbool(buf));
381 CtdlSetConfigInt("c_site_location", confbool(buf));
384 CtdlSetConfigInt("c_sysadm", confbool(buf));
389 CtdlSetConfigInt("c_maxsessions", i);
392 /* placeholder -- field no longer in use */
395 CtdlSetConfigInt("c_userpurge", atoi(buf));
398 CtdlSetConfigInt("c_roompurge", atoi(buf));
401 CtdlSetConfigStr("c_logpages", buf);
407 CtdlSetConfigInt("c_createax", i);
412 CtdlSetConfigLong("c_maxmsglen", ii);
417 if (i >= 3) { // minimum value
418 CtdlSetConfigInt("c_min_workers", i);
423 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
424 CtdlSetConfigInt("c_max_workers", i);
428 CtdlSetConfigInt("c_pop3_port", atoi(buf));
431 CtdlSetConfigInt("c_smtp_port", atoi(buf));
434 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
437 CtdlSetConfigInt("c_aide_zap", confbool(buf));
440 CtdlSetConfigInt("c_imap_port", atoi(buf));
443 CtdlSetConfigLong("c_net_freq", atol(buf));
446 CtdlSetConfigInt("c_disable_newu", confbool(buf));
453 if ((i >= 0) && (i <= 23)) {
454 CtdlSetConfigInt("c_purge_hour", i);
458 CtdlSetConfigStr("c_ldap_host", buf);
461 CtdlSetConfigInt("c_ldap_port", atoi(buf));
464 CtdlSetConfigStr("c_ldap_base_dn", buf);
467 CtdlSetConfigStr("c_ldap_bind_dn", buf);
470 CtdlSetConfigStr("c_ldap_bind_pw", buf);
473 CtdlSetConfigStr("c_ip_addr", buf);
476 CtdlSetConfigInt("c_msa_port", atoi(buf));
479 CtdlSetConfigInt("c_imaps_port", atoi(buf));
482 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
485 CtdlSetConfigInt("c_smtps_port", atoi(buf));
488 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
491 CtdlSetConfigInt("c_auto_cull", confbool(buf));
497 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
500 CtdlSetConfigInt("c_journal_email", confbool(buf));
503 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
506 CtdlSetConfigStr("c_journal_dest", buf);
509 CtdlSetConfigStr("c_default_cal_zone", buf);
512 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
515 CtdlSetConfigInt("c_managesieve_port", atoi(buf));
518 CtdlSetConfigInt("c_auth_mode", atoi(buf));
521 CtdlSetConfigStr("c_funambol_host", buf);
524 CtdlSetConfigInt("c_funambol_port", atoi(buf));
527 CtdlSetConfigStr("c_funambol_source", buf);
530 CtdlSetConfigStr("c_funambol_auth", buf);
533 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
536 CtdlSetConfigStr("c_master_user", buf);
539 CtdlSetConfigStr("c_master_pass", buf);
542 CtdlSetConfigStr("c_pager_program", buf);
545 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
548 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
551 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
554 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
557 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
560 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
563 CtdlSetConfigInt("c_guest_logins", confbool(buf));
566 CtdlSetConfigInt("c_port_number", atoi(buf));
572 CtdlSetConfigInt("c_nntp_port", atoi(buf));
575 CtdlSetConfigInt("c_nntps_port", atoi(buf));
580 snprintf(buf, sizeof buf,
581 "The global system configuration has been edited by %s.\n",
582 (CC->logged_in ? CC->curr_user : "an administrator")
584 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
586 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
587 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
589 /* If full text indexing has been disabled, invalidate the
590 * index so it doesn't try to use it later.
592 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
593 CtdlSetConfigInt("MM_fulltext_wordbreaker", 0);
597 // CONF GETSYS - retrieve arbitrary system configuration stanzas stored in the message base
598 else if (!strcasecmp(cmd, "GETSYS")) {
599 extract_token(confname, argbuf, 1, '|', sizeof confname);
600 confptr = CtdlGetSysConfig(confname);
601 if (confptr != NULL) {
604 len = strlen(confptr);
605 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
606 client_write(confptr, len);
607 if ((len > 0) && (confptr[len - 1] != 10))
608 client_write("\n", 1);
612 cprintf("%d No such configuration.\n",
613 ERROR + ILLEGAL_VALUE);
617 // CONF PUTSYS - store arbitrary system configuration stanzas in the message base
618 else if (!strcasecmp(cmd, "PUTSYS")) {
619 extract_token(confname, argbuf, 1, '|', sizeof confname);
621 cprintf("%d %s\n", SEND_LISTING, confname);
622 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0, 0);
623 CtdlPutSysConfig(confname, confptr);
627 else if (!strcasecmp(cmd, "GETVAL")) {
628 extract_token(confname, argbuf, 1, '|', sizeof confname);
629 char *v = CtdlGetConfigStr(confname);
631 cprintf("%d|%s|\n", CIT_OK, v);
634 cprintf("%d||\n", ERROR);
638 else if (!strcasecmp(cmd, "PUTVAL")) {
639 if (num_tokens(argbuf, '|') < 3) {
640 cprintf("%d name and value required\n", ERROR);
643 extract_token(confname, argbuf, 1, '|', sizeof confname);
644 extract_token(buf, argbuf, 2, '|', sizeof buf);
645 CtdlSetConfigStr(confname, buf);
646 cprintf("%d setting '%s' to '%s'\n", CIT_OK, confname, buf);
651 cprintf("%d Illegal option(s) specified.\n", ERROR + ILLEGAL_VALUE);
655 typedef struct __ConfType {
660 ConfType CfgNames[] = {
661 { {HKEY("localhost") }, 0},
662 { {HKEY("directory") }, 0},
663 { {HKEY("smarthost") }, 2},
664 { {HKEY("fallbackhost") }, 2},
665 { {HKEY("rbl") }, 3},
666 { {HKEY("spamassassin") }, 3},
667 { {HKEY("masqdomain") }, 1},
668 { {HKEY("clamav") }, 3},
669 { {HKEY("notify") }, 3},
673 HashList *CfgNameHash = NULL;
674 void cmd_gvdn(char *argbuf)
676 const ConfType *pCfg;
678 long min = atol(argbuf);
679 const char *Pos = NULL;
680 const char *PPos = NULL;
691 List = NewHash(1, NULL);
692 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
693 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
696 confptr = CtdlGetSysConfig(INTERNETCFG);
697 Config = NewStrBufPlain(confptr, -1);
700 Line = NewStrBufPlain(NULL, StrLength(Config));
701 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
702 while (StrBufSipLine(Line, Config, &Pos))
705 Cfg = NewStrBufPlain(NULL, StrLength(Line));
707 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
708 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
709 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
712 pCfg = (ConfType *) vptr;
713 if (pCfg->Type <= min)
715 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
721 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
722 It = GetNewHashPos(List, 1);
723 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
734 FreeStrBuf(&CfgToken);
738 /*****************************************************************************/
739 /* MODULE INITIALIZATION STUFF */
740 /*****************************************************************************/
742 void control_cleanup(void)
744 DeleteHash(&CfgNameHash);
746 CTDL_MODULE_INIT(control)
751 CfgNameHash = NewHash(1, NULL);
752 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
753 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");
757 CtdlRegisterCleanupHook(control_cleanup);
760 /* return our id for the Log */