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