[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.31

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