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>
35 #include "sysdep_decls.h"
39 #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);
335 else if (!strcasecmp(cmd, "SET")) {
337 cprintf("%d Send configuration...\n", SEND_LISTING);
339 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
342 safestrncpy(config.c_nodename, buf,
343 sizeof config.c_nodename);
346 safestrncpy(config.c_fqdn, buf,
347 sizeof config.c_fqdn);
350 safestrncpy(config.c_humannode, buf,
351 sizeof config.c_humannode);
354 safestrncpy(config.c_phonenum, buf,
355 sizeof config.c_phonenum);
358 config.c_creataide = atoi(buf);
361 config.c_sleeping = atoi(buf);
364 config.c_initax = atoi(buf);
365 if (config.c_initax < 1)
367 if (config.c_initax > 6)
371 config.c_regiscall = atoi(buf);
372 if (config.c_regiscall != 0)
373 config.c_regiscall = 1;
376 config.c_twitdetect = atoi(buf);
377 if (config.c_twitdetect != 0)
378 config.c_twitdetect = 1;
381 safestrncpy(config.c_twitroom, buf,
382 sizeof config.c_twitroom);
385 safestrncpy(config.c_moreprompt, buf,
386 sizeof config.c_moreprompt);
389 config.c_restrict = atoi(buf);
390 if (config.c_restrict != 0)
391 config.c_restrict = 1;
394 safestrncpy(config.c_site_location, buf,
395 sizeof config.c_site_location);
398 safestrncpy(config.c_sysadm, buf,
399 sizeof config.c_sysadm);
402 config.c_maxsessions = atoi(buf);
403 if (config.c_maxsessions < 0)
404 config.c_maxsessions = 0;
407 /* placeholder -- field no longer in use */
410 config.c_userpurge = atoi(buf);
413 config.c_roompurge = atoi(buf);
416 safestrncpy(config.c_logpages, buf,
417 sizeof config.c_logpages);
420 config.c_createax = atoi(buf);
421 if (config.c_createax < 1)
422 config.c_createax = 1;
423 if (config.c_createax > 6)
424 config.c_createax = 6;
427 if (atoi(buf) >= 8192)
428 config.c_maxmsglen = atoi(buf);
432 config.c_min_workers = atoi(buf);
434 if (atoi(buf) >= config.c_min_workers)
435 config.c_max_workers = atoi(buf);
437 config.c_pop3_port = atoi(buf);
440 config.c_smtp_port = atoi(buf);
443 config.c_rfc822_strict_from = atoi(buf);
446 config.c_aide_zap = atoi(buf);
447 if (config.c_aide_zap != 0)
448 config.c_aide_zap = 1;
451 config.c_imap_port = atoi(buf);
454 config.c_net_freq = atol(buf);
457 config.c_disable_newu = atoi(buf);
458 if (config.c_disable_newu != 0)
459 config.c_disable_newu = 1;
465 if ((config.c_purge_hour >= 0)
466 && (config.c_purge_hour <= 23)) {
467 config.c_purge_hour = atoi(buf);
472 safestrncpy(config.c_ldap_host, buf,
473 sizeof config.c_ldap_host);
476 config.c_ldap_port = atoi(buf);
479 safestrncpy(config.c_ldap_base_dn, buf,
480 sizeof config.c_ldap_base_dn);
483 safestrncpy(config.c_ldap_bind_dn, buf,
484 sizeof config.c_ldap_bind_dn);
487 safestrncpy(config.c_ldap_bind_pw, buf,
488 sizeof config.c_ldap_bind_pw);
492 safestrncpy(config.c_ip_addr, buf,
493 sizeof config.c_ip_addr);
495 config.c_msa_port = atoi(buf);
498 config.c_imaps_port = atoi(buf);
501 config.c_pop3s_port = atoi(buf);
504 config.c_smtps_port = atoi(buf);
507 config.c_enable_fulltext = atoi(buf);
510 config.c_auto_cull = atoi(buf);
513 config.c_instant_expunge = atoi(buf);
516 config.c_allow_spoofing = atoi(buf);
519 config.c_journal_email = atoi(buf);
522 config.c_journal_pubmsgs = atoi(buf);
525 safestrncpy(config.c_journal_dest, buf,
526 sizeof config.c_journal_dest);
528 safestrncpy(config.c_default_cal_zone, buf,
529 sizeof config.c_default_cal_zone);
532 config.c_pftcpdict_port = atoi(buf);
535 config.c_managesieve_port = atoi(buf);
538 config.c_auth_mode = atoi(buf);
540 safestrncpy(config.c_funambol_host, buf,
541 sizeof config.c_funambol_host);
544 config.c_funambol_port = atoi(buf);
547 safestrncpy(config.c_funambol_source,
549 sizeof config.c_funambol_source);
552 safestrncpy(config.c_funambol_auth,
554 sizeof config.c_funambol_auth);
557 config.c_rbl_at_greeting = atoi(buf);
560 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
563 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
566 safestrncpy(config.c_pager_program,
568 sizeof config.c_pager_program);
574 snprintf(buf, sizeof buf,
575 "The global system configuration has been edited by %s.\n",
577 aide_message(buf,"Citadel Configuration Manager Message");
579 if (!IsEmptyStr(config.c_logpages))
580 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
582 /* If full text indexing has been disabled, invalidate the
583 * index so it doesn't try to use it later.
585 if (config.c_enable_fulltext == 0) {
586 CitControl.fulltext_wordbreaker = 0;
591 else if (!strcasecmp(cmd, "GETSYS")) {
592 extract_token(confname, argbuf, 1, '|', sizeof confname);
593 confptr = CtdlGetSysConfig(confname);
594 if (confptr != NULL) {
595 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
596 client_write(confptr, strlen(confptr));
597 if (confptr[strlen(confptr) - 1] != 10)
598 client_write("\n", 1);
602 cprintf("%d No such configuration.\n",
603 ERROR + ILLEGAL_VALUE);
607 else if (!strcasecmp(cmd, "PUTSYS")) {
608 extract_token(confname, argbuf, 1, '|', sizeof confname);
610 cprintf("%d %s\n", SEND_LISTING, confname);
611 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
612 CtdlPutSysConfig(confname, confptr);
617 cprintf("%d Illegal option(s) specified.\n",
618 ERROR + ILLEGAL_VALUE);