* Actually _enforce_ the max msg len limit
[citadel.git] / citadel / config.c
1 /*
2  * This function reads the citadel.config file.  It should be called at
3  * the beginning of EVERY Citadel program.
4  *
5  * $Id$
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <errno.h>
14 #include <string.h>
15 #include <limits.h>
16 #include "citadel.h"
17 #include "config.h"
18
19 struct config config;
20 char bbs_home_directory[PATH_MAX] = BBSDIR;
21 int home_specified = 0;
22
23 /*
24  * get_config() is called during the initialization of any program which
25  * directly accesses Citadel data files.  It verifies the system's integrity
26  * and reads citadel.config into memory.
27  */
28 void get_config(void) {
29         FILE *cfp;
30         struct stat st;
31
32         if (chdir(home_specified ? bbs_home_directory : BBSDIR) != 0) {
33                 fprintf(stderr,
34                  "Cannot start.\nThere is no Citadel installation in %s\n%s\n",
35                         (home_specified ? bbs_home_directory : BBSDIR),
36                         strerror(errno));
37                 exit(1);
38         }
39         cfp = fopen("citadel.config", "rb");
40         if (cfp == NULL) {
41                 fprintf(stderr, "Cannot start.\n");
42                 fprintf(stderr, "There is no citadel.config in %s\n%s\n",
43                         (home_specified ? bbs_home_directory : BBSDIR),
44                         strerror(errno));
45                 exit(1);
46         }
47         fread((char *) &config, sizeof(struct config), 1, cfp);
48         if (fstat(fileno(cfp), &st)) {
49                 perror("citadel.config");
50                 exit(1);
51         }
52         if (st.st_uid != BBSUID || st.st_mode != (S_IFREG | S_IRUSR | S_IWUSR)) {
53                 fprintf(stderr, "check the permissions on citadel.config\n");
54                 exit(1);
55         }
56         fclose(cfp);
57
58         if (config.c_setup_level != REV_LEVEL) {
59                 fprintf(stderr, "config: Your data files are out of date.  ");
60                 fprintf(stderr, "Run setup to update them.\n");
61                 fprintf(stderr,
62                         "        This program requires level %d.%02d\n",
63                         (REV_LEVEL / 100), (REV_LEVEL % 100));
64                 fprintf(stderr,
65                         "        Data files are currently at %d.%02d\n",
66                         (config.c_setup_level / 100),
67                         (config.c_setup_level % 100));
68                 exit(1);
69         }
70
71         /* Default maximum message length is 'unlimited' (max int)
72          * and the minimum is 8192
73          */
74         if (config.c_maxmsglen <= 0)
75                 config.c_maxmsglen = INT_MAX;
76         if (config.c_maxmsglen < 8192)
77                 config.c_maxmsglen = 8192;
78                 
79 }
80
81
82 /*
83  * Occasionally, we will need to write the config file, because some operations
84  * change site-wide parameters.
85  */
86 void put_config(void)
87 {
88         FILE *cfp;
89
90         if ((cfp = fopen("citadel.config", "rb+")) == NULL)
91                 perror("citadel.config");
92         else {
93                 fwrite((char *) &config, sizeof(struct config), 1, cfp);
94                 fclose(cfp);
95         }
96 }