Clarify that '-m' takes a _local_ file.
[u/mdw/putty] / rlogin.c
CommitLineData
c91409da 1#include <stdio.h>
2#include <stdlib.h>
b6c680d4 3#include <ctype.h>
c91409da 4
5#include "putty.h"
6
7#ifndef FALSE
8#define FALSE 0
9#endif
10#ifndef TRUE
11#define TRUE 1
12#endif
13
5471d09a 14#define RLOGIN_MAX_BACKLOG 4096
15
51470298 16typedef struct rlogin_tag {
17 const struct plug_function_table *fn;
18 /* the above field _must_ be first in the structure */
c91409da 19
51470298 20 Socket s;
21 int bufsize;
c85623f9 22 int firstbyte;
51470298 23 int term_width, term_height;
24 void *frontend;
25} *Rlogin;
c91409da 26
51470298 27static void rlogin_size(void *handle, int width, int height);
28
29static void c_write(Rlogin rlogin, char *buf, int len)
32874aea 30{
51470298 31 int backlog = from_backend(rlogin->frontend, 0, buf, len);
32 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
c91409da 33}
34
cbe2d68f 35static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
32874aea 36 int calling_back)
37{
51470298 38 Rlogin rlogin = (Rlogin) plug;
39 if (rlogin->s) {
40 sk_close(rlogin->s);
41 rlogin->s = NULL;
f3ab576e 42 }
7e78000d 43 if (error_msg) {
32874aea 44 /* A socket error has occurred. */
a8327734 45 logevent(rlogin->frontend, error_msg);
971bcc0a 46 connection_fatal(rlogin->frontend, "%s", error_msg);
32874aea 47 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 48 return 0;
49}
50
32874aea 51static int rlogin_receive(Plug plug, int urgent, char *data, int len)
52{
51470298 53 Rlogin rlogin = (Rlogin) plug;
c91409da 54 if (urgent == 2) {
32874aea 55 char c;
56
57 c = *data++;
58 len--;
59 if (c == '\x80')
51470298 60 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
32874aea 61 /*
62 * We should flush everything (aka Telnet SYNCH) if we see
63 * 0x02, and we should turn off and on _local_ flow control
64 * on 0x10 and 0x20 respectively. I'm not convinced it's
65 * worth it...
66 */
8a5fb9e3 67 } else {
32874aea 68 /*
69 * Main rlogin protocol. This is really simple: the first
70 * byte is expected to be NULL and is ignored, and the rest
71 * is printed.
72 */
c85623f9 73 if (rlogin->firstbyte) {
32874aea 74 if (data[0] == '\0') {
75 data++;
76 len--;
77 }
c85623f9 78 rlogin->firstbyte = 0;
32874aea 79 }
2b0c045b 80 if (len > 0)
51470298 81 c_write(rlogin, data, len);
c91409da 82 }
c91409da 83 return 1;
84}
85
3ad9d396 86static void rlogin_sent(Plug plug, int bufsize)
87{
51470298 88 Rlogin rlogin = (Rlogin) plug;
89 rlogin->bufsize = bufsize;
3ad9d396 90}
91
c91409da 92/*
93 * Called to set up the rlogin connection.
94 *
95 * Returns an error message, or NULL on success.
96 *
6e1ebb76 97 * Also places the canonical host name into `realhost'. It must be
98 * freed by the caller.
c91409da 99 */
cbe2d68f 100static const char *rlogin_init(void *frontend_handle, void **backend_handle,
101 Config *cfg,
102 char *host, int port, char **realhost,
103 int nodelay)
32874aea 104{
51470298 105 static const struct plug_function_table fn_table = {
7e78000d 106 rlogin_closing,
3ad9d396 107 rlogin_receive,
108 rlogin_sent
51470298 109 };
c91409da 110 SockAddr addr;
cbe2d68f 111 const char *err;
51470298 112 Rlogin rlogin;
c91409da 113
3d88e64d 114 rlogin = snew(struct rlogin_tag);
51470298 115 rlogin->fn = &fn_table;
116 rlogin->s = NULL;
117 rlogin->frontend = frontend_handle;
86916870 118 rlogin->term_width = cfg->width;
119 rlogin->term_height = cfg->height;
c85623f9 120 rlogin->firstbyte = 1;
51470298 121 *backend_handle = rlogin;
887035a5 122
c91409da 123 /*
124 * Try to find host.
125 */
3ad9d396 126 {
57356d63 127 char *buf;
128 buf = dupprintf("Looking up host \"%s\"", host);
a8327734 129 logevent(rlogin->frontend, buf);
57356d63 130 sfree(buf);
3ad9d396 131 }
e8fa8f62 132 addr = name_lookup(host, port, realhost, cfg);
6f1e7b78 133 if ((err = sk_addr_error(addr)) != NULL)
c91409da 134 return err;
135
136 if (port < 0)
137 port = 513; /* default rlogin port */
138
139 /*
140 * Open socket.
141 */
3ad9d396 142 {
57356d63 143 char *buf, addrbuf[100];
3ad9d396 144 sk_getaddr(addr, addrbuf, 100);
57356d63 145 buf = dupprintf("Connecting to %s port %d", addrbuf, port);
a8327734 146 logevent(rlogin->frontend, buf);
57356d63 147 sfree(buf);
3ad9d396 148 }
51470298 149 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
e8fa8f62 150 nodelay, (Plug) rlogin, cfg);
6f1e7b78 151 if ((err = sk_socket_error(rlogin->s)) != NULL)
c91409da 152 return err;
153
154 sk_addr_free(addr);
155
156 /*
157 * Send local username, remote username, terminal/speed
158 */
159
160 {
32874aea 161 char z = 0;
162 char *p;
51470298 163 sk_write(rlogin->s, &z, 1);
86916870 164 sk_write(rlogin->s, cfg->localusername,
165 strlen(cfg->localusername));
51470298 166 sk_write(rlogin->s, &z, 1);
86916870 167 sk_write(rlogin->s, cfg->username,
168 strlen(cfg->username));
51470298 169 sk_write(rlogin->s, &z, 1);
86916870 170 sk_write(rlogin->s, cfg->termtype,
171 strlen(cfg->termtype));
51470298 172 sk_write(rlogin->s, "/", 1);
e93ed432 173 for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue;
86916870 174 sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed);
51470298 175 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
c91409da 176 }
177
c91409da 178 return NULL;
179}
180
fabd1805 181static void rlogin_free(void *handle)
182{
183 Rlogin rlogin = (Rlogin) handle;
184
185 if (rlogin->s)
186 sk_close(rlogin->s);
187 sfree(rlogin);
188}
189
c91409da 190/*
86916870 191 * Stub routine (we don't have any need to reconfigure this backend).
192 */
193static void rlogin_reconfig(void *handle, Config *cfg)
194{
195}
196
197/*
c91409da 198 * Called to send data down the rlogin connection.
199 */
51470298 200static int rlogin_send(void *handle, char *buf, int len)
32874aea 201{
51470298 202 Rlogin rlogin = (Rlogin) handle;
203
204 if (rlogin->s == NULL)
b5a460cd 205 return 0;
c91409da 206
51470298 207 rlogin->bufsize = sk_write(rlogin->s, buf, len);
5471d09a 208
51470298 209 return rlogin->bufsize;
5471d09a 210}
211
212/*
213 * Called to query the current socket sendability status.
214 */
51470298 215static int rlogin_sendbuffer(void *handle)
5471d09a 216{
51470298 217 Rlogin rlogin = (Rlogin) handle;
218 return rlogin->bufsize;
c91409da 219}
220
221/*
222 * Called to set the size of the window
223 */
51470298 224static void rlogin_size(void *handle, int width, int height)
32874aea 225{
51470298 226 Rlogin rlogin = (Rlogin) handle;
e418595a 227 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
c91409da 228
51470298 229 rlogin->term_width = width;
230 rlogin->term_height = height;
f278d6f8 231
51470298 232 if (rlogin->s == NULL)
cf293dd4 233 return;
f278d6f8 234
51470298 235 b[6] = rlogin->term_width >> 8;
236 b[7] = rlogin->term_width & 0xFF;
237 b[4] = rlogin->term_height >> 8;
238 b[5] = rlogin->term_height & 0xFF;
239 rlogin->bufsize = sk_write(rlogin->s, b, 12);
c91409da 240 return;
241}
242
243/*
244 * Send rlogin special codes.
245 */
51470298 246static void rlogin_special(void *handle, Telnet_Special code)
32874aea 247{
c91409da 248 /* Do nothing! */
249 return;
250}
251
125105d1 252/*
253 * Return a list of the special codes that make sense in this
254 * protocol.
255 */
256static const struct telnet_special *rlogin_get_specials(void *handle)
257{
258 return NULL;
259}
260
51470298 261static Socket rlogin_socket(void *handle)
32874aea 262{
51470298 263 Rlogin rlogin = (Rlogin) handle;
264 return rlogin->s;
32874aea 265}
c91409da 266
51470298 267static int rlogin_sendok(void *handle)
32874aea 268{
68a49acb 269 /* Rlogin rlogin = (Rlogin) handle; */
32874aea 270 return 1;
271}
c91409da 272
51470298 273static void rlogin_unthrottle(void *handle, int backlog)
5471d09a 274{
51470298 275 Rlogin rlogin = (Rlogin) handle;
276 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
5471d09a 277}
278
51470298 279static int rlogin_ldisc(void *handle, int option)
32874aea 280{
68a49acb 281 /* Rlogin rlogin = (Rlogin) handle; */
0965bee0 282 return 0;
283}
284
b9d7bcad 285static void rlogin_provide_ldisc(void *handle, void *ldisc)
286{
287 /* This is a stub. */
288}
289
a8327734 290static void rlogin_provide_logctx(void *handle, void *logctx)
291{
292 /* This is a stub. */
293}
294
51470298 295static int rlogin_exitcode(void *handle)
d8d6c7e5 296{
0da1a790 297 Rlogin rlogin = (Rlogin) handle;
298 if (rlogin->s != NULL)
299 return -1; /* still connected */
300 else
301 /* If we ever implement RSH, we'll probably need to do this properly */
302 return 0;
d8d6c7e5 303}
304
c91409da 305Backend rlogin_backend = {
306 rlogin_init,
fabd1805 307 rlogin_free,
86916870 308 rlogin_reconfig,
c91409da 309 rlogin_send,
5471d09a 310 rlogin_sendbuffer,
c91409da 311 rlogin_size,
312 rlogin_special,
125105d1 313 rlogin_get_specials,
c91409da 314 rlogin_socket,
d8d6c7e5 315 rlogin_exitcode,
c91409da 316 rlogin_sendok,
0965bee0 317 rlogin_ldisc,
b9d7bcad 318 rlogin_provide_ldisc,
a8327734 319 rlogin_provide_logctx,
5471d09a 320 rlogin_unthrottle,
c91409da 321 1
322};