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