前几天刚好用到,所以整理了一下, POST上来让大家扔砖头。 最近对邮件过滤和邮件网关有点兴趣,有没有有经验兄弟交流一把?站内联系.
+++++++++++++++++++++
一. 使用
1) qmail-queue patch
2) 修改tcp.smtp文件, 设置环境变量
127.0.0.1:allow,RELAYCLIENT="",QMAILQUEUE="/var/qmail/bin/filter"
:allow,QMAILQUEUE="/var/qmail/bin/filter"
3) 生成/var/qmail/bin/filter脚本:权限4755, 属主qmailq.qmail.
脚本中的各个filter以--分隔.
#!/bin/bash
exec /var/qmail/bin/qmail-qfilter /var/qmail/qqfilter/logfilter -- /var/qmail/qqfilter/procmail 2>/tmp/procmail
#-- /var/qmail/bin/qmail-inject -n
4) 可生成一新的目录/var/qmail/qqfilter存放所有的filter programs.
二. 文档
qmail-qfilter sends the message text through each of the
filter commands named on the command line. Each filter is
run seperately, with standard input opened to the input
email, and standard output opened to a new temporary file
that will become the input to either the next filter, or
qmail-queue. Each filter on the command line in seperated
with --.
Returns 51 (out of memory), 53 (write error), or 81
(internal error) if it can't create the temporary files or
has problems executing the filters. Returns 91 (bad enve-
lope data) if it can't read or parse the envelope data.
If a filter returns anything other than 0 or 99, qmail-
qfilter returns its exit code. If a filter returns 99,
qmail-qfilter returns 0 immediately without running any
other filters. Otherwise returns the exit code of qmail-
queue.
qmail-qfilter sets QMAILUSER and QMAILHOST to the user and
host portions of the envelope sender address, and unsets
QMAILNAME. It also sets QMAILRCPTS to the list of enve-
lope recipients, each followed by a newline.
If you are using qmail-inject -n as one of the filters,
you may want to unset MAILUSER, USER, and LOGNAME by using
env -u QMAILNAME -u MAILNAME -u NAME qmail-inject -n as
the command to invoke qmail-inject. Note that some the
env command with some OS's doesn't support the -u option.
A message with an excessive number of recipients (more
than 64K bytes of recipient data on Linux) will cause exe-
cution of the filter programs to fail, and for the message
to be rejected.
三. 代码
1. 参数解析
struct command
{
char** argv;
struct command* next;
};
typedef struct command command;
command* filters;
filters = parse_args(argc-1, argv+1);
// 所有的filter programs存放到filters链表, 执行脚本中各个filter program以--分隔.
2. 邮件原文处理
/* Copy the message from FD0 to the first temporary file */
int copy_message()
将邮件原文读取到临时文件中, 文件指针指向开头, 返回tmpfd.
3. 信封地址处理
static const char* env = 0;
static size_t env_len = 0;
结构:
struct bufchain
{
size_t len;
char* buf;
struct bufchain* next;
};
/* Read the envelope from FD 1, and parse the sender address */
bool read_envelope()
{
从FD 1读取信封信息, 每读一次存放到bufchain, 最后拷贝到env中, env_len相应赋值, 释放bufchain链表.
return parse_envelope();
// 解析信封信息, 存储到环境变量QMAILUSER, QMAILHOST, QMAILRCPTS后传递给filter program
}
4. 调用filter program:
tmpfd = run_filters(filters, tmpfd);
循环filters链表, 依次执行每个filter program:
父进程生成一个临时文件fdout, 派生子进程执行filter program,
子进程使用tmpfd作为标准输入, fdout作为标准输出.
父进程等待子进程结束, 关闭tmpfd, 将该filter program的标准输出fdout的文件指针指向开头后作为新的tmpfd, 继续下一个filter program.
循环结束后返回tmpfd(所有filter program执行过后的输出).
5. 派生子进程运行qmail-queue:
子进程:
run_qmail_queue(tmpfd, envpipe);
qmail-queue关闭0, 1, envpipe[1];
qmail-queue从tmpfd读取邮件原文;
从envpipe[0]读取信封信息;
父进程:
关闭envpipe[1], 写信封信息到envpipe[0], 关闭envpipe[0];
waitpid()等待qmail-queue结束.
6. filter program例子.
见qmail-qfilter源代码samples目录. ,