--- old/usr/src/cmd/cron/Makefile	Fri Jun 22 11:09:02 2012
+++ new/usr/src/cmd/cron/Makefile	Fri Jun 22 11:09:02 2012
@@ -72,7 +72,7 @@
 COMMONOBJ1=	permit.o
 COMMONOBJ2=	funcs.o
 COMMONOBJS=	$(COMMONOBJ1) $(COMMONOBJ2)
-CRONOBJS=	cron.o elm.o
+CRONOBJS=	cron.o elm.o cron_scf.o
 ATOBJS=		at.o att1.o att2.o
 XPG4OBJS=	values-xpg4.o
 ATRMOBJS1=	atrm.o
@@ -129,7 +129,7 @@
 at.xpg4 :=	LDLIBS += -lproject -lsecdb
 atq :=		LDLIBS += -lsecdb
 atrm :=		LDLIBS += -lsecdb
-cron :=		LDLIBS += -lpam -lproject -lcontract -lzoneinfo
+cron :=		LDLIBS += -lpam -lproject -lcontract -lzoneinfo -lscf
 crontab :=	LDLIBS += -lsecdb -lpam -lzoneinfo
 crontab.xpg6 :=	LDLIBS += -lsecdb -lpam -lzoneinfo
 crontab.xpg4 :=	LDLIBS += -lsecdb -lpam -lzoneinfo
--- old/usr/src/cmd/cron/cron.c	Fri Jun 22 11:09:03 2012
+++ new/usr/src/cmd/cron/cron.c	Fri Jun 22 11:09:03 2012
@@ -72,6 +72,7 @@
 #include <libzoneinfo.h>
 
 #include "cron.h"
+#include "cron_scf.h"
 
 /*
  * #define	DEBUG
@@ -146,6 +147,7 @@
 #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	CRONOUTNEW	"Subject: Cron <%s@%s>: %s\n\n"
 #define	MALLOCERR	"out of space, cannot create new string\n"
 
 #define	DIDFORK didfork
@@ -357,6 +359,12 @@
 static sigset_t		defmask, sigmask;
 
 /*
+ * Configuration from smf(5)
+ */
+static int legacy_subject;
+static int extra_headers;
+
+/*
  * BSM hooks
  */
 extern int	audit_cron_session(char *, char *, uid_t, gid_t, char *);
