Debian Codegraph @

contrib/mod_deflate.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_deflate -- a module for supporting on-the-fly compression
3 * Copyright (c) 2004-2026 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 *
24 * This is mod_deflate, contrib software for proftpd 1.3.x and above.
25 * For more information contact TJ Saunders <[email protected]>.
26 *
27 * $Libraries: -lz$
28 */
29
30 #include <zlib.h>
31
32 #include "conf.h"
33 #include "privs.h"
34
35 #define MOD_DEFLATE_VERSION "mod_deflate/0.6"
36
37 /* Make sure the version of proftpd is as necessary. */
38 #if PROFTPD_VERSION_NUMBER < 0x0001030604
39 # error "ProFTPD 1.3.6 or later required"
40 #endif
41
42 module deflate_module;
43
44 static int deflate_sess_init(void);
45
46 static int deflate_enabled = FALSE;
47 static int deflate_engine = FALSE;
48 static int deflate_logfd = -1;
49 static pr_netio_t *deflate_netio = NULL;
50 static pr_netio_t *deflate_next_netio = NULL;
51
52 /* These are for tracking the callbacks of the next NetIO, if any, that we
53 * override, for restoring later via OPTS.
54 */
55 static int (*deflate_next_netio_close)(pr_netio_stream_t *) = NULL;
56 static pr_netio_stream_t *(*deflate_next_netio_open)(pr_netio_stream_t *, int, int) = NULL;
57 static int (*deflate_next_netio_read)(pr_netio_stream_t *, char *, size_t) = NULL;
58 static int (*deflate_next_netio_shutdown)(pr_netio_stream_t *, int) = NULL;
59 static int (*deflate_next_netio_write)(pr_netio_stream_t *, char *, size_t) = NULL;
60
61 /* Draft-recommended ZLIB defaults:
62 *
63 * The following ZLIB [5] parameters are recommended for deflate
64 * transmission mode:
65 *
66 * Compression level: 7
67 * Compression method: Z_DEFLATED
68 * Window bits: -15
69 * Memory level: 8
70 * Strategy: Z_DEFAULT_STRATEGY
71 */
72
73 #define MOD_DEFLATE_DEFAULT_COMPRESS_LEVEL 7
74 static int deflate_compression_level = MOD_DEFLATE_DEFAULT_COMPRESS_LEVEL;
75
76 #define MOD_DEFLATE_DEFAULT_MEM_LEVEL 8
77 static int deflate_mem_level = MOD_DEFLATE_DEFAULT_MEM_LEVEL;
78
79 #define MOD_DEFLATE_DEFAULT_STRATEGY Z_DEFAULT_STRATEGY
80 static int deflate_strategy = MOD_DEFLATE_DEFAULT_STRATEGY;
81
82 #define MOD_DEFLATE_DEFAULT_WINDOW_BITS 15
83 static int deflate_window_bits = MOD_DEFLATE_DEFAULT_WINDOW_BITS;
84
85 /* The _ptr pointer always points to the start of the buffer; the _zbuf
86 * pointer points to the current place within the buffer from which to read
87 * data.
88 */
89 static Byte *deflate_zbuf_ptr = NULL;
90 static Byte *deflate_zbuf = NULL;
91 static size_t deflate_zbuflen = 0;
92 static size_t deflate_zbufsz = 0;
93
94 static Byte *deflate_rbuf = NULL;
95 static size_t deflate_rbuflen = 0;
96 static size_t deflate_rbufsz = 0;
97
98 #define DEFLATE_NETIO_NOTE "mod_deflate.z_stream"
99 static int deflate_zerrno = 0;
100
101 static const char *trace_channel = "deflate";
102
103 static const char *deflate_zstrerror(int zerrno) {
104 const char *zstr = "unknown";
105
106 switch (zerrno) {
107 case Z_OK:
108 zstr = "OK";
109 break;
110
111 case Z_STREAM_END:
112 return "End of stream";
113 break;
114
115 case Z_NEED_DICT:
116 return "Need dictionary";
117 break;
118
119 case Z_ERRNO:
120 zstr = strerror(errno);
121 break;
122
123 case Z_DATA_ERROR:
124 zstr = "Data error";
125 break;
126
127 case Z_MEM_ERROR:
128 zstr = "Memory error";
129 break;
130
131 case Z_BUF_ERROR:
132 zstr = "Buffer error";
133 break;
134
135 case Z_VERSION_ERROR:
136 zstr = "Version error";
137 break;
138 }
139
140 return zstr;
141 }
142
143 /* NetIO callbacks
144 */
145
146 static int deflate_netio_close_cb(pr_netio_stream_t *nstrm) {
147 int res = 0;
148
149 if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
150 z_stream *zstrm;
151
152 zstrm = (z_stream *) pr_table_get(nstrm->notes, DEFLATE_NETIO_NOTE, NULL);
153 if (zstrm == NULL) {
154 int xerrno = 0;
155
156 res = 0;
157
158 if (deflate_next_netio_close != NULL) {
159 res = (deflate_next_netio_close)(nstrm);
160 xerrno = errno;
161
162 if (res < 0) {
163 pr_trace_msg(trace_channel, 1, "error calling next netio close: %s",
164 strerror(xerrno));
165 }
166 }
167
168 errno = xerrno;
169 return res;
170 }
171
172 if (nstrm->strm_mode == PR_NETIO_IO_WR) {
173 if (zstrm->total_in > 0) {
174 float ratio;
175
176 ratio = ((float) zstrm->total_out / (float) zstrm->total_in);
177
178 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
179 "%s: deflated %lu bytes to %lu bytes (%0.2lf%% compression)",
180 session.curr_cmd, zstrm->total_in, zstrm->total_out,
181 (1.0 - ratio) * 100.0);
182 }
183
184 res = deflateEnd(zstrm);
185 if (res != Z_OK) {
186 pr_trace_msg(trace_channel, 3,
187 "close: error closing deflating netio: [%d] %s", res,
188 zstrm->msg ? zstrm->msg : deflate_zstrerror(res));
189
190 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
191 "error closing deflating netio: [%d] %s", res,
192 zstrm->msg ? zstrm->msg : deflate_zstrerror(res));
193 }
194
195 } else if (nstrm->strm_mode == PR_NETIO_IO_RD) {
196 if (zstrm->total_in > 0) {
197 float ratio;
198
199 ratio = ((float) zstrm->total_in / (float) zstrm->total_out);
200
201 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
202 "%s: inflated %lu bytes to %lu bytes (%0.2lf%% compression)",
203 session.curr_cmd, zstrm->total_in, zstrm->total_out,
204 (1.0 - ratio) * 100.0);
205 }
206
207 res = inflateEnd(zstrm);
208 if (res != Z_OK) {
209 pr_trace_msg(trace_channel, 3,
210 "close: error closing inflating netio: [%d] %s", res,
211 zstrm->msg ? zstrm->msg : deflate_zstrerror(res));
212
213 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
214 "error closing inflating netio: [%d] %s", res,
215 zstrm->msg ? zstrm->msg : deflate_zstrerror(res));
216 }
217 }
218
219 if (deflate_next_netio == NULL) {
220 res = close(nstrm->strm_fd);
221 nstrm->strm_fd = -1;
222 }
223
224 pr_table_remove(nstrm->notes, DEFLATE_NETIO_NOTE, NULL);
225 }
226
227 if (deflate_next_netio_close != NULL) {
228 if ((deflate_next_netio_close)(nstrm) < 0) {
229 pr_trace_msg(trace_channel, 1, "error calling next netio close: %s",
230 strerror(errno));
231 }
232 }
233
234 return res;
235 }
236
237 static pr_netio_stream_t *deflate_netio_open_cb(pr_netio_stream_t *nstrm,
238 int fd, int mode) {
239
240 nstrm->strm_fd = fd;
241 nstrm->strm_mode = mode;
242
243 if (deflate_next_netio_open != NULL) {
244 if ((deflate_next_netio_open)(nstrm, fd, mode) == NULL) {
245 int xerrno = errno;
246
247 pr_trace_msg(trace_channel, 1, "error calling next netio open: %s",
248 strerror(xerrno));
249 errno = xerrno;
250 return NULL;
251 }
252 }
253
254 if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
255 int res;
256 z_stream *zstrm;
257
258 /* Set the initial ZLIB parameters. */
259 zstrm = pcalloc(nstrm->strm_pool, sizeof(z_stream));
260 zstrm->zalloc = Z_NULL;
261 zstrm->zfree = Z_NULL;
262 zstrm->opaque = Z_NULL;
263 zstrm->next_in = Z_NULL;
264 zstrm->next_out = Z_NULL;
265 zstrm->avail_in = 0;
266 zstrm->avail_out = 0;
267
268 if (pr_table_add(nstrm->notes,
269 pstrdup(nstrm->strm_pool, DEFLATE_NETIO_NOTE), zstrm,
270 sizeof(z_stream *)) < 0) {
271 if (errno != EEXIST) {
272 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
273 "error stashing '%s' note: %s", DEFLATE_NETIO_NOTE, strerror(errno));
274 }
275 }
276
277 memset(deflate_zbuf_ptr, '\0', deflate_zbufsz);
278 deflate_zbuf = deflate_zbuf_ptr;
279
280 if (nstrm->strm_mode == PR_NETIO_IO_WR) {
281 /* Initialize the zlib data for deflation. */
282 res = deflateInit2(zstrm, deflate_compression_level, Z_DEFLATED,
283 deflate_window_bits, deflate_mem_level, deflate_strategy);
284
285 switch (res) {
286 case Z_OK:
287 zstrm->next_out = deflate_zbuf;
288 zstrm->avail_out = deflate_zbufsz;
289 break;
290
291 case Z_MEM_ERROR:
292 case Z_STREAM_ERROR:
293 pr_trace_msg(trace_channel, 3,
294 "open: error initializing for deflation: [%d] %s", res,
295 zstrm->msg ? zstrm->msg : deflate_zstrerror(res));
296
297 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
298 "error initializing for deflation: [%d] %s", res,
299 zstrm->msg ? zstrm->msg : deflate_zstrerror(res));
300
301 errno = EINVAL;
302 return NULL;
303 }
304
305 } else if (nstrm->strm_mode == PR_NETIO_IO_RD) {
306 /* Initialize the zlib data for inflation.
307 *
308 * The magic number 32 here from the zlib.h documentation; it enables
309 * the automatic header detection of zlib/gzip headers.
310 */
311 res = inflateInit2(zstrm, deflate_window_bits + 32);
312
313 switch (res) {
314 case Z_OK:
315 zstrm->next_out = deflate_zbuf;
316 zstrm->avail_out = deflate_zbufsz;
317 break;
318
319 case Z_MEM_ERROR:
320 case Z_STREAM_ERROR:
321 pr_trace_msg(trace_channel, 3,
322 "open: error initializing for inflation: [%d] %s", res,
323 zstrm->msg ? zstrm->msg : deflate_zstrerror(res));
324
325 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
326 "error initializing for inflation: [%d] %s", res,
327 zstrm->msg ? zstrm->msg : deflate_zstrerror(res));
328
329 errno = EINVAL;
330 return NULL;
331 }
332
333 /* These are used by the read callback; ensure they are initialised to
334 * zero before every read data transfer.
335 */
336 deflate_rbuflen = 0;
337 deflate_zbuflen = 0;
338 }
339 }
340
341 return nstrm;
342 }
343
344 static int deflate_netio_read_cb(pr_netio_stream_t *nstrm, char *buf,
345 size_t bufsz) {
346
347 if (bufsz == 0) {
348 return 0;
349 }
350
351 if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
352 int datalen = 0, nread = 0, res, xerrno;
353 z_stream *zstrm;
354
355 zstrm = (z_stream *) pr_table_get(nstrm->notes, DEFLATE_NETIO_NOTE, NULL);
356 if (zstrm == NULL) {
357 pr_trace_msg(trace_channel, 2,
358 "no zstream found in stream data for reading");
359 errno = EIO;
360 return -1;
361 }
362
363 res = 0;
364
365 /* If we have data leftover in deflate_zbuf, start by copying all of that
366 * into the provided buffer. Only read more data from the network and
367 * inflate it when there's no leftover data.
368 */
369
370 if (deflate_zbuflen > 0) {
371 if (bufsz >= deflate_zbuflen) {
372 /* Excellent. We can consume all of the data in the deflate_zbuf
373 * buffer.
374 */
375
376 pr_trace_msg(trace_channel, 9, "read: returning %lu bytes of "
377 "previously uncompressed data; no data read from client",
378 (unsigned long) deflate_zbuflen);
379
380 memcpy(buf, deflate_zbuf, deflate_zbuflen);
381 res = deflate_zbuflen;
382
383 /* Reset the pointer to the start of the buffer. */
384 deflate_zbuf = deflate_zbuf_ptr;
385 deflate_zbuflen = 0;
386
387 /* Manually adjust the "raw" bytes in counter, so that it will
388 * be accurate for %I logging.
389 *
390 * We subtract the number we are returning here, since our return
391 * value will simply be added back to the counter in pr_netio_read().
392 * And if our subtraction causes an underflow, it's still OK since
393 * the subsequent addition will overflow, and get the value back to
394 * what it should be.
395 */
396 session.total_raw_in -= res;
397
398 return res;
399 }
400
401 /* The given buffer can't hold all of our already-inflated data; but
402 * maybe it can hold some of it?
403 */
404
405 pr_trace_msg(trace_channel, 9, "read: returning %lu bytes of previously "
406 "uncompressed data (of %lu bytes total); no data read from client",
407 (unsigned long) bufsz, (unsigned long) deflate_zbuflen);
408
409 memcpy(buf, deflate_zbuf, bufsz);
410 res = bufsz;
411
412 deflate_zbuf += bufsz;
413 deflate_zbuflen -= bufsz;
414
415 /* Manually adjust the "raw" bytes in counter, so that it will
416 * be accurate for %I logging.
417 *
418 * We subtract the number we are returning here, since our return
419 * value will simply be added back to the counter in pr_netio_read().
420 * And if our subtraction causes an underflow, it's still OK since
421 * the subsequent addition will overflow, and get the value back to
422 * what it should be.
423 */
424 session.total_raw_in -= res;
425
426 return res;
427 }
428
429 /* If we reach this point, then the deflate_zbuf buffer is empty of
430 * uncompressed data. We might have some compressed data left over from
431 * the previous inflate() call that we need to process
432 * (i.e. zstrm->avail_in > 0), though; if so, process that until
433 * zstrm->avail_in = 0 BEFORE reading any more data from the network.
434 *
435 * After that, try to read more deta in from the network. If we get no
436 * data, AND zstrm->avail_in is zero, then we've reached EOF. Otherwise,
437 * add the new data to the inflator, and see if we can make some progress.
438 */
439
440 if (zstrm->avail_in == 0) {
441 /* We're ready for more data from the network. */
442 datalen = deflate_rbufsz - deflate_rbuflen;
443
444 if (deflate_next_netio_read != NULL) {
445 nread = (deflate_next_netio_read)(nstrm, (char *) deflate_rbuf,
446 datalen);
447
448 } else {
449 /* Read in some data from the stream's fd. */
450 nread = read(nstrm->strm_fd, deflate_rbuf, datalen);
451 }
452
453 if (nread < 0) {
454 xerrno = errno;
455
456 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
457 "error reading from socket %d: %s", nstrm->strm_fd, strerror(xerrno));
458
459 errno = xerrno;
460 return -1;
461 }
462
463 if (nread == 0) {
464 /* EOF. We know we can return zero here because the deflate_zbuf
465 * is empty (see above comment), and we haven't read any more data
466 * in from the network.
467 */
468 pr_trace_msg(trace_channel, 8,
469 "read: read EOF from client, returning 0");
470 return 0;
471 }
472
473 pr_trace_msg(trace_channel, 9,
474 "read: read %d bytes of compressed data from client", nread);
475
476 /* Manually adjust the "raw" bytes in counter, so that it will
477 * be accurate for %I logging.
478 */
479 session.total_raw_in += nread;
480
481 datalen = nread;
482 zstrm->next_in = deflate_rbuf;
483 zstrm->avail_in = datalen;
484
485 } else {
486 pr_trace_msg(trace_channel, 9,
487 "read: processing %d bytes of leftover compressed data from client",
488 zstrm->avail_in);
489 }
490
491 zstrm->next_out = deflate_zbuf;
492 zstrm->avail_out = deflate_zbufsz;
493
494 pr_trace_msg(trace_channel, 19,
495 "read: pre-inflate zstream state: avail_in = %d, avail_out = %d",
496 zstrm->avail_in, zstrm->avail_out);
497
498 deflate_zerrno = inflate(zstrm, Z_SYNC_FLUSH);
499 xerrno = errno;
500
501 pr_trace_msg(trace_channel, 19,
502 "read: post-inflate zstream state: avail_in = %d, avail_out = %d "
503 "(zerrno = %s)", zstrm->avail_in, zstrm->avail_out,
504 deflate_zstrerror(deflate_zerrno));
505
506 errno = xerrno;
507
508 switch (deflate_zerrno) {
509 case Z_OK:
510 case Z_STREAM_END:
511 break;
512
513 default:
514 pr_trace_msg(trace_channel, 3,
515 "read: error inflating %lu bytes of data: [%d] %s: %s",
516 (unsigned long) datalen, deflate_zerrno,
517 deflate_zstrerror(deflate_zerrno),
518 zstrm->msg ? zstrm->msg : "unavailable");
519
520 errno = xerrno;
521 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
522 "error inflating %lu bytes of data: [%d] %s",
523 (unsigned long) datalen, deflate_zerrno,
524 zstrm->msg ? zstrm->msg : deflate_zstrerror(deflate_zerrno));
525
526 errno = EIO;
527 return -1;
528 }
529
530 deflate_zbuflen = deflate_zbufsz - zstrm->avail_out;
531
532 /* Now all we have to do is return EAGAIN, so that the FSIO API calls
533 * us back immediately. That will hit the check for deflate_zbuflen
534 * earlier in this function, and return the data we just decompressed.
535 */
536 errno = EAGAIN;
537 return -1;
538 }
539
540 return read(nstrm->strm_fd, buf, bufsz);
541 }
542
543 static int deflate_netio_shutdown_cb(pr_netio_stream_t *nstrm, int how) {
544
545 if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
546 int res = 0;
547 z_stream *zstrm;
548
549 zstrm = (z_stream *) pr_table_get(nstrm->notes, DEFLATE_NETIO_NOTE, NULL);
550 if (zstrm == NULL) {
551 return 0;
552 }
553
554 if (nstrm->strm_mode == PR_NETIO_IO_WR) {
555 zstrm->next_in = Z_NULL;
556 zstrm->avail_in = 0;
557
558 pr_trace_msg(trace_channel, 19,
559 "shutdown: pre-deflate zstream state: avail_in = %d, avail_out = %d",
560 zstrm->avail_in, zstrm->avail_out);
561
562 deflate_zerrno = deflate(zstrm, Z_FINISH);
563
564 pr_trace_msg(trace_channel, 19,
565 "shutdown: post-inflate zstream state: avail_in = %d, avail_out = %d "
566 "(zerrno = %s)", zstrm->avail_in, zstrm->avail_out,
567 deflate_zstrerror(deflate_zerrno));
568
569 if (deflate_zerrno != Z_OK &&
570 deflate_zerrno != Z_STREAM_END) {
571 pr_trace_msg(trace_channel, 3,
572 "shutdown: error deflating data: [%d] %s: %s", deflate_zerrno,
573 deflate_zstrerror(deflate_zerrno),
574 zstrm->msg ? zstrm->msg : "unavailable");
575
576 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
577 "error deflating data: [%d] %s", deflate_zerrno,
578 zstrm->msg ? zstrm->msg : deflate_zstrerror(deflate_zerrno));
579
580 } else {
581 size_t datalen, offset;
582
583 datalen = deflate_zbufsz - zstrm->avail_out;
584 offset = 0;
585
586 while (datalen > 0) {
587 if (deflate_next_netio_write != NULL) {
588 res = (deflate_next_netio_write)(nstrm,
589 (char *) (deflate_zbuf + offset), datalen);
590
591 } else {
592 res = write(nstrm->strm_fd, deflate_zbuf + offset, datalen);
593 }
594
595 if (res < 0) {
596 if (errno == EINTR ||
597 errno == EAGAIN) {
598 /* The socket might be busy, especially if the peer is a bit
599 * slow in reading data from it.
600 */
601 pr_signals_handle();
602 continue;
603 }
604
605 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
606 "error writing to socket %d: %s", nstrm->strm_fd,
607 strerror(errno));
608 return -1;
609 }
610
611 /* Manually update the "raw" bytes counter, so that it will be
612 * accurate for %O logging.
613 */
614 session.total_raw_out += res;
615
616 /* Watch out for short writes. */
617 if ((size_t) res == datalen) {
618 break;
619 }
620
621 offset += res;
622 datalen -= res;
623 }
624 }
625
626 if (deflate_next_netio_shutdown != NULL) {
627 res = (deflate_next_netio_shutdown)(nstrm, how);
628
629 } else {
630 res = shutdown(nstrm->strm_fd, how);
631 }
632
633 return res;
634 }
635 }
636
637 return shutdown(nstrm->strm_fd, how);
638 }
639
640 static int deflate_netio_write_cb(pr_netio_stream_t *nstrm, char *buf,
641 size_t buflen) {
642
643 if (buflen == 0) {
644 return 0;
645 }
646
647 if (nstrm->strm_type == PR_NETIO_STRM_DATA) {
648 int res = 0, xerrno;
649 size_t datalen, offset = 0;
650 z_stream *zstrm;
651
652 zstrm = (z_stream *) pr_table_get(nstrm->notes, DEFLATE_NETIO_NOTE, NULL);
653 if (zstrm == NULL) {
654 pr_trace_msg(trace_channel, 2,
655 "no zstream found in stream data for writing");
656 errno = EIO;
657 return -1;
658 }
659
660 /* Deflate the data to be written out. */
661 zstrm->next_in = (Bytef *) buf;
662 zstrm->avail_in = buflen;
663
664 pr_trace_msg(trace_channel, 19,
665 "write: pre-deflate zstream state: avail_in = %d, avail_out = %d",
666 zstrm->avail_in, zstrm->avail_out);
667
668 deflate_zerrno = deflate(zstrm, Z_SYNC_FLUSH);
669 xerrno = errno;
670
671 pr_trace_msg(trace_channel, 19,
672 "write: post-inflate zstream state: avail_in = %d, avail_out = %d "
673 "(zerrno = %s)", zstrm->avail_in, zstrm->avail_out,
674 deflate_zstrerror(deflate_zerrno));
675
676 errno = xerrno;
677
678 if (deflate_zerrno != Z_OK) {
679 pr_trace_msg(trace_channel, 3, "write: error deflating data: [%d] %s: %s",
680 deflate_zerrno, deflate_zstrerror(deflate_zerrno),
681 zstrm->msg ? zstrm->msg : "unavailable");
682
683 errno = xerrno;
684
685 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
686 "error deflating data: [%d] %s", deflate_zerrno,
687 zstrm->msg ? zstrm->msg : deflate_zstrerror(deflate_zerrno));
688
689 errno = EIO;
690 return -1;
691 }
692
693 datalen = deflate_zbufsz - zstrm->avail_out;
694
695 while (datalen > 0) {
696 pr_signals_handle();
697
698 if (deflate_next_netio_write != NULL) {
699 res = (deflate_next_netio_write)(nstrm,
700 (char *) (deflate_zbuf + offset), datalen);
701
702 } else {
703 res = write(nstrm->strm_fd, deflate_zbuf + offset, datalen);
704 }
705
706 if (res < 0) {
707 if (errno == EINTR ||
708 errno == EAGAIN) {
709 /* The socket might be busy, especially if the peer is a bit
710 * slow in reading data from it.
711 */
712 pr_signals_handle();
713 continue;
714 }
715
716 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
717 "error writing to socket %d: %s", nstrm->strm_fd, strerror(errno));
718 return -1;
719 }
720
721 /* Manually adjust the "raw" bytes counter, so that it will be
722 * accurate for %O logging.
723 */
724 session.total_raw_out += res;
725
726 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
727 "wrote %d (of %lu) bytes of compressed of data to socket %d", res,
728 (unsigned long) datalen, nstrm->strm_fd);
729
730 /* Watch out for short writes */
731 if ((size_t) res == datalen) {
732 zstrm->next_out = deflate_zbuf;
733 zstrm->avail_out = deflate_zbufsz;
734 break;
735
736 } else {
737 offset += res;
738 datalen -= res;
739 }
740 }
741
742 /* Manually adjust the "raw" bytes in counter, so that it will
743 * be accurate for %O logging.
744 *
745 * We subtract the number we are returning here, since our return
746 * value will simply be added back to the counter in pr_netio_write().
747 * And if our subtraction causes an underflow, it's still OK since
748 * the subsequent addition will overflow, and get the value back to
749 * what it should be.
750 */
751
752 res = (buflen - zstrm->avail_in);
753 session.total_raw_out -= res;
754
755 pr_trace_msg(trace_channel, 9, "write: returning %d for %lu bytes",
756 res, (unsigned long) buflen);
757 return res;
758 }
759
760 return write(nstrm->strm_fd, buf, buflen);
761 }
762
763 /* Configuration handlers
764 */
765
766 /* usage: DeflateEngine on|off */
767 MODRET set_deflateengine(cmd_rec *cmd) {
768 int engine;
769 config_rec *c;
770
771 CHECK_ARGS(cmd, 1);
772 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
773
774 engine = get_boolean(cmd, 1);
775 if (engine == -1) {
776 CONF_ERROR(cmd, "expected Boolean parameter");
777 }
778
779 c = add_config_param(cmd->argv[0], 1, NULL);
780 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
781 *((unsigned int *) c->argv[0]) = engine;
782
783 return PR_HANDLED(cmd);
784 }
785
786 /* usage: DeflateLog path|"none" */
787 MODRET set_deflatelog(cmd_rec *cmd) {
788 char *path;
789
790 CHECK_ARGS(cmd, 1);
791 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
792
793 path = cmd->argv[1];
794 if (pr_fs_valid_path(path) < 0) {
795 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": ", path, " is not a valid path",
796 NULL));
797 }
798
799 add_config_param_str(cmd->argv[0], 1, path);
800 return PR_HANDLED(cmd);
801 }
802
803 /* Command handlers
804 */
805
806 MODRET deflate_opts(cmd_rec *cmd) {
807 if (deflate_engine == FALSE) {
808 return PR_DECLINED(cmd);
809 }
810
811 if (cmd->argc < 3) {
812 return PR_DECLINED(cmd);
813 }
814
815 if (strcasecmp(cmd->argv[1], "Z") == 0) {
816
817 /* Twiddle the requested ZLIB parameters. */
818
819 if (cmd->argc == 3) {
820 /* If no key/value pairs were given, reset the deflate parameters
821 * to their default settings.
822 */
823 deflate_compression_level = MOD_DEFLATE_DEFAULT_COMPRESS_LEVEL;
824 deflate_mem_level = MOD_DEFLATE_DEFAULT_MEM_LEVEL;
825 deflate_strategy = MOD_DEFLATE_DEFAULT_STRATEGY;
826 deflate_window_bits = MOD_DEFLATE_DEFAULT_WINDOW_BITS;
827
828 pr_response_add(R_200, _("%s OK"), (char *) cmd->argv[0]);
829 return PR_HANDLED(cmd);
830
831 } else {
832 register unsigned int i;
833
834 if (cmd->argc % 2 != 0) {
835 pr_response_add_err(R_501, _("Bad number of parameters"));
836
837 pr_cmd_set_errno(cmd, EINVAL);
838 errno = EINVAL;
839 return PR_ERROR(cmd);
840 }
841
842 for (i = 2; i < cmd->argc; i += 2) {
843 char *key, *val;
844
845 key = cmd->argv[i];
846 val = cmd->argv[i+1];
847
848 if (strcasecmp(key, "blocksize") == 0 ||
849 strcasecmp(key, "engine") == 0) {
850 pr_response_add_err(R_501, _("%s: unsupported MODE Z option: %s"),
851 (char *) cmd->argv[0], key);
852
853 pr_cmd_set_errno(cmd, ENOSYS);
854 errno = ENOSYS;
855 return PR_ERROR(cmd);
856
857 } else if (strcasecmp(key, "level") == 0) {
858 int level;
859
860 level = atoi(val);
861 if (level < 0 ||
862 level > 9) {
863 pr_response_add_err(R_501, _("%s: bad MODE Z option value: %s %s"),
864 (char *) cmd->argv[0], key, val);
865
866 pr_cmd_set_errno(cmd, EINVAL);
867 errno = EINVAL;
868 return PR_ERROR(cmd);
869 }
870
871 deflate_compression_level = level;
872
873 } else {
874 pr_response_add_err(R_501, _("%s: unknown MODE Z option: %s"),
875 (char *) cmd->argv[0], key);
876
877 pr_cmd_set_errno(cmd, EINVAL);
878 errno = EINVAL;
879 return PR_ERROR(cmd);
880 }
881 }
882 }
883
884 pr_response_add(R_200, _("OPTS MODE Z OK"));
885 return PR_HANDLED(cmd);
886 }
887
888 return PR_DECLINED(cmd);
889 }
890
891 MODRET deflate_mode(cmd_rec *cmd) {
892 char *mode;
893
894 if (deflate_engine == FALSE) {
895 return PR_DECLINED(cmd);
896 }
897
898 if (cmd->argc != 2) {
899 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
900 "declining MODE Z (wrong number of parameters: %d)", cmd->argc);
901 return PR_DECLINED(cmd);
902 }
903
904 mode = cmd->argv[1];
905
906 if (PR_ISALPHA((int) mode[0])) {
907 mode[0] = toupper(mode[0]);
908 }
909
910 if (mode[0] == 'Z') {
911 if (session.rfc2228_mech != NULL) {
912 if (strcasecmp(session.rfc2228_mech, "tls") != 0) {
913 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
914 "declining MODE Z (RFC2228 mechanism '%s' in effect)",
915 session.rfc2228_mech);
916 pr_log_debug(DEBUG2, MOD_DEFLATE_VERSION
917 ": declining MODE Z (RFC2228 mechanism '%s' in effect)",
918 session.rfc2228_mech);
919
920 pr_response_add_err(R_504, _("Unable to handle MODE Z at this time"));
921
922 pr_cmd_set_errno(cmd, EPERM);
923 errno = EPERM;
924 return PR_ERROR(cmd);
925 }
926 }
927
928 if (deflate_enabled == TRUE) {
929 pr_response_add(R_200, _("OK"));
930 return PR_HANDLED(cmd);
931 }
932
933 deflate_next_netio = pr_get_netio(PR_NETIO_STRM_DATA);
934 if (deflate_next_netio != NULL) {
935 /* If another module has registered a NetIO callback already (e.g.
936 * mod_tls), we want to leave it in place, but replace some (but not all)
937 * of its callbacks with our own.
938 *
939 * We cache copies/pointers of the original callbacks, for restoring
940 * later if requested.
941 */
942 pr_trace_msg(trace_channel, 9, "overriding existing %s NetIO callbacks",
943 deflate_next_netio->owner_name? deflate_next_netio->owner_name :
944 deflate_next_netio->owner->name);
945
946 deflate_next_netio_close = deflate_next_netio->close;
947 deflate_next_netio->close = deflate_netio_close_cb;
948
949 deflate_next_netio_open = deflate_next_netio->open;
950 deflate_next_netio->open = deflate_netio_open_cb;
951
952 deflate_next_netio_read = deflate_next_netio->read;
953 deflate_next_netio->read = deflate_netio_read_cb;
954
955 deflate_next_netio_shutdown = deflate_next_netio->shutdown;
956 deflate_next_netio->shutdown = deflate_netio_shutdown_cb;
957
958 deflate_next_netio_write = deflate_next_netio->write;
959 deflate_next_netio->write = deflate_netio_write_cb;
960
961 } else {
962 /* Need to install some sort of NetIO handlers here, to handle
963 * compression.
964 */
965 deflate_netio = pr_alloc_netio2(session.pool, &deflate_module, NULL);
966 deflate_netio->close = deflate_netio_close_cb;
967 deflate_netio->open = deflate_netio_open_cb;
968 deflate_netio->read = deflate_netio_read_cb;
969 deflate_netio->shutdown = deflate_netio_shutdown_cb;
970 deflate_netio->write = deflate_netio_write_cb;
971
972 if (pr_register_netio(deflate_netio, PR_NETIO_STRM_DATA) < 0) {
973 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
974 "error registering netio: %s", strerror(errno));
975 }
976 }
977
978 deflate_enabled = TRUE;
979
980 pr_response_add(R_200, _("OK"));
981 return PR_HANDLED(cmd);
982
983 } else {
984 if (deflate_enabled) {
985 /* Switch to some other transmission mode. Remove our NetIO. */
986
987 if (deflate_next_netio != NULL) {
988 deflate_next_netio->close = deflate_next_netio_close;
989 deflate_next_netio_close = NULL;
990
991 deflate_next_netio->open = deflate_next_netio_open;
992 deflate_next_netio_open = NULL;
993
994 deflate_next_netio->read = deflate_next_netio_read;
995 deflate_next_netio_read = NULL;
996
997 deflate_next_netio->shutdown = deflate_next_netio_shutdown;
998 deflate_next_netio_shutdown = NULL;
999
1000 deflate_next_netio->write = deflate_next_netio_write;
1001 deflate_next_netio_write = NULL;
1002
1003 deflate_next_netio = NULL;
1004
1005 } else {
1006 if (pr_unregister_netio(PR_NETIO_STRM_DATA) < 0) {
1007 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
1008 "error unregistering netio: %s", strerror(errno));
1009
1010 } else {
1011 (void) pr_log_writefile(deflate_logfd, MOD_DEFLATE_VERSION,
1012 "%s %s: unregistered netio", (char *) cmd->argv[0],
1013 (char *) cmd->argv[1]);
1014 }
1015
1016 if (deflate_netio != NULL) {
1017 destroy_pool(deflate_netio->pool);
1018 deflate_netio = NULL;
1019 }
1020 }
1021
1022 deflate_enabled = FALSE;
1023 }
1024 }
1025
1026 return PR_DECLINED(cmd);
1027 }
1028
1029 /* Event listeners
1030 */
1031
1032 static void deflate_sess_reinit_ev(const void *event_data, void *user_data) {
1033 int res;
1034
1035 /* A HOST command changed the main_server pointer, reinitialize ourselves. */
1036
1037 pr_event_unregister(&deflate_module, "core.session-reinit",
1038 deflate_sess_reinit_ev);
1039
1040 deflate_engine = FALSE;
1041 pr_feat_remove("MODE Z");
1042 (void) close(deflate_logfd);
1043 deflate_logfd = -1;
1044
1045 res = deflate_sess_init();
1046 if (res < 0) {
1047 pr_session_disconnect(&deflate_module,
1048 PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
1049 }
1050 }
1051
1052 /* Initialization functions
1053 */
1054
1055 static int deflate_init(void) {
1056 pr_log_debug(DEBUG5, MOD_DEFLATE_VERSION ": using zlib " ZLIB_VERSION);
1057 return 0;
1058 }
1059
1060 static int deflate_sess_init(void) {
1061 config_rec *c;
1062
1063 pr_event_register(&deflate_module, "core.session-reinit",
1064 deflate_sess_reinit_ev, NULL);
1065
1066 c = find_config(main_server->conf, CONF_PARAM, "DeflateEngine", FALSE);
1067 if (c &&
1068 *((unsigned int *) c->argv[0]) == TRUE) {
1069 deflate_engine = TRUE;
1070
1071 } else {
1072 return 0;
1073 }
1074
1075 /* Add a FEAT string indicating support of MODE Z. Note that, in the
1076 * future, other compression engines (e.g. bzip2) will need to be handled
1077 * here as well.
1078 */
1079 pr_feat_add("MODE Z");
1080
1081 c = find_config(main_server->conf, CONF_PARAM, "DeflateLog", FALSE);
1082 if (c &&
1083 strcasecmp(c->argv[0], "none") != 0) {
1084 int res, xerrno = 0;
1085
1086 pr_signals_block();
1087 PRIVS_ROOT
1088 res = pr_log_openfile(c->argv[0], &deflate_logfd, PR_LOG_SYSTEM_MODE);
1089 xerrno = errno;
1090 PRIVS_RELINQUISH
1091 pr_signals_unblock();
1092
1093 switch (res) {
1094 case -1:
1095 pr_log_pri(PR_LOG_NOTICE, MOD_DEFLATE_VERSION
1096 ": notice: unable to open DeflateLog '%s': %s",
1097 (char *) c->argv[0], strerror(xerrno));
1098 break;
1099
1100 case PR_LOG_WRITABLE_DIR:
1101 pr_log_pri(PR_LOG_WARNING, MOD_DEFLATE_VERSION
1102 ": notice: unable to use DeflateLog '%s': parent directory is "
1103 "world-writable", (char *) c->argv[0]);
1104 break;
1105
1106 case PR_LOG_SYMLINK:
1107 pr_log_pri(PR_LOG_WARNING, MOD_DEFLATE_VERSION
1108 ": notice: unable to use DeflateLog '%s': cannot log to a symlink",
1109 (char *) c->argv[0]);
1110 break;
1111 }
1112 }
1113
1114 /* Allocate the buffers which will be used for inflating/deflating data.
1115 * Look up the optimal transfer buffer size, and use a factor of 8.
1116 * Later, if needed, a larger buffer will be allocated when necessary.
1117 */
1118 if (deflate_zbuf == NULL) {
1119 deflate_zbufsz = pr_config_get_xfer_bufsz() * 8;
1120 deflate_zbuf_ptr = deflate_zbuf = pcalloc(session.pool, deflate_zbufsz);
1121 deflate_zbuflen = 0;
1122 }
1123
1124 if (deflate_rbuf == NULL) {
1125 deflate_rbufsz = pr_config_get_xfer_bufsz();
1126 deflate_rbuf = palloc(session.pool, deflate_rbufsz);
1127 deflate_rbuflen = 0;
1128 }
1129
1130 return 0;
1131 }
1132
1133 /* Module API tables
1134 */
1135
1136 static conftable deflate_conftab[] = {
1137 { "DeflateEngine", set_deflateengine, NULL },
1138 { "DeflateLog", set_deflatelog, NULL },
1139 { NULL }
1140 };
1141
1142 static cmdtable deflate_cmdtab[] = {
1143 { CMD, C_OPTS "_MODE", G_NONE, deflate_opts, FALSE, FALSE, CL_MISC },
1144 { CMD, C_MODE, G_NONE, deflate_mode, FALSE, FALSE, CL_MISC },
1145 { 0, NULL }
1146 };
1147
1148 module deflate_module = {
1149 NULL, NULL,
1150
1151 /* Module API version 2.0 */
1152 0x20,
1153
1154 /* Module name */
1155 "deflate",
1156
1157 /* Module configuration handler table */
1158 deflate_conftab,
1159
1160 /* Module command handler table */
1161 deflate_cmdtab,
1162
1163 /* Module authentication handler table */
1164 NULL,
1165
1166 /* Module initialization function */
1167 deflate_init,
1168
1169 /* Session initialization function */
1170 deflate_sess_init,
1171
1172 /* Module version */
1173 MOD_DEFLATE_VERSION
1174 };