4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * JNI wrapper for adt interface within libbsm
26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include "../com/sun/audit/AuditSession.h" /* javah output */
42 * local_throw -- throw an exception.
43 * "why" string must be i18n'd before calling here.
48 local_throw(JNIEnv
*env
, const char *exception
, const char *why
) {
50 jclass exceptionclass
;
51 jmethodID jexceptionnew
;
59 /* Get a String class and "new" method */
60 strclass
= (*env
)->FindClass(env
, "java/lang/String");
61 jstrnew
= (*env
)->GetMethodID(env
, strclass
, "<init>", "([B)V");
63 /* Create a Byte Array from message "why" */
64 jbarray
= (*env
)->NewByteArray(env
, (jsize
)(strlen(why
)));
65 (*env
)->SetByteArrayRegion(env
, jbarray
, (jsize
)0,
66 (jsize
)(strlen(why
)), (jbyte
*) why
);
68 /* Create string from byte array */
69 jmsg
= (*env
)->NewObject(env
, strclass
, jstrnew
, jbarray
);
70 exceptionclass
= (*env
)->FindClass(env
, exception
);
71 jexceptionnew
= (*env
)->GetMethodID(env
, exceptionclass
,
72 "<init>", "(Ljava/lang/String;)V");
74 jexception
= (*env
)->NewObject(env
, exceptionclass
, jexceptionnew
,
76 (*env
)->Throw(env
, jexception
);
80 * i18n the strerror return. Input is errno.
85 errno_to_i18n(int error_code
) {
90 local_text
= strerror(error_code
);
91 (void) setlocale(LC_MESSAGES
, locale
);
98 * convert java byte array into a C pointer
101 j2c_pointer(JNIEnv
*env
, jbyteArray jpointer
, caddr_t
*cpointer
) {
104 jbyte buf
[sizeof (uint64_t)];
106 size_t jpointer_length
;
109 (void) memset(u
.buf
, 0, sizeof (uint64_t));
111 assert(jpointer
!= NULL
);
113 jpointer_length
= (*env
)->GetArrayLength(env
, jpointer
);
114 if (jpointer_length
!= sizeof (uint64_t)) {
116 local_throw(env
, "java/lang/Error",
117 gettext("Bad session handle"));
118 (void) setlocale(LC_MESSAGES
, locale
);
121 (*env
)->GetByteArrayRegion(env
, jpointer
, 0, jpointer_length
,
123 *cpointer
= (caddr_t
)u
.ptr
;
131 * convert a C pointer into a java byte array
134 c2j_pointer(JNIEnv
*env
, caddr_t cpointer
, jbyteArray
*jpointer
) {
137 jbyte buf
[sizeof (uint64_t)];
140 (void) memset(u
.buf
, 0, sizeof (uint64_t));
143 *jpointer
= (*env
)->NewByteArray(env
, sizeof (uint64_t));
145 (*env
)->SetByteArrayRegion(env
, *jpointer
, 0, sizeof (uint64_t),
150 * adt_start_session wrapper
154 JNIEXPORT jbyteArray JNICALL
155 Java_com_sun_audit_AuditSession_startSession(JNIEnv
*env
, jobject cls
,
156 jbyteArray jimport
, jlong flags
) {
158 adt_session_data_t
*state
;
163 if (jimport
== NULL
) {
166 import_size
= (*env
)->GetArrayLength(env
, jimport
);
167 import
= (jbyte
*)malloc(import_size
* sizeof (jbyte
));
168 if (import
== NULL
) {
169 local_throw(env
, "java/lang/Error",
170 errno_to_i18n(errno
));
173 (*env
)->GetByteArrayRegion(env
, jimport
, 0, import_size
,
176 rc
= adt_start_session(&state
, (adt_export_data_t
*)import
, flags
);
182 local_throw(env
, "java/lang/Error", errno_to_i18n(errno
));
185 c2j_pointer(env
, (caddr_t
)state
, &jstate
);
191 * adt_end_session wrapper
195 JNIEXPORT
void JNICALL
196 Java_com_sun_audit_AuditSession_endSession(JNIEnv
*env
, jobject cls
,
198 adt_session_data_t
*state
;
201 if (j2c_pointer(env
, jstate
, (caddr_t
*)&state
))
205 return; /* invalid session, nothing to free */
207 /* presently, no errors defined, but what the heck? */
208 if (adt_end_session(state
)) {
210 local_throw(env
, "java/lang/Error",
211 gettext("Bad session handle"));
212 (void) setlocale(LC_MESSAGES
, locale
);
217 * adt_dup_session wrapper
221 JNIEXPORT jbyteArray JNICALL
222 Java_com_sun_audit_AuditSession_dupSession(JNIEnv
*env
, jobject cls
,
223 jbyteArray jsource
) {
225 adt_session_data_t
*source
, *dest
;
228 if (j2c_pointer(env
, jsource
, (caddr_t
*)&source
))
231 if (adt_dup_session(source
, &dest
)) {
233 local_throw(env
, "java/lang/Error",
234 gettext("Out of memory"));
235 (void) setlocale(LC_MESSAGES
, locale
);
238 c2j_pointer(env
, (caddr_t
)dest
, &jdest
);
244 * adt_get_session_id wrapper
249 JNIEXPORT jstring JNICALL
250 Java_com_sun_audit_AuditSession_getSessionId(JNIEnv
*env
, jobject cls
,
252 adt_session_data_t
*state
;
256 if (j2c_pointer(env
, jstate
, (caddr_t
*)&state
))
259 if (adt_get_session_id(state
, &session_id
)) {
260 return_val
= (*env
)->NewStringUTF(env
, session_id
);
268 * adt_get_session_id wrapper
272 JNIEXPORT jbyteArray JNICALL
273 Java_com_sun_audit_AuditSession_exportSessionData
274 (JNIEnv
*env
, jobject cls
, jbyteArray jstate
) {
275 adt_session_data_t
*state
;
280 if (j2c_pointer(env
, jstate
, (caddr_t
*)&state
))
283 length
= adt_export_session_data(state
, (adt_export_data_t
**)&buffer
);
285 if ((jbuf
= (*env
)->NewByteArray(env
, length
)) == NULL
) {
289 (*env
)->SetByteArrayRegion(env
, jbuf
, 0, length
, buffer
);
296 JNIEXPORT
void JNICALL
297 Java_com_sun_audit_AuditSession_sessionAttr(JNIEnv
*env
, jobject cls
,
299 jint euid
, jint egid
, jint ruid
, jint rgid
,
300 jstring jhostname
, jint context
) {
301 adt_session_data_t
*state
;
302 const char *hostname
;
303 adt_termid_t
*termid
;
305 if (j2c_pointer(env
, jstate
, (caddr_t
*)&state
))
306 return; /* j2c_pointer threw exception */
309 return; /* invalid session */
311 hostname
= (*env
)->GetStringUTFChars(env
, jhostname
, NULL
);
313 if (adt_load_hostname(hostname
, &termid
)) {
314 local_throw(env
, "java/lang/Error", errno_to_i18n(errno
));
315 } else if (adt_set_user(state
, euid
, egid
, ruid
, rgid
, termid
,
318 local_throw(env
, "java/lang/Error", errno_to_i18n(errno
));
320 (*env
)->ReleaseStringUTFChars(env
, jhostname
, hostname
);
325 JNIEXPORT jboolean JNICALL
326 Java_com_sun_audit_AuditSession_bsmAuditOn(JNIEnv
*env
, jobject cls
) {
329 if (auditon(A_GETCOND
, (caddr_t
)&condition
, sizeof (condition
)))