Blame


1 7e9b7812 2021-02-07 op /*
2 7e9b7812 2021-02-07 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 7e9b7812 2021-02-07 op *
4 7e9b7812 2021-02-07 op * Permission to use, copy, modify, and distribute this software for any
5 7e9b7812 2021-02-07 op * purpose with or without fee is hereby granted, provided that the above
6 7e9b7812 2021-02-07 op * copyright notice and this permission notice appear in all copies.
7 7e9b7812 2021-02-07 op *
8 7e9b7812 2021-02-07 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7e9b7812 2021-02-07 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7e9b7812 2021-02-07 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7e9b7812 2021-02-07 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7e9b7812 2021-02-07 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7e9b7812 2021-02-07 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7e9b7812 2021-02-07 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7e9b7812 2021-02-07 op */
16 7e9b7812 2021-02-07 op
17 52418c8d 2021-02-12 op #include "gmid.h"
18 52418c8d 2021-02-12 op
19 d278a0c3 2021-02-23 op #include <sys/types.h>
20 d278a0c3 2021-02-23 op #include <sys/uio.h>
21 d278a0c3 2021-02-23 op
22 7e9b7812 2021-02-07 op #include <errno.h>
23 d278a0c3 2021-02-23 op #include <event.h>
24 d278a0c3 2021-02-23 op #include <imsg.h>
25 7e9b7812 2021-02-07 op #include <netdb.h>
26 2ef7f631 2021-04-28 op #include <poll.h>
27 7e9b7812 2021-02-07 op #include <stdarg.h>
28 7e9b7812 2021-02-07 op #include <stdio.h>
29 7e9b7812 2021-02-07 op #include <string.h>
30 7e9b7812 2021-02-07 op #include <syslog.h>
31 b3342582 2021-04-14 op #include <time.h>
32 7e9b7812 2021-02-07 op
33 bc99d868 2021-03-19 op static struct event imsgev;
34 d278a0c3 2021-02-23 op
35 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
36 bc99d868 2021-03-19 op static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
37 bc99d868 2021-03-19 op static void handle_dispatch_imsg(int, short, void*);
38 d278a0c3 2021-02-23 op
39 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
40 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
41 bc99d868 2021-03-19 op [IMSG_LOG] = handle_imsg_log,
42 bc99d868 2021-03-19 op };
43 b3342582 2021-04-14 op
44 b3342582 2021-04-14 op static inline void
45 b3342582 2021-04-14 op print_date(void)
46 b3342582 2021-04-14 op {
47 b3342582 2021-04-14 op struct tm tminfo;
48 b3342582 2021-04-14 op time_t t;
49 b3342582 2021-04-14 op char buf[20];
50 b3342582 2021-04-14 op
51 b3342582 2021-04-14 op time(&t);
52 b3342582 2021-04-14 op strftime(buf, sizeof(buf), "%F %T",
53 b3342582 2021-04-14 op localtime_r(&t, &tminfo));
54 b3342582 2021-04-14 op fprintf(stderr, "[%s] ", buf);
55 7e9b7812 2021-02-07 op }
56 7e9b7812 2021-02-07 op
57 7e9b7812 2021-02-07 op static inline int
58 7e9b7812 2021-02-07 op should_log(int priority)
59 7e9b7812 2021-02-07 op {
60 7e9b7812 2021-02-07 op switch (priority) {
61 7e9b7812 2021-02-07 op case LOG_ERR:
62 7e9b7812 2021-02-07 op return 1;
63 7e9b7812 2021-02-07 op case LOG_WARNING:
64 7e9b7812 2021-02-07 op return 1;
65 7e9b7812 2021-02-07 op case LOG_NOTICE:
66 7e9b7812 2021-02-07 op return conf.verbose >= 1;
67 7e9b7812 2021-02-07 op case LOG_INFO:
68 7e9b7812 2021-02-07 op return conf.verbose >= 2;
69 7e9b7812 2021-02-07 op case LOG_DEBUG:
70 7e9b7812 2021-02-07 op return conf.verbose >= 3;
71 7e9b7812 2021-02-07 op default:
72 7e9b7812 2021-02-07 op return 0;
73 7e9b7812 2021-02-07 op }
74 7e9b7812 2021-02-07 op }
75 7e9b7812 2021-02-07 op
76 d278a0c3 2021-02-23 op static inline void
77 42447f67 2021-04-28 op send_log(int priority, const char *msg, size_t len)
78 d278a0c3 2021-02-23 op {
79 42447f67 2021-04-28 op imsg_compose(&logibuf, IMSG_LOG, priority, 0, -1, msg, len);
80 bc99d868 2021-03-19 op imsg_flush(&logibuf);
81 d278a0c3 2021-02-23 op }
82 d278a0c3 2021-02-23 op
83 d89a9060 2021-04-28 op void
84 d89a9060 2021-04-28 op fatal(const char *fmt, ...)
85 d89a9060 2021-04-28 op {
86 2ef7f631 2021-04-28 op struct pollfd pfd;
87 d89a9060 2021-04-28 op va_list ap;
88 d89a9060 2021-04-28 op int r;
89 d89a9060 2021-04-28 op char *fmted;
90 d89a9060 2021-04-28 op
91 d89a9060 2021-04-28 op va_start(ap, fmt);
92 d89a9060 2021-04-28 op if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
93 2ef7f631 2021-04-28 op send_log(LOG_CRIT, fmted, r+1);
94 d89a9060 2021-04-28 op free(fmted);
95 2ef7f631 2021-04-28 op
96 2ef7f631 2021-04-28 op /* wait for the logger process to shut down */
97 2ef7f631 2021-04-28 op pfd.fd = logibuf.fd;
98 2ef7f631 2021-04-28 op pfd.events = POLLIN;
99 2ef7f631 2021-04-28 op poll(&pfd, 1, 1000);
100 d89a9060 2021-04-28 op }
101 d89a9060 2021-04-28 op va_end(ap);
102 d89a9060 2021-04-28 op exit(1);
103 d89a9060 2021-04-28 op }
104 d89a9060 2021-04-28 op
105 d278a0c3 2021-02-23 op static inline void
106 d278a0c3 2021-02-23 op vlog(int priority, struct client *c,
107 7e9b7812 2021-02-07 op const char *fmt, va_list ap)
108 7e9b7812 2021-02-07 op {
109 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
110 7e9b7812 2021-02-07 op char *fmted, *s;
111 7e9b7812 2021-02-07 op size_t len;
112 7e9b7812 2021-02-07 op int ec;
113 7e9b7812 2021-02-07 op
114 7e9b7812 2021-02-07 op if (!should_log(priority))
115 7e9b7812 2021-02-07 op return;
116 7e9b7812 2021-02-07 op
117 d278a0c3 2021-02-23 op if (c != NULL) {
118 7e9b7812 2021-02-07 op len = sizeof(c->addr);
119 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
120 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
121 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
122 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
123 7e9b7812 2021-02-07 op if (ec != 0)
124 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
125 7e9b7812 2021-02-07 op }
126 7e9b7812 2021-02-07 op
127 7e9b7812 2021-02-07 op if (vasprintf(&fmted, fmt, ap) == -1)
128 7e9b7812 2021-02-07 op fatal("vasprintf: %s", strerror(errno));
129 7e9b7812 2021-02-07 op
130 d278a0c3 2021-02-23 op if (c == NULL)
131 d278a0c3 2021-02-23 op ec = asprintf(&s, "internal: %s", fmted);
132 d278a0c3 2021-02-23 op else
133 d278a0c3 2021-02-23 op ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
134 7e9b7812 2021-02-07 op
135 d278a0c3 2021-02-23 op if (ec < 0)
136 d278a0c3 2021-02-23 op fatal("asprintf: %s", strerror(errno));
137 d278a0c3 2021-02-23 op
138 42447f67 2021-04-28 op send_log(priority, s, ec+1);
139 d278a0c3 2021-02-23 op
140 7e9b7812 2021-02-07 op free(fmted);
141 d278a0c3 2021-02-23 op free(s);
142 7e9b7812 2021-02-07 op }
143 7e9b7812 2021-02-07 op
144 7e9b7812 2021-02-07 op void
145 7e9b7812 2021-02-07 op log_err(struct client *c, const char *fmt, ...)
146 7e9b7812 2021-02-07 op {
147 7e9b7812 2021-02-07 op va_list ap;
148 7e9b7812 2021-02-07 op
149 7e9b7812 2021-02-07 op va_start(ap, fmt);
150 d278a0c3 2021-02-23 op vlog(LOG_ERR, c, fmt, ap);
151 7e9b7812 2021-02-07 op va_end(ap);
152 7e9b7812 2021-02-07 op }
153 7e9b7812 2021-02-07 op
154 7e9b7812 2021-02-07 op void
155 7e9b7812 2021-02-07 op log_warn(struct client *c, const char *fmt, ...)
156 7e9b7812 2021-02-07 op {
157 7e9b7812 2021-02-07 op va_list ap;
158 7e9b7812 2021-02-07 op
159 7e9b7812 2021-02-07 op va_start(ap, fmt);
160 d278a0c3 2021-02-23 op vlog(LOG_WARNING, c, fmt, ap);
161 7e9b7812 2021-02-07 op va_end(ap);
162 7e9b7812 2021-02-07 op }
163 7e9b7812 2021-02-07 op
164 7e9b7812 2021-02-07 op void
165 7e9b7812 2021-02-07 op log_notice(struct client *c, const char *fmt, ...)
166 7e9b7812 2021-02-07 op {
167 7e9b7812 2021-02-07 op va_list ap;
168 7e9b7812 2021-02-07 op
169 7e9b7812 2021-02-07 op va_start(ap, fmt);
170 d278a0c3 2021-02-23 op vlog(LOG_NOTICE, c, fmt, ap);
171 7e9b7812 2021-02-07 op va_end(ap);
172 7e9b7812 2021-02-07 op }
173 7e9b7812 2021-02-07 op
174 7e9b7812 2021-02-07 op void
175 7e9b7812 2021-02-07 op log_info(struct client *c, const char *fmt, ...)
176 7e9b7812 2021-02-07 op {
177 7e9b7812 2021-02-07 op va_list ap;
178 7e9b7812 2021-02-07 op
179 7e9b7812 2021-02-07 op va_start(ap, fmt);
180 d278a0c3 2021-02-23 op vlog(LOG_INFO, c, fmt, ap);
181 7e9b7812 2021-02-07 op va_end(ap);
182 7e9b7812 2021-02-07 op }
183 7e9b7812 2021-02-07 op
184 7e9b7812 2021-02-07 op void
185 7e9b7812 2021-02-07 op log_debug(struct client *c, const char *fmt, ...)
186 7e9b7812 2021-02-07 op {
187 7e9b7812 2021-02-07 op va_list ap;
188 7e9b7812 2021-02-07 op
189 7e9b7812 2021-02-07 op va_start(ap, fmt);
190 d278a0c3 2021-02-23 op vlog(LOG_DEBUG, c, fmt, ap);
191 7e9b7812 2021-02-07 op va_end(ap);
192 7e9b7812 2021-02-07 op }
193 7e9b7812 2021-02-07 op
194 7e9b7812 2021-02-07 op /* strchr, but with a bound */
195 7e9b7812 2021-02-07 op static char *
196 7e9b7812 2021-02-07 op gmid_strnchr(char *s, int c, size_t len)
197 7e9b7812 2021-02-07 op {
198 7e9b7812 2021-02-07 op size_t i;
199 7e9b7812 2021-02-07 op
200 7e9b7812 2021-02-07 op for (i = 0; i < len; ++i)
201 7e9b7812 2021-02-07 op if (s[i] == c)
202 7e9b7812 2021-02-07 op return &s[i];
203 7e9b7812 2021-02-07 op return NULL;
204 7e9b7812 2021-02-07 op }
205 7e9b7812 2021-02-07 op
206 7e9b7812 2021-02-07 op void
207 7e9b7812 2021-02-07 op log_request(struct client *c, char *meta, size_t l)
208 7e9b7812 2021-02-07 op {
209 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
210 d278a0c3 2021-02-23 op char *t, *fmted;
211 7e9b7812 2021-02-07 op size_t len;
212 7e9b7812 2021-02-07 op int ec;
213 7e9b7812 2021-02-07 op
214 7e9b7812 2021-02-07 op len = sizeof(c->addr);
215 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
216 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
217 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
218 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
219 7e9b7812 2021-02-07 op if (ec != 0)
220 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
221 7e9b7812 2021-02-07 op
222 7e9b7812 2021-02-07 op if (c->iri.schema != NULL) {
223 7e9b7812 2021-02-07 op /* serialize the IRI */
224 7e9b7812 2021-02-07 op strlcpy(b, c->iri.schema, sizeof(b));
225 7e9b7812 2021-02-07 op strlcat(b, "://", sizeof(b));
226 7e9b7812 2021-02-07 op
227 7e9b7812 2021-02-07 op /* log the decoded host name, but if it was invalid
228 7e9b7812 2021-02-07 op * use the raw one. */
229 7e9b7812 2021-02-07 op if (*c->domain != '\0')
230 7e9b7812 2021-02-07 op strlcat(b, c->domain, sizeof(b));
231 7e9b7812 2021-02-07 op else
232 7e9b7812 2021-02-07 op strlcat(b, c->iri.host, sizeof(b));
233 7e9b7812 2021-02-07 op
234 7e9b7812 2021-02-07 op strlcat(b, "/", sizeof(b));
235 7e9b7812 2021-02-07 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
236 7e9b7812 2021-02-07 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
237 7e9b7812 2021-02-07 op strlcat(b, "?", sizeof(b));
238 7e9b7812 2021-02-07 op strlcat(b, c->iri.query, sizeof(b));
239 7e9b7812 2021-02-07 op }
240 7e9b7812 2021-02-07 op } else {
241 7e9b7812 2021-02-07 op strlcpy(b, c->req, sizeof(b));
242 7e9b7812 2021-02-07 op }
243 7e9b7812 2021-02-07 op
244 7e9b7812 2021-02-07 op if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
245 7e9b7812 2021-02-07 op t = meta + len;
246 7e9b7812 2021-02-07 op
247 d278a0c3 2021-02-23 op ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
248 d278a0c3 2021-02-23 op (int)(t-meta), meta);
249 d278a0c3 2021-02-23 op if (ec < 0)
250 d278a0c3 2021-02-23 op err(1, "asprintf");
251 42447f67 2021-04-28 op send_log(LOG_NOTICE, fmted, ec+1);
252 d278a0c3 2021-02-23 op free(fmted);
253 d278a0c3 2021-02-23 op }
254 d278a0c3 2021-02-23 op
255 d278a0c3 2021-02-23 op
256 d278a0c3 2021-02-23 op
257 d278a0c3 2021-02-23 op static void
258 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
259 d278a0c3 2021-02-23 op {
260 bc99d868 2021-03-19 op event_loopbreak();
261 bc99d868 2021-03-19 op }
262 d278a0c3 2021-02-23 op
263 bc99d868 2021-03-19 op static void
264 bc99d868 2021-03-19 op handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
265 bc99d868 2021-03-19 op {
266 2ef7f631 2021-04-28 op int priority, quit;
267 2ef7f631 2021-04-28 op char *msg;
268 d278a0c3 2021-02-23 op
269 bc99d868 2021-03-19 op msg = imsg->data;
270 bc99d868 2021-03-19 op msg[datalen-1] = '\0';
271 d278a0c3 2021-02-23 op
272 2ef7f631 2021-04-28 op priority = imsg->hdr.peerid;
273 2ef7f631 2021-04-28 op
274 2ef7f631 2021-04-28 op quit = 0;
275 2ef7f631 2021-04-28 op if (priority == LOG_CRIT) {
276 2ef7f631 2021-04-28 op quit = 1;
277 2ef7f631 2021-04-28 op priority = LOG_ERR;
278 2ef7f631 2021-04-28 op }
279 2ef7f631 2021-04-28 op
280 b3342582 2021-04-14 op if (conf.foreground) {
281 b3342582 2021-04-14 op print_date();
282 bc99d868 2021-03-19 op fprintf(stderr, "%s\n", msg);
283 b3342582 2021-04-14 op } else
284 2ef7f631 2021-04-28 op syslog(LOG_DAEMON | priority, "%s", msg);
285 2ef7f631 2021-04-28 op
286 2ef7f631 2021-04-28 op if (quit)
287 2ef7f631 2021-04-28 op exit(1);
288 bc99d868 2021-03-19 op }
289 d278a0c3 2021-02-23 op
290 bc99d868 2021-03-19 op static void
291 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
292 bc99d868 2021-03-19 op {
293 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
294 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
295 7e9b7812 2021-02-07 op }
296 d278a0c3 2021-02-23 op
297 376a5407 2021-02-23 op int
298 d278a0c3 2021-02-23 op logger_main(int fd, struct imsgbuf *ibuf)
299 d278a0c3 2021-02-23 op {
300 d278a0c3 2021-02-23 op event_init();
301 d278a0c3 2021-02-23 op
302 bc99d868 2021-03-19 op event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
303 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
304 d278a0c3 2021-02-23 op
305 62e001b0 2021-03-20 op sandbox_logger_process();
306 d278a0c3 2021-02-23 op
307 d278a0c3 2021-02-23 op event_dispatch();
308 d278a0c3 2021-02-23 op
309 d278a0c3 2021-02-23 op return 0;
310 d278a0c3 2021-02-23 op }