From ae23946af0e0e2ae09b56005f7030ee78205c48f Mon Sep 17 00:00:00 2001 From: Landon Fuller Date: Tue, 19 Dec 2006 20:38:18 +0000 Subject: [PATCH] seteuid()/setegid() support git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@103 ed5b42cb-e716-0410-a449-f6a68f950b19 --- .../com_threerings_util_unsafe_Unsafe.c | 20 +++++++++ .../util/unsafe/Mac OS X/libunsafe.jnilib | Bin 13712 -> 13932 bytes .../com/threerings/util/unsafe/Unsafe.java | 42 ++++++++++++++++-- .../com_threerings_util_unsafe_Unsafe.c | 28 ++++++++++++ .../com_threerings_util_unsafe_Unsafe.h | 34 +++++++++++++- 5 files changed, 119 insertions(+), 5 deletions(-) diff --git a/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_Unsafe.c b/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_Unsafe.c index 04ac0526..abe1fd3e 100644 --- a/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_Unsafe.c +++ b/src/java/com/threerings/util/unsafe/FreeBSD/com_threerings_util_unsafe_Unsafe.c @@ -77,6 +77,26 @@ Java_com_threerings_util_unsafe_Unsafe_nativeSetgid ( return JNI_TRUE; } +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSeteuid ( + JNIEnv* env, jclass clzz, jint uid) +{ + if (seteuid((uid_t)uid) != 0) { + fprintf(stderr, "seteuid(%d) failed: %s\n", uid, strerror(errno)); + return JNI_FALSE; + } + return JNI_TRUE; +} + +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSetegid ( + JNIEnv* env, jclass clazz, jint gid) +{ + if (setegid((gid_t)gid) != 0) { + fprintf(stderr, "setegid(%d) failed: %s\n", gid, strerror(errno)); + return JNI_FALSE; + } + return JNI_TRUE; +} + JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_init (JNIEnv* env, jclass clazz) { diff --git a/src/java/com/threerings/util/unsafe/Mac OS X/libunsafe.jnilib b/src/java/com/threerings/util/unsafe/Mac OS X/libunsafe.jnilib index 18b5c0750c393b36413edf99610626fd42b42cf1..4e7796d86e169d12dd0bf319d3565039d3e541b7 100755 GIT binary patch delta 1532 zcmZ{kT}V@57{{OY>|{3Qw$!HOniFSa(#NKSnSG+AVUd(2MMkhyeh`IvQBhlsf-x4^ z8w7RXmC;?w2r49%?BmKxN+=4tC>Yp9gwRDF>;Igcxzj}lo^yWh^Stl>dCv2m_gw4x z+I_9ptmY6^t3+x}|ApEGH8#?&n*Q+(+F%01n)p01D#S|pRN1_Ar5khMkc#%`i#QQmHmne4hJE&?#vUILrqdXn92?IAE+@J`iXn;Q_2?&&8X66sZ^#4>0)Jl5Ngs9+6EwFbnhhw{l|l(ldn^{jr$pf2)drL}C=Be?WJ<9WK|LqJOM1&AeQ z*pn$VqqxGL*q^51O8ffd5@Xc zF9&Qz@dvgpyR^H_@iO-tTM*waB-D%Orga_)!T2UK&XNk|4~01!{1)sN3aL0l-wj-h z&*d1u1TH7IE`?MH4(Yre?B_&SivvL}a1l7jXNw0MsKq2Jcp12nc|MT^i9f*SQbY6$ zhjooU9}l_tOL6Wr4CPg0fmekeaFS8N08P*A#G?NSI zOJGhI_iHJv7`7O;1m=bn!5nhXyE<N4>cC1NX1 delta 1214 zcmY+DT}V@57{{M?=h$q{ZR-5CrENrEkBPS z4Wx@Q${>QoScH(2l41x&*6^mFE)oZNDRmRA|Fa$4>4oQ<-}7<)&-0#h-i=Em7e@jX zrS=95jZb3Ai1r58Fohet6Q8tswI`h(PNv6Avd>yLIKWk{j*#k!kN-_1hGwtT z#{Cct6Ndtz&|E|t4~=>tIa#NTX^CnY1JYmhh_t< zaVKjeRb3!fZOSE&QP#?Xw6OXkgZf&IEX{VeJwr$z^l^?ZIRQvXPJ&o+nIq{z(~2hz zidUuI<`H#r*yi^4B_$$3-dRNc;{A1ESJ=A~nZ?$hh|v0(aO)=C<%yz*#zNt)%|*-! z;(7TUB$n~!_D8h;*!}Bz(Ih921&tZv4-Nd(z*7dEHt?*Fx2ty_Xs{6ZVIM720`*cDpVDF&Z@mas z2ICWGm#fu>OiM!U^?HlHKx*q)^ATp|W3R|2wKV%TvRf-x-t*Q7yZqoStiWCr$P1Q( eynwtwD}mMmtwmWhzs4eD`0$Hpd#wxKlKugqbLOW2 diff --git a/src/java/com/threerings/util/unsafe/Unsafe.java b/src/java/com/threerings/util/unsafe/Unsafe.java index c275b402..1a7e030f 100644 --- a/src/java/com/threerings/util/unsafe/Unsafe.java +++ b/src/java/com/threerings/util/unsafe/Unsafe.java @@ -73,7 +73,7 @@ public class Unsafe } /** - * Sets the processes uid to the specified value. + * Sets the process' uid to the specified value. * * @return true if the uid was changed, false if we were unable to do so. */ @@ -86,9 +86,9 @@ public class Unsafe } /** - * Sets the processes uid to the specified value. + * Sets the process' gid to the specified value. * - * @return true if the uid was changed, false if we were unable to do so. + * @return true if the gid was changed, false if we were unable to do so. */ public static boolean setgid (int gid) { @@ -98,6 +98,32 @@ public class Unsafe return false; } + /** + * Sets the process' effective uid to the specified value. + * + * @return true if the euid was changed, false if we were unable to do so. + */ + public static boolean seteuid (int uid) + { + if (_loaded && !RunAnywhere.isWindows()) { + return nativeSeteuid(uid); + } + return false; + } + + /** + * Sets the process' effective gid to the specified value. + * + * @return true if the egid was changed, false if we were unable to do so. + */ + public static boolean setegid (int gid) + { + if (_loaded && !RunAnywhere.isWindows()) { + return nativeSetegid(gid); + } + return false; + } + /** * Reenable garbage collection after a call to {@link #disableGC}. */ @@ -123,6 +149,16 @@ public class Unsafe */ protected static native boolean nativeSetgid (int gid); + /** + * Calls through to the native OS system call to change our euid. + */ + protected static native boolean nativeSeteuid (int uid); + + /** + * Calls through to the native OS system call to change our egid. + */ + protected static native boolean nativeSetegid (int gid); + /** * Called to initialize our library. */ diff --git a/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_Unsafe.c b/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_Unsafe.c index dcb2e950..82622f94 100644 --- a/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_Unsafe.c +++ b/src/java/com/threerings/util/unsafe/Windows/com_threerings_util_unsafe_Unsafe.c @@ -32,6 +32,34 @@ Java_com_threerings_util_unsafe_Unsafe_nativeSleep ( /* not supported */ } +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSetuid + (JNIEnv *, jclass, jint) +{ + /* not supported */ + return JNI_FALSE; +} + +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSetgid + (JNIEnv *, jclass, jint) +{ + /* not supported */ + return JNI_FALSE; +} + +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSeteuid ( + JNIEnv* env, jclass clzz, jint uid) +{ + /* not supported */ + return JNI_FALSE; +} + +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSetegid ( + JNIEnv* env, jclass clazz, jint gid) +{ + /* not supported */ + return JNI_FALSE; +} + JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_init (JNIEnv* env, jclass clazz) { diff --git a/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_Unsafe.h b/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_Unsafe.h index f439fed8..bae9f5aa 100644 --- a/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_Unsafe.h +++ b/src/java/com/threerings/util/unsafe/com_threerings_util_unsafe_Unsafe.h @@ -7,8 +7,6 @@ #ifdef __cplusplus extern "C" { #endif -/* Inaccessible static: _gcEnabled */ -/* Inaccessible static: _loaded */ /* * Class: com_threerings_util_unsafe_Unsafe * Method: enableGC @@ -33,6 +31,38 @@ JNIEXPORT void JNICALL Java_com_threerings_util_unsafe_Unsafe_disableGC JNIEXPORT void JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSleep (JNIEnv *, jclass, jint); +/* + * Class: com_threerings_util_unsafe_Unsafe + * Method: nativeSetuid + * Signature: (I)Z + */ +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSetuid + (JNIEnv *, jclass, jint); + +/* + * Class: com_threerings_util_unsafe_Unsafe + * Method: nativeSetgid + * Signature: (I)Z + */ +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSetgid + (JNIEnv *, jclass, jint); + +/* + * Class: com_threerings_util_unsafe_Unsafe + * Method: nativeSeteuid + * Signature: (I)Z + */ +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSeteuid + (JNIEnv *, jclass, jint); + +/* + * Class: com_threerings_util_unsafe_Unsafe + * Method: nativeSetegid + * Signature: (I)Z + */ +JNIEXPORT jboolean JNICALL Java_com_threerings_util_unsafe_Unsafe_nativeSetegid + (JNIEnv *, jclass, jint); + /* * Class: com_threerings_util_unsafe_Unsafe * Method: init