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