Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anacron: read SENDMAIL variable from anacrontab #135

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions anacron/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down
46 changes: 39 additions & 7 deletions anacron/runjob.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

#include <langinfo.h>

#include "matchrx.h"

static int
temp_file(job_rec *jr)
/* Open a temporary file and return its file descriptor */
Expand Down Expand Up @@ -207,6 +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;

Expand Down Expand Up @@ -247,13 +254,38 @@ 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_full = getenv("SENDMAIL");

if (mailer_full == NULL) {
/* Here, I basically mirrored the way /usr/sbin/sendmail is called
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I wouldn't duplicate the execl call but instead assign SENDMAIL to mailer_full if it is NULL.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I wouldn't duplicate the execl call but instead assign SENDMAIL to mailer_full if it is NULL.

I don't know how to do it, without making it into a special case. We need to pass these arguments to sendmail.

* 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 {

/* 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this is more complicated as for executing the mailer via execl the arguments would have to be split. Or perhaps better would be to just allow a few mailer arguments in separate environment variables. SENDMAILARG1 SENDMAILARG2 SENDMAILARG3....

Copy link
Author

@corvus1 corvus1 Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this is more complicated as for executing the mailer via execl the arguments would have to be split. Or perhaps better would be to just allow a few mailer arguments in separate environment variables. SENDMAILARG1 SENDMAILARG2 SENDMAILARG3....

Is that... even acceptable to shift the task of separating arguments to the user? I mean... that's kinda rude even for me. Is that okay? I mean... it turns out, it's incredibly hard to separate arguments in C.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a problem to properly separate the arguments in C because you'd basically have to re-implement the shell argument separator parser to make it really useful. I.e. you'd have to support at least ", ' as quotes and then you also need to somehow support escaping these quote characters, etc.

Copy link
Author

@corvus1 corvus1 Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a problem to properly separate the arguments in C because you'd basically have to re-implement the shell argument separator parser to make it really useful. I.e. you'd have to support at least ", ' as quotes and then you also need to somehow support escaping these quote characters, etc.

I thought I'd just go for something simple, rudimentary even. Look for " -" and split on that. Maybe even with a regex in a loop. There is something going on there, though. If SENDMAIL variable is longer than "/usr/bin/logger -t anacronmai" by a single character, everything breaks and I get some other string there. As if something overlapped in memory.


complain("mailer - `%s` ", mailer);
complain("mailer_args - `%s`", mailer_args);

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);
}
}
/* parent */
/* record mailer pid */
Expand Down
5 changes: 4 additions & 1 deletion man/anacrontab.5
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down