815a0e
diff -up cronie-1.4.11/src/cron.c.refresh-users cronie-1.4.11/src/cron.c
815a0e
--- cronie-1.4.11/src/cron.c.refresh-users	2013-07-18 14:27:08.000000000 +0200
815a0e
+++ cronie-1.4.11/src/cron.c	2015-04-21 14:43:11.205438697 +0200
815a0e
@@ -525,7 +525,6 @@ static void find_jobs(int vtime, cron_db
815a0e
 	int minute, hour, dom, month, dow;
815a0e
 	user *u;
815a0e
 	entry *e;
815a0e
-	const char *uname;
815a0e
 
815a0e
 	/* The support for the job-specific timezones is not perfect. There will
815a0e
 	 * be jobs missed or run twice during the DST change in the job timezone.
815a0e
@@ -562,16 +561,11 @@ static void find_jobs(int vtime, cron_db
815a0e
 		 */
815a0e
 		for (u = db->head; u != NULL; u = u->next) {
815a0e
 		for (e = u->crontab; e != NULL; e = e->next) {
815a0e
-			Debug(DSCH | DEXT, ("user [%s:%ld:%ld:...] cmd=\"%s\"\n",
815a0e
-					e->pwd->pw_name, (long) e->pwd->pw_uid,
815a0e
-					(long) e->pwd->pw_gid, e->cmd));
815a0e
-				uname = e->pwd->pw_name;
815a0e
-			/* check if user exists in time of job is being run f.e. ldap */
815a0e
-			if (getpwnam(uname) != NULL) {
815a0e
 				time_t virtualSecond = (vtime - e->delay) * SECONDS_PER_MINUTE;
815a0e
 				time_t virtualGMTSecond = virtualSecond - vGMToff;
815a0e
 				job_tz = env_get("CRON_TZ", e->envp);
815a0e
 				maketime(job_tz, orig_tz);
815a0e
+
815a0e
 				/* here we test whether time is NOW */
815a0e
 				if (bit_test(e->minute, minute) &&
815a0e
 					bit_test(e->hour, hour) &&
815a0e
@@ -591,10 +585,9 @@ static void find_jobs(int vtime, cron_db
815a0e
 							!(e->flags & (MIN_STAR | HR_STAR))) ||
815a0e
 						(doWild && (e->flags & (MIN_STAR | HR_STAR))))
815a0e
 						job_add(e, u);	/*will add job, if it isn't in queue already for NOW. */
815a0e
-				}
815a0e
-			}
815a0e
-		}
815a0e
-	}
815a0e
+			        }
815a0e
+		        }
815a0e
+	        }
815a0e
 	if (orig_tz != NULL)
815a0e
 		setenv("TZ", orig_tz, 1);
815a0e
 	else
815a0e
diff -up cronie-1.4.11/src/database.c.refresh-users cronie-1.4.11/src/database.c
815a0e
--- cronie-1.4.11/src/database.c.refresh-users	2013-07-18 14:27:08.000000000 +0200
815a0e
+++ cronie-1.4.11/src/database.c	2015-04-21 15:20:03.768846359 +0200
815a0e
@@ -152,10 +152,41 @@ check_orphans(cron_db *db) {
815a0e
 	}
815a0e
 }
815a0e
 
815a0e
+static int
815a0e
+find_orphan(const char *uname, const char *fname, const char *tabname) {
815a0e
+	orphan *o;
815a0e
+
815a0e
+	for (o = orphans; o != NULL; o = o->next) {
815a0e
+		if (uname && o->uname) {
815a0e
+			if (strcmp(uname, o->uname) != 0)
815a0e
+				continue;
815a0e
+		} else if (uname != o->uname)
815a0e
+			continue;
815a0e
+
815a0e
+		if (fname && o->fname) {
815a0e
+			if (strcmp(fname, o->fname) != 0)
815a0e
+				continue;
815a0e
+		} else if (fname != o->fname)
815a0e
+			continue;
815a0e
+
815a0e
+		if (tabname && o->tabname) {
815a0e
+			if (strcmp(tabname, o->tabname) != 0)
815a0e
+				continue;
815a0e
+		} else if (tabname != o->tabname)
815a0e
+			continue;
815a0e
+		return 1;
815a0e
+	}
815a0e
+
815a0e
+	return 0;
815a0e
+}
815a0e
+
815a0e
 static void
