| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/smtp" |
| 6 |
) |
| 7 |
|
| 8 |
// loginAuth implements the SASL LOGIN authentication mechanism for SMTP. |
| 9 |
// |
| 10 |
// Challenge prompt text from the server is ignored. |
| 11 |
// The username and password are sent in order as successive client responses. |
| 12 |
type loginAuth struct { |
| 13 |
// username is the SMTP account username sent as the first LOGIN response. |
| 14 |
username string |
| 15 |
// password is the SMTP account password sent as the second LOGIN response. |
| 16 |
password string |
| 17 |
// host is the expected SMTP server hostname checked in Start. |
| 18 |
host string |
| 19 |
// respStep tracks which LOGIN response to send next. |
| 20 |
respStep uint8 |
| 21 |
} |
| 22 |
|
| 23 |
// newLoginAuth returns an [smtp.Auth] that implements SASL LOGIN authentication. |
| 24 |
// |
| 25 |
// LOGIN is a multi-step mechanism commonly offered by servers that do not |
| 26 |
// support AUTH PLAIN: |
| 27 |
// 1. The client sends AUTH LOGIN. |
| 28 |
// 2. The server issues a challenge (often a username prompt) and the client sends the username. |
| 29 |
// 3. The server issues a challenge (often a password prompt) and the client sends the password. |
| 30 |
// |
| 31 |
// Challenge contents are ignored per draft-murchison-sasl-login-00 so that |
| 32 |
// common server variants (different wording, casing, or trailing NULs) work. |
| 33 |
// |
| 34 |
// Credentials are sent only when the connection uses TLS or the server name is |
| 35 |
// localhost. Otherwise authentication fails without transmitting credentials, |
| 36 |
// matching [smtp.PlainAuth]. |
| 37 |
// |
| 38 |
// Parameters: |
| 39 |
// - username: The SMTP account username. |
| 40 |
// - password: The SMTP account password. |
| 41 |
// - host: The expected SMTP server hostname, which must match [smtp.ServerInfo.Name] at [loginAuth.Start]. |
| 42 |
// |
| 43 |
// Returns: |
| 44 |
// - An [smtp.Auth] that performs LOGIN authentication for the given credentials and host. |
| 45 |
func newLoginAuth(username, password, host string) smtp.Auth { |
| 46 |
return &loginAuth{ |
| 47 |
username: username, |
| 48 |
password: password, |
| 49 |
host: host, |
| 50 |
respStep: 0, |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// isLocalhost reports whether name is a loopback host identity. |
| 55 |
// |
| 56 |
// Parameters: |
| 57 |
// - name: The server name to check (for example from [smtp.ServerInfo.Name]). |
| 58 |
// |
| 59 |
// Returns: |
| 60 |
// - true if name is "localhost", "127.0.0.1", or "::1"; otherwise, false. |
| 61 |
func isLocalhost(name string) bool { |
| 62 |
return name == "localhost" || name == "127.0.0.1" || name == "::1" |
| 63 |
} |
| 64 |
|
| 65 |
// Next continues the LOGIN challenge-response exchange. |
| 66 |
// |
| 67 |
// When more is true, the next credential is returned by step order (username, |
| 68 |
// then password). Challenge bytes from the server are ignored. An extra |
| 69 |
// challenge after the password step returns an error so net/smtp aborts AUTH |
| 70 |
// instead of sending a dummy client response. |
| 71 |
// |
| 72 |
// Parameters: |
| 73 |
// - fromServer: The decoded server challenge payload (ignored for LOGIN). |
| 74 |
// - more: Whether the server expects another client response (SMTP 334). |
| 75 |
// |
| 76 |
// Returns: |
| 77 |
// - The next client response (username or password bytes), or nil when the exchange is finished. |
| 78 |
// - A non-nil error if the server issues an unexpected additional challenge. |
| 79 |
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { |
| 80 |
if !more { |
| 81 |
return nil, nil |
| 82 |
} |
| 83 |
|
| 84 |
// Answer by step order; do not parse challenge text. |
| 85 |
switch a.respStep { |
| 86 |
case 0: |
| 87 |
a.respStep++ |
| 88 |
|
| 89 |
return []byte(a.username), nil |
| 90 |
case 1: |
| 91 |
a.respStep++ |
| 92 |
|
| 93 |
return []byte(a.password), nil |
| 94 |
default: |
| 95 |
return nil, fmt.Errorf("%w: %q", errUnexpectedServerChallenge, fromServer) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// Start begins LOGIN authentication after validating TLS and host identity. |
| 100 |
// |
| 101 |
// Parameters: |
| 102 |
// - server: Connection metadata from the SMTP client, including Name and TLS ([smtp.ServerInfo]). |
| 103 |
// |
| 104 |
// Returns: |
| 105 |
// - The authentication protocol name ("LOGIN"). |
| 106 |
// - An empty initial client response (LOGIN has no initial SASL payload). |
| 107 |
// - A non-nil error if the connection is unencrypted to a non-local host or the host name does not match. |
| 108 |
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) { |
| 109 |
// Require TLS unless the server name is localhost. Without TLS, [smtp.ServerInfo] |
| 110 |
// cannot be trusted and an attacker could advertise LOGIN to harvest credentials. |
| 111 |
if !server.TLS && !isLocalhost(server.Name) { |
| 112 |
return "", nil, errUnencryptedConnection |
| 113 |
} |
| 114 |
|
| 115 |
if server.Name != a.host { |
| 116 |
return "", nil, errWrongHostName |
| 117 |
} |
| 118 |
|
| 119 |
a.respStep = 0 |
| 120 |
|
| 121 |
return "LOGIN", nil, nil |
| 122 |
} |