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>
31 static struct event imsgev;
33 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
34 static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
35 static void handle_dispatch_imsg(int, short, void*);
37 static imsg_handlerfn *handlers[] = {
38 [IMSG_QUIT] = handle_imsg_quit,
39 [IMSG_LOG] = handle_imsg_log,
40 };
42 void
43 fatal(const char *fmt, ...)
44 {
45 va_list ap;
47 va_start(ap, fmt);
49 if (conf.foreground) {
50 vfprintf(stderr, fmt, ap);
51 fprintf(stderr, "\n");
52 } else
53 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
55 va_end(ap);
56 exit(1);
57 }
59 static inline int
60 should_log(int priority)
61 {
62 switch (priority) {
63 case LOG_ERR:
64 return 1;
65 case LOG_WARNING:
66 return 1;
67 case LOG_NOTICE:
68 return conf.verbose >= 1;
69 case LOG_INFO:
70 return conf.verbose >= 2;
71 case LOG_DEBUG:
72 return conf.verbose >= 3;
73 default:
74 return 0;
75 }
76 }
78 static inline void
79 send_log(const char *msg, size_t len)
80 {
81 imsg_compose(&logibuf, IMSG_LOG, 0, 0, -1, msg, len);
82 /* XXX: use event_once() */
83 imsg_flush(&logibuf);
84 }
86 static inline void
87 vlog(int priority, struct client *c,
88 const char *fmt, va_list ap)
89 {
90 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
91 char *fmted, *s;
92 size_t len;
93 int ec;
95 if (!should_log(priority))
96 return;
98 if (c != NULL) {
99 len = sizeof(c->addr);
100 ec = getnameinfo((struct sockaddr*)&c->addr, len,
101 hbuf, sizeof(hbuf),
102 sbuf, sizeof(sbuf),
103 NI_NUMERICHOST | NI_NUMERICSERV);
104 if (ec != 0)
105 fatal("getnameinfo: %s", gai_strerror(ec));
108 if (vasprintf(&fmted, fmt, ap) == -1)
109 fatal("vasprintf: %s", strerror(errno));
111 if (c == NULL)
112 ec = asprintf(&s, "internal: %s", fmted);
113 else
114 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
116 if (ec < 0)
117 fatal("asprintf: %s", strerror(errno));
119 send_log(s, ec+1);
121 free(fmted);
122 free(s);
125 void
126 log_err(struct client *c, const char *fmt, ...)
128 va_list ap;
130 va_start(ap, fmt);
131 vlog(LOG_ERR, c, fmt, ap);
132 va_end(ap);
135 void
136 log_warn(struct client *c, const char *fmt, ...)
138 va_list ap;
140 va_start(ap, fmt);
141 vlog(LOG_WARNING, c, fmt, ap);
142 va_end(ap);
145 void
146 log_notice(struct client *c, const char *fmt, ...)
148 va_list ap;
150 va_start(ap, fmt);
151 vlog(LOG_NOTICE, c, fmt, ap);
152 va_end(ap);
155 void
156 log_info(struct client *c, const char *fmt, ...)
158 va_list ap;
160 va_start(ap, fmt);
161 vlog(LOG_INFO, c, fmt, ap);
162 va_end(ap);
165 void
166 log_debug(struct client *c, const char *fmt, ...)
168 va_list ap;
170 va_start(ap, fmt);
171 vlog(LOG_DEBUG, c, fmt, ap);
172 va_end(ap);
175 /* strchr, but with a bound */
176 static char *
177 gmid_strnchr(char *s, int c, size_t len)
179 size_t i;
181 for (i = 0; i < len; ++i)
182 if (s[i] == c)
183 return &s[i];
184 return NULL;
187 void
188 log_request(struct client *c, char *meta, size_t l)
190 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
191 char *t, *fmted;
192 size_t len;
193 int ec;
195 len = sizeof(c->addr);
196 ec = getnameinfo((struct sockaddr*)&c->addr, len,
197 hbuf, sizeof(hbuf),
198 sbuf, sizeof(sbuf),
199 NI_NUMERICHOST | NI_NUMERICSERV);
200 if (ec != 0)
201 fatal("getnameinfo: %s", gai_strerror(ec));
203 if (c->iri.schema != NULL) {
204 /* serialize the IRI */
205 strlcpy(b, c->iri.schema, sizeof(b));
206 strlcat(b, "://", sizeof(b));
208 /* log the decoded host name, but if it was invalid
209 * use the raw one. */
210 if (*c->domain != '\0')
211 strlcat(b, c->domain, sizeof(b));
212 else
213 strlcat(b, c->iri.host, sizeof(b));
215 strlcat(b, "/", sizeof(b));
216 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
217 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
218 strlcat(b, "?", sizeof(b));
219 strlcat(b, c->iri.query, sizeof(b));
221 } else {
222 strlcpy(b, c->req, sizeof(b));
225 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
226 t = meta + len;
228 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
229 (int)(t-meta), meta);
230 if (ec < 0)
231 err(1, "asprintf");
232 send_log(fmted, ec+1);
233 free(fmted);
238 static void
239 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
241 event_loopbreak();
244 static void
245 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
247 char *msg;
249 msg = imsg->data;
250 msg[datalen-1] = '\0';
252 if (conf.foreground)
253 fprintf(stderr, "%s\n", msg);
254 else
255 syslog(LOG_DAEMON, "%s", msg);
258 static void
259 handle_dispatch_imsg(int fd, short ev, void *d)
261 struct imsgbuf *ibuf = d;
262 dispatch_imsg(ibuf, handlers, sizeof(handlers));
265 int
266 logger_main(int fd, struct imsgbuf *ibuf)
268 event_init();
270 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
271 event_add(&imsgev, NULL);
273 sandbox_logger_process();
275 event_dispatch();
277 return 0;