[BACK]Return to ftp.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ftp

Annotation of src/usr.bin/ftp/ftp.c, Revision 1.70

1.70    ! pyr         1: /*     $OpenBSD: ftp.c,v 1.69 2008/04/12 21:20:58 ray Exp $    */
1.22      millert     2: /*     $NetBSD: ftp.c,v 1.27 1997/08/18 10:20:23 lukem Exp $   */
1.1       deraadt     3:
                      4: /*
1.34      itojun      5:  * Copyright (C) 1997 and 1998 WIDE Project.
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. Neither the name of the project nor the names of its contributors
                     17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*
1.1       deraadt    34:  * Copyright (c) 1985, 1989, 1993, 1994
                     35:  *     The Regents of the University of California.  All rights reserved.
                     36:  *
                     37:  * Redistribution and use in source and binary forms, with or without
                     38:  * modification, are permitted provided that the following conditions
                     39:  * are met:
                     40:  * 1. Redistributions of source code must retain the above copyright
                     41:  *    notice, this list of conditions and the following disclaimer.
                     42:  * 2. Redistributions in binary form must reproduce the above copyright
                     43:  *    notice, this list of conditions and the following disclaimer in the
                     44:  *    documentation and/or other materials provided with the distribution.
1.53      millert    45:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    46:  *    may be used to endorse or promote products derived from this software
                     47:  *    without specific prior written permission.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     50:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     51:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     52:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     53:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     54:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     55:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     56:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     57:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     58:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     59:  * SUCH DAMAGE.
                     60:  */
                     61:
1.55      deraadt    62: #if !defined(lint) && !defined(SMALL)
1.70    ! pyr        63: static const char rcsid[] = "$OpenBSD: ftp.c,v 1.69 2008/04/12 21:20:58 ray Exp $";
1.55      deraadt    64: #endif /* not lint and not SMALL */
1.1       deraadt    65:
1.10      millert    66: #include <sys/types.h>
1.1       deraadt    67: #include <sys/stat.h>
                     68: #include <sys/socket.h>
                     69:
                     70: #include <netinet/in.h>
                     71: #include <netinet/in_systm.h>
                     72: #include <netinet/ip.h>
                     73: #include <arpa/inet.h>
                     74: #include <arpa/ftp.h>
                     75: #include <arpa/telnet.h>
                     76:
                     77: #include <ctype.h>
                     78: #include <err.h>
                     79: #include <errno.h>
                     80: #include <netdb.h>
1.54      millert    81: #include <poll.h>
                     82: #include <stdarg.h>
1.1       deraadt    83: #include <stdio.h>
                     84: #include <stdlib.h>
                     85: #include <string.h>
                     86: #include <unistd.h>
1.12      millert    87: #include <utime.h>
1.1       deraadt    88:
                     89: #include "ftp_var.h"
                     90:
1.34      itojun     91: union sockunion {
                     92:        struct sockinet {
                     93:                u_char si_len;
                     94:                u_char si_family;
                     95:                u_short si_port;
                     96:        } su_si;
                     97:        struct sockaddr_in  su_sin;
                     98:        struct sockaddr_in6 su_sin6;
                     99: };
                    100: #define su_len         su_si.si_len
                    101: #define su_family      su_si.si_family
                    102: #define su_port                su_si.si_port
                    103:
                    104: union sockunion myctladdr, hisctladdr, data_addr;
                    105:
1.1       deraadt   106: int    data = -1;
                    107: int    abrtflag = 0;
                    108: jmp_buf        ptabort;
                    109: int    ptabflg;
                    110: int    ptflag = 0;
                    111: off_t  restart_point = 0;
                    112:
                    113:
                    114: FILE   *cin, *cout;
                    115:
                    116: char *
1.56      deraadt   117: hookup(char *host, char *port)
1.1       deraadt   118: {
1.57      deraadt   119:        int s, tos, error;
1.10      millert   120:        static char hostnamebuf[MAXHOSTNAMELEN];
1.34      itojun    121:        struct addrinfo hints, *res, *res0;
1.51      itojun    122:        char hbuf[NI_MAXHOST];
1.34      itojun    123:        char *cause = "unknown";
1.57      deraadt   124:        socklen_t namelen;
1.34      itojun    125:
1.36      itojun    126:        epsv4bad = 0;
                    127:
1.34      itojun    128:        memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
                    129:        memset(&hints, 0, sizeof(hints));
                    130:        hints.ai_flags = AI_CANONNAME;
1.45      deraadt   131:        hints.ai_family = family;
1.34      itojun    132:        hints.ai_socktype = SOCK_STREAM;
                    133:        hints.ai_protocol = 0;
                    134:        error = getaddrinfo(host, port, &hints, &res0);
1.35      deraadt   135:        if (error == EAI_SERVICE) {
                    136:                /*
                    137:                 * If the services file is corrupt/missing, fall back
                    138:                 * on our hard-coded defines.
                    139:                 */
                    140:                char pbuf[NI_MAXSERV];
                    141:
                    142:                pbuf[0] = '\0';
                    143:                if (strcmp(port, "ftp") == 0)
                    144:                        snprintf(pbuf, sizeof(pbuf), "%d", FTP_PORT);
                    145:                else if (strcmp(port, "ftpgate") == 0)
                    146:                        snprintf(pbuf, sizeof(pbuf), "%d", GATE_PORT);
                    147:                else if (strcmp(port, "http") == 0)
                    148:                        snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);
1.63      deraadt   149: #ifndef SMALL
                    150:                else if (strcmp(port, "https") == 0)
                    151:                        snprintf(pbuf, sizeof(pbuf), "%d", HTTPS_PORT);
                    152: #endif
1.35      deraadt   153:                if (pbuf[0])
                    154:                        error = getaddrinfo(host, pbuf, &hints, &res0);
                    155:        }
1.34      itojun    156:        if (error) {
1.39      itojun    157:                if (error == EAI_SERVICE)
                    158:                        warnx("%s: bad port number `%s'", host, port);
                    159:                else
                    160:                        warnx("%s: %s", host, gai_strerror(error));
1.1       deraadt   161:                code = -1;
                    162:                return (0);
                    163:        }
1.34      itojun    164:
                    165:        if (res0->ai_canonname)
1.41      lebel     166:                strlcpy(hostnamebuf, res0->ai_canonname, sizeof(hostnamebuf));
1.34      itojun    167:        else
1.41      lebel     168:                strlcpy(hostnamebuf, host, sizeof(hostnamebuf));
1.34      itojun    169:        hostname = hostnamebuf;
                    170:
                    171:        s = -1;
                    172:        for (res = res0; res; res = res->ai_next) {
                    173: #if 0  /*old behavior*/
                    174:                if (res != res0)        /* not on the first address */
                    175: #else
                    176:                if (res0->ai_next)      /* if we have multiple possibilities */
                    177: #endif
                    178:                {
1.50      itojun    179:                        if (getnameinfo(res->ai_addr, res->ai_addrlen,
                    180:                            hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
                    181:                                strlcpy(hbuf, "unknown", sizeof(hbuf));
1.70    ! pyr       182:                        if (verbose)
        !           183:                                fprintf(ttyout, "Trying %s...\n", hbuf);
1.34      itojun    184:                }
                    185:                s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
                    186:                if (s < 0) {
                    187:                        cause = "socket";
1.27      deraadt   188:                        continue;
1.34      itojun    189:                }
                    190:                while ((error = connect(s, res->ai_addr, res->ai_addrlen)) < 0
                    191:                                && errno == EINTR) {
                    192:                        ;
                    193:                }
                    194:                if (error) {
                    195:                        /* this "if" clause is to prevent print warning twice */
                    196:                        if (res->ai_next) {
1.50      itojun    197:                                if (getnameinfo(res->ai_addr, res->ai_addrlen,
                    198:                                    hbuf, sizeof(hbuf), NULL, 0,
                    199:                                    NI_NUMERICHOST) != 0)
                    200:                                        strlcpy(hbuf, "(unknown)",
                    201:                                            sizeof(hbuf));
1.34      itojun    202:                                warn("connect to address %s", hbuf);
1.1       deraadt   203:                        }
1.34      itojun    204:                        cause = "connect";
1.60      moritz    205:                        error = errno;
1.34      itojun    206:                        close(s);
1.60      moritz    207:                        errno = error;
1.34      itojun    208:                        s = -1;
1.1       deraadt   209:                        continue;
                    210:                }
1.34      itojun    211:
                    212:                /* finally we got one */
                    213:                break;
                    214:        }
                    215:        if (s < 0) {
1.38      millert   216:                warn("%s", cause);
1.1       deraadt   217:                code = -1;
1.34      itojun    218:                freeaddrinfo(res0);
                    219:                return 0;
1.1       deraadt   220:        }
1.34      itojun    221:        memcpy(&hisctladdr, res->ai_addr, res->ai_addrlen);
1.57      deraadt   222:        namelen = res->ai_addrlen;
1.34      itojun    223:        freeaddrinfo(res0);
                    224:        res0 = res = NULL;
1.57      deraadt   225:        if (getsockname(s, (struct sockaddr *)&myctladdr, &namelen) < 0) {
1.1       deraadt   226:                warn("getsockname");
                    227:                code = -1;
                    228:                goto bad;
                    229:        }
1.34      itojun    230: #if defined(IPPROTO_IP) && defined(IP_TOS)
                    231:        if (hisctladdr.su_family == AF_INET) {
                    232:                tos = IPTOS_LOWDELAY;
                    233:                if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
                    234:                        warn("setsockopt TOS (ignored)");
                    235:        }
1.1       deraadt   236: #endif
                    237:        cin = fdopen(s, "r");
                    238:        cout = fdopen(s, "w");
                    239:        if (cin == NULL || cout == NULL) {
                    240:                warnx("fdopen failed.");
                    241:                if (cin)
1.11      millert   242:                        (void)fclose(cin);
1.1       deraadt   243:                if (cout)
1.11      millert   244:                        (void)fclose(cout);
1.1       deraadt   245:                code = -1;
                    246:                goto bad;
                    247:        }
                    248:        if (verbose)
1.18      deraadt   249:                fprintf(ttyout, "Connected to %s.\n", hostname);
1.1       deraadt   250:        if (getreply(0) > 2) {  /* read startup message from server */
                    251:                if (cin)
1.11      millert   252:                        (void)fclose(cin);
1.1       deraadt   253:                if (cout)
1.11      millert   254:                        (void)fclose(cout);
1.1       deraadt   255:                code = -1;
                    256:                goto bad;
                    257:        }
                    258: #ifdef SO_OOBINLINE
                    259:        {
                    260:        int on = 1;
                    261:
                    262:        if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
                    263:                < 0 && debug) {
                    264:                        warn("setsockopt");
                    265:                }
                    266:        }
                    267: #endif /* SO_OOBINLINE */
                    268:
                    269:        return (hostname);
                    270: bad:
1.11      millert   271:        (void)close(s);
1.1       deraadt   272:        return ((char *)0);
                    273: }
                    274:
