]> WPIA git - gigi.git/blob - natives/org_cacert_gigi_natives_SetUID.c
2e2ef79fe8c17353ba98c374b6a692f9a66f6b4c
[gigi.git] / natives / org_cacert_gigi_natives_SetUID.c
1 #include <jni.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4
5 #ifndef _Included_org_cacert_natives_SetUID
6 #define _Included_org_cacert_natives_SetUID
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 static jobject getStatus(JNIEnv *env, int successCode, const char * message) {
12     jstring message_str = (*env)->NewStringUTF(env, message);
13     jboolean success = successCode;
14     jclass cls = (*env)->FindClass(env, "Lorg/cacert/gigi/natives/SetUID$Status;");
15     jmethodID constructor = (*env)->GetMethodID(env, cls, "<init>", "(ZLjava/lang/String;)V");
16     return (*env)->NewObject(env, cls, constructor, success, message_str);
17 }
18
19 JNIEXPORT jobject JNICALL Java_org_cacert_gigi_natives_SetUID_setUid
20         (JNIEnv *env, jobject obj, jint uid, jint gid) {
21
22     /* We don't need the reference for the object/class we are working on */
23     (void)obj;
24
25     if(setgid((int)gid)) {
26         return (jobject)getStatus(env, 0, "Error while setting GID.");
27     }
28
29     if(setuid((int)uid)) {
30         return (jobject)getStatus(env, 0, "Error while setting UID.");
31     }
32
33     return (jobject)getStatus(env, 1, "Successfully set uid/gid.");
34 }
35
36 #ifdef __cplusplus
37 }
38 #endif
39 #endif