c6565b35d1696538e80ab5a848a6a547784b4889
[citadel.git] / citadel / config.c
1 /*
2  * $Id$
3  *
4  * Read and write the citadel.config file
5  *
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 <libcitadel.h>
17 #include "citadel.h"
18 #include "server.h"
19 #include "config.h"
20
21 struct config config;
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(ctdl_bbsbase_dir) != 0) {
33                 fprintf(stderr,
34                         "This program could not be started.\n"
35                         "Unable to change directory to %s\n"
36                         "Error: %s\n",
37                         ctdl_bbsbase_dir,
38                         strerror(errno));
39                 exit(CTDLEXIT_HOME);
40         }
41         cfp = fopen(file_citadel_config, "rb");
42         if (cfp == NULL) {
43                 fprintf(stderr, "This program could not be started.\n"
44                                 "Unable to open %s\n"
45                                 "Error: %s\n",
46                                 file_citadel_config,
47                                 strerror(errno));
48                 exit(CTDLEXIT_CONFIG);
49         }
50         fread((char *) &config, sizeof(struct config), 1, cfp);
51         if (fstat(fileno(cfp), &st)) {
52                 perror(file_citadel_config);
53                 exit(CTDLEXIT_CONFIG);
54         }
55
56 #ifndef __CYGWIN__
57         if (st.st_uid != CTDLUID) {
58                 fprintf(stderr, "%s must be owned by uid="F_UID_T" but "F_UID_T" owns it!\n", 
59                         file_citadel_config, CTDLUID, st.st_uid);
60                 exit(CTDLEXIT_CONFIG);
61         }
62         int desired_mode = (S_IFREG | S_IRUSR | S_IWUSR) ;
63         if (st.st_mode != desired_mode) {
64                 fprintf(stderr, "%s must be set to permissions mode %03o but they are %03o\n",
65                         file_citadel_config, (desired_mode & 0xFFF), (st.st_mode & 0xFFF));
66                 exit(CTDLEXIT_CONFIG);
67         }
68 #endif
69
70         fclose(cfp);
71
72         /* Ensure that we are linked to the correct version of libcitadel */
73         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
74                 fprintf(stderr, "    You are running libcitadel version %d.%02d\n",
75                         (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
76                 fprintf(stderr, "citserver was compiled against version %d.%02d\n",
77                         (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
78                 exit(CTDLEXIT_LIBCITADEL);
79         }
80
81         /* Only allow LDAP auth mode if we actually have LDAP support */
82 #ifndef HAVE_LDAP
83         if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
84                 fprintf(stderr, "Your system is configured for LDAP authentication,\n"
85                                 "but you are running a server built without OpenLDAP support.\n");
86                 exit(CTDL_EXIT_UNSUP_AUTH);
87         }
88 #endif
89
90         /* Check to see whether 'setup' must first be run to update data file formats */
91         if (config.c_setup_level < REV_MIN) {
92                 fprintf(stderr, "Your data files are out of date.  Run setup to update them.\n");
93                 fprintf(stderr, "        This program requires level %d.%02d\n",
94                         (REV_LEVEL / 100), (REV_LEVEL % 100));
95                 fprintf(stderr, "        Data files are currently at %d.%02d\n",
96                         (config.c_setup_level / 100),
97                         (config.c_setup_level % 100));
98                 exit(CTDLEXIT_OOD);
99         }
100
101         /* Default maximum message length is 10 megabytes.  This is site
102          * configurable.  Also check to make sure the limit has not been
103          * set below 8192 bytes.
104          */
105         if (config.c_maxmsglen <= 0)
106                 config.c_maxmsglen = 10485760;
107         if (config.c_maxmsglen < 8192)
108                 config.c_maxmsglen = 8192;
109
110         /* Default lower and upper limits on number of worker threads */
111
112         if (config.c_min_workers < 3)           /* no less than 3 */
113                 config.c_min_workers = 5;
114
115         if (config.c_max_workers == 0)                  /* default maximum */
116                 config.c_max_workers = 256;
117
118         if (config.c_max_workers < config.c_min_workers)   /* max >= min */
119                 config.c_max_workers = config.c_min_workers;
120
121         /* Networking more than once every five minutes just isn't sane */
122         if (config.c_net_freq == 0L)
123                 config.c_net_freq = 3600L;      /* once per hour default */
124         if (config.c_net_freq < 300L) 
125                 config.c_net_freq = 300L;
126
127         /* Same goes for POP3 */
128         if (config.c_pop3_fetch == 0L)
129                 config.c_pop3_fetch = 3600L;    /* once per hour default */
130         if (config.c_pop3_fetch < 300L) 
131                 config.c_pop3_fetch = 300L;
132         if (config.c_pop3_fastest == 0L)
133                 config.c_pop3_fastest = 3600L;  /* once per hour default */
134         if (config.c_pop3_fastest < 300L) 
135                 config.c_pop3_fastest = 300L;
136
137         /* "create new user" only works with native authentication mode */
138         if (config.c_auth_mode != AUTHMODE_NATIVE) {
139                 config.c_disable_newu = 1;
140         }
141 }
142
143
144 /*
145  * Occasionally, we will need to write the config file, because some operations
146  * change site-wide parameters.
147  */
148 void put_config(void)
149 {
150         FILE *cfp;
151
152         if ((cfp = fopen(file_citadel_config, "rb+")) == NULL)
153                 perror(file_citadel_config);
154         else {
155                 fwrite((char *) &config, sizeof(struct config), 1, cfp);
156                 fclose(cfp);
157         }
158 }