blob: 973c2d2d87e7b73ba532ca1dd537c08901cc4c7f [file] [log] [blame]
Damien Miller9f2abc42006-07-10 20:53:08 +10001/* $OpenBSD: sshpty.c,v 1.21 2006/07/06 16:03:53 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Allocating a pseudo-terminal, and making it the controlling tty.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * 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".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110016
17#include <sys/ioctl.h>
Damien Millerf17883e2006-03-15 11:45:54 +110018#include <sys/types.h>
19#include <sys/stat.h>
Tim Rice4b23f7c2006-03-14 22:09:50 -080020#include <signal.h>
Damien Miller95def091999-11-25 00:26:21 +110021
Damien Miller427a1d52006-07-10 20:20:33 +100022#include <grp.h>
Damien Miller03e20032006-03-15 11:16:59 +110023#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110024# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110025#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100026#include <pwd.h>
Damien Miller03e20032006-03-15 11:16:59 +110027#include <termios.h>
Damien Miller36b339a1999-12-14 10:54:47 +110028#ifdef HAVE_UTIL_H
29# include <util.h>
30#endif /* HAVE_UTIL_H */
31
Ben Lindstromd95c09c2001-02-18 19:13:33 +000032#include "sshpty.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#include "log.h"
Kevin Steves974fb9c2001-06-15 00:04:23 +000034#include "misc.h"
Damien Miller356a0b01999-11-08 15:30:59 +110035
Damien Millerfce16481999-12-08 08:53:52 +110036#ifdef HAVE_PTY_H
37# include <pty.h>
38#endif
Damien Millerfce16481999-12-08 08:53:52 +110039
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040#ifndef O_NOCTTY
41#define O_NOCTTY 0
42#endif
43
Damien Miller5428f641999-11-25 11:54:57 +110044/*
45 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
46 * nonzero if a pty was successfully allocated. On success, open file
47 * descriptors for the pty and tty sides and the name of the tty side are
48 * returned (the buffer must be able to hold at least 64 characters).
49 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Damien Miller4af51302000-04-16 11:18:38 +100051int
Damien Miller71a73672006-03-26 14:04:36 +110052pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053{
Damien Miller95def091999-11-25 00:26:21 +110054 /* openpty(3) exists in OSF/1 and some other os'es */
Damien Miller99e92432001-02-18 12:49:35 +110055 char *name;
Damien Miller95def091999-11-25 00:26:21 +110056 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
Damien Miller99e92432001-02-18 12:49:35 +110058 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +110059 if (i < 0) {
60 error("openpty: %.100s", strerror(errno));
61 return 0;
62 }
Damien Miller99e92432001-02-18 12:49:35 +110063 name = ttyname(*ttyfd);
64 if (!name)
65 fatal("openpty returns device for which ttyname fails.");
66
67 strlcpy(namebuf, name, namebuflen); /* possible truncation */
Damien Miller95def091999-11-25 00:26:21 +110068 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069}
70
Damien Miller95def091999-11-25 00:26:21 +110071/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Damien Miller4af51302000-04-16 11:18:38 +100073void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100074pty_release(const char *tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075{
Darren Tucker3f9fdc72004-06-22 12:56:01 +100076 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
77 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
78 if (chmod(tty, (mode_t) 0666) < 0)
79 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080}
81
Damien Miller469954d2003-06-18 20:25:33 +100082/* Makes the tty the process's controlling tty and sets it to sane modes. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Damien Miller4af51302000-04-16 11:18:38 +100084void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100085pty_make_controlling_tty(int *ttyfd, const char *tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086{
Ben Lindstrom6db66ff2001-08-06 23:29:16 +000087 int fd;
Damien Miller4a820ea2001-10-12 19:15:48 +100088#ifdef USE_VHANGUP
89 void *old;
90#endif /* USE_VHANGUP */
Ben Lindstrom6db66ff2001-08-06 23:29:16 +000091
Tim Rice81ed5182002-09-25 17:38:46 -070092#ifdef _UNICOS
Kevin Stevesf744b512001-08-14 20:31:49 +000093 if (setsid() < 0)
94 error("setsid: %.100s", strerror(errno));
Ben Lindstrom6db66ff2001-08-06 23:29:16 +000095
Darren Tucker3f9fdc72004-06-22 12:56:01 +100096 fd = open(tty, O_RDWR|O_NOCTTY);
Kevin Stevesf744b512001-08-14 20:31:49 +000097 if (fd != -1) {
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +000098 signal(SIGHUP, SIG_IGN);
Kevin Stevesf744b512001-08-14 20:31:49 +000099 ioctl(fd, TCVHUP, (char *)NULL);
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000100 signal(SIGHUP, SIG_DFL);
Kevin Stevesf744b512001-08-14 20:31:49 +0000101 setpgid(0, 0);
102 close(fd);
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000103 } else {
Kevin Stevesf744b512001-08-14 20:31:49 +0000104 error("Failed to disconnect from controlling tty.");
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000105 }
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000106
Kevin Stevesf744b512001-08-14 20:31:49 +0000107 debug("Setting controlling tty using TCSETCTTY.");
108 ioctl(*ttyfd, TCSETCTTY, NULL);
109 fd = open("/dev/tty", O_RDWR);
110 if (fd < 0)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000111 error("%.100s: %.100s", tty, strerror(errno));
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000112 close(*ttyfd);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000113 *ttyfd = fd;
Tim Rice81ed5182002-09-25 17:38:46 -0700114#else /* _UNICOS */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller95def091999-11-25 00:26:21 +1100116 /* First disconnect from the old controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000118 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +1100119 if (fd >= 0) {
120 (void) ioctl(fd, TIOCNOTTY, NULL);
121 close(fd);
122 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123#endif /* TIOCNOTTY */
Damien Miller95def091999-11-25 00:26:21 +1100124 if (setsid() < 0)
125 error("setsid: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller5428f641999-11-25 11:54:57 +1100127 /*
128 * Verify that we are successfully disconnected from the controlling
129 * tty.
130 */
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000131 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +1100132 if (fd >= 0) {
133 error("Failed to disconnect from controlling tty.");
134 close(fd);
135 }
136 /* Make it our controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137#ifdef TIOCSCTTY
Damien Miller95def091999-11-25 00:26:21 +1100138 debug("Setting controlling tty using TIOCSCTTY.");
Kevin Steves6c0d6272000-11-12 09:22:29 +0000139 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
140 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141#endif /* TIOCSCTTY */
Darren Tucker2be1cbb2005-05-27 21:13:40 +1000142#ifdef NEED_SETPGRP
Ben Lindstromb5628642000-10-18 00:02:25 +0000143 if (setpgrp(0,0) < 0)
144 error("SETPGRP %s",strerror(errno));
Darren Tucker2be1cbb2005-05-27 21:13:40 +1000145#endif /* NEED_SETPGRP */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100146#ifdef USE_VHANGUP
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000147 old = signal(SIGHUP, SIG_IGN);
Damien Millerd0cff3e2000-04-20 23:12:58 +1000148 vhangup();
Ben Lindstrom5ade9ab2003-08-25 01:16:21 +0000149 signal(SIGHUP, old);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100150#endif /* USE_VHANGUP */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000151 fd = open(tty, O_RDWR);
Damien Millerd0cff3e2000-04-20 23:12:58 +1000152 if (fd < 0) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000153 error("%.100s: %.100s", tty, strerror(errno));
Damien Millerd0cff3e2000-04-20 23:12:58 +1000154 } else {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100155#ifdef USE_VHANGUP
Damien Millerd0cff3e2000-04-20 23:12:58 +1000156 close(*ttyfd);
157 *ttyfd = fd;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100158#else /* USE_VHANGUP */
Damien Miller95def091999-11-25 00:26:21 +1100159 close(fd);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100160#endif /* USE_VHANGUP */
Damien Millerd0cff3e2000-04-20 23:12:58 +1000161 }
Damien Miller95def091999-11-25 00:26:21 +1100162 /* Verify that we now have a controlling tty. */
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000163 fd = open(_PATH_TTY, O_WRONLY);
Damien Miller95def091999-11-25 00:26:21 +1100164 if (fd < 0)
165 error("open /dev/tty failed - could not set controlling tty: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100166 strerror(errno));
Damien Millera8e06ce2003-11-21 23:48:55 +1100167 else
Damien Miller95def091999-11-25 00:26:21 +1100168 close(fd);
Tim Rice81ed5182002-09-25 17:38:46 -0700169#endif /* _UNICOS */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170}
171
172/* Changes the window size associated with the pty. */
173
Damien Miller4af51302000-04-16 11:18:38 +1000174void
Damien Miller71a73672006-03-26 14:04:36 +1100175pty_change_window_size(int ptyfd, u_int row, u_int col,
176 u_int xpixel, u_int ypixel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177{
Damien Miller95def091999-11-25 00:26:21 +1100178 struct winsize w;
Ben Lindstrom8ada5d02002-06-23 21:42:50 +0000179
Damien Miller71a73672006-03-26 14:04:36 +1100180 /* may truncate u_int -> u_short */
Damien Miller95def091999-11-25 00:26:21 +1100181 w.ws_row = row;
182 w.ws_col = col;
183 w.ws_xpixel = xpixel;
184 w.ws_ypixel = ypixel;
185 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186}
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100187
188void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000189pty_setowner(struct passwd *pw, const char *tty)
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100190{
191 struct group *grp;
192 gid_t gid;
193 mode_t mode;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000194 struct stat st;
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100195
196 /* Determine the group to make the owner of the tty. */
197 grp = getgrnam("tty");
198 if (grp) {
199 gid = grp->gr_gid;
200 mode = S_IRUSR | S_IWUSR | S_IWGRP;
201 } else {
202 gid = pw->pw_gid;
203 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
204 }
205
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000206 /*
207 * Change owner and mode of the tty as required.
Ben Lindstromc88785e2001-08-06 20:47:23 +0000208 * Warn but continue if filesystem is read-only and the uids match/
209 * tty is owned by root.
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000210 */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000211 if (stat(tty, &st))
212 fatal("stat(%.100s) failed: %.100s", tty,
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000213 strerror(errno));
214
Damien Miller73b42d22006-04-22 21:26:08 +1000215#ifdef WITH_SELINUX
216 ssh_selinux_setup_pty(pw->pw_name, tty);
217#endif
218
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000219 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000220 if (chown(tty, pw->pw_uid, gid) < 0) {
Damien Miller9f0f5c62001-12-21 14:45:46 +1100221 if (errno == EROFS &&
Ben Lindstrom8ada5d02002-06-23 21:42:50 +0000222 (st.st_uid == pw->pw_uid || st.st_uid == 0))
Damien Millere8cea9e2003-02-24 11:54:10 +1100223 debug("chown(%.100s, %u, %u) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000224 tty, (u_int)pw->pw_uid, (u_int)gid,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100225 strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000226 else
Ben Lindstrom5c385522002-06-23 21:23:20 +0000227 fatal("chown(%.100s, %u, %u) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000228 tty, (u_int)pw->pw_uid, (u_int)gid,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100229 strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000230 }
231 }
232
233 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000234 if (chmod(tty, mode) < 0) {
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000235 if (errno == EROFS &&
236 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
Damien Millere8cea9e2003-02-24 11:54:10 +1100237 debug("chmod(%.100s, 0%o) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000238 tty, (u_int)mode, strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000239 else
240 fatal("chmod(%.100s, 0%o) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000241 tty, (u_int)mode, strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000242 }
243 }
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100244}