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.2 2003/06/17 04:29:26 dillon 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;
100 int s, len, tos, error;
101 struct addrinfo hints, *res, *res0;
102 static char hostnamebuf[MAXHOSTNAMELEN];
105 if (*host0 == '[' && strrchr(host0, ']') != NULL) { /*IPv6 addr in []*/
106 strncpy(hostnamebuf, host0 + 1, strlen(host0) - 2);
107 hostnamebuf[strlen(host0) - 2] = '\0';
109 strncpy(hostnamebuf, host0, strlen(host0));
110 hostnamebuf[strlen(host0)] = '\0';
113 memset(&hints, 0, sizeof(hints));
114 hints.ai_flags = AI_CANONNAME;
115 hints.ai_family = family;
116 hints.ai_socktype = SOCK_STREAM;
117 hints.ai_protocol = 0;
118 error = getaddrinfo(host, port, &hints, &res0);
120 warnx("%s: %s", host, gai_strerror(error));
121 if (error == EAI_SYSTEM)
122 warnx("%s: %s", host, strerror(errno));
128 if (res->ai_canonname)
129 (void) strncpy(hostnamebuf, res->ai_canonname,
130 sizeof(hostnamebuf));
131 hostname = hostnamebuf;
134 * make sure that ai_addr is NOT an IPv4 mapped address.
135 * IPv4 mapped address complicates too many things in FTP
136 * protocol handling, as FTP protocol is defined differently
137 * between IPv4 and IPv6.
140 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
150 struct addrinfo *bindres;
153 for (bindres = bindres0;
155 bindres = bindres->ai_next)
156 if (bindres->ai_family == res->ai_family)
160 binderr = bind(s, bindres->ai_addr,
161 bindres->ai_addrlen);
174 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
177 char hname[NI_MAXHOST];
178 getnameinfo(res->ai_addr, res->ai_addrlen,
179 hname, sizeof(hname) - 1, NULL, 0,
181 warn("connect to address %s", hname);
183 getnameinfo(res->ai_addr, res->ai_addrlen,
184 hname, sizeof(hname) - 1, NULL, 0,
186 printf("Trying %s...\n", hname);
194 memcpy(&hisctladdr, res->ai_addr, res->ai_addrlen);
196 len = sizeof(myctladdr);
197 if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
203 if (myctladdr.su_family == AF_INET)
205 tos = IPTOS_LOWDELAY;
206 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
207 warn("setsockopt TOS (ignored)");
210 cin = fdopen(s, "r");
211 cout = fdopen(s, "w");
212 if (cin == NULL || cout == NULL) {
213 warnx("fdopen failed.");
222 printf("Connected to %s.\n", hostname);
223 if (getreply(0) > 2) { /* read startup message from server */
235 if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
240 #endif /* SO_OOBINLINE */
255 (void)fflush(stdout);
264 command(const char *fmt, ...)
279 fputs("---> ", stdout);
284 fmt = va_arg(ap, const char *);
286 if (strncmp("PASS ", fmt, 5) == 0)
287 fputs("PASS XXXX", stdout);
288 else if (strncmp("ACCT ", fmt, 5) == 0)
289 fputs("ACCT XXXX", stdout);
294 (void)fflush(stdout);
297 warnx("No control connection for command.");
301 oldintr = signal(SIGINT, cmdabort);
306 fmt = va_arg(ap, char *);
308 vfprintf(cout, fmt, ap);
313 r = getreply(!strcmp(fmt, "QUIT"));
314 if (abrtflag && oldintr != SIG_IGN)
316 (void)signal(SIGINT, oldintr);
320 char reply_string[BUFSIZ]; /* first line of previous reply */
326 char current_line[BUFSIZ]; /* last line of previous reply */
329 int originalcode = 0, continuation = 0;
332 char *cp, *pt = pasv;
334 oldintr = signal(SIGINT, cmdabort);
335 for (line = 0 ;; line++) {
338 while ((c = getc(cin)) != '\n') {
339 if (c == IAC) { /* handle telnet commands */
340 switch (c = getc(cin)) {
344 fprintf(cout, "%c%c%c", IAC, DONT, c);
350 fprintf(cout, "%c%c%c", IAC, WONT, c);
361 (void)signal(SIGINT, oldintr);
368 "421 Service not available, remote server has closed connection.");
369 (void)fflush(stdout);
374 if (c != '\r' && (verbose > 0 ||
375 (verbose > -1 && n == '5' && dig > 4))) {
377 (dig == 1 || (dig == 5 && verbose == 0)))
378 printf("%s:", hostname);
381 if (dig < 4 && isdigit((unsigned char)c))
382 code = code * 10 + (c - '0');
385 if (code == 227 || code == 228) {
386 /* result for PASV/LPSV */
389 } else if (code == 229) {
390 /* result for EPSV */
396 if (!(dig > 4 && isdigit((unsigned char)c)))
401 if (c != '\r' && c != ')' &&
402 pt < &pasv[sizeof(pasv)-1])
410 if (dig > 4 && c == '(')
414 if (dig == 4 && c == '-') {
421 if (cp < ¤t_line[sizeof(current_line) - 1])
424 if (verbose > 0 || (verbose > -1 && n == '5')) {
426 (void)fflush (stdout);
429 size_t len = cp - current_line;
431 if (len > sizeof(reply_string))
432 len = sizeof(reply_string);
434 (void)strncpy(reply_string, current_line, len);
435 reply_string[len] = '\0';
437 if (continuation && code != originalcode) {
438 if (originalcode == 0)
445 (void)signal(SIGINT, oldintr);
446 if (code == 421 || originalcode == 421)
448 if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
461 t.tv_sec = (long) sec;
463 return (select(32, mask, (fd_set *) 0, (fd_set *) 0, &t));
476 puts("\nsend aborted\nwaiting for remote to finish abort.");
477 (void)fflush(stdout);
478 longjmp(sendabort, 1);
482 sendrequest(cmd, local, remote, printnames)
483 const char *cmd, *local, *remote;
489 int (*closefunc) __P((FILE *));
490 sig_t oldinti, oldintr, oldintp;
491 volatile off_t hashbytes;
492 char *lmode, buf[BUFSIZ], *bufp;
495 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
510 oprogress = progress;
511 if (verbose && printnames) {
512 if (local && *local != '-')
513 printf("local: %s ", local);
515 printf("remote: %s\n", remote);
518 proxtrans(cmd, local, remote);
528 if (setjmp(sendabort)) {
537 (void)signal(SIGINT, oldintr);
539 (void)signal(SIGPIPE, oldintp);
541 (void)signal(SIGINFO, oldinti);
545 oldintr = signal(SIGINT, abortsend);
546 oldinti = signal(SIGINFO, psummary);
547 if (strcmp(local, "-") == 0) {
550 } else if (*local == '|') {
551 oldintp = signal(SIGPIPE, SIG_IGN);
552 fin = popen(local + 1, "r");
554 warn("%s", local + 1);
555 (void)signal(SIGINT, oldintr);
556 (void)signal(SIGPIPE, oldintp);
557 (void)signal(SIGINFO, oldinti);
564 fin = fopen(local, "r");
566 warn("local: %s", local);
567 (void)signal(SIGINT, oldintr);
568 (void)signal(SIGINFO, oldinti);
573 if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) {
574 printf("%s: not a plain file.\n", local);
575 (void)signal(SIGINT, oldintr);
576 (void)signal(SIGINFO, oldinti);
581 filesize = st.st_size;
584 (void)signal(SIGINT, oldintr);
585 (void)signal(SIGINFO, oldinti);
587 (void)signal(SIGPIPE, oldintp);
589 if (closefunc != NULL)
593 if (setjmp(sendabort))
597 (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
603 rc = fseek(fin, (long) restart_point, SEEK_SET);
607 rc = lseek(fileno(fin), restart_point, SEEK_SET);
611 warn("local: %s", local);
612 if (closefunc != NULL)
616 if (command("REST %qd", (long long) restart_point) !=
618 if (closefunc != NULL)
625 if (command("%s %s", cmd, remote) != PRELIM) {
626 (void)signal(SIGINT, oldintr);
627 (void)signal(SIGINFO, oldinti);
629 (void)signal(SIGPIPE, oldintp);
630 if (closefunc != NULL)
635 if (command("%s", cmd) != PRELIM) {
636 (void)signal(SIGINT, oldintr);
637 (void)signal(SIGINFO, oldinti);
639 (void)signal(SIGPIPE, oldintp);
640 if (closefunc != NULL)
644 dout = dataconn(lmode);
648 oldintp = signal(SIGPIPE, SIG_IGN);
654 while ((c = read(fileno(fin), buf, sizeof(buf))) > 0) {
656 for (bufp = buf; c > 0; c -= d, bufp += d)
657 if ((d = write(fileno(dout), bufp, c)) <= 0)
659 if (hash && (!progress || filesize < 0) ) {
660 while (bytes >= hashbytes) {
664 (void)fflush(stdout);
667 if (hash && (!progress || filesize < 0) && bytes > 0) {
671 (void)fflush(stdout);
674 warn("local: %s", local);
683 while ((c = getc(fin)) != EOF) {
685 while (hash && (!progress || filesize < 0) &&
686 (bytes >= hashbytes)) {
688 (void)fflush(stdout);
693 (void)putc('\r', dout);
698 #if 0 /* this violates RFC */
700 (void)putc('\0', dout);
705 if (hash && (!progress || filesize < 0)) {
706 if (bytes < hashbytes)
709 (void)fflush(stdout);
712 warn("local: %s", local);
721 if (closefunc != NULL)
725 (void)signal(SIGINT, oldintr);
726 (void)signal(SIGINFO, oldinti);
728 (void)signal(SIGPIPE, oldintp);
733 (void)signal(SIGINT, oldintr);
734 (void)signal(SIGINFO, oldinti);
736 (void)signal(SIGPIPE, oldintp);
749 if (closefunc != NULL && fin != NULL)
754 progress = oprogress;
768 puts("\nreceive aborted\nwaiting for remote to finish abort.");
769 (void)fflush(stdout);
770 longjmp(recvabort, 1);
774 recvrequest(cmd, local, remote, lmode, printnames, ignorespecial)
775 const char *cmd, *local, *remote, *lmode;
776 int printnames, ignorespecial;
779 int (*closefunc) __P((FILE *));
780 sig_t oldinti, oldintr, oldintp;
782 volatile int is_retr, tcrflag, bare_lfs;
783 static size_t bufsize;
785 volatile off_t hashbytes;
788 struct timeval tval[2];
792 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
806 direction = "received";
810 oprogress = progress;
811 opreserve = preserve;
812 is_retr = (strcmp(cmd, "RETR") == 0);
813 if (is_retr && verbose && printnames) {
814 if (local && (ignorespecial || *local != '-'))
815 printf("local: %s ", local);
817 printf("remote: %s\n", remote);
819 if (proxy && is_retr) {
820 proxtrans(cmd, local, remote);
826 tcrflag = !crflag && is_retr;
827 if (setjmp(recvabort)) {
836 (void)signal(SIGINT, oldintr);
838 (void)signal(SIGINFO, oldinti);
839 progress = oprogress;
840 preserve = opreserve;
844 oldintr = signal(SIGINT, abortrecv);
845 oldinti = signal(SIGINFO, psummary);
846 if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
847 if (access(local, W_OK) < 0) {
848 char *dir = strrchr(local, '/');
850 if (errno != ENOENT && errno != EACCES) {
851 warn("local: %s", local);
852 (void)signal(SIGINT, oldintr);
853 (void)signal(SIGINFO, oldinti);
859 d = access(dir == local ? "/" : dir ? local : ".", W_OK);
863 warn("local: %s", local);
864 (void)signal(SIGINT, oldintr);
865 (void)signal(SIGINFO, oldinti);
869 if (!runique && errno == EACCES &&
870 chmod(local, 0600) < 0) {
871 warn("local: %s", local);
872 (void)signal(SIGINT, oldintr);
873 (void)signal(SIGINFO, oldinti);
877 if (runique && errno == EACCES &&
878 (local = gunique(local)) == NULL) {
879 (void)signal(SIGINT, oldintr);
880 (void)signal(SIGINFO, oldinti);
885 else if (runique && (local = gunique(local)) == NULL) {
886 (void)signal(SIGINT, oldintr);
887 (void)signal(SIGINFO, oldinti);
893 if (curtype != TYPE_A)
894 changetype(TYPE_A, 0);
898 filesize = remotesize(remote, 0);
901 (void)signal(SIGINT, oldintr);
902 (void)signal(SIGINFO, oldinti);
906 if (setjmp(recvabort))
908 if (is_retr && restart_point &&
909 command("REST %qd", (long long) restart_point) != CONTINUE)
912 if (command("%s %s", cmd, remote) != PRELIM) {
913 (void)signal(SIGINT, oldintr);
914 (void)signal(SIGINFO, oldinti);
918 if (command("%s", cmd) != PRELIM) {
919 (void)signal(SIGINT, oldintr);
920 (void)signal(SIGINFO, oldinti);
927 if (!ignorespecial && strcmp(local, "-") == 0) {
931 } else if (!ignorespecial && *local == '|') {
932 oldintp = signal(SIGPIPE, SIG_IGN);
933 fout = popen(local + 1, "w");
942 fout = fopen(local, lmode);
944 warn("local: %s", local);
949 if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
950 st.st_blksize = BUFSIZ;
951 if (st.st_blksize > bufsize) {
954 buf = malloc((unsigned)st.st_blksize);
960 bufsize = st.st_blksize;
962 if (!S_ISREG(st.st_mode)) {
971 if (is_retr && restart_point &&
972 lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
973 warn("local: %s", local);
974 progress = oprogress;
975 preserve = opreserve;
976 if (closefunc != NULL)
981 while ((c = read(fileno(din), buf, bufsize)) > 0) {
982 if ((d = write(fileno(fout), buf, c)) != c)
985 if (hash && (!progress || filesize < 0)) {
986 while (bytes >= hashbytes) {
990 (void)fflush(stdout);
993 if (hash && (!progress || filesize < 0) && bytes > 0) {
997 (void)fflush(stdout);
1006 warn("local: %s", local);
1008 warnx("%s: short write", local);
1013 if (is_retr && restart_point) {
1017 if (fseek(fout, 0L, SEEK_SET) < 0)
1019 n = (long)restart_point;
1020 for (i = 0; i++ < n;) {
1021 if ((ch = getc(fout)) == EOF)
1026 if (fseek(fout, 0L, SEEK_CUR) < 0) {
1028 warn("local: %s", local);
1029 progress = oprogress;
1030 preserve = opreserve;
1031 if (closefunc != NULL)
1036 while ((c = getc(din)) != EOF) {
1040 while (hash && (!progress || filesize < 0) &&
1041 (bytes >= hashbytes)) {
1043 (void)fflush(stdout);
1047 if ((c = getc(din)) != '\n' || tcrflag) {
1050 (void)putc('\r', fout);
1059 (void)putc(c, fout);
1066 "WARNING! %d bare linefeeds received in ASCII mode.\n", bare_lfs);
1067 puts("File may not have transferred correctly.");
1069 if (hash && (!progress || filesize < 0)) {
1070 if (bytes < hashbytes)
1072 (void)putchar('\n');
1073 (void)fflush(stdout);
1081 warn("local: %s", local);
1085 progress = oprogress;
1086 preserve = opreserve;
1087 if (closefunc != NULL)
1089 (void)signal(SIGINT, oldintr);
1090 (void)signal(SIGINFO, oldinti);
1092 (void)signal(SIGPIPE, oldintp);
1095 if (bytes >= 0 && is_retr) {
1098 if (preserve && (closefunc == fclose)) {
1099 mtime = remotemodtime(remote, 0);
1101 (void)gettimeofday(&tval[0],
1102 (struct timezone *)0);
1103 tval[1].tv_sec = mtime;
1104 tval[1].tv_usec = 0;
1105 if (utimes(local, tval) == -1) {
1107 "Can't change modification time on %s to %s",
1108 local, asctime(localtime(&mtime)));
1117 /* abort using RFC959 recommended IP,SYNC sequence */
1119 progress = oprogress;
1120 preserve = opreserve;
1122 (void)signal(SIGPIPE, oldintp);
1123 (void)signal(SIGINT, SIG_IGN);
1126 (void)signal(SIGINT, oldintr);
1127 (void)signal(SIGINFO, oldinti);
1137 if (closefunc != NULL && fout != NULL)
1143 (void)signal(SIGINT, oldintr);
1144 (void)signal(SIGINFO, oldinti);
1148 * Need to start a listen on the data channel before we send the command,
1149 * otherwise the server's connect may fail.
1155 int result, len, tmpno = 0;
1164 if (myctladdr.su_family == AF_INET6
1165 && (IN6_IS_ADDR_LINKLOCAL(&myctladdr.su_sin6.sin6_addr)
1166 || IN6_IS_ADDR_SITELOCAL(&myctladdr.su_sin6.sin6_addr))) {
1167 warnx("use of scoped address can be troublesome");
1172 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
1175 data_addr = myctladdr;
1176 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1182 struct addrinfo *bindres;
1185 for (bindres = bindres0;
1187 bindres = bindres->ai_next)
1188 if (bindres->ai_family == data_addr.su_family)
1190 if (bindres == NULL)
1192 binderr = bind(data, bindres->ai_addr,
1193 bindres->ai_addrlen);
1200 if ((options & SO_DEBUG) &&
1201 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1203 warn("setsockopt (ignored)");
1204 switch (data_addr.su_family) {
1212 result = command(pasvcmd = "EPSV");
1214 if (code / 10 == 22 && code != 229) {
1215 puts("wrong server: EPSV return code must be 229");
1216 result = COMPLETE + 1;
1219 result = COMPLETE + 1;
1220 if (result != COMPLETE) {
1222 result = command(pasvcmd = "PASV");
1227 result = command(pasvcmd = "EPSV");
1228 if (code / 10 == 22 && code != 229) {
1229 puts("wrong server: EPSV return code must be 229");
1230 result = COMPLETE + 1;
1232 if (result != COMPLETE)
1233 result = command(pasvcmd = "LPSV");
1237 result = COMPLETE + 1;
1239 if (result != COMPLETE) {
1240 puts("Passive mode refused.");
1244 #define pack2(var, offset) \
1245 (((var[(offset) + 0] & 0xff) << 8) | ((var[(offset) + 1] & 0xff) << 0))
1246 #define pack4(var, offset) \
1247 (((var[(offset) + 0] & 0xff) << 24) | ((var[(offset) + 1] & 0xff) << 16) \
1248 | ((var[(offset) + 2] & 0xff) << 8) | ((var[(offset) + 3] & 0xff) << 0))
1250 * What we've got at this point is a string of comma
1251 * separated one-byte unsigned integer values.
1253 * The first four are the an IP address. The fifth is
1254 * the MSB of the port number, the sixth is the LSB.
1255 * From that we'll prepare a sockaddr_in.
1256 * In other case, the format is more complicated.
1258 if (strcmp(pasvcmd, "PASV") == 0) {
1259 if (code / 10 == 22 && code != 227) {
1260 puts("wrong server: return code must be 227");
1264 error = sscanf(pasv, "%d,%d,%d,%d,%d,%d",
1265 &h[0], &h[1], &h[2], &h[3],
1269 data_addr.su_sin.sin_addr.s_addr =
1273 } else if (strcmp(pasvcmd, "LPSV") == 0) {
1274 if (code / 10 == 22 && code != 228) {
1275 puts("wrong server: return code must be 228");
1279 switch (data_addr.su_family) {
1281 error = sscanf(pasv,
1282 "%d,%d,%d,%d,%d,%d,%d,%d,%d",
1284 &h[0], &h[1], &h[2], &h[3],
1285 &pal, &prt[0], &prt[1]);
1286 if (error == 9 && af == 4 && hal == 4 && pal == 2) {
1288 data_addr.su_sin.sin_addr.s_addr =
1295 error = sscanf(pasv,
1296 "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1298 &h[0], &h[1], &h[2], &h[3],
1299 &h[4], &h[5], &h[6], &h[7],
1300 &h[8], &h[9], &h[10], &h[11],
1301 &h[12], &h[13], &h[14], &h[15],
1302 &pal, &prt[0], &prt[1]);
1303 if (error != 21 || af != 6 || hal != 16 || pal != 2) {
1311 p32 = (u_int32_t *)&data_addr.su_sin6.sin6_addr;
1312 p32[0] = htonl(pack4(h, 0));
1313 p32[1] = htonl(pack4(h, 4));
1314 p32[2] = htonl(pack4(h, 8));
1315 p32[3] = htonl(pack4(h, 12));
1322 } else if (strcmp(pasvcmd, "EPSV") == 0) {
1326 if (code / 10 == 22 && code != 229) {
1327 puts("wrong server: return code must be 229");
1331 error = sscanf(pasv, "%c%c%c%d%c",
1332 &delim[0], &delim[1], &delim[2],
1333 &prt[1], &delim[3]);
1338 if (delim[0] != delim[1] || delim[0] != delim[2]
1339 || delim[0] != delim[3]) {
1344 data_addr = hisctladdr;
1346 prt[0] = (prt[1] & 0xff00) >> 8;
1355 "Passive mode address scan failure. Shouldn't happen!");
1359 data_addr.su_port = htons(pack2(prt, 0));
1361 if (connect(data, (struct sockaddr *)&data_addr,
1362 data_addr.su_len) < 0) {
1367 if (data_addr.su_family == AF_INET)
1369 on = IPTOS_THROUGHPUT;
1370 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1372 warn("setsockopt TOS (ignored)");
1379 data_addr = myctladdr;
1381 data_addr.su_port = 0; /* let system pick one */
1384 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1392 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
1394 warn("setsockopt (reuse address)");
1398 if (data_addr.su_family == AF_INET)
1401 ports = restricted_data_ports ? IP_PORTRANGE_HIGH : IP_PORTRANGE_DEFAULT;
1402 if (setsockopt(data, IPPROTO_IP, IP_PORTRANGE, (char *)&ports,
1404 warn("setsockopt PORTRANGE (ignored)");
1408 #ifdef IPV6_PORTRANGE
1409 if (data_addr.su_family == AF_INET6) {
1410 ports = restricted_data_ports ? IPV6_PORTRANGE_HIGH
1411 : IPV6_PORTRANGE_DEFAULT;
1412 if (setsockopt(data, IPPROTO_IPV6, IPV6_PORTRANGE,
1413 (char *)&ports, sizeof(ports)) < 0)
1414 warn("setsockopt PORTRANGE (ignored)");
1418 if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {
1422 if (options & SO_DEBUG &&
1423 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1425 warn("setsockopt (ignored)");
1426 len = sizeof(data_addr);
1427 if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1428 warn("getsockname");
1431 if (listen(data, 1) < 0)
1434 char hname[INET6_ADDRSTRLEN];
1436 struct sockaddr_in data_addr4;
1437 union sockunion *daddr;
1440 if (data_addr.su_family == AF_INET6 &&
1441 IN6_IS_ADDR_V4MAPPED(&data_addr.su_sin6.sin6_addr)) {
1442 memset(&data_addr4, 0, sizeof(data_addr4));
1443 data_addr4.sin_len = sizeof(struct sockaddr_in);
1444 data_addr4.sin_family = AF_INET;
1445 data_addr4.sin_port = data_addr.su_port;
1446 memcpy((caddr_t)&data_addr4.sin_addr,
1447 (caddr_t)&data_addr.su_sin6.sin6_addr.s6_addr[12],
1448 sizeof(struct in_addr));
1449 daddr = (union sockunion *)&data_addr4;
1456 #define UC(b) (((int)b)&0xff)
1458 switch (daddr->su_family) {
1461 af = (daddr->su_family == AF_INET) ? 1 : 2;
1462 if (daddr->su_family == AF_INET6)
1463 daddr->su_sin6.sin6_scope_id = 0;
1464 if (getnameinfo((struct sockaddr *)daddr,
1465 daddr->su_len, hname,
1466 sizeof(hname) - 1, NULL, 0,
1470 result = command("EPRT |%d|%s|%d|",
1471 af, hname, ntohs(daddr->su_port));
1476 result = COMPLETE + 1;
1479 if (result == COMPLETE)
1482 p = (char *)&daddr->su_port;
1483 switch (daddr->su_family) {
1485 a = (char *)&daddr->su_sin.sin_addr;
1486 result = command("PORT %d,%d,%d,%d,%d,%d",
1487 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
1488 UC(p[0]), UC(p[1]));
1492 a = (char *)&daddr->su_sin6.sin6_addr;
1494 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1496 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
1497 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
1498 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
1499 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
1500 2, UC(p[0]), UC(p[1]));
1504 result = COMPLETE + 1; /* xxx */
1508 if (result == ERROR && sendport == -1) {
1513 return (result != COMPLETE);
1518 if (data_addr.su_family == AF_INET)
1520 on = IPTOS_THROUGHPUT;
1521 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1522 warn("setsockopt TOS (ignored)");
1527 (void)close(data), data = -1;
1537 union sockunion from;
1538 int s, fromlen, tos;
1540 fromlen = myctladdr.su_len;
1543 return (fdopen(data, lmode));
1545 s = accept(data, (struct sockaddr *) &from, &fromlen);
1548 (void)close(data), data = -1;
1554 if (data_addr.su_family == AF_INET)
1556 tos = IPTOS_THROUGHPUT;
1557 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
1558 warn("setsockopt TOS (ignored)");
1561 return (fdopen(data, lmode));
1587 static struct comvars {
1589 char name[MAXHOSTNAMELEN];
1590 union sockunion mctl;
1591 union sockunion hctl;
1604 char mi[MAXPATHLEN];
1605 char mo[MAXPATHLEN];
1606 } proxstruct, tmpstruct;
1607 struct comvars *ip, *op;
1610 oldintr = signal(SIGINT, psabort);
1624 ip->connect = connected;
1625 connected = op->connect;
1627 (void)strncpy(ip->name, hostname, sizeof(ip->name) - 1);
1628 ip->name[sizeof(ip->name) - 1] = '\0';
1631 hostname = op->name;
1632 ip->hctl = hisctladdr;
1633 hisctladdr = op->hctl;
1634 ip->mctl = myctladdr;
1635 myctladdr = op->mctl;
1642 ip->curtpe = curtype;
1643 curtype = op->curtpe;
1646 ip->sunqe = sunique;
1647 sunique = op->sunqe;
1648 ip->runqe = runique;
1649 runique = op->runqe;
1654 (void)strncpy(ip->nti, ntin, sizeof(ip->nti) - 1);
1655 (ip->nti)[sizeof(ip->nti) - 1] = '\0';
1656 (void)strcpy(ntin, op->nti);
1657 (void)strncpy(ip->nto, ntout, sizeof(ip->nto) - 1);
1658 (ip->nto)[sizeof(ip->nto) - 1] = '\0';
1659 (void)strcpy(ntout, op->nto);
1660 ip->mapflg = mapflag;
1661 mapflag = op->mapflg;
1662 (void)strncpy(ip->mi, mapin, sizeof(ip->mi) - 1);
1663 (ip->mi)[sizeof(ip->mi) - 1] = '\0';
1664 (void)strcpy(mapin, op->mi);
1665 (void)strncpy(ip->mo, mapout, sizeof(ip->mo) - 1);
1666 (ip->mo)[sizeof(ip->mo) - 1] = '\0';
1667 (void)strcpy(mapout, op->mo);
1668 (void)signal(SIGINT, oldintr);
1682 (void)fflush(stdout);
1686 longjmp(ptabort, 1);
1690 proxtrans(cmd, local, remote)
1691 const char *cmd, *local, *remote;
1694 int prox_type, nfnd;
1695 volatile int secndflag;
1699 #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
1706 if (strcmp(cmd, "RETR"))
1709 cmd2 = runique ? "STOU" : "STOR";
1710 if ((prox_type = type) == 0) {
1711 if (unix_server && unix_proxy)
1716 if (curtype != prox_type)
1717 changetype(prox_type, 1);
1718 if (try_epsv && command("EPSV") != COMPLETE)
1720 if (!try_epsv && command("PASV") != COMPLETE) {
1721 puts("proxy server does not support third party transfers.");
1726 puts("No primary connection.");
1731 if (curtype != prox_type)
1732 changetype(prox_type, 1);
1733 if (command("PORT %s", pasv) != COMPLETE) {
1737 if (setjmp(ptabort))
1739 oldintr = signal(SIGINT, abortpt);
1740 if (command("%s %s", cmd, remote) != PRELIM) {
1741 (void)signal(SIGINT, oldintr);
1748 if (command("%s %s", cmd2, local) != PRELIM)
1754 (void)signal(SIGINT, oldintr);
1757 printf("local: %s remote: %s\n", local, remote);
1760 (void)signal(SIGINT, SIG_IGN);
1762 if (strcmp(cmd, "RETR") && !proxy)
1764 else if (!strcmp(cmd, "RETR") && proxy)
1766 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1767 if (command("%s %s", cmd2, local) != PRELIM) {
1770 abort_remote((FILE *) NULL);
1775 (void)signal(SIGINT, oldintr);
1779 abort_remote((FILE *) NULL);
1781 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1782 if (command("%s %s", cmd2, local) != PRELIM) {
1785 abort_remote((FILE *) NULL);
1789 (void)signal(SIGINT, oldintr);
1794 abort_remote((FILE *) NULL);
1798 FD_SET(fileno(cin), &mask);
1799 if ((nfnd = empty(&mask, 10)) <= 0) {
1815 (void)signal(SIGINT, oldintr);
1828 FD_SET(fileno(cin), &mask);
1829 if ((nfnd = empty(&mask, 0)) < 0) {
1844 static char new[MAXPATHLEN];
1845 char *cp = strrchr(local, '/');
1851 d = access(cp == local ? "/" : cp ? local : ".", W_OK);
1855 warn("local: %s", local);
1856 return ((char *) 0);
1858 (void)strcpy(new, local);
1859 cp = new + strlen(new);
1862 if (++count == 100) {
1863 puts("runique: can't find unique file name.");
1864 return ((char *) 0);
1872 if ((d = access(new, F_OK)) < 0)
1876 else if (*(cp - 2) == '.')
1879 *(cp - 2) = *(cp - 2) + 1;
1895 warnx("Lost control connection for abort.");
1902 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1903 * after urgent byte rather than before as is protocol now
1905 sprintf(buf, "%c%c%c", IAC, IP, IAC);
1906 if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
1908 fprintf(cout, "%cABOR\r\n", DM);
1911 FD_SET(fileno(cin), &mask);
1913 FD_SET(fileno(din), &mask);
1915 if ((nfnd = empty(&mask, 10)) <= 0) {
1923 if (din && FD_ISSET(fileno(din), &mask)) {
1924 while (read(fileno(din), buf, BUFSIZ) > 0)
1927 if (getreply(0) == ERROR && code == 552) {
1928 /* 552 needed for nic style abort */
1936 struct addrinfo *ai;
1938 struct sockaddr_in6 *sin6;
1939 struct sockaddr_in sin;
1941 if (ai->ai_family != AF_INET6)
1943 if (ai->ai_addrlen != sizeof(struct sockaddr_in6) ||
1944 sizeof(sin) > ai->ai_addrlen)
1946 sin6 = (struct sockaddr_in6 *)ai->ai_addr;
1947 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
1950 memset(&sin, 0, sizeof(sin));
1951 sin.sin_family = AF_INET;
1952 sin.sin_len = sizeof(struct sockaddr_in);
1953 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
1954 sizeof(sin.sin_addr));
1955 sin.sin_port = sin6->sin6_port;
1957 ai->ai_family = AF_INET;
1958 memcpy(ai->ai_addr, &sin, sin.sin_len);
1959 ai->ai_addrlen = sin.sin_len;