forked from drew/smtprelay
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")
|
allowedRecipStr = flag.String("allowed_recipients", "", "Regular expression for valid TO EMail addresses")
|
||||||
allowedRecipients *regexp.Regexp
|
allowedRecipients *regexp.Regexp
|
||||||
allowedUsers = flag.String("allowed_users", "", "Path to file with valid users/passwords")
|
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")
|
remoteHost = flag.String("remote_host", "", "Outgoing SMTP server")
|
||||||
remoteUser = flag.String("remote_user", "", "Username for authentication on 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")
|
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
|
// Set up logging as soon as possible
|
||||||
setupLogger()
|
setupLogger()
|
||||||
|
|
||||||
if *remoteHost == "" {
|
if *remoteHost == "" && *command == "" {
|
||||||
log.Warn("remote_host not set; mail will not be forwarded!")
|
log.Warn("no remote_host or command set; mail will not be forwarded!")
|
||||||
}
|
}
|
||||||
|
|
||||||
setupAllowedNetworks()
|
setupAllowedNetworks()
|
||||||
|
|||||||
28
main.go
28
main.go
@@ -1,11 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -163,8 +165,32 @@ func mailHandler(peer smtpd.Peer, env smtpd.Envelope) error {
|
|||||||
"uuid": generateUUID(),
|
"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 == "" {
|
if *remoteHost == "" {
|
||||||
logger.Warning("remote_host not set; discarding mail")
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,3 +78,6 @@
|
|||||||
|
|
||||||
; Sender e-mail address on outgoing SMTP server
|
; Sender e-mail address on outgoing SMTP server
|
||||||
;remote_sender =
|
;remote_sender =
|
||||||
|
|
||||||
|
; Pipe messages to external command
|
||||||
|
;command = /usr/local/bin/script
|
||||||
Reference in New Issue
Block a user