b8d190a8b254b1e742c8e5989b9db829d3461ea5
[citadel.git] / citadel / sysdep.c
1 /*
2  * Citadel "system dependent" stuff.
3  *
4  * Here's where we (hopefully) have most parts of the Citadel server that
5  * would need to be altered to run the server in a non-POSIX environment.
6  * 
7  * If we ever port to a different platform and either have multiple
8  * variants of this file or simply load it up with #ifdefs.
9  *
10  * Copyright (c) 1987-2011 by the citadel.org team
11  *
12  * This program is open source software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  */
26
27 #include "sysdep.h"
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #include <sys/socket.h>
38 #include <syslog.h>
39 #include <sys/syslog.h>
40
41 #if TIME_WITH_SYS_TIME
42 # include <sys/time.h>
43 # include <time.h>
44 #else
45 # if HAVE_SYS_TIME_H
46 #  include <sys/time.h>
47 # else
48 #  include <time.h>
49 # endif
50 #endif
51
52 #include <limits.h>
53 #include <sys/resource.h>
54 #include <netinet/in.h>
55 #include <netinet/tcp.h>
56 #include <arpa/inet.h>
57 #include <netdb.h>
58 #include <sys/un.h>
59 #include <string.h>
60 #include <pwd.h>
61 #include <errno.h>
62 #include <stdarg.h>
63 #include <grp.h>
64 #define SHOW_ME_VAPPEND_PRINTF
65 #include <libcitadel.h>
66 #include "citadel.h"
67 #include "server.h"
68 #include "sysdep_decls.h"
69 #include "citserver.h"
70 #include "support.h"
71 #include "config.h"
72 #include "database.h"
73 #include "housekeeping.h"
74 #include "modules/crypto/serv_crypto.h" /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
75 #include "ecrash.h"
76 #include "context.h"
77
78 #ifdef HAVE_SYS_SELECT_H
79 #include <sys/select.h>
80 #endif
81
82 #ifndef HAVE_SNPRINTF
83 #include "snprintf.h"
84 #endif
85
86 #include "ctdl_module.h"
87 #include "threads.h"
88 #include "user_ops.h"
89 #include "control.h"
90
91
92 /*
93  * Signal handler to shut down the server.
94  */
95
96 volatile int exit_signal = 0;
97 volatile int shutdown_and_halt = 0;
98 volatile int restart_server = 0;
99 volatile int running_as_daemon = 0;
100
101 static RETSIGTYPE signal_cleanup(int signum) {
102         syslog(LOG_DEBUG, "Caught signal %d; shutting down.", signum);
103         exit_signal = signum;
104         server_shutting_down = 1;
105 }
106
107 static RETSIGTYPE signal_exit(int signum) {
108         exit(1);
109 }
110
111
112
113 /*
114  * Some initialization stuff...
115  */
116 void init_sysdep(void) {
117         sigset_t set;
118
119         /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
120 #ifdef FD_SETSIZE
121 #ifdef RLIMIT_NOFILE
122         struct rlimit rl;
123         getrlimit(RLIMIT_NOFILE, &rl);
124         rl.rlim_cur = FD_SETSIZE;
125         rl.rlim_max = FD_SETSIZE;
126         setrlimit(RLIMIT_NOFILE, &rl);
127 #endif
128 #endif
129
130         /* If we've got OpenSSL, we're going to use it. */
131 #ifdef HAVE_OPENSSL
132         init_ssl();
133 #endif
134
135         /*
136          * Set up a place to put thread-specific data.
137          * We only need a single pointer per thread - it points to the
138          * CitContext structure (in the ContextList linked list) of the
139          * session to which the calling thread is currently bound.
140          */
141         if (pthread_key_create(&MyConKey, NULL) != 0) {
142                 syslog(LOG_CRIT, "Can't create TSD key: %s", strerror(errno));
143         }
144
145         /*
146          * Interript, hangup, and terminate signals should cause the server
147          * to gracefully clean up and shut down.
148          */
149         sigemptyset(&set);
150         sigaddset(&set, SIGINT);
151         sigaddset(&set, SIGHUP);
152         sigaddset(&set, SIGTERM);
153         sigprocmask(SIG_UNBLOCK, &set, NULL);
154
155         signal(SIGINT, signal_cleanup);
156         signal(SIGHUP, signal_cleanup);
157         signal(SIGTERM, signal_cleanup);
158         signal(SIGUSR2, signal_exit);
159
160         /*
161          * Do not shut down the server on broken pipe signals, otherwise the
162          * whole Citadel service would come down whenever a single client
163          * socket breaks.
164          */
165         signal(SIGPIPE, SIG_IGN);
166 }
167
168
169 /* 
170  * This is a generic function to set up a master socket for listening on
171  * a TCP port.  The server shuts down if the bind fails.  (IPv4/IPv6 version)
172  *
173  * ip_addr      IP address to bind
174  * port_number  port number to bind
175  * queue_len    number of incoming connections to allow in the queue
176  */
177 int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len, char *errormessage)
178 {
179         struct protoent *p;
180         struct sockaddr_in6 sin6;
181         struct sockaddr_in sin4;
182         int s, i, b;
183         int ip_version = 6;
184
185         memset(&sin6, 0, sizeof(sin6));
186         memset(&sin4, 0, sizeof(sin4));
187         sin6.sin6_family = AF_INET6;
188         sin4.sin_family = AF_INET;
189
190         if (    (ip_addr == NULL)                                                       /* any IPv6 */
191                 || (IsEmptyStr(ip_addr))
192                 || (!strcmp(ip_addr, "*"))
193         ) {
194                 ip_version = 6;
195                 sin6.sin6_addr = in6addr_any;
196         }
197         else if (!strcmp(ip_addr, "0.0.0.0"))                                           /* any IPv4 */
198         {
199                 ip_version = 4;
200                 sin4.sin_addr.s_addr = INADDR_ANY;
201         }
202         else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':')))                     /* specific IPv4 */
203         {
204                 ip_version = 4;
205                 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
206                         snprintf(errormessage, SIZ,
207                                  "Error binding to [%s] : %s", ip_addr, strerror(errno)
208                         );
209                         syslog(LOG_ALERT, "%s", errormessage);
210                         return (-1);
211                 }
212         }
213         else                                                                            /* specific IPv6 */
214         {
215                 ip_version = 6;
216                 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
217                         snprintf(errormessage, SIZ,
218                                  "Error binding to [%s] : %s", ip_addr, strerror(errno)
219                         );
220                         syslog(LOG_ALERT, "%s", errormessage);
221                         return (-1);
222                 }
223         }
224
225         if (port_number == 0) {
226                 snprintf(errormessage, SIZ, "Can't start: no port number specified.");
227                 syslog(LOG_ALERT, "%s", errormessage);
228                 return (-1);
229         }
230         sin6.sin6_port = htons((u_short) port_number);
231         sin4.sin_port = htons((u_short) port_number);
232
233         p = getprotobyname("tcp");
234
235         s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
236         if (s < 0) {
237                 snprintf(errormessage, SIZ,
238                          "Can't create a listening socket: %s", strerror(errno)
239                 );
240                 syslog(LOG_ALERT, "%s", errormessage);
241                 return (-1);
242         }
243         /* Set some socket options that make sense. */
244         i = 1;
245         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
246
247         if (ip_version == 6) {
248                 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
249         }
250         else {
251                 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
252         }
253
254         if (b < 0) {
255                 snprintf(errormessage, SIZ,
256                          "Can't bind: %s", strerror(errno)
257                 );
258                 syslog(LOG_ALERT, "%s", errormessage);
259                 return (-1);
260         }
261
262         fcntl(s, F_SETFL, O_NONBLOCK);
263
264         if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
265                 snprintf(errormessage, SIZ,
266                          "Can't listen: %s", strerror(errno)
267                 );
268                 syslog(LOG_ALERT, "%s", errormessage);
269                 return (-1);
270         }
271         return (s);
272 }
273
274
275
276
277
278 /*
279  * Create a Unix domain socket and listen on it
280  */
281 int ctdl_uds_server(char *sockpath, int queue_len, char *errormessage)
282 {
283         struct sockaddr_un addr;
284         int s;
285         int i;
286         int actual_queue_len;
287 #ifdef HAVE_STRUCT_UCRED
288         int passcred = 1;
289 #endif
290
291         actual_queue_len = queue_len;
292         if (actual_queue_len < 5) actual_queue_len = 5;
293
294         i = unlink(sockpath);
295         if ((i != 0) && (errno != ENOENT)) {
296                 snprintf(errormessage, SIZ, "citserver: can't unlink %s: %s",
297                         sockpath, strerror(errno)
298                 );
299                 syslog(LOG_EMERG, "%s", errormessage);
300                 return(-1);
301         }
302
303         memset(&addr, 0, sizeof(addr));
304         addr.sun_family = AF_UNIX;
305         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
306
307         s = socket(AF_UNIX, SOCK_STREAM, 0);
308         if (s < 0) {
309                 snprintf(errormessage, SIZ, 
310                          "citserver: Can't create a socket: %s",
311                          strerror(errno));
312                 syslog(LOG_EMERG, "%s", errormessage);
313                 return(-1);
314         }
315
316         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
317                 snprintf(errormessage, SIZ, 
318                          "citserver: Can't bind: %s",
319                          strerror(errno));
320                 syslog(LOG_EMERG, "%s", errormessage);
321                 return(-1);
322         }
323
324         /* set to nonblock - we need this for some obscure situations */
325         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
326                 snprintf(errormessage, SIZ, 
327                          "citserver: Can't set socket to non-blocking: %s",
328                          strerror(errno));
329                 syslog(LOG_EMERG, "%s", errormessage);
330                 close(s);
331                 return(-1);
332         }
333
334         if (listen(s, actual_queue_len) < 0) {
335                 snprintf(errormessage, SIZ, 
336                          "citserver: Can't listen: %s",
337                          strerror(errno));
338                 syslog(LOG_EMERG, "%s", errormessage);
339                 return(-1);
340         }
341
342 #ifdef HAVE_STRUCT_UCRED
343         setsockopt(s, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
344 #endif
345
346         chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
347         return(s);
348 }
349
350
351
352 /*
353  * The following functions implement output buffering on operating systems which
354  * support it (such as Linux and various BSD flavors).
355  */
356 #ifndef HAVE_DARWIN
357 #ifdef TCP_CORK
358 #       define HAVE_TCP_BUFFERING
359 #else
360 #       ifdef TCP_NOPUSH
361 #               define HAVE_TCP_BUFFERING
362 #               define TCP_CORK TCP_NOPUSH
363 #       endif
364 #endif /* TCP_CORK */
365 #endif /* HAVE_DARWIN */
366
367 static unsigned on = 1, off = 0;
368
369 void buffer_output(void) {
370 #ifdef HAVE_TCP_BUFFERING
371 #ifdef HAVE_OPENSSL
372         if (!CC->redirect_ssl)
373 #endif
374                 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
375 #endif
376 }
377
378 void unbuffer_output(void) {
379 #ifdef HAVE_TCP_BUFFERING
380 #ifdef HAVE_OPENSSL
381         if (!CC->redirect_ssl)
382 #endif
383                 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
384 #endif
385 }
386
387 void flush_output(void) {
388 #ifdef HAVE_TCP_BUFFERING
389         struct CitContext *CCC = CC;
390         setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
391         setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
392 #endif
393 }
394
395 /*
396 static void flush_client_inbuf(void)
397 {
398         CitContext *CCC=CC;
399
400         FlushStrBuf(CCC->ReadBuf);
401         CCC->RecvBuf->ReadWritePointer = NULL;
402
403 }
404 */
405
406
407 /*
408  * client_close()       ...     close the client socket
409  */
410 void client_close(void) {
411         CitContext *CCC = CC;
412
413         if (!CCC) return;
414         if (CCC->client_socket <= 0) return;
415         syslog(LOG_DEBUG, "Closing socket %d", CCC->client_socket);
416
417         close(CCC->client_socket);
418         CCC->client_socket = -1 ;
419 }
420
421
422
423
424 /*
425  * client_write()   ...    Send binary data to the client.
426  */
427 int client_write(const char *buf, int nbytes)
428 {
429         int bytes_written = 0;
430         int retval;
431 #ifndef HAVE_TCP_BUFFERING
432         int old_buffer_len = 0;
433 #endif
434         fd_set wset;
435         CitContext *Ctx;
436         int fdflags;
437
438         if (nbytes < 1) return(0);
439
440         Ctx = CC;
441
442 #ifdef BIGBAD_IODBG
443         {
444                 int rv = 0;
445                 char fn [SIZ];
446                 FILE *fd;
447                 
448                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", Ctx->ServiceName, Ctx->cs_pid);
449                 
450                 fd = fopen(fn, "a+");
451                 if (fd)
452                 {
453                     fprintf(fd, "Sending: BufSize: %d BufContent: [",
454                             nbytes);
455                     rv = fwrite(buf, nbytes, 1, fd);
456                     fprintf(fd, "]\n");
457                     fclose(fd);
458                 }
459         }
460 #endif
461 //      flush_client_inbuf();
462         if (Ctx->redirect_buffer != NULL) {
463                 StrBufAppendBufPlain(Ctx->redirect_buffer,
464                                      buf, nbytes, 0);
465                 return 0;
466         }
467
468 #ifdef HAVE_OPENSSL
469         if (Ctx->redirect_ssl) {
470                 client_write_ssl(buf, nbytes);
471                 return 0;
472         }
473 #endif
474         if (Ctx->client_socket == -1) return -1;
475
476         fdflags = fcntl(Ctx->client_socket, F_GETFL);
477
478         while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
479                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
480                         FD_ZERO(&wset);
481                         FD_SET(Ctx->client_socket, &wset);
482                         if (select(1, NULL, &wset, NULL, NULL) == -1) {
483                                 if (errno == EINTR)
484                                 {
485                                         syslog(LOG_DEBUG, "client_write(%d bytes) select() interrupted.",
486                                                 nbytes-bytes_written
487                                         );
488                                         if (server_shutting_down) {
489                                                 CC->kill_me = KILLME_SELECT_INTERRUPTED;
490                                                 return (-1);
491                                         } else {
492                                                 /* can't trust fd's and stuff so we need to re-create them */
493                                                 continue;
494                                         }
495                                 } else {
496                                         syslog(LOG_ERR,
497                                                 "client_write(%d bytes) select failed: %s (%d)",
498                                                 nbytes - bytes_written,
499                                                 strerror(errno), errno
500                                         );
501                                         cit_backtrace();
502                                         client_close();
503                                         Ctx->kill_me = KILLME_SELECT_FAILED;
504                                         return -1;
505                                 }
506                         }
507                 }
508
509                 retval = write(Ctx->client_socket, &buf[bytes_written], nbytes - bytes_written);
510                 if (retval < 1) {
511                         syslog(LOG_ERR,
512                                 "client_write(%d bytes) failed: %s (%d)",
513                                 nbytes - bytes_written,
514                                 strerror(errno), errno
515                         );
516                         cit_backtrace();
517                         client_close();
518                         Ctx->kill_me = KILLME_WRITE_FAILED;
519                         return -1;
520                 }
521                 bytes_written = bytes_written + retval;
522         }
523         return 0;
524 }
525
526 void cputbuf(const StrBuf *Buf) {   
527         client_write(ChrPtr(Buf), StrLength(Buf)); 
528 }   
529
530
531 /*
532  * cprintf()    Send formatted printable data to the client.
533  *              Implemented in terms of client_write() so it's technically not sysdep...
534  */
535 void cprintf(const char *format, ...) {   
536         va_list arg_ptr;   
537         char buf[1024];
538    
539         va_start(arg_ptr, format);   
540         if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
541                 buf[sizeof buf - 2] = '\n';
542         client_write(buf, strlen(buf)); 
543         va_end(arg_ptr);
544 }   
545
546
547 /*
548  * Read data from the client socket.
549  *
550  * sock         socket fd to read from
551  * buf          buffer to read into 
552  * bytes        number of bytes to read
553  * timeout      Number of seconds to wait before timing out
554  *
555  * Possible return values:
556  *      1       Requested number of bytes has been read.
557  *      0       Request timed out.
558  *      -1      Connection is broken, or other error.
559  */
560 int client_read_blob(StrBuf *Target, int bytes, int timeout)
561 {
562         CitContext *CCC=CC;
563         const char *Error;
564         int retval = 0;
565
566 #ifdef HAVE_OPENSSL
567         if (CCC->redirect_ssl) {
568 #ifdef BIGBAD_IODBG
569                 int rv = 0;
570                 char fn [SIZ];
571                 FILE *fd;
572                 
573                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
574                         
575                 fd = fopen(fn, "a+");
576                 fprintf(fd, "Reading BLOB: BufSize: %d ",
577                         bytes);
578                 rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
579                 fprintf(fd, "]\n");
580                 
581                         
582                 fclose(fd);
583 #endif
584                 retval = client_read_sslblob(Target, bytes, timeout);
585                 if (retval < 0) {
586                         syslog(LOG_CRIT, "client_read_blob() failed");
587                 }
588 #ifdef BIGBAD_IODBG
589                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
590                 
591                 fd = fopen(fn, "a+");
592                 fprintf(fd, "Read: %d BufContent: [",
593                         StrLength(Target));
594                 rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
595                 fprintf(fd, "]\n");
596                 
597                 
598                 fclose(fd);
599 #endif
600         }
601         else 
602 #endif
603         {
604 #ifdef BIGBAD_IODBG
605                 int rv = 0;
606                 char fn [SIZ];
607                 FILE *fd;
608                 
609                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
610                         
611                 fd = fopen(fn, "a+");
612                 fprintf(fd, "Reading BLOB: BufSize: %d ",
613                         bytes);
614                 rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
615                 fprintf(fd, "]\n");
616                 
617                         
618                 fclose(fd);
619 #endif
620                 retval = StrBufReadBLOBBuffered(Target, 
621                                                 CCC->RecvBuf.Buf,
622                                                 &CCC->RecvBuf.ReadWritePointer,
623                                                 &CCC->client_socket,
624                                                 1, 
625                                                 bytes,
626                                                 O_TERM,
627                                                 &Error);
628                 if (retval < 0) {
629                         syslog(LOG_CRIT, "client_read_blob() failed: %s", Error);
630                         client_close();
631                         return retval;
632                 }
633 #ifdef BIGBAD_IODBG
634                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
635                 
636                 fd = fopen(fn, "a+");
637                 fprintf(fd, "Read: %d BufContent: [",
638                         StrLength(Target));
639                 rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
640                 fprintf(fd, "]\n");
641                 fclose(fd);
642 #endif
643         }
644         return retval;
645 }
646
647
648 /*
649  * to make client_read_random_blob() more efficient, increase buffer size.
650  * just use in greeting function, else your buffer may be flushed
651  */
652 void client_set_inbound_buf(long N)
653 {
654         CitContext *CCC=CC;
655         FlushStrBuf(CCC->RecvBuf.Buf);
656         ReAdjustEmptyBuf(CCC->RecvBuf.Buf, N * SIZ, N * SIZ);
657 }
658
659 int client_read_random_blob(StrBuf *Target, int timeout)
660 {
661         CitContext *CCC=CC;
662         int rc;
663
664         rc =  client_read_blob(Target, 1, timeout);
665         if (rc > 0)
666         {
667                 long len;
668                 const char *pch;
669                 
670                 len = StrLength(CCC->RecvBuf.Buf);
671                 pch = ChrPtr(CCC->RecvBuf.Buf);
672
673                 if (len > 0)
674                 {
675                         if (CCC->RecvBuf.ReadWritePointer != NULL) {
676                                 len -= CCC->RecvBuf.ReadWritePointer - pch;
677                                 pch = CCC->RecvBuf.ReadWritePointer;
678                         }
679                         StrBufAppendBufPlain(Target, pch, len, 0);
680                         FlushStrBuf(CCC->RecvBuf.Buf);
681                         CCC->RecvBuf.ReadWritePointer = NULL;
682 #ifdef BIGBAD_IODBG
683                         {
684                                 int rv = 0;
685                                 char fn [SIZ];
686                                 FILE *fd;
687                         
688                                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
689                         
690                                 fd = fopen(fn, "a+");
691                                 fprintf(fd, "Read: BufSize: %d BufContent: [",
692                                         StrLength(Target));
693                                 rv = fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
694                                 fprintf(fd, "]\n");
695                         
696                         
697                                 fclose(fd);
698                         }
699 #endif
700         
701                         return StrLength(Target);
702                 }
703                 return rc;
704         }
705         else
706                 return rc;
707 }
708
709 int client_read_to(char *buf, int bytes, int timeout)
710 {
711         CitContext *CCC=CC;
712         int rc;
713
714         rc = client_read_blob(CCC->MigrateBuf, bytes, timeout);
715         if (rc < 0)
716         {
717                 *buf = '\0';
718                 return rc;
719         }
720         else
721         {
722                 memcpy(buf, 
723                        ChrPtr(CCC->MigrateBuf),
724                        StrLength(CCC->MigrateBuf) + 1);
725                 FlushStrBuf(CCC->MigrateBuf);
726                 return rc;
727         }
728 }
729
730
731 int HaveMoreLinesWaiting(CitContext *CCC)
732 {
733         if ((CCC->kill_me != 0) ||
734             ( (CCC->RecvBuf.ReadWritePointer == NULL) && 
735               (StrLength(CCC->RecvBuf.Buf) == 0) && 
736               (CCC->client_socket != -1)) )
737                 return 0;
738         else
739                 return 1;
740 }
741
742
743 /*
744  * Read data from the client socket with default timeout.
745  * (This is implemented in terms of client_read_to() and could be
746  * justifiably moved out of sysdep.c)
747  */
748 INLINE int client_read(char *buf, int bytes)
749 {
750         return(client_read_to(buf, bytes, config.c_sleeping));
751 }
752
753 int CtdlClientGetLine(StrBuf *Target)
754 {
755         CitContext *CCC=CC;
756         const char *Error;
757         int rc;
758
759         FlushStrBuf(Target);
760 #ifdef HAVE_OPENSSL
761         if (CCC->redirect_ssl) {
762 #ifdef BIGBAD_IODBG
763                 char fn [SIZ];
764                 FILE *fd;
765                 int len = 0;
766                 int rlen = 0;
767                 int  nlen = 0;
768                 int nrlen = 0;
769                 const char *pch;
770
771                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
772
773                 fd = fopen(fn, "a+");
774                 pch = ChrPtr(CCC->RecvBuf.Buf);
775                 len = StrLength(CCC->RecvBuf.Buf);
776                 if (CCC->RecvBuf.ReadWritePointer != NULL)
777                         rlen = CCC->RecvBuf.ReadWritePointer - pch;
778                 else
779                         rlen = 0;
780
781 /*              fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \nBufContent: [%s]\n\n_____________________\n",
782                         len, rlen, pch);
783 */
784                 fprintf(fd, "\n\n\nSSL1: BufSize: %d BufPos: %d \n_____________________\n",
785                         len, rlen);
786 #endif
787                 rc = client_readline_sslbuffer(Target,
788                                                CCC->RecvBuf.Buf,
789                                                &CCC->RecvBuf.ReadWritePointer,
790                                                1);
791 #ifdef BIGBAD_IODBG
792                 pch = ChrPtr(CCC->RecvBuf.Buf);
793                 nlen = StrLength(CCC->RecvBuf.Buf);
794                 if (CCC->RecvBuf.ReadWritePointer != NULL)
795                         nrlen = CCC->RecvBuf.ReadWritePointer - pch;
796                 else
797                         nrlen = 0;
798 /*
799                 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \nBufContent: [%s]\n\n_____________________\n",
800                         len, nlen, rlen, nrlen, pch);
801 */
802                 fprintf(fd, "\n\n\nSSL2: BufSize: was: %d is: %d BufPos: was: %d is: %d \n",
803                         len, nlen, rlen, nrlen);
804
805                 fprintf(fd, "SSL3: Read: BufSize: %d BufContent: [%s]\n\n*************\n",
806                         StrLength(Target), ChrPtr(Target));
807                 fclose(fd);
808
809                 if (rc < 0) {
810                         syslog(LOG_CRIT, "CtdlClientGetLine() failed");
811                 }
812 #endif
813                 return rc;
814         }
815         else 
816 #endif
817         {
818 #ifdef BIGBAD_IODBG
819                 char fn [SIZ];
820                 FILE *fd;
821                 int len, rlen, nlen, nrlen;
822                 const char *pch;
823
824                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
825
826                 fd = fopen(fn, "a+");
827                 pch = ChrPtr(CCC->RecvBuf.Buf);
828                 len = StrLength(CCC->RecvBuf.Buf);
829                 if (CCC->RecvBuf.ReadWritePointer != NULL)
830                         rlen = CCC->RecvBuf.ReadWritePointer - pch;
831                 else
832                         rlen = 0;
833
834 /*              fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \nBufContent: [%s]\n\n_____________________\n",
835                         len, rlen, pch);
836 */
837                 fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \n_____________________\n",
838                         len, rlen);
839 #endif
840                 rc = StrBufTCP_read_buffered_line_fast(Target, 
841                                                        CCC->RecvBuf.Buf,
842                                                        &CCC->RecvBuf.ReadWritePointer,
843                                                        &CCC->client_socket,
844                                                        5,
845                                                        1,
846                                                        &Error);
847
848 #ifdef BIGBAD_IODBG
849                 pch = ChrPtr(CCC->RecvBuf.Buf);
850                 nlen = StrLength(CCC->RecvBuf.Buf);
851                 if (CCC->RecvBuf.ReadWritePointer != NULL)
852                         nrlen = CCC->RecvBuf.ReadWritePointer - pch;
853                 else
854                         nrlen = 0;
855 /*
856                 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \nBufContent: [%s]\n\n_____________________\n",
857                         len, nlen, rlen, nrlen, pch);
858 */
859                 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \n",
860                         len, nlen, rlen, nrlen);
861
862                 fprintf(fd, "Read: BufSize: %d BufContent: [%s]\n\n*************\n",
863                         StrLength(Target), ChrPtr(Target));
864                 fclose(fd);
865
866                 if ((rc < 0) && (Error != NULL)) {
867                         syslog(LOG_CRIT, "CtdlClientGetLine() failed: %s", Error);
868                 }
869 #endif
870                 return rc;
871         }
872 }
873
874
875 /*
876  * client_getln()   ...   Get a LF-terminated line of text from the client.
877  * (This is implemented in terms of client_read() and could be
878  * justifiably moved out of sysdep.c)
879  */
880 int client_getln(char *buf, int bufsize)
881 {
882         int i, retval;
883         CitContext *CCC=CC;
884         const char *pCh;
885
886         retval = CtdlClientGetLine(CCC->MigrateBuf);
887         if (retval < 0)
888           return(retval >= 0);
889
890
891         i = StrLength(CCC->MigrateBuf);
892         pCh = ChrPtr(CCC->MigrateBuf);
893         /* Strip the trailing LF, and the trailing CR if present.
894          */
895         if (bufsize <= i)
896                 i = bufsize - 1;
897         while ( (i > 0)
898                 && ( (pCh[i - 1]==13)
899                      || ( pCh[i - 1]==10)) ) {
900                 i--;
901         }
902         memcpy(buf, pCh, i);
903         buf[i] = 0;
904
905         FlushStrBuf(CCC->MigrateBuf);
906         if (retval < 0) {
907                 safestrncpy(&buf[i], "000", bufsize - i);
908         }
909         return(retval >= 0);
910 }
911
912
913 /*
914  * Cleanup any contexts that are left lying around
915  */
916
917
918 void close_masters (void)
919 {
920         struct ServiceFunctionHook *serviceptr;
921         
922         /*
923          * close all protocol master sockets
924          */
925         for (serviceptr = ServiceHookTable; serviceptr != NULL;
926             serviceptr = serviceptr->next ) {
927
928                 if (serviceptr->tcp_port > 0)
929                 {
930                         syslog(LOG_INFO, "Closing %d listener on port %d\n",
931                                serviceptr->msock,
932                                serviceptr->tcp_port);
933                         serviceptr->tcp_port = 0;
934                 }
935                 
936                 if (serviceptr->sockpath != NULL)
937                         syslog(LOG_INFO, "Closing %d listener on '%s'\n",
938                                serviceptr->msock,
939                                serviceptr->sockpath);
940                 if (serviceptr->msock != -1)
941                         close(serviceptr->msock);
942                 /* If it's a Unix domain socket, remove the file. */
943                 if (serviceptr->sockpath != NULL) {
944                         unlink(serviceptr->sockpath);
945                         serviceptr->sockpath = NULL;
946                 }
947         }
948 }
949
950
951 /*
952  * The system-dependent part of master_cleanup() - close the master socket.
953  */
954 void sysdep_master_cleanup(void) {
955         
956         close_masters();
957         
958         context_cleanup();
959         
960 #ifdef HAVE_OPENSSL
961         destruct_ssl();
962 #endif
963         CtdlDestroyProtoHooks();
964         CtdlDestroyDeleteHooks();
965         CtdlDestroyXmsgHooks();
966         CtdlDestroyNetprocHooks();
967         CtdlDestroyUserHooks();
968         CtdlDestroyMessageHook();
969         CtdlDestroyCleanupHooks();
970         CtdlDestroyFixedOutputHooks();  
971         CtdlDestroySessionHooks();
972         CtdlDestroyServiceHook();
973         CtdlDestroyRoomHooks();
974         CtdlDestroySearchHooks();
975         CtdlDestroyDebugTable();
976         #ifdef HAVE_BACKTRACE
977 ///     eCrash_Uninit();
978         #endif
979 }
980
981
982
983 pid_t current_child;
984 void graceful_shutdown(int signum) {
985         kill(current_child, signum);
986         unlink(file_pid_file);
987         exit(0);
988 }
989
990 int nFireUps = 0;
991 int nFireUpsNonRestart = 0;
992 pid_t ForkedPid = 1;
993
994 /*
995  * Start running as a daemon.
996  */
997 void start_daemon(int unused) {
998         int status = 0;
999         pid_t child = 0;
1000         FILE *fp;
1001         int do_restart = 0;
1002
1003         current_child = 0;
1004
1005         /* Close stdin/stdout/stderr and replace them with /dev/null.
1006          * We don't just call close() because we don't want these fd's
1007          * to be reused for other files.
1008          */
1009         if (chdir(ctdl_run_dir) != 0)
1010                 syslog(LOG_EMERG, 
1011                               "unable to change into directory [%s]: %s", 
1012                               ctdl_run_dir, strerror(errno));
1013
1014         child = fork();
1015         if (child != 0) {
1016                 exit(0);
1017         }
1018         
1019         signal(SIGHUP, SIG_IGN);
1020         signal(SIGINT, SIG_IGN);
1021         signal(SIGQUIT, SIG_IGN);
1022
1023         setsid();
1024         umask(0);
1025         if ((freopen("/dev/null", "r", stdin) != stdin) || 
1026             (freopen("/dev/null", "w", stdout) != stdout) || 
1027             (freopen("/dev/null", "w", stderr) != stderr))
1028                 syslog(LOG_EMERG, 
1029                               "unable to reopen stdin/out/err %s", 
1030                               strerror(errno));
1031                 
1032
1033         do {
1034                 current_child = fork();
1035
1036                 signal(SIGTERM, graceful_shutdown);
1037         
1038                 if (current_child < 0) {
1039                         perror("fork");
1040                         exit(errno);
1041                 }
1042         
1043                 else if (current_child == 0) {
1044                         return; /* continue starting citadel. */
1045                 }
1046         
1047                 else {
1048                         fp = fopen(file_pid_file, "w");
1049                         if (fp != NULL) {
1050                                 fprintf(fp, ""F_PID_T"\n", getpid());
1051                                 fclose(fp);
1052                         }
1053                         waitpid(current_child, &status, 0);
1054                 }
1055                 do_restart = 0;
1056                 nFireUpsNonRestart = nFireUps;
1057                 
1058                 /* Exit code 0 means the watcher should exit */
1059                 if (WIFEXITED(status) && (WEXITSTATUS(status) == CTDLEXIT_SHUTDOWN)) {
1060                         do_restart = 0;
1061                 }
1062
1063                 /* Exit code 101-109 means the watcher should exit */
1064                 else if (WIFEXITED(status) && (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
1065                         do_restart = 0;
1066                 }
1067
1068                 /* Any other exit code, or no exit code, means we should restart. */
1069                 else {
1070                         do_restart = 1;
1071                         nFireUps++;
1072                         ForkedPid = current_child;
1073                 }
1074
1075         } while (do_restart);
1076
1077         unlink(file_pid_file);
1078         exit(WEXITSTATUS(status));
1079 }
1080
1081
1082
1083 void checkcrash(void)
1084 {
1085         if (nFireUpsNonRestart != nFireUps)
1086         {
1087                 StrBuf *CrashMail;
1088
1089                 CrashMail = NewStrBuf();
1090                 syslog(LOG_ALERT, "Posting crash message\n");
1091                 StrBufPrintf(CrashMail, 
1092                         " \n"
1093                         " The Citadel server process (citserver) terminated unexpectedly."
1094                         "\n \n"
1095                         " This could be the result of a bug in the server program, or some external "
1096                         "factor.\n \n"
1097                         " You can obtain more information about this by enabling core dumps.\n \n"
1098                         " For more information, please see:\n \n"
1099                         " http://citadel.org/doku.php?id=faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files"
1100                         "\n \n"
1101
1102                         " If you have already done this, the core dump is likely to be found at %score.%d\n"
1103                         ,
1104                         ctdl_run_dir, ForkedPid);
1105                 CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
1106                 FreeStrBuf(&CrashMail);
1107         }
1108 }
1109
1110
1111 /*
1112  * Generic routine to convert a login name to a full name (gecos)
1113  * Returns nonzero if a conversion took place
1114  */
1115 int convert_login(char NameToConvert[]) {
1116         struct passwd *pw;
1117         int a;
1118
1119         pw = getpwnam(NameToConvert);
1120         if (pw == NULL) {
1121                 return(0);
1122         }
1123         else {
1124                 strcpy(NameToConvert, pw->pw_gecos);
1125                 for (a=0; a<strlen(NameToConvert); ++a) {
1126                         if (NameToConvert[a] == ',') NameToConvert[a] = 0;
1127                 }
1128                 return(1);
1129         }
1130 }
1131
1132
1133
1134 /* 
1135  * This loop just keeps going and going and going...
1136  */
1137 void *worker_thread(void *blah) {
1138         int highest;
1139         CitContext *ptr;
1140         CitContext *bind_me = NULL;
1141         fd_set readfds;
1142         int retval = 0;
1143         struct timeval tv;
1144         int force_purge = 0;
1145         struct ServiceFunctionHook *serviceptr;
1146         int ssock;                      /* Descriptor for client socket */
1147         CitContext *con = NULL;         /* Temporary context pointer */
1148         int i;
1149
1150         ++num_workers;
1151
1152         while (!server_shutting_down) {
1153
1154                 /* make doubly sure we're not holding any stale db handles
1155                  * which might cause a deadlock.
1156                  */
1157                 cdb_check_handles();
1158 do_select:      force_purge = 0;
1159                 bind_me = NULL;         /* Which session shall we handle? */
1160
1161                 /* Initialize the fdset. */
1162                 FD_ZERO(&readfds);
1163                 highest = 0;
1164
1165                 /* First, add the various master sockets to the fdset. */
1166                 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
1167                         FD_SET(serviceptr->msock, &readfds);
1168                         if (serviceptr->msock > highest) {
1169                                 highest = serviceptr->msock;
1170                         }
1171                 }
1172
1173                 /* Next, add all of the client sockets. */
1174                 begin_critical_section(S_SESSION_TABLE);
1175                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1176                         if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
1177                             continue;
1178
1179                         /* Don't select on dead sessions, only truly idle ones */
1180                         if (    (ptr->state == CON_IDLE)
1181                                 && (ptr->kill_me == 0)
1182                                 && (ptr->client_socket > 0)
1183                         ) {
1184                                 FD_SET(ptr->client_socket, &readfds);
1185                                 if (ptr->client_socket > highest)
1186                                         highest = ptr->client_socket;
1187                         }
1188                         if ((bind_me == NULL) && (ptr->state == CON_READY)) {
1189                                 bind_me = ptr;
1190                                 ptr->state = CON_EXECUTING;
1191                                 break;
1192                         }
1193                         if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
1194                                 bind_me = ptr;
1195                                 ptr->state = CON_STARTING;
1196                                 break;
1197                         }
1198                 }
1199                 end_critical_section(S_SESSION_TABLE);
1200
1201                 if (bind_me) {
1202                         goto SKIP_SELECT;
1203                 }
1204
1205                 /* If we got this far, it means that there are no sessions
1206                  * which a previous thread marked for attention, so we go
1207                  * ahead and get ready to select().
1208                  */
1209
1210                 if (!server_shutting_down) {
1211                         tv.tv_sec = 1;          /* wake up every second if no input */
1212                         tv.tv_usec = 0;
1213                         retval = select(highest + 1, &readfds, NULL, NULL, &tv);
1214                 }
1215                 else {
1216                         --num_workers;
1217                         return NULL;
1218                 }
1219
1220                 /* Now figure out who made this select() unblock.
1221                  * First, check for an error or exit condition.
1222                  */
1223                 if (retval < 0) {
1224                         if (errno == EBADF) {
1225                                 syslog(LOG_NOTICE, "select() failed: (%s)\n", strerror(errno));
1226                                 goto do_select;
1227                         }
1228                         if (errno != EINTR) {
1229                                 syslog(LOG_EMERG, "Exiting (%s)\n", strerror(errno));
1230                                 server_shutting_down = 1;
1231                                 continue;
1232                         } else {
1233 #if 0
1234                                 syslog(LOG_DEBUG, "Interrupted select()\n");
1235 #endif
1236                                 if (server_shutting_down) {
1237                                         --num_workers;
1238                                         return(NULL);
1239                                 }
1240                                 goto do_select;
1241                         }
1242                 }
1243                 else if (retval == 0) {
1244                         if (server_shutting_down) {
1245                                 --num_workers;
1246                                 return(NULL);
1247                         }
1248                 }
1249
1250                 /* Next, check to see if it's a new client connecting * on a master socket. */
1251
1252                 else if ((retval > 0) && (!server_shutting_down)) for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next) {
1253
1254                         if (FD_ISSET(serviceptr->msock, &readfds)) {
1255                                 ssock = accept(serviceptr->msock, NULL, 0);
1256                                 if (ssock >= 0) {
1257                                         syslog(LOG_DEBUG, "New client socket %d", ssock);
1258
1259                                         /* The master socket is non-blocking but the client
1260                                          * sockets need to be blocking, otherwise certain
1261                                          * operations barf on FreeBSD.  Not a fatal error.
1262                                          */
1263                                         if (fcntl(ssock, F_SETFL, 0) < 0) {
1264                                                 syslog(LOG_EMERG,
1265                                                         "citserver: Can't set socket to blocking: %s\n",
1266                                                         strerror(errno));
1267                                         }
1268
1269                                         /* New context will be created already
1270                                          * set up in the CON_EXECUTING state.
1271                                          */
1272                                         con = CreateNewContext();
1273
1274                                         /* Assign our new socket number to it. */
1275                                         con->tcp_port = serviceptr->tcp_port;
1276                                         con->client_socket = ssock;
1277                                         con->h_command_function = serviceptr->h_command_function;
1278                                         con->h_async_function = serviceptr->h_async_function;
1279                                         con->h_greeting_function = serviceptr->h_greeting_function;
1280                                         con->ServiceName = serviceptr->ServiceName;
1281                                         
1282                                         /* Determine whether it's a local socket */
1283                                         if (serviceptr->sockpath != NULL) {
1284                                                 con->is_local_socket = 1;
1285                                         }
1286         
1287                                         /* Set the SO_REUSEADDR socket option */
1288                                         i = 1;
1289                                         setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
1290                                         con->state = CON_GREETING;
1291                                         retval--;
1292                                         if (retval == 0)
1293                                                 break;
1294                                 }
1295                         }
1296                 }
1297
1298                 /* It must be a client socket.  Find a context that has data
1299                  * waiting on its socket *and* is in the CON_IDLE state.  Any
1300                  * active sockets other than our chosen one are marked as
1301                  * CON_READY so the next thread that comes around can just bind
1302                  * to one without having to select() again.
1303                  */
1304                 begin_critical_section(S_SESSION_TABLE);
1305                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1306                         int checkfd = ptr->client_socket;
1307                         if ((checkfd != -1) && (ptr->state == CON_IDLE) ){
1308                                 if (FD_ISSET(checkfd, &readfds)) {
1309                                         ptr->input_waiting = 1;
1310                                         if (!bind_me) {
1311                                                 bind_me = ptr;  /* I choose you! */
1312                                                 bind_me->state = CON_EXECUTING;
1313                                         }
1314                                         else {
1315                                                 ptr->state = CON_READY;
1316                                         }
1317                                 } else if ((ptr->is_async) && (ptr->async_waiting) && (ptr->h_async_function)) {
1318                                         if (!bind_me) {
1319                                                 bind_me = ptr;  /* I choose you! */
1320                                                 bind_me->state = CON_EXECUTING;
1321                                         }
1322                                         else {
1323                                                 ptr->state = CON_READY;
1324                                         }
1325                                 }
1326                         }
1327                 }
1328                 end_critical_section(S_SESSION_TABLE);
1329
1330 SKIP_SELECT:
1331                 /* We're bound to a session */
1332                 ++active_workers;
1333                 if (bind_me != NULL) {
1334                         become_session(bind_me);
1335
1336                         if (bind_me->state == CON_STARTING) {
1337                                 bind_me->state = CON_EXECUTING;
1338                                 begin_session(bind_me);
1339                                 bind_me->h_greeting_function();
1340                         }
1341                         /* If the client has sent a command, execute it. */
1342                         if (CC->input_waiting) {
1343                                 CC->h_command_function();
1344
1345                                 while (HaveMoreLinesWaiting(CC))
1346                                        CC->h_command_function();
1347
1348                                 CC->input_waiting = 0;
1349                         }
1350
1351                         /* If there are asynchronous messages waiting and the
1352                          * client supports it, do those now */
1353                         if ((CC->is_async) && (CC->async_waiting)
1354                            && (CC->h_async_function != NULL)) {
1355                                 CC->h_async_function();
1356                                 CC->async_waiting = 0;
1357                         }
1358                         
1359                         force_purge = CC->kill_me;
1360                         become_session(NULL);
1361                         bind_me->state = CON_IDLE;
1362                 }
1363
1364                 dead_session_purge(force_purge);
1365                 do_housekeeping();
1366                 --active_workers;
1367         }
1368
1369         /* If control reaches this point, the server is shutting down */
1370         --num_workers;
1371         return(NULL);
1372 }
1373
1374
1375
1376 /*
1377  * SyslogFacility()
1378  * Translate text facility name to syslog.h defined value.
1379  */
1380 int SyslogFacility(char *name)
1381 {
1382         int i;
1383         struct
1384         {
1385                 int facility;
1386                 char *name;
1387         }   facTbl[] =
1388         {
1389                 {   LOG_KERN,   "kern"          },
1390                 {   LOG_USER,   "user"          },
1391                 {   LOG_MAIL,   "mail"          },
1392                 {   LOG_DAEMON, "daemon"        },
1393                 {   LOG_AUTH,   "auth"          },
1394                 {   LOG_SYSLOG, "syslog"        },
1395                 {   LOG_LPR,    "lpr"           },
1396                 {   LOG_NEWS,   "news"          },
1397                 {   LOG_UUCP,   "uucp"          },
1398                 {   LOG_LOCAL0, "local0"        },
1399                 {   LOG_LOCAL1, "local1"        },
1400                 {   LOG_LOCAL2, "local2"        },
1401                 {   LOG_LOCAL3, "local3"        },
1402                 {   LOG_LOCAL4, "local4"        },
1403                 {   LOG_LOCAL5, "local5"        },
1404                 {   LOG_LOCAL6, "local6"        },
1405                 {   LOG_LOCAL7, "local7"        },
1406                 {   0,            NULL          }
1407         };
1408         for(i = 0; facTbl[i].name != NULL; i++) {
1409                 if(!strcasecmp(name, facTbl[i].name))
1410                         return facTbl[i].facility;
1411         }
1412         return LOG_DAEMON;
1413 }