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;
67 * We don't implement independent EOF in each direction for Telnet
68 * connections; as soon as we get word that the remote side has
69 * sent us EOF, we wind up the whole connection.
75 notify_remote_exit(rlogin->frontend);
78 /* A socket error has occurred. */
79 logevent(rlogin->frontend, error_msg);
80 connection_fatal(rlogin->frontend, "%s", error_msg);
81 } /* Otherwise, the remote side closed the connection normally. */
85 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
87 Rlogin rlogin = (Rlogin) plug;
95 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
98 * We should flush everything (aka Telnet SYNCH) if we see
99 * 0x02, and we should turn off and on _local_ flow control
100 * on 0x10 and 0x20 respectively. I'm not convinced it's
105 * Main rlogin protocol. This is really simple: the first
106 * byte is expected to be NULL and is ignored, and the rest
109 if (rlogin->firstbyte) {
110 if (data[0] == '\0') {
114 rlogin->firstbyte = 0;
117 c_write(rlogin, data, len);
122 static void rlogin_sent(Plug plug, int bufsize)
124 Rlogin rlogin = (Rlogin) plug;
125 rlogin->bufsize = bufsize;
128 static void rlogin_startup(Rlogin rlogin, const char *ruser)
133 sk_write(rlogin->s, &z, 1);
134 p = conf_get_str(rlogin->conf, CONF_localusername);
135 sk_write(rlogin->s, p, strlen(p));
136 sk_write(rlogin->s, &z, 1);
137 sk_write(rlogin->s, ruser, strlen(ruser));
138 sk_write(rlogin->s, &z, 1);
139 p = conf_get_str(rlogin->conf, CONF_termtype);
140 sk_write(rlogin->s, p, strlen(p));
141 sk_write(rlogin->s, "/", 1);
142 p = conf_get_str(rlogin->conf, CONF_termspeed);
143 sk_write(rlogin->s, p, strspn(p, "0123456789"));
144 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
146 rlogin->prompt = NULL;
150 * Called to set up the rlogin connection.
152 * Returns an error message, or NULL on success.
154 * Also places the canonical host name into `realhost'. It must be
155 * freed by the caller.
157 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
159 char *host, int port, char **realhost,
160 int nodelay, int keepalive)
162 static const struct plug_function_table fn_table = {
175 rlogin = snew(struct rlogin_tag);
176 rlogin->fn = &fn_table;
178 rlogin->frontend = frontend_handle;
179 rlogin->term_width = conf_get_int(conf, CONF_width);
180 rlogin->term_height = conf_get_int(conf, CONF_height);
181 rlogin->firstbyte = 1;
183 rlogin->prompt = NULL;
184 rlogin->conf = conf_copy(conf);
185 *backend_handle = rlogin;
187 addressfamily = conf_get_int(conf, CONF_addressfamily);
193 buf = dupprintf("Looking up host \"%s\"%s", host,
194 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
195 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
197 logevent(rlogin->frontend, buf);
200 addr = name_lookup(host, port, realhost, conf, addressfamily);
201 if ((err = sk_addr_error(addr)) != NULL) {
207 port = 513; /* default rlogin port */
212 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
213 nodelay, keepalive, (Plug) rlogin, conf);
214 if ((err = sk_socket_error(rlogin->s)) != NULL)
217 loghost = conf_get_str(conf, CONF_loghost);
222 *realhost = dupstr(loghost);
223 colon = strrchr(*realhost, ':');
226 * FIXME: if we ever update this aspect of ssh.c for
227 * IPv6 literal management, this should change in line
235 * Send local username, remote username, terminal type and
236 * terminal speed - unless we don't have the remote username yet,
237 * in which case we prompt for it and may end up deferring doing
238 * anything else until the local prompt mechanism returns.
240 if ((ruser = get_remote_username(conf)) != NULL) {
241 rlogin_startup(rlogin, ruser);
246 rlogin->prompt = new_prompts(rlogin->frontend);
247 rlogin->prompt->to_server = TRUE;
248 rlogin->prompt->name = dupstr("Rlogin login name");
249 add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE);
250 ret = get_userpass_input(rlogin->prompt, NULL, 0);
252 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
259 static void rlogin_free(void *handle)
261 Rlogin rlogin = (Rlogin) handle;
264 free_prompts(rlogin->prompt);
267 conf_free(rlogin->conf);
272 * Stub routine (we don't have any need to reconfigure this backend).
274 static void rlogin_reconfig(void *handle, Conf *conf)
279 * Called to send data down the rlogin connection.
281 static int rlogin_send(void *handle, char *buf, int len)
283 Rlogin rlogin = (Rlogin) handle;
285 if (rlogin->s == NULL)
288 if (rlogin->prompt) {
290 * We're still prompting for a username, and aren't talking
291 * directly to the network connection yet.
293 int ret = get_userpass_input(rlogin->prompt,
294 (unsigned char *)buf, len);
296 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
297 /* that nulls out rlogin->prompt, so then we'll start sending
298 * data down the wire in the obvious way */
301 rlogin->bufsize = sk_write(rlogin->s, buf, len);
304 return rlogin->bufsize;
308 * Called to query the current socket sendability status.
310 static int rlogin_sendbuffer(void *handle)
312 Rlogin rlogin = (Rlogin) handle;
313 return rlogin->bufsize;
317 * Called to set the size of the window
319 static void rlogin_size(void *handle, int width, int height)
321 Rlogin rlogin = (Rlogin) handle;
322 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
324 rlogin->term_width = width;
325 rlogin->term_height = height;
327 if (rlogin->s == NULL || !rlogin->cansize)
330 b[6] = rlogin->term_width >> 8;
331 b[7] = rlogin->term_width & 0xFF;
332 b[4] = rlogin->term_height >> 8;
333 b[5] = rlogin->term_height & 0xFF;
334 rlogin->bufsize = sk_write(rlogin->s, b, 12);
339 * Send rlogin special codes.
341 static void rlogin_special(void *handle, Telnet_Special code)
348 * Return a list of the special codes that make sense in this
351 static const struct telnet_special *rlogin_get_specials(void *handle)
356 static int rlogin_connected(void *handle)
358 Rlogin rlogin = (Rlogin) handle;
359 return rlogin->s != NULL;
362 static int rlogin_sendok(void *handle)
364 /* Rlogin rlogin = (Rlogin) handle; */
368 static void rlogin_unthrottle(void *handle, int backlog)
370 Rlogin rlogin = (Rlogin) handle;
371 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
374 static int rlogin_ldisc(void *handle, int option)
376 /* Rlogin rlogin = (Rlogin) handle; */
380 static void rlogin_provide_ldisc(void *handle, void *ldisc)
382 /* This is a stub. */
385 static void rlogin_provide_logctx(void *handle, void *logctx)
387 /* This is a stub. */
390 static int rlogin_exitcode(void *handle)
392 Rlogin rlogin = (Rlogin) handle;
393 if (rlogin->s != NULL)
394 return -1; /* still connected */
396 /* If we ever implement RSH, we'll probably need to do this properly */
401 * cfg_info for rlogin does nothing at all.
403 static int rlogin_cfg_info(void *handle)
408 Backend rlogin_backend = {
421 rlogin_provide_ldisc,
422 rlogin_provide_logctx,