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>
30 #include <time.h>
32 static struct event imsgev;
34 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
35 static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
36 static void handle_dispatch_imsg(int, short, void*);
38 static imsg_handlerfn *handlers[] = {
39 [IMSG_QUIT] = handle_imsg_quit,
40 [IMSG_LOG] = handle_imsg_log,
41 };
43 static inline void
44 print_date(void)
45 {
46 struct tm tminfo;
47 time_t t;
48 char buf[20];
50 time(&t);
51 strftime(buf, sizeof(buf), "%F %T",
52 localtime_r(&t, &tminfo));
53 fprintf(stderr, "[%s] ", buf);
54 }
56 static inline int
57 should_log(int priority)
58 {
59 switch (priority) {
60 case LOG_ERR:
61 return 1;
62 case LOG_WARNING:
63 return 1;
64 case LOG_NOTICE:
65 return conf.verbose >= 1;
66 case LOG_INFO:
67 return conf.verbose >= 2;
68 case LOG_DEBUG:
69 return conf.verbose >= 3;
70 default:
71 return 0;
72 }
73 }
75 static inline void
76 send_log(int priority, const char *msg, size_t len)
77 {
78 imsg_compose(&logibuf, IMSG_LOG, priority, 0, -1, msg, len);
79 imsg_flush(&logibuf);
80 }
82 void
83 fatal(const char *fmt, ...)
84 {
85 va_list ap;
86 int r;
87 char *fmted;
89 va_start(ap, fmt);
90 if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
91 send_log(LOG_ERR, fmted, r+1);
92 free(fmted);
93 }
94 va_end(ap);
95 exit(1);
96 }
98 static inline void
99 vlog(int priority, struct client *c,
100 const char *fmt, va_list ap)
102 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
103 char *fmted, *s;
104 size_t len;
105 int ec;
107 if (!should_log(priority))
108 return;
110 if (c != NULL) {
111 len = sizeof(c->addr);
112 ec = getnameinfo((struct sockaddr*)&c->addr, len,
113 hbuf, sizeof(hbuf),
114 sbuf, sizeof(sbuf),
115 NI_NUMERICHOST | NI_NUMERICSERV);
116 if (ec != 0)
117 fatal("getnameinfo: %s", gai_strerror(ec));
120 if (vasprintf(&fmted, fmt, ap) == -1)
121 fatal("vasprintf: %s", strerror(errno));
123 if (c == NULL)
124 ec = asprintf(&s, "internal: %s", fmted);
125 else
126 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
128 if (ec < 0)
129 fatal("asprintf: %s", strerror(errno));
131 send_log(priority, s, ec+1);
133 free(fmted);
134 free(s);
137 void
138 log_err(struct client *c, const char *fmt, ...)
140 va_list ap;
142 va_start(ap, fmt);
143 vlog(LOG_ERR, c, fmt, ap);
144 va_end(ap);
147 void
148 log_warn(struct client *c, const char *fmt, ...)
150 va_list ap;
152 va_start(ap, fmt);
153 vlog(LOG_WARNING, c, fmt, ap);
154 va_end(ap);
157 void
158 log_notice(struct client *c, const char *fmt, ...)
160 va_list ap;
162 va_start(ap, fmt);
163 vlog(LOG_NOTICE, c, fmt, ap);
164 va_end(ap);
167 void
168 log_info(struct client *c, const char *fmt, ...)
170 va_list ap;
172 va_start(ap, fmt);
173 vlog(LOG_INFO, c, fmt, ap);
174 va_end(ap);
177 void
178 log_debug(struct client *c, const char *fmt, ...)
180 va_list ap;
182 va_start(ap, fmt);
183 vlog(LOG_DEBUG, c, fmt, ap);
184 va_end(ap);
187 /* strchr, but with a bound */
188 static char *
189 gmid_strnchr(char *s, int c, size_t len)
191 size_t i;
193 for (i = 0; i < len; ++i)
194 if (s[i] == c)
195 return &s[i];
196 return NULL;
199 void
200 log_request(struct client *c, char *meta, size_t l)
202 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
203 char *t, *fmted;
204 size_t len;
205 int ec;
207 len = sizeof(c->addr);
208 ec = getnameinfo((struct sockaddr*)&c->addr, len,
209 hbuf, sizeof(hbuf),
210 sbuf, sizeof(sbuf),
211 NI_NUMERICHOST | NI_NUMERICSERV);
212 if (ec != 0)
213 fatal("getnameinfo: %s", gai_strerror(ec));
215 if (c->iri.schema != NULL) {
216 /* serialize the IRI */
217 strlcpy(b, c->iri.schema, sizeof(b));
218 strlcat(b, "://", sizeof(b));
220 /* log the decoded host name, but if it was invalid
221 * use the raw one. */
222 if (*c->domain != '\0')
223 strlcat(b, c->domain, sizeof(b));
224 else
225 strlcat(b, c->iri.host, sizeof(b));
227 strlcat(b, "/", sizeof(b));
228 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
229 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
230 strlcat(b, "?", sizeof(b));
231 strlcat(b, c->iri.query, sizeof(b));
233 } else {
234 strlcpy(b, c->req, sizeof(b));
237 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
238 t = meta + len;
240 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
241 (int)(t-meta), meta);
242 if (ec < 0)
243 err(1, "asprintf");
244 send_log(LOG_NOTICE, fmted, ec+1);
245 free(fmted);
250 static void
251 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
253 event_loopbreak();
256 static void
257 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
259 char *msg;
261 msg = imsg->data;
262 msg[datalen-1] = '\0';
264 if (conf.foreground) {
265 print_date();
266 fprintf(stderr, "%s\n", msg);
267 } else
268 syslog(LOG_DAEMON | imsg->hdr.peerid, "%s", msg);
271 static void
272 handle_dispatch_imsg(int fd, short ev, void *d)
274 struct imsgbuf *ibuf = d;
275 dispatch_imsg(ibuf, handlers, sizeof(handlers));
278 int
279 logger_main(int fd, struct imsgbuf *ibuf)
281 event_init();
283 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
284 event_add(&imsgev, NULL);
286 sandbox_logger_process();
288 event_dispatch();
290 return 0;