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