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