我理解以root身份运行脚本的影响,尤其是在web应用程序中。然而,在我的web应用程序中,我需要使用带有tor的curl,并且需要偶尔重置tor IP。当服务使用
我已经完成了以下内容:
我该如何在不提供 root 密码的情况下以 Web 用户身份运行此程序?
service tor restart
重新启动时,tor可以获取新的IP。由于只有root才能执行此操作,因此我编写了一个C包装脚本来完成所需工作,并对其进行了编译并设置为setuid root,并更改为root用户所有权。但是,即使以非特权用户身份运行它,它仍会要求我输入root密码。作为root,服务重启不应该要求密码。
我的脚本:
#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:
我该如何在不提供 root 密码的情况下以 Web 用户身份运行此程序?