-
-
Notifications
You must be signed in to change notification settings - Fork 3
plugin_init.3
plugin_init() - Template for Dynamic SMTP Plugins
#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;
PLUGIN structure has three components: mail_func is a pointer to function to be executed 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. If you plan to write multiple plugins, define the functions as static. e.g.
static char *from_plug(char *rip, char *from, char **mesg);
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 -Wall -O2 -fPIC -fno-strict-aliasing -c smtp_plugin.c
gcc -shared -rdynamic -nostartfiles -fPIC -s -O4 -o smtpd-plugin.so smtp_plugin.o
You need to copy the plugin in /usr/usr/lib/indimail/plugins directory for it to get loaded by qmail-smtpd(8) at runtime.
If you desire to write multiple plugins, you should have your plugins as smtpd-plugin.so, smtpd-plugin0.so, smtpd-plugin1.so and so on. To ensure functions to be executed from all libraries, you should append an index to the mail_func, rcpt_func and data_func functions. eg. Your plugin_init() in smtpd-plugin0.so can be
PLUGIN *
plugin_init()
{
static PLUGIN plug;
PLUGIN *ptr;
ptr = &plug;
ptr->mail_func = from_plug_0;
ptr->rcpt_func = rcpt_plug_0;
ptr->data_func = data_plug_0;
return &plug;
}
You can test these plugins using the plugest(1) command.
The functions from_plug, rcpt_plug, data_plug must return 0 on success. These functions 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);
}
qmail-smtpd(8), dlopen(3) plugtest(1), gcc(1)