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.
27 fatal(const char *fmt, ...)
33 if (conf.foreground) {
34 vfprintf(stderr, fmt, ap);
35 fprintf(stderr, "\n");
37 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
44 should_log(int priority)
52 return conf.verbose >= 1;
54 return conf.verbose >= 2;
56 return conf.verbose >= 3;
63 do_log(int priority, struct client *c,
64 const char *fmt, va_list ap)
66 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
71 if (!should_log(priority))
75 strncpy(hbuf, "<internal>", sizeof(hbuf));
78 len = sizeof(c->addr);
79 ec = getnameinfo((struct sockaddr*)&c->addr, len,
82 NI_NUMERICHOST | NI_NUMERICSERV);
84 fatal("getnameinfo: %s", gai_strerror(ec));
87 if (vasprintf(&fmted, fmt, ap) == -1)
88 fatal("vasprintf: %s", strerror(errno));
91 fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
93 if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
94 fatal("asprintf: %s", strerror(errno));
95 syslog(priority | LOG_DAEMON, "%s", s);
103 log_err(struct client *c, const char *fmt, ...)
108 do_log(LOG_ERR, c, fmt, ap);
113 log_warn(struct client *c, const char *fmt, ...)
118 do_log(LOG_WARNING, c, fmt, ap);
123 log_notice(struct client *c, const char *fmt, ...)
128 do_log(LOG_NOTICE, c, fmt, ap);
133 log_info(struct client *c, const char *fmt, ...)
138 do_log(LOG_INFO, c, fmt, ap);
143 log_debug(struct client *c, const char *fmt, ...)
148 do_log(LOG_DEBUG, c, fmt, ap);
152 /* strchr, but with a bound */
154 gmid_strnchr(char *s, int c, size_t len)
158 for (i = 0; i < len; ++i)
165 log_request(struct client *c, char *meta, size_t l)
167 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
172 len = sizeof(c->addr);
173 ec = getnameinfo((struct sockaddr*)&c->addr, len,
176 NI_NUMERICHOST | NI_NUMERICSERV);
178 fatal("getnameinfo: %s", gai_strerror(ec));
180 if (c->iri.schema != NULL) {
181 /* serialize the IRI */
182 strlcpy(b, c->iri.schema, sizeof(b));
183 strlcat(b, "://", sizeof(b));
185 /* log the decoded host name, but if it was invalid
186 * use the raw one. */
187 if (*c->domain != '\0')
188 strlcat(b, c->domain, sizeof(b));
190 strlcat(b, c->iri.host, sizeof(b));
192 strlcat(b, "/", sizeof(b));
193 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
194 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
195 strlcat(b, "?", sizeof(b));
196 strlcat(b, c->iri.query, sizeof(b));
199 strlcpy(b, c->req, sizeof(b));
202 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
206 fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
207 (int)(t - meta), meta);
209 syslog(LOG_INFO | LOG_DAEMON, "%s:%s GET %s %.*s",
210 hbuf, sbuf, b, (int)(t - meta), meta);