Solved

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

Posted on 2008-10-17
10
1,394 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now