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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate * 267c478bd9Sstevel@tonic-gate * ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate */ 287c478bd9Sstevel@tonic-gate package com.sun.audit; 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate import java.util.Stack; 317c478bd9Sstevel@tonic-gate import java.io.Serializable; 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate public class AuditSession implements Serializable 347c478bd9Sstevel@tonic-gate { 357c478bd9Sstevel@tonic-gate // LD_LIBRARY_PATH determines directory for libadt_jni.so. 367c478bd9Sstevel@tonic-gate // When you get an UnsatisfiedLinkError, and have determined 377c478bd9Sstevel@tonic-gate // the path is right, the problem is probably in the library 387c478bd9Sstevel@tonic-gate // itself, but Java doesn't say what it is. Set up a cc 397c478bd9Sstevel@tonic-gate // command to link the library to see what the actual error 407c478bd9Sstevel@tonic-gate // is. 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate static private boolean library_loaded = false; 437c478bd9Sstevel@tonic-gate static { 447c478bd9Sstevel@tonic-gate try { 457c478bd9Sstevel@tonic-gate System.loadLibrary("adt_jni"); 467c478bd9Sstevel@tonic-gate library_loaded = true; 477c478bd9Sstevel@tonic-gate } catch (Exception ex) { 487c478bd9Sstevel@tonic-gate library_loaded = false; 497c478bd9Sstevel@tonic-gate } catch (java.lang.UnsatisfiedLinkError ul) { 507c478bd9Sstevel@tonic-gate library_loaded = false; 517c478bd9Sstevel@tonic-gate } 527c478bd9Sstevel@tonic-gate } bsmAuditOn()537c478bd9Sstevel@tonic-gate private native boolean bsmAuditOn(); startSession( byte[] context, long flags)547c478bd9Sstevel@tonic-gate private native byte[] startSession( 557c478bd9Sstevel@tonic-gate byte[] context, long flags) 567c478bd9Sstevel@tonic-gate throws Error; dupSession( byte[] source)577c478bd9Sstevel@tonic-gate private native byte[] dupSession( 587c478bd9Sstevel@tonic-gate byte[] source) 597c478bd9Sstevel@tonic-gate throws Error; endSession(byte[] sessionHandle)607c478bd9Sstevel@tonic-gate private native void endSession(byte[] sessionHandle) 617c478bd9Sstevel@tonic-gate throws Error; getSessionId(byte[] sessionHandle)627c478bd9Sstevel@tonic-gate private native String getSessionId(byte[] sessionHandle) 637c478bd9Sstevel@tonic-gate throws Error; exportSessionData(byte[] sessionHandle)647c478bd9Sstevel@tonic-gate private native byte[] exportSessionData(byte[] sessionHandle) 657c478bd9Sstevel@tonic-gate throws Error; sessionAttr(byte[] sessionHandle, int euid, int egid, int ruid, int rgid, String hostname, int context)667c478bd9Sstevel@tonic-gate private native void sessionAttr(byte[] sessionHandle, 677c478bd9Sstevel@tonic-gate int euid, int egid, int ruid, int rgid, 687c478bd9Sstevel@tonic-gate String hostname, int context) 697c478bd9Sstevel@tonic-gate throws Error; 707c478bd9Sstevel@tonic-gate //TSOL only 717c478bd9Sstevel@tonic-gate // private native void setSL(byte[] sessionHandle, String label); 727c478bd9Sstevel@tonic-gate //end TSOL 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate private byte[] sh; // current session handle 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate private Stack stateStack = new Stack(); // for push/pop 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate boolean AuditIsOn = true; // Underlying BSM state 797c478bd9Sstevel@tonic-gate boolean ValidSession = true; // Session object state 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate // Create an audit session. 827c478bd9Sstevel@tonic-gate // The fixed length of 8 corresponds to a 64 bit pointer; 837c478bd9Sstevel@tonic-gate // valid overkill on 32 bit systems. 847c478bd9Sstevel@tonic-gate // Even if bsmAuditOn returns false, need to create a session. 857c478bd9Sstevel@tonic-gate AuditSession(byte[] context)867c478bd9Sstevel@tonic-gate public AuditSession(byte[] context) { 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate if (!library_loaded) { 897c478bd9Sstevel@tonic-gate ValidSession = false; 907c478bd9Sstevel@tonic-gate AuditIsOn = false; 917c478bd9Sstevel@tonic-gate sh = new byte[8]; // NULL pointer in C 927c478bd9Sstevel@tonic-gate return; 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate AuditIsOn = bsmAuditOn(); 957c478bd9Sstevel@tonic-gate try { 967c478bd9Sstevel@tonic-gate sh = startSession(context, 0); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate catch (java.lang.Exception e) { 997c478bd9Sstevel@tonic-gate ValidSession = false; 1007c478bd9Sstevel@tonic-gate sh = new byte[8]; 1017c478bd9Sstevel@tonic-gate } 102*55fea89dSDan Cross catch (java.lang.Error e) { 1037c478bd9Sstevel@tonic-gate ValidSession = false; 1047c478bd9Sstevel@tonic-gate sh = new byte[8]; 1057c478bd9Sstevel@tonic-gate throw e; 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate // getSession() is for use by AuditEvent, not much use to caller of 1107c478bd9Sstevel@tonic-gate // AuditSession "package protected" == not public 111*55fea89dSDan Cross // 1127c478bd9Sstevel@tonic-gate // If you think you need this C pointer (sh), see 1137c478bd9Sstevel@tonic-gate // exportSession() and the "context" parameter to 1147c478bd9Sstevel@tonic-gate // startSession() for a way to pass an audit thread from one 1157c478bd9Sstevel@tonic-gate // process to another or from one language to another. 1167c478bd9Sstevel@tonic-gate getSession()1177c478bd9Sstevel@tonic-gate byte[] getSession() { 1187c478bd9Sstevel@tonic-gate return sh; 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate getSessionId()1217c478bd9Sstevel@tonic-gate public String getSessionId() throws Exception { 1227c478bd9Sstevel@tonic-gate String sessionId; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate if (ValidSession) { 1257c478bd9Sstevel@tonic-gate try { 1267c478bd9Sstevel@tonic-gate sessionId = getSessionId(sh); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate catch (Exception e) { 1297c478bd9Sstevel@tonic-gate sessionId = null; 1307c478bd9Sstevel@tonic-gate throw e; 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate catch (Error e) { 1337c478bd9Sstevel@tonic-gate sessionId = null; 1347c478bd9Sstevel@tonic-gate throw e; 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate } else { 1377c478bd9Sstevel@tonic-gate sessionId = null; 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate return sessionId; 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate // auditOn: The return value does not reveal whether or 1437c478bd9Sstevel@tonic-gate // auditing is on, but whether or not the current audit 1447c478bd9Sstevel@tonic-gate // session was created ok. 1457c478bd9Sstevel@tonic-gate auditOn()1467c478bd9Sstevel@tonic-gate public boolean auditOn() { 1477c478bd9Sstevel@tonic-gate return (ValidSession); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate finalize()1507c478bd9Sstevel@tonic-gate public void finalize() { 1517c478bd9Sstevel@tonic-gate byte[] state; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate while (!stateStack.empty()) { 1547c478bd9Sstevel@tonic-gate state = (byte[])stateStack.pop(); 1557c478bd9Sstevel@tonic-gate endSession(state); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate endSession(sh); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate // Returns export data even if auditing is off. If the 1617c478bd9Sstevel@tonic-gate // session is invalid (no jni library, memory error in 1627c478bd9Sstevel@tonic-gate // startSession), returns null. 1637c478bd9Sstevel@tonic-gate // 1647c478bd9Sstevel@tonic-gate // If you use exportSession(), it is important that you first 1657c478bd9Sstevel@tonic-gate // call setUser() even when auditOn() returns false; otherwise 1667c478bd9Sstevel@tonic-gate // the exported session will result in remote processes being 1677c478bd9Sstevel@tonic-gate // unable to generate an valid audit trail. 1687c478bd9Sstevel@tonic-gate exportSession()1697c478bd9Sstevel@tonic-gate public byte[] exportSession() throws Exception { 1707c478bd9Sstevel@tonic-gate byte[] exportedData; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate if (ValidSession) { 1737c478bd9Sstevel@tonic-gate try { 1747c478bd9Sstevel@tonic-gate exportedData = exportSessionData(sh); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate catch (java.lang.Exception e) { 1777c478bd9Sstevel@tonic-gate throw e; 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate } else { 1807c478bd9Sstevel@tonic-gate exportedData = null; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate return exportedData; 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate // ADT_NEW, ADT_UPDATE and ADT_USER are the only valid values 1867c478bd9Sstevel@tonic-gate // for the context input to setUser(). If the user has 1877c478bd9Sstevel@tonic-gate // completed initial authentication, use ADT_NEW; if the user 1887c478bd9Sstevel@tonic-gate // is to change ids, such as to a role or to root, use 1897c478bd9Sstevel@tonic-gate // ADT_UPDATE. If the process audit context is already set, 1907c478bd9Sstevel@tonic-gate // use ADT_USER. 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate // If a uid or gid is unknown (e.g., unrecognized login id) 1937c478bd9Sstevel@tonic-gate // then use ADT_NO_ATTRIB for the uid/gid. 1947c478bd9Sstevel@tonic-gate // 1957c478bd9Sstevel@tonic-gate // For ADT_UPDATE only, use ADT_NO_CHANGE for any uid or gid 1967c478bd9Sstevel@tonic-gate // that you don't wish to change. 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate public static final int ADT_NEW = 0; 1997c478bd9Sstevel@tonic-gate public static final int ADT_UPDATE = 1; 2007c478bd9Sstevel@tonic-gate public static final int ADT_USER = 2; 2017c478bd9Sstevel@tonic-gate public static final int ADT_NO_ATTRIB = -1; 2027c478bd9Sstevel@tonic-gate public static final int ADT_NO_CHANGE = -2; 2037c478bd9Sstevel@tonic-gate setUser(int euid, int egid, int ruid, int rgid, String hostname, int context)2047c478bd9Sstevel@tonic-gate public void setUser(int euid, int egid, int ruid, int rgid, 2057c478bd9Sstevel@tonic-gate String hostname, int context) { 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate if (ValidSession) { 2087c478bd9Sstevel@tonic-gate try { 2097c478bd9Sstevel@tonic-gate sessionAttr(sh, euid, egid, ruid, rgid, 2107c478bd9Sstevel@tonic-gate hostname, context); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate catch (java.lang.Error e) { 2137c478bd9Sstevel@tonic-gate throw e; 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate // pushState duplicates the session handle, puts the source 2197c478bd9Sstevel@tonic-gate // handle on a stack, and makes the duplicate the current 2207c478bd9Sstevel@tonic-gate // handle dupSession throws an out of memory error to be 2217c478bd9Sstevel@tonic-gate // caught higher up. 2227c478bd9Sstevel@tonic-gate pushState()2237c478bd9Sstevel@tonic-gate public void pushState() throws Exception { 2247c478bd9Sstevel@tonic-gate byte[] copy; 2257c478bd9Sstevel@tonic-gate int i; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate copy = dupSession(sh); 2287c478bd9Sstevel@tonic-gate stateStack.push(sh); 2297c478bd9Sstevel@tonic-gate sh = copy; 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate 232*55fea89dSDan Cross // popState frees the current handle and pops a handle off a 2337c478bd9Sstevel@tonic-gate // stack to become the new current handle. 2347c478bd9Sstevel@tonic-gate // As with pushState, it lets the caller deal with any exceptions. 2357c478bd9Sstevel@tonic-gate popState()2367c478bd9Sstevel@tonic-gate public void popState() throws Exception { 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate endSession(sh); 2397c478bd9Sstevel@tonic-gate sh = (byte[])stateStack.pop(); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate //TSOL -- stub for base Solaris; should be called even if auditOn is 2427c478bd9Sstevel@tonic-gate //false. setLabel(String label)2437c478bd9Sstevel@tonic-gate public void setLabel(String label) throws Exception { 2447c478bd9Sstevel@tonic-gate // if (ValidSession) 2457c478bd9Sstevel@tonic-gate // setSL(sh, label); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate //end TSOL 2487c478bd9Sstevel@tonic-gate } 249