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 extern struct config config;
26 FILE *control_fp = NULL;
27 long control_highest_user = 0;
31 * lock_control - acquire a lock on the control record file.
32 * This keeps multiple citservers from running concurrently.
34 void lock_control(void)
36 #if defined(LOCK_EX) && defined(LOCK_NB)
37 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
38 syslog(LOG_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
39 syslog(LOG_EMERG, "Is another citserver already running?\n");
40 exit(CTDLEXIT_CONTROL);
46 * callback to get highest room number when rebuilding control file
48 void control_find_highest(struct ctdlroom *qrbuf, void *data)
51 struct cdbdata *cdbfr;
56 int message_fixed = 0;
58 if (qrbuf->QRnumber > CitControl.MMnextroom)
60 CitControl.MMnextroom = qrbuf->QRnumber;
64 CtdlGetRoom (&room, qrbuf->QRname);
66 /* Load the message list */
67 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
69 msglist = (long *) cdbfr->ptr;
70 num_msgs = cdbfr->len / sizeof(long);
72 return; /* No messages at all? No further action. */
77 for (c=0; c<num_msgs; c++)
79 if (msglist[c] > CitControl.MMhighest)
81 CitControl.MMhighest = msglist[c];
88 syslog(LOG_INFO, "Control record checking....Fixed room counter\n");
90 syslog(LOG_INFO, "Control record checking....Fixed message count\n");
96 * Callback to get highest user number.
99 void control_find_user (struct ctdluser *EachUser, void *out_data)
103 if (EachUser->usernum > CitControl.MMnextuser)
105 CitControl.MMnextuser = EachUser->usernum;
109 syslog(LOG_INFO, "Control record checking....Fixed user count\n");
114 * get_control - read the control record into memory.
116 void get_control(void)
118 static int already_have_control = 0;
122 * If we already have the control record in memory, there's no point
123 * in reading it from disk again.
125 if (already_have_control) return;
127 /* Zero it out. If the control record on disk is missing or short,
128 * the system functions with all control record fields initialized
131 memset(&CitControl, 0, sizeof(struct CitControl));
132 if (control_fp == NULL) {
133 control_fp = fopen(file_citadel_control, "rb+");
134 if (control_fp != NULL) {
136 rv = fchown(fileno(control_fp), ctdluid, -1);
138 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
139 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));
146 if (control_fp == NULL) {
147 control_fp = fopen(file_citadel_control, "wb+");
148 if (control_fp != NULL) {
150 memset(&CitControl, 0, sizeof(struct CitControl));
152 rv = fchown(fileno(control_fp), ctdluid, -1);
154 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
155 file_citadel_control, strerror(errno));
157 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
159 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n",
160 file_citadel_control, strerror(errno));
161 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
163 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
164 file_citadel_control, strerror(errno));
168 if (control_fp == NULL) {
169 syslog(LOG_ALERT, "ERROR opening %s: %s\n", file_citadel_control, strerror(errno));
174 rv = fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
176 syslog(LOG_EMERG, "Failed to read Controlfile: %s [%s]\n",
177 file_citadel_control, strerror(errno));
178 already_have_control = 1;
179 rv = chown(file_citadel_control, ctdluid, (-1));
181 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
182 file_citadel_control, strerror(errno));
186 * put_control - write the control record to disk.
188 void put_control(void)
192 if (control_fp != NULL) {
194 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
196 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
197 file_citadel_control, strerror(errno));
204 * check_control - check the control record has sensible values for message, user and room numbers
206 void check_control(void)
208 syslog(LOG_INFO, "Checking/re-building control record\n");
210 // Find highest room number and message number.
211 CtdlForEachRoom(control_find_highest, NULL);
212 ForEachUser(control_find_user, NULL);
218 * release_control - close our fd on exit
220 void release_control(void)
222 if (control_fp != NULL) {
229 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
231 long get_new_message_number(void)
234 begin_critical_section(S_CONTROL);
236 retval = ++CitControl.MMhighest;
238 end_critical_section(S_CONTROL);
244 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
245 * This provides a quick way to initialise a variable that might be used to indicate
246 * messages that should not be processed. EG. a new Sieve script will use this
247 * to record determine that messages older than this should not be processed.
249 long CtdlGetCurrentMessageNumber(void)
252 begin_critical_section(S_CONTROL);
254 retval = CitControl.MMhighest;
255 end_critical_section(S_CONTROL);
261 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
263 long get_new_user_number(void)
266 begin_critical_section(S_CONTROL);
268 retval = ++CitControl.MMnextuser;
270 end_critical_section(S_CONTROL);
277 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
279 long get_new_room_number(void)
282 begin_critical_section(S_CONTROL);
284 retval = ++CitControl.MMnextroom;
286 end_critical_section(S_CONTROL);
293 * Get or set global configuration options
295 * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
296 * http://www.citadel.org/doku.php?id=documentation:applicationprotocol
299 void cmd_conf(char *argbuf)
307 if (CtdlAccessCheck(ac_aide)) return;
309 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
310 if (!strcasecmp(cmd, "GET")) {
311 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
312 cprintf("%s\n", config.c_nodename);
313 cprintf("%s\n", config.c_fqdn);
314 cprintf("%s\n", config.c_humannode);
315 cprintf("%s\n", config.c_phonenum);
316 cprintf("%d\n", config.c_creataide);
317 cprintf("%d\n", config.c_sleeping);
318 cprintf("%d\n", config.c_initax);
319 cprintf("%d\n", config.c_regiscall);
320 cprintf("%d\n", config.c_twitdetect);
321 cprintf("%s\n", config.c_twitroom);
322 cprintf("%s\n", config.c_moreprompt);
323 cprintf("%d\n", config.c_restrict);
324 cprintf("%s\n", config.c_site_location);
325 cprintf("%s\n", config.c_sysadm);
326 cprintf("%d\n", config.c_maxsessions);
327 cprintf("xxx\n"); /* placeholder -- field no longer in use */
328 cprintf("%d\n", config.c_userpurge);
329 cprintf("%d\n", config.c_roompurge);
330 cprintf("%s\n", config.c_logpages);
331 cprintf("%d\n", config.c_createax);
332 cprintf("%ld\n", config.c_maxmsglen);
333 cprintf("%d\n", config.c_min_workers);
334 cprintf("%d\n", config.c_max_workers);
335 cprintf("%d\n", config.c_pop3_port);
336 cprintf("%d\n", config.c_smtp_port);
337 cprintf("%d\n", config.c_rfc822_strict_from);
338 cprintf("%d\n", config.c_aide_zap);
339 cprintf("%d\n", config.c_imap_port);
340 cprintf("%ld\n", config.c_net_freq);
341 cprintf("%d\n", config.c_disable_newu);
342 cprintf("1\n"); /* niu */
343 cprintf("%d\n", config.c_purge_hour);
345 cprintf("%s\n", config.c_ldap_host);
346 cprintf("%d\n", config.c_ldap_port);
347 cprintf("%s\n", config.c_ldap_base_dn);
348 cprintf("%s\n", config.c_ldap_bind_dn);
349 cprintf("%s\n", config.c_ldap_bind_pw);
357 cprintf("%s\n", config.c_ip_addr);
358 cprintf("%d\n", config.c_msa_port);
359 cprintf("%d\n", config.c_imaps_port);
360 cprintf("%d\n", config.c_pop3s_port);
361 cprintf("%d\n", config.c_smtps_port);
362 cprintf("%d\n", config.c_enable_fulltext);
363 cprintf("%d\n", config.c_auto_cull);
365 cprintf("%d\n", config.c_allow_spoofing);
366 cprintf("%d\n", config.c_journal_email);
367 cprintf("%d\n", config.c_journal_pubmsgs);
368 cprintf("%s\n", config.c_journal_dest);
369 cprintf("%s\n", config.c_default_cal_zone);
370 cprintf("%d\n", config.c_pftcpdict_port);
371 cprintf("%d\n", config.c_managesieve_port);
372 cprintf("%d\n", config.c_auth_mode);
373 cprintf("%s\n", config.c_funambol_host);
374 cprintf("%d\n", config.c_funambol_port);
375 cprintf("%s\n", config.c_funambol_source);
376 cprintf("%s\n", config.c_funambol_auth);
377 cprintf("%d\n", config.c_rbl_at_greeting);
378 cprintf("%s\n", config.c_master_user);
379 cprintf("%s\n", config.c_master_pass);
380 cprintf("%s\n", config.c_pager_program);
381 cprintf("%d\n", config.c_imap_keep_from);
382 cprintf("%d\n", config.c_xmpp_c2s_port);
383 cprintf("%d\n", config.c_xmpp_s2s_port);
384 cprintf("%ld\n", config.c_pop3_fetch);
385 cprintf("%ld\n", config.c_pop3_fastest);
386 cprintf("%d\n", config.c_spam_flag_only);
387 cprintf("%d\n", config.c_guest_logins);
388 cprintf("%d\n", config.c_port_number);
389 cprintf("%d\n", ctdluid);
390 cprintf("%d\n", config.c_nntp_port);
391 cprintf("%d\n", config.c_nntps_port);
395 else if (!strcasecmp(cmd, "SET")) {
397 cprintf("%d Send configuration...\n", SEND_LISTING);
399 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
402 safestrncpy(config.c_nodename, buf, sizeof config.c_nodename);
405 safestrncpy(config.c_fqdn, buf, sizeof config.c_fqdn);
408 safestrncpy(config.c_humannode, buf, sizeof config.c_humannode);
411 safestrncpy(config.c_phonenum, buf, sizeof config.c_phonenum);
414 config.c_creataide = atoi(buf);
417 config.c_sleeping = atoi(buf);
420 config.c_initax = atoi(buf);
421 if (config.c_initax < 1)
423 if (config.c_initax > 6)
427 config.c_regiscall = atoi(buf);
428 if (config.c_regiscall != 0)
429 config.c_regiscall = 1;
432 config.c_twitdetect = atoi(buf);
433 if (config.c_twitdetect != 0)
434 config.c_twitdetect = 1;
437 safestrncpy(config.c_twitroom, buf, sizeof config.c_twitroom);
440 safestrncpy(config.c_moreprompt, buf, sizeof config.c_moreprompt);
443 config.c_restrict = atoi(buf);
444 if (config.c_restrict != 0)
445 config.c_restrict = 1;
448 safestrncpy(config.c_site_location, buf, sizeof config.c_site_location);
451 safestrncpy(config.c_sysadm, buf, sizeof config.c_sysadm);
454 config.c_maxsessions = atoi(buf);
455 if (config.c_maxsessions < 0)
456 config.c_maxsessions = 0;
459 /* placeholder -- field no longer in use */
462 config.c_userpurge = atoi(buf);
465 config.c_roompurge = atoi(buf);
468 safestrncpy(config.c_logpages, buf, sizeof config.c_logpages);
471 config.c_createax = atoi(buf);
472 if (config.c_createax < 1)
473 config.c_createax = 1;
474 if (config.c_createax > 6)
475 config.c_createax = 6;
478 if (atoi(buf) >= 8192)
479 config.c_maxmsglen = atoi(buf);
483 config.c_min_workers = atoi(buf);
485 if (atoi(buf) >= config.c_min_workers)
486 config.c_max_workers = atoi(buf);
488 config.c_pop3_port = atoi(buf);
491 config.c_smtp_port = atoi(buf);
494 config.c_rfc822_strict_from = atoi(buf);
497 config.c_aide_zap = atoi(buf);
498 if (config.c_aide_zap != 0)
499 config.c_aide_zap = 1;
502 config.c_imap_port = atoi(buf);
505 config.c_net_freq = atol(buf);
508 config.c_disable_newu = atoi(buf);
509 if (config.c_disable_newu != 0)
510 config.c_disable_newu = 1;
516 if ((config.c_purge_hour >= 0)
517 && (config.c_purge_hour <= 23)) {
518 config.c_purge_hour = atoi(buf);
523 safestrncpy(config.c_ldap_host, buf, sizeof config.c_ldap_host);
526 config.c_ldap_port = atoi(buf);
529 safestrncpy(config.c_ldap_base_dn, buf, sizeof config.c_ldap_base_dn);
532 safestrncpy(config.c_ldap_bind_dn, buf, sizeof config.c_ldap_bind_dn);
535 safestrncpy(config.c_ldap_bind_pw, buf, sizeof config.c_ldap_bind_pw);
539 safestrncpy(config.c_ip_addr, buf, sizeof config.c_ip_addr);
541 config.c_msa_port = atoi(buf);
544 config.c_imaps_port = atoi(buf);
547 config.c_pop3s_port = atoi(buf);
550 config.c_smtps_port = atoi(buf);
553 config.c_enable_fulltext = atoi(buf);
556 config.c_auto_cull = atoi(buf);
562 config.c_allow_spoofing = atoi(buf);
565 config.c_journal_email = atoi(buf);
568 config.c_journal_pubmsgs = atoi(buf);
571 safestrncpy(config.c_journal_dest, buf, sizeof config.c_journal_dest);
573 safestrncpy(config.c_default_cal_zone, buf, sizeof config.c_default_cal_zone);
576 config.c_pftcpdict_port = atoi(buf);
579 config.c_managesieve_port = atoi(buf);
582 config.c_auth_mode = atoi(buf);
584 safestrncpy(config.c_funambol_host, buf, sizeof config.c_funambol_host);
587 config.c_funambol_port = atoi(buf);
590 safestrncpy(config.c_funambol_source, buf, sizeof config.c_funambol_source);
593 safestrncpy(config.c_funambol_auth, buf, sizeof config.c_funambol_auth);
596 config.c_rbl_at_greeting = atoi(buf);
599 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
602 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
605 safestrncpy(config.c_pager_program, buf, sizeof config.c_pager_program);
608 config.c_imap_keep_from = atoi(buf);
611 config.c_xmpp_c2s_port = atoi(buf);
614 config.c_xmpp_s2s_port = atoi(buf);
617 config.c_pop3_fetch = atol(buf);
620 config.c_pop3_fastest = atol(buf);
623 config.c_spam_flag_only = atoi(buf);
626 config.c_guest_logins = atoi(buf);
629 config.c_port_number = atoi(buf);
635 config.c_nntp_port = atoi(buf);
638 config.c_nntps_port = atoi(buf);
644 snprintf(buf, sizeof buf,
645 "The global system configuration has been edited by %s.\n",
646 (CC->logged_in ? CC->curr_user : "an administrator")
648 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
650 if (!IsEmptyStr(config.c_logpages))
651 CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
653 /* If full text indexing has been disabled, invalidate the
654 * index so it doesn't try to use it later.
656 if (config.c_enable_fulltext == 0) {
657 CitControl.MM_fulltext_wordbreaker = 0;
662 else if (!strcasecmp(cmd, "GETSYS")) {
663 extract_token(confname, argbuf, 1, '|', sizeof confname);
664 confptr = CtdlGetSysConfig(confname);
665 if (confptr != NULL) {
668 len = strlen(confptr);
669 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
670 client_write(confptr, len);
671 if ((len > 0) && (confptr[len - 1] != 10))
672 client_write("\n", 1);
676 cprintf("%d No such configuration.\n",
677 ERROR + ILLEGAL_VALUE);
681 else if (!strcasecmp(cmd, "PUTSYS")) {
682 extract_token(confname, argbuf, 1, '|', sizeof confname);
684 cprintf("%d %s\n", SEND_LISTING, confname);
685 confptr = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
686 CtdlPutSysConfig(confname, confptr);
691 cprintf("%d Illegal option(s) specified.\n",
692 ERROR + ILLEGAL_VALUE);
696 typedef struct __ConfType {
701 ConfType CfgNames[] = {
702 { {HKEY("localhost") }, 0},
703 { {HKEY("directory") }, 0},
704 { {HKEY("smarthost") }, 2},
705 { {HKEY("fallbackhost") }, 2},
706 { {HKEY("rbl") }, 3},
707 { {HKEY("spamassassin") }, 3},
708 { {HKEY("masqdomain") }, 1},
709 { {HKEY("clamav") }, 3},
710 { {HKEY("notify") }, 3},
714 HashList *CfgNameHash = NULL;
715 void cmd_gvdn(char *argbuf)
717 const ConfType *pCfg;
719 long min = atol(argbuf);
720 const char *Pos = NULL;
721 const char *PPos = NULL;
732 List = NewHash(1, NULL);
733 Cfg = NewStrBufPlain(config.c_fqdn, -1);
734 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
737 confptr = CtdlGetSysConfig(INTERNETCFG);
738 Config = NewStrBufPlain(confptr, -1);
741 Line = NewStrBufPlain(NULL, StrLength(Config));
742 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
743 while (StrBufSipLine(Line, Config, &Pos))
746 Cfg = NewStrBufPlain(NULL, StrLength(Line));
748 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
749 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
750 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
753 pCfg = (ConfType *) vptr;
754 if (pCfg->Type <= min)
756 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
762 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
763 It = GetNewHashPos(List, 1);
764 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
775 FreeStrBuf(&CfgToken);
779 /*****************************************************************************/
780 /* MODULE INITIALIZATION STUFF */
781 /*****************************************************************************/
783 void control_cleanup(void)
785 DeleteHash(&CfgNameHash);
787 CTDL_MODULE_INIT(control)
792 CfgNameHash = NewHash(1, NULL);
793 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
794 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
796 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
797 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
798 CtdlRegisterCleanupHook(control_cleanup);
801 /* return our id for the Log */