3 Commits

+55 -31
View File
@@ -23,6 +23,7 @@ import (
"os" "os"
"strings" "strings"
"sync" "sync"
"time"
"github.com/go-ldap/ldap/v3" "github.com/go-ldap/ldap/v3"
"github.com/peterbourgon/ff" "github.com/peterbourgon/ff"
@@ -30,7 +31,7 @@ import (
) )
const ( const (
version = "0.9.6" version = "0.9.7.1"
) )
var ( var (
@@ -46,6 +47,7 @@ var (
ldapAttr *string ldapAttr *string
defDomain *string defDomain *string
pidFilePath *string pidFilePath *string
timeout *int
) )
func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) { func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
@@ -70,15 +72,41 @@ func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult,
return result, err return result, err
} }
func sendResponse(con net.Conn, respMsg string, respCode int) error {
response := fmt.Sprintf("%d %s\n", respCode, strings.Replace(respMsg, " ", "%20", -1))
_, err := con.Write([]byte(response))
if err != nil {
return err
}
return nil
}
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)
defer connClt.Close() // Close connection when this function ends
defer func() {
logstream.Info("Closing connection")
connClt.Close()
}()
timeoutDuration := time.Duration(*timeout) * time.Second
for {
// Set a deadline for reading. Read operation will fail if no data
// is received after deadline.
connClt.SetReadDeadline(time.Now().Add(timeoutDuration))
readlen, err := connClt.Read(buf) readlen, err := connClt.Read(buf)
if err != nil { if err != nil {
fmt.Println("Error reading:", err.Error()) // Dont notice if client closed connection
if err.Error() != "EOF" {
logstream.Err(fmt.Sprintln("Error reading connection :", err.Error()))
}
return
} }
// 1) Recupere le MX du domaine de l'adresse mail recue // 1) Recupere le MX du domaine de l'adresse mail recue
@@ -88,20 +116,18 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
mxs, err := net.LookupMX(domain) mxs, err := net.LookupMX(domain)
if err != nil { if err != nil {
logstream.Err(fmt.Sprintln("Error lookup mx: ", err)) logstream.Err(fmt.Sprintln("Error lookup mx: ", err))
s := strings.Replace(fmt.Sprintf("Error lookup MX: %s", err.Error()), " ", "%20", -1) s := fmt.Sprintf("Error lookup MX: %s", err.Error())
response := fmt.Sprintf("500 %s\n", s) sendResponse(connClt, s, 500)
connClt.Write([]byte(response)) continue
return
} }
// 2) Requete LDAP pour voir si le domaine de second niveau du MX possede un routage particulier // 2) Requete LDAP pour voir si le domaine de second niveau du MX possede un routage particulier
// Protection contre ca : example.org. 72 IN MX 0 . // Protection contre ca : example.org. 72 IN MX 0 .
// Considerons qu'il n'y aura jamais de "one letter TLD" // Considerons qu'il n'y aura jamais de "one letter TLD"
if len(mxs[0].Host) < 4 { if len(mxs[0].Host) < 4 {
logstream.Err(fmt.Sprintln("No usable mx found")) logstream.Err(fmt.Sprintln("No usable mx found"))
s := strings.Replace("No usable MX found", " ", "%20", -1) s := "No usable MX found"
resp := fmt.Sprintf("500 %s\n", s) sendResponse(connClt, s, 500)
connClt.Write([]byte(resp)) continue
return
} }
mxslic := strings.Split(mxs[0].Host, ".") mxslic := strings.Split(mxs[0].Host, ".")
@@ -119,29 +145,26 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
result, err := ldapSearch(searchReq, 0) result, err := ldapSearch(searchReq, 0)
if err != nil { if err != nil {
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 := err.Error()
response := fmt.Sprintf("500 %s\n", s) sendResponse(connClt, s, 500)
connClt.Write([]byte(response)) continue
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")
} }
s := strings.Replace("No route defined", " ", "%20", -1) s := "No route defined"
response := fmt.Sprintf("500 %s\n", s) sendResponse(connClt, s, 500)
connClt.Write([]byte(response)) continue
return
} }
// L'attribut n'existe pas // L'attribut n'existe pas
if len(result.Entries[0].Attributes) == 0 { 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])) 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) s := fmt.Sprintf("Attribute not found: %s", *ldapAttr)
response := fmt.Sprintf("500 %s\n", s) sendResponse(connClt, s, 500)
connClt.Write([]byte(response)) continue
return
} }
if *debug { if *debug {
@@ -149,19 +172,19 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
} }
// Check if result is a FQDN, if not append defDomain // Check if result is a FQDN, if not append defDomain
if strings.Contains(string(result.Entries[0].Attributes[0].Values[0]), ".") { 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) s = fmt.Sprintf("FILTER relay:[%s]", result.Entries[0].Attributes[0].Values[0])
} else { } else {
s = strings.Replace(fmt.Sprintf("FILTER relay:[%s]", fmt.Sprintf("%s.%s", result.Entries[0].Attributes[0].Values[0], *defDomain)), " ", "%20", -1) s = fmt.Sprintf("FILTER relay:[%s]", fmt.Sprintf("%s.%s", result.Entries[0].Attributes[0].Values[0], *defDomain))
} }
connClt.Write([]byte(fmt.Sprintf("200 %s\n", s))) sendResponse(connClt, s, 200)
} else { } else {
if *debug { if *debug {
logstream.Debug("Incorrect input format : " + string(buf[:readlen-1])) logstream.Debug("Incorrect input format : " + string(buf[:readlen-1]))
} }
s := strings.Replace("Incorrect input format", " ", "%20", -1) s := "Incorrect input format"
response := fmt.Sprintf("500 %s\n", s) sendResponse(connClt, s, 500)
connClt.Write([]byte(response)) continue
return }
} }
} }
@@ -217,7 +240,8 @@ func main() {
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") 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)")
pidFilePath= fs.String("pidfile", "/var/run/mxrouter/mxrouter.pid", "PID File (also via PIDFILE env var)") pidFilePath = fs.String("pidfile", "/var/run/mxrouter/mxrouter.pid", "PID File (also via PIDFILE env var)")
timeout = fs.Int("timeout", 5, "timeout in seconds")
_ = fs.String("config", "", "config file (optional)") _ = fs.String("config", "", "config file (optional)")
// Surcharge de la fonction Usage() // Surcharge de la fonction Usage()