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