blib/lib/Linux/usermod.pm

filter: Statementstmt 49.6% 218 / 439 Branchbran 31.7% 80 / 252 Conditioncond 18.7% 12 / 64 MC/DCmcdc 2.5% 1 / 39 Subroutinesub 57.6% 15 / 26 Podpod 85.7% 12 / 14 totaltotal 40.5% 338 / 834 CC 192 SCAR 71.5
CC
192
Cov
41%
set
3539.8
add
113.5
grpdel
72.0
CRAP 1270.3
↑ Summary
1 package Linux::usermod;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = 0.69;
8  
9 1 our $file_passwd = '/etc/passwd';
10 1 our $file_shadow = '/etc/shadow';
11 1 our $file_group = '/etc/group';
12 1 our $file_gshadow = '/etc/gshadow';
13  
14 1 my %field = (
15 NAME => 0, #The user's name
16 PPASSWORD=> 1, #The "passwd" file password
17 UID => 2, #The user's id
18 GID => 3, #The user's group id
19 COMMENT => 4, #The comment about the user
20 HOME => 5, #The user's home directory
21 SHELL => 6, #The user's shell
22 SNAME => 7, #The user's name in shadow file
23 PASSWORD => 8, #The 13-character encrypted password
24 LASTCHG => 9, #The number of days since January 1, 1970 of the last password changed date
25 MIN => 10, #The minimum number of days required between password changes
26 MAX => 11, #The maximum number of days the password is valid
27 WARN => 12, #The number of days before expiring the password that the user is warned
28 INACTIVE => 13, #The number of days of inactivity allowed for the user
29 EXPIRE => 14, #The number of days since January 1, 1970 that account is disabled
30 FLAG => 15 #Currently not used.
31 );
32  
33 1 my %gfield = (
34 NAME => 0, #The group name
35 PPASSWORD=> 1, #The group password
36 GID => 2, #The group id number
37 USERS => 3, #The group members (users)
38 SNAME => 4, #The group name in gshadow file
39 PASSWORD => 5, #The encrypted ggroup password
40 GA => 6, #The group administrators
41 GU => 7 #The group members (users)
42 );
43  
44 0 ▶sub fields { keys %field }
Subroutine fields: not called CC=1 SCAR=6.9
Pod undocumented
45  
46 0 ▶sub gfields { keys %gfield }
Subroutine gfields: not called CC=1 SCAR=6.9
Pod undocumented
47  
48 2 ▶sub new {
Subroutine new: called CC=5 SCAR=16.4
Pod documented
49 2 my $class = shift;
50 2 my $user = shift;
51 2 my $flag = shift;
52 2 my @args;
53 2 ▶ if ($flag){
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) { } 1 1 2
54 1 ▶ croak "no such group" unless _exists($user, $flag);
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($user, $flag) 0 1 1
55 1 @args = _read_grp($user);
56 1 push @args, '__G__';
57 }else{
58 1 ▶ croak "no such user" unless _exists($user, $flag);
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($user, $flag) 0 1 1
59 1 @args = _read_user($user);
60 1 push @args, '__U__';
61 }
62 2 ▶ return bless [ @args ], ref($class)||$class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
63 }
64  
65 9 ▶sub get {
Subroutine get: called CC=9 SCAR=30.5
Pod documented
66 9 my $self = shift;
67 9 my $what = shift;
68 9 my $key;
69 9 ▶ if($self->[-1] eq '__U__'){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[-1] eq '__U__') { } 6 3 9
elsif ($self->[-1] eq '__G__') { } 3 0 3
70 6 ▶ if($what =~ /^\d{1,2}$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /^\d{1,2}$/) 0 6 6
71 0 ▶ while(my($k, $v) = each %field){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %field) 0 0 0
72 0 ▶ $v == $what and $key = $k
Branch: 1 branch
BranchTrueFalseTotal
$v == $what and $key = $k 0 0 0
73 }
74 0 return $self->[$field{$key}]
75 }
76 6 $what = uc $what;
77 6 return $self->[$field{$what}]
78 }
79 elsif($self->[-1] eq '__G__'){
80 3 ▶ if($what =~ /^\d{1,2}$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /^\d{1,2}$/) 0 3 3
81 0 ▶ while(my($k, $v) = each %gfield){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %gfield) 0 0 0
82 0 ▶ $v == $what and $key = $k
Branch: 1 branch
BranchTrueFalseTotal
$v == $what and $key = $k 0 0 0
83 }
84 0 return $self->[$gfield{$key}]
85 }
86 3 $what = uc $what;
87 3 return $self->[$gfield{$what}]
88 }
89 }
90  
91 1 ▶sub set {
Subroutine set: called CC=76 SCAR=81.7
Pod documented
92 1 my $self = shift;
93 1 my $what = shift;
94 1 my $newval = shift;
95 1 $what = uc $what;
96 1 ▶ return 0 unless exists($field{$what}) or exists($gfield{$what});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $field{$what} or exists $gfield{$what} 0 1 1
Condition33%1/3
exists $field{$what} or exists $gfield{$what}
l!l&&r!l&&!r
010
MC/DC0%0/2
exists $field{$what} or exists $gfield{$what}
exists $field{$what} exists $gfield{$what}
Truth table33%1/3
A or B
A= exists $field{$what}B= exists $gfield{$what}
ABresult
1X1
011
000
97 1 ▶ return 0 if $newval =~ /:/ and ($field{$what} != 8 or $gfield{$what} != 4);
Branch: 1 branch
BranchTrueFalseTotal
if $newval =~ /:/ and $field{$what} != 8 || $gfield{$what} != 4 0 1 1
Condition16%1/6
$newval =~ /:/ and $field{$what} != 8 || $gfield{$what} != 4
!ll&&!rl&&r
100
$field{$what} != 8 or $gfield{$what} != 4
l!l&&r!l&&!r
000
MC/DC0%0/3
$newval =~ /:/ and $field{$what} != 8 || $gfield{$what} != 4
$newval =~ /:/ $field{$what} != 8 $gfield{$what} != 4
Truth table25%1/4
A and B or C
A= $newval =~ /:/B= $field{$what} != 8C= $gfield{$what} != 4
ABCresult
0XX0
1000
11X1
1011
98 1 ▶ $newval = '' if $newval =~ /^undef$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $newval =~ /^undef$/i 0 1 1
99 1 ▶ if($self->[-1] eq '__U__'){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[-1] eq '__U__') { } 0 1 1
elsif ($self->[-1] eq '__G__') { } 1 0 1
100 0 ▶ my $flag = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
101 0 my $oldval = $self->[$field{$what}];
102 0 my $name = $self->[$field{NAME}];
103 0 _clean($name);
104 0 $self->[$field{$what}] = $newval;
105 0 ▶ if($field{$what} <= 6){
Branch: 1 branch
BranchTrueFalseTotal
if ($field{$what} <= 6) 0 0 0
106 0 my @file = _io("$file_passwd", '', 'r');
107 0 my @user;
108 0 push @user, $self->[$_] for 0..6;
109 0 my $user = join ':', @user;
110 0 ▶ for(@file){ s/.+/$user/ if /^\Q$name\E:/ }
Branch: 1 branch
BranchTrueFalseTotal
if /^\Q$name\E:/ 0 0 0
111 0 _io("$file_passwd", \@file, 'w');
112 0 ▶ if($field{$what} == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($field{$what} == 0) 0 0 0
113 0 ▶ croak "invalid name" if $newval !~ /^([A-Z]|[a-z]){1}\w{0,254}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $newval =~ /^([A-Z]|[a-z]){1}\w{0,254}/ 0 0 0
114 0 my %names;
115 0 @file = @user = ();
116 0 @file = _io("$file_shadow", '', 'r');
117 0 ▶ map{ /^(.[^:]+):/ and $names{$1} = 1 }@file;
Condition0%0/2
/^(.[^:]+):/ && ($names{$1} = 1)
!ll
00
MC/DC0%0/1
/^(.[^:]+):/ && ($names{$1} = 1)
/^(.[^:]+):/
Truth table0%0/2
A
A= /^(.[^:]+):/
Aresult
00
11
118 0 ▶ croak "user name $newval already exists" if defined($names{$newval});
Branch: 1 branch
BranchTrueFalseTotal
if defined $names{$newval} 0 0 0
119 0 undef %names;
120 0 push @user, $self->[$_] for 8..14;
121 0 unshift @user, $self->[0];
122 0 $user = join ':', @user;
123 0 ▶ for(@file){ s/.+/$user/ if /^\Q$name\E:/ }
Branch: 1 branch
BranchTrueFalseTotal
if /^\Q$name\E:/ 0 0 0
124 0 ▶ _io("$file_shadow", \@file, 'w') and return 1
Branch: 1 branch
BranchTrueFalseTotal
_io("$Linux::usermod::file_shadow", \@file, "w") and return 1 0 0 0
125 }
126
127 }
128 0 ▶ if($field{$what} > 6){
Branch: 1 branch
BranchTrueFalseTotal
if ($field{$what} > 6) 0 0 0
129 0 my @file = _io("$file_shadow", '', 'r');
130 0 ▶ $self->[9] = _get_1970_diff() if $field{$what} == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $field{$what} == 8 0 0 0
131 0 ▶ if($field{$what} == 8 && $newval){
Branch: 1 branch
BranchTrueFalseTotal
if ($field{$what} == 8 and $newval) 0 0 0
Condition0%0/3
$field{$what} == 8 and $newval
!ll&&!rl&&r
000
MC/DC0%0/2
$field{$what} == 8 and $newval
$field{$what} == 8 $newval
Truth table0%0/3
A and B
A= $field{$what} == 8B= $newval
ABresult
0X0
100
111
132 0 ▶ $oldval =~ /^!/ and my $lock = 1;
Branch: 1 branch
BranchTrueFalseTotal
$oldval =~ /^!/ and my $lock = 1 0 0 0
133 0 ▶ $self->[8] = _gen_pass($self->[$field{$what}], $lock) unless $flag;
Branch: 1 branch
BranchTrueFalseTotal
unless $flag 0 0 0
134 }
135 0 my @user;
136 0 push @user, "$self->[$_]" for 7..15;
137 0 my $user = join ':', @user;
138 0 ▶ for(@file){ s/.+/$user/ if /^\Q$name\E:/ }
Branch: 1 branch
BranchTrueFalseTotal
if /^\Q$name\E:/ 0 0 0
139 0 _io("$file_shadow", \@file, 'w');
140 0 ▶ if($field{$what} == 7){
Branch: 1 branch
BranchTrueFalseTotal
if ($field{$what} == 7) 0 0 0
141 0 @file = @user = ();
142 0 @file = _io("$file_passwd", '', 'r');
143 0 push @user, $self->[$_] for 1..6;
144 0 unshift @user, $self->[7];
145 0 $user = join ':', @user;
146 0 ▶ for(@file){ s/.+/$user/ if /^\Q$name\E:/ }
Branch: 1 branch
BranchTrueFalseTotal
if /^\Q$name\E:/ 0 0 0
147 0 ▶ _io("$file_passwd", \@file, 'w') and return 1
Branch: 1 branch
BranchTrueFalseTotal
_io("$Linux::usermod::file_passwd", \@file, "w") and return 1 0 0 0
148 }
149 }
150 }
151 elsif($self->[-1] eq '__G__'){
152 1 my $name = $self->[$gfield{NAME}];
153 1 my $oldval = $self->[$gfield{$what}];
154 1 $self->[$gfield{$what}] = $newval;
155 1 ▶ if($gfield{$what} == 0 or $gfield{$what} == 4){
Branch: 1 branch
BranchTrueFalseTotal
if ($gfield{$what} == 0 or $gfield{$what} == 4) 0 1 1
Condition33%1/3
$gfield{$what} == 0 or $gfield{$what} == 4
l!l&&r!l&&!r
001
MC/DC0%0/2
$gfield{$what} == 0 or $gfield{$what} == 4
$gfield{$what} == 0 $gfield{$what} == 4
Truth table33%1/3
A or B
A= $gfield{$what} == 0B= $gfield{$what} == 4
ABresult
1X1
011
000
156 0 ▶ croak "invalid name" if $newval !~ /^([A-Z]|[a-z]){1}\w{0,254}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $newval =~ /^([A-Z]|[a-z]){1}\w{0,254}/ 0 0 0
157 0 my @file = _io($file_group, '', 'r');
158 0 my %names;
159 0 ▶ map{ m#^(.[^:]+):# and $names{$1} }@file;
Condition0%0/3
/^(.[^:]+):/ && $names{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
/^(.[^:]+):/ && $names{$1}
/^(.[^:]+):/ $names{$1}
Truth table0%0/3
A && B
A= /^(.[^:]+):/B= $names{$1}
ABresult
0X0
100
111
160 0 ▶ croak "group name $newval already exists" if exists($names{$newval});
Branch: 1 branch
BranchTrueFalseTotal
if exists $names{$newval} 0 0 0
161 0 undef %names;
162 0 for(@file){
163 0 ▶ /^$oldval:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^$oldval:/ or next 0 0 0
164 0 my $newline = "$self->[0]:$self->[1]:$self->[2]:$self->[3]";
165 0 s/.+/$newline/;
166 }
167 0 _io($file_group, \@file, 'w');
168 0 @file = _io($file_gshadow, '', 'r');
169 0 for(@file){
170 0 ▶ /^$oldval:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^$oldval:/ or next 0 0 0
171 0 $self->[4] = $newval;
172 0 my $newline = "$self->[4]:$self->[5]:$self->[6]:$self->[7]";
173 0 s/.+/$newline/;
174 }
175 0 ▶ _io($file_gshadow, \@file, 'w') and return 1
Branch: 1 branch
BranchTrueFalseTotal
_io($Linux::usermod::file_gshadow, \@file, "w") and return 1 0 0 0
176 }
177 1 ▶ if($gfield{$what} == 3 or $gfield{$what} == 7){
Branch: 1 branch
BranchTrueFalseTotal
if ($gfield{$what} == 3 or $gfield{$what} == 7) 0 1 1
Condition33%1/3
$gfield{$what} == 3 or $gfield{$what} == 7
l!l&&r!l&&!r
001
MC/DC0%0/2
$gfield{$what} == 3 or $gfield{$what} == 7
$gfield{$what} == 3 $gfield{$what} == 7
Truth table33%1/3
A or B
A= $gfield{$what} == 3B= $gfield{$what} == 7
ABresult
1X1
011
000
178 0 for(split /\s+/, $newval){
179 0 ▶ croak "$_ does not exist" unless(_exists($_))
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($_) 0 0 0
180 }
181 0 my $users = join ',', split /\s+/, "$newval";
182 0 $self->[3] = $users;
183 0 my @file = _io($file_group, '', 'r');
184 0 for(@file){
185 0 ▶ /^\Q$name\E:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$name\E:/ or next 0 0 0
186 0 my $newline = "$self->[0]:$self->[1]:$self->[2]:$self->[3]";
187 0 s/.+/$newline/;
188 }
189 0 _io($file_group, \@file, 'w');
190 0 @file = _io($file_gshadow, '', 'r');
191 0 $self->[7] = $users;
192 0 for(@file){
193 0 ▶ /^\Q$name\E:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$name\E:/ or next 0 0 0
194 0 my $newline = "$self->[4]:$self->[5]:$self->[6]:$self->[3]";
195 0 s/.+/$newline/;
196 }
197 0 ▶ _io($file_gshadow, \@file, 'w') and return 1
Branch: 1 branch
BranchTrueFalseTotal
_io($Linux::usermod::file_gshadow, \@file, "w") and return 1 0 0 0
198 }
199 1 ▶ if($gfield{$what} == 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($gfield{$what} == 2) 0 1 1
200 0 ▶ croak "wrong group id" if $newval < 1 or $newval > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $newval < 1 or $newval > 65535 0 0 0
Condition0%0/3
$newval < 1 or $newval > 65535
l!l&&r!l&&!r
000
MC/DC0%0/2
$newval < 1 or $newval > 65535
$newval < 1 $newval > 65535
Truth table0%0/3
A or B
A= $newval < 1B= $newval > 65535
ABresult
1X1
011
000
201 0 my %ids;
202 0 my @file = _io("$file_group", '', 'r');
203 0 ▶ map { /^.+?:.*?:(.+):/ and $ids{$1} = 1 } @file;
Condition0%0/2
/^.+?:.*?:(.+):/ && ($ids{$1} = 1)
!ll
00
MC/DC0%0/1
/^.+?:.*?:(.+):/ && ($ids{$1} = 1)
/^.+?:.*?:(.+):/
Truth table0%0/2
A
A= /^.+?:.*?:(.+):/
Aresult
00
11
204 0 ▶ croak "group id $newval already exists" if $ids{$newval};
Branch: 1 branch
BranchTrueFalseTotal
if $ids{$newval} 0 0 0
205 0 for(@file){
206 0 ▶ /^\Q$name\E:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$name\E:/ or next 0 0 0
207 0 my $newline = "$self->[0]:$self->[1]:$self->[2]:$self->[3]";
208 0 s/.+/$newline/;
209 }
210 0 ▶ _io($file_group, \@file, 'w') and return 1
Branch: 1 branch
BranchTrueFalseTotal
_io($Linux::usermod::file_group, \@file, "w") and return 1 0 0 0
211 }
212 1 ▶ if($gfield{$what} == 6){
Branch: 1 branch
BranchTrueFalseTotal
if ($gfield{$what} == 6) 1 0 1
213 1 ▶ croak "user $newval does not exist" unless(_exists($newval));
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($newval) 0 1 1
214 1 $self->[6] = $newval;
215 1 my @file = _io($file_gshadow, '', 'r');
216 1 for(@file){
217 1 ▶ /^\Q$name\E:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$name\E:/ or next 1 0 1
218 1 my $newline = "$self->[4]:$self->[5]:$self->[6]:$self->[7]";
219 1 s/.+/$newline/;
220 }
221 1 ▶ _io($file_gshadow, \@file, 'w') and return 1
Branch: 1 branch
BranchTrueFalseTotal
_io($Linux::usermod::file_gshadow, \@file, "w") and return 1 1 0 1
222 }
223 0 ▶ if($gfield{$what} == 1 or $gfield{$what} == 5){
Branch: 1 branch
BranchTrueFalseTotal
if ($gfield{$what} == 1 or $gfield{$what} == 5) 0 0 0
Condition0%0/3
$gfield{$what} == 1 or $gfield{$what} == 5
l!l&&r!l&&!r
000
MC/DC0%0/2
$gfield{$what} == 1 or $gfield{$what} == 5
$gfield{$what} == 1 $gfield{$what} == 5
Truth table0%0/3
A or B
A= $gfield{$what} == 1B= $gfield{$what} == 5
ABresult
1X1
011
000
224 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
225 0 my $salt = join '', ('a'..'z', 'A'..'Z', 0..9)[rand 26,rand 26,rand 26];
226 0 my $newpass;
227 0 ▶ if($newval)
Branch: 1 branch
BranchTrueFalseTotal
if ($newval) { } 0 0 0
228 0 { $newpass = crypt($newval, $salt) }
229 else
230 0 { $newpass = '!' }
231 0 my @file = _io($file_gshadow, '', 'r');
232 0 $self->[1] = 'x';
233 0 $self->[5] = $newpass;
234 0 for(@file){
235 0 ▶ /^\Q$name\E:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$name\E:/ or next 0 0 0
236 0 my $newline = "$self->[4]:$self->[5]:$self->[6]:$self->[7]";
237 0 s/.+/$newline/;
238 }
239 0 _io($file_gshadow, \@file, 'w');
240 0 @file = _io($file_group, '', 'r');
241 0 for(@file){
242 0 ▶ /^\Q$name\E:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$name\E:/ or next 0 0 0
243 0 my $newline = "$self->[0]:$self->[1]:$self->[2]:$self->[3]";
244 0 s/.+/$newline/;
245 }
246 0 ▶ _io($file_group, \@file, 'w') and return 1
Branch: 1 branch
BranchTrueFalseTotal
_io($Linux::usermod::file_group, \@file, "w") and return 1 0 0 0
247 }
248 }
249 0 return 0
250 }
251  
252 1 ▶sub _read_user {
Subroutine _read_user: called CC=7 SCAR=19.5
253 1 my $username = shift;
254 1 my (@user, @tmp, @file);
255 1 @file = _io($file_passwd, '', 'r');
256 1 for(@file){
257 1 ▶ /^\Q$username\E:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$username\E:/ or next 1 0 1
258 1 my $user = $_;
259 1 for(1..7){
260 7 $user =~ m#(.[^:]*){$_}#;
261 7 my $ss = $1;
262 7 $ss =~ s/(^:*|:*$)//;
263 7 $tmp[$_ - 1] = $ss;
264 } last
265 1 }
266 1 @user = @tmp;
267 1 @tmp = ();
268 1 @file = _io($file_shadow, '', 'r');
269 1 for(@file){
270 1 ▶ /^\Q$username\E:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$username\E:/ or next 1 0 1
271 1 my $user = $_;
272 1 for(1..9){
273 9 $user =~ m#(.[^:]*){$_}#;
274 9 my $ss = $1;
275 9 $ss =~ s/(^:*|:*$)//;
276 9 $tmp[$_ - 1] = $ss;
277 } last
278 1 }
279 1 @user = (@user, @tmp);
280 1 return (@user);
281 }
282  
283 0 ▶sub _gen_pass {
Subroutine _gen_pass: not called CC=3 SCAR=24.8
284 0 my $password = shift;
285 0 my $flag = shift;
286 0 ▶ $password or croak "no password given";
Branch: 1 branch
BranchTrueFalseTotal
$password or croak("no password given") 0 0 0
287 0 my @rands = ( "A" .. "Z", "a" .. "z", 0 .. 9 );
288 0 my $salt = join("", @rands[ map { rand @rands } ( 1 .. 8 ) ]);
289 0 ▶ $password = ($flag)?'!'.crypt($password, q($1$)."$salt"):crypt($password, q($1$)."$salt");
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 0 0 0
290 0 return $password
291 }
292  
293 4 ▶sub _exists {
Subroutine _exists: called CC=5 SCAR=16.1
294 4 my $name = shift;
295 4 my $gflag = shift;
296 4 ▶ my $file = ($gflag) ? "$file_group" : "$file_passwd";
Branch: 1 branch
BranchTrueFalseTotal
$gflag ? : 1 3 4
297 4 my @file = _io("$file", '', 'r');
298 4 my $flag;
299 4 ▶ /^\Q$name\E:/ and $flag = 1 for @file;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$name\E:/ and $flag = 1 3 0 3
300 4 ▶ return $flag ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$flag ? : 3 1 4
301 }
302  
303 1 ▶sub add {
Subroutine add: called CC=42 SCAR=47.3
Pod documented
304 1 my $class = shift;
305 1 my (%fields, $c, @args);
306 1 push @args, $_ for @_;
307 1 ▶ croak "no username given" if scalar @args == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @args == 0 0 1 1
308 1 ▶ croak "user $args[0] exists" if _exists($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
if _exists($args[0]) 0 1 1
309 1 for(@args){
310 7 chomp($_);
311 7 ▶ /^\s*$/ and $c++ and next;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*$/ and $c++ and next 1 6 7
Condition66%2/3
/^\s*$/ and $c++
!ll&&!rl&&r
601
MC/DC50%1/2
/^\s*$/ and $c++
/^\s*$/ $c++
Truth table66%2/3
A and B
A= /^\s*$/B= $c++
ABresult
0X0
100
111
312 6 $c++;
313 6 ▶ if($c == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 1) 1 5 6
314 1 ▶ croak "wrong username given" if /:/;
Branch: 1 branch
BranchTrueFalseTotal
if /:/ 0 1 1
315 1 ▶ croak "wrong username" unless /^([A-Z]|[a-z]){1}\w{0,254}/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^([A-Z]|[a-z]){1}\w{0,254}/ 0 1 1
316 1 ▶ $fields{username} = $_ || croak "no username given";
Condition50%1/2
$_ || croak('no username given')
l!l
10
MC/DC0%0/1
$_ || croak('no username given')
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
317 }
318 6 ▶ if($c == 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 2) 0 6 6
319 0 ▶ croak "wrong password length" unless /^(.*){0,254}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^(.*){0,254}$/ 0 0 0
320 0 ▶ $fields{password} = _gen_pass($_) if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
321 }
322 6 ▶ if($c == 3){
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 3) 1 5 6
323 1 ▶ $_ eq '' and $_ = 1000;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "" and $_ = 1000 0 1 1
324 1 ▶ croak "wrong uid" unless /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\d+$/ 0 1 1
325 1 ▶ croak "wrong uid" if $_ > 65535 or $_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > 65535 or $_ < 1 0 1 1
Condition33%1/3
$_ > 65535 or $_ < 1
l!l&&r!l&&!r
001
MC/DC0%0/2
$_ > 65535 or $_ < 1
$_ > 65535 $_ < 1
Truth table33%1/3
A or B
A= $_ > 65535B= $_ < 1
ABresult
1X1
011
000
326 1 ▶ $fields{uid} = $_ || 1000;
Condition50%1/2
$_ || 1000
l!l
10
MC/DC0%0/1
$_ || 1000
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
327 }
328 6 ▶ if($c == 4){
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 4) 1 5 6
329 1 ▶ $_ eq '' and $_ = 1000;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "" and $_ = 1000 0 1 1
330 1 ▶ croak "wrong gid" unless /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\d+$/ 0 1 1
331 1 ▶ if(/^\d+$/){ croak "wrong gid" if $_ > 65535 or $_ < 1 }
Branch: 2 branches
BranchTrueFalseTotal
if (/^\d+$/) 1 0 1
if $_ > 65535 or $_ < 1 0 1 1
Condition33%1/3
$_ > 65535 or $_ < 1
l!l&&r!l&&!r
001
MC/DC0%0/2
$_ > 65535 or $_ < 1
$_ > 65535 $_ < 1
Truth table33%1/3
A or B
A= $_ > 65535B= $_ < 1
ABresult
1X1
011
000
332 1 ▶ $fields{gid} = $_ || $fields{uid};
Condition33%1/3
$_ || $fields{'uid'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$_ || $fields{'uid'}
$_ $fields{'uid'}
Truth table33%1/3
A || B
A= $_B= $fields{'uid'}
ABresult
1X1
011
000
333 }
334 6 ▶ if($c == 5){
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 5) 1 5 6
335 1 ▶ croak "wrong comment given" if /:/;
Branch: 1 branch
BranchTrueFalseTotal
if /:/ 0 1 1
336 1 $fields{comment} = $_;
337 }
338 6 ▶ if($c == 6){
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 6) 1 5 6
339 1 ▶ croak "wrong home given" if /:/;
Branch: 1 branch
BranchTrueFalseTotal
if /:/ 0 1 1
340 1 $fields{home} = $_;
341 }
342 6 ▶ if($c == 7){
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 7) 1 5 6
343 1 ▶ croak " wrong shell given" if /:/;
Branch: 1 branch
BranchTrueFalseTotal
if /:/ 0 1 1
344 1 $fields{shell} = $_;
345 }
346 }
347 1 ▶ $fields{password} or $fields{password} = '!';
Branch: 1 branch
BranchTrueFalseTotal
$fields{"password"} or $fields{"password"} = "!" 0 1 1
348 1 my @file = _io("$file_passwd", '', 'r');
349 1 my @ids;
350 1 push @ids, (split /:/)[2] for @file;
351 1 for (@ids){
352 0 ▶ if ($fields{uid} == $_){
Branch: 1 branch
BranchTrueFalseTotal
if ($fields{"uid"} == $_) 0 0 0
353 0 $fields{uid} = 1000;
354 last
355 0 }
356 }
357 1 ▶ if($fields{uid} == 1000){
Branch: 1 branch
BranchTrueFalseTotal
if ($fields{"uid"} == 1000) 0 1 1
358 0 for(sort @ids){
359 0 ▶ $_ < 1000 and next;
Branch: 1 branch
BranchTrueFalseTotal
$_ < 1000 and next 0 0 0
360 0 ▶ $fields{uid} == $_ and $fields{uid}++;
Branch: 1 branch
BranchTrueFalseTotal
$fields{"uid"} == $_ and ++$fields{"uid"} 0 0 0
361 }
362 }
363 1 ▶ $fields{gid} = $fields{uid} if !$fields{gid};
Branch: 1 branch
BranchTrueFalseTotal
unless $fields{"gid"} 0 1 1
364 1 my @newuser = ("$fields{username}:x:$fields{uid}:$fields{gid}:$fields{comment}:$fields{home}:$fields{shell}");
365 1 _io("$file_passwd", \@newuser, 'a');
366 1 my $time_1970 = _get_1970_diff();
367 1 @newuser = ("$fields{username}:$fields{password}:$time_1970:0:99999:7:::");
368 1 _io("$file_shadow", \@newuser, 'a');
369 1 return 1
370 }
371  
372 1 ▶sub grpadd {
Subroutine grpadd: called CC=9 SCAR=28.6
Pod documented
373 1 my $class = shift;
374 1 ▶ my $group = shift or croak "empty group name";
Branch: 1 branch
BranchTrueFalseTotal
my $group = shift() or croak("empty group name") 1 0 1
375 1 my $gid = shift;
376 1 my $users = shift;
377 1 my (@tmp, %file, @newgroup);
378 1 my @file = _io("$file_group", '', 'r');
379 1 ▶ croak "wrong group name" unless $group =~ /^([A-Z]|[a-z]){1}\w{0,254}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $group =~ /^([A-Z]|[a-z]){1}\w{0,254}/ 0 1 1
380 0 ▶ map { @tmp = split /:/, $_ and $file{$tmp[0]} = $tmp[2] } @file;
Condition0%0/3
(@tmp = split(/:/, $_, 0)) && ($file{$tmp[0]} = $tmp[2])
!ll&&!rl&&r
000
MC/DC0%0/2
(@tmp = split(/:/, $_, 0)) && ($file{$tmp[0]} = $tmp[2])
(@tmp = split(/:/, $_, 0)) ($file{$tmp[0]} = $tmp[2])
Truth table0%0/3
A && B
A= (@tmp = split(/:/, $_, 0))B= ($file{$tmp[0]} = $tmp[2])
ABresult
0X0
100
111
381 1 ▶ exists($file{$group}) and croak "group $group already exists";
Branch: 1 branch
BranchTrueFalseTotal
exists $file{$group} and croak("group $group already exists") 0 1 1
382 1 ▶ if(!$gid){
Branch: 1 branch
BranchTrueFalseTotal
unless ($gid) 0 1 1
383 0 $gid = 100;
384 0 for(sort {$a <=> $b} values %file){
385 0 ▶ next if $_ < 100;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 100 0 0 0
386 0 ▶ $gid == $_ and $gid++
Branch: 1 branch
BranchTrueFalseTotal
$gid == $_ and ++$gid 0 0 0
387 }
388 }
389 1 my $userlist = join(',', split(/\s+/, $users));
390 1 @newgroup = ("$group:x:$gid:$userlist");
391 1 _io("$file_group", \@newgroup, 'a');
392 1 @newgroup = ("$group:!!::$userlist");
393 1 _io("$file_gshadow", \@newgroup, 'a');
394 }
395  
396 0 ▶sub del {
Subroutine del: not called CC=6 SCAR=37.4
Pod documented
397 0 my $class = shift;
398 0 my $username = shift;
399 0 ▶ _exists($username) or croak "user $username does not exist";
Branch: 1 branch
BranchTrueFalseTotal
_exists($username) or croak("user $username does not exist") 0 0 0
400 0 my @old = _io("$file_passwd", '', 'r');
401 0 my @new;
402 0 ▶ /^\Q$username\E:/ or push @new, $_ for @old;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$username\E:/ or push @new, $_ 0 0 0
403 0 _io("$file_passwd", \@new, 'w');
404 0 @new = ();
405 0 @old = _io("$file_shadow", '', 'r');
406 0 ▶ /^\Q$username\E:/ or push @new, $_ for @old;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$username\E:/ or push @new, $_ 0 0 0
407 0 _io("$file_shadow", \@new, 'w');
408 0 return 1
409 }
410  
411 0 ▶sub grpdel {
Subroutine grpdel: not called CC=8 SCAR=42.8
Pod documented
412 0 my $class = shift;
413 0 ▶ my $group = shift or croak "empty group name/gid";
Branch: 1 branch
BranchTrueFalseTotal
my $group = shift() or croak("empty group name/gid") 0 0 0
414 0 my (@tmp, %file);
415 0 my @file = _io("$file_group", '', 'r');
416 0 ▶ map { @tmp = split /:/, $_ and $file{$tmp[0]} = $tmp[2] } @file;
Condition0%0/3
(@tmp = split(/:/, $_, 0)) && ($file{$tmp[0]} = $tmp[2])
!ll&&!rl&&r
000
MC/DC0%0/2
(@tmp = split(/:/, $_, 0)) && ($file{$tmp[0]} = $tmp[2])
(@tmp = split(/:/, $_, 0)) ($file{$tmp[0]} = $tmp[2])
Truth table0%0/3
A && B
A= (@tmp = split(/:/, $_, 0))B= ($file{$tmp[0]} = $tmp[2])
ABresult
0X0
100
111
417 0 ▶ exists($file{$group}) or croak "group $group does not exists";
Branch: 1 branch
BranchTrueFalseTotal
exists $file{$group} or croak("group $group does not exists") 0 0 0
418 0 @tmp = ();
419 0 ▶ /^$group/ or push @tmp, $_ for @file;
Branch: 1 branch
BranchTrueFalseTotal
/^$group/ or push @tmp, $_ 0 0 0
420 0 _io("$file_group", \@tmp, 'w');
421 0 @file = _io("$file_gshadow", '', 'r');
422 0 @tmp = ();
423 0 ▶ /^$group/ or push @tmp, $_ for @file;
Branch: 1 branch
BranchTrueFalseTotal
/^$group/ or push @tmp, $_ 0 0 0
424 0 _io("$file_gshadow", \@tmp, 'w');
425
426 }
427  
428 1 ▶sub _read_grp {
Subroutine _read_grp: called CC=8 SCAR=20.9
429 1 ▶ my $group = shift or croak "empty group name/gid";
Branch: 1 branch
BranchTrueFalseTotal
my $group = shift() or croak("empty group name/gid") 1 0 1
430 1 my (@tmp, @grp);
431 1 my @file = _io("$file_group", '', 'r');
432 1 for(@file){
433 1 ▶ /^$group:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^$group:/ or next 1 0 1
434 1 my $user = $_;
435 1 for(1..4){
436 4 $user =~ /(.[^:]*){$_}/;
437 4 my $ss = $1;
438 4 $ss =~ s/(^:*|:*$)//;
439 4 $tmp[$_ - 1] = $ss;
440 } last
441 1 }
442 1 @grp = @tmp;
443 1 @file = _io("$file_gshadow", '', 'r');
444 1 for(@file){
445 1 ▶ /^$group:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^$group:/ or next 1 0 1
446 1 my $user = $_;
447 1 for(1..4){
448 4 $user =~ /(.[^:]*){$_}/;
449 4 my $ss = $1;
450 4 $ss =~ s/(^:*|:*$)//;
451 4 $tmp[$_ - 1] = $ss;
452 } last
453 1 }
454 1 @grp = (@grp, @tmp);
455 1 return (@grp)
456 }
457  
458 0 ▶sub tobsd{
Subroutine tobsd: not called CC=5 SCAR=34.0
Pod documented
459 0 my $self = shift;
460 0 (my @file) = _io("$file_shadow", '', 'r');
461 0 my $name = $self->get('name');
462 0 my @user;
463 0 for(@file){
464 0 ▶ /^\Q$name\E:/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^\Q$name\E:/ or next 0 0 0
465 0 push @user, $name, ':';
466 0 push @user, $self->get('password'), ':';
467 0 push @user, $self->get('uid'), ':';
468 0 push @user, $self->get('gid'), ':';
469 0 push @user, ':';
470 0 ▶ push @user, $self->get('expire') || 0, ':';
Condition0%0/2
$self->get('expire') || 0
l!l
00
MC/DC0%0/1
$self->get('expire') || 0
$self->get('expire')
Truth table0%0/2
A
A= $self->get('expire')
Aresult
11
00
471 0 ▶ push @user, $self->get('expire') || 0, ':';
Condition0%0/2
$self->get('expire') || 0
l!l
00
MC/DC0%0/1
$self->get('expire') || 0
$self->get('expire')
Truth table0%0/2
A
A= $self->get('expire')
Aresult
11
00
472 0 push @user, $self->get('comment'), ':';
473 0 push @user, $self->get('home'), ':';
474 0 push @user, $self->get('shell');
475 0 my $user = join '', @user;
476 0 s/.*/$user/;
477 }
478 0 _io("$file_shadow", \@file, 'w');
479 0 return 1
480 }
481  
482 16 ▶sub _io{
Subroutine _io: called CC=14 SCAR=27.1
483 16 my $file = shift;
484 16 my $newvals = shift;
485 16 my $flag = shift;
486 16 my @file;
487 16 ▶ croak $! unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 16 16
488 16 local *FH;
489 16 ▶ die "posible flags: r/w/a" unless $flag =~ /^(r|w|a)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $flag =~ /^(r|w|a)$/ 0 16 16
490 16 ▶ if($flag eq 'r'){
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq "r") 11 5 16
491 11 ▶ open FH, $file or croak "can't open_r $file $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, $file or croak("can't open_r $file $!") 11 0 11
492 11 ▶ flock FH, LOCK_SH or croak "can't lock_sh $file";
Branch: 1 branch
BranchTrueFalseTotal
flock FH, 1 or croak("can't lock_sh $file") 11 0 11
493 11 @file = <FH>;
494 11 close FH;
495 11 map { s/\n// } @file;
496 11 return @file;
497 }
498 5 ▶ if($flag eq 'w'){
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq "w") 1 4 5
499 1 ▶ open FH, "> $file" or croak "can't open_w $file $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, "> $file" or croak("can't open_w $file $!") 1 0 1
500 1 ▶ flock FH, LOCK_EX or croak "can't lock_ex $file";
Branch: 1 branch
BranchTrueFalseTotal
flock FH, 2 or croak("can't lock_ex $file") 1 0 1
501 1 print FH "$_\n" for @{$newvals};
502 1 close FH;
503 1 return 1
504 }
505 4 ▶ if($flag eq 'a'){
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq "a") 4 0 4
506 4 ▶ open FH, ">> $file" or croak "can't open_a $file $!";
Branch: 1 branch
BranchTrueFalseTotal
open FH, ">> $file" or croak("can't open_a $file $!") 4 0 4
507 4 ▶ flock FH, LOCK_EX or croak "can't lock_ex $file";
Branch: 1 branch
BranchTrueFalseTotal
flock FH, 2 or croak("can't lock_ex $file") 4 0 4
508 4 print FH "$_\n" for @{$newvals};
509 4 close FH;
510 4 return 1
511 }
512 }
513
514 0 ▶sub users{
Subroutine users: not called CC=3 SCAR=24.8
Pod documented
515 0 my $class = shift;
516 0 (my @file) = _io("$file_passwd", '', 'r');
517 0 my (%users, @users);
518 0 ▶ m#^(.[^:]+):# and push @users, $1 for @file;
Branch: 1 branch
BranchTrueFalseTotal
/^(.[^:]+):/ and push @users, $1 0 0 0
519 0 map{ $users{$_} = 1 }@users;
520 0 return %users
521 }
522  
523 0 ▶sub grps{
Subroutine grps: not called CC=3 SCAR=24.8
Pod documented
524 0 my $class = shift;
525 0 (my @file) = _io("$file_group", '', 'r');
526 0 my (%users, @users);
527 0 ▶ m#^(.[^:]+):# and push @users, $1 for @file;
Branch: 1 branch
BranchTrueFalseTotal
/^(.[^:]+):/ and push @users, $1 0 0 0
528 0 map{ $users{$_} = 1 }@users;
529 0 return %users
530 }
531
532 0 ▶sub lock{
Subroutine lock: not called CC=2 SCAR=17.9
Pod documented
533 0 my $self = shift;
534 0 my $password = $self->get("password");
535 0 ▶ return 1 if $password =~ /^\!/;
Branch: 1 branch
BranchTrueFalseTotal
if $password =~ /^\!/ 0 0 0
536 0 $password =~ s/(.*)/!$1/;
537 0 $self->set("password", $password, 1);
538 }
539  
540 0 ▶sub unlock{
Subroutine unlock: not called CC=3 SCAR=24.8
Pod documented
541 0 my $self = shift;
542 0 my $password = $self->get("password");
543 0 ▶ return if $password !~ /^\!/;
Branch: 1 branch
BranchTrueFalseTotal
unless $password =~ /^\!/ 0 0 0
544 0 $password =~ s/^\!//;
545 0 ▶ $password ||= 'undef';
Condition0%0/2
$password ||= "undef"
l!l
00
MC/DC0%0/1
$password ||= "undef"
$password
Truth table0%0/2
A
A= $password
Aresult
11
00
546 0 $self->set("password", $password, 1);
547 }
548  
549 1 ▶sub _get_1970_diff{ return int time / (3600 * 24) }
Subroutine _get_1970_diff: called CC=1 SCAR=0.0
550  
551 0 ▶sub _clean{
Subroutine _clean: not called CC=1 SCAR=6.9
552 0 my $specchars = \shift;
553 0 my $special = qr#\$|\*|\@|\^|\+|\.|\?|\)|\(|\||\]|\[|\{|\}#;
554 0 $$specchars =~ s/($special)/\\$1/g;
555 }
556  
557 1 1
558  
559 __END__
↑ Summary