2 * Copyright (c) 1985, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID ("$Id: ftp.c,v 1.75.2.1 2004/08/20 14:59:06 lha Exp $");
37 struct sockaddr_storage hisctladdr_ss
;
38 struct sockaddr
*hisctladdr
= (struct sockaddr
*)&hisctladdr_ss
;
39 struct sockaddr_storage data_addr_ss
;
40 struct sockaddr
*data_addr
= (struct sockaddr
*)&data_addr_ss
;
41 struct sockaddr_storage myctladdr_ss
;
42 struct sockaddr
*myctladdr
= (struct sockaddr
*)&myctladdr_ss
;
48 off_t restart_point
= 0;
53 typedef void (*sighand
) (int);
56 hookup (const char *host
, int port
)
58 static char hostnamebuf
[MaxHostNameLen
];
59 struct addrinfo
*ai
, *a
;
60 struct addrinfo hints
;
62 char portstr
[NI_MAXSERV
];
66 memset (&hints
, 0, sizeof(hints
));
67 hints
.ai_socktype
= SOCK_STREAM
;
68 hints
.ai_protocol
= IPPROTO_TCP
;
69 hints
.ai_flags
= AI_CANONNAME
;
71 snprintf (portstr
, sizeof(portstr
), "%u", ntohs(port
));
73 error
= getaddrinfo (host
, portstr
, &hints
, &ai
);
75 warnx ("%s: %s", host
, gai_strerror(error
));
79 strlcpy (hostnamebuf
, host
, sizeof(hostnamebuf
));
80 hostname
= hostnamebuf
;
82 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
83 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
87 if (a
->ai_canonname
!= NULL
)
88 strlcpy (hostnamebuf
, a
->ai_canonname
, sizeof(hostnamebuf
));
90 memcpy (hisctladdr
, a
->ai_addr
, a
->ai_addrlen
);
92 error
= connect (s
, a
->ai_addr
, a
->ai_addrlen
);
96 if (getnameinfo (a
->ai_addr
, a
->ai_addrlen
,
97 addrstr
, sizeof(addrstr
),
98 NULL
, 0, NI_NUMERICHOST
) != 0)
99 strlcpy (addrstr
, "unknown address", sizeof(addrstr
));
101 warn ("connect %s", addrstr
);
109 warnx ("failed to contact %s", host
);
114 len
= sizeof(myctladdr_ss
);
115 if (getsockname (s
, myctladdr
, &len
) < 0) {
116 warn ("getsockname");
121 #ifdef IPTOS_LOWDELAY
122 socket_set_tos (s
, IPTOS_LOWDELAY
);
124 cin
= fdopen (s
, "r");
125 cout
= fdopen (s
, "w");
126 if (cin
== NULL
|| cout
== NULL
) {
127 warnx ("fdopen failed.");
136 printf ("Connected to %s.\n", hostname
);
137 if (getreply (0) > 2) { /* read startup message from server */
145 #if defined(SO_OOBINLINE) && defined(HAVE_SETSOCKOPT)
149 if (setsockopt (s
, SOL_SOCKET
, SO_OOBINLINE
, (char *) &on
, sizeof (on
))
154 #endif /* SO_OOBINLINE */
166 char defaultpass
[128];
167 char *user
, *pass
, *acct
;
171 struct passwd
*pw
= k_getpwuid(getuid());
174 myname
= pw
->pw_name
;
176 user
= pass
= acct
= 0;
179 printf("\n*** Using plaintext user and password ***\n\n");
181 printf("Authentication successful.\n\n");
184 if (ruserpass (host
, &user
, &pass
, &acct
) < 0) {
188 while (user
== NULL
) {
190 printf ("Name (%s:%s): ", host
, myname
);
192 printf ("Name (%s): ", host
);
194 if (fgets (tmp
, sizeof (tmp
) - 1, stdin
) != NULL
)
195 tmp
[strlen (tmp
) - 1] = '\0';
201 strlcpy(username
, user
, sizeof(username
));
202 n
= command("USER %s", user
);
204 n
= command("PASS dummy"); /* DK: Compatibility with gssftp daemon */
205 else if(n
== CONTINUE
) {
209 (!strcmp(user
, "ftp") || !strcmp(user
, "anonymous"))) {
210 snprintf(defaultpass
, sizeof(defaultpass
),
211 "%s@%s", myname
, mydomain
);
212 snprintf(prompt
, sizeof(prompt
),
213 "Password (%s): ", defaultpass
);
214 } else if (sec_complete
) {
218 snprintf(prompt
, sizeof(prompt
), "Password: ");
222 des_read_pw_string (tmp
, sizeof (tmp
), prompt
, 0);
227 n
= command ("PASS %s", pass
);
232 des_read_pw_string (acct
, 128, "Account:", 0);
233 n
= command ("ACCT %s", acct
);
236 warnx ("Login failed.");
239 if (!aflag
&& acct
!= NULL
)
240 command ("ACCT %s", acct
);
243 for (n
= 0; n
< macnum
; ++n
) {
244 if (!strcmp("init", macros
[n
].mac_name
)) {
245 strlcpy (line
, "$init", sizeof (line
));
247 domacro(margc
, margv
);
251 sec_set_protection_level ();
263 longjmp (ptabort
, 1);
267 command (char *fmt
,...)
275 warn ("No control connection for command");
279 oldintr
= signal(SIGINT
, cmdabort
);
282 if (strncmp("PASS ", fmt
, 5) == 0)
286 vfprintf(stdout
, fmt
, ap
);
291 sec_vfprintf(cout
, fmt
, ap
);
297 fprintf (cout
, "\r\n");
300 r
= getreply (!strcmp (fmt
, "QUIT"));
301 if (abrtflag
&& oldintr
!= SIG_IGN
)
303 signal (SIGINT
, oldintr
);
307 char reply_string
[BUFSIZ
]; /* last line of previous reply */
310 getreply (int expecteof
)
315 struct sigaction sa
, osa
;
320 sigemptyset (&sa
.sa_mask
);
322 sa
.sa_handler
= cmdabort
;
323 sigaction (SIGINT
, &sa
, &osa
);
333 sigaction (SIGINT
, &osa
, NULL
);
339 printf ("421 Service not available, "
340 "remote server has closed connection\n");
347 if (c
== WILL
|| c
== WONT
)
348 fprintf (cout
, "%c%c%c", IAC
, DONT
, getc (cin
));
349 if (c
== DO
|| c
== DONT
)
350 fprintf (cout
, "%c%c%c", IAC
, WONT
, getc (cin
));
355 sscanf(buf
, "%d", &code
);
358 sec_read_msg(buf
, prot_safe
);
359 sscanf(buf
, "%d", &code
);
361 } else if(code
== 632){
363 sec_read_msg(buf
, prot_private
);
364 sscanf(buf
, "%d", &code
);
366 }else if(code
== 633){
368 sec_read_msg(buf
, prot_confidential
);
369 sscanf(buf
, "%d", &code
);
371 }else if(sec_complete
)
375 if(code
!= 0 && reply_code
== 0)
377 if (verbose
> 0 || (verbose
> -1 && code
> 499))
378 fprintf (stdout
, "%s%s\n", lead_string
, buf
);
379 if (code
== reply_code
&& buf
[3] == ' ') {
380 strlcpy (reply_string
, buf
, sizeof(reply_string
));
383 sigaction (SIGINT
, &osa
, NULL
);
388 osa
.sa_handler
!= cmdabort
&&
389 osa
.sa_handler
!= SIG_IGN
)
390 osa
.sa_handler (SIGINT
);
392 if (code
== 227 || code
== 229) {
395 p
= strchr (reply_string
, '(');
398 strlcpy(pasv
, p
, sizeof(pasv
));
399 p
= strrchr(pasv
, ')');
407 if(verbose
> 0 || (verbose
> -1 && code
> 499)){
409 fprintf(stdout
, "!!");
410 fprintf(stdout
, "%s\n", buf
);
417 if(p
< buf
+ sizeof(buf
) - 1)
419 else if(long_warn
== 0) {
420 fprintf(stderr
, "WARNING: incredibly long line received\n");
431 getreply (int expecteof
)
435 int originalcode
= 0, continuation
= 0;
438 char *cp
, *pt
= pasv
;
440 oldintr
= signal (SIGINT
, cmdabort
);
444 while ((c
= getc (cin
)) != '\n') {
445 if (c
== IAC
) { /* handle telnet commands */
446 switch (c
= getc (cin
)) {
450 fprintf (cout
, "%c%c%c", IAC
, DONT
, c
);
456 fprintf (cout
, "%c%c%c", IAC
, WONT
, c
);
467 signal (SIGINT
, oldintr
);
473 printf ("421 Service not available, remote server has closed connection\n");
479 if (c
!= '\r' && (verbose
> 0 ||
480 (verbose
> -1 && n
== '5' && dig
> 4))) {
482 (dig
== 1 || dig
== 5 && verbose
== 0))
483 printf ("%s:", hostname
);
486 if (dig
< 4 && isdigit (c
))
487 code
= code
* 10 + (c
- '0');
488 if (!pflag
&& code
== 227)
490 if (dig
> 4 && pflag
== 1 && isdigit (c
))
493 if (c
!= '\r' && c
!= ')')
500 if (dig
== 4 && c
== '-') {
507 if (cp
< &reply_string
[sizeof (reply_string
) - 1])
510 if (verbose
> 0 || verbose
> -1 && n
== '5') {
514 if (continuation
&& code
!= originalcode
) {
515 if (originalcode
== 0)
522 sec_read_msg(reply_string
, prot_safe
);
524 sec_read_msg(reply_string
, prot_private
);
526 sec_read_msg(reply_string
, prot_confidential
);
527 n
= code
/ 100 + '0';
531 signal (SIGINT
, oldintr
);
532 if (code
== 421 || originalcode
== 421)
534 if (abrtflag
&& oldintr
!= cmdabort
&& oldintr
!= SIG_IGN
)
543 empty (fd_set
* mask
, int sec
)
549 return (select (FD_SETSIZE
, mask
, NULL
, NULL
, &t
));
560 printf ("\nsend aborted\nwaiting for remote to finish abort\n");
562 longjmp (sendabort
, 1);
565 #define HASHBYTES 1024
568 copy_stream (FILE * from
, FILE * to
)
570 static size_t bufsize
;
575 int hashbytes
= HASHBYTES
;
578 #if defined(HAVE_MMAP) && !defined(NO_MMAP)
582 #define MAP_FAILED (-1)
585 if (fstat (fileno (from
), &st
) == 0 && S_ISREG (st
.st_mode
)) {
587 * mmap zero bytes has potential of loosing, don't do it.
591 chunk
= mmap (0, st
.st_size
, PROT_READ
, MAP_SHARED
, fileno (from
), 0);
592 if (chunk
!= (void *) MAP_FAILED
) {
595 res
= sec_write (fileno (to
), chunk
, st
.st_size
);
596 if (munmap (chunk
, st
.st_size
) < 0)
604 buf
= alloc_buffer (buf
, &bufsize
,
605 fstat (fileno (from
), &st
) >= 0 ? &st
: NULL
);
609 while ((n
= read (fileno (from
), buf
, bufsize
)) > 0) {
610 werr
= sec_write (fileno (to
), buf
, n
);
614 while (hash
&& bytes
> hashbytes
) {
616 hashbytes
+= HASHBYTES
;
632 sendrequest (char *cmd
, char *local
, char *remote
, char *lmode
, int printnames
)
635 struct timeval start
, stop
;
637 FILE *fin
, *dout
= 0;
638 int (*closefunc
) (FILE *);
639 RETSIGTYPE (*oldintr
)(int), (*oldintp
)(int);
640 long bytes
= 0, hashbytes
= HASHBYTES
;
643 if (verbose
&& printnames
) {
644 if (local
&& strcmp (local
, "-") != 0)
645 printf ("local: %s ", local
);
647 printf ("remote: %s\n", remote
);
650 proxtrans (cmd
, local
, remote
);
654 changetype (type
, 0);
659 if (setjmp (sendabort
)) {
668 signal (SIGINT
, oldintr
);
670 signal (SIGPIPE
, oldintp
);
674 oldintr
= signal (SIGINT
, abortsend
);
675 if (strcmp (local
, "-") == 0)
677 else if (*local
== '|') {
678 oldintp
= signal (SIGPIPE
, SIG_IGN
);
679 fin
= popen (local
+ 1, lmode
);
681 warn ("%s", local
+ 1);
682 signal (SIGINT
, oldintr
);
683 signal (SIGPIPE
, oldintp
);
689 fin
= fopen (local
, lmode
);
691 warn ("local: %s", local
);
692 signal (SIGINT
, oldintr
);
697 if (fstat (fileno (fin
), &st
) < 0 ||
698 (st
.st_mode
& S_IFMT
) != S_IFREG
) {
699 fprintf (stdout
, "%s: not a plain file.\n", local
);
700 signal (SIGINT
, oldintr
);
707 signal (SIGINT
, oldintr
);
709 signal (SIGPIPE
, oldintp
);
711 if (closefunc
!= NULL
)
715 if (setjmp (sendabort
))
719 (strcmp (cmd
, "STOR") == 0 || strcmp (cmd
, "APPE") == 0)) {
724 rc
= fseek (fin
, (long) restart_point
, SEEK_SET
);
728 rc
= lseek (fileno (fin
), restart_point
, SEEK_SET
);
732 warn ("local: %s", local
);
734 if (closefunc
!= NULL
)
738 if (command ("REST %ld", (long) restart_point
)
741 if (closefunc
!= NULL
)
749 if (command ("%s %s", cmd
, remote
) != PRELIM
) {
750 signal (SIGINT
, oldintr
);
752 signal (SIGPIPE
, oldintp
);
753 if (closefunc
!= NULL
)
757 } else if (command ("%s", cmd
) != PRELIM
) {
758 signal(SIGINT
, oldintr
);
760 signal(SIGPIPE
, oldintp
);
761 if (closefunc
!= NULL
)
765 dout
= dataconn(rmode
);
768 set_buffer_size (fileno (dout
), 0);
769 gettimeofday (&start
, (struct timezone
*) 0);
770 oldintp
= signal (SIGPIPE
, SIG_IGN
);
776 bytes
= copy_stream (fin
, dout
);
780 while ((c
= getc (fin
)) != EOF
) {
782 while (hash
&& (bytes
>= hashbytes
)) {
785 hashbytes
+= HASHBYTES
;
789 sec_putc ('\r', dout
);
797 if (bytes
< hashbytes
)
803 warn ("local: %s", local
);
811 if (closefunc
!= NULL
)
814 gettimeofday (&stop
, (struct timezone
*) 0);
816 signal (SIGINT
, oldintr
);
818 signal (SIGPIPE
, oldintp
);
820 ptransfer ("sent", bytes
, &start
, &stop
);
823 signal (SIGINT
, oldintr
);
825 signal (SIGPIPE
, oldintp
);
838 if (closefunc
!= NULL
&& fin
!= NULL
)
840 gettimeofday (&stop
, (struct timezone
*) 0);
842 ptransfer ("sent", bytes
, &start
, &stop
);
853 printf ("\nreceive aborted\nwaiting for remote to finish abort\n");
855 longjmp (recvabort
, 1);
859 recvrequest (char *cmd
, char *local
, char *remote
,
860 char *lmode
, int printnames
, int local_given
)
862 FILE *fout
, *din
= 0;
863 int (*closefunc
) (FILE *);
864 sighand oldintr
, oldintp
;
865 int c
, d
, is_retr
, tcrflag
, bare_lfs
= 0;
866 static size_t bufsize
;
868 long bytes
= 0, hashbytes
= HASHBYTES
;
869 struct timeval start
, stop
;
872 is_retr
= strcmp (cmd
, "RETR") == 0;
873 if (is_retr
&& verbose
&& printnames
) {
874 if (local
&& strcmp (local
, "-") != 0)
875 printf ("local: %s ", local
);
877 printf ("remote: %s\n", remote
);
879 if (proxy
&& is_retr
) {
880 proxtrans (cmd
, local
, remote
);
886 tcrflag
= !crflag
&& is_retr
;
887 if (setjmp (recvabort
)) {
896 signal (SIGINT
, oldintr
);
900 oldintr
= signal (SIGINT
, abortrecv
);
901 if (!local_given
|| (strcmp (local
, "-") && *local
!= '|')) {
902 if (access (local
, 2) < 0) {
903 char *dir
= strrchr (local
, '/');
905 if (errno
!= ENOENT
&& errno
!= EACCES
) {
906 warn ("local: %s", local
);
907 signal (SIGINT
, oldintr
);
913 d
= access (dir
? local
: ".", 2);
917 warn ("local: %s", local
);
918 signal (SIGINT
, oldintr
);
922 if (!runique
&& errno
== EACCES
&&
923 chmod (local
, 0600) < 0) {
924 warn ("local: %s", local
);
925 signal (SIGINT
, oldintr
);
926 signal (SIGINT
, oldintr
);
930 if (runique
&& errno
== EACCES
&&
931 (local
= gunique (local
)) == NULL
) {
932 signal (SIGINT
, oldintr
);
936 } else if (runique
&& (local
= gunique (local
)) == NULL
) {
937 signal(SIGINT
, oldintr
);
943 if (curtype
!= TYPE_A
)
944 changetype (TYPE_A
, 0);
945 } else if (curtype
!= type
)
946 changetype (type
, 0);
948 signal (SIGINT
, oldintr
);
952 if (setjmp (recvabort
))
954 if (is_retr
&& restart_point
&&
955 command ("REST %ld", (long) restart_point
) != CONTINUE
)
958 if (command ("%s %s", cmd
, remote
) != PRELIM
) {
959 signal (SIGINT
, oldintr
);
963 if (command ("%s", cmd
) != PRELIM
) {
964 signal (SIGINT
, oldintr
);
968 din
= dataconn ("r");
971 set_buffer_size (fileno (din
), 1);
972 if (local_given
&& strcmp (local
, "-") == 0)
974 else if (local_given
&& *local
== '|') {
975 oldintp
= signal (SIGPIPE
, SIG_IGN
);
976 fout
= popen (local
+ 1, "w");
978 warn ("%s", local
+ 1);
983 fout
= fopen (local
, lmode
);
985 warn ("local: %s", local
);
990 buf
= alloc_buffer (buf
, &bufsize
,
991 fstat (fileno (fout
), &st
) >= 0 ? &st
: NULL
);
995 gettimeofday (&start
, (struct timezone
*) 0);
1000 if (restart_point
&&
1001 lseek (fileno (fout
), restart_point
, SEEK_SET
) < 0) {
1002 warn ("local: %s", local
);
1003 if (closefunc
!= NULL
)
1004 (*closefunc
) (fout
);
1008 while ((c
= sec_read (fileno (din
), buf
, bufsize
)) > 0) {
1009 if ((d
= write (fileno (fout
), buf
, c
)) != c
)
1013 while (bytes
>= hashbytes
) {
1015 hashbytes
+= HASHBYTES
;
1020 if (hash
&& bytes
> 0) {
1021 if (bytes
< HASHBYTES
)
1033 warn ("local: %s", local
);
1035 warnx ("%s: short write", local
);
1040 if (restart_point
) {
1043 if (fseek (fout
, 0L, SEEK_SET
) < 0)
1046 for (i
= 0; i
++ < n
;) {
1047 if ((ch
= sec_getc (fout
)) == EOF
)
1052 if (fseek (fout
, 0L, SEEK_CUR
) < 0) {
1054 warn ("local: %s", local
);
1055 if (closefunc
!= NULL
)
1056 (*closefunc
) (fout
);
1060 while ((c
= sec_getc(din
)) != EOF
) {
1064 while (hash
&& (bytes
>= hashbytes
)) {
1067 hashbytes
+= HASHBYTES
;
1070 if ((c
= sec_getc (din
)) != '\n' || tcrflag
) {
1088 printf ("WARNING! %d bare linefeeds received in ASCII mode\n",
1090 printf ("File may not have transferred correctly.\n");
1093 if (bytes
< hashbytes
)
1104 warn ("local: %s", local
);
1107 if (closefunc
!= NULL
)
1108 (*closefunc
) (fout
);
1109 signal (SIGINT
, oldintr
);
1111 signal (SIGPIPE
, oldintp
);
1113 gettimeofday (&stop
, (struct timezone
*) 0);
1115 if (bytes
> 0 && is_retr
)
1116 ptransfer ("received", bytes
, &start
, &stop
);
1120 /* abort using RFC959 recommended IP,SYNC sequence */
1123 signal (SIGPIPE
, oldintr
);
1124 signal (SIGINT
, SIG_IGN
);
1127 signal (SIGINT
, oldintr
);
1136 if (closefunc
!= NULL
&& fout
!= NULL
)
1137 (*closefunc
) (fout
);
1140 gettimeofday (&stop
, (struct timezone
*) 0);
1142 ptransfer ("received", bytes
, &start
, &stop
);
1143 signal (SIGINT
, oldintr
);
1147 parse_epsv (const char *str
)
1160 port
= strtol (str
, &end
, 0);
1163 if (end
[0] != sep
|| end
[1] != '\0')
1169 parse_pasv (struct sockaddr_in
*sin
, const char *str
)
1171 int a0
, a1
, a2
, a3
, p0
, p1
;
1174 * What we've got at this point is a string of comma separated
1175 * one-byte unsigned integer values. The first four are the an IP
1176 * address. The fifth is the MSB of the port number, the sixth is the
1177 * LSB. From that we'll prepare a sockaddr_in.
1180 if (sscanf (str
, "%d,%d,%d,%d,%d,%d",
1181 &a0
, &a1
, &a2
, &a3
, &p0
, &p1
) != 6) {
1182 printf ("Passive mode address scan failure. "
1183 "Shouldn't happen!\n");
1186 if (a0
< 0 || a0
> 255 ||
1187 a1
< 0 || a1
> 255 ||
1188 a2
< 0 || a2
> 255 ||
1189 a3
< 0 || a3
> 255 ||
1190 p0
< 0 || p0
> 255 ||
1191 p1
< 0 || p1
> 255) {
1192 printf ("Can't parse passive mode string.\n");
1195 memset (sin
, 0, sizeof(*sin
));
1196 sin
->sin_family
= AF_INET
;
1197 sin
->sin_addr
.s_addr
= htonl ((a0
<< 24) | (a1
<< 16) |
1199 sin
->sin_port
= htons ((p0
<< 8) | p1
);
1208 data
= socket (myctladdr
->sa_family
, SOCK_STREAM
, 0);
1213 if (options
& SO_DEBUG
)
1214 socket_set_debug (data
);
1215 if (command ("EPSV") != COMPLETE
) {
1216 if (command ("PASV") != COMPLETE
) {
1217 printf ("Passive mode refused.\n");
1223 * Parse the reply to EPSV or PASV
1226 port
= parse_epsv (pasv
);
1228 data_addr
->sa_family
= myctladdr
->sa_family
;
1229 socket_set_address_and_port (data_addr
,
1230 socket_get_address (hisctladdr
),
1233 if (parse_pasv ((struct sockaddr_in
*)data_addr
, pasv
) < 0)
1237 if (connect (data
, data_addr
, socket_sockaddr_size (data_addr
)) < 0) {
1241 #ifdef IPTOS_THROUGHPUT
1242 socket_set_tos (data
, IPTOS_THROUGHPUT
);
1261 data_addr
->sa_family
= myctladdr
->sa_family
;
1262 socket_set_address_and_port (data_addr
, socket_get_address (myctladdr
),
1263 sendport
? 0 : socket_get_port (myctladdr
));
1267 data
= socket (data_addr
->sa_family
, SOCK_STREAM
, 0);
1275 socket_set_reuseaddr (data
, 1);
1276 if (bind (data
, data_addr
, socket_sockaddr_size (data_addr
)) < 0) {
1280 if (options
& SO_DEBUG
)
1281 socket_set_debug (data
);
1282 len
= sizeof (data_addr_ss
);
1283 if (getsockname (data
, data_addr
, &len
) < 0) {
1284 warn ("getsockname");
1287 if (listen (data
, 1) < 0)
1294 if (inet_ntop (data_addr
->sa_family
, socket_get_address (data_addr
),
1295 addr_str
, sizeof(addr_str
)) == NULL
)
1296 errx (1, "inet_ntop failed");
1297 switch (data_addr
->sa_family
) {
1307 errx (1, "bad address family %d", data_addr
->sa_family
);
1315 result
= command ("EPRT |%d|%s|%d|",
1317 ntohs(socket_get_port (data_addr
)));
1320 if (result
== ERROR
) {
1321 struct sockaddr_in
*sin
= (struct sockaddr_in
*)data_addr
;
1323 unsigned int a
= ntohl(sin
->sin_addr
.s_addr
);
1324 unsigned int p
= ntohs(sin
->sin_port
);
1326 if (data_addr
->sa_family
!= AF_INET
) {
1327 warnx ("remote server doesn't support EPRT");
1331 result
= command("PORT %d,%d,%d,%d,%d,%d",
1338 if (result
== ERROR
&& sendport
== -1) {
1343 return (result
!= COMPLETE
);
1345 return result
!= COMPLETE
;
1351 #ifdef IPTOS_THROUGHPUT
1352 socket_set_tos (data
, IPTOS_THROUGHPUT
);
1364 * Need to start a listen on the data channel before we send the command,
1365 * otherwise the server's connect may fail.
1371 return passive_mode ();
1373 return active_mode ();
1377 dataconn (const char *lmode
)
1379 struct sockaddr_storage from_ss
;
1380 struct sockaddr
*from
= (struct sockaddr
*)&from_ss
;
1381 socklen_t fromlen
= sizeof(from_ss
);
1385 return (fdopen (data
, lmode
));
1387 s
= accept (data
, from
, &fromlen
);
1390 close (data
), data
= -1;
1395 #ifdef IPTOS_THROUGHPUT
1396 socket_set_tos (s
, IPTOS_THROUGHPUT
);
1398 return (fdopen (data
, lmode
));
1402 ptransfer (char *direction
, long int bytes
,
1403 struct timeval
* t0
, struct timeval
* t1
)
1412 td
.tv_sec
= t1
->tv_sec
- t0
->tv_sec
;
1413 td
.tv_usec
= t1
->tv_usec
- t0
->tv_usec
;
1414 if (td
.tv_usec
< 0) {
1416 td
.tv_usec
+= 1000000;
1418 s
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.);
1419 bs
= bytes
/ (s
? s
: 1);
1420 if (bs
>= 1048576) {
1424 } else if (bs
>= 1024) {
1433 printf ("%ld bytes %s in %.3g seconds (%.*f %sbyte/s)\n",
1434 bytes
, direction
, s
, prec
, bs
, unit
);
1449 static struct comvars
{
1451 char name
[MaxHostNameLen
];
1452 struct sockaddr_storage mctl
;
1453 struct sockaddr_storage hctl
;
1466 char mi
[MaxPathLen
];
1467 char mo
[MaxPathLen
];
1468 } proxstruct
, tmpstruct
;
1469 struct comvars
*ip
, *op
;
1472 oldintr
= signal (SIGINT
, psabort
);
1486 ip
->connect
= connected
;
1487 connected
= op
->connect
;
1489 strlcpy (ip
->name
, hostname
, sizeof (ip
->name
));
1492 hostname
= op
->name
;
1493 ip
->hctl
= hisctladdr_ss
;
1494 hisctladdr_ss
= op
->hctl
;
1495 ip
->mctl
= myctladdr_ss
;
1496 myctladdr_ss
= op
->mctl
;
1503 ip
->curtpe
= curtype
;
1504 curtype
= op
->curtpe
;
1507 ip
->sunqe
= sunique
;
1508 sunique
= op
->sunqe
;
1509 ip
->runqe
= runique
;
1510 runique
= op
->runqe
;
1515 strlcpy (ip
->nti
, ntin
, sizeof (ip
->nti
));
1516 strlcpy (ntin
, op
->nti
, 17);
1517 strlcpy (ip
->nto
, ntout
, sizeof (ip
->nto
));
1518 strlcpy (ntout
, op
->nto
, 17);
1519 ip
->mapflg
= mapflag
;
1520 mapflag
= op
->mapflg
;
1521 strlcpy (ip
->mi
, mapin
, MaxPathLen
);
1522 strlcpy (mapin
, op
->mi
, MaxPathLen
);
1523 strlcpy (ip
->mo
, mapout
, MaxPathLen
);
1524 strlcpy (mapout
, op
->mo
, MaxPathLen
);
1525 signal(SIGINT
, oldintr
);
1528 (*oldintr
) (SIGINT
);
1541 longjmp (ptabort
, 1);
1545 proxtrans (char *cmd
, char *local
, char *remote
)
1548 int secndflag
= 0, prox_type
, nfnd
;
1552 if (strcmp (cmd
, "RETR"))
1555 cmd2
= runique
? "STOU" : "STOR";
1556 if ((prox_type
= type
) == 0) {
1557 if (unix_server
&& unix_proxy
)
1562 if (curtype
!= prox_type
)
1563 changetype (prox_type
, 1);
1564 if (command ("PASV") != COMPLETE
) {
1565 printf ("proxy server does not support third party transfers.\n");
1570 printf ("No primary connection\n");
1575 if (curtype
!= prox_type
)
1576 changetype (prox_type
, 1);
1577 if (command ("PORT %s", pasv
) != COMPLETE
) {
1581 if (setjmp (ptabort
))
1583 oldintr
= signal (SIGINT
, abortpt
);
1584 if (command ("%s %s", cmd
, remote
) != PRELIM
) {
1585 signal (SIGINT
, oldintr
);
1592 if (command ("%s %s", cmd2
, local
) != PRELIM
)
1598 signal (SIGINT
, oldintr
);
1601 printf ("local: %s remote: %s\n", local
, remote
);
1604 signal (SIGINT
, SIG_IGN
);
1606 if (strcmp (cmd
, "RETR") && !proxy
)
1608 else if (!strcmp (cmd
, "RETR") && proxy
)
1610 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1611 if (command ("%s %s", cmd2
, local
) != PRELIM
) {
1614 abort_remote ((FILE *) NULL
);
1619 signal (SIGINT
, oldintr
);
1623 abort_remote ((FILE *) NULL
);
1625 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1626 if (command ("%s %s", cmd2
, local
) != PRELIM
) {
1629 abort_remote ((FILE *) NULL
);
1633 signal (SIGINT
, oldintr
);
1638 abort_remote ((FILE *) NULL
);
1642 if (fileno(cin
) >= FD_SETSIZE
)
1643 errx (1, "fd too large");
1644 FD_SET (fileno (cin
), &mask
);
1645 if ((nfnd
= empty (&mask
, 10)) <= 0) {
1661 signal (SIGINT
, oldintr
);
1665 reset (int argc
, char **argv
)
1672 if (fileno (cin
) >= FD_SETSIZE
)
1673 errx (1, "fd too large");
1674 FD_SET (fileno (cin
), &mask
);
1675 if ((nfnd
= empty (&mask
, 0)) < 0) {
1686 gunique (char *local
)
1688 static char new[MaxPathLen
];
1689 char *cp
= strrchr (local
, '/');
1695 d
= access (cp
? local
: ".", 2);
1699 warn ("local: %s", local
);
1702 strlcpy (new, local
, sizeof(new));
1703 cp
= new + strlen(new);
1706 if (++count
== 100) {
1707 printf ("runique: can't find unique file name.\n");
1716 if ((d
= access (new, 0)) < 0)
1720 else if (*(cp
- 2) == '.')
1723 *(cp
- 2) = *(cp
- 2) + 1;
1731 abort_remote (FILE * din
)
1738 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1739 * after urgent byte rather than before as is protocol now
1741 snprintf (buf
, sizeof (buf
), "%c%c%c", IAC
, IP
, IAC
);
1742 if (send (fileno (cout
), buf
, 3, MSG_OOB
) != 3)
1744 fprintf (cout
, "%c", DM
);
1745 sec_fprintf(cout
, "ABOR");
1747 fprintf (cout
, "\r\n");
1750 if (fileno (cin
) >= FD_SETSIZE
)
1751 errx (1, "fd too large");
1752 FD_SET (fileno (cin
), &mask
);
1754 if (fileno (din
) >= FD_SETSIZE
)
1755 errx (1, "fd too large");
1756 FD_SET (fileno (din
), &mask
);
1758 if ((nfnd
= empty (&mask
, 10)) <= 0) {
1766 if (din
&& FD_ISSET (fileno (din
), &mask
)) {
1767 while (read (fileno (din
), buf
, BUFSIZ
) > 0)
1770 if (getreply (0) == ERROR
&& code
== 552) {
1771 /* 552 needed for nic style abort */