pax_global_header00006660000000000000000000000064140556610220014513gustar00rootroot0000000000000052 comment=3a4f7bc3ddd952b621f03eb6461e0380d5e3bddc libopensmtpd-0.7/000077500000000000000000000000001405566102200140615ustar00rootroot00000000000000libopensmtpd-0.7/LICENSE000066400000000000000000000013621405566102200150700ustar00rootroot00000000000000Copyright (c) 2019 Martijn van Duren 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. libopensmtpd-0.7/Makefile000066400000000000000000000017241405566102200155250ustar00rootroot00000000000000LIB= opensmtpd LOCALBASE?= /usr/local/ SRCS= opensmtpd.c iobuf.c ioev.c HDRS= opensmtpd.h MAN= osmtpd_run.3 LIBDIR= ${LOCALBASE}/lib/ MANDIR= ${LOCALBASE}/man/man LDADD= -levent DPADD= ${EVENT} CFLAGS+= -Wall -I${.CURDIR} -I${.CURDIR}/openbsd-compat CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes CFLAGS+= -Wmissing-declarations CFLAGS+= -Wshadow -Wpointer-arith -Wcast-qual CFLAGS+= -Wsign-compare CLEANFILES= ${VERSION_SCRIPT} VERSION_SCRIPT= Symbols.map SYMBOL_LIST= ${.CURDIR}/Symbols.list includes: @cd ${.CURDIR}; for i in $(HDRS); do \ j="cmp -s $$i ${DESTDIR}${LOCALBASE}/include/$$i || \ ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m 444 $$i\ ${DESTDIR}${LOCALBASE}/include/"; \ echo $$j; \ eval "$$j"; \ done; ${VERSION_SCRIPT}: ${SYMBOL_LIST} { printf '{\n\tglobal:\n'; \ sed '/^[._a-zA-Z]/s/$$/;/; s/^/ /' ${SYMBOL_LIST}; \ printf '\n\tlocal:\n\t\t*;\n};\n'; } >$@.tmp && mv $@.tmp $@ .include libopensmtpd-0.7/Makefile.gnu000066400000000000000000000075401405566102200163170ustar00rootroot00000000000000LIB= opensmtpd LOCALBASE?= /usr SRCS= opensmtpd.c iobuf.c ioev.c HDRS= opensmtpd.h MAN= osmtpd_run.3 LIBDIR?= ${LOCALBASE}/lib/ MANDIR?= ${LOCALBASE}/share/man/man3 LDLIBS+= -levent mkfile_path := ${abspath ${lastword ${MAKEFILE_LIST}}} CURDIR := ${dir ${mkfile_path}} CFLAGS+= -I${CURDIR} -I${CURDIR}/openbsd-compat/ CFLAGS+= -Wall CFLAGS+= -Wstrict-prototypes -Wmissing-prototypes CFLAGS+= -Wmissing-declarations CFLAGS+= -Wshadow -Wpointer-arith -Wcast-qual CFLAGS+= -Wsign-compare CLEANFILES= ${VERSION_SCRIPT} VERSION_SCRIPT= Symbols.map SYMBOL_LIST= ${CURDIR}/Symbols.list includes: @cd ${CURDIR}; for i in ${HDRS}; do \ j="cmp -s $$i ${DESTDIR}${LOCALBASE}/include/$$i || \ ${INSTALL} -D -o ${BINOWN} -g ${BINGRP} -m 444 $$i\ ${DESTDIR}${LOCALBASE}/include/$$i"; \ echo $$j; \ eval "$$j"; \ done; ${VERSION_SCRIPT}: ${SYMBOL_LIST} { printf '{\n\tglobal:\n'; \ sed '/^[._a-zA-Z]/s/$$/;/; s/^/ /' ${SYMBOL_LIST}; \ printf '\n\tlocal:\n\t\t*;\n};\n'; } >$@.tmp && mv $@.tmp $@ # Defines for OpenBSD-specific interfaces # Add a OS-specific defines here. NEED_EXPLICIT_BZERO?= 0 NEED_RECALLOCARRAY?= 1 NEED_REALLOCARRAY?= 0 NEED_STRLCAT?= 1 NEED_STRLCPY?= 1 NEED_STRTONUM?= 1 MANFORMAT?= mangz INSTALL?= install LINK?= ln BINOWN?= root BINGRP?= root LIBPERM?= 755 MANOWN?= root MANGRP?= root MANPERM?= 644 include ${CURDIR}/shlib_version BASE_LIB=lib${LIB}.so TARGET_LIB=lib${LIB}.so.${major}.${minor}.0 SONAME_LIB=lib${LIB}.so.${major} CFLAGS+= -fPIC LDFLAGS+= -shared -Wl,-soname=${SONAME_LIB} ifeq (${MANFORMAT}, mangz) TARGET_MAN= ${MAN}.gz CLEANFILES+= ${TARGET_MAN} ${TARGET_MAN}: ${MAN} mandoc -Tman ${MAN} | gzip > $@ else TARGET_MAN= ${MAN} endif ${SRCS:.c=.d}:%.d:%.c ${CC} ${CFLAGS} -MM $< >$@ ifeq (${NEED_EXPLICIT_BZERO}, 1) SRCS+= ${CURDIR}/openbsd-compat/explicit_bzero.c CFLAGS+= -DNEED_EXPLICIT_BZERO=1 explicit_bzero.o: ${CURDIR}/openbsd-compat/explicit_bzero.c ${CC} ${CFLAGS} -c -o explicit_bzero.o ${CURDIR}/openbsd-compat/explicit_bzero.c endif ifeq (${NEED_RECALLOCARRAY}, 1) SRCS+= ${CURDIR}/openbsd-compat/recallocarray.c CFLAGS+= -DNEED_RECALLOCARRAY=1 recallocarray.o: ${CURDIR}/openbsd-compat/recallocarray.c ${CC} ${CFLAGS} -c -o recallocarray.o ${CURDIR}/openbsd-compat/recallocarray.c endif ifeq (${NEED_REALLOCARRAY}, 1) SRCS+= ${CURDIR}/openbsd-compat/reallocarray.c CFLAGS+= -DNEED_REALLOCARRAY=1 reallocarray.o: ${CURDIR}/openbsd-compat/reallocarray.c ${CC} ${CFLAGS} -c -o reallocarray.o ${CURDIR}/openbsd-compat/reallocarray.c endif ifeq (${NEED_STRLCAT}, 1) SRCS+= ${CURDIR}/openbsd-compat/strlcat.c CFLAGS+= -DNEED_STRLCAT=1 strlcat.o: ${CURDIR}/openbsd-compat/strlcat.c ${CC} ${CFLAGS} -c -o strlcat.o ${CURDIR}/openbsd-compat/strlcat.c endif ifeq (${NEED_STRLCPY}, 1) SRCS+= ${CURDIR}/openbsd-compat/strlcpy.c CFLAGS+= -DNEED_STRLCPY=1 strlcpy.o: ${CURDIR}/openbsd-compat/strlcpy.c ${CC} ${CFLAGS} -c -o strlcpy.o ${CURDIR}/openbsd-compat/strlcpy.c endif ifeq (${NEED_STRTONUM}, 1) SRCS+= ${CURDIR}/openbsd-compat/strtonum.c CFLAGS+= -DNEED_STRTONUM=1 strtonum.o: ${CURDIR}/openbsd-compat/strtonum.c ${CC} ${CFLAGS} -c -o strtonum.o ${CURDIR}/openbsd-compat/strtonum.c endif OBJS= ${notdir ${SRCS:.c=.o}} ifdef VERSION_SCRIPT ${TARGET_LIB}: ${VERSION_SCRIPT} LDFLAGS+= -Wl,--version-script=${VERSION_SCRIPT} endif ${TARGET_LIB}: ${OBJS} ${CC} ${LDFLAGS} -o $@ ${OBJS} ${LDLIBS} .DEFAULT_GOAL= all .PHONY: all all: ${TARGET_LIB} ${TARGET_MAN} .PHONY: install install: includes ${TARGET_LIB} ${TARGET_MAN} ${INSTALL} -D -o ${BINOWN} -g ${BINGRP} -m ${LIBPERM} ${TARGET_LIB} ${DESTDIR}${LIBDIR}/${TARGET_LIB} ${LINK} -s ${TARGET_LIB} ${DESTDIR}${LIBDIR}/${SONAME_LIB} ${LINK} -s ${TARGET_LIB} ${DESTDIR}${LIBDIR}/${BASE_LIB} ${INSTALL} -D -o ${MANOWN} -g ${MANGRP} -m ${MANPERM} ${TARGET_MAN} ${DESTDIR}${MANDIR}/${TARGET_MAN} CLEANFILES+= *.o ${TARGET_LIB} .PHONY: clean clean: rm -f ${CLEANFILES} libopensmtpd-0.7/Symbols.list000066400000000000000000000021721405566102200164100ustar00rootroot00000000000000osmtpd_register_conf osmtpd_register_filter_connect osmtpd_register_filter_helo osmtpd_register_filter_ehlo osmtpd_register_filter_starttls osmtpd_register_filter_auth osmtpd_register_filter_mailfrom osmtpd_register_filter_rcptto osmtpd_register_filter_data osmtpd_register_filter_dataline osmtpd_register_filter_rset osmtpd_register_filter_quit osmtpd_register_filter_noop osmtpd_register_filter_help osmtpd_register_filter_wiz osmtpd_register_filter_commit osmtpd_register_report_connect osmtpd_register_report_disconnect osmtpd_register_report_identify osmtpd_register_report_tls osmtpd_register_report_begin osmtpd_register_report_mail osmtpd_register_report_rcpt osmtpd_register_report_envelope osmtpd_register_report_data osmtpd_register_report_commit osmtpd_register_report_rollback osmtpd_register_report_client osmtpd_register_report_server osmtpd_register_report_response osmtpd_register_report_timeout osmtpd_filter_proceed osmtpd_filter_reject osmtpd_filter_disconnect osmtpd_filter_reject_enh osmtpd_filter_disconnect_enh osmtpd_filter_dataline osmtpd_local_session osmtpd_local_message osmtpd_need osmtpd_run osmtpd_err osmtpd_errx libopensmtpd-0.7/iobuf.c000066400000000000000000000175501405566102200153410ustar00rootroot00000000000000/* $OpenBSD: iobuf.c,v 1.11 2019/06/12 17:42:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot * * 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. */ #define _GNU_SOURCE 1 #define _BSD_SOURCE 1 #include #include #include #include #include #include #include #include #include #include #ifdef IO_TLS #include #include #endif #include "openbsd-compat.h" #include "iobuf.h" #define IOBUF_MAX 65536 #define IOBUFQ_MIN 4096 struct ioqbuf *ioqbuf_alloc(struct iobuf *, size_t); void iobuf_drain(struct iobuf *, size_t); int iobuf_init(struct iobuf *io, size_t size, size_t max) { memset(io, 0, sizeof *io); if (max == 0) max = IOBUF_MAX; if (size == 0) size = max; if (size > max) return (-1); if ((io->buf = calloc(size, 1)) == NULL) return (-1); io->size = size; io->max = max; return (0); } void iobuf_clear(struct iobuf *io) { struct ioqbuf *q; free(io->buf); while ((q = io->outq)) { io->outq = q->next; free(q); } memset(io, 0, sizeof (*io)); } void iobuf_drain(struct iobuf *io, size_t n) { struct ioqbuf *q; size_t left = n; while ((q = io->outq) && left) { if ((q->wpos - q->rpos) > left) { q->rpos += left; left = 0; } else { left -= q->wpos - q->rpos; io->outq = q->next; free(q); } } io->queued -= (n - left); if (io->outq == NULL) io->outqlast = NULL; } int iobuf_extend(struct iobuf *io, size_t n) { char *t; if (n > io->max) return (-1); if (io->max - io->size < n) return (-1); t = recallocarray(io->buf, io->size, io->size + n, 1); if (t == NULL) return (-1); io->size += n; io->buf = t; return (0); } size_t iobuf_left(struct iobuf *io) { return io->size - io->wpos; } size_t iobuf_space(struct iobuf *io) { return io->size - (io->wpos - io->rpos); } size_t iobuf_len(struct iobuf *io) { return io->wpos - io->rpos; } char * iobuf_data(struct iobuf *io) { return io->buf + io->rpos; } void iobuf_drop(struct iobuf *io, size_t n) { if (n >= iobuf_len(io)) { io->rpos = io->wpos = 0; return; } io->rpos += n; } char * iobuf_getline(struct iobuf *iobuf, size_t *rlen) { char *buf; size_t len, i; buf = iobuf_data(iobuf); len = iobuf_len(iobuf); for (i = 0; i + 1 <= len; i++) if (buf[i] == '\n') { /* Note: the returned address points into the iobuf * buffer. We NUL-end it for convenience, and discard * the data from the iobuf, so that the caller doesn't * have to do it. The data remains "valid" as long * as the iobuf does not overwrite it, that is until * the next call to iobuf_normalize() or iobuf_extend(). */ iobuf_drop(iobuf, i + 1); len = (i && buf[i - 1] == '\r') ? i - 1 : i; buf[len] = '\0'; if (rlen) *rlen = len; return (buf); } return (NULL); } void iobuf_normalize(struct iobuf *io) { if (io->rpos == 0) return; if (io->rpos == io->wpos) { io->rpos = io->wpos = 0; return; } memmove(io->buf, io->buf + io->rpos, io->wpos - io->rpos); io->wpos -= io->rpos; io->rpos = 0; } ssize_t iobuf_read(struct iobuf *io, int fd) { ssize_t n; n = read(fd, io->buf + io->wpos, iobuf_left(io)); if (n == -1) { /* XXX is this really what we want? */ if (errno == EAGAIN || errno == EINTR) return (IOBUF_WANT_READ); return (IOBUF_ERROR); } if (n == 0) return (IOBUF_CLOSED); io->wpos += n; return (n); } struct ioqbuf * ioqbuf_alloc(struct iobuf *io, size_t len) { struct ioqbuf *q; if (len < IOBUFQ_MIN) len = IOBUFQ_MIN; if ((q = malloc(sizeof(*q) + len)) == NULL) return (NULL); q->rpos = 0; q->wpos = 0; q->size = len; q->next = NULL; q->buf = (char *)(q) + sizeof(*q); if (io->outqlast == NULL) io->outq = q; else io->outqlast->next = q; io->outqlast = q; return (q); } size_t iobuf_queued(struct iobuf *io) { return io->queued; } void * iobuf_reserve(struct iobuf *io, size_t len) { struct ioqbuf *q; void *r; if (len == 0) return (NULL); if (((q = io->outqlast) == NULL) || q->size - q->wpos <= len) { if ((q = ioqbuf_alloc(io, len)) == NULL) return (NULL); } r = q->buf + q->wpos; q->wpos += len; io->queued += len; return (r); } int iobuf_queue(struct iobuf *io, const void *data, size_t len) { void *buf; if (len == 0) return (0); if ((buf = iobuf_reserve(io, len)) == NULL) return (-1); memmove(buf, data, len); return (len); } int iobuf_queuev(struct iobuf *io, const struct iovec *iov, int iovcnt) { int i; size_t len = 0; char *buf; for (i = 0; i < iovcnt; i++) len += iov[i].iov_len; if ((buf = iobuf_reserve(io, len)) == NULL) return (-1); for (i = 0; i < iovcnt; i++) { if (iov[i].iov_len == 0) continue; memmove(buf, iov[i].iov_base, iov[i].iov_len); buf += iov[i].iov_len; } return (0); } int iobuf_fqueue(struct iobuf *io, const char *fmt, ...) { va_list ap; int len; va_start(ap, fmt); len = iobuf_vfqueue(io, fmt, ap); va_end(ap); return (len); } int iobuf_vfqueue(struct iobuf *io, const char *fmt, va_list ap) { char *buf; int len; len = vasprintf(&buf, fmt, ap); if (len == -1) return (-1); len = iobuf_queue(io, buf, len); free(buf); return (len); } ssize_t iobuf_write(struct iobuf *io, int fd) { struct iovec iov[IOV_MAX]; struct ioqbuf *q; int i; ssize_t n; i = 0; for (q = io->outq; q ; q = q->next) { if (i >= IOV_MAX) break; iov[i].iov_base = q->buf + q->rpos; iov[i].iov_len = q->wpos - q->rpos; i++; } n = writev(fd, iov, i); if (n == -1) { if (errno == EAGAIN || errno == EINTR) return (IOBUF_WANT_WRITE); if (errno == EPIPE) return (IOBUF_CLOSED); return (IOBUF_ERROR); } iobuf_drain(io, n); return (n); } int iobuf_flush(struct iobuf *io, int fd) { ssize_t s; while (io->queued) if ((s = iobuf_write(io, fd)) < 0) return (s); return (0); } #ifdef IO_TLS int iobuf_flush_tls(struct iobuf *io, void *tls) { ssize_t s; while (io->queued) if ((s = iobuf_write_tls(io, tls)) < 0) return (s); return (0); } ssize_t iobuf_write_tls(struct iobuf *io, void *tls) { struct ioqbuf *q; int r; ssize_t n; q = io->outq; n = SSL_write(tls, q->buf + q->rpos, q->wpos - q->rpos); if (n <= 0) { switch ((r = SSL_get_error(tls, n))) { case SSL_ERROR_WANT_READ: return (IOBUF_WANT_READ); case SSL_ERROR_WANT_WRITE: return (IOBUF_WANT_WRITE); case SSL_ERROR_ZERO_RETURN: /* connection closed */ return (IOBUF_CLOSED); case SSL_ERROR_SYSCALL: if (ERR_peek_last_error()) return (IOBUF_TLSERROR); if (r == 0) errno = EPIPE; return (IOBUF_ERROR); default: return (IOBUF_TLSERROR); } } iobuf_drain(io, n); return (n); } ssize_t iobuf_read_tls(struct iobuf *io, void *tls) { ssize_t n; int r; n = SSL_read(tls, io->buf + io->wpos, iobuf_left(io)); if (n < 0) { switch ((r = SSL_get_error(tls, n))) { case SSL_ERROR_WANT_READ: return (IOBUF_WANT_READ); case SSL_ERROR_WANT_WRITE: return (IOBUF_WANT_WRITE); case SSL_ERROR_SYSCALL: if (ERR_peek_last_error()) return (IOBUF_TLSERROR); if (r == 0) errno = EPIPE; return (IOBUF_ERROR); default: return (IOBUF_TLSERROR); } } else if (n == 0) return (IOBUF_CLOSED); io->wpos += n; return (n); } #endif /* IO_TLS */ libopensmtpd-0.7/iobuf.h000066400000000000000000000042521405566102200153410ustar00rootroot00000000000000/* $OpenBSD: iobuf.h,v 1.5 2019/06/12 17:42:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot * * 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. */ struct ioqbuf { struct ioqbuf *next; char *buf; size_t size; size_t wpos; size_t rpos; }; struct iobuf { char *buf; size_t max; size_t size; size_t wpos; size_t rpos; size_t queued; struct ioqbuf *outq; struct ioqbuf *outqlast; }; #define IOBUF_WANT_READ -1 #define IOBUF_WANT_WRITE -2 #define IOBUF_CLOSED -3 #define IOBUF_ERROR -4 #define IOBUF_TLSERROR -5 int iobuf_init(struct iobuf *, size_t, size_t); void iobuf_clear(struct iobuf *); int iobuf_extend(struct iobuf *, size_t); void iobuf_normalize(struct iobuf *); void iobuf_drop(struct iobuf *, size_t); size_t iobuf_space(struct iobuf *); size_t iobuf_len(struct iobuf *); size_t iobuf_left(struct iobuf *); char *iobuf_data(struct iobuf *); char *iobuf_getline(struct iobuf *, size_t *); ssize_t iobuf_read(struct iobuf *, int); ssize_t iobuf_read_tls(struct iobuf *, void *); size_t iobuf_queued(struct iobuf *); void* iobuf_reserve(struct iobuf *, size_t); int iobuf_queue(struct iobuf *, const void*, size_t); int iobuf_queuev(struct iobuf *, const struct iovec *, int); int iobuf_fqueue(struct iobuf *, const char *, ...); int iobuf_vfqueue(struct iobuf *, const char *, va_list); int iobuf_flush(struct iobuf *, int); int iobuf_flush_tls(struct iobuf *, void *); ssize_t iobuf_write(struct iobuf *, int); ssize_t iobuf_write_tls(struct iobuf *, void *); libopensmtpd-0.7/ioev.c000066400000000000000000000474611405566102200152030ustar00rootroot00000000000000/* $OpenBSD: ioev.c,v 1.42 2019/06/12 17:42:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot * * 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. */ #define _GNU_SOURCE 1 #define _BSD_SOURCE 1 #include #include #include #include #include #include #include #include #include #include #include #include #include "openbsd-compat.h" #include "ioev.h" #include "iobuf.h" #ifdef IO_TLS #include #include #endif enum { IO_STATE_NONE, IO_STATE_CONNECT, IO_STATE_CONNECT_TLS, IO_STATE_ACCEPT_TLS, IO_STATE_UP, IO_STATE_MAX, }; #define IO_PAUSE_IN IO_IN #define IO_PAUSE_OUT IO_OUT #define IO_READ 0x04 #define IO_WRITE 0x08 #define IO_RW (IO_READ | IO_WRITE) #define IO_RESET 0x10 /* internal */ #define IO_HELD 0x20 /* internal */ struct io { int sock; void *arg; void (*cb)(struct io*, int, void *); struct iobuf iobuf; size_t lowat; int timeout; int flags; int state; struct event ev; void *tls; const char *error; /* only valid immediately on callback */ }; const char* io_strflags(int); const char* io_evstr(short); void _io_init(void); void io_hold(struct io *); void io_release(struct io *); void io_callback(struct io*, int); void io_dispatch(int, short, void *); void io_dispatch_connect(int, short, void *); size_t io_pending(struct io *); size_t io_queued(struct io*); void io_reset(struct io *, short, void (*)(int, short, void*)); void io_frame_enter(const char *, struct io *, int); void io_frame_leave(struct io *); #ifdef IO_TLS void ssl_error(const char *); /* XXX external */ static const char* io_tls_error(void); void io_dispatch_accept_tls(int, short, void *); void io_dispatch_connect_tls(int, short, void *); void io_dispatch_read_tls(int, short, void *); void io_dispatch_write_tls(int, short, void *); void io_reload_tls(struct io *io); #endif static struct io *current = NULL; static uint64_t frame = 0; static int _io_debug = 0; #define io_debug(args...) do { if (_io_debug) printf(args); } while(0) const char* io_strio(struct io *io) { static char buf[128]; char ssl[128]; ssl[0] = '\0'; #ifdef IO_TLS if (io->tls) { (void)snprintf(ssl, sizeof ssl, " tls=%s:%s:%d", SSL_get_version(io->tls), SSL_get_cipher_name(io->tls), SSL_get_cipher_bits(io->tls, NULL)); } #endif (void)snprintf(buf, sizeof buf, "", io, io->sock, io->timeout, io_strflags(io->flags), ssl, io_pending(io), io_queued(io)); return (buf); } #define CASE(x) case x : return #x const char* io_strevent(int evt) { static char buf[32]; switch (evt) { CASE(IO_CONNECTED); CASE(IO_TLSREADY); CASE(IO_DATAIN); CASE(IO_LOWAT); CASE(IO_DISCONNECTED); CASE(IO_TIMEOUT); CASE(IO_ERROR); default: (void)snprintf(buf, sizeof(buf), "IO_? %d", evt); return buf; } } void io_set_nonblocking(int fd) { int flags; if ((flags = fcntl(fd, F_GETFL)) == -1) err(1, "io_set_blocking:fcntl(F_GETFL)"); flags |= O_NONBLOCK; if (fcntl(fd, F_SETFL, flags) == -1) err(1, "io_set_blocking:fcntl(F_SETFL)"); } void io_set_nolinger(int fd) { struct linger l; memset(&l, 0, sizeof(l)); if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) == -1) err(1, "io_set_linger:setsockopt()"); } /* * Event framing must not rely on an io pointer to refer to the "same" io * throughout the frame, because this is not always the case: * * 1) enter(addr0) -> free(addr0) -> leave(addr0) = SEGV * 2) enter(addr0) -> free(addr0) -> malloc == addr0 -> leave(addr0) = BAD! * * In both case, the problem is that the io is freed in the callback, so * the pointer becomes invalid. If that happens, the user is required to * call io_clear, so we can adapt the frame state there. */ void io_frame_enter(const char *where, struct io *io, int ev) { io_debug("\n=== %" PRIu64 " ===\n" "io_frame_enter(%s, %s, %s)\n", frame, where, io_evstr(ev), io_strio(io)); if (current) errx(1, "io_frame_enter: interleaved frames"); current = io; io_hold(io); } void io_frame_leave(struct io *io) { io_debug("io_frame_leave(%" PRIu64 ")\n", frame); if (current && current != io) errx(1, "io_frame_leave: io mismatch"); /* io has been cleared */ if (current == NULL) goto done; /* TODO: There is a possible optimization there: * In a typical half-duplex request/response scenario, * the io is waiting to read a request, and when done, it queues * the response in the output buffer and goes to write mode. * There, the write event is set and will be triggered in the next * event frame. In most case, the write call could be done * immediately as part of the last read frame, thus avoiding to go * through the event loop machinery. So, as an optimisation, we * could detect that case here and force an event dispatching. */ /* Reload the io if it has not been reset already. */ io_release(io); current = NULL; done: io_debug("=== /%" PRIu64 "\n", frame); frame += 1; } void _io_init() { static int init = 0; if (init) return; init = 1; _io_debug = getenv("IO_DEBUG") != NULL; } struct io * io_new(void) { struct io *io; _io_init(); if ((io = calloc(1, sizeof(*io))) == NULL) return NULL; io->sock = -1; io->timeout = -1; if (iobuf_init(&io->iobuf, 0, 0) == -1) { free(io); return NULL; } return io; } void io_free(struct io *io) { io_debug("io_clear(%p)\n", io); /* the current io is virtually dead */ if (io == current) current = NULL; #ifdef IO_TLS SSL_free(io->tls); io->tls = NULL; #endif if (event_initialized(&io->ev)) event_del(&io->ev); if (io->sock != -1) { close(io->sock); io->sock = -1; } iobuf_clear(&io->iobuf); free(io); } void io_hold(struct io *io) { io_debug("io_enter(%p)\n", io); if (io->flags & IO_HELD) errx(1, "io_hold: io is already held"); io->flags &= ~IO_RESET; io->flags |= IO_HELD; } void io_release(struct io *io) { if (!(io->flags & IO_HELD)) errx(1, "io_release: io is not held"); io->flags &= ~IO_HELD; if (!(io->flags & IO_RESET)) io_reload(io); } void io_set_fd(struct io *io, int fd) { io->sock = fd; if (fd != -1) io_reload(io); } void io_set_callback(struct io *io, void(*cb)(struct io *, int, void *), void *arg) { io->cb = cb; io->arg = arg; } void io_set_timeout(struct io *io, int msec) { io_debug("io_set_timeout(%p, %d)\n", io, msec); io->timeout = msec; } void io_set_lowat(struct io *io, size_t lowat) { io_debug("io_set_lowat(%p, %zu)\n", io, lowat); io->lowat = lowat; } void io_pause(struct io *io, int dir) { io_debug("io_pause(%p, %x)\n", io, dir); io->flags |= dir & (IO_PAUSE_IN | IO_PAUSE_OUT); io_reload(io); } void io_resume(struct io *io, int dir) { io_debug("io_resume(%p, %x)\n", io, dir); io->flags &= ~(dir & (IO_PAUSE_IN | IO_PAUSE_OUT)); io_reload(io); } void io_set_read(struct io *io) { int mode; io_debug("io_set_read(%p)\n", io); mode = io->flags & IO_RW; if (!(mode == 0 || mode == IO_WRITE)) errx(1, "io_set_read(): full-duplex or reading"); io->flags &= ~IO_RW; io->flags |= IO_READ; io_reload(io); } void io_set_write(struct io *io) { int mode; io_debug("io_set_write(%p)\n", io); mode = io->flags & IO_RW; if (!(mode == 0 || mode == IO_READ)) errx(1, "io_set_write(): full-duplex or writing"); io->flags &= ~IO_RW; io->flags |= IO_WRITE; io_reload(io); } const char * io_error(struct io *io) { return io->error; } void * io_tls(struct io *io) { return io->tls; } int io_fileno(struct io *io) { return io->sock; } int io_paused(struct io *io, int what) { return (io->flags & (IO_PAUSE_IN | IO_PAUSE_OUT)) == what; } /* * Buffered output functions */ int io_write(struct io *io, const void *buf, size_t len) { int r; r = iobuf_queue(&io->iobuf, buf, len); io_reload(io); return r; } int io_writev(struct io *io, const struct iovec *iov, int iovcount) { int r; r = iobuf_queuev(&io->iobuf, iov, iovcount); io_reload(io); return r; } int io_print(struct io *io, const char *s) { return io_write(io, s, strlen(s)); } int io_printf(struct io *io, const char *fmt, ...) { va_list ap; int r; va_start(ap, fmt); r = io_vprintf(io, fmt, ap); va_end(ap); return r; } int io_vprintf(struct io *io, const char *fmt, va_list ap) { char *buf; int len; len = vasprintf(&buf, fmt, ap); if (len == -1) return -1; len = io_write(io, buf, len); free(buf); return len; } size_t io_queued(struct io *io) { return iobuf_queued(&io->iobuf); } /* * Buffered input functions */ void * io_data(struct io *io) { return iobuf_data(&io->iobuf); } size_t io_datalen(struct io *io) { return iobuf_len(&io->iobuf); } char * io_getline(struct io *io, size_t *sz) { return iobuf_getline(&io->iobuf, sz); } void io_drop(struct io *io, size_t sz) { return iobuf_drop(&io->iobuf, sz); } #define IO_READING(io) (((io)->flags & IO_RW) != IO_WRITE) #define IO_WRITING(io) (((io)->flags & IO_RW) != IO_READ) /* * Setup the necessary events as required by the current io state, * honouring duplex mode and i/o pauses. */ void io_reload(struct io *io) { short events; /* io will be reloaded at release time */ if (io->flags & IO_HELD) return; iobuf_normalize(&io->iobuf); #ifdef IO_TLS if (io->tls) { io_reload_tls(io); return; } #endif io_debug("io_reload(%p)\n", io); events = 0; if (IO_READING(io) && !(io->flags & IO_PAUSE_IN)) events = EV_READ; if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) && io_queued(io)) events |= EV_WRITE; io_reset(io, events, io_dispatch); } /* Set the requested event. */ void io_reset(struct io *io, short events, void (*dispatch)(int, short, void*)) { struct timeval tv, *ptv; io_debug("io_reset(%p, %s, %p) -> %s\n", io, io_evstr(events), dispatch, io_strio(io)); /* * Indicate that the event has already been reset so that reload * is not called on frame_leave. */ io->flags |= IO_RESET; if (event_initialized(&io->ev)) event_del(&io->ev); /* * The io is paused by the user, so we don't want the timeout to be * effective. */ if (events == 0) return; event_set(&io->ev, io->sock, events, dispatch, io); if (io->timeout >= 0) { tv.tv_sec = io->timeout / 1000; tv.tv_usec = (io->timeout % 1000) * 1000; ptv = &tv; } else ptv = NULL; event_add(&io->ev, ptv); } size_t io_pending(struct io *io) { return iobuf_len(&io->iobuf); } const char* io_strflags(int flags) { static char buf[64]; buf[0] = '\0'; switch (flags & IO_RW) { case 0: (void)strlcat(buf, "rw", sizeof buf); break; case IO_READ: (void)strlcat(buf, "R", sizeof buf); break; case IO_WRITE: (void)strlcat(buf, "W", sizeof buf); break; case IO_RW: (void)strlcat(buf, "RW", sizeof buf); break; } if (flags & IO_PAUSE_IN) (void)strlcat(buf, ",F_PI", sizeof buf); if (flags & IO_PAUSE_OUT) (void)strlcat(buf, ",F_PO", sizeof buf); return buf; } const char* io_evstr(short ev) { static char buf[64]; char buf2[16]; int n; n = 0; buf[0] = '\0'; if (ev == 0) { (void)strlcat(buf, "", sizeof(buf)); return buf; } if (ev & EV_TIMEOUT) { (void)strlcat(buf, "EV_TIMEOUT", sizeof(buf)); ev &= ~EV_TIMEOUT; n++; } if (ev & EV_READ) { if (n) (void)strlcat(buf, "|", sizeof(buf)); (void)strlcat(buf, "EV_READ", sizeof(buf)); ev &= ~EV_READ; n++; } if (ev & EV_WRITE) { if (n) (void)strlcat(buf, "|", sizeof(buf)); (void)strlcat(buf, "EV_WRITE", sizeof(buf)); ev &= ~EV_WRITE; n++; } if (ev & EV_SIGNAL) { if (n) (void)strlcat(buf, "|", sizeof(buf)); (void)strlcat(buf, "EV_SIGNAL", sizeof(buf)); ev &= ~EV_SIGNAL; n++; } if (ev) { if (n) (void)strlcat(buf, "|", sizeof(buf)); (void)strlcat(buf, "EV_?=0x", sizeof(buf)); (void)snprintf(buf2, sizeof(buf2), "%hx", ev); (void)strlcat(buf, buf2, sizeof(buf)); } return buf; } void io_dispatch(__unused int fd, short ev, void *humppa) { struct io *io = humppa; size_t w; ssize_t n; int saved_errno; io_frame_enter("io_dispatch", io, ev); if (ev == EV_TIMEOUT) { io_callback(io, IO_TIMEOUT); goto leave; } if (ev & EV_WRITE && (w = io_queued(io))) { if ((n = iobuf_write(&io->iobuf, io->sock)) < 0) { if (n == IOBUF_WANT_WRITE) /* kqueue bug? */ goto read; if (n == IOBUF_CLOSED) io_callback(io, IO_DISCONNECTED); else { saved_errno = errno; io->error = strerror(errno); errno = saved_errno; io_callback(io, IO_ERROR); } goto leave; } if (w > io->lowat && w - n <= io->lowat) io_callback(io, IO_LOWAT); } read: if (ev & EV_READ) { iobuf_normalize(&io->iobuf); if ((n = iobuf_read(&io->iobuf, io->sock)) < 0) { if (n == IOBUF_CLOSED) io_callback(io, IO_DISCONNECTED); else { saved_errno = errno; io->error = strerror(errno); errno = saved_errno; io_callback(io, IO_ERROR); } goto leave; } if (n) io_callback(io, IO_DATAIN); } leave: io_frame_leave(io); } void io_callback(struct io *io, int evt) { io->cb(io, evt, io->arg); } int io_connect(struct io *io, const struct sockaddr *sa, const struct sockaddr *bsa) { int sock, errno_save; if ((sock = socket(sa->sa_family, SOCK_STREAM, 0)) == -1) goto fail; io_set_nonblocking(sock); io_set_nolinger(sock); if (bsa && bind(sock, bsa, SA_LEN(bsa)) == -1) goto fail; if (connect(sock, sa, SA_LEN(sa)) == -1) if (errno != EINPROGRESS) goto fail; io->sock = sock; io_reset(io, EV_WRITE, io_dispatch_connect); return (sock); fail: if (sock != -1) { errno_save = errno; close(sock); errno = errno_save; io->error = strerror(errno); } return (-1); } void io_dispatch_connect(int fd, short ev, void *humppa) { struct io *io = humppa; int r, e; socklen_t sl; io_frame_enter("io_dispatch_connect", io, ev); if (ev == EV_TIMEOUT) { close(fd); io->sock = -1; io_callback(io, IO_TIMEOUT); } else { sl = sizeof(e); r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &e, &sl); if (r == -1) { warn("io_dispatch_connect: getsockopt"); e = errno; } if (e) { close(fd); io->sock = -1; io->error = strerror(e); io_callback(io, e == ETIMEDOUT ? IO_TIMEOUT : IO_ERROR); } else { io->state = IO_STATE_UP; io_callback(io, IO_CONNECTED); } } io_frame_leave(io); } #ifdef IO_TLS static const char* io_tls_error(void) { static char buf[128]; unsigned long e; e = ERR_peek_last_error(); if (e) { ERR_error_string(e, buf); return (buf); } return ("No TLS error"); } int io_start_tls(struct io *io, void *tls) { int mode; mode = io->flags & IO_RW; if (mode == 0 || mode == IO_RW) errx(1, "io_start_tls(): full-duplex or unset"); if (io->tls) errx(1, "io_start_tls(): TLS already started"); io->tls = tls; if (SSL_set_fd(io->tls, io->sock) == 0) { ssl_error("io_start_tls:SSL_set_fd"); return (-1); } if (mode == IO_WRITE) { io->state = IO_STATE_CONNECT_TLS; SSL_set_connect_state(io->tls); io_reset(io, EV_WRITE, io_dispatch_connect_tls); } else { io->state = IO_STATE_ACCEPT_TLS; SSL_set_accept_state(io->tls); io_reset(io, EV_READ, io_dispatch_accept_tls); } return (0); } void io_dispatch_accept_tls(int fd, short event, void *humppa) { struct io *io = humppa; int e, ret; io_frame_enter("io_dispatch_accept_tls", io, event); if (event == EV_TIMEOUT) { io_callback(io, IO_TIMEOUT); goto leave; } if ((ret = SSL_accept(io->tls)) > 0) { io->state = IO_STATE_UP; io_callback(io, IO_TLSREADY); goto leave; } switch ((e = SSL_get_error(io->tls, ret))) { case SSL_ERROR_WANT_READ: io_reset(io, EV_READ, io_dispatch_accept_tls); break; case SSL_ERROR_WANT_WRITE: io_reset(io, EV_WRITE, io_dispatch_accept_tls); break; default: io->error = io_tls_error(); ssl_error("io_dispatch_accept_tls:SSL_accept"); io_callback(io, IO_ERROR); break; } leave: io_frame_leave(io); } void io_dispatch_connect_tls(int fd, short event, void *humppa) { struct io *io = humppa; int e, ret; io_frame_enter("io_dispatch_connect_tls", io, event); if (event == EV_TIMEOUT) { io_callback(io, IO_TIMEOUT); goto leave; } if ((ret = SSL_connect(io->tls)) > 0) { io->state = IO_STATE_UP; io_callback(io, IO_TLSREADY); goto leave; } switch ((e = SSL_get_error(io->tls, ret))) { case SSL_ERROR_WANT_READ: io_reset(io, EV_READ, io_dispatch_connect_tls); break; case SSL_ERROR_WANT_WRITE: io_reset(io, EV_WRITE, io_dispatch_connect_tls); break; default: io->error = io_tls_error(); ssl_error("io_dispatch_connect_ssl:SSL_connect"); io_callback(io, IO_TLSERROR); break; } leave: io_frame_leave(io); } void io_dispatch_read_tls(int fd, short event, void *humppa) { struct io *io = humppa; int n, saved_errno; io_frame_enter("io_dispatch_read_tls", io, event); if (event == EV_TIMEOUT) { io_callback(io, IO_TIMEOUT); goto leave; } again: iobuf_normalize(&io->iobuf); switch ((n = iobuf_read_tls(&io->iobuf, (SSL*)io->tls))) { case IOBUF_WANT_READ: io_reset(io, EV_READ, io_dispatch_read_tls); break; case IOBUF_WANT_WRITE: io_reset(io, EV_WRITE, io_dispatch_read_tls); break; case IOBUF_CLOSED: io_callback(io, IO_DISCONNECTED); break; case IOBUF_ERROR: saved_errno = errno; io->error = strerror(errno); errno = saved_errno; io_callback(io, IO_ERROR); break; case IOBUF_TLSERROR: io->error = io_tls_error(); ssl_error("io_dispatch_read_tls:SSL_read"); io_callback(io, IO_ERROR); break; default: io_debug("io_dispatch_read_tls(...) -> r=%d\n", n); io_callback(io, IO_DATAIN); if (current == io && IO_READING(io) && SSL_pending(io->tls)) goto again; } leave: io_frame_leave(io); } void io_dispatch_write_tls(int fd, short event, void *humppa) { struct io *io = humppa; int n, saved_errno; size_t w2, w; io_frame_enter("io_dispatch_write_tls", io, event); if (event == EV_TIMEOUT) { io_callback(io, IO_TIMEOUT); goto leave; } w = io_queued(io); switch ((n = iobuf_write_tls(&io->iobuf, (SSL*)io->tls))) { case IOBUF_WANT_READ: io_reset(io, EV_READ, io_dispatch_write_tls); break; case IOBUF_WANT_WRITE: io_reset(io, EV_WRITE, io_dispatch_write_tls); break; case IOBUF_CLOSED: io_callback(io, IO_DISCONNECTED); break; case IOBUF_ERROR: saved_errno = errno; io->error = strerror(errno); errno = saved_errno; io_callback(io, IO_ERROR); break; case IOBUF_TLSERROR: io->error = io_tls_error(); ssl_error("io_dispatch_write_tls:SSL_write"); io_callback(io, IO_ERROR); break; default: io_debug("io_dispatch_write_tls(...) -> w=%d\n", n); w2 = io_queued(io); if (w > io->lowat && w2 <= io->lowat) io_callback(io, IO_LOWAT); break; } leave: io_frame_leave(io); } void io_reload_tls(struct io *io) { short ev = 0; void (*dispatch)(int, short, void*) = NULL; switch (io->state) { case IO_STATE_CONNECT_TLS: ev = EV_WRITE; dispatch = io_dispatch_connect_tls; break; case IO_STATE_ACCEPT_TLS: ev = EV_READ; dispatch = io_dispatch_accept_tls; break; case IO_STATE_UP: ev = 0; if (IO_READING(io) && !(io->flags & IO_PAUSE_IN)) { ev = EV_READ; dispatch = io_dispatch_read_tls; } else if (IO_WRITING(io) && !(io->flags & IO_PAUSE_OUT) && io_queued(io)) { ev = EV_WRITE; dispatch = io_dispatch_write_tls; } if (!ev) return; /* paused */ break; default: errx(1, "io_reload_tls(): bad state"); } io_reset(io, ev, dispatch); } #endif /* IO_TLS */ libopensmtpd-0.7/ioev.h000066400000000000000000000053141405566102200151770ustar00rootroot00000000000000/* $OpenBSD: ioev.h,v 1.17 2019/06/12 17:42:53 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot * * 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. */ enum { IO_CONNECTED = 0, /* connection successful */ IO_TLSREADY, /* TLS started successfully */ IO_TLSERROR, /* XXX - needs more work */ IO_DATAIN, /* new data in input buffer */ IO_LOWAT, /* output queue running low */ IO_DISCONNECTED, /* error? */ IO_TIMEOUT, /* error? */ IO_ERROR, /* details? */ }; #define IO_IN 0x01 #define IO_OUT 0x02 /* From OpenNTPD-ish portable */ #if !defined(SA_LEN) #define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \ sizeof(struct sockaddr_in6) : \ sizeof(struct sockaddr_in)) #endif #ifndef __unused #define __unused __attribute__((unused)) #endif struct io; void io_set_nonblocking(int); void io_set_nolinger(int); struct io *io_new(void); void io_free(struct io *); void io_set_read(struct io *); void io_set_write(struct io *); void io_set_fd(struct io *, int); void io_set_callback(struct io *io, void(*)(struct io *, int, void *), void *); void io_set_timeout(struct io *, int); void io_set_lowat(struct io *, size_t); void io_pause(struct io *, int); void io_resume(struct io *, int); void io_reload(struct io *); int io_connect(struct io *, const struct sockaddr *, const struct sockaddr *); int io_start_tls(struct io *, void *); const char* io_strio(struct io *); const char* io_strevent(int); const char* io_error(struct io *); void* io_tls(struct io *); int io_fileno(struct io *); int io_paused(struct io *, int); /* Buffered output functions */ int io_write(struct io *, const void *, size_t); int io_writev(struct io *, const struct iovec *, int); int io_print(struct io *, const char *); int io_printf(struct io *, const char *, ...); int io_vprintf(struct io *, const char *, va_list); size_t io_queued(struct io *); /* Buffered input functions */ void* io_data(struct io *); size_t io_datalen(struct io *); char* io_getline(struct io *, size_t *); void io_drop(struct io *, size_t); libopensmtpd-0.7/openbsd-compat/000077500000000000000000000000001405566102200167745ustar00rootroot00000000000000libopensmtpd-0.7/openbsd-compat/explicit_bzero.c000066400000000000000000000003731405566102200221650ustar00rootroot00000000000000/* $OpenBSD: explicit_bzero.c,v 1.4 2015/08/31 02:53:57 guenther Exp $ */ /* * Public domain. * Written by Matthew Dempsky. */ #include "openbsd-compat.h" #include void explicit_bzero(void *buf, size_t len) { memset(buf, 0, len); } libopensmtpd-0.7/openbsd-compat/openbsd-compat.h000066400000000000000000000037771405566102200220760ustar00rootroot00000000000000/* $Id: openbsd-compat.h,v 1.51 2010/10/07 10:25:29 djm Exp $ */ /* * Copyright (c) 1999-2003 Damien Miller. All rights reserved. * Copyright (c) 2003 Ben Lindstrom. All rights reserved. * Copyright (c) 2002 Tim Rice. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #ifdef NEED_EXPLICIT_BZERO void explicit_bzero(void *p, size_t n); #endif #ifdef NEED_RECALLOCARRAY void *recallocarray(void *, size_t, size_t, size_t); #endif #ifdef NEED_REALLOCARRAY void *reallocarray(void *, size_t, size_t); #endif #ifdef NEED_STRLCAT size_t strlcat(char *dst, const char *src, size_t size); #endif #ifdef NEED_STRLCPY size_t strlcpy(char *dst, const char *src, size_t size); #endif #ifdef NEED_STRTONUM long long strtonum(const char *nptr, long long minval, long long maxval, const char **errstr); #endif libopensmtpd-0.7/openbsd-compat/reallocarray.c000066400000000000000000000026411405566102200216230ustar00rootroot00000000000000/* $OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $ */ /* * Copyright (c) 2008 Otto Moerbeek * * 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. */ /* OPENBSD ORIGINAL: lib/libc/stdlib/reallocarray.c */ #include "openbsd-compat.h" #include #include #include #include /* * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW */ #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) void * reallocarray(void *optr, size_t nmemb, size_t size) { if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && nmemb > 0 && SIZE_MAX / nmemb < size) { errno = ENOMEM; return NULL; } return realloc(optr, size * nmemb); } libopensmtpd-0.7/openbsd-compat/recallocarray.c000066400000000000000000000044531405566102200217710ustar00rootroot00000000000000/* $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $ */ /* * Copyright (c) 2008, 2017 Otto Moerbeek * * 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. */ /* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */ #include #include #include #include #include #include "openbsd-compat.h" /* * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW */ #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) void * recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size) { size_t oldsize, newsize; void *newptr; if (ptr == NULL) return calloc(newnmemb, size); if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && newnmemb > 0 && SIZE_MAX / newnmemb < size) { errno = ENOMEM; return NULL; } newsize = newnmemb * size; if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && oldnmemb > 0 && SIZE_MAX / oldnmemb < size) { errno = EINVAL; return NULL; } oldsize = oldnmemb * size; /* * Don't bother too much if we're shrinking just a bit, * we do not shrink for series of small steps, oh well. */ if (newsize <= oldsize) { size_t d = oldsize - newsize; if (d < oldsize / 2 && d < (size_t)getpagesize()) { memset((char *)ptr + newsize, 0, d); return ptr; } } newptr = malloc(newsize); if (newptr == NULL) return NULL; if (newsize > oldsize) { memcpy(newptr, ptr, oldsize); memset((char *)newptr + oldsize, 0, newsize - oldsize); } else memcpy(newptr, ptr, newsize); explicit_bzero(ptr, oldsize); free(ptr); return newptr; } libopensmtpd-0.7/openbsd-compat/strlcat.c000066400000000000000000000033431405566102200206170ustar00rootroot00000000000000/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ /* * Copyright (c) 1998 Todd C. Miller * * 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. */ /* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */ #include "openbsd-compat.h" #include #include /* * Appends src to string dst of size siz (unlike strncat, siz is the * full size of dst, not space left). At most siz-1 characters * will be copied. Always NUL terminates (unless siz <= strlen(dst)). * Returns strlen(src) + MIN(siz, strlen(initial dst)). * If retval >= siz, truncation occurred. */ size_t strlcat(char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; size_t dlen; /* Find the end of dst and adjust bytes left but don't go past end */ while (n-- != 0 && *d != '\0') d++; dlen = d - dst; n = siz - dlen; if (n == 0) return(dlen + strlen(s)); while (*s != '\0') { if (n != 1) { *d++ = *s; n--; } s++; } *d = '\0'; return(dlen + (s - src)); /* count does not include NUL */ } libopensmtpd-0.7/openbsd-compat/strlcpy.c000066400000000000000000000031511405566102200206400ustar00rootroot00000000000000/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ /* * Copyright (c) 1998 Todd C. Miller * * 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. */ /* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */ #include "openbsd-compat.h" #include #include /* * Copy src to string dst of size siz. At most siz-1 characters * will be copied. Always NUL terminates (unless siz == 0). * Returns strlen(src); if retval >= siz, truncation occurred. */ size_t strlcpy(char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; /* Copy as many bytes as will fit */ if (n != 0) { while (--n != 0) { if ((*d++ = *s++) == '\0') break; } } /* Not enough room in dst, add NUL and traverse rest of src */ if (n == 0) { if (siz != 0) *d = '\0'; /* NUL-terminate dst */ while (*s++) ; } return(s - src - 1); /* count does not include NUL */ } libopensmtpd-0.7/openbsd-compat/strtonum.c000066400000000000000000000035541405566102200210420ustar00rootroot00000000000000/* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */ /* * Copyright (c) 2004 Ted Unangst and Todd Miller * All rights reserved. * * 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. */ /* OPENBSD ORIGINAL: lib/libc/stdlib/strtonum.c */ #include "openbsd-compat.h" #ifndef HAVE_STRTONUM #include #include #include #define INVALID 1 #define TOOSMALL 2 #define TOOLARGE 3 long long strtonum(const char *numstr, long long minval, long long maxval, const char **errstrp) { long long ll = 0; char *ep; int error = 0; struct errval { const char *errstr; int err; } ev[4] = { { NULL, 0 }, { "invalid", EINVAL }, { "too small", ERANGE }, { "too large", ERANGE }, }; ev[0].err = errno; errno = 0; if (minval > maxval) error = INVALID; else { ll = strtoll(numstr, &ep, 10); if (numstr == ep || *ep != '\0') error = INVALID; else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) error = TOOSMALL; else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) error = TOOLARGE; } if (errstrp != NULL) *errstrp = ev[error].errstr; errno = ev[error].err; if (error) ll = 0; return (ll); } #endif /* HAVE_STRTONUM */ libopensmtpd-0.7/openbsd-compat/sys/000077500000000000000000000000001405566102200176125ustar00rootroot00000000000000libopensmtpd-0.7/openbsd-compat/sys/queue.h000066400000000000000000000516361405566102200211220ustar00rootroot00000000000000/* $OpenBSD: queue.h,v 1.36 2012/04/11 13:29:14 naddy Exp $ */ /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */ /* * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)queue.h 8.5 (Berkeley) 8/20/94 */ /* OPENBSD ORIGINAL: sys/sys/queue.h */ #ifndef _FAKE_QUEUE_H_ #define _FAKE_QUEUE_H_ /* * Require for OS/X and other platforms that have old/broken/incomplete * . */ #undef SLIST_HEAD #undef SLIST_HEAD_INITIALIZER #undef SLIST_ENTRY #undef SLIST_FOREACH_PREVPTR #undef SLIST_FIRST #undef SLIST_END #undef SLIST_EMPTY #undef SLIST_NEXT #undef SLIST_FOREACH #undef SLIST_INIT #undef SLIST_INSERT_AFTER #undef SLIST_INSERT_HEAD #undef SLIST_REMOVE_HEAD #undef SLIST_REMOVE #undef SLIST_REMOVE_NEXT #undef LIST_HEAD #undef LIST_HEAD_INITIALIZER #undef LIST_ENTRY #undef LIST_FIRST #undef LIST_END #undef LIST_EMPTY #undef LIST_NEXT #undef LIST_FOREACH #undef LIST_INIT #undef LIST_INSERT_AFTER #undef LIST_INSERT_BEFORE #undef LIST_INSERT_HEAD #undef LIST_REMOVE #undef LIST_REPLACE #undef SIMPLEQ_HEAD #undef SIMPLEQ_HEAD_INITIALIZER #undef SIMPLEQ_ENTRY #undef SIMPLEQ_FIRST #undef SIMPLEQ_END #undef SIMPLEQ_EMPTY #undef SIMPLEQ_NEXT #undef SIMPLEQ_FOREACH #undef SIMPLEQ_INIT #undef SIMPLEQ_INSERT_HEAD #undef SIMPLEQ_INSERT_TAIL #undef SIMPLEQ_INSERT_AFTER #undef SIMPLEQ_REMOVE_HEAD #undef TAILQ_HEAD #undef TAILQ_HEAD_INITIALIZER #undef TAILQ_ENTRY #undef TAILQ_FIRST #undef TAILQ_END #undef TAILQ_NEXT #undef TAILQ_LAST #undef TAILQ_PREV #undef TAILQ_EMPTY #undef TAILQ_FOREACH #undef TAILQ_FOREACH_REVERSE #undef TAILQ_INIT #undef TAILQ_INSERT_HEAD #undef TAILQ_INSERT_TAIL #undef TAILQ_INSERT_AFTER #undef TAILQ_INSERT_BEFORE #undef TAILQ_REMOVE #undef TAILQ_REPLACE #undef CIRCLEQ_HEAD #undef CIRCLEQ_HEAD_INITIALIZER #undef CIRCLEQ_ENTRY #undef CIRCLEQ_FIRST #undef CIRCLEQ_LAST #undef CIRCLEQ_END #undef CIRCLEQ_NEXT #undef CIRCLEQ_PREV #undef CIRCLEQ_EMPTY #undef CIRCLEQ_FOREACH #undef CIRCLEQ_FOREACH_REVERSE #undef CIRCLEQ_INIT #undef CIRCLEQ_INSERT_AFTER #undef CIRCLEQ_INSERT_BEFORE #undef CIRCLEQ_INSERT_HEAD #undef CIRCLEQ_INSERT_TAIL #undef CIRCLEQ_REMOVE #undef CIRCLEQ_REPLACE /* * This file defines five types of data structures: singly-linked lists, * lists, simple queues, tail queues, and circular queues. * * * A singly-linked list is headed by a single forward pointer. The elements * are singly linked for minimum space and pointer manipulation overhead at * the expense of O(n) removal for arbitrary elements. New elements can be * added to the list after an existing element or at the head of the list. * Elements being removed from the head of the list should use the explicit * macro for this purpose for optimum efficiency. A singly-linked list may * only be traversed in the forward direction. Singly-linked lists are ideal * for applications with large datasets and few or no removals or for * implementing a LIFO queue. * * A list is headed by a single forward pointer (or an array of forward * pointers for a hash table header). The elements are doubly linked * so that an arbitrary element can be removed without a need to * traverse the list. New elements can be added to the list before * or after an existing element or at the head of the list. A list * may only be traversed in the forward direction. * * A simple queue is headed by a pair of pointers, one the head of the * list and the other to the tail of the list. The elements are singly * linked to save space, so elements can only be removed from the * head of the list. New elements can be added to the list before or after * an existing element, at the head of the list, or at the end of the * list. A simple queue may only be traversed in the forward direction. * * A tail queue is headed by a pair of pointers, one to the head of the * list and the other to the tail of the list. The elements are doubly * linked so that an arbitrary element can be removed without a need to * traverse the list. New elements can be added to the list before or * after an existing element, at the head of the list, or at the end of * the list. A tail queue may be traversed in either direction. * * A circle queue is headed by a pair of pointers, one to the head of the * list and the other to the tail of the list. The elements are doubly * linked so that an arbitrary element can be removed without a need to * traverse the list. New elements can be added to the list before or after * an existing element, at the head of the list, or at the end of the list. * A circle queue may be traversed in either direction, but has a more * complex end of list detection. * * For details on the use of these macros, see the queue(3) manual page. */ #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC)) #define _Q_INVALIDATE(a) (a) = ((void *)-1) #else #define _Q_INVALIDATE(a) #endif /* * Singly-linked List definitions. */ #define SLIST_HEAD(name, type) \ struct name { \ struct type *slh_first; /* first element */ \ } #define SLIST_HEAD_INITIALIZER(head) \ { NULL } #define SLIST_ENTRY(type) \ struct { \ struct type *sle_next; /* next element */ \ } /* * Singly-linked List access methods. */ #define SLIST_FIRST(head) ((head)->slh_first) #define SLIST_END(head) NULL #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head)) #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) #define SLIST_FOREACH(var, head, field) \ for((var) = SLIST_FIRST(head); \ (var) != SLIST_END(head); \ (var) = SLIST_NEXT(var, field)) #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ for ((var) = SLIST_FIRST(head); \ (var) && ((tvar) = SLIST_NEXT(var, field), 1); \ (var) = (tvar)) /* * Singly-linked List functions. */ #define SLIST_INIT(head) { \ SLIST_FIRST(head) = SLIST_END(head); \ } #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ (elm)->field.sle_next = (slistelm)->field.sle_next; \ (slistelm)->field.sle_next = (elm); \ } while (0) #define SLIST_INSERT_HEAD(head, elm, field) do { \ (elm)->field.sle_next = (head)->slh_first; \ (head)->slh_first = (elm); \ } while (0) #define SLIST_REMOVE_AFTER(elm, field) do { \ (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \ } while (0) #define SLIST_REMOVE_HEAD(head, field) do { \ (head)->slh_first = (head)->slh_first->field.sle_next; \ } while (0) #define SLIST_REMOVE(head, elm, type, field) do { \ if ((head)->slh_first == (elm)) { \ SLIST_REMOVE_HEAD((head), field); \ } else { \ struct type *curelm = (head)->slh_first; \ \ while (curelm->field.sle_next != (elm)) \ curelm = curelm->field.sle_next; \ curelm->field.sle_next = \ curelm->field.sle_next->field.sle_next; \ _Q_INVALIDATE((elm)->field.sle_next); \ } \ } while (0) /* * List definitions. */ #define LIST_HEAD(name, type) \ struct name { \ struct type *lh_first; /* first element */ \ } #define LIST_HEAD_INITIALIZER(head) \ { NULL } #define LIST_ENTRY(type) \ struct { \ struct type *le_next; /* next element */ \ struct type **le_prev; /* address of previous next element */ \ } /* * List access methods */ #define LIST_FIRST(head) ((head)->lh_first) #define LIST_END(head) NULL #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head)) #define LIST_NEXT(elm, field) ((elm)->field.le_next) #define LIST_FOREACH(var, head, field) \ for((var) = LIST_FIRST(head); \ (var)!= LIST_END(head); \ (var) = LIST_NEXT(var, field)) #define LIST_FOREACH_SAFE(var, head, field, tvar) \ for ((var) = LIST_FIRST(head); \ (var) && ((tvar) = LIST_NEXT(var, field), 1); \ (var) = (tvar)) /* * List functions. */ #define LIST_INIT(head) do { \ LIST_FIRST(head) = LIST_END(head); \ } while (0) #define LIST_INSERT_AFTER(listelm, elm, field) do { \ if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ (listelm)->field.le_next->field.le_prev = \ &(elm)->field.le_next; \ (listelm)->field.le_next = (elm); \ (elm)->field.le_prev = &(listelm)->field.le_next; \ } while (0) #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ (elm)->field.le_prev = (listelm)->field.le_prev; \ (elm)->field.le_next = (listelm); \ *(listelm)->field.le_prev = (elm); \ (listelm)->field.le_prev = &(elm)->field.le_next; \ } while (0) #define LIST_INSERT_HEAD(head, elm, field) do { \ if (((elm)->field.le_next = (head)->lh_first) != NULL) \ (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ (head)->lh_first = (elm); \ (elm)->field.le_prev = &(head)->lh_first; \ } while (0) #define LIST_REMOVE(elm, field) do { \ if ((elm)->field.le_next != NULL) \ (elm)->field.le_next->field.le_prev = \ (elm)->field.le_prev; \ *(elm)->field.le_prev = (elm)->field.le_next; \ _Q_INVALIDATE((elm)->field.le_prev); \ _Q_INVALIDATE((elm)->field.le_next); \ } while (0) #define LIST_REPLACE(elm, elm2, field) do { \ if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ (elm2)->field.le_next->field.le_prev = \ &(elm2)->field.le_next; \ (elm2)->field.le_prev = (elm)->field.le_prev; \ *(elm2)->field.le_prev = (elm2); \ _Q_INVALIDATE((elm)->field.le_prev); \ _Q_INVALIDATE((elm)->field.le_next); \ } while (0) /* * Simple queue definitions. */ #define SIMPLEQ_HEAD(name, type) \ struct name { \ struct type *sqh_first; /* first element */ \ struct type **sqh_last; /* addr of last next element */ \ } #define SIMPLEQ_HEAD_INITIALIZER(head) \ { NULL, &(head).sqh_first } #define SIMPLEQ_ENTRY(type) \ struct { \ struct type *sqe_next; /* next element */ \ } /* * Simple queue access methods. */ #define SIMPLEQ_FIRST(head) ((head)->sqh_first) #define SIMPLEQ_END(head) NULL #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head)) #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) #define SIMPLEQ_FOREACH(var, head, field) \ for((var) = SIMPLEQ_FIRST(head); \ (var) != SIMPLEQ_END(head); \ (var) = SIMPLEQ_NEXT(var, field)) #define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ for ((var) = SIMPLEQ_FIRST(head); \ (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \ (var) = (tvar)) /* * Simple queue functions. */ #define SIMPLEQ_INIT(head) do { \ (head)->sqh_first = NULL; \ (head)->sqh_last = &(head)->sqh_first; \ } while (0) #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \ if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ (head)->sqh_last = &(elm)->field.sqe_next; \ (head)->sqh_first = (elm); \ } while (0) #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \ (elm)->field.sqe_next = NULL; \ *(head)->sqh_last = (elm); \ (head)->sqh_last = &(elm)->field.sqe_next; \ } while (0) #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\ (head)->sqh_last = &(elm)->field.sqe_next; \ (listelm)->field.sqe_next = (elm); \ } while (0) #define SIMPLEQ_REMOVE_HEAD(head, field) do { \ if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \ (head)->sqh_last = &(head)->sqh_first; \ } while (0) #define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \ if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \ == NULL) \ (head)->sqh_last = &(elm)->field.sqe_next; \ } while (0) /* * Tail queue definitions. */ #define TAILQ_HEAD(name, type) \ struct name { \ struct type *tqh_first; /* first element */ \ struct type **tqh_last; /* addr of last next element */ \ } #define TAILQ_HEAD_INITIALIZER(head) \ { NULL, &(head).tqh_first } #define TAILQ_ENTRY(type) \ struct { \ struct type *tqe_next; /* next element */ \ struct type **tqe_prev; /* address of previous next element */ \ } /* * tail queue access methods */ #define TAILQ_FIRST(head) ((head)->tqh_first) #define TAILQ_END(head) NULL #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) #define TAILQ_LAST(head, headname) \ (*(((struct headname *)((head)->tqh_last))->tqh_last)) /* XXX */ #define TAILQ_PREV(elm, headname, field) \ (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) #define TAILQ_EMPTY(head) \ (TAILQ_FIRST(head) == TAILQ_END(head)) #define TAILQ_FOREACH(var, head, field) \ for((var) = TAILQ_FIRST(head); \ (var) != TAILQ_END(head); \ (var) = TAILQ_NEXT(var, field)) #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ for ((var) = TAILQ_FIRST(head); \ (var) != TAILQ_END(head) && \ ((tvar) = TAILQ_NEXT(var, field), 1); \ (var) = (tvar)) #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ for((var) = TAILQ_LAST(head, headname); \ (var) != TAILQ_END(head); \ (var) = TAILQ_PREV(var, headname, field)) #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ for ((var) = TAILQ_LAST(head, headname); \ (var) != TAILQ_END(head) && \ ((tvar) = TAILQ_PREV(var, headname, field), 1); \ (var) = (tvar)) /* * Tail queue functions. */ #define TAILQ_INIT(head) do { \ (head)->tqh_first = NULL; \ (head)->tqh_last = &(head)->tqh_first; \ } while (0) #define TAILQ_INSERT_HEAD(head, elm, field) do { \ if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ (head)->tqh_first->field.tqe_prev = \ &(elm)->field.tqe_next; \ else \ (head)->tqh_last = &(elm)->field.tqe_next; \ (head)->tqh_first = (elm); \ (elm)->field.tqe_prev = &(head)->tqh_first; \ } while (0) #define TAILQ_INSERT_TAIL(head, elm, field) do { \ (elm)->field.tqe_next = NULL; \ (elm)->field.tqe_prev = (head)->tqh_last; \ *(head)->tqh_last = (elm); \ (head)->tqh_last = &(elm)->field.tqe_next; \ } while (0) #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ (elm)->field.tqe_next->field.tqe_prev = \ &(elm)->field.tqe_next; \ else \ (head)->tqh_last = &(elm)->field.tqe_next; \ (listelm)->field.tqe_next = (elm); \ (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ } while (0) #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ (elm)->field.tqe_next = (listelm); \ *(listelm)->field.tqe_prev = (elm); \ (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ } while (0) #define TAILQ_REMOVE(head, elm, field) do { \ if (((elm)->field.tqe_next) != NULL) \ (elm)->field.tqe_next->field.tqe_prev = \ (elm)->field.tqe_prev; \ else \ (head)->tqh_last = (elm)->field.tqe_prev; \ *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ _Q_INVALIDATE((elm)->field.tqe_prev); \ _Q_INVALIDATE((elm)->field.tqe_next); \ } while (0) #define TAILQ_REPLACE(head, elm, elm2, field) do { \ if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \ (elm2)->field.tqe_next->field.tqe_prev = \ &(elm2)->field.tqe_next; \ else \ (head)->tqh_last = &(elm2)->field.tqe_next; \ (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ *(elm2)->field.tqe_prev = (elm2); \ _Q_INVALIDATE((elm)->field.tqe_prev); \ _Q_INVALIDATE((elm)->field.tqe_next); \ } while (0) /* * Circular queue definitions. */ #define CIRCLEQ_HEAD(name, type) \ struct name { \ struct type *cqh_first; /* first element */ \ struct type *cqh_last; /* last element */ \ } #define CIRCLEQ_HEAD_INITIALIZER(head) \ { CIRCLEQ_END(&head), CIRCLEQ_END(&head) } #define CIRCLEQ_ENTRY(type) \ struct { \ struct type *cqe_next; /* next element */ \ struct type *cqe_prev; /* previous element */ \ } /* * Circular queue access methods */ #define CIRCLEQ_FIRST(head) ((head)->cqh_first) #define CIRCLEQ_LAST(head) ((head)->cqh_last) #define CIRCLEQ_END(head) ((void *)(head)) #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) #define CIRCLEQ_EMPTY(head) \ (CIRCLEQ_FIRST(head) == CIRCLEQ_END(head)) #define CIRCLEQ_FOREACH(var, head, field) \ for((var) = CIRCLEQ_FIRST(head); \ (var) != CIRCLEQ_END(head); \ (var) = CIRCLEQ_NEXT(var, field)) #define CIRCLEQ_FOREACH_SAFE(var, head, field, tvar) \ for ((var) = CIRCLEQ_FIRST(head); \ (var) != CIRCLEQ_END(head) && \ ((tvar) = CIRCLEQ_NEXT(var, field), 1); \ (var) = (tvar)) #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ for((var) = CIRCLEQ_LAST(head); \ (var) != CIRCLEQ_END(head); \ (var) = CIRCLEQ_PREV(var, field)) #define CIRCLEQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ for ((var) = CIRCLEQ_LAST(head, headname); \ (var) != CIRCLEQ_END(head) && \ ((tvar) = CIRCLEQ_PREV(var, headname, field), 1); \ (var) = (tvar)) /* * Circular queue functions. */ #define CIRCLEQ_INIT(head) do { \ (head)->cqh_first = CIRCLEQ_END(head); \ (head)->cqh_last = CIRCLEQ_END(head); \ } while (0) #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ (elm)->field.cqe_next = (listelm)->field.cqe_next; \ (elm)->field.cqe_prev = (listelm); \ if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \ (head)->cqh_last = (elm); \ else \ (listelm)->field.cqe_next->field.cqe_prev = (elm); \ (listelm)->field.cqe_next = (elm); \ } while (0) #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ (elm)->field.cqe_next = (listelm); \ (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \ (head)->cqh_first = (elm); \ else \ (listelm)->field.cqe_prev->field.cqe_next = (elm); \ (listelm)->field.cqe_prev = (elm); \ } while (0) #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ (elm)->field.cqe_next = (head)->cqh_first; \ (elm)->field.cqe_prev = CIRCLEQ_END(head); \ if ((head)->cqh_last == CIRCLEQ_END(head)) \ (head)->cqh_last = (elm); \ else \ (head)->cqh_first->field.cqe_prev = (elm); \ (head)->cqh_first = (elm); \ } while (0) #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ (elm)->field.cqe_next = CIRCLEQ_END(head); \ (elm)->field.cqe_prev = (head)->cqh_last; \ if ((head)->cqh_first == CIRCLEQ_END(head)) \ (head)->cqh_first = (elm); \ else \ (head)->cqh_last->field.cqe_next = (elm); \ (head)->cqh_last = (elm); \ } while (0) #define CIRCLEQ_REMOVE(head, elm, field) do { \ if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \ (head)->cqh_last = (elm)->field.cqe_prev; \ else \ (elm)->field.cqe_next->field.cqe_prev = \ (elm)->field.cqe_prev; \ if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \ (head)->cqh_first = (elm)->field.cqe_next; \ else \ (elm)->field.cqe_prev->field.cqe_next = \ (elm)->field.cqe_next; \ _Q_INVALIDATE((elm)->field.cqe_prev); \ _Q_INVALIDATE((elm)->field.cqe_next); \ } while (0) #define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \ if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \ CIRCLEQ_END(head)) \ (head).cqh_last = (elm2); \ else \ (elm2)->field.cqe_next->field.cqe_prev = (elm2); \ if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \ CIRCLEQ_END(head)) \ (head).cqh_first = (elm2); \ else \ (elm2)->field.cqe_prev->field.cqe_next = (elm2); \ _Q_INVALIDATE((elm)->field.cqe_prev); \ _Q_INVALIDATE((elm)->field.cqe_next); \ } while (0) #endif /* !_FAKE_QUEUE_H_ */ libopensmtpd-0.7/openbsd-compat/sys/tree.h000066400000000000000000001021471405566102200207270ustar00rootroot00000000000000/* $OpenBSD: tree.h,v 1.29 2017/07/30 19:27:20 deraadt Exp $ */ /* * Copyright 2002 Niels Provos * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef _SYS_TREE_H_ #define _SYS_TREE_H_ #include /* * This file defines data structures for different types of trees: * splay trees and red-black trees. * * A splay tree is a self-organizing data structure. Every operation * on the tree causes a splay to happen. The splay moves the requested * node to the root of the tree and partly rebalances it. * * This has the benefit that request locality causes faster lookups as * the requested nodes move to the top of the tree. On the other hand, * every lookup causes memory writes. * * The Balance Theorem bounds the total access time for m operations * and n inserts on an initially empty tree as O((m + n)lg n). The * amortized cost for a sequence of m accesses to a splay tree is O(lg n); * * A red-black tree is a binary search tree with the node color as an * extra attribute. It fulfills a set of conditions: * - every search path from the root to a leaf consists of the * same number of black nodes, * - each red node (except for the root) has a black parent, * - each leaf node is black. * * Every operation on a red-black tree is bounded as O(lg n). * The maximum height of a red-black tree is 2lg (n+1). */ #define SPLAY_HEAD(name, type) \ struct name { \ struct type *sph_root; /* root of the tree */ \ } #define SPLAY_INITIALIZER(root) \ { NULL } #define SPLAY_INIT(root) do { \ (root)->sph_root = NULL; \ } while (0) #define SPLAY_ENTRY(type) \ struct { \ struct type *spe_left; /* left element */ \ struct type *spe_right; /* right element */ \ } #define SPLAY_LEFT(elm, field) (elm)->field.spe_left #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right #define SPLAY_ROOT(head) (head)->sph_root #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL) /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */ #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \ SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \ SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ (head)->sph_root = tmp; \ } while (0) #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \ SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \ SPLAY_LEFT(tmp, field) = (head)->sph_root; \ (head)->sph_root = tmp; \ } while (0) #define SPLAY_LINKLEFT(head, tmp, field) do { \ SPLAY_LEFT(tmp, field) = (head)->sph_root; \ tmp = (head)->sph_root; \ (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \ } while (0) #define SPLAY_LINKRIGHT(head, tmp, field) do { \ SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ tmp = (head)->sph_root; \ (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \ } while (0) #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \ SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \ SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\ SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \ SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \ } while (0) /* Generates prototypes and inline functions */ #define SPLAY_PROTOTYPE(name, type, field, cmp) \ void name##_SPLAY(struct name *, struct type *); \ void name##_SPLAY_MINMAX(struct name *, int); \ struct type *name##_SPLAY_INSERT(struct name *, struct type *); \ struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \ \ /* Finds the node with the same key as elm */ \ static __unused __inline struct type * \ name##_SPLAY_FIND(struct name *head, struct type *elm) \ { \ if (SPLAY_EMPTY(head)) \ return(NULL); \ name##_SPLAY(head, elm); \ if ((cmp)(elm, (head)->sph_root) == 0) \ return (head->sph_root); \ return (NULL); \ } \ \ static __unused __inline struct type * \ name##_SPLAY_NEXT(struct name *head, struct type *elm) \ { \ name##_SPLAY(head, elm); \ if (SPLAY_RIGHT(elm, field) != NULL) { \ elm = SPLAY_RIGHT(elm, field); \ while (SPLAY_LEFT(elm, field) != NULL) { \ elm = SPLAY_LEFT(elm, field); \ } \ } else \ elm = NULL; \ return (elm); \ } \ \ static __unused __inline struct type * \ name##_SPLAY_MIN_MAX(struct name *head, int val) \ { \ name##_SPLAY_MINMAX(head, val); \ return (SPLAY_ROOT(head)); \ } /* Main splay operation. * Moves node close to the key of elm to top */ #define SPLAY_GENERATE(name, type, field, cmp) \ struct type * \ name##_SPLAY_INSERT(struct name *head, struct type *elm) \ { \ if (SPLAY_EMPTY(head)) { \ SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \ } else { \ int __comp; \ name##_SPLAY(head, elm); \ __comp = (cmp)(elm, (head)->sph_root); \ if(__comp < 0) { \ SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\ SPLAY_RIGHT(elm, field) = (head)->sph_root; \ SPLAY_LEFT((head)->sph_root, field) = NULL; \ } else if (__comp > 0) { \ SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\ SPLAY_LEFT(elm, field) = (head)->sph_root; \ SPLAY_RIGHT((head)->sph_root, field) = NULL; \ } else \ return ((head)->sph_root); \ } \ (head)->sph_root = (elm); \ return (NULL); \ } \ \ struct type * \ name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ { \ struct type *__tmp; \ if (SPLAY_EMPTY(head)) \ return (NULL); \ name##_SPLAY(head, elm); \ if ((cmp)(elm, (head)->sph_root) == 0) { \ if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \ (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\ } else { \ __tmp = SPLAY_RIGHT((head)->sph_root, field); \ (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\ name##_SPLAY(head, elm); \ SPLAY_RIGHT((head)->sph_root, field) = __tmp; \ } \ return (elm); \ } \ return (NULL); \ } \ \ void \ name##_SPLAY(struct name *head, struct type *elm) \ { \ struct type __node, *__left, *__right, *__tmp; \ int __comp; \ \ SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ __left = __right = &__node; \ \ while ((__comp = (cmp)(elm, (head)->sph_root))) { \ if (__comp < 0) { \ __tmp = SPLAY_LEFT((head)->sph_root, field); \ if (__tmp == NULL) \ break; \ if ((cmp)(elm, __tmp) < 0){ \ SPLAY_ROTATE_RIGHT(head, __tmp, field); \ if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ break; \ } \ SPLAY_LINKLEFT(head, __right, field); \ } else if (__comp > 0) { \ __tmp = SPLAY_RIGHT((head)->sph_root, field); \ if (__tmp == NULL) \ break; \ if ((cmp)(elm, __tmp) > 0){ \ SPLAY_ROTATE_LEFT(head, __tmp, field); \ if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ break; \ } \ SPLAY_LINKRIGHT(head, __left, field); \ } \ } \ SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ } \ \ /* Splay with either the minimum or the maximum element \ * Used to find minimum or maximum element in tree. \ */ \ void name##_SPLAY_MINMAX(struct name *head, int __comp) \ { \ struct type __node, *__left, *__right, *__tmp; \ \ SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ __left = __right = &__node; \ \ while (1) { \ if (__comp < 0) { \ __tmp = SPLAY_LEFT((head)->sph_root, field); \ if (__tmp == NULL) \ break; \ if (__comp < 0){ \ SPLAY_ROTATE_RIGHT(head, __tmp, field); \ if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ break; \ } \ SPLAY_LINKLEFT(head, __right, field); \ } else if (__comp > 0) { \ __tmp = SPLAY_RIGHT((head)->sph_root, field); \ if (__tmp == NULL) \ break; \ if (__comp > 0) { \ SPLAY_ROTATE_LEFT(head, __tmp, field); \ if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ break; \ } \ SPLAY_LINKRIGHT(head, __left, field); \ } \ } \ SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ } #define SPLAY_NEGINF -1 #define SPLAY_INF 1 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y) #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y) #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y) #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y) #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \ : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF)) #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \ : name##_SPLAY_MIN_MAX(x, SPLAY_INF)) #define SPLAY_FOREACH(x, name, head) \ for ((x) = SPLAY_MIN(name, head); \ (x) != NULL; \ (x) = SPLAY_NEXT(name, head, x)) /* Macros that define a red-black tree */ #define RB_HEAD(name, type) \ struct name { \ struct type *rbh_root; /* root of the tree */ \ } #define RB_INITIALIZER(root) \ { NULL } #define RB_INIT(root) do { \ (root)->rbh_root = NULL; \ } while (0) #define RB_BLACK 0 #define RB_RED 1 #define RB_ENTRY(type) \ struct { \ struct type *rbe_left; /* left element */ \ struct type *rbe_right; /* right element */ \ struct type *rbe_parent; /* parent element */ \ int rbe_color; /* node color */ \ } #define RB_LEFT(elm, field) (elm)->field.rbe_left #define RB_RIGHT(elm, field) (elm)->field.rbe_right #define RB_PARENT(elm, field) (elm)->field.rbe_parent #define RB_COLOR(elm, field) (elm)->field.rbe_color #define RB_ROOT(head) (head)->rbh_root #define RB_EMPTY(head) (RB_ROOT(head) == NULL) #define RB_SET(elm, parent, field) do { \ RB_PARENT(elm, field) = parent; \ RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \ RB_COLOR(elm, field) = RB_RED; \ } while (0) #define RB_SET_BLACKRED(black, red, field) do { \ RB_COLOR(black, field) = RB_BLACK; \ RB_COLOR(red, field) = RB_RED; \ } while (0) #ifndef RB_AUGMENT #define RB_AUGMENT(x) do {} while (0) #endif #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ (tmp) = RB_RIGHT(elm, field); \ if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \ RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \ } \ RB_AUGMENT(elm); \ if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \ if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ else \ RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ } else \ (head)->rbh_root = (tmp); \ RB_LEFT(tmp, field) = (elm); \ RB_PARENT(elm, field) = (tmp); \ RB_AUGMENT(tmp); \ if ((RB_PARENT(tmp, field))) \ RB_AUGMENT(RB_PARENT(tmp, field)); \ } while (0) #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ (tmp) = RB_LEFT(elm, field); \ if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \ RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \ } \ RB_AUGMENT(elm); \ if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \ if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ else \ RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ } else \ (head)->rbh_root = (tmp); \ RB_RIGHT(tmp, field) = (elm); \ RB_PARENT(elm, field) = (tmp); \ RB_AUGMENT(tmp); \ if ((RB_PARENT(tmp, field))) \ RB_AUGMENT(RB_PARENT(tmp, field)); \ } while (0) /* Generates prototypes and inline functions */ #define RB_PROTOTYPE(name, type, field, cmp) \ RB_PROTOTYPE_INTERNAL(name, type, field, cmp,) #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \ RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static) #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \ attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \ attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\ attr struct type *name##_RB_REMOVE(struct name *, struct type *); \ attr struct type *name##_RB_INSERT(struct name *, struct type *); \ attr struct type *name##_RB_FIND(struct name *, struct type *); \ attr struct type *name##_RB_NFIND(struct name *, struct type *); \ attr struct type *name##_RB_NEXT(struct type *); \ attr struct type *name##_RB_PREV(struct type *); \ attr struct type *name##_RB_MINMAX(struct name *, int); \ \ /* Main rb operation. * Moves node close to the key of elm to top */ #define RB_GENERATE(name, type, field, cmp) \ RB_GENERATE_INTERNAL(name, type, field, cmp,) #define RB_GENERATE_STATIC(name, type, field, cmp) \ RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static) #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \ attr void \ name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ { \ struct type *parent, *gparent, *tmp; \ while ((parent = RB_PARENT(elm, field)) && \ RB_COLOR(parent, field) == RB_RED) { \ gparent = RB_PARENT(parent, field); \ if (parent == RB_LEFT(gparent, field)) { \ tmp = RB_RIGHT(gparent, field); \ if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ RB_COLOR(tmp, field) = RB_BLACK; \ RB_SET_BLACKRED(parent, gparent, field);\ elm = gparent; \ continue; \ } \ if (RB_RIGHT(parent, field) == elm) { \ RB_ROTATE_LEFT(head, parent, tmp, field);\ tmp = parent; \ parent = elm; \ elm = tmp; \ } \ RB_SET_BLACKRED(parent, gparent, field); \ RB_ROTATE_RIGHT(head, gparent, tmp, field); \ } else { \ tmp = RB_LEFT(gparent, field); \ if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ RB_COLOR(tmp, field) = RB_BLACK; \ RB_SET_BLACKRED(parent, gparent, field);\ elm = gparent; \ continue; \ } \ if (RB_LEFT(parent, field) == elm) { \ RB_ROTATE_RIGHT(head, parent, tmp, field);\ tmp = parent; \ parent = elm; \ elm = tmp; \ } \ RB_SET_BLACKRED(parent, gparent, field); \ RB_ROTATE_LEFT(head, gparent, tmp, field); \ } \ } \ RB_COLOR(head->rbh_root, field) = RB_BLACK; \ } \ \ attr void \ name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \ { \ struct type *tmp; \ while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \ elm != RB_ROOT(head)) { \ if (RB_LEFT(parent, field) == elm) { \ tmp = RB_RIGHT(parent, field); \ if (RB_COLOR(tmp, field) == RB_RED) { \ RB_SET_BLACKRED(tmp, parent, field); \ RB_ROTATE_LEFT(head, parent, tmp, field);\ tmp = RB_RIGHT(parent, field); \ } \ if ((RB_LEFT(tmp, field) == NULL || \ RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ (RB_RIGHT(tmp, field) == NULL || \ RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ RB_COLOR(tmp, field) = RB_RED; \ elm = parent; \ parent = RB_PARENT(elm, field); \ } else { \ if (RB_RIGHT(tmp, field) == NULL || \ RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\ struct type *oleft; \ if ((oleft = RB_LEFT(tmp, field)))\ RB_COLOR(oleft, field) = RB_BLACK;\ RB_COLOR(tmp, field) = RB_RED; \ RB_ROTATE_RIGHT(head, tmp, oleft, field);\ tmp = RB_RIGHT(parent, field); \ } \ RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ RB_COLOR(parent, field) = RB_BLACK; \ if (RB_RIGHT(tmp, field)) \ RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\ RB_ROTATE_LEFT(head, parent, tmp, field);\ elm = RB_ROOT(head); \ break; \ } \ } else { \ tmp = RB_LEFT(parent, field); \ if (RB_COLOR(tmp, field) == RB_RED) { \ RB_SET_BLACKRED(tmp, parent, field); \ RB_ROTATE_RIGHT(head, parent, tmp, field);\ tmp = RB_LEFT(parent, field); \ } \ if ((RB_LEFT(tmp, field) == NULL || \ RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ (RB_RIGHT(tmp, field) == NULL || \ RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ RB_COLOR(tmp, field) = RB_RED; \ elm = parent; \ parent = RB_PARENT(elm, field); \ } else { \ if (RB_LEFT(tmp, field) == NULL || \ RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\ struct type *oright; \ if ((oright = RB_RIGHT(tmp, field)))\ RB_COLOR(oright, field) = RB_BLACK;\ RB_COLOR(tmp, field) = RB_RED; \ RB_ROTATE_LEFT(head, tmp, oright, field);\ tmp = RB_LEFT(parent, field); \ } \ RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ RB_COLOR(parent, field) = RB_BLACK; \ if (RB_LEFT(tmp, field)) \ RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\ RB_ROTATE_RIGHT(head, parent, tmp, field);\ elm = RB_ROOT(head); \ break; \ } \ } \ } \ if (elm) \ RB_COLOR(elm, field) = RB_BLACK; \ } \ \ attr struct type * \ name##_RB_REMOVE(struct name *head, struct type *elm) \ { \ struct type *child, *parent, *old = elm; \ int color; \ if (RB_LEFT(elm, field) == NULL) \ child = RB_RIGHT(elm, field); \ else if (RB_RIGHT(elm, field) == NULL) \ child = RB_LEFT(elm, field); \ else { \ struct type *left; \ elm = RB_RIGHT(elm, field); \ while ((left = RB_LEFT(elm, field))) \ elm = left; \ child = RB_RIGHT(elm, field); \ parent = RB_PARENT(elm, field); \ color = RB_COLOR(elm, field); \ if (child) \ RB_PARENT(child, field) = parent; \ if (parent) { \ if (RB_LEFT(parent, field) == elm) \ RB_LEFT(parent, field) = child; \ else \ RB_RIGHT(parent, field) = child; \ RB_AUGMENT(parent); \ } else \ RB_ROOT(head) = child; \ if (RB_PARENT(elm, field) == old) \ parent = elm; \ (elm)->field = (old)->field; \ if (RB_PARENT(old, field)) { \ if (RB_LEFT(RB_PARENT(old, field), field) == old)\ RB_LEFT(RB_PARENT(old, field), field) = elm;\ else \ RB_RIGHT(RB_PARENT(old, field), field) = elm;\ RB_AUGMENT(RB_PARENT(old, field)); \ } else \ RB_ROOT(head) = elm; \ RB_PARENT(RB_LEFT(old, field), field) = elm; \ if (RB_RIGHT(old, field)) \ RB_PARENT(RB_RIGHT(old, field), field) = elm; \ if (parent) { \ left = parent; \ do { \ RB_AUGMENT(left); \ } while ((left = RB_PARENT(left, field))); \ } \ goto color; \ } \ parent = RB_PARENT(elm, field); \ color = RB_COLOR(elm, field); \ if (child) \ RB_PARENT(child, field) = parent; \ if (parent) { \ if (RB_LEFT(parent, field) == elm) \ RB_LEFT(parent, field) = child; \ else \ RB_RIGHT(parent, field) = child; \ RB_AUGMENT(parent); \ } else \ RB_ROOT(head) = child; \ color: \ if (color == RB_BLACK) \ name##_RB_REMOVE_COLOR(head, parent, child); \ return (old); \ } \ \ /* Inserts a node into the RB tree */ \ attr struct type * \ name##_RB_INSERT(struct name *head, struct type *elm) \ { \ struct type *tmp; \ struct type *parent = NULL; \ int comp = 0; \ tmp = RB_ROOT(head); \ while (tmp) { \ parent = tmp; \ comp = (cmp)(elm, parent); \ if (comp < 0) \ tmp = RB_LEFT(tmp, field); \ else if (comp > 0) \ tmp = RB_RIGHT(tmp, field); \ else \ return (tmp); \ } \ RB_SET(elm, parent, field); \ if (parent != NULL) { \ if (comp < 0) \ RB_LEFT(parent, field) = elm; \ else \ RB_RIGHT(parent, field) = elm; \ RB_AUGMENT(parent); \ } else \ RB_ROOT(head) = elm; \ name##_RB_INSERT_COLOR(head, elm); \ return (NULL); \ } \ \ /* Finds the node with the same key as elm */ \ attr struct type * \ name##_RB_FIND(struct name *head, struct type *elm) \ { \ struct type *tmp = RB_ROOT(head); \ int comp; \ while (tmp) { \ comp = cmp(elm, tmp); \ if (comp < 0) \ tmp = RB_LEFT(tmp, field); \ else if (comp > 0) \ tmp = RB_RIGHT(tmp, field); \ else \ return (tmp); \ } \ return (NULL); \ } \ \ /* Finds the first node greater than or equal to the search key */ \ attr struct type * \ name##_RB_NFIND(struct name *head, struct type *elm) \ { \ struct type *tmp = RB_ROOT(head); \ struct type *res = NULL; \ int comp; \ while (tmp) { \ comp = cmp(elm, tmp); \ if (comp < 0) { \ res = tmp; \ tmp = RB_LEFT(tmp, field); \ } \ else if (comp > 0) \ tmp = RB_RIGHT(tmp, field); \ else \ return (tmp); \ } \ return (res); \ } \ \ /* ARGSUSED */ \ attr struct type * \ name##_RB_NEXT(struct type *elm) \ { \ if (RB_RIGHT(elm, field)) { \ elm = RB_RIGHT(elm, field); \ while (RB_LEFT(elm, field)) \ elm = RB_LEFT(elm, field); \ } else { \ if (RB_PARENT(elm, field) && \ (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ elm = RB_PARENT(elm, field); \ else { \ while (RB_PARENT(elm, field) && \ (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\ elm = RB_PARENT(elm, field); \ elm = RB_PARENT(elm, field); \ } \ } \ return (elm); \ } \ \ /* ARGSUSED */ \ attr struct type * \ name##_RB_PREV(struct type *elm) \ { \ if (RB_LEFT(elm, field)) { \ elm = RB_LEFT(elm, field); \ while (RB_RIGHT(elm, field)) \ elm = RB_RIGHT(elm, field); \ } else { \ if (RB_PARENT(elm, field) && \ (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \ elm = RB_PARENT(elm, field); \ else { \ while (RB_PARENT(elm, field) && \ (elm == RB_LEFT(RB_PARENT(elm, field), field)))\ elm = RB_PARENT(elm, field); \ elm = RB_PARENT(elm, field); \ } \ } \ return (elm); \ } \ \ attr struct type * \ name##_RB_MINMAX(struct name *head, int val) \ { \ struct type *tmp = RB_ROOT(head); \ struct type *parent = NULL; \ while (tmp) { \ parent = tmp; \ if (val < 0) \ tmp = RB_LEFT(tmp, field); \ else \ tmp = RB_RIGHT(tmp, field); \ } \ return (parent); \ } #define RB_NEGINF -1 #define RB_INF 1 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y) #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y) #define RB_FIND(name, x, y) name##_RB_FIND(x, y) #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y) #define RB_NEXT(name, x, y) name##_RB_NEXT(y) #define RB_PREV(name, x, y) name##_RB_PREV(y) #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF) #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF) #define RB_FOREACH(x, name, head) \ for ((x) = RB_MIN(name, head); \ (x) != NULL; \ (x) = name##_RB_NEXT(x)) #define RB_FOREACH_SAFE(x, name, head, y) \ for ((x) = RB_MIN(name, head); \ ((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \ (x) = (y)) #define RB_FOREACH_REVERSE(x, name, head) \ for ((x) = RB_MAX(name, head); \ (x) != NULL; \ (x) = name##_RB_PREV(x)) #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \ for ((x) = RB_MAX(name, head); \ ((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \ (x) = (y)) /* * Copyright (c) 2016 David Gwynne * * 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. */ struct rb_type { int (*t_compare)(const void *, const void *); void (*t_augment)(void *); unsigned int t_offset; /* offset of rb_entry in type */ }; struct rb_tree { struct rb_entry *rbt_root; }; struct rb_entry { struct rb_entry *rbt_parent; struct rb_entry *rbt_left; struct rb_entry *rbt_right; unsigned int rbt_color; }; #define RBT_HEAD(_name, _type) \ struct _name { \ struct rb_tree rbh_root; \ } #define RBT_ENTRY(_type) struct rb_entry static inline void _rb_init(struct rb_tree *rbt) { rbt->rbt_root = NULL; } static inline int _rb_empty(struct rb_tree *rbt) { return (rbt->rbt_root == NULL); } void *_rb_insert(const struct rb_type *, struct rb_tree *, void *); void *_rb_remove(const struct rb_type *, struct rb_tree *, void *); void *_rb_find(const struct rb_type *, struct rb_tree *, const void *); void *_rb_nfind(const struct rb_type *, struct rb_tree *, const void *); void *_rb_root(const struct rb_type *, struct rb_tree *); void *_rb_min(const struct rb_type *, struct rb_tree *); void *_rb_max(const struct rb_type *, struct rb_tree *); void *_rb_next(const struct rb_type *, void *); void *_rb_prev(const struct rb_type *, void *); void *_rb_left(const struct rb_type *, void *); void *_rb_right(const struct rb_type *, void *); void *_rb_parent(const struct rb_type *, void *); void _rb_set_left(const struct rb_type *, void *, void *); void _rb_set_right(const struct rb_type *, void *, void *); void _rb_set_parent(const struct rb_type *, void *, void *); void _rb_poison(const struct rb_type *, void *, unsigned long); int _rb_check(const struct rb_type *, void *, unsigned long); #define RBT_INITIALIZER(_head) { { NULL } } #define RBT_PROTOTYPE(_name, _type, _field, _cmp) \ extern const struct rb_type *const _name##_RBT_TYPE; \ \ __unused static inline void \ _name##_RBT_INIT(struct _name *head) \ { \ _rb_init(&head->rbh_root); \ } \ \ __unused static inline struct _type * \ _name##_RBT_INSERT(struct _name *head, struct _type *elm) \ { \ return _rb_insert(_name##_RBT_TYPE, &head->rbh_root, elm); \ } \ \ __unused static inline struct _type * \ _name##_RBT_REMOVE(struct _name *head, struct _type *elm) \ { \ return _rb_remove(_name##_RBT_TYPE, &head->rbh_root, elm); \ } \ \ __unused static inline struct _type * \ _name##_RBT_FIND(struct _name *head, const struct _type *key) \ { \ return _rb_find(_name##_RBT_TYPE, &head->rbh_root, key); \ } \ \ __unused static inline struct _type * \ _name##_RBT_NFIND(struct _name *head, const struct _type *key) \ { \ return _rb_nfind(_name##_RBT_TYPE, &head->rbh_root, key); \ } \ \ __unused static inline struct _type * \ _name##_RBT_ROOT(struct _name *head) \ { \ return _rb_root(_name##_RBT_TYPE, &head->rbh_root); \ } \ \ __unused static inline int \ _name##_RBT_EMPTY(struct _name *head) \ { \ return _rb_empty(&head->rbh_root); \ } \ \ __unused static inline struct _type * \ _name##_RBT_MIN(struct _name *head) \ { \ return _rb_min(_name##_RBT_TYPE, &head->rbh_root); \ } \ \ __unused static inline struct _type * \ _name##_RBT_MAX(struct _name *head) \ { \ return _rb_max(_name##_RBT_TYPE, &head->rbh_root); \ } \ \ __unused static inline struct _type * \ _name##_RBT_NEXT(struct _type *elm) \ { \ return _rb_next(_name##_RBT_TYPE, elm); \ } \ \ __unused static inline struct _type * \ _name##_RBT_PREV(struct _type *elm) \ { \ return _rb_prev(_name##_RBT_TYPE, elm); \ } \ \ __unused static inline struct _type * \ _name##_RBT_LEFT(struct _type *elm) \ { \ return _rb_left(_name##_RBT_TYPE, elm); \ } \ \ __unused static inline struct _type * \ _name##_RBT_RIGHT(struct _type *elm) \ { \ return _rb_right(_name##_RBT_TYPE, elm); \ } \ \ __unused static inline struct _type * \ _name##_RBT_PARENT(struct _type *elm) \ { \ return _rb_parent(_name##_RBT_TYPE, elm); \ } \ \ __unused static inline void \ _name##_RBT_SET_LEFT(struct _type *elm, struct _type *left) \ { \ return _rb_set_left(_name##_RBT_TYPE, elm, left); \ } \ \ __unused static inline void \ _name##_RBT_SET_RIGHT(struct _type *elm, struct _type *right) \ { \ return _rb_set_right(_name##_RBT_TYPE, elm, right); \ } \ \ __unused static inline void \ _name##_RBT_SET_PARENT(struct _type *elm, struct _type *parent) \ { \ return _rb_set_parent(_name##_RBT_TYPE, elm, parent); \ } \ \ __unused static inline void \ _name##_RBT_POISON(struct _type *elm, unsigned long poison) \ { \ return _rb_poison(_name##_RBT_TYPE, elm, poison); \ } \ \ __unused static inline int \ _name##_RBT_CHECK(struct _type *elm, unsigned long poison) \ { \ return _rb_check(_name##_RBT_TYPE, elm, poison); \ } #define RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _aug) \ static int \ _name##_RBT_COMPARE(const void *lptr, const void *rptr) \ { \ const struct _type *l = lptr, *r = rptr; \ return _cmp(l, r); \ } \ static const struct rb_type _name##_RBT_INFO = { \ _name##_RBT_COMPARE, \ _aug, \ offsetof(struct _type, _field), \ }; \ const struct rb_type *const _name##_RBT_TYPE = &_name##_RBT_INFO #define RBT_GENERATE_AUGMENT(_name, _type, _field, _cmp, _aug) \ static void \ _name##_RBT_AUGMENT(void *ptr) \ { \ struct _type *p = ptr; \ return _aug(p); \ } \ RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _name##_RBT_AUGMENT) #define RBT_GENERATE(_name, _type, _field, _cmp) \ RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, NULL) #define RBT_INIT(_name, _head) _name##_RBT_INIT(_head) #define RBT_INSERT(_name, _head, _elm) _name##_RBT_INSERT(_head, _elm) #define RBT_REMOVE(_name, _head, _elm) _name##_RBT_REMOVE(_head, _elm) #define RBT_FIND(_name, _head, _key) _name##_RBT_FIND(_head, _key) #define RBT_NFIND(_name, _head, _key) _name##_RBT_NFIND(_head, _key) #define RBT_ROOT(_name, _head) _name##_RBT_ROOT(_head) #define RBT_EMPTY(_name, _head) _name##_RBT_EMPTY(_head) #define RBT_MIN(_name, _head) _name##_RBT_MIN(_head) #define RBT_MAX(_name, _head) _name##_RBT_MAX(_head) #define RBT_NEXT(_name, _elm) _name##_RBT_NEXT(_elm) #define RBT_PREV(_name, _elm) _name##_RBT_PREV(_elm) #define RBT_LEFT(_name, _elm) _name##_RBT_LEFT(_elm) #define RBT_RIGHT(_name, _elm) _name##_RBT_RIGHT(_elm) #define RBT_PARENT(_name, _elm) _name##_RBT_PARENT(_elm) #define RBT_SET_LEFT(_name, _elm, _l) _name##_RBT_SET_LEFT(_elm, _l) #define RBT_SET_RIGHT(_name, _elm, _r) _name##_RBT_SET_RIGHT(_elm, _r) #define RBT_SET_PARENT(_name, _elm, _p) _name##_RBT_SET_PARENT(_elm, _p) #define RBT_POISON(_name, _elm, _p) _name##_RBT_POISON(_elm, _p) #define RBT_CHECK(_name, _elm, _p) _name##_RBT_CHECK(_elm, _p) #define RBT_FOREACH(_e, _name, _head) \ for ((_e) = RBT_MIN(_name, (_head)); \ (_e) != NULL; \ (_e) = RBT_NEXT(_name, (_e))) #define RBT_FOREACH_SAFE(_e, _name, _head, _n) \ for ((_e) = RBT_MIN(_name, (_head)); \ (_e) != NULL && ((_n) = RBT_NEXT(_name, (_e)), 1); \ (_e) = (_n)) #define RBT_FOREACH_REVERSE(_e, _name, _head) \ for ((_e) = RBT_MAX(_name, (_head)); \ (_e) != NULL; \ (_e) = RBT_PREV(_name, (_e))) #define RBT_FOREACH_REVERSE_SAFE(_e, _name, _head, _n) \ for ((_e) = RBT_MAX(_name, (_head)); \ (_e) != NULL && ((_n) = RBT_PREV(_name, (_e)), 1); \ (_e) = (_n)) #endif /* _SYS_TREE_H_ */ libopensmtpd-0.7/opensmtpd.c000066400000000000000000001501461405566102200162450ustar00rootroot00000000000000/* * Copyright (c) 2019 Martijn van Duren * * 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. */ #define _GNU_SOURCE 1 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "openbsd-compat.h" #include "opensmtpd.h" #include "ioev.h" #define NITEMS(x) (sizeof(x) / sizeof(*x)) struct osmtpd_callback { enum osmtpd_type type; enum osmtpd_phase phase; int incoming; void (*osmtpd_cb)(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); void *cb; int doregister; int storereport; }; struct osmtpd_session { struct osmtpd_ctx ctx; RB_ENTRY(osmtpd_session) entry; }; static void osmtpd_register(enum osmtpd_type, enum osmtpd_phase, int, int, void *); static const char *osmtpd_typetostr(enum osmtpd_type); static const char *osmtpd_phasetostr(enum osmtpd_phase); static enum osmtpd_phase osmtpd_strtophase(const char *, const char *); static void osmtpd_newline(struct io *, int, void *); static void osmtpd_outevt(struct io *, int, void *); static void osmtpd_noargs(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_onearg(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_connect(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_identify(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_link_connect(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_link_disconnect(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_link_greeting(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_link_identify(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_link_tls(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_tx_begin(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_tx_mail(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_tx_rcpt(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_tx_envelope(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_tx_data(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_tx_commit(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_tx_rollback(struct osmtpd_callback *, struct osmtpd_ctx *, char *, char *); static void osmtpd_addrtoss(char *, struct sockaddr_storage *, int, char *); static enum osmtpd_status osmtpd_strtostatus(const char *, char *); static int osmtpd_session_cmp(struct osmtpd_session *, struct osmtpd_session *); static void *(*oncreatecb_session)(struct osmtpd_ctx *) = NULL; static void (*ondeletecb_session)(struct osmtpd_ctx *, void *) = NULL; static void *(*oncreatecb_message)(struct osmtpd_ctx *) = NULL; static void (*ondeletecb_message)(struct osmtpd_ctx *, void *) = NULL; static void (*conf_cb)(const char *, const char *); static struct osmtpd_callback osmtpd_callbacks[] = { { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_CONNECT, 1, osmtpd_connect, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_HELO, 1, osmtpd_identify, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_EHLO, 1, osmtpd_identify, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_STARTTLS, 1, osmtpd_noargs, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_AUTH, 1, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_MAIL_FROM, 1, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_RCPT_TO, 1, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_DATA, 1, osmtpd_noargs, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_DATA_LINE, 1, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_RSET, 1, osmtpd_noargs, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_QUIT, 1, osmtpd_noargs, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_NOOP, 1, osmtpd_noargs, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_HELP, 1, osmtpd_noargs, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_WIZ, 1, osmtpd_noargs, NULL, 0, 0 }, { OSMTPD_TYPE_FILTER, OSMTPD_PHASE_COMMIT, 1, osmtpd_noargs, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_CONNECT, 1, osmtpd_link_connect, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, osmtpd_link_disconnect, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_GREETING, 1, osmtpd_link_greeting, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_IDENTIFY, 1, osmtpd_link_identify, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_TLS, 1, osmtpd_link_tls, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_BEGIN, 1, osmtpd_tx_begin, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_MAIL, 1, osmtpd_tx_mail, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_RCPT, 1, osmtpd_tx_rcpt, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ENVELOPE, 1, osmtpd_tx_envelope, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_DATA, 1, osmtpd_tx_data, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_COMMIT, 1, osmtpd_tx_commit, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ROLLBACK, 1, osmtpd_tx_rollback, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_PROTOCOL_CLIENT, 1, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_PROTOCOL_SERVER, 1, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_FILTER_RESPONSE, 1, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TIMEOUT, 1, osmtpd_noargs, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_CONNECT, 0, osmtpd_link_connect, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 0, osmtpd_link_disconnect, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_GREETING, 0, osmtpd_link_greeting, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_IDENTIFY, 0, osmtpd_link_identify, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_TLS, 0, osmtpd_link_tls, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_BEGIN, 0, osmtpd_tx_begin, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_MAIL, 0, osmtpd_tx_mail, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_RCPT, 0, osmtpd_tx_rcpt, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ENVELOPE, 0, osmtpd_tx_envelope, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_DATA, 0, osmtpd_tx_data, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_COMMIT, 0, osmtpd_tx_commit, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ROLLBACK, 0, osmtpd_tx_rollback, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_PROTOCOL_CLIENT, 0, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_PROTOCOL_SERVER, 0, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_FILTER_RESPONSE, 0, osmtpd_onearg, NULL, 0, 0 }, { OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TIMEOUT, 0, osmtpd_noargs, NULL, 0, 0 } }; static struct io *io_stdout; static int needs; static int ready = 0; /* Default from smtpd */ static int session_timeout = 300; RB_HEAD(osmtpd_sessions, osmtpd_session) osmtpd_sessions = RB_INITIALIZER(NULL); RB_PROTOTYPE_STATIC(osmtpd_sessions, osmtpd_session, entry, osmtpd_session_cmp); void osmtpd_register_conf(void (*cb)(const char *, const char *)) { conf_cb = cb; } void osmtpd_register_filter_connect(void (*cb)(struct osmtpd_ctx *, const char *, struct sockaddr_storage *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_CONNECT, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_helo(void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_HELO, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_ehlo(void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_EHLO, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_starttls(void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_STARTTLS, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_auth(void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_AUTH, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_mailfrom(void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_MAIL_FROM, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_rcptto(void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_RCPT_TO, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_data(void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_DATA, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_dataline(void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_DATA_LINE, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_rset(void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_RSET, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_quit(void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_QUIT, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_noop(void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_NOOP, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_help(void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_HELP, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_wiz(void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_WIZ, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_filter_commit(void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_FILTER, OSMTPD_PHASE_COMMIT, 1, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, 1, 0, NULL); } void osmtpd_register_report_connect(int incoming, void (*cb)(struct osmtpd_ctx *, const char *, enum osmtpd_status, struct sockaddr_storage *, struct sockaddr_storage *)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_CONNECT, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_disconnect(int incoming, void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, (void *)cb); } void osmtpd_register_report_identify(int incoming, void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_IDENTIFY, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_tls(int incoming, void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_TLS, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_begin(int incoming, void (*cb)(struct osmtpd_ctx *, uint32_t)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_BEGIN, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_mail(int incoming, void (*cb)(struct osmtpd_ctx *, uint32_t, const char *, enum osmtpd_status)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_MAIL, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_rcpt(int incoming, void (*cb)(struct osmtpd_ctx *, uint32_t, const char *, enum osmtpd_status)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_RCPT, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_envelope(int incoming, void (*cb)(struct osmtpd_ctx *, uint32_t, uint64_t)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ENVELOPE, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_data(int incoming, void (*cb)(struct osmtpd_ctx *, uint32_t, enum osmtpd_status)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_DATA, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_commit(int incoming, void (*cb)(struct osmtpd_ctx *, uint32_t, size_t)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_COMMIT, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_rollback(int incoming, void (*cb)(struct osmtpd_ctx *, uint32_t)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ROLLBACK, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_client(int incoming, void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_PROTOCOL_CLIENT, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_server(int incoming, void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_PROTOCOL_SERVER, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_response(int incoming, void (*cb)(struct osmtpd_ctx *, const char *)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_FILTER_RESPONSE, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_register_report_timeout(int incoming, void (*cb)(struct osmtpd_ctx *)) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TIMEOUT, incoming, 0, (void *)cb); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_local_session(void *(*oncreate)(struct osmtpd_ctx *), void (*ondelete)(struct osmtpd_ctx *, void *)) { oncreatecb_session = oncreate; ondeletecb_session = ondelete; } void osmtpd_local_message(void *(*oncreate)(struct osmtpd_ctx *), void (*ondelete)(struct osmtpd_ctx *, void *)) { oncreatecb_message = oncreate; ondeletecb_message = ondelete; } void osmtpd_need(int lneeds) { needs |= lneeds; } static void osmtpd_register_need(int incoming) { if (needs & (OSMTPD_NEED_SRC | OSMTPD_NEED_DST | OSMTPD_NEED_RDNS | OSMTPD_NEED_FCRDNS)) osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_CONNECT, incoming, 1, NULL); if (needs & OSMTPD_NEED_GREETING) osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_GREETING, incoming, 1, NULL); if (needs & OSMTPD_NEED_IDENTITY) osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_IDENTIFY, incoming, 1, NULL); if (needs & OSMTPD_NEED_CIPHERS) osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_TLS, incoming, 1, NULL); if (needs & OSMTPD_NEED_MSGID) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_BEGIN, incoming, 1, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ROLLBACK, incoming, 0, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_COMMIT, incoming, 0, NULL); } if (needs & OSMTPD_NEED_MAILFROM) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_MAIL, incoming, 1, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ROLLBACK, incoming, 0, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_COMMIT, incoming, 0, NULL); } if (needs & OSMTPD_NEED_RCPTTO) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_RCPT, incoming, 1, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ROLLBACK, incoming, 0, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_COMMIT, incoming, 0, NULL); } if (needs & OSMTPD_NEED_EVPID) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ENVELOPE, incoming, 1, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ROLLBACK, incoming, 0, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_COMMIT, incoming, 0, NULL); } osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_LINK_DISCONNECT, incoming, 0, NULL); } void osmtpd_run(void) { size_t i = 0; int registered = 0; struct event_base *evbase; struct io *io_stdin; struct osmtpd_callback *hidenity = NULL, *eidentity = NULL; struct osmtpd_callback *ridentity = NULL; evbase = event_init(); if ((io_stdin = io_new()) == NULL || (io_stdout = io_new()) == NULL) osmtpd_err(1, "io_new"); io_set_nonblocking(STDIN_FILENO); io_set_fd(io_stdin, STDIN_FILENO); io_set_callback(io_stdin, osmtpd_newline, NULL); io_set_read(io_stdin); io_set_nonblocking(STDOUT_FILENO); io_set_fd(io_stdout, STDOUT_FILENO); io_set_callback(io_stdout, osmtpd_outevt, NULL); io_set_write(io_stdout); for (i = 0; i < NITEMS(osmtpd_callbacks); i++) { if (osmtpd_callbacks[i].doregister) { osmtpd_register_need(osmtpd_callbacks[i].incoming); if (oncreatecb_message != NULL) { osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_BEGIN, osmtpd_callbacks[i].incoming, 0, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_ROLLBACK, osmtpd_callbacks[i].incoming, 0, NULL); osmtpd_register(OSMTPD_TYPE_REPORT, OSMTPD_PHASE_TX_COMMIT, osmtpd_callbacks[i].incoming, 0, NULL); } if (osmtpd_callbacks[i].type == OSMTPD_TYPE_FILTER && osmtpd_callbacks[i].phase == OSMTPD_PHASE_HELO) hidenity = &(osmtpd_callbacks[i]); if (osmtpd_callbacks[i].type == OSMTPD_TYPE_FILTER && osmtpd_callbacks[i].phase == OSMTPD_PHASE_EHLO) eidentity = &(osmtpd_callbacks[i]); if (osmtpd_callbacks[i].type == OSMTPD_TYPE_REPORT && osmtpd_callbacks[i].phase == OSMTPD_PHASE_LINK_IDENTIFY && osmtpd_callbacks[i].incoming == 1) ridentity = &(osmtpd_callbacks[i]); } } if (ridentity != NULL && ridentity->storereport) { if (hidenity != NULL && hidenity->doregister) hidenity->storereport = 1; if (eidentity != NULL && eidentity->doregister) eidentity->storereport = 1; } for (i = 0; i < NITEMS(osmtpd_callbacks); i++) { if (osmtpd_callbacks[i].doregister) { if (osmtpd_callbacks[i].cb != NULL) registered = 1; io_printf(io_stdout, "register|%s|smtp-%s|%s\n", osmtpd_typetostr(osmtpd_callbacks[i].type), osmtpd_callbacks[i].incoming ? "in" : "out", osmtpd_phasetostr(osmtpd_callbacks[i].phase)); } } if (!registered) osmtpd_errx(1, "No events registered"); io_printf(io_stdout, "register|ready\n"); ready = 1; event_dispatch(); io_free(io_stdin); io_free(io_stdout); event_base_free(evbase); } __dead void osmtpd_err(int eval, const char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fprintf(stderr, ": %s\n", strerror(errno)); exit(eval); } __dead void osmtpd_errx(int eval, const char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fprintf(stderr, "\n"); exit(eval); } static void osmtpd_newline(struct io *io, int ev, __unused void *arg) { static char *linedup = NULL; static size_t dupsize = 0; struct osmtpd_session *ctx, search; enum osmtpd_type type; enum osmtpd_phase phase; int version_major, version_minor, incoming; struct timespec tm; char *line = NULL; const char *errstr = NULL; size_t linelen; char *end; size_t i; if (ev == IO_DISCONNECTED) { event_loopexit(0); return; } if (ev != IO_DATAIN) return; while ((line = io_getline(io, &linelen)) != NULL) { if (dupsize < linelen) { if ((linedup = realloc(linedup, linelen + 1)) == NULL) osmtpd_err(1, NULL); dupsize = linelen + 1; } strlcpy(linedup, line, dupsize); if ((end = strchr(line, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing " "version: %s", linedup); end++[0] = '\0'; if (strcmp(line, "filter") == 0) type = OSMTPD_TYPE_FILTER; else if (strcmp(line, "report") == 0) type = OSMTPD_TYPE_REPORT; else if (strcmp(line, "config") == 0) { line = end; if (strcmp(line, "ready") == 0) { if (conf_cb != NULL) conf_cb(NULL, NULL); continue; } if ((end = strchr(line, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing " "key: %s", linedup); end++[0] = '\0'; if (conf_cb != NULL) conf_cb(line, end); if (strcmp(line, "smtp-session-timeout") == 0) { session_timeout = strtonum(end, 0, INT_MAX, &errstr); if (errstr != NULL) osmtpd_errx(1, "Invalid line received: " "invalid smtp-sesion-timeout: %s", linedup); } continue; } else osmtpd_errx(1, "Invalid line received: unknown message " "type: %s", linedup); line = end; version_major = strtoul(line, &end, 10); if (line == end || end[0] != '.') osmtpd_errx(1, "Invalid protocol received: %s", linedup); line = end + 1; version_minor = strtoul(line, &end, 10); if (end[0] == '\0') osmtpd_errx(1, "Invalid line received: missing time: " "%s", linedup); if (line == end || end[0] != '|') osmtpd_errx(1, "Invalid protocol received: %s", linedup); if (version_major != 0) osmtpd_errx(1, "Unsupported protocol received: %s", linedup); line = end + 1; if ((end = strchr(line, '.')) == NULL) osmtpd_errx(1, "Invalid line received: invalid " "timestamp: %s", linedup); end++[0] = '\0'; tm.tv_sec = (time_t) strtonum(line, 0, INT64_MAX, &errstr); if (errstr != NULL) osmtpd_errx(1, "Invalid line received: invalid " "timestamp: %s", linedup); line = end; if ((end = strchr(line, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing " "direction: %s", linedup); end++[0] = '\0'; tm.tv_nsec = (long) strtonum(line, 0, LONG_MAX, &errstr); if (errstr != NULL) osmtpd_errx(1, "Invalid line received: invalid " "timestamp: %s", linedup); tm.tv_nsec *= 10 * (9 - (end - line)); line = end; if ((end = strchr(line, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing " "phase: %s", linedup); end++[0] = '\0'; if (strcmp(line, "smtp-in") == 0) incoming = 1; else if (strcmp(line, "smtp-out") == 0) incoming = 0; else osmtpd_errx(1, "Invalid line: invalid direction: %s", linedup); line = end; if ((end = strchr(line, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing reqid: " "%s", linedup); end++[0] = '\0'; phase = osmtpd_strtophase(line, linedup); line = end; errno = 0; search.ctx.reqid = strtoull(line, &end, 16); if ((search.ctx.reqid == ULLONG_MAX && errno != 0) || (end[0] != '|' && end[0] != '\0')) osmtpd_errx(1, "Invalid line received: invalid reqid: " "%s", linedup); line = end + 1; ctx = RB_FIND(osmtpd_sessions, &osmtpd_sessions, &search); if (ctx == NULL) { if ((ctx = malloc(sizeof(*ctx))) == NULL) osmtpd_err(1, NULL); ctx->ctx.reqid = search.ctx.reqid; ctx->ctx.rdns = NULL; ctx->ctx.fcrdns = OSMTPD_STATUS_TEMPFAIL; ctx->ctx.identity = NULL; ctx->ctx.greeting.identity = NULL; ctx->ctx.ciphers = NULL; ctx->ctx.msgid = 0; ctx->ctx.mailfrom = NULL; ctx->ctx.rcptto = malloc(sizeof(*(ctx->ctx.rcptto))); if (ctx->ctx.rcptto == NULL) osmtpd_err(1, "malloc"); ctx->ctx.rcptto[0] = NULL; memset(&(ctx->ctx.src), 0, sizeof(ctx->ctx.src)); ctx->ctx.src.ss_family = AF_UNSPEC; memset(&(ctx->ctx.dst), 0, sizeof(ctx->ctx.dst)); ctx->ctx.dst.ss_family = AF_UNSPEC; RB_INSERT(osmtpd_sessions, &osmtpd_sessions, ctx); ctx->ctx.evpid = 0; ctx->ctx.local_session = NULL; ctx->ctx.local_message = NULL; if (oncreatecb_session != NULL) ctx->ctx.local_session = oncreatecb_session(&ctx->ctx); } ctx->ctx.type = type; ctx->ctx.phase = phase; ctx->ctx.version_major = version_major; ctx->ctx.version_minor = version_minor; ctx->ctx.incoming = incoming; ctx->ctx.tm.tv_sec = tm.tv_sec; ctx->ctx.tm.tv_nsec = tm.tv_nsec; ctx->ctx.token = 0; for (i = 0; i < NITEMS(osmtpd_callbacks); i++) { if (ctx->ctx.type == osmtpd_callbacks[i].type && ctx->ctx.phase == osmtpd_callbacks[i].phase && ctx->ctx.incoming == osmtpd_callbacks[i].incoming) break; } if (i == NITEMS(osmtpd_callbacks)) { osmtpd_errx(1, "Invalid line received: received " "unregistered line: %s", linedup); } if (ctx->ctx.type == OSMTPD_TYPE_FILTER) { ctx->ctx.token = strtoull(line, &end, 16); if ((ctx->ctx.token == ULLONG_MAX && errno != 0) || end[0] != '|') osmtpd_errx(1, "Invalid line received: invalid " "token: %s", linedup); line = end + 1; } osmtpd_callbacks[i].osmtpd_cb(&(osmtpd_callbacks[i]), &(ctx->ctx), line, linedup); } } static void osmtpd_outevt(__unused struct io *io, int evt, __unused void *arg) { switch (evt) { case IO_LOWAT: return; case IO_DISCONNECTED: exit(0); default: osmtpd_errx(1, "Unexpectd event"); } } static void osmtpd_noargs(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, __unused char *params, __unused char *linedup) { void (*f)(struct osmtpd_ctx *); f = cb->cb; f(ctx); } static void osmtpd_onearg(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *line, __unused char *linedup) { void (*f)(struct osmtpd_ctx *, const char *); f = cb->cb; f(ctx, line); } static void osmtpd_connect(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *params, char *linedup) { struct sockaddr_storage ss; char *hostname; char *address; void (*f)(struct osmtpd_ctx *, const char *, struct sockaddr_storage *); hostname = params; if ((address = strchr(params, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); address++[0] = '\0'; osmtpd_addrtoss(address, &ss, 0, linedup); f = cb->cb; f(ctx, hostname, &ss); } static void osmtpd_identify(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *identity, __unused char *linedup) { void (*f)(struct osmtpd_ctx *, const char *); if (cb->storereport) { free(ctx->identity); if ((ctx->identity = strdup(identity)) == NULL) osmtpd_err(1, "strdup"); } f = cb->cb; f(ctx, identity); } static void osmtpd_link_connect(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *params, char *linedup) { char *end, *rdns; enum osmtpd_status fcrdns; struct sockaddr_storage src, dst; void (*f)(struct osmtpd_ctx *, const char *, enum osmtpd_status, struct sockaddr_storage *, struct sockaddr_storage *); if ((end = strchr(params, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing fcrdns: %s", linedup); end++[0] = '\0'; rdns = params; params = end; if ((end = strchr(params, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing src: %s", linedup); end++[0] = '\0'; if (strcmp(params, "pass") == 0) fcrdns = OSMTPD_STATUS_OK; else if (strcmp(params, "fail") == 0) fcrdns = OSMTPD_STATUS_PERMFAIL; else if (strcmp(params, "error") == 0) fcrdns = OSMTPD_STATUS_TEMPFAIL; else osmtpd_errx(1, "Invalid line received: invalid fcrdns: %s", linedup); params = end; if ((end = strchr(params, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing dst: %s", linedup); end++[0] = '\0'; osmtpd_addrtoss(params, &src, 1, linedup); params = end; osmtpd_addrtoss(params, &dst, 1, linedup); if (cb->storereport) { if ((ctx->rdns = strdup(rdns)) == NULL) osmtpd_err(1, "strdup"); ctx->fcrdns = fcrdns; memcpy(&(ctx->src), &src, sizeof(ctx->src)); memcpy(&(ctx->dst), &dst, sizeof(ctx->dst)); } if ((f = cb->cb) != NULL) f(ctx, rdns, fcrdns, &src, &dst); } static void osmtpd_link_disconnect(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, __unused char *param, __unused char *linedup) { void (*f)(struct osmtpd_ctx *); size_t i; struct osmtpd_session *session, search; if ((f = cb->cb) != NULL) f(ctx); search.ctx.reqid = ctx->reqid; session = RB_FIND(osmtpd_sessions, &osmtpd_sessions, &search); if (session != NULL) { RB_REMOVE(osmtpd_sessions, &osmtpd_sessions, session); if (ondeletecb_session != NULL) ondeletecb_session(ctx, session->ctx.local_session); free(session->ctx.rdns); free(session->ctx.identity); free(session->ctx.greeting.identity); free(session->ctx.ciphers); free(session->ctx.mailfrom); for (i = 0; session->ctx.rcptto[i] != NULL; i++) free(session->ctx.rcptto[i]); free(session->ctx.rcptto); free(session); } } static void osmtpd_link_greeting(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *identity, __unused char *linedup) { void (*f)(struct osmtpd_ctx *, const char *); if (cb->storereport) { free(ctx->greeting.identity); if ((ctx->greeting.identity = strdup(identity)) == NULL) osmtpd_err(1, NULL); } if ((f = cb->cb) != NULL) f(ctx, identity); } static void osmtpd_link_identify(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *identity, __unused char *linedup) { void (*f)(struct osmtpd_ctx *, const char *); if (cb->storereport) { free(ctx->identity); if ((ctx->identity = strdup(identity)) == NULL) osmtpd_err(1, NULL); } if ((f = cb->cb) != NULL) f(ctx, identity); } static void osmtpd_link_tls(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *ciphers, __unused char *linedup) { void (*f)(struct osmtpd_ctx *, const char *); if (cb->storereport) { if ((ctx->ciphers = strdup(ciphers)) == NULL) osmtpd_err(1, NULL); } if ((f = cb->cb) != NULL) f(ctx, ciphers); } static void osmtpd_tx_begin(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *msgid, char *linedup) { unsigned long imsgid; char *endptr; void (*f)(struct osmtpd_ctx *, uint32_t); errno = 0; imsgid = strtoul(msgid, &endptr, 16); if ((imsgid == ULONG_MAX && errno != 0) || endptr[0] != '\0') osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); ctx->msgid = imsgid; /* Check if we're in range */ if ((unsigned long) ctx->msgid != imsgid) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (!cb->storereport) ctx->msgid = 0; if (oncreatecb_message != NULL) ctx->local_message = oncreatecb_message(ctx); if ((f = cb->cb) != NULL) f(ctx, imsgid); } static void osmtpd_tx_mail(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *params, char *linedup) { char *end, *mailfrom; enum osmtpd_status status; unsigned long imsgid; uint32_t msgid; void (*f)(struct osmtpd_ctx *, uint32_t, const char *, enum osmtpd_status); errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; if ((end = strchr(params, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing status: %s", linedup); end++[0] = '\0'; if (ctx->version_major == 0 && ctx->version_minor < 6) { mailfrom = params; status = osmtpd_strtostatus(end, linedup); } else { mailfrom = end; status = osmtpd_strtostatus(params, linedup); } if (cb->storereport) { if ((ctx->mailfrom = strdup(mailfrom)) == NULL) osmtpd_err(1, NULL); } if ((f = cb->cb) != NULL) f(ctx, msgid, mailfrom, status); } static void osmtpd_tx_rcpt(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *params, char *linedup) { char *end, *rcptto; enum osmtpd_status status; unsigned long imsgid; uint32_t msgid; size_t i; void (*f)(struct osmtpd_ctx *, uint32_t, const char *, enum osmtpd_status); errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; if ((end = strchr(params, '|')) == NULL) osmtpd_errx(1, "Invalid line received: missing status: %s", linedup); end++[0] = '\0'; if (ctx->version_major == 0 && ctx->version_minor < 6) { rcptto = params; status = osmtpd_strtostatus(end, linedup); } else { rcptto = end; status = osmtpd_strtostatus(params, linedup); } if (cb->storereport) { for (i = 0; ctx->rcptto[i] != NULL; i++) ; ctx->rcptto = reallocarray(ctx->rcptto, i + 2, sizeof(*(ctx->rcptto))); if (ctx->rcptto == NULL) osmtpd_err(1, NULL); if ((ctx->rcptto[i] = strdup(rcptto)) == NULL) osmtpd_err(1, NULL); ctx->rcptto[i + 1] = NULL; } if ((f = cb->cb) != NULL) f(ctx, msgid, rcptto, status); } static void osmtpd_tx_envelope(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *params, char *linedup) { unsigned long imsgid; uint32_t msgid; uint64_t evpid; char *end; void (*f)(struct osmtpd_ctx *, uint32_t, uint64_t); errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; evpid = strtoull(params, &end, 16); if ((ctx->evpid == ULLONG_MAX && errno != 0) || end[0] != '\0') osmtpd_errx(1, "Invalid line received: invalid evpid: %s", linedup); if (cb->storereport) ctx->evpid = evpid; if ((f = cb->cb) != NULL) f(ctx, msgid, evpid); } static void osmtpd_tx_data(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *params, char *linedup) { char *end; unsigned long imsgid; uint32_t msgid; void (*f)(struct osmtpd_ctx *, uint32_t, enum osmtpd_status); errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; if ((f = cb->cb) != NULL) f(ctx, msgid, osmtpd_strtostatus(params, linedup)); } static void osmtpd_tx_commit(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *params, char *linedup) { char *end; const char *errstr = NULL; unsigned long imsgid; uint32_t msgid; size_t i, msgsz; void (*f)(struct osmtpd_ctx *, uint32_t, size_t); errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '|') osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); params = end + 1; msgsz = strtonum(params, 0, UINT32_MAX, &errstr); if (errstr != NULL) osmtpd_errx(1, "Invalid line received: invalid msg size: %s", linedup); if ((f = cb->cb) != NULL) f(ctx, msgid, msgsz); if (ondeletecb_message != NULL) { ondeletecb_message(ctx, ctx->local_message); ctx->local_message = NULL; } free(ctx->mailfrom); ctx->mailfrom = NULL; for (i = 0; ctx->rcptto[i] != NULL; i++) free(ctx->rcptto[i]); ctx->rcptto[0] = NULL; ctx->evpid = 0; ctx->msgid = 0; } static void osmtpd_tx_rollback(struct osmtpd_callback *cb, struct osmtpd_ctx *ctx, char *params, char *linedup) { char *end; unsigned long imsgid; uint32_t msgid; size_t i; void (*f)(struct osmtpd_ctx *, uint32_t); errno = 0; imsgid = strtoul(params, &end, 16); if ((imsgid == ULONG_MAX && errno != 0)) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if (end[0] != '\0') osmtpd_errx(1, "Invalid line received: missing address: %s", linedup); msgid = imsgid; if ((unsigned long) msgid != imsgid) osmtpd_errx(1, "Invalid line received: invalid msgid: %s", linedup); if ((f = cb->cb) != NULL) f(ctx, msgid); if (ondeletecb_message != NULL) { ondeletecb_message(ctx, ctx->local_message); ctx->local_message = NULL; } free(ctx->mailfrom); ctx->mailfrom = NULL; for (i = 0; ctx->rcptto[i] != NULL; i++) free(ctx->rcptto[i]); ctx->rcptto[0] = NULL; ctx->evpid = 0; ctx->msgid = 0; } void osmtpd_filter_proceed(struct osmtpd_ctx *ctx) { if (ctx->version_major == 0 && ctx->version_minor < 5) io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "proceed\n", ctx->token, ctx->reqid); else io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "proceed\n", ctx->reqid, ctx->token); } void osmtpd_filter_reject(struct osmtpd_ctx *ctx, int code, const char *reason, ...) { va_list ap; if (code < 200 || code > 599) osmtpd_errx(1, "Invalid reject code"); if (ctx->version_major == 0 && ctx->version_minor < 5) io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "reject|%d ", ctx->token, ctx->reqid, code); else io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "reject|%d ", ctx->reqid, ctx->token, code); va_start(ap, reason); io_vprintf(io_stdout, reason, ap); va_end(ap); io_printf(io_stdout, "\n"); } void osmtpd_filter_reject_enh(struct osmtpd_ctx *ctx, int code, int class, int subject, int detail, const char *reason, ...) { va_list ap; if (code < 200 || code > 599) osmtpd_errx(1, "Invalid reject code"); if (class < 2 || class > 5) osmtpd_errx(1, "Invalid enhanced status class"); if (subject < 0 || subject > 999) osmtpd_errx(1, "Invalid enhanced status subject"); if (detail < 0 || detail > 999) osmtpd_errx(1, "Invalid enhanced status detail"); if (ctx->version_major == 0 && ctx->version_minor < 5) io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "reject|%d %d.%d.%d ", ctx->token, ctx->reqid, code, class, subject, detail); else io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "reject|%d %d.%d.%d ", ctx->reqid, ctx->token, code, class, subject, detail); va_start(ap, reason); io_vprintf(io_stdout, reason, ap); va_end(ap); io_printf(io_stdout, "\n"); } void osmtpd_filter_disconnect(struct osmtpd_ctx *ctx, const char *reason, ...) { va_list ap; if (ctx->version_major == 0 && ctx->version_minor < 5) io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "disconnect|421 ", ctx->token, ctx->reqid); else io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "disconnect|421 ", ctx->reqid, ctx->token); va_start(ap, reason); io_vprintf(io_stdout, reason, ap); va_end(ap); io_printf(io_stdout, "\n"); } void osmtpd_filter_disconnect_enh(struct osmtpd_ctx *ctx, int class, int subject, int detail, const char *reason, ...) { va_list ap; if (class <= 2 || class >= 5) osmtpd_errx(1, "Invalid enhanced status class"); if (subject < 0 || subject > 999) osmtpd_errx(1, "Invalid enhanced status subject"); if (detail < 0 || detail > 999) osmtpd_errx(1, "Invalid enhanced status detail"); if (ctx->version_major == 0 && ctx->version_minor < 5) io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "disconnect|421 %d.%d.%d ", ctx->token, ctx->reqid, class, subject, detail); else io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "disconnect|421 %d.%d.%d ", ctx->reqid, ctx->token, class, subject, detail); va_start(ap, reason); io_vprintf(io_stdout, reason, ap); va_end(ap); io_printf(io_stdout, "\n"); } void osmtpd_filter_rewrite(struct osmtpd_ctx *ctx, const char *value, ...) { va_list ap; if (ctx->version_major == 0 && ctx->version_minor < 5) io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "rewrite|", ctx->token, ctx->reqid); else io_printf(io_stdout, "filter-result|%016"PRIx64"|%016"PRIx64"|" "rewrite|", ctx->reqid, ctx->token); va_start(ap, value); io_vprintf(io_stdout, value, ap); va_end(ap); io_printf(io_stdout, "\n"); } void osmtpd_filter_dataline(struct osmtpd_ctx *ctx, const char *line, ...) { va_list ap; if (ctx->version_major == 0 && ctx->version_minor < 5) io_printf(io_stdout, "filter-dataline|%016"PRIx64"|%016"PRIx64"|", ctx->token, ctx->reqid); else io_printf(io_stdout, "filter-dataline|%016"PRIx64"|%016"PRIx64"|", ctx->reqid, ctx->token); va_start(ap, line); io_vprintf(io_stdout, line, ap); va_end(ap); io_printf(io_stdout, "\n"); } static void osmtpd_register(enum osmtpd_type type, enum osmtpd_phase phase, int incoming, int storereport, void *cb) { size_t i; if (ready) osmtpd_errx(1, "Can't register when proc is running"); for (i = 0; i < NITEMS(osmtpd_callbacks); i++) { if (type == osmtpd_callbacks[i].type && phase == osmtpd_callbacks[i].phase && incoming == osmtpd_callbacks[i].incoming) { if (osmtpd_callbacks[i].cb != NULL && cb != NULL) osmtpd_errx(1, "Event already registered"); if (cb != NULL) osmtpd_callbacks[i].cb = cb; osmtpd_callbacks[i].doregister = 1; if (storereport) osmtpd_callbacks[i].storereport = 1; return; } } /* NOT REACHED */ osmtpd_errx(1, "Trying to register unknown event"); } static enum osmtpd_phase osmtpd_strtophase(const char *phase, const char *linedup) { if (strcmp(phase, "connect") == 0) return OSMTPD_PHASE_CONNECT; if (strcmp(phase, "helo") == 0) return OSMTPD_PHASE_HELO; if (strcmp(phase, "ehlo") == 0) return OSMTPD_PHASE_EHLO; if (strcmp(phase, "starttls") == 0) return OSMTPD_PHASE_STARTTLS; if (strcmp(phase, "auth") == 0) return OSMTPD_PHASE_AUTH; if (strcmp(phase, "mail-from") == 0) return OSMTPD_PHASE_MAIL_FROM; if (strcmp(phase, "rcpt-to") == 0) return OSMTPD_PHASE_RCPT_TO; if (strcmp(phase, "data") == 0) return OSMTPD_PHASE_DATA; if (strcmp(phase, "data-line") == 0) return OSMTPD_PHASE_DATA_LINE; if (strcmp(phase, "rset") == 0) return OSMTPD_PHASE_RSET; if (strcmp(phase, "quit") == 0) return OSMTPD_PHASE_QUIT; if (strcmp(phase, "noop") == 0) return OSMTPD_PHASE_NOOP; if (strcmp(phase, "help") == 0) return OSMTPD_PHASE_HELP; if (strcmp(phase, "wiz") == 0) return OSMTPD_PHASE_WIZ; if (strcmp(phase, "commit") == 0) return OSMTPD_PHASE_COMMIT; if (strcmp(phase, "link-connect") == 0) return OSMTPD_PHASE_LINK_CONNECT; if (strcmp(phase, "link-disconnect") == 0) return OSMTPD_PHASE_LINK_DISCONNECT; if (strcmp(phase, "link-greeting") == 0) return OSMTPD_PHASE_LINK_GREETING; if (strcmp(phase, "link-identify") == 0) return OSMTPD_PHASE_LINK_IDENTIFY; if (strcmp(phase, "link-tls") == 0) return OSMTPD_PHASE_LINK_TLS; if (strcmp(phase, "tx-begin") == 0) return OSMTPD_PHASE_TX_BEGIN; if (strcmp(phase, "tx-mail") == 0) return OSMTPD_PHASE_TX_MAIL; if (strcmp(phase, "tx-rcpt") == 0) return OSMTPD_PHASE_TX_RCPT; if (strcmp(phase, "tx-envelope") == 0) return OSMTPD_PHASE_TX_ENVELOPE; if (strcmp(phase, "tx-data") == 0) return OSMTPD_PHASE_TX_DATA; if (strcmp(phase, "tx-commit") == 0) return OSMTPD_PHASE_TX_COMMIT; if (strcmp(phase, "tx-rollback") == 0) return OSMTPD_PHASE_TX_ROLLBACK; if (strcmp(phase, "protocol-client") == 0) return OSMTPD_PHASE_PROTOCOL_CLIENT; if (strcmp(phase, "protocol-server") == 0) return OSMTPD_PHASE_PROTOCOL_SERVER; if (strcmp(phase, "filter-response") == 0) return OSMTPD_PHASE_FILTER_RESPONSE; if (strcmp(phase, "timeout") == 0) return OSMTPD_PHASE_TIMEOUT; osmtpd_errx(1, "Invalid line received: invalid phase: %s", linedup); } static const char * osmtpd_typetostr(enum osmtpd_type type) { switch (type) { case OSMTPD_TYPE_FILTER: return "filter"; case OSMTPD_TYPE_REPORT: return "report"; } osmtpd_errx(1, "In valid type: %d\n", type); } static const char * osmtpd_phasetostr(enum osmtpd_phase phase) { switch (phase) { case OSMTPD_PHASE_CONNECT: return "connect"; case OSMTPD_PHASE_HELO: return "helo"; case OSMTPD_PHASE_EHLO: return "ehlo"; case OSMTPD_PHASE_STARTTLS: return "starttls"; case OSMTPD_PHASE_AUTH: return "auth"; case OSMTPD_PHASE_MAIL_FROM: return "mail-from"; case OSMTPD_PHASE_RCPT_TO: return "rcpt-to"; case OSMTPD_PHASE_DATA: return "data"; case OSMTPD_PHASE_DATA_LINE: return "data-line"; case OSMTPD_PHASE_RSET: return "rset"; case OSMTPD_PHASE_QUIT: return "quit"; case OSMTPD_PHASE_NOOP: return "noop"; case OSMTPD_PHASE_HELP: return "help"; case OSMTPD_PHASE_WIZ: return "wiz"; case OSMTPD_PHASE_COMMIT: return "commit"; case OSMTPD_PHASE_LINK_CONNECT: return "link-connect"; case OSMTPD_PHASE_LINK_DISCONNECT: return "link-disconnect"; case OSMTPD_PHASE_LINK_GREETING: return "link-greeting"; case OSMTPD_PHASE_LINK_IDENTIFY: return "link-identify"; case OSMTPD_PHASE_LINK_TLS: return "link-tls"; case OSMTPD_PHASE_TX_BEGIN: return "tx-begin"; case OSMTPD_PHASE_TX_MAIL: return "tx-mail"; case OSMTPD_PHASE_TX_RCPT: return "tx-rcpt"; case OSMTPD_PHASE_TX_ENVELOPE: return "tx-envelope"; case OSMTPD_PHASE_TX_DATA: return "tx-data"; case OSMTPD_PHASE_TX_COMMIT: return "tx-commit"; case OSMTPD_PHASE_TX_ROLLBACK: return "tx-rollback"; case OSMTPD_PHASE_PROTOCOL_CLIENT: return "protocol-client"; case OSMTPD_PHASE_PROTOCOL_SERVER: return "protocol-server"; case OSMTPD_PHASE_FILTER_RESPONSE: return "filter-response"; case OSMTPD_PHASE_TIMEOUT: return "timeout"; } osmtpd_errx(1, "In valid phase: %d\n", phase); } static enum osmtpd_status osmtpd_strtostatus(const char *status, char *linedup) { if (strcmp(status, "ok") == 0) return OSMTPD_STATUS_OK; else if (strcmp(status, "tempfail") == 0) return OSMTPD_STATUS_TEMPFAIL; else if (strcmp(status, "permfail") == 0) return OSMTPD_STATUS_PERMFAIL; osmtpd_errx(1, "Invalid line received: invalid status: %s\n", linedup); } static void osmtpd_addrtoss(char *addr, struct sockaddr_storage *ss, int hasport, char *linedup) { char *port = NULL; const char *errstr = NULL; struct sockaddr_in *sin; struct sockaddr_in6 *sin6; struct sockaddr_un *sun; size_t n; if (addr[0] == '[') { sin6 = (struct sockaddr_in6 *)ss; sin6->sin6_family = AF_INET6; sin6->sin6_port = 0; if (hasport) { if ((port = strrchr(addr, ':')) == NULL) osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); if (port[-1] != ']') osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); port++; sin6->sin6_port = htons(strtonum(port, 0, UINT16_MAX, &errstr)); if (errstr != NULL) osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); port[-2] = '\0'; } else { n = strlen(addr); if (addr[n - 1] != ']') osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); addr[n - 1] = '\0'; } switch (inet_pton(AF_INET6, addr + 1, &(sin6->sin6_addr))) { case 1: break; case 0: if (hasport) port[-2] = ']'; else addr[n - 1] = ']'; osmtpd_errx(1, "Invalid line received: invalid address " "(%s): %s", addr, linedup); default: if (hasport) port[-2] = ']'; else addr[n - 1] = ']'; osmtpd_err(1, "Can't parse address (%s): %s", addr, linedup); } } else if (strncasecmp(addr, "unix:", 5) == 0) { sun = (struct sockaddr_un *)ss; sun->sun_family = AF_UNIX; if (strlcpy(sun->sun_path, addr, sizeof(sun->sun_path)) >= sizeof(sun->sun_path)) { osmtpd_errx(1, "Invalid line received: address too " "long (%s): %s", addr, linedup); } } else { sin = (struct sockaddr_in *)ss; sin->sin_family = AF_INET; sin->sin_port = 0; if (hasport) { if ((port = strrchr(addr, ':')) == NULL) osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); port++; sin->sin_port = htons(strtonum(port, 0, UINT16_MAX, &errstr)); if (errstr != NULL) osmtpd_errx(1, "Invalid line received: invalid " "address (%s): %s", addr, linedup); port[-1] = '\0'; } switch (inet_pton(AF_INET, addr, &(sin->sin_addr))) { case 1: break; case 0: if (hasport) port[-1] = ':'; osmtpd_errx(1, "Invalid line received: invalid address " "(%s): %s", addr, linedup); default: if (hasport) port[-1] = ':'; osmtpd_err(1, "Can't parse address (%s): %s", addr, linedup); } } } static int osmtpd_session_cmp(struct osmtpd_session *a, struct osmtpd_session *b) { return a->ctx.reqid < b->ctx.reqid ? -1 : a->ctx.reqid > b->ctx.reqid; } RB_GENERATE_STATIC(osmtpd_sessions, osmtpd_session, entry, osmtpd_session_cmp); libopensmtpd-0.7/opensmtpd.h000066400000000000000000000151371405566102200162520ustar00rootroot00000000000000/* * Copyright (c) 2019 Martijn van Duren * * 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 #ifndef __dead #define __dead __attribute__((__noreturn__)) #endif #ifndef __unused #define __unused __attribute__((unused)) #endif enum osmtpd_status { OSMTPD_STATUS_OK, OSMTPD_STATUS_TEMPFAIL, OSMTPD_STATUS_PERMFAIL }; enum osmtpd_type { OSMTPD_TYPE_FILTER, OSMTPD_TYPE_REPORT }; enum osmtpd_phase { OSMTPD_PHASE_CONNECT, OSMTPD_PHASE_HELO, OSMTPD_PHASE_EHLO, OSMTPD_PHASE_STARTTLS, OSMTPD_PHASE_AUTH, OSMTPD_PHASE_MAIL_FROM, OSMTPD_PHASE_RCPT_TO, OSMTPD_PHASE_DATA, OSMTPD_PHASE_DATA_LINE, OSMTPD_PHASE_RSET, OSMTPD_PHASE_QUIT, OSMTPD_PHASE_NOOP, OSMTPD_PHASE_HELP, OSMTPD_PHASE_WIZ, OSMTPD_PHASE_COMMIT, OSMTPD_PHASE_LINK_CONNECT, OSMTPD_PHASE_LINK_DISCONNECT, OSMTPD_PHASE_LINK_GREETING, OSMTPD_PHASE_LINK_IDENTIFY, OSMTPD_PHASE_LINK_TLS, OSMTPD_PHASE_TX_BEGIN, OSMTPD_PHASE_TX_MAIL, OSMTPD_PHASE_TX_RCPT, OSMTPD_PHASE_TX_ENVELOPE, OSMTPD_PHASE_TX_DATA, OSMTPD_PHASE_TX_COMMIT, OSMTPD_PHASE_TX_ROLLBACK, OSMTPD_PHASE_PROTOCOL_CLIENT, OSMTPD_PHASE_PROTOCOL_SERVER, OSMTPD_PHASE_FILTER_RESPONSE, OSMTPD_PHASE_TIMEOUT }; #define OSMTPD_NEED_SRC 1 << 0 #define OSMTPD_NEED_DST 1 << 1 #define OSMTPD_NEED_RDNS 1 << 2 #define OSMTPD_NEED_FCRDNS 1 << 3 #define OSMTPD_NEED_IDENTITY 1 << 4 #define OSMTPD_NEED_GREETING 1 << 5 #define OSMTPD_NEED_CIPHERS 1 << 6 #define OSMTPD_NEED_MSGID 1 << 7 #define OSMTPD_NEED_MAILFROM 1 << 8 #define OSMTPD_NEED_RCPTTO 1 << 9 #define OSMTPD_NEED_EVPID 1 << 10 struct osmtpd_ctx { enum osmtpd_type type; enum osmtpd_phase phase; int version_major; int version_minor; struct timespec tm; int incoming; uint64_t reqid; uint64_t token; struct sockaddr_storage src; struct sockaddr_storage dst; char *rdns; enum osmtpd_status fcrdns; /* HELO/EHLO identity */ char *identity; struct greeting { char *identity; /* textstring not supplied by smtpd */ } greeting; char *ciphers; uint32_t msgid; char *mailfrom; char **rcptto; uint64_t evpid; void *local_session; void *local_message; }; void osmtpd_register_conf(void (*)(const char *, const char *)); void osmtpd_register_filter_connect(void (*)(struct osmtpd_ctx *, const char *, struct sockaddr_storage *)); void osmtpd_register_filter_helo(void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_filter_ehlo(void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_filter_starttls(void (*)(struct osmtpd_ctx *)); void osmtpd_register_filter_auth(void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_filter_mailfrom(void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_filter_rcptto(void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_filter_data(void (*)(struct osmtpd_ctx *)); void osmtpd_register_filter_dataline(void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_filter_rset(void (*)(struct osmtpd_ctx *)); void osmtpd_register_filter_quit(void (*)(struct osmtpd_ctx *)); void osmtpd_register_filter_noop(void (*)(struct osmtpd_ctx *)); void osmtpd_register_filter_help(void (*)(struct osmtpd_ctx *)); void osmtpd_register_filter_wiz(void (*)(struct osmtpd_ctx *)); void osmtpd_register_filter_commit(void (*)(struct osmtpd_ctx *)); void osmtpd_register_report_connect(int, void (*)(struct osmtpd_ctx *, const char *, enum osmtpd_status, struct sockaddr_storage *, struct sockaddr_storage *)); void osmtpd_register_report_disconnect(int, void (*)(struct osmtpd_ctx *)); void osmtpd_register_report_greeting(int, void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_report_identify(int, void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_report_tls(int, void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_report_begin(int, void (*)(struct osmtpd_ctx *, uint32_t)); void osmtpd_register_report_mail(int, void (*)(struct osmtpd_ctx *, uint32_t, const char *, enum osmtpd_status)); void osmtpd_register_report_rcpt(int, void (*)(struct osmtpd_ctx *, uint32_t, const char *, enum osmtpd_status)); void osmtpd_register_report_envelope(int, void (*)(struct osmtpd_ctx *, uint32_t, uint64_t)); void osmtpd_register_report_data(int, void (*)(struct osmtpd_ctx *, uint32_t, enum osmtpd_status)); void osmtpd_register_report_commit(int, void (*)(struct osmtpd_ctx *, uint32_t, size_t)); void osmtpd_register_report_rollback(int, void (*)(struct osmtpd_ctx *, uint32_t)); void osmtpd_register_report_client(int, void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_report_server(int, void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_report_response(int, void (*)(struct osmtpd_ctx *, const char *)); void osmtpd_register_report_timeout(int, void (*)(struct osmtpd_ctx *)); void osmtpd_local_session(void *(*)(struct osmtpd_ctx *), void (*)(struct osmtpd_ctx *, void *)); void osmtpd_local_message(void *(*)(struct osmtpd_ctx *), void (*)(struct osmtpd_ctx *, void *)); void osmtpd_need(int); void osmtpd_filter_proceed(struct osmtpd_ctx *); void osmtpd_filter_reject(struct osmtpd_ctx *, int, const char *, ...) __attribute__((__format__ (printf, 3, 4))); void osmtpd_filter_reject_enh(struct osmtpd_ctx *, int, int, int, int, const char *, ...) __attribute__((__format__ (printf, 6, 7))); void osmtpd_filter_disconnect(struct osmtpd_ctx *, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void osmtpd_filter_disconnect_enh(struct osmtpd_ctx *, int, int, int, const char *, ...) __attribute__((__format__ (printf, 5, 6))); void osmtpd_filter_rewrite(struct osmtpd_ctx *, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void osmtpd_filter_dataline(struct osmtpd_ctx *, const char *, ...) __attribute__((__format__ (printf, 2, 3))); void osmtpd_run(void); __dead void osmtpd_err(int eval, const char *fmt, ...); __dead void osmtpd_errx(int eval, const char *fmt, ...); libopensmtpd-0.7/osmtpd_run.3000066400000000000000000000342511405566102200163440ustar00rootroot00000000000000.\" $OpenBSD$ .\" .\" Copyright (c) 2019 Martijn van Duren .\" .\" 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. .\" .Dd $Mdocdate$ .Dt OSMTPD_RUN 3 .Os .Sh NAME .Nm osmtpd_register_filter_connect , .Nm osmtpd_register_filter_helo , .Nm osmtpd_register_filter_ehlo , .Nm osmtpd_register_filter_starttls , .Nm osmtpd_register_filter_auth , .Nm osmtpd_register_filter_mailfrom , .Nm osmtpd_register_filter_rcptto , .Nm osmtpd_register_filter_data , .Nm osmtpd_register_filter_dataline , .Nm osmtpd_register_filter_rset , .Nm osmtpd_register_filter_quit , .Nm osmtpd_register_filter_noop , .Nm osmtpd_register_filter_help , .Nm osmtpd_register_filter_wiz , .Nm osmtpd_register_filter_commit , .Nm osmtpd_register_report_connect , .Nm osmtpd_register_report_disconnect , .Nm osmtpd_register_report_identify , .Nm osmtpd_register_report_tls , .Nm osmtpd_register_report_begin , .Nm osmtpd_register_report_mail , .Nm osmtpd_register_report_rcpt , .Nm osmtpd_register_report_envelope , .Nm osmtpd_register_report_data , .Nm osmtpd_register_report_commit , .Nm osmtpd_register_report_rollback , .Nm osmtpd_register_report_client , .Nm osmtpd_register_report_server , .Nm osmtpd_register_report_response , .Nm osmtpd_register_report_timeout , .Nm osmtpd_local_session , .Nm osmtpd_local_message , .Nm osmtpd_need , .Nm osmtpd_filter_proceed , .Nm osmtpd_filter_reject , .Nm osmtpd_filter_disconnect , .Nm osmtpd_filter_rewrite , .Nm osmtpd_filter_dataline , .Nm osmtpd_run , .Nm osmtpd_err , .Nm osmtpd_errx .Nd C filter API for .Xr smtpd 8 .Sh SYNOPSIS .In opensmtpd.h .Ft void .Fo osmtpd_register_filter_connect .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *hostname, struct sockaddr_storage *ss)" .Fc .Ft void .Fo osmtpd_register_filter_helo .Fa "void (cb*)(struct osmtpd_ctx *ctx, const char *helo)" .Fc .Ft void .Fo osmtpd_register_filter_ehlo .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *ehlo)" .Fc .Ft void .Fo osmtpd_register_filter_starttls .Fa "void (*cb)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_register_filter_auth .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *auth)" .Fc .Ft void .Fo osmtpd_register_filter_mailfrom .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *from)" .Fc .Ft void .Fo osmtpd_register_filter_rcptto .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *rcpt)" .Fc .Ft void .Fo osmtpd_register_filter_data .Fa "void (*cb)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_register_filter_dataline .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *line)" .Fc .Ft void .Fo osmtpd_register_filter_rset .Fa "void (*cb)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_register_filter_quit .Fa "void (*cb)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_register_filter_noop .Fa "void (*cb)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_register_filter_help .Fa "void (*cb)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_register_filter_wiz .Fa "void (*cb)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_register_filter_commit .Fa "void (*cb)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_register_report_connect .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *rdns, enum osmtpd_status fcrdns, struct sockaddr_storage *src, struct sockaddr_storage *dst)" .Fc .Ft void .Fo osmtpd_register_report_disconnect .Fa "int incoming" .Fa "void (*ctx)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_register_report_identify .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *identity)" .Fc .Ft void .Fo osmtpd_register_report_tls .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *ciphers)" .Fc .Ft void .Fo osmtpd_register_report_begin .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid)" .Fc .Ft void .Fo osmtpd_register_report_mail .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, const char *mailfrom, enum osmtpd_status status)" .Fc .Ft void .Fo osmtpd_register_report_rcpt .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, const char *rcptto, enum osmtpd_status status)" .Fc .Ft void .Fo osmtpd_register_report_envelope .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, uint64_t evpid)" .Fc .Ft void .Fo osmtpd_register_report_data .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, enum osmtpd_status status)" .Fc .Ft void .Fo osmtpd_register_report_commit .Fa int incoming .Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, size_t msgsz)" .Fc .Ft void .Fo osmtpd_register_report_rollback .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid)" .Fc .Ft void .Fo osmtpd_register_report_client .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *cmd)" .Fc .Ft void .Fo osmtpd_register_report_server .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *resp)" .Fc .Ft void .Fo osmtpd_register_report_response .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *resp)" .Fc .Ft void .Fo osmtpd_register_report_timeout .Fa "int incoming" .Fa "void (*cb)(struct osmtpd_ctx *ctx)" .Fc .Ft void .Fo osmtpd_local_session .Fa "void *(*oncreate)(struct osmtpd_ctx *ctx)" .Fa "void (*ondelete)(struct osmtpd_ctx *ctx, void *data)" .Fc .Ft void .Fo osmtpd_local_message .Fa "void *(*oncreate)(struct osmtpd_ctx *ctx)" .Fa "void (*ondelete)(struct osmtpd_ctx *ctx, void *data)" .Fc .Ft void .Fn osmtpd_need "int needs" .Ft void .Fn osmtpd_filter_proceed "struct osmtpd_ctx *ctx" .Ft void .Fn osmtpd_filter_reject "struct osmtpd_ctx *ctx" "int error" "const char *msg" ... .Ft void .Fn osmtpd_filter_disconnect "struct osmtpd_ctx *ctx" "const char *msg" ... .Ft void .Fn osmtpd_filter_rewrite "struct osmtpd_ctx *ctx" "const char *value" ... .Ft void .Fn osmtpd_filter_dataline "struct osmtpd_ctx *ctx" "const char *line" ... .Ft void .Fn osmtpd_run void .Ft void .Fn osmtpd_err "int eval" "const char *fmt" ... .Ft void .Fn osmtpd_errx "int eval" "const char *fmt" ... .Sh DESCRIPTION The .Nm osmtpd API is an event based interface for writing .Xr smtpd 8 filters. Filter and report callbacks are registered via the .Nm osmtpd_register class of functions, followed by .Nm osmtpd_run . .Pp .Nm osmtpd_run starts the communication with the server and transforms network queries to callbacks. Internally it uses .Xr event_dispatch 3 , which allows filters to be written fully asynchronously. .Pp Each callback .Fa cb gets at least a pointer of the type .Fa struct osmtpd_ctx . It contains the following elements: .Bl -tag -width Ds .It Fa "enum osmtpd_type type" The type of request being made. The possible values are .Dv OSMTPD_TYPE_FILTER and .Dv OSMTPD_TYPE_REPORT . .It Vt "enum osmtpd_phase" Va phase The phase in the transaction which triggered the callback. The following values match their respective .Nm osmtpd_register function: .Bl -tag -compact -width OSMTPD_PHASE_LINK_DISCONNECT .It Dv OSMTPD_PHASE_CONNECT .Nm osmtpd_register_filter_connect .It Dv OSMTPD_PHASE_HELO .Nm osmtpd_register_filter_helo .It Dv OSMTPD_PHASE_EHLO .Nm osmtpd_register_filter_ehlo .It Dv OSMTPD_PHASE_STARTTLS .Nm osmtpd_register_filter_starttls .It Dv OSMTPD_PHASE_AUTH .Nm osmtpd_register_filter_auth .It Dv OSMTPD_PHASE_MAIL_FROM .Nm osmtpd_register_filter_mailfrom .It Dv OSMTPD_PHASE_RCPT_TO .Nm osmtpd_register_filter_rcptto .It Dv OSMTPD_PHASE_DATA .Nm osmtpd_register_filter_data .It Dv OSMTPD_PHASE_DATA_LINE .Nm osmtpd_register_filter_dataline .It Dv OSMTPD_PHASE_RSET .Nm osmtpd_register_filter_rset .It Dv OSMTPD_PHASE_QUIT .Nm osmtpd_register_filter_quit .It Dv OSMTPD_PHASE_NOOP .Nm osmtpd_register_filter_noop .It Dv OSMTPD_PHASE_HELP .Nm osmtpd_register_filter_help .It Dv OSMTPD_PHASE_WIZ .Nm osmtpd_register_filter_wiz .It Dv OSMTPD_PHASE_COMMIT .Nm osmtpd_register_filter_commit .It Dv OSMTPD_PHASE_LINK_CONNECT .Nm osmtpd_register_report_connect .It OSMTPD_PHASE_LINK_DISCONNECT .Nm osmtpd_register_report_disconnect .It Dv OSMTPD_PHASE_LINK_IDENTIFY .Nm osmtpd_register_report_identify .It Dv OSMTPD_PHASE_LINK_TLS .Nm osmtpd_register_report_tls .It Dv OSMTPD_PHASE_TX_BEGIN .Nm osmtpd_register_report_begin .It Dv OSMTPD_PHASE_TX_MAIL .Nm osmtpd_register_report_mail .It Dv OSMTPD_PHASE_TX_RCPT .Nm osmtpd_register_report_rcpt .It Dv OSMTPD_PHASE_TX_ENVELOPE .Nm osmtpd_register_report_envelope .It Dv OSMTPD_PHASE_TX_DATA .Nm osmtpd_register_report_data .It Dv OSMTPD_PHASE_TX_COMMIT .Nm osmtpd_register_report_commit .It Dv OSMTPD_PHASE_TX_ROLLBACK .Nm osmtpd_register_report_rollback .It Dv OSMTPD_PHASE_PROTOCOL_CLIENT .Nm osmtpd_register_report_client .It Dv OSMTPD_PHASE_PROTOCOL_SERVER .Nm osmtpd_register_report_server .It Dv OSMTPD_PHASE_FILTER_RESPONSE .Nm osmtpd_register_report_response .It Dv OSMTPD_PHASE_TIMEOUT . .Nm osmtpd_register_report_timeout .El .It Vt int Va version_major The major version number of the protocol. Most filters don't need this information. .It Vt int Va version_minor The minor version number of the protocol. Most filters don't need this information. .It Vt "struct timespec" Va tm The time the event was triggered inside .Xr smtpd 8 . .It Vt int Va incoming Set to 1 if the event was based on an incoming connection, 0 if it's an outgoing connection. The .Nm osmtpd_register_filter class of functions is always based on incoming connections. .Nm osmtpd_register_report can be both incoming and outgoing. .It Vt uint64_t Va reqid The request ID of the connection the event was issued on. This value can be useful for logging. Filters in need of filter specific data can use .Nm osmtpd_local_session and .Va local_session . .It Vt uint64_t Va token The filter specific token. Most filters don't need this information. .It Vt "struct sockaddr_storage" Va src The source address and port of the connection. This needs to be cast to the appropriate sockaddr type based on the .Va ss_family attribute. It can have the following families: .Dv AF_INET , .Dv AF_INET6 and .Dv AF_UNIX . To use this attribute, initialize .Nm osmtpd_need with .Dv OSMTPD_NEED_SRC . If not available the entire attribute is zeroed out. .It Vt "struct sockaddr_storage" Va dst The destination address and port of the connection. This needs to be cast to the appropriate sockaddr type based on the .Va ss_family attribute. It can have the following families: .Dv AF_INET , .Dv AF_INET6 and .Dv AF_UNIX . To use this attribute, initialize .Nm osmtpd_need with .Dv OSMTPD_NEED_DST . If not available the entire attribute is zeroed out. .It Vt char Va *rdns The reverse DNS hostname of the connection. To use this attribute, initialize .Nm osmtpd_need with .Dv OSMTPD_NEED_RDNS . If not available the attribute is set to .Dv NULL . .It Vt enum osmtpd_status Va fcrdns Whether the reverse DNS hostname is forward confirmed. To use this attribute, initialize .Nm osmtpd_need with .Dv OSMTPD_NEED_FCRDNS . If not available the attribute is set to .Dv OSMTPD_STATUS_TEMPFAIL . .It Vt char Va *identity The identity of the remote host as presented by the HELO or EHLO SMTP command. To use this attribute, initialize .Nm osmtpd_need with .Dv OSMTPD_NEED_IDENTITY . If not available the attribute is set to .Dv NULL . .It Vt char Va *ciphers The ciphers used during .Po start Pc Ns tls . To use this attribute, initialize .Nm osmtpd_need with .Dv OSMTPD_NEED_CIPHERS . If not available the attribute is set to .Dv NULL . .It Vt uint32_t Va msgid The message ID of the current message being handled in the SMTP transaction. This value can be useful for logging. .Nm osmtpd_need needs to be initialized with .Dv OSMTPD_NEED_MSGID . If not available the attribute is set to .Dv 0 . Filters in need of filter specific data can use .Nm osmtpd_local_message and .Va local_message . .It Vt char Va *mailfrom The envelope MAIL FROM address in the SMTP transaction. .Nm osmtpd_need needs to be initialized with .Dv OSMTPD_NEED_MAILFROM . If not available the attribute is set to .Dv NULL . .It Vt char Va **rcptto The envelope RCPT TO address in the SMTP transaction. .Nm osmtpd_need needs to be initialized with .Dv OSMTPD_NEED_RCPTTO . This attribute is a NULL-terminated array of address strings. If not available the first element in the array is set to .Dv NULL . .It Vt uint64_t Va evpid The envelope ID we're currently working on. .Nm osmtpd_need needs to be initialized with .Dv OSMTPD_NEED_EVPID . If not available the attribute is set to .Dv 0 . .It Vt void Va *local_session Any filter specific data that needs to be stored during the session. This is initialized on .Fa ctx creation by calling .Fa oncreate argument from .Nm osmtpd_local_session . .It Vt void Va *local_message Any filter specific data that needs to be stored during the message transaction. This is initialized on .Fa ctx creation by calling .Fa oncreate argument from .Nm osmtpd_local_message . .El .Pp The .Nm osmtpd_register_filter class of functions must call one of .Nm osmtpd_filter_proceed , .Nm osmtpd_filter_rewrite , .Nm osmtpd_filter_reject and .Nm osmtpd_filter_disconnect . This can be done either in the callback function itself, or at a later moment through another callback. Note that the session stalls until the .Nm osmtpd_filter has been called. .Pp Exceptions to the above reply options are: .Pp .Bl -bullet -compact -width Ds .It .Nm osmtpd_register_filter_connect Ns 's and .Nm osmtpd_register_filter functions' callbacks without argument can't use .Nm osmtpd_filter_rewrite . .It .Nm osmtpd_register_filter_dataline Ns 's callback can only use osmtpd_filter_dataline. .El .Pp .Nm osmtpd_err and .Nm osmtpd_errx can be used as a standin for .Xr err 3 and .Xr errx 3 without printing the program name to stderr. .Sh SEE ALSO .Xr event_init 3 , .Xr smtpd.conf 5 .Sh HISTORY The .Nm osmtpd_run API first appeared in .Ox 6.6 . .Sh AUTHORS .An Martijn van Duren Aq Mt martijn@openbsd.org libopensmtpd-0.7/shlib_version000066400000000000000000000000201405566102200166420ustar00rootroot00000000000000major=0 minor=1