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"
58 #include "ctdl_module.h"
60 struct CitControl CitControl;
61 extern struct config config;
62 FILE *control_fp = NULL;
63 long control_highest_user = 0;
67 * lock_control - acquire a lock on the control record file.
68 * This keeps multiple citservers from running concurrently.
70 void lock_control(void)
72 #if defined(LOCK_EX) && defined(LOCK_NB)
73 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
74 syslog(LOG_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
75 syslog(LOG_EMERG, "Is another citserver already running?\n");
76 exit(CTDLEXIT_CONTROL);
82 * callback to get highest room number when rebuilding control file
84 void control_find_highest(struct ctdlroom *qrbuf, void *data)
87 struct cdbdata *cdbfr;
92 int message_fixed = 0;
94 if (qrbuf->QRnumber > CitControl.MMnextroom)
96 CitControl.MMnextroom = qrbuf->QRnumber;
100 CtdlGetRoom (&room, qrbuf->QRname);
102 /* Load the message list */
103 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
105 msglist = (long *) cdbfr->ptr;
106 num_msgs = cdbfr->len / sizeof(long);
108 return; /* No messages at all? No further action. */
113 for (c=0; c<num_msgs; c++)
115 if (msglist[c] > CitControl.MMhighest)
117 CitControl.MMhighest = msglist[c];
124 syslog(LOG_INFO, "Control record checking....Fixed room counter\n");
126 syslog(LOG_INFO, "Control record checking....Fixed message count\n");
132 * Callback to get highest user number.
135 void control_find_user (struct ctdluser *EachUser, void *out_data)
139 if (EachUser->usernum > CitControl.MMnextuser)
141 CitControl.MMnextuser = EachUser->usernum;
145 syslog(LOG_INFO, "Control record checking....Fixed user count\n");
150 * get_control - read the control record into memory.
152 void get_control(void)
154 static int already_have_control = 0;
158 * If we already have the control record in memory, there's no point
159 * in reading it from disk again.
161 if (already_have_control) return;
163 /* Zero it out. If the control record on disk is missing or short,
164 * the system functions with all control record fields initialized
167 memset(&CitControl, 0, sizeof(struct CitControl));
168 if (control_fp == NULL) {
169 control_fp = fopen(file_citadel_control, "rb+");
170 if (control_fp != NULL) {
172 rv = fchown(fileno(control_fp), config.c_ctdluid, -1);
174 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
175 file_citadel_control, strerror(errno));
176 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
178 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n",
179 file_citadel_control, strerror(errno));
182 if (control_fp == NULL) {
183 control_fp = fopen(file_citadel_control, "wb+");
184 if (control_fp != NULL) {
186 memset(&CitControl, 0, sizeof(struct CitControl));
188 rv = fchown(fileno(control_fp), config.c_ctdluid, -1);
190 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
191 file_citadel_control, strerror(errno));
193 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
195 syslog(LOG_EMERG, "Failed to adjust accessrights of: %s [%s]\n",
196 file_citadel_control, strerror(errno));
197 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
199 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
200 file_citadel_control, strerror(errno));
204 if (control_fp == NULL) {
205 syslog(LOG_ALERT, "ERROR opening %s: %s\n", file_citadel_control, strerror(errno));
210 rv = fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
212 syslog(LOG_EMERG, "Failed to read Controlfile: %s [%s]\n",
213 file_citadel_control, strerror(errno));
214 already_have_control = 1;
215 rv = chown(file_citadel_control, config.c_ctdluid, (-1));
217 syslog(LOG_EMERG, "Failed to adjust ownership of: %s [%s]\n",
218 file_citadel_control, strerror(errno));
222 * put_control - write the control record to disk.
224 void put_control(void)
228 if (control_fp != NULL) {
230 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
232 syslog(LOG_EMERG, "Failed to write: %s [%s]\n",
233 file_citadel_control, strerror(errno));
240 * check_control - check the control record has sensible values for message, user and room numbers
242 void check_control(void)
244 syslog(LOG_INFO, "Checking/re-building control record\n");
246 // Find highest room number and message number.
247 CtdlForEachRoom(control_find_highest, NULL);
248 ForEachUser(control_find_user, NULL);
254 * release_control - close our fd on exit
256 void release_control(void)
258 if (control_fp != NULL) {
265 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
267 long get_new_message_number(void)
270 begin_critical_section(S_CONTROL);
272 retval = ++CitControl.MMhighest;
274 end_critical_section(S_CONTROL);
280 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
281 * This provides a quick way to initialise a variable that might be used to indicate
282 * messages that should not be processed. EG. a new Sieve script will use this
283 * to record determine that messages older than this should not be processed.
285 long CtdlGetCurrentMessageNumber(void)
288 begin_critical_section(S_CONTROL);
290 retval = CitControl.MMhighest;
291 end_critical_section(S_CONTROL);
297 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
299 long get_new_user_number(void)
302 begin_critical_section(S_CONTROL);
304 retval = ++CitControl.MMnextuser;
306 end_critical_section(S_CONTROL);
313 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
315 long get_new_room_number(void)
318 begin_critical_section(S_CONTROL);
320 retval = ++CitControl.MMnextroom;
322 end_critical_section(S_CONTROL);
329 * Get or set global configuration options
331 * IF YOU ADD OR CHANGE FIELDS HERE, YOU *MUST* DOCUMENT YOUR CHANGES AT:
332 * http://www.citadel.org/doku.php/documentation:appproto:system_config
335 void cmd_conf(char *argbuf)
343 if (CtdlAccessCheck(ac_aide)) return;
345 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
346 if (!strcasecmp(cmd, "GET")) {
347 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
348 cprintf("%s\n", config.c_nodename);
349 cprintf("%s\n", config.c_fqdn);
350 cprintf("%s\n", config.c_humannode);
351 cprintf("%s\n", config.c_phonenum);
352 cprintf("%d\n", config.c_creataide);
353 cprintf("%d\n", config.c_sleeping);
354 cprintf("%d\n", config.c_initax);
355 cprintf("%d\n", config.c_regiscall);
356 cprintf("%d\n", config.c_twitdetect);
357 cprintf("%s\n", config.c_twitroom);
358 cprintf("%s\n", config.c_moreprompt);
359 cprintf("%d\n", config.c_restrict);
360 cprintf("%s\n", config.c_site_location);
361 cprintf("%s\n", config.c_sysadm);
362 cprintf("%d\n", config.c_maxsessions);
363 cprintf("xxx\n"); /* placeholder -- field no longer in use */
364 cprintf("%d\n", config.c_userpurge);
365 cprintf("%d\n", config.c_roompurge);
366 cprintf("%s\n", config.c_logpages);
367 cprintf("%d\n", config.c_createax);
368 cprintf("%ld\n", config.c_maxmsglen);
369 cprintf("%d\n", config.c_min_workers);
370 cprintf("%d\n", config.c_max_workers);
371 cprintf("%d\n", config.c_pop3_port);
372 cprintf("%d\n", config.c_smtp_port);
373 cprintf("%d\n", config.c_rfc822_strict_from);
374 cprintf("%d\n", config.c_aide_zap);
375 cprintf("%d\n", config.c_imap_port);
376 cprintf("%ld\n", config.c_net_freq);
377 cprintf("%d\n", config.c_disable_newu);
378 cprintf("1\n"); /* niu */
379 cprintf("%d\n", config.c_purge_hour);
381 cprintf("%s\n", config.c_ldap_host);
382 cprintf("%d\n", config.c_ldap_port);
383 cprintf("%s\n", config.c_ldap_base_dn);
384 cprintf("%s\n", config.c_ldap_bind_dn);
385 cprintf("%s\n", config.c_ldap_bind_pw);
393 cprintf("%s\n", config.c_ip_addr);
394 cprintf("%d\n", config.c_msa_port);
395 cprintf("%d\n", config.c_imaps_port);
396 cprintf("%d\n", config.c_pop3s_port);
397 cprintf("%d\n", config.c_smtps_port);
398 cprintf("%d\n", config.c_enable_fulltext);
399 cprintf("%d\n", config.c_auto_cull);
400 cprintf("%d\n", config.c_instant_expunge);
401 cprintf("%d\n", config.c_allow_spoofing);
402 cprintf("%d\n", config.c_journal_email);
403 cprintf("%d\n", config.c_journal_pubmsgs);
404 cprintf("%s\n", config.c_journal_dest);
405 cprintf("%s\n", config.c_default_cal_zone);
406 cprintf("%d\n", config.c_pftcpdict_port);
407 cprintf("%d\n", config.c_managesieve_port);
408 cprintf("%d\n", config.c_auth_mode);
409 cprintf("%s\n", config.c_funambol_host);
410 cprintf("%d\n", config.c_funambol_port);
411 cprintf("%s\n", config.c_funambol_source);
412 cprintf("%s\n", config.c_funambol_auth);
413 cprintf("%d\n", config.c_rbl_at_greeting);
414 cprintf("%s\n", config.c_master_user);
415 cprintf("%s\n", config.c_master_pass);
416 cprintf("%s\n", config.c_pager_program);
417 cprintf("%d\n", config.c_imap_keep_from);
418 cprintf("%d\n", config.c_xmpp_c2s_port);
419 cprintf("%d\n", config.c_xmpp_s2s_port);
420 cprintf("%ld\n", config.c_pop3_fetch);
421 cprintf("%ld\n", config.c_pop3_fastest);
422 cprintf("%d\n", config.c_spam_flag_only);
423 cprintf("%d\n", config.c_guest_logins);
424 cprintf("%d\n", config.c_port_number);
425 cprintf("%d\n", config.c_ctdluid);
429 else if (!strcasecmp(cmd, "SET")) {
431 cprintf("%d Send configuration...\n", SEND_LISTING);
433 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
436 safestrncpy(config.c_nodename, buf,
437 sizeof config.c_nodename);
440 safestrncpy(config.c_fqdn, buf,
441 sizeof config.c_fqdn);
444 safestrncpy(config.c_humannode, buf,
445 sizeof config.c_humannode);
448 safestrncpy(config.c_phonenum, buf,
449 sizeof config.c_phonenum);
452 config.c_creataide = atoi(buf);
455 config.c_sleeping = atoi(buf);
458 config.c_initax = atoi(buf);
459 if (config.c_initax < 1)
461 if (config.c_initax > 6)
465 config.c_regiscall = atoi(buf);
466 if (config.c_regiscall != 0)
467 config.c_regiscall = 1;
470 config.c_twitdetect = atoi(buf);
471 if (config.c_twitdetect != 0)
472 config.c_twitdetect = 1;
475 safestrncpy(config.c_twitroom, buf,
476 sizeof config.c_twitroom);
479 safestrncpy(config.c_moreprompt, buf,
480 sizeof config.c_moreprompt);
483 config.c_restrict = atoi(buf);
484 if (config.c_restrict != 0)
485 config.c_restrict = 1;
488 safestrncpy(config.c_site_location, buf,
489 sizeof config.c_site_location);
492 safestrncpy(config.c_sysadm, buf,
493 sizeof config.c_sysadm);
496 config.c_maxsessions = atoi(buf);
497 if (config.c_maxsessions < 0)
498 config.c_maxsessions = 0;
501 /* placeholder -- field no longer in use */
504 config.c_userpurge = atoi(buf);
507 config.c_roompurge = atoi(buf);
510 safestrncpy(config.c_logpages, buf,
511 sizeof config.c_logpages);
514 config.c_createax = atoi(buf);
515 if (config.c_createax < 1)
516 config.c_createax = 1;
517 if (config.c_createax > 6)
518 config.c_createax = 6;
521 if (atoi(buf) >= 8192)
522 config.c_maxmsglen = atoi(buf);
526 config.c_min_workers = atoi(buf);
528 if (atoi(buf) >= config.c_min_workers)
529 config.c_max_workers = atoi(buf);
531 config.c_pop3_port = atoi(buf);
534 config.c_smtp_port = atoi(buf);
537 config.c_rfc822_strict_from = atoi(buf);
540 config.c_aide_zap = atoi(buf);
541 if (config.c_aide_zap != 0)
542 config.c_aide_zap = 1;
545 config.c_imap_port = atoi(buf);
548 config.c_net_freq = atol(buf);
551 config.c_disable_newu = atoi(buf);
552 if (config.c_disable_newu != 0)
553 config.c_disable_newu = 1;
559 if ((config.c_purge_hour >= 0)
560 && (config.c_purge_hour <= 23)) {
561 config.c_purge_hour = atoi(buf);
566 safestrncpy(config.c_ldap_host, buf,
567 sizeof config.c_ldap_host);
570 config.c_ldap_port = atoi(buf);
573 safestrncpy(config.c_ldap_base_dn, buf,
574 sizeof config.c_ldap_base_dn);
577 safestrncpy(config.c_ldap_bind_dn, buf,
578 sizeof config.c_ldap_bind_dn);
581 safestrncpy(config.c_ldap_bind_pw, buf,
582 sizeof config.c_ldap_bind_pw);
586 safestrncpy(config.c_ip_addr, buf,
587 sizeof config.c_ip_addr);
589 config.c_msa_port = atoi(buf);
592 config.c_imaps_port = atoi(buf);
595 config.c_pop3s_port = atoi(buf);
598 config.c_smtps_port = atoi(buf);
601 config.c_enable_fulltext = atoi(buf);
604 config.c_auto_cull = atoi(buf);
607 config.c_instant_expunge = atoi(buf);
610 config.c_allow_spoofing = atoi(buf);
613 config.c_journal_email = atoi(buf);
616 config.c_journal_pubmsgs = atoi(buf);
619 safestrncpy(config.c_journal_dest, buf,
620 sizeof config.c_journal_dest);
622 safestrncpy(config.c_default_cal_zone, buf,
623 sizeof config.c_default_cal_zone);
626 config.c_pftcpdict_port = atoi(buf);
629 config.c_managesieve_port = atoi(buf);
632 config.c_auth_mode = atoi(buf);
634 safestrncpy(config.c_funambol_host, buf,
635 sizeof config.c_funambol_host);
638 config.c_funambol_port = atoi(buf);
641 safestrncpy(config.c_funambol_source,
643 sizeof config.c_funambol_source);
646 safestrncpy(config.c_funambol_auth,
648 sizeof config.c_funambol_auth);
651 config.c_rbl_at_greeting = atoi(buf);
654 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
657 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
660 safestrncpy(config.c_pager_program,
662 sizeof config.c_pager_program);
665 config.c_imap_keep_from = atoi(buf);
668 config.c_xmpp_c2s_port = atoi(buf);
671 config.c_xmpp_s2s_port = atoi(buf);
674 config.c_pop3_fetch = atol(buf);
677 config.c_pop3_fastest = atol(buf);
680 config.c_spam_flag_only = atoi(buf);
683 config.c_guest_logins = atoi(buf);
686 config.c_port_number = atoi(buf);
689 config.c_ctdluid = atoi(buf);
695 snprintf(buf, sizeof buf,
696 "The global system configuration has been edited by %s.\n",
698 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
700 if (!IsEmptyStr(config.c_logpages))
701 CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
703 /* If full text indexing has been disabled, invalidate the
704 * index so it doesn't try to use it later.
706 if (config.c_enable_fulltext == 0) {
707 CitControl.fulltext_wordbreaker = 0;
712 else if (!strcasecmp(cmd, "GETSYS")) {
713 extract_token(confname, argbuf, 1, '|', sizeof confname);
714 confptr = CtdlGetSysConfig(confname);
715 if (confptr != NULL) {
718 len = strlen(confptr);
719 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
720 client_write(confptr, len);
721 if ((len > 0) && (confptr[len - 1] != 10))
722 client_write("\n", 1);
726 cprintf("%d No such configuration.\n",
727 ERROR + ILLEGAL_VALUE);
731 else if (!strcasecmp(cmd, "PUTSYS")) {
732 extract_token(confname, argbuf, 1, '|', sizeof confname);
734 cprintf("%d %s\n", SEND_LISTING, confname);
735 confptr = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
736 CtdlPutSysConfig(confname, confptr);
741 cprintf("%d Illegal option(s) specified.\n",
742 ERROR + ILLEGAL_VALUE);
747 /*****************************************************************************/
748 /* MODULE INITIALIZATION STUFF */
749 /*****************************************************************************/
752 CTDL_MODULE_INIT(control)
755 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
757 /* return our id for the Log */