Ajout domain + bugfix lorsque le MX ne termine pas par .

This commit is contained in:
yo
2021-01-06 09:11:00 +01:00
parent be07064a96
commit d9a3cae44d
+17 -4
View File
@@ -13,12 +13,12 @@
package main
import (
"flag"
"fmt"
"log"
"log/syslog"
"net"
"os"
"flag"
"strings"
"sync"
@@ -27,7 +27,7 @@ import (
)
const (
version = "0.8"
version = "0.9.1"
)
var (
@@ -39,9 +39,12 @@ var (
ldapBaseDN *string
ldapUser *string
ldapPass *string
defDomain *string
)
func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
var s string
var mxdom string
buf := make([]byte, 1024)
defer connClt.Close()
@@ -75,7 +78,11 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
mxslic := strings.Split(mxs[0].Host, ".")
// les MXs terminent par '.', on le retire
mxdom := strings.Join(mxslic[len(mxslic)-3:len(mxslic)-1], ".")
if mxs[0].Host[len(mxs[0].Host)-1] == '.' {
mxdom = strings.Join(mxslic[len(mxslic)-3:len(mxslic)-1], ".")
} else {
mxdom = strings.Join(mxslic[len(mxslic)-2:len(mxslic)], ".")
}
filter := fmt.Sprintf("(dc=%s)", ldap.EscapeFilter(mxdom))
searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,
@@ -104,7 +111,12 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
if *debug {
logstream.Debug("Got result, returning " + result.Entries[0].Attributes[0].Values[0])
}
s := strings.Replace(fmt.Sprintf("FILTER relay:[%s]", result.Entries[0].Attributes[0].Values[0]), " ", "%20", -1)
// Check if result is a FQDN, if not append defDomain
if strings.Contains(string(result.Entries[0].Attributes[0].Values[0]), ".") {
s = strings.Replace(fmt.Sprintf("FILTER relay:[%s]", result.Entries[0].Attributes[0].Values[0]), " ", "%20", -1)
} else {
s = strings.Replace(fmt.Sprintf("FILTER relay:[%s]", fmt.Sprintf("%s.%s", result.Entries[0].Attributes[0].Values[0], *defDomain)), " ", "%20", -1)
}
connClt.Write([]byte(fmt.Sprintf("200 %s\n", s)))
} else {
if *debug {
@@ -154,6 +166,7 @@ func main() {
ldapBaseDN = fs.String("ldapDN", "", "LDAP base DN (also via LDAPDN env var)")
ldapUser = fs.String("ldapUser", "", "LDAP user DN (also via LDAPUSER env var)")
ldapPass = fs.String("ldapPass", "", "LDAP user password (also via LDAPPASS env var)")
defDomain = fs.String("domain", "", "Domain to add to relay name if not a FQDN (also via DOMAIN env var)")
_ = fs.String("config", "", "config file (optional)")
// Surcharge de la fonction Usage()