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