mirror of
https://github.com/decke/smtprelay.git
synced 2025-12-27 09:22:33 -07:00
Merge pull request #18 from decke/allow-any-net
Allow any network and related enhancements
This commit is contained in:
30
config.go
30
config.go
@@ -2,8 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"net"
|
||||||
|
|
||||||
"github.com/vharitonsky/iniflags"
|
"github.com/vharitonsky/iniflags"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -21,7 +23,8 @@ var (
|
|||||||
localCert = flag.String("local_cert", "", "SSL certificate for STARTTLS/TLS")
|
localCert = flag.String("local_cert", "", "SSL certificate for STARTTLS/TLS")
|
||||||
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")
|
allowedNetsStr = flag.String("allowed_nets", "127.0.0.0/8 ::1/128", "Networks allowed to send mails")
|
||||||
|
allowedNets = []*net.IPNet{}
|
||||||
allowedSender = flag.String("allowed_sender", "", "Regular expression for valid FROM EMail addresses")
|
allowedSender = flag.String("allowed_sender", "", "Regular expression for valid FROM EMail addresses")
|
||||||
allowedRecipients = flag.String("allowed_recipients", "", "Regular expression for valid TO EMail addresses")
|
allowedRecipients = flag.String("allowed_recipients", "", "Regular expression for valid TO EMail addresses")
|
||||||
allowedUsers = flag.String("allowed_users", "", "Path to file with valid users/passwords")
|
allowedUsers = flag.String("allowed_users", "", "Path to file with valid users/passwords")
|
||||||
@@ -33,6 +36,29 @@ var (
|
|||||||
versionInfo = flag.Bool("version", false, "Show version information")
|
versionInfo = flag.Bool("version", false, "Show version information")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func setupAllowedNetworks() {
|
||||||
|
for _, netstr := range splitstr(*allowedNetsStr, ' ') {
|
||||||
|
baseIP, allowedNet, err := net.ParseCIDR(netstr)
|
||||||
|
if err != nil {
|
||||||
|
log.WithField("netstr", netstr).
|
||||||
|
WithError(err).
|
||||||
|
Fatal("Invalid CIDR notation in allowed_nets")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject any network specification where any host bits are set,
|
||||||
|
// meaning the address refers to a host and not a network.
|
||||||
|
if !allowedNet.IP.Equal(baseIP) {
|
||||||
|
log.WithFields(logrus.Fields{
|
||||||
|
"given_net": netstr,
|
||||||
|
"proper_net": allowedNet,
|
||||||
|
}).Fatal("Invalid network in allowed_nets (host bits set)")
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedNets = append(allowedNets, allowedNet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ConfigLoad() {
|
func ConfigLoad() {
|
||||||
iniflags.Parse()
|
iniflags.Parse()
|
||||||
|
|
||||||
@@ -42,4 +68,6 @@ func ConfigLoad() {
|
|||||||
if (*remoteHost == "") {
|
if (*remoteHost == "") {
|
||||||
log.Warn("remote_host not set; mail will not be forwarded!")
|
log.Warn("remote_host not set; mail will not be forwarded!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setupAllowedNetworks()
|
||||||
}
|
}
|
||||||
|
|||||||
19
main.go
19
main.go
@@ -17,20 +17,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func connectionChecker(peer smtpd.Peer) error {
|
func connectionChecker(peer smtpd.Peer) error {
|
||||||
var peerIP net.IP
|
// This can't panic because we only have TCP listeners
|
||||||
if addr, ok := peer.Addr.(*net.TCPAddr); ok {
|
peerIP := peer.Addr.(*net.TCPAddr).IP
|
||||||
peerIP = net.ParseIP(addr.IP.String())
|
|
||||||
} else {
|
if len(allowedNets) == 0 {
|
||||||
log.WithField("ip", addr.IP).
|
// Special case: empty string means allow everything
|
||||||
Warn("failed to parse IP")
|
return nil
|
||||||
return smtpd.Error{Code: 421, Message: "Denied"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nets := strings.Split(*allowedNets, " ")
|
for _, allowedNet := range allowedNets {
|
||||||
|
|
||||||
for i := range nets {
|
|
||||||
_, allowedNet, _ := net.ParseCIDR(nets[i])
|
|
||||||
|
|
||||||
if allowedNet.Contains(peerIP) {
|
if allowedNet.Contains(peerIP) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,8 @@
|
|||||||
;local_forcetls = false
|
;local_forcetls = false
|
||||||
|
|
||||||
; 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
|
; Defaults to localhost. If set to "", then any address is allowed.
|
||||||
|
;allowed_nets = 127.0.0.0/8 ::1/128
|
||||||
|
|
||||||
; Regular expression for valid FROM EMail addresses
|
; Regular expression for valid FROM EMail addresses
|
||||||
; Example: ^(.*)@localhost.localdomain$
|
; Example: ^(.*)@localhost.localdomain$
|
||||||
|
|||||||
Reference in New Issue
Block a user