使用 Java 分叉和删除权限
我正在用 Java 编写一个服务器程序,允许用户使用 DRMAA 提交作业。尽管主服务器进程以 root 身份运行,但它所做的只是验证用户身份,然后启动另一个以该用户身份运行的 Java 程序,并实际完成工作,以遵守权限最小化的原则。最初,我使用 Runtime.exec()
和 sudo
(下面的示例)来完成此操作,直到进程被守护为止,此时 sudo 很沮丧,因为它没有终端。
String[] command = {"sudo", "-i", "-u", username, java, theOtherJavaProgram};
Runtime.getRuntime().exec(command, null, getHomeDirectory(username));
作为守护进程运行时,在 Java 中执行此分叉和删除权限模式的最佳方法是什么?有办法吗?我是否必须放弃 C 语言并学习如何使用 JNI 创建 JVM?
I'm writing a server program in Java that will allow users to submit jobs using DRMAA. Although the main server process runs as root
, all it does is authenticate the user, then start another Java program which runs as that user and actually does the work in order to comply with the principle of minimising privileges. Initially, I was doing this with Runtime.exec()
and sudo
(example below) which works fine until the process is dæmonised, at which point sudo
gets upset because it doesn't have a terminal.
String[] command = {"sudo", "-i", "-u", username, java, theOtherJavaProgram};
Runtime.getRuntime().exec(command, null, getHomeDirectory(username));
What's the best way to do this fork and drop privileges pattern in Java when running as a daemon? Is there a way? Am I going to have to break out the C and learn how to create JVMs with JNI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 JNI 来放弃权限可能更容易。
这是我之前敲出的一个:
UID.java
unix_uid.c
UID.h
是由UID.class
机器生成的> 使用javah
。It's probably easier to just use JNI to drop privileges.
Here's one I knocked up earlier:
UID.java
unix_uid.c
UID.h
is machine generated fromUID.class
usingjavah
.您可以使用
su(1)
而不是sudo(8)
。su(1)
涉及的内容要少得多,并且可能不需要终端本身。 (当然,如果您的 PAM 配置需要su(1)
的终端输入,那么这也可能无法正常工作。)You could use
su(1)
instead ofsudo(8)
.su(1)
is much less involved, and probably won't want the terminal itself. (Of course, if your PAM configuration requires terminal input forsu(1)
, then this might not work well either.)如果您仅想以
root
身份启动非root
进程,那么su
就足够了。当从root
转到另一个用户时,它不会要求输入密码,因此它不需要终端。If you only want to start a non-
root
process asroot
, thensu
will be sufficient. It will not ask for a password when going fromroot
to another user, so it should not need a terminal.