| 1 | #!/usr/local/bin/perl | ||
| 2 | # | ||
| 3 | # Listgroup module | ||
| 4 | # | ||
| 5 | # $Id: Listgroup.pm,v 1.16 2003-01-21 12:24:09-05 mprewitt Exp $ | ||
| 6 | # | ||
| 7 | # ----- | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | B<Listgroup.pm> - Lists hosts/users in a netgroup group. | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Listgroup; | ||
| 16 | |||
| 17 | $array_ref_groups = listgroup(); | ||
| 18 | $array_ref_groups = listgroups(); | ||
| 19 | |||
| 20 | $array_ref_users_or_groups = listgroup({groupname}); | ||
| 21 | |||
| 22 | $array_ref_users_or_groups = listgroup_user({groupname1}, | ||
| 23 | [ [-]{groupname2}, [-]{gropuname3} ]); | ||
| 24 | |||
| 25 | $array_ref_users_or_groups = listgroup_host({groupname1}, | ||
| 26 | [ [-]{groupname2}, [-]{gropuname3} ]); | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | A library used to get groups or members of a netgroup NIS map. | ||
| 31 | B<listgroup()> without any parameters or B<listgroups()> lists all | ||
| 32 | the available netgroup groups. | ||
| 33 | |||
| 34 | With groupname parameters B<listgroup, listgroup_user, listgroup_host> will | ||
| 35 | recusively list the members of the named groups. If the groupname is preceded with | ||
| 36 | a B<-> members of that group will be excluded from the returned list. Each member | ||
| 37 | in a group is a triplet of (host,user,domain). The host portion or user portion | ||
| 38 | of the members is returned by B<listgroup_host()> and B<listgroup()>, | ||
| 39 | the user portion of the members is returned by B<listgroup_user()>. | ||
| 40 | |||
| 41 | =head1 REQUIRES | ||
| 42 | |||
| 43 | Net::NIS | ||
| 44 | |||
| 45 | =head1 SEE ALSO | ||
| 46 | |||
| 47 | L<netgroup(4)>, L<listgroup(1)>, L<Net::NIS(3)> | ||
| 48 | |||
| 49 | =head1 AUTHOR | ||
| 50 | |||
| 51 | Original unknown | ||
| 52 | |||
| 53 | Major rewrite by Marc Prewitt <mprewitt@chelsea.net> | ||
| 54 | |||
| 55 | Copyright (C) 2003 Chelsea Networks, under the GNU GPL. | ||
| 56 | listgroup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are | ||
| 57 | welcome to redistribute it under certain conditions; see the COPYING file | ||
| 58 | for details. | ||
| 59 | |||
| 60 | listgroup is free software; you can redistribute it and/or modify it under the | ||
| 61 | terms of the GNU General Public License as published by the Free Software | ||
| 62 | Foundation; either version 2 of the License, or (at your option) any later | ||
| 63 | version. | ||
| 64 | |||
| 65 | listgroup is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 66 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 67 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 68 | details. | ||
| 69 | |||
| 70 | You should have received a copy of the GNU General Public License along | ||
| 71 | with this program; if not, write to the Free Software Foundation, Inc., | ||
| 72 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 73 | |||
| 74 | =head1 PUBLIC METHODS | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | package Net::NIS::Listgroup; | ||
| 79 | require Exporter; | ||
| 80 | @ISA = qw(Exporter); | ||
| 81 | @EXPORT = qw(listgroup); | ||
| 82 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 83 | |||
| 84 | 0 | ▶ | use Net::NIS qw( :all ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 85 | use vars qw( $VERSION $DOMAIN ); | ||
| 86 | |||
| 87 | $DOMAIN = Net::NIS::yp_get_default_domain(); | ||
| 88 | |||
| 89 | my $YPCAT = '/usr/bin/ypcat'; | ||
| 90 | |||
| 91 | $VERSION = (qw $Revision: 1.16 $)[1]; | ||
| 92 | |||
| 93 | =head2 listgroups | ||
| 94 | |||
| 95 | $array_ref_groups = listgroups(); | ||
| 96 | |||
| 97 | Returns a reference to an array of groups from the netgroup nis map. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | sub listgroups { | ||
| 101 | my %netgroup; | ||
| 102 | tie %netgroup, 'Net::NIS', 'netgroup'; | ||
| 103 | return [ sort keys %netgroup ]; | ||
| 104 | } | ||
| 105 | |||
| 106 | =head2 listgroup_host, listgroup | ||
| 107 | |||
| 108 | $array_ref_users_or_groups = listgroup({groupname1}, | ||
| 109 | [ [-]{groupname2}, [-]{gropuname3} ]); | ||
| 110 | |||
| 111 | $array_ref_users_or_groups = listgroup_host({groupname1}, | ||
| 112 | [ [-]{groupname2}, [-]{gropuname3} ]); | ||
| 113 | |||
| 114 | Returns a reference to an array of the host portion of the members of the provided groups. | ||
| 115 | Members of groupnames preceded by a B<-> will be excluded from the returned list. | ||
| 116 | |||
| 117 | Groups are processed in the order they appear in the parameter list. | ||
| 118 | |||
| 119 | If the NIS map 'netgroup' does not exist or another fatal NIS error | ||
| 120 | occurs, die will be called. Wrap this call in an eval if you want | ||
| 121 | to catch that type of error. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | sub listgroup_host { | ||
| 125 | my $r = Net::NIS::Listgroup::Request->new(); | ||
| 126 | $r->setHost(); | ||
| 127 | return $r->_listgroup(@_); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub listgroup { | ||
| 131 | my $r = Net::NIS::Listgroup::Request->new(); | ||
| 132 | $r->setHost(); | ||
| 133 | return $r->_listgroup(@_); | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 listgroup_user | ||
| 137 | |||
| 138 | $array_ref_users_or_groups = listgroup_user({groupname1}, | ||
| 139 | [ [-]{groupname2}, [-]{gropuname3} ]); | ||
| 140 | |||
| 141 | Returns a reference to an array of the user portion of the members of the provided groups. | ||
| 142 | Members of groupnames preceded by a B<-> will be excluded from the returned list. | ||
| 143 | |||
| 144 | Groups are processed in the order they appear in the parameter list. | ||
| 145 | |||
| 146 | If the NIS map 'netgroup' does not exist or another fatal NIS error | ||
| 147 | occurs, die will be called. Wrap this call in an eval if you want | ||
| 148 | to catch that type of error. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | sub listgroup_user { | ||
| 152 | my $r = Net::NIS::Listgroup::Request->new(); | ||
| 153 | $r->setUser(); | ||
| 154 | return $r->_listgroup(@_); | ||
| 155 | } | ||
| 156 | |||
| 157 | #====================================================================== | ||
| 158 | |||
| 159 | package Net::NIS::Listgroup::Request; | ||
| 160 | |||
| 161 | use Net::NIS qw( :all ); | ||
| 162 | |||
| 163 | my $YPMATCH = '/usr/bin/ypmatch'; | ||
| 164 | |||
| 165 | # | ||
| 166 | # new returns an object used to encapsulate options passed in the | ||
| 167 | # original request. | ||
| 168 | # | ||
| 169 | sub new { | ||
| 170 | my $type = shift; | ||
| 171 | $type = ref($type) if ref($type); | ||
| 172 | return bless {}, $type; | ||
| 173 | } | ||
| 174 | |||
| 175 | # | ||
| 176 | # $request->setHost() | ||
| 177 | # | ||
| 178 | # This requset will return host information | ||
| 179 | # | ||
| 180 | sub setHost { | ||
| 181 | my $self = shift; | ||
| 182 | $self->{user} = 0; | ||
| 183 | return $self->{host} = 1; | ||
| 184 | } | ||
| 185 | |||
| 186 | # | ||
| 187 | # $request->setUser() | ||
| 188 | # | ||
| 189 | # This request will return user information | ||
| 190 | # | ||
| 191 | sub setUser { | ||
| 192 | my $self = shift; | ||
| 193 | $self->{host} = 0; | ||
| 194 | return $self->{user} = 1; | ||
| 195 | } | ||
| 196 | |||
| 197 | # | ||
| 198 | # $want_user = $request->getUser() | ||
| 199 | # | ||
| 200 | # Whether the user field is wanted in the request. | ||
| 201 | # | ||
| 202 | sub getUser { | ||
| 203 | my $self = shift; | ||
| 204 | return $self->{user}; | ||
| 205 | } | ||
| 206 | |||
| 207 | # | ||
| 208 | # $request->_listgroup( @groups ) | ||
| 209 | # | ||
| 210 | # Returns a arrayref of members contained or not contained | ||
| 211 | # in @groups. If a group starts with a '-' it's members | ||
| 212 | # will be excluded from the list. | ||
| 213 | # | ||
| 214 | sub _listgroup { | ||
| 215 | my $r = shift; | ||
| 216 | my @args = @_; | ||
| 217 | |||
| 218 | my ( %returns ); | ||
| 219 | |||
| 220 | foreach my $netgroup (@args) { | ||
| 221 | my $subtract; | ||
| 222 | if ( $netgroup =~ s/^-// ) { | ||
| 223 | $subtract = 1; | ||
| 224 | } | ||
| 225 | my ($status, $members) = Net::NIS::yp_match($Net::NIS::Listgroup::DOMAIN, 'netgroup', $netgroup); | ||
| 226 | die "Unknown netgroup: $netgroup [$Net::NIS::yperr]\n" unless $status == YPERR_SUCCESS; | ||
| 227 | |||
| 228 | $members =~ s/#.*//; # remove comments | ||
| 229 | |||
| 230 | foreach my $member ( split(/\s+/, $members) ) { | ||
| 231 | if ($member =~ s/^\(//) { | ||
| 232 | $member =~ s/\)$//; | ||
| 233 | my ($host, $user, $domain) = split(/,/, $member); | ||
| 234 | if ($r->getUser()) { | ||
| 235 | if ($subtract) { | ||
| 236 | delete $returns{$user}; | ||
| 237 | } else { | ||
| 238 | $returns{$user} = $user if $user; | ||
| 239 | } | ||
| 240 | } else { | ||
| 241 | if ($subtract) { | ||
| 242 | delete $returns{$host}; | ||
| 243 | } else { | ||
| 244 | $returns{$host} = $host if $host; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } else { | ||
| 248 | foreach my $thing (@{$r->_listgroup($member) || []}) { | ||
| 249 | if ($subtract) { | ||
| 250 | delete $returns{$thing}; | ||
| 251 | } else { | ||
| 252 | $returns{$thing} = $thing if $thing; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | } | ||
| 256 | } | ||
| 257 | } | ||
| 258 | return [sort keys %returns]; | ||
| 259 | } | ||
| 260 | |||
| 261 | 1; | ||