473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

how to get a script to run as root


I'm writing a script that will find every newly updated file in /var/
www/vhosts/cyber.com/httpdocs/

and then cp the the files over to /var/www/vhosts/theroad.com/
httpdocs/

I used to do this by ssh to the server and typing in the copy command
manually. But my client would like to be able to control the timing
of these updates, so I'm trying to make it an easy-to-run script.

Only thing is, when I've done this copy in the past, I've always been
root, as no other user has the permission to copy from the one
directory to the other. So I need the script to run as root. Can I use
exec() to use su to become root? Anyone have a working example of
that?


Feb 26 '08 #1
7 2257
lawrence k wrote:
I'm writing a script that will find every newly updated file in /var/
www/vhosts/cyber.com/httpdocs/

and then cp the the files over to /var/www/vhosts/theroad.com/
httpdocs/

I used to do this by ssh to the server and typing in the copy command
manually. But my client would like to be able to control the timing
of these updates, so I'm trying to make it an easy-to-run script.

Only thing is, when I've done this copy in the past, I've always been
root, as no other user has the permission to copy from the one
directory to the other. So I need the script to run as root. Can I use
exec() to use su to become root? Anyone have a working example of
that?


Much better to set the proper permissions on the file system. Giving a
script root access is a huge security hole. Unless you are VERY
CAREFUL, some hacker could wipe out your entire server with one command.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 26 '08 #2
lawrence k wrote:
Only thing is, when I've done this copy in the past, I've always been
root, as no other user has the permission to copy from the one directory
to the other. So I need the script to run as root. Can I use exec() to
use su to become root? Anyone have a working example of that?
Google: sudo

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 28 days, 5 min.]

Bottled Water
http://tobyinkster.co.uk/blog/2008/02/18/bottled-water/
Feb 26 '08 #3
On 26 Feb, 17:16, Jerry Stuckle <jstuck...@attglobal.netwrote:
lawrence k wrote:
I'm writing a script that will find every newly updated file in /var/
www/vhosts/cyber.com/httpdocs/
and then cp the the files over to /var/www/vhosts/theroad.com/
httpdocs/
<snip>
Only thing is, when I've done this copy in the past, I've always been
root, as no other user has the permission to copy from the one
directory to the other. So I need the script to run as root. Can I use
exec() to use su to become root? Anyone have a working example of
that?

Much better to set the proper permissions on the file system. Giving a
script root access is a huge security hole. Unless you are VERY
CAREFUL, some hacker could wipe out your entire server with one command.
Agreed - if you can't do it as a normal user then you've got your
permissions model in the first place. Fix it.

Also - WTF are you using PHP to do this? Rsync does it without writing
any code?

C.
Feb 26 '08 #4
lawrence k wrote:
I'm writing a script that will find every newly updated file in /var/
www/vhosts/cyber.com/httpdocs/

and then cp the the files over to /var/www/vhosts/theroad.com/
httpdocs/

I used to do this by ssh to the server and typing in the copy command
manually. But my client would like to be able to control the timing
of these updates, so I'm trying to make it an easy-to-run script.

