10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*4321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
28*4321Scasper #include <sys/param.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <sys/fcntl.h>
320Sstevel@tonic-gate #include <bsm/audit.h>
330Sstevel@tonic-gate #include <bsm/audit_record.h>
340Sstevel@tonic-gate #include <bsm/audit_uevents.h>
350Sstevel@tonic-gate #include <bsm/libbsm.h>
360Sstevel@tonic-gate #include <bsm/audit_private.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <syslog.h>
400Sstevel@tonic-gate #include <pwd.h>
410Sstevel@tonic-gate #include <netinet/in.h>
421676Sjpk #include <tsol/label.h>
430Sstevel@tonic-gate #include <locale.h>
440Sstevel@tonic-gate #include "generic.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #ifdef C2_DEBUG
470Sstevel@tonic-gate #define	dprintf(x) { printf x; }
480Sstevel@tonic-gate #else
490Sstevel@tonic-gate #define	dprintf(x)
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	UNKNOWN_CMD	"???"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static au_event_t	event;
550Sstevel@tonic-gate static int		audit_rexd_status = 0;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static char *
580Sstevel@tonic-gate build_cmd(char **cmd)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	int i, l;
610Sstevel@tonic-gate 	char *r;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	if (cmd == NULL)
640Sstevel@tonic-gate 		return (NULL);
650Sstevel@tonic-gate 	/* count the total length of command line */
660Sstevel@tonic-gate 	for (i = 0, l = 0; cmd[i] != NULL; i++)
670Sstevel@tonic-gate 		l += strlen(cmd[i]) + 1;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if (l == 0)
700Sstevel@tonic-gate 		return (NULL);
710Sstevel@tonic-gate 	r = malloc(l);
720Sstevel@tonic-gate 	if (r != NULL) {
730Sstevel@tonic-gate 		for (i = 0; cmd[i] != NULL; i++) {
740Sstevel@tonic-gate 			(void) strcat(r, cmd[i]);
750Sstevel@tonic-gate 			if (cmd[i + 1] != NULL)
760Sstevel@tonic-gate 				(void) strcat(r, " ");
770Sstevel@tonic-gate 		}
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	return (r);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static int
830Sstevel@tonic-gate selected(uid, user, event, sf)
840Sstevel@tonic-gate uid_t uid;
850Sstevel@tonic-gate char	*user;
860Sstevel@tonic-gate au_event_t	event;
870Sstevel@tonic-gate int	sf;
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	int	rc, sorf;
900Sstevel@tonic-gate 	char	naflags[512];
910Sstevel@tonic-gate 	struct au_mask mask;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	mask.am_success = mask.am_failure = 0;
94*4321Scasper 	if (uid > MAXEPHUID) {
950Sstevel@tonic-gate 		rc = getacna(naflags, 256); /* get non-attrib flags */
960Sstevel@tonic-gate 		if (rc == 0)
970Sstevel@tonic-gate 			(void) getauditflagsbin(naflags, &mask);
980Sstevel@tonic-gate 	} else {
990Sstevel@tonic-gate 		rc = au_user_mask(user, &mask);
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if (sf == 0)
1030Sstevel@tonic-gate 		sorf = AU_PRS_SUCCESS;
1040Sstevel@tonic-gate 	else if (sf == -1)
1050Sstevel@tonic-gate 		sorf = AU_PRS_FAILURE;
1060Sstevel@tonic-gate 	else
1070Sstevel@tonic-gate 		sorf = AU_PRS_BOTH;
1080Sstevel@tonic-gate 	rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD);
1090Sstevel@tonic-gate 	return (rc);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate void
1130Sstevel@tonic-gate audit_rexd_setup()
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	dprintf(("audit_rexd_setup()\n"));
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	event = AUE_rexd;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /* ARGSUSED */
1210Sstevel@tonic-gate static void
1220Sstevel@tonic-gate audit_rexd_session_setup(char *name, char *mach, uid_t uid)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	int			rc;
1250Sstevel@tonic-gate 	au_mask_t		mask;
1260Sstevel@tonic-gate 	struct auditinfo_addr	info;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (getaudit_addr(&info, sizeof (info)) < 0) {
1290Sstevel@tonic-gate 		perror("getaudit_addr");
1300Sstevel@tonic-gate 		exit(1);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	info.ai_auid = uid;
1340Sstevel@tonic-gate 	info.ai_asid = getpid();
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	mask.am_success = 0;
1370Sstevel@tonic-gate 	mask.am_failure = 0;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	(void) au_user_mask(name, &mask);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	info.ai_mask.am_success  = mask.am_success;
1420Sstevel@tonic-gate 	info.ai_mask.am_failure  = mask.am_failure;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	rc = setaudit_addr(&info, sizeof (info));
1450Sstevel@tonic-gate 	if (rc < 0) {
1460Sstevel@tonic-gate 		perror("setaudit_addr");
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate void
1510Sstevel@tonic-gate audit_rexd_fail(msg, hostname, user, uid, gid, shell, cmd)
1520Sstevel@tonic-gate 	char	*msg;		/* message containing failure information */
1530Sstevel@tonic-gate 	char	*hostname;	/* hostname of machine requesting service */
1540Sstevel@tonic-gate 	char	*user;		/* username of user requesting service */
1550Sstevel@tonic-gate 	uid_t 	uid;		/* user id of user requesting service */
1560Sstevel@tonic-gate 	gid_t	gid;		/* group of user requesting service */
1570Sstevel@tonic-gate 	char	*shell;		/* login shell of user requesting service */
1580Sstevel@tonic-gate 	char	**cmd;		/* argv to be executed locally */
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	int	rd;		/* audit record descriptor */
1610Sstevel@tonic-gate 	char	buf[256];	/* temporary buffer */
1620Sstevel@tonic-gate 	char	*tbuf;		/* temporary buffer */
1630Sstevel@tonic-gate 	int	tlen;
1640Sstevel@tonic-gate 	const char *gtxt;	/* gettext return value */
1650Sstevel@tonic-gate 	pid_t	pid;
1660Sstevel@tonic-gate 	char	*cmdbuf;
1670Sstevel@tonic-gate 	char	*audit_cmd[2] = {NULL, NULL};
1680Sstevel@tonic-gate 	int	dont_free = 0;
1690Sstevel@tonic-gate 	struct auditinfo_addr info;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	dprintf(("audit_rexd_fail()\n"));
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * check if audit_rexd_fail() or audit_rexd_success()
1750Sstevel@tonic-gate 	 * have been called already.
1760Sstevel@tonic-gate 	 */
1770Sstevel@tonic-gate 	if (audit_rexd_status == 1) {
1780Sstevel@tonic-gate 		return;
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (cannot_audit(0)) {
1820Sstevel@tonic-gate 		return;
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * set status to prevent multiple calls
1870Sstevel@tonic-gate 	 * to audit_rexd_fail() and audit_rexd_success()
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	audit_rexd_status = 1;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/* determine if we're preselected */
1920Sstevel@tonic-gate 	if (!selected(uid, user, event, -1))
1930Sstevel@tonic-gate 		return;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	pid = getpid();
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (getaudit_addr(&info, sizeof (info)) < 0) {
1980Sstevel@tonic-gate 		perror("getaudit_addr");
1990Sstevel@tonic-gate 		exit(1);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	rd = au_open();
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/* add subject token */
2050Sstevel@tonic-gate 	(void) au_write(rd,
2060Sstevel@tonic-gate 		au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid,
2070Sstevel@tonic-gate 			&info.ai_termid));
2081676Sjpk 	if (is_system_labeled())
2091676Sjpk 		(void) au_write(rd, au_to_mylabel());
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* add reason for failure */
2120Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(msg));
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/* add hostname of machine requesting service */
2150Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
2160Sstevel@tonic-gate 		"Remote execution requested by: %s"), hostname);
2170Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(buf));
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/* add username of user requesting service */
2200Sstevel@tonic-gate 	if (user == NULL)
2210Sstevel@tonic-gate 		user = "???";
2220Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
2230Sstevel@tonic-gate 	    "Username: %s"), user);
2240Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(buf));
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
2270Sstevel@tonic-gate 	    "User id: %d"), uid);
2280Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(buf));
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	if (cmd == NULL) {
2310Sstevel@tonic-gate 		audit_cmd[0] = shell;
2320Sstevel@tonic-gate 		cmd = audit_cmd;
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	cmdbuf = build_cmd(cmd);
2360Sstevel@tonic-gate 	if (cmdbuf == NULL) {
2370Sstevel@tonic-gate 		cmdbuf = UNKNOWN_CMD;
2380Sstevel@tonic-gate 		dont_free = 1;
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	gtxt = dgettext(bsm_dom, "Command line: %s");
2420Sstevel@tonic-gate 	/* over estimate of size of buffer needed (%s is replaced) */
2430Sstevel@tonic-gate 	tlen = strlen(cmdbuf) + strlen(gtxt) + 1;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if ((tbuf = malloc(tlen)) == NULL) {
2460Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
2470Sstevel@tonic-gate 		return;
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 	(void) snprintf(tbuf, tlen, gtxt, cmdbuf);
2500Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(tbuf));
2510Sstevel@tonic-gate 	(void) free(tbuf);
2520Sstevel@tonic-gate 	if (!dont_free)
2530Sstevel@tonic-gate 		(void) free(cmdbuf);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/* add return token */
2560Sstevel@tonic-gate #ifdef _LP64
2570Sstevel@tonic-gate 	(void) au_write(rd, au_to_return64(-1, (int64_t)0));
2580Sstevel@tonic-gate #else
2590Sstevel@tonic-gate 	(void) au_write(rd, au_to_return32(-1, (int32_t)0));
2600Sstevel@tonic-gate #endif
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/* write audit record */
2630Sstevel@tonic-gate 	if (au_close(rd, 1, event) < 0) {
2640Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
2650Sstevel@tonic-gate 		return;
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate void
2700Sstevel@tonic-gate audit_rexd_success(hostname, user, uid, gid, shell, cmd)
2710Sstevel@tonic-gate char	*hostname;	/* hostname of machine requesting service */
2720Sstevel@tonic-gate char	*user;		/* username of user requesting service, may be NULL */
2730Sstevel@tonic-gate uid_t 	uid;		/* user id of user requesting service */
2740Sstevel@tonic-gate gid_t	gid;		/* group of user requesting service */
2750Sstevel@tonic-gate char	*shell;		/* login shell of user requesting service */
2760Sstevel@tonic-gate char	**cmd;		/* argv to be executed locally, may be NULL */
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate 	int	rd;			/* audit record descriptor */
2790Sstevel@tonic-gate 	char	buf[256];	/* temporary buffer */
2800Sstevel@tonic-gate 	char	*tbuf;		/* temporary buffer */
2810Sstevel@tonic-gate 	int	tlen;
2820Sstevel@tonic-gate 	const char *gtxt;
2830Sstevel@tonic-gate 	pid_t	pid;
2840Sstevel@tonic-gate 	char	*cmdbuf;
2850Sstevel@tonic-gate 	char	*audit_cmd[2] = {NULL, NULL};
2860Sstevel@tonic-gate 	int	dont_free = 0;
2870Sstevel@tonic-gate 	struct auditinfo_addr info;
2880Sstevel@tonic-gate 	char	*empty = "";
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	dprintf(("audit_rexd_success()\n"));
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/*
2930Sstevel@tonic-gate 	 * check if audit_rexd_fail() or audit_rexd_success()
2940Sstevel@tonic-gate 	 * have been called already.
2950Sstevel@tonic-gate 	 */
2960Sstevel@tonic-gate 	if (audit_rexd_status == 1) {
2970Sstevel@tonic-gate 		return;
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (cannot_audit(0)) {
3010Sstevel@tonic-gate 		return;
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/* a little bullet proofing... */
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (hostname == NULL)
3070Sstevel@tonic-gate 		hostname = empty;
3080Sstevel@tonic-gate 	if (shell == NULL)
3090Sstevel@tonic-gate 		shell = empty;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/*
3120Sstevel@tonic-gate 	 * set status to prevent multiple calls
3130Sstevel@tonic-gate 	 * to audit_rexd_fail() and audit_rexd_success()
3140Sstevel@tonic-gate 	 */
3150Sstevel@tonic-gate 	audit_rexd_status = 1;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	/* determine if we're preselected */
3180Sstevel@tonic-gate 	if (!selected(uid, user, event, 0))
3190Sstevel@tonic-gate 		goto rexd_audit_session;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	pid = getpid();
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (getaudit_addr(&info, sizeof (info)) < 0) {
3240Sstevel@tonic-gate 		perror("getaudit_addr");
3250Sstevel@tonic-gate 		exit(1);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	rd = au_open();
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/* add subject token */
3310Sstevel@tonic-gate 	(void) au_write(rd,
3320Sstevel@tonic-gate 		au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid,
3330Sstevel@tonic-gate 			&info.ai_termid));
3341676Sjpk 	if (is_system_labeled())
3351676Sjpk 		(void) au_write(rd, au_to_mylabel());
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	/* add hostname of machine requesting service */
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
3400Sstevel@tonic-gate 		"Remote execution requested by: %s"), hostname);
3410Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(buf));
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/* add username at machine requesting service */
3440Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
3450Sstevel@tonic-gate 	    "Username: %s"), user);
3460Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(buf));
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (cmd == NULL) {
3490Sstevel@tonic-gate 		audit_cmd[0] = shell;
3500Sstevel@tonic-gate 		cmd = audit_cmd;
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	cmdbuf = build_cmd(cmd);
3540Sstevel@tonic-gate 	if (cmdbuf == NULL) {
3550Sstevel@tonic-gate 		cmdbuf = UNKNOWN_CMD;
3560Sstevel@tonic-gate 		dont_free = 1;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	gtxt = dgettext(bsm_dom, "Command line: %s");
3600Sstevel@tonic-gate 	tlen = strlen(cmdbuf) + strlen(gtxt) + 1;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if ((tbuf = malloc(tlen)) == NULL) {
3630Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
3640Sstevel@tonic-gate 		goto rexd_audit_session;
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	(void) snprintf(tbuf, tlen, gtxt, cmdbuf);
3680Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(tbuf));
3690Sstevel@tonic-gate 	(void) free(tbuf);
3700Sstevel@tonic-gate 	if (!dont_free)
3710Sstevel@tonic-gate 		(void) free(cmdbuf);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/* add return token */
3740Sstevel@tonic-gate #ifdef _LP64
3750Sstevel@tonic-gate 	(void) au_write(rd, au_to_return64(0, (int64_t)0));
3760Sstevel@tonic-gate #else
3770Sstevel@tonic-gate 	(void) au_write(rd, au_to_return32(0, (int32_t)0));
3780Sstevel@tonic-gate #endif
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/* write audit record */
3810Sstevel@tonic-gate 	if (au_close(rd, 1, event) < 0) {
3820Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate rexd_audit_session:
3860Sstevel@tonic-gate 	audit_rexd_session_setup(user, hostname, uid);
3870Sstevel@tonic-gate }
388