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