Reconnectto LDAP after connection went away
This commit is contained in:
+41
-13
@@ -27,7 +27,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
version = "0.9.3"
|
version = "0.9.4"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -43,6 +43,28 @@ var (
|
|||||||
defDomain *string
|
defDomain *string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func ldapSearch(conLdap *ldap.Conn, searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
|
||||||
|
mutex.Lock()
|
||||||
|
result, err := conLdap.Search(searchReq)
|
||||||
|
mutex.Unlock()
|
||||||
|
// Let's just manage connection errors here
|
||||||
|
if err != nil && strings.HasSuffix(err.Error(), "ldap: connection closed") {
|
||||||
|
logstream.Err("LDAP connection closed, retrying")
|
||||||
|
conLdap.Close()
|
||||||
|
conLdap, err = connectLdap()
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
} else {
|
||||||
|
attempt = attempt + 1
|
||||||
|
return ldapSearch(conLdap, searchReq, attempt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
||||||
var s string
|
var s string
|
||||||
var mxdom string
|
var mxdom string
|
||||||
@@ -89,14 +111,8 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,
|
searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,
|
||||||
false, filter, []string{*ldapAttr}, []ldap.Control{})
|
false, filter, []string{*ldapAttr}, []ldap.Control{})
|
||||||
|
|
||||||
mutex.Lock()
|
result, err := ldapSearch(conLdap, searchReq, 0)
|
||||||
result, err := conLdap.Search(searchReq)
|
|
||||||
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)
|
||||||
@@ -142,6 +158,21 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func connectLdap() (*ldap.Conn, error) {
|
||||||
|
conLdap, err := ldap.DialURL(*ldapURL)
|
||||||
|
if err != nil {
|
||||||
|
logstream.Err(fmt.Sprintln("Error dialing LDAP: ", err))
|
||||||
|
return conLdap, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = conLdap.Bind(*ldapUser, *ldapPass)
|
||||||
|
if err != nil {
|
||||||
|
logstream.Err(fmt.Sprintln("Error binding LDAP: ", err))
|
||||||
|
return conLdap, err
|
||||||
|
}
|
||||||
|
return conLdap, err
|
||||||
|
}
|
||||||
|
|
||||||
func run() {
|
func run() {
|
||||||
logstream.Info("start")
|
logstream.Info("start")
|
||||||
defer logstream.Info("exit")
|
defer logstream.Info("exit")
|
||||||
@@ -150,12 +181,8 @@ func run() {
|
|||||||
log.Fatal(fmt.Sprintln("Error listening: ", err))
|
log.Fatal(fmt.Sprintln("Error listening: ", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
conLdap, err := ldap.DialURL(*ldapURL)
|
conLdap, err := connectLdap()
|
||||||
defer conLdap.Close()
|
|
||||||
|
|
||||||
err = conLdap.Bind(*ldapUser, *ldapPass)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logstream.Err(fmt.Sprintln("Error binding LDAP: ", err))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,6 +193,7 @@ func run() {
|
|||||||
}
|
}
|
||||||
go handleConnection(connClt, conLdap)
|
go handleConnection(connClt, conLdap)
|
||||||
}
|
}
|
||||||
|
conLdap.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user