diodemail/smtp/commands.go

288 lines
5.7 KiB
Go
Raw Permalink Normal View History

2024-09-29 21:51:03 +00:00
/* diodemail - send-only smtp server
* Copyright (c) 2024 Gnarwhal
*
2024-10-03 15:12:20 +00:00
* This file is part of diodemail.
2024-09-29 21:51:03 +00:00
*
2024-10-03 15:12:20 +00:00
* diodemail is free software: you can redistribute it and/or modify it under the terms of
2024-09-29 21:51:03 +00:00
* 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.
*
2024-10-03 15:12:20 +00:00
* diodemail is distributed in the hope that it will be useful, but WITHOUT ANY
2024-09-29 21:51:03 +00:00
* 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
2024-10-03 15:12:20 +00:00
* diodemail. If not, see <https://www.gnu.org/licenses/>.
2024-09-29 21:51:03 +00:00
*/
package smtp
import (
"fmt"
"strings"
)
type Command struct {
name string
2024-10-02 00:28:14 +00:00
Exec func(*SMTPSession, string)(bool, error)
2024-09-29 21:51:03 +00:00
}
2024-10-02 00:28:14 +00:00
func (self Command) GetName() string {
2024-09-29 21:51:03 +00:00
return self.name
}
func (self Command) Check(message string) bool {
return strings.HasPrefix(message, self.name)
}
2024-10-02 00:28:14 +00:00
func Greet(smtp_session *SMTPSession) error {
2024-09-29 21:51:03 +00:00
err := smtp_session.Write(
2024-10-02 00:28:14 +00:00
fmt.Sprintf(
"220 %v ESMTP diodemail -- Service ready" + "\n",
smtp_session.GetHost(),
),
2024-09-29 21:51:03 +00:00
)
if err != nil {
return err
}
return nil
}
2024-10-02 00:28:14 +00:00
func Helo(smtp_session *SMTPSession, message string) (bool, error) {
2024-09-29 21:51:03 +00:00
err := smtp_session.Write(
fmt.Sprintf(
"250 %v is shy" + "\r\n",
2024-10-02 00:28:14 +00:00
smtp_session.GetHost(),
2024-09-29 21:51:03 +00:00
),
)
if err != nil {
2024-10-02 00:28:14 +00:00
return true, err
2024-09-29 21:51:03 +00:00
}
smtp_session.HasHelloed = true
2024-09-29 21:51:03 +00:00
return false, nil
}
2024-10-02 00:28:14 +00:00
func Ehlo(smtp_session *SMTPSession, message string) (bool, error) {
enable_auth := ""
if smtp_session.RequiresAuthentication() {
enable_auth = "250-AUTH PLAIN\r\n"
}
2024-09-29 21:51:03 +00:00
err := smtp_session.Write(
fmt.Sprintf(
"250-%v is shy" + "\r\n" +
"%v" +
"250-8BITMIME" + "\r\n" +
"250 SMTPUTF8" + "\r\n",
2024-10-02 00:28:14 +00:00
smtp_session.GetHost(),
enable_auth,
2024-09-29 21:51:03 +00:00
),
)
if err != nil {
2024-10-02 00:28:14 +00:00
return true, err
2024-09-29 21:51:03 +00:00
}
smtp_session.HasHelloed = true
2024-09-29 21:51:03 +00:00
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
}
2024-10-02 00:28:14 +00:00
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
}
2024-09-29 21:51:03 +00:00
match := ReversePath.FindStringSubmatch(message)
if match == nil {
2024-10-02 00:28:14 +00:00
err := smtp_session.Write(
"501 Could not parse reverse-path\n",
2024-09-29 21:51:03 +00:00
)
2024-10-02 00:28:14 +00:00
if err != nil {
return true, err
}
2024-09-29 21:51:03 +00:00
} else if len(match) > 1 {
smtp_session.MailFrom(match[1])
err := smtp_session.Write(
"250 OK\n",
)
if err != nil {
2024-10-02 00:28:14 +00:00
return true, err
2024-09-29 21:51:03 +00:00
}
}
return false, nil
}
2024-10-02 00:28:14 +00:00
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
}
2024-09-29 21:51:03 +00:00
match := ForwardPath.FindStringSubmatch(message)
if match == nil {
2024-10-02 00:28:14 +00:00
err := smtp_session.Write(
"501 Could not parse forward-path\n",
2024-09-29 21:51:03 +00:00
)
2024-10-02 00:28:14 +00:00
if err != nil {
return true, err
}
2024-09-29 21:51:03 +00:00
} else {
smtp_session.AddRecipient(match[1])
err := smtp_session.Write(
"250 OK\n",
)
if err != nil {
2024-10-02 00:28:14 +00:00
return true, err
2024-09-29 21:51:03 +00:00
}
}
return false, nil
}
2024-10-02 00:28:14 +00:00
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
}
2024-09-29 21:51:03 +00:00
err := smtp_session.Write(
"354 Start Input\n",
)
if err != nil {
2024-10-02 00:28:14 +00:00
return true, err
2024-09-29 21:51:03 +00:00
}
data, err := smtp_session.ReadUntil("\r\n.\r\n")
2024-10-02 00:28:14 +00:00
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()
2024-09-29 21:51:03 +00:00
err = smtp_session.Write(
"250 OK\n",
)
if err != nil {
2024-10-02 00:28:14 +00:00
return true, err
2024-09-29 21:51:03 +00:00
}
return false, nil
}
2024-10-02 00:28:14 +00:00
func Quit(smtp_session *SMTPSession, message string) (bool, error) {
2024-09-29 21:51:03 +00:00
err := smtp_session.Write(
"221 Goodbye :)\n",
)
if err != nil {
return true, err
}
return true, nil
}
2024-10-02 00:28:14 +00:00
func Noop(smtp_session *SMTPSession, message string) (bool, error) {
2024-09-29 21:51:03 +00:00
err := smtp_session.Write(
"250 OK\n",
)
if err != nil {
2024-10-02 00:28:14 +00:00
return true, err
2024-09-29 21:51:03 +00:00
}
return false, nil
}
2024-10-02 00:28:14 +00:00
func Rset(smtp_session *SMTPSession, message string) (bool, error) {
smtp_session.Reset()
2024-09-29 21:51:03 +00:00
err := smtp_session.Write(
"250 Reset\n",
)
if err != nil {
2024-10-02 00:28:14 +00:00
return true, err
2024-09-29 21:51:03 +00:00
}
return false, nil
}