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

Annotation of src/usr.bin/cu/cu.c, Revision 1.28

1.28    ! deraadt     1: /* $OpenBSD: cu.c,v 1.27 2019/03/22 07:03:23 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/ioctl.h>
                     20:
1.11      halex      21: #include <ctype.h>
1.1       nicm       22: #include <err.h>
                     23: #include <event.h>
                     24: #include <fcntl.h>
                     25: #include <getopt.h>
                     26: #include <paths.h>
                     27: #include <pwd.h>
                     28: #include <signal.h>
                     29: #include <stdio.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
                     32: #include <termios.h>
                     33: #include <unistd.h>
1.20      deraadt    34: #include <limits.h>
1.1       nicm       35:
                     36: #include "cu.h"
                     37:
                     38: extern char            *__progname;
                     39:
1.6       nicm       40: FILE                   *record_file;
1.1       nicm       41: struct termios          saved_tio;
                     42: struct bufferevent     *input_ev;
                     43: struct bufferevent     *output_ev;
1.27      nicm       44: int                     escape_char = '~';
1.22      nicm       45: int                     is_direct = -1;
1.26      deraadt    46: int                     restricted = 0;
1.16      nicm       47: const char             *line_path = NULL;
                     48: int                     line_speed = -1;
1.1       nicm       49: int                     line_fd;
1.12      nicm       50: struct termios          line_tio;
1.1       nicm       51: struct bufferevent     *line_ev;
                     52: struct event            sigterm_ev;
1.8       nicm       53: struct event            sighup_ev;
1.1       nicm       54: enum {
                     55:        STATE_NONE,
                     56:        STATE_NEWLINE,
1.27      nicm       57:        STATE_ESCAPE
1.1       nicm       58: } last_state = STATE_NEWLINE;
                     59:
                     60: __dead void    usage(void);
                     61: void           signal_event(int, short, void *);
                     62: void           stream_read(struct bufferevent *, void *);
                     63: void           stream_error(struct bufferevent *, short, void *);
                     64: void           line_read(struct bufferevent *, void *);
                     65: void           line_error(struct bufferevent *, short, void *);
1.17      nicm       66: void           try_remote(const char *, const char *, const char *);
1.1       nicm       67:
                     68: __dead void
                     69: usage(void)
                     70: {
1.27      nicm       71:        fprintf(stderr, "usage: %s [-dr] [-E escape_char] [-l line] "
                     72:            "[-s speed | -speed]\n", __progname);
1.21      nicm       73:        fprintf(stderr, "       %s [host]\n", __progname);
1.1       nicm       74:        exit(1);
                     75: }
                     76:
                     77: int
                     78: main(int argc, char **argv)
                     79: {
1.16      nicm       80:        const char      *errstr;
1.17      nicm       81:        char            *tmp, *s, *host;
1.22      nicm       82:        int              opt, i, flags;
1.24      deraadt    83:
                     84:        if (pledge("stdio rpath wpath cpath getpw proc exec tty",
                     85:            NULL) == -1)
                     86:                err(1, "pledge");
1.1       nicm       87:
1.16      nicm       88:        if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
                     89:                err(1, "tcgetattr");
1.1       nicm       90:
                     91:        /*
                     92:         * Convert obsolescent -### speed to modern -s### syntax which getopt()
                     93:         * can handle.
                     94:         */
                     95:        for (i = 1; i < argc; i++) {
1.10      dlg        96:                if (strcmp("--", argv[i]) == 0)
                     97:                        break;
1.27      nicm       98:                if (argv[i][0] != '-' || !isdigit((u_char)argv[i][1]))
1.10      dlg        99:                        continue;
                    100:
                    101:                if (asprintf(&argv[i], "-s%s", &argv[i][1]) == -1)
                    102:                        errx(1, "speed asprintf");
1.1       nicm      103:        }
                    104:
