Blame


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