fix everything

This commit is contained in:
Kylie Czar
2022-08-27 20:15:10 -03:00
parent 6455a1a71c
commit 5541c8f827
5 changed files with 51 additions and 34 deletions

View File

@@ -4,17 +4,16 @@ import (
"crypto/tls"
"fmt"
"net/smtp"
"log"
"github.com/lor00x/goldap/message"
ldap "github.com/vjeantet/ldapserver"
"gt.kalli.st/czar/fsldap/config"
"gt.kalli.st/czar/fsldap/utils"
)
var smtpHost = config.SmtpHostname
var smtpHostPort = fmt.Sprintf("%s:%s", smtpHost,config.SmtpPort)
var smtpHost = utils.Env("SMTP_HOSTNAME")
var smtpPort = utils.Env("SMTP_PORT")
var smtpHostPort = fmt.Sprintf("%s:%s", smtpHost,smtpPort)
var tlsconfig = &tls.Config {
InsecureSkipVerify: true,
@@ -22,28 +21,51 @@ var tlsconfig = &tls.Config {
}
func Bind(w ldap.ResponseWriter, m *ldap.Message) {
log.Print("BIND REQUEST", m.LDAPMessage.ProtocolOpName())
r := m.GetBindRequest()
res := ldap.NewBindResponse(ldap.LDAPResultSuccess)
name := string(r.Name())
if(len(name) == 0) {
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
res.SetDiagnosticMessage("invalid credentials")
w.Write(res)
return
}
log.Print("Name",name)
password := string(r.AuthenticationSimple())
log.Print("BEFORE USER")
user := utils.GetUser(name)
log.Print("AFTER USER")
mail := utils.GetMail(user)
log.Print("Auth ", smtpHost, " ",user, " ", mail)
auth := smtp.PlainAuth("", mail, password, smtpHost)
log.Print("Dial ", smtpHostPort)
client, error := smtp.Dial(smtpHostPort)
if error != nil {
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
log.Print("invalid credentials DIAL ", error, " host: ",smtpHostPort)
res.SetDiagnosticMessage("invalid credentials")
w.Write(res)
return
}
log.Print("StartTLS")
client.StartTLS(tlsconfig)
log.Print("Client AUTH")
err := client.Auth(auth)
if err != nil {
res.SetResultCode(ldap.LDAPResultInvalidCredentials)
log.Print("invalid credentials AUTH ", err)
res.SetDiagnosticMessage("invalid credentials")
w.Write(res)
return
}
log.Print("Success")
w.Write(res)
}
@@ -53,21 +75,26 @@ func WhoAmI(w ldap.ResponseWriter, m *ldap.Message) {
}
func Search(w ldap.ResponseWriter, m *ldap.Message) {
log.Print("Searching")
r := m.GetSearchRequest()
select {
case <-m.Done:
return
default:
}
name := string(r.FilterString())
name := string(r.BaseObject())
log.Print("Name ",name)
if name == ""{
return
}
user := utils.GetSearchUser(name)
user := utils.GetUser(name)
mail := utils.GetMail(user)
attr := utils.GetLdapName(user)
entry := ldap.NewSearchResultEntry(attr)
entry.AddAttribute("mail", message.AttributeValue(mail))
entry.AddAttribute("cn", message.AttributeValue(user))
log.Print(entry)
w.Write(entry)
response := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
w.Write(response)