| 1 | package smtpscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src . errors/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 6 | "net/smtpscip-go gomod github.com/golang/go/src . `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 loginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth# struct { |
| 14 | usernamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#username., passwordscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#password. string |
| 15 | hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#host. string |
| 16 | respStepscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#respStep. uint8 |
| 17 | } |
| 18 | |
| 19 | var ( |
| 20 | // errUnencryptedConnection is returned when authentication is attempted |
| 21 | // on a non-local server without TLS. |
| 22 | errUnencryptedConnectionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/errUnencryptedConnection. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("unencrypted connection") |
| 23 | |
| 24 | // errWrongHostName is returned when the server name does not match the |
| 25 | // expected host. |
| 26 | errWrongHostNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/errWrongHostName. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . 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 | errUnexpectedServerChallengescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/errUnexpectedServerChallenge. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . 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 LoginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/LoginAuth().(username, password, host string) smtpscip-go gomod github.com/golang/go/src . `net/smtp`/.Authscip-go gomod github.com/golang/go/src . `net/smtp`/Auth# { |
| 56 | return &loginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#{ |
| 57 | usernamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#username.: username, |
| 58 | passwordscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#password.: password, |
| 59 | hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#host.: host, |
| 60 | respStepscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#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 isLocalhostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/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 *loginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#) Nextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/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.respStepscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#respStep. { |
| 96 | case 0: |
| 97 | a.respStepscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#respStep.++ |
| 98 | |
| 99 | return []byte(a.usernamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#username.), nil |
| 100 | case 1: |
| 101 | a.respStepscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#respStep.++ |
| 102 | |
| 103 | return []byte(a.passwordscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#password.), nil |
| 104 | default: |
| 105 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", errUnexpectedServerChallengescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/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 *loginAuthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#) Startscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#Start().(server *smtpscip-go gomod github.com/golang/go/src . `net/smtp`/.ServerInfoscip-go gomod github.com/golang/go/src . `net/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.TLSscip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#TLS. && !isLocalhostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/isLocalhost().(server.Namescip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#Name.) { |
| 122 | return "", nil, errUnencryptedConnectionscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/errUnencryptedConnection. |
| 123 | } |
| 124 | |
| 125 | if server.Namescip-go gomod github.com/golang/go/src . `net/smtp`/ServerInfo#Name. != a.hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#host. { |
| 126 | return "", nil, errWrongHostNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/errWrongHostName. |
| 127 | } |
| 128 | |
| 129 | a.respStepscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/email/smtp`/loginAuth#respStep. = 0 |
| 130 | |
| 131 | return "LOGIN", nil, nil |
| 132 | } |