Q:

How to mount and unmount devices remotely from a web service

Don't ask me why, but I have to find a way to allow the Apache user www-data to mount and unmount devices at run-time on the host system. Such devices are, for example, external USB drives on /dev/sdaXY formatted with NTFS filesystem.
I'm running the web service on a Debian machine using Apache 2.2 and PHP 5.3. When I try to run the mount command via PHP's exec() I obviously have these error message:
exec('mount /dev/sda1 /media/usb_flash');
// -> mount: only root can do that

exec('sudo mount /dev/sda1 /media/usb_flash');
// -> sudo: no tty present and no askpass program specified

exec('sudo -n mount /dev/sda1 /media/usb_flash');
// -> sudo: sorry, a password is required to run sudo

I'm aware only root can do that (in fact, from SSH, I always need sudo when mounting) and that www-data cannot be a sudoer. But I'm sure there's a way, a workaround, to make my task possible. I don't know where to start looking at: Googling my question is useless, I bet this is not a common scenario.

A:

I solved using pmount. After I found this tool, it looked the simplest solution to me (no sudoers edits, no fstab stuff, nothing).

pmount - mount arbitrary hotpluggable devices as normal user
pmount  ("policy mount") is a wrapper around the standard mount program
        which permits normal users to mount removable devices without a  match-ing /etc/fstab entry.

As the manual says, on Debian the  permission  to  execute  pmount  is restricted to members of the system group plugdev. I had to run the following command once vis SSH, to add Apache usar to the plugdev group:
sudo adduser www-data plugdev

I restarted Apache, and now I can mount and unmount my USB drives from PHP without sudo:
exec('pmount /dev/sda1 usb_flash'); // Mounted on /media/usb_flash
exec('ls -al /media/usb_flash'); // List files, owned by www-data:600
exec('pumount usb_flash'); // Unmounted

