Mail Filter Example

This sample code is taken from the sendmail.org distribution, the sendmail/milter/README file:

Note that this filter may not be thread safe on some operating systems. You should check your system man pages for the functions used below to verify the functions are thread safe.

/* A trivial filter that logs all email to a file. */

#include <sts/types.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sysexits.h> #include <unistd.h> #include "libmilter/mfapi.h"

#ifndef true typedef int bool;

#define false 0

#define true 1

#endif /* ! true */

struct mlfiPriv

{

char *mlfi_fname;

FILE *mlfi_fp;

};

 

#define MLFIPRIV

((struct mlfiPriv *) smfi_getpriv(ctx))

extern sfsistat

mlfi_cleanup(SMFICTX *, bool); sfsistat mlfi_envfrom(ctx, envfrom)

SMFICTX *ctx; char **envfrom;

{

struct mlfiPriv *priv; int fd = -1;

/* allocate some private memory */ priv = malloc(sizeof *priv);

if (priv == NULL)

{

/* can't accept this message right now */ return SMFIS_TEMPFAIL;

}

memset(priv, '\0', sizeof *priv);

/* open a file to store this message */ priv->mlfi_fname = strdup("/tmp/msg.XXXXXXXX"); if (priv->mlfi_fname == NULL)

{free(priv);

return SMFIS_TEMPFAIL;

}

if ((fd = mkstemp(priv->mlfi_fname)) < 0 (priv->mlfi_fp = fdopen(fd, "w+")) == NULL)

{

if (fd >= 0)

(void) close(fd); free(priv->mlfi_fname); free(priv);

return SMFIS_TEMPFAIL;

}

/* save the private data */ smfi_setpriv(ctx, priv);

/* continue processing */ return SMFIS_CONTINUE;

}

sfsistat

mlfi_header(ctx, headerf, headerv) SMFICTX *ctx;

char *headerf; char *headerv;

{

/* write the header to the log file */ fprintf(MLFIPRIV->mlfi_fp, "%s: %s\r\n", headerf, headerv);

/* continue processing */ return SMFIS_CONTINUE;

}

sfsistat mlfi_eoh(ctx)

SMFICTX *ctx;

{

/* output the blank line between the header and the body */ fprintf(MLFIPRIV->mlfi_fp, "\r\n");

Mail Filter Example 269