File Coverage

lib/Rex/User/SunOS.pm
Criterion Covered Total %
statement 38 111 34.2
branch 0 38 0.0
condition 0 21 0.0
subroutine 13 15 86.6
pod 0 2 0.0
total 51 187 27.2


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::User::SunOS;
6              
7 1     1   23 use v5.12.5;
  1         4  
8 1     1   14 use warnings;
  1         2  
  1         45  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 1     1   22 use Rex::Logger;
  1         7  
  1         13  
13 1     1   30 use Rex::Commands::Run;
  1         3  
  1         9  
14 1     1   17 use Rex::Helper::Run;
  1         8  
  1         63  
15 1     1   7 use Rex::Commands::Fs;
  1         8  
  1         7  
16 1     1   16 use Rex::Commands::File;
  1         2  
  1         13  
17 1     1   14 use Rex::User::OpenBSD;
  1         6  
  1         14  
18 1     1   27 use Rex::Interface::File;
  1         8  
  1         14  
19 1     1   81 use Rex::Interface::Fs;
  1         5  
  1         20  
20 1     1   43 use Rex::Interface::Exec;
  1         2  
  1         8  
21 1     1   47 use Rex::Helper::Path;
  1         6  
  1         78  
22              
23 1     1   7 use base qw(Rex::User::OpenBSD);
  1         3  
  1         977  
24              
25             sub new {
26 0     0 0   my $that = shift;
27 0   0       my $proto = ref($that) || $that;
28 0           my $self = $proto->SUPER::new(@_);
29              
30 0           bless( $self, $proto );
31              
32 0           return $self;
33             }
34              
35             sub create_user {
36 0     0 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 0 0       if ( $data->{'create_home'} || $data->{'create-home'} ) {
    0 0        
    0 0        
      0        
      0        
44 0           $should_create_home = 1;
45             }
46             elsif ( $data->{'no_create_home'} || $data->{'no-create-home'} ) {
47 0           $should_create_home = 0;
48             }
49             elsif ( ( exists $data->{'no_create_home'} && $data->{'no_create_home'} == 0 )
50             || ( exists $data->{'no-create-home'} && $data->{'no-create-home'} == 0 ) )
51             {
52 0           $should_create_home = 1;
53             }
54              
55 0 0         if ( !defined $uid ) {
56 0           Rex::Logger::debug("User $user does not exists. Creating it now.");
57 0           $cmd = "useradd ";
58              
59 0 0         if ( exists $data->{system} ) {
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 0         if ( exists $data->{uid} ) {
70 0           $cmd .= " -u " . $data->{uid};
71             }
72              
73 0 0         if ( exists $data->{home} ) {
74 0           $cmd .= " -d " . $data->{home};
75             }
76              
77 0 0 0       if ( $should_create_home && !defined $uid ) { #useradd mode
78 0           $cmd .= " -m ";
79             }
80              
81 0 0         if ( exists $data->{shell} ) {
82 0           $cmd .= " -s " . $data->{shell};
83             }
84              
85 0 0         if ( exists $data->{comment} ) {
86 0           $cmd .= " -c '" . $data->{comment} . "'";
87             }
88              
89 0 0         if ( exists $data->{expire} ) {
90 0           $cmd .= " -e '" . $data->{expire} . "'";
91             }
92              
93 0 0         if ( exists $data->{groups} ) {
94 0           my @groups = @{ $data->{groups} };
  0            
95 0           my $pri_group = shift @groups;
96              
97 0           $cmd .= " -g $pri_group";
98              
99 0 0         if (@groups) {
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 0         if ( $? == 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 0         if ( exists $data->{password} ) {
122 0           my $expect_path;
123              
124 0 0         if ( can_run("/usr/local/bin/expect") ) {
    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 0         if ($expect_path) {
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 0         if ( $? != 0 ) { die("Error changing user's password."); }
  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;