6 #ifndef _Included_org_cacert_natives_SetUID
7 #define _Included_org_cacert_natives_SetUID
12 static jobject getStatus(JNIEnv *env, int successCode, const char * message) {
13 jstring message_str = (*env)->NewStringUTF(env, message);
14 jboolean success = successCode;
15 jclass cls = (*env)->FindClass(env, "Lorg/cacert/gigi/natives/SetUID$Status;");
16 jmethodID constructor = (*env)->GetMethodID(env, cls, "<init>", "(ZLjava/lang/String;)V");
17 return (*env)->NewObject(env, cls, constructor, success, message_str);
20 JNIEXPORT jobject JNICALL Java_org_cacert_gigi_natives_SetUID_setUid
21 (JNIEnv *env, jobject obj, jint uid_, jint gid_) {
23 /* We don't need the reference for the object/class we are working on */
25 /* Fix uid and gid types */
26 uid_t uid = (uid_t)uid_;
27 if ((jint)uid != uid_) {
28 return getStatus(env, 0, "UID does not fit in uid_t type.");
30 gid_t gid = (gid_t)gid_;
31 if ((jint)gid != gid_) {
32 return getStatus(env, 0, "GID does not fit in gid_t type.");
35 unsigned char work = 0;
37 if(getgid() != gid || getegid() != gid) {
39 return getStatus(env, 0, "Error while setting GID.");
44 if(getuid() != uid || geteuid() != uid) {
46 return getStatus(env, 0, "Error while setting UID.");
53 case 0: status = "UID and GID already set."; break;
54 case 1: status = "Successfully set GID (UID already set)."; break;
55 case 2: status = "Successfully set UID (GID already set)."; break;
56 case 3: status = "Successfully set UID and GID."; break;
57 default: return getStatus(env, 0, "Unexpected internal state.");
59 return getStatus(env, 1, status);