1.57      deraadt   275: /* ARGSUSED */
1.1       deraadt   276: void
1.57      deraadt   277: cmdabort(int signo)
1.1       deraadt   278: {
                    279:
1.10      millert   280:        alarmtimer(0);
1.18      deraadt   281:        putc('\n', ttyout);
                    282:        (void)fflush(ttyout);
1.1       deraadt   283:        abrtflag++;
                    284:        if (ptflag)
1.10      millert   285:                longjmp(ptabort, 1);
1.1       deraadt   286: }
                    287:
                    288: /*VARARGS*/
                    289: int
1.13      millert   290: command(const char *fmt, ...)
1.1       deraadt   291: {
                    292:        va_list ap;
                    293:        int r;
                    294:        sig_t oldintr;
                    295:
                    296:        abrtflag = 0;
                    297:        if (debug) {
1.18      deraadt   298:                fputs("---> ", ttyout);
1.13      millert   299:                va_start(ap, fmt);
1.1       deraadt   300:                if (strncmp("PASS ", fmt, 5) == 0)
1.18      deraadt   301:                        fputs("PASS XXXX", ttyout);
1.10      millert   302:                else if (strncmp("ACCT ", fmt, 5) == 0)
1.18      deraadt   303:                        fputs("ACCT XXXX", ttyout);
1.10      millert   304:                else
1.28      millert   305:                        vfprintf(ttyout, fmt, ap);
1.1       deraadt   306:                va_end(ap);
1.18      deraadt   307:                putc('\n', ttyout);
                    308:                (void)fflush(ttyout);
1.1       deraadt   309:        }
                    310:        if (cout == NULL) {
1.13      millert   311:                warnx("No control connection for command.");
1.1       deraadt   312:                code = -1;
                    313:                return (0);
                    314:        }
                    315:        oldintr = signal(SIGINT, cmdabort);
1.13      millert   316:        va_start(ap, fmt);
1.1       deraadt   317:        vfprintf(cout, fmt, ap);
                    318:        va_end(ap);
1.11      millert   319:        fputs("\r\n", cout);
                    320:        (void)fflush(cout);
1.1       deraadt   321:        cpend = 1;
                    322:        r = getreply(!strcmp(fmt, "QUIT"));
                    323:        if (abrtflag && oldintr != SIG_IGN)
                    324:                (*oldintr)(SIGINT);
1.11      millert   325:        (void)signal(SIGINT, oldintr);
1.1       deraadt   326:        return (r);
                    327: }
                    328:
1.68      espie     329: int keep_alive_timeout = 60;           /* 0 -> no timeout */
1.67      espie     330:
                    331: static int full_noops_sent = 0;
                    332: static time_t last_timestamp = 0;      /* 0 -> no measurement yet */
                    333: static char noop[] = "NOOP\r\n";
                    334: #define NOOP_LENGTH (sizeof noop - 1)
                    335: static int current_nop_pos = 0;                /* 0 -> no noop started */
                    336:
                    337: /* to achieve keep alive, we send noop one byte at a time */
                    338: void
                    339: send_noop_char()
                    340: {
                    341:        if (debug)
                    342:                fprintf(ttyout, "---> %c\n", noop[current_nop_pos]);
                    343:        fputc(noop[current_nop_pos++], cout);
                    344:        (void)fflush(cout);
                    345:        if (current_nop_pos >= NOOP_LENGTH) {
                    346:                full_noops_sent++;
                    347:                current_nop_pos = 0;
                    348:        }
                    349: }
                    350:
                    351: void
                    352: may_reset_noop_timeout()
                    353: {
                    354:        if (keep_alive_timeout != 0)
                    355:                last_timestamp = time(NULL);
                    356: }
                    357:
                    358: void
                    359: may_receive_noop_ack()
                    360: {
                    361:        int i;
                    362:
                    363:        /* finish sending last incomplete noop */
                    364:        if (current_nop_pos != 0) {
                    365:                fputs(&(noop[current_nop_pos]), cout);
                    366:                if (debug)
                    367:                        fprintf(ttyout, "---> %s\n", &(noop[current_nop_pos]));
                    368:                (void)fflush(cout);
                    369:                current_nop_pos = 0;
                    370:                full_noops_sent++;
                    371:        }
                    372:        /* and get the replies */
                    373:        for (i = 0; i < full_noops_sent; i++)
                    374:                (void)getreply(0);
                    375:
                    376:        full_noops_sent = 0;
                    377: }
                    378:
                    379: void
                    380: may_send_noop_char()
                    381: {
                    382:        if (keep_alive_timeout != 0) {
                    383:                if (last_timestamp != 0) {
                    384:                        time_t t = time(NULL);
                    385:
                    386:                        if (t - last_timestamp >= keep_alive_timeout) {
                    387:                                last_timestamp = t;
                    388:                                send_noop_char();
                    389:                        }
                    390:                } else {
                    391:                        last_timestamp = time(NULL);
                    392:                }
                    393:        }
                    394: }
                    395:
1.10      millert   396: char reply_string[BUFSIZ];             /* first line of previous reply */
1.1       deraadt   397:
                    398: int
1.56      deraadt   399: getreply(int expecteof)
1.1       deraadt   400: {
1.10      millert   401:        char current_line[BUFSIZ];      /* last line of previous reply */
1.64      ray       402:        int c, n, lineno;
1.1       deraadt   403:        int dig;
                    404:        int originalcode = 0, continuation = 0;
                    405:        sig_t oldintr;
                    406:        int pflag = 0;
                    407:        char *cp, *pt = pasv;
                    408:
1.25      weingart  409:        memset(current_line, 0, sizeof(current_line));
1.1       deraadt   410:        oldintr = signal(SIGINT, cmdabort);
1.64      ray       411:        for (lineno = 0 ;; lineno++) {
1.1       deraadt   412:                dig = n = code = 0;
1.10      millert   413:                cp = current_line;
1.23      millert   414:                while ((c = fgetc(cin)) != '\n') {
1.1       deraadt   415:                        if (c == IAC) {     /* handle telnet commands */
1.23      millert   416:                                switch (c = fgetc(cin)) {
1.1       deraadt   417:                                case WILL:
                    418:                                case WONT:
1.23      millert   419:                                        c = fgetc(cin);
1.1       deraadt   420:                                        fprintf(cout, "%c%c%c", IAC, DONT, c);
1.11      millert   421:                                        (void)fflush(cout);
1.1       deraadt   422:                                        break;
                    423:                                case DO:
                    424:                                case DONT:
1.23      millert   425:                                        c = fgetc(cin);
1.1       deraadt   426:                                        fprintf(cout, "%c%c%c", IAC, WONT, c);
1.11      millert   427:                                        (void)fflush(cout);
1.1       deraadt   428:                                        break;
                    429:                                default:
                    430:                                        break;
                    431:                                }
                    432:                                continue;
                    433:                        }
                    434:                        dig++;
                    435:                        if (c == EOF) {
                    436:                                if (expecteof) {
1.11      millert   437:                                        (void)signal(SIGINT, oldintr);
1.1       deraadt   438:                                        code = 221;
                    439:                                        return (0);
                    440:                                }
                    441:                                lostpeer();
                    442:                                if (verbose) {
1.18      deraadt   443:                                        fputs(
                    444: "421 Service not available, remote server has closed connection.\n", ttyout);
                    445:                                        (void)fflush(ttyout);
1.1       deraadt   446:                                }
                    447:                                code = 421;
                    448:                                return (4);
                    449:                        }
1.10      millert   450:                        if (c != '\r' && (verbose > 0 ||
1.21      mickey    451:                            ((verbose > -1 && n == '5' && dig > 4)) &&
                    452:                            (((!n && c < '5') || (n && n < '5'))
                    453:                             || !retry_connect))) {
1.1       deraadt   454:                                if (proxflag &&
1.10      millert   455:                                   (dig == 1 || (dig == 5 && verbose == 0)))
1.18      deraadt   456:                                        fprintf(ttyout, "%s:", hostname);
                    457:                                (void)putc(c, ttyout);
1.1       deraadt   458:                        }
                    459:                        if (dig < 4 && isdigit(c))
                    460:                                code = code * 10 + (c - '0');
1.34      itojun    461:                        if (!pflag && (code == 227 || code == 228))
1.1       deraadt   462:                                pflag = 1;
1.34      itojun    463:                        else if (!pflag && code == 229)
                    464:                                pflag = 100;
1.1       deraadt   465:                        if (dig > 4 && pflag == 1 && isdigit(c))
                    466:                                pflag = 2;
                    467:                        if (pflag == 2) {
1.44      itojun    468:                                if (c != '\r' && c != ')') {
                    469:                                        if (pt < &pasv[sizeof(pasv) - 1])
                    470:                                                *pt++ = c;
                    471:                                } else {
1.1       deraadt   472:                                        *pt = '\0';
                    473:                                        pflag = 3;
                    474:                                }
                    475:                        }
1.34      itojun    476:                        if (pflag == 100 && c == '(')
                    477:                                pflag = 2;
1.1       deraadt   478:                        if (dig == 4 && c == '-') {
                    479:                                if (continuation)
                    480:                                        code = 0;
                    481:                                continuation++;
                    482:                        }
1.10      millert   483:                        if (n == 0)
                    484:                                n = c;
                    485:                        if (cp < &current_line[sizeof(current_line) - 1])
1.1       deraadt   486:                                *cp++ = c;
                    487:                }
1.11      millert   488:                if (verbose > 0 || ((verbose > -1 && n == '5') &&
                    489:                    (n < '5' || !retry_connect))) {
1.18      deraadt   490:                        (void)putc(c, ttyout);
                    491:                        (void)fflush (ttyout);
1.1       deraadt   492:                }
1.64      ray       493:                if (lineno == 0) {
1.10      millert   494:                        size_t len = cp - current_line;
                    495:
                    496:                        if (len > sizeof(reply_string))
                    497:                                len = sizeof(reply_string);
                    498:
1.48      itojun    499:                        (void)strlcpy(reply_string, current_line, len);
1.10      millert   500:                }
1.1       deraadt   501:                if (continuation && code != originalcode) {
                    502:                        if (originalcode == 0)
                    503:                                originalcode = code;
                    504:                        continue;
                    505:                }
                    506:                *cp = '\0';
                    507:                if (n != '1')
                    508:                        cpend = 0;
1.11      millert   509:                (void)signal(SIGINT, oldintr);
1.1       deraadt   510:                if (code == 421 || originalcode == 421)
                    511:                        lostpeer();
                    512:                if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
                    513:                        (*oldintr)(SIGINT);
                    514:                return (n - '0');
                    515:        }
                    516: }
                    517:
                    518: jmp_buf        sendabort;
                    519:
1.57      deraadt   520: /* ARGSUSED */
1.1       deraadt   521: void
1.57      deraadt   522: abortsend(int signo)
1.1       deraadt   523: {
                    524:
1.10      millert   525:        alarmtimer(0);
1.1       deraadt   526:        mflag = 0;
                    527:        abrtflag = 0;
1.18      deraadt   528:        fputs("\nsend aborted\nwaiting for remote to finish abort.\n", ttyout);
                    529:        (void)fflush(ttyout);
1.1       deraadt   530:        longjmp(sendabort, 1);
                    531: }
                    532:
                    533: void
1.56      deraadt   534: sendrequest(const char *cmd, const char *local, const char *remote,
                    535:     int printnames)
