Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix use SMTP auth when port 25 is ban problem #16104

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ var SMTPAuths = []string{SMTPPlain, SMTPLogin}

// SMTPAuth performs an SMTP authentication.
func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error {
if cfg.TLS {
return SMTPAuthTLS(a,cfg)
}
c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
Copy link
Author

@jnan88 jnan88 Jun 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))

Only non TLS connection are used here

if err != nil {
return err
Expand Down Expand Up @@ -633,6 +636,26 @@ func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error {
return ErrUnsupportedLoginType
}

// SMTPAuthTLS SMTP authentication by TLS
func SMTPAuthTLS(a smtp.Auth, cfg *SMTPConfig) error {
addr :=fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
tlsClient, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify:cfg.SkipVerify})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tlsClient, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify:cfg.SkipVerify})
tlsClient, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify:cfg.SkipVerify})

New TLS connection

if err != nil {
log.Error("SMTPAuth error: %v", err)
return err
}
defer tlsClient.Close()
client,_ := smtp.NewClient(tlsClient, addr)
defer client.Close()
if err = client.Hello("gogs"); err != nil {
return err
}
if ok, _ := client.Extension("AUTH"); ok {
return client.Auth(a)
}
return ErrUnsupportedLoginType
}

// LoginViaSMTP queries if login/password is valid against the SMTP,
// and create a local user if success when enabled.
func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPConfig) (*User, error) {
Expand All @@ -648,7 +671,11 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC

var auth smtp.Auth
if cfg.Auth == SMTPPlain {
auth = smtp.PlainAuth("", login, password, cfg.Host)
if cfg.TLS {
auth = smtp.PlainAuth("", login, password, fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auth = smtp.PlainAuth("", login, password, fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
auth = smtp.PlainAuth("", login, password, fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))

When TLS connection is used, ports need to be added at the same time

}else {
auth = smtp.PlainAuth("", login, password, cfg.Host)
}
} else if cfg.Auth == SMTPLogin {
auth = &smtpLoginAuth{login, password}
} else {
Expand Down