• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 999
  • Last Modified:

php & chmod

Linux OS
Is there anyone interested in doing the following for 500 points?
1) clean up the code below
2) tell me what I should be looking for, "chmod" doesn't want to work, the files and directories involved are owned by user running the script, I tried to "chown" the script to "root" without success,    ---   echo $file,"<br>";  works and gives the correct output

<?
$filename="dire.txt";
$files=array();
$files=file("$filename");

foreach($files as $file)
{
$file="/home/meeting/chair" . $file;
echo $file,"<br>";
chmod($file, 0755);  // octal; correct value of mode
}

echo "last modified ".date('d F Y, H:i:s.',getlastmod());
?>

Thank you for your help
0
rblampain
Asked:
rblampain
  • 11
  • 5
  • 5
  • +1
3 Solutions
 
keteracelCommented:
just looks to me like you've missed the forward slash off the file, i.e.:

<?
$filename="dire.txt";
$files=file("$filename");

foreach($files as $file)
{
$file="/home/meeting/chair/" . $file;
echo $file,"<br>";
chmod($file, 0755);  // octal; correct value of mode
}

echo "last modified ".date('d F Y, H:i:s.',getlastmod());
?>

This works fine for me using PHP 4.3.10 on Linux,

keteracel
0
 
rblampainAuthor Commented:
Thanks to keteracel
No, that's not the problem, the file "dire.txt" contains filenames starting with a couple of directories like:
/dir1/dir2/filename
Just to check it, I  issued the command "emacs " followed by a "cut & paste" of one line of the output created by the 'echo $file,"<br>"; ' line and this editor gets the correct file.
0
 
Marcus BointonCommented:
Does the user running the script have write access to the directory the items are in? Have you tried 'chmod 777 .' to test it?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
keteracelCommented:
try to use php to write to a file. If it can't do that then you're not going to be able to change permissions... try:

<?

$fp = fopen ("test.txt", "w");
fwrite($fp, "test");
fclose($fp);

?>

If this file is not created then your permissions are messed up!

0
 
rblampainAuthor Commented:
To Squinky:
 yes user had write access to directory holding the files, it was 755, I made it 777 but no difference, see below!

To keteracel:
the file is not created but I can't figure out where the permissions are incorrect.
The script is run from
 /home/meeting/chair
and if affects files held in
/home/meeting/chair/test/a0
I made the directories "test" and "test/a0" 777 but it doesn't help.  

Any other idea?


0
 
ZhaawZSoftware DeveloperCommented:
Enabled safemode (for php) can be a matter why chmod doesn't want to work. It can be also a problem to access files that have different user (even if chmod allows it), when safemode is enabled.
0
 
rblampainAuthor Commented:
To ZhaawZ:
/etc/php.ini says "safe mode = off"
0
 
keteracelCommented:
we can try a nasty workaround using ftp!

<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

$filename="dire.txt";
$files=file("$filename");

foreach($files as $file)
{
  $file="/home/meeting/chair" . $file;
  echo $file,"<br>";

  // try to chmod $file to 755
  if (ftp_chmod($conn_id, 0755, $file) !== false) {
    echo "last modified ".date('d F Y, H:i:s.',getlastmod());
  } else {
    echo "could not chmod $file\n";
  }
}

// close the connection
ftp_close($conn_id);
?>
0
 
keteracelCommented:
the above should work (if you have PHP set up with the ftp module). What's probably happening is that PHP (or maybe Apache) is being run as nobody so PHP has no privileges. Using ftp, you can use a user with appropriate privileges as long as you enable your ftp service in Linux.
0
 
rblampainAuthor Commented:
Thanks to keteracel
I have "ftp" disabled and i'd like to exhaust all other options before I try this solution although it's good to know that something should work.
I've changed the Apache configuration file to reflect this user and its group but it makes no difference.
I had a look at "/etc/php.ini" but I can't find anything obvious that could help although I might simply miss it because I don't know.
I also had a look at "/etc/httpd/conf.d/php.conf" but this is only a couple of unrelated lines.
As far as permissions, PHP is the only suspect left but my understanding is that it will run under the permissions of user "chair" which has
permissions 777 over the directories below, or is there another file to configure?
After all the changes I rebooted the machine and having no success I restarted "httpd" (apache) but the result is always the same. I'll try to make the current directory 777 and see if it makes any difference.
0
 
rblampainAuthor Commented:
This was already 777
0
 
rblampainAuthor Commented:
To keteracel:
I tried  your ftp script, I thought it was going to tell me something to the effect that FTP was disabled but it looks like the script got half way,  it printed the first filename on the screen and then froze.
0
 
Marcus BointonCommented:
Check that your user change in apache actually happened. You shouldn't have to change something so drastic just to fix this.

If you're running PHP via mod_php (the usual arrangement), PHP will be running as the web server's user. If you're running PHP as a CGI, it will be running as some other user.

You can see all these settings in the output from phpinfo();
0
 
keteracelCommented:
ok, let's try a little more debugging:

<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);

if (!$conn_id) die("Unable to connect to ftpserver");

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

if (!$login_result) die("Login Failed...");

$filename="dire.txt";
$files=file("$filename");

foreach($files as $file)
{
  $file="/home/meeting/chair" . $file;
  echo $file,"<br>";

  // try to chmod $file to 755
  if (ftp_chmod($conn_id, 0755, $file) !== false) {
    echo "last modified ".date('d F Y, H:i:s.',getlastmod());
  } else {
    echo "could not chmod $file\n";
  }
}

// close the connection
ftp_close($conn_id);
?>
0
 
rblampainAuthor Commented:
I had ftp disabled at installation time and of course I get the message "Unable to connect to ftpserver".  I could not find out how to enable it. Is this easy?
0
 
Marcus BointonCommented:
Although FTP might let you work around this, it's a really ugly hack. It should be easier to just sort out ownership and permissions correctly.

Can you do a phpinfo() and post the bits we've been talking about? (not the whole thing!).
0
 
rblampainAuthor Commented:
To Squinky:
It's PHP HTTPD (Apache) installation
I had restored the original parameters in "httpd.conf" after I found out the change made no difference.

From phpinfo()
User/Group       apache(48)/48

From ls -l
drwxrwxrwx  37 chair meetings   4096 Mar 19 14:59 chair
drwxrwxrwx  69 chair meetings   4096 Feb 27 23:45 test
drwxrwxrwx   2 chair meetings   4096 Feb 27 23:45 a0
0
 
Marcus BointonCommented:
You're not running apache chrooted are you? If so, apache probably can't see /home/meeting/chair, and may be looking in /var/www/home/meeting/chair instead (if it's chrooted in /var/www, like it is on OpenBSD).

You could try making apache a member of the meeting group to avoid making things world writable. One other thing - Don't set execute bits on files unnecessarily. Unless you're actually running PHP scripts from a CLI using #!, you don't need it - use 666 instead of 777 on files. Directories do need it if you're going to look in them.

You need to persevere with Keteracel's file creation test. It's a somewhat more obvious way of making sure you have appropriate access. Also check the return values to see if you get any errors reported.
0
 
rblampainAuthor Commented:
Thanks to Squinky
I'll follow your suggestion about making apache a member of meetings.
I found I probably had the $_SESSION() in the wrong place. I thought it had to be first in the PHP script but it looks like it's got to be first - full stop. My scripts seem to go a bit better since I made this change ($_SESSION before PHP and HTML)

Nothing 'chroot'ed
Execute bits not set.
Apache gives following error message:
[client 192.168.1.13] PHP Warning:  chmod(): Operation not permitted in /home/meeting/chair/gedit_file.php on line 25, referer: http://192.168.1.13/display_tree.php
0
 
Marcus BointonCommented:
OK, firstly it's not clear what you're saying about $_SESSION - $_SESSION() makes no sense - it's a variable not a function. You have to say session_start() before you can use $_SESSION, so it's all within PHP, certainly not before it.

That error states that the script that was running was in /home/meetings/chair - the file that you're changing may not be in there. I suggest you print the full path to the file you're attempting to chmod (use realpath() to make sure it's right)
0
 
rblampainAuthor Commented:
I apologize about my lack of clarity, yes it's session_start().
Files to be chmod are in a number of directories below ~test/
/home/meeting/chair/test/a0
0
 
rblampainAuthor Commented:
It looks like the culprint was SELinux. It's off by default on Fedora C2 I'm using but disabling it seems to fix the problem. Even though you didn't provide the answer, your help was really ... helpful in narrowing the cause. I'll allocate the points according to your involvment and
I apologize because I really thought I was asking something simple from you all.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 11
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now