网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 邮件系统软件 > Qmail
本月文章推荐
.Procmail的基本用法.
.AMaViS一个 UNIX 下查病毒的邮件.
.SendMail上手指南.
.ExtMail Solution HOWTO for Lin.
.以Procmail-Gateway过滤寄出信件.
.用Sendmail配置你的第一台e-mail.
.为 Sendmail 建立虚拟邮件域.
.qmail Vpopmail And sqwebmail H.
.关于Postfix虚拟域的相关释疑.
.FreeBSD下架构Postfix的邮件系统.
.建设qmail服务器的病毒防护系统.
.Mutt 电子邮件快速入门指南.
.Postfix FAQ linuxaid版.
.在REDHAT9.0下安装igenus.
.中小企业服务器配置-邮件服务器.
.qmail使用系统用户作为邮件账户的.
.Postfix+ClamAV+Amavisd-new.
.qmail Vpopmail And sqwebmail H.
.DSPAM v3.4.2 README 中英文对照.
.构建全功能的邮件系统.

qmail-qfilter的使用及代码框架

发表日期:2006-12-3


前几天刚好用到,所以整理了一下, 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目录. ,
上一篇:qmail源代码分析之qmail-pop3d 人气:2854
下一篇:商用Sendmail的安全和隔离要点 人气:1970
浏览全部Qmail/Sendmail/Postf的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