Update config.guess and config.sub. Fix chdir() and -h in webcit
[citadel.git] / webcit / webserver.c
1 /*
2  * Copyright (c) 1996-2021 by the citadel.org team
3  *
4  * This program is open source software.  You can redistribute it and/or
5  * modify it under the terms of the GNU General Public License version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include "webcit.h"
14 #include "webserver.h"
15
16 #include "modules_init.h"
17
18 extern int msock;                               /* master listening socket */
19 extern char static_icon_dir[PATH_MAX];          /* where should we find our mime icons */
20 int is_https = 0;                               /* Nonzero if I am an HTTPS service */
21 int follow_xff = 0;                             /* Follow X-Forwarded-For: header? */
22 int DisableGzip = 0;
23 char *default_landing_page = NULL;
24 extern pthread_mutex_t SessionListMutex;
25
26 extern void *housekeeping_loop(void);
27 extern int webcit_tcp_server(char *ip_addr, int port_number, int queue_len);
28 extern int webcit_uds_server(char *sockpath, int queue_len);
29 extern void graceful_shutdown_watcher(int signum);
30 extern void graceful_shutdown(int signum);
31 extern void start_daemon(char *pid_file);
32 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
33 extern void worker_entry(void);
34 extern void drop_root(uid_t UID);
35
36 char socket_dir[PATH_MAX];      /* where to talk to our citadel server */
37 char *server_cookie = NULL;     /* our Cookie connection to the client */
38 int http_port = PORT_NUM;       /* Port to listen on */
39 char *ctdlhost = DEFAULT_HOST;  /* Host name or IP address of Citadel server */
40 char *ctdlport = DEFAULT_PORT;  /* Port number of Citadel server */
41 int setup_wizard = 0;           /* should we run the setup wizard? */
42 char wizard_filename[PATH_MAX]; /* location of file containing the last webcit version against which we ran setup wizard */
43 int running_as_daemon = 0;      /* should we deamonize on startup? */
44
45 /* #define DBG_PRINNT_HOOKS_AT_START */
46 #ifdef DBG_PRINNT_HOOKS_AT_START
47 extern HashList *HandlerHash;
48 const char foobuf[32];
49 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
50 #endif 
51 extern int verbose;
52 extern int dbg_analyze_msg;
53 extern int dbg_backtrace_template_errors;
54 extern int DumpTemplateI18NStrings;
55 extern StrBuf *I18nDump;
56 void InitTemplateCache(void);
57 extern int LoadTemplates;
58
59 void LoadMimeBlacklist(void);
60
61 /*
62  * Here's where it all begins.
63  */
64 int main(int argc, char **argv)
65 {
66         uid_t UID = -1;
67         size_t basesize = 2;            /* how big should strbufs be on creation? */
68         pthread_t SessThread;           /* Thread descriptor */
69         pthread_attr_t attr;            /* Thread attributes */
70         int a;                          /* General-purpose variable */
71         char ip_addr[256]="*";
72         int relh=0;
73         int home=0;
74         char relhome[PATH_MAX]="";
75         char webcitdir[PATH_MAX] = DATADIR;
76         char *pidfile = NULL;
77         char *hdir;
78         const char *basedir = NULL;
79         char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
80         const char *I18nDumpFile = NULL;
81         int max_log_level = LOG_INFO;
82
83         WildFireInitBacktrace(argv[0], 2);
84
85         start_modules();
86
87 #ifdef DBG_PRINNT_HOOKS_AT_START
88 /*      dbg_PrintHash(HandlerHash, nix, NULL);*/
89 #endif
90
91         /* Ensure that we are linked to the correct version of libcitadel */
92         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
93                 fprintf(stderr, " You are running libcitadel version %d\n", libcitadel_version_number() );
94                 fprintf(stderr, "WebCit was compiled against version %d\n", LIBCITADEL_VERSION_NUMBER );
95                 return(1);
96         }
97
98         strcpy(uds_listen_path, "");
99
100         /* Parse command line */
101 #ifdef HAVE_OPENSSL
102         while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfsS:Z:v:")) != EOF)
103 #else
104         while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfZ:v:")) != EOF)
105 #endif
106                 switch (a) {
107                 case 'u':
108                         UID = atol(optarg);
109                         break;
110                 case 'h':
111                         hdir = strdup(optarg);
112                         relh=hdir[0]!='/';
113                         if (!relh) {
114                                 safestrncpy(webcitdir, hdir, sizeof webcitdir);
115                         }
116                         else {
117                                 safestrncpy(relhome, relhome, sizeof relhome);
118                         }
119                         /* free(hdir); TODO: SHOULD WE DO THIS? */
120                         home=1;
121                         break;
122                 case 'd':
123                         running_as_daemon = 1;
124                         break;
125                 case 'D':
126                         pidfile = strdup(optarg);
127                         running_as_daemon = 1;
128                         break;
129                 case 'g':
130                         default_landing_page = strdup(optarg);
131                         break;
132                 case 'B': /* Basesize */
133                         basesize = atoi(optarg);
134                         if (basesize > 2)
135                                 StartLibCitadel(basesize);
136                         break;
137                 case 'i':
138                         safestrncpy(ip_addr, optarg, sizeof ip_addr);
139                         break;
140                 case 'p':
141                         http_port = atoi(optarg);
142                         if (http_port == 0) {
143                                 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
144                         }
145                         break;
146                 case 't':
147                         /* no longer used, but ignored so old scripts don't break */
148                         break;
149                 case 'T':
150                         LoadTemplates = atoi(optarg);
151                         dbg_analyze_msg = (LoadTemplates & (1<<1)) != 0;
152                         dbg_backtrace_template_errors = (LoadTemplates & (1<<2)) != 0;
153                         break;
154                 case 'Z':
155                         DisableGzip = 1;
156                         break;
157                 case 'x':
158                         max_log_level = atoi(optarg);
159                         break;
160                 case 'f':
161                         follow_xff = 1;
162                         break;
163                 case 'c':
164                         server_cookie = malloc(256);
165                         if (server_cookie != NULL) {
166                                 safestrncpy(server_cookie,
167                                        "Set-cookie: wcserver=",
168                                         256);
169                                 if (gethostname
170                                     (&server_cookie[strlen(server_cookie)],
171                                      200) != 0) {
172                                         syslog(LOG_INFO, "gethostname: %s", strerror(errno));
173                                         free(server_cookie);
174                                 }
175                         }
176                         break;
177 #ifdef HAVE_OPENSSL
178                 case 's':
179                         is_https = 1;
180                         break;
181                 case 'S':
182                         is_https = 1;
183                         ssl_cipher_list = strdup(optarg);
184                         break;
185 #endif
186                 case 'G':
187                         DumpTemplateI18NStrings = 1;
188                         I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
189                         I18nDumpFile = optarg;
190                         break;
191                 case 'v':
192                         verbose=1;
193                         break;
194                 default:
195                         fprintf(stderr, "usage:\nwebcit "
196                                 "[-i ip_addr] [-p http_port] "
197                                 "[-c] [-f] "
198                                 "[-T Templatedebuglevel] "
199                                 "[-d] [-Z] [-G i18ndumpfile] "
200                                 "[-u uid] [-h homedirectory] "
201                                 "[-D daemonizepid] [-v] "
202                                 "[-g defaultlandingpage] [-B basesize] "
203 #ifdef HAVE_OPENSSL
204                                 "[-s] [-S cipher_suites]"
205 #endif
206                                 "[remotehost [remoteport]]\n");
207                         return 1;
208                 }
209
210         /* Start the logger */
211         setlogmask(LOG_UPTO(max_log_level));
212         openlog("webcit",
213                 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
214                 LOG_DAEMON
215         );
216
217         if (optind < argc) {
218                 ctdlhost = argv[optind];
219                 if (++optind < argc)
220                         ctdlport = argv[optind];
221         }
222
223         /* daemonize, if we were asked to */
224         if (!DumpTemplateI18NStrings && running_as_daemon) {
225                 start_daemon(pidfile);
226         }
227         else {
228                 signal(SIGINT, graceful_shutdown);
229                 signal(SIGHUP, graceful_shutdown);
230         }
231
232         webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
233         LoadMimeBlacklist();
234         LoadIconDir(static_icon_dir);
235
236         /* Tell 'em who's in da house */
237         syslog(LOG_NOTICE, "%s", PACKAGE_STRING);
238         syslog(LOG_NOTICE, "Copyright (C) 1996-2020 by the citadel.org team");
239         syslog(LOG_NOTICE, " ");
240         syslog(LOG_NOTICE, "This program is open source software: you can redistribute it and/or");
241         syslog(LOG_NOTICE, "modify it under the terms of the GNU General Public License, version 3.");
242         syslog(LOG_NOTICE, " ");
243         syslog(LOG_NOTICE, "This program is distributed in the hope that it will be useful,");
244         syslog(LOG_NOTICE, "but WITHOUT ANY WARRANTY; without even the implied warranty of");
245         syslog(LOG_NOTICE, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
246         syslog(LOG_NOTICE, "GNU General Public License for more details.");
247         syslog(LOG_NOTICE, " ");
248
249         /* run from the webcit home directory */
250         if (chdir(webcitdir) != 0) {
251                 syslog(LOG_ERR, "webcit: %s: %m", webcitdir);
252                 exit(errno);
253         }
254
255         /* initialize various subsystems */
256         initialise_modules();
257         initialise2_modules();
258         InitTemplateCache();
259         if (DumpTemplateI18NStrings) {
260                 FILE *fd;
261                 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
262                 if (StrLength(I18nDump) < 50) {
263                         syslog(LOG_INFO, "*******************************************************************\n");
264                         syslog(LOG_INFO, "*   No strings found in templates!  Are you sure they're there?   *\n");
265                         syslog(LOG_INFO, "*******************************************************************\n");
266                         return -1;
267                 }
268                 fd = fopen(I18nDumpFile, "w");
269                 if (fd == NULL) {
270                         syslog(LOG_INFO, "***********************************************\n");
271                         syslog(LOG_INFO, "*   unable to open I18N dumpfile [%s]         *\n", I18nDumpFile);
272                         syslog(LOG_INFO, "***********************************************\n");
273                         return -1;
274                 }
275                 fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
276                 fclose(fd);
277                 return 0;
278         }
279
280         /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
281
282 #ifdef LIBICAL_ICAL_EXPORT      // cheap and sleazy way to detect libical >=2.0
283                 icalerror_set_errors_are_fatal(0);
284 #else
285                 icalerror_errors_are_fatal = 0;
286 #endif
287
288         /* Use our own prefix on tzid's generated from system tzdata */
289         icaltimezone_set_tzid_prefix("/citadel.org/");
290
291         /*
292          * Set up a place to put thread-specific data.
293          * We only need a single pointer per thread - it points to the
294          * wcsession struct to which the thread is currently bound.
295          */
296         if (pthread_key_create(&MyConKey, NULL) != 0) {
297                 syslog(LOG_ERR, "Can't create TSD key: %s", strerror(errno));
298         }
299         InitialiseSemaphores();
300
301         /*
302          * Set up a place to put thread-specific SSL data.
303          * We don't stick this in the wcsession struct because SSL starts
304          * up before the session is bound, and it gets torn down between
305          * transactions.
306          */
307 #ifdef HAVE_OPENSSL
308         if (pthread_key_create(&ThreadSSL, NULL) != 0) {
309                 syslog(LOG_ERR, "Can't create TSD key: %s", strerror(errno));
310         }
311 #endif
312
313         /*
314          * Bind the server to our favorite port.
315          * There is no need to check for errors, because webcit_tcp_server()
316          * exits if it doesn't succeed.
317          */
318
319         if (!IsEmptyStr(uds_listen_path)) {
320                 syslog(LOG_DEBUG, "Attempting to create listener socket at %s...", uds_listen_path);
321                 msock = webcit_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
322         }
323         else {
324                 syslog(LOG_DEBUG, "Attempting to bind to port %d...", http_port);
325                 msock = webcit_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
326         }
327         if (msock < 0)
328         {
329                 ShutDownWebcit();
330                 return -msock;
331         }
332
333         syslog(LOG_INFO, "Listening on socket %d", msock);
334         signal(SIGPIPE, SIG_IGN);
335
336         pthread_mutex_init(&SessionListMutex, NULL);
337
338         /*
339          * Start up the housekeeping thread
340          */
341         pthread_attr_init(&attr);
342         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
343         pthread_create(&SessThread, &attr, (void *(*)(void *)) housekeeping_loop, NULL);
344
345         /*
346          * If this is an HTTPS server, fire up SSL
347          */
348 #ifdef HAVE_OPENSSL
349         if (is_https) {
350                 init_ssl();
351         }
352 #endif
353         drop_root(UID);
354
355         /* Become a worker thread.  More worker threads will be spawned as they are needed. */
356         worker_entry();
357         ShutDownLibCitadel();
358         return 0;
359 }
360
361
362
363
364
365
366