Files
ldaps/examples/server.go
b9bf4d1baf Start tls by @shipperizer (#1)
* switch nmcclain/asn1-ber to go-asn1-ber/and1-ber.
This newer package has ben updated with tests, and looks to actually be supported.
It is also is a forked version of nmcclain/asn1-ber, so supports the same API.

* no need to print errors if client disconnects early and unexpectedly

* added in Close method on server to avoid a race condition

* test ssl certs are untrusted, ldapsearch needs to be told to allow untrusted certs

* Extra time.Wait causes the test to fail

* removed singaling with a custom quit chanel

* feat: support StartTLS in server

* lint: fmt and imports

* feat: rename library to be used by glauth

* chore: renamed packages

---------

Co-authored-by: Daniel Barney <daniel@thoughtplot.io>
Co-authored-by: Mark Rushakoff <mark@influxdata.com>
Co-authored-by: shipperizer <alexcabb@gmail.com>
2023-12-10 14:58:23 -08:00

71 lines
2.3 KiB
Go

//go:build ignore
// +build ignore
package main
import (
"log"
"net"
"github.com/glauth/ldap"
)
/////////////
// Sample searches you can try against this simple LDAP server:
//
// ldapsearch -H ldap://localhost:3389 -x -b 'dn=test,dn=com'
// ldapsearch -H ldap://localhost:3389 -x -b 'dn=test,dn=com' 'cn=ned'
// ldapsearch -H ldap://localhost:3389 -x -b 'dn=test,dn=com' 'uidnumber=5000'
/////////////
// /////////// Run a simple LDAP server
func main() {
s := ldap.NewServer()
// register Bind and Search function handlers
handler := ldapHandler{}
s.BindFunc("", handler)
s.SearchFunc("", handler)
// start the server
listen := "localhost:3389"
log.Printf("Starting example LDAP server on %s", listen)
if err := s.ListenAndServe(listen); err != nil {
log.Fatalf("LDAP Server Failed: %s", err.Error())
}
}
type ldapHandler struct {
}
// /////////// Allow anonymous binds only
func (h ldapHandler) Bind(bindDN, bindSimplePw string, conn net.Conn) (ldap.LDAPResultCode, error) {
if bindDN == "" && bindSimplePw == "" {
return ldap.LDAPResultSuccess, nil
}
return ldap.LDAPResultInvalidCredentials, nil
}
// /////////// Return some hardcoded search results - we'll respond to any baseDN for testing
func (h ldapHandler) Search(boundDN string, searchReq ldap.SearchRequest, conn net.Conn) (ldap.ServerSearchResult, error) {
entries := []*ldap.Entry{
&ldap.Entry{"cn=ned," + searchReq.BaseDN, []*ldap.EntryAttribute{
&ldap.EntryAttribute{"cn", []string{"ned"}},
&ldap.EntryAttribute{"uidNumber", []string{"5000"}},
&ldap.EntryAttribute{"accountStatus", []string{"active"}},
&ldap.EntryAttribute{"uid", []string{"ned"}},
&ldap.EntryAttribute{"description", []string{"ned"}},
&ldap.EntryAttribute{"objectClass", []string{"posixAccount"}},
}},
&ldap.Entry{"cn=trent," + searchReq.BaseDN, []*ldap.EntryAttribute{
&ldap.EntryAttribute{"cn", []string{"trent"}},
&ldap.EntryAttribute{"uidNumber", []string{"5005"}},
&ldap.EntryAttribute{"accountStatus", []string{"active"}},
&ldap.EntryAttribute{"uid", []string{"trent"}},
&ldap.EntryAttribute{"description", []string{"trent"}},
&ldap.EntryAttribute{"objectClass", []string{"posixAccount"}},
}},
}
return ldap.ServerSearchResult{entries, []string{}, []ldap.Control{}, ldap.LDAPResultSuccess}, nil
}