mirror of
https://github.com/decke/smtprelay.git
synced 2025-12-26 09:02:29 -07:00
Implement Sender and Recipient checker against regular expressions
This commit is contained in:
27
main.go
27
main.go
@@ -9,6 +9,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -29,6 +30,8 @@ var (
|
|||||||
localKey = flag.String("local_key", "", "SSL private key for STARTTLS/TLS")
|
localKey = flag.String("local_key", "", "SSL private key for STARTTLS/TLS")
|
||||||
localForceTLS = flag.Bool("local_forcetls", false, "Force STARTTLS (needs local_cert and local_key)")
|
localForceTLS = flag.Bool("local_forcetls", false, "Force STARTTLS (needs local_cert and local_key)")
|
||||||
allowedNets = flag.String("allowed_nets", "127.0.0.1/8 ::1/128", "Networks allowed to send mails")
|
allowedNets = flag.String("allowed_nets", "127.0.0.1/8 ::1/128", "Networks allowed to send mails")
|
||||||
|
allowedSender = flag.String("allowed_sender", "", "Regular expression for valid FROM EMail adresses")
|
||||||
|
allowedRecipients = flag.String("allowed_recipients", "", "Regular expression for valid TO EMail adresses")
|
||||||
remoteHost = flag.String("remote_host", "smtp.gmail.com:587", "Outgoing SMTP server")
|
remoteHost = flag.String("remote_host", "smtp.gmail.com:587", "Outgoing SMTP server")
|
||||||
remoteUser = flag.String("remote_user", "", "Username for authentication on outgoing SMTP server")
|
remoteUser = flag.String("remote_user", "", "Username for authentication on outgoing SMTP server")
|
||||||
remotePass = flag.String("remote_pass", "", "Password for authentication on outgoing SMTP server")
|
remotePass = flag.String("remote_pass", "", "Password for authentication on outgoing SMTP server")
|
||||||
@@ -57,11 +60,31 @@ func connectionChecker(peer smtpd.Peer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func senderChecker(peer smtpd.Peer, addr string) error {
|
func senderChecker(peer smtpd.Peer, addr string) error {
|
||||||
return nil
|
if *allowedSender == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
re := regexp.MustCompile(*allowedSender)
|
||||||
|
|
||||||
|
if re.MatchString(addr) {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return smtpd.Error{Code: 552, Message: "Denied"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func recipientChecker(peer smtpd.Peer, addr string) error {
|
func recipientChecker(peer smtpd.Peer, addr string) error {
|
||||||
return nil
|
if *allowedRecipients == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
re := regexp.MustCompile(*allowedRecipients)
|
||||||
|
|
||||||
|
if re.MatchString(addr) {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return smtpd.Error{Code: 552, Message: "Denied"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mailHandler(peer smtpd.Peer, env smtpd.Envelope) error {
|
func mailHandler(peer smtpd.Peer, env smtpd.Envelope) error {
|
||||||
|
|||||||
@@ -27,6 +27,14 @@
|
|||||||
; Networks that are allowed to send mails to us
|
; Networks that are allowed to send mails to us
|
||||||
;allowed_nets = 127.0.0.1/8 ::1/128
|
;allowed_nets = 127.0.0.1/8 ::1/128
|
||||||
|
|
||||||
|
; Regular expression for valid FROM EMail adresses
|
||||||
|
; Example: ^(.*)@localhost.localdomain$
|
||||||
|
;allowed_sender =
|
||||||
|
|
||||||
|
; Regular expression for valid TO EMail adresses
|
||||||
|
; Example: ^(.*)@localhost.localdomain$
|
||||||
|
;allowed_recipients =
|
||||||
|
|
||||||
; Relay all mails to this SMTP server
|
; Relay all mails to this SMTP server
|
||||||
|
|
||||||
; GMail
|
; GMail
|
||||||
|
|||||||
Reference in New Issue
Block a user