This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathptyget.c
194 lines (145 loc) · 3.62 KB
/
ptyget.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include "env.h"
#include "fmt.h"
#include "str.h"
#include "strerr.h"
#include <termios.h>
#include "hasptmx.h"
#include "hasptc.h"
#include "conf-secure.h"
#ifdef HASPTMX
#ifndef PTYGET_OK
#define PTYGET_OK
#include <sys/sysmacros.h>
#include <sys/stropts.h>
#include <sys/stream.h>
#include <sys/ptms.h>
char fnslave[10 + FMT_ULONG]; /* 10 for /dev/pts/ */
char *pty45()
{
struct strioctl si;
struct stat st;
int fd;
char *s;
fd = open("/dev/ptmx",O_RDWR | O_NDELAY);
if (fd == -1) return 0;
if (fdmove(4,fd) == -1) return 0;
if (fstat(4,&st) == -1) return 0;
s = fnslave;
s += fmt_str(s,"/dev/pts/");
s += fmt_ulong(s,(unsigned long) minor(st.st_rdev));
*s = 0;
#ifdef PTYGET_SECURE
if (chown(fnslave,getuid(),getegid()) == -1)
return 0;
if (chmod(fnslave,0600) == -1)
return 0;
#endif
si.ic_cmd = UNLKPT;
si.ic_timout = 0;
si.ic_len = 0;
si.ic_dp = 0;
if (ioctl(4,I_STR,&si) == -1) return 0;
fd = open(fnslave,O_RDWR | O_NOCTTY);
if (fd == -1) return 0;
if (fdmove(5,fd) == -1) return 0;
if (ioctl(5,I_PUSH,"ptem") == -1) return 0;
if (ioctl(5,I_PUSH,"ldterm") == -1) return 0;
if (tcflush(5,TCIOFLUSH) == -1) return 0;
return fnslave;
}
#endif
#endif
#ifdef HASPTC
#ifndef PTYGET_OK
#define PTYGET_OK
#ifdef PTYGET_SECURE
ERROR! ptc-based systems have no pty security.
#endif
#include <sys/sysmacros.h>
char fnslave[10 + FMT_ULONG]; /* 10 for /dev/pts/ */
char *pty45()
{
struct stat st;
int fd;
fd = open("/dev/ptc",O_RDWR | O_NDELAY);
if (fd == -1) return 0;
if (fdmove(4,fd) == -1) return 0;
if (fstat(4,&st) == -1) return 0;
s = fnslave;
s += fmt_str(s,"/dev/pts/");
s += fmt_ulong(s,(unsigned long) minor(st.st_rdev));
*s = 0;
fd = open(fnslave,O_RDWR | O_NOCTTY);
if (fd == -1) return 0;
if (fdmove(5,fd) == -1) return 0;
return fnslave;
}
#endif
#endif
#ifndef PTYGET_OK
char fnmaster[] = "/dev/pty??";
char fnslave[] = "/dev/tty??";
/* Do not change the sizes of pty1 and pty2! */
char pty1[16] = "pqrstuvwxyzPQRST";
char pty2[16] = "0123456789abcdef";
char *pty45()
{
int fd;
struct stat st;
int pos;
int increment;
int loop;
pos = 255 & getpid();
increment = 1 + (254 & time((long *) 0));
for (loop = 0;loop < 256;++loop) {
pos = 255 & (pos + increment);
fnslave[8] = fnmaster[8] = pty1[15 & pos];
fnslave[9] = fnmaster[9] = pty2[15 & (pos >> 4)];
if (fnmaster[8] == '-') continue;
if (stat(fnmaster,&st) == -1) { pty1[15 & pos] = '-'; continue; }
fd = open(fnmaster,O_RDWR | O_NDELAY);
if (fd != -1) {
if (fdmove(4,fd) == -1) return 0;
#ifdef PTYGET_SECURE
if (chown(fnslave,getuid(),getegid()) == -1)
return 0;
if (chmod(fnslave,0600) == -1)
return 0;
revoke(fnslave);
#endif
fd = open(fnslave,O_RDWR | O_NOCTTY);
if (fd != -1) {
if (fdmove(5,fd) == -1) return 0;
return fnslave;
}
close(4);
}
}
return 0;
}
#endif
void main(argc,argv)
int argc;
char **argv;
{
char *tty;
tty = pty45();
if (setgid(getgid()) == -1) _exit(1);
if (setuid(getuid()) == -1) _exit(1);
if (!tty)
strerr_die2sys(111,"ptyget: fatal: ","unable to allocate pty: ");
#ifndef PTYGET_SECURE
strerr_warn1("ptyget: warning: unable to allocate secure pty",0);
#endif
if (!env_put2("TTY",tty))
strerr_die2x(111,"ptyget: fatal: ","out of memory");
if (!argv[1])
strerr_die1x(1,"ptyget: usage: ptyget subprogram [ args ... ]");
execvp(argv[1],argv + 1);
strerr_die4sys(111,"ptyget: fatal: ","unable to run ",argv[1],": ");
}