1.1       deraadt   536: {
                    537:        struct stat st;
                    538:        int c, d;
1.40      millert   539:        FILE * volatile fin, * volatile dout;
1.42      millert   540:        int (* volatile closefunc)(FILE *);
1.40      millert   541:        volatile sig_t oldinti, oldintr, oldintp;
1.20      millert   542:        volatile off_t hashbytes;
1.40      millert   543:        char * volatile lmode;
                    544:        char buf[BUFSIZ], *bufp;
1.69      ray       545:        int oprogress, serrno;
1.1       deraadt   546:
1.10      millert   547:        hashbytes = mark;
                    548:        direction = "sent";
1.20      millert   549:        dout = NULL;
1.10      millert   550:        bytes = 0;
                    551:        filesize = -1;
1.13      millert   552:        oprogress = progress;
1.1       deraadt   553:        if (verbose && printnames) {
                    554:                if (local && *local != '-')
1.18      deraadt   555:                        fprintf(ttyout, "local: %s ", local);
1.1       deraadt   556:                if (remote)
1.18      deraadt   557:                        fprintf(ttyout, "remote: %s\n", remote);
1.1       deraadt   558:        }
                    559:        if (proxy) {
                    560:                proxtrans(cmd, local, remote);
                    561:                return;
                    562:        }
                    563:        if (curtype != type)
                    564:                changetype(type, 0);
                    565:        closefunc = NULL;
                    566:        oldintr = NULL;
                    567:        oldintp = NULL;
1.10      millert   568:        oldinti = NULL;
1.1       deraadt   569:        lmode = "w";
                    570:        if (setjmp(sendabort)) {
                    571:                while (cpend) {
1.11      millert   572:                        (void)getreply(0);
1.1       deraadt   573:                }
                    574:                if (data >= 0) {
1.11      millert   575:                        (void)close(data);
1.1       deraadt   576:                        data = -1;
                    577:                }
                    578:                if (oldintr)
1.11      millert   579:                        (void)signal(SIGINT, oldintr);
1.1       deraadt   580:                if (oldintp)
1.11      millert   581:                        (void)signal(SIGPIPE, oldintp);
1.10      millert   582:                if (oldinti)
1.11      millert   583:                        (void)signal(SIGINFO, oldinti);
1.13      millert   584:                progress = oprogress;
1.1       deraadt   585:                code = -1;
                    586:                return;
                    587:        }
                    588:        oldintr = signal(SIGINT, abortsend);
1.10      millert   589:        oldinti = signal(SIGINFO, psummary);
1.13      millert   590:        if (strcmp(local, "-") == 0) {
1.1       deraadt   591:                fin = stdin;
1.31      millert   592:                if (progress == 1)
                    593:                        progress = 0;
1.13      millert   594:        } else if (*local == '|') {
1.10      millert   595:                oldintp = signal(SIGPIPE, SIG_IGN);
1.1       deraadt   596:                fin = popen(local + 1, "r");
                    597:                if (fin == NULL) {
                    598:                        warn("%s", local + 1);
1.11      millert   599:                        (void)signal(SIGINT, oldintr);
                    600:                        (void)signal(SIGPIPE, oldintp);
                    601:                        (void)signal(SIGINFO, oldinti);
1.1       deraadt   602:                        code = -1;
                    603:                        return;
                    604:                }
1.31      millert   605:                if (progress == 1)
                    606:                        progress = 0;
1.1       deraadt   607:                closefunc = pclose;
                    608:        } else {
                    609:                fin = fopen(local, "r");
                    610:                if (fin == NULL) {
                    611:                        warn("local: %s", local);
1.11      millert   612:                        (void)signal(SIGINT, oldintr);
                    613:                        (void)signal(SIGINFO, oldinti);
1.1       deraadt   614:                        code = -1;
                    615:                        return;
                    616:                }
                    617:                closefunc = fclose;
                    618:                if (fstat(fileno(fin), &st) < 0 ||
1.14      millert   619:                    (st.st_mode & S_IFMT) != S_IFREG) {
1.18      deraadt   620:                        fprintf(ttyout, "%s: not a plain file.\n", local);
1.11      millert   621:                        (void)signal(SIGINT, oldintr);
                    622:                        (void)signal(SIGINFO, oldinti);
1.1       deraadt   623:                        fclose(fin);
                    624:                        code = -1;
                    625:                        return;
                    626:                }
1.10      millert   627:                filesize = st.st_size;
1.1       deraadt   628:        }
                    629:        if (initconn()) {
1.11      millert   630:                (void)signal(SIGINT, oldintr);
                    631:                (void)signal(SIGINFO, oldinti);
1.1       deraadt   632:                if (oldintp)
1.11      millert   633:                        (void)signal(SIGPIPE, oldintp);
1.1       deraadt   634:                code = -1;
1.13      millert   635:                progress = oprogress;
1.1       deraadt   636:                if (closefunc != NULL)
                    637:                        (*closefunc)(fin);
                    638:                return;
                    639:        }
                    640:        if (setjmp(sendabort))
                    641:                goto abort;
                    642:
                    643:        if (restart_point &&
                    644:            (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
1.59      deraadt   645:                int rc = -1;
1.1       deraadt   646:
                    647:                switch (curtype) {
                    648:                case TYPE_A:
1.58      sturm     649:                        rc = fseeko(fin, restart_point, SEEK_SET);
1.1       deraadt   650:                        break;
                    651:                case TYPE_I:
                    652:                case TYPE_L:
1.59      deraadt   653:                        if (lseek(fileno(fin), restart_point, SEEK_SET) != -1)
                    654:                                rc = 0;
1.1       deraadt   655:                        break;
                    656:                }
1.59      deraadt   657:                if (rc == -1) {
1.1       deraadt   658:                        warn("local: %s", local);
                    659:                        restart_point = 0;
1.13      millert   660:                        progress = oprogress;
1.1       deraadt   661:                        if (closefunc != NULL)
                    662:                                (*closefunc)(fin);
                    663:                        return;
                    664:                }
1.58      sturm     665:                if (command("REST %lld", (long long) restart_point)
1.1       deraadt   666:                        != CONTINUE) {
                    667:                        restart_point = 0;
1.13      millert   668:                        progress = oprogress;
1.1       deraadt   669:                        if (closefunc != NULL)
                    670:                                (*closefunc)(fin);
                    671:                        return;
                    672:                }
                    673:                restart_point = 0;
                    674:                lmode = "r+w";
                    675:        }
                    676:        if (remote) {
                    677:                if (command("%s %s", cmd, remote) != PRELIM) {
1.11      millert   678:                        (void)signal(SIGINT, oldintr);
                    679:                        (void)signal(SIGINFO, oldinti);
1.13      millert   680:                        progress = oprogress;
1.1       deraadt   681:                        if (oldintp)
1.11      millert   682:                                (void)signal(SIGPIPE, oldintp);
1.1       deraadt   683:                        if (closefunc != NULL)
                    684:                                (*closefunc)(fin);
                    685:                        return;
                    686:                }
                    687:        } else
                    688:                if (command("%s", cmd) != PRELIM) {
1.11      millert   689:                        (void)signal(SIGINT, oldintr);
                    690:                        (void)signal(SIGINFO, oldinti);
1.13      millert   691:                        progress = oprogress;
1.1       deraadt   692:                        if (oldintp)
1.11      millert   693:                                (void)signal(SIGPIPE, oldintp);
1.1       deraadt   694:                        if (closefunc != NULL)
                    695:                                (*closefunc)(fin);
                    696:                        return;
                    697:                }
                    698:        dout = dataconn(lmode);
                    699:        if (dout == NULL)
                    700:                goto abort;
1.10      millert   701:        progressmeter(-1);
1.67      espie     702:        may_reset_noop_timeout();
1.1       deraadt   703:        oldintp = signal(SIGPIPE, SIG_IGN);
                    704:        switch (curtype) {
                    705:
                    706:        case TYPE_I:
                    707:        case TYPE_L:
1.69      ray       708:                d = 0;
1.11      millert   709:                while ((c = read(fileno(fin), buf, sizeof(buf))) > 0) {
1.67      espie     710:                        may_send_noop_char();
1.1       deraadt   711:                        bytes += c;
                    712:                        for (bufp = buf; c > 0; c -= d, bufp += d)
1.20      millert   713:                                if ((d = write(fileno(dout), bufp, (size_t)c))
                    714:                                    <= 0)
1.1       deraadt   715:                                        break;
1.10      millert   716:                        if (hash && (!progress || filesize < 0) ) {
1.1       deraadt   717:                                while (bytes >= hashbytes) {
1.18      deraadt   718:                                        (void)putc('#', ttyout);
1.7       kstailey  719:                                        hashbytes += mark;
1.1       deraadt   720:                                }
1.18      deraadt   721:                                (void)fflush(ttyout);
1.1       deraadt   722:                        }
                    723:                }
1.69      ray       724:                if (c == -1 || d == -1)
                    725:                        serrno = errno;
1.10      millert   726:                if (hash && (!progress || filesize < 0) && bytes > 0) {
1.7       kstailey  727:                        if (bytes < mark)
1.18      deraadt   728:                                (void)putc('#', ttyout);
                    729:                        (void)putc('\n', ttyout);
                    730:                        (void)fflush(ttyout);
1.1       deraadt   731:                }
                    732:                if (c < 0)
1.69      ray       733:                        warnx("local: %s: %s", local, strerror(serrno));
1.1       deraadt   734:                if (d < 0) {
1.69      ray       735:                        if (serrno != EPIPE)
                    736:                                warnx("netout: %s", strerror(serrno));
1.1       deraadt   737:                        bytes = -1;
                    738:                }
                    739:                break;
                    740:
                    741:        case TYPE_A:
1.23      millert   742:                while ((c = fgetc(fin)) != EOF) {
1.67      espie     743:                        may_send_noop_char();
1.1       deraadt   744:                        if (c == '\n') {
1.10      millert   745:                                while (hash && (!progress || filesize < 0) &&
                    746:                                    (bytes >= hashbytes)) {
1.18      deraadt   747:                                        (void)putc('#', ttyout);
                    748:                                        (void)fflush(ttyout);
1.7       kstailey  749:                                        hashbytes += mark;
1.1       deraadt   750:                                }
                    751:                                if (ferror(dout))
                    752:                                        break;
1.11      millert   753:                                (void)putc('\r', dout);
1.1       deraadt   754:                                bytes++;
                    755:                        }
1.11      millert   756:                        (void)putc(c, dout);
1.1       deraadt   757:                        bytes++;
1.10      millert   758: #if 0  /* this violates RFC */
                    759:                        if (c == '\r') {
                    760:                                (void)putc('\0', dout);
                    761:                                bytes++;
                    762:                        }
                    763: #endif
1.1       deraadt   764:                }
1.69      ray       765:                if (ferror(fin) || ferror(dout))
                    766:                        serrno = errno;
1.10      millert   767:                if (hash && (!progress || filesize < 0)) {
1.1       deraadt   768:                        if (bytes < hashbytes)
1.18      deraadt   769:                                (void)putc('#', ttyout);
                    770:                        (void)putc('\n', ttyout);
                    771:                        (void)fflush(ttyout);
1.1       deraadt   772:                }
                    773:                if (ferror(fin))
1.69      ray       774:                        warnx("local: %s: %s", local, strerror(serrno));
1.1       deraadt   775:                if (ferror(dout)) {
                    776:                        if (errno != EPIPE)
1.69      ray       777:                                warnx("netout: %s", strerror(serrno));
1.1       deraadt   778:                        bytes = -1;
                    779:                }
                    780:                break;
                    781:        }
1.10      millert   782:        progressmeter(1);
1.13      millert   783:        progress = oprogress;
1.1       deraadt   784:        if (closefunc != NULL)
                    785:                (*closefunc)(fin);
1.11      millert   786:        (void)fclose(dout);
                    787:        (void)getreply(0);
1.67      espie     788:        may_receive_noop_ack();
1.11      millert   789:        (void)signal(SIGINT, oldintr);
                    790:        (void)signal(SIGINFO, oldinti);
1.1       deraadt   791:        if (oldintp)
1.11      millert   792:                (void)signal(SIGPIPE, oldintp);
1.1       deraadt   793:        if (bytes > 0)
1.10      millert   794:                ptransfer(0);
1.1       deraadt   795:        return;
                    796: abort:
1.11      millert   797:        (void)signal(SIGINT, oldintr);
                    798:        (void)signal(SIGINFO, oldinti);
1.13      millert   799:        progress = oprogress;
1.1       deraadt   800:        if (oldintp)
1.11      millert   801:                (void)signal(SIGPIPE, oldintp);
1.1       deraadt   802:        if (!cpend) {
                    803:                code = -1;
                    804:                return;
                    805:        }
                    806:        if (data >= 0) {
1.11      millert   807:                (void)close(data);
1.1       deraadt   808:                data = -1;
                    809:        }
                    810:        if (dout)
1.11      millert   811:                (void)fclose(dout);
                    812:        (void)getreply(0);
1.1       deraadt   813:        code = -1;
                    814:        if (closefunc != NULL && fin != NULL)
                    815:                (*closefunc)(fin);
                    816:        if (bytes > 0)
1.10      millert   817:                ptransfer(0);
1.1       deraadt   818: }
                    819:
                    820: jmp_buf        recvabort;
                    821:
1.57      deraadt   822: /* ARGSUSED */
1.1       deraadt   823: void
1.57      deraadt   824: abortrecv(int signo)
1.1       deraadt   825: {
                    826:
1.10      millert   827:        alarmtimer(0);
1.1       deraadt   828:        mflag = 0;
                    829:        abrtflag = 0;
1.18      deraadt   830:        fputs("\nreceive aborted\nwaiting for remote to finish abort.\n", ttyout);
                    831:        (void)fflush(ttyout);
1.1       deraadt   832:        longjmp(recvabort, 1);
                    833: }
                    834:
                    835: void
1.56      deraadt   836: recvrequest(const char *cmd, const char * volatile local, const char *remote,
                    837:     const char *lmode, int printnames, int ignorespecial)
1.1       deraadt   838: {
1.40      millert   839:        FILE * volatile fout, * volatile din;
1.42      millert   840:        int (* volatile closefunc)(FILE *);
1.40      millert   841:        volatile sig_t oldinti, oldintr, oldintp;
1.69      ray       842:        int c, d, serrno;
1.20      millert   843:        volatile int is_retr, tcrflag, bare_lfs;
                    844:        static size_t bufsize;
1.1       deraadt   845:        static char *buf;
1.20      millert   846:        volatile off_t hashbytes;
1.1       deraadt   847:        struct stat st;
1.10      millert   848:        time_t mtime;
1.13      millert   849:        int oprogress;
1.16      millert   850:        int opreserve;
1.1       deraadt   851:
1.20      millert   852:        fout = NULL;
                    853:        din = NULL;
                    854:        oldinti = NULL;
1.10      millert   855:        hashbytes = mark;
                    856:        direction = "received";
                    857:        bytes = 0;
1.20      millert   858:        bare_lfs = 0;
1.10      millert   859:        filesize = -1;
1.13      millert   860:        oprogress = progress;
1.16      millert   861:        opreserve = preserve;
1.1       deraadt   862:        is_retr = strcmp(cmd, "RETR") == 0;
                    863:        if (is_retr && verbose && printnames) {
1.22      millert   864:                if (local && (ignorespecial || *local != '-'))
1.18      deraadt   865:                        fprintf(ttyout, "local: %s ", local);
1.1       deraadt   866:                if (remote)
1.18      deraadt   867:                        fprintf(ttyout, "remote: %s\n", remote);
1.1       deraadt   868:        }
                    869:        if (proxy && is_retr) {
                    870:                proxtrans(cmd, local, remote);
                    871:                return;
                    872:        }
                    873:        closefunc = NULL;
                    874:        oldintr = NULL;
                    875:        oldintp = NULL;
                    876:        tcrflag = !crflag && is_retr;
                    877:        if (setjmp(recvabort)) {
                    878:                while (cpend) {
1.11      millert   879:                        (void)getreply(0);
1.1       deraadt   880:                }
                    881:                if (data >= 0) {
1.11      millert   882:                        (void)close(data);
1.1       deraadt   883:                        data = -1;
                    884:                }
                    885:                if (oldintr)
1.11      millert   886:                        (void)signal(SIGINT, oldintr);
1.10      millert   887:                if (oldinti)
1.11      millert   888:                        (void)signal(SIGINFO, oldinti);
1.15      millert   889:                progress = oprogress;
1.16      millert   890:                preserve = opreserve;
1.1       deraadt   891:                code = -1;
                    892:                return;
                    893:        }
                    894:        oldintr = signal(SIGINT, abortrecv);
1.10      millert   895:        oldinti = signal(SIGINFO, psummary);
1.22      millert   896:        if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
                    897:                if (access(local, W_OK) < 0) {
1.69      ray       898:                        char *dir;
1.1       deraadt   899:
                    900:                        if (errno != ENOENT && errno != EACCES) {
                    901:                                warn("local: %s", local);
1.11      millert   902:                                (void)signal(SIGINT, oldintr);
                    903:                                (void)signal(SIGINFO, oldinti);
1.1       deraadt   904:                                code = -1;
                    905:                                return;
                    906:                        }
1.69      ray       907:                        dir = strrchr(local, '/');
1.1       deraadt   908:                        if (dir != NULL)
                    909:                                *dir = 0;
1.22      millert   910:                        d = access(dir == local ? "/" : dir ? local : ".", W_OK);
1.1       deraadt   911:                        if (dir != NULL)
                    912:                                *dir = '/';
                    913:                        if (d < 0) {
                    914:                                warn("local: %s", local);
1.11      millert   915:                                (void)signal(SIGINT, oldintr);
                    916:                                (void)signal(SIGINFO, oldinti);
1.1       deraadt   917:                                code = -1;
                    918:                                return;
                    919:                        }
                    920:                        if (!runique && errno == EACCES &&
1.20      millert   921:                            chmod(local, (S_IRUSR|S_IWUSR)) < 0) {
1.1       deraadt   922:                                warn("local: %s", local);
1.11      millert   923:                                (void)signal(SIGINT, oldintr);
                    924:                                (void)signal(SIGINFO, oldinti);
1.1       deraadt   925:                                code = -1;
                    926:                                return;
                    927:                        }
                    928:                        if (runique && errno == EACCES &&
                    929:                           (local = gunique(local)) == NULL) {
1.11      millert   930:                                (void)signal(SIGINT, oldintr);
                    931:                                (void)signal(SIGINFO, oldinti);
1.1       deraadt   932:                                code = -1;
                    933:                                return;
                    934:                        }
                    935:                }
                    936:                else if (runique && (local = gunique(local)) == NULL) {
1.11      millert   937:                        (void)signal(SIGINT, oldintr);
                    938:                        (void)signal(SIGINFO, oldinti);
1.1       deraadt   939:                        code = -1;
                    940:                        return;
                    941:                }
                    942:        }
                    943:        if (!is_retr) {
                    944:                if (curtype != TYPE_A)
                    945:                        changetype(TYPE_A, 0);
1.10      millert   946:        } else {
                    947:                if (curtype != type)
                    948:                        changetype(type, 0);
                    949:                filesize = remotesize(remote, 0);
                    950:        }
1.1       deraadt   951:        if (initconn()) {
1.11      millert   952:                (void)signal(SIGINT, oldintr);
                    953:                (void)signal(SIGINFO, oldinti);
1.1       deraadt   954:                code = -1;
                    955:                return;
                    956:        }
                    957:        if (setjmp(recvabort))
                    958:                goto abort;
                    959:        if (is_retr && restart_point &&
1.58      sturm     960:            command("REST %lld", (long long) restart_point) != CONTINUE)
1.1       deraadt   961:                return;
                    962:        if (remote) {
                    963:                if (command("%s %s", cmd, remote) != PRELIM) {
1.11      millert   964:                        (void)signal(SIGINT, oldintr);
                    965:                        (void)signal(SIGINFO, oldinti);
1.1       deraadt   966:                        return;
                    967:                }
                    968:        } else {
                    969:                if (command("%s", cmd) != PRELIM) {
1.11      millert   970:                        (void)signal(SIGINT, oldintr);
                    971:                        (void)signal(SIGINFO, oldinti);
1.1       deraadt   972:                        return;
                    973:                }
                    974:        }
                    975:        din = dataconn("r");
                    976:        if (din == NULL)
                    977:                goto abort;
1.22      millert   978:        if (!ignorespecial && strcmp(local, "-") == 0) {
1.1       deraadt   979:                fout = stdout;
1.16      millert   980:                preserve = 0;
1.22      millert   981:        } else if (!ignorespecial && *local == '|') {
1.1       deraadt   982:                oldintp = signal(SIGPIPE, SIG_IGN);
                    983:                fout = popen(local + 1, "w");
                    984:                if (fout == NULL) {
                    985:                        warn("%s", local+1);
                    986:                        goto abort;
                    987:                }
1.31      millert   988:                if (progress == 1)
                    989:                        progress = 0;
1.16      millert   990:                preserve = 0;
1.1       deraadt   991:                closefunc = pclose;
                    992:        } else {
                    993:                fout = fopen(local, lmode);
                    994:                if (fout == NULL) {
                    995:                        warn("local: %s", local);
                    996:                        goto abort;
                    997:                }
                    998:                closefunc = fclose;
                    999:        }
                   1000:        if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
                   1001:                st.st_blksize = BUFSIZ;
                   1002:        if (st.st_blksize > bufsize) {
1.65      steven   1003:                (void)free(buf);
1.1       deraadt  1004:                buf = malloc((unsigned)st.st_blksize);
                   1005:                if (buf == NULL) {
                   1006:                        warn("malloc");
                   1007:                        bufsize = 0;
                   1008:                        goto abort;
                   1009:                }
                   1010:                bufsize = st.st_blksize;
                   1011:        }
1.14      millert  1012:        if ((st.st_mode & S_IFMT) != S_IFREG) {
1.31      millert  1013:                if (progress == 1)
                   1014:                        progress = 0;
1.13      millert  1015:                preserve = 0;
                   1016:        }
1.10      millert  1017:        progressmeter(-1);
1.67      espie    1018:        may_reset_noop_timeout();
1.1       deraadt  1019:        switch (curtype) {
                   1020:
                   1021:        case TYPE_I:
                   1022:        case TYPE_L:
                   1023:                if (restart_point &&
                   1024:                    lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
                   1025:                        warn("local: %s", local);
1.13      millert  1026:                        progress = oprogress;
1.16      millert  1027:                        preserve = opreserve;
1.1       deraadt  1028:                        if (closefunc != NULL)
                   1029:                                (*closefunc)(fout);
                   1030:                        return;
                   1031:                }
                   1032:                errno = d = 0;
                   1033:                while ((c = read(fileno(din), buf, bufsize)) > 0) {
1.57      deraadt  1034:                        ssize_t wr;
1.32      deraadt  1035:                        size_t  rd = c;
                   1036:
1.67      espie    1037:                        may_send_noop_char();
1.32      deraadt  1038:                        d = 0;
                   1039:                        do {
                   1040:                                wr = write(fileno(fout), buf + d, rd);
                   1041:                                if (wr == -1 && errno == EPIPE)
                   1042:                                        break;
                   1043:                                d += wr;
                   1044:                                rd -= wr;
                   1045:                        } while (d < c);
                   1046:                        if (rd != 0)
1.1       deraadt  1047:                                break;
                   1048:                        bytes += c;
1.10      millert  1049:                        if (hash && (!progress || filesize < 0)) {
1.1       deraadt  1050:                                while (bytes >= hashbytes) {
1.18      deraadt  1051:                                        (void)putc('#', ttyout);
1.7       kstailey 1052:                                        hashbytes += mark;
1.1       deraadt  1053:                                }
1.18      deraadt  1054:                                (void)fflush(ttyout);
1.1       deraadt  1055:                        }
                   1056:                }
1.69      ray      1057:                if (c == -1 || d < c)
                   1058:                        serrno = errno;
1.10      millert  1059:                if (hash && (!progress || filesize < 0) && bytes > 0) {
1.7       kstailey 1060:                        if (bytes < mark)
1.18      deraadt  1061:                                (void)putc('#', ttyout);
                   1062:                        (void)putc('\n', ttyout);
                   1063:                        (void)fflush(ttyout);
1.1       deraadt  1064:                }
                   1065:                if (c < 0) {
1.69      ray      1066:                        if (serrno != EPIPE)
                   1067:                                warnx("netin: %s", strerror(serrno));
1.1       deraadt  1068:                        bytes = -1;
                   1069:                }
                   1070:                if (d < c) {
                   1071:                        if (d < 0)
1.69      ray      1072:                                warnx("local: %s: %s", local, strerror(serrno));
1.1       deraadt  1073:                        else
                   1074:                                warnx("%s: short write", local);
                   1075:                }
                   1076:                break;
                   1077:
                   1078:        case TYPE_A:
                   1079:                if (restart_point) {
                   1080:                        int i, n, ch;
                   1081:
                   1082:                        if (fseek(fout, 0L, SEEK_SET) < 0)
                   1083:                                goto done;
                   1084:                        n = restart_point;
                   1085:                        for (i = 0; i++ < n;) {
1.69      ray      1086:                                if ((ch = fgetc(fout)) == EOF) {
                   1087:                                        if (!ferror(fout))
                   1088:                                                errno = 0;
1.1       deraadt  1089:                                        goto done;
1.69      ray      1090:                                }
1.1       deraadt  1091:                                if (ch == '\n')
                   1092:                                        i++;
                   1093:                        }
                   1094:                        if (fseek(fout, 0L, SEEK_CUR) < 0) {
                   1095: done:
1.69      ray      1096:                                if (errno)
                   1097:                                        warn("local: %s", local);
                   1098:                                else
                   1099:                                        warnx("local: %s", local);
1.13      millert  1100:                                progress = oprogress;
1.16      millert  1101:                                preserve = opreserve;
1.1       deraadt  1102:                                if (closefunc != NULL)
                   1103:                                        (*closefunc)(fout);
                   1104:                                return;
                   1105:                        }
                   1106:                }
1.23      millert  1107:                while ((c = fgetc(din)) != EOF) {
1.67      espie    1108:                        may_send_noop_char();
1.1       deraadt  1109:                        if (c == '\n')
                   1110:                                bare_lfs++;
                   1111:                        while (c == '\r') {
1.10      millert  1112:                                while (hash && (!progress || filesize < 0) &&
                   1113:                                    (bytes >= hashbytes)) {
1.18      deraadt  1114:                                        (void)putc('#', ttyout);
                   1115:                                        (void)fflush(ttyout);
1.7       kstailey 1116:                                        hashbytes += mark;
1.1       deraadt  1117:                                }
                   1118:                                bytes++;
1.23      millert  1119:                                if ((c = fgetc(din)) != '\n' || tcrflag) {
1.1       deraadt  1120:                                        if (ferror(fout))
                   1121:                                                goto break2;
1.11      millert  1122:                                        (void)putc('\r', fout);
1.1       deraadt  1123:                                        if (c == '\0') {
                   1124:                                                bytes++;
                   1125:                                                goto contin2;
                   1126:                                        }
                   1127:                                        if (c == EOF)
                   1128:                                                goto contin2;
                   1129:                                }
                   1130:                        }
1.11      millert  1131:                        (void)putc(c, fout);
1.1       deraadt  1132:                        bytes++;
                   1133:        contin2:        ;
                   1134:                }
                   1135: break2:
1.69      ray      1136:                if (ferror(din) || ferror(fout))
                   1137:                        serrno = errno;
1.1       deraadt  1138:                if (bare_lfs) {
1.22      millert  1139:                        fprintf(ttyout,
1.13      millert  1140: "WARNING! %d bare linefeeds received in ASCII mode.\n", bare_lfs);
1.22      millert  1141:                        fputs("File may not have transferred correctly.\n",
                   1142:                            ttyout);
1.1       deraadt  1143:                }
1.10      millert  1144:                if (hash && (!progress || filesize < 0)) {
1.1       deraadt  1145:                        if (bytes < hashbytes)
1.18      deraadt  1146:                                (void)putc('#', ttyout);
                   1147:                        (void)putc('\n', ttyout);
                   1148:                        (void)fflush(ttyout);
1.1       deraadt  1149:                }
                   1150:                if (ferror(din)) {
1.69      ray      1151:                        if (serrno != EPIPE)
                   1152:                                warnx("netin: %s", strerror(serrno));
1.1       deraadt  1153:                        bytes = -1;
                   1154:                }
                   1155:                if (ferror(fout))
1.69      ray      1156:                        warnx("local: %s: %s", local, strerror(serrno));
1.1       deraadt  1157:                break;
                   1158:        }
1.10      millert  1159:        progressmeter(1);
1.13      millert  1160:        progress = oprogress;
1.16      millert  1161:        preserve = opreserve;
1.1       deraadt  1162:        if (closefunc != NULL)
                   1163:                (*closefunc)(fout);
1.11      millert  1164:        (void)signal(SIGINT, oldintr);
                   1165:        (void)signal(SIGINFO, oldinti);
1.1       deraadt  1166:        if (oldintp)
1.11      millert  1167:                (void)signal(SIGPIPE, oldintp);
                   1168:        (void)fclose(din);
                   1169:        (void)getreply(0);
1.67      espie    1170:        may_receive_noop_ack();
1.10      millert  1171:        if (bytes >= 0 && is_retr) {
                   1172:                if (bytes > 0)
                   1173:                        ptransfer(0);
                   1174:                if (preserve && (closefunc == fclose)) {
                   1175:                        mtime = remotemodtime(remote, 0);
                   1176:                        if (mtime != -1) {
1.12      millert  1177:                                struct utimbuf ut;
                   1178:
                   1179:                                ut.actime = time(NULL);
                   1180:                                ut.modtime = mtime;
                   1181:                                if (utime(local, &ut) == -1)
1.22      millert  1182:                                        fprintf(ttyout,
1.13      millert  1183:                                "Can't change modification time on %s to %s",
1.11      millert  1184:                                            local, asctime(localtime(&mtime)));
1.10      millert  1185:                        }
                   1186:                }
                   1187:        }
1.1       deraadt  1188:        return;
1.13      millert  1189:
1.1       deraadt  1190: abort:
                   1191:
1.10      millert  1192: /* abort using RFC959 recommended IP,SYNC sequence */
1.1       deraadt  1193:
1.13      millert  1194:        progress = oprogress;
1.16      millert  1195:        preserve = opreserve;
1.1       deraadt  1196:        if (oldintp)
1.11      millert  1197:                (void)signal(SIGPIPE, oldintp);
                   1198:        (void)signal(SIGINT, SIG_IGN);
1.1       deraadt  1199:        if (!cpend) {
                   1200:                code = -1;
1.11      millert  1201:                (void)signal(SIGINT, oldintr);
                   1202:                (void)signal(SIGINFO, oldinti);
1.1       deraadt  1203:                return;
                   1204:        }
                   1205:
                   1206:        abort_remote(din);
                   1207:        code = -1;
                   1208:        if (data >= 0) {
1.11      millert  1209:                (void)close(data);
1.1       deraadt  1210:                data = -1;
                   1211:        }
                   1212:        if (closefunc != NULL && fout != NULL)
                   1213:                (*closefunc)(fout);
                   1214:        if (din)
1.11      millert  1215:                (void)fclose(din);
1.1       deraadt  1216:        if (bytes > 0)
1.10      millert  1217:                ptransfer(0);
1.11      millert  1218:        (void)signal(SIGINT, oldintr);
                   1219:        (void)signal(SIGINFO, oldinti);
1.1       deraadt  1220: }
                   1221:
                   1222: /*
                   1223:  * Need to start a listen on the data channel before we send the command,
                   1224:  * otherwise the server's connect may fail.
                   1225:  */
                   1226: int
