Solved

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

Posted on 2008-10-17
10
1,426 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Fine Tune your automatic Updates for Ubuntu / Debian
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

810 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