* Silenced a couple more warn_unused_result warnings
[citadel.git] / citadel / config.c
1 /*
2  * $Id$
3  *
4  * Read and write the citadel.config file
5  *
6  * Copyright (c) 1987-2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <libcitadel.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include "config.h"
35
36
37 #include "ctdl_module.h"
38
39
40 struct config config;
41
42 /*
43  * get_config() is called during the initialization of any program which
44  * directly accesses Citadel data files.  It verifies the system's integrity
45  * and reads citadel.config into memory.
46  */
47 void get_config(void) {
48         FILE *cfp;
49         struct stat st;
50         int rv;
51
52         if (chdir(ctdl_bbsbase_dir) != 0) {
53                 fprintf(stderr,
54                         "This program could not be started.\n"
55                         "Unable to change directory to %s\n"
56                         "Error: %s\n",
57                         ctdl_bbsbase_dir,
58                         strerror(errno));
59                 exit(CTDLEXIT_HOME);
60         }
61         cfp = fopen(file_citadel_config, "rb");
62         if (cfp == NULL) {
63                 fprintf(stderr, "This program could not be started.\n"
64                                 "Unable to open %s\n"
65                                 "Error: %s\n",
66                                 file_citadel_config,
67                                 strerror(errno));
68                 exit(CTDLEXIT_CONFIG);
69         }
70         rv = fread((char *) &config, sizeof(struct config), 1, cfp);
71         if (fstat(fileno(cfp), &st)) {
72                 perror(file_citadel_config);
73                 exit(CTDLEXIT_CONFIG);
74         }
75
76 #ifndef __CYGWIN__
77         if (st.st_uid != CTDLUID) {
78                 fprintf(stderr, "%s must be owned by uid="F_UID_T" but "F_UID_T" owns it!\n", 
79                         file_citadel_config, CTDLUID, st.st_uid);
80                 exit(CTDLEXIT_CONFIG);
81         }
82         int desired_mode = (S_IFREG | S_IRUSR | S_IWUSR) ;
83         if (st.st_mode != desired_mode) {
84                 fprintf(stderr, "%s must be set to permissions mode %03o but they are %03o\n",
85                         file_citadel_config, (desired_mode & 0xFFF), (st.st_mode & 0xFFF));
86                 exit(CTDLEXIT_CONFIG);
87         }
88 #endif
89
90         fclose(cfp);
91
92         /* Ensure that we are linked to the correct version of libcitadel */
93         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
94                 fprintf(stderr, "    You are running libcitadel version %d.%02d\n",
95                         (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
96                 fprintf(stderr, "citserver was compiled against version %d.%02d\n",
97                         (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
98                 exit(CTDLEXIT_LIBCITADEL);
99         }
100
101         /* Only allow LDAP auth mode if we actually have LDAP support */
102 #ifndef HAVE_LDAP
103         if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
104                 fprintf(stderr, "Your system is configured for LDAP authentication,\n"
105                                 "but you are running a server built without OpenLDAP support.\n");
106                 exit(CTDL_EXIT_UNSUP_AUTH);
107         }
108 #endif
109
110         /* Check to see whether 'setup' must first be run to update data file formats */
111         if (config.c_setup_level < REV_MIN) {
112                 fprintf(stderr, "Your data files are out of date.  Run setup to update them.\n");
113                 fprintf(stderr, "        This program requires level %d.%02d\n",
114                         (REV_LEVEL / 100), (REV_LEVEL % 100));
115                 fprintf(stderr, "        Data files are currently at %d.%02d\n",
116                         (config.c_setup_level / 100),
117                         (config.c_setup_level % 100));
118                 exit(CTDLEXIT_OOD);
119         }
120
121         /* Default maximum message length is 10 megabytes.  This is site
122          * configurable.  Also check to make sure the limit has not been
123          * set below 8192 bytes.
124          */
125         if (config.c_maxmsglen <= 0)
126                 config.c_maxmsglen = 10485760;
127         if (config.c_maxmsglen < 8192)
128                 config.c_maxmsglen = 8192;
129
130         /* Default lower and upper limits on number of worker threads */
131
132         if (config.c_min_workers < 3)           /* no less than 3 */
133                 config.c_min_workers = 5;
134
135         if (config.c_max_workers == 0)                  /* default maximum */
136                 config.c_max_workers = 256;
137
138         if (config.c_max_workers < config.c_min_workers)   /* max >= min */
139                 config.c_max_workers = config.c_min_workers;
140
141         /* Networking more than once every five minutes just isn't sane */
142         if (config.c_net_freq == 0L)
143                 config.c_net_freq = 3600L;      /* once per hour default */
144         if (config.c_net_freq < 300L) 
145                 config.c_net_freq = 300L;
146
147         /* Same goes for POP3 */
148         if (config.c_pop3_fetch == 0L)
149                 config.c_pop3_fetch = 3600L;    /* once per hour default */
150         if (config.c_pop3_fetch < 300L) 
151                 config.c_pop3_fetch = 300L;
152         if (config.c_pop3_fastest == 0L)
153                 config.c_pop3_fastest = 3600L;  /* once per hour default */
154         if (config.c_pop3_fastest < 300L) 
155                 config.c_pop3_fastest = 300L;
156
157         /* "create new user" only works with native authentication mode */
158         if (config.c_auth_mode != AUTHMODE_NATIVE) {
159                 config.c_disable_newu = 1;
160         }
161 }
162
163
164 /*
165  * Occasionally, we will need to write the config file, because some operations
166  * change site-wide parameters.
167  */
168 void put_config(void)
169 {
170         FILE *cfp;
171         int rv;
172
173         if ((cfp = fopen(file_citadel_config, "rb+")) == NULL)
174                 perror(file_citadel_config);
175         else {
176                 rv = fwrite((char *) &config, sizeof(struct config), 1, cfp);
177                 fclose(cfp);
178         }
179 }