2020-08-21 02:03:53 -05:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2020-08-23 02:14:41 -05:00
|
|
|
"context"
|
|
|
|
"strconv"
|
2020-08-22 22:49:54 -05:00
|
|
|
"strings"
|
2020-08-21 02:03:53 -05:00
|
|
|
|
2020-08-22 22:49:54 -05:00
|
|
|
"github.com/ps78674/goldap/message"
|
2020-08-21 02:03:53 -05:00
|
|
|
ldap "github.com/ps78674/ldapserver"
|
2020-08-22 22:49:54 -05:00
|
|
|
|
|
|
|
"github.com/netauth/ldap/internal/buildinfo"
|
2020-08-23 02:14:41 -05:00
|
|
|
|
|
|
|
pb "github.com/netauth/protocol"
|
2020-08-21 02:03:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *server) handleSearchDSE(w ldap.ResponseWriter, m *ldap.Message) {
|
2020-08-22 22:49:54 -05:00
|
|
|
nc := strings.Join(s.nc, ", ")
|
|
|
|
|
2020-08-21 02:03:53 -05:00
|
|
|
e := ldap.NewSearchResultEntry("")
|
|
|
|
e.AddAttribute("vendorName", "NetAuth")
|
2020-08-22 22:49:54 -05:00
|
|
|
e.AddAttribute("vendorVersion", message.AttributeValue(buildinfo.Version))
|
2020-08-21 02:03:53 -05:00
|
|
|
e.AddAttribute("objectClass", "top", "extensibleObject")
|
|
|
|
e.AddAttribute("supportedLDAPVersion", "3")
|
2020-08-22 22:49:54 -05:00
|
|
|
e.AddAttribute("namingContexts", message.AttributeValue(nc))
|
2020-08-21 02:03:53 -05:00
|
|
|
w.Write(e)
|
|
|
|
|
|
|
|
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
|
|
|
|
w.Write(res)
|
|
|
|
}
|
|
|
|
|
2020-08-23 02:14:41 -05:00
|
|
|
func (s *server) handleSearchEntities(w ldap.ResponseWriter, m *ldap.Message) {
|
|
|
|
ctx := context.Background()
|
|
|
|
s.l.Debug("Search Entities")
|
|
|
|
|
2020-08-21 02:03:53 -05:00
|
|
|
r := m.GetSearchRequest()
|
2020-11-08 18:45:07 -06:00
|
|
|
expr, err := s.buildBleveQuery(r.Filter())
|
2020-08-23 02:14:41 -05:00
|
|
|
if err != nil {
|
|
|
|
// If err is non-nil at this point it must mean that
|
|
|
|
// the above match didn't find a supported filter.
|
2021-10-16 18:01:02 -05:00
|
|
|
s.l.Warn("Unsupported Search Filter, this is a bug, please file a report", "filter", r.Filter())
|
2020-08-23 02:14:41 -05:00
|
|
|
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultUnwillingToPerform)
|
|
|
|
res.SetDiagnosticMessage("Filter type not supported")
|
|
|
|
w.Write(res)
|
|
|
|
return
|
2020-08-21 02:03:53 -05:00
|
|
|
}
|
|
|
|
|
2020-08-23 02:14:41 -05:00
|
|
|
s.l.Debug("Searching entities", "query", expr)
|
2020-08-21 02:03:53 -05:00
|
|
|
|
2020-08-23 02:14:41 -05:00
|
|
|
ents, err := s.c.EntitySearch(ctx, expr)
|
|
|
|
if err != nil {
|
|
|
|
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultOperationsError)
|
|
|
|
res.SetDiagnosticMessage(err.Error())
|
|
|
|
w.Write(res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range ents {
|
|
|
|
e, err := s.entitySearchResult(ctx, ents[i], r.BaseObject(), r.Attributes())
|
|
|
|
if err != nil {
|
|
|
|
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultOperationsError)
|
|
|
|
res.SetDiagnosticMessage(err.Error())
|
|
|
|
w.Write(res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.l.Debug("Entities", "res", ents)
|
2020-08-21 02:03:53 -05:00
|
|
|
|
|
|
|
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
|
|
|
|
w.Write(res)
|
|
|
|
}
|
2020-08-23 02:14:41 -05:00
|
|
|
|
|
|
|
// entitySearchResult maps an entity onto a SearchResultEntry,
|
|
|
|
// performing the additional lookup for groups to populate the
|
|
|
|
// memberOf attribute. Though not implemented, the attrs list is
|
|
|
|
// plumbed down to this level to permit attribute filtering in the
|
|
|
|
// future.
|
|
|
|
func (s *server) entitySearchResult(ctx context.Context, e *pb.Entity, dn message.LDAPDN, attrs message.AttributeSelection) (message.SearchResultEntry, error) {
|
|
|
|
res := ldap.NewSearchResultEntry("uid=" + e.GetID() + "," + string(dn))
|
|
|
|
res.AddAttribute("uid", message.AttributeValue(e.GetID()))
|
|
|
|
res.AddAttribute("uidNumber", message.AttributeValue(strconv.Itoa(int(e.GetNumber()))))
|
|
|
|
|
|
|
|
grps, err := s.c.EntityGroups(ctx, e.GetID())
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:33:21 -06:00
|
|
|
memberOf := []message.AttributeValue{}
|
2020-08-23 02:14:41 -05:00
|
|
|
for i := range grps {
|
|
|
|
g := "cn=" + grps[i].GetName() + ",ou=groups," + strings.Join(s.nc, ",")
|
2020-11-08 21:33:21 -06:00
|
|
|
memberOf = append(memberOf, message.AttributeValue(g))
|
2020-08-23 02:14:41 -05:00
|
|
|
}
|
2020-11-08 21:33:21 -06:00
|
|
|
res.AddAttribute("memberOf", memberOf...)
|
2020-08-23 02:14:41 -05:00
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-08-23 02:50:43 -05:00
|
|
|
func (s *server) handleSearchGroups(w ldap.ResponseWriter, m *ldap.Message) {
|
|
|
|
ctx := context.Background()
|
|
|
|
s.l.Debug("Search Groups")
|
|
|
|
|
|
|
|
r := m.GetSearchRequest()
|
|
|
|
|
2020-11-08 18:45:07 -06:00
|
|
|
expr, err := s.buildBleveQuery(r.Filter())
|
2020-08-23 02:50:43 -05:00
|
|
|
if err != nil {
|
|
|
|
// If err is non-nil at this point it must mean that
|
|
|
|
// the above match didn't find a supported filter.
|
2021-10-16 18:01:02 -05:00
|
|
|
s.l.Warn("Unsupported Search Filter, this is a bug, please file a report", "filter", r.Filter())
|
2020-08-23 02:50:43 -05:00
|
|
|
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultUnwillingToPerform)
|
|
|
|
res.SetDiagnosticMessage("Filter type not supported")
|
|
|
|
w.Write(res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.l.Debug("Searching groups", "expr", expr)
|
|
|
|
|
2020-08-25 20:58:56 -05:00
|
|
|
groups, err := s.c.GroupSearch(ctx, expr)
|
2020-08-23 02:50:43 -05:00
|
|
|
if err != nil {
|
|
|
|
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultOperationsError)
|
|
|
|
res.SetDiagnosticMessage(err.Error())
|
|
|
|
w.Write(res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:58:56 -05:00
|
|
|
for i := range groups {
|
|
|
|
s.l.Debug("Found group", "group", groups[i].GetName())
|
|
|
|
e, err := s.groupSearchResult(ctx, groups[i], r.BaseObject(), r.Attributes())
|
2020-08-23 02:50:43 -05:00
|
|
|
if err != nil {
|
|
|
|
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultOperationsError)
|
|
|
|
res.SetDiagnosticMessage(err.Error())
|
|
|
|
w.Write(res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := ldap.NewSearchResultDoneResponse(ldap.LDAPResultSuccess)
|
|
|
|
w.Write(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// groupSearchResult maps a group onto a SearchResultEntry, performing
|
|
|
|
// the additional lookup for groups to populate the member attribute.
|
|
|
|
// Though not implemented, the attrs list is plumbed down to this
|
|
|
|
// level to permit attribute filtering in the future.
|
|
|
|
func (s *server) groupSearchResult(ctx context.Context, g *pb.Group, dn message.LDAPDN, attrs message.AttributeSelection) (message.SearchResultEntry, error) {
|
|
|
|
res := ldap.NewSearchResultEntry("cn=" + g.GetName() + "," + string(dn))
|
|
|
|
res.AddAttribute("cn", message.AttributeValue(g.GetName()))
|
|
|
|
res.AddAttribute("gidNumber", message.AttributeValue(strconv.Itoa(int(g.GetNumber()))))
|
|
|
|
|
|
|
|
members, err := s.c.GroupMembers(ctx, g.GetName())
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:33:21 -06:00
|
|
|
memberList := []message.AttributeValue{}
|
2020-08-23 02:50:43 -05:00
|
|
|
for i := range members {
|
|
|
|
g := "uid=" + members[i].GetID() + ",ou=entities," + strings.Join(s.nc, ",")
|
2020-11-08 21:33:21 -06:00
|
|
|
memberList = append(memberList, message.AttributeValue(g))
|
2020-08-23 02:50:43 -05:00
|
|
|
}
|
2020-11-08 21:33:21 -06:00
|
|
|
res.AddAttribute("member", memberList...)
|
2020-08-23 02:50:43 -05:00
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|