home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D12
/
PERL1.TGZ
/
perl1.tar
/
usr
/
lib
/
perl5
/
ftp.pl
< prev
next >
Wrap
Perl Script
|
1996-06-28
|
24KB
|
1,077 lines
#-*-perl-*-
# This is a wrapper to the chat2.pl routines that make life easier
# to do ftp type work.
# Mostly by Lee McLoughlin <lmjm@doc.ic.ac.uk>
# based on original version by Alan R. Martello <al@ee.pitt.edu>
# And by A.Macpherson@bnr.co.uk for multi-homed hosts
#
# $Header: /a/swan/home/swan/staff/csg/lmjm/src/perl/mirror/RCS/ftp.pl,v 1.17 1993/04/21 10:06:54 lmjm Exp lmjm $
# $Log: ftp.pl,v $
# Revision 1.17 1993/04/21 10:06:54 lmjm
# Send all status reports to STDERR not to STDOUT (to allow use by ftpcat).
# Allow target file to be '-' meaning STDOUT
# Added ftp'quote
#
# Revision 1.16 1993/01/28 18:59:05 lmjm
# Allow socket arguemtns to come from main.
# Minor cleanups - removed old comments.
#
# Revision 1.15 1992/11/25 21:09:30 lmjm
# Added another REST return code.
#
# Revision 1.14 1992/08/12 14:33:42 lmjm
# Fail ftp'write if out of space.
#
# Revision 1.13 1992/03/20 21:01:03 lmjm
# Added in the proxy ftp code from Edwards Reed <err@cinops.xerox.com>
# Added ftp'delete from Aaron Wohl <aw0g+@andrew.cmu.edu>
#
# Revision 1.12 1992/02/06 23:25:56 lmjm
# Moved code around so can use this as a lib for both mirror and ftpmail.
# Time out opens. In case Unix doesn't bother to.
#
# Revision 1.11 1991/11/27 22:05:57 lmjm
# Match the response code number at the start of a line allowing
# for any leading junk.
#
# Revision 1.10 1991/10/23 22:42:20 lmjm
# Added better timeout code.
# Tried to optimise file transfer
# Moved open/close code to not leak file handles.
# Cleaned up the alarm code.
# Added $fatalerror to show wether the ftp link is really dead.
#
# Revision 1.9 1991/10/07 18:30:35 lmjm
# Made the timeout-read code work.
# Added restarting file gets.
# Be more verbose if ever have to call die.
#
# Revision 1.8 1991/09/17 22:53:16 lmjm
# Spot when open_data_socket fails and return a failure rather than dying.
#
# Revision 1.7 1991/09/12 22:40:25 lmjm
# Added Andrew Macpherson's patches for hosts without ip forwarding.
#
# Revision 1.6 1991/09/06 19:53:52 lmjm
# Relaid out the code the way I like it!
# Changed the debuggin to produce more "appropriate" messages
# Fixed bugs in the ordering of put and dir listing.
# Allow for hash printing when getting files (a la ftp).
# Added the new commands from Al.
# Don't print passwords in debugging.
#
# Revision 1.5 1991/08/29 16:23:49 lmjm
# Timeout reads from the remote ftp server.
# No longer call die expect on fatal errors. Just return fail codes.
# Changed returns so higher up routines can tell whats happening.
# Get expect/accept in correct order for dir listing.
# When ftp_show is set then print hashes every 1k transfered (like ftp).
# Allow for stripping returns out of incoming data.
# Save last error in a global string.
#
# Revision 1.4 1991/08/14 21:04:58 lmjm
# ftp'get now copes with ungetable files.
# ftp'expect code changed such that the string_to_print is
# ignored and the string sent back from the remote system is printed
# instead.
# Implemented patches from al. Removed spuiours tracing statements.
#
# Revision 1.3 1991/08/09 21:32:18 lmjm
# Allow for another ok code on cwd's
# Rejigger the log levels
# Send \r\n for some odd ftp daemons
#
# Revision 1.2 1991/08/09 18:07:37 lmjm
# Don't print messages unless ftp_show says to.
#
# Revision 1.1 1991/08/08 20:31:00 lmjm
# Initial revision
#
require 'chat2.pl';
eval "require 'socket.ph'" || eval "require 'sys/socket.ph'" || die "socket.ph missing: $!\n";
package ftp;
if( defined( &main'PF_INET ) ){
$pf_inet = &main'PF_INET;
$sock_stream = &main'SOCK_STREAM;
local($name, $aliases, $proto) = getprotobyname( 'tcp' );
$tcp_proto = $proto;
}
else {
# XXX hardwired $PF_INET, $SOCK_STREAM, 'tcp'
# but who the heck would change these anyway? (:-)
$pf_inet = 2;
$sock_stream = 1;
$tcp_proto = 6;
}
# If the remote ftp daemon doesn't respond within this time presume its dead
# or something.
$timeout = 30;
# Timeout a read if I don't get data back within this many seconds
$timeout_read = 20 * $timeout;
# Timeout an open
$timeout_open = $timeout;
# This is a "global" it contains the last response from the remote ftp server
# for use in error messages
$ftp'response = "";
# Also ftp'NS is the socket containing the data coming in from the remote ls
# command.
# The size of block to be read or written when talking to the remote
# ftp server
$ftp'ftpbufsize = 4096;
# How often to print a hash out, when debugging
$ftp'hashevery = 1024;
# Output a newline after this many hashes to prevent outputing very long lines
$ftp'hashnl = 70;
# If a proxy connection then who am I really talking to?
$real_site = "";
# This is just a tracing aid.
$ftp_show = 0;
sub ftp'debug
{
$ftp_show = @_[0];
# if( $ftp_show ){
# print STDERR "ftp debugging on\n";
# }
}
sub ftp'set_timeout
{
$timeout = @_[0];
$timeout_open = $timeout;
$timeout_read = 20 * $timeout;
if( $ftp_show ){
print STDERR "ftp timeout set to $timeout\n";
}
}
sub ftp'open_alarm
{
die "timeout: open";
}
sub ftp'timed_open
{
local( $site, $ftp_port, $retry_call, $attempts ) = @_;
local( $connect_site, $connect_port );
local( $res );
alarm( $timeout_open );
while( $attempts-- ){
if( $ftp_show ){
print STDERR "proxy connecting via $proxy_gateway [$proxy_ftp_port]\n" if $proxy;
print STDERR "Connecting to $site";
if( $ftp_port != 21 ){
print STDERR " [port $ftp_port]";
}
print STDERR "\n";
}
if( $proxy ) {
if( ! $proxy_gateway ) {
# if not otherwise set
$proxy_gateway = "internet-gateway";
}
if( $debug ) {
print STDERR "using proxy services of $proxy_gateway, ";
print STDERR "at $proxy_ftp_port\n";
}
$connect_site = $proxy_gateway;
$connect_port = $proxy_ftp_port;
$real_site = $site;
}
else {
$connect_site = $site;
$connect_port = $ftp_port;
}
if( ! &chat'open_port( $connect_site, $connect_port ) ){
if( $retry_call ){
print STDERR "Failed to connect\n" if $ftp_show;
next;
}
else {
print STDERR "proxy connection failed " if $proxy;
print STDERR "Cannot open ftp to $connect_site\n" if $ftp_show;
return 0;
}
}
$res = &ftp'expect( $timeout,
120, "service unavailable to $site", 0,
220, "ready for login to $site", 1,
421, "service unavailable to $site, closing connection", 0);
if( ! $res ){
&chat'close();
next;
}
return 1;
}
continue {
print STDERR "Pausing between retries\n";
sleep( $retry_pause );
}
return 0;
}
sub ftp'open
{
local( $site, $ftp_port, $retry_call, $attempts ) = @_;
$SIG{ 'ALRM' } = "ftp\'open_alarm";
local( $ret ) = eval "&timed_open( '$site', $ftp_port, $retry_call, $attempts )";
alarm( 0 );
if( $@ =~ /^timeout/ ){
return -1;
}
return $ret;
}
sub ftp'login
{
local( $remote_user, $remote_password ) = @_;
if( $proxy ){
&ftp'send( "USER $remote_user@$site" );
}
else {
&ftp'send( "USER $remote_user" );
}
local( $val ) =
&ftp'expect($timeout,
230, "$remote_user logged in", 1,
331, "send password for $remote_user", 2,
500, "syntax error", 0,
501, "syntax error", 0,
530, "not logged in", 0,
332, "account for login not supported", 0,
421, "service unavailable, closing connection", 0);
if( $val == 1 ){
return 1;
}
if( $val == 2 ){
# A password is needed
&ftp'send( "PASS $remote_password" );
$val = &ftp'expect( $timeout,
230, "$remote_user logged in", 1,
202, "command not implemented", 0,
332, "account for login not supported", 0,
530, "not logged in", 0,
500, "syntax error", 0,
501, "syntax error", 0,
503, "bad sequence of commands", 0,
421, "service unavailable, closing connection", 0);
if( $val == 1){
# Logged in
return 1;
}
}
# If I got here I failed to login
return 0;
}
sub ftp'close
{
&ftp'quit();
&chat'close();
}
# Change directory
# return 1 if successful
# 0 on a failure
sub ftp'cwd
{
local( $dir ) = @_;
&ftp'send( "CWD $dir" );
return &ftp'expect( $timeout,
200, "working directory = $dir", 1,
250, "working directory = $dir", 1,
500, "syntax error