fix search for mediawiki
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"log"
|
|
||||||
"github.com/lor00x/goldap/message"
|
"github.com/lor00x/goldap/message"
|
||||||
ldap "github.com/vjeantet/ldapserver"
|
ldap "github.com/vjeantet/ldapserver"
|
||||||
|
|
||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
var smtpHost = utils.Env("SMTP_HOSTNAME")
|
var smtpHost = utils.Env("SMTP_HOSTNAME")
|
||||||
var smtpPort = utils.Env("SMTP_PORT")
|
var smtpPort = utils.Env("SMTP_PORT")
|
||||||
|
var base = utils.Env("LDAP_BASE")
|
||||||
var smtpHostPort = fmt.Sprintf("%s:%s", smtpHost,smtpPort)
|
var smtpHostPort = fmt.Sprintf("%s:%s", smtpHost,smtpPort)
|
||||||
|
|
||||||
var tlsconfig = &tls.Config {
|
var tlsconfig = &tls.Config {
|
||||||
@@ -20,10 +21,7 @@ var tlsconfig = &tls.Config {
|
|||||||
ServerName: smtpHost,
|
ServerName: smtpHost,
|
||||||
}
|
}
|
||||||
|
|
||||||
func Bind(w ldap.ResponseWriter, m *ldap.Message) {
|
func Bind(w ldap.ResponseWriter, m *ldap.Message) {
|
||||||
|
|
||||||
log.Print("BIND REQUEST", m.LDAPMessage.ProtocolOpName())
|
|
||||||
|
|
||||||
r := m.GetBindRequest()
|
r := m.GetBindRequest()
|
||||||
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
|
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
|
||||||
name := string(r.Name())
|
name := string(r.Name())
|
||||||
@@ -34,38 +32,29 @@ func Bind(w ldap.ResponseWriter, m *ldap.Message) {
|
|||||||
w.Write(res)
|
w.Write(res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Print("Name",name)
|
|
||||||
password := string(r.AuthenticationSimple())
|
password := string(r.AuthenticationSimple())
|
||||||
|
|
||||||
log.Print("BEFORE USER")
|
|
||||||
user := utils.GetUser(name)
|
user := utils.GetUser(name)
|
||||||
log.Print("AFTER USER")
|
|
||||||
|
|
||||||
mail := utils.GetMail(user)
|
mail := utils.GetMail(user)
|
||||||
|
|
||||||
log.Print("Auth ", smtpHost, " ",user, " ", mail)
|
|
||||||
auth := smtp.PlainAuth("", mail, password, smtpHost)
|
auth := smtp.PlainAuth("", mail, password, smtpHost)
|
||||||
log.Print("Dial ", smtpHostPort)
|
|
||||||
client, error := smtp.Dial(smtpHostPort)
|
client, error := smtp.Dial(smtpHostPort)
|
||||||
if error != nil {
|
if error != nil {
|
||||||
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
|
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
|
||||||
log.Print("invalid credentials DIAL ", error, " host: ",smtpHostPort)
|
|
||||||
res.SetDiagnosticMessage("invalid credentials")
|
res.SetDiagnosticMessage("invalid credentials")
|
||||||
w.Write(res)
|
w.Write(res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Print("StartTLS")
|
|
||||||
client.StartTLS(tlsconfig)
|
client.StartTLS(tlsconfig)
|
||||||
log.Print("Client AUTH")
|
|
||||||
err := client.Auth(auth)
|
err := client.Auth(auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
|
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
|
||||||
log.Print("invalid credentials AUTH ", err)
|
|
||||||
res.SetDiagnosticMessage("invalid credentials")
|
res.SetDiagnosticMessage("invalid credentials")
|
||||||
w.Write(res)
|
w.Write(res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Print("Success")
|
|
||||||
w.Write(res)
|
w.Write(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,26 +64,53 @@ func WhoAmI(w ldap.ResponseWriter, m *ldap.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Search(w ldap.ResponseWriter, m *ldap.Message) {
|
func Search(w ldap.ResponseWriter, m *ldap.Message) {
|
||||||
log.Print("Searching")
|
|
||||||
r := m.GetSearchRequest()
|
r := m.GetSearchRequest()
|
||||||
select {
|
select {
|
||||||
case <-m.Done:
|
case <-m.Done:
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
name := string(r.BaseObject())
|
name := string(r.BaseObject())
|
||||||
log.Print("Name ",name)
|
search := false
|
||||||
if name == ""{
|
|
||||||
|
if name == base {
|
||||||
|
name = string(r.FilterString())
|
||||||
|
search = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
response := ldap.NewSearchResultDoneResponse(ldap.LDAPResultNoSuchObject)
|
||||||
|
w.Write(response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var user string;
|
||||||
|
|
||||||
|
if(search) {
|
||||||
|
user = utils.GetSearchUser(name)
|
||||||
|
} else {
|
||||||
|
user = utils.GetUser(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
if user == ""{
|
||||||
|
response := ldap.NewSearchResultDoneResponse(ldap.LDAPResultNoSuchObject)
|
||||||
|
w.Write(response)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
user := utils.GetUser(name)
|
|
||||||
mail := utils.GetMail(user)
|
mail := utils.GetMail(user)
|
||||||
attr := utils.GetLdapName(user)
|
|
||||||
|
if mail == ""{
|
||||||
|
response := ldap.NewSearchResultDoneResponse(ldap.LDAPResultNoSuchObject)
|
||||||
|
w.Write(response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
attr := utils.GetLdapName(user)
|
||||||
entry := ldap.NewSearchResultEntry(attr)
|
entry := ldap.NewSearchResultEntry(attr)
|
||||||
entry.AddAttribute("mail", message.AttributeValue(mail))
|
entry.AddAttribute("mail", message.AttributeValue(mail))
|
||||||
entry.AddAttribute("cn", message.AttributeValue(user))
|
entry.AddAttribute("cn", message.AttributeValue(user))
|
||||||
log.Print(entry)
|
|
||||||
w.Write(entry)
|
w.Write(entry)
|
||||||
response := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
|
response := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
|
||||||
w.Write(response)
|
w.Write(response)
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMail(user string) string {
|
func GetMail(user string) string {
|
||||||
@@ -15,14 +14,19 @@ func GetLdapName(user string) string {
|
|||||||
|
|
||||||
func GetUser(name string) string {
|
func GetUser(name string) string {
|
||||||
endIndex := len(name) - BaseLenght
|
endIndex := len(name) - BaseLenght
|
||||||
|
if endIndex < 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
user := name[UidLenght:endIndex]
|
user := name[UidLenght:endIndex]
|
||||||
log.Print("User ", user)
|
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSearchUser(name string) string {
|
func GetSearchUser(name string) string {
|
||||||
startIndex := UidLenght + 1
|
startIndex := UidLenght + 1
|
||||||
endIndex := len(name) - 1
|
endIndex := len(name) - 1
|
||||||
|
if endIndex < 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
user := name[startIndex:endIndex]
|
user := name[startIndex:endIndex]
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user