CfgTie/TieShadow.pm

filter: Statementstmt 5.7% 8 / 139 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 4.0% 1 / 25 Podpod 0.0% 0 / 1 totaltotal 3.5% 9 / 252 CC 37 SCAR 34.0
CC
37
Cov
4%
STORE
110.0
EXISTS
42.0
FETCH
30.0
CRAP 29.9
« CfgTie/TieUser.pm ↑ Summary CfgTie/TieNamed.pm »
1 #!/usr/bin/perl -Tw
2 #Copyright 1998-2001, Randall Maas. All rights reserved. This program is free
3 #software; you can redistribute it and/or modify it under the same terms as
4 #PERL itself.
5  
6  
7 package CfgTie::TieShadow;
8 1 ▶use CfgTie::filever;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 CfgTie::TieShadow -- an associative array of user names to password information
13  
14 =head1 SYNOPSIS
15  
16 This module makes the shadow database available as a regular hash.
17  
18 =head1 DESCRIPTION
19  
20 This is a straightforward hash tie that allows us to access the shadow
21 password database sanely.
22  
23 =head2 Ties
24  
25 This tie is available for programmers:
26  
27 C<tie %shadow,'CfgTie::TieShadow'>
28  
29 C<$shadow{$name}> will return a hash reference of the named shadow information
30  
31  
32 =head2 Structure of hash
33  
34 Any given shadow entry has the following information associated with it (the
35 keys are case-insensitive):
36  
37 =over 1
38  
39 =item C<Name>
40  
41 Login name
42  
43 =item C<Password>
44  
45 The encrypted password
46  
47 =item C<Last>
48  
49 Last time it was changed
50  
51 =item C<Min>
52  
53 The minimum number of days before a change is allowed
54  
55 =item C<Max>
56  
57 Maximum number of days before a change in passwords is required
58  
59 =item C<Warn>
60  
61 The number of days before expiration that they will receive a warning
62  
63 =item C<Inactive>
64  
65 The number of days before an account is inactive
66  
67 =item C<Expires>
68  
69 The date the account expires on
70  
71 =item C<Inactive>
72  
73 The number of days after a password expires that the account is considered
74 inactive and expires
75  
76 =back
77  
78 Each of these entries can be modified (even deleted), and they will be
79 reflected in the overall system. Additionally, the programmer can set any
80 other associated key, but this information will only available to the running
81 Perl script.
82  
83 =head2 Additional Routines
84  
85 =over 1
86  
87 =item C<&CfgTie::TieShadow'status()>
88  
89 =item C<&CfgTie::TieShadow_id'status()>
90  
91 Will return C<stat> information on the shadow database.
92  
93 =back
94  
95 =head2 Miscellaneous
96  
97 C<$CfgTie::Tiehadow_rec'usermod> contains the path to the program F<usermod>.
98 This can be modified as required.
99  
100 C<$CfgTie::TieShadow_rec'useradd> contains the path to the program F<useradd>.
101 This can be modified as required.
102  
103 C<$CfgTie::TieShadow_rec'userdel> contains the path to the program F<userdel>.
104 This can be modified as required.
105  
106 =head1 Files
107  
108 F</etc/passwd>
109 F</etc/group>
110 F</etc/shadow>
111  
112  
113 =head1 See Also
114  
115 L<CfgTie::Cfgfile>, L<CfgTie::TieAliases>, L<CfgTie::TieGeneric>,
116 L<CfgTie::TieGroup>,
117 L<CfgTie::TieHost>, L<CfgTie::TieNamed>, L<CfgTie::TieNet>,
118 L<CfgTie::TiePh>, L<CfgTie::TieProto>, L<CfgTie::TieServ>,
119 L<CfgTie::TieShadow>, L<CfgTie::TieUser>
120  
121 L<group(5)>,
122 L<passwd(5)>,
123 L<shadow(5)>,
124 L<usermod(8)>,
125 L<useradd(8)>,
126 L<userdel(8)>
127  
128 =head1 Caveats
129  
130 The current version does cache some shadow information.
131  
132 =head1 Author
133  
134 Randall Maas (L<randym@acm.org>)
135  
136 =cut
137  
138 1 my $Chg_FS = 1; #By default we want to update the file system when the shadow
139 # Id changes
140  
141 sub status
142 0 ▶{
Subroutine status: not called CC=1 SCAR=6.9
Pod undocumented
143 # the information for the /etc/shadow file
144 0 stat '/etc/shadow';
145 }
146  
147 sub TIEHASH
148 0 ▶{
Subroutine TIEHASH: not called CC=1 SCAR=6.9
149 0 my $self = shift;
150 0 my $node = {};
151 0 return bless $node, $self;
152 }
153  
154 sub FIRSTKEY
155 0 ▶{
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
156 0 my $self = shift;
157  
158 #Rewind outselves to the beginning.
159 0 setpwent;
160  
161 0 &NEXTKEY($self);
162 }
163  
164 sub NEXTKEY
165 0 ▶{
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
166 0 my $self = shift;
167  
168 #Get the next shadow id in the database.
169 # Get the information from the system and store it for later
170 0 my @x = getpwent;
171 0 ▶ if (! scalar @x) {return;}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @x) 0 0 0
172  
173 0 &CfgTie::TieShadow_rec'TIEHASH(0,@x);
174 0 return $x[0]; #Corresponds to the name
175 }
176  
177 sub EXISTS
178 0 ▶{
Subroutine EXISTS: not called CC=3 SCAR=24.8
179 0 my ($self,$name) = @_;
180 0 my $lname =lc($name);
181 0 ▶ if (exists $CfgTie::TieShadow_rec'by_name{$lname}) {return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CfgTie::TieShadow_rec::by_name{$lname}) 0 0 0
182  
183 # Get the information from the system and store it for later
184 0 my @x = getpwnam $name;
185 0 ▶ if (! scalar @x) {return 0;}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @x) 0 0 0
186  
187 0 tie %{$CfgTie::TieShadow_rec'by_name{$lname}}, 'CfgTie::TieShadow_rec',@x;
188 0 return 1;
189 }
190  
191 sub FETCH
192 0 ▶{
Subroutine FETCH: not called CC=3 SCAR=24.8
193 0 my ($self, $name) = @_;
194  
195 0 ▶ if (!defined $name) {return;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
196  
197 0 my $lname = lc($name);
198  
199 #check out our cache first
200 0 ▶ if (&EXISTS($self,$lname)) {return $CfgTie::TieShadow_rec'by_name{$lname};}
Branch: 1 branch
BranchTrueFalseTotal
if (&EXISTS($self, $lname)) 0 0 0
201 }
202  
203 #Bug creating shadows is not supported yet.
204 sub STORE
205 0 ▶{
Subroutine STORE: not called CC=1 SCAR=6.9
206 0 my ($self,$key,$val) = @_;
207 # shadowadd or shadowmod, depending
208 }
209  
210 #Bug need to consider how vigorously we delete things -r ? /var/mail/me, etc
211 sub DELETE
212 0 ▶{
Subroutine DELETE: not called CC=2 SCAR=17.9
213 0 my $self = shift;
214 0 my $name = shift;
215  
216 #Basically delete the shadow now.
217 0 system "$CfgTie::TieShadow_rec'shadowdel $name";
218  
219 #Remove it from our cache
220 0 ▶ if (exists $CfgTie::TieShadow_rec'by_name{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CfgTie::TieShadow_rec::by_name{$name}) 0 0 0
221 0 {delete $CfgTie::TieShadow_rec'by_name{$name};}
222 }
223  
224 package CfgTie::TieShadow_id;
225  
226 sub status
227 0 ▶{
Subroutine status: not called CC=1 SCAR=6.9
228 # the information for the /etc/shadow file
229 0 stat '/etc/shadow';
230 }
231  
232  
233 sub TIEHASH
234 0 ▶{
Subroutine TIEHASH: not called CC=1 SCAR=6.9
235 0 my $self = shift;
236 0 my $node = {};
237 0 return bless $node, $self;
238 }
239  
240 sub FIRSTKEY
241 0 ▶{
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
242 0 my $self = shift;
243  
244 #Rewind outselves to the beginning.
245 0 setpwent;
246  
247 0 &NEXTKEY($self);
248 }
249  
250 sub NEXTKEY
251 0 ▶{
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
252 0 my $self = shift;
253  
254 #Get the next shadow id in the database.
255 # Get the information from the system and store it for later
256 0 my @x = getpwent;
257 0 ▶ if (! scalar @x) {return;}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @x) 0 0 0
258  
259 0 &CfgTie::TieShadow_rec'TIEHASH(0,@x);
260 0 return $x[2]; #Corresponds to the id
261 }
262  
263 sub EXISTS
264 0 ▶{
Subroutine EXISTS: not called CC=3 SCAR=24.8
265 0 my ($self,$id) = @_;
266 0 ▶ if (exists $CfgTie::TieShadow_rec'by_id{$id}) {return 1;}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CfgTie::TieShadow_rec::by_id{$id}) 0 0 0
267  
268 # Get the information from the system and store it for later
269 0 my @x = getpwuid $id;
270 0 ▶ if (! scalar @x) {return 0;}
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @x) 0 0 0
271  
272 0 tie %{$CfgTie::TieShadow_rec'by_name{$x[0]}}, 'CfgTie::TieShadow_rec',@x;
273 0 $CfgTie::TieShadow_rec'by_id{$id} = $CfgTie::TieShadow_rec'by_name{$x[0]};
274 0 return 1;
275 }
276  
277 sub FETCH
278 0 ▶{
Subroutine FETCH: not called CC=2 SCAR=17.9
279 0 my ($self,$id) = @_;
280  
281 #check out our cache first
282 0 ▶ if (&EXISTS($self,$id)) {return $CfgTie::TieShadow_rec'by_id{$id};}
Branch: 1 branch
BranchTrueFalseTotal
if (&EXISTS($self, $id)) 0 0 0
283 }
284  
285 #Bug creating shadows is not supported yet.
286 sub STORE
287 0 ▶{
Subroutine STORE: not called CC=1 SCAR=6.9
288 0 my ($self,$key,$val) = @_;
289 # shadowadd or shadowmod, depending
290 }
291  
292 #Bug need to consider how vigorously we delete things -r ? /var/mail/me, etc
293 sub DELETE
294 0 ▶{
Subroutine DELETE: not called CC=3 SCAR=24.8
295 0 my ($self,$id) = @_;
296  
297 0 ▶ if (!exists $CfgTie::TieShadow_rec'by_id{$id})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $CfgTie::TieShadow_rec::by_id{$id}) 0 0 0
298 {
299 #Try to look up the shadow id
300 0 &FETCH($self,$id);
301 }
302
303 0 ▶ if (exists $CfgTie::TieShadow_rec'by_id{$id})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CfgTie::TieShadow_rec::by_id{$id}) 0 0 0
304 {
305 #Basically delete the shadow now.
306 0 system "$CfgTie::TieShadow_rec'shadowdel $CfgTie::TieShadow_rec'by_id{$id}->{Name}";
307  
308 #Remove it from out cache
309 0 delete $CfgTie::TieShadow_rec'by_id{$id};
310 }
311 }
312  
313  
314  
315 package CfgTie::TieShadow_rec;
316 # A package used by both shadow_id and shadow to retain record information about
317 # a person. This is the only way to access shadowmod.
318  
319 #Two hashes are used for look up
320 # $by_name{$name}
321 # $by_id{$id}
322  
323 sub TIEHASH
324 0 ▶{
Subroutine TIEHASH: not called CC=3 SCAR=24.8
325 # Ties a single shadow to a register...
326 0 my ($self,$Name,@Rest) = @_;
327 0 my $Node;
328 0 my $lname = lc($Name);
329  
330 0 ▶ if (scalar @Rest)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @Rest) 0 0 0
331 {
332 ($Node->{password},$Node->{id}, $Node->{groupid},
333 $Node->{quota}, $Node->{comment}, $Node->{gcos}, $Node->{home},
334 0 $Node->{shell}) = @Rest;
335 }
336  
337 0 ▶ if (defined $Name) {$Node->{name}=$Name;}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Name) 0 0 0
338  
339 0 return bless $Node, $self;
340 }
341  
342 sub FIRSTKEY
343 0 ▶{
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
344 0 my $self = shift;
345 0 my $a = keys %{$self};
346 0 return scalar each %{$self};
347 }
348  
349 sub NEXTKEY
350 0 ▶{
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
351 0 my $self = shift;
352 0 return scalar each %{$self};
353 }
354  
355 sub scan_lasts
356 0 ▶{
Subroutine scan_lasts: not called CC=3 SCAR=24.8
357 #Get the last time the read their email
358  
359 0 my $L = new Secure::File "</var/log/maillog";
360 0 ▶ while (<$L>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $L)) 0 0 0
361 {
362 0 ▶ if(/([\d\w\s:]+)\s\w+\s\w+\[\d+\]:\sLogout\sshadow\s(\w+).*/)
Branch: 1 branch
BranchTrueFalseTotal
if (/([\d\w\s:]+)\s\w+\s\w+\[\d+\]:\sLogout\sshadow\s(\w+).*/) 0 0 0
363 0 {$Last{lc($2)} = $1;}
364 }
365  
366 0 $L->close;
367 }
368  
369 sub EXISTS
370 0 ▶{
Subroutine EXISTS: not called CC=6 SCAR=37.4
371 0 my ($self,$key) = @_;
372 0 my $lkey=lc($key);
373  
374 0 ▶ if ($lkey eq 'last' && !exists $self->{'last'})
Branch: 1 branch
BranchTrueFalseTotal
if ($lkey eq "last" and not exists $self->{"last"}) 0 0 0
Condition0%0/3
$lkey eq "last" and not exists $self->{"last"}
!ll&&!rl&&r
000
MC/DC0%0/2
$lkey eq "last" and not exists $self->{"last"}
$lkey eq "last" not exists $self->{"last"}
Truth table0%0/3
A and not(B)
A= $lkey eq "last"B= not exists $self->{"last"}
ABresult
0X0
100
111
375 {
376 #Try to recover it from our shadow, but avoid scanning the last
377 #file if we can -- it takes a *long* time
378 0 ▶ if (!exists $Last{lc($self->{name})} && !defined $scanned_last)
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Last{lc $self->{"name"}} or defined $scanned_last) 0 0 0
Condition0%0/3
exists $Last{lc $self->{"name"}} or defined $scanned_last
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $Last{lc $self->{"name"}} or defined $scanned_last
exists $Last{lc $self->{"name"}} defined $scanned_last
Truth table0%0/3
A or B
A= exists $Last{lc $self->{"name"}}B= defined $scanned_last
ABresult
1X1
011
000
379 {
380 0 &scan_lasts();
381 0 $scanned_last=1;
382 }
383 0 ▶ if (exists $Last{lc($self->{name})})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Last{lc $self->{"name"}}) 0 0 0
384 0 {$self->{'last'} = $Last{lc($self->{name})};}
385 }
386 0 return exists $self->{$lkey};
387 }
388  
389 sub FETCH
390 0 ▶{
Subroutine FETCH: not called CC=5 SCAR=34.0
391 0 my $self = shift;
392 0 my $key = shift;
393 0 my $lkey = lc($key);
394  
395 0 ▶ if ($lkey eq 'priority')
Branch: 2 branches
BranchTrueFalseTotal
if ($lkey eq 'priority') { } 0 0 0
elsif ($lkey eq 'last' and not exists $self->{$lkey}) { } 0 0 0
396 {
397 #Get the priority setting from the system
398 0 return getpriority PRIO_shadow,$self->{Node}->{Id};
399 }
400 0 ▶ elsif ($lkey eq 'last' && !exists $self->{$lkey}) {&EXISTS($self,$lkey);}
Condition0%0/3
$lkey eq "last" and not exists $self->{$lkey}
!ll&&!rl&&r
000
MC/DC0%0/2
$lkey eq "last" and not exists $self->{$lkey}
$lkey eq "last" not exists $self->{$lkey}
Truth table0%0/3
A and not(B)
A= $lkey eq "last"B= not exists $self->{$lkey}
ABresult
0X0
100
111
401  
402 0 ▶ if (exists $self->{$lkey}) {return $self->{$lkey};}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$lkey}) 0 0 0
403 }
404  
405 # Maps the changes to a particular setting to a flag on the command line
406 1 my $shadowmod_opt =
407 {
408 comment => '-c',
409 home => '-d',
410 expire => '-e',
411 inactive=> '-f',
412 name => '-l',
413 shell => '-s',
414 group1 => '-g',
415 groups => '-G',
416 id => '-u',
417 };
418  
419 1 my $shadowmod_opt2 =
420 {
421 id => '-o',
422 };
423  
424 1 my $shadowmod = '/usr/sbin/shadowmod'; #Hard path to shadowmod
425 1 my $shadowdel = '/usr/sbin/shadowdel'; #Hard path to shadowdel
426  
427 sub STORE
428 0 ▶{
Subroutine STORE: not called CC=10 SCAR=47.0
429 # Changes a setting for the specified shadow... we basically call shadowmod
430 0 my ($self,$key,$val) = @_;
431 0 my $lkey = lc($key);
432  
433 0 ▶ if ($lkey eq 'groups')
Branch: 2 branches
BranchTrueFalseTotal
if ($lkey eq 'groups') { } 0 0 0
elsif (exists $shadowmod_opt->{$lkey}) { } 0 0 0
434 {
435 #Handle the groups thing...
436  
437 #$val is a list reference....
438 0 my ($i,@g) = @{$val};
439  
440 0 system "$shadowmod $self->{name} -g $i -G ". join(',', @g);
441 }
442 elsif (exists $shadowmod_opt->{$lkey})
443 {
444 #This is something for shadow mod!
445 0 my ($FSUp, @FSet)=(0);
446  
447 0 ▶ if (defined $CfgTie::TieShadow'Chg_FS && $shadow'Chg_FS == 1 &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $CfgTie::TieShadow::Chg_FS and $shadow::Chg_FS == 1 and $lkey eq "id") 0 0 0
Condition0%0/6
defined $CfgTie::TieShadow::Chg_FS and $shadow::Chg_FS == 1 and $lkey eq "id"
!ll&&!rl&&r
000
defined $CfgTie::TieShadow::Chg_FS and $shadow::Chg_FS == 1
!ll&&!rl&&r
000
MC/DC0%0/3
defined $CfgTie::TieShadow::Chg_FS and $shadow::Chg_FS == 1 and $lkey eq "id"
defined $CfgTie::TieShadow::Chg_FS $shadow::Chg_FS == 1 $lkey eq "id"
Truth table0%0/4
A and B and C
A= defined $CfgTie::TieShadow::Chg_FSB= $shadow::Chg_FS == 1C= $lkey eq "id"
ABCresult
0XX0
10X0
1100
1111
448 $lkey eq 'id')
449 {
450 #We are supposed to change all of the files in the system to
451 #have the new new one.
452 #We have a race condition: someone else may change soemthing in
453 #the file system, so we may not get everything, but we try to
454 #be reasonable
455  
456 # Now we need to identify all of the files.. this may take a
457 # long time
458 0 @FSet= &CfgTie::filever'find_by_user ('/', $self->{id});
459  
460 0 ▶ if (@FSet && scalar @FSet) {$FSUp = 1;}
Branch: 1 branch
BranchTrueFalseTotal
if (@FSet and scalar @FSet) 0 0 0
Condition0%0/2
@FSet and scalar @FSet
!ll
00
MC/DC0%0/1
@FSet and scalar @FSet
@FSet
Truth table0%0/2
A
A= @FSet
Aresult
00
11
461 }
462  
463 0 my $X = $shadowmod_opt2->{$lkey};
464 0 ▶ if (!defined $X) {$X='';}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $X) 0 0 0
465  
466 #Change the system tables
467 system "$shadowmod ".$shadowmod_opt->{$lkey}." $val $X ".$self->{name}.
468 0 "\n";
469 #If bad things should throw exception
470  
471 0 ▶ if ($FSUp) {chown $val,-1, @FSet;}
Branch: 1 branch
BranchTrueFalseTotal
if ($FSUp) 0 0 0
472  
473 0 $self->{$lkey} = $val;
474 }
475 else
476 {
477 #Extra setting that will be lost... 8(
478 0 $self->{$lkey}=$val;
479 }
480 }
481  
482 sub DELETE
483 0 ▶{
Subroutine DELETE: not called CC=3 SCAR=24.8
484 #Deletes a shadow setting
485 0 my ($self, $key) = @_;
486 0 my $lkey=lc($key);
487  
488 0 ▶ if ($key eq 'authmethod')
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'authmethod') { } 0 0 0
elsif (exists $shadowmod_opt->{$key}) { } 0 0 0
489 {
490 0 system "$shadowmod -A DEFAULT ".$self->{name};
491 }
492 elsif (exists $shadowmod_opt->{$key})
493 {
494 #This is something for shadow mod!
495 0 system "$shadowmod ".$shadowmod_opt->{$lkey}." ".$self->{name};
496 }
497 else
498 {
499 #Just remove our local copy
500 0 delete $self->{$lkey};
501 }
502 }
503  
504  
« CfgTie/TieUser.pm ↑ Summary CfgTie/TieNamed.pm »