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