JAAS authorization

I'm trying to use JAAS to authorize (no authentication) different users to read/write a file but it even denying write permission to a specific subject won't stop the client from writing to it.

This are the security policy:

grant codebase"file:F:/libs/jaas.jar"{

permission java.security.AllPermission;

};

grant codebase"file:F:/classes/"{

permission javax.security.auth.AuthPermission"modifyPrincipals";

permission javax.security.auth.AuthPermission"doAs";

permission java.io.FilePermission"F:/source/test/disadapter/temp.txt","read, write";

};

and the jaas permission file

grant Principal test.descriptor.User"employees"{

permission java.io.FilePermission"F:/source/test/disadapter/temp.txt","read";

};

grant Principal test.descriptor.User"managers"{

permission java.io.FilePermission"F:/source/test/disadapter/temp.txt","read, write";

};

while the code is:

import java.security.Principal;

import java.security.PrivilegedAction;

import javax.security.auth.Subject;

import java.util.*;

publicclass AccessManager{

publicstaticvoid main (String[] args)throws Exception{

PrivilegedAction read =new PrivilegedAction(){

public Object run(){

//read the file

}

};

PrivilegedAction write =new PrivilegedAction(){

public Object run(){

//write the file

}

};

Subject subject =new Subject();

subject.getPrincipals().add(new User("employees"));

Subject.doAs(subject,write);

}

}

class Userimplements Principal{

private String name =null;

public User(String name){

this.name=name;

}

public String getName(){

return name;

}

publicboolean equals(Object another){

String otherName = ((Principal) another).getName();

return name.equals(otherName);

}

public String toString(){

return name;

}

}

Running the program with the following JVM parameters-Djava.security.manager -Djava.security.auth.policy=jaas.policy -Djava.security.policy=security.policy

regardless what the user is, it will always have access to that file. Any ideas?

Thanks

[4573 byte] By [aldib] at [2007-9-26 6:33:47]
# 1
I have the same problem.Does anybody know any solution?
LPavel at 2007-7-1 15:45:48 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 2

I've got similar situation working and when I'm comparing your code and mine I find at least following differences:

1. Comment read write permission from codebase:

grant codebase "file:F:/classes/"{

permission javax.security.auth.AuthPermission "modifyPrincipals";

permission javax.security.auth.AuthPermission "doAs";

//permission java.io.FilePermission "F:/source/test/disadapter/temp.txt", "read, write"; };

because it overrides principal specific grants.

2. Make principal class public (public class User)

3. Use

doAsPrivileged(subject,action,null);

instead of

doAs(subject,action);

see documentation why.

4. Use security manager e.g.

System.setSecurityManager(new java.lang.SecurityManager());

otherwise no checks ever get made.

Right.... try these and things will rock eventually.

Kullervo

kullervokala at 2007-7-1 15:45:48 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 3
I don't know if it will be helpful but try :grant codeBase "file://F:\\libs\\jaas.jar" and not "file:F:/libs/jaas.jar"But this works only under windows systems. You have to double \ because it's an escape character.Hope this is ok.Bye.Yann P.
JOnAS_boys at 2007-7-1 15:45:48 > top of Java-index,Security,Other Security APIs, Tools, and Issues...
# 4

Hi, may i noe where to set the security manager?

I set it right before LoginContext is instantiated but had java.security.AccessControlException: access denied (javax.security.auth.AuthPermission createLoginContext).

I set it after before i call the login() method but had LoginException: javax.security.auth.login.LoginException: java.security.AccessControlException: access denied (javax.security.auth.AuthPermission modifyPrincipals).

Should the setSecurityManager method be called in CallbackHandler class? or the driver class?

tooty at 2007-7-1 15:45:48 > top of Java-index,Security,Other Security APIs, Tools, and Issues...