Only thing is, when I've done this copy in the past, I've always been
root, as no other user has the permission to copy from the one
directory to the other. So I need the script to run as root. Can I use
exec() to use su to become root? Anyone have a working example of
that?
rsync -auv /var/www/vhosts/cyber.com/httpdocs/*
/var/www/vhosts/theroad.com/httpdocs/

Either:
1) Give write access to the user that's doing the update. Add them to
the group and allow group write on those files. Or,
2) Allow the user to run rsync as a user that does have these privileges
(but not root, unless you're sick of having clients). man sudo, man sudoers

Also, stop being root all the time or you're going to get hosed, sooner
or later. Pretty much any time you find yourself thinking "I need the
script to run as root", you're doing it wrong.

Jeremy
Feb 26 '08 #5
lawrence k wrote:
I'm writing a script that will find every newly updated file in /var/
www/vhosts/cyber.com/httpdocs/

and then cp the the files over to /var/www/vhosts/theroad.com/
httpdocs/

I used to do this by ssh to the server and typing in the copy command
manually. But my client would like to be able to control the timing
of these updates, so I'm trying to make it an easy-to-run script.

Only thing is, when I've done this copy in the past, I've always been
root, as no other user has the permission to copy from the one
directory to the other. So I need the script to run as root. Can I use
exec() to use su to become root? Anyone have a working example of
that?

Its been a long time since I did stuff like this..I am going to suggest
a completely different approach.

write a teeny C program that does exactly what you want and no more, and
invoke setuid() within it. I,e,. do NOT wrote a setuid version of
cp...write a setuid program that ONLY works from a specific directory to
another specific directory etc etc.

Then if it has root permissions and IIR the sticky bit set it can be
called by any user process to do its 'one and only dangerous root
permissions' job.
You can do the same with a script, but they are a lot easier to
alter..maliciously.
I prefer the 'Can't touch me. I'm written in C' sort of program..

The MOST dangerous script is the setuid script that someone has left
world writeable after a hasty edit..
However, in your case I would be somewhat tempted to make the target
directory at lest WRITEABLE by whatever process your PHP runs under, if
not readable..a simple matter of seyting up groups and permissions..and
then giving te user a web page generated via PHP to do the whole shebang
from.
>
Feb 26 '08 #6
The Natural Philosopher wrote:
write a teeny C program that does exactly what you want and no more, and
invoke setuid() within it.

You can do the same with a script, but they are a lot easier to
alter..maliciously.
Actually, no you can't. SetUID only works on binaries -- not scripts. Some
kind of security feature.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 28 days, 15:27.]

Bottled Water
http://tobyinkster.co.uk/blog/2008/02/18/bottled-water/
Feb 27 '08 #7
Toby A Inkster wrote:
The Natural Philosopher wrote:
>write a teeny C program that does exactly what you want and no more, and
invoke setuid() within it.

You can do the same with a script, but they are a lot easier to
alter..maliciously.

Actually, no you can't. SetUID only works on binaries -- not scripts. Some
kind of security feature.
Actually, you can change it with posix_setuid(). But the PHP executable
must have the setuid bit set, which then means any script can change to
root (and do anything it wants). Definitely not good.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gmuldoon | last post by:
Help please. Setup: RH EL3, PHP 4.3.5, Apache 1.3.29 running as user "nobody". Have a bash script named shell_script. Telnet to server as user "nobody" ("nobody" having been given /bin/bash...
12
by: hokiegal99 | last post by:
Is there a forum where one could post a Python script and have it critiqued by others? Something like: Y would be more efficent if you did it this way, or doing it that way could cause problems...
1
by: Wm. Scott Miller | last post by:
I have a Custom DTS Task for SQL Server and I've got it working fine, except for when I try to access its properties from an ActiveX script. I have added a new property called Length and a method...
17
by: comp.lang.tcl | last post by:
The TCL command I am using will do a command-line action on a PHP script: set cannotRunPHP I have to do it this way as both the TCL script and the PHP script run as CLI. However, "info.php"...
5
by: Cylix | last post by:
this.menus = { root: new Array };
3
by: Joshua J. Kugler | last post by:
Yes, I've read this: http://mail.python.org/pipermail/python-list/2006-August/395943.html That's not my problem. I installed PlanetPlanet <http://www.planetplanet.org/via the package's...
24
by: Peter Michaux | last post by:
I have a Perl script that I want to run as a set-user-ID program. Many OSes don't allow scripts run as set-user-ID. To make this script portable, it seems I need to write a C wrapper program that...
15
by: Lawrence Krubner | last post by:
Does anything about this script look expensive, in terms of resources or execution time? This script dies after processing about 20 or 25 numbers, yet it leaves no errors in the error logs. This is...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.