-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathssh-ldap-ad-wrapper.go
142 lines (115 loc) · 4.35 KB
/
ssh-ldap-ad-wrapper.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
package main
import (
"os"
"log"
"fmt"
"gopkg.in/ldap.v2"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"bufio"
"strings"
)
var ldap_config_file = "/etc/nslcd.conf"
var pubkey_property = "altSecurityIdentities"
func main() {
if len(os.Args) > 1 {
var l *ldap.Conn
var conf *tls.Config
var uname = os.Args[1]
var user string
var pass string
var base_search string
var server_rootca string
var uris [2]string
var uris_cnt int64
file, err := os.Open(ldap_config_file)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if (!strings.HasPrefix(scanner.Text(),"#")) {
if (strings.HasPrefix(scanner.Text(), "uri")) {
s := strings.Split(scanner.Text(), " ")
ss := strings.Split(s[1],"//")
uris[uris_cnt] = ss[1]
uris_cnt++
continue
}
if (strings.HasPrefix(scanner.Text(), "base")) {
s := strings.Split(scanner.Text(), " ")
base_search = s[1]
continue
}
if (strings.HasPrefix(scanner.Text(), "binddn")) {
s := strings.Split(scanner.Text(), " ")
user = s[1]
continue
}
if (strings.HasPrefix(scanner.Text(), "bindpw")) {
s := strings.Split(scanner.Text(), " ")
pass = s[1]
continue
}
if (strings.HasPrefix(scanner.Text(), "tls_cacertfile")) {
s := strings.Split(scanner.Text(), " ")
server_rootca = s[1]
continue
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
//Load CA Cert
cert, err := ioutil.ReadFile(server_rootca)
if err != nil {
log.Fatalf("Couldn't load file", err)
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(cert)
// Connect
for _, uri := range uris {
ss := strings.Split(uri,":")
conf = &tls.Config{
RootCAs: certPool,
InsecureSkipVerify: false,
ServerName: ss[0],
}
l, err = ldap.Dial("tcp", uri)
if err == nil {
break
}
}
if err != nil {
log.Fatal(err)
}
defer l.Close()
err = l.StartTLS(conf)
if err != nil {
log.Fatal(err)
}
err = l.Bind(user, pass)
if err != nil {
log.Fatal(err)
}
// Search
searchRequest := ldap.NewSearchRequest(
base_search,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(sAMAccountName=" + uname + ")",
[]string{pubkey_property},
nil,
)
sr, err := l.Search(searchRequest)
if err != nil {
log.Fatal(err)
}
// Display
for _, entry := range sr.Entries {
fmt.Printf("%v\n", entry.GetAttributeValue(pubkey_property))
}
}
}