Blob


1 /*
2 * Copyright (c) 2021 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/queue.h>
21 #include <sys/uio.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <imsg.h>
26 #include <netdb.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <syslog.h>
32 static struct imsgbuf parent_ibuf, child_ibuf;
33 static struct event inlog;
34 static int logfd;
36 static void handle_log(int, short, void*);
37 static int logger_main(int, struct imsgbuf*);
39 void
40 fatal(const char *fmt, ...)
41 {
42 va_list ap;
44 va_start(ap, fmt);
46 if (conf.foreground) {
47 vfprintf(stderr, fmt, ap);
48 fprintf(stderr, "\n");
49 } else
50 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
52 va_end(ap);
53 exit(1);
54 }
56 static inline int
57 should_log(int priority)
58 {
59 switch (priority) {
60 case LOG_ERR:
61 return 1;
62 case LOG_WARNING:
63 return 1;
64 case LOG_NOTICE:
65 return conf.verbose >= 1;
66 case LOG_INFO:
67 return conf.verbose >= 2;
68 case LOG_DEBUG:
69 return conf.verbose >= 3;
70 default:
71 return 0;
72 }
73 }
75 static inline void
76 send_log(const char *msg, size_t len)
77 {
78 imsg_compose(&parent_ibuf, 0, 0, 0, -1, msg, len);
79 /* XXX: use event_once() */
80 imsg_flush(&parent_ibuf);
81 }
83 static inline void
84 vlog(int priority, struct client *c,
85 const char *fmt, va_list ap)
86 {
87 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
88 char *fmted, *s;
89 size_t len;
90 int ec;
92 if (!should_log(priority))
93 return;
95 if (c != NULL) {
96 len = sizeof(c->addr);
97 ec = getnameinfo((struct sockaddr*)&c->addr, len,
98 hbuf, sizeof(hbuf),
99 sbuf, sizeof(sbuf),
100 NI_NUMERICHOST | NI_NUMERICSERV);
101 if (ec != 0)
102 fatal("getnameinfo: %s", gai_strerror(ec));
105 if (vasprintf(&fmted, fmt, ap) == -1)
106 fatal("vasprintf: %s", strerror(errno));
108 if (c == NULL)
109 ec = asprintf(&s, "internal: %s", fmted);
110 else
111 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
113 if (ec < 0)
114 fatal("asprintf: %s", strerror(errno));
116 send_log(s, ec+1);
118 free(fmted);
119 free(s);
122 void
123 log_err(struct client *c, const char *fmt, ...)
125 va_list ap;
127 va_start(ap, fmt);
128 vlog(LOG_ERR, c, fmt, ap);
129 va_end(ap);
132 void
133 log_warn(struct client *c, const char *fmt, ...)
135 va_list ap;
137 va_start(ap, fmt);
138 vlog(LOG_WARNING, c, fmt, ap);
139 va_end(ap);
142 void
143 log_notice(struct client *c, const char *fmt, ...)
145 va_list ap;
147 va_start(ap, fmt);
148 vlog(LOG_NOTICE, c, fmt, ap);
149 va_end(ap);
152 void
153 log_info(struct client *c, const char *fmt, ...)
155 va_list ap;
157 va_start(ap, fmt);
158 vlog(LOG_INFO, c, fmt, ap);
159 va_end(ap);
162 void
163 log_debug(struct client *c, const char *fmt, ...)
165 va_list ap;
167 va_start(ap, fmt);
168 vlog(LOG_DEBUG, c, fmt, ap);
169 va_end(ap);
172 /* strchr, but with a bound */
173 static char *
174 gmid_strnchr(char *s, int c, size_t len)
176 size_t i;
178 for (i = 0; i < len; ++i)
179 if (s[i] == c)
180 return &s[i];
181 return NULL;
184 void
185 log_request(struct client *c, char *meta, size_t l)
187 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
188 char *t, *fmted;
189 size_t len;
190 int ec;
192 len = sizeof(c->addr);
193 ec = getnameinfo((struct sockaddr*)&c->addr, len,
194 hbuf, sizeof(hbuf),
195 sbuf, sizeof(sbuf),
196 NI_NUMERICHOST | NI_NUMERICSERV);
197 if (ec != 0)
198 fatal("getnameinfo: %s", gai_strerror(ec));
200 if (c->iri.schema != NULL) {
201 /* serialize the IRI */
202 strlcpy(b, c->iri.schema, sizeof(b));
203 strlcat(b, "://", sizeof(b));
205 /* log the decoded host name, but if it was invalid
206 * use the raw one. */
207 if (*c->domain != '\0')
208 strlcat(b, c->domain, sizeof(b));
209 else
210 strlcat(b, c->iri.host, sizeof(b));
212 strlcat(b, "/", sizeof(b));
213 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
214 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
215 strlcat(b, "?", sizeof(b));
216 strlcat(b, c->iri.query, sizeof(b));
218 } else {
219 strlcpy(b, c->req, sizeof(b));
222 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
223 t = meta + len;
225 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
226 (int)(t-meta), meta);
227 if (ec < 0)
228 err(1, "asprintf");
229 send_log(fmted, ec+1);
230 free(fmted);
235 static void
236 handle_log(int fd, short ev, void *d)
238 struct imsgbuf *ibuf = d;
239 struct imsg imsg;
240 ssize_t n, datalen;
241 char *msg;
243 if ((n = imsg_read(ibuf)) == -1) {
244 if (errno == EAGAIN || errno == EWOULDBLOCK)
245 return;
246 err(1, "imsg_read");
248 if (n == 0)
249 errx(1, "connection lost?");
251 for (;;) {
252 if ((n = imsg_get(ibuf, &imsg)) == -1)
253 err(1, "read error");
254 if (n == 0)
255 return;
257 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
258 msg = imsg.data;
259 msg[datalen] = '\0';
261 /* ignore imsg.hdr.type for now */
262 if (conf.foreground)
263 fprintf(stderr, "%s\n", msg);
264 else
265 syslog(LOG_DAEMON, "%s", msg);
267 imsg_free(&imsg);
271 static int
272 logger_main(int fd, struct imsgbuf *ibuf)
274 event_init();
276 event_set(&inlog, fd, EV_READ | EV_PERSIST, &handle_log, ibuf);
277 event_add(&inlog, NULL);
279 #ifdef __OpenBSD__
280 if (pledge("stdio", NULL) == -1)
281 err(1, "pledge");
282 #endif
284 event_dispatch();
286 return 0;
289 void
290 logger_init(void)
292 int p[2];
294 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
295 err(1, "socketpair");
297 switch (fork()) {
298 case -1:
299 err(1, "fork");
300 case 0:
301 logfd = p[1];
302 close(p[0]);
303 setproctitle("logger");
304 imsg_init(&child_ibuf, p[1]);
305 drop_priv();
306 _exit(logger_main(p[1], &child_ibuf));
307 default:
308 logfd = p[0];
309 close(p[1]);
310 imsg_init(&parent_ibuf, p[0]);
311 return;