-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
authenticate.go
76 lines (67 loc) · 2.35 KB
/
authenticate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package dovecot
import (
"encoding/base64"
"fmt"
"os"
"strings"
"github.com/vodolaz095/msmtpd"
)
// Authenticate performs authorization via AuthClientSocket of dovecot
func (d *Dovecot) Authenticate(tr *msmtpd.Transaction, user, passwd string) error {
if !isUsernameSafe(user) {
tr.LogWarn("user %s is considered unsafe for dovecot usage", user)
return permanentError
}
conn, err := d.dial("unix", d.PathToAuthClientSocket)
if err != nil {
tr.LogError(err, "while dialing address of dovecot's client socket")
return temporaryError
}
defer conn.Close()
tr.LogDebug("Dovecot responses seems sane on socket %s", d.PathToAuthClientSocket)
// Send our version, and then our PID.
err = write(conn, fmt.Sprintf("VERSION\t1\t1\nCPID\t%d\n", os.Getpid()))
if err != nil {
tr.LogError(err, "while receiving dovecot protocol version")
return temporaryError
}
// Read the server-side handshake. We don't care about the contents
// really, so just read all lines until we see the DONE.
for {
resp, readlineErr := conn.ReadLine()
if readlineErr != nil {
tr.LogError(err, "while receiving dovecot protocol version")
return temporaryError
}
if resp == "DONE" {
break
}
}
// We only support PLAIN authentication, so construct the request.
// Note we set the "secured" option, with the assumption that we got the
// password via d secure channel (like TLS).
// TODO: does dovecot handle utf8 domains well? do we need to encode them
// in IDNA first?
resp := base64.StdEncoding.EncodeToString(
[]byte(fmt.Sprintf("%s\x00%s\x00%s", user, user, passwd)))
err = write(conn, fmt.Sprintf(
"AUTH\t1\tPLAIN\tservice=smtp\tsecured\tno-penalty\tnologin\tresp=%s\n", resp))
if err != nil {
tr.LogError(err, "while writing auth request to dovecot")
return temporaryError
}
// Get the response, and we're done.
resp, err = conn.ReadLine()
if err != nil {
tr.LogError(err, "while receiving dovecot authentication response")
return temporaryError
} else if strings.HasPrefix(resp, "OK\t1") {
tr.LogInfo("Dovecot authorization passed for %s", user)
return nil
} else if strings.HasPrefix(resp, "FAIL\t1") {
tr.LogInfo("Dovecot authorization failed for %s", user)
return msmtpd.ErrAuthenticationCredentialsInvalid
}
tr.LogError(fmt.Errorf("invalid response: %q", resp), "while reading dovecot response for authentication")
return temporaryError
}