Allow client to issue more numerous requests on the same connection
This commit is contained in:
+99
-88
@@ -26,11 +26,11 @@ import (
|
|||||||
|
|
||||||
"github.com/go-ldap/ldap/v3"
|
"github.com/go-ldap/ldap/v3"
|
||||||
"github.com/peterbourgon/ff"
|
"github.com/peterbourgon/ff"
|
||||||
"github.com/tabalt/pidfile"
|
"github.com/tabalt/pidfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
version = "0.9.6"
|
version = "0.9.7"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -45,7 +45,7 @@ var (
|
|||||||
ldapPass *string
|
ldapPass *string
|
||||||
ldapAttr *string
|
ldapAttr *string
|
||||||
defDomain *string
|
defDomain *string
|
||||||
pidFilePath *string
|
pidFilePath *string
|
||||||
)
|
)
|
||||||
|
|
||||||
func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
|
func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
|
||||||
@@ -70,98 +70,109 @@ 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()
|
// It's client responsability to close connection
|
||||||
readlen, err := connClt.Read(buf)
|
//defer connClt.Close()
|
||||||
if err != nil {
|
for {
|
||||||
fmt.Println("Error reading:", err.Error())
|
readlen, err := connClt.Read(buf)
|
||||||
}
|
|
||||||
|
|
||||||
// 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])
|
|
||||||
domain := strings.Split(mail, "@")[1]
|
|
||||||
mxs, err := net.LookupMX(domain)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logstream.Err(fmt.Sprintln("Error lookup mx: ", err))
|
// Dont notice if client closed connection
|
||||||
s := strings.Replace(fmt.Sprintf("Error lookup MX: %s", err.Error()), " ", "%20", -1)
|
if err.Error() != "EOF" {
|
||||||
response := fmt.Sprintf("500 %s\n", s)
|
logstream.Err("Error reading:", err.Error())
|
||||||
connClt.Write([]byte(response))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// 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 .
|
|
||||||
// Considerons qu'il n'y aura jamais de "one letter TLD"
|
|
||||||
if len(mxs[0].Host) < 4 {
|
|
||||||
logstream.Err(fmt.Sprintln("No usable mx found"))
|
|
||||||
s := strings.Replace("No usable MX found", " ", "%20", -1)
|
|
||||||
resp := fmt.Sprintf("500 %s\n", s)
|
|
||||||
connClt.Write([]byte(resp))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
mxslic := strings.Split(mxs[0].Host, ".")
|
|
||||||
// les MXs terminent par '.', on le retire
|
|
||||||
if mxs[0].Host[len(mxs[0].Host)-1] == '.' {
|
|
||||||
mxdom = strings.Join(mxslic[len(mxslic)-3:len(mxslic)-1], ".")
|
|
||||||
} else {
|
|
||||||
mxdom = strings.Join(mxslic[len(mxslic)-2:len(mxslic)], ".")
|
|
||||||
}
|
|
||||||
|
|
||||||
filter := fmt.Sprintf("(dc=%s)", ldap.EscapeFilter(mxdom))
|
|
||||||
searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,
|
|
||||||
false, filter, []string{*ldapAttr}, []ldap.Control{})
|
|
||||||
|
|
||||||
result, err := ldapSearch(searchReq, 0)
|
|
||||||
if err != nil {
|
|
||||||
logstream.Err(fmt.Sprintln("Error searching into LDAP: ", err))
|
|
||||||
s := strings.Replace(err.Error(), " ", "%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")
|
|
||||||
}
|
}
|
||||||
s := strings.Replace("No route defined", " ", "%20", -1)
|
|
||||||
response := fmt.Sprintf("500 %s\n", s)
|
|
||||||
connClt.Write([]byte(response))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// L'attribut n'existe pas
|
// 1) Recupere le MX du domaine de l'adresse mail recue
|
||||||
if len(result.Entries[0].Attributes) == 0 {
|
if strings.HasPrefix(string(buf[:readlen-1]), "get ") && strings.Contains(string(buf[:readlen-1]), "@") {
|
||||||
logstream.Err(fmt.Sprintf("Error searching into LDAP: Attribute %s not found for entry %s\n", *ldapAttr, result.Entries[0]))
|
mail := string(buf[4 : readlen-1])
|
||||||
s := strings.Replace(fmt.Sprintf("Attribute not found: %s", *ldapAttr), " ", "%20", -1)
|
domain := strings.Split(mail, "@")[1]
|
||||||
response := fmt.Sprintf("500 %s\n", s)
|
mxs, err := net.LookupMX(domain)
|
||||||
connClt.Write([]byte(response))
|
if err != nil {
|
||||||
return
|
logstream.Err(fmt.Sprintln("Error lookup mx: ", err))
|
||||||
}
|
s := fmt.Sprintf("Error lookup MX: %s", err.Error())
|
||||||
|
sendResponse(connClt, s, 500)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 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 .
|
||||||
|
// Considerons qu'il n'y aura jamais de "one letter TLD"
|
||||||
|
if len(mxs[0].Host) < 4 {
|
||||||
|
logstream.Err(fmt.Sprintln("No usable mx found"))
|
||||||
|
s := "No usable MX found"
|
||||||
|
sendResponse(connClt, s, 500)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if *debug {
|
mxslic := strings.Split(mxs[0].Host, ".")
|
||||||
logstream.Debug("Got result, returning " + result.Entries[0].Attributes[0].Values[0])
|
// les MXs terminent par '.', on le retire
|
||||||
}
|
if mxs[0].Host[len(mxs[0].Host)-1] == '.' {
|
||||||
// Check if result is a FQDN, if not append defDomain
|
mxdom = strings.Join(mxslic[len(mxslic)-3:len(mxslic)-1], ".")
|
||||||
if strings.Contains(string(result.Entries[0].Attributes[0].Values[0]), ".") {
|
} else {
|
||||||
s = strings.Replace(fmt.Sprintf("FILTER relay:[%s]", result.Entries[0].Attributes[0].Values[0]), " ", "%20", -1)
|
mxdom = strings.Join(mxslic[len(mxslic)-2:len(mxslic)], ".")
|
||||||
|
}
|
||||||
|
|
||||||
|
filter := fmt.Sprintf("(dc=%s)", ldap.EscapeFilter(mxdom))
|
||||||
|
searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,
|
||||||
|
false, filter, []string{*ldapAttr}, []ldap.Control{})
|
||||||
|
|
||||||
|
result, err := ldapSearch(searchReq, 0)
|
||||||
|
if err != nil {
|
||||||
|
logstream.Err(fmt.Sprintln("Error searching into LDAP: ", err))
|
||||||
|
s := err.Error()
|
||||||
|
sendResponse(connClt, s, 500)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result.Entries) != 1 {
|
||||||
|
if *debug {
|
||||||
|
logstream.Debug("Got no result, returning 500")
|
||||||
|
}
|
||||||
|
s := "No route defined"
|
||||||
|
sendResponse(connClt, s, 500)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 := fmt.Sprintf("Attribute not found: %s", *ldapAttr)
|
||||||
|
sendResponse(connClt, s, 500)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if *debug {
|
||||||
|
logstream.Debug("Got result, returning " + result.Entries[0].Attributes[0].Values[0])
|
||||||
|
}
|
||||||
|
// Check if result is a FQDN, if not append defDomain
|
||||||
|
if strings.Contains(string(result.Entries[0].Attributes[0].Values[0]), ".") {
|
||||||
|
s = fmt.Sprintf("FILTER relay:[%s]", result.Entries[0].Attributes[0].Values[0])
|
||||||
|
} else {
|
||||||
|
s = fmt.Sprintf("FILTER relay:[%s]", fmt.Sprintf("%s.%s", result.Entries[0].Attributes[0].Values[0], *defDomain))
|
||||||
|
}
|
||||||
|
sendResponse(connClt, s, 200)
|
||||||
} else {
|
} else {
|
||||||
s = strings.Replace(fmt.Sprintf("FILTER relay:[%s]", fmt.Sprintf("%s.%s", result.Entries[0].Attributes[0].Values[0], *defDomain)), " ", "%20", -1)
|
if *debug {
|
||||||
|
logstream.Debug("Incorrect input format : " + string(buf[:readlen-1]))
|
||||||
|
}
|
||||||
|
s := "Incorrect input format"
|
||||||
|
sendResponse(connClt, s, 500)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
connClt.Write([]byte(fmt.Sprintf("200 %s\n", s)))
|
|
||||||
} else {
|
|
||||||
if *debug {
|
|
||||||
logstream.Debug("Incorrect input format : " + string(buf[:readlen-1]))
|
|
||||||
}
|
|
||||||
s := strings.Replace("Incorrect input format", " ", "%20", -1)
|
|
||||||
response := fmt.Sprintf("500 %s\n", s)
|
|
||||||
connClt.Write([]byte(response))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,7 +228,7 @@ 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)")
|
||||||
_ = fs.String("config", "", "config file (optional)")
|
_ = fs.String("config", "", "config file (optional)")
|
||||||
|
|
||||||
// Surcharge de la fonction Usage()
|
// Surcharge de la fonction Usage()
|
||||||
@@ -233,11 +244,11 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if pid, err := pidfile.Create(*pidFilePath); err != nil {
|
if pid, err := pidfile.Create(*pidFilePath); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
} else {
|
} else {
|
||||||
defer pid.Clear()
|
defer pid.Clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
if logstream, e = syslog.New(syslog.LOG_MAIL, "mxrouter"); e != nil {
|
if logstream, e = syslog.New(syslog.LOG_MAIL, "mxrouter"); e != nil {
|
||||||
log.Fatal(e)
|
log.Fatal(e)
|
||||||
|
|||||||
Reference in New Issue
Block a user