commit - 96a01110ea27c41648f05e76c0685ade3f232fee
commit + b633fa4cd119f62cce08728b29b9dbf8c023b59b
blob - e023e9cd33dfd389e375901522fb1afd0a99770b
blob + 91402a9023adaa67c5b5ade28faf9e931f9bccff
--- Makefile
+++ Makefile
PROG= dnsbl
BINDIR= /usr/bin
-SRCS+= main.c smtp_proc.c
+SRCS+= main.c log.c smtp_proc.c
CFLAGS+= -g3 -O0
LDADD+= -levent
blob - d86f9d74db45f77a2894047d1fe20c24aa860015
blob + cd91a14e4a86f1149982b5afc5d591cb5dfed4a7
--- main.c
+++ main.c
#include <sys/types.h>
#include <sys/socket.h>
-#include <err.h>
#include <errno.h>
#include <event.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <syslog.h>
#include <unistd.h>
#include <asr.h>
+#include "log.h"
#include "smtp_proc.h"
struct dnsbl_session;
{
int ch;
int i;
+ int debug = 0;
- if (pledge("stdio dns", NULL) == -1)
- err(1, "pledge");
-
- while ((ch = getopt(argc, argv, "m")) != -1) {
+ while ((ch = getopt(argc, argv, "d:m")) != -1) {
switch (ch) {
+ case 'd':
+ debug = 1;
+ break;
case 'm':
markspam = 1;
break;
}
}
+ log_init(debug, LOG_MAIL);
+ if (pledge("stdio dns", NULL) == -1)
+ fatal("pledge");
+
if ((nblacklists = argc - optind) == 0)
- errx(1, "No blacklist specified");
+ fatalx("No blacklist specified");
if ((blacklists = calloc(nblacklists, sizeof(*blacklists))) == NULL)
- err(1, NULL);
+ fatal(NULL);
for (i = 0; i < nblacklists; i++)
blacklists[i] = argv[optind + i];
if (markspam)
smtp_register_filter_dataline(dnsbl_dataline);
smtp_in_register_report_disconnect(dnsbl_disconnect);
- smtp_run();
+ smtp_run(debug);
return 0;
}
int i, try;
if ((session = calloc(1, sizeof(*session))) == NULL)
- err(1, NULL);
+ fatal(NULL);
if ((session->query = calloc(nblacklists, sizeof(*(session->query))))
== NULL)
- err(1, NULL);
+ fatal(NULL);
session->reqid = reqid;
session->token = token;
session->listed = -1;
if (snprintf(query, sizeof(query), "%u.%u.%u.%u.%s",
addr[3], addr[2], addr[1], addr[0],
blacklists[i]) >= sizeof(query))
- errx(1, "Can't create query, domain too long");
+ fatalx("Can't create query, domain too long");
} else if (xaddr->af == AF_INET6) {
if (snprintf(query, sizeof(query), "%hhx.%hhx.%hhx.%hhx"
".%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx"
(u_char) (addr[1] & 0xf), (u_char) (addr[1] >> 4),
(u_char) (addr[0] & 0xf), (u_char) (addr[0] >> 4),
blacklists[i]) >= sizeof(query))
- errx(1, "Can't create query, domain too long");
+ fatalx( "Can't create query, domain too long");
} else
- errx(1, "Invalid address family received");
+ fatalx("Invalid address family received");
session->query[i].query = gethostbyname_async(query, NULL);
session->query[i].event = event_asr_run(session->query[i].query,
blob - /dev/null
blob + 7ec8ca42e18d1c57b84e27a564c23ff352c267cf (mode 644)
--- /dev/null
+++ log.c
+/* $OpenBSD: log.c,v 1.20 2017/03/21 12:06:56 bluhm Exp $ */
+
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <syslog.h>
+#include <errno.h>
+#include <time.h>
+
+static int debug;
+static int verbose;
+const char *log_procname;
+
+void log_init(int, int);
+void log_procinit(const char *);
+void log_setverbose(int);
+int log_getverbose(void);
+void log_warn(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_warnx(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_info(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_debug(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void logit(int, const char *, ...)
+ __attribute__((__format__ (printf, 2, 3)));
+void vlog(int, const char *, va_list)
+ __attribute__((__format__ (printf, 2, 0)));
+__dead void fatal(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+__dead void fatalx(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+
+void
+log_init(int n_debug, int facility)
+{
+ extern char *__progname;
+
+ debug = n_debug;
+ verbose = n_debug;
+ log_procinit(__progname);
+
+ if (!debug)
+ openlog(__progname, LOG_PID | LOG_NDELAY, facility);
+
+ tzset();
+}
+
+void
+log_procinit(const char *procname)
+{
+ if (procname != NULL)
+ log_procname = procname;
+}
+
+void
+log_setverbose(int v)
+{
+ verbose = v;
+}
+
+int
+log_getverbose(void)
+{
+ return (verbose);
+}
+
+void
+logit(int pri, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vlog(pri, fmt, ap);
+ va_end(ap);
+}
+
+void
+vlog(int pri, const char *fmt, va_list ap)
+{
+ char *nfmt;
+ int saved_errno = errno;
+
+ if (debug) {
+ /* best effort in out of mem situations */
+ if (asprintf(&nfmt, "%s\n", fmt) == -1) {
+ vfprintf(stderr, fmt, ap);
+ fprintf(stderr, "\n");
+ } else {
+ vfprintf(stderr, nfmt, ap);
+ free(nfmt);
+ }
+ fflush(stderr);
+ } else
+ vsyslog(pri, fmt, ap);
+
+ errno = saved_errno;
+}
+
+void
+log_warn(const char *emsg, ...)
+{
+ char *nfmt;
+ va_list ap;
+ int saved_errno = errno;
+
+ /* best effort to even work in out of memory situations */
+ if (emsg == NULL)
+ logit(LOG_ERR, "%s", strerror(saved_errno));
+ else {
+ va_start(ap, emsg);
+
+ if (asprintf(&nfmt, "%s: %s", emsg,
+ strerror(saved_errno)) == -1) {
+ /* we tried it... */
+ vlog(LOG_ERR, emsg, ap);
+ logit(LOG_ERR, "%s", strerror(saved_errno));
+ } else {
+ vlog(LOG_ERR, nfmt, ap);
+ free(nfmt);
+ }
+ va_end(ap);
+ }
+
+ errno = saved_errno;
+}
+
+void
+log_warnx(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ vlog(LOG_ERR, emsg, ap);
+ va_end(ap);
+}
+
+void
+log_info(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ vlog(LOG_INFO, emsg, ap);
+ va_end(ap);
+}
+
+void
+log_debug(const char *emsg, ...)
+{
+ va_list ap;
+
+ if (verbose > 1) {
+ va_start(ap, emsg);
+ vlog(LOG_DEBUG, emsg, ap);
+ va_end(ap);
+ }
+}
+
+static void
+vfatalc(int code, const char *emsg, va_list ap)
+{
+ static char s[BUFSIZ];
+ const char *sep;
+
+ if (emsg != NULL) {
+ (void)vsnprintf(s, sizeof(s), emsg, ap);
+ sep = ": ";
+ } else {
+ s[0] = '\0';
+ sep = "";
+ }
+ if (code)
+ logit(LOG_CRIT, "%s: %s%s%s",
+ log_procname, s, sep, strerror(code));
+ else
+ logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
+}
+
+void
+fatal(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ vfatalc(errno, emsg, ap);
+ va_end(ap);
+ exit(1);
+}
+
+void
+fatalx(const char *emsg, ...)
+{
+ va_list ap;
+
+ va_start(ap, emsg);
+ vfatalc(0, emsg, ap);
+ va_end(ap);
+ exit(1);
+}
blob - 116596f41de5c2d6da3d57444c4aa4e1ddf23b8c
blob + 92e3bece94ad9baa77e1709b223d77d6f48161ff
--- smtp_proc.c
+++ smtp_proc.c
#include <sys/socket.h>
#include <arpa/inet.h>
-#include <err.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <syslog.h>
#include <unistd.h>
+#include "log.h"
#include "smtp_proc.h"
#define NITEMS(x) (sizeof(x) / sizeof(*x))
}
void
-smtp_run(void)
+smtp_run(int debug)
{
struct event stdinev;
fflush(stdout);
ready = 1;
+ log_init(debug, LOG_MAIL);
event_init();
event_set(&stdinev, STDIN_FILENO, EV_READ | EV_PERSIST, smtp_newline,
&stdinev);
event_add(&stdinev, NULL);
if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
- err(1, "fcntl");
+ fatal("fcntl");
event_dispatch();
}
while ((linelen = getline(&line, &linesize, stdin)) != -1) {
if (line[linelen - 1] != '\n')
- errx(1, "Invalid line received: missing newline");
+ fatalx("Invalid line received: missing newline");
line[linelen - 1] = '\0';
type = line;
if ((start = strchr(type, '|')) == NULL)
- errx(1, "Invalid line received: missing version");
+ fatalx("Invalid line received: missing version");
start++[0] = '\0';
if ((end = strchr(start, '|')) == NULL)
- errx(1, "Invalid line received: missing time");
+ fatalx("Invalid line received: missing time");
end++[0] = '\0';
if (strcmp(start, "1") != 0)
- errx(1, "Unsupported protocol received: %s", start);
+ fatalx("Unsupported protocol received: %s", start);
version = 1;
start = end;
if ((direction = strchr(start, '|')) == NULL)
- errx(1, "Invalid line received: missing direction");
+ fatalx("Invalid line received: missing direction");
direction++[0] = '\0';
tm.tv_sec = (time_t) strtoull(start, &end, 10);
tm.tv_nsec = 0;
if (start[0] == '\0' || (end[0] != '\0' && end[0] != '.'))
- errx(1, "Invalid line received: invalid timestamp");
+ fatalx("Invalid line received: invalid timestamp");
if (end[0] == '.') {
start = end + 1;
tm.tv_nsec = strtol(start, &end, 10);
if (start[0] == '\0' || end[0] != '\0')
- errx(1, "Invalid line received: invalid "
+ fatalx("Invalid line received: invalid "
"timestamp");
for (i = 9 - (end - start); i > 0; i--)
tm.tv_nsec *= 10;
}
if ((phase = strchr(direction, '|')) == NULL)
- errx(1, "Invalid line receieved: missing phase");
+ fatalx("Invalid line receieved: missing phase");
phase++[0] = '\0';
if ((start = strchr(phase, '|')) == NULL)
- errx(1, "Invalid line received: missing reqid");
+ fatalx("Invalid line received: missing reqid");
start++[0] = '\0';
reqid = strtoull(start, ¶ms, 16);
if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
- errx(1, "Invalid line received: invalid reqid");
+ fatalx("Invalid line received: invalid reqid");
params++;
for (i = 0; i < NITEMS(smtp_callbacks); i++) {
break;
}
if (i == NITEMS(smtp_callbacks)) {
- errx(1, "Invalid line received: received unregistered "
+ fatalx("Invalid line received: received unregistered "
"%s: %s", type, phase);
}
if (strcmp(type, "filter") == 0) {
start = params;
token = strtoull(start, ¶ms, 16);
if (start[0] == '|' || params[0] != '|')
- errx(1, "Invalid line received: invalid token");
+ fatalx("Invalid line received: invalid token");
params++;
smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
version, &tm, reqid, token, params);
hostname = params;
if ((address = strchr(params, '|')) == NULL)
- errx(1, "Invalid line received: missing address");
+ fatalx("Invalid line received: missing address");
address++[0] = '\0';
addrx.af = AF_INET;
ret = inet_pton(addrx.af, address, addrx.af == AF_INET ?
(void *)&(addrx.addr) : (void *)&(addrx.addr6));
if (ret == 0)
- errx(1, "Invalid line received: Couldn't parse address");
+ fatalx("Invalid line received: Couldn't parse address");
if (ret == -1)
- err(1, "Couldn't convert address");
+ fatalx("Couldn't convert address");
f = cb->cb;
f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
va_list ap;
if (code < 200 || code > 599)
- errx(1, "Invalid reject code");
+ fatalx("Invalid reject code");
printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
reqid, code);
int i;
if (ready)
- errx(1, "Can't register when proc is running");
+ fatalx("Can't register when proc is running");
for (i = 0; i < NITEMS(smtp_callbacks); i++) {
if (strcmp(type, smtp_callbacks[i].type) == 0 &&
blob - /dev/null
blob + 22bb416439467fde6a3c3e58209877edd7fdf468 (mode 644)
--- /dev/null
+++ log.h
+/* $OpenBSD: log.h,v 1.8 2018/04/26 20:57:59 eric Exp $ */
+
+/*
+ * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
+ *
+ * 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.
+ */
+
+#ifndef LOG_H
+#define LOG_H
+
+#include <stdarg.h>
+#include <sys/cdefs.h>
+
+void log_init(int, int);
+void log_procinit(const char *);
+void log_setverbose(int);
+int log_getverbose(void);
+void log_warn(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_warnx(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_info(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void log_debug(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+void logit(int, const char *, ...)
+ __attribute__((__format__ (printf, 2, 3)));
+void vlog(int, const char *, va_list)
+ __attribute__((__format__ (printf, 2, 0)));
+__dead void fatal(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+__dead void fatalx(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)));
+
+#endif /* LOG_H */
blob - cc0283931a74f817c3ab383f914332d905000c0c
blob + d44b76f095cc3293fd250780da6f9b83a4d621a3
--- smtp_proc.h
+++ smtp_proc.h
__attribute__((__format__ (printf, 3, 4)));
void smtp_filter_dataline(uint64_t, uint64_t, const char *, ...)
__attribute__((__format__ (printf, 3, 4)));
-void smtp_run(void);
+void smtp_run(int);