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