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 (c
= 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
!= '|') {
784 // This whole thing is a problem... access Won't work on non-existent files
785 if (access(local
, 2) < 0) {
786 char *dir
= rindex(local
, '/');
788 if (errno
!= ENOENT
&& errno
!= EACCES
) {
790 (void) signal(SIGINT
, oldintr
);
796 d
= access(dir
? local
: ".", 2);
801 (void) signal(SIGINT
, oldintr
);
805 if (!runique
&& errno
== EACCES
&&
806 chmod(local
, 0600) < 0) {
808 (void) signal(SIGINT
, oldintr
);
812 if (runique
&& errno
== EACCES
&&
813 (local
= gunique(local
)) == NULL
) {
814 (void) signal(SIGINT
, oldintr
);
819 else if (runique
&& (local
= gunique(local
)) == NULL
) {
820 (void) signal(SIGINT
, oldintr
);
827 null();// (void) signal(SIGINT, oldintr);
831 if (setjmp(recvabort
))
834 if (type
!= TYPE_A
&& (allbinary
== 0 || type
!= TYPE_I
)) {
836 oldverbose
= verbose
;
840 verbose
= oldverbose
;
842 } else if (restart_point
) {
843 if (command("REST %ld", (long) restart_point
) != CONTINUE
)
847 if (command("%s %s", cmd
, remote
) != PRELIM
) {
848 null();// (void) signal(SIGINT, oldintr);
863 verbose
= oldverbose
;
868 if (command("%s", cmd
) != PRELIM
) {
869 null();// (void) signal(SIGINT, oldintr);
884 verbose
= oldverbose
;
892 if (strcmp(local
, "-") == 0)
894 else if (*local
== '|') {
895 null();// oldintp = signal(SIGPIPE, SIG_IGN);
896 fout
= _popen(local
+ 1, "w");
903 fout
= fopen(local
, mode
);
910 (void) gettimeofday(&start
, (struct timezone
*)0);
916 lseek(fileno(fout
), (long) restart_point
, L_SET
) < 0) {
918 if (closefunc
!= NULL
)
923 // while ((c = recv(din, buf, bufsize, 1)) > 0) {
924 // if ((d = write(fileno(fout), buf, c)) != c)
925 // if ((d = write(fileno(fout), buf, c)) != c)
927 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0) {
928 write(fileno(fout
), buf
, c
);
931 while (bytes
>= hashbytes
) {
933 hashbytes
+= HASHBYTES
;
935 (void) fflush(stdout
);
938 if (hash
&& bytes
> 0) {
939 if (bytes
< HASHBYTES
)
941 (void) putchar('\n');
942 (void) fflush(stdout
);
945 // if (errno != EPIPE)
953 // fprintf(stderr, "%s: short write\n", local);
959 register int i
, n
, c
;
961 if (fseek(fout
, 0L, L_SET
) < 0)
966 if ((c
=getc(fout
)) == EOF
)
971 if (fseek(fout
, 0L, L_INCR
) < 0) {
974 if (closefunc
!= NULL
)
979 while ((c
= fgetcSocket(din
)) != EOF
) {
983 while (hash
&& (bytes
>= hashbytes
)) {
985 (void) fflush(stdout
);
986 hashbytes
+= HASHBYTES
;
989 if ((c
= fgetcSocket(din
)) != '\n' || tcrflag
) {
992 (void) putc('\r', fout
);
1001 (void) putc(c
, fout
);
1007 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs
);
1008 printf("File may not have transferred correctly.\n");
1009 (void) fflush(stdout
);
1012 if (bytes
< hashbytes
)
1013 (void) putchar('#');
1014 (void) putchar('\n');
1015 (void) fflush(stdout
);
1017 // if (ferror(din)) {
1018 // if (errno != EPIPE)
1026 if (closefunc
!= NULL
)
1028 null();// (void) signal(SIGINT, oldintr);
1030 null();// (void) signal(SIGPIPE, oldintp);
1031 (void) gettimeofday(&stop
, (struct timezone
*)0);
1032 if(closesocket(din
)) {
1033 int iret
=WSAGetLastError ();
1034 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1035 (void) fflush(stdout
);
1039 if (bytes
> 0 && is_retr
)
1040 ptransfer("received", bytes
, &start
, &stop
);
1055 verbose
= oldverbose
;
1060 /* abort using RFC959 recommended IP,SYNC sequence */
1062 (void) gettimeofday(&stop
, (struct timezone
*)0);
1064 null();// (void) signal(SIGPIPE, oldintr);
1065 null();// (void) signal(SIGINT,SIG_IGN);
1080 verbose
= oldverbose
;
1084 null();// (void) signal(SIGINT,oldintr);
1088 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1090 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
1091 /* after urgent byte rather than before as now is protocol */
1092 if (send(cout
,&msg
,1,MSG_OOB
) != 1) {
1095 fprintfSocket(cout
,"%cABOR\r\n",DM
);
1097 FD_SET(cin
, &mask
); // Need to correct this
1099 FD_SET(din
, &mask
); // Need to correct this
1101 if ((nfnd
= empty(&mask
,10)) <= 0) {
1108 if (din
&& FD_ISSET(din
, &mask
)) {
1109 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0)
1112 if ((c
= getreply(0)) == ERROR
&& code
== 552) { /* needed for nic style abort */
1125 if (closefunc
!= NULL
&& fout
!= NULL
)
1128 if(closesocket(din
)) {
1129 int iret
=WSAGetLastError ();
1130 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1131 (void) fflush(stdout
);
1135 ptransfer("received", bytes
, &start
, &stop
);
1136 null();// (void) signal(SIGINT,oldintr);
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(const 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;
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
)
1280 const char *direction
;
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 %.1f seconds (%.0f 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);
1425 (void) fflush(stdout
);
1429 longjmp(ptabort
, 1);
1433 void proxtrans(cmd
, local
, remote
)
1434 const char *cmd
, *local
, *remote
;
1436 // void (*oldintr)(int);
1437 int tmptype
, oldtype
= 0, secndflag
= 0, nfnd
;
1438 extern jmp_buf ptabort
;
1443 if (strcmp(cmd
, "RETR"))
1446 cmd2
= runique
? "STOU" : "STOR";
1447 if (command("PASV") != COMPLETE
) {
1448 printf("proxy server does not support third part transfers.\n");
1449 (void) fflush(stdout
);
1455 printf("No primary connection\n");
1456 (void) fflush(stdout
);
1461 if (type
!= tmptype
) {
1478 if (command("PORT %s", pasv
) != COMPLETE
) {
1498 if (setjmp(ptabort
))
1500 null();// oldintr = signal(SIGINT, abortpt);
1501 if (command("%s %s", cmd
, remote
) != PRELIM
) {
1502 null();// (void) signal(SIGINT, oldintr);
1525 if (command("%s %s", cmd2
, local
) != PRELIM
)
1531 null();// (void) signal(SIGINT, oldintr);
1550 printf("local: %s remote: %s\n", local
, remote
);
1551 (void) fflush(stdout
);
1554 null();// (void) signal(SIGINT, SIG_IGN);
1556 if (strcmp(cmd
, "RETR") && !proxy
)
1558 else if (!strcmp(cmd
, "RETR") && proxy
)
1560 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1561 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1582 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1584 *(msg
+1) = (char) DM
;
1585 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1587 fprintfSocket(cout
,"ABOR\r\n");
1589 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1590 if ((nfnd
= empty(&mask
,10)) <= 0) {
1605 null();// (void) signal(SIGINT, oldintr);
1611 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1613 *(msg
+1) = (char)DM
;
1614 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1616 fprintfSocket(cout
,"ABOR\r\n");
1618 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1619 if ((nfnd
= empty(&mask
,10)) <= 0) {
1631 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1632 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1653 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1655 *(msg
+1) = (char)DM
;
1656 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1658 fprintfSocket(cout
,"ABOR\r\n");
1660 // FD_SET(fileno(cin), &mask); // Chris:
1661 if ((nfnd
= empty(&mask
,10)) <= 0) {
1675 null();// (void) signal(SIGINT, oldintr);
1682 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1684 *(msg
+1) = (char)DM
;
1685 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1687 fprintfSocket(cout
,"ABOR\r\n");
1689 // FD_SET(fileno(cin), &mask); // Chris:
1690 if ((nfnd
= empty(&mask
,10)) <= 0) {
1704 // FD_SET(fileno(cin), &mask); // Chris:
1705 if ((nfnd
= empty(&mask
,10)) <= 0) {
1737 null();// (void) signal(SIGINT, oldintr);
1748 // FD_SET(fileno(cin), &mask); // Chris
1749 if ((nfnd
= empty(&mask
,0)) < 0) {
1765 static char new[MAXPATHLEN
];
1766 char *cp
= rindex(local
, '/');
1772 d
= access(cp
? local
: ".", 2);
1779 (void) strcpy(new, local
);
1780 cp
= new + strlen(new);
1783 if (++count
== 100) {
1784 printf("runique: can't find unique file name.\n");
1785 (void) fflush(stdout
);
1794 if ((d
= access(new, 0)) < 0)
1798 else if (*(cp
- 2) == '.')
1801 *(cp
- 2) = *(cp
- 2) + 1;