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