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