]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - rlogin.c
Ronald Landheer-Cieslak points out that the various back ends which
[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 }
62
63 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
64                           int calling_back)
65 {
66     Rlogin rlogin = (Rlogin) plug;
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
74     if (rlogin->s) {
75         sk_close(rlogin->s);
76         rlogin->s = NULL;
77         if (error_msg)
78             rlogin->closed_on_socket_error = TRUE;
79         notify_remote_exit(rlogin->frontend);
80     }
81     if (error_msg) {
82         /* A socket error has occurred. */
83         logevent(rlogin->frontend, error_msg);
84         connection_fatal(rlogin->frontend, "%s", error_msg);
85     }                                  /* Otherwise, the remote side closed the connection normally. */
86     return 0;
87 }
88
89 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
90 {
91     Rlogin rlogin = (Rlogin) plug;
92     if (urgent == 2) {
93         char c;
94
95         c = *data++;
96         len--;
97         if (c == '\x80') {
98             rlogin->cansize = 1;
99             rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
100         }
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          */
107     } else {
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          */
113         if (rlogin->firstbyte) {
114             if (data[0] == '\0') {
115                 data++;
116                 len--;
117             }
118             rlogin->firstbyte = 0;
119         }
120         if (len > 0)
121             c_write(rlogin, data, len);
122     }
123     return 1;
124 }
125
126 static void rlogin_sent(Plug plug, int bufsize)
127 {
128     Rlogin rlogin = (Rlogin) plug;
129     rlogin->bufsize = bufsize;
130 }
131
132 static void rlogin_startup(Rlogin rlogin, const char *ruser)
133 {
134     char z = 0;
135     char *p;
136
137     sk_write(rlogin->s, &z, 1);
138     p = conf_get_str(rlogin->conf, CONF_localusername);
139     sk_write(rlogin->s, p, strlen(p));
140     sk_write(rlogin->s, &z, 1);
141     sk_write(rlogin->s, ruser, strlen(ruser));
142     sk_write(rlogin->s, &z, 1);
143     p = conf_get_str(rlogin->conf, CONF_termtype);
144     sk_write(rlogin->s, p, strlen(p));
145     sk_write(rlogin->s, "/", 1);
146     p = conf_get_str(rlogin->conf, CONF_termspeed);
147     sk_write(rlogin->s, p, strspn(p, "0123456789"));
148     rlogin->bufsize = sk_write(rlogin->s, &z, 1);
149
150     rlogin->prompt = NULL;
151 }
152
153 /*
154  * Called to set up the rlogin connection.
155  * 
156  * Returns an error message, or NULL on success.
157  *
158  * Also places the canonical host name into `realhost'. It must be
159  * freed by the caller.
160  */
161 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
162                                Conf *conf,
163                                char *host, int port, char **realhost,
164                                int nodelay, int keepalive)
165 {
166     static const struct plug_function_table fn_table = {
167         rlogin_log,
168         rlogin_closing,
169         rlogin_receive,
170         rlogin_sent
171     };
172     SockAddr addr;
173     const char *err;
174     Rlogin rlogin;
175     char *ruser;
176     int addressfamily;
177     char *loghost;
178
179     rlogin = snew(struct rlogin_tag);
180     rlogin->fn = &fn_table;
181     rlogin->s = NULL;
182     rlogin->closed_on_socket_error = FALSE;
183     rlogin->frontend = frontend_handle;
184     rlogin->term_width = conf_get_int(conf, CONF_width);
185     rlogin->term_height = conf_get_int(conf, CONF_height);
186     rlogin->firstbyte = 1;
187     rlogin->cansize = 0;
188     rlogin->prompt = NULL;
189     rlogin->conf = conf_copy(conf);
190     *backend_handle = rlogin;
191
192     addressfamily = conf_get_int(conf, CONF_addressfamily);
193     /*
194      * Try to find host.
195      */
196     {
197         char *buf;
198         buf = dupprintf("Looking up host \"%s\"%s", host,
199                         (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
200                          (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
201                           "")));
202         logevent(rlogin->frontend, buf);
203         sfree(buf);
204     }
205     addr = name_lookup(host, port, realhost, conf, addressfamily);
206     if ((err = sk_addr_error(addr)) != NULL) {
207         sk_addr_free(addr);
208         return err;
209     }
210
211     if (port < 0)
212         port = 513;                    /* default rlogin port */
213
214     /*
215      * Open socket.
216      */
217     rlogin->s = new_connection(addr, *realhost, port, 1, 0,
218                                nodelay, keepalive, (Plug) rlogin, conf);
219     if ((err = sk_socket_error(rlogin->s)) != NULL)
220         return err;
221
222     loghost = conf_get_str(conf, CONF_loghost);
223     if (*loghost) {
224         char *colon;
225
226         sfree(*realhost);
227         *realhost = dupstr(loghost);
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
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      */
245     if ((ruser = get_remote_username(conf)) != NULL) {
246         rlogin_startup(rlogin, ruser);
247         sfree(ruser);
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");
254         add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE); 
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
261     return NULL;
262 }
263
264 static void rlogin_free(void *handle)
265 {
266     Rlogin rlogin = (Rlogin) handle;
267
268     if (rlogin->prompt)
269         free_prompts(rlogin->prompt);
270     if (rlogin->s)
271         sk_close(rlogin->s);
272     conf_free(rlogin->conf);
273     sfree(rlogin);
274 }
275
276 /*
277  * Stub routine (we don't have any need to reconfigure this backend).
278  */
279 static void rlogin_reconfig(void *handle, Conf *conf)
280 {
281 }
282
283 /*
284  * Called to send data down the rlogin connection.
285  */
286 static int rlogin_send(void *handle, char *buf, int len)
287 {
288     Rlogin rlogin = (Rlogin) handle;
289
290     if (rlogin->s == NULL)
291         return 0;
292
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     }
308
309     return rlogin->bufsize;
310 }
311
312 /*
313  * Called to query the current socket sendability status.
314  */
315 static int rlogin_sendbuffer(void *handle)
316 {
317     Rlogin rlogin = (Rlogin) handle;
318     return rlogin->bufsize;
319 }
320
321 /*
322  * Called to set the size of the window
323  */
324 static void rlogin_size(void *handle, int width, int height)
325 {
326     Rlogin rlogin = (Rlogin) handle;
327     char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
328
329     rlogin->term_width = width;
330     rlogin->term_height = height;
331
332     if (rlogin->s == NULL || !rlogin->cansize)
333         return;
334
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);
340     return;
341 }
342
343 /*
344  * Send rlogin special codes.
345  */
346 static void rlogin_special(void *handle, Telnet_Special code)
347 {
348     /* Do nothing! */
349     return;
350 }
351
352 /*
353  * Return a list of the special codes that make sense in this
354  * protocol.
355  */
356 static const struct telnet_special *rlogin_get_specials(void *handle)
357 {
358     return NULL;
359 }
360
361 static int rlogin_connected(void *handle)
362 {
363     Rlogin rlogin = (Rlogin) handle;
364     return rlogin->s != NULL;
365 }
366
367 static int rlogin_sendok(void *handle)
368 {
369     /* Rlogin rlogin = (Rlogin) handle; */
370     return 1;
371 }
372
373 static void rlogin_unthrottle(void *handle, int backlog)
374 {
375     Rlogin rlogin = (Rlogin) handle;
376     sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
377 }
378
379 static int rlogin_ldisc(void *handle, int option)
380 {
381     /* Rlogin rlogin = (Rlogin) handle; */
382     return 0;
383 }
384
385 static void rlogin_provide_ldisc(void *handle, void *ldisc)
386 {
387     /* This is a stub. */
388 }
389
390 static void rlogin_provide_logctx(void *handle, void *logctx)
391 {
392     /* This is a stub. */
393 }
394
395 static int rlogin_exitcode(void *handle)
396 {
397     Rlogin rlogin = (Rlogin) handle;
398     if (rlogin->s != NULL)
399         return -1;                     /* still connected */
400     else if (rlogin->closed_on_socket_error)
401         return INT_MAX;     /* a socket error counts as an unclean exit */
402     else
403         /* If we ever implement RSH, we'll probably need to do this properly */
404         return 0;
405 }
406
407 /*
408  * cfg_info for rlogin does nothing at all.
409  */
410 static int rlogin_cfg_info(void *handle)
411 {
412     return 0;
413 }
414
415 Backend rlogin_backend = {
416     rlogin_init,
417     rlogin_free,
418     rlogin_reconfig,
419     rlogin_send,
420     rlogin_sendbuffer,
421     rlogin_size,
422     rlogin_special,
423     rlogin_get_specials,
424     rlogin_connected,
425     rlogin_exitcode,
426     rlogin_sendok,
427     rlogin_ldisc,
428     rlogin_provide_ldisc,
429     rlogin_provide_logctx,
430     rlogin_unthrottle,
431     rlogin_cfg_info,
432     "rlogin",
433     PROT_RLOGIN,
434     513
435 };