Skip to content
This repository
Newer
Older
100644 249 lines (220 sloc) 6.125 kb
adc800bf »
2006-07-24 - stevesk@cvs.openbsd.org 2006/07/22 20:48:23
1 /* $OpenBSD: sshpty.c,v 1.25 2006/07/22 20:48:23 stevesk Exp $ */
8efc0c15 »
1999-10-27 Initial revision
2 /*
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
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.
2724e080 »
2000-04-16 - OpenBSD CVS updates.
7 *
95ecb01d »
2000-09-16 - (djm) Merge OpenBSD changes:
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".
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
13 */
8efc0c15 »
1999-10-27 Initial revision
14
15 #include "includes.h"
a4c7ce09 »
2006-03-15 - stevesk@cvs.openbsd.org 2006/02/10 00:27:13
16
17 #include <sys/ioctl.h>
31679b17 »
2006-03-15 - stevesk@cvs.openbsd.org 2006/02/20 17:02:44
18 #include <sys/types.h>
19 #include <sys/stat.h>
2c94d67f »
2006-03-15 - (tim) [openssh/sshpty.c openssh/openbsd-compat/port-tun.c] put in …
20 #include <signal.h>
8efc0c15 »
1999-10-27 Initial revision
21
cad5be1f »
2006-07-12 - stevesk@cvs.openbsd.org 2006/07/11 20:07:25
22 #include <errno.h>
e4abb5de »
2006-07-10 - stevesk@cvs.openbsd.org 2006/07/09 15:15:11
23 #include <fcntl.h>
a14052ad »
2006-07-10 - stevesk@cvs.openbsd.org 2006/07/02 22:45:59
24 #include <grp.h>
e41bbd7c »
2006-03-15 - stevesk@cvs.openbsd.org 2006/02/08 12:15:27
25 #ifdef HAVE_PATHS_H
2dba9ecf »
2006-03-15 fix spacing of include
26 # include <paths.h>
e41bbd7c »
2006-03-15 - stevesk@cvs.openbsd.org 2006/02/08 12:15:27
27 #endif
caf4a9ae »
2006-07-10 - stevesk@cvs.openbsd.org 2006/07/06 16:03:53
28 #include <pwd.h>
adc800bf »
2006-07-24 - stevesk@cvs.openbsd.org 2006/07/22 20:48:23
29 #include <string.h>
e41bbd7c »
2006-03-15 - stevesk@cvs.openbsd.org 2006/02/08 12:15:27
30 #include <termios.h>
884bcb37 »
1999-12-13 - Clean up broken includes in pty.c
31 #ifdef HAVE_UTIL_H
32 # include <util.h>
33 #endif /* HAVE_UTIL_H */
bbd31521 »
2006-07-24 - stevesk@cvs.openbsd.org 2006/07/17 01:31:10
34 #include <unistd.h>
884bcb37 »
1999-12-13 - Clean up broken includes in pty.c
35
1fb1bd0d »
2001-02-18 - (bal) Markus' blessing to rename login.[ch] -> sshlogin.[ch] and
36 #include "sshpty.h"
9205a863 »
2001-01-22 Hopefully things did not get mixed around too much. It compiles under
37 #include "log.h"
b6d54ab6 »
2001-06-15 - (stevesk) don't set SA_RESTART and set SIGCHLD to SIG_DFL
38 #include "misc.h"
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
39
6d8c4ea4 »
1999-12-07 - Compile fix for Solaris with /dev/ptmx from
40 #ifdef HAVE_PTY_H
41 # include <pty.h>
42 #endif
43
8efc0c15 »
1999-10-27 Initial revision
44 #ifndef O_NOCTTY
45 #define O_NOCTTY 0
46 #endif
47
aa3378df »
1999-11-25 - More reformatting merged from OpenBSD CVS
48 /*
49 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
50 * nonzero if a pty was successfully allocated. On success, open file
51 * descriptors for the pty and tty sides and the name of the tty side are
52 * returned (the buffer must be able to hold at least 64 characters).
53 */
8efc0c15 »
1999-10-27 Initial revision
54
2724e080 »
2000-04-16 - OpenBSD CVS updates.
55 int
aa2f677d »
2006-03-26 - deraadt@cvs.openbsd.org 2006/03/20 18:14:02
56 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
8efc0c15 »
1999-10-27 Initial revision
57 {
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
58 /* openpty(3) exists in OSF/1 and some other os'es */
89d336ee »
2001-02-18 - (djm) Use ttyname() to determine name of tty returned by openpty()
59 char *name;
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
60 int i;
8efc0c15 »
1999-10-27 Initial revision
61
89d336ee »
2001-02-18 - (djm) Use ttyname() to determine name of tty returned by openpty()
62 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
63 if (i < 0) {
64 error("openpty: %.100s", strerror(errno));
65 return 0;
66 }
89d336ee »
2001-02-18 - (djm) Use ttyname() to determine name of tty returned by openpty()
67 name = ttyname(*ttyfd);
68 if (!name)
69 fatal("openpty returns device for which ttyname fails.");
70
71 strlcpy(namebuf, name, namebuflen); /* possible truncation */
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
72 return 1;
8efc0c15 »
1999-10-27 Initial revision
73 }
74
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
75 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
8efc0c15 »
1999-10-27 Initial revision
76
2724e080 »
2000-04-16 - OpenBSD CVS updates.
77 void
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
78 pty_release(const char *tty)
8efc0c15 »
1999-10-27 Initial revision
79 {
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
80 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
81 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
82 if (chmod(tty, (mode_t) 0666) < 0)
83 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
8efc0c15 »
1999-10-27 Initial revision
84 }
85
946f1146 »
2003-06-18 - (djm) OpenBSD CVS Sync
86 /* Makes the tty the process's controlling tty and sets it to sane modes. */
8efc0c15 »
1999-10-27 Initial revision
87
2724e080 »
2000-04-16 - OpenBSD CVS updates.
88 void
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
89 pty_make_controlling_tty(int *ttyfd, const char *tty)
8efc0c15 »
1999-10-27 Initial revision
90 {
0c74f718 »
2001-08-06 - (bal) Second around of UNICOS patches. A few other things left.
91 int fd;
18ef98f0 »
2001-10-12 - (djm) Cleanup sshpty.c a little
92 #ifdef USE_VHANGUP
93 void *old;
94 #endif /* USE_VHANGUP */
0c74f718 »
2001-08-06 - (bal) Second around of UNICOS patches. A few other things left.
95
4597c675 »
2002-09-26 Cray fixes (bug 367) based on patch from Wendy Palm @ cray.
96 #ifdef _UNICOS
757488c2 »
2001-08-14 - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
97 if (setsid() < 0)
98 error("setsid: %.100s", strerror(errno));
0c74f718 »
2001-08-06 - (bal) Second around of UNICOS patches. A few other things left.
99
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
100 fd = open(tty, O_RDWR|O_NOCTTY);
757488c2 »
2001-08-14 - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
101 if (fd != -1) {
3ad60556 »
2003-08-25 - (bal) redo how we handle 'mysignal()'. Move it to
102 signal(SIGHUP, SIG_IGN);
757488c2 »
2001-08-14 - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
103 ioctl(fd, TCVHUP, (char *)NULL);
3ad60556 »
2003-08-25 - (bal) redo how we handle 'mysignal()'. Move it to
104 signal(SIGHUP, SIG_DFL);
757488c2 »
2001-08-14 - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
105 setpgid(0, 0);
106 close(fd);
0c74f718 »
2001-08-06 - (bal) Second around of UNICOS patches. A few other things left.
107 } else {
757488c2 »
2001-08-14 - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
108 error("Failed to disconnect from controlling tty.");
0c74f718 »
2001-08-06 - (bal) Second around of UNICOS patches. A few other things left.
109 }
110
757488c2 »
2001-08-14 - (stevesk) sshpty.c, cray.[ch]: whitespace, formatting and cleanup
111 debug("Setting controlling tty using TCSETCTTY.");
112 ioctl(*ttyfd, TCSETCTTY, NULL);
113 fd = open("/dev/tty", O_RDWR);
114 if (fd < 0)
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
115 error("%.100s: %.100s", tty, strerror(errno));
0c74f718 »
2001-08-06 - (bal) Second around of UNICOS patches. A few other things left.
116 close(*ttyfd);
4a79e169 »
2002-06-09 unexpand
117 *ttyfd = fd;
4597c675 »
2002-09-26 Cray fixes (bug 367) based on patch from Wendy Palm @ cray.
118 #else /* _UNICOS */
8efc0c15 »
1999-10-27 Initial revision
119
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
120 /* First disconnect from the old controlling tty. */
8efc0c15 »
1999-10-27 Initial revision
121 #ifdef TIOCNOTTY
9be250ff »
2001-02-09 - itojun@cvs.openbsd.org 2001/02/08 19:30:52
122 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
123 if (fd >= 0) {
124 (void) ioctl(fd, TIOCNOTTY, NULL);
125 close(fd);
126 }
8efc0c15 »
1999-10-27 Initial revision
127 #endif /* TIOCNOTTY */
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
128 if (setsid() < 0)
129 error("setsid: %.100s", strerror(errno));
130
aa3378df »
1999-11-25 - More reformatting merged from OpenBSD CVS
131 /*
132 * Verify that we are successfully disconnected from the controlling
133 * tty.
134 */
9be250ff »
2001-02-09 - itojun@cvs.openbsd.org 2001/02/08 19:30:52
135 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
136 if (fd >= 0) {
137 error("Failed to disconnect from controlling tty.");
138 close(fd);
139 }
140 /* Make it our controlling tty. */
8efc0c15 »
1999-10-27 Initial revision
141 #ifdef TIOCSCTTY
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
142 debug("Setting controlling tty using TIOCSCTTY.");
0cfade0c »
2000-11-12 - (stevesk) pty.c: HP-UX 10 and 11 don't define TIOCSCTTY. Add error…
143 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
144 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
8efc0c15 »
1999-10-27 Initial revision
145 #endif /* TIOCSCTTY */
b837981d »
2005-05-27 - (dtucker) [acconfig.h configure.ac defines.h includes.h sshpty.c
146 #ifdef NEED_SETPGRP
564e5128 »
2000-10-18 - (bal) Changed from GNU rx to PCRE on suggestion from djm.
147 if (setpgrp(0,0) < 0)
148 error("SETPGRP %s",strerror(errno));
b837981d »
2005-05-27 - (dtucker) [acconfig.h configure.ac defines.h includes.h sshpty.c
149 #endif /* NEED_SETPGRP */
1907a2db »
2000-09-05 - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
150 #ifdef USE_VHANGUP
3ad60556 »
2003-08-25 - (bal) redo how we handle 'mysignal()'. Move it to
151 old = signal(SIGHUP, SIG_IGN);
14ca366c »
2000-04-20 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
152 vhangup();
3ad60556 »
2003-08-25 - (bal) redo how we handle 'mysignal()'. Move it to
153 signal(SIGHUP, old);
1907a2db »
2000-09-05 - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
154 #endif /* USE_VHANGUP */
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
155 fd = open(tty, O_RDWR);
14ca366c »
2000-04-20 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
156 if (fd < 0) {
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
157 error("%.100s: %.100s", tty, strerror(errno));
14ca366c »
2000-04-20 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
158 } else {
1907a2db »
2000-09-05 - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
159 #ifdef USE_VHANGUP
14ca366c »
2000-04-20 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
160 close(*ttyfd);
161 *ttyfd = fd;
1907a2db »
2000-09-05 - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
162 #else /* USE_VHANGUP */
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
163 close(fd);
1907a2db »
2000-09-05 - (djm) Merge cygwin support from Corinna Vinschen <vinschen@cygnus.…
164 #endif /* USE_VHANGUP */
14ca366c »
2000-04-20 - Merge fixes from Debian patch from Phil Hands <phil@hands.com>
165 }
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
166 /* Verify that we now have a controlling tty. */
9be250ff »
2001-02-09 - itojun@cvs.openbsd.org 2001/02/08 19:30:52
167 fd = open(_PATH_TTY, O_WRONLY);
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
168 if (fd < 0)
169 error("open /dev/tty failed - could not set controlling tty: %.100s",
a3e6a8e8 »
2001-12-21 - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
170 strerror(errno));
e75c9554 »
2003-11-21 - djm@cvs.openbsd.org 2003/11/21 11:57:03
171 else
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
172 close(fd);
4597c675 »
2002-09-26 Cray fixes (bug 367) based on patch from Wendy Palm @ cray.
173 #endif /* _UNICOS */
8efc0c15 »
1999-10-27 Initial revision
174 }
175
176 /* Changes the window size associated with the pty. */
177
2724e080 »
2000-04-16 - OpenBSD CVS updates.
178 void
aa2f677d »
2006-03-26 - deraadt@cvs.openbsd.org 2006/03/20 18:14:02
179 pty_change_window_size(int ptyfd, u_int row, u_int col,
180 u_int xpixel, u_int ypixel)
8efc0c15 »
1999-10-27 Initial revision
181 {
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
182 struct winsize w;
5837a75d »
2002-06-23 - deraadt@cvs.openbsd.org 2002/06/23 21:06:13
183
aa2f677d »
2006-03-26 - deraadt@cvs.openbsd.org 2006/03/20 18:14:02
184 /* may truncate u_int -> u_short */
5260325f »
1999-11-24 - Merged very large OpenBSD source code reformat
185 w.ws_row = row;
186 w.ws_col = col;
187 w.ws_xpixel = xpixel;
188 w.ws_ypixel = ypixel;
189 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
8efc0c15 »
1999-10-27 Initial revision
190 }
817175bc »
2000-03-02 - Applied pty cleanup patch from markus.friedl@informatik.uni-erlang…
191
192 void
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
193 pty_setowner(struct passwd *pw, const char *tty)
817175bc »
2000-03-02 - Applied pty cleanup patch from markus.friedl@informatik.uni-erlang…
194 {
195 struct group *grp;
196 gid_t gid;
197 mode_t mode;
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
198 struct stat st;
817175bc »
2000-03-02 - Applied pty cleanup patch from markus.friedl@informatik.uni-erlang…
199
200 /* Determine the group to make the owner of the tty. */
201 grp = getgrnam("tty");
202 if (grp) {
203 gid = grp->gr_gid;
204 mode = S_IRUSR | S_IWUSR | S_IWGRP;
205 } else {
206 gid = pw->pw_gid;
207 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
208 }
209
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
210 /*
211 * Change owner and mode of the tty as required.
6d1cafbc »
2001-08-06 - markus@cvs.openbsd.org 2001/07/22 21:32:27
212 * Warn but continue if filesystem is read-only and the uids match/
213 * tty is owned by root.
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
214 */
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
215 if (stat(tty, &st))
216 fatal("stat(%.100s) failed: %.100s", tty,
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
217 strerror(errno));
218
c8a08d25 »
2006-04-22 - (djm) [Makefile.in configure.ac session.c sshpty.c]
219 #ifdef WITH_SELINUX
220 ssh_selinux_setup_pty(pw->pw_name, tty);
221 #endif
222
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
223 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
224 if (chown(tty, pw->pw_uid, gid) < 0) {
a3e6a8e8 »
2001-12-21 - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
225 if (errno == EROFS &&
5837a75d »
2002-06-23 - deraadt@cvs.openbsd.org 2002/06/23 21:06:13
226 (st.st_uid == pw->pw_uid || st.st_uid == 0))
d5506200 »
2003-02-24 - markus@cvs.openbsd.org 2003/02/03 08:56:16
227 debug("chown(%.100s, %u, %u) failed: %.100s",
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
228 tty, (u_int)pw->pw_uid, (u_int)gid,
a3e6a8e8 »
2001-12-21 - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
229 strerror(errno));
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
230 else
c199eb07 »
2002-06-23 - deraadt@cvs.openbsd.org 2002/06/23 03:30:58
231 fatal("chown(%.100s, %u, %u) failed: %.100s",
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
232 tty, (u_int)pw->pw_uid, (u_int)gid,
a3e6a8e8 »
2001-12-21 - deraadt@cvs.openbsd.org 2001/12/19 07:18:56
233 strerror(errno));
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
234 }
235 }
236
237 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
238 if (chmod(tty, mode) < 0) {
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
239 if (errno == EROFS &&
240 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
d5506200 »
2003-02-24 - markus@cvs.openbsd.org 2003/02/03 08:56:16
241 debug("chmod(%.100s, 0%o) failed: %.100s",
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
242 tty, (u_int)mode, strerror(errno));
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
243 else
244 fatal("chmod(%.100s, 0%o) failed: %.100s",
d710b819 »
2004-06-22 - avsm@cvs.openbsd.org 2004/06/21 17:36:31
245 tty, (u_int)mode, strerror(errno));
e5d1c11f »
2000-12-15 - (stevesk) OpenBSD CVS updates:
246 }
247 }
817175bc »
2000-03-02 - Applied pty cleanup patch from markus.friedl@informatik.uni-erlang…
248 }
Something went wrong with that request. Please try again.