changeset 13954:d0de3e428c42

374 cron should send more useful mail Reviewed by: Richard Lowe <richlowe@richlowe.net> Approved by: Dan McDonald <danmcd@nexenta.com>
author Joshua M. Clulow <josh@sysmgr.org>
date Mon, 21 Jan 2013 12:21:50 -0800
parents 0cc6917308f7
children be2bd4e678d9
files usr/src/cmd/cron/cron.c
diffstat 1 files changed, 31 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/cron/cron.c	Sun Feb 10 22:28:15 2013 -0800
+++ b/usr/src/cmd/cron/cron.c	Mon Jan 21 12:21:50 2013 -0800
@@ -21,6 +21,8 @@
 /*
  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright 2013 Joshua M. Clulow <josh@sysmgr.org>
  */
 
 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
@@ -145,7 +147,6 @@
 #define	CLOCK_DRIFT	"clock time drifted backwards after event!\n"
 #define	PIDERR		"unexpected pid returned %d (ignored)"
 #define	CRONTABERR	"Subject: Your crontab file has an error in it\n\n"
-#define	CRONOUT		"Subject: Output from \"cron\" command\n\n"
 #define	MALLOCERR	"out of space, cannot create new string\n"
 
 #define	DIDFORK didfork
@@ -2711,6 +2712,7 @@
 	int	nbytes;
 	char	iobuf[BUFSIZ];
 	char	*cmd;
+	char	*lowname = (pr->jobtype == CRONEVENT ? "cron" : "at");
 
 	(void) uname(&name);
 	if ((ruser_ids = getpwnam(p->name)) == NULL)
@@ -2724,31 +2726,40 @@
 	if (mailpipe == NULL)
 		exit(127);
 	(void) fprintf(mailpipe, "To: %s\n", p->name);
-	if (pr->jobtype == CRONEVENT) {
-		(void) fprintf(mailpipe, CRONOUT);
-		(void) fprintf(mailpipe, "Your \"cron\" job on %s\n",
-		    name.nodename);
-		if (pr->jobname != NULL) {
-			(void) fprintf(mailpipe, "%s\n\n", pr->jobname);
-		}
-	} else {
-		(void) fprintf(mailpipe, "Subject: Output from \"at\" job\n\n");
-		(void) fprintf(mailpipe, "Your \"at\" job on %s\n",
-		    name.nodename);
-		if (pr->jobname != NULL) {
-			(void) fprintf(mailpipe, "\"%s\"\n\n", pr->jobname);
-		}
-	}
-	/* Tmp. file is fopen'ed w/ "r",  secure open */
+	(void) fprintf(mailpipe, "Subject: %s <%s@%s> %s\n",
+	    (pr->jobtype == CRONEVENT ? "Cron" : "At"),
+	    p->name, name.nodename, pr->jobname);
+
+	/*
+	 * RFC3834 (Section 5) defines the Auto-Submitted header to prevent
+	 * vacation replies, et al, from being sent in response to
+	 * machine-generated mail.
+	 */
+	(void) fprintf(mailpipe, "Auto-Submitted: auto-generated\n");
+
+	/*
+	 * Additional headers for mail filtering and diagnostics:
+	 */
+	(void) fprintf(mailpipe, "X-Mailer: cron (%s %s)\n", name.sysname,
+	    name.release);
+	(void) fprintf(mailpipe, "X-Cron-User: %s\n", p->name);
+	(void) fprintf(mailpipe, "X-Cron-Host: %s\n", name.nodename);
+	(void) fprintf(mailpipe, "X-Cron-Job-Name: %s\n", pr->jobname);
+	(void) fprintf(mailpipe, "X-Cron-Job-Type: %s\n", lowname);
+
+	/*
+	 * Message Body:
+	 *
+	 * (Temporary file is fopen'ed with "r", secure open.)
+	 */
+	(void) fprintf(mailpipe, "\n");
 	if (filesize > 0 &&
 	    (st = fopen(pr->outfile, "r")) != NULL) {
-		(void) fprintf(mailpipe,
-		    "produced the following output:\n\n");
 		while ((nbytes = fread(iobuf, sizeof (char), BUFSIZ, st)) != 0)
 			(void) fwrite(iobuf, sizeof (char), nbytes, mailpipe);
 		(void) fclose(st);
 	} else {
-		(void) fprintf(mailpipe, "completed.\n");
+		(void) fprintf(mailpipe, "Job completed with no output.\n");
 	}
 	(void) pclose(mailpipe);
 	exit(0);