blob: bce09e255e0d17a9c6fb48fef5fb0eff0456a377 [file] [log] [blame]
deraadt@openbsd.org8b4cc4b2019-07-04 16:20:10 +00001/* $OpenBSD: sshpty.c,v 1.34 2019/07/04 16:20:10 deraadt 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
Damien Millerf17883e2006-03-15 11:45:54 +110017#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100018#include <sys/ioctl.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Tim Rice4b23f7c2006-03-14 22:09:50 -080020#include <signal.h>
Damien Miller95def091999-11-25 00:26:21 +110021
Darren Tucker39972492006-07-12 22:22:46 +100022#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100023#include <fcntl.h>
Damien Miller427a1d52006-07-10 20:20:33 +100024#include <grp.h>
Damien Miller03e20032006-03-15 11:16:59 +110025#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110026# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110027#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100028#include <pwd.h>
Damien Millerd7834352006-08-05 12:39:39 +100029#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100030#include <string.h>
Damien Miller03e20032006-03-15 11:16:59 +110031#include <termios.h>
Damien Miller36b339a1999-12-14 10:54:47 +110032#ifdef HAVE_UTIL_H
33# include <util.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#endif
Damien Millere6b3b612006-07-24 14:01:23 +100035#include <unistd.h>
Damien Miller36b339a1999-12-14 10:54:47 +110036
Ben Lindstromd95c09c2001-02-18 19:13:33 +000037#include "sshpty.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038#include "log.h"
Kevin Steves974fb9c2001-06-15 00:04:23 +000039#include "misc.h"
Damien Miller356a0b01999-11-08 15:30:59 +110040
Damien Millerfce16481999-12-08 08:53:52 +110041#ifdef HAVE_PTY_H
42# include <pty.h>
43#endif
Damien Millerfce16481999-12-08 08:53:52 +110044
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045#ifndef O_NOCTTY
46#define O_NOCTTY 0
47#endif
48
Damien Miller2de76242009-02-12 12:19:20 +110049#ifdef __APPLE__
50# include <AvailabilityMacros.h>
51# if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
52# define __APPLE_PRIVPTY__
53# endif
54#endif
55
Damien Miller5428f641999-11-25 11:54:57 +110056/*
57 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
58 * nonzero if a pty was successfully allocated. On success, open file
59 * descriptors for the pty and tty sides and the name of the tty side are
60 * returned (the buffer must be able to hold at least 64 characters).
61 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller4af51302000-04-16 11:18:38 +100063int
Damien Miller71a73672006-03-26 14:04:36 +110064pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065{
Damien Miller95def091999-11-25 00:26:21 +110066 /* openpty(3) exists in OSF/1 and some other os'es */
Damien Miller99e92432001-02-18 12:49:35 +110067 char *name;
Damien Miller95def091999-11-25 00:26:21 +110068 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Miller99e92432001-02-18 12:49:35 +110070 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +000071 if (i == -1) {
Damien Miller95def091999-11-25 00:26:21 +110072 error("openpty: %.100s", strerror(errno));
73 return 0;
74 }
Damien Miller99e92432001-02-18 12:49:35 +110075 name = ttyname(*ttyfd);
76 if (!name)
77 fatal("openpty returns device for which ttyname fails.");
78
79 strlcpy(namebuf, name, namebuflen); /* possible truncation */
Damien Miller95def091999-11-25 00:26:21 +110080 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081}
82
Damien Miller95def091999-11-25 00:26:21 +110083/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller4af51302000-04-16 11:18:38 +100085void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100086pty_release(const char *tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087{
Damien Miller59798642015-04-09 10:14:48 +100088#if !defined(__APPLE_PRIVPTY__) && !defined(HAVE_OPENPTY)
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +000089 if (chown(tty, (uid_t) 0, (gid_t) 0) == -1)
Darren Tucker3f9fdc72004-06-22 12:56:01 +100090 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +000091 if (chmod(tty, (mode_t) 0666) == -1)
Darren Tucker3f9fdc72004-06-22 12:56:01 +100092 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
Damien Miller59798642015-04-09 10:14:48 +100093#endif /* !__APPLE_PRIVPTY__ && !HAVE_OPENPTY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094}
95
Damien Miller469954d2003-06-18 20:25:33 +100096/* Makes the tty the process's controlling tty and sets it to sane modes. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller4af51302000-04-16 11:18:38 +100098void
Darren Tucker3f9fdc72004-06-22 12:56:01 +100099pty_make_controlling_tty(int *ttyfd, const char *tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100{
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000101 int fd;
102
Damien Miller95def091999-11-25 00:26:21 +1100103 /* First disconnect from the old controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000105 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +1100106 if (fd >= 0) {
107 (void) ioctl(fd, TIOCNOTTY, NULL);
108 close(fd);
109 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110#endif /* TIOCNOTTY */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000111 if (setsid() == -1)
Damien Miller95def091999-11-25 00:26:21 +1100112 error("setsid: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller5428f641999-11-25 11:54:57 +1100114 /*
115 * Verify that we are successfully disconnected from the controlling
116 * tty.
117 */
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 error("Failed to disconnect from controlling tty.");
121 close(fd);
122 }
123 /* Make it our controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124#ifdef TIOCSCTTY
Damien Miller95def091999-11-25 00:26:21 +1100125 debug("Setting controlling tty using TIOCSCTTY.");
Kevin Steves6c0d6272000-11-12 09:22:29 +0000126 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
127 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128#endif /* TIOCSCTTY */
Darren Tucker2be1cbb2005-05-27 21:13:40 +1000129#ifdef NEED_SETPGRP
Ben Lindstromb5628642000-10-18 00:02:25 +0000130 if (setpgrp(0,0) < 0)
131 error("SETPGRP %s",strerror(errno));
Darren Tucker2be1cbb2005-05-27 21:13:40 +1000132#endif /* NEED_SETPGRP */
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000133 fd = open(tty, O_RDWR);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000134 if (fd == -1)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000135 error("%.100s: %.100s", tty, strerror(errno));
Darren Tucker8f750cc2016-09-12 14:43:58 +1000136 else
Damien Miller95def091999-11-25 00:26:21 +1100137 close(fd);
Darren Tucker8f750cc2016-09-12 14:43:58 +1000138
Damien Miller95def091999-11-25 00:26:21 +1100139 /* Verify that we now have a controlling tty. */
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000140 fd = open(_PATH_TTY, O_WRONLY);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000141 if (fd == -1)
Damien Miller95def091999-11-25 00:26:21 +1100142 error("open /dev/tty failed - could not set controlling tty: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100143 strerror(errno));
Damien Millera8e06ce2003-11-21 23:48:55 +1100144 else
Damien Miller95def091999-11-25 00:26:21 +1100145 close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146}
147
148/* Changes the window size associated with the pty. */
149
Damien Miller4af51302000-04-16 11:18:38 +1000150void
Damien Miller71a73672006-03-26 14:04:36 +1100151pty_change_window_size(int ptyfd, u_int row, u_int col,
152 u_int xpixel, u_int ypixel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Miller95def091999-11-25 00:26:21 +1100154 struct winsize w;
Ben Lindstrom8ada5d02002-06-23 21:42:50 +0000155
Damien Miller71a73672006-03-26 14:04:36 +1100156 /* may truncate u_int -> u_short */
Damien Miller95def091999-11-25 00:26:21 +1100157 w.ws_row = row;
158 w.ws_col = col;
159 w.ws_xpixel = xpixel;
160 w.ws_ypixel = ypixel;
161 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162}
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100163
164void
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000165pty_setowner(struct passwd *pw, const char *tty)
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100166{
167 struct group *grp;
168 gid_t gid;
169 mode_t mode;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000170 struct stat st;
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100171
172 /* Determine the group to make the owner of the tty. */
173 grp = getgrnam("tty");
deraadt@openbsd.org8b4cc4b2019-07-04 16:20:10 +0000174 if (grp == NULL)
Darren Tuckerc5cfa902019-07-05 13:21:45 +1000175 debug("%s: no tty group", __func__);
djm@openbsd.orga5883d42014-09-03 18:55:07 +0000176 gid = (grp != NULL) ? grp->gr_gid : pw->pw_gid;
djm@openbsd.org6f941392015-07-30 23:09:15 +0000177 mode = (grp != NULL) ? 0620 : 0600;
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100178
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000179 /*
180 * Change owner and mode of the tty as required.
Ben Lindstromc88785e2001-08-06 20:47:23 +0000181 * Warn but continue if filesystem is read-only and the uids match/
182 * tty is owned by root.
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000183 */
deraadt@openbsd.org48cccc22019-07-04 16:16:51 +0000184 if (stat(tty, &st) == -1)
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000185 fatal("stat(%.100s) failed: %.100s", tty,
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000186 strerror(errno));
187
Damien Miller73b42d22006-04-22 21:26:08 +1000188#ifdef WITH_SELINUX
189 ssh_selinux_setup_pty(pw->pw_name, tty);
190#endif
191
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000192 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000193 if (chown(tty, pw->pw_uid, gid) == -1) {
Damien Miller9f0f5c62001-12-21 14:45:46 +1100194 if (errno == EROFS &&
Ben Lindstrom8ada5d02002-06-23 21:42:50 +0000195 (st.st_uid == pw->pw_uid || st.st_uid == 0))
Damien Millere8cea9e2003-02-24 11:54:10 +1100196 debug("chown(%.100s, %u, %u) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000197 tty, (u_int)pw->pw_uid, (u_int)gid,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100198 strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000199 else
Ben Lindstrom5c385522002-06-23 21:23:20 +0000200 fatal("chown(%.100s, %u, %u) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000201 tty, (u_int)pw->pw_uid, (u_int)gid,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100202 strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000203 }
204 }
205
206 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000207 if (chmod(tty, mode) == -1) {
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000208 if (errno == EROFS &&
209 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
Damien Millere8cea9e2003-02-24 11:54:10 +1100210 debug("chmod(%.100s, 0%o) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000211 tty, (u_int)mode, strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000212 else
213 fatal("chmod(%.100s, 0%o) failed: %.100s",
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000214 tty, (u_int)mode, strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000215 }
216 }
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100217}
dtucker@openbsd.orgb0899ee2016-11-29 03:54:50 +0000218
219/* Disconnect from the controlling tty. */
220void
221disconnect_controlling_tty(void)
222{
223#ifdef TIOCNOTTY
224 int fd;
225
226 if ((fd = open(_PATH_TTY, O_RDWR | O_NOCTTY)) >= 0) {
227 (void) ioctl(fd, TIOCNOTTY, NULL);
228 close(fd);
229 }
230#endif /* TIOCNOTTY */
231}