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