-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
234 lines (195 loc) · 6.95 KB
/
main.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"encoding/base64"
"fmt"
"log"
"strings"
ldap "gopkg.in/ldap.v2"
)
func main() {
demoMode("", "", "", "", "", false, false)
fmt.Println("all done")
}
const DC_SEPERATOR = ",dc="
type LdapDemo struct {
conn *ldap.Conn
gcConn *ldap.Conn
domain string
port int64
useSSL bool
verifySSLCert bool
searchBase string
binduser string
bindpassword string
rootNamingContext string
defaultNamingContext string
}
func NewLdapDemo(domain string, port int64, username string, password string, useSSL bool, verifySSLCert bool) *LdapDemo {
domainParts := strings.Split(domain, ".")
searchBase := fmt.Sprintf("dc=%s", strings.Join(domainParts, DC_SEPERATOR))
return &LdapDemo{
domain: domain,
port: port,
useSSL: useSSL,
verifySSLCert: verifySSLCert,
searchBase: searchBase,
binduser: username,
bindpassword: password,
}
}
func (demo *LdapDemo) ConnectToAD() error {
err := demo.ConnectToGC()
if err != nil {
fmt.Println("error 1")
// return err
}
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", demo.domain, demo.port))
if err != nil {
return err
}
demo.conn = l
demo.bindAsReadOnlyUser(false)
return nil
}
func (demo *LdapDemo) ConnectToGC() error {
log.Print("connecting to GC")
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", demo.domain, 3268))
if err != nil {
return err
}
demo.gcConn = l
demo.bindAsReadOnlyUser(true)
search := ldap.NewSearchRequest("", ldap.ScopeBaseObject, ldap.NeverDerefAliases, 0, 0, false, "(objectClass=domain)", nil, nil)
response, err := l.Search(search)
if err != nil {
fmt.Println(err)
}
//response.PrettyPrint(1)
demo.rootNamingContext = response.Entries[0].GetAttributeValue("rootDomainNamingContext")
demo.defaultNamingContext = response.Entries[0].GetAttributeValue("defaultNamingContext")
return nil
}
func (demo *LdapDemo) Disconnect() {
if demo.conn != nil {
demo.conn.Close()
}
if demo.gcConn != nil {
demo.gcConn.Close()
}
}
func (demo *LdapDemo) getDomainList() ([]*ldap.Entry, error) {
domainAttributes := []string{"dnsRoot", "objectGUID", "nCName"}
search := ldap.NewSearchRequest(fmt.Sprintf("CN=Partitions,CN=Configuration,%s", demo.rootNamingContext), ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, "(netbiosname=*)", domainAttributes, nil)
response, err := demo.conn.Search(search)
if err != nil {
fmt.Printf("%#v", err)
return nil, err
}
return response.Entries, nil
}
func (demo *LdapDemo) searchForUsers(searchPath string, searchTerm string) ([]*ldap.Entry, error) {
formattedSearchQuery := fmt.Sprintf("(&(objectCategory=user)(|(sn=%s)(name=%s)(displayName=%s)(sAMAccountName=%s)(userPrincipalName=%s)))", searchTerm, searchTerm, searchTerm, searchTerm, searchTerm)
var searchResults *ldap.SearchResult
var err error
log.Printf("the search path is %s", searchPath)
// if searchPath != demo.defaultNamingContext {
// log.Println("connecting to GC")
search := ldap.NewSearchRequest(searchPath, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, formattedSearchQuery, []string{"cn", "givenName", "sn", "mail", "dn", "sAMAccountName", "userPrincipalName", "userAccountControl", "memberOf", "objectGUID"}, nil)
searchResults, err = demo.gcConn.Search(search)
// } else {
// search := ldap.NewSearchRequest(searchPath, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, formattedSearchQuery, []string{"cn", "givenName", "sn", "mail", "dn", "sAMAccountName", "userPrincipalName", "userAccountControl", "memberOf", "objectGUID"}, nil)
// searchResults, err = demo.conn.Search(search)
// }
if err != nil {
fmt.Print(err)
return nil, err
}
for _, entry := range searchResults.Entries {
entry.PrettyPrint(2)
fmt.Println("--------------------------------------")
}
return searchResults.Entries, nil
}
func (demo *LdapDemo) getUsersGroups(entry *ldap.Entry) error {
return nil
}
func (demo *LdapDemo) searchForGroups(searchPath string, searchTerm string) ([]*ldap.Entry, error) {
formattedSearchQuery := fmt.Sprintf("(&(objectClass=group)(groupType:1.2.840.113556.1.4.803:=2147483648)(|(cn=%[1]s)(name=%[1]s)(sAMAccountName=%[1]s)))", searchTerm)
foo := ldap.NewControlPaging(10)
search := ldap.NewSearchRequest(searchPath, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, formattedSearchQuery, []string{"cn", "dn", "objectGUID"}, []ldap.Control{foo})
searchResults, err := demo.gcConn.Search(search)
if err != nil {
return nil, err
}
pagingControl := ldap.FindControl(searchResults.Controls, ldap.ControlTypePaging)
cookie := pagingControl.(*ldap.ControlPaging).Cookie
if len(cookie) != 0 {
cookieStr := base64.StdEncoding.EncodeToString(cookie)
fmt.Printf("%#v", cookieStr)
}
// for _, entry := range searchResults.Entries {
// // entry.PrettyPrint(2)
// // fmt.Println("--------------------------------------")
// }
return searchResults.Entries, nil
}
func (demo *LdapDemo) authenticateUser(searchBase, username, password string) error {
if searchBase == "" {
searchBase = demo.searchBase
}
searchRequest := ldap.NewSearchRequest(searchBase, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, fmt.Sprintf("(sAMAccountName=%s)", username), []string{"dn"}, nil)
sr, err := demo.conn.Search(searchRequest)
if err != nil {
return err
}
if len(sr.Entries) != 1 {
return fmt.Errorf("User %s does not exist or too many entries returned", username)
}
userdn := sr.Entries[0].DN //GetAttributeValue("userPrincipalName")
// Bind as the user to verify their password
err = demo.conn.Bind(userdn, demo.bindpassword)
defer demo.bindAsReadOnlyUser(false)
if err != nil {
return err
}
return nil
}
func (demo *LdapDemo) bindAsReadOnlyUser(useGC bool) error {
// Rebind as the read only user for any futher queries
if !useGC {
if demo.conn != nil {
return demo.conn.Bind("", demo.bindpassword)
}
return fmt.Errorf("GFY")
}
if demo.gcConn != nil {
return demo.gcConn.Bind("", demo.bindpassword)
}
return fmt.Errorf("GFY")
}
func demoMode(domain string, username string, password string, testuser string, othertestuser string, useSSL bool, verifySSLCert bool) {
ldapdemo := NewLdapDemo(domain, 389, username, password, false, verifySSLCert)
log.Printf("dialing to ldap %s", domain)
err := ldapdemo.ConnectToAD()
if err != nil {
log.Fatal(err)
}
defer ldapdemo.Disconnect()
// log.Print("Get domain list")
// domains, err := ldapdemo.getDomainList()
// if err != nil {
// return
// }
// for _, domain := range domains {
// fmt.Printf("--------search for users on %s---------\n", domain.GetAttributeValue("dnsRoot"))
// ldapdemo.searchForGroups(domain.GetAttributeValue("nCName"), ("*api*"))
// }
log.Println("----------")
log.Println("----------")
log.Println("----------")
log.Println("Get with blank search path")
log.Println("----------")
log.Println("----------")
log.Println("----------")
ldapdemo.searchForGroups("", ("TestGroup*"))
}