aboutsummaryrefslogtreecommitdiff
path: root/.examples/server.go
diff options
context:
space:
mode:
authorMarin Ivanov <[email protected]>2019-02-13 04:17:59 +0200
committerMarin Ivanov <[email protected]>2019-02-13 04:37:38 +0200
commit518f72942c1cf751010a532c6189cd0eb0a5323b (patch)
tree72e8d1673717ff1b9865f1c618ea32713b8cd478 /.examples/server.go
parentb6dd6fd0300b606613fb90bf5c5a73bf488e9a1d (diff)
Change dependency urls and package name
* Make Go skip the examples directory
Diffstat (limited to '.examples/server.go')
-rw-r--r--.examples/server.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/.examples/server.go b/.examples/server.go
new file mode 100644
index 0000000..688fb22
--- /dev/null
+++ b/.examples/server.go
@@ -0,0 +1,67 @@
+package main
+
+import (
+ "log"
+ "net"
+
+ ldapserver "github.com/metala/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 := ldapserver.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.Fatal("LDAP Server Failed: %s", err.Error())
+ }
+}
+
+type ldapHandler struct {
+}
+
+///////////// Allow anonymous binds only
+func (h ldapHandler) Bind(bindDN, bindSimplePw string, conn net.Conn) (ldapserver.LDAPResultCode, error) {
+ if bindDN == "" && bindSimplePw == "" {
+ return ldapserver.LDAPResultSuccess, nil
+ }
+ return ldapserver.LDAPResultInvalidCredentials, nil
+}
+
+///////////// Return some hardcoded search results - we'll respond to any baseDN for testing
+func (h ldapHandler) Search(boundDN string, searchReq ldapserver.SearchRequest, conn net.Conn) (ldapserver.ServerSearchResult, error) {
+ entries := []*ldapserver.Entry{
+ &ldapserver.Entry{"cn=ned," + searchReq.BaseDN, []*ldapserver.EntryAttribute{
+ &ldapserver.EntryAttribute{"cn", []string{"ned"}},
+ &ldapserver.EntryAttribute{"uidNumber", []string{"5000"}},
+ &ldapserver.EntryAttribute{"accountStatus", []string{"active"}},
+ &ldapserver.EntryAttribute{"uid", []string{"ned"}},
+ &ldapserver.EntryAttribute{"description", []string{"ned"}},
+ &ldapserver.EntryAttribute{"objectClass", []string{"posixAccount"}},
+ }},
+ &ldapserver.Entry{"cn=trent," + searchReq.BaseDN, []*ldapserver.EntryAttribute{
+ &ldapserver.EntryAttribute{"cn", []string{"trent"}},
+ &ldapserver.EntryAttribute{"uidNumber", []string{"5005"}},
+ &ldapserver.EntryAttribute{"accountStatus", []string{"active"}},
+ &ldapserver.EntryAttribute{"uid", []string{"trent"}},
+ &ldapserver.EntryAttribute{"description", []string{"trent"}},
+ &ldapserver.EntryAttribute{"objectClass", []string{"posixAccount"}},
+ }},
+ }
+ return ldapserver.ServerSearchResult{entries, []string{}, []ldapserver.Control{}, ldapserver.LDAPResultSuccess}, nil
+}