Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "gmid.h"
19 #include <errno.h>
20 #include <netdb.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <syslog.h>
26 void
27 fatal(const char *fmt, ...)
28 {
29 va_list ap;
31 va_start(ap, fmt);
33 if (conf.foreground) {
34 vfprintf(stderr, fmt, ap);
35 fprintf(stderr, "\n");
36 } else
37 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
39 va_end(ap);
40 exit(1);
41 }
43 static inline int
44 should_log(int priority)
45 {
46 switch (priority) {
47 case LOG_ERR:
48 return 1;
49 case LOG_WARNING:
50 return 1;
51 case LOG_NOTICE:
52 return conf.verbose >= 1;
53 case LOG_INFO:
54 return conf.verbose >= 2;
55 case LOG_DEBUG:
56 return conf.verbose >= 3;
57 default:
58 return 0;
59 }
60 }
62 static void
63 do_log(int priority, struct client *c,
64 const char *fmt, va_list ap)
65 {
66 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
67 char *fmted, *s;
68 size_t len;
69 int ec;
71 if (!should_log(priority))
72 return;
74 if (c == NULL) {
75 strncpy(hbuf, "<internal>", sizeof(hbuf));
76 sbuf[0] = '\0';
77 } else {
78 len = sizeof(c->addr);
79 ec = getnameinfo((struct sockaddr*)&c->addr, len,
80 hbuf, sizeof(hbuf),
81 sbuf, sizeof(sbuf),
82 NI_NUMERICHOST | NI_NUMERICSERV);
83 if (ec != 0)
84 fatal("getnameinfo: %s", gai_strerror(ec));
85 }
87 if (vasprintf(&fmted, fmt, ap) == -1)
88 fatal("vasprintf: %s", strerror(errno));
90 if (conf.foreground)
91 fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
92 else {
93 if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
94 fatal("asprintf: %s", strerror(errno));
95 syslog(priority | LOG_DAEMON, "%s", s);
96 free(s);
97 }
99 free(fmted);
102 void
103 log_err(struct client *c, const char *fmt, ...)
105 va_list ap;
107 va_start(ap, fmt);
108 do_log(LOG_ERR, c, fmt, ap);
109 va_end(ap);
112 void
113 log_warn(struct client *c, const char *fmt, ...)
115 va_list ap;
117 va_start(ap, fmt);
118 do_log(LOG_WARNING, c, fmt, ap);
119 va_end(ap);
122 void
123 log_notice(struct client *c, const char *fmt, ...)
125 va_list ap;
127 va_start(ap, fmt);
128 do_log(LOG_NOTICE, c, fmt, ap);
129 va_end(ap);
132 void
133 log_info(struct client *c, const char *fmt, ...)
135 va_list ap;
137 va_start(ap, fmt);
138 do_log(LOG_INFO, c, fmt, ap);
139 va_end(ap);
142 void
143 log_debug(struct client *c, const char *fmt, ...)
145 va_list ap;
147 va_start(ap, fmt);
148 do_log(LOG_DEBUG, c, fmt, ap);
149 va_end(ap);
152 /* strchr, but with a bound */
153 static char *
154 gmid_strnchr(char *s, int c, size_t len)
156 size_t i;
158 for (i = 0; i < len; ++i)
159 if (s[i] == c)
160 return &s[i];
161 return NULL;
164 void
165 log_request(struct client *c, char *meta, size_t l)
167 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
168 char *t;
169 size_t len;
170 int ec;
172 len = sizeof(c->addr);
173 ec = getnameinfo((struct sockaddr*)&c->addr, len,
174 hbuf, sizeof(hbuf),
175 sbuf, sizeof(sbuf),
176 NI_NUMERICHOST | NI_NUMERICSERV);
177 if (ec != 0)
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));
189 else
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));
198 } else {
199 strlcpy(b, c->req, sizeof(b));
202 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
203 t = meta + len;
205 if (conf.foreground)
206 fprintf(stderr, "%s:%s GET %s %.*s\n", hbuf, sbuf, b,
207 (int)(t - meta), meta);
208 else
209 syslog(LOG_INFO | LOG_DAEMON, "%s:%s GET %s %.*s",
210 hbuf, sbuf, b, (int)(t - meta), meta);