2020-01-26 21:08:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-02-02 13:32:37 +00:00
|
|
|
ldap "bottin/ldapserver"
|
|
|
|
|
2020-02-02 13:47:16 +00:00
|
|
|
message "github.com/lor00x/goldap/message"
|
2020-01-26 21:08:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Compare request -------------------------
|
|
|
|
|
|
|
|
func (server *Server) handleCompare(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {
|
|
|
|
state := s.(*State)
|
|
|
|
r := m.GetCompareRequest()
|
|
|
|
|
|
|
|
code, err := server.handleCompareInternal(state, &r)
|
|
|
|
|
|
|
|
res := ldap.NewResponse(code)
|
|
|
|
if err != nil {
|
|
|
|
res.SetDiagnosticMessage(err.Error())
|
|
|
|
}
|
|
|
|
w.Write(message.CompareResponse(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) handleCompareInternal(state *State, r *message.CompareRequest) (int, error) {
|
|
|
|
attr := string(r.Ava().AttributeDesc())
|
|
|
|
expected := string(r.Ava().AssertionValue())
|
|
|
|
|
2020-02-02 12:51:47 +00:00
|
|
|
dn, err := server.checkDN(string(r.Entry()), false)
|
2020-01-26 21:08:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return ldap.LDAPResultInvalidDNSyntax, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check permissions
|
|
|
|
if !server.config.Acl.Check(&state.login, dn, "read", []string{attr}) {
|
|
|
|
return ldap.LDAPResultInsufficientAccessRights, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do query
|
|
|
|
exists, err := server.objectExists(dn)
|
|
|
|
if err != nil {
|
|
|
|
return ldap.LDAPResultOperationsError, err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return ldap.LDAPResultNoSuchObject, fmt.Errorf("Not found: %s", dn)
|
|
|
|
}
|
|
|
|
|
|
|
|
values, err := server.getAttribute(dn, attr)
|
|
|
|
if err != nil {
|
|
|
|
return ldap.LDAPResultOperationsError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range values {
|
2020-02-13 12:53:21 +00:00
|
|
|
if valueMatch(attr, v, expected) {
|
2020-01-26 21:08:27 +00:00
|
|
|
return ldap.LDAPResultCompareTrue, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ldap.LDAPResultCompareFalse, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search request -------------------------
|
|
|
|
|
|
|
|
func (server *Server) handleSearch(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {
|
|
|
|
state := s.(*State)
|
|
|
|
r := m.GetSearchRequest()
|
|
|
|
|
|
|
|
code, err := server.handleSearchInternal(state, w, &r)
|
|
|
|
|
|
|
|
res := ldap.NewResponse(code)
|
|
|
|
if err != nil {
|
|
|
|
res.SetDiagnosticMessage(err.Error())
|
|
|
|
}
|
2020-01-27 23:52:30 +00:00
|
|
|
if code != ldap.LDAPResultSuccess {
|
|
|
|
server.logger.Printf("Failed to do search %#v (%s)", r, err)
|
|
|
|
}
|
2020-01-26 21:08:27 +00:00
|
|
|
w.Write(message.SearchResultDone(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) handleSearchInternal(state *State, w ldap.ResponseWriter, r *message.SearchRequest) (int, error) {
|
2020-02-01 14:05:44 +00:00
|
|
|
|
2020-02-02 12:51:47 +00:00
|
|
|
baseObject, err := server.checkDN(string(r.BaseObject()), true)
|
|
|
|
if err != nil {
|
|
|
|
return ldap.LDAPResultInvalidDNSyntax, err
|
|
|
|
}
|
|
|
|
|
2020-02-01 14:05:44 +00:00
|
|
|
server.logger.Tracef("-- SEARCH REQUEST: --")
|
2020-02-02 12:51:47 +00:00
|
|
|
server.logger.Tracef("Request BaseDn=%s", baseObject)
|
2020-02-01 14:05:44 +00:00
|
|
|
server.logger.Tracef("Request Filter=%s", r.Filter())
|
|
|
|
server.logger.Tracef("Request FilterString=%s", r.FilterString())
|
|
|
|
server.logger.Tracef("Request Attributes=%s", r.Attributes())
|
|
|
|
server.logger.Tracef("Request TimeLimit=%d", r.TimeLimit().Int())
|
2020-01-26 21:08:27 +00:00
|
|
|
|
2020-02-02 12:51:47 +00:00
|
|
|
if !server.config.Acl.Check(&state.login, "read", baseObject, []string{}) {
|
2020-01-26 21:08:27 +00:00
|
|
|
return ldap.LDAPResultInsufficientAccessRights, fmt.Errorf("Please specify a base object on which you have read rights")
|
|
|
|
}
|
|
|
|
|
2020-02-09 17:36:13 +00:00
|
|
|
baseObjectLevel := len(strings.Split(baseObject, ","))
|
|
|
|
|
2020-01-26 21:08:27 +00:00
|
|
|
basePath, err := dnToConsul(baseObject)
|
|
|
|
if err != nil {
|
|
|
|
return ldap.LDAPResultInvalidDNSyntax, err
|
|
|
|
}
|
2020-02-12 14:14:58 +00:00
|
|
|
if r.Scope() == message.SearchRequestScopeBaseObject {
|
|
|
|
basePath += "/attribute="
|
|
|
|
} else {
|
|
|
|
basePath += "/"
|
|
|
|
}
|
2020-01-26 21:08:27 +00:00
|
|
|
|
2020-02-12 14:14:58 +00:00
|
|
|
data, _, err := server.kv.List(basePath, nil)
|
2020-01-26 21:08:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return ldap.LDAPResultOperationsError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := parseConsulResult(data)
|
|
|
|
if err != nil {
|
|
|
|
return ldap.LDAPResultOperationsError, err
|
|
|
|
}
|
2020-02-01 14:05:44 +00:00
|
|
|
|
2020-02-12 14:14:58 +00:00
|
|
|
server.logger.Tracef("in %s: %#v", basePath, data)
|
2020-02-01 14:05:44 +00:00
|
|
|
server.logger.Tracef("%#v", entries)
|
2020-01-26 21:08:27 +00:00
|
|
|
|
|
|
|
for dn, entry := range entries {
|
2020-02-09 17:36:13 +00:00
|
|
|
if r.Scope() == message.SearchRequestScopeBaseObject {
|
|
|
|
if dn != baseObject {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if r.Scope() == message.SearchRequestSingleLevel {
|
|
|
|
objectLevel := len(strings.Split(dn, ","))
|
2020-02-11 22:20:33 +00:00
|
|
|
if objectLevel != baseObjectLevel+1 {
|
2020-02-09 17:36:13 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 21:08:27 +00:00
|
|
|
// Filter out if we don't match requested filter
|
|
|
|
matched, err := applyFilter(entry, r.Filter())
|
|
|
|
if err != nil {
|
|
|
|
return ldap.LDAPResultUnwillingToPerform, err
|
|
|
|
}
|
|
|
|
if !matched {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out if user is not allowed to read this
|
|
|
|
if !server.config.Acl.Check(&state.login, "read", dn, []string{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
e := ldap.NewSearchResultEntry(dn)
|
|
|
|
for attr, val := range entry {
|
|
|
|
// If attribute is not in request, exclude it from returned entry
|
|
|
|
if len(r.Attributes()) > 0 {
|
|
|
|
found := false
|
|
|
|
for _, requested := range r.Attributes() {
|
2020-02-11 22:20:33 +00:00
|
|
|
if string(requested) == "1.1" && len(r.Attributes()) == 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if string(requested) == "*" || strings.EqualFold(string(requested), attr) {
|
2020-01-26 21:08:27 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we are not allowed to read attribute, exclude it from returned entry
|
|
|
|
if !server.config.Acl.Check(&state.login, "read", dn, []string{attr}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Send result
|
|
|
|
for _, v := range val {
|
|
|
|
e.AddAttribute(message.AttributeDescription(attr),
|
|
|
|
message.AttributeValue(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ldap.LDAPResultSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyFilter(entry Entry, filter message.Filter) (bool, error) {
|
|
|
|
if fAnd, ok := filter.(message.FilterAnd); ok {
|
|
|
|
for _, cond := range fAnd {
|
|
|
|
res, err := applyFilter(entry, cond)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !res {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
} else if fOr, ok := filter.(message.FilterOr); ok {
|
|
|
|
for _, cond := range fOr {
|
|
|
|
res, err := applyFilter(entry, cond)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if res {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
} else if fNot, ok := filter.(message.FilterNot); ok {
|
|
|
|
res, err := applyFilter(entry, fNot.Filter)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return !res, nil
|
|
|
|
} else if fPresent, ok := filter.(message.FilterPresent); ok {
|
|
|
|
what := string(fPresent)
|
|
|
|
// Case insensitive search
|
|
|
|
for desc, values := range entry {
|
|
|
|
if strings.EqualFold(what, desc) {
|
|
|
|
return len(values) > 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
} else if fEquality, ok := filter.(message.FilterEqualityMatch); ok {
|
|
|
|
desc := string(fEquality.AttributeDesc())
|
|
|
|
target := string(fEquality.AssertionValue())
|
|
|
|
// Case insensitive attribute search
|
|
|
|
for entry_desc, value := range entry {
|
|
|
|
if strings.EqualFold(entry_desc, desc) {
|
|
|
|
for _, val := range value {
|
2020-02-02 12:32:12 +00:00
|
|
|
if valueMatch(entry_desc, val, target) {
|
2020-01-26 21:08:27 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
return false, fmt.Errorf("Unsupported filter: %#v %T", filter, filter)
|
|
|
|
}
|
|
|
|
}
|