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