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

1.16    ! nicm        1: /* $OpenBSD: cu.c,v 1.15 2013/11/20 20:55:09 deraadt 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/param.h>
                     20: #include <sys/ioctl.h>
                     21:
1.11      halex      22: #include <ctype.h>
1.1       nicm       23: #include <err.h>
                     24: #include <event.h>
                     25: #include <fcntl.h>
                     26: #include <getopt.h>
                     27: #include <paths.h>
                     28: #include <pwd.h>
                     29: #include <signal.h>
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <termios.h>
                     34: #include <unistd.h>
                     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.16    ! nicm       44: const char             *line_path = NULL;
        !            45: int                     line_speed = -1;
1.1       nicm       46: int                     line_fd;
1.12      nicm       47: struct termios          line_tio;
1.1       nicm       48: struct bufferevent     *line_ev;
                     49: struct event            sigterm_ev;
1.8       nicm       50: struct event            sighup_ev;
1.1       nicm       51: enum {
                     52:        STATE_NONE,
                     53:        STATE_NEWLINE,
                     54:        STATE_TILDE
                     55: } last_state = STATE_NEWLINE;
                     56:
                     57: __dead void    usage(void);
                     58: void           signal_event(int, short, void *);
                     59: void           stream_read(struct bufferevent *, void *);
                     60: void           stream_error(struct bufferevent *, short, void *);
                     61: void           line_read(struct bufferevent *, void *);
                     62: void           line_error(struct bufferevent *, short, void *);
1.16    ! nicm       63: void           try_remote(const char *, const char *);
1.1       nicm       64:
                     65: __dead void
                     66: usage(void)
                     67: {
1.16    ! nicm       68:        fprintf(stderr, "usage: %s [-l line] [-s speed | -speed] [host]\n",
1.7       nicm       69:            __progname);
1.1       nicm       70:        exit(1);
                     71: }
                     72:
                     73: int
                     74: main(int argc, char **argv)
                     75: {
1.16    ! nicm       76:        const char      *errstr;
        !            77:        char            *tmp, *s;
        !            78:        int              opt, i;
1.1       nicm       79:
1.16    ! nicm       80:        if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
        !            81:                err(1, "tcgetattr");
1.1       nicm       82:
                     83:        /*
                     84:         * Convert obsolescent -### speed to modern -s### syntax which getopt()
                     85:         * can handle.
                     86:         */
                     87:        for (i = 1; i < argc; i++) {
1.10      dlg        88:                if (strcmp("--", argv[i]) == 0)
                     89:                        break;
1.15      deraadt    90:                if (argv[i][0] != '-' || !isdigit((unsigned char)argv[i][1]))
1.10      dlg        91:                        continue;
                     92:
                     93:                if (asprintf(&argv[i], "-s%s", &argv[i][1]) == -1)
                     94:                        errx(1, "speed asprintf");
1.1       nicm       95:        }
                     96:
                     97:        while ((opt = getopt(argc, argv, "l:s:")) != -1) {
                     98:                switch (opt) {
                     99:                case 'l':
1.16    ! nicm      100:                        line_path = optarg;
1.1       nicm      101:                        break;
                    102:                case 's':
1.16    ! nicm      103:                        line_speed = strtonum(optarg, 0, INT_MAX, &errstr);
1.1       nicm      104:                        if (errstr != NULL)
                    105:                                errx(1, "speed is %s: %s", errstr, optarg);
                    106:                        break;
                    107:                default:
                    108:                        usage();
                    109:                }
                    110:        }
                    111:        argc -= optind;
                    112:        argv += optind;
1.16    ! nicm      113:        if (argc != 0 && argc != 1)
1.1       nicm      114:                usage();
                    115:
1.16    ! nicm      116:        s = getenv("REMOTE");
        !           117:        if (argc == 1) {
        !           118:                if (s != NULL && *s == '/')
        !           119:                        try_remote(argv[0], s);
        !           120:                else
        !           121:                        try_remote(argv[0], NULL);
        !           122:        } else if (s != NULL && *s != '/')
        !           123:                try_remote(s, NULL);
        !           124:
        !           125:        if (line_path == NULL)
        !           126:                line_path = "/dev/cua00";
        !           127:        if (line_speed == -1)
        !           128:                line_speed = 9600;
        !           129:
        !           130:        if (strchr(line_path, '/') == NULL) {
        !           131:                if (asprintf(&tmp, "%s%s", _PATH_DEV, line_path) == -1)
1.1       nicm      132:                        err(1, "asprintf");
1.16    ! nicm      133:                line_path = tmp;
1.1       nicm      134:        }
                    135:
1.16    ! nicm      136:        line_fd = open(line_path, O_RDWR);
1.1       nicm      137:        if (line_fd < 0)
1.16    ! nicm      138:                err(1, "open(\"%s\")", line_path);
1.1       nicm      139:        if (ioctl(line_fd, TIOCEXCL) != 0)
                    140:                err(1, "ioctl(TIOCEXCL)");
1.12      nicm      141:        if (tcgetattr(line_fd, &line_tio) != 0)
                    142:                err(1, "tcgetattr");
1.16    ! nicm      143:        if (set_line(line_speed) != 0)
1.4       nicm      144:                err(1, "tcsetattr");
1.1       nicm      145:
                    146:        event_init();
1.3       nicm      147:
1.1       nicm      148:        signal_set(&sigterm_ev, SIGTERM, signal_event, NULL);
                    149:        signal_add(&sigterm_ev, NULL);
1.8       nicm      150:        signal_set(&sighup_ev, SIGHUP, signal_event, NULL);
                    151:        signal_add(&sighup_ev, NULL);
1.3       nicm      152:        if (signal(SIGINT, SIG_IGN) == SIG_ERR)
                    153:                err(1, "signal");
                    154:        if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
                    155:                err(1, "signal");
1.1       nicm      156:
1.4       nicm      157:        set_termios(); /* after this use cu_err and friends */
1.1       nicm      158:
                    159:        /* stdin and stdout get separate events */
                    160:        input_ev = bufferevent_new(STDIN_FILENO, stream_read, NULL,
                    161:            stream_error, NULL);
                    162:        bufferevent_enable(input_ev, EV_READ);
                    163:        output_ev = bufferevent_new(STDOUT_FILENO, NULL, NULL, stream_error,
                    164:            NULL);
                    165:        bufferevent_enable(output_ev, EV_WRITE);
                    166:
                    167:        line_ev = bufferevent_new(line_fd, line_read, NULL, line_error,
                    168:            NULL);
                    169:        bufferevent_enable(line_ev, EV_READ|EV_WRITE);
                    170:
1.16    ! nicm      171:        printf("Connected to %s (speed %d)\r\n", line_path, line_speed);
1.1       nicm      172:        event_dispatch();
                    173:
1.5       nicm      174:        restore_termios();
1.1       nicm      175:        printf("\r\n[EOT]\n");
                    176:
                    177:        exit(0);
                    178: }
                    179:
                    180: void
                    181: signal_event(int fd, short events, void *data)
                    182: {
1.5       nicm      183:        restore_termios();
1.1       nicm      184:        printf("\r\n[SIG%s]\n", sys_signame[fd]);
                    185:
                    186:        exit(0);
                    187: }
                    188:
                    189: void
                    190: set_termios(void)
                    191: {
                    192:        struct termios tio;
                    193:
                    194:        if (!isatty(STDIN_FILENO))
                    195:                return;
                    196:
                    197:        memcpy(&tio, &saved_tio, sizeof(tio));
                    198:        tio.c_lflag &= ~(ICANON|IEXTEN|ECHO);
                    199:        tio.c_iflag &= ~(INPCK|ICRNL);
                    200:        tio.c_oflag &= ~OPOST;
                    201:        tio.c_cc[VMIN] = 1;
                    202:        tio.c_cc[VTIME] = 0;
                    203:        tio.c_cc[VDISCARD] = _POSIX_VDISABLE;
                    204:        tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
                    205:        tio.c_cc[VINTR] = _POSIX_VDISABLE;
                    206:        tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
                    207:        tio.c_cc[VQUIT] = _POSIX_VDISABLE;
                    208:        tio.c_cc[VSUSP] = _POSIX_VDISABLE;
                    209:        if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) != 0)
