]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - rlogin.c
Robustness fixes for KEXINIT handling and others. In particular, I've
[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 term_width, term_height;
23     void *frontend;
24 } *Rlogin;
25
26 static void rlogin_size(void *handle, int width, int height);
27
28 static void c_write(Rlogin rlogin, char *buf, int len)
29 {
30     int backlog = from_backend(rlogin->frontend, 0, buf, len);
31     sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
32 }
33
34 static int rlogin_closing(Plug plug, char *error_msg, int error_code,
35                           int calling_back)
36 {
37     Rlogin rlogin = (Rlogin) plug;
38     if (rlogin->s) {
39         sk_close(rlogin->s);
40         rlogin->s = NULL;
41     }
42     if (error_msg) {
43         /* A socket error has occurred. */
44         logevent(rlogin->frontend, error_msg);
45         connection_fatal("%s", error_msg);
46     }                                  /* Otherwise, the remote side closed the connection normally. */
47     return 0;
48 }
49
50 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
51 {
52     Rlogin rlogin = (Rlogin) plug;
53     if (urgent == 2) {
54         char c;
55
56         c = *data++;
57         len--;
58         if (c == '\x80')
59             rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
60         /*
61          * We should flush everything (aka Telnet SYNCH) if we see
62          * 0x02, and we should turn off and on _local_ flow control
63          * on 0x10 and 0x20 respectively. I'm not convinced it's
64          * worth it...
65          */
66     } else {
67         /*
68          * Main rlogin protocol. This is really simple: the first
69          * byte is expected to be NULL and is ignored, and the rest
70          * is printed.
71          */
72         static int firstbyte = 1;
73         if (firstbyte) {
74             if (data[0] == '\0') {
75                 data++;
76                 len--;
77             }
78             firstbyte = 0;
79         }
80         if (len > 0)
81             c_write(rlogin, data, len);
82     }
83     return 1;
84 }
85
86 static void rlogin_sent(Plug plug, int bufsize)
87 {
88     Rlogin rlogin = (Rlogin) plug;
89     rlogin->bufsize = bufsize;
90 }
91
92 /*
93  * Called to set up the rlogin connection.
94  * 
95  * Returns an error message, or NULL on success.
96  *
97  * Also places the canonical host name into `realhost'. It must be
98  * freed by the caller.
99  */
100 static char *rlogin_init(void *frontend_handle, void **backend_handle,
101                          char *host, int port, char **realhost, int nodelay)
102 {
103     static const struct plug_function_table fn_table = {
104         rlogin_closing,
105         rlogin_receive,
106         rlogin_sent
107     };
108     SockAddr addr;
109     char *err;
110     Rlogin rlogin;
111
112     rlogin = smalloc(sizeof(*rlogin));
113     rlogin->fn = &fn_table;
114     rlogin->s = NULL;
115     rlogin->frontend = frontend_handle;
116     rlogin->term_width = cfg.width;
117     rlogin->term_height = cfg.height;
118     *backend_handle = rlogin;
119
120     /*
121      * Try to find host.
122      */
123     {
124         char *buf;
125         buf = dupprintf("Looking up host \"%s\"", host);
126         logevent(rlogin->frontend, buf);
127         sfree(buf);
128     }
129     addr = sk_namelookup(host, realhost);
130     if ((err = sk_addr_error(addr)))
131         return err;
132
133     if (port < 0)
134         port = 513;                    /* default rlogin port */
135
136     /*
137      * Open socket.
138      */
139     {
140         char *buf, addrbuf[100];
141         sk_getaddr(addr, addrbuf, 100);
142         buf = dupprintf("Connecting to %s port %d", addrbuf, port);
143         logevent(rlogin->frontend, buf);
144         sfree(buf);
145     }
146     rlogin->s = new_connection(addr, *realhost, port, 1, 0,
147                                nodelay, (Plug) rlogin);
148     if ((err = sk_socket_error(rlogin->s)))
149         return err;
150
151     sk_addr_free(addr);
152
153     /*
154      * Send local username, remote username, terminal/speed
155      */
156
157     {
158         char z = 0;
159         char *p;
160         sk_write(rlogin->s, &z, 1);
161         sk_write(rlogin->s, cfg.localusername, strlen(cfg.localusername));
162         sk_write(rlogin->s, &z, 1);
163         sk_write(rlogin->s, cfg.username, strlen(cfg.username));
164         sk_write(rlogin->s, &z, 1);
165         sk_write(rlogin->s, cfg.termtype, strlen(cfg.termtype));
166         sk_write(rlogin->s, "/", 1);
167         for (p = cfg.termspeed; isdigit(*p); p++);
168         sk_write(rlogin->s, cfg.termspeed, p - cfg.termspeed);
169         rlogin->bufsize = sk_write(rlogin->s, &z, 1);
170     }
171
172     return NULL;
173 }
174
175 /*
176  * Called to send data down the rlogin connection.
177  */
178 static int rlogin_send(void *handle, char *buf, int len)
179 {
180     Rlogin rlogin = (Rlogin) handle;
181
182     if (rlogin->s == NULL)
183         return 0;
184
185     rlogin->bufsize = sk_write(rlogin->s, buf, len);
186
187     return rlogin->bufsize;
188 }
189
190 /*
191  * Called to query the current socket sendability status.
192  */
193 static int rlogin_sendbuffer(void *handle)
194 {
195     Rlogin rlogin = (Rlogin) handle;
196     return rlogin->bufsize;
197 }
198
199 /*
200  * Called to set the size of the window
201  */
202 static void rlogin_size(void *handle, int width, int height)
203 {
204     Rlogin rlogin = (Rlogin) handle;
205     char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
206
207     rlogin->term_width = width;
208     rlogin->term_height = height;
209
210     if (rlogin->s == NULL)
211         return;
212
213     b[6] = rlogin->term_width >> 8;
214     b[7] = rlogin->term_width & 0xFF;
215     b[4] = rlogin->term_height >> 8;
216     b[5] = rlogin->term_height & 0xFF;
217     rlogin->bufsize = sk_write(rlogin->s, b, 12);
218     return;
219 }
220
221 /*
222  * Send rlogin special codes.
223  */
224 static void rlogin_special(void *handle, Telnet_Special code)
225 {
226     /* Do nothing! */
227     return;
228 }
229
230 static Socket rlogin_socket(void *handle)
231 {
232     Rlogin rlogin = (Rlogin) handle;
233     return rlogin->s;
234 }
235
236 static int rlogin_sendok(void *handle)
237 {
238     /* Rlogin rlogin = (Rlogin) handle; */
239     return 1;
240 }
241
242 static void rlogin_unthrottle(void *handle, int backlog)
243 {
244     Rlogin rlogin = (Rlogin) handle;
245     sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
246 }
247
248 static int rlogin_ldisc(void *handle, int option)
249 {
250     /* Rlogin rlogin = (Rlogin) handle; */
251     return 0;
252 }
253
254 static void rlogin_provide_ldisc(void *handle, void *ldisc)
255 {
256     /* This is a stub. */
257 }
258
259 static void rlogin_provide_logctx(void *handle, void *logctx)
260 {
261     /* This is a stub. */
262 }
263
264 static int rlogin_exitcode(void *handle)
265 {
266     /* Rlogin rlogin = (Rlogin) handle; */
267     /* If we ever implement RSH, we'll probably need to do this properly */
268     return 0;
269 }
270
271 Backend rlogin_backend = {
272     rlogin_init,
273     rlogin_send,
274     rlogin_sendbuffer,
275     rlogin_size,
276     rlogin_special,
277     rlogin_socket,
278     rlogin_exitcode,
279     rlogin_sendok,
280     rlogin_ldisc,
281     rlogin_provide_ldisc,
282     rlogin_provide_logctx,
283     rlogin_unthrottle,
284     1
285 };