Ajout config ldapAttr + msg erreur si inexistant dnas LDAP

This commit is contained in:
yo
2021-01-12 13:07:01 +01:00
parent d9a3cae44d
commit 91a0c84593
+18 -6
View File
@@ -1,7 +1,7 @@
// mxrouter
// Copyright (c) 2020 yo000 <johan@nosd.in>
//
//
// TODO : REconnexion ldap lorsque necessaire
// checkmx : postfix tcp_table server which returns second-level domain name of the MX of a domain
// ex :
@@ -27,7 +27,7 @@ import (
)
const (
version = "0.9.1"
version = "0.9.3"
)
var (
@@ -39,6 +39,7 @@ var (
ldapBaseDN *string
ldapUser *string
ldapPass *string
ldapAttr *string
defDomain *string
)
@@ -55,7 +56,7 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
// 1) Recupere le MX du domaine de l'adresse mail recue
if strings.HasPrefix(string(buf[:readlen-1]), "get ") && strings.Contains(string(buf[:readlen-1]), "@") {
mail := string(buf[4:readlen-1])
mail := string(buf[4 : readlen-1])
domain := strings.Split(mail, "@")[1]
mxs, err := net.LookupMX(domain)
if err != nil {
@@ -86,12 +87,16 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
filter := fmt.Sprintf("(dc=%s)", ldap.EscapeFilter(mxdom))
searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,
false, filter, []string{"relayName"}, []ldap.Control{})
false, filter, []string{*ldapAttr}, []ldap.Control{})
mutex.Lock()
result, err := conLdap.Search(searchReq)
mutex.Unlock()
if err != nil {
if err.Error == "ldap: connection closed" {
// TODO : Reconnect to LDAP
return
}
logstream.Err(fmt.Sprintln("Error searching into LDAP: ", err))
s := strings.Replace(err.Error(), " ", "%20", -1)
response := fmt.Sprintf("500 %s\n", s)
@@ -99,6 +104,14 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
return
}
// L'attribut n'existe pas
if len(result.Entries[0].Attributes) == 0 {
logstream.Err(fmt.Sprintf("Error searching into LDAP: Attribute %s not found for entry %s\n", *ldapAttr, result.Entries[0]))
s := strings.Replace(fmt.Sprintf("Attribute not found: %s", *ldapAttr), " ", "%20", -1)
response := fmt.Sprintf("500 %s\n", s)
connClt.Write([]byte(response))
return
}
if len(result.Entries) != 1 {
if *debug {
logstream.Debug("Got no result, returning 500")
@@ -126,7 +139,6 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
response := fmt.Sprintf("500 %s\n", s)
connClt.Write([]byte(response))
return
}
}
@@ -166,6 +178,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)")
ldapAttr = fs.String("ldapAttr", "relayName", "LDAP attribute containing the relay name to return")
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)")
@@ -188,4 +201,3 @@ func main() {
defer logstream.Close()
run()
}