- 论坛徽章:
- 1
|
修正Qmail auth smtp中电子邮件地址任意的patch
应一些朋友的要求,加了本地发信人发给本地,也需要auth的功能。
另外而且还加了一些注释,版本号。方法和前面一样。
我稍微测试了一下。各位测恻看吧。
- --- 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: gadfly@163.com. 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 <syslog.h>;
- +
- +#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 */
- }
复制代码 |
|