mytomi
Posts: 2
Joined: Sat Nov 09, 2013 4:55 pm

Permission denied for www-data accessing /dev/video0 w/php

Sat Nov 09, 2013 5:12 pm

I'm running Raspbian 3.6.11+ #538. I have a php web page on lighttpd to take webcam photos with fswebcam.

The php part:

Code: Select all

<?php
$command = "/var/www/bin/TakeAnotherPic.sh";
echo "<H1>Taking a picture at the moment...</H1>\n";
$output = shell_exec($command);
echo "$output";
?>
The script:

Code: Select all

#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
whoami #debug
/usr/bin/fswebcam -v -r 640x480 /var/www/webcam/$(date +\%Y-\%m-\%d-\%H-\%M).jpg 2> /var/www/bin/fswebcam.txt
Now, when running from command line, as user www-data (su www-data), everything works just right. The webcam pic is saved in the right directory.

When running the script from php web page (confirmed user is www-data), the script excecutes, but the fswebcam part fails with error messages:

Code: Select all

--- Opening /dev/video0...
Trying source module v4l2...
Error opening device: /dev/video0
open: Permission denied
Trying source module v4l1...
Error opening device: /dev/video0
open: Permission denied
Unable to find a source module that can read /dev/video0.
Further info

Code: Select all

# ls -la /dev/video0
crw-rw---T 1 root video 81, 0 Nov  6 10:35 /dev/video0

Code: Select all

# grep video /etc/group
video:x:44:pi,www-data
The webcam is Microsoft Lifecam 5000 connected via externally powered USB hub.

How come the permissions for www-data are different when executing the script from php or from command line? What am I missing here? I suspect an easy fix, but what on earth is it?

Thanks

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Permission denied for www-data accessing /dev/video0 w/p

Sat Nov 09, 2013 7:46 pm

Are you sure that PHP is running as www-data?

Code: Select all

<?php
echo "<p>Current User ID is: ". posix_getuid();
echo "<p>Current Group ID is: ". posix_getgid();
?>
Are you sure when it runs the script with shell_exec() that the spawned process is running with www-data?
Are you running FastCGI? Is that chrooted (for security reasons)?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

mytomi
Posts: 2
Joined: Sat Nov 09, 2013 4:55 pm

Re: Permission denied for www-data accessing /dev/video0 w/p

Sun Nov 10, 2013 12:16 pm

Problem solved. I had to uncomment a line in php.ini:

Code: Select all

cgi.fix_pathinfo = 1
Thanks for leading me to the right direction!

Return to “Troubleshooting”