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;
88 if (qrbuf->QRnumber > CitControl.MMnextroom)
89 CitControl.MMnextroom = qrbuf->QRnumber;
91 getroom (&room, qrbuf->QRname);
93 /* Load the message list */
94 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
96 msglist = (long *) cdbfr->ptr;
97 num_msgs = cdbfr->len / sizeof(long);
99 return; /* No messages at all? No further action. */
104 for (c=0; c<num_msgs; c++)
106 if (msglist[c] > CitControl.MMhighest)
107 CitControl.MMhighest = msglist[c];
116 * Callback to get highest user number.
119 void control_find_user (struct ctdluser *EachUser, void *out_data)
121 if (EachUser->usernum > CitControl.MMnextuser)
122 CitControl.MMnextuser = EachUser->usernum;
127 * get_control - read the control record into memory.
129 void get_control(void)
131 static int already_have_control = 0;
134 * If we already have the control record in memory, there's no point
135 * in reading it from disk again.
137 if (already_have_control) return;
139 /* Zero it out. If the control record on disk is missing or short,
140 * the system functions with all control record fields initialized
143 memset(&CitControl, 0, sizeof(struct CitControl));
144 if (control_fp == NULL) {
145 control_fp = fopen(file_citadel_control, "rb+");
146 if (control_fp != NULL) {
148 fchown(fileno(control_fp), config.c_ctdluid, -1);
151 if (control_fp == NULL) {
152 control_fp = fopen(file_citadel_control, "wb+");
153 if (control_fp != NULL) {
155 fchown(fileno(control_fp), config.c_ctdluid, -1);
156 memset(&CitControl, 0, sizeof(struct CitControl));
157 // Find highest room number and message number.
158 ForEachRoom(control_find_highest, NULL);
159 ForEachUser(control_find_user, NULL);
160 fwrite(&CitControl, sizeof(struct CitControl),
165 if (control_fp == NULL) {
166 CtdlLogPrintf(CTDL_ALERT, "ERROR opening %s: %s\n",
167 file_citadel_control,
173 fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
174 already_have_control = 1;
178 * put_control - write the control record to disk.
180 void put_control(void)
183 if (control_fp != NULL) {
185 fwrite(&CitControl, sizeof(struct CitControl), 1,
192 * release_control - close our fd on exit
194 void release_control(void)
196 if (control_fp != NULL)
202 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
204 long get_new_message_number(void)
207 begin_critical_section(S_CONTROL);
209 retval = ++CitControl.MMhighest;
211 end_critical_section(S_CONTROL);
217 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
218 * This provides a quick way to initialise a variable that might be used to indicate
219 * messages that should not be processed. EG. a new Sieve script will use this
220 * to record determine that messages older than this should not be processed.
222 long CtdlGetCurrentMessageNumber(void)
225 begin_critical_section(S_CONTROL);
227 retval = CitControl.MMhighest;
228 end_critical_section(S_CONTROL);
234 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
236 long get_new_user_number(void)
239 begin_critical_section(S_CONTROL);
241 retval = ++CitControl.MMnextuser;
243 end_critical_section(S_CONTROL);
250 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
252 long get_new_room_number(void)
255 begin_critical_section(S_CONTROL);
257 retval = ++CitControl.MMnextroom;
259 end_critical_section(S_CONTROL);
266 * Get or set global configuration options
268 void cmd_conf(char *argbuf)
276 if (CtdlAccessCheck(ac_aide)) return;
278 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
279 if (!strcasecmp(cmd, "GET")) {
280 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
281 cprintf("%s\n", config.c_nodename);
282 cprintf("%s\n", config.c_fqdn);
283 cprintf("%s\n", config.c_humannode);
284 cprintf("%s\n", config.c_phonenum);
285 cprintf("%d\n", config.c_creataide);
286 cprintf("%d\n", config.c_sleeping);
287 cprintf("%d\n", config.c_initax);
288 cprintf("%d\n", config.c_regiscall);
289 cprintf("%d\n", config.c_twitdetect);
290 cprintf("%s\n", config.c_twitroom);
291 cprintf("%s\n", config.c_moreprompt);
292 cprintf("%d\n", config.c_restrict);
293 cprintf("%s\n", config.c_site_location);
294 cprintf("%s\n", config.c_sysadm);
295 cprintf("%d\n", config.c_maxsessions);
296 cprintf("xxx\n"); /* placeholder -- field no longer in use */
297 cprintf("%d\n", config.c_userpurge);
298 cprintf("%d\n", config.c_roompurge);
299 cprintf("%s\n", config.c_logpages);
300 cprintf("%d\n", config.c_createax);
301 cprintf("%ld\n", config.c_maxmsglen);
302 cprintf("%d\n", config.c_min_workers);
303 cprintf("%d\n", config.c_max_workers);
304 cprintf("%d\n", config.c_pop3_port);
305 cprintf("%d\n", config.c_smtp_port);
306 cprintf("%d\n", config.c_rfc822_strict_from);
307 cprintf("%d\n", config.c_aide_zap);
308 cprintf("%d\n", config.c_imap_port);
309 cprintf("%ld\n", config.c_net_freq);
310 cprintf("%d\n", config.c_disable_newu);
311 cprintf("1\n"); /* niu */
312 cprintf("%d\n", config.c_purge_hour);
314 cprintf("%s\n", config.c_ldap_host);
315 cprintf("%d\n", config.c_ldap_port);
316 cprintf("%s\n", config.c_ldap_base_dn);
317 cprintf("%s\n", config.c_ldap_bind_dn);
318 cprintf("%s\n", config.c_ldap_bind_pw);
326 cprintf("%s\n", config.c_ip_addr);
327 cprintf("%d\n", config.c_msa_port);
328 cprintf("%d\n", config.c_imaps_port);
329 cprintf("%d\n", config.c_pop3s_port);
330 cprintf("%d\n", config.c_smtps_port);
331 cprintf("%d\n", config.c_enable_fulltext);
332 cprintf("%d\n", config.c_auto_cull);
333 cprintf("%d\n", config.c_instant_expunge);
334 cprintf("%d\n", config.c_allow_spoofing);
335 cprintf("%d\n", config.c_journal_email);
336 cprintf("%d\n", config.c_journal_pubmsgs);
337 cprintf("%s\n", config.c_journal_dest);
338 cprintf("%s\n", config.c_default_cal_zone);
339 cprintf("%d\n", config.c_pftcpdict_port);
340 cprintf("%d\n", config.c_managesieve_port);
341 cprintf("%d\n", config.c_auth_mode);
342 cprintf("%s\n", config.c_funambol_host);
343 cprintf("%d\n", config.c_funambol_port);
344 cprintf("%s\n", config.c_funambol_source);
345 cprintf("%s\n", config.c_funambol_auth);
346 cprintf("%d\n", config.c_rbl_at_greeting);
347 cprintf("%s\n", config.c_master_user);
348 cprintf("%s\n", config.c_master_pass);
349 cprintf("%s\n", config.c_pager_program);
350 cprintf("%d\n", config.c_imap_keep_from);
351 cprintf("%d\n", config.c_xmpp_c2s_port);
352 cprintf("%d\n", config.c_xmpp_s2s_port);
353 cprintf("%ld\n", config.c_pop3_fetch);
354 cprintf("%ld\n", config.c_pop3_fastest);
358 else if (!strcasecmp(cmd, "SET")) {
360 cprintf("%d Send configuration...\n", SEND_LISTING);
362 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
365 safestrncpy(config.c_nodename, buf,
366 sizeof config.c_nodename);
369 safestrncpy(config.c_fqdn, buf,
370 sizeof config.c_fqdn);
373 safestrncpy(config.c_humannode, buf,
374 sizeof config.c_humannode);
377 safestrncpy(config.c_phonenum, buf,
378 sizeof config.c_phonenum);
381 config.c_creataide = atoi(buf);
384 config.c_sleeping = atoi(buf);
387 config.c_initax = atoi(buf);
388 if (config.c_initax < 1)
390 if (config.c_initax > 6)
394 config.c_regiscall = atoi(buf);
395 if (config.c_regiscall != 0)
396 config.c_regiscall = 1;
399 config.c_twitdetect = atoi(buf);
400 if (config.c_twitdetect != 0)
401 config.c_twitdetect = 1;
404 safestrncpy(config.c_twitroom, buf,
405 sizeof config.c_twitroom);
408 safestrncpy(config.c_moreprompt, buf,
409 sizeof config.c_moreprompt);
412 config.c_restrict = atoi(buf);
413 if (config.c_restrict != 0)
414 config.c_restrict = 1;
417 safestrncpy(config.c_site_location, buf,
418 sizeof config.c_site_location);
421 safestrncpy(config.c_sysadm, buf,
422 sizeof config.c_sysadm);
425 config.c_maxsessions = atoi(buf);
426 if (config.c_maxsessions < 0)
427 config.c_maxsessions = 0;
430 /* placeholder -- field no longer in use */
433 config.c_userpurge = atoi(buf);
436 config.c_roompurge = atoi(buf);
439 safestrncpy(config.c_logpages, buf,
440 sizeof config.c_logpages);
443 config.c_createax = atoi(buf);
444 if (config.c_createax < 1)
445 config.c_createax = 1;
446 if (config.c_createax > 6)
447 config.c_createax = 6;
450 if (atoi(buf) >= 8192)
451 config.c_maxmsglen = atoi(buf);
455 config.c_min_workers = atoi(buf);
457 if (atoi(buf) >= config.c_min_workers)
458 config.c_max_workers = atoi(buf);
460 config.c_pop3_port = atoi(buf);
463 config.c_smtp_port = atoi(buf);
466 config.c_rfc822_strict_from = atoi(buf);
469 config.c_aide_zap = atoi(buf);
470 if (config.c_aide_zap != 0)
471 config.c_aide_zap = 1;
474 config.c_imap_port = atoi(buf);
477 config.c_net_freq = atol(buf);
480 config.c_disable_newu = atoi(buf);
481 if (config.c_disable_newu != 0)
482 config.c_disable_newu = 1;
488 if ((config.c_purge_hour >= 0)
489 && (config.c_purge_hour <= 23)) {
490 config.c_purge_hour = atoi(buf);
495 safestrncpy(config.c_ldap_host, buf,
496 sizeof config.c_ldap_host);
499 config.c_ldap_port = atoi(buf);
502 safestrncpy(config.c_ldap_base_dn, buf,
503 sizeof config.c_ldap_base_dn);
506 safestrncpy(config.c_ldap_bind_dn, buf,
507 sizeof config.c_ldap_bind_dn);
510 safestrncpy(config.c_ldap_bind_pw, buf,
511 sizeof config.c_ldap_bind_pw);
515 safestrncpy(config.c_ip_addr, buf,
516 sizeof config.c_ip_addr);
518 config.c_msa_port = atoi(buf);
521 config.c_imaps_port = atoi(buf);
524 config.c_pop3s_port = atoi(buf);
527 config.c_smtps_port = atoi(buf);
530 config.c_enable_fulltext = atoi(buf);
533 config.c_auto_cull = atoi(buf);
536 config.c_instant_expunge = atoi(buf);
539 config.c_allow_spoofing = atoi(buf);
542 config.c_journal_email = atoi(buf);
545 config.c_journal_pubmsgs = atoi(buf);
548 safestrncpy(config.c_journal_dest, buf,
549 sizeof config.c_journal_dest);
551 safestrncpy(config.c_default_cal_zone, buf,
552 sizeof config.c_default_cal_zone);
555 config.c_pftcpdict_port = atoi(buf);
558 config.c_managesieve_port = atoi(buf);
561 config.c_auth_mode = atoi(buf);
563 safestrncpy(config.c_funambol_host, buf,
564 sizeof config.c_funambol_host);
567 config.c_funambol_port = atoi(buf);
570 safestrncpy(config.c_funambol_source,
572 sizeof config.c_funambol_source);
575 safestrncpy(config.c_funambol_auth,
577 sizeof config.c_funambol_auth);
580 config.c_rbl_at_greeting = atoi(buf);
583 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
586 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
589 safestrncpy(config.c_pager_program,
591 sizeof config.c_pager_program);
594 config.c_imap_keep_from = atoi(buf);
597 config.c_xmpp_c2s_port = atoi(buf);
600 config.c_xmpp_s2s_port = atoi(buf);
603 config.c_pop3_fetch = atol(buf);
606 config.c_pop3_fastest = atol(buf);
612 snprintf(buf, sizeof buf,
613 "The global system configuration has been edited by %s.\n",
615 aide_message(buf,"Citadel Configuration Manager Message");
617 if (!IsEmptyStr(config.c_logpages))
618 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
620 /* If full text indexing has been disabled, invalidate the
621 * index so it doesn't try to use it later.
623 if (config.c_enable_fulltext == 0) {
624 CitControl.fulltext_wordbreaker = 0;
629 else if (!strcasecmp(cmd, "GETSYS")) {
630 extract_token(confname, argbuf, 1, '|', sizeof confname);
631 confptr = CtdlGetSysConfig(confname);
632 if (confptr != NULL) {
633 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
634 client_write(confptr, strlen(confptr));
635 if (confptr[strlen(confptr) - 1] != 10)
636 client_write("\n", 1);
640 cprintf("%d No such configuration.\n",
641 ERROR + ILLEGAL_VALUE);
645 else if (!strcasecmp(cmd, "PUTSYS")) {
646 extract_token(confname, argbuf, 1, '|', sizeof confname);
648 cprintf("%d %s\n", SEND_LISTING, confname);
649 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
650 CtdlPutSysConfig(confname, confptr);
655 cprintf("%d Illegal option(s) specified.\n",
656 ERROR + ILLEGAL_VALUE);