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