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
16 skip_all_if_miniperl("no dynamic loading on miniperl, no POSIX");
23 skip_all('getgrgid() not implemented')
24 unless eval { my($foo) = getgrgid(0); 1 };
26 skip_all("No 'id' or 'groups'") if
27 $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS' || $^O =~ /lynxos/i;
36 # Get our supplementary groups from the system by running commands
38 my ( $groups_command, $groups_string ) = system_groups()
39 or skip_all("No 'id' or 'groups'");
40 my @extracted_groups = extract_system_groups( $groups_string )
41 or skip_all("Can't parse '${groups_command}'");
44 my ($pwgnam) = getgrgid($pwgid);
46 print "# pwgid=$pwgid pwgnam=$pwgnam \$(=$(\n";
48 # Get perl's supplementary groups by looking at $(
49 my ( $gid_count, $all_perl_groups ) = perl_groups();
50 my %basegroup = basegroups( $pwgid, $pwgnam );
51 my @extracted_supplementary_groups = remove_basegroup( \ %basegroup, \ @extracted_groups );
56 # Test: The supplementary groups in $( should match the
57 # getgroups(2) kernal API call.
59 my $ngroups_max = posix_ngroups_max();
60 if ( defined $ngroups_max && $ngroups_max < @extracted_groups ) {
61 # Some OSes (like darwin)but conceivably others might return
62 # more groups from `id -a' than can be handled by the
63 # kernel. On darwin, NGROUPS_MAX is 16 and 12 are taken up for
66 # There is more fall-out from this than just Perl's unit
67 # tests. You may be a member of a group according to Active
68 # Directory (or whatever) but the OS won't respect it because
69 # it's the 17th (or higher) group and there's no space to
70 # store your membership.
71 print "ok 1 # SKIP Your platform's `$groups_command' is broken\n";
75 # darwin uses getgrouplist(3) or an Open Directory API within
76 # /usr/bin/id and /usr/bin/groups which while "nice" isn't
77 # accurate for this test. The hard, real, list of groups we're
78 # running in derives from getgroups(2) and is not dynamic but
79 # the Libc API getgrouplist(3) is.
81 # In practical terms, this meant that while `id -a' can be
82 # relied on in other OSes to purely use getgroups(2) and show
83 # us what's real, darwin will use getgrouplist(3) to show us
84 # what might be real if only we'd open a new console.
86 print "ok 1 # SKIP darwin's `${groups_command}' can't be trusted\n";
91 # Read $( but ignore any groups in $( that we failed to parse
92 # successfully out of the `id -a` mess.
94 my @perl_groups = remove_unparsed_entries( \ @extracted_groups,
95 \ @$all_perl_groups );
96 my @supplementary_groups = remove_basegroup( \ %basegroup,
100 if ( match_groups( \ @supplementary_groups,
101 \ @extracted_supplementary_groups,
106 elsif ( cygwin_nt() ) {
107 %basegroup = unixy_cygwin_basegroups();
108 @extracted_supplementary_groups = remove_basegroup( \ %basegroup, \ @extracted_groups );
110 if ( match_groups( \ @supplementary_groups,
111 \ @extracted_supplementary_groups,
113 print "ok 1 # This Cygwin behaves like Unix (Win2k?)\n";
123 # multiple 0's indicate GROUPSTYPE is currently long but should be short
124 $gid_count->{0} //= 0;
125 if ( 0 == $pwgid || $gid_count->{0} < 2 ) {
129 print "not ok 2 (groupstype should be type short, not long)\n";
135 # Get the system groups and the command used to fetch them.
138 my ( $cmd, $groups_string ) = _system_groups();
140 if ( $groups_string ) {
141 chomp $groups_string;
142 diag_variable( groups => $groups_string );
145 return ( $cmd, $groups_string );
148 # We have to find a command that prints all (effective
149 # and real) group names (not ids). The known commands are:
153 # Beware 1: some systems do just 'id -G' even when 'id -Gn' is used.
154 # Beware 2: id -Gn or id -a format might be id(name) or name(id).
155 # Beware 3: the groups= might be anywhere in the id output.
156 # Beware 4: groups can have spaces ('id -a' being the only defense against this)
157 # Beware 5: id -a might not contain the groups= part.
159 # That is, we might meet the following:
161 # foo bar zot # accept
162 # foo 22 42 bar zot # accept
163 # 1 22 42 2 3 # reject
164 # groups=(42),foo(1),bar(2),zot me(3) # parsed by $GROUP_RX1
165 # groups=22,42,1(foo),2(bar),3(zot(me)) # parsed by $GROUP_RX2
167 # and the groups= might be after, before, or between uid=... and gid=...
168 use constant GROUP_RX1 => qr/
176 use constant GROUP_RX2 => qr/
188 # prefer 'id' over 'groups' (is this ever wrong anywhere?)
189 # and 'id -a' over 'id -Gn' (the former is good about spaces in group names)
191 $cmd = 'id -a 2>/dev/null || id 2>/dev/null';
193 if ( $str && $str =~ /groups=/ ) {
194 # $str is of the form:
195 # uid=39957(gsar) gid=22(users) groups=33536,39181,22(users),0(root),1067(dev)
196 # FreeBSD since 6.2 has a fake id -a:
197 # uid=1001(tobez) gid=20(staff) groups=20(staff), 0(wheel), 68(dialer)
200 # Linux may also have a context= field
202 return ( $cmd, $str );
205 $cmd = 'id -Gn 2>/dev/null';
207 if ( $str && $str !~ /^[\d\s]$/ ) {
208 # $str could be of the form:
209 # users 33536 39181 root dev
210 return ( $cmd, $str );
213 $cmd = 'groups 2>/dev/null';
216 # may not reflect all groups in some places, so do a sanity check
219 # These test results *may* be bogus, as you appear to have AFS,
220 # and I can't find a working 'id' in your PATH (which I have set
223 # If these tests fail, report the particular incantation you use
224 # on this platform to find *all* the groups that an arbitrary
225 # user may belong to, using the 'perlbug' program.
228 return ( $cmd, $str );
234 # Convert the strings produced by parsing `id -a' into a list of group
236 sub extract_system_groups {
237 my ( $groups_string ) = @_;
239 # Remember that group names can contain whitespace, '-', '(parens)',
240 # et cetera. That is: do not \w, do not \S.
243 my @fields = split /\b(\w+=)/, $groups_string;
245 for my $i (0..@fields-2) {
246 if ($fields[$i] eq 'groups=') {
253 my @g = split m{, ?}, $gr;
254 # prefer names over numbers
256 if ( $_ =~ GROUP_RX1() || $_ =~ GROUP_RX2() ) {
257 push @extracted, $+{gr_name} || $+{gid};
260 print "# ignoring group entry [$_]\n";
264 diag_variable( gr => $gr );
265 diag_variable( g => join ',', @g );
266 diag_variable( ex_gr => join ',', @extracted );
272 # Get the POSIX value NGROUPS_MAX.
273 sub posix_ngroups_max {
275 POSIX::NGROUPS_MAX();
279 # Test if this is Apple's darwin
281 # Observed 'darwin-2level'
282 return $Config::Config{myuname} =~ /^darwin/;
285 # Test if this is Cygwin
287 return $Config::Config{myuname} =~ /^cygwin_nt/i;
290 # Get perl's supplementary groups and the number of times each gid
293 # Lookup perl's own groups from $(
294 my @gids = split ' ', $(;
297 for my $gid ( @gids ) {
300 my ($group) = getgrgid $gid;
302 # Why does this test prefer to not test groups which we don't have
303 # a name for? One possible answer is that my primary group comes
304 # from from my entry in the user database but isn't mentioned in
305 # the group database. Are there more reasons?
306 next if ! defined $group;
309 push @gr_name, $group;
312 diag_variable( gr_name => join ',', @gr_name );
314 return ( \ %gid_count, \ @gr_name );
317 # Remove entries from our parsing of $( that don't appear in our
318 # parsing of `id -a`.
319 sub remove_unparsed_entries {
320 my ( $extracted_groups, $perl_groups ) = @_;
327 grep { $was_extracted{$_} }
331 # Get a list of base groups. I'm not sure why cygwin by default is
334 my ( $pwgid, $pwgnam ) = @_;
347 # Cygwin might have another form of basegroup which we should actually use
348 sub unixy_cygwin_basegroups {
349 my ( $pwgid, $pwgnam ) = @_;
356 # Filter a full list of groups and return only the supplementary
358 sub remove_basegroup {
359 my ( $basegroups, $groups ) = @_;
362 grep { ! $basegroups->{$_} }
366 # Test supplementary groups to see if they're a close enough match or
367 # if there aren't any supplementary groups then validate the current
370 my ( $supplementary_groups, $extracted_supplementary_groups, $pwgid ) = @_;
372 # Compare perl vs system groups
374 $g{$_}[0] = 1 for @$supplementary_groups;
375 $g{$_}[1] = 1 for @$extracted_supplementary_groups;
377 # Find any mismatches
379 grep { ! ( $g{$_}[0] && $g{$_}[1] ) }
384 || ( ! @$supplementary_groups
385 && 1 == @$extracted_supplementary_groups
386 && $pwgid == $extracted_supplementary_groups->[0] );
389 # Print a nice little diagnostic.
391 my ( $label, $content ) = @_;
393 printf "# %-11s=%s\n", $label, $content;
397 # Removes duplicates from a list
401 grep { ! $seen{$_}++ }
406 # indent-tabs-mode: nil
409 # ex: set ts=8 sts=4 sw=4 et: