qmail源代码分析之qmail-popup.c

qmail-popup也是由tcpserver或tcp-env之类的程式启动。这些程式是通过管道与qmail-popup通信的。这也是qmail的美妙之处,总观整个qmail源代码,除少量dns代码外。基本上没有使用网络编程。各个进程间大部分都是通管道通信。把监听,读写网络部分交给inetd或tcpserver来作。使得qmail代码相当容易阅读理解。

主要功能:
1.从网络读pop3命令,进行相应处理。
2.调用子进程(vchkpw或checkpassword,具体是哪一个由你在运行参数中指定,当然,仔细分析完doanddie函数后你也许就能编写自己的checkpw了,呵呵)完成检验密码,启动qmail-pop3d的工作

重要的函数是doanddie. 理解这个函数基本上就能理解qmail pop密码的检验流程。

几个程式间的关系是:

tcpserver--qmail-popup--vchkpw----认证成功-qmail-pop3d
| |
| |
<---------- 认证失败-----------+
[/code:1:1477526f27]

==========================
[code:1:1477526f27]
void die() { _exit(1); }

int saferead(fd,buf,len) int fd; char *buf; int len;
{
int r;
r = timeoutread(1200,fd,buf,len);
if (r <= 0) die();
return r;
}

int safewrite(fd,buf,len) int fd; char *buf; int len;
{
int r;
r = timeoutwrite(1200,fd,buf,len);
if (r <= 0) die();
return r;
}

char ssoutbuf[128];
substdio ssout = SUBSTDIO_FDBUF(safewrite,1,ssoutbuf,sizeof ssoutbuf);

char ssinbuf[128];
substdio ssin = SUBSTDIO_FDBUF(saferead,0,ssinbuf,sizeof ssinbuf);

void puts(s) char *s;
{
substdio_puts(%26amp;ssout,s);
}
void flush()
{
substdio_flush(%26amp;ssout);
}
void err(s) char *s;
{
puts("-ERR ");
puts(s);
puts("\r\n");
flush();
}

void die_usage() { err("usage: popup hostname subprogram"); die(); }
void die_nomem() { err("out of memory"); die(); }
void die_pipe() { err("unable to open pipe"); die(); }
void die_write() { err("unable to write pipe"); die(); }
void die_fork() { err("unable to fork"); die(); }
void die_childcrashed() { err("aack, child crashed"); }
void die_badauth() { err("authorization failed"); }

void err_syntax() { err("syntax error"); }
void err_wantuser() { err("USER first"); }
void err_authoriz() { err("authorization first"); }

void okay() { puts("+OK \r\n"); flush(); }
void pop3_quit() { okay(); die(); }

//FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */
char unique[FMT_ULONG + FMT_ULONG + 3];
char *hostname;
stralloc username = {0};
int seenuser = 0;
char **childargs;
substdio ssup;
char upbuf[128];



void doanddie(user,userlen,pass)
char *user;
unsigned int userlen; /* including 0 byte */
char *pass;
{
int child;
int wstat;
int pi[2];

if (fd_copy(2,1) == -1) die_pipe();//关闭出错(fd2),将标准输出(fd1),定向到标准出错(fd2)
close(3);
if (pipe(pi) == -1) die_pipe();
if (pi[0] != 3) die_pipe(); //确保向子进程能够读到硬编码的fd 3
switch(child = fork()) { //建立子进程执行subprogram给出的程式,一般是一个检验用户名和密码的程式
case -1:
die_fork();
case 0:
close(pi[1]);
sig_pipedefault();//子进程执行checkpassword或vchkpw之类的程式,检验密码,如果认证通过
execvp(*childargs,childargs);//这些再调用qmail-pop3d
_exit(1);
}
//父进程向子进程的fd3传送用户名及密码,这是一个约定。如果你要写自已的检验密码的程式,记得
//从fd3读密码哦。
close(pi[0]);
substdio_fdbuf(%26amp;ssup,write,pi[1],upbuf,sizeof upbuf);
if (substdio_put(%26amp;ssup,user,userlen) == -1) die_write();
if (substdio_put(%26amp;ssup,pass,str_len(pass) + 1) == -1) die_write();

//父进程向子进程传送<进程ID.当前时间@主机名>
if (substdio_puts(%26amp;ssup,"<") == -1) die_write();
if (substdio_puts(%26amp;ssup,unique) == -1) die_write();
if (substdio_puts(%26amp;ssup,hostname) == -1) die_write();
if (substdio_put(%26amp;ssup,">",2) == -1) die_write();
if (substdio_flush(%26amp;ssup) == -1) die_write();
close(pi[1]);
//清除密码及用户名缓冲区
byte_zero(pass,str_len(pass));
byte_zero(upbuf,sizeof upbuf);
if (wait_pid(%26amp;wstat,child) == -1) die();//等待子进程结束
if (wait_crashed(wstat)) die_childcrashed();
if (wait_exitcode(wstat)) die_badauth();
//完成一次pop3对话退出
die();
}


//显示欢迎信息
void pop3_greet()
{
char *s;
s = unique;
s += fmt_uint(s,getpid());
*s++ = '.';
s += fmt_ulong(s,(unsigned long) now());
*s++ = '@';
*s++ = 0;
puts("+OK <");
puts(unique);
puts(hostname);
puts(">\r\n");
flush();
}


//设置标志,初始化用户名变量
void pop3_user(arg) char *arg;
{
if (!*arg) { err_syntax(); return; }
okay();
seenuser = 1; //user命令已经执行的标志
if (!stralloc_copys(%26amp;username,arg)) die_nomem(); //将参数存入username
if (!stralloc_0(%26amp;username)) die_nomem();
}

void pop3_pass(arg) char *arg;
{

if (!seenuser) { err_wantuser(); return; }

if (!*arg) { err_syntax(); return; }
doanddie(username.s,username.len,arg);//调用子进程验正密码并等待它完成
}

void pop3_apop(arg) char *arg;//用户名及密码在一个命令中给出的情况,见user,pass
{
char *space;
space = arg + str_chr(arg,' ');
if (!*space) { err_syntax(); return; }
*space++ = 0;
doanddie(arg,space - arg,space);
}

struct commands pop3commands[] = {//命令及相应的处理函数表
{ "user", pop3_user, 0 }
, { "pass", pop3_pass, 0 }
, { "apop", pop3_apop, 0 }
, { "quit", pop3_quit, 0 }
, { "noop", okay, 0 }
, { 0, err_authoriz, 0 }
} ;



void main(argc,argv)
int argc;
char **argv;
{
sig_alarmcatch(die);//捕获sigalrm信号
sig_pipeignore();//忽略pipe信号

hostname = argv[1]; //hostname 指向 程式的第一个参数
if (!hostname) die_usage();
childargs = argv + 2;
if (!*childargs) die_usage();

pop3_greet();//显示欢迎信息后进入命令循环,等待用户命令
commands(%26amp;ssin,pop3commands);
die();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值