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