2 7e9b7812 2021-02-07 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 7e9b7812 2021-02-07 op * Permission to use, copy, modify, and distribute this software for any
5 7e9b7812 2021-02-07 op * purpose with or without fee is hereby granted, provided that the above
6 7e9b7812 2021-02-07 op * copyright notice and this permission notice appear in all copies.
8 7e9b7812 2021-02-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7e9b7812 2021-02-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7e9b7812 2021-02-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7e9b7812 2021-02-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7e9b7812 2021-02-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7e9b7812 2021-02-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7e9b7812 2021-02-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 d278a0c3 2021-02-23 op #include <sys/types.h>
20 d278a0c3 2021-02-23 op #include <sys/uio.h>
22 7e9b7812 2021-02-07 op #include <errno.h>
23 d278a0c3 2021-02-23 op #include <event.h>
24 d278a0c3 2021-02-23 op #include <imsg.h>
25 7e9b7812 2021-02-07 op #include <netdb.h>
26 7e9b7812 2021-02-07 op #include <stdarg.h>
27 7e9b7812 2021-02-07 op #include <stdio.h>
28 7e9b7812 2021-02-07 op #include <string.h>
29 7e9b7812 2021-02-07 op #include <syslog.h>
31 6b191ed5 2021-02-23 op static struct imsgbuf parent_ibuf, child_ibuf;
32 6b191ed5 2021-02-23 op static struct event inlog;
33 6b191ed5 2021-02-23 op static int logfd;
35 d278a0c3 2021-02-23 op static void handle_log(int, short, void*);
36 d278a0c3 2021-02-23 op static int logger_main(int, struct imsgbuf*);
39 7e9b7812 2021-02-07 op fatal(const char *fmt, ...)
43 7e9b7812 2021-02-07 op va_start(ap, fmt);
45 7e9b7812 2021-02-07 op if (conf.foreground) {
46 7e9b7812 2021-02-07 op vfprintf(stderr, fmt, ap);
47 7e9b7812 2021-02-07 op fprintf(stderr, "\n");
49 7e9b7812 2021-02-07 op vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
55 7e9b7812 2021-02-07 op static inline int
56 7e9b7812 2021-02-07 op should_log(int priority)
58 7e9b7812 2021-02-07 op switch (priority) {
61 7e9b7812 2021-02-07 op case LOG_WARNING:
63 7e9b7812 2021-02-07 op case LOG_NOTICE:
64 7e9b7812 2021-02-07 op return conf.verbose >= 1;
65 7e9b7812 2021-02-07 op case LOG_INFO:
66 7e9b7812 2021-02-07 op return conf.verbose >= 2;
67 7e9b7812 2021-02-07 op case LOG_DEBUG:
68 7e9b7812 2021-02-07 op return conf.verbose >= 3;
74 d278a0c3 2021-02-23 op static inline void
75 d278a0c3 2021-02-23 op send_log(const char *msg, size_t len)
77 d278a0c3 2021-02-23 op imsg_compose(&parent_ibuf, 0, 0, 0, -1, msg, len);
78 d278a0c3 2021-02-23 op /* XXX: use event_once() */
79 d278a0c3 2021-02-23 op imsg_flush(&parent_ibuf);
82 d278a0c3 2021-02-23 op static inline void
83 d278a0c3 2021-02-23 op vlog(int priority, struct client *c,
84 7e9b7812 2021-02-07 op const char *fmt, va_list ap)
86 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
87 7e9b7812 2021-02-07 op char *fmted, *s;
91 7e9b7812 2021-02-07 op if (!should_log(priority))
94 d278a0c3 2021-02-23 op if (c != NULL) {
95 7e9b7812 2021-02-07 op len = sizeof(c->addr);
96 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
97 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
98 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
99 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
101 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
104 7e9b7812 2021-02-07 op if (vasprintf(&fmted, fmt, ap) == -1)
105 7e9b7812 2021-02-07 op fatal("vasprintf: %s", strerror(errno));
107 d278a0c3 2021-02-23 op if (c == NULL)
108 d278a0c3 2021-02-23 op ec = asprintf(&s, "internal: %s", fmted);
110 d278a0c3 2021-02-23 op ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
113 d278a0c3 2021-02-23 op fatal("asprintf: %s", strerror(errno));
115 d278a0c3 2021-02-23 op send_log(s, ec+1);
122 7e9b7812 2021-02-07 op log_err(struct client *c, const char *fmt, ...)
126 7e9b7812 2021-02-07 op va_start(ap, fmt);
127 d278a0c3 2021-02-23 op vlog(LOG_ERR, c, fmt, ap);
132 7e9b7812 2021-02-07 op log_warn(struct client *c, const char *fmt, ...)
136 7e9b7812 2021-02-07 op va_start(ap, fmt);
137 d278a0c3 2021-02-23 op vlog(LOG_WARNING, c, fmt, ap);
142 7e9b7812 2021-02-07 op log_notice(struct client *c, const char *fmt, ...)
146 7e9b7812 2021-02-07 op va_start(ap, fmt);
147 d278a0c3 2021-02-23 op vlog(LOG_NOTICE, c, fmt, ap);
152 7e9b7812 2021-02-07 op log_info(struct client *c, const char *fmt, ...)
156 7e9b7812 2021-02-07 op va_start(ap, fmt);
157 d278a0c3 2021-02-23 op vlog(LOG_INFO, c, fmt, ap);
162 7e9b7812 2021-02-07 op log_debug(struct client *c, const char *fmt, ...)
166 7e9b7812 2021-02-07 op va_start(ap, fmt);
167 d278a0c3 2021-02-23 op vlog(LOG_DEBUG, c, fmt, ap);
171 7e9b7812 2021-02-07 op /* strchr, but with a bound */
172 7e9b7812 2021-02-07 op static char *
173 7e9b7812 2021-02-07 op gmid_strnchr(char *s, int c, size_t len)
177 7e9b7812 2021-02-07 op for (i = 0; i < len; ++i)
178 7e9b7812 2021-02-07 op if (s[i] == c)
179 7e9b7812 2021-02-07 op return &s[i];
184 7e9b7812 2021-02-07 op log_request(struct client *c, char *meta, size_t l)
186 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
187 d278a0c3 2021-02-23 op char *t, *fmted;
191 793835cb 2021-02-23 op if (vhost_disable_log(c->host, c->iri.path))
194 7e9b7812 2021-02-07 op len = sizeof(c->addr);
195 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
196 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
197 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
198 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
200 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
202 7e9b7812 2021-02-07 op if (c->iri.schema != NULL) {
203 7e9b7812 2021-02-07 op /* serialize the IRI */
204 7e9b7812 2021-02-07 op strlcpy(b, c->iri.schema, sizeof(b));
205 7e9b7812 2021-02-07 op strlcat(b, "://", sizeof(b));
207 7e9b7812 2021-02-07 op /* log the decoded host name, but if it was invalid
208 7e9b7812 2021-02-07 op * use the raw one. */
209 7e9b7812 2021-02-07 op if (*c->domain != '\0')
210 7e9b7812 2021-02-07 op strlcat(b, c->domain, sizeof(b));
212 7e9b7812 2021-02-07 op strlcat(b, c->iri.host, sizeof(b));
214 7e9b7812 2021-02-07 op strlcat(b, "/", sizeof(b));
215 7e9b7812 2021-02-07 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
216 7e9b7812 2021-02-07 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
217 7e9b7812 2021-02-07 op strlcat(b, "?", sizeof(b));
218 7e9b7812 2021-02-07 op strlcat(b, c->iri.query, sizeof(b));
221 7e9b7812 2021-02-07 op strlcpy(b, c->req, sizeof(b));
224 7e9b7812 2021-02-07 op if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
225 7e9b7812 2021-02-07 op t = meta + len;
227 d278a0c3 2021-02-23 op ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
228 d278a0c3 2021-02-23 op (int)(t-meta), meta);
230 d278a0c3 2021-02-23 op err(1, "asprintf");
231 d278a0c3 2021-02-23 op send_log(fmted, ec+1);
238 d278a0c3 2021-02-23 op handle_log(int fd, short ev, void *d)
240 d278a0c3 2021-02-23 op struct imsgbuf *ibuf = d;
241 d278a0c3 2021-02-23 op struct imsg imsg;
242 d278a0c3 2021-02-23 op ssize_t n, datalen;
245 d278a0c3 2021-02-23 op if ((n = imsg_read(ibuf)) == -1) {
246 d278a0c3 2021-02-23 op if (errno == EAGAIN || errno == EWOULDBLOCK)
248 d278a0c3 2021-02-23 op err(1, "imsg_read");
251 d278a0c3 2021-02-23 op errx(1, "connection lost?");
254 d278a0c3 2021-02-23 op if ((n = imsg_get(ibuf, &imsg)) == -1)
255 d278a0c3 2021-02-23 op err(1, "read error");
259 d278a0c3 2021-02-23 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
260 d278a0c3 2021-02-23 op msg = imsg.data;
261 d278a0c3 2021-02-23 op msg[datalen] = '\0';
263 d278a0c3 2021-02-23 op /* ignore imsg.hdr.type for now */
264 d278a0c3 2021-02-23 op if (conf.foreground)
265 d278a0c3 2021-02-23 op fprintf(stderr, "%s\n", msg);
267 d278a0c3 2021-02-23 op syslog(LOG_DAEMON, "%s", msg);
269 d278a0c3 2021-02-23 op imsg_free(&imsg);
274 d278a0c3 2021-02-23 op logger_main(int fd, struct imsgbuf *ibuf)
276 d278a0c3 2021-02-23 op event_init();
278 d278a0c3 2021-02-23 op event_set(&inlog, fd, EV_READ | EV_PERSIST, &handle_log, ibuf);
279 d278a0c3 2021-02-23 op event_add(&inlog, NULL);
281 d278a0c3 2021-02-23 op #ifdef __OpenBSD__
282 d278a0c3 2021-02-23 op if (pledge("stdio", NULL) == -1)
283 d278a0c3 2021-02-23 op err(1, "pledge");
286 d278a0c3 2021-02-23 op event_dispatch();
292 d278a0c3 2021-02-23 op logger_init(void)
296 d278a0c3 2021-02-23 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
297 d278a0c3 2021-02-23 op err(1, "socketpair");
299 d278a0c3 2021-02-23 op switch (fork()) {
301 d278a0c3 2021-02-23 op err(1, "fork");
303 d278a0c3 2021-02-23 op logfd = p[1];
305 d278a0c3 2021-02-23 op setproctitle("logger");
306 d278a0c3 2021-02-23 op imsg_init(&child_ibuf, p[1]);
308 d278a0c3 2021-02-23 op _exit(logger_main(p[1], &child_ibuf));
310 d278a0c3 2021-02-23 op logfd = p[0];
312 d278a0c3 2021-02-23 op imsg_init(&parent_ibuf, p[0]);