1.56      deraadt  1227: initconn(void)
1.1       deraadt  1228: {
                   1229:        char *p, *a;
1.57      deraadt  1230:        int result = ERROR, tmpno = 0;
1.1       deraadt  1231:        int on = 1;
1.34      itojun   1232:        int error;
                   1233:        u_int addr[16], port[2];
                   1234:        u_int af, hal, pal;
                   1235:        char *pasvcmd = NULL;
1.57      deraadt  1236:        socklen_t namelen;
1.34      itojun   1237:
                   1238:        if (myctladdr.su_family == AF_INET6
                   1239:         && (IN6_IS_ADDR_LINKLOCAL(&myctladdr.su_sin6.sin6_addr)
                   1240:          || IN6_IS_ADDR_SITELOCAL(&myctladdr.su_sin6.sin6_addr))) {
                   1241:                warnx("use of scoped address can be troublesome");
                   1242:        }
1.24      millert  1243: reinit:
1.1       deraadt  1244:        if (passivemode) {
1.34      itojun   1245:                data_addr = myctladdr;
                   1246:                data = socket(data_addr.su_family, SOCK_STREAM, 0);
1.1       deraadt  1247:                if (data < 0) {
1.10      millert  1248:                        warn("socket");
                   1249:                        return (1);
1.1       deraadt  1250:                }
                   1251:                if ((options & SO_DEBUG) &&
                   1252:                    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1.11      millert  1253:                               sizeof(on)) < 0)
1.10      millert  1254:                        warn("setsockopt (ignored)");
1.34      itojun   1255:                switch (data_addr.su_family) {
                   1256:                case AF_INET:
1.36      itojun   1257:                        if (epsv4 && !epsv4bad) {
1.66      beck     1258:                                int ov;
                   1259:                                /* shut this command up in case it fails */
                   1260:                                ov = verbose;
                   1261:                                verbose = -1;
1.36      itojun   1262:                                result = command(pasvcmd = "EPSV");
1.66      beck     1263:                                /*
                   1264:                                 * now back to whatever verbosity we had before
                   1265:                                 * and we can try PASV
                   1266:                                 */
                   1267:                                verbose = ov;
1.36      itojun   1268:                                if (code / 10 == 22 && code != 229) {
                   1269:                                        fputs(
1.34      itojun   1270: "wrong server: return code must be 229\n",
1.36      itojun   1271:                                                ttyout);
                   1272:                                        result = COMPLETE + 1;
                   1273:                                }
                   1274:                                if (result != COMPLETE) {
                   1275:                                        epsv4bad = 1;
                   1276:                                        if (debug) {
                   1277:                                                fputs(
                   1278: "disabling epsv4 for this connection\n",
                   1279:                                                    ttyout);
                   1280:                                        }
                   1281:                                }
1.34      itojun   1282:                        }
                   1283:                        if (result != COMPLETE)
                   1284:                                result = command(pasvcmd = "PASV");
                   1285:                        break;
                   1286:                case AF_INET6:
                   1287:                        result = command(pasvcmd = "EPSV");
                   1288:                        if (code / 10 == 22 && code != 229) {
                   1289:                                fputs(
                   1290: "wrong server: return code must be 229\n",
                   1291:                                        ttyout);
                   1292:                                result = COMPLETE + 1;
                   1293:                        }
                   1294:                        if (result != COMPLETE)
                   1295:                                result = command(pasvcmd = "LPSV");
                   1296:                        break;
                   1297:                default:
                   1298:                        result = COMPLETE + 1;
                   1299:                        break;
                   1300:                }
                   1301:                if (result != COMPLETE) {
1.24      millert  1302:                        if (activefallback) {
                   1303:                                (void)close(data);
                   1304:                                data = -1;
                   1305:                                passivemode = 0;
                   1306:                                activefallback = 0;
                   1307:                                goto reinit;
                   1308:                        }
1.18      deraadt  1309:                        fputs("Passive mode refused.\n", ttyout);
1.1       deraadt  1310:                        goto bad;
                   1311:                }
                   1312:
1.34      itojun   1313: #define pack2(var, off) \
                   1314:        (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
                   1315: #define pack4(var, off) \
                   1316:        (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
                   1317:         ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
                   1318:
1.1       deraadt  1319:                /*
1.34      itojun   1320:                 * What we've got at this point is a string of comma separated
                   1321:                 * one-byte unsigned integer values, separated by commas.
1.1       deraadt  1322:                 */
1.38      millert  1323:                if (!pasvcmd)
                   1324:                        goto bad;
1.34      itojun   1325:                if (strcmp(pasvcmd, "PASV") == 0) {
                   1326:                        if (data_addr.su_family != AF_INET) {
                   1327:                                fputs(
                   1328: "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
                   1329:                                error = 1;
                   1330:                                goto bad;
                   1331:                        }
                   1332:                        if (code / 10 == 22 && code != 227) {
                   1333:                                fputs("wrong server: return code must be 227\n",
                   1334:                                        ttyout);
                   1335:                                error = 1;
                   1336:                                goto bad;
                   1337:                        }
                   1338:                        error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",
                   1339:                                        &addr[0], &addr[1], &addr[2], &addr[3],
                   1340:                                        &port[0], &port[1]);
                   1341:                        if (error != 6) {
                   1342:                                fputs(
                   1343: "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
                   1344:                                error = 1;
                   1345:                                goto bad;
                   1346:                        }
                   1347:                        error = 0;
                   1348:                        memset(&data_addr, 0, sizeof(data_addr));
                   1349:                        data_addr.su_family = AF_INET;
                   1350:                        data_addr.su_len = sizeof(struct sockaddr_in);
                   1351:                        data_addr.su_sin.sin_addr.s_addr =
                   1352:                                htonl(pack4(addr, 0));
                   1353:                        data_addr.su_port = htons(pack2(port, 0));
                   1354:                } else if (strcmp(pasvcmd, "LPSV") == 0) {
                   1355:                        if (code / 10 == 22 && code != 228) {
                   1356:                                fputs("wrong server: return code must be 228\n",
                   1357:                                        ttyout);
                   1358:                                error = 1;
                   1359:                                goto bad;
                   1360:                        }
                   1361:                        switch (data_addr.su_family) {
                   1362:                        case AF_INET:
                   1363:                                error = sscanf(pasv,
                   1364: "%u,%u,%u,%u,%u,%u,%u,%u,%u",
                   1365:                                        &af, &hal,
                   1366:                                        &addr[0], &addr[1], &addr[2], &addr[3],
                   1367:                                        &pal, &port[0], &port[1]);
                   1368:                                if (error != 9) {
                   1369:                                        fputs(
                   1370: "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
                   1371:                                        error = 1;
                   1372:                                        goto bad;
                   1373:                                }
                   1374:                                if (af != 4 || hal != 4 || pal != 2) {
                   1375:                                        fputs(
                   1376: "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
                   1377:                                        error = 1;
                   1378:                                        goto bad;
                   1379:                                }
1.1       deraadt  1380:
1.34      itojun   1381:                                error = 0;
                   1382:                                memset(&data_addr, 0, sizeof(data_addr));
                   1383:                                data_addr.su_family = AF_INET;
                   1384:                                data_addr.su_len = sizeof(struct sockaddr_in);
                   1385:                                data_addr.su_sin.sin_addr.s_addr =
                   1386:                                        htonl(pack4(addr, 0));
                   1387:                                data_addr.su_port = htons(pack2(port, 0));
                   1388:                                break;
                   1389:                        case AF_INET6:
                   1390:                                error = sscanf(pasv,
                   1391: "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
                   1392:                                        &af, &hal,
                   1393:                                        &addr[0], &addr[1], &addr[2], &addr[3],
                   1394:                                        &addr[4], &addr[5], &addr[6], &addr[7],
                   1395:                                        &addr[8], &addr[9], &addr[10],
                   1396:                                        &addr[11], &addr[12], &addr[13],
                   1397:                                        &addr[14], &addr[15],
                   1398:                                        &pal, &port[0], &port[1]);
                   1399:                                if (error != 21) {
                   1400:                                        fputs(
1.18      deraadt  1401: "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1.34      itojun   1402:                                        error = 1;
                   1403:                                        goto bad;
                   1404:                                }
                   1405:                                if (af != 6 || hal != 16 || pal != 2) {
                   1406:                                        fputs(
                   1407: "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
                   1408:                                        error = 1;
                   1409:                                        goto bad;
                   1410:                                }
                   1411:
                   1412:                                error = 0;
                   1413:                                memset(&data_addr, 0, sizeof(data_addr));
                   1414:                                data_addr.su_family = AF_INET6;
                   1415:                                data_addr.su_len = sizeof(struct sockaddr_in6);
                   1416:                            {
                   1417:                                u_int32_t *p32;
                   1418:                                p32 = (u_int32_t *)&data_addr.su_sin6.sin6_addr;
                   1419:                                p32[0] = htonl(pack4(addr, 0));
                   1420:                                p32[1] = htonl(pack4(addr, 4));
                   1421:                                p32[2] = htonl(pack4(addr, 8));
                   1422:                                p32[3] = htonl(pack4(addr, 12));
                   1423:                            }
                   1424:                                data_addr.su_port = htons(pack2(port, 0));
                   1425:                                break;
                   1426:                        default:
                   1427:                                error = 1;
                   1428:                        }
                   1429:                } else if (strcmp(pasvcmd, "EPSV") == 0) {
                   1430:                        char delim[4];
                   1431:
                   1432:                        port[0] = 0;
                   1433:                        if (code / 10 == 22 && code != 229) {
                   1434:                                fputs("wrong server: return code must be 229\n",
                   1435:                                        ttyout);
                   1436:                                error = 1;
                   1437:                                goto bad;
                   1438:                        }
                   1439:                        if (sscanf(pasv, "%c%c%c%d%c", &delim[0],
                   1440:                                        &delim[1], &delim[2], &port[1],
                   1441:                                        &delim[3]) != 5) {
                   1442:                                fputs("parse error!\n", ttyout);
                   1443:                                error = 1;
                   1444:                                goto bad;
                   1445:                        }
                   1446:                        if (delim[0] != delim[1] || delim[0] != delim[2]
                   1447:                         || delim[0] != delim[3]) {
                   1448:                                fputs("parse error!\n", ttyout);
                   1449:                                error = 1;
                   1450:                                goto bad;
                   1451:                        }
                   1452:                        data_addr = hisctladdr;
                   1453:                        data_addr.su_port = htons(port[1]);
                   1454:                } else
1.1       deraadt  1455:                        goto bad;
                   1456:
1.27      deraadt  1457:                while (connect(data, (struct sockaddr *)&data_addr,
1.34      itojun   1458:                            data_addr.su_len) < 0) {
1.27      deraadt  1459:                        if (errno == EINTR)
                   1460:                                continue;
1.33      millert  1461:                        if (activefallback) {
                   1462:                                (void)close(data);
                   1463:                                data = -1;
                   1464:                                passivemode = 0;
                   1465:                                activefallback = 0;
                   1466:                                goto reinit;
                   1467:                        }
1.10      millert  1468:                        warn("connect");
1.1       deraadt  1469:                        goto bad;
                   1470:                }
1.34      itojun   1471: #if defined(IPPROTO_IP) && defined(IP_TOS)
                   1472:                if (data_addr.su_family == AF_INET) {
                   1473:                        on = IPTOS_THROUGHPUT;
                   1474:                        if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
                   1475:                                       sizeof(int)) < 0)
                   1476:                                warn("setsockopt TOS (ignored)");
                   1477:                }
1.1       deraadt  1478: #endif
1.10      millert  1479:                return (0);
1.1       deraadt  1480:        }
                   1481:
                   1482: noport:
                   1483:        data_addr = myctladdr;
                   1484:        if (sendport)
1.34      itojun   1485:                data_addr.su_port = 0;  /* let system pick one */
1.1       deraadt  1486:        if (data != -1)
1.11      millert  1487:                (void)close(data);
1.34      itojun   1488:        data = socket(data_addr.su_family, SOCK_STREAM, 0);
1.1       deraadt  1489:        if (data < 0) {
                   1490:                warn("socket");
                   1491:                if (tmpno)
                   1492:                        sendport = 1;
                   1493:                return (1);
                   1494:        }
                   1495:        if (!sendport)
1.10      millert  1496:                if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
1.11      millert  1497:                                sizeof(on)) < 0) {
1.1       deraadt  1498:                        warn("setsockopt (reuse address)");
                   1499:                        goto bad;
                   1500:                }
1.49      jakob    1501:        switch (data_addr.su_family) {
                   1502:        case AF_INET:
                   1503:                on = IP_PORTRANGE_HIGH;
                   1504:                if (setsockopt(data, IPPROTO_IP, IP_PORTRANGE,
                   1505:                    (char *)&on, sizeof(on)) < 0)
                   1506:                        warn("setsockopt IP_PORTRANGE (ignored)");
                   1507:                break;
                   1508:        case AF_INET6:
                   1509:                on = IPV6_PORTRANGE_HIGH;
                   1510:                if (setsockopt(data, IPPROTO_IPV6, IPV6_PORTRANGE,
                   1511:                    (char *)&on, sizeof(on)) < 0)
                   1512:                        warn("setsockopt IPV6_PORTRANGE (ignored)");
                   1513:                break;
                   1514:        }
1.34      itojun   1515:        if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {
1.1       deraadt  1516:                warn("bind");
                   1517:                goto bad;
                   1518:        }
                   1519:        if (options & SO_DEBUG &&
1.10      millert  1520:            setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1.11      millert  1521:                        sizeof(on)) < 0)
1.1       deraadt  1522:                warn("setsockopt (ignored)");
1.57      deraadt  1523:        namelen = sizeof(data_addr);
                   1524:        if (getsockname(data, (struct sockaddr *)&data_addr, &namelen) < 0) {
1.1       deraadt  1525:                warn("getsockname");
                   1526:                goto bad;
                   1527:        }
                   1528:        if (listen(data, 1) < 0)
                   1529:                warn("listen");
1.34      itojun   1530:
                   1531: #define        UC(b)   (((int)b)&0xff)
                   1532:
1.1       deraadt  1533:        if (sendport) {
1.47      itojun   1534:                char hname[NI_MAXHOST], pbuf[NI_MAXSERV];
1.64      ray      1535:                int af_tmp;
1.47      itojun   1536:                union sockunion tmp;
1.34      itojun   1537:
1.47      itojun   1538:                tmp = data_addr;
                   1539:                switch (tmp.su_family) {
1.34      itojun   1540:                case AF_INET:
1.36      itojun   1541:                        if (!epsv4 || epsv4bad) {
                   1542:                                result = COMPLETE +1;
                   1543:                                break;
                   1544:                        }
                   1545:                        /*FALLTHROUGH*/
1.34      itojun   1546:                case AF_INET6:
1.47      itojun   1547:                        if (tmp.su_family == AF_INET6)
                   1548:                                tmp.su_sin6.sin6_scope_id = 0;
1.64      ray      1549:                        af_tmp = (tmp.su_family == AF_INET) ? 1 : 2;
1.47      itojun   1550:                        if (getnameinfo((struct sockaddr *)&tmp,
                   1551:                            tmp.su_len, hname, sizeof(hname),
                   1552:                            pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
1.34      itojun   1553:                                result = ERROR;
                   1554:                        } else {
1.47      itojun   1555:                                result = command("EPRT |%d|%s|%s|",
1.64      ray      1556:                                    af_tmp, hname, pbuf);
1.36      itojun   1557:                                if (result != COMPLETE) {
                   1558:                                        epsv4bad = 1;
                   1559:                                        if (debug) {
                   1560:                                                fputs(
                   1561: "disabling epsv4 for this connection\n",
                   1562:                                                    ttyout);
                   1563:                                        }
                   1564:                                }
1.34      itojun   1565:                        }
                   1566:                        break;
                   1567:                default:
                   1568:                        result = COMPLETE + 1;
                   1569:                        break;
                   1570:                }
                   1571:                if (result == COMPLETE)
                   1572:                        goto skip_port;
                   1573:
                   1574:                switch (data_addr.su_family) {
                   1575:                case AF_INET:
                   1576:                        a = (char *)&data_addr.su_sin.sin_addr;
                   1577:                        p = (char *)&data_addr.su_port;
                   1578:                        result = command("PORT %d,%d,%d,%d,%d,%d",
                   1579:                                 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
                   1580:                                 UC(p[0]), UC(p[1]));
                   1581:                        break;
                   1582:                case AF_INET6:
                   1583:                        a = (char *)&data_addr.su_sin6.sin6_addr;
                   1584:                        p = (char *)&data_addr.su_port;
                   1585:                        result = command(
                   1586: "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
                   1587:                                 6, 16,
                   1588:                                 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
                   1589:                                 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
                   1590:                                 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
                   1591:                                 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
                   1592:                                 2, UC(p[0]), UC(p[1]));
                   1593:                        break;
                   1594:                default:
                   1595:                        result = COMPLETE + 1; /* xxx */
                   1596:                }
                   1597:        skip_port:
                   1598:
1.1       deraadt  1599:                if (result == ERROR && sendport == -1) {
                   1600:                        sendport = 0;
                   1601:                        tmpno = 1;
                   1602:                        goto noport;
                   1603:                }
                   1604:                return (result != COMPLETE);
                   1605:        }
                   1606:        if (tmpno)
                   1607:                sendport = 1;
1.34      itojun   1608: #if defined(IPPROTO_IP) && defined(IP_TOS)
                   1609:        if (data_addr.su_family == AF_INET) {
                   1610:                on = IPTOS_THROUGHPUT;
                   1611:                if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
                   1612:                               sizeof(int)) < 0)
                   1613:                        warn("setsockopt TOS (ignored)");
                   1614:        }
1.1       deraadt  1615: #endif
                   1616:        return (0);
                   1617: bad:
1.11      millert  1618:        (void)close(data), data = -1;
1.1       deraadt  1619:        if (tmpno)
                   1620:                sendport = 1;
                   1621:        return (1);
                   1622: }
                   1623:
                   1624: FILE *
