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
!= (unsigned long)-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 */
330 int originalcode
= 0, continuation
= 0;
331 void (*oldintr
)(int), cmdabort(int);
335 oldintr
= signal(SIGINT
,cmdabort
);
339 while ((c
= fgetcSocket(cin
)) != '\n') {
340 if (c
== IAC
) { /* handle telnet commands */
341 switch (c
= fgetcSocket(cin
)) {
344 c
= fgetcSocket(cin
);
345 fprintfSocket(cout
, "%c%c%c",IAC
,DONT
,c
);
349 c
= fgetcSocket(cin
);
350 fprintfSocket(cout
, "%c%c%c",IAC
,WONT
,c
);
360 // (void) signal(SIGINT,oldintr);
366 printf("421 Service not available, remote server has closed connection\n");
367 (void) fflush(stdout
);
372 if (c
!= '\r' && (verbose
> 0 ||
373 (verbose
> -1 && n
== '5' && dig
> 4))) {
375 ((dig
== 1 || dig
== 5) && verbose
== 0))
376 printf("%s:",hostname
);
378 (void) fflush(stdout
);
380 if (dig
< 4 && isdigit(c
))
381 code
= code
* 10 + (c
- '0');
382 if (!pflag
&& code
== 227)
384 if (dig
> 4 && pflag
== 1 && isdigit(c
))
387 if (c
!= '\r' && c
!= ')')
394 if (dig
== 4 && c
== '-') {
401 if (cp
< &reply_string
[sizeof(reply_string
) - 1])
404 if (verbose
> 0 || (verbose
> -1 && n
== '5')) {
406 (void) fflush (stdout
);
408 if (continuation
&& code
!= originalcode
) {
409 if (originalcode
== 0)
416 (void) signal(SIGINT
,oldintr
);
417 if (code
== 421 || originalcode
== 421)
419 if (abrtflag
&& oldintr
!= cmdabort
&& oldintr
!= SIG_IGN
)
432 t
.tv_sec
= (long) sec
;
434 return(select(32, mask
, (struct fd_set
*) 0, (struct fd_set
*) 0, &t
));
444 printf("\nsend aborted\n");
445 (void) fflush(stdout
);
446 longjmp(sendabort
, 1);
449 #define HASHBYTES 1024
451 void sendrequest(char *cmd
, char *local
, char *remote
, int printnames
)
455 int (*closefunc
)(), _pclose(), fclose();
456 sig_t (*oldintr
)(), (*oldintp
)();
458 char buf
[BUFSIZ
], *bufp
;
459 long bytes
= 0, hashbytes
= HASHBYTES
;
462 struct timeval start
, stop
;
465 if (verbose
&& printnames
) {
466 if (local
&& *local
!= '-')
467 printf("local: %s ", local
);
469 printf("remote: %s\n", remote
);
470 (void) fflush(stdout
);
473 proxtrans(cmd
, local
, remote
);
480 if (setjmp(sendabort
)) {
489 null();// (void) signal(SIGINT,oldintr);
491 null();// (void) signal(SIGPIPE,oldintp);
495 null();// oldintr = signal(SIGINT, abortsend);
496 if (strcmp(local
, "-") == 0)
498 else if (*local
== '|') {
499 null();// oldintp = signal(SIGPIPE,SIG_IGN);
500 fin
= _popen(local
+ 1, "r");
503 null();// (void) signal(SIGINT, oldintr);
504 null();// (void) signal(SIGPIPE, oldintp);
510 fin
= fopen(local
, "r");
513 null();// (void) signal(SIGINT, oldintr);
518 if (fstat(fileno(fin
), &st
) < 0 ||
519 (st
.st_mode
&S_IFMT
) != S_IFREG
) {
520 fprintf(stdout
, "%s: not a plain file.\n", local
);
521 (void) fflush(stdout
);
522 null();// (void) signal(SIGINT, oldintr);
529 null();// (void) signal(SIGINT, oldintr);
531 null();// (void) signal(SIGPIPE, oldintp);
533 if (closefunc
!= NULL
)
537 if (setjmp(sendabort
))
541 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
542 if (fseek(fin
, (long) restart_point
, 0) < 0) {
545 if (closefunc
!= NULL
)
549 if (command("REST %ld", (long) restart_point
)
552 if (closefunc
!= NULL
)
560 if (command("%s %s", cmd
, remote
) != PRELIM
) {
561 null();// (void) signal(SIGINT, oldintr);
563 null();// (void) signal(SIGPIPE, oldintp);
564 if (closefunc
!= NULL
)
569 if (command("%s", cmd
) != PRELIM
) {
570 null();// (void) signal(SIGINT, oldintr);
572 null();// (void) signal(SIGPIPE, oldintp);
573 if (closefunc
!= NULL
)
577 dout
= dataconn(mode
);
578 if (dout
== (int)NULL
)
580 (void) gettimeofday(&start
, (struct timezone
*)0);
581 null();// oldintp = signal(SIGPIPE, SIG_IGN);
587 while ((c
= read(fileno(fin
), buf
, sizeof (buf
))) > 0) {
589 for (bufp
= buf
; c
> 0; c
-= d
, bufp
+= d
)
590 if ((d
= send(dout
, bufp
, c
, 0)) <= 0)
593 while (bytes
>= hashbytes
) {
595 hashbytes
+= HASHBYTES
;
597 (void) fflush(stdout
);
600 if (hash
&& bytes
> 0) {
601 if (bytes
< HASHBYTES
)
603 (void) putchar('\n');
604 (void) fflush(stdout
);
610 fprintf(stderr
, "netout: write returned 0?\n");
611 else if (errno
!= EPIPE
)
620 static int bufsize
= 1024;
623 while ((c
= getc(fin
)) != EOF
) {
625 while (hash
&& (bytes
>= hashbytes
)) {
627 (void) fflush(stdout
);
628 hashbytes
+= HASHBYTES
;
630 // Szurgot: The following code is unncessary on Win32.
631 // (void) fputcSocket(dout, '\r');
635 if (ipos
>= bufsize
) {
636 fputSocket(dout
,buf
,ipos
);
637 if(!hash
) (void) putchar('.');
644 fputSocket(dout
,buf
,ipos
);
648 if (bytes
< hashbytes
)
650 (void) putchar('\n');
651 (void) fflush(stdout
);
655 (void) putchar('\n');
656 (void) fflush(stdout
);
660 // if (ferror(dout)) {
661 // if (errno != EPIPE)
668 (void) gettimeofday(&stop
, (struct timezone
*)0);
669 if (closefunc
!= NULL
)
671 if(closesocket(dout
)) {
672 int iret
=WSAGetLastError ();
673 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
674 (void) fflush(stdout
);
677 null();// (void) signal(SIGINT, oldintr);
679 null();// (void) signal(SIGPIPE, oldintp);
681 ptransfer("sent", bytes
, &start
, &stop
);
684 (void) gettimeofday(&stop
, (struct timezone
*)0);
685 null();// (void) signal(SIGINT, oldintr);
687 null();// (void) signal(SIGPIPE, oldintp);
697 if(closesocket(dout
)) {
698 int iret
=WSAGetLastError ();
699 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
700 (void) fflush(stdout
);
705 if (closefunc
!= NULL
&& fin
!= NULL
)
708 ptransfer("sent", bytes
, &start
, &stop
);
720 (void) fflush(stdout
);
721 longjmp(recvabort
, 1);
724 void recvrequest(char *cmd
, char *local
, char *remote
, char *mode
,
729 int (*closefunc
)(), _pclose(), fclose();
730 void (*oldintr
)(int), (*oldintp
)(int);
732 int oldverbose
, oldtype
= 0, is_retr
, tcrflag
, nfnd
, bare_lfs
= 0;
733 char *gunique(), msg
;
734 // static char *buf; // Szurgot: Shouldn't this go SOMEWHERE?
736 static int bufsize
= 1024;
737 long bytes
= 0, hashbytes
= HASHBYTES
;
741 struct timeval start
, stop
;
743 extern void *malloc();
745 is_retr
= strcmp(cmd
, "RETR") == 0;
746 if (is_retr
&& verbose
&& printnames
) {
747 if (local
&& *local
!= '-')
748 printf("local: %s ", local
);
750 printf("remote: %s\n", remote
);
751 (void) fflush(stdout
);
753 if (proxy
&& is_retr
) {
754 proxtrans(cmd
, local
, remote
);
760 tcrflag
= !crflag
&& is_retr
;
761 if (setjmp(recvabort
)) {
770 null();// (void) signal(SIGINT, oldintr);
774 null();// oldintr = signal(SIGINT, abortrecv);
775 if (strcmp(local
, "-") && *local
!= '|') {
777 // This whole thing is a problem... access Won't work on non-existent files
778 if (access(local
, 2) < 0) {
779 char *dir
= rindex(local
, '/');
781 if (errno
!= ENOENT
&& errno
!= EACCES
) {
783 (void) signal(SIGINT
, oldintr
);
789 d
= access(dir
? local
: ".", 2);
794 (void) signal(SIGINT
, oldintr
);
798 if (!runique
&& errno
== EACCES
&&
799 chmod(local
, 0600) < 0) {
801 (void) signal(SIGINT
, oldintr
);
805 if (runique
&& errno
== EACCES
&&
806 (local
= gunique(local
)) == NULL
) {
807 (void) signal(SIGINT
, oldintr
);
812 else if (runique
&& (local
= gunique(local
)) == NULL
) {
813 (void) signal(SIGINT
, oldintr
);
820 null();// (void) signal(SIGINT, oldintr);
824 if (setjmp(recvabort
))
827 if (type
!= TYPE_A
&& (allbinary
== 0 || type
!= TYPE_I
)) {
829 oldverbose
= verbose
;
833 verbose
= oldverbose
;
835 } else if (restart_point
) {
836 if (command("REST %ld", (long) restart_point
) != CONTINUE
)
840 if (command("%s %s", cmd
, remote
) != PRELIM
) {
841 null();// (void) signal(SIGINT, oldintr);
856 verbose
= oldverbose
;
861 if (command("%s", cmd
) != PRELIM
) {
862 null();// (void) signal(SIGINT, oldintr);
877 verbose
= oldverbose
;
883 if (din
== (int)NULL
)
885 if (strcmp(local
, "-") == 0)
887 else if (*local
== '|') {
888 null();// oldintp = signal(SIGPIPE, SIG_IGN);
889 fout
= _popen(local
+ 1, "w");
896 fout
= fopen(local
, mode
);
903 (void) gettimeofday(&start
, (struct timezone
*)0);
909 lseek(fileno(fout
), (long) restart_point
, L_SET
) < 0) {
911 if (closefunc
!= NULL
)
916 // while ((c = recv(din, buf, bufsize, 1)) > 0) {
917 // if ((d = write(fileno(fout), buf, c)) != c)
918 // if ((d = write(fileno(fout), buf, c)) != c)
920 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0) {
921 write(fileno(fout
), buf
, c
);
924 while (bytes
>= hashbytes
) {
926 hashbytes
+= HASHBYTES
;
928 (void) fflush(stdout
);
931 if (hash
&& bytes
> 0) {
932 if (bytes
< HASHBYTES
)
934 (void) putchar('\n');
935 (void) fflush(stdout
);
938 // if (errno != EPIPE)
946 // fprintf(stderr, "%s: short write\n", local);
952 register int i
, n
, c
;
954 if (fseek(fout
, 0L, L_SET
) < 0)
959 if ((c
=getc(fout
)) == EOF
)
964 if (fseek(fout
, 0L, L_INCR
) < 0) {
967 if (closefunc
!= NULL
)
972 while ((c
= fgetcSocket(din
)) != EOF
) {
976 while (hash
&& (bytes
>= hashbytes
)) {
978 (void) fflush(stdout
);
979 hashbytes
+= HASHBYTES
;
982 if ((c
= fgetcSocket(din
)) != '\n' || tcrflag
) {
985 (void) putc('\r', fout
);
994 (void) putc(c
, fout
);
1000 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs
);
1001 printf("File may not have transferred correctly.\n");
1002 (void) fflush(stdout
);
1005 if (bytes
< hashbytes
)
1006 (void) putchar('#');
1007 (void) putchar('\n');
1008 (void) fflush(stdout
);
1010 // if (ferror(din)) {
1011 // if (errno != EPIPE)
1019 if (closefunc
!= NULL
)
1021 null();// (void) signal(SIGINT, oldintr);
1023 null();// (void) signal(SIGPIPE, oldintp);
1024 (void) gettimeofday(&stop
, (struct timezone
*)0);
1025 if(closesocket(din
)) {
1026 int iret
=WSAGetLastError ();
1027 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1028 (void) fflush(stdout
);
1032 if (bytes
> 0 && is_retr
)
1033 ptransfer("received", bytes
, &start
, &stop
);
1048 verbose
= oldverbose
;
1053 /* abort using RFC959 recommended IP,SYNC sequence */
1055 (void) gettimeofday(&stop
, (struct timezone
*)0);
1057 null();// (void) signal(SIGPIPE, oldintr);
1058 null();// (void) signal(SIGINT,SIG_IGN);
1073 verbose
= oldverbose
;
1077 null();// (void) signal(SIGINT,oldintr);
1081 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1083 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
1084 /* after urgent byte rather than before as now is protocol */
1085 if (send(cout
,&msg
,1,MSG_OOB
) != 1) {
1088 fprintfSocket(cout
,"%cABOR\r\n",DM
);
1090 FD_SET(cin
, &mask
); // Need to correct this
1092 FD_SET(din
, &mask
); // Need to correct this
1094 if ((nfnd
= empty(&mask
,10)) <= 0) {
1101 if (din
&& FD_ISSET(din
, &mask
)) {
1102 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0)
1105 if ((c
= getreply(0)) == ERROR
&& code
== 552) { /* needed for nic style abort */
1118 if (closefunc
!= NULL
&& fout
!= NULL
)
1121 if(closesocket(din
)) {
1122 int iret
=WSAGetLastError ();
1123 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1124 (void) fflush(stdout
);
1128 ptransfer("received", bytes
, &start
, &stop
);
1129 null();// (void) signal(SIGINT,oldintr);
1133 * Need to start a listen on the data channel
1134 * before we send the command, otherwise the
1135 * server's connect may fail.
1142 register char *p
, *a
;
1143 int result
, len
, tmpno
= 0;
1145 int a0
, a1
, a2
, a3
, p0
, p1
;
1149 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1151 perror("ftp: socket");
1154 if ((options
& SO_DEBUG
) &&
1155 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
,
1157 perror("ftp: setsockopt (ignored)");
1158 if (command("PASV") != COMPLETE
) {
1159 printf("Passive mode refused.\n");
1164 * What we've got at this point is a string of comma
1165 * separated one-byte unsigned integer values.
1166 * The first four are the an IP address. The fifth is
1167 * the MSB of the port number, the sixth is the LSB.
1168 * From that we'll prepare a sockaddr_in.
1171 if (sscanf(pasv
,"%d,%d,%d,%d,%d,%d",
1172 &a0
, &a1
, &a2
, &a3
, &p0
, &p1
) != 6) {
1173 printf("Passive mode address scan failure. Shouldn't happen!\n");
1177 bzero(&data_addr
, sizeof(data_addr
));
1178 data_addr
.sin_family
= AF_INET
;
1179 a
= (char *)&data_addr
.sin_addr
.s_addr
;
1184 p
= (char *)&data_addr
.sin_port
;
1188 if (connect(data
, (struct sockaddr
*)&data_addr
,
1189 sizeof(data_addr
)) < 0) {
1190 perror("ftp: connect");
1198 data_addr
= myctladdr
;
1200 data_addr
.sin_port
= 0; /* let system pick one */
1202 (void) close (data
);
1203 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1205 perror("ftp: socket");
1211 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof (on
)) < 0) {
1212 perror("ftp: setsockopt (reuse address)");
1215 if (bind(data
, (struct sockaddr
*)&data_addr
, sizeof (data_addr
)) < 0) {
1216 perror("ftp: bind");
1219 if (options
& SO_DEBUG
&&
1220 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof (on
)) < 0)
1221 perror("ftp: setsockopt (ignored)");
1222 len
= sizeof (data_addr
);
1223 if (getsockname(data
, (struct sockaddr
*)&data_addr
, &len
) < 0) {
1224 perror("ftp: getsockname");
1227 if (listen(data
, 1) < 0)
1228 perror("ftp: listen");
1230 a
= (char *)&data_addr
.sin_addr
;
1231 p
= (char *)&data_addr
.sin_port
;
1232 #define UC(b) (((int)b)&0xff)
1234 command("PORT %d,%d,%d,%d,%d,%d",
1235 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1236 UC(p
[0]), UC(p
[1]));
1237 if (result
== ERROR
&& sendport
== -1) {
1242 return (result
!= COMPLETE
);
1248 (void) fflush(stdout
);
1249 (void) close(data
), data
= -1;
1255 int dataconn(char *mode
)
1257 struct sockaddr_in from
;
1258 int s
, fromlen
= sizeof (from
);
1263 s
= accept(data
, (struct sockaddr
*) &from
, &fromlen
);
1265 perror("ftp: accept");
1266 (void) closesocket(data
), data
= -1;
1267 return (int) (NULL
);
1269 if(closesocket(data
)) {
1270 int iret
=WSAGetLastError ();
1271 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1272 (void) fflush(stdout
);
1279 void ptransfer(direction
, bytes
, t0
, t1
)
1282 struct timeval
*t0
, *t1
;
1289 s
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.);
1290 #define nz(x) ((x) == 0 ? 1 : (x))
1292 printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n",
1293 bytes
, direction
, s
, bs
/ 1024.);
1294 (void) fflush(stdout
);
1299 struct timeval *tsum, *t0;
1302 tsum->tv_sec += t0->tv_sec;
1303 tsum->tv_usec += t0->tv_usec;
1304 if (tsum->tv_usec > 1000000)
1305 tsum->tv_sec++, tsum->tv_usec -= 1000000;
1308 void tvsub(tdiff
, t1
, t0
)
1309 struct timeval
*tdiff
, *t1
, *t0
;
1312 tdiff
->tv_sec
= t1
->tv_sec
- t0
->tv_sec
;
1313 tdiff
->tv_usec
= t1
->tv_usec
- t0
->tv_usec
;
1314 if (tdiff
->tv_usec
< 0)
1315 tdiff
->tv_sec
--, tdiff
->tv_usec
+= 1000000;
1318 void psabort(int flag
)
1320 extern int abrtflag
;
1325 void pswitch(int flag
)
1327 extern int proxy
, abrtflag
;
1329 static struct comvars
{
1331 char name
[MAXHOSTNAMELEN
];
1332 struct sockaddr_in mctl
;
1333 struct sockaddr_in hctl
;
1345 char mi
[MAXPATHLEN
];
1346 char mo
[MAXPATHLEN
];
1347 } proxstruct
, tmpstruct
;
1348 struct comvars
*ip
, *op
;
1351 oldintr
= signal(SIGINT
, psabort
);
1366 ip
->connect
= connected
;
1367 connected
= op
->connect
;
1369 (void) strncpy(ip
->name
, hostname
, sizeof(ip
->name
) - 1);
1370 ip
->name
[strlen(ip
->name
)] = '\0';
1373 hostname
= op
->name
;
1374 ip
->hctl
= hisctladdr
;
1375 hisctladdr
= op
->hctl
;
1376 ip
->mctl
= myctladdr
;
1377 myctladdr
= op
->mctl
;
1388 ip
->sunqe
= sunique
;
1389 sunique
= op
->sunqe
;
1390 ip
->runqe
= runique
;
1391 runique
= op
->runqe
;
1396 (void) strncpy(ip
->nti
, ntin
, 16);
1397 (ip
->nti
)[strlen(ip
->nti
)] = '\0';
1398 (void) strcpy(ntin
, op
->nti
);
1399 (void) strncpy(ip
->nto
, ntout
, 16);
1400 (ip
->nto
)[strlen(ip
->nto
)] = '\0';
1401 (void) strcpy(ntout
, op
->nto
);
1402 ip
->mapflg
= mapflag
;
1403 mapflag
= op
->mapflg
;
1404 (void) strncpy(ip
->mi
, mapin
, MAXPATHLEN
- 1);
1405 (ip
->mi
)[strlen(ip
->mi
)] = '\0';
1406 (void) strcpy(mapin
, op
->mi
);
1407 (void) strncpy(ip
->mo
, mapout
, MAXPATHLEN
- 1);
1408 (ip
->mo
)[strlen(ip
->mo
)] = '\0';
1409 (void) strcpy(mapout
, op
->mo
);
1410 // (void) signal(SIGINT, oldintr);
1424 (void) fflush(stdout
);
1428 longjmp(ptabort
, 1);
1431 void proxtrans(cmd
, local
, remote
)
1432 char *cmd
, *local
, *remote
;
1434 // void (*oldintr)(int);
1435 //void abortpt(int);
1436 int tmptype
, oldtype
= 0, secndflag
= 0, nfnd
;
1437 extern jmp_buf ptabort
;
1442 if (strcmp(cmd
, "RETR"))
1445 cmd2
= runique
? "STOU" : "STOR";
1446 if (command("PASV") != COMPLETE
) {
1447 printf("proxy server does not support third part transfers.\n");
1448 (void) fflush(stdout
);
1454 printf("No primary connection\n");
1455 (void) fflush(stdout
);
1460 if (type
!= tmptype
) {
1477 if (command("PORT %s", pasv
) != COMPLETE
) {
1497 if (setjmp(ptabort
))
1499 null();// oldintr = signal(SIGINT, abortpt);
1500 if (command("%s %s", cmd
, remote
) != PRELIM
) {
1501 null();// (void) signal(SIGINT, oldintr);
1524 if (command("%s %s", cmd2
, local
) != PRELIM
)
1530 null();// (void) signal(SIGINT, oldintr);
1549 printf("local: %s remote: %s\n", local
, remote
);
1550 (void) fflush(stdout
);
1553 null();// (void) signal(SIGINT, SIG_IGN);
1555 if (strcmp(cmd
, "RETR") && !proxy
)
1557 else if (!strcmp(cmd
, "RETR") && proxy
)
1559 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1560 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1581 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1583 *(msg
+1) = (char) DM
;
1584 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1586 fprintfSocket(cout
,"ABOR\r\n");
1588 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1589 if ((nfnd
= empty(&mask
,10)) <= 0) {
1604 null();// (void) signal(SIGINT, oldintr);
1610 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1612 *(msg
+1) = (char)DM
;
1613 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1615 fprintfSocket(cout
,"ABOR\r\n");
1617 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1618 if ((nfnd
= empty(&mask
,10)) <= 0) {
1630 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1631 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1652 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1654 *(msg
+1) = (char)DM
;
1655 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1657 fprintfSocket(cout
,"ABOR\r\n");
1659 // FD_SET(fileno(cin), &mask); // Chris:
1660 if ((nfnd
= empty(&mask
,10)) <= 0) {
1674 null();// (void) signal(SIGINT, oldintr);
1681 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1683 *(msg
+1) = (char)DM
;
1684 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1686 fprintfSocket(cout
,"ABOR\r\n");
1688 // FD_SET(fileno(cin), &mask); // Chris:
1689 if ((nfnd
= empty(&mask
,10)) <= 0) {
1703 // FD_SET(fileno(cin), &mask); // Chris:
1704 if ((nfnd
= empty(&mask
,10)) <= 0) {
1736 null();// (void) signal(SIGINT, oldintr);
1747 // FD_SET(fileno(cin), &mask); // Chris
1748 if ((nfnd
= empty(&mask
,0)) < 0) {
1763 static char new[MAXPATHLEN
];
1764 char *cp
= rindex(local
, '/');
1770 d
= access(cp
? local
: ".", 2);
1777 (void) strcpy(new, local
);
1778 cp
= new + strlen(new);
1781 if (++count
== 100) {
1782 printf("runique: can't find unique file name.\n");
1783 (void) fflush(stdout
);
1792 if ((d
= access(new, 0)) < 0)
1796 else if (*(cp
- 2) == '.')
1799 *(cp
- 2) = *(cp
- 2) + 1;