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,v 1.60.2.1 2000/06/23 02:45:40 assar Exp $");
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 struct hostent *hp = NULL;
60 static char hostnamebuf[MaxHostNameLen];
68 hp = getipnodebyname (host, AF_INET6, 0, &error);
71 hp = getipnodebyname (host, AF_INET, 0, &error);
74 warnx ("%s: %s", host, hstrerror(error));
78 strlcpy (hostnamebuf, hp->h_name, sizeof(hostnamebuf));
79 hostname = hostnamebuf;
80 af = hisctladdr->sa_family = hp->h_addrtype;
82 for (h = hp->h_addr_list;
86 s = socket (af, SOCK_STREAM, 0);
94 socket_set_address_and_port (hisctladdr, *h, port);
96 ret = connect (s, hisctladdr, socket_sockaddr_size(hisctladdr));
100 if (inet_ntop (af, socket_get_address(hisctladdr),
101 addr, sizeof(addr)) == NULL)
102 strlcpy (addr, "unknown address",
104 warn ("connect %s", addr);
117 len = sizeof(myctladdr_ss);
118 if (getsockname (s, myctladdr, &len) < 0) {
119 warn ("getsockname");
124 #ifdef IPTOS_LOWDELAY
125 socket_set_tos (s, IPTOS_LOWDELAY);
127 cin = fdopen (s, "r");
128 cout = fdopen (s, "w");
129 if (cin == NULL || cout == NULL) {
130 warnx ("fdopen failed.");
139 printf ("Connected to %s.\n", hostname);
140 if (getreply (0) > 2) { /* read startup message from server */
148 #if defined(SO_OOBINLINE) && defined(HAVE_SETSOCKOPT)
152 if (setsockopt (s, SOL_SOCKET, SO_OOBINLINE, (char *) &on, sizeof (on))
157 #endif /* SO_OOBINLINE */
169 char defaultpass[128];
170 char *user, *pass, *acct;
174 struct passwd *pw = k_getpwuid(getuid());
177 myname = pw->pw_name;
179 user = pass = acct = 0;
182 printf("\n*** Using plaintext user and password ***\n\n");
184 printf("Authentication successful.\n\n");
187 if (ruserpass (host, &user, &pass, &acct) < 0) {
191 while (user == NULL) {
193 printf ("Name (%s:%s): ", host, myname);
195 printf ("Name (%s): ", host);
197 if (fgets (tmp, sizeof (tmp) - 1, stdin) != NULL)
198 tmp[strlen (tmp) - 1] = '\0';
204 strlcpy(username, user, sizeof(username));
205 n = command("USER %s", user);
210 (!strcmp(user, "ftp") || !strcmp(user, "anonymous"))) {
211 snprintf(defaultpass, sizeof(defaultpass),
212 "%s@%s", myname, mydomain);
213 snprintf(prompt, sizeof(prompt),
214 "Password (%s): ", defaultpass);
215 } else if (sec_complete) {
219 snprintf(prompt, sizeof(prompt), "Password: ");
223 des_read_pw_string (tmp, sizeof (tmp), prompt, 0);
228 n = command ("PASS %s", pass);
233 des_read_pw_string (acct, 128, "Account:", 0);
234 n = command ("ACCT %s", acct);
237 warnx ("Login failed.");
240 if (!aflag && acct != NULL)
241 command ("ACCT %s", acct);
244 for (n = 0; n < macnum; ++n) {
245 if (!strcmp("init", macros[n].mac_name)) {
246 strlcpy (line, "$init", sizeof (line));
248 domacro(margc, margv);
252 sec_set_protection_level ();
264 longjmp (ptabort, 1);
268 command (char *fmt,...)
276 warn ("No control connection for command");
280 oldintr = signal(SIGINT, cmdabort);
284 if (strncmp("PASS ", fmt, 5) == 0)
287 vfprintf(stdout, fmt, ap);
290 sec_vfprintf(cout, fmt, ap);
296 fprintf (cout, "\r\n");
299 r = getreply (!strcmp (fmt, "QUIT"));
300 if (abrtflag && oldintr != SIG_IGN)
302 signal (SIGINT, oldintr);
306 char reply_string[BUFSIZ]; /* last line of previous reply */
309 getreply (int expecteof)
314 struct sigaction sa, osa;
317 sigemptyset (&sa.sa_mask);
319 sa.sa_handler = cmdabort;
320 sigaction (SIGINT, &sa, &osa);
329 sigaction (SIGINT, &osa, NULL);
335 printf ("421 Service not available, "
336 "remote server has closed connection\n");
343 if (c == WILL || c == WONT)
344 fprintf (cout, "%c%c%c", IAC, DONT, getc (cin));
345 if (c == DO || c == DONT)
346 fprintf (cout, "%c%c%c", IAC, WONT, getc (cin));
351 sscanf(buf, "%d", &code);
353 sec_read_msg(buf, prot_safe);
354 sscanf(buf, "%d", &code);
356 } else if(code == 632){
357 sec_read_msg(buf, prot_private);
358 sscanf(buf, "%d", &code);
360 }else if(code == 633){
361 sec_read_msg(buf, prot_confidential);
362 sscanf(buf, "%d", &code);
364 }else if(sec_complete)
368 if (verbose > 0 || (verbose > -1 && code > 499))
369 fprintf (stdout, "%s%s\n", lead_string, buf);
371 strcpy (reply_string, buf);
374 sigaction (SIGINT, &osa, NULL);
379 osa.sa_handler != cmdabort &&
380 osa.sa_handler != SIG_IGN)
381 osa.sa_handler (SIGINT);
383 if (code == 227 || code == 229) {
387 p = strchr (reply_string, '(');
392 memcpy (pasv, p, q - p);
400 if(verbose > 0 || (verbose > -1 && code > 499)){
402 fprintf(stdout, "!!");
403 fprintf(stdout, "%s\n", buf);
418 getreply (int expecteof)
422 int originalcode = 0, continuation = 0;
425 char *cp, *pt = pasv;
427 oldintr = signal (SIGINT, cmdabort);
431 while ((c = getc (cin)) != '\n') {
432 if (c == IAC) { /* handle telnet commands */
433 switch (c = getc (cin)) {
437 fprintf (cout, "%c%c%c", IAC, DONT, c);
443 fprintf (cout, "%c%c%c", IAC, WONT, c);
454 signal (SIGINT, oldintr);
460 printf ("421 Service not available, remote server has closed connection\n");
466 if (c != '\r' && (verbose > 0 ||
467 (verbose > -1 && n == '5' && dig > 4))) {
469 (dig == 1 || dig == 5 && verbose == 0))
470 printf ("%s:", hostname);
473 if (dig < 4 && isdigit (c))
474 code = code * 10 + (c - '0');
475 if (!pflag && code == 227)
477 if (dig > 4 && pflag == 1 && isdigit (c))
480 if (c != '\r' && c != ')')
487 if (dig == 4 && c == '-') {
494 if (cp < &reply_string[sizeof (reply_string) - 1])
497 if (verbose > 0 || verbose > -1 && n == '5') {
501 if (continuation && code != originalcode) {
502 if (originalcode == 0)
509 sec_read_msg(reply_string, prot_safe);
511 sec_read_msg(reply_string, prot_private);
513 sec_read_msg(reply_string, prot_confidential);
514 n = code / 100 + '0';
518 signal (SIGINT, oldintr);
519 if (code == 421 || originalcode == 421)
521 if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
530 empty (fd_set * mask, int sec)
534 t.tv_sec = (long) sec;
536 return (select (32, mask, NULL, NULL, &t));
547 printf ("\nsend aborted\nwaiting for remote to finish abort\n");
549 longjmp (sendabort, 1);
552 #define HASHBYTES 1024
555 copy_stream (FILE * from, FILE * to)
557 static size_t bufsize;
562 int hashbytes = HASHBYTES;
565 #if defined(HAVE_MMAP) && !defined(NO_MMAP)
569 #define MAP_FAILED (-1)
572 if (fstat (fileno (from), &st) == 0 && S_ISREG (st.st_mode)) {
574 * mmap zero bytes has potential of loosing, don't do it.
578 chunk = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fileno (from), 0);
579 if (chunk != (void *) MAP_FAILED) {
582 res = sec_write (fileno (to), chunk, st.st_size);
583 if (munmap (chunk, st.st_size) < 0)
591 buf = alloc_buffer (buf, &bufsize,
592 fstat (fileno (from), &st) >= 0 ? &st : NULL);
596 while ((n = read (fileno (from), buf, bufsize)) > 0) {
597 werr = sec_write (fileno (to), buf, n);
601 while (hash && bytes > hashbytes) {
603 hashbytes += HASHBYTES;
619 sendrequest (char *cmd, char *local, char *remote, char *lmode, int printnames)
622 struct timeval start, stop;
624 FILE *fin, *dout = 0;
625 int (*closefunc) (FILE *);
626 RETSIGTYPE (*oldintr)(), (*oldintp)();
627 long bytes = 0, hashbytes = HASHBYTES;
630 if (verbose && printnames) {
631 if (local && strcmp (local, "-") != 0)
632 printf ("local: %s ", local);
634 printf ("remote: %s\n", remote);
637 proxtrans (cmd, local, remote);
641 changetype (type, 0);
646 if (setjmp (sendabort)) {
655 signal (SIGINT, oldintr);
657 signal (SIGPIPE, oldintp);
661 oldintr = signal (SIGINT, abortsend);
662 if (strcmp (local, "-") == 0)
664 else if (*local == '|') {
665 oldintp = signal (SIGPIPE, SIG_IGN);
666 fin = popen (local + 1, lmode);
668 warn ("%s", local + 1);
669 signal (SIGINT, oldintr);
670 signal (SIGPIPE, oldintp);
676 fin = fopen (local, lmode);
678 warn ("local: %s", local);
679 signal (SIGINT, oldintr);
684 if (fstat (fileno (fin), &st) < 0 ||
685 (st.st_mode & S_IFMT) != S_IFREG) {
686 fprintf (stdout, "%s: not a plain file.\n", local);
687 signal (SIGINT, oldintr);
694 signal (SIGINT, oldintr);
696 signal (SIGPIPE, oldintp);
698 if (closefunc != NULL)
702 if (setjmp (sendabort))
706 (strcmp (cmd, "STOR") == 0 || strcmp (cmd, "APPE") == 0)) {
711 rc = fseek (fin, (long) restart_point, SEEK_SET);
715 rc = lseek (fileno (fin), restart_point, SEEK_SET);
719 warn ("local: %s", local);
721 if (closefunc != NULL)
725 if (command ("REST %ld", (long) restart_point)
728 if (closefunc != NULL)
736 if (command ("%s %s", cmd, remote) != PRELIM) {
737 signal (SIGINT, oldintr);
739 signal (SIGPIPE, oldintp);
740 if (closefunc != NULL)
744 } else if (command ("%s", cmd) != PRELIM) {
745 signal(SIGINT, oldintr);
747 signal(SIGPIPE, oldintp);
748 if (closefunc != NULL)
752 dout = dataconn(rmode);
755 set_buffer_size (fileno (dout), 0);
756 gettimeofday (&start, (struct timezone *) 0);
757 oldintp = signal (SIGPIPE, SIG_IGN);
763 bytes = copy_stream (fin, dout);
767 while ((c = getc (fin)) != EOF) {
769 while (hash && (bytes >= hashbytes)) {
772 hashbytes += HASHBYTES;
776 sec_putc ('\r', dout);
784 if (bytes < hashbytes)
790 warn ("local: %s", local);
798 if (closefunc != NULL)
801 gettimeofday (&stop, (struct timezone *) 0);
803 signal (SIGINT, oldintr);
805 signal (SIGPIPE, oldintp);
807 ptransfer ("sent", bytes, &start, &stop);
810 signal (SIGINT, oldintr);
812 signal (SIGPIPE, oldintp);
825 if (closefunc != NULL && fin != NULL)
827 gettimeofday (&stop, (struct timezone *) 0);
829 ptransfer ("sent", bytes, &start, &stop);
840 printf ("\nreceive aborted\nwaiting for remote to finish abort\n");
842 longjmp (recvabort, 1);
846 recvrequest (char *cmd, char *local, char *remote,
847 char *lmode, int printnames, int local_given)
849 FILE *fout, *din = 0;
850 int (*closefunc) (FILE *);
851 sighand oldintr, oldintp;
852 int c, d, is_retr, tcrflag, bare_lfs = 0;
853 static size_t bufsize;
855 long bytes = 0, hashbytes = HASHBYTES;
856 struct timeval start, stop;
859 is_retr = strcmp (cmd, "RETR") == 0;
860 if (is_retr && verbose && printnames) {
861 if (local && strcmp (local, "-") != 0)
862 printf ("local: %s ", local);
864 printf ("remote: %s\n", remote);
866 if (proxy && is_retr) {
867 proxtrans (cmd, local, remote);
873 tcrflag = !crflag && is_retr;
874 if (setjmp (recvabort)) {
883 signal (SIGINT, oldintr);
887 oldintr = signal (SIGINT, abortrecv);
888 if (!local_given || (strcmp (local, "-") && *local != '|')) {
889 if (access (local, 2) < 0) {
890 char *dir = strrchr (local, '/');
892 if (errno != ENOENT && errno != EACCES) {
893 warn ("local: %s", local);
894 signal (SIGINT, oldintr);
900 d = access (dir ? local : ".", 2);
904 warn ("local: %s", local);
905 signal (SIGINT, oldintr);
909 if (!runique && errno == EACCES &&
910 chmod (local, 0600) < 0) {
911 warn ("local: %s", local);
912 signal (SIGINT, oldintr);
913 signal (SIGINT, oldintr);
917 if (runique && errno == EACCES &&
918 (local = gunique (local)) == NULL) {
919 signal (SIGINT, oldintr);
923 } else if (runique && (local = gunique (local)) == NULL) {
924 signal(SIGINT, oldintr);
930 if (curtype != TYPE_A)
931 changetype (TYPE_A, 0);
932 } else if (curtype != type)
933 changetype (type, 0);
935 signal (SIGINT, oldintr);
939 if (setjmp (recvabort))
941 if (is_retr && restart_point &&
942 command ("REST %ld", (long) restart_point) != CONTINUE)
945 if (command ("%s %s", cmd, remote) != PRELIM) {
946 signal (SIGINT, oldintr);
950 if (command ("%s", cmd) != PRELIM) {
951 signal (SIGINT, oldintr);
955 din = dataconn ("r");
958 set_buffer_size (fileno (din), 1);
959 if (local_given && strcmp (local, "-") == 0)
961 else if (local_given && *local == '|') {
962 oldintp = signal (SIGPIPE, SIG_IGN);
963 fout = popen (local + 1, "w");
965 warn ("%s", local + 1);
970 fout = fopen (local, lmode);
972 warn ("local: %s", local);
977 buf = alloc_buffer (buf, &bufsize,
978 fstat (fileno (fout), &st) >= 0 ? &st : NULL);
982 gettimeofday (&start, (struct timezone *) 0);
988 lseek (fileno (fout), restart_point, SEEK_SET) < 0) {
989 warn ("local: %s", local);
990 if (closefunc != NULL)
995 while ((c = sec_read (fileno (din), buf, bufsize)) > 0) {
996 if ((d = write (fileno (fout), buf, c)) != c)
1000 while (bytes >= hashbytes) {
1002 hashbytes += HASHBYTES;
1007 if (hash && bytes > 0) {
1008 if (bytes < HASHBYTES)
1020 warn ("local: %s", local);
1022 warnx ("%s: short write", local);
1027 if (restart_point) {
1030 if (fseek (fout, 0L, SEEK_SET) < 0)
1033 for (i = 0; i++ < n;) {
1034 if ((ch = sec_getc (fout)) == EOF)
1039 if (fseek (fout, 0L, SEEK_CUR) < 0) {
1041 warn ("local: %s", local);
1042 if (closefunc != NULL)
1043 (*closefunc) (fout);
1047 while ((c = sec_getc(din)) != EOF) {
1051 while (hash && (bytes >= hashbytes)) {
1054 hashbytes += HASHBYTES;
1057 if ((c = sec_getc (din)) != '\n' || tcrflag) {
1075 printf ("WARNING! %d bare linefeeds received in ASCII mode\n",
1077 printf ("File may not have transferred correctly.\n");
1080 if (bytes < hashbytes)
1091 warn ("local: %s", local);
1094 if (closefunc != NULL)
1095 (*closefunc) (fout);
1096 signal (SIGINT, oldintr);
1098 signal (SIGPIPE, oldintp);
1100 gettimeofday (&stop, (struct timezone *) 0);
1102 if (bytes > 0 && is_retr)
1103 ptransfer ("received", bytes, &start, &stop);
1107 /* abort using RFC959 recommended IP,SYNC sequence */
1110 signal (SIGPIPE, oldintr);
1111 signal (SIGINT, SIG_IGN);
1114 signal (SIGINT, oldintr);
1123 if (closefunc != NULL && fout != NULL)
1124 (*closefunc) (fout);
1127 gettimeofday (&stop, (struct timezone *) 0);
1129 ptransfer ("received", bytes, &start, &stop);
1130 signal (SIGINT, oldintr);
1134 parse_epsv (const char *str)
1147 port = strtol (str, &end, 0);
1150 if (end[0] != sep || end[1] != '\0')
1156 parse_pasv (struct sockaddr_in *sin, const char *str)
1158 int a0, a1, a2, a3, p0, p1;
1161 * What we've got at this point is a string of comma separated
1162 * one-byte unsigned integer values. The first four are the an IP
1163 * address. The fifth is the MSB of the port number, the sixth is the
1164 * LSB. From that we'll prepare a sockaddr_in.
1167 if (sscanf (str, "%d,%d,%d,%d,%d,%d",
1168 &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
1169 printf ("Passive mode address scan failure. "
1170 "Shouldn't happen!\n");
1173 if (a0 < 0 || a0 > 255 ||
1174 a1 < 0 || a1 > 255 ||
1175 a2 < 0 || a2 > 255 ||
1176 a3 < 0 || a3 > 255 ||
1177 p0 < 0 || p0 > 255 ||
1178 p1 < 0 || p1 > 255) {
1179 printf ("Can't parse passive mode string.\n");
1182 memset (sin, 0, sizeof(*sin));
1183 sin->sin_family = AF_INET;
1184 sin->sin_addr.s_addr = htonl ((a0 << 24) | (a1 << 16) |
1186 sin->sin_port = htons ((p0 << 8) | p1);
1195 data = socket (myctladdr->sa_family, SOCK_STREAM, 0);
1200 if (options & SO_DEBUG)
1201 socket_set_debug (data);
1202 if (command ("EPSV") != COMPLETE) {
1203 if (command ("PASV") != COMPLETE) {
1204 printf ("Passive mode refused.\n");
1210 * Parse the reply to EPSV or PASV
1213 port = parse_epsv (pasv);
1215 data_addr->sa_family = myctladdr->sa_family;
1216 socket_set_address_and_port (data_addr,
1217 socket_get_address (hisctladdr),
1220 if (parse_pasv ((struct sockaddr_in *)data_addr, pasv) < 0)
1224 if (connect (data, data_addr, socket_sockaddr_size (data_addr)) < 0) {
1228 #ifdef IPTOS_THROUGHPUT
1229 socket_set_tos (data, IPTOS_THROUGHPUT);
1248 data_addr->sa_family = myctladdr->sa_family;
1249 socket_set_address_and_port (data_addr, socket_get_address (myctladdr),
1250 sendport ? 0 : socket_get_port (myctladdr));
1254 data = socket (data_addr->sa_family, SOCK_STREAM, 0);
1262 socket_set_reuseaddr (data, 1);
1263 if (bind (data, data_addr, socket_sockaddr_size (data_addr)) < 0) {
1267 if (options & SO_DEBUG)
1268 socket_set_debug (data);
1269 len = sizeof (data_addr_ss);
1270 if (getsockname (data, data_addr, &len) < 0) {
1271 warn ("getsockname");
1274 if (listen (data, 1) < 0)
1282 if (inet_ntop (data_addr->sa_family, socket_get_address (data_addr),
1283 addr_str, sizeof(addr_str)) == NULL)
1284 errx (1, "inet_ntop failed");
1285 switch (data_addr->sa_family) {
1295 errx (1, "bad address family %d", data_addr->sa_family);
1298 asprintf (&cmd, "EPRT |%d|%s|%d|",
1299 inet_af, addr_str, ntohs(socket_get_port (data_addr)));
1305 result = command (cmd);
1309 if (result == ERROR) {
1310 struct sockaddr_in *sin = (struct sockaddr_in *)data_addr;
1312 unsigned int a = ntohl(sin->sin_addr.s_addr);
1313 unsigned int p = ntohs(sin->sin_port);
1315 if (data_addr->sa_family != AF_INET) {
1316 warnx ("remote server doesn't support EPRT");
1320 result = command("PORT %d,%d,%d,%d,%d,%d",
1327 if (result == ERROR && sendport == -1) {
1332 return (result != COMPLETE);
1334 return result != COMPLETE;
1340 #ifdef IPTOS_THROUGHPUT
1341 socket_set_tos (data, IPTOS_THROUGHPUT);
1353 * Need to start a listen on the data channel before we send the command,
1354 * otherwise the server's connect may fail.
1360 return passive_mode ();
1362 return active_mode ();
1366 dataconn (const char *lmode)
1368 struct sockaddr_storage from_ss;
1369 struct sockaddr *from = (struct sockaddr *)&from_ss;
1370 int s, fromlen = sizeof (from_ss);
1373 return (fdopen (data, lmode));
1375 s = accept (data, from, &fromlen);
1378 close (data), data = -1;
1383 #ifdef IPTOS_THROUGHPUT
1384 socket_set_tos (s, IPTOS_THROUGHPUT);
1386 return (fdopen (data, lmode));
1390 ptransfer (char *direction, long int bytes,
1391 struct timeval * t0, struct timeval * t1)
1400 td.tv_sec = t1->tv_sec - t0->tv_sec;
1401 td.tv_usec = t1->tv_usec - t0->tv_usec;
1402 if (td.tv_usec < 0) {
1404 td.tv_usec += 1000000;
1406 s = td.tv_sec + (td.tv_usec / 1000000.);
1407 bs = bytes / (s ? s : 1);
1408 if (bs >= 1048576) {
1412 } else if (bs >= 1024) {
1421 printf ("%ld bytes %s in %.3g seconds (%.*f %sbyte/s)\n",
1422 bytes, direction, s, prec, bs, unit);
1437 static struct comvars {
1439 char name[MaxHostNameLen];
1440 struct sockaddr_storage mctl;
1441 struct sockaddr_storage hctl;
1454 char mi[MaxPathLen];
1455 char mo[MaxPathLen];
1456 } proxstruct, tmpstruct;
1457 struct comvars *ip, *op;
1460 oldintr = signal (SIGINT, psabort);
1474 ip->connect = connected;
1475 connected = op->connect;
1477 strlcpy (ip->name, hostname, sizeof (ip->name));
1480 hostname = op->name;
1481 ip->hctl = hisctladdr_ss;
1482 hisctladdr_ss = op->hctl;
1483 ip->mctl = myctladdr_ss;
1484 myctladdr_ss = op->mctl;
1491 ip->curtpe = curtype;
1492 curtype = op->curtpe;
1495 ip->sunqe = sunique;
1496 sunique = op->sunqe;
1497 ip->runqe = runique;
1498 runique = op->runqe;
1503 strlcpy (ip->nti, ntin, sizeof (ip->nti));
1504 strlcpy (ntin, op->nti, 17);
1505 strlcpy (ip->nto, ntout, sizeof (ip->nto));
1506 strlcpy (ntout, op->nto, 17);
1507 ip->mapflg = mapflag;
1508 mapflag = op->mapflg;
1509 strlcpy (ip->mi, mapin, MaxPathLen);
1510 strlcpy (mapin, op->mi, MaxPathLen);
1511 strlcpy (ip->mo, mapout, MaxPathLen);
1512 strlcpy (mapout, op->mo, MaxPathLen);
1513 signal(SIGINT, oldintr);
1516 (*oldintr) (SIGINT);
1529 longjmp (ptabort, 1);
1533 proxtrans (char *cmd, char *local, char *remote)
1536 int secndflag = 0, prox_type, nfnd;
1540 if (strcmp (cmd, "RETR"))
1543 cmd2 = runique ? "STOU" : "STOR";
1544 if ((prox_type = type) == 0) {
1545 if (unix_server && unix_proxy)
1550 if (curtype != prox_type)
1551 changetype (prox_type, 1);
1552 if (command ("PASV") != COMPLETE) {
1553 printf ("proxy server does not support third party transfers.\n");
1558 printf ("No primary connection\n");
1563 if (curtype != prox_type)
1564 changetype (prox_type, 1);
1565 if (command ("PORT %s", pasv) != COMPLETE) {
1569 if (setjmp (ptabort))
1571 oldintr = signal (SIGINT, abortpt);
1572 if (command ("%s %s", cmd, remote) != PRELIM) {
1573 signal (SIGINT, oldintr);
1580 if (command ("%s %s", cmd2, local) != PRELIM)
1586 signal (SIGINT, oldintr);
1589 printf ("local: %s remote: %s\n", local, remote);
1592 signal (SIGINT, SIG_IGN);
1594 if (strcmp (cmd, "RETR") && !proxy)
1596 else if (!strcmp (cmd, "RETR") && proxy)
1598 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1599 if (command ("%s %s", cmd2, local) != PRELIM) {
1602 abort_remote ((FILE *) NULL);
1607 signal (SIGINT, oldintr);
1611 abort_remote ((FILE *) NULL);
1613 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1614 if (command ("%s %s", cmd2, local) != PRELIM) {
1617 abort_remote ((FILE *) NULL);
1621 signal (SIGINT, oldintr);
1626 abort_remote ((FILE *) NULL);
1630 FD_SET (fileno (cin), &mask);
1631 if ((nfnd = empty (&mask, 10)) <= 0) {
1647 signal (SIGINT, oldintr);
1651 reset (int argc, char **argv)
1658 FD_SET (fileno (cin), &mask);
1659 if ((nfnd = empty (&mask, 0)) < 0) {
1670 gunique (char *local)
1672 static char new[MaxPathLen];
1673 char *cp = strrchr (local, '/');
1679 d = access (cp ? local : ".", 2);
1683 warn ("local: %s", local);
1686 strlcpy (new, local, sizeof(new));
1687 cp = new + strlen(new);
1690 if (++count == 100) {
1691 printf ("runique: can't find unique file name.\n");
1700 if ((d = access (new, 0)) < 0)
1704 else if (*(cp - 2) == '.')
1707 *(cp - 2) = *(cp - 2) + 1;
1715 abort_remote (FILE * din)
1722 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1723 * after urgent byte rather than before as is protocol now
1725 snprintf (buf, sizeof (buf), "%c%c%c", IAC, IP, IAC);
1726 if (send (fileno (cout), buf, 3, MSG_OOB) != 3)
1728 fprintf (cout, "%cABOR\r\n", DM);
1731 FD_SET (fileno (cin), &mask);
1733 FD_SET (fileno (din), &mask);
1735 if ((nfnd = empty (&mask, 10)) <= 0) {
1743 if (din && FD_ISSET (fileno (din), &mask)) {
1744 while (read (fileno (din), buf, BUFSIZ) > 0)
1747 if (getreply (0) == ERROR && code == 552) {
1748 /* 552 needed for nic style abort */