* Removed some temporary diagnostics that had been left in serv_sieve.c
[citadel.git] / citadel / control.c
blob305ed3d2332c2f0ee2b1a9b6b459ec452b8731ca
1 /*
2 * $Id$
4 * This module handles states which are global to the entire server.
6 */
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
15 #if TIME_WITH_SYS_TIME
16 # include <sys/time.h>
17 # include <time.h>
18 #else
19 # if HAVE_SYS_TIME_H
20 # include <sys/time.h>
21 # else
22 # include <time.h>
23 # endif
24 #endif
26 #include <ctype.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <sys/file.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "control.h"
36 #include "sysdep_decls.h"
37 #include "support.h"
38 #include "config.h"
39 #include "msgbase.h"
40 #include "citserver.h"
41 #include "room_ops.h"
42 #include "user_ops.h"
43 #include "database.h"
44 #include "threads.h"
46 #ifndef HAVE_SNPRINTF
47 #include "snprintf.h"
48 #endif
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)
62 #ifdef HAVE_FLOCK
64 * TODO: solaris manpages describe this function, but the headers
65 * don't show it!
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);
73 #endif
77 * callback to get highest room number when rebuilding control file
79 void control_find_highest(struct ctdlroom *qrbuf, void *data)
81 struct ctdlroom room;
82 struct cdbdata *cdbfr;
83 long *msglist;
84 int num_msgs=0;
85 int c;
86 int room_fixed = 0;
87 int message_fixed = 0;
89 if (qrbuf->QRnumber > CitControl.MMnextroom)
91 CitControl.MMnextroom = qrbuf->QRnumber;
92 room_fixed = 1;
95 getroom (&room, qrbuf->QRname);
97 /* Load the message list */
98 cdbfr = cdb_fetch(CDB_MSGLISTS, &room.QRnumber, sizeof(long));
99 if (cdbfr != NULL) {
100 msglist = (long *) cdbfr->ptr;
101 num_msgs = cdbfr->len / sizeof(long);
102 } else {
103 return; /* No messages at all? No further action. */
106 if (num_msgs>0)
108 for (c=0; c<num_msgs; c++)
110 if (msglist[c] > CitControl.MMhighest)
112 CitControl.MMhighest = msglist[c];
113 message_fixed = 1;
117 cdb_free(cdbfr);
118 if (room_fixed)
119 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed room counter\n");
120 if (message_fixed)
121 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed message count\n");
122 return;
127 * Callback to get highest user number.
130 void control_find_user (struct ctdluser *EachUser, void *out_data)
132 int user_fixed = 0;
134 if (EachUser->usernum > CitControl.MMnextuser)
136 CitControl.MMnextuser = EachUser->usernum;
137 user_fixed = 1;
139 if(user_fixed)
140 CtdlLogPrintf(CTDL_INFO, "Control record checking....Fixed user count\n");
145 * get_control - read the control record into memory.
147 void get_control(void)
149 static int already_have_control = 0;
152 * If we already have the control record in memory, there's no point
153 * in reading it from disk again.
155 if (already_have_control) return;
157 /* Zero it out. If the control record on disk is missing or short,
158 * the system functions with all control record fields initialized
159 * to zero.
161 memset(&CitControl, 0, sizeof(struct CitControl));
162 if (control_fp == NULL) {
163 control_fp = fopen(file_citadel_control, "rb+");
164 if (control_fp != NULL) {
165 lock_control();
166 fchown(fileno(control_fp), config.c_ctdluid, -1);
169 if (control_fp == NULL) {
170 control_fp = fopen(file_citadel_control, "wb+");
171 if (control_fp != NULL) {
172 lock_control();
173 fchown(fileno(control_fp), config.c_ctdluid, -1);
174 memset(&CitControl, 0, sizeof(struct CitControl));
175 fwrite(&CitControl, sizeof(struct CitControl),
176 1, control_fp);
177 rewind(control_fp);
180 if (control_fp == NULL) {
181 CtdlLogPrintf(CTDL_ALERT, "ERROR opening %s: %s\n",
182 file_citadel_control,
183 strerror(errno));
184 return;
187 rewind(control_fp);
188 fread(&CitControl, sizeof(struct CitControl), 1, control_fp);
189 already_have_control = 1;
190 chown(file_citadel_control, config.c_ctdluid, (-1));
194 * put_control - write the control record to disk.
196 void put_control(void)
199 if (control_fp != NULL) {
200 rewind(control_fp);
201 fwrite(&CitControl, sizeof(struct CitControl), 1,
202 control_fp);
203 fflush(control_fp);
209 * check_control - check the control record has sensible values for message, user and room numbers
211 void check_control(void)
213 CtdlLogPrintf(CTDL_INFO, "Checking/re-building control record\n");
214 get_control();
215 // Find highest room number and message number.
216 ForEachRoom(control_find_highest, NULL);
217 ForEachUser(control_find_user, NULL);
218 put_control();
223 * release_control - close our fd on exit
225 void release_control(void)
227 if (control_fp != NULL)
228 fclose(control_fp);
229 control_fp = NULL;
233 * get_new_message_number() - Obtain a new, unique ID to be used for a message.
235 long get_new_message_number(void)
237 long retval = 0L;
238 begin_critical_section(S_CONTROL);
239 get_control();
240 retval = ++CitControl.MMhighest;
241 put_control();
242 end_critical_section(S_CONTROL);
243 return(retval);
248 * CtdlGetCurrentMessageNumber() - Obtain the current highest message number in the system
249 * This provides a quick way to initialise a variable that might be used to indicate
250 * messages that should not be processed. EG. a new Sieve script will use this
251 * to record determine that messages older than this should not be processed.
253 long CtdlGetCurrentMessageNumber(void)
255 long retval = 0L;
256 begin_critical_section(S_CONTROL);
257 get_control();
258 retval = CitControl.MMhighest;
259 end_critical_section(S_CONTROL);
260 return(retval);
265 * get_new_user_number() - Obtain a new, unique ID to be used for a user.
267 long get_new_user_number(void)
269 long retval = 0L;
270 begin_critical_section(S_CONTROL);
271 get_control();
272 retval = ++CitControl.MMnextuser;
273 put_control();
274 end_critical_section(S_CONTROL);
275 return(retval);
281 * get_new_room_number() - Obtain a new, unique ID to be used for a room.
283 long get_new_room_number(void)
285 long retval = 0L;
286 begin_critical_section(S_CONTROL);
287 get_control();
288 retval = ++CitControl.MMnextroom;
289 put_control();
290 end_critical_section(S_CONTROL);
291 return(retval);
297 * Get or set global configuration options
299 void cmd_conf(char *argbuf)
301 char cmd[16];
302 char buf[256];
303 int a;
304 char *confptr;
305 char confname[128];
307 if (CtdlAccessCheck(ac_aide)) return;
309 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
310 if (!strcasecmp(cmd, "GET")) {
311 cprintf("%d Configuration...\n", LISTING_FOLLOWS);
312 cprintf("%s\n", config.c_nodename);
313 cprintf("%s\n", config.c_fqdn);
314 cprintf("%s\n", config.c_humannode);
315 cprintf("%s\n", config.c_phonenum);
316 cprintf("%d\n", config.c_creataide);
317 cprintf("%d\n", config.c_sleeping);
318 cprintf("%d\n", config.c_initax);
319 cprintf("%d\n", config.c_regiscall);
320 cprintf("%d\n", config.c_twitdetect);
321 cprintf("%s\n", config.c_twitroom);
322 cprintf("%s\n", config.c_moreprompt);
323 cprintf("%d\n", config.c_restrict);
324 cprintf("%s\n", config.c_site_location);
325 cprintf("%s\n", config.c_sysadm);
326 cprintf("%d\n", config.c_maxsessions);
327 cprintf("xxx\n"); /* placeholder -- field no longer in use */
328 cprintf("%d\n", config.c_userpurge);
329 cprintf("%d\n", config.c_roompurge);
330 cprintf("%s\n", config.c_logpages);
331 cprintf("%d\n", config.c_createax);
332 cprintf("%ld\n", config.c_maxmsglen);
333 cprintf("%d\n", config.c_min_workers);
334 cprintf("%d\n", config.c_max_workers);
335 cprintf("%d\n", config.c_pop3_port);
336 cprintf("%d\n", config.c_smtp_port);
337 cprintf("%d\n", config.c_rfc822_strict_from);
338 cprintf("%d\n", config.c_aide_zap);
339 cprintf("%d\n", config.c_imap_port);
340 cprintf("%ld\n", config.c_net_freq);
341 cprintf("%d\n", config.c_disable_newu);
342 cprintf("1\n"); /* niu */
343 cprintf("%d\n", config.c_purge_hour);
344 #ifdef HAVE_LDAP
345 cprintf("%s\n", config.c_ldap_host);
346 cprintf("%d\n", config.c_ldap_port);
347 cprintf("%s\n", config.c_ldap_base_dn);
348 cprintf("%s\n", config.c_ldap_bind_dn);
349 cprintf("%s\n", config.c_ldap_bind_pw);
350 #else
351 cprintf("\n");
352 cprintf("0\n");
353 cprintf("\n");
354 cprintf("\n");
355 cprintf("\n");
356 #endif
357 cprintf("%s\n", config.c_ip_addr);
358 cprintf("%d\n", config.c_msa_port);
359 cprintf("%d\n", config.c_imaps_port);
360 cprintf("%d\n", config.c_pop3s_port);
361 cprintf("%d\n", config.c_smtps_port);
362 cprintf("%d\n", config.c_enable_fulltext);
363 cprintf("%d\n", config.c_auto_cull);
364 cprintf("%d\n", config.c_instant_expunge);
365 cprintf("%d\n", config.c_allow_spoofing);
366 cprintf("%d\n", config.c_journal_email);
367 cprintf("%d\n", config.c_journal_pubmsgs);
368 cprintf("%s\n", config.c_journal_dest);
369 cprintf("%s\n", config.c_default_cal_zone);
370 cprintf("%d\n", config.c_pftcpdict_port);
371 cprintf("%d\n", config.c_managesieve_port);
372 cprintf("%d\n", config.c_auth_mode);
373 cprintf("%s\n", config.c_funambol_host);
374 cprintf("%d\n", config.c_funambol_port);
375 cprintf("%s\n", config.c_funambol_source);
376 cprintf("%s\n", config.c_funambol_auth);
377 cprintf("%d\n", config.c_rbl_at_greeting);
378 cprintf("%s\n", config.c_master_user);
379 cprintf("%s\n", config.c_master_pass);
380 cprintf("%s\n", config.c_pager_program);
381 cprintf("%d\n", config.c_imap_keep_from);
382 cprintf("%d\n", config.c_xmpp_c2s_port);
383 cprintf("%d\n", config.c_xmpp_s2s_port);
384 cprintf("%ld\n", config.c_pop3_fetch);
385 cprintf("%ld\n", config.c_pop3_fastest);
386 cprintf("%d\n", config.c_spam_flag_only);
387 cprintf("000\n");
390 else if (!strcasecmp(cmd, "SET")) {
391 unbuffer_output();
392 cprintf("%d Send configuration...\n", SEND_LISTING);
393 a = 0;
394 while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
395 switch (a) {
396 case 0:
397 safestrncpy(config.c_nodename, buf,
398 sizeof config.c_nodename);
399 break;
400 case 1:
401 safestrncpy(config.c_fqdn, buf,
402 sizeof config.c_fqdn);
403 break;
404 case 2:
405 safestrncpy(config.c_humannode, buf,
406 sizeof config.c_humannode);
407 break;
408 case 3:
409 safestrncpy(config.c_phonenum, buf,
410 sizeof config.c_phonenum);
411 break;
412 case 4:
413 config.c_creataide = atoi(buf);
414 break;
415 case 5:
416 config.c_sleeping = atoi(buf);
417 break;
418 case 6:
419 config.c_initax = atoi(buf);
420 if (config.c_initax < 1)
421 config.c_initax = 1;
422 if (config.c_initax > 6)
423 config.c_initax = 6;
424 break;
425 case 7:
426 config.c_regiscall = atoi(buf);
427 if (config.c_regiscall != 0)
428 config.c_regiscall = 1;
429 break;
430 case 8:
431 config.c_twitdetect = atoi(buf);
432 if (config.c_twitdetect != 0)
433 config.c_twitdetect = 1;
434 break;
435 case 9:
436 safestrncpy(config.c_twitroom, buf,
437 sizeof config.c_twitroom);
438 break;
439 case 10:
440 safestrncpy(config.c_moreprompt, buf,
441 sizeof config.c_moreprompt);
442 break;
443 case 11:
444 config.c_restrict = atoi(buf);
445 if (config.c_restrict != 0)
446 config.c_restrict = 1;
447 break;
448 case 12:
449 safestrncpy(config.c_site_location, buf,
450 sizeof config.c_site_location);
451 break;
452 case 13:
453 safestrncpy(config.c_sysadm, buf,
454 sizeof config.c_sysadm);
455 break;
456 case 14:
457 config.c_maxsessions = atoi(buf);
458 if (config.c_maxsessions < 0)
459 config.c_maxsessions = 0;
460 break;
461 case 15:
462 /* placeholder -- field no longer in use */
463 break;
464 case 16:
465 config.c_userpurge = atoi(buf);
466 break;
467 case 17:
468 config.c_roompurge = atoi(buf);
469 break;
470 case 18:
471 safestrncpy(config.c_logpages, buf,
472 sizeof config.c_logpages);
473 break;
474 case 19:
475 config.c_createax = atoi(buf);
476 if (config.c_createax < 1)
477 config.c_createax = 1;
478 if (config.c_createax > 6)
479 config.c_createax = 6;
480 break;
481 case 20:
482 if (atoi(buf) >= 8192)
483 config.c_maxmsglen = atoi(buf);
484 break;
485 case 21:
486 if (atoi(buf) >= 2)
487 config.c_min_workers = atoi(buf);
488 case 22:
489 if (atoi(buf) >= config.c_min_workers)
490 config.c_max_workers = atoi(buf);
491 case 23:
492 config.c_pop3_port = atoi(buf);
493 break;
494 case 24:
495 config.c_smtp_port = atoi(buf);
496 break;
497 case 25:
498 config.c_rfc822_strict_from = atoi(buf);
499 break;
500 case 26:
501 config.c_aide_zap = atoi(buf);
502 if (config.c_aide_zap != 0)
503 config.c_aide_zap = 1;
504 break;
505 case 27:
506 config.c_imap_port = atoi(buf);
507 break;
508 case 28:
509 config.c_net_freq = atol(buf);
510 break;
511 case 29:
512 config.c_disable_newu = atoi(buf);
513 if (config.c_disable_newu != 0)
514 config.c_disable_newu = 1;
515 break;
516 case 30:
517 /* niu */
518 break;
519 case 31:
520 if ((config.c_purge_hour >= 0)
521 && (config.c_purge_hour <= 23)) {
522 config.c_purge_hour = atoi(buf);
524 break;
525 #ifdef HAVE_LDAP
526 case 32:
527 safestrncpy(config.c_ldap_host, buf,
528 sizeof config.c_ldap_host);
529 break;
530 case 33:
531 config.c_ldap_port = atoi(buf);
532 break;
533 case 34:
534 safestrncpy(config.c_ldap_base_dn, buf,
535 sizeof config.c_ldap_base_dn);
536 break;
537 case 35:
538 safestrncpy(config.c_ldap_bind_dn, buf,
539 sizeof config.c_ldap_bind_dn);
540 break;
541 case 36:
542 safestrncpy(config.c_ldap_bind_pw, buf,
543 sizeof config.c_ldap_bind_pw);
544 break;
545 #endif
546 case 37:
547 safestrncpy(config.c_ip_addr, buf,
548 sizeof config.c_ip_addr);
549 case 38:
550 config.c_msa_port = atoi(buf);
551 break;
552 case 39:
553 config.c_imaps_port = atoi(buf);
554 break;
555 case 40:
556 config.c_pop3s_port = atoi(buf);
557 break;
558 case 41:
559 config.c_smtps_port = atoi(buf);
560 break;
561 case 42:
562 config.c_enable_fulltext = atoi(buf);
563 break;
564 case 43:
565 config.c_auto_cull = atoi(buf);
566 break;
567 case 44:
568 config.c_instant_expunge = atoi(buf);
569 break;
570 case 45:
571 config.c_allow_spoofing = atoi(buf);
572 break;
573 case 46:
574 config.c_journal_email = atoi(buf);
575 break;
576 case 47:
577 config.c_journal_pubmsgs = atoi(buf);
578 break;
579 case 48:
580 safestrncpy(config.c_journal_dest, buf,
581 sizeof config.c_journal_dest);
582 case 49:
583 safestrncpy(config.c_default_cal_zone, buf,
584 sizeof config.c_default_cal_zone);
585 break;
586 case 50:
587 config.c_pftcpdict_port = atoi(buf);
588 break;
589 case 51:
590 config.c_managesieve_port = atoi(buf);
591 break;
592 case 52:
593 config.c_auth_mode = atoi(buf);
594 case 53:
595 safestrncpy(config.c_funambol_host, buf,
596 sizeof config.c_funambol_host);
597 break;
598 case 54:
599 config.c_funambol_port = atoi(buf);
600 break;
601 case 55:
602 safestrncpy(config.c_funambol_source,
603 buf,
604 sizeof config.c_funambol_source);
605 break;
606 case 56:
607 safestrncpy(config.c_funambol_auth,
608 buf,
609 sizeof config.c_funambol_auth);
610 break;
611 case 57:
612 config.c_rbl_at_greeting = atoi(buf);
613 break;
614 case 58:
615 safestrncpy(config.c_master_user, buf, sizeof config.c_master_user);
616 break;
617 case 59:
618 safestrncpy(config.c_master_pass, buf, sizeof config.c_master_pass);
619 break;
620 case 60:
621 safestrncpy(config.c_pager_program,
622 buf,
623 sizeof config.c_pager_program);
624 break;
625 case 61:
626 config.c_imap_keep_from = atoi(buf);
627 break;
628 case 62:
629 config.c_xmpp_c2s_port = atoi(buf);
630 break;
631 case 63:
632 config.c_xmpp_s2s_port = atoi(buf);
633 break;
634 case 64:
635 config.c_pop3_fetch = atol(buf);
636 break;
637 case 65:
638 config.c_pop3_fastest = atol(buf);
639 break;
640 case 66:
641 config.c_spam_flag_only = atoi(buf);
642 break;
644 ++a;
646 put_config();
647 snprintf(buf, sizeof buf,
648 "The global system configuration has been edited by %s.\n",
649 CC->curr_user);
650 aide_message(buf,"Citadel Configuration Manager Message");
652 if (!IsEmptyStr(config.c_logpages))
653 create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
655 /* If full text indexing has been disabled, invalidate the
656 * index so it doesn't try to use it later.
658 if (config.c_enable_fulltext == 0) {
659 CitControl.fulltext_wordbreaker = 0;
660 put_control();
664 else if (!strcasecmp(cmd, "GETSYS")) {
665 extract_token(confname, argbuf, 1, '|', sizeof confname);
666 confptr = CtdlGetSysConfig(confname);
667 if (confptr != NULL) {
668 cprintf("%d %s\n", LISTING_FOLLOWS, confname);
669 client_write(confptr, strlen(confptr));
670 if (confptr[strlen(confptr) - 1] != 10)
671 client_write("\n", 1);
672 cprintf("000\n");
673 free(confptr);
674 } else {
675 cprintf("%d No such configuration.\n",
676 ERROR + ILLEGAL_VALUE);
680 else if (!strcasecmp(cmd, "PUTSYS")) {
681 extract_token(confname, argbuf, 1, '|', sizeof confname);
682 unbuffer_output();
683 cprintf("%d %s\n", SEND_LISTING, confname);
684 confptr = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
685 CtdlPutSysConfig(confname, confptr);
686 free(confptr);
689 else {
690 cprintf("%d Illegal option(s) specified.\n",
691 ERROR + ILLEGAL_VALUE);