1 /* $FreeBSD: src/usr.bin/ftp/ftp.c,v 1.28.2.5 2002/07/25 15:29:18 ume Exp $ */
2 /* $DragonFly: src/usr.bin/ftp/Attic/ftp.c,v 1.4 2003/11/03 19:31:29 eirikn Exp $ */
3 /* $NetBSD: ftp.c,v 1.29.2.1 1997/11/18 01:01:04 mellon Exp $ */
6 * Copyright (c) 1985, 1989, 1993, 1994
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)ftp.c 8.6 (Berkeley) 10/27/94
38 * $NetBSD: ftp.c,v 1.29.2.1 1997/11/18 01:01:04 mellon Exp $
39 * $FreeBSD: src/usr.bin/ftp/ftp.c,v 1.28.2.5 2002/07/25 15:29:18 ume Exp $
42 #include <sys/cdefs.h>
44 #include <sys/types.h>
46 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <arpa/inet.h>
54 #include <arpa/telnet.h>
86 struct sockaddr_in su_sin;
87 struct sockaddr_in6 su_sin6;
89 #define su_len su_si.si_len
90 #define su_family su_si.si_family
91 #define su_port su_si.si_port
93 union sockunion myctladdr, hisctladdr, data_addr;
96 hookup(const char *host0, char *port)
98 int s, len, tos, error;
99 struct addrinfo hints, *res, *res0;
100 static char hostnamebuf[MAXHOSTNAMELEN];
103 if (*host0 == '[' && strrchr(host0, ']') != NULL) { /*IPv6 addr in []*/
104 strncpy(hostnamebuf, host0 + 1, strlen(host0) - 2);
105 hostnamebuf[strlen(host0) - 2] = '\0';
107 strncpy(hostnamebuf, host0, strlen(host0));
108 hostnamebuf[strlen(host0)] = '\0';
111 memset(&hints, 0, sizeof(hints));
112 hints.ai_flags = AI_CANONNAME;
113 hints.ai_family = family;
114 hints.ai_socktype = SOCK_STREAM;
115 hints.ai_protocol = 0;
116 error = getaddrinfo(host, port, &hints, &res0);
118 warnx("%s: %s", host, gai_strerror(error));
119 if (error == EAI_SYSTEM)
120 warnx("%s: %s", host, strerror(errno));
126 if (res->ai_canonname)
127 (void) strncpy(hostnamebuf, res->ai_canonname,
128 sizeof(hostnamebuf));
129 hostname = hostnamebuf;
132 * make sure that ai_addr is NOT an IPv4 mapped address.
133 * IPv4 mapped address complicates too many things in FTP
134 * protocol handling, as FTP protocol is defined differently
135 * between IPv4 and IPv6.
138 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
148 struct addrinfo *bindres;
151 for (bindres = bindres0;
153 bindres = bindres->ai_next)
154 if (bindres->ai_family == res->ai_family)
158 binderr = bind(s, bindres->ai_addr,
159 bindres->ai_addrlen);
172 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
175 char hname[NI_MAXHOST];
176 getnameinfo(res->ai_addr, res->ai_addrlen,
177 hname, sizeof(hname) - 1, NULL, 0,
179 warn("connect to address %s", hname);
181 getnameinfo(res->ai_addr, res->ai_addrlen,
182 hname, sizeof(hname) - 1, NULL, 0,
184 printf("Trying %s...\n", hname);
192 memcpy(&hisctladdr, res->ai_addr, res->ai_addrlen);
194 len = sizeof(myctladdr);
195 if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
201 if (myctladdr.su_family == AF_INET)
203 tos = IPTOS_LOWDELAY;
204 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
205 warn("setsockopt TOS (ignored)");
208 cin = fdopen(s, "r");
209 cout = fdopen(s, "w");
210 if (cin == NULL || cout == NULL) {
211 warnx("fdopen failed.");
220 printf("Connected to %s.\n", hostname);
221 if (getreply(0) > 2) { /* read startup message from server */
233 if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
238 #endif /* SO_OOBINLINE */
247 cmdabort(int notused)
252 (void)fflush(stdout);
260 command(const char *fmt, ...)
267 fputs("---> ", stdout);
269 if (strncmp("PASS ", fmt, 5) == 0)
270 fputs("PASS XXXX", stdout);
271 else if (strncmp("ACCT ", fmt, 5) == 0)
272 fputs("ACCT XXXX", stdout);
277 (void)fflush(stdout);
280 warnx("No control connection for command.");
284 oldintr = signal(SIGINT, cmdabort);
286 vfprintf(cout, fmt, ap);
291 r = getreply(!strcmp(fmt, "QUIT"));
292 if (abrtflag && oldintr != SIG_IGN)
294 (void)signal(SIGINT, oldintr);
298 char reply_string[BUFSIZ]; /* first line of previous reply */
301 getreply(int expecteof)
303 char current_line[BUFSIZ]; /* last line of previous reply */
306 int originalcode = 0, continuation = 0;
309 char *cp, *pt = pasv;
311 oldintr = signal(SIGINT, cmdabort);
312 for (line = 0 ;; line++) {
315 while ((c = getc(cin)) != '\n') {
316 if (c == IAC) { /* handle telnet commands */
317 switch (c = getc(cin)) {
321 fprintf(cout, "%c%c%c", IAC, DONT, c);
327 fprintf(cout, "%c%c%c", IAC, WONT, c);
338 (void)signal(SIGINT, oldintr);
345 "421 Service not available, remote server has closed connection.");
346 (void)fflush(stdout);
351 if (c != '\r' && (verbose > 0 ||
352 (verbose > -1 && n == '5' && dig > 4))) {
354 (dig == 1 || (dig == 5 && verbose == 0)))
355 printf("%s:", hostname);
358 if (dig < 4 && isdigit((unsigned char)c))
359 code = code * 10 + (c - '0');
362 if (code == 227 || code == 228) {
363 /* result for PASV/LPSV */
366 } else if (code == 229) {
367 /* result for EPSV */
373 if (!(dig > 4 && isdigit((unsigned char)c)))
378 if (c != '\r' && c != ')' &&
379 pt < &pasv[sizeof(pasv)-1])
387 if (dig > 4 && c == '(')
391 if (dig == 4 && c == '-') {
398 if (cp < ¤t_line[sizeof(current_line) - 1])
401 if (verbose > 0 || (verbose > -1 && n == '5')) {
403 (void)fflush (stdout);
406 size_t len = cp - current_line;
408 if (len > sizeof(reply_string))
409 len = sizeof(reply_string);
411 (void)strncpy(reply_string, current_line, len);
412 reply_string[len] = '\0';
414 if (continuation && code != originalcode) {
415 if (originalcode == 0)
422 (void)signal(SIGINT, oldintr);
423 if (code == 421 || originalcode == 421)
425 if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
432 empty(fd_set *mask, int sec)
436 t.tv_sec = (long) sec;
438 return (select(32, mask, (fd_set *) 0, (fd_set *) 0, &t));
444 abortsend(int notused)
450 puts("\nsend aborted\nwaiting for remote to finish abort.");
451 (void)fflush(stdout);
452 longjmp(sendabort, 1);
456 sendrequest(const char *cmd, const char *local, const char *remote,
462 int (*closefunc)(FILE *);
463 sig_t oldinti, oldintr, oldintp;
464 volatile off_t hashbytes;
465 char *lmode, buf[BUFSIZ], *bufp;
468 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
483 oprogress = progress;
484 if (verbose && printnames) {
485 if (local && *local != '-')
486 printf("local: %s ", local);
488 printf("remote: %s\n", remote);
491 proxtrans(cmd, local, remote);
501 if (setjmp(sendabort)) {
510 (void)signal(SIGINT, oldintr);
512 (void)signal(SIGPIPE, oldintp);
514 (void)signal(SIGINFO, oldinti);
518 oldintr = signal(SIGINT, abortsend);
519 oldinti = signal(SIGINFO, psummary);
520 if (strcmp(local, "-") == 0) {
523 } else if (*local == '|') {
524 oldintp = signal(SIGPIPE, SIG_IGN);
525 fin = popen(local + 1, "r");
527 warn("%s", local + 1);
528 (void)signal(SIGINT, oldintr);
529 (void)signal(SIGPIPE, oldintp);
530 (void)signal(SIGINFO, oldinti);
537 fin = fopen(local, "r");
539 warn("local: %s", local);
540 (void)signal(SIGINT, oldintr);
541 (void)signal(SIGINFO, oldinti);
546 if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) {
547 printf("%s: not a plain file.\n", local);
548 (void)signal(SIGINT, oldintr);
549 (void)signal(SIGINFO, oldinti);
554 filesize = st.st_size;
557 (void)signal(SIGINT, oldintr);
558 (void)signal(SIGINFO, oldinti);
560 (void)signal(SIGPIPE, oldintp);
562 if (closefunc != NULL)
566 if (setjmp(sendabort))
570 (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
576 rc = fseek(fin, (long) restart_point, SEEK_SET);
580 rc = lseek(fileno(fin), restart_point, SEEK_SET);
584 warn("local: %s", local);
585 if (closefunc != NULL)
589 if (command("REST %qd", (long long) restart_point) !=
591 if (closefunc != NULL)
598 if (command("%s %s", cmd, remote) != PRELIM) {
599 (void)signal(SIGINT, oldintr);
600 (void)signal(SIGINFO, oldinti);
602 (void)signal(SIGPIPE, oldintp);
603 if (closefunc != NULL)
608 if (command("%s", cmd) != PRELIM) {
609 (void)signal(SIGINT, oldintr);
610 (void)signal(SIGINFO, oldinti);
612 (void)signal(SIGPIPE, oldintp);
613 if (closefunc != NULL)
617 dout = dataconn(lmode);
621 oldintp = signal(SIGPIPE, SIG_IGN);
627 while ((c = read(fileno(fin), buf, sizeof(buf))) > 0) {
629 for (bufp = buf; c > 0; c -= d, bufp += d)
630 if ((d = write(fileno(dout), bufp, c)) <= 0)
632 if (hash && (!progress || filesize < 0) ) {
633 while (bytes >= hashbytes) {
637 (void)fflush(stdout);
640 if (hash && (!progress || filesize < 0) && bytes > 0) {
644 (void)fflush(stdout);
647 warn("local: %s", local);
656 while ((c = getc(fin)) != EOF) {
658 while (hash && (!progress || filesize < 0) &&
659 (bytes >= hashbytes)) {
661 (void)fflush(stdout);
666 (void)putc('\r', dout);
671 #if 0 /* this violates RFC */
673 (void)putc('\0', dout);
678 if (hash && (!progress || filesize < 0)) {
679 if (bytes < hashbytes)
682 (void)fflush(stdout);
685 warn("local: %s", local);
694 if (closefunc != NULL)
698 (void)signal(SIGINT, oldintr);
699 (void)signal(SIGINFO, oldinti);
701 (void)signal(SIGPIPE, oldintp);
706 (void)signal(SIGINT, oldintr);
707 (void)signal(SIGINFO, oldinti);
709 (void)signal(SIGPIPE, oldintp);
722 if (closefunc != NULL && fin != NULL)
727 progress = oprogress;
734 abortrecv(int notused)
740 puts("\nreceive aborted\nwaiting for remote to finish abort.");
741 (void)fflush(stdout);
742 longjmp(recvabort, 1);
746 recvrequest(const char *cmd, const char *local, const char *remote,
747 const char *lmode, int printnames, int ignorespecial)
750 int (*closefunc)(FILE *);
751 sig_t oldinti, oldintr, oldintp;
753 volatile int is_retr, tcrflag, bare_lfs;
754 static size_t bufsize;
756 volatile off_t hashbytes;
759 struct timeval tval[2];
763 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
777 direction = "received";
781 oprogress = progress;
782 opreserve = preserve;
783 is_retr = (strcmp(cmd, "RETR") == 0);
784 if (is_retr && verbose && printnames) {
785 if (local && (ignorespecial || *local != '-'))
786 printf("local: %s ", local);
788 printf("remote: %s\n", remote);
790 if (proxy && is_retr) {
791 proxtrans(cmd, local, remote);
797 tcrflag = !crflag && is_retr;
798 if (setjmp(recvabort)) {
807 (void)signal(SIGINT, oldintr);
809 (void)signal(SIGINFO, oldinti);
810 progress = oprogress;
811 preserve = opreserve;
815 oldintr = signal(SIGINT, abortrecv);
816 oldinti = signal(SIGINFO, psummary);
817 if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
818 if (access(local, W_OK) < 0) {
819 char *dir = strrchr(local, '/');
821 if (errno != ENOENT && errno != EACCES) {
822 warn("local: %s", local);
823 (void)signal(SIGINT, oldintr);
824 (void)signal(SIGINFO, oldinti);
830 d = access(dir == local ? "/" : dir ? local : ".", W_OK);
834 warn("local: %s", local);
835 (void)signal(SIGINT, oldintr);
836 (void)signal(SIGINFO, oldinti);
840 if (!runique && errno == EACCES &&
841 chmod(local, 0600) < 0) {
842 warn("local: %s", local);
843 (void)signal(SIGINT, oldintr);
844 (void)signal(SIGINFO, oldinti);
848 if (runique && errno == EACCES &&
849 (local = gunique(local)) == NULL) {
850 (void)signal(SIGINT, oldintr);
851 (void)signal(SIGINFO, oldinti);
856 else if (runique && (local = gunique(local)) == NULL) {
857 (void)signal(SIGINT, oldintr);
858 (void)signal(SIGINFO, oldinti);
864 if (curtype != TYPE_A)
865 changetype(TYPE_A, 0);
869 filesize = remotesize(remote, 0);
872 (void)signal(SIGINT, oldintr);
873 (void)signal(SIGINFO, oldinti);
877 if (setjmp(recvabort))
879 if (is_retr && restart_point &&
880 command("REST %qd", (long long) restart_point) != CONTINUE)
883 if (command("%s %s", cmd, remote) != PRELIM) {
884 (void)signal(SIGINT, oldintr);
885 (void)signal(SIGINFO, oldinti);
889 if (command("%s", cmd) != PRELIM) {
890 (void)signal(SIGINT, oldintr);
891 (void)signal(SIGINFO, oldinti);
898 if (!ignorespecial && strcmp(local, "-") == 0) {
902 } else if (!ignorespecial && *local == '|') {
903 oldintp = signal(SIGPIPE, SIG_IGN);
904 fout = popen(local + 1, "w");
913 fout = fopen(local, lmode);
915 warn("local: %s", local);
920 if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
921 st.st_blksize = BUFSIZ;
922 if (st.st_blksize > bufsize) {
925 buf = malloc((unsigned)st.st_blksize);
931 bufsize = st.st_blksize;
933 if (!S_ISREG(st.st_mode)) {
942 if (is_retr && restart_point &&
943 lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
944 warn("local: %s", local);
945 progress = oprogress;
946 preserve = opreserve;
947 if (closefunc != NULL)
952 while ((c = read(fileno(din), buf, bufsize)) > 0) {
953 if ((d = write(fileno(fout), buf, c)) != c)
956 if (hash && (!progress || filesize < 0)) {
957 while (bytes >= hashbytes) {
961 (void)fflush(stdout);
964 if (hash && (!progress || filesize < 0) && bytes > 0) {
968 (void)fflush(stdout);
977 warn("local: %s", local);
979 warnx("%s: short write", local);
984 if (is_retr && restart_point) {
988 if (fseek(fout, 0L, SEEK_SET) < 0)
990 n = (long)restart_point;
991 for (i = 0; i++ < n;) {
992 if ((ch = getc(fout)) == EOF)
997 if (fseek(fout, 0L, SEEK_CUR) < 0) {
999 warn("local: %s", local);
1000 progress = oprogress;
1001 preserve = opreserve;
1002 if (closefunc != NULL)
1007 while ((c = getc(din)) != EOF) {
1011 while (hash && (!progress || filesize < 0) &&
1012 (bytes >= hashbytes)) {
1014 (void)fflush(stdout);
1018 if ((c = getc(din)) != '\n' || tcrflag) {
1021 (void)putc('\r', fout);
1030 (void)putc(c, fout);
1037 "WARNING! %d bare linefeeds received in ASCII mode.\n", bare_lfs);
1038 puts("File may not have transferred correctly.");
1040 if (hash && (!progress || filesize < 0)) {
1041 if (bytes < hashbytes)
1043 (void)putchar('\n');
1044 (void)fflush(stdout);
1052 warn("local: %s", local);
1056 progress = oprogress;
1057 preserve = opreserve;
1058 if (closefunc != NULL)
1060 (void)signal(SIGINT, oldintr);
1061 (void)signal(SIGINFO, oldinti);
1063 (void)signal(SIGPIPE, oldintp);
1066 if (bytes >= 0 && is_retr) {
1069 if (preserve && (closefunc == fclose)) {
1070 mtime = remotemodtime(remote, 0);
1072 (void)gettimeofday(&tval[0],
1073 (struct timezone *)0);
1074 tval[1].tv_sec = mtime;
1075 tval[1].tv_usec = 0;
1076 if (utimes(local, tval) == -1) {
1078 "Can't change modification time on %s to %s",
1079 local, asctime(localtime(&mtime)));
1088 /* abort using RFC959 recommended IP,SYNC sequence */
1090 progress = oprogress;
1091 preserve = opreserve;
1093 (void)signal(SIGPIPE, oldintp);
1094 (void)signal(SIGINT, SIG_IGN);
1097 (void)signal(SIGINT, oldintr);
1098 (void)signal(SIGINFO, oldinti);
1108 if (closefunc != NULL && fout != NULL)
1114 (void)signal(SIGINT, oldintr);
1115 (void)signal(SIGINFO, oldinti);
1119 * Need to start a listen on the data channel before we send the command,
1120 * otherwise the server's connect may fail.
1126 int result, len, tmpno = 0;
1135 if (myctladdr.su_family == AF_INET6
1136 && (IN6_IS_ADDR_LINKLOCAL(&myctladdr.su_sin6.sin6_addr)
1137 || IN6_IS_ADDR_SITELOCAL(&myctladdr.su_sin6.sin6_addr))) {
1138 warnx("use of scoped address can be troublesome");
1143 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
1146 data_addr = myctladdr;
1147 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1153 struct addrinfo *bindres;
1156 for (bindres = bindres0;
1158 bindres = bindres->ai_next)
1159 if (bindres->ai_family == data_addr.su_family)
1161 if (bindres == NULL)
1163 binderr = bind(data, bindres->ai_addr,
1164 bindres->ai_addrlen);
1171 if ((options & SO_DEBUG) &&
1172 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1174 warn("setsockopt (ignored)");
1175 switch (data_addr.su_family) {
1183 result = command(pasvcmd = "EPSV");
1185 if (code / 10 == 22 && code != 229) {
1186 puts("wrong server: EPSV return code must be 229");
1187 result = COMPLETE + 1;
1190 result = COMPLETE + 1;
1191 if (result != COMPLETE) {
1193 result = command(pasvcmd = "PASV");
1198 result = command(pasvcmd = "EPSV");
1199 if (code / 10 == 22 && code != 229) {
1200 puts("wrong server: EPSV return code must be 229");
1201 result = COMPLETE + 1;
1203 if (result != COMPLETE)
1204 result = command(pasvcmd = "LPSV");
1208 result = COMPLETE + 1;
1210 if (result != COMPLETE) {
1211 puts("Passive mode refused.");
1215 #define pack2(var, offset) \
1216 (((var[(offset) + 0] & 0xff) << 8) | ((var[(offset) + 1] & 0xff) << 0))
1217 #define pack4(var, offset) \
1218 (((var[(offset) + 0] & 0xff) << 24) | ((var[(offset) + 1] & 0xff) << 16) \
1219 | ((var[(offset) + 2] & 0xff) << 8) | ((var[(offset) + 3] & 0xff) << 0))
1221 * What we've got at this point is a string of comma
1222 * separated one-byte unsigned integer values.
1224 * The first four are the an IP address. The fifth is
1225 * the MSB of the port number, the sixth is the LSB.
1226 * From that we'll prepare a sockaddr_in.
1227 * In other case, the format is more complicated.
1229 if (strcmp(pasvcmd, "PASV") == 0) {
1230 if (code / 10 == 22 && code != 227) {
1231 puts("wrong server: return code must be 227");
1235 error = sscanf(pasv, "%d,%d,%d,%d,%d,%d",
1236 &h[0], &h[1], &h[2], &h[3],
1240 data_addr.su_sin.sin_addr.s_addr =
1244 } else if (strcmp(pasvcmd, "LPSV") == 0) {
1245 if (code / 10 == 22 && code != 228) {
1246 puts("wrong server: return code must be 228");
1250 switch (data_addr.su_family) {
1252 error = sscanf(pasv,
1253 "%d,%d,%d,%d,%d,%d,%d,%d,%d",
1255 &h[0], &h[1], &h[2], &h[3],
1256 &pal, &prt[0], &prt[1]);
1257 if (error == 9 && af == 4 && hal == 4 && pal == 2) {
1259 data_addr.su_sin.sin_addr.s_addr =
1266 error = sscanf(pasv,
1267 "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1269 &h[0], &h[1], &h[2], &h[3],
1270 &h[4], &h[5], &h[6], &h[7],
1271 &h[8], &h[9], &h[10], &h[11],
1272 &h[12], &h[13], &h[14], &h[15],
1273 &pal, &prt[0], &prt[1]);
1274 if (error != 21 || af != 6 || hal != 16 || pal != 2) {
1282 p32 = (u_int32_t *)&data_addr.su_sin6.sin6_addr;
1283 p32[0] = htonl(pack4(h, 0));
1284 p32[1] = htonl(pack4(h, 4));
1285 p32[2] = htonl(pack4(h, 8));
1286 p32[3] = htonl(pack4(h, 12));
1293 } else if (strcmp(pasvcmd, "EPSV") == 0) {
1297 if (code / 10 == 22 && code != 229) {
1298 puts("wrong server: return code must be 229");
1302 error = sscanf(pasv, "%c%c%c%d%c",
1303 &delim[0], &delim[1], &delim[2],
1304 &prt[1], &delim[3]);
1309 if (delim[0] != delim[1] || delim[0] != delim[2]
1310 || delim[0] != delim[3]) {
1315 data_addr = hisctladdr;
1317 prt[0] = (prt[1] & 0xff00) >> 8;
1326 "Passive mode address scan failure. Shouldn't happen!");
1330 data_addr.su_port = htons(pack2(prt, 0));
1332 if (connect(data, (struct sockaddr *)&data_addr,
1333 data_addr.su_len) < 0) {
1338 if (data_addr.su_family == AF_INET)
1340 on = IPTOS_THROUGHPUT;
1341 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1343 warn("setsockopt TOS (ignored)");
1350 data_addr = myctladdr;
1352 data_addr.su_port = 0; /* let system pick one */
1355 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1363 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
1365 warn("setsockopt (reuse address)");
1369 if (data_addr.su_family == AF_INET)
1372 ports = restricted_data_ports ? IP_PORTRANGE_HIGH : IP_PORTRANGE_DEFAULT;
1373 if (setsockopt(data, IPPROTO_IP, IP_PORTRANGE, (char *)&ports,
1375 warn("setsockopt PORTRANGE (ignored)");
1379 #ifdef IPV6_PORTRANGE
1380 if (data_addr.su_family == AF_INET6) {
1381 ports = restricted_data_ports ? IPV6_PORTRANGE_HIGH
1382 : IPV6_PORTRANGE_DEFAULT;
1383 if (setsockopt(data, IPPROTO_IPV6, IPV6_PORTRANGE,
1384 (char *)&ports, sizeof(ports)) < 0)
1385 warn("setsockopt PORTRANGE (ignored)");
1389 if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {
1393 if (options & SO_DEBUG &&
1394 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1396 warn("setsockopt (ignored)");
1397 len = sizeof(data_addr);
1398 if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1399 warn("getsockname");
1402 if (listen(data, 1) < 0)
1405 char hname[INET6_ADDRSTRLEN];
1407 struct sockaddr_in data_addr4;
1408 union sockunion *daddr;
1411 if (data_addr.su_family == AF_INET6 &&
1412 IN6_IS_ADDR_V4MAPPED(&data_addr.su_sin6.sin6_addr)) {
1413 memset(&data_addr4, 0, sizeof(data_addr4));
1414 data_addr4.sin_len = sizeof(struct sockaddr_in);
1415 data_addr4.sin_family = AF_INET;
1416 data_addr4.sin_port = data_addr.su_port;
1417 memcpy((caddr_t)&data_addr4.sin_addr,
1418 (caddr_t)&data_addr.su_sin6.sin6_addr.s6_addr[12],
1419 sizeof(struct in_addr));
1420 daddr = (union sockunion *)&data_addr4;
1427 #define UC(b) (((int)b)&0xff)
1429 switch (daddr->su_family) {
1432 af = (daddr->su_family == AF_INET) ? 1 : 2;
1433 if (daddr->su_family == AF_INET6)
1434 daddr->su_sin6.sin6_scope_id = 0;
1435 if (getnameinfo((struct sockaddr *)daddr,
1436 daddr->su_len, hname,
1437 sizeof(hname) - 1, NULL, 0,
1441 result = command("EPRT |%d|%s|%d|",
1442 af, hname, ntohs(daddr->su_port));
1447 result = COMPLETE + 1;
1450 if (result == COMPLETE)
1453 p = (char *)&daddr->su_port;
1454 switch (daddr->su_family) {
1456 a = (char *)&daddr->su_sin.sin_addr;
1457 result = command("PORT %d,%d,%d,%d,%d,%d",
1458 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
1459 UC(p[0]), UC(p[1]));
1463 a = (char *)&daddr->su_sin6.sin6_addr;
1465 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1467 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
1468 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
1469 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
1470 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
1471 2, UC(p[0]), UC(p[1]));
1475 result = COMPLETE + 1; /* xxx */
1479 if (result == ERROR && sendport == -1) {
1484 return (result != COMPLETE);
1489 if (data_addr.su_family == AF_INET)
1491 on = IPTOS_THROUGHPUT;
1492 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1493 warn("setsockopt TOS (ignored)");
1498 (void)close(data), data = -1;
1505 dataconn(const char *lmode)
1507 union sockunion from;
1508 int s, fromlen, tos;
1510 fromlen = myctladdr.su_len;
1513 return (fdopen(data, lmode));
1515 s = accept(data, (struct sockaddr *) &from, &fromlen);
1518 (void)close(data), data = -1;
1524 if (data_addr.su_family == AF_INET)
1526 tos = IPTOS_THROUGHPUT;
1527 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
1528 warn("setsockopt TOS (ignored)");
1531 return (fdopen(data, lmode));
1535 psummary(int notused)
1543 psabort(int notused)
1554 static struct comvars {
1556 char name[MAXHOSTNAMELEN];
1557 union sockunion mctl;
1558 union sockunion hctl;
1571 char mi[MAXPATHLEN];
1572 char mo[MAXPATHLEN];
1573 } proxstruct, tmpstruct;
1574 struct comvars *ip, *op;
1577 oldintr = signal(SIGINT, psabort);
1591 ip->connect = connected;
1592 connected = op->connect;
1594 (void)strncpy(ip->name, hostname, sizeof(ip->name) - 1);
1595 ip->name[sizeof(ip->name) - 1] = '\0';
1598 hostname = op->name;
1599 ip->hctl = hisctladdr;
1600 hisctladdr = op->hctl;
1601 ip->mctl = myctladdr;
1602 myctladdr = op->mctl;
1609 ip->curtpe = curtype;
1610 curtype = op->curtpe;
1613 ip->sunqe = sunique;
1614 sunique = op->sunqe;
1615 ip->runqe = runique;
1616 runique = op->runqe;
1621 (void)strncpy(ip->nti, ntin, sizeof(ip->nti) - 1);
1622 (ip->nti)[sizeof(ip->nti) - 1] = '\0';
1623 (void)strcpy(ntin, op->nti);
1624 (void)strncpy(ip->nto, ntout, sizeof(ip->nto) - 1);
1625 (ip->nto)[sizeof(ip->nto) - 1] = '\0';
1626 (void)strcpy(ntout, op->nto);
1627 ip->mapflg = mapflag;
1628 mapflag = op->mapflg;
1629 (void)strncpy(ip->mi, mapin, sizeof(ip->mi) - 1);
1630 (ip->mi)[sizeof(ip->mi) - 1] = '\0';
1631 (void)strcpy(mapin, op->mi);
1632 (void)strncpy(ip->mo, mapout, sizeof(ip->mo) - 1);
1633 (ip->mo)[sizeof(ip->mo) - 1] = '\0';
1634 (void)strcpy(mapout, op->mo);
1635 (void)signal(SIGINT, oldintr);
1643 abortpt(int notused)
1648 (void)fflush(stdout);
1652 longjmp(ptabort, 1);
1656 proxtrans(const char *cmd, const char *local, const char *remote)
1659 int prox_type, nfnd;
1660 volatile int secndflag;
1664 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
1671 if (strcmp(cmd, "RETR"))
1674 cmd2 = runique ? "STOU" : "STOR";
1675 if ((prox_type = type) == 0) {
1676 if (unix_server && unix_proxy)
1681 if (curtype != prox_type)
1682 changetype(prox_type, 1);
1683 if (try_epsv && command("EPSV") != COMPLETE)
1685 if (!try_epsv && command("PASV") != COMPLETE) {
1686 puts("proxy server does not support third party transfers.");
1691 puts("No primary connection.");
1696 if (curtype != prox_type)
1697 changetype(prox_type, 1);
1698 if (command("PORT %s", pasv) != COMPLETE) {
1702 if (setjmp(ptabort))
1704 oldintr = signal(SIGINT, abortpt);
1705 if (command("%s %s", cmd, remote) != PRELIM) {
1706 (void)signal(SIGINT, oldintr);
1713 if (command("%s %s", cmd2, local) != PRELIM)
1719 (void)signal(SIGINT, oldintr);
1722 printf("local: %s remote: %s\n", local, remote);
1725 (void)signal(SIGINT, SIG_IGN);
1727 if (strcmp(cmd, "RETR") && !proxy)
1729 else if (!strcmp(cmd, "RETR") && proxy)
1731 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1732 if (command("%s %s", cmd2, local) != PRELIM) {
1735 abort_remote((FILE *) NULL);
1740 (void)signal(SIGINT, oldintr);
1744 abort_remote((FILE *) NULL);
1746 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1747 if (command("%s %s", cmd2, local) != PRELIM) {
1750 abort_remote((FILE *) NULL);
1754 (void)signal(SIGINT, oldintr);
1759 abort_remote((FILE *) NULL);
1763 FD_SET(fileno(cin), &mask);
1764 if ((nfnd = empty(&mask, 10)) <= 0) {
1780 (void)signal(SIGINT, oldintr);
1784 reset(int argc, char **argv)
1791 FD_SET(fileno(cin), &mask);
1792 if ((nfnd = empty(&mask, 0)) < 0) {
1804 gunique(const char *local)
1806 static char new[MAXPATHLEN];
1807 char *cp = strrchr(local, '/');
1813 d = access(cp == local ? "/" : cp ? local : ".", W_OK);
1817 warn("local: %s", local);
1818 return ((char *) 0);
1820 (void)strcpy(new, local);
1821 cp = new + strlen(new);
1824 if (++count == 100) {
1825 puts("runique: can't find unique file name.");
1826 return ((char *) 0);
1834 if ((d = access(new, F_OK)) < 0)
1838 else if (*(cp - 2) == '.')
1841 *(cp - 2) = *(cp - 2) + 1;
1849 abort_remote(FILE *din)
1856 warnx("Lost control connection for abort.");
1863 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1864 * after urgent byte rather than before as is protocol now
1866 sprintf(buf, "%c%c%c", IAC, IP, IAC);
1867 if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
1869 fprintf(cout, "%cABOR\r\n", DM);
1872 FD_SET(fileno(cin), &mask);
1874 FD_SET(fileno(din), &mask);
1876 if ((nfnd = empty(&mask, 10)) <= 0) {
1884 if (din && FD_ISSET(fileno(din), &mask)) {
1885 while (read(fileno(din), buf, BUFSIZ) > 0)
1888 if (getreply(0) == ERROR && code == 552) {
1889 /* 552 needed for nic style abort */
1896 ai_unmapped(struct addrinfo *ai)
1898 struct sockaddr_in6 *sin6;
1899 struct sockaddr_in sin;
1901 if (ai->ai_family != AF_INET6)
1903 if (ai->ai_addrlen != sizeof(struct sockaddr_in6) ||
1904 sizeof(sin) > ai->ai_addrlen)
1906 sin6 = (struct sockaddr_in6 *)ai->ai_addr;
1907 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
1910 memset(&sin, 0, sizeof(sin));
1911 sin.sin_family = AF_INET;
1912 sin.sin_len = sizeof(struct sockaddr_in);
1913 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
1914 sizeof(sin.sin_addr));
1915 sin.sin_port = sin6->sin6_port;
1917 ai->ai_family = AF_INET;
1918 memcpy(ai->ai_addr, &sin, sin.sin_len);
1919 ai->ai_addrlen = sin.sin_len;