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