2010-11-15 66 views
3

我想子进程的用户转变为轻微的特权 用户,但是当我与父同用户执行的ProcessBuilder子进程EXEC的启动方法更改用户环境下的的ProcessBuilder + java的

 LinkedList<String> commands = new LinkedList<String>(); 
     commands.add("vlc"); 
     ProcessBuilder builder = new ProcessBuilder(commands); 
     Map<String,String> enviroment = builder.environment(); 
     enviroment.clear();    
     enviroment.put("USER", "otheruser"); 
     enviroment.put("LOGNAME", "otheruser"); 
     enviroment.put("PWD", "/home/otheruser"); 
     enviroment.put("HOME", "/home/otheruser"); 
     enviroment.put("USERNAME", "otheruser"); 
     enviroment.put("SHELL", "/bin/false"); 
     builder.directory(new File("/home/otheruser"));    

     Process process = builder.start(); 
     process.waitFor(); 

我在Linux操作系统(Ubuntu的)

回答

0

你不能仅仅通过传递不同USER环境变量改变有效用户的工作。这是Linux的安全功能(通常是Unix),否则恶意用户可以将USER变量设置为ROOT。除非可执行文件标记为setuid,或者进程执行setuid()更改有效用户(并允许setuid()),否则子进程始终以与父进程相同的用户身份执行。

3

吉姆是绝对正确的。 但是,如果您仍想以不同的用户身份运行程序,则必须使用用户平台相关工具。

Windows: 使用runas命令,例如:runas/user:domain \ jamesbond regedt32.exe 不幸的是runas要求用户手动输入密码。 以下文章解释了如何解决此问题: http://www.windowsnetworking.com/kbase/WindowsTips/WindowsXP/AdminTips/Miscellaneous/RunprogramsasanotheruserinWindows2000WindowsXP.html

或者,您可以在VBS中编写自己的实用程序并从java运行它。看到这篇文章的详细信息:http://weblogs.asp.net/hernandl/archive/2005/12/02/startprocessasuser.aspx

Unix: 请参阅su和sudo的参考。 su很好,但它也需要密码(除非当前用户是root)。 要解决此问题,您可以创建期望脚本(请参阅http://en.wikipedia.org/wiki/Expect)。 Expect默认安装在大多数unix发行版上。

祝你好运!