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
);
298 if (cout
== (int) NULL
) {
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
)
464 int (*closefunc
)(), _pclose(), fclose();
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
);
586 if (dout
== (int)NULL
)
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
,
738 int (*closefunc
)(), _pclose(), fclose();
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
;
751 extern void *malloc();
753 is_retr
= strcmp(cmd
, "RETR") == 0;
754 if (is_retr
&& verbose
&& printnames
) {
755 if (local
&& *local
!= '-')
756 printf("local: %s ", local
);
758 printf("remote: %s\n", remote
);
759 (void) fflush(stdout
);
761 if (proxy
&& is_retr
) {
762 proxtrans(cmd
, local
, remote
);
768 tcrflag
= !crflag
&& is_retr
;
769 if (setjmp(recvabort
)) {
778 null();// (void) signal(SIGINT, oldintr);
782 null();// oldintr = signal(SIGINT, abortrecv);
783 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
;
891 if (din
== (int)NULL
)
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 ((c
= recv(din
, buf
, bufsize
, 0)) > 0)
1113 if ((c
= 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);
1141 * Need to start a listen on the data channel
1142 * before we send the command, otherwise the
1143 * server's connect may fail.
1150 register char *p
, *a
;
1151 int result
, len
, tmpno
= 0;
1153 int a0
, a1
, a2
, a3
, p0
, p1
;
1157 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1159 perror("ftp: socket");
1162 if ((options
& SO_DEBUG
) &&
1163 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
,
1165 perror("ftp: setsockopt (ignored)");
1166 if (command("PASV") != COMPLETE
) {
1167 printf("Passive mode refused.\n");
1172 * What we've got at this point is a string of comma
1173 * separated one-byte unsigned integer values.
1174 * The first four are the an IP address. The fifth is
1175 * the MSB of the port number, the sixth is the LSB.
1176 * From that we'll prepare a sockaddr_in.
1179 if (sscanf(pasv
,"%d,%d,%d,%d,%d,%d",
1180 &a0
, &a1
, &a2
, &a3
, &p0
, &p1
) != 6) {
1181 printf("Passive mode address scan failure. Shouldn't happen!\n");
1185 bzero(&data_addr
, sizeof(data_addr
));
1186 data_addr
.sin_family
= AF_INET
;
1187 a
= (char *)&data_addr
.sin_addr
.s_addr
;
1192 p
= (char *)&data_addr
.sin_port
;
1196 if (connect(data
, (struct sockaddr
*)&data_addr
,
1197 sizeof(data_addr
)) < 0) {
1198 perror("ftp: connect");
1206 data_addr
= myctladdr
;
1208 data_addr
.sin_port
= 0; /* let system pick one */
1210 (void) close (data
);
1211 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1213 perror("ftp: socket");
1219 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof (on
)) < 0) {
1220 perror("ftp: setsockopt (reuse address)");
1223 if (bind(data
, (struct sockaddr
*)&data_addr
, sizeof (data_addr
)) < 0) {
1224 perror("ftp: bind");
1227 if (options
& SO_DEBUG
&&
1228 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof (on
)) < 0)
1229 perror("ftp: setsockopt (ignored)");
1230 len
= sizeof (data_addr
);
1231 if (getsockname(data
, (struct sockaddr
*)&data_addr
, &len
) < 0) {
1232 perror("ftp: getsockname");
1235 if (listen(data
, 1) < 0)
1236 perror("ftp: listen");
1238 a
= (char *)&data_addr
.sin_addr
;
1239 p
= (char *)&data_addr
.sin_port
;
1240 #define UC(b) (((int)b)&0xff)
1242 command("PORT %d,%d,%d,%d,%d,%d",
1243 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1244 UC(p
[0]), UC(p
[1]));
1245 if (result
== ERROR
&& sendport
== -1) {
1250 return (result
!= COMPLETE
);
1256 (void) fflush(stdout
);
1257 (void) close(data
), data
= -1;
1263 int dataconn(const char *mode
)
1265 struct sockaddr_in from
;
1266 int s
, fromlen
= sizeof (from
);
1271 s
= accept(data
, (struct sockaddr
*) &from
, &fromlen
);
1273 perror("ftp: accept");
1274 (void) closesocket(data
), data
= -1;
1275 return (int) (NULL
);
1277 if(closesocket(data
)) {
1278 int iret
=WSAGetLastError ();
1279 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1280 (void) fflush(stdout
);
1287 void ptransfer(direction
, bytes
, t0
, t1
)
1288 const char *direction
;
1290 struct timeval
*t0
, *t1
;
1297 s
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.);
1298 #define nz(x) ((x) == 0 ? 1 : (x))
1300 printf("%ld bytes %s in %.1f seconds (%.0f Kbytes/s)\n",
1301 bytes
, direction
, s
, bs
/ 1024.);
1302 (void) fflush(stdout
);
1307 struct timeval *tsum, *t0;
1310 tsum->tv_sec += t0->tv_sec;
1311 tsum->tv_usec += t0->tv_usec;
1312 if (tsum->tv_usec > 1000000)
1313 tsum->tv_sec++, tsum->tv_usec -= 1000000;
1316 void tvsub(tdiff
, t1
, t0
)
1317 struct timeval
*tdiff
, *t1
, *t0
;
1320 tdiff
->tv_sec
= t1
->tv_sec
- t0
->tv_sec
;
1321 tdiff
->tv_usec
= t1
->tv_usec
- t0
->tv_usec
;
1322 if (tdiff
->tv_usec
< 0)
1323 tdiff
->tv_sec
--, tdiff
->tv_usec
+= 1000000;
1326 void psabort(int flag
)
1328 extern int abrtflag
;
1333 void pswitch(int flag
)
1335 extern int proxy
, abrtflag
;
1337 static struct comvars
{
1339 char name
[MAXHOSTNAMELEN
];
1340 struct sockaddr_in mctl
;
1341 struct sockaddr_in hctl
;
1353 char mi
[MAXPATHLEN
];
1354 char mo
[MAXPATHLEN
];
1355 } proxstruct
, tmpstruct
;
1356 struct comvars
*ip
, *op
;
1359 oldintr
= signal(SIGINT
, psabort
);
1374 ip
->connect
= connected
;
1375 connected
= op
->connect
;
1377 (void) strncpy(ip
->name
, hostname
, sizeof(ip
->name
) - 1);
1378 ip
->name
[strlen(ip
->name
)] = '\0';
1381 hostname
= op
->name
;
1382 ip
->hctl
= hisctladdr
;
1383 hisctladdr
= op
->hctl
;
1384 ip
->mctl
= myctladdr
;
1385 myctladdr
= op
->mctl
;
1396 ip
->sunqe
= sunique
;
1397 sunique
= op
->sunqe
;
1398 ip
->runqe
= runique
;
1399 runique
= op
->runqe
;
1404 (void) strncpy(ip
->nti
, ntin
, 16);
1405 (ip
->nti
)[strlen(ip
->nti
)] = '\0';
1406 (void) strcpy(ntin
, op
->nti
);
1407 (void) strncpy(ip
->nto
, ntout
, 16);
1408 (ip
->nto
)[strlen(ip
->nto
)] = '\0';
1409 (void) strcpy(ntout
, op
->nto
);
1410 ip
->mapflg
= mapflag
;
1411 mapflag
= op
->mapflg
;
1412 (void) strncpy(ip
->mi
, mapin
, MAXPATHLEN
- 1);
1413 (ip
->mi
)[strlen(ip
->mi
)] = '\0';
1414 (void) strcpy(mapin
, op
->mi
);
1415 (void) strncpy(ip
->mo
, mapout
, MAXPATHLEN
- 1);
1416 (ip
->mo
)[strlen(ip
->mo
)] = '\0';
1417 (void) strcpy(mapout
, op
->mo
);
1418 // (void) signal(SIGINT, oldintr);
1433 (void) fflush(stdout
);
1437 longjmp(ptabort
, 1);
1441 void proxtrans(cmd
, local
, remote
)
1442 const char *cmd
, *local
, *remote
;
1444 // void (*oldintr)(int);
1445 int tmptype
, oldtype
= 0, secndflag
= 0, nfnd
;
1446 extern jmp_buf ptabort
;
1451 if (strcmp(cmd
, "RETR"))
1454 cmd2
= runique
? "STOU" : "STOR";
1455 if (command("PASV") != COMPLETE
) {
1456 printf("proxy server does not support third part transfers.\n");
1457 (void) fflush(stdout
);
1463 printf("No primary connection\n");
1464 (void) fflush(stdout
);
1469 if (type
!= tmptype
) {
1486 if (command("PORT %s", pasv
) != COMPLETE
) {
1506 if (setjmp(ptabort
))
1508 null();// oldintr = signal(SIGINT, abortpt);
1509 if (command("%s %s", cmd
, remote
) != PRELIM
) {
1510 null();// (void) signal(SIGINT, oldintr);
1533 if (command("%s %s", cmd2
, local
) != PRELIM
)
1539 null();// (void) signal(SIGINT, oldintr);
1558 printf("local: %s remote: %s\n", local
, remote
);
1559 (void) fflush(stdout
);
1562 null();// (void) signal(SIGINT, SIG_IGN);
1564 if (strcmp(cmd
, "RETR") && !proxy
)
1566 else if (!strcmp(cmd
, "RETR") && proxy
)
1568 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1569 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1590 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1592 *(msg
+1) = (char) DM
;
1593 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1595 fprintfSocket(cout
,"ABOR\r\n");
1597 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1598 if ((nfnd
= empty(&mask
,10)) <= 0) {
1613 null();// (void) signal(SIGINT, oldintr);
1619 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1621 *(msg
+1) = (char)DM
;
1622 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1624 fprintfSocket(cout
,"ABOR\r\n");
1626 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1627 if ((nfnd
= empty(&mask
,10)) <= 0) {
1639 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1640 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1661 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1663 *(msg
+1) = (char)DM
;
1664 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1666 fprintfSocket(cout
,"ABOR\r\n");
1668 // FD_SET(fileno(cin), &mask); // Chris:
1669 if ((nfnd
= empty(&mask
,10)) <= 0) {
1683 null();// (void) signal(SIGINT, oldintr);
1690 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1692 *(msg
+1) = (char)DM
;
1693 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1695 fprintfSocket(cout
,"ABOR\r\n");
1697 // FD_SET(fileno(cin), &mask); // Chris:
1698 if ((nfnd
= empty(&mask
,10)) <= 0) {
1712 // FD_SET(fileno(cin), &mask); // Chris:
1713 if ((nfnd
= empty(&mask
,10)) <= 0) {
1745 null();// (void) signal(SIGINT, oldintr);
1756 // FD_SET(fileno(cin), &mask); // Chris
1757 if ((nfnd
= empty(&mask
,0)) < 0) {
1773 static char new[MAXPATHLEN
];
1774 char *cp
= rindex(local
, '/');
1780 d
= access(cp
? local
: ".", 2);
1787 (void) strcpy(new, local
);
1788 cp
= new + strlen(new);
1791 if (++count
== 100) {
1792 printf("runique: can't find unique file name.\n");
1793 (void) fflush(stdout
);
1802 if ((d
= access(new, 0)) < 0)
1806 else if (*(cp
- 2) == '.')
1809 *(cp
- 2) = *(cp
- 2) + 1;