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 bc99d868 2021-03-19 op static struct event imsgev;
33 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
34 bc99d868 2021-03-19 op static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
35 bc99d868 2021-03-19 op static void handle_dispatch_imsg(int, short, void*);
37 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
38 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
39 bc99d868 2021-03-19 op [IMSG_LOG] = handle_imsg_log,
43 7e9b7812 2021-02-07 op fatal(const char *fmt, ...)
47 7e9b7812 2021-02-07 op va_start(ap, fmt);
49 7e9b7812 2021-02-07 op if (conf.foreground) {
50 7e9b7812 2021-02-07 op vfprintf(stderr, fmt, ap);
51 7e9b7812 2021-02-07 op fprintf(stderr, "\n");
53 7e9b7812 2021-02-07 op vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
59 7e9b7812 2021-02-07 op static inline int
60 7e9b7812 2021-02-07 op should_log(int priority)
62 7e9b7812 2021-02-07 op switch (priority) {
65 7e9b7812 2021-02-07 op case LOG_WARNING:
67 7e9b7812 2021-02-07 op case LOG_NOTICE:
68 7e9b7812 2021-02-07 op return conf.verbose >= 1;
69 7e9b7812 2021-02-07 op case LOG_INFO:
70 7e9b7812 2021-02-07 op return conf.verbose >= 2;
71 7e9b7812 2021-02-07 op case LOG_DEBUG:
72 7e9b7812 2021-02-07 op return conf.verbose >= 3;
78 d278a0c3 2021-02-23 op static inline void
79 d278a0c3 2021-02-23 op send_log(const char *msg, size_t len)
81 bc99d868 2021-03-19 op imsg_compose(&logibuf, IMSG_LOG, 0, 0, -1, msg, len);
82 d278a0c3 2021-02-23 op /* XXX: use event_once() */
83 bc99d868 2021-03-19 op imsg_flush(&logibuf);
86 d278a0c3 2021-02-23 op static inline void
87 d278a0c3 2021-02-23 op vlog(int priority, struct client *c,
88 7e9b7812 2021-02-07 op const char *fmt, va_list ap)
90 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
91 7e9b7812 2021-02-07 op char *fmted, *s;
95 7e9b7812 2021-02-07 op if (!should_log(priority))
98 d278a0c3 2021-02-23 op if (c != NULL) {
99 7e9b7812 2021-02-07 op len = sizeof(c->addr);
100 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
101 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
102 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
103 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
105 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
108 7e9b7812 2021-02-07 op if (vasprintf(&fmted, fmt, ap) == -1)
109 7e9b7812 2021-02-07 op fatal("vasprintf: %s", strerror(errno));
111 d278a0c3 2021-02-23 op if (c == NULL)
112 d278a0c3 2021-02-23 op ec = asprintf(&s, "internal: %s", fmted);
114 d278a0c3 2021-02-23 op ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
117 d278a0c3 2021-02-23 op fatal("asprintf: %s", strerror(errno));
119 d278a0c3 2021-02-23 op send_log(s, ec+1);
126 7e9b7812 2021-02-07 op log_err(struct client *c, const char *fmt, ...)
130 7e9b7812 2021-02-07 op va_start(ap, fmt);
131 d278a0c3 2021-02-23 op vlog(LOG_ERR, c, fmt, ap);
136 7e9b7812 2021-02-07 op log_warn(struct client *c, const char *fmt, ...)
140 7e9b7812 2021-02-07 op va_start(ap, fmt);
141 d278a0c3 2021-02-23 op vlog(LOG_WARNING, c, fmt, ap);
146 7e9b7812 2021-02-07 op log_notice(struct client *c, const char *fmt, ...)
150 7e9b7812 2021-02-07 op va_start(ap, fmt);
151 d278a0c3 2021-02-23 op vlog(LOG_NOTICE, c, fmt, ap);
156 7e9b7812 2021-02-07 op log_info(struct client *c, const char *fmt, ...)
160 7e9b7812 2021-02-07 op va_start(ap, fmt);
161 d278a0c3 2021-02-23 op vlog(LOG_INFO, c, fmt, ap);
166 7e9b7812 2021-02-07 op log_debug(struct client *c, const char *fmt, ...)
170 7e9b7812 2021-02-07 op va_start(ap, fmt);
171 d278a0c3 2021-02-23 op vlog(LOG_DEBUG, c, fmt, ap);
175 7e9b7812 2021-02-07 op /* strchr, but with a bound */
176 7e9b7812 2021-02-07 op static char *
177 7e9b7812 2021-02-07 op gmid_strnchr(char *s, int c, size_t len)
181 7e9b7812 2021-02-07 op for (i = 0; i < len; ++i)
182 7e9b7812 2021-02-07 op if (s[i] == c)
183 7e9b7812 2021-02-07 op return &s[i];
188 7e9b7812 2021-02-07 op log_request(struct client *c, char *meta, size_t l)
190 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
191 d278a0c3 2021-02-23 op char *t, *fmted;
195 7e9b7812 2021-02-07 op len = sizeof(c->addr);
196 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
197 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
198 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
199 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
201 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
203 7e9b7812 2021-02-07 op if (c->iri.schema != NULL) {
204 7e9b7812 2021-02-07 op /* serialize the IRI */
205 7e9b7812 2021-02-07 op strlcpy(b, c->iri.schema, sizeof(b));
206 7e9b7812 2021-02-07 op strlcat(b, "://", sizeof(b));
208 7e9b7812 2021-02-07 op /* log the decoded host name, but if it was invalid
209 7e9b7812 2021-02-07 op * use the raw one. */
210 7e9b7812 2021-02-07 op if (*c->domain != '\0')
211 7e9b7812 2021-02-07 op strlcat(b, c->domain, sizeof(b));
213 7e9b7812 2021-02-07 op strlcat(b, c->iri.host, sizeof(b));
215 7e9b7812 2021-02-07 op strlcat(b, "/", sizeof(b));
216 7e9b7812 2021-02-07 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
217 7e9b7812 2021-02-07 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
218 7e9b7812 2021-02-07 op strlcat(b, "?", sizeof(b));
219 7e9b7812 2021-02-07 op strlcat(b, c->iri.query, sizeof(b));
222 7e9b7812 2021-02-07 op strlcpy(b, c->req, sizeof(b));
225 7e9b7812 2021-02-07 op if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
226 7e9b7812 2021-02-07 op t = meta + len;
228 d278a0c3 2021-02-23 op ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
229 d278a0c3 2021-02-23 op (int)(t-meta), meta);
231 d278a0c3 2021-02-23 op err(1, "asprintf");
232 d278a0c3 2021-02-23 op send_log(fmted, ec+1);
239 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
241 bc99d868 2021-03-19 op event_loopbreak();
245 bc99d868 2021-03-19 op handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
249 bc99d868 2021-03-19 op msg = imsg->data;
250 bc99d868 2021-03-19 op msg[datalen-1] = '\0';
252 bc99d868 2021-03-19 op if (conf.foreground)
253 bc99d868 2021-03-19 op fprintf(stderr, "%s\n", msg);
255 bc99d868 2021-03-19 op syslog(LOG_DAEMON, "%s", msg);
259 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
261 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
262 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
266 d278a0c3 2021-02-23 op logger_main(int fd, struct imsgbuf *ibuf)
268 d278a0c3 2021-02-23 op event_init();
270 bc99d868 2021-03-19 op event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
271 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
273 62e001b0 2021-03-20 op sandbox_logger_process();
275 d278a0c3 2021-02-23 op event_dispatch();