LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-08-2013, 01:25 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
PHP5 - chown() - Warning: chown(): Operation not permitted


Hi,

I am making a database backup program. The program makes a call as system("mysqldump........"). I then insert a line "CREATE DATABASE IF NOT EXISTS dbase" into the dumped file. In order to do this I write part of the file to a temp file, insert the line and then copy the rest of the file into the temp file. Finally I delete the original file and rename the temp file to the original file name.

This new file has ownership and group www-data. I need to change that so use chown() to change owner but it won't work. Error shown below.

Would appreciate any help solving this'

Thanks, R

Code:
$fptmp=fopen("tempfile", "w+");
 
$fh=fopen($filename, "r+") or die ("Can't open file; Check if exists.");

	 do {
		$str=fgets($fh);                       // copy file contents to tmp
		fwrite($fptmp, $str);                  // until ..
	} while(! strpos($str, "Table structure")); // find words "Table structure"
	
		fwrite($fptmp, "--\n");                // skip line
		fwrite($fptmp, $createdb);             // Insert "CREATE ..."
		fwrite($fptmp, "--\n");                // skip line
		fgets($fh);
	 while(! feof($fh)) {                       // copy rest of file to tmp
		$str=fgets($fh);		              // skipping line w/ "Table Str..
		fwrite($fptmp, $str);
	}
fclose($fh);                                     // close the files
fclose($fptmp);

unlink($filename);                               // delete source file  
rename("tempfile", $filename);                   // rename temp file to org name

chown($filename, "rick");                        // change owner from www-data
Recieve following error warning:

Warning: chown(): Operation not permitted in /home/rick/DB-Web/testonly/DbaseBakup.php on line 70
 
Old 09-09-2013, 03:09 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,875
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
> This new file has ownership and group www-data.

True.

> I need to change that so use chown() to change owner

Why should you change owner? Your PHP is running as www-data, so it can handle files owned by user www-data.

> but it won't work.

Of course. Only root can change file-ownership.

PS: database exports/imports should be performed from shell-access (ssh), not via Apache+PHP

Last edited by NevemTeve; 09-09-2013 at 03:11 AM.
 
Old 09-09-2013, 04:55 AM   #3
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
You could run the script from the command line and invoke it as a cron job. Then it is run by root who may use the chown command successfully (if it is not disabled in php.ini)
You need the cli extension of php for this.
 
Old 09-09-2013, 05:34 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by j-ray View Post
invoke it as a cron job. Then it is run by root
This is a terrible idea. Running anything automated as root is a terrible idea. Running automated PHP script as root is even worse idea.

If two different users need to access the same data, the best way to do it, is put them in the same group, and change group of the files. Furthermore, one might set setgid bit on the containing directory in which case all the files inside of it will automatically inherit the group of the directory.
 
Old 09-09-2013, 06:53 AM   #5
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
Quote:
Running anything automated as root is a terrible idea.
So cron jobs in general are to be abolished? Stupid idea I think.
 
Old 09-09-2013, 06:58 AM   #6
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by j-ray View Post
So cron jobs in general are to be abolished? Stupid idea I think.
Cron jobs do not have to be run as root.
 
Old 09-09-2013, 07:35 AM   #7
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
@mina86: Billions of cron jobs are run as root every day as crond does it by default as far as I know. What's so terrible about that?

Anyway you can let it be run by someoneelse if you prefer. But generally spoken, invocation from command line is the better choice as NevemTeve already mentioned. Is that possible for you or don't you have any shell access to that machine?
 
Old 09-09-2013, 11:35 AM   #8
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks for the replies guys/girls,

I am doing this backup from inside my database program . "Backup" is just part of the options in that program. Actually I would prefer php writing the file to owner:group rick:rick instead of www-data:www.data. Is there any way to do that.

I did try using a line, in lieu of: "chown($filename, "rick;, system(sudo(chown($filename, "rick)));. Just hangs the machine.

NevemTeve;

> PS: database exports/imports should be performed from shell-access (ssh),

How would I do this. Can it be done inside PHP as a system call?

Thanks,
R
 
Old 09-09-2013, 01:09 PM   #9
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by j-ray View Post
@mina86: Billions of cron jobs are run as root every day as crond does it by default as far as I know. What's so terrible about that?
Crond does not do it “by default”. It runs the jobs as whoever the cron jobs were set up to run as. If they are installed by user, they are run by that given user. Jobs in “/etc/crontab” have additional field where user is specified explicitly.

What's wrong about it is that any process run as root is an attack vector, and “run as root” should be the least resort after all other options are exhausted.

Quote:
Originally Posted by pizzipie View Post
I would prefer php writing the file to owner:group rick:rick instead of www-data:www-data. Is there any way to do that.
Like I've described, you can make it write the files as “www-data:rick” with little problems. All you have to do is create a directory where the files are to be saved, set it's ownership to “www-data:rick”, and set setgid bit on it:
Code:
mkdir /some/path
chown www-data:rick /some/path
chmon 2770 /some/path
This way, whenever a file is created in that directory, it will inherit the “rick” group.

Quote:
Originally Posted by pizzipie View Post
system(sudo(chown($filename, "rick")));
This does not even look like a valid PHP to me. Or at least not one that will do what you want. If you really want to go that way (which I advice against to be honest, at least before you look at other options), you can do something like that:
Code:
system('sudo chown rick:rick -- ' . escapeshellarg($filename))
and then configure sudo (see “/ete/sudoers”) so that www-data can run this command without password.

Quote:
Originally Posted by pizzipie View Post
How would I do this. Can it be done inside PHP as a system call?
It depends what database are we talking about and what exactly the “Backup” function of the script you're using is doing. If you just have a MySQL database, all you need to do is set invocation of “mysql_dump” (I may misremember the name) every day (or whatever). If you Google a little, you'll find scripts which can automatically do the backup and then mail it to you.

Setting this up, however, requires that you have shell access to the server, or at least possibility of setting up cron jobs via your hosting provider's web interface. However, in the latter, you may be limited to what exactly you can run in those cron jobs.

Last edited by mina86; 09-10-2013 at 07:40 AM.
 
Old 09-09-2013, 03:23 PM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
The web-server process should never have elevated privileges, let alone "rootly" ones. It can initiate a request to a separate process which does have super-powers, but the requester (i.e. "the web page") should have no influence over the process that it launches.
 
Old 09-10-2013, 01:47 AM   #11
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
Quote:
How would I do this. Can it be done inside PHP as a system call?
You have to install the cli extension of php then you can run php scripts like a shell or perl script from the command line.
 
Old 09-10-2013, 03:53 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,875
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Well, first ask your server-provider if you have or not shell access, and if you have, how to use it (well, you have to know what ssh, bash, mc, mysqldump, etc are).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
chown: changing ownership Operation not permitted cdhar Linux - Software 4 07-29-2012 11:05 PM
chown: Operation not permitted ocicat Linux - General 4 05-08-2012 06:01 AM
chown: Operation not permitted presarioch Linux - Newbie 13 06-22-2008 03:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration