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