telnet.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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_nwrite(data, 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. result = Curl_pgrsSetDownloadCounter(data, total_dl);
  1411. if(!result)
  1412. result = telrcv(data, (unsigned char *)buf, nread);
  1413. if(result) {
  1414. keepon = FALSE;
  1415. break;
  1416. }
  1417. /* Negotiate if the peer has started negotiating,
  1418. otherwise don't. We don't want to speak telnet with
  1419. non-telnet servers, like POP or SMTP. */
  1420. if(tn->please_negotiate && !tn->already_negotiated) {
  1421. negotiate(data);
  1422. tn->already_negotiated = 1;
  1423. }
  1424. }
  1425. nread = 0;
  1426. if(poll_cnt == 2) {
  1427. if(pfd[1].revents & POLLIN) { /* read from in file */
  1428. nread = read(pfd[1].fd, buf, data->set.buffer_size);
  1429. }
  1430. }
  1431. else {
  1432. /* read from user-supplied method */
  1433. nread = (int)data->state.fread_func(buf, 1, data->set.buffer_size,
  1434. data->state.in);
  1435. if(nread == CURL_READFUNC_ABORT) {
  1436. keepon = FALSE;
  1437. break;
  1438. }
  1439. if(nread == CURL_READFUNC_PAUSE)
  1440. break;
  1441. }
  1442. if(nread > 0) {
  1443. result = send_telnet_data(data, buf, nread);
  1444. if(result) {
  1445. keepon = FALSE;
  1446. break;
  1447. }
  1448. total_ul += nread;
  1449. Curl_pgrsSetUploadCounter(data, total_ul);
  1450. }
  1451. else if(nread < 0)
  1452. keepon = FALSE;
  1453. break;
  1454. } /* poll switch statement */
  1455. if(data->set.timeout) {
  1456. now = Curl_now();
  1457. if(Curl_timediff(now, conn->created) >= data->set.timeout) {
  1458. failf(data, "Time-out");
  1459. result = CURLE_OPERATION_TIMEDOUT;
  1460. keepon = FALSE;
  1461. }
  1462. }
  1463. if(Curl_pgrsUpdate(data)) {
  1464. result = CURLE_ABORTED_BY_CALLBACK;
  1465. break;
  1466. }
  1467. }
  1468. #endif
  1469. /* mark this as "no further transfer wanted" */
  1470. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  1471. return result;
  1472. }
  1473. #endif