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