searchTLS.go 968 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "log"
  8. "github.com/gogits/gogs/modules/ldap"
  9. )
  10. var (
  11. LdapServer string = "localhost"
  12. LdapPort uint16 = 389
  13. BaseDN string = "dc=enterprise,dc=org"
  14. Filter string = "(cn=kirkj)"
  15. Attributes []string = []string{"mail"}
  16. )
  17. func main() {
  18. l, err := ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", LdapServer, LdapPort), nil)
  19. if err != nil {
  20. log.Fatalf("ERROR: %s\n", err.Error())
  21. }
  22. defer l.Close()
  23. // l.Debug = true
  24. search := ldap.NewSearchRequest(
  25. BaseDN,
  26. ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
  27. Filter,
  28. Attributes,
  29. nil)
  30. sr, err := l.Search(search)
  31. if err != nil {
  32. log.Fatalf("ERROR: %s\n", err.Error())
  33. return
  34. }
  35. log.Printf("Search: %s -> num of entries = %d\n", search.Filter, len(sr.Entries))
  36. sr.PrettyPrint(0)
  37. }