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