2021-02-07 17:15:51 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
|
|
|
|
|
*
|
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-02-12 13:47:20 +01:00
|
|
|
|
#include "gmid.h"
|
|
|
|
|
|
2021-02-14 13:20:34 +01:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
2021-02-07 17:15:51 +01:00
|
|
|
|
#include <errno.h>
|
2021-02-14 13:20:34 +01:00
|
|
|
|
#include <event.h>
|
|
|
|
|
#include <imsg.h>
|
2021-02-07 17:15:51 +01:00
|
|
|
|
#include <netdb.h>
|
2021-04-28 14:43:17 +02:00
|
|
|
|
#include <poll.h>
|
2021-02-07 17:15:51 +01:00
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <syslog.h>
|
2021-04-14 16:52:47 +02:00
|
|
|
|
#include <time.h>
|
2021-02-07 17:15:51 +01:00
|
|
|
|
|
2023-06-05 19:07:52 +02:00
|
|
|
|
#include "log.h"
|
|
|
|
|
|
2021-03-19 20:21:29 +01:00
|
|
|
|
static struct event imsgev;
|
2021-02-14 13:20:34 +01:00
|
|
|
|
|
2021-06-15 10:06:10 +02:00
|
|
|
|
static FILE *log;
|
|
|
|
|
|
2021-03-19 20:21:29 +01:00
|
|
|
|
static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
|
|
|
|
|
static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
|
2021-06-15 10:06:10 +02:00
|
|
|
|
static void handle_imsg_log_type(struct imsgbuf*, struct imsg*, size_t);
|
2021-03-19 20:21:29 +01:00
|
|
|
|
static void handle_dispatch_imsg(int, short, void*);
|
|
|
|
|
|
|
|
|
|
static imsg_handlerfn *handlers[] = {
|
|
|
|
|
[IMSG_QUIT] = handle_imsg_quit,
|
|
|
|
|
[IMSG_LOG] = handle_imsg_log,
|
2021-07-19 09:31:40 +02:00
|
|
|
|
[IMSG_LOG_REQUEST] = handle_imsg_log,
|
2021-06-15 10:06:10 +02:00
|
|
|
|
[IMSG_LOG_TYPE] = handle_imsg_log_type,
|
2021-03-19 20:21:29 +01:00
|
|
|
|
};
|
2021-02-14 13:20:34 +01:00
|
|
|
|
|
2021-04-14 16:52:47 +02:00
|
|
|
|
static inline void
|
2021-06-15 10:06:10 +02:00
|
|
|
|
print_date(FILE *f)
|
2021-04-14 16:52:47 +02:00
|
|
|
|
{
|
|
|
|
|
struct tm tminfo;
|
|
|
|
|
time_t t;
|
|
|
|
|
char buf[20];
|
|
|
|
|
|
|
|
|
|
time(&t);
|
|
|
|
|
strftime(buf, sizeof(buf), "%F %T",
|
|
|
|
|
localtime_r(&t, &tminfo));
|
2021-06-15 10:06:10 +02:00
|
|
|
|
fprintf(f, "[%s] ", buf);
|
2021-04-14 16:52:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 17:15:51 +01:00
|
|
|
|
static inline int
|
|
|
|
|
should_log(int priority)
|
|
|
|
|
{
|
|
|
|
|
switch (priority) {
|
|
|
|
|
case LOG_ERR:
|
|
|
|
|
return 1;
|
|
|
|
|
case LOG_WARNING:
|
|
|
|
|
return 1;
|
|
|
|
|
case LOG_NOTICE:
|
|
|
|
|
return conf.verbose >= 1;
|
|
|
|
|
case LOG_INFO:
|
|
|
|
|
return conf.verbose >= 2;
|
|
|
|
|
case LOG_DEBUG:
|
|
|
|
|
return conf.verbose >= 3;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 13:20:34 +01:00
|
|
|
|
static inline void
|
2021-07-19 09:23:38 +02:00
|
|
|
|
send_log(int type, int priority, const char *msg, size_t len)
|
2021-02-14 13:20:34 +01:00
|
|
|
|
{
|
2021-07-19 09:23:38 +02:00
|
|
|
|
imsg_compose(&logibuf, type, priority, 0, -1, msg, len);
|
2021-03-19 20:21:29 +01:00
|
|
|
|
imsg_flush(&logibuf);
|
2021-02-14 13:20:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 19:07:52 +02:00
|
|
|
|
static __dead void
|
|
|
|
|
fatal_impl(int use_err, const char *fmt, va_list ap)
|
2021-04-28 14:43:17 +02:00
|
|
|
|
{
|
2021-04-28 14:43:17 +02:00
|
|
|
|
struct pollfd pfd;
|
2023-06-05 19:07:52 +02:00
|
|
|
|
char *str, *tmp;
|
|
|
|
|
int r, t, err;
|
|
|
|
|
|
|
|
|
|
err = errno;
|
|
|
|
|
|
|
|
|
|
if ((r = vasprintf(&str, fmt, ap)) != -1) {
|
|
|
|
|
if (use_err &&
|
|
|
|
|
(t = asprintf(&tmp, "%s: %s", str, strerror(err))) !=
|
|
|
|
|
-1) {
|
|
|
|
|
free(str);
|
|
|
|
|
str = tmp;
|
|
|
|
|
r = t;
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
str = NULL, r = 0;
|
|
|
|
|
|
|
|
|
|
send_log(IMSG_LOG, LOG_CRIT, str, r);
|
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
|
|
/* wait for the logger process to shut down */
|
|
|
|
|
memset(&pfd, 0, sizeof(pfd));
|
|
|
|
|
pfd.fd = logibuf.fd;
|
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
|
poll(&pfd, 1, 1000);
|
|
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void __dead
|
|
|
|
|
fatal(const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
2021-04-28 14:43:17 +02:00
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2023-06-05 19:07:52 +02:00
|
|
|
|
fatal_impl(1, fmt, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void __dead
|
|
|
|
|
fatalx(const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
fatal_impl(0, fmt, ap);
|
2021-04-28 14:43:17 +02:00
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 13:20:34 +01:00
|
|
|
|
static inline void
|
|
|
|
|
vlog(int priority, struct client *c,
|
2021-02-07 17:15:51 +01:00
|
|
|
|
const char *fmt, va_list ap)
|
|
|
|
|
{
|
|
|
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
|
|
|
|
char *fmted, *s;
|
|
|
|
|
size_t len;
|
|
|
|
|
int ec;
|
|
|
|
|
|
|
|
|
|
if (!should_log(priority))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-02-14 13:20:34 +01:00
|
|
|
|
if (c != NULL) {
|
2021-02-07 17:15:51 +01:00
|
|
|
|
len = sizeof(c->addr);
|
|
|
|
|
ec = getnameinfo((struct sockaddr*)&c->addr, len,
|
|
|
|
|
hbuf, sizeof(hbuf),
|
|
|
|
|
sbuf, sizeof(sbuf),
|
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
|
if (ec != 0)
|
2023-06-05 19:07:52 +02:00
|
|
|
|
fatal("getnameinfo: %s", gai_strerror(ec));
|
2021-02-07 17:15:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vasprintf(&fmted, fmt, ap) == -1)
|
|
|
|
|
fatal("vasprintf: %s", strerror(errno));
|
|
|
|
|
|
2021-07-06 12:56:13 +02:00
|
|
|
|
if (c == NULL)
|
2021-02-14 13:20:34 +01:00
|
|
|
|
ec = asprintf(&s, "internal: %s", fmted);
|
|
|
|
|
else
|
|
|
|
|
ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
|
|
|
|
|
|
|
|
|
|
if (ec < 0)
|
|
|
|
|
fatal("asprintf: %s", strerror(errno));
|
|
|
|
|
|
2021-08-24 00:36:01 +02:00
|
|
|
|
send_log(IMSG_LOG, priority, s, ec+1);
|
2021-02-07 17:15:51 +01:00
|
|
|
|
|
|
|
|
|
free(fmted);
|
2021-02-14 13:20:34 +01:00
|
|
|
|
free(s);
|
2021-02-07 17:15:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
log_err(struct client *c, const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2021-02-14 13:20:34 +01:00
|
|
|
|
vlog(LOG_ERR, c, fmt, ap);
|
2021-02-07 17:15:51 +01:00
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
log_warn(struct client *c, const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2021-02-14 13:20:34 +01:00
|
|
|
|
vlog(LOG_WARNING, c, fmt, ap);
|
2021-02-07 17:15:51 +01:00
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
log_notice(struct client *c, const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2021-02-14 13:20:34 +01:00
|
|
|
|
vlog(LOG_NOTICE, c, fmt, ap);
|
2021-02-07 17:15:51 +01:00
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
log_info(struct client *c, const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2021-02-14 13:20:34 +01:00
|
|
|
|
vlog(LOG_INFO, c, fmt, ap);
|
2021-02-07 17:15:51 +01:00
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
log_debug(struct client *c, const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2021-02-14 13:20:34 +01:00
|
|
|
|
vlog(LOG_DEBUG, c, fmt, ap);
|
2021-02-07 17:15:51 +01:00
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* strchr, but with a bound */
|
|
|
|
|
static char *
|
|
|
|
|
gmid_strnchr(char *s, int c, size_t len)
|
|
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
|
if (s[i] == c)
|
|
|
|
|
return &s[i];
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
log_request(struct client *c, char *meta, size_t l)
|
|
|
|
|
{
|
|
|
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
|
2021-10-15 09:51:15 +02:00
|
|
|
|
char *fmted;
|
|
|
|
|
const char *t;
|
2021-02-07 17:15:51 +01:00
|
|
|
|
size_t len;
|
|
|
|
|
int ec;
|
|
|
|
|
|
|
|
|
|
len = sizeof(c->addr);
|
|
|
|
|
ec = getnameinfo((struct sockaddr*)&c->addr, len,
|
|
|
|
|
hbuf, sizeof(hbuf),
|
|
|
|
|
sbuf, sizeof(sbuf),
|
|
|
|
|
NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
|
if (ec != 0)
|
2023-06-05 19:07:52 +02:00
|
|
|
|
fatalx("getnameinfo: %s", gai_strerror(ec));
|
2021-02-07 17:15:51 +01:00
|
|
|
|
|
|
|
|
|
if (c->iri.schema != NULL) {
|
|
|
|
|
/* serialize the IRI */
|
|
|
|
|
strlcpy(b, c->iri.schema, sizeof(b));
|
|
|
|
|
strlcat(b, "://", sizeof(b));
|
|
|
|
|
|
|
|
|
|
/* log the decoded host name, but if it was invalid
|
|
|
|
|
* use the raw one. */
|
|
|
|
|
if (*c->domain != '\0')
|
|
|
|
|
strlcat(b, c->domain, sizeof(b));
|
|
|
|
|
else
|
|
|
|
|
strlcat(b, c->iri.host, sizeof(b));
|
|
|
|
|
|
2021-10-24 18:34:30 +02:00
|
|
|
|
if (*c->iri.path != '/')
|
|
|
|
|
strlcat(b, "/", sizeof(b));
|
2021-02-07 17:15:51 +01:00
|
|
|
|
strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
|
|
|
|
|
if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
|
|
|
|
|
strlcat(b, "?", sizeof(b));
|
|
|
|
|
strlcat(b, c->iri.query, sizeof(b));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-10-15 09:51:15 +02:00
|
|
|
|
if ((t = c->req) == NULL)
|
|
|
|
|
t = "";
|
|
|
|
|
strlcpy(b, t, sizeof(b));
|
2021-02-07 17:15:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
|
|
|
|
|
t = meta + len;
|
|
|
|
|
|
2021-02-14 13:20:34 +01:00
|
|
|
|
ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
|
|
|
|
|
(int)(t-meta), meta);
|
|
|
|
|
if (ec < 0)
|
|
|
|
|
err(1, "asprintf");
|
2021-08-24 00:36:01 +02:00
|
|
|
|
send_log(IMSG_LOG_REQUEST, LOG_NOTICE, fmted, ec+1);
|
2021-02-14 13:20:34 +01:00
|
|
|
|
free(fmted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-06-15 10:06:10 +02:00
|
|
|
|
static void
|
2021-07-19 09:31:40 +02:00
|
|
|
|
do_log(int type, int priority, const char *msg)
|
2021-06-15 10:06:10 +02:00
|
|
|
|
{
|
|
|
|
|
int quit = 0;
|
|
|
|
|
|
|
|
|
|
if (priority == LOG_CRIT) {
|
|
|
|
|
quit = 1;
|
|
|
|
|
priority = LOG_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (log != NULL) {
|
2021-07-19 09:31:40 +02:00
|
|
|
|
if (type != IMSG_LOG_REQUEST)
|
|
|
|
|
print_date(log);
|
2021-06-15 10:06:10 +02:00
|
|
|
|
fprintf(log, "%s\n", msg);
|
|
|
|
|
} else
|
|
|
|
|
syslog(LOG_DAEMON | priority, "%s", msg);
|
|
|
|
|
|
|
|
|
|
if (quit)
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 13:20:34 +01:00
|
|
|
|
static void
|
2021-03-19 20:21:29 +01:00
|
|
|
|
handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
|
2021-02-14 13:20:34 +01:00
|
|
|
|
{
|
2021-03-19 20:21:29 +01:00
|
|
|
|
event_loopbreak();
|
|
|
|
|
}
|
2021-02-14 13:20:34 +01:00
|
|
|
|
|
2021-03-19 20:21:29 +01:00
|
|
|
|
static void
|
|
|
|
|
handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
|
|
|
|
|
{
|
2021-06-16 16:38:47 +02:00
|
|
|
|
int priority;
|
2021-04-28 14:43:17 +02:00
|
|
|
|
char *msg;
|
2021-03-19 20:21:29 +01:00
|
|
|
|
|
|
|
|
|
msg = imsg->data;
|
|
|
|
|
msg[datalen-1] = '\0';
|
2021-04-28 14:43:17 +02:00
|
|
|
|
priority = imsg->hdr.peerid;
|
2021-07-19 09:31:40 +02:00
|
|
|
|
do_log(imsg->hdr.type, priority, msg);
|
2021-06-15 10:06:10 +02:00
|
|
|
|
}
|
2021-04-28 14:43:17 +02:00
|
|
|
|
|
2021-06-15 10:06:10 +02:00
|
|
|
|
static void
|
|
|
|
|
handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
|
|
|
|
|
{
|
2021-07-07 11:18:24 +02:00
|
|
|
|
if (log != NULL && log != stderr) {
|
2021-06-15 10:06:10 +02:00
|
|
|
|
fflush(log);
|
|
|
|
|
fclose(log);
|
2021-04-28 14:43:17 +02:00
|
|
|
|
}
|
2021-07-07 11:18:24 +02:00
|
|
|
|
log = NULL;
|
2021-04-28 14:43:17 +02:00
|
|
|
|
|
2021-06-15 10:06:10 +02:00
|
|
|
|
if (imsg->fd != -1) {
|
|
|
|
|
if ((log = fdopen(imsg->fd, "a")) == NULL) {
|
|
|
|
|
syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s",
|
|
|
|
|
strerror(errno));
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-19 20:21:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_dispatch_imsg(int fd, short ev, void *d)
|
|
|
|
|
{
|
|
|
|
|
struct imsgbuf *ibuf = d;
|
|
|
|
|
dispatch_imsg(ibuf, handlers, sizeof(handlers));
|
2021-02-14 13:20:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 13:05:54 +01:00
|
|
|
|
int
|
2021-02-14 13:20:34 +01:00
|
|
|
|
logger_main(int fd, struct imsgbuf *ibuf)
|
|
|
|
|
{
|
2021-07-07 11:18:24 +02:00
|
|
|
|
log = stderr;
|
|
|
|
|
|
2021-09-17 18:17:16 +02:00
|
|
|
|
openlog(getprogname(), LOG_NDELAY, LOG_DAEMON);
|
2022-07-07 20:15:06 +02:00
|
|
|
|
tzset();
|
2021-09-17 18:17:16 +02:00
|
|
|
|
|
2021-02-14 13:20:34 +01:00
|
|
|
|
event_init();
|
|
|
|
|
|
2021-03-19 20:21:29 +01:00
|
|
|
|
event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
|
|
|
|
|
event_add(&imsgev, NULL);
|
2021-02-14 13:20:34 +01:00
|
|
|
|
|
2021-03-20 09:42:08 +01:00
|
|
|
|
sandbox_logger_process();
|
2021-02-14 13:20:34 +01:00
|
|
|
|
|
|
|
|
|
event_dispatch();
|
|
|
|
|
|
2021-09-17 18:17:16 +02:00
|
|
|
|
closelog();
|
|
|
|
|
|
2021-02-14 13:20:34 +01:00
|
|
|
|
return 0;
|
|
|
|
|
}
|