HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Using JNI for Safer Java Servers Under UNIX"
Vol. 5, Issue 10, p. 46

	


Listing 1

micah/util/UID.java

package micah.util;

public class UID {

    public static final int SUCCESS = 0;
    public static final int FAILURE = -1;

    public static native int setuid(int uid);
    public static native int seteuid(int uid);
    public static native int setgid(int gid);
    public static native int setegid(int gid);
    
    static {
	System.loadLibrary("uid");
    }
}

Listing 2

micah/util/micah_util_UID.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class micah_util_UID */

#ifndef _Included_micah_util_UID
#define _Included_micah_util_UID
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     micah_util_UID
 * Method:    setuid
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_micah_util_UID_setuid
  (JNIEnv *, jclass, jint);

/*
 * Class:     micah_util_UID
 * Method:    seteuid
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_micah_util_UID_seteuid
  (JNIEnv *, jclass, jint);

/*
 * Class:     micah_util_UID
 * Method:    setgid
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_micah_util_UID_setgid
  (JNIEnv *, jclass, jint);

/*
 * Class:     micah_util_UID
 * Method:    setegid
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_micah_util_UID_setegid
  (JNIEnv *, jclass, jint);

#ifdef __cplusplus
}
#endif
#endif

Listing 3

micah/util/unix_uid.c

#include <jni.h>
#include "micah_util_UID.h"
#include <sys/types.h>
#include <unistd.h>
  
JNIEXPORT jint JNICALL
Java_micah_util_UID_setuid (JNIEnv * jnienv,
jclass j, jint uid)
{
    return((jint)setuid((uid_t)uid));
}

JNIEXPORT jint JNICALL
Java_micah_util_UID_seteuid (JNIEnv * jnienv,
jclass j, jint uid)
{
    return((jint)seteuid((uid_t)uid));
}

JNIEXPORT jint JNICALL
Java_micah_util_UID_setgid (JNIEnv * jnienv,
jclass j, jint gid)
{
    return((jint)setgid((uid_t)gid));
}

JNIEXPORT jint JNICALL
Java_micah_util_UID_setegid (JNIEnv * jnienv,
jclass j, jint gid)
{
    return((jint)setegid((uid_t)gid));
}

Listing 4

micah/util/win_uid.c

#include <jni.h>
#include "micah_util_UID.h"

JNIEXPORT jint JNICALL 
Java_micah_util_UID_setuid (JNIEnv * jn, jclass j,
jint uid)
{
	return(0);
}

JNIEXPORT jint JNICALL 
Java_micah_util_UID_seteuid (JNIEnv * jn, jclass j, 
jint uid)
{
	return(0);
}

JNIEXPORT jint JNICALL 
Java_micah_util_UID_setgid (JNIEnv * jn, jclass j, 
jint gid)
{
	return(0);
}

JNIEXPORT jint JNICALL 
Java_micah_util_UID_setegid (JNIEnv * jn, jclass j, 
jint gid)
{
	return(0);
}

Listing 5

testchUID.java

import java.io.*;
import micah.util.*;

class testchUID {
	public static void main(String[] args) {
		try {
			System.in.read();
		}
		catch (IOException ioe) {}
		int result=UID.setuid(1010);
		if (result == UID.SUCCESS) {
		    System.out.println("Success!");
		}
		else if (result == UID.FAILURE) {
		    System.out.println("Failure!");
		}
		try {
			System.in.read();
		}
		catch (IOException ioe) {}
	}
}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.