diff options
author | ned <ned@appliedtrust.com> | 2014-11-12 22:52:16 +0100 |
---|---|---|
committer | ned <ned@appliedtrust.com> | 2014-11-12 22:52:16 +0100 |
commit | f4e67fa4cd924fbe6f271611514caf5589e6a6e5 (patch) | |
tree | e696dd77956b8e1ce1aa342a35036db3a9494575 /examples/server.go | |
parent | Ping / Abandon request should not expect response (diff) | |
download | ldap-f4e67fa4cd924fbe6f271611514caf5589e6a6e5.tar ldap-f4e67fa4cd924fbe6f271611514caf5589e6a6e5.tar.gz ldap-f4e67fa4cd924fbe6f271611514caf5589e6a6e5.tar.bz2 ldap-f4e67fa4cd924fbe6f271611514caf5589e6a6e5.tar.lz ldap-f4e67fa4cd924fbe6f271611514caf5589e6a6e5.tar.xz ldap-f4e67fa4cd924fbe6f271611514caf5589e6a6e5.tar.zst ldap-f4e67fa4cd924fbe6f271611514caf5589e6a6e5.zip |
Diffstat (limited to '')
-rw-r--r-- | examples/server.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/server.go b/examples/server.go new file mode 100644 index 0000000..dca74ed --- /dev/null +++ b/examples/server.go @@ -0,0 +1,64 @@ +package main + +import ( + "github.com/nmcclain/ldap" + "log" + "net" +) + +///////////// +// 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 + if err := s.ListenAndServe("localhost:3389"); err != nil { + log.Fatal("LDAP Server Failed: %s", err.Error()) + } +} + +type ldapHandler struct { +} + +///////////// Allow anonymous binds only +func (h ldapHandler) Bind(bindDN, bindSimplePw string, conn net.Conn) (uint64, 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 +} |