xref: /onnv-gate/usr/src/lib/libbsm/common/audit_rshd.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>
264321Scasper #include <sys/param.h>
270Sstevel@tonic-gate #include <stdio.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 <netinet/in.h>
381676Sjpk #include <tsol/label.h>
390Sstevel@tonic-gate #include <locale.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <generic.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static au_event_t	rshd_event;	/* audit event number */
440Sstevel@tonic-gate static uint32_t		rshd_addr[4];	/* peer address */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static void generate_record(char *, char *, char *, int, char *);
470Sstevel@tonic-gate static void setup_session(char *);
480Sstevel@tonic-gate static int selected(uid_t, char *, au_event_t, int);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate int
audit_rshd_setup()510Sstevel@tonic-gate audit_rshd_setup()
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	rshd_event = AUE_rshd;
540Sstevel@tonic-gate 	return (0);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /* ARGSUSED */
580Sstevel@tonic-gate int
audit_rshd_fail(msg,hostname,remuser,locuser,cmdbuf)590Sstevel@tonic-gate audit_rshd_fail(msg, hostname, remuser, locuser, cmdbuf)
600Sstevel@tonic-gate char	*msg;		/* message containing failure information */
610Sstevel@tonic-gate char	*hostname;		/* hostname of machine requesting service */
620Sstevel@tonic-gate char	*remuser;		/* username at machine requesting service */
630Sstevel@tonic-gate char	*locuser;		/* username of local machine */
640Sstevel@tonic-gate char	*cmdbuf;		/* command line to be executed locally */
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	if (cannot_audit(0)) {
670Sstevel@tonic-gate 		return (0);
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	generate_record(remuser, locuser, cmdbuf, -1, msg);
700Sstevel@tonic-gate 	return (0);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* ARGSUSED */
740Sstevel@tonic-gate int
audit_rshd_success(hostname,remuser,locuser,cmdbuf)750Sstevel@tonic-gate audit_rshd_success(hostname, remuser, locuser, cmdbuf)
760Sstevel@tonic-gate char	*hostname;		/* hostname of machine requesting service */
770Sstevel@tonic-gate char	*remuser;		/* username at machine requesting service */
780Sstevel@tonic-gate char	*locuser;		/* username at local machine */
790Sstevel@tonic-gate char	*cmdbuf;		/* command line to be executed locally */
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	if (cannot_audit(0)) {
820Sstevel@tonic-gate 		return (0);
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	generate_record(remuser, locuser, cmdbuf, 0, "");
850Sstevel@tonic-gate 	setup_session(locuser);
860Sstevel@tonic-gate 	return (0);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #include <pwd.h>
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static void
generate_record(char * remuser,char * locuser,char * cmdbuf,int sf_flag,char * msg)930Sstevel@tonic-gate generate_record(char *remuser,	/* username at machine requesting service */
940Sstevel@tonic-gate 		char *locuser,	/* username of local machine */
950Sstevel@tonic-gate 		char *cmdbuf,	/* command line to be executed locally */
960Sstevel@tonic-gate 		int sf_flag,	/* success (0) or failure (-1) flag */
970Sstevel@tonic-gate 		char *msg)	/* message containing failure information */
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	int	rd;		/* audit record descriptor */
1000Sstevel@tonic-gate 	char	buf[256];	/* temporary buffer */
1010Sstevel@tonic-gate 	char	*tbuf;		/* temporary buffer */
1020Sstevel@tonic-gate 	int	tlen;
1030Sstevel@tonic-gate 	const char *gtxt;
1040Sstevel@tonic-gate 	uid_t	uid;
1050Sstevel@tonic-gate 	gid_t	gid;
1060Sstevel@tonic-gate 	pid_t	pid;
1070Sstevel@tonic-gate 	struct passwd *pwd;
1080Sstevel@tonic-gate 	struct auditinfo_addr info;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	if (cannot_audit(0)) {
1110Sstevel@tonic-gate 		return;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	pwd = getpwnam(locuser);
1150Sstevel@tonic-gate 	if (pwd == NULL) {
1164321Scasper 		uid = (uid_t)-1;
1174321Scasper 		gid = (gid_t)-1;
1180Sstevel@tonic-gate 	} else {
1190Sstevel@tonic-gate 		uid = pwd->pw_uid;
1200Sstevel@tonic-gate 		gid = pwd->pw_gid;
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (!selected(uid, locuser, rshd_event, sf_flag))
1240Sstevel@tonic-gate 		return;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	pid = getpid();
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/* see if terminal id already set */
1290Sstevel@tonic-gate 	if (getaudit_addr(&info, sizeof (info)) < 0) {
1300Sstevel@tonic-gate 		perror("getaudit");
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	rd = au_open();
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	(void) au_write(rd, au_to_subject_ex(uid, uid, gid, uid, gid, pid, pid,
135*12918SJan.Friedel@Sun.COM 	    &info.ai_termid));
1361676Sjpk 	if (is_system_labeled())
1371676Sjpk 		(void) au_write(rd, au_to_mylabel());
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	gtxt = dgettext(bsm_dom, "cmd %s");
1400Sstevel@tonic-gate 	tlen = strlen(gtxt) + strlen(cmdbuf) + 1;
1410Sstevel@tonic-gate 	if ((tbuf = malloc(tlen)) == NULL) {
1420Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
1430Sstevel@tonic-gate 		return;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	(void) snprintf(tbuf, tlen, gtxt, cmdbuf);
1460Sstevel@tonic-gate 	(void) au_write(rd, au_to_text(tbuf));
1470Sstevel@tonic-gate 	(void) free(tbuf);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if (strcmp(remuser, locuser) != 0) {
1500Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
151*12918SJan.Friedel@Sun.COM 		    "remote user %s"), remuser);
1520Sstevel@tonic-gate 		(void) au_write(rd, au_to_text(buf));
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (sf_flag == -1) {
1560Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), dgettext(bsm_dom,
157*12918SJan.Friedel@Sun.COM 		    "local user %s"), locuser);
1580Sstevel@tonic-gate 		(void) au_write(rd, au_to_text(buf));
1590Sstevel@tonic-gate 		(void) au_write(rd, au_to_text(msg));
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate #ifdef _LP64
1630Sstevel@tonic-gate 	(void) au_write(rd, au_to_return64(sf_flag, (int64_t)0));
1640Sstevel@tonic-gate #else
1650Sstevel@tonic-gate 	(void) au_write(rd, au_to_return32(sf_flag, (int32_t)0));
1660Sstevel@tonic-gate #endif
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (au_close(rd, 1, rshd_event) < 0) {
1690Sstevel@tonic-gate 		(void) au_close(rd, 0, 0);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static int
selected(uid_t uid,char * locuser,au_event_t event,int sf)1740Sstevel@tonic-gate selected(uid_t uid, char *locuser, au_event_t event, int sf)
1750Sstevel@tonic-gate {
176*12918SJan.Friedel@Sun.COM 	int		sorf;
177*12918SJan.Friedel@Sun.COM 	struct au_mask	mask;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	mask.am_success = mask.am_failure = 0;
1804321Scasper 	if (uid > MAXEPHUID) {
181*12918SJan.Friedel@Sun.COM 		/* get non-attrib flags */
182*12918SJan.Friedel@Sun.COM 		(void) auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask));
1830Sstevel@tonic-gate 	} else {
184*12918SJan.Friedel@Sun.COM 		(void) au_user_mask(locuser, &mask);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
187*12918SJan.Friedel@Sun.COM 	if (sf == 0) {
1880Sstevel@tonic-gate 		sorf = AU_PRS_SUCCESS;
189*12918SJan.Friedel@Sun.COM 	} else if (sf == -1) {
1900Sstevel@tonic-gate 		sorf = AU_PRS_FAILURE;
191*12918SJan.Friedel@Sun.COM 	} else {
1920Sstevel@tonic-gate 		sorf = AU_PRS_BOTH;
193*12918SJan.Friedel@Sun.COM 	}
194*12918SJan.Friedel@Sun.COM 
195*12918SJan.Friedel@Sun.COM 	return (au_preselect(event, &mask, sorf, AU_PRS_REREAD));
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate static void
setup_session(char * locuser)1990Sstevel@tonic-gate setup_session(char *locuser)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	int	rc;
2020Sstevel@tonic-gate 	struct auditinfo_addr info;
2030Sstevel@tonic-gate 	au_mask_t		mask;
2040Sstevel@tonic-gate 	uid_t			uid;
2050Sstevel@tonic-gate 	struct passwd *pwd;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	pwd = getpwnam(locuser);
2080Sstevel@tonic-gate 	if (pwd == NULL)
2094321Scasper 		uid = (uid_t)-1;
2100Sstevel@tonic-gate 	else
2110Sstevel@tonic-gate 		uid = pwd->pw_uid;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/* see if terminal id already set */
2140Sstevel@tonic-gate 	if (getaudit_addr(&info, sizeof (info)) < 0) {
2150Sstevel@tonic-gate 		perror("getaudit");
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	info.ai_auid = uid;
2190Sstevel@tonic-gate 	info.ai_asid = getpid();
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	mask.am_success = 0;
2220Sstevel@tonic-gate 	mask.am_failure = 0;
2230Sstevel@tonic-gate 	(void) au_user_mask(locuser, &mask);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	info.ai_mask.am_success = mask.am_success;
2260Sstevel@tonic-gate 	info.ai_mask.am_failure = mask.am_failure;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	rshd_addr[0] = info.ai_termid.at_addr[0];
2290Sstevel@tonic-gate 	rshd_addr[1] = info.ai_termid.at_addr[1];
2300Sstevel@tonic-gate 	rshd_addr[2] = info.ai_termid.at_addr[2];
2310Sstevel@tonic-gate 	rshd_addr[3] = info.ai_termid.at_addr[3];
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	rc = setaudit_addr(&info, sizeof (info));
2340Sstevel@tonic-gate 	if (rc < 0) {
2350Sstevel@tonic-gate 		perror("setaudit");
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate }
238