1.27      nicm      105:        while ((opt = getopt(argc, argv, "drE:l:s:")) != -1) {
1.1       nicm      106:                switch (opt) {
1.22      nicm      107:                case 'd':
                    108:                        is_direct = 1;
                    109:                        break;
1.26      deraadt   110:                case 'r':
                    111:                        if (pledge("stdio rpath wpath tty", NULL) == -1)
                    112:                                err(1, "pledge");
                    113:                        restricted = 1;
                    114:                        break;
1.27      nicm      115:                case 'E':
                    116:                        if (optarg[0] == '^' && optarg[2] == '\0' &&
                    117:                            (u_char)optarg[1] >= 64 && (u_char)optarg[1] < 128)
                    118:                                escape_char = (u_char)optarg[1] & 31;
                    119:                        else if (strlen(optarg) == 1)
                    120:                                escape_char = (u_char)optarg[0];
                    121:                        else
                    122:                                errx(1, "invalid escape character: %s", optarg);
                    123:                        break;
1.1       nicm      124:                case 'l':
1.16      nicm      125:                        line_path = optarg;
1.1       nicm      126:                        break;
                    127:                case 's':
1.16      nicm      128:                        line_speed = strtonum(optarg, 0, INT_MAX, &errstr);
1.1       nicm      129:                        if (errstr != NULL)
                    130:                                errx(1, "speed is %s: %s", errstr, optarg);
                    131:                        break;
                    132:                default:
                    133:                        usage();
                    134:                }
                    135:        }
                    136:        argc -= optind;
                    137:        argv += optind;
1.16      nicm      138:        if (argc != 0 && argc != 1)
1.1       nicm      139:                usage();
                    140:
1.22      nicm      141:        if (line_path != NULL || line_speed != -1 || is_direct != -1) {
1.21      nicm      142:                if (argc != 0)
                    143:                        usage();
                    144:        } else {
                    145:                if (argc == 1)
                    146:                        host = argv[0];
                    147:                else
                    148:                        host = getenv("HOST");
                    149:                if (host != NULL && *host != '\0') {
                    150:                        if (*host == '/')
1.19      nicm      151:                                line_path = host;
1.21      nicm      152:                        else {
                    153:                                s = getenv("REMOTE");
                    154:                                if (s != NULL && *s == '/')
                    155:                                        try_remote(host, s, NULL);
                    156:                                else
                    157:                                        try_remote(host, NULL, s);
                    158:                        }
1.18      nicm      159:                }
1.17      nicm      160:        }
1.16      nicm      161:
                    162:        if (line_path == NULL)
                    163:                line_path = "/dev/cua00";
                    164:        if (line_speed == -1)
                    165:                line_speed = 9600;
1.22      nicm      166:        if (is_direct == -1)
                    167:                is_direct = 0;
1.16      nicm      168:
                    169:        if (strchr(line_path, '/') == NULL) {
                    170:                if (asprintf(&tmp, "%s%s", _PATH_DEV, line_path) == -1)
1.1       nicm      171:                        err(1, "asprintf");
1.16      nicm      172:                line_path = tmp;
1.1       nicm      173:        }
                    174:
1.22      nicm      175:        flags = O_RDWR;
                    176:        if (is_direct)
                    177:                flags |= O_NONBLOCK;
                    178:        line_fd = open(line_path, flags);
1.28    ! deraadt   179:        if (line_fd == -1)
1.16      nicm      180:                err(1, "open(\"%s\")", line_path);
1.26      deraadt   181:        if (restricted && pledge("stdio tty", NULL) == -1)
                    182:                err(1, "pledge");
1.25      mestre    183:        if (!isatty(line_fd))
                    184:                err(1, "%s", line_path);
1.1       nicm      185:        if (ioctl(line_fd, TIOCEXCL) != 0)
                    186:                err(1, "ioctl(TIOCEXCL)");
1.12      nicm      187:        if (tcgetattr(line_fd, &line_tio) != 0)
                    188:                err(1, "tcgetattr");
