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