xref: /onnv-gate/usr/src/cmd/smserverd/myaudit.c (revision 7753:ebbac916a413)
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*7753STon.Nguyen@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <netdb.h>
280Sstevel@tonic-gate #include <netinet/in.h>
290Sstevel@tonic-gate #include <pwd.h>
300Sstevel@tonic-gate #include <sys/errno.h>
310Sstevel@tonic-gate #include <sys/mutex.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/socket.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <sys/smedia.h>
401676Sjpk #include <tsol/label.h>
410Sstevel@tonic-gate #include "smserver.h"
420Sstevel@tonic-gate #include <bsm/audit.h>
430Sstevel@tonic-gate #include <bsm/libbsm.h>
440Sstevel@tonic-gate #include <bsm/audit_uevents.h>
450Sstevel@tonic-gate #include <bsm/audit_record.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /* Private Functions */
480Sstevel@tonic-gate static int selected(au_event_t, au_mask_t *, int);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static int audit_selected(door_data_t *);
510Sstevel@tonic-gate static int audit_na_selected(door_data_t *);
520Sstevel@tonic-gate static int audit_save_namask(door_data_t *door_dp);
530Sstevel@tonic-gate static int audit_save_policy(door_data_t *door_dp);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * can_audit:
570Sstevel@tonic-gate  *	Return 1 if audit module is loaded.
580Sstevel@tonic-gate  *	Return 0 otherwise.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate int
620Sstevel@tonic-gate can_audit(void)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	static int auc = AUC_UNSET;
650Sstevel@tonic-gate 	int cond = 0;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if (auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond))) {
680Sstevel@tonic-gate 		auc = AUC_DISABLED;
690Sstevel@tonic-gate 	} else {
700Sstevel@tonic-gate 		auc = cond;
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 	if (auc == AUC_DISABLED)
730Sstevel@tonic-gate 		return (0);
740Sstevel@tonic-gate 	else return (1);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static int
780Sstevel@tonic-gate audit_save_policy(door_data_t *door_dp)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	int policy;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (auditon(A_GETPOLICY, (caddr_t)&policy, sizeof (policy))) {
830Sstevel@tonic-gate 		return (-1);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	door_dp->audit_policy = policy;
860Sstevel@tonic-gate 	return (0);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * audit_init():
910Sstevel@tonic-gate  *	Initialize variables.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate void
940Sstevel@tonic-gate audit_init(door_data_t *door_dp)
950Sstevel@tonic-gate {
964321Scasper 	door_dp->audit_auid = (uid_t)-1;
974321Scasper 	door_dp->audit_uid = (uid_t)-1;
984321Scasper 	door_dp->audit_euid = (uid_t)-1;
994321Scasper 	door_dp->audit_gid = (gid_t)-1;
1004321Scasper 	door_dp->audit_egid = (gid_t)-1;
1010Sstevel@tonic-gate 	door_dp->audit_pid = -1;
1020Sstevel@tonic-gate 	door_dp->audit_tid.at_port = 0;
1030Sstevel@tonic-gate 	door_dp->audit_tid.at_type = 0;
1040Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[0] = 0;
1050Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[1] = 0;
1060Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[2] = 0;
1070Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[3] = 0;
1080Sstevel@tonic-gate 	door_dp->audit_namask.am_success = (int)-1;
1090Sstevel@tonic-gate 	door_dp->audit_namask.am_failure = (int)-1;
1100Sstevel@tonic-gate 	door_dp->audit_event = 0;
1110Sstevel@tonic-gate 	door_dp->audit_sorf = -2;
1120Sstevel@tonic-gate 	door_dp->audit_user = NULL;
1130Sstevel@tonic-gate 	door_dp->audit_text[0] = NULL;
1140Sstevel@tonic-gate 	door_dp->audit_text1[0] = NULL;
1150Sstevel@tonic-gate 	door_dp->audit_na = 0;
116*7753STon.Nguyen@Sun.COM 	door_dp->audit_asid = (au_asid_t)(-1);
1170Sstevel@tonic-gate 	door_dp->audit_path = NULL;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate int
1210Sstevel@tonic-gate audit_save_me(door_data_t	*door_dp)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	door_cred_t	client_cred;
1240Sstevel@tonic-gate 	int		ret_val;
1250Sstevel@tonic-gate 	int		i;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	ret_val = door_cred(&client_cred);
1280Sstevel@tonic-gate 	if (ret_val == -1)
1290Sstevel@tonic-gate 		return (ret_val);
1300Sstevel@tonic-gate 	door_dp->audit_ap.ap_pid = client_cred.dc_pid;
1310Sstevel@tonic-gate 	ret_val = auditon(A_GETPINFO_ADDR, (caddr_t)&door_dp->audit_ap,
1320Sstevel@tonic-gate 		sizeof (door_dp->audit_ap));
1330Sstevel@tonic-gate 	if (ret_val == -1)
1340Sstevel@tonic-gate 		return (ret_val);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	door_dp->audit_auid = door_dp->audit_ap.ap_auid;
1370Sstevel@tonic-gate 	door_dp->audit_euid = client_cred.dc_euid;
1380Sstevel@tonic-gate 	door_dp->audit_egid = client_cred.dc_egid;
1390Sstevel@tonic-gate 	door_dp->audit_uid = client_cred.dc_ruid;
1400Sstevel@tonic-gate 	door_dp->audit_gid = client_cred.dc_rgid;
1410Sstevel@tonic-gate 	door_dp->audit_pid = client_cred.dc_pid;
1420Sstevel@tonic-gate 	door_dp->audit_asid = door_dp->audit_ap.ap_asid;
1430Sstevel@tonic-gate 	door_dp->audit_tid.at_port = door_dp->audit_ap.ap_termid.at_port;
1440Sstevel@tonic-gate 	door_dp->audit_tid.at_type = door_dp->audit_ap.ap_termid.at_type;
1450Sstevel@tonic-gate 	for (i = 0; i < (door_dp->audit_ap.ap_termid.at_type/4); i++)
1460Sstevel@tonic-gate 		door_dp->audit_tid.at_addr[i] =
1470Sstevel@tonic-gate 			door_dp->audit_ap.ap_termid.at_addr[i];
1480Sstevel@tonic-gate 	(void) audit_save_policy(door_dp);
1490Sstevel@tonic-gate 	return (0);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * audit_save_namask():
1540Sstevel@tonic-gate  *	Save the namask using the naflags entry in the audit_control file.
1550Sstevel@tonic-gate  *	Return 0 if successful.
1560Sstevel@tonic-gate  *	Return -1, and don't change the namask, if failed.
1570Sstevel@tonic-gate  *	Side Effect: Sets audit_na to -1 if error, 1 if successful.
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate static int
1600Sstevel@tonic-gate audit_save_namask(door_data_t *door_dp)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	au_mask_t mask;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	door_dp->audit_na = -1;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	/*
1670Sstevel@tonic-gate 	 * get non-attributable system event mask from kernel.
1680Sstevel@tonic-gate 	 */
1690Sstevel@tonic-gate 	if (auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask)) != 0) {
1700Sstevel@tonic-gate 		return (-1);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	door_dp->audit_namask.am_success = mask.am_success;
1740Sstevel@tonic-gate 	door_dp->audit_namask.am_failure = mask.am_failure;
1750Sstevel@tonic-gate 	door_dp->audit_na = 1;
1760Sstevel@tonic-gate 	return (0);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * audit_audit:
1810Sstevel@tonic-gate  *	Cut and audit record if it is selected.
1820Sstevel@tonic-gate  *	Return 0, if successfully written.
1830Sstevel@tonic-gate  *	Return 0, if not written, and not expected to write.
1840Sstevel@tonic-gate  *	Return -1, if not written because of unexpected error.
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate int
1870Sstevel@tonic-gate audit_audit(door_data_t *door_dp)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	int ad;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (can_audit() == 0) {
1920Sstevel@tonic-gate 		return (0);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (door_dp->audit_na) {
1960Sstevel@tonic-gate 		if (!audit_na_selected(door_dp)) {
1970Sstevel@tonic-gate 			return (0);
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 	} else if (!audit_selected(door_dp)) {
2000Sstevel@tonic-gate 		return (0);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if ((ad = au_open()) == -1) {
2040Sstevel@tonic-gate 		return (-1);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	(void) au_write(ad, au_to_subject_ex(door_dp->audit_auid,
2080Sstevel@tonic-gate 		door_dp->audit_euid,
2090Sstevel@tonic-gate 		door_dp->audit_egid,
2100Sstevel@tonic-gate 		door_dp->audit_uid, door_dp->audit_gid, door_dp->audit_pid,
2110Sstevel@tonic-gate 		door_dp->audit_asid, &door_dp->audit_tid));
2122425Sgww 	if (is_system_labeled())
2132425Sgww 		(void) au_write(ad, au_to_mylabel());
2140Sstevel@tonic-gate 	if (door_dp->audit_policy & AUDIT_GROUP) {
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		int ng;
2170Sstevel@tonic-gate 		gid_t grplst[NGROUPS_MAX];
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		(void) memset(grplst, 0, sizeof (grplst));
2200Sstevel@tonic-gate 		if ((ng = getgroups(NGROUPS_UMAX, grplst))) {
2210Sstevel@tonic-gate 			(void) au_write(ad, au_to_newgroups(ng, grplst));
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 	if (strlen(door_dp->audit_text) != 0) {
2250Sstevel@tonic-gate 		(void) au_write(ad, au_to_text(door_dp->audit_text));
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 	if (strlen(door_dp->audit_text1) != 0) {
2280Sstevel@tonic-gate 		(void) au_write(ad, au_to_text(door_dp->audit_text1));
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 	if (door_dp->audit_path != NULL) {
2310Sstevel@tonic-gate 		(void) au_write(ad, au_to_path(door_dp->audit_path));
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate #ifdef _LP64
2340Sstevel@tonic-gate 	(void) au_write(ad, au_to_return64((door_dp->audit_sorf == 0) ? 0 : -1,
2350Sstevel@tonic-gate 		(int64_t)door_dp->audit_sorf));
2360Sstevel@tonic-gate #else
2370Sstevel@tonic-gate 	(void) au_write(ad, au_to_return32((door_dp->audit_sorf == 0) ? 0 : -1,
2380Sstevel@tonic-gate 		(int32_t)door_dp->audit_sorf));
2390Sstevel@tonic-gate #endif
2400Sstevel@tonic-gate 	if (au_close(ad, 1, door_dp->audit_event) < 0) {
2410Sstevel@tonic-gate 		(void) au_close(ad, 0, 0);
2420Sstevel@tonic-gate 		return (-1);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	return (0);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate static int
2490Sstevel@tonic-gate audit_na_selected(door_data_t *door_dp)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	if (door_dp->audit_na == -1) {
2520Sstevel@tonic-gate 		return (-1);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	return (selected(door_dp->audit_event,
2560Sstevel@tonic-gate 		&door_dp->audit_namask, door_dp->audit_sorf));
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate static int
2600Sstevel@tonic-gate audit_selected(door_data_t *door_dp)
2610Sstevel@tonic-gate {
2620Sstevel@tonic-gate 
2634321Scasper 	if (door_dp->audit_uid > MAXUID) {
2640Sstevel@tonic-gate 		(void) audit_save_namask(door_dp);
2650Sstevel@tonic-gate 		return (audit_na_selected(door_dp));
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	return (selected(door_dp->audit_event,
2690Sstevel@tonic-gate 		&door_dp->audit_ap.ap_mask, door_dp->audit_sorf));
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate static int
2730Sstevel@tonic-gate selected(au_event_t e, au_mask_t *m, int sorf)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	int prs_sorf;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (sorf == 0) {
2780Sstevel@tonic-gate 		prs_sorf = AU_PRS_SUCCESS;
2790Sstevel@tonic-gate 	} else if (sorf == -1) {
2800Sstevel@tonic-gate 		prs_sorf = AU_PRS_FAILURE;
2810Sstevel@tonic-gate 	} else {
2820Sstevel@tonic-gate 		prs_sorf = AU_PRS_BOTH;
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
2860Sstevel@tonic-gate }
287