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 inlog;
33 static void handle_log(int, short, void*);
36 fatal(const char *fmt, ...)
42 if (conf.foreground) {
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
46 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
53 should_log(int priority)
61 return conf.verbose >= 1;
63 return conf.verbose >= 2;
65 return conf.verbose >= 3;
72 send_log(const char *msg, size_t len)
74 imsg_compose(&logpibuf, 0, 0, 0, -1, msg, len);
75 /* XXX: use event_once() */
76 imsg_flush(&logpibuf);
80 vlog(int priority, struct client *c,
81 const char *fmt, va_list ap)
83 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
88 if (!should_log(priority))
92 len = sizeof(c->addr);
93 ec = getnameinfo((struct sockaddr*)&c->addr, len,
96 NI_NUMERICHOST | NI_NUMERICSERV);
98 fatal("getnameinfo: %s", gai_strerror(ec));
101 if (vasprintf(&fmted, fmt, ap) == -1)
102 fatal("vasprintf: %s", strerror(errno));
105 ec = asprintf(&s, "internal: %s", fmted);
107 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
110 fatal("asprintf: %s", strerror(errno));
119 log_err(struct client *c, const char *fmt, ...)
124 vlog(LOG_ERR, c, fmt, ap);
129 log_warn(struct client *c, const char *fmt, ...)
134 vlog(LOG_WARNING, c, fmt, ap);
139 log_notice(struct client *c, const char *fmt, ...)
144 vlog(LOG_NOTICE, c, fmt, ap);
149 log_info(struct client *c, const char *fmt, ...)
154 vlog(LOG_INFO, c, fmt, ap);
159 log_debug(struct client *c, const char *fmt, ...)
164 vlog(LOG_DEBUG, c, fmt, ap);
168 /* strchr, but with a bound */
170 gmid_strnchr(char *s, int c, size_t len)
174 for (i = 0; i < len; ++i)
181 log_request(struct client *c, char *meta, size_t l)
183 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
188 len = sizeof(c->addr);
189 ec = getnameinfo((struct sockaddr*)&c->addr, len,
192 NI_NUMERICHOST | NI_NUMERICSERV);
194 fatal("getnameinfo: %s", gai_strerror(ec));
196 if (c->iri.schema != NULL) {
197 /* serialize the IRI */
198 strlcpy(b, c->iri.schema, sizeof(b));
199 strlcat(b, "://", sizeof(b));
201 /* log the decoded host name, but if it was invalid
202 * use the raw one. */
203 if (*c->domain != '\0')
204 strlcat(b, c->domain, sizeof(b));
206 strlcat(b, c->iri.host, sizeof(b));
208 strlcat(b, "/", sizeof(b));
209 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
210 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
211 strlcat(b, "?", sizeof(b));
212 strlcat(b, c->iri.query, sizeof(b));
215 strlcpy(b, c->req, sizeof(b));
218 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
221 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
222 (int)(t-meta), meta);
225 send_log(fmted, ec+1);
232 handle_log(int fd, short ev, void *d)
234 struct imsgbuf *ibuf = d;
239 if ((n = imsg_read(ibuf)) == -1) {
240 if (errno == EAGAIN || errno == EWOULDBLOCK)
245 errx(1, "connection lost?");
248 if ((n = imsg_get(ibuf, &imsg)) == -1)
249 err(1, "read error");
253 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
255 msg[datalen-1] = '\0';
257 /* ignore imsg.hdr.type for now */
259 fprintf(stderr, "%s\n", msg);
261 syslog(LOG_DAEMON, "%s", msg);
268 logger_main(int fd, struct imsgbuf *ibuf)
272 event_set(&inlog, fd, EV_READ | EV_PERSIST, &handle_log, ibuf);
273 event_add(&inlog, NULL);
276 if (pledge("stdio", NULL) == -1)