@@ -675,6 +683,7 @@
 	(void) fprintf(stderr, "in initialize\n");
 #endif
 	if (firstpass) {
+		int val;
 		/* for mail(1), make sure messages come from root */
 		if (putenv("LOGNAME=root") != 0) {
 			crabort("cannot expand env variable",
@@ -705,6 +714,21 @@
 			}
 			crabort("cannot start cron; FIFO exists", CONSOLE_MSG);
 		}
+		switch (init_scf()) {
+		case 0:
+			val = get_config_boolean("legacy_subject");
+			legacy_subject = (val < 0 ? 0 : val);
+			val = get_config_boolean("extra_headers");
+			extra_headers = (val < 0 ? 1 : val);
+			break;
+		case -2:
+			crabort("cron not running under smf(5)",
+			    REMOVE_FIFO|CONSOLE_MSG);
+			break;
+		default:
+			crabort("could not initialise libscf",
+			    REMOVE_FIFO|CONSOLE_MSG);
+		}
 	}
 
 	if ((msgfd = open(FIFO, O_RDWR)) < 0) {
@@ -2724,8 +2748,21 @@
 	if (mailpipe == NULL)
 		exit(127);
 	(void) fprintf(mailpipe, "To: %s\n", p->name);
+	if (extra_headers) {
+		(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",
+		    (pr->jobtype == CRONEVENT ? "cron" : "at"));
+	}
 	if (pr->jobtype == CRONEVENT) {
-		(void) fprintf(mailpipe, CRONOUT);
+		if (legacy_subject)
+			(void) fprintf(mailpipe, CRONOUT);
+		else
+			(void) fprintf(mailpipe, CRONOUTNEW,
+			    p->name, name.nodename, pr->jobname);
 		(void) fprintf(mailpipe, "Your \"cron\" job on %s\n",
 		    name.nodename);
 		if (pr->jobname != NULL) {
--- old/usr/src/cmd/cron/cron.xml	Fri Jun 22 11:09:03 2012
+++ new/usr/src/cmd/cron/cron.xml	Fri Jun 22 11:09:03 2012
@@ -97,6 +97,15 @@
 			value='solaris.smf.manage.cron' />
 	</property_group>
 
+	<property_group name='config' type='application'>
+		<!-- use legacy (static) subject string in mail? -->
+		<propval name='legacy_subject' type='boolean'
+			value='false' />
+		<!-- include X-Mailer, X-Cron-* headers in mail? -->
+		<propval name='extra_headers' type='boolean'
+			value='true' />
+	</property_group>
+
 	<instance name='default' enabled='false' />
 
 	<stability value='Unstable' />
--- /dev/null	Fri Jun 22 11:09:04 2012
+++ new/usr/src/cmd/cron/cron_scf.c	Fri Jun 22 11:09:04 2012
@@ -0,0 +1,171 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <err.h>
+
+#include <libscf.h>
+
+#include "cron_scf.h"
+
+extern void *xmalloc(size_t); /* from funcs.c */
+
+static char *fmri;
+static scf_handle_t *scf;
+static scf_instance_t *inst;
+static scf_propertygroup_t *pg;
+
+/*
+ * Initialise our connection to smf(5).  Returns:
+ *    0 on success
+ *   <0 for any failure
+ *   -2 if we believe we're not actually running under smf(5)
+ */
+int
+init_scf(void)
+{
+	int rc = -1;
+	size_t fmrisz;
+
+	scf = scf_handle_create(SCF_VERSION);
+	if (scf == NULL)
+		return (-1);
+	if (scf_handle_bind(scf) != 0)
+		goto cleanup0;
+
+#ifdef DEBUG
+	if (getenv("SMF_TEST_FMRI") != NULL) {
+		printf("DEBUG: smf test mode engaged\n");
+		fmri = strdup(getenv("SMF_TEST_FMRI"));
+		goto have_fmri;
+	}
+#endif /* DEBUG */
+
+	fmrisz = scf_myname(scf, NULL, 0);
+	if (fmrisz == -1) {
+		if (scf_error() == SCF_ERROR_NOT_SET) {
+			rc = -2;
+		}
+		goto cleanup1;
+	}
+	fmri = xmalloc(fmrisz + 1);
+	fmrisz = scf_myname(scf, fmri, fmrisz + 1);
+	if (fmrisz == -1)
+		goto cleanup2;
+
+have_fmri:
+
+	if ((inst = scf_instance_create(scf)) == NULL ||
+	    (pg = scf_pg_create(scf)) == NULL)
+		goto cleanup3;
+
+	if (scf_handle_decode_fmri(scf, fmri, NULL, NULL, inst,
+	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0)
+		goto cleanup3;
+
+	if (scf_instance_get_pg_composed(inst, NULL, "config", pg) != 0)
+		goto cleanup3;
+
+	return (0);
+
+cleanup3:
+	if (pg != NULL) {
+		scf_pg_destroy(pg);
+		pg = NULL;
+	}
+	if (inst != NULL) {
+		scf_instance_destroy(inst);
+		inst = NULL;
+	}
+cleanup2:
+	free(fmri);
+	fmri = NULL;
+cleanup1:
+	(void) scf_handle_unbind(scf);
+cleanup0:
+	(void) scf_handle_destroy(scf);
+	scf = NULL;
+	return (rc);
+}
+
+void
+fini_scf(void)
+{
+	(void) scf_pg_destroy(pg);
+	pg = NULL;
+	(void) scf_instance_destroy(inst);
+	inst = NULL;
+	free(fmri);
+	fmri = NULL;
+	(void) scf_handle_unbind(scf);
+	(void) scf_handle_destroy(scf);
+	scf = NULL;
+}
+
+/*
+ * Fetch the boolean value of a property from the 'config' property
+ * group in our smf(5) instance.
+ * Returns:
+ *   <0 on failure
+ *    0 if found and false
+ *    1 if found and true
+ */
+int
+get_config_boolean(char *name)
+{
+	scf_property_t *prop = NULL;
+	scf_value_t *val = NULL;
+	uint8_t out;
+	int rc = -1;
+
+	prop = scf_property_create(scf);
+	val = scf_value_create(scf);
+	if (prop == NULL || val == NULL)
+		goto cleanup0;
+
+	if (scf_pg_get_property(pg, name, prop) != 0)
+		goto cleanup0;
+
+	if (scf_property_is_type(prop, SCF_TYPE_BOOLEAN) != 0)
+		goto cleanup0;
+
+	if (scf_property_get_value(prop, val) != 0)
+		goto cleanup0;
+
+	if (scf_value_get_boolean(val, &out) != 0)
+		goto cleanup0;
+
+	rc = out;
+
+cleanup0:
+	if (val != NULL)
+		scf_value_destroy(val);
+	if (prop != NULL)
+		scf_property_destroy(prop);
+
+	return (rc);
+}
--- /dev/null	Fri Jun 22 11:09:04 2012
+++ new/usr/src/cmd/cron/cron_scf.h	Fri Jun 22 11:09:04 2012
@@ -0,0 +1,40 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
+ */
+
+#ifndef	_CRON_SCF_H
+#define	_CRON_SCF_H
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+int init_scf(void);
+void fini_scf(void);
+int get_config_boolean(char *);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* _CRON_SCF_H */
