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 2ef7f631 2021-04-28 op #include <poll.h>
27 7e9b7812 2021-02-07 op #include <stdarg.h>
28 7e9b7812 2021-02-07 op #include <stdio.h>
29 7e9b7812 2021-02-07 op #include <string.h>
30 7e9b7812 2021-02-07 op #include <syslog.h>
31 b3342582 2021-04-14 op #include <time.h>
33 bc99d868 2021-03-19 op static struct event imsgev;
35 e952c505 2021-06-15 op static FILE *log;
37 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
38 bc99d868 2021-03-19 op static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
39 e952c505 2021-06-15 op static void handle_imsg_log_type(struct imsgbuf*, struct imsg*, size_t);
40 bc99d868 2021-03-19 op static void handle_dispatch_imsg(int, short, void*);
42 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
43 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
44 bc99d868 2021-03-19 op [IMSG_LOG] = handle_imsg_log,
45 e952c505 2021-06-15 op [IMSG_LOG_TYPE] = handle_imsg_log_type,
48 b3342582 2021-04-14 op static inline void
49 e952c505 2021-06-15 op print_date(FILE *f)
51 b3342582 2021-04-14 op struct tm tminfo;
56 b3342582 2021-04-14 op strftime(buf, sizeof(buf), "%F %T",
57 b3342582 2021-04-14 op localtime_r(&t, &tminfo));
58 e952c505 2021-06-15 op fprintf(f, "[%s] ", buf);
61 7e9b7812 2021-02-07 op static inline int
62 7e9b7812 2021-02-07 op should_log(int priority)
64 7e9b7812 2021-02-07 op switch (priority) {
67 7e9b7812 2021-02-07 op case LOG_WARNING:
69 7e9b7812 2021-02-07 op case LOG_NOTICE:
70 7e9b7812 2021-02-07 op return conf.verbose >= 1;
71 7e9b7812 2021-02-07 op case LOG_INFO:
72 7e9b7812 2021-02-07 op return conf.verbose >= 2;
73 7e9b7812 2021-02-07 op case LOG_DEBUG:
74 7e9b7812 2021-02-07 op return conf.verbose >= 3;
80 d278a0c3 2021-02-23 op static inline void
81 42447f67 2021-04-28 op send_log(int priority, const char *msg, size_t len)
83 42447f67 2021-04-28 op imsg_compose(&logibuf, IMSG_LOG, priority, 0, -1, msg, len);
84 bc99d868 2021-03-19 op imsg_flush(&logibuf);
88 d89a9060 2021-04-28 op fatal(const char *fmt, ...)
90 2ef7f631 2021-04-28 op struct pollfd pfd;
95 d89a9060 2021-04-28 op va_start(ap, fmt);
96 d89a9060 2021-04-28 op if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
97 2ef7f631 2021-04-28 op send_log(LOG_CRIT, fmted, r+1);
100 2ef7f631 2021-04-28 op /* wait for the logger process to shut down */
101 2ef7f631 2021-04-28 op pfd.fd = logibuf.fd;
102 2ef7f631 2021-04-28 op pfd.events = POLLIN;
103 2ef7f631 2021-04-28 op poll(&pfd, 1, 1000);
109 d278a0c3 2021-02-23 op static inline void
110 d278a0c3 2021-02-23 op vlog(int priority, struct client *c,
111 7e9b7812 2021-02-07 op const char *fmt, va_list ap)
113 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
114 7e9b7812 2021-02-07 op char *fmted, *s;
118 7e9b7812 2021-02-07 op if (!should_log(priority))
121 d278a0c3 2021-02-23 op if (c != NULL) {
122 7e9b7812 2021-02-07 op len = sizeof(c->addr);
123 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
124 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
125 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
126 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
128 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
131 7e9b7812 2021-02-07 op if (vasprintf(&fmted, fmt, ap) == -1)
132 7e9b7812 2021-02-07 op fatal("vasprintf: %s", strerror(errno));
134 7700ab07 2021-07-06 op if (c == NULL)
135 d278a0c3 2021-02-23 op ec = asprintf(&s, "internal: %s", fmted);
137 d278a0c3 2021-02-23 op ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
140 d278a0c3 2021-02-23 op fatal("asprintf: %s", strerror(errno));
142 42447f67 2021-04-28 op send_log(priority, s, ec+1);
149 7e9b7812 2021-02-07 op log_err(struct client *c, const char *fmt, ...)
153 7e9b7812 2021-02-07 op va_start(ap, fmt);
154 d278a0c3 2021-02-23 op vlog(LOG_ERR, c, fmt, ap);
159 7e9b7812 2021-02-07 op log_warn(struct client *c, const char *fmt, ...)
163 7e9b7812 2021-02-07 op va_start(ap, fmt);
164 d278a0c3 2021-02-23 op vlog(LOG_WARNING, c, fmt, ap);
169 7e9b7812 2021-02-07 op log_notice(struct client *c, const char *fmt, ...)
173 7e9b7812 2021-02-07 op va_start(ap, fmt);
174 d278a0c3 2021-02-23 op vlog(LOG_NOTICE, c, fmt, ap);
179 7e9b7812 2021-02-07 op log_info(struct client *c, const char *fmt, ...)
183 7e9b7812 2021-02-07 op va_start(ap, fmt);
184 d278a0c3 2021-02-23 op vlog(LOG_INFO, c, fmt, ap);
189 7e9b7812 2021-02-07 op log_debug(struct client *c, const char *fmt, ...)
193 7e9b7812 2021-02-07 op va_start(ap, fmt);
194 d278a0c3 2021-02-23 op vlog(LOG_DEBUG, c, fmt, ap);
198 7e9b7812 2021-02-07 op /* strchr, but with a bound */
199 7e9b7812 2021-02-07 op static char *
200 7e9b7812 2021-02-07 op gmid_strnchr(char *s, int c, size_t len)
204 7e9b7812 2021-02-07 op for (i = 0; i < len; ++i)
205 7e9b7812 2021-02-07 op if (s[i] == c)
206 7e9b7812 2021-02-07 op return &s[i];
211 7e9b7812 2021-02-07 op log_request(struct client *c, char *meta, size_t l)
213 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
214 d278a0c3 2021-02-23 op char *t, *fmted;
218 7e9b7812 2021-02-07 op len = sizeof(c->addr);
219 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
220 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
221 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
222 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
224 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
226 7e9b7812 2021-02-07 op if (c->iri.schema != NULL) {
227 7e9b7812 2021-02-07 op /* serialize the IRI */
228 7e9b7812 2021-02-07 op strlcpy(b, c->iri.schema, sizeof(b));
229 7e9b7812 2021-02-07 op strlcat(b, "://", sizeof(b));
231 7e9b7812 2021-02-07 op /* log the decoded host name, but if it was invalid
232 7e9b7812 2021-02-07 op * use the raw one. */
233 7e9b7812 2021-02-07 op if (*c->domain != '\0')
234 7e9b7812 2021-02-07 op strlcat(b, c->domain, sizeof(b));
236 7e9b7812 2021-02-07 op strlcat(b, c->iri.host, sizeof(b));
238 7e9b7812 2021-02-07 op strlcat(b, "/", sizeof(b));
239 7e9b7812 2021-02-07 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
240 7e9b7812 2021-02-07 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
241 7e9b7812 2021-02-07 op strlcat(b, "?", sizeof(b));
242 7e9b7812 2021-02-07 op strlcat(b, c->iri.query, sizeof(b));
245 7e9b7812 2021-02-07 op strlcpy(b, c->req, sizeof(b));
248 7e9b7812 2021-02-07 op if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
249 7e9b7812 2021-02-07 op t = meta + len;
251 d278a0c3 2021-02-23 op ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
252 d278a0c3 2021-02-23 op (int)(t-meta), meta);
254 d278a0c3 2021-02-23 op err(1, "asprintf");
255 42447f67 2021-04-28 op send_log(LOG_NOTICE, fmted, ec+1);
262 e952c505 2021-06-15 op do_log(int priority, const char *msg)
264 e952c505 2021-06-15 op int quit = 0;
266 e952c505 2021-06-15 op if (priority == LOG_CRIT) {
268 e952c505 2021-06-15 op priority = LOG_ERR;
271 e952c505 2021-06-15 op if (log != NULL) {
272 e952c505 2021-06-15 op print_date(log);
273 e952c505 2021-06-15 op fprintf(log, "%s\n", msg);
275 e952c505 2021-06-15 op syslog(LOG_DAEMON | priority, "%s", msg);
282 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
284 bc99d868 2021-03-19 op event_loopbreak();
288 bc99d868 2021-03-19 op handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
290 984c46a8 2021-06-16 op int priority;
293 bc99d868 2021-03-19 op msg = imsg->data;
294 bc99d868 2021-03-19 op msg[datalen-1] = '\0';
295 2ef7f631 2021-04-28 op priority = imsg->hdr.peerid;
296 e952c505 2021-06-15 op do_log(priority, msg);
300 e952c505 2021-06-15 op handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
302 e952c505 2021-06-15 op if (log != NULL) {
308 e952c505 2021-06-15 op if (imsg->fd != -1) {
309 e952c505 2021-06-15 op if ((log = fdopen(imsg->fd, "a")) == NULL) {
310 e952c505 2021-06-15 op syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s",
311 e952c505 2021-06-15 op strerror(errno));
318 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
320 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
321 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
325 d278a0c3 2021-02-23 op logger_main(int fd, struct imsgbuf *ibuf)
327 d278a0c3 2021-02-23 op event_init();
329 bc99d868 2021-03-19 op event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
330 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
332 62e001b0 2021-03-20 op sandbox_logger_process();
334 d278a0c3 2021-02-23 op event_dispatch();