package handler import ( "crypto/tls" "fmt" "net/smtp" "log" "github.com/lor00x/goldap/message" ldap "github.com/vjeantet/ldapserver" "gt.kalli.st/czar/fsldap/utils" ) 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, ServerName: smtpHost, } 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) } func WhoAmI(w ldap.ResponseWriter, m *ldap.Message) { res := ldap.NewExtendedResponse(ldap.LDAPResultSuccess) w.Write(res) } func Search(w ldap.ResponseWriter, m *ldap.Message) { log.Print("Searching") r := m.GetSearchRequest() select { case <-m.Done: return default: } name := string(r.BaseObject()) log.Print("Name ",name) if name == ""{ return } 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) }