Ajout config ldapAttr + msg erreur si inexistant dnas LDAP
This commit is contained in:
+17
-5
@@ -1,7 +1,7 @@
|
|||||||
// mxrouter
|
// mxrouter
|
||||||
// Copyright (c) 2020 yo000 <johan@nosd.in>
|
// 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
|
// checkmx : postfix tcp_table server which returns second-level domain name of the MX of a domain
|
||||||
// ex :
|
// ex :
|
||||||
@@ -27,7 +27,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
version = "0.9.1"
|
version = "0.9.3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -39,6 +39,7 @@ var (
|
|||||||
ldapBaseDN *string
|
ldapBaseDN *string
|
||||||
ldapUser *string
|
ldapUser *string
|
||||||
ldapPass *string
|
ldapPass *string
|
||||||
|
ldapAttr *string
|
||||||
defDomain *string
|
defDomain *string
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -86,12 +87,16 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
|
|
||||||
filter := fmt.Sprintf("(dc=%s)", ldap.EscapeFilter(mxdom))
|
filter := fmt.Sprintf("(dc=%s)", ldap.EscapeFilter(mxdom))
|
||||||
searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,
|
searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,
|
||||||
false, filter, []string{"relayName"}, []ldap.Control{})
|
false, filter, []string{*ldapAttr}, []ldap.Control{})
|
||||||
|
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
result, err := conLdap.Search(searchReq)
|
result, err := conLdap.Search(searchReq)
|
||||||
mutex.Unlock()
|
mutex.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err.Error == "ldap: connection closed" {
|
||||||
|
// TODO : Reconnect to LDAP
|
||||||
|
return
|
||||||
|
}
|
||||||
logstream.Err(fmt.Sprintln("Error searching into LDAP: ", err))
|
logstream.Err(fmt.Sprintln("Error searching into LDAP: ", err))
|
||||||
s := strings.Replace(err.Error(), " ", "%20", -1)
|
s := strings.Replace(err.Error(), " ", "%20", -1)
|
||||||
response := fmt.Sprintf("500 %s\n", s)
|
response := fmt.Sprintf("500 %s\n", s)
|
||||||
@@ -99,6 +104,14 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
return
|
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 len(result.Entries) != 1 {
|
||||||
if *debug {
|
if *debug {
|
||||||
logstream.Debug("Got no result, returning 500")
|
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)
|
response := fmt.Sprintf("500 %s\n", s)
|
||||||
connClt.Write([]byte(response))
|
connClt.Write([]byte(response))
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,6 +178,7 @@ func main() {
|
|||||||
ldapBaseDN = fs.String("ldapDN", "", "LDAP base DN (also via LDAPDN env var)")
|
ldapBaseDN = fs.String("ldapDN", "", "LDAP base DN (also via LDAPDN env var)")
|
||||||
ldapUser = fs.String("ldapUser", "", "LDAP user DN (also via LDAPUSER 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)")
|
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)")
|
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)")
|
_ = fs.String("config", "", "config file (optional)")
|
||||||
|
|
||||||
@@ -188,4 +201,3 @@ func main() {
|
|||||||
defer logstream.Close()
|
defer logstream.Close()
|
||||||
run()
|
run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user