eaf1e20a |
1 | /* |
2 | * Telnet backend. |
3 | */ |
4 | |
374330e2 |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
96b720b3 |
7 | #include <limits.h> |
374330e2 |
8 | |
9 | #include "putty.h" |
10 | |
11 | #ifndef FALSE |
12 | #define FALSE 0 |
13 | #endif |
14 | #ifndef TRUE |
15 | #define TRUE 1 |
16 | #endif |
17 | |
32874aea |
18 | #define IAC 255 /* interpret as command: */ |
19 | #define DONT 254 /* you are not to use option */ |
20 | #define DO 253 /* please, you use option */ |
21 | #define WONT 252 /* I won't use option */ |
22 | #define WILL 251 /* I will use option */ |
23 | #define SB 250 /* interpret as subnegotiation */ |
24 | #define SE 240 /* end sub negotiation */ |
25 | |
26 | #define GA 249 /* you may reverse the line */ |
27 | #define EL 248 /* erase the current line */ |
28 | #define EC 247 /* erase the current character */ |
29 | #define AYT 246 /* are you there */ |
30 | #define AO 245 /* abort output--but let prog finish */ |
31 | #define IP 244 /* interrupt process--permanently */ |
32 | #define BREAK 243 /* break */ |
33 | #define DM 242 /* data mark--for connect. cleaning */ |
34 | #define NOP 241 /* nop */ |
35 | #define EOR 239 /* end of record (transparent mode) */ |
36 | #define ABORT 238 /* Abort process */ |
37 | #define SUSP 237 /* Suspend process */ |
38 | #define xEOF 236 /* End of file: EOF is already used... */ |
39 | |
6196fb7e |
40 | #define TELOPTS(X) \ |
41 | X(BINARY, 0) /* 8-bit data path */ \ |
42 | X(ECHO, 1) /* echo */ \ |
43 | X(RCP, 2) /* prepare to reconnect */ \ |
44 | X(SGA, 3) /* suppress go ahead */ \ |
45 | X(NAMS, 4) /* approximate message size */ \ |
46 | X(STATUS, 5) /* give status */ \ |
47 | X(TM, 6) /* timing mark */ \ |
48 | X(RCTE, 7) /* remote controlled transmission and echo */ \ |
49 | X(NAOL, 8) /* negotiate about output line width */ \ |
50 | X(NAOP, 9) /* negotiate about output page size */ \ |
51 | X(NAOCRD, 10) /* negotiate about CR disposition */ \ |
52 | X(NAOHTS, 11) /* negotiate about horizontal tabstops */ \ |
53 | X(NAOHTD, 12) /* negotiate about horizontal tab disposition */ \ |
54 | X(NAOFFD, 13) /* negotiate about formfeed disposition */ \ |
55 | X(NAOVTS, 14) /* negotiate about vertical tab stops */ \ |
56 | X(NAOVTD, 15) /* negotiate about vertical tab disposition */ \ |
57 | X(NAOLFD, 16) /* negotiate about output LF disposition */ \ |
58 | X(XASCII, 17) /* extended ascic character set */ \ |
59 | X(LOGOUT, 18) /* force logout */ \ |
60 | X(BM, 19) /* byte macro */ \ |
61 | X(DET, 20) /* data entry terminal */ \ |
62 | X(SUPDUP, 21) /* supdup protocol */ \ |
63 | X(SUPDUPOUTPUT, 22) /* supdup output */ \ |
64 | X(SNDLOC, 23) /* send location */ \ |
65 | X(TTYPE, 24) /* terminal type */ \ |
66 | X(EOR, 25) /* end or record */ \ |
67 | X(TUID, 26) /* TACACS user identification */ \ |
68 | X(OUTMRK, 27) /* output marking */ \ |
69 | X(TTYLOC, 28) /* terminal location number */ \ |
70 | X(3270REGIME, 29) /* 3270 regime */ \ |
71 | X(X3PAD, 30) /* X.3 PAD */ \ |
72 | X(NAWS, 31) /* window size */ \ |
73 | X(TSPEED, 32) /* terminal speed */ \ |
74 | X(LFLOW, 33) /* remote flow control */ \ |
75 | X(LINEMODE, 34) /* Linemode option */ \ |
76 | X(XDISPLOC, 35) /* X Display Location */ \ |
77 | X(OLD_ENVIRON, 36) /* Old - Environment variables */ \ |
78 | X(AUTHENTICATION, 37) /* Authenticate */ \ |
79 | X(ENCRYPT, 38) /* Encryption option */ \ |
80 | X(NEW_ENVIRON, 39) /* New - Environment variables */ \ |
81 | X(TN3270E, 40) /* TN3270 enhancements */ \ |
82 | X(XAUTH, 41) \ |
83 | X(CHARSET, 42) /* Character set */ \ |
84 | X(RSP, 43) /* Remote serial port */ \ |
85 | X(COM_PORT_OPTION, 44) /* Com port control */ \ |
86 | X(SLE, 45) /* Suppress local echo */ \ |
87 | X(STARTTLS, 46) /* Start TLS */ \ |
88 | X(KERMIT, 47) /* Automatic Kermit file transfer */ \ |
89 | X(SEND_URL, 48) \ |
90 | X(FORWARD_X, 49) \ |
91 | X(PRAGMA_LOGON, 138) \ |
92 | X(SSPI_LOGON, 139) \ |
93 | X(PRAGMA_HEARTBEAT, 140) \ |
94 | X(EXOPL, 255) /* extended-options-list */ |
95 | |
96 | #define telnet_enum(x,y) TELOPT_##x = y, |
97 | enum { TELOPTS(telnet_enum) dummy=0 }; |
98 | #undef telnet_enum |
32874aea |
99 | |
100 | #define TELQUAL_IS 0 /* option is... */ |
101 | #define TELQUAL_SEND 1 /* send option */ |
102 | #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */ |
374330e2 |
103 | #define BSD_VAR 1 |
104 | #define BSD_VALUE 0 |
105 | #define RFC_VAR 0 |
106 | #define RFC_VALUE 1 |
107 | |
108 | #define CR 13 |
109 | #define LF 10 |
110 | #define NUL 0 |
111 | |
656fa244 |
112 | #define iswritable(x) \ |
113 | ( (x) != IAC && \ |
114 | (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR)) |
374330e2 |
115 | |
32874aea |
116 | static char *telopt(int opt) |
117 | { |
6196fb7e |
118 | #define telnet_str(x,y) case TELOPT_##x: return #x; |
119 | switch (opt) { |
120 | TELOPTS(telnet_str) |
121 | default: |
122 | return "<unknown>"; |
123 | } |
124 | #undef telnet_str |
374330e2 |
125 | } |
126 | |
51470298 |
127 | static void telnet_size(void *handle, int width, int height); |
374330e2 |
128 | |
129 | struct Opt { |
130 | int send; /* what we initially send */ |
131 | int nsend; /* -ve send if requested to stop it */ |
132 | int ack, nak; /* +ve and -ve acknowledgements */ |
133 | int option; /* the option code */ |
51470298 |
134 | int index; /* index into telnet->opt_states[] */ |
374330e2 |
135 | enum { |
136 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
51470298 |
137 | } initial_state; |
138 | }; |
139 | |
140 | enum { |
141 | OPTINDEX_NAWS, |
142 | OPTINDEX_TSPEED, |
143 | OPTINDEX_TTYPE, |
144 | OPTINDEX_OENV, |
145 | OPTINDEX_NENV, |
146 | OPTINDEX_ECHO, |
147 | OPTINDEX_WE_SGA, |
148 | OPTINDEX_THEY_SGA, |
656fa244 |
149 | OPTINDEX_WE_BIN, |
150 | OPTINDEX_THEY_BIN, |
51470298 |
151 | NUM_OPTS |
374330e2 |
152 | }; |
153 | |
51470298 |
154 | static const struct Opt o_naws = |
155 | { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED }; |
156 | static const struct Opt o_tspeed = |
157 | { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED }; |
158 | static const struct Opt o_ttype = |
159 | { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED }; |
5f95fdf1 |
160 | static const struct Opt o_oenv = |
161 | { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE }; |
162 | static const struct Opt o_nenv = |
163 | { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED }; |
51470298 |
164 | static const struct Opt o_echo = |
165 | { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED }; |
166 | static const struct Opt o_we_sga = |
167 | { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED }; |
168 | static const struct Opt o_they_sga = |
169 | { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED }; |
656fa244 |
170 | static const struct Opt o_we_bin = |
171 | { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE }; |
172 | static const struct Opt o_they_bin = |
173 | { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE }; |
51470298 |
174 | |
175 | static const struct Opt *const opts[] = { |
374330e2 |
176 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
656fa244 |
177 | &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL |
374330e2 |
178 | }; |
179 | |
51470298 |
180 | typedef struct telnet_tag { |
181 | const struct plug_function_table *fn; |
182 | /* the above field _must_ be first in the structure */ |
183 | |
184 | Socket s; |
96b720b3 |
185 | int closed_on_socket_error; |
51470298 |
186 | |
187 | void *frontend; |
b9d7bcad |
188 | void *ldisc; |
51470298 |
189 | int term_width, term_height; |
190 | |
191 | int opt_states[NUM_OPTS]; |
192 | |
193 | int echoing, editing; |
194 | int activated; |
195 | int bufsize; |
196 | int in_synch; |
197 | int sb_opt, sb_len; |
a1fd562d |
198 | unsigned char *sb_buf; |
51470298 |
199 | int sb_size; |
200 | |
201 | enum { |
202 | TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
203 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
204 | } state; |
205 | |
4a693cfc |
206 | Conf *conf; |
39934deb |
207 | |
208 | Pinger pinger; |
51470298 |
209 | } *Telnet; |
210 | |
5471d09a |
211 | #define TELNET_MAX_BACKLOG 4096 |
212 | |
374330e2 |
213 | #define SB_DELTA 1024 |
214 | |
ee00c99a |
215 | static void c_write(Telnet telnet, char *buf, int len) |
32874aea |
216 | { |
5471d09a |
217 | int backlog; |
ee00c99a |
218 | backlog = from_backend(telnet->frontend, 0, buf, len); |
51470298 |
219 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
fe50e814 |
220 | } |
221 | |
a8327734 |
222 | static void log_option(Telnet telnet, char *sender, int cmd, int option) |
32874aea |
223 | { |
57356d63 |
224 | char *buf; |
24d7f854 |
225 | /* |
226 | * The strange-looking "<?""?>" below is there to avoid a |
227 | * trigraph - a double question mark followed by > maps to a |
228 | * closing brace character! |
229 | */ |
57356d63 |
230 | buf = dupprintf("%s:\t%s %s", sender, |
231 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
232 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<?""?>"), |
233 | telopt(option)); |
a8327734 |
234 | logevent(telnet->frontend, buf); |
57356d63 |
235 | sfree(buf); |
374330e2 |
236 | } |
237 | |
51470298 |
238 | static void send_opt(Telnet telnet, int cmd, int option) |
32874aea |
239 | { |
374330e2 |
240 | unsigned char b[3]; |
241 | |
32874aea |
242 | b[0] = IAC; |
243 | b[1] = cmd; |
244 | b[2] = option; |
a1fd562d |
245 | telnet->bufsize = sk_write(telnet->s, (char *)b, 3); |
a8327734 |
246 | log_option(telnet, "client", cmd, option); |
374330e2 |
247 | } |
248 | |
51470298 |
249 | static void deactivate_option(Telnet telnet, const struct Opt *o) |
32874aea |
250 | { |
51470298 |
251 | if (telnet->opt_states[o->index] == REQUESTED || |
252 | telnet->opt_states[o->index] == ACTIVE) |
253 | send_opt(telnet, o->nsend, o->option); |
254 | telnet->opt_states[o->index] = REALLY_INACTIVE; |
374330e2 |
255 | } |
256 | |
708bbbbe |
257 | /* |
258 | * Generate side effects of enabling or disabling an option. |
259 | */ |
51470298 |
260 | static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled) |
32874aea |
261 | { |
0965bee0 |
262 | if (o->option == TELOPT_ECHO && o->send == DO) |
51470298 |
263 | telnet->echoing = !enabled; |
b6c680d4 |
264 | else if (o->option == TELOPT_SGA && o->send == DO) |
51470298 |
265 | telnet->editing = !enabled; |
b9d7bcad |
266 | if (telnet->ldisc) /* cause ldisc to notice the change */ |
267 | ldisc_send(telnet->ldisc, NULL, 0, 0); |
8faa456c |
268 | |
269 | /* Ensure we get the minimum options */ |
51470298 |
270 | if (!telnet->activated) { |
271 | if (telnet->opt_states[o_echo.index] == INACTIVE) { |
272 | telnet->opt_states[o_echo.index] = REQUESTED; |
273 | send_opt(telnet, o_echo.send, o_echo.option); |
8faa456c |
274 | } |
51470298 |
275 | if (telnet->opt_states[o_we_sga.index] == INACTIVE) { |
276 | telnet->opt_states[o_we_sga.index] = REQUESTED; |
277 | send_opt(telnet, o_we_sga.send, o_we_sga.option); |
8faa456c |
278 | } |
51470298 |
279 | if (telnet->opt_states[o_they_sga.index] == INACTIVE) { |
280 | telnet->opt_states[o_they_sga.index] = REQUESTED; |
281 | send_opt(telnet, o_they_sga.send, o_they_sga.option); |
8faa456c |
282 | } |
51470298 |
283 | telnet->activated = TRUE; |
8faa456c |
284 | } |
708bbbbe |
285 | } |
286 | |
51470298 |
287 | static void activate_option(Telnet telnet, const struct Opt *o) |
32874aea |
288 | { |
374330e2 |
289 | if (o->send == WILL && o->option == TELOPT_NAWS) |
51470298 |
290 | telnet_size(telnet, telnet->term_width, telnet->term_height); |
374330e2 |
291 | if (o->send == WILL && |
292 | (o->option == TELOPT_NEW_ENVIRON || |
293 | o->option == TELOPT_OLD_ENVIRON)) { |
294 | /* |
295 | * We may only have one kind of ENVIRON going at a time. |
296 | * This is a hack, but who cares. |
297 | */ |
51470298 |
298 | deactivate_option(telnet, o->option == |
32874aea |
299 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
374330e2 |
300 | } |
51470298 |
301 | option_side_effects(telnet, o, 1); |
374330e2 |
302 | } |
303 | |
51470298 |
304 | static void refused_option(Telnet telnet, const struct Opt *o) |
32874aea |
305 | { |
374330e2 |
306 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
51470298 |
307 | telnet->opt_states[o_oenv.index] == INACTIVE) { |
308 | send_opt(telnet, WILL, TELOPT_OLD_ENVIRON); |
309 | telnet->opt_states[o_oenv.index] = REQUESTED; |
374330e2 |
310 | } |
51470298 |
311 | option_side_effects(telnet, o, 0); |
374330e2 |
312 | } |
313 | |
51470298 |
314 | static void proc_rec_opt(Telnet telnet, int cmd, int option) |
32874aea |
315 | { |
51470298 |
316 | const struct Opt *const *o; |
374330e2 |
317 | |
a8327734 |
318 | log_option(telnet, "server", cmd, option); |
374330e2 |
319 | for (o = opts; *o; o++) { |
320 | if ((*o)->option == option && (*o)->ack == cmd) { |
51470298 |
321 | switch (telnet->opt_states[(*o)->index]) { |
374330e2 |
322 | case REQUESTED: |
51470298 |
323 | telnet->opt_states[(*o)->index] = ACTIVE; |
324 | activate_option(telnet, *o); |
374330e2 |
325 | break; |
326 | case ACTIVE: |
327 | break; |
328 | case INACTIVE: |
51470298 |
329 | telnet->opt_states[(*o)->index] = ACTIVE; |
330 | send_opt(telnet, (*o)->send, option); |
331 | activate_option(telnet, *o); |
374330e2 |
332 | break; |
333 | case REALLY_INACTIVE: |
51470298 |
334 | send_opt(telnet, (*o)->nsend, option); |
374330e2 |
335 | break; |
336 | } |
337 | return; |
338 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
51470298 |
339 | switch (telnet->opt_states[(*o)->index]) { |
374330e2 |
340 | case REQUESTED: |
51470298 |
341 | telnet->opt_states[(*o)->index] = INACTIVE; |
342 | refused_option(telnet, *o); |
374330e2 |
343 | break; |
344 | case ACTIVE: |
51470298 |
345 | telnet->opt_states[(*o)->index] = INACTIVE; |
346 | send_opt(telnet, (*o)->nsend, option); |
347 | option_side_effects(telnet, *o, 0); |
374330e2 |
348 | break; |
349 | case INACTIVE: |
350 | case REALLY_INACTIVE: |
351 | break; |
352 | } |
353 | return; |
354 | } |
355 | } |
356 | /* |
357 | * If we reach here, the option was one we weren't prepared to |
108b0b94 |
358 | * cope with. If the request was positive (WILL or DO), we send |
359 | * a negative ack to indicate refusal. If the request was |
360 | * negative (WONT / DONT), we must do nothing. |
374330e2 |
361 | */ |
108b0b94 |
362 | if (cmd == WILL || cmd == DO) |
363 | send_opt(telnet, (cmd == WILL ? DONT : WONT), option); |
374330e2 |
364 | } |
365 | |
51470298 |
366 | static void process_subneg(Telnet telnet) |
32874aea |
367 | { |
4a693cfc |
368 | unsigned char *b, *p, *q; |
369 | int var, value, n, bsize; |
370 | char *e, *eval, *ekey, *user; |
374330e2 |
371 | |
51470298 |
372 | switch (telnet->sb_opt) { |
374330e2 |
373 | case TELOPT_TSPEED: |
51470298 |
374 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
57356d63 |
375 | char *logbuf; |
4a693cfc |
376 | char *termspeed = conf_get_str(telnet->conf, CONF_termspeed); |
377 | b = snewn(20 + strlen(termspeed), unsigned char); |
32874aea |
378 | b[0] = IAC; |
379 | b[1] = SB; |
380 | b[2] = TELOPT_TSPEED; |
374330e2 |
381 | b[3] = TELQUAL_IS; |
4a693cfc |
382 | strcpy((char *)(b + 4), termspeed); |
383 | n = 4 + strlen(termspeed); |
32874aea |
384 | b[n] = IAC; |
385 | b[n + 1] = SE; |
a1fd562d |
386 | telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2); |
a8327734 |
387 | logevent(telnet->frontend, "server:\tSB TSPEED SEND"); |
4a693cfc |
388 | logbuf = dupprintf("client:\tSB TSPEED IS %s", termspeed); |
a8327734 |
389 | logevent(telnet->frontend, logbuf); |
57356d63 |
390 | sfree(logbuf); |
4a693cfc |
391 | sfree(b); |
374330e2 |
392 | } else |
a8327734 |
393 | logevent(telnet->frontend, "server:\tSB TSPEED <something weird>"); |
374330e2 |
394 | break; |
395 | case TELOPT_TTYPE: |
51470298 |
396 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
57356d63 |
397 | char *logbuf; |
4a693cfc |
398 | char *termtype = conf_get_str(telnet->conf, CONF_termtype); |
399 | b = snewn(20 + strlen(termtype), unsigned char); |
32874aea |
400 | b[0] = IAC; |
401 | b[1] = SB; |
402 | b[2] = TELOPT_TTYPE; |
374330e2 |
403 | b[3] = TELQUAL_IS; |
4a693cfc |
404 | for (n = 0; termtype[n]; n++) |
405 | b[n + 4] = (termtype[n] >= 'a' && termtype[n] <= 'z' ? |
406 | termtype[n] + 'A' - 'a' : |
407 | termtype[n]); |
32874aea |
408 | b[n + 4] = IAC; |
409 | b[n + 5] = SE; |
a1fd562d |
410 | telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6); |
32874aea |
411 | b[n + 4] = 0; |
a8327734 |
412 | logevent(telnet->frontend, "server:\tSB TTYPE SEND"); |
57356d63 |
413 | logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4); |
a8327734 |
414 | logevent(telnet->frontend, logbuf); |
57356d63 |
415 | sfree(logbuf); |
4a693cfc |
416 | sfree(b); |
374330e2 |
417 | } else |
a8327734 |
418 | logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
419 | break; |
420 | case TELOPT_OLD_ENVIRON: |
32874aea |
421 | case TELOPT_NEW_ENVIRON: |
51470298 |
422 | p = telnet->sb_buf; |
423 | q = p + telnet->sb_len; |
374330e2 |
424 | if (p < q && *p == TELQUAL_SEND) { |
57356d63 |
425 | char *logbuf; |
374330e2 |
426 | p++; |
57356d63 |
427 | logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt)); |
a8327734 |
428 | logevent(telnet->frontend, logbuf); |
57356d63 |
429 | sfree(logbuf); |
51470298 |
430 | if (telnet->sb_opt == TELOPT_OLD_ENVIRON) { |
4f6e3bae |
431 | if (conf_get_int(telnet->conf, CONF_rfc_environ)) { |
374330e2 |
432 | value = RFC_VALUE; |
433 | var = RFC_VAR; |
434 | } else { |
435 | value = BSD_VALUE; |
436 | var = BSD_VAR; |
437 | } |
438 | /* |
439 | * Try to guess the sense of VAR and VALUE. |
440 | */ |
441 | while (p < q) { |
442 | if (*p == RFC_VAR) { |
443 | value = RFC_VALUE; |
444 | var = RFC_VAR; |
445 | } else if (*p == BSD_VAR) { |
446 | value = BSD_VALUE; |
447 | var = BSD_VAR; |
448 | } |
449 | p++; |
450 | } |
451 | } else { |
452 | /* |
453 | * With NEW_ENVIRON, the sense of VAR and VALUE |
454 | * isn't in doubt. |
455 | */ |
456 | value = RFC_VALUE; |
457 | var = RFC_VAR; |
458 | } |
4a693cfc |
459 | bsize = 20; |
460 | for (eval = conf_get_str_strs(telnet->conf, CONF_environmt, |
461 | NULL, &ekey); |
462 | eval != NULL; |
463 | eval = conf_get_str_strs(telnet->conf, CONF_environmt, |
464 | ekey, &ekey)) |
465 | bsize += strlen(ekey) + strlen(eval) + 2; |
466 | user = get_remote_username(telnet->conf); |
467 | if (user) |
468 | bsize += 6 + strlen(user); |
469 | |
470 | b = snewn(bsize, unsigned char); |
32874aea |
471 | b[0] = IAC; |
472 | b[1] = SB; |
51470298 |
473 | b[2] = telnet->sb_opt; |
374330e2 |
474 | b[3] = TELQUAL_IS; |
475 | n = 4; |
4a693cfc |
476 | for (eval = conf_get_str_strs(telnet->conf, CONF_environmt, |
477 | NULL, &ekey); |
478 | eval != NULL; |
479 | eval = conf_get_str_strs(telnet->conf, CONF_environmt, |
480 | ekey, &ekey)) { |
374330e2 |
481 | b[n++] = var; |
4a693cfc |
482 | for (e = ekey; *e; e++) |
483 | b[n++] = *e; |
374330e2 |
484 | b[n++] = value; |
4a693cfc |
485 | for (e = eval; *e; e++) |
486 | b[n++] = *e; |
374330e2 |
487 | } |
4a693cfc |
488 | if (user) { |
489 | b[n++] = var; |
490 | b[n++] = 'U'; |
491 | b[n++] = 'S'; |
492 | b[n++] = 'E'; |
493 | b[n++] = 'R'; |
494 | b[n++] = value; |
495 | for (e = user; *e; e++) |
496 | b[n++] = *e; |
497 | } |
498 | b[n++] = IAC; |
499 | b[n++] = SE; |
500 | telnet->bufsize = sk_write(telnet->s, (char *)b, n); |
501 | if (n == 6) { |
502 | logbuf = dupprintf("client:\tSB %s IS <nothing>", |
503 | telopt(telnet->sb_opt)); |
471c20b0 |
504 | logevent(telnet->frontend, logbuf); |
505 | sfree(logbuf); |
4a693cfc |
506 | } else { |
507 | logbuf = dupprintf("client:\tSB %s IS:", |
508 | telopt(telnet->sb_opt)); |
509 | logevent(telnet->frontend, logbuf); |
510 | sfree(logbuf); |
511 | for (eval = conf_get_str_strs(telnet->conf, CONF_environmt, |
512 | NULL, &ekey); |
513 | eval != NULL; |
514 | eval = conf_get_str_strs(telnet->conf, CONF_environmt, |
515 | ekey, &ekey)) { |
516 | logbuf = dupprintf("\t%s=%s", ekey, eval); |
517 | logevent(telnet->frontend, logbuf); |
518 | sfree(logbuf); |
519 | } |
520 | if (user) { |
521 | logbuf = dupprintf("\tUSER=%s", user); |
522 | logevent(telnet->frontend, logbuf); |
523 | sfree(logbuf); |
524 | } |
374330e2 |
525 | } |
4a693cfc |
526 | sfree(b); |
527 | sfree(user); |
374330e2 |
528 | } |
529 | break; |
530 | } |
531 | } |
532 | |
51470298 |
533 | static void do_telnet_read(Telnet telnet, char *buf, int len) |
32874aea |
534 | { |
ee00c99a |
535 | char *outbuf = NULL; |
536 | int outbuflen = 0, outbufsize = 0; |
537 | |
538 | #define ADDTOBUF(c) do { \ |
539 | if (outbuflen >= outbufsize) { \ |
540 | outbufsize = outbuflen + 256; \ |
541 | outbuf = sresize(outbuf, outbufsize, char); \ |
542 | } \ |
543 | outbuf[outbuflen++] = (c); \ |
544 | } while (0) |
374330e2 |
545 | |
546 | while (len--) { |
547 | int c = (unsigned char) *buf++; |
548 | |
51470298 |
549 | switch (telnet->state) { |
887035a5 |
550 | case TOP_LEVEL: |
374330e2 |
551 | case SEENCR: |
51470298 |
552 | if (c == NUL && telnet->state == SEENCR) |
553 | telnet->state = TOP_LEVEL; |
374330e2 |
554 | else if (c == IAC) |
51470298 |
555 | telnet->state = SEENIAC; |
374330e2 |
556 | else { |
51470298 |
557 | if (!telnet->in_synch) |
ee00c99a |
558 | ADDTOBUF(c); |
2f938b83 |
559 | |
560 | #if 1 |
561 | /* I can't get the F***ing winsock to insert the urgent IAC |
562 | * into the right position! Even with SO_OOBINLINE it gives |
563 | * it to recv too soon. And of course the DM byte (that |
564 | * arrives in the same packet!) appears several K later!! |
565 | * |
566 | * Oh well, we do get the DM in the right place so I'll |
567 | * just stop hiding on the next 0xf2 and hope for the best. |
568 | */ |
32874aea |
569 | else if (c == DM) |
51470298 |
570 | telnet->in_synch = 0; |
2f938b83 |
571 | #endif |
656fa244 |
572 | if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE) |
51470298 |
573 | telnet->state = SEENCR; |
374330e2 |
574 | else |
51470298 |
575 | telnet->state = TOP_LEVEL; |
374330e2 |
576 | } |
577 | break; |
578 | case SEENIAC: |
32874aea |
579 | if (c == DO) |
51470298 |
580 | telnet->state = SEENDO; |
32874aea |
581 | else if (c == DONT) |
51470298 |
582 | telnet->state = SEENDONT; |
32874aea |
583 | else if (c == WILL) |
51470298 |
584 | telnet->state = SEENWILL; |
32874aea |
585 | else if (c == WONT) |
51470298 |
586 | telnet->state = SEENWONT; |
32874aea |
587 | else if (c == SB) |
51470298 |
588 | telnet->state = SEENSB; |
2f938b83 |
589 | else if (c == DM) { |
51470298 |
590 | telnet->in_synch = 0; |
591 | telnet->state = TOP_LEVEL; |
32874aea |
592 | } else { |
ded38628 |
593 | /* ignore everything else; print it if it's IAC */ |
594 | if (c == IAC) { |
ee00c99a |
595 | ADDTOBUF(c); |
ded38628 |
596 | } |
51470298 |
597 | telnet->state = TOP_LEVEL; |
300d41b0 |
598 | } |
374330e2 |
599 | break; |
600 | case SEENWILL: |
51470298 |
601 | proc_rec_opt(telnet, WILL, c); |
602 | telnet->state = TOP_LEVEL; |
374330e2 |
603 | break; |
604 | case SEENWONT: |
51470298 |
605 | proc_rec_opt(telnet, WONT, c); |
606 | telnet->state = TOP_LEVEL; |
374330e2 |
607 | break; |
608 | case SEENDO: |
51470298 |
609 | proc_rec_opt(telnet, DO, c); |
610 | telnet->state = TOP_LEVEL; |
374330e2 |
611 | break; |
612 | case SEENDONT: |
51470298 |
613 | proc_rec_opt(telnet, DONT, c); |
614 | telnet->state = TOP_LEVEL; |
374330e2 |
615 | break; |
616 | case SEENSB: |
51470298 |
617 | telnet->sb_opt = c; |
618 | telnet->sb_len = 0; |
619 | telnet->state = SUBNEGOT; |
374330e2 |
620 | break; |
621 | case SUBNEGOT: |
622 | if (c == IAC) |
51470298 |
623 | telnet->state = SUBNEG_IAC; |
374330e2 |
624 | else { |
32874aea |
625 | subneg_addchar: |
51470298 |
626 | if (telnet->sb_len >= telnet->sb_size) { |
51470298 |
627 | telnet->sb_size += SB_DELTA; |
3d88e64d |
628 | telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size, |
629 | unsigned char); |
374330e2 |
630 | } |
3d88e64d |
631 | telnet->sb_buf[telnet->sb_len++] = c; |
51470298 |
632 | telnet->state = SUBNEGOT; /* in case we came here by goto */ |
374330e2 |
633 | } |
634 | break; |
635 | case SUBNEG_IAC: |
636 | if (c != SE) |
637 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
638 | else { |
51470298 |
639 | process_subneg(telnet); |
640 | telnet->state = TOP_LEVEL; |
374330e2 |
641 | } |
642 | break; |
643 | } |
644 | } |
ee00c99a |
645 | |
646 | if (outbuflen) |
647 | c_write(telnet, outbuf, outbuflen); |
648 | sfree(outbuf); |
374330e2 |
649 | } |
650 | |
7555d6a5 |
651 | static void telnet_log(Plug plug, int type, SockAddr addr, int port, |
652 | const char *error_msg, int error_code) |
653 | { |
654 | Telnet telnet = (Telnet) plug; |
655 | char addrbuf[256], *msg; |
656 | |
657 | sk_getaddr(addr, addrbuf, lenof(addrbuf)); |
658 | |
659 | if (type == 0) |
660 | msg = dupprintf("Connecting to %s port %d", addrbuf, port); |
661 | else |
662 | msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); |
663 | |
664 | logevent(telnet->frontend, msg); |
665 | } |
666 | |
cbe2d68f |
667 | static int telnet_closing(Plug plug, const char *error_msg, int error_code, |
32874aea |
668 | int calling_back) |
669 | { |
51470298 |
670 | Telnet telnet = (Telnet) plug; |
671 | |
bc06669b |
672 | /* |
673 | * We don't implement independent EOF in each direction for Telnet |
674 | * connections; as soon as we get word that the remote side has |
675 | * sent us EOF, we wind up the whole connection. |
676 | */ |
677 | |
51470298 |
678 | if (telnet->s) { |
679 | sk_close(telnet->s); |
680 | telnet->s = NULL; |
96b720b3 |
681 | if (error_msg) |
682 | telnet->closed_on_socket_error = TRUE; |
39934deb |
683 | notify_remote_exit(telnet->frontend); |
f3ab576e |
684 | } |
7e78000d |
685 | if (error_msg) { |
a8327734 |
686 | logevent(telnet->frontend, error_msg); |
971bcc0a |
687 | connection_fatal(telnet->frontend, "%s", error_msg); |
7555d6a5 |
688 | } |
689 | /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
690 | return 0; |
691 | } |
692 | |
32874aea |
693 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
694 | { |
51470298 |
695 | Telnet telnet = (Telnet) plug; |
32874aea |
696 | if (urgent) |
51470298 |
697 | telnet->in_synch = TRUE; |
698 | do_telnet_read(telnet, data, len); |
8df7a775 |
699 | return 1; |
700 | } |
701 | |
3ad9d396 |
702 | static void telnet_sent(Plug plug, int bufsize) |
703 | { |
51470298 |
704 | Telnet telnet = (Telnet) plug; |
705 | telnet->bufsize = bufsize; |
3ad9d396 |
706 | } |
707 | |
374330e2 |
708 | /* |
8df7a775 |
709 | * Called to set up the Telnet connection. |
374330e2 |
710 | * |
711 | * Returns an error message, or NULL on success. |
712 | * |
6e1ebb76 |
713 | * Also places the canonical host name into `realhost'. It must be |
714 | * freed by the caller. |
374330e2 |
715 | */ |
cbe2d68f |
716 | static const char *telnet_init(void *frontend_handle, void **backend_handle, |
4a693cfc |
717 | Conf *conf, char *host, int port, |
718 | char **realhost, int nodelay, int keepalive) |
32874aea |
719 | { |
51470298 |
720 | static const struct plug_function_table fn_table = { |
7555d6a5 |
721 | telnet_log, |
7e78000d |
722 | telnet_closing, |
3ad9d396 |
723 | telnet_receive, |
724 | telnet_sent |
51470298 |
725 | }; |
8df7a775 |
726 | SockAddr addr; |
cbe2d68f |
727 | const char *err; |
51470298 |
728 | Telnet telnet; |
4a693cfc |
729 | char *loghost; |
730 | int addressfamily; |
51470298 |
731 | |
3d88e64d |
732 | telnet = snew(struct telnet_tag); |
51470298 |
733 | telnet->fn = &fn_table; |
4a693cfc |
734 | telnet->conf = conf_copy(conf); |
51470298 |
735 | telnet->s = NULL; |
96b720b3 |
736 | telnet->closed_on_socket_error = FALSE; |
51470298 |
737 | telnet->echoing = TRUE; |
738 | telnet->editing = TRUE; |
739 | telnet->activated = FALSE; |
740 | telnet->sb_buf = NULL; |
741 | telnet->sb_size = 0; |
742 | telnet->frontend = frontend_handle; |
4a693cfc |
743 | telnet->term_width = conf_get_int(telnet->conf, CONF_width); |
744 | telnet->term_height = conf_get_int(telnet->conf, CONF_height); |
51470298 |
745 | telnet->state = TOP_LEVEL; |
cc31ab78 |
746 | telnet->ldisc = NULL; |
39934deb |
747 | telnet->pinger = NULL; |
51470298 |
748 | *backend_handle = telnet; |
887035a5 |
749 | |
374330e2 |
750 | /* |
751 | * Try to find host. |
752 | */ |
3ad9d396 |
753 | { |
57356d63 |
754 | char *buf; |
4a693cfc |
755 | addressfamily = conf_get_int(telnet->conf, CONF_addressfamily); |
05581745 |
756 | buf = dupprintf("Looking up host \"%s\"%s", host, |
4a693cfc |
757 | (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : |
758 | (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : |
05581745 |
759 | ""))); |
a8327734 |
760 | logevent(telnet->frontend, buf); |
57356d63 |
761 | sfree(buf); |
3ad9d396 |
762 | } |
4a693cfc |
763 | addr = name_lookup(host, port, realhost, telnet->conf, addressfamily); |
f85e6f6e |
764 | if ((err = sk_addr_error(addr)) != NULL) { |
765 | sk_addr_free(addr); |
8df7a775 |
766 | return err; |
f85e6f6e |
767 | } |
374330e2 |
768 | |
769 | if (port < 0) |
770 | port = 23; /* default telnet port */ |
771 | |
772 | /* |
773 | * Open socket. |
774 | */ |
51470298 |
775 | telnet->s = new_connection(addr, *realhost, port, 0, 1, |
4a693cfc |
776 | nodelay, keepalive, (Plug) telnet, telnet->conf); |
a1fd562d |
777 | if ((err = sk_socket_error(telnet->s)) != NULL) |
8df7a775 |
778 | return err; |
374330e2 |
779 | |
4a693cfc |
780 | telnet->pinger = pinger_new(telnet->conf, &telnet_backend, telnet); |
39934deb |
781 | |
374330e2 |
782 | /* |
783 | * Initialise option states. |
784 | */ |
4a693cfc |
785 | if (conf_get_int(telnet->conf, CONF_passive_telnet)) { |
51470298 |
786 | const struct Opt *const *o; |
8faa456c |
787 | |
788 | for (o = opts; *o; o++) |
51470298 |
789 | telnet->opt_states[(*o)->index] = INACTIVE; |
8faa456c |
790 | } else { |
51470298 |
791 | const struct Opt *const *o; |
374330e2 |
792 | |
51470298 |
793 | for (o = opts; *o; o++) { |
794 | telnet->opt_states[(*o)->index] = (*o)->initial_state; |
795 | if (telnet->opt_states[(*o)->index] == REQUESTED) |
796 | send_opt(telnet, (*o)->send, (*o)->option); |
797 | } |
798 | telnet->activated = TRUE; |
374330e2 |
799 | } |
800 | |
374330e2 |
801 | /* |
802 | * Set up SYNCH state. |
803 | */ |
51470298 |
804 | telnet->in_synch = FALSE; |
6f34e365 |
805 | |
533b1743 |
806 | /* |
807 | * We can send special commands from the start. |
808 | */ |
809 | update_specials_menu(telnet->frontend); |
810 | |
881da168 |
811 | /* |
812 | * loghost overrides realhost, if specified. |
813 | */ |
4a693cfc |
814 | loghost = conf_get_str(telnet->conf, CONF_loghost); |
815 | if (*loghost) { |
881da168 |
816 | char *colon; |
817 | |
818 | sfree(*realhost); |
4a693cfc |
819 | *realhost = dupstr(loghost); |
881da168 |
820 | colon = strrchr(*realhost, ':'); |
821 | if (colon) { |
822 | /* |
823 | * FIXME: if we ever update this aspect of ssh.c for |
824 | * IPv6 literal management, this should change in line |
825 | * with it. |
826 | */ |
827 | *colon++ = '\0'; |
828 | } |
829 | } |
830 | |
374330e2 |
831 | return NULL; |
832 | } |
833 | |
fabd1805 |
834 | static void telnet_free(void *handle) |
835 | { |
836 | Telnet telnet = (Telnet) handle; |
837 | |
838 | sfree(telnet->sb_buf); |
839 | if (telnet->s) |
840 | sk_close(telnet->s); |
39934deb |
841 | if (telnet->pinger) |
842 | pinger_free(telnet->pinger); |
4a693cfc |
843 | conf_free(telnet->conf); |
fabd1805 |
844 | sfree(telnet); |
845 | } |
374330e2 |
846 | /* |
86916870 |
847 | * Reconfigure the Telnet backend. There's no immediate action |
848 | * necessary, in this backend: we just save the fresh config for |
849 | * any subsequent negotiations. |
850 | */ |
4a693cfc |
851 | static void telnet_reconfig(void *handle, Conf *conf) |
86916870 |
852 | { |
853 | Telnet telnet = (Telnet) handle; |
4a693cfc |
854 | pinger_reconfig(telnet->pinger, telnet->conf, conf); |
855 | conf_free(telnet->conf); |
856 | telnet->conf = conf_copy(conf); |
86916870 |
857 | } |
858 | |
859 | /* |
374330e2 |
860 | * Called to send data down the Telnet connection. |
861 | */ |
51470298 |
862 | static int telnet_send(void *handle, char *buf, int len) |
32874aea |
863 | { |
51470298 |
864 | Telnet telnet = (Telnet) handle; |
a1fd562d |
865 | unsigned char *p, *end; |
c85623f9 |
866 | static const unsigned char iac[2] = { IAC, IAC }; |
867 | static const unsigned char cr[2] = { CR, NUL }; |
a5f3e637 |
868 | #if 0 |
c85623f9 |
869 | static const unsigned char nl[2] = { CR, LF }; |
a5f3e637 |
870 | #endif |
374330e2 |
871 | |
51470298 |
872 | if (telnet->s == NULL) |
5471d09a |
873 | return 0; |
374330e2 |
874 | |
a1fd562d |
875 | p = (unsigned char *)buf; |
876 | end = (unsigned char *)(buf + len); |
877 | while (p < end) { |
878 | unsigned char *q = p; |
374330e2 |
879 | |
a1fd562d |
880 | while (p < end && iswritable(*p)) |
32874aea |
881 | p++; |
a1fd562d |
882 | telnet->bufsize = sk_write(telnet->s, (char *)q, p - q); |
374330e2 |
883 | |
a1fd562d |
884 | while (p < end && !iswritable(*p)) { |
51470298 |
885 | telnet->bufsize = |
a1fd562d |
886 | sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2); |
374330e2 |
887 | p++; |
888 | } |
889 | } |
5471d09a |
890 | |
51470298 |
891 | return telnet->bufsize; |
5471d09a |
892 | } |
893 | |
894 | /* |
895 | * Called to query the current socket sendability status. |
896 | */ |
51470298 |
897 | static int telnet_sendbuffer(void *handle) |
5471d09a |
898 | { |
51470298 |
899 | Telnet telnet = (Telnet) handle; |
900 | return telnet->bufsize; |
374330e2 |
901 | } |
902 | |
903 | /* |
904 | * Called to set the size of the window from Telnet's POV. |
905 | */ |
51470298 |
906 | static void telnet_size(void *handle, int width, int height) |
32874aea |
907 | { |
51470298 |
908 | Telnet telnet = (Telnet) handle; |
ef41972c |
909 | unsigned char b[24]; |
910 | int n; |
57356d63 |
911 | char *logbuf; |
374330e2 |
912 | |
51470298 |
913 | telnet->term_width = width; |
914 | telnet->term_height = height; |
f278d6f8 |
915 | |
51470298 |
916 | if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE) |
374330e2 |
917 | return; |
ef41972c |
918 | n = 0; |
919 | b[n++] = IAC; |
920 | b[n++] = SB; |
921 | b[n++] = TELOPT_NAWS; |
922 | b[n++] = telnet->term_width >> 8; |
923 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ |
924 | b[n++] = telnet->term_width & 0xFF; |
925 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ |
926 | b[n++] = telnet->term_height >> 8; |
927 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ |
928 | b[n++] = telnet->term_height & 0xFF; |
929 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ |
930 | b[n++] = IAC; |
931 | b[n++] = SE; |
a1fd562d |
932 | telnet->bufsize = sk_write(telnet->s, (char *)b, n); |
57356d63 |
933 | logbuf = dupprintf("client:\tSB NAWS %d,%d", |
ef41972c |
934 | telnet->term_width, telnet->term_height); |
a8327734 |
935 | logevent(telnet->frontend, logbuf); |
57356d63 |
936 | sfree(logbuf); |
374330e2 |
937 | } |
938 | |
939 | /* |
940 | * Send Telnet special codes. |
941 | */ |
51470298 |
942 | static void telnet_special(void *handle, Telnet_Special code) |
32874aea |
943 | { |
51470298 |
944 | Telnet telnet = (Telnet) handle; |
374330e2 |
945 | unsigned char b[2]; |
946 | |
51470298 |
947 | if (telnet->s == NULL) |
374330e2 |
948 | return; |
949 | |
950 | b[0] = IAC; |
951 | switch (code) { |
32874aea |
952 | case TS_AYT: |
953 | b[1] = AYT; |
a1fd562d |
954 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
955 | break; |
956 | case TS_BRK: |
957 | b[1] = BREAK; |
a1fd562d |
958 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
959 | break; |
960 | case TS_EC: |
961 | b[1] = EC; |
a1fd562d |
962 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
963 | break; |
964 | case TS_EL: |
965 | b[1] = EL; |
a1fd562d |
966 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
967 | break; |
968 | case TS_GA: |
969 | b[1] = GA; |
a1fd562d |
970 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
971 | break; |
972 | case TS_NOP: |
973 | b[1] = NOP; |
a1fd562d |
974 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
975 | break; |
976 | case TS_ABORT: |
977 | b[1] = ABORT; |
a1fd562d |
978 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
979 | break; |
980 | case TS_AO: |
981 | b[1] = AO; |
a1fd562d |
982 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
983 | break; |
984 | case TS_IP: |
985 | b[1] = IP; |
a1fd562d |
986 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
987 | break; |
988 | case TS_SUSP: |
989 | b[1] = SUSP; |
a1fd562d |
990 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
991 | break; |
992 | case TS_EOR: |
993 | b[1] = EOR; |
a1fd562d |
994 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
995 | break; |
996 | case TS_EOF: |
997 | b[1] = xEOF; |
a1fd562d |
998 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
999 | break; |
a5f3e637 |
1000 | case TS_EOL: |
6ee0cb6d |
1001 | /* In BINARY mode, CR-LF becomes just CR - |
1002 | * and without the NUL suffix too. */ |
656fa244 |
1003 | if (telnet->opt_states[o_we_bin.index] == ACTIVE) |
6ee0cb6d |
1004 | telnet->bufsize = sk_write(telnet->s, "\r", 1); |
656fa244 |
1005 | else |
1006 | telnet->bufsize = sk_write(telnet->s, "\r\n", 2); |
a5f3e637 |
1007 | break; |
374330e2 |
1008 | case TS_SYNCH: |
32874aea |
1009 | b[1] = DM; |
a1fd562d |
1010 | telnet->bufsize = sk_write(telnet->s, (char *)b, 1); |
1011 | telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1); |
32874aea |
1012 | break; |
684d367c |
1013 | case TS_RECHO: |
51470298 |
1014 | if (telnet->opt_states[o_echo.index] == INACTIVE || |
1015 | telnet->opt_states[o_echo.index] == REALLY_INACTIVE) { |
1016 | telnet->opt_states[o_echo.index] = REQUESTED; |
1017 | send_opt(telnet, o_echo.send, o_echo.option); |
684d367c |
1018 | } |
1019 | break; |
1020 | case TS_LECHO: |
51470298 |
1021 | if (telnet->opt_states[o_echo.index] == ACTIVE) { |
1022 | telnet->opt_states[o_echo.index] = REQUESTED; |
1023 | send_opt(telnet, o_echo.nsend, o_echo.option); |
684d367c |
1024 | } |
374330e2 |
1025 | break; |
32874aea |
1026 | case TS_PING: |
51470298 |
1027 | if (telnet->opt_states[o_they_sga.index] == ACTIVE) { |
32874aea |
1028 | b[1] = NOP; |
a1fd562d |
1029 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
ec55b220 |
1030 | } |
32874aea |
1031 | break; |
6f2d0cde |
1032 | default: |
1033 | break; /* never heard of it */ |
374330e2 |
1034 | } |
1035 | } |
1036 | |
125105d1 |
1037 | static const struct telnet_special *telnet_get_specials(void *handle) |
1038 | { |
1039 | static const struct telnet_special specials[] = { |
1040 | {"Are You There", TS_AYT}, |
1041 | {"Break", TS_BRK}, |
1042 | {"Synch", TS_SYNCH}, |
1043 | {"Erase Character", TS_EC}, |
1044 | {"Erase Line", TS_EL}, |
1045 | {"Go Ahead", TS_GA}, |
1046 | {"No Operation", TS_NOP}, |
6f2d0cde |
1047 | {NULL, TS_SEP}, |
125105d1 |
1048 | {"Abort Process", TS_ABORT}, |
1049 | {"Abort Output", TS_AO}, |
1050 | {"Interrupt Process", TS_IP}, |
1051 | {"Suspend Process", TS_SUSP}, |
6f2d0cde |
1052 | {NULL, TS_SEP}, |
125105d1 |
1053 | {"End Of Record", TS_EOR}, |
1054 | {"End Of File", TS_EOF}, |
6f2d0cde |
1055 | {NULL, TS_EXITMENU} |
125105d1 |
1056 | }; |
1057 | return specials; |
1058 | } |
1059 | |
6226c939 |
1060 | static int telnet_connected(void *handle) |
32874aea |
1061 | { |
51470298 |
1062 | Telnet telnet = (Telnet) handle; |
6226c939 |
1063 | return telnet->s != NULL; |
32874aea |
1064 | } |
8ccc75b0 |
1065 | |
51470298 |
1066 | static int telnet_sendok(void *handle) |
32874aea |
1067 | { |
68a49acb |
1068 | /* Telnet telnet = (Telnet) handle; */ |
32874aea |
1069 | return 1; |
1070 | } |
4017be6d |
1071 | |
51470298 |
1072 | static void telnet_unthrottle(void *handle, int backlog) |
5471d09a |
1073 | { |
51470298 |
1074 | Telnet telnet = (Telnet) handle; |
1075 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
5471d09a |
1076 | } |
1077 | |
51470298 |
1078 | static int telnet_ldisc(void *handle, int option) |
32874aea |
1079 | { |
51470298 |
1080 | Telnet telnet = (Telnet) handle; |
32874aea |
1081 | if (option == LD_ECHO) |
51470298 |
1082 | return telnet->echoing; |
32874aea |
1083 | if (option == LD_EDIT) |
51470298 |
1084 | return telnet->editing; |
0965bee0 |
1085 | return FALSE; |
1086 | } |
1087 | |
b9d7bcad |
1088 | static void telnet_provide_ldisc(void *handle, void *ldisc) |
1089 | { |
1090 | Telnet telnet = (Telnet) handle; |
1091 | telnet->ldisc = ldisc; |
1092 | } |
1093 | |
a8327734 |
1094 | static void telnet_provide_logctx(void *handle, void *logctx) |
1095 | { |
1096 | /* This is a stub. */ |
1097 | } |
1098 | |
51470298 |
1099 | static int telnet_exitcode(void *handle) |
d8d6c7e5 |
1100 | { |
0da1a790 |
1101 | Telnet telnet = (Telnet) handle; |
1102 | if (telnet->s != NULL) |
1103 | return -1; /* still connected */ |
96b720b3 |
1104 | else if (telnet->closed_on_socket_error) |
1105 | return INT_MAX; /* a socket error counts as an unclean exit */ |
0da1a790 |
1106 | else |
1107 | /* Telnet doesn't transmit exit codes back to the client */ |
1108 | return 0; |
d8d6c7e5 |
1109 | } |
1110 | |
f89c3294 |
1111 | /* |
1112 | * cfg_info for Telnet does nothing at all. |
1113 | */ |
1114 | static int telnet_cfg_info(void *handle) |
1115 | { |
1116 | return 0; |
1117 | } |
1118 | |
374330e2 |
1119 | Backend telnet_backend = { |
1120 | telnet_init, |
fabd1805 |
1121 | telnet_free, |
86916870 |
1122 | telnet_reconfig, |
374330e2 |
1123 | telnet_send, |
5471d09a |
1124 | telnet_sendbuffer, |
374330e2 |
1125 | telnet_size, |
4017be6d |
1126 | telnet_special, |
125105d1 |
1127 | telnet_get_specials, |
6226c939 |
1128 | telnet_connected, |
d8d6c7e5 |
1129 | telnet_exitcode, |
97db3be4 |
1130 | telnet_sendok, |
0965bee0 |
1131 | telnet_ldisc, |
b9d7bcad |
1132 | telnet_provide_ldisc, |
a8327734 |
1133 | telnet_provide_logctx, |
5471d09a |
1134 | telnet_unthrottle, |
f89c3294 |
1135 | telnet_cfg_info, |
9e164d82 |
1136 | "telnet", |
1137 | PROT_TELNET, |
97db3be4 |
1138 | 23 |
374330e2 |
1139 | }; |