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 #include "ctdl_module.h"
52 struct CitControl CitControl;
53 extern struct config config;
54 FILE *control_fp = NULL;
55 long control_highest_user = 0;
59 * lock_control - acquire a lock on the control record file.
60 * This keeps multiple citservers from running concurrently.
62 void lock_control(void)
66 * TODO: solaris manpages describe this function, but the headers
70 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
71 CtdlLogPrintf(CTDL_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
72 CtdlLogPrintf(CTDL_EMERG, "Is another citserver already running?\n");
73 exit(CTDLEXIT_CONTROL);
79 * callback to get highest room number when rebuilding control file
81 void control_find_highest(struct ctdlroom *qrbuf, void *data)
84 struct cdbdata *cdbfr;
89 int message_fixed = 0;
91 if (qrbuf->QRnumber > CitControl.MMnextroom)
93 CitControl.MMnextroom = qrbuf->QRnumber;
97 getroom (&room, qrbuf->QRname);
99 /* Load the message list */
100 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
102 msglist = (long *) cdbfr->ptr;
103 num_msgs = cdbfr->len / sizeof(long);
105 return; /* No messages at all? No further action. */
110 for (c=0; c<num_msgs; c++)
112 if (msglist[c] > CitControl.MMhighest)
114 CitControl.MMhighest = msglist[c];
121 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed room counter\n");
123 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed message count\n");
129 * Callback to get highest user number.
132 void control_find_user (struct ctdluser *EachUser, void *out_data)
136 if (EachUser->usernum > CitControl.MMnextuser)
138 CitControl.MMnextuser = EachUser->usernum;
142 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed user count\n");
147 * get_control - read the control record into memory.
149 void get_control(void)
151 static int already_have_control = 0;
154 * If we already have the control record in memory, there's no point
155 * in reading it from disk again.
157 if (already_have_control) return;
159 /* Zero it out. If the control record on disk is missing or short,
160 * the system functions with all control record fields initialized
163 memset(&CitControl, 0, sizeof(struct CitControl));
164 if (control_fp == NULL) {
165 control_fp = fopen(file_citadel_control, "rb+");
166 if (control_fp != NULL) {
168 fchown(fileno(control_fp), config.c_ctdluid, -1);
169 fchmod(fileno(control_fp),
173 if (control_fp == NULL) {
174 control_fp = fopen(file_citadel_control, "wb+");
175 if (control_fp != NULL) {
177 fchown(fileno(control_fp), config.c_ctdluid, -1);
178 fchmod(fileno(control_fp),
180 memset(&CitControl, 0, sizeof(struct CitControl));
181 fwrite(&CitControl, sizeof(struct CitControl),
186 if (control_fp == NULL) {
187 CtdlLogPrintf(CTDL_ALERT, "ERROR opening %s: %s\n",
188 file_citadel_control,
194 fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
195 already_have_control = 1;
196 chown(file_citadel_control, config.c_ctdluid, (-1));
201 * put_control - write the control record to disk.
203 void put_control(void)
206 if (control_fp != NULL) {
208 fwrite(&CitControl, sizeof(struct CitControl), 1,
216 * check_control - check the control record has sensible values for message, user and room numbers
218 void check_control(void)
220 CtdlLogPrintf(CTDL_INFO, "Checking/re-building control record\n");
222 // Find highest room number and message number.
223 ForEachRoom(control_find_highest, NULL);
224 ForEachUser(control_find_user, NULL);
230 * release_control - close our fd on exit
232 void release_control(void)
234 if (control_fp != NULL)
240 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
242 long get_new_message_number(void)
245 begin_critical_section(S_CONTROL);
247 retval = ++CitControl.MMhighest;
249 end_critical_section(S_CONTROL);
255 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
256 * This provides a quick way to initialise a variable that might be used to indicate
257 * messages that should not be processed. EG. a new Sieve script will use this
258 * to record determine that messages older than this should not be processed.
260 long CtdlGetCurrentMessageNumber(void)
263 begin_critical_section(S_CONTROL);
265 retval = CitControl.MMhighest;
266 end_critical_section(S_CONTROL);
272 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
274 long get_new_user_number(void)
277 begin_critical_section(S_CONTROL);
279 retval = ++CitControl.MMnextuser;
281 end_critical_section(S_CONTROL);
288 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
290 long get_new_room_number(void)
293 begin_critical_section(S_CONTROL);
295 retval = ++CitControl.MMnextroom;
297 end_critical_section(S_CONTROL);
304 * Get or set global configuration options
306 void cmd_conf(char *argbuf)
314 if (CtdlAccessCheck(ac_aide)) return;
316 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
317 if (!strcasecmp(cmd, "GET")) {
318 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
319 cprintf("%s\n", config.c_nodename);
320 cprintf("%s\n", config.c_fqdn);
321 cprintf("%s\n", config.c_humannode);
322 cprintf("%s\n", config.c_phonenum);
323 cprintf("%d\n", config.c_creataide);
324 cprintf("%d\n", config.c_sleeping);
325 cprintf("%d\n", config.c_initax);
326 cprintf("%d\n", config.c_regiscall);
327 cprintf("%d\n", config.c_twitdetect);
328 cprintf("%s\n", config.c_twitroom);
329 cprintf("%s\n", config.c_moreprompt);
330 cprintf("%d\n", config.c_restrict);
331 cprintf("%s\n", config.c_site_location);
332 cprintf("%s\n", config.c_sysadm);
333 cprintf("%d\n", config.c_maxsessions);
334 cprintf("xxx\n"); /* placeholder -- field no longer in use */
335 cprintf("%d\n", config.c_userpurge);
336 cprintf("%d\n", config.c_roompurge);
337 cprintf("%s\n", config.c_logpages);
338 cprintf("%d\n", config.c_createax);
339 cprintf("%ld\n", config.c_maxmsglen);
340 cprintf("%d\n", config.c_min_workers);
341 cprintf("%d\n", config.c_max_workers);
342 cprintf("%d\n", config.c_pop3_port);
343 cprintf("%d\n", config.c_smtp_port);
344 cprintf("%d\n", config.c_rfc822_strict_from);
345 cprintf("%d\n", config.c_aide_zap);
346 cprintf("%d\n", config.c_imap_port);
347 cprintf("%ld\n", config.c_net_freq);
348 cprintf("%d\n", config.c_disable_newu);
349 cprintf("1\n"); /* niu */
350 cprintf("%d\n", config.c_purge_hour);
352 cprintf("%s\n", config.c_ldap_host);
353 cprintf("%d\n", config.c_ldap_port);
354 cprintf("%s\n", config.c_ldap_base_dn);
355 cprintf("%s\n", config.c_ldap_bind_dn);
356 cprintf("%s\n", config.c_ldap_bind_pw);
364 cprintf("%s\n", config.c_ip_addr);
365 cprintf("%d\n", config.c_msa_port);
366 cprintf("%d\n", config.c_imaps_port);
367 cprintf("%d\n", config.c_pop3s_port);
368 cprintf("%d\n", config.c_smtps_port);
369 cprintf("%d\n", config.c_enable_fulltext);
370 cprintf("%d\n", config.c_auto_cull);
371 cprintf("%d\n", config.c_instant_expunge);
372 cprintf("%d\n", config.c_allow_spoofing);
373 cprintf("%d\n", config.c_journal_email);
374 cprintf("%d\n", config.c_journal_pubmsgs);
375 cprintf("%s\n", config.c_journal_dest);
376 cprintf("%s\n", config.c_default_cal_zone);
377 cprintf("%d\n", config.c_pftcpdict_port);
378 cprintf("%d\n", config.c_managesieve_port);
379 cprintf("%d\n", config.c_auth_mode);
380 cprintf("%s\n", config.c_funambol_host);
381 cprintf("%d\n", config.c_funambol_port);
382 cprintf("%s\n", config.c_funambol_source);
383 cprintf("%s\n", config.c_funambol_auth);
384 cprintf("%d\n", config.c_rbl_at_greeting);
385 cprintf("%s\n", config.c_master_user);
386 cprintf("%s\n", config.c_master_pass);
387 cprintf("%s\n", config.c_pager_program);
388 cprintf("%d\n", config.c_imap_keep_from);
389 cprintf("%d\n", config.c_xmpp_c2s_port);
390 cprintf("%d\n", config.c_xmpp_s2s_port);
391 cprintf("%ld\n", config.c_pop3_fetch);
392 cprintf("%ld\n", config.c_pop3_fastest);
393 cprintf("%d\n", config.c_spam_flag_only);
397 else if (!strcasecmp(cmd, "SET")) {
399 cprintf("%d Send configuration...\n", SEND_LISTING);
401 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
404 safestrncpy(config.c_nodename, buf,
405 sizeof config.c_nodename);
408 safestrncpy(config.c_fqdn, buf,
409 sizeof config.c_fqdn);
412 safestrncpy(config.c_humannode, buf,
413 sizeof config.c_humannode);
416 safestrncpy(config.c_phonenum, buf,
417 sizeof config.c_phonenum);
420 config.c_creataide = atoi(buf);
423 config.c_sleeping = atoi(buf);
426 config.c_initax = atoi(buf);
427 if (config.c_initax < 1)
429 if (config.c_initax > 6)
433 config.c_regiscall = atoi(buf);
434 if (config.c_regiscall != 0)
435 config.c_regiscall = 1;
438 config.c_twitdetect = atoi(buf);
439 if (config.c_twitdetect != 0)
440 config.c_twitdetect = 1;
443 safestrncpy(config.c_twitroom, buf,
444 sizeof config.c_twitroom);
447 safestrncpy(config.c_moreprompt, buf,
448 sizeof config.c_moreprompt);
451 config.c_restrict = atoi(buf);
452 if (config.c_restrict != 0)
453 config.c_restrict = 1;
456 safestrncpy(config.c_site_location, buf,
457 sizeof config.c_site_location);
460 safestrncpy(config.c_sysadm, buf,
461 sizeof config.c_sysadm);
464 config.c_maxsessions = atoi(buf);
465 if (config.c_maxsessions < 0)
466 config.c_maxsessions = 0;
469 /* placeholder -- field no longer in use */
472 config.c_userpurge = atoi(buf);
475 config.c_roompurge = atoi(buf);
478 safestrncpy(config.c_logpages, buf,
479 sizeof config.c_logpages);
482 config.c_createax = atoi(buf);
483 if (config.c_createax < 1)
484 config.c_createax = 1;
485 if (config.c_createax > 6)
486 config.c_createax = 6;
489 if (atoi(buf) >= 8192)
490 config.c_maxmsglen = atoi(buf);
494 config.c_min_workers = atoi(buf);
496 if (atoi(buf) >= config.c_min_workers)
497 config.c_max_workers = atoi(buf);
499 config.c_pop3_port = atoi(buf);
502 config.c_smtp_port = atoi(buf);
505 config.c_rfc822_strict_from = atoi(buf);
508 config.c_aide_zap = atoi(buf);
509 if (config.c_aide_zap != 0)
510 config.c_aide_zap = 1;
513 config.c_imap_port = atoi(buf);
516 config.c_net_freq = atol(buf);
519 config.c_disable_newu = atoi(buf);
520 if (config.c_disable_newu != 0)
521 config.c_disable_newu = 1;
527 if ((config.c_purge_hour >= 0)
528 && (config.c_purge_hour <= 23)) {
529 config.c_purge_hour = atoi(buf);
534 safestrncpy(config.c_ldap_host, buf,
535 sizeof config.c_ldap_host);
538 config.c_ldap_port = atoi(buf);
541 safestrncpy(config.c_ldap_base_dn, buf,
542 sizeof config.c_ldap_base_dn);
545 safestrncpy(config.c_ldap_bind_dn, buf,
546 sizeof config.c_ldap_bind_dn);
549 safestrncpy(config.c_ldap_bind_pw, buf,
550 sizeof config.c_ldap_bind_pw);
554 safestrncpy(config.c_ip_addr, buf,
555 sizeof config.c_ip_addr);
557 config.c_msa_port = atoi(buf);
560 config.c_imaps_port = atoi(buf);
563 config.c_pop3s_port = atoi(buf);
566 config.c_smtps_port = atoi(buf);
569 config.c_enable_fulltext = atoi(buf);
572 config.c_auto_cull = atoi(buf);
575 config.c_instant_expunge = atoi(buf);
578 config.c_allow_spoofing = atoi(buf);
581 config.c_journal_email = atoi(buf);
584 config.c_journal_pubmsgs = atoi(buf);
587 safestrncpy(config.c_journal_dest, buf,
588 sizeof config.c_journal_dest);
590 safestrncpy(config.c_default_cal_zone, buf,
591 sizeof config.c_default_cal_zone);
594 config.c_pftcpdict_port = atoi(buf);
597 config.c_managesieve_port = atoi(buf);
600 config.c_auth_mode = atoi(buf);
602 safestrncpy(config.c_funambol_host, buf,
603 sizeof config.c_funambol_host);
606 config.c_funambol_port = atoi(buf);
609 safestrncpy(config.c_funambol_source,
611 sizeof config.c_funambol_source);
614 safestrncpy(config.c_funambol_auth,
616 sizeof config.c_funambol_auth);
619 config.c_rbl_at_greeting = atoi(buf);
622 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
625 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
628 safestrncpy(config.c_pager_program,
630 sizeof config.c_pager_program);
633 config.c_imap_keep_from = atoi(buf);
636 config.c_xmpp_c2s_port = atoi(buf);
639 config.c_xmpp_s2s_port = atoi(buf);
642 config.c_pop3_fetch = atol(buf);
645 config.c_pop3_fastest = atol(buf);
648 config.c_spam_flag_only = atoi(buf);
654 snprintf(buf, sizeof buf,
655 "The global system configuration has been edited by %s.\n",
657 aide_message(buf,"Citadel Configuration Manager Message");
659 if (!IsEmptyStr(config.c_logpages))
660 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
662 /* If full text indexing has been disabled, invalidate the
663 * index so it doesn't try to use it later.
665 if (config.c_enable_fulltext == 0) {
666 CitControl.fulltext_wordbreaker = 0;
671 else if (!strcasecmp(cmd, "GETSYS")) {
672 extract_token(confname, argbuf, 1, '|', sizeof confname);
673 confptr = CtdlGetSysConfig(confname);
674 if (confptr != NULL) {
675 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
676 client_write(confptr, strlen(confptr));
677 if (confptr[strlen(confptr) - 1] != 10)
678 client_write("\n", 1);
682 cprintf("%d No such configuration.\n",
683 ERROR + ILLEGAL_VALUE);
687 else if (!strcasecmp(cmd, "PUTSYS")) {
688 extract_token(confname, argbuf, 1, '|', sizeof confname);
690 cprintf("%d %s\n", SEND_LISTING, confname);
691 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
692 CtdlPutSysConfig(confname, confptr);
697 cprintf("%d Illegal option(s) specified.\n",
698 ERROR + ILLEGAL_VALUE);
703 /*****************************************************************************/
704 /* MODULE INITIALIZATION STUFF */
705 /*****************************************************************************/
708 CTDL_MODULE_INIT(control)
710 CtdlRegisterProtoHook(cmd_conf, "CONF", "Autoconverted. TODO: document me.");
711 /* return our Subversion id for the Log */