Nginx 使用 Perl 进行 IMAP 的认证
时间:2011-01-19
来源:互联网
首先要使用以下编译参数引入 perl 和邮件模块
./configure --with-http_perl_module --with-mail
[代码] nginx.conf
复制代码
[代码] mailauth.pm
复制代码
./configure --with-http_perl_module --with-mail
[代码] nginx.conf
- user nobody;
- worker_processes 1;
- error_log logs/error.log info;
- pid logs/nginx.pid;
-
- events {
- worker_connections 1024;
- multi_accept on;
- }
-
- http {
- perl_modules perl/lib;
- perl_require mailauth.pm;
-
- server {
- location /auth {
- perl mailauth::handler;
- }
- }
- }
-
- mail {
- auth_http 127.0.0.1:80/auth;
-
- pop3_capabilities "TOP" "USER";
- imap_capabilities "IMAP4rev1" "UIDPLUS";
-
- server {
- listen 110;
- protocol pop3;
- proxy on;
- }
-
- server {
- listen 143;
- protocol imap;
- proxy on;
- }
- }
- package mailauth;
- use nginx;
- use DBI;
- my $dsn="DBI:mysql:database=DBNAME;host=HOSTNAME";
- our $dbh=DBI->connect_cached($dsn, 'dbusername', 'dbpass', {AutoCommit => 1});
- our $sth=$dbh->prepare("select password,mail_server from mailaccounts where username=? limit 1");
-
- our $auth_ok;
- our $mail_server_ip={};
- our $protocol_ports={};
- $mail_server_ip->{'mailhost01'}="192.168.1.22";
- $mail_server_ip->{'mailhost02'}="192.168.1.33";
- $protocol_ports->{'pop3'}=110;
- $protocol_ports->{'imap'}=143;
-
- sub handler {
- my $r = shift;
- $auth_ok=0;
-
- $sth->execute($r->header_in("Auth-User"));
- my $hash=$sth->fetchrow_hashref();
- # assuming that the query results password and mail_server
- # assuming that the password is in crypt format
-
- if (crypt($r->header_in("Auth-Pass"), $hash->{'password'}) eq $r->header_in("Auth-Pass")){
- $auth_ok=1;
- }
- if ($auth_ok==1){
- $r->header_out("Auth-Status", "OK") ;
- $r->header_out("Auth-Server", $mail_server_ip->{$hash->{'mail_server'}});
- $r->header_out("Auth-Port", $protocol_ports->{$r->header_in("Auth-Protocol")});
- } else {
- $r->header_out("Auth-Status", "Invalid login or password") ;
- }
-
- $r->send_http_header("text/html");
-
- return OK;
- }
-
- 1;
- __END__
作者: lockend 发布时间: 2011-01-19
nginx作为mail的反向代理,设计上就有问题。
它非得加个强行认证,其实将认证信息转发给realserver由后端realserver来认证不就得了么?
个人觉得很少有需求再在nginx这里设置一层认证,不管是LDAP还是啥也好。
结果,在需要nginx的mail反向代理时,不得不多此一举自己搞个认证模块。
我上次就非常恼怒的写了一个认证代码全部加起来不到5行,一路返回OK骗过nginx这个SB设置就行。
它非得加个强行认证,其实将认证信息转发给realserver由后端realserver来认证不就得了么?
个人觉得很少有需求再在nginx这里设置一层认证,不管是LDAP还是啥也好。
结果,在需要nginx的mail反向代理时,不得不多此一举自己搞个认证模块。
我上次就非常恼怒的写了一个认证代码全部加起来不到5行,一路返回OK骗过nginx这个SB设置就行。
作者: 兰花仙子 发布时间: 2011-01-19
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28