1.56      deraadt  1625: dataconn(const char *lmode)
1.1       deraadt  1626: {
1.34      itojun   1627:        union sockunion from;
1.57      deraadt  1628:        socklen_t fromlen = myctladdr.su_len;
                   1629:        int s;
1.1       deraadt  1630:
                   1631:        if (passivemode)
                   1632:                return (fdopen(data, lmode));
                   1633:
                   1634:        s = accept(data, (struct sockaddr *) &from, &fromlen);
                   1635:        if (s < 0) {
                   1636:                warn("accept");
1.11      millert  1637:                (void)close(data), data = -1;
1.1       deraadt  1638:                return (NULL);
                   1639:        }
1.11      millert  1640:        (void)close(data);
1.1       deraadt  1641:        data = s;
1.34      itojun   1642: #if defined(IPPROTO_IP) && defined(IP_TOS)
                   1643:        if (from.su_family == AF_INET) {
                   1644:                int tos = IPTOS_THROUGHPUT;
                   1645:                if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
                   1646:                                sizeof(int)) < 0) {
                   1647:                        warn("setsockopt TOS (ignored)");
                   1648:                }
                   1649:        }
1.1       deraadt  1650: #endif
                   1651:        return (fdopen(data, lmode));
                   1652: }
                   1653:
