-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathldap.go
162 lines (137 loc) · 3.22 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
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
package main
import (
"errors"
"fmt"
"net"
"strings"
"crypto/tls"
"crypto/x509"
"gopkg.in/ldap.v2"
)
type ldapEnv struct {
host string
port int
base string
filter string
tls bool
skip bool
debug bool
uid string
binddn string
bindpw string
}
func (l *ldapEnv) connect() (*ldap.Conn, error) {
host, err := l.getHost()
if err != nil {
logging(err)
return nil, err
}
return ldap.Dial("tcp", fmt.Sprintf("%s:%d", host, l.port))
}
func (l *ldapEnv) connectTLS() (*ldap.Conn, error) {
host, err := l.getHost()
if err != nil {
logging(err)
return nil, err
}
certs, err := x509.SystemCertPool()
if err != nil {
logging(err)
return nil, err
}
tlsConfig := &tls.Config{
RootCAs: certs,
}
if !isAddr(host) {
tlsConfig.ServerName = host
}
if isAddr(host) || l.skip {
tlsConfig.InsecureSkipVerify = true
}
return ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", host, l.port), tlsConfig)
}
func simpleBind(c *ldap.Conn, l *ldapEnv) error {
bindRequest := ldap.NewSimpleBindRequest(l.binddn, l.bindpw, nil)
_, err := c.SimpleBind(bindRequest)
return err
}
func (l *ldapEnv) search(c *ldap.Conn) ([]*ldap.Entry, error) {
searchRequest := ldap.NewSearchRequest(
l.base, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases,
0, 0, false,
fmt.Sprintf(l.filter, l.uid), []string{sshPublicKeyName}, nil)
sr, err := c.Search(searchRequest)
return sr.Entries, err
}
func printPubkey(entries []*ldap.Entry) error {
if len(entries) != 1 {
return errors.New("User does not exist or too many entries returned")
}
if len(entries[0].GetAttributeValues("sshPublicKey")) == 0 {
return errors.New("User does not use ldapPublicKey")
}
for _, pubkey := range entries[0].GetAttributeValues("sshPublicKey") {
fmt.Println(pubkey)
}
return nil
}
func isAddr(host string) bool {
ip := net.ParseIP(host)
return isIPv4(ip) || isIPv6(ip)
}
func (l *ldapEnv) validateIPv6LinkLocal() (string, error) {
addr := strings.Split(l.host, "%")
ip := net.ParseIP(addr[0])
if isIPv6(ip) && ip.IsLinkLocalUnicast() {
return fmt.Sprintf("[%s]", l.host), nil
}
return "", errors.New("Invalid IPv6 link-local address")
}
func isIPv6(ip net.IP) bool {
return ip.To4() == nil && ip.To16() != nil
}
func isIPv4(ip net.IP) bool {
return ip.To4() != nil && ip.To16() != nil
}
func (l *ldapEnv) validateIPv6() (string, error) {
if strings.Contains(l.host, "%") {
return l.validateIPv6LinkLocal()
}
ip := net.ParseIP(l.host)
// global scope
if isIPv6(ip) {
return fmt.Sprintf("[%s]", l.host), nil
}
return "", errors.New("Invalid IPv6 address")
}
func (l *ldapEnv) validateHost() (string, error) {
var host string
var err error
addrs, err := net.LookupHost(l.host)
if err == nil && len(addrs) > 0 {
host = l.host
} else {
host = ""
err = errors.New("Invalid hostname / FQDN")
}
return host, err
}
func (l *ldapEnv) getHost() (string, error) {
var host string
var err error
if strings.HasPrefix(l.host, "[") || strings.HasSuffix(l.host, "]") {
err = errors.New("Invalid host")
} else {
// IPv6
if strings.Contains(l.host, ":") {
host, err = l.validateIPv6()
} else if isIPv4(net.ParseIP(l.host)) {
// ipv4
host = l.host
} else {
// fqdn
host, err = l.validateHost()
}
}
return host, err
}