blob: 8478803d1c29e59aa8f10bd251d0f2d818a63395 [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 */
39#define RFC1143_NO 0x00
40#define RFC1143_YES 0x01
41
42#define RFC1143_WANT 0x02
43#define RFC1143_OP 0x04
44
45#define RFC1143_WANTNO (RFC1143_WANT|RFC1143_YES)
46#define RFC1143_WANTYES (RFC1143_WANT|RFC1143_NO)
47#define RFC1143_WANTNO_OP (RFC1143_WANTNO|RFC1143_OP)
48#define RFC1143_WANTYES_OP (RFC1143_WANTYES|RFC1143_OP)
49
Sean Middleditch4d9444d2009-03-13 22:48:05 -040050/* buffer sizes */
Sean Middleditch340a51b2009-03-19 02:08:46 -040051static const size_t _buffer_sizes[] = {
Sean Middleditch4d9444d2009-03-13 22:48:05 -040052 0,
53 512,
54 2048,
55 8192,
56 16384,
57};
Sean Middleditch340a51b2009-03-19 02:08:46 -040058static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) /
Sean Middleditch812358d2009-03-15 23:24:03 -040059 sizeof(_buffer_sizes[0]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -040060
Sean Middleditch5b5bc922009-03-15 23:02:10 -040061/* event dispatch helper; return value is value of the accept field of the
62 * event struct after dispatch; used for the funky REQUEST event */
Sean Middleditch9b07e922009-03-19 18:18:44 -040063static INLINE int _event(telnet_t *telnet, telnet_event_type_t type,
Sean Middleditch97a8cb22009-03-16 16:51:41 -040064 unsigned char command, unsigned char telopt,
Sean Middleditch340a51b2009-03-19 02:08:46 -040065 const char *buffer, size_t size) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -040066 telnet_event_t ev;
Sean Middleditch5b5bc922009-03-15 23:02:10 -040067 ev.buffer = buffer;
68 ev.size = size;
Sean Middleditch637df7f2009-03-15 12:57:32 -040069 ev.type = type;
70 ev.command = command;
71 ev.telopt = telopt;
Sean Middleditch5b5bc922009-03-15 23:02:10 -040072 ev.accept = 0;
Sean Middleditch637df7f2009-03-15 12:57:32 -040073
Sean Middleditch9f79cc52009-03-15 13:39:24 -040074 telnet->eh(telnet, &ev, telnet->ud);
Sean Middleditch5b5bc922009-03-15 23:02:10 -040075
76 return ev.accept;
Sean Middleditch637df7f2009-03-15 12:57:32 -040077}
78
Sean Middleditchd922c6f2009-03-14 22:35:01 -040079/* error generation function */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040080static telnet_error_t _error(telnet_t *telnet, unsigned line,
81 const char* func, telnet_error_t err, int fatal, const char *fmt,
Sean Middleditchfbe93e32009-03-15 23:39:31 -040082 ...) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -040083 char buffer[512];
84 va_list va;
85
86 /* format error intro */
Sean Middleditch812358d2009-03-15 23:24:03 -040087 snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func);
Sean Middleditchd922c6f2009-03-14 22:35:01 -040088
Sean Middleditchb43c10c2009-03-20 23:21:02 -040089 /* format informational text */
Sean Middleditchd922c6f2009-03-14 22:35:01 -040090 va_start(va, fmt);
91 vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
92 fmt, va);
93 va_end(va);
94
Sean Middleditchb43c10c2009-03-20 23:21:02 -040095 /* send error event to the user */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040096 _event(telnet, fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING, err,
Sean Middleditchb43c10c2009-03-20 23:21:02 -040097 0, buffer, strlen(buffer));
Sean Middleditchfbe93e32009-03-15 23:39:31 -040098
99 return err;
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400100}
101
Sean Middleditche5b47592009-03-16 17:37:43 -0400102#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400103/* initialize the zlib box for a telnet box; if deflate is non-zero, it
104 * initializes zlib for delating (compression), otherwise for inflating
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400105 * (decompression). returns TELNET_EOK on success, something else on
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400106 * failure.
Sean Middleditch72cc9642009-03-15 11:50:36 -0400107 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400108telnet_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400109 z_stream *z;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400110 int rs;
111
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400112 /* if compression is already enabled, fail loudly */
113 if (telnet->z != 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400114 return _error(telnet, __LINE__, __func__, TELNET_EBADVAL,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400115 err_fatal, "cannot initialize compression twice");
116
Sean Middleditch72cc9642009-03-15 11:50:36 -0400117 /* allocate zstream box */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400118 if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400119 return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal,
Sean Middleditch16992272009-03-15 19:42:03 -0400120 "malloc() failed: %s", strerror(errno));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400121
122 /* initialize */
123 if (deflate) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400124 if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) {
125 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400126 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400127 err_fatal, "deflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400128 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400129 telnet->flags |= TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400130 } else {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400131 if ((rs = inflateInit(z)) != Z_OK) {
132 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400133 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400134 err_fatal, "inflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400135 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400136 telnet->flags &= ~TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400137 }
138
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400139 telnet->z = z;
140
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400141 return TELNET_EOK;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400142}
Sean Middleditche5b47592009-03-16 17:37:43 -0400143#endif
Sean Middleditch72cc9642009-03-15 11:50:36 -0400144
Sean Middleditch8b788962009-03-16 01:06:27 -0400145/* push bytes out, compressing them first if need be */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400146static void _send(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400147 size_t size) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400148#ifdef HAVE_ZLIB
149 /* if we have a deflate (compression) zlib box, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400150 if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400151 char deflate_buffer[1024];
Sean Middleditch8b788962009-03-16 01:06:27 -0400152 int rs;
153
154 /* initialize z state */
Sean Middleditch97a8cb22009-03-16 16:51:41 -0400155 telnet->z->next_in = (unsigned char *)buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400156 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400157 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400158 telnet->z->avail_out = sizeof(deflate_buffer);
159
160 /* deflate until buffer exhausted and all output is produced */
161 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
162 /* compress */
163 if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400164 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch8b788962009-03-16 01:06:27 -0400165 "deflate() failed: %s", zError(rs));
166 deflateEnd(telnet->z);
167 free(telnet->z);
168 telnet->z = 0;
169 break;
170 }
171
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400172 _event(telnet, TELNET_EV_SEND, 0, 0, deflate_buffer,
Sean Middleditch8b788962009-03-16 01:06:27 -0400173 sizeof(deflate_buffer) - telnet->z->avail_out);
174
175 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400176 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400177 telnet->z->avail_out = sizeof(deflate_buffer);
178 }
179
180 /* COMPRESS2 is not negotiated, just send */
181 } else
182#endif /* HAVE_ZLIB */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400183 _event(telnet, TELNET_EV_SEND, 0, 0, buffer, size);
Sean Middleditch8b788962009-03-16 01:06:27 -0400184}
185
186/* retrieve RFC1143 option state */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400187static INLINE telnet_rfc1143_t _get_rfc1143(telnet_t *telnet,
188 unsigned char telopt) {
Sean Middleditch4987f9f2009-03-19 12:51:40 -0400189 const telnet_rfc1143_t empty = { telopt, 0, 0};
Sean Middleditch8b788962009-03-16 01:06:27 -0400190 int i;
191
192 /* search for entry */
193 for (i = 0; i != telnet->q_size; ++i)
194 if (telnet->q[i].telopt == telopt)
195 return telnet->q[i];
196
197 /* not found, return empty value */
198 return empty;
199}
200
201/* save RFC1143 option state */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400202static INLINE void _set_rfc1143(telnet_t *telnet, telnet_rfc1143_t q) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400203 telnet_rfc1143_t *qtmp;
Sean Middleditch8b788962009-03-16 01:06:27 -0400204 int i;
205
206 /* search for entry */
207 for (i = 0; i != telnet->q_size; ++i) {
208 if (telnet->q[i].telopt == q.telopt) {
209 telnet->q[i] = q;
210 return;
211 }
212 }
213
214 /* we're going to need to track state for it, so grow the queue
215 * and put the telopt into it; bail on allocation error
216 */
Sean Middleditch5dea1f72009-03-19 12:45:34 -0400217 if ((qtmp = (telnet_rfc1143_t *)realloc(telnet->q, sizeof(
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400218 telnet_rfc1143_t) * (telnet->q_size + 1))) == 0) {
219 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch8b788962009-03-16 01:06:27 -0400220 "malloc() failed: %s", strerror(errno));
221 return;
222 }
223 telnet->q = qtmp;
224 telnet->q[telnet->q_size++] = q;
225}
226
Sean Middleditch90e79da2009-03-19 15:17:13 -0400227/* send negotiation bytes */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400228static INLINE void _send_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400229 unsigned char telopt) {
230 char bytes[3] = { TELNET_IAC, cmd, telopt };
231 _send(telnet, bytes, 3);
232}
233
Sean Middleditch8b788962009-03-16 01:06:27 -0400234/* negotiation handling magic for RFC1143 */
Sean Middleditch330c2612009-03-20 23:29:17 -0400235static void _negotiate(telnet_t *telnet, unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400236 telnet_rfc1143_t q;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400237
238 /* in PROXY mode, just pass it thru and do nothing */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400239 if (telnet->flags & TELNET_FLAG_PROXY) {
Sean Middleditch330c2612009-03-20 23:29:17 -0400240 switch ((int)telnet->state) {
241 case TELNET_STATE_WILL:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400242 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400243 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400244 case TELNET_STATE_WONT:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400245 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400246 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400247 case TELNET_STATE_DO:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400248 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400249 break;
Sean Middleditch330c2612009-03-20 23:29:17 -0400250 case TELNET_STATE_DONT:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400251 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400252 break;
253 }
254 return;
255 }
256
257 /* lookup the current state of the option */
Sean Middleditch8b788962009-03-16 01:06:27 -0400258 q = _get_rfc1143(telnet, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400259
260 /* start processing... */
Sean Middleditch330c2612009-03-20 23:29:17 -0400261 switch ((int)telnet->state) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400262 /* request to enable option on remote end or confirm DO */
Sean Middleditch330c2612009-03-20 23:29:17 -0400263 case TELNET_STATE_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400264 switch (q.him) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400265 case RFC1143_NO:
Sean Middleditch330c2612009-03-20 23:29:17 -0400266 if (_event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0) == 1) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400267 q.him = RFC1143_YES;
268 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400269 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400270 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400271 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400272 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400273 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400274 q.him = RFC1143_NO;
275 _set_rfc1143(telnet, q);
Sean Middleditch330c2612009-03-20 23:29:17 -0400276 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400277 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400278 "DONT answered by WILL");
279 break;
280 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400281 q.him = RFC1143_YES;
282 _set_rfc1143(telnet, q);
Sean Middleditch330c2612009-03-20 23:29:17 -0400283 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400284 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400285 "DONT answered by WILL");
286 break;
287 case RFC1143_WANTYES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400288 q.him = RFC1143_YES;
289 _set_rfc1143(telnet, q);
Sean Middleditch330c2612009-03-20 23:29:17 -0400290 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400291 break;
292 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400293 q.him = RFC1143_WANTNO;
294 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400295 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch330c2612009-03-20 23:29:17 -0400296 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400297 break;
298 }
299 break;
300
301 /* request to disable option on remote end, confirm DONT, reject DO */
Sean Middleditch330c2612009-03-20 23:29:17 -0400302 case TELNET_STATE_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400303 switch (q.him) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400304 case RFC1143_YES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400305 q.him = RFC1143_NO;
306 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400307 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditchf7133022009-03-19 14:57:15 -0400308 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400309 break;
310 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400311 q.him = RFC1143_NO;
312 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400313 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400314 break;
315 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400316 q.him = RFC1143_WANTYES;
317 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400318 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400319 break;
320 case RFC1143_WANTYES:
321 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400322 q.him = RFC1143_NO;
323 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400324 break;
325 }
326 break;
327
328 /* request to enable option on local end or confirm WILL */
Sean Middleditch330c2612009-03-20 23:29:17 -0400329 case TELNET_STATE_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400330 switch (q.us) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400331 case RFC1143_NO:
Sean Middleditch330c2612009-03-20 23:29:17 -0400332 if (_event(telnet, TELNET_EV_DO, 0, telopt, 0, 0) == 1) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400333 q.us = RFC1143_YES;
334 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400335 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400336 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400337 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400338 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400339 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400340 q.us = RFC1143_NO;
341 _set_rfc1143(telnet, q);
Sean Middleditch330c2612009-03-20 23:29:17 -0400342 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400343 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400344 "WONT answered by DO");
345 break;
346 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400347 q.us = RFC1143_YES;
348 _set_rfc1143(telnet, q);
Sean Middleditch330c2612009-03-20 23:29:17 -0400349 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400350 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400351 "WONT answered by DO");
352 break;
353 case RFC1143_WANTYES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400354 q.us = RFC1143_YES;
355 _set_rfc1143(telnet, q);
Sean Middleditch330c2612009-03-20 23:29:17 -0400356 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400357 break;
358 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400359 q.us = RFC1143_WANTNO;
360 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400361 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch330c2612009-03-20 23:29:17 -0400362 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400363 break;
364 }
365 break;
366
367 /* request to disable option on local end, confirm WONT, reject WILL */
Sean Middleditch330c2612009-03-20 23:29:17 -0400368 case TELNET_STATE_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400369 switch (q.us) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400370 case RFC1143_YES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400371 q.us = RFC1143_NO;
372 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400373 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400374 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400375 break;
376 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400377 q.us = RFC1143_NO;
378 _set_rfc1143(telnet, q);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400379 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400380 break;
381 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400382 q.us = RFC1143_WANTYES;
383 _set_rfc1143(telnet, q);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400384 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400385 break;
386 case RFC1143_WANTYES:
387 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400388 q.us = RFC1143_NO;
389 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400390 break;
391 }
392 break;
393 }
394}
395
Sean Middleditch29144852009-03-12 23:14:47 -0400396/* initialize a telnet state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400397void telnet_init(telnet_t *telnet, telnet_event_handler_t eh,
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400398 unsigned char flags, void *user_data) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400399 memset(telnet, 0, sizeof(telnet_t));
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400400 telnet->ud = user_data;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400401 telnet->eh = eh;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400402 telnet->flags = flags;
Sean Middleditch29144852009-03-12 23:14:47 -0400403}
404
405/* free up any memory allocated by a state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400406void telnet_free(telnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400407 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400408 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400409 free(telnet->buffer);
410 telnet->buffer = 0;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400411 telnet->buffer_size = 0;
412 telnet->buffer_pos = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400413 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400414
Sean Middleditche5b47592009-03-16 17:37:43 -0400415#ifdef HAVE_ZLIB
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400416 /* free zlib box */
417 if (telnet->z != 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400418 if (telnet->flags & TELNET_PFLAG_DEFLATE)
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400419 deflateEnd(telnet->z);
420 else
421 inflateEnd(telnet->z);
422 free(telnet->z);
423 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400424 }
Sean Middleditche5b47592009-03-16 17:37:43 -0400425#endif
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400426
427 /* free RFC1143 queue */
428 if (telnet->q) {
429 free(telnet->q);
430 telnet->q = 0;
431 telnet->q_size = 0;
432 }
Sean Middleditch29144852009-03-12 23:14:47 -0400433}
434
Sean Middleditch51ad6792009-03-13 20:15:59 -0400435/* push a byte into the telnet buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400436static telnet_error_t _buffer_byte(telnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400437 unsigned char byte) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400438 char *new_buffer;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400439 size_t i;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400440
Sean Middleditch51ad6792009-03-13 20:15:59 -0400441 /* check if we're out of room */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400442 if (telnet->buffer_pos == telnet->buffer_size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400443 /* find the next buffer size */
444 for (i = 0; i != _buffer_sizes_count; ++i) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400445 if (_buffer_sizes[i] == telnet->buffer_size)
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400446 break;
447 }
448
449 /* overflow -- can't grow any more */
450 if (i >= _buffer_sizes_count - 1) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400451 _error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400452 "subnegotiation buffer size limit reached");
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400453 telnet_free(telnet);
454 return TELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400455 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400456
457 /* (re)allocate buffer */
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400458 new_buffer = (char *)realloc(telnet->buffer, _buffer_sizes[i + 1]);
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400459 if (new_buffer == 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400460 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch16992272009-03-15 19:42:03 -0400461 "realloc() failed");
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400462 telnet_free(telnet);
463 return TELNET_ENOMEM;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400464 }
465
466 telnet->buffer = new_buffer;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400467 telnet->buffer_size = _buffer_sizes[i + 1];
Sean Middleditch51ad6792009-03-13 20:15:59 -0400468 }
469
470 /* push the byte, all set */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400471 telnet->buffer[telnet->buffer_pos++] = byte;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400472 return TELNET_EOK;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400473}
474
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400475static void _process(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400476 size_t size) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400477 unsigned char byte;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400478 size_t i, start;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400479 for (i = start = 0; i != size; ++i) {
480 byte = buffer[i];
481 switch (telnet->state) {
482 /* regular data */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400483 case TELNET_STATE_DATA:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400484 /* on an IAC byte, pass through all pending bytes and
485 * switch states */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400486 if (byte == TELNET_IAC) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400487 if (i != start)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400488 _event(telnet, TELNET_EV_DATA, 0, 0, &buffer[start],
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400489 i - start);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400490 telnet->state = TELNET_STATE_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400491 }
492 break;
493
494 /* IAC command */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400495 case TELNET_STATE_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400496 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400497 /* subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400498 case TELNET_SB:
499 telnet->state = TELNET_STATE_SB;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400500 break;
501 /* negotiation commands */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400502 case TELNET_WILL:
503 telnet->state = TELNET_STATE_WILL;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400504 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400505 case TELNET_WONT:
506 telnet->state = TELNET_STATE_WONT;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400507 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400508 case TELNET_DO:
509 telnet->state = TELNET_STATE_DO;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400510 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400511 case TELNET_DONT:
512 telnet->state = TELNET_STATE_DONT;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400513 break;
514 /* IAC escaping */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400515 case TELNET_IAC:
516 _event(telnet, TELNET_EV_DATA, 0, 0, (char*)&byte, 1);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400517 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400518 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400519 break;
520 /* some other command */
521 default:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400522 _event(telnet, TELNET_EV_IAC, byte, 0, 0, 0);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400523 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400524 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400525 }
526 break;
527
528 /* negotiation commands */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400529 case TELNET_STATE_WILL:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400530 case TELNET_STATE_WONT:
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400531 case TELNET_STATE_DO:
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400532 case TELNET_STATE_DONT:
Sean Middleditch330c2612009-03-20 23:29:17 -0400533 _negotiate(telnet, byte);
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400534 start = i + 1;
535 telnet->state = TELNET_STATE_DATA;
536 break;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400537
Sean Middleditchda0e6952009-03-15 13:28:09 -0400538 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400539 case TELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400540 telnet->sb_telopt = byte;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400541 telnet->buffer_pos = 0;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400542 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditchda0e6952009-03-15 13:28:09 -0400543 break;
544
545 /* subnegotiation -- buffer bytes until end request */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400546 case TELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400547 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400548 if (byte == TELNET_IAC) {
549 telnet->state = TELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400550 /* buffer the byte, or bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400551 } else if (_buffer_byte(telnet, byte) != TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400552 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400553 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400554 }
555 break;
556
Sean Middleditch6b372882009-03-14 13:06:47 -0400557 /* IAC escaping inside a subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400558 case TELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400559 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400560 /* end subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400561 case TELNET_SE:
Sean Middleditch9de15982009-03-14 03:35:49 -0400562 /* return to default state */
563 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400564 telnet->state = TELNET_STATE_DATA;
Sean Middleditch9de15982009-03-14 03:35:49 -0400565
Sean Middleditch9de15982009-03-14 03:35:49 -0400566 /* invoke callback */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400567 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400568 telnet->sb_telopt, telnet->buffer, telnet->buffer_pos);
Sean Middleditch9de15982009-03-14 03:35:49 -0400569
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400570#ifdef HAVE_ZLIB
Sean Middleditch3df17f92009-03-16 01:12:16 -0400571 /* received COMPRESS2 begin marker, setup our zlib box and
572 * start handling the compressed stream if it's not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400573 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400574 if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) {
575 if (_init_zlib(telnet, 0, 1) != TELNET_EOK)
Sean Middleditch9de15982009-03-14 03:35:49 -0400576 break;
Sean Middleditch9de15982009-03-14 03:35:49 -0400577
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400578 /* notify app that compression was enabled */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400579 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400580
Sean Middleditch9de15982009-03-14 03:35:49 -0400581 /* any remaining bytes in the buffer are compressed.
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400582 * we have to re-invoke telnet_recv to get those
Sean Middleditch9de15982009-03-14 03:35:49 -0400583 * bytes inflated and abort trying to process the
584 * remaining compressed bytes in the current _process
585 * buffer argument
586 */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400587 telnet_recv(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400588 return;
589 }
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400590#endif /* HAVE_ZLIB */
591
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400592 break;
593 /* escaped IAC byte */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400594 case TELNET_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400595 /* push IAC into buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400596 if (_buffer_byte(telnet, TELNET_IAC) !=
597 TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400598 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400599 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400600 } else {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400601 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400602 }
603 break;
604 /* something else -- protocol error */
605 default:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400606 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400607 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400608 byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400609 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400610 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400611 break;
612 }
613 break;
614 }
615 }
616
617 /* pass through any remaining bytes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400618 if (telnet->state == TELNET_STATE_DATA && i != start)
619 _event(telnet, TELNET_EV_DATA, 0, 0, buffer + start, i - start);
Sean Middleditch29144852009-03-12 23:14:47 -0400620}
621
Sean Middleditch9de15982009-03-14 03:35:49 -0400622/* push a bytes into the state tracker */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400623void telnet_recv(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400624 size_t size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400625#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400626 /* if we have an inflate (decompression) zlib stream, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400627 if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400628 char inflate_buffer[4096];
Sean Middleditch9de15982009-03-14 03:35:49 -0400629 int rs;
630
631 /* initialize zlib state */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400632 telnet->z->next_in = (unsigned char*)buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400633 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400634 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400635 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400636
637 /* inflate until buffer exhausted and all output is produced */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400638 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400639 /* reset output buffer */
640
641 /* decompress */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400642 rs = inflate(telnet->z, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400643
644 /* process the decompressed bytes on success */
645 if (rs == Z_OK || rs == Z_STREAM_END)
646 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400647 telnet->z->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400648 else
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400649 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch16992272009-03-15 19:42:03 -0400650 "inflate() failed: %s", zError(rs));
Sean Middleditch9de15982009-03-14 03:35:49 -0400651
652 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400653 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400654 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400655
656 /* on error (or on end of stream) disable further inflation */
657 if (rs != Z_OK) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400658 _event(telnet, TELNET_EV_COMPRESS, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400659
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400660 inflateEnd(telnet->z);
661 free(telnet->z);
662 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400663 break;
664 }
665 }
666
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400667 /* COMPRESS2 is not negotiated, just process */
668 } else
669#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400670 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400671}
672
Sean Middleditch29144852009-03-12 23:14:47 -0400673/* send an iac command */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400674void telnet_iac(telnet_t *telnet, unsigned char cmd) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400675 char bytes[2] = { TELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400676 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400677}
678
679/* send negotiation */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400680void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch8b788962009-03-16 01:06:27 -0400681 unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400682 telnet_rfc1143_t q;
Sean Middleditch8b788962009-03-16 01:06:27 -0400683
684 /* if we're in proxy mode, just send it now */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400685 if (telnet->flags & TELNET_FLAG_PROXY) {
686 char bytes[3] = { TELNET_IAC, cmd, telopt };
Sean Middleditch8b788962009-03-16 01:06:27 -0400687 _send(telnet, bytes, 3);
688 return;
689 }
690
691 /* get current option states */
692 q = _get_rfc1143(telnet, telopt);
693
694 switch (cmd) {
695 /* advertise willingess to support an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400696 case TELNET_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400697 switch (q.us) {
698 case RFC1143_NO:
699 q.us = RFC1143_WANTYES;
700 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400701 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400702 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400703 case RFC1143_WANTNO:
704 q.us = RFC1143_WANTNO_OP;
705 _set_rfc1143(telnet, q);
706 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400707 case RFC1143_WANTYES_OP:
708 q.us = RFC1143_WANTYES;
709 _set_rfc1143(telnet, q);
710 break;
711 }
712 break;
713
714 /* force turn-off of locally enabled option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400715 case TELNET_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400716 switch (q.us) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400717 case RFC1143_YES:
718 q.us = RFC1143_WANTNO;
719 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400720 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400721 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400722 case RFC1143_WANTYES:
723 q.us = RFC1143_WANTYES_OP;
724 _set_rfc1143(telnet, q);
725 break;
726 case RFC1143_WANTNO_OP:
727 q.us = RFC1143_WANTNO;
728 _set_rfc1143(telnet, q);
729 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400730 }
731 break;
732
733 /* ask remote end to enable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400734 case TELNET_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400735 switch (q.him) {
736 case RFC1143_NO:
737 q.him = RFC1143_WANTYES;
738 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400739 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400740 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400741 case RFC1143_WANTNO:
742 q.him = RFC1143_WANTNO_OP;
743 _set_rfc1143(telnet, q);
744 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400745 case RFC1143_WANTYES_OP:
746 q.him = RFC1143_WANTYES;
747 _set_rfc1143(telnet, q);
748 break;
749 }
750 break;
751
752 /* demand remote end disable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400753 case TELNET_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400754 switch (q.him) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400755 case RFC1143_YES:
756 q.him = RFC1143_WANTNO;
757 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400758 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400759 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400760 case RFC1143_WANTYES:
761 q.him = RFC1143_WANTYES_OP;
762 _set_rfc1143(telnet, q);
763 break;
764 case RFC1143_WANTNO_OP:
765 q.him = RFC1143_WANTNO;
766 _set_rfc1143(telnet, q);
767 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400768 }
769 break;
770 }
Sean Middleditch29144852009-03-12 23:14:47 -0400771}
772
773/* send non-command data (escapes IAC bytes) */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400774void telnet_send(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400775 size_t size) {
776 size_t i, l;
Sean Middleditchc337ba62009-03-16 16:47:27 -0400777
Sean Middleditch29144852009-03-12 23:14:47 -0400778 for (l = i = 0; i != size; ++i) {
779 /* dump prior portion of text, send escaped bytes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400780 if (buffer[i] == TELNET_IAC) {
Sean Middleditch29144852009-03-12 23:14:47 -0400781 /* dump prior text if any */
782 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400783 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400784 l = i + 1;
785
786 /* send escape */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400787 telnet_iac(telnet, TELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400788 }
789 }
790
791 /* send whatever portion of buffer is left */
792 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400793 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400794}
795
Sean Middleditch78943842009-03-19 15:22:06 -0400796/* send subnegotiation header */
797void telnet_begin_subnegotiation(telnet_t *telnet, unsigned char telopt) {
798 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
799 _send(telnet, sb, 3);
800}
801
802
803/* send complete subnegotiation */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400804void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400805 const char *buffer, size_t size) {
Sean Middleditch90e79da2009-03-19 15:17:13 -0400806 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
807 static const char se[2] = { TELNET_IAC, TELNET_SE };
808
809 _send(telnet, sb, 3);
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400810 telnet_send(telnet, buffer, size);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400811 _send(telnet, se, 2);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400812
813#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400814 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
815 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400816 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400817 if (telnet->flags & TELNET_FLAG_PROXY &&
818 telopt == TELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400819
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400820 if (_init_zlib(telnet, 1, 1) != TELNET_EOK)
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400821 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400822
823 /* notify app that compression was enabled */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400824 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400825 }
826#endif /* HAVE_ZLIB */
Sean Middleditch29144852009-03-12 23:14:47 -0400827}
Sean Middleditch124a1c22009-03-15 13:20:03 -0400828
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400829void telnet_begin_compress2(telnet_t *telnet) {
Sean Middleditch124a1c22009-03-15 13:20:03 -0400830#ifdef HAVE_ZLIB
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400831 static const char compress2[] = { TELNET_IAC, TELNET_SB,
832 TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE };
Sean Middleditch124a1c22009-03-15 13:20:03 -0400833
Sean Middleditch124a1c22009-03-15 13:20:03 -0400834 /* attempt to create output stream first, bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400835 if (_init_zlib(telnet, 1, 0) != TELNET_EOK)
Sean Middleditch124a1c22009-03-15 13:20:03 -0400836 return;
837
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400838 /* send compression marker. we send directly to the event handler
839 * instead of passing through _send because _send would result in
840 * the compress marker itself being compressed.
841 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400842 _event(telnet, TELNET_EV_SEND, 0, 0, compress2, sizeof(compress2));
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400843
844 /* notify app that compression was successfully enabled */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400845 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400846#endif /* HAVE_ZLIB */
847}
Sean Middleditchd58f49f2009-03-16 12:49:35 -0400848
Sean Middleditch4a156042009-03-16 17:10:58 -0400849/* send formatted data with \r and \n translation in addition to IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400850int telnet_printf(telnet_t *telnet, const char *fmt, ...) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400851 static const char CRLF[] = { '\r', '\n' };
852 static const char CRNUL[] = { '\r', '\0' };
Sean Middleditch4a156042009-03-16 17:10:58 -0400853 char buffer[4096];
854 va_list va;
855 int rs, i, l;
856
857 /* format */
858 va_start(va, fmt);
859 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
860 va_end(va);
861
862 /* send */
863 for (l = i = 0; i != rs; ++i) {
864 /* special characters */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400865 if (buffer[i] == TELNET_IAC || buffer[i] == '\r' ||
Sean Middleditch4a156042009-03-16 17:10:58 -0400866 buffer[i] == '\n') {
867 /* dump prior portion of text */
868 if (i != l)
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400869 _send(telnet, buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -0400870 l = i + 1;
871
872 /* IAC -> IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400873 if (buffer[i] == TELNET_IAC)
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400874 telnet_iac(telnet, TELNET_IAC);
Sean Middleditch4a156042009-03-16 17:10:58 -0400875 /* automatic translation of \r -> CRNUL */
876 else if (buffer[i] == '\r')
877 _send(telnet, CRNUL, 2);
878 /* automatic translation of \n -> CRLF */
879 else if (buffer[i] == '\n')
880 _send(telnet, CRLF, 2);
881 }
882 }
883
884 /* send whatever portion of buffer is left */
885 if (i != l)
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400886 _send(telnet, buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -0400887
888 return rs;
889}
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400890
891/* send formatted data through telnet_send_data */
892int telnet_printf2(telnet_t *telnet, const char *fmt, ...) {
893 char buffer[4096];
894 va_list va;
895 int rs;
896
897 /* format */
898 va_start(va, fmt);
899 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
900 va_end(va);
901
902 /* send */
Sean Middleditchb43c10c2009-03-20 23:21:02 -0400903 telnet_send(telnet, buffer, rs);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400904
905 return rs;
906}