Blame SOURCES/cronie-1.4.11-anacron-mailto.patch

304494
diff -up cronie-1.4.11/anacron/runjob.c.mailto cronie-1.4.11/anacron/runjob.c
304494
--- cronie-1.4.11/anacron/runjob.c.mailto	2013-07-18 14:27:08.000000000 +0200
304494
+++ cronie-1.4.11/anacron/runjob.c	2017-03-07 14:00:06.968348389 +0100
304494
@@ -88,10 +88,18 @@ static char *
304494
 username(void)
304494
 {
304494
     struct passwd *ps;
304494
+    static char *user;
304494
+
304494
+    if (user)
304494
+	return user;
304494
 
304494
     ps = getpwuid(geteuid());
304494
-    if (ps == NULL) die_e("getpwuid() error");
304494
-    return ps->pw_name;
304494
+    if (ps == NULL || ps->pw_name == NULL) die_e("getpwuid() error");
304494
+
304494
+    user = strdup(ps->pw_name);
304494
+    if (user == NULL) die_e("memory allocation error");
304494
+
304494
+    return user;
304494
 }
304494
 
304494
 static void
304494
@@ -167,6 +175,12 @@ launch_mailer(job_rec *jr)
304494
     pid_t pid;
304494
     struct stat buf;
304494
 
304494
+    if (jr->mailto == NULL)
304494
+    {
304494
+	explain("Empty MAILTO set, not mailing output");
304494
+	return;
304494
+    }
304494
+
304494
     /* Check that we have a way of sending mail. */
304494
     if(stat(SENDMAIL, &buf))
304494
     {
304494
@@ -245,14 +259,12 @@ launch_job(job_rec *jr)
304494
     }
304494
 
304494
     setup_env(jr);
304494
-   
304494
+
304494
     /* Get the destination email address if set, or current user otherwise */
304494
     mailto = getenv("MAILTO");
304494
 
304494
-    if (mailto)
304494
-	    jr->mailto = mailto;
304494
-    else
304494
-	    jr->mailto = username ();
304494
+    if (mailto == NULL)
304494
+	mailto = username();
304494
 
304494
     /* create temporary file for stdout and stderr of the job */
304494
     temp_file(jr); fd = jr->output_fd;
304494
@@ -262,11 +274,7 @@ launch_job(job_rec *jr)
304494
     xwrite(fd, username());
304494
     xwrite(fd, ">\n");
304494
     xwrite(fd, "To: ");
304494
-    if (mailto) {
304494
-       xwrite(fd, mailto);
304494
-    } else {
304494
-       xwrite(fd, username());
304494
-    }
304494
+    xwrite(fd, mailto);
304494
     xwrite(fd, "\n");
304494
     xwrite(fd, "Content-Type: text/plain; charset=\"");
304494
     xwrite(fd, nl_langinfo(CODESET));
304494
@@ -277,6 +285,12 @@ launch_job(job_rec *jr)
304494
     xwrite(fd, hostname);
304494
     xwrite(fd, "\n\n");
304494
 
304494
+    if (*mailto == '\0')
304494
+	jr->mailto = NULL;
304494
+    else
304494
+	/* ugly but works without strdup() */
304494
+	jr->mailto = mailto;
304494
+
304494
     jr->mail_header_size = file_size(fd);
304494
 
304494
     pid = xfork();
304494
@@ -305,7 +319,7 @@ tend_job(job_rec *jr, int status)
304494
     if (file_size(jr->output_fd) > jr->mail_header_size) mail_output = 1;
304494
     else mail_output = 0;
304494
 
304494
-    m = mail_output ? " (mailing output)" : "";
304494
+    m = mail_output ? " (produced output)" : "";
304494
     if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
304494
 	explain("Job `%s' terminated%s", jr->ident, m);
304494
     else if (WIFEXITED(status))