Debian Codegraph @

ftp/ftp.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1985, 1989, 1993, 1994
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53 #include <sys/socket.h>
54 #include <sys/time.h>
55 #include <time.h>
56 #include <sys/file.h>
57
58 #include <netinet/in.h>
59 #ifdef HAVE_NETINET_IN_SYSTM_H
60 # include <netinet/in_systm.h>
61 #endif
62 #ifdef HAVE_NETINET_IP_H
63 # include <netinet/ip.h>
64 #endif
65 #include <arpa/inet.h>
66 #include <arpa/ftp.h>
67 #include <arpa/telnet.h>
68
69 #include <ctype.h>
70 #include <error.h>
71 #include <errno.h>
72 #include <fcntl.h>
73 #include <float.h>
74 #include <netdb.h>
75 #include <pwd.h>
76 #include <signal.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <stdint.h> /* intmax_t */
80 #include <string.h>
81 #include <unistd.h>
82 #include <stdarg.h>
83 #include <sys/select.h>
84
85 #if defined HAVE_IDN2_H && defined HAVE_IDN2
86 # include <idn2.h>
87 #elif defined HAVE_IDNA_H
88 # include <idna.h>
89 #endif
90
91 #include <intprops.h>
92 #include <timespec.h>
93
94 #include "ftp_var.h"
95 #include "attribute.h"
96
97 int data = -1;
98 int abrtflag = 0;
99 jmp_buf ptabort;
100 int ptabflg;
101 int ptflag = 0;
102 off_t restart_point = 0;
103
104 struct sockaddr_storage myctladdr;
105 struct sockaddr_storage hisctladdr;
106 struct sockaddr_storage data_addr;
107 socklen_t ctladdrlen; /* Applies to all addresses. */
108
109 /* For temporary resolving: hookup() and initconn()/noport. */
110 static char ia[INET6_ADDRSTRLEN];
111 static char portstr[INT_STRLEN_BOUND (in_port_t) + 1];
112
113 FILE *cin, *cout;
114
115 #if ! defined FTP_CONNECT_TIMEOUT || FTP_CONNECT_TIMEOUT < 1
116 # define FTP_CONNECT_TIMEOUT 5
117 #endif
118
119 char *
120 hookup (char *host, in_port_t port)
121 {
122 struct addrinfo hints, *ai = NULL, *res = NULL;
123 struct timeval timeout;
124 int status, again = 0;
125 int s, tos;
126 socklen_t len;
127 static char hostnamebuf[80];
128 char *rhost, *p;
129
130 p = strrchr (host, '@');
131 if (p && p != host && isprint (p[1]))
132 host = ++p;
133
134 #if defined HAVE_IDN || defined HAVE_IDN2
135 status = idna_to_ascii_lz (host, &rhost, 0);
136 if (status)
137 {
138 error (0, 0, "%s: %s", host, idna_strerror (status));
139 code = -1;
140 return ((char *) 0);
141 }
142 #else /* !HAVE_IDN && !HAVE_IDN2 */
143 rhost = strdup (host);
144 #endif
145
146
147 snprintf (portstr, sizeof portstr, "%u", port);
148 memset (&hisctladdr, 0, sizeof (hisctladdr));
149 memset (&hints, 0, sizeof (hints));
150
151 hints.ai_family = usefamily;
152 hints.ai_socktype = SOCK_STREAM;
153 hints.ai_flags = AI_CANONNAME;
154 #ifdef AI_IDN
155 hints.ai_flags |= AI_IDN;
156 #endif
157 #ifdef AI_CANONIDN
158 hints.ai_flags |= AI_CANONIDN;
159 #endif
160
161 status = getaddrinfo (rhost, portstr, &hints, &res);
162 if (status)
163 {
164 error (0, 0, "%s: %s", rhost, gai_strerror (status));
165 code = -1;
166 free (rhost);
167 return ((char *) 0);
168 }
169
170 if (res->ai_canonname)
171 strncpy (hostnamebuf, res->ai_canonname, sizeof (hostnamebuf) - 1);
172 else
173 strncpy (hostnamebuf, rhost, sizeof (hostnamebuf) - 1);
174 hostnamebuf[sizeof (hostnamebuf) - 1] = 0;
175
176 hostname = hostnamebuf;
177 free (rhost);
178
179 for (ai = res; ai != NULL; ai = ai->ai_next, ++again)
180 {
181 if (again)
182 {
183 getnameinfo (ai->ai_addr, ai->ai_addrlen, ia, sizeof (ia),
184 NULL, 0, NI_NUMERICHOST);
185 error (0, 0, "Trying %s ...", ia);
186 }
187
188 s = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
189 if (s < 0)
190 continue;
191
192 timeout.tv_sec = FTP_CONNECT_TIMEOUT;
193 timeout.tv_usec = 0;
194 if (setsockopt (s, SOL_SOCKET, SO_SNDTIMEO, &timeout,
195 sizeof (timeout)) < 0 && debug)
196 error (0, errno, "setsockopt (SO_SNDTIMEO)");
197
198 if (connect (s, ai->ai_addr, ai->ai_addrlen) < 0)
199 {
200 int oerrno = (errno != EINPROGRESS) ? errno : ETIMEDOUT;
201
202 getnameinfo (ai->ai_addr, ai->ai_addrlen, ia, sizeof (ia),
203 NULL, 0, NI_NUMERICHOST);
204 error (0, oerrno, "connect to address %s", ia);
205 close (s);
206 s = -1;
207 continue;
208 }
209
210 /* A standing connection is found: register the address. */
211 timeout.tv_sec = 0;
212 timeout.tv_usec = 0;
213 (void) setsockopt (s, SOL_SOCKET, SO_SNDTIMEO, &timeout,
214 sizeof (timeout));
215
216 ctladdrlen = ai->ai_addrlen;
217 memmove (&hisctladdr, ai->ai_addr, ai->ai_addrlen);
218 break;
219 } /* for (ai = ai->ai_next) */
220
221 if (res)
222 freeaddrinfo (res);
223
224 if (ai == NULL)
225 {
226 error (0, 0, "no response from host");
227 code = -1;
228 goto bad;
229 }
230
231 len = sizeof (myctladdr);
232 if (getsockname (s, (struct sockaddr *) &myctladdr, &len) < 0)
233 {
234 error (0, errno, "getsockname");
235 code = -1;
236 goto bad;
237 }
238
239 #if defined IP_TOS && defined IPPROTO_IP && defined IPTOS_LOWDELAY
240 tos = IPTOS_LOWDELAY;
241 if (myctladdr.ss_family == AF_INET &&
242 setsockopt (s, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof (int)) < 0)
243 error (0, errno, "setsockopt TOS (ignored)");
244 #endif
245
246 cin = fdopen (s, "r");
247 /* dup(s) is for sake of stdio implementations who do not
248 allow two fdopen's on the same file-descriptor */
249 cout = fdopen (dup (s), "w");
250 if (cin == NULL || cout == NULL)
251 {
252 error (0, 0, "fdopen failed.");
253 if (cin)
254 fclose (cin);
255 if (cout)
256 fclose (cout);
257 code = -1;
258 goto bad;
259 }
260 if (verbose)
261 printf ("Connected to %s.\n", hostname);
262 if (getreply (0) > 2)
263 { /* read startup message from server */
264 if (cin)
265 fclose (cin);
266 if (cout)
267 fclose (cout);
268 code = -1;
269 goto bad;
270 }
271
272 #ifdef SO_OOBINLINE
273 {
274 int on = 1;
275
276 if (setsockopt (s, SOL_SOCKET, SO_OOBINLINE, (char *) &on, sizeof (on))
277 < 0 && debug)
278 {
279 error (0, errno, "setsockopt");
280 }
281 }
282 #endif /* SO_OOBINLINE */
283
284 return (hostname);
285 bad:
286 close (s);
287 return ((char *) 0);
288 }
289
290 int
291 login (char *host)
292 {
293 char tmp[80];
294 char *user, *pass, *acct, *p;
295 int n, aflag = 0;
296
297 user = pass = acct = 0;
298 if (remote_userpass (host, &user, &pass, &acct) < 0)
299 {
300 code = -1;
301 return (0);
302 }
303
304 p = strrchr (host, '@');
305 if (user == NULL && p && p != host && isprint (p[1]))
306 {
307 *p = 0;
308 user = host;
309 host = ++p;
310 }
311
312 while (user == NULL)
313 {
314 char *myname = getlogin ();
315
316 if (myname == NULL)
317 {
318 struct passwd *pp = getpwuid (getuid ());
319
320 if (pp != NULL)
321 myname = pp->pw_name;
322 }
323 if (myname)
324 printf ("Name (%s:%s): ", host, myname);
325 else
326 printf ("Name (%s): ", host);
327 if (fgets (tmp, sizeof (tmp) - 1, stdin))
328 {
329 /* If the user presses return immediately, we get "\n".
330 * In all other cases, the assignment is a no-op,
331 * and is always well defined thanks to fgets().
332 */
333 tmp[strlen (tmp) - 1] = '\0';
334 }
335 else
336 *tmp = '\0'; /* Ctrl-D received. */
337 if (*tmp == '\0')
338 user = myname;
339 else
340 user = tmp;
341 }
342 n = command ("USER %s", user);
343 if (n == CONTINUE)
344 {
345 /* Is this a case of challenge-response?
346 * RFC 2228 stipulates code 336 for this.
347 * Suppress the message in verbose mode,
348 * since it has already been displayed.
349 */
350 if (code == 336 && !verbose)
351 printf ("%s\n", reply_string + strlen ("336 "));
352 /* In addition, any password given on the
353 * command line is irrelevant, so ignore it.
354 */
355 if (pass == NULL || code == 336)
356 pass = getpass ("Password: ");
357 n = command ("PASS %s", pass);
358 if (pass)
359 memset (pass, 0, strlen (pass));
360 }
361 if (n == CONTINUE)
362 {
363 aflag++;
364 acct = getpass ("Account: ");
365 n = command ("ACCT %s", acct);
366 if (acct)
367 memset (acct, 0, strlen (acct));
368 }
369 if (n != COMPLETE)
370 {
371 error (0, 0, "Login failed.");
372 return (0);
373 }
374 if (!aflag && acct != NULL)
375 {
376 command ("ACCT %s", acct);
377 memset (acct, 0, strlen (acct));
378 }
379 if (proxy)
380 return (1);
381 for (n = 0; n < macnum; ++n)
382 {
383 if (!strcmp ("init", macros[n].mac_name))
384 {
385 free (line);
386 line = calloc (MAXLINE, sizeof (*line));
387 linelen = MAXLINE;
388 if (!line)
389 quit (0, 0);
390
391 /* Simulate input of the macro 'init'. */
392 strcpy (line, "$init");
393 makeargv ();
394 domacro (margc, margv);
395 break;
396 }
397 }
398 return (1);
399 }
400
401 void
402 cmdabort (int sig MAYBE_UNUSED)
403 {
404
405 printf ("\n");
406 fflush (stdout);
407 abrtflag++;
408 if (ptflag)
409 longjmp (ptabort, 1);
410 }
411
412 int
413 command (const char *fmt, ...)
414 {
415 va_list ap;
416 int r;
417 sighandler_t oldintr;
418
419 abrtflag = 0;
420 if (debug)
421 {
422 printf ("---> ");
423 va_start (ap, fmt);
424 if (strncmp ("PASS ", fmt, 5) == 0)
425 printf ("PASS XXXX");
426 else if (strncmp ("ACCT ", fmt, 5) == 0)
427 printf ("ACCT XXXX");
428 else
429 vfprintf (stdout, fmt, ap);
430 va_end (ap);
431 printf ("\n");
432 fflush (stdout);
433 }
434 if (cout == NULL)
435 {
436 error (0, 0, "No control connection for command");
437 code = -1;
438 return (0);
439 }
440 oldintr = signal (SIGINT, cmdabort);
441 /* Under weird circumstances, we get a SIGPIPE from fflush(). */
442 signal (SIGPIPE, SIG_IGN);
443 va_start (ap, fmt);
444 vfprintf (cout, fmt, ap);
445 va_end (ap);
446 fprintf (cout, "\r\n");
447 fflush (cout);
448 cpend = 1;
449 r = getreply (!strcmp (fmt, "QUIT"));
450 if (abrtflag && oldintr != SIG_IGN)
451 (*oldintr) (SIGINT);
452 signal (SIGINT, oldintr);
453 signal (SIGPIPE, SIG_DFL);
454 return (r);
455 }
456
457 char reply_string[BUFSIZ]; /* last line of previous reply */
458
459 int
460 getreply (int expecteof)
461 {
462 int c, n;
463 int dig;
464 int originalcode = 0, continuation = 0;
465 sighandler_t oldintr;
466 int pflag = 0;
467 char *cp, *pt = pasv;
468
469 oldintr = signal (SIGINT, cmdabort);
470 for (;;)
471 {
472 dig = n = code = 0;
473 cp = reply_string;
474 while ((c = getc (cin)) != '\n')
475 {
476 if (c == IAC)
477 { /* handle telnet commands */
478 switch (c = getc (cin))
479 {
480 case WILL:
481 case WONT:
482 c = getc (cin);
483 fprintf (cout, "%c%c%c", IAC, DONT, c);
484 fflush (cout);
485 break;
486 case DO:
487 case DONT:
488 c = getc (cin);
489 fprintf (cout, "%c%c%c", IAC, WONT, c);
490 fflush (cout);
491 break;
492 default:
493 break;
494 }
495 continue;
496 }
497 dig++;
498 if (c == EOF)
499 {
500 if (expecteof)
501 {
502 signal (SIGINT, oldintr);
503 code = 221;
504 return (0);
505 }
506 lostpeer (0);
507 if (verbose)
508 {
509 printf
510 ("421 Service not available, remote server has closed connection\n");
511 fflush (stdout);
512 }
513 code = 421;
514 return (TRANSIENT);
515 }
516 if (c != '\r' && (verbose > 0 ||
517 (verbose > -1 && n == ERROR && dig > 4)))
518 {
519 if (proxflag && (dig == 1 || (dig == 5 && verbose == 0)))
520 printf ("%s:", hostname);
521 putchar (c);
522 }
523 if (dig < 4 && isdigit (c))
524 code = code * 10 + (c - '0');
525 if (!pflag && (code == 227 || code == 228 || code == 229)) /* PASV || LPSV || EPSV */
526 pflag = 1;
527 if (dig > 4 && pflag == 1 && isdigit (c))
528 pflag = 2;
529 if (pflag == 2)
530 {
531 if (c != '\r' && c != ')')
532 {
533 if (pt < &pasv[sizeof (pasv) - 1])
534 *pt++ = c;
535 }
536 else
537 {
538 *pt = '\0';
539 pflag = 3;
540 }
541 }
542 if (dig == 4 && c == '-')
543 {
544 if (continuation)
545 code = 0;
546 continuation++;
547 }
548 if (n == 0)
549 n = c - '0'; /* Extract ARPA's reply code. */
550 if (cp < &reply_string[sizeof (reply_string) - 1])
551 *cp++ = c;
552 }
553 if (verbose > 0 || (verbose > -1 && n == ERROR))
554 {
555 putchar (c);
556 fflush (stdout);
557 }
558 if (continuation && code != originalcode)
559 {
560 if (originalcode == 0)
561 originalcode = code;
562 continue;
563 }
564 *cp = '\0';
565 if (n != PRELIM)
566 cpend = 0;
567 signal (SIGINT, oldintr);
568 if (code == 421 || originalcode == 421)
569 lostpeer (0);
570 if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
571 (*oldintr) (SIGINT);
572 return n;
573 }
574 }
575
576 int
577 empty (fd_set *mask, int sec)
578 {
579 struct timeval t;
580
581 t.tv_sec = (long) sec;
582 t.tv_usec = 0;
583 return (select (32, mask, (fd_set *) 0, (fd_set *) 0, &t));
584 }
585
586 jmp_buf sendabort;
587
588 void
589 abortsend (int sig MAYBE_UNUSED)
590 {
591
592 mflag = 0;
593 abrtflag = 0;
594 printf ("\nsend aborted\nwaiting for remote to finish abort\n");
595 fflush (stdout);
596 longjmp (sendabort, 1);
597 }
598
599 void
600 sendrequest (char *cmd, char *local, char *remote, int printnames)
601 {
602 struct stat st;
603 struct timespec start, stop;
604 int c, d;
605 FILE *fin, *dout = 0, *popen (const char *, const char *);
606 int (*closefunc) (FILE *);
607 sighandler_t oldintr, oldintp;
608 long long bytes = 0, local_hashbytes = hashbytes;
609 char *lmode, *bufp;
610 int blksize = BUFSIZ;
611 static int bufsize = 0;
612 static char *buf;
613
614 if (verbose && printnames)
615 {
616 if (local && *local != '-')
617 printf ("local: %s ", local);
618 if (remote)
619 printf ("remote: %s\n", remote);
620 }
621 if (proxy)
622 {
623 proxtrans (cmd, local, remote);
624 return;
625 }
626 if (curtype != type)
627 changetype (type, 0);
628 closefunc = NULL;
629 oldintr = NULL;
630 oldintp = NULL;
631 lmode = "w";
632 if (setjmp (sendabort))
633 {
634 while (cpend)
635 {
636 getreply (0);
637 }
638 if (data >= 0)
639 {
640 close (data);
641 data = -1;
642 }
643 if (oldintr)
644 signal (SIGINT, oldintr);
645 if (oldintp)
646 signal (SIGPIPE, oldintp);
647 code = -1;
648 return;
649 }
650 oldintr = signal (SIGINT, abortsend);
651 if (strcmp (local, "-") == 0)
652 fin = stdin;
653 else if (*local == '|')
654 {
655 oldintp = signal (SIGPIPE, SIG_IGN);
656 fin = popen (local + 1, "r");
657 if (fin == NULL)
658 {
659 error (0, errno, "%s", local + 1);
660 signal (SIGINT, oldintr);
661 signal (SIGPIPE, oldintp);
662 code = -1;
663 return;
664 }
665 closefunc = pclose;
666 }
667 else
668 {
669 fin = fopen (local, "r");
670 if (fin == NULL)
671 {
672 error (0, errno, "local: %s", local);
673 signal (SIGINT, oldintr);
674 code = -1;
675 return;
676 }
677 closefunc = fclose;
678 if (fstat (fileno (fin), &st) < 0 || (st.st_mode & S_IFMT) != S_IFREG)
679 {
680 fprintf (stdout, "%s: not a plain file.\n", local);
681 signal (SIGINT, oldintr);
682 fclose (fin);
683 code = -1;
684 return;
685 }
686 blksize = st.st_blksize;
687 }
688 if (initconn ())
689 {
690 signal (SIGINT, oldintr);
691 if (oldintp)
692 signal (SIGPIPE, oldintp);
693 code = -1;
694 if (closefunc != NULL)
695 (*closefunc) (fin);
696 return;
697 }
698 if (setjmp (sendabort))
699 goto abort;
700
701 if (restart_point &&
702 (strcmp (cmd, "STOR") == 0 || strcmp (cmd, "APPE") == 0))
703 {
704 off_t rc;
705
706 switch (curtype)
707 {
708 case TYPE_A:
709 rc = fseeko (fin, restart_point, SEEK_SET);
710 break;
711 case TYPE_I:
712 case TYPE_L:
713 rc = lseek (fileno (fin), restart_point, SEEK_SET);
714 break;
715 }
716 if (rc < 0)
717 {
718 (void) command ("ABOR");
719 getreply (0);
720 error (0, errno, "local: %s", local);
721 restart_point = 0;
722 if (closefunc != NULL)
723 (*closefunc) (fin);
724 return;
725 }
726 if (command ("REST %jd", (intmax_t) restart_point) != CONTINUE)
727 {
728 restart_point = 0;
729 if (closefunc != NULL)
730 (*closefunc) (fin);
731 return;
732 }
733 restart_point = 0;
734 lmode = "r+w";
735 }
736 if (remote)
737 {
738 if (command ("%s %s", cmd, remote) != PRELIM)
739 {
740 signal (SIGINT, oldintr);
741 if (oldintp)
742 signal (SIGPIPE, oldintp);
743 if (closefunc != NULL)
744 (*closefunc) (fin);
745 return;
746 }
747 }
748 else if (command ("%s", cmd) != PRELIM)
749 {
750 signal (SIGINT, oldintr);
751 if (oldintp)
752 signal (SIGPIPE, oldintp);
753 if (closefunc != NULL)
754 (*closefunc) (fin);
755 return;
756 }
757 dout = dataconn (lmode);
758 if (dout == NULL)
759 goto abort;
760
761 if (blksize > bufsize)
762 {
763 free (buf);
764 buf = malloc ((unsigned) blksize);
765 if (buf == NULL)
766 {
767 error (0, errno, "malloc");
768 bufsize = 0;
769 goto abort;
770 }
771 bufsize = blksize;
772 }
773
774 start = current_timespec ();
775 oldintp = signal (SIGPIPE, SIG_IGN);
776 switch (curtype)
777 {
778
779 case TYPE_I:
780 case TYPE_L:
781 errno = d = 0;
782 while ((c = read (fileno (fin), buf, bufsize)) > 0)
783 {
784 bytes += c;
785 for (bufp = buf; c > 0; c -= d, bufp += d)
786 if ((d = write (fileno (dout), bufp, c)) <= 0)
787 break;
788 if (hash)
789 {
790 while (bytes >= local_hashbytes)
791 {
792 putchar ('#');
793 local_hashbytes += hashbytes;
794 }
795 fflush (stdout);
796 }
797 }
798 if (hash && bytes > 0)
799 {
800 if (bytes < local_hashbytes)
801 putchar ('#');
802 putchar ('\n');
803 fflush (stdout);
804 }
805 if (c < 0)
806 error (0, errno, "local: %s", local);
807 if (d < 0)
808 {
809 if (errno != EPIPE)
810 error (0, errno, "netout");
811 bytes = -1;
812 }
813 break;
814
815 case TYPE_A:
816 while ((c = getc (fin)) != EOF)
817 {
818 if (c == '\n')
819 {
820 while (hash && (bytes >= local_hashbytes))
821 {
822 putchar ('#');
823 fflush (stdout);
824 local_hashbytes += hashbytes;
825 }
826 if (ferror (dout))
827 break;
828 putc ('\r', dout);
829 bytes++;
830 }
831 putc (c, dout);
832 bytes++;
833 /* if (c == '\r') { */
834 /* putc('\0', dout); // this violates rfc */
835 /* bytes++; */
836 /* } */
837 }
838 if (hash)
839 {
840 if (bytes < local_hashbytes)
841 putchar ('#');
842 putchar ('\n');
843 fflush (stdout);
844 }
845 if (ferror (fin))
846 error (0, errno, "local: %s", local);
847 if (ferror (dout))
848 {
849 if (errno != EPIPE)
850 error (0, errno, "netout");
851 bytes = -1;
852 }
853 break;
854 }
855 if (closefunc != NULL)
856 (*closefunc) (fin);
857 fclose (dout);
858 stop = current_timespec ();
859 getreply (0);
860 signal (SIGINT, oldintr);
861 if (oldintp)
862 signal (SIGPIPE, oldintp);
863 if (bytes > 0)
864 ptransfer ("sent", bytes, &start, &stop);
865 return;
866 abort:
867 signal (SIGINT, oldintr);
868 if (oldintp)
869 signal (SIGPIPE, oldintp);
870 if (!cpend)
871 {
872 code = -1;
873 return;
874 }
875 if (data >= 0)
876 {
877 close (data);
878 data = -1;
879 }
880 if (dout)
881 fclose (dout);
882 getreply (0);
883 code = -1;
884 if (closefunc != NULL && fin != NULL)
885 (*closefunc) (fin);
886 stop = current_timespec ();
887 if (bytes > 0)
888 ptransfer ("sent", bytes, &start, &stop);
889 }
890
891 jmp_buf recvabort;
892
893 void
894 abortrecv (int sig MAYBE_UNUSED)
895 {
896
897 mflag = 0;
898 abrtflag = 0;
899 printf ("\nreceive aborted\nwaiting for remote to finish abort\n");
900 fflush (stdout);
901 longjmp (recvabort, 1);
902 }
903
904 void
905 recvrequest (char *cmd, char *local, char *remote, char *lmode,
906 int printnames)
907 {
908 FILE *fout, *din = 0;
909 int (*closefunc) (FILE *);
910 sighandler_t oldintr, oldintp;
911 int c, d, is_retr, tcrflag, bare_lfs = 0;
912 int blksize = BUFSIZ;
913 static int bufsize = 0;
914 static char *buf;
915 long long bytes = 0, local_hashbytes = hashbytes;
916 struct timespec start, stop;
917
918 is_retr = strcmp (cmd, "RETR") == 0;
919 if (is_retr && verbose && printnames)
920 {
921 if (local && *local != '-')
922 printf ("local: %s ", local);
923 if (remote)
924 printf ("remote: %s\n", remote);
925 }
926 if (proxy && is_retr)
927 {
928 proxtrans (cmd, local, remote);
929 return;
930 }
931 closefunc = NULL;
932 oldintr = NULL;
933 oldintp = NULL;
934 tcrflag = !crflag && is_retr;
935 if (setjmp (recvabort))
936 {
937 while (cpend)
938 {
939 getreply (0);
940 }
941 if (data >= 0)
942 {
943 close (data);
944 data = -1;
945 }
946 if (oldintr)
947 signal (SIGINT, oldintr);
948 code = -1;
949 return;
950 }
951 oldintr = signal (SIGINT, abortrecv);
952 if (strcmp (local, "-") && *local != '|')
953 {
954 if (runique && (local = gunique (local)) == NULL)
955 {
956 signal (SIGINT, oldintr);
957 code = -1;
958 return;
959 }
960 }
961 if (!is_retr)
962 {
963 if (curtype != TYPE_A)
964 changetype (TYPE_A, 0);
965 }
966 else if (curtype != type)
967 changetype (type, 0);
968 if (initconn ())
969 {
970 signal (SIGINT, oldintr);
971 code = -1;
972 return;
973 }
974 if (setjmp (recvabort))
975 goto abort;
976 if (is_retr && restart_point &&
977 command ("REST %jd", (intmax_t) restart_point) != CONTINUE)
978 return;
979 if (remote)
980 {
981 if (command ("%s %s", cmd, remote) != PRELIM)
982 {
983 signal (SIGINT, oldintr);
984 return;
985 }
986 }
987 else
988 {
989 if (command ("%s", cmd) != PRELIM)
990 {
991 signal (SIGINT, oldintr);
992 return;
993 }
994 }
995 din = dataconn ("r");
996 if (din == NULL)
997 goto abort;
998
999 if (strcmp (local, "-") == 0)
1000 fout = stdout;
1001 else if (*local == '|')
1002 {
1003 oldintp = signal (SIGPIPE, SIG_IGN);
1004 fout = popen (local + 1, "w");
1005 if (fout == NULL)
1006 {
1007 error (0, errno, "%s", local + 1);
1008 goto abort;
1009 }
1010 closefunc = pclose;
1011 }
1012 else
1013 {
1014 struct stat st;
1015
1016 fout = fopen (local, lmode);
1017 if (fout == NULL || fstat (fileno (fout), &st) < 0)
1018 {
1019 error (0, errno, "local: %s", local);
1020 goto abort;
1021 }
1022 closefunc = fclose;
1023 blksize = st.st_blksize;
1024 }
1025
1026 if (blksize > bufsize)
1027 {
1028 free (buf);
1029 buf = malloc ((unsigned) blksize);
1030 if (buf == NULL)
1031 {
1032 error (0, errno, "malloc");
1033 bufsize = 0;
1034 goto abort;
1035 }
1036 bufsize = blksize;
1037 }
1038
1039 start = current_timespec ();
1040 switch (curtype)
1041 {
1042
1043 case TYPE_I:
1044 case TYPE_L:
1045 if (restart_point && lseek (fileno (fout), restart_point, SEEK_SET) < 0)
1046 {
1047 error (0, errno, "local: %s", local);
1048 if (closefunc != NULL)
1049 (*closefunc) (fout);
1050 return;
1051 }
1052 errno = d = 0;
1053 while ((c = read (fileno (din), buf, bufsize)) > 0)
1054 {
1055 if ((d = write (fileno (fout), buf, c)) != c)
1056 break;
1057 bytes += c;
1058 if (hash)
1059 {
1060 while (bytes >= local_hashbytes)
1061 {
1062 putchar ('#');
1063 local_hashbytes += hashbytes;
1064 }
1065 fflush (stdout);
1066 }
1067 }
1068
1069 if (hash && bytes > 0)
1070 {
1071 if (bytes < local_hashbytes)
1072 putchar ('#');
1073 putchar ('\n');
1074 fflush (stdout);
1075 }
1076 if (c < 0)
1077 {
1078 if (errno != EPIPE)
1079 error (0, errno, "netin");
1080 bytes = -1;
1081 }
1082 if (d < c)
1083 {
1084 if (d < 0)
1085 error (0, errno, "local: %s", local);
1086 else
1087 error (0, 0, "%s: short write", local);
1088 }
1089 break;
1090
1091 case TYPE_A:
1092 if (restart_point)
1093 {
1094 off_t i, n;
1095 int ch;
1096
1097 errno = 0;
1098
1099 if (fseeko (fout, 0L, SEEK_SET) < 0)
1100 goto done;
1101 n = restart_point;
1102 for (i = 0; i++ < n;)
1103 {
1104 if ((ch = getc (fout)) == EOF)
1105 goto done;
1106 if (ch == '\n')
1107 i++;
1108 }
1109 if (fseeko (fout, 0L, SEEK_CUR) < 0)
1110 {
1111 done:
1112 /* Cancel server's action quickly. */
1113 (void) command ("ABOR");
1114 getreply (0);
1115
1116 /* Explain our failure. */
1117 if (ch == EOF)
1118 printf ("Action not taken: offset %jd is outside of %s.\n",
1119 restart_point, local);
1120 else
1121 error (0, errno, "local: %s", local);
1122
1123 if (closefunc != NULL)
1124 (*closefunc) (fout);
1125 return;
1126 }
1127 }
1128 while ((c = getc (din)) != EOF)
1129 {
1130 if (c == '\n')
1131 bare_lfs++;
1132 while (c == '\r')
1133 {
1134 while (hash && (bytes >= local_hashbytes))
1135 {
1136 putchar ('#');
1137 fflush (stdout);
1138 local_hashbytes += hashbytes;
1139 }
1140 bytes++;
1141 if ((c = getc (din)) != '\n' || tcrflag)
1142 {
1143 if (ferror (fout))
1144 goto break2;
1145 putc ('\r', fout);
1146 if (c == '\0')
1147 {
1148 bytes++;
1149 goto contin2;
1150 }
1151 if (c == EOF)
1152 goto contin2;
1153 }
1154 }
1155 putc (c, fout);
1156 bytes++;
1157 contin2:;
1158 }
1159 break2:
1160 if (bare_lfs)
1161 {
1162 printf ("WARNING! %d bare linefeeds received in ASCII mode\n",
1163 bare_lfs);
1164 printf ("File may not have transferred correctly.\n");
1165 }
1166 if (hash)
1167 {
1168 if (bytes < local_hashbytes)
1169 putchar ('#');
1170 putchar ('\n');
1171 fflush (stdout);
1172 }
1173 if (ferror (din))
1174 {
1175 if (errno != EPIPE)
1176 error (0, errno, "netin");
1177 bytes = -1;
1178 }
1179 if (ferror (fout))
1180 error (0, errno, "local: %s", local);
1181 break;
1182 }
1183 if (closefunc != NULL)
1184 (*closefunc) (fout);
1185 signal (SIGINT, oldintr);
1186 if (oldintp)
1187 signal (SIGPIPE, oldintp);
1188 fclose (din);
1189 stop = current_timespec ();
1190 getreply (0);
1191 if (bytes > 0 && is_retr)
1192 ptransfer ("received", bytes, &start, &stop);
1193 return;
1194 abort:
1195
1196 /* abort using RFC959 recommended IP,SYNC sequence */
1197
1198 if (oldintp)
1199 signal (SIGPIPE, oldintr);
1200 signal (SIGINT, SIG_IGN);
1201 if (!cpend)
1202 {
1203 code = -1;
1204 signal (SIGINT, oldintr);
1205 return;
1206 }
1207
1208 abort_remote (din);
1209 code = -1;
1210 if (data >= 0)
1211 {
1212 close (data);
1213 data = -1;
1214 }
1215 if (closefunc != NULL && fout != NULL)
1216 (*closefunc) (fout);
1217 if (din)
1218 fclose (din);
1219 stop = current_timespec ();
1220 if (bytes > 0)
1221 ptransfer ("received", bytes, &start, &stop);
1222 signal (SIGINT, oldintr);
1223 }
1224
1225 /*
1226 * Need to start a listen on the data channel before we send the command,
1227 * otherwise the server's connect may fail.
1228 */
1229 int
1230 initconn (void)
1231 {
1232 char *p = NULL, *a = NULL;
1233 int result, tmpno = 0;
1234 int good_epsv = 0, good_lpsv = 0, j;
1235 socklen_t len;
1236 int on = 1;
1237 uint32_t a0, a1, a2, a3, p0, p1, port;
1238 uint32_t af, hal, h[16], pal; /* RFC 1639: LPSV response. */
1239 struct sockaddr_in *data_addr_sa4 = (struct sockaddr_in *) &data_addr;
1240 struct sockaddr_in6 *data_addr_sa6 = (struct sockaddr_in6 *) &data_addr;
1241
1242 if (passivemode)
1243 {
1244 data = socket (myctladdr.ss_family, SOCK_STREAM, 0);
1245 if (data < 0)
1246 {
1247 perror ("ftp: socket");
1248 return (1);
1249 }
1250 if ((options & SO_DEBUG) &&
1251 setsockopt (data, SOL_SOCKET, SO_DEBUG, (char *) &on,
1252 sizeof (on)) < 0)
1253 if (errno != EACCES) /* Ignore insufficient permission. */
1254 error (0, errno, "setsockopt DEBUG (ignored)");
1255
1256 /* Be contemporary:
1257 * first try EPSV,
1258 * then fall back to PASV/LPSV.
1259 */
1260 switch (myctladdr.ss_family)
1261 {
1262 case AF_INET:
1263 if (doepsv4 && command ("EPSV") == COMPLETE)
1264 {
1265 good_epsv = 1;
1266 break;
1267 }
1268 if (doepsv4)
1269 {
1270 /* When arriving here, EPSV failed. Prevent new attempts. */
1271 doepsv4 = 0;
1272 }
1273 if (command ("PASV") == COMPLETE)
1274 break;
1275 if (command ("LPSV") == COMPLETE)
1276 {
1277 good_lpsv = 1;
1278 break;
1279 }
1280 printf ("Passive mode refused.\n");
1281 goto bad;
1282 break;
1283 case AF_INET6:
1284 if (command ("EPSV") == COMPLETE)
1285 {
1286 good_epsv = 1;
1287 break;
1288 }
1289 if (command ("LPSV") == COMPLETE)
1290 {
1291 good_lpsv = 1;
1292 break;
1293 }
1294 printf ("Passive mode refused.\n");
1295 goto bad;
1296 break;
1297 }
1298
1299 if (good_epsv)
1300 {
1301 /* EPSV: IPv4 or IPv6
1302 *
1303 * Expected response (perl): pasv =~ '%u|'
1304 * This communicates a port number.
1305 */
1306 if (sscanf (pasv, "%u|", &port) != 1)
1307 {
1308 printf ("Extended passive mode scan failure. "
1309 "Should not happen!\n");
1310 (void) command ("ABOR"); /* Cancel any open connection. */
1311 goto bad;
1312 }
1313 data_addr = hisctladdr;
1314 switch (data_addr.ss_family)
1315 {
1316 case AF_INET:
1317 data_addr_sa4->sin_port = htons (port);
1318 break;
1319 case AF_INET6:
1320 data_addr_sa6->sin6_port = htons (port);
1321 break;
1322 }
1323 } /* EPSV */
1324 else if (good_lpsv)
1325 {
1326 /* LPSV: IPv4 or IPv6
1327 *
1328 * At this point we have got a string of comma
1329 * separated, one-byte unsigned integer values.
1330 * Length and interpretation depends on address
1331 * family.
1332 */
1333
1334 if (myctladdr.ss_family == AF_INET)
1335 {
1336 if ((sscanf (pasv, "%u," /* af */
1337 "%u,%u,%u,%u,%u," /* hal, h[4] */
1338 "%u,%u,%u", /* pal, p0, p1 */
1339 &af, &hal, &h[0], &h[1], &h[2], &h[3], &pal, &p0,
1340 &p1) != 9) || ( /* Strong checking */ af != 4
1341 || hal != 4 || pal != 2))
1342 {
1343 printf ("Passive mode address scan failure. "
1344 "Shouldn't happen!\n");
1345 (void) command ("ABOR"); /* Cancel any open connection. */
1346 goto bad;
1347 }
1348 for (j = 0; j < 4; ++j)
1349 h[j] &= 0xff; /* Mask only the significant bits. */
1350
1351 data_addr.ss_family = AF_INET;
1352 data_addr_sa4->sin_port =
1353 htons (((p0 & 0xff) << 8) | (p1 & 0xff));
1354
1355 {
1356 uint32_t *pu32 =
1357 (uint32_t *) & data_addr_sa4->sin_addr.s_addr;
1358 pu32[0] =
1359 htonl ((h[0] << 24) | (h[1] << 16) | (h[2] << 8) | h[3]);
1360 }
1361 if (data_addr_sa4->sin_addr.s_addr
1362 != ((struct sockaddr_in *) &hisctladdr)->sin_addr.s_addr)
1363 {
1364 printf ("Passive mode address mismatch.\n");
1365 (void) command ("ABOR"); /* Cancel any open connection. */
1366 goto bad;
1367 }
1368 } /* LPSV IPv4 */
1369 else /* IPv6 */
1370 {
1371 if ((sscanf (pasv, "%u," /* af */
1372 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u," /* hal, h[16] */
1373 "%u,%u,%u", /* pal, p0, p1 */
1374 &af, &hal, &h[0], &h[1], &h[2], &h[3], &h[4],
1375 &h[5], &h[6], &h[7], &h[8], &h[9], &h[10], &h[11],
1376 &h[12], &h[13], &h[14], &h[15], &pal, &p0,
1377 &p1) != 21) || ( /* Strong checking */ af != 6
1378 || hal != 16 || pal != 2))
1379 {
1380 printf ("Passive mode address scan failure. "
1381 "Shouldn't happen!\n");
1382 (void) command ("ABOR"); /* Cancel any open connection. */
1383 goto bad;
1384 }
1385 for (j = 0; j < 16; ++j)
1386 h[j] &= 0xff; /* Mask only the significant bits. */
1387
1388 data_addr.ss_family = AF_INET6;
1389 data_addr_sa6->sin6_port =
1390 htons (((p0 & 0xff) << 8) | (p1 & 0xff));
1391
1392 {
1393 uint32_t *pu32 =
1394 (uint32_t *) & data_addr_sa6->sin6_addr.s6_addr;
1395 pu32[0] =
1396 htonl ((h[0] << 24) | (h[1] << 16) | (h[2] << 8) | h[3]);
1397 pu32[1] =
1398 htonl ((h[4] << 24) | (h[5] << 16) | (h[6] << 8) | h[7]);
1399 pu32[2] =
1400 htonl ((h[8] << 24) | (h[9] << 16) | (h[10] << 8) | h[11]);
1401 pu32[3] =
1402 htonl ((h[12] << 24) | (h[13] << 16) | (h[14] << 8) |
1403 h[15]);
1404 }
1405 if (data_addr_sa6->sin6_addr.s6_addr
1406 != ((struct sockaddr_in6 *) &hisctladdr)->sin6_addr.s6_addr)
1407 {
1408 printf ("Passive mode address mismatch.\n");
1409 (void) command ("ABOR"); /* Cancel any open connection. */
1410 goto bad;
1411 }
1412 } /* LPSV IPv6 */
1413 }
1414 else /* !EPSV && !LPSV */
1415 { /* PASV */
1416 if (myctladdr.ss_family == AF_INET)
1417 { /* PASV */
1418 if (sscanf (pasv, "%u,%u,%u,%u,%u,%u",
1419 &a0, &a1, &a2, &a3, &p0, &p1) != 6)
1420 {
1421 printf ("Passive mode address scan failure. "
1422 "Shouldn't happen!\n");
1423 (void) command ("ABOR"); /* Cancel any open connection. */
1424 goto bad;
1425 }
1426 data_addr.ss_family = AF_INET;
1427 data_addr_sa4->sin_addr.s_addr =
1428 htonl ((a0 << 24) | ((a1 & 0xff) << 16)
1429 | ((a2 & 0xff) << 8) | (a3 & 0xff));
1430 data_addr_sa4->sin_port =
1431 htons (((p0 & 0xff) << 8) | (p1 & 0xff));
1432 if (data_addr_sa4->sin_addr.s_addr
1433 != ((struct sockaddr_in *) &hisctladdr)->sin_addr.s_addr)
1434 {
1435 printf ("Passive mode address mismatch.\n");
1436 (void) command ("ABOR"); /* Cancel any open connection. */
1437 goto bad;
1438 }
1439 } /* PASV */
1440 else
1441 {
1442 /* Catch all impossible cases. */
1443 printf
1444 ("Passive mode address scan failure. Shouldn't happen!\n");
1445 goto bad;
1446 }
1447 } /* PASV */
1448
1449 if (connect (data, (struct sockaddr *) &data_addr, ctladdrlen) < 0)
1450 {
1451 perror ("ftp: connect");
1452 goto bad;
1453 }
1454 #if defined IP_TOS && defined IPPROTO_IP && defined IPTOS_THROUGHPUT
1455 on = IPTOS_THROUGHPUT;
1456 if (data_addr.ss_family == AF_INET &&
1457 setsockopt (data, IPPROTO_IP, IP_TOS, (char *) &on,
1458 sizeof (int)) < 0)
1459 perror ("ftp: setsockopt TOS (ignored)");
1460 #endif
1461 return (0);
1462 }
1463
1464 noport:
1465 data_addr = myctladdr;
1466 if (sendport)
1467 /* Let the system pick a port. */
1468 switch (myctladdr.ss_family)
1469 {
1470 case AF_INET:
1471 data_addr_sa4->sin_port = 0;
1472 break;
1473 case AF_INET6:
1474 data_addr_sa6->sin6_port = 0;
1475 break;
1476 }
1477
1478 if (data != -1)
1479 close (data);
1480 data = socket (myctladdr.ss_family, SOCK_STREAM, 0);
1481 if (data < 0)
1482 {
1483 error (0, errno, "socket");
1484 if (tmpno)
1485 sendport = 1;
1486 return (1);
1487 }
1488 if (!sendport)
1489 if (setsockopt (data, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on))
1490 < 0)
1491 {
1492 error (0, errno, "setsockopt (reuse address)");
1493 goto bad;
1494 }
1495 if (bind (data, (struct sockaddr *) &data_addr, ctladdrlen) < 0)
1496 {
1497 error (0, errno, "bind");
1498 goto bad;
1499 }
1500 if (options & SO_DEBUG
1501 && setsockopt (data, SOL_SOCKET, SO_DEBUG,
1502 (char *) &on, sizeof (on)) < 0)
1503 if (errno != EACCES) /* Ignore insufficient permission. */
1504 error (0, errno, "setsockopt DEBUG (ignored)");
1505 len = sizeof (data_addr);
1506 if (getsockname (data, (struct sockaddr *) &data_addr, &len) < 0)
1507 {
1508 error (0, errno, "getsockname");
1509 goto bad;
1510 }
1511 if (listen (data, 1) < 0)
1512 error (0, errno, "listen");
1513 if (sendport)
1514 {
1515 #define UC(b) (((int)b)&0xff)
1516 /* Preferences:
1517 * IPv4: EPRT, PORT, LPRT
1518 * IPv6: EPRT, LPRT
1519 */
1520 result = ERROR; /* For success detection. */
1521 if (data_addr.ss_family != AF_INET || doepsv4)
1522 {
1523 /* Use EPRT mode. */
1524 getnameinfo ((struct sockaddr *) &data_addr, ctladdrlen,
1525 ia, sizeof (ia), portstr, sizeof (portstr),
1526 NI_NUMERICHOST | NI_NUMERICSERV);
1527 result = command ("EPRT |%d|%s|%s|",
1528 (data_addr.ss_family == AF_INET) ? 1 : 2,
1529 ia, portstr);
1530 }
1531
1532 if (data_addr.ss_family == AF_INET && doepsv4 && result != COMPLETE)
1533 /* Do not try EPRT with IPv4 again. It fails for this host. */
1534 doepsv4 = 0;
1535
1536 if (data_addr.ss_family == AF_INET && result != COMPLETE)
1537 {
1538 /* PORT for IPv4; possibly EPRT has failed. */
1539 a = (char *) &data_addr_sa4->sin_addr;
1540 p = (char *) &data_addr_sa4->sin_port;
1541 result = command ("PORT %d,%d,%d,%d,%d,%d",
1542 UC (a[0]), UC (a[1]), UC (a[2]), UC (a[3]),
1543 UC (p[0]), UC (p[1]));
1544 }
1545
1546 if (result != COMPLETE)
1547 {
1548 /* Fall back to LPRT. */
1549 uint8_t *h, *p;
1550
1551 switch (data_addr.ss_family)
1552 {
1553 case AF_INET:
1554 h = (uint8_t *) & data_addr_sa4->sin_addr;
1555 p = (uint8_t *) & data_addr_sa4->sin_port;
1556 result = command ("LPRT 4,4,%u,%u,%u,%u,2,%u,%u",
1557 h[0], h[1], h[2], h[3], p[0], p[1]);
1558 break;
1559 case AF_INET6:
1560 h = (uint8_t *) & data_addr_sa6->sin6_addr;
1561 p = (uint8_t *) & data_addr_sa6->sin6_port;
1562 result = command ("LPRT 6,16," /* af, hal */
1563 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u," /* h[16] */
1564 "2,%u,%u", /* pal, p[2] */
1565 h[0], h[1], h[2], h[3], h[4], h[5], h[6],
1566 h[7], h[8], h[9], h[10], h[11], h[12], h[13],
1567 h[14], h[15], p[0], p[1]);
1568 break;
1569 }
1570 }
1571
1572 if (result == ERROR && sendport == -1)
1573 {
1574 sendport = 0;
1575 tmpno = 1;
1576 goto noport;
1577 }
1578 return (result != COMPLETE);
1579 }
1580 if (tmpno)
1581 sendport = 1;
1582 #if defined IP_TOS && defined IPPROTO_IP && defined IPTOS_THROUGHPUT
1583 on = IPTOS_THROUGHPUT;
1584 if (data_addr.ss_family == AF_INET &&
1585 setsockopt (data, IPPROTO_IP, IP_TOS, (char *) &on, sizeof (int)) < 0)
1586 error (0, errno, "setsockopt TOS (ignored)");
1587 #endif
1588 return (0);
1589 bad:
1590 close (data), data = -1;
1591 if (tmpno)
1592 sendport = 1;
1593 return (1);
1594 }
1595
1596 FILE *
1597 dataconn (char *lmode)
1598 {
1599 struct sockaddr_storage from;
1600 int s, tos;
1601 socklen_t fromlen = sizeof (from);
1602
1603 if (passivemode)
1604 return (fdopen (data, lmode));
1605
1606 s = accept (data, (struct sockaddr *) &from, &fromlen);
1607 if (s < 0)
1608 {
1609 error (0, errno, "accept");
1610 close (data), data = -1;
1611 return (NULL);
1612 }
1613 close (data);
1614 data = s;
1615 #if defined IP_TOS && defined IPPROTO_IP && defined IPTOS_THROUGHPUT
1616 tos = IPTOS_THROUGHPUT;
1617 if (from.ss_family == AF_INET &&
1618 setsockopt (s, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof (int)) < 0)
1619 error (0, errno, "setsockopt TOS (ignored)");
1620 #endif
1621 return (fdopen (data, lmode));
1622 }
1623
1624 void
1625 ptransfer (char *direction, long long int bytes, struct timespec *t0,
1626 struct timespec *t1)
1627 {
1628 if (verbose)
1629 {
1630 struct timespec ts = timespec_sub (*t1, *t0);
1631 double seconds = timespectod (ts);
1632 double bytes_per_second;
1633
1634 /* Don't divide by zero. Can this happen? */
1635 if (seconds == 0.0)
1636 seconds = DBL_MIN;
1637
1638 bytes_per_second = bytes / seconds;
1639
1640 printf ("%lld bytes %s in %.4f seconds", bytes, direction, seconds);
1641
1642 if (bytes_per_second > 1048576.0)
1643 printf (" (%.4f Mbytes/s)\n", bytes_per_second / 1048576.0);
1644 else if (bytes_per_second > 1024.0)
1645 printf (" (%.4f kbytes/s)\n", bytes_per_second / 1024.0);
1646 else
1647 printf (" (%.4f bytes/s)\n", bytes_per_second);
1648 }
1649 }
1650
1651 void
1652 psabort (int sig MAYBE_UNUSED)
1653 {
1654
1655 abrtflag++;
1656 }
1657
1658 void
1659 pswitch (int flag)
1660 {
1661 sighandler_t oldintr;
1662 static struct comvars
1663 {
1664 int connect;
1665 char *name;
1666 struct sockaddr_storage mctl;
1667 struct sockaddr_storage hctl;
1668 FILE *in;
1669 FILE *out;
1670 int tpe;
1671 int curtpe;
1672 int cpnd;
1673 int sunqe;
1674 int runqe;
1675 int mcse;
1676 int ntflg;
1677 char nti[sizeof (ntin)];
1678 char nto[sizeof (ntout)];
1679 int mapflg;
1680 char *mi;
1681 char *mo;
1682 } proxstruct = {
1683 0
1684 }, tmpstruct = {
1685 0
1686 };
1687 struct comvars *ip, *op;
1688
1689 abrtflag = 0;
1690 oldintr = signal (SIGINT, psabort);
1691 if (flag)
1692 {
1693 if (proxy)
1694 return;
1695 ip = &tmpstruct;
1696 op = &proxstruct;
1697 proxy++;
1698 }
1699 else
1700 {
1701 if (!proxy)
1702 return;
1703 ip = &proxstruct;
1704 op = &tmpstruct;
1705 proxy = 0;
1706 }
1707 ip->connect = connected;
1708 connected = op->connect;
1709
1710 free (ip->name);
1711 ip->name = hostname;
1712 hostname = op->name;
1713 op->name = 0;
1714
1715 ip->hctl = hisctladdr;
1716 hisctladdr = op->hctl;
1717 ip->mctl = myctladdr;
1718 myctladdr = op->mctl;
1719 ip->in = cin;
1720 cin = op->in;
1721 ip->out = cout;
1722 cout = op->out;
1723 ip->tpe = type;
1724 type = op->tpe;
1725 ip->curtpe = curtype;
1726 curtype = op->curtpe;
1727 ip->cpnd = cpend;
1728 cpend = op->cpnd;
1729 ip->sunqe = sunique;
1730 sunique = op->sunqe;
1731 ip->runqe = runique;
1732 runique = op->runqe;
1733 ip->mcse = mcase;
1734 mcase = op->mcse;
1735 ip->ntflg = ntflag;
1736 ntflag = op->ntflg;
1737 strncpy (ip->nti, ntin, sizeof (ntin) - 1);
1738 (ip->nti)[sizeof (ntin) - 1] = '\0';
1739 strcpy (ntin, op->nti);
1740 strncpy (ip->nto, ntout, sizeof (ntout) - 1);
1741 (ip->nto)[sizeof (ntout) - 1] = '\0';
1742 strcpy (ntout, op->nto);
1743 ip->mapflg = mapflag;
1744 mapflag = op->mapflg;
1745
1746 free (ip->mi);
1747 ip->mi = mapin;
1748 mapin = op->mi;
1749 op->mi = 0;
1750
1751 free (ip->mo);
1752 ip->mo = mapout;
1753 mapout = op->mo;
1754 op->mo = 0;
1755
1756 signal (SIGINT, oldintr);
1757 if (abrtflag)
1758 {
1759 abrtflag = 0;
1760 (*oldintr) (SIGINT);
1761 }
1762 }
1763
1764 void
1765 abortpt (int sig MAYBE_UNUSED)
1766 {
1767
1768 printf ("\n");
1769 fflush (stdout);
1770 ptabflg++;
1771 mflag = 0;
1772 abrtflag = 0;
1773 longjmp (ptabort, 1);
1774 }
1775
1776 void
1777 proxtrans (char *cmd, char *local, char *remote)
1778 {
1779 sighandler_t oldintr;
1780 int secndflag = 0, prox_type, nfnd;
1781 char *cmd2;
1782 fd_set mask;
1783
1784 if (strcmp (cmd, "RETR"))
1785 cmd2 = "RETR";
1786 else
1787 cmd2 = runique ? "STOU" : "STOR";
1788 if ((prox_type = type) == 0)
1789 {
1790 if (unix_server && unix_proxy)
1791 prox_type = TYPE_I;
1792 else
1793 prox_type = TYPE_A;
1794 }
1795 if (curtype != prox_type)
1796 changetype (prox_type, 1);
1797 if (command ("PASV") != COMPLETE)
1798 {
1799 printf ("proxy server does not support third party transfers.\n");
1800 return;
1801 }
1802 pswitch (0);
1803 if (!connected)
1804 {
1805 printf ("No primary connection\n");
1806 pswitch (1);
1807 code = -1;
1808 return;
1809 }
1810 if (curtype != prox_type)
1811 changetype (prox_type, 1);
1812 if (command ("PORT %s", pasv) != COMPLETE)
1813 {
1814 pswitch (1);
1815 return;
1816 }
1817 if (setjmp (ptabort))
1818 goto abort;
1819 oldintr = signal (SIGINT, abortpt);
1820 if (command ("%s %s", cmd, remote) != PRELIM)
1821 {
1822 signal (SIGINT, oldintr);
1823 pswitch (1);
1824 return;
1825 }
1826 sleep (2);
1827 pswitch (1);
1828 secndflag++;
1829 if (command ("%s %s", cmd2, local) != PRELIM)
1830 goto abort;
1831 ptflag++;
1832 getreply (0);
1833 pswitch (0);
1834 getreply (0);
1835 signal (SIGINT, oldintr);
1836 pswitch (1);
1837 ptflag = 0;
1838 printf ("local: %s remote: %s\n", local, remote);
1839 return;
1840 abort:
1841 signal (SIGINT, SIG_IGN);
1842 ptflag = 0;
1843 if (strcmp (cmd, "RETR") && !proxy)
1844 pswitch (1);
1845 else if (!strcmp (cmd, "RETR") && proxy)
1846 pswitch (0);
1847 if (!cpend && !secndflag)
1848 { /* only here if cmd = "STOR" (proxy=1) */
1849 if (command ("%s %s", cmd2, local) != PRELIM)
1850 {
1851 pswitch (0);
1852 if (cpend)
1853 abort_remote ((FILE *) NULL);
1854 }
1855 pswitch (1);
1856 if (ptabflg)
1857 code = -1;
1858 signal (SIGINT, oldintr);
1859 return;
1860 }
1861 if (cpend)
1862 abort_remote ((FILE *) NULL);
1863 pswitch (!proxy);
1864 if (!cpend && !secndflag)
1865 { /* only if cmd = "RETR" (proxy=1) */
1866 if (command ("%s %s", cmd2, local) != PRELIM)
1867 {
1868 pswitch (0);
1869 if (cpend)
1870 abort_remote ((FILE *) NULL);
1871 pswitch (1);
1872 if (ptabflg)
1873 code = -1;
1874 signal (SIGINT, oldintr);
1875 return;
1876 }
1877 }
1878 if (cpend)
1879 abort_remote ((FILE *) NULL);
1880 pswitch (!proxy);
1881 if (cpend)
1882 {
1883 FD_ZERO (&mask);
1884 FD_SET (fileno (cin), &mask);
1885 if ((nfnd = empty (&mask, 10)) <= 0)
1886 {
1887 if (nfnd < 0)
1888 {
1889 error (0, errno, "abort");
1890 }
1891 if (ptabflg)
1892 code = -1;
1893 lostpeer (0);
1894 }
1895 getreply (0);
1896 getreply (0);
1897 }
1898 if (proxy)
1899 pswitch (0);
1900 pswitch (1);
1901 if (ptabflg)
1902 code = -1;
1903 signal (SIGINT, oldintr);
1904 }
1905
1906 void
1907 reset (int argc MAYBE_UNUSED, char **argv MAYBE_UNUSED)
1908 {
1909 fd_set mask;
1910 int nfnd = 1;
1911
1912 FD_ZERO (&mask);
1913 while (nfnd > 0)
1914 {
1915 FD_SET (fileno (cin), &mask);
1916 if ((nfnd = empty (&mask, 0)) < 0)
1917 {
1918 error (0, errno, "reset");
1919 code = -1;
1920 lostpeer (0);
1921 }
1922 else if (nfnd)
1923 {
1924 getreply (0);
1925 }
1926 }
1927 }
1928
1929 char *
1930 gunique (char *local)
1931 {
1932 static char *new = 0;
1933 char *cp;
1934 int count = 0;
1935 char ext = '1';
1936
1937 free (new);
1938 new = malloc (strlen (local) + 1 + 3 + 1); /* '.' + 100 + '\0' */
1939 if (!new)
1940 {
1941 printf ("gunique: malloc failed.\n");
1942 return 0;
1943 }
1944 strcpy (new, local);
1945
1946 cp = new + strlen (new);
1947 *cp++ = '.';
1948 for (;;)
1949 {
1950 struct stat st;
1951
1952 if (++count == 100)
1953 {
1954 printf ("runique: can't find unique file name.\n");
1955 return ((char *) 0);
1956 }
1957 *cp++ = ext;
1958 *cp = '\0';
1959 if (ext == '9')
1960 ext = '0';
1961 else
1962 ext++;
1963
1964 if (stat (new, &st) != 0)
1965 {
1966 if (errno == ENOENT)
1967 return new;
1968 else
1969 return 0;
1970 }
1971
1972 if (ext != '0')
1973 cp--;
1974 else if (*(cp - 2) == '.')
1975 *(cp - 1) = '1';
1976 else
1977 {
1978 *(cp - 2) = *(cp - 2) + 1;
1979 cp--;
1980 }
1981 }
1982 }
1983
1984 void
1985 abort_remote (FILE *din)
1986 {
1987 char buf[BUFSIZ];
1988 int nfnd;
1989 fd_set mask;
1990
1991 /*
1992 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1993 * after urgent byte rather than before as is protocol now
1994 */
1995 sprintf (buf, "%c%c%c", IAC, IP, IAC);
1996 if (send (fileno (cout), buf, 3, MSG_OOB) != 3)
1997 error (0, errno, "abort");
1998 fprintf (cout, "%cABOR\r\n", DM);
1999 fflush (cout);
2000 FD_ZERO (&mask);
2001 FD_SET (fileno (cin), &mask);
2002 if (din)
2003 {
2004 FD_SET (fileno (din), &mask);
2005 }
2006 if ((nfnd = empty (&mask, 10)) <= 0)
2007 {
2008 if (nfnd < 0)
2009 {
2010 error (0, errno, "abort");
2011 }
2012 if (ptabflg)
2013 code = -1;
2014 lostpeer (0);
2015 }
2016 if (din && FD_ISSET (fileno (din), &mask))
2017 {
2018 while (read (fileno (din), buf, sizeof (buf)) > 0)
2019 /* LOOP */ ;
2020 }
2021 if (getreply (0) == ERROR && code == 552)
2022 {
2023 /* 552 needed for nic style abort */
2024 getreply (0);
2025 }
2026 getreply (0);
2027 }