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"
49 struct CitControl CitControl;
50 extern struct config config;
51 FILE *control_fp = NULL;
52 long control_highest_user = 0;
56 * lock_control - acquire a lock on the control record file.
57 * This keeps multiple citservers from running concurrently.
59 void lock_control(void)
63 * TODO: solaris manpages describe this function, but the headers
67 if (flock(fileno(control_fp), (LOCK_EX | LOCK_NB))) {
68 lprintf(CTDL_EMERG, "citserver: unable to lock %s.\n", file_citadel_control);
69 lprintf(CTDL_EMERG, "Is another citserver already running?\n");
70 exit(CTDLEXIT_CONTROL);
76 * callback to get highest room number when rebuilding control file
78 void control_find_highest(struct ctdlroom *qrbuf, void *data)
81 struct cdbdata *cdbfr;
87 if (qrbuf->QRnumber > CitControl.MMnextroom)
88 CitControl.MMnextroom = qrbuf->QRnumber;
90 getroom (&room, qrbuf->QRname);
92 /* Load the message list */
93 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
95 msglist = (long *) cdbfr->ptr;
96 num_msgs = cdbfr->len / sizeof(long);
98 return; /* No messages at all? No further action. */
103 for (c=0; c<num_msgs; c++)
105 if (msglist[c] > CitControl.MMhighest)
106 CitControl.MMhighest = msglist[c];
115 * Callback to get highest user number.
118 void control_find_user (struct ctdluser *EachUser, void *out_data)
120 if (EachUser->usernum > CitControl.MMnextuser)
121 CitControl.MMnextuser = EachUser->usernum;
126 * get_control - read the control record into memory.
128 void get_control(void)
130 static int already_have_control = 0;
133 * If we already have the control record in memory, there's no point
134 * in reading it from disk again.
136 if (already_have_control) return;
138 /* Zero it out. If the control record on disk is missing or short,
139 * the system functions with all control record fields initialized
142 memset(&CitControl, 0, sizeof(struct CitControl));
143 if (control_fp == NULL) {
144 control_fp = fopen(file_citadel_control, "rb+");
145 if (control_fp != NULL) {
147 fchown(fileno(control_fp), config.c_ctdluid, -1);
150 if (control_fp == NULL) {
151 control_fp = fopen(file_citadel_control, "wb+");
152 if (control_fp != NULL) {
154 fchown(fileno(control_fp), config.c_ctdluid, -1);
155 memset(&CitControl, 0, sizeof(struct CitControl));
156 // Find highest room number and message number.
157 ForEachRoom(control_find_highest, NULL);
158 ForEachUser(control_find_user, NULL);
159 fwrite(&CitControl, sizeof(struct CitControl),
164 if (control_fp == NULL) {
165 lprintf(CTDL_ALERT, "ERROR opening %s: %s\n",
166 file_citadel_control,
172 fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
173 already_have_control = 1;
177 * put_control - write the control record to disk.
179 void put_control(void)
182 if (control_fp != NULL) {
184 fwrite(&CitControl, sizeof(struct CitControl), 1,
191 * release_control - close our fd on exit
193 void release_control(void)
195 if (control_fp != NULL)
201 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
203 long get_new_message_number(void)
206 begin_critical_section(S_CONTROL);
208 retval = ++CitControl.MMhighest;
210 end_critical_section(S_CONTROL);
216 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
218 long get_new_user_number(void)
221 begin_critical_section(S_CONTROL);
223 retval = ++CitControl.MMnextuser;
225 end_critical_section(S_CONTROL);
232 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
234 long get_new_room_number(void)
237 begin_critical_section(S_CONTROL);
239 retval = ++CitControl.MMnextroom;
241 end_critical_section(S_CONTROL);
248 * Get or set global configuration options
250 void cmd_conf(char *argbuf)
258 if (CtdlAccessCheck(ac_aide)) return;
260 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
261 if (!strcasecmp(cmd, "GET")) {
262 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
263 cprintf("%s\n", config.c_nodename);
264 cprintf("%s\n", config.c_fqdn);
265 cprintf("%s\n", config.c_humannode);
266 cprintf("%s\n", config.c_phonenum);
267 cprintf("%d\n", config.c_creataide);
268 cprintf("%d\n", config.c_sleeping);
269 cprintf("%d\n", config.c_initax);
270 cprintf("%d\n", config.c_regiscall);
271 cprintf("%d\n", config.c_twitdetect);
272 cprintf("%s\n", config.c_twitroom);
273 cprintf("%s\n", config.c_moreprompt);
274 cprintf("%d\n", config.c_restrict);
275 cprintf("%s\n", config.c_site_location);
276 cprintf("%s\n", config.c_sysadm);
277 cprintf("%d\n", config.c_maxsessions);
278 cprintf("xxx\n"); /* placeholder -- field no longer in use */
279 cprintf("%d\n", config.c_userpurge);
280 cprintf("%d\n", config.c_roompurge);
281 cprintf("%s\n", config.c_logpages);
282 cprintf("%d\n", config.c_createax);
283 cprintf("%ld\n", config.c_maxmsglen);
284 cprintf("%d\n", config.c_min_workers);
285 cprintf("%d\n", config.c_max_workers);
286 cprintf("%d\n", config.c_pop3_port);
287 cprintf("%d\n", config.c_smtp_port);
288 cprintf("%d\n", config.c_rfc822_strict_from);
289 cprintf("%d\n", config.c_aide_zap);
290 cprintf("%d\n", config.c_imap_port);
291 cprintf("%ld\n", config.c_net_freq);
292 cprintf("%d\n", config.c_disable_newu);
293 cprintf("1\n"); /* niu */
294 cprintf("%d\n", config.c_purge_hour);
296 cprintf("%s\n", config.c_ldap_host);
297 cprintf("%d\n", config.c_ldap_port);
298 cprintf("%s\n", config.c_ldap_base_dn);
299 cprintf("%s\n", config.c_ldap_bind_dn);
300 cprintf("%s\n", config.c_ldap_bind_pw);
308 cprintf("%s\n", config.c_ip_addr);
309 cprintf("%d\n", config.c_msa_port);
310 cprintf("%d\n", config.c_imaps_port);
311 cprintf("%d\n", config.c_pop3s_port);
312 cprintf("%d\n", config.c_smtps_port);
313 cprintf("%d\n", config.c_enable_fulltext);
314 cprintf("%d\n", config.c_auto_cull);
315 cprintf("%d\n", config.c_instant_expunge);
316 cprintf("%d\n", config.c_allow_spoofing);
317 cprintf("%d\n", config.c_journal_email);
318 cprintf("%d\n", config.c_journal_pubmsgs);
319 cprintf("%s\n", config.c_journal_dest);
320 cprintf("%s\n", config.c_default_cal_zone);
321 cprintf("%d\n", config.c_pftcpdict_port);
322 cprintf("%d\n", config.c_managesieve_port);
323 cprintf("%d\n", config.c_auth_mode);
324 cprintf("%s\n", config.c_funambol_host);
325 cprintf("%d\n", config.c_funambol_port);
326 cprintf("%s\n", config.c_funambol_source);
327 cprintf("%s\n", config.c_funambol_auth);
328 cprintf("%d\n", config.c_rbl_at_greeting);
329 cprintf("%s\n", config.c_master_user);
330 cprintf("%s\n", config.c_master_pass);
331 cprintf("%s\n", config.c_pager_program);
332 cprintf("%d\n", config.c_imap_keep_from);
336 else if (!strcasecmp(cmd, "SET")) {
338 cprintf("%d Send configuration...\n", SEND_LISTING);
340 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
343 safestrncpy(config.c_nodename, buf,
344 sizeof config.c_nodename);
347 safestrncpy(config.c_fqdn, buf,
348 sizeof config.c_fqdn);
351 safestrncpy(config.c_humannode, buf,
352 sizeof config.c_humannode);
355 safestrncpy(config.c_phonenum, buf,
356 sizeof config.c_phonenum);
359 config.c_creataide = atoi(buf);
362 config.c_sleeping = atoi(buf);
365 config.c_initax = atoi(buf);
366 if (config.c_initax < 1)
368 if (config.c_initax > 6)
372 config.c_regiscall = atoi(buf);
373 if (config.c_regiscall != 0)
374 config.c_regiscall = 1;
377 config.c_twitdetect = atoi(buf);
378 if (config.c_twitdetect != 0)
379 config.c_twitdetect = 1;
382 safestrncpy(config.c_twitroom, buf,
383 sizeof config.c_twitroom);
386 safestrncpy(config.c_moreprompt, buf,
387 sizeof config.c_moreprompt);
390 config.c_restrict = atoi(buf);
391 if (config.c_restrict != 0)
392 config.c_restrict = 1;
395 safestrncpy(config.c_site_location, buf,
396 sizeof config.c_site_location);
399 safestrncpy(config.c_sysadm, buf,
400 sizeof config.c_sysadm);
403 config.c_maxsessions = atoi(buf);
404 if (config.c_maxsessions < 0)
405 config.c_maxsessions = 0;
408 /* placeholder -- field no longer in use */
411 config.c_userpurge = atoi(buf);
414 config.c_roompurge = atoi(buf);
417 safestrncpy(config.c_logpages, buf,
418 sizeof config.c_logpages);
421 config.c_createax = atoi(buf);
422 if (config.c_createax < 1)
423 config.c_createax = 1;
424 if (config.c_createax > 6)
425 config.c_createax = 6;
428 if (atoi(buf) >= 8192)
429 config.c_maxmsglen = atoi(buf);
433 config.c_min_workers = atoi(buf);
435 if (atoi(buf) >= config.c_min_workers)
436 config.c_max_workers = atoi(buf);
438 config.c_pop3_port = atoi(buf);
441 config.c_smtp_port = atoi(buf);
444 config.c_rfc822_strict_from = atoi(buf);
447 config.c_aide_zap = atoi(buf);
448 if (config.c_aide_zap != 0)
449 config.c_aide_zap = 1;
452 config.c_imap_port = atoi(buf);
455 config.c_net_freq = atol(buf);
458 config.c_disable_newu = atoi(buf);
459 if (config.c_disable_newu != 0)
460 config.c_disable_newu = 1;
466 if ((config.c_purge_hour >= 0)
467 && (config.c_purge_hour <= 23)) {
468 config.c_purge_hour = atoi(buf);
473 safestrncpy(config.c_ldap_host, buf,
474 sizeof config.c_ldap_host);
477 config.c_ldap_port = atoi(buf);
480 safestrncpy(config.c_ldap_base_dn, buf,
481 sizeof config.c_ldap_base_dn);
484 safestrncpy(config.c_ldap_bind_dn, buf,
485 sizeof config.c_ldap_bind_dn);
488 safestrncpy(config.c_ldap_bind_pw, buf,
489 sizeof config.c_ldap_bind_pw);
493 safestrncpy(config.c_ip_addr, buf,
494 sizeof config.c_ip_addr);
496 config.c_msa_port = atoi(buf);
499 config.c_imaps_port = atoi(buf);
502 config.c_pop3s_port = atoi(buf);
505 config.c_smtps_port = atoi(buf);
508 config.c_enable_fulltext = atoi(buf);
511 config.c_auto_cull = atoi(buf);
514 config.c_instant_expunge = atoi(buf);
517 config.c_allow_spoofing = atoi(buf);
520 config.c_journal_email = atoi(buf);
523 config.c_journal_pubmsgs = atoi(buf);
526 safestrncpy(config.c_journal_dest, buf,
527 sizeof config.c_journal_dest);
529 safestrncpy(config.c_default_cal_zone, buf,
530 sizeof config.c_default_cal_zone);
533 config.c_pftcpdict_port = atoi(buf);
536 config.c_managesieve_port = atoi(buf);
539 config.c_auth_mode = atoi(buf);
541 safestrncpy(config.c_funambol_host, buf,
542 sizeof config.c_funambol_host);
545 config.c_funambol_port = atoi(buf);
548 safestrncpy(config.c_funambol_source,
550 sizeof config.c_funambol_source);
553 safestrncpy(config.c_funambol_auth,
555 sizeof config.c_funambol_auth);
558 config.c_rbl_at_greeting = atoi(buf);
561 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
564 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
567 safestrncpy(config.c_pager_program,
569 sizeof config.c_pager_program);
572 config.c_imap_keep_from = atoi(buf);
578 snprintf(buf, sizeof buf,
579 "The global system configuration has been edited by %s.\n",
581 aide_message(buf,"Citadel Configuration Manager Message");
583 if (!IsEmptyStr(config.c_logpages))
584 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
586 /* If full text indexing has been disabled, invalidate the
587 * index so it doesn't try to use it later.
589 if (config.c_enable_fulltext == 0) {
590 CitControl.fulltext_wordbreaker = 0;
595 else if (!strcasecmp(cmd, "GETSYS")) {
596 extract_token(confname, argbuf, 1, '|', sizeof confname);
597 confptr = CtdlGetSysConfig(confname);
598 if (confptr != NULL) {
599 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
600 client_write(confptr, strlen(confptr));
601 if (confptr[strlen(confptr) - 1] != 10)
602 client_write("\n", 1);
606 cprintf("%d No such configuration.\n",
607 ERROR + ILLEGAL_VALUE);
611 else if (!strcasecmp(cmd, "PUTSYS")) {
612 extract_token(confname, argbuf, 1, '|', sizeof confname);
614 cprintf("%d %s\n", SEND_LISTING, confname);
615 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
616 CtdlPutSysConfig(confname, confptr);
621 cprintf("%d Illegal option(s) specified.\n",
622 ERROR + ILLEGAL_VALUE);