* Applied a patch sent in by Wilfried Goesgens which allows the various
[citadel.git] / citadel / config.c
1 /*
2  * $Id$
3  *
4  * This function reads the citadel.config file.  It should be called at
5  * the beginning of EVERY Citadel program.
6  *
7  */
8
9 #ifdef DLL_EXPORT
10 #define IN_LIBCIT
11 #endif
12
13 #include "sysdep.h"
14 #include <stdlib.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <limits.h>
21 #include "citadel.h"
22 #include "server.h"
23 #include "serv_extensions.h"
24 #include "config.h"
25
26 struct config config;
27 char ctdl_home_directory[PATH_MAX] = CTDLDIR;
28 int home_specified = 0;
29
30 /*
31  * get_config() is called during the initialization of any program which
32  * directly accesses Citadel data files.  It verifies the system's integrity
33  * and reads citadel.config into memory.
34  */
35 void get_config(void) {
36         FILE *cfp;
37         struct stat st;
38
39         if (chdir(home_specified ? ctdl_home_directory : CTDLDIR) != 0) {
40                 fprintf(stderr,
41                         "This program could not be started.\n"
42                         "Unable to change directory to %s\n"
43                         "Error: %s\n",
44                         (home_specified ? ctdl_home_directory : CTDLDIR),
45                         strerror(errno));
46                 exit(1);
47         }
48         cfp = fopen(
49 #ifndef HAVE_ETC_DIR
50                                 "."
51 #else
52                                 ETC_DIR
53 #endif
54                                 "/citadel.config", "rb");
55         if (cfp == NULL) {
56                 fprintf(stderr, "This program could not be started.\n"
57                         "Unable to open %s/citadel.config\n"
58                         "Error: %s\n",
59                         (home_specified ? ctdl_home_directory : CTDLDIR),
60                         strerror(errno));
61                 exit(1);
62         }
63         fread((char *) &config, sizeof(struct config), 1, cfp);
64         if (fstat(fileno(cfp), &st)) {
65                 perror("citadel.config");
66                 exit(1);
67         }
68 #ifndef __CYGWIN__
69         if (st.st_uid != CTDLUID || st.st_mode != (S_IFREG | S_IRUSR | S_IWUSR)) {
70                 fprintf(stderr, "check the permissions on citadel.config\n");
71                 exit(1);
72         }
73 #endif
74         fclose(cfp);
75
76         if (config.c_setup_level < REV_MIN) {
77                 fprintf(stderr, "config: Your data files are out of date.  ");
78                 fprintf(stderr, "Run setup to update them.\n");
79                 fprintf(stderr,
80                         "        This program requires level %d.%02d\n",
81                         (REV_LEVEL / 100), (REV_LEVEL % 100));
82                 fprintf(stderr,
83                         "        Data files are currently at %d.%02d\n",
84                         (config.c_setup_level / 100),
85                         (config.c_setup_level % 100));
86                 exit(1);
87         }
88
89         /* Default maximum message length is 10 megabytes.  This is site
90          * configurable.  Also check to make sure the limit has not been
91          * set below 8192 bytes.
92          */
93         if (config.c_maxmsglen <= 0)
94                 config.c_maxmsglen = 10485760;
95         if (config.c_maxmsglen < 8192)
96                 config.c_maxmsglen = 8192;
97
98         /* Default lower and upper limits on number of worker threads */
99
100         if (config.c_min_workers < 3)           /* no less than 3 */
101                 config.c_min_workers = 5;
102
103         if (config.c_max_workers == 0)                  /* default maximum */
104                 config.c_max_workers = 256;
105
106         if (config.c_max_workers < config.c_min_workers)   /* max >= min */
107                 config.c_max_workers = config.c_min_workers;
108
109         /* Networking more than once every five minutes just isn't sane */
110         if (config.c_net_freq == 0L)
111                 config.c_net_freq = 3600L;      /* once per hour default */
112         if (config.c_net_freq < 300L) 
113                 config.c_net_freq = 300L;
114 }
115
116
117 /*
118  * Occasionally, we will need to write the config file, because some operations
119  * change site-wide parameters.
120  */
121 void put_config(void)
122 {
123         FILE *cfp;
124
125         if ((cfp = fopen(
126 #ifndef HAVE_ETC_DIR
127                                          "."
128 #else
129                                          ETC_DIR
130 #endif
131                                          "/citadel.config", "rb+")) == NULL)
132                 perror("citadel.config");
133         else {
134                 fwrite((char *) &config, sizeof(struct config), 1, cfp);
135                 fclose(cfp);
136         }
137 }