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