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 void
57 fatal(const char *fmt, ...)
58 {
59 va_list ap;
61 va_start(ap, fmt);
63 if (conf.foreground) {
64 print_date();
65 vfprintf(stderr, fmt, ap);
66 fprintf(stderr, "\n");
67 } else
68 vsyslog(LOG_DAEMON | LOG_ERR, fmt, ap);
70 va_end(ap);
71 exit(1);
72 }
74 static inline int
75 should_log(int priority)
76 {
77 switch (priority) {
78 case LOG_ERR:
79 return 1;
80 case LOG_WARNING:
81 return 1;
82 case LOG_NOTICE:
83 return conf.verbose >= 1;
84 case LOG_INFO:
85 return conf.verbose >= 2;
86 case LOG_DEBUG:
87 return conf.verbose >= 3;
88 default:
89 return 0;
90 }
91 }
93 static inline void
94 send_log(int priority, const char *msg, size_t len)
95 {
96 imsg_compose(&logibuf, IMSG_LOG, priority, 0, -1, msg, len);
97 imsg_flush(&logibuf);
98 }
100 static inline void
101 vlog(int priority, struct client *c,
102 const char *fmt, va_list ap)
104 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
105 char *fmted, *s;
106 size_t len;
107 int ec;
109 if (!should_log(priority))
110 return;
112 if (c != NULL) {
113 len = sizeof(c->addr);
114 ec = getnameinfo((struct sockaddr*)&c->addr, len,
115 hbuf, sizeof(hbuf),
116 sbuf, sizeof(sbuf),
117 NI_NUMERICHOST | NI_NUMERICSERV);
118 if (ec != 0)
119 fatal("getnameinfo: %s", gai_strerror(ec));
122 if (vasprintf(&fmted, fmt, ap) == -1)
123 fatal("vasprintf: %s", strerror(errno));
125 if (c == NULL)
126 ec = asprintf(&s, "internal: %s", fmted);
127 else
128 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
130 if (ec < 0)
131 fatal("asprintf: %s", strerror(errno));
133 send_log(priority, s, ec+1);
135 free(fmted);
136 free(s);
139 void
140 log_err(struct client *c, const char *fmt, ...)
142 va_list ap;
144 va_start(ap, fmt);
145 vlog(LOG_ERR, c, fmt, ap);
146 va_end(ap);
149 void
150 log_warn(struct client *c, const char *fmt, ...)
152 va_list ap;
154 va_start(ap, fmt);
155 vlog(LOG_WARNING, c, fmt, ap);
156 va_end(ap);
159 void
160 log_notice(struct client *c, const char *fmt, ...)
162 va_list ap;
164 va_start(ap, fmt);
165 vlog(LOG_NOTICE, c, fmt, ap);
166 va_end(ap);
169 void
170 log_info(struct client *c, const char *fmt, ...)
172 va_list ap;
174 va_start(ap, fmt);
175 vlog(LOG_INFO, c, fmt, ap);
176 va_end(ap);
179 void
180 log_debug(struct client *c, const char *fmt, ...)
182 va_list ap;
184 va_start(ap, fmt);
185 vlog(LOG_DEBUG, c, fmt, ap);
186 va_end(ap);
189 /* strchr, but with a bound */
190 static char *
191 gmid_strnchr(char *s, int c, size_t len)
193 size_t i;
195 for (i = 0; i < len; ++i)
196 if (s[i] == c)
197 return &s[i];
198 return NULL;
201 void
202 log_request(struct client *c, char *meta, size_t l)
204 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
205 char *t, *fmted;
206 size_t len;
207 int ec;
209 len = sizeof(c->addr);
210 ec = getnameinfo((struct sockaddr*)&c->addr, len,
211 hbuf, sizeof(hbuf),
212 sbuf, sizeof(sbuf),
213 NI_NUMERICHOST | NI_NUMERICSERV);
214 if (ec != 0)
215 fatal("getnameinfo: %s", gai_strerror(ec));
217 if (c->iri.schema != NULL) {
218 /* serialize the IRI */
219 strlcpy(b, c->iri.schema, sizeof(b));
220 strlcat(b, "://", sizeof(b));
222 /* log the decoded host name, but if it was invalid
223 * use the raw one. */
224 if (*c->domain != '\0')
225 strlcat(b, c->domain, sizeof(b));
226 else
227 strlcat(b, c->iri.host, sizeof(b));
229 strlcat(b, "/", sizeof(b));
230 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
231 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
232 strlcat(b, "?", sizeof(b));
233 strlcat(b, c->iri.query, sizeof(b));
235 } else {
236 strlcpy(b, c->req, sizeof(b));
239 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
240 t = meta + len;
242 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
243 (int)(t-meta), meta);
244 if (ec < 0)
245 err(1, "asprintf");
246 send_log(LOG_NOTICE, fmted, ec+1);
247 free(fmted);
252 static void
253 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
255 event_loopbreak();
258 static void
259 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
261 char *msg;
263 msg = imsg->data;
264 msg[datalen-1] = '\0';
266 if (conf.foreground) {
267 print_date();
268 fprintf(stderr, "%s\n", msg);
269 } else
270 syslog(LOG_DAEMON | imsg->hdr.peerid, "%s", msg);
273 static void
274 handle_dispatch_imsg(int fd, short ev, void *d)
276 struct imsgbuf *ibuf = d;
277 dispatch_imsg(ibuf, handlers, sizeof(handlers));
280 int
281 logger_main(int fd, struct imsgbuf *ibuf)
283 event_init();
285 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
286 event_add(&imsgev, NULL);
288 sandbox_logger_process();
290 event_dispatch();
292 return 0;