a cgi wrap to do setuid to root - question for jsatterl or anyone else

Can you tell me how to write the cgi wrap that can be setuid to root and then change the process id of the owner of a file?

Please refer to question below; my problem is not resolved yet.
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?qid=20123690

To summarise my  request:  I want to be able to write to a file from a cgi script. The file does not have world writable permissions.
Setting the owner if the file to be the owner of the script did not work for me.
Thanks for any help.


chazhsAsked:
Who is Participating?
 
dcgamesCommented:
Hmm..

If you type:

($n,$p, $u,$g, $q,$c,$gc,$dir,$sh)=getpwuid($<);
print "User Name is $n\n");

You should get the user id name in $n..

But regardless, if you are the owner of a file, you should be able to give the file to someone else (except root), no?

Something like:

 chmod 775 myfile.pl
 chown 60001 myfile.pl

This makes the file owned by 60001 but you can still edit it because it's in your group. I'm not quite certain of the chown format. It may be:

  chown uid grpid myfile.pl

in which case you want to make sure the grpid is your OWN group Id so you retain access to the file.

---

Perhaps we are going about this the wrong way. if you own the file yourself, and say it's UID/GID are 1234 and 5678 (for example), in PERL you could

$> = 1234; # set the Effective UID for this perl script to 1234

Same for $<, $) and $(, you can just assign the number to it directly (I think).

So perl gains access to the file by changing it's user id to YOU..

Maybe that works better?

Dave



0
 
dcgamesCommented:
Actually, I think you may have gotten this one a bit wrong.
You weren't supposed to set the owner of the file to the owner of the script. You were supposed to set the owner of the file to the user that the CGI script is running under.

To check what it is, create a simple perl script that displays the userid.

Does this make a difference? Cause if not, I can suggest a few different ways to get around this, but you really shouldn't have to do that. Setting the file to the right ownership should be sufficient.

Dave
0
 
chazhsAuthor Commented:
Yes, you are absolutely right, I was confused with "owner of the script" and "owner of the CGI"
I wrote a small pipe to look under which user the cgi is running:  But bad luck, $string does not have a value in the below code.
So I am not sure who is the" owner of cgi" on the server. Does this make sense, or am I doing something wrong still?

# Create the pipe to get the output of the shell command
open (PS, "/usr/bin/ps -f |");
while (<PS>) {
        $string = <PS>;
print "This process is running as $string";
# the o/p i get for $string is blank
}

Thanks for your time.

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
dcgamesCommented:
What's the OS? Unix?

Try this:

@x = `/usr/bin/ps -f`;
print "PS returned @x\n";

See if it works better.

But There is also a PERL way of finding out the user id.

I believe $> has the "effective UID" and $< has the "real UID". Or if you use "english names", then $EUID and $UID are the token names.

$) and $( have the effective GROUP id and the REAL group id respectively.

So

print "EUID = $>\n";
print "UID  = $>\n";
print "EGUID = $)\n";
print "GID   = $(\n";

Dave

0
 
dcgamesCommented:
Oops Typo. SHould be:

print "EUID = $>\n";
print "UID  = $<\n";     # this one was wrong.
print "EGUID = $)\n";
print "GID   = $(\n";

0
 
chazhsAuthor Commented:
Here is the output to your code:

PS returned
EUID = 60001 UID = 60001 EGUID = 60001 60001 GID = 60001 60001

So ps returned with blank value again, and effective UID has some numbers. Does this make sense?
My o.s is solaris5.7

Maybe it means my cgi is running under user " "? Dont know if/how I can change it. And probably, I cannot, since I dont have root permissions on this server?
Thankyou.
0
 
chazhsAuthor Commented:
Im just testing, hey
($n,$p, $u,$g, $q,$c,$gc,$dir,$sh)=getpwuid($<);
                     print "User Name is $n\n");
printed "nobody"

So I have did
chgrp nobody to my file
chmod 775
and then I WAS ABLE TO WRITE TO IT FRM THE WEB!!!

I will test more and let you know.

thanks!
0
 
dcgamesCommented:
Cool.
0
 
chazhsAuthor Commented:
1) First method above works. Thanks, that is so cool.
I set the group of the file to 60001 and permission 775  and I was able to write.

2) I wonder why it shows the owner of cgi as 60001 but when I printed $n from getpwuid, it printed "nobody"  And when I did "ps -ef" it printed blank.

Can you suggest me some good reading material on this one?
Thanks much for your help.

3) Re: method 2 above,

" Perhaps we are going about this the wrong way. if you own the file yourself, and say it's UID/GID are   1234 and 5678 (for example), in PERL you could
                     $> = 1234; # set the Effective UID for this perl script to 1234
                     So perl gains access to the file by changing it's user id to YOU..
                     Maybe that works better?            "

My file's uid/gid are "web" and "dba" dont know how this translates into numbers.
I tried the following in my script, so perl has access to the file
$> =' web';
$< =' web';
$) = ' dba';
$( = 'dba' ;

but this did not work.  I tried without quotes too.

Thanks for your efforts on this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.