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