19 #define RLOGIN_MAX_BACKLOG 4096
\r
21 typedef struct rlogin_tag {
\r
22 const struct plug_function_table *fn;
\r
23 /* the above field _must_ be first in the structure */
\r
26 int closed_on_socket_error;
\r
30 int term_width, term_height;
\r
35 /* In case we need to read a username from the terminal before starting */
\r
39 static void rlogin_size(void *handle, int width, int height);
\r
41 static void c_write(Rlogin rlogin, char *buf, int len)
\r
43 int backlog = from_backend(rlogin->frontend, 0, buf, len);
\r
44 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
\r
47 static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
\r
48 const char *error_msg, int error_code)
\r
50 Rlogin rlogin = (Rlogin) plug;
\r
51 char addrbuf[256], *msg;
\r
53 sk_getaddr(addr, addrbuf, lenof(addrbuf));
\r
56 msg = dupprintf("Connecting to %s port %d", addrbuf, port);
\r
58 msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
\r
60 logevent(rlogin->frontend, msg);
\r
64 static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
\r
67 Rlogin rlogin = (Rlogin) plug;
\r
70 * We don't implement independent EOF in each direction for Telnet
\r
71 * connections; as soon as we get word that the remote side has
\r
72 * sent us EOF, we wind up the whole connection.
\r
76 sk_close(rlogin->s);
\r
79 rlogin->closed_on_socket_error = TRUE;
\r
80 notify_remote_exit(rlogin->frontend);
\r
83 /* A socket error has occurred. */
\r
84 logevent(rlogin->frontend, error_msg);
\r
85 connection_fatal(rlogin->frontend, "%s", error_msg);
\r
86 } /* Otherwise, the remote side closed the connection normally. */
\r
90 static int rlogin_receive(Plug plug, int urgent, char *data, int len)
\r
92 Rlogin rlogin = (Rlogin) plug;
\r
99 rlogin->cansize = 1;
\r
100 rlogin_size(rlogin, rlogin->term_width, rlogin->term_height);
\r
103 * We should flush everything (aka Telnet SYNCH) if we see
\r
104 * 0x02, and we should turn off and on _local_ flow control
\r
105 * on 0x10 and 0x20 respectively. I'm not convinced it's
\r
110 * Main rlogin protocol. This is really simple: the first
\r
111 * byte is expected to be NULL and is ignored, and the rest
\r
114 if (rlogin->firstbyte) {
\r
115 if (data[0] == '\0') {
\r
119 rlogin->firstbyte = 0;
\r
122 c_write(rlogin, data, len);
\r
127 static void rlogin_sent(Plug plug, int bufsize)
\r
129 Rlogin rlogin = (Rlogin) plug;
\r
130 rlogin->bufsize = bufsize;
\r
133 static void rlogin_startup(Rlogin rlogin, const char *ruser)
\r
138 sk_write(rlogin->s, &z, 1);
\r
139 p = conf_get_str(rlogin->conf, CONF_localusername);
\r
140 sk_write(rlogin->s, p, strlen(p));
\r
141 sk_write(rlogin->s, &z, 1);
\r
142 sk_write(rlogin->s, ruser, strlen(ruser));
\r
143 sk_write(rlogin->s, &z, 1);
\r
144 p = conf_get_str(rlogin->conf, CONF_termtype);
\r
145 sk_write(rlogin->s, p, strlen(p));
\r
146 sk_write(rlogin->s, "/", 1);
\r
147 p = conf_get_str(rlogin->conf, CONF_termspeed);
\r
148 sk_write(rlogin->s, p, strspn(p, "0123456789"));
\r
149 rlogin->bufsize = sk_write(rlogin->s, &z, 1);
\r
151 rlogin->prompt = NULL;
\r
155 * Called to set up the rlogin connection.
\r
157 * Returns an error message, or NULL on success.
\r
159 * Also places the canonical host name into `realhost'. It must be
\r
160 * freed by the caller.
\r
162 static const char *rlogin_init(void *frontend_handle, void **backend_handle,
\r
164 char *host, int port, char **realhost,
\r
165 int nodelay, int keepalive)
\r
167 static const struct plug_function_table fn_table = {
\r
180 rlogin = snew(struct rlogin_tag);
\r
181 rlogin->fn = &fn_table;
\r
183 rlogin->closed_on_socket_error = FALSE;
\r
184 rlogin->frontend = frontend_handle;
\r
185 rlogin->term_width = conf_get_int(conf, CONF_width);
\r
186 rlogin->term_height = conf_get_int(conf, CONF_height);
\r
187 rlogin->firstbyte = 1;
\r
188 rlogin->cansize = 0;
\r
189 rlogin->prompt = NULL;
\r
190 rlogin->conf = conf_copy(conf);
\r
191 *backend_handle = rlogin;
\r
193 addressfamily = conf_get_int(conf, CONF_addressfamily);
\r
195 * Try to find host.
\r
199 buf = dupprintf("Looking up host \"%s\"%s", host,
\r
200 (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
\r
201 (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
\r
203 logevent(rlogin->frontend, buf);
\r
206 addr = name_lookup(host, port, realhost, conf, addressfamily);
\r
207 if ((err = sk_addr_error(addr)) != NULL) {
\r
208 sk_addr_free(addr);
\r
213 port = 513; /* default rlogin port */
\r
218 rlogin->s = new_connection(addr, *realhost, port, 1, 0,
\r
219 nodelay, keepalive, (Plug) rlogin, conf);
\r
220 if ((err = sk_socket_error(rlogin->s)) != NULL)
\r
223 loghost = conf_get_str(conf, CONF_loghost);
\r
228 *realhost = dupstr(loghost);
\r
230 colon = host_strrchr(*realhost, ':');
\r
236 * Send local username, remote username, terminal type and
\r
237 * terminal speed - unless we don't have the remote username yet,
\r
238 * in which case we prompt for it and may end up deferring doing
\r
239 * anything else until the local prompt mechanism returns.
\r
241 if ((ruser = get_remote_username(conf)) != NULL) {
\r
242 rlogin_startup(rlogin, ruser);
\r
247 rlogin->prompt = new_prompts(rlogin->frontend);
\r
248 rlogin->prompt->to_server = TRUE;
\r
249 rlogin->prompt->name = dupstr("Rlogin login name");
\r
250 add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE);
\r
251 ret = get_userpass_input(rlogin->prompt, NULL, 0);
\r
253 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
\r
260 static void rlogin_free(void *handle)
\r
262 Rlogin rlogin = (Rlogin) handle;
\r
264 if (rlogin->prompt)
\r
265 free_prompts(rlogin->prompt);
\r
267 sk_close(rlogin->s);
\r
268 conf_free(rlogin->conf);
\r
273 * Stub routine (we don't have any need to reconfigure this backend).
\r
275 static void rlogin_reconfig(void *handle, Conf *conf)
\r
280 * Called to send data down the rlogin connection.
\r
282 static int rlogin_send(void *handle, char *buf, int len)
\r
284 Rlogin rlogin = (Rlogin) handle;
\r
286 if (rlogin->s == NULL)
\r
289 if (rlogin->prompt) {
\r
291 * We're still prompting for a username, and aren't talking
\r
292 * directly to the network connection yet.
\r
294 int ret = get_userpass_input(rlogin->prompt,
\r
295 (unsigned char *)buf, len);
\r
297 rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result);
\r
298 /* that nulls out rlogin->prompt, so then we'll start sending
\r
299 * data down the wire in the obvious way */
\r
302 rlogin->bufsize = sk_write(rlogin->s, buf, len);
\r
305 return rlogin->bufsize;
\r
309 * Called to query the current socket sendability status.
\r
311 static int rlogin_sendbuffer(void *handle)
\r
313 Rlogin rlogin = (Rlogin) handle;
\r
314 return rlogin->bufsize;
\r
318 * Called to set the size of the window
\r
320 static void rlogin_size(void *handle, int width, int height)
\r
322 Rlogin rlogin = (Rlogin) handle;
\r
323 char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
\r
325 rlogin->term_width = width;
\r
326 rlogin->term_height = height;
\r
328 if (rlogin->s == NULL || !rlogin->cansize)
\r
331 b[6] = rlogin->term_width >> 8;
\r
332 b[7] = rlogin->term_width & 0xFF;
\r
333 b[4] = rlogin->term_height >> 8;
\r
334 b[5] = rlogin->term_height & 0xFF;
\r
335 rlogin->bufsize = sk_write(rlogin->s, b, 12);
\r
340 * Send rlogin special codes.
\r
342 static void rlogin_special(void *handle, Telnet_Special code)
\r
349 * Return a list of the special codes that make sense in this
\r
352 static const struct telnet_special *rlogin_get_specials(void *handle)
\r
357 static int rlogin_connected(void *handle)
\r
359 Rlogin rlogin = (Rlogin) handle;
\r
360 return rlogin->s != NULL;
\r
363 static int rlogin_sendok(void *handle)
\r
365 /* Rlogin rlogin = (Rlogin) handle; */
\r
369 static void rlogin_unthrottle(void *handle, int backlog)
\r
371 Rlogin rlogin = (Rlogin) handle;
\r
372 sk_set_frozen(rlogin->s, backlog > RLOGIN_MAX_BACKLOG);
\r
375 static int rlogin_ldisc(void *handle, int option)
\r
377 /* Rlogin rlogin = (Rlogin) handle; */
\r
381 static void rlogin_provide_ldisc(void *handle, void *ldisc)
\r
383 /* This is a stub. */
\r
386 static void rlogin_provide_logctx(void *handle, void *logctx)
\r
388 /* This is a stub. */
\r
391 static int rlogin_exitcode(void *handle)
\r
393 Rlogin rlogin = (Rlogin) handle;
\r
394 if (rlogin->s != NULL)
\r
395 return -1; /* still connected */
\r
396 else if (rlogin->closed_on_socket_error)
\r
397 return INT_MAX; /* a socket error counts as an unclean exit */
\r
399 /* If we ever implement RSH, we'll probably need to do this properly */
\r
404 * cfg_info for rlogin does nothing at all.
\r
406 static int rlogin_cfg_info(void *handle)
\r
411 Backend rlogin_backend = {
\r
419 rlogin_get_specials,
\r
424 rlogin_provide_ldisc,
\r
425 rlogin_provide_logctx,
\r