Skip to content

Commit dd39b35

Browse files
albertonyemersion
authored andcommitted
Fix anonymous authentication with empty trace information string
With anonymous authentication according to RFC4505 the trace information string is optional, and SMTP authentication extension described in RFC4954 states that: If the client is transmitting an initial response of zero length, it MUST instead transmit the response as a single equals sign ("="). This indicates that the response is present, but contains no data.
1 parent ff8f376 commit dd39b35

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,13 @@ func (c *Client) Auth(a sasl.Client) error {
356356
if err != nil {
357357
return err
358358
}
359-
resp64 := make([]byte, encoding.EncodedLen(len(resp)))
360-
encoding.Encode(resp64, resp)
359+
var resp64 []byte
360+
if len(resp) > 0 {
361+
resp64 = make([]byte, encoding.EncodedLen(len(resp)))
362+
encoding.Encode(resp64, resp)
363+
} else if resp != nil {
364+
resp64 = []byte{'='}
365+
}
361366
code, msg64, err := c.cmd(0, strings.TrimSpace(fmt.Sprintf("AUTH %s %s", mech, resp64)))
362367
for err == nil {
363368
var msg []byte

conn.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,15 @@ func (c *Conn) handleAuth(arg string) {
773773
// Parse client initial response if there is one
774774
var ir []byte
775775
if len(parts) > 1 {
776-
var err error
777-
ir, err = base64.StdEncoding.DecodeString(parts[1])
778-
if err != nil {
779-
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
780-
return
776+
if parts[1] == "=" {
777+
ir = []byte{}
778+
} else {
779+
var err error
780+
ir, err = base64.StdEncoding.DecodeString(parts[1])
781+
if err != nil {
782+
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
783+
return
784+
}
781785
}
782786
}
783787

@@ -816,10 +820,14 @@ func (c *Conn) handleAuth(arg string) {
816820
return
817821
}
818822

819-
response, err = base64.StdEncoding.DecodeString(encoded)
820-
if err != nil {
821-
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
822-
return
823+
if encoded == "=" {
824+
response = []byte{}
825+
} else {
826+
response, err = base64.StdEncoding.DecodeString(encoded)
827+
if err != nil {
828+
c.writeResponse(454, EnhancedCode{4, 7, 0}, "Invalid base64 data")
829+
return
830+
}
823831
}
824832
}
825833

0 commit comments

Comments
 (0)