4 * This module handles states which are global to the entire server.
6 * Copyright (c) 1987-2010 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) {
248 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
250 long get_new_message_number(void)
253 begin_critical_section(S_CONTROL);
255 retval = ++CitControl.MMhighest;
257 end_critical_section(S_CONTROL);
263 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
264 * This provides a quick way to initialise a variable that might be used to indicate
265 * messages that should not be processed. EG. a new Sieve script will use this
266 * to record determine that messages older than this should not be processed.
268 long CtdlGetCurrentMessageNumber(void)
271 begin_critical_section(S_CONTROL);
273 retval = CitControl.MMhighest;
274 end_critical_section(S_CONTROL);
280 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
282 long get_new_user_number(void)
285 begin_critical_section(S_CONTROL);
287 retval = ++CitControl.MMnextuser;
289 end_critical_section(S_CONTROL);
296 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
298 long get_new_room_number(void)
301 begin_critical_section(S_CONTROL);
303 retval = ++CitControl.MMnextroom;
305 end_critical_section(S_CONTROL);
312 * Get or set global configuration options
314 void cmd_conf(char *argbuf)
322 if (CtdlAccessCheck(ac_aide)) return;
324 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
325 if (!strcasecmp(cmd, "GET")) {
326 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
327 cprintf("%s\n", config.c_nodename);
328 cprintf("%s\n", config.c_fqdn);
329 cprintf("%s\n", config.c_humannode);
330 cprintf("%s\n", config.c_phonenum);
331 cprintf("%d\n", config.c_creataide);
332 cprintf("%d\n", config.c_sleeping);
333 cprintf("%d\n", config.c_initax);
334 cprintf("%d\n", config.c_regiscall);
335 cprintf("%d\n", config.c_twitdetect);
336 cprintf("%s\n", config.c_twitroom);
337 cprintf("%s\n", config.c_moreprompt);
338 cprintf("%d\n", config.c_restrict);
339 cprintf("%s\n", config.c_site_location);
340 cprintf("%s\n", config.c_sysadm);
341 cprintf("%d\n", config.c_maxsessions);
342 cprintf("xxx\n"); /* placeholder -- field no longer in use */
343 cprintf("%d\n", config.c_userpurge);
344 cprintf("%d\n", config.c_roompurge);
345 cprintf("%s\n", config.c_logpages);
346 cprintf("%d\n", config.c_createax);
347 cprintf("%ld\n", config.c_maxmsglen);
348 cprintf("%d\n", config.c_min_workers);
349 cprintf("%d\n", config.c_max_workers);
350 cprintf("%d\n", config.c_pop3_port);
351 cprintf("%d\n", config.c_smtp_port);
352 cprintf("%d\n", config.c_rfc822_strict_from);
353 cprintf("%d\n", config.c_aide_zap);
354 cprintf("%d\n", config.c_imap_port);
355 cprintf("%ld\n", config.c_net_freq);
356 cprintf("%d\n", config.c_disable_newu);
357 cprintf("1\n"); /* niu */
358 cprintf("%d\n", config.c_purge_hour);
360 cprintf("%s\n", config.c_ldap_host);
361 cprintf("%d\n", config.c_ldap_port);
362 cprintf("%s\n", config.c_ldap_base_dn);
363 cprintf("%s\n", config.c_ldap_bind_dn);
364 cprintf("%s\n", config.c_ldap_bind_pw);
372 cprintf("%s\n", config.c_ip_addr);
373 cprintf("%d\n", config.c_msa_port);
374 cprintf("%d\n", config.c_imaps_port);
375 cprintf("%d\n", config.c_pop3s_port);
376 cprintf("%d\n", config.c_smtps_port);
377 cprintf("%d\n", config.c_enable_fulltext);
378 cprintf("%d\n", config.c_auto_cull);
379 cprintf("%d\n", config.c_instant_expunge);
380 cprintf("%d\n", config.c_allow_spoofing);
381 cprintf("%d\n", config.c_journal_email);
382 cprintf("%d\n", config.c_journal_pubmsgs);
383 cprintf("%s\n", config.c_journal_dest);
384 cprintf("%s\n", config.c_default_cal_zone);
385 cprintf("%d\n", config.c_pftcpdict_port);
386 cprintf("%d\n", config.c_managesieve_port);
387 cprintf("%d\n", config.c_auth_mode);
388 cprintf("%s\n", config.c_funambol_host);
389 cprintf("%d\n", config.c_funambol_port);
390 cprintf("%s\n", config.c_funambol_source);
391 cprintf("%s\n", config.c_funambol_auth);
392 cprintf("%d\n", config.c_rbl_at_greeting);
393 cprintf("%s\n", config.c_master_user);
394 cprintf("%s\n", config.c_master_pass);
395 cprintf("%s\n", config.c_pager_program);
396 cprintf("%d\n", config.c_imap_keep_from);
397 cprintf("%d\n", config.c_xmpp_c2s_port);
398 cprintf("%d\n", config.c_xmpp_s2s_port);
399 cprintf("%ld\n", config.c_pop3_fetch);
400 cprintf("%ld\n", config.c_pop3_fastest);
401 cprintf("%d\n", config.c_spam_flag_only);
405 else if (!strcasecmp(cmd, "SET")) {
407 cprintf("%d Send configuration...\n", SEND_LISTING);
409 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
412 safestrncpy(config.c_nodename, buf,
413 sizeof config.c_nodename);
416 safestrncpy(config.c_fqdn, buf,
417 sizeof config.c_fqdn);
420 safestrncpy(config.c_humannode, buf,
421 sizeof config.c_humannode);
424 safestrncpy(config.c_phonenum, buf,
425 sizeof config.c_phonenum);
428 config.c_creataide = atoi(buf);
431 config.c_sleeping = atoi(buf);
434 config.c_initax = atoi(buf);
435 if (config.c_initax < 1)
437 if (config.c_initax > 6)
441 config.c_regiscall = atoi(buf);
442 if (config.c_regiscall != 0)
443 config.c_regiscall = 1;
446 config.c_twitdetect = atoi(buf);
447 if (config.c_twitdetect != 0)
448 config.c_twitdetect = 1;
451 safestrncpy(config.c_twitroom, buf,
452 sizeof config.c_twitroom);
455 safestrncpy(config.c_moreprompt, buf,
456 sizeof config.c_moreprompt);
459 config.c_restrict = atoi(buf);
460 if (config.c_restrict != 0)
461 config.c_restrict = 1;
464 safestrncpy(config.c_site_location, buf,
465 sizeof config.c_site_location);
468 safestrncpy(config.c_sysadm, buf,
469 sizeof config.c_sysadm);
472 config.c_maxsessions = atoi(buf);
473 if (config.c_maxsessions < 0)
474 config.c_maxsessions = 0;
477 /* placeholder -- field no longer in use */
480 config.c_userpurge = atoi(buf);
483 config.c_roompurge = atoi(buf);
486 safestrncpy(config.c_logpages, buf,
487 sizeof config.c_logpages);
490 config.c_createax = atoi(buf);
491 if (config.c_createax < 1)
492 config.c_createax = 1;
493 if (config.c_createax > 6)
494 config.c_createax = 6;
497 if (atoi(buf) >= 8192)
498 config.c_maxmsglen = atoi(buf);
502 config.c_min_workers = atoi(buf);
504 if (atoi(buf) >= config.c_min_workers)
505 config.c_max_workers = atoi(buf);
507 config.c_pop3_port = atoi(buf);
510 config.c_smtp_port = atoi(buf);
513 config.c_rfc822_strict_from = atoi(buf);
516 config.c_aide_zap = atoi(buf);
517 if (config.c_aide_zap != 0)
518 config.c_aide_zap = 1;
521 config.c_imap_port = atoi(buf);
524 config.c_net_freq = atol(buf);
527 config.c_disable_newu = atoi(buf);
528 if (config.c_disable_newu != 0)
529 config.c_disable_newu = 1;
535 if ((config.c_purge_hour >= 0)
536 && (config.c_purge_hour <= 23)) {
537 config.c_purge_hour = atoi(buf);
542 safestrncpy(config.c_ldap_host, buf,
543 sizeof config.c_ldap_host);
546 config.c_ldap_port = atoi(buf);
549 safestrncpy(config.c_ldap_base_dn, buf,
550 sizeof config.c_ldap_base_dn);
553 safestrncpy(config.c_ldap_bind_dn, buf,
554 sizeof config.c_ldap_bind_dn);
557 safestrncpy(config.c_ldap_bind_pw, buf,
558 sizeof config.c_ldap_bind_pw);
562 safestrncpy(config.c_ip_addr, buf,
563 sizeof config.c_ip_addr);
565 config.c_msa_port = atoi(buf);
568 config.c_imaps_port = atoi(buf);
571 config.c_pop3s_port = atoi(buf);
574 config.c_smtps_port = atoi(buf);
577 config.c_enable_fulltext = atoi(buf);
580 config.c_auto_cull = atoi(buf);
583 config.c_instant_expunge = atoi(buf);
586 config.c_allow_spoofing = atoi(buf);
589 config.c_journal_email = atoi(buf);
592 config.c_journal_pubmsgs = atoi(buf);
595 safestrncpy(config.c_journal_dest, buf,
596 sizeof config.c_journal_dest);
598 safestrncpy(config.c_default_cal_zone, buf,
599 sizeof config.c_default_cal_zone);
602 config.c_pftcpdict_port = atoi(buf);
605 config.c_managesieve_port = atoi(buf);
608 config.c_auth_mode = atoi(buf);
610 safestrncpy(config.c_funambol_host, buf,
611 sizeof config.c_funambol_host);
614 config.c_funambol_port = atoi(buf);
617 safestrncpy(config.c_funambol_source,
619 sizeof config.c_funambol_source);
622 safestrncpy(config.c_funambol_auth,
624 sizeof config.c_funambol_auth);
627 config.c_rbl_at_greeting = atoi(buf);
630 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
633 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
636 safestrncpy(config.c_pager_program,
638 sizeof config.c_pager_program);
641 config.c_imap_keep_from = atoi(buf);
644 config.c_xmpp_c2s_port = atoi(buf);
647 config.c_xmpp_s2s_port = atoi(buf);
650 config.c_pop3_fetch = atol(buf);
653 config.c_pop3_fastest = atol(buf);
656 config.c_spam_flag_only = atoi(buf);
662 snprintf(buf, sizeof buf,
663 "The global system configuration has been edited by %s.\n",
665 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
667 if (!IsEmptyStr(config.c_logpages))
668 CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
670 /* If full text indexing has been disabled, invalidate the
671 * index so it doesn't try to use it later.
673 if (config.c_enable_fulltext == 0) {
674 CitControl.fulltext_wordbreaker = 0;
679 else if (!strcasecmp(cmd, "GETSYS")) {
680 extract_token(confname, argbuf, 1, '|', sizeof confname);
681 confptr = CtdlGetSysConfig(confname);
682 if (confptr != NULL) {
683 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
684 client_write(confptr, strlen(confptr));
685 if (confptr[strlen(confptr) - 1] != 10)
686 client_write("\n", 1);
690 cprintf("%d No such configuration.\n",
691 ERROR + ILLEGAL_VALUE);
695 else if (!strcasecmp(cmd, "PUTSYS")) {
696 extract_token(confname, argbuf, 1, '|', sizeof confname);
698 cprintf("%d %s\n", SEND_LISTING, confname);
699 confptr = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
700 CtdlPutSysConfig(confname, confptr);
705 cprintf("%d Illegal option(s) specified.\n",
706 ERROR + ILLEGAL_VALUE);
711 /*****************************************************************************/
712 /* MODULE INITIALIZATION STUFF */
713 /*****************************************************************************/
716 CTDL_MODULE_INIT(control)
719 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
721 /* return our Subversion id for the Log */