Skip to content
Newer
Older
100644 419 lines (381 sloc) 10.5 KB
8efc0c1 Initial revision
damien authored
1 /*
5260325 - Merged very large OpenBSD source code reformat
damien authored
2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Allocating a pseudo-terminal, and making it the controlling tty.
2724e08 - OpenBSD CVS updates.
damien authored
6 *
95ecb01 - (djm) Merge OpenBSD changes:
djm authored
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".
5260325 - Merged very large OpenBSD source code reformat
damien authored
12 */
8efc0c1 Initial revision
damien authored
13
14 #include "includes.h"
c199eb0 - deraadt@cvs.openbsd.org 2002/06/23 03:30:58
mouring authored
15 RCSID("$OpenBSD: sshpty.c,v 1.5 2002/06/23 03:30:58 deraadt Exp $");
8efc0c1 Initial revision
damien authored
16
884bcb3 - Clean up broken includes in pty.c
damien authored
17 #ifdef HAVE_UTIL_H
18 # include <util.h>
19 #endif /* HAVE_UTIL_H */
20
1fb1bd0 - (bal) Markus' blessing to rename login.[ch] -> sshlogin.[ch] and
mouring authored
21 #include "sshpty.h"
9205a86 Hopefully things did not get mixed around too much. It compiles under
mouring authored
22 #include "log.h"
b6d54ab - (stevesk) don't set SA_RESTART and set SIGCHLD to SIG_DFL
stevesk authored
23 #include "misc.h"
5260325 - Merged very large OpenBSD source code reformat
damien authored
24
8efc0c1 Initial revision
damien authored
25 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
26 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
27 #undef HAVE_DEV_PTMX
28 #endif
29
6d8c4ea - Compile fix for Solaris with /dev/ptmx from
damien authored
30 #ifdef HAVE_PTY_H
31 # include <pty.h>
32 #endif
33 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
34 # include <sys/stropts.h>
35 #endif
36
8efc0c1 Initial revision
damien authored
37 #ifndef O_NOCTTY
38 #define O_NOCTTY 0
39 #endif
40
aa3378d - More reformatting merged from OpenBSD CVS
damien authored
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 */
8efc0c1 Initial revision
damien authored
47
2724e08 - OpenBSD CVS updates.
damien authored
48 int
a408af7 - Merged more OpenBSD changes:
damien authored
49 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
8efc0c1 Initial revision
damien authored
50 {
a408af7 - Merged more OpenBSD changes:
damien authored
51 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
5260325 - Merged very large OpenBSD source code reformat
damien authored
52 /* openpty(3) exists in OSF/1 and some other os'es */
89d336e - (djm) Use ttyname() to determine name of tty returned by openpty()
djm authored
53 char *name;
5260325 - Merged very large OpenBSD source code reformat
damien authored
54 int i;
8efc0c1 Initial revision
damien authored
55
89d336e - (djm) Use ttyname() to determine name of tty returned by openpty()
djm authored
56 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
5260325 - Merged very large OpenBSD source code reformat
damien authored
57 if (i < 0) {
58 error("openpty: %.100s", strerror(errno));
59 return 0;
60 }
89d336e - (djm) Use ttyname() to determine name of tty returned by openpty()
djm authored
61 name = ttyname(*ttyfd);
62 if (!name)
63 fatal("openpty returns device for which ttyname fails.");
64
65 strlcpy(namebuf, name, namebuflen); /* possible truncation */
5260325 - Merged very large OpenBSD source code reformat
damien authored
66 return 1;
8efc0c1 Initial revision
damien authored
67 #else /* HAVE_OPENPTY */
68 #ifdef HAVE__GETPTY
aa3378d - More reformatting merged from OpenBSD CVS
damien authored
69 /*
70 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
71 * pty's automagically when needed
72 */
5260325 - Merged very large OpenBSD source code reformat
damien authored
73 char *slave;
74
75 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
76 if (slave == NULL) {
77 error("_getpty: %.100s", strerror(errno));
78 return 0;
79 }
a408af7 - Merged more OpenBSD changes:
damien authored
80 strlcpy(namebuf, slave, namebuflen);
5260325 - Merged very large OpenBSD source code reformat
damien authored
81 /* Open the slave side. */
82 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
83 if (*ttyfd < 0) {
84 error("%.200s: %.100s", namebuf, strerror(errno));
85 close(*ptyfd);
86 return 0;
87 }
88 return 1;
8efc0c1 Initial revision
damien authored
89 #else /* HAVE__GETPTY */
a7effaa - Integration of large HPUX patch from Andre Lucas
damien authored
90 #if defined(HAVE_DEV_PTMX)
aa3378d - More reformatting merged from OpenBSD CVS
damien authored
91 /*
92 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
93 * also has bsd-style ptys, but they simply do not work.)
94 */
5260325 - Merged very large OpenBSD source code reformat
damien authored
95 int ptm;
96 char *pts;
b6d54ab - (stevesk) don't set SA_RESTART and set SIGCHLD to SIG_DFL
stevesk authored
97 mysig_t old_signal;
5260325 - Merged very large OpenBSD source code reformat
damien authored
98
99 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
100 if (ptm < 0) {
101 error("/dev/ptmx: %.100s", strerror(errno));
102 return 0;
103 }
b6d54ab - (stevesk) don't set SA_RESTART and set SIGCHLD to SIG_DFL
stevesk authored
104 old_signal = mysignal(SIGCHLD, SIG_DFL);
5260325 - Merged very large OpenBSD source code reformat
damien authored
105 if (grantpt(ptm) < 0) {
106 error("grantpt: %.100s", strerror(errno));
107 return 0;
108 }
b6d54ab - (stevesk) don't set SA_RESTART and set SIGCHLD to SIG_DFL
stevesk authored
109 mysignal(SIGCHLD, old_signal);
5260325 - Merged very large OpenBSD source code reformat
damien authored
110 if (unlockpt(ptm) < 0) {
111 error("unlockpt: %.100s", strerror(errno));
112 return 0;
113 }
114 pts = ptsname(ptm);
115 if (pts == NULL)
116 error("Slave pty side name could not be obtained.");
a408af7 - Merged more OpenBSD changes:
damien authored
117 strlcpy(namebuf, pts, namebuflen);
5260325 - Merged very large OpenBSD source code reformat
damien authored
118 *ptyfd = ptm;
119
120 /* Open the slave side. */
121 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
122 if (*ttyfd < 0) {
123 error("%.100s: %.100s", namebuf, strerror(errno));
124 close(*ptyfd);
125 return 0;
126 }
1907a2d - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
djm authored
127 #ifndef HAVE_CYGWIN
997d311 - (stevesk) pty.c: use __hpux to identify HP-UX.
stevesk authored
128 /*
129 * Push the appropriate streams modules, as described in Solaris pts(7).
130 * HP-UX pts(7) doesn't have ttcompat module.
131 */
5260325 - Merged very large OpenBSD source code reformat
damien authored
132 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
133 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
134 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
135 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
997d311 - (stevesk) pty.c: use __hpux to identify HP-UX.
stevesk authored
136 #ifndef __hpux
5260325 - Merged very large OpenBSD source code reformat
damien authored
137 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
138 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
3b822d6 - Runtime error fix for HPUX from Otmar Stahl
damien authored
139 #endif
1907a2d - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
djm authored
140 #endif
5260325 - Merged very large OpenBSD source code reformat
damien authored
141 return 1;
8efc0c1 Initial revision
damien authored
142 #else /* HAVE_DEV_PTMX */
143 #ifdef HAVE_DEV_PTS_AND_PTC
5260325 - Merged very large OpenBSD source code reformat
damien authored
144 /* AIX-style pty code. */
145 const char *name;
8efc0c1 Initial revision
damien authored
146
5260325 - Merged very large OpenBSD source code reformat
damien authored
147 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
148 if (*ptyfd < 0) {
149 error("Could not open /dev/ptc: %.100s", strerror(errno));
150 return 0;
151 }
152 name = ttyname(*ptyfd);
153 if (!name)
154 fatal("Open of /dev/ptc returns device for which ttyname fails.");
a408af7 - Merged more OpenBSD changes:
damien authored
155 strlcpy(namebuf, name, namebuflen);
5260325 - Merged very large OpenBSD source code reformat
damien authored
156 *ttyfd = open(name, O_RDWR | O_NOCTTY);
157 if (*ttyfd < 0) {
158 error("Could not open pty slave side %.100s: %.100s",
a3e6a8e - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
djm authored
159 name, strerror(errno));
5260325 - Merged very large OpenBSD source code reformat
damien authored
160 close(*ptyfd);
161 return 0;
162 }
163 return 1;
8efc0c1 Initial revision
damien authored
164 #else /* HAVE_DEV_PTS_AND_PTC */
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
165 #ifdef _CRAY
166 char buf[64];
4a79e16 unexpand
mouring authored
167 int i;
168 int highpty;
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
169
170 #ifdef _SC_CRAY_NPTY
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
171 highpty = sysconf(_SC_CRAY_NPTY);
172 if (highpty == -1)
173 highpty = 128;
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
174 #else
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
175 highpty = 128;
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
176 #endif
177
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
178 for (i = 0; i < highpty; i++) {
179 snprintf(buf, sizeof(buf), "/dev/pty/%03d", i);
180 *ptyfd = open(buf, O_RDWR|O_NOCTTY);
181 if (*ptyfd < 0)
182 continue;
183 snprintf(namebuf, namebuflen, "/dev/ttyp%03d", i);
184 /* Open the slave side. */
185 *ttyfd = open(namebuf, O_RDWR|O_NOCTTY);
186 if (*ttyfd < 0) {
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
187 error("%.100s: %.100s", namebuf, strerror(errno));
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
188 close(*ptyfd);
bbfe653 - (stevesk) sshpty.c: return 0 on error in cray pty code;
stevesk authored
189 return 0;
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
190 }
191 return 1;
192 }
193 return 0;
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
194 #else
5260325 - Merged very large OpenBSD source code reformat
damien authored
195 /* BSD-style pty code. */
196 char buf[64];
197 int i;
aa3378d - More reformatting merged from OpenBSD CVS
damien authored
198 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
5260325 - Merged very large OpenBSD source code reformat
damien authored
199 const char *ptyminors = "0123456789abcdef";
200 int num_minors = strlen(ptyminors);
201 int num_ptys = strlen(ptymajors) * num_minors;
2189e0e 20020514
tim authored
202 struct termios tio;
5260325 - Merged very large OpenBSD source code reformat
damien authored
203
204 for (i = 0; i < num_ptys; i++) {
205 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
206 ptyminors[i % num_minors]);
95f1ecc - OpenBSD CVS Changes
damien authored
207 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
a408af7 - Merged more OpenBSD changes:
damien authored
208 ptymajors[i / num_minors], ptyminors[i % num_minors]);
5260325 - Merged very large OpenBSD source code reformat
damien authored
209
198c491 - (djm) Check for SCO pty naming style (ptyp%d/ttyp%d). Based on fix…
djm authored
210 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
211 if (*ptyfd < 0) {
212 /* Try SCO style naming */
213 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
214 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
215 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
216 if (*ptyfd < 0)
217 continue;
49d8222 - stevesk@cvs.openbsd.org 2001/02/04 08:32:27
stevesk authored
218 }
219
5260325 - Merged very large OpenBSD source code reformat
damien authored
220 /* Open the slave side. */
221 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
222 if (*ttyfd < 0) {
223 error("%.100s: %.100s", namebuf, strerror(errno));
224 close(*ptyfd);
225 return 0;
226 }
2189e0e 20020514
tim authored
227 /* set tty modes to a sane state for broken clients */
228 if (tcgetattr(*ptyfd, &tio) < 0)
229 log("Getting tty modes for pty failed: %.100s", strerror(errno));
230 else {
231 tio.c_lflag |= (ECHO | ISIG | ICANON);
232 tio.c_oflag |= (OPOST | ONLCR);
233 tio.c_iflag |= ICRNL;
234
235 /* Set the new modes for the terminal. */
236 if (tcsetattr(*ptyfd, TCSANOW, &tio) < 0)
237 log("Setting tty modes for pty failed: %.100s", strerror(errno));
238 }
239
5260325 - Merged very large OpenBSD source code reformat
damien authored
240 return 1;
8efc0c1 Initial revision
damien authored
241 }
5260325 - Merged very large OpenBSD source code reformat
damien authored
242 return 0;
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
243 #endif /* CRAY */
8efc0c1 Initial revision
damien authored
244 #endif /* HAVE_DEV_PTS_AND_PTC */
245 #endif /* HAVE_DEV_PTMX */
246 #endif /* HAVE__GETPTY */
247 #endif /* HAVE_OPENPTY */
248 }
249
5260325 - Merged very large OpenBSD source code reformat
damien authored
250 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
8efc0c1 Initial revision
damien authored
251
2724e08 - OpenBSD CVS updates.
damien authored
252 void
5260325 - Merged very large OpenBSD source code reformat
damien authored
253 pty_release(const char *ttyname)
8efc0c1 Initial revision
damien authored
254 {
3276571 - Warning was valid - possible race condition on PTYs. Avoided using
damien authored
255 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
817175b - Applied pty cleanup patch from markus.friedl@informatik.uni-erlang…
damien authored
256 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
3276571 - Warning was valid - possible race condition on PTYs. Avoided using
damien authored
257 if (chmod(ttyname, (mode_t) 0666) < 0)
817175b - Applied pty cleanup patch from markus.friedl@informatik.uni-erlang…
damien authored
258 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
8efc0c1 Initial revision
damien authored
259 }
260
261 /* Makes the tty the processes controlling tty and sets it to sane modes. */
262
2724e08 - OpenBSD CVS updates.
damien authored
263 void
5260325 - Merged very large OpenBSD source code reformat
damien authored
264 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
8efc0c1 Initial revision
damien authored
265 {
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
266 int fd;
18ef98f - (djm) Cleanup sshpty.c a little
djm authored
267 #ifdef USE_VHANGUP
268 void *old;
269 #endif /* USE_VHANGUP */
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
270
18ef98f - (djm) Cleanup sshpty.c a little
djm authored
271 #ifdef _CRAY
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
272 if (setsid() < 0)
273 error("setsid: %.100s", strerror(errno));
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
274
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
275 fd = open(ttyname, O_RDWR|O_NOCTTY);
276 if (fd != -1) {
18ef98f - (djm) Cleanup sshpty.c a little
djm authored
277 mysignal(SIGHUP, SIG_IGN);
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
278 ioctl(fd, TCVHUP, (char *)NULL);
18ef98f - (djm) Cleanup sshpty.c a little
djm authored
279 mysignal(SIGHUP, SIG_DFL);
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
280 setpgid(0, 0);
281 close(fd);
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
282 } else {
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
283 error("Failed to disconnect from controlling tty.");
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
284 }
285
757488c - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
stevesk authored
286 debug("Setting controlling tty using TCSETCTTY.");
287 ioctl(*ttyfd, TCSETCTTY, NULL);
288 fd = open("/dev/tty", O_RDWR);
289 if (fd < 0)
290 error("%.100s: %.100s", ttyname, strerror(errno));
0c74f71 - (bal) Second around of UNICOS patches. A few other things left.
mouring authored
291 close(*ttyfd);
4a79e16 unexpand
mouring authored
292 *ttyfd = fd;
18ef98f - (djm) Cleanup sshpty.c a little
djm authored
293 #else /* _CRAY */
8efc0c1 Initial revision
damien authored
294
5260325 - Merged very large OpenBSD source code reformat
damien authored
295 /* First disconnect from the old controlling tty. */
8efc0c1 Initial revision
damien authored
296 #ifdef TIOCNOTTY
9be250f - itojun@cvs.openbsd.org 2001/02/08 19:30:52
mouring authored
297 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
5260325 - Merged very large OpenBSD source code reformat
damien authored
298 if (fd >= 0) {
299 (void) ioctl(fd, TIOCNOTTY, NULL);
300 close(fd);
301 }
8efc0c1 Initial revision
damien authored
302 #endif /* TIOCNOTTY */
5260325 - Merged very large OpenBSD source code reformat
damien authored
303 if (setsid() < 0)
304 error("setsid: %.100s", strerror(errno));
305
aa3378d - More reformatting merged from OpenBSD CVS
damien authored
306 /*
307 * Verify that we are successfully disconnected from the controlling
308 * tty.
309 */
9be250f - itojun@cvs.openbsd.org 2001/02/08 19:30:52
mouring authored
310 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
5260325 - Merged very large OpenBSD source code reformat
damien authored
311 if (fd >= 0) {
312 error("Failed to disconnect from controlling tty.");
313 close(fd);
314 }
315 /* Make it our controlling tty. */
8efc0c1 Initial revision
damien authored
316 #ifdef TIOCSCTTY
5260325 - Merged very large OpenBSD source code reformat
damien authored
317 debug("Setting controlling tty using TIOCSCTTY.");
0cfade0 - (stevesk) pty.c: HP-UX 10 and 11 don't define TIOCSCTTY. Add error…
stevesk authored
318 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
319 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
8efc0c1 Initial revision
damien authored
320 #endif /* TIOCSCTTY */
564e512 - (bal) Changed from GNU rx to PCRE on suggestion from djm.
mouring authored
321 #ifdef HAVE_NEWS4
322 if (setpgrp(0,0) < 0)
323 error("SETPGRP %s",strerror(errno));
324 #endif /* HAVE_NEWS4 */
1907a2d - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
djm authored
325 #ifdef USE_VHANGUP
18ef98f - (djm) Cleanup sshpty.c a little
djm authored
326 old = mysignal(SIGHUP, SIG_IGN);
14ca366 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
damien authored
327 vhangup();
18ef98f - (djm) Cleanup sshpty.c a little
djm authored
328 mysignal(SIGHUP, old);
1907a2d - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
djm authored
329 #endif /* USE_VHANGUP */
5260325 - Merged very large OpenBSD source code reformat
damien authored
330 fd = open(ttyname, O_RDWR);
14ca366 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
damien authored
331 if (fd < 0) {
5260325 - Merged very large OpenBSD source code reformat
damien authored
332 error("%.100s: %.100s", ttyname, strerror(errno));
14ca366 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
damien authored
333 } else {
1907a2d - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
djm authored
334 #ifdef USE_VHANGUP
14ca366 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
damien authored
335 close(*ttyfd);
336 *ttyfd = fd;
1907a2d - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
djm authored
337 #else /* USE_VHANGUP */
5260325 - Merged very large OpenBSD source code reformat
damien authored
338 close(fd);
1907a2d - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
djm authored
339 #endif /* USE_VHANGUP */
14ca366 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
damien authored
340 }
5260325 - Merged very large OpenBSD source code reformat
damien authored
341 /* Verify that we now have a controlling tty. */
9be250f - itojun@cvs.openbsd.org 2001/02/08 19:30:52
mouring authored
342 fd = open(_PATH_TTY, O_WRONLY);
5260325 - Merged very large OpenBSD source code reformat
damien authored
343 if (fd < 0)
344 error("open /dev/tty failed - could not set controlling tty: %.100s",
a3e6a8e - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
djm authored
345 strerror(errno));
5260325 - Merged very large OpenBSD source code reformat
damien authored
346 else {
347 close(fd);
348 }
18ef98f - (djm) Cleanup sshpty.c a little
djm authored
349 #endif /* _CRAY */
8efc0c1 Initial revision
damien authored
350 }
351
352 /* Changes the window size associated with the pty. */
353
2724e08 - OpenBSD CVS updates.
damien authored
354 void
5260325 - Merged very large OpenBSD source code reformat
damien authored
355 pty_change_window_size(int ptyfd, int row, int col,
a3e6a8e - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
djm authored
356 int xpixel, int ypixel)
8efc0c1 Initial revision
damien authored
357 {
5260325 - Merged very large OpenBSD source code reformat
damien authored
358 struct winsize w;
359 w.ws_row = row;
360 w.ws_col = col;
361 w.ws_xpixel = xpixel;
362 w.ws_ypixel = ypixel;
363 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
8efc0c1 Initial revision
damien authored
364 }
817175b - Applied pty cleanup patch from markus.friedl@informatik.uni-erlang…
damien authored
365
366 void
367 pty_setowner(struct passwd *pw, const char *ttyname)
368 {
369 struct group *grp;
370 gid_t gid;
371 mode_t mode;
e5d1c11 - (stevesk) OpenBSD CVS updates:
stevesk authored
372 struct stat st;
817175b - Applied pty cleanup patch from markus.friedl@informatik.uni-erlang…
damien authored
373
374 /* Determine the group to make the owner of the tty. */
375 grp = getgrnam("tty");
376 if (grp) {
377 gid = grp->gr_gid;
378 mode = S_IRUSR | S_IWUSR | S_IWGRP;
379 } else {
380 gid = pw->pw_gid;
381 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
382 }
383
e5d1c11 - (stevesk) OpenBSD CVS updates:
stevesk authored
384 /*
385 * Change owner and mode of the tty as required.
6d1cafb - markus@cvs.openbsd.org 2001/07/22 21:32:27
mouring authored
386 * Warn but continue if filesystem is read-only and the uids match/
387 * tty is owned by root.
e5d1c11 - (stevesk) OpenBSD CVS updates:
stevesk authored
388 */
389 if (stat(ttyname, &st))
390 fatal("stat(%.100s) failed: %.100s", ttyname,
391 strerror(errno));
392
393 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
394 if (chown(ttyname, pw->pw_uid, gid) < 0) {
a3e6a8e - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
djm authored
395 if (errno == EROFS &&
3fe8ff6 - OpenBSD CVS Sync
mouring authored
396 (st.st_uid == pw->pw_uid || st.st_uid == 0))
c199eb0 - deraadt@cvs.openbsd.org 2002/06/23 03:30:58
mouring authored
397 error("chown(%.100s, %u, %u) failed: %.100s",
a3e6a8e - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
djm authored
398 ttyname, pw->pw_uid, gid,
399 strerror(errno));
e5d1c11 - (stevesk) OpenBSD CVS updates:
stevesk authored
400 else
c199eb0 - deraadt@cvs.openbsd.org 2002/06/23 03:30:58
mouring authored
401 fatal("chown(%.100s, %u, %u) failed: %.100s",
a3e6a8e - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
djm authored
402 ttyname, pw->pw_uid, gid,
403 strerror(errno));
e5d1c11 - (stevesk) OpenBSD CVS updates:
stevesk authored
404 }
405 }
406
407 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
408 if (chmod(ttyname, mode) < 0) {
409 if (errno == EROFS &&
410 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
411 error("chmod(%.100s, 0%o) failed: %.100s",
a3e6a8e - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
djm authored
412 ttyname, mode, strerror(errno));
e5d1c11 - (stevesk) OpenBSD CVS updates:
stevesk authored
413 else
414 fatal("chmod(%.100s, 0%o) failed: %.100s",
a3e6a8e - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
djm authored
415 ttyname, mode, strerror(errno));
e5d1c11 - (stevesk) OpenBSD CVS updates:
stevesk authored
416 }
417 }
817175b - Applied pty cleanup patch from markus.friedl@informatik.uni-erlang…
damien authored
418 }
Something went wrong with that request. Please try again.