Leaked source code of windows server 2003
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.

1086 lines
25 KiB

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