service_matcher-go/ldap/ldap.go
crusader 704ba5cc5d ing
2018-09-03 16:23:25 +09:00

87 lines
1.9 KiB
Go

package ldap
import (
"math/rand"
"time"
osm "git.loafle.net/overflow/service_matcher-go"
ber "gopkg.in/asn1-ber.v1"
)
const (
ApplicationBindRequest = 0
ApplicationBindResponse = 1
)
type LDAPMatcher struct {
osm.Matchers
reqID int64
}
func (l *LDAPMatcher) Key() string {
return "LDAP"
}
func (l *LDAPMatcher) Name() string {
return "LDAP"
}
func (l *LDAPMatcher) Meta() osm.Metadata {
return nil
}
func (l *LDAPMatcher) IsPrePacket() bool {
return false
}
func (l *LDAPMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
return false
}
func (l *LDAPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
p := ber.DecodePacket(packet.Buffer)
if len(p.Children) <= 1 {
return osm.NotMatchedError()
}
respID, ok := p.Children[0].Value.(int64)
if !ok {
return osm.NotMatchedError()
}
if respID != l.reqID {
return osm.NotMatchedError()
}
if p.Children[1].Tag != ApplicationBindResponse {
return osm.NotMatchedError()
}
return nil
}
func NewMatcher() osm.Matcher {
m := &LDAPMatcher{}
rand.Seed(time.Now().UnixNano())
m.reqID = rand.Int63n(1000)
p := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
p.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, m.reqID, "MessageID"))
bindRequest := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
bindRequest.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 2, "Version"))
bindRequest.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "LOAFLEOVERFLOW", "User Name"))
bindRequest.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimitive, 0, "LOAFLEOVERFLOW", "Password"))
p.AppendChild(bindRequest)
m.AddPacket(osm.NewPacket(p.Bytes(), len(p.Bytes())))
return m
}