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;
30 * lock_control - acquire a lock on the control record file.
31 * This keeps multiple citservers from running concurrently.
33 void lock_control(void)
35 #if defined(LOCK_EX) && defined(LOCK_NB)
36 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
37 syslog(LOG_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
38 syslog(LOG_EMERG, "Is another citserver already running?\n");
39 exit(CTDLEXIT_CONTROL);
45 * callback to get highest room number when rebuilding control file
47 void control_find_highest(struct ctdlroom *qrbuf, void *data)
50 struct cdbdata *cdbfr;
55 int message_fixed = 0;
57 if (qrbuf->QRnumber > CitControl.MMnextroom)
59 CitControl.MMnextroom = qrbuf->QRnumber;
63 CtdlGetRoom (&room, qrbuf->QRname);
65 /* Load the message list */
66 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
68 msglist = (long *) cdbfr->ptr;
69 num_msgs = cdbfr->len / sizeof(long);
71 return; /* No messages at all? No further action. */
76 for (c=0; c<num_msgs; c++)
78 if (msglist[c] > CitControl.MMhighest)
80 CitControl.MMhighest = msglist[c];
87 syslog(LOG_INFO, "Control record checking....Fixed room counter\n");
89 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 > CitControl.MMnextuser)
104 CitControl.MMnextuser = EachUser->usernum;
108 syslog(LOG_INFO, "Control record checking....Fixed user count\n");
113 * get_control - read the control record into memory.
115 void get_control(void)
117 static int already_have_control = 0;
121 * If we already have the control record in memory, there's no point
122 * in reading it from disk again.
124 if (already_have_control) return;
126 /* Zero it out. If the control record on disk is missing or short,
127 * the system functions with all control record fields initialized
130 memset(&CitControl, 0, sizeof(struct CitControl));
131 if (control_fp == NULL) {
132 control_fp = fopen(file_citadel_control, "rb+");
133 if (control_fp != NULL) {
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));
139 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
141 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n",
142 file_citadel_control, strerror(errno));
145 if (control_fp == NULL) {
146 control_fp = fopen(file_citadel_control, "wb+");
147 if (control_fp != NULL) {
149 memset(&CitControl, 0, sizeof(struct CitControl));
151 rv = fchown(fileno(control_fp), ctdluid, -1);
153 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
154 file_citadel_control, strerror(errno));
156 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
158 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n",
159 file_citadel_control, strerror(errno));
160 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
162 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
163 file_citadel_control, strerror(errno));
167 if (control_fp == NULL) {
168 syslog(LOG_ALERT, "ERROR opening %s: %s\n", file_citadel_control, strerror(errno));
173 rv = fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
175 syslog(LOG_EMERG, "Failed to read Controlfile: %s [%s]\n",
176 file_citadel_control, strerror(errno));
177 already_have_control = 1;
178 rv = chown(file_citadel_control, ctdluid, (-1));
180 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
181 file_citadel_control, strerror(errno));
185 * put_control - write the control record to disk.
187 void put_control(void)
191 if (control_fp != NULL) {
193 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
195 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
196 file_citadel_control, strerror(errno));
203 * check_control - check the control record has sensible values for message, user and room numbers
205 void check_control(void)
207 syslog(LOG_INFO, "Checking/re-building control record\n");
209 // Find highest room number and message number.
210 CtdlForEachRoom(control_find_highest, NULL);
211 ForEachUser(control_find_user, NULL);
217 * release_control - close our fd on exit
219 void release_control(void)
221 if (control_fp != NULL) {
228 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
230 long get_new_message_number(void)
233 begin_critical_section(S_CONTROL);
235 retval = ++CitControl.MMhighest;
237 end_critical_section(S_CONTROL);
243 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
244 * This provides a quick way to initialise a variable that might be used to indicate
245 * messages that should not be processed. EG. a new Sieve script will use this
246 * to record determine that messages older than this should not be processed.
248 long CtdlGetCurrentMessageNumber(void)
251 begin_critical_section(S_CONTROL);
253 retval = CitControl.MMhighest;
254 end_critical_section(S_CONTROL);
260 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
262 long get_new_user_number(void)
265 begin_critical_section(S_CONTROL);
267 retval = ++CitControl.MMnextuser;
269 end_critical_section(S_CONTROL);
276 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
278 long get_new_room_number(void)
281 begin_critical_section(S_CONTROL);
283 retval = ++CitControl.MMnextroom;
285 end_critical_section(S_CONTROL);
292 * Get or set global configuration options
294 * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
295 * http://www.citadel.org/doku.php?id=documentation:applicationprotocol
298 void cmd_conf(char *argbuf)
306 if (CtdlAccessCheck(ac_aide)) return;
308 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
309 if (!strcasecmp(cmd, "GET")) {
310 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
311 cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
312 cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
313 cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
314 cprintf("xxx\n"); /* placeholder -- field no longer in use */
315 cprintf("%d\n", CtdlGetConfigInt("c_creataide"));
316 cprintf("%d\n", CtdlGetConfigInt("c_sleeping"));
317 cprintf("%d\n", CtdlGetConfigInt("c_initax"));
318 cprintf("%d\n", CtdlGetConfigInt("c_regiscall"));
319 cprintf("%d\n", CtdlGetConfigInt("c_twitdetect"));
320 cprintf("%s\n", CtdlGetConfigStr("c_twitroom"));
321 cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
322 cprintf("%d\n", CtdlGetConfigInt("c_restrict"));
323 cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
324 cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
325 cprintf("%d\n", CtdlGetConfigInt("c_maxsessions"));
326 cprintf("xxx\n"); /* placeholder -- field no longer in use */
327 cprintf("%d\n", CtdlGetConfigInt("c_userpurge"));
328 cprintf("%d\n", CtdlGetConfigInt("c_roompurge"));
329 cprintf("%s\n", CtdlGetConfigStr("c_logpages"));
330 cprintf("%d\n", CtdlGetConfigInt("c_createax"));
331 cprintf("%ld\n", CtdlGetConfigLong("c_maxmsglen"));
332 cprintf("%d\n", CtdlGetConfigInt("c_min_workers"));
333 cprintf("%d\n", CtdlGetConfigInt("c_max_workers"));
334 cprintf("%d\n", CtdlGetConfigInt("c_pop3_port"));
335 cprintf("%d\n", CtdlGetConfigInt("c_smtp_port"));
336 cprintf("%d\n", CtdlGetConfigInt("c_rfc822_strict_from"));
337 cprintf("%d\n", CtdlGetConfigInt("c_aide_zap"));
338 cprintf("%d\n", CtdlGetConfigInt("c_imap_port"));
339 cprintf("%ld\n", CtdlGetConfigLong("c_net_freq"));
340 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
341 cprintf("1\n"); /* niu */
342 cprintf("%d\n", CtdlGetConfigInt("c_purge_hour"));
344 cprintf("%s\n", CtdlGetConfigStr("c_ldap_host"));
345 cprintf("%d\n", CtdlGetConfigInt("c_ldap_port"));
346 cprintf("%s\n", CtdlGetConfigStr("c_ldap_base_dn"));
347 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_dn"));
348 cprintf("%s\n", CtdlGetConfigStr("c_ldap_bind_pw"));
356 cprintf("%s\n", CtdlGetConfigStr("c_ip_addr"));
357 cprintf("%d\n", CtdlGetConfigInt("c_msa_port"));
358 cprintf("%d\n", CtdlGetConfigInt("c_imaps_port"));
359 cprintf("%d\n", CtdlGetConfigInt("c_pop3s_port"));
360 cprintf("%d\n", CtdlGetConfigInt("c_smtps_port"));
361 cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
362 cprintf("%d\n", CtdlGetConfigInt("c_auto_cull"));
364 cprintf("%d\n", CtdlGetConfigInt("c_allow_spoofing"));
365 cprintf("%d\n", CtdlGetConfigInt("c_journal_email"));
366 cprintf("%d\n", CtdlGetConfigInt("c_journal_pubmsgs"));
367 cprintf("%s\n", CtdlGetConfigStr("c_journal_dest"));
368 cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
369 cprintf("%d\n", CtdlGetConfigInt("c_pftcpdict_port"));
370 cprintf("%d\n", CtdlGetConfigInt("c_managesieve_port"));
371 cprintf("%d\n", CtdlGetConfigInt("c_auth_mode"));
372 cprintf("%s\n", CtdlGetConfigStr("c_funambol_host"));
373 cprintf("%d\n", CtdlGetConfigInt("c_funambol_port"));
374 cprintf("%s\n", CtdlGetConfigStr("c_funambol_source"));
375 cprintf("%s\n", CtdlGetConfigStr("c_funambol_auth"));
376 cprintf("%d\n", CtdlGetConfigInt("c_rbl_at_greeting"));
377 cprintf("%s\n", CtdlGetConfigStr("c_master_user"));
378 cprintf("%s\n", CtdlGetConfigStr("c_master_pass"));
379 cprintf("%s\n", CtdlGetConfigStr("c_pager_program"));
380 cprintf("%d\n", CtdlGetConfigInt("c_imap_keep_from"));
381 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_c2s_port"));
382 cprintf("%d\n", CtdlGetConfigInt("c_xmpp_s2s_port"));
383 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fetch"));
384 cprintf("%ld\n", CtdlGetConfigLong("c_pop3_fastest"));
385 cprintf("%d\n", CtdlGetConfigInt("c_spam_flag_only"));
386 cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
387 cprintf("%d\n", CtdlGetConfigInt("c_port_number"));
388 cprintf("%d\n", ctdluid);
389 cprintf("%d\n", CtdlGetConfigInt("c_nntp_port"));
390 cprintf("%d\n", CtdlGetConfigInt("c_nntps_port"));
394 else if (!strcasecmp(cmd, "SET")) {
396 cprintf("%d Send configuration...\n", SEND_LISTING);
398 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
401 safestrncpy(config.c_nodename, buf, sizeof config.c_nodename);
404 safestrncpy(config.c_fqdn, buf, sizeof config.c_fqdn);
407 safestrncpy(config.c_humannode, buf, sizeof config.c_humannode);
410 /* placeholder -- field no longer in use */
413 config.c_creataide = atoi(buf);
416 config.c_sleeping = atoi(buf);
419 config.c_initax = atoi(buf);
420 if (config.c_initax < 1)
422 if (config.c_initax > 6)
426 config.c_regiscall = atoi(buf);
427 if (config.c_regiscall != 0)
428 config.c_regiscall = 1;
431 config.c_twitdetect = atoi(buf);
432 if (config.c_twitdetect != 0)
433 config.c_twitdetect = 1;
436 safestrncpy(config.c_twitroom, buf, sizeof config.c_twitroom);
439 safestrncpy(config.c_moreprompt, buf, sizeof config.c_moreprompt);
442 config.c_restrict = atoi(buf);
443 if (config.c_restrict != 0)
444 config.c_restrict = 1;
447 safestrncpy(config.c_site_location, buf, sizeof config.c_site_location);
450 safestrncpy(config.c_sysadm, buf, sizeof config.c_sysadm);
453 config.c_maxsessions = atoi(buf);
454 if (config.c_maxsessions < 0)
455 config.c_maxsessions = 0;
458 /* placeholder -- field no longer in use */
461 config.c_userpurge = atoi(buf);
464 config.c_roompurge = atoi(buf);
467 safestrncpy(config.c_logpages, buf, sizeof config.c_logpages);
470 config.c_createax = atoi(buf);
471 if (config.c_createax < 1)
472 config.c_createax = 1;
473 if (config.c_createax > 6)
474 config.c_createax = 6;
477 if (atoi(buf) >= 8192)
478 config.c_maxmsglen = atoi(buf);
482 config.c_min_workers = atoi(buf);
484 if (atoi(buf) >= config.c_min_workers)
485 config.c_max_workers = atoi(buf);
487 config.c_pop3_port = atoi(buf);
490 config.c_smtp_port = atoi(buf);
493 config.c_rfc822_strict_from = atoi(buf);
496 config.c_aide_zap = atoi(buf);
497 if (config.c_aide_zap != 0)
498 config.c_aide_zap = 1;
501 config.c_imap_port = atoi(buf);
504 config.c_net_freq = atol(buf);
507 config.c_disable_newu = atoi(buf);
508 if (config.c_disable_newu != 0)
509 config.c_disable_newu = 1;
515 if ((config.c_purge_hour >= 0)
516 && (config.c_purge_hour <= 23)) {
517 config.c_purge_hour = atoi(buf);
522 safestrncpy(config.c_ldap_host, buf, sizeof config.c_ldap_host);
525 config.c_ldap_port = atoi(buf);
528 safestrncpy(config.c_ldap_base_dn, buf, sizeof config.c_ldap_base_dn);
531 safestrncpy(config.c_ldap_bind_dn, buf, sizeof config.c_ldap_bind_dn);
534 safestrncpy(config.c_ldap_bind_pw, buf, sizeof config.c_ldap_bind_pw);
538 safestrncpy(config.c_ip_addr, buf, sizeof config.c_ip_addr);
540 config.c_msa_port = atoi(buf);
543 config.c_imaps_port = atoi(buf);
546 config.c_pop3s_port = atoi(buf);
549 config.c_smtps_port = atoi(buf);
552 config.c_enable_fulltext = atoi(buf);
555 config.c_auto_cull = atoi(buf);
561 config.c_allow_spoofing = atoi(buf);
564 config.c_journal_email = atoi(buf);
567 config.c_journal_pubmsgs = atoi(buf);
570 safestrncpy(config.c_journal_dest, buf, sizeof config.c_journal_dest);
572 safestrncpy(config.c_default_cal_zone, buf, sizeof config.c_default_cal_zone);
575 config.c_pftcpdict_port = atoi(buf);
578 config.c_managesieve_port = atoi(buf);
581 config.c_auth_mode = atoi(buf);
583 safestrncpy(config.c_funambol_host, buf, sizeof config.c_funambol_host);
586 config.c_funambol_port = atoi(buf);
589 safestrncpy(config.c_funambol_source, buf, sizeof config.c_funambol_source);
592 safestrncpy(config.c_funambol_auth, buf, sizeof config.c_funambol_auth);
595 config.c_rbl_at_greeting = atoi(buf);
598 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
601 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
604 safestrncpy(config.c_pager_program, buf, sizeof config.c_pager_program);
607 config.c_imap_keep_from = atoi(buf);
610 config.c_xmpp_c2s_port = atoi(buf);
613 config.c_xmpp_s2s_port = atoi(buf);
616 config.c_pop3_fetch = atol(buf);
619 config.c_pop3_fastest = atol(buf);
622 config.c_spam_flag_only = atoi(buf);
625 config.c_guest_logins = atoi(buf);
628 config.c_port_number = atoi(buf);
634 config.c_nntp_port = atoi(buf);
637 config.c_nntps_port = atoi(buf);
643 snprintf(buf, sizeof buf,
644 "The global system configuration has been edited by %s.\n",
645 (CC->logged_in ? CC->curr_user : "an administrator")
647 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
649 if (!IsEmptyStr(config.c_logpages))
650 CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
652 /* If full text indexing has been disabled, invalidate the
653 * index so it doesn't try to use it later.
655 if (config.c_enable_fulltext == 0) {
656 CitControl.MM_fulltext_wordbreaker = 0;
661 else if (!strcasecmp(cmd, "GETSYS")) {
662 extract_token(confname, argbuf, 1, '|', sizeof confname);
663 confptr = CtdlGetSysConfig(confname);
664 if (confptr != NULL) {
667 len = strlen(confptr);
668 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
669 client_write(confptr, len);
670 if ((len > 0) && (confptr[len - 1] != 10))
671 client_write("\n", 1);
675 cprintf("%d No such configuration.\n",
676 ERROR + ILLEGAL_VALUE);
680 else if (!strcasecmp(cmd, "PUTSYS")) {
681 extract_token(confname, argbuf, 1, '|', sizeof confname);
683 cprintf("%d %s\n", SEND_LISTING, confname);
684 confptr = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
685 CtdlPutSysConfig(confname, confptr);
690 cprintf("%d Illegal option(s) specified.\n",
691 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(config.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 */