/
service.go
227 lines (188 loc) · 6.02 KB
/
service.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
package ldap
import (
"fmt"
"sync"
"github.com/cyverse/ldap-irods-auth/commons"
ldap_message "github.com/lor00x/goldap/message"
log "github.com/sirupsen/logrus"
"github.com/vjeantet/ldapserver"
)
// LDAPService is a service object
type LDAPService struct {
config *commons.Config
ldapServer *ldapserver.Server
irodsAuth *IRODSAuth
terminate bool
mutex sync.Mutex
}
// NewLDAP creates a new LDAP service
func NewLDAP(config *commons.Config) (*LDAPService, error) {
ldapserver.Logger = log.WithFields(log.Fields{
"package": "ldap",
"module": "github.com/vjeantet/ldapserver",
})
server := ldapserver.NewServer()
routes := ldapserver.NewRouteMux()
irodsAuth, err := NewIRODSAuth(config)
if err != nil {
return nil, err
}
svc := &LDAPService{
config: config,
ldapServer: server,
irodsAuth: irodsAuth,
}
routes.NotFound(svc.handleNotFound)
routes.Abandon(svc.handleAbandon)
routes.Bind(svc.handleBind)
routes.Search(svc.handleSearch).Label("Search - Generic")
server.Handle(routes)
return svc, nil
}
func (svc *LDAPService) Start() error {
logger := log.WithFields(log.Fields{
"package": "ldap",
"struct": "LDAPService",
"function": "Start",
})
logger.Info("Starting the LDAP-iRODS-Auth service")
// start service
hostport := fmt.Sprintf("%s:%d", svc.config.ServiceHost, svc.config.ServicePort)
return svc.ldapServer.ListenAndServe(hostport)
}
// Destroy destroys the LDAP service
func (svc *LDAPService) Destroy() {
logger := log.WithFields(log.Fields{
"package": "ldap",
"struct": "LDAPService",
"function": "Destroy",
})
svc.mutex.Lock()
defer svc.mutex.Unlock()
if svc.terminate {
// already terminated
return
}
svc.terminate = true
logger.Info("Destroying the LDAP-iRODS-Auth service")
svc.ldapServer.Stop()
}
func (svc *LDAPService) handleNotFound(w ldapserver.ResponseWriter, m *ldapserver.Message) {
switch m.ProtocolOpType() {
case ldapserver.ApplicationBindRequest:
res := ldapserver.NewBindResponse(ldapserver.LDAPResultSuccess)
res.SetDiagnosticMessage("Default binding behavior set to return Success")
w.Write(res)
default:
res := ldapserver.NewResponse(ldapserver.LDAPResultUnwillingToPerform)
res.SetDiagnosticMessage("Operation not implemented by server")
w.Write(res)
}
}
func (svc *LDAPService) handleAbandon(w ldapserver.ResponseWriter, m *ldapserver.Message) {
var req = m.GetAbandonRequest()
// retreive the request to abandon, and send a abort signal to it
if requestToAbandon, ok := m.Client.GetMessageByID(int(req)); ok {
requestToAbandon.Abandon()
log.Printf("Abandon signal sent to request processor [messageID=%d]", int(req))
}
}
func (svc *LDAPService) handleBind(w ldapserver.ResponseWriter, m *ldapserver.Message) {
r := m.GetBindRequest()
if r.AuthenticationChoice() == "simple" {
dn := string(r.Name())
if dn == "" {
// anonymous access
log.Printf("Anonymous user bind")
w.Write(ldapserver.NewBindResponse(ldapserver.LDAPResultSuccess))
return
}
irodsPassword := string(r.AuthenticationSimple())
authSuccess, _ := svc.irodsAuth.Auth(dn, irodsPassword)
if authSuccess {
log.Printf("Bind User=%s", dn)
w.Write(ldapserver.NewBindResponse(ldapserver.LDAPResultSuccess))
return
}
log.Printf("Bind failed User=%s", dn)
failRes := ldapserver.NewBindResponse(ldapserver.LDAPResultInvalidCredentials)
failRes.SetDiagnosticMessage("invalid credentials")
w.Write(failRes)
return
}
log.Printf("Unhandled bind authentication choice %s", r.AuthenticationChoice())
failRes := ldapserver.NewBindResponse(ldapserver.LDAPResultUnwillingToPerform)
failRes.SetDiagnosticMessage("authentication choice not supported")
w.Write(failRes)
}
func (svc *LDAPService) handleSearch(w ldapserver.ResponseWriter, m *ldapserver.Message) {
r := m.GetSearchRequest()
log.Printf("Request BaseDn=%s", r.BaseObject())
log.Printf("Request Filter=%s", r.Filter())
log.Printf("Request FilterString=%s", r.FilterString())
log.Printf("Request Attributes=%s", r.Attributes())
log.Printf("Request TimeLimit=%d", r.TimeLimit().Int())
// Handle Stop Signal (server stop / client disconnected / Abandoned request....)
select {
case <-m.Done:
log.Print("Leaving handleSearch...")
return
default:
}
// to quick search
attributes := map[string]string{}
for _, att := range r.Attributes() {
attributes[string(att)] = string(att)
}
// included all id
usersAdded := map[string]string{}
dns := svc.irodsAuth.GetDNs()
for _, dn := range dns {
username := GetUsernameFromDN(dn)
filter := r.FilterString()
if CheckDNFilter(filter, dn) {
log.Printf("Returning search result - %s", dn)
e := ldapserver.NewSearchResultEntry(dn)
if len(attributes) == 0 {
// display all
e.AddAttribute("mail", ldap_message.AttributeValue(username+"@cyverse.org"))
e.AddAttribute("cn", ldap_message.AttributeValue(username))
} else {
if _, ok := attributes["uid"]; ok {
e.AddAttribute("uid", ldap_message.AttributeValue(username))
}
if _, ok := attributes["cn"]; ok {
e.AddAttribute("cn", ldap_message.AttributeValue(username))
}
}
w.Write(e)
usersAdded[username] = dn
}
}
// include asked id
askedUser := ExtractFilterValue(r.FilterString(), "uid")
log.Printf("asked user := %s", askedUser)
if _, ok := usersAdded[askedUser]; !ok {
// not added
// make a new dn
log.Printf("Adding an asked user %s to search result", askedUser)
dn := fmt.Sprintf("uid=%s,ou=People,%s", askedUser, r.BaseObject())
e := ldapserver.NewSearchResultEntry(dn)
if len(attributes) == 0 {
// display all
e.AddAttribute("mail", ldap_message.AttributeValue(askedUser+"@cyverse.org"))
e.AddAttribute("cn", ldap_message.AttributeValue(askedUser))
} else {
if _, ok := attributes["uid"]; ok {
e.AddAttribute("uid", ldap_message.AttributeValue(askedUser))
}
if _, ok := attributes["cn"]; ok {
e.AddAttribute("cn", ldap_message.AttributeValue(askedUser))
}
}
w.Write(e)
usersAdded[askedUser] = dn
}
res := ldapserver.NewSearchResultDoneResponse(ldapserver.LDAPResultSuccess)
w.Write(res)
}