Home | History | Annotate | Line # | Download | only in dist
sshpty.c revision 1.2.26.1
      1 /*	$NetBSD: sshpty.c,v 1.2.26.1 2015/04/30 06:07:31 riz Exp $	*/
      2 /* $OpenBSD: sshpty.c,v 1.29 2014/09/03 18:55:07 djm Exp $ */
      3 /*
      4  * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
      5  * Copyright (c) 1995 Tatu Ylonen <ylo (at) 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.2.26.1 2015/04/30 06:07:31 riz 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 
     47 int
     48 pty_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 < 0) {
     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 
     64 void
     65 pty_release(const char *tty)
     66 {
     67 	if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
     68 		error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
     69 	if (chmod(tty, (mode_t) 0666) < 0)
     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 
     75 void
     76 pty_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() < 0)
     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) < 0)
    104 		error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
    105 #endif /* TIOCSCTTY */
    106 	fd = open(tty, O_RDWR);
    107 	if (fd < 0)
    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 < 0)
    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 
    123 void
    124 pty_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 
    137 void
    138 pty_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 	gid = (grp != NULL) ? grp->gr_gid : pw->pw_gid;
    148 	mode = (grp != NULL) ? 0622 : 0600;
    149 
    150 	/*
    151 	 * Change owner and mode of the tty as required.
    152 	 * Warn but continue if filesystem is read-only and the uids match/
    153 	 * tty is owned by root.
    154 	 */
    155 	if (stat(tty, &st))
    156 		fatal("stat(%.100s) failed: %.100s", tty,
    157 		    strerror(errno));
    158 
    159 	if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
    160 		if (chown(tty, pw->pw_uid, gid) < 0) {
    161 			if (errno == EROFS &&
    162 			    (st.st_uid == pw->pw_uid || st.st_uid == 0))
    163 				debug("chown(%.100s, %u, %u) failed: %.100s",
    164 				    tty, (u_int)pw->pw_uid, (u_int)gid,
    165 				    strerror(errno));
    166 			else
    167 				fatal("chown(%.100s, %u, %u) failed: %.100s",
    168 				    tty, (u_int)pw->pw_uid, (u_int)gid,
    169 				    strerror(errno));
    170 		}
    171 	}
    172 
    173 	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
    174 		if (chmod(tty, mode) < 0) {
    175 			if (errno == EROFS &&
    176 			    (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
    177 				debug("chmod(%.100s, 0%o) failed: %.100s",
    178 				    tty, (u_int)mode, strerror(errno));
    179 			else
    180 				fatal("chmod(%.100s, 0%o) failed: %.100s",
    181 				    tty, (u_int)mode, strerror(errno));
    182 		}
    183 	}
    184 }
    185