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