]> andersk Git - openssh.git/blob - sshpty.c
- (djm) [Makefile.in configure.ac session.c sshpty.c]
[openssh.git] / sshpty.c
1 /* $OpenBSD: sshpty.c,v 1.19 2006/03/25 13:17:03 djm 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 #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, size_t 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, u_int row, u_int col,
174         u_int xpixel, u_int ypixel)
175 {
176         struct winsize w;
177
178         /* may truncate u_int -> u_short */
179         w.ws_row = row;
180         w.ws_col = col;
181         w.ws_xpixel = xpixel;
182         w.ws_ypixel = ypixel;
183         (void) ioctl(ptyfd, TIOCSWINSZ, &w);
184 }
185
186 void
187 pty_setowner(struct passwd *pw, const char *tty)
188 {
189         struct group *grp;
190         gid_t gid;
191         mode_t mode;
192         struct stat st;
193
194         /* Determine the group to make the owner of the tty. */
195         grp = getgrnam("tty");
196         if (grp) {
197                 gid = grp->gr_gid;
198                 mode = S_IRUSR | S_IWUSR | S_IWGRP;
199         } else {
200                 gid = pw->pw_gid;
201                 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
202         }
203
204         /*
205          * Change owner and mode of the tty as required.
206          * Warn but continue if filesystem is read-only and the uids match/
207          * tty is owned by root.
208          */
209         if (stat(tty, &st))
210                 fatal("stat(%.100s) failed: %.100s", tty,
211                     strerror(errno));
212
213 #ifdef WITH_SELINUX
214         ssh_selinux_setup_pty(pw->pw_name, tty);
215 #endif
216
217         if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
218                 if (chown(tty, pw->pw_uid, gid) < 0) {
219                         if (errno == EROFS &&
220                             (st.st_uid == pw->pw_uid || st.st_uid == 0))
221                                 debug("chown(%.100s, %u, %u) failed: %.100s",
222                                     tty, (u_int)pw->pw_uid, (u_int)gid,
223                                     strerror(errno));
224                         else
225                                 fatal("chown(%.100s, %u, %u) failed: %.100s",
226                                     tty, (u_int)pw->pw_uid, (u_int)gid,
227                                     strerror(errno));
228                 }
229         }
230
231         if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
232                 if (chmod(tty, mode) < 0) {
233                         if (errno == EROFS &&
234                             (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
235                                 debug("chmod(%.100s, 0%o) failed: %.100s",
236                                     tty, (u_int)mode, strerror(errno));
237                         else
238                                 fatal("chmod(%.100s, 0%o) failed: %.100s",
239                                     tty, (u_int)mode, strerror(errno));
240                 }
241         }
242 }
This page took 0.051511 seconds and 5 git commands to generate.