Skip to content

Commit

Permalink
Expand MAILTO and MAILFROM environment variables
Browse files Browse the repository at this point in the history
Closes cronie-crond#26

Signed-off-by: Fernando Cappi <[email protected]>
  • Loading branch information
fcappi committed Jun 19, 2020
1 parent 25d3ddd commit 17df7af
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
2 changes: 2 additions & 0 deletions anacron/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#define FAILURE_EXIT 1
#define MAX_MSG 150
#define MAX_EMAILSTR 255 /* max length of email address strings (254 + \0) */

#include <signal.h>
#include <time.h>
Expand Down Expand Up @@ -159,5 +160,6 @@ void fake_job(job_rec *jr);
/* runjob.c */
void tend_children();
void launch_job(job_rec *jr);
void expand_env_variable(const char *, char *);

#endif
39 changes: 34 additions & 5 deletions anacron/runjob.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <wordexp.h>
#include "global.h"

#include <langinfo.h>
Expand Down Expand Up @@ -286,6 +287,8 @@ launch_job(job_rec *jr)
char hostname[512];
char *mailto;
char *mailfrom;
char mailto_expanded[MAX_EMAILSTR];
char mailfrom_expanded[MAX_EMAILSTR];

/* get hostname */
if (gethostname(hostname, 512)) {
Expand All @@ -296,14 +299,23 @@ launch_job(job_rec *jr)

/* Get the destination email address if set, or current user otherwise */
mailto = getenv("MAILTO");

if (mailto == NULL)
mailto = username();
if (mailto == NULL) {
mailto = username();
}
else {
expand_env_variable(mailto, mailto_expanded);
mailto = mailto_expanded;
}

/* Get the source email address if set, or current user otherwise */
mailfrom = getenv("MAILFROM");
if (mailfrom == NULL)
mailfrom = username();
if (mailfrom == NULL) {
mailfrom = username();
}
else {
expand_env_variable(mailfrom, mailfrom_expanded);
mailfrom = mailfrom_expanded;
}

/* create temporary file for stdout and stderr of the job */
temp_file(jr); fd = jr->output_fd;
Expand Down Expand Up @@ -399,3 +411,20 @@ tend_children(void)
j++;
}
}

/* Expand env variables in 'source' arg and save to 'result'
*/
void expand_env_variable(const char *source, char *result) {
wordexp_t p;

// do not substitute commands
// consider unknown variable as error
int flags = WRDE_NOCMD | WRDE_UNDEF;

if (wordexp(source, &p, flags) == EXIT_SUCCESS) {
strcpy(result, p.we_wordv[0]);
wordfree(&p);
}
else
strcpy(result, source);
}
12 changes: 12 additions & 0 deletions src/do_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ void do_command(entry * e, user * u) {
static int child_process(entry * e, char **jobenv) {
int stdin_pipe[2], stdout_pipe[2];
char *input_data, *usernm, *mailto, *mailfrom;
char mailto_expanded[MAX_EMAILSTR];
char mailfrom_expanded[MAX_EMAILSTR];
int children = 0;
pid_t pid = getpid();
struct sigaction sa;
Expand Down Expand Up @@ -126,6 +128,16 @@ static int child_process(entry * e, char **jobenv) {
usernm = e->pwd->pw_name;
mailto = env_get("MAILTO", jobenv);
mailfrom = env_get("MAILFROM", e->envp);

if (mailto != NULL) {
expand_env_variable(mailto, mailto_expanded);
mailto = mailto_expanded;
}

if (mailfrom != NULL) {
expand_env_variable(mailfrom, mailfrom_expanded);
mailfrom = mailfrom_expanded;
}

/* create some pipes to talk to our future child
*/
Expand Down
18 changes: 18 additions & 0 deletions src/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <wordexp.h>

#include "globals.h"
#include "funcs.h"
Expand Down Expand Up @@ -303,3 +304,20 @@ char **env_update_home(char **envp, const char *dir) {

return envp;
}

/* Expand env variables in 'source' arg and save to 'result'
*/
void expand_env_variable(const char *source, char *result) {
wordexp_t p;

// do not substitute commands
// consider unknown variable as error
int flags = WRDE_NOCMD | WRDE_UNDEF;

if (wordexp(source, &p, flags) == EXIT_SUCCESS) {
strcpy(result, p.we_wordv[0]);
wordfree(&p);
}
else
strcpy(result, source);
}
3 changes: 2 additions & 1 deletion src/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ void set_cron_uid(void),
acquire_daemonlock(int),
log_it(const char *, PID_T, const char *, const char *, int),
log_close(void),
check_orphans(cron_db *);
check_orphans(cron_db *),
expand_env_variable(const char *, char *);
#if defined WITH_INOTIFY
void set_cron_watched(int ),
set_cron_unwatched(int ),
Expand Down
1 change: 1 addition & 0 deletions src/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#define MAX_USER_ENTRIES 1000 /* maximum crontab entries in user's crontab */
#define MAX_GARBAGE 32768 /* max num of chars of comments and whitespaces between entries */
#define MAX_CLOSE_FD 10000 /* max fd num to close when spawning a child process */
#define MAX_EMAILSTR 255 /* max length of email address strings (254 + \0) */

/* NOTE: these correspond to DebugFlagNames,
* defined below.
Expand Down

0 comments on commit 17df7af

Please sign in to comment.