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