-
Notifications
You must be signed in to change notification settings - Fork 35
Examples
Code overview :
package main
// imports
import (
"fmt"
"github.com/mqu/openldap"
)
// initialize
ldap, err := openldap.Initialize(url)
// Options
ldap.SetOption(openldap.LDAP_OPT_PROTOCOL_VERSION, openldap.LDAP_VERSION3)
// authentification
err = ldap.Bind(user, passwd)
// searching
result, err := ldap.**SearchAll**(base, scope, filter, attributes)
// Closing connexion
err = ldap.Close()
Here is an example using opeldap library.
connecting to LDAP server :
-
url is server address in format : ldap//host:port/, or ldaps//host:port/
-
user and passwd are need for authentification, empty user and passwd allow anonymous access.
var user, passwd, url, base string
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 }
setup some options
ldap.SetOption(openldap.LDAP_OPT_PROTOCOL_VERSION, openldap.LDAP_VERSION3)
LDAP authentification with Bind() ; allways close connexion when finished.
err = ldap.Bind(user, passwd)
if err != nil {
fmt.Printf("LDAP::Bind() : bind error\n")
fmt.Println(err)
return
}
defer ldap.Close()
Searching LDAP server. SearchAll() is the easiest way to search and read data from LDAP server.
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
result, err := ldap.**SearchAll**(base, scope, filter, attributes)
if err != nil {
fmt.Println(err)
return
}
Printing search results - lazy way :
fmt.Printf(result)
Printing search results - easy way (same result):
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")
}