4 * This module handles states which are global to the entire server.
6 * Copyright (c) 1987-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
45 #include <sys/types.h>
48 #include <libcitadel.h>
52 #include "sysdep_decls.h"
56 #include "citserver.h"
66 #include "ctdl_module.h"
68 struct CitControl CitControl;
69 extern struct config config;
70 FILE *control_fp = NULL;
71 long control_highest_user = 0;
75 * lock_control - acquire a lock on the control record file.
76 * This keeps multiple citservers from running concurrently.
78 void lock_control(void)
80 #if defined(LOCK_EX) && defined(LOCK_NB)
81 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
82 CtdlLogPrintf(CTDL_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
83 CtdlLogPrintf(CTDL_EMERG, "Is another citserver already running?\n");
84 exit(CTDLEXIT_CONTROL);
90 * callback to get highest room number when rebuilding control file
92 void control_find_highest(struct ctdlroom *qrbuf, void *data)
95 struct cdbdata *cdbfr;
100 int message_fixed = 0;
102 if (qrbuf->QRnumber > CitControl.MMnextroom)
104 CitControl.MMnextroom = qrbuf->QRnumber;
108 CtdlGetRoom (&room, qrbuf->QRname);
110 /* Load the message list */
111 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
113 msglist = (long *) cdbfr->ptr;
114 num_msgs = cdbfr->len / sizeof(long);
116 return; /* No messages at all? No further action. */
121 for (c=0; c<num_msgs; c++)
123 if (msglist[c] > CitControl.MMhighest)
125 CitControl.MMhighest = msglist[c];
132 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed room counter\n");
134 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed message count\n");
140 * Callback to get highest user number.
143 void control_find_user (struct ctdluser *EachUser, void *out_data)
147 if (EachUser->usernum > CitControl.MMnextuser)
149 CitControl.MMnextuser = EachUser->usernum;
153 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed user count\n");
158 * get_control - read the control record into memory.
160 void get_control(void)
162 static int already_have_control = 0;
166 * If we already have the control record in memory, there's no point
167 * in reading it from disk again.
169 if (already_have_control) return;
171 /* Zero it out. If the control record on disk is missing or short,
172 * the system functions with all control record fields initialized
175 memset(&CitControl, 0, sizeof(struct CitControl));
176 if (control_fp == NULL) {
177 control_fp = fopen(file_citadel_control, "rb+");
178 if (control_fp != NULL) {
180 rv = fchown(fileno(control_fp), config.c_ctdluid, -1);
181 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
184 if (control_fp == NULL) {
185 control_fp = fopen(file_citadel_control, "wb+");
186 if (control_fp != NULL) {
188 rv = fchown(fileno(control_fp), config.c_ctdluid, -1);
189 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
190 memset(&CitControl, 0, sizeof(struct CitControl));
191 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
195 if (control_fp == NULL) {
196 CtdlLogPrintf(CTDL_ALERT, "ERROR opening %s: %s\n", file_citadel_control, strerror(errno));
201 rv = fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
202 already_have_control = 1;
203 rv = chown(file_citadel_control, config.c_ctdluid, (-1));
208 * put_control - write the control record to disk.
210 void put_control(void)
214 if (control_fp != NULL) {
216 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
223 * check_control - check the control record has sensible values for message, user and room numbers
225 void check_control(void)
227 CtdlLogPrintf(CTDL_INFO, "Checking/re-building control record\n");
229 // Find highest room number and message number.
230 CtdlForEachRoom(control_find_highest, NULL);
231 ForEachUser(control_find_user, NULL);
237 * release_control - close our fd on exit
239 void release_control(void)
241 if (control_fp != NULL)
247 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
249 long get_new_message_number(void)
252 begin_critical_section(S_CONTROL);
254 retval = ++CitControl.MMhighest;
256 end_critical_section(S_CONTROL);
262 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
263 * This provides a quick way to initialise a variable that might be used to indicate
264 * messages that should not be processed. EG. a new Sieve script will use this
265 * to record determine that messages older than this should not be processed.
267 long CtdlGetCurrentMessageNumber(void)
270 begin_critical_section(S_CONTROL);
272 retval = CitControl.MMhighest;
273 end_critical_section(S_CONTROL);
279 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
281 long get_new_user_number(void)
284 begin_critical_section(S_CONTROL);
286 retval = ++CitControl.MMnextuser;
288 end_critical_section(S_CONTROL);
295 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
297 long get_new_room_number(void)
300 begin_critical_section(S_CONTROL);
302 retval = ++CitControl.MMnextroom;
304 end_critical_section(S_CONTROL);
311 * Get or set global configuration options
313 void cmd_conf(char *argbuf)
321 if (CtdlAccessCheck(ac_aide)) return;
323 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
324 if (!strcasecmp(cmd, "GET")) {
325 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
326 cprintf("%s\n", config.c_nodename);
327 cprintf("%s\n", config.c_fqdn);
328 cprintf("%s\n", config.c_humannode);
329 cprintf("%s\n", config.c_phonenum);
330 cprintf("%d\n", config.c_creataide);
331 cprintf("%d\n", config.c_sleeping);
332 cprintf("%d\n", config.c_initax);
333 cprintf("%d\n", config.c_regiscall);
334 cprintf("%d\n", config.c_twitdetect);
335 cprintf("%s\n", config.c_twitroom);
336 cprintf("%s\n", config.c_moreprompt);
337 cprintf("%d\n", config.c_restrict);
338 cprintf("%s\n", config.c_site_location);
339 cprintf("%s\n", config.c_sysadm);
340 cprintf("%d\n", config.c_maxsessions);
341 cprintf("xxx\n"); /* placeholder -- field no longer in use */
342 cprintf("%d\n", config.c_userpurge);
343 cprintf("%d\n", config.c_roompurge);
344 cprintf("%s\n", config.c_logpages);
345 cprintf("%d\n", config.c_createax);
346 cprintf("%ld\n", config.c_maxmsglen);
347 cprintf("%d\n", config.c_min_workers);
348 cprintf("%d\n", config.c_max_workers);
349 cprintf("%d\n", config.c_pop3_port);
350 cprintf("%d\n", config.c_smtp_port);
351 cprintf("%d\n", config.c_rfc822_strict_from);
352 cprintf("%d\n", config.c_aide_zap);
353 cprintf("%d\n", config.c_imap_port);
354 cprintf("%ld\n", config.c_net_freq);
355 cprintf("%d\n", config.c_disable_newu);
356 cprintf("1\n"); /* niu */
357 cprintf("%d\n", config.c_purge_hour);
359 cprintf("%s\n", config.c_ldap_host);
360 cprintf("%d\n", config.c_ldap_port);
361 cprintf("%s\n", config.c_ldap_base_dn);
362 cprintf("%s\n", config.c_ldap_bind_dn);
363 cprintf("%s\n", config.c_ldap_bind_pw);
371 cprintf("%s\n", config.c_ip_addr);
372 cprintf("%d\n", config.c_msa_port);
373 cprintf("%d\n", config.c_imaps_port);
374 cprintf("%d\n", config.c_pop3s_port);
375 cprintf("%d\n", config.c_smtps_port);
376 cprintf("%d\n", config.c_enable_fulltext);
377 cprintf("%d\n", config.c_auto_cull);
378 cprintf("%d\n", config.c_instant_expunge);
379 cprintf("%d\n", config.c_allow_spoofing);
380 cprintf("%d\n", config.c_journal_email);
381 cprintf("%d\n", config.c_journal_pubmsgs);
382 cprintf("%s\n", config.c_journal_dest);
383 cprintf("%s\n", config.c_default_cal_zone);
384 cprintf("%d\n", config.c_pftcpdict_port);
385 cprintf("%d\n", config.c_managesieve_port);
386 cprintf("%d\n", config.c_auth_mode);
387 cprintf("%s\n", config.c_funambol_host);
388 cprintf("%d\n", config.c_funambol_port);
389 cprintf("%s\n", config.c_funambol_source);
390 cprintf("%s\n", config.c_funambol_auth);
391 cprintf("%d\n", config.c_rbl_at_greeting);
392 cprintf("%s\n", config.c_master_user);
393 cprintf("%s\n", config.c_master_pass);
394 cprintf("%s\n", config.c_pager_program);
395 cprintf("%d\n", config.c_imap_keep_from);
396 cprintf("%d\n", config.c_xmpp_c2s_port);
397 cprintf("%d\n", config.c_xmpp_s2s_port);
398 cprintf("%ld\n", config.c_pop3_fetch);
399 cprintf("%ld\n", config.c_pop3_fastest);
400 cprintf("%d\n", config.c_spam_flag_only);
404 else if (!strcasecmp(cmd, "SET")) {
406 cprintf("%d Send configuration...\n", SEND_LISTING);
408 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
411 safestrncpy(config.c_nodename, buf,
412 sizeof config.c_nodename);
415 safestrncpy(config.c_fqdn, buf,
416 sizeof config.c_fqdn);
419 safestrncpy(config.c_humannode, buf,
420 sizeof config.c_humannode);
423 safestrncpy(config.c_phonenum, buf,
424 sizeof config.c_phonenum);
427 config.c_creataide = atoi(buf);
430 config.c_sleeping = atoi(buf);
433 config.c_initax = atoi(buf);
434 if (config.c_initax < 1)
436 if (config.c_initax > 6)
440 config.c_regiscall = atoi(buf);
441 if (config.c_regiscall != 0)
442 config.c_regiscall = 1;
445 config.c_twitdetect = atoi(buf);
446 if (config.c_twitdetect != 0)
447 config.c_twitdetect = 1;
450 safestrncpy(config.c_twitroom, buf,
451 sizeof config.c_twitroom);
454 safestrncpy(config.c_moreprompt, buf,
455 sizeof config.c_moreprompt);
458 config.c_restrict = atoi(buf);
459 if (config.c_restrict != 0)
460 config.c_restrict = 1;
463 safestrncpy(config.c_site_location, buf,
464 sizeof config.c_site_location);
467 safestrncpy(config.c_sysadm, buf,
468 sizeof config.c_sysadm);
471 config.c_maxsessions = atoi(buf);
472 if (config.c_maxsessions < 0)
473 config.c_maxsessions = 0;
476 /* placeholder -- field no longer in use */
479 config.c_userpurge = atoi(buf);
482 config.c_roompurge = atoi(buf);
485 safestrncpy(config.c_logpages, buf,
486 sizeof config.c_logpages);
489 config.c_createax = atoi(buf);
490 if (config.c_createax < 1)
491 config.c_createax = 1;
492 if (config.c_createax > 6)
493 config.c_createax = 6;
496 if (atoi(buf) >= 8192)
497 config.c_maxmsglen = atoi(buf);
501 config.c_min_workers = atoi(buf);
503 if (atoi(buf) >= config.c_min_workers)
504 config.c_max_workers = atoi(buf);
506 config.c_pop3_port = atoi(buf);
509 config.c_smtp_port = atoi(buf);
512 config.c_rfc822_strict_from = atoi(buf);
515 config.c_aide_zap = atoi(buf);
516 if (config.c_aide_zap != 0)
517 config.c_aide_zap = 1;
520 config.c_imap_port = atoi(buf);
523 config.c_net_freq = atol(buf);
526 config.c_disable_newu = atoi(buf);
527 if (config.c_disable_newu != 0)
528 config.c_disable_newu = 1;
534 if ((config.c_purge_hour >= 0)
535 && (config.c_purge_hour <= 23)) {
536 config.c_purge_hour = atoi(buf);
541 safestrncpy(config.c_ldap_host, buf,
542 sizeof config.c_ldap_host);
545 config.c_ldap_port = atoi(buf);
548 safestrncpy(config.c_ldap_base_dn, buf,
549 sizeof config.c_ldap_base_dn);
552 safestrncpy(config.c_ldap_bind_dn, buf,
553 sizeof config.c_ldap_bind_dn);
556 safestrncpy(config.c_ldap_bind_pw, buf,
557 sizeof config.c_ldap_bind_pw);
561 safestrncpy(config.c_ip_addr, buf,
562 sizeof config.c_ip_addr);
564 config.c_msa_port = atoi(buf);
567 config.c_imaps_port = atoi(buf);
570 config.c_pop3s_port = atoi(buf);
573 config.c_smtps_port = atoi(buf);
576 config.c_enable_fulltext = atoi(buf);
579 config.c_auto_cull = atoi(buf);
582 config.c_instant_expunge = atoi(buf);
585 config.c_allow_spoofing = atoi(buf);
588 config.c_journal_email = atoi(buf);
591 config.c_journal_pubmsgs = atoi(buf);
594 safestrncpy(config.c_journal_dest, buf,
595 sizeof config.c_journal_dest);
597 safestrncpy(config.c_default_cal_zone, buf,
598 sizeof config.c_default_cal_zone);
601 config.c_pftcpdict_port = atoi(buf);
604 config.c_managesieve_port = atoi(buf);
607 config.c_auth_mode = atoi(buf);
609 safestrncpy(config.c_funambol_host, buf,
610 sizeof config.c_funambol_host);
613 config.c_funambol_port = atoi(buf);
616 safestrncpy(config.c_funambol_source,
618 sizeof config.c_funambol_source);
621 safestrncpy(config.c_funambol_auth,
623 sizeof config.c_funambol_auth);
626 config.c_rbl_at_greeting = atoi(buf);
629 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
632 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
635 safestrncpy(config.c_pager_program,
637 sizeof config.c_pager_program);
640 config.c_imap_keep_from = atoi(buf);
643 config.c_xmpp_c2s_port = atoi(buf);
646 config.c_xmpp_s2s_port = atoi(buf);
649 config.c_pop3_fetch = atol(buf);
652 config.c_pop3_fastest = atol(buf);
655 config.c_spam_flag_only = atoi(buf);
661 snprintf(buf, sizeof buf,
662 "The global system configuration has been edited by %s.\n",
664 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
666 if (!IsEmptyStr(config.c_logpages))
667 CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
669 /* If full text indexing has been disabled, invalidate the
670 * index so it doesn't try to use it later.
672 if (config.c_enable_fulltext == 0) {
673 CitControl.fulltext_wordbreaker = 0;
678 else if (!strcasecmp(cmd, "GETSYS")) {
679 extract_token(confname, argbuf, 1, '|', sizeof confname);
680 confptr = CtdlGetSysConfig(confname);
681 if (confptr != NULL) {
682 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
683 client_write(confptr, strlen(confptr));
684 if (confptr[strlen(confptr) - 1] != 10)
685 client_write("\n", 1);
689 cprintf("%d No such configuration.\n",
690 ERROR + ILLEGAL_VALUE);
694 else if (!strcasecmp(cmd, "PUTSYS")) {
695 extract_token(confname, argbuf, 1, '|', sizeof confname);
697 cprintf("%d %s\n", SEND_LISTING, confname);
698 confptr = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
699 CtdlPutSysConfig(confname, confptr);
704 cprintf("%d Illegal option(s) specified.\n",
705 ERROR + ILLEGAL_VALUE);
710 /*****************************************************************************/
711 /* MODULE INITIALIZATION STUFF */
712 /*****************************************************************************/
715 CTDL_MODULE_INIT(control)
718 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
720 /* return our Subversion id for the Log */