bmf-milter

bmf filter milter daemon
git clone git://r-36.net/bmf-milter
Log | Files | Refs | LICENSE

commit b3b81916fc31048e89e418a5538d1f8812814479
parent fafa0402f5075a10dd4791c4f5b924f10c7db13d
Author: Christoph Lohmann <20h@r-36.net>
Date:   Sun, 14 Jun 2020 11:53:13 +0200

Better feature selection for libmilter.

Diffstat:
bmf-milter.c | 124+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 58 insertions(+), 66 deletions(-)

diff --git a/bmf-milter.c b/bmf-milter.c @@ -16,6 +16,7 @@ #include <signal.h> #include "libmilter/mfapi.h" +#include "libmilter/mfdef.h" #include "arg.h" @@ -28,16 +29,40 @@ struct Priv #define MLFIPRIV ((struct Priv *) smfi_getpriv(ctx)) -extern sfsistat mlfi_cleanup(SMFICTX *); +sfsistat +mlfi_cleanup(SMFICTX *ctx) +{ + struct Priv *priv = MLFIPRIV; + int retcode = -1; + + fprintf(stderr, "mlfi_cleanup\n"); + + if (priv == NULL) + return SMFIS_CONTINUE; + + close(priv->execpipe[1]); + waitpid(priv->execpid, &retcode, 0); + + if (retcode == 0) { + if (smfi_addheader(ctx, "X-Spam-Flag", "YES") == MI_FAILURE) + return SMFIS_TEMPFAIL; + if (smfi_addheader(ctx, "X-BMF-Processed", + "YES") == MI_FAILURE) { + return SMFIS_TEMPFAIL; + } + } + + return SMFIS_CONTINUE; +} sfsistat -mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) +mlfi_helo(SMFICTX *ctx, char *helohost) { struct Priv *priv; char *ident; int pid; - printf("mlfi_connect(%s)\n", hostname); + fprintf(stderr, "mlfi_helo(%s)\n", helohost); priv = malloc(sizeof *priv); if (priv == NULL) @@ -75,19 +100,11 @@ mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) } sfsistat -mlfi_helo(SMFICTX *ctx, char *helohost) -{ - printf("mlfi_help\n"); - - return SMFIS_CONTINUE; -} - -sfsistat mlfi_envfrom(SMFICTX *ctx, char *argv[]) { struct Priv *priv = MLFIPRIV; - printf("mlfi_envfrom(%s)\n", argv[0]); + fprintf(stderr, "mlfi_envfrom(%s)\n", argv[0]); dprintf(priv->execpipe[1], "From: %s\n", argv[0]); @@ -99,7 +116,7 @@ mlfi_envrcpt(SMFICTX *ctx, char *argv[]) { struct Priv *priv = MLFIPRIV; - printf("mfli_envrcpt(%s)\n", argv[0]); + fprintf(stderr, "mfli_envrcpt(%s)\n", argv[0]); dprintf(priv->execpipe[1], "To: %s\n", argv[0]); @@ -111,7 +128,7 @@ mlfi_header(SMFICTX *ctx, char *headerf, char *headerv) { struct Priv *priv = MLFIPRIV; - printf("mlfi_header(%s = '%s')\n", headerf, headerv); + fprintf(stderr, "mlfi_header(%s = '%s')\n", headerf, headerv); dprintf(priv->execpipe[1], "%s: %s\n", headerf, headerv); @@ -123,9 +140,9 @@ mlfi_eoh(SMFICTX *ctx) { struct Priv *priv = MLFIPRIV; - printf("mlfi_eoh\n"); + fprintf(stderr, "mlfi_eoh\n"); - dprintf(priv->execpipe[1], "\n"); + dprintf(priv->execpipe[1], "\r\n"); return SMFIS_CONTINUE; } @@ -136,7 +153,7 @@ mlfi_body(SMFICTX *ctx, unsigned char *bodyp, size_t bodylen) struct Priv *priv = MLFIPRIV; int written; - printf("mlfi_body(%ld bytes)\n", bodylen); + fprintf(stderr, "mlfi_body(%ld bytes)\n", bodylen); for (int written = 0, rw = 0; written <= bodylen; written += rw) rw = write(priv->execpipe[1], bodyp+written, bodylen-written); @@ -147,49 +164,23 @@ mlfi_body(SMFICTX *ctx, unsigned char *bodyp, size_t bodylen) sfsistat mlfi_eom(SMFICTX *ctx) { - printf("mlfi_eom\n"); + fprintf(stderr, "mlfi_eom\n"); return mlfi_cleanup(ctx); } sfsistat mlfi_abort(SMFICTX *ctx) { - printf("mlfi_abort\n"); + fprintf(stderr, "mlfi_abort\n"); return mlfi_cleanup(ctx); } sfsistat -mlfi_cleanup(SMFICTX *ctx) -{ - struct Priv *priv = MLFIPRIV; - int retcode = -1; - - printf("mlfi_cleanup\n"); - - if (priv == NULL) - return SMFIS_CONTINUE; - - close(priv->execpipe[1]); - waitpid(priv->execpid, &retcode, 0); - - if (retcode == 0) { - if (smfi_addheader(ctx, "X-Spam-Flag", "YES") == MI_FAILURE) - return SMFIS_TEMPFAIL; - if (smfi_addheader(ctx, "X-BMF-Processed", - "YES") == MI_FAILURE) { - return SMFIS_TEMPFAIL; - } - } - - return SMFIS_CONTINUE; -} - -sfsistat mlfi_close(SMFICTX *ctx) { struct Priv *priv = MLFIPRIV; - printf("mlfi_close\n"); + fprintf(stderr, "mlfi_close\n"); if (priv != NULL) { if (priv->execpipe[1] != 0) @@ -204,20 +195,6 @@ mlfi_close(SMFICTX *ctx) } sfsistat -mlfi_unknown(SMFICTX *ctx, const char *cmd) -{ - printf("mlfi_unknown(%s)\n", cmd); - return SMFIS_CONTINUE; -} - -sfsistat -mlfi_data(SMFICTX * ctx) -{ - printf("mlfi_data\n"); - return SMFIS_CONTINUE; -} - -sfsistat mlfi_negotiate(SMFICTX *ctx, unsigned long f0, unsigned long f1, @@ -228,8 +205,23 @@ mlfi_negotiate(SMFICTX *ctx, unsigned long *pf2, unsigned long *pf3) { - printf("mfli_negotiate\n"); - return SMFIS_ALL_OPTS; + fprintf(stderr, "mlfi_negotiate\n"); + + /* milter actions */ + *pf0 = 0; + if (f0 & SMFIF_ADDHDRS) + f0 |= SMFIF_ADDHDRS; + + /* milter protocol steps */ + *pf1 = f1 & (SMFIP_NOCONNECT|SMFIP_NOUNKNOWN|SMFIP_NODATA); + if (f1 & SMFIP_HDR_LEADSPC) + *pf1 |= SMFIP_HDR_LEADSPC; + + /* future */ + *pf2 = 0; + *pf3 = 0; + + return SMFIS_CONTINUE; } struct smfiDesc smfilter = @@ -237,7 +229,7 @@ struct smfiDesc smfilter = "BMF", /* filter name */ SMFI_VERSION, /* version code -- do not change */ SMFIF_ADDHDRS, /* flags */ - mlfi_connect, /* connection info filter */ + NULL, /* connection info filter */ mlfi_helo, /* SMTP HELO command filter */ mlfi_envfrom, /* envelope sender filter */ mlfi_envrcpt, /* envelope recipient filter */ @@ -247,8 +239,8 @@ struct smfiDesc smfilter = mlfi_eom, /* end of message */ mlfi_abort, /* message aborted */ mlfi_close, /* connection cleanup */ - mlfi_unknown, /* unknown SMTP commands */ - mlfi_data, /* DATA command */ + NULL, /* unknown SMTP commands */ + NULL, /* DATA command */ mlfi_negotiate /* Once, at the start of each SMTP connection */ }; @@ -284,7 +276,7 @@ main(int argc, char *argv[]) return 1; } ARGEND; - printf("port = %s; timetout = %d;\n", port, timeout); + fprintf(stderr, "port = %s; timeout = %d;\n", port, timeout); if (smfi_setconn(port) == MI_FAILURE) { perror("smfi_setconn");