Client/Smbclient.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Client/Rsync.pm ↑ Summary Client/Splunk.pm »
1 #
2 # $Id$
3 #
4 # client::smbclient Brik
5 #
6 package Metabrik::Client::Smbclient;
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use base qw(Metabrik::Shell::Command Metabrik::System::Package);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub 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 domain => [ qw(domain) ],
21 user => [ qw(username) ],
22 password => [ qw(password) ],
23 host => [ qw(host) ],
24 share => [ qw(path) ],
25 remote_path => [ qw(path) ],
26 },
27 attributes_default => {
28 domain => 'WORKGROUP',
29 user => 'Administrator',
30 host => '127.0.0.1',
31 share => 'c$',
32 remote_path => '\\windows\\temp',
33 },
34 commands => {
35 install => [ ], # Inherited
36 upload => [ qw(file|file_list remote_path|OPTIONAL) ],
37 download => [ qw(file|file_list output_dir|OPTIONAL remote_path|OPTIONAL) ],
38 download_in_background => [ qw(file|file_list output_dir|OPTIONAL remote_path|OPTIONAL) ],
39 },
40 require_modules => {
41 'Metabrik::System::Process' => [ ],
42 },
43 require_binaries => {
44 smbclient => [ ],
45 },
46 need_packages => {
47 ubuntu => [ qw(smbclient) ],
48 debian => [ qw(smbclient) ],
49 kali => [ qw(smbclient) ],
50 },
51 };
52 }
53  
54 #
55 # More good stuff here: https://github.com/jrmdev/smbwrapper
56 #
57  
58 #
59 # run client::smbclient upload $file \\windows\temp\ c$
60 #
61 sub upload {
62 my $self = shift;
63 my ($files, $remote_path, $share) = @_;
64  
65 $remote_path ||= $self->remote_path;
66 $share ||= $self->share;
67 $self->brik_help_run_undef_arg('upload', $files) or return;
68 my $ref = $self->brik_help_run_invalid_arg('upload', $files, 'ARRAY', 'SCALAR')
69 or return;
70  
71 my $domain = $self->domain;
72 my $username = $self->user;
73 my $password = $self->password;
74 my $host = $self->host;
75 $self->brik_help_set_undef_arg('upload', $domain) or return;
76 $self->brik_help_set_undef_arg('upload', $username) or return;
77 $self->brik_help_set_undef_arg('upload', $password) or return;
78 $self->brik_help_set_undef_arg('upload', $host) or return;
79  
80 if ($ref eq 'ARRAY') {
81 my @files = ();
82 for my $file (@$files) {
83 my $this = $self->upload($file, $remote_path, $share) or next;
84 push @files, $this;
85 }
86  
87 return \@files;
88 }
89 else {
90 my ($this_file) = $files =~ m{^(?:.*/)?(.*)$};
91 my $cmd = "smbclient -U $domain/$username%$password //$host/$share -c ".
92 "'put \"$files\" $remote_path\\$this_file'";
93  
94 (my $cmd_hidden = $cmd) =~ s{$password}{XXX};
95 $self->log->verbose("upload: cmd[$cmd_hidden]");
96  
97 my $level = $self->log->level;
98 $self->log->level(0);
99 $self->system($cmd) or return;
100 $self->log->level($level);
101  
102 return "$remote_path\\$this_file";
103 }
104  
105 return $self->log->error("upload: unhandled exception");
106 }
107  
108 #
109 # run client::smbclient download c:\\windows\temp\file.txt /tmp/
110 #
111 sub download {
112 my $self = shift;
113 my ($files, $output_dir, $share) = @_;
114  
115 $output_dir ||= defined($self->shell) && $self->shell->full_pwd || '/tmp';
116 $share ||= $self->share;
117 $self->brik_help_run_undef_arg('download', $files) or return;
118 my $ref = $self->brik_help_run_invalid_arg('download', $files, 'ARRAY', 'SCALAR')
119 or return;
120  
121 my $domain = $self->domain;
122 my $username = $self->user;
123 my $password = $self->password;
124 my $host = $self->host;
125 $self->brik_help_set_undef_arg('download', $domain) or return;
126 $self->brik_help_set_undef_arg('download', $username) or return;
127 $self->brik_help_set_undef_arg('download', $password) or return;
128 $self->brik_help_set_undef_arg('download', $host) or return;
129  
130 if ($ref eq 'ARRAY') {
131 my @files = ();
132 for my $file (@$files) {
133 my $this = $self->download($file, $output_dir, $share) or next;
134 push @files, $this;
135 }
136  
137 return \@files;
138 }
139 else {
140 # Convert path to \\ and remove potentiel initial drive letter
141 $files =~ s{/}{\\}g;
142 my ($drive) = $files =~ m{^([a-zA-Z]):};
143 my ($output_file) = $files =~ m{\\([^\\]+)$};
144 $files =~ s{^[a-zA-Z]:}{};
145 $drive ? ($drive .= '$') : ($drive = $share);
146 $output_file ||= '';
147  
148 my $output = $output_dir ? "$output_dir/$output_file" : $output_file;
149  
150 my $cmd = "smbclient -U $domain/$username%$password //$host/$drive -c ".
151 "'get $files $output'";
152  
153 (my $cmd_hidden = $cmd) =~ s{$password}{XXX};
154 $self->log->verbose("download: cmd[$cmd_hidden]");
155  
156 my $level = $self->log->level;
157 $self->log->level(0);
158 $self->system($cmd) or return;
159 $self->log->level($level);
160  
161 return $output;
162 }
163  
164 return $self->log->error("download: unhandled exception");
165 }
166  
167 sub download_in_background {
168 my $self = shift;
169 my ($files, $output_dir, $share) = @_;
170  
171 $output_dir ||= defined($self->shell) && $self->shell->full_pwd || '/tmp';
172 $share ||= $self->share;
173 $self->brik_help_run_undef_arg('download_in_background', $files) or return;
174 my $ref = $self->brik_help_run_invalid_arg('download_in_background', $files,
175 'ARRAY', 'SCALAR') or return;
176  
177 my $domain = $self->domain;
178 my $username = $self->user;
179 my $password = $self->password;
180 my $host = $self->host;
181 $self->brik_help_set_undef_arg('download_in_background', $domain) or return;
182 $self->brik_help_set_undef_arg('download_in_background', $username) or return;
183 $self->brik_help_set_undef_arg('download_in_background', $password) or return;
184 $self->brik_help_set_undef_arg('download_in_background', $host) or return;
185  
186 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
187 $sp->close_output_on_start(1);
188  
189 # Convert SCALAR to ARRAY
190 if (ref($files) eq '') {
191 $files = [ $files ];
192 }
193  
194 for my $this ($files) {
195 $sp->start(sub {
196 $self->download($this, $output_dir, $share);
197 });
198 }
199  
200 return 1;
201 }
202  
203 1;
204  
205 __END__
« Client/Rsync.pm ↑ Summary Client/Splunk.pm »