2 /* $OpenBSD: sshpty.c,v 1.28 2007/09/11 23:49:09 stevesk Exp $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * Allocating a pseudo-terminal, and making it the controlling tty.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
17 __RCSID("$NetBSD: sshpty.c,v 1.13 2008/04/06 23:38:20 christos Exp $");
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
41 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
42 * nonzero if a pty was successfully allocated. On success, open file
43 * descriptors for the pty and tty sides and the name of the tty side are
44 * returned (the buffer must be able to hold at least 64 characters).
48 pty_allocate(int *ptyfd
, int *ttyfd
, char *namebuf
, size_t namebuflen
)
53 i
= openpty(ptyfd
, ttyfd
, buf
, NULL
, NULL
);
55 error("openpty: %.100s", strerror(errno
));
58 strlcpy(namebuf
, buf
, namebuflen
); /* possible truncation */
62 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
65 pty_release(const char *tty
)
67 if (chown(tty
, (uid_t
) 0, (gid_t
) 0) < 0)
68 error("chown %.100s 0 0 failed: %.100s", tty
, strerror(errno
));
69 if (chmod(tty
, (mode_t
) 0666) < 0)
70 error("chmod %.100s 0666 failed: %.100s", tty
, strerror(errno
));
73 /* Makes the tty the process's controlling tty and sets it to sane modes. */
76 pty_make_controlling_tty(int *ttyfd
, const char *tty
)
80 /* First disconnect from the old controlling tty. */
82 fd
= open(_PATH_TTY
, O_RDWR
| O_NOCTTY
);
84 (void) ioctl(fd
, TIOCNOTTY
, NULL
);
87 #endif /* TIOCNOTTY */
89 error("setsid: %.100s", strerror(errno
));
92 * Verify that we are successfully disconnected from the controlling
95 fd
= open(_PATH_TTY
, O_RDWR
| O_NOCTTY
);
97 error("Failed to disconnect from controlling tty.");
100 /* Make it our controlling tty. */
102 debug("Setting controlling tty using TIOCSCTTY.");
103 if (ioctl(*ttyfd
, TIOCSCTTY
, NULL
) < 0)
104 error("ioctl(TIOCSCTTY): %.100s", strerror(errno
));
105 #endif /* TIOCSCTTY */
106 fd
= open(tty
, O_RDWR
);
108 error("%.100s: %.100s", tty
, strerror(errno
));
112 /* Verify that we now have a controlling tty. */
113 fd
= open(_PATH_TTY
, O_WRONLY
);
115 error("open /dev/tty failed - could not set controlling tty: %.100s",
121 /* Changes the window size associated with the pty. */
124 pty_change_window_size(int ptyfd
, u_int row
, u_int col
,
125 u_int xpixel
, u_int ypixel
)
129 /* may truncate u_int -> u_short */
132 w
.ws_xpixel
= xpixel
;
133 w
.ws_ypixel
= ypixel
;
134 (void) ioctl(ptyfd
, TIOCSWINSZ
, &w
);
138 pty_setowner(struct passwd
*pw
, const char *tty
)
145 /* Determine the group to make the owner of the tty. */
146 grp
= getgrnam("tty");
149 mode
= S_IRUSR
| S_IWUSR
| S_IWGRP
;
152 mode
= S_IRUSR
| S_IWUSR
| S_IWGRP
| S_IWOTH
;
156 * Change owner and mode of the tty as required.
157 * Warn but continue if filesystem is read-only and the uids match/
158 * tty is owned by root.
161 fatal("stat(%.100s) failed: %.100s", tty
,
164 if (st
.st_uid
!= pw
->pw_uid
|| st
.st_gid
!= gid
) {
165 if (chown(tty
, pw
->pw_uid
, gid
) < 0) {
166 if (errno
== EROFS
&&
167 (st
.st_uid
== pw
->pw_uid
|| st
.st_uid
== 0))
168 debug("chown(%.100s, %u, %u) failed: %.100s",
169 tty
, (u_int
)pw
->pw_uid
, (u_int
)gid
,
172 fatal("chown(%.100s, %u, %u) failed: %.100s",
173 tty
, (u_int
)pw
->pw_uid
, (u_int
)gid
,
178 if ((st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
)) != mode
) {
179 if (chmod(tty
, mode
) < 0) {
180 if (errno
== EROFS
&&
181 (st
.st_mode
& (S_IRGRP
| S_IROTH
)) == 0)
182 debug("chmod(%.100s, 0%o) failed: %.100s",
183 tty
, (u_int
)mode
, strerror(errno
));
185 fatal("chmod(%.100s, 0%o) failed: %.100s",
186 tty
, (u_int
)mode
, strerror(errno
));