1.16      nicm      189:        if (set_line(line_speed) != 0)
1.4       nicm      190:                err(1, "tcsetattr");
1.1       nicm      191:
                    192:        event_init();
1.3       nicm      193:
1.1       nicm      194:        signal_set(&sigterm_ev, SIGTERM, signal_event, NULL);
                    195:        signal_add(&sigterm_ev, NULL);
1.8       nicm      196:        signal_set(&sighup_ev, SIGHUP, signal_event, NULL);
                    197:        signal_add(&sighup_ev, NULL);
1.3       nicm      198:        if (signal(SIGINT, SIG_IGN) == SIG_ERR)
                    199:                err(1, "signal");
                    200:        if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
                    201:                err(1, "signal");
1.1       nicm      202:
1.4       nicm      203:        set_termios(); /* after this use cu_err and friends */
1.1       nicm      204:
                    205:        /* stdin and stdout get separate events */
                    206:        input_ev = bufferevent_new(STDIN_FILENO, stream_read, NULL,
                    207:            stream_error, NULL);
                    208:        bufferevent_enable(input_ev, EV_READ);
                    209:        output_ev = bufferevent_new(STDOUT_FILENO, NULL, NULL, stream_error,
                    210:            NULL);
                    211:        bufferevent_enable(output_ev, EV_WRITE);
                    212:
1.23      nicm      213:        set_blocking(line_fd, 0);
1.1       nicm      214:        line_ev = bufferevent_new(line_fd, line_read, NULL, line_error,
                    215:            NULL);
                    216:        bufferevent_enable(line_ev, EV_READ|EV_WRITE);
                    217:
1.16      nicm      218:        printf("Connected to %s (speed %d)\r\n", line_path, line_speed);
1.1       nicm      219:        event_dispatch();
                    220:
1.5       nicm      221:        restore_termios();
1.1       nicm      222:        printf("\r\n[EOT]\n");
                    223:
                    224:        exit(0);
                    225: }
                    226:
                    227: void
                    228: signal_event(int fd, short events, void *data)
                    229: {
1.5       nicm      230:        restore_termios();
1.1       nicm      231:        printf("\r\n[SIG%s]\n", sys_signame[fd]);
                    232:
                    233:        exit(0);
                    234: }
                    235:
                    236: void
1.23      nicm      237: set_blocking(int fd, int state)
                    238: {
                    239:        int mode;
                    240:
                    241:        state = state ? 0 : O_NONBLOCK;
                    242:        if ((mode = fcntl(fd, F_GETFL)) == -1)
                    243:                cu_err(1, "fcntl");
                    244:        if ((mode & O_NONBLOCK) != state) {
                    245:                mode = (mode & ~O_NONBLOCK) | state;
                    246:                if (fcntl(fd, F_SETFL, mode) == -1)
                    247:                        cu_err(1, "fcntl");
                    248:        }
                    249: }
                    250:
                    251: void
1.1       nicm      252: set_termios(void)
                    253: {
                    254:        struct termios tio;
                    255:
                    256:        if (!isatty(STDIN_FILENO))
                    257:                return;
                    258:
                    259:        memcpy(&tio, &saved_tio, sizeof(tio));
                    260:        tio.c_lflag &= ~(ICANON|IEXTEN|ECHO);
                    261:        tio.c_iflag &= ~(INPCK|ICRNL);
                    262:        tio.c_oflag &= ~OPOST;
                    263:        tio.c_cc[VMIN] = 1;
                    264:        tio.c_cc[VTIME] = 0;
                    265:        tio.c_cc[VDISCARD] = _POSIX_VDISABLE;
                    266:        tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
                    267:        tio.c_cc[VINTR] = _POSIX_VDISABLE;
                    268:        tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
                    269:        tio.c_cc[VQUIT] = _POSIX_VDISABLE;
                    270:        tio.c_cc[VSUSP] = _POSIX_VDISABLE;
                    271:        if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) != 0)
