Annotation of src/usr.bin/ssh/sshpty.c, Revision 1.28
1.28 ! stevesk 1: /* $OpenBSD: sshpty.c,v 1.27 2007/09/11 04:36:29 stevesk 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);
51: if (i < 0) {
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.12 avsm 64: if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
65: error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
66: if (chmod(tty, (mode_t) 0666) < 0)
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 */
85: if (setsid() < 0)
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.");
100: if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
101: error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
102: #endif /* TIOCSCTTY */
1.12 avsm 103: fd = open(tty, O_RDWR);
1.1 djm 104: if (fd < 0)
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);
111: if (fd < 0)
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");
144: if (grp) {
145: gid = grp->gr_gid;
146: mode = S_IRUSR | S_IWUSR | S_IWGRP;
147: } else {
148: gid = pw->pw_gid;
149: mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
150: }
151:
152: /*
153: * Change owner and mode of the tty as required.
1.3 markus 154: * Warn but continue if filesystem is read-only and the uids match/
155: * tty is owned by root.
1.1 djm 156: */
1.12 avsm 157: if (stat(tty, &st))
158: fatal("stat(%.100s) failed: %.100s", tty,
1.1 djm 159: strerror(errno));
160:
161: if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
1.12 avsm 162: if (chown(tty, pw->pw_uid, gid) < 0) {
1.4 deraadt 163: if (errno == EROFS &&
1.6 deraadt 164: (st.st_uid == pw->pw_uid || st.st_uid == 0))
1.8 markus 165: debug("chown(%.100s, %u, %u) failed: %.100s",
1.12 avsm 166: tty, (u_int)pw->pw_uid, (u_int)gid,
1.4 deraadt 167: strerror(errno));
1.1 djm 168: else
1.5 deraadt 169: fatal("chown(%.100s, %u, %u) failed: %.100s",
1.12 avsm 170: tty, (u_int)pw->pw_uid, (u_int)gid,
1.4 deraadt 171: strerror(errno));
1.1 djm 172: }
173: }
174:
175: if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
1.12 avsm 176: if (chmod(tty, mode) < 0) {
1.1 djm 177: if (errno == EROFS &&
178: (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
1.8 markus 179: debug("chmod(%.100s, 0%o) failed: %.100s",
1.12 avsm 180: tty, (u_int)mode, strerror(errno));
1.1 djm 181: else
182: fatal("chmod(%.100s, 0%o) failed: %.100s",
1.12 avsm 183: tty, (u_int)mode, strerror(errno));
1.1 djm 184: }
185: }
186: }