telnet.c 44 KB

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