2013-06-24 09:36:40 +02:00
|
|
|
/*
|
2018-01-20 22:51:06 +01:00
|
|
|
* Copyright (c) 2013-2018 Joris Vink <joris@coders.se>
|
2013-06-24 09:36:40 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <poll.h>
|
2016-01-07 09:24:45 +01:00
|
|
|
#include <time.h>
|
2013-06-24 09:36:40 +02:00
|
|
|
|
|
|
|
#include "kore.h"
|
|
|
|
#include "http.h"
|
|
|
|
|
|
|
|
struct kore_log_packet {
|
|
|
|
u_int8_t method;
|
|
|
|
int status;
|
2018-06-28 14:25:32 +02:00
|
|
|
size_t length;
|
2018-10-07 20:49:16 +02:00
|
|
|
int family;
|
2013-07-27 20:56:15 +02:00
|
|
|
u_int8_t addr[sizeof(struct in6_addr)];
|
2013-06-24 09:36:40 +02:00
|
|
|
char host[KORE_DOMAINNAME_LEN];
|
|
|
|
char path[HTTP_URI_LEN];
|
|
|
|
char agent[HTTP_USERAGENT_LEN];
|
2018-06-29 22:37:48 +02:00
|
|
|
char referer[HTTP_REFERER_LEN];
|
2016-01-07 09:20:09 +01:00
|
|
|
#if !defined(KORE_NO_TLS)
|
2014-03-05 11:38:47 +01:00
|
|
|
char cn[X509_CN_LENGTH];
|
2016-01-07 09:20:09 +01:00
|
|
|
#endif
|
2013-06-24 09:36:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_accesslog_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_accesslog_worker_init(void)
|
|
|
|
{
|
2013-06-24 11:32:45 +02:00
|
|
|
kore_domain_closelogs();
|
2013-06-24 09:36:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-06-23 18:17:14 +02:00
|
|
|
kore_accesslog_write(const void *data, u_int32_t len)
|
2013-06-24 09:36:40 +02:00
|
|
|
{
|
2015-06-23 18:17:14 +02:00
|
|
|
int l;
|
2013-06-24 09:36:40 +02:00
|
|
|
time_t now;
|
2018-06-28 14:25:32 +02:00
|
|
|
struct tm *tm;
|
2015-06-23 18:17:14 +02:00
|
|
|
ssize_t sent;
|
2013-06-24 11:32:45 +02:00
|
|
|
struct kore_domain *dom;
|
2013-06-24 09:36:40 +02:00
|
|
|
struct kore_log_packet logpacket;
|
2018-06-28 14:25:32 +02:00
|
|
|
char *method, *buf, *cn;
|
|
|
|
char addr[INET6_ADDRSTRLEN], tbuf[128];
|
2013-06-24 09:36:40 +02:00
|
|
|
|
2015-06-23 18:17:14 +02:00
|
|
|
if (len != sizeof(struct kore_log_packet))
|
2013-06-24 09:36:40 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2015-06-23 18:17:14 +02:00
|
|
|
(void)memcpy(&logpacket, data, sizeof(logpacket));
|
2013-06-24 09:36:40 +02:00
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if ((dom = kore_domain_lookup(logpacket.host)) == NULL) {
|
2013-06-24 09:36:40 +02:00
|
|
|
kore_log(LOG_WARNING,
|
|
|
|
"got accesslog packet for unknown domain: %s",
|
|
|
|
logpacket.host);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2014-03-05 11:38:47 +01:00
|
|
|
switch (logpacket.method) {
|
|
|
|
case HTTP_METHOD_GET:
|
2013-06-24 09:36:40 +02:00
|
|
|
method = "GET";
|
2014-03-05 11:38:47 +01:00
|
|
|
break;
|
|
|
|
case HTTP_METHOD_POST:
|
2013-06-24 09:36:40 +02:00
|
|
|
method = "POST";
|
2014-03-05 11:38:47 +01:00
|
|
|
break;
|
2014-10-08 11:03:14 +02:00
|
|
|
case HTTP_METHOD_PUT:
|
|
|
|
method = "PUT";
|
|
|
|
break;
|
|
|
|
case HTTP_METHOD_DELETE:
|
|
|
|
method = "DELETE";
|
|
|
|
break;
|
|
|
|
case HTTP_METHOD_HEAD:
|
|
|
|
method = "HEAD";
|
|
|
|
break;
|
2018-01-02 22:27:59 +01:00
|
|
|
case HTTP_METHOD_PATCH:
|
|
|
|
method = "PATCH";
|
|
|
|
break;
|
2014-03-05 11:38:47 +01:00
|
|
|
default:
|
|
|
|
method = "UNKNOWN";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-06-28 14:25:32 +02:00
|
|
|
cn = "-";
|
2016-01-07 09:20:09 +01:00
|
|
|
#if !defined(KORE_NO_TLS)
|
2014-03-05 11:38:47 +01:00
|
|
|
if (logpacket.cn[0] != '\0')
|
|
|
|
cn = logpacket.cn;
|
2016-01-07 09:20:09 +01:00
|
|
|
#endif
|
2013-06-24 09:36:40 +02:00
|
|
|
|
2018-10-07 20:49:16 +02:00
|
|
|
if (logpacket.family != AF_UNIX) {
|
|
|
|
if (inet_ntop(logpacket.family, &(logpacket.addr),
|
|
|
|
addr, sizeof(addr)) == NULL)
|
|
|
|
(void)kore_strlcpy(addr, "-", sizeof(addr));
|
|
|
|
} else {
|
|
|
|
(void)kore_strlcpy(addr, "unix-socket", sizeof(addr));
|
|
|
|
}
|
2013-07-27 20:56:15 +02:00
|
|
|
|
2013-06-24 09:36:40 +02:00
|
|
|
time(&now);
|
2018-06-28 14:52:49 +02:00
|
|
|
tm = localtime(&now);
|
2018-06-28 14:25:32 +02:00
|
|
|
(void)strftime(tbuf, sizeof(tbuf), "%d/%b/%Y:%H:%M:%S %z", tm);
|
|
|
|
|
|
|
|
l = asprintf(&buf,
|
2018-06-29 22:37:48 +02:00
|
|
|
"%s - %s [%s] \"%s %s HTTP/1.1\" %d %zu \"%s\" \"%s\"\n",
|
2018-06-28 14:25:32 +02:00
|
|
|
addr, cn, tbuf, method, logpacket.path, logpacket.status,
|
2018-06-29 22:37:48 +02:00
|
|
|
logpacket.length, logpacket.referer, logpacket.agent);
|
2014-03-05 11:38:47 +01:00
|
|
|
if (l == -1) {
|
|
|
|
kore_log(LOG_WARNING,
|
2018-06-28 14:53:43 +02:00
|
|
|
"kore_accesslog_write(): asprintf(): %s", errno_s);
|
2014-03-05 11:38:47 +01:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:17:14 +02:00
|
|
|
sent = write(dom->accesslog, buf, l);
|
|
|
|
if (sent == -1) {
|
2014-04-09 14:35:14 +02:00
|
|
|
free(buf);
|
2013-06-24 09:36:40 +02:00
|
|
|
kore_log(LOG_WARNING,
|
2015-06-23 18:17:14 +02:00
|
|
|
"kore_accesslog_write(): write(): %s", errno_s);
|
2013-06-24 09:36:40 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:17:14 +02:00
|
|
|
if (sent != l)
|
2018-06-28 14:25:32 +02:00
|
|
|
kore_log(LOG_WARNING, "kore_accesslog_write(): short write");
|
2013-06-24 09:36:40 +02:00
|
|
|
|
2014-04-09 14:35:14 +02:00
|
|
|
free(buf);
|
2013-06-24 09:36:40 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_accesslog(struct http_request *req)
|
|
|
|
{
|
|
|
|
struct kore_log_packet logpacket;
|
|
|
|
|
2018-10-07 20:49:16 +02:00
|
|
|
logpacket.family = req->owner->family;
|
|
|
|
|
|
|
|
switch (logpacket.family) {
|
|
|
|
case AF_INET:
|
2013-07-27 20:56:15 +02:00
|
|
|
memcpy(logpacket.addr,
|
|
|
|
&(req->owner->addr.ipv4.sin_addr),
|
|
|
|
sizeof(req->owner->addr.ipv4.sin_addr));
|
2018-10-07 20:49:16 +02:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2013-07-27 20:56:15 +02:00
|
|
|
memcpy(logpacket.addr,
|
|
|
|
&(req->owner->addr.ipv6.sin6_addr),
|
|
|
|
sizeof(req->owner->addr.ipv6.sin6_addr));
|
2018-10-07 20:49:16 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-07-27 20:56:15 +02:00
|
|
|
}
|
|
|
|
|
2013-06-24 09:36:40 +02:00
|
|
|
logpacket.status = req->status;
|
|
|
|
logpacket.method = req->method;
|
2018-06-28 14:25:32 +02:00
|
|
|
logpacket.length = req->content_length;
|
2016-07-04 11:41:37 +02:00
|
|
|
|
|
|
|
if (kore_strlcpy(logpacket.host,
|
|
|
|
req->host, sizeof(logpacket.host)) >= sizeof(logpacket.host))
|
|
|
|
kore_log(LOG_NOTICE, "kore_accesslog: host truncated");
|
|
|
|
|
|
|
|
if (kore_strlcpy(logpacket.path,
|
|
|
|
req->path, sizeof(logpacket.path)) >= sizeof(logpacket.path))
|
|
|
|
kore_log(LOG_NOTICE, "kore_accesslog: path truncated");
|
2013-07-05 22:03:05 +02:00
|
|
|
|
|
|
|
if (req->agent != NULL) {
|
2016-07-04 11:41:37 +02:00
|
|
|
if (kore_strlcpy(logpacket.agent, req->agent,
|
|
|
|
sizeof(logpacket.agent)) >= sizeof(logpacket.agent))
|
|
|
|
kore_log(LOG_NOTICE, "kore_accesslog: agent truncated");
|
2013-07-05 22:03:05 +02:00
|
|
|
} else {
|
2018-06-28 14:25:32 +02:00
|
|
|
(void)kore_strlcpy(logpacket.agent, "-",
|
2013-07-05 22:03:05 +02:00
|
|
|
sizeof(logpacket.agent));
|
|
|
|
}
|
2013-06-24 09:36:40 +02:00
|
|
|
|
2018-06-29 22:37:48 +02:00
|
|
|
if (req->referer != NULL) {
|
|
|
|
if (kore_strlcpy(logpacket.referer, req->referer,
|
|
|
|
sizeof(logpacket.referer)) >= sizeof(logpacket.referer)) {
|
|
|
|
kore_log(LOG_NOTICE,
|
|
|
|
"kore_accesslog: referer truncated");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(void)kore_strlcpy(logpacket.referer, "-",
|
|
|
|
sizeof(logpacket.referer));
|
|
|
|
}
|
|
|
|
|
2015-05-25 15:42:34 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
2016-01-07 09:20:09 +01:00
|
|
|
memset(logpacket.cn, '\0', sizeof(logpacket.cn));
|
2014-03-05 11:38:47 +01:00
|
|
|
if (req->owner->cert != NULL) {
|
|
|
|
if (X509_GET_CN(req->owner->cert,
|
|
|
|
logpacket.cn, sizeof(logpacket.cn)) == -1) {
|
|
|
|
kore_log(LOG_WARNING, "client cert without a CN?");
|
|
|
|
}
|
|
|
|
}
|
2014-08-01 10:22:32 +02:00
|
|
|
#endif
|
2014-03-05 11:38:47 +01:00
|
|
|
|
2015-07-06 21:08:36 +02:00
|
|
|
kore_msg_send(KORE_MSG_PARENT,
|
|
|
|
KORE_MSG_ACCESSLOG, &logpacket, sizeof(logpacket));
|
2013-06-24 09:36:40 +02:00
|
|
|
}
|