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>
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,
43 fatal(const char *fmt, ...)
49 if (conf.foreground) {
50 vfprintf(stderr, fmt, ap);
51 fprintf(stderr, "\n");
53 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
60 should_log(int priority)
68 return conf.verbose >= 1;
70 return conf.verbose >= 2;
72 return conf.verbose >= 3;
79 send_log(const char *msg, size_t len)
81 imsg_compose(&logibuf, IMSG_LOG, 0, 0, -1, msg, len);
82 /* XXX: use event_once() */
87 vlog(int priority, struct client *c,
88 const char *fmt, va_list ap)
90 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
95 if (!should_log(priority))
99 len = sizeof(c->addr);
100 ec = getnameinfo((struct sockaddr*)&c->addr, len,
103 NI_NUMERICHOST | NI_NUMERICSERV);
105 fatal("getnameinfo: %s", gai_strerror(ec));
108 if (vasprintf(&fmted, fmt, ap) == -1)
109 fatal("vasprintf: %s", strerror(errno));
112 ec = asprintf(&s, "internal: %s", fmted);
114 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
117 fatal("asprintf: %s", strerror(errno));
126 log_err(struct client *c, const char *fmt, ...)
131 vlog(LOG_ERR, c, fmt, ap);
136 log_warn(struct client *c, const char *fmt, ...)
141 vlog(LOG_WARNING, c, fmt, ap);
146 log_notice(struct client *c, const char *fmt, ...)
151 vlog(LOG_NOTICE, c, fmt, ap);
156 log_info(struct client *c, const char *fmt, ...)
161 vlog(LOG_INFO, c, fmt, ap);
166 log_debug(struct client *c, const char *fmt, ...)
171 vlog(LOG_DEBUG, c, fmt, ap);
175 /* strchr, but with a bound */
177 gmid_strnchr(char *s, int c, size_t len)
181 for (i = 0; i < len; ++i)
188 log_request(struct client *c, char *meta, size_t l)
190 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
195 len = sizeof(c->addr);
196 ec = getnameinfo((struct sockaddr*)&c->addr, len,
199 NI_NUMERICHOST | NI_NUMERICSERV);
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));
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));
222 strlcpy(b, c->req, sizeof(b));
225 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
228 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
229 (int)(t-meta), meta);
232 send_log(fmted, ec+1);
239 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
245 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
250 msg[datalen-1] = '\0';
253 fprintf(stderr, "%s\n", msg);
255 syslog(LOG_DAEMON, "%s", msg);
259 handle_dispatch_imsg(int fd, short ev, void *d)
261 struct imsgbuf *ibuf = d;
262 dispatch_imsg(ibuf, handlers, sizeof(handlers));
266 logger_main(int fd, struct imsgbuf *ibuf)
270 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
271 event_add(&imsgev, NULL);
273 sandbox_logger_process();