Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1077 lines
23 KiB

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