lib/Rex/User/Linux.pm

filter: Statementstmt 18.6% 49 / 263 Branchbran 0.0% 0 / 108 Conditioncond 2.2% 1 / 45 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 53.5% 15 / 28 Podpod 0.0% 0 / 13 totaltotal 13.4% 65 / 483 CC 70 SCAR 66.8
CC
70
Cov
12%
create_user
1806.0
create_group
110.0
rm_user
30.0
CRAP 798.3
« lib/Rex/CLI.pm ↑ Summary lib/Rex/Inventory/HP/ACU.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::User::Linux;
6  
7 2 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '1.16.1'; # VERSION
11  
12 2 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 require Rex::Commands;
14 2 ▶use Rex::Commands::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Rex::Helper::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Rex::Interface::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Rex::Interface::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use Rex::User::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use base qw(Rex::User::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
28 2 my $that = shift;
29 2 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
30 2 my $self = $proto->SUPER::new(@_);
31  
32 2 bless( $self, $proto );
33  
34 2 return $self;
35 }
36  
37 0 ▶sub create_user {
Subroutine create_user: not called CC=42 SCAR=75.0
Pod undocumented
38 0 my ( $self, $user, $data ) = @_;
39  
40 0 my $cmd;
41  
42 0 my $uid = $self->get_uid($user);
43  
44 0 my $run_cmd = 0;
45 0 my $should_create_home;
46  
47 # if any home creation intent has been defined,
48 # don't follow the default home creation policy
49 my $use_default_home_policy =
50 ( defined $data->{'create_home'}
51 || defined $data->{'create-home'}
52 || defined $data->{'no_create_home'}
53 0 ▶ || defined $data->{'no-create-home'} ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'create_home'} || defined $data->{'create-home'} || defined $data->{'no_create_home'} || defined $data->{'no-create-home'} ? : 0 0 0
Condition0%0/9
defined $data->{"create_home"} or defined $data->{"create-home"} or defined $data->{"no_create_home"} or defined $data->{"no-create-home"}
l!l&&r!l&&!r
000
defined $data->{"create_home"} or defined $data->{"create-home"} or defined $data->{"no_create_home"}
l!l&&r!l&&!r
000
defined $data->{"create_home"} or defined $data->{"create-home"}
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $data->{"create_home"} or defined $data->{"create-home"} or defined $data->{"no_create_home"} or defined $data->{"no-create-home"}
defined $data->{"create_home"} defined $data->{"create-home"} defined $data->{"no_create_home"} defined $data->{"no-create-home"}
Truth table0%0/5
A or B or C or D
A= defined $data->{"create_home"}B= defined $data->{"create-home"}C= defined $data->{"no_create_home"}D= defined $data->{"no-create-home"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
54  
55 0 ▶ if ( !$use_default_home_policy ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_default_home_policy) 0 0 0
56 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
57 0 $should_create_home = 1;
58 }
59 ▶ 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
60 0 $should_create_home = 0;
61 }
62 ▶ elsif (
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
63 ( exists $data->{'no_create_home'} && $data->{'no_create_home'} == 0 )
64 || ( exists $data->{'no-create-home'} && $data->{'no-create-home'} == 0 )
65 )
66 {
67 0 $should_create_home = 1;
68 }
69 }
70  
71 0 ▶ if ( !defined $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uid) { } 0 0 0
72 0 Rex::Logger::debug("User $user does not exists. Creating it now.");
73 0 $cmd = "/usr/sbin/useradd ";
74  
75 0 ▶ if ( exists $data->{system} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"system"}) 0 0 0
76 0 $cmd .= " -r";
77 }
78  
79 0 $run_cmd = 1;
80 }
81 else {
82 # only the user should be there, no modifications.
83 # so just return
84 0 ▶ if ( !defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
85 return {
86 0 changed => 0,
87 uid => $uid,
88 };
89 }
90  
91 0 Rex::Logger::debug("User $user already exists. Updating...");
92  
93 0 ▶ if ( exists $data->{uid} && $data->{uid} == $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"uid"} and $data->{"uid"} == $uid) 0 0 0
Condition0%0/3
exists $data->{"uid"} and $data->{"uid"} == $uid
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"uid"} and $data->{"uid"} == $uid
exists $data->{"uid"} $data->{"uid"} == $uid
Truth table0%0/3
A and B
A= exists $data->{"uid"}B= $data->{"uid"} == $uid
ABresult
0X0
100
111
94 0 delete $data->{uid};
95 }
96  
97 0 $cmd = "/usr/sbin/usermod ";
98 }
99  
100 0 ▶ if ( exists $data->{non_uniq} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"non_uniq"}) 0 0 0
101 0 $cmd .= " -o ";
102 0 $run_cmd = 1;
103 }
104  
105 0 ▶ if ( exists $data->{uid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"uid"}) 0 0 0
106 0 $cmd .= " --uid " . $data->{uid};
107 0 $run_cmd = 1;
108 }
109  
110 0 ▶ if ( exists $data->{home} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"home"}) 0 0 0
111 0 $run_cmd = 1;
112 0 $cmd .= " -d " . $data->{home};
113  
114 # don't create home directory in useradd mode if it already exists
115 0 ▶ $should_create_home = 0 if ( !defined $uid && is_dir( $data->{home} ) );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $uid and is_dir($data->{"home"}) 0 0 0
Condition0%0/3
not defined $uid and is_dir($data->{"home"})
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $uid and is_dir($data->{"home"})
not defined $uid is_dir($data->{"home"})
Truth table0%0/3
not(A) and B
A= not defined $uidB= is_dir($data->{"home"})
ABresult
0X0
100
111
116 }
117  
118 0 ▶ if ( !$use_default_home_policy ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_default_home_policy) 0 0 0
119 0 ▶ if ( !defined $uid ) { #useradd mode
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uid) { } 0 0 0
120 0 ▶ if ($should_create_home) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_create_home) { } 0 0 0
121 0 $cmd .= " -m ";
122 }
123 else {
124 0 $cmd .= " -M ";
125 }
126 }
127 else { #usermod mode
128 0 ▶ $cmd .= " -m " if ( exists $data->{home} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"home"} 0 0 0
129 }
130 }
131  
132 0 ▶ if ( exists $data->{shell} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"shell"}) 0 0 0
133 0 $run_cmd = 1;
134 0 $cmd .= " --shell " . $data->{shell};
135 }
136  
137 0 ▶ if ( exists $data->{comment} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"comment"}) 0 0 0
138 0 $run_cmd = 1;
139 0 $cmd .= " --comment '" . $data->{comment} . "'";
140 }
141  
142 0 ▶ if ( exists $data->{expire} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"expire"}) 0 0 0
143 0 $run_cmd = 1;
144 0 $cmd .= " --expiredate '" . $data->{expire} . "'";
145 }
146  
147 0 ▶ if ( exists $data->{groups} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"groups"}) 0 0 0
148 0 $run_cmd = 1;
149 0 my @groups = @{ $data->{groups} };
150 0 my $pri_group = shift @groups;
151  
152 0 $cmd .= " --gid $pri_group";
153  
154 0 ▶ if (@groups) {
Branch: 1 branch
BranchTrueFalseTotal
if (@groups) 0 0 0
155 0 $cmd .= " --groups " . join( ",", @groups );
156 }
157 }
158  
159 0 my $old_pw_md5 = md5("/etc/passwd");
160 0 my $old_sh_md5 = "";
161 0 eval { $old_sh_md5 = md5("/etc/shadow"); };
162  
163 # only run the cmd if needed
164 0 ▶ if ($run_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run_cmd) 0 0 0
165 0 my $rnd_file = get_tmp_file;
166 0 my $fh = Rex::Interface::File->create;
167 0 $fh->open( ">", $rnd_file );
168 0 $fh->write("rm \$0\n$cmd $user\nexit \$?\n");
169 0 $fh->close;
170  
171 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
172 0 ▶ if ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 0 0 0
173 0 Rex::Logger::debug("User $user created/updated.");
174 }
175 else {
176 0 Rex::Logger::info( "Error creating/updating user $user", "warn" );
177 0 die("Error creating/updating user $user");
178 }
179  
180 }
181  
182 0 ▶ if ( exists $data->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"password"}) 0 0 0
183 0 my $rnd_file = get_tmp_file;
184 0 my $fh = Rex::Interface::File->create;
185 0 $fh->open( ">", $rnd_file );
186 $fh->write( "rm \$0\n/bin/echo -e '"
187 . $data->{password} . "\\n"
188 . $data->{password}
189 0 . "' | /usr/bin/passwd $user\nexit \$?\n" );
190 0 $fh->close;
191  
192 0 Rex::Logger::debug("Changing password of $user.");
193 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
194 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
195 0 die("Error setting password for $user");
196 }
197  
198 }
199  
200 0 ▶ if ( exists $data->{crypt_password} && $data->{crypt_password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"crypt_password"} and $data->{"crypt_password"}) 0 0 0
Condition0%0/3
exists $data->{"crypt_password"} and $data->{"crypt_password"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"crypt_password"} and $data->{"crypt_password"}
exists $data->{"crypt_password"} $data->{"crypt_password"}
Truth table0%0/3
A and B
A= exists $data->{"crypt_password"}B= $data->{"crypt_password"}
ABresult
0X0
100
111
201 0 my $rnd_file = get_tmp_file;
202 0 my $fh = Rex::Interface::File->create;
203 0 $fh->open( ">", $rnd_file );
204 $fh->write( "rm \$0\nusermod -p '"
205 . $data->{crypt_password}
206 0 . "' $user\nexit \$?\n" );
207 0 $fh->close;
208  
209 0 Rex::Logger::debug("Setting encrypted password of $user");
210 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
211 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
212 0 die("Error setting password for $user");
213 }
214 }
215  
216 0 my $new_pw_md5 = md5("/etc/passwd");
217 0 my $new_sh_md5 = "";
218 0 eval { $new_sh_md5 = md5("/etc/shadow"); };
219  
220 0 ▶ if ( $new_pw_md5 eq $old_pw_md5 && $new_sh_md5 eq $old_sh_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_pw_md5 eq $old_pw_md5 and $new_sh_md5 eq $old_sh_md5) { } 0 0 0
Condition0%0/3
$new_pw_md5 eq $old_pw_md5 and $new_sh_md5 eq $old_sh_md5
!ll&&!rl&&r
000
MC/DC0%0/2
$new_pw_md5 eq $old_pw_md5 and $new_sh_md5 eq $old_sh_md5
$new_pw_md5 eq $old_pw_md5 $new_sh_md5 eq $old_sh_md5
Truth table0%0/3
A and B
A= $new_pw_md5 eq $old_pw_md5B= $new_sh_md5 eq $old_sh_md5
ABresult
0X0
100
111
221 return {
222 0 changed => 0,
223 ret => $self->get_uid($user),
224 };
225 }
226 else {
227 return {
228 0 changed => 1,
229 ret => $self->get_uid($user),
230 },
231 ;
232 }
233  
234 }
235  
236 0 ▶sub rm_user {
Subroutine rm_user: not called CC=5 SCAR=34.0
Pod undocumented
237 0 my ( $self, $user, $data ) = @_;
238  
239 0 Rex::Logger::debug("Removing user $user");
240  
241 0 my $cmd = "/usr/sbin/userdel";
242  
243 0 ▶ if ( exists $data->{delete_home} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"delete_home"}) 0 0 0
244 0 $cmd .= " --remove";
245 }
246  
247 0 ▶ if ( exists $data->{force} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"force"}) 0 0 0
248 0 $cmd .= " --force";
249 }
250  
251 0 my $output = i_run $cmd . " " . $user, fail_ok => 1;
252 0 ▶ if ( $? == 6 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($? == 6) { } 0 0 0
elsif ($? != 0) { } 0 0 0
253 0 Rex::Logger::info( "Cannot delete user $user (no such user)", "warn" );
254 }
255 elsif ( $? != 0 ) {
256 0 die("Error deleting user $user ($output)");
257 }
258  
259 }
260  
261 0 ▶sub get_uid {
Subroutine get_uid: not called CC=1 SCAR=6.9
Pod undocumented
262 0 my ( $self, $user ) = @_;
263  
264 0 my %data = $self->get_user($user);
265 0 return $data{uid};
266 }
267  
268 0 ▶sub user_groups {
Subroutine user_groups: not called CC=4 SCAR=30.0
Pod undocumented
269 0 my ( $self, $user ) = @_;
270  
271 0 Rex::Logger::debug("Getting group membership of $user");
272 0 my $rnd_file = get_tmp_file;
273 0 my $fh = Rex::Interface::File->create;
274 0 my $script = q|
275 unlink $0;
276 $exe = "/usr/bin/groups";
277 if(! -x $exe) {
278 $exe = "/bin/groups";
279 } print to_json([ map {chomp; $_ =~ s/^[^:]*:\s*(.*)\s*$/$1/; split / /, $_} qx{$exe $ARGV[0]} ]);
280  
281 |;
282  
283 0 $fh->open( ">", $rnd_file );
284 0 $fh->write($script);
285 0 $fh->write( func_to_json() );
286 0 $fh->close;
287  
288 0 my $data_str = i_run "perl $rnd_file $user", fail_ok => 1;
289 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
290 0 die("Error getting group list");
291 }
292  
293 0 my $data = decode_json($data_str);
294  
295 0 my $wantarray = wantarray();
296  
297 0 ▶ if ( defined $wantarray && !$wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wantarray and not $wantarray) 0 0 0
Condition0%0/3
defined $wantarray and not $wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
defined $wantarray and not $wantarray
defined $wantarray not $wantarray
Truth table0%0/3
A and not(B)
A= defined $wantarrayB= not $wantarray
ABresult
0X0
100
111
298  
299 # arrayref
300 0 return $data;
301 }
302  
303 0 return @{$data};
304 }
305  
306 0 ▶sub user_list {
Subroutine user_list: not called CC=2 SCAR=17.9
Pod undocumented
307 0 my $self = shift;
308  
309 0 Rex::Logger::debug("Getting user list");
310 0 my $rnd_file = get_tmp_file;
311 0 my $script = q|
312 unlink $0;
313 print to_json([ map {chomp; $_ =~ s/^([^:]*):.*$/$1/; $_} qx{/usr/bin/getent passwd} ]);
314 |;
315 0 my $fh = Rex::Interface::File->create;
316 0 $fh->open( ">", $rnd_file );
317 0 $fh->write($script);
318 0 $fh->write( func_to_json() );
319 0 $fh->close;
320  
321 0 my $data_str = i_run "perl $rnd_file", fail_ok => 1;
322 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
323 0 die("Error getting user list");
324 }
325  
326 0 my $data = decode_json($data_str);
327  
328 0 return @$data;
329 }
330  
331 0 ▶sub get_user {
Subroutine get_user: not called CC=3 SCAR=24.8
Pod undocumented
332 0 my ( $self, $user ) = @_;
333  
334 0 Rex::Logger::debug("Getting information for $user");
335 0 my $rnd_file = get_tmp_file;
336 0 my $fh = Rex::Interface::File->create;
337 0 my $script = q|
338 unlink $0;
339 print to_json([ getpwnam($ARGV[0]) ]);
340 |;
341 0 $fh->open( ">", $rnd_file );
342 0 $fh->write($script);
343 0 $fh->write( func_to_json() );
344 0 $fh->close;
345  
346 0 my $data_str = i_run "perl $rnd_file $user", fail_ok => 1;
347 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
348 0 die("Error getting user information for $user");
349 }
350  
351 0 my $data = decode_json($data_str);
352  
353 return (
354 0 ▶ name => $data->[0],
Branch: 1 branch
BranchTrueFalseTotal
exists $data->[9] ? : 0 0 0
355 password => $data->[1],
356 uid => $data->[2],
357 gid => $data->[3],
358 comment => $data->[5],
359 home => $data->[7],
360 shell => $data->[8],
361 expire => exists $data->[9] ? $data->[9] : 0,
362 );
363 }
364  
365 0 ▶sub lock_password {
Subroutine lock_password: not called CC=4 SCAR=30.0
Pod undocumented
366 0 my ( $self, $user ) = @_;
367  
368 # Is the password already locked?
369 0 my $result = i_run "passwd --status $user", fail_ok => 1;
370  
371 0 ▶ die "Unexpected result from passwd: $result"
Branch: 1 branch
BranchTrueFalseTotal
unless $result =~ /^$user\s+(L|NP|P)\s+/u 0 0 0
372 unless $result =~ /^$user\s+(L|NP|P)\s+/;
373  
374 0 ▶ if ( $1 eq 'L' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'L') { } 0 0 0
375  
376 # Already locked
377 0 return { changed => 0 };
378 }
379 else {
380 0 my $ret = i_run "passwd --lock $user", fail_ok => 1;
381 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
382 0 die("Error locking account $user: $ret");
383 }
384 return {
385 0 changed => 1,
386 ret => $ret,
387 };
388 }
389 }
390  
391 0 ▶sub unlock_password {
Subroutine unlock_password: not called CC=4 SCAR=30.0
Pod undocumented
392 0 my ( $self, $user ) = @_;
393  
394 # Is the password already unlocked?
395 0 my $result = i_run "passwd --status $user", fail_ok => 1;
396  
397 0 ▶ die "Unexpected result from passwd: $result"
Branch: 1 branch
BranchTrueFalseTotal
unless $result =~ /^$user\s+(L|NP|P)\s+/u 0 0 0
398 unless $result =~ /^$user\s+(L|NP|P)\s+/;
399  
400 0 ▶ if ( $1 eq 'P' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'P') { } 0 0 0
401  
402 # Already unlocked
403 0 return { changed => 0 };
404 }
405 else {
406 # Capture error string on failure (eg. account has no password)
407 0 ▶ my ( $ret, $err ) = i_run "passwd --unlock $user", sub { @_ }, fail_ok => 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
408 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
409 0 die("Error unlocking account $user: $err");
410 }
411 return {
412 0 changed => 1,
413 ret => $ret,
414 };
415 }
416 }
417  
418 0 ▶sub create_group {
Subroutine create_group: not called CC=10 SCAR=47.0
Pod undocumented
419 0 my ( $self, $group, $data ) = @_;
420  
421 0 my $cmd;
422  
423 0 my $gid = $self->get_gid($group);
424  
425 0 ▶ if ( !defined $gid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $gid) { } 0 0 0
elsif (exists $data->{'gid'} and $data->{'gid'} == $gid) { } 0 0 0
426 0 Rex::Logger::debug("Creating new group $group");
427  
428 0 $cmd = "/usr/sbin/groupadd ";
429 }
430 ▶ elsif ( exists $data->{gid} && $data->{gid} == $gid ) {
Condition0%0/3
exists $data->{"gid"} and $data->{"gid"} == $gid
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"gid"} and $data->{"gid"} == $gid
exists $data->{"gid"} $data->{"gid"} == $gid
Truth table0%0/3
A and B
A= exists $data->{"gid"}B= $data->{"gid"} == $gid
ABresult
0X0
100
111
431 0 ▶ if ( Rex::Config->get_do_reporting ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Config"->get_do_reporting) 0 0 0
432 return {
433 0 changed => 0,
434 ret => $gid,
435 };
436 }
437 0 return $gid;
438 }
439 else {
440 0 ▶ if ( !defined $data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
441 0 ▶ if ( Rex::Config->get_do_reporting ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Config"->get_do_reporting) 0 0 0
442 return {
443 0 changed => 0,
444 ret => $gid,
445 };
446 }
447  
448 0 return $gid;
449 }
450 0 Rex::Logger::debug("Group $group already exists. Updating...");
451 0 $cmd = "/usr/sbin/groupmod ";
452 }
453  
454 0 ▶ if ( exists $data->{gid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"gid"}) 0 0 0
455 0 $cmd .= " -g " . $data->{gid};
456 0 $gid = undef;
457 }
458  
459 0 i_run $cmd . " " . $group, fail_ok => 1;
460 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
461 0 die("Error creating/modifying group $group");
462 }
463  
464 0 ▶ if ( defined $gid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gid) 0 0 0
465 0 return $gid;
466 }
467  
468 0 return $self->get_gid($group);
469 }
470  
471 0 ▶sub get_gid {
Subroutine get_gid: not called CC=1 SCAR=6.9
Pod undocumented
472 0 my ( $self, $group ) = @_;
473  
474 0 my %data = $self->get_group($group);
475 0 return $data{gid};
476 }
477  
478 0 ▶sub get_group {
Subroutine get_group: not called CC=2 SCAR=17.9
Pod undocumented
479 0 my ( $self, $group ) = @_;
480  
481 0 Rex::Logger::debug("Getting information for $group");
482 0 my @data =
483 split(
484 " ",
485 ""
486 . i_run(
487 "perl -le 'print join(\" \", getgrnam(\$ARGV[0]));' '$group'",
488 fail_ok => 1
489 ),
490 4
491 );
492 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
493 0 die("Error getting group information");
494 }
495  
496 return (
497 0 name => $data[0],
498 password => $data[1],
499 gid => $data[2],
500 members => $data[3],
501 );
502 }
503  
504 0 ▶sub rm_group {
Subroutine rm_group: not called CC=2 SCAR=17.9
Pod undocumented
505 0 my ( $self, $group ) = @_;
506  
507 0 i_run "/usr/sbin/groupdel $group", fail_ok => 1;
508 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
509 0 die("Error deleting group $group");
510 }
511 }
512  
513 2 1;
« lib/Rex/CLI.pm ↑ Summary lib/Rex/Inventory/HP/ACU.pm »