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;
156 * If we already have the control record in memory, there's no point
157 * in reading it from disk again.
159 if (already_have_control) return;
161 /* Zero it out. If the control record on disk is missing or short,
162 * the system functions with all control record fields initialized
165 memset(&CitControl, 0, sizeof(struct CitControl));
166 if (control_fp == NULL) {
167 control_fp = fopen(file_citadel_control, "rb+");
168 if (control_fp != NULL) {
170 rv = fchown(fileno(control_fp), config.c_ctdluid, -1);
171 rv = fchmod(fileno(control_fp), S_IRUSR|S_IWUSR);
174 if (control_fp == NULL) {
175 control_fp = fopen(file_citadel_control, "wb+");
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);
180 memset(&CitControl, 0, sizeof(struct CitControl));
181 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
185 if (control_fp == NULL) {
186 CtdlLogPrintf(CTDL_ALERT, "ERROR opening %s: %s\n", file_citadel_control, strerror(errno));
191 rv = fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
192 already_have_control = 1;
193 rv = chown(file_citadel_control, config.c_ctdluid, (-1));
198 * put_control - write the control record to disk.
200 void put_control(void)
204 if (control_fp != NULL) {
206 rv = fwrite(&CitControl, sizeof(struct CitControl), 1, control_fp);
213 * check_control - check the control record has sensible values for message, user and room numbers
215 void check_control(void)
217 CtdlLogPrintf(CTDL_INFO, "Checking/re-building control record\n");
219 // Find highest room number and message number.
220 ForEachRoom(control_find_highest, NULL);
221 ForEachUser(control_find_user, NULL);
227 * release_control - close our fd on exit
229 void release_control(void)
231 if (control_fp != NULL)
237 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
239 long get_new_message_number(void)
242 begin_critical_section(S_CONTROL);
244 retval = ++CitControl.MMhighest;
246 end_critical_section(S_CONTROL);
252 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
253 * This provides a quick way to initialise a variable that might be used to indicate
254 * messages that should not be processed. EG. a new Sieve script will use this
255 * to record determine that messages older than this should not be processed.
257 long CtdlGetCurrentMessageNumber(void)
260 begin_critical_section(S_CONTROL);
262 retval = CitControl.MMhighest;
263 end_critical_section(S_CONTROL);
269 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
271 long get_new_user_number(void)
274 begin_critical_section(S_CONTROL);
276 retval = ++CitControl.MMnextuser;
278 end_critical_section(S_CONTROL);
285 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
287 long get_new_room_number(void)
290 begin_critical_section(S_CONTROL);
292 retval = ++CitControl.MMnextroom;
294 end_critical_section(S_CONTROL);
301 * Get or set global configuration options
303 void cmd_conf(char *argbuf)
311 if (CtdlAccessCheck(ac_aide)) return;
313 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
314 if (!strcasecmp(cmd, "GET")) {
315 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
316 cprintf("%s\n", config.c_nodename);
317 cprintf("%s\n", config.c_fqdn);
318 cprintf("%s\n", config.c_humannode);
319 cprintf("%s\n", config.c_phonenum);
320 cprintf("%d\n", config.c_creataide);
321 cprintf("%d\n", config.c_sleeping);
322 cprintf("%d\n", config.c_initax);
323 cprintf("%d\n", config.c_regiscall);
324 cprintf("%d\n", config.c_twitdetect);
325 cprintf("%s\n", config.c_twitroom);
326 cprintf("%s\n", config.c_moreprompt);
327 cprintf("%d\n", config.c_restrict);
328 cprintf("%s\n", config.c_site_location);
329 cprintf("%s\n", config.c_sysadm);
330 cprintf("%d\n", config.c_maxsessions);
331 cprintf("xxx\n"); /* placeholder -- field no longer in use */
332 cprintf("%d\n", config.c_userpurge);
333 cprintf("%d\n", config.c_roompurge);
334 cprintf("%s\n", config.c_logpages);
335 cprintf("%d\n", config.c_createax);
336 cprintf("%ld\n", config.c_maxmsglen);
337 cprintf("%d\n", config.c_min_workers);
338 cprintf("%d\n", config.c_max_workers);
339 cprintf("%d\n", config.c_pop3_port);
340 cprintf("%d\n", config.c_smtp_port);
341 cprintf("%d\n", config.c_rfc822_strict_from);
342 cprintf("%d\n", config.c_aide_zap);
343 cprintf("%d\n", config.c_imap_port);
344 cprintf("%ld\n", config.c_net_freq);
345 cprintf("%d\n", config.c_disable_newu);
346 cprintf("1\n"); /* niu */
347 cprintf("%d\n", config.c_purge_hour);
349 cprintf("%s\n", config.c_ldap_host);
350 cprintf("%d\n", config.c_ldap_port);
351 cprintf("%s\n", config.c_ldap_base_dn);
352 cprintf("%s\n", config.c_ldap_bind_dn);
353 cprintf("%s\n", config.c_ldap_bind_pw);
361 cprintf("%s\n", config.c_ip_addr);
362 cprintf("%d\n", config.c_msa_port);
363 cprintf("%d\n", config.c_imaps_port);
364 cprintf("%d\n", config.c_pop3s_port);
365 cprintf("%d\n", config.c_smtps_port);
366 cprintf("%d\n", config.c_enable_fulltext);
367 cprintf("%d\n", config.c_auto_cull);
368 cprintf("%d\n", config.c_instant_expunge);
369 cprintf("%d\n", config.c_allow_spoofing);
370 cprintf("%d\n", config.c_journal_email);
371 cprintf("%d\n", config.c_journal_pubmsgs);
372 cprintf("%s\n", config.c_journal_dest);
373 cprintf("%s\n", config.c_default_cal_zone);
374 cprintf("%d\n", config.c_pftcpdict_port);
375 cprintf("%d\n", config.c_managesieve_port);
376 cprintf("%d\n", config.c_auth_mode);
377 cprintf("%s\n", config.c_funambol_host);
378 cprintf("%d\n", config.c_funambol_port);
379 cprintf("%s\n", config.c_funambol_source);
380 cprintf("%s\n", config.c_funambol_auth);
381 cprintf("%d\n", config.c_rbl_at_greeting);
382 cprintf("%s\n", config.c_master_user);
383 cprintf("%s\n", config.c_master_pass);
384 cprintf("%s\n", config.c_pager_program);
385 cprintf("%d\n", config.c_imap_keep_from);
386 cprintf("%d\n", config.c_xmpp_c2s_port);
387 cprintf("%d\n", config.c_xmpp_s2s_port);
388 cprintf("%ld\n", config.c_pop3_fetch);
389 cprintf("%ld\n", config.c_pop3_fastest);
390 cprintf("%d\n", config.c_spam_flag_only);
394 else if (!strcasecmp(cmd, "SET")) {
396 cprintf("%d Send configuration...\n", SEND_LISTING);
398 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
401 safestrncpy(config.c_nodename, buf,
402 sizeof config.c_nodename);
405 safestrncpy(config.c_fqdn, buf,
406 sizeof config.c_fqdn);
409 safestrncpy(config.c_humannode, buf,
410 sizeof config.c_humannode);
413 safestrncpy(config.c_phonenum, buf,
414 sizeof config.c_phonenum);
417 config.c_creataide = atoi(buf);
420 config.c_sleeping = atoi(buf);
423 config.c_initax = atoi(buf);
424 if (config.c_initax < 1)
426 if (config.c_initax > 6)
430 config.c_regiscall = atoi(buf);
431 if (config.c_regiscall != 0)
432 config.c_regiscall = 1;
435 config.c_twitdetect = atoi(buf);
436 if (config.c_twitdetect != 0)
437 config.c_twitdetect = 1;
440 safestrncpy(config.c_twitroom, buf,
441 sizeof config.c_twitroom);
444 safestrncpy(config.c_moreprompt, buf,
445 sizeof config.c_moreprompt);
448 config.c_restrict = atoi(buf);
449 if (config.c_restrict != 0)
450 config.c_restrict = 1;
453 safestrncpy(config.c_site_location, buf,
454 sizeof config.c_site_location);
457 safestrncpy(config.c_sysadm, buf,
458 sizeof config.c_sysadm);
461 config.c_maxsessions = atoi(buf);
462 if (config.c_maxsessions < 0)
463 config.c_maxsessions = 0;
466 /* placeholder -- field no longer in use */
469 config.c_userpurge = atoi(buf);
472 config.c_roompurge = atoi(buf);
475 safestrncpy(config.c_logpages, buf,
476 sizeof config.c_logpages);
479 config.c_createax = atoi(buf);
480 if (config.c_createax < 1)
481 config.c_createax = 1;
482 if (config.c_createax > 6)
483 config.c_createax = 6;
486 if (atoi(buf) >= 8192)
487 config.c_maxmsglen = atoi(buf);
491 config.c_min_workers = atoi(buf);
493 if (atoi(buf) >= config.c_min_workers)
494 config.c_max_workers = atoi(buf);
496 config.c_pop3_port = atoi(buf);
499 config.c_smtp_port = atoi(buf);
502 config.c_rfc822_strict_from = atoi(buf);
505 config.c_aide_zap = atoi(buf);
506 if (config.c_aide_zap != 0)
507 config.c_aide_zap = 1;
510 config.c_imap_port = atoi(buf);
513 config.c_net_freq = atol(buf);
516 config.c_disable_newu = atoi(buf);
517 if (config.c_disable_newu != 0)
518 config.c_disable_newu = 1;
524 if ((config.c_purge_hour >= 0)
525 && (config.c_purge_hour <= 23)) {
526 config.c_purge_hour = atoi(buf);
531 safestrncpy(config.c_ldap_host, buf,
532 sizeof config.c_ldap_host);
535 config.c_ldap_port = atoi(buf);
538 safestrncpy(config.c_ldap_base_dn, buf,
539 sizeof config.c_ldap_base_dn);
542 safestrncpy(config.c_ldap_bind_dn, buf,
543 sizeof config.c_ldap_bind_dn);
546 safestrncpy(config.c_ldap_bind_pw, buf,
547 sizeof config.c_ldap_bind_pw);
551 safestrncpy(config.c_ip_addr, buf,
552 sizeof config.c_ip_addr);
554 config.c_msa_port = atoi(buf);
557 config.c_imaps_port = atoi(buf);
560 config.c_pop3s_port = atoi(buf);
563 config.c_smtps_port = atoi(buf);
566 config.c_enable_fulltext = atoi(buf);
569 config.c_auto_cull = atoi(buf);
572 config.c_instant_expunge = atoi(buf);
575 config.c_allow_spoofing = atoi(buf);
578 config.c_journal_email = atoi(buf);
581 config.c_journal_pubmsgs = atoi(buf);
584 safestrncpy(config.c_journal_dest, buf,
585 sizeof config.c_journal_dest);
587 safestrncpy(config.c_default_cal_zone, buf,
588 sizeof config.c_default_cal_zone);
591 config.c_pftcpdict_port = atoi(buf);
594 config.c_managesieve_port = atoi(buf);
597 config.c_auth_mode = atoi(buf);
599 safestrncpy(config.c_funambol_host, buf,
600 sizeof config.c_funambol_host);
603 config.c_funambol_port = atoi(buf);
606 safestrncpy(config.c_funambol_source,
608 sizeof config.c_funambol_source);
611 safestrncpy(config.c_funambol_auth,
613 sizeof config.c_funambol_auth);
616 config.c_rbl_at_greeting = atoi(buf);
619 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
622 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
625 safestrncpy(config.c_pager_program,
627 sizeof config.c_pager_program);
630 config.c_imap_keep_from = atoi(buf);
633 config.c_xmpp_c2s_port = atoi(buf);
636 config.c_xmpp_s2s_port = atoi(buf);
639 config.c_pop3_fetch = atol(buf);
642 config.c_pop3_fastest = atol(buf);
645 config.c_spam_flag_only = atoi(buf);
651 snprintf(buf, sizeof buf,
652 "The global system configuration has been edited by %s.\n",
654 aide_message(buf,"Citadel Configuration Manager Message");
656 if (!IsEmptyStr(config.c_logpages))
657 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
659 /* If full text indexing has been disabled, invalidate the
660 * index so it doesn't try to use it later.
662 if (config.c_enable_fulltext == 0) {
663 CitControl.fulltext_wordbreaker = 0;
668 else if (!strcasecmp(cmd, "GETSYS")) {
669 extract_token(confname, argbuf, 1, '|', sizeof confname);
670 confptr = CtdlGetSysConfig(confname);
671 if (confptr != NULL) {
672 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
673 client_write(confptr, strlen(confptr));
674 if (confptr[strlen(confptr) - 1] != 10)
675 client_write("\n", 1);
679 cprintf("%d No such configuration.\n",
680 ERROR + ILLEGAL_VALUE);
684 else if (!strcasecmp(cmd, "PUTSYS")) {
685 extract_token(confname, argbuf, 1, '|', sizeof confname);
687 cprintf("%d %s\n", SEND_LISTING, confname);
688 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
689 CtdlPutSysConfig(confname, confptr);
694 cprintf("%d Illegal option(s) specified.\n",
695 ERROR + ILLEGAL_VALUE);
700 /*****************************************************************************/
701 /* MODULE INITIALIZATION STUFF */
702 /*****************************************************************************/
705 CTDL_MODULE_INIT(control)
707 CtdlRegisterProtoHook(cmd_conf, "CONF", "Autoconverted. TODO: document me.");
708 /* return our Subversion id for the Log */