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