Run shell command in Tomcat

So, I have the following problem: I have a web service running inside the Tomcat7 server on Linux. However, the web service must execute some commands (mainly file operations such as copying and mounting). Copy I replaced java.nio, but I don't think there is a replacement for mount.

So, I am trying to execute shell commands from my Tomcat Java process. Unfortunately, he does not execute my commands . I have already implemented shell command execution in Java. Therefore, my code should be correct:

Process pr = Runtime.getRuntime().exec("mount -o loop -t iso9660 <myimage> <mymountpoint>");
pr.waitFor();

<myimage>and <mymountpoint>are absolute paths, so no problem.

  • I debugged my commands and they work when executed on the console.
  • I tried sending other commands. Simple commands like idand pwdwork!
  • I tried using /bin/bash -c "<command>"one that didn't work.
  • I tried to execute a shell script that executes a command that does not work.
  • I tried to avoid gaps in my team, which did not work.

So, I went even deeper, and now I suspect some Tomcat security policies ( Sandbox ?), Which prevents me from executing the command. Since security is not a problem for me (this is an internal system completely isolated from the outside world), I tried to hack, which recently became popular:

System.setSecurityManager(null);

. Java7 Tomcat7 RHEL6. Tomcat7 ! /etc/.. , /opt/tomcat/, zip Tomcat. /opt/tomcat/conf , , , catalina.policy, , .

?

+5
5

, Runtime.exec. ProcessBuilder , Java, ( ).

ProcessBuilder pb = new ProcessBuilder("/bin/mount", "-o", "loop", /*...*/);
pb.redirectErrorStream(true); // equivalent of 2>&1
Process p = pb.start();

, RHEL, selinux active? , ( , audit.log, , , selinux). , , , serverfault, SO...

+5

:

System.setSecurityManager(null);

.

, Tomcat root. id, root.

!

. Tomcat, -, , script Java-. , ( ) root- Tomcat. , . , /etc/fstab . POV , , tomcat. tomcat root. , :
1) Tomcat root
2) Tomcat
3) tomcat, (, ..)
4) , tomcat
5) /etc/fstab, .

+7

, , , Runtime.exec() . . , .

+1

- Swing.

, ProcessBuilder, , , - , script, , , . ProcessBuilder script.

, , , , , , .

+1

sudo -S tomcat7: tomcat7 ALL=(ALL) NOPASSWD:ALL

+1

All Articles