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 backend_socket_log(rlogin->frontend, type, addr, port,
52 error_msg, error_code);
55 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
58 Rlogin rlogin = (Rlogin) plug;
61 * We don't implement independent EOF in each direction for Telnet
62 * connections; as soon as we get word that the remote side has
63 * sent us EOF, we wind up the whole connection.
70 rlogin->closed_on_socket_error = TRUE;
71 notify_remote_exit(rlogin->frontend);
74 /* A socket error has occurred. */
75 logevent(rlogin->frontend, error_msg);
76 connection_fatal(rlogin->frontend, "%s", error_msg);
77 } /* Otherwise, the remote side closed the connection normally. */
81 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
83 Rlogin rlogin = (Rlogin) plug;
91 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
94 * We should flush everything (aka Telnet SYNCH) if we see
95 * 0x02, and we should turn off and on _local_ flow control
96 * on 0x10 and 0x20 respectively. I'm not convinced it's
101 * Main rlogin protocol. This is really simple: the first
102 * byte is expected to be NULL and is ignored, and the rest
105 if (rlogin->firstbyte) {
106 if (data[0] == '\0') {
110 rlogin->firstbyte = 0;
113 c_write(rlogin, data, len);
118 static void rlogin_sent(Plug plug, int bufsize)
120 Rlogin rlogin = (Rlogin) plug;
121 rlogin->bufsize = bufsize;
124 static void rlogin_startup(Rlogin rlogin, const char *ruser)
129 sk_write(rlogin->s, &z, 1);
130 p = conf_get_str(rlogin->conf, CONF_localusername);
131 sk_write(rlogin->s, p, strlen(p));
132 sk_write(rlogin->s, &z, 1);
133 sk_write(rlogin->s, ruser, strlen(ruser));
134 sk_write(rlogin->s, &z, 1);
135 p = conf_get_str(rlogin->conf, CONF_termtype);
136 sk_write(rlogin->s, p, strlen(p));
137 sk_write(rlogin->s, "/", 1);
138 p = conf_get_str(rlogin->conf, CONF_termspeed);
139 sk_write(rlogin->s, p, strspn(p, "0123456789"));
140 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
142 rlogin->prompt = NULL;
146 * Called to set up the rlogin connection.
148 * Returns an error message, or NULL on success.
150 * Also places the canonical host name into `realhost'. It must be
151 * freed by the caller.
153 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
155 const char *host, int port, char **realhost,
156 int nodelay, int keepalive)
158 static const struct plug_function_table fn_table = {
171 rlogin = snew(struct rlogin_tag);
172 rlogin->fn = &fn_table;
174 rlogin->closed_on_socket_error = FALSE;
175 rlogin->frontend = frontend_handle;
176 rlogin->term_width = conf_get_int(conf, CONF_width);
177 rlogin->term_height = conf_get_int(conf, CONF_height);
178 rlogin->firstbyte = 1;
180 rlogin->prompt = NULL;
181 rlogin->conf = conf_copy(conf);
182 *backend_handle = rlogin;
184 addressfamily = conf_get_int(conf, CONF_addressfamily);
188 addr = name_lookup(host, port, realhost, conf, addressfamily,
189 rlogin->frontend, "rlogin connection");
190 if ((err = sk_addr_error(addr)) != NULL) {
196 port = 513; /* default rlogin port */
201 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
202 nodelay, keepalive, (Plug) rlogin, conf);
203 if ((err = sk_socket_error(rlogin->s)) != NULL)
206 loghost = conf_get_str(conf, CONF_loghost);
211 *realhost = dupstr(loghost);
213 colon = host_strrchr(*realhost, ':');
219 * Send local username, remote username, terminal type and
220 * terminal speed - unless we don't have the remote username yet,
221 * in which case we prompt for it and may end up deferring doing
222 * anything else until the local prompt mechanism returns.
224 if ((ruser = get_remote_username(conf)) != NULL) {
225 rlogin_startup(rlogin, ruser);
230 rlogin->prompt = new_prompts(rlogin->frontend);
231 rlogin->prompt->to_server = TRUE;
232 rlogin->prompt->name = dupstr("Rlogin login name");
233 add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE);
234 ret = get_userpass_input(rlogin->prompt, NULL, 0);
236 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
243 static void rlogin_free(void *handle)
245 Rlogin rlogin = (Rlogin) handle;
248 free_prompts(rlogin->prompt);
251 conf_free(rlogin->conf);
256 * Stub routine (we don't have any need to reconfigure this backend).
258 static void rlogin_reconfig(void *handle, Conf *conf)
263 * Called to send data down the rlogin connection.
265 static int rlogin_send(void *handle, const char *buf, int len)
267 Rlogin rlogin = (Rlogin) handle;
269 if (rlogin->s == NULL)
272 if (rlogin->prompt) {
274 * We're still prompting for a username, and aren't talking
275 * directly to the network connection yet.
277 int ret = get_userpass_input(rlogin->prompt,
278 (unsigned char *)buf, len);
280 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
281 /* that nulls out rlogin->prompt, so then we'll start sending
282 * data down the wire in the obvious way */
285 rlogin->bufsize = sk_write(rlogin->s, buf, len);
288 return rlogin->bufsize;
292 * Called to query the current socket sendability status.
294 static int rlogin_sendbuffer(void *handle)
296 Rlogin rlogin = (Rlogin) handle;
297 return rlogin->bufsize;
301 * Called to set the size of the window
303 static void rlogin_size(void *handle, int width, int height)
305 Rlogin rlogin = (Rlogin) handle;
306 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
308 rlogin->term_width = width;
309 rlogin->term_height = height;
311 if (rlogin->s == NULL || !rlogin->cansize)
314 b[6] = rlogin->term_width >> 8;
315 b[7] = rlogin->term_width & 0xFF;
316 b[4] = rlogin->term_height >> 8;
317 b[5] = rlogin->term_height & 0xFF;
318 rlogin->bufsize = sk_write(rlogin->s, b, 12);
323 * Send rlogin special codes.
325 static void rlogin_special(void *handle, Telnet_Special code)
332 * Return a list of the special codes that make sense in this
335 static const struct telnet_special *rlogin_get_specials(void *handle)
340 static int rlogin_connected(void *handle)
342 Rlogin rlogin = (Rlogin) handle;
343 return rlogin->s != NULL;
346 static int rlogin_sendok(void *handle)
348 /* Rlogin rlogin = (Rlogin) handle; */
352 static void rlogin_unthrottle(void *handle, int backlog)
354 Rlogin rlogin = (Rlogin) handle;
355 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
358 static int rlogin_ldisc(void *handle, int option)
360 /* Rlogin rlogin = (Rlogin) handle; */
364 static void rlogin_provide_ldisc(void *handle, void *ldisc)
366 /* This is a stub. */
369 static void rlogin_provide_logctx(void *handle, void *logctx)
371 /* This is a stub. */
374 static int rlogin_exitcode(void *handle)
376 Rlogin rlogin = (Rlogin) handle;
377 if (rlogin->s != NULL)
378 return -1; /* still connected */
379 else if (rlogin->closed_on_socket_error)
380 return INT_MAX; /* a socket error counts as an unclean exit */
382 /* If we ever implement RSH, we'll probably need to do this properly */
387 * cfg_info for rlogin does nothing at all.
389 static int rlogin_cfg_info(void *handle)
394 Backend rlogin_backend = {
407 rlogin_provide_ldisc,
408 rlogin_provide_logctx,
411 NULL /* test_for_upstream */,