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