18 #define RLOGIN_MAX_BACKLOG 4096
20 typedef struct rlogin_tag {
21 const struct plug_function_table *fn;
22 /* the above field _must_ be first in the structure */
28 int term_width, term_height;
33 /* In case we need to read a username from the terminal before starting */
37 static void rlogin_size(void *handle, int width, int height);
39 static void c_write(Rlogin rlogin, char *buf, int len)
41 int backlog = from_backend(rlogin->frontend, 0, buf, len);
42 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
45 static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
46 const char *error_msg, int error_code)
48 Rlogin rlogin = (Rlogin) plug;
49 char addrbuf[256], *msg;
51 sk_getaddr(addr, addrbuf, lenof(addrbuf));
54 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
56 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
58 logevent(rlogin->frontend, msg);
61 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
64 Rlogin rlogin = (Rlogin) plug;
68 notify_remote_exit(rlogin->frontend);
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. */
78 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
80 Rlogin rlogin = (Rlogin) plug;
88 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
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
98 * Main rlogin protocol. This is really simple: the first
99 * byte is expected to be NULL and is ignored, and the rest
102 if (rlogin->firstbyte) {
103 if (data[0] == '\0') {
107 rlogin->firstbyte = 0;
110 c_write(rlogin, data, len);
115 static void rlogin_sent(Plug plug, int bufsize)
117 Rlogin rlogin = (Rlogin) plug;
118 rlogin->bufsize = bufsize;
121 static void rlogin_startup(Rlogin rlogin, const char *ruser)
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);
139 rlogin->prompt = NULL;
143 * Called to set up the rlogin connection.
145 * Returns an error message, or NULL on success.
147 * Also places the canonical host name into `realhost'. It must be
148 * freed by the caller.
150 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
152 char *host, int port, char **realhost,
153 int nodelay, int keepalive)
155 static const struct plug_function_table fn_table = {
168 rlogin = snew(struct rlogin_tag);
169 rlogin->fn = &fn_table;
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;
176 rlogin->prompt = NULL;
177 rlogin->conf = conf_copy(conf);
178 *backend_handle = rlogin;
180 addressfamily = conf_get_int(conf, CONF_addressfamily);
186 buf = dupprintf("Looking up host \"%s\"%s", host,
187 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
188 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
190 logevent(rlogin->frontend, buf);
193 addr = name_lookup(host, port, realhost, conf, addressfamily);
194 if ((err = sk_addr_error(addr)) != NULL) {
200 port = 513; /* default rlogin port */
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)
210 loghost = conf_get_str(conf, CONF_loghost);
215 *realhost = dupstr(loghost);
216 colon = strrchr(*realhost, ':');
219 * FIXME: if we ever update this aspect of ssh.c for
220 * IPv6 literal management, this should change in line
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.
233 if ((ruser = get_remote_username(conf)) == NULL) {
234 rlogin_startup(rlogin, ruser);
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);
246 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
253 static void rlogin_free(void *handle)
255 Rlogin rlogin = (Rlogin) handle;
258 free_prompts(rlogin->prompt);
261 conf_free(rlogin->conf);
266 * Stub routine (we don't have any need to reconfigure this backend).
268 static void rlogin_reconfig(void *handle, Conf *conf)
273 * Called to send data down the rlogin connection.
275 static int rlogin_send(void *handle, char *buf, int len)
277 Rlogin rlogin = (Rlogin) handle;
279 if (rlogin->s == NULL)
282 if (rlogin->prompt) {
284 * We're still prompting for a username, and aren't talking
285 * directly to the network connection yet.
287 int ret = get_userpass_input(rlogin->prompt,
288 (unsigned char *)buf, len);
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 */
295 rlogin->bufsize = sk_write(rlogin->s, buf, len);
298 return rlogin->bufsize;
302 * Called to query the current socket sendability status.
304 static int rlogin_sendbuffer(void *handle)
306 Rlogin rlogin = (Rlogin) handle;
307 return rlogin->bufsize;
311 * Called to set the size of the window
313 static void rlogin_size(void *handle, int width, int height)
315 Rlogin rlogin = (Rlogin) handle;
316 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
318 rlogin->term_width = width;
319 rlogin->term_height = height;
321 if (rlogin->s == NULL || !rlogin->cansize)
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);
333 * Send rlogin special codes.
335 static void rlogin_special(void *handle, Telnet_Special code)
342 * Return a list of the special codes that make sense in this
345 static const struct telnet_special *rlogin_get_specials(void *handle)
350 static int rlogin_connected(void *handle)
352 Rlogin rlogin = (Rlogin) handle;
353 return rlogin->s != NULL;
356 static int rlogin_sendok(void *handle)
358 /* Rlogin rlogin = (Rlogin) handle; */
362 static void rlogin_unthrottle(void *handle, int backlog)
364 Rlogin rlogin = (Rlogin) handle;
365 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
368 static int rlogin_ldisc(void *handle, int option)
370 /* Rlogin rlogin = (Rlogin) handle; */
374 static void rlogin_provide_ldisc(void *handle, void *ldisc)
376 /* This is a stub. */
379 static void rlogin_provide_logctx(void *handle, void *logctx)
381 /* This is a stub. */
384 static int rlogin_exitcode(void *handle)
386 Rlogin rlogin = (Rlogin) handle;
387 if (rlogin->s != NULL)
388 return -1; /* still connected */
390 /* If we ever implement RSH, we'll probably need to do this properly */
395 * cfg_info for rlogin does nothing at all.
397 static int rlogin_cfg_info(void *handle)
402 Backend rlogin_backend = {
415 rlogin_provide_ldisc,
416 rlogin_provide_logctx,