2 #define L_INCR SEEK_CUR
5 * Copyright (c) 1985, 1989 Regents of the University of California.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static char sccsid
[] = "@(#)ftp.c 5.28 (Berkeley) 4/20/89";
29 #include <sys/param.h>
30 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <netinet/in.h>
36 #include <arpa/telnet.h>
50 #include "prototypes.h"
51 #ifndef MAXHOSTNAMELEN
52 #define MAXHOSTNAMELEN 64
56 #define vfprintf(a,b,c) _doprnt(b,c,a)
60 /* FD_SET wasn't defined until 4.0. its a cheap test for uid_t presence */
62 #define NBBY 8 /* number of bits in a byte */
64 * Select uses bit masks of file descriptors in longs.
65 * These macros manipulate such bit fields (the filesystem macros use chars).
66 * FD_SETSIZE may be defined by the user, but the default here
67 * should be >= NOFILE (param.h).
70 #define FD_SETSIZE 256
74 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
76 #define howmany(x, y) (((x)+((y)-1))/(y))
79 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
80 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
81 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
82 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
89 struct sockaddr_in hisctladdr
;
90 struct sockaddr_in data_addr
;
95 struct sockaddr_in myctladdr
;
98 off_t restart_point
= 0;
101 int dataconn(char *mode
);
103 int command(char *fmt
, ...);
107 typedef void (*Sig_t
)(int);
111 void psabort(int sig
);
113 char *hookup(char *host
, int port
)
115 register struct hostent
*hp
= 0;
118 static char hostnamebuf
[80];
120 bzero((char *)&hisctladdr
, sizeof (hisctladdr
));
121 hisctladdr
.sin_addr
.s_addr
= inet_addr(host
);
122 if (hisctladdr
.sin_addr
.s_addr
!= -1) {
123 hisctladdr
.sin_family
= AF_INET
;
124 (void) strncpy(hostnamebuf
, host
, sizeof(hostnamebuf
));
126 hp
= gethostbyname(host
);
128 fprintf(stderr
, "ftp: %s: ", host
);
129 herror((char *)NULL
);
133 hisctladdr
.sin_family
= hp
->h_addrtype
;
134 bcopy(hp
->h_addr_list
[0],
135 (caddr_t
)&hisctladdr
.sin_addr
, hp
->h_length
);
136 (void) strncpy(hostnamebuf
, hp
->h_name
, sizeof(hostnamebuf
));
138 hostname
= hostnamebuf
;
139 s
= socket(hisctladdr
.sin_family
, SOCK_STREAM
, 0);
140 if (s
== INVALID_SOCKET
) {
141 perror("ftp: socket");
145 hisctladdr
.sin_port
= port
;
146 while (connect(s
, (struct sockaddr
*)&hisctladdr
, sizeof (hisctladdr
)) < 0) {
147 if (hp
&& hp
->h_addr_list
[1]) {
150 fprintf(stderr
, "ftp: connect to address %s: ",
151 inet_ntoa(hisctladdr
.sin_addr
));
155 bcopy(hp
->h_addr_list
[0],
156 (caddr_t
)&hisctladdr
.sin_addr
, hp
->h_length
);
157 fprintf(stdout
, "Trying %s...\n",
158 inet_ntoa(hisctladdr
.sin_addr
));
159 (void) fflush(stdout
);
161 s
= socket(hisctladdr
.sin_family
, SOCK_STREAM
, 0);
163 perror("ftp: socket");
169 perror("ftp: connect");
173 len
= sizeof (myctladdr
);
174 if (getsockname(s
, (struct sockaddr
*)&myctladdr
, &len
) < 0) {
175 perror("ftp: getsockname");
181 printf("Connected to %s.\n", hostname
);
182 (void) fflush(stdout
);
184 if (getreply(0) > 2) { /* read startup message from server */
193 if (setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, (const char *) &on
, sizeof(on
))
195 perror("ftp: setsockopt");
198 #endif //SO_OOBINLINE
206 int login(char *host
)
209 char *user
, *pass
, *acct
;
212 user
= pass
= acct
= 0;
213 if (ruserpass(host
, &user
, &pass
, &acct
) < 0) {
217 while (user
== NULL
) {
218 char *myname
= "none"; // This needs to become the usename env
221 printf("Name (%s:%s): ", host
, myname
);
223 printf("Name (%s): ", host
);
224 (void) fflush(stdout
);
225 (void) fgets(tmp
, sizeof(tmp
) - 1, stdin
);
226 tmp
[strlen(tmp
) - 1] = '\0';
232 n
= command("USER %s", user
);
235 pass
= getpass("Password:");
236 n
= command("PASS %s", pass
);
241 acct
= getpass("Account:");
242 n
= command("ACCT %s", acct
);
245 fprintf(stderr
, "Login failed.\n");
248 if (!aflag
&& acct
!= NULL
)
249 (void) command("ACCT %s", acct
);
252 for (n
= 0; n
< macnum
; ++n
) {
253 if (!strcmp("init", macros
[n
].mac_name
)) {
254 (void) strcpy(line
, "$init");
256 domacro(margc
, margv
);
266 extern jmp_buf ptabort
;
269 (void) fflush(stdout
);
276 int command(char *fmt
, ...)
280 void (*oldintr
)(int), cmdabort(int);
286 vfprintf(stdout
, fmt
, ap
);
289 (void) fflush(stdout
);
291 if (cout
== (int) NULL
) {
292 perror ("No control connection for command");
296 oldintr
= signal(SIGINT
,cmdabort
);
301 vsprintf(buffer
, fmt
, ap
);
303 //DLJ: to work through firewalls - send the command as a single message
304 strcat(buffer
,"\r\n");
305 fprintfSocket(cout
, buffer
);
307 //DLJ: the following two lines are replaced by the strcat above - seems to
308 // make it work through firewalls.
309 // fprintfSocket(cout, "\r\n");
310 // (void) fflush(cout);
312 r
= getreply(!strcmp(fmt
, "QUIT"));
313 if (abrtflag
&& oldintr
!= SIG_IGN
)
315 // (void) signal(SIGINT, oldintr);
319 char reply_string
[BUFSIZ
]; /* last line of previous reply */
329 int originalcode
= 0, continuation
= 0;
330 void (*oldintr
)(int), cmdabort(int);
334 oldintr
= signal(SIGINT
,cmdabort
);
338 while ((c
= fgetcSocket(cin
)) != '\n') {
339 if (c
== IAC
) { /* handle telnet commands */
340 switch (c
= fgetcSocket(cin
)) {
343 c
= fgetcSocket(cin
);
344 fprintfSocket(cout
, "%c%c%c",IAC
,DONT
,c
);
348 c
= fgetcSocket(cin
);
349 fprintfSocket(cout
, "%c%c%c",IAC
,WONT
,c
);
359 // (void) signal(SIGINT,oldintr);
365 printf("421 Service not available, remote server has closed connection\n");
366 (void) fflush(stdout
);
371 if (c
!= '\r' && (verbose
> 0 ||
372 (verbose
> -1 && n
== '5' && dig
> 4))) {
374 (dig
== 1 || dig
== 5 && verbose
== 0))
375 printf("%s:",hostname
);
377 (void) fflush(stdout
);
379 if (dig
< 4 && isdigit(c
))
380 code
= code
* 10 + (c
- '0');
381 if (!pflag
&& code
== 227)
383 if (dig
> 4 && pflag
== 1 && isdigit(c
))
386 if (c
!= '\r' && c
!= ')')
393 if (dig
== 4 && c
== '-') {
400 if (cp
< &reply_string
[sizeof(reply_string
) - 1])
403 if (verbose
> 0 || verbose
> -1 && n
== '5') {
405 (void) fflush (stdout
);
407 if (continuation
&& code
!= originalcode
) {
408 if (originalcode
== 0)
415 (void) signal(SIGINT
,oldintr
);
416 if (code
== 421 || originalcode
== 421)
418 if (abrtflag
&& oldintr
!= cmdabort
&& oldintr
!= SIG_IGN
)
430 t
.tv_sec
= (long) sec
;
432 return(select(32, mask
, (struct fd_set
*) 0, (struct fd_set
*) 0, &t
));
442 printf("\nsend aborted\n");
443 (void) fflush(stdout
);
444 longjmp(sendabort
, 1);
447 #define HASHBYTES 1024
449 void sendrequest(char *cmd
, char *local
, char *remote
, int printnames
)
453 int (*closefunc
)(), _pclose(), fclose();
454 sig_t (*oldintr
)(), (*oldintp
)();
456 char buf
[BUFSIZ
], *bufp
;
457 long bytes
= 0, hashbytes
= HASHBYTES
;
460 struct timeval start
, stop
;
463 if (verbose
&& printnames
) {
464 if (local
&& *local
!= '-')
465 printf("local: %s ", local
);
467 printf("remote: %s\n", remote
);
468 (void) fflush(stdout
);
471 proxtrans(cmd
, local
, remote
);
478 if (setjmp(sendabort
)) {
487 null();// (void) signal(SIGINT,oldintr);
489 null();// (void) signal(SIGPIPE,oldintp);
493 null();// oldintr = signal(SIGINT, abortsend);
494 if (strcmp(local
, "-") == 0)
496 else if (*local
== '|') {
497 null();// oldintp = signal(SIGPIPE,SIG_IGN);
498 fin
= _popen(local
+ 1, "r");
501 null();// (void) signal(SIGINT, oldintr);
502 null();// (void) signal(SIGPIPE, oldintp);
508 fin
= fopen(local
, "r");
511 null();// (void) signal(SIGINT, oldintr);
516 if (fstat(fileno(fin
), &st
) < 0 ||
517 (st
.st_mode
&S_IFMT
) != S_IFREG
) {
518 fprintf(stdout
, "%s: not a plain file.\n", local
);
519 (void) fflush(stdout
);
520 null();// (void) signal(SIGINT, oldintr);
527 null();// (void) signal(SIGINT, oldintr);
529 null();// (void) signal(SIGPIPE, oldintp);
531 if (closefunc
!= NULL
)
535 if (setjmp(sendabort
))
539 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
540 if (fseek(fin
, (long) restart_point
, 0) < 0) {
543 if (closefunc
!= NULL
)
547 if (command("REST %ld", (long) restart_point
)
550 if (closefunc
!= NULL
)
558 if (command("%s %s", cmd
, remote
) != PRELIM
) {
559 null();// (void) signal(SIGINT, oldintr);
561 null();// (void) signal(SIGPIPE, oldintp);
562 if (closefunc
!= NULL
)
567 if (command("%s", cmd
) != PRELIM
) {
568 null();// (void) signal(SIGINT, oldintr);
570 null();// (void) signal(SIGPIPE, oldintp);
571 if (closefunc
!= NULL
)
575 dout
= dataconn(mode
);
576 if (dout
== (int)NULL
)
578 (void) gettimeofday(&start
, (struct timezone
*)0);
579 null();// oldintp = signal(SIGPIPE, SIG_IGN);
585 while ((c
= read(fileno(fin
), buf
, sizeof (buf
))) > 0) {
587 for (bufp
= buf
; c
> 0; c
-= d
, bufp
+= d
)
588 if ((d
= send(dout
, bufp
, c
, 0)) <= 0)
591 while (bytes
>= hashbytes
) {
593 hashbytes
+= HASHBYTES
;
595 (void) fflush(stdout
);
598 if (hash
&& bytes
> 0) {
599 if (bytes
< HASHBYTES
)
601 (void) putchar('\n');
602 (void) fflush(stdout
);
608 fprintf(stderr
, "netout: write returned 0?\n");
609 else if (errno
!= EPIPE
)
618 static int bufsize
= 1024;
621 while ((c
= getc(fin
)) != EOF
) {
623 while (hash
&& (bytes
>= hashbytes
)) {
625 (void) fflush(stdout
);
626 hashbytes
+= HASHBYTES
;
628 // Szurgot: The following code is unncessary on Win32.
629 // (void) fputcSocket(dout, '\r');
633 if (ipos
>= bufsize
) {
634 fputSocket(dout
,buf
,ipos
);
635 if(!hash
) (void) putchar('.');
642 fputSocket(dout
,buf
,ipos
);
646 if (bytes
< hashbytes
)
648 (void) putchar('\n');
649 (void) fflush(stdout
);
653 (void) putchar('\n');
654 (void) fflush(stdout
);
658 // if (ferror(dout)) {
659 // if (errno != EPIPE)
666 (void) gettimeofday(&stop
, (struct timezone
*)0);
667 if (closefunc
!= NULL
)
669 if(closesocket(dout
)) {
670 int iret
=WSAGetLastError ();
671 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
672 (void) fflush(stdout
);
675 null();// (void) signal(SIGINT, oldintr);
677 null();// (void) signal(SIGPIPE, oldintp);
679 ptransfer("sent", bytes
, &start
, &stop
);
682 (void) gettimeofday(&stop
, (struct timezone
*)0);
683 null();// (void) signal(SIGINT, oldintr);
685 null();// (void) signal(SIGPIPE, oldintp);
695 if(closesocket(dout
)) {
696 int iret
=WSAGetLastError ();
697 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
698 (void) fflush(stdout
);
703 if (closefunc
!= NULL
&& fin
!= NULL
)
706 ptransfer("sent", bytes
, &start
, &stop
);
718 (void) fflush(stdout
);
719 longjmp(recvabort
, 1);
722 void recvrequest(char *cmd
, char *local
, char *remote
, char *mode
,
727 int (*closefunc
)(), _pclose(), fclose();
728 void (*oldintr
)(int), (*oldintp
)(int);
730 int oldverbose
, oldtype
= 0, is_retr
, tcrflag
, nfnd
, bare_lfs
= 0;
731 char *gunique(), msg
;
732 // static char *buf; // Szurgot: Shouldn't this go SOMEWHERE?
734 static int bufsize
= 1024;
735 long bytes
= 0, hashbytes
= HASHBYTES
;
739 struct timeval start
, stop
;
741 extern void *malloc();
743 is_retr
= strcmp(cmd
, "RETR") == 0;
744 if (is_retr
&& verbose
&& printnames
) {
745 if (local
&& *local
!= '-')
746 printf("local: %s ", local
);
748 printf("remote: %s\n", remote
);
749 (void) fflush(stdout
);
751 if (proxy
&& is_retr
) {
752 proxtrans(cmd
, local
, remote
);
758 tcrflag
= !crflag
&& is_retr
;
759 if (setjmp(recvabort
)) {
768 null();// (void) signal(SIGINT, oldintr);
772 null();// oldintr = signal(SIGINT, abortrecv);
773 if (strcmp(local
, "-") && *local
!= '|') {
775 // This whole thing is a problem... access Won't work on non-existent files
776 if (access(local
, 2) < 0) {
777 char *dir
= rindex(local
, '/');
779 if (errno
!= ENOENT
&& errno
!= EACCES
) {
781 (void) signal(SIGINT
, oldintr
);
787 d
= access(dir
? local
: ".", 2);
792 (void) signal(SIGINT
, oldintr
);
796 if (!runique
&& errno
== EACCES
&&
797 chmod(local
, 0600) < 0) {
799 (void) signal(SIGINT
, oldintr
);
803 if (runique
&& errno
== EACCES
&&
804 (local
= gunique(local
)) == NULL
) {
805 (void) signal(SIGINT
, oldintr
);
810 else if (runique
&& (local
= gunique(local
)) == NULL
) {
811 (void) signal(SIGINT
, oldintr
);
818 null();// (void) signal(SIGINT, oldintr);
822 if (setjmp(recvabort
))
825 if (type
!= TYPE_A
&& (allbinary
== 0 || type
!= TYPE_I
)) {
827 oldverbose
= verbose
;
831 verbose
= oldverbose
;
833 } else if (restart_point
) {
834 if (command("REST %ld", (long) restart_point
) != CONTINUE
)
838 if (command("%s %s", cmd
, remote
) != PRELIM
) {
839 null();// (void) signal(SIGINT, oldintr);
854 verbose
= oldverbose
;
859 if (command("%s", cmd
) != PRELIM
) {
860 null();// (void) signal(SIGINT, oldintr);
875 verbose
= oldverbose
;
881 if (din
== (int)NULL
)
883 if (strcmp(local
, "-") == 0)
885 else if (*local
== '|') {
886 null();// oldintp = signal(SIGPIPE, SIG_IGN);
887 fout
= _popen(local
+ 1, "w");
894 fout
= fopen(local
, mode
);
901 (void) gettimeofday(&start
, (struct timezone
*)0);
907 lseek(fileno(fout
), (long) restart_point
, L_SET
) < 0) {
909 if (closefunc
!= NULL
)
914 // while ((c = recv(din, buf, bufsize, 1)) > 0) {
915 // if ((d = write(fileno(fout), buf, c)) != c)
916 // if ((d = write(fileno(fout), buf, c)) != c)
918 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0) {
919 write(fileno(fout
), buf
, c
);
922 while (bytes
>= hashbytes
) {
924 hashbytes
+= HASHBYTES
;
926 (void) fflush(stdout
);
929 if (hash
&& bytes
> 0) {
930 if (bytes
< HASHBYTES
)
932 (void) putchar('\n');
933 (void) fflush(stdout
);
936 // if (errno != EPIPE)
944 // fprintf(stderr, "%s: short write\n", local);
950 register int i
, n
, c
;
952 if (fseek(fout
, 0L, L_SET
) < 0)
957 if ((c
=getc(fout
)) == EOF
)
962 if (fseek(fout
, 0L, L_INCR
) < 0) {
965 if (closefunc
!= NULL
)
970 while ((c
= fgetcSocket(din
)) != EOF
) {
974 while (hash
&& (bytes
>= hashbytes
)) {
976 (void) fflush(stdout
);
977 hashbytes
+= HASHBYTES
;
980 if ((c
= fgetcSocket(din
)) != '\n' || tcrflag
) {
983 (void) putc('\r', fout
);
992 (void) putc(c
, fout
);
998 printf("WARNING! %d bare linefeeds received in ASCII mode\n");
999 printf("File may not have transferred correctly.\n");
1000 (void) fflush(stdout
);
1003 if (bytes
< hashbytes
)
1004 (void) putchar('#');
1005 (void) putchar('\n');
1006 (void) fflush(stdout
);
1008 // if (ferror(din)) {
1009 // if (errno != EPIPE)
1017 if (closefunc
!= NULL
)
1019 null();// (void) signal(SIGINT, oldintr);
1021 null();// (void) signal(SIGPIPE, oldintp);
1022 (void) gettimeofday(&stop
, (struct timezone
*)0);
1023 if(closesocket(din
)) {
1024 int iret
=WSAGetLastError ();
1025 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1026 (void) fflush(stdout
);
1030 if (bytes
> 0 && is_retr
)
1031 ptransfer("received", bytes
, &start
, &stop
);
1046 verbose
= oldverbose
;
1051 /* abort using RFC959 recommended IP,SYNC sequence */
1053 (void) gettimeofday(&stop
, (struct timezone
*)0);
1055 null();// (void) signal(SIGPIPE, oldintr);
1056 null();// (void) signal(SIGINT,SIG_IGN);
1071 verbose
= oldverbose
;
1075 null();// (void) signal(SIGINT,oldintr);
1079 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1081 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
1082 /* after urgent byte rather than before as now is protocol */
1083 if (send(cout
,&msg
,1,MSG_OOB
) != 1) {
1086 fprintfSocket(cout
,"%cABOR\r\n",DM
);
1088 FD_SET(cin
, &mask
); // Need to correct this
1090 FD_SET(din
, &mask
); // Need to correct this
1092 if ((nfnd
= empty(&mask
,10)) <= 0) {
1099 if (din
&& FD_ISSET(din
, &mask
)) {
1100 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0)
1103 if ((c
= getreply(0)) == ERROR
&& code
== 552) { /* needed for nic style abort */
1116 if (closefunc
!= NULL
&& fout
!= NULL
)
1119 if(closesocket(din
)) {
1120 int iret
=WSAGetLastError ();
1121 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1122 (void) fflush(stdout
);
1126 ptransfer("received", bytes
, &start
, &stop
);
1127 null();// (void) signal(SIGINT,oldintr);
1131 * Need to start a listen on the data channel
1132 * before we send the command, otherwise the
1133 * server's connect may fail.
1139 register char *p
, *a
;
1140 int result
, len
, tmpno
= 0;
1142 int a0
, a1
, a2
, a3
, p0
, p1
;
1146 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1148 perror("ftp: socket");
1151 if ((options
& SO_DEBUG
) &&
1152 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
,
1154 perror("ftp: setsockopt (ignored)");
1155 if (command("PASV") != COMPLETE
) {
1156 printf("Passive mode refused.\n");
1161 * What we've got at this point is a string of comma
1162 * separated one-byte unsigned integer values.
1163 * The first four are the an IP address. The fifth is
1164 * the MSB of the port number, the sixth is the LSB.
1165 * From that we'll prepare a sockaddr_in.
1168 if (sscanf(pasv
,"%d,%d,%d,%d,%d,%d",
1169 &a0
, &a1
, &a2
, &a3
, &p0
, &p1
) != 6) {
1170 printf("Passive mode address scan failure. Shouldn't happen!\n");
1174 bzero(&data_addr
, sizeof(data_addr
));
1175 data_addr
.sin_family
= AF_INET
;
1176 a
= (char *)&data_addr
.sin_addr
.s_addr
;
1181 p
= (char *)&data_addr
.sin_port
;
1185 if (connect(data
, (struct sockaddr
*)&data_addr
,
1186 sizeof(data_addr
)) < 0) {
1187 perror("ftp: connect");
1195 data_addr
= myctladdr
;
1197 data_addr
.sin_port
= 0; /* let system pick one */
1199 (void) close (data
);
1200 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1202 perror("ftp: socket");
1208 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof (on
)) < 0) {
1209 perror("ftp: setsockopt (reuse address)");
1212 if (bind(data
, (struct sockaddr
*)&data_addr
, sizeof (data_addr
)) < 0) {
1213 perror("ftp: bind");
1216 if (options
& SO_DEBUG
&&
1217 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof (on
)) < 0)
1218 perror("ftp: setsockopt (ignored)");
1219 len
= sizeof (data_addr
);
1220 if (getsockname(data
, (struct sockaddr
*)&data_addr
, &len
) < 0) {
1221 perror("ftp: getsockname");
1224 if (listen(data
, 1) < 0)
1225 perror("ftp: listen");
1227 a
= (char *)&data_addr
.sin_addr
;
1228 p
= (char *)&data_addr
.sin_port
;
1229 #define UC(b) (((int)b)&0xff)
1231 command("PORT %d,%d,%d,%d,%d,%d",
1232 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1233 UC(p
[0]), UC(p
[1]));
1234 if (result
== ERROR
&& sendport
== -1) {
1239 return (result
!= COMPLETE
);
1245 (void) fflush(stdout
);
1246 (void) close(data
), data
= -1;
1252 int dataconn(char *mode
)
1254 struct sockaddr_in from
;
1255 int s
, fromlen
= sizeof (from
);
1260 s
= accept(data
, (struct sockaddr
*) &from
, &fromlen
);
1262 perror("ftp: accept");
1263 (void) closesocket(data
), data
= -1;
1264 return (int) (NULL
);
1266 if(closesocket(data
)) {
1267 int iret
=WSAGetLastError ();
1268 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1269 (void) fflush(stdout
);
1276 void ptransfer(direction
, bytes
, t0
, t1
)
1279 struct timeval
*t0
, *t1
;
1286 s
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.);
1287 #define nz(x) ((x) == 0 ? 1 : (x))
1289 printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n",
1290 bytes
, direction
, s
, bs
/ 1024.);
1291 (void) fflush(stdout
);
1296 struct timeval *tsum, *t0;
1299 tsum->tv_sec += t0->tv_sec;
1300 tsum->tv_usec += t0->tv_usec;
1301 if (tsum->tv_usec > 1000000)
1302 tsum->tv_sec++, tsum->tv_usec -= 1000000;
1305 void tvsub(tdiff
, t1
, t0
)
1306 struct timeval
*tdiff
, *t1
, *t0
;
1309 tdiff
->tv_sec
= t1
->tv_sec
- t0
->tv_sec
;
1310 tdiff
->tv_usec
= t1
->tv_usec
- t0
->tv_usec
;
1311 if (tdiff
->tv_usec
< 0)
1312 tdiff
->tv_sec
--, tdiff
->tv_usec
+= 1000000;
1315 void psabort(int flag
)
1317 extern int abrtflag
;
1322 void pswitch(int flag
)
1324 extern int proxy
, abrtflag
;
1326 static struct comvars
{
1328 char name
[MAXHOSTNAMELEN
];
1329 struct sockaddr_in mctl
;
1330 struct sockaddr_in hctl
;
1342 char mi
[MAXPATHLEN
];
1343 char mo
[MAXPATHLEN
];
1344 } proxstruct
, tmpstruct
;
1345 struct comvars
*ip
, *op
;
1348 oldintr
= signal(SIGINT
, psabort
);
1363 ip
->connect
= connected
;
1364 connected
= op
->connect
;
1366 (void) strncpy(ip
->name
, hostname
, sizeof(ip
->name
) - 1);
1367 ip
->name
[strlen(ip
->name
)] = '\0';
1370 hostname
= op
->name
;
1371 ip
->hctl
= hisctladdr
;
1372 hisctladdr
= op
->hctl
;
1373 ip
->mctl
= myctladdr
;
1374 myctladdr
= op
->mctl
;
1385 ip
->sunqe
= sunique
;
1386 sunique
= op
->sunqe
;
1387 ip
->runqe
= runique
;
1388 runique
= op
->runqe
;
1393 (void) strncpy(ip
->nti
, ntin
, 16);
1394 (ip
->nti
)[strlen(ip
->nti
)] = '\0';
1395 (void) strcpy(ntin
, op
->nti
);
1396 (void) strncpy(ip
->nto
, ntout
, 16);
1397 (ip
->nto
)[strlen(ip
->nto
)] = '\0';
1398 (void) strcpy(ntout
, op
->nto
);
1399 ip
->mapflg
= mapflag
;
1400 mapflag
= op
->mapflg
;
1401 (void) strncpy(ip
->mi
, mapin
, MAXPATHLEN
- 1);
1402 (ip
->mi
)[strlen(ip
->mi
)] = '\0';
1403 (void) strcpy(mapin
, op
->mi
);
1404 (void) strncpy(ip
->mo
, mapout
, MAXPATHLEN
- 1);
1405 (ip
->mo
)[strlen(ip
->mo
)] = '\0';
1406 (void) strcpy(mapout
, op
->mo
);
1407 // (void) signal(SIGINT, oldintr);
1421 (void) fflush(stdout
);
1425 longjmp(ptabort
, 1);
1428 void proxtrans(cmd
, local
, remote
)
1429 char *cmd
, *local
, *remote
;
1431 // void (*oldintr)(int);
1432 //void abortpt(int);
1433 int tmptype
, oldtype
= 0, secndflag
= 0, nfnd
;
1434 extern jmp_buf ptabort
;
1439 if (strcmp(cmd
, "RETR"))
1442 cmd2
= runique
? "STOU" : "STOR";
1443 if (command("PASV") != COMPLETE
) {
1444 printf("proxy server does not support third part transfers.\n");
1445 (void) fflush(stdout
);
1451 printf("No primary connection\n");
1452 (void) fflush(stdout
);
1457 if (type
!= tmptype
) {
1474 if (command("PORT %s", pasv
) != COMPLETE
) {
1494 if (setjmp(ptabort
))
1496 null();// oldintr = signal(SIGINT, abortpt);
1497 if (command("%s %s", cmd
, remote
) != PRELIM
) {
1498 null();// (void) signal(SIGINT, oldintr);
1521 if (command("%s %s", cmd2
, local
) != PRELIM
)
1527 null();// (void) signal(SIGINT, oldintr);
1546 printf("local: %s remote: %s\n", local
, remote
);
1547 (void) fflush(stdout
);
1550 null();// (void) signal(SIGINT, SIG_IGN);
1552 if (strcmp(cmd
, "RETR") && !proxy
)
1554 else if (!strcmp(cmd
, "RETR") && proxy
)
1556 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1557 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1578 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1580 *(msg
+1) = (char) DM
;
1581 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1583 fprintfSocket(cout
,"ABOR\r\n");
1585 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1586 if ((nfnd
= empty(&mask
,10)) <= 0) {
1601 null();// (void) signal(SIGINT, oldintr);
1607 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1609 *(msg
+1) = (char)DM
;
1610 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1612 fprintfSocket(cout
,"ABOR\r\n");
1614 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1615 if ((nfnd
= empty(&mask
,10)) <= 0) {
1627 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1628 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1649 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1651 *(msg
+1) = (char)DM
;
1652 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1654 fprintfSocket(cout
,"ABOR\r\n");
1656 // FD_SET(fileno(cin), &mask); // Chris:
1657 if ((nfnd
= empty(&mask
,10)) <= 0) {
1671 null();// (void) signal(SIGINT, oldintr);
1678 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1680 *(msg
+1) = (char)DM
;
1681 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1683 fprintfSocket(cout
,"ABOR\r\n");
1685 // FD_SET(fileno(cin), &mask); // Chris:
1686 if ((nfnd
= empty(&mask
,10)) <= 0) {
1700 // FD_SET(fileno(cin), &mask); // Chris:
1701 if ((nfnd
= empty(&mask
,10)) <= 0) {
1733 null();// (void) signal(SIGINT, oldintr);
1744 // FD_SET(fileno(cin), &mask); // Chris
1745 if ((nfnd
= empty(&mask
,0)) < 0) {
1760 static char new[MAXPATHLEN
];
1761 char *cp
= rindex(local
, '/');
1767 d
= access(cp
? local
: ".", 2);
1774 (void) strcpy(new, local
);
1775 cp
= new + strlen(new);
1778 if (++count
== 100) {
1779 printf("runique: can't find unique file name.\n");
1780 (void) fflush(stdout
);
1789 if ((d
= access(new, 0)) < 0)
1793 else if (*(cp
- 2) == '.')
1796 *(cp
- 2) = *(cp
- 2) + 1;