51CTO技术论坛_中国领先的IT技术社区's Archiver

wantin6 发表于 2006-8-15 17:14

修正Qmail auth smtp中电子邮件地址任意的patch

qmail 的auth smtp有两个patch,
分别在:[url]http://members.elysium.pl/brush/qmail-smtpd-auth/[/url]和[url]www.nimh.org/hacks/qmail-smtpd.c[/url]。

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

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

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

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

安装方法,如下,将下面这些内容存到qmail-smtpd.patch。放到qmail的源码目录。然后
1. patch QUOTE:
--- 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,""[img]http://bbs.chinaunix.net/images/smilies/icon_wink.gif[/img]) 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"[img]http://bbs.chinaunix.net/images/smilies/icon_wink.gif[/img];
+  } else {
+    out("must use username as From authenticated! (#5.7.1)\r\n"[img]http://bbs.chinaunix.net/images/smilies/icon_wink.gif[/img];
+  }
}
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: [email]gadfly@163.com[/email].
+ * 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 ;
+
+#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, "@"[img]http://bbs.chinaunix.net/images/smilies/icon_wink.gif[/img] )
+      {
+        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"[img]http://bbs.chinaunix.net/images/smilies/icon_wink.gif[/img];
+
+  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, ""[img]http://bbs.chinaunix.net/images/smilies/icon_wink.gif[/img]) return -1;
@@ -611,11 +691,14 @@
   wait_pid(&st, pid);
   if (wait_exitcode(st) == 0) {
     out("235 go ahead\r\n"[img]http://bbs.chinaunix.net/images/smilies/icon_wink.gif[/img];
+    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"[img]http://bbs.chinaunix.net/images/smilies/icon_wink.gif[/img]; flush(); _exit(0);
   /* done */
}呵呵,正式用之前,各位多测试测试。

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

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

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

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

代码如下:

CODE:
[url=http://bbs.chinaunix.net/viewthread.php?tid=45055&extra=page%3D1%26filter%3Ddigest###][color=#000000][Copy to clipboard][/color][/url]
--- qmail-smtpd.c        2003-06-02 18:34:51.000000000 -0400
+++ qmail-smtpd.c.new        2003-06-02 18:42:31.000000000 -0400
@@ -268,6 +268,7 @@
   int r;
   r = rcpthosts(addr.s,str_len(addr.s));
   if (r == -1) die_control();
+  if (!localauthd()) return 0;
   return r;
}

@@ -304,7 +305,11 @@
   if (!stralloc_copys(&rcptto,"")) die_nomem();
   if (!stralloc_copys(&mailfrom,addr.s)) die_nomem();
   if (!stralloc_0(&mailfrom)) die_nomem();
-  out("250 ok\r\n");
+  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 +532,89 @@
static stralloc smtpauth = {0};
static char smtpauthlogin[65];
static char smtpauthpass[65];
+static int authd = 0;
+
+/* Author: [email]gadfly@163.com[/email]. Version: 1.2.
+ * 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.
+ * 3. If mail from local, rcpt to local domain, user must auth before send data.
+ */
+#include ;
+
+#define SMTP_AUTH_SUCCESS     0
+#define SMTP_AUTH_FAILED      1
+
+int localauthd() {
+
+  if (rcpthosts(mailfrom.s, strlen(mailfrom.s)) && !authd) return 0;
+  return 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 +699,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 */
}另外,感谢erehw提供的空间可以下载这个patch。

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.