2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
19 #include <sys/types.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,
51 strftime(buf, sizeof(buf), "%F %T",
52 localtime_r(&t, &tminfo));
53 fprintf(stderr, "[%s] ", buf);
57 should_log(int priority)
65 return conf.verbose >= 1;
67 return conf.verbose >= 2;
69 return conf.verbose >= 3;
76 send_log(int priority, const char *msg, size_t len)
78 imsg_compose(&logibuf, IMSG_LOG, priority, 0, -1, msg, len);
83 fatal(const char *fmt, ...)
90 if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
91 send_log(LOG_ERR, fmted, r+1);
99 vlog(int priority, struct client *c,
100 const char *fmt, va_list ap)
102 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
107 if (!should_log(priority))
111 len = sizeof(c->addr);
112 ec = getnameinfo((struct sockaddr*)&c->addr, len,
115 NI_NUMERICHOST | NI_NUMERICSERV);
117 fatal("getnameinfo: %s", gai_strerror(ec));
120 if (vasprintf(&fmted, fmt, ap) == -1)
121 fatal("vasprintf: %s", strerror(errno));
124 ec = asprintf(&s, "internal: %s", fmted);
126 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
129 fatal("asprintf: %s", strerror(errno));
131 send_log(priority, s, ec+1);
138 log_err(struct client *c, const char *fmt, ...)
143 vlog(LOG_ERR, c, fmt, ap);
148 log_warn(struct client *c, const char *fmt, ...)
153 vlog(LOG_WARNING, c, fmt, ap);
158 log_notice(struct client *c, const char *fmt, ...)
163 vlog(LOG_NOTICE, c, fmt, ap);
168 log_info(struct client *c, const char *fmt, ...)
173 vlog(LOG_INFO, c, fmt, ap);
178 log_debug(struct client *c, const char *fmt, ...)
183 vlog(LOG_DEBUG, c, fmt, ap);
187 /* strchr, but with a bound */
189 gmid_strnchr(char *s, int c, size_t len)
193 for (i = 0; i < len; ++i)
200 log_request(struct client *c, char *meta, size_t l)
202 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
207 len = sizeof(c->addr);
208 ec = getnameinfo((struct sockaddr*)&c->addr, len,
211 NI_NUMERICHOST | NI_NUMERICSERV);
213 fatal("getnameinfo: %s", gai_strerror(ec));
215 if (c->iri.schema != NULL) {
216 /* serialize the IRI */
217 strlcpy(b, c->iri.schema, sizeof(b));
218 strlcat(b, "://", sizeof(b));
220 /* log the decoded host name, but if it was invalid
221 * use the raw one. */
222 if (*c->domain != '\0')
223 strlcat(b, c->domain, sizeof(b));
225 strlcat(b, c->iri.host, sizeof(b));
227 strlcat(b, "/", sizeof(b));
228 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
229 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
230 strlcat(b, "?", sizeof(b));
231 strlcat(b, c->iri.query, sizeof(b));
234 strlcpy(b, c->req, sizeof(b));
237 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
240 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
241 (int)(t-meta), meta);
244 send_log(LOG_NOTICE, fmted, ec+1);
251 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
257 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
262 msg[datalen-1] = '\0';
264 if (conf.foreground) {
266 fprintf(stderr, "%s\n", msg);
268 syslog(LOG_DAEMON | imsg->hdr.peerid, "%s", msg);
272 handle_dispatch_imsg(int fd, short ev, void *d)
274 struct imsgbuf *ibuf = d;
275 dispatch_imsg(ibuf, handlers, sizeof(handlers));
279 logger_main(int fd, struct imsgbuf *ibuf)
283 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
284 event_add(&imsgev, NULL);
286 sandbox_logger_process();