image
image

|


Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old June 16th, 2008, 05:16 PM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,441
Thanks: 2
Thanked 4 Times in 4 Posts
Mikuro is on a distinguished road
Question Making an executable run as root every time

I want to make a simple shell script that I can easily run as root WITHOUT needing to enter my password every time.

I've heard that you can set an executable file to run as its owner all the time, so if you make its owner root, you're in business. I've done this like so, following instructions I've found in several places online:
Code:
sudo chmod 4711 <file>
sudo chown root <file>
But this does not work. After a little investigation I learned that Apple does not allow this for shell scripts anymore for security reasons, but it DOES (supposedly) allow compiled applications. So I made a simple AppleScript to execute the shell script (hard-coded into the AppleScript file with the "do shell script" command), saved it as an application, and repeated the above steps.

Again, it does not work. The applet will not even load. It just flickers in the Dock for an instant and then disappears.

Am I doing something wrong, or is Leopard even more limited? (Most of the tips I've found online seem to be from 10.3 or 10.4.)

Is there any other way to accomplish this without A) typing my password every time, or B) leaving my root password exposed in the source of a script?
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.2

I'm now a four-browser man. How on earth did this happen?!

Useful programs: PithHelmet, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
  #2  
Old June 17th, 2008, 10:13 AM
Registered User
 
Join Date: Mar 2005
Posts: 191
Thanks: 0
Thanked 1 Time in 1 Post
artov is on a distinguished road
Ok, this is obvious, but you are already doing it. Sudo. When you run the program
with sudo, you are not prompted for the password, if you have run it short while
ago. Edit file /etc/sudoers to specify how long the time is, who can run the program
etc.
Reply With Quote
  #3  
Old June 17th, 2008, 08:38 PM
macbri's Avatar
Mac (r)evolution
 
Join Date: Jun 2005
Location: Ireland
Posts: 247
Thanks: 1
Thanked 0 Times in 0 Posts
macbri is on a distinguished road
You could have sudo not ever require a password for a certain command with an entry in /etc/sudoers:

Code:
mikuro    ALL= NOPASSWD: /path/to/command
Or for the setuid route, it looks like in Leopard setting the setuid bit isn't enough any more -- the code also has to call setuid(), which is the way it should be, I believe. So for example:

Code:
#include <stdio.h>
int main(void) 
{
    if (setuid(0) < 0)
        fprintf(stderr, "setuid() failed\n");
    else
        system("/usr/bin/whoami");
    return(0);
}
Try that with the setuid bit set and it should do what you want.

I'll spare you the usual security warnings and all that...
__________________
Cyber Feen Blog
Reply With Quote
  #4  
Old June 21st, 2008, 09:09 AM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,441
Thanks: 2
Thanked 4 Times in 4 Posts
Mikuro is on a distinguished road
Thanks a lot, Macbri! That sudoers trick sounds like exactly what I need. I'll make a custom shell script, set its owner to root so nobody can edit it, and then add it to my sudoers file.

The setuid() function also looks promising, but I guess it would require making a C program for it, which would probably just make things more complicated.
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.2

I'm now a four-browser man. How on earth did this happen?!

Useful programs: PithHelmet, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
  #5  
Old June 29th, 2008, 07:37 PM
michaelsanford's Avatar
Psycholinguist
 
Join Date: Oct 2002
Location: Ottawa/Montrιal
Posts: 2,152
Thanks: 0
Thanked 0 Times in 0 Posts
michaelsanford is on a distinguished road
macbri's suggestion that "You could have sudo not ever require a password for a certain command with an entry in /etc/sudoers" was the first thing I thought of when reading the post title.

However, you need to be very careful about doing this, as it opens up a huge security hole. If someone sits down at your computer, or logs into it from outside as your user, you've just given them unimpeded root access.

Just something to keep in mind.
__________________
michaelsanford.com • Blog • Twitter • Tumblr • LinkedIn
• iBook G4 1.42 GHz | MacOS X 10.5-current | 1 GB RAM, 100 GB HDD
• iMac G4 TFT 700 MHz | MacOS X 10.4.11 (8S165) | 768 MB RAM, 40 GB HDD
• AMD Athlon64 3500+ | Slackware 12 (2.6.21.5-smp) | 2 GB RAM, 2•120 GB RAID 1, 2•500 GB RAID 0
Reply With Quote
  #6  
Old June 29th, 2008, 09:11 PM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,441
Thanks: 2
Thanked 4 Times in 4 Posts
Mikuro is on a distinguished road
I still need the password for all commands except the one I specified, though. It seems to work fine. When I do "sudo my/special/script.sh", it works with no password. If I do "sudo <anything else>", I need my password. Since both the sudoers file and my special script are root-owned, nobody should be able to mess with them unless they already have root access.

Or is there something I've overlooked?
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.2

I'm now a four-browser man. How on earth did this happen?!

Useful programs: PithHelmet, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
  #7  
Old June 29th, 2008, 09:14 PM
michaelsanford's Avatar
Psycholinguist
 
Join Date: Oct 2002
Location: Ottawa/Montrιal
Posts: 2,152
Thanks: 0
Thanked 0 Times in 0 Posts
michaelsanford is on a distinguished road
There's not really anything you've overlooked, it's just something I wanted to mention, especially for those finding the thread via search.

Though, if the script file were writeable by the hacking user, they could run arbitrary code: good idea to chown and chmod it as you did.
__________________
michaelsanford.com • Blog • Twitter • Tumblr • LinkedIn
• iBook G4 1.42 GHz | MacOS X 10.5-current | 1 GB RAM, 100 GB HDD
• iMac G4 TFT 700 MHz | MacOS X 10.4.11 (8S165) | 768 MB RAM, 40 GB HDD
• AMD Athlon64 3500+ | Slackware 12 (2.6.21.5-smp) | 2 GB RAM, 2•120 GB RAID 1, 2•500 GB RAID 0
Reply With Quote
Reply

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -5. The time now is 02:27 PM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.