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

Annotation of src/usr.bin/ssh/sshpty.c, Revision 1.13

1.1       djm         1: /*
                      2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * Allocating a pseudo-terminal, and making it the controlling tty.
                      6:  *
                      7:  * As far as I am concerned, the code I have written for this software
                      8:  * can be used freely for any purpose.  Any derived versions of this
                      9:  * software must be clearly marked as such, and if the derived work is
                     10:  * incompatible with the protocol description in the RFC file, it must be
                     11:  * called by a name other than "ssh" or "Secure Shell".
                     12:  */
                     13:
                     14: #include "includes.h"
1.13    ! stevesk    15: RCSID("$OpenBSD: sshpty.c,v 1.12 2004/06/21 17:36:31 avsm Exp $");
1.1       djm        16:
1.13    ! stevesk    17: #include <termios.h>
1.1       djm        18: #include <util.h>
1.13    ! stevesk    19:
1.1       djm        20: #include "sshpty.h"
                     21: #include "log.h"
                     22:
                     23: /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
                     24: #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
                     25: #undef HAVE_DEV_PTMX
                     26: #endif
                     27:
                     28: #ifndef O_NOCTTY
                     29: #define O_NOCTTY 0
                     30: #endif
                     31:
                     32: /*
                     33:  * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
                     34:  * nonzero if a pty was successfully allocated.  On success, open file
                     35:  * descriptors for the pty and tty sides and the name of the tty side are
                     36:  * returned (the buffer must be able to hold at least 64 characters).
                     37:  */
                     38:
                     39: int
                     40: pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
                     41: {
                     42:        char buf[64];
                     43:        int i;
                     44:
                     45:        i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
                     46:        if (i < 0) {
                     47:                error("openpty: %.100s", strerror(errno));
                     48:                return 0;
                     49:        }
                     50:        strlcpy(namebuf, buf, namebuflen);      /* possible truncation */
                     51:        return 1;
                     52: }
                     53:
                     54: /* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
                     55:
                     56: void
1.12      avsm       57: pty_release(const char *tty)
1.1       djm        58: {
1.12      avsm       59:        if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
                     60:                error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
                     61:        if (chmod(tty, (mode_t) 0666) < 0)
                     62:                error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
1.1       djm        63: }
                     64:
1.10      markus     65: /* Makes the tty the process's controlling tty and sets it to sane modes. */
1.1       djm        66:
                     67: void
1.12      avsm       68: pty_make_controlling_tty(int *ttyfd, const char *tty)
1.1       djm        69: {
                     70:        int fd;
                     71:
                     72:        /* First disconnect from the old controlling tty. */
                     73: #ifdef TIOCNOTTY
                     74:        fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
                     75:        if (fd >= 0) {
                     76:                (void) ioctl(fd, TIOCNOTTY, NULL);
                     77:                close(fd);
                     78:        }
                     79: #endif /* TIOCNOTTY */
                     80:        if (setsid() < 0)
                     81:                error("setsid: %.100s", strerror(errno));
                     82:
                     83:        /*
                     84:         * Verify that we are successfully disconnected from the controlling
                     85:         * tty.
                     86:         */
                     87:        fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
                     88:        if (fd >= 0) {
                     89:                error("Failed to disconnect from controlling tty.");
                     90:                close(fd);
                     91:        }
                     92:        /* Make it our controlling tty. */
                     93: #ifdef TIOCSCTTY
                     94:        debug("Setting controlling tty using TIOCSCTTY.");
                     95:        if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
                     96:                error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
                     97: #endif /* TIOCSCTTY */
1.12      avsm       98:        fd = open(tty, O_RDWR);
1.1       djm        99:        if (fd < 0)
1.12      avsm      100:                error("%.100s: %.100s", tty, strerror(errno));
1.1       djm       101:        else
                    102:                close(fd);
                    103:
                    104:        /* Verify that we now have a controlling tty. */
                    105:        fd = open(_PATH_TTY, O_WRONLY);
                    106:        if (fd < 0)
                    107:                error("open /dev/tty failed - could not set controlling tty: %.100s",
1.4       deraadt   108:                    strerror(errno));
1.6       deraadt   109:        else
1.1       djm       110:                close(fd);
                    111: }
                    112:
                    113: /* Changes the window size associated with the pty. */
                    114:
                    115: void
                    116: pty_change_window_size(int ptyfd, int row, int col,
1.4       deraadt   117:        int xpixel, int ypixel)
1.1       djm       118: {
                    119:        struct winsize w;
1.6       deraadt   120:
1.1       djm       121:        w.ws_row = row;
                    122:        w.ws_col = col;
                    123:        w.ws_xpixel = xpixel;
                    124:        w.ws_ypixel = ypixel;
                    125:        (void) ioctl(ptyfd, TIOCSWINSZ, &w);
                    126: }
                    127:
                    128: void
1.12      avsm      129: pty_setowner(struct passwd *pw, const char *tty)
1.1       djm       130: {
                    131:        struct group *grp;
                    132:        gid_t gid;
                    133:        mode_t mode;
                    134:        struct stat st;
                    135:
                    136:        /* Determine the group to make the owner of the tty. */
                    137:        grp = getgrnam("tty");
                    138:        if (grp) {
                    139:                gid = grp->gr_gid;
                    140:                mode = S_IRUSR | S_IWUSR | S_IWGRP;
                    141:        } else {
                    142:                gid = pw->pw_gid;
                    143:                mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
                    144:        }
                    145:
                    146:        /*
                    147:         * Change owner and mode of the tty as required.
1.3       markus    148:         * Warn but continue if filesystem is read-only and the uids match/
                    149:         * tty is owned by root.
1.1       djm       150:         */
1.12      avsm      151:        if (stat(tty, &st))
                    152:                fatal("stat(%.100s) failed: %.100s", tty,
1.1       djm       153:                    strerror(errno));
                    154:
                    155:        if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
1.12      avsm      156:                if (chown(tty, pw->pw_uid, gid) < 0) {
1.4       deraadt   157:                        if (errno == EROFS &&
1.6       deraadt   158:                            (st.st_uid == pw->pw_uid || st.st_uid == 0))
1.8       markus    159:                                debug("chown(%.100s, %u, %u) failed: %.100s",
1.12      avsm      160:                                    tty, (u_int)pw->pw_uid, (u_int)gid,
1.4       deraadt   161:                                    strerror(errno));
1.1       djm       162:                        else
1.5       deraadt   163:                                fatal("chown(%.100s, %u, %u) failed: %.100s",
1.12      avsm      164:                                    tty, (u_int)pw->pw_uid, (u_int)gid,
1.4       deraadt   165:                                    strerror(errno));
1.1       djm       166:                }
                    167:        }
                    168:
                    169:        if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
1.12      avsm      170:                if (chmod(tty, mode) < 0) {
1.1       djm       171:                        if (errno == EROFS &&
                    172:                            (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
1.8       markus    173:                                debug("chmod(%.100s, 0%o) failed: %.100s",
1.12      avsm      174:                                    tty, (u_int)mode, strerror(errno));
1.1       djm       175:                        else
                    176:                                fatal("chmod(%.100s, 0%o) failed: %.100s",
1.12      avsm      177:                                    tty, (u_int)mode, strerror(errno));
1.1       djm       178:                }
                    179:        }
                    180: }