blob: df8f93536eb91e9177cb4cff4d20b349fd1c0aa9 [file] [log] [blame]
Sean Middleditch6aef0732009-03-12 23:27:35 -04001/*
Sean Middleditch9de15982009-03-14 03:35:49 -04002 * Sean Middleditch
3 * sean@sourcemud.org
4 *
Sean Middleditch6aef0732009-03-12 23:27:35 -04005 * The author or authors of this code dedicate any and all copyright interest
6 * in this code to the public domain. We make this dedication for the benefit
7 * of the public at large and to the detriment of our heirs and successors. We
8 * intend this dedication to be an overt act of relinquishment in perpetuity of
9 * all present and future rights to this code under copyright law.
10 */
11
Sean Middleditch4d9444d2009-03-13 22:48:05 -040012#include <malloc.h>
Sean Middleditch9de15982009-03-14 03:35:49 -040013#include <string.h>
Sean Middleditchd922c6f2009-03-14 22:35:01 -040014#include <stdio.h>
15#include <errno.h>
16#include <string.h>
17#include <stdarg.h>
Sean Middleditch9de15982009-03-14 03:35:49 -040018
19#ifdef HAVE_ZLIB
20#include "zlib.h"
21#endif
22
Sean Middleditch29144852009-03-12 23:14:47 -040023#include "libtelnet.h"
24
Sean Middleditch9b07e922009-03-19 18:18:44 -040025/* inlinable functions */
26#if __GNUC__ || __STDC_VERSION__ >= 199901L
27# define INLINE inline
28#else
29# define INLINE
30#endif
31
Sean Middleditch90e79da2009-03-19 15:17:13 -040032/* RFC1143 option negotiation state */
33typedef struct telnet_rfc1143_t {
34 unsigned char telopt;
35 char us:4, him:4;
36} telnet_rfc1143_t;
37
Sean Middleditch5b5bc922009-03-15 23:02:10 -040038/* RFC1143 state names */
Sean Middleditch7491bf42009-03-20 23:52:18 -040039#define Q_NO 0
40#define Q_YES 1
41#define Q_WANTNO 2
42#define Q_WANTYES 3
43#define Q_WANTNO_OP 4
44#define Q_WANTYES_OP 5
Sean Middleditch5b5bc922009-03-15 23:02:10 -040045
Sean Middleditch4d9444d2009-03-13 22:48:05 -040046/* buffer sizes */
Sean Middleditch340a51b2009-03-19 02:08:46 -040047static const size_t _buffer_sizes[] = {
Sean Middleditch4d9444d2009-03-13 22:48:05 -040048 0,
49 512,
50 2048,
51 8192,
52 16384,
53};
Sean Middleditch340a51b2009-03-19 02:08:46 -040054static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) /
Sean Middleditch812358d2009-03-15 23:24:03 -040055 sizeof(_buffer_sizes[0]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -040056
Sean Middleditch34bb0992009-03-21 00:20:44 -040057/* event dispatch helper */
58static INLINE void _event(telnet_t *telnet, telnet_event_type_t type,
Sean Middleditch97a8cb22009-03-16 16:51:41 -040059 unsigned char command, unsigned char telopt,
Sean Middleditche5327da2009-03-21 00:54:50 -040060 const char *buffer, size_t size, const char **argv, size_t argc) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -040061 telnet_event_t ev;
Sean Middleditche5327da2009-03-21 00:54:50 -040062 ev.argv = argv;
63 ev.argc = argc;
Sean Middleditch5b5bc922009-03-15 23:02:10 -040064 ev.buffer = buffer;
65 ev.size = size;
Sean Middleditch637df7f2009-03-15 12:57:32 -040066 ev.type = type;
67 ev.command = command;
68 ev.telopt = telopt;
Sean Middleditch637df7f2009-03-15 12:57:32 -040069
Sean Middleditch9f79cc52009-03-15 13:39:24 -040070 telnet->eh(telnet, &ev, telnet->ud);
Sean Middleditch637df7f2009-03-15 12:57:32 -040071}
72
Sean Middleditchd922c6f2009-03-14 22:35:01 -040073/* error generation function */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040074static telnet_error_t _error(telnet_t *telnet, unsigned line,
75 const char* func, telnet_error_t err, int fatal, const char *fmt,
Sean Middleditchfbe93e32009-03-15 23:39:31 -040076 ...) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -040077 char buffer[512];
78 va_list va;
79
80 /* format error intro */
Sean Middleditch812358d2009-03-15 23:24:03 -040081 snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func);
Sean Middleditchd922c6f2009-03-14 22:35:01 -040082
Sean Middleditchb43c10c2009-03-20 23:21:02 -040083 /* format informational text */
Sean Middleditchd922c6f2009-03-14 22:35:01 -040084 va_start(va, fmt);
85 vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
86 fmt, va);
87 va_end(va);
88
Sean Middleditchb43c10c2009-03-20 23:21:02 -040089 /* send error event to the user */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040090 _event(telnet, fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING, err,
Sean Middleditche5327da2009-03-21 00:54:50 -040091 0, buffer, strlen(buffer), 0, 0);
Sean Middleditchfbe93e32009-03-15 23:39:31 -040092
93 return err;
Sean Middleditchd922c6f2009-03-14 22:35:01 -040094}
95
Sean Middleditche5b47592009-03-16 17:37:43 -040096#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -040097/* initialize the zlib box for a telnet box; if deflate is non-zero, it
98 * initializes zlib for delating (compression), otherwise for inflating
Sean Middleditchf65f27d2009-03-19 02:32:04 -040099 * (decompression). returns TELNET_EOK on success, something else on
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400100 * failure.
Sean Middleditch72cc9642009-03-15 11:50:36 -0400101 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400102telnet_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400103 z_stream *z;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400104 int rs;
105
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400106 /* if compression is already enabled, fail loudly */
107 if (telnet->z != 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400108 return _error(telnet, __LINE__, __func__, TELNET_EBADVAL,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400109 err_fatal, "cannot initialize compression twice");
110
Sean Middleditch72cc9642009-03-15 11:50:36 -0400111 /* allocate zstream box */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400112 if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400113 return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal,
Sean Middleditch16992272009-03-15 19:42:03 -0400114 "malloc() failed: %s", strerror(errno));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400115
116 /* initialize */
117 if (deflate) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400118 if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) {
119 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400120 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400121 err_fatal, "deflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400122 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400123 telnet->flags |= TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400124 } else {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400125 if ((rs = inflateInit(z)) != Z_OK) {
126 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400127 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400128 err_fatal, "inflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400129 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400130 telnet->flags &= ~TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400131 }
132
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400133 telnet->z = z;
134
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400135 return TELNET_EOK;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400136}
Sean Middleditche5b47592009-03-16 17:37:43 -0400137#endif
Sean Middleditch72cc9642009-03-15 11:50:36 -0400138
Sean Middleditch8b788962009-03-16 01:06:27 -0400139/* push bytes out, compressing them first if need be */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400140static void _send(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400141 size_t size) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400142#ifdef HAVE_ZLIB
143 /* if we have a deflate (compression) zlib box, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400144 if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400145 char deflate_buffer[1024];
Sean Middleditch8b788962009-03-16 01:06:27 -0400146 int rs;
147
148 /* initialize z state */
Sean Middleditch97a8cb22009-03-16 16:51:41 -0400149 telnet->z->next_in = (unsigned char *)buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400150 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400151 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400152 telnet->z->avail_out = sizeof(deflate_buffer);
153
154 /* deflate until buffer exhausted and all output is produced */
155 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
156 /* compress */
157 if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400158 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch8b788962009-03-16 01:06:27 -0400159 "deflate() failed: %s", zError(rs));
160 deflateEnd(telnet->z);
161 free(telnet->z);
162 telnet->z = 0;
163 break;
164 }
165
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400166 _event(telnet, TELNET_EV_SEND, 0, 0, deflate_buffer,
Sean Middleditche5327da2009-03-21 00:54:50 -0400167 sizeof(deflate_buffer) - telnet->z->avail_out, 0, 0);
Sean Middleditch8b788962009-03-16 01:06:27 -0400168
169 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400170 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400171 telnet->z->avail_out = sizeof(deflate_buffer);
172 }
173
174 /* COMPRESS2 is not negotiated, just send */
175 } else
176#endif /* HAVE_ZLIB */
Sean Middleditche5327da2009-03-21 00:54:50 -0400177 _event(telnet, TELNET_EV_SEND, 0, 0, buffer, size, 0, 0);
Sean Middleditch8b788962009-03-16 01:06:27 -0400178}
179
Sean Middleditch34bb0992009-03-21 00:20:44 -0400180/* check if we support a particular telopt; if us is non-zero, we
181 * check if we (local) supports it, otherwise we check if he (remote)
182 * supports it. return non-zero if supported, zero if not supported.
183 */
184static INLINE int _check_telopt(telnet_t *telnet, unsigned char telopt,
185 int us) {
186 int i;
187
188 /* if we have no telopts table, we obviously don't support it */
189 if (telnet->telopts == 0)
190 return 0;
191
192 /* loop unti found or end marker (us and him both 0) */
Sean Middleditchbfc641e2009-03-22 16:26:06 -0400193 for (i = 0; telnet->telopts[i].telopt != -1; ++i) {
194 if (telnet->telopts[i].telopt == telopt) {
195 if (us && telnet->telopts[i].us == TELNET_WILL)
196 return 1;
197 else if (!us && telnet->telopts[i].him == TELNET_DO)
198 return 1;
199 else
200 return 0;
201 }
202 }
Sean Middleditch34bb0992009-03-21 00:20:44 -0400203
204 /* not found, so not supported */
205 return 0;
206}
207
Sean Middleditch8b788962009-03-16 01:06:27 -0400208/* retrieve RFC1143 option state */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400209static INLINE telnet_rfc1143_t _get_rfc1143(telnet_t *telnet,
210 unsigned char telopt) {
Sean Middleditch4987f9f2009-03-19 12:51:40 -0400211 const telnet_rfc1143_t empty = { telopt, 0, 0};
Sean Middleditch8b788962009-03-16 01:06:27 -0400212 int i;
213
214 /* search for entry */
215 for (i = 0; i != telnet->q_size; ++i)
216 if (telnet->q[i].telopt == telopt)
217 return telnet->q[i];
218
219 /* not found, return empty value */
220 return empty;
221}
222
223/* save RFC1143 option state */
Sean Middleditch7491bf42009-03-20 23:52:18 -0400224static INLINE void _set_rfc1143(telnet_t *telnet, unsigned char telopt,
225 char us, char him) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400226 telnet_rfc1143_t *qtmp;
Sean Middleditch8b788962009-03-16 01:06:27 -0400227 int i;
228
229 /* search for entry */
230 for (i = 0; i != telnet->q_size; ++i) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400231 if (telnet->q[i].telopt == telopt) {
232 telnet->q[i].us = us;
233 telnet->q[i].him = him;
Sean Middleditch8b788962009-03-16 01:06:27 -0400234 return;
235 }
236 }
237
238 /* we're going to need to track state for it, so grow the queue
239 * and put the telopt into it; bail on allocation error
240 */
Sean Middleditch5dea1f72009-03-19 12:45:34 -0400241 if ((qtmp = (telnet_rfc1143_t *)realloc(telnet->q, sizeof(
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400242 telnet_rfc1143_t) * (telnet->q_size + 1))) == 0) {
243 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch8b788962009-03-16 01:06:27 -0400244 "malloc() failed: %s", strerror(errno));
245 return;
246 }
247 telnet->q = qtmp;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400248 telnet->q[telnet->q_size].telopt = telopt;
249 telnet->q[telnet->q_size].us = us;
250 telnet->q[telnet->q_size].him = him;
251 ++telnet->q_size;
Sean Middleditch8b788962009-03-16 01:06:27 -0400252}
253
Sean Middleditch90e79da2009-03-19 15:17:13 -0400254/* send negotiation bytes */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400255static INLINE void _send_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400256 unsigned char telopt) {
257 char bytes[3] = { TELNET_IAC, cmd, telopt };
258 _send(telnet, bytes, 3);
259}
260
Sean Middleditch8b788962009-03-16 01:06:27 -0400261/* negotiation handling magic for RFC1143 */
Sean Middleditch330c2612009-03-20 23:29:17 -0400262static void _negotiate(telnet_t *telnet, unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400263 telnet_rfc1143_t q;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400264
265 /* in PROXY mode, just pass it thru and do nothing */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400266 if (telnet->flags & TELNET_FLAG_PROXY) {
Sean Middleditch330c2612009-03-20 23:29:17 -0400267 switch ((int)telnet->state) {
268 case TELNET_STATE_WILL:
Sean Middleditche5327da2009-03-21 00:54:50 -0400269 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400270 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400271 case TELNET_STATE_WONT:
Sean Middleditche5327da2009-03-21 00:54:50 -0400272 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400273 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400274 case TELNET_STATE_DO:
Sean Middleditche5327da2009-03-21 00:54:50 -0400275 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400276 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400277 case TELNET_STATE_DONT:
Sean Middleditche5327da2009-03-21 00:54:50 -0400278 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400279 break;
280 }
281 return;
282 }
283
284 /* lookup the current state of the option */
Sean Middleditch8b788962009-03-16 01:06:27 -0400285 q = _get_rfc1143(telnet, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400286
287 /* start processing... */
Sean Middleditch330c2612009-03-20 23:29:17 -0400288 switch ((int)telnet->state) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400289 /* request to enable option on remote end or confirm DO */
Sean Middleditch330c2612009-03-20 23:29:17 -0400290 case TELNET_STATE_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400291 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400292 case Q_NO:
Sean Middleditch34bb0992009-03-21 00:20:44 -0400293 if (_check_telopt(telnet, telopt, 0)) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400294 _set_rfc1143(telnet, telopt, q.us, Q_YES);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400295 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch94738a42009-03-22 16:33:22 -0400296 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400297 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400298 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400299 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400300 case Q_WANTNO:
301 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditche5327da2009-03-21 00:54:50 -0400302 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400303 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400304 "DONT answered by WILL");
305 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400306 case Q_WANTNO_OP:
307 _set_rfc1143(telnet, telopt, q.us, Q_YES);
Sean Middleditche5327da2009-03-21 00:54:50 -0400308 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400309 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400310 "DONT answered by WILL");
311 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400312 case Q_WANTYES:
313 _set_rfc1143(telnet, telopt, q.us, Q_YES);
Sean Middleditche5327da2009-03-21 00:54:50 -0400314 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400315 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400316 case Q_WANTYES_OP:
317 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400318 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400319 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400320 break;
321 }
322 break;
323
324 /* request to disable option on remote end, confirm DONT, reject DO */
Sean Middleditch330c2612009-03-20 23:29:17 -0400325 case TELNET_STATE_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400326 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400327 case Q_YES:
328 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400329 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400330 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400331 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400332 case Q_WANTNO:
333 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditche5327da2009-03-21 00:54:50 -0400334 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400335 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400336 case Q_WANTNO_OP:
337 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES);
Sean Middleditche5327da2009-03-21 00:54:50 -0400338 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400339 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400340 case Q_WANTYES:
341 case Q_WANTYES_OP:
342 _set_rfc1143(telnet, telopt, q.us, Q_NO);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400343 break;
344 }
345 break;
346
347 /* request to enable option on local end or confirm WILL */
Sean Middleditch330c2612009-03-20 23:29:17 -0400348 case TELNET_STATE_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400349 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400350 case Q_NO:
Sean Middleditch34bb0992009-03-21 00:20:44 -0400351 if (_check_telopt(telnet, telopt, 1)) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400352 _set_rfc1143(telnet, telopt, Q_YES, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400353 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch94738a42009-03-22 16:33:22 -0400354 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400355 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400356 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400357 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400358 case Q_WANTNO:
359 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400360 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400361 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400362 "WONT answered by DO");
363 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400364 case Q_WANTNO_OP:
365 _set_rfc1143(telnet, telopt, Q_YES, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400366 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400367 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400368 "WONT answered by DO");
369 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400370 case Q_WANTYES:
371 _set_rfc1143(telnet, telopt, Q_YES, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400372 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400373 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400374 case Q_WANTYES_OP:
375 _set_rfc1143(telnet, telopt, Q_WANTNO, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400376 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400377 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400378 break;
379 }
380 break;
381
382 /* request to disable option on local end, confirm WONT, reject WILL */
Sean Middleditch330c2612009-03-20 23:29:17 -0400383 case TELNET_STATE_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400384 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400385 case Q_YES:
386 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400387 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditche5327da2009-03-21 00:54:50 -0400388 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400389 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400390 case Q_WANTNO:
391 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400392 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400393 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400394 case Q_WANTNO_OP:
395 _set_rfc1143(telnet, telopt, Q_WANTYES, q.him);
Sean Middleditche5327da2009-03-21 00:54:50 -0400396 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400397 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400398 case Q_WANTYES:
399 case Q_WANTYES_OP:
400 _set_rfc1143(telnet, telopt, Q_NO, q.him);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400401 break;
402 }
403 break;
404 }
405}
406
Sean Middleditche5327da2009-03-21 00:54:50 -0400407/* process a subnegotiation buffer; return non-zero if the current buffer
408 * must be aborted and reprocessed due to COMPRESS2 being activated
409 */
410static int _subnegotiate(telnet_t *telnet) {
411 /* invoke callback */
412 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt,
413 telnet->buffer, telnet->buffer_pos, 0, 0);
414
415 /* received COMPRESS2 begin marker, setup our zlib box and
416 * start handling the compressed stream if it's not already.
417 */
418#ifdef HAVE_ZLIB
419 if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) {
420 if (_init_zlib(telnet, 0, 1) != TELNET_EOK)
421 return 0;
422
423 /* notify app that compression was enabled */
424 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0);
425 return 1;
426 }
427#endif /* HAVE_ZLIB */
428
429 /* parse ZMP args */
430 if (telnet->sb_telopt == TELNET_TELOPT_ZMP) {
431 const char **argv;
432 const char *c = telnet->buffer;
433 size_t i, argc = 0;
434
435 /* make sure this is a valid ZMP buffer */
436 if (telnet->buffer_pos == 0 || telnet->buffer[telnet->buffer_pos - 1]
437 != 0)
438 return 0;
439
440 /* count arguments */
Sean Middleditchf6daa852009-03-21 01:16:20 -0400441 while (c != telnet->buffer + telnet->buffer_pos) {
Sean Middleditche5327da2009-03-21 00:54:50 -0400442 ++argc;
443 c += strlen(c) + 1;
444 }
445
446 /* allocate argument array, bail on error */
447 if ((argv = (const char **)malloc(sizeof(char *) * argc)) == 0) {
448 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
449 "malloc() failed: %s", strerror(errno));
450 return 0;
451 }
452
453 /* populate argument array */
454 for (i = 0, c = telnet->buffer; i != argc; ++i) {
455 argv[i] = c;
456 c += strlen(c) + 1;
457 }
458
459 /* invoke ZMP event */
460 _event(telnet, TELNET_EV_ZMP, 0, 0, 0, 0, argv, argc);
461
462 /* free argument array */
463 free(argv);
464 }
465
466 return 0;
467}
468
Sean Middleditch29144852009-03-12 23:14:47 -0400469/* initialize a telnet state tracker */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400470void telnet_init(telnet_t *telnet, const telnet_telopt_t *telopts,
471 telnet_event_handler_t eh, unsigned char flags, void *user_data) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400472 memset(telnet, 0, sizeof(telnet_t));
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400473 telnet->ud = user_data;
Sean Middleditch34bb0992009-03-21 00:20:44 -0400474 telnet->telopts = telopts;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400475 telnet->eh = eh;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400476 telnet->flags = flags;
Sean Middleditch29144852009-03-12 23:14:47 -0400477}
478
479/* free up any memory allocated by a state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400480void telnet_free(telnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400481 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400482 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400483 free(telnet->buffer);
484 telnet->buffer = 0;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400485 telnet->buffer_size = 0;
486 telnet->buffer_pos = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400487 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400488
Sean Middleditche5b47592009-03-16 17:37:43 -0400489#ifdef HAVE_ZLIB
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400490 /* free zlib box */
491 if (telnet->z != 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400492 if (telnet->flags & TELNET_PFLAG_DEFLATE)
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400493 deflateEnd(telnet->z);
494 else
495 inflateEnd(telnet->z);
496 free(telnet->z);
497 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400498 }
Sean Middleditche5b47592009-03-16 17:37:43 -0400499#endif
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400500
501 /* free RFC1143 queue */
502 if (telnet->q) {
503 free(telnet->q);
504 telnet->q = 0;
505 telnet->q_size = 0;
506 }
Sean Middleditch29144852009-03-12 23:14:47 -0400507}
508
Sean Middleditch51ad6792009-03-13 20:15:59 -0400509/* push a byte into the telnet buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400510static telnet_error_t _buffer_byte(telnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400511 unsigned char byte) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400512 char *new_buffer;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400513 size_t i;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400514
Sean Middleditch51ad6792009-03-13 20:15:59 -0400515 /* check if we're out of room */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400516 if (telnet->buffer_pos == telnet->buffer_size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400517 /* find the next buffer size */
518 for (i = 0; i != _buffer_sizes_count; ++i) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400519 if (_buffer_sizes[i] == telnet->buffer_size)
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400520 break;
521 }
522
523 /* overflow -- can't grow any more */
524 if (i >= _buffer_sizes_count - 1) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400525 _error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400526 "subnegotiation buffer size limit reached");
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400527 telnet_free(telnet);
528 return TELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400529 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400530
531 /* (re)allocate buffer */
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400532 new_buffer = (char *)realloc(telnet->buffer, _buffer_sizes[i + 1]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400533 if (new_buffer == 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400534 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch16992272009-03-15 19:42:03 -0400535 "realloc() failed");
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400536 telnet_free(telnet);
537 return TELNET_ENOMEM;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400538 }
539
540 telnet->buffer = new_buffer;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400541 telnet->buffer_size = _buffer_sizes[i + 1];
Sean Middleditch51ad6792009-03-13 20:15:59 -0400542 }
543
544 /* push the byte, all set */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400545 telnet->buffer[telnet->buffer_pos++] = byte;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400546 return TELNET_EOK;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400547}
548
Sean Middleditch2d5f4bf2009-03-20 23:32:47 -0400549static void _process(telnet_t *telnet, const char *buffer, size_t size) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400550 unsigned char byte;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400551 size_t i, start;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400552 for (i = start = 0; i != size; ++i) {
553 byte = buffer[i];
554 switch (telnet->state) {
555 /* regular data */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400556 case TELNET_STATE_DATA:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400557 /* on an IAC byte, pass through all pending bytes and
558 * switch states */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400559 if (byte == TELNET_IAC) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400560 if (i != start)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400561 _event(telnet, TELNET_EV_DATA, 0, 0, &buffer[start],
Sean Middleditche5327da2009-03-21 00:54:50 -0400562 i - start, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400563 telnet->state = TELNET_STATE_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400564 }
565 break;
566
567 /* IAC command */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400568 case TELNET_STATE_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400569 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400570 /* subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400571 case TELNET_SB:
572 telnet->state = TELNET_STATE_SB;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400573 break;
574 /* negotiation commands */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400575 case TELNET_WILL:
576 telnet->state = TELNET_STATE_WILL;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400577 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400578 case TELNET_WONT:
579 telnet->state = TELNET_STATE_WONT;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400580 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400581 case TELNET_DO:
582 telnet->state = TELNET_STATE_DO;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400583 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400584 case TELNET_DONT:
585 telnet->state = TELNET_STATE_DONT;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400586 break;
587 /* IAC escaping */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400588 case TELNET_IAC:
Sean Middleditche5327da2009-03-21 00:54:50 -0400589 _event(telnet, TELNET_EV_DATA, 0, 0, (char*)&byte, 1, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400590 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400591 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400592 break;
593 /* some other command */
594 default:
Sean Middleditche5327da2009-03-21 00:54:50 -0400595 _event(telnet, TELNET_EV_IAC, byte, 0, 0, 0, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400596 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400597 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400598 }
599 break;
600
601 /* negotiation commands */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400602 case TELNET_STATE_WILL:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400603 case TELNET_STATE_WONT:
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400604 case TELNET_STATE_DO:
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400605 case TELNET_STATE_DONT:
Sean Middleditch330c2612009-03-20 23:29:17 -0400606 _negotiate(telnet, byte);
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400607 start = i + 1;
608 telnet->state = TELNET_STATE_DATA;
609 break;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400610
Sean Middleditchda0e6952009-03-15 13:28:09 -0400611 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400612 case TELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400613 telnet->sb_telopt = byte;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400614 telnet->buffer_pos = 0;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400615 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditchda0e6952009-03-15 13:28:09 -0400616 break;
617
618 /* subnegotiation -- buffer bytes until end request */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400619 case TELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400620 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400621 if (byte == TELNET_IAC) {
622 telnet->state = TELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400623 /* buffer the byte, or bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400624 } else if (_buffer_byte(telnet, byte) != TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400625 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400626 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400627 }
628 break;
629
Sean Middleditch6b372882009-03-14 13:06:47 -0400630 /* IAC escaping inside a subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400631 case TELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400632 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400633 /* end subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400634 case TELNET_SE:
Sean Middleditch8c567352009-03-21 01:07:18 -0400635 /* return to default state */
636 start = i + 1;
637 telnet->state = TELNET_STATE_DATA;
638
Sean Middleditche5327da2009-03-21 00:54:50 -0400639 /* process subnegotiation */
640 if (_subnegotiate(telnet) != 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400641 /* any remaining bytes in the buffer are compressed.
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400642 * we have to re-invoke telnet_recv to get those
Sean Middleditch9de15982009-03-14 03:35:49 -0400643 * bytes inflated and abort trying to process the
644 * remaining compressed bytes in the current _process
645 * buffer argument
646 */
Sean Middleditch8c567352009-03-21 01:07:18 -0400647 telnet_recv(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400648 return;
649 }
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400650 break;
651 /* escaped IAC byte */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400652 case TELNET_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400653 /* push IAC into buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400654 if (_buffer_byte(telnet, TELNET_IAC) !=
655 TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400656 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400657 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400658 } else {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400659 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400660 }
661 break;
Sean Middleditch2d5f4bf2009-03-20 23:32:47 -0400662 /* something else -- protocol error. attempt to process
663 * content in subnegotiation buffer, then evaluate the
664 * given command as an IAC code.
665 */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400666 default:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400667 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400668 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400669 byte);
Sean Middleditch2d5f4bf2009-03-20 23:32:47 -0400670
Sean Middleditchf6daa852009-03-21 01:16:20 -0400671 /* enter IAC state */
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400672 start = i + 1;
Sean Middleditchf6daa852009-03-21 01:16:20 -0400673 telnet->state = TELNET_STATE_IAC;
Sean Middleditche5327da2009-03-21 00:54:50 -0400674
675 /* process subnegotiation; see comment in
676 * TELNET_STATE_SB_DATA_IAC about invoking telnet_recv()
677 */
678 if (_subnegotiate(telnet) != 0) {
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400679 telnet_recv(telnet, &buffer[start], size - start);
Sean Middleditche5327da2009-03-21 00:54:50 -0400680 return;
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400681 } else {
682 /* recursive call to get the current input byte processed
683 * as a regular IAC command. we could use a goto, but
684 * that would be gross.
685 */
Sean Middleditch3f96c5c2009-03-21 01:11:46 -0400686 _process(telnet, (char *)&byte, 1);
Sean Middleditche5327da2009-03-21 00:54:50 -0400687 }
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400688 break;
689 }
690 break;
691 }
692 }
693
694 /* pass through any remaining bytes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400695 if (telnet->state == TELNET_STATE_DATA && i != start)
Sean Middleditche5327da2009-03-21 00:54:50 -0400696 _event(telnet, TELNET_EV_DATA, 0, 0, buffer + start, i - start, 0, 0);
Sean Middleditch29144852009-03-12 23:14:47 -0400697}
698
Sean Middleditch9de15982009-03-14 03:35:49 -0400699/* push a bytes into the state tracker */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400700void telnet_recv(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400701 size_t size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400702#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400703 /* if we have an inflate (decompression) zlib stream, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400704 if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400705 char inflate_buffer[4096];
Sean Middleditch9de15982009-03-14 03:35:49 -0400706 int rs;
707
708 /* initialize zlib state */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400709 telnet->z->next_in = (unsigned char*)buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400710 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400711 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400712 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400713
714 /* inflate until buffer exhausted and all output is produced */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400715 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400716 /* reset output buffer */
717
718 /* decompress */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400719 rs = inflate(telnet->z, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400720
721 /* process the decompressed bytes on success */
722 if (rs == Z_OK || rs == Z_STREAM_END)
723 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400724 telnet->z->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400725 else
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400726 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch16992272009-03-15 19:42:03 -0400727 "inflate() failed: %s", zError(rs));
Sean Middleditch9de15982009-03-14 03:35:49 -0400728
729 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400730 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400731 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400732
733 /* on error (or on end of stream) disable further inflation */
734 if (rs != Z_OK) {
Sean Middleditche5327da2009-03-21 00:54:50 -0400735 _event(telnet, TELNET_EV_COMPRESS, 0, 0, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400736
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400737 inflateEnd(telnet->z);
738 free(telnet->z);
739 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400740 break;
741 }
742 }
743
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400744 /* COMPRESS2 is not negotiated, just process */
745 } else
746#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400747 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400748}
749
Sean Middleditch29144852009-03-12 23:14:47 -0400750/* send an iac command */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400751void telnet_iac(telnet_t *telnet, unsigned char cmd) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400752 char bytes[2] = { TELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400753 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400754}
755
756/* send negotiation */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400757void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch8b788962009-03-16 01:06:27 -0400758 unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400759 telnet_rfc1143_t q;
Sean Middleditch8b788962009-03-16 01:06:27 -0400760
761 /* if we're in proxy mode, just send it now */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400762 if (telnet->flags & TELNET_FLAG_PROXY) {
763 char bytes[3] = { TELNET_IAC, cmd, telopt };
Sean Middleditch8b788962009-03-16 01:06:27 -0400764 _send(telnet, bytes, 3);
765 return;
766 }
767
768 /* get current option states */
769 q = _get_rfc1143(telnet, telopt);
770
771 switch (cmd) {
772 /* advertise willingess to support an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400773 case TELNET_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400774 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400775 case Q_NO:
776 _set_rfc1143(telnet, telopt, Q_WANTYES, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400777 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400778 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400779 case Q_WANTNO:
780 _set_rfc1143(telnet, telopt, Q_WANTNO_OP, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400781 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400782 case Q_WANTYES_OP:
783 _set_rfc1143(telnet, telopt, Q_WANTYES, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400784 break;
785 }
786 break;
787
788 /* force turn-off of locally enabled option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400789 case TELNET_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400790 switch (q.us) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400791 case Q_YES:
792 _set_rfc1143(telnet, telopt, Q_WANTNO, q.him);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400793 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400794 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400795 case Q_WANTYES:
796 _set_rfc1143(telnet, telopt, Q_WANTYES_OP, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400797 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400798 case Q_WANTNO_OP:
799 _set_rfc1143(telnet, telopt, Q_WANTNO, q.him);
Sean Middleditch8b788962009-03-16 01:06:27 -0400800 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400801 }
802 break;
803
804 /* ask remote end to enable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400805 case TELNET_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400806 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400807 case Q_NO:
808 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400809 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400810 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400811 case Q_WANTNO:
812 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO_OP);
Sean Middleditch8b788962009-03-16 01:06:27 -0400813 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400814 case Q_WANTYES_OP:
815 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES);
Sean Middleditch8b788962009-03-16 01:06:27 -0400816 break;
817 }
818 break;
819
820 /* demand remote end disable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400821 case TELNET_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400822 switch (q.him) {
Sean Middleditch7491bf42009-03-20 23:52:18 -0400823 case Q_YES:
824 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400825 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400826 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400827 case Q_WANTYES:
828 _set_rfc1143(telnet, telopt, q.us, Q_WANTYES_OP);
Sean Middleditch8b788962009-03-16 01:06:27 -0400829 break;
Sean Middleditch7491bf42009-03-20 23:52:18 -0400830 case Q_WANTNO_OP:
831 _set_rfc1143(telnet, telopt, q.us, Q_WANTNO);
Sean Middleditch8b788962009-03-16 01:06:27 -0400832 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400833 }
834 break;
835 }
Sean Middleditch29144852009-03-12 23:14:47 -0400836}
837
838/* send non-command data (escapes IAC bytes) */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400839void telnet_send(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400840 size_t size) {
841 size_t i, l;
Sean Middleditchc337ba62009-03-16 16:47:27 -0400842
Sean Middleditch29144852009-03-12 23:14:47 -0400843 for (l = i = 0; i != size; ++i) {
844 /* dump prior portion of text, send escaped bytes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400845 if (buffer[i] == TELNET_IAC) {
Sean Middleditch29144852009-03-12 23:14:47 -0400846 /* dump prior text if any */
847 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400848 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400849 l = i + 1;
850
851 /* send escape */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400852 telnet_iac(telnet, TELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400853 }
854 }
855
856 /* send whatever portion of buffer is left */
857 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400858 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400859}
860
Sean Middleditch78943842009-03-19 15:22:06 -0400861/* send subnegotiation header */
Sean Middleditch1443ca42009-03-21 00:21:04 -0400862void telnet_begin_sb(telnet_t *telnet, unsigned char telopt) {
Sean Middleditch78943842009-03-19 15:22:06 -0400863 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
864 _send(telnet, sb, 3);
865}
866
867
868/* send complete subnegotiation */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400869void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400870 const char *buffer, size_t size) {
Sean Middleditch90e79da2009-03-19 15:17:13 -0400871 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
872 static const char se[2] = { TELNET_IAC, TELNET_SE };
873
874 _send(telnet, sb, 3);
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400875 telnet_send(telnet, buffer, size);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400876 _send(telnet, se, 2);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400877
878#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400879 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
880 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400881 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400882 if (telnet->flags & TELNET_FLAG_PROXY &&
883 telopt == TELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400884
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400885 if (_init_zlib(telnet, 1, 1) != TELNET_EOK)
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400886 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400887
888 /* notify app that compression was enabled */
Sean Middleditche5327da2009-03-21 00:54:50 -0400889 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400890 }
891#endif /* HAVE_ZLIB */
Sean Middleditch29144852009-03-12 23:14:47 -0400892}
Sean Middleditch124a1c22009-03-15 13:20:03 -0400893
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400894void telnet_begin_compress2(telnet_t *telnet) {
Sean Middleditch124a1c22009-03-15 13:20:03 -0400895#ifdef HAVE_ZLIB
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400896 static const char compress2[] = { TELNET_IAC, TELNET_SB,
897 TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE };
Sean Middleditch124a1c22009-03-15 13:20:03 -0400898
Sean Middleditch124a1c22009-03-15 13:20:03 -0400899 /* attempt to create output stream first, bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400900 if (_init_zlib(telnet, 1, 0) != TELNET_EOK)
Sean Middleditch124a1c22009-03-15 13:20:03 -0400901 return;
902
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400903 /* send compression marker. we send directly to the event handler
904 * instead of passing through _send because _send would result in
905 * the compress marker itself being compressed.
906 */
Sean Middleditche5327da2009-03-21 00:54:50 -0400907 _event(telnet, TELNET_EV_SEND, 0, 0, compress2, sizeof(compress2), 0, 0);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400908
909 /* notify app that compression was successfully enabled */
Sean Middleditche5327da2009-03-21 00:54:50 -0400910 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400911#endif /* HAVE_ZLIB */
912}
Sean Middleditchd58f49f2009-03-16 12:49:35 -0400913
Sean Middleditch4a156042009-03-16 17:10:58 -0400914/* send formatted data with \r and \n translation in addition to IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400915int telnet_printf(telnet_t *telnet, const char *fmt, ...) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400916 static const char CRLF[] = { '\r', '\n' };
917 static const char CRNUL[] = { '\r', '\0' };
Sean Middleditch4a156042009-03-16 17:10:58 -0400918 char buffer[4096];
919 va_list va;
920 int rs, i, l;
921
922 /* format */
923 va_start(va, fmt);
924 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
925 va_end(va);
926
927 /* send */
928 for (l = i = 0; i != rs; ++i) {
929 /* special characters */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400930 if (buffer[i] == TELNET_IAC || buffer[i] == '\r' ||
Sean Middleditch4a156042009-03-16 17:10:58 -0400931 buffer[i] == '\n') {
932 /* dump prior portion of text */
933 if (i != l)
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400934 _send(telnet, buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -0400935 l = i + 1;
936
937 /* IAC -> IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400938 if (buffer[i] == TELNET_IAC)
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400939 telnet_iac(telnet, TELNET_IAC);
Sean Middleditch4a156042009-03-16 17:10:58 -0400940 /* automatic translation of \r -> CRNUL */
941 else if (buffer[i] == '\r')
942 _send(telnet, CRNUL, 2);
943 /* automatic translation of \n -> CRLF */
944 else if (buffer[i] == '\n')
945 _send(telnet, CRLF, 2);
946 }
947 }
948
949 /* send whatever portion of buffer is left */
950 if (i != l)
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400951 _send(telnet, buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -0400952
953 return rs;
954}
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400955
Sean Middleditch7491bf42009-03-20 23:52:18 -0400956/* send formatted data through telnet_send */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400957int telnet_printf2(telnet_t *telnet, const char *fmt, ...) {
958 char buffer[4096];
959 va_list va;
960 int rs;
961
962 /* format */
963 va_start(va, fmt);
964 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
965 va_end(va);
966
967 /* send */
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400968 telnet_send(telnet, buffer, rs);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400969
970 return rs;
971}
Sean Middleditche22b4772009-03-22 16:44:40 -0400972
973/* send ZMP data */
974void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv) {
975 size_t i;
976
977 /* ZMP header */
978 telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
979
980 /* send out each argument, including trailing NUL byte */
981 for (i = 0; i != argc; ++i)
982 telnet_send(telnet, argv[i], strlen(argv[i] + 1));
983
984 /* ZMP footer */
985 telnet_finish_sb(telnet);
986}
987
988/* send ZMP data using varargs */
989void telnet_send_zmpv(telnet_t *telnet, ...) {
990 va_list va;
991 const char* arg;
992
993 /* ZMP header */
994 telnet_begin_sb(telnet, TELNET_TELOPT_ZMP);
995
996 /* send out each argument, including trailing NUL byte */
997 va_start(va, telnet);
998 while ((arg = va_arg(va, const char *)) != NULL)
999 telnet_send(telnet, arg, strlen(arg) + 1);
1000 va_end(va);
1001
1002 /* ZMP footer */
1003 telnet_finish_sb(telnet);
1004}