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 /* 22*d0fa49b7STony Nguyen * Copyright 2008 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> 397c478bd9Sstevel@tonic-gate #include <sys/smedia.h> 4045916cd2Sjpk #include <tsol/label.h> 417c478bd9Sstevel@tonic-gate #include "smserver.h" 427c478bd9Sstevel@tonic-gate #include <bsm/audit.h> 437c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h> 447c478bd9Sstevel@tonic-gate #include <bsm/audit_uevents.h> 457c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h> 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /* Private Functions */ 487c478bd9Sstevel@tonic-gate static int selected(au_event_t, au_mask_t *, int); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate static int audit_selected(door_data_t *); 517c478bd9Sstevel@tonic-gate static int audit_na_selected(door_data_t *); 527c478bd9Sstevel@tonic-gate static int audit_save_namask(door_data_t *door_dp); 537c478bd9Sstevel@tonic-gate static int audit_save_policy(door_data_t *door_dp); 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate /* 567c478bd9Sstevel@tonic-gate * can_audit: 577c478bd9Sstevel@tonic-gate * Return 1 if audit module is loaded. 587c478bd9Sstevel@tonic-gate * Return 0 otherwise. 597c478bd9Sstevel@tonic-gate * 607c478bd9Sstevel@tonic-gate */ 617c478bd9Sstevel@tonic-gate int 627c478bd9Sstevel@tonic-gate can_audit(void) 637c478bd9Sstevel@tonic-gate { 647c478bd9Sstevel@tonic-gate static int auc = AUC_UNSET; 657c478bd9Sstevel@tonic-gate int cond = 0; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate if (auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond))) { 687c478bd9Sstevel@tonic-gate auc = AUC_DISABLED; 697c478bd9Sstevel@tonic-gate } else { 707c478bd9Sstevel@tonic-gate auc = cond; 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate if (auc == AUC_DISABLED) 737c478bd9Sstevel@tonic-gate return (0); 747c478bd9Sstevel@tonic-gate else return (1); 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate static int 787c478bd9Sstevel@tonic-gate audit_save_policy(door_data_t *door_dp) 797c478bd9Sstevel@tonic-gate { 807c478bd9Sstevel@tonic-gate int policy; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if (auditon(A_GETPOLICY, (caddr_t)&policy, sizeof (policy))) { 837c478bd9Sstevel@tonic-gate return (-1); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate door_dp->audit_policy = policy; 867c478bd9Sstevel@tonic-gate return (0); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * audit_init(): 917c478bd9Sstevel@tonic-gate * Initialize variables. 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate void 947c478bd9Sstevel@tonic-gate audit_init(door_data_t *door_dp) 957c478bd9Sstevel@tonic-gate { 96f48205beScasper door_dp->audit_auid = (uid_t)-1; 97f48205beScasper door_dp->audit_uid = (uid_t)-1; 98f48205beScasper door_dp->audit_euid = (uid_t)-1; 99f48205beScasper door_dp->audit_gid = (gid_t)-1; 100f48205beScasper door_dp->audit_egid = (gid_t)-1; 1017c478bd9Sstevel@tonic-gate door_dp->audit_pid = -1; 1027c478bd9Sstevel@tonic-gate door_dp->audit_tid.at_port = 0; 1037c478bd9Sstevel@tonic-gate door_dp->audit_tid.at_type = 0; 1047c478bd9Sstevel@tonic-gate door_dp->audit_tid.at_addr[0] = 0; 1057c478bd9Sstevel@tonic-gate door_dp->audit_tid.at_addr[1] = 0; 1067c478bd9Sstevel@tonic-gate door_dp->audit_tid.at_addr[2] = 0; 1077c478bd9Sstevel@tonic-gate door_dp->audit_tid.at_addr[3] = 0; 1087c478bd9Sstevel@tonic-gate door_dp->audit_namask.am_success = (int)-1; 1097c478bd9Sstevel@tonic-gate door_dp->audit_namask.am_failure = (int)-1; 1107c478bd9Sstevel@tonic-gate door_dp->audit_event = 0; 1117c478bd9Sstevel@tonic-gate door_dp->audit_sorf = -2; 1127c478bd9Sstevel@tonic-gate door_dp->audit_user = NULL; 1137c478bd9Sstevel@tonic-gate door_dp->audit_text[0] = NULL; 1147c478bd9Sstevel@tonic-gate door_dp->audit_text1[0] = NULL; 1157c478bd9Sstevel@tonic-gate door_dp->audit_na = 0; 116*d0fa49b7STony Nguyen door_dp->audit_asid = (au_asid_t)(-1); 1177c478bd9Sstevel@tonic-gate door_dp->audit_path = NULL; 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate int 1217c478bd9Sstevel@tonic-gate audit_save_me(door_data_t *door_dp) 1227c478bd9Sstevel@tonic-gate { 1237c478bd9Sstevel@tonic-gate door_cred_t client_cred; 1247c478bd9Sstevel@tonic-gate int ret_val; 1257c478bd9Sstevel@tonic-gate int i; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate ret_val = door_cred(&client_cred); 1287c478bd9Sstevel@tonic-gate if (ret_val == -1) 1297c478bd9Sstevel@tonic-gate return (ret_val); 1307c478bd9Sstevel@tonic-gate door_dp->audit_ap.ap_pid = client_cred.dc_pid; 1317c478bd9Sstevel@tonic-gate ret_val = auditon(A_GETPINFO_ADDR, (caddr_t)&door_dp->audit_ap, 1327c478bd9Sstevel@tonic-gate sizeof (door_dp->audit_ap)); 1337c478bd9Sstevel@tonic-gate if (ret_val == -1) 1347c478bd9Sstevel@tonic-gate return (ret_val); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate door_dp->audit_auid = door_dp->audit_ap.ap_auid; 1377c478bd9Sstevel@tonic-gate door_dp->audit_euid = client_cred.dc_euid; 1387c478bd9Sstevel@tonic-gate door_dp->audit_egid = client_cred.dc_egid; 1397c478bd9Sstevel@tonic-gate door_dp->audit_uid = client_cred.dc_ruid; 1407c478bd9Sstevel@tonic-gate door_dp->audit_gid = client_cred.dc_rgid; 1417c478bd9Sstevel@tonic-gate door_dp->audit_pid = client_cred.dc_pid; 1427c478bd9Sstevel@tonic-gate door_dp->audit_asid = door_dp->audit_ap.ap_asid; 1437c478bd9Sstevel@tonic-gate door_dp->audit_tid.at_port = door_dp->audit_ap.ap_termid.at_port; 1447c478bd9Sstevel@tonic-gate door_dp->audit_tid.at_type = door_dp->audit_ap.ap_termid.at_type; 1457c478bd9Sstevel@tonic-gate for (i = 0; i < (door_dp->audit_ap.ap_termid.at_type/4); i++) 1467c478bd9Sstevel@tonic-gate door_dp->audit_tid.at_addr[i] = 1477c478bd9Sstevel@tonic-gate door_dp->audit_ap.ap_termid.at_addr[i]; 1487c478bd9Sstevel@tonic-gate (void) audit_save_policy(door_dp); 1497c478bd9Sstevel@tonic-gate return (0); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * audit_save_namask(): 1547c478bd9Sstevel@tonic-gate * Save the namask using the naflags entry in the audit_control file. 1557c478bd9Sstevel@tonic-gate * Return 0 if successful. 1567c478bd9Sstevel@tonic-gate * Return -1, and don't change the namask, if failed. 1577c478bd9Sstevel@tonic-gate * Side Effect: Sets audit_na to -1 if error, 1 if successful. 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate static int 1607c478bd9Sstevel@tonic-gate audit_save_namask(door_data_t *door_dp) 1617c478bd9Sstevel@tonic-gate { 1627c478bd9Sstevel@tonic-gate au_mask_t mask; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate door_dp->audit_na = -1; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * get non-attributable system event mask from kernel. 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate if (auditon(A_GETKMASK, (caddr_t)&mask, sizeof (mask)) != 0) { 1707c478bd9Sstevel@tonic-gate return (-1); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate door_dp->audit_namask.am_success = mask.am_success; 1747c478bd9Sstevel@tonic-gate door_dp->audit_namask.am_failure = mask.am_failure; 1757c478bd9Sstevel@tonic-gate door_dp->audit_na = 1; 1767c478bd9Sstevel@tonic-gate return (0); 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* 1807c478bd9Sstevel@tonic-gate * audit_audit: 1817c478bd9Sstevel@tonic-gate * Cut and audit record if it is selected. 1827c478bd9Sstevel@tonic-gate * Return 0, if successfully written. 1837c478bd9Sstevel@tonic-gate * Return 0, if not written, and not expected to write. 1847c478bd9Sstevel@tonic-gate * Return -1, if not written because of unexpected error. 1857c478bd9Sstevel@tonic-gate */ 1867c478bd9Sstevel@tonic-gate int 1877c478bd9Sstevel@tonic-gate audit_audit(door_data_t *door_dp) 1887c478bd9Sstevel@tonic-gate { 1897c478bd9Sstevel@tonic-gate int ad; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate if (can_audit() == 0) { 1927c478bd9Sstevel@tonic-gate return (0); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate if (door_dp->audit_na) { 1967c478bd9Sstevel@tonic-gate if (!audit_na_selected(door_dp)) { 1977c478bd9Sstevel@tonic-gate return (0); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate } else if (!audit_selected(door_dp)) { 2007c478bd9Sstevel@tonic-gate return (0); 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate if ((ad = au_open()) == -1) { 2047c478bd9Sstevel@tonic-gate return (-1); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_subject_ex(door_dp->audit_auid, 2087c478bd9Sstevel@tonic-gate door_dp->audit_euid, 2097c478bd9Sstevel@tonic-gate door_dp->audit_egid, 2107c478bd9Sstevel@tonic-gate door_dp->audit_uid, door_dp->audit_gid, door_dp->audit_pid, 2117c478bd9Sstevel@tonic-gate door_dp->audit_asid, &door_dp->audit_tid)); 21281490fd2Sgww if (is_system_labeled()) 21381490fd2Sgww (void) au_write(ad, au_to_mylabel()); 2147c478bd9Sstevel@tonic-gate if (door_dp->audit_policy & AUDIT_GROUP) { 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate int ng; 2177c478bd9Sstevel@tonic-gate gid_t grplst[NGROUPS_MAX]; 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate (void) memset(grplst, 0, sizeof (grplst)); 2207c478bd9Sstevel@tonic-gate if ((ng = getgroups(NGROUPS_UMAX, grplst))) { 2217c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_newgroups(ng, grplst)); 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate if (strlen(door_dp->audit_text) != 0) { 2257c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_text(door_dp->audit_text)); 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate if (strlen(door_dp->audit_text1) != 0) { 2287c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_text(door_dp->audit_text1)); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate if (door_dp->audit_path != NULL) { 2317c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_path(door_dp->audit_path)); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate #ifdef _LP64 2347c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_return64((door_dp->audit_sorf == 0) ? 0 : -1, 2357c478bd9Sstevel@tonic-gate (int64_t)door_dp->audit_sorf)); 2367c478bd9Sstevel@tonic-gate #else 2377c478bd9Sstevel@tonic-gate (void) au_write(ad, au_to_return32((door_dp->audit_sorf == 0) ? 0 : -1, 2387c478bd9Sstevel@tonic-gate (int32_t)door_dp->audit_sorf)); 2397c478bd9Sstevel@tonic-gate #endif 2407c478bd9Sstevel@tonic-gate if (au_close(ad, 1, door_dp->audit_event) < 0) { 2417c478bd9Sstevel@tonic-gate (void) au_close(ad, 0, 0); 2427c478bd9Sstevel@tonic-gate return (-1); 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate return (0); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate static int 2497c478bd9Sstevel@tonic-gate audit_na_selected(door_data_t *door_dp) 2507c478bd9Sstevel@tonic-gate { 2517c478bd9Sstevel@tonic-gate if (door_dp->audit_na == -1) { 2527c478bd9Sstevel@tonic-gate return (-1); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate return (selected(door_dp->audit_event, 2567c478bd9Sstevel@tonic-gate &door_dp->audit_namask, door_dp->audit_sorf)); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate static int 2607c478bd9Sstevel@tonic-gate audit_selected(door_data_t *door_dp) 2617c478bd9Sstevel@tonic-gate { 2627c478bd9Sstevel@tonic-gate 263f48205beScasper if (door_dp->audit_uid > MAXUID) { 2647c478bd9Sstevel@tonic-gate (void) audit_save_namask(door_dp); 2657c478bd9Sstevel@tonic-gate return (audit_na_selected(door_dp)); 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate return (selected(door_dp->audit_event, 2697c478bd9Sstevel@tonic-gate &door_dp->audit_ap.ap_mask, door_dp->audit_sorf)); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate static int 2737c478bd9Sstevel@tonic-gate selected(au_event_t e, au_mask_t *m, int sorf) 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate int prs_sorf; 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if (sorf == 0) { 2787c478bd9Sstevel@tonic-gate prs_sorf = AU_PRS_SUCCESS; 2797c478bd9Sstevel@tonic-gate } else if (sorf == -1) { 2807c478bd9Sstevel@tonic-gate prs_sorf = AU_PRS_FAILURE; 2817c478bd9Sstevel@tonic-gate } else { 2827c478bd9Sstevel@tonic-gate prs_sorf = AU_PRS_BOTH; 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD)); 2867c478bd9Sstevel@tonic-gate } 287