tags:

views:

113

answers:

5

Hi All, Hi I need to put some files in some directory which myself as a user have no rights but for administrator has rights. In this case I want my program should ask for admin username and password. If i put correct admin username and password then i must be able to put the folder in that directory. Otherwise i should get a message box for wrong username and password

Thanks Sunil Kumar Sahoo

A: 

Create a folder such that the permissions are as follows: (but you get a choice!)

Choose this option if you (and the 'others') want to be able to save, copy, or move files into this folder, but not see the contents of it:

 ___________________________________
|        | User   | R | W | X | Val |
|--------+--------+---+---+---+-----|
| user   | you    | X | √ | X |  2  |
| group  | admins | √ | √ | √ |  7  |
| others | N/A    | X | √ | X |  2  |
|________|________|___|___|___|_____|

Choose this option if you (and the 'others') should not have any privileges to this folder:

 ___________________________________
|        | User   | R | W | X | Val |
|--------+--------+---+---+---+-----|
| user   | you    | X | X | X |  0  |
| group  | admins | √ | √ | √ |  7  |
| others | N/A    | X | X | X |  0  |
|________|________|___|___|___|_____|

Does this solve your problem or am I misinterpreting it?

Alexsander Akers
Both tables are same buddy. :)
Adeel Ansari
Whoops, I copied and pasted it, but forgot to change the second one. :)
Alexsander Akers
+2  A: 

AFAIK you cant do this with Java alone, you may need JNI.

Suraj Chandran
Does she need to do this via Java or is it mis-tagged? It seems like she could do this through `chmod` et al., but the `Java` tag seems to be misleading.
Alexsander Akers
JNI is Java Native Interface, which is the only option to change permissions etc' from Java
abyx
I wan to do the same thing using java
Deepak
@Sunil - as the answer says, you can't do it in Java.
abyx
+1  A: 

At least in Unix-like operating systems (e.g. Linux), you cannot possibly do that in Java, not even with JNI. You need a binary executable file (not a shell script, java program etc.), owned by root, with the set-user-ID-on-execution bit set. This program should (but technically, doesn't have to) check that you are permitted to do what you want to do (by asking you for the password or whatever) and do the operation for you. BTW, su and sudo are like this.

The other possibility, and I think that's the standard way in Windows, is to have a daemon program running (started by the admin so it has the necessary privileges) and this daemon waits for commands to process (by some means of IPC, network etc.), checks if the user should be allowed to do that (however this is determined) and eventually processes those commands.

ammoQ
A: 

Since Java 1.6 the java.io.File has the following methods:

  1. setReadable()
  2. setWritable()
  3. setExecutable()

Do a guess what they do. There's a overloaded method with a boolean ownerOnly. This should be the solution if you're already logged in as admin (even more; in Windows it's impossible to give a file admin-only rights if you're already not the admin).

BalusC
A: 

Its not possible in java. without the permission of root/admin the user cannot craete any folder or directory in root file system. The folder needs admin access.

Deepak