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 lprintf(CTDL_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
70 lprintf(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 lprintf(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 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
219 long get_new_user_number(void)
222 begin_critical_section(S_CONTROL);
224 retval = ++CitControl.MMnextuser;
226 end_critical_section(S_CONTROL);
233 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
235 long get_new_room_number(void)
238 begin_critical_section(S_CONTROL);
240 retval = ++CitControl.MMnextroom;
242 end_critical_section(S_CONTROL);
249 * Get or set global configuration options
251 void cmd_conf(char *argbuf)
259 if (CtdlAccessCheck(ac_aide)) return;
261 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
262 if (!strcasecmp(cmd, "GET")) {
263 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
264 cprintf("%s\n", config.c_nodename);
265 cprintf("%s\n", config.c_fqdn);
266 cprintf("%s\n", config.c_humannode);
267 cprintf("%s\n", config.c_phonenum);
268 cprintf("%d\n", config.c_creataide);
269 cprintf("%d\n", config.c_sleeping);
270 cprintf("%d\n", config.c_initax);
271 cprintf("%d\n", config.c_regiscall);
272 cprintf("%d\n", config.c_twitdetect);
273 cprintf("%s\n", config.c_twitroom);
274 cprintf("%s\n", config.c_moreprompt);
275 cprintf("%d\n", config.c_restrict);
276 cprintf("%s\n", config.c_site_location);
277 cprintf("%s\n", config.c_sysadm);
278 cprintf("%d\n", config.c_maxsessions);
279 cprintf("xxx\n"); /* placeholder -- field no longer in use */
280 cprintf("%d\n", config.c_userpurge);
281 cprintf("%d\n", config.c_roompurge);
282 cprintf("%s\n", config.c_logpages);
283 cprintf("%d\n", config.c_createax);
284 cprintf("%ld\n", config.c_maxmsglen);
285 cprintf("%d\n", config.c_min_workers);
286 cprintf("%d\n", config.c_max_workers);
287 cprintf("%d\n", config.c_pop3_port);
288 cprintf("%d\n", config.c_smtp_port);
289 cprintf("%d\n", config.c_rfc822_strict_from);
290 cprintf("%d\n", config.c_aide_zap);
291 cprintf("%d\n", config.c_imap_port);
292 cprintf("%ld\n", config.c_net_freq);
293 cprintf("%d\n", config.c_disable_newu);
294 cprintf("1\n"); /* niu */
295 cprintf("%d\n", config.c_purge_hour);
297 cprintf("%s\n", config.c_ldap_host);
298 cprintf("%d\n", config.c_ldap_port);
299 cprintf("%s\n", config.c_ldap_base_dn);
300 cprintf("%s\n", config.c_ldap_bind_dn);
301 cprintf("%s\n", config.c_ldap_bind_pw);
309 cprintf("%s\n", config.c_ip_addr);
310 cprintf("%d\n", config.c_msa_port);
311 cprintf("%d\n", config.c_imaps_port);
312 cprintf("%d\n", config.c_pop3s_port);
313 cprintf("%d\n", config.c_smtps_port);
314 cprintf("%d\n", config.c_enable_fulltext);
315 cprintf("%d\n", config.c_auto_cull);
316 cprintf("%d\n", config.c_instant_expunge);
317 cprintf("%d\n", config.c_allow_spoofing);
318 cprintf("%d\n", config.c_journal_email);
319 cprintf("%d\n", config.c_journal_pubmsgs);
320 cprintf("%s\n", config.c_journal_dest);
321 cprintf("%s\n", config.c_default_cal_zone);
322 cprintf("%d\n", config.c_pftcpdict_port);
323 cprintf("%d\n", config.c_managesieve_port);
324 cprintf("%d\n", config.c_auth_mode);
325 cprintf("%s\n", config.c_funambol_host);
326 cprintf("%d\n", config.c_funambol_port);
327 cprintf("%s\n", config.c_funambol_source);
328 cprintf("%s\n", config.c_funambol_auth);
329 cprintf("%d\n", config.c_rbl_at_greeting);
330 cprintf("%s\n", config.c_master_user);
331 cprintf("%s\n", config.c_master_pass);
332 cprintf("%s\n", config.c_pager_program);
333 cprintf("%d\n", config.c_imap_keep_from);
334 cprintf("%d\n", config.c_xmpp_c2s_port);
335 cprintf("%d\n", config.c_xmpp_s2s_port);
339 else if (!strcasecmp(cmd, "SET")) {
341 cprintf("%d Send configuration...\n", SEND_LISTING);
343 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
346 safestrncpy(config.c_nodename, buf,
347 sizeof config.c_nodename);
350 safestrncpy(config.c_fqdn, buf,
351 sizeof config.c_fqdn);
354 safestrncpy(config.c_humannode, buf,
355 sizeof config.c_humannode);
358 safestrncpy(config.c_phonenum, buf,
359 sizeof config.c_phonenum);
362 config.c_creataide = atoi(buf);
365 config.c_sleeping = atoi(buf);
368 config.c_initax = atoi(buf);
369 if (config.c_initax < 1)
371 if (config.c_initax > 6)
375 config.c_regiscall = atoi(buf);
376 if (config.c_regiscall != 0)
377 config.c_regiscall = 1;
380 config.c_twitdetect = atoi(buf);
381 if (config.c_twitdetect != 0)
382 config.c_twitdetect = 1;
385 safestrncpy(config.c_twitroom, buf,
386 sizeof config.c_twitroom);
389 safestrncpy(config.c_moreprompt, buf,
390 sizeof config.c_moreprompt);
393 config.c_restrict = atoi(buf);
394 if (config.c_restrict != 0)
395 config.c_restrict = 1;
398 safestrncpy(config.c_site_location, buf,
399 sizeof config.c_site_location);
402 safestrncpy(config.c_sysadm, buf,
403 sizeof config.c_sysadm);
406 config.c_maxsessions = atoi(buf);
407 if (config.c_maxsessions < 0)
408 config.c_maxsessions = 0;
411 /* placeholder -- field no longer in use */
414 config.c_userpurge = atoi(buf);
417 config.c_roompurge = atoi(buf);
420 safestrncpy(config.c_logpages, buf,
421 sizeof config.c_logpages);
424 config.c_createax = atoi(buf);
425 if (config.c_createax < 1)
426 config.c_createax = 1;
427 if (config.c_createax > 6)
428 config.c_createax = 6;
431 if (atoi(buf) >= 8192)
432 config.c_maxmsglen = atoi(buf);
436 config.c_min_workers = atoi(buf);
438 if (atoi(buf) >= config.c_min_workers)
439 config.c_max_workers = atoi(buf);
441 config.c_pop3_port = atoi(buf);
444 config.c_smtp_port = atoi(buf);
447 config.c_rfc822_strict_from = atoi(buf);
450 config.c_aide_zap = atoi(buf);
451 if (config.c_aide_zap != 0)
452 config.c_aide_zap = 1;
455 config.c_imap_port = atoi(buf);
458 config.c_net_freq = atol(buf);
461 config.c_disable_newu = atoi(buf);
462 if (config.c_disable_newu != 0)
463 config.c_disable_newu = 1;
469 if ((config.c_purge_hour >= 0)
470 && (config.c_purge_hour <= 23)) {
471 config.c_purge_hour = atoi(buf);
476 safestrncpy(config.c_ldap_host, buf,
477 sizeof config.c_ldap_host);
480 config.c_ldap_port = atoi(buf);
483 safestrncpy(config.c_ldap_base_dn, buf,
484 sizeof config.c_ldap_base_dn);
487 safestrncpy(config.c_ldap_bind_dn, buf,
488 sizeof config.c_ldap_bind_dn);
491 safestrncpy(config.c_ldap_bind_pw, buf,
492 sizeof config.c_ldap_bind_pw);
496 safestrncpy(config.c_ip_addr, buf,
497 sizeof config.c_ip_addr);
499 config.c_msa_port = atoi(buf);
502 config.c_imaps_port = atoi(buf);
505 config.c_pop3s_port = atoi(buf);
508 config.c_smtps_port = atoi(buf);
511 config.c_enable_fulltext = atoi(buf);
514 config.c_auto_cull = atoi(buf);
517 config.c_instant_expunge = atoi(buf);
520 config.c_allow_spoofing = atoi(buf);
523 config.c_journal_email = atoi(buf);
526 config.c_journal_pubmsgs = atoi(buf);
529 safestrncpy(config.c_journal_dest, buf,
530 sizeof config.c_journal_dest);
532 safestrncpy(config.c_default_cal_zone, buf,
533 sizeof config.c_default_cal_zone);
536 config.c_pftcpdict_port = atoi(buf);
539 config.c_managesieve_port = atoi(buf);
542 config.c_auth_mode = atoi(buf);
544 safestrncpy(config.c_funambol_host, buf,
545 sizeof config.c_funambol_host);
548 config.c_funambol_port = atoi(buf);
551 safestrncpy(config.c_funambol_source,
553 sizeof config.c_funambol_source);
556 safestrncpy(config.c_funambol_auth,
558 sizeof config.c_funambol_auth);
561 config.c_rbl_at_greeting = atoi(buf);
564 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
567 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
570 safestrncpy(config.c_pager_program,
572 sizeof config.c_pager_program);
575 config.c_imap_keep_from = atoi(buf);
578 config.c_xmpp_c2s_port = atoi(buf);
581 config.c_xmpp_s2s_port = atoi(buf);
587 snprintf(buf, sizeof buf,
588 "The global system configuration has been edited by %s.\n",
590 aide_message(buf,"Citadel Configuration Manager Message");
592 if (!IsEmptyStr(config.c_logpages))
593 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
595 /* If full text indexing has been disabled, invalidate the
596 * index so it doesn't try to use it later.
598 if (config.c_enable_fulltext == 0) {
599 CitControl.fulltext_wordbreaker = 0;
604 else if (!strcasecmp(cmd, "GETSYS")) {
605 extract_token(confname, argbuf, 1, '|', sizeof confname);
606 confptr = CtdlGetSysConfig(confname);
607 if (confptr != NULL) {
608 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
609 client_write(confptr, strlen(confptr));
610 if (confptr[strlen(confptr) - 1] != 10)
611 client_write("\n", 1);
615 cprintf("%d No such configuration.\n",
616 ERROR + ILLEGAL_VALUE);
620 else if (!strcasecmp(cmd, "PUTSYS")) {
621 extract_token(confname, argbuf, 1, '|', sizeof confname);
623 cprintf("%d %s\n", SEND_LISTING, confname);
624 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
625 CtdlPutSysConfig(confname, confptr);
630 cprintf("%d Illegal option(s) specified.\n",
631 ERROR + ILLEGAL_VALUE);