qmail-popup.c·ÖÎö
Programmer:ҹδÃß
Come from:ChongQing Gearbox co.,ltd
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(&ssout,s);
}
void flush()
{
substdio_flush(&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(&ssup,write,pi[1],upbuf,sizeof upbuf);
if (substdio_put(&ssup,user,userlen) == -1) die_write();
if (substdio_put(&ssup,pass,str_len(pass) + 1) == -1) die_write();
//¸¸½ø³ÌÏò×Ó½ø³Ì´«ËÍ<½ø³ÌID.µ±Ç°Ê±¼ä@Ö÷»úÃû>
if (substdio_puts(&ssup,"<") == -1) die_write();
if (substdio_puts(&ssup,unique) == -1) die_write();
if (substdio_puts(&ssup,hostname) == -1) die_write();
if (substdio_put(&ssup,">",2) == -1) die_write();
if (substdio_flush(&ssup) == -1) die_write();
close(pi[1]);
//Çå³ýÃÜÂë¼°Óû§Ãû»º³åÇø
byte_zero(pass,str_len(pass));
byte_zero(upbuf,sizeof upbuf);
if (wait_pid(&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(&username,arg)) die_nomem(); //½«²ÎÊý´æÈëusername
if (!stralloc_0(&username)) die_nomem();
}
void pop3_pass(arg) char *arg;
{
if (!seenuser) { err_wantuser(); return; }//Èç¹ûûÓÐÖ´ÐÐuserÃüÁî,·µ»Ø
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(&ssin,pop3commands);
die();
},