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_REQUEST] = handle_imsg_log,
46 [IMSG_LOG_TYPE] = handle_imsg_log_type,
47 };
49 static inline void
50 print_date(FILE *f)
51 {
52 struct tm tminfo;
53 time_t t;
54 char buf[20];
56 time(&t);
57 strftime(buf, sizeof(buf), "%F %T",
58 localtime_r(&t, &tminfo));
59 fprintf(f, "[%s] ", buf);
60 }
62 static inline int
63 should_log(int priority)
64 {
65 switch (priority) {
66 case LOG_ERR:
67 return 1;
68 case LOG_WARNING:
69 return 1;
70 case LOG_NOTICE:
71 return conf.verbose >= 1;
72 case LOG_INFO:
73 return conf.verbose >= 2;
74 case LOG_DEBUG:
75 return conf.verbose >= 3;
76 default:
77 return 0;
78 }
79 }
81 static inline void
82 send_log(int type, int priority, const char *msg, size_t len)
83 {
84 imsg_compose(&logibuf, type, priority, 0, -1, msg, len);
85 imsg_flush(&logibuf);
86 }
88 void
89 fatal(const char *fmt, ...)
90 {
91 struct pollfd pfd;
92 va_list ap;
93 int r;
94 char *fmted;
96 va_start(ap, fmt);
97 if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
98 send_log(IMSG_LOG, LOG_CRIT, fmted, r+1);
99 free(fmted);
101 /* wait for the logger process to shut down */
102 pfd.fd = logibuf.fd;
103 pfd.events = POLLIN;
104 poll(&pfd, 1, 1000);
106 va_end(ap);
107 exit(1);
110 static inline void
111 vlog(int priority, struct client *c,
112 const char *fmt, va_list ap)
114 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
115 char *fmted, *s;
116 size_t len;
117 int ec;
119 if (!should_log(priority))
120 return;
122 if (c != NULL) {
123 len = sizeof(c->addr);
124 ec = getnameinfo((struct sockaddr*)&c->addr, len,
125 hbuf, sizeof(hbuf),
126 sbuf, sizeof(sbuf),
127 NI_NUMERICHOST | NI_NUMERICSERV);
128 if (ec != 0)
129 fatal("getnameinfo: %s: %s",
130 gai_strerror(ec), strerror(errno));
133 if (vasprintf(&fmted, fmt, ap) == -1)
134 fatal("vasprintf: %s", strerror(errno));
136 if (c == NULL)
137 ec = asprintf(&s, "internal: %s", fmted);
138 else
139 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
141 if (ec < 0)
142 fatal("asprintf: %s", strerror(errno));
144 send_log(IMSG_LOG, priority, s, ec+1);
146 free(fmted);
147 free(s);
150 void
151 log_err(struct client *c, const char *fmt, ...)
153 va_list ap;
155 va_start(ap, fmt);
156 vlog(LOG_ERR, c, fmt, ap);
157 va_end(ap);
160 void
161 log_warn(struct client *c, const char *fmt, ...)
163 va_list ap;
165 va_start(ap, fmt);
166 vlog(LOG_WARNING, c, fmt, ap);
167 va_end(ap);
170 void
171 log_notice(struct client *c, const char *fmt, ...)
173 va_list ap;
175 va_start(ap, fmt);
176 vlog(LOG_NOTICE, c, fmt, ap);
177 va_end(ap);
180 void
181 log_info(struct client *c, const char *fmt, ...)
183 va_list ap;
185 va_start(ap, fmt);
186 vlog(LOG_INFO, c, fmt, ap);
187 va_end(ap);
190 void
191 log_debug(struct client *c, const char *fmt, ...)
193 va_list ap;
195 va_start(ap, fmt);
196 vlog(LOG_DEBUG, c, fmt, ap);
197 va_end(ap);
200 /* strchr, but with a bound */
201 static char *
202 gmid_strnchr(char *s, int c, size_t len)
204 size_t i;
206 for (i = 0; i < len; ++i)
207 if (s[i] == c)
208 return &s[i];
209 return NULL;
212 void
213 log_request(struct client *c, char *meta, size_t l)
215 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
216 char *fmted;
217 const char *t;
218 size_t len;
219 int ec;
221 len = sizeof(c->addr);
222 ec = getnameinfo((struct sockaddr*)&c->addr, len,
223 hbuf, sizeof(hbuf),
224 sbuf, sizeof(sbuf),
225 NI_NUMERICHOST | NI_NUMERICSERV);
226 if (ec != 0)
227 fatal("getnameinfo: %s", gai_strerror(ec));
229 if (c->iri.schema != NULL) {
230 /* serialize the IRI */
231 strlcpy(b, c->iri.schema, sizeof(b));
232 strlcat(b, "://", sizeof(b));
234 /* log the decoded host name, but if it was invalid
235 * use the raw one. */
236 if (*c->domain != '\0')
237 strlcat(b, c->domain, sizeof(b));
238 else
239 strlcat(b, c->iri.host, sizeof(b));
241 if (*c->iri.path != '/')
242 strlcat(b, "/", sizeof(b));
243 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
244 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
245 strlcat(b, "?", sizeof(b));
246 strlcat(b, c->iri.query, sizeof(b));
248 } else {
249 if ((t = c->req) == NULL)
250 t = "";
251 strlcpy(b, t, sizeof(b));
254 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
255 t = meta + len;
257 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
258 (int)(t-meta), meta);
259 if (ec < 0)
260 err(1, "asprintf");
261 send_log(IMSG_LOG_REQUEST, LOG_NOTICE, fmted, ec+1);
262 free(fmted);
267 static void
268 do_log(int type, int priority, const char *msg)
270 int quit = 0;
272 if (priority == LOG_CRIT) {
273 quit = 1;
274 priority = LOG_ERR;
277 if (log != NULL) {
278 if (type != IMSG_LOG_REQUEST)
279 print_date(log);
280 fprintf(log, "%s\n", msg);
281 } else
282 syslog(LOG_DAEMON | priority, "%s", msg);
284 if (quit)
285 exit(1);
288 static void
289 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
291 event_loopbreak();
294 static void
295 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
297 int priority;
298 char *msg;
300 msg = imsg->data;
301 msg[datalen-1] = '\0';
302 priority = imsg->hdr.peerid;
303 do_log(imsg->hdr.type, priority, msg);
306 static void
307 handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
309 if (log != NULL && log != stderr) {
310 fflush(log);
311 fclose(log);
313 log = NULL;
315 if (imsg->fd != -1) {
316 if ((log = fdopen(imsg->fd, "a")) == NULL) {
317 syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s",
318 strerror(errno));
319 exit(1);
324 static void
325 handle_dispatch_imsg(int fd, short ev, void *d)
327 struct imsgbuf *ibuf = d;
328 dispatch_imsg(ibuf, handlers, sizeof(handlers));
331 int
332 logger_main(int fd, struct imsgbuf *ibuf)
334 log = stderr;
336 openlog(getprogname(), LOG_NDELAY, LOG_DAEMON);
338 event_init();
340 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
341 event_add(&imsgev, NULL);
343 sandbox_logger_process();
345 event_dispatch();
347 closelog();
349 return 0;