LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-02-2004, 05:34 PM   #1
m3kgt
Member
 
Registered: Oct 2003
Location: WA
Distribution: Redhat 3.0ES & Slackware 8.1
Posts: 44

Rep: Reputation: 15
creating shell script that executes as root regardless of who runs the script?


I have created a little shell script that goes through a specified directory and changes permissions on a few files. The script works perfect when I am logged in as root and run the script from command line.

What I want to do, is make a website, with a button to click that says "reset permissions" and the button will then execute (via PHP) the shell script I made. Getting the webpage to do that is no problem, but there is a problem with the script actually working because apache runs as apache, not as root. And apache isnt able to change permissions on files it doesnt own.

Does that make sense?

Is there a way to make a script that apache can execute that would be able to change permissions on files? Normally only the owner/root would be able to do this... but I need apache to be able to do it. I dont want to change what GID or UID apache runs as because I dont want to end up with big gapping security holes.

If you need any clarifcation, let me know! Thanks.

Joe
 
Old 06-02-2004, 07:23 PM   #2
Shade
Senior Member
 
Registered: Mar 2003
Location: Burke, VA
Distribution: RHEL, Slackware, Ubuntu, Fedora
Posts: 1,418
Blog Entries: 1

Rep: Reputation: 46
What you're looking for is called the SETUID bit. It lets programs execute with the permissions of their owners.

X runs like this, for example -- always with root permissions.

chmod 4xxx filename

example:
chmod 4755 script.sh
should work

where xxx are the permissions you'd normally set.

--Shade
 
Old 06-02-2004, 10:43 PM   #3
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
I don't think this will work exactly. The kernel ignores the setuid bit on shell scripts because it opens up a host of security concerns. Try it yourself with the following script:

Code:
#!/bin/sh

echo I am PID $$ executing as user $UID
On all systems I'm familiar with, it prints out my own UID and does not have root priviliges, regardless of whether or not the SEUID bit is set.
 
Old 06-03-2004, 12:38 AM   #4
Shade
Senior Member
 
Registered: Mar 2003
Location: Burke, VA
Distribution: RHEL, Slackware, Ubuntu, Fedora
Posts: 1,418
Blog Entries: 1

Rep: Reputation: 46
Good call there.

You're indeed right. Does the kernel ignore the SUID for shell scripts only? In other words, this only works with binaries?

--Shade

EDIT: I really feel like I'm missing something here... This should work according to what I've read...

Last edited by Shade; 06-03-2004 at 12:45 AM.
 
Old 06-03-2004, 01:36 AM   #5
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
It should work if the file is owned by root, and is allowed to be executed by anyone.

As root:

Code:
chown root:root /file
chmod ug+rwx,o+x /file
 
Old 06-03-2004, 01:53 AM   #6
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
You could use sudo ? Just make sure the sudo entrie is for apache or what you php script runs as.
 
Old 06-03-2004, 02:38 AM   #7
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Shade,

There are a number of serious security issues with SetUID shell scripts so the Linux kernel simply does not honor the SetUID bit on shell scripts, only binary files. There's a good article I found on this here.
 
Old 06-03-2004, 06:47 AM   #8
chii-chan
Member
 
Registered: Sep 2003
Location: chikyuu (E103N6)
Distribution: Redhat 8.0 (2.4.25-custom), Fedora Core 1 (2.4.30-custom)
Posts: 357

Rep: Reputation: 30
I think editing /etc/sudoers is the way to go. I give my example here. I want my user to be able to run /sbin/rmmod and /sbin/insmod to be able to load and unload usb-storage module. This is what I did to my /etc/sudoers file:

# User privilege specification
root ALL=(ALL) ALL
user ALL=NOPASSWD: /sbin/rmmod usb-storage,/sbin/insmod usb-storage

Then I made a script like this:

#!/bin/bash
#re-usb

sudo /sbin/rmmod usb-storage;sudo /sbin/insmod usb-storage

It is more secure (definitely) that setuid. Use 'visudo'.

Last edited by chii-chan; 06-03-2004 at 07:00 AM.
 
Old 06-03-2004, 11:36 AM   #9
m3kgt
Member
 
Registered: Oct 2003
Location: WA
Distribution: Redhat 3.0ES & Slackware 8.1
Posts: 44

Original Poster
Rep: Reputation: 15
Using chii-chan's method of sudo... lets say I had a shell script called access.sh, and the script looked like this...

Code:
#!bin/bash

echo "Setting permissions for $1."
sudo chown -R $1.users /home/$1/public_html
sudo chmod -R 775 /home/$1/public_html
echo "Done!"
What would my sudoers file need to look like? Would I need to give apache sudo access to the "access.sh" script and then remove the sudo commands from the script, or would I need to give apache access to chown and chmod and leave sudo in the script on each line?

Something about giving apache access to chmod and chown seems a little scary....

What I am trying to acomplish is, sometimes permissions/owners get messed up on files that have been uploaded or modified by users. So I am I trying to make a webpage with a little drop down menu where they can select their site from the list, and hit "reset permissions" and it will change everything back to what it should be. Make sense?

Thanks for all the help everyone.

Last edited by m3kgt; 06-03-2004 at 11:38 AM.
 
Old 06-03-2004, 12:09 PM   #10
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
What is causing the sites to have incorrect permissions?
 
Old 06-03-2004, 12:59 PM   #11
m3kgt
Member
 
Registered: Oct 2003
Location: WA
Distribution: Redhat 3.0ES & Slackware 8.1
Posts: 44

Original Poster
Rep: Reputation: 15
You bring up another good point... this little shell script I have made wouldnt even be needed if the permissions/ownership didnt change all the time.

Whats happening is, I will set all permissions and ownership to what it should be. Then one of my web designers ( we'll call them designer1) uploads a new index.html file with some changes he or she made. Now all of the sudden designer1 is the owner of the file. Now lets say designer2 comes along and wants to make a change to index.html, they cant because designer1 is now the owner of the file. Neither designer1 or designer2 should be the owner of the file, it should always stay as the username associated with the virtual host, which is what I set it to.

Whats happening is, whenever a file is modified or over written (uploaded with scp) then the ownership changes.

Is there a way to stop that from happening?

If you need clarification, just ask.
 
Old 06-03-2004, 01:17 PM   #12
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
You could use a cron script (runs every minute or hour) to recursively chown and chmod. This way you have no manual pulldown work or PHP to Shell script task.
 
Old 06-03-2004, 04:43 PM   #13
Shade
Senior Member
 
Registered: Mar 2003
Location: Burke, VA
Distribution: RHEL, Slackware, Ubuntu, Fedora
Posts: 1,418
Blog Entries: 1

Rep: Reputation: 46
btmiller, That's an EXCELLENT article.

I'm about to dive in on a bit more security reading.
Thanks so much for that clarification.

--Shade
 
Old 06-04-2004, 10:23 PM   #14
Kroenecker
Member
 
Registered: May 2003
Location: The States
Distribution: Gentoo
Posts: 245

Rep: Reputation: 30
I have to second that: the article was very interesting. Thanks for the link!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with creating a shell script windisch Programming 66 10-07-2005 06:26 AM
what shell script runs only at bootup/startup pwhitey86 Mandriva 2 12-22-2004 10:13 PM
creating shell script programming using KNOPPIX.. help cinderella Linux - Newbie 4 12-20-2004 07:12 PM
Creating a shell script to run Java program paultaylor Programming 7 11-12-2004 03:11 PM
Help creating a directory back up shell script WarriorWarren Linux - General 6 04-06-2003 09:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration