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
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
;
83 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
84 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
88 if (a
->ai_canonname
!= NULL
)
89 strlcpy (hostnamebuf
, a
->ai_canonname
, sizeof(hostnamebuf
));
91 memcpy (hisctladdr
, a
->ai_addr
, a
->ai_addrlen
);
93 error
= connect (s
, a
->ai_addr
, a
->ai_addrlen
);
97 if (getnameinfo (a
->ai_addr
, a
->ai_addrlen
,
98 addrstr
, sizeof(addrstr
),
99 NULL
, 0, NI_NUMERICHOST
) != 0)
100 strlcpy (addrstr
, "unknown address", sizeof(addrstr
));
102 warn ("connect %s", addrstr
);
111 warnx ("failed to contact %s", host
);
116 len
= sizeof(myctladdr_ss
);
117 if (getsockname (s
, myctladdr
, &len
) < 0) {
118 warn ("getsockname");
123 #ifdef IPTOS_LOWDELAY
124 socket_set_tos (s
, IPTOS_LOWDELAY
);
126 cin
= fdopen (s
, "r");
127 cout
= fdopen (s
, "w");
128 if (cin
== NULL
|| cout
== NULL
) {
129 warnx ("fdopen failed.");
138 printf ("Connected to %s.\n", hostname
);
139 if (getreply (0) > 2) { /* read startup message from server */
147 #if defined(SO_OOBINLINE) && defined(HAVE_SETSOCKOPT)
151 if (setsockopt (s
, SOL_SOCKET
, SO_OOBINLINE
, (char *) &on
, sizeof (on
))
156 #endif /* SO_OOBINLINE */
168 char defaultpass
[128];
169 char *userstr
, *pass
, *acctstr
;
170 char *ruserstr
, *rpass
, *racctstr
;
174 struct passwd
*pw
= k_getpwuid(getuid());
177 myname
= pw
->pw_name
;
179 ruserstr
= rpass
= racctstr
= NULL
;
182 printf("\n*** Using plaintext user and password ***\n\n");
184 printf("Authentication successful.\n\n");
187 if (ruserpassword (host
, &ruserstr
, &rpass
, &racctstr
) < 0) {
195 while (userstr
== NULL
) {
197 printf ("Name (%s:%s): ", host
, myname
);
199 printf ("Name (%s): ", host
);
201 if (fgets (tmp
, sizeof (tmp
) - 1, stdin
) != NULL
)
202 tmp
[strlen (tmp
) - 1] = '\0';
208 strlcpy(username
, userstr
, sizeof(username
));
212 n
= command("USER %s", userstr
);
214 n
= command("PASS dummy"); /* DK: Compatibility with gssftp daemon */
215 else if(n
== CONTINUE
) {
219 (!strcmp(userstr
, "ftp") || !strcmp(userstr
, "anonymous"))) {
220 snprintf(defaultpass
, sizeof(defaultpass
),
221 "%s@%s", myname
, mydomain
);
222 snprintf(prompt
, sizeof(prompt
),
223 "Password (%s): ", defaultpass
);
224 } else if (sec_complete
) {
228 snprintf(prompt
, sizeof(prompt
), "Password: ");
232 UI_UTIL_read_pw_string (tmp
, sizeof (tmp
), prompt
, 0);
237 n
= command ("PASS %s", pass
);
243 UI_UTIL_read_pw_string (tmp
, sizeof(tmp
), "Account:", 0);
245 n
= command ("ACCT %s", acctstr
);
250 warnx ("Login failed.");
253 if (!aflag
&& acctstr
!= NULL
)
254 command ("ACCT %s", acctstr
);
259 for (n
= 0; n
< macnum
; ++n
) {
260 if (!strcmp("init", macros
[n
].mac_name
)) {
261 strlcpy (line
, "$init", sizeof (line
));
263 domacro(margc
, margv
);
267 sec_set_protection_level ();
279 longjmp (ptabort
, 1);
283 command (char *fmt
,...)
291 warn ("No control connection for command");
295 oldintr
= signal(SIGINT
, cmdabort
);
298 if (strncmp("PASS ", fmt
, 5) == 0)
302 vfprintf(stdout
, fmt
, ap
);
307 sec_vfprintf(cout
, fmt
, ap
);
313 fprintf (cout
, "\r\n");
316 r
= getreply (!strcmp (fmt
, "QUIT"));
317 if (abrtflag
&& oldintr
!= SIG_IGN
)
319 signal (SIGINT
, oldintr
);
323 char reply_string
[BUFSIZ
]; /* last line of previous reply */
326 getreply (int expecteof
)
331 struct sigaction sa
, osa
;
336 sigemptyset (&sa
.sa_mask
);
338 sa
.sa_handler
= cmdabort
;
339 sigaction (SIGINT
, &sa
, &osa
);
349 sigaction (SIGINT
, &osa
, NULL
);
355 printf ("421 Service not available, "
356 "remote server has closed connection\n");
363 if (c
== WILL
|| c
== WONT
)
364 fprintf (cout
, "%c%c%c", IAC
, DONT
, getc (cin
));
365 if (c
== DO
|| c
== DONT
)
366 fprintf (cout
, "%c%c%c", IAC
, WONT
, getc (cin
));
370 if(isdigit((unsigned char)buf
[0])){
371 sscanf(buf
, "%d", &code
);
374 sec_read_msg(buf
, prot_safe
);
375 sscanf(buf
, "%d", &code
);
377 } else if(code
== 632){
379 sec_read_msg(buf
, prot_private
);
380 sscanf(buf
, "%d", &code
);
382 }else if(code
== 633){
384 sec_read_msg(buf
, prot_confidential
);
385 sscanf(buf
, "%d", &code
);
387 }else if(sec_complete
)
391 if(code
!= 0 && reply_code
== 0)
393 if (verbose
> 0 || (verbose
> -1 && code
> 499))
394 fprintf (stdout
, "%s%s\n", lead_string
, buf
);
395 if (code
== reply_code
&& buf
[3] == ' ') {
396 strlcpy (reply_string
, buf
, sizeof(reply_string
));
399 sigaction (SIGINT
, &osa
, NULL
);
404 osa
.sa_handler
!= cmdabort
&&
405 osa
.sa_handler
!= SIG_IGN
)
406 osa
.sa_handler (SIGINT
);
408 if (code
== 227 || code
== 229) {
411 q
= strchr (reply_string
, '(');
414 strlcpy(pasv
, q
, sizeof(pasv
));
415 q
= strrchr(pasv
, ')');
423 if(verbose
> 0 || (verbose
> -1 && code
> 499)){
425 fprintf(stdout
, "!!");
426 fprintf(stdout
, "%s\n", buf
);
433 if(p
< buf
+ sizeof(buf
) - 1)
435 else if(long_warn
== 0) {
436 fprintf(stderr
, "WARNING: incredibly long line received\n");
447 getreply (int expecteof
)
451 int originalcode
= 0, continuation
= 0;
454 char *cp
, *pt
= pasv
;
456 oldintr
= signal (SIGINT
, cmdabort
);
460 while ((c
= getc (cin
)) != '\n') {
461 if (c
== IAC
) { /* handle telnet commands */
462 switch (c
= getc (cin
)) {
466 fprintf (cout
, "%c%c%c", IAC
, DONT
, c
);
472 fprintf (cout
, "%c%c%c", IAC
, WONT
, c
);
483 signal (SIGINT
, oldintr
);
489 printf ("421 Service not available, remote server has closed connection\n");
495 if (c
!= '\r' && (verbose
> 0 ||
496 (verbose
> -1 && n
== '5' && dig
> 4))) {
498 (dig
== 1 || dig
== 5 && verbose
== 0))
499 printf ("%s:", hostname
);
502 if (dig
< 4 && isdigit (c
))
503 code
= code
* 10 + (c
- '0');
504 if (!pflag
&& code
== 227)
506 if (dig
> 4 && pflag
== 1 && isdigit (c
))
509 if (c
!= '\r' && c
!= ')')
516 if (dig
== 4 && c
== '-') {
523 if (cp
< &reply_string
[sizeof (reply_string
) - 1])
526 if (verbose
> 0 || verbose
> -1 && n
== '5') {
530 if (continuation
&& code
!= originalcode
) {
531 if (originalcode
== 0)
538 sec_read_msg(reply_string
, prot_safe
);
540 sec_read_msg(reply_string
, prot_private
);
542 sec_read_msg(reply_string
, prot_confidential
);
543 n
= code
/ 100 + '0';
547 signal (SIGINT
, oldintr
);
548 if (code
== 421 || originalcode
== 421)
550 if (abrtflag
&& oldintr
!= cmdabort
&& oldintr
!= SIG_IGN
)
559 empty (fd_set
* mask
, int sec
)
565 return (select (FD_SETSIZE
, mask
, NULL
, NULL
, &t
));
576 printf ("\nsend aborted\nwaiting for remote to finish abort\n");
578 longjmp (sendabort
, 1);
581 #define HASHBYTES 1024
584 copy_stream (FILE * from
, FILE * to
)
586 static size_t bufsize
;
591 int hashbytes
= HASHBYTES
;
594 #if defined(HAVE_MMAP) && !defined(NO_MMAP)
598 #define BLOCKSIZE (1024 * 1024 * 10)
601 #define MAP_FAILED (-1)
604 if (fstat (fileno (from
), &st
) == 0 && S_ISREG (st
.st_mode
)) {
606 * mmap zero bytes has potential of loosing, don't do it.
611 while (off
!= st
.st_size
) {
615 len
= st
.st_size
- off
;
619 chunk
= mmap (0, len
, PROT_READ
, MAP_SHARED
, fileno (from
), off
);
620 if (chunk
== (void *) MAP_FAILED
) {
621 if (off
== 0) /* try read if mmap doesn't work */
626 res
= sec_write (fileno (to
), chunk
, len
);
627 if (msync (chunk
, len
, MS_ASYNC
))
629 if (munmap (chunk
, len
) < 0)
641 buf
= alloc_buffer (buf
, &bufsize
,
642 fstat (fileno (from
), &st
) >= 0 ? &st
: NULL
);
646 while ((n
= read (fileno (from
), buf
, bufsize
)) > 0) {
647 werr
= sec_write (fileno (to
), buf
, n
);
651 while (hash
&& bytes
> hashbytes
) {
653 hashbytes
+= HASHBYTES
;
669 sendrequest (char *cmd
, char *local
, char *remote
, char *lmode
, int printnames
)
672 struct timeval start
, stop
;
674 FILE *fin
, *dout
= 0;
675 int (*closefunc
) (FILE *);
676 RETSIGTYPE (*oldintr
)(int), (*oldintp
)(int);
677 long bytes
= 0, hashbytes
= HASHBYTES
;
680 if (verbose
&& printnames
) {
681 if (strcmp (local
, "-") != 0)
682 printf ("local: %s ", local
);
684 printf ("remote: %s\n", remote
);
687 proxtrans (cmd
, local
, remote
);
691 changetype (type
, 0);
696 if (setjmp (sendabort
)) {
705 signal (SIGINT
, oldintr
);
707 signal (SIGPIPE
, oldintp
);
711 oldintr
= signal (SIGINT
, abortsend
);
712 if (strcmp (local
, "-") == 0)
714 else if (*local
== '|') {
715 oldintp
= signal (SIGPIPE
, SIG_IGN
);
716 fin
= popen (local
+ 1, lmode
);
718 warn ("%s", local
+ 1);
719 signal (SIGINT
, oldintr
);
720 signal (SIGPIPE
, oldintp
);
726 fin
= fopen (local
, lmode
);
728 warn ("local: %s", local
);
729 signal (SIGINT
, oldintr
);
734 if (fstat (fileno (fin
), &st
) < 0 || !S_ISREG(st
.st_mode
)) {
735 fprintf (stdout
, "%s: not a plain file.\n", local
);
736 signal (SIGINT
, oldintr
);
743 signal (SIGINT
, oldintr
);
745 signal (SIGPIPE
, oldintp
);
747 if (closefunc
!= NULL
)
751 if (setjmp (sendabort
))
755 (strcmp (cmd
, "STOR") == 0 || strcmp (cmd
, "APPE") == 0)) {
760 rc
= fseek (fin
, (long) restart_point
, SEEK_SET
);
764 rc
= lseek (fileno (fin
), restart_point
, SEEK_SET
);
770 warn ("local: %s", local
);
772 if (closefunc
!= NULL
)
776 if (command ("REST %ld", (long) restart_point
)
779 if (closefunc
!= NULL
)
787 if (command ("%s %s", cmd
, remote
) != PRELIM
) {
788 signal (SIGINT
, oldintr
);
790 signal (SIGPIPE
, oldintp
);
791 if (closefunc
!= NULL
)
795 } else if (command ("%s", cmd
) != PRELIM
) {
796 signal(SIGINT
, oldintr
);
798 signal(SIGPIPE
, oldintp
);
799 if (closefunc
!= NULL
)
803 dout
= dataconn(rmode
);
806 set_buffer_size (fileno (dout
), 0);
807 gettimeofday (&start
, (struct timezone
*) 0);
808 oldintp
= signal (SIGPIPE
, SIG_IGN
);
814 bytes
= copy_stream (fin
, dout
);
818 while ((c
= getc (fin
)) != EOF
) {
820 while (hash
&& (bytes
>= hashbytes
)) {
823 hashbytes
+= HASHBYTES
;
827 sec_putc ('\r', dout
);
835 if (bytes
< hashbytes
)
841 warn ("local: %s", local
);
849 if (closefunc
!= NULL
)
852 gettimeofday (&stop
, (struct timezone
*) 0);
854 signal (SIGINT
, oldintr
);
856 signal (SIGPIPE
, oldintp
);
858 ptransfer ("sent", bytes
, &start
, &stop
);
861 signal (SIGINT
, oldintr
);
863 signal (SIGPIPE
, oldintp
);
876 if (closefunc
!= NULL
&& fin
!= NULL
)
878 gettimeofday (&stop
, (struct timezone
*) 0);
880 ptransfer ("sent", bytes
, &start
, &stop
);
891 printf ("\nreceive aborted\nwaiting for remote to finish abort\n");
893 longjmp (recvabort
, 1);
897 recvrequest (char *cmd
, char *local
, char *remote
,
898 char *lmode
, int printnames
, int local_given
)
900 FILE *fout
= NULL
, *din
= NULL
;
901 int (*closefunc
) (FILE *);
902 sighand oldintr
, oldintp
;
903 int c
, d
, is_retr
, tcrflag
, bare_lfs
= 0;
904 static size_t bufsize
;
906 long bytes
= 0, hashbytes
= HASHBYTES
;
907 struct timeval start
, stop
;
910 is_retr
= strcmp (cmd
, "RETR") == 0;
911 if (is_retr
&& verbose
&& printnames
) {
912 if (strcmp (local
, "-") != 0)
913 printf ("local: %s ", local
);
915 printf ("remote: %s\n", remote
);
917 if (proxy
&& is_retr
) {
918 proxtrans (cmd
, local
, remote
);
924 tcrflag
= !crflag
&& is_retr
;
925 if (setjmp (recvabort
)) {
934 signal (SIGINT
, oldintr
);
938 oldintr
= signal (SIGINT
, abortrecv
);
939 if (!local_given
|| (strcmp(local
, "-") && *local
!= '|')) {
940 if (access (local
, 2) < 0) {
941 char *dir
= strrchr (local
, '/');
943 if (errno
!= ENOENT
&& errno
!= EACCES
) {
944 warn ("local: %s", local
);
945 signal (SIGINT
, oldintr
);
951 d
= access (dir
? local
: ".", 2);
955 warn ("local: %s", local
);
956 signal (SIGINT
, oldintr
);
960 if (!runique
&& errno
== EACCES
&&
961 chmod (local
, 0600) < 0) {
962 warn ("local: %s", local
);
963 signal (SIGINT
, oldintr
);
964 signal (SIGINT
, oldintr
);
968 if (runique
&& errno
== EACCES
&&
969 (local
= gunique (local
)) == NULL
) {
970 signal (SIGINT
, oldintr
);
974 } else if (runique
&& (local
= gunique (local
)) == NULL
) {
975 signal(SIGINT
, oldintr
);
981 if (curtype
!= TYPE_A
)
982 changetype (TYPE_A
, 0);
983 } else if (curtype
!= type
)
984 changetype (type
, 0);
986 signal (SIGINT
, oldintr
);
990 if (setjmp (recvabort
))
992 if (is_retr
&& restart_point
&&
993 command ("REST %ld", (long) restart_point
) != CONTINUE
)
996 if (command ("%s %s", cmd
, remote
) != PRELIM
) {
997 signal (SIGINT
, oldintr
);
1001 if (command ("%s", cmd
) != PRELIM
) {
1002 signal (SIGINT
, oldintr
);
1006 din
= dataconn ("r");
1009 set_buffer_size (fileno (din
), 1);
1010 if (local_given
&& strcmp (local
, "-") == 0)
1012 else if (local_given
&& *local
== '|') {
1013 oldintp
= signal (SIGPIPE
, SIG_IGN
);
1014 fout
= popen (local
+ 1, "w");
1016 warn ("%s", local
+ 1);
1021 fout
= fopen (local
, lmode
);
1023 warn ("local: %s", local
);
1028 buf
= alloc_buffer (buf
, &bufsize
,
1029 fstat (fileno (fout
), &st
) >= 0 ? &st
: NULL
);
1033 gettimeofday (&start
, (struct timezone
*) 0);
1038 if (restart_point
&&
1039 lseek (fileno (fout
), restart_point
, SEEK_SET
) < 0) {
1040 warn ("local: %s", local
);
1041 if (closefunc
!= NULL
)
1042 (*closefunc
) (fout
);
1046 while ((c
= sec_read (fileno (din
), buf
, bufsize
)) > 0) {
1047 if ((d
= write (fileno (fout
), buf
, c
)) != c
)
1051 while (bytes
>= hashbytes
) {
1053 hashbytes
+= HASHBYTES
;
1058 if (hash
&& bytes
> 0) {
1059 if (bytes
< HASHBYTES
)
1071 warn ("local: %s", local
);
1073 warnx ("%s: short write", local
);
1078 if (restart_point
) {
1081 if (fseek (fout
, 0L, SEEK_SET
) < 0)
1084 for (i
= 0; i
++ < n
;) {
1085 if ((ch
= sec_getc (fout
)) == EOF
)
1090 if (fseek (fout
, 0L, SEEK_CUR
) < 0) {
1092 warn ("local: %s", local
);
1093 if (closefunc
!= NULL
)
1094 (*closefunc
) (fout
);
1098 while ((c
= sec_getc(din
)) != EOF
) {
1102 while (hash
&& (bytes
>= hashbytes
)) {
1105 hashbytes
+= HASHBYTES
;
1108 if ((c
= sec_getc (din
)) != '\n' || tcrflag
) {
1126 printf ("WARNING! %d bare linefeeds received in ASCII mode\n",
1128 printf ("File may not have transferred correctly.\n");
1131 if (bytes
< hashbytes
)
1142 warn ("local: %s", local
);
1145 if (closefunc
!= NULL
)
1146 (*closefunc
) (fout
);
1147 signal (SIGINT
, oldintr
);
1149 signal (SIGPIPE
, oldintp
);
1151 gettimeofday (&stop
, (struct timezone
*) 0);
1153 if (bytes
> 0 && is_retr
)
1154 ptransfer ("received", bytes
, &start
, &stop
);
1158 /* abort using RFC959 recommended IP,SYNC sequence */
1161 signal (SIGPIPE
, oldintr
);
1162 signal (SIGINT
, SIG_IGN
);
1165 signal (SIGINT
, oldintr
);
1174 if (closefunc
!= NULL
&& fout
!= NULL
)
1175 (*closefunc
) (fout
);
1178 gettimeofday (&stop
, (struct timezone
*) 0);
1180 ptransfer ("received", bytes
, &start
, &stop
);
1181 signal (SIGINT
, oldintr
);
1185 parse_epsv (const char *str
)
1198 port
= strtol (str
, &end
, 0);
1201 if (end
[0] != sep
|| end
[1] != '\0')
1207 parse_pasv (struct sockaddr_in
*sin4
, const char *str
)
1209 int a0
, a1
, a2
, a3
, p0
, p1
;
1212 * What we've got at this point is a string of comma separated
1213 * one-byte unsigned integer values. The first four are the an IP
1214 * address. The fifth is the MSB of the port number, the sixth is the
1215 * LSB. From that we'll prepare a sockaddr_in.
1218 if (sscanf (str
, "%d,%d,%d,%d,%d,%d",
1219 &a0
, &a1
, &a2
, &a3
, &p0
, &p1
) != 6) {
1220 printf ("Passive mode address scan failure. "
1221 "Shouldn't happen!\n");
1224 if (a0
< 0 || a0
> 255 ||
1225 a1
< 0 || a1
> 255 ||
1226 a2
< 0 || a2
> 255 ||
1227 a3
< 0 || a3
> 255 ||
1228 p0
< 0 || p0
> 255 ||
1229 p1
< 0 || p1
> 255) {
1230 printf ("Can't parse passive mode string.\n");
1233 memset (sin4
, 0, sizeof(*sin4
));
1234 sin4
->sin_family
= AF_INET
;
1235 sin4
->sin_addr
.s_addr
= htonl ((a0
<< 24) | (a1
<< 16) |
1237 sin4
->sin_port
= htons ((p0
<< 8) | p1
);
1246 data
= socket (myctladdr
->sa_family
, SOCK_STREAM
, 0);
1251 if (options
& SO_DEBUG
)
1252 socket_set_debug (data
);
1253 if (command ("EPSV") != COMPLETE
) {
1254 if (command ("PASV") != COMPLETE
) {
1255 printf ("Passive mode refused.\n");
1261 * Parse the reply to EPSV or PASV
1264 port
= parse_epsv (pasv
);
1266 data_addr
->sa_family
= myctladdr
->sa_family
;
1267 socket_set_address_and_port (data_addr
,
1268 socket_get_address (hisctladdr
),
1271 if (parse_pasv ((struct sockaddr_in
*)data_addr
, pasv
) < 0)
1275 if (connect (data
, data_addr
, socket_sockaddr_size (data_addr
)) < 0) {
1279 #ifdef IPTOS_THROUGHPUT
1280 socket_set_tos (data
, IPTOS_THROUGHPUT
);
1299 data_addr
->sa_family
= myctladdr
->sa_family
;
1300 socket_set_address_and_port (data_addr
, socket_get_address (myctladdr
),
1301 sendport
? 0 : socket_get_port (myctladdr
));
1305 data
= socket (data_addr
->sa_family
, SOCK_STREAM
, 0);
1313 socket_set_reuseaddr (data
, 1);
1314 if (bind (data
, data_addr
, socket_sockaddr_size (data_addr
)) < 0) {
1318 if (options
& SO_DEBUG
)
1319 socket_set_debug (data
);
1320 len
= sizeof (data_addr_ss
);
1321 if (getsockname (data
, data_addr
, &len
) < 0) {
1322 warn ("getsockname");
1325 if (listen (data
, 1) < 0)
1332 if (inet_ntop (data_addr
->sa_family
, socket_get_address (data_addr
),
1333 addr_str
, sizeof(addr_str
)) == NULL
)
1334 errx (1, "inet_ntop failed");
1335 switch (data_addr
->sa_family
) {
1345 errx (1, "bad address family %d", data_addr
->sa_family
);
1353 result
= command ("EPRT |%d|%s|%d|",
1355 ntohs(socket_get_port (data_addr
)));
1358 if (result
== ERROR
) {
1359 struct sockaddr_in
*sin4
= (struct sockaddr_in
*)data_addr
;
1361 unsigned int a
= ntohl(sin4
->sin_addr
.s_addr
);
1362 unsigned int p
= ntohs(sin4
->sin_port
);
1364 if (data_addr
->sa_family
!= AF_INET
) {
1365 warnx ("remote server doesn't support EPRT");
1369 result
= command("PORT %d,%d,%d,%d,%d,%d",
1376 if (result
== ERROR
&& sendport
== -1) {
1381 return (result
!= COMPLETE
);
1383 return result
!= COMPLETE
;
1389 #ifdef IPTOS_THROUGHPUT
1390 socket_set_tos (data
, IPTOS_THROUGHPUT
);
1402 * Need to start a listen on the data channel before we send the command,
1403 * otherwise the server's connect may fail.
1409 return passive_mode ();
1411 return active_mode ();
1415 dataconn (const char *lmode
)
1417 struct sockaddr_storage from_ss
;
1418 struct sockaddr
*from
= (struct sockaddr
*)&from_ss
;
1419 socklen_t fromlen
= sizeof(from_ss
);
1423 return (fdopen (data
, lmode
));
1425 s
= accept (data
, from
, &fromlen
);
1428 close (data
), data
= -1;
1433 #ifdef IPTOS_THROUGHPUT
1434 socket_set_tos (s
, IPTOS_THROUGHPUT
);
1436 return (fdopen (data
, lmode
));
1440 ptransfer (char *direction
, long int bytes
,
1441 struct timeval
* t0
, struct timeval
* t1
)
1450 td
.tv_sec
= t1
->tv_sec
- t0
->tv_sec
;
1451 td
.tv_usec
= t1
->tv_usec
- t0
->tv_usec
;
1452 if (td
.tv_usec
< 0) {
1454 td
.tv_usec
+= 1000000;
1456 s
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.);
1457 bs
= bytes
/ (s
? s
: 1);
1458 if (bs
>= 1048576) {
1462 } else if (bs
>= 1024) {
1471 printf ("%ld bytes %s in %.3g seconds (%.*f %sbyte/s)\n",
1472 bytes
, direction
, s
, prec
, bs
, unit
);
1487 static struct comvars
{
1489 char name
[MaxHostNameLen
];
1490 struct sockaddr_storage mctl
;
1491 struct sockaddr_storage hctl
;
1504 char mi
[MaxPathLen
];
1505 char mo
[MaxPathLen
];
1506 } proxstruct
, tmpstruct
;
1507 struct comvars
*ip
, *op
;
1510 oldintr
= signal (SIGINT
, psabort
);
1524 ip
->connect
= connected
;
1525 connected
= op
->connect
;
1527 strlcpy (ip
->name
, hostname
, sizeof (ip
->name
));
1530 hostname
= op
->name
;
1531 ip
->hctl
= hisctladdr_ss
;
1532 hisctladdr_ss
= op
->hctl
;
1533 ip
->mctl
= myctladdr_ss
;
1534 myctladdr_ss
= op
->mctl
;
1541 ip
->curtpe
= curtype
;
1542 curtype
= op
->curtpe
;
1545 ip
->sunqe
= sunique
;
1546 sunique
= op
->sunqe
;
1547 ip
->runqe
= runique
;
1548 runique
= op
->runqe
;
1553 strlcpy (ip
->nti
, ntin
, sizeof (ip
->nti
));
1554 strlcpy (ntin
, op
->nti
, 17);
1555 strlcpy (ip
->nto
, ntout
, sizeof (ip
->nto
));
1556 strlcpy (ntout
, op
->nto
, 17);
1557 ip
->mapflg
= mapflag
;
1558 mapflag
= op
->mapflg
;
1559 strlcpy (ip
->mi
, mapin
, MaxPathLen
);
1560 strlcpy (mapin
, op
->mi
, MaxPathLen
);
1561 strlcpy (ip
->mo
, mapout
, MaxPathLen
);
1562 strlcpy (mapout
, op
->mo
, MaxPathLen
);
1563 signal(SIGINT
, oldintr
);
1566 (*oldintr
) (SIGINT
);
1579 longjmp (ptabort
, 1);
1583 proxtrans (char *cmd
, char *local
, char *remote
)
1585 sighand oldintr
= NULL
;
1586 int secndflag
= 0, prox_type
, nfnd
;
1590 if (strcmp (cmd
, "RETR"))
1593 cmd2
= runique
? "STOU" : "STOR";
1594 if ((prox_type
= type
) == 0) {
1595 if (unix_server
&& unix_proxy
)
1600 if (curtype
!= prox_type
)
1601 changetype (prox_type
, 1);
1602 if (command ("PASV") != COMPLETE
) {
1603 printf ("proxy server does not support third party transfers.\n");
1608 printf ("No primary connection\n");
1613 if (curtype
!= prox_type
)
1614 changetype (prox_type
, 1);
1615 if (command ("PORT %s", pasv
) != COMPLETE
) {
1619 if (setjmp (ptabort
))
1621 oldintr
= signal (SIGINT
, abortpt
);
1622 if (command ("%s %s", cmd
, remote
) != PRELIM
) {
1623 signal (SIGINT
, oldintr
);
1630 if (command ("%s %s", cmd2
, local
) != PRELIM
)
1636 signal (SIGINT
, oldintr
);
1639 printf ("local: %s remote: %s\n", local
, remote
);
1642 signal (SIGINT
, SIG_IGN
);
1644 if (strcmp (cmd
, "RETR") && !proxy
)
1646 else if (!strcmp (cmd
, "RETR") && proxy
)
1648 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1649 if (command ("%s %s", cmd2
, local
) != PRELIM
) {
1652 abort_remote ((FILE *) NULL
);
1658 signal (SIGINT
, oldintr
);
1662 abort_remote ((FILE *) NULL
);
1664 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1665 if (command ("%s %s", cmd2
, local
) != PRELIM
) {
1668 abort_remote ((FILE *) NULL
);
1672 signal (SIGINT
, oldintr
);
1677 abort_remote ((FILE *) NULL
);
1681 if (fileno(cin
) >= FD_SETSIZE
)
1682 errx (1, "fd too large");
1683 FD_SET (fileno (cin
), &mask
);
1684 if ((nfnd
= empty (&mask
, 10)) <= 0) {
1700 signal (SIGINT
, oldintr
);
1704 reset (int argc
, char **argv
)
1711 if (fileno (cin
) >= FD_SETSIZE
)
1712 errx (1, "fd too large");
1713 FD_SET (fileno (cin
), &mask
);
1714 if ((nfnd
= empty (&mask
, 0)) < 0) {
1725 gunique (char *local
)
1727 static char new[MaxPathLen
];
1728 char *cp
= strrchr (local
, '/');
1734 d
= access (cp
? local
: ".", 2);
1738 warn ("local: %s", local
);
1741 strlcpy (new, local
, sizeof(new));
1742 cp
= new + strlen(new);
1745 if (++count
== 100) {
1746 printf ("runique: can't find unique file name.\n");
1755 if ((d
= access (new, 0)) < 0)
1759 else if (*(cp
- 2) == '.')
1762 *(cp
- 2) = *(cp
- 2) + 1;
1770 abort_remote (FILE * din
)
1777 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1778 * after urgent byte rather than before as is protocol now
1780 snprintf (buf
, sizeof (buf
), "%c%c%c", IAC
, IP
, IAC
);
1781 if (send (fileno (cout
), buf
, 3, MSG_OOB
) != 3)
1783 fprintf (cout
, "%c", DM
);
1784 sec_fprintf(cout
, "ABOR");
1786 fprintf (cout
, "\r\n");
1789 if (fileno (cin
) >= FD_SETSIZE
)
1790 errx (1, "fd too large");
1791 FD_SET (fileno (cin
), &mask
);
1793 if (fileno (din
) >= FD_SETSIZE
)
1794 errx (1, "fd too large");
1795 FD_SET (fileno (din
), &mask
);
1797 if ((nfnd
= empty (&mask
, 10)) <= 0) {
1805 if (din
&& FD_ISSET (fileno (din
), &mask
)) {
1806 while (read (fileno (din
), buf
, BUFSIZ
) > 0)
1809 if (getreply (0) == ERROR
&& code
== 552) {
1810 /* 552 needed for nic style abort */