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