This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ttyspy.c
383 lines (329 loc) · 10.3 KB
/
ttyspy.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
#include <pwd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <syslog.h>
#include <termios.h>
#include <unistd.h>
#ifdef HAVE_UTIL_H
#include <util.h>
#endif
#ifdef HAVE_UTMP_H
#include <utmp.h>
#endif
#include "config.h"
#include "ttyspy_protocol.h"
static void usage() __attribute__ ((noreturn));
static void sig_handler(int);
static struct TTYSpyRequest *build_request();
static int open_ttyspy_session(const char *);
static ssize_t write_all(int, const void *, size_t);
static void exec_shell_or_command(const char *, int , char *[]) __attribute__ ((noreturn));
/* global so we can pass along window size change from signal handler: */
static int master;
int
main(int argc, char *argv[]) {
int ch;
const char *config_file = NULL;
while ((ch = getopt(argc, argv, "c:")) != -1) {
switch (ch) {
case 'c':
config_file = optarg;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
/* Read configuration */
struct Config *config = load_config(config_file);
if (config == NULL)
return 1;
/* Gather user info */
struct passwd *user = getpwuid(getuid());
if (user == NULL) {
perror(PACKAGE ": getpwuid");
return 1;
}
/* Skip ttyspy for root */
if (user->pw_uid == 0)
exec_shell_or_command(user->pw_shell, argc, argv);
char *ident = NULL;
int result = asprintf(&ident, "%s (%s)", PACKAGE, user->pw_name);
if (result < 0) {
perror("asprintf");
return 1;
}
openlog(ident, LOG_PID, LOG_DAEMON);
if (!isatty(STDIN_FILENO)) {
syslog(LOG_NOTICE, "STDIN is not a tty");
int ttyspyd_sock = open_ttyspy_session(config->socket);
FILE *session = fdopen(ttyspyd_sock, "w");
fprintf(session, "Non tty session initiated.\n");
fprintf(session, "SSH_ORIGINAL_COMMAND=%s\n", getenv("SSH_ORIGINAL_COMMAND"));
fclose(session);
exec_shell_or_command(user->pw_shell, argc, argv);
}
/* Get terminal settings */
struct termios term;
result = tcgetattr(STDIN_FILENO, &term);
if (result < 0) {
syslog(LOG_INFO, "tcgetattr: %s", strerror(errno));
}
struct winsize win;
result = ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
if (result < 0) {
syslog(LOG_INFO, "ioctl TIOCGWINSZ: %s", strerror(errno));
}
/* Allocate a PTY */
int slave = -1;
int ret = openpty(&master, &slave, NULL, &term, &win);
if (ret < 0) {
syslog(LOG_ERR, "openpty: %s", strerror(errno));
/* exec shell */
syslog(LOG_ERR, "Unable to log current session, falling back to unlogged sesssion");
exec_shell_or_command(user->pw_shell, argc, argv);
}
pid_t pid = fork();
if (pid < 0) {
syslog(LOG_ERR, "fork: %s", strerror(errno));
close(slave);
close(master);
/* exec shell */
syslog(LOG_ERR, "Unable to log current session, falling back to unlogged sesssion");
exec_shell_or_command(user->pw_shell, argc, argv);
} else if (pid == 0) {
/* Child */
/* Setup file descriptors */
close(master);
if (login_tty(slave) < 0) {
syslog(LOG_ERR, "login_tty: %s", strerror(errno));
exit(1);
}
/* exec shell */
exec_shell_or_command(user->pw_shell, argc, argv);
} else {
/* Parent */
close(slave);
int ttyspyd_sock = open_ttyspy_session(config->socket);
/* Set local terminal to raw mode */
struct termios rawterm = term;
cfmakeraw(&rawterm);
rawterm.c_lflag &= ~ECHO;
/* rawterm.cflags &= ~ECHO; */
tcsetattr(STDIN_FILENO, TCSAFLUSH, &rawterm);
/* Install signal handler */
struct sigaction sa = {
.sa_handler = sig_handler,
.sa_flags = SA_RESTART,
};
sigemptyset(&sa.sa_mask);
sigaction(SIGWINCH, &sa, NULL);
sigaction(SIGCHLD, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);
for (;;) {
fd_set rfds;
fd_set efds;
char buffer[256];
FD_ZERO(&rfds);
FD_ZERO(&efds);
FD_SET(STDIN_FILENO, &rfds);
FD_SET(STDIN_FILENO, &efds);
FD_SET(master, &rfds);
FD_SET(master, &efds);
result = select(master + 1, &rfds, NULL, &efds, NULL);
if (result < 0 && errno == EINTR) /* received signal */
continue;
if (result < 0) {
syslog(LOG_INFO, "select: %s", strerror(errno));
break;
}
if (FD_ISSET(STDIN_FILENO, &efds)) {
break;
}
if (FD_ISSET(master, &efds)) {
break;
}
if (FD_ISSET(STDIN_FILENO, &rfds)) {
ssize_t len = read(STDIN_FILENO, buffer, sizeof(buffer));
if (len < 0) {
syslog(LOG_INFO, "read STDIN: %s", strerror(errno));
break;
}
result = write_all(master, buffer, len);
/* XXX ignored */
}
if (FD_ISSET(master, &rfds)) {
ssize_t len = read(master, buffer, sizeof(buffer));
if (len < 0) {
syslog(LOG_INFO, "read pty: %s", strerror(errno));
break;
}
result = write_all(STDOUT_FILENO, buffer, len);
/* XXX ignored */
result = write_all(ttyspyd_sock, buffer, len);
/* XXX ignored */
}
}
/* Reset terminal */
tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
syslog(LOG_INFO, "exiting...");
}
return 0;
}
static void
usage() {
fprintf(stderr, PACKAGE ": ttyspy client\nusage: ttyspy [-c <config_file>]\n");
exit(1);
}
static void
sig_handler(int signo) {
struct winsize win;
int result;
int status;
pid_t pid;
switch (signo) {
case SIGWINCH:
result = ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
if (result < 0) {
syslog(LOG_INFO, "ioctl TIOCGWINSZ: %s", strerror(errno));
break;
}
ioctl(master, TIOCSWINSZ, &win);
break;
case SIGCHLD:
do {
pid = waitpid(-1, &status, WNOHANG);
if (pid > 0)
syslog(LOG_INFO, "child [%d] terminated with %d\n", pid, status);
if (pid < 0 && errno != ECHILD)
syslog(LOG_ERR, "waitpid: %s", strerror(errno));
} while (pid > 0);
break;
case SIGPIPE:
/* noop */
break;
default:
syslog(LOG_NOTICE, "received signal %d\n", signo);
break;
}
}
static struct TTYSpyRequest *
build_request() {
struct TTYSpyRequest *req = malloc(sizeof(struct TTYSpyRequest));
if (req != NULL) {
memset(req, 0, sizeof(struct TTYSpyRequest));
char *login_tty = getenv("SSH_TTY");
if (login_tty != NULL)
strlcpy(req->login_tty, login_tty, sizeof(req->login_tty));
char *ssh_client = getenv("SSH_CLIENT");
if (ssh_client != NULL)
strlcpy(req->ssh_client, ssh_client, sizeof(req->ssh_client));
}
return req;
}
static int
open_ttyspy_session(const char *sock_path) {
struct TTYSpyRequest *req = build_request();
if (req == NULL) {
syslog(LOG_ERR, "build_request: %s", strerror(errno));
return -1;
}
int sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_fd < 0) {
syslog(LOG_ERR, "socket: %s", strerror(errno));
return -1;
}
struct sockaddr_un un;
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
int path_len = strlcpy(un.sun_path, sock_path, sizeof(un.sun_path));
if (path_len >= sizeof(un.sun_path)) {
syslog(LOG_INFO, "Socket path is tool long!");
return -1;
}
int size = offsetof(struct sockaddr_un, sun_path) + path_len;
if (connect(sock_fd, (struct sockaddr *)&un, size) < 0) {
syslog(LOG_ERR, "Unable to connect to %s: %s", sock_path, strerror(errno));
return -1;
}
/* transmit ttyspy request */
if (write_all(sock_fd, req, sizeof(struct TTYSpyRequest)) < 0) {
syslog(LOG_INFO, "write: %s", strerror(errno));
return -1;
}
return sock_fd;
}
static ssize_t
write_all(int fd, const void *src, size_t nbyte) {
size_t bytes_written = 0;
const char *pos = (const char *)src;
while (bytes_written < nbyte) {
ssize_t result = write(fd, pos, nbyte - bytes_written);
if (result < 0) {
return result;
}
pos += result;
bytes_written += result;
}
return 0;
}
static void
exec_shell_or_command(const char *shell, int argc, char *argv[]) {
char **new_argv = NULL;
const char *ssh_orig_cmd = getenv("SSH_ORIGINAL_COMMAND");
/* For debugging
if (1) {
printf("args = {");
for (int i = 0; i < argc; i++) {
if (i > 0)
printf(", ");
printf("\"%s\"", argv[i]);
}
printf("}\n");
}
*/
if (argc > 0) {
/* If additional filter specified as argument exec it passing the
* remaining arguments */
new_argv = argv;
} else if (ssh_orig_cmd != NULL) {
/* If an SSH command was specified run it */
new_argv = malloc(4 * sizeof(char *));
if (new_argv != NULL) {
new_argv[0] = (char *)shell;
new_argv[1] = "-c";
new_argv[2] = (char *)ssh_orig_cmd;
new_argv[3] = NULL;
}
} else {
/* Otherwise spawn the user's login shell */
new_argv = malloc(3 * sizeof(char *));
if (new_argv != NULL) {
new_argv[0] = (char *)shell;
new_argv[1] = "-l";
new_argv[2] = NULL;
}
}
if (new_argv == NULL) {
perror("malloc new argv");
exit(1);
}
execv(new_argv[0], new_argv);
fprintf(stderr, "Unable to exec %s: %s\nA fully qualified path is required.\n", new_argv[0], strerror(errno));
exit(1);
}