1.4       nicm      210:                cu_err(1, "tcsetattr");
1.1       nicm      211: }
                    212:
                    213: void
                    214: restore_termios(void)
                    215: {
1.4       nicm      216:        if (isatty(STDIN_FILENO))
                    217:                tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio);
1.2       nicm      218: }
                    219:
                    220: int
                    221: set_line(int speed)
                    222: {
                    223:        struct termios   tio;
                    224:
1.12      nicm      225:        memcpy(&tio, &line_tio, sizeof(tio));
                    226:        tio.c_iflag &= ~(ISTRIP|ICRNL);
                    227:        tio.c_oflag &= ~OPOST;
                    228:        tio.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
                    229:        tio.c_cflag &= ~(CSIZE|PARENB);
                    230:        tio.c_cflag |= CREAD|CS8|CLOCAL;
1.2       nicm      231:        tio.c_cc[VMIN] = 1;
                    232:        tio.c_cc[VTIME] = 0;
                    233:        cfsetspeed(&tio, speed);
1.4       nicm      234:        if (tcsetattr(line_fd, TCSAFLUSH, &tio) != 0)
1.2       nicm      235:                return (-1);
                    236:        return (0);
1.1       nicm      237: }
                    238:
                    239: void
                    240: stream_read(struct bufferevent *bufev, void *data)
                    241: {
                    242:        char    *new_data, *ptr;
                    243:        size_t   new_size;
                    244:        int      state_change;
                    245:
                    246:        new_data = EVBUFFER_DATA(input_ev->input);
                    247:        new_size = EVBUFFER_LENGTH(input_ev->input);
                    248:        if (new_size == 0)
                    249:                return;
                    250:
                    251:        state_change = isatty(STDIN_FILENO);
                    252:        for (ptr = new_data; ptr < new_data + new_size; ptr++) {
                    253:                switch (last_state) {
                    254:                case STATE_NONE:
                    255:                        if (state_change && *ptr == '\r')
                    256:                                last_state = STATE_NEWLINE;
                    257:                        break;
                    258:                case STATE_NEWLINE:
                    259:                        if (state_change && *ptr == '~') {
                    260:                                last_state = STATE_TILDE;
                    261:                                continue;
                    262:                        }
                    263:                        if (*ptr != '\r')
                    264:                                last_state = STATE_NONE;
                    265:                        break;
                    266:                case STATE_TILDE:
                    267:                        do_command(*ptr);
                    268:                        last_state = STATE_NEWLINE;
                    269:                        continue;
                    270:                }
                    271:
                    272:                bufferevent_write(line_ev, ptr, 1);
                    273:        }
                    274:
                    275:        evbuffer_drain(input_ev->input, new_size);
                    276: }
                    277:
                    278: void
                    279: stream_error(struct bufferevent *bufev, short what, void *data)
                    280: {
                    281:        event_loopexit(NULL);
                    282: }
                    283:
                    284: void
                    285: line_read(struct bufferevent *bufev, void *data)
                    286: {
                    287:        char    *new_data;
                    288:        size_t   new_size;
                    289:
                    290:        new_data = EVBUFFER_DATA(line_ev->input);
                    291:        new_size = EVBUFFER_LENGTH(line_ev->input);
                    292:        if (new_size == 0)
                    293:                return;
                    294:
1.6       nicm      295:        if (record_file != NULL)
                    296:                fwrite(new_data, 1, new_size, record_file);
1.1       nicm      297:        bufferevent_write(output_ev, new_data, new_size);
                    298:
                    299:        evbuffer_drain(line_ev->input, new_size);
                    300: }
                    301:
                    302: void
                    303: line_error(struct bufferevent *bufev, short what, void *data)
                    304: {
                    305:        event_loopexit(NULL);
1.16    ! nicm      306: }
        !           307:
        !           308: void
        !           309: try_remote(const char *host, const char *path)
        !           310: {
        !           311:        const char      *paths[] = { "/etc/remote", NULL, NULL };
        !           312:        char            *cp, *s;
        !           313:        long             l;
        !           314:        int              error;
        !           315:
        !           316:        if (path != NULL) {
        !           317:                paths[0] = path;
        !           318:                paths[1] = "/etc/remote";
        !           319:        }
        !           320:
        !           321:        error = cgetent(&cp, (char**)paths, (char*)host);
        !           322:        if (error < 0) {
        !           323:                switch (error) {
        !           324:                case -1:
        !           325:                        cu_errx(1, "unknown host %s", host);
        !           326:                case -2:
        !           327:                        cu_errx(1, "can't open remote file");
        !           328:                case -3:
        !           329:                        cu_errx(1, "loop in remote file");
        !           330:                default:
        !           331:                        cu_errx(1, "unknown error in remote file");
        !           332:                }
        !           333:        }
        !           334:
        !           335:        if (line_path == NULL && cgetstr(cp, "dv", &s) >= 0)
        !           336:                line_path = s;
        !           337:
        !           338:        if (line_speed == -1 && cgetnum(cp, "br", &l) >= 0) {
        !           339:                if (l < 0 || l > INT_MAX)
        !           340:                        cu_errx(1, "speed out of range");
        !           341:                line_speed = l;
        !           342:        }
1.1       nicm      343: }
                    344:
                    345: /* Expands tildes in the file name. Based on code from ssh/misc.c. */
                    346: char *
                    347: tilde_expand(const char *filename1)
                    348: {
1.14      tedu      349:        const char      *filename, *path, *sep;
                    350:        char             user[128], *out;
1.1       nicm      351:        struct passwd   *pw;
                    352:        u_int            len, slash;
1.14      tedu      353:        int              rv;
1.1       nicm      354:
                    355:        if (*filename1 != '~')
                    356:                goto no_change;
                    357:        filename = filename1 + 1;
                    358:
                    359:        path = strchr(filename, '/');
                    360:        if (path != NULL && path > filename) {          /* ~user/path */
                    361:                slash = path - filename;
                    362:                if (slash > sizeof(user) - 1)
                    363:                        goto no_change;
                    364:                memcpy(user, filename, slash);
                    365:                user[slash] = '\0';
                    366:                if ((pw = getpwnam(user)) == NULL)
                    367:                        goto no_change;
                    368:        } else if ((pw = getpwuid(getuid())) == NULL)   /* ~/path */
                    369:                goto no_change;
                    370:
                    371:        /* Make sure directory has a trailing '/' */
                    372:        len = strlen(pw->pw_dir);
1.14      tedu      373:        if (len == 0 || pw->pw_dir[len - 1] != '/')
                    374:                sep = "/";
                    375:        else
                    376:                sep = "";
1.1       nicm      377:
                    378:        /* Skip leading '/' from specified path */
                    379:        if (path != NULL)
                    380:                filename = path + 1;
1.14      tedu      381:
                    382:        if ((rv = asprintf(&out, "%s%s%s", pw->pw_dir, sep, filename)) == -1)
                    383:                cu_err(1, "asprintf");
                    384:        if (rv >= MAXPATHLEN) {
                    385:                free(out);
1.1       nicm      386:                goto no_change;
1.14      tedu      387:        }
1.1       nicm      388:
                    389:        return (out);
                    390:
                    391: no_change:
                    392:        out = strdup(filename1);
                    393:        if (out == NULL)
1.4       nicm      394:                cu_err(1, "strdup");
1.1       nicm      395:        return (out);
                    396: }