18 #define RLOGIN_MAX_BACKLOG 4096
\r
20 typedef struct rlogin_tag {
\r
21 const struct plug_function_table *fn;
\r
22 /* the above field _must_ be first in the structure */
\r
28 int term_width, term_height;
\r
33 /* In case we need to read a username from the terminal before starting */
\r
37 static void rlogin_size(void *handle, int width, int height);
\r
39 static void c_write(Rlogin rlogin, char *buf, int len)
\r
41 int backlog = from_backend(rlogin->frontend, 0, buf, len);
\r
42 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
\r
45 static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
\r
46 const char *error_msg, int error_code)
\r
48 Rlogin rlogin = (Rlogin) plug;
\r
49 char addrbuf[256], *msg;
\r
51 sk_getaddr(addr, addrbuf, lenof(addrbuf));
\r
54 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
\r
56 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
\r
58 logevent(rlogin->frontend, msg);
\r
61 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
\r
64 Rlogin rlogin = (Rlogin) plug;
\r
66 sk_close(rlogin->s);
\r
68 notify_remote_exit(rlogin->frontend);
\r
71 /* A socket error has occurred. */
\r
72 logevent(rlogin->frontend, error_msg);
\r
73 connection_fatal(rlogin->frontend, "%s", error_msg);
\r
74 } /* Otherwise, the remote side closed the connection normally. */
\r
78 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
\r
80 Rlogin rlogin = (Rlogin) plug;
\r
87 rlogin->cansize = 1;
\r
88 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
\r
91 * We should flush everything (aka Telnet SYNCH) if we see
\r
92 * 0x02, and we should turn off and on _local_ flow control
\r
93 * on 0x10 and 0x20 respectively. I'm not convinced it's
\r
98 * Main rlogin protocol. This is really simple: the first
\r
99 * byte is expected to be NULL and is ignored, and the rest
\r
102 if (rlogin->firstbyte) {
\r
103 if (data[0] == '\0') {
\r
107 rlogin->firstbyte = 0;
\r
110 c_write(rlogin, data, len);
\r
115 static void rlogin_sent(Plug plug, int bufsize)
\r
117 Rlogin rlogin = (Rlogin) plug;
\r
118 rlogin->bufsize = bufsize;
\r
121 static void rlogin_startup(Rlogin rlogin, const char *ruser)
\r
125 sk_write(rlogin->s, &z, 1);
\r
126 sk_write(rlogin->s, rlogin->cfg.localusername,
\r
127 strlen(rlogin->cfg.localusername));
\r
128 sk_write(rlogin->s, &z, 1);
\r
129 sk_write(rlogin->s, ruser,
\r
131 sk_write(rlogin->s, &z, 1);
\r
132 sk_write(rlogin->s, rlogin->cfg.termtype,
\r
133 strlen(rlogin->cfg.termtype));
\r
134 sk_write(rlogin->s, "/", 1);
\r
135 for (p = rlogin->cfg.termspeed; isdigit((unsigned char)*p); p++) continue;
\r
136 sk_write(rlogin->s, rlogin->cfg.termspeed, p - rlogin->cfg.termspeed);
\r
137 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
\r
139 rlogin->prompt = NULL;
\r
143 * Called to set up the rlogin connection.
\r
145 * Returns an error message, or NULL on success.
\r
147 * Also places the canonical host name into `realhost'. It must be
\r
148 * freed by the caller.
\r
150 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
\r
152 char *host, int port, char **realhost,
\r
153 int nodelay, int keepalive)
\r
155 static const struct plug_function_table fn_table = {
\r
164 char ruser[sizeof(cfg->username)];
\r
166 rlogin = snew(struct rlogin_tag);
\r
167 rlogin->fn = &fn_table;
\r
169 rlogin->frontend = frontend_handle;
\r
170 rlogin->term_width = cfg->width;
\r
171 rlogin->term_height = cfg->height;
\r
172 rlogin->firstbyte = 1;
\r
173 rlogin->cansize = 0;
\r
174 rlogin->prompt = NULL;
\r
175 rlogin->cfg = *cfg; /* STRUCTURE COPY */
\r
176 *backend_handle = rlogin;
\r
179 * Try to find host.
\r
183 buf = dupprintf("Looking up host \"%s\"%s", host,
\r
184 (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
\r
185 (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
\r
187 logevent(rlogin->frontend, buf);
\r
190 addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily);
\r
191 if ((err = sk_addr_error(addr)) != NULL) {
\r
192 sk_addr_free(addr);
\r
197 port = 513; /* default rlogin port */
\r
202 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
\r
203 nodelay, keepalive, (Plug) rlogin, cfg);
\r
204 if ((err = sk_socket_error(rlogin->s)) != NULL)
\r
207 if (*cfg->loghost) {
\r
211 *realhost = dupstr(cfg->loghost);
\r
212 colon = strrchr(*realhost, ':');
\r
215 * FIXME: if we ever update this aspect of ssh.c for
\r
216 * IPv6 literal management, this should change in line
\r
224 * Send local username, remote username, terminal type and
\r
225 * terminal speed - unless we don't have the remote username yet,
\r
226 * in which case we prompt for it and may end up deferring doing
\r
227 * anything else until the local prompt mechanism returns.
\r
229 if (get_remote_username(cfg, ruser, sizeof(ruser))) {
\r
230 rlogin_startup(rlogin, ruser);
\r
234 rlogin->prompt = new_prompts(rlogin->frontend);
\r
235 rlogin->prompt->to_server = TRUE;
\r
236 rlogin->prompt->name = dupstr("Rlogin login name");
\r
237 add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE,
\r
238 sizeof(cfg->username));
\r
239 ret = get_userpass_input(rlogin->prompt, NULL, 0);
\r
241 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
\r
248 static void rlogin_free(void *handle)
\r
250 Rlogin rlogin = (Rlogin) handle;
\r
252 if (rlogin->prompt)
\r
253 free_prompts(rlogin->prompt);
\r
255 sk_close(rlogin->s);
\r
260 * Stub routine (we don't have any need to reconfigure this backend).
\r
262 static void rlogin_reconfig(void *handle, Config *cfg)
\r
267 * Called to send data down the rlogin connection.
\r
269 static int rlogin_send(void *handle, char *buf, int len)
\r
271 Rlogin rlogin = (Rlogin) handle;
\r
273 if (rlogin->s == NULL)
\r
276 if (rlogin->prompt) {
\r
278 * We're still prompting for a username, and aren't talking
\r
279 * directly to the network connection yet.
\r
281 int ret = get_userpass_input(rlogin->prompt,
\r
282 (unsigned char *)buf, len);
\r
284 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
\r
285 /* that nulls out rlogin->prompt, so then we'll start sending
\r
286 * data down the wire in the obvious way */
\r
289 rlogin->bufsize = sk_write(rlogin->s, buf, len);
\r
292 return rlogin->bufsize;
\r
296 * Called to query the current socket sendability status.
\r
298 static int rlogin_sendbuffer(void *handle)
\r
300 Rlogin rlogin = (Rlogin) handle;
\r
301 return rlogin->bufsize;
\r
305 * Called to set the size of the window
\r
307 static void rlogin_size(void *handle, int width, int height)
\r
309 Rlogin rlogin = (Rlogin) handle;
\r
310 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
\r
312 rlogin->term_width = width;
\r
313 rlogin->term_height = height;
\r
315 if (rlogin->s == NULL || !rlogin->cansize)
\r
318 b[6] = rlogin->term_width >> 8;
\r
319 b[7] = rlogin->term_width & 0xFF;
\r
320 b[4] = rlogin->term_height >> 8;
\r
321 b[5] = rlogin->term_height & 0xFF;
\r
322 rlogin->bufsize = sk_write(rlogin->s, b, 12);
\r
327 * Send rlogin special codes.
\r
329 static void rlogin_special(void *handle, Telnet_Special code)
\r
336 * Return a list of the special codes that make sense in this
\r
339 static const struct telnet_special *rlogin_get_specials(void *handle)
\r
344 static int rlogin_connected(void *handle)
\r
346 Rlogin rlogin = (Rlogin) handle;
\r
347 return rlogin->s != NULL;
\r
350 static int rlogin_sendok(void *handle)
\r
352 /* Rlogin rlogin = (Rlogin) handle; */
\r
356 static void rlogin_unthrottle(void *handle, int backlog)
\r
358 Rlogin rlogin = (Rlogin) handle;
\r
359 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
\r
362 static int rlogin_ldisc(void *handle, int option)
\r
364 /* Rlogin rlogin = (Rlogin) handle; */
\r
368 static void rlogin_provide_ldisc(void *handle, void *ldisc)
\r
370 /* This is a stub. */
\r
373 static void rlogin_provide_logctx(void *handle, void *logctx)
\r
375 /* This is a stub. */
\r
378 static int rlogin_exitcode(void *handle)
\r
380 Rlogin rlogin = (Rlogin) handle;
\r
381 if (rlogin->s != NULL)
\r
382 return -1; /* still connected */
\r
384 /* If we ever implement RSH, we'll probably need to do this properly */
\r
389 * cfg_info for rlogin does nothing at all.
\r
391 static int rlogin_cfg_info(void *handle)
\r
396 Backend rlogin_backend = {
\r
404 rlogin_get_specials,
\r
409 rlogin_provide_ldisc,
\r
410 rlogin_provide_logctx,
\r