ftp.c revision 1.2 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1985, 1989 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.2 mycroft /*static char sccsid[] = "from: @(#)ftp.c 5.38 (Berkeley) 4/22/91";*/
36 1.2 mycroft static char rcsid[] = "$Id: ftp.c,v 1.2 1993/08/01 18:15:28 mycroft Exp $";
37 1.1 cgd #endif /* not lint */
38 1.1 cgd
39 1.1 cgd #include <sys/param.h>
40 1.1 cgd #include <sys/stat.h>
41 1.1 cgd #include <sys/ioctl.h>
42 1.1 cgd #include <sys/socket.h>
43 1.1 cgd #include <sys/time.h>
44 1.1 cgd #include <sys/file.h>
45 1.1 cgd
46 1.1 cgd #include <netinet/in.h>
47 1.1 cgd #include <netinet/in_systm.h>
48 1.1 cgd #include <netinet/ip.h>
49 1.1 cgd #include <arpa/ftp.h>
50 1.1 cgd #include <arpa/telnet.h>
51 1.1 cgd
52 1.1 cgd #include <stdio.h>
53 1.1 cgd #include <signal.h>
54 1.1 cgd #include <errno.h>
55 1.1 cgd #include <netdb.h>
56 1.1 cgd #include <fcntl.h>
57 1.1 cgd #include <pwd.h>
58 1.1 cgd #include <varargs.h>
59 1.1 cgd
60 1.1 cgd #include "ftp_var.h"
61 1.1 cgd
62 1.1 cgd struct sockaddr_in hisctladdr;
63 1.1 cgd struct sockaddr_in data_addr;
64 1.1 cgd int data = -1;
65 1.1 cgd int abrtflag = 0;
66 1.1 cgd int ptflag = 0;
67 1.1 cgd struct sockaddr_in myctladdr;
68 1.1 cgd uid_t getuid();
69 1.1 cgd sig_t lostpeer();
70 1.1 cgd off_t restart_point = 0;
71 1.1 cgd
72 1.1 cgd extern char *strerror();
73 1.1 cgd extern int connected, errno;
74 1.1 cgd
75 1.1 cgd FILE *cin, *cout;
76 1.1 cgd FILE *dataconn();
77 1.1 cgd
78 1.1 cgd char *
79 1.1 cgd hookup(host, port)
80 1.1 cgd char *host;
81 1.1 cgd int port;
82 1.1 cgd {
83 1.1 cgd register struct hostent *hp = 0;
84 1.1 cgd int s, len, tos;
85 1.1 cgd static char hostnamebuf[80];
86 1.1 cgd
87 1.1 cgd bzero((char *)&hisctladdr, sizeof (hisctladdr));
88 1.1 cgd hisctladdr.sin_addr.s_addr = inet_addr(host);
89 1.1 cgd if (hisctladdr.sin_addr.s_addr != -1) {
90 1.1 cgd hisctladdr.sin_family = AF_INET;
91 1.1 cgd (void) strncpy(hostnamebuf, host, sizeof(hostnamebuf));
92 1.1 cgd } else {
93 1.1 cgd hp = gethostbyname(host);
94 1.1 cgd if (hp == NULL) {
95 1.1 cgd fprintf(stderr, "ftp: %s: ", host);
96 1.1 cgd herror((char *)NULL);
97 1.1 cgd code = -1;
98 1.1 cgd return((char *) 0);
99 1.1 cgd }
100 1.1 cgd hisctladdr.sin_family = hp->h_addrtype;
101 1.1 cgd bcopy(hp->h_addr_list[0],
102 1.1 cgd (caddr_t)&hisctladdr.sin_addr, hp->h_length);
103 1.1 cgd (void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
104 1.1 cgd }
105 1.1 cgd hostname = hostnamebuf;
106 1.1 cgd s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
107 1.1 cgd if (s < 0) {
108 1.1 cgd perror("ftp: socket");
109 1.1 cgd code = -1;
110 1.1 cgd return (0);
111 1.1 cgd }
112 1.1 cgd hisctladdr.sin_port = port;
113 1.1 cgd while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
114 1.1 cgd if (hp && hp->h_addr_list[1]) {
115 1.1 cgd int oerrno = errno;
116 1.1 cgd extern char *inet_ntoa();
117 1.1 cgd
118 1.1 cgd fprintf(stderr, "ftp: connect to address %s: ",
119 1.1 cgd inet_ntoa(hisctladdr.sin_addr));
120 1.1 cgd errno = oerrno;
121 1.1 cgd perror((char *) 0);
122 1.1 cgd hp->h_addr_list++;
123 1.1 cgd bcopy(hp->h_addr_list[0],
124 1.1 cgd (caddr_t)&hisctladdr.sin_addr, hp->h_length);
125 1.1 cgd fprintf(stdout, "Trying %s...\n",
126 1.1 cgd inet_ntoa(hisctladdr.sin_addr));
127 1.1 cgd (void) close(s);
128 1.1 cgd s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
129 1.1 cgd if (s < 0) {
130 1.1 cgd perror("ftp: socket");
131 1.1 cgd code = -1;
132 1.1 cgd return (0);
133 1.1 cgd }
134 1.1 cgd continue;
135 1.1 cgd }
136 1.1 cgd perror("ftp: connect");
137 1.1 cgd code = -1;
138 1.1 cgd goto bad;
139 1.1 cgd }
140 1.1 cgd len = sizeof (myctladdr);
141 1.1 cgd if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
142 1.1 cgd perror("ftp: getsockname");
143 1.1 cgd code = -1;
144 1.1 cgd goto bad;
145 1.1 cgd }
146 1.1 cgd #ifdef IP_TOS
147 1.1 cgd tos = IPTOS_LOWDELAY;
148 1.1 cgd if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
149 1.1 cgd perror("ftp: setsockopt TOS (ignored)");
150 1.1 cgd #endif
151 1.1 cgd cin = fdopen(s, "r");
152 1.1 cgd cout = fdopen(s, "w");
153 1.1 cgd if (cin == NULL || cout == NULL) {
154 1.1 cgd fprintf(stderr, "ftp: fdopen failed.\n");
155 1.1 cgd if (cin)
156 1.1 cgd (void) fclose(cin);
157 1.1 cgd if (cout)
158 1.1 cgd (void) fclose(cout);
159 1.1 cgd code = -1;
160 1.1 cgd goto bad;
161 1.1 cgd }
162 1.1 cgd if (verbose)
163 1.1 cgd printf("Connected to %s.\n", hostname);
164 1.1 cgd if (getreply(0) > 2) { /* read startup message from server */
165 1.1 cgd if (cin)
166 1.1 cgd (void) fclose(cin);
167 1.1 cgd if (cout)
168 1.1 cgd (void) fclose(cout);
169 1.1 cgd code = -1;
170 1.1 cgd goto bad;
171 1.1 cgd }
172 1.1 cgd #ifdef SO_OOBINLINE
173 1.1 cgd {
174 1.1 cgd int on = 1;
175 1.1 cgd
176 1.1 cgd if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
177 1.1 cgd < 0 && debug) {
178 1.1 cgd perror("ftp: setsockopt");
179 1.1 cgd }
180 1.1 cgd }
181 1.1 cgd #endif /* SO_OOBINLINE */
182 1.1 cgd
183 1.1 cgd return (hostname);
184 1.1 cgd bad:
185 1.1 cgd (void) close(s);
186 1.1 cgd return ((char *)0);
187 1.1 cgd }
188 1.1 cgd
189 1.1 cgd login(host)
190 1.1 cgd char *host;
191 1.1 cgd {
192 1.1 cgd char tmp[80];
193 1.1 cgd char *user, *pass, *acct, *getlogin(), *getpass();
194 1.1 cgd int n, aflag = 0;
195 1.1 cgd
196 1.1 cgd user = pass = acct = 0;
197 1.1 cgd if (ruserpass(host, &user, &pass, &acct) < 0) {
198 1.1 cgd code = -1;
199 1.1 cgd return(0);
200 1.1 cgd }
201 1.1 cgd while (user == NULL) {
202 1.1 cgd char *myname = getlogin();
203 1.1 cgd
204 1.1 cgd if (myname == NULL) {
205 1.1 cgd struct passwd *pp = getpwuid(getuid());
206 1.1 cgd
207 1.1 cgd if (pp != NULL)
208 1.1 cgd myname = pp->pw_name;
209 1.1 cgd }
210 1.1 cgd if (myname)
211 1.1 cgd printf("Name (%s:%s): ", host, myname);
212 1.1 cgd else
213 1.1 cgd printf("Name (%s): ", host);
214 1.1 cgd (void) fgets(tmp, sizeof(tmp) - 1, stdin);
215 1.1 cgd tmp[strlen(tmp) - 1] = '\0';
216 1.1 cgd if (*tmp == '\0')
217 1.1 cgd user = myname;
218 1.1 cgd else
219 1.1 cgd user = tmp;
220 1.1 cgd }
221 1.1 cgd n = command("USER %s", user);
222 1.1 cgd if (n == CONTINUE) {
223 1.1 cgd if (pass == NULL)
224 1.1 cgd pass = getpass("Password:");
225 1.1 cgd n = command("PASS %s", pass);
226 1.1 cgd }
227 1.1 cgd if (n == CONTINUE) {
228 1.1 cgd aflag++;
229 1.1 cgd acct = getpass("Account:");
230 1.1 cgd n = command("ACCT %s", acct);
231 1.1 cgd }
232 1.1 cgd if (n != COMPLETE) {
233 1.1 cgd fprintf(stderr, "Login failed.\n");
234 1.1 cgd return (0);
235 1.1 cgd }
236 1.1 cgd if (!aflag && acct != NULL)
237 1.1 cgd (void) command("ACCT %s", acct);
238 1.1 cgd if (proxy)
239 1.1 cgd return(1);
240 1.1 cgd for (n = 0; n < macnum; ++n) {
241 1.1 cgd if (!strcmp("init", macros[n].mac_name)) {
242 1.1 cgd (void) strcpy(line, "$init");
243 1.1 cgd makeargv();
244 1.1 cgd domacro(margc, margv);
245 1.1 cgd break;
246 1.1 cgd }
247 1.1 cgd }
248 1.1 cgd return (1);
249 1.1 cgd }
250 1.1 cgd
251 1.1 cgd void
252 1.1 cgd cmdabort()
253 1.1 cgd {
254 1.1 cgd extern jmp_buf ptabort;
255 1.1 cgd
256 1.1 cgd printf("\n");
257 1.1 cgd (void) fflush(stdout);
258 1.1 cgd abrtflag++;
259 1.1 cgd if (ptflag)
260 1.1 cgd longjmp(ptabort,1);
261 1.1 cgd }
262 1.1 cgd
263 1.1 cgd /*VARARGS*/
264 1.1 cgd command(va_alist)
265 1.1 cgd va_dcl
266 1.1 cgd {
267 1.1 cgd va_list ap;
268 1.1 cgd char *fmt;
269 1.1 cgd int r;
270 1.1 cgd sig_t oldintr;
271 1.1 cgd void cmdabort();
272 1.1 cgd
273 1.1 cgd abrtflag = 0;
274 1.1 cgd if (debug) {
275 1.1 cgd printf("---> ");
276 1.1 cgd va_start(ap);
277 1.1 cgd fmt = va_arg(ap, char *);
278 1.1 cgd if (strncmp("PASS ", fmt, 5) == 0)
279 1.1 cgd printf("PASS XXXX");
280 1.1 cgd else
281 1.1 cgd vfprintf(stdout, fmt, ap);
282 1.1 cgd va_end(ap);
283 1.1 cgd printf("\n");
284 1.1 cgd (void) fflush(stdout);
285 1.1 cgd }
286 1.1 cgd if (cout == NULL) {
287 1.1 cgd perror ("No control connection for command");
288 1.1 cgd code = -1;
289 1.1 cgd return (0);
290 1.1 cgd }
291 1.1 cgd oldintr = signal(SIGINT, cmdabort);
292 1.1 cgd va_start(ap);
293 1.1 cgd fmt = va_arg(ap, char *);
294 1.1 cgd vfprintf(cout, fmt, ap);
295 1.1 cgd va_end(ap);
296 1.1 cgd fprintf(cout, "\r\n");
297 1.1 cgd (void) fflush(cout);
298 1.1 cgd cpend = 1;
299 1.1 cgd r = getreply(!strcmp(fmt, "QUIT"));
300 1.1 cgd if (abrtflag && oldintr != SIG_IGN)
301 1.1 cgd (*oldintr)(SIGINT);
302 1.1 cgd (void) signal(SIGINT, oldintr);
303 1.1 cgd return(r);
304 1.1 cgd }
305 1.1 cgd
306 1.1 cgd char reply_string[BUFSIZ]; /* last line of previous reply */
307 1.1 cgd
308 1.1 cgd #include <ctype.h>
309 1.1 cgd
310 1.1 cgd getreply(expecteof)
311 1.1 cgd int expecteof;
312 1.1 cgd {
313 1.1 cgd register int c, n;
314 1.1 cgd register int dig;
315 1.1 cgd register char *cp;
316 1.1 cgd int originalcode = 0, continuation = 0;
317 1.1 cgd sig_t oldintr;
318 1.1 cgd int pflag = 0;
319 1.1 cgd char *pt = pasv;
320 1.1 cgd void cmdabort();
321 1.1 cgd
322 1.1 cgd oldintr = signal(SIGINT, cmdabort);
323 1.1 cgd for (;;) {
324 1.1 cgd dig = n = code = 0;
325 1.1 cgd cp = reply_string;
326 1.1 cgd while ((c = getc(cin)) != '\n') {
327 1.1 cgd if (c == IAC) { /* handle telnet commands */
328 1.1 cgd switch (c = getc(cin)) {
329 1.1 cgd case WILL:
330 1.1 cgd case WONT:
331 1.1 cgd c = getc(cin);
332 1.1 cgd fprintf(cout, "%c%c%c", IAC, DONT, c);
333 1.1 cgd (void) fflush(cout);
334 1.1 cgd break;
335 1.1 cgd case DO:
336 1.1 cgd case DONT:
337 1.1 cgd c = getc(cin);
338 1.1 cgd fprintf(cout, "%c%c%c", IAC, WONT, c);
339 1.1 cgd (void) fflush(cout);
340 1.1 cgd break;
341 1.1 cgd default:
342 1.1 cgd break;
343 1.1 cgd }
344 1.1 cgd continue;
345 1.1 cgd }
346 1.1 cgd dig++;
347 1.1 cgd if (c == EOF) {
348 1.1 cgd if (expecteof) {
349 1.1 cgd (void) signal(SIGINT,oldintr);
350 1.1 cgd code = 221;
351 1.1 cgd return (0);
352 1.1 cgd }
353 1.1 cgd lostpeer();
354 1.1 cgd if (verbose) {
355 1.1 cgd printf("421 Service not available, remote server has closed connection\n");
356 1.1 cgd (void) fflush(stdout);
357 1.1 cgd }
358 1.1 cgd code = 421;
359 1.1 cgd return(4);
360 1.1 cgd }
361 1.1 cgd if (c != '\r' && (verbose > 0 ||
362 1.1 cgd (verbose > -1 && n == '5' && dig > 4))) {
363 1.1 cgd if (proxflag &&
364 1.1 cgd (dig == 1 || dig == 5 && verbose == 0))
365 1.1 cgd printf("%s:",hostname);
366 1.1 cgd (void) putchar(c);
367 1.1 cgd }
368 1.1 cgd if (dig < 4 && isdigit(c))
369 1.1 cgd code = code * 10 + (c - '0');
370 1.1 cgd if (!pflag && code == 227)
371 1.1 cgd pflag = 1;
372 1.1 cgd if (dig > 4 && pflag == 1 && isdigit(c))
373 1.1 cgd pflag = 2;
374 1.1 cgd if (pflag == 2) {
375 1.1 cgd if (c != '\r' && c != ')')
376 1.1 cgd *pt++ = c;
377 1.1 cgd else {
378 1.1 cgd *pt = '\0';
379 1.1 cgd pflag = 3;
380 1.1 cgd }
381 1.1 cgd }
382 1.1 cgd if (dig == 4 && c == '-') {
383 1.1 cgd if (continuation)
384 1.1 cgd code = 0;
385 1.1 cgd continuation++;
386 1.1 cgd }
387 1.1 cgd if (n == 0)
388 1.1 cgd n = c;
389 1.1 cgd if (cp < &reply_string[sizeof(reply_string) - 1])
390 1.1 cgd *cp++ = c;
391 1.1 cgd }
392 1.1 cgd if (verbose > 0 || verbose > -1 && n == '5') {
393 1.1 cgd (void) putchar(c);
394 1.1 cgd (void) fflush (stdout);
395 1.1 cgd }
396 1.1 cgd if (continuation && code != originalcode) {
397 1.1 cgd if (originalcode == 0)
398 1.1 cgd originalcode = code;
399 1.1 cgd continue;
400 1.1 cgd }
401 1.1 cgd *cp = '\0';
402 1.1 cgd if (n != '1')
403 1.1 cgd cpend = 0;
404 1.1 cgd (void) signal(SIGINT,oldintr);
405 1.1 cgd if (code == 421 || originalcode == 421)
406 1.1 cgd lostpeer();
407 1.1 cgd if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
408 1.1 cgd (*oldintr)(SIGINT);
409 1.1 cgd return (n - '0');
410 1.1 cgd }
411 1.1 cgd }
412 1.1 cgd
413 1.1 cgd empty(mask, sec)
414 1.1 cgd struct fd_set *mask;
415 1.1 cgd int sec;
416 1.1 cgd {
417 1.1 cgd struct timeval t;
418 1.1 cgd
419 1.1 cgd t.tv_sec = (long) sec;
420 1.1 cgd t.tv_usec = 0;
421 1.1 cgd return(select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t));
422 1.1 cgd }
423 1.1 cgd
424 1.1 cgd jmp_buf sendabort;
425 1.1 cgd
426 1.1 cgd void
427 1.1 cgd abortsend()
428 1.1 cgd {
429 1.1 cgd
430 1.1 cgd mflag = 0;
431 1.1 cgd abrtflag = 0;
432 1.1 cgd printf("\nsend aborted\nwaiting for remote to finish abort\n");
433 1.1 cgd (void) fflush(stdout);
434 1.1 cgd longjmp(sendabort, 1);
435 1.1 cgd }
436 1.1 cgd
437 1.1 cgd #define HASHBYTES 1024
438 1.1 cgd
439 1.1 cgd sendrequest(cmd, local, remote, printnames)
440 1.1 cgd char *cmd, *local, *remote;
441 1.1 cgd int printnames;
442 1.1 cgd {
443 1.1 cgd struct stat st;
444 1.1 cgd struct timeval start, stop;
445 1.1 cgd register int c, d;
446 1.1 cgd FILE *fin, *dout = 0, *popen();
447 1.1 cgd int (*closefunc)(), pclose(), fclose();
448 1.1 cgd sig_t oldintr, oldintp;
449 1.1 cgd long bytes = 0, hashbytes = HASHBYTES;
450 1.1 cgd char *lmode, buf[BUFSIZ], *bufp;
451 1.1 cgd void abortsend();
452 1.1 cgd
453 1.1 cgd if (verbose && printnames) {
454 1.1 cgd if (local && *local != '-')
455 1.1 cgd printf("local: %s ", local);
456 1.1 cgd if (remote)
457 1.1 cgd printf("remote: %s\n", remote);
458 1.1 cgd }
459 1.1 cgd if (proxy) {
460 1.1 cgd proxtrans(cmd, local, remote);
461 1.1 cgd return;
462 1.1 cgd }
463 1.1 cgd if (curtype != type)
464 1.1 cgd changetype(type, 0);
465 1.1 cgd closefunc = NULL;
466 1.1 cgd oldintr = NULL;
467 1.1 cgd oldintp = NULL;
468 1.1 cgd lmode = "w";
469 1.1 cgd if (setjmp(sendabort)) {
470 1.1 cgd while (cpend) {
471 1.1 cgd (void) getreply(0);
472 1.1 cgd }
473 1.1 cgd if (data >= 0) {
474 1.1 cgd (void) close(data);
475 1.1 cgd data = -1;
476 1.1 cgd }
477 1.1 cgd if (oldintr)
478 1.1 cgd (void) signal(SIGINT,oldintr);
479 1.1 cgd if (oldintp)
480 1.1 cgd (void) signal(SIGPIPE,oldintp);
481 1.1 cgd code = -1;
482 1.1 cgd return;
483 1.1 cgd }
484 1.1 cgd oldintr = signal(SIGINT, abortsend);
485 1.1 cgd if (strcmp(local, "-") == 0)
486 1.1 cgd fin = stdin;
487 1.1 cgd else if (*local == '|') {
488 1.1 cgd oldintp = signal(SIGPIPE,SIG_IGN);
489 1.1 cgd fin = popen(local + 1, "r");
490 1.1 cgd if (fin == NULL) {
491 1.1 cgd perror(local + 1);
492 1.1 cgd (void) signal(SIGINT, oldintr);
493 1.1 cgd (void) signal(SIGPIPE, oldintp);
494 1.1 cgd code = -1;
495 1.1 cgd return;
496 1.1 cgd }
497 1.1 cgd closefunc = pclose;
498 1.1 cgd } else {
499 1.1 cgd fin = fopen(local, "r");
500 1.1 cgd if (fin == NULL) {
501 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
502 1.1 cgd strerror(errno));
503 1.1 cgd (void) signal(SIGINT, oldintr);
504 1.1 cgd code = -1;
505 1.1 cgd return;
506 1.1 cgd }
507 1.1 cgd closefunc = fclose;
508 1.1 cgd if (fstat(fileno(fin), &st) < 0 ||
509 1.1 cgd (st.st_mode&S_IFMT) != S_IFREG) {
510 1.1 cgd fprintf(stdout, "%s: not a plain file.\n", local);
511 1.1 cgd (void) signal(SIGINT, oldintr);
512 1.1 cgd fclose(fin);
513 1.1 cgd code = -1;
514 1.1 cgd return;
515 1.1 cgd }
516 1.1 cgd }
517 1.1 cgd if (initconn()) {
518 1.1 cgd (void) signal(SIGINT, oldintr);
519 1.1 cgd if (oldintp)
520 1.1 cgd (void) signal(SIGPIPE, oldintp);
521 1.1 cgd code = -1;
522 1.1 cgd if (closefunc != NULL)
523 1.1 cgd (*closefunc)(fin);
524 1.1 cgd return;
525 1.1 cgd }
526 1.1 cgd if (setjmp(sendabort))
527 1.1 cgd goto abort;
528 1.1 cgd
529 1.1 cgd if (restart_point &&
530 1.1 cgd (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
531 1.1 cgd if (fseek(fin, (long) restart_point, 0) < 0) {
532 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
533 1.1 cgd strerror(errno));
534 1.1 cgd restart_point = 0;
535 1.1 cgd if (closefunc != NULL)
536 1.1 cgd (*closefunc)(fin);
537 1.1 cgd return;
538 1.1 cgd }
539 1.1 cgd if (command("REST %ld", (long) restart_point)
540 1.1 cgd != CONTINUE) {
541 1.1 cgd restart_point = 0;
542 1.1 cgd if (closefunc != NULL)
543 1.1 cgd (*closefunc)(fin);
544 1.1 cgd return;
545 1.1 cgd }
546 1.1 cgd restart_point = 0;
547 1.1 cgd lmode = "r+w";
548 1.1 cgd }
549 1.1 cgd if (remote) {
550 1.1 cgd if (command("%s %s", cmd, remote) != PRELIM) {
551 1.1 cgd (void) signal(SIGINT, oldintr);
552 1.1 cgd if (oldintp)
553 1.1 cgd (void) signal(SIGPIPE, oldintp);
554 1.1 cgd if (closefunc != NULL)
555 1.1 cgd (*closefunc)(fin);
556 1.1 cgd return;
557 1.1 cgd }
558 1.1 cgd } else
559 1.1 cgd if (command("%s", cmd) != PRELIM) {
560 1.1 cgd (void) signal(SIGINT, oldintr);
561 1.1 cgd if (oldintp)
562 1.1 cgd (void) signal(SIGPIPE, oldintp);
563 1.1 cgd if (closefunc != NULL)
564 1.1 cgd (*closefunc)(fin);
565 1.1 cgd return;
566 1.1 cgd }
567 1.1 cgd dout = dataconn(lmode);
568 1.1 cgd if (dout == NULL)
569 1.1 cgd goto abort;
570 1.1 cgd (void) gettimeofday(&start, (struct timezone *)0);
571 1.1 cgd oldintp = signal(SIGPIPE, SIG_IGN);
572 1.1 cgd switch (curtype) {
573 1.1 cgd
574 1.1 cgd case TYPE_I:
575 1.1 cgd case TYPE_L:
576 1.1 cgd errno = d = 0;
577 1.1 cgd while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
578 1.1 cgd bytes += c;
579 1.1 cgd for (bufp = buf; c > 0; c -= d, bufp += d)
580 1.1 cgd if ((d = write(fileno(dout), bufp, c)) <= 0)
581 1.1 cgd break;
582 1.1 cgd if (hash) {
583 1.1 cgd while (bytes >= hashbytes) {
584 1.1 cgd (void) putchar('#');
585 1.1 cgd hashbytes += HASHBYTES;
586 1.1 cgd }
587 1.1 cgd (void) fflush(stdout);
588 1.1 cgd }
589 1.1 cgd }
590 1.1 cgd if (hash && bytes > 0) {
591 1.1 cgd if (bytes < HASHBYTES)
592 1.1 cgd (void) putchar('#');
593 1.1 cgd (void) putchar('\n');
594 1.1 cgd (void) fflush(stdout);
595 1.1 cgd }
596 1.1 cgd if (c < 0)
597 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
598 1.1 cgd strerror(errno));
599 1.1 cgd if (d < 0) {
600 1.1 cgd if (errno != EPIPE)
601 1.1 cgd perror("netout");
602 1.1 cgd bytes = -1;
603 1.1 cgd }
604 1.1 cgd break;
605 1.1 cgd
606 1.1 cgd case TYPE_A:
607 1.1 cgd while ((c = getc(fin)) != EOF) {
608 1.1 cgd if (c == '\n') {
609 1.1 cgd while (hash && (bytes >= hashbytes)) {
610 1.1 cgd (void) putchar('#');
611 1.1 cgd (void) fflush(stdout);
612 1.1 cgd hashbytes += HASHBYTES;
613 1.1 cgd }
614 1.1 cgd if (ferror(dout))
615 1.1 cgd break;
616 1.1 cgd (void) putc('\r', dout);
617 1.1 cgd bytes++;
618 1.1 cgd }
619 1.1 cgd (void) putc(c, dout);
620 1.1 cgd bytes++;
621 1.1 cgd /* if (c == '\r') { */
622 1.1 cgd /* (void) putc('\0', dout); /* this violates rfc */
623 1.1 cgd /* bytes++; */
624 1.1 cgd /* } */
625 1.1 cgd }
626 1.1 cgd if (hash) {
627 1.1 cgd if (bytes < hashbytes)
628 1.1 cgd (void) putchar('#');
629 1.1 cgd (void) putchar('\n');
630 1.1 cgd (void) fflush(stdout);
631 1.1 cgd }
632 1.1 cgd if (ferror(fin))
633 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
634 1.1 cgd strerror(errno));
635 1.1 cgd if (ferror(dout)) {
636 1.1 cgd if (errno != EPIPE)
637 1.1 cgd perror("netout");
638 1.1 cgd bytes = -1;
639 1.1 cgd }
640 1.1 cgd break;
641 1.1 cgd }
642 1.1 cgd (void) gettimeofday(&stop, (struct timezone *)0);
643 1.1 cgd if (closefunc != NULL)
644 1.1 cgd (*closefunc)(fin);
645 1.1 cgd (void) fclose(dout);
646 1.1 cgd (void) getreply(0);
647 1.1 cgd (void) signal(SIGINT, oldintr);
648 1.1 cgd if (oldintp)
649 1.1 cgd (void) signal(SIGPIPE, oldintp);
650 1.1 cgd if (bytes > 0)
651 1.1 cgd ptransfer("sent", bytes, &start, &stop);
652 1.1 cgd return;
653 1.1 cgd abort:
654 1.1 cgd (void) gettimeofday(&stop, (struct timezone *)0);
655 1.1 cgd (void) signal(SIGINT, oldintr);
656 1.1 cgd if (oldintp)
657 1.1 cgd (void) signal(SIGPIPE, oldintp);
658 1.1 cgd if (!cpend) {
659 1.1 cgd code = -1;
660 1.1 cgd return;
661 1.1 cgd }
662 1.1 cgd if (data >= 0) {
663 1.1 cgd (void) close(data);
664 1.1 cgd data = -1;
665 1.1 cgd }
666 1.1 cgd if (dout)
667 1.1 cgd (void) fclose(dout);
668 1.1 cgd (void) getreply(0);
669 1.1 cgd code = -1;
670 1.1 cgd if (closefunc != NULL && fin != NULL)
671 1.1 cgd (*closefunc)(fin);
672 1.1 cgd if (bytes > 0)
673 1.1 cgd ptransfer("sent", bytes, &start, &stop);
674 1.1 cgd }
675 1.1 cgd
676 1.1 cgd jmp_buf recvabort;
677 1.1 cgd
678 1.1 cgd void
679 1.1 cgd abortrecv()
680 1.1 cgd {
681 1.1 cgd
682 1.1 cgd mflag = 0;
683 1.1 cgd abrtflag = 0;
684 1.1 cgd printf("\nreceive aborted\nwaiting for remote to finish abort\n");
685 1.1 cgd (void) fflush(stdout);
686 1.1 cgd longjmp(recvabort, 1);
687 1.1 cgd }
688 1.1 cgd
689 1.1 cgd recvrequest(cmd, local, remote, lmode, printnames)
690 1.1 cgd char *cmd, *local, *remote, *lmode;
691 1.1 cgd {
692 1.1 cgd FILE *fout, *din = 0, *popen();
693 1.1 cgd int (*closefunc)(), pclose(), fclose();
694 1.1 cgd sig_t oldintr, oldintp;
695 1.1 cgd int is_retr, tcrflag, bare_lfs = 0;
696 1.1 cgd char *gunique();
697 1.1 cgd static int bufsize;
698 1.1 cgd static char *buf;
699 1.1 cgd long bytes = 0, hashbytes = HASHBYTES;
700 1.1 cgd register int c, d;
701 1.1 cgd struct timeval start, stop;
702 1.1 cgd struct stat st;
703 1.1 cgd off_t lseek();
704 1.1 cgd void abortrecv();
705 1.1 cgd char *malloc();
706 1.1 cgd
707 1.1 cgd is_retr = strcmp(cmd, "RETR") == 0;
708 1.1 cgd if (is_retr && verbose && printnames) {
709 1.1 cgd if (local && *local != '-')
710 1.1 cgd printf("local: %s ", local);
711 1.1 cgd if (remote)
712 1.1 cgd printf("remote: %s\n", remote);
713 1.1 cgd }
714 1.1 cgd if (proxy && is_retr) {
715 1.1 cgd proxtrans(cmd, local, remote);
716 1.1 cgd return;
717 1.1 cgd }
718 1.1 cgd closefunc = NULL;
719 1.1 cgd oldintr = NULL;
720 1.1 cgd oldintp = NULL;
721 1.1 cgd tcrflag = !crflag && is_retr;
722 1.1 cgd if (setjmp(recvabort)) {
723 1.1 cgd while (cpend) {
724 1.1 cgd (void) getreply(0);
725 1.1 cgd }
726 1.1 cgd if (data >= 0) {
727 1.1 cgd (void) close(data);
728 1.1 cgd data = -1;
729 1.1 cgd }
730 1.1 cgd if (oldintr)
731 1.1 cgd (void) signal(SIGINT, oldintr);
732 1.1 cgd code = -1;
733 1.1 cgd return;
734 1.1 cgd }
735 1.1 cgd oldintr = signal(SIGINT, abortrecv);
736 1.1 cgd if (strcmp(local, "-") && *local != '|') {
737 1.1 cgd if (access(local, 2) < 0) {
738 1.1 cgd char *dir = rindex(local, '/');
739 1.1 cgd
740 1.1 cgd if (errno != ENOENT && errno != EACCES) {
741 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
742 1.1 cgd strerror(errno));
743 1.1 cgd (void) signal(SIGINT, oldintr);
744 1.1 cgd code = -1;
745 1.1 cgd return;
746 1.1 cgd }
747 1.1 cgd if (dir != NULL)
748 1.1 cgd *dir = 0;
749 1.1 cgd d = access(dir ? local : ".", 2);
750 1.1 cgd if (dir != NULL)
751 1.1 cgd *dir = '/';
752 1.1 cgd if (d < 0) {
753 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
754 1.1 cgd strerror(errno));
755 1.1 cgd (void) signal(SIGINT, oldintr);
756 1.1 cgd code = -1;
757 1.1 cgd return;
758 1.1 cgd }
759 1.1 cgd if (!runique && errno == EACCES &&
760 1.1 cgd chmod(local, 0600) < 0) {
761 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
762 1.1 cgd strerror(errno));
763 1.1 cgd (void) signal(SIGINT, oldintr);
764 1.1 cgd (void) signal(SIGINT, oldintr);
765 1.1 cgd code = -1;
766 1.1 cgd return;
767 1.1 cgd }
768 1.1 cgd if (runique && errno == EACCES &&
769 1.1 cgd (local = gunique(local)) == NULL) {
770 1.1 cgd (void) signal(SIGINT, oldintr);
771 1.1 cgd code = -1;
772 1.1 cgd return;
773 1.1 cgd }
774 1.1 cgd }
775 1.1 cgd else if (runique && (local = gunique(local)) == NULL) {
776 1.1 cgd (void) signal(SIGINT, oldintr);
777 1.1 cgd code = -1;
778 1.1 cgd return;
779 1.1 cgd }
780 1.1 cgd }
781 1.1 cgd if (!is_retr) {
782 1.1 cgd if (curtype != TYPE_A)
783 1.1 cgd changetype(TYPE_A, 0);
784 1.1 cgd } else if (curtype != type)
785 1.1 cgd changetype(type, 0);
786 1.1 cgd if (initconn()) {
787 1.1 cgd (void) signal(SIGINT, oldintr);
788 1.1 cgd code = -1;
789 1.1 cgd return;
790 1.1 cgd }
791 1.1 cgd if (setjmp(recvabort))
792 1.1 cgd goto abort;
793 1.1 cgd if (is_retr && restart_point &&
794 1.1 cgd command("REST %ld", (long) restart_point) != CONTINUE)
795 1.1 cgd return;
796 1.1 cgd if (remote) {
797 1.1 cgd if (command("%s %s", cmd, remote) != PRELIM) {
798 1.1 cgd (void) signal(SIGINT, oldintr);
799 1.1 cgd return;
800 1.1 cgd }
801 1.1 cgd } else {
802 1.1 cgd if (command("%s", cmd) != PRELIM) {
803 1.1 cgd (void) signal(SIGINT, oldintr);
804 1.1 cgd return;
805 1.1 cgd }
806 1.1 cgd }
807 1.1 cgd din = dataconn("r");
808 1.1 cgd if (din == NULL)
809 1.1 cgd goto abort;
810 1.1 cgd if (strcmp(local, "-") == 0)
811 1.1 cgd fout = stdout;
812 1.1 cgd else if (*local == '|') {
813 1.1 cgd oldintp = signal(SIGPIPE, SIG_IGN);
814 1.1 cgd fout = popen(local + 1, "w");
815 1.1 cgd if (fout == NULL) {
816 1.1 cgd perror(local+1);
817 1.1 cgd goto abort;
818 1.1 cgd }
819 1.1 cgd closefunc = pclose;
820 1.1 cgd } else {
821 1.1 cgd fout = fopen(local, lmode);
822 1.1 cgd if (fout == NULL) {
823 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
824 1.1 cgd strerror(errno));
825 1.1 cgd goto abort;
826 1.1 cgd }
827 1.1 cgd closefunc = fclose;
828 1.1 cgd }
829 1.1 cgd if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
830 1.1 cgd st.st_blksize = BUFSIZ;
831 1.1 cgd if (st.st_blksize > bufsize) {
832 1.1 cgd if (buf)
833 1.1 cgd (void) free(buf);
834 1.1 cgd buf = malloc((unsigned)st.st_blksize);
835 1.1 cgd if (buf == NULL) {
836 1.1 cgd perror("malloc");
837 1.1 cgd bufsize = 0;
838 1.1 cgd goto abort;
839 1.1 cgd }
840 1.1 cgd bufsize = st.st_blksize;
841 1.1 cgd }
842 1.1 cgd (void) gettimeofday(&start, (struct timezone *)0);
843 1.1 cgd switch (curtype) {
844 1.1 cgd
845 1.1 cgd case TYPE_I:
846 1.1 cgd case TYPE_L:
847 1.1 cgd if (restart_point &&
848 1.1 cgd lseek(fileno(fout), (long) restart_point, L_SET) < 0) {
849 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
850 1.1 cgd strerror(errno));
851 1.1 cgd if (closefunc != NULL)
852 1.1 cgd (*closefunc)(fout);
853 1.1 cgd return;
854 1.1 cgd }
855 1.1 cgd errno = d = 0;
856 1.1 cgd while ((c = read(fileno(din), buf, bufsize)) > 0) {
857 1.1 cgd if ((d = write(fileno(fout), buf, c)) != c)
858 1.1 cgd break;
859 1.1 cgd bytes += c;
860 1.1 cgd if (hash) {
861 1.1 cgd while (bytes >= hashbytes) {
862 1.1 cgd (void) putchar('#');
863 1.1 cgd hashbytes += HASHBYTES;
864 1.1 cgd }
865 1.1 cgd (void) fflush(stdout);
866 1.1 cgd }
867 1.1 cgd }
868 1.1 cgd if (hash && bytes > 0) {
869 1.1 cgd if (bytes < HASHBYTES)
870 1.1 cgd (void) putchar('#');
871 1.1 cgd (void) putchar('\n');
872 1.1 cgd (void) fflush(stdout);
873 1.1 cgd }
874 1.1 cgd if (c < 0) {
875 1.1 cgd if (errno != EPIPE)
876 1.1 cgd perror("netin");
877 1.1 cgd bytes = -1;
878 1.1 cgd }
879 1.1 cgd if (d < c) {
880 1.1 cgd if (d < 0)
881 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
882 1.1 cgd strerror(errno));
883 1.1 cgd else
884 1.1 cgd fprintf(stderr, "%s: short write\n", local);
885 1.1 cgd }
886 1.1 cgd break;
887 1.1 cgd
888 1.1 cgd case TYPE_A:
889 1.1 cgd if (restart_point) {
890 1.1 cgd register int i, n, ch;
891 1.1 cgd
892 1.1 cgd if (fseek(fout, 0L, L_SET) < 0)
893 1.1 cgd goto done;
894 1.1 cgd n = restart_point;
895 1.1 cgd for (i = 0; i++ < n;) {
896 1.1 cgd if ((ch = getc(fout)) == EOF)
897 1.1 cgd goto done;
898 1.1 cgd if (ch == '\n')
899 1.1 cgd i++;
900 1.1 cgd }
901 1.1 cgd if (fseek(fout, 0L, L_INCR) < 0) {
902 1.1 cgd done:
903 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
904 1.1 cgd strerror(errno));
905 1.1 cgd if (closefunc != NULL)
906 1.1 cgd (*closefunc)(fout);
907 1.1 cgd return;
908 1.1 cgd }
909 1.1 cgd }
910 1.1 cgd while ((c = getc(din)) != EOF) {
911 1.1 cgd if (c == '\n')
912 1.1 cgd bare_lfs++;
913 1.1 cgd while (c == '\r') {
914 1.1 cgd while (hash && (bytes >= hashbytes)) {
915 1.1 cgd (void) putchar('#');
916 1.1 cgd (void) fflush(stdout);
917 1.1 cgd hashbytes += HASHBYTES;
918 1.1 cgd }
919 1.1 cgd bytes++;
920 1.1 cgd if ((c = getc(din)) != '\n' || tcrflag) {
921 1.1 cgd if (ferror(fout))
922 1.1 cgd goto break2;
923 1.1 cgd (void) putc('\r', fout);
924 1.1 cgd if (c == '\0') {
925 1.1 cgd bytes++;
926 1.1 cgd goto contin2;
927 1.1 cgd }
928 1.1 cgd if (c == EOF)
929 1.1 cgd goto contin2;
930 1.1 cgd }
931 1.1 cgd }
932 1.1 cgd (void) putc(c, fout);
933 1.1 cgd bytes++;
934 1.1 cgd contin2: ;
935 1.1 cgd }
936 1.1 cgd break2:
937 1.1 cgd if (bare_lfs) {
938 1.1 cgd printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs);
939 1.1 cgd printf("File may not have transferred correctly.\n");
940 1.1 cgd }
941 1.1 cgd if (hash) {
942 1.1 cgd if (bytes < hashbytes)
943 1.1 cgd (void) putchar('#');
944 1.1 cgd (void) putchar('\n');
945 1.1 cgd (void) fflush(stdout);
946 1.1 cgd }
947 1.1 cgd if (ferror(din)) {
948 1.1 cgd if (errno != EPIPE)
949 1.1 cgd perror("netin");
950 1.1 cgd bytes = -1;
951 1.1 cgd }
952 1.1 cgd if (ferror(fout))
953 1.1 cgd fprintf(stderr, "local: %s: %s\n", local,
954 1.1 cgd strerror(errno));
955 1.1 cgd break;
956 1.1 cgd }
957 1.1 cgd if (closefunc != NULL)
958 1.1 cgd (*closefunc)(fout);
959 1.1 cgd (void) signal(SIGINT, oldintr);
960 1.1 cgd if (oldintp)
961 1.1 cgd (void) signal(SIGPIPE, oldintp);
962 1.1 cgd (void) gettimeofday(&stop, (struct timezone *)0);
963 1.1 cgd (void) fclose(din);
964 1.1 cgd (void) getreply(0);
965 1.1 cgd if (bytes > 0 && is_retr)
966 1.1 cgd ptransfer("received", bytes, &start, &stop);
967 1.1 cgd return;
968 1.1 cgd abort:
969 1.1 cgd
970 1.1 cgd /* abort using RFC959 recommended IP,SYNC sequence */
971 1.1 cgd
972 1.1 cgd (void) gettimeofday(&stop, (struct timezone *)0);
973 1.1 cgd if (oldintp)
974 1.1 cgd (void) signal(SIGPIPE, oldintr);
975 1.1 cgd (void) signal(SIGINT, SIG_IGN);
976 1.1 cgd if (!cpend) {
977 1.1 cgd code = -1;
978 1.1 cgd (void) signal(SIGINT, oldintr);
979 1.1 cgd return;
980 1.1 cgd }
981 1.1 cgd
982 1.1 cgd abort_remote(din);
983 1.1 cgd code = -1;
984 1.1 cgd if (data >= 0) {
985 1.1 cgd (void) close(data);
986 1.1 cgd data = -1;
987 1.1 cgd }
988 1.1 cgd if (closefunc != NULL && fout != NULL)
989 1.1 cgd (*closefunc)(fout);
990 1.1 cgd if (din)
991 1.1 cgd (void) fclose(din);
992 1.1 cgd if (bytes > 0)
993 1.1 cgd ptransfer("received", bytes, &start, &stop);
994 1.1 cgd (void) signal(SIGINT, oldintr);
995 1.1 cgd }
996 1.1 cgd
997 1.1 cgd /*
998 1.1 cgd * Need to start a listen on the data channel before we send the command,
999 1.1 cgd * otherwise the server's connect may fail.
1000 1.1 cgd */
1001 1.1 cgd initconn()
1002 1.1 cgd {
1003 1.1 cgd register char *p, *a;
1004 1.1 cgd int result, len, tmpno = 0;
1005 1.1 cgd int on = 1;
1006 1.1 cgd
1007 1.1 cgd noport:
1008 1.1 cgd data_addr = myctladdr;
1009 1.1 cgd if (sendport)
1010 1.1 cgd data_addr.sin_port = 0; /* let system pick one */
1011 1.1 cgd if (data != -1)
1012 1.1 cgd (void) close(data);
1013 1.1 cgd data = socket(AF_INET, SOCK_STREAM, 0);
1014 1.1 cgd if (data < 0) {
1015 1.1 cgd perror("ftp: socket");
1016 1.1 cgd if (tmpno)
1017 1.1 cgd sendport = 1;
1018 1.1 cgd return (1);
1019 1.1 cgd }
1020 1.1 cgd if (!sendport)
1021 1.1 cgd if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
1022 1.1 cgd perror("ftp: setsockopt (reuse address)");
1023 1.1 cgd goto bad;
1024 1.1 cgd }
1025 1.1 cgd if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
1026 1.1 cgd perror("ftp: bind");
1027 1.1 cgd goto bad;
1028 1.1 cgd }
1029 1.1 cgd if (options & SO_DEBUG &&
1030 1.1 cgd setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
1031 1.1 cgd perror("ftp: setsockopt (ignored)");
1032 1.1 cgd len = sizeof (data_addr);
1033 1.1 cgd if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1034 1.1 cgd perror("ftp: getsockname");
1035 1.1 cgd goto bad;
1036 1.1 cgd }
1037 1.1 cgd if (listen(data, 1) < 0)
1038 1.1 cgd perror("ftp: listen");
1039 1.1 cgd if (sendport) {
1040 1.1 cgd a = (char *)&data_addr.sin_addr;
1041 1.1 cgd p = (char *)&data_addr.sin_port;
1042 1.1 cgd #define UC(b) (((int)b)&0xff)
1043 1.1 cgd result =
1044 1.1 cgd command("PORT %d,%d,%d,%d,%d,%d",
1045 1.1 cgd UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1046 1.1 cgd UC(p[0]), UC(p[1]));
1047 1.1 cgd if (result == ERROR && sendport == -1) {
1048 1.1 cgd sendport = 0;
1049 1.1 cgd tmpno = 1;
1050 1.1 cgd goto noport;
1051 1.1 cgd }
1052 1.1 cgd return (result != COMPLETE);
1053 1.1 cgd }
1054 1.1 cgd if (tmpno)
1055 1.1 cgd sendport = 1;
1056 1.1 cgd #ifdef IP_TOS
1057 1.1 cgd on = IPTOS_THROUGHPUT;
1058 1.1 cgd if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1059 1.1 cgd perror("ftp: setsockopt TOS (ignored)");
1060 1.1 cgd #endif
1061 1.1 cgd return (0);
1062 1.1 cgd bad:
1063 1.1 cgd (void) close(data), data = -1;
1064 1.1 cgd if (tmpno)
1065 1.1 cgd sendport = 1;
1066 1.1 cgd return (1);
1067 1.1 cgd }
1068 1.1 cgd
1069 1.1 cgd FILE *
1070 1.1 cgd dataconn(lmode)
1071 1.1 cgd char *lmode;
1072 1.1 cgd {
1073 1.1 cgd struct sockaddr_in from;
1074 1.1 cgd int s, fromlen = sizeof (from), tos;
1075 1.1 cgd
1076 1.1 cgd s = accept(data, (struct sockaddr *) &from, &fromlen);
1077 1.1 cgd if (s < 0) {
1078 1.1 cgd perror("ftp: accept");
1079 1.1 cgd (void) close(data), data = -1;
1080 1.1 cgd return (NULL);
1081 1.1 cgd }
1082 1.1 cgd (void) close(data);
1083 1.1 cgd data = s;
1084 1.1 cgd #ifdef IP_TOS
1085 1.1 cgd tos = IPTOS_THROUGHPUT;
1086 1.1 cgd if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
1087 1.1 cgd perror("ftp: setsockopt TOS (ignored)");
1088 1.1 cgd #endif
1089 1.1 cgd return (fdopen(data, lmode));
1090 1.1 cgd }
1091 1.1 cgd
1092 1.1 cgd ptransfer(direction, bytes, t0, t1)
1093 1.1 cgd char *direction;
1094 1.1 cgd long bytes;
1095 1.1 cgd struct timeval *t0, *t1;
1096 1.1 cgd {
1097 1.1 cgd struct timeval td;
1098 1.1 cgd float s, bs;
1099 1.1 cgd
1100 1.1 cgd if (verbose) {
1101 1.1 cgd tvsub(&td, t1, t0);
1102 1.1 cgd s = td.tv_sec + (td.tv_usec / 1000000.);
1103 1.1 cgd #define nz(x) ((x) == 0 ? 1 : (x))
1104 1.1 cgd bs = bytes / nz(s);
1105 1.1 cgd printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n",
1106 1.1 cgd bytes, direction, s, bs / 1024.);
1107 1.1 cgd }
1108 1.1 cgd }
1109 1.1 cgd
1110 1.1 cgd /*tvadd(tsum, t0)
1111 1.1 cgd struct timeval *tsum, *t0;
1112 1.1 cgd {
1113 1.1 cgd
1114 1.1 cgd tsum->tv_sec += t0->tv_sec;
1115 1.1 cgd tsum->tv_usec += t0->tv_usec;
1116 1.1 cgd if (tsum->tv_usec > 1000000)
1117 1.1 cgd tsum->tv_sec++, tsum->tv_usec -= 1000000;
1118 1.1 cgd } */
1119 1.1 cgd
1120 1.1 cgd tvsub(tdiff, t1, t0)
1121 1.1 cgd struct timeval *tdiff, *t1, *t0;
1122 1.1 cgd {
1123 1.1 cgd
1124 1.1 cgd tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
1125 1.1 cgd tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
1126 1.1 cgd if (tdiff->tv_usec < 0)
1127 1.1 cgd tdiff->tv_sec--, tdiff->tv_usec += 1000000;
1128 1.1 cgd }
1129 1.1 cgd
1130 1.1 cgd void
1131 1.1 cgd psabort()
1132 1.1 cgd {
1133 1.1 cgd extern int abrtflag;
1134 1.1 cgd
1135 1.1 cgd abrtflag++;
1136 1.1 cgd }
1137 1.1 cgd
1138 1.1 cgd pswitch(flag)
1139 1.1 cgd int flag;
1140 1.1 cgd {
1141 1.1 cgd extern int proxy, abrtflag;
1142 1.1 cgd sig_t oldintr;
1143 1.1 cgd static struct comvars {
1144 1.1 cgd int connect;
1145 1.1 cgd char name[MAXHOSTNAMELEN];
1146 1.1 cgd struct sockaddr_in mctl;
1147 1.1 cgd struct sockaddr_in hctl;
1148 1.1 cgd FILE *in;
1149 1.1 cgd FILE *out;
1150 1.1 cgd int tpe;
1151 1.1 cgd int curtpe;
1152 1.1 cgd int cpnd;
1153 1.1 cgd int sunqe;
1154 1.1 cgd int runqe;
1155 1.1 cgd int mcse;
1156 1.1 cgd int ntflg;
1157 1.1 cgd char nti[17];
1158 1.1 cgd char nto[17];
1159 1.1 cgd int mapflg;
1160 1.1 cgd char mi[MAXPATHLEN];
1161 1.1 cgd char mo[MAXPATHLEN];
1162 1.1 cgd } proxstruct, tmpstruct;
1163 1.1 cgd struct comvars *ip, *op;
1164 1.1 cgd
1165 1.1 cgd abrtflag = 0;
1166 1.1 cgd oldintr = signal(SIGINT, psabort);
1167 1.1 cgd if (flag) {
1168 1.1 cgd if (proxy)
1169 1.1 cgd return;
1170 1.1 cgd ip = &tmpstruct;
1171 1.1 cgd op = &proxstruct;
1172 1.1 cgd proxy++;
1173 1.1 cgd } else {
1174 1.1 cgd if (!proxy)
1175 1.1 cgd return;
1176 1.1 cgd ip = &proxstruct;
1177 1.1 cgd op = &tmpstruct;
1178 1.1 cgd proxy = 0;
1179 1.1 cgd }
1180 1.1 cgd ip->connect = connected;
1181 1.1 cgd connected = op->connect;
1182 1.1 cgd if (hostname) {
1183 1.1 cgd (void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
1184 1.1 cgd ip->name[strlen(ip->name)] = '\0';
1185 1.1 cgd } else
1186 1.1 cgd ip->name[0] = 0;
1187 1.1 cgd hostname = op->name;
1188 1.1 cgd ip->hctl = hisctladdr;
1189 1.1 cgd hisctladdr = op->hctl;
1190 1.1 cgd ip->mctl = myctladdr;
1191 1.1 cgd myctladdr = op->mctl;
1192 1.1 cgd ip->in = cin;
1193 1.1 cgd cin = op->in;
1194 1.1 cgd ip->out = cout;
1195 1.1 cgd cout = op->out;
1196 1.1 cgd ip->tpe = type;
1197 1.1 cgd type = op->tpe;
1198 1.1 cgd ip->curtpe = curtype;
1199 1.1 cgd curtype = op->curtpe;
1200 1.1 cgd ip->cpnd = cpend;
1201 1.1 cgd cpend = op->cpnd;
1202 1.1 cgd ip->sunqe = sunique;
1203 1.1 cgd sunique = op->sunqe;
1204 1.1 cgd ip->runqe = runique;
1205 1.1 cgd runique = op->runqe;
1206 1.1 cgd ip->mcse = mcase;
1207 1.1 cgd mcase = op->mcse;
1208 1.1 cgd ip->ntflg = ntflag;
1209 1.1 cgd ntflag = op->ntflg;
1210 1.1 cgd (void) strncpy(ip->nti, ntin, 16);
1211 1.1 cgd (ip->nti)[strlen(ip->nti)] = '\0';
1212 1.1 cgd (void) strcpy(ntin, op->nti);
1213 1.1 cgd (void) strncpy(ip->nto, ntout, 16);
1214 1.1 cgd (ip->nto)[strlen(ip->nto)] = '\0';
1215 1.1 cgd (void) strcpy(ntout, op->nto);
1216 1.1 cgd ip->mapflg = mapflag;
1217 1.1 cgd mapflag = op->mapflg;
1218 1.1 cgd (void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
1219 1.1 cgd (ip->mi)[strlen(ip->mi)] = '\0';
1220 1.1 cgd (void) strcpy(mapin, op->mi);
1221 1.1 cgd (void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
1222 1.1 cgd (ip->mo)[strlen(ip->mo)] = '\0';
1223 1.1 cgd (void) strcpy(mapout, op->mo);
1224 1.1 cgd (void) signal(SIGINT, oldintr);
1225 1.1 cgd if (abrtflag) {
1226 1.1 cgd abrtflag = 0;
1227 1.1 cgd (*oldintr)(SIGINT);
1228 1.1 cgd }
1229 1.1 cgd }
1230 1.1 cgd
1231 1.1 cgd jmp_buf ptabort;
1232 1.1 cgd int ptabflg;
1233 1.1 cgd
1234 1.1 cgd void
1235 1.1 cgd abortpt()
1236 1.1 cgd {
1237 1.1 cgd printf("\n");
1238 1.1 cgd (void) fflush(stdout);
1239 1.1 cgd ptabflg++;
1240 1.1 cgd mflag = 0;
1241 1.1 cgd abrtflag = 0;
1242 1.1 cgd longjmp(ptabort, 1);
1243 1.1 cgd }
1244 1.1 cgd
1245 1.1 cgd proxtrans(cmd, local, remote)
1246 1.1 cgd char *cmd, *local, *remote;
1247 1.1 cgd {
1248 1.1 cgd sig_t oldintr;
1249 1.1 cgd int secndflag = 0, prox_type, nfnd;
1250 1.1 cgd extern jmp_buf ptabort;
1251 1.1 cgd char *cmd2;
1252 1.1 cgd struct fd_set mask;
1253 1.1 cgd void abortpt();
1254 1.1 cgd
1255 1.1 cgd if (strcmp(cmd, "RETR"))
1256 1.1 cgd cmd2 = "RETR";
1257 1.1 cgd else
1258 1.1 cgd cmd2 = runique ? "STOU" : "STOR";
1259 1.1 cgd if ((prox_type = type) == 0) {
1260 1.1 cgd if (unix_server && unix_proxy)
1261 1.1 cgd prox_type = TYPE_I;
1262 1.1 cgd else
1263 1.1 cgd prox_type = TYPE_A;
1264 1.1 cgd }
1265 1.1 cgd if (curtype != prox_type)
1266 1.1 cgd changetype(prox_type, 1);
1267 1.1 cgd if (command("PASV") != COMPLETE) {
1268 1.1 cgd printf("proxy server does not support third party transfers.\n");
1269 1.1 cgd return;
1270 1.1 cgd }
1271 1.1 cgd pswitch(0);
1272 1.1 cgd if (!connected) {
1273 1.1 cgd printf("No primary connection\n");
1274 1.1 cgd pswitch(1);
1275 1.1 cgd code = -1;
1276 1.1 cgd return;
1277 1.1 cgd }
1278 1.1 cgd if (curtype != prox_type)
1279 1.1 cgd changetype(prox_type, 1);
1280 1.1 cgd if (command("PORT %s", pasv) != COMPLETE) {
1281 1.1 cgd pswitch(1);
1282 1.1 cgd return;
1283 1.1 cgd }
1284 1.1 cgd if (setjmp(ptabort))
1285 1.1 cgd goto abort;
1286 1.1 cgd oldintr = signal(SIGINT, abortpt);
1287 1.1 cgd if (command("%s %s", cmd, remote) != PRELIM) {
1288 1.1 cgd (void) signal(SIGINT, oldintr);
1289 1.1 cgd pswitch(1);
1290 1.1 cgd return;
1291 1.1 cgd }
1292 1.1 cgd sleep(2);
1293 1.1 cgd pswitch(1);
1294 1.1 cgd secndflag++;
1295 1.1 cgd if (command("%s %s", cmd2, local) != PRELIM)
1296 1.1 cgd goto abort;
1297 1.1 cgd ptflag++;
1298 1.1 cgd (void) getreply(0);
1299 1.1 cgd pswitch(0);
1300 1.1 cgd (void) getreply(0);
1301 1.1 cgd (void) signal(SIGINT, oldintr);
1302 1.1 cgd pswitch(1);
1303 1.1 cgd ptflag = 0;
1304 1.1 cgd printf("local: %s remote: %s\n", local, remote);
1305 1.1 cgd return;
1306 1.1 cgd abort:
1307 1.1 cgd (void) signal(SIGINT, SIG_IGN);
1308 1.1 cgd ptflag = 0;
1309 1.1 cgd if (strcmp(cmd, "RETR") && !proxy)
1310 1.1 cgd pswitch(1);
1311 1.1 cgd else if (!strcmp(cmd, "RETR") && proxy)
1312 1.1 cgd pswitch(0);
1313 1.1 cgd if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1314 1.1 cgd if (command("%s %s", cmd2, local) != PRELIM) {
1315 1.1 cgd pswitch(0);
1316 1.1 cgd if (cpend)
1317 1.1 cgd abort_remote((FILE *) NULL);
1318 1.1 cgd }
1319 1.1 cgd pswitch(1);
1320 1.1 cgd if (ptabflg)
1321 1.1 cgd code = -1;
1322 1.1 cgd (void) signal(SIGINT, oldintr);
1323 1.1 cgd return;
1324 1.1 cgd }
1325 1.1 cgd if (cpend)
1326 1.1 cgd abort_remote((FILE *) NULL);
1327 1.1 cgd pswitch(!proxy);
1328 1.1 cgd if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1329 1.1 cgd if (command("%s %s", cmd2, local) != PRELIM) {
1330 1.1 cgd pswitch(0);
1331 1.1 cgd if (cpend)
1332 1.1 cgd abort_remote((FILE *) NULL);
1333 1.1 cgd pswitch(1);
1334 1.1 cgd if (ptabflg)
1335 1.1 cgd code = -1;
1336 1.1 cgd (void) signal(SIGINT, oldintr);
1337 1.1 cgd return;
1338 1.1 cgd }
1339 1.1 cgd }
1340 1.1 cgd if (cpend)
1341 1.1 cgd abort_remote((FILE *) NULL);
1342 1.1 cgd pswitch(!proxy);
1343 1.1 cgd if (cpend) {
1344 1.1 cgd FD_ZERO(&mask);
1345 1.1 cgd FD_SET(fileno(cin), &mask);
1346 1.1 cgd if ((nfnd = empty(&mask, 10)) <= 0) {
1347 1.1 cgd if (nfnd < 0) {
1348 1.1 cgd perror("abort");
1349 1.1 cgd }
1350 1.1 cgd if (ptabflg)
1351 1.1 cgd code = -1;
1352 1.1 cgd lostpeer();
1353 1.1 cgd }
1354 1.1 cgd (void) getreply(0);
1355 1.1 cgd (void) getreply(0);
1356 1.1 cgd }
1357 1.1 cgd if (proxy)
1358 1.1 cgd pswitch(0);
1359 1.1 cgd pswitch(1);
1360 1.1 cgd if (ptabflg)
1361 1.1 cgd code = -1;
1362 1.1 cgd (void) signal(SIGINT, oldintr);
1363 1.1 cgd }
1364 1.1 cgd
1365 1.1 cgd reset()
1366 1.1 cgd {
1367 1.1 cgd struct fd_set mask;
1368 1.1 cgd int nfnd = 1;
1369 1.1 cgd
1370 1.1 cgd FD_ZERO(&mask);
1371 1.1 cgd while (nfnd > 0) {
1372 1.1 cgd FD_SET(fileno(cin), &mask);
1373 1.1 cgd if ((nfnd = empty(&mask,0)) < 0) {
1374 1.1 cgd perror("reset");
1375 1.1 cgd code = -1;
1376 1.1 cgd lostpeer();
1377 1.1 cgd }
1378 1.1 cgd else if (nfnd) {
1379 1.1 cgd (void) getreply(0);
1380 1.1 cgd }
1381 1.1 cgd }
1382 1.1 cgd }
1383 1.1 cgd
1384 1.1 cgd char *
1385 1.1 cgd gunique(local)
1386 1.1 cgd char *local;
1387 1.1 cgd {
1388 1.1 cgd static char new[MAXPATHLEN];
1389 1.1 cgd char *cp = rindex(local, '/');
1390 1.1 cgd int d, count=0;
1391 1.1 cgd char ext = '1';
1392 1.1 cgd
1393 1.1 cgd if (cp)
1394 1.1 cgd *cp = '\0';
1395 1.1 cgd d = access(cp ? local : ".", 2);
1396 1.1 cgd if (cp)
1397 1.1 cgd *cp = '/';
1398 1.1 cgd if (d < 0) {
1399 1.1 cgd fprintf(stderr, "local: %s: %s\n", local, strerror(errno));
1400 1.1 cgd return((char *) 0);
1401 1.1 cgd }
1402 1.1 cgd (void) strcpy(new, local);
1403 1.1 cgd cp = new + strlen(new);
1404 1.1 cgd *cp++ = '.';
1405 1.1 cgd while (!d) {
1406 1.1 cgd if (++count == 100) {
1407 1.1 cgd printf("runique: can't find unique file name.\n");
1408 1.1 cgd return((char *) 0);
1409 1.1 cgd }
1410 1.1 cgd *cp++ = ext;
1411 1.1 cgd *cp = '\0';
1412 1.1 cgd if (ext == '9')
1413 1.1 cgd ext = '0';
1414 1.1 cgd else
1415 1.1 cgd ext++;
1416 1.1 cgd if ((d = access(new, 0)) < 0)
1417 1.1 cgd break;
1418 1.1 cgd if (ext != '0')
1419 1.1 cgd cp--;
1420 1.1 cgd else if (*(cp - 2) == '.')
1421 1.1 cgd *(cp - 1) = '1';
1422 1.1 cgd else {
1423 1.1 cgd *(cp - 2) = *(cp - 2) + 1;
1424 1.1 cgd cp--;
1425 1.1 cgd }
1426 1.1 cgd }
1427 1.1 cgd return(new);
1428 1.1 cgd }
1429 1.1 cgd
1430 1.1 cgd abort_remote(din)
1431 1.1 cgd FILE *din;
1432 1.1 cgd {
1433 1.1 cgd char buf[BUFSIZ];
1434 1.1 cgd int nfnd;
1435 1.1 cgd struct fd_set mask;
1436 1.1 cgd
1437 1.1 cgd /*
1438 1.1 cgd * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1439 1.1 cgd * after urgent byte rather than before as is protocol now
1440 1.1 cgd */
1441 1.1 cgd sprintf(buf, "%c%c%c", IAC, IP, IAC);
1442 1.1 cgd if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
1443 1.1 cgd perror("abort");
1444 1.1 cgd fprintf(cout,"%cABOR\r\n", DM);
1445 1.1 cgd (void) fflush(cout);
1446 1.1 cgd FD_ZERO(&mask);
1447 1.1 cgd FD_SET(fileno(cin), &mask);
1448 1.1 cgd if (din) {
1449 1.1 cgd FD_SET(fileno(din), &mask);
1450 1.1 cgd }
1451 1.1 cgd if ((nfnd = empty(&mask, 10)) <= 0) {
1452 1.1 cgd if (nfnd < 0) {
1453 1.1 cgd perror("abort");
1454 1.1 cgd }
1455 1.1 cgd if (ptabflg)
1456 1.1 cgd code = -1;
1457 1.1 cgd lostpeer();
1458 1.1 cgd }
1459 1.1 cgd if (din && FD_ISSET(fileno(din), &mask)) {
1460 1.1 cgd while (read(fileno(din), buf, BUFSIZ) > 0)
1461 1.1 cgd /* LOOP */;
1462 1.1 cgd }
1463 1.1 cgd if (getreply(0) == ERROR && code == 552) {
1464 1.1 cgd /* 552 needed for nic style abort */
1465 1.1 cgd (void) getreply(0);
1466 1.1 cgd }
1467 1.1 cgd (void) getreply(0);
1468 1.1 cgd }
1469