2 * This module handles states which are global to the entire server.
4 * Copyright (c) 1987-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
33 # include <sys/time.h>
43 #include <sys/types.h>
46 #include <libcitadel.h>
50 #include "sysdep_decls.h"
54 #include "citserver.h"
64 #include "ctdl_module.h"
66 struct CitControl CitControl;
67 extern struct config config;
68 FILE *control_fp = NULL;
69 long control_highest_user = 0;
73 * lock_control - acquire a lock on the control record file.
74 * This keeps multiple citservers from running concurrently.
76 void lock_control(void)
78 #if defined(LOCK_EX) && defined(LOCK_NB)
79 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
80 CtdlLogPrintf(CTDL_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
81 CtdlLogPrintf(CTDL_EMERG, "Is another citserver already running?\n");
82 exit(CTDLEXIT_CONTROL);
88 * callback to get highest room number when rebuilding control file
90 void control_find_highest(struct ctdlroom *qrbuf, void *data)
93 struct cdbdata *cdbfr;
98 int message_fixed = 0;
100 if (qrbuf->QRnumber > CitControl.MMnextroom)
102 CitControl.MMnextroom = qrbuf->QRnumber;
106 CtdlGetRoom (&room, qrbuf->QRname);
108 /* Load the message list */
109 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
111 msglist = (long *) cdbfr->ptr;
112 num_msgs = cdbfr->len / sizeof(long);
114 return; /* No messages at all? No further action. */
119 for (c=0; c<num_msgs; c++)
121 if (msglist[c] > CitControl.MMhighest)
123 CitControl.MMhighest = msglist[c];
130 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed room counter\n");
132 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed message count\n");
138 * Callback to get highest user number.
141 void control_find_user (struct ctdluser *EachUser, void *out_data)
145 if (EachUser->usernum > CitControl.MMnextuser)
147 CitControl.MMnextuser = EachUser->usernum;
151 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed user count\n");
156 * get_control - read the control record into memory.
158 void get_control(void)
160 static int already_have_control = 0;
164 * If we already have the control record in memory, there's no point
165 * in reading it from disk again.
167 if (already_have_control) return;
169 /* Zero it out. If the control record on disk is missing or short,
170 * the system functions with all control record fields initialized
173 memset(&CitControl, 0, sizeof(struct CitControl));
174 if (control_fp == NULL) {
175 control_fp = fopen(file_citadel_control, "rb+");
176 if (control_fp != NULL) {
178 rv = fchown(fileno(control_fp), config.c_ctdluid, -1);
179 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
182 if (control_fp == NULL) {
183 control_fp = fopen(file_citadel_control, "wb+");
184 if (control_fp != NULL) {
186 rv = fchown(fileno(control_fp), config.c_ctdluid, -1);
187 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
188 memset(&CitControl, 0, sizeof(struct CitControl));
189 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
193 if (control_fp == NULL) {
194 CtdlLogPrintf(CTDL_ALERT, "ERROR opening %s: %s\n", file_citadel_control, strerror(errno));
199 rv = fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
200 already_have_control = 1;
201 rv = chown(file_citadel_control, config.c_ctdluid, (-1));
206 * put_control - write the control record to disk.
208 void put_control(void)
212 if (control_fp != NULL) {
214 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
221 * check_control - check the control record has sensible values for message, user and room numbers
223 void check_control(void)
225 CtdlLogPrintf(CTDL_INFO, "Checking/re-building control record\n");
227 // Find highest room number and message number.
228 CtdlForEachRoom(control_find_highest, NULL);
229 ForEachUser(control_find_user, NULL);
235 * release_control - close our fd on exit
237 void release_control(void)
239 if (control_fp != NULL) {
246 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
248 long get_new_message_number(void)
251 begin_critical_section(S_CONTROL);
253 retval = ++CitControl.MMhighest;
255 end_critical_section(S_CONTROL);
261 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
262 * This provides a quick way to initialise a variable that might be used to indicate
263 * messages that should not be processed. EG. a new Sieve script will use this
264 * to record determine that messages older than this should not be processed.
266 long CtdlGetCurrentMessageNumber(void)
269 begin_critical_section(S_CONTROL);
271 retval = CitControl.MMhighest;
272 end_critical_section(S_CONTROL);
278 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
280 long get_new_user_number(void)
283 begin_critical_section(S_CONTROL);
285 retval = ++CitControl.MMnextuser;
287 end_critical_section(S_CONTROL);
294 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
296 long get_new_room_number(void)
299 begin_critical_section(S_CONTROL);
301 retval = ++CitControl.MMnextroom;
303 end_critical_section(S_CONTROL);
310 * Get or set global configuration options
312 void cmd_conf(char *argbuf)
320 if (CtdlAccessCheck(ac_aide)) return;
322 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
323 if (!strcasecmp(cmd, "GET")) {
324 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
325 cprintf("%s\n", config.c_nodename);
326 cprintf("%s\n", config.c_fqdn);
327 cprintf("%s\n", config.c_humannode);
328 cprintf("%s\n", config.c_phonenum);
329 cprintf("%d\n", config.c_creataide);
330 cprintf("%d\n", config.c_sleeping);
331 cprintf("%d\n", config.c_initax);
332 cprintf("%d\n", config.c_regiscall);
333 cprintf("%d\n", config.c_twitdetect);
334 cprintf("%s\n", config.c_twitroom);
335 cprintf("%s\n", config.c_moreprompt);
336 cprintf("%d\n", config.c_restrict);
337 cprintf("%s\n", config.c_site_location);
338 cprintf("%s\n", config.c_sysadm);
339 cprintf("%d\n", config.c_maxsessions);
340 cprintf("xxx\n"); /* placeholder -- field no longer in use */
341 cprintf("%d\n", config.c_userpurge);
342 cprintf("%d\n", config.c_roompurge);
343 cprintf("%s\n", config.c_logpages);
344 cprintf("%d\n", config.c_createax);
345 cprintf("%ld\n", config.c_maxmsglen);
346 cprintf("%d\n", config.c_min_workers);
347 cprintf("%d\n", config.c_max_workers);
348 cprintf("%d\n", config.c_pop3_port);
349 cprintf("%d\n", config.c_smtp_port);
350 cprintf("%d\n", config.c_rfc822_strict_from);
351 cprintf("%d\n", config.c_aide_zap);
352 cprintf("%d\n", config.c_imap_port);
353 cprintf("%ld\n", config.c_net_freq);
354 cprintf("%d\n", config.c_disable_newu);
355 cprintf("1\n"); /* niu */
356 cprintf("%d\n", config.c_purge_hour);
358 cprintf("%s\n", config.c_ldap_host);
359 cprintf("%d\n", config.c_ldap_port);
360 cprintf("%s\n", config.c_ldap_base_dn);
361 cprintf("%s\n", config.c_ldap_bind_dn);
362 cprintf("%s\n", config.c_ldap_bind_pw);
370 cprintf("%s\n", config.c_ip_addr);
371 cprintf("%d\n", config.c_msa_port);
372 cprintf("%d\n", config.c_imaps_port);
373 cprintf("%d\n", config.c_pop3s_port);
374 cprintf("%d\n", config.c_smtps_port);
375 cprintf("%d\n", config.c_enable_fulltext);
376 cprintf("%d\n", config.c_auto_cull);
377 cprintf("%d\n", config.c_instant_expunge);
378 cprintf("%d\n", config.c_allow_spoofing);
379 cprintf("%d\n", config.c_journal_email);
380 cprintf("%d\n", config.c_journal_pubmsgs);
381 cprintf("%s\n", config.c_journal_dest);
382 cprintf("%s\n", config.c_default_cal_zone);
383 cprintf("%d\n", config.c_pftcpdict_port);
384 cprintf("%d\n", config.c_managesieve_port);
385 cprintf("%d\n", config.c_auth_mode);
386 cprintf("%s\n", config.c_funambol_host);
387 cprintf("%d\n", config.c_funambol_port);
388 cprintf("%s\n", config.c_funambol_source);
389 cprintf("%s\n", config.c_funambol_auth);
390 cprintf("%d\n", config.c_rbl_at_greeting);
391 cprintf("%s\n", config.c_master_user);
392 cprintf("%s\n", config.c_master_pass);
393 cprintf("%s\n", config.c_pager_program);
394 cprintf("%d\n", config.c_imap_keep_from);
395 cprintf("%d\n", config.c_xmpp_c2s_port);
396 cprintf("%d\n", config.c_xmpp_s2s_port);
397 cprintf("%ld\n", config.c_pop3_fetch);
398 cprintf("%ld\n", config.c_pop3_fastest);
399 cprintf("%d\n", config.c_spam_flag_only);
403 else if (!strcasecmp(cmd, "SET")) {
405 cprintf("%d Send configuration...\n", SEND_LISTING);
407 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
410 safestrncpy(config.c_nodename, buf,
411 sizeof config.c_nodename);
414 safestrncpy(config.c_fqdn, buf,
415 sizeof config.c_fqdn);
418 safestrncpy(config.c_humannode, buf,
419 sizeof config.c_humannode);
422 safestrncpy(config.c_phonenum, buf,
423 sizeof config.c_phonenum);
426 config.c_creataide = atoi(buf);
429 config.c_sleeping = atoi(buf);
432 config.c_initax = atoi(buf);
433 if (config.c_initax < 1)
435 if (config.c_initax > 6)
439 config.c_regiscall = atoi(buf);
440 if (config.c_regiscall != 0)
441 config.c_regiscall = 1;
444 config.c_twitdetect = atoi(buf);
445 if (config.c_twitdetect != 0)
446 config.c_twitdetect = 1;
449 safestrncpy(config.c_twitroom, buf,
450 sizeof config.c_twitroom);
453 safestrncpy(config.c_moreprompt, buf,
454 sizeof config.c_moreprompt);
457 config.c_restrict = atoi(buf);
458 if (config.c_restrict != 0)
459 config.c_restrict = 1;
462 safestrncpy(config.c_site_location, buf,
463 sizeof config.c_site_location);
466 safestrncpy(config.c_sysadm, buf,
467 sizeof config.c_sysadm);
470 config.c_maxsessions = atoi(buf);
471 if (config.c_maxsessions < 0)
472 config.c_maxsessions = 0;
475 /* placeholder -- field no longer in use */
478 config.c_userpurge = atoi(buf);
481 config.c_roompurge = atoi(buf);
484 safestrncpy(config.c_logpages, buf,
485 sizeof config.c_logpages);
488 config.c_createax = atoi(buf);
489 if (config.c_createax < 1)
490 config.c_createax = 1;
491 if (config.c_createax > 6)
492 config.c_createax = 6;
495 if (atoi(buf) >= 8192)
496 config.c_maxmsglen = atoi(buf);
500 config.c_min_workers = atoi(buf);
502 if (atoi(buf) >= config.c_min_workers)
503 config.c_max_workers = atoi(buf);
505 config.c_pop3_port = atoi(buf);
508 config.c_smtp_port = atoi(buf);
511 config.c_rfc822_strict_from = atoi(buf);
514 config.c_aide_zap = atoi(buf);
515 if (config.c_aide_zap != 0)
516 config.c_aide_zap = 1;
519 config.c_imap_port = atoi(buf);
522 config.c_net_freq = atol(buf);
525 config.c_disable_newu = atoi(buf);
526 if (config.c_disable_newu != 0)
527 config.c_disable_newu = 1;
533 if ((config.c_purge_hour >= 0)
534 && (config.c_purge_hour <= 23)) {
535 config.c_purge_hour = atoi(buf);
540 safestrncpy(config.c_ldap_host, buf,
541 sizeof config.c_ldap_host);
544 config.c_ldap_port = atoi(buf);
547 safestrncpy(config.c_ldap_base_dn, buf,
548 sizeof config.c_ldap_base_dn);
551 safestrncpy(config.c_ldap_bind_dn, buf,
552 sizeof config.c_ldap_bind_dn);
555 safestrncpy(config.c_ldap_bind_pw, buf,
556 sizeof config.c_ldap_bind_pw);
560 safestrncpy(config.c_ip_addr, buf,
561 sizeof config.c_ip_addr);
563 config.c_msa_port = atoi(buf);
566 config.c_imaps_port = atoi(buf);
569 config.c_pop3s_port = atoi(buf);
572 config.c_smtps_port = atoi(buf);
575 config.c_enable_fulltext = atoi(buf);
578 config.c_auto_cull = atoi(buf);
581 config.c_instant_expunge = atoi(buf);
584 config.c_allow_spoofing = atoi(buf);
587 config.c_journal_email = atoi(buf);
590 config.c_journal_pubmsgs = atoi(buf);
593 safestrncpy(config.c_journal_dest, buf,
594 sizeof config.c_journal_dest);
596 safestrncpy(config.c_default_cal_zone, buf,
597 sizeof config.c_default_cal_zone);
600 config.c_pftcpdict_port = atoi(buf);
603 config.c_managesieve_port = atoi(buf);
606 config.c_auth_mode = atoi(buf);
608 safestrncpy(config.c_funambol_host, buf,
609 sizeof config.c_funambol_host);
612 config.c_funambol_port = atoi(buf);
615 safestrncpy(config.c_funambol_source,
617 sizeof config.c_funambol_source);
620 safestrncpy(config.c_funambol_auth,
622 sizeof config.c_funambol_auth);
625 config.c_rbl_at_greeting = atoi(buf);
628 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
631 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
634 safestrncpy(config.c_pager_program,
636 sizeof config.c_pager_program);
639 config.c_imap_keep_from = atoi(buf);
642 config.c_xmpp_c2s_port = atoi(buf);
645 config.c_xmpp_s2s_port = atoi(buf);
648 config.c_pop3_fetch = atol(buf);
651 config.c_pop3_fastest = atol(buf);
654 config.c_spam_flag_only = atoi(buf);
660 snprintf(buf, sizeof buf,
661 "The global system configuration has been edited by %s.\n",
663 CtdlAideMessage(buf,"Citadel Configuration Manager Message");
665 if (!IsEmptyStr(config.c_logpages))
666 CtdlCreateRoom(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
668 /* If full text indexing has been disabled, invalidate the
669 * index so it doesn't try to use it later.
671 if (config.c_enable_fulltext == 0) {
672 CitControl.fulltext_wordbreaker = 0;
677 else if (!strcasecmp(cmd, "GETSYS")) {
678 extract_token(confname, argbuf, 1, '|', sizeof confname);
679 confptr = CtdlGetSysConfig(confname);
680 if (confptr != NULL) {
681 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
682 client_write(confptr, strlen(confptr));
683 if (confptr[strlen(confptr) - 1] != 10)
684 client_write("\n", 1);
688 cprintf("%d No such configuration.\n",
689 ERROR + ILLEGAL_VALUE);
693 else if (!strcasecmp(cmd, "PUTSYS")) {
694 extract_token(confname, argbuf, 1, '|', sizeof confname);
696 cprintf("%d %s\n", SEND_LISTING, confname);
697 confptr = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
698 CtdlPutSysConfig(confname, confptr);
703 cprintf("%d Illegal option(s) specified.\n",
704 ERROR + ILLEGAL_VALUE);
709 /*****************************************************************************/
710 /* MODULE INITIALIZATION STUFF */
711 /*****************************************************************************/
714 CTDL_MODULE_INIT(control)
717 CtdlRegisterProtoHook(cmd_conf, "CONF", "get/set system configuration");
719 /* return our Subversion id for the Log */