source: repository/lib/Metabrik/Remote/Winexe.pm

Last change on this file was 942:170e9944f7e0, checked in by GomoR <gomor@…>, 6 weeks ago
  • UPDATE: *: debug base Attribute has been removed. Update all Briks by removing $self->debug.
  • UPDATE: *: global, context, shell base Attributes are no more initialized with a default Brik, they may now be undefined. Update all Briks by checking definedness when $self->global, $self->context or $self->shell are used.
File size: 6.2 KB
Line 
1#
2# $Id$
3#
4# remote::winexe Brik
5#
6package Metabrik::Remote::Winexe;
7use strict;
8use warnings;
9
10use base qw(Metabrik::Shell::Command Metabrik::System::Package);
11
12sub brik_properties {
13   return {
14      revision => '$Revision$',
15      tags => [ qw(unstable) ],
16      author => 'GomoR <GomoR[at]metabrik.org>',
17      license => 'http://opensource.org/licenses/BSD-3-Clause',
18      attributes => {
19         datadir => [ qw(datadir) ],
20         host => [ qw(host) ],
21         user => [ qw(username) ],
22         password => [ qw(password) ],
23      },
24      attributes_default => {
25      },
26      commands => {
27         install => [ ], # Inherited
28         execute => [ qw(command host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
29         execute_in_background => [ qw(command host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
30      },
31      require_modules => {
32         'Metabrik::System::File' => [ ],
33      },
34      require_binaries => {
35         tar => [ ],
36         winexe => [ ],
37      },
38      need_packages => {
39         ubuntu => [  qw(build-essential autoconf) ],
40         debian => [  qw(build-essential autoconf) ],
41      },
42   };
43}
44
45#
46# Compilation process
47#
48# http://techedemic.com/2014/09/17/installing-wmic-in-ubuntu-14-04-lts-64-bit/
49# http://wiki.monitoring-fr.org/nagios/windows-client/superivision-wmi
50#
51# 2017-02-20: New compilation process for winexe 1.1:
52#
53# http://rand0m.org/2015/08/05/winexe-1-1-centos-6-and-windows-2012-r2/
54# https://sourceforge.net/p/winexe/winexe-waf/ci/master/tree/
55#
56# sudo apt-get install gcc-mingw-w64 comerr-dev libpopt-dev libbsd-dev zlib1g-dev
57#    libc6-dev python-dev gnutls-dev acl-dev libldap-dev
58# git clone git://git.code.sf.net/p/winexe/winexe-waf winexe-winexe-waf
59# wget https://download.samba.org/pub/samba/stable/samba-4.1.23.tar.gz
60# tar zxvf samba-4.1.23.tar.gz
61# cd winexe-winexe-waf/source
62# vi wscript_build
63# -        stlib='smb_static bsd z resolv rt',
64# -        lib='dl'
65# +        stlib='smb_static z rt',
66# +        lib='dl resolv bsd'
67# ./waf --samba-dir=../../samba-4.1.23 configure build
68# cp build/winexe-static /usr/local/bin/winexe11
69#
70sub install {
71   my $self = shift;
72
73   # Install needed packages
74   $self->SUPER::install() or return;
75
76   my $datadir = $self->datadir;
77
78   my $version = '1.3.14';
79
80   my $url = 'http://www.openvas.org/download/wmi/wmi-'.$version.'.tar.bz2';
81   my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
82   my $files = $cw->mirror($url, "wmi-$version.tar.bz2", $datadir) or return;
83
84   if (@$files > 0) {
85      my $cmd = "tar jxvf $datadir/wmi-$version.tar.bz2 -C $datadir/";
86      $self->SUPER::execute($cmd) or return;
87   }
88
89   # cd wmi-$version/Samba/source
90   # ./autogen.sh
91   # ./configure
92   # make "CPP=gcc -E -ffreestanding"
93   # make proto bin/wmic
94   # make proto bin/winexe
95
96   my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp';
97   if (defined($self->shell)) {
98      $self->shell->run_cd("$datadir/wmi-$version/Samba/source") or return;
99   }
100   else {
101      chdir("$datadir/wmi-$version/Samba/source")
102         or return $self->log->error("install: chdir: $!");
103   }
104
105   $self->system('./autogen.sh') or return;
106   $self->system('./configure') or return;
107   $self->system('make "CPP=gcc -E -ffreestanding"') or return;
108   $self->system('make proto bin/wmic') or return;
109   $self->system('make proto bin/winexe') or return;
110
111   if (defined($self->shell)) {
112      $self->shell->run_cd($cwd);
113   }
114   else {
115      chdir($cwd) or return $self->log->error("install: chdir: $!");
116   }
117
118   my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
119   $sf->copy("$datadir/wmi-$version/Samba/source/bin/wmic", '/usr/local/bin/') or return;
120   $sf->copy("$datadir/wmi-$version/Samba/source/bin/winexe", '/usr/local/bin/') or return;
121
122   return 1;
123}
124
125#
126# A. Activate file sharing on local network
127#
128# B. Instructions to activate WINEXESVC under Windows 7
129#
130# 1. Add LocalAccountTokenFilterPolicy registry key
131#
132#   runas administrator a cmd.exe
133#
134#   reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\system"
135#      /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f
136#
137# 2. Add winexesvc service (update: not necessary since winexe 1.1, it installs the
138#    service by itself)
139#
140#   runas administrator a cmd.exe
141#
142# C:\> sc create winexesvc binPath= C:\WINDOWS\WINEXESVC.EXE start= auto DisplayName= winexesvc
143# C:\> sc description winexesvc "Remote command provider"
144#
145sub execute {
146   my $self = shift;
147   my ($command, $host, $user, $password) = @_;
148
149   $host ||= $self->host;
150   $user ||= $self->user;
151   $password ||= $self->password;
152   $self->brik_help_run_undef_arg('execute', $command) or return;
153   $self->brik_help_run_undef_arg('execute', $host) or return;
154   $self->brik_help_run_undef_arg('execute', $user) or return;
155   $self->brik_help_run_undef_arg('execute', $password) or return;
156
157   my $winexe = 'winexe';
158
159   # If winexe 1.1 is available, we use it instead of venerable winexe from wmi package.
160   if (-f '/usr/local/bin/winexe11') {
161      $self->log->verbose("execute: winexe11 found, using it");
162      $winexe = 'winexe11';
163   }
164
165   # Do not put $command between quotes, let user do it.
166   my $cmd = "$winexe -U$user".'%'."$password //$host $command";
167
168   $self->log->verbose("execute: cmd[$cmd]");
169
170   return $self->SUPER::execute($cmd);
171}
172
173sub execute_in_background {
174   my $self = shift;
175   my ($command, $host, $user, $password) = @_;
176
177   $host ||= $self->host;
178   $user ||= $self->user;
179   $password ||= $self->password;
180   $self->brik_help_run_undef_arg('execute_in_background', $command) or return;
181   $self->brik_help_run_undef_arg('execute_in_background', $host) or return;
182   $self->brik_help_run_undef_arg('execute_in_background', $user) or return;
183   $self->brik_help_run_undef_arg('execute_in_background', $password) or return;
184
185   # Do not put $command between quotes, let user do it.
186   my $cmd = "winexe -U$user".'%'."$password //$host $command &";
187
188   return $self->SUPER::execute($cmd);
189}
190
1911;
192
193__END__
194
195=head1 NAME
196
197Metabrik::Remote::Winexe - remote::winexe Brik
198
199=head1 COPYRIGHT AND LICENSE
200
201Copyright (c) 2014-2017, Patrice E<lt>GomoRE<gt> Auffret
202
203You may distribute this module under the terms of The BSD 3-Clause License.
204See LICENSE file in the source distribution archive.
205
206=head1 AUTHOR
207
208Patrice E<lt>GomoRE<gt> Auffret
209
210=cut
Note: See TracBrowser for help on using the repository browser.