#688716 cron: optionally inherit PATH from parent process

Package:
cron
Source:
cron
Description:
process scheduling daemon
Submitter:
Tom Jones
Date:
2021-08-06 11:39:03 UTC
Severity:
wishlist
#688716#5
Date:
2012-09-25 01:35:12 UTC
From:
To:
The default PATH of cron isn't very useful.  This patch adds an
option -P to leave PATH alone, so that it's inherited from whatever
execs cron.  In our case that is daemontools, but could also be
start-stop-daemon or whatever.  This puts our sysadmins in control
of cron's (and child processes') PATH, as they are able to set it in the
run script.  Without the -P option, crontabs that don't set PATH
themselves get a value of /usr/bin:/bin, which is hardcoded as
_PATH_DEFPATH in pathnames.h.

diff --git a/cron.8 b/cron.8
index 586d9e5..41387fa 100644
--- a/cron.8
+++ b/cron.8
@@ -36,6 +36,9 @@ runlevels.
 .B -f
 Stay in foreground mode, don't daemonize.
 .TP
+.B -P
+Don't set PATH for child processes.  Let it inherit instead.
+.TP
 .B -l
 Enable LSB compliant names for /etc/cron.d files. This setting, however, does
 not affect the parsing of files under /etc/cron.hourly, /etc/cron.daily,
diff --git a/cron.c b/cron.c
index 5630b4d..638ca3f 100644
--- a/cron.c
+++ b/cron.c
@@ -93,7 +93,9 @@ main(argc, argv)
 	set_cron_cwd();

 #if defined(POSIX)
-	setenv("PATH", _PATH_DEFPATH, 1);
+	if (change_child_path) {
+		setenv("PATH", _PATH_DEFPATH, 1);
+	}
 #endif

        /* Get the default locale character set for the mail
@@ -468,15 +470,19 @@ parse_args(argc, argv)

 	log_level = 1;
 	stay_foreground = 0;
+	change_child_path = 1;
         lsbsysinit_mode = 0;

-	while (EOF != (argch = getopt(argc, argv, "lfx:L:"))) {
+	while (EOF != (argch = getopt(argc, argv, "lfPx:L:"))) {
 		switch (argch) {
 		default:
 			usage();
 		case 'f':
 			stay_foreground = 1;
 			break;
+		case 'P':
+			change_child_path = 0;
+			break;
 		case 'x':
 			if (!set_debug_flags(optarg))
 				usage();
diff --git a/cron.h b/cron.h
index d1b2bcf..8e10393 100644
--- a/cron.h
+++ b/cron.h
@@ -304,6 +304,7 @@ time_min clockTime;
 static long GMToff;

 int	stay_foreground;
+int	change_child_path;
 int     lsbsysinit_mode;
 int     log_level = 1;
 char    cron_default_mail_charset[MAX_ENVSTR] = "";
diff --git a/debian/changelog b/debian/changelog
index 954b5ce..5bd7e51 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
+cron (3.0pl1-124.1) unstable; urgency=low
+
+  * Non-maintainer upload.
+  * Add -P option to preserve PATH.
+
+ -- Tom Jones <tom@oxix.org>  Tue, 25 Sep 2012 00:57:10 +0100
+
 cron (3.0pl1-124) unstable; urgency=medium

   [ Release targeted to Wheezy including fixes for critical/important bugs ]
diff --git a/entry.c b/entry.c
index f191143..492e36f 100644
--- a/entry.c
+++ b/entry.c
@@ -28,6 +28,7 @@ static char rcsid[] = "$Id: entry.c,v 2.12 1994/01/17 03:20:37 vixie Exp $";

 #include "cron.h"

+extern int change_child_path;

 typedef	enum ecode {
 	e_none, e_minute, e_hour, e_dom, e_month, e_dow,
@@ -285,7 +286,9 @@ load_entry(file, error_func, pw, envp)
 		}
 	}
 	if (!env_get("PATH", e->envp)) {
-		snprintf(envstr, MAX_ENVSTR, "PATH=%s", _PATH_DEFPATH);
+		if (change_child_path) {
+			snprintf(envstr, MAX_ENVSTR, "PATH=%s", _PATH_DEFPATH);
+		}
 		if ((tenvp = env_set(e->envp, envstr))) {
 			e->envp = tenvp;
 		} else {
--- usr/bin/editor:
/bin/nano
--- /usr/bin/crontab: -rwxr-sr-x 1 root crontab 30248 Dec 18 2010 /usr/bin/crontab
--- /var/spool/cron drwxr-xr-x 5 root root 4096 Sep 25 01:11 /var/spool/cron
--- /var/spool/cron/crontabs drwx-wx--T 2 root crontab 4096 Dec 20 2006 /var/spool/cron/crontabs
#688716#10
Date:
2021-08-06 11:35:39 UTC
From:
To:
Hi Maintainer

This change was recently adopted in Ubuntu, along with the attached
patch to update crontab(5) manpage to match the new behaviour.

Regards
Graham