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