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