4 my $p = "/bin:/usr/bin:/usr/xpg4/bin:/usr/ucb";
10 $ENV{LC_ALL} = "C"; # so that external utilities speak English
11 $ENV{LANGUAGE} = 'C'; # GNU locale extension
21 unless (eval { my($foo) = getgrgid(0); 1 }) {
22 quit( "getgrgid() not implemented" );
25 quit("No `id' or `groups'") if
38 # Get our supplementary groups from the system by running commands
40 my ( $groups_command, $groups_string ) = system_groups()
41 or quit( "No `id' or `groups'" );
42 my @extracted_groups = extract_system_groups( $groups_string )
43 or quit( "Can't parse `${groups_command}'" );
46 my ($pwgnam) = getgrgid($pwgid);
48 print "# pwgid=$pwgid pwgnam=$pwgnam \$(=$(\n";
50 # Get perl's supplementary groups by looking at $(
51 my ( $gid_count, $all_perl_groups ) = perl_groups();
52 my %basegroup = basegroups( $pwgid, $pwgnam );
53 my @extracted_supplementary_groups = remove_basegroup( \ %basegroup, \ @extracted_groups );
58 # Test: The supplementary groups in $( should match the
59 # getgroups(2) kernal API call.
61 my $ngroups_max = posix_ngroups_max();
62 if ( defined $ngroups_max && $ngroups_max < @extracted_groups ) {
63 # Some OSes (like darwin)but conceivably others might return
64 # more groups from `id -a' than can be handled by the
65 # kernel. On darwin, NGROUPS_MAX is 16 and 12 are taken up for
68 # There is more fall-out from this than just Perl's unit
69 # tests. You may be a member of a group according to Active
70 # Directory (or whatever) but the OS won't respect it because
71 # it's the 17th (or higher) group and there's no space to
72 # store your membership.
73 print "ok 1 # SKIP Your platform's `$groups_command' is broken\n";
77 # darwin uses getgrouplist(3) or an Open Directory API within
78 # /usr/bin/id and /usr/bin/groups which while "nice" isn't
79 # accurate for this test. The hard, real, list of groups we're
80 # running in derives from getgroups(2) and is not dynamic but
81 # the Libc API getgrouplist(3) is.
83 # In practical terms, this meant that while `id -a' can be
84 # relied on in other OSes to purely use getgroups(2) and show
85 # us what's real, darwin will use getgrouplist(3) to show us
86 # what might be real if only we'd open a new console.
88 print "ok 1 # SKIP darwin's `${groups_command}' can't be trusted\n";
93 # Read $( but ignore any groups in $( that we failed to parse
94 # successfully out of the `id -a` mess.
96 my @perl_groups = remove_unparsed_entries( \ @extracted_groups,
97 \ @$all_perl_groups );
98 my @supplementary_groups = remove_basegroup( \ %basegroup,
102 if ( match_groups( \ @supplementary_groups,
103 \ @extracted_supplementary_groups,
108 elsif ( cygwin_nt() ) {
109 %basegroup = unixy_cygwin_basegroups();
110 @extracted_supplementary_groups = remove_basegroup( \ %basegroup, \ @extracted_groups );
112 if ( match_groups( \ @supplementary_groups,
113 \ @extracted_supplementary_groups,
115 print "ok 1 # This Cygwin behaves like Unix (Win2k?)\n";
125 # multiple 0's indicate GROUPSTYPE is currently long but should be short
126 $gid_count->{0} //= 0;
127 if ( 0 == $pwgid || $gid_count->{0} < 2 ) {
131 print "not ok 2 (groupstype should be type short, not long)\n";
137 # Cleanly abort this entire test file
139 print "1..0 # SKIP: @_\n";
143 # Get the system groups and the command used to fetch them.
146 my ( $cmd, $groups_string ) = _system_groups();
148 if ( $groups_string ) {
149 chomp $groups_string;
150 diag_variable( groups => $groups_string );
153 return ( $cmd, $groups_string );
156 # We have to find a command that prints all (effective
157 # and real) group names (not ids). The known commands are:
161 # Beware 1: some systems do just 'id -G' even when 'id -Gn' is used.
162 # Beware 2: id -Gn or id -a format might be id(name) or name(id).
163 # Beware 3: the groups= might be anywhere in the id output.
164 # Beware 4: groups can have spaces ('id -a' being the only defense against this)
165 # Beware 5: id -a might not contain the groups= part.
167 # That is, we might meet the following:
169 # foo bar zot # accept
170 # foo 22 42 bar zot # accept
171 # 1 22 42 2 3 # reject
172 # groups=(42),foo(1),bar(2),zot me(3) # parsed by $GROUP_RX1
173 # groups=22,42,1(foo),2(bar),3(zot(me)) # parsed by $GROUP_RX2
175 # and the groups= might be after, before, or between uid=... and gid=...
176 use constant GROUP_RX1 => qr/
184 use constant GROUP_RX2 => qr/
196 # prefer 'id' over 'groups' (is this ever wrong anywhere?)
197 # and 'id -a' over 'id -Gn' (the former is good about spaces in group names)
199 $cmd = 'id -a 2>/dev/null || id 2>/dev/null';
201 if ( $str && $str =~ /groups=/ ) {
202 # $str is of the form:
203 # uid=39957(gsar) gid=22(users) groups=33536,39181,22(users),0(root),1067(dev)
204 # FreeBSD since 6.2 has a fake id -a:
205 # uid=1001(tobez) gid=20(staff) groups=20(staff), 0(wheel), 68(dialer)
208 # Linux may also have a context= field
210 return ( $cmd, $str );
213 $cmd = 'id -Gn 2>/dev/null';
215 if ( $str && $str !~ /^[\d\s]$/ ) {
216 # $str could be of the form:
217 # users 33536 39181 root dev
218 return ( $cmd, $str );
221 $cmd = 'groups 2>/dev/null';
224 # may not reflect all groups in some places, so do a sanity check
227 # These test results *may* be bogus, as you appear to have AFS,
228 # and I can't find a working 'id' in your PATH (which I have set
231 # If these tests fail, report the particular incantation you use
232 # on this platform to find *all* the groups that an arbitrary
233 # user may belong to, using the 'perlbug' program.
236 return ( $cmd, $str );
242 # Convert the strings produced by parsing `id -a' into a list of group
244 sub extract_system_groups {
245 my ( $groups_string ) = @_;
247 # Remember that group names can contain whitespace, '-', '(parens)',
248 # et cetera. That is: do not \w, do not \S.
251 my @fields = split /\b(\w+=)/, $groups_string;
253 for my $i (0..@fields-2) {
254 if ($fields[$i] eq 'groups=') {
261 my @g = split m{, ?}, $gr;
262 # prefer names over numbers
264 if ( $_ =~ GROUP_RX1() || $_ =~ GROUP_RX2() ) {
265 push @extracted, $+{gr_name} || $+{gid};
268 print "# ignoring group entry [$_]\n";
272 diag_variable( gr => $gr );
273 diag_variable( g => join ',', @g );
274 diag_variable( ex_gr => join ',', @extracted );
280 # Get the POSIX value NGROUPS_MAX.
281 sub posix_ngroups_max {
283 POSIX::NGROUPS_MAX();
287 # Test if this is Apple's darwin
289 # Observed 'darwin-2level'
290 return $Config::Config{myuname} =~ /^darwin/;
293 # Test if this is Cygwin
295 return $Config::Config{myuname} =~ /^cygwin_nt/i;
298 # Get perl's supplementary groups and the number of times each gid
301 # Lookup perl's own groups from $(
302 my @gids = split ' ', $(;
305 for my $gid ( @gids ) {
308 my ($group) = getgrgid $gid;
310 # Why does this test prefer to not test groups which we don't have
311 # a name for? One possible answer is that my primary group comes
312 # from from my entry in the user database but isn't mentioned in
313 # the group database. Are there more reasons?
314 next if ! defined $group;
317 push @gr_name, $group;
320 diag_variable( gr_name => join ',', @gr_name );
322 return ( \ %gid_count, \ @gr_name );
325 # Remove entries from our parsing of $( that don't appear in our
326 # parsing of `id -a`.
327 sub remove_unparsed_entries {
328 my ( $extracted_groups, $perl_groups ) = @_;
335 grep { $was_extracted{$_} }
339 # Get a list of base groups. I'm not sure why cygwin by default is
342 my ( $pwgid, $pwgnam ) = @_;
355 # Cygwin might have another form of basegroup which we should actually use
356 sub unixy_cygwin_basegroups {
357 my ( $pwgid, $pwgnam ) = @_;
364 # Filter a full list of groups and return only the supplementary
366 sub remove_basegroup {
367 my ( $basegroups, $groups ) = @_;
370 grep { ! $basegroups->{$_} }
374 # Test supplementary groups to see if they're a close enough match or
375 # if there aren't any supplementary groups then validate the current
378 my ( $supplementary_groups, $extracted_supplementary_groups, $pwgid ) = @_;
380 # Compare perl vs system groups
382 $g{$_}[0] = 1 for @$supplementary_groups;
383 $g{$_}[1] = 1 for @$extracted_supplementary_groups;
385 # Find any mismatches
387 grep { ! ( $g{$_}[0] && $g{$_}[1] ) }
392 || ( ! @$supplementary_groups
393 && 1 == @$extracted_supplementary_groups
394 && $pwgid == $extracted_supplementary_groups->[0] );
397 # Print a nice little diagnostic.
399 my ( $label, $content ) = @_;
401 printf "# %-11s=%s\n", $label, $content;
405 # Removes duplicates from a list
409 grep { ! $seen{$_}++ }
414 # indent-tabs-mode: nil
417 # ex: set ts=8 sts=4 sw=4 noet: