forked from ubccr/goipa
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipa_ldap.go
130 lines (99 loc) · 2.6 KB
/
ipa_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
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
package ipa
import (
"gopkg.in/ldap.v2"
"fmt"
"crypto/tls"
"errors"
)
type LdapClient struct {
BaseDN string
Connection *ldap.Conn
}
func LdapConnect(host string, baseDn string, username string, password string) (*LdapClient, error) {
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", host, 389))
if err != nil {
return nil, err
}
// TODO: Fix needing InsecureSkipVerify
config := &tls.Config{ServerName: host, RootCAs: ipaCertPool, InsecureSkipVerify: true}
config.BuildNameToCertificate()
// Reconnect with TLS
err = l.StartTLS(config)
if err != nil {
l.Close()
return nil, err
}
err = l.Bind(fmt.Sprintf("uid=%s,cn=users,cn=accounts,%s", username, baseDn), password)
if err != nil {
l.Close()
return nil, err
}
return &LdapClient{BaseDN: baseDn, Connection: l}, nil
}
func (c *LdapClient) Close() {
c.Connection.Close()
}
func (c *LdapClient) Search(childDn string, filter string, attributes []string) (*ldap.SearchResult, error) {
dn := c.BaseDN
if len(childDn) > 0 {
dn = fmt.Sprintf("%s,%s", childDn, c.BaseDN)
}
searchRequest := ldap.NewSearchRequest(
dn,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
filter,
attributes,
nil,
)
return c.Connection.Search(searchRequest)
}
func (c *LdapClient) GetUserForUUID(uuid string) (*string, error) {
sr, err := c.Search("cn=users,cn=accounts",
fmt.Sprintf("(ipaUniqueID=%s)", uuid),
[]string{"uid"})
if err != nil {
return nil, err
}
if len(sr.Entries) != 1 {
return nil, errors.New("too many entries returned")
}
uid := sr.Entries[0].GetAttributeValue("uid")
return &uid, nil
}
func (c *LdapClient) UserExistsForUUID(uuid string) (bool, error) {
sr, err := c.Search("cn=users,cn=accounts",
fmt.Sprintf("(ipaUniqueID=%s)", uuid),
[]string{})
if err != nil {
return false, err
}
if len(sr.Entries) > 1 {
return false, errors.New("too many entries returned")
}
return len(sr.Entries) == 1, nil
}
func (c *LdapClient) GetGroupForUUID(uuid string) (*string, error) {
sr, err := c.Search("cn=groups,cn=accounts",
fmt.Sprintf("(ipaUniqueID=%s)", uuid),
[]string{"cn"})
if err != nil {
return nil, err
}
if len(sr.Entries) != 1 {
return nil, errors.New("too many entries returned")
}
gid := sr.Entries[0].GetAttributeValue("cn")
return &gid, nil
}
func (c *LdapClient) GroupExistsForUUID(uuid string) (bool, error) {
sr, err := c.Search("cn=groups,cn=accounts",
fmt.Sprintf("(ipaUniqueID=%s)", uuid),
[]string{})
if err != nil {
return false, err
}
if len(sr.Entries) > 1 {
return false, errors.New("too many entries returned")
}
return len(sr.Entries) == 1, nil
}