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