2017-08-02 174 views
5

我了解以root用户身份运行脚本的含义,特别是通过Web应用程序。然而,作为我的Web应用程序的一部分,我需要使用卷曲与tor,这需要偶尔重置tor ip。当服务重新启动时,tor可以获得一个新的IP地址为service tor restart。因为只有root可以做到这一点,所以我写了一个C wrapper脚本来完成我所需要的工作,然后编译它并设置setuid root并将其更改为root用户所有权。但是,当它作为非特权用户运行时,它仍然问我root密码。以root用户身份,服务重启不应该要求密码。以root用户身份执行命令,无需root密码或sudo

我的脚本:

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 

void ExecAsRoot (char* str); 
int main() 
{ 
    setuid (0); 
    setvbuf(stdout, NULL, _IONBF, 0); 
    printf ("Host real ip is: "); 
    ExecAsRoot("ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'"); 
    ExecAsRoot("/usr/sbin/service tor restart"); 
    // sleep(2); 
    printf ("Tor should have switched to a new ip by now.\nNew ip is: "); 
    ExecAsRoot("torify curl ifconfig.co 2>/dev/null"); 
    return 0; 
} 

void ExecAsRoot (char* str) { 
    system (str); 
} 

我已经做了以下内容:

chown root restartor 
chmod u=rwx,go=xr restartor 

输出:

Host real ip is: 7.17.11.23 
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === 
Authentication is required to restart 'tor.service'. 
Authenticating as: root 
Password: 

我怎样才能得到这为网络用户的身份运行,而无需提供根密码?

回答

5

您没有设置setuid位的文件权限:

#-------v 
chmod u=srwx,go=xr restartor 
相关问题