1.4       nicm      272:                cu_err(1, "tcsetattr");
1.1       nicm      273: }
                    274:
                    275: void
                    276: restore_termios(void)
                    277: {
1.4       nicm      278:        if (isatty(STDIN_FILENO))
                    279:                tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio);
1.2       nicm      280: }
                    281:
                    282: int
                    283: set_line(int speed)
                    284: {
                    285:        struct termios   tio;
                    286:
1.12      nicm      287:        memcpy(&tio, &line_tio, sizeof(tio));
                    288:        tio.c_iflag &= ~(ISTRIP|ICRNL);
                    289:        tio.c_oflag &= ~OPOST;
                    290:        tio.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
                    291:        tio.c_cflag &= ~(CSIZE|PARENB);
                    292:        tio.c_cflag |= CREAD|CS8|CLOCAL;
1.2       nicm      293:        tio.c_cc[VMIN] = 1;
                    294:        tio.c_cc[VTIME] = 0;
                    295:        cfsetspeed(&tio, speed);
1.4       nicm      296:        if (tcsetattr(line_fd, TCSAFLUSH, &tio) != 0)
1.2       nicm      297:                return (-1);
                    298:        return (0);
1.1       nicm      299: }
                    300:
                    301: void
                    302: stream_read(struct bufferevent *bufev, void *data)
                    303: {
                    304:        char    *new_data, *ptr;
                    305:        size_t   new_size;
                    306:        int      state_change;
                    307:
                    308:        new_data = EVBUFFER_DATA(input_ev->input);
                    309:        new_size = EVBUFFER_LENGTH(input_ev->input);
                    310:        if (new_size == 0)
                    311:                return;
                    312:
                    313:        state_change = isatty(STDIN_FILENO);
                    314:        for (ptr = new_data; ptr < new_data + new_size; ptr++) {
                    315:                switch (last_state) {
                    316:                case STATE_NONE:
                    317:                        if (state_change && *ptr == '\r')
                    318:                                last_state = STATE_NEWLINE;
                    319:                        break;
                    320:                case STATE_NEWLINE:
1.27      nicm      321:                        if (state_change && (u_char)*ptr == escape_char) {
                    322:                                last_state = STATE_ESCAPE;
1.1       nicm      323:                                continue;
                    324:                        }
                    325:                        if (*ptr != '\r')
                    326:                                last_state = STATE_NONE;
                    327:                        break;
1.27      nicm      328:                case STATE_ESCAPE:
1.1       nicm      329:                        do_command(*ptr);
                    330:                        last_state = STATE_NEWLINE;
                    331:                        continue;
                    332:                }
                    333:
                    334:                bufferevent_write(line_ev, ptr, 1);
                    335:        }
                    336:
                    337:        evbuffer_drain(input_ev->input, new_size);
                    338: }
                    339:
                    340: void
                    341: stream_error(struct bufferevent *bufev, short what, void *data)
                    342: {
                    343:        event_loopexit(NULL);
                    344: }
                    345:
                    346: void
                    347: line_read(struct bufferevent *bufev, void *data)
                    348: {
                    349:        char    *new_data;
                    350:        size_t   new_size;
                    351:
                    352:        new_data = EVBUFFER_DATA(line_ev->input);
                    353:        new_size = EVBUFFER_LENGTH(line_ev->input);
                    354:        if (new_size == 0)
                    355:                return;
                    356:
1.6       nicm      357:        if (record_file != NULL)
                    358:                fwrite(new_data, 1, new_size, record_file);
1.1       nicm      359:        bufferevent_write(output_ev, new_data, new_size);
                    360:
                    361:        evbuffer_drain(line_ev->input, new_size);
                    362: }
                    363:
                    364: void
                    365: line_error(struct bufferevent *bufev, short what, void *data)
                    366: {
                    367:        event_loopexit(NULL);
1.16      nicm      368: }
                    369:
                    370: void
