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