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 char addrbuf
[256], *msg
;
53 sk_getaddr(addr
, addrbuf
, lenof(addrbuf
));
56 msg
= dupprintf("Connecting to %s port %d", addrbuf
, port
);
58 msg
= dupprintf("Failed to connect to %s: %s", addrbuf
, error_msg
);
60 logevent(rlogin
->frontend
, msg
);
63 static int rlogin_closing(Plug plug
, const char *error_msg
, int error_code
,
66 Rlogin rlogin
= (Rlogin
) plug
;
69 * We don't implement independent EOF in each direction for Telnet
70 * connections; as soon as we get word that the remote side has
71 * sent us EOF, we wind up the whole connection.
78 rlogin
->closed_on_socket_error
= TRUE
;
79 notify_remote_exit(rlogin
->frontend
);
82 /* A socket error has occurred. */
83 logevent(rlogin
->frontend
, error_msg
);
84 connection_fatal(rlogin
->frontend
, "%s", error_msg
);
85 } /* Otherwise, the remote side closed the connection normally. */
89 static int rlogin_receive(Plug plug
, int urgent
, char *data
, int len
)
91 Rlogin rlogin
= (Rlogin
) plug
;
99 rlogin_size(rlogin
, rlogin
->term_width
, rlogin
->term_height
);
102 * We should flush everything (aka Telnet SYNCH) if we see
103 * 0x02, and we should turn off and on _local_ flow control
104 * on 0x10 and 0x20 respectively. I'm not convinced it's
109 * Main rlogin protocol. This is really simple: the first
110 * byte is expected to be NULL and is ignored, and the rest
113 if (rlogin
->firstbyte
) {
114 if (data
[0] == '\0') {
118 rlogin
->firstbyte
= 0;
121 c_write(rlogin
, data
, len
);
126 static void rlogin_sent(Plug plug
, int bufsize
)
128 Rlogin rlogin
= (Rlogin
) plug
;
129 rlogin
->bufsize
= bufsize
;
132 static void rlogin_startup(Rlogin rlogin
, const char *ruser
)
137 sk_write(rlogin
->s
, &z
, 1);
138 p
= conf_get_str(rlogin
->conf
, CONF_localusername
);
139 sk_write(rlogin
->s
, p
, strlen(p
));
140 sk_write(rlogin
->s
, &z
, 1);
141 sk_write(rlogin
->s
, ruser
, strlen(ruser
));
142 sk_write(rlogin
->s
, &z
, 1);
143 p
= conf_get_str(rlogin
->conf
, CONF_termtype
);
144 sk_write(rlogin
->s
, p
, strlen(p
));
145 sk_write(rlogin
->s
, "/", 1);
146 p
= conf_get_str(rlogin
->conf
, CONF_termspeed
);
147 sk_write(rlogin
->s
, p
, strspn(p
, "0123456789"));
148 rlogin
->bufsize
= sk_write(rlogin
->s
, &z
, 1);
150 rlogin
->prompt
= NULL
;
154 * Called to set up the rlogin connection.
156 * Returns an error message, or NULL on success.
158 * Also places the canonical host name into `realhost'. It must be
159 * freed by the caller.
161 static const char *rlogin_init(void *frontend_handle
, void **backend_handle
,
163 char *host
, int port
, char **realhost
,
164 int nodelay
, int keepalive
)
166 static const struct plug_function_table fn_table
= {
179 rlogin
= snew(struct rlogin_tag
);
180 rlogin
->fn
= &fn_table
;
182 rlogin
->closed_on_socket_error
= FALSE
;
183 rlogin
->frontend
= frontend_handle
;
184 rlogin
->term_width
= conf_get_int(conf
, CONF_width
);
185 rlogin
->term_height
= conf_get_int(conf
, CONF_height
);
186 rlogin
->firstbyte
= 1;
188 rlogin
->prompt
= NULL
;
189 rlogin
->conf
= conf_copy(conf
);
190 *backend_handle
= rlogin
;
192 addressfamily
= conf_get_int(conf
, CONF_addressfamily
);
198 buf
= dupprintf("Looking up host \"%s\"%s", host
,
199 (addressfamily
== ADDRTYPE_IPV4 ?
" (IPv4)" :
200 (addressfamily
== ADDRTYPE_IPV6 ?
" (IPv6)" :
202 logevent(rlogin
->frontend
, buf
);
205 addr
= name_lookup(host
, port
, realhost
, conf
, addressfamily
);
206 if ((err
= sk_addr_error(addr
)) != NULL
) {
212 port
= 513; /* default rlogin port */
217 rlogin
->s
= new_connection(addr
, *realhost
, port
, 1, 0,
218 nodelay
, keepalive
, (Plug
) rlogin
, conf
);
219 if ((err
= sk_socket_error(rlogin
->s
)) != NULL
)
222 loghost
= conf_get_str(conf
, CONF_loghost
);
227 *realhost
= dupstr(loghost
);
228 colon
= strrchr(*realhost
, ':');
231 * FIXME: if we ever update this aspect of ssh.c for
232 * IPv6 literal management, this should change in line
240 * Send local username, remote username, terminal type and
241 * terminal speed - unless we don't have the remote username yet,
242 * in which case we prompt for it and may end up deferring doing
243 * anything else until the local prompt mechanism returns.
245 if ((ruser
= get_remote_username(conf
)) != NULL
) {
246 rlogin_startup(rlogin
, ruser
);
251 rlogin
->prompt
= new_prompts(rlogin
->frontend
);
252 rlogin
->prompt
->to_server
= TRUE
;
253 rlogin
->prompt
->name
= dupstr("Rlogin login name");
254 add_prompt(rlogin
->prompt
, dupstr("rlogin username: "), TRUE
);
255 ret
= get_userpass_input(rlogin
->prompt
, NULL
, 0);
257 rlogin_startup(rlogin
, rlogin
->prompt
->prompts
[0]->result
);
264 static void rlogin_free(void *handle
)
266 Rlogin rlogin
= (Rlogin
) handle
;
269 free_prompts(rlogin
->prompt
);
272 conf_free(rlogin
->conf
);
277 * Stub routine (we don't have any need to reconfigure this backend).
279 static void rlogin_reconfig(void *handle
, Conf
*conf
)
284 * Called to send data down the rlogin connection.
286 static int rlogin_send(void *handle
, char *buf
, int len
)
288 Rlogin rlogin
= (Rlogin
) handle
;
290 if (rlogin
->s
== NULL
)
293 if (rlogin
->prompt
) {
295 * We're still prompting for a username, and aren't talking
296 * directly to the network connection yet.
298 int ret
= get_userpass_input(rlogin
->prompt
,
299 (unsigned char *)buf
, len
);
301 rlogin_startup(rlogin
, rlogin
->prompt
->prompts
[0]->result
);
302 /* that nulls out rlogin->prompt, so then we'll start sending
303 * data down the wire in the obvious way */
306 rlogin
->bufsize
= sk_write(rlogin
->s
, buf
, len
);
309 return rlogin
->bufsize
;
313 * Called to query the current socket sendability status.
315 static int rlogin_sendbuffer(void *handle
)
317 Rlogin rlogin
= (Rlogin
) handle
;
318 return rlogin
->bufsize
;
322 * Called to set the size of the window
324 static void rlogin_size(void *handle
, int width
, int height
)
326 Rlogin rlogin
= (Rlogin
) handle
;
327 char b
[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 };
329 rlogin
->term_width
= width
;
330 rlogin
->term_height
= height
;
332 if (rlogin
->s
== NULL
|| !rlogin
->cansize
)
335 b
[6] = rlogin
->term_width
>> 8;
336 b
[7] = rlogin
->term_width
& 0xFF;
337 b
[4] = rlogin
->term_height
>> 8;
338 b
[5] = rlogin
->term_height
& 0xFF;
339 rlogin
->bufsize
= sk_write(rlogin
->s
, b
, 12);
344 * Send rlogin special codes.
346 static void rlogin_special(void *handle
, Telnet_Special code
)
353 * Return a list of the special codes that make sense in this
356 static const struct telnet_special
*rlogin_get_specials(void *handle
)
361 static int rlogin_connected(void *handle
)
363 Rlogin rlogin
= (Rlogin
) handle
;
364 return rlogin
->s
!= NULL
;
367 static int rlogin_sendok(void *handle
)
369 /* Rlogin rlogin = (Rlogin) handle; */
373 static void rlogin_unthrottle(void *handle
, int backlog
)
375 Rlogin rlogin
= (Rlogin
) handle
;
376 sk_set_frozen(rlogin
->s
, backlog
> RLOGIN_MAX_BACKLOG
);
379 static int rlogin_ldisc(void *handle
, int option
)
381 /* Rlogin rlogin = (Rlogin) handle; */
385 static void rlogin_provide_ldisc(void *handle
, void *ldisc
)
387 /* This is a stub. */
390 static void rlogin_provide_logctx(void *handle
, void *logctx
)
392 /* This is a stub. */
395 static int rlogin_exitcode(void *handle
)
397 Rlogin rlogin
= (Rlogin
) handle
;
398 if (rlogin
->s
!= NULL
)
399 return -1; /* still connected */
400 else if (rlogin
->closed_on_socket_error
)
401 return INT_MAX
; /* a socket error counts as an unclean exit */
403 /* If we ever implement RSH, we'll probably need to do this properly */
408 * cfg_info for rlogin does nothing at all.
410 static int rlogin_cfg_info(void *handle
)
415 Backend rlogin_backend
= {
428 rlogin_provide_ldisc
,
429 rlogin_provide_logctx
,