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