Can the user a script runs as be changed?
theAcolyte
created: 2004-07-01 23:05:09

This may be more a linux question then a perl question, but I'm stumped, and don't even have an idea of where to start.

I have a script that is attempting to run/call a program on the system, but is unable to do so because it doesn't have enough privlidges. I'm root on the system -- so I considered changing the program to allow a non-privlidged user to run it, but I'd rather not do so.

The question becomes -- how do I change the user a particular script runs as? Is there an equivalent in perl do typing "su - thisuser" at the prompt?

Thanks in advance for any help ... completely lost. :-\

- Erik

Re: Can the user a script runs as be changed?
created: 2004-07-01 23:11:36

See the manual page of the chmod command for information regarding the setuid bit, which will automatically bump your script up to root (or, more accurately, the owner of the file) whenever it's run. This is what the "s" means in a set of permission flags in a "ls -l" listing.

perlsec has some good information on setuid scripts; you almost certainly want them to run with taint mode on.

Re^2: Can the user a script runs as be changed?
created: 2004-07-01 23:44:43

This is IMHO bad advice. Firstly it simply won't work for many configurations and secondly suid root and web servers are a dangerous combination - especially if someone needs to have suid explained to them. There are other, safer ways to skin this particular cat.

Re^3: Can the user a script runs as be changed?
created: 2004-07-02 00:18:17

The original author didn't specify whether it was running on a web server or not - the instance of a CGI script hadn't occurred to me actually. Yes, CGI scripts shouldn't be run suid root.

Perhaps the author could clarify?

Re^4: Can the user a script runs as be changed?
created: 2004-07-02 00:23:01

Ah, very good point. Don't know why I thought it was a CGI question having just re-read it (can you change root node ins SOPW - I could have sworn it originally said CGI/nobody/apache somewhere). I like jacques answer the best so far ;-)

Re^3: Can the user a script runs as be changed?
created: 2004-07-04 13:44:25
Just because it's dangerous isn't a reason not to teach it to them. Everyone has to learn about it for the first time sometime. It is responsible to give them the "But don't do that." disclaimer, though.
Re^4: Can the user a script runs as be changed?
created: 2004-07-04 19:58:27

Everyone has to learn about it for the first time sometime.

Of course they do but hopefully by that stage they have discovered the man pages and/or read a basic book. Of the two objections I raised the first was the fact that you typically can't run suid scripts on a large number of the servers out there without recompiling the kernel to remove that restriction or wrapping the script with a short C execv() function. Have you ever actually tried it?

[user]$ cat test.pl
#!/usr/bin/perl
print "This is a suid test\n";
[user]$ chmod +s test.pl
[user]$ ll rover.pl
-rwsr-xr-x    1 user coders        203 Mar 10 02:41 test.pl
[user]$ ./test.pl
Can't do setuid
[user]$ su root
Password:
[root]# ./test.pl
This is a suid test
[root]# exit
exit
[user]$ ./test.pl
Can't do setuid
$ uname -sr
Linux 2.4.18-27.7.xsmp
$

cheers

tachyon

Re^5: Can the user a script runs as be changed?
created: 2004-07-15 14:51:22
I've never needed to do anything in this arena, but my comments aren't about what is the right thing to do. I just wanted to emphasize that it's good to talk about all available options. 1) It helps you understand the underlying mechanics better. 2) When you see a new option, you might realize that you were going about it all wrong anyway, and rearranging your code around the new option would increase elegance. I would rather say, "setuid is an answer, but it's the wrong one." than not mention it at all. That's all.
Re^6: Can the user a script runs as be changed?
created: 2004-07-15 18:02:42

Suck it and see. 50% of my wisdom is sucked, 50% is seed.... Somewhere we need to make room for at least 10% attitude and total BS but you get that......

cheers

tachyon

Re: Can the user a script runs as be changed?
created: 2004-07-01 23:24:10

You can use a suid perl/apache but don't do it that way. The best way (IMHO) is to give the web server process permission to execute the program via sudo/sudoers. For example

[root@devel3 log]# cat /etc/sudoers
# sudoers file.
# This file MUST be edited with the 'visudo' command as root.
# See the sudoers man page for the details on how to write a sudoers file.

# let apache send HUP to squid
apache ALL=NOPASSWD:/home/www/utility/sendHUP.pl


[root@devel3 log]# ll /home/www/utility/sendHUP.pl
-rwxr-xr-x    1 apache   coders       1114 Mar 10 02:43 /home/www/utility/sendHUP.pl


[root@devel3 log]# cat /home/www/utility/sendHUP.pl
#!/usr/bin/perl -w

# this script need to be run as root, to do this we add an entry to
# /etc/sudoers so that apache can run it (you edit using visudo)
# visudo -f /etc/sudoers
# add this line
# apache ALL=NOPASSWD:/home/www/utility/sendHUP.pl
# call as system('sudo', '/home/www/utility/sendHUP.pl');

(kill HUP, $PROGRAM) or exit 42;
exit 0;

My webserver runs as apache, but yours may be nobody or something else. What the line in sudoers does is allow apache to *potentialy* run the sendHUP.pl with root privileges. This is required to send (in this case squid) a HUP signal. Note that the actual sendHUP.pl script is not owned by root or suid. It is just a normal script. Note also you need to call this with system( 'sudo', '/some/prog.pl' ) from within your script to execute the program with root privilege.

So by using sudo/sudoers you can limit the webserver to being able to execute as little as a single command/program as root which is better than letting it be able to execute lots of stuff which is quite possible if you go the suid root (route ;-)

cheers

tachyon

Re^2: Can the user a script runs as be changed?
created: 2004-07-02 00:12:28

Note that you can find out what user Apache runs as by looking in the httpd.conf file (mine's located at /etc/httpd/httpd.conf) and looking for lines like

User www
Group www
Obviously Apache runs as www for me.

Re^3: Can the user a script runs as be changed?
created: 2004-07-02 00:19:01
grep "^User " `locate httpd.conf`
Re: Can the user a script runs as be changed?
created: 2004-07-01 23:48:35
something else you can do as far as changing the user a script is running as:
my ($login,$pass,$uid,$gid) = getpwnam('username');
$) = $gid;
$> = $uid;
#now it should be running as username
This probably won't be applicable in this case but its good to know as an FYI
Re: Can the user a script runs as be changed?
created: 2004-07-01 23:58:12
I'm root on the system

Scary.

Re: Can the user a script runs as be changed?
created: 2004-07-02 13:30:03
Depending upon your needs I'd give this general generic advice:

You can set the SUID or equivalent for the group of the script to allow it to run as another user. I'd try to stay away from this as much as possible, but if you do, ALWAYS use tainting checking to verify your input. If your script is a cgi script, consider writing a go-between script elswhere on the system that is SUID and not viewable directly by the webserver.

perlmonks.org content © perlmonks.org and Anonymous Monk, ercparker, jacques, Lexicon, nightwatch, Nkuvu, tachyon, theAcolyte

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03