Basically, what I'm trying to do, is write a script where people log in with their username/password, and the script changes it's level of priveleges to match those of the user logging in (same uid, gid, and groups). The script starts out suid root, and then drops priveleges.

I'm having problems understanding how the $UID, $GID, $EUID, and $EGID variables work. Here are some examples: If I assign the variables like this:

$UID = $new_uid; $GID = $new_gid; $EUID = $new_uid; $EGID = "$new_gid $new_gid";
Then it appears as if my script is still in one of root's groups. My debugging output shows:
UID: '1001' GID: '100 33' EUID: '1001' EGID: '100 33'
1001 and 100 are the user's uid and gid, so those are correct, but 33 is a group that root is in, but the user is not in. The script shouldn't be in that group anymore.

However, if I assign the variables in a different order, the results are not the same. Assigning like this:

($UID, $GID) = ($new_uid, $new_gid); ($EUID, $EGID) = ($new_uid, "$new_gid $new_gid");
results in what appears to be what I want:
UID: '1001' GID: '100 100' EUID: '1001' EGID: '100 100'
I know that list variables inside a list assignment are assigned in parallel.... but why does the order affect the result? And should I be changing $UID and $GID, or just $EUID and $EGID?

In reply to changing uid/gid in suid cgi script by bless$self=>perlmonks;

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.