Readjust checklist, because actually the section on updating the
[u/mdw/putty] / rlogin.c
CommitLineData
eaf1e20a 1/*
2 * Rlogin backend.
3 */
4
c91409da 5#include <stdio.h>
6#include <stdlib.h>
b6c680d4 7#include <ctype.h>
c91409da 8
9#include "putty.h"
10
11#ifndef FALSE
12#define FALSE 0
13#endif
14#ifndef TRUE
15#define TRUE 1
16#endif
17
5471d09a 18#define RLOGIN_MAX_BACKLOG 4096
19
51470298 20typedef struct rlogin_tag {
21 const struct plug_function_table *fn;
22 /* the above field _must_ be first in the structure */
c91409da 23
51470298 24 Socket s;
25 int bufsize;
c85623f9 26 int firstbyte;
2c42b725 27 int cansize;
51470298 28 int term_width, term_height;
29 void *frontend;
2d8d01c8 30
31 Config cfg;
32
33 /* In case we need to read a username from the terminal before starting */
34 prompts_t *prompt;
51470298 35} *Rlogin;
c91409da 36
51470298 37static void rlogin_size(void *handle, int width, int height);
38
39static void c_write(Rlogin rlogin, char *buf, int len)
32874aea 40{
51470298 41 int backlog = from_backend(rlogin->frontend, 0, buf, len);
42 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
c91409da 43}
44
7555d6a5 45static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
46 const char *error_msg, int error_code)
47{
48 Rlogin rlogin = (Rlogin) plug;
49 char addrbuf[256], *msg;
50
51 sk_getaddr(addr, addrbuf, lenof(addrbuf));
52
53 if (type == 0)
54 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
55 else
56 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
57
58 logevent(rlogin->frontend, msg);
59}
60
cbe2d68f 61static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
32874aea 62 int calling_back)
63{
51470298 64 Rlogin rlogin = (Rlogin) plug;
65 if (rlogin->s) {
66 sk_close(rlogin->s);
67 rlogin->s = NULL;
39934deb 68 notify_remote_exit(rlogin->frontend);
f3ab576e 69 }
7e78000d 70 if (error_msg) {
32874aea 71 /* A socket error has occurred. */
a8327734 72 logevent(rlogin->frontend, error_msg);
971bcc0a 73 connection_fatal(rlogin->frontend, "%s", error_msg);
32874aea 74 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 75 return 0;
76}
77
32874aea 78static int rlogin_receive(Plug plug, int urgent, char *data, int len)
79{
51470298 80 Rlogin rlogin = (Rlogin) plug;
c91409da 81 if (urgent == 2) {
32874aea 82 char c;
83
84 c = *data++;
85 len--;
2c42b725 86 if (c == '\x80') {
87 rlogin->cansize = 1;
51470298 88 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
2c42b725 89 }
32874aea 90 /*
91 * We should flush everything (aka Telnet SYNCH) if we see
92 * 0x02, and we should turn off and on _local_ flow control
93 * on 0x10 and 0x20 respectively. I'm not convinced it's
94 * worth it...
95 */
8a5fb9e3 96 } else {
32874aea 97 /*
98 * Main rlogin protocol. This is really simple: the first
99 * byte is expected to be NULL and is ignored, and the rest
100 * is printed.
101 */
c85623f9 102 if (rlogin->firstbyte) {
32874aea 103 if (data[0] == '\0') {
104 data++;
105 len--;
106 }
c85623f9 107 rlogin->firstbyte = 0;
32874aea 108 }
2b0c045b 109 if (len > 0)
51470298 110 c_write(rlogin, data, len);
c91409da 111 }
c91409da 112 return 1;
113}
114
3ad9d396 115static void rlogin_sent(Plug plug, int bufsize)
116{
51470298 117 Rlogin rlogin = (Rlogin) plug;
118 rlogin->bufsize = bufsize;
3ad9d396 119}
120
2d8d01c8 121static void rlogin_startup(Rlogin rlogin, const char *ruser)
122{
123 char z = 0;
124 char *p;
125 sk_write(rlogin->s, &z, 1);
126 sk_write(rlogin->s, rlogin->cfg.localusername,
127 strlen(rlogin->cfg.localusername));
128 sk_write(rlogin->s, &z, 1);
129 sk_write(rlogin->s, ruser,
130 strlen(ruser));
131 sk_write(rlogin->s, &z, 1);
132 sk_write(rlogin->s, rlogin->cfg.termtype,
133 strlen(rlogin->cfg.termtype));
134 sk_write(rlogin->s, "/", 1);
135 for (p = rlogin->cfg.termspeed; isdigit((unsigned char)*p); p++) continue;
136 sk_write(rlogin->s, rlogin->cfg.termspeed, p - rlogin->cfg.termspeed);
137 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
138
139 rlogin->prompt = NULL;
140}
141
c91409da 142/*
143 * Called to set up the rlogin connection.
144 *
145 * Returns an error message, or NULL on success.
146 *
6e1ebb76 147 * Also places the canonical host name into `realhost'. It must be
148 * freed by the caller.
c91409da 149 */
cbe2d68f 150static const char *rlogin_init(void *frontend_handle, void **backend_handle,
151 Config *cfg,
152 char *host, int port, char **realhost,
79bf227b 153 int nodelay, int keepalive)
32874aea 154{
51470298 155 static const struct plug_function_table fn_table = {
7555d6a5 156 rlogin_log,
7e78000d 157 rlogin_closing,
3ad9d396 158 rlogin_receive,
159 rlogin_sent
51470298 160 };
c91409da 161 SockAddr addr;
cbe2d68f 162 const char *err;
51470298 163 Rlogin rlogin;
2d8d01c8 164 char ruser[sizeof(cfg->username)];
c91409da 165
3d88e64d 166 rlogin = snew(struct rlogin_tag);
51470298 167 rlogin->fn = &fn_table;
168 rlogin->s = NULL;
169 rlogin->frontend = frontend_handle;
86916870 170 rlogin->term_width = cfg->width;
171 rlogin->term_height = cfg->height;
c85623f9 172 rlogin->firstbyte = 1;
2c42b725 173 rlogin->cansize = 0;
2d8d01c8 174 rlogin->prompt = NULL;
175 rlogin->cfg = *cfg; /* STRUCTURE COPY */
51470298 176 *backend_handle = rlogin;
887035a5 177
c91409da 178 /*
179 * Try to find host.
180 */
3ad9d396 181 {
57356d63 182 char *buf;
05581745 183 buf = dupprintf("Looking up host \"%s\"%s", host,
184 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
185 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
186 "")));
a8327734 187 logevent(rlogin->frontend, buf);
57356d63 188 sfree(buf);
3ad9d396 189 }
05581745 190 addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
f85e6f6e 191 if ((err = sk_addr_error(addr)) != NULL) {
192 sk_addr_free(addr);
c91409da 193 return err;
f85e6f6e 194 }
c91409da 195
196 if (port < 0)
197 port = 513; /* default rlogin port */
198
199 /*
200 * Open socket.
201 */
51470298 202 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
79bf227b 203 nodelay, keepalive, (Plug) rlogin, cfg);
6f1e7b78 204 if ((err = sk_socket_error(rlogin->s)) != NULL)
c91409da 205 return err;
206
881da168 207 if (*cfg->loghost) {
208 char *colon;
209
210 sfree(*realhost);
211 *realhost = dupstr(cfg->loghost);
212 colon = strrchr(*realhost, ':');
213 if (colon) {
214 /*
215 * FIXME: if we ever update this aspect of ssh.c for
216 * IPv6 literal management, this should change in line
217 * with it.
218 */
219 *colon++ = '\0';
220 }
221 }
222
2d8d01c8 223 /*
224 * Send local username, remote username, terminal type and
225 * terminal speed - unless we don't have the remote username yet,
226 * in which case we prompt for it and may end up deferring doing
227 * anything else until the local prompt mechanism returns.
228 */
229 if (get_remote_username(cfg, ruser, sizeof(ruser))) {
230 rlogin_startup(rlogin, ruser);
231 } else {
232 int ret;
233
234 rlogin->prompt = new_prompts(rlogin->frontend);
235 rlogin->prompt->to_server = TRUE;
236 rlogin->prompt->name = dupstr("Rlogin login name");
237 add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE,
238 sizeof(cfg->username));
239 ret = get_userpass_input(rlogin->prompt, NULL, 0);
240 if (ret >= 0) {
241 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
242 }
243 }
244
c91409da 245 return NULL;
246}
247
fabd1805 248static void rlogin_free(void *handle)
249{
250 Rlogin rlogin = (Rlogin) handle;
251
2d8d01c8 252 if (rlogin->prompt)
253 free_prompts(rlogin->prompt);
fabd1805 254 if (rlogin->s)
255 sk_close(rlogin->s);
256 sfree(rlogin);
257}
258
c91409da 259/*
86916870 260 * Stub routine (we don't have any need to reconfigure this backend).
261 */
262static void rlogin_reconfig(void *handle, Config *cfg)
263{
264}
265
266/*
c91409da 267 * Called to send data down the rlogin connection.
268 */
51470298 269static int rlogin_send(void *handle, char *buf, int len)
32874aea 270{
51470298 271 Rlogin rlogin = (Rlogin) handle;
272
273 if (rlogin->s == NULL)
b5a460cd 274 return 0;
c91409da 275
2d8d01c8 276 if (rlogin->prompt) {
277 /*
278 * We're still prompting for a username, and aren't talking
279 * directly to the network connection yet.
280 */
281 int ret = get_userpass_input(rlogin->prompt,
282 (unsigned char *)buf, len);
283 if (ret >= 0) {
284 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
285 /* that nulls out rlogin->prompt, so then we'll start sending
286 * data down the wire in the obvious way */
287 }
288 } else {
289 rlogin->bufsize = sk_write(rlogin->s, buf, len);
290 }
5471d09a 291
51470298 292 return rlogin->bufsize;
5471d09a 293}
294
295/*
296 * Called to query the current socket sendability status.
297 */
51470298 298static int rlogin_sendbuffer(void *handle)
5471d09a 299{
51470298 300 Rlogin rlogin = (Rlogin) handle;
301 return rlogin->bufsize;
c91409da 302}
303
304/*
305 * Called to set the size of the window
306 */
51470298 307static void rlogin_size(void *handle, int width, int height)
32874aea 308{
51470298 309 Rlogin rlogin = (Rlogin) handle;
e418595a 310 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
c91409da 311
51470298 312 rlogin->term_width = width;
313 rlogin->term_height = height;
f278d6f8 314
2c42b725 315 if (rlogin->s == NULL || !rlogin->cansize)
cf293dd4 316 return;
f278d6f8 317
51470298 318 b[6] = rlogin->term_width >> 8;
319 b[7] = rlogin->term_width & 0xFF;
320 b[4] = rlogin->term_height >> 8;
321 b[5] = rlogin->term_height & 0xFF;
322 rlogin->bufsize = sk_write(rlogin->s, b, 12);
c91409da 323 return;
324}
325
326/*
327 * Send rlogin special codes.
328 */
51470298 329static void rlogin_special(void *handle, Telnet_Special code)
32874aea 330{
c91409da 331 /* Do nothing! */
332 return;
333}
334
125105d1 335/*
336 * Return a list of the special codes that make sense in this
337 * protocol.
338 */
339static const struct telnet_special *rlogin_get_specials(void *handle)
340{
341 return NULL;
342}
343
6226c939 344static int rlogin_connected(void *handle)
32874aea 345{
51470298 346 Rlogin rlogin = (Rlogin) handle;
6226c939 347 return rlogin->s != NULL;
32874aea 348}
c91409da 349
51470298 350static int rlogin_sendok(void *handle)
32874aea 351{
68a49acb 352 /* Rlogin rlogin = (Rlogin) handle; */
32874aea 353 return 1;
354}
c91409da 355
51470298 356static void rlogin_unthrottle(void *handle, int backlog)
5471d09a 357{
51470298 358 Rlogin rlogin = (Rlogin) handle;
359 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
5471d09a 360}
361
51470298 362static int rlogin_ldisc(void *handle, int option)
32874aea 363{
68a49acb 364 /* Rlogin rlogin = (Rlogin) handle; */
0965bee0 365 return 0;
366}
367
b9d7bcad 368static void rlogin_provide_ldisc(void *handle, void *ldisc)
369{
370 /* This is a stub. */
371}
372
a8327734 373static void rlogin_provide_logctx(void *handle, void *logctx)
374{
375 /* This is a stub. */
376}
377
51470298 378static int rlogin_exitcode(void *handle)
d8d6c7e5 379{
0da1a790 380 Rlogin rlogin = (Rlogin) handle;
381 if (rlogin->s != NULL)
382 return -1; /* still connected */
383 else
384 /* If we ever implement RSH, we'll probably need to do this properly */
385 return 0;
d8d6c7e5 386}
387
f89c3294 388/*
389 * cfg_info for rlogin does nothing at all.
390 */
391static int rlogin_cfg_info(void *handle)
392{
393 return 0;
394}
395
c91409da 396Backend rlogin_backend = {
397 rlogin_init,
fabd1805 398 rlogin_free,
86916870 399 rlogin_reconfig,
c91409da 400 rlogin_send,
5471d09a 401 rlogin_sendbuffer,
c91409da 402 rlogin_size,
403 rlogin_special,
125105d1 404 rlogin_get_specials,
6226c939 405 rlogin_connected,
d8d6c7e5 406 rlogin_exitcode,
c91409da 407 rlogin_sendok,
0965bee0 408 rlogin_ldisc,
b9d7bcad 409 rlogin_provide_ldisc,
a8327734 410 rlogin_provide_logctx,
5471d09a 411 rlogin_unthrottle,
f89c3294 412 rlogin_cfg_info,
9e164d82 413 "rlogin",
414 PROT_RLOGIN,
a4451dd1 415 513
c91409da 416};