setting uid gid after fork

setting uid gid after fork

am 28.08.2007 09:35:04 von hakim

Hi list,

I have the following script, and it gets started as root:

#!/usr/bin/perl -w

use strict;
use POSIX;

my $uid;
my $gid;

if(($uid = getpwnam("iday")) && ($gid = getgrnam("iday"))) {
print "iday:\n";
print "UID = $uid\n";
print "GID = $gid\n";
$< = $> = $uid;
$( = $) = $gid;
} elsif(($uid = getpwnam("idayserv")) && ($gid =
getgrnam("idayserv"))) {
print "idayserv:\n";
print "UID = $uid\n";
print "GID = $gid\n";
} else {
print "Sorry\n";
}


while(1) {
sleep 10;
}


With "ps -ax -o euid,egid,user,group,command":
EUID EGID USER GROUP COMMAND
1001 0 iday root /usr/bin/perl -w ./test

The user iday has uid 1001 and gid 1001 (groupname iday), but that
does not show up.

How can I change the process uid and gid for example like an apache
server does?

Thanks...

Re: setting uid gid after fork

am 28.08.2007 22:25:11 von Dummy

hakim wrote:
>
> I have the following script, and it gets started as root:
>
> #!/usr/bin/perl -w
>
> use strict;
> use POSIX;
>
> my $uid;
> my $gid;
>
> if(($uid = getpwnam("iday")) && ($gid = getgrnam("iday"))) {
> print "iday:\n";
> print "UID = $uid\n";
> print "GID = $gid\n";
> $< = $> = $uid;
> $( = $) = $gid;
> } elsif(($uid = getpwnam("idayserv")) && ($gid =
> getgrnam("idayserv"))) {
> print "idayserv:\n";
> print "UID = $uid\n";
> print "GID = $gid\n";
> } else {
> print "Sorry\n";
> }
>
>
> while(1) {
> sleep 10;
> }
>
>
> With "ps -ax -o euid,egid,user,group,command":
> EUID EGID USER GROUP COMMAND
> 1001 0 iday root /usr/bin/perl -w ./test
>
> The user iday has uid 1001 and gid 1001 (groupname iday), but that
> does not show up.
>
> How can I change the process uid and gid for example like an apache
> server does?

Once you change the UID you don't have permission to then change the GID so
change:

$< = $> = $uid;
$( = $) = $gid;

To:

$( = $) = $gid;
$< = $> = $uid;

And change the GID first.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall