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>
32 #include <libcitadel.h>
36 #include "sysdep_decls.h"
40 #include "citserver.h"
50 struct CitControl CitControl;
51 extern struct config config;
52 FILE *control_fp = NULL;
53 long control_highest_user = 0;
57 * lock_control - acquire a lock on the control record file.
58 * This keeps multiple citservers from running concurrently.
60 void lock_control(void)
64 * TODO: solaris manpages describe this function, but the headers
68 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
69 CtdlLogPrintf(CTDL_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
70 CtdlLogPrintf(CTDL_EMERG, "Is another citserver already running?\n");
71 exit(CTDLEXIT_CONTROL);
77 * callback to get highest room number when rebuilding control file
79 void control_find_highest(struct ctdlroom *qrbuf, void *data)
82 struct cdbdata *cdbfr;
87 int message_fixed = 0;
89 if (qrbuf->QRnumber > CitControl.MMnextroom)
91 CitControl.MMnextroom = qrbuf->QRnumber;
95 getroom (&room, qrbuf->QRname);
97 /* Load the message list */
98 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
100 msglist = (long *) cdbfr->ptr;
101 num_msgs = cdbfr->len / sizeof(long);
103 return; /* No messages at all? No further action. */
108 for (c=0; c<num_msgs; c++)
110 if (msglist[c] > CitControl.MMhighest)
112 CitControl.MMhighest = msglist[c];
119 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed room counter\n");
121 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed message count\n");
127 * Callback to get highest user number.
130 void control_find_user (struct ctdluser *EachUser, void *out_data)
134 if (EachUser->usernum > CitControl.MMnextuser)
136 CitControl.MMnextuser = EachUser->usernum;
140 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed user count\n");
145 * get_control - read the control record into memory.
147 void get_control(void)
149 static int already_have_control = 0;
152 * If we already have the control record in memory, there's no point
153 * in reading it from disk again.
155 if (already_have_control) return;
157 /* Zero it out. If the control record on disk is missing or short,
158 * the system functions with all control record fields initialized
161 memset(&CitControl, 0, sizeof(struct CitControl));
162 if (control_fp == NULL) {
163 control_fp = fopen(file_citadel_control, "rb+");
164 if (control_fp != NULL) {
166 fchown(fileno(control_fp), config.c_ctdluid, -1);
167 fchmod(fileno(control_fp),
171 if (control_fp == NULL) {
172 control_fp = fopen(file_citadel_control, "wb+");
173 if (control_fp != NULL) {
175 fchown(fileno(control_fp), config.c_ctdluid, -1);
176 fchmod(fileno(control_fp),
178 memset(&CitControl, 0, sizeof(struct CitControl));
179 fwrite(&CitControl, sizeof(struct CitControl),
184 if (control_fp == NULL) {
185 CtdlLogPrintf(CTDL_ALERT, "ERROR opening %s: %s\n",
186 file_citadel_control,
192 fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
193 already_have_control = 1;
194 chown(file_citadel_control, config.c_ctdluid, (-1));
199 * put_control - write the control record to disk.
201 void put_control(void)
204 if (control_fp != NULL) {
206 fwrite(&CitControl, sizeof(struct CitControl), 1,
214 * check_control - check the control record has sensible values for message, user and room numbers
216 void check_control(void)
218 CtdlLogPrintf(CTDL_INFO, "Checking/re-building control record\n");
220 // Find highest room number and message number.
221 ForEachRoom(control_find_highest, NULL);
222 ForEachUser(control_find_user, NULL);
228 * release_control - close our fd on exit
230 void release_control(void)
232 if (control_fp != NULL)
238 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
240 long get_new_message_number(void)
243 begin_critical_section(S_CONTROL);
245 retval = ++CitControl.MMhighest;
247 end_critical_section(S_CONTROL);
253 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
254 * This provides a quick way to initialise a variable that might be used to indicate
255 * messages that should not be processed. EG. a new Sieve script will use this
256 * to record determine that messages older than this should not be processed.
258 long CtdlGetCurrentMessageNumber(void)
261 begin_critical_section(S_CONTROL);
263 retval = CitControl.MMhighest;
264 end_critical_section(S_CONTROL);
270 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
272 long get_new_user_number(void)
275 begin_critical_section(S_CONTROL);
277 retval = ++CitControl.MMnextuser;
279 end_critical_section(S_CONTROL);
286 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
288 long get_new_room_number(void)
291 begin_critical_section(S_CONTROL);
293 retval = ++CitControl.MMnextroom;
295 end_critical_section(S_CONTROL);
302 * Get or set global configuration options
304 void cmd_conf(char *argbuf)
312 if (CtdlAccessCheck(ac_aide)) return;
314 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
315 if (!strcasecmp(cmd, "GET")) {
316 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
317 cprintf("%s\n", config.c_nodename);
318 cprintf("%s\n", config.c_fqdn);
319 cprintf("%s\n", config.c_humannode);
320 cprintf("%s\n", config.c_phonenum);
321 cprintf("%d\n", config.c_creataide);
322 cprintf("%d\n", config.c_sleeping);
323 cprintf("%d\n", config.c_initax);
324 cprintf("%d\n", config.c_regiscall);
325 cprintf("%d\n", config.c_twitdetect);
326 cprintf("%s\n", config.c_twitroom);
327 cprintf("%s\n", config.c_moreprompt);
328 cprintf("%d\n", config.c_restrict);
329 cprintf("%s\n", config.c_site_location);
330 cprintf("%s\n", config.c_sysadm);
331 cprintf("%d\n", config.c_maxsessions);
332 cprintf("xxx\n"); /* placeholder -- field no longer in use */
333 cprintf("%d\n", config.c_userpurge);
334 cprintf("%d\n", config.c_roompurge);
335 cprintf("%s\n", config.c_logpages);
336 cprintf("%d\n", config.c_createax);
337 cprintf("%ld\n", config.c_maxmsglen);
338 cprintf("%d\n", config.c_min_workers);
339 cprintf("%d\n", config.c_max_workers);
340 cprintf("%d\n", config.c_pop3_port);
341 cprintf("%d\n", config.c_smtp_port);
342 cprintf("%d\n", config.c_rfc822_strict_from);
343 cprintf("%d\n", config.c_aide_zap);
344 cprintf("%d\n", config.c_imap_port);
345 cprintf("%ld\n", config.c_net_freq);
346 cprintf("%d\n", config.c_disable_newu);
347 cprintf("1\n"); /* niu */
348 cprintf("%d\n", config.c_purge_hour);
350 cprintf("%s\n", config.c_ldap_host);
351 cprintf("%d\n", config.c_ldap_port);
352 cprintf("%s\n", config.c_ldap_base_dn);
353 cprintf("%s\n", config.c_ldap_bind_dn);
354 cprintf("%s\n", config.c_ldap_bind_pw);
362 cprintf("%s\n", config.c_ip_addr);
363 cprintf("%d\n", config.c_msa_port);
364 cprintf("%d\n", config.c_imaps_port);
365 cprintf("%d\n", config.c_pop3s_port);
366 cprintf("%d\n", config.c_smtps_port);
367 cprintf("%d\n", config.c_enable_fulltext);
368 cprintf("%d\n", config.c_auto_cull);
369 cprintf("%d\n", config.c_instant_expunge);
370 cprintf("%d\n", config.c_allow_spoofing);
371 cprintf("%d\n", config.c_journal_email);
372 cprintf("%d\n", config.c_journal_pubmsgs);
373 cprintf("%s\n", config.c_journal_dest);
374 cprintf("%s\n", config.c_default_cal_zone);
375 cprintf("%d\n", config.c_pftcpdict_port);
376 cprintf("%d\n", config.c_managesieve_port);
377 cprintf("%d\n", config.c_auth_mode);
378 cprintf("%s\n", config.c_funambol_host);
379 cprintf("%d\n", config.c_funambol_port);
380 cprintf("%s\n", config.c_funambol_source);
381 cprintf("%s\n", config.c_funambol_auth);
382 cprintf("%d\n", config.c_rbl_at_greeting);
383 cprintf("%s\n", config.c_master_user);
384 cprintf("%s\n", config.c_master_pass);
385 cprintf("%s\n", config.c_pager_program);
386 cprintf("%d\n", config.c_imap_keep_from);
387 cprintf("%d\n", config.c_xmpp_c2s_port);
388 cprintf("%d\n", config.c_xmpp_s2s_port);
389 cprintf("%ld\n", config.c_pop3_fetch);
390 cprintf("%ld\n", config.c_pop3_fastest);
391 cprintf("%d\n", config.c_spam_flag_only);
395 else if (!strcasecmp(cmd, "SET")) {
397 cprintf("%d Send configuration...\n", SEND_LISTING);
399 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
402 safestrncpy(config.c_nodename, buf,
403 sizeof config.c_nodename);
406 safestrncpy(config.c_fqdn, buf,
407 sizeof config.c_fqdn);
410 safestrncpy(config.c_humannode, buf,
411 sizeof config.c_humannode);
414 safestrncpy(config.c_phonenum, buf,
415 sizeof config.c_phonenum);
418 config.c_creataide = atoi(buf);
421 config.c_sleeping = atoi(buf);
424 config.c_initax = atoi(buf);
425 if (config.c_initax < 1)
427 if (config.c_initax > 6)
431 config.c_regiscall = atoi(buf);
432 if (config.c_regiscall != 0)
433 config.c_regiscall = 1;
436 config.c_twitdetect = atoi(buf);
437 if (config.c_twitdetect != 0)
438 config.c_twitdetect = 1;
441 safestrncpy(config.c_twitroom, buf,
442 sizeof config.c_twitroom);
445 safestrncpy(config.c_moreprompt, buf,
446 sizeof config.c_moreprompt);
449 config.c_restrict = atoi(buf);
450 if (config.c_restrict != 0)
451 config.c_restrict = 1;
454 safestrncpy(config.c_site_location, buf,
455 sizeof config.c_site_location);
458 safestrncpy(config.c_sysadm, buf,
459 sizeof config.c_sysadm);
462 config.c_maxsessions = atoi(buf);
463 if (config.c_maxsessions < 0)
464 config.c_maxsessions = 0;
467 /* placeholder -- field no longer in use */
470 config.c_userpurge = atoi(buf);
473 config.c_roompurge = atoi(buf);
476 safestrncpy(config.c_logpages, buf,
477 sizeof config.c_logpages);
480 config.c_createax = atoi(buf);
481 if (config.c_createax < 1)
482 config.c_createax = 1;
483 if (config.c_createax > 6)
484 config.c_createax = 6;
487 if (atoi(buf) >= 8192)
488 config.c_maxmsglen = atoi(buf);
492 config.c_min_workers = atoi(buf);
494 if (atoi(buf) >= config.c_min_workers)
495 config.c_max_workers = atoi(buf);
497 config.c_pop3_port = atoi(buf);
500 config.c_smtp_port = atoi(buf);
503 config.c_rfc822_strict_from = atoi(buf);
506 config.c_aide_zap = atoi(buf);
507 if (config.c_aide_zap != 0)
508 config.c_aide_zap = 1;
511 config.c_imap_port = atoi(buf);
514 config.c_net_freq = atol(buf);
517 config.c_disable_newu = atoi(buf);
518 if (config.c_disable_newu != 0)
519 config.c_disable_newu = 1;
525 if ((config.c_purge_hour >= 0)
526 && (config.c_purge_hour <= 23)) {
527 config.c_purge_hour = atoi(buf);
532 safestrncpy(config.c_ldap_host, buf,
533 sizeof config.c_ldap_host);
536 config.c_ldap_port = atoi(buf);
539 safestrncpy(config.c_ldap_base_dn, buf,
540 sizeof config.c_ldap_base_dn);
543 safestrncpy(config.c_ldap_bind_dn, buf,
544 sizeof config.c_ldap_bind_dn);
547 safestrncpy(config.c_ldap_bind_pw, buf,
548 sizeof config.c_ldap_bind_pw);
552 safestrncpy(config.c_ip_addr, buf,
553 sizeof config.c_ip_addr);
555 config.c_msa_port = atoi(buf);
558 config.c_imaps_port = atoi(buf);
561 config.c_pop3s_port = atoi(buf);
564 config.c_smtps_port = atoi(buf);
567 config.c_enable_fulltext = atoi(buf);
570 config.c_auto_cull = atoi(buf);
573 config.c_instant_expunge = atoi(buf);
576 config.c_allow_spoofing = atoi(buf);
579 config.c_journal_email = atoi(buf);
582 config.c_journal_pubmsgs = atoi(buf);
585 safestrncpy(config.c_journal_dest, buf,
586 sizeof config.c_journal_dest);
588 safestrncpy(config.c_default_cal_zone, buf,
589 sizeof config.c_default_cal_zone);
592 config.c_pftcpdict_port = atoi(buf);
595 config.c_managesieve_port = atoi(buf);
598 config.c_auth_mode = atoi(buf);
600 safestrncpy(config.c_funambol_host, buf,
601 sizeof config.c_funambol_host);
604 config.c_funambol_port = atoi(buf);
607 safestrncpy(config.c_funambol_source,
609 sizeof config.c_funambol_source);
612 safestrncpy(config.c_funambol_auth,
614 sizeof config.c_funambol_auth);
617 config.c_rbl_at_greeting = atoi(buf);
620 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
623 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
626 safestrncpy(config.c_pager_program,
628 sizeof config.c_pager_program);
631 config.c_imap_keep_from = atoi(buf);
634 config.c_xmpp_c2s_port = atoi(buf);
637 config.c_xmpp_s2s_port = atoi(buf);
640 config.c_pop3_fetch = atol(buf);
643 config.c_pop3_fastest = atol(buf);
646 config.c_spam_flag_only = atoi(buf);
652 snprintf(buf, sizeof buf,
653 "The global system configuration has been edited by %s.\n",
655 aide_message(buf,"Citadel Configuration Manager Message");
657 if (!IsEmptyStr(config.c_logpages))
658 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
660 /* If full text indexing has been disabled, invalidate the
661 * index so it doesn't try to use it later.
663 if (config.c_enable_fulltext == 0) {
664 CitControl.fulltext_wordbreaker = 0;
669 else if (!strcasecmp(cmd, "GETSYS")) {
670 extract_token(confname, argbuf, 1, '|', sizeof confname);
671 confptr = CtdlGetSysConfig(confname);
672 if (confptr != NULL) {
673 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
674 client_write(confptr, strlen(confptr));
675 if (confptr[strlen(confptr) - 1] != 10)
676 client_write("\n", 1);
680 cprintf("%d No such configuration.\n",
681 ERROR + ILLEGAL_VALUE);
685 else if (!strcasecmp(cmd, "PUTSYS")) {
686 extract_token(confname, argbuf, 1, '|', sizeof confname);
688 cprintf("%d %s\n", SEND_LISTING, confname);
689 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
690 CtdlPutSysConfig(confname, confptr);
695 cprintf("%d Illegal option(s) specified.\n",
696 ERROR + ILLEGAL_VALUE);