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";
27 #ifndef MAXHOSTNAMELEN
28 #define MAXHOSTNAMELEN 64
32 #define vfprintf(a,b,c) _doprnt(b,c,a)
36 /* FD_SET wasn't defined until 4.0. its a cheap test for uid_t presence */
38 #define NBBY 8 /* number of bits in a byte */
40 * Select uses bit masks of file descriptors in longs.
41 * These macros manipulate such bit fields (the filesystem macros use chars).
42 * FD_SETSIZE may be defined by the user, but the default here
43 * should be >= NOFILE (param.h).
46 #define FD_SETSIZE 256
50 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
52 #define howmany(x, y) (((x)+((y)-1))/(y))
55 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
56 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
57 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
58 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
65 struct sockaddr_in hisctladdr
;
66 struct sockaddr_in data_addr
;
71 struct sockaddr_in myctladdr
;
74 off_t restart_point
= 0;
77 int dataconn(const char *mode
);
79 int command(const char *fmt
, ...);
83 typedef void (*Sig_t
)(int);
87 void psabort(int sig
);
89 char *hookup(const char *host
, int port
)
91 register struct hostent
*hp
= 0;
94 static char hostnamebuf
[80];
96 bzero((char *)&hisctladdr
, sizeof (hisctladdr
));
97 hisctladdr
.sin_addr
.s_addr
= inet_addr(host
);
98 if (hisctladdr
.sin_addr
.s_addr
!= (unsigned long)-1) {
99 hisctladdr
.sin_family
= AF_INET
;
100 (void) strncpy(hostnamebuf
, host
, sizeof(hostnamebuf
));
102 hp
= gethostbyname(host
);
104 fprintf(stderr
, "ftp: %s: ", host
);
105 herror((char *)NULL
);
109 hisctladdr
.sin_family
= hp
->h_addrtype
;
110 bcopy(hp
->h_addr_list
[0],
111 (caddr_t
)&hisctladdr
.sin_addr
, hp
->h_length
);
112 (void) strncpy(hostnamebuf
, hp
->h_name
, sizeof(hostnamebuf
));
114 hostname
= hostnamebuf
;
115 s
= socket(hisctladdr
.sin_family
, SOCK_STREAM
, 0);
116 if (s
== INVALID_SOCKET
) {
117 perror("ftp: socket");
121 hisctladdr
.sin_port
= port
;
122 while (connect(s
, (struct sockaddr
*)&hisctladdr
, sizeof (hisctladdr
)) < 0) {
123 if (hp
&& hp
->h_addr_list
[1]) {
126 fprintf(stderr
, "ftp: connect to address %s: ",
127 inet_ntoa(hisctladdr
.sin_addr
));
131 bcopy(hp
->h_addr_list
[0],
132 (caddr_t
)&hisctladdr
.sin_addr
, hp
->h_length
);
133 fprintf(stdout
, "Trying %s...\n",
134 inet_ntoa(hisctladdr
.sin_addr
));
135 (void) fflush(stdout
);
137 s
= socket(hisctladdr
.sin_family
, SOCK_STREAM
, 0);
138 if (s
== INVALID_SOCKET
) {
139 perror("ftp: socket");
145 perror("ftp: connect");
149 len
= sizeof (myctladdr
);
150 if (getsockname(s
, (struct sockaddr
*)&myctladdr
, &len
) < 0) {
151 perror("ftp: getsockname");
157 printf("Connected to %s.\n", hostname
);
158 (void) fflush(stdout
);
160 if (getreply(0) > 2) { /* read startup message from server */
169 if (setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, (const char *) &on
, sizeof(on
))
171 perror("ftp: setsockopt");
174 #endif //SO_OOBINLINE
182 int login(const char *host
)
185 char *puser
, *ppass
, *pacct
;
186 const char *user
, *pass
, *acct
;
189 user
= pass
= acct
= 0;
190 n
= ruserpass(host
, &puser
, &ppass
, &pacct
);
200 while (user
== NULL
) {
201 const char *myname
= "none"; // This needs to become the usename env
204 printf("Name (%s:%s): ", host
, myname
);
206 printf("Name (%s): ", host
);
207 (void) fflush(stdout
);
208 (void) fgets(tmp
, sizeof(tmp
) - 1, stdin
);
209 tmp
[strlen(tmp
) - 1] = '\0';
215 n
= command("USER %s", user
);
218 pass
= getpass("Password:");
219 n
= command("PASS %s", pass
);
224 acct
= getpass("Account:");
225 n
= command("ACCT %s", acct
);
228 fprintf(stderr
, "Login failed.\n");
231 if (!aflag
&& acct
!= NULL
)
232 (void) command("ACCT %s", acct
);
235 for (n
= 0; n
< macnum
; ++n
) {
236 if (!strcmp("init", macros
[n
].mac_name
)) {
237 (void) strcpy(line
, "$init");
239 domacro(margc
, margv
);
249 extern jmp_buf ptabort
;
252 (void) fflush(stdout
);
259 int command(const char *fmt
, ...)
263 void (*oldintr
)(int);
269 vfprintf(stdout
, fmt
, ap
);
272 (void) fflush(stdout
);
275 perror ("No control connection for command");
279 oldintr
= signal(SIGINT
,cmdabort
);
284 vsprintf(buffer
, fmt
, ap
);
286 //DLJ: to work through firewalls - send the command as a single message
287 strcat(buffer
,"\r\n");
288 fprintfSocket(cout
, buffer
);
290 //DLJ: the following two lines are replaced by the strcat above - seems to
291 // make it work through firewalls.
292 // fprintfSocket(cout, "\r\n");
293 // (void) fflush(cout);
295 r
= getreply(!strcmp(fmt
, "QUIT"));
296 if (abrtflag
&& oldintr
!= SIG_IGN
)
298 // (void) signal(SIGINT, oldintr);
302 char reply_string
[BUFSIZ
]; /* last line of previous reply */
313 int originalcode
= 0, continuation
= 0;
314 void (*oldintr
)(int);
318 oldintr
= signal(SIGINT
,cmdabort
);
322 while ((c
= fgetcSocket(cin
)) != '\n') {
323 if (c
== IAC
) { /* handle telnet commands */
324 switch (fgetcSocket(cin
)) {
327 c
= fgetcSocket(cin
);
328 fprintfSocket(cout
, "%c%c%c",IAC
,DONT
,c
);
332 c
= fgetcSocket(cin
);
333 fprintfSocket(cout
, "%c%c%c",IAC
,WONT
,c
);
343 // (void) signal(SIGINT,oldintr);
349 printf("421 Service not available, remote server has closed connection\n");
350 (void) fflush(stdout
);
355 if (c
!= '\r' && (verbose
> 0 ||
356 (verbose
> -1 && n
== '5' && dig
> 4))) {
358 ((dig
== 1 || dig
== 5) && verbose
== 0))
359 printf("%s:",hostname
);
361 (void) fflush(stdout
);
363 if (dig
< 4 && isdigit(c
))
364 code
= code
* 10 + (c
- '0');
365 if (!pflag
&& code
== 227)
367 if (dig
> 4 && pflag
== 1 && isdigit(c
))
370 if (c
!= '\r' && c
!= ')')
377 if (dig
== 4 && c
== '-') {
384 if (cp
< &reply_string
[sizeof(reply_string
) - 1])
387 if (verbose
> 0 || (verbose
> -1 && n
== '5')) {
389 (void) fflush (stdout
);
391 if (continuation
&& code
!= originalcode
) {
392 if (originalcode
== 0)
399 (void) signal(SIGINT
,oldintr
);
400 if (code
== 421 || originalcode
== 421)
402 if (abrtflag
&& oldintr
!= cmdabort
&& oldintr
!= SIG_IGN
)
415 t
.tv_sec
= (long) sec
;
417 return(select(32, mask
, (struct fd_set
*) 0, (struct fd_set
*) 0, &t
));
428 printf("\nsend aborted\n");
429 (void) fflush(stdout
);
430 longjmp(sendabort
, 1);
434 #define HASHBYTES 1024
436 void sendrequest(const char *cmd
, const char *local
, const char *remote
, int printnames
)
441 sig_t (*oldintr
)(), (*oldintp
)();
442 char buf
[BUFSIZ
], *bufp
;
443 long bytes
= 0, hashbytes
= HASHBYTES
;
446 struct timeval start
, stop
;
449 if (verbose
&& printnames
) {
450 if (local
&& *local
!= '-')
451 printf("local: %s ", local
);
453 printf("remote: %s\n", remote
);
454 (void) fflush(stdout
);
457 proxtrans(cmd
, local
, remote
);
464 if (setjmp(sendabort
)) {
473 null();// (void) signal(SIGINT,oldintr);
475 null();// (void) signal(SIGPIPE,oldintp);
479 null();// oldintr = signal(SIGINT, abortsend);
480 if (strcmp(local
, "-") == 0)
482 else if (*local
== '|') {
483 null();// oldintp = signal(SIGPIPE,SIG_IGN);
484 fin
= _popen(local
+ 1, "r");
487 null();// (void) signal(SIGINT, oldintr);
488 null();// (void) signal(SIGPIPE, oldintp);
494 fin
= fopen(local
, "r");
497 null();// (void) signal(SIGINT, oldintr);
502 if (fstat(fileno(fin
), &st
) < 0 ||
503 (st
.st_mode
&S_IFMT
) != S_IFREG
) {
504 fprintf(stdout
, "%s: not a plain file.\n", local
);
505 (void) fflush(stdout
);
506 null();// (void) signal(SIGINT, oldintr);
513 null();// (void) signal(SIGINT, oldintr);
515 null();// (void) signal(SIGPIPE, oldintp);
517 if (closefunc
!= NULL
)
521 if (setjmp(sendabort
))
525 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
526 if (fseek(fin
, (long) restart_point
, 0) < 0) {
529 if (closefunc
!= NULL
)
533 if (command("REST %ld", (long) restart_point
)
536 if (closefunc
!= NULL
)
544 if (command("%s %s", cmd
, remote
) != PRELIM
) {
545 null();// (void) signal(SIGINT, oldintr);
547 null();// (void) signal(SIGPIPE, oldintp);
548 if (closefunc
!= NULL
)
553 if (command("%s", cmd
) != PRELIM
) {
554 null();// (void) signal(SIGINT, oldintr);
556 null();// (void) signal(SIGPIPE, oldintp);
557 if (closefunc
!= NULL
)
561 dout
= dataconn(mode
);
564 (void) gettimeofday(&start
, (struct timezone
*)0);
565 null();// oldintp = signal(SIGPIPE, SIG_IGN);
571 while ((c
= read(fileno(fin
), buf
, sizeof (buf
))) > 0) {
573 for (bufp
= buf
; c
> 0; c
-= d
, bufp
+= d
)
574 if ((d
= send(dout
, bufp
, c
, 0)) <= 0)
577 while (bytes
>= hashbytes
) {
579 hashbytes
+= HASHBYTES
;
581 (void) fflush(stdout
);
584 if (hash
&& bytes
> 0) {
585 if (bytes
< HASHBYTES
)
587 (void) putchar('\n');
588 (void) fflush(stdout
);
594 fprintf(stderr
, "netout: write returned 0?\n");
595 else if (errno
!= EPIPE
)
604 static int bufsize
= 1024;
607 while ((c
= getc(fin
)) != EOF
) {
609 while (hash
&& (bytes
>= hashbytes
)) {
611 (void) fflush(stdout
);
612 hashbytes
+= HASHBYTES
;
614 // Szurgot: The following code is unncessary on Win32.
615 // (void) fputcSocket(dout, '\r');
619 if (ipos
>= bufsize
) {
620 fputSocket(dout
,buf
,ipos
);
621 if(!hash
) (void) putchar('.');
628 fputSocket(dout
,buf
,ipos
);
632 if (bytes
< hashbytes
)
634 (void) putchar('\n');
635 (void) fflush(stdout
);
639 (void) putchar('\n');
640 (void) fflush(stdout
);
644 // if (ferror(dout)) {
645 // if (errno != EPIPE)
652 (void) gettimeofday(&stop
, (struct timezone
*)0);
653 if (closefunc
!= NULL
)
655 if(closesocket(dout
)) {
656 int iret
=WSAGetLastError ();
657 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
658 (void) fflush(stdout
);
661 null();// (void) signal(SIGINT, oldintr);
663 null();// (void) signal(SIGPIPE, oldintp);
665 ptransfer("sent", bytes
, &start
, &stop
);
668 (void) gettimeofday(&stop
, (struct timezone
*)0);
669 null();// (void) signal(SIGINT, oldintr);
671 null();// (void) signal(SIGPIPE, oldintp);
681 if(closesocket(dout
)) {
682 int iret
=WSAGetLastError ();
683 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
684 (void) fflush(stdout
);
689 if (closefunc
!= NULL
&& fin
!= NULL
)
692 ptransfer("sent", bytes
, &start
, &stop
);
704 (void) fflush(stdout
);
705 longjmp(recvabort
, 1);
709 void recvrequest(const char *cmd
, const char *local
, const char *remote
, const char *mode
,
715 void (*oldintr
)(int), (*oldintp
)(int);
716 int oldverbose
= 0, oldtype
= 0, is_retr
, tcrflag
, nfnd
, bare_lfs
= 0;
718 // static char *buf; // Szurgot: Shouldn't this go SOMEWHERE?
720 static int bufsize
= 1024;
721 long bytes
= 0, hashbytes
= HASHBYTES
;
725 struct timeval start
, stop
;
728 is_retr
= strcmp(cmd
, "RETR") == 0;
729 if (is_retr
&& verbose
&& printnames
) {
730 if (local
&& *local
!= '-')
731 printf("local: %s ", local
);
733 printf("remote: %s\n", remote
);
734 (void) fflush(stdout
);
736 if (proxy
&& is_retr
) {
737 proxtrans(cmd
, local
, remote
);
743 tcrflag
= !crflag
&& is_retr
;
744 if (setjmp(recvabort
)) {
753 null();// (void) signal(SIGINT, oldintr);
757 null();// oldintr = signal(SIGINT, abortrecv);
758 if (strcmp(local
, "-") && *local
!= '|') {
761 // This whole thing is a problem... access Won't work on non-existent files
762 if (access(local
, 2) < 0) {
763 char *dir
= rindex(local
, '/');
765 if (errno
!= ENOENT
&& errno
!= EACCES
) {
767 (void) signal(SIGINT
, oldintr
);
773 d
= access(dir
? local
: ".", 2);
778 (void) signal(SIGINT
, oldintr
);
782 if (!runique
&& errno
== EACCES
&&
783 chmod(local
, 0600) < 0) {
785 (void) signal(SIGINT
, oldintr
);
789 if (runique
&& errno
== EACCES
&&
790 (local
= gunique(local
)) == NULL
) {
791 (void) signal(SIGINT
, oldintr
);
796 else if (runique
&& (local
= gunique(local
)) == NULL
) {
797 (void) signal(SIGINT
, oldintr
);
804 null();// (void) signal(SIGINT, oldintr);
808 if (setjmp(recvabort
))
811 if (type
!= TYPE_A
&& (allbinary
== 0 || type
!= TYPE_I
)) {
813 oldverbose
= verbose
;
817 verbose
= oldverbose
;
819 } else if (restart_point
) {
820 if (command("REST %ld", (long) restart_point
) != CONTINUE
)
824 if (command("%s %s", cmd
, remote
) != PRELIM
) {
825 null();// (void) signal(SIGINT, oldintr);
840 verbose
= oldverbose
;
845 if (command("%s", cmd
) != PRELIM
) {
846 null();// (void) signal(SIGINT, oldintr);
861 verbose
= oldverbose
;
869 if (strcmp(local
, "-") == 0)
871 else if (*local
== '|') {
872 null();// oldintp = signal(SIGPIPE, SIG_IGN);
873 fout
= _popen(local
+ 1, "w");
880 fout
= fopen(local
, mode
);
887 (void) gettimeofday(&start
, (struct timezone
*)0);
893 lseek(fileno(fout
), (long) restart_point
, L_SET
) < 0) {
895 if (closefunc
!= NULL
)
900 // while ((c = recv(din, buf, bufsize, 1)) > 0) {
901 // if ((d = write(fileno(fout), buf, c)) != c)
902 // if ((d = write(fileno(fout), buf, c)) != c)
904 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0) {
905 write(fileno(fout
), buf
, c
);
908 while (bytes
>= hashbytes
) {
910 hashbytes
+= HASHBYTES
;
912 (void) fflush(stdout
);
915 if (hash
&& bytes
> 0) {
916 if (bytes
< HASHBYTES
)
918 (void) putchar('\n');
919 (void) fflush(stdout
);
922 // if (errno != EPIPE)
930 // fprintf(stderr, "%s: short write\n", local);
936 register int i
, n
, c
;
938 if (fseek(fout
, 0L, L_SET
) < 0)
943 if ((c
=getc(fout
)) == EOF
)
948 if (fseek(fout
, 0L, L_INCR
) < 0) {
951 if (closefunc
!= NULL
)
956 while ((c
= fgetcSocket(din
)) != EOF
) {
960 while (hash
&& (bytes
>= hashbytes
)) {
962 (void) fflush(stdout
);
963 hashbytes
+= HASHBYTES
;
966 if ((c
= fgetcSocket(din
)) != '\n' || tcrflag
) {
969 (void) putc('\r', fout
);
978 (void) putc(c
, fout
);
984 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs
);
985 printf("File may not have transferred correctly.\n");
986 (void) fflush(stdout
);
989 if (bytes
< hashbytes
)
991 (void) putchar('\n');
992 (void) fflush(stdout
);
994 // if (ferror(din)) {
995 // if (errno != EPIPE)
1003 if (closefunc
!= NULL
)
1005 null();// (void) signal(SIGINT, oldintr);
1007 null();// (void) signal(SIGPIPE, oldintp);
1008 (void) gettimeofday(&stop
, (struct timezone
*)0);
1009 if(closesocket(din
)) {
1010 int iret
=WSAGetLastError ();
1011 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1012 (void) fflush(stdout
);
1016 if (bytes
> 0 && is_retr
)
1017 ptransfer("received", bytes
, &start
, &stop
);
1032 verbose
= oldverbose
;
1037 /* abort using RFC959 recommended IP,SYNC sequence */
1039 (void) gettimeofday(&stop
, (struct timezone
*)0);
1041 null();// (void) signal(SIGPIPE, oldintr);
1042 null();// (void) signal(SIGINT,SIG_IGN);
1057 verbose
= oldverbose
;
1061 null();// (void) signal(SIGINT,oldintr);
1065 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1067 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
1068 /* after urgent byte rather than before as now is protocol */
1069 if (send(cout
,&msg
,1,MSG_OOB
) != 1) {
1072 fprintfSocket(cout
,"%cABOR\r\n",DM
);
1074 FD_SET(cin
, &mask
); // Need to correct this
1076 FD_SET(din
, &mask
); // Need to correct this
1078 if ((nfnd
= empty(&mask
,10)) <= 0) {
1085 if (din
&& FD_ISSET(din
, &mask
)) {
1086 while (recv(din
, buf
, bufsize
, 0) > 0)
1089 if (getreply(0) == ERROR
&& code
== 552) { /* needed for nic style abort */
1102 if (closefunc
!= NULL
&& fout
!= NULL
)
1105 if(closesocket(din
)) {
1106 int iret
=WSAGetLastError ();
1107 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1108 (void) fflush(stdout
);
1112 ptransfer("received", bytes
, &start
, &stop
);
1113 null();// (void) signal(SIGINT,oldintr);
1119 register char *p
, *a
;
1120 int result
, len
, tmpno
= 0;
1122 int a0
, a1
, a2
, a3
, p0
, p1
;
1126 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1128 perror("ftp: socket");
1131 if ((options
& SO_DEBUG
) &&
1132 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
,
1134 perror("ftp: setsockopt (ignored)");
1135 if (command("PASV") != COMPLETE
) {
1136 printf("Passive mode refused.\n");
1141 * What we've got at this point is a string of comma
1142 * separated one-byte unsigned integer values.
1143 * The first four are the an IP address. The fifth is
1144 * the MSB of the port number, the sixth is the LSB.
1145 * From that we'll prepare a sockaddr_in.
1148 if (sscanf(pasv
,"%d,%d,%d,%d,%d,%d",
1149 &a0
, &a1
, &a2
, &a3
, &p0
, &p1
) != 6) {
1150 printf("Passive mode address scan failure. Shouldn't happen!\n");
1154 bzero(&data_addr
, sizeof(data_addr
));
1155 data_addr
.sin_family
= AF_INET
;
1156 a
= (char *)&data_addr
.sin_addr
.s_addr
;
1161 p
= (char *)&data_addr
.sin_port
;
1165 if (connect(data
, (struct sockaddr
*)&data_addr
,
1166 sizeof(data_addr
)) < 0) {
1167 perror("ftp: connect");
1175 data_addr
= myctladdr
;
1177 data_addr
.sin_port
= 0; /* let system pick one */
1179 (void) close (data
);
1180 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1182 perror("ftp: socket");
1188 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof (on
)) < 0) {
1189 perror("ftp: setsockopt (reuse address)");
1192 if (bind(data
, (struct sockaddr
*)&data_addr
, sizeof (data_addr
)) < 0) {
1193 perror("ftp: bind");
1196 if (options
& SO_DEBUG
&&
1197 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof (on
)) < 0)
1198 perror("ftp: setsockopt (ignored)");
1199 len
= sizeof (data_addr
);
1200 if (getsockname(data
, (struct sockaddr
*)&data_addr
, &len
) < 0) {
1201 perror("ftp: getsockname");
1204 if (listen(data
, 1) < 0)
1205 perror("ftp: listen");
1207 a
= (char *)&data_addr
.sin_addr
;
1208 p
= (char *)&data_addr
.sin_port
;
1209 #define UC(b) (((int)b)&0xff)
1211 command("PORT %d,%d,%d,%d,%d,%d",
1212 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1213 UC(p
[0]), UC(p
[1]));
1214 if (result
== ERROR
&& sendport
== -1) {
1219 return (result
!= COMPLETE
);
1225 (void) fflush(stdout
);
1226 (void) close(data
), data
= -1;
1232 int dataconn(const char *mode
)
1234 struct sockaddr_in from
;
1235 int s
, fromlen
= sizeof (from
);
1240 s
= accept(data
, (struct sockaddr
*) &from
, &fromlen
);
1242 perror("ftp: accept");
1243 (void) closesocket(data
), data
= -1;
1246 if(closesocket(data
)) {
1247 int iret
=WSAGetLastError ();
1248 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1249 (void) fflush(stdout
);
1256 void ptransfer(direction
, bytes
, t0
, t1
)
1257 const char *direction
;
1259 struct timeval
*t0
, *t1
;
1266 s
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.);
1267 #define nz(x) ((x) == 0 ? 1 : (x))
1269 printf("%ld bytes %s in %.1f seconds (%.0f Kbytes/s)\n",
1270 bytes
, direction
, s
, bs
/ 1024.);
1271 (void) fflush(stdout
);
1276 struct timeval *tsum, *t0;
1279 tsum->tv_sec += t0->tv_sec;
1280 tsum->tv_usec += t0->tv_usec;
1281 if (tsum->tv_usec > 1000000)
1282 tsum->tv_sec++, tsum->tv_usec -= 1000000;
1285 void tvsub(tdiff
, t1
, t0
)
1286 struct timeval
*tdiff
, *t1
, *t0
;
1289 tdiff
->tv_sec
= t1
->tv_sec
- t0
->tv_sec
;
1290 tdiff
->tv_usec
= t1
->tv_usec
- t0
->tv_usec
;
1291 if (tdiff
->tv_usec
< 0)
1292 tdiff
->tv_sec
--, tdiff
->tv_usec
+= 1000000;
1295 void psabort(int flag
)
1297 extern int abrtflag
;
1302 void pswitch(int flag
)
1304 extern int proxy
, abrtflag
;
1306 static struct comvars
{
1308 char name
[MAXHOSTNAMELEN
];
1309 struct sockaddr_in mctl
;
1310 struct sockaddr_in hctl
;
1322 char mi
[MAXPATHLEN
];
1323 char mo
[MAXPATHLEN
];
1324 } proxstruct
, tmpstruct
;
1325 struct comvars
*ip
, *op
;
1328 oldintr
= signal(SIGINT
, psabort
);
1343 ip
->connect
= connected
;
1344 connected
= op
->connect
;
1346 (void) strncpy(ip
->name
, hostname
, sizeof(ip
->name
) - 1);
1347 ip
->name
[strlen(ip
->name
)] = '\0';
1350 hostname
= op
->name
;
1351 ip
->hctl
= hisctladdr
;
1352 hisctladdr
= op
->hctl
;
1353 ip
->mctl
= myctladdr
;
1354 myctladdr
= op
->mctl
;
1365 ip
->sunqe
= sunique
;
1366 sunique
= op
->sunqe
;
1367 ip
->runqe
= runique
;
1368 runique
= op
->runqe
;
1373 (void) strncpy(ip
->nti
, ntin
, 16);
1374 (ip
->nti
)[strlen(ip
->nti
)] = '\0';
1375 (void) strcpy(ntin
, op
->nti
);
1376 (void) strncpy(ip
->nto
, ntout
, 16);
1377 (ip
->nto
)[strlen(ip
->nto
)] = '\0';
1378 (void) strcpy(ntout
, op
->nto
);
1379 ip
->mapflg
= mapflag
;
1380 mapflag
= op
->mapflg
;
1381 (void) strncpy(ip
->mi
, mapin
, MAXPATHLEN
- 1);
1382 (ip
->mi
)[strlen(ip
->mi
)] = '\0';
1383 (void) strcpy(mapin
, op
->mi
);
1384 (void) strncpy(ip
->mo
, mapout
, MAXPATHLEN
- 1);
1385 (ip
->mo
)[strlen(ip
->mo
)] = '\0';
1386 (void) strcpy(mapout
, op
->mo
);
1387 // (void) signal(SIGINT, oldintr);
1402 (void) fflush(stdout
);
1406 longjmp(ptabort
, 1);
1410 void proxtrans(cmd
, local
, remote
)
1411 const char *cmd
, *local
, *remote
;
1413 // void (*oldintr)(int);
1414 int tmptype
, oldtype
= 0, secndflag
= 0, nfnd
;
1415 extern jmp_buf ptabort
;
1420 if (strcmp(cmd
, "RETR"))
1423 cmd2
= runique
? "STOU" : "STOR";
1424 if (command("PASV") != COMPLETE
) {
1425 printf("proxy server does not support third part transfers.\n");
1426 (void) fflush(stdout
);
1432 printf("No primary connection\n");
1433 (void) fflush(stdout
);
1438 if (type
!= tmptype
) {
1455 if (command("PORT %s", pasv
) != COMPLETE
) {
1475 if (setjmp(ptabort
))
1477 null();// oldintr = signal(SIGINT, abortpt);
1478 if (command("%s %s", cmd
, remote
) != PRELIM
) {
1479 null();// (void) signal(SIGINT, oldintr);
1502 if (command("%s %s", cmd2
, local
) != PRELIM
)
1508 null();// (void) signal(SIGINT, oldintr);
1527 printf("local: %s remote: %s\n", local
, remote
);
1528 (void) fflush(stdout
);
1531 null();// (void) signal(SIGINT, SIG_IGN);
1533 if (strcmp(cmd
, "RETR") && !proxy
)
1535 else if (!strcmp(cmd
, "RETR") && proxy
)
1537 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1538 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1559 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1561 *(msg
+1) = (char) DM
;
1562 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1564 fprintfSocket(cout
,"ABOR\r\n");
1566 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1567 if ((nfnd
= empty(&mask
,10)) <= 0) {
1582 null();// (void) signal(SIGINT, oldintr);
1588 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1590 *(msg
+1) = (char)DM
;
1591 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1593 fprintfSocket(cout
,"ABOR\r\n");
1595 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1596 if ((nfnd
= empty(&mask
,10)) <= 0) {
1608 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1609 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1630 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1632 *(msg
+1) = (char)DM
;
1633 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1635 fprintfSocket(cout
,"ABOR\r\n");
1637 // FD_SET(fileno(cin), &mask); // Chris:
1638 if ((nfnd
= empty(&mask
,10)) <= 0) {
1652 null();// (void) signal(SIGINT, oldintr);
1659 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1661 *(msg
+1) = (char)DM
;
1662 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1664 fprintfSocket(cout
,"ABOR\r\n");
1666 // FD_SET(fileno(cin), &mask); // Chris:
1667 if ((nfnd
= empty(&mask
,10)) <= 0) {
1681 // FD_SET(fileno(cin), &mask); // Chris:
1682 if ((nfnd
= empty(&mask
,10)) <= 0) {
1714 null();// (void) signal(SIGINT, oldintr);
1717 void reset(int argc
, const char *argv
[])
1725 // FD_SET(fileno(cin), &mask); // Chris
1726 if ((nfnd
= empty(&mask
,0)) < 0) {
1742 static char new[MAXPATHLEN
];
1743 char *cp
= rindex(local
, '/');
1749 d
= access(cp
? local
: ".", 2);
1756 (void) strcpy(new, local
);
1757 cp
= new + strlen(new);
1760 if (++count
== 100) {
1761 printf("runique: can't find unique file name.\n");
1762 (void) fflush(stdout
);
1771 if ((d
= access(new, 0)) < 0)
1775 else if (*(cp
- 2) == '.')
1778 *(cp
- 2) = *(cp
- 2) + 1;