1/*	$NetBSD: sshpty.c,v 1.8 2019/10/12 18:32:22 christos Exp $	*/
2/* $OpenBSD: sshpty.c,v 1.34 2019/07/04 16:20:10 deraadt Exp $ */
3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 *                    All rights reserved
7 * Allocating a pseudo-terminal, and making it the controlling tty.
8 *
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose.  Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 */
15
16#include "includes.h"
17__RCSID("$NetBSD: sshpty.c,v 1.8 2019/10/12 18:32:22 christos Exp $");
18#include <sys/types.h>
19#include <sys/ioctl.h>
20#include <sys/stat.h>
21
22#include <errno.h>
23#include <fcntl.h>
24#include <grp.h>
25#include <paths.h>
26#include <pwd.h>
27#include <stdarg.h>
28#include <string.h>
29#include <termios.h>
30#include <unistd.h>
31#include <util.h>
32
33#include "sshpty.h"
34#include "log.h"
35
36#ifndef O_NOCTTY
37#define O_NOCTTY 0
38#endif
39
40/*
41 * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
42 * nonzero if a pty was successfully allocated.  On success, open file
43 * descriptors for the pty and tty sides and the name of the tty side are
44 * returned (the buffer must be able to hold at least 64 characters).
45 */
46
47int
48pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
49{
50	char buf[64];
51	int i;
52
53	i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
54	if (i == -1) {
55		error("openpty: %.100s", strerror(errno));
56		return 0;
57	}
58	strlcpy(namebuf, buf, namebuflen);	/* possible truncation */
59	return 1;
60}
61
62/* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
63
64void
65pty_release(const char *tty)
66{
67	if (chown(tty, (uid_t) 0, (gid_t) 0) == -1)
68		error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
69	if (chmod(tty, (mode_t) 0666) == -1)
70		error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
71}
72
73/* Makes the tty the process's controlling tty and sets it to sane modes. */
74
75void
76pty_make_controlling_tty(int *ttyfd, const char *tty)
77{
78	int fd;
79
80	/* First disconnect from the old controlling tty. */
81#ifdef TIOCNOTTY
82	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
83	if (fd >= 0) {
84		(void) ioctl(fd, TIOCNOTTY, NULL);
85		close(fd);
86	}
87#endif /* TIOCNOTTY */
88	if (setsid() == -1)
89		error("setsid: %.100s", strerror(errno));
90
91	/*
92	 * Verify that we are successfully disconnected from the controlling
93	 * tty.
94	 */
95	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
96	if (fd >= 0) {
97		error("Failed to disconnect from controlling tty.");
98		close(fd);
99	}
100	/* Make it our controlling tty. */
101#ifdef TIOCSCTTY
102	debug("Setting controlling tty using TIOCSCTTY.");
103	if (ioctl(*ttyfd, TIOCSCTTY, NULL) == -1)
104		error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
105#endif /* TIOCSCTTY */
106	fd = open(tty, O_RDWR);
107	if (fd == -1)
108		error("%.100s: %.100s", tty, strerror(errno));
109	else
110		close(fd);
111
112	/* Verify that we now have a controlling tty. */
113	fd = open(_PATH_TTY, O_WRONLY);
114	if (fd == -1)
115		error("open /dev/tty failed - could not set controlling tty: %.100s",
116		    strerror(errno));
117	else
118		close(fd);
119}
120
121/* Changes the window size associated with the pty. */
122
123void
124pty_change_window_size(int ptyfd, u_int row, u_int col,
125	u_int xpixel, u_int ypixel)
126{
127	struct winsize w;
128
129	/* may truncate u_int -> u_short */
130	w.ws_row = row;
131	w.ws_col = col;
132	w.ws_xpixel = xpixel;
133	w.ws_ypixel = ypixel;
134	(void) ioctl(ptyfd, TIOCSWINSZ, &w);
135}
136
137void
138pty_setowner(struct passwd *pw, const char *tty)
139{
140	struct group *grp;
141	gid_t gid;
142	mode_t mode;
143	struct stat st;
144
145	/* Determine the group to make the owner of the tty. */
146	grp = getgrnam("tty");
147	if (grp == NULL)
148		fatal("no tty group");
149	gid = (grp != NULL) ? grp->gr_gid : pw->pw_gid;
150	mode = (grp != NULL) ? 0620 : 0600;
151
152	/*
153	 * Change owner and mode of the tty as required.
154	 * Warn but continue if filesystem is read-only and the uids match/
155	 * tty is owned by root.
156	 */
157	if (stat(tty, &st) == -1)
158		fatal("stat(%.100s) failed: %.100s", tty,
159		    strerror(errno));
160
161	if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
162		if (chown(tty, pw->pw_uid, gid) == -1) {
163			if (errno == EROFS &&
164			    (st.st_uid == pw->pw_uid || st.st_uid == 0))
165				debug("chown(%.100s, %u, %u) failed: %.100s",
166				    tty, (u_int)pw->pw_uid, (u_int)gid,
167				    strerror(errno));
168			else
169				fatal("chown(%.100s, %u, %u) failed: %.100s",
170				    tty, (u_int)pw->pw_uid, (u_int)gid,
171				    strerror(errno));
172		}
173	}
174
175	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
176		if (chmod(tty, mode) == -1) {
177			if (errno == EROFS &&
178			    (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
179				debug("chmod(%.100s, 0%o) failed: %.100s",
180				    tty, (u_int)mode, strerror(errno));
181			else
182				fatal("chmod(%.100s, 0%o) failed: %.100s",
183				    tty, (u_int)mode, strerror(errno));
184		}
185	}
186}
187
188/* Disconnect from the controlling tty. */
189void
190disconnect_controlling_tty(void)
191{
192	int fd;
193
194	if ((fd = open(_PATH_TTY, O_RDWR | O_NOCTTY)) >= 0) {
195		(void) ioctl(fd, TIOCNOTTY, NULL);
196		close(fd);
197	}
198}
199