Blob


1 /*
2 * Copyright (c) 2021, 2023 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "gmid.h"
19 #include <sys/types.h>
20 #include <sys/uio.h>
22 #include <errno.h>
23 #include <event.h>
24 #include <imsg.h>
25 #include <netdb.h>
26 #include <poll.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <time.h>
33 #include "log.h"
34 #include "proc.h"
36 #ifndef nitems
37 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
38 #endif
40 static int logfd = -1;
41 static int log_to_syslog = 1;
43 static void logger_init(struct privsep *, struct privsep_proc *, void *);
44 static void logger_shutdown(void);
45 static int logger_dispatch_parent(int, struct privsep_proc *, struct imsg *);
46 static int logger_dispatch_server(int, struct privsep_proc *, struct imsg *);
48 static struct privsep_proc procs[] = {
49 { "parent", PROC_PARENT, logger_dispatch_parent },
50 { "server", PROC_SERVER, logger_dispatch_server },
51 };
53 void
54 logger(struct privsep *ps, struct privsep_proc *p)
55 {
56 proc_run(ps, p, procs, nitems(procs), logger_init, NULL);
57 }
59 static void
60 logger_init(struct privsep *ps, struct privsep_proc *p, void *arg)
61 {
62 p->p_shutdown = logger_shutdown;
63 sandbox_logger_process();
64 }
66 static void
67 logger_shutdown(void)
68 {
69 closelog();
70 if (logfd != -1)
71 close(logfd);
72 }
74 static int
75 logger_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
76 {
77 switch (imsg->hdr.type) {
78 case IMSG_LOG_SYSLOG:
79 if (IMSG_DATA_SIZE(imsg) != sizeof(log_to_syslog))
80 fatal("corrupted IMSG_LOG_SYSLOG");
81 memcpy(&log_to_syslog, imsg->data, sizeof(log_to_syslog));
82 break;
83 case IMSG_LOG_ACCESS:
84 if (logfd != -1)
85 close(logfd);
86 logfd = -1;
88 if (imsg->fd != -1)
89 logfd = imsg->fd;
90 break;
91 default:
92 return -1;
93 }
95 return 0;
96 }
98 static int
99 logger_dispatch_server(int fd, struct privsep_proc *p, struct imsg *imsg)
101 char *msg;
102 size_t datalen;
104 switch (imsg->hdr.type) {
105 case IMSG_LOG_REQUEST:
106 msg = imsg->data;
107 datalen = IMSG_DATA_SIZE(imsg);
108 if (datalen == 0)
109 fatal("got invalid IMSG_LOG_REQUEST");
110 msg[datalen - 1] = '\0';
111 if (logfd != -1)
112 dprintf(logfd, "%s\n", msg);
113 if (log_to_syslog)
114 syslog(LOG_DAEMON | LOG_NOTICE, "%s", msg);
115 break;
116 default:
117 return -1;
120 return 0;