From d9d56552f396a3a3cc0014c70b77089bf917bc32 Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Sun, 24 May 2020 02:08:11 +0100 Subject: [PATCH] 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 who sent an alternative patch for this functionality. --- chasquid.go | 9 ++++- docs/man/chasquid.conf.5 | 5 ++- docs/man/chasquid.conf.5.pod | 3 +- etc/chasquid/chasquid.conf | 1 + internal/config/config.pb.go | 1 + internal/config/config.proto | 1 + test/t-17-maillog/.gitignore | 1 + test/t-17-maillog/config/chasquid.conf.in | 10 +++++ test/t-17-maillog/content | 4 ++ test/t-17-maillog/hosts | 1 + test/t-17-maillog/msmtprc | 28 +++++++++++++ test/t-17-maillog/run.sh | 48 +++++++++++++++++++++++ 12 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 test/t-17-maillog/.gitignore create mode 100644 test/t-17-maillog/config/chasquid.conf.in create mode 100644 test/t-17-maillog/content create mode 100644 test/t-17-maillog/hosts create mode 100644 test/t-17-maillog/msmtprc create mode 100755 test/t-17-maillog/run.sh 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 == "" { + switch path { + case "": maillog.Default, err = maillog.NewSyslog() - } else { + case "": + maillog.Default = maillog.New(os.Stdout) + case "": + 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`\*(C'\fR, log using the -syslog (at \f(CW\*(C`MAIL|INFO\*(C'\fR priority). Default: \f(CW\*(C`\*(C'\fR. +syslog (at \f(CW\*(C`MAIL|INFO\*(C'\fR priority). If \f(CW\*(C`\*(C'\fR, log to stdout; if +\&\f(CW\*(C`\*(C'\fR, log to stderr. Default: \f(CW\*(C`\*(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. Default: C<.>. =item B (string): Path where to write the mail log to. If C<< >>, log using the -syslog (at C priority). Default: C<< >>. +syslog (at C priority). If C<< >>, log to stdout; if +C<< >>, log to stderr. Default: C<< >>. =item B (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 "", log using the syslog (at MAIL|INFO priority). +# If "", log to stdout; if "", log to stderr. # Default: #mail_log_path: "" 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 "", log using the syslog (at MAIL|INFO priority). + // If "", log to stdout; if "", log to stderr. // Default: 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 "", log using the syslog (at MAIL|INFO priority). + // If "", log to stdout; if "", log to stderr. // Default: 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="" +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="" +send_one +if ! grep -q "from=user@testserver all done" .logs/stderr; then + fail "entries not found in .logs/stderr" +fi + +success