117 lines
2.7 KiB
Go
117 lines
2.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 main
|
|
|
|
import (
|
|
"flag"
|
|
"crypto/tls"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"forge.monodon.me/Gnarwhal/diodemail/smtp"
|
|
)
|
|
|
|
const Version = "1.0.0"
|
|
|
|
func main() {
|
|
log.Logger = zerolog.
|
|
New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.DateTime}).
|
|
With().
|
|
Timestamp().
|
|
Logger().
|
|
Level(zerolog.ErrorLevel)
|
|
|
|
var config_path string
|
|
var log_level string
|
|
|
|
flag.StringVar(
|
|
&config_path,
|
|
"config-file",
|
|
"/etc/diodemail/config.json",
|
|
"Path to config file",
|
|
)
|
|
flag.StringVar(
|
|
&log_level,
|
|
"log-level",
|
|
"",
|
|
"Maximum log level to log",
|
|
)
|
|
|
|
flag.Parse()
|
|
config, err := LoadConfig(config_path)
|
|
if err != nil {
|
|
log.Fatal().Msgf("%v", err)
|
|
}
|
|
if log_level == "" {
|
|
if config.LogLevel != "" {
|
|
log_level = config.LogLevel
|
|
} else {
|
|
log_level = "info"
|
|
}
|
|
}
|
|
|
|
parsed_log_level, err := zerolog.ParseLevel(log_level)
|
|
if err != nil {
|
|
log.Fatal().Msgf("%v", err)
|
|
}
|
|
|
|
log.Logger = zerolog.
|
|
New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.DateTime}).
|
|
With().
|
|
Timestamp().
|
|
Logger().
|
|
Level(parsed_log_level)
|
|
log.Info().Msgf("Starting diodemail v%v", Version)
|
|
log.Info().Msgf("Loaded config from: %v", config_path)
|
|
|
|
if config.Certificate.CertFile == "" || config.Certificate.KeyFile == "" {
|
|
log.Fatal().Msgf(
|
|
"Must provide CertFile (got '%v') and KeyFile (got '%v')",
|
|
config.Certificate.CertFile,
|
|
config.Certificate.KeyFile,
|
|
)
|
|
}
|
|
|
|
certificate, err := tls.LoadX509KeyPair(
|
|
config.Certificate.CertFile,
|
|
config.Certificate.KeyFile,
|
|
)
|
|
if err != nil {
|
|
log.Fatal().Msgf("Failed to load TLS config: %v", err)
|
|
}
|
|
|
|
if config.Auth.Enabled && config.Auth.PasswordHash == "" {
|
|
log.Fatal().Msgf("Authentication is enabled but no password hash was supplied")
|
|
}
|
|
|
|
err = smtp.Run(
|
|
config.Host,
|
|
config.Ports.Plain,
|
|
config.Ports.TLS,
|
|
tls.Config{Certificates: []tls.Certificate{certificate}},
|
|
config.Auth.Enabled,
|
|
config.Auth.PasswordHash,
|
|
)
|
|
if err != nil {
|
|
log.Fatal().Msgf("%v", err)
|
|
}
|
|
}
|