Checking whether user has access to file/folder

  • Follow


Hi,

I was wondering hwo to go about making sure that a user has access to
read or write to a folder/file on the local/network file system given
just the path from Java. I have a simple test program:

import java.io.*;
import java.lang.*;

public class FileCheck {
  public static void main(String[] args) {
    if (args.length < 1) {
      System.out.println("Please provide the path to the file to
process.");
      return;
    }

    String filePath = args[0];
    SecurityManager sm = System.getSecurityManager();
     if (sm == null) {
       System.out.println("Whoops!! Security manager not found.");
     }
     else {
       sm.checkRead(filePath);
       sm.checkWrite(filePath);
     }
  }
}

which I run as:
java -Djava.security.manager FileCheck <<path to file/folder>>

The problem:
Read access check succeeds for any files in the folder from where I
run the app, however Write access fails everywhere and Read access
fails everywhere outside the folder from which it is run.

TraceBack:
java.security.AccessControlException: access denied
(java.io.FilePermission FileCheck.java write)
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269)
        at java.security.AccessController.checkPermission(AccessController.java:401)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
        at java.lang.SecurityManager.checkWrite(SecurityManager.java:954)
        at FileCheck.main(FileCheck.java:20)


I have also tried using FilePermission:
       FilePermission fp = new FilePermission(filePath, "read");
       sm.checkPermission(fp);
       fp = new FilePermission(filePath, "write");
       sm.checkPermission(fp);
Traceback:
java.security.AccessControlException: access denied
(java.io.FilePermission FileCheck.java write)
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269)
        at java.security.AccessController.checkPermission(AccessController.java:401)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
        at FileCheck.main(FileCheck.java:18)

What I would like to be able to do is is the user on that system has
the right to read/write a file system resource, irrespective of where
its run from.

Any help would be greatly appreciated.

TIA
AM
0
Reply am 11/29/2004 11:07:11 PM



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.801 / Virus Database: 544 - Release Date: 25/11/2004


0
Reply Chris 11/30/2004 12:18:32 AM


Hi AM

Check this site for Java Applets that access local file systems, it may
gib=ve you a lead.

http://www.jensign.com/JavaScience/www/index.html

All the best

Chris

"AM" <am@mailinator.com> wrote in message
news:95b27499.0411291507.6ab97b00@posting.google.com...
> Hi,
>
> I was wondering hwo to go about making sure that a user has access to
> read or write to a folder/file on the local/network file system given
> just the path from Java. I have a simple test program:
>
> import java.io.*;
> import java.lang.*;
>
> public class FileCheck {
>   public static void main(String[] args) {
>     if (args.length < 1) {
>       System.out.println("Please provide the path to the file to
> process.");
>       return;
>     }
>
>     String filePath = args[0];
>     SecurityManager sm = System.getSecurityManager();
>      if (sm == null) {
>        System.out.println("Whoops!! Security manager not found.");
>      }
>      else {
>        sm.checkRead(filePath);
>        sm.checkWrite(filePath);
>      }
>   }
> }
>
> which I run as:
> java -Djava.security.manager FileCheck <<path to file/folder>>
>
> The problem:
> Read access check succeeds for any files in the folder from where I
> run the app, however Write access fails everywhere and Read access
> fails everywhere outside the folder from which it is run.
>
> TraceBack:
> java.security.AccessControlException: access denied
> (java.io.FilePermission FileCheck.java write)
>         at
java.security.AccessControlContext.checkPermission(AccessControlContext.java
:269)
>         at
java.security.AccessController.checkPermission(AccessController.java:401)
>         at
java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
>         at java.lang.SecurityManager.checkWrite(SecurityManager.java:954)
>         at FileCheck.main(FileCheck.java:20)
>
>
> I have also tried using FilePermission:
>        FilePermission fp = new FilePermission(filePath, "read");
>        sm.checkPermission(fp);
>        fp = new FilePermission(filePath, "write");
>        sm.checkPermission(fp);
> Traceback:
> java.security.AccessControlException: access denied
> (java.io.FilePermission FileCheck.java write)
>         at
java.security.AccessControlContext.checkPermission(AccessControlContext.java
:269)
>         at
java.security.AccessController.checkPermission(AccessController.java:401)
>         at
java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
>         at FileCheck.main(FileCheck.java:18)
>
> What I would like to be able to do is is the user on that system has
> the right to read/write a file system resource, irrespective of where
> its run from.
>
> Any help would be greatly appreciated.
>
> TIA
> AM


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.801 / Virus Database: 544 - Release Date: 25/11/2004


0
Reply Chris 11/30/2004 12:21:45 AM

am@mailinator.com (AM) wrote in message news:<95b27499.0411291507.6ab97b00@posting.google.com>...
> Hi,
> 
> I was wondering hwo to go about making sure that a user has access to
> read or write to a folder/file on the local/network file system given
> just the path from Java. I have a simple test program:
> 
> import java.io.*;
> import java.lang.*;

By default, your application when run under a security manager has
access to files in the directory from which the app is run. That makes
sense because otherwise your app would not even be allowed to start.

If you want to read/write elsewhere in the file system you need to
give permissions to your code in a policy file. If you want to go
there, you should put your code in a jar file (give it a package name
also!) and then make a policy file MyPolicyFile.txt like

grant "file:/this/is/my/code.jar" {
permission java.io.FilePermission "/tmp/a", "read,write";
}

run your code with something like:
java -Djava.security.manager
-Djava.security.policy=/tmp/MyPolicyFile.txt -classpath
/this/is/my/code.jar mypackagename.FileCheck /tmp/a

Hope that helps.
0
Reply almut_herzog 11/30/2004 8:01:38 AM

> 
> By default, your application when run under a security manager has
> access to files in the directory from which the app is run. That makes
> sense because otherwise your app would not even be allowed to start.
> 
> If you want to read/write elsewhere in the file system you need to
> give permissions to your code in a policy file. If you want to go
> there, you should put your code in a jar file (give it a package name
> also!) and then make a policy file MyPolicyFile.txt like
> 
> grant "file:/this/is/my/code.jar" {
> permission java.io.FilePermission "/tmp/a", "read,write";
> }
> 
> run your code with something like:
> java -Djava.security.manager
> -Djava.security.policy=/tmp/MyPolicyFile.txt -classpath
> /this/is/my/code.jar mypackagename.FileCheck /tmp/a
> 
> Hope that helps.


Thanks so much for clarifying, wonder why I couldnt find that solution
on the web. Thanks to Chris too for pointing me to the resource.
0
Reply am 11/30/2004 4:48:15 PM

4 Replies
494 Views

(page loaded in 0.088 seconds)

Similiar Articles:













7/24/2012 4:32:12 AM


Reply: