Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

spyon.c 4.83 KB
Newer Older
1 2 3 4
/* spyon.c */

/* Synchronet for *nix node spy */

5
/* $Id: spyon.c,v 1.16 2020/06/30 18:30:40 deuce Exp $ */
6 7 8 9 10

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
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
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

deuce's avatar
deuce committed
38
#include <stdlib.h>
39
#include "sockwrap.h"	/* Must go before <sys/un.h> */
deuce's avatar
deuce committed
40
#ifndef _WIN32
41
#include <sys/un.h>
deuce's avatar
deuce committed
42
#endif
43 44
#include <stdio.h>
#include <string.h>
deuce's avatar
deuce committed
45 46 47

#include <genwrap.h>
#include <sockwrap.h>
48
#include "spyon.h"
49
#include "ciolib.h"
deuce's avatar
deuce committed
50
#include "cterm.h"
deuce's avatar
deuce committed
51
#include "uifc.h"
deuce's avatar
deuce committed
52 53

struct cterminal *cterm;
deuce's avatar
deuce committed
54
extern uifcapi_t uifc; /* User Interface (UIFC) Library API */
55 56

int spyon(char *sockname)  {
deuce's avatar
deuce committed
57 58 59 60
#if defined _WIN32
	uifc.msg("Spying not supported on Win32 yet!");
	return SPY_SOCKETLOST;
#else
61 62 63
	SOCKET		spy_sock=INVALID_SOCKET;
	struct sockaddr_un spy_name;
	socklen_t	spy_len;
64 65
	unsigned char		key;
	unsigned char		buf;
66 67 68 69 70
	int		i;
	fd_set	rd;
	BOOL	b;
	int		retval=0;
	int		telnet_strip=0;
71 72
	struct text_info ti;
	char *scrn;
73 74 75 76

	/* ToDo Test for it actually being a socket! */
	/* Well, it will fail to connect won't it?   */

deuce's avatar
deuce committed
77
	if((spy_sock=socket(PF_UNIX,SOCK_STREAM,0))==INVALID_SOCKET)  {
78 79 80 81
		printf("ERROR %d creating local spy socket", errno);
		return(SPY_NOSOCKET);
	}
	
deuce's avatar
deuce committed
82
	spy_name.sun_family=AF_UNIX;
83
	SAFECOPY(spy_name.sun_path,sockname);
84
#ifdef SUN_LEN
85
	spy_len=SUN_LEN(&spy_name);
86 87 88
#else
	spy_len=sizeof(struct sockaddr_un);
#endif
89 90 91 92 93
	if(connect(spy_sock,(struct sockaddr *)&spy_name,spy_len))  {
		return(SPY_NOCONNECT);
	}
	i=1;

94
	gettextinfo(&ti);
95
	scrn=(char *)alloca(ti.screenwidth*ti.screenheight*2);
96 97 98 99 100 101 102
	gettext(1,1,ti.screenwidth,ti.screenheight,scrn);
	textcolor(YELLOW);
	textbackground(BLUE);
	clrscr();
	gotoxy(1,ti.screenheight);
	cputs("Local spy mode... press CTRL-C to return to monitor");
	clreol();
103
	cterm = cterm_init(ti.screenheight - 1, ti.screenwidth, 1, 1, 0, 0, NULL, CTERM_EMULATION_ANSI_BBS);
deuce's avatar
deuce committed
104
	while(spy_sock!=INVALID_SOCKET && cterm != NULL)  {
105 106 107
		struct timeval tv;
		tv.tv_sec=0;
		tv.tv_usec=100;
108 109
		FD_ZERO(&rd);
		FD_SET(spy_sock,&rd);
110
		if((select(spy_sock+1,&rd,NULL,NULL,&tv))<0)  {
111 112
			close(spy_sock);
			spy_sock=INVALID_SOCKET;
113
			retval=SPY_SELECTFAILED;
114 115 116 117 118 119 120 121 122 123 124 125
			break;
		}
		if(!socket_check(spy_sock,NULL,&b,0)) {
			close(spy_sock);
			spy_sock=INVALID_SOCKET;
			retval=SPY_SOCKETLOST;
			break;
		}
		if(spy_sock != INVALID_SOCKET && FD_ISSET(spy_sock,&rd))  {
			if((i=read(spy_sock,&buf,1))==1)  {
				if(telnet_strip) {
					telnet_strip++;
126
					if(buf==255 && telnet_strip==2) {
127
						telnet_strip=0;
deuce's avatar
deuce committed
128
						cterm_write(cterm, &buf,1,NULL,0,NULL);
129 130 131 132 133
					}
					if(telnet_strip==3)
						telnet_strip=0;
				}
				else
134
					if(buf==255)
135 136
						telnet_strip=1;
					else
deuce's avatar
deuce committed
137
						cterm_write(cterm, &buf,1,NULL,0,NULL);
138 139 140 141 142 143 144 145
			}
			else if(i<0) {
				close(spy_sock);
				spy_sock=INVALID_SOCKET;
				retval=SPY_SOCKETLOST;
				break;
			}
		}
146 147 148 149 150
		if(kbhit())  {
			key=getch();
			/* Check for control keys */
			switch(key)  {
				case 0:		/* Extended keys */
151
				case 0xe0:
152 153 154 155 156 157 158 159 160 161 162
					getch();
					break;
				case 3:	/* CTRL-C */
					close(spy_sock);
					spy_sock=INVALID_SOCKET;
					retval=SPY_CLOSED;
					break;
				default:
					write(spy_sock,&key,1);
			}
		}
163
	}
164
	cterm_end(cterm, 1);
165 166 167 168
	puttext(1,1,ti.screenwidth,ti.screenheight,scrn);
	window(ti.winleft,ti.wintop,ti.winright,ti.winbottom);
	textattr(ti.attribute);
	gotoxy(ti.curx,ti.cury);
169
	return(retval);
deuce's avatar
deuce committed
170
#endif
171 172
}