2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
19 #include <sys/types.h>
36 static struct event imsgev;
40 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
41 static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
42 static void handle_imsg_log_type(struct imsgbuf*, struct imsg*, size_t);
43 static void handle_dispatch_imsg(int, short, void*);
45 static imsg_handlerfn *handlers[] = {
46 [IMSG_QUIT] = handle_imsg_quit,
47 [IMSG_LOG] = handle_imsg_log,
48 [IMSG_LOG_REQUEST] = handle_imsg_log,
49 [IMSG_LOG_TYPE] = handle_imsg_log_type,
53 log_request(struct client *c, char *meta, size_t l)
55 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
61 len = sizeof(c->addr);
62 ec = getnameinfo((struct sockaddr*)&c->addr, len,
65 NI_NUMERICHOST | NI_NUMERICSERV);
67 fatalx("getnameinfo: %s", gai_strerror(ec));
69 if (c->iri.schema != NULL) {
70 /* serialize the IRI */
71 strlcpy(b, c->iri.schema, sizeof(b));
72 strlcat(b, "://", sizeof(b));
74 /* log the decoded host name, but if it was invalid
76 if (*c->domain != '\0')
77 strlcat(b, c->domain, sizeof(b));
79 strlcat(b, c->iri.host, sizeof(b));
81 if (*c->iri.path != '/')
82 strlcat(b, "/", sizeof(b));
83 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
84 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
85 strlcat(b, "?", sizeof(b));
86 strlcat(b, c->iri.query, sizeof(b));
89 if ((t = c->req) == NULL)
91 strlcpy(b, t, sizeof(b));
94 if ((t = memchr(meta, '\r', l)) == NULL)
97 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
102 imsg_compose(&logibuf, IMSG_LOG_REQUEST, 0, 0, -1, fmted, ec + 1);
103 imsg_flush(&logibuf);
111 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
117 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
122 msg[datalen-1] = '\0';
125 fprintf(log, "%s\n", msg);
127 syslog(LOG_DAEMON | LOG_NOTICE, "%s", msg);
131 handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
133 if (log != NULL && log != stderr) {
139 if (imsg->fd != -1) {
140 if ((log = fdopen(imsg->fd, "a")) == NULL) {
141 syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s",
149 handle_dispatch_imsg(int fd, short ev, void *d)
151 struct imsgbuf *ibuf = d;
152 dispatch_imsg(ibuf, handlers, sizeof(handlers));
156 logger_main(int fd, struct imsgbuf *ibuf)
162 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
163 event_add(&imsgev, NULL);
165 sandbox_logger_process();