Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for configurable logger #366

Merged
merged 4 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions add.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ldap

import (
"log"

ber "github.com/go-asn1-ber/asn1-ber"
)

Expand Down Expand Up @@ -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
}
11 changes: 5 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/tls"
"errors"
"fmt"
"log"
"net"
"net/url"
"sync"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions debug.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ldap

import (
"log"

ber "github.com/go-asn1-ber/asn1-ber"
)

Expand All @@ -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)
}
}
4 changes: 1 addition & 3 deletions del.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ldap

import (
"log"

ber "github.com/go-asn1-ber/asn1-ber"
)

Expand Down Expand Up @@ -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
}
14 changes: 11 additions & 3 deletions ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ldap
import (
"fmt"
"io/ioutil"
"log"
"os"

ber "github.com/go-asn1-ber/asn1-ber"
Expand Down Expand Up @@ -82,6 +83,13 @@ var BeheraPasswordPolicyErrorMap = map[int8]string{
BeheraPasswordInHistory: "New password is in list of old passwords",
}

var logger = log.Default()
n3integration marked this conversation as resolved.
Show resolved Hide resolved

// 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() {
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 2 additions & 4 deletions moddn.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ldap

import (
"log"

ber "github.com/go-asn1-ber/asn1-ber"
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ldap

import (
"errors"
"log"

ber "github.com/go-asn1-ber/asn1-ber"
)
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions v3/add.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ldap

import (
"log"

ber "github.com/go-asn1-ber/asn1-ber"
)

Expand Down Expand Up @@ -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
}
11 changes: 5 additions & 6 deletions v3/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/tls"
"errors"
"fmt"
"log"
"net"
"net/url"
"sync"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions v3/debug.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ldap

import (
"log"

ber "github.com/go-asn1-ber/asn1-ber"
)

Expand All @@ -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)
}
}
4 changes: 1 addition & 3 deletions v3/del.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ldap

import (
"log"

ber "github.com/go-asn1-ber/asn1-ber"
)

Expand Down Expand Up @@ -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
}
14 changes: 11 additions & 3 deletions v3/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ldap
import (
"fmt"
"io/ioutil"
"log"
"os"

ber "github.com/go-asn1-ber/asn1-ber"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 2 additions & 4 deletions v3/moddn.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ldap

import (
"log"

ber "github.com/go-asn1-ber/asn1-ber"
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}