home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / ftp.pl < prev    next >
Encoding:
Perl Script  |  2003-09-16  |  24.7 KB  |  1,090 lines

  1. #-*-perl-*-
  2. #
  3. # This library is no longer being maintained, and is included for backward
  4. # compatibility with Perl 4 programs which may require it.
  5. #
  6. # In particular, this should not be used as an example of modern Perl
  7. # programming techniques.
  8. #
  9. # Suggested alternative: Net::FTP
  10. #
  11. # This is a wrapper to the chat2.pl routines that make life easier
  12. # to do ftp type work.
  13. # Mostly by Lee McLoughlin <lmjm@doc.ic.ac.uk>
  14. # based on original version by Alan R. Martello <al@ee.pitt.edu>
  15. # And by A.Macpherson@bnr.co.uk for multi-homed hosts
  16. #
  17. # $Header: /srv/cvs/tsw/WEB/Apache2/perl/lib/ftp.pl,v 1.2 2003/09/16 13:01:42 joker Exp $
  18. # $Log: ftp.pl,v $
  19. # Revision 1.2  2003/09/16 13:01:42  joker
  20. # Neues Zeilenende (Migr. von UNIX nach PC)
  21. #
  22. # Revision 1.17  1993/04/21  10:06:54  lmjm
  23. # Send all status reports to STDERR not to STDOUT (to allow use by ftpcat).
  24. # Allow target file to be '-' meaning STDOUT
  25. # Added ftp'quote
  26. #
  27. # Revision 1.16  1993/01/28  18:59:05  lmjm
  28. # Allow socket arguemtns to come from main.
  29. # Minor cleanups - removed old comments.
  30. #
  31. # Revision 1.15  1992/11/25  21:09:30  lmjm
  32. # Added another REST return code.
  33. #
  34. # Revision 1.14  1992/08/12  14:33:42  lmjm
  35. # Fail ftp'write if out of space.
  36. #
  37. # Revision 1.13  1992/03/20  21:01:03  lmjm
  38. # Added in the proxy ftp code from Edwards Reed <err@cinops.xerox.com>
  39. # Added  ftp'delete from Aaron Wohl <aw0g+@andrew.cmu.edu>
  40. #
  41. # Revision 1.12  1992/02/06  23:25:56  lmjm
  42. # Moved code around so can use this as a lib for both mirror and ftpmail.
  43. # Time out opens.  In case Unix doesn't bother to.
  44. #
  45. # Revision 1.11  1991/11/27  22:05:57  lmjm
  46. # Match the response code number at the start of a line allowing
  47. # for any leading junk.
  48. #
  49. # Revision 1.10  1991/10/23  22:42:20  lmjm
  50. # Added better timeout code.
  51. # Tried to optimise file transfer
  52. # Moved open/close code to not leak file handles.
  53. # Cleaned up the alarm code.
  54. # Added $fatalerror to show wether the ftp link is really dead.
  55. #
  56. # Revision 1.9  1991/10/07  18:30:35  lmjm
  57. # Made the timeout-read code work.
  58. # Added restarting file gets.
  59. # Be more verbose if ever have to call die.
  60. #
  61. # Revision 1.8  1991/09/17  22:53:16  lmjm
  62. # Spot when open_data_socket fails and return a failure rather than dying.
  63. #
  64. # Revision 1.7  1991/09/12  22:40:25  lmjm
  65. # Added Andrew Macpherson's patches for hosts without ip forwarding.
  66. #
  67. # Revision 1.6  1991/09/06  19:53:52  lmjm
  68. # Relaid out the code the way I like it!
  69. # Changed the debuggin to produce more "appropriate" messages
  70. # Fixed bugs in the ordering of put and dir listing.
  71. # Allow for hash printing when getting files (a la ftp).
  72. # Added the new commands from Al.
  73. # Don't print passwords in debugging.
  74. #
  75. # Revision 1.5  1991/08/29  16:23:49  lmjm
  76. # Timeout reads from the remote ftp server.
  77. # No longer call die expect on fatal errors.  Just return fail codes.
  78. # Changed returns so higher up routines can tell whats happening.
  79. # Get expect/accept in correct order for dir listing.
  80. # When ftp_show is set then print hashes every 1k transferred (like ftp).
  81. # Allow for stripping returns out of incoming data.
  82. # Save last error in a global string.
  83. #
  84. # Revision 1.4  1991/08/14  21:04:58  lmjm
  85. # ftp'get now copes with ungetable files.
  86. # ftp'expect code changed such that the string_to_print is
  87. # ignored and the string sent back from the remote system is printed
  88. # instead.
  89. # Implemented patches from al.  Removed spuiours tracing statements.
  90. #
  91. # Revision 1.3  1991/08/09  21:32:18  lmjm
  92. # Allow for another ok code on cwd's
  93. # Rejigger the log levels
  94. # Send \r\n for some odd ftp daemons
  95. #
  96. # Revision 1.2  1991/08/09  18:07:37  lmjm
  97. # Don't print messages unless ftp_show says to.
  98. #
  99. # Revision 1.1  1991/08/08  20:31:00  lmjm
  100. # Initial revision
  101. #
  102.  
  103. require 'chat2.pl';    # into main
  104. eval "require 'socket.ph'" || eval "require 'sys/socket.ph'"
  105.     || die "socket.ph missing: $!\n";
  106.  
  107.  
  108. package ftp;
  109.  
  110. if( defined( &main'PF_INET ) ){
  111.     $pf_inet = &main'PF_INET;
  112.     $sock_stream = &main'SOCK_STREAM;
  113.     local($name, $aliases, $proto) = getprotobyname( 'tcp' );
  114.     $tcp_proto = $proto;
  115. }
  116. else {
  117.     # XXX hardwired $PF_INET, $SOCK_STREAM, 'tcp'
  118.     # but who the heck would change these anyway? (:-)
  119.     $pf_inet = 2;
  120.     $sock_stream = 1;
  121.     $tcp_proto = 6;
  122. }
  123.  
  124. # If the remote ftp daemon doesn't respond within this time presume its dead
  125. # or something.
  126. $timeout = 30;
  127.  
  128. # Timeout a read if I don't get data back within this many seconds
  129. $timeout_read = 20 * $timeout;
  130.  
  131. # Timeout an open
  132. $timeout_open = $timeout;
  133.  
  134. # This is a "global" it contains the last response from the remote ftp server
  135. # for use in error messages
  136. $ftp'response = "";
  137. # Also ftp'NS is the socket containing the data coming in from the remote ls
  138. # command.
  139.  
  140. # The size of block to be read or written when talking to the remote
  141. # ftp server
  142. $ftp'ftpbufsize = 4096;
  143.  
  144. # How often to print a hash out, when debugging
  145. $ftp'hashevery = 1024;
  146. # Output a newline after this many hashes to prevent outputing very long lines
  147. $ftp'hashnl = 70;
  148.  
  149. # If a proxy connection then who am I really talking to?
  150. $real_site = "";
  151.  
  152. # This is just a tracing aid.
  153. $ftp_show = 0;
  154. sub ftp'debug
  155. {
  156.     $ftp_show = $_[0];
  157. #    if( $ftp_show ){
  158. #        print STDERR "ftp debugging on\n";
  159. #    }
  160. }
  161.  
  162. sub ftp'set_timeout
  163. {
  164.     $timeout = $_[0];
  165.     $timeout_open = $timeout;
  166.     $timeout_read = 20 * $timeout;
  167.     if( $ftp_show ){
  168.         print STDERR "ftp timeout set to $timeout\n";
  169.     }
  170. }
  171.  
  172.  
  173. sub ftp'open_alarm
  174. {
  175.     die "timeout: open";
  176. }
  177.  
  178. sub ftp'timed_open
  179. {
  180.     local( $site, $ftp_port, $retry_call, $attempts ) = @_;
  181.     local( $connect_site, $connect_port );
  182.     local( $res );
  183.  
  184.     alarm( $timeout_open );
  185.  
  186.     while( $attempts-- ){
  187.         if( $ftp_show ){
  188.             print STDERR "proxy connecting via $proxy_gateway [$proxy_ftp_port]\n" if $proxy;
  189.             print STDERR "Connecting to $site";
  190.             if( $ftp_port != 21 ){
  191.                 print STDERR " [port $ftp_port]";
  192.             }
  193.             print STDERR "\n";
  194.         }
  195.         
  196.         if( $proxy ) {
  197.             if( ! $proxy_gateway ) {
  198.                 # if not otherwise set
  199.                 $proxy_gateway = "internet-gateway";
  200.             }
  201.             if( $debug ) {
  202.                 print STDERR "using proxy services of $proxy_gateway, ";
  203.                 print STDERR "at $proxy_ftp_port\n";
  204.             }
  205.             $connect_site = $proxy_gateway;
  206.             $connect_port = $proxy_ftp_port;
  207.             $real_site = $site;
  208.         }
  209.         else {
  210.             $connect_site = $site;
  211.             $connect_port = $ftp_port;
  212.         }
  213.         if( ! &chat'open_port( $connect_site, $connect_port ) ){
  214.             if( $retry_call ){
  215.                 print STDERR "Failed to connect\n" if $ftp_show;
  216.                 next;
  217.             }
  218.             else {
  219.                 print STDERR "proxy connection failed " if $proxy;
  220.                 print STDERR "Cannot open ftp to $connect_site\n" if $ftp_show;
  221.                 return 0;
  222.             }
  223.         }
  224.         $res = &ftp'expect( $timeout,
  225.                     120, "service unavailable to $site", 0, 
  226.                                 220, "ready for login to $site", 1,
  227.                     421, "service unavailable to $site, closing connection", 0);
  228.         if( ! $res ){
  229.             &chat'close();
  230.             next;
  231.         }
  232.         return 1;
  233.     }
  234.     continue {
  235.         print STDERR "Pausing between retries\n";
  236.         sleep( $retry_pause );
  237.     }
  238.     return 0;
  239. }
  240.  
  241. sub ftp'open
  242. {
  243.     local( $site, $ftp_port, $retry_call, $attempts ) = @_;
  244.  
  245.     $SIG{ 'ALRM' } = "ftp\'open_alarm";
  246.  
  247.     local( $ret ) = eval "&timed_open( '$site', $ftp_port, $retry_call, $attempts )";
  248.     alarm( 0 );
  249.  
  250.     if( $@ =~ /^timeout/ ){
  251.         return -1;
  252.     }
  253.     return $ret;
  254. }
  255.  
  256. sub ftp'login
  257. {
  258.     local( $remote_user, $remote_password ) = @_;
  259.  
  260.     if( $proxy ){
  261.         &ftp'send( "USER $remote_user\@$site" );
  262.     }
  263.     else {
  264.         &ftp'send( "USER $remote_user" );
  265.     }
  266.         local( $val ) =
  267.                &ftp'expect($timeout,
  268.                230, "$remote_user logged in", 1,
  269.            331, "send password for $remote_user", 2,
  270.  
  271.            500, "syntax error", 0,
  272.            501, "syntax error", 0,
  273.            530, "not logged in", 0,
  274.            332, "account for login not supported", 0,
  275.  
  276.            421, "service unavailable, closing connection", 0);
  277.     if( $val == 1 ){
  278.         return 1;
  279.     }
  280.     if( $val == 2 ){
  281.         # A password is needed
  282.         &ftp'send( "PASS $remote_password" );
  283.  
  284.         $val = &ftp'expect( $timeout,
  285.            230, "$remote_user logged in", 1,
  286.  
  287.            202, "command not implemented", 0,
  288.            332, "account for login not supported", 0,
  289.  
  290.            530, "not logged in", 0,
  291.            500, "syntax error", 0,
  292.            501, "syntax error", 0,
  293.            503, "bad sequence of commands", 0, 
  294.  
  295.            421, "service unavailable, closing connection", 0);
  296.         if( $val == 1){
  297.             # Logged in
  298.             return 1;
  299.         }
  300.     }
  301.     # If I got here I failed to login
  302.     return 0;
  303. }
  304.  
  305. sub ftp'close
  306. {
  307.     &ftp'quit();
  308.     &chat'close();
  309. }
  310.  
  311. # Change directory
  312. # return 1 if successful
  313. # 0 on a failure
  314. sub ftp'cwd
  315. {
  316.     local( $dir ) = @_;
  317.  
  318.     &ftp'send( "CWD $dir" );
  319.  
  320.     return &ftp'expect( $timeout,
  321.         200, "working directory = $dir", 1,
  322.         250, "working directory = $dir", 1,
  323.  
  324.         500, "syntax error", 0,
  325.         501, "syntax error", 0,
  326.                 502, "command not implemented", 0,
  327.         530, "not logged in", 0,
  328.                 550, "cannot change directory", 0,
  329.         421, "service unavailable, closing connection", 0 );
  330. }
  331.  
  332. # Get a full directory listing:
  333. # &ftp'dir( remote LIST options )
  334. # Start a list goin with the given options.
  335. # Presuming that the remote deamon uses the ls command to generate the
  336. # data to send back then you can send it some extra options (eg: -lRa)
  337. # return 1 if sucessful and 0 on a failure
  338. sub ftp'dir_open
  339. {
  340.     local( $options ) = @_;
  341.     local( $ret );
  342.     
  343.     if( ! &ftp'open_data_socket() ){
  344.         return 0;
  345.     }
  346.     
  347.     if( $options ){
  348.         &ftp'send( "LIST $options" );
  349.     }
  350.     else {
  351.         &ftp'send( "LIST" );
  352.     }
  353.     
  354.     $ret = &ftp'expect( $timeout,
  355.         150, "reading directory", 1,
  356.     
  357.         125, "data connection already open?", 0,
  358.     
  359.         450, "file unavailable", 0,
  360.         500, "syntax error", 0,
  361.         501, "syntax error", 0,
  362.         502, "command not implemented", 0,
  363.         530, "not logged in", 0,
  364.     
  365.            421, "service unavailable, closing connection", 0 );
  366.     if( ! $ret ){
  367.         &ftp'close_data_socket;
  368.         return 0;
  369.     }
  370.     
  371.     # 
  372.     # the data should be coming at us now
  373.     #
  374.     
  375.     # now accept
  376.     accept(NS,S) || die "accept failed $!";
  377.     
  378.     return 1;
  379. }
  380.  
  381.  
  382. # Close down reading the result of a remote ls command
  383. # return 1 if successful and 0 on failure
  384. sub ftp'dir_close
  385. {
  386.     local( $ret );
  387.  
  388.     # read the close
  389.     #
  390.     $ret = &ftp'expect($timeout,
  391.             226, "", 1,     # transfer complete, closing connection
  392.             250, "", 1,     # action completed
  393.  
  394.             425, "can't open data connection", 0,
  395.             426, "connection closed, transfer aborted", 0,
  396.             451, "action aborted, local error", 0,
  397.             421, "service unavailable, closing connection", 0);
  398.  
  399.     # shut down our end of the socket
  400.     &ftp'close_data_socket;
  401.  
  402.     if( ! $ret ){
  403.         return 0;
  404.     }
  405.  
  406.     return 1;
  407. }
  408.  
  409. # Quit from the remote ftp server
  410. # return 1 if successful and 0 on failure
  411. sub ftp'quit
  412. {
  413.     $site_command_check = 0;
  414.     @site_command_list = ();
  415.  
  416.     &ftp'send("QUIT");
  417.  
  418.     return &ftp'expect($timeout, 
  419.         221, "Goodbye", 1,     # transfer complete, closing connection
  420.     
  421.         500, "error quitting??", 0);
  422. }
  423.  
  424. sub ftp'read_alarm
  425. {
  426.     die "timeout: read";
  427. }
  428.  
  429. sub ftp'timed_read
  430. {
  431.     alarm( $timeout_read );
  432.     return sysread( NS, $buf, $ftpbufsize );
  433. }
  434.  
  435. sub ftp'read
  436. {
  437.     $SIG{ 'ALRM' } = "ftp\'read_alarm";
  438.  
  439.     local( $ret ) = eval '&timed_read()';
  440.     alarm( 0 );
  441.  
  442.     if( $@ =~ /^timeout/ ){
  443.         return -1;
  444.     }
  445.     return $ret;
  446. }
  447.  
  448. # Get a remote file back into a local file.
  449. # If no loc_fname passed then uses rem_fname.
  450. # returns 1 on success and 0 on failure
  451. sub ftp'get
  452. {
  453.     local($rem_fname, $loc_fname, $restart ) = @_;
  454.     
  455.     if ($loc_fname eq "") {
  456.         $loc_fname = $rem_fname;
  457.     }
  458.     
  459.     if( ! &ftp'open_data_socket() ){
  460.         print STDERR "Cannot open data socket\n";
  461.         return 0;
  462.     }
  463.  
  464.     if( $loc_fname ne '-' ){
  465.         # Find the size of the target file
  466.         local( $restart_at ) = &ftp'filesize( $loc_fname );
  467.         if( $restart && $restart_at > 0 && &ftp'restart( $restart_at ) ){
  468.             $restart = 1;
  469.             # Make sure the file can be updated
  470.             chmod( 0644, $loc_fname );
  471.         }
  472.         else {
  473.             $restart = 0;
  474.             unlink( $loc_fname );
  475.         }
  476.     }
  477.  
  478.     &ftp'send( "RETR $rem_fname" );
  479.     
  480.     local( $ret ) =
  481.         &ftp'expect($timeout, 
  482.                    150, "receiving $rem_fname", 1,
  483.  
  484.                    125, "data connection already open?", 0,
  485.  
  486.                    450, "file unavailable", 2,
  487.                    550, "file unavailable", 2,
  488.  
  489.            500, "syntax error", 0,
  490.            501, "syntax error", 0,
  491.            530, "not logged in", 0,
  492.  
  493.            421, "service unavailable, closing connection", 0);
  494.     if( $ret != 1 ){
  495.         print STDERR "Failure on RETR command\n";
  496.  
  497.         # shut down our end of the socket
  498.         &ftp'close_data_socket;
  499.  
  500.         return 0;
  501.     }
  502.  
  503.     # 
  504.     # the data should be coming at us now
  505.     #
  506.  
  507.     # now accept
  508.     accept(NS,S) || die "accept failed: $!";
  509.  
  510.     #
  511.     #  open the local fname
  512.     #  concatenate on the end if restarting, else just overwrite
  513.     if( !open(FH, ($restart ? '>>' : '>') . $loc_fname) ){
  514.         print STDERR "Cannot create local file $loc_fname\n";
  515.  
  516.         # shut down our end of the socket
  517.         &ftp'close_data_socket;
  518.  
  519.         return 0;
  520.     }
  521.  
  522. #    while (<NS>) {
  523. #        print FH ;
  524. #    }
  525.  
  526.     local( $start_time ) = time;
  527.     local( $bytes, $lasthash, $hashes ) = (0, 0, 0);
  528.     while( ($len = &ftp'read()) > 0 ){
  529.         $bytes += $len;
  530.         if( $strip_cr ){
  531.             $ftp'buf =~ s/\r//g;
  532.         }
  533.         if( $ftp_show ){
  534.             while( $bytes > ($lasthash + $ftp'hashevery) ){
  535.                 print STDERR '#';
  536.                 $lasthash += $ftp'hashevery;
  537.                 $hashes++;
  538.                 if( ($hashes % $ftp'hashnl) == 0 ){
  539.                     print STDERR "\n";
  540.                 }
  541.             }
  542.         }
  543.         if( ! print FH $ftp'buf ){
  544.             print STDERR "\nfailed to write data";
  545.             return 0;
  546.         }
  547.     }
  548.     close( FH );
  549.  
  550.     # shut down our end of the socket
  551.     &ftp'close_data_socket;
  552.  
  553.     if( $len < 0 ){
  554.         print STDERR "\ntimed out reading data!\n";
  555.  
  556.         return 0;
  557.     }
  558.         
  559.     if( $ftp_show ){
  560.         if( $hashes && ($hashes % $ftp'hashnl) != 0 ){
  561.             print STDERR "\n";
  562.         }
  563.         local( $secs ) = (time - $start_time);
  564.         if( $secs <= 0 ){
  565.             $secs = 1; # To avoid a divide by zero;
  566.         }
  567.  
  568.         local( $rate ) = int( $bytes / $secs );
  569.         print STDERR "Got $bytes bytes ($rate bytes/sec)\n";
  570.     }
  571.  
  572.     #
  573.     # read the close
  574.     #
  575.  
  576.     $ret = &ftp'expect($timeout, 
  577.         226, "Got file", 1,     # transfer complete, closing connection
  578.             250, "Got file", 1,     # action completed
  579.     
  580.             110, "restart not supported", 0,
  581.             425, "can't open data connection", 0,
  582.             426, "connection closed, transfer aborted", 0,
  583.             451, "action aborted, local error", 0,
  584.         421, "service unavailable, closing connection", 0);
  585.  
  586.     return $ret;
  587. }
  588.  
  589. sub ftp'delete
  590. {
  591.     local( $rem_fname, $val ) = @_;
  592.  
  593.     &ftp'send("DELE $rem_fname" );
  594.     $val = &ftp'expect( $timeout, 
  595.                250,"Deleted $rem_fname", 1,
  596.                550,"Permission denied",0
  597.                );
  598.     return $val == 1;
  599. }
  600.  
  601. sub ftp'deldir
  602. {
  603.     local( $fname ) = @_;
  604.  
  605.     # not yet implemented
  606.     # RMD
  607. }
  608.  
  609. # UPDATE ME!!!!!!
  610. # Add in the hash printing and newline conversion
  611. sub ftp'put
  612. {
  613.     local( $loc_fname, $rem_fname ) = @_;
  614.     local( $strip_cr );
  615.     
  616.     if ($loc_fname eq "") {
  617.         $loc_fname = $rem_fname;
  618.     }
  619.     
  620.     if( ! &ftp'open_data_socket() ){
  621.         return 0;
  622.     }
  623.     
  624.     &ftp'send("STOR $rem_fname");
  625.     
  626.     # 
  627.     # the data should be coming at us now
  628.     #
  629.     
  630.     local( $ret ) =
  631.     &ftp'expect($timeout, 
  632.         150, "sending $loc_fname", 1,
  633.  
  634.         125, "data connection already open?", 0,
  635.         450, "file unavailable", 0,
  636.  
  637.         532, "need account for storing files", 0,
  638.         452, "insufficient storage on system", 0,
  639.         553, "file name not allowed", 0,
  640.  
  641.         500, "syntax error", 0,
  642.         501, "syntax error", 0,
  643.         530, "not logged in", 0,
  644.  
  645.         421, "service unavailable, closing connection", 0);
  646.  
  647.     if( $ret != 1 ){
  648.         # shut down our end of the socket
  649.         &ftp'close_data_socket;
  650.  
  651.         return 0;
  652.     }
  653.  
  654.  
  655.     # 
  656.     # the data should be coming at us now
  657.     #
  658.     
  659.     # now accept
  660.     accept(NS,S) || die "accept failed: $!";
  661.     
  662.     #
  663.     #  open the local fname
  664.     #
  665.     if( !open(FH, "<$loc_fname") ){
  666.         print STDERR "Cannot open local file $loc_fname\n";
  667.  
  668.         # shut down our end of the socket
  669.         &ftp'close_data_socket;
  670.  
  671.         return 0;
  672.     }
  673.     
  674.     while (<FH>) {
  675.         print NS ;
  676.     }
  677.     close(FH);
  678.     
  679.     # shut down our end of the socket to signal EOF
  680.     &ftp'close_data_socket;
  681.     
  682.     #
  683.     # read the close
  684.     #
  685.     
  686.     $ret = &ftp'expect($timeout, 
  687.         226, "file put", 1,     # transfer complete, closing connection
  688.         250, "file put", 1,     # action completed
  689.     
  690.         110, "restart not supported", 0,
  691.         425, "can't open data connection", 0,
  692.         426, "connection closed, transfer aborted", 0,
  693.         451, "action aborted, local error", 0,
  694.         551, "page type unknown", 0,
  695.         552, "storage allocation exceeded", 0,
  696.     
  697.         421, "service unavailable, closing connection", 0);
  698.     if( ! $ret ){
  699.         print STDERR "error putting $loc_fname\n";
  700.     }
  701.     return $ret;
  702. }
  703.  
  704. sub ftp'restart
  705. {
  706.     local( $restart_point, $ret ) = @_;
  707.  
  708.     &ftp'send("REST $restart_point");
  709.  
  710.     # 
  711.     # see what they say
  712.  
  713.     $ret = &ftp'expect($timeout, 
  714.                350, "restarting at $restart_point", 1,
  715.                
  716.                500, "syntax error", 0,
  717.                501, "syntax error", 0,
  718.                502, "REST not implemented", 2,
  719.                530, "not logged in", 0,
  720.                554, "REST not implemented", 2,
  721.                
  722.                421, "service unavailable, closing connection", 0);
  723.     return $ret;
  724. }
  725.  
  726. # Set the file transfer type
  727. sub ftp'type
  728. {
  729.     local( $type ) = @_;
  730.  
  731.     &ftp'send("TYPE $type");
  732.  
  733.     # 
  734.     # see what they say
  735.  
  736.     $ret = &ftp'expect($timeout, 
  737.                200, "file type set to $type", 1,
  738.                
  739.                500, "syntax error", 0,
  740.                501, "syntax error", 0,
  741.                504, "Invalid form or byte size for type $type", 0,
  742.                
  743.                421, "service unavailable, closing connection", 0);
  744.     return $ret;
  745. }
  746.  
  747. $site_command_check = 0;
  748. @site_command_list = ();
  749.  
  750. # routine to query the remote server for 'SITE' commands supported
  751. sub ftp'site_commands
  752. {
  753.     local( $ret );
  754.     
  755.     # if we havent sent a 'HELP SITE', send it now
  756.     if( !$site_command_check ){
  757.     
  758.         $site_command_check = 1;
  759.     
  760.         &ftp'send( "HELP SITE" );
  761.     
  762.         # assume the line in the HELP SITE response with the 'HELP'
  763.         # command is the one for us
  764.         $ret = &ftp'expect( $timeout,
  765.             ".*HELP.*", "", "\$1",
  766.             214, "", "0",
  767.             202, "", "0" );
  768.     
  769.         if( $ret eq "0" ){
  770.             print STDERR "No response from HELP SITE\n" if( $ftp_show );
  771.         }
  772.     
  773.         @site_command_list = split(/\s+/, $ret);
  774.     }
  775.     
  776.     return @site_command_list;
  777. }
  778.  
  779. # return the pwd, or null if we can't get the pwd
  780. sub ftp'pwd
  781. {
  782.     local( $ret, $cwd );
  783.  
  784.     &ftp'send( "PWD" );
  785.  
  786.     # 
  787.     # see what they say
  788.  
  789.     $ret = &ftp'expect( $timeout, 
  790.                257, "working dir is", 1,
  791.                500, "syntax error", 0,
  792.                501, "syntax error", 0,
  793.                502, "PWD not implemented", 0,
  794.                        550, "file unavailable", 0,
  795.  
  796.                421, "service unavailable, closing connection", 0 );
  797.     if( $ret ){
  798.         if( $ftp'response =~ /^257\s"(.*)"\s.*$/ ){
  799.             $cwd = $1;
  800.         }
  801.     }
  802.     return $cwd;
  803. }
  804.  
  805. # return 1 for success, 0 for failure
  806. sub ftp'mkdir
  807. {
  808.     local( $path ) = @_;
  809.     local( $ret );
  810.  
  811.     &ftp'send( "MKD $path" );
  812.  
  813.     # 
  814.     # see what they say
  815.  
  816.     $ret = &ftp'expect( $timeout, 
  817.                257, "made directory $path", 1,
  818.                
  819.                500, "syntax error", 0,
  820.                501, "syntax error", 0,
  821.                502, "MKD not implemented", 0,
  822.                530, "not logged in", 0,
  823.                        550, "file unavailable", 0,
  824.  
  825.                421, "service unavailable, closing connection", 0 );
  826.     return $ret;
  827. }
  828.  
  829. # return 1 for success, 0 for failure
  830. sub ftp'chmod
  831. {
  832.     local( $path, $mode ) = @_;
  833.     local( $ret );
  834.  
  835.     &ftp'send( sprintf( "SITE CHMOD %o $path", $mode ) );
  836.  
  837.     # 
  838.     # see what they say
  839.  
  840.     $ret = &ftp'expect( $timeout, 
  841.                200, "chmod $mode $path succeeded", 1,
  842.                
  843.                500, "syntax error", 0,
  844.                501, "syntax error", 0,
  845.                502, "CHMOD not implemented", 0,
  846.                530, "not logged in", 0,
  847.                        550, "file unavailable", 0,
  848.  
  849.                421, "service unavailable, closing connection", 0 );
  850.     return $ret;
  851. }
  852.  
  853. # rename a file
  854. sub ftp'rename
  855. {
  856.     local( $old_name, $new_name ) = @_;
  857.     local( $ret );
  858.  
  859.     &ftp'send( "RNFR $old_name" );
  860.  
  861.     # 
  862.     # see what they say
  863.  
  864.     $ret = &ftp'expect( $timeout, 
  865.                350, "", 1,
  866.                
  867.                500, "syntax error", 0,
  868.                501, "syntax error", 0,
  869.                502, "RNFR not implemented", 0,
  870.                530, "not logged in", 0,
  871.                        550, "file unavailable", 0,
  872.                        450, "file unavailable", 0,
  873.                
  874.                421, "service unavailable, closing connection", 0);
  875.  
  876.  
  877.     # check if the "rename from" occurred ok
  878.     if( $ret ) {
  879.         &ftp'send( "RNTO $new_name" );
  880.     
  881.         # 
  882.         # see what they say
  883.     
  884.         $ret = &ftp'expect( $timeout, 
  885.                        250, "rename $old_name to $new_name", 1, 
  886.  
  887.                    500, "syntax error", 0,
  888.                    501, "syntax error", 0,
  889.                    502, "RNTO not implemented", 0,
  890.                    503, "bad sequence of commands", 0,
  891.                    530, "not logged in", 0,
  892.                            532, "need account for storing files", 0,
  893.                            553, "file name not allowed", 0,
  894.                    
  895.                    421, "service unavailable, closing connection", 0);
  896.     }
  897.  
  898.     return $ret;
  899. }
  900.  
  901.  
  902. sub ftp'quote
  903. {
  904.       local( $cmd ) = @_;
  905.  
  906.       &ftp'send( $cmd );
  907.  
  908.       return &ftp'expect( $timeout, 
  909.               200, "Remote '$cmd' OK", 1,
  910.               500, "error in remote '$cmd'", 0 );
  911. }
  912.  
  913. # ------------------------------------------------------------------------------
  914. # These are the lower level support routines
  915.  
  916. sub ftp'expectgot
  917. {
  918.     ($ftp'response, $ftp'fatalerror) = @_;
  919.     if( $ftp_show ){
  920.         print STDERR "$ftp'response\n";
  921.     }
  922. }
  923.  
  924. #
  925. #  create the list of parameters for chat'expect
  926. #
  927. #  ftp'expect(time_out, {value, string_to_print, return value});
  928. #     if the string_to_print is "" then nothing is printed
  929. #  the last response is stored in $ftp'response
  930. #
  931. # NOTE: lmjm has changed this code such that the string_to_print is
  932. # ignored and the string sent back from the remote system is printed
  933. # instead.
  934. #
  935. sub ftp'expect {
  936.     local( $ret );
  937.     local( $time_out );
  938.     local( $expect_args );
  939.     
  940.     $ftp'response = '';
  941.     $ftp'fatalerror = 0;
  942.  
  943.     @expect_args = ();
  944.     
  945.     $time_out = shift(@_);
  946.     
  947.     while( @_ ){
  948.         local( $code ) = shift( @_ );
  949.         local( $pre ) = '^';
  950.         if( $code =~ /^\d/ ){
  951.             $pre =~ "[.|\n]*^";
  952.         }
  953.         push( @expect_args, "$pre(" . $code . " .*)\\015\\n" );
  954.         shift( @_ );
  955.         push( @expect_args, 
  956.             "&ftp'expectgot( \$1, 0 ); " . shift( @_ ) );
  957.     }
  958.     
  959.     # Treat all unrecognised lines as continuations
  960.     push( @expect_args, "^(.*)\\015\\n" );
  961.     push( @expect_args, "&ftp'expectgot( \$1, 0 ); 100" );
  962.     
  963.     # add patterns TIMEOUT and EOF
  964.     
  965.     push( @expect_args, 'TIMEOUT' );
  966.     push( @expect_args, "&ftp'expectgot( \"timed out\", 1 ); 0" );
  967.     
  968.     push( @expect_args, 'EOF' );
  969.     push( @expect_args, "&ftp'expectgot( \"remote server gone away\", 1 ); 0" );
  970.     
  971.     if( $ftp_show > 9 ){
  972.         &printargs( $time_out, @expect_args );
  973.     }
  974.     
  975.     $ret = &chat'expect( $time_out, @expect_args );
  976.     if( $ret == 100 ){
  977.         # we saw a continuation line, wait for the end
  978.         push( @expect_args, "^.*\n" );
  979.         push( @expect_args, "100" );
  980.     
  981.         while( $ret == 100 ){
  982.             $ret = &chat'expect( $time_out, @expect_args );
  983.         }
  984.     }
  985.     
  986.     return $ret;
  987. }
  988.  
  989. #
  990. #  opens NS for io
  991. #
  992. sub ftp'open_data_socket
  993. {
  994.     local( $ret );
  995.     local( $hostname );
  996.     local( $sockaddr, $name, $aliases, $proto, $port );
  997.     local( $type, $len, $thisaddr, $myaddr, $a, $b, $c, $d );
  998.     local( $mysockaddr, $family, $hi, $lo );
  999.     
  1000.     
  1001.     $sockaddr = 'S n a4 x8';
  1002.     chop( $hostname = `hostname` );
  1003.     
  1004.     $port = "ftp";
  1005.     
  1006.     ($name, $aliases, $proto) = getprotobyname( 'tcp' );
  1007.     ($name, $aliases, $port) = getservbyname( $port, 'tcp' );
  1008.     
  1009. #    ($name, $aliases, $type, $len, $thisaddr) =
  1010. #    gethostbyname( $hostname );
  1011.     ($a,$b,$c,$d) = unpack( 'C4', $chat'thisaddr );
  1012.     
  1013. #    $this = pack( $sockaddr, &main'AF_INET, 0, $thisaddr );
  1014.     $this = $chat'thisproc;
  1015.     
  1016.     socket(S, $pf_inet, $sock_stream, $proto ) || die "socket: $!";
  1017.     bind(S, $this) || die "bind: $!";
  1018.     
  1019.     # get the port number
  1020.     $mysockaddr = getsockname(S);
  1021.     ($family, $port, $myaddr) = unpack( $sockaddr, $mysockaddr );
  1022.     
  1023.     $hi = ($port >> 8) & 0x00ff;
  1024.     $lo = $port & 0x00ff;
  1025.     
  1026.     #
  1027.     # we MUST do a listen before sending the port otherwise
  1028.     # the PORT may fail
  1029.     #
  1030.     listen( S, 5 ) || die "listen";
  1031.     
  1032.     &ftp'send( "PORT $a,$b,$c,$d,$hi,$lo" );
  1033.     
  1034.     return &ftp'expect($timeout,
  1035.         200, "PORT command successful", 1,
  1036.         250, "PORT command successful", 1 ,
  1037.  
  1038.         500, "syntax error", 0,
  1039.         501, "syntax error", 0,
  1040.         530, "not logged in", 0,
  1041.  
  1042.         421, "service unavailable, closing connection", 0);
  1043. }
  1044.     
  1045. sub ftp'close_data_socket
  1046. {
  1047.     close(NS);
  1048. }
  1049.  
  1050. sub ftp'send
  1051. {
  1052.     local($send_cmd) = @_;
  1053.     if( $send_cmd =~ /\n/ ){
  1054.         print STDERR "ERROR, \\n in send string for $send_cmd\n";
  1055.     }
  1056.     
  1057.     if( $ftp_show ){
  1058.         local( $sc ) = $send_cmd;
  1059.  
  1060.         if( $send_cmd =~ /^PASS/){
  1061.             $sc = "PASS <somestring>";
  1062.         }
  1063.         print STDERR "---> $sc\n";
  1064.     }
  1065.     
  1066.     &chat'print( "$send_cmd\r\n" );
  1067. }
  1068.  
  1069. sub ftp'printargs
  1070. {
  1071.     while( @_ ){
  1072.         print STDERR shift( @_ ) . "\n";
  1073.     }
  1074. }
  1075.  
  1076. sub ftp'filesize
  1077. {
  1078.     local( $fname ) = @_;
  1079.  
  1080.     if( ! -f $fname ){
  1081.         return -1;
  1082.     }
  1083.  
  1084.     return (stat( _ ))[ 7 ];
  1085.     
  1086. }
  1087.  
  1088. # make this package return true
  1089. 1;
  1090.