1.57      deraadt  1654: /* ARGSUSED */
1.1       deraadt  1655: void
1.57      deraadt  1656: psummary(int signo)
1.1       deraadt  1657: {
1.26      deraadt  1658:        int save_errno = errno;
1.10      millert  1659:
                   1660:        if (bytes > 0)
                   1661:                ptransfer(1);
1.26      deraadt  1662:        errno = save_errno;
1.1       deraadt  1663: }
                   1664:
1.57      deraadt  1665: /* ARGSUSED */
1.1       deraadt  1666: void
1.57      deraadt  1667: psabort(int signo)
1.1       deraadt  1668: {
                   1669:
1.10      millert  1670:        alarmtimer(0);
1.1       deraadt  1671:        abrtflag++;
                   1672: }
                   1673:
                   1674: void
1.56      deraadt  1675: pswitch(int flag)
1.1       deraadt  1676: {
                   1677:        sig_t oldintr;
                   1678:        static struct comvars {
                   1679:                int connect;
                   1680:                char name[MAXHOSTNAMELEN];
1.34      itojun   1681:                union sockunion mctl;
                   1682:                union sockunion hctl;
1.1       deraadt  1683:                FILE *in;
                   1684:                FILE *out;
                   1685:                int tpe;
                   1686:                int curtpe;
                   1687:                int cpnd;
                   1688:                int sunqe;
                   1689:                int runqe;
                   1690:                int mcse;
                   1691:                int ntflg;
                   1692:                char nti[17];
                   1693:                char nto[17];
                   1694:                int mapflg;
                   1695:                char mi[MAXPATHLEN];
                   1696:                char mo[MAXPATHLEN];
                   1697:        } proxstruct, tmpstruct;
                   1698:        struct comvars *ip, *op;
                   1699:
                   1700:        abrtflag = 0;
                   1701:        oldintr = signal(SIGINT, psabort);
                   1702:        if (flag) {
                   1703:                if (proxy)
                   1704:                        return;
                   1705:                ip = &tmpstruct;
                   1706:                op = &proxstruct;
                   1707:                proxy++;
                   1708:        } else {
                   1709:                if (!proxy)
                   1710:                        return;
                   1711:                ip = &proxstruct;
                   1712:                op = &tmpstruct;
                   1713:                proxy = 0;
                   1714:        }
                   1715:        ip->connect = connected;
                   1716:        connected = op->connect;
                   1717:        if (hostname) {
1.41      lebel    1718:                (void)strlcpy(ip->name, hostname, sizeof(ip->name));
1.1       deraadt  1719:        } else
1.11      millert  1720:                ip->name[0] = '\0';
1.1       deraadt  1721:        hostname = op->name;
                   1722:        ip->hctl = hisctladdr;
                   1723:        hisctladdr = op->hctl;
                   1724:        ip->mctl = myctladdr;
                   1725:        myctladdr = op->mctl;
                   1726:        ip->in = cin;
                   1727:        cin = op->in;
                   1728:        ip->out = cout;
                   1729:        cout = op->out;
                   1730:        ip->tpe = type;
                   1731:        type = op->tpe;
                   1732:        ip->curtpe = curtype;
                   1733:        curtype = op->curtpe;
                   1734:        ip->cpnd = cpend;
                   1735:        cpend = op->cpnd;
                   1736:        ip->sunqe = sunique;
                   1737:        sunique = op->sunqe;
                   1738:        ip->runqe = runique;
                   1739:        runique = op->runqe;
                   1740:        ip->mcse = mcase;
                   1741:        mcase = op->mcse;
                   1742:        ip->ntflg = ntflag;
                   1743:        ntflag = op->ntflg;
1.41      lebel    1744:        (void)strlcpy(ip->nti, ntin, sizeof(ip->nti));
1.52      deraadt  1745:        (void)strlcpy(ntin, op->nti, sizeof ntin);
1.41      lebel    1746:        (void)strlcpy(ip->nto, ntout, sizeof(ip->nto));
1.52      deraadt  1747:        (void)strlcpy(ntout, op->nto, sizeof ntout);
1.1       deraadt  1748:        ip->mapflg = mapflag;
                   1749:        mapflag = op->mapflg;
1.41      lebel    1750:        (void)strlcpy(ip->mi, mapin, sizeof(ip->mi));
1.52      deraadt  1751:        (void)strlcpy(mapin, op->mi, sizeof mapin);
1.41      lebel    1752:        (void)strlcpy(ip->mo, mapout, sizeof(ip->mo));
1.52      deraadt  1753:        (void)strlcpy(mapout, op->mo, sizeof mapout);
1.11      millert  1754:        (void)signal(SIGINT, oldintr);
1.1       deraadt  1755:        if (abrtflag) {
                   1756:                abrtflag = 0;
                   1757:                (*oldintr)(SIGINT);
                   1758:        }
                   1759: }
                   1760:
