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 static struct event imsgev;
35 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
36 static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
37 static void handle_dispatch_imsg(int, short, void*);
39 static imsg_handlerfn *handlers[] = {
40 [IMSG_QUIT] = handle_imsg_quit,
41 [IMSG_LOG] = handle_imsg_log,
42 };
44 static inline void
45 print_date(void)
46 {
47 struct tm tminfo;
48 time_t t;
49 char buf[20];
51 time(&t);
52 strftime(buf, sizeof(buf), "%F %T",
53 localtime_r(&t, &tminfo));
54 fprintf(stderr, "[%s] ", buf);
55 }
57 static inline int
58 should_log(int priority)
59 {
60 switch (priority) {
61 case LOG_ERR:
62 return 1;
63 case LOG_WARNING:
64 return 1;
65 case LOG_NOTICE:
66 return conf.verbose >= 1;
67 case LOG_INFO:
68 return conf.verbose >= 2;
69 case LOG_DEBUG:
70 return conf.verbose >= 3;
71 default:
72 return 0;
73 }
74 }
76 static inline void
77 send_log(int priority, const char *msg, size_t len)
78 {
79 imsg_compose(&logibuf, IMSG_LOG, priority, 0, -1, msg, len);
80 imsg_flush(&logibuf);
81 }
83 void
84 fatal(const char *fmt, ...)
85 {
86 struct pollfd pfd;
87 va_list ap;
88 int r;
89 char *fmted;
91 va_start(ap, fmt);
92 if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
93 send_log(LOG_CRIT, fmted, r+1);
94 free(fmted);
96 /* wait for the logger process to shut down */
97 pfd.fd = logibuf.fd;
98 pfd.events = POLLIN;
99 poll(&pfd, 1, 1000);
101 va_end(ap);
102 exit(1);
105 static inline void
106 vlog(int priority, struct client *c,
107 const char *fmt, va_list ap)
109 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
110 char *fmted, *s;
111 size_t len;
112 int ec;
114 if (!should_log(priority))
115 return;
117 if (c != NULL) {
118 len = sizeof(c->addr);
119 ec = getnameinfo((struct sockaddr*)&c->addr, len,
120 hbuf, sizeof(hbuf),
121 sbuf, sizeof(sbuf),
122 NI_NUMERICHOST | NI_NUMERICSERV);
123 if (ec != 0)
124 fatal("getnameinfo: %s", gai_strerror(ec));
127 if (vasprintf(&fmted, fmt, ap) == -1)
128 fatal("vasprintf: %s", strerror(errno));
130 if (c == NULL)
131 ec = asprintf(&s, "internal: %s", fmted);
132 else
133 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
135 if (ec < 0)
136 fatal("asprintf: %s", strerror(errno));
138 send_log(priority, s, ec+1);
140 free(fmted);
141 free(s);
144 void
145 log_err(struct client *c, const char *fmt, ...)
147 va_list ap;
149 va_start(ap, fmt);
150 vlog(LOG_ERR, c, fmt, ap);
151 va_end(ap);
154 void
155 log_warn(struct client *c, const char *fmt, ...)
157 va_list ap;
159 va_start(ap, fmt);
160 vlog(LOG_WARNING, c, fmt, ap);
161 va_end(ap);
164 void
165 log_notice(struct client *c, const char *fmt, ...)
167 va_list ap;
169 va_start(ap, fmt);
170 vlog(LOG_NOTICE, c, fmt, ap);
171 va_end(ap);
174 void
175 log_info(struct client *c, const char *fmt, ...)
177 va_list ap;
179 va_start(ap, fmt);
180 vlog(LOG_INFO, c, fmt, ap);
181 va_end(ap);
184 void
185 log_debug(struct client *c, const char *fmt, ...)
187 va_list ap;
189 va_start(ap, fmt);
190 vlog(LOG_DEBUG, c, fmt, ap);
191 va_end(ap);
194 /* strchr, but with a bound */
195 static char *
196 gmid_strnchr(char *s, int c, size_t len)
198 size_t i;
200 for (i = 0; i < len; ++i)
201 if (s[i] == c)
202 return &s[i];
203 return NULL;
206 void
207 log_request(struct client *c, char *meta, size_t l)
209 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
210 char *t, *fmted;
211 size_t len;
212 int ec;
214 len = sizeof(c->addr);
215 ec = getnameinfo((struct sockaddr*)&c->addr, len,
216 hbuf, sizeof(hbuf),
217 sbuf, sizeof(sbuf),
218 NI_NUMERICHOST | NI_NUMERICSERV);
219 if (ec != 0)
220 fatal("getnameinfo: %s", gai_strerror(ec));
222 if (c->iri.schema != NULL) {
223 /* serialize the IRI */
224 strlcpy(b, c->iri.schema, sizeof(b));
225 strlcat(b, "://", sizeof(b));
227 /* log the decoded host name, but if it was invalid
228 * use the raw one. */
229 if (*c->domain != '\0')
230 strlcat(b, c->domain, sizeof(b));
231 else
232 strlcat(b, c->iri.host, sizeof(b));
234 strlcat(b, "/", sizeof(b));
235 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
236 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
237 strlcat(b, "?", sizeof(b));
238 strlcat(b, c->iri.query, sizeof(b));
240 } else {
241 strlcpy(b, c->req, sizeof(b));
244 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
245 t = meta + len;
247 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
248 (int)(t-meta), meta);
249 if (ec < 0)
250 err(1, "asprintf");
251 send_log(LOG_NOTICE, fmted, ec+1);
252 free(fmted);
257 static void
258 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
260 event_loopbreak();
263 static void
264 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
266 int priority, quit;
267 char *msg;
269 msg = imsg->data;
270 msg[datalen-1] = '\0';
272 priority = imsg->hdr.peerid;
274 quit = 0;
275 if (priority == LOG_CRIT) {
276 quit = 1;
277 priority = LOG_ERR;
280 if (conf.foreground) {
281 print_date();
282 fprintf(stderr, "%s\n", msg);
283 } else
284 syslog(LOG_DAEMON | priority, "%s", msg);
286 if (quit)
287 exit(1);
290 static void
291 handle_dispatch_imsg(int fd, short ev, void *d)
293 struct imsgbuf *ibuf = d;
294 dispatch_imsg(ibuf, handlers, sizeof(handlers));
297 int
298 logger_main(int fd, struct imsgbuf *ibuf)
300 event_init();
302 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
303 event_add(&imsgev, NULL);
305 sandbox_logger_process();
307 event_dispatch();
309 return 0;