1.17      nicm      371: try_remote(const char *host, const char *path, const char *entry)
1.16      nicm      372: {
                    373:        const char      *paths[] = { "/etc/remote", NULL, NULL };
                    374:        char            *cp, *s;
                    375:        long             l;
                    376:        int              error;
                    377:
                    378:        if (path != NULL) {
                    379:                paths[0] = path;
                    380:                paths[1] = "/etc/remote";
                    381:        }
                    382:
1.17      nicm      383:        if (entry != NULL && cgetset(entry) != 0)
                    384:                cu_errx(1, "cgetset failed");
1.23      nicm      385:        error = cgetent(&cp, (char **)paths, (char *)host);
1.16      nicm      386:        if (error < 0) {
                    387:                switch (error) {
                    388:                case -1:
                    389:                        cu_errx(1, "unknown host %s", host);
                    390:                case -2:
                    391:                        cu_errx(1, "can't open remote file");
                    392:                case -3:
                    393:                        cu_errx(1, "loop in remote file");
                    394:                default:
                    395:                        cu_errx(1, "unknown error in remote file");
                    396:                }
                    397:        }
1.22      nicm      398:
                    399:        if (is_direct == -1 && cgetcap(cp, "dc", ':') != NULL)
                    400:                is_direct = 1;
1.16      nicm      401:
                    402:        if (line_path == NULL && cgetstr(cp, "dv", &s) >= 0)
                    403:                line_path = s;
                    404:
                    405:        if (line_speed == -1 && cgetnum(cp, "br", &l) >= 0) {
                    406:                if (l < 0 || l > INT_MAX)
                    407:                        cu_errx(1, "speed out of range");
                    408:                line_speed = l;
                    409:        }
1.1       nicm      410: }
                    411:
                    412: /* Expands tildes in the file name. Based on code from ssh/misc.c. */
                    413: char *
                    414: tilde_expand(const char *filename1)
                    415: {
1.14      tedu      416:        const char      *filename, *path, *sep;
                    417:        char             user[128], *out;
1.1       nicm      418:        struct passwd   *pw;
                    419:        u_int            len, slash;
1.14      tedu      420:        int              rv;
1.1       nicm      421:
                    422:        if (*filename1 != '~')
                    423:                goto no_change;
                    424:        filename = filename1 + 1;
                    425:
                    426:        path = strchr(filename, '/');
                    427:        if (path != NULL && path > filename) {          /* ~user/path */
                    428:                slash = path - filename;
                    429:                if (slash > sizeof(user) - 1)
                    430:                        goto no_change;
                    431:                memcpy(user, filename, slash);
                    432:                user[slash] = '\0';
                    433:                if ((pw = getpwnam(user)) == NULL)
                    434:                        goto no_change;
                    435:        } else if ((pw = getpwuid(getuid())) == NULL)   /* ~/path */
                    436:                goto no_change;
                    437:
                    438:        /* Make sure directory has a trailing '/' */
                    439:        len = strlen(pw->pw_dir);
1.14      tedu      440:        if (len == 0 || pw->pw_dir[len - 1] != '/')
                    441:                sep = "/";
                    442:        else
                    443:                sep = "";
1.1       nicm      444:
                    445:        /* Skip leading '/' from specified path */
                    446:        if (path != NULL)
                    447:                filename = path + 1;
1.14      tedu      448:
                    449:        if ((rv = asprintf(&out, "%s%s%s", pw->pw_dir, sep, filename)) == -1)
                    450:                cu_err(1, "asprintf");
1.20      deraadt   451:        if (rv >= PATH_MAX) {
1.14      tedu      452:                free(out);
1.1       nicm      453:                goto no_change;
1.14      tedu      454:        }
1.1       nicm      455:
                    456:        return (out);
                    457:
                    458: no_change:
                    459:        out = strdup(filename1);
                    460:        if (out == NULL)
1.4       nicm      461:                cu_err(1, "strdup");
1.1       nicm      462:        return (out);
                    463: }