forked from nelhage/reptyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reptyr.c
308 lines (280 loc) · 8.78 KB
/
reptyr.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (C) 2011 by Nelson Elhage
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <termios.h>
#include <signal.h>
#include "reptyr.h"
#include "reallocarray.h"
#include "platform/platform.h"
static int verbose = 0;
void _debug(const char *pfx, const char *msg, va_list ap) {
if (pfx)
fprintf(stderr, "%s", pfx);
vfprintf(stderr, msg, ap);
fprintf(stderr, "\n");
}
void die(const char *msg, ...) {
va_list ap;
va_start(ap, msg);
_debug("[!] ", msg, ap);
va_end(ap);
exit(1);
}
void debug(const char *msg, ...) {
va_list ap;
if (!verbose)
return;
va_start(ap, msg);
_debug("[+] ", msg, ap);
va_end(ap);
}
void error(const char *msg, ...) {
va_list ap;
va_start(ap, msg);
_debug("[-] ", msg, ap);
va_end(ap);
}
void setup_raw(struct termios *save) {
struct termios set;
if (tcgetattr(0, save) < 0) {
fprintf(stderr, "Unable to read terminal attributes: %m");
return;
}
set = *save;
cfmakeraw(&set);
if (tcsetattr(0, TCSANOW, &set) < 0)
die("Unable to set terminal attributes: %m");
}
void resize_pty(int pty) {
struct winsize sz;
if (ioctl(0, TIOCGWINSZ, &sz) < 0) {
// provide fake size to workaround some problems
struct winsize defaultsize = {30, 80, 640, 480};
if (ioctl(pty, TIOCSWINSZ, &defaultsize) < 0) {
fprintf(stderr, "Cannot set terminal size\n");
}
return;
}
ioctl(pty, TIOCSWINSZ, &sz);
}
int writeall(int fd, const void *buf, ssize_t count) {
ssize_t rv;
while (count > 0) {
rv = write(fd, buf, count);
if (rv < 0) {
if (errno == EINTR)
continue;
return rv;
}
count -= rv;
buf += rv;
}
return 0;
}
volatile sig_atomic_t winch_happened = 0;
void do_winch(int signal) {
winch_happened = 1;
}
void do_proxy(int pty) {
char buf[4096];
ssize_t count;
fd_set set;
sigset_t mask;
sigset_t select_mask;
struct sigaction sa;
// Block WINCH while we're outside the select, but unblock it
// while we're inside:
sigemptyset(&mask);
sigaddset(&mask, SIGWINCH);
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
fprintf(stderr, "sigprocmask: %m");
return;
}
sa.sa_handler = do_winch;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGWINCH, &sa, NULL);
resize_pty(pty);
while (1) {
if (winch_happened) {
winch_happened = 0;
resize_pty(pty);
}
FD_ZERO(&set);
FD_SET(0, &set);
FD_SET(pty, &set);
sigemptyset(&select_mask);
if (pselect(pty + 1, &set, NULL, NULL, NULL, &select_mask) < 0) {
if (errno == EINTR)
continue;
fprintf(stderr, "select: %m");
return;
}
if (FD_ISSET(0, &set)) {
count = read(0, buf, sizeof buf);
if (count < 0)
return;
writeall(pty, buf, count);
}
if (FD_ISSET(pty, &set)) {
count = read(pty, buf, sizeof buf);
if (count <= 0)
return;
writeall(1, buf, count);
}
}
}
void usage(char *me) {
fprintf(stderr, "Usage: %s [-s] PID\n", me);
fprintf(stderr, " %s -l|-L [COMMAND [ARGS]]\n", me);
fprintf(stderr, " -l Create a new pty pair and print the name of the slave.\n");
fprintf(stderr, " if there are command-line arguments after -l\n");
fprintf(stderr, " they are executed with REPTYR_PTY set to path of pty.\n");
fprintf(stderr, " -L Like '-l', but also redirect the child's stdio to the slave.\n");
fprintf(stderr, " -s Attach fds 0-2 on the target, even if it is not attached to a tty.\n");
fprintf(stderr, " -T Steal the entire terminal session of the target.\n");
fprintf(stderr, " [experimental] May be more reliable, and will attach all\n");
fprintf(stderr, " processes running on the terminal.\n");
fprintf(stderr, " -h Print this help message and exit.\n");
fprintf(stderr, " -v Print the version number and exit.\n");
fprintf(stderr, " -V Print verbose debug output.\n");
}
int main(int argc, char **argv) {
struct termios saved_termios;
int pty;
int opt;
int err;
int do_attach = 1;
int force_stdio = 0;
int do_steal = 0;
int unattached_script_redirection = 0;
while ((opt = getopt(argc, argv, "hlLsTvV")) != -1) {
switch (opt) {
case 'h':
usage(argv[0]);
return 0;
case 'l':
do_attach = 0;
break;
case 'L':
do_attach = 0;
unattached_script_redirection = 1;
break;
case 's':
force_stdio = 1;
break;
case 'T':
do_steal = 1;
break;
case 'v':
printf("This is reptyr version %s.\n", REPTYR_VERSION);
printf(" by Nelson Elhage <[email protected]>\n");
printf("http://github.com/nelhage/reptyr/\n");
return 0;
case 'V':
verbose = 1;
break;
default:
usage(argv[0]);
return 1;
}
if (opt == 'l' || opt == 'L') break; // the rest is a command line
}
if (do_attach && optind >= argc) {
fprintf(stderr, "%s: No pid specified to attach\n", argv[0]);
usage(argv[0]);
return 1;
}
if (!do_steal) {
if ((pty = get_pt()) < 0)
die("Unable to allocate a new pseudo-terminal: %m");
if (unlockpt(pty) < 0)
die("Unable to unlockpt: %m");
if (grantpt(pty) < 0)
die("Unable to grantpt: %m");
}
if (do_attach) {
char *endptr = NULL;
errno = 0;
long t = strtol(argv[optind], &endptr, 10);
if (errno == ERANGE)
die("Invalid pid: %m");
if (*endptr)
die("Invalid pid: must be integer");
/* check for overflow/underflow */
pid_t child = (pid_t)t;
if (child < t || t < 1) /* pids can't be < 1, so no *real* underflow check */
die("Invalid pid: %s", strerror(ERANGE));
if (do_steal) {
err = steal_pty(child, &pty);
} else {
err = attach_child(child, ptsname(pty), force_stdio);
}
if (err) {
fprintf(stderr, "Unable to attach to pid %d: %s\n", child, strerror(err));
if (err == EPERM) {
check_ptrace_scope();
}
return 1;
}
} else {
printf("Opened a new pty: %s\n", ptsname(pty));
fflush(stdout);
if (argc > 2) {
if (!fork()) {
setenv("REPTYR_PTY", ptsname(pty), 1);
if (unattached_script_redirection) {
int f;
setpgid(0, getppid());
setsid();
f = open(ptsname(pty), O_RDONLY, 0);
dup2(f, 0);
close(f);
f = open(ptsname(pty), O_WRONLY, 0);
dup2(f, 1);
dup2(f, 2);
close(f);
}
close(pty);
execvp(argv[2], argv + 2);
exit(1);
}
}
}
setup_raw(&saved_termios);
do_proxy(pty);
do {
errno = 0;
if (tcsetattr(0, TCSANOW, &saved_termios) && errno != EINTR)
die("Unable to tcsetattr: %m");
} while (errno == EINTR);
return 0;
}