| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "mod_sftp.h" |
| 26 |
|
| 27 |
#include "ssh2.h" |
| 28 |
#include "msg.h" |
| 29 |
#include "packet.h" |
| 30 |
#include "crypto.h" |
| 31 |
#include "compress.h" |
| 32 |
|
| 33 |
#if defined(HAVE_ZLIB_H) |
| 34 |
#include <zlib.h> |
| 35 |
|
| 36 |
static const char *trace_channel = "ssh2"; |
| 37 |
|
| 38 |
struct sftp_compress { |
| 39 |
int use_zlib; |
| 40 |
int stream_ready; |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static struct sftp_compress read_compresses[] = { |
| 51 |
{ FALSE, FALSE }, |
| 52 |
{ FALSE, FALSE } |
| 53 |
}; |
| 54 |
static z_stream read_streams[2]; |
| 55 |
|
| 56 |
static struct sftp_compress write_compresses[] = { |
| 57 |
{ FALSE, FALSE }, |
| 58 |
{ FALSE, FALSE } |
| 59 |
}; |
| 60 |
static z_stream write_streams[2]; |
| 61 |
|
| 62 |
static unsigned int read_comp_idx = 0; |
| 63 |
static unsigned int write_comp_idx = 0; |
| 64 |
|
| 65 |
static unsigned int get_next_read_index(void) { |
| 66 |
if (read_comp_idx == 1) { |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
return 1; |
| 71 |
} |
| 72 |
|
| 73 |
static unsigned int get_next_write_index(void) { |
| 74 |
if (write_comp_idx == 1) { |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
return 1; |
| 79 |
} |
| 80 |
|
| 81 |
static void switch_read_compress(int flags) { |
| 82 |
struct sftp_compress *comp; |
| 83 |
z_stream *stream; |
| 84 |
|
| 85 |
comp = &(read_compresses[read_comp_idx]); |
| 86 |
stream = &(read_streams[read_comp_idx]); |
| 87 |
|
| 88 |
|
| 89 |
if (comp->use_zlib == flags && |
| 90 |
comp->stream_ready) { |
| 91 |
|
| 92 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 93 |
"done decompressing data: decompressed %" PR_LU " bytes to %" PR_LU |
| 94 |
" bytes of data (%.2f)", (pr_off_t) stream->total_in, |
| 95 |
(pr_off_t) stream->total_out, |
| 96 |
stream->total_in == 0 ? 0.0 : |
| 97 |
(float) stream->total_out / stream->total_in); |
| 98 |
|
| 99 |
inflateEnd(stream); |
| 100 |
comp->use_zlib = FALSE; |
| 101 |
comp->stream_ready = FALSE; |
| 102 |
|
| 103 |
|
| 104 |
if (read_comp_idx == 1) { |
| 105 |
read_comp_idx = 0; |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
read_comp_idx = 1; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
static void switch_write_compress(int flags) { |
| 114 |
struct sftp_compress *comp; |
| 115 |
z_stream *stream; |
| 116 |
|
| 117 |
comp = &(write_compresses[write_comp_idx]); |
| 118 |
stream = &(write_streams[write_comp_idx]); |
| 119 |
|
| 120 |
|
| 121 |
if (comp->use_zlib == flags && |
| 122 |
comp->stream_ready) { |
| 123 |
|
| 124 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 125 |
"done compressing data: compressed %" PR_LU " bytes to %" PR_LU |
| 126 |
" bytes of data (%.2f)", (pr_off_t) stream->total_in, |
| 127 |
(pr_off_t) stream->total_out, |
| 128 |
stream->total_in == 0 ? 0.0 : |
| 129 |
(float) stream->total_out / stream->total_in); |
| 130 |
|
| 131 |
deflateEnd(stream); |
| 132 |
comp->use_zlib = FALSE; |
| 133 |
comp->stream_ready = FALSE; |
| 134 |
|
| 135 |
|
| 136 |
if (write_comp_idx == 1) { |
| 137 |
write_comp_idx = 0; |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
write_comp_idx = 1; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
const char *sftp_compress_get_read_algo(void) { |
| 146 |
struct sftp_compress *comp; |
| 147 |
|
| 148 |
comp = &(read_compresses[read_comp_idx]); |
| 149 |
|
| 150 |
if (comp->use_zlib) { |
| 151 |
if (comp->use_zlib == SFTP_COMPRESS_FL_NEW_KEY) { |
| 152 |
return "zlib"; |
| 153 |
} |
| 154 |
|
| 155 |
if (comp->use_zlib == SFTP_COMPRESS_FL_AUTHENTICATED) { |
| 156 |
return "[email protected]"; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return "none"; |
| 161 |
} |
| 162 |
|
| 163 |
int sftp_compress_set_read_algo(const char *algo) { |
| 164 |
unsigned int idx = read_comp_idx; |
| 165 |
|
| 166 |
if (read_compresses[idx].stream_ready) { |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
idx = get_next_read_index(); |
| 171 |
} |
| 172 |
|
| 173 |
if (strncmp(algo, "[email protected]", 17) == 0) { |
| 174 |
read_compresses[idx].use_zlib = SFTP_COMPRESS_FL_AUTHENTICATED; |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
if (strncmp(algo, "zlib", 5) == 0) { |
| 179 |
read_compresses[idx].use_zlib = SFTP_COMPRESS_FL_NEW_KEY; |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
if (strncmp(algo, "none", 5) == 0) { |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
errno = EINVAL; |
| 188 |
return -1; |
| 189 |
} |
| 190 |
|
| 191 |
int sftp_compress_init_read(int flags) { |
| 192 |
struct sftp_compress *comp; |
| 193 |
z_stream *stream; |
| 194 |
|
| 195 |
switch_read_compress(flags); |
| 196 |
|
| 197 |
comp = &(read_compresses[read_comp_idx]); |
| 198 |
stream = &(read_streams[read_comp_idx]); |
| 199 |
|
| 200 |
if (comp->use_zlib == flags && |
| 201 |
!comp->stream_ready) { |
| 202 |
int zres; |
| 203 |
|
| 204 |
zres = inflateInit(stream); |
| 205 |
if (zres != Z_OK) { |
| 206 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 207 |
"error preparing decompression stream (%d)", zres); |
| 208 |
} |
| 209 |
|
| 210 |
pr_event_generate("mod_sftp.ssh.client-compression", NULL); |
| 211 |
comp->stream_ready = TRUE; |
| 212 |
} |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
int sftp_compress_read_data(struct ssh2_packet *pkt) { |
| 218 |
struct sftp_compress *comp; |
| 219 |
z_stream *stream; |
| 220 |
|
| 221 |
comp = &(read_compresses[read_comp_idx]); |
| 222 |
stream = &(read_streams[read_comp_idx]); |
| 223 |
|
| 224 |
if (comp->use_zlib && |
| 225 |
comp->stream_ready) { |
| 226 |
unsigned char buf[16384], *input; |
| 227 |
char *payload; |
| 228 |
uint32_t input_len, payload_len = 0, payload_sz; |
| 229 |
pool *sub_pool; |
| 230 |
int zres; |
| 231 |
|
| 232 |
if (pkt->payload_len == 0) { |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
sub_pool = make_sub_pool(pkt->pool); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
input_len = pkt->payload_len; |
| 242 |
input = palloc(sub_pool, input_len); |
| 243 |
memcpy(input, pkt->payload, input_len); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
payload_sz = input_len * 8; |
| 249 |
payload = palloc(sub_pool, payload_sz); |
| 250 |
|
| 251 |
stream->next_in = input; |
| 252 |
stream->avail_in = input_len; |
| 253 |
|
| 254 |
while (1) { |
| 255 |
size_t copy_len = 0; |
| 256 |
|
| 257 |
pr_signals_handle(); |
| 258 |
|
| 259 |
stream->next_out = buf; |
| 260 |
stream->avail_out = sizeof(buf); |
| 261 |
|
| 262 |
zres = inflate(stream, Z_SYNC_FLUSH); |
| 263 |
switch (zres) { |
| 264 |
case Z_OK: |
| 265 |
copy_len = sizeof(buf) - stream->avail_out; |
| 266 |
|
| 267 |
|
| 268 |
if ((payload_len + copy_len) > payload_sz) { |
| 269 |
uint32_t new_sz; |
| 270 |
char *tmp; |
| 271 |
|
| 272 |
pr_signals_handle(); |
| 273 |
|
| 274 |
new_sz = payload_sz; |
| 275 |
while ((payload_len + copy_len) > new_sz) { |
| 276 |
pr_signals_handle(); |
| 277 |
|
| 278 |
|
| 279 |
new_sz *= 2; |
| 280 |
} |
| 281 |
|
| 282 |
if (new_sz > SFTP_MAX_PACKET_LEN) { |
| 283 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 284 |
"decompression error: inflated payload size (%lu bytes) " |
| 285 |
"exceeds %lu byte maximum", (unsigned long) new_sz, |
| 286 |
(unsigned long) SFTP_MAX_PACKET_LEN); |
| 287 |
destroy_pool(sub_pool); |
| 288 |
errno = EIO; |
| 289 |
return -1; |
| 290 |
} |
| 291 |
|
| 292 |
pr_trace_msg(trace_channel, 20, |
| 293 |
"allocating larger payload size (%lu bytes) for " |
| 294 |
"inflated data (%lu bytes) plus existing payload %lu bytes", |
| 295 |
(unsigned long) new_sz, (unsigned long) copy_len, |
| 296 |
(unsigned long) payload_len); |
| 297 |
|
| 298 |
tmp = palloc(sub_pool, new_sz); |
| 299 |
memcpy(tmp, payload, payload_len); |
| 300 |
payload = tmp; |
| 301 |
payload_sz = new_sz; |
| 302 |
} |
| 303 |
|
| 304 |
if (copy_len > 0) { |
| 305 |
memcpy(payload + payload_len, buf, copy_len); |
| 306 |
payload_len += copy_len; |
| 307 |
|
| 308 |
pr_trace_msg(trace_channel, 20, |
| 309 |
"inflated %lu bytes to %lu bytes", |
| 310 |
(unsigned long) input_len, (unsigned long) copy_len); |
| 311 |
} |
| 312 |
|
| 313 |
continue; |
| 314 |
|
| 315 |
case Z_BUF_ERROR: |
| 316 |
break; |
| 317 |
|
| 318 |
default: |
| 319 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 320 |
"unhandled zlib error (%d) while decompressing", zres); |
| 321 |
destroy_pool(sub_pool); |
| 322 |
return -1; |
| 323 |
} |
| 324 |
|
| 325 |
break; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
if (pkt->payload_len < payload_len) { |
| 332 |
pkt->payload = palloc(pkt->pool, payload_len); |
| 333 |
} |
| 334 |
|
| 335 |
memcpy(pkt->payload, payload, payload_len); |
| 336 |
pkt->payload_len = payload_len; |
| 337 |
|
| 338 |
pr_trace_msg(trace_channel, 20, |
| 339 |
"finished inflating (payload len = %lu bytes)", |
| 340 |
(unsigned long) payload_len); |
| 341 |
|
| 342 |
destroy_pool(sub_pool); |
| 343 |
} |
| 344 |
|
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
const char *sftp_compress_get_write_algo(void) { |
| 349 |
struct sftp_compress *comp; |
| 350 |
|
| 351 |
comp = &(write_compresses[write_comp_idx]); |
| 352 |
|
| 353 |
if (comp->use_zlib) { |
| 354 |
if (comp->use_zlib == SFTP_COMPRESS_FL_NEW_KEY) { |
| 355 |
return "zlib"; |
| 356 |
} |
| 357 |
|
| 358 |
if (comp->use_zlib == SFTP_COMPRESS_FL_AUTHENTICATED) { |
| 359 |
return "[email protected]"; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
return "none"; |
| 364 |
} |
| 365 |
|
| 366 |
int sftp_compress_set_write_algo(const char *algo) { |
| 367 |
unsigned int idx = write_comp_idx; |
| 368 |
|
| 369 |
if (write_compresses[idx].stream_ready) { |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
idx = get_next_write_index(); |
| 374 |
} |
| 375 |
|
| 376 |
if (strncmp(algo, "[email protected]", 17) == 0) { |
| 377 |
write_compresses[idx].use_zlib = SFTP_COMPRESS_FL_AUTHENTICATED; |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
if (strncmp(algo, "zlib", 5) == 0) { |
| 382 |
write_compresses[idx].use_zlib = SFTP_COMPRESS_FL_NEW_KEY; |
| 383 |
return 0; |
| 384 |
} |
| 385 |
|
| 386 |
if (strncmp(algo, "none", 5) == 0) { |
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
errno = EINVAL; |
| 391 |
return -1; |
| 392 |
} |
| 393 |
|
| 394 |
int sftp_compress_init_write(int flags) { |
| 395 |
struct sftp_compress *comp; |
| 396 |
z_stream *stream; |
| 397 |
|
| 398 |
switch_write_compress(flags); |
| 399 |
|
| 400 |
comp = &(write_compresses[write_comp_idx]); |
| 401 |
stream = &(write_streams[write_comp_idx]); |
| 402 |
|
| 403 |
if (comp->use_zlib == flags && |
| 404 |
!comp->stream_ready) { |
| 405 |
int zres; |
| 406 |
|
| 407 |
zres = deflateInit(stream, Z_DEFAULT_COMPRESSION); |
| 408 |
if (zres != Z_OK) { |
| 409 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 410 |
"error preparing compression stream (%d)", zres); |
| 411 |
} |
| 412 |
|
| 413 |
pr_event_generate("mod_sftp.ssh.server-compression", NULL); |
| 414 |
comp->stream_ready = TRUE; |
| 415 |
} |
| 416 |
|
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
int sftp_compress_write_data(struct ssh2_packet *pkt) { |
| 421 |
struct sftp_compress *comp; |
| 422 |
z_stream *stream; |
| 423 |
|
| 424 |
comp = &(write_compresses[write_comp_idx]); |
| 425 |
stream = &(write_streams[write_comp_idx]); |
| 426 |
|
| 427 |
if (comp->use_zlib && |
| 428 |
comp->stream_ready) { |
| 429 |
unsigned char buf[16384], *input; |
| 430 |
char *payload; |
| 431 |
uint32_t input_len, payload_len = 0, payload_sz; |
| 432 |
pool *sub_pool; |
| 433 |
int zres; |
| 434 |
|
| 435 |
if (pkt->payload_len == 0) { |
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
sub_pool = make_sub_pool(pkt->pool); |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
input_len = pkt->payload_len; |
| 445 |
input = palloc(sub_pool, input_len); |
| 446 |
memcpy(input, pkt->payload, input_len); |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
payload_sz = 1024; |
| 452 |
if ((input_len * 2) > payload_sz) { |
| 453 |
payload_sz = input_len * 2; |
| 454 |
} |
| 455 |
payload = palloc(sub_pool, payload_sz); |
| 456 |
|
| 457 |
stream->next_in = input; |
| 458 |
stream->avail_in = input_len; |
| 459 |
stream->avail_out = 0; |
| 460 |
|
| 461 |
while (stream->avail_out == 0) { |
| 462 |
size_t copy_len = 0; |
| 463 |
|
| 464 |
pr_signals_handle(); |
| 465 |
|
| 466 |
stream->next_out = buf; |
| 467 |
stream->avail_out = sizeof(buf); |
| 468 |
|
| 469 |
zres = deflate(stream, Z_SYNC_FLUSH); |
| 470 |
|
| 471 |
switch (zres) { |
| 472 |
case Z_OK: |
| 473 |
copy_len = sizeof(buf) - stream->avail_out; |
| 474 |
|
| 475 |
|
| 476 |
if ((payload_len + copy_len) > payload_sz) { |
| 477 |
uint32_t new_sz; |
| 478 |
char *tmp; |
| 479 |
|
| 480 |
new_sz = payload_sz; |
| 481 |
while ((payload_len + copy_len) > new_sz) { |
| 482 |
pr_signals_handle(); |
| 483 |
|
| 484 |
|
| 485 |
new_sz *= 2; |
| 486 |
} |
| 487 |
|
| 488 |
pr_trace_msg(trace_channel, 20, |
| 489 |
"allocating larger payload size (%lu bytes) for " |
| 490 |
"deflated data (%lu bytes) plus existing payload %lu bytes", |
| 491 |
(unsigned long) new_sz, (unsigned long) copy_len, |
| 492 |
(unsigned long) payload_len); |
| 493 |
|
| 494 |
tmp = palloc(sub_pool, new_sz); |
| 495 |
memcpy(tmp, payload, payload_len); |
| 496 |
payload = tmp; |
| 497 |
payload_sz = new_sz; |
| 498 |
} |
| 499 |
|
| 500 |
memcpy(payload + payload_len, buf, copy_len); |
| 501 |
payload_len += copy_len; |
| 502 |
|
| 503 |
pr_trace_msg(trace_channel, 20, |
| 504 |
"deflated %lu bytes to %lu bytes", |
| 505 |
(unsigned long) input_len, (unsigned long) copy_len); |
| 506 |
|
| 507 |
break; |
| 508 |
|
| 509 |
default: |
| 510 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 511 |
"unhandled zlib error (%d) while compressing", zres); |
| 512 |
destroy_pool(sub_pool); |
| 513 |
errno = EIO; |
| 514 |
return -1; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
if (payload_len > 0) { |
| 519 |
if (pkt->payload_len < payload_len) { |
| 520 |
pkt->payload = palloc(pkt->pool, payload_len); |
| 521 |
} |
| 522 |
|
| 523 |
memcpy(pkt->payload, payload, payload_len); |
| 524 |
pkt->payload_len = payload_len; |
| 525 |
|
| 526 |
pr_trace_msg(trace_channel, 20, |
| 527 |
"finished deflating (payload len = %lu bytes)", |
| 528 |
(unsigned long) payload_len); |
| 529 |
} |
| 530 |
|
| 531 |
destroy_pool(sub_pool); |
| 532 |
} |
| 533 |
|
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
#else |
| 538 |
|
| 539 |
int sftp_compress_init_read(int flags) { |
| 540 |
return 0; |
| 541 |
} |
| 542 |
|
| 543 |
const char *sftp_compress_get_read_algo(void) { |
| 544 |
return "none"; |
| 545 |
} |
| 546 |
|
| 547 |
int sftp_compress_set_read_algo(const char *algo) { |
| 548 |
if (strncmp(algo, "none", 5) == 0) { |
| 549 |
return 0; |
| 550 |
} |
| 551 |
|
| 552 |
errno = EINVAL; |
| 553 |
return -1; |
| 554 |
} |
| 555 |
|
| 556 |
int sftp_compress_read_data(struct ssh2_packet *pkt) { |
| 557 |
return 0; |
| 558 |
} |
| 559 |
|
| 560 |
int sftp_compress_init_write(int flags) { |
| 561 |
return 0; |
| 562 |
} |
| 563 |
|
| 564 |
const char *sftp_compress_get_write_algo(void) { |
| 565 |
return "none"; |
| 566 |
} |
| 567 |
|
| 568 |
int sftp_compress_set_write_algo(const char *algo) { |
| 569 |
if (strncmp(algo, "none", 5) == 0) { |
| 570 |
return 0; |
| 571 |
} |
| 572 |
|
| 573 |
errno = EINVAL; |
| 574 |
return -1; |
| 575 |
} |
| 576 |
|
| 577 |
int sftp_compress_write_data(struct ssh2_packet *pkt) { |
| 578 |
return 0; |
| 579 |
} |
| 580 |
|
| 581 |
#endif |