lib/Rex/User/SunOS.pm

filter: Statementstmt 35.4% 40 / 113 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 2 totaltotal 26.3% 53 / 201 CC 27 SCAR 61.0
CC
27
Cov
23%
create_user
702.0
new
6.0
BEGIN
1.0
CRAP 445.8
« lib/Rex/Interface/Connection/OpenSSH.pm ↑ Summary lib/Rex/User/OpenBSD.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::User::SunOS;
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::Run;
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::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Rex::User::OpenBSD;
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::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use base qw(Rex::User::OpenBSD);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
26 0 my $that = shift;
27 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
28 0 my $self = $proto->SUPER::new(@_);
29  
30 0 bless( $self, $proto );
31  
32 0 return $self;
33 }
34  
35 0 ▶sub create_user {
Subroutine create_user: not called CC=26 SCAR=65.5
Pod undocumented
36 0 my ( $self, $user, $data ) = @_;
37  
38 0 my $cmd;
39  
40 0 my $uid = $self->get_uid($user);
41 0 my $should_create_home;
42  
43 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
44 0 $should_create_home = 1;
45 }
46 ▶ 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
47 0 $should_create_home = 0;
48 }
49 ▶ 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
50 || ( exists $data->{'no-create-home'} && $data->{'no-create-home'} == 0 ) )
51 {
52 0 $should_create_home = 1;
53 }
54  
55 0 ▶ if ( !defined $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uid) { } 0 0 0
56 0 Rex::Logger::debug("User $user does not exists. Creating it now.");
57 0 $cmd = "useradd ";
58  
59 0 ▶ if ( exists $data->{system} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"system"}) 0 0 0
60 0 $cmd .= " -r";
61 }
62 }
63 else {
64 0 Rex::Logger::debug("User $user already exists. Updating...");
65  
66 0 $cmd = "usermod ";
67 }
68  
69 0 ▶ if ( exists $data->{uid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"uid"}) 0 0 0
70 0 $cmd .= " -u " . $data->{uid};
71 }
72  
73 0 ▶ if ( exists $data->{home} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"home"}) 0 0 0
74 0 $cmd .= " -d " . $data->{home};
75 }
76  
77 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
78 0 $cmd .= " -m ";
79 }
80  
81 0 ▶ if ( exists $data->{shell} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"shell"}) 0 0 0
82 0 $cmd .= " -s " . $data->{shell};
83 }
84  
85 0 ▶ if ( exists $data->{comment} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"comment"}) 0 0 0
86 0 $cmd .= " -c '" . $data->{comment} . "'";
87 }
88  
89 0 ▶ if ( exists $data->{expire} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"expire"}) 0 0 0
90 0 $cmd .= " -e '" . $data->{expire} . "'";
91 }
92  
93 0 ▶ if ( exists $data->{groups} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"groups"}) 0 0 0
94 0 my @groups = @{ $data->{groups} };
95 0 my $pri_group = shift @groups;
96  
97 0 $cmd .= " -g $pri_group";
98  
99 0 ▶ if (@groups) {
Branch: 1 branch
BranchTrueFalseTotal
if (@groups) 0 0 0
100 0 $cmd .= " -G " . join( ",", @groups );
101 }
102 }
103  
104 0 my $rnd_file = get_tmp_file;
105 0 my $fh = Rex::Interface::File->create;
106 0 $fh->open( ">", $rnd_file );
107 0 $fh->write("$cmd $user\nexit \$?\n");
108 0 $fh->close;
109  
110 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
111 0 ▶ if ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 0 0 0
112 0 Rex::Logger::debug("User $user created/updated.");
113 }
114 else {
115 0 Rex::Logger::info( "Error creating/updating user $user", "warn" );
116 0 die("Error creating/updating user $user");
117 }
118  
119 0 Rex::Interface::Fs->create()->unlink($rnd_file);
120  
121 0 ▶ if ( exists $data->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"password"}) 0 0 0
122 0 my $expect_path;
123  
124 0 ▶ if ( can_run("/usr/local/bin/expect") ) {
Branch: 2 branches
BranchTrueFalseTotal
if (can_run('/usr/local/bin/expect')) { } 0 0 0
elsif (can_run('/usr/bin/expect')) { } 0 0 0
125 0 $expect_path = "/usr/local/bin/expect";
126 }
127 elsif ( can_run("/usr/bin/expect") ) {
128 0 $expect_path = "/usr/bin/expect";
129 }
130  
131 0 ▶ if ($expect_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect_path) { } 0 0 0
132 0 my $chpasswd_file = get_tmp_file;
133 0 my $fh = file_write $chpasswd_file;
134 0 $fh->write(
135 qq~#!$expect_path --
136 # Input: username password
137 set USER [lindex \$argv 0]
138 set PASS [lindex \$argv 1]
139  
140 if { \$USER == "" || \$PASS == "" } {
141 puts "Usage: /tmp/chpasswd username password\n"
142 exit 1
143 }
144  
145 spawn passwd \$USER
146 expect "assword:"
147 send "\$PASS\r"
148 expect "assword:"
149 send "\$PASS\r"
150 expect eof
151 ~
152 );
153 0 $fh->close;
154  
155 0 $rnd_file = get_tmp_file;
156 0 $fh = Rex::Interface::File->create;
157 0 $fh->open( ">", $rnd_file );
158 $fh->write(
159 0 "$chpasswd_file $user '" . $data->{"password"} . "'\nexit \$?\n" );
160 0 $fh->close;
161  
162 0 chmod 700, $chpasswd_file;
163 0 i_run "/bin/sh $rnd_file", fail_ok => 1;
164 0 ▶ if ( $? != 0 ) { die("Error changing user's password."); }
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
165  
166 0 rm $chpasswd_file;
167 0 rm $rnd_file;
168 }
169 else {
170 0 die(
171 "No expect found in /usr/local/bin or /usr/bin. Can't set user password."
172 );
173 }
174 }
175  
176 return {
177 0 changed => 0,
178 ret => $self->get_uid($user),
179 };
180  
181 }
182  
183 1 1;
« lib/Rex/Interface/Connection/OpenSSH.pm ↑ Summary lib/Rex/User/OpenBSD.pm »