]> andersk Git - openssh.git/blame - sshpty.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / sshpty.c
CommitLineData
73209290 1/* $OpenBSD: sshpty.c,v 1.28 2007/09/11 23:49:09 stevesk Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5260325f 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
5260325f 6 * Allocating a pseudo-terminal, and making it the controlling tty.
6ae2364d 7 *
bcbf86ec 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".
5260325f 13 */
8efc0c15 14
15#include "includes.h"
bd7c69ea 16
4095f623 17#include <sys/types.h>
31652869 18#include <sys/ioctl.h>
4095f623 19#include <sys/stat.h>
3e9b2b1b 20#include <signal.h>
8efc0c15 21
028094f4 22#include <errno.h>
d3221cca 23#include <fcntl.h>
16fbe330 24#include <grp.h>
a75f5360 25#ifdef HAVE_PATHS_H
6f61e0ec 26# include <paths.h>
a75f5360 27#endif
b1842393 28#include <pwd.h>
31652869 29#include <stdarg.h>
00146caa 30#include <string.h>
a75f5360 31#include <termios.h>
884bcb37 32#ifdef HAVE_UTIL_H
33# include <util.h>
31652869 34#endif
5188ba17 35#include <unistd.h>
884bcb37 36
1729c161 37#include "sshpty.h"
42f11eb2 38#include "log.h"
5ad9f968 39#include "misc.h"
5260325f 40
6d8c4ea4 41#ifdef HAVE_PTY_H
42# include <pty.h>
43#endif
6d8c4ea4 44
8efc0c15 45#ifndef O_NOCTTY
46#define O_NOCTTY 0
47#endif
48
f7b8146b 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
aa3378df 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 */
8efc0c15 62
6ae2364d 63int
148de80c 64pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
8efc0c15 65{
5260325f 66 /* openpty(3) exists in OSF/1 and some other os'es */
b82f1310 67 char *name;
5260325f 68 int i;
8efc0c15 69
b82f1310 70 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
5260325f 71 if (i < 0) {
72 error("openpty: %.100s", strerror(errno));
73 return 0;
74 }
b82f1310 75 name = ttyname(*ttyfd);
76 if (!name)
77 fatal("openpty returns device for which ttyname fails.");
78
79 strlcpy(namebuf, name, namebuflen); /* possible truncation */
5260325f 80 return 1;
8efc0c15 81}
82
5260325f 83/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
8efc0c15 84
6ae2364d 85void
ca75d7de 86pty_release(const char *tty)
8efc0c15 87{
f7b8146b 88#ifndef __APPLE_PRIVPTY__
ca75d7de 89 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
90 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
91 if (chmod(tty, (mode_t) 0666) < 0)
92 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
f7b8146b 93#endif /* __APPLE_PRIVPTY__ */
8efc0c15 94}
95
b8e04133 96/* Makes the tty the process's controlling tty and sets it to sane modes. */
8efc0c15 97
6ae2364d 98void
ca75d7de 99pty_make_controlling_tty(int *ttyfd, const char *tty)
8efc0c15 100{
1a23ac2c 101 int fd;
8140f2aa 102#ifdef USE_VHANGUP
103 void *old;
104#endif /* USE_VHANGUP */
1a23ac2c 105
ef51930f 106#ifdef _UNICOS
68187931 107 if (setsid() < 0)
108 error("setsid: %.100s", strerror(errno));
1a23ac2c 109
ca75d7de 110 fd = open(tty, O_RDWR|O_NOCTTY);
68187931 111 if (fd != -1) {
f00d1f78 112 signal(SIGHUP, SIG_IGN);
68187931 113 ioctl(fd, TCVHUP, (char *)NULL);
f00d1f78 114 signal(SIGHUP, SIG_DFL);
68187931 115 setpgid(0, 0);
116 close(fd);
1a23ac2c 117 } else {
68187931 118 error("Failed to disconnect from controlling tty.");
1a23ac2c 119 }
1a23ac2c 120
68187931 121 debug("Setting controlling tty using TCSETCTTY.");
122 ioctl(*ttyfd, TCSETCTTY, NULL);
123 fd = open("/dev/tty", O_RDWR);
124 if (fd < 0)
ca75d7de 125 error("%.100s: %.100s", tty, strerror(errno));
1a23ac2c 126 close(*ttyfd);
875ec275 127 *ttyfd = fd;
ef51930f 128#else /* _UNICOS */
8efc0c15 129
5260325f 130 /* First disconnect from the old controlling tty. */
8efc0c15 131#ifdef TIOCNOTTY
5ca51e19 132 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
5260325f 133 if (fd >= 0) {
134 (void) ioctl(fd, TIOCNOTTY, NULL);
135 close(fd);
136 }
8efc0c15 137#endif /* TIOCNOTTY */
5260325f 138 if (setsid() < 0)
139 error("setsid: %.100s", strerror(errno));
140
aa3378df 141 /*
142 * Verify that we are successfully disconnected from the controlling
143 * tty.
144 */
5ca51e19 145 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
5260325f 146 if (fd >= 0) {
147 error("Failed to disconnect from controlling tty.");
148 close(fd);
149 }
150 /* Make it our controlling tty. */
8efc0c15 151#ifdef TIOCSCTTY
5260325f 152 debug("Setting controlling tty using TIOCSCTTY.");
a3bf38d0 153 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
154 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
8efc0c15 155#endif /* TIOCSCTTY */
35fc74ed 156#ifdef NEED_SETPGRP
66d6c27e 157 if (setpgrp(0,0) < 0)
158 error("SETPGRP %s",strerror(errno));
35fc74ed 159#endif /* NEED_SETPGRP */
3c62e7eb 160#ifdef USE_VHANGUP
f00d1f78 161 old = signal(SIGHUP, SIG_IGN);
4e577b89 162 vhangup();
f00d1f78 163 signal(SIGHUP, old);
3c62e7eb 164#endif /* USE_VHANGUP */
ca75d7de 165 fd = open(tty, O_RDWR);
4e577b89 166 if (fd < 0) {
ca75d7de 167 error("%.100s: %.100s", tty, strerror(errno));
4e577b89 168 } else {
3c62e7eb 169#ifdef USE_VHANGUP
4e577b89 170 close(*ttyfd);
171 *ttyfd = fd;
3c62e7eb 172#else /* USE_VHANGUP */
5260325f 173 close(fd);
3c62e7eb 174#endif /* USE_VHANGUP */
4e577b89 175 }
5260325f 176 /* Verify that we now have a controlling tty. */
5ca51e19 177 fd = open(_PATH_TTY, O_WRONLY);
5260325f 178 if (fd < 0)
179 error("open /dev/tty failed - could not set controlling tty: %.100s",
184eed6a 180 strerror(errno));
aff51935 181 else
5260325f 182 close(fd);
ef51930f 183#endif /* _UNICOS */
8efc0c15 184}
185
186/* Changes the window size associated with the pty. */
187
6ae2364d 188void
148de80c 189pty_change_window_size(int ptyfd, u_int row, u_int col,
190 u_int xpixel, u_int ypixel)
8efc0c15 191{
5260325f 192 struct winsize w;
f09ce20a 193
148de80c 194 /* may truncate u_int -> u_short */
5260325f 195 w.ws_row = row;
196 w.ws_col = col;
197 w.ws_xpixel = xpixel;
198 w.ws_ypixel = ypixel;
199 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
8efc0c15 200}
817175bc 201
202void
ca75d7de 203pty_setowner(struct passwd *pw, const char *tty)
817175bc 204{
205 struct group *grp;
206 gid_t gid;
207 mode_t mode;
227e8e86 208 struct stat st;
817175bc 209
210 /* Determine the group to make the owner of the tty. */
211 grp = getgrnam("tty");
212 if (grp) {
213 gid = grp->gr_gid;
214 mode = S_IRUSR | S_IWUSR | S_IWGRP;
215 } else {
216 gid = pw->pw_gid;
217 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
218 }
219
227e8e86 220 /*
221 * Change owner and mode of the tty as required.
4f7893dc 222 * Warn but continue if filesystem is read-only and the uids match/
223 * tty is owned by root.
227e8e86 224 */
ca75d7de 225 if (stat(tty, &st))
226 fatal("stat(%.100s) failed: %.100s", tty,
227e8e86 227 strerror(errno));
228
ef4d1846 229#ifdef WITH_SELINUX
230 ssh_selinux_setup_pty(pw->pw_name, tty);
231#endif
232
227e8e86 233 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
ca75d7de 234 if (chown(tty, pw->pw_uid, gid) < 0) {
184eed6a 235 if (errno == EROFS &&
f09ce20a 236 (st.st_uid == pw->pw_uid || st.st_uid == 0))
d6dc3576 237 debug("chown(%.100s, %u, %u) failed: %.100s",
ca75d7de 238 tty, (u_int)pw->pw_uid, (u_int)gid,
184eed6a 239 strerror(errno));
227e8e86 240 else
d6133f43 241 fatal("chown(%.100s, %u, %u) failed: %.100s",
ca75d7de 242 tty, (u_int)pw->pw_uid, (u_int)gid,
184eed6a 243 strerror(errno));
227e8e86 244 }
245 }
246
247 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
ca75d7de 248 if (chmod(tty, mode) < 0) {
227e8e86 249 if (errno == EROFS &&
250 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
d6dc3576 251 debug("chmod(%.100s, 0%o) failed: %.100s",
ca75d7de 252 tty, (u_int)mode, strerror(errno));
227e8e86 253 else
254 fatal("chmod(%.100s, 0%o) failed: %.100s",
ca75d7de 255 tty, (u_int)mode, strerror(errno));
227e8e86 256 }
257 }
817175bc 258}
This page took 0.364179 seconds and 5 git commands to generate.