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