2 7e9b7812 2021-02-07 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 7e9b7812 2021-02-07 op #include <errno.h>
18 7e9b7812 2021-02-07 op #include <netdb.h>
19 7e9b7812 2021-02-07 op #include <stdarg.h>
20 7e9b7812 2021-02-07 op #include <stdio.h>
21 7e9b7812 2021-02-07 op #include <string.h>
22 7e9b7812 2021-02-07 op #include <syslog.h>
24 7e9b7812 2021-02-07 op #include "gmid.h"
27 7e9b7812 2021-02-07 op fatal(const char *fmt, ...)
31 7e9b7812 2021-02-07 op va_start(ap, fmt);
33 7e9b7812 2021-02-07 op if (conf.foreground) {
34 7e9b7812 2021-02-07 op vfprintf(stderr, fmt, ap);
35 7e9b7812 2021-02-07 op fprintf(stderr, "\n");
37 7e9b7812 2021-02-07 op vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
43 7e9b7812 2021-02-07 op static inline int
44 7e9b7812 2021-02-07 op should_log(int priority)
46 7e9b7812 2021-02-07 op switch (priority) {
49 7e9b7812 2021-02-07 op case LOG_WARNING:
51 7e9b7812 2021-02-07 op case LOG_NOTICE:
52 7e9b7812 2021-02-07 op return conf.verbose >= 1;
53 7e9b7812 2021-02-07 op case LOG_INFO:
54 7e9b7812 2021-02-07 op return conf.verbose >= 2;
55 7e9b7812 2021-02-07 op case LOG_DEBUG:
56 7e9b7812 2021-02-07 op return conf.verbose >= 3;
63 7e9b7812 2021-02-07 op do_log(int priority, struct client *c,
64 7e9b7812 2021-02-07 op const char *fmt, va_list ap)
66 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
67 7e9b7812 2021-02-07 op char *fmted, *s;
71 7e9b7812 2021-02-07 op if (!should_log(priority))
74 7e9b7812 2021-02-07 op if (c == NULL) {
75 7e9b7812 2021-02-07 op strncpy(hbuf, "<internal>", sizeof(hbuf));
76 7e9b7812 2021-02-07 op sbuf[0] = '\0';
78 7e9b7812 2021-02-07 op len = sizeof(c->addr);
79 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
80 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
81 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
82 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
84 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
87 7e9b7812 2021-02-07 op if (vasprintf(&fmted, fmt, ap) == -1)
88 7e9b7812 2021-02-07 op fatal("vasprintf: %s", strerror(errno));
90 7e9b7812 2021-02-07 op if (conf.foreground)
91 7e9b7812 2021-02-07 op fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
93 7e9b7812 2021-02-07 op if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
94 7e9b7812 2021-02-07 op fatal("asprintf: %s", strerror(errno));
95 7e9b7812 2021-02-07 op syslog(priority | LOG_DAEMON, "%s", s);
103 7e9b7812 2021-02-07 op log_err(struct client *c, const char *fmt, ...)
107 7e9b7812 2021-02-07 op va_start(ap, fmt);
108 7e9b7812 2021-02-07 op do_log(LOG_ERR, c, fmt, ap);
113 7e9b7812 2021-02-07 op log_warn(struct client *c, const char *fmt, ...)
117 7e9b7812 2021-02-07 op va_start(ap, fmt);
118 7e9b7812 2021-02-07 op do_log(LOG_WARNING, c, fmt, ap);
123 7e9b7812 2021-02-07 op log_notice(struct client *c, const char *fmt, ...)
127 7e9b7812 2021-02-07 op va_start(ap, fmt);
128 7e9b7812 2021-02-07 op do_log(LOG_NOTICE, c, fmt, ap);
133 7e9b7812 2021-02-07 op log_info(struct client *c, const char *fmt, ...)
137 7e9b7812 2021-02-07 op va_start(ap, fmt);
138 7e9b7812 2021-02-07 op do_log(LOG_INFO, c, fmt, ap);
143 7e9b7812 2021-02-07 op log_debug(struct client *c, const char *fmt, ...)
147 7e9b7812 2021-02-07 op va_start(ap, fmt);
148 7e9b7812 2021-02-07 op do_log(LOG_DEBUG, c, fmt, ap);
152 7e9b7812 2021-02-07 op /* strchr, but with a bound */
153 7e9b7812 2021-02-07 op static char *
154 7e9b7812 2021-02-07 op gmid_strnchr(char *s, int c, size_t len)
158 7e9b7812 2021-02-07 op for (i = 0; i < len; ++i)
159 7e9b7812 2021-02-07 op if (s[i] == c)
160 7e9b7812 2021-02-07 op return &s[i];
165 7e9b7812 2021-02-07 op log_request(struct client *c, char *meta, size_t l)
167 7e9b7812 2021-02-07 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
172 7e9b7812 2021-02-07 op len = sizeof(c->addr);
173 7e9b7812 2021-02-07 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
174 7e9b7812 2021-02-07 op hbuf, sizeof(hbuf),
175 7e9b7812 2021-02-07 op sbuf, sizeof(sbuf),
176 7e9b7812 2021-02-07 op NI_NUMERICHOST | NI_NUMERICSERV);
178 7e9b7812 2021-02-07 op fatal("getnameinfo: %s", gai_strerror(ec));
180 7e9b7812 2021-02-07 op if (c->iri.schema != NULL) {
181 7e9b7812 2021-02-07 op /* serialize the IRI */
182 7e9b7812 2021-02-07 op strlcpy(b, c->iri.schema, sizeof(b));
183 7e9b7812 2021-02-07 op strlcat(b, "://", sizeof(b));
185 7e9b7812 2021-02-07 op /* log the decoded host name, but if it was invalid
186 7e9b7812 2021-02-07 op * use the raw one. */
187 7e9b7812 2021-02-07 op if (*c->domain != '\0')
188 7e9b7812 2021-02-07 op strlcat(b, c->domain, sizeof(b));
190 7e9b7812 2021-02-07 op strlcat(b, c->iri.host, sizeof(b));
192 7e9b7812 2021-02-07 op strlcat(b, "/", sizeof(b));
193 7e9b7812 2021-02-07 op strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
194 7e9b7812 2021-02-07 op if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
195 7e9b7812 2021-02-07 op strlcat(b, "?", sizeof(b));
196 7e9b7812 2021-02-07 op strlcat(b, c->iri.query, sizeof(b));
199 7e9b7812 2021-02-07 op strlcpy(b, c->req, sizeof(b));
202 7e9b7812 2021-02-07 op if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
203 7e9b7812 2021-02-07 op t = meta + len;
205 7e9b7812 2021-02-07 op if (conf.foreground)
206 7e9b7812 2021-02-07 op fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
207 7e9b7812 2021-02-07 op (int)(t - meta), meta);
209 7e9b7812 2021-02-07 op syslog(LOG_INFO | LOG_DAEMON, "%s:%s GET %s %.*s",
210 7e9b7812 2021-02-07 op hbuf, sbuf, b, (int)(t - meta), meta);