Friday, April 15, 2011

Plugin for qmail-smtpd

IndiMail now has a plugin functionality for qmail-smtpd. You can write your own plugin to carry out tasks during the MAIL, RCPT or the DATA phase of SMTP.  See the man page for plugin_init(3) for details.

NAME
plugin_init() - Template for Dynamic SMTP Plugins

SYNTAX
#include smtp_plugin.h

char *from_plug(char *rip, char *from, char **mesg);

char *rcpt_plug(char *rip, char *from, char *rcpt, char **mesg);

char *data_plug(char *local, char *rip, char *rhost, char *rinfo, char **mesg);

PLUGIN *plugin_init();

typedef struct
{
int (*mail_func) (char *, char *, char **);
int (*rcpt_func) (char *, char *, char *, char **);
int (*data_func) (char *, char *, char *, char *, char **);
} PLUGIN;

DESCRIPTION
PLUGIN structure has three components: mail_func is a pointer to function to be exe‐
cuted in the SMTP MAIL session. rcpt_func is a pointer to function to be executed in
the SMTP RCPT session. data_func is a pointer to function to be executed in the SMTP
DATA session.

To write a SMTP plugin you have to write the plugin_init() function. Depending on
which phase of SMTP (MAIL, RCPT, DATA) you want to call your function, you have to
write the from_plug, rcpt_plug, data_plug functions.

The plugin_init() function can be written as below

PLUGIN *
plugin_init()
{
static PLUGIN plug;
PLUGIN *ptr;

ptr = &plug;
ptr->mail_func = from_plug;
ptr->rcpt_func = rcpt_plug;
ptr->data_func = data_plug;
return &plug;
}

To compile the plugin you can use gcc(1).

gcc -shared -rdynamic -nostartfiles -fPIC -s -O4 -o smtpd-plugin.so smtp_plugin.o

RETURN VALUE
The functions from_plug, rcpt_plug, data_plug must return 0 on success. These func‐
tions should return 1 to terminate the sesson with a message. You can set your own
message by assigning mesg variable. If you have the below function in smtpd-plugin.so

int
rcpt_plug(char *remoteip, char *from, char *rcpt, char **mesg)
{
if (!strstr(rcpt, "@yahoo.com"))
{
*mesg = "530 We are serious and don't Yahoo (#5.7.1)
return (1);
}
return (0);
}

SEE ALSO
qmail-smtpd(8), dlopen(3), gcc(1)

No comments:

IndiMail Queue Mechanism

Indimail has the ability of configuring multiple local and remote queues. A queue is a location on your hard disk where email are deposited ...