More sane behavior on a new installation
[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 <sys/utsname.h>
24 #include <libcitadel.h>
25 #include "citadel.h"
26 #include "server.h"
27 #include "config.h"
28 #include "ctdl_module.h"
29
30 struct config config;
31
32 /*
33  * Put some sane default values into our configuration.  Some will be overridden when we run setup.
34  */
35 void brand_new_installation_set_defaults(void) {
36
37         struct passwd *pw;
38         struct utsname my_utsname;
39         struct hostent *he;
40
41         /* Determine our host name, in case we need to use it as a default */
42         uname(&my_utsname);
43
44         /* set some sample/default values in place of blanks... */
45         char c_nodename[256];
46         safestrncpy(c_nodename, my_utsname.nodename, sizeof c_nodename);
47         strtok(config.c_nodename, ".");
48         if (IsEmptyStr(config.c_fqdn) ) {
49                 if ((he = gethostbyname(my_utsname.nodename)) != NULL) {
50                         safestrncpy(config.c_fqdn, he->h_name, sizeof config.c_fqdn);
51                 }
52                 else {
53                         safestrncpy(config.c_fqdn, my_utsname.nodename, sizeof config.c_fqdn);
54                 }
55         }
56
57         safestrncpy(config.c_humannode, "Citadel Server", sizeof config.c_humannode);
58         safestrncpy(config.c_phonenum, "US 800 555 1212", sizeof config.c_phonenum);
59         config.c_initax = 4;
60         safestrncpy(config.c_moreprompt, "<more>", sizeof config.c_moreprompt);
61         safestrncpy(config.c_twitroom, "Trashcan", sizeof config.c_twitroom);
62         safestrncpy(config.c_baseroom, BASEROOM, sizeof config.c_baseroom);
63         safestrncpy(config.c_aideroom, "Aide", sizeof config.c_aideroom);
64         config.c_port_number = 504;
65         config.c_sleeping = 900;
66         config.c_instant_expunge = 1;
67
68         if (config.c_ctdluid == 0) {
69                 pw = getpwnam("citadel");
70                 if (pw != NULL) {
71                         config.c_ctdluid = pw->pw_uid;
72                 }
73         }
74         if (config.c_ctdluid == 0) {
75                 pw = getpwnam("bbs");
76                 if (pw != NULL) {
77                         config.c_ctdluid = pw->pw_uid;
78                 }
79         }
80         if (config.c_ctdluid == 0) {
81                 pw = getpwnam("guest");
82                 if (pw != NULL) {
83                         config.c_ctdluid = pw->pw_uid;
84                 }
85         }
86         if (config.c_createax == 0) {
87                 config.c_createax = 3;
88         }
89
90         /*
91          * Default port numbers for various services
92          */
93         config.c_smtp_port = 25;
94         config.c_pop3_port = 110;
95         config.c_imap_port = 143;
96         config.c_msa_port = 587;
97         config.c_smtps_port = 465;
98         config.c_pop3s_port = 995;
99         config.c_imaps_port = 993;
100         config.c_pftcpdict_port = -1 ;
101         config.c_managesieve_port = 2020;
102         config.c_xmpp_c2s_port = 5222;
103         config.c_xmpp_s2s_port = 5269;
104 }
105
106
107
108 /*
109  * get_config() is called during the initialization of Citadel server.
110  * It verifies the system's integrity and reads citadel.config into memory.
111  */
112 void get_config(void) {
113         FILE *cfp;
114         int rv;
115
116         if (chdir(ctdl_bbsbase_dir) != 0) {
117                 fprintf(stderr,
118                         "This program could not be started.\nUnable to change directory to %s\nError: %s\n",
119                         ctdl_bbsbase_dir,
120                         strerror(errno)
121                 );
122                 exit(CTDLEXIT_HOME);
123         }
124
125         memset(&config, 0, sizeof(struct config));
126         cfp = fopen(file_citadel_config, "rb");
127         if (cfp != NULL) {
128                 rv = fread((char *) &config, sizeof(struct config), 1, cfp);
129                 if (rv != 1)
130                 {
131                         fprintf(stderr, 
132                                 "Warning: The config file %s has unexpected size. \n",
133                                 file_citadel_config);
134                 }
135                 fclose(cfp);
136         }
137         else {
138                 brand_new_installation_set_defaults();
139         }
140
141         /* Ensure that we are linked to the correct version of libcitadel */
142         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
143                 fprintf(stderr, "    You are running libcitadel version %d.%02d\n",
144                         (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
145                 fprintf(stderr, "citserver was compiled against version %d.%02d\n",
146                         (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
147                 exit(CTDLEXIT_LIBCITADEL);
148         }
149
150         /* Only allow LDAP auth mode if we actually have LDAP support */
151 #ifndef HAVE_LDAP
152         if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
153                 fprintf(stderr, "Your system is configured for LDAP authentication,\n"
154                                 "but you are running a server built without OpenLDAP support.\n");
155                 exit(CTDL_EXIT_UNSUP_AUTH);
156         }
157 #endif
158
159         /* Default maximum message length is 10 megabytes.  This is site
160          * configurable.  Also check to make sure the limit has not been
161          * set below 8192 bytes.
162          */
163         if (config.c_maxmsglen <= 0)
164                 config.c_maxmsglen = 10485760;
165         if (config.c_maxmsglen < 8192)
166                 config.c_maxmsglen = 8192;
167
168         /* Default lower and upper limits on number of worker threads */
169
170         if (config.c_min_workers < 3)           /* no less than 3 */
171                 config.c_min_workers = 5;
172
173         if (config.c_max_workers == 0)                  /* default maximum */
174                 config.c_max_workers = 256;
175
176         if (config.c_max_workers < config.c_min_workers)   /* max >= min */
177                 config.c_max_workers = config.c_min_workers;
178
179         /* Networking more than once every five minutes just isn't sane */
180         if (config.c_net_freq == 0L)
181                 config.c_net_freq = 3600L;      /* once per hour default */
182         if (config.c_net_freq < 300L) 
183                 config.c_net_freq = 300L;
184
185         /* Same goes for POP3 */
186         if (config.c_pop3_fetch == 0L)
187                 config.c_pop3_fetch = 3600L;    /* once per hour default */
188         if (config.c_pop3_fetch < 300L) 
189                 config.c_pop3_fetch = 300L;
190         if (config.c_pop3_fastest == 0L)
191                 config.c_pop3_fastest = 3600L;  /* once per hour default */
192         if (config.c_pop3_fastest < 300L) 
193                 config.c_pop3_fastest = 300L;
194
195         /* "create new user" only works with native authentication mode */
196         if (config.c_auth_mode != AUTHMODE_NATIVE) {
197                 config.c_disable_newu = 1;
198         }
199 }
200
201
202 /*
203  * Occasionally, we will need to write the config file, because some operations
204  * change site-wide parameters.
205  */
206 void put_config(void)
207 {
208         FILE *cfp;
209         int rv;
210
211         if ((cfp = fopen(file_citadel_config, "rb+")) == NULL)
212                 perror(file_citadel_config);
213         else {
214                 rv = fwrite((char *) &config, sizeof(struct config), 1, cfp);
215                 if (rv == -1)
216                         syslog(LOG_EMERG, "Failed to write: %s [%s]\n", 
217                                file_citadel_config, strerror(errno));
218                 fclose(cfp);
219         }
220 }