File: ttysnoop.c

package info (click to toggle)
ttysnoop 0.12d-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 396 kB
  • ctags: 460
  • sloc: ansic: 5,419; makefile: 172
file content (132 lines) | stat: -rw-r--r-- 2,882 bytes parent folder | download | duplicates (2)
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
/*
	ttysnoop.c
	
	ttysnoop is the client to ttysnoops. it can only be used on ptys
	that are snooped via a socket 'device' in /etc/snooptab.
	
	v0.00	9-1-94	Carl Declerck	- first version
	v0.10	8-8-94	     ""		- bug fixes
	v0.11	9-8-94       ""		- pwd authentication
	v0.12	6-9-94	     ""		- see ttysnoops.c
*/

#define _BSD_SOURCE	/* SUN_LEN from <sys/un.h> */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <grp.h>
#include <utmp.h>
#include <errno.h>
#include <signal.h>

#include "config.h"
#include "common.h"

#define BUFF_SIZE	256

char buff[BUFF_SIZE];

void message (void)
{
	printf ("\r\nBack at local tty.\r\n");
}

int main (int argc, char *argv[])
{
	fd_set readset;
	struct sockaddr_un sock_addr;
	int sockfd, fdmax, quit = 0, n;
	char sockname[128];
	
	if (geteuid())
		errorf("Insufficient privileges.\n");

	if (argc < 2)
		errorf ("Usage: ttysnoop <pty>\n");
	
	/* create the client socket */
	
	if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
		errorf ("Cannot create client socket.\n");
	
	snprintf (sockname, sizeof(sockname), "%s/%s", SPOOLDIR, argv[1]);
	memset (&sock_addr, '\0', sizeof(sock_addr));
	sock_addr.sun_family = AF_UNIX;
	strncpy (sock_addr.sun_path, sockname, sizeof(sock_addr.sun_path) - 1);

	if (connect(sockfd, (struct sockaddr *) &sock_addr, SUN_LEN(&sock_addr)) < 0)
		errorf ("Cannot connect to server. Is it really a valid socket?\n");
	
	/* put stdin into raw mode */
	
	stty_initstore ();
	atexit (stty_orig);
	atexit (message);
	if (isatty(STDIN_FILENO))
		stty_raw (STDIN_FILENO);

	/* calc max file descriptor for select() */
	
	fdmax = max(STDIN_FILENO, sockfd);
	
	/* do our thing */
	
	signal(SIGPIPE, SIG_IGN);
	FD_ZERO (&readset);

	while (!quit)
	{
		int ret;

		FD_SET (STDIN_FILENO, &readset);
		FD_SET (sockfd, &readset);
		
		if ( (ret = select (fdmax + 1, &readset, NULL, NULL, NULL)) < 0 ) {
			if ( errno == EINTR )
				continue;

			break;	/* Cannot recover. */
		}
		
		/* Give precedence to the end point where we snoop. */
		if (FD_ISSET(sockfd, &readset))
		{
			if ((n = read(sockfd, buff, BUFF_SIZE)) <= 0) {
				if ( (n < 0) && (errno == EINTR) )
					continue;
				if ( n == 0 )
					break;	/* EOF on socket. */

				quit = 1;	/* Will soon exit! */
			}
			if (n > 0)
				write (STDOUT_FILENO, buff, n);
		}
		
		if (FD_ISSET(STDIN_FILENO, &readset))
		{
			n = read(STDIN_FILENO, buff, BUFF_SIZE);
			if ( n == 0 ) {
				/* The observer has left. Close down! */
				shutdown(sockfd, SHUT_WR);
				FD_CLR(STDIN_FILENO, &readset);
				quit = 1;
			}

			if ( ( n > 0 ) && ( write(sockfd, buff, n) < 0 ) )
				quit = 1;	/* The terminal service has already closed down. */
		}
	}
	
	return 0;
}