-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap.go
78 lines (65 loc) · 1.49 KB
/
ldap.go
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
// LDAP 検索
package main
import (
"fmt"
ldap "gopkg.in/ldap.v2"
)
// Params は ldapSearch のパラメータを指定するための構造体型
type Params struct {
Server string
BaseDN string
BindDN string
Password string
Pattern string
Attributes []string
}
// ldapSearch は LDAPサーバに接続し、検索する関数
func ldapSearch(p Params) (sr *ldap.SearchResult, err error) {
var con *ldap.Conn
// LDAPサーバへ接続
fmt.Printf("\nConnecting %s...\n", p.Server)
con, err = ldap.Dial("tcp", p.Server)
if err != nil {
return
}
fmt.Println("Connected.")
defer func() {
con.Close()
fmt.Println("Disconnected.")
}()
// LDAPサーバ認証(LDAP BIND)
err = con.Bind(p.BindDN, p.Password)
if err != nil {
return
}
fmt.Println("Logged in.")
/*
検索リクエスト作成
コンストラクタ関数の引数
func NewSearchRequest(
BaseDN string,
Scope int,
DerefAliases int,
SizeLimit int,
TimeLimit int,
TypesOnly bool,
Filter string,
Attributes []string,
Controls []Control,
) *SearchRequest
*/
searchRequest := ldap.NewSearchRequest(
p.BaseDN,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
0,
0,
false,
p.Pattern,
p.Attributes,
nil,
)
//検索リクエストを基にディレクトリ内検索
sr, err = con.Search(searchRequest)
return
}