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