forked from threatstack/authkeys
-
Notifications
You must be signed in to change notification settings - Fork 1
/
authkeys.go
252 lines (231 loc) · 6.87 KB
/
authkeys.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// authkeys - lookup a user's SSH keys as stored in LDAP
// authkeys.go: the whole thing.
//
// Copyright 2017 Threat Stack, Inc.
// Licensed under the BSD 3-clause license; see LICENSE for more information.
// Author: Patrick T. Cable II <pat.cable@threatstack.com>
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"time"
"gopkg.in/ldap.v2"
)
type AuthkeysConfig struct {
BaseDN string
GroupObject string
DialTimeout int
KeyAttribute string
LDAPServer string
LDAPPort int
RootCAFile string
UserAttribute string
UserPostfix string
BindDN string
BindPW string
}
type User struct {
Uid string `json:"id"`
UidNumber string `json:"uid"`
GidNumber string `json:"gid"`
MemberOf []string `json:"groups"`
HomeDirectory string `json:"home"`
Shell string `json:"shell"`
}
func NewConfig(fname string) AuthkeysConfig {
data, err := ioutil.ReadFile(fname)
if err != nil {
panic(err)
}
config := AuthkeysConfig{}
err = json.Unmarshal(data, &config)
if err != nil {
panic(err)
}
return config
}
func main() {
var config AuthkeysConfig
var configfile string
var attributes []string
// Get configuration
if os.Getenv("AUTHKEYS_CONFIG") == "" {
configfile = "/etc/authkeys.json"
} else {
configfile = os.Getenv("AUTHKEYS_CONFIG")
}
if _, err := os.Stat(configfile); err == nil {
config = NewConfig(configfile)
}
groupPtr := flag.String("group", "", "List members of this LDAP group")
minPtr := flag.String("min", "", "Use minimal attributes. (For LDAP that does not support memberOf)")
flag.Parse()
listUsers := false
username := ""
if *groupPtr != "" {
listUsers = true
} else if len(os.Args) != 2 {
log.Fatalf("Not enough parameters specified (or too many): just need LDAP username.")
} else {
username = os.Args[1]
username += config.UserPostfix
}
if *minPtr != "" {
attributes = []string{"uid", "uidNumber", "gidNumber", "homeDirectory", "loginShell"}
} else {
attributes = []string{"uid", "uidNumber", "gidNumber", "memberOf", "homeDirectory", "loginShell"}
}
// Begin initial LDAP TCP connection. The LDAP library does have a Dial
// function that does most of what we need -- but its default timeout is 60
// seconds, which can be annoying if we're testing something in, say, Vagrant
var conntimeout time.Duration
if config.DialTimeout != 0 {
conntimeout = time.Duration(config.DialTimeout) * time.Second
} else {
conntimeout = time.Duration(5) * time.Second
}
server, err := net.DialTimeout("tcp",
fmt.Sprintf("%s:%d", config.LDAPServer, config.LDAPPort),
conntimeout)
if err != nil {
log.Fatal(err)
}
l := ldap.NewConn(server, false)
l.Start()
defer l.Close()
// Need a place to store TLS configuration
tlsConfig := &tls.Config{
InsecureSkipVerify: false,
ServerName: config.LDAPServer,
}
// Configure additional trust roots if necessary
if config.RootCAFile != "" {
rootCerts := x509.NewCertPool()
rootCAFile, err := ioutil.ReadFile(config.RootCAFile)
if err != nil {
log.Fatalf("Unable to read RootCAFile: %s", err)
}
if !rootCerts.AppendCertsFromPEM(rootCAFile) {
log.Fatalf("Unable to append to CertPool from RootCAFile")
}
tlsConfig.RootCAs = rootCerts
}
// TLS our connection up
err = l.StartTLS(tlsConfig)
if err != nil {
log.Fatalf("Unable to start TLS connection: %s", err)
}
// If we have a BindDN go ahead and bind before searching
if config.BindDN != "" && config.BindPW != "" {
err = l.Bind(config.BindDN, config.BindPW)
if err != nil {
log.Fatalf("Unable to bind: %s", err)
}
}
var searchRequest *ldap.SearchRequest
if listUsers {
searchRequest = ldap.NewSearchRequest(
config.BaseDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(&(objectClass=inetOrgPerson)(memberOf=cn=%s,ou=%s,%s))", *groupPtr, config.GroupObject, config.BaseDN),
attributes, // attributes to retrieve
nil,
)
} else {
// Set up an LDAP search and actually do the search
searchRequest = ldap.NewSearchRequest(
config.BaseDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(%s=%s)", config.UserAttribute, username),
[]string{config.KeyAttribute},
nil,
)
}
sr, err := l.Search(searchRequest)
if err != nil {
log.Fatal(err)
}
if len(sr.Entries) == 0 {
log.Fatalf("No entries returned from LDAP")
} else if !listUsers && (len(sr.Entries) > 1) {
log.Fatalf("Too many entries returned from LDAP")
}
var attribute string
cn := "cn="
if listUsers {
var Users []User
for _, entry := range sr.Entries {
rawMemberOf := entry.GetAttributeValues("memberOf")
// If it is a minimal ldap integration search for memberOf for each user.
if *minPtr != "" {
userSearchRequest := ldap.NewSearchRequest(
config.BaseDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(%s=%s)", config.UserAttribute, entry.GetAttributeValues(config.UserAttribute)[0]),
[]string{"memberOf"},
nil,
)
userSr, err := l.Search(userSearchRequest)
if err != nil {
log.Fatal(err)
}
for _, userEntry := range userSr.Entries {
rawMemberOf = userEntry.GetAttributeValues("memberOf")
}
}
var memberOf []string
var username string
for group := range rawMemberOf {
cnLoc := strings.Index(rawMemberOf[group], cn)
termLoc := strings.Index(rawMemberOf[group], ",")
memberOf = append(memberOf, rawMemberOf[group][cnLoc+len(cn):termLoc])
}
// Some Idp do not support memberOf from a group listing so lets iterate over the user
if len(memberOf) == 0 {
memberOf = append(memberOf, *groupPtr)
}
// If the uid returns an email only use the prefix.
if strings.Contains(string(entry.GetAttributeValue("uid")), "@") {
email := string(entry.GetAttributeValue("uid"))
components := strings.Split(email, "@")
username = components[0]
} else {
username = string(entry.GetAttributeValue("uid"))
}
homeDir := string(entry.GetAttributeValue("homeDirectory"))
loginShell := string(entry.GetAttributeValue("loginShell"))
Users = append(Users, User{
Uid: username,
UidNumber: string(entry.GetAttributeValue("uidNumber")),
GidNumber: string(entry.GetAttributeValue("gidNumber")),
MemberOf: memberOf,
HomeDirectory: homeDir,
Shell: loginShell,
})
}
myUsers, err := json.Marshal(Users)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", myUsers)
} else {
attribute = config.KeyAttribute
for _, entry := range sr.Entries {
keys := entry.GetAttributeValues(attribute)
for _, key := range keys {
fmt.Printf("%s\n", key)
}
}
}
// Get the keys & print 'em. This will only print keys for the first user
// returned from LDAP, but if you have multiple users with the same name maybe
// setting a different BaseDN may be useful.
}