diff --git a/add.go b/add.go index baecd787..c3101b76 100644 --- a/add.go +++ b/add.go @@ -1,8 +1,6 @@ package ldap import ( - "log" - ber "github.com/go-asn1-ber/asn1-ber" ) @@ -85,7 +83,7 @@ func (l *Conn) Add(addRequest *AddRequest) error { return err } } else { - log.Printf("Unexpected Response: %d", packet.Children[1].Tag) + logger.Printf("Unexpected Response: %d", packet.Children[1].Tag) } return nil } diff --git a/conn.go b/conn.go index ae5e19af..6ed7b5e6 100644 --- a/conn.go +++ b/conn.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "errors" "fmt" - "log" "net" "net/url" "sync" @@ -272,7 +271,7 @@ func (l *Conn) Close() { l.Debug.Printf("Closing network connection") if err := l.conn.Close(); err != nil { - log.Println(err) + logger.Println(err) } l.wgClose.Done() @@ -443,7 +442,7 @@ func (l *Conn) sendProcessMessage(message *messagePacket) bool { func (l *Conn) processMessages() { defer func() { if err := recover(); err != nil { - log.Printf("ldap: recovered panic in processMessages: %v", err) + logger.Printf("ldap: recovered panic in processMessages: %v", err) } for messageID, msgCtx := range l.messageContexts { // If we are closing due to an error, inform anyone who @@ -492,7 +491,7 @@ func (l *Conn) processMessages() { go func() { defer func() { if err := recover(); err != nil { - log.Printf("ldap: recovered panic in RequestTimeout: %v", err) + logger.Printf("ldap: recovered panic in RequestTimeout: %v", err) } }() time.Sleep(requestTimeout) @@ -508,7 +507,7 @@ func (l *Conn) processMessages() { if msgCtx, ok := l.messageContexts[message.MessageID]; ok { msgCtx.sendResponse(&PacketResponse{message.Packet, nil}) } else { - log.Printf("Received unexpected message %d, %v", message.MessageID, l.IsClosing()) + logger.Printf("Received unexpected message %d, %v", message.MessageID, l.IsClosing()) l.Debug.PrintPacket(message.Packet) } case MessageTimeout: @@ -535,7 +534,7 @@ func (l *Conn) reader() { cleanstop := false defer func() { if err := recover(); err != nil { - log.Printf("ldap: recovered panic in reader: %v", err) + logger.Printf("ldap: recovered panic in reader: %v", err) } if !cleanstop { l.Close() diff --git a/debug.go b/debug.go index d0a8fc15..74cc65a4 100644 --- a/debug.go +++ b/debug.go @@ -1,8 +1,6 @@ package ldap import ( - "log" - ber "github.com/go-asn1-ber/asn1-ber" ) @@ -18,13 +16,13 @@ func (debug *debugging) Enable(b bool) { // Printf writes debug output. func (debug debugging) Printf(format string, args ...interface{}) { if debug { - log.Printf(format, args...) + logger.Printf(format, args...) } } // PrintPacket dumps a packet. func (debug debugging) PrintPacket(packet *ber.Packet) { if debug { - ber.WritePacket(log.Writer(), packet) + ber.WritePacket(logger.Writer(), packet) } } diff --git a/del.go b/del.go index 6e987267..bac0dfb7 100644 --- a/del.go +++ b/del.go @@ -1,8 +1,6 @@ package ldap import ( - "log" - ber "github.com/go-asn1-ber/asn1-ber" ) @@ -53,7 +51,7 @@ func (l *Conn) Del(delRequest *DelRequest) error { return err } } else { - log.Printf("Unexpected Response: %d", packet.Children[1].Tag) + logger.Printf("Unexpected Response: %d", packet.Children[1].Tag) } return nil } diff --git a/ldap.go b/ldap.go index 7ae6dfe2..fb75a89c 100644 --- a/ldap.go +++ b/ldap.go @@ -3,6 +3,7 @@ package ldap import ( "fmt" "io/ioutil" + "log" "os" ber "github.com/go-asn1-ber/asn1-ber" @@ -82,6 +83,13 @@ var BeheraPasswordPolicyErrorMap = map[int8]string{ BeheraPasswordInHistory: "New password is in list of old passwords", } +var logger = log.New(os.Stderr, "", log.LstdFlags) + +// Logger allows clients to override the default logger +func Logger(l *log.Logger) { + logger = l +} + // Adds descriptions to an LDAP Response packet for debugging func addLDAPDescriptions(packet *ber.Packet) (err error) { defer func() { @@ -221,18 +229,18 @@ func addControlDescriptions(packet *ber.Packet) error { sequence := value.Children[0] for _, child := range sequence.Children { if child.Tag == 0 { - //Warning + // Warning warningPacket := child.Children[0] val, err := ber.ParseInt64(warningPacket.Data.Bytes()) if err != nil { return fmt.Errorf("failed to decode data bytes: %s", err) } if warningPacket.Tag == 0 { - //timeBeforeExpiration + // timeBeforeExpiration value.Description += " (TimeBeforeExpiration)" warningPacket.Value = val } else if warningPacket.Tag == 1 { - //graceAuthNsRemaining + // graceAuthNsRemaining value.Description += " (GraceAuthNsRemaining)" warningPacket.Value = val } diff --git a/moddn.go b/moddn.go index 71cdcd0b..ec246d1f 100644 --- a/moddn.go +++ b/moddn.go @@ -1,8 +1,6 @@ package ldap import ( - "log" - ber "github.com/go-asn1-ber/asn1-ber" ) @@ -42,7 +40,7 @@ func NewModifyDNRequest(dn string, rdn string, delOld bool, newSup string) *Modi // // Refer NewModifyDNRequest for other parameters func NewModifyDNWithControlsRequest(dn string, rdn string, delOld bool, - newSup string, controls []Control) *ModifyDNRequest { + newSup string, controls []Control) *ModifyDNRequest { return &ModifyDNRequest{ DN: dn, NewRDN: rdn, @@ -94,7 +92,7 @@ func (l *Conn) ModifyDN(m *ModifyDNRequest) error { return err } } else { - log.Printf("Unexpected Response: %d", packet.Children[1].Tag) + logger.Printf("Unexpected Response: %d", packet.Children[1].Tag) } return nil } diff --git a/modify.go b/modify.go index 1821413d..f6fe3859 100644 --- a/modify.go +++ b/modify.go @@ -2,7 +2,6 @@ package ldap import ( "errors" - "log" ber "github.com/go-asn1-ber/asn1-ber" ) @@ -127,7 +126,7 @@ func (l *Conn) Modify(modifyRequest *ModifyRequest) error { return err } } else { - log.Printf("Unexpected Response: %d", packet.Children[1].Tag) + logger.Printf("Unexpected Response: %d", packet.Children[1].Tag) } return nil } diff --git a/v3/add.go b/v3/add.go index baecd787..c3101b76 100644 --- a/v3/add.go +++ b/v3/add.go @@ -1,8 +1,6 @@ package ldap import ( - "log" - ber "github.com/go-asn1-ber/asn1-ber" ) @@ -85,7 +83,7 @@ func (l *Conn) Add(addRequest *AddRequest) error { return err } } else { - log.Printf("Unexpected Response: %d", packet.Children[1].Tag) + logger.Printf("Unexpected Response: %d", packet.Children[1].Tag) } return nil } diff --git a/v3/conn.go b/v3/conn.go index ae5e19af..6ed7b5e6 100644 --- a/v3/conn.go +++ b/v3/conn.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "errors" "fmt" - "log" "net" "net/url" "sync" @@ -272,7 +271,7 @@ func (l *Conn) Close() { l.Debug.Printf("Closing network connection") if err := l.conn.Close(); err != nil { - log.Println(err) + logger.Println(err) } l.wgClose.Done() @@ -443,7 +442,7 @@ func (l *Conn) sendProcessMessage(message *messagePacket) bool { func (l *Conn) processMessages() { defer func() { if err := recover(); err != nil { - log.Printf("ldap: recovered panic in processMessages: %v", err) + logger.Printf("ldap: recovered panic in processMessages: %v", err) } for messageID, msgCtx := range l.messageContexts { // If we are closing due to an error, inform anyone who @@ -492,7 +491,7 @@ func (l *Conn) processMessages() { go func() { defer func() { if err := recover(); err != nil { - log.Printf("ldap: recovered panic in RequestTimeout: %v", err) + logger.Printf("ldap: recovered panic in RequestTimeout: %v", err) } }() time.Sleep(requestTimeout) @@ -508,7 +507,7 @@ func (l *Conn) processMessages() { if msgCtx, ok := l.messageContexts[message.MessageID]; ok { msgCtx.sendResponse(&PacketResponse{message.Packet, nil}) } else { - log.Printf("Received unexpected message %d, %v", message.MessageID, l.IsClosing()) + logger.Printf("Received unexpected message %d, %v", message.MessageID, l.IsClosing()) l.Debug.PrintPacket(message.Packet) } case MessageTimeout: @@ -535,7 +534,7 @@ func (l *Conn) reader() { cleanstop := false defer func() { if err := recover(); err != nil { - log.Printf("ldap: recovered panic in reader: %v", err) + logger.Printf("ldap: recovered panic in reader: %v", err) } if !cleanstop { l.Close() diff --git a/v3/debug.go b/v3/debug.go index d0a8fc15..74cc65a4 100644 --- a/v3/debug.go +++ b/v3/debug.go @@ -1,8 +1,6 @@ package ldap import ( - "log" - ber "github.com/go-asn1-ber/asn1-ber" ) @@ -18,13 +16,13 @@ func (debug *debugging) Enable(b bool) { // Printf writes debug output. func (debug debugging) Printf(format string, args ...interface{}) { if debug { - log.Printf(format, args...) + logger.Printf(format, args...) } } // PrintPacket dumps a packet. func (debug debugging) PrintPacket(packet *ber.Packet) { if debug { - ber.WritePacket(log.Writer(), packet) + ber.WritePacket(logger.Writer(), packet) } } diff --git a/v3/del.go b/v3/del.go index 6e987267..bac0dfb7 100644 --- a/v3/del.go +++ b/v3/del.go @@ -1,8 +1,6 @@ package ldap import ( - "log" - ber "github.com/go-asn1-ber/asn1-ber" ) @@ -53,7 +51,7 @@ func (l *Conn) Del(delRequest *DelRequest) error { return err } } else { - log.Printf("Unexpected Response: %d", packet.Children[1].Tag) + logger.Printf("Unexpected Response: %d", packet.Children[1].Tag) } return nil } diff --git a/v3/ldap.go b/v3/ldap.go index 7ae6dfe2..fb75a89c 100644 --- a/v3/ldap.go +++ b/v3/ldap.go @@ -3,6 +3,7 @@ package ldap import ( "fmt" "io/ioutil" + "log" "os" ber "github.com/go-asn1-ber/asn1-ber" @@ -82,6 +83,13 @@ var BeheraPasswordPolicyErrorMap = map[int8]string{ BeheraPasswordInHistory: "New password is in list of old passwords", } +var logger = log.New(os.Stderr, "", log.LstdFlags) + +// Logger allows clients to override the default logger +func Logger(l *log.Logger) { + logger = l +} + // Adds descriptions to an LDAP Response packet for debugging func addLDAPDescriptions(packet *ber.Packet) (err error) { defer func() { @@ -221,18 +229,18 @@ func addControlDescriptions(packet *ber.Packet) error { sequence := value.Children[0] for _, child := range sequence.Children { if child.Tag == 0 { - //Warning + // Warning warningPacket := child.Children[0] val, err := ber.ParseInt64(warningPacket.Data.Bytes()) if err != nil { return fmt.Errorf("failed to decode data bytes: %s", err) } if warningPacket.Tag == 0 { - //timeBeforeExpiration + // timeBeforeExpiration value.Description += " (TimeBeforeExpiration)" warningPacket.Value = val } else if warningPacket.Tag == 1 { - //graceAuthNsRemaining + // graceAuthNsRemaining value.Description += " (GraceAuthNsRemaining)" warningPacket.Value = val } diff --git a/v3/moddn.go b/v3/moddn.go index 71cdcd0b..ec246d1f 100644 --- a/v3/moddn.go +++ b/v3/moddn.go @@ -1,8 +1,6 @@ package ldap import ( - "log" - ber "github.com/go-asn1-ber/asn1-ber" ) @@ -42,7 +40,7 @@ func NewModifyDNRequest(dn string, rdn string, delOld bool, newSup string) *Modi // // Refer NewModifyDNRequest for other parameters func NewModifyDNWithControlsRequest(dn string, rdn string, delOld bool, - newSup string, controls []Control) *ModifyDNRequest { + newSup string, controls []Control) *ModifyDNRequest { return &ModifyDNRequest{ DN: dn, NewRDN: rdn, @@ -94,7 +92,7 @@ func (l *Conn) ModifyDN(m *ModifyDNRequest) error { return err } } else { - log.Printf("Unexpected Response: %d", packet.Children[1].Tag) + logger.Printf("Unexpected Response: %d", packet.Children[1].Tag) } return nil } diff --git a/v3/modify.go b/v3/modify.go index 1821413d..f6fe3859 100644 --- a/v3/modify.go +++ b/v3/modify.go @@ -2,7 +2,6 @@ package ldap import ( "errors" - "log" ber "github.com/go-asn1-ber/asn1-ber" ) @@ -127,7 +126,7 @@ func (l *Conn) Modify(modifyRequest *ModifyRequest) error { return err } } else { - log.Printf("Unexpected Response: %d", packet.Children[1].Tag) + logger.Printf("Unexpected Response: %d", packet.Children[1].Tag) } return nil }