Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | #include "urldata.h" |
26 | | #include "telnet.h" |
27 | | |
28 | | #ifndef CURL_DISABLE_TELNET |
29 | | |
30 | | #ifdef HAVE_NETINET_IN_H |
31 | | #include <netinet/in.h> |
32 | | #endif |
33 | | #ifdef HAVE_NETDB_H |
34 | | #include <netdb.h> |
35 | | #endif |
36 | | #ifdef HAVE_ARPA_INET_H |
37 | | #include <arpa/inet.h> |
38 | | #endif |
39 | | #ifdef HAVE_NET_IF_H |
40 | | #include <net/if.h> |
41 | | #endif |
42 | | #ifdef HAVE_SYS_IOCTL_H |
43 | | #include <sys/ioctl.h> |
44 | | #endif |
45 | | |
46 | | #ifdef HAVE_SYS_PARAM_H |
47 | | #include <sys/param.h> |
48 | | #endif |
49 | | |
50 | | #include "url.h" |
51 | | #include "transfer.h" |
52 | | #include "sendf.h" |
53 | | #include "curl_trc.h" |
54 | | #include "progress.h" |
55 | | #include "arpa_telnet.h" |
56 | | #include "select.h" |
57 | | #include "curlx/strparse.h" |
58 | | |
59 | | #define SUBBUFSIZE 512 |
60 | | |
61 | 0 | #define CURL_SB_CLEAR(x) x->subpointer = (x)->subbuffer |
62 | | #define CURL_SB_TERM(x) \ |
63 | 0 | do { \ |
64 | 0 | (x)->subend = (x)->subpointer; \ |
65 | 0 | CURL_SB_CLEAR(x); \ |
66 | 0 | } while(0) |
67 | | #define CURL_SB_ACCUM(x, c) \ |
68 | 0 | do { \ |
69 | 0 | if((x)->subpointer < ((x)->subbuffer + sizeof((x)->subbuffer))) \ |
70 | 0 | *(x)->subpointer++ = (c); \ |
71 | 0 | } while(0) |
72 | | |
73 | 0 | #define CURL_SB_GET(x) ((*(x)->subpointer++) & 0xff) |
74 | 0 | #define CURL_SB_LEN(x) ((x)->subend - (x)->subpointer) |
75 | | |
76 | | /* For posterity: |
77 | | #define CURL_SB_PEEK(x) ((*x->subpointer)&0xff) |
78 | | #define CURL_SB_EOF(x) (x->subpointer >= x->subend) */ |
79 | | |
80 | | /* For negotiation compliant to RFC 1143 */ |
81 | 0 | #define CURL_NO 0 |
82 | 0 | #define CURL_YES 1 |
83 | 0 | #define CURL_WANTYES 2 |
84 | 0 | #define CURL_WANTNO 3 |
85 | | |
86 | 0 | #define CURL_EMPTY 0 |
87 | 0 | #define CURL_OPPOSITE 1 |
88 | | |
89 | | /* meta key for storing protocol meta at easy handle */ |
90 | 0 | #define CURL_META_TELNET_EASY "meta:proto:telnet:easy" |
91 | | |
92 | | /* |
93 | | * Telnet receiver states for fsm |
94 | | */ |
95 | | typedef enum { |
96 | | CURL_TS_DATA = 0, |
97 | | CURL_TS_IAC, |
98 | | CURL_TS_WILL, |
99 | | CURL_TS_WONT, |
100 | | CURL_TS_DO, |
101 | | CURL_TS_DONT, |
102 | | CURL_TS_CR, |
103 | | CURL_TS_SB, /* sub-option collection */ |
104 | | CURL_TS_SE /* looking for sub-option end */ |
105 | | } TelnetReceive; |
106 | | |
107 | | struct TELNET { |
108 | | int please_negotiate; |
109 | | int already_negotiated; |
110 | | int us[256]; |
111 | | int usq[256]; |
112 | | int us_preferred[256]; |
113 | | int him[256]; |
114 | | int himq[256]; |
115 | | int him_preferred[256]; |
116 | | int subnegotiation[256]; |
117 | | const char *subopt_ttype; /* Set with suboption TTYPE */ |
118 | | const char *subopt_xdisploc; /* Set with suboption XDISPLOC */ |
119 | | unsigned short subopt_wsx; /* Set with suboption NAWS */ |
120 | | unsigned short subopt_wsy; /* Set with suboption NAWS */ |
121 | | TelnetReceive telrcv_state; |
122 | | struct curl_slist *telnet_vars; /* Environment variables */ |
123 | | struct dynbuf out; /* output buffer */ |
124 | | |
125 | | /* suboptions */ |
126 | | unsigned char subbuffer[SUBBUFSIZE]; |
127 | | unsigned char *subpointer, *subend; /* buffer for sub-options */ |
128 | | }; |
129 | | |
130 | | #ifndef CURLVERBOSE |
131 | | #define printoption(a, b, c, d) Curl_nop_stmt |
132 | | #else |
133 | | static void printoption(struct Curl_easy *data, |
134 | | const char *direction, int cmd, int option) |
135 | 0 | { |
136 | 0 | if(data->set.verbose) { |
137 | 0 | if(cmd == CURL_IAC) { |
138 | 0 | if(CURL_TELCMD_OK(option)) |
139 | 0 | infof(data, "%s IAC %s", direction, CURL_TELCMD(option)); |
140 | 0 | else |
141 | 0 | infof(data, "%s IAC %d", direction, option); |
142 | 0 | } |
143 | 0 | else { |
144 | 0 | const char *fmt = (cmd == CURL_WILL) ? "WILL" : |
145 | 0 | (cmd == CURL_WONT) ? "WONT" : |
146 | 0 | (cmd == CURL_DO) ? "DO" : |
147 | 0 | (cmd == CURL_DONT) ? "DONT" : 0; |
148 | 0 | if(fmt) { |
149 | 0 | const char *opt; |
150 | 0 | if(CURL_TELOPT_OK(option)) |
151 | 0 | opt = CURL_TELOPT(option); |
152 | 0 | else if(option == CURL_TELOPT_EXOPL) |
153 | 0 | opt = "EXOPL"; |
154 | 0 | else |
155 | 0 | opt = NULL; |
156 | |
|
157 | 0 | if(opt) |
158 | 0 | infof(data, "%s %s %s", direction, fmt, opt); |
159 | 0 | else |
160 | 0 | infof(data, "%s %s %d", direction, fmt, option); |
161 | 0 | } |
162 | 0 | else |
163 | 0 | infof(data, "%s %d %d", direction, cmd, option); |
164 | 0 | } |
165 | 0 | } |
166 | 0 | } |
167 | | #endif /* !CURLVERBOSE */ |
168 | | |
169 | | static void telnet_easy_dtor(void *key, size_t klen, void *entry) |
170 | 0 | { |
171 | 0 | struct TELNET *tn = entry; |
172 | 0 | (void)key; |
173 | 0 | (void)klen; |
174 | 0 | curl_slist_free_all(tn->telnet_vars); |
175 | 0 | curlx_dyn_free(&tn->out); |
176 | 0 | curlx_free(tn); |
177 | 0 | } |
178 | | |
179 | | static CURLcode init_telnet(struct Curl_easy *data) |
180 | 0 | { |
181 | 0 | struct TELNET *tn; |
182 | |
|
183 | 0 | tn = curlx_calloc(1, sizeof(struct TELNET)); |
184 | 0 | if(!tn) |
185 | 0 | return CURLE_OUT_OF_MEMORY; |
186 | | |
187 | 0 | curlx_dyn_init(&tn->out, 0xffff); |
188 | |
|
189 | 0 | tn->telrcv_state = CURL_TS_DATA; |
190 | | |
191 | | /* Init suboptions */ |
192 | 0 | CURL_SB_CLEAR(tn); |
193 | | |
194 | | /* Set the options we want by default */ |
195 | 0 | tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES; |
196 | 0 | tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES; |
197 | | |
198 | | /* To be compliant with previous releases of libcurl |
199 | | we enable this option by default. This behavior |
200 | | can be changed thanks to the "BINARY" option in |
201 | | CURLOPT_TELNETOPTIONS |
202 | | */ |
203 | 0 | tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES; |
204 | 0 | tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES; |
205 | | |
206 | | /* We must allow the server to echo what we sent |
207 | | but it is not necessary to request the server |
208 | | to do so (it might forces the server to close |
209 | | the connection). Hence, we ignore ECHO in the |
210 | | negotiate function |
211 | | */ |
212 | 0 | tn->him_preferred[CURL_TELOPT_ECHO] = CURL_YES; |
213 | | |
214 | | /* Set the subnegotiation fields to send information after negotiation |
215 | | passed (do/will) |
216 | | |
217 | | Default values are (0,0) initialized by calloc. |
218 | | According to the RFC1013 it is valid: |
219 | | A value equal to zero is acceptable for the width (or height), |
220 | | and means that no character width (or height) is being sent. |
221 | | In this case, the width (or height) that will be assumed by the |
222 | | Telnet server is operating system specific (it will probably be |
223 | | based upon the terminal type information that may have been sent |
224 | | using the TERMINAL TYPE Telnet option). */ |
225 | 0 | tn->subnegotiation[CURL_TELOPT_NAWS] = CURL_YES; |
226 | |
|
227 | 0 | return Curl_meta_set(data, CURL_META_TELNET_EASY, tn, telnet_easy_dtor); |
228 | 0 | } |
229 | | |
230 | | static void send_negotiation(struct Curl_easy *data, int cmd, int option) |
231 | 0 | { |
232 | 0 | unsigned char buf[3]; |
233 | 0 | ssize_t bytes_written; |
234 | 0 | struct connectdata *conn = data->conn; |
235 | |
|
236 | 0 | buf[0] = CURL_IAC; |
237 | 0 | buf[1] = (unsigned char)cmd; |
238 | 0 | buf[2] = (unsigned char)option; |
239 | |
|
240 | 0 | bytes_written = swrite(conn->sock[FIRSTSOCKET], buf, 3); |
241 | 0 | if(bytes_written < 0) { |
242 | 0 | int err = SOCKERRNO; |
243 | 0 | failf(data, "Sending data failed (%d)", err); |
244 | 0 | } |
245 | |
|
246 | 0 | printoption(data, "SENT", cmd, option); |
247 | 0 | } |
248 | | |
249 | | static void set_remote_option(struct Curl_easy *data, struct TELNET *tn, |
250 | | int option, int newstate) |
251 | 0 | { |
252 | 0 | if(newstate == CURL_YES) { |
253 | 0 | switch(tn->him[option]) { |
254 | 0 | case CURL_NO: |
255 | 0 | tn->him[option] = CURL_WANTYES; |
256 | 0 | send_negotiation(data, CURL_DO, option); |
257 | 0 | break; |
258 | | |
259 | 0 | case CURL_YES: |
260 | | /* Already enabled */ |
261 | 0 | break; |
262 | | |
263 | 0 | case CURL_WANTNO: |
264 | 0 | switch(tn->himq[option]) { |
265 | 0 | case CURL_EMPTY: |
266 | | /* Already negotiating for CURL_YES, queue the request */ |
267 | 0 | tn->himq[option] = CURL_OPPOSITE; |
268 | 0 | break; |
269 | 0 | case CURL_OPPOSITE: |
270 | | /* Error: already queued an enable request */ |
271 | 0 | break; |
272 | 0 | } |
273 | 0 | break; |
274 | | |
275 | 0 | case CURL_WANTYES: |
276 | 0 | switch(tn->himq[option]) { |
277 | 0 | case CURL_EMPTY: |
278 | | /* Error: already negotiating for enable */ |
279 | 0 | break; |
280 | 0 | case CURL_OPPOSITE: |
281 | 0 | tn->himq[option] = CURL_EMPTY; |
282 | 0 | break; |
283 | 0 | } |
284 | 0 | break; |
285 | 0 | } |
286 | 0 | } |
287 | 0 | else { /* NO */ |
288 | 0 | switch(tn->him[option]) { |
289 | 0 | case CURL_NO: |
290 | | /* Already disabled */ |
291 | 0 | break; |
292 | | |
293 | 0 | case CURL_YES: |
294 | 0 | tn->him[option] = CURL_WANTNO; |
295 | 0 | send_negotiation(data, CURL_DONT, option); |
296 | 0 | break; |
297 | | |
298 | 0 | case CURL_WANTNO: |
299 | 0 | switch(tn->himq[option]) { |
300 | 0 | case CURL_EMPTY: |
301 | | /* Already negotiating for NO */ |
302 | 0 | break; |
303 | 0 | case CURL_OPPOSITE: |
304 | 0 | tn->himq[option] = CURL_EMPTY; |
305 | 0 | break; |
306 | 0 | } |
307 | 0 | break; |
308 | | |
309 | 0 | case CURL_WANTYES: |
310 | 0 | switch(tn->himq[option]) { |
311 | 0 | case CURL_EMPTY: |
312 | 0 | tn->himq[option] = CURL_OPPOSITE; |
313 | 0 | break; |
314 | 0 | case CURL_OPPOSITE: |
315 | 0 | break; |
316 | 0 | } |
317 | 0 | break; |
318 | 0 | } |
319 | 0 | } |
320 | 0 | } |
321 | | |
322 | | static void set_local_option(struct Curl_easy *data, struct TELNET *tn, |
323 | | int option, int newstate) |
324 | 0 | { |
325 | 0 | if(newstate == CURL_YES) { |
326 | 0 | switch(tn->us[option]) { |
327 | 0 | case CURL_NO: |
328 | 0 | tn->us[option] = CURL_WANTYES; |
329 | 0 | send_negotiation(data, CURL_WILL, option); |
330 | 0 | break; |
331 | | |
332 | 0 | case CURL_YES: |
333 | | /* Already enabled */ |
334 | 0 | break; |
335 | | |
336 | 0 | case CURL_WANTNO: |
337 | 0 | switch(tn->usq[option]) { |
338 | 0 | case CURL_EMPTY: |
339 | | /* Already negotiating for CURL_YES, queue the request */ |
340 | 0 | tn->usq[option] = CURL_OPPOSITE; |
341 | 0 | break; |
342 | 0 | case CURL_OPPOSITE: |
343 | | /* Error: already queued an enable request */ |
344 | 0 | break; |
345 | 0 | } |
346 | 0 | break; |
347 | | |
348 | 0 | case CURL_WANTYES: |
349 | 0 | switch(tn->usq[option]) { |
350 | 0 | case CURL_EMPTY: |
351 | | /* Error: already negotiating for enable */ |
352 | 0 | break; |
353 | 0 | case CURL_OPPOSITE: |
354 | 0 | tn->usq[option] = CURL_EMPTY; |
355 | 0 | break; |
356 | 0 | } |
357 | 0 | break; |
358 | 0 | } |
359 | 0 | } |
360 | 0 | else { /* NO */ |
361 | 0 | switch(tn->us[option]) { |
362 | 0 | case CURL_NO: |
363 | | /* Already disabled */ |
364 | 0 | break; |
365 | | |
366 | 0 | case CURL_YES: |
367 | 0 | tn->us[option] = CURL_WANTNO; |
368 | 0 | send_negotiation(data, CURL_WONT, option); |
369 | 0 | break; |
370 | | |
371 | 0 | case CURL_WANTNO: |
372 | 0 | switch(tn->usq[option]) { |
373 | 0 | case CURL_EMPTY: |
374 | | /* Already negotiating for NO */ |
375 | 0 | break; |
376 | 0 | case CURL_OPPOSITE: |
377 | 0 | tn->usq[option] = CURL_EMPTY; |
378 | 0 | break; |
379 | 0 | } |
380 | 0 | break; |
381 | | |
382 | 0 | case CURL_WANTYES: |
383 | 0 | switch(tn->usq[option]) { |
384 | 0 | case CURL_EMPTY: |
385 | 0 | tn->usq[option] = CURL_OPPOSITE; |
386 | 0 | break; |
387 | 0 | case CURL_OPPOSITE: |
388 | 0 | break; |
389 | 0 | } |
390 | 0 | break; |
391 | 0 | } |
392 | 0 | } |
393 | 0 | } |
394 | | |
395 | | static void telnet_negotiate(struct Curl_easy *data, struct TELNET *tn) |
396 | 0 | { |
397 | 0 | int i; |
398 | |
|
399 | 0 | for(i = 0; i < CURL_NTELOPTS; i++) { |
400 | 0 | if(i == CURL_TELOPT_ECHO) |
401 | 0 | continue; |
402 | | |
403 | 0 | if(tn->us_preferred[i] == CURL_YES) |
404 | 0 | set_local_option(data, tn, i, CURL_YES); |
405 | |
|
406 | 0 | if(tn->him_preferred[i] == CURL_YES) |
407 | 0 | set_remote_option(data, tn, i, CURL_YES); |
408 | 0 | } |
409 | 0 | } |
410 | | |
411 | | static void rec_will(struct Curl_easy *data, struct TELNET *tn, int option) |
412 | 0 | { |
413 | 0 | switch(tn->him[option]) { |
414 | 0 | case CURL_NO: |
415 | 0 | if(tn->him_preferred[option] == CURL_YES) { |
416 | 0 | tn->him[option] = CURL_YES; |
417 | 0 | send_negotiation(data, CURL_DO, option); |
418 | 0 | } |
419 | 0 | else |
420 | 0 | send_negotiation(data, CURL_DONT, option); |
421 | |
|
422 | 0 | break; |
423 | | |
424 | 0 | case CURL_YES: |
425 | | /* Already enabled */ |
426 | 0 | break; |
427 | | |
428 | 0 | case CURL_WANTNO: |
429 | 0 | switch(tn->himq[option]) { |
430 | 0 | case CURL_EMPTY: |
431 | | /* Error: DONT answered by WILL */ |
432 | 0 | tn->him[option] = CURL_NO; |
433 | 0 | break; |
434 | 0 | case CURL_OPPOSITE: |
435 | | /* Error: DONT answered by WILL */ |
436 | 0 | tn->him[option] = CURL_YES; |
437 | 0 | tn->himq[option] = CURL_EMPTY; |
438 | 0 | break; |
439 | 0 | } |
440 | 0 | break; |
441 | | |
442 | 0 | case CURL_WANTYES: |
443 | 0 | switch(tn->himq[option]) { |
444 | 0 | case CURL_EMPTY: |
445 | 0 | tn->him[option] = CURL_YES; |
446 | 0 | break; |
447 | 0 | case CURL_OPPOSITE: |
448 | 0 | tn->him[option] = CURL_WANTNO; |
449 | 0 | tn->himq[option] = CURL_EMPTY; |
450 | 0 | send_negotiation(data, CURL_DONT, option); |
451 | 0 | break; |
452 | 0 | } |
453 | 0 | break; |
454 | 0 | } |
455 | 0 | } |
456 | | |
457 | | static void rec_wont(struct Curl_easy *data, struct TELNET *tn, int option) |
458 | 0 | { |
459 | 0 | switch(tn->him[option]) { |
460 | 0 | case CURL_NO: |
461 | | /* Already disabled */ |
462 | 0 | break; |
463 | | |
464 | 0 | case CURL_YES: |
465 | 0 | tn->him[option] = CURL_NO; |
466 | 0 | send_negotiation(data, CURL_DONT, option); |
467 | 0 | break; |
468 | | |
469 | 0 | case CURL_WANTNO: |
470 | 0 | switch(tn->himq[option]) { |
471 | 0 | case CURL_EMPTY: |
472 | 0 | tn->him[option] = CURL_NO; |
473 | 0 | break; |
474 | | |
475 | 0 | case CURL_OPPOSITE: |
476 | 0 | tn->him[option] = CURL_WANTYES; |
477 | 0 | tn->himq[option] = CURL_EMPTY; |
478 | 0 | send_negotiation(data, CURL_DO, option); |
479 | 0 | break; |
480 | 0 | } |
481 | 0 | break; |
482 | | |
483 | 0 | case CURL_WANTYES: |
484 | 0 | switch(tn->himq[option]) { |
485 | 0 | case CURL_EMPTY: |
486 | 0 | tn->him[option] = CURL_NO; |
487 | 0 | break; |
488 | 0 | case CURL_OPPOSITE: |
489 | 0 | tn->him[option] = CURL_NO; |
490 | 0 | tn->himq[option] = CURL_EMPTY; |
491 | 0 | break; |
492 | 0 | } |
493 | 0 | break; |
494 | 0 | } |
495 | 0 | } |
496 | | |
497 | | static void printsub(struct Curl_easy *data, |
498 | | int direction, /* '<' or '>' */ |
499 | | const unsigned char *pointer, /* ptr to suboption data */ |
500 | | size_t length) /* suboption data length */ |
501 | 0 | { |
502 | 0 | if(data->set.verbose) { |
503 | 0 | unsigned int i = 0; |
504 | 0 | if(direction) { |
505 | 0 | infof(data, "%s IAC SB ", (direction == '<') ? "RCVD" : "SENT"); |
506 | 0 | if(length >= 3) { |
507 | 0 | int j; |
508 | |
|
509 | 0 | i = pointer[length - 2]; |
510 | 0 | j = pointer[length - 1]; |
511 | |
|
512 | 0 | if(i != CURL_IAC || j != CURL_SE) { |
513 | 0 | infof(data, "(terminated by "); |
514 | 0 | if(CURL_TELOPT_OK(i)) |
515 | 0 | infof(data, "%s ", CURL_TELOPT(i)); |
516 | 0 | else if(CURL_TELCMD_OK(i)) |
517 | 0 | infof(data, "%s ", CURL_TELCMD(i)); |
518 | 0 | else |
519 | 0 | infof(data, "%u ", i); |
520 | 0 | if(CURL_TELOPT_OK(j)) |
521 | 0 | infof(data, "%s", CURL_TELOPT(j)); |
522 | 0 | else if(CURL_TELCMD_OK(j)) |
523 | 0 | infof(data, "%s", CURL_TELCMD(j)); |
524 | 0 | else |
525 | 0 | infof(data, "%d", j); |
526 | 0 | infof(data, ", not IAC SE) "); |
527 | 0 | } |
528 | 0 | } |
529 | 0 | if(length >= 2) |
530 | 0 | length -= 2; |
531 | 0 | else /* bad input */ |
532 | 0 | return; |
533 | 0 | } |
534 | 0 | if(length <= 1) { |
535 | 0 | infof(data, "(Empty suboption?)"); |
536 | 0 | return; |
537 | 0 | } |
538 | | |
539 | 0 | if(CURL_TELOPT_OK(pointer[0])) { |
540 | 0 | switch(pointer[0]) { |
541 | 0 | case CURL_TELOPT_TTYPE: |
542 | 0 | case CURL_TELOPT_XDISPLOC: |
543 | 0 | case CURL_TELOPT_NEW_ENVIRON: |
544 | 0 | case CURL_TELOPT_NAWS: |
545 | 0 | infof(data, "%s", CURL_TELOPT(pointer[0])); |
546 | 0 | break; |
547 | 0 | default: |
548 | 0 | infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0])); |
549 | 0 | break; |
550 | 0 | } |
551 | 0 | } |
552 | 0 | else |
553 | 0 | infof(data, "%d (unknown)", pointer[0]); |
554 | | |
555 | 0 | switch(pointer[0]) { |
556 | 0 | case CURL_TELOPT_NAWS: |
557 | 0 | if(length > 4) |
558 | 0 | infof(data, "Width: %d ; Height: %d", (pointer[1] << 8) | pointer[2], |
559 | 0 | (pointer[3] << 8) | pointer[4]); |
560 | 0 | break; |
561 | 0 | default: |
562 | 0 | switch(pointer[1]) { |
563 | 0 | case CURL_TELQUAL_IS: |
564 | 0 | infof(data, " IS"); |
565 | 0 | break; |
566 | 0 | case CURL_TELQUAL_SEND: |
567 | 0 | infof(data, " SEND"); |
568 | 0 | break; |
569 | 0 | case CURL_TELQUAL_INFO: |
570 | 0 | infof(data, " INFO/REPLY"); |
571 | 0 | break; |
572 | 0 | case CURL_TELQUAL_NAME: |
573 | 0 | infof(data, " NAME"); |
574 | 0 | break; |
575 | 0 | } |
576 | | |
577 | 0 | switch(pointer[0]) { |
578 | 0 | case CURL_TELOPT_TTYPE: |
579 | 0 | case CURL_TELOPT_XDISPLOC: |
580 | 0 | infof(data, " \"%.*s\"", |
581 | 0 | (int)((length > 2) ? (length - 2) : 0), &pointer[2]); |
582 | 0 | break; |
583 | 0 | case CURL_TELOPT_NEW_ENVIRON: |
584 | 0 | if(pointer[1] == CURL_TELQUAL_IS) { |
585 | 0 | infof(data, " "); |
586 | 0 | for(i = 3; i < length; i++) { |
587 | 0 | switch(pointer[i]) { |
588 | 0 | case CURL_NEW_ENV_VAR: |
589 | 0 | infof(data, ", "); |
590 | 0 | break; |
591 | 0 | case CURL_NEW_ENV_VALUE: |
592 | 0 | infof(data, " = "); |
593 | 0 | break; |
594 | 0 | default: |
595 | 0 | infof(data, "%c", pointer[i]); |
596 | 0 | break; |
597 | 0 | } |
598 | 0 | } |
599 | 0 | } |
600 | 0 | break; |
601 | 0 | default: |
602 | 0 | for(i = 2; i < length; i++) |
603 | 0 | infof(data, " %.2x", pointer[i]); |
604 | 0 | break; |
605 | 0 | } |
606 | 0 | } |
607 | 0 | } |
608 | 0 | } |
609 | | |
610 | | /* Escape and send a telnet data block */ |
611 | | static CURLcode send_telnet_data(struct Curl_easy *data, |
612 | | struct TELNET *tn, |
613 | | const char *buffer, ssize_t nread) |
614 | 0 | { |
615 | 0 | size_t i, outlen; |
616 | 0 | const unsigned char *outbuf; |
617 | 0 | CURLcode result = CURLE_OK; |
618 | 0 | size_t bytes_written; |
619 | 0 | size_t total_written = 0; |
620 | 0 | struct connectdata *conn = data->conn; |
621 | |
|
622 | 0 | DEBUGASSERT(tn); |
623 | 0 | DEBUGASSERT(nread > 0); |
624 | 0 | if(nread < 0) |
625 | 0 | return CURLE_TOO_LARGE; |
626 | | |
627 | 0 | if(memchr(buffer, CURL_IAC, nread)) { |
628 | | /* only use the escape buffer when necessary */ |
629 | 0 | curlx_dyn_reset(&tn->out); |
630 | |
|
631 | 0 | for(i = 0; i < (size_t)nread && !result; i++) { |
632 | 0 | result = curlx_dyn_addn(&tn->out, &buffer[i], 1); |
633 | 0 | if(!result && ((unsigned char)buffer[i] == CURL_IAC)) |
634 | | /* IAC is FF in hex */ |
635 | 0 | result = curlx_dyn_addn(&tn->out, "\xff", 1); |
636 | 0 | } |
637 | |
|
638 | 0 | outlen = curlx_dyn_len(&tn->out); |
639 | 0 | outbuf = curlx_dyn_uptr(&tn->out); |
640 | 0 | } |
641 | 0 | else { |
642 | 0 | outlen = (size_t)nread; |
643 | 0 | outbuf = (const unsigned char *)buffer; |
644 | 0 | } |
645 | 0 | while(!result && total_written < outlen) { |
646 | | /* Make sure socket is writable to avoid EWOULDBLOCK condition */ |
647 | 0 | struct pollfd pfd[1]; |
648 | 0 | pfd[0].fd = conn->sock[FIRSTSOCKET]; |
649 | 0 | pfd[0].events = POLLOUT; |
650 | 0 | switch(Curl_poll(pfd, 1, -1)) { |
651 | 0 | case -1: /* error, abort writing */ |
652 | 0 | case 0: /* timeout (will never happen) */ |
653 | 0 | result = CURLE_SEND_ERROR; |
654 | 0 | break; |
655 | 0 | default: /* write! */ |
656 | 0 | bytes_written = 0; |
657 | 0 | result = Curl_xfer_send(data, outbuf + total_written, |
658 | 0 | outlen - total_written, FALSE, &bytes_written); |
659 | 0 | total_written += bytes_written; |
660 | 0 | break; |
661 | 0 | } |
662 | 0 | } |
663 | | |
664 | 0 | return result; |
665 | 0 | } |
666 | | |
667 | | /* |
668 | | * sendsuboption() |
669 | | * |
670 | | * Send suboption information to the server side. |
671 | | */ |
672 | | static void sendsuboption(struct Curl_easy *data, |
673 | | struct TELNET *tn, int option) |
674 | 0 | { |
675 | 0 | ssize_t bytes_written; |
676 | 0 | int err; |
677 | 0 | unsigned short x, y; |
678 | 0 | const unsigned char *uc1, *uc2; |
679 | 0 | struct connectdata *conn = data->conn; |
680 | |
|
681 | 0 | switch(option) { |
682 | 0 | case CURL_TELOPT_NAWS: |
683 | | /* We prepare data to be sent */ |
684 | 0 | CURL_SB_CLEAR(tn); |
685 | 0 | CURL_SB_ACCUM(tn, CURL_IAC); |
686 | 0 | CURL_SB_ACCUM(tn, CURL_SB); |
687 | 0 | CURL_SB_ACCUM(tn, CURL_TELOPT_NAWS); |
688 | | /* We must deal either with little or big endian processors */ |
689 | | /* Window size must be sent according to the 'network order' */ |
690 | 0 | x = htons(tn->subopt_wsx); |
691 | 0 | y = htons(tn->subopt_wsy); |
692 | 0 | uc1 = (const unsigned char *)&x; |
693 | 0 | uc2 = (const unsigned char *)&y; |
694 | 0 | CURL_SB_ACCUM(tn, uc1[0]); |
695 | 0 | CURL_SB_ACCUM(tn, uc1[1]); |
696 | 0 | CURL_SB_ACCUM(tn, uc2[0]); |
697 | 0 | CURL_SB_ACCUM(tn, uc2[1]); |
698 | |
|
699 | 0 | CURL_SB_ACCUM(tn, CURL_IAC); |
700 | 0 | CURL_SB_ACCUM(tn, CURL_SE); |
701 | 0 | CURL_SB_TERM(tn); |
702 | | /* data suboption is now ready */ |
703 | |
|
704 | 0 | printsub(data, '>', (const unsigned char *)tn->subbuffer + 2, |
705 | 0 | CURL_SB_LEN(tn) - 2); |
706 | | |
707 | | /* we send the header of the suboption... */ |
708 | 0 | bytes_written = swrite(conn->sock[FIRSTSOCKET], tn->subbuffer, 3); |
709 | 0 | if(bytes_written < 0) { |
710 | 0 | err = SOCKERRNO; |
711 | 0 | failf(data, "Sending data failed (%d)", err); |
712 | 0 | } |
713 | | /* ... then the window size with the send_telnet_data() function |
714 | | to deal with 0xFF cases ... */ |
715 | 0 | send_telnet_data(data, tn, (const char *)tn->subbuffer + 3, 4); |
716 | | /* ... and the footer */ |
717 | 0 | bytes_written = swrite(conn->sock[FIRSTSOCKET], tn->subbuffer + 7, 2); |
718 | 0 | if(bytes_written < 0) { |
719 | 0 | err = SOCKERRNO; |
720 | 0 | failf(data, "Sending data failed (%d)", err); |
721 | 0 | } |
722 | 0 | break; |
723 | 0 | } |
724 | 0 | } |
725 | | |
726 | | static void rec_do(struct Curl_easy *data, struct TELNET *tn, int option) |
727 | 0 | { |
728 | 0 | switch(tn->us[option]) { |
729 | 0 | case CURL_NO: |
730 | 0 | if(tn->us_preferred[option] == CURL_YES) { |
731 | 0 | tn->us[option] = CURL_YES; |
732 | 0 | send_negotiation(data, CURL_WILL, option); |
733 | 0 | if(tn->subnegotiation[option] == CURL_YES) |
734 | | /* transmission of data option */ |
735 | 0 | sendsuboption(data, tn, option); |
736 | 0 | } |
737 | 0 | else if(tn->subnegotiation[option] == CURL_YES) { |
738 | | /* send information to achieve this option */ |
739 | 0 | tn->us[option] = CURL_YES; |
740 | 0 | send_negotiation(data, CURL_WILL, option); |
741 | 0 | sendsuboption(data, tn, option); |
742 | 0 | } |
743 | 0 | else |
744 | 0 | send_negotiation(data, CURL_WONT, option); |
745 | 0 | break; |
746 | | |
747 | 0 | case CURL_YES: |
748 | | /* Already enabled */ |
749 | 0 | break; |
750 | | |
751 | 0 | case CURL_WANTNO: |
752 | 0 | switch(tn->usq[option]) { |
753 | 0 | case CURL_EMPTY: |
754 | | /* Error: DONT answered by WILL */ |
755 | 0 | tn->us[option] = CURL_NO; |
756 | 0 | break; |
757 | 0 | case CURL_OPPOSITE: |
758 | | /* Error: DONT answered by WILL */ |
759 | 0 | tn->us[option] = CURL_YES; |
760 | 0 | tn->usq[option] = CURL_EMPTY; |
761 | 0 | break; |
762 | 0 | } |
763 | 0 | break; |
764 | | |
765 | 0 | case CURL_WANTYES: |
766 | 0 | switch(tn->usq[option]) { |
767 | 0 | case CURL_EMPTY: |
768 | 0 | tn->us[option] = CURL_YES; |
769 | 0 | if(tn->subnegotiation[option] == CURL_YES) { |
770 | | /* transmission of data option */ |
771 | 0 | sendsuboption(data, tn, option); |
772 | 0 | } |
773 | 0 | break; |
774 | 0 | case CURL_OPPOSITE: |
775 | 0 | tn->us[option] = CURL_WANTNO; |
776 | 0 | tn->himq[option] = CURL_EMPTY; |
777 | 0 | send_negotiation(data, CURL_WONT, option); |
778 | 0 | break; |
779 | 0 | } |
780 | 0 | break; |
781 | 0 | } |
782 | 0 | } |
783 | | |
784 | | static void rec_dont(struct Curl_easy *data, struct TELNET *tn, int option) |
785 | 0 | { |
786 | 0 | switch(tn->us[option]) { |
787 | 0 | case CURL_NO: |
788 | | /* Already disabled */ |
789 | 0 | break; |
790 | | |
791 | 0 | case CURL_YES: |
792 | 0 | tn->us[option] = CURL_NO; |
793 | 0 | send_negotiation(data, CURL_WONT, option); |
794 | 0 | break; |
795 | | |
796 | 0 | case CURL_WANTNO: |
797 | 0 | switch(tn->usq[option]) { |
798 | 0 | case CURL_EMPTY: |
799 | 0 | tn->us[option] = CURL_NO; |
800 | 0 | break; |
801 | | |
802 | 0 | case CURL_OPPOSITE: |
803 | 0 | tn->us[option] = CURL_WANTYES; |
804 | 0 | tn->usq[option] = CURL_EMPTY; |
805 | 0 | send_negotiation(data, CURL_WILL, option); |
806 | 0 | break; |
807 | 0 | } |
808 | 0 | break; |
809 | | |
810 | 0 | case CURL_WANTYES: |
811 | 0 | switch(tn->usq[option]) { |
812 | 0 | case CURL_EMPTY: |
813 | 0 | tn->us[option] = CURL_NO; |
814 | 0 | break; |
815 | 0 | case CURL_OPPOSITE: |
816 | 0 | tn->us[option] = CURL_NO; |
817 | 0 | tn->usq[option] = CURL_EMPTY; |
818 | 0 | break; |
819 | 0 | } |
820 | 0 | break; |
821 | 0 | } |
822 | 0 | } |
823 | | |
824 | | static bool str_is_nonascii(const char *str) |
825 | 0 | { |
826 | 0 | char c; |
827 | 0 | while((c = *str++) != 0) |
828 | 0 | if(c & 0x80) |
829 | 0 | return TRUE; |
830 | | |
831 | 0 | return FALSE; |
832 | 0 | } |
833 | | |
834 | | static CURLcode check_telnet_options(struct Curl_easy *data, |
835 | | struct TELNET *tn) |
836 | 0 | { |
837 | 0 | struct curl_slist *head; |
838 | 0 | struct curl_slist *beg; |
839 | 0 | CURLcode result = CURLE_OK; |
840 | | |
841 | | /* Add the username as an environment variable if it |
842 | | was given on the command line */ |
843 | 0 | if(data->state.aptr.user) { |
844 | 0 | char buffer[256]; |
845 | 0 | if(str_is_nonascii(data->conn->user)) { |
846 | 0 | DEBUGF(infof(data, "set a non ASCII username in telnet")); |
847 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
848 | 0 | } |
849 | 0 | curl_msnprintf(buffer, sizeof(buffer), "USER,%s", data->conn->user); |
850 | 0 | beg = curl_slist_append(tn->telnet_vars, buffer); |
851 | 0 | if(!beg) { |
852 | 0 | curl_slist_free_all(tn->telnet_vars); |
853 | 0 | tn->telnet_vars = NULL; |
854 | 0 | return CURLE_OUT_OF_MEMORY; |
855 | 0 | } |
856 | 0 | tn->telnet_vars = beg; |
857 | 0 | tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES; |
858 | 0 | } |
859 | | |
860 | 0 | for(head = data->set.telnet_options; head && !result; head = head->next) { |
861 | 0 | size_t olen; |
862 | 0 | const char *option = head->data; |
863 | 0 | const char *arg; |
864 | 0 | const char *sep = strchr(option, '='); |
865 | 0 | if(sep) { |
866 | 0 | olen = sep - option; |
867 | 0 | arg = ++sep; |
868 | 0 | if(str_is_nonascii(arg)) |
869 | 0 | continue; |
870 | 0 | switch(olen) { |
871 | 0 | case 5: |
872 | | /* Terminal type */ |
873 | 0 | if(curl_strnequal(option, "TTYPE", 5)) { |
874 | 0 | tn->subopt_ttype = arg; |
875 | 0 | tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES; |
876 | 0 | break; |
877 | 0 | } |
878 | 0 | result = CURLE_UNKNOWN_OPTION; |
879 | 0 | break; |
880 | | |
881 | 0 | case 8: |
882 | | /* Display variable */ |
883 | 0 | if(curl_strnequal(option, "XDISPLOC", 8)) { |
884 | 0 | tn->subopt_xdisploc = arg; |
885 | 0 | tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES; |
886 | 0 | break; |
887 | 0 | } |
888 | 0 | result = CURLE_UNKNOWN_OPTION; |
889 | 0 | break; |
890 | | |
891 | 0 | case 7: |
892 | | /* Environment variable */ |
893 | 0 | if(curl_strnequal(option, "NEW_ENV", 7)) { |
894 | 0 | beg = curl_slist_append(tn->telnet_vars, arg); |
895 | 0 | if(!beg) { |
896 | 0 | result = CURLE_OUT_OF_MEMORY; |
897 | 0 | break; |
898 | 0 | } |
899 | 0 | tn->telnet_vars = beg; |
900 | 0 | tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES; |
901 | 0 | } |
902 | 0 | else |
903 | 0 | result = CURLE_UNKNOWN_OPTION; |
904 | 0 | break; |
905 | | |
906 | 0 | case 2: |
907 | | /* Window Size */ |
908 | 0 | if(curl_strnequal(option, "WS", 2)) { |
909 | 0 | const char *p = arg; |
910 | 0 | curl_off_t x = 0; |
911 | 0 | curl_off_t y = 0; |
912 | 0 | if(curlx_str_number(&p, &x, 0xffff) || |
913 | 0 | curlx_str_single(&p, 'x') || |
914 | 0 | curlx_str_number(&p, &y, 0xffff)) { |
915 | 0 | failf(data, "Syntax error in telnet option: %s", head->data); |
916 | 0 | result = CURLE_SETOPT_OPTION_SYNTAX; |
917 | 0 | } |
918 | 0 | else { |
919 | 0 | tn->subopt_wsx = (unsigned short)x; |
920 | 0 | tn->subopt_wsy = (unsigned short)y; |
921 | 0 | tn->us_preferred[CURL_TELOPT_NAWS] = CURL_YES; |
922 | 0 | } |
923 | 0 | } |
924 | 0 | else |
925 | 0 | result = CURLE_UNKNOWN_OPTION; |
926 | 0 | break; |
927 | | |
928 | 0 | case 6: |
929 | | /* To take care or not of the 8th bit in data exchange */ |
930 | 0 | if(curl_strnequal(option, "BINARY", 6)) { |
931 | 0 | const char *p = arg; |
932 | 0 | curl_off_t binary_option; |
933 | 0 | if(!curlx_str_number(&p, &binary_option, 1) && |
934 | 0 | (binary_option != 1)) { |
935 | 0 | tn->us_preferred[CURL_TELOPT_BINARY] = CURL_NO; |
936 | 0 | tn->him_preferred[CURL_TELOPT_BINARY] = CURL_NO; |
937 | 0 | } |
938 | 0 | } |
939 | 0 | else |
940 | 0 | result = CURLE_UNKNOWN_OPTION; |
941 | 0 | break; |
942 | 0 | default: |
943 | 0 | failf(data, "Unknown telnet option %s", head->data); |
944 | 0 | result = CURLE_UNKNOWN_OPTION; |
945 | 0 | break; |
946 | 0 | } |
947 | 0 | } |
948 | 0 | else { |
949 | 0 | failf(data, "Syntax error in telnet option: %s", head->data); |
950 | 0 | result = CURLE_SETOPT_OPTION_SYNTAX; |
951 | 0 | } |
952 | 0 | } |
953 | | |
954 | 0 | if(result) { |
955 | 0 | curl_slist_free_all(tn->telnet_vars); |
956 | 0 | tn->telnet_vars = NULL; |
957 | 0 | } |
958 | |
|
959 | 0 | return result; |
960 | 0 | } |
961 | | |
962 | | /* if the option contains an IAC code, it should be escaped in the output, but |
963 | | as we cannot think of any legit way to send that as part of the content we |
964 | | rather ban its use instead */ |
965 | | static bool bad_option(const char *data) |
966 | 0 | { |
967 | 0 | return !data || !!strchr(data, CURL_IAC); |
968 | 0 | } |
969 | | |
970 | | /* |
971 | | * suboption() |
972 | | * |
973 | | * Look at the sub-option buffer, and try to be helpful to the other |
974 | | * side. |
975 | | */ |
976 | | static CURLcode suboption(struct Curl_easy *data, struct TELNET *tn) |
977 | 0 | { |
978 | 0 | struct curl_slist *v; |
979 | 0 | unsigned char temp[2048]; |
980 | 0 | ssize_t bytes_written; |
981 | 0 | size_t len; |
982 | 0 | int err; |
983 | 0 | struct connectdata *conn = data->conn; |
984 | |
|
985 | 0 | if(!CURL_SB_LEN(tn)) /* ignore empty suboption */ |
986 | 0 | return CURLE_OK; |
987 | | |
988 | 0 | printsub(data, '<', (const unsigned char *)tn->subbuffer, |
989 | 0 | CURL_SB_LEN(tn) + 2); |
990 | 0 | switch(CURL_SB_GET(tn)) { |
991 | 0 | case CURL_TELOPT_TTYPE: |
992 | 0 | if(bad_option(tn->subopt_ttype)) |
993 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
994 | 0 | if(strlen(tn->subopt_ttype) > 1000) { |
995 | 0 | failf(data, "Tool long telnet TTYPE"); |
996 | 0 | return CURLE_SEND_ERROR; |
997 | 0 | } |
998 | 0 | len = curl_msnprintf((char *)temp, sizeof(temp), "%c%c%c%c%s%c%c", |
999 | 0 | CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE, |
1000 | 0 | CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, |
1001 | 0 | CURL_SE); |
1002 | 0 | bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len); |
1003 | |
|
1004 | 0 | if(bytes_written < 0) { |
1005 | 0 | err = SOCKERRNO; |
1006 | 0 | failf(data, "Sending data failed (%d)", err); |
1007 | 0 | return CURLE_SEND_ERROR; |
1008 | 0 | } |
1009 | 0 | printsub(data, '>', &temp[2], len-2); |
1010 | 0 | break; |
1011 | 0 | case CURL_TELOPT_XDISPLOC: |
1012 | 0 | if(bad_option(tn->subopt_xdisploc)) |
1013 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1014 | 0 | if(strlen(tn->subopt_xdisploc) > 1000) { |
1015 | 0 | failf(data, "Tool long telnet XDISPLOC"); |
1016 | 0 | return CURLE_SEND_ERROR; |
1017 | 0 | } |
1018 | 0 | len = curl_msnprintf((char *)temp, sizeof(temp), "%c%c%c%c%s%c%c", |
1019 | 0 | CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC, |
1020 | 0 | CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, |
1021 | 0 | CURL_SE); |
1022 | 0 | bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len); |
1023 | 0 | if(bytes_written < 0) { |
1024 | 0 | err = SOCKERRNO; |
1025 | 0 | failf(data, "Sending data failed (%d)", err); |
1026 | 0 | return CURLE_SEND_ERROR; |
1027 | 0 | } |
1028 | 0 | printsub(data, '>', &temp[2], len - 2); |
1029 | 0 | break; |
1030 | 0 | case CURL_TELOPT_NEW_ENVIRON: |
1031 | 0 | len = curl_msnprintf((char *)temp, sizeof(temp), "%c%c%c%c", |
1032 | 0 | CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON, |
1033 | 0 | CURL_TELQUAL_IS); |
1034 | 0 | for(v = tn->telnet_vars; v; v = v->next) { |
1035 | 0 | size_t tmplen = (strlen(v->data) + 1); |
1036 | 0 | if(bad_option(v->data)) |
1037 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1038 | | /* Add the variable if it fits */ |
1039 | 0 | if(len + tmplen < (int)sizeof(temp) - 6) { |
1040 | 0 | const char *s = strchr(v->data, ','); |
1041 | 0 | if(!s) |
1042 | 0 | len += curl_msnprintf((char *)&temp[len], sizeof(temp) - len, |
1043 | 0 | "%c%s", CURL_NEW_ENV_VAR, v->data); |
1044 | 0 | else { |
1045 | 0 | size_t vlen = s - v->data; |
1046 | 0 | len += curl_msnprintf((char *)&temp[len], sizeof(temp) - len, |
1047 | 0 | "%c%.*s%c%s", CURL_NEW_ENV_VAR, |
1048 | 0 | (int)vlen, v->data, CURL_NEW_ENV_VALUE, ++s); |
1049 | 0 | } |
1050 | 0 | } |
1051 | 0 | } |
1052 | 0 | curl_msnprintf((char *)&temp[len], sizeof(temp) - len, |
1053 | 0 | "%c%c", CURL_IAC, CURL_SE); |
1054 | 0 | len += 2; |
1055 | 0 | bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len); |
1056 | 0 | if(bytes_written < 0) { |
1057 | 0 | err = SOCKERRNO; |
1058 | 0 | failf(data, "Sending data failed (%d)", err); |
1059 | 0 | } |
1060 | 0 | printsub(data, '>', &temp[2], len - 2); |
1061 | 0 | break; |
1062 | 0 | } |
1063 | 0 | return CURLE_OK; |
1064 | 0 | } |
1065 | | |
1066 | | static CURLcode telrcv(struct Curl_easy *data, |
1067 | | struct TELNET *tn, |
1068 | | const unsigned char *inbuf, /* Data received from |
1069 | | socket */ |
1070 | | ssize_t count) /* Number of bytes |
1071 | | received */ |
1072 | 0 | { |
1073 | 0 | unsigned char c; |
1074 | 0 | CURLcode result; |
1075 | 0 | int in = 0; |
1076 | 0 | int startwrite = -1; |
1077 | |
|
1078 | 0 | #define startskipping() \ |
1079 | 0 | if(startwrite >= 0) { \ |
1080 | 0 | result = Curl_client_write(data, \ |
1081 | 0 | CLIENTWRITE_BODY, \ |
1082 | 0 | (const char *)&inbuf[startwrite], \ |
1083 | 0 | in-startwrite); \ |
1084 | 0 | if(result) \ |
1085 | 0 | return result; \ |
1086 | 0 | } \ |
1087 | 0 | startwrite = -1 |
1088 | |
|
1089 | 0 | #define writebyte() \ |
1090 | 0 | if(startwrite < 0) \ |
1091 | 0 | startwrite = in |
1092 | |
|
1093 | 0 | #define bufferflush() startskipping() |
1094 | |
|
1095 | 0 | while(count--) { |
1096 | 0 | c = inbuf[in]; |
1097 | |
|
1098 | 0 | switch(tn->telrcv_state) { |
1099 | 0 | case CURL_TS_CR: |
1100 | 0 | tn->telrcv_state = CURL_TS_DATA; |
1101 | 0 | if(c == '\0') { |
1102 | 0 | startskipping(); |
1103 | 0 | break; /* Ignore \0 after CR */ |
1104 | 0 | } |
1105 | 0 | writebyte(); |
1106 | 0 | break; |
1107 | | |
1108 | 0 | case CURL_TS_DATA: |
1109 | 0 | if(c == CURL_IAC) { |
1110 | 0 | tn->telrcv_state = CURL_TS_IAC; |
1111 | 0 | startskipping(); |
1112 | 0 | break; |
1113 | 0 | } |
1114 | 0 | else if(c == '\r') |
1115 | 0 | tn->telrcv_state = CURL_TS_CR; |
1116 | 0 | writebyte(); |
1117 | 0 | break; |
1118 | | |
1119 | 0 | case CURL_TS_IAC: |
1120 | 0 | DEBUGASSERT(startwrite < 0); |
1121 | 0 | switch(c) { |
1122 | 0 | case CURL_WILL: |
1123 | 0 | tn->telrcv_state = CURL_TS_WILL; |
1124 | 0 | break; |
1125 | 0 | case CURL_WONT: |
1126 | 0 | tn->telrcv_state = CURL_TS_WONT; |
1127 | 0 | break; |
1128 | 0 | case CURL_DO: |
1129 | 0 | tn->telrcv_state = CURL_TS_DO; |
1130 | 0 | break; |
1131 | 0 | case CURL_DONT: |
1132 | 0 | tn->telrcv_state = CURL_TS_DONT; |
1133 | 0 | break; |
1134 | 0 | case CURL_SB: |
1135 | 0 | CURL_SB_CLEAR(tn); |
1136 | 0 | tn->telrcv_state = CURL_TS_SB; |
1137 | 0 | break; |
1138 | 0 | case CURL_IAC: |
1139 | 0 | tn->telrcv_state = CURL_TS_DATA; |
1140 | 0 | writebyte(); |
1141 | 0 | break; |
1142 | 0 | case CURL_DM: |
1143 | 0 | case CURL_NOP: |
1144 | 0 | case CURL_GA: |
1145 | 0 | default: |
1146 | 0 | tn->telrcv_state = CURL_TS_DATA; |
1147 | 0 | printoption(data, "RCVD", CURL_IAC, c); |
1148 | 0 | break; |
1149 | 0 | } |
1150 | 0 | break; |
1151 | | |
1152 | 0 | case CURL_TS_WILL: |
1153 | 0 | printoption(data, "RCVD", CURL_WILL, c); |
1154 | 0 | tn->please_negotiate = 1; |
1155 | 0 | rec_will(data, tn, c); |
1156 | 0 | tn->telrcv_state = CURL_TS_DATA; |
1157 | 0 | break; |
1158 | | |
1159 | 0 | case CURL_TS_WONT: |
1160 | 0 | printoption(data, "RCVD", CURL_WONT, c); |
1161 | 0 | tn->please_negotiate = 1; |
1162 | 0 | rec_wont(data, tn, c); |
1163 | 0 | tn->telrcv_state = CURL_TS_DATA; |
1164 | 0 | break; |
1165 | | |
1166 | 0 | case CURL_TS_DO: |
1167 | 0 | printoption(data, "RCVD", CURL_DO, c); |
1168 | 0 | tn->please_negotiate = 1; |
1169 | 0 | rec_do(data, tn, c); |
1170 | 0 | tn->telrcv_state = CURL_TS_DATA; |
1171 | 0 | break; |
1172 | | |
1173 | 0 | case CURL_TS_DONT: |
1174 | 0 | printoption(data, "RCVD", CURL_DONT, c); |
1175 | 0 | tn->please_negotiate = 1; |
1176 | 0 | rec_dont(data, tn, c); |
1177 | 0 | tn->telrcv_state = CURL_TS_DATA; |
1178 | 0 | break; |
1179 | | |
1180 | 0 | case CURL_TS_SB: |
1181 | 0 | if(c == CURL_IAC) |
1182 | 0 | tn->telrcv_state = CURL_TS_SE; |
1183 | 0 | else |
1184 | 0 | CURL_SB_ACCUM(tn, c); |
1185 | 0 | break; |
1186 | | |
1187 | 0 | case CURL_TS_SE: |
1188 | 0 | if(c != CURL_SE) { |
1189 | 0 | if(c != CURL_IAC) { |
1190 | | /* |
1191 | | * This is an error. We only expect to get "IAC IAC" or "IAC SE". |
1192 | | * Several things may have happened. An IAC was not doubled, the IAC |
1193 | | * SE was left off, or another option got inserted into the |
1194 | | * suboption are all possibilities. |
1195 | | */ |
1196 | 0 | failf(data, "telnet: suboption error"); |
1197 | 0 | return CURLE_RECV_ERROR; |
1198 | 0 | } |
1199 | 0 | CURL_SB_ACCUM(tn, c); |
1200 | 0 | tn->telrcv_state = CURL_TS_SB; |
1201 | 0 | } |
1202 | 0 | else { |
1203 | 0 | CURL_SB_ACCUM(tn, CURL_IAC); |
1204 | 0 | CURL_SB_ACCUM(tn, CURL_SE); |
1205 | 0 | tn->subpointer -= 2; |
1206 | 0 | CURL_SB_TERM(tn); |
1207 | 0 | result = suboption(data, tn); /* handle sub-option */ |
1208 | 0 | if(result) |
1209 | 0 | return result; |
1210 | 0 | tn->telrcv_state = CURL_TS_DATA; |
1211 | 0 | } |
1212 | 0 | break; |
1213 | 0 | } |
1214 | 0 | ++in; |
1215 | 0 | } |
1216 | 0 | bufferflush(); |
1217 | 0 | return CURLE_OK; |
1218 | 0 | } |
1219 | | |
1220 | | static CURLcode telnet_done(struct Curl_easy *data, |
1221 | | CURLcode status, bool premature) |
1222 | 0 | { |
1223 | 0 | (void)status; |
1224 | 0 | (void)premature; |
1225 | 0 | Curl_meta_remove(data, CURL_META_TELNET_EASY); |
1226 | 0 | return CURLE_OK; |
1227 | 0 | } |
1228 | | |
1229 | | static CURLcode telnet_do(struct Curl_easy *data, bool *done) |
1230 | 0 | { |
1231 | 0 | CURLcode result; |
1232 | 0 | struct connectdata *conn = data->conn; |
1233 | 0 | curl_socket_t sockfd = conn->sock[FIRSTSOCKET]; |
1234 | | #ifdef USE_WINSOCK |
1235 | | WSAEVENT event_handle; |
1236 | | WSANETWORKEVENTS events; |
1237 | | HANDLE stdin_handle; |
1238 | | HANDLE objs[2]; |
1239 | | DWORD obj_count; |
1240 | | DWORD wait_timeout; |
1241 | | DWORD readfile_read; |
1242 | | int err; |
1243 | | #else |
1244 | 0 | timediff_t interval_ms; |
1245 | 0 | struct pollfd pfd[2]; |
1246 | 0 | int poll_cnt; |
1247 | 0 | ssize_t snread; |
1248 | 0 | #endif |
1249 | 0 | bool keepon = TRUE; |
1250 | 0 | char buffer[4 * 1024]; |
1251 | 0 | struct TELNET *tn; |
1252 | |
|
1253 | 0 | *done = TRUE; /* unconditionally */ |
1254 | |
|
1255 | 0 | result = init_telnet(data); |
1256 | 0 | if(result) |
1257 | 0 | return result; |
1258 | | |
1259 | 0 | tn = Curl_meta_get(data, CURL_META_TELNET_EASY); |
1260 | 0 | if(!tn) |
1261 | 0 | return CURLE_FAILED_INIT; |
1262 | | |
1263 | 0 | result = check_telnet_options(data, tn); |
1264 | 0 | if(result) |
1265 | 0 | return result; |
1266 | | |
1267 | | #ifdef USE_WINSOCK |
1268 | | /* We want to wait for both stdin and the socket. Since |
1269 | | * the select() function in Winsock only works on sockets |
1270 | | * we have to use the WaitForMultipleObjects() call. |
1271 | | */ |
1272 | | |
1273 | | /* First, create a sockets event object */ |
1274 | | event_handle = WSACreateEvent(); |
1275 | | if(event_handle == WSA_INVALID_EVENT) { |
1276 | | failf(data, "WSACreateEvent failed (%d)", SOCKERRNO); |
1277 | | return CURLE_FAILED_INIT; |
1278 | | } |
1279 | | |
1280 | | /* Tell Winsock what events we want to listen to */ |
1281 | | if(WSAEventSelect(sockfd, event_handle, FD_READ | FD_CLOSE) != 0) { |
1282 | | WSACloseEvent(event_handle); |
1283 | | return CURLE_RECV_ERROR; |
1284 | | } |
1285 | | |
1286 | | /* The get the Windows file handle for stdin */ |
1287 | | stdin_handle = GetStdHandle(STD_INPUT_HANDLE); |
1288 | | |
1289 | | /* Create the list of objects to wait for */ |
1290 | | objs[0] = event_handle; |
1291 | | objs[1] = stdin_handle; |
1292 | | |
1293 | | /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it, |
1294 | | else use the old WaitForMultipleObjects() way */ |
1295 | | if(GetFileType(stdin_handle) == FILE_TYPE_PIPE || data->set.is_fread_set) { |
1296 | | /* Do not wait for stdin_handle, wait for event_handle */ |
1297 | | obj_count = 1; |
1298 | | /* Check stdin_handle per 100 milliseconds */ |
1299 | | wait_timeout = 100; |
1300 | | } |
1301 | | else { |
1302 | | obj_count = 2; |
1303 | | wait_timeout = 1000; |
1304 | | } |
1305 | | |
1306 | | /* Keep on listening and act on events */ |
1307 | | while(keepon) { |
1308 | | const DWORD buf_size = (DWORD)sizeof(buffer); |
1309 | | DWORD waitret = WaitForMultipleObjects(obj_count, objs, |
1310 | | FALSE, wait_timeout); |
1311 | | switch(waitret) { |
1312 | | |
1313 | | case WAIT_TIMEOUT: { |
1314 | | for(;;) { |
1315 | | if(data->set.is_fread_set) { |
1316 | | size_t n; |
1317 | | /* read from user-supplied method */ |
1318 | | n = data->state.fread_func(buffer, 1, buf_size, data->state.in); |
1319 | | if(n == CURL_READFUNC_ABORT) { |
1320 | | keepon = FALSE; |
1321 | | result = CURLE_READ_ERROR; |
1322 | | break; |
1323 | | } |
1324 | | |
1325 | | if(n == CURL_READFUNC_PAUSE) |
1326 | | break; |
1327 | | |
1328 | | if(n == 0) /* no bytes */ |
1329 | | break; |
1330 | | |
1331 | | /* fall through with number of bytes read */ |
1332 | | readfile_read = (DWORD)n; |
1333 | | } |
1334 | | else { |
1335 | | /* read from stdin */ |
1336 | | if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, |
1337 | | &readfile_read, NULL)) { |
1338 | | keepon = FALSE; |
1339 | | result = CURLE_READ_ERROR; |
1340 | | break; |
1341 | | } |
1342 | | |
1343 | | if(!readfile_read) |
1344 | | break; |
1345 | | |
1346 | | if(!ReadFile(stdin_handle, buffer, buf_size, &readfile_read, NULL)) { |
1347 | | keepon = FALSE; |
1348 | | result = CURLE_READ_ERROR; |
1349 | | break; |
1350 | | } |
1351 | | } |
1352 | | |
1353 | | result = send_telnet_data(data, tn, buffer, readfile_read); |
1354 | | if(result) { |
1355 | | keepon = FALSE; |
1356 | | break; |
1357 | | } |
1358 | | } |
1359 | | } |
1360 | | break; |
1361 | | |
1362 | | case WAIT_OBJECT_0 + 1: { |
1363 | | if(!ReadFile(stdin_handle, buffer, buf_size, &readfile_read, NULL)) { |
1364 | | keepon = FALSE; |
1365 | | result = CURLE_READ_ERROR; |
1366 | | break; |
1367 | | } |
1368 | | |
1369 | | result = send_telnet_data(data, tn, buffer, readfile_read); |
1370 | | if(result) { |
1371 | | keepon = FALSE; |
1372 | | break; |
1373 | | } |
1374 | | } |
1375 | | break; |
1376 | | |
1377 | | case WAIT_OBJECT_0: { |
1378 | | events.lNetworkEvents = 0; |
1379 | | if(WSAEnumNetworkEvents(sockfd, event_handle, &events) != 0) { |
1380 | | err = SOCKERRNO; |
1381 | | if(err != SOCKEINPROGRESS) { |
1382 | | infof(data, "WSAEnumNetworkEvents failed (%d)", err); |
1383 | | keepon = FALSE; |
1384 | | result = CURLE_READ_ERROR; |
1385 | | } |
1386 | | break; |
1387 | | } |
1388 | | if(events.lNetworkEvents & FD_READ) { |
1389 | | /* read data from network */ |
1390 | | size_t nread; |
1391 | | result = Curl_xfer_recv(data, buffer, sizeof(buffer), &nread); |
1392 | | /* read would have blocked. Loop again */ |
1393 | | if(result == CURLE_AGAIN) |
1394 | | break; |
1395 | | /* returned not-zero, this an error */ |
1396 | | else if(result) { |
1397 | | keepon = FALSE; |
1398 | | break; |
1399 | | } |
1400 | | /* returned zero but actually received 0 or less here, |
1401 | | the server closed the connection and we bail out */ |
1402 | | else if(!nread) { |
1403 | | keepon = FALSE; |
1404 | | break; |
1405 | | } |
1406 | | |
1407 | | result = telrcv(data, tn, (unsigned char *)buffer, nread); |
1408 | | if(result) { |
1409 | | keepon = FALSE; |
1410 | | break; |
1411 | | } |
1412 | | |
1413 | | /* Negotiate if the peer has started negotiating, |
1414 | | otherwise do not. We do not want to speak telnet with |
1415 | | non-telnet servers, like POP or SMTP. */ |
1416 | | if(tn->please_negotiate && !tn->already_negotiated) { |
1417 | | telnet_negotiate(data, tn); |
1418 | | tn->already_negotiated = 1; |
1419 | | } |
1420 | | } |
1421 | | if(events.lNetworkEvents & FD_CLOSE) { |
1422 | | keepon = FALSE; |
1423 | | } |
1424 | | break; |
1425 | | } |
1426 | | } /* switch */ |
1427 | | |
1428 | | if(data->set.timeout) { |
1429 | | if(curlx_ptimediff_ms(Curl_pgrs_now(data), &conn->created) >= |
1430 | | data->set.timeout) { |
1431 | | failf(data, "Time-out"); |
1432 | | result = CURLE_OPERATION_TIMEDOUT; |
1433 | | keepon = FALSE; |
1434 | | } |
1435 | | } |
1436 | | } |
1437 | | |
1438 | | /* We called WSACreateEvent, so call WSACloseEvent */ |
1439 | | if(!WSACloseEvent(event_handle)) { |
1440 | | infof(data, "WSACloseEvent failed (%d)", SOCKERRNO); |
1441 | | } |
1442 | | #else |
1443 | 0 | pfd[0].fd = sockfd; |
1444 | 0 | pfd[0].events = POLLIN; |
1445 | |
|
1446 | 0 | if(data->set.is_fread_set) { |
1447 | 0 | poll_cnt = 1; |
1448 | 0 | interval_ms = 100; /* poll user-supplied read function */ |
1449 | 0 | } |
1450 | 0 | else { |
1451 | | /* really using fread, so infile is a FILE* */ |
1452 | 0 | pfd[1].fd = fileno((FILE *)data->state.in); |
1453 | 0 | pfd[1].events = POLLIN; |
1454 | 0 | poll_cnt = 2; |
1455 | 0 | interval_ms = 1 * 1000; |
1456 | 0 | if(pfd[1].fd < 0) { |
1457 | 0 | failf(data, "cannot read input"); |
1458 | 0 | result = CURLE_RECV_ERROR; |
1459 | 0 | keepon = FALSE; |
1460 | 0 | } |
1461 | 0 | } |
1462 | |
|
1463 | 0 | while(keepon) { |
1464 | 0 | DEBUGF(infof(data, "telnet_do, poll %d fds", poll_cnt)); |
1465 | 0 | switch(Curl_poll(pfd, (unsigned int)poll_cnt, interval_ms)) { |
1466 | 0 | case -1: /* error, stop reading */ |
1467 | 0 | keepon = FALSE; |
1468 | 0 | continue; |
1469 | 0 | case 0: /* timeout */ |
1470 | 0 | pfd[0].revents = 0; |
1471 | 0 | pfd[1].revents = 0; |
1472 | 0 | FALLTHROUGH(); |
1473 | 0 | default: /* read! */ |
1474 | 0 | if(pfd[0].revents & POLLIN) { |
1475 | | /* read data from network */ |
1476 | 0 | size_t nread; |
1477 | 0 | result = Curl_xfer_recv(data, buffer, sizeof(buffer), &nread); |
1478 | | /* read would have blocked. Loop again */ |
1479 | 0 | if(result == CURLE_AGAIN) |
1480 | 0 | break; |
1481 | | /* returned not-zero, this an error */ |
1482 | 0 | if(result) { |
1483 | 0 | keepon = FALSE; |
1484 | | /* In test 1452, macOS sees a ECONNRESET sometimes? Is this the |
1485 | | * telnet test server not shutting down the socket in a clean way? |
1486 | | * Seems to be timing related, happens more on slow debug build */ |
1487 | 0 | if(data->state.os_errno == SOCKECONNRESET) { |
1488 | 0 | DEBUGF(infof(data, "telnet_do, unexpected ECONNRESET on recv")); |
1489 | 0 | } |
1490 | 0 | break; |
1491 | 0 | } |
1492 | | /* returned zero but actually received 0 or less here, |
1493 | | the server closed the connection and we bail out */ |
1494 | 0 | else if(!nread) { |
1495 | 0 | keepon = FALSE; |
1496 | 0 | break; |
1497 | 0 | } |
1498 | | |
1499 | 0 | Curl_pgrs_download_inc(data, nread); |
1500 | 0 | result = telrcv(data, tn, (unsigned char *)buffer, nread); |
1501 | 0 | if(result) { |
1502 | 0 | keepon = FALSE; |
1503 | 0 | break; |
1504 | 0 | } |
1505 | | |
1506 | | /* Negotiate if the peer has started negotiating, |
1507 | | otherwise do not. We do not want to speak telnet with |
1508 | | non-telnet servers, like POP or SMTP. */ |
1509 | 0 | if(tn->please_negotiate && !tn->already_negotiated) { |
1510 | 0 | telnet_negotiate(data, tn); |
1511 | 0 | tn->already_negotiated = 1; |
1512 | 0 | } |
1513 | 0 | } |
1514 | | |
1515 | 0 | snread = 0; |
1516 | 0 | if(poll_cnt == 2) { |
1517 | 0 | if(pfd[1].revents & POLLIN) { /* read from in file */ |
1518 | 0 | snread = read(pfd[1].fd, buffer, sizeof(buffer)); |
1519 | 0 | } |
1520 | 0 | } |
1521 | 0 | else { |
1522 | | /* read from user-supplied method */ |
1523 | 0 | snread = (int)data->state.fread_func(buffer, 1, sizeof(buffer), |
1524 | 0 | data->state.in); |
1525 | 0 | if(snread == CURL_READFUNC_ABORT) { |
1526 | 0 | keepon = FALSE; |
1527 | 0 | break; |
1528 | 0 | } |
1529 | 0 | if(snread == CURL_READFUNC_PAUSE) |
1530 | 0 | break; |
1531 | 0 | } |
1532 | | |
1533 | 0 | if(snread > 0) { |
1534 | 0 | result = send_telnet_data(data, tn, buffer, snread); |
1535 | 0 | if(result) { |
1536 | 0 | keepon = FALSE; |
1537 | 0 | break; |
1538 | 0 | } |
1539 | 0 | Curl_pgrs_upload_inc(data, (size_t)snread); |
1540 | 0 | } |
1541 | 0 | else if(snread < 0) |
1542 | 0 | keepon = FALSE; |
1543 | | |
1544 | 0 | break; |
1545 | 0 | } /* poll switch statement */ |
1546 | | |
1547 | 0 | if(data->set.timeout) { |
1548 | 0 | if(curlx_ptimediff_ms(Curl_pgrs_now(data), &conn->created) >= |
1549 | 0 | data->set.timeout) { |
1550 | 0 | failf(data, "Time-out"); |
1551 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
1552 | 0 | keepon = FALSE; |
1553 | 0 | } |
1554 | 0 | } |
1555 | |
|
1556 | 0 | if(!result) { |
1557 | 0 | result = Curl_pgrsUpdate(data); |
1558 | 0 | if(result) |
1559 | 0 | keepon = FALSE; |
1560 | 0 | } |
1561 | 0 | } |
1562 | 0 | #endif |
1563 | | /* mark this as "no further transfer wanted" */ |
1564 | 0 | Curl_xfer_setup_nop(data); |
1565 | |
|
1566 | 0 | return result; |
1567 | 0 | } |
1568 | | |
1569 | | /* |
1570 | | * TELNET protocol handler. |
1571 | | */ |
1572 | | const struct Curl_protocol Curl_protocol_telnet = { |
1573 | | ZERO_NULL, /* setup_connection */ |
1574 | | telnet_do, /* do_it */ |
1575 | | telnet_done, /* done */ |
1576 | | ZERO_NULL, /* do_more */ |
1577 | | ZERO_NULL, /* connect_it */ |
1578 | | ZERO_NULL, /* connecting */ |
1579 | | ZERO_NULL, /* doing */ |
1580 | | ZERO_NULL, /* proto_pollset */ |
1581 | | ZERO_NULL, /* doing_pollset */ |
1582 | | ZERO_NULL, /* domore_pollset */ |
1583 | | ZERO_NULL, /* perform_pollset */ |
1584 | | ZERO_NULL, /* disconnect */ |
1585 | | ZERO_NULL, /* write_resp */ |
1586 | | ZERO_NULL, /* write_resp_hd */ |
1587 | | ZERO_NULL, /* connection_is_dead */ |
1588 | | ZERO_NULL, /* attach connection */ |
1589 | | ZERO_NULL, /* follow */ |
1590 | | }; |
1591 | | |
1592 | | #endif /* !CURL_DISABLE_TELNET */ |