From: Janis Streib Date: Sun, 6 Jul 2014 12:06:57 +0000 (+0200) Subject: Merge branch 'BenBE_work' X-Git-Url: https://code.wpia.club/?p=gigi.git;a=commitdiff_plain;h=b2cd4d4f25d6e40e6675a455c7ed11657e1f7514;hp=40f85c683288d3b19d7ff057854df9cd7ff469c3 Merge branch 'BenBE_work' Conflicts: natives/org_cacert_gigi_natives_SetUID.c --- diff --git a/natives/Makefile b/natives/Makefile index b58e4006..faf073cd 100644 --- a/natives/Makefile +++ b/natives/Makefile @@ -1,11 +1,16 @@ SYSTEM= $(shell uname | awk '{print tolower($$0)}') +JAVAH?=javah +CC?=gcc +CFLAGS?=-O3 -g -flto -Wall -Werror -Wextra -pedantic -fsanitize=address -fPIC +CFLAGS+=-I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/$(SYSTEM) +LDFLAGS?=-shared all: libsetuid.so -libsetuid.so: - javah -classpath ../bin/ -jni org.cacert.gigi.natives.SetUID - gcc -fPIC -o libsetuid.so -shared -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/$(SYSTEM) org_cacert_gigi_natives_SetUID.c +libsetuid.so: org_cacert_gigi_natives_SetUID.c + $(JAVAH) -classpath ../bin/ -jni org.cacert.gigi.natives.SetUID && \ + $(CC) $(CFLAGS) $(LDFLAGS) -o libsetuid.so org_cacert_gigi_natives_SetUID.c clean: rm -f *.so diff --git a/natives/org_cacert_gigi_natives_SetUID.c b/natives/org_cacert_gigi_natives_SetUID.c index 96bf4c21..6c94d619 100644 --- a/natives/org_cacert_gigi_natives_SetUID.c +++ b/natives/org_cacert_gigi_natives_SetUID.c @@ -1,36 +1,40 @@ -#include +#include #include -#include - -#ifndef _Included_org_cacert_natives_SetUID -#define _Included_org_cacert_natives_SetUID -#ifdef __cplusplus -extern "C" { -#endif - -static jobject getStatus(JNIEnv *env, int successCode, const char * message) { - - jstring message_str = (*env)->NewStringUTF(env, message); - jboolean success = successCode; - jclass cls = (*env)->FindClass(env, "Lorg/cacert/gigi/natives/SetUID$Status;"); - jmethodID constructor = (*env)->GetMethodID(env, cls, "", "(ZLjava/lang/String;)V"); - return (*env)->NewObject(env, cls, constructor, success, message_str); -} - -JNIEXPORT jobject JNICALL Java_org_cacert_gigi_natives_SetUID_setUid - (JNIEnv *env, jobject obj, jint uid, jint gid) { - if(setgid((int)gid)) { - return (jobject)getStatus(env, 0, "Error while setting GID."); - } - - if(setuid((int)uid)) { - return (jobject)getStatus(env, 0, "Error while setting UID."); - } - - return (jobject)getStatus(env, 1, "Successfully set uid/gid."); -} - -#ifdef __cplusplus -} -#endif -#endif + +#include + +#ifndef _Included_org_cacert_natives_SetUID +#define _Included_org_cacert_natives_SetUID +#ifdef __cplusplus +extern "C" { +#endif + +static jobject getStatus(JNIEnv *env, int successCode, const char * message) { + jstring message_str = (*env)->NewStringUTF(env, message); + jboolean success = successCode; + jclass cls = (*env)->FindClass(env, "Lorg/cacert/gigi/natives/SetUID$Status;"); + jmethodID constructor = (*env)->GetMethodID(env, cls, "", "(ZLjava/lang/String;)V"); + return (*env)->NewObject(env, cls, constructor, success, message_str); +} + +JNIEXPORT jobject JNICALL Java_org_cacert_gigi_natives_SetUID_setUid + (JNIEnv *env, jobject obj, jint uid, jint gid) { + + /* We don't need the reference for the object/class we are working on */ + (void)obj; + + if(setgid((int)gid)) { + return (jobject)getStatus(env, 0, "Error while setting GID."); + } + + if(setuid((int)uid)) { + return (jobject)getStatus(env, 0, "Error while setting UID."); + } + + return (jobject)getStatus(env, 1, "Successfully set uid/gid."); +} + +#ifdef __cplusplus +} +#endif +#endif