Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-17
10
Medium Priority
?
1,506 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 400 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 1600 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 8

Assisted Solution

by:aldanch
aldanch earned 1600 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 1600 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 1600 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

721 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