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