This question is totally general and not only applicable to my situation, but... I have a small busybox appliance where I want a non-root user to be able to execute a particular script with root priviliges. For example, something like this small script to enable DHCP, where the only variable (
$1) to send in on the cmdline (!!) is what host name to send out:
#!/bin/bash udhcpc -b -i eth0 -h $1
running udhcpc like this requires root access, so to do this I plan on modifying
/etc/sudoers to contain this line:
joe ALL = NOPASSWD: /path/to/enable_dhcp.sh
which should enable "joe" to easily run that script with root privileges by simply running:
and not being asked for a password (which is what I want, since I want joe to be able to script this).
Now.. I know (or at least think I do) that using
$1 in a script that can easily be run with root privileges is a HORRIBLE idea since you can inject whatever you want into that.
So... what is the best way to deal with this? How do I let joe do what I want with root privileges, allow him to pass in a variable (or effectively do so like with an environment variable), while not being wide open to injection attacks?