setting uid gid after fork

Discussion in 'Perl Misc' started by hakim, Aug 28, 2007.

  1. hakim

    hakim Guest

    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...
    hakim, Aug 28, 2007
    #1
    1. Advertising

  2. 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
    John W. Krahn, Aug 28, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Lindner

    Find out username and UID/GID

    Florian Lindner, Jan 4, 2004, in forum: Python
    Replies:
    2
    Views:
    528
    Gerrit Holl
    Jan 4, 2004
  2. Groove
    Replies:
    0
    Views:
    319
    Groove
    Nov 8, 2006
  3. raocheng
    Replies:
    0
    Views:
    3,719
    raocheng
    Jan 20, 2008
  4. Scott McNab
    Replies:
    2
    Views:
    101
    Scott McNab
    Jun 5, 2007
  5. Dave Ardrey

    Messing with UID's and GID's

    Dave Ardrey, Jun 30, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    123
    Greg Bacon
    Jun 30, 2003
Loading...

Share This Page