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