Skip to content

Commit

Permalink
add Conn.LocalAddr and Conn.RemoteAddr methods
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinz01 committed Apr 16, 2024
1 parent ce4fd5c commit 5610b15
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 10 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ type Conn struct {
MessageCache map[MessageID]any
}

// Returns the local address of the connection
func (c *Conn) LocalAddr() net.Addr {
return c.Conn.LocalAddr()
}

// Returns the remote address of the connection
func (c *Conn) RemoteAddr() net.Addr {
return c.Conn.RemoteAddr()
}

// Closes the underlying connection and stops reading messages.
func (c *Conn) Close() {
c.Conn.Close()
Expand Down
14 changes: 7 additions & 7 deletions test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,34 @@ func (t *TestHandler) Bind(conn *ldapserver.Conn, msg *ldapserver.Message, req *
start := time.Now()
switch req.AuthType {
case ldapserver.AuthenticationTypeSimple:
log.Printf("Simple authentication from %s for \"%s\"\n", conn.Conn.RemoteAddr(), req.Name)
log.Printf("Simple authentication from %s for \"%s\"\n", conn.RemoteAddr(), req.Name)
if t.checkPassword(dn, req.Credentials.(string)) {
log.Printf("Successful Bind from %s for \"%s\"\n", conn.Conn.RemoteAddr(), dn)
log.Printf("Successful Bind from %s for \"%s\"\n", conn.RemoteAddr(), dn)
conn.Authentication = dn
res.ResultCode = ldapserver.ResultSuccess
} else {
log.Printf("Invalid credentials from %s for \"%s\"\n", conn.Conn.RemoteAddr(), dn)
log.Printf("Invalid credentials from %s for \"%s\"\n", conn.RemoteAddr(), dn)
conn.Authentication = nil
res.ResultCode = ldapserver.ResultInvalidCredentials
}
case ldapserver.AuthenticationTypeSASL:
creds := req.Credentials.(*ldapserver.SASLCredentials)
log.Printf("SASL authentication from %s for \"%s\" using mechanism %s", conn.Conn.RemoteAddr(), dn, creds.Mechanism)
log.Printf("SASL authentication from %s for \"%s\" using mechanism %s", conn.RemoteAddr(), dn, creds.Mechanism)
switch creds.Mechanism {
case "CRAM-MD5":
// Put verification code in here
log.Printf("CRAM-MD5 authentication from %s for \"%s\"\n", conn.Conn.RemoteAddr(), dn)
log.Printf("CRAM-MD5 authentication from %s for \"%s\"\n", conn.RemoteAddr(), dn)
conn.Authentication = nil
res.ResultCode = ldapserver.ResultAuthMethodNotSupported
res.DiagnosticMessage = "the CRAM-MD5 authentication method is not supported"
default:
log.Printf("Unsupported SASL mechanism from %s for \"%s\"\n", conn.Conn.RemoteAddr(), dn)
log.Printf("Unsupported SASL mechanism from %s for \"%s\"\n", conn.RemoteAddr(), dn)
conn.Authentication = nil
res.ResultCode = ldapserver.ResultAuthMethodNotSupported
res.DiagnosticMessage = "the SASL authentication method requested is not supported"
}
default:
log.Printf("Unsupported authentication method from %s for \"%s\"\n", conn.Conn.RemoteAddr(), dn)
log.Printf("Unsupported authentication method from %s for \"%s\"\n", conn.RemoteAddr(), dn)
res.ResultCode = ldapserver.ResultAuthMethodNotSupported
res.DiagnosticMessage = "the authentication method requested is not supported by this server"
}
Expand Down

0 comments on commit 5610b15

Please sign in to comment.