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