开发者

Restarting a job (upstart) in a php script

I am currently writing the admin portal for my most recent project. I have been fighting with my computer to get upstart working, and now that it is, I wanted to be able to operate upstart from the web. I need to execute the 开发者_开发技巧following in my php script

sudo restart job
sudo start job
sudo stop job

as you can see sudo is the theme of those commands, so I need to somehow run sudo from this php script. How can I attack this problem, or is there a work around for this.

in case it matters:

# which start
 /sbin/start
# which stop 
 /sbin/stop
# which restart
 /sbin/restart


I see several approaches. You could certainly add www-data to the sudoers list, and then either hard-code www-data's password into your script (not so good) or read it from a file (a little better) to make sudo work. If you go this route, you'll probably need to manually override www-data's password, since it doesn't really have a usable password. Either of these approaches should be discouraged because www-data is deliberately stripped of most privileges as a security measure. Granting it a big one (like sudo) and putting its password in a file or script creates a significant security vulnerability.

You might be able to grant www-data very limited sudo privileges that enable the functionality you need without opening up too many security holes. Read up on sudo and the sudoers file for more information.

A better approach might be to write a script that takes a start/stop/restart argument and invokes the appropriate upstart command. The trick is to make this script setuid root (chown root:root script.sh; chmod +s script.sh), so that it runs as root instead of www-data. Give this script 755 permissions so only root can change it. This limits the security risk and still gives you the powers you need, but no more.

The setuid approach probably won't work directly on your system (try it first), since most Unix systems these days deliberately disable setuid for working as advertised on shell scripts since the security risk is too high. Here is an article that explains a workaround that uses a C program (on which setuid still works) to invoke your script. It is a little convoluted, but it should work.

Possibly the best approach would be to leverage upstart's event mechanism and have your web code fire events that upstart would catch and forward to your .conf file. I am just learning upstart myself, so I can't give you more specifics, but I get the sense it is meant to be used this way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