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*11134SCasper.Dik@Sun.COM * Copyright 2009 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> 39*11134SCasper.Dik@Sun.COM #include <alloca.h> 400Sstevel@tonic-gate #include <sys/smedia.h> 411676Sjpk #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 { 974321Scasper door_dp->audit_auid = (uid_t)-1; 984321Scasper door_dp->audit_uid = (uid_t)-1; 994321Scasper door_dp->audit_euid = (uid_t)-1; 1004321Scasper door_dp->audit_gid = (gid_t)-1; 1014321Scasper door_dp->audit_egid = (gid_t)-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; 1177753STon.Nguyen@Sun.COM door_dp->audit_asid = (au_asid_t)(-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, 133*11134SCasper.Dik@Sun.COM 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] = 148*11134SCasper.Dik@Sun.COM 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, 209*11134SCasper.Dik@Sun.COM door_dp->audit_euid, 210*11134SCasper.Dik@Sun.COM door_dp->audit_egid, 211*11134SCasper.Dik@Sun.COM door_dp->audit_uid, door_dp->audit_gid, door_dp->audit_pid, 212*11134SCasper.Dik@Sun.COM door_dp->audit_asid, &door_dp->audit_tid)); 2132425Sgww if (is_system_labeled()) 2142425Sgww (void) au_write(ad, au_to_mylabel()); 2150Sstevel@tonic-gate if (door_dp->audit_policy & AUDIT_GROUP) { 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate int ng; 218*11134SCasper.Dik@Sun.COM int maxgrp = getgroups(0, NULL); 219*11134SCasper.Dik@Sun.COM gid_t *grplst = alloca(maxgrp * sizeof (gid_t)); 2200Sstevel@tonic-gate 221*11134SCasper.Dik@Sun.COM if ((ng = getgroups(maxgrp, grplst))) { 2220Sstevel@tonic-gate (void) au_write(ad, au_to_newgroups(ng, grplst)); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate if (strlen(door_dp->audit_text) != 0) { 2260Sstevel@tonic-gate (void) au_write(ad, au_to_text(door_dp->audit_text)); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate if (strlen(door_dp->audit_text1) != 0) { 2290Sstevel@tonic-gate (void) au_write(ad, au_to_text(door_dp->audit_text1)); 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate if (door_dp->audit_path != NULL) { 2320Sstevel@tonic-gate (void) au_write(ad, au_to_path(door_dp->audit_path)); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate #ifdef _LP64 2350Sstevel@tonic-gate (void) au_write(ad, au_to_return64((door_dp->audit_sorf == 0) ? 0 : -1, 236*11134SCasper.Dik@Sun.COM (int64_t)door_dp->audit_sorf)); 2370Sstevel@tonic-gate #else 2380Sstevel@tonic-gate (void) au_write(ad, au_to_return32((door_dp->audit_sorf == 0) ? 0 : -1, 239*11134SCasper.Dik@Sun.COM (int32_t)door_dp->audit_sorf)); 2400Sstevel@tonic-gate #endif 2410Sstevel@tonic-gate if (au_close(ad, 1, door_dp->audit_event) < 0) { 2420Sstevel@tonic-gate (void) au_close(ad, 0, 0); 2430Sstevel@tonic-gate return (-1); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate return (0); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate static int 2500Sstevel@tonic-gate audit_na_selected(door_data_t *door_dp) 2510Sstevel@tonic-gate { 2520Sstevel@tonic-gate if (door_dp->audit_na == -1) { 2530Sstevel@tonic-gate return (-1); 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate return (selected(door_dp->audit_event, 257*11134SCasper.Dik@Sun.COM &door_dp->audit_namask, door_dp->audit_sorf)); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate static int 2610Sstevel@tonic-gate audit_selected(door_data_t *door_dp) 2620Sstevel@tonic-gate { 2630Sstevel@tonic-gate 2644321Scasper if (door_dp->audit_uid > MAXUID) { 2650Sstevel@tonic-gate (void) audit_save_namask(door_dp); 2660Sstevel@tonic-gate return (audit_na_selected(door_dp)); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate return (selected(door_dp->audit_event, 270*11134SCasper.Dik@Sun.COM &door_dp->audit_ap.ap_mask, door_dp->audit_sorf)); 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate static int 2740Sstevel@tonic-gate selected(au_event_t e, au_mask_t *m, int sorf) 2750Sstevel@tonic-gate { 2760Sstevel@tonic-gate int prs_sorf; 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate if (sorf == 0) { 2790Sstevel@tonic-gate prs_sorf = AU_PRS_SUCCESS; 2800Sstevel@tonic-gate } else if (sorf == -1) { 2810Sstevel@tonic-gate prs_sorf = AU_PRS_FAILURE; 2820Sstevel@tonic-gate } else { 2830Sstevel@tonic-gate prs_sorf = AU_PRS_BOTH; 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate return (au_preselect(e, m, prs_sorf, AU_PRS_REREAD)); 2870Sstevel@tonic-gate } 288