1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <sys/types.h>
29*0Sstevel@tonic-gate #include <sys/systeminfo.h>
30*0Sstevel@tonic-gate #include <bsm/audit.h>
31*0Sstevel@tonic-gate #include <bsm/libbsm.h>
32*0Sstevel@tonic-gate #include <bsm/audit_uevents.h>
33*0Sstevel@tonic-gate #include <bsm/audit_private.h>
34*0Sstevel@tonic-gate #include <unistd.h>
35*0Sstevel@tonic-gate #include <wait.h>
36*0Sstevel@tonic-gate #include <fcntl.h>
37*0Sstevel@tonic-gate #include <pwd.h>
38*0Sstevel@tonic-gate #include <string.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <errno.h>
41*0Sstevel@tonic-gate #include <syslog.h>
42*0Sstevel@tonic-gate #include <sys/stat.h>
43*0Sstevel@tonic-gate #include <sys/socket.h>
44*0Sstevel@tonic-gate #include <netinet/in.h>
45*0Sstevel@tonic-gate #include <arpa/inet.h>
46*0Sstevel@tonic-gate #include <libgen.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include <locale.h>
49*0Sstevel@tonic-gate #include "generic.h"
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #define	F_AUID	"%d\n"
52*0Sstevel@tonic-gate #define	F_SMASK	"%x\n"
53*0Sstevel@tonic-gate #define	F_FMASK	"%x\n"
54*0Sstevel@tonic-gate #define	F_PORT	"%lx\n"
55*0Sstevel@tonic-gate #define	F_TYPE	"%x\n"
56*0Sstevel@tonic-gate #define	F_MACH	"%x %x %x %x\n"
57*0Sstevel@tonic-gate #define	F_ASID	"%u\n"
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #define	AU_SUFFIX	".au"
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #define	ANC_BAD_FILE	-1
62*0Sstevel@tonic-gate #define	ANC_BAD_FORMAT	-2
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate #define	AUDIT_CRON_TEXTBUF	256
65*0Sstevel@tonic-gate static char	textbuf[AUDIT_CRON_TEXTBUF];
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate int
68*0Sstevel@tonic-gate audit_cron_mode()
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	return (!cannot_audit(0));
71*0Sstevel@tonic-gate }
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static void
74*0Sstevel@tonic-gate audit_cron_syslog(const char *message) {
75*0Sstevel@tonic-gate 	static	int	is_open = 0;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	if (!is_open) {
78*0Sstevel@tonic-gate 		openlog("BSM-audit", LOG_ODELAY, LOG_CRON);
79*0Sstevel@tonic-gate 		is_open = 1;
80*0Sstevel@tonic-gate 	}
81*0Sstevel@tonic-gate 	syslog(LOG_WARNING, "%s", message);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate /*
85*0Sstevel@tonic-gate  * audit_cron_getinfo returns the audit characteristics from the relevant
86*0Sstevel@tonic-gate  * auxiliary file, it if exists.  If not, it creates them from the crontab
87*0Sstevel@tonic-gate  * or atjob uid.
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate static int
91*0Sstevel@tonic-gate audit_cron_getinfo(char *fname, char *fname_aux, struct auditinfo_addr *info)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	int		fd;
94*0Sstevel@tonic-gate 	struct stat	st;
95*0Sstevel@tonic-gate 	au_mask_t mask;
96*0Sstevel@tonic-gate 	struct passwd	pwd;
97*0Sstevel@tonic-gate 	char		pwd_buff[1024];
98*0Sstevel@tonic-gate 	static char	*msg =
99*0Sstevel@tonic-gate 	    "Used defaults instead of ancilary audit file";
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	if ((fd = open(fname_aux, O_RDONLY)) == -1) {
102*0Sstevel@tonic-gate 		/* no syslog here; common case */
103*0Sstevel@tonic-gate 		goto make_it_up;
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	if (fstat(fd, &st) == -1) {
106*0Sstevel@tonic-gate 		/* no syslog here either; common case */
107*0Sstevel@tonic-gate 		goto delete_first;
108*0Sstevel@tonic-gate 	}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	if (read(fd, textbuf, st.st_size) != st.st_size) {
111*0Sstevel@tonic-gate 		audit_cron_syslog(msg);
112*0Sstevel@tonic-gate 		goto delete_first;
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if (sscanf(textbuf,
116*0Sstevel@tonic-gate 			F_AUID
117*0Sstevel@tonic-gate 			F_SMASK
118*0Sstevel@tonic-gate 			F_FMASK
119*0Sstevel@tonic-gate 			F_PORT
120*0Sstevel@tonic-gate 			F_TYPE
121*0Sstevel@tonic-gate 			F_MACH
122*0Sstevel@tonic-gate 			F_ASID,
123*0Sstevel@tonic-gate 				(int *)&(info->ai_auid),
124*0Sstevel@tonic-gate 				&(info->ai_mask.am_success),
125*0Sstevel@tonic-gate 				&(info->ai_mask.am_failure),
126*0Sstevel@tonic-gate 				&(info->ai_termid.at_port),
127*0Sstevel@tonic-gate 				&(info->ai_termid.at_type),
128*0Sstevel@tonic-gate 				&(info->ai_termid.at_addr[0]),
129*0Sstevel@tonic-gate 				&(info->ai_termid.at_addr[1]),
130*0Sstevel@tonic-gate 				&(info->ai_termid.at_addr[2]),
131*0Sstevel@tonic-gate 				&(info->ai_termid.at_addr[3]),
132*0Sstevel@tonic-gate 				(unsigned int *)&(info->ai_asid)) != 10) {
133*0Sstevel@tonic-gate 		audit_cron_syslog(msg);
134*0Sstevel@tonic-gate 		goto delete_first;
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 	(void) close(fd);
137*0Sstevel@tonic-gate 	return (0);
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate delete_first:
140*0Sstevel@tonic-gate 	(void) close(fd);
141*0Sstevel@tonic-gate 	if (unlink(fname_aux)) {
142*0Sstevel@tonic-gate 		if (errno != ENOENT)
143*0Sstevel@tonic-gate 			audit_cron_syslog(
144*0Sstevel@tonic-gate 			    "Failed to remove invalid ancilary audit file");
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 	/* intentionally falls through */
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate make_it_up:
149*0Sstevel@tonic-gate 	if (stat(fname, &st))
150*0Sstevel@tonic-gate 		return (-1);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	/* port and IP are zero */
153*0Sstevel@tonic-gate 	(void) memset(&(info->ai_termid), 0, sizeof (au_tid_addr_t));
154*0Sstevel@tonic-gate 	info->ai_termid.at_type = AU_IPv4;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/* the caller is the child of cron which will run the job. */
157*0Sstevel@tonic-gate 	info->ai_asid = getpid();
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	info->ai_mask.am_success = 0;	/* cover error case */
160*0Sstevel@tonic-gate 	info->ai_mask.am_failure = 0;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	if (strstr(fname, "crontabs") != NULL) {
163*0Sstevel@tonic-gate 		if (getpwnam_r(basename(fname), &pwd, pwd_buff,
164*0Sstevel@tonic-gate 		    sizeof (pwd_buff)) == NULL)
165*0Sstevel@tonic-gate 			return (-1); /* getpwnam_r sets errno */
166*0Sstevel@tonic-gate 	} else {
167*0Sstevel@tonic-gate 		if (getpwuid_r(st.st_uid, &pwd, pwd_buff, sizeof (pwd_buff)) ==
168*0Sstevel@tonic-gate 		    NULL)
169*0Sstevel@tonic-gate 			return (-1); /* getpwuid_r sets errno */
170*0Sstevel@tonic-gate 	}
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	info->ai_auid = pwd.pw_uid;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	if (au_user_mask(pwd.pw_name, &mask)) {
175*0Sstevel@tonic-gate 		errno = EINVAL; /* pw_name lookup failed */
176*0Sstevel@tonic-gate 		return (-1);
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate 	info->ai_mask.am_success = mask.am_success;
179*0Sstevel@tonic-gate 	info->ai_mask.am_failure = mask.am_failure;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	return (0);
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate int
185*0Sstevel@tonic-gate audit_cron_setinfo(char *fname, struct auditinfo_addr *info)
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	int		fd, len, r;
188*0Sstevel@tonic-gate 	int		save_err;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	r = chmod(fname, 0200);
191*0Sstevel@tonic-gate 	if (r == -1 && errno != ENOENT)
192*0Sstevel@tonic-gate 		return (-1);
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	if ((fd = open(fname, O_CREAT|O_WRONLY|O_TRUNC, 0200)) == -1)
195*0Sstevel@tonic-gate 		return (-1);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	len = sprintf(textbuf,
198*0Sstevel@tonic-gate 			F_AUID
199*0Sstevel@tonic-gate 			F_SMASK
200*0Sstevel@tonic-gate 			F_FMASK
201*0Sstevel@tonic-gate 			F_PORT
202*0Sstevel@tonic-gate 			F_TYPE
203*0Sstevel@tonic-gate 			F_MACH
204*0Sstevel@tonic-gate 			F_ASID,
205*0Sstevel@tonic-gate 				(int)info->ai_auid,
206*0Sstevel@tonic-gate 				info->ai_mask.am_success,
207*0Sstevel@tonic-gate 				info->ai_mask.am_failure,
208*0Sstevel@tonic-gate 				info->ai_termid.at_port,
209*0Sstevel@tonic-gate 				info->ai_termid.at_type,
210*0Sstevel@tonic-gate 				info->ai_termid.at_addr[0],
211*0Sstevel@tonic-gate 				info->ai_termid.at_addr[1],
212*0Sstevel@tonic-gate 				info->ai_termid.at_addr[2],
213*0Sstevel@tonic-gate 				info->ai_termid.at_addr[3],
214*0Sstevel@tonic-gate 				(unsigned int)info->ai_asid);
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	if (write(fd, textbuf, len) != len)
217*0Sstevel@tonic-gate 		goto audit_setinfo_clean;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	if (fchmod(fd, 0400) == -1)
220*0Sstevel@tonic-gate 		goto audit_setinfo_clean;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	(void) close(fd);
223*0Sstevel@tonic-gate 	return (0);
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate audit_setinfo_clean:
226*0Sstevel@tonic-gate 	save_err = errno;
227*0Sstevel@tonic-gate 	(void) close(fd);
228*0Sstevel@tonic-gate 	(void) unlink(fname);
229*0Sstevel@tonic-gate 	errno = save_err;
230*0Sstevel@tonic-gate 	return (-1);
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate char *
234*0Sstevel@tonic-gate audit_cron_make_anc_name(char *fname)
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate 	char *anc_name;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	anc_name = (char *)malloc(strlen(fname) + strlen(AU_SUFFIX) + 1);
239*0Sstevel@tonic-gate 	if (anc_name == NULL)
240*0Sstevel@tonic-gate 		return (NULL);
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	(void) strcpy(anc_name, fname);
243*0Sstevel@tonic-gate 	(void) strcat(anc_name, AU_SUFFIX);
244*0Sstevel@tonic-gate 	return (anc_name);
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate int
248*0Sstevel@tonic-gate audit_cron_is_anc_name(char *name)
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate 	int	pos;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	pos = strlen(name) - strlen(AU_SUFFIX);
253*0Sstevel@tonic-gate 	if (pos <= 0)
254*0Sstevel@tonic-gate 		return (0);
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	if (strcmp(name + pos, AU_SUFFIX) == 0)
257*0Sstevel@tonic-gate 		return (1);
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	return (0);
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate static void
263*0Sstevel@tonic-gate audit_cron_session_failure(char *name, int type, char *err_str)
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate 	const char	*mess;
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 	if (type == 0)
268*0Sstevel@tonic-gate 		mess = dgettext(bsm_dom,
269*0Sstevel@tonic-gate 		"at-job session for user %s failed: ancillary file: %s");
270*0Sstevel@tonic-gate 	else
271*0Sstevel@tonic-gate 		mess = dgettext(bsm_dom,
272*0Sstevel@tonic-gate 		"crontab job session for user %s failed: ancillary file: %s");
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	(void) snprintf(textbuf, sizeof (textbuf), mess, name, err_str);
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	aug_save_event(AUE_cron_invoke);
277*0Sstevel@tonic-gate 	aug_save_sorf(4);
278*0Sstevel@tonic-gate 	aug_save_text(textbuf);
279*0Sstevel@tonic-gate 	(void) aug_audit();
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate int
284*0Sstevel@tonic-gate audit_cron_session(
285*0Sstevel@tonic-gate 		char *name,
286*0Sstevel@tonic-gate 		char *path,
287*0Sstevel@tonic-gate 		uid_t uid,
288*0Sstevel@tonic-gate 		gid_t gid,
289*0Sstevel@tonic-gate 		char *at_jobname)
290*0Sstevel@tonic-gate {
291*0Sstevel@tonic-gate 	struct auditinfo_addr	info;
292*0Sstevel@tonic-gate 	au_mask_t		mask;
293*0Sstevel@tonic-gate 	char			*anc_file, *fname;
294*0Sstevel@tonic-gate 	int			r = 0;
295*0Sstevel@tonic-gate 	char			full_path[PATH_MAX];
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	if (cannot_audit(0)) {
298*0Sstevel@tonic-gate 		return (0);
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	/* get auditinfo from ancillary file */
302*0Sstevel@tonic-gate 	if (at_jobname == NULL) {
303*0Sstevel@tonic-gate 		/*
304*0Sstevel@tonic-gate 		 *	this is a cron-event, so we can get
305*0Sstevel@tonic-gate 		 *	filename from "name" arg
306*0Sstevel@tonic-gate 		 */
307*0Sstevel@tonic-gate 		fname = name;
308*0Sstevel@tonic-gate 		if (path != NULL) {
309*0Sstevel@tonic-gate 			if (strlen(path) + strlen(fname) + 2 > PATH_MAX) {
310*0Sstevel@tonic-gate 				errno = ENAMETOOLONG;
311*0Sstevel@tonic-gate 				r = -1;
312*0Sstevel@tonic-gate 			}
313*0Sstevel@tonic-gate 			(void) strcat(strcat(strcpy(full_path, path), "/"),
314*0Sstevel@tonic-gate 			    fname);
315*0Sstevel@tonic-gate 			fname = full_path;
316*0Sstevel@tonic-gate 		}
317*0Sstevel@tonic-gate 	} else {
318*0Sstevel@tonic-gate 		/* this is an at-event, use "at_jobname" */
319*0Sstevel@tonic-gate 		fname = at_jobname;
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	if (r == 0) {
323*0Sstevel@tonic-gate 		anc_file = audit_cron_make_anc_name(fname);
324*0Sstevel@tonic-gate 		if (anc_file == NULL) {
325*0Sstevel@tonic-gate 			r = -1;
326*0Sstevel@tonic-gate 		} else {
327*0Sstevel@tonic-gate 			r = audit_cron_getinfo(fname, anc_file, &info);
328*0Sstevel@tonic-gate 		}
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	if (r != 0) {
332*0Sstevel@tonic-gate 		char *err_str;
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 		if (r == ANC_BAD_FORMAT)
335*0Sstevel@tonic-gate 			err_str = dgettext(bsm_dom, "bad format");
336*0Sstevel@tonic-gate 		else
337*0Sstevel@tonic-gate 			err_str = strerror(errno);
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 		audit_cron_session_failure(name,
340*0Sstevel@tonic-gate 					at_jobname == NULL,
341*0Sstevel@tonic-gate 					err_str);
342*0Sstevel@tonic-gate 		if (anc_file != NULL)
343*0Sstevel@tonic-gate 			free(anc_file);
344*0Sstevel@tonic-gate 		return (r);
345*0Sstevel@tonic-gate 	}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	free(anc_file);
348*0Sstevel@tonic-gate 	aug_init();
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	/* get current audit masks */
351*0Sstevel@tonic-gate 	if (au_user_mask(name, &mask) == 0) {
352*0Sstevel@tonic-gate 		info.ai_mask.am_success  |= mask.am_success;
353*0Sstevel@tonic-gate 		info.ai_mask.am_failure  |= mask.am_failure;
354*0Sstevel@tonic-gate 	}
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	/* save audit attributes for further use in current process */
357*0Sstevel@tonic-gate 	aug_save_auid(info.ai_auid);
358*0Sstevel@tonic-gate 	aug_save_asid(info.ai_asid);
359*0Sstevel@tonic-gate 	aug_save_tid_ex(info.ai_termid.at_port, info.ai_termid.at_addr,
360*0Sstevel@tonic-gate 		info.ai_termid.at_type);
361*0Sstevel@tonic-gate 	aug_save_pid(getpid());
362*0Sstevel@tonic-gate 	aug_save_uid(uid);
363*0Sstevel@tonic-gate 	aug_save_gid(gid);
364*0Sstevel@tonic-gate 	aug_save_euid(uid);
365*0Sstevel@tonic-gate 	aug_save_egid(gid);
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	/* set mixed audit masks */
368*0Sstevel@tonic-gate 	return (setaudit_addr(&info, sizeof (info)));
369*0Sstevel@tonic-gate }
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate /*
372*0Sstevel@tonic-gate  * audit_cron_new_job - create audit record with an information
373*0Sstevel@tonic-gate  *			about new job started by cron.
374*0Sstevel@tonic-gate  *	args:
375*0Sstevel@tonic-gate  *	cmd  - command being run by cron daemon.
376*0Sstevel@tonic-gate  *	type - type of job (0 - at-job, 1 - crontab job).
377*0Sstevel@tonic-gate  *	event - not used. pointer to cron event structure.
378*0Sstevel@tonic-gate  */
379*0Sstevel@tonic-gate /*ARGSUSED*/
380*0Sstevel@tonic-gate void
381*0Sstevel@tonic-gate audit_cron_new_job(char *cmd, int type, void *event)
382*0Sstevel@tonic-gate {
383*0Sstevel@tonic-gate 	if (cannot_audit(0))
384*0Sstevel@tonic-gate 		return;
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	if (type == 0) {
387*0Sstevel@tonic-gate 	    (void) snprintf(textbuf, sizeof (textbuf),
388*0Sstevel@tonic-gate 		    dgettext(bsm_dom, "at-job"));
389*0Sstevel@tonic-gate 	} else if (type == 1) {
390*0Sstevel@tonic-gate 	    (void) snprintf(textbuf, sizeof (textbuf),
391*0Sstevel@tonic-gate 		    dgettext(bsm_dom, "batch-job"));
392*0Sstevel@tonic-gate 	} else if (type == 2) {
393*0Sstevel@tonic-gate 	    (void) snprintf(textbuf, sizeof (textbuf),
394*0Sstevel@tonic-gate 		    dgettext(bsm_dom, "crontab-job"));
395*0Sstevel@tonic-gate 	} else if ((type > 2) && (type <= 25)) {	/* 25 from cron.h */
396*0Sstevel@tonic-gate 	    (void) snprintf(textbuf, sizeof (textbuf),
397*0Sstevel@tonic-gate 		    dgettext(bsm_dom, "queue-job (%c)"), (type+'a'));
398*0Sstevel@tonic-gate 	} else {
399*0Sstevel@tonic-gate 	    (void) snprintf(textbuf, sizeof (textbuf),
400*0Sstevel@tonic-gate 		    dgettext(bsm_dom, "unknown job type (%d)"), type);
401*0Sstevel@tonic-gate 	}
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	aug_save_event(AUE_cron_invoke);
404*0Sstevel@tonic-gate 	aug_save_sorf(0);
405*0Sstevel@tonic-gate 	aug_save_text(textbuf);
406*0Sstevel@tonic-gate 	aug_save_text1(cmd);
407*0Sstevel@tonic-gate 	(void) aug_audit();
408*0Sstevel@tonic-gate }
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate void
411*0Sstevel@tonic-gate audit_cron_bad_user(char *name)
412*0Sstevel@tonic-gate {
413*0Sstevel@tonic-gate 	if (cannot_audit(0))
414*0Sstevel@tonic-gate 		return;
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	(void) snprintf(textbuf, sizeof (textbuf),
417*0Sstevel@tonic-gate 			dgettext(bsm_dom, "bad user %s"), name);
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 	aug_save_event(AUE_cron_invoke);
420*0Sstevel@tonic-gate 	aug_save_sorf(2);
421*0Sstevel@tonic-gate 	aug_save_text(textbuf);
422*0Sstevel@tonic-gate 	(void) aug_audit();
423*0Sstevel@tonic-gate }
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate void
426*0Sstevel@tonic-gate audit_cron_user_acct_expired(char *name)
427*0Sstevel@tonic-gate {
428*0Sstevel@tonic-gate 	if (cannot_audit(0))
429*0Sstevel@tonic-gate 		return;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	(void) snprintf(textbuf, sizeof (textbuf),
432*0Sstevel@tonic-gate 			dgettext(bsm_dom,
433*0Sstevel@tonic-gate 				"user %s account expired"), name);
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	aug_save_event(AUE_cron_invoke);
436*0Sstevel@tonic-gate 	aug_save_sorf(3);
437*0Sstevel@tonic-gate 	aug_save_text(textbuf);
438*0Sstevel@tonic-gate 	(void) aug_audit();
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate int
442*0Sstevel@tonic-gate audit_cron_create_anc_file(char *name, char *path, char *uname, uid_t uid)
443*0Sstevel@tonic-gate {
444*0Sstevel@tonic-gate 	au_mask_t	msk;
445*0Sstevel@tonic-gate 	auditinfo_addr_t ai;
446*0Sstevel@tonic-gate 	int		pid;
447*0Sstevel@tonic-gate 	char		*anc_name;
448*0Sstevel@tonic-gate 	char		full_path[PATH_MAX];
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	if (cannot_audit(0))
451*0Sstevel@tonic-gate 		return (0);
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 	if (name == NULL)
454*0Sstevel@tonic-gate 		return (0);
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	if (path != NULL) {
457*0Sstevel@tonic-gate 		if (strlen(path) + strlen(name) + 2 > PATH_MAX)
458*0Sstevel@tonic-gate 			return (-1);
459*0Sstevel@tonic-gate 		(void) strcat(strcat(strcpy(full_path, path), "/"), name);
460*0Sstevel@tonic-gate 		name = full_path;
461*0Sstevel@tonic-gate 	}
462*0Sstevel@tonic-gate 	anc_name = audit_cron_make_anc_name(name);
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	if (access(anc_name, F_OK) != 0) {
465*0Sstevel@tonic-gate 		if (au_user_mask(uname, &msk) != 0) {
466*0Sstevel@tonic-gate 			free(anc_name);
467*0Sstevel@tonic-gate 			return (-1);
468*0Sstevel@tonic-gate 		}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 		ai.ai_mask = msk;
471*0Sstevel@tonic-gate 		ai.ai_auid = uid;
472*0Sstevel@tonic-gate 		ai.ai_termid.at_port = 0;
473*0Sstevel@tonic-gate 		ai.ai_termid.at_type = AU_IPv4;
474*0Sstevel@tonic-gate 		ai.ai_termid.at_addr[0] = 0;
475*0Sstevel@tonic-gate 		ai.ai_termid.at_addr[1] = 0;
476*0Sstevel@tonic-gate 		ai.ai_termid.at_addr[2] = 0;
477*0Sstevel@tonic-gate 		ai.ai_termid.at_addr[3] = 0;
478*0Sstevel@tonic-gate 		/* generate new pid to use it as asid */
479*0Sstevel@tonic-gate 		pid = vfork();
480*0Sstevel@tonic-gate 		if (pid == -1) {
481*0Sstevel@tonic-gate 			free(anc_name);
482*0Sstevel@tonic-gate 			return (-1);
483*0Sstevel@tonic-gate 		}
484*0Sstevel@tonic-gate 		if (pid == 0)
485*0Sstevel@tonic-gate 			exit(0);
486*0Sstevel@tonic-gate 		else {
487*0Sstevel@tonic-gate 		/*
488*0Sstevel@tonic-gate 		 * we need to clear status of children for
489*0Sstevel@tonic-gate 		 * wait() call in "cron"
490*0Sstevel@tonic-gate 		 */
491*0Sstevel@tonic-gate 			int lock;
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 			(void) waitpid(pid, &lock, 0);
494*0Sstevel@tonic-gate 		}
495*0Sstevel@tonic-gate 		ai.ai_asid = pid;
496*0Sstevel@tonic-gate 		if (audit_cron_setinfo(anc_name, &ai) != 0) {
497*0Sstevel@tonic-gate 			free(anc_name);
498*0Sstevel@tonic-gate 			return (-1);
499*0Sstevel@tonic-gate 		}
500*0Sstevel@tonic-gate 	}
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	free(anc_name);
503*0Sstevel@tonic-gate 	return (0);
504*0Sstevel@tonic-gate }
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate int
507*0Sstevel@tonic-gate audit_cron_delete_anc_file(char *name, char *path)
508*0Sstevel@tonic-gate {
509*0Sstevel@tonic-gate 	char	*anc_name;
510*0Sstevel@tonic-gate 	char	full_path[PATH_MAX];
511*0Sstevel@tonic-gate 	int	r;
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	if (name == NULL)
514*0Sstevel@tonic-gate 		return (0);
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 	if (path != NULL) {
517*0Sstevel@tonic-gate 		if (strlen(path) + strlen(name) + 2 > PATH_MAX)
518*0Sstevel@tonic-gate 			return (-1);
519*0Sstevel@tonic-gate 		(void) strcat(strcat(strcpy(full_path, path), "/"), name);
520*0Sstevel@tonic-gate 		name = full_path;
521*0Sstevel@tonic-gate 	}
522*0Sstevel@tonic-gate 	anc_name = audit_cron_make_anc_name(name);
523*0Sstevel@tonic-gate 	r = unlink(anc_name);
524*0Sstevel@tonic-gate 	free(anc_name);
525*0Sstevel@tonic-gate 	return (r);
526*0Sstevel@tonic-gate }
527