287 lines
5.7 KiB
Go
287 lines
5.7 KiB
Go
/* diodemail - send-only smtp server
|
|
* Copyright (c) 2024 Gnarwhal
|
|
*
|
|
* This file is part of diodemail.
|
|
*
|
|
* diodemail is free software: you can redistribute it and/or modify it under the terms of
|
|
* the GNU General Public License as published by the Free Software Foundation,
|
|
* either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* diodemail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* diodemail. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package smtp
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Command struct {
|
|
name string
|
|
Exec func(*SMTPSession, string)(bool, error)
|
|
}
|
|
|
|
func (self Command) GetName() string {
|
|
return self.name
|
|
}
|
|
|
|
func (self Command) Check(message string) bool {
|
|
return strings.HasPrefix(message, self.name)
|
|
}
|
|
|
|
func Greet(smtp_session *SMTPSession) error {
|
|
err := smtp_session.Write(
|
|
fmt.Sprintf(
|
|
"220 %v ESMTP diodemail -- Service ready" + "\n",
|
|
smtp_session.GetHost(),
|
|
),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func Helo(smtp_session *SMTPSession, message string) (bool, error) {
|
|
err := smtp_session.Write(
|
|
fmt.Sprintf(
|
|
"250 %v is shy" + "\r\n",
|
|
smtp_session.GetHost(),
|
|
),
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
|
|
smtp_session.HasHelloed = true
|
|
return false, nil
|
|
}
|
|
|
|
func Ehlo(smtp_session *SMTPSession, message string) (bool, error) {
|
|
enable_auth := ""
|
|
if smtp_session.RequiresAuthentication() {
|
|
enable_auth = "250-AUTH PLAIN\r\n"
|
|
}
|
|
err := smtp_session.Write(
|
|
fmt.Sprintf(
|
|
"250-%v is shy" + "\r\n" +
|
|
"%v" +
|
|
"250-8BITMIME" + "\r\n" +
|
|
"250 SMTPUTF8" + "\r\n",
|
|
smtp_session.GetHost(),
|
|
enable_auth,
|
|
),
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
|
|
smtp_session.HasHelloed = true
|
|
return false, nil
|
|
}
|
|
|
|
func Auth(smtp_session *SMTPSession, message string) (bool, error) {
|
|
if !smtp_session.HasHelloed {
|
|
err := smtp_session.Write(
|
|
"503 Must HELO/EHLO first\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
return false, nil
|
|
}
|
|
parts := strings.Split(message, " ")[1:]
|
|
if parts[0] == "PLAIN" {
|
|
var password string
|
|
if len(parts) > 1 {
|
|
password = parts[1][:len(parts[1]) - 1]
|
|
} else {
|
|
err := smtp_session.Write("334 \n")
|
|
if err != nil {
|
|
return true, nil
|
|
}
|
|
password, err = smtp_session.ReadUntil("\r\n")
|
|
if err != nil {
|
|
return true, nil
|
|
}
|
|
}
|
|
if smtp_session.ValidatePassword(password) {
|
|
smtp_session.Write("235 Authentication successful\n")
|
|
} else {
|
|
smtp_session.Write("535 Authentication credentials invalid\n")
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func MailFrom(smtp_session *SMTPSession, message string) (bool, error) {
|
|
if !smtp_session.HasHelloed {
|
|
err := smtp_session.Write(
|
|
"503 Must HELO/EHLO first\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
return false, nil
|
|
}
|
|
if !smtp_session.AuthenticationSatisfied() {
|
|
err := smtp_session.Write(
|
|
"530 Authentication required\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
return false, nil
|
|
}
|
|
match := ReversePath.FindStringSubmatch(message)
|
|
if match == nil {
|
|
err := smtp_session.Write(
|
|
"501 Could not parse reverse-path\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
} else if len(match) > 1 {
|
|
smtp_session.MailFrom(match[1])
|
|
err := smtp_session.Write(
|
|
"250 OK\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
func RcptTo(smtp_session *SMTPSession, message string) (bool, error) {
|
|
if !smtp_session.HasHelloed {
|
|
err := smtp_session.Write(
|
|
"503 Must HELO/EHLO first\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
return false, nil
|
|
}
|
|
if !smtp_session.AuthenticationSatisfied() {
|
|
err := smtp_session.Write(
|
|
"530 Authentication required\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
return false, nil
|
|
}
|
|
match := ForwardPath.FindStringSubmatch(message)
|
|
if match == nil {
|
|
err := smtp_session.Write(
|
|
"501 Could not parse forward-path\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
} else {
|
|
smtp_session.AddRecipient(match[1])
|
|
err := smtp_session.Write(
|
|
"250 OK\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
func Data(smtp_session *SMTPSession, message string) (bool, error) {
|
|
if !smtp_session.HasHelloed {
|
|
err := smtp_session.Write(
|
|
"503 Must HELO/EHLO first\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
return false, nil
|
|
}
|
|
if !smtp_session.AuthenticationSatisfied() {
|
|
err := smtp_session.Write(
|
|
"530 Authentication required\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
return false, nil
|
|
}
|
|
err := smtp_session.Write(
|
|
"354 Start Input\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
data, err := smtp_session.ReadUntil("\r\n.\r\n")
|
|
if err != nil {
|
|
smtp_session.Write(
|
|
"550 Action not taken\n",
|
|
)
|
|
return false, err
|
|
}
|
|
err = smtp_session.SendMail(data)
|
|
if err != nil {
|
|
smtp_session.Write(
|
|
"550 Action not taken\n",
|
|
)
|
|
return false, err
|
|
}
|
|
smtp_session.Reset()
|
|
err = smtp_session.Write(
|
|
"250 OK\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
func Quit(smtp_session *SMTPSession, message string) (bool, error) {
|
|
err := smtp_session.Write(
|
|
"221 Goodbye :)\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func Noop(smtp_session *SMTPSession, message string) (bool, error) {
|
|
err := smtp_session.Write(
|
|
"250 OK\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
func Rset(smtp_session *SMTPSession, message string) (bool, error) {
|
|
smtp_session.Reset()
|
|
err := smtp_session.Write(
|
|
"250 Reset\n",
|
|
)
|
|
if err != nil {
|
|
return true, err
|
|
}
|
|
|
|
return false, nil
|
|
}
|