telnet.c 44 KB

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