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