indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / sysdep.c
1
2 /*
3  * WebCit "system dependent" code.
4  *
5  * Copyright (c) 1996-2021 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <ctype.h>
22 #include <signal.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/wait.h>
26 #include <sys/socket.h>
27 #include <syslog.h>
28 #include <sys/syslog.h>
29 #include <time.h>
30 #include <limits.h>
31 #include <sys/resource.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
36 #include <sys/un.h>
37 #include <string.h>
38 #include <pwd.h>
39 #include <errno.h>
40 #include <stdarg.h>
41 #include <grp.h>
42 #ifdef HAVE_PTHREAD_H
43 #include <pthread.h>
44 #endif
45 #include "webcit.h"
46 #include "sysdep.h"
47
48 #ifdef HAVE_SYS_SELECT_H
49 #include <sys/select.h>
50 #endif
51
52 #include "webserver.h"
53 #include "modules_init.h"
54 #if HAVE_BACKTRACE
55 #include <execinfo.h>
56 #endif
57
58 pthread_mutex_t Critters[MAX_SEMAPHORES];       /* Things needing locking */
59 pthread_key_t MyConKey;         /* TSD key for MyContext() */
60 pthread_key_t MyReq;            /* TSD key for MyReq() */
61 int msock;                      /* master listening socket */
62 int time_to_die = 0;            /* Nonzero if server is shutting down */
63
64 extern void *context_loop(ParsedHttpHdrs * Hdr);
65 extern void *housekeeping_loop(void);
66 extern void do_housekeeping(void);
67
68 char file_etc_mimelist[PATH_MAX] = "";
69
70 char etc_dir[PATH_MAX];
71 char static_dir[PATH_MAX];      /* calculated on startup */
72 char static_local_dir[PATH_MAX];        /* calculated on startup */
73 char static_icon_dir[PATH_MAX]; /* where should we find our mime icons? */
74 char *static_dirs[] = {         /* needs same sort order as the web mapping */
75         "./static",
76         "./static.local",
77         "./tiny_mce",
78         "./static/webcit_icons"
79 };
80
81 int ExitPipe[2];
82 HashList *GZMimeBlackList = NULL;       /* mimetypes which shouldn't be gzip compressed */
83
84 void LoadMimeBlacklist(void) {
85         StrBuf *MimeBlackLine;
86         IOBuffer IOB;
87         eReadState state;
88
89         memset(&IOB, 0, sizeof(IOBuffer));
90         IOB.fd = open(file_etc_mimelist, O_RDONLY);
91
92         IOB.Buf = NewStrBuf();
93         MimeBlackLine = NewStrBuf();
94         GZMimeBlackList = NewHash(1, NULL);
95
96         do {
97                 state = StrBufChunkSipLine(MimeBlackLine, &IOB);
98
99                 switch (state) {
100                 case eMustReadMore:
101                         if (StrBuf_read_one_chunk_callback(IOB.fd, 0, &IOB) <= 0)
102                                 state = eReadFail;
103                         break;
104                 case eReadSuccess:
105                         if ((StrLength(MimeBlackLine) > 1) && (*ChrPtr(MimeBlackLine) != '#')) {
106                                 Put(GZMimeBlackList, SKEY(MimeBlackLine), (void *) 1, reference_free_handler);
107                         }
108                         FlushStrBuf(MimeBlackLine);
109                         break;
110                 case eReadFail:
111                         break;
112                 case eBufferNotEmpty:
113                         break;
114                 }
115         }
116         while (state != eReadFail);
117
118         close(IOB.fd);
119
120         FreeStrBuf(&IOB.Buf);
121         FreeStrBuf(&MimeBlackLine);
122 }
123
124 void CheckGZipCompressionAllowed(const char *MimeType, long MLen) {
125         void *v;
126
127         if (WC->Hdr->HR.gzip_ok)
128                 WC->Hdr->HR.gzip_ok = GetHash(GZMimeBlackList, MimeType, MLen, &v) == 0;
129 }
130
131 void InitialiseSemaphores(void) {
132         int i;
133
134         /* Set up a bunch of semaphores to be used for critical sections */
135         for (i = 0; i < MAX_SEMAPHORES; ++i) {
136                 pthread_mutex_init(&Critters[i], NULL);
137         }
138
139         if (pipe(ExitPipe)) {
140                 syslog(LOG_WARNING, "Failed to open exit pipe: %d [%s]\n", errno, strerror(errno));
141
142                 exit(-1);
143         }
144 }
145
146 /*
147  * Obtain a semaphore lock to begin a critical section.
148  */
149 void begin_critical_section(int which_one) {
150         pthread_mutex_lock(&Critters[which_one]);
151 }
152
153 /*
154  * Release a semaphore lock to end a critical section.
155  */
156 void end_critical_section(int which_one) {
157         pthread_mutex_unlock(&Critters[which_one]);
158 }
159
160
161 void ShutDownWebcit(void) {
162
163         DeleteHash(&GZMimeBlackList);
164         free_zone_directory();
165         icaltimezone_release_zone_tab();
166         icalmemory_free_ring();
167         ShutDownLibCitadel();
168         shutdown_modules();
169 }
170
171 /*
172  * Entry point for worker threads
173  */
174 void worker_entry(void) {
175         int ssock;
176         int i = 0;
177         int fail_this_transaction = 0;
178         ParsedHttpHdrs Hdr;
179
180         memset(&Hdr, 0, sizeof(ParsedHttpHdrs));
181         Hdr.HR.eReqType = eGET;
182         http_new_modules(&Hdr);
183
184         do {
185                 /* Each worker thread blocks on accept() while waiting for something to do. */
186                 fail_this_transaction = 0;
187                 ssock = -1;
188                 errno = EAGAIN;
189                 do {
190                         fd_set wset;
191                         --num_threads_executing;
192                         FD_ZERO(&wset);
193                         FD_SET(msock, &wset);
194                         FD_SET(ExitPipe[1], &wset);
195
196                         select(msock + 1, NULL, &wset, NULL, NULL);
197                         if (time_to_die)
198                                 break;
199
200                         ssock = accept(msock, NULL, 0);
201                         ++num_threads_executing;
202                         if (ssock < 0)
203                                 fail_this_transaction = 1;
204                 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
205
206                 if ((msock == -1) || (time_to_die)) {   /* ok, we're going down. */
207                         int shutdown = 0;
208
209                         /* The first thread to get here will have to do the cleanup.
210                          * Make sure it's really just one.
211                          */
212                         begin_critical_section(S_SHUTDOWN);
213                         if (msock == -1) {
214                                 msock = -2;
215                                 shutdown = 1;
216                         }
217                         end_critical_section(S_SHUTDOWN);
218                         if (shutdown == 1) {    /* we're the one to cleanup the mess. */
219                                 http_destroy_modules(&Hdr);
220                                 syslog(LOG_DEBUG, "I'm master shutdown: tagging sessions to be killed.\n");
221                                 shutdown_sessions();
222                                 syslog(LOG_DEBUG, "master shutdown: waiting for others\n");
223                                 sleeeeeeeeeep(1);       /* wait so some others might finish... */
224                                 syslog(LOG_DEBUG, "master shutdown: cleaning up sessions\n");
225                                 do_housekeeping();
226                                 syslog(LOG_DEBUG, "master shutdown: cleaning up libical\n");
227
228                                 ShutDownWebcit();
229
230                                 syslog(LOG_DEBUG, "master shutdown exiting.\n");
231                                 exit(0);
232                         }
233                         break;
234                 }
235                 if (ssock < 0)
236                         continue;
237
238                 check_thread_pool_size();
239
240                 /* Now do something. */
241                 if (msock < 0) {
242                         if (ssock > 0)
243                                 close(ssock);
244                         syslog(LOG_DEBUG, "in between.");
245                         pthread_exit(NULL);
246                 }
247                 else {
248                         /* Got it? do some real work! */
249                         /* Set the SO_REUSEADDR socket option */
250                         i = 1;
251                         setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
252
253                         /* If we are an HTTPS server, go crypto now. */
254 #ifdef HAVE_OPENSSL
255                         if (is_https) {
256                                 if (starttls(ssock) != 0) {
257                                         fail_this_transaction = 1;
258                                         close(ssock);
259                                 }
260                         }
261                         else
262 #endif
263                         {
264                                 int fdflags;
265                                 fdflags = fcntl(ssock, F_GETFL);
266                                 if (fdflags < 0)
267                                         syslog(LOG_WARNING, "unable to get server socket flags! %s \n", strerror(errno));
268                                 fdflags = fdflags | O_NONBLOCK;
269                                 if (fcntl(ssock, F_SETFL, fdflags) < 0)
270                                         syslog(LOG_WARNING, "unable to set server socket nonblocking flags! %s \n",
271                                                strerror(errno));
272                         }
273
274                         if (fail_this_transaction == 0) {
275                                 Hdr.http_sock = ssock;
276
277                                 /* Perform an HTTP transaction... */
278                                 context_loop(&Hdr);
279
280                                 /* Shut down SSL/TLS if required... */
281 #ifdef HAVE_OPENSSL
282                                 if (is_https) {
283                                         endtls();
284                                 }
285 #endif
286
287                                 /* ...and close the socket. */
288                                 if (Hdr.http_sock > 0) {
289                                         lingering_close(ssock);
290                                 }
291                                 http_detach_modules(&Hdr);
292
293                         }
294
295                 }
296
297         } while (!time_to_die);
298
299         http_destroy_modules(&Hdr);
300         syslog(LOG_DEBUG, "Thread exiting.\n");
301         pthread_exit(NULL);
302 }
303
304
305 /*
306  * Shut us down the regular way.
307  * signum is the signal we want to forward
308  */
309 pid_t current_child;
310 void graceful_shutdown_watcher(int signum) {
311         syslog(LOG_INFO, "Watcher thread exiting.\n");
312         write(ExitPipe[0], HKEY("                              "));
313         kill(current_child, signum);
314         if (signum != SIGHUP)
315                 exit(0);
316 }
317
318
319 /*
320  * Shut us down the regular way.
321  * signum is the signal we want to forward
322  */
323 pid_t current_child;
324 void graceful_shutdown(int signum) {
325         FILE *FD;
326         int fd;
327
328         syslog(LOG_INFO, "WebCit is being shut down on signal %d.\n", signum);
329         fd = msock;
330         msock = -1;
331         time_to_die = 1;
332         FD = fdopen(fd, "a+");
333         fflush(FD);
334         fclose(FD);
335         close(fd);
336         write(ExitPipe[0], HKEY("                              "));
337 }
338
339
340 /*
341  * Start running as a daemon.
342  */
343 void start_daemon(char *pid_file) {
344         int status = 0;
345         pid_t child = 0;
346         FILE *fp;
347         int do_restart = 0;
348
349         current_child = 0;
350
351         /* Close stdin/stdout/stderr and replace them with /dev/null.
352          * We don't just call close() because we don't want these fd's
353          * to be reused for other files.
354          */
355         chdir("/");
356
357         signal(SIGHUP, SIG_IGN);
358         signal(SIGINT, SIG_IGN);
359         signal(SIGQUIT, SIG_IGN);
360
361         child = fork();
362         if (child != 0) {
363                 exit(0);
364         }
365
366         setsid();
367         umask(0);
368         freopen("/dev/null", "r", stdin);
369         freopen("/dev/null", "w", stdout);
370         freopen("/dev/null", "w", stderr);
371         signal(SIGTERM, graceful_shutdown_watcher);
372         signal(SIGHUP, graceful_shutdown_watcher);
373
374         do {
375                 current_child = fork();
376
377
378                 if (current_child < 0) {
379                         perror("fork");
380                         ShutDownLibCitadel();
381                         exit(errno);
382                 }
383
384                 else if (current_child == 0) {  /* child process */
385                         signal(SIGHUP, graceful_shutdown);
386
387                         return; /* continue starting webcit. */
388                 }
389                 else {          /* watcher process */
390                         if (pid_file) {
391                                 fp = fopen(pid_file, "w");
392                                 if (fp != NULL) {
393                                         fprintf(fp, "%d\n", getpid());
394                                         fclose(fp);
395                                 }
396                         }
397                         waitpid(current_child, &status, 0);
398                 }
399
400                 do_restart = 0;
401
402                 /* Did the main process exit with an actual exit code? */
403                 if (WIFEXITED(status)) {
404
405                         /* Exit code 0 means the watcher should exit */
406                         if (WEXITSTATUS(status) == 0) {
407                                 do_restart = 0;
408                         }
409
410                         /* Exit code 101-109 means the watcher should exit */
411                         else if ((WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
412                                 do_restart = 0;
413                         }
414
415                         /* Any other exit code means we should restart. */
416                         else {
417                                 do_restart = 1;
418                         }
419                 }
420
421                 /* Any other type of termination (signals, etc.) should also restart. */
422                 else {
423                         do_restart = 1;
424                 }
425
426         } while (do_restart);
427
428         if (pid_file) {
429                 unlink(pid_file);
430         }
431         ShutDownLibCitadel();
432         exit(WEXITSTATUS(status));
433 }
434
435
436 /*
437  * Spawn an additional worker thread into the pool.
438  */
439 void spawn_another_worker_thread() {
440         pthread_t SessThread;   /* Thread descriptor */
441         pthread_attr_t attr;    /* Thread attributes */
442         int ret;
443
444         ++num_threads_existing;
445         ++num_threads_executing;
446
447         /* set attributes for the new thread */
448         pthread_attr_init(&attr);
449         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
450
451         /*
452          * Our per-thread stacks need to be bigger than the default size,
453          * otherwise the MIME parser crashes on FreeBSD.
454          */
455         if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
456                 syslog(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(ret));
457                 pthread_attr_destroy(&attr);
458         }
459
460         /* now create the thread */
461         if (pthread_create(&SessThread, &attr, (void *(*)(void *)) worker_entry, NULL) != 0) {
462                 syslog(LOG_WARNING, "Can't create thread: %s\n", strerror(errno));
463         }
464
465         /* free up the attributes */
466         pthread_attr_destroy(&attr);
467 }
468
469
470 void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome) {
471         char dirbuffer[PATH_MAX] = "";
472         /* calculate all our path on a central place */
473         /* where to keep our config */
474
475 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
476         snprintf(SUBDIR,sizeof SUBDIR,  "%s%s%s%s%s%s%s", \
477                          (home&!relh)?webcitdir:basedir, \
478              ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
479              ((basedir!=webcitdir)&(home&!relh))?"/":"", \
480                          relhome, \
481              (relhome[0]!='\0')?"/":"",\
482                          dirbuffer,\
483                          (dirbuffer[0]!='\0')?"/":"");
484         basedir = RUNDIR;
485         COMPUTE_DIRECTORY(socket_dir);
486         basedir = WWWDIR "/static";
487         COMPUTE_DIRECTORY(static_dir);
488         basedir = WWWDIR "/static/icons";
489         COMPUTE_DIRECTORY(static_icon_dir);
490         basedir = WWWDIR "/static.local";
491         COMPUTE_DIRECTORY(static_local_dir);
492         StripSlashes(static_dir, 1);
493         StripSlashes(static_icon_dir, 1);
494         StripSlashes(static_local_dir, 1);
495
496         basedir = ETCDIR;
497         COMPUTE_DIRECTORY(etc_dir);
498         StripSlashes(etc_dir, 1);
499         snprintf(file_etc_mimelist, sizeof file_etc_mimelist, "%s/nogz-mimetypes.txt", etc_dir);
500
501         /* we should go somewhere we can leave our coredump, if enabled... */
502         syslog(LOG_INFO, "Changing directory to %s\n", socket_dir);
503         if (chdir(webcitdir) != 0) {
504                 perror("chdir");
505         }
506 }
507
508 void drop_root(uid_t UID) {
509         struct passwd pw, *pwp = NULL;
510 #ifdef HAVE_GETPWUID_R
511         char pwbuf[SIZ];
512 #endif
513
514         /*
515          * Now that we've bound the sockets, change to the Citadel user id and its
516          * corresponding group ids
517          */
518         if (UID != -1) {
519
520 #ifdef HAVE_GETPWUID_R
521 #ifdef SOLARIS_GETPWUID
522                 pwp = getpwuid_r(UID, &pw, pwbuf, sizeof(pwbuf));
523 #else                           /* SOLARIS_GETPWUID */
524                 getpwuid_r(UID, &pw, pwbuf, sizeof(pwbuf), &pwp);
525 #endif                          /* SOLARIS_GETPWUID */
526 #else                           /* HAVE_GETPWUID_R */
527                 pwp = NULL;
528 #endif                          /* HAVE_GETPWUID_R */
529
530                 if (pwp == NULL)
531                         syslog(LOG_CRIT, "WARNING: getpwuid(%d): %s\n" "Group IDs will be incorrect.\n", UID, strerror(errno));
532                 else {
533                         initgroups(pw.pw_name, pw.pw_gid);
534                         if (setgid(pw.pw_gid))
535                                 syslog(LOG_CRIT, "setgid(%ld): %s\n", (long) pw.pw_gid, strerror(errno));
536                 }
537                 syslog(LOG_INFO, "Changing uid to %ld\n", (long) UID);
538                 if (setuid(UID) != 0) {
539                         syslog(LOG_CRIT, "setuid() failed: %s\n", strerror(errno));
540                 }
541 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
542                 prctl(PR_SET_DUMPABLE, 1);
543 #endif
544         }
545 }
546
547
548 /*
549  * print the actual stack frame.
550  */
551 void wc_backtrace(long LogLevel) {
552 #ifdef HAVE_BACKTRACE
553         void *stack_frames[50];
554         size_t size, i;
555         char **strings;
556
557
558         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void *));
559         strings = backtrace_symbols(stack_frames, size);
560         for (i = 0; i < size; i++) {
561                 if (strings != NULL)
562                         syslog(LogLevel, "%s\n", strings[i]);
563                 else
564                         syslog(LogLevel, "%p\n", stack_frames[i]);
565         }
566         free(strings);
567 #endif
568 }