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