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