2001-01-19 10:10:37 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-02-05 13:04:00 +00:00
|
|
|
#include <ctype.h>
|
2001-01-19 10:10:37 +00:00
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
|
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
|
|
|
|
2001-08-25 17:09:23 +00:00
|
|
|
#define RLOGIN_MAX_BACKLOG 4096
|
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
typedef struct rlogin_tag {
|
|
|
|
const struct plug_function_table *fn;
|
|
|
|
/* the above field _must_ be first in the structure */
|
2001-01-19 10:10:37 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
Socket s;
|
|
|
|
int bufsize;
|
|
|
|
int term_width, term_height;
|
|
|
|
void *frontend;
|
|
|
|
} *Rlogin;
|
2001-01-19 10:10:37 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static void rlogin_size(void *handle, int width, int height);
|
|
|
|
|
|
|
|
static void c_write(Rlogin rlogin, char *buf, int len)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
int backlog = from_backend(rlogin->frontend, 0, buf, len);
|
|
|
|
sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
|
2001-01-19 10:10:37 +00:00
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
static int rlogin_closing(Plug plug, char *error_msg, int error_code,
|
|
|
|
int calling_back)
|
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) plug;
|
|
|
|
if (rlogin->s) {
|
|
|
|
sk_close(rlogin->s);
|
|
|
|
rlogin->s = NULL;
|
2001-07-31 14:23:21 +00:00
|
|
|
}
|
2001-03-13 10:22:45 +00:00
|
|
|
if (error_msg) {
|
2001-05-06 14:35:20 +00:00
|
|
|
/* A socket error has occurred. */
|
2002-03-23 18:04:27 +00:00
|
|
|
logevent(error_msg);
|
|
|
|
connection_fatal("%s", error_msg);
|
2001-05-06 14:35:20 +00:00
|
|
|
} /* Otherwise, the remote side closed the connection normally. */
|
2001-03-13 10:22:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-06 14:35:20 +00:00
|
|
|
static int rlogin_receive(Plug plug, int urgent, char *data, int len)
|
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) plug;
|
2001-01-19 10:10:37 +00:00
|
|
|
if (urgent == 2) {
|
2001-05-06 14:35:20 +00:00
|
|
|
char c;
|
|
|
|
|
|
|
|
c = *data++;
|
|
|
|
len--;
|
|
|
|
if (c == '\x80')
|
2002-10-25 11:30:33 +00:00
|
|
|
rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
|
2001-05-06 14:35:20 +00:00
|
|
|
/*
|
|
|
|
* We should flush everything (aka Telnet SYNCH) if we see
|
|
|
|
* 0x02, and we should turn off and on _local_ flow control
|
|
|
|
* on 0x10 and 0x20 respectively. I'm not convinced it's
|
|
|
|
* worth it...
|
|
|
|
*/
|
2001-02-01 14:09:00 +00:00
|
|
|
} else {
|
2001-05-06 14:35:20 +00:00
|
|
|
/*
|
|
|
|
* Main rlogin protocol. This is really simple: the first
|
|
|
|
* byte is expected to be NULL and is ignored, and the rest
|
|
|
|
* is printed.
|
|
|
|
*/
|
|
|
|
static int firstbyte = 1;
|
|
|
|
if (firstbyte) {
|
|
|
|
if (data[0] == '\0') {
|
|
|
|
data++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
firstbyte = 0;
|
|
|
|
}
|
2002-03-01 13:17:45 +00:00
|
|
|
if (len > 0)
|
2002-10-25 11:30:33 +00:00
|
|
|
c_write(rlogin, data, len);
|
2001-01-19 10:10:37 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-09-07 22:39:01 +00:00
|
|
|
static void rlogin_sent(Plug plug, int bufsize)
|
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) plug;
|
|
|
|
rlogin->bufsize = bufsize;
|
2001-09-07 22:39:01 +00:00
|
|
|
}
|
|
|
|
|
2001-01-19 10:10:37 +00:00
|
|
|
/*
|
|
|
|
* Called to set up the rlogin connection.
|
|
|
|
*
|
|
|
|
* Returns an error message, or NULL on success.
|
|
|
|
*
|
2001-05-09 14:01:15 +00:00
|
|
|
* Also places the canonical host name into `realhost'. It must be
|
|
|
|
* freed by the caller.
|
2001-01-19 10:10:37 +00:00
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static char *rlogin_init(void *frontend_handle, void **backend_handle,
|
2002-10-22 16:11:33 +00:00
|
|
|
char *host, int port, char **realhost, int nodelay)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
static const struct plug_function_table fn_table = {
|
2001-03-13 10:22:45 +00:00
|
|
|
rlogin_closing,
|
2001-09-07 22:39:01 +00:00
|
|
|
rlogin_receive,
|
|
|
|
rlogin_sent
|
2002-10-25 11:30:33 +00:00
|
|
|
};
|
2001-01-19 10:10:37 +00:00
|
|
|
SockAddr addr;
|
|
|
|
char *err;
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin;
|
2001-01-19 10:10:37 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
rlogin = smalloc(sizeof(*rlogin));
|
|
|
|
rlogin->fn = &fn_table;
|
|
|
|
rlogin->s = NULL;
|
|
|
|
rlogin->frontend = frontend_handle;
|
|
|
|
rlogin->term_width = cfg.width;
|
|
|
|
rlogin->term_height = cfg.height;
|
|
|
|
*backend_handle = rlogin;
|
2002-10-22 16:11:33 +00:00
|
|
|
|
2001-01-19 10:10:37 +00:00
|
|
|
/*
|
|
|
|
* Try to find host.
|
|
|
|
*/
|
2001-09-07 22:39:01 +00:00
|
|
|
{
|
|
|
|
char buf[200];
|
|
|
|
sprintf(buf, "Looking up host \"%.170s\"", host);
|
|
|
|
logevent(buf);
|
|
|
|
}
|
2001-01-19 10:10:37 +00:00
|
|
|
addr = sk_namelookup(host, realhost);
|
2001-05-06 14:35:20 +00:00
|
|
|
if ((err = sk_addr_error(addr)))
|
2001-01-19 10:10:37 +00:00
|
|
|
return err;
|
|
|
|
|
|
|
|
if (port < 0)
|
|
|
|
port = 513; /* default rlogin port */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open socket.
|
|
|
|
*/
|
2001-09-07 22:39:01 +00:00
|
|
|
{
|
|
|
|
char buf[200], addrbuf[100];
|
|
|
|
sk_getaddr(addr, addrbuf, 100);
|
|
|
|
sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
|
|
|
|
logevent(buf);
|
|
|
|
}
|
2002-10-25 11:30:33 +00:00
|
|
|
rlogin->s = new_connection(addr, *realhost, port, 1, 0,
|
|
|
|
nodelay, (Plug) rlogin);
|
|
|
|
if ((err = sk_socket_error(rlogin->s)))
|
2001-01-19 10:10:37 +00:00
|
|
|
return err;
|
|
|
|
|
|
|
|
sk_addr_free(addr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send local username, remote username, terminal/speed
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
2001-05-06 14:35:20 +00:00
|
|
|
char z = 0;
|
|
|
|
char *p;
|
2002-10-25 11:30:33 +00:00
|
|
|
sk_write(rlogin->s, &z, 1);
|
|
|
|
sk_write(rlogin->s, cfg.localusername, strlen(cfg.localusername));
|
|
|
|
sk_write(rlogin->s, &z, 1);
|
|
|
|
sk_write(rlogin->s, cfg.username, strlen(cfg.username));
|
|
|
|
sk_write(rlogin->s, &z, 1);
|
|
|
|
sk_write(rlogin->s, cfg.termtype, strlen(cfg.termtype));
|
|
|
|
sk_write(rlogin->s, "/", 1);
|
2001-05-06 14:35:20 +00:00
|
|
|
for (p = cfg.termspeed; isdigit(*p); p++);
|
2002-10-25 11:30:33 +00:00
|
|
|
sk_write(rlogin->s, cfg.termspeed, p - cfg.termspeed);
|
|
|
|
rlogin->bufsize = sk_write(rlogin->s, &z, 1);
|
2001-01-19 10:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to send data down the rlogin connection.
|
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static int rlogin_send(void *handle, char *buf, int len)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
|
|
|
|
|
|
|
if (rlogin->s == NULL)
|
2001-08-27 15:55:44 +00:00
|
|
|
return 0;
|
2001-01-19 10:10:37 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
rlogin->bufsize = sk_write(rlogin->s, buf, len);
|
2001-08-25 17:09:23 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
return rlogin->bufsize;
|
2001-08-25 17:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to query the current socket sendability status.
|
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static int rlogin_sendbuffer(void *handle)
|
2001-08-25 17:09:23 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
|
|
|
return rlogin->bufsize;
|
2001-01-19 10:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called to set the size of the window
|
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static void rlogin_size(void *handle, int width, int height)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
2001-01-22 17:24:38 +00:00
|
|
|
char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
|
2001-01-19 10:10:37 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
rlogin->term_width = width;
|
|
|
|
rlogin->term_height = height;
|
2002-10-23 12:41:35 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
if (rlogin->s == NULL)
|
2001-09-25 20:07:55 +00:00
|
|
|
return;
|
2002-10-23 12:41:35 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
b[6] = rlogin->term_width >> 8;
|
|
|
|
b[7] = rlogin->term_width & 0xFF;
|
|
|
|
b[4] = rlogin->term_height >> 8;
|
|
|
|
b[5] = rlogin->term_height & 0xFF;
|
|
|
|
rlogin->bufsize = sk_write(rlogin->s, b, 12);
|
2001-01-19 10:10:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send rlogin special codes.
|
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
static void rlogin_special(void *handle, Telnet_Special code)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2001-01-19 10:10:37 +00:00
|
|
|
/* Do nothing! */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static Socket rlogin_socket(void *handle)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
|
|
|
return rlogin->s;
|
2001-05-06 14:35:20 +00:00
|
|
|
}
|
2001-01-19 10:10:37 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static int rlogin_sendok(void *handle)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
2001-05-06 14:35:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2001-01-19 10:10:37 +00:00
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static void rlogin_unthrottle(void *handle, int backlog)
|
2001-08-25 17:09:23 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
|
|
|
sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
|
2001-08-25 17:09:23 +00:00
|
|
|
}
|
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static int rlogin_ldisc(void *handle, int option)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
2001-01-24 14:08:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static int rlogin_exitcode(void *handle)
|
2001-12-29 15:31:42 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
2001-12-29 15:31:42 +00:00
|
|
|
/* If we ever implement RSH, we'll probably need to do this properly */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-19 10:10:37 +00:00
|
|
|
Backend rlogin_backend = {
|
|
|
|
rlogin_init,
|
|
|
|
rlogin_send,
|
2001-08-25 17:09:23 +00:00
|
|
|
rlogin_sendbuffer,
|
2001-01-19 10:10:37 +00:00
|
|
|
rlogin_size,
|
|
|
|
rlogin_special,
|
|
|
|
rlogin_socket,
|
2001-12-29 15:31:42 +00:00
|
|
|
rlogin_exitcode,
|
2001-01-19 10:10:37 +00:00
|
|
|
rlogin_sendok,
|
2001-01-24 14:08:20 +00:00
|
|
|
rlogin_ldisc,
|
2001-08-25 17:09:23 +00:00
|
|
|
rlogin_unthrottle,
|
2001-01-19 10:10:37 +00:00
|
|
|
1
|
|
|
|
};
|