/
ldap.go
62 lines (52 loc) · 1.33 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
package checks
import (
"fmt"
"strings"
ldap "github.com/go-ldap/ldap/v3"
)
type Ldap struct {
checkBase
Domain string
Encrypted bool
}
func (c Ldap) Run(teamID uint, boxIp string, res chan Result) {
// Set timeout
ldap.DefaultTimeout = GlobalTimeout
username, password := getCreds(teamID, c.CredLists, c.Name)
scheme := "ldap"
if c.Encrypted {
scheme = "ldaps"
}
lconn, err := ldap.DialURL(fmt.Sprintf("%s://%s:%d", scheme, boxIp, c.Port))
if err != nil {
res <- Result{
Error: "failed to connect",
Debug: "login " + username + " password " + password + " failed with error: " + err.Error(),
}
return
}
defer lconn.Close()
// Set message timeout
lconn.SetTimeout(GlobalTimeout)
// Attempt to login
splitDomain := strings.Split(c.Domain, ".")
if len(splitDomain) != 2 {
res <- Result{
Error: "Configured domain is not valid (needs to be domain and tld)",
}
return
}
authString := fmt.Sprintf("%s@%s", username, c.Domain)
err = lconn.Bind(authString, password)
if err != nil {
res <- Result{
Error: "login failed for " + username,
Debug: "auth string " + authString + ", login " + username + " password " + password + " failed with error: " + err.Error(),
}
return
}
res <- Result{
Status: true,
Debug: "login successful for username " + username + " password " + password,
}
}