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