Bare minimum required commands to act as a reciever implemented
This commit is contained in:
parent
a213bfe34e
commit
542ef9e528
4 changed files with 168 additions and 19 deletions
|
@ -20,30 +20,65 @@ package smtp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
// "fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
connection net.Conn
|
connection net.Conn
|
||||||
|
|
||||||
|
ReversePathBuffer *string
|
||||||
|
ForwardPathBuffer *string
|
||||||
|
MailBuffer *string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Connection) Read() (string, error) {
|
func NewConnection(connection net.Conn) Connection {
|
||||||
buffer := [64]byte{}
|
return Connection{
|
||||||
read, err := self.connection.Read(buffer[:])
|
connection,
|
||||||
if err != nil {
|
nil,
|
||||||
return "", err
|
nil,
|
||||||
|
nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var buffer = [1024]byte{}
|
||||||
|
func (self Connection) Read(delimiter string) (string, error) {
|
||||||
|
var message string
|
||||||
|
for !strings.Contains(message, delimiter) {
|
||||||
|
num_read, err := self.connection.Read(buffer[:])
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
message += string(buffer[:num_read])
|
||||||
}
|
}
|
||||||
|
|
||||||
message := string(buffer[:read - 2])
|
lines := strings.Split(message, "\n")
|
||||||
|
for index, line := range lines {
|
||||||
|
if index != len(lines) - 1 || line != "" {
|
||||||
|
if index == 0 {
|
||||||
|
log.Trace().Msgf("%v -> %v", self.RemoteAddr(), line)
|
||||||
|
} else {
|
||||||
|
log.Trace().Msgf("%v %v", self.RemoteAddr(), line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Trace().Msgf("%v -> %v", self.RemoteAddr(), message)
|
return message[:len(message) - len(delimiter)], nil
|
||||||
|
|
||||||
return message, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Connection) Write(message string) error {
|
func (self Connection) Write(message string) error {
|
||||||
log.Trace().Msgf("%v <- %v", self.RemoteAddr(), message[:len(message) - 1])
|
lines := strings.Split(message, "\n")
|
||||||
|
for index, line := range lines {
|
||||||
|
if index != len(lines) - 1 || line != "" {
|
||||||
|
if index == 0 {
|
||||||
|
log.Trace().Msgf("%v <- %v", self.RemoteAddr(), line)
|
||||||
|
} else {
|
||||||
|
log.Trace().Msgf("%v %v", self.RemoteAddr(), line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_, err := self.connection.Write([]byte(message))
|
_, err := self.connection.Write([]byte(message))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ import (
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
name string
|
name string
|
||||||
Exec func(Connection)([]Command, error)
|
Exec func(Connection, string)([]Command, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Command) Name() string {
|
func (self Command) Name() string {
|
||||||
|
@ -42,7 +42,7 @@ func (self Connection) Chain() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
message, err := self.Read()
|
message, err := self.Read("\r\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ func (self Connection) Chain() error {
|
||||||
command_found := false
|
command_found := false
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
if command.Check(message) {
|
if command.Check(message) {
|
||||||
commands, err = command.Exec(self)
|
commands, err = command.Exec(self, message[len(command.Name()):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -80,24 +80,105 @@ func Greet(connection Connection) ([]Command, error) {
|
||||||
"220 localhost ESMTP diodemail -- Service ready" + "\n",
|
"220 localhost ESMTP diodemail -- Service ready" + "\n",
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return []Command{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return []Command{
|
return []Command{
|
||||||
Command{ "HELO", Hello },
|
HELO,
|
||||||
Command{ "EHLO", Hello },
|
EHLO,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --- HELO/EHLO RESPONSE --- */
|
/* --- HELO/EHLO RESPONSE --- */
|
||||||
|
|
||||||
func Hello(connection Connection) ([]Command, error) {
|
var HELO = Command{ "HELO", Hello }
|
||||||
|
var EHLO = Command{ "EHLO", Hello }
|
||||||
|
func Hello(connection Connection, message string) ([]Command, error) {
|
||||||
err := connection.Write(
|
err := connection.Write(
|
||||||
fmt.Sprintf("250 %v is shy", connection.connection.LocalAddr()) + "\n",
|
fmt.Sprintf(
|
||||||
|
"250 %v is shy" + "\r\n",
|
||||||
|
connection.connection.LocalAddr(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return []Command{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return []Command{
|
||||||
|
MAIL_FROM,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --- MAIL FROM RESPONSE --- */
|
||||||
|
|
||||||
|
var MAIL_FROM = Command{ "MAIL FROM:", MailFrom }
|
||||||
|
func MailFrom(connection Connection, message string) ([]Command, error) {
|
||||||
|
match := ReversePath.FindStringSubmatch(message)
|
||||||
|
if match == nil {
|
||||||
|
return []Command{}, fmt.Errorf("Invalid forward-path: %v", message)
|
||||||
|
} else if len(match) > 1 {
|
||||||
|
connection.ReversePathBuffer = &match[1]
|
||||||
|
}
|
||||||
|
err := connection.Write(
|
||||||
|
"250 OK\n",
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return []Command{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return []Command{
|
||||||
|
RCPT_TO,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var RCPT_TO = Command{ "RCPT TO:", RcptTo }
|
||||||
|
func RcptTo(connection Connection, message string) ([]Command, error) {
|
||||||
|
match := ForwardPath.FindStringSubmatch(message)
|
||||||
|
if match == nil {
|
||||||
|
return []Command{}, fmt.Errorf("Invalid forward-path: %v", message)
|
||||||
|
} else {
|
||||||
|
connection.ForwardPathBuffer = &match[1]
|
||||||
|
}
|
||||||
|
err := connection.Write(
|
||||||
|
"250 OK\n",
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return []Command{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return []Command{
|
||||||
|
DATA,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var DATA = Command { "DATA", Data }
|
||||||
|
func Data(connection Connection, message string) ([]Command, error) {
|
||||||
|
err := connection.Write(
|
||||||
|
"354 Start Input\n",
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return []Command{}, err
|
||||||
|
}
|
||||||
|
data, err := connection.Read("\r\n.\r\n")
|
||||||
|
connection.MailBuffer = &data
|
||||||
|
err = connection.Write(
|
||||||
|
"250 OK\n",
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return []Command{
|
||||||
|
QUIT,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var QUIT = Command { "QUIT", Quit }
|
||||||
|
func Quit(connection Connection, message string) ([]Command, error) {
|
||||||
|
err := connection.Write(
|
||||||
|
"221 OK\n",
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return []Command{}, nil
|
return []Command{}, nil
|
||||||
}
|
}
|
||||||
|
|
33
smtp/parsers.go
Normal file
33
smtp/parsers.go
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
/* diodemail - send-only smtp server
|
||||||
|
* Copyright (c) 2024 Gnarwhal
|
||||||
|
*
|
||||||
|
* This file is part of SSHare.
|
||||||
|
*
|
||||||
|
* SSHare 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.
|
||||||
|
*
|
||||||
|
* SSHare 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
|
||||||
|
* SSHare. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package smtp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ReversePath = regexp.MustCompile(fmt.Sprintf("(?:%v)|<>", path))
|
||||||
|
var ForwardPath = regexp.MustCompile(path)
|
||||||
|
|
||||||
|
// https://datatracker.ietf.org/doc/html/rfc5321#page-41
|
||||||
|
// Is this...legal, m'lord? (no, but ¯\_(ツ)_/¯)
|
||||||
|
// ...
|
||||||
|
// Ok fine. TODO. Happy now?
|
||||||
|
var path = "<(?:.*:)?(\\w+@\\w+(?:\\.\\w+)*)>"
|
|
@ -60,7 +60,7 @@ func Run(host string, implicit_tls bool) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
go handle(Connection{connection})
|
go handle(NewConnection(connection))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue