-
Notifications
You must be signed in to change notification settings - Fork 1
/
openpty.c
148 lines (128 loc) · 3.48 KB
/
openpty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
** openpty.c for zssh
**
** Made by Matthieu Lucotte
** Login <gounter@users.sourceforge.net>
**
** Started on Thu Jun 29 19:10:25 2000 Matthieu Lucotte
** Last update Wed Sep 24 00:05:06 2003
*/
#include "zssh.h"
#define GL_SLAVENAMELEN 50
static char gl_slavename[GL_SLAVENAMELEN + 1] = { 0 };
static char gl_hook_slavename[GL_SLAVENAMELEN + 1] = { 0 };
#ifdef HAVE_UTIL_H
#include <util.h>
#endif
#ifdef HAVE_LIBUTIL_H
#include <libutil.h>
#endif
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
void getmaster(void)
{
#ifdef DEBUG
printf("Using openpty() for tty allocation\n");
#endif
if (openpty(&gl_master, &gl_slave, gl_slavename, &gl_tt, &gl_win) < 0)
error(0, "openpty");
if (gl_copty) {
if (openpty(&gl_hook_master, &gl_hook_slave, gl_hook_slavename, &gl_rtt, &gl_win) < 0)
error(0, "openpty");
}
}
void getslave(void)
{
testslave(gl_slavename);
if (gl_copty) {
testslave(gl_hook_slavename);
}
}
void my_tcsetpgrp(int fd, int pgrpid)
{
int ret;
#ifdef HAVE_TCSETPGRP
ret = tcsetpgrp(fd, pgrpid);
#else
ret = ioctl(fd, TIOCSPGRP, &pgrpid);
#endif /* HAVE_TCSETPGRP */
if (ret < 0)
error(0, "my_tcsetpgrp");
}
/* set raw mode */
void my_cfmakeraw(struct termios *pt)
{
/* beginning of 'official' cfmakeraw function */
pt->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
pt->c_oflag &= ~OPOST;
pt->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
pt->c_cflag &= ~(CSIZE | PARENB);
pt->c_cflag |= CS8;
/* end of 'official' cfmakeraw function */
pt->c_cc[VMIN] = 1;
pt->c_cc[VTIME] = 0;
/* pt->c_oflag |= OPOST; */
/* pt->c_lflag &= ~ECHO; */
}
/* called by getslave()
* test tty permissions and warn user if insecure
*/
void testslave(char *ttyname)
{
struct stat st;
struct passwd *pwd;
int ask = 0;
if (fstat(gl_slave, &st) < 0)
error(0, "fstat tty");
if (st.st_uid != getuid()) { /* tty is not owned by the user, this can be a security issue so prompt the user */
if ( (pwd = getpwuid(st.st_uid)) )
printf("*** %s: This tty is owned by someone else (%s) !\n", ttyname, pwd->pw_name);
else
printf("*** %s: This tty is owned by someone else (uid %lu) !\n", ttyname, (long)st.st_uid);
ask = 1;
}
if (st.st_mode & S_IWOTH)
/* tty is world writeable: this can be abused but there is no serious security issue here
* so just print a warning. */
printf("*** %s: this tty is world writeable !\n", ttyname);
if (st.st_mode & S_IROTH) { /* tty is world readable: this is very insecure so prompt the user */
printf("*** %s: this tty is world readable !\n", ttyname);
ask = 1;
}
if (ask) {
printf("*** This is a security issue\n");
if (!ask_user("Do you want to continue anyway ?", 0, 1))
error("aborting\n", "");
}
}
/* init slave after call to getslave
* make slave the controlling tty for current process
*/
void initslave(void)
{
close(gl_master);
if (gl_copty) {
close(gl_hook_master);
close(gl_hook_slave);
}
setsid();
/* by now we should have dropped the controlling tty
* make sure it is indeed the case
*/
if (open("/dev/tty", O_RDWR) >= 0)
error("Couldn't drop controlling tty\n", "");
#ifdef TIOCSCTTY
if (ioctl(gl_slave, TIOCSCTTY, 0) < 0)
perror("ioctl(slave, TIOCSCTTY, 0)");
#else /* re-open the tty so that it becomes the controlling tty */
close(gl_slave);
if ( (gl_slave = open(gl_slavename, O_RDWR)) < 0 )
error(0, gl_slavename);
#endif /* TIOCSCTTY */
if (dup2(gl_slave, 0) < 0)
error(0, "dup2(slave, 0)");
dup2(gl_slave, 1);
close(gl_slave);
}