init
This commit is contained in:
82
ldap/ldap.go
Normal file
82
ldap/ldap.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package ldap
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
csm "git.loafle.net/commons/service_matcher-go"
|
||||
ber "gopkg.in/asn1-ber.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
ApplicationBindRequest = 0
|
||||
ApplicationBindResponse = 1
|
||||
)
|
||||
|
||||
type LDAPMatcher struct {
|
||||
csm.Matchers
|
||||
reqID int64
|
||||
}
|
||||
|
||||
func (l *LDAPMatcher) Key() string {
|
||||
return "LDAP"
|
||||
}
|
||||
|
||||
func (l *LDAPMatcher) Name() string {
|
||||
return "LDAP"
|
||||
}
|
||||
|
||||
func (l *LDAPMatcher) Meta() csm.Metadata {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LDAPMatcher) IsPrePacket() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (l *LDAPMatcher) IsError(info csm.MatchInfo, index int, packet *csm.Packet) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (l *LDAPMatcher) Match(info csm.MatchInfo, index int, packet *csm.Packet) error {
|
||||
|
||||
if packet == nil || packet.Buffer == nil || packet.Len == 0 {
|
||||
return csm.NoPacketReceivedError()
|
||||
}
|
||||
p := ber.DecodePacket(packet.Buffer)
|
||||
|
||||
respID, ok := p.Children[0].Value.(int64)
|
||||
if !ok {
|
||||
return csm.NotMatchedError()
|
||||
}
|
||||
if respID != l.reqID {
|
||||
return csm.NotMatchedError()
|
||||
}
|
||||
|
||||
if p.Children[1].Tag != ApplicationBindResponse {
|
||||
return csm.NotMatchedError()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewMatcher() csm.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(csm.NewPacket(p.Bytes(), len(p.Bytes())))
|
||||
|
||||
return m
|
||||
|
||||
}
|
||||
80
ldap/ldap_test.go
Normal file
80
ldap/ldap_test.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package ldap
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"math/rand"
|
||||
|
||||
csm "git.loafle.net/commons/service_matcher-go"
|
||||
"gopkg.in/asn1-ber.v1"
|
||||
)
|
||||
|
||||
func TestLdap(t *testing.T) {
|
||||
m := NewMatcher()
|
||||
|
||||
conn, err := net.Dial("tcp", "192.168.1.10:10389")
|
||||
if err != nil {
|
||||
t.Errorf("ERR %s", err)
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
for i := 0; i < m.PacketCount(); i++ {
|
||||
_, err := conn.Write(m.Packet(i).Buffer)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
bytes := make([]byte, 1024)
|
||||
n, _ := conn.Read(bytes)
|
||||
p := csm.NewPacket(bytes, n)
|
||||
|
||||
if err := m.Match(nil, i, p); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
t.Log(m.Name())
|
||||
t.Log(m.Meta())
|
||||
}
|
||||
|
||||
func TestBer(t *testing.T) {
|
||||
const (
|
||||
ApplicationBindRequest = 0
|
||||
ApplicationBindResponse = 1
|
||||
)
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
reqID := rand.Int63n(1000)
|
||||
packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
|
||||
|
||||
packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimitive, ber.TagInteger, 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"))
|
||||
packet.AppendChild(bindRequest)
|
||||
|
||||
conn, err := net.Dial("tcp", "192.168.1.10:10389")
|
||||
if err != nil {
|
||||
t.Errorf("ERR %s", err)
|
||||
}
|
||||
conn.Write(packet.Bytes())
|
||||
|
||||
p, err := ber.ReadPacket(conn)
|
||||
if err != nil {
|
||||
t.Errorf("ERR %s", err)
|
||||
}
|
||||
|
||||
respID, ok := p.Children[0].Value.(int64)
|
||||
if !ok {
|
||||
t.Errorf("%s", "cannot cast response ID")
|
||||
}
|
||||
if respID != reqID {
|
||||
t.Error("not matched message ID")
|
||||
}
|
||||
|
||||
if p.Children[1].Tag != ApplicationBindResponse {
|
||||
t.Error("Not match")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user