19 #define RLOGIN_MAX_BACKLOG 4096
21 typedef struct rlogin_tag {
22 const struct plug_function_table *fn;
23 /* the above field _must_ be first in the structure */
26 int closed_on_socket_error;
30 int term_width, term_height;
35 /* In case we need to read a username from the terminal before starting */
39 static void rlogin_size(void *handle, int width, int height);
41 static void c_write(Rlogin rlogin, char *buf, int len)
43 int backlog = from_backend(rlogin->frontend, 0, buf, len);
44 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
47 static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
48 const char *error_msg, int error_code)
50 Rlogin rlogin = (Rlogin) plug;
51 char addrbuf[256], *msg;
53 sk_getaddr(addr, addrbuf, lenof(addrbuf));
56 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
58 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
60 logevent(rlogin->frontend, msg);
64 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
67 Rlogin rlogin = (Rlogin) plug;
70 * We don't implement independent EOF in each direction for Telnet
71 * connections; as soon as we get word that the remote side has
72 * sent us EOF, we wind up the whole connection.
79 rlogin->closed_on_socket_error = TRUE;
80 notify_remote_exit(rlogin->frontend);
83 /* A socket error has occurred. */
84 logevent(rlogin->frontend, error_msg);
85 connection_fatal(rlogin->frontend, "%s", error_msg);
86 } /* Otherwise, the remote side closed the connection normally. */
90 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
92 Rlogin rlogin = (Rlogin) plug;
100 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
103 * We should flush everything (aka Telnet SYNCH) if we see
104 * 0x02, and we should turn off and on _local_ flow control
105 * on 0x10 and 0x20 respectively. I'm not convinced it's
110 * Main rlogin protocol. This is really simple: the first
111 * byte is expected to be NULL and is ignored, and the rest
114 if (rlogin->firstbyte) {
115 if (data[0] == '\0') {
119 rlogin->firstbyte = 0;
122 c_write(rlogin, data, len);
127 static void rlogin_sent(Plug plug, int bufsize)
129 Rlogin rlogin = (Rlogin) plug;
130 rlogin->bufsize = bufsize;
133 static void rlogin_startup(Rlogin rlogin, const char *ruser)
138 sk_write(rlogin->s, &z, 1);
139 p = conf_get_str(rlogin->conf, CONF_localusername);
140 sk_write(rlogin->s, p, strlen(p));
141 sk_write(rlogin->s, &z, 1);
142 sk_write(rlogin->s, ruser, strlen(ruser));
143 sk_write(rlogin->s, &z, 1);
144 p = conf_get_str(rlogin->conf, CONF_termtype);
145 sk_write(rlogin->s, p, strlen(p));
146 sk_write(rlogin->s, "/", 1);
147 p = conf_get_str(rlogin->conf, CONF_termspeed);
148 sk_write(rlogin->s, p, strspn(p, "0123456789"));
149 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
151 rlogin->prompt = NULL;
155 * Called to set up the rlogin connection.
157 * Returns an error message, or NULL on success.
159 * Also places the canonical host name into `realhost'. It must be
160 * freed by the caller.
162 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
164 char *host, int port, char **realhost,
165 int nodelay, int keepalive)
167 static const struct plug_function_table fn_table = {
180 rlogin = snew(struct rlogin_tag);
181 rlogin->fn = &fn_table;
183 rlogin->closed_on_socket_error = FALSE;
184 rlogin->frontend = frontend_handle;
185 rlogin->term_width = conf_get_int(conf, CONF_width);
186 rlogin->term_height = conf_get_int(conf, CONF_height);
187 rlogin->firstbyte = 1;
189 rlogin->prompt = NULL;
190 rlogin->conf = conf_copy(conf);
191 *backend_handle = rlogin;
193 addressfamily = conf_get_int(conf, CONF_addressfamily);
199 buf = dupprintf("Looking up host \"%s\"%s", host,
200 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
201 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
203 logevent(rlogin->frontend, buf);
206 addr = name_lookup(host, port, realhost, conf, addressfamily);
207 if ((err = sk_addr_error(addr)) != NULL) {
213 port = 513; /* default rlogin port */
218 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
219 nodelay, keepalive, (Plug) rlogin, conf);
220 if ((err = sk_socket_error(rlogin->s)) != NULL)
223 loghost = conf_get_str(conf, CONF_loghost);
228 *realhost = dupstr(loghost);
229 colon = strrchr(*realhost, ':');
232 * FIXME: if we ever update this aspect of ssh.c for
233 * IPv6 literal management, this should change in line
241 * Send local username, remote username, terminal type and
242 * terminal speed - unless we don't have the remote username yet,
243 * in which case we prompt for it and may end up deferring doing
244 * anything else until the local prompt mechanism returns.
246 if ((ruser = get_remote_username(conf)) != NULL) {
247 rlogin_startup(rlogin, ruser);
252 rlogin->prompt = new_prompts(rlogin->frontend);
253 rlogin->prompt->to_server = TRUE;
254 rlogin->prompt->name = dupstr("Rlogin login name");
255 add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE);
256 ret = get_userpass_input(rlogin->prompt, NULL, 0);
258 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
265 static void rlogin_free(void *handle)
267 Rlogin rlogin = (Rlogin) handle;
270 free_prompts(rlogin->prompt);
273 conf_free(rlogin->conf);
278 * Stub routine (we don't have any need to reconfigure this backend).
280 static void rlogin_reconfig(void *handle, Conf *conf)
285 * Called to send data down the rlogin connection.
287 static int rlogin_send(void *handle, char *buf, int len)
289 Rlogin rlogin = (Rlogin) handle;
291 if (rlogin->s == NULL)
294 if (rlogin->prompt) {
296 * We're still prompting for a username, and aren't talking
297 * directly to the network connection yet.
299 int ret = get_userpass_input(rlogin->prompt,
300 (unsigned char *)buf, len);
302 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
303 /* that nulls out rlogin->prompt, so then we'll start sending
304 * data down the wire in the obvious way */
307 rlogin->bufsize = sk_write(rlogin->s, buf, len);
310 return rlogin->bufsize;
314 * Called to query the current socket sendability status.
316 static int rlogin_sendbuffer(void *handle)
318 Rlogin rlogin = (Rlogin) handle;
319 return rlogin->bufsize;
323 * Called to set the size of the window
325 static void rlogin_size(void *handle, int width, int height)
327 Rlogin rlogin = (Rlogin) handle;
328 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
330 rlogin->term_width = width;
331 rlogin->term_height = height;
333 if (rlogin->s == NULL || !rlogin->cansize)
336 b[6] = rlogin->term_width >> 8;
337 b[7] = rlogin->term_width & 0xFF;
338 b[4] = rlogin->term_height >> 8;
339 b[5] = rlogin->term_height & 0xFF;
340 rlogin->bufsize = sk_write(rlogin->s, b, 12);
345 * Send rlogin special codes.
347 static void rlogin_special(void *handle, Telnet_Special code)
354 * Return a list of the special codes that make sense in this
357 static const struct telnet_special *rlogin_get_specials(void *handle)
362 static int rlogin_connected(void *handle)
364 Rlogin rlogin = (Rlogin) handle;
365 return rlogin->s != NULL;
368 static int rlogin_sendok(void *handle)
370 /* Rlogin rlogin = (Rlogin) handle; */
374 static void rlogin_unthrottle(void *handle, int backlog)
376 Rlogin rlogin = (Rlogin) handle;
377 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
380 static int rlogin_ldisc(void *handle, int option)
382 /* Rlogin rlogin = (Rlogin) handle; */
386 static void rlogin_provide_ldisc(void *handle, void *ldisc)
388 /* This is a stub. */
391 static void rlogin_provide_logctx(void *handle, void *logctx)
393 /* This is a stub. */
396 static int rlogin_exitcode(void *handle)
398 Rlogin rlogin = (Rlogin) handle;
399 if (rlogin->s != NULL)
400 return -1; /* still connected */
401 else if (rlogin->closed_on_socket_error)
402 return INT_MAX; /* a socket error counts as an unclean exit */
404 /* If we ever implement RSH, we'll probably need to do this properly */
409 * cfg_info for rlogin does nothing at all.
411 static int rlogin_cfg_info(void *handle)
416 Backend rlogin_backend = {
429 rlogin_provide_ldisc,
430 rlogin_provide_logctx,