From cb9cf1ab36b6f971423011d6bb6920bd78b81ca7 Mon Sep 17 00:00:00 2001 From: yo Date: Thu, 11 Feb 2021 14:43:16 +0100 Subject: [PATCH] BUFIX : Check MX format with regex instead of old split method --- mxrouter.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/mxrouter.go b/mxrouter.go index 6febc49..3b698d5 100644 --- a/mxrouter.go +++ b/mxrouter.go @@ -10,6 +10,7 @@ // Use a single LDAP connection protected by a mutex // // v0.9.6 : add PID file +// v0.9.7.2 : Check MX format with regex // package main @@ -21,6 +22,7 @@ import ( "log/syslog" "net" "os" + "regexp" "strings" "sync" "time" @@ -31,7 +33,10 @@ import ( ) const ( - version = "0.9.7.1" + version = "0.9.7.2" + // 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 ( @@ -85,8 +90,11 @@ func sendResponse(con net.Conn, respMsg string, respCode int) error { func handleConnection(connClt net.Conn, conLdap *ldap.Conn) { var s string var mxdom string + buf := make([]byte, 1024) + re := regexp.MustCompile(DomainRegexpFormat) + // Close connection when this function ends defer func() { logstream.Debug("Closing connection") @@ -130,13 +138,16 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) { continue } - 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)], ".") + // On recuperer le nom de domaine de 1er niveau + group := re.FindSubmatch([]byte(mxs[0].Host)) + if len(group) < 3 || len(group[2]) == 0 { + logstream.Err(fmt.Sprintln("MX format incorrect: ", mxs[0].Host)) + w + s := fmt.Sprintf("MX format incorrect: %s", err.Error()) + sendResponse(connClt, s, 500) + continue } + mxdom = string(group[1]) + "." + string(group[2]) filter := fmt.Sprintf("(dc=%s)", ldap.EscapeFilter(mxdom)) searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,