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