lib/Rex/User/NetBSD.pm

filter: Statementstmt 28.6% 37 / 129 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 3 totaltotal 21.0% 49 / 233 CC 33 SCAR 59.8
CC
33
Cov
18%
create_user
702.0
rm_user
56.0
new
6.0
CRAP 397.2
« lib/Rex/Pkg/Gentoo.pm ↑ Summary lib/Rex/Commands/Partition.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::User::NetBSD;
6  
7 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.16.1'; # VERSION
11  
12 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Rex::Commands::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Rex::User::Linux;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Rex::Interface::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Rex::Interface::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use base qw(Rex::User::Linux);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
25 0 my $that = shift;
26 0 ▶ my $proto = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
27 0 my $self = $proto->SUPER::new(@_);
28  
29 0 bless( $self, $proto );
30  
31 0 return $self;
32 }
33  
34 0 ▶sub create_user {
Subroutine create_user: not called CC=26 SCAR=65.5
Pod undocumented
35 0 my ( $self, $user, $data ) = @_;
36  
37 0 my $cmd;
38  
39 0 my $uid = $self->get_uid($user);
40 0 my $should_create_home;
41  
42 0 my $old_pw_md5 = md5("/etc/passwd");
43  
44 0 ▶ if ( $data->{'create_home'} || $data->{'create-home'} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($data->{'create_home'} or $data->{'create-home'}) { } 0 0 0
elsif ($data->{'no_create_home'} or $data->{'no-create-home'}) { } 0 0 0
elsif (exists $data->{'no_create_home'} and $data->{'no_create_home'} == 0 or exists $data->{'no-create-home'} and $data->{'no-create-home'} == 0) { } 0 0 0
Condition0%0/3
$data->{"create_home"} or $data->{"create-home"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"create_home"} or $data->{"create-home"}
$data->{"create_home"} $data->{"create-home"}
Truth table0%0/3
A or B
A= $data->{"create_home"}B= $data->{"create-home"}
ABresult
1X1
011
000
45 0 $should_create_home = 1;
46 }
47 ▶ elsif ( $data->{'no_create_home'} || $data->{'no-create-home'} ) {
Condition0%0/3
$data->{"no_create_home"} or $data->{"no-create-home"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"no_create_home"} or $data->{"no-create-home"}
$data->{"no_create_home"} $data->{"no-create-home"}
Truth table0%0/3
A or B
A= $data->{"no_create_home"}B= $data->{"no-create-home"}
ABresult
1X1
011
000
48 0 $should_create_home = 0;
49 }
50 ▶ elsif ( ( exists $data->{'no_create_home'} && $data->{'no_create_home'} == 0 )
Condition0%0/9
exists $data->{"no_create_home"} and $data->{"no_create_home"} == 0 or exists $data->{"no-create-home"} and $data->{"no-create-home"} == 0
l!l&&r!l&&!r
000
exists $data->{"no_create_home"} and $data->{"no_create_home"} == 0
!ll&&!rl&&r
000
exists $data->{"no-create-home"} and $data->{"no-create-home"} == 0
!ll&&!rl&&r
000
MC/DC0%0/4
exists $data->{"no_create_home"} and $data->{"no_create_home"} == 0 or exists $data->{"no-create-home"} and $data->{"no-create-home"} == 0
exists $data->{"no_create_home"} $data->{"no_create_home"} == 0 exists $data->{"no-create-home"} $data->{"no-create-home"} == 0
Truth table0%0/7
A and B or C and D
A= exists $data->{"no_create_home"}B= $data->{"no_create_home"} == 0C= exists $data->{"no-create-home"}D= $data->{"no-create-home"} == 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
51 || ( exists $data->{'no-create-home'} && $data->{'no-create-home'} == 0 ) )
52 {
53 0 $should_create_home = 1;
54 }
55  
56 0 ▶ if ( !defined $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uid) { } 0 0 0
57 0 Rex::Logger::debug("User $user does not exists. Creating it now.");
58 0 $cmd = "useradd ";
59  
60 0 ▶ if ( exists $data->{system} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"system"}) 0 0 0
61 0 $cmd .= " -r";
62 }
63 }
64 else {
65 0 Rex::Logger::debug("User $user already exists. Updating...");
66  
67 0 $cmd = "usermod ";
68 }
69  
70 0 ▶ if ( exists $data->{uid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"uid"}) 0 0 0
71 0 $cmd .= " -u " . $data->{uid};
72 }
73  
74 0 ▶ if ( exists $data->{home} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"home"}) 0 0 0
75 0 $cmd .= " -d " . $data->{home};
76 }
77  
78 0 ▶ if ( $should_create_home && !defined $uid ) { #useradd mode
Branch: 1 branch
BranchTrueFalseTotal
if ($should_create_home and not defined $uid) 0 0 0
Condition0%0/3
$should_create_home and not defined $uid
!ll&&!rl&&r
000
MC/DC0%0/2
$should_create_home and not defined $uid
$should_create_home not defined $uid
Truth table0%0/3
A and not(B)
A= $should_create_homeB= not defined $uid
ABresult
0X0
100
111
79 0 $cmd .= " -m ";
80 }
81  
82 0 ▶ if ( exists $data->{shell} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"shell"}) 0 0 0
83 0 $cmd .= " -s " . $data->{shell};
84 }
85  
86 0 ▶ if ( exists $data->{comment} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"comment"}) 0 0 0
87 0 $cmd .= " -c '" . $data->{comment} . "'";
88 }
89  
90 0 ▶ if ( exists $data->{expire} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"expire"}) 0 0 0
91 0 $cmd .= " -e '" . $data->{expire} . "'";
92 }
93  
94 0 ▶ if ( exists $data->{groups} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"groups"}) 0 0 0
95 0 my @groups = @{ $data->{groups} };
96 0 my $pri_group = shift @groups;
97  
98 0 $cmd .= " -g $pri_group";
99  
100 0 ▶ if (@groups) {
Branch: 1 branch
BranchTrueFalseTotal
if (@groups) 0 0 0
101 0 $cmd .= " -G " . join( ",", @groups );
102 }
103 }
104  
105 0 my $rnd_file = get_tmp_file;
106 0 my $fh = Rex::Interface::File->create;
107 0 $fh->open( ">", $rnd_file );
108 0 $fh->write("$cmd $user\nexit \$?\n");
109 0 $fh->close;
110  
111 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
112 0 ▶ if ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 0 0 0
113 0 Rex::Logger::debug("User $user created/updated.");
114 }
115 else {
116 0 Rex::Logger::info( "Error creating/updating user $user", "warn" );
117 0 die("Error creating/updating user $user");
118 }
119  
120 0 Rex::Interface::Fs->create()->unlink($rnd_file);
121  
122 0 ▶ if ( exists $data->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"password"}) 0 0 0
123 0 Rex::Logger::debug("Changing password of $user.");
124  
125 0 $rnd_file = get_tmp_file;
126 0 $fh = Rex::Interface::File->create;
127 0 $fh->open( ">", $rnd_file );
128 $fh->write(
129 0 "usermod -p \$(pwhash '" . $data->{password} . "') $user\nexit \$?\n" );
130 0 $fh->close;
131  
132 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
133 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
134 0 die("Error setting password for $user");
135 }
136  
137 0 Rex::Interface::Fs->create()->unlink($rnd_file);
138 }
139  
140 0 ▶ if ( exists $data->{crypt_password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"crypt_password"}) 0 0 0
141 0 Rex::Logger::debug("Setting encrypted password of $user");
142  
143 0 $rnd_file = get_tmp_file;
144 0 $fh = Rex::Interface::File->create;
145 0 $fh->open( ">", $rnd_file );
146 $fh->write(
147 0 "usermod -p '" . $data->{crypt_password} . "' $user\nexit \$?\n" );
148 0 $fh->close;
149  
150 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
151 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
152 0 die("Error setting password for $user");
153 }
154  
155 0 Rex::Interface::Fs->create()->unlink($rnd_file);
156 }
157  
158 0 my $new_pw_md5 = md5("/etc/passwd");
159  
160 0 ▶ if ( $new_pw_md5 eq $old_pw_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_pw_md5 eq $old_pw_md5) { } 0 0 0
161 return {
162 0 changed => 0,
163 ret => $self->get_uid($user),
164 };
165 }
166 else {
167 return {
168 0 changed => 1,
169 ret => $self->get_uid($user),
170 },
171 ;
172 }
173  
174 }
175  
176 0 ▶sub rm_user {
Subroutine rm_user: not called CC=7 SCAR=40.3
Pod undocumented
177 0 my ( $self, $user, $data ) = @_;
178  
179 0 Rex::Logger::debug("Removing user $user");
180  
181 0 my %user_info = $self->get_user($user);
182  
183 0 my $cmd = "userdel";
184  
185 0 ▶ if ( exists $data->{delete_home} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"delete_home"}) 0 0 0
186 0 $cmd .= " -r";
187 }
188  
189 0 my $output = i_run $cmd . " " . $user, fail_ok => 1;
190 0 ▶ if ( $? == 67 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == 67) { } 0 0 0
elsif ($? != 0) { } 0 0 0
191 0 Rex::Logger::info( "Cannot delete user $user (no such user)", "warn" );
192 }
193 elsif ( $? != 0 ) {
194 0 die("Error deleting user $user ($output)");
195 }
196  
197 0 ▶ if ( exists $data->{delete_home} && is_dir( $user_info{home} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"delete_home"} and is_dir($user_info{"home"})) 0 0 0
Condition0%0/3
exists $data->{"delete_home"} and is_dir($user_info{"home"})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"delete_home"} and is_dir($user_info{"home"})
exists $data->{"delete_home"} is_dir($user_info{"home"})
Truth table0%0/3
A and B
A= exists $data->{"delete_home"}B= is_dir($user_info{"home"})
ABresult
0X0
100
111
198 0 Rex::Logger::debug(
199 "userdel doesn't delete home directory. removing it now by hand...");
200 0 rmdir $user_info{home};
201 }
202  
203 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
204 0 die( "Error removing " . $user_info{home} );
205 }
206  
207 }
208  
209 1 1;
« lib/Rex/Pkg/Gentoo.pm ↑ Summary lib/Rex/Commands/Partition.pm »