瀏覽代碼

Check sender email against auth file when user is authenticated

Bernhard Froehlich 6 年之前
父節點
當前提交
a82b0faf96
共有 3 個文件被更改,包括 27 次插入2 次删除
  1. 1 1
      README.md
  2. 25 0
      main.go
  3. 1 1
      smtp-proxy.ini

+ 1 - 1
README.md

@@ -25,5 +25,5 @@ produces mail.
 * Authentication support with file (LOGIN, PLAIN)
 * Authentication support with file (LOGIN, PLAIN)
 * Enforce encryption for authentication
 * Enforce encryption for authentication
 * Forwards all mail to a smarthost (GMail, MailGun or any other SMTP server)
 * Forwards all mail to a smarthost (GMail, MailGun or any other SMTP server)
-* Small codebase (smtp-proxy ~250 LoC, chrj/smtpd ~1200 LoC)
+* Small codebase (smtp-proxy ~300 LoC, chrj/smtpd ~1200 LoC)
 * IPv6 support
 * IPv6 support

+ 25 - 0
main.go

@@ -63,6 +63,31 @@ func connectionChecker(peer smtpd.Peer) error {
 }
 }
 
 
 func senderChecker(peer smtpd.Peer, addr string) error {
 func senderChecker(peer smtpd.Peer, addr string) error {
+	// check sender address from auth file if user is authenticated
+	if *allowedUsers != "" && peer.Username != "" {
+		file, err := os.Open(*allowedUsers)
+		if err != nil {
+			log.Printf("User file not found %v", err)
+			return smtpd.Error{Code: 451, Message: "Bad sender address"}
+		}
+		defer file.Close()
+
+		scanner := bufio.NewScanner(file)
+		for scanner.Scan() {
+			parts := strings.Fields(scanner.Text())
+
+			if len(parts) != 3 {
+				continue
+			}
+
+			if peer.Username == parts[0] {
+				if strings.ToLower(addr) != strings.ToLower(parts[2])  {
+					return smtpd.Error{Code: 451, Message: "Bad sender address"}
+				}
+			}
+		}
+	}
+
 	if *allowedSender == "" {
 	if *allowedSender == "" {
 		return nil
 		return nil
 	}
 	}

+ 1 - 1
smtp-proxy.ini

@@ -37,7 +37,7 @@
 
 
 ; File which contains username and password used for
 ; File which contains username and password used for
 ; authentication before they can send mail.
 ; authentication before they can send mail.
-; File format: username bcrypt-hash
+; File format: username bcrypt-hash email
 ;allowed_users =
 ;allowed_users =
 
 
 ; Relay all mails to this SMTP server
 ; Relay all mails to this SMTP server