libtelnet.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  1. /*
  2. * libtelnet - TELNET protocol handling library
  3. *
  4. * Sean Middleditch
  5. * sean@sourcemud.org
  6. *
  7. * The author or authors of this code dedicate any and all copyright interest
  8. * in this code to the public domain. We make this dedication for the benefit
  9. * of the public at large and to the detriment of our heirs and successors. We
  10. * intend this dedication to be an overt act of relinquishment in perpetuity of
  11. * all present and future rights to this code under copyright law.
  12. */
  13. #ifdef HAVE_CONFIG_H
  14. #include <config.h>
  15. #endif
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <stdarg.h>
  22. /* Win32 compatibility */
  23. #if defined(_WIN32)
  24. # define vsnprintf _vsnprintf
  25. # define __func__ __FUNCTION__
  26. # define ZLIB_WINAPI 1
  27. #endif
  28. #if defined(HAVE_ZLIB)
  29. # include <zlib.h>
  30. #endif
  31. #include "libtelnet.h"
  32. /* inlinable functions */
  33. #if defined(__GNUC__) || __STDC_VERSION__ >= 199901L
  34. # define INLINE __inline__
  35. #else
  36. # define INLINE
  37. #endif
  38. /* helper for Q-method option tracking */
  39. #define Q_US(q) ((q).state & 0x0F)
  40. #define Q_HIM(q) (((q).state & 0xF0) >> 4)
  41. #define Q_MAKE(us,him) ((us) | ((him) << 4))
  42. /* helper for the negotiation routines */
  43. #define NEGOTIATE_EVENT(telnet,cmd,opt) \
  44. ev.type = (cmd); \
  45. ev.neg.telopt = (opt); \
  46. (telnet)->eh((telnet), &ev, (telnet)->ud);
  47. /* telnet state codes */
  48. enum telnet_state_t {
  49. TELNET_STATE_DATA = 0,
  50. TELNET_STATE_IAC,
  51. TELNET_STATE_WILL,
  52. TELNET_STATE_WONT,
  53. TELNET_STATE_DO,
  54. TELNET_STATE_DONT,
  55. TELNET_STATE_SB,
  56. TELNET_STATE_SB_DATA,
  57. TELNET_STATE_SB_DATA_IAC
  58. };
  59. typedef enum telnet_state_t telnet_state_t;
  60. /* telnet state tracker */
  61. struct telnet_t {
  62. /* user data */
  63. void *ud;
  64. /* telopt support table */
  65. const telnet_telopt_t *telopts;
  66. /* event handler */
  67. telnet_event_handler_t eh;
  68. #if defined(HAVE_ZLIB)
  69. /* zlib (mccp2) compression */
  70. z_stream *z;
  71. #endif
  72. /* RFC1143 option negotiation states */
  73. struct telnet_rfc1143_t *q;
  74. /* sub-request buffer */
  75. char *buffer;
  76. /* current size of the buffer */
  77. size_t buffer_size;
  78. /* current buffer write position (also length of buffer data) */
  79. size_t buffer_pos;
  80. /* current state */
  81. enum telnet_state_t state;
  82. /* option flags */
  83. unsigned char flags;
  84. /* current subnegotiation telopt */
  85. unsigned char sb_telopt;
  86. /* length of RFC1143 queue */
  87. unsigned char q_size;
  88. };
  89. /* RFC1143 option negotiation state */
  90. typedef struct telnet_rfc1143_t {
  91. unsigned char telopt;
  92. unsigned char state;
  93. } telnet_rfc1143_t;
  94. /* RFC1143 state names */
  95. #define Q_NO 0
  96. #define Q_YES 1
  97. #define Q_WANTNO 2
  98. #define Q_WANTYES 3
  99. #define Q_WANTNO_OP 4
  100. #define Q_WANTYES_OP 5
  101. /* buffer sizes */
  102. static const size_t _buffer_sizes[] = { 0, 512, 2048, 8192, 16384, };
  103. static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) /
  104. sizeof(_buffer_sizes[0]);
  105. /* error generation function */
  106. static telnet_error_t _error(telnet_t *telnet, unsigned line,
  107. const char* func, telnet_error_t err, int fatal, const char *fmt,
  108. ...) {
  109. telnet_event_t ev;
  110. char buffer[512];
  111. va_list va;
  112. /* format informational text */
  113. va_start(va, fmt);
  114. vsnprintf(buffer, sizeof(buffer), fmt, va);
  115. va_end(va);
  116. /* send error event to the user */
  117. ev.type = fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING;
  118. ev.error.file = __FILE__;
  119. ev.error.func = func;
  120. ev.error.line = line;
  121. ev.error.msg = buffer;
  122. telnet->eh(telnet, &ev, telnet->ud);
  123. return err;
  124. }
  125. #if defined(HAVE_ZLIB)
  126. /* initialize the zlib box for a telnet box; if deflate is non-zero, it
  127. * initializes zlib for delating (compression), otherwise for inflating
  128. * (decompression). returns TELNET_EOK on success, something else on
  129. * failure.
  130. */
  131. telnet_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) {
  132. z_stream *z;
  133. int rs;
  134. /* if compression is already enabled, fail loudly */
  135. if (telnet->z != 0)
  136. return _error(telnet, __LINE__, __func__, TELNET_EBADVAL,
  137. err_fatal, "cannot initialize compression twice");
  138. /* allocate zstream box */
  139. if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0)
  140. return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal,
  141. "malloc() failed: %s", strerror(errno));
  142. /* initialize */
  143. if (deflate) {
  144. if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) {
  145. free(z);
  146. return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
  147. err_fatal, "deflateInit() failed: %s", zError(rs));
  148. }
  149. telnet->flags |= TELNET_PFLAG_DEFLATE;
  150. } else {
  151. if ((rs = inflateInit(z)) != Z_OK) {
  152. free(z);
  153. return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
  154. err_fatal, "inflateInit() failed: %s", zError(rs));
  155. }
  156. telnet->flags &= ~TELNET_PFLAG_DEFLATE;
  157. }
  158. telnet->z = z;
  159. return TELNET_EOK;
  160. }
  161. #endif /* defined(HAVE_ZLIB) */
  162. /* push bytes out, compressing them first if need be */
  163. static void _send(telnet_t *telnet, const char *buffer,
  164. size_t size) {
  165. telnet_event_t ev;
  166. #if defined(HAVE_ZLIB)
  167. /* if we have a deflate (compression) zlib box, use it */
  168. if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) {
  169. char deflate_buffer[1024];
  170. int rs;
  171. /* initialize z state */
  172. telnet->z->next_in = (unsigned char *)buffer;
  173. telnet->z->avail_in = (unsigned int)size;
  174. telnet->z->next_out = (unsigned char *)deflate_buffer;
  175. telnet->z->avail_out = sizeof(deflate_buffer);
  176. /* deflate until buffer exhausted and all output is produced */
  177. while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
  178. /* compress */
  179. if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) {
  180. _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
  181. "deflate() failed: %s", zError(rs));
  182. deflateEnd(telnet->z);
  183. free(telnet->z);
  184. telnet->z = 0;
  185. break;
  186. }
  187. /* send event */
  188. ev.type = TELNET_EV_SEND;
  189. ev.data.buffer = deflate_buffer;
  190. ev.data.size = sizeof(deflate_buffer) - telnet->z->avail_out;
  191. telnet->eh(telnet, &ev, telnet->ud);
  192. /* prepare output buffer for next run */
  193. telnet->z->next_out = (unsigned char *)deflate_buffer;
  194. telnet->z->avail_out = sizeof(deflate_buffer);
  195. }
  196. /* do not continue with remaining code */
  197. return;
  198. }
  199. #endif /* defined(HAVE_ZLIB) */
  200. ev.type = TELNET_EV_SEND;
  201. ev.data.buffer = buffer;
  202. ev.data.size = size;
  203. telnet->eh(telnet, &ev, telnet->ud);
  204. }
  205. /* to send bags of unsigned chars */
  206. #define _sendu(t, d, s) _send((t), (const char*)(d), (s))
  207. /* check if we support a particular telopt; if us is non-zero, we
  208. * check if we (local) supports it, otherwise we check if he (remote)
  209. * supports it. return non-zero if supported, zero if not supported.
  210. */
  211. static INLINE int _check_telopt(telnet_t *telnet, unsigned char telopt,
  212. int us) {
  213. int i;
  214. /* if we have no telopts table, we obviously don't support it */
  215. if (telnet->telopts == 0)
  216. return 0;
  217. /* loop unti found or end marker (us and him both 0) */
  218. for (i = 0; telnet->telopts[i].telopt != -1; ++i) {
  219. if (telnet->telopts[i].telopt == telopt) {
  220. if (us && telnet->telopts[i].us == TELNET_WILL)
  221. return 1;
  222. else if (!us && telnet->telopts[i].him == TELNET_DO)
  223. return 1;
  224. else
  225. return 0;
  226. }
  227. }
  228. /* not found, so not supported */
  229. return 0;
  230. }
  231. /* retrieve RFC1143 option state */
  232. static INLINE telnet_rfc1143_t _get_rfc1143(telnet_t *telnet,
  233. unsigned char telopt) {
  234. telnet_rfc1143_t empty;
  235. int i;
  236. /* search for entry */
  237. for (i = 0; i != telnet->q_size; ++i) {
  238. if (telnet->q[i].telopt == telopt) {
  239. return telnet->q[i];
  240. }
  241. }
  242. /* not found, return empty value */
  243. empty.telopt = telopt;
  244. empty.state = 0;
  245. return empty;
  246. }
  247. /* save RFC1143 option state */
  248. static INLINE void _set_rfc1143(telnet_t *telnet, unsigned char telopt,
  249. char us, char him) {
  250. telnet_rfc1143_t *qtmp;
  251. int i;
  252. /* search for entry */
  253. for (i = 0; i != telnet->q_size; ++i) {
  254. if (telnet->q[i].telopt == telopt) {
  255. telnet->q[i].state = Q_MAKE(us,him);
  256. return;
  257. }
  258. }
  259. /* we're going to need to track state for it, so grow the queue
  260. * by 4 (four) elements and put the telopt into it; bail on allocation
  261. * error. we go by four because it seems like a reasonable guess as
  262. * to the number of enabled options for most simple code, and it
  263. * allows for an acceptable number of reallocations for complex code.
  264. */
  265. if ((qtmp = (telnet_rfc1143_t *)realloc(telnet->q,
  266. sizeof(telnet_rfc1143_t) * (telnet->q_size + 4))) == 0) {
  267. _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
  268. "realloc() failed: %s", strerror(errno));
  269. return;
  270. }
  271. memset(&qtmp[telnet->q_size], 0, sizeof(telnet_rfc1143_t) * 4);
  272. telnet->q = qtmp;
  273. telnet->q[telnet->q_size].telopt = telopt;
  274. telnet->q[telnet->q_size].state = Q_MAKE(us, him);
  275. telnet->q_size += 4;
  276. }
  277. /* send negotiation bytes */
  278. static INLINE void _send_negotiate(telnet_t *telnet, unsigned char cmd,
  279. unsigned char telopt) {
  280. unsigned char bytes[3];
  281. bytes[0] = TELNET_IAC;
  282. bytes[1] = cmd;
  283. bytes[2] = telopt;
  284. _sendu(telnet, bytes, 3);
  285. }
  286. /* negotiation handling magic for RFC1143 */
  287. static void _negotiate(telnet_t *telnet, unsigned char telopt) {
  288. telnet_event_t ev;
  289. telnet_rfc1143_t q;
  290. /* in PROXY mode, just pass it thru and do nothing */
  291. if (telnet->flags & TELNET_FLAG_PROXY) {
  292. switch ((int)telnet->state) {
  293. case TELNET_STATE_WILL:
  294. NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
  295. break;
  296. case TELNET_STATE_WONT:
  297. NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
  298. break;
  299. case TELNET_STATE_DO:
  300. NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
  301. break;
  302. case TELNET_STATE_DONT:
  303. NEGOTIATE_EVENT(telnet, TELNET_EV_DONT, telopt);
  304. break;
  305. }
  306. return;
  307. }
  308. /* lookup the current state of the option */
  309. q = _get_rfc1143(telnet, telopt);
  310. /* start processing... */
  311. switch ((int)telnet->state) {
  312. /* request to enable option on remote end or confirm DO */
  313. case TELNET_STATE_WILL:
  314. switch (Q_HIM(q)) {
  315. case Q_NO:
  316. if (_check_telopt(telnet, telopt, 0)) {
  317. _set_rfc1143(telnet, telopt, Q_US(q), Q_YES);
  318. _send_negotiate(telnet, TELNET_DO, telopt);
  319. NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
  320. } else
  321. _send_negotiate(telnet, TELNET_DONT, telopt);
  322. break;
  323. case Q_WANTNO:
  324. _set_rfc1143(telnet, telopt, Q_US(q), Q_NO);
  325. NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
  326. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  327. "DONT answered by WILL");
  328. break;
  329. case Q_WANTNO_OP:
  330. _set_rfc1143(telnet, telopt, Q_US(q), Q_YES);
  331. NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
  332. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  333. "DONT answered by WILL");
  334. break;
  335. case Q_WANTYES:
  336. _set_rfc1143(telnet, telopt, Q_US(q), Q_YES);
  337. NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
  338. break;
  339. case Q_WANTYES_OP:
  340. _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO);
  341. _send_negotiate(telnet, TELNET_DONT, telopt);
  342. NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
  343. break;
  344. }
  345. break;
  346. /* request to disable option on remote end, confirm DONT, reject DO */
  347. case TELNET_STATE_WONT:
  348. switch (Q_HIM(q)) {
  349. case Q_YES:
  350. _set_rfc1143(telnet, telopt, Q_US(q), Q_NO);
  351. _send_negotiate(telnet, TELNET_DONT, telopt);
  352. NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
  353. break;
  354. case Q_WANTNO:
  355. _set_rfc1143(telnet, telopt, Q_US(q), Q_NO);
  356. NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
  357. break;
  358. case Q_WANTNO_OP:
  359. _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES);
  360. NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
  361. break;
  362. case Q_WANTYES:
  363. case Q_WANTYES_OP:
  364. _set_rfc1143(telnet, telopt, Q_US(q), Q_NO);
  365. break;
  366. }
  367. break;
  368. /* request to enable option on local end or confirm WILL */
  369. case TELNET_STATE_DO:
  370. switch (Q_US(q)) {
  371. case Q_NO:
  372. if (_check_telopt(telnet, telopt, 1)) {
  373. _set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q));
  374. _send_negotiate(telnet, TELNET_WILL, telopt);
  375. NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
  376. } else
  377. _send_negotiate(telnet, TELNET_WONT, telopt);
  378. break;
  379. case Q_WANTNO:
  380. _set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q));
  381. NEGOTIATE_EVENT(telnet, TELNET_EV_DONT, telopt);
  382. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  383. "WONT answered by DO");
  384. break;
  385. case Q_WANTNO_OP:
  386. _set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q));
  387. NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
  388. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  389. "WONT answered by DO");
  390. break;
  391. case Q_WANTYES:
  392. _set_rfc1143(telnet, telopt, Q_YES, Q_HIM(q));
  393. NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
  394. break;
  395. case Q_WANTYES_OP:
  396. _set_rfc1143(telnet, telopt, Q_WANTNO, Q_HIM(q));
  397. _send_negotiate(telnet, TELNET_WONT, telopt);
  398. NEGOTIATE_EVENT(telnet, TELNET_EV_DO, telopt);
  399. break;
  400. }
  401. break;
  402. /* request to disable option on local end, confirm WONT, reject WILL */
  403. case TELNET_STATE_DONT:
  404. switch (Q_US(q)) {
  405. case Q_YES:
  406. _set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q));
  407. _send_negotiate(telnet, TELNET_WONT, telopt);
  408. NEGOTIATE_EVENT(telnet, TELNET_EV_DONT, telopt);
  409. break;
  410. case Q_WANTNO:
  411. _set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q));
  412. NEGOTIATE_EVENT(telnet, TELNET_EV_WONT, telopt);
  413. break;
  414. case Q_WANTNO_OP:
  415. _set_rfc1143(telnet, telopt, Q_WANTYES, Q_HIM(q));
  416. _send_negotiate(telnet, TELNET_WILL, telopt);
  417. NEGOTIATE_EVENT(telnet, TELNET_EV_WILL, telopt);
  418. break;
  419. case Q_WANTYES:
  420. case Q_WANTYES_OP:
  421. _set_rfc1143(telnet, telopt, Q_NO, Q_HIM(q));
  422. break;
  423. }
  424. break;
  425. }
  426. }
  427. /* process an ENVIRON/NEW-ENVIRON subnegotiation buffer
  428. *
  429. * the algorithm and approach used here is kind of a hack,
  430. * but it reduces the number of memory allocations we have
  431. * to make.
  432. *
  433. * we copy the bytes back into the buffer, starting at the very
  434. * beginning, which makes it easy to handle the ENVIRON ESC
  435. * escape mechanism as well as ensure the variable name and
  436. * value strings are NUL-terminated, all while fitting inside
  437. * of the original buffer.
  438. */
  439. static int _environ_telnet(telnet_t *telnet, unsigned char type,
  440. char* buffer, size_t size) {
  441. telnet_event_t ev;
  442. struct telnet_environ_t *values = 0;
  443. char *c, *last, *out;
  444. size_t index, count;
  445. /* if we have no data, just pass it through */
  446. if (size == 0) {
  447. return 0;
  448. }
  449. /* first byte must be a valid command */
  450. if ((unsigned)buffer[0] != TELNET_ENVIRON_SEND &&
  451. (unsigned)buffer[0] != TELNET_ENVIRON_IS &&
  452. (unsigned)buffer[0] != TELNET_ENVIRON_INFO) {
  453. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  454. "telopt %d subneg has invalid command", type);
  455. return 0;
  456. }
  457. /* store ENVIRON command */
  458. ev.environ.cmd = buffer[0];
  459. /* if we have no arguments, send an event with no data end return */
  460. if (size == 1) {
  461. /* no list of variables given */
  462. ev.environ.values = 0;
  463. ev.environ.size = 0;
  464. /* invoke event with our arguments */
  465. ev.type = TELNET_EV_ENVIRON;
  466. telnet->eh(telnet, &ev, telnet->ud);
  467. return 1;
  468. }
  469. /* very second byte must be VAR or USERVAR, if present */
  470. if ((unsigned)buffer[1] != TELNET_ENVIRON_VAR &&
  471. (unsigned)buffer[1] != TELNET_ENVIRON_USERVAR) {
  472. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  473. "telopt %d subneg missing variable type", type);
  474. return 0;
  475. }
  476. /* ensure last byte is not an escape byte (makes parsing later easier) */
  477. if ((unsigned)buffer[size - 1] == TELNET_ENVIRON_ESC) {
  478. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  479. "telopt %d subneg ends with ESC", type);
  480. return 0;
  481. }
  482. /* count arguments; each valid entry starts with VAR or USERVAR */
  483. count = 0;
  484. for (c = buffer + 1; c < buffer + size; ++c) {
  485. if (*c == TELNET_ENVIRON_VAR || *c == TELNET_ENVIRON_USERVAR) {
  486. ++count;
  487. } else if (*c == TELNET_ENVIRON_ESC) {
  488. /* skip the next byte */
  489. ++c;
  490. }
  491. }
  492. /* allocate argument array, bail on error */
  493. if ((values = (struct telnet_environ_t *)calloc(count,
  494. sizeof(struct telnet_environ_t))) == 0) {
  495. _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
  496. "calloc() failed: %s", strerror(errno));
  497. return 0;
  498. }
  499. /* parse argument array strings */
  500. out = buffer;
  501. c = buffer + 1;
  502. for (index = 0; index != count; ++index) {
  503. /* remember the variable type (will be VAR or USERVAR) */
  504. values[index].type = *c++;
  505. /* scan until we find an end-marker, and buffer up unescaped
  506. * bytes into our buffer */
  507. last = out;
  508. while (c < buffer + size) {
  509. /* stop at the next variable or at the value */
  510. if ((unsigned)*c == TELNET_ENVIRON_VAR ||
  511. (unsigned)*c == TELNET_ENVIRON_VALUE ||
  512. (unsigned)*c == TELNET_ENVIRON_USERVAR) {
  513. break;
  514. }
  515. /* buffer next byte (taking into account ESC) */
  516. if (*c == TELNET_ENVIRON_ESC) {
  517. ++c;
  518. }
  519. *out++ = *c++;
  520. }
  521. *out++ = '\0';
  522. /* store the variable name we have just received */
  523. values[index].var = last;
  524. values[index].value = "";
  525. /* if we got a value, find the next end marker and
  526. * store the value; otherwise, store empty string */
  527. if (c < buffer + size && *c == TELNET_ENVIRON_VALUE) {
  528. ++c;
  529. last = out;
  530. while (c < buffer + size) {
  531. /* stop when we find the start of the next variable */
  532. if ((unsigned)*c == TELNET_ENVIRON_VAR ||
  533. (unsigned)*c == TELNET_ENVIRON_USERVAR) {
  534. break;
  535. }
  536. /* buffer next byte (taking into account ESC) */
  537. if (*c == TELNET_ENVIRON_ESC) {
  538. ++c;
  539. }
  540. *out++ = *c++;
  541. }
  542. *out++ = '\0';
  543. /* store the variable value */
  544. values[index].value = last;
  545. }
  546. }
  547. /* pass values array and count to event */
  548. ev.environ.values = values;
  549. ev.environ.size = count;
  550. /* invoke event with our arguments */
  551. ev.type = TELNET_EV_ENVIRON;
  552. telnet->eh(telnet, &ev, telnet->ud);
  553. /* clean up */
  554. free(values);
  555. return 1;
  556. }
  557. /* process an MSSP subnegotiation buffer */
  558. static int _mssp_telnet(telnet_t *telnet, char* buffer, size_t size) {
  559. telnet_event_t ev;
  560. struct telnet_environ_t *values;
  561. char *var = 0;
  562. char *c, *last, *out;
  563. size_t i, count;
  564. unsigned char next_type;
  565. /* if we have no data, just pass it through */
  566. if (size == 0) {
  567. return 0;
  568. }
  569. /* first byte must be a VAR */
  570. if ((unsigned)buffer[0] != TELNET_MSSP_VAR) {
  571. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  572. "MSSP subnegotiation has invalid data");
  573. return 0;
  574. }
  575. /* count the arguments, any part that starts with VALUE */
  576. for (count = 0, i = 0; i != size; ++i) {
  577. if ((unsigned)buffer[i] == TELNET_MSSP_VAL) {
  578. ++count;
  579. }
  580. }
  581. /* allocate argument array, bail on error */
  582. if ((values = (struct telnet_environ_t *)calloc(count,
  583. sizeof(struct telnet_environ_t))) == 0) {
  584. _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
  585. "calloc() failed: %s", strerror(errno));
  586. return 0;
  587. }
  588. ev.mssp.values = values;
  589. ev.mssp.size = count;
  590. /* allocate strings in argument array */
  591. out = last = buffer;
  592. next_type = buffer[0];
  593. for (i = 0, c = buffer + 1; c < buffer + size;) {
  594. /* search for end marker */
  595. while (c < buffer + size && (unsigned)*c != TELNET_MSSP_VAR &&
  596. (unsigned)*c != TELNET_MSSP_VAL) {
  597. *out++ = *c++;
  598. }
  599. *out++ = '\0';
  600. /* if it's a variable name, just store the name for now */
  601. if (next_type == TELNET_MSSP_VAR) {
  602. var = last;
  603. } else if (next_type == TELNET_MSSP_VAL && var != 0) {
  604. values[i].var = var;
  605. values[i].value = last;
  606. ++i;
  607. } else {
  608. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  609. "invalid MSSP subnegotiation data");
  610. free(values);
  611. return 0;
  612. }
  613. /* remember our next type and increment c for next loop run */
  614. last = out;
  615. next_type = *c++;
  616. }
  617. /* invoke event with our arguments */
  618. ev.type = TELNET_EV_MSSP;
  619. telnet->eh(telnet, &ev, telnet->ud);
  620. /* clean up */
  621. free(values);
  622. return 0;
  623. }
  624. /* parse ZMP command subnegotiation buffers */
  625. static int _zmp_telnet(telnet_t *telnet, const char* buffer, size_t size) {
  626. telnet_event_t ev;
  627. char **argv;
  628. const char *c;
  629. size_t i, argc;
  630. /* make sure this is a valid ZMP buffer */
  631. if (size == 0 || buffer[size - 1] != 0) {
  632. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  633. "incomplete ZMP frame");
  634. return 0;
  635. }
  636. /* count arguments */
  637. for (argc = 0, c = buffer; c != buffer + size; ++argc)
  638. c += strlen(c) + 1;
  639. /* allocate argument array, bail on error */
  640. if ((argv = (char **)calloc(argc, sizeof(char *))) == 0) {
  641. _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
  642. "calloc() failed: %s", strerror(errno));
  643. return 0;
  644. }
  645. /* populate argument array */
  646. for (i = 0, c = buffer; i != argc; ++i) {
  647. argv[i] = (char *)c;
  648. c += strlen(c) + 1;
  649. }
  650. /* invoke event with our arguments */
  651. ev.type = TELNET_EV_ZMP;
  652. ev.zmp.argv = (const char**)argv;
  653. ev.zmp.argc = argc;
  654. telnet->eh(telnet, &ev, telnet->ud);
  655. /* clean up */
  656. free(argv);
  657. return 0;
  658. }
  659. /* parse TERMINAL-TYPE command subnegotiation buffers */
  660. static int _ttype_telnet(telnet_t *telnet, const char* buffer, size_t size) {
  661. telnet_event_t ev;
  662. /* make sure request is not empty */
  663. if (size == 0) {
  664. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  665. "incomplete TERMINAL-TYPE request");
  666. return 0;
  667. }
  668. /* make sure request has valid command type */
  669. if (buffer[0] != TELNET_TTYPE_IS &&
  670. buffer[0] != TELNET_TTYPE_SEND) {
  671. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  672. "TERMINAL-TYPE request has invalid type");
  673. return 0;
  674. }
  675. /* send proper event */
  676. if (buffer[0] == TELNET_TTYPE_IS) {
  677. char *name;
  678. /* allocate space for name */
  679. if ((name = (char *)malloc(size)) == 0) {
  680. _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
  681. "malloc() failed: %s", strerror(errno));
  682. return 0;
  683. }
  684. memcpy(name, buffer + 1, size - 1);
  685. name[size - 1] = '\0';
  686. ev.type = TELNET_EV_TTYPE;
  687. ev.ttype.cmd = TELNET_TTYPE_IS;
  688. ev.ttype.name = name;
  689. telnet->eh(telnet, &ev, telnet->ud);
  690. /* clean up */
  691. free(name);
  692. } else {
  693. ev.type = TELNET_EV_TTYPE;
  694. ev.ttype.cmd = TELNET_TTYPE_SEND;
  695. ev.ttype.name = 0;
  696. telnet->eh(telnet, &ev, telnet->ud);
  697. }
  698. return 0;
  699. }
  700. /* process a subnegotiation buffer; return non-zero if the current buffer
  701. * must be aborted and reprocessed due to COMPRESS2 being activated
  702. */
  703. static int _subnegotiate(telnet_t *telnet) {
  704. telnet_event_t ev;
  705. /* standard subnegotiation event */
  706. ev.type = TELNET_EV_SUBNEGOTIATION;
  707. ev.sub.telopt = telnet->sb_telopt;
  708. ev.sub.buffer = telnet->buffer;
  709. ev.sub.size = telnet->buffer_pos;
  710. telnet->eh(telnet, &ev, telnet->ud);
  711. switch (telnet->sb_telopt) {
  712. #if defined(HAVE_ZLIB)
  713. /* received COMPRESS2 begin marker, setup our zlib box and
  714. * start handling the compressed stream if it's not already.
  715. */
  716. case TELNET_TELOPT_COMPRESS2:
  717. if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) {
  718. if (_init_zlib(telnet, 0, 1) != TELNET_EOK)
  719. return 0;
  720. /* notify app that compression was enabled */
  721. ev.type = TELNET_EV_COMPRESS;
  722. ev.compress.state = 1;
  723. telnet->eh(telnet, &ev, telnet->ud);
  724. return 1;
  725. }
  726. return 0;
  727. #endif /* defined(HAVE_ZLIB) */
  728. /* specially handled subnegotiation telopt types */
  729. case TELNET_TELOPT_ZMP:
  730. return _zmp_telnet(telnet, telnet->buffer, telnet->buffer_pos);
  731. case TELNET_TELOPT_TTYPE:
  732. return _ttype_telnet(telnet, telnet->buffer, telnet->buffer_pos);
  733. case TELNET_TELOPT_ENVIRON:
  734. case TELNET_TELOPT_NEW_ENVIRON:
  735. return _environ_telnet(telnet, telnet->sb_telopt, telnet->buffer,
  736. telnet->buffer_pos);
  737. case TELNET_TELOPT_MSSP:
  738. return _mssp_telnet(telnet, telnet->buffer, telnet->buffer_pos);
  739. default:
  740. return 0;
  741. }
  742. }
  743. /* initialize a telnet state tracker */
  744. telnet_t *telnet_init(const telnet_telopt_t *telopts,
  745. telnet_event_handler_t eh, unsigned char flags, void *user_data) {
  746. /* allocate structure */
  747. struct telnet_t *telnet = (telnet_t*)calloc(1, sizeof(telnet_t));
  748. if (telnet == 0)
  749. return 0;
  750. /* initialize data */
  751. telnet->ud = user_data;
  752. telnet->telopts = telopts;
  753. telnet->eh = eh;
  754. telnet->flags = flags;
  755. return telnet;
  756. }
  757. /* free up any memory allocated by a state tracker */
  758. void telnet_free(telnet_t *telnet) {
  759. /* free sub-request buffer */
  760. if (telnet->buffer != 0) {
  761. free(telnet->buffer);
  762. telnet->buffer = 0;
  763. telnet->buffer_size = 0;
  764. telnet->buffer_pos = 0;
  765. }
  766. #if defined(HAVE_ZLIB)
  767. /* free zlib box */
  768. if (telnet->z != 0) {
  769. if (telnet->flags & TELNET_PFLAG_DEFLATE)
  770. deflateEnd(telnet->z);
  771. else
  772. inflateEnd(telnet->z);
  773. free(telnet->z);
  774. telnet->z = 0;
  775. }
  776. #endif /* defined(HAVE_ZLIB) */
  777. /* free RFC1143 queue */
  778. if (telnet->q) {
  779. free(telnet->q);
  780. telnet->q = 0;
  781. telnet->q_size = 0;
  782. }
  783. /* free the telnet structure itself */
  784. free(telnet);
  785. }
  786. /* push a byte into the telnet buffer */
  787. static telnet_error_t _buffer_byte(telnet_t *telnet,
  788. unsigned char byte) {
  789. char *new_buffer;
  790. size_t i;
  791. /* check if we're out of room */
  792. if (telnet->buffer_pos == telnet->buffer_size) {
  793. /* find the next buffer size */
  794. for (i = 0; i != _buffer_sizes_count; ++i) {
  795. if (_buffer_sizes[i] == telnet->buffer_size) {
  796. break;
  797. }
  798. }
  799. /* overflow -- can't grow any more */
  800. if (i >= _buffer_sizes_count - 1) {
  801. _error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0,
  802. "subnegotiation buffer size limit reached");
  803. return TELNET_EOVERFLOW;
  804. }
  805. /* (re)allocate buffer */
  806. new_buffer = (char *)realloc(telnet->buffer, _buffer_sizes[i + 1]);
  807. if (new_buffer == 0) {
  808. _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
  809. "realloc() failed");
  810. return TELNET_ENOMEM;
  811. }
  812. telnet->buffer = new_buffer;
  813. telnet->buffer_size = _buffer_sizes[i + 1];
  814. }
  815. /* push the byte, all set */
  816. telnet->buffer[telnet->buffer_pos++] = byte;
  817. return TELNET_EOK;
  818. }
  819. static void _process(telnet_t *telnet, const char *buffer, size_t size) {
  820. telnet_event_t ev;
  821. unsigned char byte;
  822. size_t i, start;
  823. for (i = start = 0; i != size; ++i) {
  824. byte = buffer[i];
  825. switch (telnet->state) {
  826. /* regular data */
  827. case TELNET_STATE_DATA:
  828. /* on an IAC byte, pass through all pending bytes and
  829. * switch states */
  830. if (byte == TELNET_IAC) {
  831. if (i != start) {
  832. ev.type = TELNET_EV_DATA;
  833. ev.data.buffer = buffer + start;
  834. ev.data.size = i - start;
  835. telnet->eh(telnet, &ev, telnet->ud);
  836. }
  837. telnet->state = TELNET_STATE_IAC;
  838. }
  839. break;
  840. /* IAC command */
  841. case TELNET_STATE_IAC:
  842. switch (byte) {
  843. /* subnegotiation */
  844. case TELNET_SB:
  845. telnet->state = TELNET_STATE_SB;
  846. break;
  847. /* negotiation commands */
  848. case TELNET_WILL:
  849. telnet->state = TELNET_STATE_WILL;
  850. break;
  851. case TELNET_WONT:
  852. telnet->state = TELNET_STATE_WONT;
  853. break;
  854. case TELNET_DO:
  855. telnet->state = TELNET_STATE_DO;
  856. break;
  857. case TELNET_DONT:
  858. telnet->state = TELNET_STATE_DONT;
  859. break;
  860. /* IAC escaping */
  861. case TELNET_IAC:
  862. /* event */
  863. ev.type = TELNET_EV_DATA;
  864. ev.data.buffer = (char*)&byte;
  865. ev.data.size = 1;
  866. telnet->eh(telnet, &ev, telnet->ud);
  867. /* state update */
  868. start = i + 1;
  869. telnet->state = TELNET_STATE_DATA;
  870. break;
  871. /* some other command */
  872. default:
  873. /* event */
  874. ev.type = TELNET_EV_IAC;
  875. ev.iac.cmd = byte;
  876. telnet->eh(telnet, &ev, telnet->ud);
  877. /* state update */
  878. start = i + 1;
  879. telnet->state = TELNET_STATE_DATA;
  880. }
  881. break;
  882. /* negotiation commands */
  883. case TELNET_STATE_WILL:
  884. case TELNET_STATE_WONT:
  885. case TELNET_STATE_DO:
  886. case TELNET_STATE_DONT:
  887. _negotiate(telnet, byte);
  888. start = i + 1;
  889. telnet->state = TELNET_STATE_DATA;
  890. break;
  891. /* subnegotiation -- determine subnegotiation telopt */
  892. case TELNET_STATE_SB:
  893. telnet->sb_telopt = byte;
  894. telnet->buffer_pos = 0;
  895. telnet->state = TELNET_STATE_SB_DATA;
  896. break;
  897. /* subnegotiation -- buffer bytes until end request */
  898. case TELNET_STATE_SB_DATA:
  899. /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
  900. if (byte == TELNET_IAC) {
  901. telnet->state = TELNET_STATE_SB_DATA_IAC;
  902. } else if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS && byte == TELNET_WILL) {
  903. /* In 1998 MCCP used TELOPT 85 and the protocol defined an invalid
  904. * subnegotiation sequence (IAC SB 85 WILL SE) to start compression.
  905. * Subsequently MCCP version 2 was created in 2000 using TELOPT 86
  906. * and a valid subnegotiation (IAC SB 86 IAC SE). libtelnet for now
  907. * just captures and discards MCCPv1 sequences.
  908. */
  909. start = i + 2;
  910. telnet->state = TELNET_STATE_DATA;
  911. /* buffer the byte, or bail if we can't */
  912. } else if (_buffer_byte(telnet, byte) != TELNET_EOK) {
  913. start = i + 1;
  914. telnet->state = TELNET_STATE_DATA;
  915. }
  916. break;
  917. /* IAC escaping inside a subnegotiation */
  918. case TELNET_STATE_SB_DATA_IAC:
  919. switch (byte) {
  920. /* end subnegotiation */
  921. case TELNET_SE:
  922. /* return to default state */
  923. start = i + 1;
  924. telnet->state = TELNET_STATE_DATA;
  925. /* process subnegotiation */
  926. if (_subnegotiate(telnet) != 0) {
  927. /* any remaining bytes in the buffer are compressed.
  928. * we have to re-invoke telnet_recv to get those
  929. * bytes inflated and abort trying to process the
  930. * remaining compressed bytes in the current _process
  931. * buffer argument
  932. */
  933. telnet_recv(telnet, &buffer[start], size - start);
  934. return;
  935. }
  936. break;
  937. /* escaped IAC byte */
  938. case TELNET_IAC:
  939. /* push IAC into buffer */
  940. if (_buffer_byte(telnet, TELNET_IAC) !=
  941. TELNET_EOK) {
  942. start = i + 1;
  943. telnet->state = TELNET_STATE_DATA;
  944. } else {
  945. telnet->state = TELNET_STATE_SB_DATA;
  946. }
  947. break;
  948. /* something else -- protocol error. attempt to process
  949. * content in subnegotiation buffer, then evaluate the
  950. * given command as an IAC code.
  951. */
  952. default:
  953. _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
  954. "unexpected byte after IAC inside SB: %d",
  955. byte);
  956. /* enter IAC state */
  957. start = i + 1;
  958. telnet->state = TELNET_STATE_IAC;
  959. /* process subnegotiation; see comment in
  960. * TELNET_STATE_SB_DATA_IAC about invoking telnet_recv()
  961. */
  962. if (_subnegotiate(telnet) != 0) {
  963. telnet_recv(telnet, &buffer[start], size - start);
  964. return;
  965. } else {
  966. /* recursive call to get the current input byte processed
  967. * as a regular IAC command. we could use a goto, but
  968. * that would be gross.
  969. */
  970. _process(telnet, (char *)&byte, 1);
  971. }
  972. break;
  973. }
  974. break;
  975. }
  976. }
  977. /* pass through any remaining bytes */
  978. if (telnet->state == TELNET_STATE_DATA && i != start) {
  979. ev.type = TELNET_EV_DATA;
  980. ev.data.buffer = buffer + start;
  981. ev.data.size = i - start;
  982. telnet->eh(telnet, &ev, telnet->ud);
  983. }
  984. }
  985. /* push a bytes into the state tracker */
  986. void telnet_recv(telnet_t *telnet, const char *buffer,
  987. size_t size) {
  988. #if defined(HAVE_ZLIB)
  989. /* if we have an inflate (decompression) zlib stream, use it */
  990. if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) {
  991. char inflate_buffer[1024];
  992. int rs;
  993. /* initialize zlib state */
  994. telnet->z->next_in = (unsigned char*)buffer;
  995. telnet->z->avail_in = (unsigned int)size;
  996. telnet->z->next_out = (unsigned char *)inflate_buffer;
  997. telnet->z->avail_out = sizeof(inflate_buffer);
  998. /* inflate until buffer exhausted and all output is produced */
  999. while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
  1000. /* reset output buffer */
  1001. /* decompress */
  1002. rs = inflate(telnet->z, Z_SYNC_FLUSH);
  1003. /* process the decompressed bytes on success */
  1004. if (rs == Z_OK || rs == Z_STREAM_END)
  1005. _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
  1006. telnet->z->avail_out);
  1007. else
  1008. _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
  1009. "inflate() failed: %s", zError(rs));
  1010. /* prepare output buffer for next run */
  1011. telnet->z->next_out = (unsigned char *)inflate_buffer;
  1012. telnet->z->avail_out = sizeof(inflate_buffer);
  1013. /* on error (or on end of stream) disable further inflation */
  1014. if (rs != Z_OK) {
  1015. telnet_event_t ev;
  1016. /* disable compression */
  1017. inflateEnd(telnet->z);
  1018. free(telnet->z);
  1019. telnet->z = 0;
  1020. /* send event */
  1021. ev.type = TELNET_EV_COMPRESS;
  1022. ev.compress.state = 0;
  1023. telnet->eh(telnet, &ev, telnet->ud);
  1024. break;
  1025. }
  1026. }
  1027. /* COMPRESS2 is not negotiated, just process */
  1028. } else
  1029. #endif /* defined(HAVE_ZLIB) */
  1030. _process(telnet, buffer, size);
  1031. }
  1032. /* send an iac command */
  1033. void telnet_iac(telnet_t *telnet, unsigned char cmd) {
  1034. unsigned char bytes[2];
  1035. bytes[0] = TELNET_IAC;
  1036. bytes[1] = cmd;
  1037. _sendu(telnet, bytes, 2);
  1038. }
  1039. /* send negotiation */
  1040. void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
  1041. unsigned char telopt) {
  1042. telnet_rfc1143_t q;
  1043. /* if we're in proxy mode, just send it now */
  1044. if (telnet->flags & TELNET_FLAG_PROXY) {
  1045. unsigned char bytes[3];
  1046. bytes[0] = TELNET_IAC;
  1047. bytes[1] = cmd;
  1048. bytes[2] = telopt;
  1049. _sendu(telnet, bytes, 3);
  1050. return;
  1051. }
  1052. /* get current option states */
  1053. q = _get_rfc1143(telnet, telopt);
  1054. switch (cmd) {
  1055. /* advertise willingess to support an option */
  1056. case TELNET_WILL:
  1057. switch (Q_US(q)) {
  1058. case Q_NO:
  1059. _set_rfc1143(telnet, telopt, Q_WANTYES, Q_HIM(q));
  1060. _send_negotiate(telnet, TELNET_WILL, telopt);
  1061. break;
  1062. case Q_WANTNO:
  1063. _set_rfc1143(telnet, telopt, Q_WANTNO_OP, Q_HIM(q));
  1064. break;
  1065. case Q_WANTYES_OP:
  1066. _set_rfc1143(telnet, telopt, Q_WANTYES, Q_HIM(q));
  1067. break;
  1068. }
  1069. break;
  1070. /* force turn-off of locally enabled option */
  1071. case TELNET_WONT:
  1072. switch (Q_US(q)) {
  1073. case Q_YES:
  1074. _set_rfc1143(telnet, telopt, Q_WANTNO, Q_HIM(q));
  1075. _send_negotiate(telnet, TELNET_WONT, telopt);
  1076. break;
  1077. case Q_WANTYES:
  1078. _set_rfc1143(telnet, telopt, Q_WANTYES_OP, Q_HIM(q));
  1079. break;
  1080. case Q_WANTNO_OP:
  1081. _set_rfc1143(telnet, telopt, Q_WANTNO, Q_HIM(q));
  1082. break;
  1083. }
  1084. break;
  1085. /* ask remote end to enable an option */
  1086. case TELNET_DO:
  1087. switch (Q_HIM(q)) {
  1088. case Q_NO:
  1089. _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES);
  1090. _send_negotiate(telnet, TELNET_DO, telopt);
  1091. break;
  1092. case Q_WANTNO:
  1093. _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO_OP);
  1094. break;
  1095. case Q_WANTYES_OP:
  1096. _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES);
  1097. break;
  1098. }
  1099. break;
  1100. /* demand remote end disable an option */
  1101. case TELNET_DONT:
  1102. switch (Q_HIM(q)) {
  1103. case Q_YES:
  1104. _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO);
  1105. _send_negotiate(telnet, TELNET_DONT, telopt);
  1106. break;
  1107. case Q_WANTYES:
  1108. _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTYES_OP);
  1109. break;
  1110. case Q_WANTNO_OP:
  1111. _set_rfc1143(telnet, telopt, Q_US(q), Q_WANTNO);
  1112. break;
  1113. }
  1114. break;
  1115. }
  1116. }
  1117. /* send non-command data (escapes IAC bytes) */
  1118. void telnet_send(telnet_t *telnet, const char *buffer,
  1119. size_t size) {
  1120. size_t i, l;
  1121. for (l = i = 0; i != size; ++i) {
  1122. /* dump prior portion of text, send escaped bytes */
  1123. if (buffer[i] == (char)TELNET_IAC) {
  1124. /* dump prior text if any */
  1125. if (i != l) {
  1126. _send(telnet, buffer + l, i - l);
  1127. }
  1128. l = i + 1;
  1129. /* send escape */
  1130. telnet_iac(telnet, TELNET_IAC);
  1131. }
  1132. }
  1133. /* send whatever portion of buffer is left */
  1134. if (i != l) {
  1135. _send(telnet, buffer + l, i - l);
  1136. }
  1137. }
  1138. /* send subnegotiation header */
  1139. void telnet_begin_sb(telnet_t *telnet, unsigned char telopt) {
  1140. unsigned char sb[3];
  1141. sb[0] = TELNET_IAC;
  1142. sb[1] = TELNET_SB;
  1143. sb[2] = telopt;
  1144. _sendu(telnet, sb, 3);
  1145. }
  1146. /* send complete subnegotiation */
  1147. void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
  1148. const char *buffer, size_t size) {
  1149. unsigned char bytes[5];
  1150. bytes[0] = TELNET_IAC;
  1151. bytes[1] = TELNET_SB;
  1152. bytes[2] = telopt;
  1153. bytes[3] = TELNET_IAC;
  1154. bytes[4] = TELNET_SE;
  1155. _sendu(telnet, bytes, 3);
  1156. telnet_send(telnet, buffer, size);
  1157. _sendu(telnet, bytes + 3, 2);
  1158. #if defined(HAVE_ZLIB)
  1159. /* if we're a proxy and we just sent the COMPRESS2 marker, we must
  1160. * make sure all further data is compressed if not already.
  1161. */
  1162. if (telnet->flags & TELNET_FLAG_PROXY &&
  1163. telopt == TELNET_TELOPT_COMPRESS2) {
  1164. telnet_event_t ev;
  1165. if (_init_zlib(telnet, 1, 1) != TELNET_EOK)
  1166. return;
  1167. /* notify app that compression was enabled */
  1168. ev.type = TELNET_EV_COMPRESS;
  1169. ev.compress.state = 1;
  1170. telnet->eh(telnet, &ev, telnet->ud);
  1171. }
  1172. #endif /* defined(HAVE_ZLIB) */
  1173. }
  1174. void telnet_begin_compress2(telnet_t *telnet) {
  1175. #if defined(HAVE_ZLIB)
  1176. static const unsigned char compress2[] = { TELNET_IAC, TELNET_SB,
  1177. TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE };
  1178. telnet_event_t ev;
  1179. /* attempt to create output stream first, bail if we can't */
  1180. if (_init_zlib(telnet, 1, 0) != TELNET_EOK)
  1181. return;
  1182. /* send compression marker. we send directly to the event handler
  1183. * instead of passing through _send because _send would result in
  1184. * the compress marker itself being compressed.
  1185. */
  1186. ev.type = TELNET_EV_SEND;
  1187. ev.data.buffer = (const char*)compress2;
  1188. ev.data.size = sizeof(compress2);
  1189. telnet->eh(telnet, &ev, telnet->ud);
  1190. /* notify app that compression was successfully enabled */
  1191. ev.type = TELNET_EV_COMPRESS;
  1192. ev.compress.state = 1;
  1193. telnet->eh(telnet, &ev, telnet->ud);
  1194. #endif /* defined(HAVE_ZLIB) */
  1195. }
  1196. /* send formatted data with \r and \n translation in addition to IAC IAC */
  1197. int telnet_vprintf(telnet_t *telnet, const char *fmt, va_list va) {
  1198. static const char CRLF[] = { '\r', '\n' };
  1199. static const char CRNUL[] = { '\r', '\0' };
  1200. char buffer[1024];
  1201. char *output = buffer;
  1202. int rs, i, l;
  1203. /* format */
  1204. rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
  1205. if (rs >= sizeof(buffer)) {
  1206. output = (char*)malloc(rs + 1);
  1207. if (output == 0) {
  1208. _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
  1209. "malloc() failed: %s", strerror(errno));
  1210. return -1;
  1211. }
  1212. rs = vsnprintf(output, rs + 1, fmt, va);
  1213. }
  1214. /* send */
  1215. for (l = i = 0; i != rs; ++i) {
  1216. /* special characters */
  1217. if (output[i] == (char)TELNET_IAC || output[i] == '\r' ||
  1218. output[i] == '\n') {
  1219. /* dump prior portion of text */
  1220. if (i != l)
  1221. _send(telnet, output + l, i - l);
  1222. l = i + 1;
  1223. /* IAC -> IAC IAC */
  1224. if (output[i] == (char)TELNET_IAC)
  1225. telnet_iac(telnet, TELNET_IAC);
  1226. /* automatic translation of \r -> CRNUL */
  1227. else if (output[i] == '\r')
  1228. _send(telnet, CRNUL, 2);
  1229. /* automatic translation of \n -> CRLF */
  1230. else if (output[i] == '\n')
  1231. _send(telnet, CRLF, 2);
  1232. }
  1233. }
  1234. /* send whatever portion of output is left */
  1235. if (i != l) {
  1236. _send(telnet, output + l, i - l);
  1237. }
  1238. /* free allocated memory, if any */
  1239. if (output != buffer) {
  1240. free(output);
  1241. }
  1242. return rs;
  1243. }
  1244. /* see telnet_vprintf */
  1245. int telnet_printf(telnet_t *telnet, const char *fmt, ...) {
  1246. va_list va;
  1247. int rs;
  1248. va_start(va, fmt);
  1249. rs = telnet_vprintf(telnet, fmt, va);
  1250. va_end(va);
  1251. return rs;
  1252. }
  1253. /* send formatted data through telnet_send */
  1254. int telnet_raw_vprintf(telnet_t *telnet, const char *fmt, va_list va) {
  1255. char buffer[1024];
  1256. char *output = buffer;
  1257. int rs;
  1258. /* format; allocate more space if necessary */
  1259. rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
  1260. if (rs >= sizeof(buffer)) {
  1261. output = (char*)malloc(rs + 1);
  1262. if (output == 0) {
  1263. _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
  1264. "malloc() failed: %s", strerror(errno));
  1265. return -1;
  1266. }
  1267. rs = vsnprintf(output, rs + 1, fmt, va);
  1268. }
  1269. /* send out the formatted data */
  1270. telnet_send(telnet, output, rs);
  1271. /* release allocated memory, if any */
  1272. if (output != buffer) {
  1273. free(output);
  1274. }
  1275. return rs;
  1276. }
  1277. /* see telnet_raw_vprintf */
  1278. int telnet_raw_printf(telnet_t *telnet, const char *fmt, ...) {
  1279. va_list va;
  1280. int rs;
  1281. va_start(va, fmt);
  1282. rs = telnet_raw_vprintf(telnet, fmt, va);
  1283. va_end(va);
  1284. return rs;
  1285. }
  1286. /* begin NEW-ENVIRON subnegotation */
  1287. void telnet_begin_newenviron(telnet_t *telnet, unsigned char cmd) {
  1288. telnet_begin_sb(telnet, TELNET_TELOPT_NEW_ENVIRON);
  1289. telnet_send(telnet, (const char *)&cmd, 1);
  1290. }
  1291. /* send a NEW-ENVIRON value */
  1292. void telnet_newenviron_value(telnet_t *telnet, unsigned char type,
  1293. const char *string) {
  1294. telnet_send(telnet, (const char*)&type, 1);
  1295. if (string != 0) {
  1296. telnet_send(telnet, string, strlen(string));
  1297. }
  1298. }
  1299. /* send TERMINAL-TYPE SEND command */
  1300. void telnet_ttype_send(telnet_t *telnet) {
  1301. static const unsigned char SEND[] = { TELNET_IAC, TELNET_SB,
  1302. TELNET_TELOPT_TTYPE, TELNET_TTYPE_SEND, TELNET_IAC, TELNET_SE };
  1303. _sendu(telnet, SEND, sizeof(SEND));
  1304. }
  1305. /* send TERMINAL-TYPE IS command */
  1306. void telnet_ttype_is(telnet_t *telnet, const char* ttype) {
  1307. static const unsigned char IS[] = { TELNET_IAC, TELNET_SB,
  1308. TELNET_TELOPT_TTYPE, TELNET_TTYPE_IS };
  1309. _sendu(telnet, IS, sizeof(IS));
  1310. _send(telnet, ttype, strlen(ttype));
  1311. telnet_finish_sb(telnet);
  1312. }
  1313. /* send ZMP data */
  1314. void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv) {
  1315. size_t i;
  1316. /* ZMP header */
  1317. telnet_begin_zmp(telnet, argv[0]);
  1318. /* send out each argument, including trailing NUL byte */
  1319. for (i = 1; i != argc; ++i)
  1320. telnet_zmp_arg(telnet, argv[i]);
  1321. /* ZMP footer */
  1322. telnet_finish_zmp(telnet);
  1323. }
  1324. /* send ZMP data using varargs */
  1325. void telnet_send_vzmpv(telnet_t *telnet, va_list va) {
  1326. const char* arg;
  1327. /* ZMP header */
  1328. telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
  1329. /* send out each argument, including trailing NUL byte */
  1330. while ((arg = va_arg(va, const char *)) != 0)
  1331. telnet_zmp_arg(telnet, arg);
  1332. /* ZMP footer */
  1333. telnet_finish_zmp(telnet);
  1334. }
  1335. /* see telnet_send_vzmpv */
  1336. void telnet_send_zmpv(telnet_t *telnet, ...) {
  1337. va_list va;
  1338. va_start(va, telnet);
  1339. telnet_send_vzmpv(telnet, va);
  1340. va_end(va);
  1341. }
  1342. /* begin a ZMP command */
  1343. void telnet_begin_zmp(telnet_t *telnet, const char *cmd) {
  1344. telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
  1345. telnet_zmp_arg(telnet, cmd);
  1346. }
  1347. /* send a ZMP argument */
  1348. void telnet_zmp_arg(telnet_t *telnet, const char* arg) {
  1349. telnet_send(telnet, arg, strlen(arg) + 1);
  1350. }