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