/usr/share/gocode/src/github.com/mqu/openldap/_examples/ldapsearch.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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | package main
/*
* Author : Marc Quinton / 2012.
*
* ldapsearch command mimics openldap/seach command. Supported options :
* - host : ldap[s]://hostname:port/ format,
* - user,
* - password,
* - base
*
* arguments : filter [attributes]
* - filter is an LDAP filter (ex: objectClass=*, cn=*admin*", ...
* - attributes is an LDAP attribute list ; can be empty. ex: cn, sn, givenName, mail, ...
*
*/
import (
"fmt"
"os"
"errors"
"flag"
"github.com/mqu/openldap"
)
type LdapSearchOptions struct {
host string
user string
passwd string
base string
filter string
attributes []string
scope int
}
type LdapSearchApp struct {
ldap *openldap.Ldap
opts *LdapSearchOptions
}
func NewLdapSearchApp() *LdapSearchApp{
app := new(LdapSearchApp)
return app
}
// Show ldapsearch app usage
func (self *LdapSearchApp) Usage(){
fmt.Printf("usage: %s filter [attribute list]\n", os.Args[0])
flag.PrintDefaults()
}
// Parse ldapsearch app options using flag package.
func (self *LdapSearchApp) ParseOpts() (*LdapSearchOptions, error){
var opts LdapSearchOptions
flag.StringVar(&opts.host, "host", "ldap://localhost:389/", "ldap server URL format : ldap[s]://hostname:port/")
flag.StringVar(&opts.user, "user", "" , "user for authentification")
flag.StringVar(&opts.passwd, "passwd", "" , "password for authentification")
flag.StringVar(&opts.base, "base", "" , "base DN for search")
flag.Parse()
if flag.NArg() == 0 {
self.Usage()
return nil, errors.New(fmt.Sprintf("ParseOpts() error ; see usage for more information"))
}
opts.filter = flag.Arg(0)
if len(flag.Args()) == 1 {
opts.attributes = []string{}
} else {
opts.attributes = flag.Args()[1:]
}
return &opts, nil
}
// Connect and Bind to LDAP server using self.opts
func (self *LdapSearchApp) Connect() (error){
var err error
self.ldap, err = openldap.Initialize(self.opts.host)
if err != nil {
return err
}
//FIXME: should be an external option
self.ldap.SetOption(openldap.LDAP_OPT_PROTOCOL_VERSION, openldap.LDAP_VERSION3)
err = self.ldap.Bind(self.opts.user, self.opts.passwd)
if err != nil {
return err
}
return nil
}
// Close() disconnect application from Ldap server
func (self *LdapSearchApp) Close() (error){
return self.ldap.Close()
}
// Search using filter and returning attributes list
func (self *LdapSearchApp) Search() (*openldap.LdapSearchResult, error){
//FIXME: should be an external option
scope := openldap.LDAP_SCOPE_SUBTREE
return self.ldap.SearchAll(
self.opts.base,
scope,
self.opts.filter,
self.opts.attributes)
}
// Print search result
func (self *LdapSearchApp) Print(res *openldap.LdapSearchResult) (error){
fmt.Println(res)
return nil
}
func main() {
var err error
app := NewLdapSearchApp()
app.opts, err = app.ParseOpts()
if err != nil {
fmt.Println(err)
return
}
err = app.Connect()
if err != nil {
fmt.Println(err)
return
}
result, err := app.Search()
if(err != nil) {
fmt.Println("search error:", err)
return
}
app.Print(result)
app.Close()
}
|