blob: 71c48b5730248bac2a8dc54ff44f6395f220df42 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Allocating a pseudo-terminal, and making it the controlling tty.
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * 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".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Damien Miller9f0f5c62001-12-21 14:45:46 +110015RCSID("$OpenBSD: sshpty.c,v 1.4 2001/12/19 07:18:56 deraadt Exp $");
Damien Miller95def091999-11-25 00:26:21 +110016
Damien Miller36b339a1999-12-14 10:54:47 +110017#ifdef HAVE_UTIL_H
18# include <util.h>
19#endif /* HAVE_UTIL_H */
20
Ben Lindstromd95c09c2001-02-18 19:13:33 +000021#include "sshpty.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000022#include "log.h"
Kevin Steves974fb9c2001-06-15 00:04:23 +000023#include "misc.h"
Damien Miller356a0b01999-11-08 15:30:59 +110024
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025/* 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
Damien Millerfce16481999-12-08 08:53:52 +110030#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
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037#ifndef O_NOCTTY
38#define O_NOCTTY 0
39#endif
40
Damien Miller5428f641999-11-25 11:54:57 +110041/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
Damien Miller4af51302000-04-16 11:18:38 +100048int
Damien Miller037a0dc1999-12-07 15:38:31 +110049pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050{
Damien Miller037a0dc1999-12-07 15:38:31 +110051#if defined(HAVE_OPENPTY) || defined(BSD4_4)
Damien Miller95def091999-11-25 00:26:21 +110052 /* openpty(3) exists in OSF/1 and some other os'es */
Damien Miller99e92432001-02-18 12:49:35 +110053 char *name;
Damien Miller95def091999-11-25 00:26:21 +110054 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
Damien Miller99e92432001-02-18 12:49:35 +110056 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +110057 if (i < 0) {
58 error("openpty: %.100s", strerror(errno));
59 return 0;
60 }
Damien Miller99e92432001-02-18 12:49:35 +110061 name = ttyname(*ttyfd);
62 if (!name)
63 fatal("openpty returns device for which ttyname fails.");
64
65 strlcpy(namebuf, name, namebuflen); /* possible truncation */
Damien Miller95def091999-11-25 00:26:21 +110066 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067#else /* HAVE_OPENPTY */
68#ifdef HAVE__GETPTY
Damien Miller5428f641999-11-25 11:54:57 +110069 /*
70 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
71 * pty's automagically when needed
72 */
Damien Miller95def091999-11-25 00:26:21 +110073 char *slave;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller95def091999-11-25 00:26:21 +110075 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
76 if (slave == NULL) {
77 error("_getpty: %.100s", strerror(errno));
78 return 0;
79 }
Damien Miller037a0dc1999-12-07 15:38:31 +110080 strlcpy(namebuf, slave, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +110081 /* 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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089#else /* HAVE__GETPTY */
Damien Miller76112de1999-12-21 11:18:08 +110090#if defined(HAVE_DEV_PTMX)
Damien Miller5428f641999-11-25 11:54:57 +110091 /*
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 */
Damien Miller95def091999-11-25 00:26:21 +110095 int ptm;
96 char *pts;
Kevin Steves974fb9c2001-06-15 00:04:23 +000097 mysig_t old_signal;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller95def091999-11-25 00:26:21 +110099 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
100 if (ptm < 0) {
101 error("/dev/ptmx: %.100s", strerror(errno));
102 return 0;
103 }
Kevin Steves974fb9c2001-06-15 00:04:23 +0000104 old_signal = mysignal(SIGCHLD, SIG_DFL);
Damien Miller95def091999-11-25 00:26:21 +1100105 if (grantpt(ptm) < 0) {
106 error("grantpt: %.100s", strerror(errno));
107 return 0;
108 }
Kevin Steves974fb9c2001-06-15 00:04:23 +0000109 mysignal(SIGCHLD, old_signal);
Damien Miller95def091999-11-25 00:26:21 +1100110 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.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100117 strlcpy(namebuf, pts, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100118 *ptyfd = ptm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller95def091999-11-25 00:26:21 +1100120 /* 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 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100127#ifndef HAVE_CYGWIN
Kevin Steves85b3db62000-11-05 15:31:36 +0000128 /*
129 * Push the appropriate streams modules, as described in Solaris pts(7).
130 * HP-UX pts(7) doesn't have ttcompat module.
131 */
Damien Miller95def091999-11-25 00:26:21 +1100132 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));
Kevin Steves85b3db62000-11-05 15:31:36 +0000136#ifndef __hpux
Damien Miller95def091999-11-25 00:26:21 +1100137 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
138 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
Damien Millere37ac5a2000-03-17 23:58:59 +1100139#endif
Damien Millerbac2d8a2000-09-05 16:13:06 +1100140#endif
Damien Miller95def091999-11-25 00:26:21 +1100141 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142#else /* HAVE_DEV_PTMX */
143#ifdef HAVE_DEV_PTS_AND_PTC
Damien Miller95def091999-11-25 00:26:21 +1100144 /* AIX-style pty code. */
145 const char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller95def091999-11-25 00:26:21 +1100147 *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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151 }
Damien Miller95def091999-11-25 00:26:21 +1100152 name = ttyname(*ptyfd);
153 if (!name)
154 fatal("Open of /dev/ptc returns device for which ttyname fails.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100155 strlcpy(namebuf, name, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100156 *ttyfd = open(name, O_RDWR | O_NOCTTY);
157 if (*ttyfd < 0) {
158 error("Could not open pty slave side %.100s: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100159 name, strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100160 close(*ptyfd);
161 return 0;
162 }
163 return 1;
164#else /* HAVE_DEV_PTS_AND_PTC */
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000165#ifdef _CRAY
166 char buf[64];
167 int i;
168 int highpty;
169
170#ifdef _SC_CRAY_NPTY
Kevin Stevesf744b512001-08-14 20:31:49 +0000171 highpty = sysconf(_SC_CRAY_NPTY);
172 if (highpty == -1)
173 highpty = 128;
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000174#else
Kevin Stevesf744b512001-08-14 20:31:49 +0000175 highpty = 128;
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000176#endif
177
Kevin Stevesf744b512001-08-14 20:31:49 +0000178 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) {
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000187 error("%.100s: %.100s", namebuf, strerror(errno));
Kevin Stevesf744b512001-08-14 20:31:49 +0000188 close(*ptyfd);
Kevin Steves25ee4e42001-08-14 20:41:34 +0000189 return 0;
Kevin Stevesf744b512001-08-14 20:31:49 +0000190 }
191 return 1;
192 }
193 return 0;
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000194#else
Damien Miller95def091999-11-25 00:26:21 +1100195 /* BSD-style pty code. */
196 char buf[64];
197 int i;
Damien Miller5428f641999-11-25 11:54:57 +1100198 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
Damien Miller95def091999-11-25 00:26:21 +1100199 const char *ptyminors = "0123456789abcdef";
200 int num_minors = strlen(ptyminors);
201 int num_ptys = strlen(ptymajors) * num_minors;
202
203 for (i = 0; i < num_ptys; i++) {
204 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
205 ptyminors[i % num_minors]);
Damien Millera34a28b1999-12-14 10:47:15 +1100206 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
Damien Miller037a0dc1999-12-07 15:38:31 +1100207 ptymajors[i / num_minors], ptyminors[i % num_minors]);
Damien Miller95def091999-11-25 00:26:21 +1100208
Damien Miller6d8c11f2000-08-29 11:52:38 +1100209 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
210 if (*ptyfd < 0) {
211 /* Try SCO style naming */
212 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
213 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
214 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
215 if (*ptyfd < 0)
216 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000217 }
218
Damien Miller95def091999-11-25 00:26:21 +1100219 /* Open the slave side. */
220 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
221 if (*ttyfd < 0) {
222 error("%.100s: %.100s", namebuf, strerror(errno));
223 close(*ptyfd);
224 return 0;
225 }
226 return 1;
227 }
228 return 0;
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000229#endif /* CRAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230#endif /* HAVE_DEV_PTS_AND_PTC */
231#endif /* HAVE_DEV_PTMX */
232#endif /* HAVE__GETPTY */
233#endif /* HAVE_OPENPTY */
234}
235
Damien Miller95def091999-11-25 00:26:21 +1100236/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237
Damien Miller4af51302000-04-16 11:18:38 +1000238void
Damien Miller95def091999-11-25 00:26:21 +1100239pty_release(const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240{
Damien Miller204ad072000-03-02 23:56:12 +1100241 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100242 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
Damien Miller204ad072000-03-02 23:56:12 +1100243 if (chmod(ttyname, (mode_t) 0666) < 0)
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100244 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245}
246
247/* Makes the tty the processes controlling tty and sets it to sane modes. */
248
Damien Miller4af51302000-04-16 11:18:38 +1000249void
Damien Miller95def091999-11-25 00:26:21 +1100250pty_make_controlling_tty(int *ttyfd, const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251{
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000252 int fd;
Damien Miller4a820ea2001-10-12 19:15:48 +1000253#ifdef USE_VHANGUP
254 void *old;
255#endif /* USE_VHANGUP */
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000256
Damien Miller4a820ea2001-10-12 19:15:48 +1000257#ifdef _CRAY
Kevin Stevesf744b512001-08-14 20:31:49 +0000258 if (setsid() < 0)
259 error("setsid: %.100s", strerror(errno));
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000260
Kevin Stevesf744b512001-08-14 20:31:49 +0000261 fd = open(ttyname, O_RDWR|O_NOCTTY);
262 if (fd != -1) {
Damien Miller4a820ea2001-10-12 19:15:48 +1000263 mysignal(SIGHUP, SIG_IGN);
Kevin Stevesf744b512001-08-14 20:31:49 +0000264 ioctl(fd, TCVHUP, (char *)NULL);
Damien Miller4a820ea2001-10-12 19:15:48 +1000265 mysignal(SIGHUP, SIG_DFL);
Kevin Stevesf744b512001-08-14 20:31:49 +0000266 setpgid(0, 0);
267 close(fd);
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000268 } else {
Kevin Stevesf744b512001-08-14 20:31:49 +0000269 error("Failed to disconnect from controlling tty.");
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000270 }
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000271
Kevin Stevesf744b512001-08-14 20:31:49 +0000272 debug("Setting controlling tty using TCSETCTTY.");
273 ioctl(*ttyfd, TCSETCTTY, NULL);
274 fd = open("/dev/tty", O_RDWR);
275 if (fd < 0)
276 error("%.100s: %.100s", ttyname, strerror(errno));
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000277 close(*ttyfd);
278 *ttyfd = fd;
Damien Miller4a820ea2001-10-12 19:15:48 +1000279#else /* _CRAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280
Damien Miller95def091999-11-25 00:26:21 +1100281 /* First disconnect from the old controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282#ifdef TIOCNOTTY
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000283 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +1100284 if (fd >= 0) {
285 (void) ioctl(fd, TIOCNOTTY, NULL);
286 close(fd);
287 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288#endif /* TIOCNOTTY */
Damien Miller95def091999-11-25 00:26:21 +1100289 if (setsid() < 0)
290 error("setsid: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291
Damien Miller5428f641999-11-25 11:54:57 +1100292 /*
293 * Verify that we are successfully disconnected from the controlling
294 * tty.
295 */
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000296 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
Damien Miller95def091999-11-25 00:26:21 +1100297 if (fd >= 0) {
298 error("Failed to disconnect from controlling tty.");
299 close(fd);
300 }
301 /* Make it our controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302#ifdef TIOCSCTTY
Damien Miller95def091999-11-25 00:26:21 +1100303 debug("Setting controlling tty using TIOCSCTTY.");
Kevin Steves6c0d6272000-11-12 09:22:29 +0000304 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
305 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306#endif /* TIOCSCTTY */
Ben Lindstromb5628642000-10-18 00:02:25 +0000307#ifdef HAVE_NEWS4
308 if (setpgrp(0,0) < 0)
309 error("SETPGRP %s",strerror(errno));
310#endif /* HAVE_NEWS4 */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100311#ifdef USE_VHANGUP
Damien Miller4a820ea2001-10-12 19:15:48 +1000312 old = mysignal(SIGHUP, SIG_IGN);
Damien Millerd0cff3e2000-04-20 23:12:58 +1000313 vhangup();
Damien Miller4a820ea2001-10-12 19:15:48 +1000314 mysignal(SIGHUP, old);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100315#endif /* USE_VHANGUP */
Damien Miller95def091999-11-25 00:26:21 +1100316 fd = open(ttyname, O_RDWR);
Damien Millerd0cff3e2000-04-20 23:12:58 +1000317 if (fd < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100318 error("%.100s: %.100s", ttyname, strerror(errno));
Damien Millerd0cff3e2000-04-20 23:12:58 +1000319 } else {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100320#ifdef USE_VHANGUP
Damien Millerd0cff3e2000-04-20 23:12:58 +1000321 close(*ttyfd);
322 *ttyfd = fd;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100323#else /* USE_VHANGUP */
Damien Miller95def091999-11-25 00:26:21 +1100324 close(fd);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100325#endif /* USE_VHANGUP */
Damien Millerd0cff3e2000-04-20 23:12:58 +1000326 }
Damien Miller95def091999-11-25 00:26:21 +1100327 /* Verify that we now have a controlling tty. */
Ben Lindstrom31ca54a2001-02-09 02:11:24 +0000328 fd = open(_PATH_TTY, O_WRONLY);
Damien Miller95def091999-11-25 00:26:21 +1100329 if (fd < 0)
330 error("open /dev/tty failed - could not set controlling tty: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100331 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100332 else {
333 close(fd);
334 }
Damien Miller4a820ea2001-10-12 19:15:48 +1000335#endif /* _CRAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336}
337
338/* Changes the window size associated with the pty. */
339
Damien Miller4af51302000-04-16 11:18:38 +1000340void
Damien Miller95def091999-11-25 00:26:21 +1100341pty_change_window_size(int ptyfd, int row, int col,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100342 int xpixel, int ypixel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000343{
Damien Miller95def091999-11-25 00:26:21 +1100344 struct winsize w;
345 w.ws_row = row;
346 w.ws_col = col;
347 w.ws_xpixel = xpixel;
348 w.ws_ypixel = ypixel;
349 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350}
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100351
352void
353pty_setowner(struct passwd *pw, const char *ttyname)
354{
355 struct group *grp;
356 gid_t gid;
357 mode_t mode;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000358 struct stat st;
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100359
360 /* Determine the group to make the owner of the tty. */
361 grp = getgrnam("tty");
362 if (grp) {
363 gid = grp->gr_gid;
364 mode = S_IRUSR | S_IWUSR | S_IWGRP;
365 } else {
366 gid = pw->pw_gid;
367 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
368 }
369
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000370 /*
371 * Change owner and mode of the tty as required.
Ben Lindstromc88785e2001-08-06 20:47:23 +0000372 * Warn but continue if filesystem is read-only and the uids match/
373 * tty is owned by root.
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000374 */
375 if (stat(ttyname, &st))
376 fatal("stat(%.100s) failed: %.100s", ttyname,
377 strerror(errno));
378
379 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
380 if (chown(ttyname, pw->pw_uid, gid) < 0) {
Damien Miller9f0f5c62001-12-21 14:45:46 +1100381 if (errno == EROFS &&
Ben Lindstromd01ba982001-07-22 20:36:57 +0000382 (st.st_uid == pw->pw_uid || st.st_uid == 0))
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000383 error("chown(%.100s, %d, %d) failed: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100384 ttyname, pw->pw_uid, gid,
385 strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000386 else
387 fatal("chown(%.100s, %d, %d) failed: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100388 ttyname, pw->pw_uid, gid,
389 strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000390 }
391 }
392
393 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
394 if (chmod(ttyname, mode) < 0) {
395 if (errno == EROFS &&
396 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
397 error("chmod(%.100s, 0%o) failed: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100398 ttyname, mode, strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000399 else
400 fatal("chmod(%.100s, 0%o) failed: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100401 ttyname, mode, strerror(errno));
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000402 }
403 }
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100404}