File Coverage

blib/lib/Net/NIS/Listgroup.pm
Criterion Covered Total %
statement 15 62 24.1
branch 0 24 0.0
condition n/a
subroutine 5 13 38.4
pod 4 4 100.0
total 24 103 23.3


line stmt bran cond sub pod time code
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 - 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 without any parameters or B lists all
32             the available netgroup groups.
33              
34             With groupname parameters B 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 and B,
39             the user portion of the members is returned by B.
40              
41             =head1 REQUIRES
42              
43             Net::NIS
44              
45             =head1 SEE ALSO
46              
47             L, L, L
48              
49             =head1 AUTHOR
50              
51             Original unknown
52              
53             Major rewrite by Marc Prewitt
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     2   9210 use strict;
  2         4  
  2         118  
83              
84 2     2   1593 use Net::NIS qw( :all );
  2         11267  
  2         676  
85 2     2   20 use vars qw( $VERSION $DOMAIN );
  2         8  
  2         511  
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 1     1 1 413 my %netgroup;
102 1         9 tie %netgroup, 'Net::NIS', 'netgroup';
103 1         57 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 0     0 1   my $r = Net::NIS::Listgroup::Request->new();
126 0           $r->setHost();
127 0           return $r->_listgroup(@_);
128             }
129              
130             sub listgroup {
131 0     0 1   my $r = Net::NIS::Listgroup::Request->new();
132 0           $r->setHost();
133 0           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 0     0 1   my $r = Net::NIS::Listgroup::Request->new();
153 0           $r->setUser();
154 0           return $r->_listgroup(@_);
155             }
156              
157             #======================================================================
158              
159             package Net::NIS::Listgroup::Request;
160              
161 2     2   11 use Net::NIS qw( :all );
  2         3  
  2         1351  
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 0     0     my $type = shift;
171 0 0         $type = ref($type) if ref($type);
172 0           return bless {}, $type;
173             }
174              
175             #
176             # $request->setHost()
177             #
178             # This requset will return host information
179             #
180             sub setHost {
181 0     0     my $self = shift;
182 0           $self->{user} = 0;
183 0           return $self->{host} = 1;
184             }
185              
186             #
187             # $request->setUser()
188             #
189             # This request will return user information
190             #
191             sub setUser {
192 0     0     my $self = shift;
193 0           $self->{host} = 0;
194 0           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 0     0     my $self = shift;
204 0           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 0     0     my $r = shift;
216 0           my @args = @_;
217              
218 0           my ( %returns );
219              
220 0           foreach my $netgroup (@args) {
221 0           my $subtract;
222 0 0         if ( $netgroup =~ s/^-// ) {
223 0           $subtract = 1;
224             }
225 0           my ($status, $members) = Net::NIS::yp_match($Net::NIS::Listgroup::DOMAIN, 'netgroup', $netgroup);
226 0 0         die "Unknown netgroup: $netgroup [$Net::NIS::yperr]\n" unless $status == YPERR_SUCCESS;
227              
228 0           $members =~ s/#.*//; # remove comments
229              
230 0           foreach my $member ( split(/\s+/, $members) ) {
231 0 0         if ($member =~ s/^\(//) {
232 0           $member =~ s/\)$//;
233 0           my ($host, $user, $domain) = split(/,/, $member);
234 0 0         if ($r->getUser()) {
235 0 0         if ($subtract) {
236 0           delete $returns{$user};
237             } else {
238 0 0         $returns{$user} = $user if $user;
239             }
240             } else {
241 0 0         if ($subtract) {
242 0           delete $returns{$host};
243             } else {
244 0 0         $returns{$host} = $host if $host;
245             }
246             }
247             } else {
248 0 0         foreach my $thing (@{$r->_listgroup($member) || []}) {
  0            
249 0 0         if ($subtract) {
250 0           delete $returns{$thing};
251             } else {
252 0 0         $returns{$thing} = $thing if $thing;
253             }
254             }
255             }
256             }
257             }
258 0           return [sort keys %returns];
259             }
260              
261             1;