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