1.57      deraadt  1761: /* ARGSUSED */
1.1       deraadt  1762: void
1.57      deraadt  1763: abortpt(int signo)
1.1       deraadt  1764: {
                   1765:
1.10      millert  1766:        alarmtimer(0);
1.18      deraadt  1767:        putc('\n', ttyout);
                   1768:        (void)fflush(ttyout);
1.1       deraadt  1769:        ptabflg++;
                   1770:        mflag = 0;
                   1771:        abrtflag = 0;
                   1772:        longjmp(ptabort, 1);
                   1773: }
                   1774:
                   1775: void
1.56      deraadt  1776: proxtrans(const char *cmd, const char *local, const char *remote)
1.1       deraadt  1777: {
1.40      millert  1778:        volatile sig_t oldintr;
1.20      millert  1779:        int prox_type, nfnd;
                   1780:        volatile int secndflag;
1.40      millert  1781:        char * volatile cmd2;
1.54      millert  1782:        struct pollfd pfd[1];
1.20      millert  1783:
                   1784:        oldintr = NULL;
                   1785:        secndflag = 0;
1.1       deraadt  1786:        if (strcmp(cmd, "RETR"))
                   1787:                cmd2 = "RETR";
                   1788:        else
                   1789:                cmd2 = runique ? "STOU" : "STOR";
                   1790:        if ((prox_type = type) == 0) {
                   1791:                if (unix_server && unix_proxy)
                   1792:                        prox_type = TYPE_I;
                   1793:                else
                   1794:                        prox_type = TYPE_A;
                   1795:        }
                   1796:        if (curtype != prox_type)
                   1797:                changetype(prox_type, 1);
                   1798:        if (command("PASV") != COMPLETE) {
1.18      deraadt  1799:                fputs("proxy server does not support third party transfers.\n",
                   1800:                    ttyout);
1.1       deraadt  1801:                return;
                   1802:        }
                   1803:        pswitch(0);
                   1804:        if (!connected) {
1.18      deraadt  1805:                fputs("No primary connection.\n", ttyout);
1.1       deraadt  1806:                pswitch(1);
                   1807:                code = -1;
                   1808:                return;
                   1809:        }
                   1810:        if (curtype != prox_type)
                   1811:                changetype(prox_type, 1);
                   1812:        if (command("PORT %s", pasv) != COMPLETE) {
                   1813:                pswitch(1);
                   1814:                return;
                   1815:        }
                   1816:        if (setjmp(ptabort))
                   1817:                goto abort;
                   1818:        oldintr = signal(SIGINT, abortpt);
                   1819:        if (command("%s %s", cmd, remote) != PRELIM) {
1.11      millert  1820:                (void)signal(SIGINT, oldintr);
1.1       deraadt  1821:                pswitch(1);
                   1822:                return;
                   1823:        }
                   1824:        sleep(2);
                   1825:        pswitch(1);
                   1826:        secndflag++;
                   1827:        if (command("%s %s", cmd2, local) != PRELIM)
                   1828:                goto abort;
                   1829:        ptflag++;
1.11      millert  1830:        (void)getreply(0);
1.1       deraadt  1831:        pswitch(0);
1.11      millert  1832:        (void)getreply(0);
                   1833:        (void)signal(SIGINT, oldintr);
1.1       deraadt  1834:        pswitch(1);
                   1835:        ptflag = 0;
1.18      deraadt  1836:        fprintf(ttyout, "local: %s remote: %s\n", local, remote);
1.1       deraadt  1837:        return;
                   1838: abort:
1.11      millert  1839:        (void)signal(SIGINT, SIG_IGN);
1.1       deraadt  1840:        ptflag = 0;
                   1841:        if (strcmp(cmd, "RETR") && !proxy)
                   1842:                pswitch(1);
                   1843:        else if (!strcmp(cmd, "RETR") && proxy)
                   1844:                pswitch(0);
                   1845:        if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
                   1846:                if (command("%s %s", cmd2, local) != PRELIM) {
                   1847:                        pswitch(0);
                   1848:                        if (cpend)
1.19      kstailey 1849:                                abort_remote(NULL);
1.1       deraadt  1850:                }
                   1851:                pswitch(1);
                   1852:                if (ptabflg)
                   1853:                        code = -1;
1.11      millert  1854:                (void)signal(SIGINT, oldintr);
1.1       deraadt  1855:                return;
                   1856:        }
                   1857:        if (cpend)
