blob: 36788c4d794a69d62ba0bb457d960f97109f2be3 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Allocating a pseudo-terminal, and making it the controlling tty.
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * 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".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Darren Tucker3f9fdc72004-06-22 12:56:01 +100015RCSID("$OpenBSD: sshpty.c,v 1.12 2004/06/21 17:36:31 avsm Exp $");
Damien Miller95def091999-11-25 00:26:21 +110016
Damien Miller36b339a1999-12-14 10:54:47 +110017#ifdef HAVE_UTIL_H
18# include <util.h>
19#endif /* HAVE_UTIL_H */
20
Ben Lindstromd95c09c2001-02-18 19:13:33 +000021#include "sshpty.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000022#include "log.h"
Kevin Steves974fb9c2001-06-15 00:04:23 +000023#include "misc.h"
Damien Miller356a0b01999-11-08 15:30:59 +110024
Damien Millerfce16481999-12-08 08:53:52 +110025#ifdef HAVE_PTY_H
26# include <pty.h>
27#endif
Damien Millerfce16481999-12-08 08:53:52 +110028
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029#ifndef O_NOCTTY
30#define O_NOCTTY 0
31#endif
32
Damien Miller5428f641999-11-25 11:54:57 +110033/*
34 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
35 * nonzero if a pty was successfully allocated. On success, open file
36 * descriptors for the pty and tty sides and the name of the tty side are
37 * returned (the buffer must be able to hold at least 64 characters).
38 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
Damien Miller4af51302000-04-16 11:18:38 +100040int
Damien Miller037a0dc1999-12-07 15:38:31 +110041pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042{
Damien Miller95def091999-11-25 00:26:21 +110043 /* openpty(3) exists in OSF/1 and some other os'es */
Damien Miller99e92432001-02-18 12:49:35 +110044 char *name;
Damien Miller95def091999-11-25 00:26:21 +110045 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Damien Miller99e92432001-02-18 12:49:35 +110047 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +110048 if (i < 0) {
49 error("openpty: %.100s", strerror(errno));
50 return 0;
51 }
Damien Miller99e92432001-02-18 12:49:35 +110052 name = ttyname(*ttyfd);
53 if (!name)
54 fatal("openpty returns device for which ttyname fails.");
55
56 strlcpy(namebuf, name, namebuflen); /* possible truncation */
Damien Miller95def091999-11-25 00:26:21 +110057 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058}
59
Damien Miller95def091999-11-25 00:26:21 +110060/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller4af51302000-04-16 11:18:38 +100062void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100063pty_release(const char *tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064{
Darren Tucker3f9fdc72004-06-22 12:56:01 +100065 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
66 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
67 if (chmod(tty, (mode_t) 0666) < 0)
68 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069}
70
Damien Miller469954d2003-06-18 20:25:33 +100071/* Makes the tty the process's controlling tty and sets it to sane modes. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Damien Miller4af51302000-04-16 11:18:38 +100073void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100074pty_make_controlling_tty(int *ttyfd, const char *tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075{
Ben Lindstrom6db66ff2001-08-06 23:29:16 +000076 int fd;
Damien Miller4a820ea2001-10-12 19:15:48 +100077#ifdef USE_VHANGUP
78 void *old;
79#endif /* USE_VHANGUP */
Ben Lindstrom6db66ff2001-08-06 23:29:16 +000080
Tim Rice81ed5182002-09-25 17:38:46 -070081#ifdef _UNICOS
Kevin Stevesf744b512001-08-14 20:31:49 +000082 if (setsid() < 0)
83 error("setsid: %.100s", strerror(errno));
Ben Lindstrom6db66ff2001-08-06 23:29:16 +000084
Darren Tucker3f9fdc72004-06-22 12:56:01 +100085 fd = open(tty, O_RDWR|O_NOCTTY);
Kevin Stevesf744b512001-08-14 20:31:49 +000086 if (fd != -1) {
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +000087 signal(SIGHUP, SIG_IGN);
Kevin Stevesf744b512001-08-14 20:31:49 +000088 ioctl(fd, TCVHUP, (char *)NULL);
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +000089 signal(SIGHUP, SIG_DFL);
Kevin Stevesf744b512001-08-14 20:31:49 +000090 setpgid(0, 0);
91 close(fd);
Ben Lindstrom6db66ff2001-08-06 23:29:16 +000092 } else {
Kevin Stevesf744b512001-08-14 20:31:49 +000093 error("Failed to disconnect from controlling tty.");
Ben Lindstrom6db66ff2001-08-06 23:29:16 +000094 }
Ben Lindstrom6db66ff2001-08-06 23:29:16 +000095
Kevin Stevesf744b512001-08-14 20:31:49 +000096 debug("Setting controlling tty using TCSETCTTY.");
97 ioctl(*ttyfd, TCSETCTTY, NULL);
98 fd = open("/dev/tty", O_RDWR);
99 if (fd < 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000100 error("%.100s: %.100s", tty, strerror(errno));
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000101 close(*ttyfd);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000102 *ttyfd = fd;
Tim Rice81ed5182002-09-25 17:38:46 -0700103#else /* _UNICOS */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Miller95def091999-11-25 00:26:21 +1100105 /* First disconnect from the old controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000107 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +1100108 if (fd >= 0) {
109 (void) ioctl(fd, TIOCNOTTY, NULL);
110 close(fd);
111 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112#endif /* TIOCNOTTY */
Damien Miller95def091999-11-25 00:26:21 +1100113 if (setsid() < 0)
114 error("setsid: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller5428f641999-11-25 11:54:57 +1100116 /*
117 * Verify that we are successfully disconnected from the controlling
118 * tty.
119 */
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000120 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +1100121 if (fd >= 0) {
122 error("Failed to disconnect from controlling tty.");
123 close(fd);
124 }
125 /* Make it our controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126#ifdef TIOCSCTTY
Damien Miller95def091999-11-25 00:26:21 +1100127 debug("Setting controlling tty using TIOCSCTTY.");
Kevin Steves6c0d6272000-11-12 09:22:29 +0000128 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
129 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130#endif /* TIOCSCTTY */
Darren Tucker2be1cbb2005-05-27 21:13:40 +1000131#ifdef NEED_SETPGRP
Ben Lindstromb5628642000-10-18 00:02:25 +0000132 if (setpgrp(0,0) < 0)
133 error("SETPGRP %s",strerror(errno));
Darren Tucker2be1cbb2005-05-27 21:13:40 +1000134#endif /* NEED_SETPGRP */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100135#ifdef USE_VHANGUP
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000136 old = signal(SIGHUP, SIG_IGN);
Damien Millerd0cff3e2000-04-20 23:12:58 +1000137 vhangup();
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000138 signal(SIGHUP, old);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100139#endif /* USE_VHANGUP */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000140 fd = open(tty, O_RDWR);
Damien Millerd0cff3e2000-04-20 23:12:58 +1000141 if (fd < 0) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000142 error("%.100s: %.100s", tty, strerror(errno));
Damien Millerd0cff3e2000-04-20 23:12:58 +1000143 } else {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100144#ifdef USE_VHANGUP
Damien Millerd0cff3e2000-04-20 23:12:58 +1000145 close(*ttyfd);
146 *ttyfd = fd;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100147#else /* USE_VHANGUP */
Damien Miller95def091999-11-25 00:26:21 +1100148 close(fd);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100149#endif /* USE_VHANGUP */
Damien Millerd0cff3e2000-04-20 23:12:58 +1000150 }
Damien Miller95def091999-11-25 00:26:21 +1100151 /* Verify that we now have a controlling tty. */
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000152 fd = open(_PATH_TTY, O_WRONLY);
Damien Miller95def091999-11-25 00:26:21 +1100153 if (fd < 0)
154 error("open /dev/tty failed - could not set controlling tty: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100155 strerror(errno));
Damien Millera8e06ce2003-11-21 23:48:55 +1100156 else
Damien Miller95def091999-11-25 00:26:21 +1100157 close(fd);
Tim Rice81ed5182002-09-25 17:38:46 -0700158#endif /* _UNICOS */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159}
160
161/* Changes the window size associated with the pty. */
162
Damien Miller4af51302000-04-16 11:18:38 +1000163void
Damien Miller95def091999-11-25 00:26:21 +1100164pty_change_window_size(int ptyfd, int row, int col,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100165 int xpixel, int ypixel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166{
Damien Miller95def091999-11-25 00:26:21 +1100167 struct winsize w;
Ben Lindstrom8ada5d02002-06-23 21:42:50 +0000168
Damien Miller95def091999-11-25 00:26:21 +1100169 w.ws_row = row;
170 w.ws_col = col;
171 w.ws_xpixel = xpixel;
172 w.ws_ypixel = ypixel;
173 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174}
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100175
176void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000177pty_setowner(struct passwd *pw, const char *tty)
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100178{
179 struct group *grp;
180 gid_t gid;
181 mode_t mode;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000182 struct stat st;
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100183
184 /* Determine the group to make the owner of the tty. */
185 grp = getgrnam("tty");
186 if (grp) {
187 gid = grp->gr_gid;
188 mode = S_IRUSR | S_IWUSR | S_IWGRP;
189 } else {
190 gid = pw->pw_gid;
191 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
192 }
193
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000194 /*
195 * Change owner and mode of the tty as required.
Ben Lindstromc88785e2001-08-06 20:47:23 +0000196 * Warn but continue if filesystem is read-only and the uids match/
197 * tty is owned by root.
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000198 */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000199 if (stat(tty, &st))
200 fatal("stat(%.100s) failed: %.100s", tty,
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000201 strerror(errno));
202
203 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000204 if (chown(tty, pw->pw_uid, gid) < 0) {
Damien Miller9f0f5c62001-12-21 14:45:46 +1100205 if (errno == EROFS &&
Ben Lindstrom8ada5d02002-06-23 21:42:50 +0000206 (st.st_uid == pw->pw_uid || st.st_uid == 0))
Damien Millere8cea9e2003-02-24 11:54:10 +1100207 debug("chown(%.100s, %u, %u) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000208 tty, (u_int)pw->pw_uid, (u_int)gid,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100209 strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000210 else
Ben Lindstrom5c385522002-06-23 21:23:20 +0000211 fatal("chown(%.100s, %u, %u) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000212 tty, (u_int)pw->pw_uid, (u_int)gid,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100213 strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000214 }
215 }
216
217 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000218 if (chmod(tty, mode) < 0) {
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000219 if (errno == EROFS &&
220 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
Damien Millere8cea9e2003-02-24 11:54:10 +1100221 debug("chmod(%.100s, 0%o) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000222 tty, (u_int)mode, strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000223 else
224 fatal("chmod(%.100s, 0%o) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000225 tty, (u_int)mode, strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000226 }
227 }
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100228}