Discussion:
Problem with PHP and mounted folder
Cliff Nieuwenhuis
2014-02-28 16:46:48 UTC
Permalink
In short, PHP isn't recognizing a folder I have that is a mount point to a CIFS share.


Environment / Background information:

PHP 5.3.3 on Apache/2.2.15 (centOS) 2.6.32-431.5.1.el6.i686.
SElinux disabled; firewall off during testing.

Website document root is "/var/www/html" Within this directory I have a
folder called "projects", which is a mount point to a CIFS share on a
Windows 2003 server. It is mounted via fstab entry:

//WIN01/Projects /var/www/html/projects cifs rw,domain=XXX,credentials=/etc/samba/auth.support,uid=48,gid=48 0 0

(my UID 48 = apache)

So far this works as expected -- the projects folder is mounted by the
Windows user identified in the credentials file (I've verified this on
the Windows server) but appears to be owned by apache.apache. If I do this:

#su - apache -s/bin/bash

..I can browse to the /var/www/html/projects folder and its sub-folders
and add / edit files as expected.

When mounted, the /var/www/html/projects contains many levels of subfolders,
for example project_1, project_2, etc.

In addition to the projects folder, the website has a number of other "normal"
folders: test_1, test_2.

Finally, the PHP configuration file has this setting defined:
php_admin_value open_basedir "/var/www/html/:/var/www/html/projects/:/tmp/"
..although I've also tried:
php_admin_value open_basedir none



The Problem:

The web application needs to read and create files in the projects folder, but
PHP doesn't recognize the projects folder as folder! I suspect a
permissions problem of some sort, but I can't figure our where. I see nothing
in the HTTPD error logs. Also, I did have this same code working on an older server
[PHP 5.3.28 Apache/2.2.3 (CentOS) 2.6.18-371.4.1.el5 ]-- the
trouble started when I set the project up on the newer CentOS system.


Here's some test code that shows the problem:

$dirs = glob("$pat", GLOB_ONLYDIR );
if ( is_array($dirs) ) {
foreach ( $dirs as $dirname ) {
echo "Folder -> $dirname <br />\n";
}
}

Output:
Folder -> test_1
Folder -> test_2

(note -- no folder 'projects'!)


Tried the following:

echo "projects " . ( is_dir('projects') ? 'is' : 'is not' ) . " a directory<br />\n";
echo "projects " . ( is_file('projects') ? 'is' : 'is not' ) . " a file<br />\n";

Output:
projects is not a directory
projects is not a file

echo "test_1 " . ( is_dir('test_1') ? 'is' : 'is not' ) . " a directory<br />\n";
echo "test_1 " . ( is_file('test_') ? 'is' : 'is not' ) . " a file<br />\n";

