Hi Chagrin,

Note that what you're setting with $( is the real gid, not the effective gid. Its doc says:

If you are on a machine that supports membership in multiple groups simultaneously, gives a space separated list of groups you are in. ... However, a value assigned to $( must be a single number used to set the real gid.

The doc for the effective gid, $), says:

If you are on a machine that supports membership in multiple groups simultaneously, gives a space separated list of groups you are in. ... Similarly, a value assigned to $) must also be a space-separated list of numbers. The first number sets the effective gid, and the rest (if any) are passed to setgroups().

A while back I wrote some code that worked well at the time, which changes the effective gid to with one primary and one (!) secondary effective gid. This is an excerpt:

if ( $) !~ /^$WANT_GID\b.*\b$WANT_GRP2\b/ ) { $) = "$WANT_GID $WANT_GRP2"; die "Error: EGID change failed: $!" if $! || $) ne "$WANT_GID $WANT_GRP2"; } warn "After: RUID=$<, EUID=$>, RGID=$(, EGID=$), umask=" .sprintf("%o",umask)."\n";

As for getting the groups of a user on the machine, after a little bit of research it appears you'll have to walk the group entries yourself, something like this perhaps:

use warnings; use strict; use User::pwent; use User::grent; use List::Util qw/any/; my $u = $ENV{USER}; print " primary: ", getpwnam($u)->gid, "\n"; print "seconday: $_\n" for get_sec_grps($u); sub get_sec_grps { my $user = shift; my @sec_grps; endgrent; # reset (?) while (my $g=getgrent) { push @sec_grps, $g->gid if any {$_ eq $user} @{$g->members}; } endgrent; return @sec_grps; }

Hope this helps,
-- Hauke D


In reply to Re: Using setuid() and absorbing that user's groups by haukex
in thread Using setuid() and absorbing that user's groups by Chagrin

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.