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 * 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);
336 cprintf("%ld\n", config.c_pop3_fetch);
337 cprintf("%ld\n", config.c_pop3_fastest);
341 else if (!strcasecmp(cmd, "SET")) {
343 cprintf("%d Send configuration...\n", SEND_LISTING);
345 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
348 safestrncpy(config.c_nodename, buf,
349 sizeof config.c_nodename);
352 safestrncpy(config.c_fqdn, buf,
353 sizeof config.c_fqdn);
356 safestrncpy(config.c_humannode, buf,
357 sizeof config.c_humannode);
360 safestrncpy(config.c_phonenum, buf,
361 sizeof config.c_phonenum);
364 config.c_creataide = atoi(buf);
367 config.c_sleeping = atoi(buf);
370 config.c_initax = atoi(buf);
371 if (config.c_initax < 1)
373 if (config.c_initax > 6)
377 config.c_regiscall = atoi(buf);
378 if (config.c_regiscall != 0)
379 config.c_regiscall = 1;
382 config.c_twitdetect = atoi(buf);
383 if (config.c_twitdetect != 0)
384 config.c_twitdetect = 1;
387 safestrncpy(config.c_twitroom, buf,
388 sizeof config.c_twitroom);
391 safestrncpy(config.c_moreprompt, buf,
392 sizeof config.c_moreprompt);
395 config.c_restrict = atoi(buf);
396 if (config.c_restrict != 0)
397 config.c_restrict = 1;
400 safestrncpy(config.c_site_location, buf,
401 sizeof config.c_site_location);
404 safestrncpy(config.c_sysadm, buf,
405 sizeof config.c_sysadm);
408 config.c_maxsessions = atoi(buf);
409 if (config.c_maxsessions < 0)
410 config.c_maxsessions = 0;
413 /* placeholder -- field no longer in use */
416 config.c_userpurge = atoi(buf);
419 config.c_roompurge = atoi(buf);
422 safestrncpy(config.c_logpages, buf,
423 sizeof config.c_logpages);
426 config.c_createax = atoi(buf);
427 if (config.c_createax < 1)
428 config.c_createax = 1;
429 if (config.c_createax > 6)
430 config.c_createax = 6;
433 if (atoi(buf) >= 8192)
434 config.c_maxmsglen = atoi(buf);
438 config.c_min_workers = atoi(buf);
440 if (atoi(buf) >= config.c_min_workers)
441 config.c_max_workers = atoi(buf);
443 config.c_pop3_port = atoi(buf);
446 config.c_smtp_port = atoi(buf);
449 config.c_rfc822_strict_from = atoi(buf);
452 config.c_aide_zap = atoi(buf);
453 if (config.c_aide_zap != 0)
454 config.c_aide_zap = 1;
457 config.c_imap_port = atoi(buf);
460 config.c_net_freq = atol(buf);
463 config.c_disable_newu = atoi(buf);
464 if (config.c_disable_newu != 0)
465 config.c_disable_newu = 1;
471 if ((config.c_purge_hour >= 0)
472 && (config.c_purge_hour <= 23)) {
473 config.c_purge_hour = atoi(buf);
478 safestrncpy(config.c_ldap_host, buf,
479 sizeof config.c_ldap_host);
482 config.c_ldap_port = atoi(buf);
485 safestrncpy(config.c_ldap_base_dn, buf,
486 sizeof config.c_ldap_base_dn);
489 safestrncpy(config.c_ldap_bind_dn, buf,
490 sizeof config.c_ldap_bind_dn);
493 safestrncpy(config.c_ldap_bind_pw, buf,
494 sizeof config.c_ldap_bind_pw);
498 safestrncpy(config.c_ip_addr, buf,
499 sizeof config.c_ip_addr);
501 config.c_msa_port = atoi(buf);
504 config.c_imaps_port = atoi(buf);
507 config.c_pop3s_port = atoi(buf);
510 config.c_smtps_port = atoi(buf);
513 config.c_enable_fulltext = atoi(buf);
516 config.c_auto_cull = atoi(buf);
519 config.c_instant_expunge = atoi(buf);
522 config.c_allow_spoofing = atoi(buf);
525 config.c_journal_email = atoi(buf);
528 config.c_journal_pubmsgs = atoi(buf);
531 safestrncpy(config.c_journal_dest, buf,
532 sizeof config.c_journal_dest);
534 safestrncpy(config.c_default_cal_zone, buf,
535 sizeof config.c_default_cal_zone);
538 config.c_pftcpdict_port = atoi(buf);
541 config.c_managesieve_port = atoi(buf);
544 config.c_auth_mode = atoi(buf);
546 safestrncpy(config.c_funambol_host, buf,
547 sizeof config.c_funambol_host);
550 config.c_funambol_port = atoi(buf);
553 safestrncpy(config.c_funambol_source,
555 sizeof config.c_funambol_source);
558 safestrncpy(config.c_funambol_auth,
560 sizeof config.c_funambol_auth);
563 config.c_rbl_at_greeting = atoi(buf);
566 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
569 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
572 safestrncpy(config.c_pager_program,
574 sizeof config.c_pager_program);
577 config.c_imap_keep_from = atoi(buf);
580 config.c_xmpp_c2s_port = atoi(buf);
583 config.c_xmpp_s2s_port = atoi(buf);
586 config.c_pop3_fetch = atol(buf);
589 config.c_pop3_fastest = atol(buf);
595 snprintf(buf, sizeof buf,
596 "The global system configuration has been edited by %s.\n",
598 aide_message(buf,"Citadel Configuration Manager Message");
600 if (!IsEmptyStr(config.c_logpages))
601 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
603 /* If full text indexing has been disabled, invalidate the
604 * index so it doesn't try to use it later.
606 if (config.c_enable_fulltext == 0) {
607 CitControl.fulltext_wordbreaker = 0;
612 else if (!strcasecmp(cmd, "GETSYS")) {
613 extract_token(confname, argbuf, 1, '|', sizeof confname);
614 confptr = CtdlGetSysConfig(confname);
615 if (confptr != NULL) {
616 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
617 client_write(confptr, strlen(confptr));
618 if (confptr[strlen(confptr) - 1] != 10)
619 client_write("\n", 1);
623 cprintf("%d No such configuration.\n",
624 ERROR + ILLEGAL_VALUE);
628 else if (!strcasecmp(cmd, "PUTSYS")) {
629 extract_token(confname, argbuf, 1, '|', sizeof confname);
631 cprintf("%d %s\n", SEND_LISTING, confname);
632 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
633 CtdlPutSysConfig(confname, confptr);
638 cprintf("%d Illegal option(s) specified.\n",
639 ERROR + ILLEGAL_VALUE);