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