From a213bfe34eff473c7ffa378ed32babeacf879747 Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Sat, 28 Sep 2024 20:54:07 +0000 Subject: [PATCH] Initial server setup and performs initial greeting and hello --- cmd/server/main.go | 32 ++++++++++++++ go.mod | 8 ++++ go.sum | 15 +++++++ smtp/connection.go | 57 +++++++++++++++++++++++++ smtp/handlers.go | 103 +++++++++++++++++++++++++++++++++++++++++++++ smtp/server.go | 69 ++++++++++++++++++++++++++++++ 6 files changed, 284 insertions(+) create mode 100644 cmd/server/main.go create mode 100644 go.sum create mode 100644 smtp/connection.go create mode 100644 smtp/handlers.go create mode 100644 smtp/server.go diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..c66fe75 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,32 @@ +/* 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 . + */ + +package main + +import ( + "log" + + "forge.monodon.me/Gnarwhal/diodemail/smtp" +) + +func main() { + err := smtp.Run(":4650", false) + if err != nil { + log.Fatal(err) + } +} diff --git a/go.mod b/go.mod index 55c51c9..6cdcd65 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module forge.monodon.me/Gnarwhal/diodemail go 1.23.1 + +require github.com/rs/zerolog v1.33.0 + +require ( + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + golang.org/x/sys v0.12.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..98afda4 --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= +github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/smtp/connection.go b/smtp/connection.go new file mode 100644 index 0000000..a185b84 --- /dev/null +++ b/smtp/connection.go @@ -0,0 +1,57 @@ +/* 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 . + */ + +package smtp + +import ( + "net" + + "github.com/rs/zerolog/log" +) + +type Connection struct { + connection net.Conn +} + +func (self Connection) Read() (string, error) { + buffer := [64]byte{} + read, err := self.connection.Read(buffer[:]) + if err != nil { + return "", err + } + + message := string(buffer[:read - 2]) + + log.Trace().Msgf("%v -> %v", self.RemoteAddr(), message) + + return message, nil +} + +func (self Connection) Write(message string) error { + log.Trace().Msgf("%v <- %v", self.RemoteAddr(), message[:len(message) - 1]) + _, err := self.connection.Write([]byte(message)) + return err +} + +func (self Connection) Close() { + self.connection.Close() +} + +func (self Connection) RemoteAddr() net.Addr { + return self.connection.RemoteAddr() +} diff --git a/smtp/handlers.go b/smtp/handlers.go new file mode 100644 index 0000000..e5ff087 --- /dev/null +++ b/smtp/handlers.go @@ -0,0 +1,103 @@ +/* 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 . + */ + +package smtp + +import ( + "fmt" + "strings" +) + +type Command struct { + name string + Exec func(Connection)([]Command, error) +} + +func (self Command) Name() string { + return self.name +} + +func (self Command) Check(message string) bool { + return strings.HasPrefix(message, self.name) +} + +func (self Connection) Chain() error { + commands, err := Greet(self) + if err != nil { + return err + } + for { + message, err := self.Read() + if err != nil { + return err + } + + command_found := false + for _, command := range commands { + if command.Check(message) { + commands, err = command.Exec(self) + if err != nil { + return err + } + command_found = true + break + } + } + if !command_found { + expected := make([]string, len(commands)) + for index, command := range commands { + expected[index] = command.Name() + } + return fmt.Errorf("Expected one of %v, but got: %v", expected, message) + } + + if len(commands) == 0 { + break + } + } + return nil +} + +/* --- GREETING RESPONSE --- */ + +func Greet(connection Connection) ([]Command, error) { + err := connection.Write( + "220 localhost ESMTP diodemail -- Service ready" + "\n", + ) + if err != nil { + return nil, err + } + + return []Command{ + Command{ "HELO", Hello }, + Command{ "EHLO", Hello }, + }, nil +} + +/* --- HELO/EHLO RESPONSE --- */ + +func Hello(connection Connection) ([]Command, error) { + err := connection.Write( + fmt.Sprintf("250 %v is shy", connection.connection.LocalAddr()) + "\n", + ) + if err != nil { + return nil, err + } + + return []Command{}, nil +} diff --git a/smtp/server.go b/smtp/server.go new file mode 100644 index 0000000..21b9fa1 --- /dev/null +++ b/smtp/server.go @@ -0,0 +1,69 @@ +/* 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 . + */ + +package smtp + +import ( + "net" + "os" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +type PlainListener struct { + listener net.Listener +} + +func handle(connection Connection) { + log.Info().Msgf("New connection %v", connection.RemoteAddr()) + defer connection.Close() + err := connection.Chain() + if err != nil { + log.Error().Msgf("Failed to serve %v: %v", connection.RemoteAddr(), err) + } else { + log.Info().Msgf("Successfully served %v", connection.RemoteAddr()) + } +} + +func Run(host string, implicit_tls bool) error { + log.Logger = zerolog. + New(zerolog.ConsoleWriter{Out: os.Stderr}). + With(). + Timestamp(). + Logger(). + Level(zerolog.TraceLevel) + + listener, err := net.Listen("tcp", host) + if err != nil { + return err + } + + for { + connection, err := listener.Accept() + if err != nil { + return err + } + + go handle(Connection{connection}) + } +} + +func (self PlainListener) Close() error { + return self.listener.Close() +}