-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckHost.go
50 lines (44 loc) · 1.32 KB
/
checkHost.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package spf
import (
"fmt"
"net"
"regexp"
"strings"
)
var hostnameRgx *regexp.Regexp = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9-.]*\.\D{2,4}$`)
// Checks that a sender (MAIL FROM or HELO) and ip has permission to send mail from a domain
// (see rfc7208 section 4.1)
//
// Parameters:
//
// ip: the net.IP of the sender (either ip6 or ip4)
// domain: the claimed domain of the sender (ex 'colorado.edu' if mail is from '[email protected] )
// sender: the "MAIL FROM" or "HELO" identity, this will likely match the domain
// Returns:
//
// res: the Result enum (see README for all possible results)
// err: an error object, only relevant if res = ResultPermError or ResultTempError
func CheckHost(ip net.IP, domain, sender string) (res Result, err error) {
// invalid domain should return none
if hostnameRgx.MatchString(domain) {
return ResultNone, nil
}
senderSplit := strings.Split(sender, "@")
if len(senderSplit) != 2 || len(senderSplit[1]) < 1 {
return ResultPermError, fmt.Errorf("invalid sender '%s', should be in form 'local@domain'", sender)
}
// "If the <sender> has no local-part, substitute the string 'postmaster' for the local-part"
if senderSplit[0] == "" {
senderSplit[0] = "postmaster"
}
s := session{
LookupLimit,
ip,
domain,
sender,
senderSplit[0],
senderSplit[1],
"",
}
return s.checkHost()
}