I can see two methods:
Allow the user to use
sudo without restriction (which is something dangerous, this means you trust the user somehow), and run the script with the permissions of the user. The script will invoke
sudo each time
/sbin/iptables is needed. This is assuming the script execution will be quick, since some password input will eventually be required at regular intervals¹.
- Advantage: you don't need to have any trust in the script.
- Disadvantage: as already mentioned, allowing the user to use
/sbin/iptables without restriction is something dangerous.
Allow the user to call only the script through
- Advantage: the use of
/sbin/iptables is restricted by the script.
- Disadvantage: the script must be flawless.
About the problem you mentioned: if the script is owned by let's say
root and has usual permissions:
rwxr-xr-x, others users cannot modify it, they can only execute it (eventually through
sudo to obtain more privileges).
With solution 2, and in the case of shell scripts (compared to more robust binaries/programs), beware of environment variables and the several external factors that can modify the execution of your script. Check that your sudo configuration resets properly the definition of every potentially harmful variable.
1. In fact,
sudo can be configured with
NOPASSWD if needed.