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 event inlog;
33 d278a0c3 2021-02-23 op static void handle_log(int, short, void*);
36 7e9b7812 2021-02-07 op fatal(const char *fmt, ...)
40 7e9b7812 2021-02-07 op va_start(ap, fmt);
42 7e9b7812 2021-02-07 op if (conf.foreground) {
43 7e9b7812 2021-02-07 op vfprintf(stderr, fmt, ap);
44 7e9b7812 2021-02-07 op fprintf(stderr, "\n");
46 7e9b7812 2021-02-07 op vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
52 7e9b7812 2021-02-07 op static inline int
53 7e9b7812 2021-02-07 op should_log(int priority)
55 7e9b7812 2021-02-07 op switch (priority) {
58 7e9b7812 2021-02-07 op case LOG_WARNING:
60 7e9b7812 2021-02-07 op case LOG_NOTICE:
61 7e9b7812 2021-02-07 op return conf.verbose >= 1;
62 7e9b7812 2021-02-07 op case LOG_INFO:
63 7e9b7812 2021-02-07 op return conf.verbose >= 2;
64 7e9b7812 2021-02-07 op case LOG_DEBUG:
65 7e9b7812 2021-02-07 op return conf.verbose >= 3;
71 d278a0c3 2021-02-23 op static inline void
72 d278a0c3 2021-02-23 op send_log(const char *msg, size_t len)
74 376a5407 2021-02-23 op imsg_compose(&logpibuf, 0, 0, 0, -1, msg, len);
75 d278a0c3 2021-02-23 op /* XXX: use event_once() */
76 376a5407 2021-02-23 op imsg_flush(&logpibuf);
79 d278a0c3 2021-02-23 op static inline void
80 d278a0c3 2021-02-23 op vlog(int priority, struct client *c,
81 7e9b7812 2021-02-07 op const char *fmt, va_list ap)
83 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
84 7e9b7812 2021-02-07 op char *fmted, *s;
88 7e9b7812 2021-02-07 op if (!should_log(priority))
91 d278a0c3 2021-02-23 op if (c != NULL) {
92 7e9b7812 2021-02-07 op len = sizeof(c->addr);
93 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
94 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
95 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
96 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
98 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
101 7e9b7812 2021-02-07 op if (vasprintf(&fmted, fmt, ap) == -1)
102 7e9b7812 2021-02-07 op fatal("vasprintf: %s", strerror(errno));
104 d278a0c3 2021-02-23 op if (c == NULL)
105 d278a0c3 2021-02-23 op ec = asprintf(&s, "internal: %s", fmted);
107 d278a0c3 2021-02-23 op ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
110 d278a0c3 2021-02-23 op fatal("asprintf: %s", strerror(errno));
112 d278a0c3 2021-02-23 op send_log(s, ec+1);
119 7e9b7812 2021-02-07 op log_err(struct client *c, const char *fmt, ...)
123 7e9b7812 2021-02-07 op va_start(ap, fmt);
124 d278a0c3 2021-02-23 op vlog(LOG_ERR, c, fmt, ap);
129 7e9b7812 2021-02-07 op log_warn(struct client *c, const char *fmt, ...)
133 7e9b7812 2021-02-07 op va_start(ap, fmt);
134 d278a0c3 2021-02-23 op vlog(LOG_WARNING, c, fmt, ap);
139 7e9b7812 2021-02-07 op log_notice(struct client *c, const char *fmt, ...)
143 7e9b7812 2021-02-07 op va_start(ap, fmt);
144 d278a0c3 2021-02-23 op vlog(LOG_NOTICE, c, fmt, ap);
149 7e9b7812 2021-02-07 op log_info(struct client *c, const char *fmt, ...)
153 7e9b7812 2021-02-07 op va_start(ap, fmt);
154 d278a0c3 2021-02-23 op vlog(LOG_INFO, c, fmt, ap);
159 7e9b7812 2021-02-07 op log_debug(struct client *c, const char *fmt, ...)
163 7e9b7812 2021-02-07 op va_start(ap, fmt);
164 d278a0c3 2021-02-23 op vlog(LOG_DEBUG, c, fmt, ap);
168 7e9b7812 2021-02-07 op /* strchr, but with a bound */
169 7e9b7812 2021-02-07 op static char *
170 7e9b7812 2021-02-07 op gmid_strnchr(char *s, int c, size_t len)
174 7e9b7812 2021-02-07 op for (i = 0; i < len; ++i)
175 7e9b7812 2021-02-07 op if (s[i] == c)
176 7e9b7812 2021-02-07 op return &s[i];
181 7e9b7812 2021-02-07 op log_request(struct client *c, char *meta, size_t l)
183 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
184 d278a0c3 2021-02-23 op char *t, *fmted;
188 7e9b7812 2021-02-07 op len = sizeof(c->addr);
189 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
190 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
191 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
192 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
194 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
196 7e9b7812 2021-02-07 op if (c->iri.schema != NULL) {
197 7e9b7812 2021-02-07 op /* serialize the IRI */
198 7e9b7812 2021-02-07 op strlcpy(b, c->iri.schema, sizeof(b));
199 7e9b7812 2021-02-07 op strlcat(b, "://", sizeof(b));
201 7e9b7812 2021-02-07 op /* log the decoded host name, but if it was invalid
202 7e9b7812 2021-02-07 op * use the raw one. */
203 7e9b7812 2021-02-07 op if (*c->domain != '\0')
204 7e9b7812 2021-02-07 op strlcat(b, c->domain, sizeof(b));
206 7e9b7812 2021-02-07 op strlcat(b, c->iri.host, sizeof(b));
208 7e9b7812 2021-02-07 op strlcat(b, "/", sizeof(b));
209 7e9b7812 2021-02-07 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
210 7e9b7812 2021-02-07 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
211 7e9b7812 2021-02-07 op strlcat(b, "?", sizeof(b));
212 7e9b7812 2021-02-07 op strlcat(b, c->iri.query, sizeof(b));
215 7e9b7812 2021-02-07 op strlcpy(b, c->req, sizeof(b));
218 7e9b7812 2021-02-07 op if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
219 7e9b7812 2021-02-07 op t = meta + len;
221 d278a0c3 2021-02-23 op ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
222 d278a0c3 2021-02-23 op (int)(t-meta), meta);
224 d278a0c3 2021-02-23 op err(1, "asprintf");
225 d278a0c3 2021-02-23 op send_log(fmted, ec+1);
232 d278a0c3 2021-02-23 op handle_log(int fd, short ev, void *d)
234 d278a0c3 2021-02-23 op struct imsgbuf *ibuf = d;
235 d278a0c3 2021-02-23 op struct imsg imsg;
236 d278a0c3 2021-02-23 op ssize_t n, datalen;
239 d278a0c3 2021-02-23 op if ((n = imsg_read(ibuf)) == -1) {
240 d278a0c3 2021-02-23 op if (errno == EAGAIN || errno == EWOULDBLOCK)
242 d278a0c3 2021-02-23 op err(1, "imsg_read");
245 d278a0c3 2021-02-23 op errx(1, "connection lost?");
248 d278a0c3 2021-02-23 op if ((n = imsg_get(ibuf, &imsg)) == -1)
249 d278a0c3 2021-02-23 op err(1, "read error");
253 d278a0c3 2021-02-23 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
254 d278a0c3 2021-02-23 op msg = imsg.data;
255 d278a0c3 2021-02-23 op msg[datalen] = '\0';
257 d278a0c3 2021-02-23 op /* ignore imsg.hdr.type for now */
258 d278a0c3 2021-02-23 op if (conf.foreground)
259 d278a0c3 2021-02-23 op fprintf(stderr, "%s\n", msg);
261 d278a0c3 2021-02-23 op syslog(LOG_DAEMON, "%s", msg);
263 d278a0c3 2021-02-23 op imsg_free(&imsg);
268 d278a0c3 2021-02-23 op logger_main(int fd, struct imsgbuf *ibuf)
270 d278a0c3 2021-02-23 op event_init();
272 d278a0c3 2021-02-23 op event_set(&inlog, fd, EV_READ | EV_PERSIST, &handle_log, ibuf);
273 d278a0c3 2021-02-23 op event_add(&inlog, NULL);
275 d278a0c3 2021-02-23 op #ifdef __OpenBSD__
276 d278a0c3 2021-02-23 op if (pledge("stdio", NULL) == -1)
277 d278a0c3 2021-02-23 op err(1, "pledge");
280 d278a0c3 2021-02-23 op event_dispatch();