blob: bd8633306191d0ed16edd38ee97520f0fec9418a [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
89 va_start(va, fmt);
90 vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
91 fmt, va);
92 va_end(va);
93
Sean Middleditchf65f27d2009-03-19 02:32:04 -040094 _event(telnet, fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING, err,
Sean Middleditch8daf7742009-03-19 02:05:24 -040095 0, (char *)buffer, strlen(buffer));
Sean Middleditchfbe93e32009-03-15 23:39:31 -040096
97 return err;
Sean Middleditchd922c6f2009-03-14 22:35:01 -040098}
99
Sean Middleditche5b47592009-03-16 17:37:43 -0400100#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400101/* initialize the zlib box for a telnet box; if deflate is non-zero, it
102 * initializes zlib for delating (compression), otherwise for inflating
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400103 * (decompression). returns TELNET_EOK on success, something else on
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400104 * failure.
Sean Middleditch72cc9642009-03-15 11:50:36 -0400105 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400106telnet_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400107 z_stream *z;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400108 int rs;
109
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400110 /* if compression is already enabled, fail loudly */
111 if (telnet->z != 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400112 return _error(telnet, __LINE__, __func__, TELNET_EBADVAL,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400113 err_fatal, "cannot initialize compression twice");
114
Sean Middleditch72cc9642009-03-15 11:50:36 -0400115 /* allocate zstream box */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400116 if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0)
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400117 return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal,
Sean Middleditch16992272009-03-15 19:42:03 -0400118 "malloc() failed: %s", strerror(errno));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400119
120 /* initialize */
121 if (deflate) {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400122 if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) {
123 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400124 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400125 err_fatal, "deflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400126 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400127 telnet->flags |= TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400128 } else {
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400129 if ((rs = inflateInit(z)) != Z_OK) {
130 free(z);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400131 return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS,
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400132 err_fatal, "inflateInit() failed: %s", zError(rs));
Sean Middleditch72cc9642009-03-15 11:50:36 -0400133 }
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400134 telnet->flags &= ~TELNET_PFLAG_DEFLATE;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400135 }
136
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400137 telnet->z = z;
138
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400139 return TELNET_EOK;
Sean Middleditch72cc9642009-03-15 11:50:36 -0400140}
Sean Middleditche5b47592009-03-16 17:37:43 -0400141#endif
Sean Middleditch72cc9642009-03-15 11:50:36 -0400142
Sean Middleditch8b788962009-03-16 01:06:27 -0400143/* push bytes out, compressing them first if need be */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400144static void _send(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400145 size_t size) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400146#ifdef HAVE_ZLIB
147 /* if we have a deflate (compression) zlib box, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400148 if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400149 char deflate_buffer[1024];
Sean Middleditch8b788962009-03-16 01:06:27 -0400150 int rs;
151
152 /* initialize z state */
Sean Middleditch97a8cb22009-03-16 16:51:41 -0400153 telnet->z->next_in = (unsigned char *)buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400154 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400155 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400156 telnet->z->avail_out = sizeof(deflate_buffer);
157
158 /* deflate until buffer exhausted and all output is produced */
159 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
160 /* compress */
161 if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400162 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch8b788962009-03-16 01:06:27 -0400163 "deflate() failed: %s", zError(rs));
164 deflateEnd(telnet->z);
165 free(telnet->z);
166 telnet->z = 0;
167 break;
168 }
169
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400170 _event(telnet, TELNET_EV_SEND, 0, 0, deflate_buffer,
Sean Middleditch8b788962009-03-16 01:06:27 -0400171 sizeof(deflate_buffer) - telnet->z->avail_out);
172
173 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400174 telnet->z->next_out = (unsigned char *)deflate_buffer;
Sean Middleditch8b788962009-03-16 01:06:27 -0400175 telnet->z->avail_out = sizeof(deflate_buffer);
176 }
177
178 /* COMPRESS2 is not negotiated, just send */
179 } else
180#endif /* HAVE_ZLIB */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400181 _event(telnet, TELNET_EV_SEND, 0, 0, buffer, size);
Sean Middleditch8b788962009-03-16 01:06:27 -0400182}
183
184/* retrieve RFC1143 option state */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400185static INLINE telnet_rfc1143_t _get_rfc1143(telnet_t *telnet,
186 unsigned char telopt) {
Sean Middleditch4987f9f2009-03-19 12:51:40 -0400187 const telnet_rfc1143_t empty = { telopt, 0, 0};
Sean Middleditch8b788962009-03-16 01:06:27 -0400188 int i;
189
190 /* search for entry */
191 for (i = 0; i != telnet->q_size; ++i)
192 if (telnet->q[i].telopt == telopt)
193 return telnet->q[i];
194
195 /* not found, return empty value */
196 return empty;
197}
198
199/* save RFC1143 option state */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400200static INLINE void _set_rfc1143(telnet_t *telnet, telnet_rfc1143_t q) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400201 telnet_rfc1143_t *qtmp;
Sean Middleditch8b788962009-03-16 01:06:27 -0400202 int i;
203
204 /* search for entry */
205 for (i = 0; i != telnet->q_size; ++i) {
206 if (telnet->q[i].telopt == q.telopt) {
207 telnet->q[i] = q;
208 return;
209 }
210 }
211
212 /* we're going to need to track state for it, so grow the queue
213 * and put the telopt into it; bail on allocation error
214 */
Sean Middleditch5dea1f72009-03-19 12:45:34 -0400215 if ((qtmp = (telnet_rfc1143_t *)realloc(telnet->q, sizeof(
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400216 telnet_rfc1143_t) * (telnet->q_size + 1))) == 0) {
217 _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0,
Sean Middleditch8b788962009-03-16 01:06:27 -0400218 "malloc() failed: %s", strerror(errno));
219 return;
220 }
221 telnet->q = qtmp;
222 telnet->q[telnet->q_size++] = q;
223}
224
Sean Middleditch90e79da2009-03-19 15:17:13 -0400225/* send negotiation bytes */
Sean Middleditch9b07e922009-03-19 18:18:44 -0400226static INLINE void _send_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400227 unsigned char telopt) {
228 char bytes[3] = { TELNET_IAC, cmd, telopt };
229 _send(telnet, bytes, 3);
230}
231
Sean Middleditch8b788962009-03-16 01:06:27 -0400232/* negotiation handling magic for RFC1143 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400233static void _negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400234 unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400235 telnet_rfc1143_t q;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400236
237 /* in PROXY mode, just pass it thru and do nothing */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400238 if (telnet->flags & TELNET_FLAG_PROXY) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400239 switch (cmd) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400240 case TELNET_WILL:
241 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400242 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400243 case TELNET_WONT:
244 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400245 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400246 case TELNET_DO:
247 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400248 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400249 case TELNET_DONT:
250 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400251 break;
252 }
253 return;
254 }
255
256 /* lookup the current state of the option */
Sean Middleditch8b788962009-03-16 01:06:27 -0400257 q = _get_rfc1143(telnet, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400258
259 /* start processing... */
260 switch (cmd) {
261 /* request to enable option on remote end or confirm DO */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400262 case TELNET_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400263 switch (q.him) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400264 case RFC1143_NO:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400265 if (_event(telnet, TELNET_EV_WILL, cmd, telopt, 0, 0) == 1) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400266 q.him = RFC1143_YES;
267 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400268 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400269 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400270 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400271 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400272 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400273 q.him = RFC1143_NO;
274 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400275 _event(telnet, TELNET_EV_WONT, cmd, telopt, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400276 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400277 "DONT answered by WILL");
278 break;
279 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400280 q.him = RFC1143_YES;
281 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400282 _event(telnet, TELNET_EV_WILL, cmd, telopt, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400283 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400284 "DONT answered by WILL");
285 break;
286 case RFC1143_WANTYES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400287 q.him = RFC1143_YES;
288 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400289 _event(telnet, TELNET_EV_WILL, cmd, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400290 break;
291 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400292 q.him = RFC1143_WANTNO;
293 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400294 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditchf7133022009-03-19 14:57:15 -0400295 _event(telnet, TELNET_EV_WILL, cmd, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400296 break;
297 }
298 break;
299
300 /* request to disable option on remote end, confirm DONT, reject DO */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400301 case TELNET_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400302 switch (q.him) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400303 case RFC1143_YES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400304 q.him = RFC1143_NO;
305 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400306 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditchf7133022009-03-19 14:57:15 -0400307 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400308 break;
309 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400310 q.him = RFC1143_NO;
311 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400312 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400313 break;
314 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400315 q.him = RFC1143_WANTYES;
316 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400317 _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400318 break;
319 case RFC1143_WANTYES:
320 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400321 q.him = RFC1143_NO;
322 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400323 break;
324 }
325 break;
326
327 /* request to enable option on local end or confirm WILL */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400328 case TELNET_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400329 switch (q.us) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400330 case RFC1143_NO:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400331 if (_event(telnet, TELNET_EV_DO, cmd, telopt, 0, 0) == 1) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400332 q.us = RFC1143_YES;
333 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400334 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400335 } else
Sean Middleditch90e79da2009-03-19 15:17:13 -0400336 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400337 break;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400338 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400339 q.us = RFC1143_NO;
340 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400341 _event(telnet, TELNET_EV_DONT, cmd, telopt, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400342 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400343 "WONT answered by DO");
344 break;
345 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400346 q.us = RFC1143_YES;
347 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400348 _event(telnet, TELNET_EV_DO, cmd, telopt, 0, 0);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400349 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400350 "WONT answered by DO");
351 break;
352 case RFC1143_WANTYES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400353 q.us = RFC1143_YES;
354 _set_rfc1143(telnet, q);
Sean Middleditchf7133022009-03-19 14:57:15 -0400355 _event(telnet, TELNET_EV_DO, cmd, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400356 break;
357 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400358 q.us = RFC1143_WANTNO;
359 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400360 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditchf7133022009-03-19 14:57:15 -0400361 _event(telnet, TELNET_EV_DO, cmd, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400362 break;
363 }
364 break;
365
366 /* request to disable option on local end, confirm WONT, reject WILL */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400367 case TELNET_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400368 switch (q.us) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400369 case RFC1143_YES:
Sean Middleditch8b788962009-03-16 01:06:27 -0400370 q.us = RFC1143_NO;
371 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400372 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400373 _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400374 break;
375 case RFC1143_WANTNO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400376 q.us = RFC1143_NO;
377 _set_rfc1143(telnet, q);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400378 _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400379 break;
380 case RFC1143_WANTNO_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400381 q.us = RFC1143_WANTYES;
382 _set_rfc1143(telnet, q);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400383 _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400384 break;
385 case RFC1143_WANTYES:
386 case RFC1143_WANTYES_OP:
Sean Middleditch8b788962009-03-16 01:06:27 -0400387 q.us = RFC1143_NO;
388 _set_rfc1143(telnet, q);
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400389 break;
390 }
391 break;
392 }
393}
394
Sean Middleditch29144852009-03-12 23:14:47 -0400395/* initialize a telnet state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400396void telnet_init(telnet_t *telnet, telnet_event_handler_t eh,
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400397 unsigned char flags, void *user_data) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400398 memset(telnet, 0, sizeof(telnet_t));
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400399 telnet->ud = user_data;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400400 telnet->eh = eh;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400401 telnet->flags = flags;
Sean Middleditch29144852009-03-12 23:14:47 -0400402}
403
404/* free up any memory allocated by a state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400405void telnet_free(telnet_t *telnet) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400406 /* free sub-request buffer */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400407 if (telnet->buffer != 0) {
Sean Middleditch29144852009-03-12 23:14:47 -0400408 free(telnet->buffer);
409 telnet->buffer = 0;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400410 telnet->buffer_size = 0;
411 telnet->buffer_pos = 0;
Sean Middleditch29144852009-03-12 23:14:47 -0400412 }
Sean Middleditch9de15982009-03-14 03:35:49 -0400413
Sean Middleditche5b47592009-03-16 17:37:43 -0400414#ifdef HAVE_ZLIB
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400415 /* free zlib box */
416 if (telnet->z != 0) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400417 if (telnet->flags & TELNET_PFLAG_DEFLATE)
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400418 deflateEnd(telnet->z);
419 else
420 inflateEnd(telnet->z);
421 free(telnet->z);
422 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400423 }
Sean Middleditche5b47592009-03-16 17:37:43 -0400424#endif
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400425
426 /* free RFC1143 queue */
427 if (telnet->q) {
428 free(telnet->q);
429 telnet->q = 0;
430 telnet->q_size = 0;
431 }
Sean Middleditch29144852009-03-12 23:14:47 -0400432}
433
Sean Middleditch51ad6792009-03-13 20:15:59 -0400434/* push a byte into the telnet buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400435static telnet_error_t _buffer_byte(telnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400436 unsigned char byte) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400437 char *new_buffer;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400438 size_t i;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400439
Sean Middleditch51ad6792009-03-13 20:15:59 -0400440 /* check if we're out of room */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400441 if (telnet->buffer_pos == telnet->buffer_size) {
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400442 /* find the next buffer size */
443 for (i = 0; i != _buffer_sizes_count; ++i) {
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400444 if (_buffer_sizes[i] == telnet->buffer_size)
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400445 break;
446 }
447
448 /* overflow -- can't grow any more */
449 if (i >= _buffer_sizes_count - 1) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400450 _error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400451 "subnegotiation buffer size limit reached");
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400452 telnet_free(telnet);
453 return TELNET_EOVERFLOW;
Sean Middleditch51ad6792009-03-13 20:15:59 -0400454 }
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400455
456 /* (re)allocate buffer */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400457 new_buffer = (char *)realloc(telnet->buffer,
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400458 _buffer_sizes[i + 1]);
459 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_DO:
530 _negotiate(telnet, TELNET_DO, byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400531 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400532 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400533 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400534 case TELNET_STATE_DONT:
535 _negotiate(telnet, TELNET_DONT, byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400536 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400537 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400538 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400539 case TELNET_STATE_WILL:
540 _negotiate(telnet, TELNET_WILL, byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400541 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400542 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400543 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400544 case TELNET_STATE_WONT:
545 _negotiate(telnet, TELNET_WONT, byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400546 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400547 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400548 break;
549
Sean Middleditchda0e6952009-03-15 13:28:09 -0400550 /* subnegotiation -- determine subnegotiation telopt */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400551 case TELNET_STATE_SB:
Sean Middleditchda0e6952009-03-15 13:28:09 -0400552 telnet->sb_telopt = byte;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400553 telnet->buffer_pos = 0;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400554 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditchda0e6952009-03-15 13:28:09 -0400555 break;
556
557 /* subnegotiation -- buffer bytes until end request */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400558 case TELNET_STATE_SB_DATA:
Sean Middleditch6b372882009-03-14 13:06:47 -0400559 /* IAC command in subnegotiation -- either IAC SE or IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400560 if (byte == TELNET_IAC) {
561 telnet->state = TELNET_STATE_SB_DATA_IAC;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400562 /* buffer the byte, or bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400563 } else if (_buffer_byte(telnet, byte) != TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400564 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400565 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400566 }
567 break;
568
Sean Middleditch6b372882009-03-14 13:06:47 -0400569 /* IAC escaping inside a subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400570 case TELNET_STATE_SB_DATA_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400571 switch (byte) {
Sean Middleditch6b372882009-03-14 13:06:47 -0400572 /* end subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400573 case TELNET_SE:
Sean Middleditch9de15982009-03-14 03:35:49 -0400574 /* return to default state */
575 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400576 telnet->state = TELNET_STATE_DATA;
Sean Middleditch9de15982009-03-14 03:35:49 -0400577
Sean Middleditch9de15982009-03-14 03:35:49 -0400578 /* invoke callback */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400579 _event(telnet, TELNET_EV_SUBNEGOTIATION, 0,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400580 telnet->sb_telopt, telnet->buffer, telnet->buffer_pos);
Sean Middleditch9de15982009-03-14 03:35:49 -0400581
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400582#ifdef HAVE_ZLIB
Sean Middleditch3df17f92009-03-16 01:12:16 -0400583 /* received COMPRESS2 begin marker, setup our zlib box and
584 * start handling the compressed stream if it's not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400585 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400586 if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) {
587 if (_init_zlib(telnet, 0, 1) != TELNET_EOK)
Sean Middleditch9de15982009-03-14 03:35:49 -0400588 break;
Sean Middleditch9de15982009-03-14 03:35:49 -0400589
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400590 /* notify app that compression was enabled */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400591 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400592
Sean Middleditch9de15982009-03-14 03:35:49 -0400593 /* any remaining bytes in the buffer are compressed.
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400594 * we have to re-invoke telnet_push to get those
Sean Middleditch9de15982009-03-14 03:35:49 -0400595 * bytes inflated and abort trying to process the
596 * remaining compressed bytes in the current _process
597 * buffer argument
598 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400599 telnet_push(telnet, &buffer[start], size - start);
Sean Middleditch9de15982009-03-14 03:35:49 -0400600 return;
601 }
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400602#endif /* HAVE_ZLIB */
603
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400604 break;
605 /* escaped IAC byte */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400606 case TELNET_IAC:
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400607 /* push IAC into buffer */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400608 if (_buffer_byte(telnet, TELNET_IAC) !=
609 TELNET_EOK) {
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400610 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400611 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400612 } else {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400613 telnet->state = TELNET_STATE_SB_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400614 }
615 break;
616 /* something else -- protocol error */
617 default:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400618 _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400619 "unexpected byte after IAC inside SB: %d",
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400620 byte);
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400621 start = i + 1;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400622 telnet->state = TELNET_STATE_DATA;
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400623 break;
624 }
625 break;
626 }
627 }
628
629 /* pass through any remaining bytes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400630 if (telnet->state == TELNET_STATE_DATA && i != start)
631 _event(telnet, TELNET_EV_DATA, 0, 0, buffer + start, i - start);
Sean Middleditch29144852009-03-12 23:14:47 -0400632}
633
Sean Middleditch9de15982009-03-14 03:35:49 -0400634/* push a bytes into the state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400635void telnet_push(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400636 size_t size) {
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400637#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400638 /* if we have an inflate (decompression) zlib stream, use it */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400639 if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400640 char inflate_buffer[4096];
Sean Middleditch9de15982009-03-14 03:35:49 -0400641 int rs;
642
643 /* initialize zlib state */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400644 telnet->z->next_in = (unsigned char*)buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400645 telnet->z->avail_in = size;
Sean Middleditch8daf7742009-03-19 02:05:24 -0400646 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400647 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400648
649 /* inflate until buffer exhausted and all output is produced */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400650 while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
Sean Middleditch9de15982009-03-14 03:35:49 -0400651 /* reset output buffer */
652
653 /* decompress */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400654 rs = inflate(telnet->z, Z_SYNC_FLUSH);
Sean Middleditch9de15982009-03-14 03:35:49 -0400655
656 /* process the decompressed bytes on success */
657 if (rs == Z_OK || rs == Z_STREAM_END)
658 _process(telnet, inflate_buffer, sizeof(inflate_buffer) -
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400659 telnet->z->avail_out);
Sean Middleditch9de15982009-03-14 03:35:49 -0400660 else
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400661 _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
Sean Middleditch16992272009-03-15 19:42:03 -0400662 "inflate() failed: %s", zError(rs));
Sean Middleditch9de15982009-03-14 03:35:49 -0400663
664 /* prepare output buffer for next run */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400665 telnet->z->next_out = (unsigned char *)inflate_buffer;
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400666 telnet->z->avail_out = sizeof(inflate_buffer);
Sean Middleditch9de15982009-03-14 03:35:49 -0400667
668 /* on error (or on end of stream) disable further inflation */
669 if (rs != Z_OK) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400670 _event(telnet, TELNET_EV_COMPRESS, 0, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400671
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400672 inflateEnd(telnet->z);
673 free(telnet->z);
674 telnet->z = 0;
Sean Middleditch9de15982009-03-14 03:35:49 -0400675 break;
676 }
677 }
678
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400679 /* COMPRESS2 is not negotiated, just process */
680 } else
681#endif /* HAVE_ZLIB */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400682 _process(telnet, buffer, size);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400683}
684
Sean Middleditch29144852009-03-12 23:14:47 -0400685/* send an iac command */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400686void telnet_send_command(telnet_t *telnet, unsigned char cmd) {
687 char bytes[2] = { TELNET_IAC, cmd };
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400688 _send(telnet, bytes, 2);
Sean Middleditch29144852009-03-12 23:14:47 -0400689}
690
691/* send negotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400692void telnet_send_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch8b788962009-03-16 01:06:27 -0400693 unsigned char telopt) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400694 telnet_rfc1143_t q;
Sean Middleditch8b788962009-03-16 01:06:27 -0400695
696 /* if we're in proxy mode, just send it now */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400697 if (telnet->flags & TELNET_FLAG_PROXY) {
698 char bytes[3] = { TELNET_IAC, cmd, telopt };
Sean Middleditch8b788962009-03-16 01:06:27 -0400699 _send(telnet, bytes, 3);
700 return;
701 }
702
703 /* get current option states */
704 q = _get_rfc1143(telnet, telopt);
705
706 switch (cmd) {
707 /* advertise willingess to support an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400708 case TELNET_WILL:
Sean Middleditch8b788962009-03-16 01:06:27 -0400709 switch (q.us) {
710 case RFC1143_NO:
711 q.us = RFC1143_WANTYES;
712 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400713 _send_negotiate(telnet, TELNET_WILL, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400714 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400715 case RFC1143_WANTNO:
716 q.us = RFC1143_WANTNO_OP;
717 _set_rfc1143(telnet, q);
718 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400719 case RFC1143_WANTYES_OP:
720 q.us = RFC1143_WANTYES;
721 _set_rfc1143(telnet, q);
722 break;
723 }
724 break;
725
726 /* force turn-off of locally enabled option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400727 case TELNET_WONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400728 switch (q.us) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400729 case RFC1143_YES:
730 q.us = RFC1143_WANTNO;
731 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400732 _send_negotiate(telnet, TELNET_WONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400733 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400734 case RFC1143_WANTYES:
735 q.us = RFC1143_WANTYES_OP;
736 _set_rfc1143(telnet, q);
737 break;
738 case RFC1143_WANTNO_OP:
739 q.us = RFC1143_WANTNO;
740 _set_rfc1143(telnet, q);
741 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400742 }
743 break;
744
745 /* ask remote end to enable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400746 case TELNET_DO:
Sean Middleditch8b788962009-03-16 01:06:27 -0400747 switch (q.him) {
748 case RFC1143_NO:
749 q.him = RFC1143_WANTYES;
750 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400751 _send_negotiate(telnet, TELNET_DO, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400752 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400753 case RFC1143_WANTNO:
754 q.him = RFC1143_WANTNO_OP;
755 _set_rfc1143(telnet, q);
756 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400757 case RFC1143_WANTYES_OP:
758 q.him = RFC1143_WANTYES;
759 _set_rfc1143(telnet, q);
760 break;
761 }
762 break;
763
764 /* demand remote end disable an option */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400765 case TELNET_DONT:
Sean Middleditch8b788962009-03-16 01:06:27 -0400766 switch (q.him) {
Sean Middleditch8b788962009-03-16 01:06:27 -0400767 case RFC1143_YES:
768 q.him = RFC1143_WANTNO;
769 _set_rfc1143(telnet, q);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400770 _send_negotiate(telnet, TELNET_DONT, telopt);
Sean Middleditch8b788962009-03-16 01:06:27 -0400771 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400772 case RFC1143_WANTYES:
773 q.him = RFC1143_WANTYES_OP;
774 _set_rfc1143(telnet, q);
775 break;
776 case RFC1143_WANTNO_OP:
777 q.him = RFC1143_WANTNO;
778 _set_rfc1143(telnet, q);
779 break;
Sean Middleditch8b788962009-03-16 01:06:27 -0400780 }
781 break;
782 }
Sean Middleditch29144852009-03-12 23:14:47 -0400783}
784
785/* send non-command data (escapes IAC bytes) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400786void telnet_send_data(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400787 size_t size) {
788 size_t i, l;
Sean Middleditchc337ba62009-03-16 16:47:27 -0400789
Sean Middleditch29144852009-03-12 23:14:47 -0400790 for (l = i = 0; i != size; ++i) {
791 /* dump prior portion of text, send escaped bytes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400792 if (buffer[i] == TELNET_IAC) {
Sean Middleditch29144852009-03-12 23:14:47 -0400793 /* dump prior text if any */
794 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400795 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400796 l = i + 1;
797
798 /* send escape */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400799 telnet_send_command(telnet, TELNET_IAC);
Sean Middleditch29144852009-03-12 23:14:47 -0400800 }
801 }
802
803 /* send whatever portion of buffer is left */
804 if (i != l)
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400805 _send(telnet, buffer + l, i - l);
Sean Middleditch29144852009-03-12 23:14:47 -0400806}
807
Sean Middleditch78943842009-03-19 15:22:06 -0400808/* send subnegotiation header */
809void telnet_begin_subnegotiation(telnet_t *telnet, unsigned char telopt) {
810 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
811 _send(telnet, sb, 3);
812}
813
814
815/* send complete subnegotiation */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400816void telnet_send_subnegotiation(telnet_t *telnet, unsigned char telopt,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400817 const char *buffer, size_t size) {
Sean Middleditch90e79da2009-03-19 15:17:13 -0400818 const char sb[3] = { TELNET_IAC, TELNET_SB, telopt };
819 static const char se[2] = { TELNET_IAC, TELNET_SE };
820
821 _send(telnet, sb, 3);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400822 telnet_send_data(telnet, buffer, size);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400823 _send(telnet, se, 2);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400824
825#ifdef HAVE_ZLIB
Sean Middleditch72cc9642009-03-15 11:50:36 -0400826 /* if we're a proxy and we just sent the COMPRESS2 marker, we must
827 * make sure all further data is compressed if not already.
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400828 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400829 if (telnet->flags & TELNET_FLAG_PROXY &&
830 telopt == TELNET_TELOPT_COMPRESS2) {
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400831
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400832 if (_init_zlib(telnet, 1, 1) != TELNET_EOK)
Sean Middleditchd922c6f2009-03-14 22:35:01 -0400833 return;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400834
835 /* notify app that compression was enabled */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400836 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400837 }
838#endif /* HAVE_ZLIB */
Sean Middleditch29144852009-03-12 23:14:47 -0400839}
Sean Middleditch124a1c22009-03-15 13:20:03 -0400840
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400841void telnet_begin_compress2(telnet_t *telnet) {
Sean Middleditch124a1c22009-03-15 13:20:03 -0400842#ifdef HAVE_ZLIB
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400843 static const char compress2[] = { TELNET_IAC, TELNET_SB,
844 TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE };
Sean Middleditch124a1c22009-03-15 13:20:03 -0400845
Sean Middleditch124a1c22009-03-15 13:20:03 -0400846 /* attempt to create output stream first, bail if we can't */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400847 if (_init_zlib(telnet, 1, 0) != TELNET_EOK)
Sean Middleditch124a1c22009-03-15 13:20:03 -0400848 return;
849
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400850 /* send compression marker. we send directly to the event handler
851 * instead of passing through _send because _send would result in
852 * the compress marker itself being compressed.
853 */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400854 _event(telnet, TELNET_EV_SEND, 0, 0, compress2, sizeof(compress2));
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400855
856 /* notify app that compression was successfully enabled */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400857 _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400858#endif /* HAVE_ZLIB */
859}
Sean Middleditchd58f49f2009-03-16 12:49:35 -0400860
Sean Middleditch4a156042009-03-16 17:10:58 -0400861/* send formatted data with \r and \n translation in addition to IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400862int telnet_printf(telnet_t *telnet, const char *fmt, ...) {
Sean Middleditch8daf7742009-03-19 02:05:24 -0400863 static const char CRLF[] = { '\r', '\n' };
864 static const char CRNUL[] = { '\r', '\0' };
Sean Middleditch4a156042009-03-16 17:10:58 -0400865 char buffer[4096];
866 va_list va;
867 int rs, i, l;
868
869 /* format */
870 va_start(va, fmt);
871 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
872 va_end(va);
873
874 /* send */
875 for (l = i = 0; i != rs; ++i) {
876 /* special characters */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400877 if (buffer[i] == TELNET_IAC || buffer[i] == '\r' ||
Sean Middleditch4a156042009-03-16 17:10:58 -0400878 buffer[i] == '\n') {
879 /* dump prior portion of text */
880 if (i != l)
Sean Middleditch8daf7742009-03-19 02:05:24 -0400881 _send(telnet, (char *)buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -0400882 l = i + 1;
883
884 /* IAC -> IAC IAC */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400885 if (buffer[i] == TELNET_IAC)
886 telnet_send_command(telnet, TELNET_IAC);
Sean Middleditch4a156042009-03-16 17:10:58 -0400887 /* automatic translation of \r -> CRNUL */
888 else if (buffer[i] == '\r')
889 _send(telnet, CRNUL, 2);
890 /* automatic translation of \n -> CRLF */
891 else if (buffer[i] == '\n')
892 _send(telnet, CRLF, 2);
893 }
894 }
895
896 /* send whatever portion of buffer is left */
897 if (i != l)
Sean Middleditch8daf7742009-03-19 02:05:24 -0400898 _send(telnet, (char *)buffer + l, i - l);
Sean Middleditch4a156042009-03-16 17:10:58 -0400899
900 return rs;
901}
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400902
903/* send formatted data through telnet_send_data */
904int telnet_printf2(telnet_t *telnet, const char *fmt, ...) {
905 char buffer[4096];
906 va_list va;
907 int rs;
908
909 /* format */
910 va_start(va, fmt);
911 rs = vsnprintf(buffer, sizeof(buffer), fmt, va);
912 va_end(va);
913
914 /* send */
915 telnet_send_data(telnet, (char *)buffer, rs);
916
917 return rs;
918}