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