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