/
SFTP.pm
353 lines (259 loc) · 9.83 KB
/
SFTP.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package Bric::Util::Trans::SFTP;
=head1 Name
Bric::Util::Trans::SFTP - SFTP Client interface for distributing resources.
=cut
# Grab the Version Number.
require Bric; our $VERSION = Bric->VERSION;
=head1 Synopsis
use Bric::Util::Trans::SFTP
=head1 Description
The distribution API uses this class to distribute resources to other servers
via SFTP.
=cut
################################################################################
# Dependencies
################################################################################
# Standard Dependencies
use strict;
################################################################################
# Programmatic Dependencies
use Net::SSH2;
use Net::SSH2::File;
use Net::SSH2::SFTP;
use Bric::Util::Fault qw(throw_gen);
use Bric::Util::Trans::FS;
use Bric::Config qw(:dist);
use Bric::Util::ApacheUtil qw(unescape_uri escape_uri);
################################################################################
# Inheritance
################################################################################
use base qw(Bric);
################################################################################
# Function and Closure Prototypes
################################################################################
my ($no_warn);
################################################################################
# Constants
################################################################################
use constant DEBUG => 0;
################################################################################
# Fields
################################################################################
# Public Class Fields
################################################################################
# Private Class Fields
my $fs = Bric::Util::Trans::FS->new;
################################################################################
################################################################################
# Instance Fields
BEGIN { Bric::register_fields() }
################################################################################
# Class Methods
################################################################################
=head1 Interface
=head2 Constructors
NONE.
=head2 Destructors
NONE.
=head2 Public Class Methods
NONE.
=head2 Public Instance Methods
NONE.
=head2 Public Functions
=over
=item my $bool = Bric::Util::Trans::SFTP->put_res($resources, $st)
Puts the files specified by the $resources object on the servers
specified by $st.
B<Throws:>
=over 4
=item *
Unable to login to remote server.
=item *
Unable to create directory on remote server.
=item *
Unable to put resource on remote server.
=back
B<Side Effects:> NONE.
B<Notes:> Uses Net::SFTP internally.
=cut
sub put_res {
my ($pkg, $resources, $st) = @_;
foreach my $s ($st->get_servers) {
# Skip inactive servers.
next unless $s->is_active;
# Instantiate a Net::SSH2 object and login.
my ($hn, $port) = split /:/, $s->get_host_name;
my $user = $s->get_login;
my $password = $s->get_password;
my $ssh2 = Net::SSH2->new();
my $connect = eval {
$ssh2->connect($hn, $port);
$ssh2->method('CRYPT_CS', SFTP_MOVER_CIPHER ) if SFTP_MOVER_CIPHER;
$ssh2->method('HOSTKEY', SFTP_KEY_TYPE ) if SFTP_KEY_TYPE;
$ssh2->auth( username => $user, password => $password,
publickey => SFTP_PUBLIC_KEY_FILE,
privatekey => SFTP_PRIVATE_KEY_FILE);
};
throw_gen error => "Unable to login to remote server '$hn'.",
payload => $@
if $@ || !$ssh2->auth_ok;
# Get the document root.
my $doc_root = $s->get_doc_root;
# Create a Net::SSH2::SFTP object to use later
my $sftp = $ssh2->sftp;
# Now, put each file on the remote server.
my %dirs;
foreach my $res (@$resources) {
# Get the source and destination paths for the resource.
my $src = $res->get_tmp_path || $res->get_path;
# Create the destination directory if it doesn't exist and we
# haven't created it already.
my $dest_dir = $fs->dir_name(unescape_uri $res->get_uri);
my ($status, $dirhandle);
unless ($dirs{$dest_dir}) {
$dirhandle = eval {
local $SIG{__WARN__} = $no_warn;
$sftp->opendir($fs->cat_dir($doc_root, substr $dest_dir, 1));
};
unless (defined $dirhandle) {
# The directory doesn't exist.
# Get the list of all of the directories.
my $subdir = $doc_root;
foreach my $dir ($fs->split_dir($dest_dir)) {
# Create each one if it doesn't exist.
$subdir = $fs->cat_dir($subdir, $dir);
# Mark that we've created it, so we don't try to do it
# again.
$dirs{$subdir} = 1;
$dirhandle = eval{
local $SIG{__WARN__} = $no_warn;
$sftp->opendir($subdir);
};
unless (defined $dirhandle) {
$status = eval {
local $SIG{__WARN__} = $no_warn;
$sftp->mkdir($subdir);
};
unless (defined $status) {
my $msg = "Unable to create directory '$subdir'"
. " on remote server '$hn'";
throw_gen(error => $msg);
}
}
}
}
}
# Now, put the file on the server.
my $dest_file = $fs->cat_file($doc_root, unescape_uri substr $res->get_uri, 1);
# Strip the filename off end of dest and escape it
my $orig_base = $fs->base_name($dest_file);
my $escaped_base;
($escaped_base = $orig_base) =~ s/(.)/\\$1/g;
# Strip off directory destination and
# re-name file with escapes included
my $base_dir = $fs->dir_name($dest_file);
my $dest_file_esc = $fs->cat_dir($base_dir, $escaped_base);
# Create temporary destination and put file on server
my $tmp_dest = $dest_file . '.tmp';
my $tmp_dest_esc = $dest_file_esc . '.tmp';
$status = eval{
local $SIG{__WARN__} = $no_warn;
$sftp->unlink($tmp_dest) if FTP_UNLINK_BEFORE_MOVE;
my $f = $ssh2->scp_put($src, $tmp_dest_esc);
$sftp->unlink($dest_file) if FTP_UNLINK_BEFORE_MOVE;
$sftp->rename($tmp_dest_esc, $dest_file);
};
unless (defined $status) {
my $msg = "Unable to put file '$dest_file' on remote host"
. " '$hn', status '" . $sftp->error. "'";
throw_gen(error => $msg);
}
}
# Disconnect and free memory
$ssh2->disconnect;
undef $sftp;
}
return 1;
}
################################################################################
=item my $bool = Bric::Util::Trans::SFTP->del_res($resources, $st)
Deletes the files specified by the $resources object from the servers
specified by $st.
B<Throws:>
=over 4
=item *
Unable to login to remote server.
=item *
Unable to delete resource from remote server.
=back
B<Side Effects:> NONE.
B<Notes:> See put_res(), above.
=cut
sub del_res {
my ($pkg, $resources, $st) = @_;
foreach my $s ($st->get_servers) {
# Skip inactive servers.
next unless $s->is_active;
# Instantiate a Net::SSH2 object and login.
my ($hn, $port) = split /:/, $s->get_host_name;
my $user = $s->get_login;
my $password = $s->get_password;
my $ssh2 = Net::SSH2->new();
my $connect = eval {
$ssh2->connect($hn, $port);
$ssh2->method('CRYPT_CS', SFTP_MOVER_CIPHER ) if SFTP_MOVER_CIPHER;
$ssh2->method('HOSTKEY', SFTP_KEY_TYPE ) if SFTP_KEY_TYPE;
$ssh2->auth( username => $user, password => $password,
publickey => SFTP_PUBLIC_KEY_FILE,
privatekey => SFTP_PRIVATE_KEY_FILE);
};
throw_gen error => "Unable to login to remote server '$hn'.",
payload => $@
if $@ || !$ssh2->auth_ok;
# Create a Net::SSH2::SFTP object to use later
my $sftp = $ssh2->sftp;
# Get the document root.
my $doc_root = $s->get_doc_root;
foreach my $res (@$resources) {
# Get the name of the file to be deleted.
my $file = $fs->cat_file($doc_root, unescape_uri substr $res->get_uri, 1);
# Delete the file
$sftp->unlink($file);
}
# Disconnect and free memory
$ssh2->disconnect;
undef $sftp;
undef $ssh2;
}
return 1;
}
=back
=head1 Private
=head2 Private Class Methods
NONE.
=head2 Private Instance Methods
NONE.
=head2 Private Functions
=over 4
=item local $SIG{__WARN__} = $no_warn;
Anonymous subroutine to prevent warning messages.
B<Throws:> NONE.
B<Notes:> NONE.
=cut
$no_warn = sub { };
=back
=head1 Notes
NONE.
=head1 Author
Scott Lanning E<lt>slanning@theworld.comE<gt>
Sarah Mercier E<lt>mercie_s@denison.eduE<gt>
Charlie Reitsma E<lt>reitsma@denison.eduE<gt>
Matt Rolf E<lt>rolfm@denison.eduE<gt>
=head1 See Also
L<Bric|Bric>,
L<Bric::Dist::Action|Bric::Dist::Action>,
L<Bric::Dist::Action::Mover|Bric::Dist::Action::Mover>,
L<Bric::Util::Trans::FS|Bric::Util::Trans::FS>,
L<Net::SSH2|Net::SSH2>,
L<Net::SSH::Perl|Net::SSH::Perl>
=cut