2 * Copyright (c) 1985, 1989 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 #define L_SET SEEK_SET
23 #define L_INCR SEEK_CUR
24 #define caddr_t void *
27 static char sccsid
[] = "@(#)ftp.c 5.28 (Berkeley) 4/20/89";
30 #ifndef MAXHOSTNAMELEN
31 #define MAXHOSTNAMELEN 64
35 #define vfprintf(a,b,c) _doprnt(b,c,a)
39 /* FD_SET wasn't defined until 4.0. its a cheap test for uid_t presence */
41 #define NBBY 8 /* number of bits in a byte */
43 * Select uses bit masks of file descriptors in longs.
44 * These macros manipulate such bit fields (the filesystem macros use chars).
45 * FD_SETSIZE may be defined by the user, but the default here
46 * should be >= NOFILE (param.h).
49 #define FD_SETSIZE 256
53 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
55 #define howmany(x, y) (((x)+((y)-1))/(y))
58 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
59 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
60 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
61 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
68 struct sockaddr_in hisctladdr
;
69 struct sockaddr_in data_addr
;
74 struct sockaddr_in myctladdr
;
77 off_t restart_point
= 0;
80 int dataconn(const char *mode
);
82 int command(const char *fmt
, ...);
86 typedef void (*Sig_t
)(int);
90 void psabort(int sig
);
92 char *hookup(const char *host
, int port
)
94 register struct hostent
*hp
= 0;
97 static char hostnamebuf
[80];
99 bzero((char *)&hisctladdr
, sizeof (hisctladdr
));
100 hisctladdr
.sin_addr
.s_addr
= inet_addr(host
);
101 if (hisctladdr
.sin_addr
.s_addr
!= (unsigned long)-1) {
102 hisctladdr
.sin_family
= AF_INET
;
103 (void) strncpy(hostnamebuf
, host
, sizeof(hostnamebuf
));
105 hp
= gethostbyname(host
);
107 fprintf(stderr
, "ftp: %s: ", host
);
108 herror((char *)NULL
);
112 hisctladdr
.sin_family
= hp
->h_addrtype
;
113 bcopy(hp
->h_addr_list
[0],
114 (caddr_t
)&hisctladdr
.sin_addr
, hp
->h_length
);
115 (void) strncpy(hostnamebuf
, hp
->h_name
, sizeof(hostnamebuf
));
117 hostname
= hostnamebuf
;
118 s
= socket(hisctladdr
.sin_family
, SOCK_STREAM
, 0);
119 if (s
== INVALID_SOCKET
) {
120 perror("ftp: socket");
124 hisctladdr
.sin_port
= port
;
125 while (connect(s
, (struct sockaddr
*)&hisctladdr
, sizeof (hisctladdr
)) < 0) {
126 if (hp
&& hp
->h_addr_list
[1]) {
129 fprintf(stderr
, "ftp: connect to address %s: ",
130 inet_ntoa(hisctladdr
.sin_addr
));
134 bcopy(hp
->h_addr_list
[0],
135 (caddr_t
)&hisctladdr
.sin_addr
, hp
->h_length
);
136 fprintf(stdout
, "Trying %s...\n",
137 inet_ntoa(hisctladdr
.sin_addr
));
138 (void) fflush(stdout
);
140 s
= socket(hisctladdr
.sin_family
, SOCK_STREAM
, 0);
141 if (s
== INVALID_SOCKET
) {
142 perror("ftp: socket");
148 perror("ftp: connect");
152 len
= sizeof (myctladdr
);
153 if (getsockname(s
, (struct sockaddr
*)&myctladdr
, &len
) < 0) {
154 perror("ftp: getsockname");
160 printf("Connected to %s.\n", hostname
);
161 (void) fflush(stdout
);
163 if (getreply(0) > 2) { /* read startup message from server */
172 if (setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, (const char *) &on
, sizeof(on
))
174 perror("ftp: setsockopt");
177 #endif //SO_OOBINLINE
185 int login(const char *host
)
188 char *puser
, *ppass
, *pacct
;
189 const char *user
, *pass
, *acct
;
192 user
= pass
= acct
= 0;
193 n
= ruserpass(host
, &puser
, &ppass
, &pacct
);
203 while (user
== NULL
) {
204 const char *myname
= "none"; // This needs to become the usename env
207 printf("Name (%s:%s): ", host
, myname
);
209 printf("Name (%s): ", host
);
210 (void) fflush(stdout
);
211 (void) fgets(tmp
, sizeof(tmp
) - 1, stdin
);
212 tmp
[strlen(tmp
) - 1] = '\0';
218 n
= command("USER %s", user
);
221 pass
= getpass("Password:");
222 n
= command("PASS %s", pass
);
227 acct
= getpass("Account:");
228 n
= command("ACCT %s", acct
);
231 fprintf(stderr
, "Login failed.\n");
234 if (!aflag
&& acct
!= NULL
)
235 (void) command("ACCT %s", acct
);
238 for (n
= 0; n
< macnum
; ++n
) {
239 if (!strcmp("init", macros
[n
].mac_name
)) {
240 (void) strcpy(line
, "$init");
242 domacro(margc
, margv
);
252 extern jmp_buf ptabort
;
255 (void) fflush(stdout
);
262 int command(const char *fmt
, ...)
266 void (*oldintr
)(int);
272 vfprintf(stdout
, fmt
, ap
);
275 (void) fflush(stdout
);
278 perror ("No control connection for command");
282 oldintr
= signal(SIGINT
,cmdabort
);
287 vsprintf(buffer
, fmt
, ap
);
289 //DLJ: to work through firewalls - send the command as a single message
290 strcat(buffer
,"\r\n");
291 fprintfSocket(cout
, buffer
);
293 //DLJ: the following two lines are replaced by the strcat above - seems to
294 // make it work through firewalls.
295 // fprintfSocket(cout, "\r\n");
296 // (void) fflush(cout);
298 r
= getreply(!strcmp(fmt
, "QUIT"));
299 if (abrtflag
&& oldintr
!= SIG_IGN
)
301 // (void) signal(SIGINT, oldintr);
305 char reply_string
[BUFSIZ
]; /* last line of previous reply */
316 int originalcode
= 0, continuation
= 0;
317 void (*oldintr
)(int);
321 oldintr
= signal(SIGINT
,cmdabort
);
325 while ((c
= fgetcSocket(cin
)) != '\n') {
326 if (c
== IAC
) { /* handle telnet commands */
327 switch (fgetcSocket(cin
)) {
330 c
= fgetcSocket(cin
);
331 fprintfSocket(cout
, "%c%c%c",IAC
,DONT
,c
);
335 c
= fgetcSocket(cin
);
336 fprintfSocket(cout
, "%c%c%c",IAC
,WONT
,c
);
346 // (void) signal(SIGINT,oldintr);
352 printf("421 Service not available, remote server has closed connection\n");
353 (void) fflush(stdout
);
358 if (c
!= '\r' && (verbose
> 0 ||
359 (verbose
> -1 && n
== '5' && dig
> 4))) {
361 ((dig
== 1 || dig
== 5) && verbose
== 0))
362 printf("%s:",hostname
);
364 (void) fflush(stdout
);
366 if (dig
< 4 && isdigit(c
))
367 code
= code
* 10 + (c
- '0');
368 if (!pflag
&& code
== 227)
370 if (dig
> 4 && pflag
== 1 && isdigit(c
))
373 if (c
!= '\r' && c
!= ')')
380 if (dig
== 4 && c
== '-') {
387 if (cp
< &reply_string
[sizeof(reply_string
) - 1])
390 if (verbose
> 0 || (verbose
> -1 && n
== '5')) {
392 (void) fflush (stdout
);
394 if (continuation
&& code
!= originalcode
) {
395 if (originalcode
== 0)
402 (void) signal(SIGINT
,oldintr
);
403 if (code
== 421 || originalcode
== 421)
405 if (abrtflag
&& oldintr
!= cmdabort
&& oldintr
!= SIG_IGN
)
418 t
.tv_sec
= (long) sec
;
420 return(select(32, mask
, (struct fd_set
*) 0, (struct fd_set
*) 0, &t
));
431 printf("\nsend aborted\n");
432 (void) fflush(stdout
);
433 longjmp(sendabort
, 1);
437 #define HASHBYTES 1024
439 void sendrequest(const char *cmd
, const char *local
, const char *remote
, int printnames
)
444 sig_t (*oldintr
)(), (*oldintp
)();
445 char buf
[BUFSIZ
], *bufp
;
446 long bytes
= 0, hashbytes
= HASHBYTES
;
449 struct timeval start
, stop
;
452 if (verbose
&& printnames
) {
453 if (local
&& *local
!= '-')
454 printf("local: %s ", local
);
456 printf("remote: %s\n", remote
);
457 (void) fflush(stdout
);
460 proxtrans(cmd
, local
, remote
);
467 if (setjmp(sendabort
)) {
476 null();// (void) signal(SIGINT,oldintr);
478 null();// (void) signal(SIGPIPE,oldintp);
482 null();// oldintr = signal(SIGINT, abortsend);
483 if (strcmp(local
, "-") == 0)
485 else if (*local
== '|') {
486 null();// oldintp = signal(SIGPIPE,SIG_IGN);
487 fin
= _popen(local
+ 1, "r");
490 null();// (void) signal(SIGINT, oldintr);
491 null();// (void) signal(SIGPIPE, oldintp);
497 fin
= fopen(local
, "r");
500 null();// (void) signal(SIGINT, oldintr);
505 if (fstat(fileno(fin
), &st
) < 0 ||
506 (st
.st_mode
&S_IFMT
) != S_IFREG
) {
507 fprintf(stdout
, "%s: not a plain file.\n", local
);
508 (void) fflush(stdout
);
509 null();// (void) signal(SIGINT, oldintr);
516 null();// (void) signal(SIGINT, oldintr);
518 null();// (void) signal(SIGPIPE, oldintp);
520 if (closefunc
!= NULL
)
524 if (setjmp(sendabort
))
528 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
529 if (fseek(fin
, (long) restart_point
, 0) < 0) {
532 if (closefunc
!= NULL
)
536 if (command("REST %ld", (long) restart_point
)
539 if (closefunc
!= NULL
)
547 if (command("%s %s", cmd
, remote
) != PRELIM
) {
548 null();// (void) signal(SIGINT, oldintr);
550 null();// (void) signal(SIGPIPE, oldintp);
551 if (closefunc
!= NULL
)
556 if (command("%s", cmd
) != PRELIM
) {
557 null();// (void) signal(SIGINT, oldintr);
559 null();// (void) signal(SIGPIPE, oldintp);
560 if (closefunc
!= NULL
)
564 dout
= dataconn(mode
);
567 (void) gettimeofday(&start
, (struct timezone
*)0);
568 null();// oldintp = signal(SIGPIPE, SIG_IGN);
574 while ((c
= read(fileno(fin
), buf
, sizeof (buf
))) > 0) {
576 for (bufp
= buf
; c
> 0; c
-= d
, bufp
+= d
)
577 if ((d
= send(dout
, bufp
, c
, 0)) <= 0)
580 while (bytes
>= hashbytes
) {
582 hashbytes
+= HASHBYTES
;
584 (void) fflush(stdout
);
587 if (hash
&& bytes
> 0) {
588 if (bytes
< HASHBYTES
)
590 (void) putchar('\n');
591 (void) fflush(stdout
);
597 fprintf(stderr
, "netout: write returned 0?\n");
598 else if (errno
!= EPIPE
)
607 static int bufsize
= 1024;
610 while ((c
= getc(fin
)) != EOF
) {
612 while (hash
&& (bytes
>= hashbytes
)) {
614 (void) fflush(stdout
);
615 hashbytes
+= HASHBYTES
;
617 // Szurgot: The following code is unncessary on Win32.
618 // (void) fputcSocket(dout, '\r');
622 if (ipos
>= bufsize
) {
623 fputSocket(dout
,buf
,ipos
);
624 if(!hash
) (void) putchar('.');
631 fputSocket(dout
,buf
,ipos
);
635 if (bytes
< hashbytes
)
637 (void) putchar('\n');
638 (void) fflush(stdout
);
642 (void) putchar('\n');
643 (void) fflush(stdout
);
647 // if (ferror(dout)) {
648 // if (errno != EPIPE)
655 (void) gettimeofday(&stop
, (struct timezone
*)0);
656 if (closefunc
!= NULL
)
658 if(closesocket(dout
)) {
659 int iret
=WSAGetLastError ();
660 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
661 (void) fflush(stdout
);
664 null();// (void) signal(SIGINT, oldintr);
666 null();// (void) signal(SIGPIPE, oldintp);
668 ptransfer("sent", bytes
, &start
, &stop
);
671 (void) gettimeofday(&stop
, (struct timezone
*)0);
672 null();// (void) signal(SIGINT, oldintr);
674 null();// (void) signal(SIGPIPE, oldintp);
684 if(closesocket(dout
)) {
685 int iret
=WSAGetLastError ();
686 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
687 (void) fflush(stdout
);
692 if (closefunc
!= NULL
&& fin
!= NULL
)
695 ptransfer("sent", bytes
, &start
, &stop
);
707 (void) fflush(stdout
);
708 longjmp(recvabort
, 1);
712 void recvrequest(const char *cmd
, const char *local
, const char *remote
, const char *mode
,
718 void (*oldintr
)(int), (*oldintp
)(int);
719 int oldverbose
= 0, oldtype
= 0, is_retr
, tcrflag
, nfnd
, bare_lfs
= 0;
721 // static char *buf; // Szurgot: Shouldn't this go SOMEWHERE?
723 static int bufsize
= 1024;
724 long bytes
= 0, hashbytes
= HASHBYTES
;
728 struct timeval start
, stop
;
731 is_retr
= strcmp(cmd
, "RETR") == 0;
732 if (is_retr
&& verbose
&& printnames
) {
733 if (local
&& *local
!= '-')
734 printf("local: %s ", local
);
736 printf("remote: %s\n", remote
);
737 (void) fflush(stdout
);
739 if (proxy
&& is_retr
) {
740 proxtrans(cmd
, local
, remote
);
746 tcrflag
= !crflag
&& is_retr
;
747 if (setjmp(recvabort
)) {
756 null();// (void) signal(SIGINT, oldintr);
760 null();// oldintr = signal(SIGINT, abortrecv);
761 if (strcmp(local
, "-") && *local
!= '|') {
764 // This whole thing is a problem... access Won't work on non-existent files
765 if (access(local
, 2) < 0) {
766 char *dir
= rindex(local
, '/');
768 if (errno
!= ENOENT
&& errno
!= EACCES
) {
770 (void) signal(SIGINT
, oldintr
);
776 d
= access(dir
? local
: ".", 2);
781 (void) signal(SIGINT
, oldintr
);
785 if (!runique
&& errno
== EACCES
&&
786 chmod(local
, 0600) < 0) {
788 (void) signal(SIGINT
, oldintr
);
792 if (runique
&& errno
== EACCES
&&
793 (local
= gunique(local
)) == NULL
) {
794 (void) signal(SIGINT
, oldintr
);
799 else if (runique
&& (local
= gunique(local
)) == NULL
) {
800 (void) signal(SIGINT
, oldintr
);
807 null();// (void) signal(SIGINT, oldintr);
811 if (setjmp(recvabort
))
814 if (type
!= TYPE_A
&& (allbinary
== 0 || type
!= TYPE_I
)) {
816 oldverbose
= verbose
;
820 verbose
= oldverbose
;
822 } else if (restart_point
) {
823 if (command("REST %ld", (long) restart_point
) != CONTINUE
)
827 if (command("%s %s", cmd
, remote
) != PRELIM
) {
828 null();// (void) signal(SIGINT, oldintr);
843 verbose
= oldverbose
;
848 if (command("%s", cmd
) != PRELIM
) {
849 null();// (void) signal(SIGINT, oldintr);
864 verbose
= oldverbose
;
872 if (strcmp(local
, "-") == 0)
874 else if (*local
== '|') {
875 null();// oldintp = signal(SIGPIPE, SIG_IGN);
876 fout
= _popen(local
+ 1, "w");
883 fout
= fopen(local
, mode
);
890 (void) gettimeofday(&start
, (struct timezone
*)0);
896 lseek(fileno(fout
), (long) restart_point
, L_SET
) < 0) {
898 if (closefunc
!= NULL
)
903 // while ((c = recv(din, buf, bufsize, 1)) > 0) {
904 // if ((d = write(fileno(fout), buf, c)) != c)
905 // if ((d = write(fileno(fout), buf, c)) != c)
907 while ((c
= recv(din
, buf
, bufsize
, 0)) > 0) {
908 write(fileno(fout
), buf
, c
);
911 while (bytes
>= hashbytes
) {
913 hashbytes
+= HASHBYTES
;
915 (void) fflush(stdout
);
918 if (hash
&& bytes
> 0) {
919 if (bytes
< HASHBYTES
)
921 (void) putchar('\n');
922 (void) fflush(stdout
);
925 // if (errno != EPIPE)
933 // fprintf(stderr, "%s: short write\n", local);
939 register int i
, n
, c
;
941 if (fseek(fout
, 0L, L_SET
) < 0)
946 if ((c
=getc(fout
)) == EOF
)
951 if (fseek(fout
, 0L, L_INCR
) < 0) {
954 if (closefunc
!= NULL
)
959 while ((c
= fgetcSocket(din
)) != EOF
) {
963 while (hash
&& (bytes
>= hashbytes
)) {
965 (void) fflush(stdout
);
966 hashbytes
+= HASHBYTES
;
969 if ((c
= fgetcSocket(din
)) != '\n' || tcrflag
) {
972 (void) putc('\r', fout
);
981 (void) putc(c
, fout
);
987 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs
);
988 printf("File may not have transferred correctly.\n");
989 (void) fflush(stdout
);
992 if (bytes
< hashbytes
)
994 (void) putchar('\n');
995 (void) fflush(stdout
);
997 // if (ferror(din)) {
998 // if (errno != EPIPE)
1006 if (closefunc
!= NULL
)
1008 null();// (void) signal(SIGINT, oldintr);
1010 null();// (void) signal(SIGPIPE, oldintp);
1011 (void) gettimeofday(&stop
, (struct timezone
*)0);
1012 if(closesocket(din
)) {
1013 int iret
=WSAGetLastError ();
1014 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1015 (void) fflush(stdout
);
1019 if (bytes
> 0 && is_retr
)
1020 ptransfer("received", bytes
, &start
, &stop
);
1035 verbose
= oldverbose
;
1040 /* abort using RFC959 recommended IP,SYNC sequence */
1042 (void) gettimeofday(&stop
, (struct timezone
*)0);
1044 null();// (void) signal(SIGPIPE, oldintr);
1045 null();// (void) signal(SIGINT,SIG_IGN);
1060 verbose
= oldverbose
;
1064 null();// (void) signal(SIGINT,oldintr);
1068 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1070 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
1071 /* after urgent byte rather than before as now is protocol */
1072 if (send(cout
,&msg
,1,MSG_OOB
) != 1) {
1075 fprintfSocket(cout
,"%cABOR\r\n",DM
);
1077 FD_SET(cin
, &mask
); // Need to correct this
1079 FD_SET(din
, &mask
); // Need to correct this
1081 if ((nfnd
= empty(&mask
,10)) <= 0) {
1088 if (din
&& FD_ISSET(din
, &mask
)) {
1089 while (recv(din
, buf
, bufsize
, 0) > 0)
1092 if (getreply(0) == ERROR
&& code
== 552) { /* needed for nic style abort */
1105 if (closefunc
!= NULL
&& fout
!= NULL
)
1108 if(closesocket(din
)) {
1109 int iret
=WSAGetLastError ();
1110 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1111 (void) fflush(stdout
);
1115 ptransfer("received", bytes
, &start
, &stop
);
1116 null();// (void) signal(SIGINT,oldintr);
1122 register char *p
, *a
;
1123 int result
, len
, tmpno
= 0;
1125 int a0
, a1
, a2
, a3
, p0
, p1
;
1129 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1131 perror("ftp: socket");
1134 if ((options
& SO_DEBUG
) &&
1135 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
,
1137 perror("ftp: setsockopt (ignored)");
1138 if (command("PASV") != COMPLETE
) {
1139 printf("Passive mode refused.\n");
1144 * What we've got at this point is a string of comma
1145 * separated one-byte unsigned integer values.
1146 * The first four are the an IP address. The fifth is
1147 * the MSB of the port number, the sixth is the LSB.
1148 * From that we'll prepare a sockaddr_in.
1151 if (sscanf(pasv
,"%d,%d,%d,%d,%d,%d",
1152 &a0
, &a1
, &a2
, &a3
, &p0
, &p1
) != 6) {
1153 printf("Passive mode address scan failure. Shouldn't happen!\n");
1157 bzero(&data_addr
, sizeof(data_addr
));
1158 data_addr
.sin_family
= AF_INET
;
1159 a
= (char *)&data_addr
.sin_addr
.s_addr
;
1164 p
= (char *)&data_addr
.sin_port
;
1168 if (connect(data
, (struct sockaddr
*)&data_addr
,
1169 sizeof(data_addr
)) < 0) {
1170 perror("ftp: connect");
1178 data_addr
= myctladdr
;
1180 data_addr
.sin_port
= 0; /* let system pick one */
1182 (void) close (data
);
1183 data
= socket(AF_INET
, SOCK_STREAM
, 0);
1185 perror("ftp: socket");
1191 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof (on
)) < 0) {
1192 perror("ftp: setsockopt (reuse address)");
1195 if (bind(data
, (struct sockaddr
*)&data_addr
, sizeof (data_addr
)) < 0) {
1196 perror("ftp: bind");
1199 if (options
& SO_DEBUG
&&
1200 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof (on
)) < 0)
1201 perror("ftp: setsockopt (ignored)");
1202 len
= sizeof (data_addr
);
1203 if (getsockname(data
, (struct sockaddr
*)&data_addr
, &len
) < 0) {
1204 perror("ftp: getsockname");
1207 if (listen(data
, 1) < 0)
1208 perror("ftp: listen");
1210 a
= (char *)&data_addr
.sin_addr
;
1211 p
= (char *)&data_addr
.sin_port
;
1212 #define UC(b) (((int)b)&0xff)
1214 command("PORT %d,%d,%d,%d,%d,%d",
1215 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1216 UC(p
[0]), UC(p
[1]));
1217 if (result
== ERROR
&& sendport
== -1) {
1222 return (result
!= COMPLETE
);
1228 (void) fflush(stdout
);
1229 (void) close(data
), data
= -1;
1235 int dataconn(const char *mode
)
1237 struct sockaddr_in from
;
1238 int s
, fromlen
= sizeof (from
);
1243 s
= accept(data
, (struct sockaddr
*) &from
, &fromlen
);
1245 perror("ftp: accept");
1246 (void) closesocket(data
), data
= -1;
1249 if(closesocket(data
)) {
1250 int iret
=WSAGetLastError ();
1251 fprintf(stdout
,"Error closing socket(%d)\n",iret
);
1252 (void) fflush(stdout
);
1259 void ptransfer(direction
, bytes
, t0
, t1
)
1260 const char *direction
;
1262 struct timeval
*t0
, *t1
;
1269 s
= td
.tv_sec
+ (td
.tv_usec
/ 1000000.);
1270 #define nz(x) ((x) == 0 ? 1 : (x))
1272 printf("%ld bytes %s in %.1f seconds (%.0f Kbytes/s)\n",
1273 bytes
, direction
, s
, bs
/ 1024.);
1274 (void) fflush(stdout
);
1279 struct timeval *tsum, *t0;
1282 tsum->tv_sec += t0->tv_sec;
1283 tsum->tv_usec += t0->tv_usec;
1284 if (tsum->tv_usec > 1000000)
1285 tsum->tv_sec++, tsum->tv_usec -= 1000000;
1288 void tvsub(tdiff
, t1
, t0
)
1289 struct timeval
*tdiff
, *t1
, *t0
;
1292 tdiff
->tv_sec
= t1
->tv_sec
- t0
->tv_sec
;
1293 tdiff
->tv_usec
= t1
->tv_usec
- t0
->tv_usec
;
1294 if (tdiff
->tv_usec
< 0)
1295 tdiff
->tv_sec
--, tdiff
->tv_usec
+= 1000000;
1298 void psabort(int flag
)
1300 extern int abrtflag
;
1305 void pswitch(int flag
)
1307 extern int proxy
, abrtflag
;
1309 static struct comvars
{
1311 char name
[MAXHOSTNAMELEN
];
1312 struct sockaddr_in mctl
;
1313 struct sockaddr_in hctl
;
1325 char mi
[MAXPATHLEN
];
1326 char mo
[MAXPATHLEN
];
1327 } proxstruct
, tmpstruct
;
1328 struct comvars
*ip
, *op
;
1331 oldintr
= signal(SIGINT
, psabort
);
1346 ip
->connect
= connected
;
1347 connected
= op
->connect
;
1349 (void) strncpy(ip
->name
, hostname
, sizeof(ip
->name
) - 1);
1350 ip
->name
[strlen(ip
->name
)] = '\0';
1353 hostname
= op
->name
;
1354 ip
->hctl
= hisctladdr
;
1355 hisctladdr
= op
->hctl
;
1356 ip
->mctl
= myctladdr
;
1357 myctladdr
= op
->mctl
;
1368 ip
->sunqe
= sunique
;
1369 sunique
= op
->sunqe
;
1370 ip
->runqe
= runique
;
1371 runique
= op
->runqe
;
1376 (void) strncpy(ip
->nti
, ntin
, 16);
1377 (ip
->nti
)[strlen(ip
->nti
)] = '\0';
1378 (void) strcpy(ntin
, op
->nti
);
1379 (void) strncpy(ip
->nto
, ntout
, 16);
1380 (ip
->nto
)[strlen(ip
->nto
)] = '\0';
1381 (void) strcpy(ntout
, op
->nto
);
1382 ip
->mapflg
= mapflag
;
1383 mapflag
= op
->mapflg
;
1384 (void) strncpy(ip
->mi
, mapin
, MAXPATHLEN
- 1);
1385 (ip
->mi
)[strlen(ip
->mi
)] = '\0';
1386 (void) strcpy(mapin
, op
->mi
);
1387 (void) strncpy(ip
->mo
, mapout
, MAXPATHLEN
- 1);
1388 (ip
->mo
)[strlen(ip
->mo
)] = '\0';
1389 (void) strcpy(mapout
, op
->mo
);
1390 // (void) signal(SIGINT, oldintr);
1405 (void) fflush(stdout
);
1409 longjmp(ptabort
, 1);
1413 void proxtrans(cmd
, local
, remote
)
1414 const char *cmd
, *local
, *remote
;
1416 // void (*oldintr)(int);
1417 int tmptype
, oldtype
= 0, secndflag
= 0, nfnd
;
1418 extern jmp_buf ptabort
;
1423 if (strcmp(cmd
, "RETR"))
1426 cmd2
= runique
? "STOU" : "STOR";
1427 if (command("PASV") != COMPLETE
) {
1428 printf("proxy server does not support third part transfers.\n");
1429 (void) fflush(stdout
);
1435 printf("No primary connection\n");
1436 (void) fflush(stdout
);
1441 if (type
!= tmptype
) {
1458 if (command("PORT %s", pasv
) != COMPLETE
) {
1478 if (setjmp(ptabort
))
1480 null();// oldintr = signal(SIGINT, abortpt);
1481 if (command("%s %s", cmd
, remote
) != PRELIM
) {
1482 null();// (void) signal(SIGINT, oldintr);
1505 if (command("%s %s", cmd2
, local
) != PRELIM
)
1511 null();// (void) signal(SIGINT, oldintr);
1530 printf("local: %s remote: %s\n", local
, remote
);
1531 (void) fflush(stdout
);
1534 null();// (void) signal(SIGINT, SIG_IGN);
1536 if (strcmp(cmd
, "RETR") && !proxy
)
1538 else if (!strcmp(cmd
, "RETR") && proxy
)
1540 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1541 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1562 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1564 *(msg
+1) = (char) DM
;
1565 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1567 fprintfSocket(cout
,"ABOR\r\n");
1569 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1570 if ((nfnd
= empty(&mask
,10)) <= 0) {
1585 null();// (void) signal(SIGINT, oldintr);
1591 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1593 *(msg
+1) = (char)DM
;
1594 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1596 fprintfSocket(cout
,"ABOR\r\n");
1598 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1599 if ((nfnd
= empty(&mask
,10)) <= 0) {
1611 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1612 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1633 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1635 *(msg
+1) = (char)DM
;
1636 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1638 fprintfSocket(cout
,"ABOR\r\n");
1640 // FD_SET(fileno(cin), &mask); // Chris:
1641 if ((nfnd
= empty(&mask
,10)) <= 0) {
1655 null();// (void) signal(SIGINT, oldintr);
1662 fprintfSocket(cout
,"%c%c",IAC
,IP
);
1664 *(msg
+1) = (char)DM
;
1665 if (send(cout
,msg
,2,MSG_OOB
) != 2)
1667 fprintfSocket(cout
,"ABOR\r\n");
1669 // FD_SET(fileno(cin), &mask); // Chris:
1670 if ((nfnd
= empty(&mask
,10)) <= 0) {
1684 // FD_SET(fileno(cin), &mask); // Chris:
1685 if ((nfnd
= empty(&mask
,10)) <= 0) {
1717 null();// (void) signal(SIGINT, oldintr);
1720 void reset(int argc
, const char *argv
[])
1728 // FD_SET(fileno(cin), &mask); // Chris
1729 if ((nfnd
= empty(&mask
,0)) < 0) {
1745 static char new[MAXPATHLEN
];
1746 char *cp
= rindex(local
, '/');
1752 d
= access(cp
? local
: ".", 2);
1759 (void) strcpy(new, local
);
1760 cp
= new + strlen(new);
1763 if (++count
== 100) {
1764 printf("runique: can't find unique file name.\n");
1765 (void) fflush(stdout
);
1774 if ((d
= access(new, 0)) < 0)
1778 else if (*(cp
- 2) == '.')
1781 *(cp
- 2) = *(cp
- 2) + 1;