Solved

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

Posted on 2008-10-17
10
1,449 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
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

685 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