Export/Unix/UserDB.pm

filter: Statementstmt 86.7% 320 / 369 Branchbran 60.6% 171 / 282 Conditioncond 56.9% 70 / 123 MC/DCmcdc 30.9% 22 / 71 Subroutinesub 88.4% 69 / 78 Podpod 100% 18 / 18 totaltotal 71.2% 670 / 941 CC 213 SCAR 32.7
CC
213
Cov
72%
import_user
155.1
load
40.1
_generate_linux_passwd_format
21.6
CRAP 26.4
« Export/ISO9660.pm ↑ Summary Export/GPT.pm »
1 package Sys::Export::Unix::UserDB;
2  
3 # ABSTRACT: Abstractions for Unix passwd/group/shadow files
4 2 our $VERSION = '0.006'; # VERSION
5  
6 2 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Spec::Functions qw( catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use User::pwent qw( getpwnam pw_has );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Sys::Export qw( isa_userdb isa_user isa_group );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # making lexical subs allows these to be seen by inner packages as well
17 # and removes need for namespace::clean
18 2 ▶my sub carp { goto \&Carp::carp }
Subroutine carp: called CC=1 SCAR=0.0
19 9 ▶my sub croak { goto \&Carp::croak }
Subroutine croak: called CC=1 SCAR=0.0
20 51 ▶my sub isa_hash :prototype($) { ref $_[0] eq 'HASH' }
Subroutine isa_hash: called CC=1 SCAR=0.0
21 24 ▶my sub isa_array :prototype($) { ref $_[0] eq 'ARRAY' }
Subroutine isa_array: called CC=1 SCAR=0.0
22 119 ▶my sub isa_int :prototype($) { Scalar::Util::looks_like_number($_[0]) && int($_[0]) == $_[0] }
Subroutine isa_int: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($_[0]) and int $_[0] == $_[0] 13 106 119
23  
24  
25 13 ▶sub new($class, @args) {
Subroutine new: called CC=16 SCAR=30.0
Pod documented
26 13 ▶ my %args= @args == 1 && isa_hash($args[0])? $args[0]->%* : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 && isa_hash($args[0]) ? : 1 12 13
Condition66%2/3
@args == 1 and isa_hash($args[0])
!ll&&!rl&&r
1201
MC/DC50%1/2
@args == 1 and isa_hash($args[0])
@args == 1 isa_hash($args[0])
Truth table66%2/3
A and B
A= @args == 1B= isa_hash($args[0])
ABresult
0X0
100
111
27 13 my $self = bless {
28 users => {},
29 uids => {},
30 groups => {},
31 gids => {},
32 }, $class;
33 # I don't want to declare this as an official attribute because a subclass might decide
34 # to implement is_valid_name using something other than a regex, and then the attribute
35 # would be inconsistent.
36 13 ▶ $self->{valid_name_regex}= $args{valid_name_regex} if defined $args{valid_name_regex};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"valid_name_regex"} 0 13 13
37 # Extract initial user and group lists before initializing attributes, then apply afterward
38 13 my $g= delete $args{groups};
39 13 my $u= delete $args{users};
40  
41 # Apply writable attributes and/or initial methods
42 13 for (keys %args) {
43 5 ▶ croak "Unknown option '$_'" unless $self->can($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($_) 1 4 5
44 4 $self->$_($args{$_});
45 }
46 12 ▶ if ($g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g) 2 10 12
47 2 ▶ if (isa_array $g) {
Branch: 2 branches
BranchTrueFalseTotal
if (isa_array $g) { } 0 2 2
elsif (isa_hash $g) { } 2 0 2
48 0 $self->add_group($_) for $g->@*;
49 } elsif (isa_hash $g) {
50 2 $self->add_group($_ => $g->{$_}->%*) for keys %$g;
51 } else {
52 0 croak "Option 'groups' must be arrayref or hashref of group objects";
53 }
54 }
55 12 ▶ if ($u) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u) 2 10 12
56 2 ▶ if (isa_array $u) {
Branch: 2 branches
BranchTrueFalseTotal
if (isa_array $u) { } 0 2 2
elsif (isa_hash $u) { } 2 0 2
57 0 $self->add_user($_) for $u->@*;
58 } elsif (isa_hash $u) {
59 2 $self->add_user($_ => $u->{$_}->%*) for keys %$u;
60 } else {
61 0 croak "Option 'users' must be arrayref or hashref of user objects";
62 }
63 }
64 12 return $self;
65 }
66  
67  
68 13 ▶sub users($self) { $self->{users} }
Subroutine users: called CC=1 SCAR=0.0
Pod documented
69  
70 1 ▶sub uids($self) { $self->{uids} }
Subroutine uids: called CC=1 SCAR=0.0
Pod documented
71  
72 5 ▶sub groups($self) { $self->{groups} }
Subroutine groups: called CC=1 SCAR=0.0
Pod documented
73  
74 1 ▶sub gids($self) { $self->{gids} }
Subroutine gids: called CC=1 SCAR=0.0
Pod documented
75  
76  
77 66 ▶sub auto_import($self, @val) {
Subroutine auto_import: called CC=3 SCAR=11.0
Pod documented
78 # coerce anything other than a userdb into a boolean
79 @val? ($self->{auto_import}= isa_userdb($val[0])? $val[0] : !!$val[0]) : $self->{auto_import}
80 66 ▶}
Branch: 2 branches
BranchTrueFalseTotal
@val ? : 3 63 66
&isa_userdb($val[0]) ? : 1 2 3
81  
82  
83 1 ▶sub clone($self) {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
84 1 return dclone($self);
85 }
86  
87  
88 91 ▶sub is_valid_name($self, $name) {
Subroutine is_valid_name: called CC=2 SCAR=7.6
Pod documented
89 defined $self->{valid_name_regex}? scalar( $name =~ $self->{valid_name_regex} )
90 91 ▶ : scalar( $name =~ /^[A-Za-z_][-A-Za-z0-9_.]{0,30}[-A-Za-z0-9_.\$]?\z/ )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'valid_name_regex'} ? : 0 91 91
91 }
92  
93  
94 3 ▶sub load($self, $path, %options) {
Subroutine load: called CC=23 SCAR=36.9
Pod documented
95 3 ▶ croak "Path is required" unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 3 3
96 3 my $passwd_file = catfile($path, 'passwd');
97 3 my $group_file = catfile($path, 'group');
98 3 my $shadow_file = catfile($path, 'shadow');
99  
100 3 ▶ unless ($options{format}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"format"}) 3 0 3
101 # If we have passwd, group, and shadow, assume Linux.
102 3 ▶ $options{format}= (-f $passwd_file && -f $group_file && -f $shadow_file)? 'Linux'
Branch: 4 branches
BranchTrueFalseTotal
-f $passwd_file && -f $group_file && -f $shadow_file ? : 2 1 3
-f $passwd_file ? : 0 1 1
-f $group_file ? : 0 1 1
-f $shadow_file ? : 0 1 1
Condition66%4/6
-f $passwd_file and -f $group_file and -f $shadow_file
!ll&&!rl&&r
102
-f $passwd_file and -f $group_file
!ll&&!rl&&r
102
MC/DC33%1/3
-f $passwd_file and -f $group_file and -f $shadow_file
-f $passwd_file -f $group_file -f $shadow_file
Truth table50%2/4
A and B and C
A= -f $passwd_fileB= -f $group_fileC= -f $shadow_file
ABCresult
0XX0
10X0
1100
1111
103 : croak "Unable to detect format: passwd ".(-f $passwd_file? "[found]":"[not found]")
104 ." group ".(-f $group_file? "[found]" : "[not found]")
105 ." shadow ".(-f $shadow_file? "[found]" : "[not found]");
106 }
107 2 my $records;
108 2 ▶ if ($options{format} eq 'Linux') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'format'} eq 'Linux') { } 2 0 2
109 2 ▶ $records= $self->_parse_linux_passwd_format({
Branch: 1 branch
BranchTrueFalseTotal
-r $shadow_file ? : 2 0 2
110 passwd => _slurp($passwd_file),
111 group => _slurp($group_file),
112 (-r $shadow_file? ( shadow => _slurp($shadow_file) ) : ()),
113 });
114 } else {
115 0 croak "Unsupported format '$options{format}'";
116 }
117  
118 # convert user primary gid to group name
119 2 my (%group_by_name, %group_by_gid, %user_by_name);
120 2 for ($records->{groups}->@*) {
121 9 ▶ $group_by_name{$_->{name}} //= $_;
Condition33%1/3
$group_by_name{$_->{"name"}} //= $_
l!l&&r!l&&!r
090
MC/DC0%0/2
$group_by_name{$_->{"name"}} //= $_
$group_by_name{$_->{"name"}} $_
Truth table33%1/3
A //= B
A= $group_by_name{$_->{"name"}}B= $_
ABresult
1X1
011
000
122 9 ▶ $group_by_gid{$_->{gid}} //= $_;
Condition33%1/3
$group_by_gid{$_->{"gid"}} //= $_
l!l&&r!l&&!r
090
MC/DC0%0/2
$group_by_gid{$_->{"gid"}} //= $_
$group_by_gid{$_->{"gid"}} $_
Truth table33%1/3
A //= B
A= $group_by_gid{$_->{"gid"}}B= $_
ABresult
1X1
011
000
123 }
124 2 for ($records->{users}->@*) {
125 7 ▶ $user_by_name{$_->{name}} //= $_;
Condition33%1/3
$user_by_name{$_->{"name"}} //= $_
l!l&&r!l&&!r
070
MC/DC0%0/2
$user_by_name{$_->{"name"}} //= $_
$user_by_name{$_->{"name"}} $_
Truth table33%1/3
A //= B
A= $user_by_name{$_->{"name"}}B= $_
ABresult
1X1
011
000
126 7 my $gid= delete $_->{gid};
127 7 ▶ if (my $primary_group= $group_by_gid{$gid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $primary_group = $group_by_gid{$gid}) { } 7 0 7
128 7 $_->{group}= $primary_group->{name};
129 } else {
130 0 carp "User '$_->{name}' references non-existent gid $gid";
131 # This allows the gid to pass through for a round trip, but will generate
132 # warning in various places.
133 0 $_->{group}= $gid;
134 }
135 }
136  
137 # move group membership to user records
138 2 for my $g ($records->{groups}->@*) {
139 9 ▶ if (my $members= delete $g->{members}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $members = delete $g->{"members"}) 4 5 9
140 4 for (split ',', $members) {
141 my $u= $user_by_name{$_}
142 0 ▶ or do { carp "Group '$g->{name}' references non-existent user '$_'"; next; };
Branch: 1 branch
BranchTrueFalseTotal
my $u = $user_by_name{$_} or do { carp qq[Group '$g->{"name"}' references non-existent user '${_}']; next } 6 0 6
143 6 push @{$u->{groups}}, $g->{name};
144 }
145 }
146 }
147  
148 $self->_add_group_object(Sys::Export::Unix::UserDB::Group->new(%$_))
149 2 for $records->{groups}->@*;
150 $self->_add_user_object(Sys::Export::Unix::UserDB::User->new(%$_))
151 2 for $records->{users}->@*;
152  
153 2 return $self;
154 }
155  
156  
157 4 ▶sub save($self, $target, %options) {
Subroutine save: called CC=9 SCAR=22.8
Pod documented
158 4 ▶ my $format= $options{format} // $self->{default_format} // 'Linux';
Condition40%2/5
($options{"format"} // $self->{"default_format"}) // "Linux"
l!l
04
$options{"format"} // $self->{"default_format"}
l!l&&r!l&&!r
004
MC/DC0%0/2
($options{"format"} // $self->{"default_format"}) // "Linux"
$options{"format"} $self->{"default_format"}
Truth table33%1/3
A // B
A= $options{"format"}B= $self->{"default_format"}
ABresult
1X1
011
000
159 4 my $data;
160 4 ▶ if ($format eq 'Linux') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'Linux') { } 4 0 4
161 4 $data= $self->_generate_linux_passwd_format(%options);
162 } else {
163 0 croak "Unsupported format $options{format}";
164 }
165 4 ▶ if (isa_hash $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (isa_hash $target) { } 2 2 4
166 2 %$target= %$data;
167 } else {
168 2 ▶ croak "path does not exist: '$target'" unless -e $target;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $target 0 2 2
169 2 for (keys %$data) {
170 6 ▶ my $public= $_ eq 'passwd' || $_ eq 'group';
Condition100%3/3
$_ eq 'passwd' || $_ eq 'group'
l!l&&r!l&&!r
222
MC/DC100%2/2
$_ eq 'passwd' || $_ eq 'group'
$_ eq 'passwd' $_ eq 'group'
Truth table100%3/3
A || B
A= $_ eq 'passwd'B= $_ eq 'group'
ABresult
1X1
011
000
171 6 ▶ _mkfile(catfile($target, $_), $data->{$_}, $public? 0755 : 0700);
Branch: 1 branch
BranchTrueFalseTotal
$public ? : 4 2 6
172 }
173 }
174 4 return $self;
175 }
176  
177 6 ▶sub _mkfile($name, $data, $mode=undef) {
Subroutine _mkfile: called CC=7 SCAR=23.7
178 6 ▶ open my $fh, '>:raw', $name or croak "open(>$name): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $name or croak "open(>$name): $!" 6 0 6
179 6 ▶ $fh->print($data) or croak "write($name): $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($data) or croak "write($name): $!" 6 0 6
180 6 ▶ $fh->close or croak "close($name): $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak "close($name): $!" 6 0 6
181 6 ▶ chmod $mode, $name or croak "chmod($name, $mode): $!"
Branch: 1 branch
BranchTrueFalseTotal
if defined $mode 6 0 6
Condition33%2/6
defined $mode && (chmod($mode, $name) || croak("chmod($name, $mode): $!"))
!ll
060
chmod $mode, $name or croak "chmod($name, $mode): $!"
l!l
600
MC/DC0%0/3
defined $mode && (chmod($mode, $name) || croak("chmod($name, $mode): $!"))
defined $mode chmod $mode, $name croak "chmod($name, $mode): $!"
Truth table0%0/4
A && B or C
A= defined $modeB= chmod $mode, $nameC= croak "chmod($name, $mode): $!"
ABCresult
0XX0
1000
11X1
1011
182 if defined $mode;
183 }
184 6 ▶sub _slurp($name) {
Subroutine _slurp: called CC=3 SCAR=11.3
185 6 ▶ open my $fh, '<:raw', $name or die "open(<$name): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $name or die "open(<$name): $!" 6 0 6
186 6 local $/;
187 6 my $ret= scalar <$fh>;
188 6 ▶ close $fh or die "close($name): $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "close($name): $!" 6 0 6
189 6 $ret;
190 }
191  
192 2 our @_linux_shadow_fields= qw( passwd pw_change_time pw_min_days pw_max_days pw_warn_days pw_inactive_days expire_time );
193  
194 # Unix time ignores leap seconds, so the conversion from seconds to days is simple division
195 10 ▶sub _time_to_days_since_1970($t) { !length $t? undef : int($t / 86400) }
Subroutine _time_to_days_since_1970: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!length($t) ? : 6 4 10
196 8 ▶sub _days_since_1970_to_time($d) { !length $d? undef : $d * 86400; }
Subroutine _days_since_1970_to_time: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
!length($d) ? : 4 4 8
197  
198 2 ▶sub _parse_linux_passwd_format($self, $files) {
Subroutine _parse_linux_passwd_format: called CC=10 SCAR=24.0
199 2 my @users;
200 my %users;
201 2 for (split "\n", $files->{passwd}) {
202 7 ▶ next if /^\s*(#|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(#|\z)/u 0 7 7
203 7 my %r;
204 7 @r{qw( name pw_flag uid gid gecos dir shell )}= split ':';
205 7 delete $r{pw_flag};
206 7 push @users, \%r;
207 7 ▶ $users{$r{name}} //= \%r;
Condition50%1/2
$users{$r{"name"}} //= \%r
l!l
07
MC/DC0%0/1
$users{$r{"name"}} //= \%r
$users{$r{"name"}}
Truth table50%1/2
A
A= $users{$r{"name"}}
Aresult
11
00
208 }
209  
210 2 ▶ for (split "\n", ($files->{shadow}//'')) {
Condition50%1/2
$files->{"shadow"} // ""
l!l
20
MC/DC0%0/1
$files->{"shadow"} // ""
$files->{"shadow"}
Truth table50%1/2
A
A= $files->{"shadow"}
Aresult
11
00
211 4 ▶ next if /^\s*(#|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(#|\z)/u 0 4 4
212 4 my ($name,@vals)= split ':';
213 my $r= $users{$name}
214 0 ▶ or do { carp "Found shadow entry for non-existent user '$name'"; next; };
Branch: 1 branch
BranchTrueFalseTotal
my $r = $users{$name} or do { carp "Found shadow entry for non-existent user '${name}'"; next } 4 0 4
215 4 @{$r}{@_linux_shadow_fields}= @vals;
216 4 $r->{pw_change_time}= _days_since_1970_to_time($r->{pw_change_time});
217 4 $r->{expire_time}= _days_since_1970_to_time($r->{expire_time});
218 }
219  
220 2 my @groups;
221 2 for (split "\n", $files->{group}) {
222 9 ▶ next if /^\s*(#|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(#|\z)/u 0 9 9
223 9 my %r;
224 9 @r{qw( name passwd gid members )}= split ":";
225 9 push @groups, \%r;
226 }
227 2 return { users => \@users, groups => \@groups };
228 }
229  
230 4 ▶sub _generate_linux_passwd_format($self, %options) {
Subroutine _generate_linux_passwd_format: called CC=20 SCAR=30.7
231 4 my ($passwd, $group, $shadow)= ('','','');
232 4 my @users= sort { $a->uid <=> $b->uid } values $self->{users}->%*;
233 4 my @groups= sort { $a->gid <=> $b->gid } values $self->{groups}->%*;
234  
235 # Generate passwd content
236 4 for my $user (@users) {
237 9 my $gid;
238 9 ▶ if ($user->group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user->group) { } 9 0 9
239 9 ▶ my $group= $self->group($user->group)
Branch: 1 branch
BranchTrueFalseTotal
my $group = $self->group($user->group) or croak q[User '] . $user->name . q[' has invalid group '] . $user->group . q['] 9 0 9
240 or croak "User '".$user->name."' has invalid group '".$user->group."'";
241 9 $gid= $group->gid;
242 } else {
243 0 ▶ $gid= $user->gid
Branch: 1 branch
BranchTrueFalseTotal
$gid = $user->gid or croak q[User '] . $user->name . q[' lacks 'group' or 'gid' attribute] 0 0 0
244 or croak "User '".$user->name."' lacks 'group' or 'gid' attribute";
245 }
246 # If shadow fields exist, write a 'x' in passwd, else '*'.
247 9 my $pw_flag= '*';
248 9 ▶ if (grep defined, @{$user}{@_linux_shadow_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep defined($_), @{$user;}{@Sys::Export::Unix::UserDB::_linux_shadow_fields}) 5 4 9
249 5 $pw_flag= 'x';
250 5 ▶ $shadow .= sprintf "%s:%s:%s:%s:%s:%s:%s:%s:%s\n",
Condition78%11/14
$user->passwd // "*"
l!l
50
_time_to_days_since_1970($user->pw_change_time) // ""
l!l
41
$user->pw_min_days // ""
l!l
14
$user->pw_max_days // ""
l!l
14
$user->pw_warn_days // ""
l!l
14
$user->pw_inactive_days // ""
l!l
05
_time_to_days_since_1970($user->expire_time) // ""
l!l
05
MC/DC0%0/1
$user->passwd // "*"
$user->passwd
MC/DC100%1/1
_time_to_days_since_1970($user->pw_change_time) // ""
_time_to_days_since_1970($user->pw_change_time)
MC/DC100%1/1
$user->pw_min_days // ""
$user->pw_min_days
MC/DC100%1/1
$user->pw_max_days // ""
$user->pw_max_days
MC/DC100%1/1
$user->pw_warn_days // ""
$user->pw_warn_days
MC/DC0%0/1
$user->pw_inactive_days // ""
$user->pw_inactive_days
MC/DC0%0/1
_time_to_days_since_1970($user->expire_time) // ""
_time_to_days_since_1970($user->expire_time)
Truth table50%1/2
A
A= $user->passwd
Aresult
11
00
Truth table100%2/2
A
A= _time_to_days_since_1970($user->pw_change_time)
Aresult
11
00
Truth table100%2/2
A
A= $user->pw_min_days
Aresult
11
00
Truth table100%2/2
A
A= $user->pw_max_days
Aresult
11
00
Truth table100%2/2
A
A= $user->pw_warn_days
Aresult
11
00
Truth table50%1/2
A
A= $user->pw_inactive_days
Aresult
11
00
Truth table50%1/2
A
A= _time_to_days_since_1970($user->expire_time)
Aresult
11
00
251 $user->name,
252 $user->passwd // '*',
253 _time_to_days_since_1970($user->pw_change_time) // '',
254 $user->pw_min_days // '',
255 $user->pw_max_days // '',
256 $user->pw_warn_days // '',
257 $user->pw_inactive_days // '',
258 _time_to_days_since_1970($user->expire_time) // '',
259 ''; # reserved field, no idea what to name it in user object...
260 }
261 9 ▶ $passwd .= sprintf "%s:%s:%d:%d:%s:%s:%s\n",
Condition100%6/6
$user->gecos // ""
l!l
72
$user->dir // ""
l!l
72
$user->shell // ""
l!l
72
MC/DC100%1/1
$user->gecos // ""
$user->gecos
MC/DC100%1/1
$user->dir // ""
$user->dir
MC/DC100%1/1
$user->shell // ""
$user->shell
Truth table100%2/2
A
A= $user->gecos
Aresult
11
00
Truth table100%2/2
A
A= $user->dir
Aresult
11
00
Truth table100%2/2
A
A= $user->shell
Aresult
11
00
262 $user->name, $pw_flag, $user->uid, $gid,
263 $user->gecos//'', $user->dir//'', $user->shell//'';
264 }
265  
266 # Generate group content
267 4 for my $g (@groups) {
268 12 my $grnam= $g->name;
269 # Collect members from users who have this group, excluding users whose primary
270 # group is already the group.
271 12 ▶ my @members= map $_->name, grep $_->groups->{$grnam} && ($_->group//'') ne $grnam, @users;
Condition80%4/5
$_->groups->{$grnam} && ($_->group // '') ne $grnam
!ll&&!rl&&r
1766
$_->group // ""
l!l
120
MC/DC50%1/2
$_->groups->{$grnam} && ($_->group // '') ne $grnam
$_->groups->{$grnam} $_->group
Truth table66%2/3
A && B
A= $_->groups->{$grnam}B= $_->group
ABresult
0X0
100
111
272 12 ▶ $group .= sprintf "%s:%s:%d:%s\n",
Condition100%2/2
$g->passwd // "*"
l!l
57
MC/DC100%1/1
$g->passwd // "*"
$g->passwd
Truth table100%2/2
A
A= $g->passwd
Aresult
11
00
273 $grnam, $g->passwd // '*', $g->gid, join ',', sort @members;
274 }
275  
276 4 return { passwd => $passwd, group => $group, shadow => $shadow };
277 }
278  
279  
280 3 ▶sub import_user($self, $name_or_obj, %attrs) {
Subroutine import_user: called CC=29 SCAR=50.4
Pod documented
281 3 ▶ ref $name_or_obj or length $name_or_obj or croak "Attempt to import empty username";
Branch: 1 branch
BranchTrueFalseTotal
ref $name_or_obj or length $name_or_obj or croak "Attempt to import empty username" 3 0 3
Condition66%2/3
ref $name_or_obj or length $name_or_obj
l!l&&r!l&&!r
210
MC/DC0%0/2
ref $name_or_obj or length $name_or_obj
ref $name_or_obj length $name_or_obj
Truth table66%2/3
A or B
A= ref $name_or_objB= length $name_or_obj
ABresult
1X1
011
000
282 3 ▶ if (isa_hash($name_or_obj) || isa_user($name_or_obj)) {
Branch: 2 branches
BranchTrueFalseTotal
if (isa_hash $name_or_obj or &isa_user($name_or_obj)) { } 2 1 3
elsif (keys %attrs) { } 0 1 1
Condition66%2/3
isa_hash $name_or_obj or &isa_user($name_or_obj)
l!l&&r!l&&!r
021
MC/DC50%1/2
isa_hash $name_or_obj or &isa_user($name_or_obj)
isa_hash $name_or_obj &isa_user($name_or_obj)
Truth table66%2/3
A or B
A= isa_hash $name_or_objB= &isa_user($name_or_obj)
ABresult
1X1
011
000
283 2 %attrs= ( %$name_or_obj, %attrs );
284 } elsif (keys %attrs) {
285 0 $attrs{name}= "$name_or_obj";
286 } else {
287 1 ▶ my $pw= getpwnam($name_or_obj)
Branch: 1 branch
BranchTrueFalseTotal
my $pw = &Sys::Export::Unix::UserDB::getpwnam($name_or_obj) or croak "User '${name_or_obj}' not found in system" 1 0 1
288 or croak "User '$name_or_obj' not found in system";
289 1 $attrs{name}= $pw->name;
290 1 $attrs{passwd}= $pw->passwd;
291 1 $attrs{uid}= $pw->uid;
292 1 ▶ $attrs{quota}= $pw->quota if pw_has('quota'); # BSD
Branch: 1 branch
BranchTrueFalseTotal
if pw_has("quota") 0 1 1
293 1 ▶ $attrs{class}= $pw->class if pw_has('class'); # BSD
Branch: 1 branch
BranchTrueFalseTotal
if pw_has("class") 0 1 1
294 1 ▶ $attrs{comment}= $pw->comment if pw_has('comment'); # always empty on Linux and BSD...
Branch: 1 branch
BranchTrueFalseTotal
if pw_has("comment") 0 1 1
295 1 ▶ $attrs{gecos}= $pw->gecos if pw_has('gecos');
Branch: 1 branch
BranchTrueFalseTotal
if pw_has("gecos") 1 0 1
296 1 $attrs{dir}= $pw->dir;
297 1 $attrs{shell}= $pw->shell;
298 1 ▶ if (pw_has('expire')) {
Branch: 1 branch
BranchTrueFalseTotal
if (pw_has("expire")) 0 1 1
299 # Normalize this field to epoch seconds, based on the current platform.
300 # FreeBSD has expire in seconds. Linux has an expire field of days, in /etc/shadow, but
301 # all tests on Linux so far have had pw_has('expire') = false.
302 # getpwnam dies with 'not implemented' on Strawberry perl for Windows.
303 0 ▶ $attrs{expire_time}= $^O eq 'FreeBSD'? $pw->expire
Branch: 2 branches
BranchTrueFalseTotal
$^O eq 'FreeBSD' ? : 0 0 0
$^O eq 'linux' ? : 0 0 0
304 : $^O eq 'linux'? _days_since_1970_to_time($pw->expire)
305 : undef;
306 }
307 # convert gid to group name
308 1 my $gid= $pw->gid;
309 1 ▶ if (my $grnam= getgrgid($gid)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $grnam = getgrgid $gid) { } 1 0 1
elsif ($gid == $pw->uid or $pw->uid < 1000) { } 0 0 0
310 1 $attrs{group}= $grnam;
311 ▶ } elsif ($gid == $pw->uid || $pw->uid < 1000) {
Condition0%0/3
$gid == $pw->uid or $pw->uid < 1000
l!l&&r!l&&!r
000
MC/DC0%0/2
$gid == $pw->uid or $pw->uid < 1000
$gid == $pw->uid $pw->uid < 1000
Truth table0%0/3
A or B
A= $gid == $pw->uidB= $pw->uid < 1000
ABresult
1X1
011
000
312 # If the group wasn't found, but the uid and gid are identical and look like system
313 # accounts then assume the group will be the same name.
314 0 $attrs{group}= $pw->name;
315 } else {
316 0 carp "User '$name_or_obj' primary group $gid doesn't exist";
317 0 $attrs{group}= 'nogroup'; # it has to be something. Could croak instead of this...
318 }
319 }
320 3 ▶ $attrs{group} //= 'nogroup';
Condition50%1/2
$attrs{"group"} //= "nogroup"
l!l
30
MC/DC0%0/1
$attrs{"group"} //= "nogroup"
$attrs{"group"}
Truth table50%1/2
A
A= $attrs{"group"}
Aresult
11
00
321 3 ▶ $attrs{groups} //= {};
Condition100%2/2
$attrs{"groups"} //= {}
l!l
21
MC/DC100%1/1
$attrs{"groups"} //= {}
$attrs{"groups"}
Truth table100%2/2
A
A= $attrs{"groups"}
Aresult
11
00
322  
323 # Check for UID collision
324 defined $attrs{uid}
325 3 ▶ or croak "Can't import user $attrs{name} without a 'uid'";
Branch: 1 branch
BranchTrueFalseTotal
defined $attrs{"uid"} or croak qq[Can't import user $attrs{"name"} without a 'uid'] 3 0 3
326 3 ▶ if ($self->{uids}{$attrs{uid}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"uids"}{$attrs{"uid"}}) 0 3 3
327 0 ▶ if ($attrs{uid} >= 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs{'uid'} >= 1000) { } 0 0 0
328 0 ▶ ++$attrs{uid} while exists $self->{uids}{$attrs{uid}};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"uids"}{$attrs{"uid"}} and do { ++$attrs{"uid"} } 0 0 0
329 } else {
330 0 for (101..999) {
331 0 ▶ if (!exists $self->{uids}{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"uids"}{$_}) 0 0 0
332 0 $attrs{uid}= $_;
333 0 last;
334 }
335 }
336 croak "No available UIDs below 1000 for $attrs{name}"
337 0 ▶ if $self->{uids}{$attrs{uid}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"uids"}{$attrs{"uid"}} 0 0 0
338 }
339 }
340  
341 # do the groups exist? Calling ->group will trigger auto_import if enabled.
342 3 for my $gname (grep !$self->group($_), $attrs{group}, keys $attrs{groups}->%*) {
343 # Is the group name the same as the user name? Try creating with GID = UID
344 0 ▶ if ($gname eq $attrs{name}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($gname eq $attrs{'name'}) { } 0 0 0
elsif ($gname =~ /^(nobody|nogroup)/u) { } 0 0 0
345 0 $self->import_group($gname, gid => $attrs{uid});
346 } elsif ($gname =~ /^(nobody|nogroup)/) {
347 0 $self->import_group($gname, gid => 65534);
348 } else {
349 0 croak "User '$attrs{name}' references non-existent group '$gname'";
350 }
351 }
352  
353 3 my $u= Sys::Export::Unix::UserDB::User->new(%attrs);
354 3 $self->_add_user_object($u);
355 3 return $u;
356 }
357  
358  
359 4 ▶sub import_group($self, $name_or_obj, %attrs) {
Subroutine import_group: called CC=11 SCAR=25.2
Pod documented
360 4 ▶ ref $name_or_obj or length $name_or_obj or croak "Attempt to import empty username";
Branch: 1 branch
BranchTrueFalseTotal
ref $name_or_obj or length $name_or_obj or croak "Attempt to import empty username" 4 0 4
Condition66%2/3
ref $name_or_obj or length $name_or_obj
l!l&&r!l&&!r
310
MC/DC0%0/2
ref $name_or_obj or length $name_or_obj
ref $name_or_obj length $name_or_obj
Truth table66%2/3
A or B
A= ref $name_or_objB= length $name_or_obj
ABresult
1X1
011
000
361 4 ▶ if (isa_hash($name_or_obj) || isa_group($name_or_obj)) {
Branch: 2 branches
BranchTrueFalseTotal
if (isa_hash $name_or_obj or &isa_group($name_or_obj)) { } 3 1 4
elsif (keys %attrs) { } 0 1 1
Condition66%2/3
isa_hash $name_or_obj or &isa_group($name_or_obj)
l!l&&r!l&&!r
031
MC/DC50%1/2
isa_hash $name_or_obj or &isa_group($name_or_obj)
isa_hash $name_or_obj &isa_group($name_or_obj)
Truth table66%2/3
A or B
A= isa_hash $name_or_objB= &isa_group($name_or_obj)
ABresult
1X1
011
000
362 3 %attrs= ( %$name_or_obj, %attrs );
363 } elsif (keys %attrs) {
364 0 $attrs{name}= "$name_or_obj";
365 } else {
366 1 ▶ my ($grnam, $passwd, $gid, $members) = getgrnam($name_or_obj)
Branch: 1 branch
BranchTrueFalseTotal
() = getgrnam $name_or_obj or croak "Group '${name_or_obj}' not found in system" 1 0 1
367 or croak "Group '$name_or_obj' not found in system";
368 1 $attrs{name}= $grnam;
369 1 $attrs{passwd}= $passwd;
370 1 $attrs{gid}= $gid;
371 1 $attrs{members}= $members;
372 }
373 4 my $members= delete $attrs{members};
374 4 my $g= Sys::Export::Unix::UserDB::Group->new(%attrs);
375 4 $self->_add_group_object($g);
376  
377 # Can't store member list in group, so store these for later when a user gets added
378 4 ▶ if (defined $members) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $members) 1 3 4
379 $self->_lazy_add_user_to_group($_, $attrs{name})
380 1 ▶ for (isa_array $members? @$members
Branch: 2 branches
BranchTrueFalseTotal
isa_array $members ? : 0 1 1
isa_hash $members ? : 0 1 1
381 : isa_hash $members? keys %$members
382 : split / /, $members);
383 }
384 4 return $g;
385 }
386  
387  
388 15 ▶sub add_user($self, $name_or_obj, %attrs) {
Subroutine add_user: called CC=7 SCAR=25.7
Pod documented
389 15 ▶ if (isa_hash($name_or_obj) || isa_user($name_or_obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isa_hash $name_or_obj or &isa_user($name_or_obj)) { } 1 14 15
Condition66%2/3
isa_hash $name_or_obj or &isa_user($name_or_obj)
l!l&&r!l&&!r
0114
MC/DC50%1/2
isa_hash $name_or_obj or &isa_user($name_or_obj)
isa_hash $name_or_obj &isa_user($name_or_obj)
Truth table66%2/3
A or B
A= isa_hash $name_or_objB= &isa_user($name_or_obj)
ABresult
1X1
011
000
390 1 %attrs= ( %$name_or_obj, %attrs );
391 } else {
392 14 my $name= "$name_or_obj";
393 14 ▶ if (keys %attrs) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %attrs) { } 14 0 14
elsif ($self->auto_import and not $self->{'users'}{$name}) { } 0 0 0
394 14 $attrs{name}= $name;
395 }
396 # trigger an import if just a name, and auto_import enabled
397 ▶ elsif ($self->auto_import && !$self->{users}{$name}) {
Condition0%0/3
$self->auto_import and not $self->{"users"}{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->auto_import and not $self->{"users"}{$name}
$self->auto_import not $self->{"users"}{$name}
Truth table0%0/3
A and not(B)
A= $self->auto_importB= not $self->{"users"}{$name}
ABresult
0X0
100
111
398 0 ▶ $self->user($name)
Branch: 1 branch
BranchTrueFalseTotal
$self->user($name) or croak "Failed to import user $name" 0 0 0
399 or croak "Failed to import user $name";
400 0 return $self;
401 }
402 }
403 15 $self->_add_user_object(Sys::Export::Unix::UserDB::User->new(%attrs));
404 }
405  
406  
407 14 ▶sub add_group($self, $name_or_obj, %attrs) {
Subroutine add_group: called CC=7 SCAR=25.7
Pod documented
408 14 ▶ if (isa_hash($name_or_obj) || isa_group($name_or_obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isa_hash $name_or_obj or &isa_group($name_or_obj)) { } 1 13 14
Condition66%2/3
isa_hash $name_or_obj or &isa_group($name_or_obj)
l!l&&r!l&&!r
0113
MC/DC50%1/2
isa_hash $name_or_obj or &isa_group($name_or_obj)
isa_hash $name_or_obj &isa_group($name_or_obj)
Truth table66%2/3
A or B
A= isa_hash $name_or_objB= &isa_group($name_or_obj)
ABresult
1X1
011
000
409 1 %attrs= ( %$name_or_obj, %attrs );
410 } else {
411 13 my $name= "$name_or_obj";
412 13 ▶ if (keys %attrs) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %attrs) { } 13 0 13
elsif ($self->auto_import and not $self->{'groups'}{$name}) { } 0 0 0
413 13 $attrs{name}= $name;
414 }
415 # trigger an import if just a name, and auto_import enabled
416 ▶ elsif ($self->auto_import && !$self->{groups}{$name}) {
Condition0%0/3
$self->auto_import and not $self->{"groups"}{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->auto_import and not $self->{"groups"}{$name}
$self->auto_import not $self->{"groups"}{$name}
Truth table0%0/3
A and not(B)
A= $self->auto_importB= not $self->{"groups"}{$name}
ABresult
0X0
100
111
417 0 ▶ $self->group($name)
Branch: 1 branch
BranchTrueFalseTotal
$self->group($name) or croak "Failed to import group $name" 0 0 0
418 or croak "Failed to import group $name";
419 0 return $self;
420 }
421 }
422 14 $self->_add_group_object(Sys::Export::Unix::UserDB::Group->new(%attrs));
423 }
424  
425  
426 16 ▶sub user($self, $spec) {
Subroutine user: called CC=11 SCAR=25.1
Pod documented
427 16 ▶ length $spec or return undef;
Branch: 1 branch
BranchTrueFalseTotal
length $spec or return undef 16 0 16
428 16 ▶ my $u= isa_int $spec? $self->{uids}{$spec} : $self->{users}{$spec};
Branch: 1 branch
BranchTrueFalseTotal
isa_int $spec ? : 5 11 16
429 16 ▶ if (!$u && $self->auto_import) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $u and $self->auto_import) 3 13 16
Condition100%3/3
not $u and $self->auto_import
!ll&&!rl&&r
1033
MC/DC100%2/2
not $u and $self->auto_import
not $u $self->auto_import
Truth table100%3/3
not(A) and B
A= not $uB= $self->auto_import
ABresult
0X0
100
111
430 3 ▶ if (isa_userdb $self->auto_import) {
Branch: 1 branch
BranchTrueFalseTotal
if (&isa_userdb(scalar $self->auto_import)) { } 2 1 3
431 2 ▶ my $peer_u= $self->auto_import->user($spec) // return undef;
Condition50%1/2
$self->auto_import->user($spec) // (return undef)
l!l
20
MC/DC0%0/1
$self->auto_import->user($spec) // (return undef)
$self->auto_import->user($spec)
Truth table50%1/2
A
A= $self->auto_import->user($spec)
Aresult
11
00
432 2 ▶ $u= eval { $self->import_user($peer_u) } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
$u = eval { do { $self->import_user($peer_u) } } or warn $@ 2 0 2
433 } else {
434 1 ▶ my $name= isa_int $spec? (getpwuid($spec) // return undef) : $spec;
Branch: 1 branch
BranchTrueFalseTotal
isa_int $spec ? : 0 1 1
Condition0%0/2
getpwuid $spec // (return undef)
l!l
00
MC/DC0%0/1
getpwuid $spec // (return undef)
getpwuid $spec
Truth table0%0/2
A
A= getpwuid $spec
Aresult
11
00
435 1 ▶ $u= eval { $self->import_user($name) } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
$u = eval { do { $self->import_user($name) } } or warn $@ 1 0 1
436 }
437 }
438 16 $u;
439 }
440  
441 4 ▶sub has_user($self, $spec) {
Subroutine has_user: called CC=2 SCAR=6.9
Pod documented
442 4 ▶ defined(isa_int $spec? $self->{uids}{$spec} : $self->{users}{$spec});
Branch: 1 branch
BranchTrueFalseTotal
isa_int $spec ? : 2 2 4
443 }
444  
445 69 ▶sub group($self, $spec) {
Subroutine group: called CC=11 SCAR=25.1
Pod documented
446 69 ▶ length $spec or return undef;
Branch: 1 branch
BranchTrueFalseTotal
length $spec or return undef 69 0 69
447 69 ▶ my $g= isa_int $spec? $self->{gids}{$spec} : $self->{groups}{$spec};
Branch: 1 branch
BranchTrueFalseTotal
isa_int $spec ? : 4 65 69
448 69 ▶ if (!$g && $self->auto_import) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $g and $self->auto_import) 4 65 69
Condition100%3/3
not $g and $self->auto_import
!ll&&!rl&&r
6324
MC/DC100%2/2
not $g and $self->auto_import
not $g $self->auto_import
Truth table100%3/3
not(A) and B
A= not $gB= $self->auto_import
ABresult
0X0
100
111
449 4 ▶ if (isa_userdb $self->auto_import) {
Branch: 1 branch
BranchTrueFalseTotal
if (&isa_userdb(scalar $self->auto_import)) { } 3 1 4
450 3 ▶ my $peer_g= $self->auto_import->group($spec) // return undef;
Condition50%1/2
$self->auto_import->group($spec) // (return undef)
l!l
30
MC/DC0%0/1
$self->auto_import->group($spec) // (return undef)
$self->auto_import->group($spec)
Truth table50%1/2
A
A= $self->auto_import->group($spec)
Aresult
11
00
451 3 ▶ $g= eval { $self->import_group($peer_g) } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
$g = eval { do { $self->import_group($peer_g) } } or warn $@ 3 0 3
452 } else {
453 1 ▶ my $name= isa_int $spec? (getgrgid($spec) // return undef) : $spec;
Branch: 1 branch
BranchTrueFalseTotal
isa_int $spec ? : 0 1 1
Condition0%0/2
getgrgid $spec // (return undef)
l!l
00
MC/DC0%0/1
getgrgid $spec // (return undef)
getgrgid $spec
Truth table0%0/2
A
A= getgrgid $spec
Aresult
11
00
454 1 ▶ $g= eval { $self->import_group($name) } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
$g = eval { do { $self->import_group($name) } } or warn $@ 1 0 1
455 }
456 }
457 69 $g;
458 }
459  
460 4 ▶sub has_group($self, $spec) {
Subroutine has_group: called CC=2 SCAR=6.9
Pod documented
461 4 ▶ defined(isa_int $spec? $self->{gids}{$spec} : $self->{groups}{$spec});
Branch: 1 branch
BranchTrueFalseTotal
isa_int $spec ? : 2 2 4
462 }
463  
464 # Private methods
465  
466 # Allows adding user to group before user is defined
467 0 ▶sub _lazy_add_user_to_group($self, $unam, $grnam) {
Subroutine _lazy_add_user_to_group: not called CC=2 SCAR=17.9
468 0 ▶ if (my $u= $self->{users}{$unam}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $u = $self->{'users'}{$unam}) { } 0 0 0
469 0 $u->add_group($grnam);
470 } else {
471 0 push $self->{_lazy_add_user_to_group}{$unam}->@*, $grnam;
472 }
473 }
474  
475 25 ▶sub _add_user_object($self, $user) {
Subroutine _add_user_object: called CC=13 SCAR=27.8
476 25 my $name = $user->name;
477 25 my $uid = $user->uid;
478 25 ▶ $self->is_valid_name($name) or croak "Invalid user name '$name'";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_valid_name($name) or croak "Invalid user name '${name}'" 25 0 25
479
480 # Check for name conflicts
481 croak "Username '$name' already exists"
482 25 ▶ if defined $self->{users}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"users"}{$name} 1 24 25
483
484 # Warn about UID conflicts
485 carp "UID $uid already exists for user '".$self->{uids}{$uid}->name."', now also used by '$name'"
486 24 ▶ if defined $self->{uids}{$uid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"uids"}{$uid} 1 23 24
487  
488 # Check for references to non-existent groups
489 # If auto_import is enabled, accessing ->group will trigger their creation.
490 24 ▶ for ((isa_int $user->group? () : ($user->group)), keys $user->groups->%*) {
Branch: 1 branch
BranchTrueFalseTotal
isa_int($user->group) ? : 0 24 24
491 39 ▶ $self->is_valid_name($_)
Branch: 1 branch
BranchTrueFalseTotal
$self->is_valid_name($_) or croak "Invalid group name '${_}' for user '${name}'" 39 0 39
492 or croak "Invalid group name '$_' for user '$name'";
493 # add the user temporarily so auto_import feature can see it
494 39 ▶ local $self->{users}{$name} = $user
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_import 6 33 39
495 if $self->auto_import;
496 39 ▶ croak "User '$name' references non-existent group '$_'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->group($_) 0 39 39
497 unless $self->group($_);
498 }
499  
500 # Add lazy group membership from earlier
501 24 ▶ if (my $lazy= delete $self->{_lazy_add_user_to_group}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lazy = delete $self->{"_lazy_add_user_to_group"}{$name}) 0 24 24
502 $self->{groups}{$_} && $user->add_group($_)
503 0 ▶ for @$lazy;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"groups"}{$_} and $user->add_group($_) 0 0 0
504 }
505  
506 24 ▶ $self->{uids}{$uid} //= $user;
Condition66%2/3
$self->{"uids"}{$uid} //= $user
l!l&&r!l&&!r
1230
MC/DC0%0/2
$self->{"uids"}{$uid} //= $user
$self->{"uids"}{$uid} $user
Truth table66%2/3
A //= B
A= $self->{"uids"}{$uid}B= $user
ABresult
1X1
011
000
507 24 $self->{users}{$name} = $user;
508 }
509  
510 27 ▶sub _add_group_object($self, $group) {
Subroutine _add_group_object: called CC=5 SCAR=16.2
511 27 my $name = $group->name;
512 27 my $gid = $group->gid;
513 27 ▶ $self->is_valid_name($name) or croak "Invalid group name '$name'";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_valid_name($name) or croak "Invalid group name '${name}'" 27 0 27
514
515 # Check for name conflicts
516 croak "Group name '$name' already exists"
517 27 ▶ if defined $self->{groups}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"groups"}{$name} 1 26 27
518
519 # Warn about GID conflicts
520 carp "GID $gid already exists for group '".$self->{gids}{$gid}->name."', now also used by '$name'"
521 26 ▶ if defined $self->{gids}{$gid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"gids"}{$gid} 1 25 26
522
523 26 ▶ $self->{gids}{$gid} //= $group;
Condition66%2/3
$self->{"gids"}{$gid} //= $group
l!l&&r!l&&!r
1250
MC/DC0%0/2
$self->{"gids"}{$gid} //= $group
$self->{"gids"}{$gid} $group
Truth table66%2/3
A //= B
A= $self->{"gids"}{$gid}B= $group
ABresult
1X1
011
000
524 26 $self->{groups}{$name} = $group;
525 }
526  
527  
528 2 package Sys::Export::Unix::UserDB::User {
529 2 ▶ use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
530 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
531 2 ▶ use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
532 2 our @CARP_NOT= qw( Sys::Export::Unix::UserDB );
533 2 our %known_attrs= map +($_ => 1), qw( name uid passwd group groups comment gecos dir shell
534 quota class pw_change_time pw_min_days pw_max_days pw_warn_days pw_inactive_days expire_time );
535 30 ▶ sub new($class, %attrs) {
Subroutine new: called CC=8 SCAR=20.8
536 my $self= bless {
537 name => delete $attrs{name},
538 uid => delete $attrs{uid},
539 group => delete $attrs{group},
540 30 groups => {},
541 }, $class;
542 30 ▶ croak "User 'name' is required" unless defined $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"name"} 1 29 30
543 29 ▶ croak "User 'uid' is required" unless defined $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"uid"} 1 28 29
544 28 ▶ unless (defined $self->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"group"}) 5 23 28
545 # pull primary group from first element of list if not provided
546 5 ▶ if (isa_array $attrs{groups}) {
Branch: 1 branch
BranchTrueFalseTotal
if (isa_array($attrs{"groups"})) 4 1 5
547 4 $self->{group}= $attrs{groups}[0];
548 }
549 5 ▶ croak "User primary 'group' is required" unless length $self->{group};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"group"} 1 4 5
550 }
551 27 for my $key (keys %attrs) {
552 83 ▶ carp "Unknown user attribute '$key'" unless $known_attrs{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $Sys::Export::Unix::UserDB::User::known_attrs{$key} 0 83 83
553 83 $self->$key($attrs{$key});
554 }
555 27 return $self;
556 }
557 1 ▶ sub clone($self, %attrs) {
Subroutine clone: called CC=1 SCAR=0.0
558 1 return ref($self)->new( %$self, %attrs );
559 }
560
561 # Read-only attributes
562 48 ▶ sub name($self) { $self->{name} }
Subroutine name: called CC=1 SCAR=0.0
563 56 ▶ sub uid($self) { $self->{uid} }
Subroutine uid: called CC=1 SCAR=0.0
564
565 # Writable attributes
566 84 ▶ sub group($self, @val) {
Subroutine group: called CC=2 SCAR=7.6
567 84 ▶ @val? ($self->{group}= $val[0]) : $self->{group};
Branch: 1 branch
BranchTrueFalseTotal
@val ? : 0 84 84
568 }
569
570 78 ▶ sub groups($self, @val) {
Subroutine groups: called CC=6 SCAR=19.4
571 78 ▶ if (@val) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val) 14 64 78
572 14 ▶ if (@val > 1 || !ref $val[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if (@val > 1 or not ref $val[0]) { } 0 14 14
elsif (isa_array($val[0])) { } 9 5 14
elsif (isa_hash($val[0])) { } 5 0 5
Condition33%1/3
@val > 1 or not ref $val[0]
l!l&&r!l&&!r
0014
MC/DC0%0/2
@val > 1 or not ref $val[0]
@val > 1 not ref $val[0]
Truth table33%1/3
A or not(B)
A= @val > 1B= not ref $val[0]
ABresult
1X1
011
000
573 0 $self->{groups}= { map +($_ => 1), @val };
574 } elsif (isa_array $val[0]) {
575 9 $self->{groups}= { map +($_ => 1), @{$val[0]} };
576 } elsif (isa_hash $val[0]) {
577 5 $self->{groups}= { %{$val[0]} };
578 } else {
579 0 $self->{groups}= { $val[0] => 1 }; # just stringify it
580 }
581 }
582 $self->{groups}
583 78 }
584  
585 7 ▶ sub add_group($self, $group_name) {
Subroutine add_group: called CC=1 SCAR=0.0
586 7 $self->{groups}{$group_name}= 1;
587 7 return $self;
588 }
589
590 1 ▶ sub remove_group($self, $group_name) {
Subroutine remove_group: called CC=1 SCAR=0.0
591 1 delete $self->{groups}{$group_name};
592 1 return $self;
593 }
594  
595 {
596 # Generate generic read/write accessors for all other known attributes
597 2 my $pl= join "\n",
598 map <<~PL, grep !__PACKAGE__->can($_), keys %known_attrs;
599 sub $_(\$self, \@val) {
600 \@val ? (\$self->{$_} = \$val[0]) : \$self->{$_};
601 }
602 PL
603 0 ▶ eval $pl." 1" or croak $@;
Subroutine class: not called CC=2 SCAR=7.2
Subroutine comment: not called CC=2 SCAR=7.2
Subroutine dir: called CC=2 SCAR=7.2
Subroutine expire_time: called CC=2 SCAR=7.2
Subroutine gecos: called CC=2 SCAR=7.2
Subroutine passwd: called CC=2 SCAR=7.2
Subroutine pw_change_time: called CC=2 SCAR=7.2
Subroutine pw_inactive_days: called CC=2 SCAR=7.2
Subroutine pw_max_days: called CC=2 SCAR=7.2
Subroutine pw_min_days: called CC=2 SCAR=7.2
Subroutine pw_warn_days: called CC=2 SCAR=7.2
Subroutine quota: not called CC=2 SCAR=7.2
Subroutine shell: called CC=2 SCAR=7.2
Branch: 14 branches
BranchTrueFalseTotal
@val ? : 0 0 0
@val ? : 0 0 0
@val ? : 12 11 23
@val ? : 4 5 9
@val ? : 13 11 24
@val ? : 11 11 22
@val ? : 5 6 11
@val ? : 4 5 9
@val ? : 4 5 9
@val ? : 4 5 9
@val ? : 4 5 9
@val ? : 0 0 0
@val ? : 12 11 23
eval $pl . ' 1' or croak $@ 2 0 2
604 }
605  
606 # Other generic read/write accessors
607 2 our $AUTOLOAD;
608 0 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=4 SCAR=30.0
609 0 my $attr= substr($AUTOLOAD, rindex($AUTOLOAD, ':')+1);
610 0 my $self= shift;
611 carp "Unknown user attribute '$attr'"
612 0 ▶ unless exists $known_attrs{$attr} || exists $self->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Sys::Export::Unix::UserDB::User::known_attrs{$attr} or exists $self->{$attr} 0 0 0
Condition0%0/3
exists $Sys::Export::Unix::UserDB::User::known_attrs{$attr} or exists $self->{$attr}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $Sys::Export::Unix::UserDB::User::known_attrs{$attr} or exists $self->{$attr}
exists $Sys::Export::Unix::UserDB::User::known_attrs{$attr} exists $self->{$attr}
Truth table0%0/3
A or B
A= exists $Sys::Export::Unix::UserDB::User::known_attrs{$attr}B= exists $self->{$attr}
ABresult
1X1
011
000
613 0 ▶ @_? ( $self->{$attr}= shift ) : $self->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
614 }
615  
616 0 ▶ sub import {}
Subroutine import: not called CC=1 SCAR=0.0
617 0 ▶ sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
618 }
619  
620  
621 2 package Sys::Export::Unix::UserDB::Group {
622 2 ▶ use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
623 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
624 2 ▶ use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
625 2 our @CARP_NOT= qw( Sys::Export::Unix::UserDB );
626 2 our %known_attrs= map +($_ => 1), qw( name gid passwd );
627  
628 31 ▶ sub new($class, %attrs) {
Subroutine new: called CC=5 SCAR=17.5
629 my $self= bless {
630 name => delete $attrs{name},
631 gid => delete $attrs{gid},
632 passwd => delete $attrs{passwd},
633 31 }, $class;
634 31 ▶ croak "Group 'name' is required" unless defined $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"name"} 1 30 31
635 30 ▶ croak "Group 'gid' is required" unless defined $self->{gid};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"gid"} 1 29 30
636 29 for my $key (keys %attrs) {
637 0 ▶ carp "Unknown group attribute: '$key'" unless $known_attrs{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $Sys::Export::Unix::UserDB::Group::known_attrs{$key} 0 0 0
638 0 $self->$key($attrs{$key});
639 }
640 29 return $self;
641 }
642  
643 # Read-only attributes
644 42 ▶ sub name($self) { $self->{name} }
Subroutine name: called CC=1 SCAR=0.0
645 87 ▶ sub gid($self) { $self->{gid} }
Subroutine gid: called CC=1 SCAR=0.0
646
647 1 ▶ sub clone($self, %attrs) {
Subroutine clone: called CC=1 SCAR=0.0
648 1 return ref($self)->new( %$self, %attrs );
649 }
650  
651 # Other generic read/write accessors
652 2 our $AUTOLOAD;
653 15 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=14.6
654 15 my $attr= substr($AUTOLOAD, rindex($AUTOLOAD, ':')+1);
655 15 my $self= shift;
656 carp "Unknown group attribute '$attr'"
657 15 ▶ unless exists $known_attrs{$attr} || exists $self->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Sys::Export::Unix::UserDB::Group::known_attrs{$attr} or exists $self->{$attr} 0 15 15
Condition33%1/3
exists $Sys::Export::Unix::UserDB::Group::known_attrs{$attr} or exists $self->{$attr}
l!l&&r!l&&!r
1500
MC/DC0%0/2
exists $Sys::Export::Unix::UserDB::Group::known_attrs{$attr} or exists $self->{$attr}
exists $Sys::Export::Unix::UserDB::Group::known_attrs{$attr} exists $self->{$attr}
Truth table33%1/3
A or B
A= exists $Sys::Export::Unix::UserDB::Group::known_attrs{$attr}B= exists $self->{$attr}
ABresult
1X1
011
000
658 15 ▶ @_? ( $self->{$attr}= shift ) : $self->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 14 15
659 }
660  
661 0 ▶ sub import {}
Subroutine import: not called CC=1 SCAR=0.0
662 0 ▶ sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
663 }
664  
665 # Avoiding dependency on namespace::clean
666 2 delete @{Sys::Export::Unix::UserDB::}{qw( dclone isa_userdb isa_user isa_group getpwnam pw_has )};
667 2 1;
668  
669 __END__
« Export/ISO9660.pm ↑ Summary Export/GPT.pm »