xref: /titanic_50/usr/src/lib/libbsm/common/audit_cron.c (revision d0fa49b78d1f40d84ec76c363cdc38cf128511dd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ec923578Sjf206706  * Common Development and Distribution License (the "License").
6ec923578Sjf206706  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22ec923578Sjf206706  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
287c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
297c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
307c478bd9Sstevel@tonic-gate #include <bsm/audit_uevents.h>
317c478bd9Sstevel@tonic-gate #include <bsm/audit_private.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <wait.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <pwd.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <syslog.h>
407c478bd9Sstevel@tonic-gate #include <sys/stat.h>
417c478bd9Sstevel@tonic-gate #include <sys/socket.h>
427c478bd9Sstevel@tonic-gate #include <netinet/in.h>
437c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
447c478bd9Sstevel@tonic-gate #include <libgen.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <locale.h>
477c478bd9Sstevel@tonic-gate #include "generic.h"
487c478bd9Sstevel@tonic-gate 
49*d0fa49b7STony Nguyen #define	F_AUID	"%u\n"
507c478bd9Sstevel@tonic-gate #define	F_SMASK	"%x\n"
517c478bd9Sstevel@tonic-gate #define	F_FMASK	"%x\n"
527c478bd9Sstevel@tonic-gate #define	F_PORT	"%lx\n"
537c478bd9Sstevel@tonic-gate #define	F_TYPE	"%x\n"
547c478bd9Sstevel@tonic-gate #define	F_MACH	"%x %x %x %x\n"
557c478bd9Sstevel@tonic-gate #define	F_ASID	"%u\n"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	AU_SUFFIX	".au"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	ANC_BAD_FILE	-1
607c478bd9Sstevel@tonic-gate #define	ANC_BAD_FORMAT	-2
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	AUDIT_CRON_TEXTBUF	256
637c478bd9Sstevel@tonic-gate static char	textbuf[AUDIT_CRON_TEXTBUF];
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate int
audit_cron_mode()667c478bd9Sstevel@tonic-gate audit_cron_mode()
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	return (!cannot_audit(0));
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static void
audit_cron_syslog(const char * message)727c478bd9Sstevel@tonic-gate audit_cron_syslog(const char *message) {
737c478bd9Sstevel@tonic-gate 	static	int	is_open = 0;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (!is_open) {
76ec923578Sjf206706 		openlog("Solaris_Audit", LOG_ODELAY, LOG_CRON);
777c478bd9Sstevel@tonic-gate 		is_open = 1;
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	syslog(LOG_WARNING, "%s", message);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * audit_cron_getinfo returns the audit characteristics from the relevant
847c478bd9Sstevel@tonic-gate  * auxiliary file, it if exists.  If not, it creates them from the crontab
857c478bd9Sstevel@tonic-gate  * or atjob uid.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static int
audit_cron_getinfo(char * fname,char * fname_aux,struct auditinfo_addr * info)897c478bd9Sstevel@tonic-gate audit_cron_getinfo(char *fname, char *fname_aux, struct auditinfo_addr *info)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	int		fd;
927c478bd9Sstevel@tonic-gate 	struct stat	st;
937c478bd9Sstevel@tonic-gate 	au_mask_t mask;
947c478bd9Sstevel@tonic-gate 	struct passwd	pwd;
957c478bd9Sstevel@tonic-gate 	char		pwd_buff[1024];
967c478bd9Sstevel@tonic-gate 	static char	*msg =
977c478bd9Sstevel@tonic-gate 	    "Used defaults instead of ancilary audit file";
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if ((fd = open(fname_aux, O_RDONLY)) == -1) {
1007c478bd9Sstevel@tonic-gate 		/* no syslog here; common case */
1017c478bd9Sstevel@tonic-gate 		goto make_it_up;
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 	if (fstat(fd, &st) == -1) {
1047c478bd9Sstevel@tonic-gate 		/* no syslog here either; common case */
1057c478bd9Sstevel@tonic-gate 		goto delete_first;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (read(fd, textbuf, st.st_size) != st.st_size) {
1097c478bd9Sstevel@tonic-gate 		audit_cron_syslog(msg);
1107c478bd9Sstevel@tonic-gate 		goto delete_first;
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (sscanf(textbuf,
1147c478bd9Sstevel@tonic-gate 	    F_AUID
1157c478bd9Sstevel@tonic-gate 	    F_SMASK
1167c478bd9Sstevel@tonic-gate 	    F_FMASK
1177c478bd9Sstevel@tonic-gate 	    F_PORT
1187c478bd9Sstevel@tonic-gate 	    F_TYPE
1197c478bd9Sstevel@tonic-gate 	    F_MACH
1207c478bd9Sstevel@tonic-gate 	    F_ASID,
121*d0fa49b7STony Nguyen 	    &(info->ai_auid),
1227c478bd9Sstevel@tonic-gate 	    &(info->ai_mask.am_success),
1237c478bd9Sstevel@tonic-gate 	    &(info->ai_mask.am_failure),
1247c478bd9Sstevel@tonic-gate 	    &(info->ai_termid.at_port),
1257c478bd9Sstevel@tonic-gate 	    &(info->ai_termid.at_type),
1267c478bd9Sstevel@tonic-gate 	    &(info->ai_termid.at_addr[0]),
1277c478bd9Sstevel@tonic-gate 	    &(info->ai_termid.at_addr[1]),
1287c478bd9Sstevel@tonic-gate 	    &(info->ai_termid.at_addr[2]),
1297c478bd9Sstevel@tonic-gate 	    &(info->ai_termid.at_addr[3]),
130*d0fa49b7STony Nguyen 	    &(info->ai_asid)) != 10) {
1317c478bd9Sstevel@tonic-gate 		audit_cron_syslog(msg);
1327c478bd9Sstevel@tonic-gate 		goto delete_first;
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	(void) close(fd);
1357c478bd9Sstevel@tonic-gate 	return (0);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate delete_first:
1387c478bd9Sstevel@tonic-gate 	(void) close(fd);
1397c478bd9Sstevel@tonic-gate 	if (unlink(fname_aux)) {
1407c478bd9Sstevel@tonic-gate 		if (errno != ENOENT)
1417c478bd9Sstevel@tonic-gate 			audit_cron_syslog(
1427c478bd9Sstevel@tonic-gate 			    "Failed to remove invalid ancilary audit file");
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 	/* intentionally falls through */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate make_it_up:
1477c478bd9Sstevel@tonic-gate 	if (stat(fname, &st))
1487c478bd9Sstevel@tonic-gate 		return (-1);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/* port and IP are zero */
1517c478bd9Sstevel@tonic-gate 	(void) memset(&(info->ai_termid), 0, sizeof (au_tid_addr_t));
1527c478bd9Sstevel@tonic-gate 	info->ai_termid.at_type = AU_IPv4;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/* the caller is the child of cron which will run the job. */
1557c478bd9Sstevel@tonic-gate 	info->ai_asid = getpid();
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	info->ai_mask.am_success = 0;	/* cover error case */
1587c478bd9Sstevel@tonic-gate 	info->ai_mask.am_failure = 0;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (strstr(fname, "crontabs") != NULL) {
1617c478bd9Sstevel@tonic-gate 		if (getpwnam_r(basename(fname), &pwd, pwd_buff,
1627c478bd9Sstevel@tonic-gate 		    sizeof (pwd_buff)) == NULL)
1637c478bd9Sstevel@tonic-gate 			return (-1); /* getpwnam_r sets errno */
1647c478bd9Sstevel@tonic-gate 	} else {
1657c478bd9Sstevel@tonic-gate 		if (getpwuid_r(st.st_uid, &pwd, pwd_buff, sizeof (pwd_buff)) ==
1667c478bd9Sstevel@tonic-gate 		    NULL)
1677c478bd9Sstevel@tonic-gate 			return (-1); /* getpwuid_r sets errno */
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	info->ai_auid = pwd.pw_uid;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if (au_user_mask(pwd.pw_name, &mask)) {
1737c478bd9Sstevel@tonic-gate 		errno = EINVAL; /* pw_name lookup failed */
1747c478bd9Sstevel@tonic-gate 		return (-1);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 	info->ai_mask.am_success = mask.am_success;
1777c478bd9Sstevel@tonic-gate 	info->ai_mask.am_failure = mask.am_failure;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	return (0);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate int
audit_cron_setinfo(char * fname,struct auditinfo_addr * info)1837c478bd9Sstevel@tonic-gate audit_cron_setinfo(char *fname, struct auditinfo_addr *info)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	int		fd, len, r;
1867c478bd9Sstevel@tonic-gate 	int		save_err;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	r = chmod(fname, 0200);
1897c478bd9Sstevel@tonic-gate 	if (r == -1 && errno != ENOENT)
1907c478bd9Sstevel@tonic-gate 		return (-1);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if ((fd = open(fname, O_CREAT|O_WRONLY|O_TRUNC, 0200)) == -1)
1937c478bd9Sstevel@tonic-gate 		return (-1);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	len = sprintf(textbuf,
1967c478bd9Sstevel@tonic-gate 	    F_AUID
1977c478bd9Sstevel@tonic-gate 	    F_SMASK
1987c478bd9Sstevel@tonic-gate 	    F_FMASK
1997c478bd9Sstevel@tonic-gate 	    F_PORT
2007c478bd9Sstevel@tonic-gate 	    F_TYPE
2017c478bd9Sstevel@tonic-gate 	    F_MACH
2027c478bd9Sstevel@tonic-gate 	    F_ASID,
203*d0fa49b7STony Nguyen 	    info->ai_auid,
2047c478bd9Sstevel@tonic-gate 	    info->ai_mask.am_success,
2057c478bd9Sstevel@tonic-gate 	    info->ai_mask.am_failure,
2067c478bd9Sstevel@tonic-gate 	    info->ai_termid.at_port,
2077c478bd9Sstevel@tonic-gate 	    info->ai_termid.at_type,
2087c478bd9Sstevel@tonic-gate 	    info->ai_termid.at_addr[0],
2097c478bd9Sstevel@tonic-gate 	    info->ai_termid.at_addr[1],
2107c478bd9Sstevel@tonic-gate 	    info->ai_termid.at_addr[2],
2117c478bd9Sstevel@tonic-gate 	    info->ai_termid.at_addr[3],
212*d0fa49b7STony Nguyen 	    info->ai_asid);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (write(fd, textbuf, len) != len)
2157c478bd9Sstevel@tonic-gate 		goto audit_setinfo_clean;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (fchmod(fd, 0400) == -1)
2187c478bd9Sstevel@tonic-gate 		goto audit_setinfo_clean;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	(void) close(fd);
2217c478bd9Sstevel@tonic-gate 	return (0);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate audit_setinfo_clean:
2247c478bd9Sstevel@tonic-gate 	save_err = errno;
2257c478bd9Sstevel@tonic-gate 	(void) close(fd);
2267c478bd9Sstevel@tonic-gate 	(void) unlink(fname);
2277c478bd9Sstevel@tonic-gate 	errno = save_err;
2287c478bd9Sstevel@tonic-gate 	return (-1);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate char *
audit_cron_make_anc_name(char * fname)2327c478bd9Sstevel@tonic-gate audit_cron_make_anc_name(char *fname)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	char *anc_name;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	anc_name = (char *)malloc(strlen(fname) + strlen(AU_SUFFIX) + 1);
2377c478bd9Sstevel@tonic-gate 	if (anc_name == NULL)
2387c478bd9Sstevel@tonic-gate 		return (NULL);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	(void) strcpy(anc_name, fname);
2417c478bd9Sstevel@tonic-gate 	(void) strcat(anc_name, AU_SUFFIX);
2427c478bd9Sstevel@tonic-gate 	return (anc_name);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate int
audit_cron_is_anc_name(char * name)2467c478bd9Sstevel@tonic-gate audit_cron_is_anc_name(char *name)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	int	pos;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	pos = strlen(name) - strlen(AU_SUFFIX);
2517c478bd9Sstevel@tonic-gate 	if (pos <= 0)
2527c478bd9Sstevel@tonic-gate 		return (0);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (strcmp(name + pos, AU_SUFFIX) == 0)
2557c478bd9Sstevel@tonic-gate 		return (1);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	return (0);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static void
audit_cron_session_failure(char * name,int type,char * err_str)2617c478bd9Sstevel@tonic-gate audit_cron_session_failure(char *name, int type, char *err_str)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	const char	*mess;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (type == 0)
2667c478bd9Sstevel@tonic-gate 		mess = dgettext(bsm_dom,
2677c478bd9Sstevel@tonic-gate 		"at-job session for user %s failed: ancillary file: %s");
2687c478bd9Sstevel@tonic-gate 	else
2697c478bd9Sstevel@tonic-gate 		mess = dgettext(bsm_dom,
2707c478bd9Sstevel@tonic-gate 		"crontab job session for user %s failed: ancillary file: %s");
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	(void) snprintf(textbuf, sizeof (textbuf), mess, name, err_str);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	aug_save_event(AUE_cron_invoke);
2757c478bd9Sstevel@tonic-gate 	aug_save_sorf(4);
2767c478bd9Sstevel@tonic-gate 	aug_save_text(textbuf);
2777c478bd9Sstevel@tonic-gate 	(void) aug_audit();
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate int
audit_cron_session(char * name,char * path,uid_t uid,gid_t gid,char * at_jobname)2827c478bd9Sstevel@tonic-gate audit_cron_session(
2837c478bd9Sstevel@tonic-gate 		char *name,
2847c478bd9Sstevel@tonic-gate 		char *path,
2857c478bd9Sstevel@tonic-gate 		uid_t uid,
2867c478bd9Sstevel@tonic-gate 		gid_t gid,
2877c478bd9Sstevel@tonic-gate 		char *at_jobname)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	struct auditinfo_addr	info;
2907c478bd9Sstevel@tonic-gate 	au_mask_t		mask;
2917c478bd9Sstevel@tonic-gate 	char			*anc_file, *fname;
2927c478bd9Sstevel@tonic-gate 	int			r = 0;
2937c478bd9Sstevel@tonic-gate 	char			full_path[PATH_MAX];
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (cannot_audit(0)) {
2967c478bd9Sstevel@tonic-gate 		return (0);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/* get auditinfo from ancillary file */
3007c478bd9Sstevel@tonic-gate 	if (at_jobname == NULL) {
3017c478bd9Sstevel@tonic-gate 		/*
3027c478bd9Sstevel@tonic-gate 		 *	this is a cron-event, so we can get
3037c478bd9Sstevel@tonic-gate 		 *	filename from "name" arg
3047c478bd9Sstevel@tonic-gate 		 */
3057c478bd9Sstevel@tonic-gate 		fname = name;
3067c478bd9Sstevel@tonic-gate 		if (path != NULL) {
3077c478bd9Sstevel@tonic-gate 			if (strlen(path) + strlen(fname) + 2 > PATH_MAX) {
3087c478bd9Sstevel@tonic-gate 				errno = ENAMETOOLONG;
3097c478bd9Sstevel@tonic-gate 				r = -1;
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 			(void) strcat(strcat(strcpy(full_path, path), "/"),
3127c478bd9Sstevel@tonic-gate 			    fname);
3137c478bd9Sstevel@tonic-gate 			fname = full_path;
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 	} else {
3167c478bd9Sstevel@tonic-gate 		/* this is an at-event, use "at_jobname" */
3177c478bd9Sstevel@tonic-gate 		fname = at_jobname;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (r == 0) {
3217c478bd9Sstevel@tonic-gate 		anc_file = audit_cron_make_anc_name(fname);
3227c478bd9Sstevel@tonic-gate 		if (anc_file == NULL) {
3237c478bd9Sstevel@tonic-gate 			r = -1;
3247c478bd9Sstevel@tonic-gate 		} else {
3257c478bd9Sstevel@tonic-gate 			r = audit_cron_getinfo(fname, anc_file, &info);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (r != 0) {
3307c478bd9Sstevel@tonic-gate 		char *err_str;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		if (r == ANC_BAD_FORMAT)
3337c478bd9Sstevel@tonic-gate 			err_str = dgettext(bsm_dom, "bad format");
3347c478bd9Sstevel@tonic-gate 		else
3357c478bd9Sstevel@tonic-gate 			err_str = strerror(errno);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		audit_cron_session_failure(name,
3387c478bd9Sstevel@tonic-gate 		    at_jobname == NULL,
3397c478bd9Sstevel@tonic-gate 		    err_str);
3407c478bd9Sstevel@tonic-gate 		if (anc_file != NULL)
3417c478bd9Sstevel@tonic-gate 			free(anc_file);
3427c478bd9Sstevel@tonic-gate 		return (r);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	free(anc_file);
3467c478bd9Sstevel@tonic-gate 	aug_init();
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/* get current audit masks */
3497c478bd9Sstevel@tonic-gate 	if (au_user_mask(name, &mask) == 0) {
3507c478bd9Sstevel@tonic-gate 		info.ai_mask.am_success  |= mask.am_success;
3517c478bd9Sstevel@tonic-gate 		info.ai_mask.am_failure  |= mask.am_failure;
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/* save audit attributes for further use in current process */
3557c478bd9Sstevel@tonic-gate 	aug_save_auid(info.ai_auid);
3567c478bd9Sstevel@tonic-gate 	aug_save_asid(info.ai_asid);
3577c478bd9Sstevel@tonic-gate 	aug_save_tid_ex(info.ai_termid.at_port, info.ai_termid.at_addr,
3587c478bd9Sstevel@tonic-gate 	    info.ai_termid.at_type);
3597c478bd9Sstevel@tonic-gate 	aug_save_pid(getpid());
3607c478bd9Sstevel@tonic-gate 	aug_save_uid(uid);
3617c478bd9Sstevel@tonic-gate 	aug_save_gid(gid);
3627c478bd9Sstevel@tonic-gate 	aug_save_euid(uid);
3637c478bd9Sstevel@tonic-gate 	aug_save_egid(gid);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/* set mixed audit masks */
3667c478bd9Sstevel@tonic-gate 	return (setaudit_addr(&info, sizeof (info)));
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * audit_cron_new_job - create audit record with an information
3717c478bd9Sstevel@tonic-gate  *			about new job started by cron.
3727c478bd9Sstevel@tonic-gate  *	args:
3737c478bd9Sstevel@tonic-gate  *	cmd  - command being run by cron daemon.
3747c478bd9Sstevel@tonic-gate  *	type - type of job (0 - at-job, 1 - crontab job).
3757c478bd9Sstevel@tonic-gate  *	event - not used. pointer to cron event structure.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3787c478bd9Sstevel@tonic-gate void
audit_cron_new_job(char * cmd,int type,void * event)3797c478bd9Sstevel@tonic-gate audit_cron_new_job(char *cmd, int type, void *event)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	if (cannot_audit(0))
3827c478bd9Sstevel@tonic-gate 		return;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (type == 0) {
3857c478bd9Sstevel@tonic-gate 		(void) snprintf(textbuf, sizeof (textbuf),
3867c478bd9Sstevel@tonic-gate 		    dgettext(bsm_dom, "at-job"));
3877c478bd9Sstevel@tonic-gate 	} else if (type == 1) {
3887c478bd9Sstevel@tonic-gate 		(void) snprintf(textbuf, sizeof (textbuf),
3897c478bd9Sstevel@tonic-gate 		    dgettext(bsm_dom, "batch-job"));
3907c478bd9Sstevel@tonic-gate 	} else if (type == 2) {
3917c478bd9Sstevel@tonic-gate 		(void) snprintf(textbuf, sizeof (textbuf),
3927c478bd9Sstevel@tonic-gate 		    dgettext(bsm_dom, "crontab-job"));
3937c478bd9Sstevel@tonic-gate 	} else if ((type > 2) && (type <= 25)) {	/* 25 from cron.h */
3947c478bd9Sstevel@tonic-gate 		(void) snprintf(textbuf, sizeof (textbuf),
3957c478bd9Sstevel@tonic-gate 		    dgettext(bsm_dom, "queue-job (%c)"), (type+'a'));
3967c478bd9Sstevel@tonic-gate 	} else {
3977c478bd9Sstevel@tonic-gate 		(void) snprintf(textbuf, sizeof (textbuf),
3987c478bd9Sstevel@tonic-gate 		    dgettext(bsm_dom, "unknown job type (%d)"), type);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	aug_save_event(AUE_cron_invoke);
4027c478bd9Sstevel@tonic-gate 	aug_save_sorf(0);
4037c478bd9Sstevel@tonic-gate 	aug_save_text(textbuf);
4047c478bd9Sstevel@tonic-gate 	aug_save_text1(cmd);
4057c478bd9Sstevel@tonic-gate 	(void) aug_audit();
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate void
audit_cron_bad_user(char * name)4097c478bd9Sstevel@tonic-gate audit_cron_bad_user(char *name)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	if (cannot_audit(0))
4127c478bd9Sstevel@tonic-gate 		return;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	(void) snprintf(textbuf, sizeof (textbuf),
4157c478bd9Sstevel@tonic-gate 	    dgettext(bsm_dom, "bad user %s"), name);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	aug_save_event(AUE_cron_invoke);
4187c478bd9Sstevel@tonic-gate 	aug_save_sorf(2);
4197c478bd9Sstevel@tonic-gate 	aug_save_text(textbuf);
4207c478bd9Sstevel@tonic-gate 	(void) aug_audit();
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate void
audit_cron_user_acct_expired(char * name)4247c478bd9Sstevel@tonic-gate audit_cron_user_acct_expired(char *name)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	if (cannot_audit(0))
4277c478bd9Sstevel@tonic-gate 		return;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	(void) snprintf(textbuf, sizeof (textbuf),
4307c478bd9Sstevel@tonic-gate 	    dgettext(bsm_dom,
4317c478bd9Sstevel@tonic-gate 	    "user %s account expired"), name);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	aug_save_event(AUE_cron_invoke);
4347c478bd9Sstevel@tonic-gate 	aug_save_sorf(3);
4357c478bd9Sstevel@tonic-gate 	aug_save_text(textbuf);
4367c478bd9Sstevel@tonic-gate 	(void) aug_audit();
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate int
audit_cron_create_anc_file(char * name,char * path,char * uname,uid_t uid)4407c478bd9Sstevel@tonic-gate audit_cron_create_anc_file(char *name, char *path, char *uname, uid_t uid)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	au_mask_t	msk;
4437c478bd9Sstevel@tonic-gate 	auditinfo_addr_t ai;
4447c478bd9Sstevel@tonic-gate 	int		pid;
4457c478bd9Sstevel@tonic-gate 	char		*anc_name;
4467c478bd9Sstevel@tonic-gate 	char		full_path[PATH_MAX];
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (cannot_audit(0))
4497c478bd9Sstevel@tonic-gate 		return (0);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (name == NULL)
4527c478bd9Sstevel@tonic-gate 		return (0);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	if (path != NULL) {
4557c478bd9Sstevel@tonic-gate 		if (strlen(path) + strlen(name) + 2 > PATH_MAX)
4567c478bd9Sstevel@tonic-gate 			return (-1);
4577c478bd9Sstevel@tonic-gate 		(void) strcat(strcat(strcpy(full_path, path), "/"), name);
4587c478bd9Sstevel@tonic-gate 		name = full_path;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	anc_name = audit_cron_make_anc_name(name);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (access(anc_name, F_OK) != 0) {
4637c478bd9Sstevel@tonic-gate 		if (au_user_mask(uname, &msk) != 0) {
4647c478bd9Sstevel@tonic-gate 			free(anc_name);
4657c478bd9Sstevel@tonic-gate 			return (-1);
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		ai.ai_mask = msk;
4697c478bd9Sstevel@tonic-gate 		ai.ai_auid = uid;
4707c478bd9Sstevel@tonic-gate 		ai.ai_termid.at_port = 0;
4717c478bd9Sstevel@tonic-gate 		ai.ai_termid.at_type = AU_IPv4;
4727c478bd9Sstevel@tonic-gate 		ai.ai_termid.at_addr[0] = 0;
4737c478bd9Sstevel@tonic-gate 		ai.ai_termid.at_addr[1] = 0;
4747c478bd9Sstevel@tonic-gate 		ai.ai_termid.at_addr[2] = 0;
4757c478bd9Sstevel@tonic-gate 		ai.ai_termid.at_addr[3] = 0;
4767c478bd9Sstevel@tonic-gate 		/* generate new pid to use it as asid */
4777c478bd9Sstevel@tonic-gate 		pid = vfork();
4787c478bd9Sstevel@tonic-gate 		if (pid == -1) {
4797c478bd9Sstevel@tonic-gate 			free(anc_name);
4807c478bd9Sstevel@tonic-gate 			return (-1);
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 		if (pid == 0)
4837c478bd9Sstevel@tonic-gate 			exit(0);
4847c478bd9Sstevel@tonic-gate 		else {
4857c478bd9Sstevel@tonic-gate 		/*
4867c478bd9Sstevel@tonic-gate 		 * we need to clear status of children for
4877c478bd9Sstevel@tonic-gate 		 * wait() call in "cron"
4887c478bd9Sstevel@tonic-gate 		 */
4897c478bd9Sstevel@tonic-gate 			int lock;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 			(void) waitpid(pid, &lock, 0);
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 		ai.ai_asid = pid;
4947c478bd9Sstevel@tonic-gate 		if (audit_cron_setinfo(anc_name, &ai) != 0) {
4957c478bd9Sstevel@tonic-gate 			free(anc_name);
4967c478bd9Sstevel@tonic-gate 			return (-1);
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	free(anc_name);
5017c478bd9Sstevel@tonic-gate 	return (0);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate int
audit_cron_delete_anc_file(char * name,char * path)5057c478bd9Sstevel@tonic-gate audit_cron_delete_anc_file(char *name, char *path)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	char	*anc_name;
5087c478bd9Sstevel@tonic-gate 	char	full_path[PATH_MAX];
5097c478bd9Sstevel@tonic-gate 	int	r;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (name == NULL)
5127c478bd9Sstevel@tonic-gate 		return (0);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (path != NULL) {
5157c478bd9Sstevel@tonic-gate 		if (strlen(path) + strlen(name) + 2 > PATH_MAX)
5167c478bd9Sstevel@tonic-gate 			return (-1);
5177c478bd9Sstevel@tonic-gate 		(void) strcat(strcat(strcpy(full_path, path), "/"), name);
5187c478bd9Sstevel@tonic-gate 		name = full_path;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	anc_name = audit_cron_make_anc_name(name);
5217c478bd9Sstevel@tonic-gate 	r = unlink(anc_name);
5227c478bd9Sstevel@tonic-gate 	free(anc_name);
5237c478bd9Sstevel@tonic-gate 	return (r);
5247c478bd9Sstevel@tonic-gate }
525