Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes ! That's it.</p> <p>I have thought about that yesterday afternoon, and I went on the same solution. I'll post here my code for those who would be curious of that. 1) The NativeRMISecurityManager 2) The C code (you have to generate the .h with javah</p> <p>(nb: I won't traduce it in english as there are loads of french comments)</p> <pre><code>package rmi; import java.rmi.RMISecurityManager; /** * &lt;p&gt; Ce SecurityManager, qui herite de RMISecurityManager, * implemente une verification supplementaire des droits * d'acces aux fichiers. * A la creation du SecurityManager et lors de la creation * de nouveaux threads, on renseigne ThreadLocal du nom du * _user_ du thread. * &lt;p&gt;Ainsi, lors des checkRead() et checkWrite() * notre SecurityManager appelle une methode native (JNI) * qui va verifier directement si le user a les droits * d'acces a la ressource. * &lt;p&gt;&lt;b&gt;Warning : NE PAS OUBLIER DE FAIRE APPEL A * setCurrentUser() DANS CHAQUE THREAD CREE.&lt;/b&gt; * &lt;p&gt; &lt;b&gt;Remarque :&lt;/b&gt; Pour les informations sur la compilation * et l'execution de la lib ecrite en C, cf. le fichier README. * @author a_po */ public class NativeRMISecurityManager extends RMISecurityManager { private boolean unix; protected ThreadLocal user = new ThreadLocal(); /** * Constructeur par defaut. * &lt;p&gt;&lt;b&gt;ATTENTION :&lt;/b&gt; Bien faire appel a la methode setCurrentUser(String) ! * Sinon le SecurityManager se comportera comme un RMISecurityManager classique. * @see public void setCurrentUser(String userName) */ public NativeRMISecurityManager() { super(); String OS = System.getProperty("os.name").toLowerCase(); unix = (OS.compareTo("windows") != 0); /* Si le systeme * n'EST PAS windows, * alors c'est UNIX... * * Pas tres rigoureux, * mais sinon il faut tester * Systeme V, Linux, *BSD, * Sun OS, ... */ /* * User du ThreadLocal : Chaque thread est considere comme ayant des * droits d'acces au systeme potentiellement differents. */ this.user.set(user); if (!unix) { System.out.println("Systeme : "+OS); } } /** * Verification en lecture. * &lt;p&gt; * Dans le cas ou l'on est sur une plateforme POSIX, * on souhaite verifier que le _user_ du Thread a le droit * de lecture sur le fichier. * &lt;p&gt; * De plus, dans le cas ou user est null, cela signifie * OBLIGATOIREMENT que le thread a ete cree "automatiquement" * et que le thread courant n'est pas un thread de "tache a executer". * &lt;p&gt; * En effet, le user est recupere dans le ThreadLocal * et on force l'initialisation de cette variable a l'instanciation * du SecurityManager (en mettant le constructeur par defaut prive) ou * en faisant appel a setCurrentUser(String) * @see void rmi.NativeRMISecurityManager.setCurrentUser(String user) */ public void checkRead(String file) { super.checkRead(file); String str_user = (String)this.user.get(); if (unix &amp;&amp; str_user != null) { if (file == null) { throw new SecurityException("file = NULL !!!"); } int ret = c_checkRead(file, str_user); if (ret != 0) { throw new SecurityException("Erreur d'acces au fichier : " + file); } } } /** * Verification d'acces en ecriture sur un fichier. * @see void rmi.NativeRMISecurityManager.checkRead(String file) */ public void checkWrite(String file) { super.checkWrite(file); String str_user = (String)this.user.get(); if (unix &amp;&amp; str_user != null) { if (file == null) { throw new SecurityException("file = NULL !!!"); } int ret = c_checkWrite(file, str_user); if (ret != 0) { throw new SecurityException("Erreur d'acces au fichier : " + file); } } } /** * Configure le thread courant pour que le user soit pris en compte * dans les verifications d'acces aux fichiers. * @param user */ public void setCurrentUser(String userName) { this.user = new ThreadLocal(); this.user.set(userName); } public String getCurrentUser() { if (user!=null){ return (String)user.get(); } else return null; } /** * Methode native a implementer en C. * @param file * @param user * @return 0 si ok &lt;p&gt; -1 sinon */ public native int c_checkRead(String file, String user); /** * Idem que pour c_checkRead * @param file * @param user * @return * @see int rmi.NativeRMISecurityManager.c_checkRead(String file, String user) */ public native int c_checkWrite(String file, String user); /** * Chargement de la bibliotheque JNI. */ static { System.loadLibrary("rmi_NativeRMISecurityManager"); } } </code></pre> <p>And the C library:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;jni.h&gt; #include &lt;sys/stat.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;pwd.h&gt; #include &lt;stdlib.h&gt; #include &lt;grp.h&gt; #include &lt;string.h&gt; #include "rmi_NativeRMISecurityManager.h" /* Droits en lecture / ecriture / execution */ #define R_RIGHT 4 #define X_RIGHT 1 #define W_RIGHT 2 JNIEXPORT jint JNICALL Java_rmi_NativeRMISecurityManager_c_1checkRead (JNIEnv *env, jobject obj, jstring file, jstring user) { int ret = check_permission(env, obj, file, user); /** * La permission d'acces a un fichier vaut ceci : * 1 pour l'execution * 2 pour l'ecriture * 4 pour la lecture. * Donc : * * Droit en lecture : 4, 5, 6, 7 * * Droit en ecriture : 2, 3, 6, 7 * * Droit en execution : 1, 3, 5, 7. */ if (ret == R_RIGHT || ret == R_RIGHT + W_RIGHT || ret == R_RIGHT + X_RIGHT || ret == R_RIGHT + W_RIGHT + X_RIGHT) { return 0; } else return -1; } JNIEXPORT jint JNICALL Java_rmi_NativeRMISecurityManager_c_1checkWrite (JNIEnv *env, jobject obj, jstring file, jstring user) { int ret = check_permission(env, obj, file, user); /** * La permission d'acces a un fichier vaut ceci : * 1 pour l'execution * 2 pour l'ecriture * 4 pour la lecture. * Donc : * * Droit en lecture : 4, 5, 6, 7 * * Droit en ecriture : 2, 3, 6, 7 * * Droit en execution : 1, 3, 5, 7. */ if (ret == W_RIGHT || ret == W_RIGHT + R_RIGHT || ret == W_RIGHT + X_RIGHT || ret == W_RIGHT + R_RIGHT + X_RIGHT) { return 0; } else return -1; } int check_permission(JNIEnv *env, jobject obj, jstring file, jstring user) { struct stat pstat; const char* pzcfile = (*env)-&gt;GetStringUTFChars(env, file, 0); const char* pzcuser = (*env)-&gt;GetStringUTFChars(env, user, 0); struct passwd* puserInfo; int bisOwner = 0; int bisGroup = 0; struct group* pgroupInfo; int i; int droits = 0; /* recuperer les informations relatives au fichier */ if(lstat(pzcfile, &amp;pstat)&lt;0) { fprintf(stderr,"* Le fichier %s n'exite pas.\n", pzcfile); (*env)-&gt;ReleaseStringUTFChars(env, file, pzcfile); (*env)-&gt;ReleaseStringUTFChars(env, user, pzcuser); return -1; } /* recuperer l'identifiant du user */ puserInfo = getpwnam(pzcuser); if(puserInfo == NULL) { fprintf(stderr,"* L'utilisateur %s n'est pas connu du systeme.\n", pzcuser); (*env)-&gt;ReleaseStringUTFChars(env, file, pzcfile); (*env)-&gt;ReleaseStringUTFChars(env, user, pzcuser); return -2; } /* regarder si le user est proprietaire du fichier */ if(puserInfo-&gt;pw_uid == pstat.st_uid) { bisOwner = 1; } /* si le user n'est pas proprietaire, verifier s'il est membre du groupe */ if(!bisOwner) { /* recuperer les informations relatives au groupe */ pgroupInfo = getgrgid(pstat.st_gid); /* parcourir la liste des membres du groupe a la recherche du user */ for(i=0;;i++) { if(pgroupInfo-&gt;gr_mem[i] == NULL) { break; } if(strcmp(pgroupInfo-&gt;gr_mem[i],pzcuser) == 0) { bisGroup = 1; break; } } } /* recuperer les droits correspondants au user */ if(bisOwner) { droits = (pstat.st_mode &amp; S_IRWXU) &gt;&gt; 6; } else if(bisGroup) { droits = (pstat.st_mode &amp; S_IRWXG) &gt;&gt; 3; } else { droits = pstat.st_mode &amp; S_IRWXO; } /* liberer les espaces memoire alloues */ (*env)-&gt;ReleaseStringUTFChars(env, file, pzcfile); (*env)-&gt;ReleaseStringUTFChars(env, user, pzcuser); return droits; } </code></pre> <p>Thank you very much Greg Case. This comfort me because we have found the same solution. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload