4 * This module handles states which are global to the entire server.
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
20 # include <sys/time.h>
30 #include <sys/types.h>
33 #include <libcitadel.h>
37 #include "sysdep_decls.h"
41 #include "citserver.h"
51 #include "ctdl_module.h"
53 struct CitControl CitControl;
54 extern struct config config;
55 FILE *control_fp = NULL;
56 long control_highest_user = 0;
60 * lock_control - acquire a lock on the control record file.
61 * This keeps multiple citservers from running concurrently.
63 void lock_control(void)
67 * TODO: solaris manpages describe this function, but the headers
71 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
72 CtdlLogPrintf(CTDL_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
73 CtdlLogPrintf(CTDL_EMERG, "Is another citserver already running?\n");
74 exit(CTDLEXIT_CONTROL);
80 * callback to get highest room number when rebuilding control file
82 void control_find_highest(struct ctdlroom *qrbuf, void *data)
85 struct cdbdata *cdbfr;
90 int message_fixed = 0;
92 if (qrbuf->QRnumber > CitControl.MMnextroom)
94 CitControl.MMnextroom = qrbuf->QRnumber;
98 getroom (&room, qrbuf->QRname);
100 /* Load the message list */
101 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
103 msglist = (long *) cdbfr->ptr;
104 num_msgs = cdbfr->len / sizeof(long);
106 return; /* No messages at all? No further action. */
111 for (c=0; c<num_msgs; c++)
113 if (msglist[c] > CitControl.MMhighest)
115 CitControl.MMhighest = msglist[c];
122 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed room counter\n");
124 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed message count\n");
130 * Callback to get highest user number.
133 void control_find_user (struct ctdluser *EachUser, void *out_data)
137 if (EachUser->usernum > CitControl.MMnextuser)
139 CitControl.MMnextuser = EachUser->usernum;
143 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed user count\n");
148 * get_control - read the control record into memory.
150 void get_control(void)
152 static int already_have_control = 0;
155 * If we already have the control record in memory, there's no point
156 * in reading it from disk again.
158 if (already_have_control) return;
160 /* Zero it out. If the control record on disk is missing or short,
161 * the system functions with all control record fields initialized
164 memset(&CitControl, 0, sizeof(struct CitControl));
165 if (control_fp == NULL) {
166 control_fp = fopen(file_citadel_control, "rb+");
167 if (control_fp != NULL) {
169 fchown(fileno(control_fp), config.c_ctdluid, -1);
170 fchmod(fileno(control_fp),
174 if (control_fp == NULL) {
175 control_fp = fopen(file_citadel_control, "wb+");
176 if (control_fp != NULL) {
178 fchown(fileno(control_fp), config.c_ctdluid, -1);
179 fchmod(fileno(control_fp),
181 memset(&CitControl, 0, sizeof(struct CitControl));
182 fwrite(&CitControl, sizeof(struct CitControl),
187 if (control_fp == NULL) {
188 CtdlLogPrintf(CTDL_ALERT, "ERROR opening %s: %s\n",
189 file_citadel_control,
195 fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
196 already_have_control = 1;
197 chown(file_citadel_control, config.c_ctdluid, (-1));
202 * put_control - write the control record to disk.
204 void put_control(void)
207 if (control_fp != NULL) {
209 fwrite(&CitControl, sizeof(struct CitControl), 1,
217 * check_control - check the control record has sensible values for message, user and room numbers
219 void check_control(void)
221 CtdlLogPrintf(CTDL_INFO, "Checking/re-building control record\n");
223 // Find highest room number and message number.
224 ForEachRoom(control_find_highest, NULL);
225 ForEachUser(control_find_user, NULL);
231 * release_control - close our fd on exit
233 void release_control(void)
235 if (control_fp != NULL)
241 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
243 long get_new_message_number(void)
246 begin_critical_section(S_CONTROL);
248 retval = ++CitControl.MMhighest;
250 end_critical_section(S_CONTROL);
256 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
257 * This provides a quick way to initialise a variable that might be used to indicate
258 * messages that should not be processed. EG. a new Sieve script will use this
259 * to record determine that messages older than this should not be processed.
261 long CtdlGetCurrentMessageNumber(void)
264 begin_critical_section(S_CONTROL);
266 retval = CitControl.MMhighest;
267 end_critical_section(S_CONTROL);
273 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
275 long get_new_user_number(void)
278 begin_critical_section(S_CONTROL);
280 retval = ++CitControl.MMnextuser;
282 end_critical_section(S_CONTROL);
289 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
291 long get_new_room_number(void)
294 begin_critical_section(S_CONTROL);
296 retval = ++CitControl.MMnextroom;
298 end_critical_section(S_CONTROL);
305 * Get or set global configuration options
307 void cmd_conf(char *argbuf)
315 if (CtdlAccessCheck(ac_aide)) return;
317 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
318 if (!strcasecmp(cmd, "GET")) {
319 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
320 cprintf("%s\n", config.c_nodename);
321 cprintf("%s\n", config.c_fqdn);
322 cprintf("%s\n", config.c_humannode);
323 cprintf("%s\n", config.c_phonenum);
324 cprintf("%d\n", config.c_creataide);
325 cprintf("%d\n", config.c_sleeping);
326 cprintf("%d\n", config.c_initax);
327 cprintf("%d\n", config.c_regiscall);
328 cprintf("%d\n", config.c_twitdetect);
329 cprintf("%s\n", config.c_twitroom);
330 cprintf("%s\n", config.c_moreprompt);
331 cprintf("%d\n", config.c_restrict);
332 cprintf("%s\n", config.c_site_location);
333 cprintf("%s\n", config.c_sysadm);
334 cprintf("%d\n", config.c_maxsessions);
335 cprintf("xxx\n"); /* placeholder -- field no longer in use */
336 cprintf("%d\n", config.c_userpurge);
337 cprintf("%d\n", config.c_roompurge);
338 cprintf("%s\n", config.c_logpages);
339 cprintf("%d\n", config.c_createax);
340 cprintf("%ld\n", config.c_maxmsglen);
341 cprintf("%d\n", config.c_min_workers);
342 cprintf("%d\n", config.c_max_workers);
343 cprintf("%d\n", config.c_pop3_port);
344 cprintf("%d\n", config.c_smtp_port);
345 cprintf("%d\n", config.c_rfc822_strict_from);
346 cprintf("%d\n", config.c_aide_zap);
347 cprintf("%d\n", config.c_imap_port);
348 cprintf("%ld\n", config.c_net_freq);
349 cprintf("%d\n", config.c_disable_newu);
350 cprintf("1\n"); /* niu */
351 cprintf("%d\n", config.c_purge_hour);
353 cprintf("%s\n", config.c_ldap_host);
354 cprintf("%d\n", config.c_ldap_port);
355 cprintf("%s\n", config.c_ldap_base_dn);
356 cprintf("%s\n", config.c_ldap_bind_dn);
357 cprintf("%s\n", config.c_ldap_bind_pw);
365 cprintf("%s\n", config.c_ip_addr);
366 cprintf("%d\n", config.c_msa_port);
367 cprintf("%d\n", config.c_imaps_port);
368 cprintf("%d\n", config.c_pop3s_port);
369 cprintf("%d\n", config.c_smtps_port);
370 cprintf("%d\n", config.c_enable_fulltext);
371 cprintf("%d\n", config.c_auto_cull);
372 cprintf("%d\n", config.c_instant_expunge);
373 cprintf("%d\n", config.c_allow_spoofing);
374 cprintf("%d\n", config.c_journal_email);
375 cprintf("%d\n", config.c_journal_pubmsgs);
376 cprintf("%s\n", config.c_journal_dest);
377 cprintf("%s\n", config.c_default_cal_zone);
378 cprintf("%d\n", config.c_pftcpdict_port);
379 cprintf("%d\n", config.c_managesieve_port);
380 cprintf("%d\n", config.c_auth_mode);
381 cprintf("%s\n", config.c_funambol_host);
382 cprintf("%d\n", config.c_funambol_port);
383 cprintf("%s\n", config.c_funambol_source);
384 cprintf("%s\n", config.c_funambol_auth);
385 cprintf("%d\n", config.c_rbl_at_greeting);
386 cprintf("%s\n", config.c_master_user);
387 cprintf("%s\n", config.c_master_pass);
388 cprintf("%s\n", config.c_pager_program);
389 cprintf("%d\n", config.c_imap_keep_from);
390 cprintf("%d\n", config.c_xmpp_c2s_port);
391 cprintf("%d\n", config.c_xmpp_s2s_port);
392 cprintf("%ld\n", config.c_pop3_fetch);
393 cprintf("%ld\n", config.c_pop3_fastest);
394 cprintf("%d\n", config.c_spam_flag_only);
398 else if (!strcasecmp(cmd, "SET")) {
400 cprintf("%d Send configuration...\n", SEND_LISTING);
402 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
405 safestrncpy(config.c_nodename, buf,
406 sizeof config.c_nodename);
409 safestrncpy(config.c_fqdn, buf,
410 sizeof config.c_fqdn);
413 safestrncpy(config.c_humannode, buf,
414 sizeof config.c_humannode);
417 safestrncpy(config.c_phonenum, buf,
418 sizeof config.c_phonenum);
421 config.c_creataide = atoi(buf);
424 config.c_sleeping = atoi(buf);
427 config.c_initax = atoi(buf);
428 if (config.c_initax < 1)
430 if (config.c_initax > 6)
434 config.c_regiscall = atoi(buf);
435 if (config.c_regiscall != 0)
436 config.c_regiscall = 1;
439 config.c_twitdetect = atoi(buf);
440 if (config.c_twitdetect != 0)
441 config.c_twitdetect = 1;
444 safestrncpy(config.c_twitroom, buf,
445 sizeof config.c_twitroom);
448 safestrncpy(config.c_moreprompt, buf,
449 sizeof config.c_moreprompt);
452 config.c_restrict = atoi(buf);
453 if (config.c_restrict != 0)
454 config.c_restrict = 1;
457 safestrncpy(config.c_site_location, buf,
458 sizeof config.c_site_location);
461 safestrncpy(config.c_sysadm, buf,
462 sizeof config.c_sysadm);
465 config.c_maxsessions = atoi(buf);
466 if (config.c_maxsessions < 0)
467 config.c_maxsessions = 0;
470 /* placeholder -- field no longer in use */
473 config.c_userpurge = atoi(buf);
476 config.c_roompurge = atoi(buf);
479 safestrncpy(config.c_logpages, buf,
480 sizeof config.c_logpages);
483 config.c_createax = atoi(buf);
484 if (config.c_createax < 1)
485 config.c_createax = 1;
486 if (config.c_createax > 6)
487 config.c_createax = 6;
490 if (atoi(buf) >= 8192)
491 config.c_maxmsglen = atoi(buf);
495 config.c_min_workers = atoi(buf);
497 if (atoi(buf) >= config.c_min_workers)
498 config.c_max_workers = atoi(buf);
500 config.c_pop3_port = atoi(buf);
503 config.c_smtp_port = atoi(buf);
506 config.c_rfc822_strict_from = atoi(buf);
509 config.c_aide_zap = atoi(buf);
510 if (config.c_aide_zap != 0)
511 config.c_aide_zap = 1;
514 config.c_imap_port = atoi(buf);
517 config.c_net_freq = atol(buf);
520 config.c_disable_newu = atoi(buf);
521 if (config.c_disable_newu != 0)
522 config.c_disable_newu = 1;
528 if ((config.c_purge_hour >= 0)
529 && (config.c_purge_hour <= 23)) {
530 config.c_purge_hour = atoi(buf);
535 safestrncpy(config.c_ldap_host, buf,
536 sizeof config.c_ldap_host);
539 config.c_ldap_port = atoi(buf);
542 safestrncpy(config.c_ldap_base_dn, buf,
543 sizeof config.c_ldap_base_dn);
546 safestrncpy(config.c_ldap_bind_dn, buf,
547 sizeof config.c_ldap_bind_dn);
550 safestrncpy(config.c_ldap_bind_pw, buf,
551 sizeof config.c_ldap_bind_pw);
555 safestrncpy(config.c_ip_addr, buf,
556 sizeof config.c_ip_addr);
558 config.c_msa_port = atoi(buf);
561 config.c_imaps_port = atoi(buf);
564 config.c_pop3s_port = atoi(buf);
567 config.c_smtps_port = atoi(buf);
570 config.c_enable_fulltext = atoi(buf);
573 config.c_auto_cull = atoi(buf);
576 config.c_instant_expunge = atoi(buf);
579 config.c_allow_spoofing = atoi(buf);
582 config.c_journal_email = atoi(buf);
585 config.c_journal_pubmsgs = atoi(buf);
588 safestrncpy(config.c_journal_dest, buf,
589 sizeof config.c_journal_dest);
591 safestrncpy(config.c_default_cal_zone, buf,
592 sizeof config.c_default_cal_zone);
595 config.c_pftcpdict_port = atoi(buf);
598 config.c_managesieve_port = atoi(buf);
601 config.c_auth_mode = atoi(buf);
603 safestrncpy(config.c_funambol_host, buf,
604 sizeof config.c_funambol_host);
607 config.c_funambol_port = atoi(buf);
610 safestrncpy(config.c_funambol_source,
612 sizeof config.c_funambol_source);
615 safestrncpy(config.c_funambol_auth,
617 sizeof config.c_funambol_auth);
620 config.c_rbl_at_greeting = atoi(buf);
623 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
626 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
629 safestrncpy(config.c_pager_program,
631 sizeof config.c_pager_program);
634 config.c_imap_keep_from = atoi(buf);
637 config.c_xmpp_c2s_port = atoi(buf);
640 config.c_xmpp_s2s_port = atoi(buf);
643 config.c_pop3_fetch = atol(buf);
646 config.c_pop3_fastest = atol(buf);
649 config.c_spam_flag_only = atoi(buf);
655 snprintf(buf, sizeof buf,
656 "The global system configuration has been edited by %s.\n",
658 aide_message(buf,"Citadel Configuration Manager Message");
660 if (!IsEmptyStr(config.c_logpages))
661 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
663 /* If full text indexing has been disabled, invalidate the
664 * index so it doesn't try to use it later.
666 if (config.c_enable_fulltext == 0) {
667 CitControl.fulltext_wordbreaker = 0;
672 else if (!strcasecmp(cmd, "GETSYS")) {
673 extract_token(confname, argbuf, 1, '|', sizeof confname);
674 confptr = CtdlGetSysConfig(confname);
675 if (confptr != NULL) {
676 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
677 client_write(confptr, strlen(confptr));
678 if (confptr[strlen(confptr) - 1] != 10)
679 client_write("\n", 1);
683 cprintf("%d No such configuration.\n",
684 ERROR + ILLEGAL_VALUE);
688 else if (!strcasecmp(cmd, "PUTSYS")) {
689 extract_token(confname, argbuf, 1, '|', sizeof confname);
691 cprintf("%d %s\n", SEND_LISTING, confname);
692 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
693 CtdlPutSysConfig(confname, confptr);
698 cprintf("%d Illegal option(s) specified.\n",
699 ERROR + ILLEGAL_VALUE);
704 /*****************************************************************************/
705 /* MODULE INITIALIZATION STUFF */
706 /*****************************************************************************/
709 CTDL_MODULE_INIT(control)
711 CtdlRegisterProtoHook(cmd_conf, "CONF", "Autoconverted. TODO: document me.");
712 /* return our Subversion id for the Log */