2 * This module handles states which are global to the entire server.
4 * Copyright (c) 1987-2012 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.
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
37 #include <sys/types.h>
40 #include <libcitadel.h>
44 #include "sysdep_decls.h"
48 #include "citserver.h"
53 #include "ctdl_module.h"
55 struct CitControl CitControl;
56 extern struct config config;
57 FILE *control_fp = NULL;
58 long control_highest_user = 0;
62 * lock_control - acquire a lock on the control record file.
63 * This keeps multiple citservers from running concurrently.
65 void lock_control(void)
67 #if defined(LOCK_EX) && defined(LOCK_NB)
68 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
69 syslog(LOG_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
70 syslog(LOG_EMERG, "Is another citserver already running?\n");
71 exit(CTDLEXIT_CONTROL);
77 * callback to get highest room number when rebuilding control file
79 void control_find_highest(struct ctdlroom *qrbuf, void *data)
82 struct cdbdata *cdbfr;
87 int message_fixed = 0;
89 if (qrbuf->QRnumber > CitControl.MMnextroom)
91 CitControl.MMnextroom = qrbuf->QRnumber;
95 CtdlGetRoom (&room, qrbuf->QRname);
97 /* Load the message list */
98 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
100 msglist = (long *) cdbfr->ptr;
101 num_msgs = cdbfr->len / sizeof(long);
103 return; /* No messages at all? No further action. */
108 for (c=0; c<num_msgs; c++)
110 if (msglist[c] > CitControl.MMhighest)
112 CitControl.MMhighest = msglist[c];
119 syslog(LOG_INFO, "Control record checking....Fixed room counter\n");
121 syslog(LOG_INFO, "Control record checking....Fixed message count\n");
127 * Callback to get highest user number.
130 void control_find_user (struct ctdluser *EachUser, void *out_data)
134 if (EachUser->usernum > CitControl.MMnextuser)
136 CitControl.MMnextuser = EachUser->usernum;
140 syslog(LOG_INFO, "Control record checking....Fixed user count\n");
145 * get_control - read the control record into memory.
147 void get_control(void)
149 static int already_have_control = 0;
153 * If we already have the control record in memory, there's no point
154 * in reading it from disk again.
156 if (already_have_control) return;
158 /* Zero it out. If the control record on disk is missing or short,
159 * the system functions with all control record fields initialized
162 memset(&CitControl, 0, sizeof(struct CitControl));
163 if (control_fp == NULL) {
164 control_fp = fopen(file_citadel_control, "rb+");
165 if (control_fp != NULL) {
167 rv = fchown(fileno(control_fp), config.c_ctdluid, -1);
169 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
170 file_citadel_control, strerror(errno));
171 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
173 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n",
174 file_citadel_control, strerror(errno));
177 if (control_fp == NULL) {
178 control_fp = fopen(file_citadel_control, "wb+");
179 if (control_fp != NULL) {
181 memset(&CitControl, 0, sizeof(struct CitControl));
183 rv = fchown(fileno(control_fp), config.c_ctdluid, -1);
185 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
186 file_citadel_control, strerror(errno));
188 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
190 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n",
191 file_citadel_control, strerror(errno));
192 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
194 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
195 file_citadel_control, strerror(errno));
199 if (control_fp == NULL) {
200 syslog(LOG_ALERT, "ERROR opening %s: %s\n", file_citadel_control, strerror(errno));
205 rv = fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
207 syslog(LOG_EMERG, "Failed to read Controlfile: %s [%s]\n",
208 file_citadel_control, strerror(errno));
209 already_have_control = 1;
210 rv = chown(file_citadel_control, config.c_ctdluid, (-1));
212 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
213 file_citadel_control, strerror(errno));
217 * put_control - write the control record to disk.
219 void put_control(void)
223 if (control_fp != NULL) {
225 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
227 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
228 file_citadel_control, strerror(errno));
235 * check_control - check the control record has sensible values for message, user and room numbers
237 void check_control(void)
239 syslog(LOG_INFO, "Checking/re-building control record\n");
241 // Find highest room number and message number.
242 CtdlForEachRoom(control_find_highest, NULL);
243 ForEachUser(control_find_user, NULL);
249 * release_control - close our fd on exit
251 void release_control(void)
253 if (control_fp != NULL) {
260 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
262 long get_new_message_number(void)
265 begin_critical_section(S_CONTROL);
267 retval = ++CitControl.MMhighest;
269 end_critical_section(S_CONTROL);
275 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
276 * This provides a quick way to initialise a variable that might be used to indicate
277 * messages that should not be processed. EG. a new Sieve script will use this
278 * to record determine that messages older than this should not be processed.
280 long CtdlGetCurrentMessageNumber(void)
283 begin_critical_section(S_CONTROL);
285 retval = CitControl.MMhighest;
286 end_critical_section(S_CONTROL);
292 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
294 long get_new_user_number(void)
297 begin_critical_section(S_CONTROL);
299 retval = ++CitControl.MMnextuser;
301 end_critical_section(S_CONTROL);
308 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
310 long get_new_room_number(void)
313 begin_critical_section(S_CONTROL);
315 retval = ++CitControl.MMnextroom;
317 end_critical_section(S_CONTROL);
324 * Get or set global configuration options
326 * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
327 * http://www.citadel.org/doku.php?id=documentation:applicationprotocol
330 void cmd_conf(char *argbuf)
338 if (CtdlAccessCheck(ac_aide)) return;
340 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
341 if (!strcasecmp(cmd, "GET")) {
342 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
343 cprintf("%s\n", config.c_nodename);
344 cprintf("%s\n", config.c_fqdn);
345 cprintf("%s\n", config.c_humannode);
346 cprintf("%s\n", config.c_phonenum);
347 cprintf("%d\n", config.c_creataide);
348 cprintf("%d\n", config.c_sleeping);
349 cprintf("%d\n", config.c_initax);
350 cprintf("%d\n", config.c_regiscall);
351 cprintf("%d\n", config.c_twitdetect);
352 cprintf("%s\n", config.c_twitroom);
353 cprintf("%s\n", config.c_moreprompt);
354 cprintf("%d\n", config.c_restrict);
355 cprintf("%s\n", config.c_site_location);
356 cprintf("%s\n", config.c_sysadm);
357 cprintf("%d\n", config.c_maxsessions);
358 cprintf("xxx\n"); /* placeholder -- field no longer in use */
359 cprintf("%d\n", config.c_userpurge);
360 cprintf("%d\n", config.c_roompurge);
361 cprintf("%s\n", config.c_logpages);
362 cprintf("%d\n", config.c_createax);
363 cprintf("%ld\n", config.c_maxmsglen);
364 cprintf("%d\n", config.c_min_workers);
365 cprintf("%d\n", config.c_max_workers);
366 cprintf("%d\n", config.c_pop3_port);
367 cprintf("%d\n", config.c_smtp_port);
368 cprintf("%d\n", config.c_rfc822_strict_from);
369 cprintf("%d\n", config.c_aide_zap);
370 cprintf("%d\n", config.c_imap_port);
371 cprintf("%ld\n", config.c_net_freq);
372 cprintf("%d\n", config.c_disable_newu);
373 cprintf("1\n"); /* niu */
374 cprintf("%d\n", config.c_purge_hour);
376 cprintf("%s\n", config.c_ldap_host);
377 cprintf("%d\n", config.c_ldap_port);
378 cprintf("%s\n", config.c_ldap_base_dn);
379 cprintf("%s\n", config.c_ldap_bind_dn);
380 cprintf("%s\n", config.c_ldap_bind_pw);
388 cprintf("%s\n", config.c_ip_addr);
389 cprintf("%d\n", config.c_msa_port);
390 cprintf("%d\n", config.c_imaps_port);
391 cprintf("%d\n", config.c_pop3s_port);
392 cprintf("%d\n", config.c_smtps_port);
393 cprintf("%d\n", config.c_enable_fulltext);
394 cprintf("%d\n", config.c_auto_cull);
395 cprintf("%d\n", config.c_instant_expunge);
396 cprintf("%d\n", config.c_allow_spoofing);
397 cprintf("%d\n", config.c_journal_email);
398 cprintf("%d\n", config.c_journal_pubmsgs);
399 cprintf("%s\n", config.c_journal_dest);
400 cprintf("%s\n", config.c_default_cal_zone);
401 cprintf("%d\n", config.c_pftcpdict_port);
402 cprintf("%d\n", config.c_managesieve_port);
403 cprintf("%d\n", config.c_auth_mode);
404 cprintf("%s\n", config.c_funambol_host);
405 cprintf("%d\n", config.c_funambol_port);
406 cprintf("%s\n", config.c_funambol_source);
407 cprintf("%s\n", config.c_funambol_auth);
408 cprintf("%d\n", config.c_rbl_at_greeting);
409 cprintf("%s\n", config.c_master_user);
410 cprintf("%s\n", config.c_master_pass);
411 cprintf("%s\n", config.c_pager_program);
412 cprintf("%d\n", config.c_imap_keep_from);
413 cprintf("%d\n", config.c_xmpp_c2s_port);
414 cprintf("%d\n", config.c_xmpp_s2s_port);
415 cprintf("%ld\n", config.c_pop3_fetch);
416 cprintf("%ld\n", config.c_pop3_fastest);
417 cprintf("%d\n", config.c_spam_flag_only);
418 cprintf("%d\n", config.c_guest_logins);
419 cprintf("%d\n", config.c_port_number);
420 cprintf("%d\n", config.c_ctdluid);
424 else if (!strcasecmp(cmd, "SET")) {
426 cprintf("%d Send configuration...\n", SEND_LISTING);
428 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
431 safestrncpy(config.c_nodename, buf,
432 sizeof config.c_nodename);
435 safestrncpy(config.c_fqdn, buf,
436 sizeof config.c_fqdn);
439 safestrncpy(config.c_humannode, buf,
440 sizeof config.c_humannode);
443 safestrncpy(config.c_phonenum, buf,
444 sizeof config.c_phonenum);
447 config.c_creataide = atoi(buf);
450 config.c_sleeping = atoi(buf);
453 config.c_initax = atoi(buf);
454 if (config.c_initax < 1)
456 if (config.c_initax > 6)
460 config.c_regiscall = atoi(buf);
461 if (config.c_regiscall != 0)
462 config.c_regiscall = 1;
465 config.c_twitdetect = atoi(buf);
466 if (config.c_twitdetect != 0)
467 config.c_twitdetect = 1;
470 safestrncpy(config.c_twitroom, buf,
471 sizeof config.c_twitroom);
474 safestrncpy(config.c_moreprompt, buf,
475 sizeof config.c_moreprompt);
478 config.c_restrict = atoi(buf);
479 if (config.c_restrict != 0)
480 config.c_restrict = 1;
483 safestrncpy(config.c_site_location, buf,
484 sizeof config.c_site_location);
487 safestrncpy(config.c_sysadm, buf,
488 sizeof config.c_sysadm);
491 config.c_maxsessions = atoi(buf);
492 if (config.c_maxsessions < 0)
493 config.c_maxsessions = 0;
496 /* placeholder -- field no longer in use */
499 config.c_userpurge = atoi(buf);
502 config.c_roompurge = atoi(buf);
505 safestrncpy(config.c_logpages, buf,
506 sizeof config.c_logpages);
509 config.c_createax = atoi(buf);
510 if (config.c_createax < 1)
511 config.c_createax = 1;
512 if (config.c_createax > 6)
513 config.c_createax = 6;
516 if (atoi(buf) >= 8192)
517 config.c_maxmsglen = atoi(buf);
521 config.c_min_workers = atoi(buf);
523 if (atoi(buf) >= config.c_min_workers)
524 config.c_max_workers = atoi(buf);
526 config.c_pop3_port = atoi(buf);
529 config.c_smtp_port = atoi(buf);
532 config.c_rfc822_strict_from = atoi(buf);
535 config.c_aide_zap = atoi(buf);
536 if (config.c_aide_zap != 0)
537 config.c_aide_zap = 1;
540 config.c_imap_port = atoi(buf);
543 config.c_net_freq = atol(buf);
546 config.c_disable_newu = atoi(buf);
547 if (config.c_disable_newu != 0)
548 config.c_disable_newu = 1;
554 if ((config.c_purge_hour >= 0)
555 && (config.c_purge_hour <= 23)) {
556 config.c_purge_hour = atoi(buf);
561 safestrncpy(config.c_ldap_host, buf,
562 sizeof config.c_ldap_host);
565 config.c_ldap_port = atoi(buf);
568 safestrncpy(config.c_ldap_base_dn, buf,
569 sizeof config.c_ldap_base_dn);
572 safestrncpy(config.c_ldap_bind_dn, buf,
573 sizeof config.c_ldap_bind_dn);
576 safestrncpy(config.c_ldap_bind_pw, buf,
577 sizeof config.c_ldap_bind_pw);
581 safestrncpy(config.c_ip_addr, buf,
582 sizeof config.c_ip_addr);
584 config.c_msa_port = atoi(buf);
587 config.c_imaps_port = atoi(buf);
590 config.c_pop3s_port = atoi(buf);
593 config.c_smtps_port = atoi(buf);
596 config.c_enable_fulltext = atoi(buf);
599 config.c_auto_cull = atoi(buf);
602 config.c_instant_expunge = atoi(buf);
605 config.c_allow_spoofing = atoi(buf);
608 config.c_journal_email = atoi(buf);
611 config.c_journal_pubmsgs = atoi(buf);
614 safestrncpy(config.c_journal_dest, buf,
615 sizeof config.c_journal_dest);
617 safestrncpy(config.c_default_cal_zone, buf,
618 sizeof config.c_default_cal_zone);
621 config.c_pftcpdict_port = atoi(buf);
624 config.c_managesieve_port = atoi(buf);
627 config.c_auth_mode = atoi(buf);
629 safestrncpy(config.c_funambol_host, buf,
630 sizeof config.c_funambol_host);
633 config.c_funambol_port = atoi(buf);
636 safestrncpy(config.c_funambol_source,
638 sizeof config.c_funambol_source);
641 safestrncpy(config.c_funambol_auth,
643 sizeof config.c_funambol_auth);
646 config.c_rbl_at_greeting = atoi(buf);
649 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
652 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
655 safestrncpy(config.c_pager_program,
657 sizeof config.c_pager_program);
660 config.c_imap_keep_from = atoi(buf);
663 config.c_xmpp_c2s_port = atoi(buf);
666 config.c_xmpp_s2s_port = atoi(buf);
669 config.c_pop3_fetch = atol(buf);
672 config.c_pop3_fastest = atol(buf);
675 config.c_spam_flag_only = atoi(buf);
678 config.c_guest_logins = atoi(buf);
681 config.c_port_number = atoi(buf);
684 config.c_ctdluid = atoi(buf);
690 snprintf(buf, sizeof buf,
691 "The global system configuration has been edited by %s.\n",
692 (CC->logged_in ? CC->curr_user : "an administrator")
694 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
696 if (!IsEmptyStr(config.c_logpages))
697 CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
699 /* If full text indexing has been disabled, invalidate the
700 * index so it doesn't try to use it later.
702 if (config.c_enable_fulltext == 0) {
703 CitControl.fulltext_wordbreaker = 0;
708 else if (!strcasecmp(cmd, "GETSYS")) {
709 extract_token(confname, argbuf, 1, '|', sizeof confname);
710 confptr = CtdlGetSysConfig(confname);
711 if (confptr != NULL) {
714 len = strlen(confptr);
715 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
716 client_write(confptr, len);
717 if ((len > 0) && (confptr[len - 1] != 10))
718 client_write("\n", 1);
722 cprintf("%d No such configuration.\n",
723 ERROR + ILLEGAL_VALUE);
727 else if (!strcasecmp(cmd, "PUTSYS")) {
728 extract_token(confname, argbuf, 1, '|', sizeof confname);
730 cprintf("%d %s\n", SEND_LISTING, confname);
731 confptr = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
732 CtdlPutSysConfig(confname, confptr);
737 cprintf("%d Illegal option(s) specified.\n",
738 ERROR + ILLEGAL_VALUE);
742 typedef struct __ConfType {
747 ConfType CfgNames[] = {
748 { {HKEY("localhost") }, 0},
749 { {HKEY("directory") }, 0},
750 { {HKEY("smarthost") }, 2},
751 { {HKEY("fallbackhost") }, 2},
752 { {HKEY("rbl") }, 3},
753 { {HKEY("spamassassin") }, 3},
754 { {HKEY("masqdomain") }, 1},
755 { {HKEY("clamav") }, 3},
756 { {HKEY("notify") }, 3},
760 HashList *CfgNameHash = NULL;
761 void cmd_gvdn(char *argbuf)
763 const ConfType *pCfg;
765 long min = atol(argbuf);
766 const char *Pos = NULL;
767 const char *PPos = NULL;
778 List = NewHash(1, NULL);
779 Cfg = NewStrBufPlain(config.c_fqdn, -1);
780 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
783 confptr = CtdlGetSysConfig(INTERNETCFG);
784 Config = NewStrBufPlain(confptr, -1);
787 Line = NewStrBufPlain(NULL, StrLength(Config));
788 CfgToken = NewStrBufPlain(NULL, StrLength(Config));
789 while (StrBufSipLine(Line, Config, &Pos))
792 Cfg = NewStrBufPlain(NULL, StrLength(Line));
794 StrBufExtract_NextToken(Cfg, Line, &PPos, '|');
795 StrBufExtract_NextToken(CfgToken, Line, &PPos, '|');
796 if (GetHash(CfgNameHash, SKEY(CfgToken), &vptr) &&
799 pCfg = (ConfType *) vptr;
800 if (pCfg->Type <= min)
802 Put(List, SKEY(Cfg), Cfg, HFreeStrBuf);
808 cprintf("%d Valid Domains\n", LISTING_FOLLOWS);
809 It = GetNewHashPos(List, 1);
810 while (GetNextHashPos(List, It, &HKLen, &HKey, &vptr))
821 FreeStrBuf(&CfgToken);
825 /*****************************************************************************/
826 /* MODULE INITIALIZATION STUFF */
827 /*****************************************************************************/
830 CTDL_MODULE_INIT(control)
835 CfgNameHash = NewHash(1, NULL);
836 for (i = 0; CfgNames[i].Name.Key != NULL; i++)
837 Put(CfgNameHash, CKEY(CfgNames[i].Name), &CfgNames[i], reference_free_handler);
839 CtdlRegisterProtoHook(cmd_gvdn, "GVDN", "get valid domain names");
840 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
842 /* return our id for the Log */