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 FILE *log;
37 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
38 static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
39 static void handle_imsg_log_type(struct imsgbuf*, struct imsg*, size_t);
40 static void handle_dispatch_imsg(int, short, void*);
42 static imsg_handlerfn *handlers[] = {
43 [IMSG_QUIT] = handle_imsg_quit,
44 [IMSG_LOG] = handle_imsg_log,
45 [IMSG_LOG_TYPE] = handle_imsg_log_type,
46 };
48 static inline void
49 print_date(FILE *f)
50 {
51 struct tm tminfo;
52 time_t t;
53 char buf[20];
55 time(&t);
56 strftime(buf, sizeof(buf), "%F %T",
57 localtime_r(&t, &tminfo));
58 fprintf(f, "[%s] ", buf);
59 }
61 static inline int
62 should_log(int priority)
63 {
64 switch (priority) {
65 case LOG_ERR:
66 return 1;
67 case LOG_WARNING:
68 return 1;
69 case LOG_NOTICE:
70 return conf.verbose >= 1;
71 case LOG_INFO:
72 return conf.verbose >= 2;
73 case LOG_DEBUG:
74 return conf.verbose >= 3;
75 default:
76 return 0;
77 }
78 }
80 static inline void
81 send_log(int priority, const char *msg, size_t len)
82 {
83 imsg_compose(&logibuf, IMSG_LOG, priority, 0, -1, msg, len);
84 imsg_flush(&logibuf);
85 }
87 void
88 fatal(const char *fmt, ...)
89 {
90 struct pollfd pfd;
91 va_list ap;
92 int r;
93 char *fmted;
95 va_start(ap, fmt);
96 if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
97 send_log(LOG_CRIT, fmted, r+1);
98 free(fmted);
100 /* wait for the logger process to shut down */
101 pfd.fd = logibuf.fd;
102 pfd.events = POLLIN;
103 poll(&pfd, 1, 1000);
105 va_end(ap);
106 exit(1);
109 static inline void
110 vlog(int priority, struct client *c,
111 const char *fmt, va_list ap)
113 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
114 char *fmted, *s;
115 size_t len;
116 int ec;
118 if (!should_log(priority))
119 return;
121 if (c != NULL) {
122 len = sizeof(c->addr);
123 ec = getnameinfo((struct sockaddr*)&c->addr, len,
124 hbuf, sizeof(hbuf),
125 sbuf, sizeof(sbuf),
126 NI_NUMERICHOST | NI_NUMERICSERV);
127 if (ec != 0)
128 fatal("getnameinfo: %s", gai_strerror(ec));
131 if (vasprintf(&fmted, fmt, ap) == -1)
132 fatal("vasprintf: %s", strerror(errno));
134 if (c == NULL)
135 ec = asprintf(&s, "internal: %s", fmted);
136 else
137 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
139 if (ec < 0)
140 fatal("asprintf: %s", strerror(errno));
142 send_log(priority, s, ec+1);
144 free(fmted);
145 free(s);
148 void
149 log_err(struct client *c, const char *fmt, ...)
151 va_list ap;
153 va_start(ap, fmt);
154 vlog(LOG_ERR, c, fmt, ap);
155 va_end(ap);
158 void
159 log_warn(struct client *c, const char *fmt, ...)
161 va_list ap;
163 va_start(ap, fmt);
164 vlog(LOG_WARNING, c, fmt, ap);
165 va_end(ap);
168 void
169 log_notice(struct client *c, const char *fmt, ...)
171 va_list ap;
173 va_start(ap, fmt);
174 vlog(LOG_NOTICE, c, fmt, ap);
175 va_end(ap);
178 void
179 log_info(struct client *c, const char *fmt, ...)
181 va_list ap;
183 va_start(ap, fmt);
184 vlog(LOG_INFO, c, fmt, ap);
185 va_end(ap);
188 void
189 log_debug(struct client *c, const char *fmt, ...)
191 va_list ap;
193 va_start(ap, fmt);
194 vlog(LOG_DEBUG, c, fmt, ap);
195 va_end(ap);
198 /* strchr, but with a bound */
199 static char *
200 gmid_strnchr(char *s, int c, size_t len)
202 size_t i;
204 for (i = 0; i < len; ++i)
205 if (s[i] == c)
206 return &s[i];
207 return NULL;
210 void
211 log_request(struct client *c, char *meta, size_t l)
213 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
214 char *t, *fmted;
215 size_t len;
216 int ec;
218 len = sizeof(c->addr);
219 ec = getnameinfo((struct sockaddr*)&c->addr, len,
220 hbuf, sizeof(hbuf),
221 sbuf, sizeof(sbuf),
222 NI_NUMERICHOST | NI_NUMERICSERV);
223 if (ec != 0)
224 fatal("getnameinfo: %s", gai_strerror(ec));
226 if (c->iri.schema != NULL) {
227 /* serialize the IRI */
228 strlcpy(b, c->iri.schema, sizeof(b));
229 strlcat(b, "://", sizeof(b));
231 /* log the decoded host name, but if it was invalid
232 * use the raw one. */
233 if (*c->domain != '\0')
234 strlcat(b, c->domain, sizeof(b));
235 else
236 strlcat(b, c->iri.host, sizeof(b));
238 strlcat(b, "/", sizeof(b));
239 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
240 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
241 strlcat(b, "?", sizeof(b));
242 strlcat(b, c->iri.query, sizeof(b));
244 } else {
245 strlcpy(b, c->req, sizeof(b));
248 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
249 t = meta + len;
251 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
252 (int)(t-meta), meta);
253 if (ec < 0)
254 err(1, "asprintf");
255 send_log(LOG_NOTICE, fmted, ec+1);
256 free(fmted);
261 static void
262 do_log(int priority, const char *msg)
264 int quit = 0;
266 if (priority == LOG_CRIT) {
267 quit = 1;
268 priority = LOG_ERR;
271 if (log != NULL) {
272 print_date(log);
273 fprintf(log, "%s\n", msg);
274 } else
275 syslog(LOG_DAEMON | priority, "%s", msg);
277 if (quit)
278 exit(1);
281 static void
282 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
284 event_loopbreak();
287 static void
288 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
290 int priority;
291 char *msg;
293 msg = imsg->data;
294 msg[datalen-1] = '\0';
295 priority = imsg->hdr.peerid;
296 do_log(priority, msg);
299 static void
300 handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
302 if (log != NULL) {
303 fflush(log);
304 fclose(log);
305 log = NULL;
308 if (imsg->fd != -1) {
309 if ((log = fdopen(imsg->fd, "a")) == NULL) {
310 syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s",
311 strerror(errno));
312 exit(1);
317 static void
318 handle_dispatch_imsg(int fd, short ev, void *d)
320 struct imsgbuf *ibuf = d;
321 dispatch_imsg(ibuf, handlers, sizeof(handlers));
324 int
325 logger_main(int fd, struct imsgbuf *ibuf)
327 event_init();
329 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
330 event_add(&imsgev, NULL);
332 sandbox_logger_process();
334 event_dispatch();
336 return 0;