]> andersk Git - gssapi-openssh.git/blob - openssh/sshpty.c
The man2html from jbasney on pkilab2 works whereas the standard one doesn't.
[gssapi-openssh.git] / openssh / 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.3 2001/07/22 21:32:27 markus Exp $");
16
17 #ifdef HAVE_UTIL_H
18 # include <util.h>
19 #endif /* HAVE_UTIL_H */
20
21 #include "sshpty.h"
22 #include "log.h"
23 #include "misc.h"
24
25 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
26 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
27 #undef HAVE_DEV_PTMX
28 #endif
29
30 #ifdef HAVE_PTY_H
31 # include <pty.h>
32 #endif
33 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
34 # include <sys/stropts.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 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
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 #else /* HAVE_OPENPTY */
68 #ifdef HAVE__GETPTY
69         /*
70          * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
71          * pty's automagically when needed
72          */
73         char *slave;
74
75         slave = _getpty(ptyfd, O_RDWR, 0622, 0);
76         if (slave == NULL) {
77                 error("_getpty: %.100s", strerror(errno));
78                 return 0;
79         }
80         strlcpy(namebuf, slave, namebuflen);
81         /* Open the slave side. */
82         *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
83         if (*ttyfd < 0) {
84                 error("%.200s: %.100s", namebuf, strerror(errno));
85                 close(*ptyfd);
86                 return 0;
87         }
88         return 1;
89 #else /* HAVE__GETPTY */
90 #if defined(HAVE_DEV_PTMX)
91         /*
92          * This code is used e.g. on Solaris 2.x.  (Note that Solaris 2.3
93          * also has bsd-style ptys, but they simply do not work.)
94          */
95         int ptm;
96         char *pts;
97         mysig_t old_signal;
98
99         ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
100         if (ptm < 0) {
101                 error("/dev/ptmx: %.100s", strerror(errno));
102                 return 0;
103         }
104         old_signal = mysignal(SIGCHLD, SIG_DFL);
105         if (grantpt(ptm) < 0) {
106                 error("grantpt: %.100s", strerror(errno));
107                 return 0;
108         }
109         mysignal(SIGCHLD, old_signal);
110         if (unlockpt(ptm) < 0) {
111                 error("unlockpt: %.100s", strerror(errno));
112                 return 0;
113         }
114         pts = ptsname(ptm);
115         if (pts == NULL)
116                 error("Slave pty side name could not be obtained.");
117         strlcpy(namebuf, pts, namebuflen);
118         *ptyfd = ptm;
119
120         /* Open the slave side. */
121         *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
122         if (*ttyfd < 0) {
123                 error("%.100s: %.100s", namebuf, strerror(errno));
124                 close(*ptyfd);
125                 return 0;
126         }
127 #ifndef HAVE_CYGWIN
128         /*
129          * Push the appropriate streams modules, as described in Solaris pts(7).
130          * HP-UX pts(7) doesn't have ttcompat module.
131          */
132         if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
133                 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
134         if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
135                 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
136 #ifndef __hpux
137         if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
138                 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
139 #endif
140 #endif
141         return 1;
142 #else /* HAVE_DEV_PTMX */
143 #ifdef HAVE_DEV_PTS_AND_PTC
144         /* AIX-style pty code. */
145         const char *name;
146
147         *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
148         if (*ptyfd < 0) {
149                 error("Could not open /dev/ptc: %.100s", strerror(errno));
150                 return 0;
151         }
152         name = ttyname(*ptyfd);
153         if (!name)
154                 fatal("Open of /dev/ptc returns device for which ttyname fails.");
155         strlcpy(namebuf, name, namebuflen);
156         *ttyfd = open(name, O_RDWR | O_NOCTTY);
157         if (*ttyfd < 0) {
158                 error("Could not open pty slave side %.100s: %.100s",
159                       name, strerror(errno));
160                 close(*ptyfd);
161                 return 0;
162         }
163         return 1;
164 #else /* HAVE_DEV_PTS_AND_PTC */
165 #ifdef _CRAY
166         char buf[64];
167         int i;
168         int highpty;
169
170 #ifdef _SC_CRAY_NPTY
171         highpty = sysconf(_SC_CRAY_NPTY);
172         if (highpty == -1)
173                 highpty = 128;
174 #else
175         highpty = 128;
176 #endif
177
178         for (i = 0; i < highpty; i++) {
179                 snprintf(buf, sizeof(buf), "/dev/pty/%03d", i);
180                 *ptyfd = open(buf, O_RDWR|O_NOCTTY);
181                 if (*ptyfd < 0)
182                         continue;
183                 snprintf(namebuf, namebuflen, "/dev/ttyp%03d", i);
184                 /* Open the slave side. */
185                 *ttyfd = open(namebuf, O_RDWR|O_NOCTTY);
186                 if (*ttyfd < 0) {
187                         error("%.100s: %.100s", namebuf, strerror(errno));
188                         close(*ptyfd);
189                         return 0;
190                 }
191                 return 1;
192         }
193         return 0;
194 #else
195         /* BSD-style pty code. */
196         char buf[64];
197         int i;
198         const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
199         const char *ptyminors = "0123456789abcdef";
200         int num_minors = strlen(ptyminors);
201         int num_ptys = strlen(ptymajors) * num_minors;
202
203         for (i = 0; i < num_ptys; i++) {
204                 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
205                          ptyminors[i % num_minors]);
206                 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
207                     ptymajors[i / num_minors], ptyminors[i % num_minors]);
208
209                 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
210                 if (*ptyfd < 0) {
211                         /* Try SCO style naming */
212                         snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
213                         snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
214                         *ptyfd = open(buf, O_RDWR | O_NOCTTY);
215                         if (*ptyfd < 0)
216                                 continue;
217                 }
218
219                 /* Open the slave side. */
220                 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
221                 if (*ttyfd < 0) {
222                         error("%.100s: %.100s", namebuf, strerror(errno));
223                         close(*ptyfd);
224                         return 0;
225                 }
226                 return 1;
227         }
228         return 0;
229 #endif /* CRAY */
230 #endif /* HAVE_DEV_PTS_AND_PTC */
231 #endif /* HAVE_DEV_PTMX */
232 #endif /* HAVE__GETPTY */
233 #endif /* HAVE_OPENPTY */
234 }
235
236 /* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
237
238 void
239 pty_release(const char *ttyname)
240 {
241         if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
242                 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
243         if (chmod(ttyname, (mode_t) 0666) < 0)
244                 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
245 }
246
247 /* Makes the tty the processes controlling tty and sets it to sane modes. */
248
249 void
250 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
251 {
252         int fd;
253 #ifdef USE_VHANGUP
254         void *old;
255 #endif /* USE_VHANGUP */
256
257 #ifdef _CRAY
258         if (setsid() < 0)
259                 error("setsid: %.100s", strerror(errno));
260
261         fd = open(ttyname, O_RDWR|O_NOCTTY);
262         if (fd != -1) {
263                 mysignal(SIGHUP, SIG_IGN);
264                 ioctl(fd, TCVHUP, (char *)NULL);
265                 mysignal(SIGHUP, SIG_DFL);
266                 setpgid(0, 0);
267                 close(fd);
268         } else {
269                 error("Failed to disconnect from controlling tty.");
270         }
271
272         debug("Setting controlling tty using TCSETCTTY.");
273         ioctl(*ttyfd, TCSETCTTY, NULL);
274         fd = open("/dev/tty", O_RDWR);
275         if (fd < 0)
276                 error("%.100s: %.100s", ttyname, strerror(errno));
277         close(*ttyfd);
278         *ttyfd = fd;
279 #else /* _CRAY */
280
281         /* First disconnect from the old controlling tty. */
282 #ifdef TIOCNOTTY
283         fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
284         if (fd >= 0) {
285                 (void) ioctl(fd, TIOCNOTTY, NULL);
286                 close(fd);
287         }
288 #endif /* TIOCNOTTY */
289         if (setsid() < 0)
290                 error("setsid: %.100s", strerror(errno));
291
292         /*
293          * Verify that we are successfully disconnected from the controlling
294          * tty.
295          */
296         fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
297         if (fd >= 0) {
298                 error("Failed to disconnect from controlling tty.");
299                 close(fd);
300         }
301         /* Make it our controlling tty. */
302 #ifdef TIOCSCTTY
303         debug("Setting controlling tty using TIOCSCTTY.");
304         if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
305                 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
306 #endif /* TIOCSCTTY */
307 #ifdef HAVE_NEWS4
308         if (setpgrp(0,0) < 0)
309                 error("SETPGRP %s",strerror(errno));
310 #endif /* HAVE_NEWS4 */
311 #ifdef USE_VHANGUP
312         old = mysignal(SIGHUP, SIG_IGN);
313         vhangup();
314         mysignal(SIGHUP, old);
315 #endif /* USE_VHANGUP */
316         fd = open(ttyname, O_RDWR);
317         if (fd < 0) {
318                 error("%.100s: %.100s", ttyname, strerror(errno));
319         } else {
320 #ifdef USE_VHANGUP
321                 close(*ttyfd);
322                 *ttyfd = fd;
323 #else /* USE_VHANGUP */
324                 close(fd);
325 #endif /* USE_VHANGUP */
326         }
327         /* Verify that we now have a controlling tty. */
328         fd = open(_PATH_TTY, O_WRONLY);
329         if (fd < 0)
330                 error("open /dev/tty failed - could not set controlling tty: %.100s",
331                       strerror(errno));
332         else {
333                 close(fd);
334         }
335 #endif /* _CRAY */
336 }
337
338 /* Changes the window size associated with the pty. */
339
340 void
341 pty_change_window_size(int ptyfd, int row, int col,
342                        int xpixel, int ypixel)
343 {
344         struct winsize w;
345         w.ws_row = row;
346         w.ws_col = col;
347         w.ws_xpixel = xpixel;
348         w.ws_ypixel = ypixel;
349         (void) ioctl(ptyfd, TIOCSWINSZ, &w);
350 }
351
352 void
353 pty_setowner(struct passwd *pw, const char *ttyname)
354 {
355         struct group *grp;
356         gid_t gid;
357         mode_t mode;
358         struct stat st;
359
360         /* Determine the group to make the owner of the tty. */
361         grp = getgrnam("tty");
362         if (grp) {
363                 gid = grp->gr_gid;
364                 mode = S_IRUSR | S_IWUSR | S_IWGRP;
365         } else {
366                 gid = pw->pw_gid;
367                 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
368         }
369
370         /*
371          * Change owner and mode of the tty as required.
372          * Warn but continue if filesystem is read-only and the uids match/
373          * tty is owned by root.
374          */
375         if (stat(ttyname, &st))
376                 fatal("stat(%.100s) failed: %.100s", ttyname,
377                     strerror(errno));
378
379         if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
380                 if (chown(ttyname, pw->pw_uid, gid) < 0) {
381                         if (errno == EROFS && 
382                            (st.st_uid == pw->pw_uid || st.st_uid == 0))
383                                 error("chown(%.100s, %d, %d) failed: %.100s",
384                                       ttyname, pw->pw_uid, gid,
385                                       strerror(errno));
386                         else
387                                 fatal("chown(%.100s, %d, %d) failed: %.100s",
388                                       ttyname, pw->pw_uid, gid,
389                                       strerror(errno));
390                 }
391         }
392
393         if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
394                 if (chmod(ttyname, mode) < 0) {
395                         if (errno == EROFS &&
396                             (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
397                                 error("chmod(%.100s, 0%o) failed: %.100s",
398                                       ttyname, mode, strerror(errno));
399                         else
400                                 fatal("chmod(%.100s, 0%o) failed: %.100s",
401                                       ttyname, mode, strerror(errno));
402                 }
403         }
404 }
This page took 0.068733 seconds and 5 git commands to generate.