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