Post-release destabilisation! Completely remove the struct type
[u/mdw/putty] / rlogin.c
1 /*
2 * Rlogin backend.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <ctype.h>
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
18 #define RLOGIN_MAX_BACKLOG 4096
19
20 typedef struct rlogin_tag {
21 const struct plug_function_table *fn;
22 /* the above field _must_ be first in the structure */
23
24 Socket s;
25 int bufsize;
26 int firstbyte;
27 int cansize;
28 int term_width, term_height;
29 void *frontend;
30
31 Conf *conf;
32
33 /* In case we need to read a username from the terminal before starting */
34 prompts_t *prompt;
35 } *Rlogin;
36
37 static void rlogin_size(void *handle, int width, int height);
38
39 static void c_write(Rlogin rlogin, char *buf, int len)
40 {
41 int backlog = from_backend(rlogin->frontend, 0, buf, len);
42 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
43 }
44
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
61 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
62 int calling_back)
63 {
64 Rlogin rlogin = (Rlogin) plug;
65 if (rlogin->s) {
66 sk_close(rlogin->s);
67 rlogin->s = NULL;
68 notify_remote_exit(rlogin->frontend);
69 }
70 if (error_msg) {
71 /* A socket error has occurred. */
72 logevent(rlogin->frontend, error_msg);
73 connection_fatal(rlogin->frontend, "%s", error_msg);
74 } /* Otherwise, the remote side closed the connection normally. */
75 return 0;
76 }
77
78 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
79 {
80 Rlogin rlogin = (Rlogin) plug;
81 if (urgent == 2) {
82 char c;
83
84 c = *data++;
85 len--;
86 if (c == '\x80') {
87 rlogin->cansize = 1;
88 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
89 }
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 */
96 } else {
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 */
102 if (rlogin->firstbyte) {
103 if (data[0] == '\0') {
104 data++;
105 len--;
106 }
107 rlogin->firstbyte = 0;
108 }
109 if (len > 0)
110 c_write(rlogin, data, len);
111 }
112 return 1;
113 }
114
115 static void rlogin_sent(Plug plug, int bufsize)
116 {
117 Rlogin rlogin = (Rlogin) plug;
118 rlogin->bufsize = bufsize;
119 }
120
121 static void rlogin_startup(Rlogin rlogin, const char *ruser)
122 {
123 char z = 0;
124 char *p;
125
126 sk_write(rlogin->s, &z, 1);
127 p = conf_get_str(rlogin->conf, CONF_localusername);
128 sk_write(rlogin->s, p, strlen(p));
129 sk_write(rlogin->s, &z, 1);
130 sk_write(rlogin->s, ruser, strlen(ruser));
131 sk_write(rlogin->s, &z, 1);
132 p = conf_get_str(rlogin->conf, CONF_termtype);
133 sk_write(rlogin->s, p, strlen(p));
134 sk_write(rlogin->s, "/", 1);
135 p = conf_get_str(rlogin->conf, CONF_termspeed);
136 sk_write(rlogin->s, p, strspn(p, "0123456789"));
137 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
138
139 rlogin->prompt = NULL;
140 }
141
142 /*
143 * Called to set up the rlogin connection.
144 *
145 * Returns an error message, or NULL on success.
146 *
147 * Also places the canonical host name into `realhost'. It must be
148 * freed by the caller.
149 */
150 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
151 Conf *conf,
152 char *host, int port, char **realhost,
153 int nodelay, int keepalive)
154 {
155 static const struct plug_function_table fn_table = {
156 rlogin_log,
157 rlogin_closing,
158 rlogin_receive,
159 rlogin_sent
160 };
161 SockAddr addr;
162 const char *err;
163 Rlogin rlogin;
164 char *ruser;
165 int addressfamily;
166 char *loghost;
167
168 rlogin = snew(struct rlogin_tag);
169 rlogin->fn = &fn_table;
170 rlogin->s = NULL;
171 rlogin->frontend = frontend_handle;
172 rlogin->term_width = conf_get_int(conf, CONF_width);
173 rlogin->term_height = conf_get_int(conf, CONF_height);
174 rlogin->firstbyte = 1;
175 rlogin->cansize = 0;
176 rlogin->prompt = NULL;
177 rlogin->conf = conf_copy(conf);
178 *backend_handle = rlogin;
179
180 addressfamily = conf_get_int(conf, CONF_addressfamily);
181 /*
182 * Try to find host.
183 */
184 {
185 char *buf;
186 buf = dupprintf("Looking up host \"%s\"%s", host,
187 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
188 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
189 "")));
190 logevent(rlogin->frontend, buf);
191 sfree(buf);
192 }
193 addr = name_lookup(host, port, realhost, conf, addressfamily);
194 if ((err = sk_addr_error(addr)) != NULL) {
195 sk_addr_free(addr);
196 return err;
197 }
198
199 if (port < 0)
200 port = 513; /* default rlogin port */
201
202 /*
203 * Open socket.
204 */
205 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
206 nodelay, keepalive, (Plug) rlogin, conf);
207 if ((err = sk_socket_error(rlogin->s)) != NULL)
208 return err;
209
210 loghost = conf_get_str(conf, CONF_loghost);
211 if (*loghost) {
212 char *colon;
213
214 sfree(*realhost);
215 *realhost = dupstr(loghost);
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
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 */
233 if ((ruser = get_remote_username(conf)) == NULL) {
234 rlogin_startup(rlogin, ruser);
235 sfree(ruser);
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");
242 /* 512 is an arbitrary limit :-( */
243 add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE, 512);
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
250 return NULL;
251 }
252
253 static void rlogin_free(void *handle)
254 {
255 Rlogin rlogin = (Rlogin) handle;
256
257 if (rlogin->prompt)
258 free_prompts(rlogin->prompt);
259 if (rlogin->s)
260 sk_close(rlogin->s);
261 conf_free(rlogin->conf);
262 sfree(rlogin);
263 }
264
265 /*
266 * Stub routine (we don't have any need to reconfigure this backend).
267 */
268 static void rlogin_reconfig(void *handle, Conf *conf)
269 {
270 }
271
272 /*
273 * Called to send data down the rlogin connection.
274 */
275 static int rlogin_send(void *handle, char *buf, int len)
276 {
277 Rlogin rlogin = (Rlogin) handle;
278
279 if (rlogin->s == NULL)
280 return 0;
281
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 }
297
298 return rlogin->bufsize;
299 }
300
301 /*
302 * Called to query the current socket sendability status.
303 */
304 static int rlogin_sendbuffer(void *handle)
305 {
306 Rlogin rlogin = (Rlogin) handle;
307 return rlogin->bufsize;
308 }
309
310 /*
311 * Called to set the size of the window
312 */
313 static void rlogin_size(void *handle, int width, int height)
314 {
315 Rlogin rlogin = (Rlogin) handle;
316 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
317
318 rlogin->term_width = width;
319 rlogin->term_height = height;
320
321 if (rlogin->s == NULL || !rlogin->cansize)
322 return;
323
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);
329 return;
330 }
331
332 /*
333 * Send rlogin special codes.
334 */
335 static void rlogin_special(void *handle, Telnet_Special code)
336 {
337 /* Do nothing! */
338 return;
339 }
340
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
350 static int rlogin_connected(void *handle)
351 {
352 Rlogin rlogin = (Rlogin) handle;
353 return rlogin->s != NULL;
354 }
355
356 static int rlogin_sendok(void *handle)
357 {
358 /* Rlogin rlogin = (Rlogin) handle; */
359 return 1;
360 }
361
362 static void rlogin_unthrottle(void *handle, int backlog)
363 {
364 Rlogin rlogin = (Rlogin) handle;
365 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
366 }
367
368 static int rlogin_ldisc(void *handle, int option)
369 {
370 /* Rlogin rlogin = (Rlogin) handle; */
371 return 0;
372 }
373
374 static void rlogin_provide_ldisc(void *handle, void *ldisc)
375 {
376 /* This is a stub. */
377 }
378
379 static void rlogin_provide_logctx(void *handle, void *logctx)
380 {
381 /* This is a stub. */
382 }
383
384 static int rlogin_exitcode(void *handle)
385 {
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;
392 }
393
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
402 Backend rlogin_backend = {
403 rlogin_init,
404 rlogin_free,
405 rlogin_reconfig,
406 rlogin_send,
407 rlogin_sendbuffer,
408 rlogin_size,
409 rlogin_special,
410 rlogin_get_specials,
411 rlogin_connected,
412 rlogin_exitcode,
413 rlogin_sendok,
414 rlogin_ldisc,
415 rlogin_provide_ldisc,
416 rlogin_provide_logctx,
417 rlogin_unthrottle,
418 rlogin_cfg_info,
419 "rlogin",
420 PROT_RLOGIN,
421 513
422 };