Skip to content

Add external command support #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down
32 changes: 29 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"bytes"
"crypto/tls"
"fmt"
"net"
"net/textproto"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
Expand Down Expand Up @@ -163,15 +165,39 @@ 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
}

env.AddReceivedLine(peer)

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
}

logger.Info("delivering mail from peer using smarthost")

env.AddReceivedLine(peer)

var sender string

if *remoteSender == "" {
Expand Down
3 changes: 3 additions & 0 deletions smtprelay.ini
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@

; Sender e-mail address on outgoing SMTP server
;remote_sender =

; Pipe messages to external command
;command = /usr/local/bin/script