e30f098a28a719ee5c28a0c54fa64f3f451525fc
[citadel.git] / citadel / config.c
1 /*
2  * Read and write the citadel.config file
3  *
4  * Copyright (c) 1987-2015 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 <stdio.h>
17 #include <sys/utsname.h>
18 #include <libcitadel.h>
19 #include "config.h"
20 #include "ctdl_module.h"
21
22 struct config config;
23
24 #define STR_NOT_EMPTY(CFG_FIELDNAME) if (IsEmptyStr(config.CFG_FIELDNAME)) \
25                 syslog(LOG_EMERG, "configuration setting "#CFG_FIELDNAME" is empty, but must not - check your config!");
26
27 #define TEST_PORT(CFG_PORT, DEFAULTPORT)                        \
28         if ((config.CFG_PORT < -1) ||           \
29             (config.CFG_PORT == 0) ||           \
30             (config.CFG_PORT > UINT16_MAX))     \
31                 syslog(LOG_EMERG, "configuration setting "#CFG_PORT" is not -1 (disabled) or a valid TCP-Port - check your config! Default setting is: "#DEFAULTPORT);
32                         
33
34 void validate_config(void) {
35 /* these shouldn't be empty: */
36         STR_NOT_EMPTY(c_fqdn);
37
38         STR_NOT_EMPTY(c_baseroom);
39         STR_NOT_EMPTY(c_aideroom);
40         STR_NOT_EMPTY(c_twitroom);
41         STR_NOT_EMPTY(c_nodename);
42         STR_NOT_EMPTY(c_default_cal_zone);
43
44 /* we bind a lot of ports: */
45         TEST_PORT(c_smtp_port, 25);
46         TEST_PORT(c_pop3_port, 110);
47         TEST_PORT(c_imap_port, 143);
48         TEST_PORT(c_msa_port, 587);
49         TEST_PORT(c_port_number, 504);
50         TEST_PORT(c_smtps_port, 465);
51         TEST_PORT(c_pop3s_port, 995);
52         TEST_PORT(c_imaps_port, 993);
53         TEST_PORT(c_pftcpdict_port, -1);
54         TEST_PORT(c_managesieve_port, 2020);
55         TEST_PORT(c_xmpp_c2s_port, 5222);
56         TEST_PORT(c_xmpp_s2s_port, 5269);
57         TEST_PORT(c_nntp_port, 119);
58         TEST_PORT(c_nntps_port, 563);
59
60         if (config.c_ctdluid == 0)
61                 syslog(LOG_EMERG, "citadel should not be configured to run as root! Check the value of c_ctdluid");
62         else if (getpwuid(CTDLUID) == NULL)
63                 syslog(LOG_EMERG, "The UID (%d) citadel is configured to use is not defined in your system (/etc/passwd?)! Check the value of c_ctdluid", CTDLUID);
64         
65 }
66
67 /*
68  * Put some sane default values into our configuration.  Some will be overridden when we run setup.
69  */
70 void brand_new_installation_set_defaults(void) {
71
72         struct passwd *pw;
73         struct utsname my_utsname;
74         struct hostent *he;
75
76         /* Determine our host name, in case we need to use it as a default */
77         uname(&my_utsname);
78
79         /* set some sample/default values in place of blanks... */
80         extract_token(config.c_nodename, my_utsname.nodename, 0, '.', sizeof config.c_nodename);
81         if (IsEmptyStr(config.c_fqdn) ) {
82                 if ((he = gethostbyname(my_utsname.nodename)) != NULL) {
83                         safestrncpy(config.c_fqdn, he->h_name, sizeof config.c_fqdn);
84                 }
85                 else {
86                         safestrncpy(config.c_fqdn, my_utsname.nodename, sizeof config.c_fqdn);
87                 }
88         }
89
90         safestrncpy(config.c_humannode, "Citadel Server", sizeof config.c_humannode);
91         safestrncpy(config.c_phonenum, "US 800 555 1212", sizeof config.c_phonenum);
92         config.c_initax = 4;
93         safestrncpy(config.c_moreprompt, "<more>", sizeof config.c_moreprompt);
94         safestrncpy(config.c_twitroom, "Trashcan", sizeof config.c_twitroom);
95         safestrncpy(config.c_baseroom, BASEROOM, sizeof config.c_baseroom);
96         safestrncpy(config.c_aideroom, "Aide", sizeof config.c_aideroom);
97         config.c_port_number = 504;
98         config.c_sleeping = 900;
99
100         if (config.c_ctdluid == 0) {
101                 pw = getpwnam("citadel");
102                 if (pw != NULL) {
103                         config.c_ctdluid = pw->pw_uid;
104                 }
105         }
106         if (config.c_ctdluid == 0) {
107                 pw = getpwnam("bbs");
108                 if (pw != NULL) {
109                         config.c_ctdluid = pw->pw_uid;
110                 }
111         }
112         if (config.c_ctdluid == 0) {
113                 pw = getpwnam("guest");
114                 if (pw != NULL) {
115                         config.c_ctdluid = pw->pw_uid;
116                 }
117         }
118         if (config.c_createax == 0) {
119                 config.c_createax = 3;
120         }
121
122         /*
123          * Default port numbers for various services
124          */
125         config.c_smtp_port = 25;
126         config.c_pop3_port = 110;
127         config.c_imap_port = 143;
128         config.c_msa_port = 587;
129         config.c_smtps_port = 465;
130         config.c_pop3s_port = 995;
131         config.c_imaps_port = 993;
132         config.c_pftcpdict_port = -1 ;
133         config.c_managesieve_port = 2020;
134         config.c_xmpp_c2s_port = 5222;
135         config.c_xmpp_s2s_port = 5269;
136         config.c_nntp_port = 119;
137         config.c_nntps_port = 563;
138 }
139
140
141
142 /*
143  * Called during the initialization of Citadel server.
144  * It verifies the system's integrity and reads citadel.config into memory.
145  */
146 void get_config(void) {
147         FILE *cfp;
148         int rv;
149
150         if (chdir(ctdl_bbsbase_dir) != 0) {
151                 fprintf(stderr,
152                         "This program could not be started.\nUnable to change directory to %s\nError: %s\n",
153                         ctdl_bbsbase_dir,
154                         strerror(errno)
155                 );
156                 exit(CTDLEXIT_HOME);
157         }
158
159         memset(&config, 0, sizeof(struct config));
160         cfp = fopen(file_citadel_config, "rb");
161         if (cfp != NULL) {
162                 rv = fread((char *) &config, sizeof(struct config), 1, cfp);
163                 if (rv != 1)
164                 {
165                         fprintf(stderr, 
166                                 "Warning: The config file %s has unexpected size. \n",
167                                 file_citadel_config
168                         );
169                 }
170                 fclose(cfp);
171         }
172         else {
173                 brand_new_installation_set_defaults();
174         }
175
176         /* Ensure that we are linked to the correct version of libcitadel */
177         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
178                 fprintf(stderr, "    You are running libcitadel version %d.%02d\n",
179                         (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
180                 fprintf(stderr, "citserver was compiled against version %d.%02d\n",
181                         (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
182                 exit(CTDLEXIT_LIBCITADEL);
183         }
184
185         /* Only allow LDAP auth mode if we actually have LDAP support */
186 #ifndef HAVE_LDAP
187         if ((config.c_auth_mode == AUTHMODE_LDAP) || (config.c_auth_mode == AUTHMODE_LDAP_AD)) {
188                 fprintf(stderr, "Your system is configured for LDAP authentication,\n"
189                                 "but you are running a server built without OpenLDAP support.\n");
190                 exit(CTDL_EXIT_UNSUP_AUTH);
191         }
192 #endif
193
194         /* Default maximum message length is 10 megabytes.  This is site
195          * configurable.  Also check to make sure the limit has not been
196          * set below 8192 bytes.
197          */
198         if (config.c_maxmsglen <= 0)
199                 config.c_maxmsglen = 10485760;
200         if (config.c_maxmsglen < 8192)
201                 config.c_maxmsglen = 8192;
202
203         /* Default lower and upper limits on number of worker threads */
204
205         if (config.c_min_workers < 3)           /* no less than 3 */
206                 config.c_min_workers = 5;
207
208         if (config.c_max_workers == 0)                  /* default maximum */
209                 config.c_max_workers = 256;
210
211         if (config.c_max_workers < config.c_min_workers)   /* max >= min */
212                 config.c_max_workers = config.c_min_workers;
213
214         /* Networking more than once every five minutes just isn't sane */
215         if (config.c_net_freq == 0L)
216                 config.c_net_freq = 3600L;      /* once per hour default */
217         if (config.c_net_freq < 300L) 
218                 config.c_net_freq = 300L;
219
220         /* Same goes for POP3 */
221         if (config.c_pop3_fetch == 0L)
222                 config.c_pop3_fetch = 3600L;    /* once per hour default */
223         if (config.c_pop3_fetch < 300L) 
224                 config.c_pop3_fetch = 300L;
225         if (config.c_pop3_fastest == 0L)
226                 config.c_pop3_fastest = 3600L;  /* once per hour default */
227         if (config.c_pop3_fastest < 300L) 
228                 config.c_pop3_fastest = 300L;
229
230         /* "create new user" only works with native authentication mode */
231         if (config.c_auth_mode != AUTHMODE_NATIVE) {
232                 config.c_disable_newu = 1;
233         }
234 }
235
236 long config_msgnum = 0;
237
238 /*
239  * Occasionally, we will need to write the config file, because some operations
240  * change site-wide parameters.
241  */
242 void put_config(void)
243 {
244         FILE *cfp;
245         int blocks_written = 0;
246
247         cfp = fopen(file_citadel_config, "w");
248         if (cfp != NULL) {
249                 blocks_written = fwrite((char *) &config, sizeof(struct config), 1, cfp);
250                 if (blocks_written == 1) {
251                         chown(file_citadel_config, CTDLUID, (-1));
252                         chmod(file_citadel_config, 0600);
253                         fclose(cfp);
254                         return;
255                 }
256                 fclose(cfp);
257         }
258         syslog(LOG_EMERG, "%s: %s", file_citadel_config, strerror(errno));
259 }
260
261
262
263 void CtdlGetSysConfigBackend(long msgnum, void *userdata) {
264         config_msgnum = msgnum;
265 }
266
267
268 char *CtdlGetSysConfig(char *sysconfname) {
269         char hold_rm[ROOMNAMELEN];
270         long msgnum;
271         char *conf;
272         struct CtdlMessage *msg;
273         char buf[SIZ];
274         
275         strcpy(hold_rm, CC->room.QRname);
276         if (CtdlGetRoom(&CC->room, SYSCONFIGROOM) != 0) {
277                 CtdlGetRoom(&CC->room, hold_rm);
278                 return NULL;
279         }
280
281
282         /* We want the last (and probably only) config in this room */
283         begin_critical_section(S_CONFIG);
284         config_msgnum = (-1L);
285         CtdlForEachMessage(MSGS_LAST, 1, NULL, sysconfname, NULL,
286                            CtdlGetSysConfigBackend, NULL);
287         msgnum = config_msgnum;
288         end_critical_section(S_CONFIG);
289
290         if (msgnum < 0L) {
291                 conf = NULL;
292         }
293         else {
294                 msg = CtdlFetchMessage(msgnum, 1);
295                 if (msg != NULL) {
296                         conf = strdup(msg->cm_fields[eMesageText]);
297                         CM_Free(msg);
298                 }
299                 else {
300                         conf = NULL;
301                 }
302         }
303
304         CtdlGetRoom(&CC->room, hold_rm);
305
306         if (conf != NULL) do {
307                         extract_token(buf, conf, 0, '\n', sizeof buf);
308                         strcpy(conf, &conf[strlen(buf)+1]);
309                 } while ( (!IsEmptyStr(conf)) && (!IsEmptyStr(buf)) );
310
311         return(conf);
312 }
313
314
315 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
316         CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 1, 0);
317 }
318