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