4th Floor, 4 Tabernacle Street London EC2A 4LU

Recursively Expand a NIS Netgroup


 @results=expand_ng();


# Given a netgroup, will return a list of members.  Will recurse
# netgroups if the $recurse flag is set
sub expand_ng
{
        my($netgroup)=@_;
        my(@returnng);
        my($ng)=`ypmatch $netgroup netgroup`;
        $? != 0 and return;     #Don't continue for duff netgroups
        my($value);
        $ng =~ s/\s+/ /g;       #Compress whitespace
        $ng =~ s/\s+,/,/g;      #Remove space before a comma
        $ng =~ s/,\s+/,/g;      #Remove space after a comma

        foreach $value (split /\s+/,$ng)
        {
                # A member will either be a  tuple in brackets or another
                # netgroup.
                if($value =~ /^[^(]/)
                {
                        if ($recurse)
                        {
                                $debug and print "::recursing $value\n";
                                push (@returnng,expand_ng($value));
                        }
                        else
                        {
                                $debug and print "::adding(non-recursed) $value\n";
                                push (@returnng,$value);
                        }
                }
                else
                {
                        $debug and print "::adding $value\n";
                        push (@returnng,$value);
                }
        }
        return @returnng;
}