Browse Source

Added external command support.

Ben Ubois 4 năm trước cách đây
mục cha
commit
d8860fc917
3 tập tin đã thay đổi với 33 bổ sung3 xóa
  1. 3 2
      config.go
  2. 27 1
      main.go
  3. 3 0
      smtprelay.ini

+ 3 - 2
config.go

@@ -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()

+ 27 - 1
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
 	}
 

+ 3 - 0
smtprelay.ini

@@ -78,3 +78,6 @@
 
 ; Sender e-mail address on outgoing SMTP server
 ;remote_sender = 
+
+; Pipe messages to external command
+;command = /usr/local/bin/script