1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi>
3*7c478bd9Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4*7c478bd9Sstevel@tonic-gate * All rights reserved
5*7c478bd9Sstevel@tonic-gate * Allocating a pseudo-terminal, and making it the controlling tty.
6*7c478bd9Sstevel@tonic-gate *
7*7c478bd9Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
8*7c478bd9Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
9*7c478bd9Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
10*7c478bd9Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
11*7c478bd9Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
12*7c478bd9Sstevel@tonic-gate */
13*7c478bd9Sstevel@tonic-gate
14*7c478bd9Sstevel@tonic-gate #include "includes.h"
15*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: sshpty.c,v 1.7 2002/06/24 17:57:20 deraadt Exp $");
16*7c478bd9Sstevel@tonic-gate
17*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
18*7c478bd9Sstevel@tonic-gate
19*7c478bd9Sstevel@tonic-gate #ifdef HAVE_UTIL_H
20*7c478bd9Sstevel@tonic-gate # include <util.h>
21*7c478bd9Sstevel@tonic-gate #endif /* HAVE_UTIL_H */
22*7c478bd9Sstevel@tonic-gate
23*7c478bd9Sstevel@tonic-gate #include "sshpty.h"
24*7c478bd9Sstevel@tonic-gate #include "log.h"
25*7c478bd9Sstevel@tonic-gate #include "misc.h"
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
28*7c478bd9Sstevel@tonic-gate #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
29*7c478bd9Sstevel@tonic-gate #undef HAVE_DEV_PTMX
30*7c478bd9Sstevel@tonic-gate #endif
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #ifdef HAVE_PTY_H
33*7c478bd9Sstevel@tonic-gate # include <pty.h>
34*7c478bd9Sstevel@tonic-gate #endif
35*7c478bd9Sstevel@tonic-gate #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
36*7c478bd9Sstevel@tonic-gate # include <sys/stropts.h>
37*7c478bd9Sstevel@tonic-gate #endif
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate #ifndef O_NOCTTY
40*7c478bd9Sstevel@tonic-gate #define O_NOCTTY 0
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
45*7c478bd9Sstevel@tonic-gate * nonzero if a pty was successfully allocated. On success, open file
46*7c478bd9Sstevel@tonic-gate * descriptors for the pty and tty sides and the name of the tty side are
47*7c478bd9Sstevel@tonic-gate * returned (the buffer must be able to hold at least 64 characters).
48*7c478bd9Sstevel@tonic-gate */
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate int
pty_allocate(int * ptyfd,int * ttyfd,char * namebuf,int namebuflen)51*7c478bd9Sstevel@tonic-gate pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate #if defined(HAVE_OPENPTY) || defined(BSD4_4)
54*7c478bd9Sstevel@tonic-gate /* openpty(3) exists in OSF/1 and some other os'es */
55*7c478bd9Sstevel@tonic-gate char *name;
56*7c478bd9Sstevel@tonic-gate int i;
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
59*7c478bd9Sstevel@tonic-gate if (i < 0) {
60*7c478bd9Sstevel@tonic-gate error("openpty: %.100s", strerror(errno));
61*7c478bd9Sstevel@tonic-gate return 0;
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate name = ttyname(*ttyfd);
64*7c478bd9Sstevel@tonic-gate if (!name)
65*7c478bd9Sstevel@tonic-gate fatal("openpty returns device for which ttyname fails.");
66*7c478bd9Sstevel@tonic-gate
67*7c478bd9Sstevel@tonic-gate strlcpy(namebuf, name, namebuflen); /* possible truncation */
68*7c478bd9Sstevel@tonic-gate return 1;
69*7c478bd9Sstevel@tonic-gate #else /* HAVE_OPENPTY */
70*7c478bd9Sstevel@tonic-gate #ifdef HAVE__GETPTY
71*7c478bd9Sstevel@tonic-gate /*
72*7c478bd9Sstevel@tonic-gate * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
73*7c478bd9Sstevel@tonic-gate * pty's automagically when needed
74*7c478bd9Sstevel@tonic-gate */
75*7c478bd9Sstevel@tonic-gate char *slave;
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate slave = _getpty(ptyfd, O_RDWR, 0622, 0);
78*7c478bd9Sstevel@tonic-gate if (slave == NULL) {
79*7c478bd9Sstevel@tonic-gate error("_getpty: %.100s", strerror(errno));
80*7c478bd9Sstevel@tonic-gate return 0;
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate strlcpy(namebuf, slave, namebuflen);
83*7c478bd9Sstevel@tonic-gate /* Open the slave side. */
84*7c478bd9Sstevel@tonic-gate *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
85*7c478bd9Sstevel@tonic-gate if (*ttyfd < 0) {
86*7c478bd9Sstevel@tonic-gate error("%.200s: %.100s", namebuf, strerror(errno));
87*7c478bd9Sstevel@tonic-gate close(*ptyfd);
88*7c478bd9Sstevel@tonic-gate return 0;
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate return 1;
91*7c478bd9Sstevel@tonic-gate #else /* HAVE__GETPTY */
92*7c478bd9Sstevel@tonic-gate #if defined(HAVE_DEV_PTMX)
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
95*7c478bd9Sstevel@tonic-gate * also has bsd-style ptys, but they simply do not work.)
96*7c478bd9Sstevel@tonic-gate */
97*7c478bd9Sstevel@tonic-gate int ptm;
98*7c478bd9Sstevel@tonic-gate char *pts;
99*7c478bd9Sstevel@tonic-gate mysig_t old_signal;
100*7c478bd9Sstevel@tonic-gate
101*7c478bd9Sstevel@tonic-gate ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
102*7c478bd9Sstevel@tonic-gate if (ptm < 0) {
103*7c478bd9Sstevel@tonic-gate error("/dev/ptmx: %.100s", strerror(errno));
104*7c478bd9Sstevel@tonic-gate return 0;
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate old_signal = mysignal(SIGCHLD, SIG_DFL);
107*7c478bd9Sstevel@tonic-gate if (grantpt(ptm) < 0) {
108*7c478bd9Sstevel@tonic-gate error("grantpt: %.100s", strerror(errno));
109*7c478bd9Sstevel@tonic-gate return 0;
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate mysignal(SIGCHLD, old_signal);
112*7c478bd9Sstevel@tonic-gate if (unlockpt(ptm) < 0) {
113*7c478bd9Sstevel@tonic-gate error("unlockpt: %.100s", strerror(errno));
114*7c478bd9Sstevel@tonic-gate return 0;
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate pts = ptsname(ptm);
117*7c478bd9Sstevel@tonic-gate if (pts == NULL)
118*7c478bd9Sstevel@tonic-gate error("Slave pty side name could not be obtained.");
119*7c478bd9Sstevel@tonic-gate strlcpy(namebuf, pts, namebuflen);
120*7c478bd9Sstevel@tonic-gate *ptyfd = ptm;
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate /* Open the slave side. */
123*7c478bd9Sstevel@tonic-gate *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
124*7c478bd9Sstevel@tonic-gate if (*ttyfd < 0) {
125*7c478bd9Sstevel@tonic-gate error("%.100s: %.100s", namebuf, strerror(errno));
126*7c478bd9Sstevel@tonic-gate close(*ptyfd);
127*7c478bd9Sstevel@tonic-gate return 0;
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate #ifndef HAVE_CYGWIN
130*7c478bd9Sstevel@tonic-gate /*
131*7c478bd9Sstevel@tonic-gate * Push the appropriate streams modules, as described in Solaris pts(7).
132*7c478bd9Sstevel@tonic-gate * HP-UX pts(7) doesn't have ttcompat module.
133*7c478bd9Sstevel@tonic-gate */
134*7c478bd9Sstevel@tonic-gate if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
135*7c478bd9Sstevel@tonic-gate error("ioctl I_PUSH ptem: %.100s", strerror(errno));
136*7c478bd9Sstevel@tonic-gate if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
137*7c478bd9Sstevel@tonic-gate error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
138*7c478bd9Sstevel@tonic-gate #ifndef __hpux
139*7c478bd9Sstevel@tonic-gate if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
140*7c478bd9Sstevel@tonic-gate error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
141*7c478bd9Sstevel@tonic-gate #endif
142*7c478bd9Sstevel@tonic-gate #endif
143*7c478bd9Sstevel@tonic-gate return 1;
144*7c478bd9Sstevel@tonic-gate #else /* HAVE_DEV_PTMX */
145*7c478bd9Sstevel@tonic-gate #ifdef HAVE_DEV_PTS_AND_PTC
146*7c478bd9Sstevel@tonic-gate /* AIX-style pty code. */
147*7c478bd9Sstevel@tonic-gate const char *name;
148*7c478bd9Sstevel@tonic-gate
149*7c478bd9Sstevel@tonic-gate *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
150*7c478bd9Sstevel@tonic-gate if (*ptyfd < 0) {
151*7c478bd9Sstevel@tonic-gate error("Could not open /dev/ptc: %.100s", strerror(errno));
152*7c478bd9Sstevel@tonic-gate return 0;
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate name = ttyname(*ptyfd);
155*7c478bd9Sstevel@tonic-gate if (!name)
156*7c478bd9Sstevel@tonic-gate fatal("Open of /dev/ptc returns device for which ttyname fails.");
157*7c478bd9Sstevel@tonic-gate strlcpy(namebuf, name, namebuflen);
158*7c478bd9Sstevel@tonic-gate *ttyfd = open(name, O_RDWR | O_NOCTTY);
159*7c478bd9Sstevel@tonic-gate if (*ttyfd < 0) {
160*7c478bd9Sstevel@tonic-gate error("Could not open pty slave side %.100s: %.100s",
161*7c478bd9Sstevel@tonic-gate name, strerror(errno));
162*7c478bd9Sstevel@tonic-gate close(*ptyfd);
163*7c478bd9Sstevel@tonic-gate return 0;
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate return 1;
166*7c478bd9Sstevel@tonic-gate #else /* HAVE_DEV_PTS_AND_PTC */
167*7c478bd9Sstevel@tonic-gate #ifdef _UNICOS
168*7c478bd9Sstevel@tonic-gate char buf[64];
169*7c478bd9Sstevel@tonic-gate int i;
170*7c478bd9Sstevel@tonic-gate int highpty;
171*7c478bd9Sstevel@tonic-gate
172*7c478bd9Sstevel@tonic-gate #ifdef _SC_CRAY_NPTY
173*7c478bd9Sstevel@tonic-gate highpty = sysconf(_SC_CRAY_NPTY);
174*7c478bd9Sstevel@tonic-gate if (highpty == -1)
175*7c478bd9Sstevel@tonic-gate highpty = 128;
176*7c478bd9Sstevel@tonic-gate #else
177*7c478bd9Sstevel@tonic-gate highpty = 128;
178*7c478bd9Sstevel@tonic-gate #endif
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate for (i = 0; i < highpty; i++) {
181*7c478bd9Sstevel@tonic-gate snprintf(buf, sizeof(buf), "/dev/pty/%03d", i);
182*7c478bd9Sstevel@tonic-gate *ptyfd = open(buf, O_RDWR|O_NOCTTY);
183*7c478bd9Sstevel@tonic-gate if (*ptyfd < 0)
184*7c478bd9Sstevel@tonic-gate continue;
185*7c478bd9Sstevel@tonic-gate snprintf(namebuf, namebuflen, "/dev/ttyp%03d", i);
186*7c478bd9Sstevel@tonic-gate /* Open the slave side. */
187*7c478bd9Sstevel@tonic-gate *ttyfd = open(namebuf, O_RDWR|O_NOCTTY);
188*7c478bd9Sstevel@tonic-gate if (*ttyfd < 0) {
189*7c478bd9Sstevel@tonic-gate error("%.100s: %.100s", namebuf, strerror(errno));
190*7c478bd9Sstevel@tonic-gate close(*ptyfd);
191*7c478bd9Sstevel@tonic-gate return 0;
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate return 1;
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate return 0;
196*7c478bd9Sstevel@tonic-gate #else
197*7c478bd9Sstevel@tonic-gate /* BSD-style pty code. */
198*7c478bd9Sstevel@tonic-gate char buf[64];
199*7c478bd9Sstevel@tonic-gate int i;
200*7c478bd9Sstevel@tonic-gate const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
201*7c478bd9Sstevel@tonic-gate const char *ptyminors = "0123456789abcdef";
202*7c478bd9Sstevel@tonic-gate int num_minors = strlen(ptyminors);
203*7c478bd9Sstevel@tonic-gate int num_ptys = strlen(ptymajors) * num_minors;
204*7c478bd9Sstevel@tonic-gate struct termios tio;
205*7c478bd9Sstevel@tonic-gate
206*7c478bd9Sstevel@tonic-gate for (i = 0; i < num_ptys; i++) {
207*7c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
208*7c478bd9Sstevel@tonic-gate ptyminors[i % num_minors]);
209*7c478bd9Sstevel@tonic-gate snprintf(namebuf, namebuflen, "/dev/tty%c%c",
210*7c478bd9Sstevel@tonic-gate ptymajors[i / num_minors], ptyminors[i % num_minors]);
211*7c478bd9Sstevel@tonic-gate
212*7c478bd9Sstevel@tonic-gate *ptyfd = open(buf, O_RDWR | O_NOCTTY);
213*7c478bd9Sstevel@tonic-gate if (*ptyfd < 0) {
214*7c478bd9Sstevel@tonic-gate /* Try SCO style naming */
215*7c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
216*7c478bd9Sstevel@tonic-gate snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
217*7c478bd9Sstevel@tonic-gate *ptyfd = open(buf, O_RDWR | O_NOCTTY);
218*7c478bd9Sstevel@tonic-gate if (*ptyfd < 0)
219*7c478bd9Sstevel@tonic-gate continue;
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate /* Open the slave side. */
223*7c478bd9Sstevel@tonic-gate *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
224*7c478bd9Sstevel@tonic-gate if (*ttyfd < 0) {
225*7c478bd9Sstevel@tonic-gate error("%.100s: %.100s", namebuf, strerror(errno));
226*7c478bd9Sstevel@tonic-gate close(*ptyfd);
227*7c478bd9Sstevel@tonic-gate return 0;
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate /* set tty modes to a sane state for broken clients */
230*7c478bd9Sstevel@tonic-gate if (tcgetattr(*ptyfd, &tio) < 0)
231*7c478bd9Sstevel@tonic-gate log("Getting tty modes for pty failed: %.100s", strerror(errno));
232*7c478bd9Sstevel@tonic-gate else {
233*7c478bd9Sstevel@tonic-gate tio.c_lflag |= (ECHO | ISIG | ICANON);
234*7c478bd9Sstevel@tonic-gate tio.c_oflag |= (OPOST | ONLCR);
235*7c478bd9Sstevel@tonic-gate tio.c_iflag |= ICRNL;
236*7c478bd9Sstevel@tonic-gate
237*7c478bd9Sstevel@tonic-gate /* Set the new modes for the terminal. */
238*7c478bd9Sstevel@tonic-gate if (tcsetattr(*ptyfd, TCSANOW, &tio) < 0)
239*7c478bd9Sstevel@tonic-gate log("Setting tty modes for pty failed: %.100s", strerror(errno));
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate return 1;
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate return 0;
245*7c478bd9Sstevel@tonic-gate #endif /* CRAY */
246*7c478bd9Sstevel@tonic-gate #endif /* HAVE_DEV_PTS_AND_PTC */
247*7c478bd9Sstevel@tonic-gate #endif /* HAVE_DEV_PTMX */
248*7c478bd9Sstevel@tonic-gate #endif /* HAVE__GETPTY */
249*7c478bd9Sstevel@tonic-gate #endif /* HAVE_OPENPTY */
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate
252*7c478bd9Sstevel@tonic-gate /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
253*7c478bd9Sstevel@tonic-gate
254*7c478bd9Sstevel@tonic-gate void
pty_release(const char * ttyname)255*7c478bd9Sstevel@tonic-gate pty_release(const char *ttyname)
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
258*7c478bd9Sstevel@tonic-gate error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
259*7c478bd9Sstevel@tonic-gate if (chmod(ttyname, (mode_t) 0666) < 0)
260*7c478bd9Sstevel@tonic-gate error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate
263*7c478bd9Sstevel@tonic-gate /* Makes the tty the processes controlling tty and sets it to sane modes. */
264*7c478bd9Sstevel@tonic-gate
265*7c478bd9Sstevel@tonic-gate void
pty_make_controlling_tty(int * ttyfd,const char * ttyname)266*7c478bd9Sstevel@tonic-gate pty_make_controlling_tty(int *ttyfd, const char *ttyname)
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate int fd;
269*7c478bd9Sstevel@tonic-gate #ifdef USE_VHANGUP
270*7c478bd9Sstevel@tonic-gate void *old;
271*7c478bd9Sstevel@tonic-gate #endif /* USE_VHANGUP */
272*7c478bd9Sstevel@tonic-gate
273*7c478bd9Sstevel@tonic-gate #ifdef _UNICOS
274*7c478bd9Sstevel@tonic-gate if (setsid() < 0)
275*7c478bd9Sstevel@tonic-gate error("setsid: %.100s", strerror(errno));
276*7c478bd9Sstevel@tonic-gate
277*7c478bd9Sstevel@tonic-gate fd = open(ttyname, O_RDWR|O_NOCTTY);
278*7c478bd9Sstevel@tonic-gate if (fd != -1) {
279*7c478bd9Sstevel@tonic-gate mysignal(SIGHUP, SIG_IGN);
280*7c478bd9Sstevel@tonic-gate ioctl(fd, TCVHUP, (char *)NULL);
281*7c478bd9Sstevel@tonic-gate mysignal(SIGHUP, SIG_DFL);
282*7c478bd9Sstevel@tonic-gate setpgid(0, 0);
283*7c478bd9Sstevel@tonic-gate close(fd);
284*7c478bd9Sstevel@tonic-gate } else {
285*7c478bd9Sstevel@tonic-gate error("Failed to disconnect from controlling tty.");
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate
288*7c478bd9Sstevel@tonic-gate debug("Setting controlling tty using TCSETCTTY.");
289*7c478bd9Sstevel@tonic-gate ioctl(*ttyfd, TCSETCTTY, NULL);
290*7c478bd9Sstevel@tonic-gate fd = open("/dev/tty", O_RDWR);
291*7c478bd9Sstevel@tonic-gate if (fd < 0)
292*7c478bd9Sstevel@tonic-gate error("%.100s: %.100s", ttyname, strerror(errno));
293*7c478bd9Sstevel@tonic-gate close(*ttyfd);
294*7c478bd9Sstevel@tonic-gate *ttyfd = fd;
295*7c478bd9Sstevel@tonic-gate #else /* _UNICOS */
296*7c478bd9Sstevel@tonic-gate
297*7c478bd9Sstevel@tonic-gate /* First disconnect from the old controlling tty. */
298*7c478bd9Sstevel@tonic-gate #ifdef TIOCNOTTY
299*7c478bd9Sstevel@tonic-gate fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
300*7c478bd9Sstevel@tonic-gate if (fd >= 0) {
301*7c478bd9Sstevel@tonic-gate (void) ioctl(fd, TIOCNOTTY, NULL);
302*7c478bd9Sstevel@tonic-gate close(fd);
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate #endif /* TIOCNOTTY */
305*7c478bd9Sstevel@tonic-gate if (setsid() < 0)
306*7c478bd9Sstevel@tonic-gate error("setsid: %.100s", strerror(errno));
307*7c478bd9Sstevel@tonic-gate
308*7c478bd9Sstevel@tonic-gate /*
309*7c478bd9Sstevel@tonic-gate * Verify that we are successfully disconnected from the controlling
310*7c478bd9Sstevel@tonic-gate * tty.
311*7c478bd9Sstevel@tonic-gate */
312*7c478bd9Sstevel@tonic-gate fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
313*7c478bd9Sstevel@tonic-gate if (fd >= 0) {
314*7c478bd9Sstevel@tonic-gate error("Failed to disconnect from controlling tty.");
315*7c478bd9Sstevel@tonic-gate close(fd);
316*7c478bd9Sstevel@tonic-gate }
317*7c478bd9Sstevel@tonic-gate /* Make it our controlling tty. */
318*7c478bd9Sstevel@tonic-gate #ifdef TIOCSCTTY
319*7c478bd9Sstevel@tonic-gate debug("Setting controlling tty using TIOCSCTTY.");
320*7c478bd9Sstevel@tonic-gate if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
321*7c478bd9Sstevel@tonic-gate error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
322*7c478bd9Sstevel@tonic-gate #endif /* TIOCSCTTY */
323*7c478bd9Sstevel@tonic-gate #ifdef HAVE_NEWS4
324*7c478bd9Sstevel@tonic-gate if (setpgrp(0,0) < 0)
325*7c478bd9Sstevel@tonic-gate error("SETPGRP %s",strerror(errno));
326*7c478bd9Sstevel@tonic-gate #endif /* HAVE_NEWS4 */
327*7c478bd9Sstevel@tonic-gate #ifdef USE_VHANGUP
328*7c478bd9Sstevel@tonic-gate old = mysignal(SIGHUP, SIG_IGN);
329*7c478bd9Sstevel@tonic-gate vhangup();
330*7c478bd9Sstevel@tonic-gate mysignal(SIGHUP, old);
331*7c478bd9Sstevel@tonic-gate #endif /* USE_VHANGUP */
332*7c478bd9Sstevel@tonic-gate fd = open(ttyname, O_RDWR);
333*7c478bd9Sstevel@tonic-gate if (fd < 0) {
334*7c478bd9Sstevel@tonic-gate error("%.100s: %.100s", ttyname, strerror(errno));
335*7c478bd9Sstevel@tonic-gate } else {
336*7c478bd9Sstevel@tonic-gate #ifdef USE_VHANGUP
337*7c478bd9Sstevel@tonic-gate close(*ttyfd);
338*7c478bd9Sstevel@tonic-gate *ttyfd = fd;
339*7c478bd9Sstevel@tonic-gate #else /* USE_VHANGUP */
340*7c478bd9Sstevel@tonic-gate close(fd);
341*7c478bd9Sstevel@tonic-gate #endif /* USE_VHANGUP */
342*7c478bd9Sstevel@tonic-gate }
343*7c478bd9Sstevel@tonic-gate /* Verify that we now have a controlling tty. */
344*7c478bd9Sstevel@tonic-gate fd = open(_PATH_TTY, O_WRONLY);
345*7c478bd9Sstevel@tonic-gate if (fd < 0)
346*7c478bd9Sstevel@tonic-gate error("open /dev/tty failed - could not set controlling tty: %.100s",
347*7c478bd9Sstevel@tonic-gate strerror(errno));
348*7c478bd9Sstevel@tonic-gate else
349*7c478bd9Sstevel@tonic-gate close(fd);
350*7c478bd9Sstevel@tonic-gate #endif /* _UNICOS */
351*7c478bd9Sstevel@tonic-gate }
352*7c478bd9Sstevel@tonic-gate
353*7c478bd9Sstevel@tonic-gate /* Changes the window size associated with the pty. */
354*7c478bd9Sstevel@tonic-gate
355*7c478bd9Sstevel@tonic-gate void
pty_change_window_size(int ptyfd,int row,int col,int xpixel,int ypixel)356*7c478bd9Sstevel@tonic-gate pty_change_window_size(int ptyfd, int row, int col,
357*7c478bd9Sstevel@tonic-gate int xpixel, int ypixel)
358*7c478bd9Sstevel@tonic-gate {
359*7c478bd9Sstevel@tonic-gate struct winsize w;
360*7c478bd9Sstevel@tonic-gate
361*7c478bd9Sstevel@tonic-gate w.ws_row = row;
362*7c478bd9Sstevel@tonic-gate w.ws_col = col;
363*7c478bd9Sstevel@tonic-gate w.ws_xpixel = xpixel;
364*7c478bd9Sstevel@tonic-gate w.ws_ypixel = ypixel;
365*7c478bd9Sstevel@tonic-gate (void) ioctl(ptyfd, TIOCSWINSZ, &w);
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate
368*7c478bd9Sstevel@tonic-gate void
pty_setowner(struct passwd * pw,const char * ttyname)369*7c478bd9Sstevel@tonic-gate pty_setowner(struct passwd *pw, const char *ttyname)
370*7c478bd9Sstevel@tonic-gate {
371*7c478bd9Sstevel@tonic-gate struct group *grp;
372*7c478bd9Sstevel@tonic-gate gid_t gid;
373*7c478bd9Sstevel@tonic-gate mode_t mode;
374*7c478bd9Sstevel@tonic-gate struct stat st;
375*7c478bd9Sstevel@tonic-gate
376*7c478bd9Sstevel@tonic-gate /* Determine the group to make the owner of the tty. */
377*7c478bd9Sstevel@tonic-gate grp = getgrnam("tty");
378*7c478bd9Sstevel@tonic-gate if (grp) {
379*7c478bd9Sstevel@tonic-gate gid = grp->gr_gid;
380*7c478bd9Sstevel@tonic-gate mode = S_IRUSR | S_IWUSR | S_IWGRP;
381*7c478bd9Sstevel@tonic-gate } else {
382*7c478bd9Sstevel@tonic-gate gid = pw->pw_gid;
383*7c478bd9Sstevel@tonic-gate mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate
386*7c478bd9Sstevel@tonic-gate /*
387*7c478bd9Sstevel@tonic-gate * Change owner and mode of the tty as required.
388*7c478bd9Sstevel@tonic-gate * Warn but continue if filesystem is read-only and the uids match/
389*7c478bd9Sstevel@tonic-gate * tty is owned by root.
390*7c478bd9Sstevel@tonic-gate */
391*7c478bd9Sstevel@tonic-gate if (stat(ttyname, &st))
392*7c478bd9Sstevel@tonic-gate fatal("stat(%.100s) failed: %.100s", ttyname,
393*7c478bd9Sstevel@tonic-gate strerror(errno));
394*7c478bd9Sstevel@tonic-gate
395*7c478bd9Sstevel@tonic-gate if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
396*7c478bd9Sstevel@tonic-gate if (chown(ttyname, pw->pw_uid, gid) < 0) {
397*7c478bd9Sstevel@tonic-gate if (errno == EROFS &&
398*7c478bd9Sstevel@tonic-gate (st.st_uid == pw->pw_uid || st.st_uid == 0))
399*7c478bd9Sstevel@tonic-gate error("chown(%.100s, %u, %u) failed: %.100s",
400*7c478bd9Sstevel@tonic-gate ttyname, (u_int)pw->pw_uid, (u_int)gid,
401*7c478bd9Sstevel@tonic-gate strerror(errno));
402*7c478bd9Sstevel@tonic-gate else
403*7c478bd9Sstevel@tonic-gate fatal("chown(%.100s, %u, %u) failed: %.100s",
404*7c478bd9Sstevel@tonic-gate ttyname, (u_int)pw->pw_uid, (u_int)gid,
405*7c478bd9Sstevel@tonic-gate strerror(errno));
406*7c478bd9Sstevel@tonic-gate }
407*7c478bd9Sstevel@tonic-gate }
408*7c478bd9Sstevel@tonic-gate
409*7c478bd9Sstevel@tonic-gate if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
410*7c478bd9Sstevel@tonic-gate if (chmod(ttyname, mode) < 0) {
411*7c478bd9Sstevel@tonic-gate if (errno == EROFS &&
412*7c478bd9Sstevel@tonic-gate (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
413*7c478bd9Sstevel@tonic-gate error("chmod(%.100s, 0%o) failed: %.100s",
414*7c478bd9Sstevel@tonic-gate ttyname, (int)mode, strerror(errno));
415*7c478bd9Sstevel@tonic-gate else
416*7c478bd9Sstevel@tonic-gate fatal("chmod(%.100s, 0%o) failed: %.100s",
417*7c478bd9Sstevel@tonic-gate ttyname, (int)mode, strerror(errno));
418*7c478bd9Sstevel@tonic-gate }
419*7c478bd9Sstevel@tonic-gate }
420*7c478bd9Sstevel@tonic-gate }
421