Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 90e0af59c3 | |||
| b7a6689b9b | |||
| ab597c221b | |||
| add02d2554 | |||
| a4adf1210a | |||
| cb9cf1ab36 | |||
| 17d41e4f61 | |||
| 558519f048 | |||
| 122f76c3f9 |
@@ -6,5 +6,5 @@ ldapPass here_lies_the_password
|
|||||||
# "domain" sera ajoute a relayName pour former le FQDN
|
# "domain" sera ajoute a relayName pour former le FQDN
|
||||||
domain example.org
|
domain example.org
|
||||||
# Le nom de l'attribut LDAP contenant le relais a retourner
|
# Le nom de l'attribut LDAP contenant le relais a retourner
|
||||||
lapAttr relayName
|
ldapAttr relayName
|
||||||
|
|
||||||
|
|||||||
+46
-20
@@ -10,6 +10,7 @@
|
|||||||
// Use a single LDAP connection protected by a mutex
|
// Use a single LDAP connection protected by a mutex
|
||||||
//
|
//
|
||||||
// v0.9.6 : add PID file
|
// v0.9.6 : add PID file
|
||||||
|
// v0.9.8 : Check MX format with regex
|
||||||
//
|
//
|
||||||
|
|
||||||
package main
|
package main
|
||||||
@@ -21,6 +22,7 @@ import (
|
|||||||
"log/syslog"
|
"log/syslog"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -31,7 +33,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
version = "0.9.7.1"
|
version = "0.9.9"
|
||||||
|
// Match le nom de domaine, soit les 2 dernieres chaines separees par un point.
|
||||||
|
// Peut aussi terminer par un point (Ex: srv.domaine.com. => domaine.com)
|
||||||
|
DomainRegexpFormat = `([0-9A-Za-z\-]*)\.([0-9A-Za-z\-]*)(?:\.)?$`
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -50,22 +55,38 @@ var (
|
|||||||
timeout *int
|
timeout *int
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// attempt is useless now. It was introduced to break after N attempts, but this is not implemented. Should it be?
|
||||||
func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
|
func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
result, err := conLdap.Search(searchReq)
|
result, err := conLdap.Search(searchReq)
|
||||||
mutex.Unlock()
|
mutex.Unlock()
|
||||||
// Let's just manage connection errors here
|
// Let's just manage connection errors here
|
||||||
if err != nil && strings.HasSuffix(err.Error(), "ldap: connection closed") {
|
if err != nil {
|
||||||
logstream.Err("LDAP connection closed, retrying")
|
if strings.HasSuffix(err.Error(), "ldap: connection closed") {
|
||||||
mutex.Lock()
|
logstream.Err("LDAP connection closed, retrying")
|
||||||
conLdap.Close()
|
mutex.Lock()
|
||||||
conLdap, err = connectLdap()
|
conLdap.Close()
|
||||||
mutex.Unlock()
|
conLdap, err = connectLdap()
|
||||||
if err != nil {
|
mutex.Unlock()
|
||||||
return result, err
|
if err != nil {
|
||||||
} else {
|
return result, err
|
||||||
attempt = attempt + 1
|
} else {
|
||||||
return ldapSearch(searchReq, attempt)
|
attempt = attempt + 1
|
||||||
|
return ldapSearch(searchReq, attempt)
|
||||||
|
}
|
||||||
|
} else if err == ldap.ErrNilConnection {
|
||||||
|
logstream.Err("LDAP connection is nil, reconnecting")
|
||||||
|
mutex.Lock()
|
||||||
|
// conLdap is nil so this would be a sigsegv/panic
|
||||||
|
//conLdap.Close()
|
||||||
|
conLdap, err = connectLdap()
|
||||||
|
mutex.Unlock()
|
||||||
|
if err != nil {
|
||||||
|
return result, err
|
||||||
|
} else {
|
||||||
|
attempt = attempt + 1
|
||||||
|
return ldapSearch(searchReq, attempt)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,11 +106,14 @@ func sendResponse(con net.Conn, respMsg string, respCode int) error {
|
|||||||
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
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
|
|
||||||
|
re := regexp.MustCompile(DomainRegexpFormat)
|
||||||
|
|
||||||
// Close connection when this function ends
|
// Close connection when this function ends
|
||||||
defer func() {
|
defer func() {
|
||||||
logstream.Info("Closing connection")
|
logstream.Debug("Closing connection")
|
||||||
connClt.Close()
|
connClt.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -103,7 +127,7 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
readlen, err := connClt.Read(buf)
|
readlen, err := connClt.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Dont notice if client closed connection
|
// Dont notice if client closed connection
|
||||||
if err.Error() != "EOF" {
|
if err.Error() != "EOF" && !strings.HasSuffix(err.Error(), "i/o timeout") {
|
||||||
logstream.Err(fmt.Sprintln("Error reading connection :", err.Error()))
|
logstream.Err(fmt.Sprintln("Error reading connection :", err.Error()))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -130,13 +154,15 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
mxslic := strings.Split(mxs[0].Host, ".")
|
// On recuperer le nom de domaine de 1er niveau
|
||||||
// les MXs terminent par '.', on le retire
|
group := re.FindSubmatch([]byte(mxs[0].Host))
|
||||||
if mxs[0].Host[len(mxs[0].Host)-1] == '.' {
|
if len(group) < 3 || len(group[2]) == 0 {
|
||||||
mxdom = strings.Join(mxslic[len(mxslic)-3:len(mxslic)-1], ".")
|
logstream.Err(fmt.Sprintln("MX format incorrect: ", mxs[0].Host))
|
||||||
} else {
|
s := fmt.Sprintf("MX format incorrect: %s", mxs[0].Host)
|
||||||
mxdom = strings.Join(mxslic[len(mxslic)-2:len(mxslic)], ".")
|
sendResponse(connClt, s, 500)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
mxdom = string(group[1]) + "." + string(group[2])
|
||||||
|
|
||||||
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,
|
||||||
|
|||||||
Reference in New Issue
Block a user