mirror of
https://github.com/decke/smtprelay.git
synced 2025-12-25 07:43:06 -07:00
Added external command support.
This commit is contained in:
@@ -34,6 +34,7 @@ var (
|
||||
allowedRecipStr = flag.String("allowed_recipients", "", "Regular expression for valid TO EMail addresses")
|
||||
allowedRecipients *regexp.Regexp
|
||||
allowedUsers = flag.String("allowed_users", "", "Path to file with valid users/passwords")
|
||||
command = flag.String("command", "", "Path to pipe command")
|
||||
remoteHost = flag.String("remote_host", "", "Outgoing SMTP server")
|
||||
remoteUser = flag.String("remote_user", "", "Username for authentication on outgoing SMTP server")
|
||||
remotePass = flag.String("remote_pass", "", "Password for authentication on outgoing SMTP server")
|
||||
@@ -172,8 +173,8 @@ func ConfigLoad() {
|
||||
// Set up logging as soon as possible
|
||||
setupLogger()
|
||||
|
||||
if *remoteHost == "" {
|
||||
log.Warn("remote_host not set; mail will not be forwarded!")
|
||||
if *remoteHost == "" && *command == "" {
|
||||
log.Warn("no remote_host or command set; mail will not be forwarded!")
|
||||
}
|
||||
|
||||
setupAllowedNetworks()
|
||||
|
||||
28
main.go
28
main.go
@@ -1,11 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
@@ -163,8 +165,32 @@ func mailHandler(peer smtpd.Peer, env smtpd.Envelope) error {
|
||||
"uuid": generateUUID(),
|
||||
})
|
||||
|
||||
if *remoteHost == "" && *command == "" {
|
||||
logger.Warning("no remote_host or command set; discarding mail")
|
||||
return nil
|
||||
}
|
||||
|
||||
if *command != "" {
|
||||
cmdLogger := logger.WithField("command", *command)
|
||||
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
cmd := exec.Command(*command)
|
||||
cmd.Stdin = bytes.NewReader(env.Data)
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
cmdLogger.WithError(err).Error(stderr.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
cmdLogger.Info("pipe command successful: " + stdout.String())
|
||||
}
|
||||
|
||||
if *remoteHost == "" {
|
||||
logger.Warning("remote_host not set; discarding mail")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -78,3 +78,6 @@
|
||||
|
||||
; Sender e-mail address on outgoing SMTP server
|
||||
;remote_sender =
|
||||
|
||||
; Pipe messages to external command
|
||||
;command = /usr/local/bin/script
|
||||
Reference in New Issue
Block a user