blob: a4eb8e9ffe091764e751981a1a425d1be14ffd6f [file] [log] [blame]
Sean Middleditch6aef0732009-03-12 23:27:35 -04001/*
Sean Middleditch55655962009-10-24 15:38:46 -07002 * libtelnet - TELNET protocol handling library
Sean Middleditchae39cee2009-03-22 22:47:30 -04003 *
Sean Middleditch9de15982009-03-14 03:35:49 -04004 * Sean Middleditch
5 * sean@sourcemud.org
6 *
Sean Middleditch6aef0732009-03-12 23:27:35 -04007 * 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
Sean Middleditch4d9444d2009-03-13 22:48:05 -040014#include <malloc.h>
Sean Middleditch9de15982009-03-14 03:35:49 -040015#include <string.h>
Sean Middleditchd922c6f2009-03-14 22:35:01 -040016#include <stdio.h>
17#include <errno.h>
18#include <string.h>
19#include <stdarg.h>
Sean Middleditch9de15982009-03-14 03:35:49 -040020
Sean Middleditchdb128162009-10-25 02:02:17 -070021#if defined(HAVE_ALLOCA)
22# include <alloca.h>
Sean Middleditch2d5c4992009-03-22 22:21:42 -040023#endif
24
Sean Middleditchdb128162009-10-25 02:02:17 -070025#if defined(HAVE_ZLIB)
26# include <zlib.h>
Sean Middleditch9de15982009-03-14 03:35:49 -040027#endif
28
Sean Middleditch29144852009-03-12 23:14:47 -040029#include "libtelnet.h"
30
Sean Middleditch9b07e922009-03-19 18:18:44 -040031/* inlinable functions */
Sean Middleditchdb128162009-10-25 02:02:17 -070032#if defined(__GNUC__) || __STDC_VERSION__ >= 199901L
Sean Middleditch53a86612009-03-22 17:43:29 -040033# define INLINE __inline__
Sean Middleditch9b07e922009-03-19 18:18:44 -040034#else
35# define INLINE
36#endif
37
Sean Middleditche97ac9e2009-10-24 15:41:05 -070038/* telnet state codes */
39enum telnet_state_t {
40 TELNET_STATE_DATA = 0,
41 TELNET_STATE_IAC,
42 TELNET_STATE_WILL,
43 TELNET_STATE_WONT,
44 TELNET_STATE_DO,
45 TELNET_STATE_DONT,
46 TELNET_STATE_SB,
47 TELNET_STATE_SB_DATA,
48 TELNET_STATE_SB_DATA_IAC
49};
50typedef enum telnet_state_t telnet_state_t;
51
52/* telnet state tracker */
Sean Middleditchd2466a02009-09-19 14:35:48 -070053struct telnet_t {
54 /* user data */
55 void *ud;
56 /* telopt support table */
57 const telnet_telopt_t *telopts;
58 /* event handler */
59 telnet_event_handler_t eh;
Sean Middleditchdb128162009-10-25 02:02:17 -070060#if defined(HAVE_ZLIB)
Sean Middleditchd2466a02009-09-19 14:35:48 -070061 /* zlib (mccp2) compression */
62 z_stream *z;
63#endif
64 /* RFC1143 option negotiation states */
65 struct telnet_rfc1143_t *q;
66 /* sub-request buffer */
67 char *buffer;
68 /* current size of the buffer */
69 size_t buffer_size;
70 /* current buffer write position (also length of buffer data) */
71 size_t buffer_pos;
72 /* current state */
73 enum telnet_state_t state;
74 /* option flags */
75 unsigned char flags;
76 /* current subnegotiation telopt */
77 unsigned char sb_telopt;
78 /* length of RFC1143 queue */
79 unsigned char q_size;
80};
81
Sean Middleditch90e79da2009-03-19 15:17:13 -040082/* RFC1143 option negotiation state */
83typedef struct telnet_rfc1143_t {
84 unsigned char telopt;
85 char us:4, him:4;
86} telnet_rfc1143_t;
87
Sean Middleditch5b5bc922009-03-15 23:02:10 -040088/* RFC1143 state names */
Sean Middleditch7491bf42009-03-20 23:52:18 -040089#define Q_NO 0
90#define Q_YES 1
91#define Q_WANTNO 2
92#define Q_WANTYES 3
93#define Q_WANTNO_OP 4
94#define Q_WANTYES_OP 5
Sean Middleditch5b5bc922009-03-15 23:02:10 -040095
Sean Middleditch4d9444d2009-03-13 22:48:05 -040096/* buffer sizes */
Sean Middleditch8c111792009-03-22 22:44:38 -040097static const size_t _buffer_sizes[] = { 0, 512, 2048, 8192, 16384, };
Sean Middleditch340a51b2009-03-19 02:08:46 -040098static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) /
Sean Middleditch812358d2009-03-15 23:24:03 -040099 sizeof(_buffer_sizes[0]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400100
Sean Middleditch34bb0992009-03-21 00:20:44 -0400101/* event dispatch helper */
102static INLINE void _event(telnet_t *telnet, telnet_event_type_t type,
Sean Middleditch97a8cb22009-03-16 16:51:41 -0400103 unsigned char command, unsigned char telopt,
Sean Middleditche5327da2009-03-21 00:54:50 -0400104 const char *buffer, size_t size, const char **argv, size_t argc) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400105 telnet_event_t ev;
Sean Middleditche5327da2009-03-21 00:54:50 -0400106 ev.argv = argv;
107 ev.argc = argc;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400108 ev.buffer = buffer;
109 ev.size = size;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400110 ev.type = type;
111 ev.command = command;
112 ev.telopt = telopt;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400113
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400114 telnet->eh(telnet, &ev, telnet->ud);
Sean Middleditch637df7f2009-03-15 12:57:32 -0400115}
116
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400117/* error generation function */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400118static telnet_error_t _error(telnet_t *telnet, unsigned line,
119 const char* func, telnet_error_t err, int fatal, const char *fmt,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400120 ...) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400121 char buffer[512];
122 va_list va;
123
124 /* format error intro */
Sean Middleditch812358d2009-03-15 23:24:03 -0400125 snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func);
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400126
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400127 /* format informational text */
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400128 va_start(va, fmt);
129 vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
130 fmt, va);
131 va_end(va);
132
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400133 /* send error event to the user */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400134 _event(telnet, fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING, err,
Sean Middleditche5327da2009-03-21 00:54:50 -0400135 0, buffer, strlen(buffer), 0, 0);
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400136
137 return err;
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400138}
139
Sean Middleditchdb128162009-10-25 02:02:17 -0700140#if defined(HAVE_ZLIB)
Sean Middleditch72cc9642009-03-15 11:50:36 -0400141/* initialize the zlib box for a telnet box; if deflate is non-zero, it
142 * initializes zlib for delating (compression), otherwise for inflating
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400143 * (decompression). returns TELNET_EOK on success, something else on
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400144 * failure.
Sean Middleditch72cc9642009-03-15 11:50:36 -0400145 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400146telnet_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400147 z_stream *z;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400148 int rs;
149
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400150 /* if compression is already enabled, fail loudly */
151 if (telnet->z != 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400152 return _error(telnet, __LINE__, __func__, TELNET_EBADVAL,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400153 err_fatal, "cannot initialize compression twice");
154
Sean Middleditch72cc9642009-03-15 11:50:36 -0400155 /* allocate zstream box */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400156 if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400157 return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal,
Sean Middleditch16992272009-03-15 19:42:03 -0400158 "malloc() failed: %s", strerror(errno));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400159
160 /* initialize */
161 if (deflate) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400162 if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) {
163 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400164 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400165 err_fatal, "deflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400166 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400167 telnet->flags |= TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400168 } else {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400169 if ((rs = inflateInit(z)) != Z_OK) {
170 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400171 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400172 err_fatal, "inflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400173 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400174 telnet->flags &= ~TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400175 }
176
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400177 telnet->z = z;
178
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400179 return TELNET_EOK;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400180}
Sean Middleditchdb128162009-10-25 02:02:17 -0700181#endif /* defined(HAVE_ZLIB) */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400182
Sean Middleditch8b788962009-03-16 01:06:27 -0400183/* push bytes out, compressing them first if need be */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400184static void _send(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400185 size_t size) {
Sean Middleditchdb128162009-10-25 02:02:17 -0700186#if defined(HAVE_ZLIB)
Sean Middleditch8b788962009-03-16 01:06:27 -0400187 /* if we have a deflate (compression) zlib box, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400188 if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400189 char deflate_buffer[1024];
Sean Middleditch8b788962009-03-16 01:06:27 -0400190 int rs;
191
192 /* initialize z state */
Sean Middleditch97a8cb22009-03-16 16:51:41 -0400193 telnet->z->next_in = (unsigned char *)buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400194 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400195 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400196 telnet->z->avail_out = sizeof(deflate_buffer);
197
198 /* deflate until buffer exhausted and all output is produced */
199 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
200 /* compress */
201 if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400202 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch8b788962009-03-16 01:06:27 -0400203 "deflate() failed: %s", zError(rs));
204 deflateEnd(telnet->z);
205 free(telnet->z);
206 telnet->z = 0;
207 break;
208 }
209
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400210 _event(telnet, TELNET_EV_SEND, 0, 0, deflate_buffer,
Sean Middleditche5327da2009-03-21 00:54:50 -0400211 sizeof(deflate_buffer) - telnet->z->avail_out, 0, 0);
Sean Middleditch8b788962009-03-16 01:06:27 -0400212
213 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400214 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400215 telnet->z->avail_out = sizeof(deflate_buffer);
216 }
217
218 /* COMPRESS2 is not negotiated, just send */
219 } else
Sean Middleditchdb128162009-10-25 02:02:17 -0700220#endif /* defined(HAVE_ZLIB) */
Sean Middleditche5327da2009-03-21 00:54:50 -0400221 _event(telnet, TELNET_EV_SEND, 0, 0, buffer, size, 0, 0);
Sean Middleditch8b788962009-03-16 01:06:27 -0400222}
223
Sean Middleditch34bb0992009-03-21 00:20:44 -0400224/* check if we support a particular telopt; if us is non-zero, we
225 * check if we (local) supports it, otherwise we check if he (remote)
226 * supports it. return non-zero if supported, zero if not supported.
227 */
228static INLINE int _check_telopt(telnet_t *telnet, unsigned char telopt,
229 int us) {
230 int i;
231
232 /* if we have no telopts table, we obviously don't support it */
233 if (telnet->telopts == 0)
234 return 0;
235
236 /* loop unti found or end marker (us and him both 0) */
Sean Middleditchbfc641e2009-03-22 16:26:06 -0400237 for (i = 0; telnet->telopts[i].telopt != -1; ++i) {
238 if (telnet->telopts[i].telopt == telopt) {
239 if (us && telnet->telopts[i].us == TELNET_WILL)
240 return 1;
241 else if (!us && telnet->telopts[i].him == TELNET_DO)
242 return 1;
243 else
244 return 0;
245 }
246 }
Sean Middleditch34bb0992009-03-21 00:20:44 -0400247
248 /* not found, so not supported */
249 return 0;
250}
251
Sean Middleditch8b788962009-03-16 01:06:27 -0400252/* retrieve RFC1143 option state */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400253static INLINE telnet_rfc1143_t _get_rfc1143(telnet_t *telnet,
254 unsigned char telopt) {
Sean Middleditch4987f9f2009-03-19 12:51:40 -0400255 const telnet_rfc1143_t empty = { telopt, 0, 0};
Sean Middleditch8b788962009-03-16 01:06:27 -0400256 int i;
257
258 /* search for entry */
259 for (i = 0; i != telnet->q_size; ++i)
260 if (telnet->q[i].telopt == telopt)
261 return telnet->q[i];
262
263 /* not found, return empty value */
264 return empty;
265}
266
267/* save RFC1143 option state */
Sean Middleditch7491bf42009-03-20 23:52:18 -0400268static INLINE void _set_rfc1143(telnet_t *telnet, unsigned char telopt,
269 char us, char him) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400270 telnet_rfc1143_t *qtmp;
Sean Middleditch8b788962009-03-16 01:06:27 -0400271 int i;
272
273 /* search for entry */
274 for (i = 0; i != telnet->q_size; ++i) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400275 if (telnet->q[i].telopt == telopt) {
276 telnet->q[i].us = us;
277 telnet->q[i].him = him;
Sean Middleditch8b788962009-03-16 01:06:27 -0400278 return;
279 }
280 }
281
282 /* we're going to need to track state for it, so grow the queue
Sean Middleditch1e80f522009-03-22 18:24:18 -0400283 * by 4 (four) elements and put the telopt into it; bail on allocation
284 * error. we go by four because it seems like a reasonable guess as
285 * to the number of enabled options for most simple code, and it
286 * allows for an acceptable number of reallocations for complex code.
Sean Middleditch8b788962009-03-16 01:06:27 -0400287 */
Sean Middleditcha63ff7f2009-04-03 01:27:06 -0400288 if ((qtmp = (telnet_rfc1143_t *)realloc(telnet->q,
289 sizeof(telnet_rfc1143_t) * (telnet->q_size + 4))) == 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400290 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch8b788962009-03-16 01:06:27 -0400291 "malloc() failed: %s", strerror(errno));
292 return;
293 }
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400294 memset(&qtmp[telnet->q_size], 0, sizeof(telnet_rfc1143_t) * 4);
Sean Middleditch8b788962009-03-16 01:06:27 -0400295 telnet->q = qtmp;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400296 telnet->q[telnet->q_size].telopt = telopt;
297 telnet->q[telnet->q_size].us = us;
298 telnet->q[telnet->q_size].him = him;
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400299 telnet->q_size += 4;
Sean Middleditch8b788962009-03-16 01:06:27 -0400300}
301
Sean Middleditch90e79da2009-03-19 15:17:13 -0400302/* send negotiation bytes */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400303static INLINE void _send_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400304 unsigned char telopt) {
305 char bytes[3] = { TELNET_IAC, cmd, telopt };
306 _send(telnet, bytes, 3);
307}
308
Sean Middleditch8b788962009-03-16 01:06:27 -0400309/* negotiation handling magic for RFC1143 */
Sean Middleditch330c2612009-03-20 23:29:17 -0400310static void _negotiate(telnet_t *telnet, unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400311 telnet_rfc1143_t q;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400312
313 /* in PROXY mode, just pass it thru and do nothing */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400314 if (telnet->flags & TELNET_FLAG_PROXY) {
Sean Middleditch330c2612009-03-20 23:29:17 -0400315 switch ((int)telnet->state) {
316 case TELNET_STATE_WILL:
Sean Middleditche5327da2009-03-21 00:54:50 -0400317 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400318 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400319 case TELNET_STATE_WONT:
Sean Middleditche5327da2009-03-21 00:54:50 -0400320 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400321 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400322 case TELNET_STATE_DO:
Sean Middleditche5327da2009-03-21 00:54:50 -0400323 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400324 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400325 case TELNET_STATE_DONT:
Sean Middleditche5327da2009-03-21 00:54:50 -0400326 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400327 break;
328 }
329 return;
330 }
331
332 /* lookup the current state of the option */
Sean Middleditch8b788962009-03-16 01:06:27 -0400333 q = _get_rfc1143(telnet, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400334
335 /* start processing... */
Sean Middleditch330c2612009-03-20 23:29:17 -0400336 switch ((int)telnet->state) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400337 /* request to enable option on remote end or confirm DO */
Sean Middleditch330c2612009-03-20 23:29:17 -0400338 case TELNET_STATE_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400339 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400340 case Q_NO:
Sean Middleditch34bb0992009-03-21 00:20:44 -0400341 if (_check_telopt(telnet, telopt, 0)) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400342 _set_rfc1143(telnet, telopt, q.us, Q_YES);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400343 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch94738a42009-03-22 16:33:22 -0400344 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400345 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400346 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400347 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400348 case Q_WANTNO:
349 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditche5327da2009-03-21 00:54:50 -0400350 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400351 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400352 "DONT answered by WILL");
353 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400354 case Q_WANTNO_OP:
355 _set_rfc1143(telnet, telopt, q.us, Q_YES);
Sean Middleditche5327da2009-03-21 00:54:50 -0400356 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400357 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400358 "DONT answered by WILL");
359 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400360 case Q_WANTYES:
361 _set_rfc1143(telnet, telopt, q.us, Q_YES);
Sean Middleditche5327da2009-03-21 00:54:50 -0400362 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400363 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400364 case Q_WANTYES_OP:
365 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400366 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400367 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400368 break;
369 }
370 break;
371
372 /* request to disable option on remote end, confirm DONT, reject DO */
Sean Middleditch330c2612009-03-20 23:29:17 -0400373 case TELNET_STATE_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400374 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400375 case Q_YES:
376 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400377 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400378 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400379 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400380 case Q_WANTNO:
381 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditche5327da2009-03-21 00:54:50 -0400382 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400383 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400384 case Q_WANTNO_OP:
385 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES);
Sean Middleditche5327da2009-03-21 00:54:50 -0400386 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400387 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400388 case Q_WANTYES:
389 case Q_WANTYES_OP:
390 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400391 break;
392 }
393 break;
394
395 /* request to enable option on local end or confirm WILL */
Sean Middleditch330c2612009-03-20 23:29:17 -0400396 case TELNET_STATE_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400397 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400398 case Q_NO:
Sean Middleditch34bb0992009-03-21 00:20:44 -0400399 if (_check_telopt(telnet, telopt, 1)) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400400 _set_rfc1143(telnet, telopt, Q_YES, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400401 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch94738a42009-03-22 16:33:22 -0400402 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400403 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400404 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400405 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400406 case Q_WANTNO:
407 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400408 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400409 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400410 "WONT answered by DO");
411 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400412 case Q_WANTNO_OP:
413 _set_rfc1143(telnet, telopt, Q_YES, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400414 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400415 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400416 "WONT answered by DO");
417 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400418 case Q_WANTYES:
419 _set_rfc1143(telnet, telopt, Q_YES, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400420 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400421 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400422 case Q_WANTYES_OP:
423 _set_rfc1143(telnet, telopt, Q_WANTNO, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400424 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400425 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400426 break;
427 }
428 break;
429
430 /* request to disable option on local end, confirm WONT, reject WILL */
Sean Middleditch330c2612009-03-20 23:29:17 -0400431 case TELNET_STATE_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400432 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400433 case Q_YES:
434 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400435 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400436 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400437 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400438 case Q_WANTNO:
439 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400440 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400441 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400442 case Q_WANTNO_OP:
443 _set_rfc1143(telnet, telopt, Q_WANTYES, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400444 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400445 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400446 case Q_WANTYES:
447 case Q_WANTYES_OP:
448 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400449 break;
450 }
451 break;
452 }
453}
454
Sean Middleditche5327da2009-03-21 00:54:50 -0400455/* process a subnegotiation buffer; return non-zero if the current buffer
456 * must be aborted and reprocessed due to COMPRESS2 being activated
457 */
458static int _subnegotiate(telnet_t *telnet) {
Sean Middleditchb10946c2009-03-22 18:21:14 -0400459 switch (telnet->sb_telopt) {
Sean Middleditchdb128162009-10-25 02:02:17 -0700460#if defined(HAVE_ZLIB)
Sean Middleditche5327da2009-03-21 00:54:50 -0400461 /* received COMPRESS2 begin marker, setup our zlib box and
462 * start handling the compressed stream if it's not already.
463 */
Sean Middleditchb10946c2009-03-22 18:21:14 -0400464 case TELNET_TELOPT_COMPRESS2:
465 if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) {
466 if (_init_zlib(telnet, 0, 1) != TELNET_EOK)
Sean Middleditch8c111792009-03-22 22:44:38 -0400467 return 0;
Sean Middleditche5327da2009-03-21 00:54:50 -0400468
Sean Middleditchb10946c2009-03-22 18:21:14 -0400469 /* standard SB notification */
470 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
471 telnet->buffer, telnet->buffer_pos, 0, 0);
472
473 /* notify app that compression was enabled */
474 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0);
475 return 1;
476 }
Sean Middleditch8c111792009-03-22 22:44:38 -0400477 return 0;
Sean Middleditchdb128162009-10-25 02:02:17 -0700478#endif /* defined(HAVE_ZLIB) */
479#if defined(HAVE_ALLOCA)
Sean Middleditch8c111792009-03-22 22:44:38 -0400480
Sean Middleditchb10946c2009-03-22 18:21:14 -0400481 /* ZMP command */
Sean Middleditch8c111792009-03-22 22:44:38 -0400482 case TELNET_TELOPT_ZMP: {
483 const char **argv, *c;
484 size_t i, argc;
Sean Middleditche5327da2009-03-21 00:54:50 -0400485 /* make sure this is a valid ZMP buffer */
Sean Middleditchb10946c2009-03-22 18:21:14 -0400486 if (telnet->buffer_pos == 0 ||
487 telnet->buffer[telnet->buffer_pos - 1] != 0) {
488 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
489 "incomplete ZMP frame");
490 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
491 telnet->buffer, telnet->buffer_pos, 0, 0);
Sean Middleditch8c111792009-03-22 22:44:38 -0400492 return 0;
Sean Middleditchb10946c2009-03-22 18:21:14 -0400493 }
Sean Middleditche5327da2009-03-21 00:54:50 -0400494
495 /* count arguments */
Sean Middleditchb10946c2009-03-22 18:21:14 -0400496 for (argc = 0, c = telnet->buffer; c != telnet->buffer +
497 telnet->buffer_pos; ++argc)
Sean Middleditche5327da2009-03-21 00:54:50 -0400498 c += strlen(c) + 1;
Sean Middleditche5327da2009-03-21 00:54:50 -0400499
500 /* allocate argument array, bail on error */
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400501 if ((argv = (const char **)alloca(sizeof(char *) * argc)) == 0) {
Sean Middleditche5327da2009-03-21 00:54:50 -0400502 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400503 "alloca() failed: %s", strerror(errno));
Sean Middleditchb10946c2009-03-22 18:21:14 -0400504 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
505 telnet->buffer, telnet->buffer_pos, 0, 0);
Sean Middleditch8c111792009-03-22 22:44:38 -0400506 return 0;
Sean Middleditche5327da2009-03-21 00:54:50 -0400507 }
508
509 /* populate argument array */
510 for (i = 0, c = telnet->buffer; i != argc; ++i) {
511 argv[i] = c;
512 c += strlen(c) + 1;
513 }
514
Sean Middleditchb10946c2009-03-22 18:21:14 -0400515 /* invoke event with our arguments */
516 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
517 telnet->buffer, telnet->buffer_pos, argv, argc);
Sean Middleditch8c111792009-03-22 22:44:38 -0400518 return 0;
519 }
520
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400521 /* any of a number of commands that use the form <BYTE>data<BYTE>data,
522 * including TTYPE, ENVIRON, NEW-ENVIRON, and MSSP
523 */
524 case TELNET_TELOPT_TTYPE:
525 case TELNET_TELOPT_ENVIRON:
526 case TELNET_TELOPT_NEW_ENVIRON:
Sean Middleditch8c111792009-03-22 22:44:38 -0400527 case TELNET_TELOPT_MSSP: {
528 char **argv, *c, *l;
529 size_t i, argc;
530
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400531 /* if we have no data, just pass it through */
532 if (telnet->buffer_pos == 0) {
533 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
534 telnet->buffer, telnet->buffer_pos, 0, 0);
Sean Middleditch8c111792009-03-22 22:44:38 -0400535 return 0;
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400536 }
537
538 /* very first byte must be in range 0-3 */
539 if ((unsigned)telnet->buffer[0] > 3) {
540 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
541 "telopt %d subneg has invalid data", telnet->sb_telopt);
542 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
543 telnet->buffer, telnet->buffer_pos, 0, 0);
Sean Middleditch8c111792009-03-22 22:44:38 -0400544 return 0;
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400545 }
546
547 /* count arguments; each argument is preceded by a byte in the
548 * range 0-3, so just count those.
549 * NOTE: we don't support the ENVIRON/NEW-ENVIRON ESC handling
550 * properly at all. guess that's a FIXME.
551 */
552 for (argc = 0, i = 0; i != telnet->buffer_pos; ++i)
553 if ((unsigned)telnet->buffer[i] <= 3)
554 ++argc;
555
556 /* allocate argument array, bail on error */
Sean Middleditch8c111792009-03-22 22:44:38 -0400557 if ((argv = (char **)alloca(sizeof(char *) * argc)) == 0) {
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400558 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
559 "alloca() failed: %s", strerror(errno));
560 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
561 telnet->buffer, telnet->buffer_pos, 0, 0);
Sean Middleditch8c111792009-03-22 22:44:38 -0400562 return 0;
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400563 }
564
565 /* allocate strings in argument array */
566 for (i = 0, l = telnet->buffer; i != argc; ++i) {
Sean Middleditch8c111792009-03-22 22:44:38 -0400567 /* search for end marker */
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400568 c = l + 1;
569 while (c != telnet->buffer + telnet->buffer_pos &&
570 (unsigned)*c > 3)
571 ++c;
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400572
Sean Middleditch8c111792009-03-22 22:44:38 -0400573 /* allocate space; bail on error */
574 if ((argv[i] = (char *)alloca(c - l + 1)) == 0) {
575 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
576 "alloca() failed: %s", strerror(errno));
577 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
578 telnet->buffer, telnet->buffer_pos, 0, 0);
579 return 0;
580 }
581
582 /* copy data */
583 memcpy(argv[i], l, c - l);
584 argv[i][c - l] = 0;
585
586 /* prepare for next loop */
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400587 l = c;
588 }
589
590 /* invoke event with our arguments */
591 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
Sean Middleditch8c111792009-03-22 22:44:38 -0400592 telnet->buffer, telnet->buffer_pos, (const char **)argv, argc);
593 return 0;
594 }
Sean Middleditchdb128162009-10-25 02:02:17 -0700595#endif /* defined(HAVE_ALLOCA) */
Sean Middleditch8c111792009-03-22 22:44:38 -0400596
Sean Middleditchb10946c2009-03-22 18:21:14 -0400597 /* other generic subnegotiation */
598 default:
599 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
600 telnet->buffer, telnet->buffer_pos, 0, 0);
Sean Middleditch8c111792009-03-22 22:44:38 -0400601 return 0;
Sean Middleditche5327da2009-03-21 00:54:50 -0400602 }
Sean Middleditche5327da2009-03-21 00:54:50 -0400603}
604
Sean Middleditch29144852009-03-12 23:14:47 -0400605/* initialize a telnet state tracker */
Sean Middleditchd2466a02009-09-19 14:35:48 -0700606telnet_t *telnet_init(const telnet_telopt_t *telopts,
Sean Middleditch34bb0992009-03-21 00:20:44 -0400607 telnet_event_handler_t eh, unsigned char flags, void *user_data) {
Sean Middleditchd2466a02009-09-19 14:35:48 -0700608 /* allocate structure */
609 struct telnet_t *telnet = (telnet_t*)calloc(1, sizeof(telnet_t));
610 if (telnet == 0)
611 return 0;
612
613 /* initialize data */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400614 telnet->ud = user_data;
Sean Middleditch34bb0992009-03-21 00:20:44 -0400615 telnet->telopts = telopts;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400616 telnet->eh = eh;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400617 telnet->flags = flags;
Sean Middleditchd2466a02009-09-19 14:35:48 -0700618
619 return telnet;
Sean Middleditch29144852009-03-12 23:14:47 -0400620}
621
622/* free up any memory allocated by a state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400623void telnet_free(telnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400624 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400625 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400626 free(telnet->buffer);
627 telnet->buffer = 0;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400628 telnet->buffer_size = 0;
629 telnet->buffer_pos = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400630 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400631
Sean Middleditchdb128162009-10-25 02:02:17 -0700632#if defined(HAVE_ZLIB)
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400633 /* free zlib box */
634 if (telnet->z != 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400635 if (telnet->flags & TELNET_PFLAG_DEFLATE)
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400636 deflateEnd(telnet->z);
637 else
638 inflateEnd(telnet->z);
639 free(telnet->z);
640 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400641 }
Sean Middleditchdb128162009-10-25 02:02:17 -0700642#endif /* defined(HAVE_ZLIB) */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400643
644 /* free RFC1143 queue */
645 if (telnet->q) {
646 free(telnet->q);
647 telnet->q = 0;
648 telnet->q_size = 0;
649 }
Sean Middleditchd2466a02009-09-19 14:35:48 -0700650
651 /* free the telnet structure itself */
652 free(telnet);
Sean Middleditch29144852009-03-12 23:14:47 -0400653}
654
Sean Middleditch51ad6792009-03-13 20:15:59 -0400655/* push a byte into the telnet buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400656static telnet_error_t _buffer_byte(telnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400657 unsigned char byte) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400658 char *new_buffer;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400659 size_t i;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400660
Sean Middleditch51ad6792009-03-13 20:15:59 -0400661 /* check if we're out of room */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400662 if (telnet->buffer_pos == telnet->buffer_size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400663 /* find the next buffer size */
664 for (i = 0; i != _buffer_sizes_count; ++i) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400665 if (_buffer_sizes[i] == telnet->buffer_size)
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400666 break;
667 }
668
669 /* overflow -- can't grow any more */
670 if (i >= _buffer_sizes_count - 1) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400671 _error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400672 "subnegotiation buffer size limit reached");
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400673 return TELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400674 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400675
676 /* (re)allocate buffer */
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400677 new_buffer = (char *)realloc(telnet->buffer, _buffer_sizes[i + 1]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400678 if (new_buffer == 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400679 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch16992272009-03-15 19:42:03 -0400680 "realloc() failed");
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400681 return TELNET_ENOMEM;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400682 }
683
684 telnet->buffer = new_buffer;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400685 telnet->buffer_size = _buffer_sizes[i + 1];
Sean Middleditch51ad6792009-03-13 20:15:59 -0400686 }
687
688 /* push the byte, all set */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400689 telnet->buffer[telnet->buffer_pos++] = byte;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400690 return TELNET_EOK;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400691}
692
Sean Middleditch2d5f4bf2009-03-20 23:32:47 -0400693static void _process(telnet_t *telnet, const char *buffer, size_t size) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400694 unsigned char byte;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400695 size_t i, start;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400696 for (i = start = 0; i != size; ++i) {
697 byte = buffer[i];
698 switch (telnet->state) {
699 /* regular data */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400700 case TELNET_STATE_DATA:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400701 /* on an IAC byte, pass through all pending bytes and
702 * switch states */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400703 if (byte == TELNET_IAC) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400704 if (i != start)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400705 _event(telnet, TELNET_EV_DATA, 0, 0, &buffer[start],
Sean Middleditche5327da2009-03-21 00:54:50 -0400706 i - start, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400707 telnet->state = TELNET_STATE_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400708 }
709 break;
710
711 /* IAC command */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400712 case TELNET_STATE_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400713 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400714 /* subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400715 case TELNET_SB:
716 telnet->state = TELNET_STATE_SB;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400717 break;
718 /* negotiation commands */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400719 case TELNET_WILL:
720 telnet->state = TELNET_STATE_WILL;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400721 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400722 case TELNET_WONT:
723 telnet->state = TELNET_STATE_WONT;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400724 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400725 case TELNET_DO:
726 telnet->state = TELNET_STATE_DO;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400727 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400728 case TELNET_DONT:
729 telnet->state = TELNET_STATE_DONT;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400730 break;
731 /* IAC escaping */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400732 case TELNET_IAC:
Sean Middleditche5327da2009-03-21 00:54:50 -0400733 _event(telnet, TELNET_EV_DATA, 0, 0, (char*)&byte, 1, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400734 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400735 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400736 break;
737 /* some other command */
738 default:
Sean Middleditche5327da2009-03-21 00:54:50 -0400739 _event(telnet, TELNET_EV_IAC, byte, 0, 0, 0, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400740 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400741 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400742 }
743 break;
744
745 /* negotiation commands */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400746 case TELNET_STATE_WILL:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400747 case TELNET_STATE_WONT:
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400748 case TELNET_STATE_DO:
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400749 case TELNET_STATE_DONT:
Sean Middleditch330c2612009-03-20 23:29:17 -0400750 _negotiate(telnet, byte);
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400751 start = i + 1;
752 telnet->state = TELNET_STATE_DATA;
753 break;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400754
Sean Middleditchda0e6952009-03-15 13:28:09 -0400755 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400756 case TELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400757 telnet->sb_telopt = byte;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400758 telnet->buffer_pos = 0;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400759 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditchda0e6952009-03-15 13:28:09 -0400760 break;
761
762 /* subnegotiation -- buffer bytes until end request */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400763 case TELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400764 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400765 if (byte == TELNET_IAC) {
766 telnet->state = TELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400767 /* buffer the byte, or bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400768 } else if (_buffer_byte(telnet, byte) != TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400769 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400770 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400771 }
772 break;
773
Sean Middleditch6b372882009-03-14 13:06:47 -0400774 /* IAC escaping inside a subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400775 case TELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400776 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400777 /* end subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400778 case TELNET_SE:
Sean Middleditch8c567352009-03-21 01:07:18 -0400779 /* return to default state */
780 start = i + 1;
781 telnet->state = TELNET_STATE_DATA;
782
Sean Middleditche5327da2009-03-21 00:54:50 -0400783 /* process subnegotiation */
784 if (_subnegotiate(telnet) != 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400785 /* any remaining bytes in the buffer are compressed.
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400786 * we have to re-invoke telnet_recv to get those
Sean Middleditch9de15982009-03-14 03:35:49 -0400787 * bytes inflated and abort trying to process the
788 * remaining compressed bytes in the current _process
789 * buffer argument
790 */
Sean Middleditch8c567352009-03-21 01:07:18 -0400791 telnet_recv(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400792 return;
793 }
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400794 break;
795 /* escaped IAC byte */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400796 case TELNET_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400797 /* push IAC into buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400798 if (_buffer_byte(telnet, TELNET_IAC) !=
799 TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400800 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400801 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400802 } else {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400803 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400804 }
805 break;
Sean Middleditch2d5f4bf2009-03-20 23:32:47 -0400806 /* something else -- protocol error. attempt to process
807 * content in subnegotiation buffer, then evaluate the
808 * given command as an IAC code.
809 */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400810 default:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400811 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400812 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400813 byte);
Sean Middleditch2d5f4bf2009-03-20 23:32:47 -0400814
Sean Middleditchf6daa852009-03-21 01:16:20 -0400815 /* enter IAC state */
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400816 start = i + 1;
Sean Middleditchf6daa852009-03-21 01:16:20 -0400817 telnet->state = TELNET_STATE_IAC;
Sean Middleditche5327da2009-03-21 00:54:50 -0400818
819 /* process subnegotiation; see comment in
820 * TELNET_STATE_SB_DATA_IAC about invoking telnet_recv()
821 */
822 if (_subnegotiate(telnet) != 0) {
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400823 telnet_recv(telnet, &buffer[start], size - start);
Sean Middleditche5327da2009-03-21 00:54:50 -0400824 return;
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400825 } else {
826 /* recursive call to get the current input byte processed
827 * as a regular IAC command. we could use a goto, but
828 * that would be gross.
829 */
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400830 _process(telnet, (char *)&byte, 1);
Sean Middleditche5327da2009-03-21 00:54:50 -0400831 }
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400832 break;
833 }
834 break;
835 }
836 }
837
838 /* pass through any remaining bytes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400839 if (telnet->state == TELNET_STATE_DATA && i != start)
Sean Middleditche5327da2009-03-21 00:54:50 -0400840 _event(telnet, TELNET_EV_DATA, 0, 0, buffer + start, i - start, 0, 0);
Sean Middleditch29144852009-03-12 23:14:47 -0400841}
842
Sean Middleditch9de15982009-03-14 03:35:49 -0400843/* push a bytes into the state tracker */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400844void telnet_recv(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400845 size_t size) {
Sean Middleditchdb128162009-10-25 02:02:17 -0700846#if defined(HAVE_ZLIB)
Sean Middleditch72cc9642009-03-15 11:50:36 -0400847 /* if we have an inflate (decompression) zlib stream, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400848 if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400849 char inflate_buffer[4096];
Sean Middleditch9de15982009-03-14 03:35:49 -0400850 int rs;
851
852 /* initialize zlib state */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400853 telnet->z->next_in = (unsigned char*)buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400854 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400855 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400856 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400857
858 /* inflate until buffer exhausted and all output is produced */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400859 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400860 /* reset output buffer */
861
862 /* decompress */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400863 rs = inflate(telnet->z, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400864
865 /* process the decompressed bytes on success */
866 if (rs == Z_OK || rs == Z_STREAM_END)
867 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400868 telnet->z->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400869 else
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400870 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch16992272009-03-15 19:42:03 -0400871 "inflate() failed: %s", zError(rs));
Sean Middleditch9de15982009-03-14 03:35:49 -0400872
873 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400874 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400875 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400876
877 /* on error (or on end of stream) disable further inflation */
878 if (rs != Z_OK) {
Sean Middleditche5327da2009-03-21 00:54:50 -0400879 _event(telnet, TELNET_EV_COMPRESS, 0, 0, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400880
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400881 inflateEnd(telnet->z);
882 free(telnet->z);
883 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400884 break;
885 }
886 }
887
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400888 /* COMPRESS2 is not negotiated, just process */
889 } else
Sean Middleditchdb128162009-10-25 02:02:17 -0700890#endif /* defined(HAVE_ZLIB) */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400891 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400892}
893
Sean Middleditch29144852009-03-12 23:14:47 -0400894/* send an iac command */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400895void telnet_iac(telnet_t *telnet, unsigned char cmd) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400896 char bytes[2] = { TELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400897 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400898}
899
900/* send negotiation */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400901void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch8b788962009-03-16 01:06:27 -0400902 unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400903 telnet_rfc1143_t q;
Sean Middleditch8b788962009-03-16 01:06:27 -0400904
905 /* if we're in proxy mode, just send it now */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400906 if (telnet->flags & TELNET_FLAG_PROXY) {
907 char bytes[3] = { TELNET_IAC, cmd, telopt };
Sean Middleditch8b788962009-03-16 01:06:27 -0400908 _send(telnet, bytes, 3);
909 return;
910 }
911
912 /* get current option states */
913 q = _get_rfc1143(telnet, telopt);
914
915 switch (cmd) {
916 /* advertise willingess to support an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400917 case TELNET_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400918 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400919 case Q_NO:
920 _set_rfc1143(telnet, telopt, Q_WANTYES, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400921 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400922 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400923 case Q_WANTNO:
924 _set_rfc1143(telnet, telopt, Q_WANTNO_OP, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400925 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400926 case Q_WANTYES_OP:
927 _set_rfc1143(telnet, telopt, Q_WANTYES, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400928 break;
929 }
930 break;
931
932 /* force turn-off of locally enabled option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400933 case TELNET_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400934 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400935 case Q_YES:
936 _set_rfc1143(telnet, telopt, Q_WANTNO, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400937 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400938 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400939 case Q_WANTYES:
940 _set_rfc1143(telnet, telopt, Q_WANTYES_OP, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400941 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400942 case Q_WANTNO_OP:
943 _set_rfc1143(telnet, telopt, Q_WANTNO, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400944 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400945 }
946 break;
947
948 /* ask remote end to enable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400949 case TELNET_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400950 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400951 case Q_NO:
952 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400953 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400954 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400955 case Q_WANTNO:
956 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO_OP);
Sean Middleditch8b788962009-03-16 01:06:27 -0400957 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400958 case Q_WANTYES_OP:
959 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES);
Sean Middleditch8b788962009-03-16 01:06:27 -0400960 break;
961 }
962 break;
963
964 /* demand remote end disable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400965 case TELNET_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400966 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400967 case Q_YES:
968 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400969 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400970 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400971 case Q_WANTYES:
972 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES_OP);
Sean Middleditch8b788962009-03-16 01:06:27 -0400973 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400974 case Q_WANTNO_OP:
975 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO);
Sean Middleditch8b788962009-03-16 01:06:27 -0400976 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400977 }
978 break;
979 }
Sean Middleditch29144852009-03-12 23:14:47 -0400980}
981
982/* send non-command data (escapes IAC bytes) */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400983void telnet_send(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400984 size_t size) {
985 size_t i, l;
Sean Middleditchc337ba62009-03-16 16:47:27 -0400986
Sean Middleditch29144852009-03-12 23:14:47 -0400987 for (l = i = 0; i != size; ++i) {
988 /* dump prior portion of text, send escaped bytes */
Sean Middleditch0003f052009-04-27 19:26:05 -0400989 if (buffer[i] == (char)TELNET_IAC) {
Sean Middleditch29144852009-03-12 23:14:47 -0400990 /* dump prior text if any */
991 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400992 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400993 l = i + 1;
994
995 /* send escape */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400996 telnet_iac(telnet, TELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400997 }
998 }
999
1000 /* send whatever portion of buffer is left */
1001 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -04001002 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -04001003}
1004
Sean Middleditch78943842009-03-19 15:22:06 -04001005/* send subnegotiation header */
Sean Middleditch1443ca42009-03-21 00:21:04 -04001006void telnet_begin_sb(telnet_t *telnet, unsigned char telopt) {
Sean Middleditch78943842009-03-19 15:22:06 -04001007 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
1008 _send(telnet, sb, 3);
1009}
1010
1011
1012/* send complete subnegotiation */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -04001013void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
Sean Middleditch340a51b2009-03-19 02:08:46 -04001014 const char *buffer, size_t size) {
Sean Middleditch90e79da2009-03-19 15:17:13 -04001015 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
1016 static const char se[2] = { TELNET_IAC, TELNET_SE };
1017
1018 _send(telnet, sb, 3);
Sean Middleditch4f0c37f2009-03-20 23:08:55 -04001019 telnet_send(telnet, buffer, size);
Sean Middleditch90e79da2009-03-19 15:17:13 -04001020 _send(telnet, se, 2);
Sean Middleditch61f8eb62009-03-14 04:57:27 -04001021
Sean Middleditchdb128162009-10-25 02:02:17 -07001022#if defined(HAVE_ZLIB)
Sean Middleditch72cc9642009-03-15 11:50:36 -04001023 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
1024 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -04001025 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -04001026 if (telnet->flags & TELNET_FLAG_PROXY &&
1027 telopt == TELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -04001028
Sean Middleditchf65f27d2009-03-19 02:32:04 -04001029 if (_init_zlib(telnet, 1, 1) != TELNET_EOK)
Sean Middleditchd922c6f2009-03-14 22:35:01 -04001030 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -04001031
1032 /* notify app that compression was enabled */
Sean Middleditche5327da2009-03-21 00:54:50 -04001033 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -04001034 }
Sean Middleditchdb128162009-10-25 02:02:17 -07001035#endif /* defined(HAVE_ZLIB) */
Sean Middleditch29144852009-03-12 23:14:47 -04001036}
Sean Middleditch124a1c22009-03-15 13:20:03 -04001037
Sean Middleditchf65f27d2009-03-19 02:32:04 -04001038void telnet_begin_compress2(telnet_t *telnet) {
Sean Middleditchdb128162009-10-25 02:02:17 -07001039#if defined(HAVE_ZLIB)
Sean Middleditchf65f27d2009-03-19 02:32:04 -04001040 static const char compress2[] = { TELNET_IAC, TELNET_SB,
1041 TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE };
Sean Middleditch124a1c22009-03-15 13:20:03 -04001042
Sean Middleditch124a1c22009-03-15 13:20:03 -04001043 /* attempt to create output stream first, bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -04001044 if (_init_zlib(telnet, 1, 0) != TELNET_EOK)
Sean Middleditch124a1c22009-03-15 13:20:03 -04001045 return;
1046
Sean Middleditchfbe93e32009-03-15 23:39:31 -04001047 /* send compression marker. we send directly to the event handler
1048 * instead of passing through _send because _send would result in
1049 * the compress marker itself being compressed.
1050 */
Sean Middleditche5327da2009-03-21 00:54:50 -04001051 _event(telnet, TELNET_EV_SEND, 0, 0, compress2, sizeof(compress2), 0, 0);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -04001052
1053 /* notify app that compression was successfully enabled */
Sean Middleditche5327da2009-03-21 00:54:50 -04001054 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0);
Sean Middleditchdb128162009-10-25 02:02:17 -07001055#endif /* defined(HAVE_ZLIB) */
Sean Middleditch124a1c22009-03-15 13:20:03 -04001056}
Sean Middleditchd58f49f2009-03-16 12:49:35 -04001057
Sean Middleditch4a156042009-03-16 17:10:58 -04001058/* send formatted data with \r and \n translation in addition to IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -04001059int telnet_printf(telnet_t *telnet, const char *fmt, ...) {
Sean Middleditch8daf7742009-03-19 02:05:24 -04001060 static const char CRLF[] = { '\r', '\n' };
1061 static const char CRNUL[] = { '\r', '\0' };
Sean Middleditch4a156042009-03-16 17:10:58 -04001062 char buffer[4096];
1063 va_list va;
1064 int rs, i, l;
1065
1066 /* format */
1067 va_start(va, fmt);
1068 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
1069 va_end(va);
1070
1071 /* send */
1072 for (l = i = 0; i != rs; ++i) {
1073 /* special characters */
Sean Middleditch0003f052009-04-27 19:26:05 -04001074 if (buffer[i] == (char)TELNET_IAC || buffer[i] == '\r' ||
Sean Middleditch4a156042009-03-16 17:10:58 -04001075 buffer[i] == '\n') {
1076 /* dump prior portion of text */
1077 if (i != l)
Sean Middleditchb43c10c2009-03-20 23:21:02 -04001078 _send(telnet, buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -04001079 l = i + 1;
1080
1081 /* IAC -> IAC IAC */
Sean Middleditch0003f052009-04-27 19:26:05 -04001082 if (buffer[i] == (char)TELNET_IAC)
Sean Middleditch4f0c37f2009-03-20 23:08:55 -04001083 telnet_iac(telnet, TELNET_IAC);
Sean Middleditch4a156042009-03-16 17:10:58 -04001084 /* automatic translation of \r -> CRNUL */
1085 else if (buffer[i] == '\r')
1086 _send(telnet, CRNUL, 2);
1087 /* automatic translation of \n -> CRLF */
1088 else if (buffer[i] == '\n')
1089 _send(telnet, CRLF, 2);
1090 }
1091 }
1092
1093 /* send whatever portion of buffer is left */
1094 if (i != l)
Sean Middleditchb43c10c2009-03-20 23:21:02 -04001095 _send(telnet, buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -04001096
1097 return rs;
1098}
Sean Middleditchf65f27d2009-03-19 02:32:04 -04001099
Sean Middleditch7491bf42009-03-20 23:52:18 -04001100/* send formatted data through telnet_send */
Sean Middleditch9dcfd4b2009-10-14 16:17:46 -07001101int telnet_raw_printf(telnet_t *telnet, const char *fmt, ...) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -04001102 char buffer[4096];
1103 va_list va;
1104 int rs;
1105
1106 /* format */
1107 va_start(va, fmt);
1108 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
1109 va_end(va);
1110
1111 /* send */
Sean Middleditchb43c10c2009-03-20 23:21:02 -04001112 telnet_send(telnet, buffer, rs);
Sean Middleditchf65f27d2009-03-19 02:32:04 -04001113
1114 return rs;
1115}
Sean Middleditche22b4772009-03-22 16:44:40 -04001116
Sean Middleditcheb950a82009-03-22 23:04:28 -04001117/* send formatted subnegotiation data for TTYPE/ENVIRON/NEW-ENVIRON/MSSP */
1118void telnet_format_sb(telnet_t *telnet, unsigned char telopt,
1119 size_t count, ...) {
1120 va_list va;
1121 size_t i;
1122
1123 /* subnegotiation header */
1124 telnet_begin_sb(telnet, telopt);
1125
1126 /* iterate over the arguments pulling out integers and strings */
1127 va_start(va, count);
1128 for (i = 0; i != count; ++i) {
1129 char t;
1130 const char* s;
1131 t = va_arg(va, int);
1132 s = va_arg(va, const char *);
1133 telnet_send(telnet, &t, 1);
1134 telnet_send(telnet, s, strlen(s));
1135 }
1136 va_end(va);
1137
1138 /* footer */
1139 telnet_finish_sb(telnet);
1140}
1141
Sean Middleditche22b4772009-03-22 16:44:40 -04001142/* send ZMP data */
1143void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv) {
1144 size_t i;
1145
1146 /* ZMP header */
1147 telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
1148
1149 /* send out each argument, including trailing NUL byte */
1150 for (i = 0; i != argc; ++i)
1151 telnet_send(telnet, argv[i], strlen(argv[i] + 1));
1152
1153 /* ZMP footer */
1154 telnet_finish_sb(telnet);
1155}
1156
1157/* send ZMP data using varargs */
1158void telnet_send_zmpv(telnet_t *telnet, ...) {
1159 va_list va;
1160 const char* arg;
1161
1162 /* ZMP header */
1163 telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
1164
1165 /* send out each argument, including trailing NUL byte */
1166 va_start(va, telnet);
1167 while ((arg = va_arg(va, const char *)) != NULL)
1168 telnet_send(telnet, arg, strlen(arg) + 1);
1169 va_end(va);
1170
1171 /* ZMP footer */
1172 telnet_finish_sb(telnet);
1173}