lib/Rex/User/FreeBSD.pm

filter: Statementstmt 25.2% 46 / 182 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 62.5% 15 / 24 Podpod 0.0% 0 / 9 totaltotal 20.2% 61 / 302 CC 36 SCAR 52.9
CC
36
Cov
18%
create_user
506.0
create_group
56.0
rm_user
20.0
CRAP 198.2
« lib/Rex/Virtualization/LibVirt/import.pm ↑ Summary blib/lib/Rex.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::User::FreeBSD;
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::Helper::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Rex::Interface::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Rex::Interface::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Rex::User::Linux;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use base qw(Rex::User::Linux);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
28 0 my $that = shift;
29 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
30 0 my $self = $proto->SUPER::new(@_);
31  
32 0 bless( $self, $proto );
33  
34 0 return $self;
35 }
36  
37 0 ▶sub create_user {
Subroutine create_user: not called CC=22 SCAR=62.3
Pod undocumented
38 0 my ( $self, $user, $data ) = @_;
39  
40 0 my $cmd;
41  
42 0 my $uid = $self->get_uid($user);
43 0 my $should_create_home;
44  
45 0 my $old_pw_md5 = md5("/etc/passwd");
46  
47 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
48 0 $should_create_home = 1;
49 }
50 ▶ 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
51 0 $should_create_home = 0;
52 }
53 ▶ 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
54 || ( exists $data->{'no-create-home'} && $data->{'no-create-home'} == 0 ) )
55 {
56 0 $should_create_home = 1;
57 }
58  
59 0 ▶ if ( !defined $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uid) { } 0 0 0
60 0 Rex::Logger::debug("User $user does not exists. Creating it now.");
61 0 $cmd = "pw useradd ";
62 }
63 else {
64 0 Rex::Logger::debug("User $user already exists. Updating...");
65 0 $cmd = "pw usermod ";
66 }
67  
68 0 ▶ if ( $data->{"uid"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"uid"}) 0 0 0
69 0 $cmd .= " -u " . $data->{"uid"};
70 }
71  
72 0 ▶ if ( $data->{"home"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"home"}) 0 0 0
73 0 $cmd .= " -d " . $data->{"home"};
74 }
75  
76 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
77 0 $cmd .= " -m ";
78 }
79  
80 0 ▶ if ( exists $data->{shell} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"shell"}) 0 0 0
81 0 $cmd .= " -s " . $data->{shell};
82 }
83  
84 0 ▶ if ( $data->{"comment"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"comment"}) 0 0 0
85 0 $cmd .= " -c \"" . $data->{"comment"} . "\" ";
86 }
87  
88 0 ▶ if ( $data->{"expire"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"expire"}) 0 0 0
89 0 $cmd .= " -e " . $data->{"expire"};
90 }
91  
92 0 ▶ if ( $data->{"groups"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"groups"}) 0 0 0
93 0 my @groups = @{ $data->{groups} };
94 0 $cmd .= " -g " . $groups[0];
95 0 $cmd .= " -G " . join( ",", @groups );
96 }
97  
98 0 my $rnd_file = get_tmp_file;
99 0 my $fh = Rex::Interface::File->create;
100 0 $fh->open( ">", $rnd_file );
101 0 $fh->write("$cmd -n $user\nexit \$?\n");
102 0 $fh->close;
103  
104 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
105 0 my $retval = $?;
106 0 Rex::Interface::Fs->create()->unlink($rnd_file);
107  
108 0 ▶ if ( $retval == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval == 0) { } 0 0 0
109 0 Rex::Logger::debug("User $user created/updated.");
110 }
111 else {
112 0 Rex::Logger::info( "Error creating/updating user $user", "warn" );
113 0 die("Error creating/updating user $user");
114 }
115  
116 0 ▶ if ( exists $data->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"password"}) 0 0 0
117 0 Rex::Logger::debug("Changing password of $user.");
118  
119 0 $rnd_file = get_tmp_file;
120 0 $fh = Rex::Interface::File->create;
121 0 $fh->open( ">", $rnd_file );
122 $fh->write(
123 0 "echo '" . $data->{password} . "' | pw usermod $user -h 0\nexit \$?\n" );
124 0 $fh->close;
125  
126 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
127 0 my $pw_retval = $?;
128 0 Rex::Interface::Fs->create()->unlink($rnd_file);
129  
130 0 ▶ if ( $pw_retval != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pw_retval != 0) 0 0 0
131 0 die("Error setting password for $user");
132 }
133  
134 }
135  
136 0 my $new_pw_md5 = md5("/etc/passwd");
137  
138 0 ▶ if ( $new_pw_md5 eq $old_pw_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_pw_md5 eq $old_pw_md5) { } 0 0 0
139 return {
140 0 changed => 0,
141 ret => $self->get_uid($user),
142 };
143 }
144 else {
145 return {
146 0 changed => 1,
147 ret => $self->get_uid($user),
148 },
149 ;
150 }
151  
152 }
153  
154 0 ▶sub rm_user {
Subroutine rm_user: not called CC=4 SCAR=30.0
Pod undocumented
155 0 my ( $self, $user, $data ) = @_;
156  
157 0 Rex::Logger::debug("Removing user $user");
158  
159 0 my $cmd = "pw userdel";
160  
161 0 ▶ if ( exists $data->{delete_home} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"delete_home"}) 0 0 0
162 0 $cmd .= " -r ";
163 }
164  
165 0 my $output = i_run $cmd . " -n " . $user, fail_ok => 1;
166 0 ▶ if ( $? == 67 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == 67) { } 0 0 0
elsif ($? != 0) { } 0 0 0
167 0 Rex::Logger::info( "Cannot delete user $user (no such user)", "warn" );
168 }
169 elsif ( $? != 0 ) {
170 0 die("Error deleting user $user ($output)");
171 }
172  
173 }
174  
175 0 ▶sub get_uid {
Subroutine get_uid: not called CC=1 SCAR=6.9
Pod undocumented
176 0 my ( $self, $user ) = @_;
177  
178 0 my %data = $self->get_user($user);
179 0 return $data{uid};
180 }
181  
182 0 ▶sub get_user {
Subroutine get_user: not called CC=3 SCAR=24.8
Pod undocumented
183 0 my ( $self, $user ) = @_;
184  
185 0 Rex::Logger::debug("Getting information for $user");
186 0 my $rnd_file = get_tmp_file;
187 0 my $fh = Rex::Interface::File->create;
188 0 my $script = q|
189 unlink $0;
190 print to_json([ getpwnam($ARGV[0]) ]);
191 |;
192 0 $fh->open( ">", $rnd_file );
193 0 $fh->write($script);
194 0 $fh->write( func_to_json() );
195 0 $fh->close;
196  
197 0 my $data_str = i_run "perl $rnd_file $user", fail_ok => 1;
198 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
199 0 die("Error getting user information for $user");
200 }
201  
202 0 Rex::Interface::Fs->create()->unlink($rnd_file);
203  
204 0 my $data = decode_json($data_str);
205  
206 return (
207 0 ▶ name => $data->[0],
Branch: 1 branch
BranchTrueFalseTotal
exists $data->[9] ? : 0 0 0
208 password => $data->[1],
209 uid => $data->[2],
210 gid => $data->[3],
211 comment => $data->[5],
212 home => $data->[7],
213 shell => $data->[8],
214 expire => exists $data->[9] ? $data->[9] : 0,
215 );
216 }
217  
218 0 ▶sub create_group {
Subroutine create_group: not called CC=7 SCAR=40.3
Pod undocumented
219 0 my ( $self, $group, $data ) = @_;
220  
221 0 my $cmd;
222  
223 0 ▶ if ( !defined $self->get_gid($group) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->get_gid($group)) { } 0 0 0
224 0 Rex::Logger::debug("Creating new group $group");
225  
226 0 $cmd = "pw groupadd ";
227  
228 0 ▶ if ( exists $data->{gid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"gid"}) 0 0 0
229 0 $cmd .= " -g " . $data->{gid};
230 }
231  
232 0 i_run $cmd . " -n " . $group, fail_ok => 1;
233 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
234 0 die("Error creating/modifying group $group");
235 }
236 }
237 else {
238 0 Rex::Logger::debug("Group $group already exists. Updating...");
239  
240 # updating with pw groupmod doesn't work good in freebsd 10
241 # so we directly edit the /etc/group file
242 #$cmd = "pw groupmod ";
243  
244 0 ▶ if ( exists $data->{gid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"gid"}) 0 0 0
245 eval {
246 0 my @content = split( /\n/, cat("/etc/group") );
247 0 my $gid = $data->{gid};
248 0 for (@content) {
249 0 s/^$group:([^:]+):(\d+):/$group:$1:$gid:/;
250 }
251 0 my $fh = file_write("/etc/group");
252 0 $fh->write( join( "\n", @content ) );
253 0 $fh->close;
254 0 1;
255 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my @content = split(/\n/u, cat("/etc/group"), 0); my $gid = $data->{"gid"}; foreach $_ (@content) { s/^$group:([^:]+):(\d+):/${group}:$1:${gid}:/u; } ; my $fh = file_write("/etc/group"); $fh->write(join("\n", @content)); $fh->close; 1 } } or do { die "Error creating/modifying group $group" } 0 0 0
256 0 die("Error creating/modifying group $group");
257 };
258 }
259 }
260  
261 0 return $self->get_gid($group);
262  
263 }
264  
265 0 ▶sub get_gid {
Subroutine get_gid: not called CC=1 SCAR=6.9
Pod undocumented
266 0 my ( $self, $group ) = @_;
267  
268 0 my %data = $self->get_group($group);
269 0 return $data{gid};
270 }
271  
272 0 ▶sub get_group {
Subroutine get_group: not called CC=2 SCAR=17.9
Pod undocumented
273 0 my ( $self, $group ) = @_;
274  
275 0 Rex::Logger::debug("Getting information for $group");
276 0 my $rnd_file = get_tmp_file;
277 0 my $fh = Rex::Interface::File->create;
278 0 my $script = q|
279 unlink $0;
280 print to_json([ getgrnam($ARGV[0]) ]);
281 |;
282 0 $fh->open( ">", $rnd_file );
283 0 $fh->write($script);
284 0 $fh->write( func_to_json() );
285 0 $fh->close;
286  
287 0 my $data_str = i_run "perl $rnd_file $group", fail_ok => 1;
288 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
289 0 die("Error getting group information");
290 }
291  
292 0 Rex::Interface::Fs->create()->unlink($rnd_file);
293  
294 0 my $data = decode_json($data_str);
295  
296 return (
297 0 name => $data->[0],
298 password => $data->[1],
299 gid => $data->[2],
300 members => $data->[3],
301 );
302  
303 }
304  
305 0 ▶sub rm_group {
Subroutine rm_group: not called CC=2 SCAR=17.9
Pod undocumented
306 0 my ( $self, $group ) = @_;
307  
308 0 i_run "pw groupdel $group", fail_ok => 1;
309 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
310 0 die("Error deleting group $group");
311 }
312 }
313  
314 1 1;
« lib/Rex/Virtualization/LibVirt/import.pm ↑ Summary blib/lib/Rex.pm »