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

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