Annotation of src/usr.bin/ssh/sshpty.c, Revision 1.18
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
1.18 ! deraadt 44: pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
1.1 djm 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
1.18 ! deraadt 120: pty_change_window_size(int ptyfd, u_int row, u_int col,
! 121: u_int xpixel, u_int ypixel)
1.1 djm 122: {
123: struct winsize w;
1.6 deraadt 124:
1.18 ! deraadt 125: /* may truncate u_int -> u_short */
1.1 djm 126: w.ws_row = row;
127: w.ws_col = col;
128: w.ws_xpixel = xpixel;
129: w.ws_ypixel = ypixel;
130: (void) ioctl(ptyfd, TIOCSWINSZ, &w);
131: }
132:
133: void
1.12 avsm 134: pty_setowner(struct passwd *pw, const char *tty)
1.1 djm 135: {
136: struct group *grp;
137: gid_t gid;
138: mode_t mode;
139: struct stat st;
140:
141: /* Determine the group to make the owner of the tty. */
142: grp = getgrnam("tty");
143: if (grp) {
144: gid = grp->gr_gid;
145: mode = S_IRUSR | S_IWUSR | S_IWGRP;
146: } else {
147: gid = pw->pw_gid;
148: mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
149: }
150:
151: /*
152: * Change owner and mode of the tty as required.
1.3 markus 153: * Warn but continue if filesystem is read-only and the uids match/
154: * tty is owned by root.
1.1 djm 155: */
1.12 avsm 156: if (stat(tty, &st))
157: fatal("stat(%.100s) failed: %.100s", tty,
1.1 djm 158: strerror(errno));
159:
160: if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
1.12 avsm 161: if (chown(tty, pw->pw_uid, gid) < 0) {
1.4 deraadt 162: if (errno == EROFS &&
1.6 deraadt 163: (st.st_uid == pw->pw_uid || st.st_uid == 0))
1.8 markus 164: debug("chown(%.100s, %u, %u) failed: %.100s",
1.12 avsm 165: tty, (u_int)pw->pw_uid, (u_int)gid,
1.4 deraadt 166: strerror(errno));
1.1 djm 167: else
1.5 deraadt 168: fatal("chown(%.100s, %u, %u) failed: %.100s",
1.12 avsm 169: tty, (u_int)pw->pw_uid, (u_int)gid,
1.4 deraadt 170: strerror(errno));
1.1 djm 171: }
172: }
173:
174: if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
1.12 avsm 175: if (chmod(tty, mode) < 0) {
1.1 djm 176: if (errno == EROFS &&
177: (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
1.8 markus 178: debug("chmod(%.100s, 0%o) failed: %.100s",
1.12 avsm 179: tty, (u_int)mode, strerror(errno));
1.1 djm 180: else
181: fatal("chmod(%.100s, 0%o) failed: %.100s",
1.12 avsm 182: tty, (u_int)mode, strerror(errno));
1.1 djm 183: }
184: }
185: }