Home | History | Annotate | Download | only in openssh
      1 /* $OpenBSD: sshpty.c,v 1.29 2014/09/03 18:55:07 djm Exp $ */
      2 /*
      3  * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
      4  * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
      5  *                    All rights reserved
      6  * Allocating a pseudo-terminal, and making it the controlling tty.
      7  *
      8  * 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".
     13  */
     14 
     15 #include "includes.h"
     16 
     17 #include <sys/types.h>
     18 #include <sys/ioctl.h>
     19 #include <sys/stat.h>
     20 #include <signal.h>
     21 
     22 #include <errno.h>
     23 #include <fcntl.h>
     24 #include <grp.h>
     25 #ifdef HAVE_PATHS_H
     26 # include <paths.h>
     27 #endif
     28 #include <pwd.h>
     29 #include <stdarg.h>
     30 #include <string.h>
     31 #include <termios.h>
     32 #ifdef HAVE_UTIL_H
     33 # include <util.h>
     34 #endif
     35 #include <unistd.h>
     36 
     37 #include "sshpty.h"
     38 #include "log.h"
     39 #include "misc.h"
     40 
     41 #ifdef HAVE_PTY_H
     42 # include <pty.h>
     43 #endif
     44 
     45 #ifndef O_NOCTTY
     46 #define O_NOCTTY 0
     47 #endif
     48 
     49 #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 
     56 /*
     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  */
     62 
     63 int
     64 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
     65 {
     66 	/* openpty(3) exists in OSF/1 and some other os'es */
     67 	char *name;
     68 	int i;
     69 
     70 	i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
     71 	if (i < 0) {
     72 		error("openpty: %.100s", strerror(errno));
     73 		return 0;
     74 	}
     75 #ifdef ANDROID
     76 	/* Android does not have a working ttyname() */
     77 	name = "/dev/ptmx";
     78 #else
     79 	name = ttyname(*ttyfd);
     80 	if (!name)
     81 		fatal("openpty returns device for which ttyname fails.");
     82 #endif
     83 
     84 	strlcpy(namebuf, name, namebuflen);	/* possible truncation */
     85 	return 1;
     86 }
     87 
     88 /* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
     89 
     90 void
     91 pty_release(const char *tty)
     92 {
     93 #ifndef __APPLE_PRIVPTY__
     94 	if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
     95 		error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
     96 	if (chmod(tty, (mode_t) 0666) < 0)
     97 		error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
     98 #endif /* __APPLE_PRIVPTY__ */
     99 }
    100 
    101 /* Makes the tty the process's controlling tty and sets it to sane modes. */
    102 
    103 void
    104 pty_make_controlling_tty(int *ttyfd, const char *tty)
    105 {
    106 	int fd;
    107 
    108 #ifdef _UNICOS
    109 	if (setsid() < 0)
    110 		error("setsid: %.100s", strerror(errno));
    111 
    112 	fd = open(tty, O_RDWR|O_NOCTTY);
    113 	if (fd != -1) {
    114 		signal(SIGHUP, SIG_IGN);
    115 		ioctl(fd, TCVHUP, (char *)NULL);
    116 		signal(SIGHUP, SIG_DFL);
    117 		setpgid(0, 0);
    118 		close(fd);
    119 	} else {
    120 		error("Failed to disconnect from controlling tty.");
    121 	}
    122 
    123 	debug("Setting controlling tty using TCSETCTTY.");
    124 	ioctl(*ttyfd, TCSETCTTY, NULL);
    125 	fd = open("/dev/tty", O_RDWR);
    126 	if (fd < 0)
    127 		error("%.100s: %.100s", tty, strerror(errno));
    128 	close(*ttyfd);
    129 	*ttyfd = fd;
    130 #else /* _UNICOS */
    131 
    132 	/* First disconnect from the old controlling tty. */
    133 #ifdef TIOCNOTTY
    134 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
    135 	if (fd >= 0) {
    136 		(void) ioctl(fd, TIOCNOTTY, NULL);
    137 		close(fd);
    138 	}
    139 #endif /* TIOCNOTTY */
    140 	if (setsid() < 0)
    141 		error("setsid: %.100s", strerror(errno));
    142 
    143 	/*
    144 	 * Verify that we are successfully disconnected from the controlling
    145 	 * tty.
    146 	 */
    147 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
    148 	if (fd >= 0) {
    149 		error("Failed to disconnect from controlling tty.");
    150 		close(fd);
    151 	}
    152 	/* Make it our controlling tty. */
    153 #ifdef TIOCSCTTY
    154 	debug("Setting controlling tty using TIOCSCTTY.");
    155 	if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
    156 		error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
    157 #endif /* TIOCSCTTY */
    158 #ifdef NEED_SETPGRP
    159 	if (setpgrp(0,0) < 0)
    160 		error("SETPGRP %s",strerror(errno));
    161 #endif /* NEED_SETPGRP */
    162 	fd = open(tty, O_RDWR);
    163 	if (fd < 0) {
    164 		error("%.100s: %.100s", tty, strerror(errno));
    165 	} else {
    166 		close(fd);
    167 	}
    168 	/* Verify that we now have a controlling tty. */
    169 	fd = open(_PATH_TTY, O_WRONLY);
    170 	if (fd < 0)
    171 		error("open /dev/tty failed - could not set controlling tty: %.100s",
    172 		    strerror(errno));
    173 	else
    174 		close(fd);
    175 #endif /* _UNICOS */
    176 }
    177 
    178 /* Changes the window size associated with the pty. */
    179 
    180 void
    181 pty_change_window_size(int ptyfd, u_int row, u_int col,
    182 	u_int xpixel, u_int ypixel)
    183 {
    184 	struct winsize w;
    185 
    186 	/* may truncate u_int -> u_short */
    187 	w.ws_row = row;
    188 	w.ws_col = col;
    189 	w.ws_xpixel = xpixel;
    190 	w.ws_ypixel = ypixel;
    191 	(void) ioctl(ptyfd, TIOCSWINSZ, &w);
    192 }
    193 
    194 void
    195 pty_setowner(struct passwd *pw, const char *tty)
    196 {
    197 	struct group *grp;
    198 	gid_t gid;
    199 	mode_t mode;
    200 	struct stat st;
    201 
    202 	/* Determine the group to make the owner of the tty. */
    203 	grp = getgrnam("tty");
    204 	gid = (grp != NULL) ? grp->gr_gid : pw->pw_gid;
    205 	mode = (grp != NULL) ? 0622 : 0600;
    206 
    207 	/*
    208 	 * Change owner and mode of the tty as required.
    209 	 * Warn but continue if filesystem is read-only and the uids match/
    210 	 * tty is owned by root.
    211 	 */
    212 	if (stat(tty, &st))
    213 		fatal("stat(%.100s) failed: %.100s", tty,
    214 		    strerror(errno));
    215 
    216 #ifdef WITH_SELINUX
    217 	ssh_selinux_setup_pty(pw->pw_name, tty);
    218 #endif
    219 
    220 	if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
    221 		if (chown(tty, pw->pw_uid, gid) < 0) {
    222 			if (errno == EROFS &&
    223 			    (st.st_uid == pw->pw_uid || st.st_uid == 0))
    224 				debug("chown(%.100s, %u, %u) failed: %.100s",
    225 				    tty, (u_int)pw->pw_uid, (u_int)gid,
    226 				    strerror(errno));
    227 			else
    228 				fatal("chown(%.100s, %u, %u) failed: %.100s",
    229 				    tty, (u_int)pw->pw_uid, (u_int)gid,
    230 				    strerror(errno));
    231 		}
    232 	}
    233 
    234 	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
    235 		if (chmod(tty, mode) < 0) {
    236 			if (errno == EROFS &&
    237 			    (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
    238 				debug("chmod(%.100s, 0%o) failed: %.100s",
    239 				    tty, (u_int)mode, strerror(errno));
    240 			else
    241 				fatal("chmod(%.100s, 0%o) failed: %.100s",
    242 				    tty, (u_int)mode, strerror(errno));
    243 		}
    244 	}
    245 }
    246