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 581490fd2Sgww * Common Development and Distribution License (the "License"). 681490fd2Sgww * 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 /* 227c478bd9Sstevel@tonic-gate * adt_jni.c 237c478bd9Sstevel@tonic-gate * 247c478bd9Sstevel@tonic-gate * JNI wrapper for adt interface within libbsm 257c478bd9Sstevel@tonic-gate * 26*33267025Sgww * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 277c478bd9Sstevel@tonic-gate * Use is subject to license terms. 287c478bd9Sstevel@tonic-gate * 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 337c478bd9Sstevel@tonic-gate #include "adt_jni.h" 347c478bd9Sstevel@tonic-gate #include <jni.h> 357c478bd9Sstevel@tonic-gate #include "../com/sun/audit/AuditSession.h" /* javah output */ 367c478bd9Sstevel@tonic-gate #include <assert.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <string.h> 397c478bd9Sstevel@tonic-gate #include <netdb.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * local_throw -- throw an exception. 437c478bd9Sstevel@tonic-gate * "why" string must be i18n'd before calling here. 447c478bd9Sstevel@tonic-gate * 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate void 487c478bd9Sstevel@tonic-gate local_throw(JNIEnv *env, const char *exception, const char *why) { 497c478bd9Sstevel@tonic-gate jobject jexception; 507c478bd9Sstevel@tonic-gate jclass exceptionclass; 517c478bd9Sstevel@tonic-gate jmethodID jexceptionnew; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate jbyteArray jbarray; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate jstring jmsg; 567c478bd9Sstevel@tonic-gate jclass strclass; 577c478bd9Sstevel@tonic-gate jmethodID jstrnew; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /* Get a String class and "new" method */ 607c478bd9Sstevel@tonic-gate strclass = (*env)->FindClass(env, "java/lang/String"); 617c478bd9Sstevel@tonic-gate jstrnew = (*env)->GetMethodID(env, strclass, "<init>", "([B)V"); 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* Create a Byte Array from message "why" */ 647c478bd9Sstevel@tonic-gate jbarray = (*env)->NewByteArray(env, (jsize)(strlen(why))); 657c478bd9Sstevel@tonic-gate (*env)->SetByteArrayRegion(env, jbarray, (jsize)0, 667c478bd9Sstevel@tonic-gate (jsize)(strlen(why)), (jbyte*) why); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate /* Create string from byte array */ 697c478bd9Sstevel@tonic-gate jmsg = (*env)->NewObject(env, strclass, jstrnew, jbarray); 707c478bd9Sstevel@tonic-gate exceptionclass = (*env)->FindClass(env, exception); 717c478bd9Sstevel@tonic-gate jexceptionnew = (*env)->GetMethodID(env, exceptionclass, 727c478bd9Sstevel@tonic-gate "<init>", "(Ljava/lang/String;)V"); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate jexception = (*env)->NewObject(env, exceptionclass, jexceptionnew, 757c478bd9Sstevel@tonic-gate jmsg); 767c478bd9Sstevel@tonic-gate (*env)->Throw(env, jexception); 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate /* 807c478bd9Sstevel@tonic-gate * i18n the strerror return. Input is errno. 817c478bd9Sstevel@tonic-gate * 827c478bd9Sstevel@tonic-gate */ 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate static char * 857c478bd9Sstevel@tonic-gate errno_to_i18n(int error_code) { 867c478bd9Sstevel@tonic-gate char *locale; 877c478bd9Sstevel@tonic-gate char *local_text; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate locale = I18N_SETUP; 907c478bd9Sstevel@tonic-gate local_text = strerror(error_code); 917c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 927c478bd9Sstevel@tonic-gate return (local_text); 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * j2c_pointer 977c478bd9Sstevel@tonic-gate * 987c478bd9Sstevel@tonic-gate * convert java byte array into a C pointer 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate int 1017c478bd9Sstevel@tonic-gate j2c_pointer(JNIEnv *env, jbyteArray jpointer, caddr_t *cpointer) { 1027c478bd9Sstevel@tonic-gate union { 1037c478bd9Sstevel@tonic-gate caddr_t ptr; 1047c478bd9Sstevel@tonic-gate jbyte buf[sizeof (uint64_t)]; 1057c478bd9Sstevel@tonic-gate } u; 1067c478bd9Sstevel@tonic-gate size_t jpointer_length; 1077c478bd9Sstevel@tonic-gate char *locale; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate (void) memset(u.buf, 0, sizeof (uint64_t)); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate assert(jpointer != NULL); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate jpointer_length = (*env)->GetArrayLength(env, jpointer); 1147c478bd9Sstevel@tonic-gate if (jpointer_length != sizeof (uint64_t)) { 1157c478bd9Sstevel@tonic-gate locale = I18N_SETUP; 1167c478bd9Sstevel@tonic-gate local_throw(env, "java/lang/Error", 1177c478bd9Sstevel@tonic-gate gettext("Bad session handle")); 1187c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 1197c478bd9Sstevel@tonic-gate return (-1); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate (*env)->GetByteArrayRegion(env, jpointer, 0, jpointer_length, 1227c478bd9Sstevel@tonic-gate &(u.buf[0])); 1237c478bd9Sstevel@tonic-gate *cpointer = (caddr_t)u.ptr; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate return (0); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate /* 1297c478bd9Sstevel@tonic-gate * c2j_pointer 1307c478bd9Sstevel@tonic-gate * 1317c478bd9Sstevel@tonic-gate * convert a C pointer into a java byte array 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate void 1347c478bd9Sstevel@tonic-gate c2j_pointer(JNIEnv *env, caddr_t cpointer, jbyteArray *jpointer) { 1357c478bd9Sstevel@tonic-gate union { 1367c478bd9Sstevel@tonic-gate caddr_t ptr; 1377c478bd9Sstevel@tonic-gate jbyte buf[sizeof (uint64_t)]; 1387c478bd9Sstevel@tonic-gate } u; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate (void) memset(u.buf, 0, sizeof (uint64_t)); 1417c478bd9Sstevel@tonic-gate u.ptr = cpointer; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate *jpointer = (*env)->NewByteArray(env, sizeof (uint64_t)); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate (*env)->SetByteArrayRegion(env, *jpointer, 0, sizeof (uint64_t), 1467c478bd9Sstevel@tonic-gate &(u.buf[0])); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate /* 1507c478bd9Sstevel@tonic-gate * adt_start_session wrapper 1517c478bd9Sstevel@tonic-gate * 1527c478bd9Sstevel@tonic-gate */ 1537c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1547c478bd9Sstevel@tonic-gate JNIEXPORT jbyteArray JNICALL 1557c478bd9Sstevel@tonic-gate Java_com_sun_audit_AuditSession_startSession(JNIEnv *env, jobject cls, 1567c478bd9Sstevel@tonic-gate jbyteArray jimport, jlong flags) { 1577c478bd9Sstevel@tonic-gate jbyteArray jstate; 1587c478bd9Sstevel@tonic-gate adt_session_data_t *state; 1597c478bd9Sstevel@tonic-gate jbyte *import; 1607c478bd9Sstevel@tonic-gate size_t import_size; 1617c478bd9Sstevel@tonic-gate int rc; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (jimport == NULL) { 1647c478bd9Sstevel@tonic-gate import = NULL; 1657c478bd9Sstevel@tonic-gate } else { 1667c478bd9Sstevel@tonic-gate import_size = (*env)->GetArrayLength(env, jimport); 1677c478bd9Sstevel@tonic-gate import = (jbyte *)malloc(import_size * sizeof (jbyte)); 1687c478bd9Sstevel@tonic-gate if (import == NULL) { 1697c478bd9Sstevel@tonic-gate local_throw(env, "java/lang/Error", 1707c478bd9Sstevel@tonic-gate errno_to_i18n(errno)); 1717c478bd9Sstevel@tonic-gate return (NULL); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate (*env)->GetByteArrayRegion(env, jimport, 0, import_size, 1747c478bd9Sstevel@tonic-gate import); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate rc = adt_start_session(&state, (adt_export_data_t *)import, flags); 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate if (import != NULL) 1797c478bd9Sstevel@tonic-gate free(import); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate if (rc) { 1827c478bd9Sstevel@tonic-gate local_throw(env, "java/lang/Error", errno_to_i18n(errno)); 1837c478bd9Sstevel@tonic-gate return (NULL); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate c2j_pointer(env, (caddr_t)state, &jstate); 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate return (jstate); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* 1917c478bd9Sstevel@tonic-gate * adt_end_session wrapper 1927c478bd9Sstevel@tonic-gate */ 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1957c478bd9Sstevel@tonic-gate JNIEXPORT void JNICALL 1967c478bd9Sstevel@tonic-gate Java_com_sun_audit_AuditSession_endSession(JNIEnv *env, jobject cls, 1977c478bd9Sstevel@tonic-gate jbyteArray jstate) { 1987c478bd9Sstevel@tonic-gate adt_session_data_t *state; 1997c478bd9Sstevel@tonic-gate char *locale; 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate if (j2c_pointer(env, jstate, (caddr_t *)&state)) 2027c478bd9Sstevel@tonic-gate return; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate if (state == NULL) 2057c478bd9Sstevel@tonic-gate return; /* invalid session, nothing to free */ 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate /* presently, no errors defined, but what the heck? */ 2087c478bd9Sstevel@tonic-gate if (adt_end_session(state)) { 2097c478bd9Sstevel@tonic-gate locale = I18N_SETUP; 2107c478bd9Sstevel@tonic-gate local_throw(env, "java/lang/Error", 2117c478bd9Sstevel@tonic-gate gettext("Bad session handle")); 2127c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate /* 2177c478bd9Sstevel@tonic-gate * adt_dup_session wrapper 2187c478bd9Sstevel@tonic-gate */ 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2217c478bd9Sstevel@tonic-gate JNIEXPORT jbyteArray JNICALL 2227c478bd9Sstevel@tonic-gate Java_com_sun_audit_AuditSession_dupSession(JNIEnv *env, jobject cls, 2237c478bd9Sstevel@tonic-gate jbyteArray jsource) { 2247c478bd9Sstevel@tonic-gate jbyteArray jdest; 2257c478bd9Sstevel@tonic-gate adt_session_data_t *source, *dest; 2267c478bd9Sstevel@tonic-gate char *locale; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate if (j2c_pointer(env, jsource, (caddr_t *)&source)) 2297c478bd9Sstevel@tonic-gate return (NULL); 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate if (adt_dup_session(source, &dest)) { 2327c478bd9Sstevel@tonic-gate locale = I18N_SETUP; 2337c478bd9Sstevel@tonic-gate local_throw(env, "java/lang/Error", 2347c478bd9Sstevel@tonic-gate gettext("Out of memory")); 2357c478bd9Sstevel@tonic-gate (void) setlocale(LC_MESSAGES, locale); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate c2j_pointer(env, (caddr_t)dest, &jdest); 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate return (jdest); 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate /* 2447c478bd9Sstevel@tonic-gate * adt_get_session_id wrapper 2457c478bd9Sstevel@tonic-gate * 2467c478bd9Sstevel@tonic-gate */ 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2497c478bd9Sstevel@tonic-gate JNIEXPORT jstring JNICALL 2507c478bd9Sstevel@tonic-gate Java_com_sun_audit_AuditSession_getSessionId(JNIEnv *env, jobject cls, 2517c478bd9Sstevel@tonic-gate jbyteArray jstate) { 2527c478bd9Sstevel@tonic-gate adt_session_data_t *state; 2537c478bd9Sstevel@tonic-gate char *session_id; 2547c478bd9Sstevel@tonic-gate jstring return_val; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (j2c_pointer(env, jstate, (caddr_t *)&state)) 2577c478bd9Sstevel@tonic-gate return (NULL); 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate if (adt_get_session_id(state, &session_id)) { 2607c478bd9Sstevel@tonic-gate return_val = (*env)->NewStringUTF(env, session_id); 2617c478bd9Sstevel@tonic-gate free(session_id); 2627c478bd9Sstevel@tonic-gate return (return_val); 2637c478bd9Sstevel@tonic-gate } else 2647c478bd9Sstevel@tonic-gate return (NULL); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate /* 2687c478bd9Sstevel@tonic-gate * adt_get_session_id wrapper 2697c478bd9Sstevel@tonic-gate */ 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2727c478bd9Sstevel@tonic-gate JNIEXPORT jbyteArray JNICALL 2737c478bd9Sstevel@tonic-gate Java_com_sun_audit_AuditSession_exportSessionData 2747c478bd9Sstevel@tonic-gate (JNIEnv *env, jobject cls, jbyteArray jstate) { 2757c478bd9Sstevel@tonic-gate adt_session_data_t *state; 2767c478bd9Sstevel@tonic-gate size_t length; 2777c478bd9Sstevel@tonic-gate jbyte *buffer; 2787c478bd9Sstevel@tonic-gate jbyteArray jbuf; 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate if (j2c_pointer(env, jstate, (caddr_t *)&state)) 2817c478bd9Sstevel@tonic-gate return (NULL); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate length = adt_export_session_data(state, (adt_export_data_t **)&buffer); 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate if ((jbuf = (*env)->NewByteArray(env, length)) == NULL) { 2867c478bd9Sstevel@tonic-gate free(buffer); 2877c478bd9Sstevel@tonic-gate return (NULL); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate (*env)->SetByteArrayRegion(env, jbuf, 0, length, buffer); 2907c478bd9Sstevel@tonic-gate free(buffer); 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate return (jbuf); 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2967c478bd9Sstevel@tonic-gate JNIEXPORT void JNICALL 2977c478bd9Sstevel@tonic-gate Java_com_sun_audit_AuditSession_sessionAttr(JNIEnv *env, jobject cls, 2987c478bd9Sstevel@tonic-gate jbyteArray jstate, 2997c478bd9Sstevel@tonic-gate jint euid, jint egid, jint ruid, jint rgid, 3007c478bd9Sstevel@tonic-gate jstring jhostname, jint context) { 3017c478bd9Sstevel@tonic-gate adt_session_data_t *state; 3027c478bd9Sstevel@tonic-gate const char *hostname; 3037c478bd9Sstevel@tonic-gate adt_termid_t *termid; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate if (j2c_pointer(env, jstate, (caddr_t *)&state)) 3067c478bd9Sstevel@tonic-gate return; /* j2c_pointer threw exception */ 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate if (state == NULL) 3097c478bd9Sstevel@tonic-gate return; /* invalid session */ 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate hostname = (*env)->GetStringUTFChars(env, jhostname, NULL); 3127c478bd9Sstevel@tonic-gate 313*33267025Sgww if (adt_load_hostname(hostname, &termid)) { 3147c478bd9Sstevel@tonic-gate local_throw(env, "java/lang/Error", errno_to_i18n(errno)); 315*33267025Sgww } else if (adt_set_user(state, euid, egid, ruid, rgid, termid, 316*33267025Sgww context)) { 317*33267025Sgww free(termid); 3187c478bd9Sstevel@tonic-gate local_throw(env, "java/lang/Error", errno_to_i18n(errno)); 319*33267025Sgww } 3207c478bd9Sstevel@tonic-gate (*env)->ReleaseStringUTFChars(env, jhostname, hostname); 321*33267025Sgww free(termid); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate /* ARGSUSED */ 3257c478bd9Sstevel@tonic-gate JNIEXPORT jboolean JNICALL 3267c478bd9Sstevel@tonic-gate Java_com_sun_audit_AuditSession_bsmAuditOn(JNIEnv *env, jobject cls) { 3277c478bd9Sstevel@tonic-gate int condition; 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate if (auditon(A_GETCOND, (caddr_t)&condition, sizeof (condition))) 3307c478bd9Sstevel@tonic-gate return (0); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate return (1); 3337c478bd9Sstevel@tonic-gate } 334