2016-11-03 18:16:01 -04:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-02-18 07:34:37 -05:00
|
|
|
"fmt"
|
2016-11-03 18:16:01 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2019-01-23 18:25:33 -05:00
|
|
|
"gopkg.in/asn1-ber.v1"
|
2016-11-03 18:16:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// LDAP Application Codes
|
|
|
|
const (
|
|
|
|
ApplicationBindRequest = 0
|
|
|
|
ApplicationBindResponse = 1
|
|
|
|
ApplicationUnbindRequest = 2
|
|
|
|
ApplicationSearchRequest = 3
|
|
|
|
ApplicationSearchResultEntry = 4
|
|
|
|
ApplicationSearchResultDone = 5
|
|
|
|
ApplicationModifyRequest = 6
|
|
|
|
ApplicationModifyResponse = 7
|
|
|
|
ApplicationAddRequest = 8
|
|
|
|
ApplicationAddResponse = 9
|
|
|
|
ApplicationDelRequest = 10
|
|
|
|
ApplicationDelResponse = 11
|
|
|
|
ApplicationModifyDNRequest = 12
|
|
|
|
ApplicationModifyDNResponse = 13
|
|
|
|
ApplicationCompareRequest = 14
|
|
|
|
ApplicationCompareResponse = 15
|
|
|
|
ApplicationAbandonRequest = 16
|
|
|
|
ApplicationSearchResultReference = 19
|
|
|
|
ApplicationExtendedRequest = 23
|
|
|
|
ApplicationExtendedResponse = 24
|
|
|
|
)
|
|
|
|
|
|
|
|
// ApplicationMap contains human readable descriptions of LDAP Application Codes
|
|
|
|
var ApplicationMap = map[uint8]string{
|
|
|
|
ApplicationBindRequest: "Bind Request",
|
|
|
|
ApplicationBindResponse: "Bind Response",
|
|
|
|
ApplicationUnbindRequest: "Unbind Request",
|
|
|
|
ApplicationSearchRequest: "Search Request",
|
|
|
|
ApplicationSearchResultEntry: "Search Result Entry",
|
|
|
|
ApplicationSearchResultDone: "Search Result Done",
|
|
|
|
ApplicationModifyRequest: "Modify Request",
|
|
|
|
ApplicationModifyResponse: "Modify Response",
|
|
|
|
ApplicationAddRequest: "Add Request",
|
|
|
|
ApplicationAddResponse: "Add Response",
|
|
|
|
ApplicationDelRequest: "Del Request",
|
|
|
|
ApplicationDelResponse: "Del Response",
|
|
|
|
ApplicationModifyDNRequest: "Modify DN Request",
|
|
|
|
ApplicationModifyDNResponse: "Modify DN Response",
|
|
|
|
ApplicationCompareRequest: "Compare Request",
|
|
|
|
ApplicationCompareResponse: "Compare Response",
|
|
|
|
ApplicationAbandonRequest: "Abandon Request",
|
|
|
|
ApplicationSearchResultReference: "Search Result Reference",
|
|
|
|
ApplicationExtendedRequest: "Extended Request",
|
|
|
|
ApplicationExtendedResponse: "Extended Response",
|
|
|
|
}
|
|
|
|
|
2017-04-24 10:31:46 -04:00
|
|
|
// Ldap Behera Password Policy Draft 10 (https://tools.ietf.org/html/draft-behera-ldap-password-policy-10)
|
2016-11-03 18:16:01 -04:00
|
|
|
const (
|
|
|
|
BeheraPasswordExpired = 0
|
|
|
|
BeheraAccountLocked = 1
|
|
|
|
BeheraChangeAfterReset = 2
|
|
|
|
BeheraPasswordModNotAllowed = 3
|
|
|
|
BeheraMustSupplyOldPassword = 4
|
|
|
|
BeheraInsufficientPasswordQuality = 5
|
|
|
|
BeheraPasswordTooShort = 6
|
|
|
|
BeheraPasswordTooYoung = 7
|
|
|
|
BeheraPasswordInHistory = 8
|
|
|
|
)
|
|
|
|
|
|
|
|
// BeheraPasswordPolicyErrorMap contains human readable descriptions of Behera Password Policy error codes
|
|
|
|
var BeheraPasswordPolicyErrorMap = map[int8]string{
|
|
|
|
BeheraPasswordExpired: "Password expired",
|
|
|
|
BeheraAccountLocked: "Account locked",
|
|
|
|
BeheraChangeAfterReset: "Password must be changed",
|
|
|
|
BeheraPasswordModNotAllowed: "Policy prevents password modification",
|
|
|
|
BeheraMustSupplyOldPassword: "Policy requires old password in order to change password",
|
|
|
|
BeheraInsufficientPasswordQuality: "Password fails quality checks",
|
|
|
|
BeheraPasswordTooShort: "Password is too short for policy",
|
|
|
|
BeheraPasswordTooYoung: "Password has been changed too recently",
|
|
|
|
BeheraPasswordInHistory: "New password is in list of old passwords",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds descriptions to an LDAP Response packet for debugging
|
|
|
|
func addLDAPDescriptions(packet *ber.Packet) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = NewError(ErrorDebugging, errors.New("ldap: cannot process packet to add descriptions"))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
packet.Description = "LDAP Response"
|
|
|
|
packet.Children[0].Description = "Message ID"
|
|
|
|
|
|
|
|
application := uint8(packet.Children[1].Tag)
|
|
|
|
packet.Children[1].Description = ApplicationMap[application]
|
|
|
|
|
|
|
|
switch application {
|
|
|
|
case ApplicationBindRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationBindResponse:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addDefaultLDAPResponseDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationUnbindRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationSearchRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationSearchResultEntry:
|
|
|
|
packet.Children[1].Children[0].Description = "Object Name"
|
|
|
|
packet.Children[1].Children[1].Description = "Attributes"
|
|
|
|
for _, child := range packet.Children[1].Children[1].Children {
|
|
|
|
child.Description = "Attribute"
|
|
|
|
child.Children[0].Description = "Attribute Name"
|
|
|
|
child.Children[1].Description = "Attribute Values"
|
|
|
|
for _, grandchild := range child.Children[1].Children {
|
|
|
|
grandchild.Description = "Attribute Value"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(packet.Children) == 3 {
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addControlDescriptions(packet.Children[2])
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
case ApplicationSearchResultDone:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addDefaultLDAPResponseDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationModifyRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationModifyResponse:
|
|
|
|
case ApplicationAddRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationAddResponse:
|
|
|
|
case ApplicationDelRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationDelResponse:
|
|
|
|
case ApplicationModifyDNRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationModifyDNResponse:
|
|
|
|
case ApplicationCompareRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationCompareResponse:
|
|
|
|
case ApplicationAbandonRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationSearchResultReference:
|
|
|
|
case ApplicationExtendedRequest:
|
2019-02-18 07:34:37 -05:00
|
|
|
err = addRequestDescriptions(packet)
|
2016-11-03 18:16:01 -04:00
|
|
|
case ApplicationExtendedResponse:
|
|
|
|
}
|
|
|
|
|
2019-02-18 07:34:37 -05:00
|
|
|
return err
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
2019-02-18 07:34:37 -05:00
|
|
|
func addControlDescriptions(packet *ber.Packet) error {
|
2016-11-03 18:16:01 -04:00
|
|
|
packet.Description = "Controls"
|
|
|
|
for _, child := range packet.Children {
|
2019-01-23 18:25:33 -05:00
|
|
|
var value *ber.Packet
|
|
|
|
controlType := ""
|
2016-11-03 18:16:01 -04:00
|
|
|
child.Description = "Control"
|
2019-01-23 18:25:33 -05:00
|
|
|
switch len(child.Children) {
|
|
|
|
case 0:
|
|
|
|
// at least one child is required for control type
|
2019-02-18 07:34:37 -05:00
|
|
|
return fmt.Errorf("at least one child is required for control type")
|
2019-01-23 18:25:33 -05:00
|
|
|
|
|
|
|
case 1:
|
|
|
|
// just type, no criticality or value
|
|
|
|
controlType = child.Children[0].Value.(string)
|
|
|
|
child.Children[0].Description = "Control Type (" + ControlTypeMap[controlType] + ")"
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
controlType = child.Children[0].Value.(string)
|
|
|
|
child.Children[0].Description = "Control Type (" + ControlTypeMap[controlType] + ")"
|
|
|
|
// Children[1] could be criticality or value (both are optional)
|
|
|
|
// duck-type on whether this is a boolean
|
|
|
|
if _, ok := child.Children[1].Value.(bool); ok {
|
|
|
|
child.Children[1].Description = "Criticality"
|
|
|
|
} else {
|
|
|
|
child.Children[1].Description = "Control Value"
|
|
|
|
value = child.Children[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
// criticality and value present
|
|
|
|
controlType = child.Children[0].Value.(string)
|
|
|
|
child.Children[0].Description = "Control Type (" + ControlTypeMap[controlType] + ")"
|
2016-11-03 18:16:01 -04:00
|
|
|
child.Children[1].Description = "Criticality"
|
2019-01-23 18:25:33 -05:00
|
|
|
child.Children[2].Description = "Control Value"
|
2016-11-03 18:16:01 -04:00
|
|
|
value = child.Children[2]
|
|
|
|
|
2019-01-23 18:25:33 -05:00
|
|
|
default:
|
|
|
|
// more than 3 children is invalid
|
2019-02-18 07:34:37 -05:00
|
|
|
return fmt.Errorf("more than 3 children for control packet found")
|
2019-01-23 18:25:33 -05:00
|
|
|
}
|
2019-02-18 07:34:37 -05:00
|
|
|
|
2019-01-23 18:25:33 -05:00
|
|
|
if value == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch controlType {
|
2016-11-03 18:16:01 -04:00
|
|
|
case ControlTypePaging:
|
|
|
|
value.Description += " (Paging)"
|
|
|
|
if value.Value != nil {
|
2019-02-18 07:34:37 -05:00
|
|
|
valueChildren, err := ber.DecodePacketErr(value.Data.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode data bytes: %s", err)
|
|
|
|
}
|
2016-11-03 18:16:01 -04:00
|
|
|
value.Data.Truncate(0)
|
|
|
|
value.Value = nil
|
|
|
|
valueChildren.Children[1].Value = valueChildren.Children[1].Data.Bytes()
|
|
|
|
value.AppendChild(valueChildren)
|
|
|
|
}
|
|
|
|
value.Children[0].Description = "Real Search Control Value"
|
|
|
|
value.Children[0].Children[0].Description = "Paging Size"
|
|
|
|
value.Children[0].Children[1].Description = "Cookie"
|
|
|
|
|
|
|
|
case ControlTypeBeheraPasswordPolicy:
|
|
|
|
value.Description += " (Password Policy - Behera Draft)"
|
|
|
|
if value.Value != nil {
|
2019-02-18 07:34:37 -05:00
|
|
|
valueChildren, err := ber.DecodePacketErr(value.Data.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode data bytes: %s", err)
|
|
|
|
}
|
2016-11-03 18:16:01 -04:00
|
|
|
value.Data.Truncate(0)
|
|
|
|
value.Value = nil
|
|
|
|
value.AppendChild(valueChildren)
|
|
|
|
}
|
|
|
|
sequence := value.Children[0]
|
|
|
|
for _, child := range sequence.Children {
|
|
|
|
if child.Tag == 0 {
|
|
|
|
//Warning
|
2019-01-23 18:25:33 -05:00
|
|
|
warningPacket := child.Children[0]
|
2019-02-18 07:34:37 -05:00
|
|
|
packet, err := ber.DecodePacketErr(warningPacket.Data.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode data bytes: %s", err)
|
|
|
|
}
|
2016-11-03 18:16:01 -04:00
|
|
|
val, ok := packet.Value.(int64)
|
|
|
|
if ok {
|
2019-01-23 18:25:33 -05:00
|
|
|
if warningPacket.Tag == 0 {
|
2016-11-03 18:16:01 -04:00
|
|
|
//timeBeforeExpiration
|
|
|
|
value.Description += " (TimeBeforeExpiration)"
|
2019-01-23 18:25:33 -05:00
|
|
|
warningPacket.Value = val
|
|
|
|
} else if warningPacket.Tag == 1 {
|
2016-11-03 18:16:01 -04:00
|
|
|
//graceAuthNsRemaining
|
|
|
|
value.Description += " (GraceAuthNsRemaining)"
|
2019-01-23 18:25:33 -05:00
|
|
|
warningPacket.Value = val
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if child.Tag == 1 {
|
|
|
|
// Error
|
2019-02-18 07:34:37 -05:00
|
|
|
packet, err := ber.DecodePacketErr(child.Data.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode data bytes: %s", err)
|
|
|
|
}
|
2016-11-03 18:16:01 -04:00
|
|
|
val, ok := packet.Value.(int8)
|
|
|
|
if !ok {
|
|
|
|
val = -1
|
|
|
|
}
|
|
|
|
child.Description = "Error"
|
|
|
|
child.Value = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 07:34:37 -05:00
|
|
|
return nil
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
2019-02-18 07:34:37 -05:00
|
|
|
func addRequestDescriptions(packet *ber.Packet) error {
|
2016-11-03 18:16:01 -04:00
|
|
|
packet.Description = "LDAP Request"
|
|
|
|
packet.Children[0].Description = "Message ID"
|
|
|
|
packet.Children[1].Description = ApplicationMap[uint8(packet.Children[1].Tag)]
|
|
|
|
if len(packet.Children) == 3 {
|
2019-02-18 07:34:37 -05:00
|
|
|
return addControlDescriptions(packet.Children[2])
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
2019-02-18 07:34:37 -05:00
|
|
|
return nil
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
2019-02-18 07:34:37 -05:00
|
|
|
func addDefaultLDAPResponseDescriptions(packet *ber.Packet) error {
|
|
|
|
err := GetLDAPError(packet)
|
|
|
|
packet.Children[1].Children[0].Description = "Result Code (" + LDAPResultCodeMap[err.(*Error).ResultCode] + ")"
|
|
|
|
packet.Children[1].Children[1].Description = "Matched DN (" + err.(*Error).MatchedDN + ")"
|
2016-11-03 18:16:01 -04:00
|
|
|
packet.Children[1].Children[2].Description = "Error Message"
|
|
|
|
if len(packet.Children[1].Children) > 3 {
|
|
|
|
packet.Children[1].Children[3].Description = "Referral"
|
|
|
|
}
|
|
|
|
if len(packet.Children) == 3 {
|
2019-02-18 07:34:37 -05:00
|
|
|
return addControlDescriptions(packet.Children[2])
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
2019-02-18 07:34:37 -05:00
|
|
|
return nil
|
2016-11-03 18:16:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DebugBinaryFile reads and prints packets from the given filename
|
|
|
|
func DebugBinaryFile(fileName string) error {
|
|
|
|
file, err := ioutil.ReadFile(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return NewError(ErrorDebugging, err)
|
|
|
|
}
|
|
|
|
ber.PrintBytes(os.Stdout, file, "")
|
2019-02-18 07:34:37 -05:00
|
|
|
packet, err := ber.DecodePacketErr(file)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode packet: %s", err)
|
|
|
|
}
|
|
|
|
if err := addLDAPDescriptions(packet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-03 18:16:01 -04:00
|
|
|
ber.PrintPacket(packet)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var hex = "0123456789abcdef"
|
|
|
|
|
|
|
|
func mustEscape(c byte) bool {
|
|
|
|
return c > 0x7f || c == '(' || c == ')' || c == '\\' || c == '*' || c == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// EscapeFilter escapes from the provided LDAP filter string the special
|
|
|
|
// characters in the set `()*\` and those out of the range 0 < c < 0x80,
|
|
|
|
// as defined in RFC4515.
|
|
|
|
func EscapeFilter(filter string) string {
|
|
|
|
escape := 0
|
|
|
|
for i := 0; i < len(filter); i++ {
|
|
|
|
if mustEscape(filter[i]) {
|
|
|
|
escape++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if escape == 0 {
|
|
|
|
return filter
|
|
|
|
}
|
|
|
|
buf := make([]byte, len(filter)+escape*2)
|
|
|
|
for i, j := 0, 0; i < len(filter); i++ {
|
|
|
|
c := filter[i]
|
|
|
|
if mustEscape(c) {
|
|
|
|
buf[j+0] = '\\'
|
|
|
|
buf[j+1] = hex[c>>4]
|
|
|
|
buf[j+2] = hex[c&0xf]
|
|
|
|
j += 3
|
|
|
|
} else {
|
|
|
|
buf[j] = c
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(buf)
|
|
|
|
}
|