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