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