3

My application has multiple users and 1 superuser. I am trying to write and store a file in Linux through Java code but i get permission denied error. I used the following code:

Process process = Runtime.getRuntime().exec("/usr/bin/tiff2pdf -o /tmp/tiff_dir/temp.pdf /tmp/tiff_dir/image.tiff");
int returnCode = process.waitFor();

I get following error:

java.io.FileNotFoundException: image.tiff (Permission denied)

From my analysis, it seems that because the user does not have root permissions, i am getting this error. What is the solution to this?

17
  • use sudo with your commandline. but you will need to enter a password. be careful with root... Commented Feb 13, 2014 at 9:24
  • 11
    Find out why you're not having the permissions you expect instead of running superuser commands. That does not seem like a command which should be needing root access. Commented Feb 13, 2014 at 9:25
  • Have you tried with full path to file? Probably the issue is with cwd not being what you think it is. Privilege elevation is never an answer. Commented Feb 13, 2014 at 9:33
  • @PhilippSander But i don't want users to be typing in the password for root. Commented Feb 13, 2014 at 9:33
  • 2
    Don't you think that it would be a huge security hole if random programs were allowed to reach superuser privileges without any password prompt or the like? Commented Feb 13, 2014 at 9:35

3 Answers 3

1

You shouldn't run a command like that as a super user because it poses a security risk (i.e. if someone gained control of your java program, then they have the keys to the kingdom). Instead, you should run with lower permissions.

It looks like the issue is with access to image.tiff not with tiff2pdf. Check the owner and permissions of image.tiff.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes, i know that the issue is with the permissions of the file "image.tiff". Problem is how do i change permission of this file from a non superuser?
How do you elevate the permissions of the process? Take a look at this: stackoverflow.com/questions/4662574/…
I would add, however, that this should ONLY be the plan if managing permissions is within the application's scope. If its simply a "way to get it done", you need to find another way.
Another possible solution would be to create a service whose sole purpose is to run the tiff2pdf command. You would run it as a daemon with higher privileges and make calls to it from your lower privileged program. This way you can contain the security risk if you absolutely need to run it as a super-user.
@orourkedd, this is clearly a Linux question, elevating the UAC isn't particularly relevant here. Anyway, there's something wrong with the question, since it can't possibly throw this exception there.
|
1

Firstly, these two lines will not produce a java.io.FileNotFoundException: image.tiff (Permission denied):

Process process = Runtime.getRuntime().exec("/usr/bin/tiff2pdf -o temp.pdf image.tiff");
int returnCode = process.waitFor();

If for some reason, the command fail, it will return a non-zero return code will produce some output on the process's standard error (that's the convention). You can get that standard error from process.getErrorStream() (it might be worth having a look at the standard output too, just in case). If there's an issue with the file not being found there, it will not throw a FileNotFoundException like this, since Java cannot understand the expected output from your command.

EDIT, following your comment:

It was thrown from this point only and value of returnCode was 1. Also everything worked fine once i manually changed the file permissions from a root user.

That's just not possible. If your application throws an exception at either of these two lines, it will exit the normal control flow: you will not be able to read the returnCode at all.

Secondly, your should run your exec command with each argument in a String[] instead of having it all in one line, this should prevent quotation problems if file names have spaces for example.

I would also suggest using absolute paths in your command, to make sure you're working in the directories you expect. (*EDIT: * Now that you're using absolute paths, make sure your user has rwx permissions on /tmp/tiff_dir .)

To answer your question more directly, you can certainly run sudo with Runtime.exec(new String[] {"/usr/bin/sudo", ... the rest of your command ... }, but this is a bad idea, for security reasons. You'd also need to change the sudoers file to allow it without password, or find a way to pass in a password, either on the command line (definitely a security risk!) or by passing it to the input stream manually, somehow.)

1 Comment

Is their a setid() method in Java like C?
0

Try this :

File file = new File("/opt/image.tiff");
Runtime runtime = Runtime.getRuntime();
runtime.exec(new String[] { "/bin/chmod", "777",file.getPath()});

This will execute full permission on the file.

6 Comments

Dude... He does not want to change permissions on the file.
@noloader Yes, but changing the permission the file is better than executing it as super user
No. Never change what the user set, at least not before asking the user. There might be a certain reason for a file to have a certain permission mode.
@Sandeep Can a non-superuser change the file permissions? I am unable to change permissions from a non super user.
@akshay, normal users change change file permissions on files they own. However, once again, the error you get was not produced by the two lines of code you're showing us.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.