test_1 is a directory
test_1 is not a file
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
2014-02-28 17:24:51 UTC
Permalink
On Fri, Feb 28, 2014 at 11:46 AM, Cliff Nieuwenhuis <
Post by Cliff Nieuwenhuis
In short, PHP isn't recognizing a folder I have that is a mount point to a CIFS share.
PHP 5.3.3 on Apache/2.2.15 (centOS) 2.6.32-431.5.1.el6.i686.
SElinux disabled; firewall off during testing.
Website document root is "/var/www/html" Within this directory I have a
folder called "projects", which is a mount point to a CIFS share on a
//WIN01/Projects /var/www/html/projects cifs
rw,domain=XXX,credentials=/etc/samba/auth.support,uid=48,gid=48 0 0
(my UID 48 = apache)
So far this works as expected -- the projects folder is mounted by the
Windows user identified in the credentials file (I've verified this on
#su - apache -s/bin/bash
..I can browse to the /var/www/html/projects folder and its sub-folders
and add / edit files as expected.
When mounted, the /var/www/html/projects contains many levels of subfolders,
for example project_1, project_2, etc.
In addition to the projects folder, the website has a number of other "normal"
folders: test_1, test_2.
php_admin_value open_basedir
"/var/www/html/:/var/www/html/projects/:/tmp/"
php_admin_value open_basedir none
The web application needs to read and create files in the projects folder, but
PHP doesn't recognize the projects folder as folder! I suspect a
permissions problem of some sort, but I can't figure our where. I see nothing
in the HTTPD error logs. Also, I did have this same code working on an older server
[PHP 5.3.28 Apache/2.2.3 (CentOS) 2.6.18-371.4.1.el5 ]-- the
trouble started when I set the project up on the newer CentOS system.
$dirs = glob("$pat", GLOB_ONLYDIR );
if ( is_array($dirs) ) {
foreach ( $dirs as $dirname ) {
echo "Folder -> $dirname <br />\n";
}
}
Folder -> test_1
Folder -> test_2
(note -- no folder 'projects'!)
echo "projects " . ( is_dir('projects') ? 'is' : 'is not' ) . " a directory<br />\n";
echo "projects " . ( is_file('projects') ? 'is' : 'is not' ) . " a file<br />\n";
projects is not a directory
projects is not a file
echo "test_1 " . ( is_dir('test_1') ? 'is' : 'is not' ) . " a directory<br />\n";
echo "test_1 " . ( is_file('test_') ? 'is' : 'is not' ) . " a file<br />\n";
test_1 is a directory
test_1 is not a file
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
You guessed right. PHP usually runs under a user with less permissions
(www, user, etc..). You are probably logged in as a user with permissions
to access that mounted drive while PHP isn't. Both fixes are ugly, but what
I would recommend is giving permission to those files as opposed to giving
PHP user more power.
Cliff Nieuwenhuis
2014-02-28 18:00:42 UTC
Permalink
Cliff Nieuwenhuis wrote:
[snip]
Post by Cliff Nieuwenhuis
The web application needs to read and create files in the projects folder, but
PHP doesn't recognize the projects folder as folder!  I suspect a
permissions problem of some sort, but I can't figure out where.
 
[snip]
Post by Cliff Nieuwenhuis
You guessed right. PHP usually runs under a user with less permissions (www, user, etc..). You are probably
logged in as a user with permissions to access that mounted drive while PHP isn't. Both fixes are ugly, but
what I would recommend is giving permission to those files as opposed to giving PHP user more power.
Thanks for the reply.

I thought that PHP runs under HTTPD as user 'apache'. I know that the Apache webserver runs as user 'apache' on my system -- does PHP run as a different user? Assuming that it does not, wouldn't my successful test of traversing, reading, and writing files under the 'projects' folder by doing 'su - apache -s/bin/bash' mean that the permissions are OK? I guess not, but I can
Aziz Saleh
2014-02-28 19:41:07 UTC
Permalink
r


On Fri, Feb 28, 2014 at 1:00 PM, Cliff Nieuwenhuis <
Post by Cliff Nieuwenhuis
[snip]
Post by Cliff Nieuwenhuis
The web application needs to read and create files in the projects
folder, but
Post by Cliff Nieuwenhuis
PHP doesn't recognize the projects folder as folder! I suspect a
permissions problem of some sort, but I can't figure out where.
[snip]
Post by Cliff Nieuwenhuis
You guessed right. PHP usually runs under a user with less permissions
(www, user, etc..). You are probably
Post by Cliff Nieuwenhuis
logged in as a user with permissions to access that mounted drive while
PHP isn't. Both fixes are ugly, but
Post by Cliff Nieuwenhuis
what I would recommend is giving permission to those files as opposed to
giving PHP user more power.
Thanks for the reply.
I thought that PHP runs under HTTPD as user 'apache'. I know that the
Apache webserver runs as user 'apache' on my system -- does PHP run as a
different user? Assuming that it does not, wouldn't my successful test of
traversing, reading, and writing files under the 'projects' folder by doing
'su - apache -s/bin/bash' mean that the permissions are OK? I guess not,
but I can't see why not.
--
Cliff Nieuwenhuis
You are correct, when I said PHP I meant through the server. Try using
runuser command instead. If you have exec enabled, try running an ls
command as a user with higher permissions and see if that works.
Cliff Nieuwenhuis
2014-02-28 20:43:14 UTC
Permalink
From: Aziz Saleh [mailto:***@gmail.com]
Sent: Friday, February 28, 2014 1:41 PM
To: Cliff Nieuwenhuis
Cc: php-***@lists.php.net
Subject: Re: [PHP] Problem with PHP and mounted folder

r

On Fri, Feb 28, 2014 at 1:00 PM, Cliff Nieuwenhuis <***@adcidesign.com> wrote:

Cliff Nieuwenhuis wrote:
[snip]
Post by Cliff Nieuwenhuis
The web application needs to read and create files in the projects folder, but
PHP doesn't recognize the projects folder as folder!  I suspect a
permissions problem of some sort, but I can't figure out where.
  
[snip]
Post by Cliff Nieuwenhuis
You guessed right. PHP usually runs under a user with less permissions (www, user, etc..). You are probably
logged in as a user with permissions to access that mounted drive while PHP isn't. Both fixes are ugly, but
what I would recommend is giving permission to those files as opposed to giving PHP user more power.
Thanks for the reply.

I thought that PHP runs under HTTPD as user 'apache'.  I know that the Apache webserver runs as user 'apache' on my system -- does PHP run as a different user?  Assuming that it does not, wouldn't my successful test of traversing, reading, and writing files under the 'projects' folder by doing 'su - apache -s/bin/bash' mean that the permissions are OK?  I guess not, but I can't see why not.
--
Cliff Nieuwenhuis

You are correct, when I said PHP I meant through the server. Try using runuser command instead. If you have exec enabled, try running an ls command as a user with higher permissions and see if that works.


OK -- tried with runuser. I can see all the projects in the projects folder, and I can edit files that I should be able to edit (those files that can be edited by the Windows user account given in the credentials file that is used by mount.cifs). But I can't descend into the projects folder via PHP.

If I browse to http://myserver/projects I see Apache's listing of all the subfolders and files. If I click on a test text (test.txt) file in that list, I see the text file contents in the browser.

If I write php code like this:

$contents = file_get_contents('projects/test.txt');
echo "Contents: $contents";

..it works; the contents are displayed. BUT is_file('projects/test.txt') wi
Aziz Saleh
2014-02-28 20:50:15 UTC
Permalink
On Fri, Feb 28, 2014 at 3:43 PM, Cliff Nieuwenhuis <
Post by Cliff Nieuwenhuis
Sent: Friday, February 28, 2014 1:41 PM
To: Cliff Nieuwenhuis
Subject: Re: [PHP] Problem with PHP and mounted folder
r
On Fri, Feb 28, 2014 at 1:00 PM, Cliff Nieuwenhuis <
[snip]
Post by Cliff Nieuwenhuis
The web application needs to read and create files in the projects
folder, but
Post by Cliff Nieuwenhuis
PHP doesn't recognize the projects folder as folder! I suspect a
permissions problem of some sort, but I can't figure out where.
[snip]
Post by Cliff Nieuwenhuis
You guessed right. PHP usually runs under a user with less permissions
(www, user, etc..). You are probably
Post by Cliff Nieuwenhuis
logged in as a user with permissions to access that mounted drive while
PHP isn't. Both fixes are ugly, but
Post by Cliff Nieuwenhuis
what I would recommend is giving permission to those files as opposed to
giving PHP user more power.
Thanks for the reply.
I thought that PHP runs under HTTPD as user 'apache'. I know that the
Apache webserver runs as user 'apache' on my system -- does PHP run as a
different user? Assuming that it does not, wouldn't my successful test of
traversing, reading, and writing files under the 'projects' folder by doing
'su - apache -s/bin/bash' mean that the permissions are OK? I guess not,
but I can't see why not.
--
Cliff Nieuwenhuis
You are correct, when I said PHP I meant through the server. Try using
runuser command instead. If you have exec enabled, try running an ls
command as a user with higher permissions and see if that works.
OK -- tried with runuser. I can see all the projects in the projects
folder, and I can edit files that I should be able to edit (those files
that can be edited by the Windows user account given in the credentials
file that is used by mount.cifs). But I can't descend into the projects
folder via PHP.
If I browse to http://myserver/projects I see Apache's listing of all the
subfolders and files. If I click on a test text (test.txt) file in that
list, I see the text file contents in the browser.
$contents = file_get_contents('projects/test.txt');
echo "Contents: $contents";
..it works; the contents are displayed. BUT is_file('projects/test.txt')
will return FALSE.
Does file_exists work? is_file will return false if the user doesn't have
permission on the parent directory.
Aziz Saleh
2014-02-28 21:01:59 UTC
Permalink
Post by Aziz Saleh
On Fri, Feb 28, 2014 at 3:43 PM, Cliff Nieuwenhuis <
Post by Cliff Nieuwenhuis
Sent: Friday, February 28, 2014 1:41 PM
To: Cliff Nieuwenhuis
Subject: Re: [PHP] Problem with PHP and mounted folder
r
On Fri, Feb 28, 2014 at 1:00 PM, Cliff Nieuwenhuis <
[snip]
Post by Cliff Nieuwenhuis
The web application needs to read and create files in the projects
folder, but
Post by Cliff Nieuwenhuis
PHP doesn't recognize the projects folder as folder! I suspect a
permissions problem of some sort, but I can't figure out where.
[snip]
Post by Cliff Nieuwenhuis
You guessed right. PHP usually runs under a user with less permissions
(www, user, etc..). You are probably
Post by Cliff Nieuwenhuis
logged in as a user with permissions to access that mounted drive while
PHP isn't. Both fixes are ugly, but
Post by Cliff Nieuwenhuis
what I would recommend is giving permission to those files as opposed
to giving PHP user more power.
Thanks for the reply.
I thought that PHP runs under HTTPD as user 'apache'. I know that the
Apache webserver runs as user 'apache' on my system -- does PHP run as a
different user? Assuming that it does not, wouldn't my successful test of
traversing, reading, and writing files under the 'projects' folder by doing
'su - apache -s/bin/bash' mean that the permissions are OK? I guess not,
but I can't see why not.
--
Cliff Nieuwenhuis
You are correct, when I said PHP I meant through the server. Try using
runuser command instead. If you have exec enabled, try running an ls
command as a user with higher permissions and see if that works.
OK -- tried with runuser. I can see all the projects in the projects
folder, and I can edit files that I should be able to edit (those files
that can be edited by the Windows user account given in the credentials
file that is used by mount.cifs). But I can't descend into the projects
folder via PHP.
If I browse to http://myserver/projects I see Apache's listing of all
the subfolders and files. If I click on a test text (test.txt) file in
that list, I see the text file contents in the browser.
$contents = file_get_contents('projects/test.txt');
echo "Contents: $contents";
..it works; the contents are displayed. BUT is_file('projects/test.txt')
will return FALSE.
Does file_exists work? is_file will return false if the user doesn't have
permission on the parent directory.
Another option which is a bit faster than file_exists:
stream_resolve_inlcude_path() !== false.
Cliff Nieuwenhuis
2014-02-28 22:12:17 UTC
Permalink
From: Aziz Saleh [mailto:***@gmail.com]
Sent: Friday, February 28, 2014 3:02 PM
To: Cliff Nieuwenhuis
Cc: php-***@lists.php.net
Subject: Re: [PHP] Problem with PHP and mounted folder



On Fri, Feb 28, 2014 at 3:50 PM, Aziz Saleh <***@gmail.com> wrote:


On Fri, Feb 28, 2014 at 3:43 PM, Cliff Nieuwenhuis <***@adcidesign.com> wrote:
From: Aziz Saleh [mailto:***@gmail.com]
Sent: Friday, February 28, 2014 1:41 PM
To: Cliff Nieuwenhuis
Cc: php-***@lists.php.net
Subject: Re: [PHP] Problem with PHP and mounted folder

r

On Fri, Feb 28, 2014 at 1:00 PM, Cliff Nieuwenhuis <***@adcidesign.com> wrote:

Cliff Nieuwenhuis wrote:
[snip]
Post by Cliff Nieuwenhuis
The web application needs to read and create files in the projects folder, but
PHP doesn't recognize the projects folder as folder!  I suspect a
permissions problem of some sort, but I can't figure out where.
  
[snip]
Post by Cliff Nieuwenhuis
You guessed right. PHP usually runs under a user with less permissions (www, user, etc..). You are probably
logged in as a user with permissions to access that mounted drive while PHP isn't. Both fixes are ugly, but
what I would recommend is giving permission to those files as opposed to giving PHP user more power.
Thanks for the reply.

I thought that PHP runs under HTTPD as user 'apache'.  I know that the Apache webserver runs as user 'apache' on my system -- does PHP run as a different user?  Assuming that it does not, wouldn't my successful test of traversing, reading, and writing files under the 'projects' folder by doing 'su - apache -s/bin/bash' mean that the permissions are OK?  I guess not, but I can't see why not.
--
Cliff Nieuwenhuis

You are correct, when I said PHP I meant through the server. Try using runuser command instead. If you have exec enabled, try running an ls command as a user with higher permissions and see if that works.

OK -- tried with runuser.  I can see all the projects in the projects folder, and I can edit files that I should be able to edit (those files that can be edited by the Windows user account given in the credentials file that is used by mount.cifs).  But I can't descend into the projects folder via PHP.

If I browse to http://myserver/projects I see Apache's listing of all the subfolders and files.  If I click on a test text (test.txt) file in that list, I see the text file contents in the browser.

If I write php code like this:

$contents = file_get_contents('projects/test.txt');
echo "Contents: $contents";

..it works; the contents are displayed.  BUT is_file('projects/test.txt') will return FALSE.


Does file_exists work? is_file will return false if the user doesn't have permission on the parent directory.

Another option which is a bit faster than file_exists: stream_resolve_inlcude_path() !== false.


Thanks again for the suggestions. Here's a summary...

is_file('projects/test.txt') -> FALSE

file_exists('projects/test.txt') -> TRUE

var_dump(stream_resolve_include_path('projects')) -> bool(false)

var_dump(stream_resolve_include_pat
Cliff Nieuwenhuis
2014-03-13 21:38:59 UTC
Permalink
On Fri, Feb 28, 2014 at 1:00 PM, Cliff Nieuwenhuis <***@adcidesign.com> wrote:

Cliff Nieuwenhuis wrote:
[snip]
Post by Cliff Nieuwenhuis
The web application needs to read and create files in the projects folder, but
PHP doesn't recognize the projects folder as folder!  I suspect a
permissions problem of some sort, but I can't figure out where.
  
[snip]
Post by Cliff Nieuwenhuis
You guessed right. PHP usually runs under a user with less permissions (www, user, etc..). You are probably
logged in as a user with permissions to access that mounted drive while PHP isn't. Both fixes are ugly, but
what I would recommend is giving permission to those files as opposed to giving PHP user more power.
[snip]
Post by Cliff Nieuwenhuis
Thanks again for the suggestions. Here's a summary...
is_file('projects/test.txt') -> FALSE
file_exists('projects/test.txt') -> TRUE
var_dump(stream_resolve_include_path('projects')) -> bool(false)
var_dump(stream_resolve_include_path('projects/test.txt')) -> bool(false)
Just a follow-up in case anyone has a similar problem. I needed to add the 'noserverino' option to the mount command. Once I did that, the php functions I was using on the mounted folder worked as expected.
Continue reading on narkive:
Loading...