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