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 struct CitControl CitControl;
25 FILE *control_fp = NULL;
26 long control_highest_user = 0;
31 * callback to get highest room number when rebuilding control file
33 void control_find_highest(struct ctdlroom *qrbuf, void *data)
36 struct cdbdata *cdbfr;
41 int message_fixed = 0;
43 if (qrbuf->QRnumber > CitControl.MMnextroom)
45 CitControl.MMnextroom = qrbuf->QRnumber;
49 CtdlGetRoom (&room, qrbuf->QRname);
51 /* Load the message list */
52 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
54 msglist = (long *) cdbfr->ptr;
55 num_msgs = cdbfr->len / sizeof(long);
57 return; /* No messages at all? No further action. */
62 for (c=0; c<num_msgs; c++)
64 if (msglist[c] > CitControl.MMhighest)
66 CitControl.MMhighest = msglist[c];
73 syslog(LOG_INFO, "Control record checking....Fixed room counter\n");
75 syslog(LOG_INFO, "Control record checking....Fixed message count\n");
81 * Callback to get highest user number.
84 void control_find_user (struct ctdluser *EachUser, void *out_data)
88 if (EachUser->usernum > CitControl.MMnextuser)
90 CitControl.MMnextuser = EachUser->usernum;
94 syslog(LOG_INFO, "Control record checking....Fixed user count\n");
99 * get_control - read the control record into memory.
101 void get_control(void)
103 static int already_have_control = 0;
107 * If we already have the control record in memory, there's no point
108 * in reading it from disk again.
110 if (already_have_control) return;
112 /* Zero it out. If the control record on disk is missing or short,
113 * the system functions with all control record fields initialized
116 memset(&CitControl, 0, sizeof(struct CitControl));
117 if (control_fp == NULL) {
118 control_fp = fopen(file_citadel_control, "rb+");
119 if (control_fp != NULL) {
120 rv = fchown(fileno(control_fp), ctdluid, -1);
122 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
123 file_citadel_control, strerror(errno));
124 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
126 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n",
127 file_citadel_control, strerror(errno));
130 if (control_fp == NULL) {
131 control_fp = fopen(file_citadel_control, "wb+");
132 if (control_fp != NULL) {
133 memset(&CitControl, 0, sizeof(struct CitControl));
135 rv = fchown(fileno(control_fp), ctdluid, -1);
137 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
138 file_citadel_control, strerror(errno));
140 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
142 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n",
143 file_citadel_control, strerror(errno));
144 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
146 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
147 file_citadel_control, strerror(errno));
151 if (control_fp == NULL) {
152 syslog(LOG_ALERT, "ERROR opening %s: %s\n", file_citadel_control, strerror(errno));
157 rv = fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
159 syslog(LOG_EMERG, "Failed to read Controlfile: %s [%s]\n",
160 file_citadel_control, strerror(errno));
161 already_have_control = 1;
162 rv = chown(file_citadel_control, ctdluid, (-1));
164 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
165 file_citadel_control, strerror(errno));
169 * put_control - write the control record to disk.
171 void put_control(void)
175 if (control_fp != NULL) {
177 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
179 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
180 file_citadel_control, strerror(errno));
187 * check_control - check the control record has sensible values for message, user and room numbers
189 void check_control(void)
191 syslog(LOG_INFO, "Checking/re-building control record\n");
193 // Find highest room number and message number.
194 CtdlForEachRoom(control_find_highest, NULL);
195 ForEachUser(control_find_user, NULL);
201 * release_control - close our fd on exit
203 void release_control(void)
205 if (control_fp != NULL) {
212 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
214 long get_new_message_number(void)
217 begin_critical_section(S_CONTROL);
219 retval = ++CitControl.MMhighest;
221 end_critical_section(S_CONTROL);
227 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
228 * This provides a quick way to initialise a variable that might be used to indicate
229 * messages that should not be processed. EG. a new Sieve script will use this
230 * to record determine that messages older than this should not be processed.
232 long CtdlGetCurrentMessageNumber(void)
235 begin_critical_section(S_CONTROL);
237 retval = CitControl.MMhighest;
238 end_critical_section(S_CONTROL);
244 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
246 long get_new_user_number(void)
249 begin_critical_section(S_CONTROL);
251 retval = ++CitControl.MMnextuser;
253 end_critical_section(S_CONTROL);
260 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
262 long get_new_room_number(void)
265 begin_critical_section(S_CONTROL);
267 retval = ++CitControl.MMnextroom;
269 end_critical_section(S_CONTROL);
276 * Helper function for cmd_conf() to handle boolean values
278 int confbool(char *v)
280 if (IsEmptyStr(v)) return(0);
281 if (atoi(v) != 0) return(1);
287 * Get or set global configuration options
289 * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
290 * http://www.citadel.org/doku.php?id=documentation:applicationprotocol
293 void cmd_conf(char *argbuf)
302 if (CtdlAccessCheck(ac_aide)) return;
304 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
305 if (!strcasecmp(cmd, "GET")) {
306 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
307 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
308 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
309 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
310 cprintf("xxx\n"); /* placeholder -- field no longer in use */
311 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
312 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
313 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
314 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
315 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
316 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
317 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
318 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
319 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
320 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
321 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
322 cprintf("xxx\n"); /* placeholder -- field no longer in use */
323 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
324 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
325 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
326 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
327 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
328 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
329 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
330 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
331 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
332 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
333 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
334 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
335 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
336 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
337 cprintf("1\n"); /* niu */
338 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
340 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
341 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
342 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
343 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
344 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
352 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
353 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
354 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
355 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
356 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
357 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
358 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
360 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
361 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
362 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
363 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
364 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
365 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
366 cprintf("%d\n", CtdlGetConfigInt("c_managesieve_port"));
367 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
368 cprintf("%s\n", CtdlGetConfigStr("c_funambol_host"));
369 cprintf("%d\n", CtdlGetConfigInt("c_funambol_port"));
370 cprintf("%s\n", CtdlGetConfigStr("c_funambol_source"));
371 cprintf("%s\n", CtdlGetConfigStr("c_funambol_auth"));
372 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
373 cprintf("%s\n", CtdlGetConfigStr("c_master_user"));
374 cprintf("%s\n", CtdlGetConfigStr("c_master_pass"));
375 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
376 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
377 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
378 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
379 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
380 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
381 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
382 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
383 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
384 cprintf("%d\n", ctdluid);
385 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
386 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
390 else if (!strcasecmp(cmd, "SET")) {
392 cprintf("%d Send configuration...\n", SEND_LISTING);
394 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
397 CtdlSetConfigStr("c_nodename", buf);
400 CtdlSetConfigStr("c_fqdn", buf);
403 CtdlSetConfigStr("c_humannode", buf);
406 /* placeholder -- field no longer in use */
409 CtdlSetConfigInt("c_creataide", atoi(buf));
412 CtdlSetConfigInt("c_sleeping", atoi(buf));
418 CtdlSetConfigInt("c_initax", i);
421 CtdlSetConfigInt("c_regiscall", confbool(buf));
424 CtdlSetConfigInt("c_twitdetect", confbool(buf));
427 CtdlSetConfigStr("c_twitroom", buf);
430 CtdlSetConfigStr("c_moreprompt", buf);
433 CtdlSetConfigInt("c_restrict", confbool(buf));
436 CtdlSetConfigInt("c_site_location", confbool(buf));
439 CtdlSetConfigInt("c_sysadm", confbool(buf));
444 CtdlSetConfigInt("c_maxsessions", i);
447 /* placeholder -- field no longer in use */
450 CtdlSetConfigInt("c_userpurge", atoi(buf));
453 CtdlSetConfigInt("c_roompurge", atoi(buf));
456 CtdlSetConfigStr("c_logpages", buf);
462 CtdlSetConfigInt("c_createax", i);
467 CtdlSetConfigLong("c_maxmsglen", ii);
472 if (i >= 3) { // minimum value
473 CtdlSetConfigInt("c_min_workers", i);
478 if (i >= CtdlGetConfigInt("c_min_workers")) { // max must be >= min
479 CtdlSetConfigInt("c_max_workers", i);
483 CtdlSetConfigInt("c_pop3_port", atoi(buf));
486 CtdlSetConfigInt("c_smtp_port", atoi(buf));
489 CtdlSetConfigInt("c_rfc822_strict_from", atoi(buf));
492 CtdlSetConfigInt("c_aide_zap", confbool(buf));
495 CtdlSetConfigInt("c_imap_port", atoi(buf));
498 CtdlSetConfigLong("c_net_freq", atol(buf));
501 CtdlSetConfigInt("c_disable_newu", confbool(buf));
508 if ((i >= 0) && (i <= 23)) {
509 CtdlSetConfigInt("c_purge_hour", i);
513 CtdlSetConfigStr("c_ldap_host", buf);
516 CtdlSetConfigInt("c_ldap_port", atoi(buf));
519 CtdlSetConfigStr("c_ldap_base_dn", buf);
522 CtdlSetConfigStr("c_ldap_bind_dn", buf);
525 CtdlSetConfigStr("c_ldap_bind_pw", buf);
528 CtdlSetConfigStr("c_ip_addr", buf);
531 CtdlSetConfigInt("c_msa_port", atoi(buf));
534 CtdlSetConfigInt("c_imaps_port", atoi(buf));
537 CtdlSetConfigInt("c_pop3s_port", atoi(buf));
540 CtdlSetConfigInt("c_smtps_port", atoi(buf));
543 CtdlSetConfigInt("c_enable_fulltext", confbool(buf));
546 CtdlSetConfigInt("c_auto_cull", confbool(buf));
552 CtdlSetConfigInt("c_allow_spoofing", confbool(buf));
555 CtdlSetConfigInt("c_journal_email", confbool(buf));
558 CtdlSetConfigInt("c_journal_pubmsgs", confbool(buf));
561 CtdlSetConfigStr("c_journal_dest", buf);
564 CtdlSetConfigStr("c_default_cal_zone", buf);
567 CtdlSetConfigInt("c_pftcpdict_port", atoi(buf));
570 CtdlSetConfigInt("c_managesieve_port", atoi(buf));
573 CtdlSetConfigInt("c_auth_mode", atoi(buf));
576 CtdlSetConfigStr("c_funambol_host", buf);
579 CtdlSetConfigInt("c_funambol_port", atoi(buf));
582 CtdlSetConfigStr("c_funambol_source", buf);
585 CtdlSetConfigStr("c_funambol_auth", buf);
588 CtdlSetConfigInt("c_rbl_at_greeting", confbool(buf));
591 CtdlSetConfigStr("c_master_user", buf);
594 CtdlSetConfigStr("c_master_pass", buf);
597 CtdlSetConfigStr("c_pager_program", buf);
600 CtdlSetConfigInt("c_imap_keep_from", confbool(buf));
603 CtdlSetConfigInt("c_xmpp_c2s_port", atoi(buf));
606 CtdlSetConfigInt("c_xmpp_s2s_port", atoi(buf));
609 CtdlSetConfigLong("c_pop3_fetch", atol(buf));
612 CtdlSetConfigLong("c_pop3_fastest", atol(buf));
615 CtdlSetConfigInt("c_spam_flag_only", confbool(buf));
618 CtdlSetConfigInt("c_guest_logins", confbool(buf));
621 CtdlSetConfigInt("c_port_number", atoi(buf));
627 CtdlSetConfigInt("c_nntp_port", atoi(buf));
630 CtdlSetConfigInt("c_nntps_port", atoi(buf));
635 snprintf(buf, sizeof buf,
636 "The global system configuration has been edited by %s.\n",
637 (CC->logged_in ? CC->curr_user : "an administrator")
639 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
641 if (!IsEmptyStr(CtdlGetConfigStr("c_logpages")))
642 CtdlCreateRoom(CtdlGetConfigStr("c_logpages"), 3, "", 0, 1, 1, VIEW_BBS);
644 /* If full text indexing has been disabled, invalidate the
645 * index so it doesn't try to use it later.
647 if (CtdlGetConfigInt("c_enable_fulltext") == 0) {
648 CitControl.MM_fulltext_wordbreaker = 0;
653 else if (!strcasecmp(cmd, "GETSYS")) {
654 extract_token(confname, argbuf, 1, '|', sizeof confname);
655 confptr = CtdlGetSysConfig(confname);
656 if (confptr != NULL) {
659 len = strlen(confptr);
660 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
661 client_write(confptr, len);
662 if ((len > 0) && (confptr[len - 1] != 10))
663 client_write("\n", 1);
667 cprintf("%d No such configuration.\n",
668 ERROR + ILLEGAL_VALUE);
672 else if (!strcasecmp(cmd, "PUTSYS")) {
673 extract_token(confname, argbuf, 1, '|', sizeof confname);
675 cprintf("%d %s\n", SEND_LISTING, confname);
676 confptr = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0, 0);
677 CtdlPutSysConfig(confname, confptr);
682 cprintf("%d Illegal option(s) specified.\n",
683 ERROR + ILLEGAL_VALUE);
687 typedef struct __ConfType {
692 ConfType CfgNames[] = {
693 { {HKEY("localhost") }, 0},
694 { {HKEY("directory") }, 0},
695 { {HKEY("smarthost") }, 2},
696 { {HKEY("fallbackhost") }, 2},
697 { {HKEY("rbl") }, 3},
698 { {HKEY("spamassassin") }, 3},
699 { {HKEY("masqdomain") }, 1},
700 { {HKEY("clamav") }, 3},
701 { {HKEY("notify") }, 3},
705 HashList *CfgNameHash = NULL;
706 void cmd_gvdn(char *argbuf)
708 const ConfType *pCfg;
710 long min = atol(argbuf);
711 const char *Pos = NULL;
712 const char *PPos = NULL;
723 List = NewHash(1, NULL);
724 Cfg = NewStrBufPlain(CtdlGetConfigStr("c_fqdn"), -1);
725 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
728 confptr = CtdlGetSysConfig(INTERNETCFG);
729 Config = NewStrBufPlain(confptr, -1);
732 Line = NewStrBufPlain(NULL, StrLength(Config));
733 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
734 while (StrBufSipLine(Line, Config, &Pos))
737 Cfg = NewStrBufPlain(NULL, StrLength(Line));
739 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
740 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
741 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
744 pCfg = (ConfType *) vptr;
745 if (pCfg->Type <= min)
747 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
753 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
754 It = GetNewHashPos(List, 1);
755 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
766 FreeStrBuf(&CfgToken);
770 /*****************************************************************************/
771 /* MODULE INITIALIZATION STUFF */
772 /*****************************************************************************/
774 void control_cleanup(void)
776 DeleteHash(&CfgNameHash);
778 CTDL_MODULE_INIT(control)
783 CfgNameHash = NewHash(1, NULL);
784 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
785 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
787 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
788 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
789 CtdlRegisterCleanupHook(control_cleanup);
792 /* return our id for the Log */