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

telgate.cpp 7.25 KB
Newer Older
1 2 3 4 5 6
/* Synchronet telnet gateway routines */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *																			*
 * 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										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"
#include "telnet.h" 

25
void sbbs_t::telnet_gate(char* destaddr, ulong mode, char* client_user_name, char* server_user_name, char* term_type)
26 27 28 29 30
{
	char*	p;
	uchar	buf[512];
	int		i;
	int		rd;
31
	uint	attempts;
32 33
	ulong	l;
	bool	gotline;
34
	ushort	port;
35 36 37 38 39
	ulong	ip_addr;
	ulong	save_console;
	SOCKET	remote_socket;
	SOCKADDR_IN	addr;

40 41 42 43 44
	if(mode&TG_RLOGIN)
		port=513;
	else
		port=IPPORT_TELNET;

45 46 47 48 49 50 51
	p=strchr(destaddr,':');
	if(p!=NULL) {
		*p=0;
		port=atoi(p+1);
	}

	ip_addr=resolve_ip(destaddr);
52
	if(ip_addr==INADDR_NONE) {
53
		lprintf(LOG_NOTICE,"!TELGATE Failed to resolve address: %s",destaddr);
rswindell's avatar
rswindell committed
54
		bprintf("!Failed to resolve address: %s\r\n",destaddr);
55 56 57
		return;
	}

58
    if((remote_socket = open_socket(PF_INET, SOCK_STREAM, client.protocol)) == INVALID_SOCKET) {
59 60 61 62 63
		errormsg(WHERE,ERR_OPEN,"socket",0);
		return;
	}

	memset(&addr,0,sizeof(addr));
deuce's avatar
deuce committed
64
	addr.sin_addr.s_addr = htonl(startup->outgoing4.s_addr);
65 66 67
	addr.sin_family = AF_INET;

	if((i=bind(remote_socket, (struct sockaddr *) &addr, sizeof (addr)))!=0) {
68
		lprintf(LOG_NOTICE,"!TELGATE ERROR %d (%d) binding to socket %d",i, ERROR_VALUE, remote_socket);
rswindell's avatar
rswindell committed
69
		bprintf("!ERROR %d (%d) binding to socket\r\n",i, ERROR_VALUE);
rswindell's avatar
rswindell committed
70
		close_socket(remote_socket);
71 72 73 74
		return;
	}

	memset(&addr,0,sizeof(addr));
75
	addr.sin_addr.s_addr = ip_addr;
76 77 78 79
	addr.sin_family = AF_INET;
	addr.sin_port   = htons(port);

	if((i=connect(remote_socket, (struct sockaddr *)&addr, sizeof(addr)))!=0) {
80
		lprintf(LOG_NOTICE,"!TELGATE ERROR %d (%d) connecting to server: %s"
81
			,i,ERROR_VALUE, destaddr);
rswindell's avatar
rswindell committed
82
		bprintf("!ERROR %d (%d) connecting to server: %s\r\n"
83
			,i,ERROR_VALUE, destaddr);
rswindell's avatar
rswindell committed
84
		close_socket(remote_socket);
85 86 87 88 89 90
		return;
	}

	l=1;

	if((i = ioctlsocket(remote_socket, FIONBIO, &l))!=0) {
91
		lprintf(LOG_NOTICE,"!TELGATE ERROR %d (%d) disabling socket blocking"
92
			,i, ERROR_VALUE);
rswindell's avatar
rswindell committed
93
		close_socket(remote_socket);
94 95 96
		return;
	}

rswindell's avatar
rswindell committed
97
	lprintf(LOG_INFO,"Node %d %s gate to %s port %u on socket %d"
98 99 100
		,cfg.node_num
		,mode&TG_RLOGIN ? "RLogin" : "Telnet"
		,destaddr,port,remote_socket);
101 102 103 104

	if(!(mode&TG_CTRLKEYS))
		console|=CON_RAW_IN;

105
	if(mode&TG_RLOGIN) {
106 107 108 109
		if (client_user_name == NULL)
			client_user_name = (mode&TG_RLOGINSWAP) ? useron.name : useron.alias;
		if (server_user_name == NULL)
			server_user_name = (mode&TG_RLOGINSWAP) ? useron.alias : useron.name;
110 111
		p=(char*)buf;
		*(p++)=0;
112
		p+=sprintf(p,"%s",client_user_name);
113
		p++;	// Add NULL
114
		p+=sprintf(p,"%s",server_user_name);
115
		p++;	// Add NULL
116 117 118
		if(term_type!=NULL)
			p+=sprintf(p,"%s",term_type);
		else
deuce's avatar
deuce committed
119
			p+=sprintf(p,"%s/%lu",terminal, cur_rate);
120
		p++;	// Add NULL
121
		l=p-(char*)buf;
122
		(void)sendsocket(remote_socket,(char*)buf,l);
123
		mode|=TG_NOLF;	/* Send LF (to remote host) when Telnet client sends CRLF (when not in binary mode) */
124
	}
125

126 127 128
	/* This is required for gating to Unix telnetd */
	if(mode&TG_NOTERMTYPE)
		request_telnet_opt(TELNET_DONT,TELNET_TERM_TYPE, 3000);	// Re-negotiation of terminal type
129

130 131
	/* Text/NVT mode by default */
	request_telnet_opt(TELNET_DONT,TELNET_BINARY_TX, 3000);
132

133
	if(!(telnet_mode&TELNET_MODE_OFF) && (mode&TG_PASSTHRU))
134
		telnet_mode|=TELNET_MODE_GATE;	// Pass-through telnet commands
135

136
	while(online) {
137 138
		if(!(mode&TG_NOCHKTIME))
			gettimeleft();
rswindell's avatar
rswindell committed
139
		rd=RingBufRead(&inbuf,buf,sizeof(buf)-1);
140
		if(rd) {
141 142 143 144 145 146 147 148 149 150
#if 0
			if(memchr(buf,TELNET_IAC,rd)) {
				char dump[2048];
				dump[0];
				p=dump;
				for(int i=0;i<rd;i++)
					p+=sprintf(p,"%u ",buf[i]);
				lprintf(LOG_DEBUG,"Node %d Telnet cmd from client: %s", cfg.node_num, dump);
			}
#endif
151
			if(telnet_remote_option[TELNET_BINARY_TX]!=TELNET_WILL) {
152
				if(*buf==CTRL_CLOSE_BRACKET) {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
					save_console=console;
					console&=~CON_RAW_IN;	// Allow Ctrl-U/Ctrl-P
					CRLF;
					while(online) {
						SYNC;
						mnemonics("\1n\r\n\1h\1bTelnet Gate: \1y~D\1wisconnect, "
							"\1y~E\1wcho toggle, \1y~L\1wist Users, \1y~P\1wrivate message, "
							"\1y~Q\1wuit: ");
						switch(getkeys("DELPQ",0)) {
							case 'D':
								closesocket(remote_socket);
								break;
							case 'E':
								mode^=TG_ECHO;
								bprintf(text[EchoIsNow]
									,mode&TG_ECHO
									? text[ON]:text[OFF]);
								continue;
							case 'L':
								whos_online(true);
								continue;
							case 'P':
								nodemsg();
								continue;
177
						}
178
						break;
179
					}
180 181 182
					attr(LIGHTGRAY);
					console=save_console;
				}
183
				else if(*buf<' ' && (mode&TG_CTRLKEYS))
184 185
					handle_ctrlkey(*buf, K_NONE);
				gotline=false;
186
				if((mode&TG_LINEMODE) && buf[0]!='\r') {
187 188 189 190 191 192 193
					ungetkey(buf[0]);
					l=K_CHAT;
					if(!(mode&TG_ECHO))
						l|=K_NOECHO;
					rd=getstr((char*)buf,sizeof(buf)-1,l);
					if(!rd)
						continue;
194
					SAFECAT(buf,crlf);
195 196 197
					rd+=2;
					gotline=true;
				}
198
				if((mode&TG_CRLF) && buf[rd-1]=='\r')
199
					buf[rd++]='\n';
200
				else if((mode&TG_NOLF) && buf[rd-1]=='\n')
201 202
					rd--;
				if(!gotline && (mode&TG_ECHO) && rd) {
203
					RingBufWrite(&outbuf,buf,rd);
204
				}
205
			} /* Not Telnet Binary mode */
206 207 208 209 210 211 212 213 214 215 216
			if(rd > 0) {
				for(attempts=0;attempts<60 && online; attempts++) /* added retry loop here, Jan-20-2003 */
				{
					if((i=sendsocket(remote_socket,(char*)buf,rd))>=0)
						break;
					if(ERROR_VALUE!=EWOULDBLOCK)
						break;
					mswait(500);
				} 
				if(i<0) {
					lprintf(LOG_NOTICE,"!TELGATE ERROR %d sending on socket %d",ERROR_VALUE,remote_socket);
217
					break;
218
				}
219 220 221 222
			}
		}
		rd=recv(remote_socket,(char*)buf,sizeof(buf),0);
		if(rd<0) {
223
			if(ERROR_VALUE==EWOULDBLOCK) {
224 225
				if(mode&TG_NODESYNC) {
					SYNC;
226 227 228 229 230
				} else {
					// Check if the node has been interrupted
					getnodedat(cfg.node_num,&thisnode,0);
					if(thisnode.misc&NODE_INTR)
						break;
231
				}
232
				YIELD();
233 234
				continue;
			}
235
			lprintf(LOG_NOTICE,"!TELGATE ERROR %d receiving on socket %d",ERROR_VALUE,remote_socket);
236 237 238
			break;
		}
		if(!rd) {
239
			lprintf(LOG_INFO,"Node %d Telnet gate disconnected",cfg.node_num);
240 241
			break;
		}
242 243 244 245 246 247 248
#if 0
		if(memchr(buf,TELNET_IAC,rd)) {
			char dump[2048];
			dump[0];
			p=dump;
			for(int i=0;i<rd;i++)
				p+=sprintf(p,"%u ",buf[i]);
249
			lprintf(LOG_DEBUG,"Node %d Telnet cmd from server: %s", cfg.node_num, dump);
250 251
		}
#endif
252 253 254 255 256
		RingBufWrite(&outbuf,buf,rd);
	}
	console&=~CON_RAW_IN;
	telnet_mode&=~TELNET_MODE_GATE;

257 258
	/* Disable Telnet Terminal Echo */
	request_telnet_opt(TELNET_WILL,TELNET_ECHO);
259 260 261

	close_socket(remote_socket);

262
	lprintf(LOG_INFO,"Node %d Telnet gate to %s finished",cfg.node_num,destaddr);
263
}