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