]> andersk Git - openssh.git/blob - sshpty.c
973c2d2d87e7b73ba532ca1dd537c08901cc4c7f
[openssh.git] / sshpty.c
1 /* $OpenBSD: sshpty.c,v 1.21 2006/07/06 16:03:53 stevesk Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * Allocating a pseudo-terminal, and making it the controlling tty.
7  *
8  * As far as I am concerned, the code I have written for this software
9  * can be used freely for any purpose.  Any derived versions of this
10  * software must be clearly marked as such, and if the derived work is
11  * incompatible with the protocol description in the RFC file, it must be
12  * called by a name other than "ssh" or "Secure Shell".
13  */
14
15 #include "includes.h"
16
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <signal.h>
21
22 #include <grp.h>
23 #ifdef HAVE_PATHS_H
24 # include <paths.h>
25 #endif
26 #include <pwd.h>
27 #include <termios.h>
28 #ifdef HAVE_UTIL_H
29 # include <util.h>
30 #endif /* HAVE_UTIL_H */
31
32 #include "sshpty.h"
33 #include "log.h"
34 #include "misc.h"
35
36 #ifdef HAVE_PTY_H
37 # include <pty.h>
38 #endif
39
40 #ifndef O_NOCTTY
41 #define O_NOCTTY 0
42 #endif
43
44 /*
45  * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
46  * nonzero if a pty was successfully allocated.  On success, open file
47  * descriptors for the pty and tty sides and the name of the tty side are
48  * returned (the buffer must be able to hold at least 64 characters).
49  */
50
51 int
52 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
53 {
54         /* openpty(3) exists in OSF/1 and some other os'es */
55         char *name;
56         int i;
57
58         i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
59         if (i < 0) {
60                 error("openpty: %.100s", strerror(errno));
61                 return 0;
62         }
63         name = ttyname(*ttyfd);
64         if (!name)
65                 fatal("openpty returns device for which ttyname fails.");
66
67         strlcpy(namebuf, name, namebuflen);     /* possible truncation */
68         return 1;
69 }
70
71 /* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
72
73 void
74 pty_release(const char *tty)
75 {
76         if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
77                 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
78         if (chmod(tty, (mode_t) 0666) < 0)
79                 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
80 }
81
82 /* Makes the tty the process's controlling tty and sets it to sane modes. */
83
84 void
85 pty_make_controlling_tty(int *ttyfd, const char *tty)
86 {
87         int fd;
88 #ifdef USE_VHANGUP
89         void *old;
90 #endif /* USE_VHANGUP */
91
92 #ifdef _UNICOS
93         if (setsid() < 0)
94                 error("setsid: %.100s", strerror(errno));
95
96         fd = open(tty, O_RDWR|O_NOCTTY);
97         if (fd != -1) {
98                 signal(SIGHUP, SIG_IGN);
99                 ioctl(fd, TCVHUP, (char *)NULL);
100                 signal(SIGHUP, SIG_DFL);
101                 setpgid(0, 0);
102                 close(fd);
103         } else {
104                 error("Failed to disconnect from controlling tty.");
105         }
106
107         debug("Setting controlling tty using TCSETCTTY.");
108         ioctl(*ttyfd, TCSETCTTY, NULL);
109         fd = open("/dev/tty", O_RDWR);
110         if (fd < 0)
111                 error("%.100s: %.100s", tty, strerror(errno));
112         close(*ttyfd);
113         *ttyfd = fd;
114 #else /* _UNICOS */
115
116         /* First disconnect from the old controlling tty. */
117 #ifdef TIOCNOTTY
118         fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
119         if (fd >= 0) {
120                 (void) ioctl(fd, TIOCNOTTY, NULL);
121                 close(fd);
122         }
123 #endif /* TIOCNOTTY */
124         if (setsid() < 0)
125                 error("setsid: %.100s", strerror(errno));
126
127         /*
128          * Verify that we are successfully disconnected from the controlling
129          * tty.
130          */
131         fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
132         if (fd >= 0) {
133                 error("Failed to disconnect from controlling tty.");
134                 close(fd);
135         }
136         /* Make it our controlling tty. */
137 #ifdef TIOCSCTTY
138         debug("Setting controlling tty using TIOCSCTTY.");
139         if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
140                 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
141 #endif /* TIOCSCTTY */
142 #ifdef NEED_SETPGRP
143         if (setpgrp(0,0) < 0)
144                 error("SETPGRP %s",strerror(errno));
145 #endif /* NEED_SETPGRP */
146 #ifdef USE_VHANGUP
147         old = signal(SIGHUP, SIG_IGN);
148         vhangup();
149         signal(SIGHUP, old);
150 #endif /* USE_VHANGUP */
151         fd = open(tty, O_RDWR);
152         if (fd < 0) {
153                 error("%.100s: %.100s", tty, strerror(errno));
154         } else {
155 #ifdef USE_VHANGUP
156                 close(*ttyfd);
157                 *ttyfd = fd;
158 #else /* USE_VHANGUP */
159                 close(fd);
160 #endif /* USE_VHANGUP */
161         }
162         /* Verify that we now have a controlling tty. */
163         fd = open(_PATH_TTY, O_WRONLY);
164         if (fd < 0)
165                 error("open /dev/tty failed - could not set controlling tty: %.100s",
166                     strerror(errno));
167         else
168                 close(fd);
169 #endif /* _UNICOS */
170 }
171
172 /* Changes the window size associated with the pty. */
173
174 void
175 pty_change_window_size(int ptyfd, u_int row, u_int col,
176         u_int xpixel, u_int ypixel)
177 {
178         struct winsize w;
179
180         /* may truncate u_int -> u_short */
181         w.ws_row = row;
182         w.ws_col = col;
183         w.ws_xpixel = xpixel;
184         w.ws_ypixel = ypixel;
185         (void) ioctl(ptyfd, TIOCSWINSZ, &w);
186 }
187
188 void
189 pty_setowner(struct passwd *pw, const char *tty)
190 {
191         struct group *grp;
192         gid_t gid;
193         mode_t mode;
194         struct stat st;
195
196         /* Determine the group to make the owner of the tty. */
197         grp = getgrnam("tty");
198         if (grp) {
199                 gid = grp->gr_gid;
200                 mode = S_IRUSR | S_IWUSR | S_IWGRP;
201         } else {
202                 gid = pw->pw_gid;
203                 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
204         }
205
206         /*
207          * Change owner and mode of the tty as required.
208          * Warn but continue if filesystem is read-only and the uids match/
209          * tty is owned by root.
210          */
211         if (stat(tty, &st))
212                 fatal("stat(%.100s) failed: %.100s", tty,
213                     strerror(errno));
214
215 #ifdef WITH_SELINUX
216         ssh_selinux_setup_pty(pw->pw_name, tty);
217 #endif
218
219         if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
220                 if (chown(tty, pw->pw_uid, gid) < 0) {
221                         if (errno == EROFS &&
222                             (st.st_uid == pw->pw_uid || st.st_uid == 0))
223                                 debug("chown(%.100s, %u, %u) failed: %.100s",
224                                     tty, (u_int)pw->pw_uid, (u_int)gid,
225                                     strerror(errno));
226                         else
227                                 fatal("chown(%.100s, %u, %u) failed: %.100s",
228                                     tty, (u_int)pw->pw_uid, (u_int)gid,
229                                     strerror(errno));
230                 }
231         }
232
233         if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
234                 if (chmod(tty, mode) < 0) {
235                         if (errno == EROFS &&
236                             (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
237                                 debug("chmod(%.100s, 0%o) failed: %.100s",
238                                     tty, (u_int)mode, strerror(errno));
239                         else
240                                 fatal("chmod(%.100s, 0%o) failed: %.100s",
241                                     tty, (u_int)mode, strerror(errno));
242                 }
243         }
244 }
This page took 0.300454 seconds and 3 git commands to generate.