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;
175 chown(file_citadel_control, config.c_ctdluid, (-1));
179 * put_control - write the control record to disk.
181 void put_control(void)
184 if (control_fp != NULL) {
186 fwrite(&CitControl, sizeof(struct CitControl), 1,
193 * release_control - close our fd on exit
195 void release_control(void)
197 if (control_fp != NULL)
203 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
205 long get_new_message_number(void)
208 begin_critical_section(S_CONTROL);
210 retval = ++CitControl.MMhighest;
212 end_critical_section(S_CONTROL);
218 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
219 * This provides a quick way to initialise a variable that might be used to indicate
220 * messages that should not be processed. EG. a new Sieve script will use this
221 * to record determine that messages older than this should not be processed.
223 long CtdlGetCurrentMessageNumber(void)
226 begin_critical_section(S_CONTROL);
228 retval = CitControl.MMhighest;
229 end_critical_section(S_CONTROL);
235 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
237 long get_new_user_number(void)
240 begin_critical_section(S_CONTROL);
242 retval = ++CitControl.MMnextuser;
244 end_critical_section(S_CONTROL);
251 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
253 long get_new_room_number(void)
256 begin_critical_section(S_CONTROL);
258 retval = ++CitControl.MMnextroom;
260 end_critical_section(S_CONTROL);
267 * Get or set global configuration options
269 void cmd_conf(char *argbuf)
277 if (CtdlAccessCheck(ac_aide)) return;
279 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
280 if (!strcasecmp(cmd, "GET")) {
281 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
282 cprintf("%s\n", config.c_nodename);
283 cprintf("%s\n", config.c_fqdn);
284 cprintf("%s\n", config.c_humannode);
285 cprintf("%s\n", config.c_phonenum);
286 cprintf("%d\n", config.c_creataide);
287 cprintf("%d\n", config.c_sleeping);
288 cprintf("%d\n", config.c_initax);
289 cprintf("%d\n", config.c_regiscall);
290 cprintf("%d\n", config.c_twitdetect);
291 cprintf("%s\n", config.c_twitroom);
292 cprintf("%s\n", config.c_moreprompt);
293 cprintf("%d\n", config.c_restrict);
294 cprintf("%s\n", config.c_site_location);
295 cprintf("%s\n", config.c_sysadm);
296 cprintf("%d\n", config.c_maxsessions);
297 cprintf("xxx\n"); /* placeholder -- field no longer in use */
298 cprintf("%d\n", config.c_userpurge);
299 cprintf("%d\n", config.c_roompurge);
300 cprintf("%s\n", config.c_logpages);
301 cprintf("%d\n", config.c_createax);
302 cprintf("%ld\n", config.c_maxmsglen);
303 cprintf("%d\n", config.c_min_workers);
304 cprintf("%d\n", config.c_max_workers);
305 cprintf("%d\n", config.c_pop3_port);
306 cprintf("%d\n", config.c_smtp_port);
307 cprintf("%d\n", config.c_rfc822_strict_from);
308 cprintf("%d\n", config.c_aide_zap);
309 cprintf("%d\n", config.c_imap_port);
310 cprintf("%ld\n", config.c_net_freq);
311 cprintf("%d\n", config.c_disable_newu);
312 cprintf("1\n"); /* niu */
313 cprintf("%d\n", config.c_purge_hour);
315 cprintf("%s\n", config.c_ldap_host);
316 cprintf("%d\n", config.c_ldap_port);
317 cprintf("%s\n", config.c_ldap_base_dn);
318 cprintf("%s\n", config.c_ldap_bind_dn);
319 cprintf("%s\n", config.c_ldap_bind_pw);
327 cprintf("%s\n", config.c_ip_addr);
328 cprintf("%d\n", config.c_msa_port);
329 cprintf("%d\n", config.c_imaps_port);
330 cprintf("%d\n", config.c_pop3s_port);
331 cprintf("%d\n", config.c_smtps_port);
332 cprintf("%d\n", config.c_enable_fulltext);
333 cprintf("%d\n", config.c_auto_cull);
334 cprintf("%d\n", config.c_instant_expunge);
335 cprintf("%d\n", config.c_allow_spoofing);
336 cprintf("%d\n", config.c_journal_email);
337 cprintf("%d\n", config.c_journal_pubmsgs);
338 cprintf("%s\n", config.c_journal_dest);
339 cprintf("%s\n", config.c_default_cal_zone);
340 cprintf("%d\n", config.c_pftcpdict_port);
341 cprintf("%d\n", config.c_managesieve_port);
342 cprintf("%d\n", config.c_auth_mode);
343 cprintf("%s\n", config.c_funambol_host);
344 cprintf("%d\n", config.c_funambol_port);
345 cprintf("%s\n", config.c_funambol_source);
346 cprintf("%s\n", config.c_funambol_auth);
347 cprintf("%d\n", config.c_rbl_at_greeting);
348 cprintf("%s\n", config.c_master_user);
349 cprintf("%s\n", config.c_master_pass);
350 cprintf("%s\n", config.c_pager_program);
351 cprintf("%d\n", config.c_imap_keep_from);
352 cprintf("%d\n", config.c_xmpp_c2s_port);
353 cprintf("%d\n", config.c_xmpp_s2s_port);
354 cprintf("%ld\n", config.c_pop3_fetch);
355 cprintf("%ld\n", config.c_pop3_fastest);
359 else if (!strcasecmp(cmd, "SET")) {
361 cprintf("%d Send configuration...\n", SEND_LISTING);
363 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
366 safestrncpy(config.c_nodename, buf,
367 sizeof config.c_nodename);
370 safestrncpy(config.c_fqdn, buf,
371 sizeof config.c_fqdn);
374 safestrncpy(config.c_humannode, buf,
375 sizeof config.c_humannode);
378 safestrncpy(config.c_phonenum, buf,
379 sizeof config.c_phonenum);
382 config.c_creataide = atoi(buf);
385 config.c_sleeping = atoi(buf);
388 config.c_initax = atoi(buf);
389 if (config.c_initax < 1)
391 if (config.c_initax > 6)
395 config.c_regiscall = atoi(buf);
396 if (config.c_regiscall != 0)
397 config.c_regiscall = 1;
400 config.c_twitdetect = atoi(buf);
401 if (config.c_twitdetect != 0)
402 config.c_twitdetect = 1;
405 safestrncpy(config.c_twitroom, buf,
406 sizeof config.c_twitroom);
409 safestrncpy(config.c_moreprompt, buf,
410 sizeof config.c_moreprompt);
413 config.c_restrict = atoi(buf);
414 if (config.c_restrict != 0)
415 config.c_restrict = 1;
418 safestrncpy(config.c_site_location, buf,
419 sizeof config.c_site_location);
422 safestrncpy(config.c_sysadm, buf,
423 sizeof config.c_sysadm);
426 config.c_maxsessions = atoi(buf);
427 if (config.c_maxsessions < 0)
428 config.c_maxsessions = 0;
431 /* placeholder -- field no longer in use */
434 config.c_userpurge = atoi(buf);
437 config.c_roompurge = atoi(buf);
440 safestrncpy(config.c_logpages, buf,
441 sizeof config.c_logpages);
444 config.c_createax = atoi(buf);
445 if (config.c_createax < 1)
446 config.c_createax = 1;
447 if (config.c_createax > 6)
448 config.c_createax = 6;
451 if (atoi(buf) >= 8192)
452 config.c_maxmsglen = atoi(buf);
456 config.c_min_workers = atoi(buf);
458 if (atoi(buf) >= config.c_min_workers)
459 config.c_max_workers = atoi(buf);
461 config.c_pop3_port = atoi(buf);
464 config.c_smtp_port = atoi(buf);
467 config.c_rfc822_strict_from = atoi(buf);
470 config.c_aide_zap = atoi(buf);
471 if (config.c_aide_zap != 0)
472 config.c_aide_zap = 1;
475 config.c_imap_port = atoi(buf);
478 config.c_net_freq = atol(buf);
481 config.c_disable_newu = atoi(buf);
482 if (config.c_disable_newu != 0)
483 config.c_disable_newu = 1;
489 if ((config.c_purge_hour >= 0)
490 && (config.c_purge_hour <= 23)) {
491 config.c_purge_hour = atoi(buf);
496 safestrncpy(config.c_ldap_host, buf,
497 sizeof config.c_ldap_host);
500 config.c_ldap_port = atoi(buf);
503 safestrncpy(config.c_ldap_base_dn, buf,
504 sizeof config.c_ldap_base_dn);
507 safestrncpy(config.c_ldap_bind_dn, buf,
508 sizeof config.c_ldap_bind_dn);
511 safestrncpy(config.c_ldap_bind_pw, buf,
512 sizeof config.c_ldap_bind_pw);
516 safestrncpy(config.c_ip_addr, buf,
517 sizeof config.c_ip_addr);
519 config.c_msa_port = atoi(buf);
522 config.c_imaps_port = atoi(buf);
525 config.c_pop3s_port = atoi(buf);
528 config.c_smtps_port = atoi(buf);
531 config.c_enable_fulltext = atoi(buf);
534 config.c_auto_cull = atoi(buf);
537 config.c_instant_expunge = atoi(buf);
540 config.c_allow_spoofing = atoi(buf);
543 config.c_journal_email = atoi(buf);
546 config.c_journal_pubmsgs = atoi(buf);
549 safestrncpy(config.c_journal_dest, buf,
550 sizeof config.c_journal_dest);
552 safestrncpy(config.c_default_cal_zone, buf,
553 sizeof config.c_default_cal_zone);
556 config.c_pftcpdict_port = atoi(buf);
559 config.c_managesieve_port = atoi(buf);
562 config.c_auth_mode = atoi(buf);
564 safestrncpy(config.c_funambol_host, buf,
565 sizeof config.c_funambol_host);
568 config.c_funambol_port = atoi(buf);
571 safestrncpy(config.c_funambol_source,
573 sizeof config.c_funambol_source);
576 safestrncpy(config.c_funambol_auth,
578 sizeof config.c_funambol_auth);
581 config.c_rbl_at_greeting = atoi(buf);
584 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
587 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
590 safestrncpy(config.c_pager_program,
592 sizeof config.c_pager_program);
595 config.c_imap_keep_from = atoi(buf);
598 config.c_xmpp_c2s_port = atoi(buf);
601 config.c_xmpp_s2s_port = atoi(buf);
604 config.c_pop3_fetch = atol(buf);
607 config.c_pop3_fastest = atol(buf);
613 snprintf(buf, sizeof buf,
614 "The global system configuration has been edited by %s.\n",
616 aide_message(buf,"Citadel Configuration Manager Message");
618 if (!IsEmptyStr(config.c_logpages))
619 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
621 /* If full text indexing has been disabled, invalidate the
622 * index so it doesn't try to use it later.
624 if (config.c_enable_fulltext == 0) {
625 CitControl.fulltext_wordbreaker = 0;
630 else if (!strcasecmp(cmd, "GETSYS")) {
631 extract_token(confname, argbuf, 1, '|', sizeof confname);
632 confptr = CtdlGetSysConfig(confname);
633 if (confptr != NULL) {
634 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
635 client_write(confptr, strlen(confptr));
636 if (confptr[strlen(confptr) - 1] != 10)
637 client_write("\n", 1);
641 cprintf("%d No such configuration.\n",
642 ERROR + ILLEGAL_VALUE);
646 else if (!strcasecmp(cmd, "PUTSYS")) {
647 extract_token(confname, argbuf, 1, '|', sizeof confname);
649 cprintf("%d %s\n", SEND_LISTING, confname);
650 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
651 CtdlPutSysConfig(confname, confptr);
656 cprintf("%d Illegal option(s) specified.\n",
657 ERROR + ILLEGAL_VALUE);