diodemail/smtp/session.go

226 lines
5.2 KiB
Go
Raw Normal View History

/* diodemail - send-only smtp server
* Copyright (c) 2024 Gnarwhal
*
2024-10-03 15:12:20 +00:00
* This file is part of diodemail.
*
2024-10-03 15:12:20 +00:00
* 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.
*
2024-10-03 15:12:20 +00:00
* 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
2024-10-03 15:12:20 +00:00
* diodemail. If not, see <https://www.gnu.org/licenses/>.
*/
package smtp
import (
"net"
2024-10-02 00:28:14 +00:00
"net/smtp"
2024-09-29 21:51:03 +00:00
"fmt"
"strings"
2024-10-02 00:28:14 +00:00
"crypto/tls"
"crypto/sha256"
"github.com/rs/zerolog/log"
)
2024-09-29 21:51:03 +00:00
type SMTPSession struct {
connection net.Conn
host string
password_hash string
tls_config tls.Config
buffer [4096]byte
HasHelloed bool
requires_auth bool
is_authed bool
ReversePathBuffer *string
2024-09-29 18:24:06 +00:00
ForwardPathBuffer []string
}
func MakeSMTPSession(connection net.Conn, host string, tls_config tls.Config, auth bool, password_hash string) SMTPSession {
2024-09-29 21:51:03 +00:00
return SMTPSession{
connection: connection,
host: host,
password_hash: password_hash,
requires_auth: auth,
}
}
2024-10-02 00:28:14 +00:00
func (self *SMTPSession) Run() error {
2024-09-29 21:51:03 +00:00
err := Greet(self)
if err != nil {
return err
}
COMMANDS := []Command{
Command{ "HELO", Helo },
Command{ "EHLO", Ehlo },
Command{ "AUTH", Auth },
2024-09-29 21:51:03 +00:00
Command{ "MAIL FROM:", MailFrom },
Command{ "RCPT TO:", RcptTo },
Command{ "DATA", Data },
Command{ "QUIT", Quit },
Command{ "NOOP", Noop },
Command{ "RSET", Rset },
}
quit := false
for !quit {
message, err := self.ReadUntil("\n")
if err != nil {
2024-09-29 21:51:03 +00:00
return err
}
command_found := false
for _, command := range COMMANDS {
if command.Check(message) {
command_found = true
2024-10-02 00:28:14 +00:00
quit, err = command.Exec(self, message[len(command.GetName()):])
2024-09-29 21:51:03 +00:00
if err != nil {
2024-10-02 00:28:14 +00:00
if quit {
return err
} else {
log.Error().Msgf("%v: %v", self.connection.RemoteAddr(), err)
}
2024-09-29 21:51:03 +00:00
}
break
}
}
if !command_found {
self.Write("500 Unrecognized Command\n")
}
}
2024-09-29 21:51:03 +00:00
return nil
}
2024-10-02 00:28:14 +00:00
func (self *SMTPSession) TraceSession(direction string, message string) {
lines := strings.Split(message, "\n")
for index, line := range lines {
if index != len(lines) - 1 || line != "" {
if index == 0 {
2024-09-29 21:51:03 +00:00
log.Trace().Msgf("%v %v %v", self.connection.RemoteAddr(), direction, line)
} else {
2024-10-02 00:28:14 +00:00
log.Trace().Msgf("%v %v", self.connection.RemoteAddr(), line)
}
}
}
}
func (self *SMTPSession) ReadUntil(delimiter string) (string, error) {
2024-09-29 21:51:03 +00:00
var message string
for !strings.Contains(message, delimiter) {
2024-10-02 05:54:34 +00:00
num_read, err := self.connection.Read(self.buffer[:])
2024-09-29 21:51:03 +00:00
if err != nil {
return "", err
}
2024-10-02 05:54:34 +00:00
message += string(self.buffer[:num_read])
}
2024-09-29 21:51:03 +00:00
self.TraceSession(" ->", message)
return message[:len(message) - len(delimiter)], nil
}
func (self *SMTPSession) ReadCount(num_bytes int) ([]byte, error) {
bytes := make([]byte, num_bytes)
_, err := self.connection.Read(bytes[:])
return bytes, err
}
2024-10-02 00:28:14 +00:00
func (self *SMTPSession) Write(message string) error {
2024-09-29 21:51:03 +00:00
self.TraceSession("<- ", message)
_, err := self.connection.Write([]byte(message))
return err
}
2024-10-02 00:28:14 +00:00
func (self *SMTPSession) GetHost() string {
return self.host
2024-09-29 18:24:06 +00:00
}
func (self *SMTPSession) RequiresAuthentication() bool {
return self.requires_auth
}
func (self *SMTPSession) AuthenticationSatisfied() bool {
fmt.Println(self.requires_auth, self.is_authed)
return !self.requires_auth || self.is_authed
}
func (self *SMTPSession) ValidatePassword(password string) bool {
return fmt.Sprintf("%x", sha256.Sum256([]byte(password))) == self.password_hash
}
2024-10-02 00:28:14 +00:00
func (self *SMTPSession) MailFrom(from string) {
2024-09-29 21:51:03 +00:00
self.Reset()
self.ReversePathBuffer = &from
}
2024-10-02 00:28:14 +00:00
func (self *SMTPSession) AddRecipient(recipient string) {
2024-09-29 21:51:03 +00:00
self.ForwardPathBuffer = append(self.ForwardPathBuffer, recipient)
2024-09-29 18:24:06 +00:00
}
2024-10-02 00:28:14 +00:00
func (self *SMTPSession) SendMail(data string) error {
mx_hostname := Domain.FindStringSubmatch(self.ForwardPathBuffer[0])[1]
mx, err := net.LookupMX(mx_hostname)
if err != nil {
return err
}
var smtp_hostname string
if len(mx) == 0 {
2024-10-02 19:11:01 +00:00
smtp_hostname = mx_hostname
2024-10-02 00:28:14 +00:00
} else {
smtp_hostname = mx[0].Host
}
smtp_client, err := smtp.Dial(fmt.Sprintf("%v:25", smtp_hostname))
2024-10-02 00:28:14 +00:00
if err != nil {
return err
}
defer smtp_client.Close()
err = smtp_client.Hello(self.host)
if err != nil {
return err
}
tls_config := self.tls_config
tls_config.ServerName = smtp_hostname
err = smtp_client.StartTLS(&tls_config)
if err != nil {
return err
}
2024-10-02 00:28:14 +00:00
err = smtp_client.Mail(*self.ReversePathBuffer)
if err != nil {
return err
}
err = smtp_client.Rcpt(self.ForwardPathBuffer[0])
if err != nil {
return err
}
writer, err := smtp_client.Data()
if err != nil {
return err
}
_, err = writer.Write([]byte(data))
if err != nil {
return err
}
err = writer.Close()
if err != nil {
return err
}
err = smtp_client.Quit()
if err != nil {
return err
}
return nil
}
2024-10-02 00:28:14 +00:00
func (self *SMTPSession) Reset() {
2024-09-29 21:51:03 +00:00
self.ReversePathBuffer = nil
self.ForwardPathBuffer = []string{}
}