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