No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / sshpty.c
bloba262e76864321a91805ea5c2898d7581d4fd969e
1 /* $NetBSD$ */
2 /* $OpenBSD: sshpty.c,v 1.28 2007/09/11 23:49:09 stevesk Exp $ */
3 /*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
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".
16 #include "includes.h"
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>
20 #include <sys/stat.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <grp.h>
25 #include <paths.h>
26 #include <pwd.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <termios.h>
30 #include <unistd.h>
31 #include <util.h>
33 #include "sshpty.h"
34 #include "log.h"
36 #ifndef O_NOCTTY
37 #define O_NOCTTY 0
38 #endif
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).
47 int
48 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
50 char buf[64];
51 int i;
53 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
54 if (i < 0) {
55 error("openpty: %.100s", strerror(errno));
56 return 0;
58 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
59 return 1;
62 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
64 void
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. */
75 void
76 pty_make_controlling_tty(int *ttyfd, const char *tty)
78 int fd;
80 /* First disconnect from the old controlling tty. */
81 #ifdef TIOCNOTTY
82 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
83 if (fd >= 0) {
84 (void) ioctl(fd, TIOCNOTTY, NULL);
85 close(fd);
87 #endif /* TIOCNOTTY */
88 if (setsid() < 0)
89 error("setsid: %.100s", strerror(errno));
92 * Verify that we are successfully disconnected from the controlling
93 * tty.
95 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
96 if (fd >= 0) {
97 error("Failed to disconnect from controlling tty.");
98 close(fd);
100 /* Make it our controlling tty. */
101 #ifdef TIOCSCTTY
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);
107 if (fd < 0)
108 error("%.100s: %.100s", tty, strerror(errno));
109 else
110 close(fd);
112 /* Verify that we now have a controlling tty. */
113 fd = open(_PATH_TTY, O_WRONLY);
114 if (fd < 0)
115 error("open /dev/tty failed - could not set controlling tty: %.100s",
116 strerror(errno));
117 else
118 close(fd);
121 /* Changes the window size associated with the pty. */
123 void
124 pty_change_window_size(int ptyfd, u_int row, u_int col,
125 u_int xpixel, u_int ypixel)
127 struct winsize w;
129 /* may truncate u_int -> u_short */
130 w.ws_row = row;
131 w.ws_col = col;
132 w.ws_xpixel = xpixel;
133 w.ws_ypixel = ypixel;
134 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
137 void
138 pty_setowner(struct passwd *pw, const char *tty)
140 struct group *grp;
141 gid_t gid;
142 mode_t mode;
143 struct stat st;
145 /* Determine the group to make the owner of the tty. */
146 grp = getgrnam("tty");
147 if (grp) {
148 gid = grp->gr_gid;
149 mode = S_IRUSR | S_IWUSR | S_IWGRP;
150 } else {
151 gid = pw->pw_gid;
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.
160 if (stat(tty, &st))
161 fatal("stat(%.100s) failed: %.100s", tty,
162 strerror(errno));
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,
170 strerror(errno));
171 else
172 fatal("chown(%.100s, %u, %u) failed: %.100s",
173 tty, (u_int)pw->pw_uid, (u_int)gid,
174 strerror(errno));
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));
184 else
185 fatal("chmod(%.100s, 0%o) failed: %.100s",
186 tty, (u_int)mode, strerror(errno));