@@ -2,8 +2,10 @@ package main
2
2
3
3
import (
4
4
"flag"
5
+ "net"
5
6
6
7
"github.com/vharitonsky/iniflags"
8
+ "github.com/sirupsen/logrus"
7
9
)
8
10
9
11
var (
21
23
localCert = flag .String ("local_cert" , "" , "SSL certificate for STARTTLS/TLS" )
22
24
localKey = flag .String ("local_key" , "" , "SSL private key for STARTTLS/TLS" )
23
25
localForceTLS = flag .Bool ("local_forcetls" , false , "Force STARTTLS (needs local_cert and local_key)" )
24
- allowedNets = flag .String ("allowed_nets" , "127.0.0.1/8 ::1/128" , "Networks allowed to send mails" )
26
+ allowedNetsStr = flag .String ("allowed_nets" , "127.0.0.0/8 ::1/128" , "Networks allowed to send mails" )
27
+ allowedNets = []* net.IPNet {}
25
28
allowedSender = flag .String ("allowed_sender" , "" , "Regular expression for valid FROM EMail addresses" )
26
29
allowedRecipients = flag .String ("allowed_recipients" , "" , "Regular expression for valid TO EMail addresses" )
27
30
allowedUsers = flag .String ("allowed_users" , "" , "Path to file with valid users/passwords" )
33
36
versionInfo = flag .Bool ("version" , false , "Show version information" )
34
37
)
35
38
39
+
40
+ func setupAllowedNetworks () {
41
+ for _ , netstr := range splitstr (* allowedNetsStr , ' ' ) {
42
+ baseIP , allowedNet , err := net .ParseCIDR (netstr )
43
+ if err != nil {
44
+ log .WithField ("netstr" , netstr ).
45
+ WithError (err ).
46
+ Fatal ("Invalid CIDR notation in allowed_nets" )
47
+ }
48
+
49
+ // Reject any network specification where any host bits are set,
50
+ // meaning the address refers to a host and not a network.
51
+ if ! allowedNet .IP .Equal (baseIP ) {
52
+ log .WithFields (logrus.Fields {
53
+ "given_net" : netstr ,
54
+ "proper_net" : allowedNet ,
55
+ }).Fatal ("Invalid network in allowed_nets (host bits set)" )
56
+ }
57
+
58
+ allowedNets = append (allowedNets , allowedNet )
59
+ }
60
+ }
61
+
36
62
func ConfigLoad () {
37
63
iniflags .Parse ()
38
64
@@ -42,4 +68,6 @@ func ConfigLoad() {
42
68
if (* remoteHost == "" ) {
43
69
log .Warn ("remote_host not set; mail will not be forwarded!" )
44
70
}
71
+
72
+ setupAllowedNetworks ()
45
73
}
0 commit comments