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