telnet.c 46 KB

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