Detecting The Sudo User In Phing

10th April 2013 - 9 minutes read time

I use Phing for a lot of different tasks, it helps me to automate things that I would otherwise mess up if left to my own devices. Prime candidates for Phing scripts are things that I don't do that much and forget how to do them, or that have a number of complex steps. The only problem I have found is that because many of the Phing scripts I create rely on system changes (eg, configuring an Apache server) they therefore require system changing privileges. Normally I would just prefix the Phing command with sudo, but every now and then I forget all about that step and the build fails. This can be dangerous as I am then left with a build that failed, which might leave a system partly configured or even take a server offline.

This got me to thinking about what Phing could do to remind me if I forget to use sudo on my sudo requiring scripts. There must a better way of reminding myself that I should be running the script as sudo rather than just having the build fail on me. After looking through the Phing documentation and a bit of searching online I found nothing that does this.

The @phingofficial channel on Twitter (who are awesome by the way) suggested that I could use the output of 'whoami' or 'id -u' to test if a user was running as sudo. This is quite easy to do using the task, which I can use to set a property. Here are a couple of examples.

The following sets the property 'currentuser' to be the output of the 'whoami' command. If the script is being run as sudo then this will be 'root' otherwise it will be the username of the current user.

<exec command="whoami" outputProperty="currentuser" />

The following sets the property 'currentuser' to be the output of the 'id -u' command. If the script is being run as sudo then this will be '0', otherwise it will be the ID of the current user (1000 on my localhost).

<exec command="id -u" outputProperty="currentuser" />

If you are on Windows then you might be able to use the following command, which will produce an access denied message if the user is not an administrator. If you have any other suggestions on how to detect the administrator user on Windows via the command line (or even through PHP) then I'd be happy to hear from you.

With that in place I can use the property to create a boolean value that I can use later in the script using the task. Assuming that I use the 'whoami' command from the above examples I can do the following.

  1. <!-- Create the currentuser property -->
  2. <exec command="whoami" outputProperty="currentuser" />
  4. <!-- Set the 'issudo' property to be true if the user is root -->
  5. <condition property="issudo">
  6. <equals arg1="${currentuser}" arg2="root" />
  7. </condition>
  9. <!-- Fail the build if the user is not root -->
  10. <if>
  11. <not>
  12. <equals arg1="${issudo}" arg2="true" />
  13. </not>
  14. <then>
  15. <fail message="You must run this build as sudo!" />
  16. </then>
  17. </if>

Alternatively, I could just a single task to do the same sort of check.

  1. <!-- Fail the build if the user is not root -->
  2. <if>
  3. <not>
  4. <equals arg1="${currentuser}" arg2="root" />
  5. </not>
  6. <then>
  7. <fail message="You must run this build as sudo!" />
  8. </then>
  9. </if>

The task here will just stop all execution of the script and show that the build has failed to the user, along with the message text.

I could have left if there, but I wanted to take it further by creating a custom Phing task that will then create a property called 'issudo' that will tell me if the user has used sudo to run the build script. This involves creating a PHP class that checks the sudo status using PHP, which should therefore work on any platform. To integrate with Phing you just need to create a class that extends Task and implements a method called main(). The main() method is executed when the object is used so it is within this method that the work needs to be done to check the user and create the property.

To find out if a user has run a PHP script using sudo is quite easy on Linux hosts. The $_SERVER array will contain a few elements that are to do with who the origin user was and what command was run when used on the command line. If we look for the presence of SUDO_COMMAND within the $_SERVER array then we can be pretty sure that the user is running the script as a sudo user. Again, if you have more information on how this works on Windows I'd be happy to hear from you.

To set a property that can be used in the Phing build file the $this->project->setProperty() method needs to be called. This will create a property with a given name and takes two parameters.

  • $name : The name of property to set.
  • $value : The value of the property. If the property name already exists then this will overrite the previous value.

The following class was placed into the same directory as my build script with the name of IsSudo.php. The call to log() just prints out a message to the terminal so that we can see that the class has been run.

  1. /**
  2.  * Sets a phing property called 'issudo' that can be used to see if the user has root privilages.
  3.  *
  4.  */
  5. class IsSudo extends Task {
  7. /**
  8.   * Set the property.
  9.   */
  10. public function main() {
  11. $issudo = false;
  13. $this->log('Determining sudo.');
  15. if (isset($_SERVER['SUDO_COMMAND'])) {
  16. $issudo = true;
  17. }
  19. $this->project->setProperty('issudo', $issudo);
  20. }
  21. }

The final step here is to tell Phing that we have a class that we want to be used. This is done using the task. In it's simplest terms this task takes two parameters of the name of the task we want to create and the name of the class that contains the code behind the task. The following code creates a task called , which can be found in the class IsSudo.

This allows us to use the custom issudo task that creates a property called ${issudo}, which can then be used to see if the user has run the script as sudo.

  1. <!-- Create issudo property -->
  2. <issudo />
  4. <!-- Check issudo -->
  5. <if>
  6. <not>
  7. <istrue value="${issudo}" />
  8. </not>
  9. <then>
  10. <fail message="You must run this build as sudo!" />
  11. </then>
  12. </if>

This works as intended and will stop the execution of the build file if I've forgotten to run it via sudo.

I did think that it might be possible to integrate with the comparison operators within Phing and create an element that works in much the same way as the or tasks. After some investigation I don't think there is a way of doing this without altering the Phing source code.

Update : @Beryllium9 on twitter suggested that a good way of forcing a sudo check is to create a single sudo target and then require that as a prerequisite to running any sudo requiring targets. The way to do this is to use the depends property of the target element. Adding a depends attribute to the target element will make Phing run the named target first before running the current target. This is the case even if the default target is being called.

As an example the following build file will only run the main target if it has been run using sudo.

  1. <?xml version="1.0"?>
  2. <project name="sudotest" default="main">
  4. <target name="sudotest">
  5. <!-- Create the currentuser property -->
  6. <exec command="whoami" outputProperty="currentuser" />
  8. <!-- Set the 'issudo' property to be true if the user is root -->
  9. <condition property="issudo">
  10. <equals arg1="${currentuser}" arg2="root" />
  11. </condition>
  13. <!-- Fail the build if the user is not root -->
  14. <if>
  15. <not>
  16. <equals arg1="${issudo}" arg2="true" />
  17. </not>
  18. <then>
  19. <fail message="You must run this build as sudo!" />
  20. </then>
  21. </if>
  22. </target>
  24. <target name="main" depends="sudotest">
  25. <echo>Running main target</echo>
  26. </target>
  27. </project>


Add new comment

The content of this field is kept private and will not be shown publicly.