以另一个用户身份加载流程?

时间:2015-05-15 17:22:04

标签: java c linux unix

所以,我有一个root进程(以root身份运行),我希望它使用非root uid加载另一个进程。

目前,我正在调用seteuidsetegid,然后在创建进程后重置为root。我发现该进程仍然使用root的uid加载。我该怎么做才能做到这一点?

Java Code(JNA):

public boolean loadVHost(String java, File sockfile) throws IOException {
    if (CLib.INSTANCE.setegid(suid) != 0) {
        log("setegid C call failed! @ " + id);
        return false;
    }
    if (CLib.INSTANCE.seteuid(suid) != 0) {
        log("seteuid C call failed! @ " + id);
        return false;
    }
    if (CLib.INSTANCE.getegid() != suid || CLib.INSTANCE.geteuid() != suid) {
        log("geteuid/egid C call returned unwanted value! @ " + id + " (returned " + CLib.INSTANCE.getuid() + ":" + CLib.INSTANCE.getgid() + ")");
        return false;
    }
    File hp = new File(homepath);
    hp.mkdirs();
    File avuna = new File(hp, "avuna.jar");
    File main = new File(hp, "main.cfg"); // TODO: add linux user-based RAM/HDD/bandwidth caps
    File hosts = new File(hp, "hosts.cfg");
    if (!avuna.exists() || !main.exists() || !hosts.exists()) {
        log("VHost corrupted, avuna.jar/main.cfg/hosts.cfg is missing! Reinstalling...");
        // if (createVHost(java, sockfile.getAbsolutePath())) {
        // log("Reinstallation completed, vhost loading...");
        // }else {
        // log("Reinstallation failed, vhost NOT loading.");
        // return false;
        // }
    }
    ProcessBuilder builder = new ProcessBuilder(java, "-Xmx" + maxram + "M", "-Xms16M", "-jar", avuna.getAbsolutePath(), main.getAbsolutePath());
    // TODO: if we want to be able to pass std input/output/err, this would be the place
    builder.redirectErrorStream(true);
    this.process = builder.start();
    if (CLib.INSTANCE.seteuid(0) != 0) {
        log("[CRITICAL] setuid C call failed! @ " + id + ", the VHost was loaded, but we were NOT able to re-escalate!");
        return false;
    }
    if (CLib.INSTANCE.setegid(0) != 0) {
        log("[CRITICAL] setgid C call failed! @ " + id + ", the VHost was loaded, but we were NOT able to re-escalate!");
        return false;
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

我做了Brian建议的,并使用了另一个进程来setuid,然后只是执行了我的东西。