ftp.c revision 1.71 1 1.71 lukem /* $NetBSD: ftp.c,v 1.71 1999/09/30 06:35:13 lukem Exp $ */
2 1.48 itojun
3 1.48 itojun /*
4 1.48 itojun * Copyright (C) 1997 and 1998 WIDE Project.
5 1.48 itojun * All rights reserved.
6 1.48 itojun *
7 1.48 itojun * Redistribution and use in source and binary forms, with or without
8 1.48 itojun * modification, are permitted provided that the following conditions
9 1.48 itojun * are met:
10 1.48 itojun * 1. Redistributions of source code must retain the above copyright
11 1.48 itojun * notice, this list of conditions and the following disclaimer.
12 1.48 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.48 itojun * notice, this list of conditions and the following disclaimer in the
14 1.48 itojun * documentation and/or other materials provided with the distribution.
15 1.48 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.48 itojun * may be used to endorse or promote products derived from this software
17 1.48 itojun * without specific prior written permission.
18 1.48 itojun *
19 1.48 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.48 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.48 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.48 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.48 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.48 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.48 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.48 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.48 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.48 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.48 itojun * SUCH DAMAGE.
30 1.48 itojun */
31 1.12 tls
32 1.1 cgd /*
33 1.6 cgd * Copyright (c) 1985, 1989, 1993, 1994
34 1.6 cgd * The Regents of the University of California. All rights reserved.
35 1.1 cgd *
36 1.1 cgd * Redistribution and use in source and binary forms, with or without
37 1.1 cgd * modification, are permitted provided that the following conditions
38 1.1 cgd * are met:
39 1.1 cgd * 1. Redistributions of source code must retain the above copyright
40 1.1 cgd * notice, this list of conditions and the following disclaimer.
41 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 cgd * notice, this list of conditions and the following disclaimer in the
43 1.1 cgd * documentation and/or other materials provided with the distribution.
44 1.1 cgd * 3. All advertising materials mentioning features or use of this software
45 1.1 cgd * must display the following acknowledgement:
46 1.1 cgd * This product includes software developed by the University of
47 1.1 cgd * California, Berkeley and its contributors.
48 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
49 1.1 cgd * may be used to endorse or promote products derived from this software
50 1.1 cgd * without specific prior written permission.
51 1.1 cgd *
52 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 1.1 cgd * SUCH DAMAGE.
63 1.1 cgd */
64 1.1 cgd
65 1.26 lukem #include <sys/cdefs.h>
66 1.1 cgd #ifndef lint
67 1.12 tls #if 0
68 1.12 tls static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94";
69 1.12 tls #else
70 1.71 lukem __RCSID("$NetBSD: ftp.c,v 1.71 1999/09/30 06:35:13 lukem Exp $");
71 1.12 tls #endif
72 1.1 cgd #endif /* not lint */
73 1.1 cgd
74 1.21 lukem #include <sys/types.h>
75 1.1 cgd #include <sys/stat.h>
76 1.1 cgd #include <sys/socket.h>
77 1.33 christos #include <sys/time.h>
78 1.1 cgd
79 1.1 cgd #include <netinet/in.h>
80 1.1 cgd #include <netinet/in_systm.h>
81 1.1 cgd #include <netinet/ip.h>
82 1.6 cgd #include <arpa/inet.h>
83 1.1 cgd #include <arpa/ftp.h>
84 1.1 cgd #include <arpa/telnet.h>
85 1.1 cgd
86 1.6 cgd #include <ctype.h>
87 1.6 cgd #include <err.h>
88 1.1 cgd #include <errno.h>
89 1.1 cgd #include <netdb.h>
90 1.6 cgd #include <stdio.h>
91 1.6 cgd #include <stdlib.h>
92 1.6 cgd #include <string.h>
93 1.32 kleink #include <time.h>
94 1.6 cgd #include <unistd.h>
95 1.23 lukem #ifdef __STDC__
96 1.23 lukem #include <stdarg.h>
97 1.23 lukem #else
98 1.1 cgd #include <varargs.h>
99 1.23 lukem #endif
100 1.45 christos #ifndef __USE_SELECT
101 1.45 christos #include <poll.h>
102 1.45 christos #endif
103 1.1 cgd
104 1.1 cgd #include "ftp_var.h"
105 1.48 itojun
106 1.1 cgd int data = -1;
107 1.1 cgd int abrtflag = 0;
108 1.6 cgd jmp_buf ptabort;
109 1.6 cgd int ptabflg;
110 1.1 cgd int ptflag = 0;
111 1.48 itojun off_t restart_point = 0;
112 1.1 cgd
113 1.45 christos static int empty __P((FILE *, FILE *, int));
114 1.45 christos
115 1.48 itojun union sockunion {
116 1.48 itojun struct sockinet {
117 1.52 christos #ifdef BSD4_4
118 1.48 itojun u_char si_len;
119 1.48 itojun u_char si_family;
120 1.52 christos #else
121 1.52 christos u_short si_family;
122 1.52 christos #endif
123 1.48 itojun u_short si_port;
124 1.52 christos #ifndef BSD4_4
125 1.62 lukem u_char si_pad[
126 1.62 lukem #ifdef INET6
127 1.62 lukem sizeof(struct sockaddr_in6)
128 1.62 lukem #else
129 1.62 lukem sizeof(struct sockaddr_in)
130 1.62 lukem #endif
131 1.62 lukem - sizeof(u_int)];
132 1.52 christos u_char si_len;
133 1.52 christos #endif
134 1.48 itojun } su_si;
135 1.48 itojun struct sockaddr_in su_sin;
136 1.62 lukem #ifdef INET6
137 1.48 itojun struct sockaddr_in6 su_sin6;
138 1.62 lukem #endif
139 1.48 itojun };
140 1.52 christos
141 1.48 itojun #define su_len su_si.si_len
142 1.48 itojun #define su_family su_si.si_family
143 1.48 itojun #define su_port su_si.si_port
144 1.48 itojun
145 1.48 itojun union sockunion myctladdr, hisctladdr, data_addr;
146 1.1 cgd
147 1.1 cgd FILE *cin, *cout;
148 1.1 cgd
149 1.1 cgd char *
150 1.1 cgd hookup(host, port)
151 1.48 itojun char *host;
152 1.48 itojun char *port;
153 1.1 cgd {
154 1.58 christos int s = -1, len, error;
155 1.58 christos #ifdef NI_NUMERICHOST
156 1.49 itojun struct addrinfo hints, *res, *res0;
157 1.58 christos char hbuf[MAXHOSTNAMELEN];
158 1.58 christos #else
159 1.58 christos struct hostent *hp = NULL;
160 1.58 christos struct servent *sp = NULL;
161 1.58 christos char **ptr;
162 1.58 christos struct sockaddr_in sin;
163 1.58 christos #endif
164 1.14 lukem static char hostnamebuf[MAXHOSTNAMELEN];
165 1.49 itojun char *cause = "unknown";
166 1.58 christos int family;
167 1.48 itojun
168 1.58 christos #ifdef NI_NUMERICHOST
169 1.48 itojun memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
170 1.48 itojun memset(&hints, 0, sizeof(hints));
171 1.48 itojun hints.ai_flags = AI_CANONNAME;
172 1.48 itojun hints.ai_family = AF_UNSPEC;
173 1.48 itojun hints.ai_socktype = SOCK_STREAM;
174 1.48 itojun hints.ai_protocol = 0;
175 1.49 itojun error = getaddrinfo(host, port, &hints, &res0);
176 1.48 itojun if (error) {
177 1.61 mycroft warnx(gai_strerror(error));
178 1.48 itojun code = -1;
179 1.48 itojun return (0);
180 1.48 itojun }
181 1.1 cgd
182 1.49 itojun if (res0->ai_canonname)
183 1.68 lukem (void)strlcpy(hostnamebuf, res0->ai_canonname,
184 1.68 lukem sizeof(hostnamebuf));
185 1.48 itojun else
186 1.68 lukem (void)strlcpy(hostnamebuf, host, sizeof(hostnamebuf));
187 1.1 cgd hostname = hostnamebuf;
188 1.48 itojun
189 1.49 itojun for (res = res0; res; res = res->ai_next) {
190 1.49 itojun #if 0 /*old behavior*/
191 1.49 itojun if (res != res0) /* not on the first address */
192 1.49 itojun #else
193 1.49 itojun if (res0->ai_next) /* if we have multiple possibilities */
194 1.49 itojun #endif
195 1.49 itojun {
196 1.48 itojun getnameinfo(res->ai_addr, res->ai_addrlen,
197 1.48 itojun hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST);
198 1.49 itojun fprintf(ttyout, "Trying %s...\n", hbuf);
199 1.49 itojun }
200 1.49 itojun s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
201 1.49 itojun if (s < 0) {
202 1.49 itojun cause = "socket";
203 1.49 itojun continue;
204 1.49 itojun }
205 1.49 itojun while ((error = xconnect(s, res->ai_addr, res->ai_addrlen)) < 0
206 1.49 itojun && errno == EINTR) {
207 1.49 itojun ;
208 1.48 itojun }
209 1.49 itojun if (error) {
210 1.49 itojun /* this "if" clause is to prevent print warning twice */
211 1.49 itojun if (res->ai_next) {
212 1.49 itojun getnameinfo(res->ai_addr, res->ai_addrlen,
213 1.49 itojun hbuf, sizeof(hbuf), NULL, 0,
214 1.49 itojun NI_NUMERICHOST);
215 1.49 itojun warn("connect to address %s", hbuf);
216 1.1 cgd }
217 1.49 itojun cause = "connect";
218 1.49 itojun close(s);
219 1.49 itojun s = -1;
220 1.1 cgd continue;
221 1.1 cgd }
222 1.49 itojun
223 1.49 itojun /* finally we got one */
224 1.49 itojun break;
225 1.49 itojun }
226 1.49 itojun if (s < 0) {
227 1.49 itojun warn(cause);
228 1.1 cgd code = -1;
229 1.50 itojun freeaddrinfo(res0);
230 1.49 itojun return 0;
231 1.1 cgd }
232 1.48 itojun memcpy(&hisctladdr, res->ai_addr, res->ai_addrlen);
233 1.48 itojun len = res->ai_addrlen;
234 1.49 itojun freeaddrinfo(res0);
235 1.49 itojun res0 = res = NULL;
236 1.58 christos family = hisctladdr.su_family;
237 1.58 christos #else
238 1.58 christos memset(&sin, 0, sizeof(sin));
239 1.58 christos sin.sin_family = AF_INET;
240 1.58 christos if ((hp = gethostbyname(host)) == NULL) {
241 1.58 christos warnx("%s: %s", host, hstrerror(h_errno));
242 1.58 christos code = -1;
243 1.58 christos return 0;
244 1.58 christos }
245 1.58 christos
246 1.58 christos if ((sp = getservbyname(port, "tcp")) == NULL) {
247 1.58 christos sin.sin_port = htons(21);
248 1.58 christos }
249 1.58 christos else
250 1.58 christos sin.sin_port = sp->s_port;
251 1.58 christos
252 1.68 lukem (void)strlcpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
253 1.58 christos hostname = hostnamebuf;
254 1.58 christos
255 1.58 christos if (hp->h_length > sizeof(sin.sin_addr))
256 1.58 christos hp->h_length = sizeof(sin.sin_addr);
257 1.58 christos
258 1.58 christos for (ptr = hp->h_addr_list; *ptr; ptr++) {
259 1.58 christos memcpy(&sin.sin_addr, *ptr, (size_t)hp->h_length);
260 1.58 christos if (hp->h_addr_list[1])
261 1.58 christos fprintf(ttyout, "Trying %s...\n",
262 1.58 christos inet_ntoa(sin.sin_addr));
263 1.58 christos s = socket(AF_INET, SOCK_STREAM, 0);
264 1.58 christos if (s < 0) {
265 1.58 christos cause = "socket";
266 1.58 christos continue;
267 1.58 christos }
268 1.58 christos while ((error = xconnect(s, (struct sockaddr *)&sin,
269 1.58 christos sizeof(sin))) < 0 && errno == EINTR) {
270 1.58 christos ;
271 1.58 christos }
272 1.58 christos if (error) {
273 1.58 christos /* this "if" clause is to prevent print warning twice */
274 1.58 christos if (hp->h_addr_list[1]) {
275 1.58 christos warn("connect to address %s",
276 1.58 christos inet_ntoa(sin.sin_addr));
277 1.58 christos }
278 1.58 christos cause = "connect";
279 1.58 christos close(s);
280 1.58 christos s = -1;
281 1.58 christos continue;
282 1.58 christos }
283 1.58 christos
284 1.58 christos /* finally we got one */
285 1.58 christos break;
286 1.58 christos }
287 1.58 christos if (s < 0) {
288 1.58 christos warn(cause);
289 1.58 christos code = -1;
290 1.58 christos return 0;
291 1.58 christos }
292 1.58 christos memcpy(&hisctladdr, &sin, sizeof(sin));
293 1.58 christos len = sizeof(sin);
294 1.58 christos if (hisctladdr.su_len == 0)
295 1.58 christos hisctladdr.su_len = len;
296 1.58 christos family = AF_INET;
297 1.58 christos #endif
298 1.58 christos
299 1.1 cgd if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
300 1.6 cgd warn("getsockname");
301 1.1 cgd code = -1;
302 1.1 cgd goto bad;
303 1.1 cgd }
304 1.58 christos if (myctladdr.su_len == 0)
305 1.58 christos myctladdr.su_len = len;
306 1.58 christos
307 1.48 itojun #if defined(IPPROTO_IP) && defined(IP_TOS)
308 1.58 christos if (family == AF_INET) {
309 1.53 christos int tos = IPTOS_LOWDELAY;
310 1.48 itojun if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
311 1.48 itojun sizeof(int)) < 0)
312 1.48 itojun warn("setsockopt TOS (ignored)");
313 1.48 itojun }
314 1.1 cgd #endif
315 1.1 cgd cin = fdopen(s, "r");
316 1.1 cgd cout = fdopen(s, "w");
317 1.1 cgd if (cin == NULL || cout == NULL) {
318 1.6 cgd warnx("fdopen failed.");
319 1.1 cgd if (cin)
320 1.23 lukem (void)fclose(cin);
321 1.1 cgd if (cout)
322 1.23 lukem (void)fclose(cout);
323 1.1 cgd code = -1;
324 1.1 cgd goto bad;
325 1.1 cgd }
326 1.1 cgd if (verbose)
327 1.35 lukem fprintf(ttyout, "Connected to %s.\n", hostname);
328 1.1 cgd if (getreply(0) > 2) { /* read startup message from server */
329 1.1 cgd if (cin)
330 1.23 lukem (void)fclose(cin);
331 1.1 cgd if (cout)
332 1.23 lukem (void)fclose(cout);
333 1.1 cgd code = -1;
334 1.1 cgd goto bad;
335 1.1 cgd }
336 1.1 cgd #ifdef SO_OOBINLINE
337 1.1 cgd {
338 1.1 cgd int on = 1;
339 1.1 cgd
340 1.1 cgd if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
341 1.1 cgd < 0 && debug) {
342 1.6 cgd warn("setsockopt");
343 1.1 cgd }
344 1.1 cgd }
345 1.1 cgd #endif /* SO_OOBINLINE */
346 1.1 cgd
347 1.1 cgd return (hostname);
348 1.1 cgd bad:
349 1.23 lukem (void)close(s);
350 1.31 lukem return (NULL);
351 1.1 cgd }
352 1.1 cgd
353 1.1 cgd void
354 1.23 lukem cmdabort(notused)
355 1.23 lukem int notused;
356 1.1 cgd {
357 1.1 cgd
358 1.22 lukem alarmtimer(0);
359 1.35 lukem putc('\n', ttyout);
360 1.1 cgd abrtflag++;
361 1.1 cgd if (ptflag)
362 1.15 lukem longjmp(ptabort, 1);
363 1.1 cgd }
364 1.1 cgd
365 1.1 cgd /*VARARGS*/
366 1.6 cgd int
367 1.23 lukem #ifdef __STDC__
368 1.23 lukem command(const char *fmt, ...)
369 1.23 lukem #else
370 1.1 cgd command(va_alist)
371 1.23 lukem va_dcl
372 1.23 lukem #endif
373 1.1 cgd {
374 1.1 cgd va_list ap;
375 1.1 cgd int r;
376 1.1 cgd sig_t oldintr;
377 1.23 lukem #ifndef __STDC__
378 1.23 lukem const char *fmt;
379 1.23 lukem #endif
380 1.1 cgd
381 1.1 cgd abrtflag = 0;
382 1.1 cgd if (debug) {
383 1.35 lukem fputs("---> ", ttyout);
384 1.23 lukem #ifdef __STDC__
385 1.23 lukem va_start(ap, fmt);
386 1.23 lukem #else
387 1.1 cgd va_start(ap);
388 1.23 lukem fmt = va_arg(ap, const char *);
389 1.23 lukem #endif
390 1.1 cgd if (strncmp("PASS ", fmt, 5) == 0)
391 1.35 lukem fputs("PASS XXXX", ttyout);
392 1.14 lukem else if (strncmp("ACCT ", fmt, 5) == 0)
393 1.35 lukem fputs("ACCT XXXX", ttyout);
394 1.14 lukem else
395 1.35 lukem vfprintf(ttyout, fmt, ap);
396 1.1 cgd va_end(ap);
397 1.35 lukem putc('\n', ttyout);
398 1.1 cgd }
399 1.1 cgd if (cout == NULL) {
400 1.23 lukem warnx("No control connection for command.");
401 1.1 cgd code = -1;
402 1.1 cgd return (0);
403 1.1 cgd }
404 1.67 lukem oldintr = xsignal(SIGINT, cmdabort);
405 1.23 lukem #ifdef __STDC__
406 1.23 lukem va_start(ap, fmt);
407 1.23 lukem #else
408 1.1 cgd va_start(ap);
409 1.1 cgd fmt = va_arg(ap, char *);
410 1.23 lukem #endif
411 1.1 cgd vfprintf(cout, fmt, ap);
412 1.1 cgd va_end(ap);
413 1.23 lukem fputs("\r\n", cout);
414 1.23 lukem (void)fflush(cout);
415 1.1 cgd cpend = 1;
416 1.1 cgd r = getreply(!strcmp(fmt, "QUIT"));
417 1.1 cgd if (abrtflag && oldintr != SIG_IGN)
418 1.1 cgd (*oldintr)(SIGINT);
419 1.67 lukem (void)xsignal(SIGINT, oldintr);
420 1.6 cgd return (r);
421 1.1 cgd }
422 1.1 cgd
423 1.16 lukem char reply_string[BUFSIZ]; /* first line of previous reply */
424 1.1 cgd
425 1.6 cgd int
426 1.1 cgd getreply(expecteof)
427 1.1 cgd int expecteof;
428 1.1 cgd {
429 1.16 lukem char current_line[BUFSIZ]; /* last line of previous reply */
430 1.16 lukem int c, n, line;
431 1.6 cgd int dig;
432 1.1 cgd int originalcode = 0, continuation = 0;
433 1.1 cgd sig_t oldintr;
434 1.1 cgd int pflag = 0;
435 1.6 cgd char *cp, *pt = pasv;
436 1.1 cgd
437 1.67 lukem oldintr = xsignal(SIGINT, cmdabort);
438 1.16 lukem for (line = 0 ;; line++) {
439 1.1 cgd dig = n = code = 0;
440 1.16 lukem cp = current_line;
441 1.1 cgd while ((c = getc(cin)) != '\n') {
442 1.1 cgd if (c == IAC) { /* handle telnet commands */
443 1.1 cgd switch (c = getc(cin)) {
444 1.1 cgd case WILL:
445 1.1 cgd case WONT:
446 1.1 cgd c = getc(cin);
447 1.1 cgd fprintf(cout, "%c%c%c", IAC, DONT, c);
448 1.23 lukem (void)fflush(cout);
449 1.1 cgd break;
450 1.1 cgd case DO:
451 1.1 cgd case DONT:
452 1.1 cgd c = getc(cin);
453 1.1 cgd fprintf(cout, "%c%c%c", IAC, WONT, c);
454 1.23 lukem (void)fflush(cout);
455 1.1 cgd break;
456 1.1 cgd default:
457 1.1 cgd break;
458 1.1 cgd }
459 1.1 cgd continue;
460 1.1 cgd }
461 1.1 cgd dig++;
462 1.1 cgd if (c == EOF) {
463 1.1 cgd if (expecteof) {
464 1.67 lukem (void)xsignal(SIGINT, oldintr);
465 1.1 cgd code = 221;
466 1.1 cgd return (0);
467 1.1 cgd }
468 1.1 cgd lostpeer();
469 1.1 cgd if (verbose) {
470 1.35 lukem fputs(
471 1.35 lukem "421 Service not available, remote server has closed connection.\n",
472 1.35 lukem ttyout);
473 1.1 cgd }
474 1.1 cgd code = 421;
475 1.6 cgd return (4);
476 1.1 cgd }
477 1.1 cgd if (c != '\r' && (verbose > 0 ||
478 1.35 lukem ((verbose > -1 && n == '5' && dig > 4) &&
479 1.35 lukem (((!n && c < '5') || (n && n < '5'))
480 1.35 lukem || !retry_connect)))) {
481 1.1 cgd if (proxflag &&
482 1.16 lukem (dig == 1 || (dig == 5 && verbose == 0)))
483 1.35 lukem fprintf(ttyout, "%s:", hostname);
484 1.35 lukem (void)putc(c, ttyout);
485 1.1 cgd }
486 1.1 cgd if (dig < 4 && isdigit(c))
487 1.1 cgd code = code * 10 + (c - '0');
488 1.48 itojun if (!pflag && (code == 227 || code == 228))
489 1.1 cgd pflag = 1;
490 1.48 itojun else if (!pflag && code == 229)
491 1.48 itojun pflag = 100;
492 1.1 cgd if (dig > 4 && pflag == 1 && isdigit(c))
493 1.1 cgd pflag = 2;
494 1.1 cgd if (pflag == 2) {
495 1.1 cgd if (c != '\r' && c != ')')
496 1.1 cgd *pt++ = c;
497 1.1 cgd else {
498 1.1 cgd *pt = '\0';
499 1.1 cgd pflag = 3;
500 1.1 cgd }
501 1.1 cgd }
502 1.48 itojun if (pflag == 100 && c == '(')
503 1.48 itojun pflag = 2;
504 1.1 cgd if (dig == 4 && c == '-') {
505 1.1 cgd if (continuation)
506 1.1 cgd code = 0;
507 1.1 cgd continuation++;
508 1.1 cgd }
509 1.1 cgd if (n == 0)
510 1.1 cgd n = c;
511 1.16 lukem if (cp < ¤t_line[sizeof(current_line) - 1])
512 1.1 cgd *cp++ = c;
513 1.1 cgd }
514 1.35 lukem if (verbose > 0 || ((verbose > -1 && n == '5') &&
515 1.35 lukem (n < '5' || !retry_connect))) {
516 1.35 lukem (void)putc(c, ttyout);
517 1.35 lukem (void)fflush (ttyout);
518 1.1 cgd }
519 1.18 christos if (line == 0) {
520 1.18 christos size_t len = cp - current_line;
521 1.18 christos
522 1.18 christos if (len > sizeof(reply_string))
523 1.18 christos len = sizeof(reply_string);
524 1.18 christos
525 1.68 lukem (void)strlcpy(reply_string, current_line, len);
526 1.18 christos }
527 1.1 cgd if (continuation && code != originalcode) {
528 1.1 cgd if (originalcode == 0)
529 1.1 cgd originalcode = code;
530 1.1 cgd continue;
531 1.1 cgd }
532 1.1 cgd *cp = '\0';
533 1.1 cgd if (n != '1')
534 1.1 cgd cpend = 0;
535 1.67 lukem (void)xsignal(SIGINT, oldintr);
536 1.1 cgd if (code == 421 || originalcode == 421)
537 1.1 cgd lostpeer();
538 1.1 cgd if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
539 1.1 cgd (*oldintr)(SIGINT);
540 1.1 cgd return (n - '0');
541 1.1 cgd }
542 1.1 cgd }
543 1.1 cgd
544 1.45 christos static int
545 1.45 christos empty(cin, din, sec)
546 1.45 christos FILE *cin;
547 1.45 christos FILE *din;
548 1.1 cgd int sec;
549 1.1 cgd {
550 1.45 christos int nr;
551 1.45 christos int nfd = 0;
552 1.45 christos
553 1.45 christos #ifdef __USE_SELECT
554 1.1 cgd struct timeval t;
555 1.45 christos fd_set rmask;
556 1.1 cgd
557 1.71 lukem FD_ZERO(&rmask);
558 1.45 christos if (cin) {
559 1.45 christos if (nfd < fileno(cin))
560 1.45 christos nfd = fileno(cin);
561 1.45 christos FD_SET(fileno(cin), &rmask);
562 1.45 christos }
563 1.45 christos if (din) {
564 1.45 christos if (nfd < fileno(din))
565 1.45 christos nfd = fileno(din);
566 1.45 christos FD_SET(fileno(din), &rmask);
567 1.45 christos }
568 1.45 christos
569 1.1 cgd t.tv_sec = (long) sec;
570 1.1 cgd t.tv_usec = 0;
571 1.45 christos if ((nr = select(nfd, &rmask, NULL, NULL, &t)) <= 0)
572 1.45 christos return nr;
573 1.45 christos
574 1.45 christos nr = 0;
575 1.45 christos if (cin)
576 1.45 christos nr |= FD_ISSET(fileno(cin), &rmask) ? 1 : 0;
577 1.45 christos if (din)
578 1.45 christos nr |= FD_ISSET(fileno(din), &rmask) ? 2 : 0;
579 1.45 christos
580 1.45 christos #else
581 1.45 christos struct pollfd pfd[2];
582 1.45 christos
583 1.45 christos if (cin) {
584 1.45 christos pfd[nfd].fd = fileno(cin);
585 1.45 christos pfd[nfd++].events = POLLIN;
586 1.45 christos }
587 1.45 christos
588 1.45 christos if (din) {
589 1.45 christos pfd[nfd].fd = fileno(din);
590 1.45 christos pfd[nfd++].events = POLLIN;
591 1.45 christos }
592 1.45 christos
593 1.45 christos if ((nr = poll(pfd, nfd, sec * 1000)) <= 0)
594 1.45 christos return nr;
595 1.45 christos
596 1.45 christos nr = 0;
597 1.45 christos nfd = 0;
598 1.45 christos if (cin)
599 1.45 christos nr |= (pfd[nfd++].revents & POLLIN) ? 1 : 0;
600 1.45 christos if (din)
601 1.45 christos nr |= (pfd[nfd++].revents & POLLIN) ? 2 : 0;
602 1.45 christos #endif
603 1.45 christos return nr;
604 1.1 cgd }
605 1.1 cgd
606 1.1 cgd jmp_buf sendabort;
607 1.1 cgd
608 1.1 cgd void
609 1.23 lukem abortsend(notused)
610 1.23 lukem int notused;
611 1.1 cgd {
612 1.1 cgd
613 1.22 lukem alarmtimer(0);
614 1.1 cgd mflag = 0;
615 1.1 cgd abrtflag = 0;
616 1.35 lukem fputs("\nsend aborted\nwaiting for remote to finish abort.\n", ttyout);
617 1.1 cgd longjmp(sendabort, 1);
618 1.1 cgd }
619 1.1 cgd
620 1.6 cgd void
621 1.1 cgd sendrequest(cmd, local, remote, printnames)
622 1.15 lukem const char *cmd, *local, *remote;
623 1.1 cgd int printnames;
624 1.1 cgd {
625 1.1 cgd struct stat st;
626 1.6 cgd int c, d;
627 1.26 lukem FILE *fin, *dout;
628 1.6 cgd int (*closefunc) __P((FILE *));
629 1.14 lukem sig_t oldinti, oldintr, oldintp;
630 1.26 lukem volatile off_t hashbytes;
631 1.64 lukem char *lmode, *bufp;
632 1.64 lukem static size_t bufsize;
633 1.64 lukem static char *buf;
634 1.23 lukem int oprogress;
635 1.1 cgd
636 1.35 lukem #ifdef __GNUC__ /* to shut up gcc warnings */
637 1.26 lukem (void)&fin;
638 1.26 lukem (void)&dout;
639 1.26 lukem (void)&closefunc;
640 1.26 lukem (void)&oldinti;
641 1.26 lukem (void)&oldintr;
642 1.26 lukem (void)&oldintp;
643 1.26 lukem (void)&lmode;
644 1.26 lukem #endif
645 1.26 lukem
646 1.16 lukem hashbytes = mark;
647 1.14 lukem direction = "sent";
648 1.26 lukem dout = NULL;
649 1.14 lukem bytes = 0;
650 1.16 lukem filesize = -1;
651 1.23 lukem oprogress = progress;
652 1.1 cgd if (verbose && printnames) {
653 1.1 cgd if (local && *local != '-')
654 1.35 lukem fprintf(ttyout, "local: %s ", local);
655 1.1 cgd if (remote)
656 1.35 lukem fprintf(ttyout, "remote: %s\n", remote);
657 1.1 cgd }
658 1.1 cgd if (proxy) {
659 1.1 cgd proxtrans(cmd, local, remote);
660 1.1 cgd return;
661 1.1 cgd }
662 1.1 cgd if (curtype != type)
663 1.1 cgd changetype(type, 0);
664 1.1 cgd closefunc = NULL;
665 1.1 cgd oldintr = NULL;
666 1.1 cgd oldintp = NULL;
667 1.14 lukem oldinti = NULL;
668 1.1 cgd lmode = "w";
669 1.1 cgd if (setjmp(sendabort)) {
670 1.1 cgd while (cpend) {
671 1.23 lukem (void)getreply(0);
672 1.1 cgd }
673 1.1 cgd if (data >= 0) {
674 1.23 lukem (void)close(data);
675 1.1 cgd data = -1;
676 1.1 cgd }
677 1.1 cgd if (oldintr)
678 1.67 lukem (void)xsignal(SIGINT, oldintr);
679 1.1 cgd if (oldintp)
680 1.67 lukem (void)xsignal(SIGPIPE, oldintp);
681 1.14 lukem if (oldinti)
682 1.38 lukem (void)xsignal(SIGINFO, oldinti);
683 1.1 cgd code = -1;
684 1.30 lukem goto cleanupsend;
685 1.1 cgd }
686 1.67 lukem oldintr = xsignal(SIGINT, abortsend);
687 1.38 lukem oldinti = xsignal(SIGINFO, psummary);
688 1.23 lukem if (strcmp(local, "-") == 0) {
689 1.1 cgd fin = stdin;
690 1.23 lukem progress = 0;
691 1.23 lukem } else if (*local == '|') {
692 1.67 lukem oldintp = xsignal(SIGPIPE, SIG_IGN);
693 1.1 cgd fin = popen(local + 1, "r");
694 1.1 cgd if (fin == NULL) {
695 1.6 cgd warn("%s", local + 1);
696 1.67 lukem (void)xsignal(SIGINT, oldintr);
697 1.67 lukem (void)xsignal(SIGPIPE, oldintp);
698 1.38 lukem (void)xsignal(SIGINFO, oldinti);
699 1.1 cgd code = -1;
700 1.30 lukem goto cleanupsend;
701 1.1 cgd }
702 1.23 lukem progress = 0;
703 1.1 cgd closefunc = pclose;
704 1.1 cgd } else {
705 1.1 cgd fin = fopen(local, "r");
706 1.1 cgd if (fin == NULL) {
707 1.6 cgd warn("local: %s", local);
708 1.67 lukem (void)xsignal(SIGINT, oldintr);
709 1.38 lukem (void)xsignal(SIGINFO, oldinti);
710 1.1 cgd code = -1;
711 1.30 lukem goto cleanupsend;
712 1.1 cgd }
713 1.1 cgd closefunc = fclose;
714 1.29 mycroft if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) {
715 1.35 lukem fprintf(ttyout, "%s: not a plain file.\n", local);
716 1.67 lukem (void)xsignal(SIGINT, oldintr);
717 1.38 lukem (void)xsignal(SIGINFO, oldinti);
718 1.1 cgd fclose(fin);
719 1.1 cgd code = -1;
720 1.30 lukem goto cleanupsend;
721 1.1 cgd }
722 1.16 lukem filesize = st.st_size;
723 1.1 cgd }
724 1.1 cgd if (initconn()) {
725 1.67 lukem (void)xsignal(SIGINT, oldintr);
726 1.38 lukem (void)xsignal(SIGINFO, oldinti);
727 1.1 cgd if (oldintp)
728 1.67 lukem (void)xsignal(SIGPIPE, oldintp);
729 1.1 cgd code = -1;
730 1.1 cgd if (closefunc != NULL)
731 1.1 cgd (*closefunc)(fin);
732 1.30 lukem goto cleanupsend;
733 1.1 cgd }
734 1.1 cgd if (setjmp(sendabort))
735 1.1 cgd goto abort;
736 1.1 cgd
737 1.1 cgd if (restart_point &&
738 1.1 cgd (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
739 1.6 cgd int rc;
740 1.6 cgd
741 1.21 lukem rc = -1;
742 1.6 cgd switch (curtype) {
743 1.6 cgd case TYPE_A:
744 1.6 cgd rc = fseek(fin, (long) restart_point, SEEK_SET);
745 1.6 cgd break;
746 1.6 cgd case TYPE_I:
747 1.6 cgd case TYPE_L:
748 1.6 cgd rc = lseek(fileno(fin), restart_point, SEEK_SET);
749 1.6 cgd break;
750 1.6 cgd }
751 1.6 cgd if (rc < 0) {
752 1.6 cgd warn("local: %s", local);
753 1.1 cgd if (closefunc != NULL)
754 1.1 cgd (*closefunc)(fin);
755 1.30 lukem goto cleanupsend;
756 1.1 cgd }
757 1.33 christos #ifndef NO_QUAD
758 1.66 lukem if (command("REST %lld", (long long) restart_point) !=
759 1.33 christos #else
760 1.33 christos if (command("REST %ld", (long) restart_point) !=
761 1.33 christos #endif
762 1.30 lukem CONTINUE) {
763 1.1 cgd if (closefunc != NULL)
764 1.1 cgd (*closefunc)(fin);
765 1.30 lukem goto cleanupsend;
766 1.1 cgd }
767 1.1 cgd lmode = "r+w";
768 1.1 cgd }
769 1.1 cgd if (remote) {
770 1.1 cgd if (command("%s %s", cmd, remote) != PRELIM) {
771 1.67 lukem (void)xsignal(SIGINT, oldintr);
772 1.38 lukem (void)xsignal(SIGINFO, oldinti);
773 1.1 cgd if (oldintp)
774 1.67 lukem (void)xsignal(SIGPIPE, oldintp);
775 1.1 cgd if (closefunc != NULL)
776 1.1 cgd (*closefunc)(fin);
777 1.30 lukem goto cleanupsend;
778 1.1 cgd }
779 1.1 cgd } else
780 1.1 cgd if (command("%s", cmd) != PRELIM) {
781 1.67 lukem (void)xsignal(SIGINT, oldintr);
782 1.38 lukem (void)xsignal(SIGINFO, oldinti);
783 1.1 cgd if (oldintp)
784 1.67 lukem (void)xsignal(SIGPIPE, oldintp);
785 1.1 cgd if (closefunc != NULL)
786 1.1 cgd (*closefunc)(fin);
787 1.30 lukem goto cleanupsend;
788 1.1 cgd }
789 1.1 cgd dout = dataconn(lmode);
790 1.1 cgd if (dout == NULL)
791 1.1 cgd goto abort;
792 1.64 lukem
793 1.64 lukem if (sndbuf_size > bufsize) {
794 1.64 lukem if (buf)
795 1.64 lukem (void)free(buf);
796 1.64 lukem bufsize = sndbuf_size;
797 1.64 lukem buf = xmalloc(bufsize);
798 1.64 lukem }
799 1.64 lukem
800 1.16 lukem progressmeter(-1);
801 1.67 lukem oldintp = xsignal(SIGPIPE, SIG_IGN);
802 1.64 lukem
803 1.1 cgd switch (curtype) {
804 1.1 cgd
805 1.1 cgd case TYPE_I:
806 1.1 cgd case TYPE_L:
807 1.46 lukem while (1) {
808 1.46 lukem struct timeval then, now, td;
809 1.64 lukem off_t bufrem;
810 1.46 lukem
811 1.62 lukem if (rate_put)
812 1.62 lukem (void)gettimeofday(&then, NULL);
813 1.46 lukem errno = c = d = 0;
814 1.46 lukem bufrem = rate_put ? rate_put : bufsize;
815 1.46 lukem while (bufrem > 0) {
816 1.46 lukem if ((c = read(fileno(fin), buf,
817 1.46 lukem MIN(bufsize, bufrem))) <= 0)
818 1.46 lukem goto senddone;
819 1.46 lukem bytes += c;
820 1.46 lukem bufrem -= c;
821 1.46 lukem for (bufp = buf; c > 0; c -= d, bufp += d)
822 1.46 lukem if ((d = write(fileno(dout), bufp, c))
823 1.46 lukem <= 0)
824 1.46 lukem break;
825 1.46 lukem if (d < 0)
826 1.46 lukem goto senddone;
827 1.46 lukem if (hash && (!progress || filesize < 0) ) {
828 1.46 lukem while (bytes >= hashbytes) {
829 1.46 lukem (void)putc('#', ttyout);
830 1.46 lukem hashbytes += mark;
831 1.46 lukem }
832 1.46 lukem (void)fflush(ttyout);
833 1.46 lukem }
834 1.46 lukem }
835 1.46 lukem if (rate_put) {
836 1.46 lukem while (1) {
837 1.46 lukem (void)gettimeofday(&now, NULL);
838 1.46 lukem timersub(&now, &then, &td);
839 1.46 lukem if (td.tv_sec > 0)
840 1.46 lukem break;
841 1.46 lukem usleep(1000000 - td.tv_usec);
842 1.1 cgd }
843 1.1 cgd }
844 1.1 cgd }
845 1.46 lukem senddone:
846 1.22 lukem if (hash && (!progress || filesize < 0) && bytes > 0) {
847 1.14 lukem if (bytes < mark)
848 1.35 lukem (void)putc('#', ttyout);
849 1.35 lukem (void)putc('\n', ttyout);
850 1.1 cgd }
851 1.1 cgd if (c < 0)
852 1.6 cgd warn("local: %s", local);
853 1.1 cgd if (d < 0) {
854 1.14 lukem if (errno != EPIPE)
855 1.6 cgd warn("netout");
856 1.1 cgd bytes = -1;
857 1.1 cgd }
858 1.1 cgd break;
859 1.1 cgd
860 1.1 cgd case TYPE_A:
861 1.1 cgd while ((c = getc(fin)) != EOF) {
862 1.1 cgd if (c == '\n') {
863 1.22 lukem while (hash && (!progress || filesize < 0) &&
864 1.16 lukem (bytes >= hashbytes)) {
865 1.35 lukem (void)putc('#', ttyout);
866 1.35 lukem (void)fflush(ttyout);
867 1.14 lukem hashbytes += mark;
868 1.1 cgd }
869 1.1 cgd if (ferror(dout))
870 1.1 cgd break;
871 1.23 lukem (void)putc('\r', dout);
872 1.1 cgd bytes++;
873 1.1 cgd }
874 1.23 lukem (void)putc(c, dout);
875 1.1 cgd bytes++;
876 1.16 lukem #if 0 /* this violates RFC */
877 1.16 lukem if (c == '\r') {
878 1.16 lukem (void)putc('\0', dout);
879 1.16 lukem bytes++;
880 1.16 lukem }
881 1.16 lukem #endif
882 1.1 cgd }
883 1.22 lukem if (hash && (!progress || filesize < 0)) {
884 1.1 cgd if (bytes < hashbytes)
885 1.35 lukem (void)putc('#', ttyout);
886 1.35 lukem (void)putc('\n', ttyout);
887 1.1 cgd }
888 1.1 cgd if (ferror(fin))
889 1.6 cgd warn("local: %s", local);
890 1.1 cgd if (ferror(dout)) {
891 1.1 cgd if (errno != EPIPE)
892 1.6 cgd warn("netout");
893 1.1 cgd bytes = -1;
894 1.1 cgd }
895 1.1 cgd break;
896 1.1 cgd }
897 1.22 lukem progressmeter(1);
898 1.1 cgd if (closefunc != NULL)
899 1.1 cgd (*closefunc)(fin);
900 1.23 lukem (void)fclose(dout);
901 1.23 lukem (void)getreply(0);
902 1.67 lukem (void)xsignal(SIGINT, oldintr);
903 1.38 lukem (void)xsignal(SIGINFO, oldinti);
904 1.1 cgd if (oldintp)
905 1.67 lukem (void)xsignal(SIGPIPE, oldintp);
906 1.1 cgd if (bytes > 0)
907 1.16 lukem ptransfer(0);
908 1.30 lukem goto cleanupsend;
909 1.1 cgd abort:
910 1.67 lukem (void)xsignal(SIGINT, oldintr);
911 1.38 lukem (void)xsignal(SIGINFO, oldinti);
912 1.1 cgd if (oldintp)
913 1.67 lukem (void)xsignal(SIGPIPE, oldintp);
914 1.1 cgd if (!cpend) {
915 1.1 cgd code = -1;
916 1.1 cgd return;
917 1.1 cgd }
918 1.1 cgd if (data >= 0) {
919 1.23 lukem (void)close(data);
920 1.1 cgd data = -1;
921 1.1 cgd }
922 1.1 cgd if (dout)
923 1.23 lukem (void)fclose(dout);
924 1.23 lukem (void)getreply(0);
925 1.1 cgd code = -1;
926 1.1 cgd if (closefunc != NULL && fin != NULL)
927 1.1 cgd (*closefunc)(fin);
928 1.1 cgd if (bytes > 0)
929 1.16 lukem ptransfer(0);
930 1.30 lukem cleanupsend:
931 1.30 lukem progress = oprogress;
932 1.30 lukem restart_point = 0;
933 1.1 cgd }
934 1.1 cgd
935 1.1 cgd jmp_buf recvabort;
936 1.1 cgd
937 1.1 cgd void
938 1.23 lukem abortrecv(notused)
939 1.23 lukem int notused;
940 1.1 cgd {
941 1.1 cgd
942 1.22 lukem alarmtimer(0);
943 1.1 cgd mflag = 0;
944 1.1 cgd abrtflag = 0;
945 1.35 lukem fputs("\nreceive aborted\nwaiting for remote to finish abort.\n",
946 1.35 lukem ttyout);
947 1.1 cgd longjmp(recvabort, 1);
948 1.1 cgd }
949 1.1 cgd
950 1.6 cgd void
951 1.27 lukem recvrequest(cmd, local, remote, lmode, printnames, ignorespecial)
952 1.15 lukem const char *cmd, *local, *remote, *lmode;
953 1.27 lukem int printnames, ignorespecial;
954 1.1 cgd {
955 1.26 lukem FILE *fout, *din;
956 1.6 cgd int (*closefunc) __P((FILE *));
957 1.14 lukem sig_t oldinti, oldintr, oldintp;
958 1.26 lukem int c, d;
959 1.26 lukem volatile int is_retr, tcrflag, bare_lfs;
960 1.47 lukem static size_t bufsize;
961 1.47 lukem static char *buf;
962 1.26 lukem volatile off_t hashbytes;
963 1.1 cgd struct stat st;
964 1.15 lukem time_t mtime;
965 1.16 lukem struct timeval tval[2];
966 1.23 lukem int oprogress;
967 1.24 lukem int opreserve;
968 1.1 cgd
969 1.35 lukem #ifdef __GNUC__ /* to shut up gcc warnings */
970 1.26 lukem (void)&local;
971 1.26 lukem (void)&fout;
972 1.26 lukem (void)&din;
973 1.26 lukem (void)&closefunc;
974 1.26 lukem (void)&oldinti;
975 1.26 lukem (void)&oldintr;
976 1.26 lukem (void)&oldintp;
977 1.26 lukem #endif
978 1.26 lukem
979 1.26 lukem fout = NULL;
980 1.26 lukem din = NULL;
981 1.26 lukem oldinti = NULL;
982 1.16 lukem hashbytes = mark;
983 1.14 lukem direction = "received";
984 1.14 lukem bytes = 0;
985 1.26 lukem bare_lfs = 0;
986 1.16 lukem filesize = -1;
987 1.23 lukem oprogress = progress;
988 1.24 lukem opreserve = preserve;
989 1.30 lukem is_retr = (strcmp(cmd, "RETR") == 0);
990 1.1 cgd if (is_retr && verbose && printnames) {
991 1.27 lukem if (local && (ignorespecial || *local != '-'))
992 1.35 lukem fprintf(ttyout, "local: %s ", local);
993 1.1 cgd if (remote)
994 1.35 lukem fprintf(ttyout, "remote: %s\n", remote);
995 1.1 cgd }
996 1.1 cgd if (proxy && is_retr) {
997 1.1 cgd proxtrans(cmd, local, remote);
998 1.1 cgd return;
999 1.1 cgd }
1000 1.1 cgd closefunc = NULL;
1001 1.1 cgd oldintr = NULL;
1002 1.1 cgd oldintp = NULL;
1003 1.1 cgd tcrflag = !crflag && is_retr;
1004 1.1 cgd if (setjmp(recvabort)) {
1005 1.1 cgd while (cpend) {
1006 1.23 lukem (void)getreply(0);
1007 1.1 cgd }
1008 1.1 cgd if (data >= 0) {
1009 1.23 lukem (void)close(data);
1010 1.1 cgd data = -1;
1011 1.1 cgd }
1012 1.1 cgd if (oldintr)
1013 1.67 lukem (void)xsignal(SIGINT, oldintr);
1014 1.14 lukem if (oldinti)
1015 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1016 1.23 lukem progress = oprogress;
1017 1.24 lukem preserve = opreserve;
1018 1.1 cgd code = -1;
1019 1.1 cgd return;
1020 1.1 cgd }
1021 1.67 lukem oldintr = xsignal(SIGINT, abortrecv);
1022 1.38 lukem oldinti = xsignal(SIGINFO, psummary);
1023 1.27 lukem if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
1024 1.28 lukem if (access(local, W_OK) < 0) {
1025 1.6 cgd char *dir = strrchr(local, '/');
1026 1.1 cgd
1027 1.1 cgd if (errno != ENOENT && errno != EACCES) {
1028 1.6 cgd warn("local: %s", local);
1029 1.67 lukem (void)xsignal(SIGINT, oldintr);
1030 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1031 1.1 cgd code = -1;
1032 1.1 cgd return;
1033 1.1 cgd }
1034 1.1 cgd if (dir != NULL)
1035 1.1 cgd *dir = 0;
1036 1.46 lukem d = access(dir == local ? "/" :
1037 1.46 lukem dir ? local : ".", W_OK);
1038 1.1 cgd if (dir != NULL)
1039 1.1 cgd *dir = '/';
1040 1.1 cgd if (d < 0) {
1041 1.6 cgd warn("local: %s", local);
1042 1.67 lukem (void)xsignal(SIGINT, oldintr);
1043 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1044 1.1 cgd code = -1;
1045 1.1 cgd return;
1046 1.1 cgd }
1047 1.1 cgd if (!runique && errno == EACCES &&
1048 1.35 lukem chmod(local, (S_IRUSR|S_IWUSR)) < 0) {
1049 1.6 cgd warn("local: %s", local);
1050 1.67 lukem (void)xsignal(SIGINT, oldintr);
1051 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1052 1.1 cgd code = -1;
1053 1.1 cgd return;
1054 1.1 cgd }
1055 1.1 cgd if (runique && errno == EACCES &&
1056 1.1 cgd (local = gunique(local)) == NULL) {
1057 1.67 lukem (void)xsignal(SIGINT, oldintr);
1058 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1059 1.1 cgd code = -1;
1060 1.1 cgd return;
1061 1.1 cgd }
1062 1.1 cgd }
1063 1.1 cgd else if (runique && (local = gunique(local)) == NULL) {
1064 1.67 lukem (void)xsignal(SIGINT, oldintr);
1065 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1066 1.1 cgd code = -1;
1067 1.1 cgd return;
1068 1.1 cgd }
1069 1.1 cgd }
1070 1.1 cgd if (!is_retr) {
1071 1.1 cgd if (curtype != TYPE_A)
1072 1.1 cgd changetype(TYPE_A, 0);
1073 1.16 lukem } else {
1074 1.16 lukem if (curtype != type)
1075 1.16 lukem changetype(type, 0);
1076 1.19 lukem filesize = remotesize(remote, 0);
1077 1.16 lukem }
1078 1.1 cgd if (initconn()) {
1079 1.67 lukem (void)xsignal(SIGINT, oldintr);
1080 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1081 1.1 cgd code = -1;
1082 1.1 cgd return;
1083 1.1 cgd }
1084 1.1 cgd if (setjmp(recvabort))
1085 1.1 cgd goto abort;
1086 1.1 cgd if (is_retr && restart_point &&
1087 1.33 christos #ifndef NO_QUAD
1088 1.66 lukem command("REST %lld", (long long) restart_point) != CONTINUE)
1089 1.33 christos #else
1090 1.33 christos command("REST %ld", (long) restart_point) != CONTINUE)
1091 1.33 christos #endif
1092 1.1 cgd return;
1093 1.1 cgd if (remote) {
1094 1.1 cgd if (command("%s %s", cmd, remote) != PRELIM) {
1095 1.67 lukem (void)xsignal(SIGINT, oldintr);
1096 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1097 1.1 cgd return;
1098 1.1 cgd }
1099 1.1 cgd } else {
1100 1.1 cgd if (command("%s", cmd) != PRELIM) {
1101 1.67 lukem (void)xsignal(SIGINT, oldintr);
1102 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1103 1.1 cgd return;
1104 1.1 cgd }
1105 1.1 cgd }
1106 1.1 cgd din = dataconn("r");
1107 1.1 cgd if (din == NULL)
1108 1.1 cgd goto abort;
1109 1.27 lukem if (!ignorespecial && strcmp(local, "-") == 0) {
1110 1.1 cgd fout = stdout;
1111 1.23 lukem progress = 0;
1112 1.24 lukem preserve = 0;
1113 1.27 lukem } else if (!ignorespecial && *local == '|') {
1114 1.67 lukem oldintp = xsignal(SIGPIPE, SIG_IGN);
1115 1.1 cgd fout = popen(local + 1, "w");
1116 1.1 cgd if (fout == NULL) {
1117 1.6 cgd warn("%s", local+1);
1118 1.1 cgd goto abort;
1119 1.1 cgd }
1120 1.23 lukem progress = 0;
1121 1.24 lukem preserve = 0;
1122 1.1 cgd closefunc = pclose;
1123 1.1 cgd } else {
1124 1.1 cgd fout = fopen(local, lmode);
1125 1.1 cgd if (fout == NULL) {
1126 1.6 cgd warn("local: %s", local);
1127 1.1 cgd goto abort;
1128 1.1 cgd }
1129 1.1 cgd closefunc = fclose;
1130 1.1 cgd }
1131 1.46 lukem
1132 1.64 lukem if (fstat(fileno(fout), &st) != -1 && !S_ISREG(st.st_mode)) {
1133 1.64 lukem progress = 0;
1134 1.64 lukem preserve = 0;
1135 1.64 lukem }
1136 1.64 lukem if (rcvbuf_size > bufsize) {
1137 1.1 cgd if (buf)
1138 1.23 lukem (void)free(buf);
1139 1.64 lukem bufsize = rcvbuf_size;
1140 1.37 lukem buf = xmalloc(bufsize);
1141 1.1 cgd }
1142 1.64 lukem
1143 1.16 lukem progressmeter(-1);
1144 1.64 lukem
1145 1.1 cgd switch (curtype) {
1146 1.1 cgd
1147 1.1 cgd case TYPE_I:
1148 1.1 cgd case TYPE_L:
1149 1.30 lukem if (is_retr && restart_point &&
1150 1.6 cgd lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
1151 1.6 cgd warn("local: %s", local);
1152 1.23 lukem progress = oprogress;
1153 1.24 lukem preserve = opreserve;
1154 1.1 cgd if (closefunc != NULL)
1155 1.1 cgd (*closefunc)(fout);
1156 1.1 cgd return;
1157 1.1 cgd }
1158 1.46 lukem while (1) {
1159 1.46 lukem struct timeval then, now, td;
1160 1.46 lukem off_t bufrem;
1161 1.46 lukem
1162 1.62 lukem if (rate_get)
1163 1.62 lukem (void)gettimeofday(&then, NULL);
1164 1.46 lukem errno = c = d = 0;
1165 1.46 lukem bufrem = rate_get ? rate_get : bufsize;
1166 1.46 lukem while (bufrem > 0) {
1167 1.46 lukem if ((c = read(fileno(din), buf,
1168 1.46 lukem MIN(bufsize, bufrem))) <= 0)
1169 1.46 lukem goto recvdone;
1170 1.46 lukem bytes += c;
1171 1.46 lukem bufrem -=c;
1172 1.46 lukem if ((d = write(fileno(fout), buf, c)) != c)
1173 1.46 lukem goto recvdone;
1174 1.46 lukem if (hash && (!progress || filesize < 0)) {
1175 1.46 lukem while (bytes >= hashbytes) {
1176 1.46 lukem (void)putc('#', ttyout);
1177 1.46 lukem hashbytes += mark;
1178 1.46 lukem }
1179 1.46 lukem (void)fflush(ttyout);
1180 1.46 lukem }
1181 1.46 lukem }
1182 1.46 lukem if (rate_get) {
1183 1.46 lukem while (1) {
1184 1.46 lukem (void)gettimeofday(&now, NULL);
1185 1.46 lukem timersub(&now, &then, &td);
1186 1.46 lukem if (td.tv_sec > 0)
1187 1.46 lukem break;
1188 1.46 lukem usleep(1000000 - td.tv_usec);
1189 1.1 cgd }
1190 1.1 cgd }
1191 1.1 cgd }
1192 1.46 lukem recvdone:
1193 1.22 lukem if (hash && (!progress || filesize < 0) && bytes > 0) {
1194 1.14 lukem if (bytes < mark)
1195 1.35 lukem (void)putc('#', ttyout);
1196 1.35 lukem (void)putc('\n', ttyout);
1197 1.1 cgd }
1198 1.1 cgd if (c < 0) {
1199 1.1 cgd if (errno != EPIPE)
1200 1.6 cgd warn("netin");
1201 1.1 cgd bytes = -1;
1202 1.1 cgd }
1203 1.1 cgd if (d < c) {
1204 1.1 cgd if (d < 0)
1205 1.6 cgd warn("local: %s", local);
1206 1.1 cgd else
1207 1.6 cgd warnx("%s: short write", local);
1208 1.1 cgd }
1209 1.1 cgd break;
1210 1.1 cgd
1211 1.1 cgd case TYPE_A:
1212 1.30 lukem if (is_retr && restart_point) {
1213 1.30 lukem int ch;
1214 1.30 lukem long i, n;
1215 1.1 cgd
1216 1.6 cgd if (fseek(fout, 0L, SEEK_SET) < 0)
1217 1.1 cgd goto done;
1218 1.30 lukem n = (long)restart_point;
1219 1.1 cgd for (i = 0; i++ < n;) {
1220 1.1 cgd if ((ch = getc(fout)) == EOF)
1221 1.1 cgd goto done;
1222 1.1 cgd if (ch == '\n')
1223 1.1 cgd i++;
1224 1.1 cgd }
1225 1.6 cgd if (fseek(fout, 0L, SEEK_CUR) < 0) {
1226 1.1 cgd done:
1227 1.6 cgd warn("local: %s", local);
1228 1.23 lukem progress = oprogress;
1229 1.24 lukem preserve = opreserve;
1230 1.1 cgd if (closefunc != NULL)
1231 1.1 cgd (*closefunc)(fout);
1232 1.1 cgd return;
1233 1.1 cgd }
1234 1.1 cgd }
1235 1.1 cgd while ((c = getc(din)) != EOF) {
1236 1.1 cgd if (c == '\n')
1237 1.1 cgd bare_lfs++;
1238 1.1 cgd while (c == '\r') {
1239 1.22 lukem while (hash && (!progress || filesize < 0) &&
1240 1.16 lukem (bytes >= hashbytes)) {
1241 1.35 lukem (void)putc('#', ttyout);
1242 1.35 lukem (void)fflush(ttyout);
1243 1.14 lukem hashbytes += mark;
1244 1.1 cgd }
1245 1.1 cgd bytes++;
1246 1.1 cgd if ((c = getc(din)) != '\n' || tcrflag) {
1247 1.1 cgd if (ferror(fout))
1248 1.1 cgd goto break2;
1249 1.23 lukem (void)putc('\r', fout);
1250 1.1 cgd if (c == '\0') {
1251 1.1 cgd bytes++;
1252 1.1 cgd goto contin2;
1253 1.1 cgd }
1254 1.1 cgd if (c == EOF)
1255 1.1 cgd goto contin2;
1256 1.1 cgd }
1257 1.1 cgd }
1258 1.23 lukem (void)putc(c, fout);
1259 1.1 cgd bytes++;
1260 1.1 cgd contin2: ;
1261 1.1 cgd }
1262 1.1 cgd break2:
1263 1.22 lukem if (hash && (!progress || filesize < 0)) {
1264 1.1 cgd if (bytes < hashbytes)
1265 1.35 lukem (void)putc('#', ttyout);
1266 1.35 lukem (void)putc('\n', ttyout);
1267 1.1 cgd }
1268 1.1 cgd if (ferror(din)) {
1269 1.1 cgd if (errno != EPIPE)
1270 1.6 cgd warn("netin");
1271 1.1 cgd bytes = -1;
1272 1.1 cgd }
1273 1.1 cgd if (ferror(fout))
1274 1.6 cgd warn("local: %s", local);
1275 1.1 cgd break;
1276 1.1 cgd }
1277 1.22 lukem progressmeter(1);
1278 1.1 cgd if (closefunc != NULL)
1279 1.1 cgd (*closefunc)(fout);
1280 1.67 lukem (void)xsignal(SIGINT, oldintr);
1281 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1282 1.1 cgd if (oldintp)
1283 1.67 lukem (void)xsignal(SIGPIPE, oldintp);
1284 1.23 lukem (void)fclose(din);
1285 1.23 lukem (void)getreply(0);
1286 1.43 lukem if (bare_lfs) {
1287 1.43 lukem fprintf(ttyout,
1288 1.43 lukem "WARNING! %d bare linefeeds received in ASCII mode.\n",
1289 1.43 lukem bare_lfs);
1290 1.43 lukem fputs("File may not have transferred correctly.\n", ttyout);
1291 1.43 lukem }
1292 1.19 lukem if (bytes >= 0 && is_retr) {
1293 1.19 lukem if (bytes > 0)
1294 1.19 lukem ptransfer(0);
1295 1.15 lukem if (preserve && (closefunc == fclose)) {
1296 1.19 lukem mtime = remotemodtime(remote, 0);
1297 1.15 lukem if (mtime != -1) {
1298 1.31 lukem (void)gettimeofday(&tval[0], NULL);
1299 1.16 lukem tval[1].tv_sec = mtime;
1300 1.16 lukem tval[1].tv_usec = 0;
1301 1.16 lukem if (utimes(local, tval) == -1) {
1302 1.35 lukem fprintf(ttyout,
1303 1.23 lukem "Can't change modification time on %s to %s",
1304 1.23 lukem local, asctime(localtime(&mtime)));
1305 1.15 lukem }
1306 1.15 lukem }
1307 1.15 lukem }
1308 1.15 lukem }
1309 1.34 pk progress = oprogress;
1310 1.34 pk preserve = opreserve;
1311 1.1 cgd return;
1312 1.23 lukem
1313 1.1 cgd abort:
1314 1.1 cgd
1315 1.42 lukem /* abort using RFC 959 recommended IP,SYNC sequence */
1316 1.1 cgd
1317 1.23 lukem progress = oprogress;
1318 1.24 lukem preserve = opreserve;
1319 1.1 cgd if (oldintp)
1320 1.67 lukem (void)xsignal(SIGPIPE, oldintp);
1321 1.67 lukem (void)xsignal(SIGINT, SIG_IGN);
1322 1.1 cgd if (!cpend) {
1323 1.1 cgd code = -1;
1324 1.67 lukem (void)xsignal(SIGINT, oldintr);
1325 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1326 1.1 cgd return;
1327 1.1 cgd }
1328 1.1 cgd
1329 1.1 cgd abort_remote(din);
1330 1.1 cgd code = -1;
1331 1.1 cgd if (data >= 0) {
1332 1.23 lukem (void)close(data);
1333 1.1 cgd data = -1;
1334 1.1 cgd }
1335 1.1 cgd if (closefunc != NULL && fout != NULL)
1336 1.1 cgd (*closefunc)(fout);
1337 1.1 cgd if (din)
1338 1.23 lukem (void)fclose(din);
1339 1.1 cgd if (bytes > 0)
1340 1.16 lukem ptransfer(0);
1341 1.67 lukem (void)xsignal(SIGINT, oldintr);
1342 1.38 lukem (void)xsignal(SIGINFO, oldinti);
1343 1.1 cgd }
1344 1.1 cgd
1345 1.1 cgd /*
1346 1.1 cgd * Need to start a listen on the data channel before we send the command,
1347 1.1 cgd * otherwise the server's connect may fail.
1348 1.1 cgd */
1349 1.6 cgd int
1350 1.1 cgd initconn()
1351 1.1 cgd {
1352 1.6 cgd char *p, *a;
1353 1.1 cgd int result, len, tmpno = 0;
1354 1.1 cgd int on = 1;
1355 1.48 itojun int error;
1356 1.48 itojun u_int addr[16], port[2];
1357 1.48 itojun u_int af, hal, pal;
1358 1.48 itojun char *pasvcmd = NULL;
1359 1.8 cgd
1360 1.62 lukem #ifdef INET6
1361 1.59 itojun if (myctladdr.su_family == AF_INET6
1362 1.59 itojun && (IN6_IS_ADDR_LINKLOCAL(&myctladdr.su_sin6.sin6_addr)
1363 1.59 itojun || IN6_IS_ADDR_SITELOCAL(&myctladdr.su_sin6.sin6_addr))) {
1364 1.59 itojun warnx("use of scoped address can be troublesome");
1365 1.59 itojun }
1366 1.62 lukem #endif
1367 1.35 lukem reinit:
1368 1.8 cgd if (passivemode) {
1369 1.48 itojun data_addr = myctladdr;
1370 1.48 itojun data = socket(data_addr.su_family, SOCK_STREAM, 0);
1371 1.8 cgd if (data < 0) {
1372 1.19 lukem warn("socket");
1373 1.21 lukem return (1);
1374 1.8 cgd }
1375 1.8 cgd if ((options & SO_DEBUG) &&
1376 1.8 cgd setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1377 1.23 lukem sizeof(on)) < 0)
1378 1.19 lukem warn("setsockopt (ignored)");
1379 1.54 itojun result = COMPLETE + 1;
1380 1.48 itojun switch (data_addr.su_family) {
1381 1.48 itojun case AF_INET:
1382 1.55 itojun if (epsv4) {
1383 1.54 itojun result = command(pasvcmd = "EPSV");
1384 1.55 itojun /*
1385 1.55 itojun * this code is to be friendly with broken
1386 1.55 itojun * BSDI ftpd
1387 1.55 itojun */
1388 1.56 itojun if (code / 10 == 22 && code != 229) {
1389 1.55 itojun fputs(
1390 1.55 itojun "wrong server: return code must be 229\n",
1391 1.55 itojun ttyout);
1392 1.55 itojun result = COMPLETE + 1;
1393 1.55 itojun }
1394 1.55 itojun }
1395 1.48 itojun if (result != COMPLETE)
1396 1.48 itojun result = command(pasvcmd = "PASV");
1397 1.48 itojun break;
1398 1.62 lukem #ifdef INET6
1399 1.48 itojun case AF_INET6:
1400 1.48 itojun result = command(pasvcmd = "EPSV");
1401 1.55 itojun /* this code is to be friendly with broken BSDI ftpd */
1402 1.56 itojun if (code / 10 == 22 && code != 229) {
1403 1.55 itojun fputs(
1404 1.55 itojun "wrong server: return code must be 229\n",
1405 1.55 itojun ttyout);
1406 1.55 itojun result = COMPLETE + 1;
1407 1.55 itojun }
1408 1.48 itojun if (result != COMPLETE)
1409 1.48 itojun result = command(pasvcmd = "LPSV");
1410 1.48 itojun break;
1411 1.62 lukem #endif
1412 1.48 itojun default:
1413 1.57 itojun result = COMPLETE + 1;
1414 1.54 itojun break;
1415 1.48 itojun }
1416 1.48 itojun if (result != COMPLETE) {
1417 1.35 lukem if (activefallback) {
1418 1.35 lukem (void)close(data);
1419 1.35 lukem data = -1;
1420 1.35 lukem passivemode = 0;
1421 1.35 lukem activefallback = 0;
1422 1.35 lukem goto reinit;
1423 1.35 lukem }
1424 1.35 lukem fputs("Passive mode refused.\n", ttyout);
1425 1.8 cgd goto bad;
1426 1.8 cgd }
1427 1.8 cgd
1428 1.48 itojun #define pack2(var, off) \
1429 1.48 itojun (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
1430 1.48 itojun #define pack4(var, off) \
1431 1.48 itojun (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
1432 1.48 itojun ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
1433 1.48 itojun
1434 1.8 cgd /*
1435 1.48 itojun * What we've got at this point is a string of comma separated
1436 1.48 itojun * one-byte unsigned integer values, separated by commas.
1437 1.8 cgd */
1438 1.55 itojun if (strcmp(pasvcmd, "PASV") == 0) {
1439 1.55 itojun if (data_addr.su_family != AF_INET) {
1440 1.55 itojun fputs(
1441 1.55 itojun "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1442 1.55 itojun error = 1;
1443 1.55 itojun goto bad;
1444 1.55 itojun }
1445 1.56 itojun if (code / 10 == 22 && code != 227) {
1446 1.55 itojun fputs("wrong server: return code must be 227\n",
1447 1.55 itojun ttyout);
1448 1.55 itojun error = 1;
1449 1.55 itojun goto bad;
1450 1.55 itojun }
1451 1.55 itojun error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",
1452 1.55 itojun &addr[0], &addr[1], &addr[2], &addr[3],
1453 1.55 itojun &port[0], &port[1]);
1454 1.55 itojun if (error != 6) {
1455 1.55 itojun fputs(
1456 1.55 itojun "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1457 1.55 itojun error = 1;
1458 1.55 itojun goto bad;
1459 1.55 itojun }
1460 1.55 itojun error = 0;
1461 1.55 itojun memset(&data_addr, 0, sizeof(data_addr));
1462 1.55 itojun data_addr.su_family = AF_INET;
1463 1.55 itojun data_addr.su_len = sizeof(struct sockaddr_in);
1464 1.55 itojun data_addr.su_sin.sin_addr.s_addr =
1465 1.55 itojun htonl(pack4(addr, 0));
1466 1.55 itojun data_addr.su_port = htons(pack2(port, 0));
1467 1.55 itojun } else if (strcmp(pasvcmd, "LPSV") == 0) {
1468 1.56 itojun if (code / 10 == 22 && code != 228) {
1469 1.55 itojun fputs("wrong server: return code must be 228\n",
1470 1.55 itojun ttyout);
1471 1.55 itojun error = 1;
1472 1.55 itojun goto bad;
1473 1.55 itojun }
1474 1.48 itojun switch (data_addr.su_family) {
1475 1.48 itojun case AF_INET:
1476 1.55 itojun error = sscanf(pasv,
1477 1.55 itojun "%u,%u,%u,%u,%u,%u,%u,%u,%u",
1478 1.57 itojun &af, &hal,
1479 1.55 itojun &addr[0], &addr[1], &addr[2], &addr[3],
1480 1.55 itojun &pal, &port[0], &port[1]);
1481 1.57 itojun if (error != 9) {
1482 1.55 itojun fputs(
1483 1.57 itojun "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1484 1.55 itojun error = 1;
1485 1.55 itojun goto bad;
1486 1.55 itojun }
1487 1.57 itojun if (af != 4 || hal != 4 || pal != 2) {
1488 1.48 itojun fputs(
1489 1.57 itojun "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1490 1.48 itojun error = 1;
1491 1.48 itojun goto bad;
1492 1.48 itojun }
1493 1.57 itojun
1494 1.48 itojun error = 0;
1495 1.48 itojun memset(&data_addr, 0, sizeof(data_addr));
1496 1.48 itojun data_addr.su_family = AF_INET;
1497 1.48 itojun data_addr.su_len = sizeof(struct sockaddr_in);
1498 1.48 itojun data_addr.su_sin.sin_addr.s_addr =
1499 1.48 itojun htonl(pack4(addr, 0));
1500 1.48 itojun data_addr.su_port = htons(pack2(port, 0));
1501 1.48 itojun break;
1502 1.62 lukem #ifdef INET6
1503 1.48 itojun case AF_INET6:
1504 1.48 itojun error = sscanf(pasv,
1505 1.48 itojun "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
1506 1.48 itojun &af, &hal,
1507 1.48 itojun &addr[0], &addr[1], &addr[2], &addr[3],
1508 1.48 itojun &addr[4], &addr[5], &addr[6], &addr[7],
1509 1.48 itojun &addr[8], &addr[9], &addr[10],
1510 1.48 itojun &addr[11], &addr[12], &addr[13],
1511 1.48 itojun &addr[14], &addr[15],
1512 1.48 itojun &pal, &port[0], &port[1]);
1513 1.57 itojun if (error != 21) {
1514 1.48 itojun fputs(
1515 1.57 itojun "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1516 1.48 itojun error = 1;
1517 1.48 itojun goto bad;
1518 1.48 itojun }
1519 1.57 itojun if (af != 6 || hal != 16 || pal != 2) {
1520 1.48 itojun fputs(
1521 1.57 itojun "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1522 1.48 itojun error = 1;
1523 1.48 itojun goto bad;
1524 1.48 itojun }
1525 1.8 cgd
1526 1.48 itojun error = 0;
1527 1.48 itojun memset(&data_addr, 0, sizeof(data_addr));
1528 1.48 itojun data_addr.su_family = AF_INET6;
1529 1.48 itojun data_addr.su_len = sizeof(struct sockaddr_in6);
1530 1.60 itojun {
1531 1.60 itojun u_int32_t *p32;
1532 1.60 itojun p32 = (u_int32_t *)&data_addr.su_sin6.sin6_addr;
1533 1.60 itojun p32[0] = htonl(pack4(addr, 0));
1534 1.60 itojun p32[1] = htonl(pack4(addr, 4));
1535 1.60 itojun p32[2] = htonl(pack4(addr, 8));
1536 1.60 itojun p32[3] = htonl(pack4(addr, 12));
1537 1.60 itojun }
1538 1.48 itojun data_addr.su_port = htons(pack2(port, 0));
1539 1.48 itojun break;
1540 1.62 lukem #endif
1541 1.48 itojun default:
1542 1.48 itojun error = 1;
1543 1.48 itojun }
1544 1.48 itojun } else if (strcmp(pasvcmd, "EPSV") == 0) {
1545 1.48 itojun char delim[4];
1546 1.48 itojun
1547 1.48 itojun port[0] = 0;
1548 1.56 itojun if (code / 10 == 22 && code != 229) {
1549 1.55 itojun fputs("wrong server: return code must be 229\n",
1550 1.55 itojun ttyout);
1551 1.55 itojun error = 1;
1552 1.55 itojun goto bad;
1553 1.55 itojun }
1554 1.48 itojun if (sscanf(pasv, "%c%c%c%d%c", &delim[0],
1555 1.48 itojun &delim[1], &delim[2], &port[1],
1556 1.48 itojun &delim[3]) != 5) {
1557 1.57 itojun fputs("parse error!\n", ttyout);
1558 1.57 itojun error = 1;
1559 1.48 itojun goto bad;
1560 1.48 itojun }
1561 1.48 itojun if (delim[0] != delim[1] || delim[0] != delim[2]
1562 1.48 itojun || delim[0] != delim[3]) {
1563 1.57 itojun fputs("parse error!\n", ttyout);
1564 1.57 itojun error = 1;
1565 1.48 itojun goto bad;
1566 1.48 itojun }
1567 1.48 itojun data_addr = hisctladdr;
1568 1.48 itojun data_addr.su_port = htons(port[1]);
1569 1.48 itojun } else
1570 1.8 cgd goto bad;
1571 1.8 cgd
1572 1.36 thorpej while (xconnect(data, (struct sockaddr *)&data_addr,
1573 1.48 itojun data_addr.su_len) < 0) {
1574 1.35 lukem if (errno == EINTR)
1575 1.35 lukem continue;
1576 1.39 lukem if (activefallback) {
1577 1.39 lukem (void)close(data);
1578 1.39 lukem data = -1;
1579 1.39 lukem passivemode = 0;
1580 1.39 lukem activefallback = 0;
1581 1.39 lukem goto reinit;
1582 1.39 lukem }
1583 1.19 lukem warn("connect");
1584 1.8 cgd goto bad;
1585 1.8 cgd }
1586 1.48 itojun #if defined(IPPROTO_IP) && defined(IP_TOS)
1587 1.48 itojun if (data_addr.su_family == AF_INET) {
1588 1.48 itojun on = IPTOS_THROUGHPUT;
1589 1.48 itojun if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1590 1.48 itojun sizeof(int)) < 0)
1591 1.48 itojun warn("setsockopt TOS (ignored)");
1592 1.48 itojun }
1593 1.8 cgd #endif
1594 1.21 lukem return (0);
1595 1.8 cgd }
1596 1.1 cgd
1597 1.1 cgd noport:
1598 1.1 cgd data_addr = myctladdr;
1599 1.1 cgd if (sendport)
1600 1.48 itojun data_addr.su_port = 0; /* let system pick one */
1601 1.1 cgd if (data != -1)
1602 1.23 lukem (void)close(data);
1603 1.48 itojun data = socket(data_addr.su_family, SOCK_STREAM, 0);
1604 1.1 cgd if (data < 0) {
1605 1.6 cgd warn("socket");
1606 1.1 cgd if (tmpno)
1607 1.1 cgd sendport = 1;
1608 1.1 cgd return (1);
1609 1.1 cgd }
1610 1.1 cgd if (!sendport)
1611 1.15 lukem if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
1612 1.23 lukem sizeof(on)) < 0) {
1613 1.6 cgd warn("setsockopt (reuse address)");
1614 1.1 cgd goto bad;
1615 1.1 cgd }
1616 1.48 itojun if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {
1617 1.6 cgd warn("bind");
1618 1.1 cgd goto bad;
1619 1.1 cgd }
1620 1.1 cgd if (options & SO_DEBUG &&
1621 1.15 lukem setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1622 1.23 lukem sizeof(on)) < 0)
1623 1.6 cgd warn("setsockopt (ignored)");
1624 1.23 lukem len = sizeof(data_addr);
1625 1.1 cgd if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1626 1.6 cgd warn("getsockname");
1627 1.1 cgd goto bad;
1628 1.1 cgd }
1629 1.36 thorpej if (xlisten(data, 1) < 0)
1630 1.6 cgd warn("listen");
1631 1.48 itojun
1632 1.48 itojun #define UC(b) (((int)b)&0xff)
1633 1.48 itojun
1634 1.1 cgd if (sendport) {
1635 1.58 christos #ifdef INET6
1636 1.48 itojun char hname[INET6_ADDRSTRLEN];
1637 1.48 itojun int af;
1638 1.58 christos #endif
1639 1.48 itojun
1640 1.48 itojun switch (data_addr.su_family) {
1641 1.48 itojun case AF_INET:
1642 1.54 itojun if (!epsv4) {
1643 1.54 itojun result = COMPLETE + 1;
1644 1.54 itojun break;
1645 1.54 itojun }
1646 1.54 itojun /* FALLTHROUGH */
1647 1.58 christos #ifdef INET6
1648 1.48 itojun case AF_INET6:
1649 1.48 itojun af = (data_addr.su_family == AF_INET) ? 1 : 2;
1650 1.48 itojun if (getnameinfo((struct sockaddr *)&data_addr,
1651 1.48 itojun data_addr.su_len, hname, sizeof(hname),
1652 1.48 itojun NULL, 0, NI_NUMERICHOST)) {
1653 1.48 itojun result = ERROR;
1654 1.48 itojun } else {
1655 1.48 itojun result = command("EPRT |%d|%s|%d|",
1656 1.48 itojun af, hname, ntohs(data_addr.su_port));
1657 1.48 itojun }
1658 1.48 itojun break;
1659 1.58 christos #endif
1660 1.48 itojun default:
1661 1.48 itojun result = COMPLETE + 1;
1662 1.48 itojun break;
1663 1.48 itojun }
1664 1.48 itojun if (result == COMPLETE)
1665 1.48 itojun goto skip_port;
1666 1.48 itojun
1667 1.48 itojun switch (data_addr.su_family) {
1668 1.48 itojun case AF_INET:
1669 1.48 itojun a = (char *)&data_addr.su_sin.sin_addr;
1670 1.48 itojun p = (char *)&data_addr.su_port;
1671 1.48 itojun result = command("PORT %d,%d,%d,%d,%d,%d",
1672 1.48 itojun UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1673 1.48 itojun UC(p[0]), UC(p[1]));
1674 1.48 itojun break;
1675 1.58 christos #ifdef INET6
1676 1.48 itojun case AF_INET6:
1677 1.48 itojun a = (char *)&data_addr.su_sin6.sin6_addr;
1678 1.48 itojun p = (char *)&data_addr.su_port;
1679 1.48 itojun result = command(
1680 1.48 itojun "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1681 1.48 itojun 6, 16,
1682 1.48 itojun UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
1683 1.48 itojun UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
1684 1.48 itojun UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
1685 1.48 itojun UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
1686 1.48 itojun 2, UC(p[0]), UC(p[1]));
1687 1.48 itojun break;
1688 1.58 christos #endif
1689 1.48 itojun default:
1690 1.48 itojun result = COMPLETE + 1; /* xxx */
1691 1.48 itojun }
1692 1.48 itojun skip_port:
1693 1.48 itojun
1694 1.1 cgd if (result == ERROR && sendport == -1) {
1695 1.1 cgd sendport = 0;
1696 1.1 cgd tmpno = 1;
1697 1.1 cgd goto noport;
1698 1.1 cgd }
1699 1.1 cgd return (result != COMPLETE);
1700 1.1 cgd }
1701 1.1 cgd if (tmpno)
1702 1.1 cgd sendport = 1;
1703 1.48 itojun #if defined(IPPROTO_IP) && defined(IP_TOS)
1704 1.48 itojun if (data_addr.su_family == AF_INET) {
1705 1.48 itojun on = IPTOS_THROUGHPUT;
1706 1.48 itojun if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1707 1.48 itojun sizeof(int)) < 0)
1708 1.48 itojun warn("setsockopt TOS (ignored)");
1709 1.48 itojun }
1710 1.1 cgd #endif
1711 1.1 cgd return (0);
1712 1.1 cgd bad:
1713 1.23 lukem (void)close(data), data = -1;
1714 1.1 cgd if (tmpno)
1715 1.1 cgd sendport = 1;
1716 1.1 cgd return (1);
1717 1.1 cgd }
1718 1.1 cgd
1719 1.1 cgd FILE *
1720 1.1 cgd dataconn(lmode)
1721 1.15 lukem const char *lmode;
1722 1.1 cgd {
1723 1.48 itojun union sockunion from;
1724 1.48 itojun int s, fromlen = myctladdr.su_len;
1725 1.8 cgd
1726 1.8 cgd if (passivemode)
1727 1.8 cgd return (fdopen(data, lmode));
1728 1.1 cgd
1729 1.1 cgd s = accept(data, (struct sockaddr *) &from, &fromlen);
1730 1.1 cgd if (s < 0) {
1731 1.6 cgd warn("accept");
1732 1.23 lukem (void)close(data), data = -1;
1733 1.1 cgd return (NULL);
1734 1.1 cgd }
1735 1.23 lukem (void)close(data);
1736 1.1 cgd data = s;
1737 1.48 itojun #if defined(IPPROTO_IP) && defined(IP_TOS)
1738 1.48 itojun if (from.su_family == AF_INET) {
1739 1.48 itojun int tos = IPTOS_THROUGHPUT;
1740 1.48 itojun if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
1741 1.48 itojun sizeof(int)) < 0) {
1742 1.48 itojun warn("setsockopt TOS (ignored)");
1743 1.48 itojun }
1744 1.48 itojun }
1745 1.1 cgd #endif
1746 1.1 cgd return (fdopen(data, lmode));
1747 1.1 cgd }
1748 1.1 cgd
1749 1.1 cgd void
1750 1.14 lukem psummary(notused)
1751 1.14 lukem int notused;
1752 1.14 lukem {
1753 1.46 lukem int oerrno;
1754 1.16 lukem
1755 1.46 lukem oerrno = errno;
1756 1.14 lukem if (bytes > 0)
1757 1.16 lukem ptransfer(1);
1758 1.46 lukem errno = oerrno;
1759 1.14 lukem }
1760 1.14 lukem
1761 1.14 lukem void
1762 1.23 lukem psabort(notused)
1763 1.23 lukem int notused;
1764 1.1 cgd {
1765 1.1 cgd
1766 1.22 lukem alarmtimer(0);
1767 1.1 cgd abrtflag++;
1768 1.1 cgd }
1769 1.1 cgd
1770 1.6 cgd void
1771 1.1 cgd pswitch(flag)
1772 1.1 cgd int flag;
1773 1.1 cgd {
1774 1.1 cgd sig_t oldintr;
1775 1.1 cgd static struct comvars {
1776 1.1 cgd int connect;
1777 1.1 cgd char name[MAXHOSTNAMELEN];
1778 1.48 itojun union sockunion mctl;
1779 1.48 itojun union sockunion hctl;
1780 1.1 cgd FILE *in;
1781 1.1 cgd FILE *out;
1782 1.1 cgd int tpe;
1783 1.1 cgd int curtpe;
1784 1.1 cgd int cpnd;
1785 1.1 cgd int sunqe;
1786 1.1 cgd int runqe;
1787 1.1 cgd int mcse;
1788 1.1 cgd int ntflg;
1789 1.1 cgd char nti[17];
1790 1.1 cgd char nto[17];
1791 1.1 cgd int mapflg;
1792 1.1 cgd char mi[MAXPATHLEN];
1793 1.1 cgd char mo[MAXPATHLEN];
1794 1.1 cgd } proxstruct, tmpstruct;
1795 1.1 cgd struct comvars *ip, *op;
1796 1.1 cgd
1797 1.1 cgd abrtflag = 0;
1798 1.67 lukem oldintr = xsignal(SIGINT, psabort);
1799 1.1 cgd if (flag) {
1800 1.1 cgd if (proxy)
1801 1.1 cgd return;
1802 1.1 cgd ip = &tmpstruct;
1803 1.1 cgd op = &proxstruct;
1804 1.1 cgd proxy++;
1805 1.1 cgd } else {
1806 1.1 cgd if (!proxy)
1807 1.1 cgd return;
1808 1.1 cgd ip = &proxstruct;
1809 1.1 cgd op = &tmpstruct;
1810 1.1 cgd proxy = 0;
1811 1.1 cgd }
1812 1.1 cgd ip->connect = connected;
1813 1.1 cgd connected = op->connect;
1814 1.68 lukem if (hostname)
1815 1.68 lukem (void)strlcpy(ip->name, hostname, sizeof(ip->name));
1816 1.68 lukem else
1817 1.23 lukem ip->name[0] = '\0';
1818 1.1 cgd hostname = op->name;
1819 1.1 cgd ip->hctl = hisctladdr;
1820 1.1 cgd hisctladdr = op->hctl;
1821 1.1 cgd ip->mctl = myctladdr;
1822 1.1 cgd myctladdr = op->mctl;
1823 1.1 cgd ip->in = cin;
1824 1.1 cgd cin = op->in;
1825 1.1 cgd ip->out = cout;
1826 1.1 cgd cout = op->out;
1827 1.1 cgd ip->tpe = type;
1828 1.1 cgd type = op->tpe;
1829 1.1 cgd ip->curtpe = curtype;
1830 1.1 cgd curtype = op->curtpe;
1831 1.1 cgd ip->cpnd = cpend;
1832 1.1 cgd cpend = op->cpnd;
1833 1.1 cgd ip->sunqe = sunique;
1834 1.1 cgd sunique = op->sunqe;
1835 1.1 cgd ip->runqe = runique;
1836 1.1 cgd runique = op->runqe;
1837 1.1 cgd ip->mcse = mcase;
1838 1.1 cgd mcase = op->mcse;
1839 1.1 cgd ip->ntflg = ntflag;
1840 1.1 cgd ntflag = op->ntflg;
1841 1.68 lukem (void)strlcpy(ip->nti, ntin, sizeof(ip->nti));
1842 1.68 lukem (void)strlcpy(ntin, op->nti, sizeof(ntin));
1843 1.68 lukem (void)strlcpy(ip->nto, ntout, sizeof(ip->nto));
1844 1.68 lukem (void)strlcpy(ntout, op->nto, sizeof(ntout));
1845 1.1 cgd ip->mapflg = mapflag;
1846 1.1 cgd mapflag = op->mapflg;
1847 1.68 lukem (void)strlcpy(ip->mi, mapin, sizeof(ip->mi));
1848 1.68 lukem (void)strlcpy(mapin, op->mi, sizeof(mapin));
1849 1.68 lukem (void)strlcpy(ip->mo, mapout, sizeof(ip->mo));
1850 1.68 lukem (void)strlcpy(mapout, op->mo, sizeof(mapout));
1851 1.67 lukem (void)xsignal(SIGINT, oldintr);
1852 1.1 cgd if (abrtflag) {
1853 1.1 cgd abrtflag = 0;
1854 1.1 cgd (*oldintr)(SIGINT);
1855 1.1 cgd }
1856 1.1 cgd }
1857 1.1 cgd
1858 1.1 cgd void
1859 1.23 lukem abortpt(notused)
1860 1.23 lukem int notused;
1861 1.1 cgd {
1862 1.6 cgd
1863 1.22 lukem alarmtimer(0);
1864 1.35 lukem putc('\n', ttyout);
1865 1.1 cgd ptabflg++;
1866 1.1 cgd mflag = 0;
1867 1.1 cgd abrtflag = 0;
1868 1.1 cgd longjmp(ptabort, 1);
1869 1.1 cgd }
1870 1.1 cgd
1871 1.6 cgd void
1872 1.1 cgd proxtrans(cmd, local, remote)
1873 1.15 lukem const char *cmd, *local, *remote;
1874 1.1 cgd {
1875 1.1 cgd sig_t oldintr;
1876 1.26 lukem int prox_type, nfnd;
1877 1.26 lukem volatile int secndflag;
1878 1.1 cgd char *cmd2;
1879 1.1 cgd
1880 1.35 lukem #ifdef __GNUC__ /* to shut up gcc warnings */
1881 1.26 lukem (void)&oldintr;
1882 1.26 lukem (void)&cmd2;
1883 1.26 lukem #endif
1884 1.26 lukem
1885 1.26 lukem oldintr = NULL;
1886 1.26 lukem secndflag = 0;
1887 1.1 cgd if (strcmp(cmd, "RETR"))
1888 1.1 cgd cmd2 = "RETR";
1889 1.1 cgd else
1890 1.1 cgd cmd2 = runique ? "STOU" : "STOR";
1891 1.1 cgd if ((prox_type = type) == 0) {
1892 1.1 cgd if (unix_server && unix_proxy)
1893 1.1 cgd prox_type = TYPE_I;
1894 1.1 cgd else
1895 1.1 cgd prox_type = TYPE_A;
1896 1.1 cgd }
1897 1.1 cgd if (curtype != prox_type)
1898 1.1 cgd changetype(prox_type, 1);
1899 1.1 cgd if (command("PASV") != COMPLETE) {
1900 1.35 lukem fputs("proxy server does not support third party transfers.\n",
1901 1.35 lukem ttyout);
1902 1.1 cgd return;
1903 1.1 cgd }
1904 1.1 cgd pswitch(0);
1905 1.1 cgd if (!connected) {
1906 1.35 lukem fputs("No primary connection.\n", ttyout);
1907 1.1 cgd pswitch(1);
1908 1.1 cgd code = -1;
1909 1.1 cgd return;
1910 1.1 cgd }
1911 1.1 cgd if (curtype != prox_type)
1912 1.1 cgd changetype(prox_type, 1);
1913 1.1 cgd if (command("PORT %s", pasv) != COMPLETE) {
1914 1.1 cgd pswitch(1);
1915 1.1 cgd return;
1916 1.1 cgd }
1917 1.1 cgd if (setjmp(ptabort))
1918 1.1 cgd goto abort;
1919 1.67 lukem oldintr = xsignal(SIGINT, abortpt);
1920 1.41 lukem if ((restart_point &&
1921 1.41 lukem #ifndef NO_QUAD
1922 1.66 lukem (command("REST %lld", (long long) restart_point) != CONTINUE)
1923 1.41 lukem #else
1924 1.41 lukem (command("REST %ld", (long) restart_point) != CONTINUE)
1925 1.41 lukem #endif
1926 1.41 lukem ) || (command("%s %s", cmd, remote) != PRELIM)) {
1927 1.67 lukem (void)xsignal(SIGINT, oldintr);
1928 1.1 cgd pswitch(1);
1929 1.1 cgd return;
1930 1.1 cgd }
1931 1.1 cgd sleep(2);
1932 1.1 cgd pswitch(1);
1933 1.1 cgd secndflag++;
1934 1.41 lukem if ((restart_point &&
1935 1.41 lukem #ifndef NO_QUAD
1936 1.66 lukem (command("REST %lld", (long long) restart_point) != CONTINUE)
1937 1.41 lukem #else
1938 1.41 lukem (command("REST %ld", (long) restart_point) != CONTINUE)
1939 1.41 lukem #endif
1940 1.41 lukem ) || (command("%s %s", cmd2, local) != PRELIM))
1941 1.1 cgd goto abort;
1942 1.1 cgd ptflag++;
1943 1.23 lukem (void)getreply(0);
1944 1.1 cgd pswitch(0);
1945 1.23 lukem (void)getreply(0);
1946 1.67 lukem (void)xsignal(SIGINT, oldintr);
1947 1.1 cgd pswitch(1);
1948 1.1 cgd ptflag = 0;
1949 1.35 lukem fprintf(ttyout, "local: %s remote: %s\n", local, remote);
1950 1.1 cgd return;
1951 1.1 cgd abort:
1952 1.67 lukem (void)xsignal(SIGINT, SIG_IGN);
1953 1.1 cgd ptflag = 0;
1954 1.1 cgd if (strcmp(cmd, "RETR") && !proxy)
1955 1.1 cgd pswitch(1);
1956 1.1 cgd else if (!strcmp(cmd, "RETR") && proxy)
1957 1.1 cgd pswitch(0);
1958 1.1 cgd if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1959 1.1 cgd if (command("%s %s", cmd2, local) != PRELIM) {
1960 1.1 cgd pswitch(0);
1961 1.1 cgd if (cpend)
1962 1.31 lukem abort_remote(NULL);
1963 1.1 cgd }
1964 1.1 cgd pswitch(1);
1965 1.1 cgd if (ptabflg)
1966 1.1 cgd code = -1;
1967 1.67 lukem (void)xsignal(SIGINT, oldintr);
1968 1.1 cgd return;
1969 1.1 cgd }
1970 1.1 cgd if (cpend)
1971 1.31 lukem abort_remote(NULL);
1972 1.1 cgd pswitch(!proxy);
1973 1.1 cgd if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1974 1.1 cgd if (command("%s %s", cmd2, local) != PRELIM) {
1975 1.1 cgd pswitch(0);
1976 1.1 cgd if (cpend)
1977 1.31 lukem abort_remote(NULL);
1978 1.1 cgd pswitch(1);
1979 1.1 cgd if (ptabflg)
1980 1.1 cgd code = -1;
1981 1.67 lukem (void)xsignal(SIGINT, oldintr);
1982 1.1 cgd return;
1983 1.1 cgd }
1984 1.1 cgd }
1985 1.1 cgd if (cpend)
1986 1.31 lukem abort_remote(NULL);
1987 1.1 cgd pswitch(!proxy);
1988 1.1 cgd if (cpend) {
1989 1.45 christos if ((nfnd = empty(cin, NULL, 10)) <= 0) {
1990 1.1 cgd if (nfnd < 0) {
1991 1.6 cgd warn("abort");
1992 1.1 cgd }
1993 1.1 cgd if (ptabflg)
1994 1.1 cgd code = -1;
1995 1.1 cgd lostpeer();
1996 1.1 cgd }
1997 1.23 lukem (void)getreply(0);
1998 1.23 lukem (void)getreply(0);
1999 1.1 cgd }
2000 1.1 cgd if (proxy)
2001 1.1 cgd pswitch(0);
2002 1.1 cgd pswitch(1);
2003 1.1 cgd if (ptabflg)
2004 1.1 cgd code = -1;
2005 1.67 lukem (void)xsignal(SIGINT, oldintr);
2006 1.1 cgd }
2007 1.1 cgd
2008 1.6 cgd void
2009 1.6 cgd reset(argc, argv)
2010 1.6 cgd int argc;
2011 1.6 cgd char *argv[];
2012 1.1 cgd {
2013 1.1 cgd int nfnd = 1;
2014 1.1 cgd
2015 1.1 cgd while (nfnd > 0) {
2016 1.45 christos if ((nfnd = empty(cin, NULL, 0)) < 0) {
2017 1.6 cgd warn("reset");
2018 1.1 cgd code = -1;
2019 1.1 cgd lostpeer();
2020 1.1 cgd }
2021 1.1 cgd else if (nfnd) {
2022 1.23 lukem (void)getreply(0);
2023 1.1 cgd }
2024 1.1 cgd }
2025 1.1 cgd }
2026 1.1 cgd
2027 1.1 cgd char *
2028 1.1 cgd gunique(local)
2029 1.15 lukem const char *local;
2030 1.1 cgd {
2031 1.1 cgd static char new[MAXPATHLEN];
2032 1.6 cgd char *cp = strrchr(local, '/');
2033 1.68 lukem int d, count=0, len;
2034 1.1 cgd char ext = '1';
2035 1.1 cgd
2036 1.1 cgd if (cp)
2037 1.1 cgd *cp = '\0';
2038 1.28 lukem d = access(cp == local ? "/" : cp ? local : ".", W_OK);
2039 1.1 cgd if (cp)
2040 1.1 cgd *cp = '/';
2041 1.1 cgd if (d < 0) {
2042 1.6 cgd warn("local: %s", local);
2043 1.31 lukem return (NULL);
2044 1.1 cgd }
2045 1.68 lukem len = strlcpy(new, local, sizeof(new));
2046 1.68 lukem cp = &new[len];
2047 1.68 lukem *cp++ = '.';
2048 1.1 cgd while (!d) {
2049 1.1 cgd if (++count == 100) {
2050 1.35 lukem fputs("runique: can't find unique file name.\n",
2051 1.35 lukem ttyout);
2052 1.31 lukem return (NULL);
2053 1.1 cgd }
2054 1.1 cgd *cp++ = ext;
2055 1.1 cgd *cp = '\0';
2056 1.1 cgd if (ext == '9')
2057 1.1 cgd ext = '0';
2058 1.1 cgd else
2059 1.1 cgd ext++;
2060 1.28 lukem if ((d = access(new, F_OK)) < 0)
2061 1.1 cgd break;
2062 1.1 cgd if (ext != '0')
2063 1.1 cgd cp--;
2064 1.1 cgd else if (*(cp - 2) == '.')
2065 1.1 cgd *(cp - 1) = '1';
2066 1.1 cgd else {
2067 1.1 cgd *(cp - 2) = *(cp - 2) + 1;
2068 1.1 cgd cp--;
2069 1.1 cgd }
2070 1.1 cgd }
2071 1.6 cgd return (new);
2072 1.1 cgd }
2073 1.1 cgd
2074 1.6 cgd void
2075 1.1 cgd abort_remote(din)
2076 1.6 cgd FILE *din;
2077 1.1 cgd {
2078 1.65 lukem char buf[BUFSIZ];
2079 1.1 cgd int nfnd;
2080 1.1 cgd
2081 1.23 lukem if (cout == NULL) {
2082 1.23 lukem warnx("Lost control connection for abort.");
2083 1.23 lukem if (ptabflg)
2084 1.23 lukem code = -1;
2085 1.23 lukem lostpeer();
2086 1.23 lukem return;
2087 1.23 lukem }
2088 1.1 cgd /*
2089 1.1 cgd * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
2090 1.1 cgd * after urgent byte rather than before as is protocol now
2091 1.1 cgd */
2092 1.63 lukem buf[0] = IAC;
2093 1.63 lukem buf[1] = IP;
2094 1.63 lukem buf[2] = IAC;
2095 1.1 cgd if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
2096 1.6 cgd warn("abort");
2097 1.15 lukem fprintf(cout, "%cABOR\r\n", DM);
2098 1.23 lukem (void)fflush(cout);
2099 1.45 christos if ((nfnd = empty(cin, din, 10)) <= 0) {
2100 1.1 cgd if (nfnd < 0) {
2101 1.6 cgd warn("abort");
2102 1.1 cgd }
2103 1.1 cgd if (ptabflg)
2104 1.1 cgd code = -1;
2105 1.1 cgd lostpeer();
2106 1.1 cgd }
2107 1.45 christos if (din && (nfnd & 2)) {
2108 1.1 cgd while (read(fileno(din), buf, BUFSIZ) > 0)
2109 1.45 christos continue;
2110 1.1 cgd }
2111 1.1 cgd if (getreply(0) == ERROR && code == 552) {
2112 1.1 cgd /* 552 needed for nic style abort */
2113 1.23 lukem (void)getreply(0);
2114 1.1 cgd }
2115 1.23 lukem (void)getreply(0);
2116 1.1 cgd }
2117