Skip to content

Commit

Permalink
Expose some query structures externally
Browse files Browse the repository at this point in the history
  • Loading branch information
lkarlslund committed Apr 14, 2023
1 parent b63280c commit fecf730
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 82 deletions.
4 changes: 2 additions & 2 deletions modules/query/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type IndexSelectorInfo struct {
func Execute(q NodeFilter, ao *engine.Objects) *engine.Objects {
var potentialindexes []IndexSelectorInfo
switch t := q.(type) {
case andquery:
case AndQuery:
// Iterate over all subitems
for _, st := range t.subitems {
for _, st := range t.Subitems {
if qo, ok := st.(FilterOneAttribute); ok {
if sm, ok := qo.q.(hasStringMatch); ok {
// This might be in an index
Expand Down
14 changes: 7 additions & 7 deletions modules/query/ldapparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func parseLDAPRuneQuery(s []rune, ao *engine.Objects) ([]rune, NodeFilter, error
}
// Strip )
s = s[1:]
result = andquery{subqueries}
result = AndQuery{subqueries}
case '|':
s, subqueries, err = parseMultipleLDAPRuneQueries(s[1:], ao)
if err != nil {
Expand All @@ -76,7 +76,7 @@ func parseLDAPRuneQuery(s []rune, ao *engine.Objects) ([]rune, NodeFilter, error
}
// Strip )
s = s[1:]
result = orquery{subqueries}
result = OrQuery{subqueries}
}

if result != nil {
Expand Down Expand Up @@ -255,7 +255,7 @@ valueloop:
if strings.EqualFold(attributename, "_pwnable") || strings.EqualFold(attributename, "in") {
direction = engine.In
}
return s, edgeQuery{direction, edge, target}, nil
return s, EdgeQuery{direction, edge, target}, nil
default:
attribute := engine.A(attributename)
if attribute == engine.NonExistingAttribute {
Expand Down Expand Up @@ -353,7 +353,7 @@ valueloop:
result = genwrapper(BinaryOrModifier{valuenum})
case "1.2.840.113556.1.4.1941", "dnchain":
// Matching rule in chain
result = genwrapper(recursiveDNmatcher{value, ao})
result = genwrapper(RecursiveDNmatcher{value, ao})
default:
return nil, nil, errors.New("Unknown modifier " + modifier)
}
Expand All @@ -370,7 +370,7 @@ valueloop:
if err != nil {
return nil, nil, err
}
result = genwrapper(hasRegexpMatch{r})
result = genwrapper(HasRegexpMatch{r})
} else if strings.ContainsAny(value, "?*") {
// glob magic
pattern := value
Expand All @@ -382,9 +382,9 @@ valueloop:
return nil, nil, err
}
if casesensitive {
result = genwrapper(hasGlobMatch{true, pattern, g})
result = genwrapper(HasGlobMatch{true, pattern, g})
} else {
result = genwrapper(hasGlobMatch{false, pattern, g})
result = genwrapper(HasGlobMatch{false, pattern, g})
}
} else {
result = genwrapper(hasStringMatch{casesensitive, value})
Expand Down
146 changes: 73 additions & 73 deletions modules/query/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,30 +172,30 @@ func (a LowerStringAttribute) Strings(o *engine.Object) []string {
return lo
}

type andquery struct {
subitems []NodeFilter
type AndQuery struct {
Subitems []NodeFilter
}

func (q andquery) Evaluate(o *engine.Object) bool {
for _, query := range q.subitems {
func (q AndQuery) Evaluate(o *engine.Object) bool {
for _, query := range q.Subitems {
if !query.Evaluate(o) {
return false
}
}
return true
}

func (q andquery) ToLDAPFilter() string {
func (q AndQuery) ToLDAPFilter() string {
result := "&"
for _, query := range q.subitems {
for _, query := range q.Subitems {
result += query.ToLDAPFilter()
}
return result
}

func (q andquery) ToWhereClause() string {
func (q AndQuery) ToWhereClause() string {
var result string
for i, query := range q.subitems {
for i, query := range q.Subitems {
if i > 0 {
result += " AND "
}
Expand All @@ -204,30 +204,30 @@ func (q andquery) ToWhereClause() string {
return result
}

type orquery struct {
subitems []NodeFilter
type OrQuery struct {
Subitems []NodeFilter
}

func (q orquery) Evaluate(o *engine.Object) bool {
for _, query := range q.subitems {
func (q OrQuery) Evaluate(o *engine.Object) bool {
for _, query := range q.Subitems {
if query.Evaluate(o) {
return true
}
}
return false
}

func (q orquery) ToLDAPFilter() string {
func (q OrQuery) ToLDAPFilter() string {
result := "|"
for _, query := range q.subitems {
for _, query := range q.Subitems {
result += query.ToLDAPFilter()
}
return result
}

func (q orquery) ToWhereClause() string {
func (q OrQuery) ToWhereClause() string {
result := "("
for i, query := range q.subitems {
for i, query := range q.Subitems {
if i > 0 {
result += " OR "
}
Expand All @@ -238,15 +238,15 @@ func (q orquery) ToWhereClause() string {
}

type NotQuery struct {
subitem NodeFilter
Subitem NodeFilter
}

func (q NotQuery) Evaluate(o *engine.Object) bool {
return !q.subitem.Evaluate(o)
return !q.Subitem.Evaluate(o)
}

func (q NotQuery) ToLDAPFilter() string {
return "!(" + q.subitem.ToLDAPFilter() + ")"
return "!(" + q.Subitem.ToLDAPFilter() + ")"
}

func (q NotQuery) ToWhereClause() string {
Expand Down Expand Up @@ -346,9 +346,9 @@ func (sm SinceModifier) ToWhereClause(a string) string {
}

type TimediffModifier struct {
a2 engine.Attribute
c comparatortype
ts *timespan.Timespan
A2 engine.Attribute
C comparatortype
TS *timespan.Timespan
}

func (td TimediffModifier) Evaluate(a engine.Attribute, o *engine.Object) bool {
Expand All @@ -357,7 +357,7 @@ func (td TimediffModifier) Evaluate(a engine.Attribute, o *engine.Object) bool {
return false
}

val2s, found := o.Get(td.a2)
val2s, found := o.Get(td.A2)
if !found {
return false
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func (td TimediffModifier) Evaluate(a engine.Attribute, o *engine.Object) bool {
return false // break
}

if td.c.Compare(t1.Unix(), td.ts.From(t2).Unix()) {
if td.C.Compare(t1.Unix(), td.TS.From(t2).Unix()) {
result = true
return false // break
}
Expand All @@ -401,11 +401,11 @@ func (td TimediffModifier) Evaluate(a engine.Attribute, o *engine.Object) bool {
}

func (td TimediffModifier) ToLDAPFilter(a string) string {
return a + ":timediff(" + td.a2.String() + "): " + td.c.String() + td.ts.String()
return a + ":timediff(" + td.A2.String() + "): " + td.C.String() + td.TS.String()
}

func (td TimediffModifier) ToWhereClause(a string) string {
return "TIMEIDFF(" + a + "," + td.a2.String() + ")" + td.c.String() + td.ts.String()
return "TIMEIDFF(" + a + "," + td.A2.String() + ")" + td.C.String() + td.TS.String()
}

type BinaryAndModifier struct {
Expand Down Expand Up @@ -572,22 +572,22 @@ func (hsm hasStringMatch) ToWhereClause(a string) string {
return a + "=" + hsm.m
}

type hasGlobMatch struct {
casesensitive bool
globstr string
m glob.Glob
type HasGlobMatch struct {
Casesensitive bool
Globstr string
M glob.Glob
}

func (hgm hasGlobMatch) Evaluate(a engine.Attribute, o *engine.Object) bool {
func (hgm HasGlobMatch) Evaluate(a engine.Attribute, o *engine.Object) bool {
var result bool
o.AttrRendered(a).Iterate(func(value engine.AttributeValue) bool {
if !hgm.casesensitive {
if hgm.m.Match(strings.ToLower(value.String())) {
if !hgm.Casesensitive {
if hgm.M.Match(strings.ToLower(value.String())) {
result = true
return false // break
}
} else {
if hgm.m.Match(value.String()) {
if hgm.M.Match(value.String()) {
result = true
return false // break
}
Expand All @@ -597,21 +597,21 @@ func (hgm hasGlobMatch) Evaluate(a engine.Attribute, o *engine.Object) bool {
return result
}

func (hgm hasGlobMatch) ToLDAPFilter(a string) string {
return a + ":glob:=" + hgm.globstr
func (hgm HasGlobMatch) ToLDAPFilter(a string) string {
return a + ":glob:=" + hgm.Globstr
}

func (hgm hasGlobMatch) ToWhereClause(a string) string {
return "GLOB(" + a + "," + hgm.globstr + ")"
func (hgm HasGlobMatch) ToWhereClause(a string) string {
return "GLOB(" + a + "," + hgm.Globstr + ")"
}

type hasRegexpMatch struct {
m *regexp.Regexp
type HasRegexpMatch struct {
R *regexp.Regexp
}

func (hrm hasRegexpMatch) Evaluate(a engine.Attribute, o *engine.Object) (result bool) {
func (hrm HasRegexpMatch) Evaluate(a engine.Attribute, o *engine.Object) (result bool) {
o.AttrRendered(a).Iterate(func(value engine.AttributeValue) bool {
if hrm.m.MatchString(value.String()) {
if hrm.R.MatchString(value.String()) {
result = true
return false // break
}
Expand All @@ -620,29 +620,29 @@ func (hrm hasRegexpMatch) Evaluate(a engine.Attribute, o *engine.Object) (result
return
}

func (hrm hasRegexpMatch) ToLDAPFilter(a string) string {
return a + "=/" + hrm.m.String() + "/"
func (hrm HasRegexpMatch) ToLDAPFilter(a string) string {
return a + "=/" + hrm.R.String() + "/"
}

func (hrm hasRegexpMatch) ToWhereClause(a string) string {
return "REGEXP(" + a + ", " + hrm.m.String() + ")"
func (hrm HasRegexpMatch) ToWhereClause(a string) string {
return "REGEXP(" + a + ", " + hrm.R.String() + ")"
}

type recursiveDNmatcher struct {
dn string
ao *engine.Objects
type RecursiveDNmatcher struct {
DN string
AO *engine.Objects
}

func (rdn recursiveDNmatcher) Evaluate(a engine.Attribute, o *engine.Object) bool {
return recursiveDNmatchFunc(o, a, rdn.dn, 10, rdn.ao)
func (rdn RecursiveDNmatcher) Evaluate(a engine.Attribute, o *engine.Object) bool {
return recursiveDNmatchFunc(o, a, rdn.DN, 10, rdn.AO)
}

func (rdn recursiveDNmatcher) ToLDAPFilter(a string) string {
return a + ":recursiveDN:=" + rdn.dn
func (rdn RecursiveDNmatcher) ToLDAPFilter(a string) string {
return a + ":recursiveDN:=" + rdn.DN
}

func (rdn recursiveDNmatcher) ToWhereClause(a string) string {
return "RECURSIVEDNMATCH(" + a + ", " + rdn.dn + ")"
func (rdn RecursiveDNmatcher) ToWhereClause(a string) string {
return "RECURSIVEDNMATCH(" + a + ", " + rdn.DN + ")"
}

func recursiveDNmatchFunc(o *engine.Object, a engine.Attribute, dn string, maxdepth int, ao *engine.Objects) (result bool) {
Expand All @@ -667,17 +667,17 @@ func recursiveDNmatchFunc(o *engine.Object, a engine.Attribute, dn string, maxde
return
}

type edgeQuery struct {
direction engine.EdgeDirection
edge engine.Edge
target NodeFilter
type EdgeQuery struct {
Direction engine.EdgeDirection
Edge engine.Edge
Target NodeFilter
}

func (p edgeQuery) Evaluate(o *engine.Object) bool {
func (p EdgeQuery) Evaluate(o *engine.Object) bool {
var result bool
o.Edges(p.direction).Range(func(target *engine.Object, edge engine.EdgeBitmap) bool {
if (p.edge == engine.AnyEdgeType && !edge.IsBlank()) || edge.IsSet(p.edge) {
if p.target == nil || p.target.Evaluate(target) {
o.Edges(p.Direction).Range(func(target *engine.Object, edge engine.EdgeBitmap) bool {
if (p.Edge == engine.AnyEdgeType && !edge.IsBlank()) || edge.IsSet(p.Edge) {
if p.Target == nil || p.Target.Evaluate(target) {
result = true
return false // return from loop
}
Expand All @@ -687,30 +687,30 @@ func (p edgeQuery) Evaluate(o *engine.Object) bool {
return result
}

func (p edgeQuery) ToLDAPFilter() string {
func (p EdgeQuery) ToLDAPFilter() string {
var result string
if p.direction == engine.Out {
if p.Direction == engine.Out {
result += "out"
} else {
result += "in"
}
result += "=" + p.edge.String()
if p.target != nil {
result += "(" + p.target.ToLDAPFilter() + ")"
result += "=" + p.Edge.String()
if p.Target != nil {
result += "(" + p.Target.ToLDAPFilter() + ")"
}
return result
}

func (p edgeQuery) ToWhereClause() string {
func (p EdgeQuery) ToWhereClause() string {
var result string
if p.direction == engine.Out {
if p.Direction == engine.Out {
result += "out"
} else {
result += "in"
}
result += "=" + p.edge.String()
if p.target != nil {
result += "(" + p.target.ToWhereClause() + ")"
result += "=" + p.Edge.String()
if p.Target != nil {
result += "(" + p.Target.ToWhereClause() + ")"
}
return result
}

0 comments on commit fecf730

Please sign in to comment.