From 74fb03b579020d36e40d87ea48fb13e87e22acdf Mon Sep 17 00:00:00 2001 From: chayleaf Date: Mon, 23 Oct 2023 15:31:06 +0700 Subject: [PATCH] make sendmail path configurable using an env var Closes: #80 --- cmd/certspotter/main.go | 5 +++++ man/certspotter.md | 4 ++++ monitor/config.go | 1 + monitor/notify.go | 6 +++--- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/certspotter/main.go b/cmd/certspotter/main.go index 9730789..0dae8c9 100644 --- a/cmd/certspotter/main.go +++ b/cmd/certspotter/main.go @@ -201,11 +201,16 @@ func main() { Verbose: flags.verbose, Script: flags.script, ScriptDir: defaultScriptDir(), + SendmailPath: "/usr/sbin/sendmail", Email: flags.email, Stdout: flags.stdout, HealthCheckInterval: flags.healthcheck, } + if envVar := os.Getenv("SENDMAIL_PATH"); envVar != "" { + config.SendmailPath = envVar + } + emailFileExists := false if emailRecipients, err := readEmailFile(defaultEmailFile()); err == nil { emailFileExists = true diff --git a/man/certspotter.md b/man/certspotter.md index 728d09e..649c5cb 100644 --- a/man/certspotter.md +++ b/man/certspotter.md @@ -218,6 +218,10 @@ and non-zero when a serious error occurs. : URL of proxy server for making HTTPS requests. `http://`, `https://`, and `socks5://` URLs are supported. By default, no proxy server is used. +`SENDMAIL_PATH` + +: Path to the sendmail binary. Defaults to `/usr/sbin/sendmail`. + # SEE ALSO certspotter-script(8) diff --git a/monitor/config.go b/monitor/config.go index 1e0d60c..d1bc430 100644 --- a/monitor/config.go +++ b/monitor/config.go @@ -20,6 +20,7 @@ type Config struct { WatchList WatchList Verbose bool SaveCerts bool + SendmailPath string Script string ScriptDir string Email []string diff --git a/monitor/notify.go b/monitor/notify.go index 8fc6d09..86cabca 100644 --- a/monitor/notify.go +++ b/monitor/notify.go @@ -36,7 +36,7 @@ func notify(ctx context.Context, config *Config, notif notification) error { } if len(config.Email) > 0 { - if err := sendEmail(ctx, config.Email, notif); err != nil { + if err := sendEmail(ctx, config.SendmailPath, config.Email, notif); err != nil { return err } } @@ -62,7 +62,7 @@ func writeToStdout(notif notification) { os.Stdout.WriteString(notif.Text() + "\n") } -func sendEmail(ctx context.Context, to []string, notif notification) error { +func sendEmail(ctx context.Context, sendmailPath string, to []string, notif notification) error { stdin := new(bytes.Buffer) stderr := new(bytes.Buffer) @@ -77,7 +77,7 @@ func sendEmail(ctx context.Context, to []string, notif notification) error { args := []string{"-i", "--"} args = append(args, to...) - sendmail := exec.CommandContext(ctx, "/usr/sbin/sendmail", args...) + sendmail := exec.CommandContext(ctx, sendmailPath, args...) sendmail.Stdin = stdin sendmail.Stderr = stderr