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)
125 sk_write(rlogin->s, &z, 1);
126 sk_write(rlogin->s, rlogin->cfg.localusername,
127 strlen(rlogin->cfg.localusername));
128 sk_write(rlogin->s, &z, 1);
129 sk_write(rlogin->s, ruser,
131 sk_write(rlogin->s, &z, 1);
132 sk_write(rlogin->s, rlogin->cfg.termtype,
133 strlen(rlogin->cfg.termtype));
134 sk_write(rlogin->s, "/", 1);
135 for (p = rlogin->cfg.termspeed; isdigit((unsigned char)*p); p++) continue;
136 sk_write(rlogin->s, rlogin->cfg.termspeed, p - rlogin->cfg.termspeed);
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 = {
164 char ruser[sizeof(cfg->username)];
166 rlogin = snew(struct rlogin_tag);
167 rlogin->fn = &fn_table;
169 rlogin->frontend = frontend_handle;
170 rlogin->term_width = cfg->width;
171 rlogin->term_height = cfg->height;
172 rlogin->firstbyte = 1;
174 rlogin->prompt = NULL;
175 rlogin->cfg = *cfg; /* STRUCTURE COPY */
176 *backend_handle = rlogin;
183 buf = dupprintf("Looking up host \"%s\"%s", host,
184 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
185 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
187 logevent(rlogin->frontend, buf);
190 addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
191 if ((err = sk_addr_error(addr)) != NULL) {
197 port = 513; /* default rlogin port */
202 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
203 nodelay, keepalive, (Plug) rlogin, cfg);
204 if ((err = sk_socket_error(rlogin->s)) != NULL)
211 *realhost = dupstr(cfg->loghost);
212 colon = strrchr(*realhost, ':');
215 * FIXME: if we ever update this aspect of ssh.c for
216 * IPv6 literal management, this should change in line
224 * Send local username, remote username, terminal type and
225 * terminal speed - unless we don't have the remote username yet,
226 * in which case we prompt for it and may end up deferring doing
227 * anything else until the local prompt mechanism returns.
229 if (get_remote_username(cfg, ruser, sizeof(ruser))) {
230 rlogin_startup(rlogin, ruser);
234 rlogin->prompt = new_prompts(rlogin->frontend);
235 rlogin->prompt->to_server = TRUE;
236 rlogin->prompt->name = dupstr("Rlogin login name");
237 add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE,
238 sizeof(cfg->username));
239 ret = get_userpass_input(rlogin->prompt, NULL, 0);
241 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
248 static void rlogin_free(void *handle)
250 Rlogin rlogin = (Rlogin) handle;
253 free_prompts(rlogin->prompt);
260 * Stub routine (we don't have any need to reconfigure this backend).
262 static void rlogin_reconfig(void *handle, Config *cfg)
267 * Called to send data down the rlogin connection.
269 static int rlogin_send(void *handle, char *buf, int len)
271 Rlogin rlogin = (Rlogin) handle;
273 if (rlogin->s == NULL)
276 if (rlogin->prompt) {
278 * We're still prompting for a username, and aren't talking
279 * directly to the network connection yet.
281 int ret = get_userpass_input(rlogin->prompt,
282 (unsigned char *)buf, len);
284 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
285 /* that nulls out rlogin->prompt, so then we'll start sending
286 * data down the wire in the obvious way */
289 rlogin->bufsize = sk_write(rlogin->s, buf, len);
292 return rlogin->bufsize;
296 * Called to query the current socket sendability status.
298 static int rlogin_sendbuffer(void *handle)
300 Rlogin rlogin = (Rlogin) handle;
301 return rlogin->bufsize;
305 * Called to set the size of the window
307 static void rlogin_size(void *handle, int width, int height)
309 Rlogin rlogin = (Rlogin) handle;
310 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
312 rlogin->term_width = width;
313 rlogin->term_height = height;
315 if (rlogin->s == NULL || !rlogin->cansize)
318 b[6] = rlogin->term_width >> 8;
319 b[7] = rlogin->term_width & 0xFF;
320 b[4] = rlogin->term_height >> 8;
321 b[5] = rlogin->term_height & 0xFF;
322 rlogin->bufsize = sk_write(rlogin->s, b, 12);
327 * Send rlogin special codes.
329 static void rlogin_special(void *handle, Telnet_Special code)
336 * Return a list of the special codes that make sense in this
339 static const struct telnet_special *rlogin_get_specials(void *handle)
344 static int rlogin_connected(void *handle)
346 Rlogin rlogin = (Rlogin) handle;
347 return rlogin->s != NULL;
350 static int rlogin_sendok(void *handle)
352 /* Rlogin rlogin = (Rlogin) handle; */
356 static void rlogin_unthrottle(void *handle, int backlog)
358 Rlogin rlogin = (Rlogin) handle;
359 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
362 static int rlogin_ldisc(void *handle, int option)
364 /* Rlogin rlogin = (Rlogin) handle; */
368 static void rlogin_provide_ldisc(void *handle, void *ldisc)
370 /* This is a stub. */
373 static void rlogin_provide_logctx(void *handle, void *logctx)
375 /* This is a stub. */
378 static int rlogin_exitcode(void *handle)
380 Rlogin rlogin = (Rlogin) handle;
381 if (rlogin->s != NULL)
382 return -1; /* still connected */
384 /* If we ever implement RSH, we'll probably need to do this properly */
389 * cfg_info for rlogin does nothing at all.
391 static int rlogin_cfg_info(void *handle)
396 Backend rlogin_backend = {
409 rlogin_provide_ldisc,
410 rlogin_provide_logctx,