2006-04-23 18:26:03 +00:00
|
|
|
/*
|
|
|
|
* Rlogin backend.
|
|
|
|
*/
|
|
|
|
|
2001-01-19 10:10:37 +00:00
|
|
|
#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;
|
2003-01-14 18:43:45 +00:00
|
|
|
int firstbyte;
|
2005-06-14 14:48:17 +00:00
|
|
|
int cansize;
|
2002-10-25 11:30:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-01-16 14:29:34 +00:00
|
|
|
static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
|
|
|
|
const char *error_msg, int error_code)
|
|
|
|
{
|
|
|
|
Rlogin rlogin = (Rlogin) plug;
|
|
|
|
char addrbuf[256], *msg;
|
|
|
|
|
|
|
|
sk_getaddr(addr, addrbuf, lenof(addrbuf));
|
|
|
|
|
|
|
|
if (type == 0)
|
|
|
|
msg = dupprintf("Connecting to %s port %d", addrbuf, port);
|
|
|
|
else
|
|
|
|
msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
|
|
|
|
|
|
|
|
logevent(rlogin->frontend, msg);
|
|
|
|
}
|
|
|
|
|
2003-05-04 14:18:18 +00:00
|
|
|
static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
|
2001-05-06 14:35:20 +00:00
|
|
|
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;
|
2004-11-27 13:20:21 +00:00
|
|
|
notify_remote_exit(rlogin->frontend);
|
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-10-26 12:58:13 +00:00
|
|
|
logevent(rlogin->frontend, error_msg);
|
2003-05-04 14:14:10 +00:00
|
|
|
connection_fatal(rlogin->frontend, "%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--;
|
2005-06-14 14:48:17 +00:00
|
|
|
if (c == '\x80') {
|
|
|
|
rlogin->cansize = 1;
|
2002-10-25 11:30:33 +00:00
|
|
|
rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
|
2005-06-14 14:48:17 +00:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2003-01-14 18:43:45 +00:00
|
|
|
if (rlogin->firstbyte) {
|
2001-05-06 14:35:20 +00:00
|
|
|
if (data[0] == '\0') {
|
|
|
|
data++;
|
|
|
|
len--;
|
|
|
|
}
|
2003-01-14 18:43:45 +00:00
|
|
|
rlogin->firstbyte = 0;
|
2001-05-06 14:35:20 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2003-05-04 14:18:18 +00:00
|
|
|
static const char *rlogin_init(void *frontend_handle, void **backend_handle,
|
|
|
|
Config *cfg,
|
|
|
|
char *host, int port, char **realhost,
|
2004-06-20 17:07:38 +00:00
|
|
|
int nodelay, int keepalive)
|
2001-05-06 14:35:20 +00:00
|
|
|
{
|
2002-10-25 11:30:33 +00:00
|
|
|
static const struct plug_function_table fn_table = {
|
2005-01-16 14:29:34 +00:00
|
|
|
rlogin_log,
|
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;
|
2003-05-04 14:18:18 +00:00
|
|
|
const char *err;
|
2002-10-25 11:30:33 +00:00
|
|
|
Rlogin rlogin;
|
2001-01-19 10:10:37 +00:00
|
|
|
|
2003-03-29 16:14:26 +00:00
|
|
|
rlogin = snew(struct rlogin_tag);
|
2002-10-25 11:30:33 +00:00
|
|
|
rlogin->fn = &fn_table;
|
|
|
|
rlogin->s = NULL;
|
|
|
|
rlogin->frontend = frontend_handle;
|
2003-01-12 14:48:29 +00:00
|
|
|
rlogin->term_width = cfg->width;
|
|
|
|
rlogin->term_height = cfg->height;
|
2003-01-14 18:43:45 +00:00
|
|
|
rlogin->firstbyte = 1;
|
2005-06-14 14:48:17 +00:00
|
|
|
rlogin->cansize = 0;
|
2002-10-25 11:30:33 +00:00
|
|
|
*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
|
|
|
{
|
2002-11-07 19:49:03 +00:00
|
|
|
char *buf;
|
2004-12-30 16:45:11 +00:00
|
|
|
buf = dupprintf("Looking up host \"%s\"%s", host,
|
|
|
|
(cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
|
|
|
|
(cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
|
|
|
|
"")));
|
2002-10-26 12:58:13 +00:00
|
|
|
logevent(rlogin->frontend, buf);
|
2002-11-07 19:49:03 +00:00
|
|
|
sfree(buf);
|
2001-09-07 22:39:01 +00:00
|
|
|
}
|
2004-12-30 16:45:11 +00:00
|
|
|
addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
|
2003-08-07 16:04:33 +00:00
|
|
|
if ((err = sk_addr_error(addr)) != NULL) {
|
|
|
|
sk_addr_free(addr);
|
2001-01-19 10:10:37 +00:00
|
|
|
return err;
|
2003-08-07 16:04:33 +00:00
|
|
|
}
|
2001-01-19 10:10:37 +00:00
|
|
|
|
|
|
|
if (port < 0)
|
|
|
|
port = 513; /* default rlogin port */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open socket.
|
|
|
|
*/
|
2002-10-25 11:30:33 +00:00
|
|
|
rlogin->s = new_connection(addr, *realhost, port, 1, 0,
|
2004-06-20 17:07:38 +00:00
|
|
|
nodelay, keepalive, (Plug) rlogin, cfg);
|
2003-01-04 16:21:17 +00:00
|
|
|
if ((err = sk_socket_error(rlogin->s)) != NULL)
|
2001-01-19 10:10:37 +00:00
|
|
|
return err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2003-01-12 14:48:29 +00:00
|
|
|
sk_write(rlogin->s, cfg->localusername,
|
|
|
|
strlen(cfg->localusername));
|
2002-10-25 11:30:33 +00:00
|
|
|
sk_write(rlogin->s, &z, 1);
|
2003-01-12 14:48:29 +00:00
|
|
|
sk_write(rlogin->s, cfg->username,
|
|
|
|
strlen(cfg->username));
|
2002-10-25 11:30:33 +00:00
|
|
|
sk_write(rlogin->s, &z, 1);
|
2003-01-12 14:48:29 +00:00
|
|
|
sk_write(rlogin->s, cfg->termtype,
|
|
|
|
strlen(cfg->termtype));
|
2002-10-25 11:30:33 +00:00
|
|
|
sk_write(rlogin->s, "/", 1);
|
2003-03-11 09:30:31 +00:00
|
|
|
for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
|
2003-01-12 14:48:29 +00:00
|
|
|
sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
|
2002-10-25 11:30:33 +00:00
|
|
|
rlogin->bufsize = sk_write(rlogin->s, &z, 1);
|
2001-01-19 10:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-01-15 23:30:21 +00:00
|
|
|
static void rlogin_free(void *handle)
|
|
|
|
{
|
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
|
|
|
|
|
|
|
if (rlogin->s)
|
|
|
|
sk_close(rlogin->s);
|
|
|
|
sfree(rlogin);
|
|
|
|
}
|
|
|
|
|
2003-01-12 14:48:29 +00:00
|
|
|
/*
|
|
|
|
* Stub routine (we don't have any need to reconfigure this backend).
|
|
|
|
*/
|
|
|
|
static void rlogin_reconfig(void *handle, Config *cfg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-01-19 10:10:37 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2005-06-14 14:48:17 +00:00
|
|
|
if (rlogin->s == NULL || !rlogin->cansize)
|
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;
|
|
|
|
}
|
|
|
|
|
2003-04-04 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* Return a list of the special codes that make sense in this
|
|
|
|
* protocol.
|
|
|
|
*/
|
|
|
|
static const struct telnet_special *rlogin_get_specials(void *handle)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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-30 17:57:31 +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-30 17:57:31 +00:00
|
|
|
/* Rlogin rlogin = (Rlogin) handle; */
|
2001-01-24 14:08:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-26 10:16:19 +00:00
|
|
|
static void rlogin_provide_ldisc(void *handle, void *ldisc)
|
|
|
|
{
|
|
|
|
/* This is a stub. */
|
|
|
|
}
|
|
|
|
|
2002-10-26 12:58:13 +00:00
|
|
|
static void rlogin_provide_logctx(void *handle, void *logctx)
|
|
|
|
{
|
|
|
|
/* This is a stub. */
|
|
|
|
}
|
|
|
|
|
2002-10-25 11:30:33 +00:00
|
|
|
static int rlogin_exitcode(void *handle)
|
2001-12-29 15:31:42 +00:00
|
|
|
{
|
2003-03-31 12:10:08 +00:00
|
|
|
Rlogin rlogin = (Rlogin) handle;
|
|
|
|
if (rlogin->s != NULL)
|
|
|
|
return -1; /* still connected */
|
|
|
|
else
|
|
|
|
/* If we ever implement RSH, we'll probably need to do this properly */
|
|
|
|
return 0;
|
2001-12-29 15:31:42 +00:00
|
|
|
}
|
|
|
|
|
2004-12-29 12:32:25 +00:00
|
|
|
/*
|
|
|
|
* cfg_info for rlogin does nothing at all.
|
|
|
|
*/
|
|
|
|
static int rlogin_cfg_info(void *handle)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-19 10:10:37 +00:00
|
|
|
Backend rlogin_backend = {
|
|
|
|
rlogin_init,
|
2003-01-15 23:30:21 +00:00
|
|
|
rlogin_free,
|
2003-01-12 14:48:29 +00:00
|
|
|
rlogin_reconfig,
|
2001-01-19 10:10:37 +00:00
|
|
|
rlogin_send,
|
2001-08-25 17:09:23 +00:00
|
|
|
rlogin_sendbuffer,
|
2001-01-19 10:10:37 +00:00
|
|
|
rlogin_size,
|
|
|
|
rlogin_special,
|
2003-04-04 20:21:05 +00:00
|
|
|
rlogin_get_specials,
|
2001-01-19 10:10:37 +00:00
|
|
|
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,
|
2002-10-26 10:16:19 +00:00
|
|
|
rlogin_provide_ldisc,
|
2002-10-26 12:58:13 +00:00
|
|
|
rlogin_provide_logctx,
|
2001-08-25 17:09:23 +00:00
|
|
|
rlogin_unthrottle,
|
2004-12-29 12:32:25 +00:00
|
|
|
rlogin_cfg_info,
|
2001-01-19 10:10:37 +00:00
|
|
|
1
|
|
|
|
};
|