1.19      kstailey 1858:                abort_remote(NULL);
1.1       deraadt  1859:        pswitch(!proxy);
                   1860:        if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
                   1861:                if (command("%s %s", cmd2, local) != PRELIM) {
                   1862:                        pswitch(0);
                   1863:                        if (cpend)
1.19      kstailey 1864:                                abort_remote(NULL);
1.1       deraadt  1865:                        pswitch(1);
                   1866:                        if (ptabflg)
                   1867:                                code = -1;
1.11      millert  1868:                        (void)signal(SIGINT, oldintr);
1.1       deraadt  1869:                        return;
                   1870:                }
                   1871:        }
                   1872:        if (cpend)
1.19      kstailey 1873:                abort_remote(NULL);
1.1       deraadt  1874:        pswitch(!proxy);
                   1875:        if (cpend) {
1.54      millert  1876:                pfd[0].fd = fileno(cin);
                   1877:                pfd[0].events = POLLIN;
                   1878:                if ((nfnd = poll(pfd, 1, 10 * 1000)) <= 0) {
                   1879:                        if (nfnd < 0)
1.1       deraadt  1880:                                warn("abort");
                   1881:                        if (ptabflg)
                   1882:                                code = -1;
                   1883:                        lostpeer();
                   1884:                }
1.11      millert  1885:                (void)getreply(0);
                   1886:                (void)getreply(0);
1.1       deraadt  1887:        }
                   1888:        if (proxy)
                   1889:                pswitch(0);
                   1890:        pswitch(1);
                   1891:        if (ptabflg)
                   1892:                code = -1;
1.11      millert  1893:        (void)signal(SIGINT, oldintr);
1.1       deraadt  1894: }
                   1895:
1.57      deraadt  1896: /* ARGSUSED */
1.1       deraadt  1897: void
1.56      deraadt  1898: reset(int argc, char *argv[])
1.1       deraadt  1899: {
1.54      millert  1900:        struct pollfd pfd[1];
1.1       deraadt  1901:        int nfnd = 1;
                   1902:
1.54      millert  1903:        pfd[0].fd = fileno(cin);
                   1904:        pfd[0].events = POLLIN;
1.1       deraadt  1905:        while (nfnd > 0) {
1.54      millert  1906:                if ((nfnd = poll(pfd, 1, 0)) < 0) {
1.1       deraadt  1907:                        warn("reset");
                   1908:                        code = -1;
                   1909:                        lostpeer();
1.54      millert  1910:                } else if (nfnd) {
1.11      millert  1911:                        (void)getreply(0);
1.1       deraadt  1912:                }
                   1913:        }
                   1914: }
                   1915:
                   1916: char *
1.56      deraadt  1917: gunique(const char *local)
1.1       deraadt  1918: {
                   1919:        static char new[MAXPATHLEN];
                   1920:        char *cp = strrchr(local, '/');
                   1921:        int d, count=0;
                   1922:        char ext = '1';
                   1923:
                   1924:        if (cp)
                   1925:                *cp = '\0';
1.22      millert  1926:        d = access(cp == local ? "/" : cp ? local : ".", W_OK);
1.1       deraadt  1927:        if (cp)
                   1928:                *cp = '/';
                   1929:        if (d < 0) {
                   1930:                warn("local: %s", local);
                   1931:                return ((char *) 0);
                   1932:        }
1.52      deraadt  1933:        (void)strlcpy(new, local, sizeof new);
1.1       deraadt  1934:        cp = new + strlen(new);
                   1935:        *cp++ = '.';
                   1936:        while (!d) {
                   1937:                if (++count == 100) {
1.18      deraadt  1938:                        fputs("runique: can't find unique file name.\n", ttyout);
1.1       deraadt  1939:                        return ((char *) 0);
                   1940:                }
                   1941:                *cp++ = ext;
                   1942:                *cp = '\0';
                   1943:                if (ext == '9')
                   1944:                        ext = '0';
                   1945:                else
                   1946:                        ext++;
1.22      millert  1947:                if ((d = access(new, F_OK)) < 0)
1.1       deraadt  1948:                        break;
                   1949:                if (ext != '0')
                   1950:                        cp--;
                   1951:                else if (*(cp - 2) == '.')
                   1952:                        *(cp - 1) = '1';
                   1953:                else {
                   1954:                        *(cp - 2) = *(cp - 2) + 1;
                   1955:                        cp--;
                   1956:                }
                   1957:        }
                   1958:        return (new);
                   1959: }
                   1960:
                   1961: void
1.56      deraadt  1962: abort_remote(FILE *din)
1.1       deraadt  1963: {
                   1964:        char buf[BUFSIZ];
                   1965:        int nfnd;
1.54      millert  1966:        struct pollfd pfd[2];
1.13      millert  1967:
                   1968:        if (cout == NULL) {
                   1969:                warnx("Lost control connection for abort.");
                   1970:                if (ptabflg)
                   1971:                        code = -1;
                   1972:                lostpeer();
                   1973:                return;
                   1974:        }
1.1       deraadt  1975:
                   1976:        /*
                   1977:         * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
                   1978:         * after urgent byte rather than before as is protocol now
                   1979:         */
1.46      deraadt  1980:        snprintf(buf, sizeof buf, "%c%c%c", IAC, IP, IAC);
1.1       deraadt  1981:        if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
                   1982:                warn("abort");
1.10      millert  1983:        fprintf(cout, "%cABOR\r\n", DM);
1.11      millert  1984:        (void)fflush(cout);
1.54      millert  1985:        pfd[0].fd = fileno(cin);
                   1986:        pfd[0].events = POLLIN;
                   1987:        nfnd = 1;
1.10      millert  1988:        if (din) {
1.54      millert  1989:                pfd[1].fd = fileno(din);
                   1990:                pfd[1].events = POLLIN;
                   1991:                nfnd++;
1.1       deraadt  1992:        }
1.54      millert  1993:        if ((nfnd = poll(pfd, nfnd, 10 * 1000)) <= 0) {
                   1994:                if (nfnd < 0)
1.1       deraadt  1995:                        warn("abort");
                   1996:                if (ptabflg)
                   1997:                        code = -1;
                   1998:                lostpeer();
                   1999:        }
1.54      millert  2000:        if (din && (pfd[1].revents & POLLIN)) {
1.1       deraadt  2001:                while (read(fileno(din), buf, BUFSIZ) > 0)
                   2002:                        /* LOOP */;
                   2003:        }
                   2004:        if (getreply(0) == ERROR && code == 552) {
                   2005:                /* 552 needed for nic style abort */
1.11      millert  2006:                (void)getreply(0);
1.1       deraadt  2007:        }
1.11      millert  2008:        (void)getreply(0);
1.1       deraadt  2009: }