14 static Socket s
= NULL
;
16 #define IAC 255 /* interpret as command: */
17 #define DONT 254 /* you are not to use option */
18 #define DO 253 /* please, you use option */
19 #define WONT 252 /* I won't use option */
20 #define WILL 251 /* I will use option */
21 #define SB 250 /* interpret as subnegotiation */
22 #define SE 240 /* end sub negotiation */
24 #define GA 249 /* you may reverse the line */
25 #define EL 248 /* erase the current line */
26 #define EC 247 /* erase the current character */
27 #define AYT 246 /* are you there */
28 #define AO 245 /* abort output--but let prog finish */
29 #define IP 244 /* interrupt process--permanently */
30 #define BREAK 243 /* break */
31 #define DM 242 /* data mark--for connect. cleaning */
32 #define NOP 241 /* nop */
33 #define EOR 239 /* end of record (transparent mode) */
34 #define ABORT 238 /* Abort process */
35 #define SUSP 237 /* Suspend process */
36 #define xEOF 236 /* End of file: EOF is already used... */
38 #define TELOPT_BINARY 0 /* 8-bit data path */
39 #define TELOPT_ECHO 1 /* echo */
40 #define TELOPT_RCP 2 /* prepare to reconnect */
41 #define TELOPT_SGA 3 /* suppress go ahead */
42 #define TELOPT_NAMS 4 /* approximate message size */
43 #define TELOPT_STATUS 5 /* give status */
44 #define TELOPT_TM 6 /* timing mark */
45 #define TELOPT_RCTE 7 /* remote controlled transmission and echo */
46 #define TELOPT_NAOL 8 /* negotiate about output line width */
47 #define TELOPT_NAOP 9 /* negotiate about output page size */
48 #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */
49 #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */
50 #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */
51 #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */
52 #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */
53 #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */
54 #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */
55 #define TELOPT_XASCII 17 /* extended ascic character set */
56 #define TELOPT_LOGOUT 18 /* force logout */
57 #define TELOPT_BM 19 /* byte macro */
58 #define TELOPT_DET 20 /* data entry terminal */
59 #define TELOPT_SUPDUP 21 /* supdup protocol */
60 #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */
61 #define TELOPT_SNDLOC 23 /* send location */
62 #define TELOPT_TTYPE 24 /* terminal type */
63 #define TELOPT_EOR 25 /* end or record */
64 #define TELOPT_TUID 26 /* TACACS user identification */
65 #define TELOPT_OUTMRK 27 /* output marking */
66 #define TELOPT_TTYLOC 28 /* terminal location number */
67 #define TELOPT_3270REGIME 29 /* 3270 regime */
68 #define TELOPT_X3PAD 30 /* X.3 PAD */
69 #define TELOPT_NAWS 31 /* window size */
70 #define TELOPT_TSPEED 32 /* terminal speed */
71 #define TELOPT_LFLOW 33 /* remote flow control */
72 #define TELOPT_LINEMODE 34 /* Linemode option */
73 #define TELOPT_XDISPLOC 35 /* X Display Location */
74 #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */
75 #define TELOPT_AUTHENTICATION 37 /* Authenticate */
76 #define TELOPT_ENCRYPT 38 /* Encryption option */
77 #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */
78 #define TELOPT_EXOPL 255 /* extended-options-list */
80 #define TELQUAL_IS 0 /* option is... */
81 #define TELQUAL_SEND 1 /* send option */
82 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
92 #define iswritable(x) ( (x) != IAC && (x) != CR )
94 static char *telopt(int opt
)
96 #define i(x) if (opt == TELOPT_ ## x) return #x;
141 static void telnet_size(void);
144 int send
; /* what we initially send */
145 int nsend
; /* -ve send if requested to stop it */
146 int ack
, nak
; /* +ve and -ve acknowledgements */
147 int option
; /* the option code */
149 REQUESTED
, ACTIVE
, INACTIVE
, REALLY_INACTIVE
153 static struct Opt o_naws
=
154 { WILL
, WONT
, DO
, DONT
, TELOPT_NAWS
, REQUESTED
};
155 static struct Opt o_tspeed
=
156 { WILL
, WONT
, DO
, DONT
, TELOPT_TSPEED
, REQUESTED
};
157 static struct Opt o_ttype
=
158 { WILL
, WONT
, DO
, DONT
, TELOPT_TTYPE
, REQUESTED
};
159 static struct Opt o_oenv
= { WILL
, WONT
, DO
, DONT
, TELOPT_OLD_ENVIRON
,
162 static struct Opt o_nenv
= { WILL
, WONT
, DO
, DONT
, TELOPT_NEW_ENVIRON
,
165 static struct Opt o_echo
=
166 { DO
, DONT
, WILL
, WONT
, TELOPT_ECHO
, REQUESTED
};
167 static struct Opt o_we_sga
=
168 { WILL
, WONT
, DO
, DONT
, TELOPT_SGA
, REQUESTED
};
169 static struct Opt o_they_sga
=
170 { DO
, DONT
, WILL
, WONT
, TELOPT_SGA
, REQUESTED
};
172 static struct Opt
*opts
[] = {
173 &o_naws
, &o_tspeed
, &o_ttype
, &o_oenv
, &o_nenv
, &o_echo
,
174 &o_we_sga
, &o_they_sga
, NULL
177 static int echoing
= TRUE
, editing
= TRUE
;
178 static int activated
= FALSE
;
181 static int sb_opt
, sb_len
;
182 static char *sb_buf
= NULL
;
183 static int sb_size
= 0;
184 #define SB_DELTA 1024
186 static void c_write1(int c
)
189 from_backend(0, &cc
, 1);
192 static void log_option(char *sender
, int cmd
, int option
)
195 sprintf(buf
, "%s:\t%s %s", sender
,
196 (cmd
== WILL ?
"WILL" : cmd
== WONT ?
"WONT" :
197 cmd
== DO ?
"DO" : cmd
== DONT ?
"DONT" : "<??>"),
202 static void send_opt(int cmd
, int option
)
210 log_option("client", cmd
, option
);
213 static void deactivate_option(struct Opt
*o
)
215 if (o
->state
== REQUESTED
|| o
->state
== ACTIVE
)
216 send_opt(o
->nsend
, o
->option
);
217 o
->state
= REALLY_INACTIVE
;
221 * Generate side effects of enabling or disabling an option.
223 static void option_side_effects(struct Opt
*o
, int enabled
)
225 if (o
->option
== TELOPT_ECHO
&& o
->send
== DO
)
227 else if (o
->option
== TELOPT_SGA
&& o
->send
== DO
)
229 ldisc_send(NULL
, 0); /* cause ldisc to notice the change */
231 /* Ensure we get the minimum options */
233 if (o_echo
.state
== INACTIVE
) {
234 o_echo
.state
= REQUESTED
;
235 send_opt(o_echo
.send
, o_echo
.option
);
237 if (o_we_sga
.state
== INACTIVE
) {
238 o_we_sga
.state
= REQUESTED
;
239 send_opt(o_we_sga
.send
, o_we_sga
.option
);
241 if (o_they_sga
.state
== INACTIVE
) {
242 o_they_sga
.state
= REQUESTED
;
243 send_opt(o_they_sga
.send
, o_they_sga
.option
);
249 static void activate_option(struct Opt
*o
)
251 if (o
->send
== WILL
&& o
->option
== TELOPT_NAWS
)
253 if (o
->send
== WILL
&&
254 (o
->option
== TELOPT_NEW_ENVIRON
||
255 o
->option
== TELOPT_OLD_ENVIRON
)) {
257 * We may only have one kind of ENVIRON going at a time.
258 * This is a hack, but who cares.
260 deactivate_option(o
->option
==
261 TELOPT_NEW_ENVIRON ?
&o_oenv
: &o_nenv
);
263 option_side_effects(o
, 1);
266 static void refused_option(struct Opt
*o
)
268 if (o
->send
== WILL
&& o
->option
== TELOPT_NEW_ENVIRON
&&
269 o_oenv
.state
== INACTIVE
) {
270 send_opt(WILL
, TELOPT_OLD_ENVIRON
);
271 o_oenv
.state
= REQUESTED
;
273 option_side_effects(o
, 0);
276 static void proc_rec_opt(int cmd
, int option
)
280 log_option("server", cmd
, option
);
281 for (o
= opts
; *o
; o
++) {
282 if ((*o
)->option
== option
&& (*o
)->ack
== cmd
) {
283 switch ((*o
)->state
) {
285 (*o
)->state
= ACTIVE
;
291 (*o
)->state
= ACTIVE
;
292 send_opt((*o
)->send
, option
);
295 case REALLY_INACTIVE
:
296 send_opt((*o
)->nsend
, option
);
300 } else if ((*o
)->option
== option
&& (*o
)->nak
== cmd
) {
301 switch ((*o
)->state
) {
303 (*o
)->state
= INACTIVE
;
307 (*o
)->state
= INACTIVE
;
308 send_opt((*o
)->nsend
, option
);
309 option_side_effects(*o
, 0);
312 case REALLY_INACTIVE
:
319 * If we reach here, the option was one we weren't prepared to
320 * cope with. So send a negative ack.
322 send_opt((cmd
== WILL ? DONT
: WONT
), option
);
325 static void process_subneg(void)
327 unsigned char b
[2048], *p
, *q
;
333 if (sb_len
== 1 && sb_buf
[0] == TELQUAL_SEND
) {
334 char logbuf
[sizeof(cfg
.termspeed
) + 80];
337 b
[2] = TELOPT_TSPEED
;
339 strcpy(b
+ 4, cfg
.termspeed
);
340 n
= 4 + strlen(cfg
.termspeed
);
343 sk_write(s
, b
, n
+ 2);
344 logevent("server:\tSB TSPEED SEND");
345 sprintf(logbuf
, "client:\tSB TSPEED IS %s", cfg
.termspeed
);
348 logevent("server:\tSB TSPEED <something weird>");
351 if (sb_len
== 1 && sb_buf
[0] == TELQUAL_SEND
) {
352 char logbuf
[sizeof(cfg
.termtype
) + 80];
357 for (n
= 0; cfg
.termtype
[n
]; n
++)
358 b
[n
+ 4] = (cfg
.termtype
[n
] >= 'a'
359 && cfg
.termtype
[n
] <=
360 'z' ? cfg
.termtype
[n
] + 'A' -
361 'a' : cfg
.termtype
[n
]);
364 sk_write(s
, b
, n
+ 6);
366 logevent("server:\tSB TTYPE SEND");
367 sprintf(logbuf
, "client:\tSB TTYPE IS %s", b
+ 4);
370 logevent("server:\tSB TTYPE <something weird>\r\n");
372 case TELOPT_OLD_ENVIRON
:
373 case TELOPT_NEW_ENVIRON
:
376 if (p
< q
&& *p
== TELQUAL_SEND
) {
379 sprintf(logbuf
, "server:\tSB %s SEND", telopt(sb_opt
));
381 if (sb_opt
== TELOPT_OLD_ENVIRON
) {
382 if (cfg
.rfc_environ
) {
390 * Try to guess the sense of VAR and VALUE.
396 } else if (*p
== BSD_VAR
) {
404 * With NEW_ENVIRON, the sense of VAR and VALUE
418 while (*e
&& *e
!= '\t')
441 sprintf(logbuf
, "client:\tSB %s IS %s", telopt(sb_opt
),
442 n
== 6 ?
"<nothing>" : "<stuff>");
450 TOPLEVEL
, SEENIAC
, SEENWILL
, SEENWONT
, SEENDO
, SEENDONT
,
451 SEENSB
, SUBNEGOT
, SUBNEG_IAC
, SEENCR
452 } telnet_state
= TOPLEVEL
;
454 static void do_telnet_read(char *buf
, int len
)
458 int c
= (unsigned char) *buf
++;
460 switch (telnet_state
) {
463 if (c
== NUL
&& telnet_state
== SEENCR
)
464 telnet_state
= TOPLEVEL
;
466 telnet_state
= SEENIAC
;
472 /* I can't get the F***ing winsock to insert the urgent IAC
473 * into the right position! Even with SO_OOBINLINE it gives
474 * it to recv too soon. And of course the DM byte (that
475 * arrives in the same packet!) appears several K later!!
477 * Oh well, we do get the DM in the right place so I'll
478 * just stop hiding on the next 0xf2 and hope for the best.
484 telnet_state
= SEENCR
;
486 telnet_state
= TOPLEVEL
;
491 telnet_state
= SEENDO
;
493 telnet_state
= SEENDONT
;
495 telnet_state
= SEENWILL
;
497 telnet_state
= SEENWONT
;
499 telnet_state
= SEENSB
;
502 telnet_state
= TOPLEVEL
;
504 /* ignore everything else; print it if it's IAC */
508 telnet_state
= TOPLEVEL
;
512 proc_rec_opt(WILL
, c
);
513 telnet_state
= TOPLEVEL
;
516 proc_rec_opt(WONT
, c
);
517 telnet_state
= TOPLEVEL
;
521 telnet_state
= TOPLEVEL
;
524 proc_rec_opt(DONT
, c
);
525 telnet_state
= TOPLEVEL
;
530 telnet_state
= SUBNEGOT
;
534 telnet_state
= SUBNEG_IAC
;
537 if (sb_len
>= sb_size
) {
541 srealloc(sb_buf
, sb_size
) :
548 if (sb_len
< sb_size
)
549 sb_buf
[sb_len
++] = c
;
550 telnet_state
= SUBNEGOT
; /* in case we came here by goto */
555 goto subneg_addchar
; /* yes, it's a hack, I know, but... */
558 telnet_state
= TOPLEVEL
;
565 static int telnet_closing(Plug plug
, char *error_msg
, int error_code
,
573 /* A socket error has occurred. */
574 connection_fatal(error_msg
);
575 } /* Otherwise, the remote side closed the connection normally. */
579 static int telnet_receive(Plug plug
, int urgent
, char *data
, int len
)
583 do_telnet_read(data
, len
);
588 * Called to set up the Telnet connection.
590 * Returns an error message, or NULL on success.
592 * Also places the canonical host name into `realhost'. It must be
593 * freed by the caller.
595 static char *telnet_init(char *host
, int port
, char **realhost
)
597 static struct plug_function_table fn_table
= {
600 }, *fn_table_ptr
= &fn_table
;
608 addr
= sk_namelookup(host
, realhost
);
609 if ((err
= sk_addr_error(addr
)))
613 port
= 23; /* default telnet port */
618 s
= sk_new(addr
, port
, 0, 1, &fn_table_ptr
);
619 if ((err
= sk_socket_error(s
)))
625 * Initialise option states.
627 if (cfg
.passive_telnet
) {
630 for (o
= opts
; *o
; o
++)
631 if ((*o
)->state
== REQUESTED
)
632 (*o
)->state
= INACTIVE
;
636 for (o
= opts
; *o
; o
++)
637 if ((*o
)->state
== REQUESTED
)
638 send_opt((*o
)->send
, (*o
)->option
);
643 * Set up SYNCH state.
651 * Called to send data down the Telnet connection.
653 static void telnet_send(char *buf
, int len
)
656 static unsigned char iac
[2] = { IAC
, IAC
};
657 static unsigned char cr
[2] = { CR
, NUL
};
659 static unsigned char nl
[2] = { CR
, LF
};
666 while (p
< buf
+ len
) {
669 while (iswritable((unsigned char) *p
) && p
< buf
+ len
)
671 sk_write(s
, q
, p
- q
);
673 while (p
< buf
+ len
&& !iswritable((unsigned char) *p
)) {
674 sk_write(s
, (unsigned char) *p
== IAC ? iac
: cr
, 2);
681 * Called to set the size of the window from Telnet's POV.
683 static void telnet_size(void)
688 if (s
== NULL
|| o_naws
.state
!= ACTIVE
)
700 sprintf(logbuf
, "client:\tSB NAWS %d,%d",
701 ((unsigned char) b
[3] << 8) + (unsigned char) b
[4],
702 ((unsigned char) b
[5] << 8) + (unsigned char) b
[6]);
707 * Send Telnet special codes.
709 static void telnet_special(Telnet_Special code
)
767 sk_write(s
, "\r\n", 2);
772 sk_write_oob(s
, b
+ 1, 1);
775 if (o_echo
.state
== INACTIVE
|| o_echo
.state
== REALLY_INACTIVE
) {
776 o_echo
.state
= REQUESTED
;
777 send_opt(o_echo
.send
, o_echo
.option
);
781 if (o_echo
.state
== ACTIVE
) {
782 o_echo
.state
= REQUESTED
;
783 send_opt(o_echo
.nsend
, o_echo
.option
);
787 if (o_they_sga
.state
== ACTIVE
) {
795 static Socket
telnet_socket(void)
800 static int telnet_sendok(void)
805 static int telnet_ldisc(int option
)
807 if (option
== LD_ECHO
)
809 if (option
== LD_EDIT
)
814 Backend telnet_backend
= {