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/uio.h>
22 #include <errno.h>
23 #include <event.h>
24 #include <imsg.h>
25 #include <netdb.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <syslog.h>
31 static struct event inlog;
33 static void handle_log(int, short, void*);
35 void
36 fatal(const char *fmt, ...)
37 {
38 va_list ap;
40 va_start(ap, fmt);
42 if (conf.foreground) {
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
45 } else
46 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
48 va_end(ap);
49 exit(1);
50 }
52 static inline int
53 should_log(int priority)
54 {
55 switch (priority) {
56 case LOG_ERR:
57 return 1;
58 case LOG_WARNING:
59 return 1;
60 case LOG_NOTICE:
61 return conf.verbose >= 1;
62 case LOG_INFO:
63 return conf.verbose >= 2;
64 case LOG_DEBUG:
65 return conf.verbose >= 3;
66 default:
67 return 0;
68 }
69 }
71 static inline void
72 send_log(const char *msg, size_t len)
73 {
74 imsg_compose(&logpibuf, 0, 0, 0, -1, msg, len);
75 /* XXX: use event_once() */
76 imsg_flush(&logpibuf);
77 }
79 static inline void
80 vlog(int priority, struct client *c,
81 const char *fmt, va_list ap)
82 {
83 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
84 char *fmted, *s;
85 size_t len;
86 int ec;
88 if (!should_log(priority))
89 return;
91 if (c != NULL) {
92 len = sizeof(c->addr);
93 ec = getnameinfo((struct sockaddr*)&c->addr, len,
94 hbuf, sizeof(hbuf),
95 sbuf, sizeof(sbuf),
96 NI_NUMERICHOST | NI_NUMERICSERV);
97 if (ec != 0)
98 fatal("getnameinfo: %s", gai_strerror(ec));
99 }
101 if (vasprintf(&fmted, fmt, ap) == -1)
102 fatal("vasprintf: %s", strerror(errno));
104 if (c == NULL)
105 ec = asprintf(&s, "internal: %s", fmted);
106 else
107 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
109 if (ec < 0)
110 fatal("asprintf: %s", strerror(errno));
112 send_log(s, ec+1);
114 free(fmted);
115 free(s);
118 void
119 log_err(struct client *c, const char *fmt, ...)
121 va_list ap;
123 va_start(ap, fmt);
124 vlog(LOG_ERR, c, fmt, ap);
125 va_end(ap);
128 void
129 log_warn(struct client *c, const char *fmt, ...)
131 va_list ap;
133 va_start(ap, fmt);
134 vlog(LOG_WARNING, c, fmt, ap);
135 va_end(ap);
138 void
139 log_notice(struct client *c, const char *fmt, ...)
141 va_list ap;
143 va_start(ap, fmt);
144 vlog(LOG_NOTICE, c, fmt, ap);
145 va_end(ap);
148 void
149 log_info(struct client *c, const char *fmt, ...)
151 va_list ap;
153 va_start(ap, fmt);
154 vlog(LOG_INFO, c, fmt, ap);
155 va_end(ap);
158 void
159 log_debug(struct client *c, const char *fmt, ...)
161 va_list ap;
163 va_start(ap, fmt);
164 vlog(LOG_DEBUG, c, fmt, ap);
165 va_end(ap);
168 /* strchr, but with a bound */
169 static char *
170 gmid_strnchr(char *s, int c, size_t len)
172 size_t i;
174 for (i = 0; i < len; ++i)
175 if (s[i] == c)
176 return &s[i];
177 return NULL;
180 void
181 log_request(struct client *c, char *meta, size_t l)
183 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
184 char *t, *fmted;
185 size_t len;
186 int ec;
188 len = sizeof(c->addr);
189 ec = getnameinfo((struct sockaddr*)&c->addr, len,
190 hbuf, sizeof(hbuf),
191 sbuf, sizeof(sbuf),
192 NI_NUMERICHOST | NI_NUMERICSERV);
193 if (ec != 0)
194 fatal("getnameinfo: %s", gai_strerror(ec));
196 if (c->iri.schema != NULL) {
197 /* serialize the IRI */
198 strlcpy(b, c->iri.schema, sizeof(b));
199 strlcat(b, "://", sizeof(b));
201 /* log the decoded host name, but if it was invalid
202 * use the raw one. */
203 if (*c->domain != '\0')
204 strlcat(b, c->domain, sizeof(b));
205 else
206 strlcat(b, c->iri.host, sizeof(b));
208 strlcat(b, "/", sizeof(b));
209 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
210 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
211 strlcat(b, "?", sizeof(b));
212 strlcat(b, c->iri.query, sizeof(b));
214 } else {
215 strlcpy(b, c->req, sizeof(b));
218 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
219 t = meta + len;
221 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
222 (int)(t-meta), meta);
223 if (ec < 0)
224 err(1, "asprintf");
225 send_log(fmted, ec+1);
226 free(fmted);
231 static void
232 handle_log(int fd, short ev, void *d)
234 struct imsgbuf *ibuf = d;
235 struct imsg imsg;
236 ssize_t n, datalen;
237 char *msg;
239 if ((n = imsg_read(ibuf)) == -1) {
240 if (errno == EAGAIN || errno == EWOULDBLOCK)
241 return;
242 err(1, "imsg_read");
244 if (n == 0)
245 errx(1, "connection lost?");
247 for (;;) {
248 if ((n = imsg_get(ibuf, &imsg)) == -1)
249 err(1, "read error");
250 if (n == 0)
251 return;
253 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
254 msg = imsg.data;
255 msg[datalen-1] = '\0';
257 /* ignore imsg.hdr.type for now */
258 if (conf.foreground)
259 fprintf(stderr, "%s\n", msg);
260 else
261 syslog(LOG_DAEMON, "%s", msg);
263 imsg_free(&imsg);
267 int
268 logger_main(int fd, struct imsgbuf *ibuf)
270 event_init();
272 event_set(&inlog, fd, EV_READ | EV_PERSIST, &handle_log, ibuf);
273 event_add(&inlog, NULL);
275 #ifdef __OpenBSD__
276 if (pledge("stdio", NULL) == -1)
277 err(1, "pledge");
278 #endif
280 event_dispatch();
282 return 0;