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