git » chasquid » commit d9d5655

maillog: Support logging to stdout and stderr

author Alberto Bertogli
2020-05-24 01:08:11 UTC
committer Alberto Bertogli
2020-05-24 01:26:18 UTC
parent d83c1dc5916c8b26f50154666d90b0e1dc2e288c

maillog: Support logging to stdout and stderr

This patch adds support for writing maillog to stdout and stderr, which
can be desirable in certain environments.

Thanks to Denys Vitali <denys@denv.it> who sent an alternative patch for
this functionality.

chasquid.go +7 -2
docs/man/chasquid.conf.5 +3 -2
docs/man/chasquid.conf.5.pod +2 -1
etc/chasquid/chasquid.conf +1 -0
internal/config/config.pb.go +1 -0
internal/config/config.proto +1 -0
test/t-17-maillog/.gitignore +1 -0
test/t-17-maillog/config/chasquid.conf.in +10 -0
test/t-17-maillog/content +4 -0
test/t-17-maillog/hosts +1 -0
test/t-17-maillog/msmtprc +28 -0
test/t-17-maillog/run.sh +48 -0

diff --git a/chasquid.go b/chasquid.go
index 97e6cd5..b3a1c47 100644
--- a/chasquid.go
+++ b/chasquid.go
@@ -216,9 +216,14 @@ func loadAddresses(srv *smtpsrv.Server, addrs []string, ls []net.Listener, mode
 func initMailLog(path string) {
 	var err error
 
-	if path == "<syslog>" {
+	switch path {
+	case "<syslog>":
 		maillog.Default, err = maillog.NewSyslog()
-	} else {
+	case "<stdout>":
+		maillog.Default = maillog.New(os.Stdout)
+	case "<stderr>":
+		maillog.Default = maillog.New(os.Stderr)
+	default:
 		_ = os.MkdirAll(filepath.Dir(path), 0775)
 		maillog.Default, err = maillog.NewFile(path)
 	}
diff --git a/docs/man/chasquid.conf.5 b/docs/man/chasquid.conf.5
index 8da5861..64c45c2 100644
--- a/docs/man/chasquid.conf.5
+++ b/docs/man/chasquid.conf.5
@@ -133,7 +133,7 @@
 .\" ========================================================================
 .\"
 .IX Title "chasquid.conf 5"
-.TH chasquid.conf 5 "2020-05-13" "" ""
+.TH chasquid.conf 5 "2020-05-24" "" ""
 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
 .\" way too many mistakes in technical documents.
 .if n .ad l
@@ -224,7 +224,8 @@ you set this to \f(CW\*(C`._\*(C'\fR, email to local user \f(CW\*(C`u.se_r\*(C'\
 .IP "\fBmail_log_path\fR (string):" 8
 .IX Item "mail_log_path (string):"
 Path where to write the mail log to.  If \f(CW\*(C`<syslog>\*(C'\fR, log using the
-syslog (at \f(CW\*(C`MAIL|INFO\*(C'\fR priority).  Default: \f(CW\*(C`<syslog>\*(C'\fR.
+syslog (at \f(CW\*(C`MAIL|INFO\*(C'\fR priority).  If \f(CW\*(C`<stdout>\*(C'\fR, log to stdout; if
+\&\f(CW\*(C`<stderr>\*(C'\fR, log to stderr.  Default: \f(CW\*(C`<syslog>\*(C'\fR.
 .IP "\fBdovecot_auth\fR (bool):" 8
 .IX Item "dovecot_auth (bool):"
 Enable dovecot authentication. If true, users that are not found in chasquid's
diff --git a/docs/man/chasquid.conf.5.pod b/docs/man/chasquid.conf.5.pod
index 2bd2005..fa6705d 100644
--- a/docs/man/chasquid.conf.5.pod
+++ b/docs/man/chasquid.conf.5.pod
@@ -101,7 +101,8 @@ C<user>.  Default: C<.>.
 =item B<mail_log_path> (string):
 
 Path where to write the mail log to.  If C<< <syslog> >>, log using the
-syslog (at C<MAIL|INFO> priority).  Default: C<< <syslog> >>.
+syslog (at C<MAIL|INFO> priority).  If C<< <stdout> >>, log to stdout; if
+C<< <stderr> >>, log to stderr.  Default: C<< <syslog> >>.
 
 =item B<dovecot_auth> (bool):
 
diff --git a/etc/chasquid/chasquid.conf b/etc/chasquid/chasquid.conf
index 5cc23fc..ea07108 100644
--- a/etc/chasquid/chasquid.conf
+++ b/etc/chasquid/chasquid.conf
@@ -71,6 +71,7 @@
 
 # Path where to write the mail log to.
 # If "<syslog>", log using the syslog (at MAIL|INFO priority).
+# If "<stdout>", log to stdout; if "<stderr>", log to stderr.
 # Default: <syslog>
 #mail_log_path: "<syslog>"
 
diff --git a/internal/config/config.pb.go b/internal/config/config.pb.go
index ffa704d..bc5a891 100644
--- a/internal/config/config.pb.go
+++ b/internal/config/config.pb.go
@@ -93,6 +93,7 @@ type Config struct {
 	DropCharacters string `protobuf:"bytes,11,opt,name=drop_characters,json=dropCharacters,proto3" json:"drop_characters,omitempty"`
 	// Path where to write the mail log to.
 	// If "<syslog>", log using the syslog (at MAIL|INFO priority).
+	// If "<stdout>", log to stdout; if "<stderr>", log to stderr.
 	// Default: <syslog>
 	MailLogPath string `protobuf:"bytes,12,opt,name=mail_log_path,json=mailLogPath,proto3" json:"mail_log_path,omitempty"`
 	// Enable dovecot authentication.
diff --git a/internal/config/config.proto b/internal/config/config.proto
index 9e8c31e..3c1bb39 100644
--- a/internal/config/config.proto
+++ b/internal/config/config.proto
@@ -77,6 +77,7 @@ message Config {
 
 	// Path where to write the mail log to.
 	// If "<syslog>", log using the syslog (at MAIL|INFO priority).
+	// If "<stdout>", log to stdout; if "<stderr>", log to stderr.
 	// Default: <syslog>
 	string mail_log_path = 12;
 
diff --git a/test/t-17-maillog/.gitignore b/test/t-17-maillog/.gitignore
new file mode 100644
index 0000000..5441a6c
--- /dev/null
+++ b/test/t-17-maillog/.gitignore
@@ -0,0 +1 @@
+config/chasquid.conf
diff --git a/test/t-17-maillog/config/chasquid.conf.in b/test/t-17-maillog/config/chasquid.conf.in
new file mode 100644
index 0000000..6ad2e11
--- /dev/null
+++ b/test/t-17-maillog/config/chasquid.conf.in
@@ -0,0 +1,10 @@
+smtp_address: ":1025"
+submission_address: ":1587"
+submission_over_tls_address: ":1465"
+monitoring_address: ":1099"
+
+mail_delivery_agent_bin: "test-mda"
+mail_delivery_agent_args: "%to%"
+
+data_dir: "../.data"
+mail_log_path: "$MAIL_LOG_PATH"
diff --git a/test/t-17-maillog/content b/test/t-17-maillog/content
new file mode 100644
index 0000000..76a8b16
--- /dev/null
+++ b/test/t-17-maillog/content
@@ -0,0 +1,4 @@
+Subject: Prueba desde el test
+
+Crece desde el test el futuro
+Crece desde el test
diff --git a/test/t-17-maillog/hosts b/test/t-17-maillog/hosts
new file mode 100644
index 0000000..2b9b623
--- /dev/null
+++ b/test/t-17-maillog/hosts
@@ -0,0 +1 @@
+testserver localhost
diff --git a/test/t-17-maillog/msmtprc b/test/t-17-maillog/msmtprc
new file mode 100644
index 0000000..eed8751
--- /dev/null
+++ b/test/t-17-maillog/msmtprc
@@ -0,0 +1,28 @@
+account default
+
+host testserver
+port 1587
+
+tls on
+tls_trust_file config/certs/testserver/fullchain.pem
+
+from user@testserver
+
+auth on
+user user@testserver
+password secretpassword
+
+account smtpport : default
+port 1025
+
+account subm_tls : default
+port 1465
+tls_starttls off
+
+account baduser : default
+user unknownuser@testserver
+password secretpassword
+
+account badpasswd : default
+user user@testserver
+password badsecretpassword
diff --git a/test/t-17-maillog/run.sh b/test/t-17-maillog/run.sh
new file mode 100755
index 0000000..37d0410
--- /dev/null
+++ b/test/t-17-maillog/run.sh
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+set -e
+. $(dirname ${0})/../util/lib.sh
+
+init
+
+mkdir -p .logs
+
+generate_certs_for testserver
+add_user user@testserver secretpassword
+add_user someone@testserver secretpassword
+
+function send_one() {
+	rm -f .logs/mail_log .logs/stdout .logs/stderr
+	envsubst < config/chasquid.conf.in > config/chasquid.conf
+
+	chasquid -v=2 --logfile=.logs/chasquid.log --config_dir=config \
+		> .logs/stdout 2> .logs/stderr &
+	wait_until_ready 1025
+
+	run_msmtp someone@testserver < content
+	wait_for_file .mail/someone@testserver
+	mail_diff content .mail/someone@testserver
+
+	pkill -s 0 chasquid
+	sleep 0.2
+}
+
+export MAIL_LOG_PATH="../.logs/mail_log"
+send_one
+if ! grep -q "from=user@testserver all done" .logs/mail_log; then
+	fail "entries not found in .logs/mail_log"
+fi
+
+export MAIL_LOG_PATH="<stdout>"
+send_one
+if ! grep -q "from=user@testserver all done" .logs/stdout; then
+	fail "entries not found in .logs/stdout"
+fi
+
+export MAIL_LOG_PATH="<stderr>"
+send_one
+if ! grep -q "from=user@testserver all done" .logs/stderr; then
+	fail "entries not found in .logs/stderr"
+fi
+
+success