/usr/share/gocode/src/github.com/mqu/openldap/_examples/test-openldap.go is in golang-openldap-dev 0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | package main
import (
"fmt"
"github.com/mqu/openldap"
)
/*
*
* openldap example program :
*
* - 1 :
*
* - specify URL for LDAP connexion with user and passwd
* - ldap and ldaps is supported,
* - anonymous connexion is done with an empty user string
* - base (DN) is needed for many LDAP server (it depends on LDAP data design)
*
* - 2 :
*
* - you can set some LDAP options.
* - authentification with Bind()
*
* - 3 : setup LDAP query search.
* - 4 : print search results.
*
*/
func main() {
var user, passwd, url, base string
// (1) - connexion options
url = "ldap://some.host:389/"
// url = "ldaps://some.host:636/"
user = "..."
passwd = "..."
base = ""
ldap, err := openldap.Initialize(url)
if err != nil {
fmt.Printf("LDAP::Initialize() : connexion error\n")
return
}
// (2.1) - options
ldap.SetOption(openldap.LDAP_OPT_PROTOCOL_VERSION, openldap.LDAP_VERSION3)
// (2.2) - authentification (Bind)
err = ldap.Bind(user, passwd)
if err != nil {
fmt.Printf("LDAP::Bind() : bind error\n")
fmt.Println(err)
return
}
defer ldap.Close()
// (3) : search method
// -------------------------------------- Ldap::SearchAll() --------------------------------------
scope := openldap.LDAP_SCOPE_SUBTREE // LDAP_SCOPE_BASE, LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
filter := "cn=*admin*"
attributes := []string{"cn", "sn", "givenname", "mail"} // leave empty for all attributes
// SearchAll(base string, scope int, filter string, attributes []string) (*LdapSearchResult, error)
result, err := ldap.SearchAll(base, scope, filter, attributes)
if err != nil {
fmt.Println(err)
return
}
// (4) - print LdapSearchResult(s)
fmt.Printf("# num results : %d\n", result.Count())
fmt.Printf("# search : %s\n", result.Filter())
fmt.Printf("# base : %s\n", result.Base())
fmt.Printf("# attributes : [%s]\n", strings.Join(result.Attributes(), ", "))
for _, entry := range result.Entries() {
fmt.Printf("dn=%s\n", entry.Dn())
for _, attr := range entry.Attributes() {
fmt.Printf("%s=[%s]\n", attr.Name(), strings.Join(attr.Values(), ", "))
}
fmt.Printf("\n")
}
}
|