commit - 43f97b7c420dd7281de55b19f215a7221e41c3b9
commit + 0a17996036b08cae5fa25ef79c6fbcf2874a45c7
blob - f27e022f35caea7ba9880dcbcfaff4da28ee510f
blob + ac8369da1e85debdce73e9a24fd086307973d538
--- Makefile
+++ Makefile
PROG= filter-dnsbl
BINDIR= /usr/libexec/smtpd/
-SRCS+= main.c log.c smtp_proc.c
+SRCS+= main.c smtp_proc.c
CFLAGS+= -g3 -O0
LDADD+= -levent
blob - 7ec8ca42e18d1c57b84e27a564c23ff352c267cf (mode 644)
blob + /dev/null
--- log.c
+++ /dev/null
-/* $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 - 22bb416439467fde6a3c3e58209877edd7fdf468 (mode 644)
blob + /dev/null
--- log.h
+++ /dev/null
-/* $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 - 346f9822741ed8a6ce7cafbef358d69fc628be58
blob + d1ae5a278784c61b80310851f4d7149fa3e89e58
--- main.c
+++ main.c
#include <sys/socket.h>
#include <arpa/inet.h>
+#include <err.h>
#include <errno.h>
#include <event.h>
#include <inttypes.h>
#include <unistd.h>
#include <asr.h>
-#include "log.h"
#include "smtp_proc.h"
struct dnsbl_session;
int i;
int debug = 0;
- while ((ch = getopt(argc, argv, "dm")) != -1) {
+ while ((ch = getopt(argc, argv, "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");
+ err(1, "pledge");
if ((nblacklists = argc - optind) == 0)
- fatalx("No blacklist specified");
+ errx(1, "No blacklist specified");
if ((blacklists = calloc(nblacklists, sizeof(*blacklists))) == NULL)
- fatal(NULL);
+ err(1, NULL);
for (i = 0; i < nblacklists; i++)
blacklists[i] = argv[optind + i];
int i, try;
if ((session = calloc(1, sizeof(*session))) == NULL)
- fatal(NULL);
+ err(1, NULL);
if ((session->query = calloc(nblacklists, sizeof(*(session->query))))
== NULL)
- fatal(NULL);
+ err(1, NULL);
session->reqid = reqid;
session->token = token;
session->listed = -1;
if (inet_ntop(xaddr->af, xaddr->af == AF_INET ?
(void *)&(xaddr->addr) : (void *)&(xaddr->addr6), session->addr,
sizeof(session->addr)) == NULL)
- fatal("inet_ntop");
+ err(1, "inet_ntop");
RB_INSERT(dnsbl_sessions, &dnsbl_sessions, session);
if (snprintf(query, sizeof(query), "%u.%u.%u.%u.%s",
addr[3], addr[2], addr[1], addr[0],
blacklists[i]) >= sizeof(query))
- fatalx("Can't create query, domain too long");
+ errx(1, "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))
- fatalx( "Can't create query, domain too long");
+ errx(1, "Can't create query, domain too long");
} else
- fatalx("Invalid address family received");
+ errx(1, "Invalid address family received");
session->query[i].query = gethostbyname_async(query, NULL);
session->query[i].event = event_asr_run(session->query[i].query,
if (!markspam) {
smtp_filter_disconnect(session->reqid, session->token,
"Listed at %s", blacklists[query->blacklist]);
- log_info("%016"PRIx64" listed at %s: rejected",
+ warnx("%016"PRIx64" listed at %s: rejected",
session->reqid, blacklists[query->blacklist]);
dnsbl_session_free(session);
} else {
return;
}
smtp_filter_proceed(session->reqid, session->token);
- log_info("%016"PRIx64" not listed", session->reqid);
+ warnx("%016"PRIx64" not listed", session->reqid);
}
void
if (session->listed != -1) {
if (!session->logged_mark) {
- log_info("%016"PRIx64" listed at %s: Marking as spam",
+ warnx("%016"PRIx64" listed at %s: Marking as spam",
session->reqid, blacklists[session->listed]);
session->logged_mark = 1;
}
blob - e522c7d28e3357f5421e4b30c01507bb1c7b64ae
blob + e6708b1ce7cb46802b1c4c47df2df0f9dc92c4a7
--- 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 <syslog.h>
#include <unistd.h>
-#include "log.h"
#include "smtp_proc.h"
#define NITEMS(x) (sizeof(x) / sizeof(*x))
smtp_printf("register|ready\n");
ready = 1;
- log_init(debug, LOG_MAIL);
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)
- fatal("fcntl");
+ err(1, "fcntl");
event_dispatch();
}
*size = sepoff - rsoff + 1;
*buf = realloc(*buf, sepoff - rsoff + 1);
if (*buf == NULL)
- fatal(NULL);
+ err(1, NULL);
}
sep[0] = '\0';
strlen = strlcpy(*buf, rbuf + rsoff, *size);
if (strlen >= *size)
- fatalx("copy buffer too small");
+ errx(1, "copy buffer too small");
rsoff = sepoff + 1;
return strlen;
}
/* If we still can't fill alloc some new memory. */
if (rbsize - reoff < 1500) {
if ((rbuf = realloc(rbuf, rbsize + 4096)) == NULL)
- fatal(NULL);
+ err(1, NULL);
rbsize += 4096;
}
nread = read(STDIN_FILENO, rbuf + reoff, rbsize - reoff);
while ((linelen = smtp_getline(&line, &linesize)) > 0) {
if (dupsize < linesize) {
if ((linedup = realloc(linedup, linesize)) == NULL)
- fatal(NULL);
+ err(1, NULL);
dupsize = linesize;
}
strlcpy(linedup, line, dupsize);
type = line;
if ((start = strchr(type, '|')) == NULL)
- fatalx("Invalid line received: missing version: %s", linedup);
+ errx(1, "Invalid line received: missing version: %s", linedup);
start++[0] = '\0';
if ((end = strchr(start, '|')) == NULL)
- fatalx("Invalid line received: missing time: %s", linedup);
+ errx(1, "Invalid line received: missing time: %s", linedup);
end++[0] = '\0';
if (strcmp(start, "1") != 0)
- fatalx("Unsupported protocol received: %s: %s", start, linedup);
+ errx(1, "Unsupported protocol received: %s: %s", start, linedup);
version = 1;
start = end;
if ((direction = strchr(start, '|')) == NULL)
- fatalx("Invalid line received: missing direction: %s", linedup);
+ errx(1, "Invalid line received: missing direction: %s", linedup);
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] != '.'))
- fatalx("Invalid line received: invalid timestamp: %s", linedup);
+ errx(1, "Invalid line received: invalid timestamp: %s", linedup);
if (end[0] == '.') {
start = end + 1;
tm.tv_nsec = strtol(start, &end, 10);
if (start[0] == '\0' || end[0] != '\0')
- fatalx("Invalid line received: invalid "
+ errx(1, "Invalid line received: invalid "
"timestamp: %s", linedup);
for (i = 9 - (end - start); i > 0; i--)
tm.tv_nsec *= 10;
}
if ((phase = strchr(direction, '|')) == NULL)
- fatalx("Invalid line receieved: missing phase: %s", linedup);
+ errx(1, "Invalid line receieved: missing phase: %s", linedup);
phase++[0] = '\0';
if ((start = strchr(phase, '|')) == NULL)
- fatalx("Invalid line received: missing reqid: %s", linedup);
+ errx(1, "Invalid line received: missing reqid: %s", linedup);
start++[0] = '\0';
reqid = strtoull(start, ¶ms, 16);
if (start[0] == '|' || (params[0] != '|' & params[0] != '\0'))
- fatalx("Invalid line received: invalid reqid: %s", linedup);
+ errx(1, "Invalid line received: invalid reqid: %s", linedup);
params++;
for (i = 0; i < NITEMS(smtp_callbacks); i++) {
break;
}
if (i == NITEMS(smtp_callbacks)) {
- fatalx("Invalid line received: received unregistered "
+ errx(1, "Invalid line received: received unregistered "
"%s: %s: %s", type, phase, linedup);
}
if (strcmp(type, "filter") == 0) {
start = params;
token = strtoull(start, ¶ms, 16);
if (start[0] == '|' || params[0] != '|')
- fatalx("Invalid line received: invalid token: %s", linedup);
+ errx(1, "Invalid line received: invalid token: %s", linedup);
params++;
smtp_callbacks[i].smtp_filter(&(smtp_callbacks[i]),
version, &tm, reqid, token, params);
hostname = params;
if ((address = strchr(params, '|')) == NULL)
- fatalx("Invalid line received: missing address: %s", params);
+ errx(1, "Invalid line received: missing address: %s", params);
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)
- fatalx("Invalid line received: Couldn't parse address: %s", params);
+ errx(1, "Invalid line received: Couldn't parse address: %s", params);
if (ret == -1)
- fatal("Couldn't convert address: %s", params);
+ err(1, "Couldn't convert address: %s", params);
f = cb->cb;
f(cb->type, version, tm, cb->direction, cb->phase, reqid, token,
fmtlen = vsnprintf(buf.buf + buf.buflen, buf.bufsize - buf.buflen, fmt,
ap);
if (fmtlen == -1)
- fatal("vsnprintf");
+ err(1, "vsnprintf");
if (fmtlen >= buf.bufsize - buf.buflen) {
buf.bufsize = buf.buflen + fmtlen + 1;
buf.buf = reallocarray(buf.buf, buf.bufsize,
sizeof(*(buf.buf)));
if (buf.buf == NULL)
- fatal(NULL);
+ err(1, NULL);
fmtlen = vsnprintf(buf.buf + buf.buflen,
buf.bufsize - buf.buflen, fmt, cap);
if (fmtlen == -1)
- fatal("vsnprintf");
+ err(1, "vsnprintf");
}
va_end(cap);
buf.buflen += fmtlen;
wlen = write(fd, buf->buf, buf->buflen);
if (wlen == -1) {
if (errno != EAGAIN && errno != EINTR)
- fatal("Failed to write to smtpd");
+ err(1, "Failed to write to smtpd");
event_add(&stdoutev, NULL);
return;
}
va_list ap;
if (code < 200 || code > 599)
- fatalx("Invalid reject code");
+ errx(1, "Invalid reject code");
smtp_printf("filter-result|%016"PRIx64"|%016"PRIx64"|reject|%d ", token,
reqid, code);
static int evinit = 0;
if (ready)
- fatalx("Can't register when proc is running");
+ errx(1, "Can't register when proc is running");
if (!evinit) {
event_init();