xref: /illumos-gate/usr/src/cmd/smserverd/myaudit.c (revision 14916c4b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2296093503SMarek Pospisil  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <netdb.h>
287c478bd9Sstevel@tonic-gate #include <netinet/in.h>
297c478bd9Sstevel@tonic-gate #include <pwd.h>
307c478bd9Sstevel@tonic-gate #include <sys/errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/socket.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
3967dbe2beSCasper H.S. Dik #include <alloca.h>
407c478bd9Sstevel@tonic-gate #include <sys/smedia.h>
4145916cd2Sjpk #include <tsol/label.h>
427c478bd9Sstevel@tonic-gate #include "smserver.h"
437c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
447c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
457c478bd9Sstevel@tonic-gate #include <bsm/audit_uevents.h>
467c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* Private Functions */
497c478bd9Sstevel@tonic-gate static int selected(au_event_t, au_mask_t *, int);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static int audit_selected(door_data_t *);
527c478bd9Sstevel@tonic-gate static int audit_na_selected(door_data_t *);
537c478bd9Sstevel@tonic-gate static int audit_save_namask(door_data_t *door_dp);
547c478bd9Sstevel@tonic-gate static int audit_save_policy(door_data_t *door_dp);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * can_audit:
587c478bd9Sstevel@tonic-gate  *	Return 1 if audit module is loaded.
597c478bd9Sstevel@tonic-gate  *	Return 0 otherwise.
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate int
can_audit(void)637c478bd9Sstevel@tonic-gate can_audit(void)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	static int auc = AUC_UNSET;
667c478bd9Sstevel@tonic-gate 	int cond = 0;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond))) {
697c478bd9Sstevel@tonic-gate 		auc = AUC_DISABLED;
707c478bd9Sstevel@tonic-gate 	} else {
717c478bd9Sstevel@tonic-gate 		auc = cond;
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	if (auc == AUC_DISABLED)
747c478bd9Sstevel@tonic-gate 		return (0);
757c478bd9Sstevel@tonic-gate 	else return (1);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static int
audit_save_policy(door_data_t * door_dp)797c478bd9Sstevel@tonic-gate audit_save_policy(door_data_t *door_dp)
807c478bd9Sstevel@tonic-gate {
8196093503SMarek Pospisil 	uint32_t policy;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (auditon(A_GETPOLICY, (caddr_t)&policy, sizeof (policy))) {
847c478bd9Sstevel@tonic-gate 		return (-1);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	door_dp->audit_policy = policy;
877c478bd9Sstevel@tonic-gate 	return (0);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * audit_init():
927c478bd9Sstevel@tonic-gate  *	Initialize variables.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate void
audit_init(door_data_t * door_dp)957c478bd9Sstevel@tonic-gate audit_init(door_data_t *door_dp)
967c478bd9Sstevel@tonic-gate {
97f48205beScasper 	door_dp->audit_auid = (uid_t)-1;
98f48205beScasper 	door_dp->audit_uid = (uid_t)-1;
99f48205beScasper 	door_dp->audit_euid = (uid_t)-1;
100f48205beScasper 	door_dp->audit_gid = (gid_t)-1;
101f48205beScasper 	door_dp->audit_egid = (gid_t)-1;
1027c478bd9Sstevel@tonic-gate 	door_dp->audit_pid = -1;
1037c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_port = 0;
1047c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_type = 0;
1057c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[0] = 0;
1067c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[1] = 0;
1077c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[2] = 0;
1087c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_addr[3] = 0;
1097c478bd9Sstevel@tonic-gate 	door_dp->audit_namask.am_success = (int)-1;
1107c478bd9Sstevel@tonic-gate 	door_dp->audit_namask.am_failure = (int)-1;
1117c478bd9Sstevel@tonic-gate 	door_dp->audit_event = 0;
1127c478bd9Sstevel@tonic-gate 	door_dp->audit_sorf = -2;
1137c478bd9Sstevel@tonic-gate 	door_dp->audit_user = NULL;
114*14916c4bSToomas Soome 	door_dp->audit_text[0] = '\0';
115*14916c4bSToomas Soome 	door_dp->audit_text1[0] = '\0';
1167c478bd9Sstevel@tonic-gate 	door_dp->audit_na = 0;
117d0fa49b7STony Nguyen 	door_dp->audit_asid = (au_asid_t)(-1);
1187c478bd9Sstevel@tonic-gate 	door_dp->audit_path = NULL;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate int
audit_save_me(door_data_t * door_dp)1227c478bd9Sstevel@tonic-gate audit_save_me(door_data_t	*door_dp)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	door_cred_t	client_cred;
1257c478bd9Sstevel@tonic-gate 	int		ret_val;
1267c478bd9Sstevel@tonic-gate 	int		i;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	ret_val = door_cred(&client_cred);
1297c478bd9Sstevel@tonic-gate 	if (ret_val == -1)
1307c478bd9Sstevel@tonic-gate 		return (ret_val);
1317c478bd9Sstevel@tonic-gate 	door_dp->audit_ap.ap_pid = client_cred.dc_pid;
1327c478bd9Sstevel@tonic-gate 	ret_val = auditon(A_GETPINFO_ADDR, (caddr_t)&door_dp->audit_ap,
13367dbe2beSCasper H.S. Dik 	    sizeof (door_dp->audit_ap));
1347c478bd9Sstevel@tonic-gate 	if (ret_val == -1)
1357c478bd9Sstevel@tonic-gate 		return (ret_val);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	door_dp->audit_auid = door_dp->audit_ap.ap_auid;
1387c478bd9Sstevel@tonic-gate 	door_dp->audit_euid = client_cred.dc_euid;
1397c478bd9Sstevel@tonic-gate 	door_dp->audit_egid = client_cred.dc_egid;
1407c478bd9Sstevel@tonic-gate 	door_dp->audit_uid = client_cred.dc_ruid;
1417c478bd9Sstevel@tonic-gate 	door_dp->audit_gid = client_cred.dc_rgid;
1427c478bd9Sstevel@tonic-gate 	door_dp->audit_pid = client_cred.dc_pid;
1437c478bd9Sstevel@tonic-gate 	door_dp->audit_asid = door_dp->audit_ap.ap_asid;
1447c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_port = door_dp->audit_ap.ap_termid.at_port;
1457c478bd9Sstevel@tonic-gate 	door_dp->audit_tid.at_type = door_dp->audit_ap.ap_termid.at_type;
1467c478bd9Sstevel@tonic-gate 	for (i = 0; i < (door_dp->audit_ap.ap_termid.at_type/4); i++)
1477c478bd9Sstevel@tonic-gate 		door_dp->audit_tid.at_addr[i] =
14867dbe2beSCasper H.S. Dik 		    door_dp->audit_ap.ap_termid.at_addr[i];
1497c478bd9Sstevel@tonic-gate 	(void) audit_save_policy(door_dp);
1507c478bd9Sstevel@tonic-gate 	return (0);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * audit_save_namask():
1557c478bd9Sstevel@tonic-gate  *	Save the namask using the naflags entry in the audit_control file.
1567c478bd9Sstevel@tonic-gate  *	Return 0 if successful.
1577c478bd9Sstevel@tonic-gate  *	Return -1, and don't change the namask, if failed.
1587c478bd9Sstevel@tonic-gate  *	Side Effect: Sets audit_na to -1 if error, 1 if successful.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate static int
audit_save_namask(door_data_t * door_dp)1617c478bd9Sstevel@tonic-gate audit_save_namask(door_data_t *door_dp)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	au_mask_t mask;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	door_dp->audit_na = -1;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * get non-attributable system event mask from kernel.
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 	if (auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask)) != 0) {
1717c478bd9Sstevel@tonic-gate 		return (-1);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	door_dp->audit_namask.am_success = mask.am_success;
1757c478bd9Sstevel@tonic-gate 	door_dp->audit_namask.am_failure = mask.am_failure;
1767c478bd9Sstevel@tonic-gate 	door_dp->audit_na = 1;
1777c478bd9Sstevel@tonic-gate 	return (0);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * audit_audit:
1827c478bd9Sstevel@tonic-gate  *	Cut and audit record if it is selected.
1837c478bd9Sstevel@tonic-gate  *	Return 0, if successfully written.
1847c478bd9Sstevel@tonic-gate  *	Return 0, if not written, and not expected to write.
1857c478bd9Sstevel@tonic-gate  *	Return -1, if not written because of unexpected error.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate int
audit_audit(door_data_t * door_dp)1887c478bd9Sstevel@tonic-gate audit_audit(door_data_t *door_dp)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	int ad;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (can_audit() == 0) {
1937c478bd9Sstevel@tonic-gate 		return (0);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (door_dp->audit_na) {
1977c478bd9Sstevel@tonic-gate 		if (!audit_na_selected(door_dp)) {
1987c478bd9Sstevel@tonic-gate 			return (0);
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 	} else if (!audit_selected(door_dp)) {
2017c478bd9Sstevel@tonic-gate 		return (0);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if ((ad = au_open()) == -1) {
2057c478bd9Sstevel@tonic-gate 		return (-1);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	(void) au_write(ad, au_to_subject_ex(door_dp->audit_auid,
20967dbe2beSCasper H.S. Dik 	    door_dp->audit_euid,
21067dbe2beSCasper H.S. Dik 	    door_dp->audit_egid,
21167dbe2beSCasper H.S. Dik 	    door_dp->audit_uid, door_dp->audit_gid, door_dp->audit_pid,
21267dbe2beSCasper H.S. Dik 	    door_dp->audit_asid, &door_dp->audit_tid));
21381490fd2Sgww 	if (is_system_labeled())
21481490fd2Sgww 		(void) au_write(ad, au_to_mylabel());
2157c478bd9Sstevel@tonic-gate 	if (door_dp->audit_policy & AUDIT_GROUP) {
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		int ng;
21867dbe2beSCasper H.S. Dik 		int maxgrp = getgroups(0, NULL);
21967dbe2beSCasper H.S. Dik 		gid_t *grplst = alloca(maxgrp * sizeof (gid_t));
2207c478bd9Sstevel@tonic-gate 
22167dbe2beSCasper H.S. Dik 		if ((ng = getgroups(maxgrp, grplst))) {
2227c478bd9Sstevel@tonic-gate 			(void) au_write(ad, au_to_newgroups(ng, grplst));
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 	if (strlen(door_dp->audit_text) != 0) {
2267c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_text(door_dp->audit_text));
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	if (strlen(door_dp->audit_text1) != 0) {
2297c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_text(door_dp->audit_text1));
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	if (door_dp->audit_path != NULL) {
2327c478bd9Sstevel@tonic-gate 		(void) au_write(ad, au_to_path(door_dp->audit_path));
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate #ifdef _LP64
2357c478bd9Sstevel@tonic-gate 	(void) au_write(ad, au_to_return64((door_dp->audit_sorf == 0) ? 0 : -1,
23667dbe2beSCasper H.S. Dik 	    (int64_t)door_dp->audit_sorf));
2377c478bd9Sstevel@tonic-gate #else
2387c478bd9Sstevel@tonic-gate 	(void) au_write(ad, au_to_return32((door_dp->audit_sorf == 0) ? 0 : -1,
23967dbe2beSCasper H.S. Dik 	    (int32_t)door_dp->audit_sorf));
2407c478bd9Sstevel@tonic-gate #endif
2417c478bd9Sstevel@tonic-gate 	if (au_close(ad, 1, door_dp->audit_event) < 0) {
2427c478bd9Sstevel@tonic-gate 		(void) au_close(ad, 0, 0);
2437c478bd9Sstevel@tonic-gate 		return (-1);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	return (0);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static int
audit_na_selected(door_data_t * door_dp)2507c478bd9Sstevel@tonic-gate audit_na_selected(door_data_t *door_dp)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	if (door_dp->audit_na == -1) {
2537c478bd9Sstevel@tonic-gate 		return (-1);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	return (selected(door_dp->audit_event,
25767dbe2beSCasper H.S. Dik 	    &door_dp->audit_namask, door_dp->audit_sorf));
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate static int
audit_selected(door_data_t * door_dp)2617c478bd9Sstevel@tonic-gate audit_selected(door_data_t *door_dp)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 
264f48205beScasper 	if (door_dp->audit_uid > MAXUID) {
2657c478bd9Sstevel@tonic-gate 		(void) audit_save_namask(door_dp);
2667c478bd9Sstevel@tonic-gate 		return (audit_na_selected(door_dp));
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	return (selected(door_dp->audit_event,
27067dbe2beSCasper H.S. Dik 	    &door_dp->audit_ap.ap_mask, door_dp->audit_sorf));
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate static int
selected(au_event_t e,au_mask_t * m,int sorf)2747c478bd9Sstevel@tonic-gate selected(au_event_t e, au_mask_t *m, int sorf)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	int prs_sorf;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (sorf == 0) {
2797c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_SUCCESS;
2807c478bd9Sstevel@tonic-gate 	} else if (sorf == -1) {
2817c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_FAILURE;
2827c478bd9Sstevel@tonic-gate 	} else {
2837c478bd9Sstevel@tonic-gate 		prs_sorf = AU_PRS_BOTH;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD));
2877c478bd9Sstevel@tonic-gate }
288