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