00fafbbed19a4d4510584cff1749ab76df4963b2
[citadel.git] / webcit / sysdep.c
1 /*
2  * $Id$
3  *
4  * Citadel "system dependent" stuff.
5  * See copyright.txt for copyright information.
6  *
7  * Here's where we (hopefully) have most parts of the Citadel server that
8  * would need to be altered to run the server in a non-POSIX environment.
9  * 
10  * If we ever port to a different platform and either have multiple
11  * variants of this file or simply load it up with #ifdefs.
12  *
13  */
14 #include "sysdep.h"
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <fcntl.h>
19 #include <ctype.h>
20 #include <signal.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/wait.h>
24 #include <sys/socket.h>
25 #include <syslog.h>
26 #include <sys/syslog.h>
27
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
30 # include <time.h>
31 #else
32 # if HAVE_SYS_TIME_H
33 #  include <sys/time.h>
34 # else
35 #  include <time.h>
36 # endif
37 #endif
38
39 #include <limits.h>
40 #include <sys/resource.h>
41 #include <netinet/in.h>
42 #include <netinet/tcp.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45 #include <sys/un.h>
46 #include <string.h>
47 #include <pwd.h>
48 #include <errno.h>
49 #include <stdarg.h>
50 #include <grp.h>
51 #ifdef HAVE_PTHREAD_H
52 #include <pthread.h>
53 #endif
54 #include "webcit.h"
55 #include "sysdep.h"
56
57 #ifdef HAVE_SYS_SELECT_H
58 #include <sys/select.h>
59 #endif
60
61 #ifndef HAVE_SNPRINTF
62 #include "snprintf.h"
63 #endif
64 #include "webserver.h"
65 #include "modules_init.h"
66 #if HAVE_BACKTRACE
67 #include <execinfo.h>
68 #endif
69
70 pthread_mutex_t Critters[MAX_SEMAPHORES];       /* Things needing locking */
71 pthread_key_t MyConKey;                         /* TSD key for MyContext() */
72 pthread_key_t MyReq;                            /* TSD key for MyReq() */
73 int msock;                      /* master listening socket */
74 int time_to_die = 0;            /* Nonzero if server is shutting down */
75 int verbosity = 9;              /* Logging level */
76
77 extern void *context_loop(ParsedHttpHdrs *Hdr);
78 extern void *housekeeping_loop(void);
79
80 char ctdl_key_dir[PATH_MAX]=SSL_DIR;
81 char file_crpt_file_key[PATH_MAX]="";
82 char file_crpt_file_csr[PATH_MAX]="";
83 char file_crpt_file_cer[PATH_MAX]="";
84
85 const char editor_absolut_dir[PATH_MAX]=EDITORDIR;      /* nailed to what configure gives us. */
86 char static_dir[PATH_MAX];              /* calculated on startup */
87 char static_local_dir[PATH_MAX];                /* calculated on startup */
88 char static_icon_dir[PATH_MAX];          /* where should we find our mime icons? */
89 char  *static_dirs[]={                          /* needs same sort order as the web mapping */
90         (char*)static_dir,                      /* our templates on disk */
91         (char*)static_local_dir,                /* user provided templates disk */
92         (char*)editor_absolut_dir,              /* the editor on disk */
93         (char*)static_icon_dir                  /* our icons... */
94 };
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
106 /*
107  * Obtain a semaphore lock to begin a critical section.
108  */
109 void begin_critical_section(int which_one)
110 {
111         /* lprintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
112         pthread_mutex_lock(&Critters[which_one]);
113 }
114
115 /*
116  * Release a semaphore lock to end a critical section.
117  */
118 void end_critical_section(int which_one)
119 {
120         pthread_mutex_unlock(&Critters[which_one]);
121 }
122
123
124 void ShutDownWebcit(void)
125 {
126         free_zone_directory ();
127         icaltimezone_release_zone_tab ();
128         icalmemory_free_ring ();
129         ShutDownLibCitadel ();
130         shutdown_modules ();
131 #ifdef HAVE_OPENSSL
132         if (is_https) {
133                 shutdown_ssl();
134         }
135 #endif
136 }
137
138 /*
139  * Entry point for worker threads
140  */
141 void worker_entry(void)
142 {
143         int ssock;
144         int i = 0;
145         int fail_this_transaction = 0;
146         int ret;
147         struct timeval tv;
148         fd_set readset, tempset;
149         ParsedHttpHdrs Hdr;
150
151         memset(&Hdr, 0, sizeof(ParsedHttpHdrs));
152         Hdr.HR.eReqType = eGET;
153         http_new_modules(&Hdr); 
154         tv.tv_sec = 0;
155         tv.tv_usec = 10000;
156         FD_ZERO(&readset);
157         FD_SET(msock, &readset);
158
159         do {
160                 /* Only one thread can accept at a time */
161                 fail_this_transaction = 0;
162                 ssock = -1; 
163                 errno = EAGAIN;
164                 do {
165                         ret = -1; /* just one at once should select... */
166                         begin_critical_section(S_SELECT);
167
168                         FD_ZERO(&tempset);
169                         if (msock > 0) FD_SET(msock, &tempset);
170                         tv.tv_sec = 0;
171                         tv.tv_usec = 10000;
172                         if (msock > 0)  ret = select(msock+1, &tempset, NULL, NULL,  &tv);
173                         end_critical_section(S_SELECT);
174                         if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
175                         {/* EINTR and EAGAIN are thrown but not of interest. */
176                                 lprintf(2, "accept() failed:%d %s\n",
177                                         errno, strerror(errno));
178                         }
179                         else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
180                         {/* Successfully selected, and still not shutting down? Accept! */
181                                 ssock = accept(msock, NULL, 0);
182                         }
183                         
184                 } while ((msock > 0) && (ssock < 0)  && (time_to_die == 0));
185
186                 if ((msock == -1)||(time_to_die))
187                 {/* ok, we're going down. */
188                         int shutdown = 0;
189
190                         /* the first to come here will have to do the cleanup.
191                          * make shure its realy just one.
192                          */
193                         begin_critical_section(S_SHUTDOWN);
194                         if (msock == -1)
195                         {
196                                 msock = -2;
197                                 shutdown = 1;
198                         }
199                         end_critical_section(S_SHUTDOWN);
200                         if (shutdown == 1)
201                         {/* we're the one to cleanup the mess. */
202                                 http_destroy_modules(&Hdr);
203                                 lprintf(2, "I'm master shutdown: tagging sessions to be killed.\n");
204                                 shutdown_sessions();
205                                 lprintf(2, "master shutdown: waiting for others\n");
206                                 sleeeeeeeeeep(1); /* wait so some others might finish... */
207                                 lprintf(2, "master shutdown: cleaning up sessions\n");
208                                 do_housekeeping();
209                                 lprintf(2, "master shutdown: cleaning up libical\n");
210
211                                 ShutDownWebcit();
212
213                                 lprintf(2, "master shutdown exiting!.\n");                              
214                                 exit(0);
215                         }
216                         break;
217                 }
218                 if (ssock < 0 ) continue;
219
220                 if (msock < 0) {
221                         if (ssock > 0) close (ssock);
222                         lprintf(2, "inbetween.");
223                         pthread_exit(NULL);
224                 } else { /* Got it? do some real work! */
225                         /* Set the SO_REUSEADDR socket option */
226                         i = 1;
227                         setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
228                                    &i, sizeof(i));
229
230                         /* If we are an HTTPS server, go crypto now. */
231 #ifdef HAVE_OPENSSL
232                         if (is_https) {
233                                 if (starttls(ssock) != 0) {
234                                         fail_this_transaction = 1;
235                                         close(ssock);
236                                 }
237                         }
238                         else 
239 #endif
240                         {
241                                 int fdflags; 
242                                 fdflags = fcntl(ssock, F_GETFL);
243                                 if (fdflags < 0)
244                                         lprintf(1, "unable to get server socket flags! %s \n",
245                                                 strerror(errno));
246                                 fdflags = fdflags | O_NONBLOCK;
247                                 if (fcntl(ssock, F_SETFL, fdflags) < 0)
248                                         lprintf(1, "unable to set server socket nonblocking flags! %s \n",
249                                                 strerror(errno));
250                         }
251
252                         if (fail_this_transaction == 0) {
253                                 Hdr.http_sock = ssock;
254
255                                 /* Perform an HTTP transaction... */
256                                 context_loop(&Hdr);
257
258                                 /* Shut down SSL/TLS if required... */
259 #ifdef HAVE_OPENSSL
260                                 if (is_https) {
261                                         endtls();
262                                 }
263 #endif
264
265                                 /* ...and close the socket. */
266                                 if (Hdr.http_sock > 0)
267                                         lingering_close(ssock);
268                                 http_detach_modules(&Hdr);
269
270                         }
271
272                 }
273
274         } while (!time_to_die);
275
276         http_destroy_modules(&Hdr);
277         lprintf (1, "bye\n");
278         pthread_exit(NULL);
279 }
280
281 /*
282  * print log messages 
283  * logs to stderr if loglevel is lower than the verbosity set at startup
284  *
285  * loglevel     level of the message
286  * format       the printf like format string
287  * ...          the strings to put into format
288  */
289 int lprintf(int loglevel, const char *format, ...)
290 {
291         va_list ap;
292
293         if (loglevel <= verbosity) {
294                 va_start(ap, format);
295                 vfprintf(stderr, format, ap);
296                 va_end(ap);
297                 fflush(stderr);
298         }
299         return 1;
300 }
301
302 /*
303  * Shut us down the regular way.
304  * signum is the signal we want to forward
305  */
306 pid_t current_child;
307 void graceful_shutdown_watcher(int signum) {
308         lprintf (1, "bye; shutting down watcher.");
309         kill(current_child, signum);
310         if (signum != SIGHUP)
311                 exit(0);
312 }
313
314
315
316
317 /*
318  * Shut us down the regular way.
319  * signum is the signal we want to forward
320  */
321 pid_t current_child;
322 void graceful_shutdown(int signum) {
323         FILE *FD;
324         int fd;
325
326         lprintf (1, "WebCit is being shut down on signal %d.\n", signum);
327         fd = msock;
328         msock = -1;
329         time_to_die = 1;
330         FD=fdopen(fd, "a+");
331         fflush (FD);
332         fclose (FD);
333         close(fd);
334 }
335
336
337 /*
338  * Start running as a daemon.
339  */
340 void start_daemon(char *pid_file) 
341 {
342         int status = 0;
343         pid_t child = 0;
344         FILE *fp;
345         int do_restart = 0;
346         int rv;
347         FILE *rvfp = NULL;
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         rv = 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         rvfp = freopen("/dev/null", "r", stdin);
369         rvfp = freopen("/dev/null", "w", stdout);
370         rvfp = 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  * Spawn an additional worker thread into the pool.
437  */
438 void spawn_another_worker_thread()
439 {
440         pthread_t SessThread;   /* Thread descriptor */
441         pthread_attr_t attr;    /* Thread attributes */
442         int ret;
443
444         lprintf(3, "Creating a new thread.  Pool size is now %d\n", ++num_threads);
445
446         /* set attributes for the new thread */
447         pthread_attr_init(&attr);
448         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
449
450         /*
451          * Our per-thread stacks need to be bigger than the default size,
452          * otherwise the MIME parser crashes on FreeBSD.
453          */
454         if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
455                 lprintf(1, "pthread_attr_setstacksize: %s\n",
456                         strerror(ret));
457                 pthread_attr_destroy(&attr);
458         }
459
460         /* now create the thread */
461         if (pthread_create(&SessThread, &attr,
462                            (void *(*)(void *)) worker_entry, NULL)
463             != 0) {
464                 lprintf(1, "Can't create thread: %s\n", strerror(errno));
465         }
466
467         /* free up the attributes */
468         pthread_attr_destroy(&attr);
469 }
470
471
472 void
473 webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome)
474 {
475         char dirbuffer[PATH_MAX]="";
476         /* calculate all our path on a central place */
477     /* where to keep our config */
478         
479 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
480         snprintf(SUBDIR,sizeof SUBDIR,  "%s%s%s%s%s%s%s", \
481                          (home&!relh)?webcitdir:basedir, \
482              ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
483              ((basedir!=webcitdir)&(home&!relh))?"/":"", \
484                          relhome, \
485              (relhome[0]!='\0')?"/":"",\
486                          dirbuffer,\
487                          (dirbuffer[0]!='\0')?"/":"");
488         basedir=RUNDIR;
489         COMPUTE_DIRECTORY(socket_dir);
490         basedir=WWWDIR "/static";
491         COMPUTE_DIRECTORY(static_dir);
492         basedir=WWWDIR "/static/icons";
493         COMPUTE_DIRECTORY(static_icon_dir);
494         basedir=WWWDIR "/static.local";
495         COMPUTE_DIRECTORY(static_local_dir);
496         StripSlashes(static_dir, 1);
497         StripSlashes(static_icon_dir, 1);
498         StripSlashes(static_local_dir, 1);
499
500         snprintf(file_crpt_file_key,
501                  sizeof file_crpt_file_key, 
502                  "%s/citadel.key",
503                  ctdl_key_dir);
504         snprintf(file_crpt_file_csr,
505                  sizeof file_crpt_file_csr, 
506                  "%s/citadel.csr",
507                  ctdl_key_dir);
508         snprintf(file_crpt_file_cer,
509                  sizeof file_crpt_file_cer, 
510                  "%s/citadel.cer",
511                  ctdl_key_dir);
512
513         /* we should go somewhere we can leave our coredump, if enabled... */
514         lprintf(9, "Changing directory to %s\n", socket_dir);
515         if (chdir(webcitdir) != 0) {
516                 perror("chdir");
517         }
518 }
519 void drop_root(uid_t UID)
520 {
521         struct passwd pw, *pwp = NULL;
522
523         /*
524          * Now that we've bound the sockets, change to the Citadel user id and its
525          * corresponding group ids
526          */
527         if (UID != -1) {
528                 
529 #ifdef HAVE_GETPWUID_R
530 #ifdef SOLARIS_GETPWUID
531                 pwp = getpwuid_r(UID, &pw, pwbuf, sizeof(pwbuf));
532 #else // SOLARIS_GETPWUID
533                 getpwuid_r(UID, &pw, pwbuf, sizeof(pwbuf), &pwp);
534 #endif // SOLARIS_GETPWUID
535 #else // HAVE_GETPWUID_R
536                 pwp = NULL;
537 #endif // HAVE_GETPWUID_R
538
539                 if (pwp == NULL)
540                         lprintf(CTDL_CRIT, "WARNING: getpwuid(%ld): %s\n"
541                                 "Group IDs will be incorrect.\n", UID,
542                                 strerror(errno));
543                 else {
544                         initgroups(pw.pw_name, pw.pw_gid);
545                         if (setgid(pw.pw_gid))
546                                 lprintf(CTDL_CRIT, "setgid(%ld): %s\n", (long)pw.pw_gid,
547                                         strerror(errno));
548                 }
549                 lprintf(CTDL_INFO, "Changing uid to %ld\n", (long)UID);
550                 if (setuid(UID) != 0) {
551                         lprintf(CTDL_CRIT, "setuid() failed: %s\n", strerror(errno));
552                 }
553 #if defined (HAVE_SYS_PRCTL_H) && defined (PR_SET_DUMPABLE)
554                 prctl(PR_SET_DUMPABLE, 1);
555 #endif
556         }
557 }
558
559
560 /*
561  * print the actual stack frame.
562  */
563 void wc_backtrace(void)
564 {
565 #ifdef HAVE_BACKTRACE
566         void *stack_frames[50];
567         size_t size, i;
568         char **strings;
569
570
571         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
572         strings = backtrace_symbols(stack_frames, size);
573         for (i = 0; i < size; i++) {
574                 if (strings != NULL)
575                         lprintf(1, "%s\n", strings[i]);
576                 else
577                         lprintf(1, "%p\n", stack_frames[i]);
578         }
579         free(strings);
580 #endif
581 }