From 02fbcf2e106580d12093fa3336371e21e8299f6a Mon Sep 17 00:00:00 2001 From: Corvus Date: Mon, 9 Jan 2023 22:15:18 +0400 Subject: [PATCH 1/4] anacron: read SENDMAIL variable from anacrontab Use alternative mailer command --- anacron/global.h | 1 + anacron/runjob.c | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/anacron/global.h b/anacron/global.h index 1856854..c496a9a 100644 --- a/anacron/global.h +++ b/anacron/global.h @@ -30,6 +30,7 @@ #define SYSLOG_FACILITY LOG_CRON #define EXPLAIN_LEVEL LOG_NOTICE /* informational messages */ #define COMPLAIN_LEVEL LOG_ERR /* error messages */ +#define DEBUG DEBUG #define DEBUG_LEVEL LOG_DEBUG /* only used when DEBUG is defined */ /* Mail interface. (All MTAs should supply this command) */ diff --git a/anacron/runjob.c b/anacron/runjob.c index 04d6904..2f927db 100644 --- a/anacron/runjob.c +++ b/anacron/runjob.c @@ -207,6 +207,7 @@ xwait(pid_t pid , int *status) static void launch_mailer(job_rec *jr) { + char *mailer; pid_t pid; struct stat buf; @@ -247,13 +248,22 @@ launch_mailer(job_rec *jr) /* fdflags = fcntl(0, F_GETFL); fdflags &= ~O_APPEND; */ /* fcntl(0, F_SETFL, fdflags ); */ - /* Here, I basically mirrored the way /usr/sbin/sendmail is called - * by cron on a Debian system, except for the "-oem" and "-or0s" - * options, which don't seem to be appropriate here. - * Hopefully, this will keep all the MTAs happy. */ - execl(SENDMAIL, SENDMAIL, "-FAnacron", "-odi", - jr->mailto, (char *)NULL); - die_e("Can't exec " SENDMAIL); + /* Get SENDMAIL variable from the environment if set. + /* If not, will use the predefined SENDMAIL from global.h */ + mailer = getenv("SENDMAIL"); + + if (mailer == NULL) { + /* Here, I basically mirrored the way /usr/sbin/sendmail is called + * by cron on a Debian system, except for the "-oem" and "-or0s" + * options, which don't seem to be appropriate here. + * Hopefully, this will keep all the MTAs happy. */ + execl(SENDMAIL, SENDMAIL, "-FAnacron", "-odi", + jr->mailto, (char *)NULL); + die_e("Can't exec " SENDMAIL); + } else { + execl(mailer, mailer, (char *)NULL); + die_e("Can't exec the mailer"); + } } /* parent */ /* record mailer pid */ From dff6a110c721176dbc240b7ed528f0705f07a716 Mon Sep 17 00:00:00 2001 From: Corvus Date: Thu, 12 Jan 2023 18:27:12 +0400 Subject: [PATCH 2/4] Getting command line parameters is partially working. The regex worked! But has all sorts of issues. execl() is acting weird when "" is passed as parameters. And I think I reached the extent of my C programming capabilities. For some bizarre reason I can't even if (string == NULL) anymore. Please help! --- anacron/runjob.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/anacron/runjob.c b/anacron/runjob.c index 2f927db..f2828e9 100644 --- a/anacron/runjob.c +++ b/anacron/runjob.c @@ -39,6 +39,8 @@ #include +#include "matchrx.h" + static int temp_file(job_rec *jr) /* Open a temporary file and return its file descriptor */ @@ -207,7 +209,11 @@ xwait(pid_t pid , int *status) static void launch_mailer(job_rec *jr) { + int r; char *mailer; + char *mailer_args; + char *mailer_full; + pid_t pid; struct stat buf; @@ -250,9 +256,9 @@ launch_mailer(job_rec *jr) /* Get SENDMAIL variable from the environment if set. /* If not, will use the predefined SENDMAIL from global.h */ - mailer = getenv("SENDMAIL"); + mailer_full = getenv("SENDMAIL"); - if (mailer == NULL) { + if (mailer_full == NULL) { /* Here, I basically mirrored the way /usr/sbin/sendmail is called * by cron on a Debian system, except for the "-oem" and "-or0s" * options, which don't seem to be appropriate here. @@ -261,8 +267,19 @@ launch_mailer(job_rec *jr) jr->mailto, (char *)NULL); die_e("Can't exec " SENDMAIL); } else { - execl(mailer, mailer, (char *)NULL); - die_e("Can't exec the mailer"); + + /* Splitting into binary path and command line parameters*/ + + complain("mailer_full - `%s`", mailer_full); + + strcat(mailer_full," "); + r = match_rx("(^[^ ]*) (.*)", mailer_full, 2, &mailer, &mailer_args); + + complain("mailer - `%s` ", mailer); + complain("mailer_args - `%s`", mailer_args); + + execl(mailer, mailer, mailer_args, (char *)NULL); + die_e("Can't exec the mailer %s", mailer); } } /* parent */ From e72488b60bb522cc610038c503fa7ea4d526ab9b Mon Sep 17 00:00:00 2001 From: Corvus Date: Sat, 14 Jan 2023 07:29:40 +0400 Subject: [PATCH 3/4] ugly but works. Check for '-' in parameters. If not found, don't pass. --- anacron/runjob.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/anacron/runjob.c b/anacron/runjob.c index f2828e9..242e1b4 100644 --- a/anacron/runjob.c +++ b/anacron/runjob.c @@ -277,8 +277,13 @@ launch_mailer(job_rec *jr) complain("mailer - `%s` ", mailer); complain("mailer_args - `%s`", mailer_args); - - execl(mailer, mailer, mailer_args, (char *)NULL); + + if ( strchr(mailer_args, '-') != NULL ) { + execl(mailer, mailer, mailer_args, (char *)NULL); + } else { + execl(mailer, mailer, (char *)NULL); + } + die_e("Can't exec the mailer %s", mailer); } } From c06a13da3e01776312df383f56fb2f9262469ebd Mon Sep 17 00:00:00 2001 From: Corvus Date: Tue, 17 Jan 2023 03:26:52 +0400 Subject: [PATCH 4/4] update anacrontab manpage --- man/anacrontab.5 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/man/anacrontab.5 b/man/anacrontab.5 index 507cfd8..77d85d6 100644 --- a/man/anacrontab.5 +++ b/man/anacrontab.5 @@ -60,11 +60,14 @@ The .I RANDOM_DELAY variable denotes the maximum number of minutes that will be added to the delay in minutes variable which is specified for each job. A -.I RANDOM_DELAY set to 12 would therefore add, randomly, between 0 and 12 minutes to the delay in minutes for each job in that particular anacrontab. When set to 0, no random delay is added. .PP +.I SENDMAIL +variable allows to assign an alternative mailer program, instead of +default /usr/sbin/sendmail +.PP If .I MAILTO is defined (and non-empty), mail is sent to the specified address,