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