#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include "libmilter/mfapi.h"
#include "libmilter/mfdef.h"
#ifndef true
# define false 0
# define true 1
#endif
struct mlfiPriv
{
char *mlfi_fname;
FILE *mlfi_fp;
};
#define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx))
static unsigned long mta_caps = 0;
sfsistat
mlfi_cleanup(ctx, ok)
SMFICTX *ctx;
bool ok;
{
sfsistat rstat = SMFIS_CONTINUE;
struct mlfiPriv *priv = MLFIPRIV;
char *p;
char host[512];
char hbuf[1024];
if (priv == NULL)
return rstat;
if (priv->mlfi_fp != NULL && fclose(priv->mlfi_fp) == EOF)
{
rstat = SMFIS_TEMPFAIL;
(void) unlink(priv->mlfi_fname);
}
else if (ok)
{
if (gethostname(host, sizeof host) < 0)
snprintf(host, sizeof host, "localhost");
p = strrchr(priv->mlfi_fname, '/');
if (p == NULL)
p = priv->mlfi_fname;
else
p++;
snprintf(hbuf, sizeof hbuf, "%s@%s", p, host);
smfi_addheader(ctx, "X-Archived", hbuf);
}
else
{
(void) unlink(priv->mlfi_fname);
}
free(priv->mlfi_fname);
free(priv);
smfi_setpriv(ctx, NULL);
return rstat;
}
sfsistat
mlfi_envfrom(ctx, envfrom)
SMFICTX *ctx;
char **envfrom;
{
struct mlfiPriv *priv;
int fd = -1;
priv = malloc(sizeof *priv);
if (priv == NULL)
{
return SMFIS_TEMPFAIL;
}
memset(priv, '\0', sizeof *priv);
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;
}
smfi_setpriv(ctx, priv);
return SMFIS_CONTINUE;
}
sfsistat
mlfi_header(ctx, headerf, headerv)
SMFICTX *ctx;
char *headerf;
char *headerv;
{
fprintf(MLFIPRIV->mlfi_fp, "%s: %s\r\n", headerf, headerv);
return ((mta_caps & SMFIP_NR_HDR) != 0)
? SMFIS_NOREPLY : SMFIS_CONTINUE;
}
sfsistat
mlfi_eoh(ctx)
SMFICTX *ctx;
{
fprintf(MLFIPRIV->mlfi_fp, "\r\n");
return SMFIS_CONTINUE;
}
sfsistat
mlfi_body(ctx, bodyp, bodylen)
SMFICTX *ctx;
u_char *bodyp;
size_t bodylen;
{
if (fwrite(bodyp, bodylen, 1, MLFIPRIV->mlfi_fp) <= 0)
{
(void) mlfi_cleanup(ctx, false);
return SMFIS_TEMPFAIL;
}
return SMFIS_CONTINUE;
}
sfsistat
mlfi_eom(ctx)
SMFICTX *ctx;
{
return mlfi_cleanup(ctx, true);
}
sfsistat
mlfi_close(ctx)
SMFICTX *ctx;
{
return SMFIS_ACCEPT;
}
sfsistat
mlfi_abort(ctx)
SMFICTX *ctx;
{
return mlfi_cleanup(ctx, false);
}
sfsistat
mlfi_unknown(ctx, cmd)
SMFICTX *ctx;
char *cmd;
{
return SMFIS_CONTINUE;
}
sfsistat
mlfi_data(ctx)
SMFICTX *ctx;
{
return SMFIS_CONTINUE;
}
sfsistat
mlfi_negotiate(ctx, f0, f1, f2, f3, pf0, pf1, pf2, pf3)
SMFICTX *ctx;
unsigned long f0;
unsigned long f1;
unsigned long f2;
unsigned long f3;
unsigned long *pf0;
unsigned long *pf1;
unsigned long *pf2;
unsigned long *pf3;
{
*pf0 = SMFIF_ADDHDRS;
*pf1 = SMFIP_NOCONNECT|SMFIP_NOHELO|SMFIP_NORCPT;
mta_caps = f1;
if ((mta_caps & SMFIP_NR_HDR) != 0)
*pf1 |= SMFIP_NR_HDR;
*pf2 = 0;
*pf3 = 0;
return SMFIS_CONTINUE;
}
struct smfiDesc smfilter =
{
"SampleFilter",
SMFI_VERSION,
SMFIF_ADDHDRS,
NULL,
NULL,
mlfi_envfrom,
NULL,
mlfi_header,
mlfi_eoh,
mlfi_body,
mlfi_eom,
mlfi_abort,
mlfi_close,
mlfi_unknown,
mlfi_data,
mlfi_negotiate
};
int
main(argc, argv)
int argc;
char *argv[];
{
bool setconn;
int c;
setconn = false;
while ((c = getopt(argc, argv, "p:")) != -1)
{
switch (c)
{
case 'p':
if (optarg == NULL || *optarg == '\0')
{
(void) fprintf(stderr, "Illegal conn: %s\n",
optarg);
exit(EX_USAGE);
}
(void) smfi_setconn(optarg);
setconn = true;
break;
}
}
if (!setconn)
{
fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
exit(EX_USAGE);
}
if (smfi_register(smfilter) == MI_FAILURE)
{
fprintf(stderr, "smfi_register failed\n");
exit(EX_UNAVAILABLE);
}
return smfi_main();
}