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