免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345下一页
最近访问板块 发新帖
查看: 21565 | 回复: 48
打印 上一主题 下一主题

[Mail] 修正Qmail auth smtp中电子邮件地址任意的patch [复制链接]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-04-05 18:15 |只看该作者 |正序浏览
qmail 的auth smtp有两个patch,
分别在:http://members.elysium.pl/brush/qmail-smtpd-auth/和www.nimh.org/hacks/qmail-smtpd.c。

不过都有个问题,就是auth 的用户可以和mail from的用户不一样,这样就
给了垃圾邮件发送者可乘之机,只要知道了某个用户的地址和口令,就可以
以自己的名义发信。同时认证用户的过程并不记录在邮件日志中,这给了系统管理员造成了很大不便,难以知道是哪个用户被黑掉了。

下面这个patch就是针对这种情况写的。

作用有两个:
1. 通过环境变量来控制是否记录认证过程。
2. 如果mail from和auth user name不一致,拒绝发送信件。


这个patch是基于www.nimh.org/hacks/qmail-smtpd.c,iceblood版本中实际上也是的这个。因此,用iceblood版本的可以直接使用。如果是用
http://members.elysium.pl/brush/qmail-smtpd-auth/版本的就需要自己
相应的修改了。

安装方法,如下,将下面这些内容存到qmail-smtpd.patch。放到qmail的源码目录。然后
1. patch < qmail-smtpd.patch
2. make qmail-smtpd
3. 替换qmail-smtpd
   或者复制到新的执行文件例如qmail-smtpd.auth,这样你的旧版本就能保留
  了,只需要在smtpd的启动脚本中,修改为对应的执行文件就可以。

如果你需要记录smtp认证的信息,在smtpd的启动脚本中定义环境变量,如下:
LOG_AUTH=1
export LOG_AUTH

缺省情况下,认证信息不记入log

