002
2021-02-07
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
004
2021-02-07
op
* Permission to use, copy, modify, and distribute this software for any
005
2021-02-07
op
* purpose with or without fee is hereby granted, provided that the above
006
2021-02-07
op
* copyright notice and this permission notice appear in all copies.
008
2021-02-07
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2021-02-07
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2021-02-07
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2021-02-07
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2021-02-07
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2021-02-07
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2021-02-07
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2021-02-12
op
#include "gmid.h"
019
2021-02-23
op
#include <sys/types.h>
020
2021-02-23
op
#include <sys/uio.h>
022
2021-02-07
op
#include <errno.h>
023
2021-02-23
op
#include <event.h>
024
2021-02-23
op
#include <imsg.h>
025
2021-02-07
op
#include <netdb.h>
026
2021-04-28
op
#include <poll.h>
027
2021-02-07
op
#include <stdarg.h>
028
2021-02-07
op
#include <stdio.h>
029
2021-02-07
op
#include <string.h>
030
2021-02-07
op
#include <syslog.h>
031
2021-04-14
op
#include <time.h>
033
2021-03-19
op
static struct event imsgev;
035
2021-06-15
op
static FILE *log;
037
2021-03-19
op
static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
038
2021-03-19
op
static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
039
2021-06-15
op
static void handle_imsg_log_type(struct imsgbuf*, struct imsg*, size_t);
040
2021-03-19
op
static void handle_dispatch_imsg(int, short, void*);
042
2021-03-19
op
static imsg_handlerfn *handlers[] = {
043
2021-03-19
op
[IMSG_QUIT] = handle_imsg_quit,
044
2021-03-19
op
[IMSG_LOG] = handle_imsg_log,
045
2021-07-19
op
[IMSG_LOG_REQUEST] = handle_imsg_log,
046
2021-06-15
op
[IMSG_LOG_TYPE] = handle_imsg_log_type,
049
2021-04-14
op
static inline void
050
2021-06-15
op
print_date(FILE *f)
052
2021-04-14
op
struct tm tminfo;
053
2021-04-14
op
time_t t;
054
2021-04-14
op
char buf[20];
056
2021-04-14
op
time(&t);
057
2021-04-14
op
strftime(buf, sizeof(buf), "%F %T",
058
2021-04-14
op
localtime_r(&t, &tminfo));
059
2021-06-15
op
fprintf(f, "[%s] ", buf);
062
2021-02-07
op
static inline int
063
2021-02-07
op
should_log(int priority)
065
2021-02-07
op
switch (priority) {
066
2021-02-07
op
case LOG_ERR:
067
2021-02-07
op
return 1;
068
2021-02-07
op
case LOG_WARNING:
069
2021-02-07
op
return 1;
070
2021-02-07
op
case LOG_NOTICE:
071
2021-02-07
op
return conf.verbose >= 1;
072
2021-02-07
op
case LOG_INFO:
073
2021-02-07
op
return conf.verbose >= 2;
074
2021-02-07
op
case LOG_DEBUG:
075
2021-02-07
op
return conf.verbose >= 3;
077
2021-02-07
op
return 0;
081
2021-02-23
op
static inline void
082
2021-07-19
op
send_log(int type, int priority, const char *msg, size_t len)
084
2021-07-19
op
imsg_compose(&logibuf, type, priority, 0, -1, msg, len);
085
2021-03-19
op
imsg_flush(&logibuf);
089
2021-04-28
op
fatal(const char *fmt, ...)
091
2021-04-28
op
struct pollfd pfd;
092
2021-04-28
op
va_list ap;
094
2021-04-28
op
char *fmted;
096
2021-04-28
op
va_start(ap, fmt);
097
2021-04-28
op
if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
098
2021-08-23
op
send_log(IMSG_LOG, LOG_CRIT, fmted, r+1);
099
2021-04-28
op
free(fmted);
101
2021-04-28
op
/* wait for the logger process to shut down */
102
2021-04-28
op
pfd.fd = logibuf.fd;
103
2021-04-28
op
pfd.events = POLLIN;
104
2021-04-28
op
poll(&pfd, 1, 1000);
106
2021-04-28
op
va_end(ap);
110
2021-02-23
op
static inline void
111
2021-02-23
op
vlog(int priority, struct client *c,
112
2021-02-07
op
const char *fmt, va_list ap)
114
2021-02-07
op
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
115
2021-02-07
op
char *fmted, *s;
116
2021-02-07
op
size_t len;
119
2021-02-07
op
if (!should_log(priority))
122
2021-02-23
op
if (c != NULL) {
123
2021-02-07
op
len = sizeof(c->addr);
124
2021-02-07
op
ec = getnameinfo((struct sockaddr*)&c->addr, len,
125
2021-02-07
op
hbuf, sizeof(hbuf),
126
2021-02-07
op
sbuf, sizeof(sbuf),
127
2021-02-07
op
NI_NUMERICHOST | NI_NUMERICSERV);
128
2021-02-07
op
if (ec != 0)
129
2021-10-02
op
fatal("getnameinfo: %s: %s",
130
2021-10-02
op
gai_strerror(ec), strerror(errno));
133
2021-02-07
op
if (vasprintf(&fmted, fmt, ap) == -1)
134
2021-02-07
op
fatal("vasprintf: %s", strerror(errno));
136
2021-07-06
op
if (c == NULL)
137
2021-02-23
op
ec = asprintf(&s, "internal: %s", fmted);
139
2021-02-23
op
ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
141
2021-02-23
op
if (ec < 0)
142
2021-02-23
op
fatal("asprintf: %s", strerror(errno));
144
2021-08-23
op
send_log(IMSG_LOG, priority, s, ec+1);
146
2021-02-07
op
free(fmted);
151
2021-02-07
op
log_err(struct client *c, const char *fmt, ...)
153
2021-02-07
op
va_list ap;
155
2021-02-07
op
va_start(ap, fmt);
156
2021-02-23
op
vlog(LOG_ERR, c, fmt, ap);
157
2021-02-07
op
va_end(ap);
161
2021-02-07
op
log_warn(struct client *c, const char *fmt, ...)
163
2021-02-07
op
va_list ap;
165
2021-02-07
op
va_start(ap, fmt);
166
2021-02-23
op
vlog(LOG_WARNING, c, fmt, ap);
167
2021-02-07
op
va_end(ap);
171
2021-02-07
op
log_notice(struct client *c, const char *fmt, ...)
173
2021-02-07
op
va_list ap;
175
2021-02-07
op
va_start(ap, fmt);
176
2021-02-23
op
vlog(LOG_NOTICE, c, fmt, ap);
177
2021-02-07
op
va_end(ap);
181
2021-02-07
op
log_info(struct client *c, const char *fmt, ...)
183
2021-02-07
op
va_list ap;
185
2021-02-07
op
va_start(ap, fmt);
186
2021-02-23
op
vlog(LOG_INFO, c, fmt, ap);
187
2021-02-07
op
va_end(ap);
191
2021-02-07
op
log_debug(struct client *c, const char *fmt, ...)
193
2021-02-07
op
va_list ap;
195
2021-02-07
op
va_start(ap, fmt);
196
2021-02-23
op
vlog(LOG_DEBUG, c, fmt, ap);
197
2021-02-07
op
va_end(ap);
200
2021-02-07
op
/* strchr, but with a bound */
201
2021-02-07
op
static char *
202
2021-02-07
op
gmid_strnchr(char *s, int c, size_t len)
204
2021-02-07
op
size_t i;
206
2021-02-07
op
for (i = 0; i < len; ++i)
207
2021-02-07
op
if (s[i] == c)
208
2021-02-07
op
return &s[i];
209
2021-02-07
op
return NULL;
213
2021-02-07
op
log_request(struct client *c, char *meta, size_t l)
215
2021-02-07
op
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
216
2021-10-15
op
char *fmted;
217
2021-10-15
op
const char *t;
218
2021-02-07
op
size_t len;
221
2021-02-07
op
len = sizeof(c->addr);
222
2021-02-07
op
ec = getnameinfo((struct sockaddr*)&c->addr, len,
223
2021-02-07
op
hbuf, sizeof(hbuf),
224
2021-02-07
op
sbuf, sizeof(sbuf),
225
2021-02-07
op
NI_NUMERICHOST | NI_NUMERICSERV);
226
2021-02-07
op
if (ec != 0)
227
2021-02-07
op
fatal("getnameinfo: %s", gai_strerror(ec));
229
2021-02-07
op
if (c->iri.schema != NULL) {
230
2021-02-07
op
/* serialize the IRI */
231
2021-02-07
op
strlcpy(b, c->iri.schema, sizeof(b));
232
2021-02-07
op
strlcat(b, "://", sizeof(b));
234
2021-02-07
op
/* log the decoded host name, but if it was invalid
235
2021-02-07
op
* use the raw one. */
236
2021-02-07
op
if (*c->domain != '\0')
237
2021-02-07
op
strlcat(b, c->domain, sizeof(b));
239
2021-02-07
op
strlcat(b, c->iri.host, sizeof(b));
241
2021-10-24
op
if (*c->iri.path != '/')
242
2021-10-24
op
strlcat(b, "/", sizeof(b));
243
2021-02-07
op
strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
244
2021-02-07
op
if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
245
2021-02-07
op
strlcat(b, "?", sizeof(b));
246
2021-02-07
op
strlcat(b, c->iri.query, sizeof(b));
249
2021-10-15
op
if ((t = c->req) == NULL)
251
2021-10-15
op
strlcpy(b, t, sizeof(b));
254
2021-02-07
op
if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
255
2021-02-07
op
t = meta + len;
257
2021-02-23
op
ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
258
2021-02-23
op
(int)(t-meta), meta);
259
2021-02-23
op
if (ec < 0)
260
2021-02-23
op
err(1, "asprintf");
261
2021-08-23
op
send_log(IMSG_LOG_REQUEST, LOG_NOTICE, fmted, ec+1);
262
2021-02-23
op
free(fmted);
267
2021-02-23
op
static void
268
2021-07-19
op
do_log(int type, int priority, const char *msg)
270
2021-06-15
op
int quit = 0;
272
2021-06-15
op
if (priority == LOG_CRIT) {
273
2021-06-15
op
quit = 1;
274
2021-06-15
op
priority = LOG_ERR;
277
2021-06-15
op
if (log != NULL) {
278
2021-07-19
op
if (type != IMSG_LOG_REQUEST)
279
2021-07-19
op
print_date(log);
280
2021-06-15
op
fprintf(log, "%s\n", msg);
282
2021-06-15
op
syslog(LOG_DAEMON | priority, "%s", msg);
284
2021-06-15
op
if (quit)
288
2021-06-15
op
static void
289
2021-03-19
op
handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
291
2021-03-19
op
event_loopbreak();
294
2021-03-19
op
static void
295
2021-03-19
op
handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
297
2021-06-16
op
int priority;
298
2021-04-28
op
char *msg;
300
2021-03-19
op
msg = imsg->data;
301
2021-03-19
op
msg[datalen-1] = '\0';
302
2021-04-28
op
priority = imsg->hdr.peerid;
303
2021-07-19
op
do_log(imsg->hdr.type, priority, msg);
306
2021-06-15
op
static void
307
2021-06-15
op
handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
309
2021-07-07
op
if (log != NULL && log != stderr) {
310
2021-06-15
op
fflush(log);
311
2021-06-15
op
fclose(log);
313
2021-07-07
op
log = NULL;
315
2021-06-15
op
if (imsg->fd != -1) {
316
2021-06-15
op
if ((log = fdopen(imsg->fd, "a")) == NULL) {
317
2021-06-15
op
syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s",
318
2021-06-15
op
strerror(errno));
324
2021-03-19
op
static void
325
2021-03-19
op
handle_dispatch_imsg(int fd, short ev, void *d)
327
2021-03-19
op
struct imsgbuf *ibuf = d;
328
2021-03-19
op
dispatch_imsg(ibuf, handlers, sizeof(handlers));
332
2021-02-23
op
logger_main(int fd, struct imsgbuf *ibuf)
334
2021-07-07
op
log = stderr;
336
2021-09-17
op
openlog(getprogname(), LOG_NDELAY, LOG_DAEMON);
339
2021-02-23
op
event_init();
341
2021-03-19
op
event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
342
2021-03-19
op
event_add(&imsgev, NULL);
344
2021-03-20
op
sandbox_logger_process();
346
2021-02-23
op
event_dispatch();
348
2021-09-17
op
closelog();
350
2021-02-23
op
return 0;