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);
162 if (s
== INVALID_SOCKET
) {
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 n
= ruserpass(host
, &puser
, &ppass
, &pacct
);
224 while (user
== NULL
) {
225 const char *myname
= "none"; // This needs to become the usename env
228 printf("Name (%s:%s): ", host
, myname
);
230 printf("Name (%s): ", host
);
231 (void) fflush(stdout
);
232 (void) fgets(tmp
, sizeof(tmp
) - 1, stdin
);
233 tmp
[strlen(tmp
) - 1] = '\0';
239 n
= command("USER %s", user
);
242 pass
= getpass("Password:");
243 n
= command("PASS %s", pass
);
248 acct
= getpass("Account:");
249 n
= command("ACCT %s", acct
);
252 fprintf(stderr
, "Login failed.\n");
255 if (!aflag
&& acct
!= NULL
)
256 (void) command("ACCT %s", acct
);
259 for (n
= 0; n
< macnum
; ++n
) {
260 if (!strcmp("init", macros
[n
].mac_name
)) {
261 (void) strcpy(line
, "$init");
263 domacro(margc
, margv
);
273 extern jmp_buf ptabort
;
276 (void) fflush(stdout
);
283 int command(const char *fmt
, ...)
287 void (*oldintr
)(int);
293 vfprintf(stdout
, fmt
, ap
);
296 (void) fflush(stdout
);
299 perror ("No control connection for command");
303 oldintr
= signal(SIGINT
,cmdabort
);
308 vsprintf(buffer
, fmt
, ap
);
310 //DLJ: to work through firewalls - send the command as a single message
311 strcat(buffer
,"\r\n");
312 fprintfSocket(cout
, buffer
);
314 //DLJ: the following two lines are replaced by the strcat above - seems to
315 // make it work through firewalls.
316 // fprintfSocket(cout, "\r\n");
317 // (void) fflush(cout);
319 r
= getreply(!strcmp(fmt
, "QUIT"));
320 if (abrtflag
&& oldintr
!= SIG_IGN
)
322 // (void) signal(SIGINT, oldintr);
326 char reply_string
[BUFSIZ
]; /* last line of previous reply */
337 int originalcode
= 0, continuation
= 0;
338 void (*oldintr
)(int);
342 oldintr
= signal(SIGINT
,cmdabort
);
346 while ((c
= fgetcSocket(cin
)) != '\n') {
347 if (c
== IAC
) { /* handle telnet commands */
348 switch (fgetcSocket(cin
)) {
351 c
= fgetcSocket(cin
);
352 fprintfSocket(cout
, "%c%c%c",IAC
,DONT
,c
);
356 c
= fgetcSocket(cin
);
357 fprintfSocket(cout
, "%c%c%c",IAC
,WONT
,c
);
367 // (void) signal(SIGINT,oldintr);
373 printf("421 Service not available, remote server has closed connection\n");
374 (void) fflush(stdout
);
379 if (c
!= '\r' && (verbose
> 0 ||
380 (verbose
> -1 && n
== '5' && dig
> 4))) {
382 ((dig
== 1 || dig
== 5) && verbose
== 0))
383 printf("%s:",hostname
);
385 (void) fflush(stdout
);
387 if (dig
< 4 && isdigit(c
))
388 code
= code
* 10 + (c
- '0');
389 if (!pflag
&& code
== 227)
391 if (dig
> 4 && pflag
== 1 && isdigit(c
))
394 if (c
!= '\r' && c
!= ')')
401 if (dig
== 4 && c
== '-') {
408 if (cp
< &reply_string
[sizeof(reply_string
) - 1])
411 if (verbose
> 0 || (verbose
> -1 && n
== '5')) {
413 (void) fflush (stdout
);
415 if (continuation
&& code
!= originalcode
) {
416 if (originalcode
== 0)
423 (void) signal(SIGINT
,oldintr
);
424 if (code
== 421 || originalcode
== 421)
426 if (abrtflag
&& oldintr
!= cmdabort
&& oldintr
!= SIG_IGN
)
439 t
.tv_sec
= (long) sec
;
441 return(select(32, mask
, (struct fd_set
*) 0, (struct fd_set
*) 0, &t
));
452 printf("\nsend aborted\n");
453 (void) fflush(stdout
);
454 longjmp(sendabort
, 1);
458 #define HASHBYTES 1024
460 void sendrequest(const char *cmd
, const char *local
, const char *remote
, int printnames
)
465 sig_t (*oldintr
)(), (*oldintp
)();
466 char buf
[BUFSIZ
], *bufp
;
467 long bytes
= 0, hashbytes
= HASHBYTES
;
470 struct timeval start
, stop
;
473 if (verbose
&& printnames
) {
474 if (local
&& *local
!= '-')
475 printf("local: %s ", local
);
477 printf("remote: %s\n", remote
);
478 (void) fflush(stdout
);
481 proxtrans(cmd
, local
, remote
);
488 if (setjmp(sendabort
)) {
497 null();// (void) signal(SIGINT,oldintr);
499 null();// (void) signal(SIGPIPE,oldintp);
503 null();// oldintr = signal(SIGINT, abortsend);
504 if (strcmp(local
, "-") == 0)
506 else if (*local
== '|') {
507 null();// oldintp = signal(SIGPIPE,SIG_IGN);
508 fin
= _popen(local
+ 1, "r");
511 null();// (void) signal(SIGINT, oldintr);
512 null();// (void) signal(SIGPIPE, oldintp);
518 fin
= fopen(local
, "r");
521 null();// (void) signal(SIGINT, oldintr);
526 if (fstat(fileno(fin
), &st
) < 0 ||
527 (st
.st_mode
&S_IFMT
) != S_IFREG
) {
528 fprintf(stdout
, "%s: not a plain file.\n", local
);
529 (void) fflush(stdout
);
530 null();// (void) signal(SIGINT, oldintr);
537 null();// (void) signal(SIGINT, oldintr);
539 null();// (void) signal(SIGPIPE, oldintp);
541 if (closefunc
!= NULL
)
545 if (setjmp(sendabort
))
549 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
550 if (fseek(fin
, (long) restart_point
, 0) < 0) {
553 if (closefunc
!= NULL
)
557 if (command("REST %ld", (long) restart_point
)
560 if (closefunc
!= NULL
)
568 if (command("%s %s", cmd
, remote
) != PRELIM
) {
569 null();// (void) signal(SIGINT, oldintr);
571 null();// (void) signal(SIGPIPE, oldintp);
572 if (closefunc
!= NULL
)
577 if (command("%s", cmd
) != PRELIM
) {
578 null();// (void) signal(SIGINT, oldintr);
580 null();// (void) signal(SIGPIPE, oldintp);
581 if (closefunc
!= NULL
)
585 dout
= dataconn(mode
);
588 (void) gettimeofday(&start
, (struct timezone
*)0);
589 null();// oldintp = signal(SIGPIPE, SIG_IGN);
595 while ((c
= read(fileno(fin
), buf
, sizeof (buf
))) > 0) {
597 for (bufp
= buf
; c
> 0; c
-= d
, bufp
+= d
)
598 if ((d
= send(dout
, bufp
, c
, 0)) <= 0)
601 while (bytes
>= hashbytes
) {
603 hashbytes
+= HASHBYTES
;
605 (void) fflush(stdout
);
608 if (hash
&& bytes
> 0) {
609 if (bytes
< HASHBYTES
)
611 (void) putchar('\n');
612 (void) fflush(stdout
);
618 fprintf(stderr
, "netout: write returned 0?\n");
619 else if (errno
!= EPIPE
)
628 static int bufsize
= 1024;
631 while ((c
= getc(fin
)) != EOF
) {
633 while (hash
&& (bytes
>= hashbytes
)) {
635 (void) fflush(stdout
);
636 hashbytes
+= HASHBYTES
;
638 // Szurgot: The following code is unncessary on Win32.
639 // (void) fputcSocket(dout, '\r');
643 if (ipos
>= bufsize
) {
644 fputSocket(dout
,buf
,ipos
);
645 if(!hash
) (void) putchar('.');
652 fputSocket(dout
,buf
,ipos
);
656 if (bytes
< hashbytes
)
658 (void) putchar('\n');
659 (void) fflush(stdout
);
663 (void) putchar('\n');
664 (void) fflush(stdout
);
668 // if (ferror(dout)) {
669 // if (errno != EPIPE)
676 (void) gettimeofday(&stop
, (struct timezone
*)0);
677 if (closefunc
!= NULL
)
679 if(closesocket(dout
)) {
680 int iret
=WSAGetLastError ();
681 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
682 (void) fflush(stdout
);
685 null();// (void) signal(SIGINT, oldintr);
687 null();// (void) signal(SIGPIPE, oldintp);
689 ptransfer("sent", bytes
, &start
, &stop
);
692 (void) gettimeofday(&stop
, (struct timezone
*)0);
693 null();// (void) signal(SIGINT, oldintr);
695 null();// (void) signal(SIGPIPE, oldintp);
705 if(closesocket(dout
)) {
706 int iret
=WSAGetLastError ();
707 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
708 (void) fflush(stdout
);
713 if (closefunc
!= NULL
&& fin
!= NULL
)
716 ptransfer("sent", bytes
, &start
, &stop
);
728 (void) fflush(stdout
);
729 longjmp(recvabort
, 1);
733 void recvrequest(const char *cmd
, const char *local
, const char *remote
, const char *mode
,
739 void (*oldintr
)(int), (*oldintp
)(int);
740 int oldverbose
= 0, oldtype
= 0, is_retr
, tcrflag
, nfnd
, bare_lfs
= 0;
742 // static char *buf; // Szurgot: Shouldn't this go SOMEWHERE?
744 static int bufsize
= 1024;
745 long bytes
= 0, hashbytes
= HASHBYTES
;
749 struct timeval start
, stop
;
752 is_retr
= strcmp(cmd
, "RETR") == 0;
753 if (is_retr
&& verbose
&& printnames
) {
754 if (local
&& *local
!= '-')
755 printf("local: %s ", local
);
757 printf("remote: %s\n", remote
);
758 (void) fflush(stdout
);
760 if (proxy
&& is_retr
) {
761 proxtrans(cmd
, local
, remote
);
767 tcrflag
= !crflag
&& is_retr
;
768 if (setjmp(recvabort
)) {
777 null();// (void) signal(SIGINT, oldintr);
781 null();// oldintr = signal(SIGINT, abortrecv);
782 if (strcmp(local
, "-") && *local
!= '|') {
785 // This whole thing is a problem... access Won't work on non-existent files
786 if (access(local
, 2) < 0) {
787 char *dir
= rindex(local
, '/');
789 if (errno
!= ENOENT
&& errno
!= EACCES
) {
791 (void) signal(SIGINT
, oldintr
);
797 d
= access(dir
? local
: ".", 2);
802 (void) signal(SIGINT
, oldintr
);
806 if (!runique
&& errno
== EACCES
&&
807 chmod(local
, 0600) < 0) {
809 (void) signal(SIGINT
, oldintr
);
813 if (runique
&& errno
== EACCES
&&
814 (local
= gunique(local
)) == NULL
) {
815 (void) signal(SIGINT
, oldintr
);
820 else if (runique
&& (local
= gunique(local
)) == NULL
) {
821 (void) signal(SIGINT
, oldintr
);
828 null();// (void) signal(SIGINT, oldintr);
832 if (setjmp(recvabort
))
835 if (type
!= TYPE_A
&& (allbinary
== 0 || type
!= TYPE_I
)) {
837 oldverbose
= verbose
;
841 verbose
= oldverbose
;
843 } else if (restart_point
) {
844 if (command("REST %ld", (long) restart_point
) != CONTINUE
)
848 if (command("%s %s", cmd
, remote
) != PRELIM
) {
849 null();// (void) signal(SIGINT, oldintr);
864 verbose
= oldverbose
;
869 if (command("%s", cmd
) != PRELIM
) {
870 null();// (void) signal(SIGINT, oldintr);
885 verbose
= oldverbose
;
893 if (strcmp(local
, "-") == 0)
895 else if (*local
== '|') {
896 null();// oldintp = signal(SIGPIPE, SIG_IGN);
897 fout
= _popen(local
+ 1, "w");
904 fout
= fopen(local
, mode
);
911 (void) gettimeofday(&start
, (struct timezone
*)0);
917 lseek(fileno(fout
), (long) restart_point
, L_SET
) < 0) {
919 if (closefunc
!= NULL
)
924 // while ((c = recv(din, buf, bufsize, 1)) > 0) {
925 // if ((d = write(fileno(fout), buf, c)) != c)
926 // if ((d = write(fileno(fout), buf, c)) != c)
928 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0) {
929 write(fileno(fout
), buf
, c
);
932 while (bytes
>= hashbytes
) {
934 hashbytes
+= HASHBYTES
;
936 (void) fflush(stdout
);
939 if (hash
&& bytes
> 0) {
940 if (bytes
< HASHBYTES
)
942 (void) putchar('\n');
943 (void) fflush(stdout
);
946 // if (errno != EPIPE)
954 // fprintf(stderr, "%s: short write\n", local);
960 register int i
, n
, c
;
962 if (fseek(fout
, 0L, L_SET
) < 0)
967 if ((c
=getc(fout
)) == EOF
)
972 if (fseek(fout
, 0L, L_INCR
) < 0) {
975 if (closefunc
!= NULL
)
980 while ((c
= fgetcSocket(din
)) != EOF
) {
984 while (hash
&& (bytes
>= hashbytes
)) {
986 (void) fflush(stdout
);
987 hashbytes
+= HASHBYTES
;
990 if ((c
= fgetcSocket(din
)) != '\n' || tcrflag
) {
993 (void) putc('\r', fout
);
1002 (void) putc(c
, fout
);
1008 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs
);
1009 printf("File may not have transferred correctly.\n");
1010 (void) fflush(stdout
);
1013 if (bytes
< hashbytes
)
1014 (void) putchar('#');
1015 (void) putchar('\n');
1016 (void) fflush(stdout
);
1018 // if (ferror(din)) {
1019 // if (errno != EPIPE)
1027 if (closefunc
!= NULL
)
1029 null();// (void) signal(SIGINT, oldintr);
1031 null();// (void) signal(SIGPIPE, oldintp);
1032 (void) gettimeofday(&stop
, (struct timezone
*)0);
1033 if(closesocket(din
)) {
1034 int iret
=WSAGetLastError ();
1035 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1036 (void) fflush(stdout
);
1040 if (bytes
> 0 && is_retr
)
1041 ptransfer("received", bytes
, &start
, &stop
);
1056 verbose
= oldverbose
;
1061 /* abort using RFC959 recommended IP,SYNC sequence */
1063 (void) gettimeofday(&stop
, (struct timezone
*)0);
1065 null();// (void) signal(SIGPIPE, oldintr);
1066 null();// (void) signal(SIGINT,SIG_IGN);
1081 verbose
= oldverbose
;
1085 null();// (void) signal(SIGINT,oldintr);
1089 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1091 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
1092 /* after urgent byte rather than before as now is protocol */
1093 if (send(cout
,&msg
,1,MSG_OOB
) != 1) {
1096 fprintfSocket(cout
,"%cABOR\r\n",DM
);
1098 FD_SET(cin
, &mask
); // Need to correct this
1100 FD_SET(din
, &mask
); // Need to correct this
1102 if ((nfnd
= empty(&mask
,10)) <= 0) {
1109 if (din
&& FD_ISSET(din
, &mask
)) {
1110 while (recv(din
, buf
, bufsize
, 0) > 0)
1113 if (getreply(0) == ERROR
&& code
== 552) { /* needed for nic style abort */
1126 if (closefunc
!= NULL
&& fout
!= NULL
)
1129 if(closesocket(din
)) {
1130 int iret
=WSAGetLastError ();
1131 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1132 (void) fflush(stdout
);
1136 ptransfer("received", bytes
, &start
, &stop
);
1137 null();// (void) signal(SIGINT,oldintr);
1143 register char *p
, *a
;
1144 int result
, len
, tmpno
= 0;
1146 int a0
, a1
, a2
, a3
, p0
, p1
;
1150 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1152 perror("ftp: socket");
1155 if ((options
& SO_DEBUG
) &&
1156 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
,
1158 perror("ftp: setsockopt (ignored)");
1159 if (command("PASV") != COMPLETE
) {
1160 printf("Passive mode refused.\n");
1165 * What we've got at this point is a string of comma
1166 * separated one-byte unsigned integer values.
1167 * The first four are the an IP address. The fifth is
1168 * the MSB of the port number, the sixth is the LSB.
1169 * From that we'll prepare a sockaddr_in.
1172 if (sscanf(pasv
,"%d,%d,%d,%d,%d,%d",
1173 &a0
, &a1
, &a2
, &a3
, &p0
, &p1
) != 6) {
1174 printf("Passive mode address scan failure. Shouldn't happen!\n");
1178 bzero(&data_addr
, sizeof(data_addr
));
1179 data_addr
.sin_family
= AF_INET
;
1180 a
= (char *)&data_addr
.sin_addr
.s_addr
;
1185 p
= (char *)&data_addr
.sin_port
;
1189 if (connect(data
, (struct sockaddr
*)&data_addr
,
1190 sizeof(data_addr
)) < 0) {
1191 perror("ftp: connect");
1199 data_addr
= myctladdr
;
1201 data_addr
.sin_port
= 0; /* let system pick one */
1203 (void) close (data
);
1204 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1206 perror("ftp: socket");
1212 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof (on
)) < 0) {
1213 perror("ftp: setsockopt (reuse address)");
1216 if (bind(data
, (struct sockaddr
*)&data_addr
, sizeof (data_addr
)) < 0) {
1217 perror("ftp: bind");
1220 if (options
& SO_DEBUG
&&
1221 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof (on
)) < 0)
1222 perror("ftp: setsockopt (ignored)");
1223 len
= sizeof (data_addr
);
1224 if (getsockname(data
, (struct sockaddr
*)&data_addr
, &len
) < 0) {
1225 perror("ftp: getsockname");
1228 if (listen(data
, 1) < 0)
1229 perror("ftp: listen");
1231 a
= (char *)&data_addr
.sin_addr
;
1232 p
= (char *)&data_addr
.sin_port
;
1233 #define UC(b) (((int)b)&0xff)
1235 command("PORT %d,%d,%d,%d,%d,%d",
1236 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1237 UC(p
[0]), UC(p
[1]));
1238 if (result
== ERROR
&& sendport
== -1) {
1243 return (result
!= COMPLETE
);
1249 (void) fflush(stdout
);
1250 (void) close(data
), data
= -1;
1256 int dataconn(const char *mode
)
1258 struct sockaddr_in from
;
1259 int s
, fromlen
= sizeof (from
);
1264 s
= accept(data
, (struct sockaddr
*) &from
, &fromlen
);
1266 perror("ftp: accept");
1267 (void) closesocket(data
), data
= -1;
1270 if(closesocket(data
)) {
1271 int iret
=WSAGetLastError ();
1272 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1273 (void) fflush(stdout
);
1280 void ptransfer(direction
, bytes
, t0
, t1
)
1281 const char *direction
;
1283 struct timeval
*t0
, *t1
;
1290 s
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.);
1291 #define nz(x) ((x) == 0 ? 1 : (x))
1293 printf("%ld bytes %s in %.1f seconds (%.0f Kbytes/s)\n",
1294 bytes
, direction
, s
, bs
/ 1024.);
1295 (void) fflush(stdout
);
1300 struct timeval *tsum, *t0;
1303 tsum->tv_sec += t0->tv_sec;
1304 tsum->tv_usec += t0->tv_usec;
1305 if (tsum->tv_usec > 1000000)
1306 tsum->tv_sec++, tsum->tv_usec -= 1000000;
1309 void tvsub(tdiff
, t1
, t0
)
1310 struct timeval
*tdiff
, *t1
, *t0
;
1313 tdiff
->tv_sec
= t1
->tv_sec
- t0
->tv_sec
;
1314 tdiff
->tv_usec
= t1
->tv_usec
- t0
->tv_usec
;
1315 if (tdiff
->tv_usec
< 0)
1316 tdiff
->tv_sec
--, tdiff
->tv_usec
+= 1000000;
1319 void psabort(int flag
)
1321 extern int abrtflag
;
1326 void pswitch(int flag
)
1328 extern int proxy
, abrtflag
;
1330 static struct comvars
{
1332 char name
[MAXHOSTNAMELEN
];
1333 struct sockaddr_in mctl
;
1334 struct sockaddr_in hctl
;
1346 char mi
[MAXPATHLEN
];
1347 char mo
[MAXPATHLEN
];
1348 } proxstruct
, tmpstruct
;
1349 struct comvars
*ip
, *op
;
1352 oldintr
= signal(SIGINT
, psabort
);
1367 ip
->connect
= connected
;
1368 connected
= op
->connect
;
1370 (void) strncpy(ip
->name
, hostname
, sizeof(ip
->name
) - 1);
1371 ip
->name
[strlen(ip
->name
)] = '\0';
1374 hostname
= op
->name
;
1375 ip
->hctl
= hisctladdr
;
1376 hisctladdr
= op
->hctl
;
1377 ip
->mctl
= myctladdr
;
1378 myctladdr
= op
->mctl
;
1389 ip
->sunqe
= sunique
;
1390 sunique
= op
->sunqe
;
1391 ip
->runqe
= runique
;
1392 runique
= op
->runqe
;
1397 (void) strncpy(ip
->nti
, ntin
, 16);
1398 (ip
->nti
)[strlen(ip
->nti
)] = '\0';
1399 (void) strcpy(ntin
, op
->nti
);
1400 (void) strncpy(ip
->nto
, ntout
, 16);
1401 (ip
->nto
)[strlen(ip
->nto
)] = '\0';
1402 (void) strcpy(ntout
, op
->nto
);
1403 ip
->mapflg
= mapflag
;
1404 mapflag
= op
->mapflg
;
1405 (void) strncpy(ip
->mi
, mapin
, MAXPATHLEN
- 1);
1406 (ip
->mi
)[strlen(ip
->mi
)] = '\0';
1407 (void) strcpy(mapin
, op
->mi
);
1408 (void) strncpy(ip
->mo
, mapout
, MAXPATHLEN
- 1);
1409 (ip
->mo
)[strlen(ip
->mo
)] = '\0';
1410 (void) strcpy(mapout
, op
->mo
);
1411 // (void) signal(SIGINT, oldintr);
1426 (void) fflush(stdout
);
1430 longjmp(ptabort
, 1);
1434 void proxtrans(cmd
, local
, remote
)
1435 const char *cmd
, *local
, *remote
;
1437 // void (*oldintr)(int);
1438 int tmptype
, oldtype
= 0, secndflag
= 0, nfnd
;
1439 extern jmp_buf ptabort
;
1444 if (strcmp(cmd
, "RETR"))
1447 cmd2
= runique
? "STOU" : "STOR";
1448 if (command("PASV") != COMPLETE
) {
1449 printf("proxy server does not support third part transfers.\n");
1450 (void) fflush(stdout
);
1456 printf("No primary connection\n");
1457 (void) fflush(stdout
);
1462 if (type
!= tmptype
) {
1479 if (command("PORT %s", pasv
) != COMPLETE
) {
1499 if (setjmp(ptabort
))
1501 null();// oldintr = signal(SIGINT, abortpt);
1502 if (command("%s %s", cmd
, remote
) != PRELIM
) {
1503 null();// (void) signal(SIGINT, oldintr);
1526 if (command("%s %s", cmd2
, local
) != PRELIM
)
1532 null();// (void) signal(SIGINT, oldintr);
1551 printf("local: %s remote: %s\n", local
, remote
);
1552 (void) fflush(stdout
);
1555 null();// (void) signal(SIGINT, SIG_IGN);
1557 if (strcmp(cmd
, "RETR") && !proxy
)
1559 else if (!strcmp(cmd
, "RETR") && proxy
)
1561 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1562 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1583 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1585 *(msg
+1) = (char) DM
;
1586 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1588 fprintfSocket(cout
,"ABOR\r\n");
1590 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1591 if ((nfnd
= empty(&mask
,10)) <= 0) {
1606 null();// (void) signal(SIGINT, oldintr);
1612 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1614 *(msg
+1) = (char)DM
;
1615 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1617 fprintfSocket(cout
,"ABOR\r\n");
1619 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1620 if ((nfnd
= empty(&mask
,10)) <= 0) {
1632 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1633 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1654 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1656 *(msg
+1) = (char)DM
;
1657 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1659 fprintfSocket(cout
,"ABOR\r\n");
1661 // FD_SET(fileno(cin), &mask); // Chris:
1662 if ((nfnd
= empty(&mask
,10)) <= 0) {
1676 null();// (void) signal(SIGINT, oldintr);
1683 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1685 *(msg
+1) = (char)DM
;
1686 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1688 fprintfSocket(cout
,"ABOR\r\n");
1690 // FD_SET(fileno(cin), &mask); // Chris:
1691 if ((nfnd
= empty(&mask
,10)) <= 0) {
1705 // FD_SET(fileno(cin), &mask); // Chris:
1706 if ((nfnd
= empty(&mask
,10)) <= 0) {
1738 null();// (void) signal(SIGINT, oldintr);
1749 // FD_SET(fileno(cin), &mask); // Chris
1750 if ((nfnd
= empty(&mask
,0)) < 0) {
1766 static char new[MAXPATHLEN
];
1767 char *cp
= rindex(local
, '/');
1773 d
= access(cp
? local
: ".", 2);
1780 (void) strcpy(new, local
);
1781 cp
= new + strlen(new);
1784 if (++count
== 100) {
1785 printf("runique: can't find unique file name.\n");
1786 (void) fflush(stdout
);
1795 if ((d
= access(new, 0)) < 0)
1799 else if (*(cp
- 2) == '.')
1802 *(cp
- 2) = *(cp
- 2) + 1;