--- qmail-smtpd.c       2003-04-05 17:55:20.000000000 -0500
+++ qmail-smtpd.c.new   2003-04-05 17:50:58.000000000 -0500
@@ -304,7 +304,11 @@
   if (!stralloc_copys(&rcptto,"") die_nomem();
   if (!stralloc_copys(&mailfrom,addr.s)) die_nomem();
   if (!stralloc_0(&mailfrom)) die_nomem();
+  if (smtp_auth_validfrom(mailfrom.s)) {
   out("250 ok\r\n";
+  } else {
+    out("must use username as From authenticated! (#5.7.1)\r\n";
+  }
}
void smtp_rcpt(arg) char *arg; {
   if (!seenmail) { err_wantmail(); return; }
@@ -527,6 +531,82 @@
static stralloc smtpauth = {0};
static char smtpauthlogin[65];
static char smtpauthpass[65];
+static int authd = 0;
+
+/* Author: gadfly@163.com.
+ * 1. Check consistent between auth user and 'From' user.
+ * 2. Read the LOG_AUTH enviroment variable to determine whether logging the auth info.
+ * LOG_AUTH=1, write the auth smtp info into syslog.
+ */
+#include <syslog.h>;
+
+#define SMTP_AUTH_SUCCESS     0
+#define SMTP_AUTH_FAILED      1
+
+int smtp_auth_validfrom(from) char * from;
+{
+  stralloc tmplogin={0},mydefaultdomain={0};
+  char authusername[256];
+  int k, userlen;
+
+  if (!authd) return 1;
+
+  authusername[255] = '\0';
+  userlen = k = str_len(smtpauthlogin);
+  k = byte_rchr(smtpauthlogin, k, '@');
+
+  if (k == userlen)
+  {
+    k = byte_rchr(smtpauthlogin, userlen, '%');
+
+    if (k == userlen)
+    {
+      if (control_readfile(&mydefaultdomain,"/var/qmail/control/me",1) != 1)
+      {
+        die_nomem();
+      }
+      if (!stralloc_copys(&tmplogin, smtpauthlogin) )
+      {
+        die_nomem();
+      }
+      if (!stralloc_cats(&tmplogin, "@" )
+      {
+        die_nomem();
+      }
+      if (!stralloc_cat(&tmplogin, &mydefaultdomain))
+      {
+        die_nomem();
+      }
+      strncpy(authusername, tmplogin.s, sizeof(authusername));
+      }
+      else
+      {
+        strcpy(authusername, smtpauthlogin);
+      authusername[k]='@';
+      }
+  }
+  else {
+    strcpy(authusername, smtpauthlogin);
+  }
+  return !strcasecmp(from, authusername);
+}
+
+
+void auth_smtplog(authlogin, authresult)
+char * authlogin;
+int authresult;
+{
+  char * x = env_get("LOG_AUTH";
+
+  if (!x || (*x != '1')) return;
+
+  if (authresult == SMTP_AUTH_SUCCESS) {
+    syslog(LOG_MAIL | LOG_INFO, "smtp auth: user name is %s, success!", authlogin);
+  } else {
+    syslog(LOG_MAIL | LOG_INFO, "smtp auth: user name is %s, failed!", authlogin);
+  }
+}
+
static int smtpauth_getl(void) {
   int i;
   if (!stralloc_copys(&smtpauth, "") return -1;
@@ -611,11 +691,14 @@
   wait_pid(&st, pid);
   if (wait_exitcode(st) == 0) {
     out("235 go ahead\r\n";
+    authd = 1;
+    auth_smtplog(smtpauthlogin, SMTP_AUTH_SUCCESS);
     flush();
     relayclient="";
     return;
   }
   sleep(2);
+  auth_smtplog(smtpauthlogin, SMTP_AUTH_FAILED);
   out("535 auth failure\r\n"; flush(); _exit(0);
   /* done */
}


呵呵,正式用之前,各位多测试测试。

几种情况需要测测看:
1. 其它服务器发到本域,smtp过程是否正常,本域能否收到
2. from和auth user 不一样会出现什么情况, 一样的话,能否发信。
3. LOG_AUTH是否起作用。

呵呵,qmail的对齐用空格,实在不习惯,没办法,为保持一致,我也用了。     

更新版本
应一些朋友的要求,加了本地发信人发给本地,也需要auth的功能。

另外而且还加了一些注释,版本号。方法和前面一样。

代码如下:

  1. --- qmail-smtpd.c        2003-06-02 18:34:51.000000000 -0400
  2. +++ qmail-smtpd.c.new        2003-06-02 18:42:31.000000000 -0400
  3. @@ -268,6 +268,7 @@
  4.    int r;
  5.    r = rcpthosts(addr.s,str_len(addr.s));
  6.    if (r == -1) die_control();
  7. +  if (!localauthd()) return 0;
  8.    return r;
  9. }

  10. @@ -304,7 +305,11 @@
  11.    if (!stralloc_copys(&rcptto,"")) die_nomem();
  12.    if (!stralloc_copys(&mailfrom,addr.s)) die_nomem();
  13.    if (!stralloc_0(&mailfrom)) die_nomem();
  14. -  out("250 ok\r\n");
  15. +  if (smtp_auth_validfrom(mailfrom.s)) {
  16. +    out("250 ok\r\n");
  17. +  } else {
  18. +    out("must use username as From authenticated! (#5.7.1)\r\n");
  19. +  }
  20. }
  21. void smtp_rcpt(arg) char *arg; {
  22.    if (!seenmail) { err_wantmail(); return; }
  23. @@ -527,6 +532,89 @@
  24. static stralloc smtpauth = {0};
  25. static char smtpauthlogin[65];
  26. static char smtpauthpass[65];
  27. +static int authd = 0;
  28. +
  29. +/* Author: gadfly@163.com. Version: 1.2.
  30. + * 1. Check consistent between auth user and 'From' user.
  31. + * 2. Read the LOG_AUTH enviroment variable to determine whether logging the auth info.
  32. + * LOG_AUTH=1, write the auth smtp info into syslog.
  33. + * 3. If mail from local, rcpt to local domain, user must auth before send data.
  34. + */
  35. +#include <syslog.h>;
  36. +
  37. +#define SMTP_AUTH_SUCCESS     0
  38. +#define SMTP_AUTH_FAILED      1
  39. +
  40. +int localauthd() {
  41. +
  42. +  if (rcpthosts(mailfrom.s, strlen(mailfrom.s)) && !authd) return 0;
  43. +  return 1;
  44. +}
  45. +
  46. +int smtp_auth_validfrom(from) char * from;
  47. +{
  48. +  stralloc tmplogin={0},mydefaultdomain={0};
  49. +  char authusername[256];
  50. +  int k, userlen;
  51. +
  52. +  if (!authd) return 1;
  53. +
  54. +  authusername[255] = '\0';
  55. +  userlen = k = str_len(smtpauthlogin);
  56. +  k = byte_rchr(smtpauthlogin, k, '@');
  57. +
  58. +  if (k == userlen)
  59. +  {
  60. +    k = byte_rchr(smtpauthlogin, userlen, '%');
  61. +
  62. +    if (k == userlen)
  63. +    {
  64. +      if (control_readfile(&mydefaultdomain,"/var/qmail/control/me",1) != 1)
  65. +      {
  66. +        die_nomem();
  67. +      }
  68. +      if (!stralloc_copys(&tmplogin, smtpauthlogin) )
  69. +      {
  70. +        die_nomem();
  71. +      }
  72. +      if (!stralloc_cats(&tmplogin, "@") )
  73. +      {
  74. +        die_nomem();
  75. +      }
  76. +      if (!stralloc_cat(&tmplogin, &mydefaultdomain))
  77. +      {
  78. +        die_nomem();
  79. +      }
  80. +      strncpy(authusername, tmplogin.s, sizeof(authusername));
  81. +      }
  82. +      else
  83. +      {
  84. +        strcpy(authusername, smtpauthlogin);
  85. +      authusername[k]='@';
  86. +      }
  87. +  }
  88. +  else {
  89. +    strcpy(authusername, smtpauthlogin);
  90. +  }
  91. +  return !strcasecmp(from, authusername);
  92. +}
  93. +
  94. +
  95. +void auth_smtplog(authlogin, authresult)
  96. +char * authlogin;
  97. +int authresult;
  98. +{
  99. +  char * x = env_get("LOG_AUTH");
  100. +
  101. +  if (!x || (*x != '1')) return;
  102. +
  103. +  if (authresult == SMTP_AUTH_SUCCESS) {
  104. +    syslog(LOG_MAIL | LOG_INFO, "smtp auth: user name is %s, success!", authlogin);
  105. +  } else {
  106. +    syslog(LOG_MAIL | LOG_INFO, "smtp auth: user name is %s, failed!", authlogin);
  107. +  }
  108. +}
  109. +
  110. static int smtpauth_getl(void) {
  111.    int i;
  112.    if (!stralloc_copys(&smtpauth, "")) return -1;
  113. @@ -611,11 +699,14 @@
  114.    wait_pid(&st, pid);
  115.    if (wait_exitcode(st) == 0) {
  116.      out("235 go ahead\r\n");
  117. +    authd = 1;
  118. +    auth_smtplog(smtpauthlogin, SMTP_AUTH_SUCCESS);
  119.      flush();
  120.      relayclient="";
  121.      return;
  122.    }
  123.    sleep(2);
  124. +  auth_smtplog(smtpauthlogin, SMTP_AUTH_FAILED);
  125.    out("535 auth failure\r\n"); flush(); _exit(0);
  126.    /* done */
  127. }
复制代码


另外,感谢erehw提供的空间可以下载这个patch。
http://gadfly.shanji.com/qmail-smtpd/


由于网页上的代码贴上去,格式有问题。
我就不再更新了。
请各位到以上的url下载。
或给我发信索要

论坛徽章:
0
49 [报告]
发表于 2008-06-08 11:45 |只看该作者

现在的下载地址在那里

没有下载的了

论坛徽章:
0
48 [报告]
发表于 2006-07-07 11:58 |只看该作者
文件又找不到了希望那位发给我一份yjc2688@163.com

论坛徽章:
0
47 [报告]
发表于 2006-06-30 02:31 |只看该作者
我的服务器安装完后也出现了与楼主一样的故障,任何非法用户只要使用了我的服务器就可以给我的域内所有成员发邮件,我的服务器环境是:

qmail 1.05 +vpopmail 5.4.10 +mysql+LINUX AS 4

请问应该用什么补丁,或者有什么比较好的解决方案呢,

先谢谢大家的帮助

详细烦请查看连接:http://bbs.chinaunix.net/viewthr ... &extra=page%3D1

论坛徽章:
0
46 [报告]
发表于 2006-06-29 11:17 |只看该作者
好文章!!!

论坛徽章:
0
45 [报告]
发表于 2006-02-18 20:40 |只看该作者
原帖由 vyouzhi 于 2006-2-18 11:18 发表
留个印
过些时候有用

已经没有必要了

论坛徽章:
0
44 [报告]
发表于 2006-02-18 11:18 |只看该作者
留个印
过些时候有用

论坛徽章:
0
43 [报告]
发表于 2005-11-19 09:10 |只看该作者
[root@testmailserver netqmail-1.05]# patch < qmail-smtpd.patch
patching file qmail-smtpd.c
Hunk #1 succeeded at 216 (offset -52 lines).
Hunk #3 succeeded at 447 (offset -85 lines).
Hunk #4 succeeded at 693 (offset -6 lines).
[root@testmailserver netqmail-1.05]# make qmail-smtpd
./compile qmail-smtpd.c
qmail-smtpd.c: In function `main':
qmail-smtpd.c:642: warning: return type of `main' is not `int'
./load qmail-smtpd rcpthosts.o commands.o timeoutread.o
timeoutwrite.o ip.o ipme.o ipalloc.o control.o constmap.o
received.o date822fmt.o now.o qmail.o cdb.a fd.a wait.a
datetime.a getln.a open.a sig.a case.a env.a stralloc.a
alloc.a substdio.a error.a str.a fs.a auto_qmail.o  `cat
socket.lib`

论坛徽章:
0
42 [报告]
发表于 2005-11-17 09:15 |只看该作者
我多说一句。首先感谢gladfly.
下面代码的第3行
stralloc tmplogin={0},mydefaultdomain={0};
应该改为
static stralloc tmplogin={0},mydefaultdomain={0};
否则有内存泄露。
因为坏人可以用RSET在一个连接发出许多MAIL FROM
smtp_auth_validfrom()被许多次调用。
tmplogin, mydefaultdomain会占用内存不退,越占越多。

也许我看的不完全对,因为我没有看PATCH后的代码。但希望你研究。




  1. +int smtp_auth_validfrom(from) char * from;
  2. +{
  3. +  stralloc tmplogin={0},mydefaultdomain={0};
  4. +  char authusername[256];
  5. +  int k, userlen;
  6. +
  7. +  if (!authd) return 1;
  8. +
  9. +  authusername[255] = '';
  10. +  userlen = k = str_len(smtpauthlogin);
  11. +  k = byte_rchr(smtpauthlogin, k, '@');
  12. +
  13. +  if (k == userlen)
  14. +  {
  15. +    k = byte_rchr(smtpauthlogin, userlen, '%');
  16. +
  17. +    if (k == userlen)

  18. ......................
复制代码

论坛徽章:
0
41 [报告]
发表于 2005-11-16 16:46 |只看该作者
# vi /service/qmail-smtpd/run
exec /usr/local/bin/softlimit -m 40000000
    /usr/local/bin/tcpserver -v -H -R -l 0
    -x /home/vpopmail/etc/tcp.smtp.cdb -c "$MAXSMTPD"
    -u "$QMAILDUID" -g "$NOFILESGID" 0 smtp
    /var/qmail/bin/qmail-smtpd
    /home/vpopmail/bin/vchkpw /bin/true 2>&1

这样的配置如何应用这个补丁?
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP