FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Fix for deleting files/directories owned by apache





n0xvb
Read all of this and understand what is happening before you do any of it, I won't be responsible for what you do to your site!!! Wink
Here is a simple php that you can use to delete those files and directories that are owned by UID apache and GID apache (these are created through the normal operation of phpBB for example.):
First, create a file (I called this one fixmod.php) in your public_html directory:
Code:

<?php

system('./fixmod');

print "\n\nComplete!";
?>


Next create a file also in the public_html directory (I used the name fixmod with no file extension) with this code:
Code:

#!/bin/sh
#
find . -type d -exec chmod 777 {} \; 2>&1
find . -type f -exec chmod 777 {} \; 2>&1
#

Whatever you call this second file, will need to be put into the php script in the first block of code.
Next in DirectAdmin, change the permissions of your public_html directory to 777. Also change the permissions of the fixmod file to 777.
Point your webbrowser to your subdomain and load the php file.
For my subdomain n0xvb, I use this url:
http://n0xvb.frih.net/fixmod.php
You will receive some errors about the operation not being permitted, this is because apache will not have write access to the files/directories that your user profile owns.
After it finishes, change the permissions of your public_html folder back to 755.
You can now use DirectAdmin to verify that the files and directories owned by apache have 777 permissions, if so then delete the unwanted folders/files! Woo Hoo!

I have not tested this, but the same may be able to be accomplished in one fixmod.php file like so:
Code:

<?php

system('find . -type d -exec chmod 777 {} \; 2>&1');
system('find . -type f -exec chmod 777 {} \; 2>&1');

print "\n\nComplete!";
?>


Here are a couple risks, I'm sure there's more:
  • After you set the public_html permissions to 777 ANYONE (and I do mean anyone!) has full access to your entire public web space. That being said, you should be able to run the fixmod.php script using these instructions fairly quickly and the risk is really quite low.
  • Through its normal use, phpBB may create some files/directories that will not have 777 permissions but will be owned by apache:apache. You will need to be careful because these directories & files that you want to keep will have their permissions changed as well! You may need to go back and set them to their proper permissions after you're through.


An explanation of what the code does:
The first file, fixmod.php, simply calls the second program fixmod which is a shell script. The shell script uses the find command to recursively enter any directories it has access to and does a chmod on each directory it finds. It then does the same thing for all files, again recursively through all the directories it has access to. The second file must be set 777 so that it is executable, the public_html directory must be set to 777 so that the apache user has access to make any changes. If either of these permissions are not set the scripts will not work.

One last thought, what I wanted to do was a chown instead of a chmod in the find commands, but apache does not have authority to change ownership to someone else.

Cheers!
Jay

MODS, feel free to put this into the scripting forum if you find it is more appropriate.
Bondings
Thank you for the tutorial, it will be quite useful for the people who encounter this problem.

This problem (uid and gid apache instead of your own) is most of the time caused by a 'badly-written' script.

The only thing you are able to do is to change are the permissions of the files, you can't change the gid/uid back to what it was. This means that this is only a way to delete the files. Once the gid/uid is apache, you can't change it yourself anymore.

So, just copy the files in question and create them back with ftp/file manager and then they should have the right gid/uid.
n0xvb
Bondings wrote:
Thank you for the tutorial, it will be quite useful for the people who encounter this problem.

This problem (uid and gid apache instead of your own) is most of the time caused by a 'badly-written' script.

The only thing you are able to do is to change are the permissions of the files, you can't change the gid/uid back to what it was. This means that this is only a way to delete the files. Once the gid/uid is apache, you can't change it yourself anymore.

So, just copy the files in question and create them back with ftp/file manager and then they should have the right gid/uid.


And yes, this is a perfect solution after running the above scripts. Because they have 777 permissions, they can be downloaded, deleted and uploaded again under my user ID. (I did just that Very Happy)
n0xvb
I wanted to share a little more information on the apache:apache ownership issue.

The problem is caused purely due to the fact that php scripts are server side scripts and being run by the apache server. It follows that the process is owned by the user apache (because the server is running under this user id). If you are running phpBB or nuke or SMF or other flavor of forum, you'll remember that part of the install process includes setting directories to 777 permissions. This is to allow the apache user to create files and/or directories in places where they are owned by your individual user id. Through the normal operation of your forum, if any files or directories are created, the script is actually run by the apache user and that is why they're owned by apache.

Some good news: this method of allowing you to delete these apache objects does not need to be done solely in your public_html directory. You can also place the scripts into public_html/phpBB2/images (or whatever directory you want) and execute them. This will only affect objects in the current directory and subdirectories, not your entire site!
Daniel15
I was thinking, couldn't FriHost install something like suEXEC on the server? suEXEC runs scripts as the owner of the script, instead of Apache. I believe there's a version that allows you to do this for PHP scripts as well.

EDIT: I think it's called suPHP
n0xvb
daniel15 wrote:


EDIT: I think it's called suPHP


Yep, that's it.. Interesting package! I wonder if Bondings would be willing to give it a go. I'm not sure, however, how helpful it would be unless the forum software developers write their code to use it.

The package is here: http://www.suphp.org/Home.html
evilryu530
i tried this turotial because i have the apache problem. its not working for me. check it out

www.design.frih.net
n0xvb
evilryu530 wrote:
i tried this turotial because i have the apache problem. its not working for me. check it out

www.design.frih.net


When you run the script does the browser have a message that says Complete! or a different message?
djclue917
Oh I see. So we could really execute shell scripts using PHP. Hmmm.
Thanks by the way!
n0obie4life
Sure, abuse it and we'll have your IP banned.
Jovan
Thx, i'm very happy
djclue917
Bondings wrote:
Thank you for the tutorial, it will be quite useful for the people who encounter this problem.

This problem (uid and gid apache instead of your own) is most of the time caused by a 'badly-written' script.

The only thing you are able to do is to change are the permissions of the files, you can't change the gid/uid back to what it was. This means that this is only a way to delete the files. Once the gid/uid is apache, you can't change it yourself anymore.

So, just copy the files in question and create them back with ftp/file manager and then they should have the right gid/uid.


Since we don't have direct shell access, we could execute shell scripts or commands via PHP or Perl. I just want to know what commands are allowed to be executed. I am ("almost") a full-time Linux user that is why I love to use (Linux) commands. Please issue a list of available commands. Thanks.
djclue917
n0obie4life wrote:
Sure, abuse it and we'll have your IP banned.


First of all, I am not that kind of person who likes to abuse anything. I am not also stupid to try to do crazy things like executing dangerous scripts. Thanks for the warning anyway.

I just don't get what you meant by getting my IP banned. Or are you talking about my account?

Peace. Smile
dac_nip
thanks. I was thinking of using SSH to delete the files, but i don't think it will be allowed here, maybe due to security reasons. so we can execute shell scripts anyway. remarkable!
The_Gamer294
Ohhh man this is a godsend! Thanks much, I've been having this problem forever.
Daniel15
I just though I'd say that this problem will no longer happen on server 1, due to PHP Suexec being enabled. So, all the files that used to be created under the 'nobody' user will now be correctly owned by you.
The_Gamer294
how come it isnt enabled on server 2?
deneb.star
I modified my public_html to 777
I uploaded the php + shell and the php
I tried both technics and with the php script I get the following error :

chmod: changing permissions of `.': Operation not permitted chmod: changing permissions of `./administrator': Operation not permitted chmod: changing permissions of `./administrator/components': Operation not permitted chmod: changing permissions of `./components': Operation not permitted chmod: changing permissions of `./templates': Operation not permitted chmod: changing permissions of `./modules': Operation not permitted chmod: changing permissions of `./fixmod': Operation not permitted chmod: changing permissions of `./fixmod.php': Operation not permitted chmod: changing permissions of `./fixmodphp.php': Operation not permitted Complete!

Any suggetsion ?
deneb.star
I found the reason

some directories were belonging to my user and so the script could not modify them

I had to play with that script among various directories but I got it working in the end

thanks for the post
swizzy
Is is true that files with GID/UID apache do not affect the allocated disk space?? i noticed so.. or is the disk space refresh time slow?
alwom
i deleted joomla and some files just refused to go, i used this script and it worked perfectly, i saved the fixmod.php and fixmod files in my home directory so if i have a prob agan all i have to do is point my brownser to it. lol thank you so much.
SpellcasterDX
alwom wrote:
i deleted joomla and some files just refused to go, i used this script and it worked perfectly, i saved the fixmod.php and fixmod files in my home directory so if i have a prob agan all i have to do is point my brownser to it. lol thank you so much.

I hope those are CHMODedded to 0 so nowbody can execute them.
alwom
ill take care of that right now, i didnt know i had to to that, i apologize
k10000s
I tried this method and I got a massage "Complete!". However, the folder owned by apache still has permition 706. So, I cannot delete the folder.

Can anyone tell me what I did wrong or other method?
Animal
k10000s wrote:
I tried this method and I got a massage "Complete!". However, the folder owned by apache still has permition 706. So, I cannot delete the folder.

Can anyone tell me what I did wrong or other method?

I found that I had the same problem using the dual-file method. Did you try using the single "fixmod.php" file containing the following?
Code:

<?php

system('find . -type d -exec chmod 777 {} \; 2>&1');
system('find . -type f -exec chmod 777 {} \; 2>&1');

print "\n\nComplete!";
?>

This worked for me, where the method using two files did not. Also, did you CHMOD your public_html directory to 777 before you started?
Daniel15
swizzy wrote:
Is is true that files with GID/UID apache do not affect the allocated disk space?? i noticed so.. or is the disk space refresh time slow?

That's a flaw rather than anything else. The quotas are calculated using the Quota utilities in Linux, which adds up the total amount of space for all files owned by you, on the whole hard drive. Since the user nobody is not you, the files don't count towards your quota.
There's a script on WHM/cPanel which will go through all the files in /home and CHOWN all of them to the correct owner, but I'm not sure if something exists like that for DirectAdmin.
k10000s
Animal wrote:
k10000s wrote:
I tried this method and I got a massage "Complete!". However, the folder owned by apache still has permition 706. So, I cannot delete the folder.

Can anyone tell me what I did wrong or other method?

I found that I had the same problem using the dual-file method. Did you try using the single "fixmod.php" file containing the following?
Code:

<?php

system('find . -type d -exec chmod 777 {} \; 2>&1');
system('find . -type f -exec chmod 777 {} \; 2>&1');

print "\n\nComplete!";
?>

This worked for me, where the method using two files did not. Also, did you CHMOD your public_html directory to 777 before you started?


Thank you~. This works. Finally, I deleted the folder~~!
theem
Thanks man !!!
You have solved a big issue for me
i was about to go mad about this problem
thanks frihost.com forums....

Laughing Laughing Laughing Laughing
cr3ativ3
Bondings wrote:

This problem (uid and gid apache instead of your own) is most of the time caused by a 'badly-written' script.


I was wondering if you had any examples of these bad written scripts vs. a good written one.

Was just wondering so that I could modify my scripts so that this won't happen in the future.

Thanks

P.S.
Thanks for the tutorial the single file version worked perfect Very Happy
mjohnson
Wow, that is so magic.you are great man.I resolved the problem finally.
thank you very much.
Bondings
The fix will probably stop working now due to security measures. Luckily DirectAdmin now has a feature in the file manager that changes the owner of a file from apache to you, so that shouldn't be a problem anymore.
ronaldallan.mojica
Hello everyone lately i receive this free host from frihost and i want to install typo3 on my folder but the problem is that directadmin has no fantastico that's why i look for alternate solution on my problem... luckily i found a software name net2ftp.com and then i try to install typo3 and it works thanks goodness... ok now, the problem is that the installer from net2ftp created temp files to install it to your hosting my problem is that i cant delete those temp folders and later i found that it was owned by user apache...

i try to reset each folder but it was so hard because you will reset each file just to change the owner....
thanks for this it will now free my space... tnx again...
ronaldallan.mojica
I tried this script but its not working this is the error message:

Quote:

Warning: system() has been disabled for security reasons in /home/ronaldal/domains/rmojica.frih.net/public_html/fixmod.php on line 3

Warning: system() has been disabled for security reasons in /home/ronaldal/domains/rmojica.frih.net/public_html/fixmod.php on line 4
Complete!



please help me...
ronaldallan.mojica
can i request to enable system() for me? or even exec() ... or maybe the admin can make a fix for resetting the ownership recursively... because i had to do it in every single file... i can;t delete those files....
Daniel15
ronaldallan.mojica wrote:
can i request to enable system() for me? or even exec() ... or maybe the admin can make a fix for resetting the ownership recursively... because i had to do it in every single file... i can;t delete those files....

No, unfortunately, functions can only be enabled or disabled on a server-wide basis. They are disabled now for security reasons.
I guess you could ask one of the admins to change the ownership of the files for you.
SoutlinK
Hello guys, Whats sup?

Thanks for this script n0xvb.

But I have a question yet. These is.

If I put the script in, for example, public_html/OMG/OMG2/, the script will only take effect to the files that are in public_html/OMG/OMG2/ or the files in public_html/OMG/OMG1/ will take efect?

I dont know if you understand what i mean (i know my english sux xD) but is simple. What happend with the files in public_html/OMG/OMG1/ if I run the script in public_html/OMG/OMG2/

Thanks
rvec
Bondings wrote:
The fix will probably stop working now due to security measures. Luckily DirectAdmin now has a feature in the file manager that changes the owner of a file from apache to you, so that shouldn't be a problem anymore.


So the script won't work and you have to find another way to do the same thing. I am trying to figure out how to do it in direct-admin now.

edit:
Go to directadmin, open the file-manager and go to the folder you want to change the owner of.

click on "Reset Ownership" and a "File ownership reset" message should appear.
Bockman
rvec wrote:

Go to directadmin, open the file-manager and go to the folder you want to change the owner of.

click on "Reset Ownership" and a "File ownership reset" message should appear.



Or choose the option Recursively to reset the folder and all subfolders and files. This way you won't need to manually reset every file and folder.

Be Well Cool
rvec
lol I was wondering what the difference was between those two Razz
Ghost900
I have reclaimed all my files back but the main "public_html" file will not change, when I click "Reset Ownership" it said success but then it stays the same, I have refreshed it and loged out and back in and it still is the same, I even retried it a few times, is there something I can do or can "public_html" not be changed?

Thanks.

It doesn't cause any problems as far as uploading the website but it won't register my websites as using space and I just want it to work right if I can.
mk12327
I suspect i am having the same problem as well on my account. I'm using Server 1. Is there a way i can check the file ownerships of my site? If i do not have access, can any administrator help me change the file ownerships?

EDIT: I just tried the script and apparently it worked. It's great since it solves my problem, but what i am curious is that Bondings mentioned that it has already been disabled on Server 1. I did try the direct command of "chmod()" from php which does not work. Any explanations?
sktanmoy
If run this script, file permission will be set to 777. Is it possible to set it 644 or 666 while the script will be run?
todmordenfred
At last an answer to my problem..

Mine was cause because my other server does not have the option to upload and unpack a zip file. I had to create a php script that would unpack a predefined zip file that was uploaded once run.
Only problem with that is all the files are now owned by apache.. ooops.
Back to the drawing board with that one..
todmordenfred
Hmm, I receive the error
Warning: system() has been disabled for security reasons in /var/www/vhosts/bluelightuk.co.uk/httpdocs/myfolder/fixmod.php on line 3
Complete!

I think that is because safemode is one, but I have no control over that. Tried both single and dual file methods, result is the same.
Also only wanted files and folder from myfolder onwards, so put the files in that folder, not my httpdocs folder.
yosefa
All, what the php version in this hosting?
Related topics
Reply to topic    Frihost Forum Index -> Support and Web Hosting -> Web Hosting Support

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.