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 <poll.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <time.h>
33 #include "logger.h"
34 #include "log.h"
36 static struct event imsgev;
38 static FILE *log;
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,
50 };
52 void
53 log_request(struct client *c, char *meta, size_t l)
54 {
55 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
56 char *fmted;
57 const char *t;
58 size_t len;
59 int ec;
61 len = sizeof(c->addr);
62 ec = getnameinfo((struct sockaddr*)&c->addr, len,
63 hbuf, sizeof(hbuf),
64 sbuf, sizeof(sbuf),
65 NI_NUMERICHOST | NI_NUMERICSERV);
66 if (ec != 0)
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
75 * use the raw one. */
76 if (*c->domain != '\0')
77 strlcat(b, c->domain, sizeof(b));
78 else
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));
87 }
88 } else {
89 if ((t = c->req) == NULL)
90 t = "";
91 strlcpy(b, t, sizeof(b));
92 }
94 if ((t = memchr(meta, '\r', l)) == NULL)
95 t = meta + len;
97 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
98 (int)(t-meta), meta);
99 if (ec == -1)
100 err(1, "asprintf");
102 imsg_compose(&logibuf, IMSG_LOG_REQUEST, 0, 0, -1, fmted, ec + 1);
103 imsg_flush(&logibuf);
105 free(fmted);
110 static void
111 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
113 event_loopbreak();
116 static void
117 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
119 char *msg;
121 msg = imsg->data;
122 msg[datalen-1] = '\0';
124 if (log != NULL)
125 fprintf(log, "%s\n", msg);
126 else
127 syslog(LOG_DAEMON | LOG_NOTICE, "%s", msg);
130 static void
131 handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
133 if (log != NULL && log != stderr) {
134 fflush(log);
135 fclose(log);
137 log = NULL;
139 if (imsg->fd != -1) {
140 if ((log = fdopen(imsg->fd, "a")) == NULL) {
141 syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s",
142 strerror(errno));
143 exit(1);
148 static void
149 handle_dispatch_imsg(int fd, short ev, void *d)
151 struct imsgbuf *ibuf = d;
152 dispatch_imsg(ibuf, handlers, sizeof(handlers));
155 int
156 logger_main(int fd, struct imsgbuf *ibuf)
158 log = stderr;
160 event_init();
162 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
163 event_add(&imsgev, NULL);
165 sandbox_logger_process();
167 event_dispatch();
169 closelog();
171 return 0;