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