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(const char *mode
);
103 int command(const 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(const char *host
)
209 char *puser
, *ppass
, *pacct
;
210 const char *user
, *pass
, *acct
;
213 user
= pass
= acct
= 0;
214 if (ruserpass(host
, &puser
, &ppass
, &pacct
) < 0) {
221 while (user
== NULL
) {
222 const char *myname
= "none"; // This needs to become the usename env
225 printf("Name (%s:%s): ", host
, myname
);
227 printf("Name (%s): ", host
);
228 (void) fflush(stdout
);
229 (void) fgets(tmp
, sizeof(tmp
) - 1, stdin
);
230 tmp
[strlen(tmp
) - 1] = '\0';
236 n
= command("USER %s", user
);
239 pass
= getpass("Password:");
240 n
= command("PASS %s", pass
);
245 acct
= getpass("Account:");
246 n
= command("ACCT %s", acct
);
249 fprintf(stderr
, "Login failed.\n");
252 if (!aflag
&& acct
!= NULL
)
253 (void) command("ACCT %s", acct
);
256 for (n
= 0; n
< macnum
; ++n
) {
257 if (!strcmp("init", macros
[n
].mac_name
)) {
258 (void) strcpy(line
, "$init");
260 domacro(margc
, margv
);
270 extern jmp_buf ptabort
;
273 (void) fflush(stdout
);
280 int command(const char *fmt
, ...)
284 void (*oldintr
)(int), cmdabort(int);
290 vfprintf(stdout
, fmt
, ap
);
293 (void) fflush(stdout
);
295 if (cout
== (int) NULL
) {
296 perror ("No control connection for command");
300 oldintr
= signal(SIGINT
,cmdabort
);
305 vsprintf(buffer
, fmt
, ap
);
307 //DLJ: to work through firewalls - send the command as a single message
308 strcat(buffer
,"\r\n");
309 fprintfSocket(cout
, buffer
);
311 //DLJ: the following two lines are replaced by the strcat above - seems to
312 // make it work through firewalls.
313 // fprintfSocket(cout, "\r\n");
314 // (void) fflush(cout);
316 r
= getreply(!strcmp(fmt
, "QUIT"));
317 if (abrtflag
&& oldintr
!= SIG_IGN
)
319 // (void) signal(SIGINT, oldintr);
323 char reply_string
[BUFSIZ
]; /* last line of previous reply */
334 int originalcode
= 0, continuation
= 0;
335 void (*oldintr
)(int), cmdabort(int);
339 oldintr
= signal(SIGINT
,cmdabort
);
343 while ((c
= fgetcSocket(cin
)) != '\n') {
344 if (c
== IAC
) { /* handle telnet commands */
345 switch (c
= fgetcSocket(cin
)) {
348 c
= fgetcSocket(cin
);
349 fprintfSocket(cout
, "%c%c%c",IAC
,DONT
,c
);
353 c
= fgetcSocket(cin
);
354 fprintfSocket(cout
, "%c%c%c",IAC
,WONT
,c
);
364 // (void) signal(SIGINT,oldintr);
370 printf("421 Service not available, remote server has closed connection\n");
371 (void) fflush(stdout
);
376 if (c
!= '\r' && (verbose
> 0 ||
377 (verbose
> -1 && n
== '5' && dig
> 4))) {
379 ((dig
== 1 || dig
== 5) && verbose
== 0))
380 printf("%s:",hostname
);
382 (void) fflush(stdout
);
384 if (dig
< 4 && isdigit(c
))
385 code
= code
* 10 + (c
- '0');
386 if (!pflag
&& code
== 227)
388 if (dig
> 4 && pflag
== 1 && isdigit(c
))
391 if (c
!= '\r' && c
!= ')')
398 if (dig
== 4 && c
== '-') {
405 if (cp
< &reply_string
[sizeof(reply_string
) - 1])
408 if (verbose
> 0 || (verbose
> -1 && n
== '5')) {
410 (void) fflush (stdout
);
412 if (continuation
&& code
!= originalcode
) {
413 if (originalcode
== 0)
420 (void) signal(SIGINT
,oldintr
);
421 if (code
== 421 || originalcode
== 421)
423 if (abrtflag
&& oldintr
!= cmdabort
&& oldintr
!= SIG_IGN
)
436 t
.tv_sec
= (long) sec
;
438 return(select(32, mask
, (struct fd_set
*) 0, (struct fd_set
*) 0, &t
));
449 printf("\nsend aborted\n");
450 (void) fflush(stdout
);
451 longjmp(sendabort
, 1);
455 #define HASHBYTES 1024
457 void sendrequest(const char *cmd
, const char *local
, const char *remote
, int printnames
)
461 int (*closefunc
)(), _pclose(), fclose();
462 sig_t (*oldintr
)(), (*oldintp
)();
463 char buf
[BUFSIZ
], *bufp
;
464 long bytes
= 0, hashbytes
= HASHBYTES
;
467 struct timeval start
, stop
;
470 if (verbose
&& printnames
) {
471 if (local
&& *local
!= '-')
472 printf("local: %s ", local
);
474 printf("remote: %s\n", remote
);
475 (void) fflush(stdout
);
478 proxtrans(cmd
, local
, remote
);
485 if (setjmp(sendabort
)) {
494 null();// (void) signal(SIGINT,oldintr);
496 null();// (void) signal(SIGPIPE,oldintp);
500 null();// oldintr = signal(SIGINT, abortsend);
501 if (strcmp(local
, "-") == 0)
503 else if (*local
== '|') {
504 null();// oldintp = signal(SIGPIPE,SIG_IGN);
505 fin
= _popen(local
+ 1, "r");
508 null();// (void) signal(SIGINT, oldintr);
509 null();// (void) signal(SIGPIPE, oldintp);
515 fin
= fopen(local
, "r");
518 null();// (void) signal(SIGINT, oldintr);
523 if (fstat(fileno(fin
), &st
) < 0 ||
524 (st
.st_mode
&S_IFMT
) != S_IFREG
) {
525 fprintf(stdout
, "%s: not a plain file.\n", local
);
526 (void) fflush(stdout
);
527 null();// (void) signal(SIGINT, oldintr);
534 null();// (void) signal(SIGINT, oldintr);
536 null();// (void) signal(SIGPIPE, oldintp);
538 if (closefunc
!= NULL
)
542 if (setjmp(sendabort
))
546 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
547 if (fseek(fin
, (long) restart_point
, 0) < 0) {
550 if (closefunc
!= NULL
)
554 if (command("REST %ld", (long) restart_point
)
557 if (closefunc
!= NULL
)
565 if (command("%s %s", cmd
, remote
) != PRELIM
) {
566 null();// (void) signal(SIGINT, oldintr);
568 null();// (void) signal(SIGPIPE, oldintp);
569 if (closefunc
!= NULL
)
574 if (command("%s", cmd
) != PRELIM
) {
575 null();// (void) signal(SIGINT, oldintr);
577 null();// (void) signal(SIGPIPE, oldintp);
578 if (closefunc
!= NULL
)
582 dout
= dataconn(mode
);
583 if (dout
== (int)NULL
)
585 (void) gettimeofday(&start
, (struct timezone
*)0);
586 null();// oldintp = signal(SIGPIPE, SIG_IGN);
592 while ((c
= read(fileno(fin
), buf
, sizeof (buf
))) > 0) {
594 for (bufp
= buf
; c
> 0; c
-= d
, bufp
+= d
)
595 if ((d
= send(dout
, bufp
, c
, 0)) <= 0)
598 while (bytes
>= hashbytes
) {
600 hashbytes
+= HASHBYTES
;
602 (void) fflush(stdout
);
605 if (hash
&& bytes
> 0) {
606 if (bytes
< HASHBYTES
)
608 (void) putchar('\n');
609 (void) fflush(stdout
);
615 fprintf(stderr
, "netout: write returned 0?\n");
616 else if (errno
!= EPIPE
)
625 static int bufsize
= 1024;
628 while ((c
= getc(fin
)) != EOF
) {
630 while (hash
&& (bytes
>= hashbytes
)) {
632 (void) fflush(stdout
);
633 hashbytes
+= HASHBYTES
;
635 // Szurgot: The following code is unncessary on Win32.
636 // (void) fputcSocket(dout, '\r');
640 if (ipos
>= bufsize
) {
641 fputSocket(dout
,buf
,ipos
);
642 if(!hash
) (void) putchar('.');
649 fputSocket(dout
,buf
,ipos
);
653 if (bytes
< hashbytes
)
655 (void) putchar('\n');
656 (void) fflush(stdout
);
660 (void) putchar('\n');
661 (void) fflush(stdout
);
665 // if (ferror(dout)) {
666 // if (errno != EPIPE)
673 (void) gettimeofday(&stop
, (struct timezone
*)0);
674 if (closefunc
!= NULL
)
676 if(closesocket(dout
)) {
677 int iret
=WSAGetLastError ();
678 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
679 (void) fflush(stdout
);
682 null();// (void) signal(SIGINT, oldintr);
684 null();// (void) signal(SIGPIPE, oldintp);
686 ptransfer("sent", bytes
, &start
, &stop
);
689 (void) gettimeofday(&stop
, (struct timezone
*)0);
690 null();// (void) signal(SIGINT, oldintr);
692 null();// (void) signal(SIGPIPE, oldintp);
702 if(closesocket(dout
)) {
703 int iret
=WSAGetLastError ();
704 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
705 (void) fflush(stdout
);
710 if (closefunc
!= NULL
&& fin
!= NULL
)
713 ptransfer("sent", bytes
, &start
, &stop
);
725 (void) fflush(stdout
);
726 longjmp(recvabort
, 1);
730 void recvrequest(const char *cmd
, const char *local
, const char *remote
, const char *mode
,
735 int (*closefunc
)(), _pclose(), fclose();
736 void (*oldintr
)(int), (*oldintp
)(int);
737 int oldverbose
= 0, oldtype
= 0, is_retr
, tcrflag
, nfnd
, bare_lfs
= 0;
739 // static char *buf; // Szurgot: Shouldn't this go SOMEWHERE?
741 static int bufsize
= 1024;
742 long bytes
= 0, hashbytes
= HASHBYTES
;
746 struct timeval start
, stop
;
748 extern void *malloc();
750 is_retr
= strcmp(cmd
, "RETR") == 0;
751 if (is_retr
&& verbose
&& printnames
) {
752 if (local
&& *local
!= '-')
753 printf("local: %s ", local
);
755 printf("remote: %s\n", remote
);
756 (void) fflush(stdout
);
758 if (proxy
&& is_retr
) {
759 proxtrans(cmd
, local
, remote
);
765 tcrflag
= !crflag
&& is_retr
;
766 if (setjmp(recvabort
)) {
775 null();// (void) signal(SIGINT, oldintr);
779 null();// oldintr = signal(SIGINT, abortrecv);
780 if (strcmp(local
, "-") && *local
!= '|') {
782 // This whole thing is a problem... access Won't work on non-existent files
783 if (access(local
, 2) < 0) {
784 char *dir
= rindex(local
, '/');
786 if (errno
!= ENOENT
&& errno
!= EACCES
) {
788 (void) signal(SIGINT
, oldintr
);
794 d
= access(dir
? local
: ".", 2);
799 (void) signal(SIGINT
, oldintr
);
803 if (!runique
&& errno
== EACCES
&&
804 chmod(local
, 0600) < 0) {
806 (void) signal(SIGINT
, oldintr
);
810 if (runique
&& errno
== EACCES
&&
811 (local
= gunique(local
)) == NULL
) {
812 (void) signal(SIGINT
, oldintr
);
817 else if (runique
&& (local
= gunique(local
)) == NULL
) {
818 (void) signal(SIGINT
, oldintr
);
825 null();// (void) signal(SIGINT, oldintr);
829 if (setjmp(recvabort
))
832 if (type
!= TYPE_A
&& (allbinary
== 0 || type
!= TYPE_I
)) {
834 oldverbose
= verbose
;
838 verbose
= oldverbose
;
840 } else if (restart_point
) {
841 if (command("REST %ld", (long) restart_point
) != CONTINUE
)
845 if (command("%s %s", cmd
, remote
) != PRELIM
) {
846 null();// (void) signal(SIGINT, oldintr);
861 verbose
= oldverbose
;
866 if (command("%s", cmd
) != PRELIM
) {
867 null();// (void) signal(SIGINT, oldintr);
882 verbose
= oldverbose
;
888 if (din
== (int)NULL
)
890 if (strcmp(local
, "-") == 0)
892 else if (*local
== '|') {
893 null();// oldintp = signal(SIGPIPE, SIG_IGN);
894 fout
= _popen(local
+ 1, "w");
901 fout
= fopen(local
, mode
);
908 (void) gettimeofday(&start
, (struct timezone
*)0);
914 lseek(fileno(fout
), (long) restart_point
, L_SET
) < 0) {
916 if (closefunc
!= NULL
)
921 // while ((c = recv(din, buf, bufsize, 1)) > 0) {
922 // if ((d = write(fileno(fout), buf, c)) != c)
923 // if ((d = write(fileno(fout), buf, c)) != c)
925 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0) {
926 write(fileno(fout
), buf
, c
);
929 while (bytes
>= hashbytes
) {
931 hashbytes
+= HASHBYTES
;
933 (void) fflush(stdout
);
936 if (hash
&& bytes
> 0) {
937 if (bytes
< HASHBYTES
)
939 (void) putchar('\n');
940 (void) fflush(stdout
);
943 // if (errno != EPIPE)
951 // fprintf(stderr, "%s: short write\n", local);
957 register int i
, n
, c
;
959 if (fseek(fout
, 0L, L_SET
) < 0)
964 if ((c
=getc(fout
)) == EOF
)
969 if (fseek(fout
, 0L, L_INCR
) < 0) {
972 if (closefunc
!= NULL
)
977 while ((c
= fgetcSocket(din
)) != EOF
) {
981 while (hash
&& (bytes
>= hashbytes
)) {
983 (void) fflush(stdout
);
984 hashbytes
+= HASHBYTES
;
987 if ((c
= fgetcSocket(din
)) != '\n' || tcrflag
) {
990 (void) putc('\r', fout
);
999 (void) putc(c
, fout
);
1005 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs
);
1006 printf("File may not have transferred correctly.\n");
1007 (void) fflush(stdout
);
1010 if (bytes
< hashbytes
)
1011 (void) putchar('#');
1012 (void) putchar('\n');
1013 (void) fflush(stdout
);
1015 // if (ferror(din)) {
1016 // if (errno != EPIPE)
1024 if (closefunc
!= NULL
)
1026 null();// (void) signal(SIGINT, oldintr);
1028 null();// (void) signal(SIGPIPE, oldintp);
1029 (void) gettimeofday(&stop
, (struct timezone
*)0);
1030 if(closesocket(din
)) {
1031 int iret
=WSAGetLastError ();
1032 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1033 (void) fflush(stdout
);
1037 if (bytes
> 0 && is_retr
)
1038 ptransfer("received", bytes
, &start
, &stop
);
1053 verbose
= oldverbose
;
1058 /* abort using RFC959 recommended IP,SYNC sequence */
1060 (void) gettimeofday(&stop
, (struct timezone
*)0);
1062 null();// (void) signal(SIGPIPE, oldintr);
1063 null();// (void) signal(SIGINT,SIG_IGN);
1078 verbose
= oldverbose
;
1082 null();// (void) signal(SIGINT,oldintr);
1086 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1088 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
1089 /* after urgent byte rather than before as now is protocol */
1090 if (send(cout
,&msg
,1,MSG_OOB
) != 1) {
1093 fprintfSocket(cout
,"%cABOR\r\n",DM
);
1095 FD_SET(cin
, &mask
); // Need to correct this
1097 FD_SET(din
, &mask
); // Need to correct this
1099 if ((nfnd
= empty(&mask
,10)) <= 0) {
1106 if (din
&& FD_ISSET(din
, &mask
)) {
1107 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0)
1110 if ((c
= getreply(0)) == ERROR
&& code
== 552) { /* needed for nic style abort */
1123 if (closefunc
!= NULL
&& fout
!= NULL
)
1126 if(closesocket(din
)) {
1127 int iret
=WSAGetLastError ();
1128 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1129 (void) fflush(stdout
);
1133 ptransfer("received", bytes
, &start
, &stop
);
1134 null();// (void) signal(SIGINT,oldintr);
1138 * Need to start a listen on the data channel
1139 * before we send the command, otherwise the
1140 * server's connect may fail.
1147 register char *p
, *a
;
1148 int result
, len
, tmpno
= 0;
1150 int a0
, a1
, a2
, a3
, p0
, p1
;
1154 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1156 perror("ftp: socket");
1159 if ((options
& SO_DEBUG
) &&
1160 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
,
1162 perror("ftp: setsockopt (ignored)");
1163 if (command("PASV") != COMPLETE
) {
1164 printf("Passive mode refused.\n");
1169 * What we've got at this point is a string of comma
1170 * separated one-byte unsigned integer values.
1171 * The first four are the an IP address. The fifth is
1172 * the MSB of the port number, the sixth is the LSB.
1173 * From that we'll prepare a sockaddr_in.
1176 if (sscanf(pasv
,"%d,%d,%d,%d,%d,%d",
1177 &a0
, &a1
, &a2
, &a3
, &p0
, &p1
) != 6) {
1178 printf("Passive mode address scan failure. Shouldn't happen!\n");
1182 bzero(&data_addr
, sizeof(data_addr
));
1183 data_addr
.sin_family
= AF_INET
;
1184 a
= (char *)&data_addr
.sin_addr
.s_addr
;
1189 p
= (char *)&data_addr
.sin_port
;
1193 if (connect(data
, (struct sockaddr
*)&data_addr
,
1194 sizeof(data_addr
)) < 0) {
1195 perror("ftp: connect");
1203 data_addr
= myctladdr
;
1205 data_addr
.sin_port
= 0; /* let system pick one */
1207 (void) close (data
);
1208 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1210 perror("ftp: socket");
1216 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof (on
)) < 0) {
1217 perror("ftp: setsockopt (reuse address)");
1220 if (bind(data
, (struct sockaddr
*)&data_addr
, sizeof (data_addr
)) < 0) {
1221 perror("ftp: bind");
1224 if (options
& SO_DEBUG
&&
1225 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof (on
)) < 0)
1226 perror("ftp: setsockopt (ignored)");
1227 len
= sizeof (data_addr
);
1228 if (getsockname(data
, (struct sockaddr
*)&data_addr
, &len
) < 0) {
1229 perror("ftp: getsockname");
1232 if (listen(data
, 1) < 0)
1233 perror("ftp: listen");
1235 a
= (char *)&data_addr
.sin_addr
;
1236 p
= (char *)&data_addr
.sin_port
;
1237 #define UC(b) (((int)b)&0xff)
1239 command("PORT %d,%d,%d,%d,%d,%d",
1240 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1241 UC(p
[0]), UC(p
[1]));
1242 if (result
== ERROR
&& sendport
== -1) {
1247 return (result
!= COMPLETE
);
1253 (void) fflush(stdout
);
1254 (void) close(data
), data
= -1;
1260 int dataconn(const char *mode
)
1262 struct sockaddr_in from
;
1263 int s
, fromlen
= sizeof (from
);
1268 s
= accept(data
, (struct sockaddr
*) &from
, &fromlen
);
1270 perror("ftp: accept");
1271 (void) closesocket(data
), data
= -1;
1272 return (int) (NULL
);
1274 if(closesocket(data
)) {
1275 int iret
=WSAGetLastError ();
1276 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1277 (void) fflush(stdout
);
1284 void ptransfer(direction
, bytes
, t0
, t1
)
1285 const char *direction
;
1287 struct timeval
*t0
, *t1
;
1294 s
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.);
1295 #define nz(x) ((x) == 0 ? 1 : (x))
1297 printf("%ld bytes %s in %.1f seconds (%.0f Kbytes/s)\n",
1298 bytes
, direction
, s
, bs
/ 1024.);
1299 (void) fflush(stdout
);
1304 struct timeval *tsum, *t0;
1307 tsum->tv_sec += t0->tv_sec;
1308 tsum->tv_usec += t0->tv_usec;
1309 if (tsum->tv_usec > 1000000)
1310 tsum->tv_sec++, tsum->tv_usec -= 1000000;
1313 void tvsub(tdiff
, t1
, t0
)
1314 struct timeval
*tdiff
, *t1
, *t0
;
1317 tdiff
->tv_sec
= t1
->tv_sec
- t0
->tv_sec
;
1318 tdiff
->tv_usec
= t1
->tv_usec
- t0
->tv_usec
;
1319 if (tdiff
->tv_usec
< 0)
1320 tdiff
->tv_sec
--, tdiff
->tv_usec
+= 1000000;
1323 void psabort(int flag
)
1325 extern int abrtflag
;
1330 void pswitch(int flag
)
1332 extern int proxy
, abrtflag
;
1334 static struct comvars
{
1336 char name
[MAXHOSTNAMELEN
];
1337 struct sockaddr_in mctl
;
1338 struct sockaddr_in hctl
;
1350 char mi
[MAXPATHLEN
];
1351 char mo
[MAXPATHLEN
];
1352 } proxstruct
, tmpstruct
;
1353 struct comvars
*ip
, *op
;
1356 oldintr
= signal(SIGINT
, psabort
);
1371 ip
->connect
= connected
;
1372 connected
= op
->connect
;
1374 (void) strncpy(ip
->name
, hostname
, sizeof(ip
->name
) - 1);
1375 ip
->name
[strlen(ip
->name
)] = '\0';
1378 hostname
= op
->name
;
1379 ip
->hctl
= hisctladdr
;
1380 hisctladdr
= op
->hctl
;
1381 ip
->mctl
= myctladdr
;
1382 myctladdr
= op
->mctl
;
1393 ip
->sunqe
= sunique
;
1394 sunique
= op
->sunqe
;
1395 ip
->runqe
= runique
;
1396 runique
= op
->runqe
;
1401 (void) strncpy(ip
->nti
, ntin
, 16);
1402 (ip
->nti
)[strlen(ip
->nti
)] = '\0';
1403 (void) strcpy(ntin
, op
->nti
);
1404 (void) strncpy(ip
->nto
, ntout
, 16);
1405 (ip
->nto
)[strlen(ip
->nto
)] = '\0';
1406 (void) strcpy(ntout
, op
->nto
);
1407 ip
->mapflg
= mapflag
;
1408 mapflag
= op
->mapflg
;
1409 (void) strncpy(ip
->mi
, mapin
, MAXPATHLEN
- 1);
1410 (ip
->mi
)[strlen(ip
->mi
)] = '\0';
1411 (void) strcpy(mapin
, op
->mi
);
1412 (void) strncpy(ip
->mo
, mapout
, MAXPATHLEN
- 1);
1413 (ip
->mo
)[strlen(ip
->mo
)] = '\0';
1414 (void) strcpy(mapout
, op
->mo
);
1415 // (void) signal(SIGINT, oldintr);
1430 (void) fflush(stdout
);
1434 longjmp(ptabort
, 1);
1438 void proxtrans(cmd
, local
, remote
)
1439 const char *cmd
, *local
, *remote
;
1441 // void (*oldintr)(int);
1442 int tmptype
, oldtype
= 0, secndflag
= 0, nfnd
;
1443 extern jmp_buf ptabort
;
1448 if (strcmp(cmd
, "RETR"))
1451 cmd2
= runique
? "STOU" : "STOR";
1452 if (command("PASV") != COMPLETE
) {
1453 printf("proxy server does not support third part transfers.\n");
1454 (void) fflush(stdout
);
1460 printf("No primary connection\n");
1461 (void) fflush(stdout
);
1466 if (type
!= tmptype
) {
1483 if (command("PORT %s", pasv
) != COMPLETE
) {
1503 if (setjmp(ptabort
))
1505 null();// oldintr = signal(SIGINT, abortpt);
1506 if (command("%s %s", cmd
, remote
) != PRELIM
) {
1507 null();// (void) signal(SIGINT, oldintr);
1530 if (command("%s %s", cmd2
, local
) != PRELIM
)
1536 null();// (void) signal(SIGINT, oldintr);
1555 printf("local: %s remote: %s\n", local
, remote
);
1556 (void) fflush(stdout
);
1559 null();// (void) signal(SIGINT, SIG_IGN);
1561 if (strcmp(cmd
, "RETR") && !proxy
)
1563 else if (!strcmp(cmd
, "RETR") && proxy
)
1565 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1566 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1587 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1589 *(msg
+1) = (char) DM
;
1590 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1592 fprintfSocket(cout
,"ABOR\r\n");
1594 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1595 if ((nfnd
= empty(&mask
,10)) <= 0) {
1610 null();// (void) signal(SIGINT, oldintr);
1616 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1618 *(msg
+1) = (char)DM
;
1619 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1621 fprintfSocket(cout
,"ABOR\r\n");
1623 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1624 if ((nfnd
= empty(&mask
,10)) <= 0) {
1636 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1637 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1658 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1660 *(msg
+1) = (char)DM
;
1661 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1663 fprintfSocket(cout
,"ABOR\r\n");
1665 // FD_SET(fileno(cin), &mask); // Chris:
1666 if ((nfnd
= empty(&mask
,10)) <= 0) {
1680 null();// (void) signal(SIGINT, oldintr);
1687 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1689 *(msg
+1) = (char)DM
;
1690 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1692 fprintfSocket(cout
,"ABOR\r\n");
1694 // FD_SET(fileno(cin), &mask); // Chris:
1695 if ((nfnd
= empty(&mask
,10)) <= 0) {
1709 // FD_SET(fileno(cin), &mask); // Chris:
1710 if ((nfnd
= empty(&mask
,10)) <= 0) {
1742 null();// (void) signal(SIGINT, oldintr);
1753 // FD_SET(fileno(cin), &mask); // Chris
1754 if ((nfnd
= empty(&mask
,0)) < 0) {
1770 static char new[MAXPATHLEN
];
1771 char *cp
= rindex(local
, '/');
1777 d
= access(cp
? local
: ".", 2);
1784 (void) strcpy(new, local
);
1785 cp
= new + strlen(new);
1788 if (++count
== 100) {
1789 printf("runique: can't find unique file name.\n");
1790 (void) fflush(stdout
);
1799 if ((d
= access(new, 0)) < 0)
1803 else if (*(cp
- 2) == '.')
1806 *(cp
- 2) = *(cp
- 2) + 1;