815a0e
 add_orphan(const char *uname, const char *fname, const char *tabname) {
815a0e
 	orphan *o;
815a0e
 
815a0e
+	if (find_orphan(uname, fname, tabname))
815a0e
+		return;
815a0e
+
815a0e
 	o = calloc(1, sizeof(*o));
815a0e
 	if (o == NULL)
815a0e
 		return;
815a0e
diff -up cronie-1.4.11/src/entry.c.refresh-users cronie-1.4.11/src/entry.c
815a0e
--- cronie-1.4.11/src/entry.c.refresh-users	2013-07-18 14:27:08.000000000 +0200
815a0e
+++ cronie-1.4.11/src/entry.c	2015-04-21 14:40:13.473310662 +0200
815a0e
@@ -99,6 +99,7 @@ entry *load_entry(FILE * file, void (*er
815a0e
 	char envstr[MAX_ENVSTR];
815a0e
 	char **tenvp;
815a0e
 	char *p;
815a0e
+	struct passwd temppw;
815a0e
 
815a0e
 	Debug(DPARS, ("load_entry()...about to eat comments\n"));
815a0e
 
815a0e
@@ -286,11 +287,15 @@ entry *load_entry(FILE * file, void (*er
815a0e
 
815a0e
 		pw = getpwnam(username);
815a0e
 		if (pw == NULL) {
815a0e
-			ecode = e_username;
815a0e
-			goto eof;
815a0e
-		}
815a0e
-		Debug(DPARS, ("load_entry()...uid %ld, gid %ld\n",
815a0e
+			Debug(DPARS, ("load_entry()...unknown user entry\n"));
815a0e
+			memset(&temppw, 0, sizeof (temppw));
815a0e
+			temppw.pw_name = username;
815a0e
+			temppw.pw_passwd = "";
815a0e
+			pw = &temppw;
815a0e
+		} else {
815a0e
+			Debug(DPARS, ("load_entry()...uid %ld, gid %ld\n",
815a0e
 				(long) pw->pw_uid, (long) pw->pw_gid));
815a0e
+		}
815a0e
 	}
815a0e
 
815a0e
 	if ((e->pwd = pw_dup(pw)) == NULL) {
815a0e
@@ -331,17 +336,11 @@ entry *load_entry(FILE * file, void (*er
815a0e
 		else
815a0e
 			log_it("CRON", getpid(), "ERROR", "can't set SHELL", 0);
815a0e
 	}
815a0e
-	if (!env_get("HOME", e->envp)) {
815a0e
-		if (glue_strings(envstr, sizeof envstr, "HOME", pw->pw_dir, '=')) {
815a0e
-			if ((tenvp = env_set(e->envp, envstr)) == NULL) {
815a0e
-				ecode = e_memory;
815a0e
-				goto eof;
815a0e
-			}
815a0e
-			e->envp = tenvp;
815a0e
-		}
815a0e
-		else
815a0e
-			log_it("CRON", getpid(), "ERROR", "can't set HOME", 0);
815a0e
+	if ((tenvp = env_update_home(e->envp, pw->pw_dir)) == NULL) {
815a0e
+		ecode = e_memory;
815a0e
+		goto eof;
815a0e
 	}
815a0e
+	e->envp = tenvp;
815a0e
 #ifndef LOGIN_CAP
815a0e
 	/* If login.conf is in used we will get the default PATH later. */
815a0e
 	if (ChangePath && !env_get("PATH", e->envp)) {
815a0e
diff -up cronie-1.4.11/src/env.c.refresh-users cronie-1.4.11/src/env.c
815a0e
--- cronie-1.4.11/src/env.c.refresh-users	2013-07-18 14:27:08.000000000 +0200
815a0e
+++ cronie-1.4.11/src/env.c	2015-04-21 14:40:13.473310662 +0200
815a0e
@@ -25,6 +25,7 @@
815a0e
 #include <errno.h>
815a0e
 #include <stdlib.h>
815a0e
 #include <string.h>
815a0e
+#include <sys/types.h>
815a0e
 #include <unistd.h>
815a0e
 
815a0e
 #include "globals.h"
815a0e
@@ -291,3 +292,19 @@ char *env_get(const char *name, char **e
815a0e
 	}
815a0e
 	return (NULL);
815a0e
 }
815a0e
+
815a0e
+char **env_update_home(char **envp, const char *dir) {
815a0e
+	char envstr[MAX_ENVSTR];
815a0e
+
815a0e
+	if (dir == NULL || *dir == '\0' || env_get("HOME", envp)) {
815a0e
+		return envp;
815a0e
+	}
815a0e
+
815a0e
+	if (glue_strings(envstr, sizeof envstr, "HOME", dir, '=')) {
815a0e
+		envp = env_set(envp, envstr);
815a0e
+	}			
815a0e
+	else
815a0e
+		log_it("CRON", getpid(), "ERROR", "can't set HOME", 0);
815a0e
+
815a0e
+	return envp;
815a0e
+}
815a0e
diff -up cronie-1.4.11/src/funcs.h.refresh-users cronie-1.4.11/src/funcs.h
815a0e
--- cronie-1.4.11/src/funcs.h.refresh-users	2013-07-18 14:27:08.000000000 +0200
815a0e
+++ cronie-1.4.11/src/funcs.h	2015-04-21 14:40:13.473310662 +0200
815a0e
@@ -82,7 +82,8 @@ char		*env_get(const char *, char **),
815a0e
 		*first_word(const char *, const char *),
815a0e
 		**env_init(void),
815a0e
 		**env_copy(char **),
815a0e
-		**env_set(char **, const char *);
815a0e
+		**env_set(char **, const char *),
815a0e
+		**env_update_home(char **, const char *);
815a0e
 
815a0e
 user		*load_user(int, struct passwd *, const char *, const char *, const char *),
815a0e
 		*find_user(cron_db *, const char *, const char *);
815a0e
diff -up cronie-1.4.11/src/job.c.refresh-users cronie-1.4.11/src/job.c
815a0e
--- cronie-1.4.11/src/job.c.refresh-users	2013-07-18 14:27:08.000000000 +0200
815a0e
+++ cronie-1.4.11/src/job.c	2015-04-21 14:40:13.474310685 +0200
815a0e
@@ -22,6 +22,11 @@
815a0e
 #include "config.h"
815a0e
 
815a0e
 #include <stdlib.h>
815a0e
+#include <pwd.h>
815a0e
+#include <errno.h>
815a0e
+#include <sys/types.h>
815a0e
+#include <unistd.h>
815a0e
+#include <string.h>
815a0e
 
815a0e
 #include "funcs.h"
815a0e
 #include "globals.h"
815a0e
@@ -36,12 +41,42 @@ static job *jhead = NULL, *jtail = NULL;
815a0e
 
815a0e
 void job_add(entry * e, user * u) {
815a0e
 	job *j;
815a0e
+	struct passwd *newpwd;
815a0e
+	struct passwd *temppwd;
815a0e
+	const char *uname;
815a0e
 
815a0e
 	/* if already on queue, keep going */
815a0e
 	for (j = jhead; j != NULL; j = j->next)
815a0e
 		if (j->e == e && j->u == u)
815a0e
 			return;
815a0e
 
815a0e
+	uname = e->pwd->pw_name;
815a0e
+	/* check if user exists in time of job is being run f.e. ldap */
815a0e
+	if ((temppwd = getpwnam(uname)) != NULL) {
815a0e
+		char **tenvp;
815a0e
+
815a0e
+		Debug(DSCH | DEXT, ("user [%s:%ld:%ld:...] cmd=\"%s\"\n",
815a0e
+				e->pwd->pw_name, (long) temppwd->pw_uid,
815a0e
+				(long) temppwd->pw_gid, e->cmd));
815a0e
+		if ((newpwd = pw_dup(temppwd)) == NULL) {
815a0e
+			log_it(uname, getpid(), "ERROR", "memory allocation failed", errno);
815a0e
+			return;
815a0e
+		}
815a0e
+		free(e->pwd);
815a0e
+		e->pwd = newpwd;
815a0e
+
815a0e
+		if ((tenvp = env_update_home(e->envp, e->pwd->pw_dir)) == NULL) {
815a0e
+			log_it(uname, getpid(), "ERROR", "memory allocation failed", errno);
815a0e
+			return;
815a0e
+		}
815a0e
+		e->envp = tenvp;
815a0e
+	} else {
815a0e
+		log_it(uname, getpid(), "ERROR", "getpwnam() failed",errno);
815a0e
+		Debug(DSCH | DEXT, ("%s:%d pid=%d time=%ld getpwnam(%s) failed errno=%d error=%s\n",
815a0e
+			__FILE__,__LINE__,getpid(),time(NULL),uname,errno,strerror(errno)));
815a0e
+		return;
815a0e
+	}
815a0e
+
815a0e
 	/* build a job queue element */
815a0e
 	if ((j = (job *) malloc(sizeof (job))) == NULL)
815a0e
 		return;