2 * Copyright (c) 1985, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID ("$Id: ftp.c 16650 2006-01-24 08:16:08Z lha $");
37 struct sockaddr_storage hisctladdr_ss;
38 struct sockaddr *hisctladdr = (struct sockaddr *)&hisctladdr_ss;
39 struct sockaddr_storage data_addr_ss;
40 struct sockaddr *data_addr = (struct sockaddr *)&data_addr_ss;
41 struct sockaddr_storage myctladdr_ss;
42 struct sockaddr *myctladdr = (struct sockaddr *)&myctladdr_ss;
48 off_t restart_point = 0;
53 typedef void (*sighand) (int);
56 hookup (const char *host, int port)
58 static char hostnamebuf[MaxHostNameLen];
59 struct addrinfo *ai, *a;
60 struct addrinfo hints;
62 char portstr[NI_MAXSERV];
66 memset (&hints, 0, sizeof(hints));
67 hints.ai_socktype = SOCK_STREAM;
68 hints.ai_protocol = IPPROTO_TCP;
69 hints.ai_flags = AI_CANONNAME;
71 snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
73 error = getaddrinfo (host, portstr, &hints, &ai);
75 warnx ("%s: %s", host, gai_strerror(error));
79 strlcpy (hostnamebuf, host, sizeof(hostnamebuf));
80 hostname = hostnamebuf;
83 for (a = ai; a != NULL; a = a->ai_next) {
84 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
88 if (a->ai_canonname != NULL)
89 strlcpy (hostnamebuf, a->ai_canonname, sizeof(hostnamebuf));
91 memcpy (hisctladdr, a->ai_addr, a->ai_addrlen);
93 error = connect (s, a->ai_addr, a->ai_addrlen);
97 if (getnameinfo (a->ai_addr, a->ai_addrlen,
98 addrstr, sizeof(addrstr),
99 NULL, 0, NI_NUMERICHOST) != 0)
100 strlcpy (addrstr, "unknown address", sizeof(addrstr));
102 warn ("connect %s", addrstr);
111 warnx ("failed to contact %s", host);
116 len = sizeof(myctladdr_ss);
117 if (getsockname (s, myctladdr, &len) < 0) {
118 warn ("getsockname");
123 #ifdef IPTOS_LOWDELAY
124 socket_set_tos (s, IPTOS_LOWDELAY);
126 cin = fdopen (s, "r");
127 cout = fdopen (s, "w");
128 if (cin == NULL || cout == NULL) {
129 warnx ("fdopen failed.");
138 printf ("Connected to %s.\n", hostname);
139 if (getreply (0) > 2) { /* read startup message from server */
147 #if defined(SO_OOBINLINE) && defined(HAVE_SETSOCKOPT)
151 if (setsockopt (s, SOL_SOCKET, SO_OOBINLINE, (char *) &on, sizeof (on))
156 #endif /* SO_OOBINLINE */
168 char defaultpass[128];
169 char *userstr, *pass, *acctstr;
173 struct passwd *pw = k_getpwuid(getuid());
176 myname = pw->pw_name;
178 userstr = pass = acctstr = 0;
181 printf("\n*** Using plaintext user and password ***\n\n");
183 printf("Authentication successful.\n\n");
186 if (ruserpass (host, &userstr, &pass, &acctstr) < 0) {
190 while (userstr == NULL) {
192 printf ("Name (%s:%s): ", host, myname);
194 printf ("Name (%s): ", host);
196 if (fgets (tmp, sizeof (tmp) - 1, stdin) != NULL)
197 tmp[strlen (tmp) - 1] = '\0';
203 strlcpy(username, userstr, sizeof(username));
204 n = command("USER %s", userstr);
206 n = command("PASS dummy"); /* DK: Compatibility with gssftp daemon */
207 else if(n == CONTINUE) {
211 (!strcmp(userstr, "ftp") || !strcmp(userstr, "anonymous"))) {
212 snprintf(defaultpass, sizeof(defaultpass),
213 "%s@%s", myname, mydomain);
214 snprintf(prompt, sizeof(prompt),
215 "Password (%s): ", defaultpass);
216 } else if (sec_complete) {
220 snprintf(prompt, sizeof(prompt), "Password: ");
224 UI_UTIL_read_pw_string (tmp, sizeof (tmp), prompt, 0);
229 n = command ("PASS %s", pass);
234 UI_UTIL_read_pw_string (acctstr, 128, "Account:", 0);
235 n = command ("ACCT %s", acctstr);
238 warnx ("Login failed.");
241 if (!aflag && acctstr != NULL)
242 command ("ACCT %s", acctstr);
245 for (n = 0; n < macnum; ++n) {
246 if (!strcmp("init", macros[n].mac_name)) {
247 strlcpy (line, "$init", sizeof (line));
249 domacro(margc, margv);
253 sec_set_protection_level ();
265 longjmp (ptabort, 1);
269 command (char *fmt,...)
277 warn ("No control connection for command");
281 oldintr = signal(SIGINT, cmdabort);
284 if (strncmp("PASS ", fmt, 5) == 0)
288 vfprintf(stdout, fmt, ap);
293 sec_vfprintf(cout, fmt, ap);
299 fprintf (cout, "\r\n");
302 r = getreply (!strcmp (fmt, "QUIT"));
303 if (abrtflag && oldintr != SIG_IGN)
305 signal (SIGINT, oldintr);
309 char reply_string[BUFSIZ]; /* last line of previous reply */
312 getreply (int expecteof)
317 struct sigaction sa, osa;
322 sigemptyset (&sa.sa_mask);
324 sa.sa_handler = cmdabort;
325 sigaction (SIGINT, &sa, &osa);
335 sigaction (SIGINT, &osa, NULL);
341 printf ("421 Service not available, "
342 "remote server has closed connection\n");
349 if (c == WILL || c == WONT)
350 fprintf (cout, "%c%c%c", IAC, DONT, getc (cin));
351 if (c == DO || c == DONT)
352 fprintf (cout, "%c%c%c", IAC, WONT, getc (cin));
356 if(isdigit((unsigned char)buf[0])){
357 sscanf(buf, "%d", &code);
360 sec_read_msg(buf, prot_safe);
361 sscanf(buf, "%d", &code);
363 } else if(code == 632){
365 sec_read_msg(buf, prot_private);
366 sscanf(buf, "%d", &code);
368 }else if(code == 633){
370 sec_read_msg(buf, prot_confidential);
371 sscanf(buf, "%d", &code);
373 }else if(sec_complete)
377 if(code != 0 && reply_code == 0)
379 if (verbose > 0 || (verbose > -1 && code > 499))
380 fprintf (stdout, "%s%s\n", lead_string, buf);
381 if (code == reply_code && buf[3] == ' ') {
382 strlcpy (reply_string, buf, sizeof(reply_string));
385 sigaction (SIGINT, &osa, NULL);
390 osa.sa_handler != cmdabort &&
391 osa.sa_handler != SIG_IGN)
392 osa.sa_handler (SIGINT);
394 if (code == 227 || code == 229) {
397 q = strchr (reply_string, '(');
400 strlcpy(pasv, q, sizeof(pasv));
401 q = strrchr(pasv, ')');
409 if(verbose > 0 || (verbose > -1 && code > 499)){
411 fprintf(stdout, "!!");
412 fprintf(stdout, "%s\n", buf);
419 if(p < buf + sizeof(buf) - 1)
421 else if(long_warn == 0) {
422 fprintf(stderr, "WARNING: incredibly long line received\n");
433 getreply (int expecteof)
437 int originalcode = 0, continuation = 0;
440 char *cp, *pt = pasv;
442 oldintr = signal (SIGINT, cmdabort);
446 while ((c = getc (cin)) != '\n') {
447 if (c == IAC) { /* handle telnet commands */
448 switch (c = getc (cin)) {
452 fprintf (cout, "%c%c%c", IAC, DONT, c);
458 fprintf (cout, "%c%c%c", IAC, WONT, c);
469 signal (SIGINT, oldintr);
475 printf ("421 Service not available, remote server has closed connection\n");
481 if (c != '\r' && (verbose > 0 ||
482 (verbose > -1 && n == '5' && dig > 4))) {
484 (dig == 1 || dig == 5 && verbose == 0))
485 printf ("%s:", hostname);
488 if (dig < 4 && isdigit (c))
489 code = code * 10 + (c - '0');
490 if (!pflag && code == 227)
492 if (dig > 4 && pflag == 1 && isdigit (c))
495 if (c != '\r' && c != ')')
502 if (dig == 4 && c == '-') {
509 if (cp < &reply_string[sizeof (reply_string) - 1])
512 if (verbose > 0 || verbose > -1 && n == '5') {
516 if (continuation && code != originalcode) {
517 if (originalcode == 0)
524 sec_read_msg(reply_string, prot_safe);
526 sec_read_msg(reply_string, prot_private);
528 sec_read_msg(reply_string, prot_confidential);
529 n = code / 100 + '0';
533 signal (SIGINT, oldintr);
534 if (code == 421 || originalcode == 421)
536 if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
545 empty (fd_set * mask, int sec)
551 return (select (FD_SETSIZE, mask, NULL, NULL, &t));
562 printf ("\nsend aborted\nwaiting for remote to finish abort\n");
564 longjmp (sendabort, 1);
567 #define HASHBYTES 1024
570 copy_stream (FILE * from, FILE * to)
572 static size_t bufsize;
577 int hashbytes = HASHBYTES;
580 #if defined(HAVE_MMAP) && !defined(NO_MMAP)
584 #define MAP_FAILED (-1)
587 if (fstat (fileno (from), &st) == 0 && S_ISREG (st.st_mode)) {
589 * mmap zero bytes has potential of loosing, don't do it.
593 chunk = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fileno (from), 0);
594 if (chunk != (void *) MAP_FAILED) {
597 res = sec_write (fileno (to), chunk, st.st_size);
598 if (munmap (chunk, st.st_size) < 0)
606 buf = alloc_buffer (buf, &bufsize,
607 fstat (fileno (from), &st) >= 0 ? &st : NULL);
611 while ((n = read (fileno (from), buf, bufsize)) > 0) {
612 werr = sec_write (fileno (to), buf, n);
616 while (hash && bytes > hashbytes) {
618 hashbytes += HASHBYTES;
634 sendrequest (char *cmd, char *local, char *remote, char *lmode, int printnames)
637 struct timeval start, stop;
639 FILE *fin, *dout = 0;
640 int (*closefunc) (FILE *);
641 RETSIGTYPE (*oldintr)(int), (*oldintp)(int);
642 long bytes = 0, hashbytes = HASHBYTES;
645 if (verbose && printnames) {
646 if (local && strcmp (local, "-") != 0)
647 printf ("local: %s ", local);
649 printf ("remote: %s\n", remote);
652 proxtrans (cmd, local, remote);
656 changetype (type, 0);
661 if (setjmp (sendabort)) {
670 signal (SIGINT, oldintr);
672 signal (SIGPIPE, oldintp);
676 oldintr = signal (SIGINT, abortsend);
677 if (strcmp (local, "-") == 0)
679 else if (*local == '|') {
680 oldintp = signal (SIGPIPE, SIG_IGN);
681 fin = popen (local + 1, lmode);
683 warn ("%s", local + 1);
684 signal (SIGINT, oldintr);
685 signal (SIGPIPE, oldintp);
691 fin = fopen (local, lmode);
693 warn ("local: %s", local);
694 signal (SIGINT, oldintr);
699 if (fstat (fileno (fin), &st) < 0 ||
700 (st.st_mode & S_IFMT) != S_IFREG) {
701 fprintf (stdout, "%s: not a plain file.\n", local);
702 signal (SIGINT, oldintr);
709 signal (SIGINT, oldintr);
711 signal (SIGPIPE, oldintp);
713 if (closefunc != NULL)
717 if (setjmp (sendabort))
721 (strcmp (cmd, "STOR") == 0 || strcmp (cmd, "APPE") == 0)) {
726 rc = fseek (fin, (long) restart_point, SEEK_SET);
730 rc = lseek (fileno (fin), restart_point, SEEK_SET);
736 warn ("local: %s", local);
738 if (closefunc != NULL)
742 if (command ("REST %ld", (long) restart_point)
745 if (closefunc != NULL)
753 if (command ("%s %s", cmd, remote) != PRELIM) {
754 signal (SIGINT, oldintr);
756 signal (SIGPIPE, oldintp);
757 if (closefunc != NULL)
761 } else if (command ("%s", cmd) != PRELIM) {
762 signal(SIGINT, oldintr);
764 signal(SIGPIPE, oldintp);
765 if (closefunc != NULL)
769 dout = dataconn(rmode);
772 set_buffer_size (fileno (dout), 0);
773 gettimeofday (&start, (struct timezone *) 0);
774 oldintp = signal (SIGPIPE, SIG_IGN);
780 bytes = copy_stream (fin, dout);
784 while ((c = getc (fin)) != EOF) {
786 while (hash && (bytes >= hashbytes)) {
789 hashbytes += HASHBYTES;
793 sec_putc ('\r', dout);
801 if (bytes < hashbytes)
807 warn ("local: %s", local);
815 if (closefunc != NULL)
818 gettimeofday (&stop, (struct timezone *) 0);
820 signal (SIGINT, oldintr);
822 signal (SIGPIPE, oldintp);
824 ptransfer ("sent", bytes, &start, &stop);
827 signal (SIGINT, oldintr);
829 signal (SIGPIPE, oldintp);
842 if (closefunc != NULL && fin != NULL)
844 gettimeofday (&stop, (struct timezone *) 0);
846 ptransfer ("sent", bytes, &start, &stop);
857 printf ("\nreceive aborted\nwaiting for remote to finish abort\n");
859 longjmp (recvabort, 1);
863 recvrequest (char *cmd, char *local, char *remote,
864 char *lmode, int printnames, int local_given)
866 FILE *fout = NULL, *din = NULL;
867 int (*closefunc) (FILE *);
868 sighand oldintr, oldintp;
869 int c, d, is_retr, tcrflag, bare_lfs = 0;
870 static size_t bufsize;
872 long bytes = 0, hashbytes = HASHBYTES;
873 struct timeval start, stop;
876 is_retr = strcmp (cmd, "RETR") == 0;
877 if (is_retr && verbose && printnames) {
878 if (local && strcmp (local, "-") != 0)
879 printf ("local: %s ", local);
881 printf ("remote: %s\n", remote);
883 if (proxy && is_retr) {
884 proxtrans (cmd, local, remote);
890 tcrflag = !crflag && is_retr;
891 if (setjmp (recvabort)) {
900 signal (SIGINT, oldintr);
904 oldintr = signal (SIGINT, abortrecv);
905 if (!local_given || (strcmp (local, "-") && *local != '|')) {
906 if (access (local, 2) < 0) {
907 char *dir = strrchr (local, '/');
909 if (errno != ENOENT && errno != EACCES) {
910 warn ("local: %s", local);
911 signal (SIGINT, oldintr);
917 d = access (dir ? local : ".", 2);
921 warn ("local: %s", local);
922 signal (SIGINT, oldintr);
926 if (!runique && errno == EACCES &&
927 chmod (local, 0600) < 0) {
928 warn ("local: %s", local);
929 signal (SIGINT, oldintr);
930 signal (SIGINT, oldintr);
934 if (runique && errno == EACCES &&
935 (local = gunique (local)) == NULL) {
936 signal (SIGINT, oldintr);
940 } else if (runique && (local = gunique (local)) == NULL) {
941 signal(SIGINT, oldintr);
947 if (curtype != TYPE_A)
948 changetype (TYPE_A, 0);
949 } else if (curtype != type)
950 changetype (type, 0);
952 signal (SIGINT, oldintr);
956 if (setjmp (recvabort))
958 if (is_retr && restart_point &&
959 command ("REST %ld", (long) restart_point) != CONTINUE)
962 if (command ("%s %s", cmd, remote) != PRELIM) {
963 signal (SIGINT, oldintr);
967 if (command ("%s", cmd) != PRELIM) {
968 signal (SIGINT, oldintr);
972 din = dataconn ("r");
975 set_buffer_size (fileno (din), 1);
976 if (local_given && strcmp (local, "-") == 0)
978 else if (local_given && *local == '|') {
979 oldintp = signal (SIGPIPE, SIG_IGN);
980 fout = popen (local + 1, "w");
982 warn ("%s", local + 1);
987 fout = fopen (local, lmode);
989 warn ("local: %s", local);
994 buf = alloc_buffer (buf, &bufsize,
995 fstat (fileno (fout), &st) >= 0 ? &st : NULL);
999 gettimeofday (&start, (struct timezone *) 0);
1004 if (restart_point &&
1005 lseek (fileno (fout), restart_point, SEEK_SET) < 0) {
1006 warn ("local: %s", local);
1007 if (closefunc != NULL)
1008 (*closefunc) (fout);
1012 while ((c = sec_read (fileno (din), buf, bufsize)) > 0) {
1013 if ((d = write (fileno (fout), buf, c)) != c)
1017 while (bytes >= hashbytes) {
1019 hashbytes += HASHBYTES;
1024 if (hash && bytes > 0) {
1025 if (bytes < HASHBYTES)
1037 warn ("local: %s", local);
1039 warnx ("%s: short write", local);
1044 if (restart_point) {
1047 if (fseek (fout, 0L, SEEK_SET) < 0)
1050 for (i = 0; i++ < n;) {
1051 if ((ch = sec_getc (fout)) == EOF)
1056 if (fseek (fout, 0L, SEEK_CUR) < 0) {
1058 warn ("local: %s", local);
1059 if (closefunc != NULL)
1060 (*closefunc) (fout);
1064 while ((c = sec_getc(din)) != EOF) {
1068 while (hash && (bytes >= hashbytes)) {
1071 hashbytes += HASHBYTES;
1074 if ((c = sec_getc (din)) != '\n' || tcrflag) {
1092 printf ("WARNING! %d bare linefeeds received in ASCII mode\n",
1094 printf ("File may not have transferred correctly.\n");
1097 if (bytes < hashbytes)
1108 warn ("local: %s", local);
1111 if (closefunc != NULL)
1112 (*closefunc) (fout);
1113 signal (SIGINT, oldintr);
1115 signal (SIGPIPE, oldintp);
1117 gettimeofday (&stop, (struct timezone *) 0);
1119 if (bytes > 0 && is_retr)
1120 ptransfer ("received", bytes, &start, &stop);
1124 /* abort using RFC959 recommended IP,SYNC sequence */
1127 signal (SIGPIPE, oldintr);
1128 signal (SIGINT, SIG_IGN);
1131 signal (SIGINT, oldintr);
1140 if (closefunc != NULL && fout != NULL)
1141 (*closefunc) (fout);
1144 gettimeofday (&stop, (struct timezone *) 0);
1146 ptransfer ("received", bytes, &start, &stop);
1147 signal (SIGINT, oldintr);
1151 parse_epsv (const char *str)
1164 port = strtol (str, &end, 0);
1167 if (end[0] != sep || end[1] != '\0')
1173 parse_pasv (struct sockaddr_in *sin4, const char *str)
1175 int a0, a1, a2, a3, p0, p1;
1178 * What we've got at this point is a string of comma separated
1179 * one-byte unsigned integer values. The first four are the an IP
1180 * address. The fifth is the MSB of the port number, the sixth is the
1181 * LSB. From that we'll prepare a sockaddr_in.
1184 if (sscanf (str, "%d,%d,%d,%d,%d,%d",
1185 &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
1186 printf ("Passive mode address scan failure. "
1187 "Shouldn't happen!\n");
1190 if (a0 < 0 || a0 > 255 ||
1191 a1 < 0 || a1 > 255 ||
1192 a2 < 0 || a2 > 255 ||
1193 a3 < 0 || a3 > 255 ||
1194 p0 < 0 || p0 > 255 ||
1195 p1 < 0 || p1 > 255) {
1196 printf ("Can't parse passive mode string.\n");
1199 memset (sin4, 0, sizeof(*sin4));
1200 sin4->sin_family = AF_INET;
1201 sin4->sin_addr.s_addr = htonl ((a0 << 24) | (a1 << 16) |
1203 sin4->sin_port = htons ((p0 << 8) | p1);
1212 data = socket (myctladdr->sa_family, SOCK_STREAM, 0);
1217 if (options & SO_DEBUG)
1218 socket_set_debug (data);
1219 if (command ("EPSV") != COMPLETE) {
1220 if (command ("PASV") != COMPLETE) {
1221 printf ("Passive mode refused.\n");
1227 * Parse the reply to EPSV or PASV
1230 port = parse_epsv (pasv);
1232 data_addr->sa_family = myctladdr->sa_family;
1233 socket_set_address_and_port (data_addr,
1234 socket_get_address (hisctladdr),
1237 if (parse_pasv ((struct sockaddr_in *)data_addr, pasv) < 0)
1241 if (connect (data, data_addr, socket_sockaddr_size (data_addr)) < 0) {
1245 #ifdef IPTOS_THROUGHPUT
1246 socket_set_tos (data, IPTOS_THROUGHPUT);
1265 data_addr->sa_family = myctladdr->sa_family;
1266 socket_set_address_and_port (data_addr, socket_get_address (myctladdr),
1267 sendport ? 0 : socket_get_port (myctladdr));
1271 data = socket (data_addr->sa_family, SOCK_STREAM, 0);
1279 socket_set_reuseaddr (data, 1);
1280 if (bind (data, data_addr, socket_sockaddr_size (data_addr)) < 0) {
1284 if (options & SO_DEBUG)
1285 socket_set_debug (data);
1286 len = sizeof (data_addr_ss);
1287 if (getsockname (data, data_addr, &len) < 0) {
1288 warn ("getsockname");
1291 if (listen (data, 1) < 0)
1298 if (inet_ntop (data_addr->sa_family, socket_get_address (data_addr),
1299 addr_str, sizeof(addr_str)) == NULL)
1300 errx (1, "inet_ntop failed");
1301 switch (data_addr->sa_family) {
1311 errx (1, "bad address family %d", data_addr->sa_family);
1319 result = command ("EPRT |%d|%s|%d|",
1321 ntohs(socket_get_port (data_addr)));
1324 if (result == ERROR) {
1325 struct sockaddr_in *sin4 = (struct sockaddr_in *)data_addr;
1327 unsigned int a = ntohl(sin4->sin_addr.s_addr);
1328 unsigned int p = ntohs(sin4->sin_port);
1330 if (data_addr->sa_family != AF_INET) {
1331 warnx ("remote server doesn't support EPRT");
1335 result = command("PORT %d,%d,%d,%d,%d,%d",
1342 if (result == ERROR && sendport == -1) {
1347 return (result != COMPLETE);
1349 return result != COMPLETE;
1355 #ifdef IPTOS_THROUGHPUT
1356 socket_set_tos (data, IPTOS_THROUGHPUT);
1368 * Need to start a listen on the data channel before we send the command,
1369 * otherwise the server's connect may fail.
1375 return passive_mode ();
1377 return active_mode ();
1381 dataconn (const char *lmode)
1383 struct sockaddr_storage from_ss;
1384 struct sockaddr *from = (struct sockaddr *)&from_ss;
1385 socklen_t fromlen = sizeof(from_ss);
1389 return (fdopen (data, lmode));
1391 s = accept (data, from, &fromlen);
1394 close (data), data = -1;
1399 #ifdef IPTOS_THROUGHPUT
1400 socket_set_tos (s, IPTOS_THROUGHPUT);
1402 return (fdopen (data, lmode));
1406 ptransfer (char *direction, long int bytes,
1407 struct timeval * t0, struct timeval * t1)
1416 td.tv_sec = t1->tv_sec - t0->tv_sec;
1417 td.tv_usec = t1->tv_usec - t0->tv_usec;
1418 if (td.tv_usec < 0) {
1420 td.tv_usec += 1000000;
1422 s = td.tv_sec + (td.tv_usec / 1000000.);
1423 bs = bytes / (s ? s : 1);
1424 if (bs >= 1048576) {
1428 } else if (bs >= 1024) {
1437 printf ("%ld bytes %s in %.3g seconds (%.*f %sbyte/s)\n",
1438 bytes, direction, s, prec, bs, unit);
1453 static struct comvars {
1455 char name[MaxHostNameLen];
1456 struct sockaddr_storage mctl;
1457 struct sockaddr_storage hctl;
1470 char mi[MaxPathLen];
1471 char mo[MaxPathLen];
1472 } proxstruct, tmpstruct;
1473 struct comvars *ip, *op;
1476 oldintr = signal (SIGINT, psabort);
1490 ip->connect = connected;
1491 connected = op->connect;
1493 strlcpy (ip->name, hostname, sizeof (ip->name));
1496 hostname = op->name;
1497 ip->hctl = hisctladdr_ss;
1498 hisctladdr_ss = op->hctl;
1499 ip->mctl = myctladdr_ss;
1500 myctladdr_ss = op->mctl;
1507 ip->curtpe = curtype;
1508 curtype = op->curtpe;
1511 ip->sunqe = sunique;
1512 sunique = op->sunqe;
1513 ip->runqe = runique;
1514 runique = op->runqe;
1519 strlcpy (ip->nti, ntin, sizeof (ip->nti));
1520 strlcpy (ntin, op->nti, 17);
1521 strlcpy (ip->nto, ntout, sizeof (ip->nto));
1522 strlcpy (ntout, op->nto, 17);
1523 ip->mapflg = mapflag;
1524 mapflag = op->mapflg;
1525 strlcpy (ip->mi, mapin, MaxPathLen);
1526 strlcpy (mapin, op->mi, MaxPathLen);
1527 strlcpy (ip->mo, mapout, MaxPathLen);
1528 strlcpy (mapout, op->mo, MaxPathLen);
1529 signal(SIGINT, oldintr);
1532 (*oldintr) (SIGINT);
1545 longjmp (ptabort, 1);
1549 proxtrans (char *cmd, char *local, char *remote)
1551 sighand oldintr = NULL;
1552 int secndflag = 0, prox_type, nfnd;
1556 if (strcmp (cmd, "RETR"))
1559 cmd2 = runique ? "STOU" : "STOR";
1560 if ((prox_type = type) == 0) {
1561 if (unix_server && unix_proxy)
1566 if (curtype != prox_type)
1567 changetype (prox_type, 1);
1568 if (command ("PASV") != COMPLETE) {
1569 printf ("proxy server does not support third party transfers.\n");
1574 printf ("No primary connection\n");
1579 if (curtype != prox_type)
1580 changetype (prox_type, 1);
1581 if (command ("PORT %s", pasv) != COMPLETE) {
1585 if (setjmp (ptabort))
1587 oldintr = signal (SIGINT, abortpt);
1588 if (command ("%s %s", cmd, remote) != PRELIM) {
1589 signal (SIGINT, oldintr);
1596 if (command ("%s %s", cmd2, local) != PRELIM)
1602 signal (SIGINT, oldintr);
1605 printf ("local: %s remote: %s\n", local, remote);
1608 signal (SIGINT, SIG_IGN);
1610 if (strcmp (cmd, "RETR") && !proxy)
1612 else if (!strcmp (cmd, "RETR") && proxy)
1614 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1615 if (command ("%s %s", cmd2, local) != PRELIM) {
1618 abort_remote ((FILE *) NULL);
1624 signal (SIGINT, oldintr);
1628 abort_remote ((FILE *) NULL);
1630 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1631 if (command ("%s %s", cmd2, local) != PRELIM) {
1634 abort_remote ((FILE *) NULL);
1638 signal (SIGINT, oldintr);
1643 abort_remote ((FILE *) NULL);
1647 if (fileno(cin) >= FD_SETSIZE)
1648 errx (1, "fd too large");
1649 FD_SET (fileno (cin), &mask);
1650 if ((nfnd = empty (&mask, 10)) <= 0) {
1666 signal (SIGINT, oldintr);
1670 reset (int argc, char **argv)
1677 if (fileno (cin) >= FD_SETSIZE)
1678 errx (1, "fd too large");
1679 FD_SET (fileno (cin), &mask);
1680 if ((nfnd = empty (&mask, 0)) < 0) {
1691 gunique (char *local)
1693 static char new[MaxPathLen];
1694 char *cp = strrchr (local, '/');
1700 d = access (cp ? local : ".", 2);
1704 warn ("local: %s", local);
1707 strlcpy (new, local, sizeof(new));
1708 cp = new + strlen(new);
1711 if (++count == 100) {
1712 printf ("runique: can't find unique file name.\n");
1721 if ((d = access (new, 0)) < 0)
1725 else if (*(cp - 2) == '.')
1728 *(cp - 2) = *(cp - 2) + 1;
1736 abort_remote (FILE * din)
1743 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1744 * after urgent byte rather than before as is protocol now
1746 snprintf (buf, sizeof (buf), "%c%c%c", IAC, IP, IAC);
1747 if (send (fileno (cout), buf, 3, MSG_OOB) != 3)
1749 fprintf (cout, "%c", DM);
1750 sec_fprintf(cout, "ABOR");
1752 fprintf (cout, "\r\n");
1755 if (fileno (cin) >= FD_SETSIZE)
1756 errx (1, "fd too large");
1757 FD_SET (fileno (cin), &mask);
1759 if (fileno (din) >= FD_SETSIZE)
1760 errx (1, "fd too large");
1761 FD_SET (fileno (din), &mask);
1763 if ((nfnd = empty (&mask, 10)) <= 0) {
1771 if (din && FD_ISSET (fileno (din), &mask)) {
1772 while (read (fileno (din), buf, BUFSIZ) > 0)
1775 if (getreply (0) == ERROR && code == 552) {
1776 /* 552 needed for nic style abort */