Solved

Use PHP/shell_exec() to extract tar as specific user

Posted on 2008-10-17
10
1,477 Views
Last Modified: 2012-06-21
Hello,

I would like to extract a tar using shell_exec.

I run a Linux server.

<?php  shell_exec('tar xf tar.tar'); ?>

The problem is that when I do this the ownership of all files gets set to nobody.
I want the owner to be me.

My username is:
user123

My password is:
password123

I was told I could do this using sudo / su but I have no ida how to do this.
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 1

Assisted Solution

by:cristides
cristides earned 100 total points
ID: 22744115
You can't do that because the script running under apache user, this is the reason why the files have the owner nobody after unpacked
<?php  shell_exec('tar xf tar.tar'); ?>

If you want do that you can try :
shell_exec("cd destination_directory; gunzip {$filename}| tar xf -;");
0
 
LVL 8

Accepted Solution

by:
aldanch earned 400 total points
ID: 22744127
Can you not shell_execute a "chown -R user123:user123 directory" after you extract it?
0
 
LVL 16

Author Comment

by:hankknight
ID: 22744496
None of your ideas work.

I cannot shell_execute a chown because shell exec is user nobody not root
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Assisted Solution

by:aldanch
aldanch earned 400 total points
ID: 22744641
Probably not a very secure way of doing this but maybe this idea will help...

create a sudo user, modify you sudoers file to include this use. (You could add 'nobody' to sudo users but that would be very dangerous)

Then do something like:

exec("sudo -u sudouser chown -R user:user dir");
0
 
LVL 1

Expert Comment

by:djashley
ID: 22746325
You can create a folder with group sticky bit on . so whenever you extract files in that group the owner is always the group and you can add yourself to that group
0
 
LVL 16

Author Comment

by:hankknight
ID: 22752217
aldanch wrote:
->>exec("sudo -u sudouser chown -R user:user dir");

How do I tell sudo my password?
0
 
LVL 8

Assisted Solution

by:aldanch
aldanch earned 400 total points
ID: 22753407
I am not sure which Linux distribution you are using but on Ubuntu you would want to edit the /etc/sudoers file.

You may see something along the lines of:

root ALL=(ALL) ALL

You would want to add something like the following:

mysudouser ALL=(ALL) ALL


Where "mysudouser" is the user you created to perform the action. You will not need a password once you do this.
0
 
LVL 8

Expert Comment

by:aldanch
ID: 22753411
Forgot to add the man (5) page for the sudoers file.

http://www.sudo.ws/sudo/man/sudoers.html

This will tell you exactly how it works.
0
 
LVL 16

Author Comment

by:hankknight
ID: 22756131
I don't have root access to this server.  That is why I am trying to do Linux command lines using shell_exec instead of SSH.  Is there no way that I can log in through the command line with a username / password?
0
 
LVL 8

Assisted Solution

by:aldanch
aldanch earned 400 total points
ID: 22759225
That depends on your host. Do they allow shell access? If so, then yes :-)

The original question stated that you ran a Linux server.
0

Featured Post

A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question