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*11706SJan.Friedel@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <sys/fcntl.h>
300Sstevel@tonic-gate #include <bsm/audit.h>
310Sstevel@tonic-gate #include <bsm/audit_record.h>
320Sstevel@tonic-gate #include <bsm/audit_uevents.h>
330Sstevel@tonic-gate #include <bsm/libbsm.h>
340Sstevel@tonic-gate #include <bsm/audit_private.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <syslog.h>
380Sstevel@tonic-gate #include <pwd.h>
390Sstevel@tonic-gate #include <netinet/in.h>
401676Sjpk #include <tsol/label.h>
410Sstevel@tonic-gate #include <locale.h>
420Sstevel@tonic-gate #include "generic.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #ifdef C2_DEBUG
45*11706SJan.Friedel@Sun.COM #define	dprintf(x) { (void) printf x; }
460Sstevel@tonic-gate #else
470Sstevel@tonic-gate #define	dprintf(x)
480Sstevel@tonic-gate #endif
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static au_event_t	event;
510Sstevel@tonic-gate static int		audit_rexecd_status = 0;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static int
540Sstevel@tonic-gate selected(uid, user, event, sf)
550Sstevel@tonic-gate uid_t uid;
560Sstevel@tonic-gate char	*user;
570Sstevel@tonic-gate au_event_t	event;
580Sstevel@tonic-gate int	sf;
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	int	rc, sorf;
610Sstevel@tonic-gate 	char	naflags[512];
620Sstevel@tonic-gate 	struct au_mask mask;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	mask.am_success = mask.am_failure = 0;
654321Scasper 	if (uid > MAXEPHUID) {
660Sstevel@tonic-gate 		rc = getacna(naflags, 256); /* get non-attrib flags */
670Sstevel@tonic-gate 		if (rc == 0)
680Sstevel@tonic-gate 			(void) getauditflagsbin(naflags, &mask);
690Sstevel@tonic-gate 	} else {
700Sstevel@tonic-gate 		rc = au_user_mask(user, &mask);
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (sf == 0)
740Sstevel@tonic-gate 		sorf = AU_PRS_SUCCESS;
750Sstevel@tonic-gate 	else if (sf == -1)
760Sstevel@tonic-gate 		sorf = AU_PRS_FAILURE;
770Sstevel@tonic-gate 	else
780Sstevel@tonic-gate 		sorf = AU_PRS_BOTH;
790Sstevel@tonic-gate 	rc = au_preselect(event, &mask, sorf, AU_PRS_REREAD);
800Sstevel@tonic-gate 	return (rc);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate void
840Sstevel@tonic-gate audit_rexecd_setup()
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	dprintf(("audit_rexecd_setup()\n"));
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	event = AUE_rexecd;
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static void
930Sstevel@tonic-gate audit_rexecd_session_setup(char *name, char *mach, uid_t uid)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	int			rc;
960Sstevel@tonic-gate 	au_mask_t		mask;
970Sstevel@tonic-gate 	struct auditinfo_addr	info;
980Sstevel@tonic-gate 	uint32_t addr[4], type;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	info.ai_auid = uid;
1010Sstevel@tonic-gate 	info.ai_asid = getpid();
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	mask.am_success = 0;
1040Sstevel@tonic-gate 	mask.am_failure = 0;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	(void) au_user_mask(name, &mask);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	info.ai_mask.am_success  = mask.am_success;
1090Sstevel@tonic-gate 	info.ai_mask.am_failure  = mask.am_failure;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	rc = aug_get_machine(mach, addr, &type);
1120Sstevel@tonic-gate 	if (rc < 0) {
1130Sstevel@tonic-gate 		perror("get address");
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 	info.ai_termid.at_port = aug_get_port();
1160Sstevel@tonic-gate 	info.ai_termid.at_type    = type;
1170Sstevel@tonic-gate 	info.ai_termid.at_addr[0] = addr[0];
1180Sstevel@tonic-gate 	info.ai_termid.at_addr[1] = addr[1];
1190Sstevel@tonic-gate 	info.ai_termid.at_addr[2] = addr[2];
1200Sstevel@tonic-gate 	info.ai_termid.at_addr[3] = addr[3];
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	rc = setaudit_addr(&info, sizeof (info));
1230Sstevel@tonic-gate 	if (rc < 0) {
1240Sstevel@tonic-gate 		perror("setaudit");
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate void
1290Sstevel@tonic-gate audit_rexecd_fail(msg, hostname, user, cmdbuf)
1300Sstevel@tonic-gate char	*msg;		/* message containing failure information */
1310Sstevel@tonic-gate char	*hostname;	/* hostname of machine requesting service */
1320Sstevel@tonic-gate char	*user;		/* username of user requesting service */
1330Sstevel@tonic-gate char	*cmdbuf;	/* command line to be executed locally */
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	int	rd;		/* audit record descriptor */
1360Sstevel@tonic-gate 	char	buf[256];	/* temporary buffer */
1370Sstevel@tonic-gate 	char	*tbuf;		/* temporary buffer */
1380Sstevel@tonic-gate 	int	tlen;
1390Sstevel@tonic-gate 	const char *gtxt;
1400Sstevel@tonic-gate 	uid_t 	uid;
1410Sstevel@tonic-gate 	gid_t	gid;
1420Sstevel@tonic-gate 	pid_t	pid;
1430Sstevel@tonic-gate 	au_tid_addr_t	tid;
1440Sstevel@tonic-gate 	struct passwd	*pwd;
1450Sstevel@tonic-gate 	uint32_t addr[4], type;
1460Sstevel@tonic-gate 	int rc;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	dprintf(("audit_rexecd_fail()\n"));
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	/*
1510Sstevel@tonic-gate 	 * check if audit_rexecd_fail() or audit_rexecd_success()
1520Sstevel@tonic-gate 	 * have been called already.
1530Sstevel@tonic-gate 	 */
1540Sstevel@tonic-gate 	if (audit_rexecd_status == 1) {
1550Sstevel@tonic-gate 		return;
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (cannot_audit(0)) {
1590Sstevel@tonic-gate 		return;
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/*
1630Sstevel@tonic-gate 	 * set status to prevent multiple calls
1640Sstevel@tonic-gate 	 * to audit_rexecd_fail() and audit_rexecd_success()
1650Sstevel@tonic-gate 	 */
1660Sstevel@tonic-gate 	audit_rexecd_status = 1;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	pwd = getpwnam(user);
1690Sstevel@tonic-gate 	if (pwd == NULL) {
1704321Scasper 		uid = (uid_t)-1;
1714321Scasper 		gid = (gid_t)-1;
1720Sstevel@tonic-gate 	} else {
1730Sstevel@tonic-gate 		uid = pwd->pw_uid;
1740Sstevel@tonic-gate 		gid = pwd->pw_gid;
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/* determine if we're preselected */
1780Sstevel@tonic-gate 	if (!selected(uid, user, event, -1))
1790Sstevel@tonic-gate 		return;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	pid = getpid();
1820Sstevel@tonic-gate 	rc = aug_get_machine(hostname, addr, &type);
1830Sstevel@tonic-gate 	if (rc < 0) {
1840Sstevel@tonic-gate 		perror("get address");
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	tid.at_port    = aug_get_port();
1880Sstevel@tonic-gate 	tid.at_addr[0] = addr[0];
1890Sstevel@tonic-gate 	tid.at_addr[1] = addr[1];
1900Sstevel@tonic-gate 	tid.at_addr[2] = addr[2];
1910Sstevel@tonic-gate 	tid.at_addr[3] = addr[3];
1920Sstevel@tonic-gate 	tid.at_type    = type;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	rd = au_open();
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/* add subject token */
1970Sstevel@tonic-gate 	(void) au_write(rd,
1980Sstevel@tonic-gate 		au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid, &tid));
1991676Sjpk 	if (is_system_labeled())
2001676Sjpk 		(void) au_write(rd, au_to_mylabel());
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/* add reason for failure */
2030Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(msg));
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/* add hostname of machine requesting service */
2060Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
2070Sstevel@tonic-gate 		"Remote execution requested by: %s"), hostname);
2080Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(buf));
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* add username of user requesting service */
2110Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
2120Sstevel@tonic-gate 		"Username: %s"), user);
2130Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(buf));
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/* add command line to be executed locally */
2160Sstevel@tonic-gate 	gtxt = dgettext(bsm_dom, "Command line: %s");
2170Sstevel@tonic-gate 	tlen = strlen(gtxt) + strlen(cmdbuf) + 1;
2180Sstevel@tonic-gate 	if ((tbuf = malloc(tlen)) == NULL) {
2190Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
2200Sstevel@tonic-gate 		return;
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 	(void) snprintf(tbuf, tlen, gtxt, cmdbuf);
2230Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(tbuf));
2240Sstevel@tonic-gate 	(void) free(tbuf);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/* add return token */
2270Sstevel@tonic-gate #ifdef _LP64
2280Sstevel@tonic-gate 	(void) au_write(rd, au_to_return64(-1, (int64_t)0));
2290Sstevel@tonic-gate #else
2300Sstevel@tonic-gate 	(void) au_write(rd, au_to_return32(-1, (int32_t)0));
2310Sstevel@tonic-gate #endif
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/* write audit record */
2340Sstevel@tonic-gate 	if (au_close(rd, 1, event) < 0) {
2350Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
2360Sstevel@tonic-gate 		return;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate void
2410Sstevel@tonic-gate audit_rexecd_success(hostname, user, cmdbuf)
2420Sstevel@tonic-gate char	*hostname;	/* hostname of machine requesting service */
2430Sstevel@tonic-gate char	*user;		/* username of user requesting service */
2440Sstevel@tonic-gate char	*cmdbuf;	/* command line to be executed locally */
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	int	rd;		/* audit record descriptor */
2470Sstevel@tonic-gate 	char	buf[256];	/* temporary buffer */
2480Sstevel@tonic-gate 	char	*tbuf;		/* temporary buffer */
2490Sstevel@tonic-gate 	int	tlen;
2500Sstevel@tonic-gate 	const char *gtxt;
2510Sstevel@tonic-gate 	uid_t 	uid;
2520Sstevel@tonic-gate 	gid_t	gid;
2530Sstevel@tonic-gate 	pid_t	pid;
2540Sstevel@tonic-gate 	au_tid_addr_t	tid;
2550Sstevel@tonic-gate 	struct passwd	*pwd;
2560Sstevel@tonic-gate 	uint32_t addr[4], type;
2570Sstevel@tonic-gate 	int rc;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	dprintf(("audit_rexecd_success()\n"));
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * check if audit_rexecd_fail() or audit_rexecd_success()
2630Sstevel@tonic-gate 	 * have been called already.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	if (audit_rexecd_status == 1) {
2660Sstevel@tonic-gate 		return;
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (cannot_audit(0)) {
2700Sstevel@tonic-gate 		return;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	/*
2740Sstevel@tonic-gate 	 * set status to prevent multiple calls
2750Sstevel@tonic-gate 	 * to audit_rexecd_fail() and audit_rexecd_success()
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 	audit_rexecd_status = 1;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	pwd = getpwnam(user);
2800Sstevel@tonic-gate 	if (pwd == NULL) {
2814321Scasper 		uid = (uid_t)-1;
2824321Scasper 		gid = (gid_t)-1;
2830Sstevel@tonic-gate 	} else {
2840Sstevel@tonic-gate 		uid = pwd->pw_uid;
2850Sstevel@tonic-gate 		gid = pwd->pw_gid;
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/* determine if we're preselected */
2890Sstevel@tonic-gate 	if (!selected(uid, user, event, 0))
2900Sstevel@tonic-gate 		goto rexecd_audit_session;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	pid = getpid();
2930Sstevel@tonic-gate 	rc = aug_get_machine(hostname, addr, &type);
2940Sstevel@tonic-gate 	if (rc < 0) {
2950Sstevel@tonic-gate 		perror("get address");
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	tid.at_port    = aug_get_port();
2990Sstevel@tonic-gate 	tid.at_addr[0] = addr[0];
3000Sstevel@tonic-gate 	tid.at_addr[1] = addr[1];
3010Sstevel@tonic-gate 	tid.at_addr[2] = addr[2];
3020Sstevel@tonic-gate 	tid.at_addr[3] = addr[3];
3030Sstevel@tonic-gate 	tid.at_type    = type;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	rd = au_open();
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	/* add subject token */
3080Sstevel@tonic-gate 	(void) au_write(rd,
3090Sstevel@tonic-gate 		au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid, &tid));
3101676Sjpk 	if (is_system_labeled())
3111676Sjpk 		(void) au_write(rd, au_to_mylabel());
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/* add hostname of machine requesting service */
3140Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
3150Sstevel@tonic-gate 		"Remote execution requested by: %s"), hostname);
3160Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(buf));
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	/* add username at machine requesting service */
3190Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
3200Sstevel@tonic-gate 	    "Username: %s"), user);
3210Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(buf));
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/* add command line to be executed locally */
3240Sstevel@tonic-gate 	gtxt = dgettext(bsm_dom, "Command line: %s");
3250Sstevel@tonic-gate 	tlen = strlen(gtxt) + strlen(cmdbuf) + 1;
3260Sstevel@tonic-gate 	if ((tbuf = malloc(tlen)) == NULL) {
3270Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
3280Sstevel@tonic-gate 	} else {
3290Sstevel@tonic-gate 		(void) snprintf(tbuf, tlen, gtxt, cmdbuf);
3300Sstevel@tonic-gate 		(void) au_write(rd, au_to_text(tbuf));
3310Sstevel@tonic-gate 		(void) free(tbuf);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		/* add return token */
3340Sstevel@tonic-gate #ifdef _LP64
3350Sstevel@tonic-gate 		(void) au_write(rd, au_to_return64(0, (int64_t)0));
3360Sstevel@tonic-gate #else
3370Sstevel@tonic-gate 		(void) au_write(rd, au_to_return32(0, (int32_t)0));
3380Sstevel@tonic-gate #endif
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		/* write audit record */
3410Sstevel@tonic-gate 		if (au_close(rd, 1, event) < 0) {
3420Sstevel@tonic-gate 			(void) au_close(rd, 0, 0);
3430Sstevel@tonic-gate 		}
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate rexecd_audit_session:
3470Sstevel@tonic-gate 	audit_rexecd_session_setup(user, hostname, uid);
3480Sstevel@tonic-gate }
349