Sudo
You will often want to run a DCli script using sudo.
Using sudo can complicate things:
There are two core issues:
1) on debian (and probably other distros) sudo has its own path which is unlikely to include the dart or dcli paths.
2) when trying to run a dart script you may cause the pub cache to be update at which point it will be owned by root and your normally user account won't be able to access it.
If you do this by mistake you can run the following command to fix the problem.
3) You only want parts of your script to run as privileged.
The following dart code also works on Windows. The privilege options simply check that you are running as an Administrator and throw an exception if you are not. Running as a Windows Administrator does not have the same problems that sudo introduces.
Solutions
The following provides guidelines on how to solve the problems.
1) where ever possible avoid using sudo. Of course this often just isn't practical
2) use the 'start' function and pass in the privileged flag:
The privileged flag will check if you are running as sudo, if not it will run the chmod script under sudo. This will cause sudo to prompt the user for their sudo password.
This is a good technique as it limits the use of sudo to just those parts of the script that actually need sudo.
3) compile the script.
Compiling your dart script has a number of benefits.
A compiled script is a completely self contained executable which means it will never cause your pub cache to be accessed which means sudo won't screw it up.
It also fixes the path issues as you don't need dart or dcli on your path for the script to run.
3) pass your path down to sudo
This technique passes your existing user path into sudo which means it can find both dart and dcli.
This method is still dangerous as if dart decides your script needs to be updated then your pub cache will become owned by root.
4) Use withPrivileges
The intent is to allow you to start your script with sudo but only the parts of your script to that need sudo will actually use it.
We still recommend you compile your script to avoid dart changing permissions on pub-cache.
You do this by starting your script with sudo but immediately downgrading your sudo access when the script starts and then using the withPrivileges method for those parts of your script that need to run as sudo.
To run the above script:
5) Pass a password into sudo
If you a trying to run sudo without user intervention then you are likely going to have to pass the password to sudo.
A typically scenario might be calling scripts on a remote system over ssh
he safest way to do this is to:
create a file with 600 as the password (so only you have read/write access)
write the sudo password into that file
create and compile a dart script that can output the password
run sudo -A to run you command and retrieve the password.
Create a script to ask the user for the sudo password
Name the following script something like sudo_ask.dart
In the real world creation of the password file would happen in another part of your code base.
Create a script to be called by sudo when it needs the password:
Call this script sudo_askpass.dart
Create and compile the DCli script that you want to run under sudo.
as well as the sudo_ask.dart script.
Run the script under sudo
Last updated