| 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 |
#include "ssh2.h" |
| 27 |
#include "msg.h" |
| 28 |
#include "packet.h" |
| 29 |
#include "channel.h" |
| 30 |
#include "disconnect.h" |
| 31 |
#include "interop.h" |
| 32 |
#include "fxp.h" |
| 33 |
#include "scp.h" |
| 34 |
#include "date.h" |
| 35 |
|
| 36 |
extern module sftp_module; |
| 37 |
|
| 38 |
|
| 39 |
struct ssh2_channel_exec_handler { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
module *m; |
| 44 |
|
| 45 |
|
| 46 |
const char *command; |
| 47 |
|
| 48 |
int (*set_params)(pool *, uint32_t, array_header *); |
| 49 |
int (*prepare)(uint32_t); |
| 50 |
int (*postopen)(uint32_t); |
| 51 |
int (*handle_packet)(pool *, void *, uint32_t, unsigned char *, uint32_t); |
| 52 |
int (*finish)(uint32_t); |
| 53 |
}; |
| 54 |
|
| 55 |
static array_header *channel_exec_handlers = NULL; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
struct ssh2_channel_databuf { |
| 61 |
pool *pool; |
| 62 |
|
| 63 |
struct ssh2_channel_databuf *next; |
| 64 |
|
| 65 |
|
| 66 |
char *ptr; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
char *buf; |
| 72 |
|
| 73 |
uint32_t buflen; |
| 74 |
uint32_t bufsz; |
| 75 |
}; |
| 76 |
|
| 77 |
static pool *channel_pool = NULL; |
| 78 |
static uint32_t channelno = 0; |
| 79 |
|
| 80 |
static unsigned int channel_max = SFTP_SSH2_CHANNEL_MAX_COUNT; |
| 81 |
static unsigned int channel_count = 0; |
| 82 |
|
| 83 |
static pool *channel_databuf_pool = NULL; |
| 84 |
|
| 85 |
|
| 86 |
static array_header *channel_list = NULL; |
| 87 |
|
| 88 |
static uint32_t chan_window_size = SFTP_SSH2_CHANNEL_WINDOW_SIZE; |
| 89 |
static uint32_t chan_packet_size = SFTP_SSH2_CHANNEL_MAX_PACKET_SIZE; |
| 90 |
|
| 91 |
static array_header *accepted_envs = NULL; |
| 92 |
|
| 93 |
static const char *trace_channel = "ssh2"; |
| 94 |
|
| 95 |
static int send_channel_window_adjust(struct ssh2_channel *); |
| 96 |
static int send_channel_done(pool *, uint32_t); |
| 97 |
|
| 98 |
static struct ssh2_channel *alloc_channel(const char *type, |
| 99 |
uint32_t remote_channel_id, uint32_t remote_windowsz, |
| 100 |
uint32_t remote_max_packetsz) { |
| 101 |
register unsigned int i; |
| 102 |
struct ssh2_channel *chan = NULL, **chans = NULL; |
| 103 |
pool *sub_pool = NULL; |
| 104 |
int found_existing_slot = FALSE; |
| 105 |
|
| 106 |
sub_pool = make_sub_pool(channel_pool); |
| 107 |
pr_pool_tag(sub_pool, "SSH2 channel pool"); |
| 108 |
|
| 109 |
chan = pcalloc(sub_pool, sizeof(struct ssh2_channel)); |
| 110 |
chan->pool = sub_pool; |
| 111 |
chan->type = pstrdup(sub_pool, type); |
| 112 |
|
| 113 |
chan->local_channel_id = channelno++; |
| 114 |
|
| 115 |
chan->local_windowsz = chan_window_size; |
| 116 |
chan->local_max_packetsz = chan_packet_size; |
| 117 |
|
| 118 |
chan->remote_channel_id = remote_channel_id; |
| 119 |
chan->remote_windowsz = remote_windowsz; |
| 120 |
chan->remote_max_packetsz = remote_max_packetsz; |
| 121 |
|
| 122 |
if (channel_list == NULL) { |
| 123 |
channel_list = make_array(channel_pool, 1, sizeof(struct ssh2_channel *)); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
chans = channel_list->elts; |
| 130 |
for (i = 0; i < channel_list->nelts; i++) { |
| 131 |
if (chans[i] == NULL) { |
| 132 |
chans[i] = chan; |
| 133 |
found_existing_slot = TRUE; |
| 134 |
|
| 135 |
pr_trace_msg(trace_channel, 22, |
| 136 |
"reusing existing empty slot in channel list (%d item count) for new " |
| 137 |
"channel ID %lu", channel_list->nelts, |
| 138 |
(unsigned long) chan->local_channel_id); |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if (found_existing_slot == FALSE) { |
| 144 |
*((struct ssh2_channel **) push_array(channel_list)) = chan; |
| 145 |
} |
| 146 |
|
| 147 |
channel_count++; |
| 148 |
return chan; |
| 149 |
} |
| 150 |
|
| 151 |
static void destroy_channel(uint32_t channel_id) { |
| 152 |
register unsigned int i; |
| 153 |
struct ssh2_channel **chans; |
| 154 |
|
| 155 |
if (channel_list == NULL) { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
chans = channel_list->elts; |
| 160 |
for (i = 0; i < channel_list->nelts; i++) { |
| 161 |
if (chans[i] != NULL && |
| 162 |
chans[i]->local_channel_id == channel_id) { |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (chans[i]->recvd_close == TRUE && |
| 168 |
chans[i]->sent_close == TRUE) { |
| 169 |
if (chans[i]->finish != NULL) { |
| 170 |
pr_trace_msg(trace_channel, 15, |
| 171 |
"calling finish handler for channel ID %lu", |
| 172 |
(unsigned long) channel_id); |
| 173 |
(chans[i]->finish)(channel_id); |
| 174 |
} |
| 175 |
|
| 176 |
destroy_pool(chans[i]->pool); |
| 177 |
chans[i] = NULL; |
| 178 |
|
| 179 |
channel_count--; |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
static struct ssh2_channel *get_channel(uint32_t channel_id) { |
| 187 |
register unsigned int i; |
| 188 |
struct ssh2_channel **chans; |
| 189 |
|
| 190 |
if (channel_list == NULL) { |
| 191 |
errno = EACCES; |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
chans = channel_list->elts; |
| 196 |
for (i = 0; i < channel_list->nelts; i++) { |
| 197 |
if (chans[i] != NULL && |
| 198 |
chans[i]->local_channel_id == channel_id) { |
| 199 |
return chans[i]; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
errno = ENOENT; |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
|
| 207 |
static void drain_pending_incoming_channel_data(uint32_t channel_id) { |
| 208 |
struct ssh2_channel *chan; |
| 209 |
pool *tmp_pool; |
| 210 |
struct ssh2_channel_databuf *db; |
| 211 |
|
| 212 |
chan = get_channel(channel_id); |
| 213 |
if (chan == NULL) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
if (chan->incoming_head == NULL) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
tmp_pool = make_sub_pool(channel_pool); |
| 222 |
pr_pool_tag(tmp_pool, "SSH2 pending incoming data drain pool"); |
| 223 |
|
| 224 |
pr_trace_msg(trace_channel, 15, |
| 225 |
"draining pending incoming data for channel ID %lu (%lu bytes)", |
| 226 |
(unsigned long) channel_id, (unsigned long) chan->incoming_len); |
| 227 |
|
| 228 |
db = chan->incoming_head; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
while (db != NULL && |
| 235 |
chan->remote_windowsz > 0) { |
| 236 |
struct ssh2_channel_databuf *next; |
| 237 |
struct ssh2_packet *pkt; |
| 238 |
unsigned char *buf, *ptr; |
| 239 |
uint32_t bufsz, buflen; |
| 240 |
int res; |
| 241 |
|
| 242 |
pr_signals_handle(); |
| 243 |
|
| 244 |
pkt = sftp_ssh2_packet_create(tmp_pool); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
bufsz = buflen = db->buflen + 9; |
| 251 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 252 |
|
| 253 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_CHANNEL_DATA); |
| 254 |
sftp_msg_write_int(&buf, &buflen, chan->remote_channel_id); |
| 255 |
sftp_msg_write_int(&buf, &buflen, db->buflen); |
| 256 |
if (db->buflen > 0) { |
| 257 |
memcpy(buf, db->buf, db->buflen); |
| 258 |
} |
| 259 |
buflen -= db->buflen; |
| 260 |
|
| 261 |
pkt->payload = ptr; |
| 262 |
pkt->payload_len = (bufsz - buflen); |
| 263 |
|
| 264 |
pr_trace_msg(trace_channel, 9, "handling CHANNEL_DATA (remote channel " |
| 265 |
"ID %lu, %lu data bytes)", (unsigned long) chan->remote_channel_id, |
| 266 |
(unsigned long) pkt->payload_len); |
| 267 |
|
| 268 |
res = chan->handle_packet(pkt->pool, pkt, chan->local_channel_id, |
| 269 |
(unsigned char *) db->buf, db->buflen); |
| 270 |
if (res < 0) { |
| 271 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 272 |
"error draining pending incoming CHANNEL_DATA for channel ID %lu: %s", |
| 273 |
(unsigned long) channel_id, strerror(errno)); |
| 274 |
destroy_pool(tmp_pool); |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
destroy_pool(pkt->pool); |
| 279 |
chan->local_windowsz -= db->buflen; |
| 280 |
send_channel_window_adjust(chan); |
| 281 |
|
| 282 |
|
| 283 |
chan->incoming_len -= db->buflen; |
| 284 |
next = db->next; |
| 285 |
destroy_pool(db->pool); |
| 286 |
chan->incoming_head = db = next; |
| 287 |
} |
| 288 |
|
| 289 |
if (chan->incoming_head == NULL) { |
| 290 |
chan->incoming_tail = NULL; |
| 291 |
|
| 292 |
} else { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
pr_trace_msg(trace_channel, 15, |
| 298 |
"still have pending incoming channel data (%lu bytes) for channel " |
| 299 |
"ID %lu (window at %lu bytes)", (unsigned long) chan->incoming_len, |
| 300 |
(unsigned long) channel_id, (unsigned long) chan->remote_windowsz); |
| 301 |
} |
| 302 |
|
| 303 |
destroy_pool(tmp_pool); |
| 304 |
} |
| 305 |
|
| 306 |
static void drain_pending_outgoing_channel_data(uint32_t channel_id) { |
| 307 |
struct ssh2_channel *chan; |
| 308 |
pool *tmp_pool; |
| 309 |
struct ssh2_channel_databuf *db; |
| 310 |
|
| 311 |
chan = get_channel(channel_id); |
| 312 |
if (chan == NULL) { |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
if (chan->outgoing_head == NULL) { |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
tmp_pool = make_sub_pool(channel_pool); |
| 321 |
pr_pool_tag(tmp_pool, "SSH2 pending outgoing data drain pool"); |
| 322 |
|
| 323 |
pr_trace_msg(trace_channel, 15, |
| 324 |
"draining pending outgoing data for channel ID %lu (%lu bytes)", |
| 325 |
(unsigned long) channel_id, (unsigned long) chan->outgoing_len); |
| 326 |
|
| 327 |
db = chan->outgoing_head; |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
while (!(sftp_sess_state & SFTP_SESS_STATE_REKEYING) && |
| 335 |
db != NULL && |
| 336 |
db->buflen > 0 && |
| 337 |
chan->remote_windowsz > 0) { |
| 338 |
struct ssh2_packet *pkt; |
| 339 |
unsigned char *buf, *ptr; |
| 340 |
uint32_t bufsz, buflen, payload_len; |
| 341 |
int res; |
| 342 |
|
| 343 |
pr_signals_handle(); |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
payload_len = db->buflen; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if (payload_len > chan->remote_max_packetsz) { |
| 356 |
payload_len = chan->remote_max_packetsz; |
| 357 |
} |
| 358 |
|
| 359 |
if (payload_len > chan->remote_windowsz) { |
| 360 |
payload_len = chan->remote_windowsz; |
| 361 |
} |
| 362 |
|
| 363 |
pkt = sftp_ssh2_packet_create(tmp_pool); |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
bufsz = buflen = payload_len + 9; |
| 370 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 371 |
|
| 372 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_CHANNEL_DATA); |
| 373 |
sftp_msg_write_int(&buf, &buflen, chan->remote_channel_id); |
| 374 |
sftp_msg_write_int(&buf, &buflen, payload_len); |
| 375 |
memcpy(buf, db->buf, payload_len); |
| 376 |
buflen -= payload_len; |
| 377 |
|
| 378 |
pkt->payload = ptr; |
| 379 |
pkt->payload_len = (bufsz - buflen); |
| 380 |
|
| 381 |
pr_trace_msg(trace_channel, 9, "sending CHANNEL_DATA (remote channel " |
| 382 |
"ID %lu, %lu data bytes)", (unsigned long) chan->remote_channel_id, |
| 383 |
(unsigned long) payload_len); |
| 384 |
|
| 385 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 386 |
if (res < 0) { |
| 387 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 388 |
"error draining outgoing pending CHANNEL_DATA for channel ID %lu: %s", |
| 389 |
(unsigned long) channel_id, strerror(errno)); |
| 390 |
destroy_pool(tmp_pool); |
| 391 |
return; |
| 392 |
} |
| 393 |
|
| 394 |
chan->remote_windowsz -= payload_len; |
| 395 |
|
| 396 |
pr_trace_msg(trace_channel, 11, |
| 397 |
"channel ID %lu remote window size currently at %lu bytes", |
| 398 |
(unsigned long) chan->remote_channel_id, |
| 399 |
(unsigned long) chan->remote_windowsz); |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
chan->outgoing_len -= payload_len; |
| 408 |
|
| 409 |
if (payload_len == db->buflen) { |
| 410 |
struct ssh2_channel_databuf *next; |
| 411 |
|
| 412 |
next = db->next; |
| 413 |
destroy_pool(db->pool); |
| 414 |
chan->outgoing_head = db = next; |
| 415 |
|
| 416 |
} else { |
| 417 |
db->buf += payload_len; |
| 418 |
db->buflen -= payload_len; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
if (chan->outgoing_head == NULL) { |
| 423 |
chan->outgoing_tail = NULL; |
| 424 |
|
| 425 |
} else { |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
pr_trace_msg(trace_channel, 15, |
| 431 |
"still have pending outgoing channel data (%lu bytes) for channel " |
| 432 |
"ID %lu (window at %lu bytes)", (unsigned long) chan->outgoing_len, |
| 433 |
(unsigned long) channel_id, (unsigned long) chan->remote_windowsz); |
| 434 |
} |
| 435 |
|
| 436 |
destroy_pool(tmp_pool); |
| 437 |
} |
| 438 |
|
| 439 |
static struct ssh2_channel_databuf *get_databuf(uint32_t channel_id, |
| 440 |
uint32_t buflen) { |
| 441 |
struct ssh2_channel *chan; |
| 442 |
struct ssh2_channel_databuf *db; |
| 443 |
pool *sub_pool; |
| 444 |
|
| 445 |
chan = get_channel(channel_id); |
| 446 |
if (chan == NULL) { |
| 447 |
errno = EPERM; |
| 448 |
return NULL; |
| 449 |
} |
| 450 |
|
| 451 |
if (channel_databuf_pool == NULL) { |
| 452 |
channel_databuf_pool = make_sub_pool(channel_pool); |
| 453 |
pr_pool_tag(channel_databuf_pool, "SSH2 Channel data buffer pool"); |
| 454 |
} |
| 455 |
|
| 456 |
sub_pool = pr_pool_create_sz(channel_databuf_pool, 128); |
| 457 |
pr_pool_tag(sub_pool, "channel databuf pool"); |
| 458 |
|
| 459 |
db = pcalloc(sub_pool, sizeof(struct ssh2_channel_databuf)); |
| 460 |
db->pool = sub_pool; |
| 461 |
db->bufsz = buflen; |
| 462 |
db->ptr = db->buf = palloc(db->pool, db->bufsz); |
| 463 |
|
| 464 |
db->buflen = 0; |
| 465 |
db->next = NULL; |
| 466 |
|
| 467 |
return db; |
| 468 |
} |
| 469 |
|
| 470 |
static struct ssh2_channel_databuf *get_incoming_databuf(uint32_t channel_id, |
| 471 |
uint32_t buflen) { |
| 472 |
struct ssh2_channel_databuf *db; |
| 473 |
struct ssh2_channel *chan; |
| 474 |
|
| 475 |
db = get_databuf(channel_id, buflen); |
| 476 |
if (db == NULL) { |
| 477 |
return NULL; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
chan = get_channel(channel_id); |
| 484 |
if (chan->incoming_tail != NULL) { |
| 485 |
chan->incoming_tail->next = db; |
| 486 |
chan->incoming_tail = db; |
| 487 |
|
| 488 |
} else { |
| 489 |
chan->incoming_head = chan->incoming_tail = db; |
| 490 |
} |
| 491 |
|
| 492 |
return db; |
| 493 |
} |
| 494 |
|
| 495 |
static struct ssh2_channel_databuf *get_outgoing_databuf(uint32_t channel_id, |
| 496 |
uint32_t buflen) { |
| 497 |
struct ssh2_channel_databuf *db; |
| 498 |
struct ssh2_channel *chan; |
| 499 |
|
| 500 |
db = get_databuf(channel_id, buflen); |
| 501 |
if (db == NULL) { |
| 502 |
return NULL; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
chan = get_channel(channel_id); |
| 509 |
if (chan->outgoing_tail != NULL) { |
| 510 |
chan->outgoing_tail->next = db; |
| 511 |
chan->outgoing_tail = db; |
| 512 |
|
| 513 |
} else { |
| 514 |
chan->outgoing_head = chan->outgoing_tail = db; |
| 515 |
} |
| 516 |
|
| 517 |
return db; |
| 518 |
} |
| 519 |
|
| 520 |
static int read_channel_open(struct ssh2_packet *pkt, uint32_t *channel_id) { |
| 521 |
unsigned char *buf; |
| 522 |
char *channel_type; |
| 523 |
uint32_t buflen, initial_windowsz, max_packetsz; |
| 524 |
cmd_rec *cmd; |
| 525 |
|
| 526 |
buf = pkt->payload; |
| 527 |
buflen = pkt->payload_len; |
| 528 |
|
| 529 |
channel_type = sftp_msg_read_string(pkt->pool, &buf, &buflen); |
| 530 |
*channel_id = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
if (channel_count + 1 > channel_max) { |
| 536 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 537 |
"maximum number of channels (%u) open, denying request to " |
| 538 |
"open '%s' channel", channel_count, channel_type); |
| 539 |
errno = EPERM; |
| 540 |
return -1; |
| 541 |
} |
| 542 |
|
| 543 |
initial_windowsz = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 544 |
max_packetsz = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 545 |
|
| 546 |
pr_trace_msg(trace_channel, 8, "open of '%s' channel using remote " |
| 547 |
"ID %lu requested: initial client window len = %lu bytes, client max " |
| 548 |
"packet size = %lu bytes", channel_type, (unsigned long) *channel_id, |
| 549 |
(unsigned long) initial_windowsz, (unsigned long) max_packetsz); |
| 550 |
|
| 551 |
cmd = pr_cmd_alloc(pkt->pool, 2, pstrdup(pkt->pool, "CHANNEL_OPEN"), |
| 552 |
pstrdup(pkt->pool, channel_type)); |
| 553 |
cmd->arg = channel_type; |
| 554 |
cmd->cmd_class = CL_MISC|CL_SSH; |
| 555 |
cmd->cmd_id = SFTP_CMD_ID; |
| 556 |
|
| 557 |
if (strncmp(channel_type, "session", 8) != 0) { |
| 558 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 559 |
"unsupported channel type '%s' requested, denying", channel_type); |
| 560 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 561 |
errno = ENOENT; |
| 562 |
return -1; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
if (max_packetsz == 0) { |
| 569 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 570 |
"unsupported channel max packet size %lu requested, denying", |
| 571 |
(unsigned long) max_packetsz); |
| 572 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 573 |
errno = EINVAL; |
| 574 |
return -1; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
if (initial_windowsz == 0) { |
| 579 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 580 |
"unsupported channel initial window size %lu requested, denying", |
| 581 |
(unsigned long) initial_windowsz); |
| 582 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 583 |
errno = EINVAL; |
| 584 |
return -1; |
| 585 |
} |
| 586 |
|
| 587 |
if (alloc_channel(channel_type, *channel_id, initial_windowsz, |
| 588 |
max_packetsz) == NULL) { |
| 589 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 590 |
"error allocating channel"); |
| 591 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 592 |
return -1; |
| 593 |
} |
| 594 |
|
| 595 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
static int handle_channel_close(struct ssh2_packet *pkt) { |
| 600 |
char chan_str[16]; |
| 601 |
unsigned char *buf; |
| 602 |
uint32_t buflen, channel_id; |
| 603 |
struct ssh2_channel *chan; |
| 604 |
cmd_rec *cmd; |
| 605 |
|
| 606 |
buf = pkt->payload; |
| 607 |
buflen = pkt->payload_len; |
| 608 |
|
| 609 |
channel_id = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 610 |
|
| 611 |
memset(chan_str, '\0', sizeof(chan_str)); |
| 612 |
pr_snprintf(chan_str, sizeof(chan_str)-1, "%lu", (unsigned long) channel_id); |
| 613 |
|
| 614 |
cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "CHANNEL_CLOSE")); |
| 615 |
cmd->arg = pstrdup(pkt->pool, chan_str); |
| 616 |
cmd->cmd_class = CL_MISC|CL_SSH; |
| 617 |
cmd->cmd_id = SFTP_CMD_ID; |
| 618 |
|
| 619 |
chan = get_channel(channel_id); |
| 620 |
if (chan == NULL) { |
| 621 |
pr_trace_msg(trace_channel, 8, "unable to close channel ID %lu: %s", |
| 622 |
(unsigned long) channel_id, strerror(errno)); |
| 623 |
|
| 624 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 625 |
"no open channel for channel ID %lu", (unsigned long) channel_id); |
| 626 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 627 |
return -1; |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
if (chan->sent_close == FALSE) { |
| 636 |
send_channel_done(pkt->pool, channel_id); |
| 637 |
} |
| 638 |
|
| 639 |
chan->recvd_close = TRUE; |
| 640 |
destroy_channel(channel_id); |
| 641 |
|
| 642 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 643 |
return 0; |
| 644 |
} |
| 645 |
|
| 646 |
static int process_channel_data(struct ssh2_channel *chan, |
| 647 |
struct ssh2_packet *pkt, unsigned char *data, uint32_t datalen) { |
| 648 |
int res; |
| 649 |
|
| 650 |
if (chan->handle_packet == NULL) { |
| 651 |
pr_trace_msg(trace_channel, 3, "no handler registered for data on " |
| 652 |
"channel ID %lu, rejecting packet", |
| 653 |
(unsigned long) chan->local_channel_id); |
| 654 |
errno = EACCES; |
| 655 |
return -1; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
if (chan->remote_windowsz == 0) { |
| 665 |
struct ssh2_channel_databuf *db; |
| 666 |
const char *reason = "remote window size too small"; |
| 667 |
|
| 668 |
db = get_incoming_databuf(chan->local_channel_id, datalen); |
| 669 |
db->buflen = datalen; |
| 670 |
if (datalen > 0) { |
| 671 |
memcpy(db->buf, data, datalen); |
| 672 |
} |
| 673 |
chan->incoming_len += datalen; |
| 674 |
|
| 675 |
pr_trace_msg(trace_channel, 8, "buffering %lu bytes of incoming data (%s)", |
| 676 |
(unsigned long) datalen, reason); |
| 677 |
return 0; |
| 678 |
} |
| 679 |
|
| 680 |
res = chan->handle_packet(pkt->pool, pkt, chan->local_channel_id, data, |
| 681 |
datalen); |
| 682 |
|
| 683 |
chan->local_windowsz -= datalen; |
| 684 |
send_channel_window_adjust(chan); |
| 685 |
|
| 686 |
return res; |
| 687 |
} |
| 688 |
|
| 689 |
static int handle_channel_data(struct ssh2_packet *pkt, uint32_t *channel_id) { |
| 690 |
unsigned char *buf, *data; |
| 691 |
uint32_t buflen, datalen; |
| 692 |
struct ssh2_channel *chan; |
| 693 |
|
| 694 |
buf = pkt->payload; |
| 695 |
buflen = pkt->payload_len; |
| 696 |
|
| 697 |
*channel_id = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 698 |
|
| 699 |
chan = get_channel(*channel_id); |
| 700 |
if (chan == NULL) { |
| 701 |
pr_trace_msg(trace_channel, 8, "unable to handle data for " |
| 702 |
"channel ID %lu: %s", (unsigned long) *channel_id, strerror(errno)); |
| 703 |
|
| 704 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 705 |
"no open channel for remote channel ID %lu", (unsigned long) *channel_id); |
| 706 |
return -1; |
| 707 |
} |
| 708 |
|
| 709 |
if (chan->recvd_eof == TRUE) { |
| 710 |
pr_trace_msg(trace_channel, 3, "received data on channel ID %lu after " |
| 711 |
"client had sent CHANNEL_EOF", (unsigned long) *channel_id); |
| 712 |
} |
| 713 |
|
| 714 |
datalen = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 715 |
|
| 716 |
if (datalen > chan->local_windowsz) { |
| 717 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 718 |
"received too much data (%lu bytes) for local window size (%lu bytes) " |
| 719 |
"for channel ID %lu, ignoring CHANNEL_DATA message", |
| 720 |
(unsigned long) datalen, (unsigned long) chan->local_windowsz, |
| 721 |
(unsigned long) *channel_id); |
| 722 |
return 0; |
| 723 |
} |
| 724 |
|
| 725 |
pr_trace_msg(trace_channel, 17, |
| 726 |
"processing %lu %s of data for channel ID %lu", (unsigned long) datalen, |
| 727 |
datalen != 1 ? "bytes" : "byte", (unsigned long) *channel_id); |
| 728 |
data = sftp_msg_read_data_direct(pkt->pool, &buf, &buflen, datalen); |
| 729 |
|
| 730 |
return process_channel_data(chan, pkt, data, datalen); |
| 731 |
} |
| 732 |
|
| 733 |
static int send_channel_window_adjust(struct ssh2_channel *chan) { |
| 734 |
unsigned char *buf, *ptr; |
| 735 |
uint32_t buflen, bufsz, window_adjlen; |
| 736 |
struct ssh2_packet *resp; |
| 737 |
pool *tmp_pool; |
| 738 |
|
| 739 |
if (chan->local_windowsz >= (chan->local_max_packetsz * 3)) { |
| 740 |
return 0; |
| 741 |
} |
| 742 |
|
| 743 |
tmp_pool = make_sub_pool(chan->pool); |
| 744 |
pr_pool_tag(tmp_pool, "SSH2 send channel window adjust pool"); |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
buflen = bufsz = 128; |
| 750 |
ptr = buf = palloc(tmp_pool, bufsz); |
| 751 |
|
| 752 |
window_adjlen = chan_window_size - chan->local_windowsz; |
| 753 |
|
| 754 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_CHANNEL_WINDOW_ADJUST); |
| 755 |
sftp_msg_write_int(&buf, &buflen, chan->remote_channel_id); |
| 756 |
sftp_msg_write_int(&buf, &buflen, window_adjlen); |
| 757 |
|
| 758 |
pr_trace_msg(trace_channel, 15, "sending CHANNEL_WINDOW_ADJUST message " |
| 759 |
"for channel ID %lu, adding %lu bytes to the window size (currently %lu " |
| 760 |
"bytes)", (unsigned long) chan->local_channel_id, |
| 761 |
(unsigned long) window_adjlen, (unsigned long) chan->local_windowsz); |
| 762 |
|
| 763 |
resp = sftp_ssh2_packet_create(tmp_pool); |
| 764 |
resp->payload = ptr; |
| 765 |
resp->payload_len = (bufsz - buflen); |
| 766 |
|
| 767 |
if (sftp_ssh2_packet_write(sftp_conn->wfd, resp) < 0) { |
| 768 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 769 |
"error sending CHANNEL_WINDOW_ADJUST request to client: %s", |
| 770 |
strerror(errno)); |
| 771 |
} |
| 772 |
|
| 773 |
destroy_pool(tmp_pool); |
| 774 |
chan->local_windowsz += window_adjlen; |
| 775 |
|
| 776 |
return 0; |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
static int send_channel_done(pool *p, uint32_t channel_id) { |
| 783 |
int res; |
| 784 |
unsigned char *buf, *ptr; |
| 785 |
uint32_t buflen, bufsz; |
| 786 |
struct ssh2_channel *chan; |
| 787 |
struct ssh2_packet *pkt; |
| 788 |
|
| 789 |
chan = get_channel(channel_id); |
| 790 |
if (chan == NULL) { |
| 791 |
return 0; |
| 792 |
} |
| 793 |
|
| 794 |
buflen = bufsz = 128; |
| 795 |
ptr = buf = palloc(p, bufsz); |
| 796 |
|
| 797 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_CHANNEL_REQUEST); |
| 798 |
sftp_msg_write_int(&buf, &buflen, chan->remote_channel_id); |
| 799 |
sftp_msg_write_string(&buf, &buflen, "exit-status"); |
| 800 |
sftp_msg_write_bool(&buf, &buflen, FALSE); |
| 801 |
sftp_msg_write_int(&buf, &buflen, 0); |
| 802 |
|
| 803 |
pkt = sftp_ssh2_packet_create(p); |
| 804 |
pkt->payload = ptr; |
| 805 |
pkt->payload_len = (bufsz - buflen); |
| 806 |
|
| 807 |
pr_trace_msg(trace_channel, 9, |
| 808 |
"sending CHANNEL_REQUEST (remote channel ID %lu, exit status 0)", |
| 809 |
(unsigned long) chan->remote_channel_id); |
| 810 |
|
| 811 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 812 |
if (res < 0) { |
| 813 |
destroy_pool(pkt->pool); |
| 814 |
return res; |
| 815 |
} |
| 816 |
|
| 817 |
if (chan->sent_eof == FALSE) { |
| 818 |
buf = ptr; |
| 819 |
buflen = bufsz; |
| 820 |
|
| 821 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_CHANNEL_EOF); |
| 822 |
sftp_msg_write_int(&buf, &buflen, chan->remote_channel_id); |
| 823 |
|
| 824 |
pkt = sftp_ssh2_packet_create(p); |
| 825 |
pkt->payload = ptr; |
| 826 |
pkt->payload_len = (bufsz - buflen); |
| 827 |
|
| 828 |
pr_trace_msg(trace_channel, 9, |
| 829 |
"sending CHANNEL_EOF (remote channel ID %lu)", |
| 830 |
(unsigned long) chan->remote_channel_id); |
| 831 |
|
| 832 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 833 |
if (res < 0) { |
| 834 |
destroy_pool(pkt->pool); |
| 835 |
return res; |
| 836 |
} |
| 837 |
|
| 838 |
chan->sent_eof = TRUE; |
| 839 |
} |
| 840 |
|
| 841 |
if (chan->sent_close == FALSE) { |
| 842 |
buf = ptr; |
| 843 |
buflen = bufsz; |
| 844 |
|
| 845 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_CHANNEL_CLOSE); |
| 846 |
sftp_msg_write_int(&buf, &buflen, chan->remote_channel_id); |
| 847 |
|
| 848 |
pkt->payload = ptr; |
| 849 |
pkt->payload_len = (bufsz - buflen); |
| 850 |
|
| 851 |
pr_trace_msg(trace_channel, 9, |
| 852 |
"sending CHANNEL_CLOSE (remote channel ID %lu)", |
| 853 |
(unsigned long) chan->remote_channel_id); |
| 854 |
|
| 855 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 856 |
if (res < 0) { |
| 857 |
destroy_pool(pkt->pool); |
| 858 |
return res; |
| 859 |
} |
| 860 |
|
| 861 |
destroy_pool(pkt->pool); |
| 862 |
chan->sent_close = TRUE; |
| 863 |
} |
| 864 |
|
| 865 |
destroy_channel(channel_id); |
| 866 |
return res; |
| 867 |
} |
| 868 |
|
| 869 |
static int handle_channel_eof(struct ssh2_packet *pkt) { |
| 870 |
char chan_str[16]; |
| 871 |
unsigned char *buf; |
| 872 |
uint32_t buflen, channel_id; |
| 873 |
struct ssh2_channel *chan; |
| 874 |
cmd_rec *cmd; |
| 875 |
|
| 876 |
buf = pkt->payload; |
| 877 |
buflen = pkt->payload_len; |
| 878 |
|
| 879 |
channel_id = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 880 |
|
| 881 |
memset(chan_str, '\0', sizeof(chan_str)); |
| 882 |
pr_snprintf(chan_str, sizeof(chan_str)-1, "%lu", (unsigned long) channel_id); |
| 883 |
|
| 884 |
cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "CHANNEL_EOF")); |
| 885 |
cmd->arg = pstrdup(pkt->pool, chan_str); |
| 886 |
cmd->cmd_class = CL_MISC|CL_SSH; |
| 887 |
cmd->cmd_id = SFTP_CMD_ID; |
| 888 |
|
| 889 |
chan = get_channel(channel_id); |
| 890 |
if (chan == NULL) { |
| 891 |
pr_trace_msg(trace_channel, 8, "unable to handle EOF for " |
| 892 |
"channel ID %lu: %s", (unsigned long) channel_id, strerror(errno)); |
| 893 |
|
| 894 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 895 |
"no open channel for remote channel ID %lu", (unsigned long) channel_id); |
| 896 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 897 |
return -1; |
| 898 |
} |
| 899 |
|
| 900 |
|
| 901 |
chan->recvd_eof = TRUE; |
| 902 |
|
| 903 |
|
| 904 |
drain_pending_outgoing_channel_data(channel_id); |
| 905 |
|
| 906 |
if (chan->sent_eof == FALSE) { |
| 907 |
send_channel_done(pkt->pool, channel_id); |
| 908 |
} |
| 909 |
|
| 910 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 911 |
return 0; |
| 912 |
} |
| 913 |
|
| 914 |
static int allow_env(const char *key) { |
| 915 |
register unsigned int i; |
| 916 |
char **elts; |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
const char *prohibited_envs[] = { |
| 927 |
"DYLD_LIBRARY_PATH", |
| 928 |
"HOME", |
| 929 |
"LD_CONFIG", |
| 930 |
"LD_CONFIG_32", |
| 931 |
"LD_CONFIG_64", |
| 932 |
"LD_LIBMAP", |
| 933 |
"LD_LIBRARY_PATH", |
| 934 |
"LD_NOCONFIG", |
| 935 |
"LD_NOCONFIG_32", |
| 936 |
"LD_NOCONFIG_64", |
| 937 |
"LD_PRELOAD", |
| 938 |
"LD_RUN_PATH", |
| 939 |
"LIBPATH", |
| 940 |
"PATH", |
| 941 |
"SFTP", |
| 942 |
"SFTP_LIBRARY_VERSION", |
| 943 |
"SFTP_CLIENT_CIPHER_ALGO", |
| 944 |
"SFTP_CLIENT_MAC_ALGO", |
| 945 |
"SFTP_CLIENT_COMPRESSION_ALGO", |
| 946 |
"SFTP_KEX_ALGO", |
| 947 |
"SFTP_SERVER_CIPHER_ALGO", |
| 948 |
"SFTP_SERVER_MAC_ALGO", |
| 949 |
"SFTP_SERVER_COMPRESSION_ALGO", |
| 950 |
"SHLIB_PATH", |
| 951 |
"TMP", |
| 952 |
"TMPDIR", |
| 953 |
"TZ", |
| 954 |
"USER", |
| 955 |
NULL |
| 956 |
}; |
| 957 |
|
| 958 |
for (i = 0; prohibited_envs[i]; i++) { |
| 959 |
if (strcasecmp(key, prohibited_envs[i]) == 0) { |
| 960 |
return FALSE; |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
elts = accepted_envs->elts; |
| 965 |
for (i = 0; i < accepted_envs->nelts; i++) { |
| 966 |
if (pr_fnmatch(elts[i], key, 0) == 0) { |
| 967 |
return TRUE; |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
return FALSE; |
| 973 |
} |
| 974 |
|
| 975 |
static int handle_exec_channel(struct ssh2_channel *chan, |
| 976 |
struct ssh2_packet *pkt, unsigned char **buf, uint32_t *buflen) { |
| 977 |
register unsigned int i; |
| 978 |
int flags = PR_STR_FL_PRESERVE_WHITESPACE, have_handler = FALSE; |
| 979 |
char *command, *ptr, *word; |
| 980 |
array_header *req; |
| 981 |
struct ssh2_channel_exec_handler **handlers; |
| 982 |
|
| 983 |
command = sftp_msg_read_string(pkt->pool, buf, buflen); |
| 984 |
|
| 985 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 986 |
"'exec' channel request: command = '%s'", command); |
| 987 |
|
| 988 |
req = make_array(pkt->pool, 2, sizeof(char *)); |
| 989 |
ptr = command; |
| 990 |
|
| 991 |
while ((word = pr_str_get_word(&ptr, flags)) != NULL) { |
| 992 |
pr_signals_handle(); |
| 993 |
*((char **) push_array(req)) = pstrdup(pkt->pool, word); |
| 994 |
} |
| 995 |
|
| 996 |
*((char **) push_array(req)) = NULL; |
| 997 |
|
| 998 |
handlers = channel_exec_handlers->elts; |
| 999 |
for (i = 0; i < channel_exec_handlers->nelts; i++) { |
| 1000 |
struct ssh2_channel_exec_handler *handler; |
| 1001 |
|
| 1002 |
handler = handlers[i]; |
| 1003 |
|
| 1004 |
pr_trace_msg(trace_channel, 18, |
| 1005 |
"checking exec command '%s' against handler registered by 'mod_%s.c'", |
| 1006 |
command, handler->m->name); |
| 1007 |
|
| 1008 |
if (strcmp(command, handler->command) == 0) { |
| 1009 |
int res; |
| 1010 |
|
| 1011 |
pr_trace_msg(trace_channel, 18, |
| 1012 |
"found '%s' exec handler registered by 'mod_%s.c'", |
| 1013 |
command, handler->m->name); |
| 1014 |
|
| 1015 |
res = (handler->set_params)(pkt->pool, chan->local_channel_id, req); |
| 1016 |
if (res < 0) { |
| 1017 |
int xerrno = errno; |
| 1018 |
|
| 1019 |
pr_trace_msg(trace_channel, 18, "'set_params' callback error: %s", |
| 1020 |
strerror(xerrno)); |
| 1021 |
|
| 1022 |
errno = xerrno; |
| 1023 |
return -1; |
| 1024 |
} |
| 1025 |
|
| 1026 |
chan->prepare = handler->prepare; |
| 1027 |
chan->postopen = handler->postopen; |
| 1028 |
chan->handle_packet = handler->handle_packet; |
| 1029 |
chan->finish = handler->finish; |
| 1030 |
|
| 1031 |
have_handler = TRUE; |
| 1032 |
break; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
if (!have_handler) { |
| 1037 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1038 |
"unsupported exec command '%s'", command); |
| 1039 |
return -1; |
| 1040 |
} |
| 1041 |
|
| 1042 |
return 0; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static int handle_env_channel(struct ssh2_channel *chan, |
| 1046 |
struct ssh2_packet *pkt, unsigned char **buf, uint32_t *buflen) { |
| 1047 |
int res; |
| 1048 |
char *key, *value; |
| 1049 |
|
| 1050 |
key = sftp_msg_read_string(pkt->pool, buf, buflen); |
| 1051 |
value = sftp_msg_read_string(pkt->pool, buf, buflen); |
| 1052 |
|
| 1053 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1054 |
"'env' channel request: '%s' = '%s'", key, value); |
| 1055 |
|
| 1056 |
if (allow_env(key) == TRUE) { |
| 1057 |
res = pr_env_set(sftp_pool, pstrdup(session.pool, key), |
| 1058 |
pstrdup(session.pool, value)); |
| 1059 |
if (res < 0) { |
| 1060 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1061 |
"error setting environment variable '%s' with value '%s': %s", |
| 1062 |
key, value, strerror(errno)); |
| 1063 |
} |
| 1064 |
|
| 1065 |
} else { |
| 1066 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1067 |
"environment variable '%s' prohibited by policy", key); |
| 1068 |
res = -1; |
| 1069 |
} |
| 1070 |
|
| 1071 |
return res; |
| 1072 |
} |
| 1073 |
|
| 1074 |
static int handle_signal_channel(struct ssh2_channel *chan, |
| 1075 |
struct ssh2_packet *pkt, unsigned char **buf, uint32_t *buflen) { |
| 1076 |
int res; |
| 1077 |
char b, *sig_name; |
| 1078 |
|
| 1079 |
b = sftp_msg_read_bool(pkt->pool, buf, buflen); |
| 1080 |
if (b != FALSE) { |
| 1081 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1082 |
"malformed 'signal' request (bool must be FALSE)"); |
| 1083 |
} |
| 1084 |
|
| 1085 |
sig_name = sftp_msg_read_string(pkt->pool, buf, buflen); |
| 1086 |
|
| 1087 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1088 |
"'signal' channel request: SIG%s", sig_name); |
| 1089 |
|
| 1090 |
if (strcmp(sig_name, "ABRT") == 0) { |
| 1091 |
res = raise(SIGABRT); |
| 1092 |
|
| 1093 |
} else if (strcmp(sig_name, "ALRM") == 0) { |
| 1094 |
res = raise(SIGALRM); |
| 1095 |
|
| 1096 |
#if defined(SIGFPE) |
| 1097 |
} else if (strcmp(sig_name, "FPE") == 0) { |
| 1098 |
res = raise(SIGFPE); |
| 1099 |
#endif |
| 1100 |
|
| 1101 |
} else if (strcmp(sig_name, "HUP") == 0) { |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
res = 0; |
| 1106 |
|
| 1107 |
#if defined(SIGILL) |
| 1108 |
} else if (strcmp(sig_name, "ILL") == 0) { |
| 1109 |
res = raise(SIGILL); |
| 1110 |
#endif |
| 1111 |
|
| 1112 |
} else if (strcmp(sig_name, "INT") == 0) { |
| 1113 |
res = raise(SIGINT); |
| 1114 |
|
| 1115 |
} else if (strcmp(sig_name, "KILL") == 0) { |
| 1116 |
res = raise(SIGKILL); |
| 1117 |
|
| 1118 |
} else if (strcmp(sig_name, "PIPE") == 0) { |
| 1119 |
|
| 1120 |
res = 0; |
| 1121 |
|
| 1122 |
} else if (strcmp(sig_name, "QUIT") == 0) { |
| 1123 |
res = raise(SIGQUIT); |
| 1124 |
|
| 1125 |
} else if (strcmp(sig_name, "SEGV") == 0) { |
| 1126 |
res = raise(SIGSEGV); |
| 1127 |
|
| 1128 |
} else if (strcmp(sig_name, "TERM") == 0) { |
| 1129 |
res = raise(SIGTERM); |
| 1130 |
|
| 1131 |
} else if (strcmp(sig_name, "USR1") == 0 || |
| 1132 |
strcmp(sig_name, "USR2") == 0) { |
| 1133 |
|
| 1134 |
res = 0; |
| 1135 |
|
| 1136 |
} else { |
| 1137 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1138 |
"unknown signal name 'SIG%s'", sig_name); |
| 1139 |
res = -1; |
| 1140 |
} |
| 1141 |
|
| 1142 |
return res; |
| 1143 |
} |
| 1144 |
|
| 1145 |
static int handle_subsystem_channel(struct ssh2_channel *chan, |
| 1146 |
struct ssh2_packet *pkt, unsigned char **buf, uint32_t *buflen) { |
| 1147 |
char *subsystem; |
| 1148 |
|
| 1149 |
subsystem = sftp_msg_read_string(pkt->pool, buf, buflen); |
| 1150 |
|
| 1151 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1152 |
"'subsystem' channel request for '%s' subsystem", subsystem); |
| 1153 |
|
| 1154 |
if (strncmp(subsystem, "sftp", 5) == 0) { |
| 1155 |
|
| 1156 |
if (sftp_services & SFTP_SERVICE_FL_SFTP) { |
| 1157 |
chan->prepare = sftp_fxp_open_session; |
| 1158 |
chan->postopen = NULL; |
| 1159 |
chan->handle_packet = sftp_fxp_handle_packet; |
| 1160 |
chan->finish = sftp_fxp_close_session; |
| 1161 |
|
| 1162 |
} else { |
| 1163 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1164 |
"'%s' subsystem denied by Protocols config", subsystem); |
| 1165 |
return -1; |
| 1166 |
} |
| 1167 |
|
| 1168 |
} else { |
| 1169 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1170 |
"subsystem '%s' unsupported", subsystem); |
| 1171 |
return -1; |
| 1172 |
} |
| 1173 |
|
| 1174 |
return 0; |
| 1175 |
} |
| 1176 |
|
| 1177 |
static int handle_channel_req(struct ssh2_packet *pkt) { |
| 1178 |
unsigned char *buf; |
| 1179 |
char *channel_request; |
| 1180 |
uint32_t buflen, channel_id; |
| 1181 |
int res, unsupported = FALSE, want_reply; |
| 1182 |
struct ssh2_channel *chan; |
| 1183 |
cmd_rec *cmd; |
| 1184 |
|
| 1185 |
buf = pkt->payload; |
| 1186 |
buflen = pkt->payload_len; |
| 1187 |
|
| 1188 |
channel_id = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 1189 |
channel_request = sftp_msg_read_string(pkt->pool, &buf, &buflen); |
| 1190 |
want_reply = sftp_msg_read_bool(pkt->pool, &buf, &buflen); |
| 1191 |
|
| 1192 |
pr_trace_msg(trace_channel, 7, |
| 1193 |
"received '%s' request for channel ID %lu, want reply = %s", |
| 1194 |
channel_request, (unsigned long) channel_id, |
| 1195 |
want_reply ? "true" : "false"); |
| 1196 |
|
| 1197 |
cmd = pr_cmd_alloc(pkt->pool, 2, pstrdup(pkt->pool, "CHANNEL_REQUEST"), |
| 1198 |
pstrdup(pkt->pool, channel_request)); |
| 1199 |
cmd->arg = channel_request; |
| 1200 |
cmd->cmd_class = CL_MISC|CL_SSH; |
| 1201 |
cmd->cmd_id = SFTP_CMD_ID; |
| 1202 |
|
| 1203 |
chan = get_channel(channel_id); |
| 1204 |
if (chan == NULL) { |
| 1205 |
pr_trace_msg(trace_channel, 8, "unable to handle request for " |
| 1206 |
"channel ID %lu: %s", (unsigned long) channel_id, strerror(errno)); |
| 1207 |
|
| 1208 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1209 |
"no open channel for remote channel ID %lu", (unsigned long) channel_id); |
| 1210 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1211 |
return -1; |
| 1212 |
} |
| 1213 |
|
| 1214 |
if (strncmp(channel_request, "subsystem", 10) == 0) { |
| 1215 |
res = handle_subsystem_channel(chan, pkt, &buf, &buflen); |
| 1216 |
|
| 1217 |
} else if (strncmp(channel_request, "exec", 5) == 0) { |
| 1218 |
res = handle_exec_channel(chan, pkt, &buf, &buflen); |
| 1219 |
|
| 1220 |
} else if (strncmp(channel_request, "env", 4) == 0) { |
| 1221 |
res = handle_env_channel(chan, pkt, &buf, &buflen); |
| 1222 |
|
| 1223 |
} else if (strncmp(channel_request, "signal", 7) == 0) { |
| 1224 |
res = handle_signal_channel(chan, pkt, &buf, &buflen); |
| 1225 |
|
| 1226 |
} else if (strncmp(channel_request, "break", 6) == 0) { |
| 1227 |
uint32_t breaklen; |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
breaklen = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 1234 |
|
| 1235 |
pr_trace_msg(trace_channel, 10, |
| 1236 |
"received '%s' request for %lu millisecs, ignoring", channel_request, |
| 1237 |
(unsigned long) breaklen); |
| 1238 |
|
| 1239 |
res = -1; |
| 1240 |
|
| 1241 |
} else { |
| 1242 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1243 |
"unsupported '%s' channel requested, ignoring", channel_request); |
| 1244 |
res = -1; |
| 1245 |
unsupported = TRUE; |
| 1246 |
} |
| 1247 |
|
| 1248 |
if (res == 0 && |
| 1249 |
chan->prepare) { |
| 1250 |
if ((chan->prepare)(chan->local_channel_id) < 0) { |
| 1251 |
int xerrno = errno; |
| 1252 |
|
| 1253 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1254 |
"unable to prepare channel ID %lu: %s", |
| 1255 |
(unsigned long) chan->local_channel_id, strerror(xerrno)); |
| 1256 |
|
| 1257 |
errno = xerrno; |
| 1258 |
res = -1; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
if (want_reply) { |
| 1263 |
struct ssh2_packet *pkt2; |
| 1264 |
unsigned char *buf2, *ptr2; |
| 1265 |
uint32_t buflen2, bufsz2; |
| 1266 |
|
| 1267 |
buflen2 = bufsz2 = 128; |
| 1268 |
buf2 = ptr2 = palloc(pkt->pool, bufsz2); |
| 1269 |
|
| 1270 |
if (res < 0) { |
| 1271 |
sftp_msg_write_byte(&buf2, &buflen2, SFTP_SSH2_MSG_CHANNEL_FAILURE); |
| 1272 |
|
| 1273 |
} else { |
| 1274 |
sftp_msg_write_byte(&buf2, &buflen2, SFTP_SSH2_MSG_CHANNEL_SUCCESS); |
| 1275 |
} |
| 1276 |
|
| 1277 |
sftp_msg_write_int(&buf2, &buflen2, chan->remote_channel_id); |
| 1278 |
|
| 1279 |
pkt2 = sftp_ssh2_packet_create(pkt->pool); |
| 1280 |
pkt2->payload = ptr2; |
| 1281 |
pkt2->payload_len = (bufsz2 - buflen2); |
| 1282 |
|
| 1283 |
if (sftp_ssh2_packet_write(sftp_conn->wfd, pkt2) < 0) { |
| 1284 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1285 |
"error sending reply to CHANNEL_REQUEST: %s", strerror(errno)); |
| 1286 |
} |
| 1287 |
|
| 1288 |
destroy_pool(pkt2->pool); |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
if (res == 0 && |
| 1293 |
chan->postopen) { |
| 1294 |
int pres; |
| 1295 |
|
| 1296 |
pr_trace_msg(trace_channel, 18, |
| 1297 |
"calling '%s' handler postopen callback", channel_request); |
| 1298 |
|
| 1299 |
pres = (chan->postopen)(chan->local_channel_id); |
| 1300 |
if (pres < 0) { |
| 1301 |
int xerrno = errno; |
| 1302 |
|
| 1303 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1304 |
"postopen error on channel ID %lu: %s", |
| 1305 |
(unsigned long) chan->local_channel_id, strerror(xerrno)); |
| 1306 |
|
| 1307 |
} else if (pres == 1) { |
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
pr_trace_msg(trace_channel, 18, |
| 1315 |
"sending CHANNEL_CLOSE for '%s', due to postopen return value", |
| 1316 |
channel_request); |
| 1317 |
send_channel_done(pkt->pool, chan->local_channel_id); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
if (!unsupported && |
| 1328 |
res < 0) { |
| 1329 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1330 |
return 0; |
| 1331 |
} |
| 1332 |
|
| 1333 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1334 |
return 0; |
| 1335 |
} |
| 1336 |
|
| 1337 |
static int handle_channel_window_adjust(struct ssh2_packet *pkt) { |
| 1338 |
char adjust_str[32]; |
| 1339 |
unsigned char *buf; |
| 1340 |
uint32_t buflen, channel_id, adjust_len, max_adjust_len; |
| 1341 |
struct ssh2_channel *chan; |
| 1342 |
cmd_rec *cmd; |
| 1343 |
|
| 1344 |
buf = pkt->payload; |
| 1345 |
buflen = pkt->payload_len; |
| 1346 |
|
| 1347 |
channel_id = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 1348 |
adjust_len = sftp_msg_read_int(pkt->pool, &buf, &buflen); |
| 1349 |
|
| 1350 |
memset(adjust_str, '\0', sizeof(adjust_str)); |
| 1351 |
pr_snprintf(adjust_str, sizeof(adjust_str)-1, "%lu %lu", |
| 1352 |
(unsigned long) channel_id, (unsigned long) adjust_len); |
| 1353 |
|
| 1354 |
cmd = pr_cmd_alloc(pkt->pool, 1, pstrdup(pkt->pool, "CHANNEL_WINDOW_ADJUST")); |
| 1355 |
cmd->arg = pstrdup(pkt->pool, adjust_str); |
| 1356 |
cmd->cmd_class = CL_MISC|CL_SSH; |
| 1357 |
cmd->cmd_id = SFTP_CMD_ID; |
| 1358 |
|
| 1359 |
chan = get_channel(channel_id); |
| 1360 |
if (chan == NULL) { |
| 1361 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1362 |
"no open channel for channel ID %lu", (unsigned long) channel_id); |
| 1363 |
pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1364 |
return -1; |
| 1365 |
} |
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
max_adjust_len = SFTP_SSH2_CHANNEL_WINDOW_SIZE - chan->remote_windowsz; |
| 1378 |
|
| 1379 |
if (adjust_len > max_adjust_len) { |
| 1380 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1381 |
"received WINDOW_ADJUST message whose window size adjustment (%lu bytes) " |
| 1382 |
"exceeds max possible adjustment (%lu bytes), trimming", |
| 1383 |
(unsigned long) adjust_len, (unsigned long) max_adjust_len); |
| 1384 |
adjust_len = max_adjust_len; |
| 1385 |
} |
| 1386 |
|
| 1387 |
pr_trace_msg(trace_channel, 15, "adjusting remote window size " |
| 1388 |
"for local channel ID %lu, adding %lu bytes to current window size " |
| 1389 |
"(%lu bytes)", (unsigned long) channel_id, (unsigned long) adjust_len, |
| 1390 |
(unsigned long) chan->remote_windowsz); |
| 1391 |
|
| 1392 |
chan->remote_windowsz += adjust_len; |
| 1393 |
|
| 1394 |
drain_pending_outgoing_channel_data(channel_id); |
| 1395 |
drain_pending_incoming_channel_data(channel_id); |
| 1396 |
|
| 1397 |
pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1398 |
return 0; |
| 1399 |
} |
| 1400 |
|
| 1401 |
static int write_channel_open_confirm(struct ssh2_packet *pkt, |
| 1402 |
uint32_t channel_id) { |
| 1403 |
register unsigned int i; |
| 1404 |
unsigned char *buf, *ptr; |
| 1405 |
uint32_t buflen, bufsz; |
| 1406 |
struct ssh2_channel *chan = NULL, **chans; |
| 1407 |
|
| 1408 |
chans = channel_list->elts; |
| 1409 |
for (i = 0; i < channel_list->nelts; i++) { |
| 1410 |
if (chans[i] != NULL && |
| 1411 |
chans[i]->remote_channel_id == channel_id) { |
| 1412 |
chan = chans[i]; |
| 1413 |
break; |
| 1414 |
} |
| 1415 |
} |
| 1416 |
|
| 1417 |
if (chan == NULL) { |
| 1418 |
pr_trace_msg(trace_channel, 8, "unable to confirm open channel ID %lu: %s", |
| 1419 |
(unsigned long) channel_id, strerror(errno)); |
| 1420 |
|
| 1421 |
return -1; |
| 1422 |
} |
| 1423 |
|
| 1424 |
buflen = bufsz = 1024; |
| 1425 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 1426 |
|
| 1427 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 1428 |
sftp_msg_write_int(&buf, &buflen, chan->remote_channel_id); |
| 1429 |
sftp_msg_write_int(&buf, &buflen, chan->local_channel_id); |
| 1430 |
sftp_msg_write_int(&buf, &buflen, chan->local_windowsz); |
| 1431 |
sftp_msg_write_int(&buf, &buflen, chan->local_max_packetsz); |
| 1432 |
|
| 1433 |
pr_trace_msg(trace_channel, 8, "confirm open channel remote ID %lu, " |
| 1434 |
"local ID %lu: initial server window len = %lu bytes, server max " |
| 1435 |
"packet size = %lu bytes", (unsigned long) chan->remote_channel_id, |
| 1436 |
(unsigned long) chan->local_channel_id, (unsigned long) |
| 1437 |
chan->local_windowsz, (unsigned long) chan->local_max_packetsz); |
| 1438 |
|
| 1439 |
pkt->payload = ptr; |
| 1440 |
pkt->payload_len = (bufsz - buflen); |
| 1441 |
|
| 1442 |
return 0; |
| 1443 |
} |
| 1444 |
|
| 1445 |
static void write_channel_open_failed(struct ssh2_packet *pkt, |
| 1446 |
uint32_t channel_id, int reason_code) { |
| 1447 |
unsigned char *buf, *ptr; |
| 1448 |
uint32_t buflen, bufsz; |
| 1449 |
char *reason_desc = "unknown error"; |
| 1450 |
|
| 1451 |
buflen = bufsz = 1024; |
| 1452 |
ptr = buf = palloc(pkt->pool, bufsz); |
| 1453 |
|
| 1454 |
switch (reason_code) { |
| 1455 |
case SFTP_SSH2_CHANNEL_OPEN_ADMINISTRATIVELY_PROHIBITED: |
| 1456 |
reason_desc = "Invalid channel parameters provided"; |
| 1457 |
break; |
| 1458 |
|
| 1459 |
case SFTP_SSH2_CHANNEL_OPEN_UNKNOWN_CHANNEL_TYPE: |
| 1460 |
reason_desc = "Unsupported channel type requested"; |
| 1461 |
break; |
| 1462 |
|
| 1463 |
case SFTP_SSH2_CHANNEL_OPEN_RESOURCE_SHORTAGE: |
| 1464 |
reason_desc = "Resource shortage"; |
| 1465 |
break; |
| 1466 |
|
| 1467 |
default: |
| 1468 |
reason_desc = "Unknown error"; |
| 1469 |
} |
| 1470 |
|
| 1471 |
sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 1472 |
sftp_msg_write_int(&buf, &buflen, channel_id); |
| 1473 |
sftp_msg_write_int(&buf, &buflen, reason_code); |
| 1474 |
sftp_msg_write_string(&buf, &buflen, reason_desc); |
| 1475 |
sftp_msg_write_string(&buf, &buflen, "en-US"); |
| 1476 |
|
| 1477 |
pkt->payload = ptr; |
| 1478 |
pkt->payload_len = (bufsz - buflen); |
| 1479 |
} |
| 1480 |
|
| 1481 |
uint32_t sftp_channel_get_windowsz(uint32_t channel_id) { |
| 1482 |
struct ssh2_channel *chan; |
| 1483 |
|
| 1484 |
chan = get_channel(channel_id); |
| 1485 |
if (chan == NULL) { |
| 1486 |
pr_trace_msg(trace_channel, 1, "cannot return window size for unknown " |
| 1487 |
"channel ID %lu", (unsigned long) channel_id); |
| 1488 |
return 0; |
| 1489 |
} |
| 1490 |
|
| 1491 |
return chan->remote_windowsz; |
| 1492 |
} |
| 1493 |
|
| 1494 |
unsigned int sftp_channel_set_max_count(unsigned int max) { |
| 1495 |
unsigned int prev_max; |
| 1496 |
|
| 1497 |
prev_max = channel_max; |
| 1498 |
channel_max = max; |
| 1499 |
|
| 1500 |
return prev_max; |
| 1501 |
} |
| 1502 |
|
| 1503 |
uint32_t sftp_channel_get_max_packetsz(void) { |
| 1504 |
return chan_packet_size; |
| 1505 |
} |
| 1506 |
|
| 1507 |
uint32_t sftp_channel_set_max_packetsz(uint32_t packetsz) { |
| 1508 |
uint32_t prev_packetsz; |
| 1509 |
|
| 1510 |
prev_packetsz = chan_packet_size; |
| 1511 |
chan_packet_size = packetsz; |
| 1512 |
|
| 1513 |
return prev_packetsz; |
| 1514 |
} |
| 1515 |
|
| 1516 |
uint32_t sftp_channel_set_max_windowsz(uint32_t windowsz) { |
| 1517 |
uint32_t prev_windowsz; |
| 1518 |
|
| 1519 |
prev_windowsz = chan_window_size; |
| 1520 |
chan_window_size = windowsz; |
| 1521 |
|
| 1522 |
return prev_windowsz; |
| 1523 |
} |
| 1524 |
|
| 1525 |
int sftp_channel_handle(struct ssh2_packet *pkt, char msg_type) { |
| 1526 |
int res; |
| 1527 |
uint32_t channel_id; |
| 1528 |
|
| 1529 |
switch (msg_type) { |
| 1530 |
case SFTP_SSH2_MSG_CHANNEL_OPEN: { |
| 1531 |
res = read_channel_open(pkt, &channel_id); |
| 1532 |
if (res < 0) { |
| 1533 |
int reason_code, xerrno; |
| 1534 |
struct ssh2_packet *pkt2; |
| 1535 |
|
| 1536 |
xerrno = errno; |
| 1537 |
pkt2 = sftp_ssh2_packet_create(channel_pool); |
| 1538 |
|
| 1539 |
switch (xerrno) { |
| 1540 |
case EPERM: |
| 1541 |
reason_code = SFTP_SSH2_CHANNEL_OPEN_RESOURCE_SHORTAGE; |
| 1542 |
break; |
| 1543 |
|
| 1544 |
case EINVAL: |
| 1545 |
reason_code = SFTP_SSH2_CHANNEL_OPEN_ADMINISTRATIVELY_PROHIBITED; |
| 1546 |
break; |
| 1547 |
|
| 1548 |
case ENOENT: |
| 1549 |
default: |
| 1550 |
reason_code = SFTP_SSH2_CHANNEL_OPEN_UNKNOWN_CHANNEL_TYPE; |
| 1551 |
} |
| 1552 |
|
| 1553 |
write_channel_open_failed(pkt2, channel_id, reason_code); |
| 1554 |
(void) sftp_ssh2_packet_write(sftp_conn->wfd, pkt2); |
| 1555 |
|
| 1556 |
destroy_pool(pkt2->pool); |
| 1557 |
destroy_pool(pkt->pool); |
| 1558 |
|
| 1559 |
return -1; |
| 1560 |
} |
| 1561 |
|
| 1562 |
destroy_pool(pkt->pool); |
| 1563 |
|
| 1564 |
pkt = sftp_ssh2_packet_create(channel_pool); |
| 1565 |
res = write_channel_open_confirm(pkt, channel_id); |
| 1566 |
if (res < 0) { |
| 1567 |
destroy_pool(pkt->pool); |
| 1568 |
return -1; |
| 1569 |
} |
| 1570 |
|
| 1571 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 1572 |
if (res < 0) { |
| 1573 |
destroy_pool(pkt->pool); |
| 1574 |
return -1; |
| 1575 |
} |
| 1576 |
|
| 1577 |
destroy_pool(pkt->pool); |
| 1578 |
return 0; |
| 1579 |
} |
| 1580 |
|
| 1581 |
case SFTP_SSH2_MSG_CHANNEL_REQUEST: |
| 1582 |
res = handle_channel_req(pkt); |
| 1583 |
destroy_pool(pkt->pool); |
| 1584 |
return res; |
| 1585 |
|
| 1586 |
case SFTP_SSH2_MSG_CHANNEL_CLOSE: |
| 1587 |
res = handle_channel_close(pkt); |
| 1588 |
destroy_pool(pkt->pool); |
| 1589 |
return res; |
| 1590 |
|
| 1591 |
case SFTP_SSH2_MSG_CHANNEL_DATA: |
| 1592 |
res = handle_channel_data(pkt, &channel_id); |
| 1593 |
if (res == 1) { |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
res = send_channel_done(pkt->pool, channel_id); |
| 1598 |
} |
| 1599 |
|
| 1600 |
destroy_pool(pkt->pool); |
| 1601 |
return res; |
| 1602 |
|
| 1603 |
case SFTP_SSH2_MSG_CHANNEL_EOF: |
| 1604 |
res = handle_channel_eof(pkt); |
| 1605 |
destroy_pool(pkt->pool); |
| 1606 |
return res; |
| 1607 |
|
| 1608 |
case SFTP_SSH2_MSG_CHANNEL_WINDOW_ADJUST: |
| 1609 |
res = handle_channel_window_adjust(pkt); |
| 1610 |
destroy_pool(pkt->pool); |
| 1611 |
return res; |
| 1612 |
|
| 1613 |
default: |
| 1614 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1615 |
"expecting CHANNEL message, received %s (%d), disconnecting", |
| 1616 |
sftp_ssh2_packet_get_msg_type_desc(msg_type), msg_type); |
| 1617 |
destroy_pool(pkt->pool); |
| 1618 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_PROTOCOL_ERROR, NULL); |
| 1619 |
} |
| 1620 |
|
| 1621 |
errno = EINVAL; |
| 1622 |
return -1; |
| 1623 |
} |
| 1624 |
|
| 1625 |
int sftp_channel_free(void) { |
| 1626 |
register unsigned int i; |
| 1627 |
struct ssh2_channel **chans; |
| 1628 |
|
| 1629 |
if (channel_count == 0 || |
| 1630 |
channel_list == NULL) { |
| 1631 |
return 0; |
| 1632 |
} |
| 1633 |
|
| 1634 |
|
| 1635 |
chans = channel_list->elts; |
| 1636 |
for (i = 0; i < channel_list->nelts; i++) { |
| 1637 |
if (chans[i] != NULL) { |
| 1638 |
pr_trace_msg(trace_channel, 15, |
| 1639 |
"destroying unclosed channel ID %lu (%lu incoming bytes pending, " |
| 1640 |
"%lu outgoing bytes pending)", |
| 1641 |
(unsigned long) chans[i]->local_channel_id, |
| 1642 |
(unsigned long) chans[i]->incoming_len, |
| 1643 |
(unsigned long) chans[i]->outgoing_len); |
| 1644 |
|
| 1645 |
if (chans[i]->finish != NULL) { |
| 1646 |
(chans[i]->finish)(chans[i]->local_channel_id); |
| 1647 |
} |
| 1648 |
|
| 1649 |
destroy_pool(chans[i]->pool); |
| 1650 |
chans[i] = NULL; |
| 1651 |
|
| 1652 |
channel_count--; |
| 1653 |
} |
| 1654 |
} |
| 1655 |
|
| 1656 |
return 0; |
| 1657 |
} |
| 1658 |
|
| 1659 |
int sftp_channel_init(void) { |
| 1660 |
struct ssh2_channel_exec_handler *handler; |
| 1661 |
config_rec *c; |
| 1662 |
|
| 1663 |
if (channel_pool == NULL) { |
| 1664 |
channel_pool = make_sub_pool(sftp_pool); |
| 1665 |
pr_pool_tag(channel_pool, "SSH2 Channel Pool"); |
| 1666 |
} |
| 1667 |
|
| 1668 |
if (channel_exec_handlers == NULL) { |
| 1669 |
|
| 1670 |
channel_exec_handlers = make_array(channel_pool, 1, |
| 1671 |
sizeof(struct ssh2_channel_exec_handler *)); |
| 1672 |
} |
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
handler = pcalloc(channel_pool, sizeof(struct ssh2_channel_exec_handler)); |
| 1677 |
handler->m = &sftp_module; |
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
handler->command = pstrdup(channel_pool, "scp"); |
| 1683 |
handler->set_params = sftp_scp_set_params; |
| 1684 |
handler->prepare = sftp_scp_open_session; |
| 1685 |
handler->postopen = NULL; |
| 1686 |
handler->handle_packet = sftp_scp_handle_packet; |
| 1687 |
handler->finish = sftp_scp_close_session; |
| 1688 |
|
| 1689 |
*((struct ssh2_channel_exec_handler **) push_array(channel_exec_handlers)) = |
| 1690 |
handler; |
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
handler = pcalloc(channel_pool, sizeof(struct ssh2_channel_exec_handler)); |
| 1695 |
handler->m = &sftp_module; |
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
handler->command = pstrdup(channel_pool, "date"); |
| 1701 |
handler->set_params = sftp_date_set_params; |
| 1702 |
handler->prepare = sftp_date_open_session; |
| 1703 |
handler->postopen = sftp_date_postopen_session; |
| 1704 |
handler->handle_packet = sftp_date_handle_packet; |
| 1705 |
handler->finish = sftp_date_close_session; |
| 1706 |
|
| 1707 |
*((struct ssh2_channel_exec_handler **) push_array(channel_exec_handlers)) = |
| 1708 |
handler; |
| 1709 |
|
| 1710 |
accepted_envs = make_array(channel_pool, 0, sizeof(char *)); |
| 1711 |
|
| 1712 |
c = find_config(main_server->conf, CONF_PARAM, "SFTPAcceptEnv", FALSE); |
| 1713 |
if (c) { |
| 1714 |
while (c) { |
| 1715 |
register unsigned int i; |
| 1716 |
array_header *envs; |
| 1717 |
char **elts; |
| 1718 |
|
| 1719 |
pr_signals_handle(); |
| 1720 |
|
| 1721 |
envs = c->argv[0]; |
| 1722 |
elts = envs->elts; |
| 1723 |
for (i = 0; i < envs->nelts; i++) { |
| 1724 |
*((char **) push_array(accepted_envs)) = pstrdup(channel_pool, elts[i]); |
| 1725 |
} |
| 1726 |
|
| 1727 |
c = find_config_next(c, c->next, CONF_PARAM, "SFTPAcceptEnv", FALSE); |
| 1728 |
} |
| 1729 |
|
| 1730 |
} else { |
| 1731 |
|
| 1732 |
*((char **) push_array(accepted_envs)) = pstrdup(channel_pool, "LANG"); |
| 1733 |
} |
| 1734 |
|
| 1735 |
return 0; |
| 1736 |
} |
| 1737 |
|
| 1738 |
int sftp_channel_drain_data(void) { |
| 1739 |
register unsigned int i; |
| 1740 |
struct ssh2_channel **chans; |
| 1741 |
|
| 1742 |
if (channel_list == NULL) { |
| 1743 |
errno = EACCES; |
| 1744 |
return -1; |
| 1745 |
} |
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
chans = channel_list->elts; |
| 1751 |
for (i = 0; i < channel_list->nelts; i++) { |
| 1752 |
if (chans[i] != NULL) { |
| 1753 |
pr_trace_msg(trace_channel, 15, |
| 1754 |
"draining pending outgoing data for local channel ID %lu", |
| 1755 |
(unsigned long) chans[i]->local_channel_id); |
| 1756 |
|
| 1757 |
drain_pending_outgoing_channel_data(chans[i]->local_channel_id); |
| 1758 |
} |
| 1759 |
} |
| 1760 |
|
| 1761 |
return 0; |
| 1762 |
} |
| 1763 |
|
| 1764 |
static int channel_write_data(pool *p, uint32_t channel_id, |
| 1765 |
unsigned char *buf, uint32_t buflen, char msg_type, uint32_t data_type) { |
| 1766 |
struct ssh2_channel *chan; |
| 1767 |
int res; |
| 1768 |
|
| 1769 |
chan = get_channel(channel_id); |
| 1770 |
if (chan == NULL) { |
| 1771 |
errno = EACCES; |
| 1772 |
return -1; |
| 1773 |
} |
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
while (!(sftp_sess_state & SFTP_SESS_STATE_REKEYING) && |
| 1781 |
chan->remote_windowsz > 0 && |
| 1782 |
buflen > 0) { |
| 1783 |
uint32_t payload_len; |
| 1784 |
|
| 1785 |
pr_signals_handle(); |
| 1786 |
|
| 1787 |
|
| 1788 |
drain_pending_outgoing_channel_data(channel_id); |
| 1789 |
if (chan->remote_windowsz == 0) { |
| 1790 |
break; |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
payload_len = buflen; |
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
if (payload_len > chan->remote_max_packetsz) { |
| 1804 |
payload_len = chan->remote_max_packetsz; |
| 1805 |
} |
| 1806 |
|
| 1807 |
if (payload_len > chan->remote_windowsz) { |
| 1808 |
payload_len = chan->remote_windowsz; |
| 1809 |
} |
| 1810 |
|
| 1811 |
if (payload_len > 0) { |
| 1812 |
struct ssh2_packet *pkt; |
| 1813 |
unsigned char *buf2, *ptr2; |
| 1814 |
uint32_t bufsz2, buflen2; |
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
bufsz2 = buflen2 = payload_len + 13; |
| 1821 |
|
| 1822 |
pkt = sftp_ssh2_packet_create(p); |
| 1823 |
ptr2 = buf2 = palloc(pkt->pool, bufsz2); |
| 1824 |
|
| 1825 |
sftp_msg_write_byte(&buf2, &buflen2, msg_type); |
| 1826 |
sftp_msg_write_int(&buf2, &buflen2, chan->remote_channel_id); |
| 1827 |
|
| 1828 |
if (data_type != 0) { |
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
sftp_msg_write_int(&buf2, &buflen2, data_type); |
| 1833 |
} |
| 1834 |
|
| 1835 |
sftp_msg_write_int(&buf2, &buflen2, payload_len); |
| 1836 |
memcpy(buf2, buf, payload_len); |
| 1837 |
buflen2 -= payload_len; |
| 1838 |
|
| 1839 |
pkt->payload = ptr2; |
| 1840 |
pkt->payload_len = (bufsz2 - buflen2); |
| 1841 |
|
| 1842 |
pr_trace_msg(trace_channel, 9, "sending %s (remote channel ID %lu, " |
| 1843 |
"%lu data bytes)", |
| 1844 |
msg_type == SFTP_SSH2_MSG_CHANNEL_DATA ? "CHANNEL_DATA" : |
| 1845 |
"CHANNEL_EXTENDED_DATA", (unsigned long) chan->remote_channel_id, |
| 1846 |
(unsigned long) payload_len); |
| 1847 |
|
| 1848 |
res = sftp_ssh2_packet_write(sftp_conn->wfd, pkt); |
| 1849 |
if (res == 0) { |
| 1850 |
chan->remote_windowsz -= payload_len; |
| 1851 |
|
| 1852 |
pr_trace_msg(trace_channel, 11, |
| 1853 |
"channel ID %lu remote window size currently at %lu bytes", |
| 1854 |
(unsigned long) chan->remote_channel_id, |
| 1855 |
(unsigned long) chan->remote_windowsz); |
| 1856 |
} |
| 1857 |
|
| 1858 |
destroy_pool(pkt->pool); |
| 1859 |
|
| 1860 |
|
| 1861 |
if (payload_len == buflen) { |
| 1862 |
return res; |
| 1863 |
} |
| 1864 |
|
| 1865 |
buf += payload_len; |
| 1866 |
buflen -= payload_len; |
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
} else { |
| 1873 |
pr_trace_msg(trace_channel, 6, "allowed payload size of %lu bytes is too " |
| 1874 |
"small for data (%lu bytes)", (unsigned long) payload_len, |
| 1875 |
(unsigned long) buflen); |
| 1876 |
|
| 1877 |
|
| 1878 |
break; |
| 1879 |
} |
| 1880 |
} |
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
if (buflen > 0) { |
| 1887 |
struct ssh2_channel_databuf *db; |
| 1888 |
const char *reason; |
| 1889 |
|
| 1890 |
db = get_outgoing_databuf(channel_id, buflen); |
| 1891 |
|
| 1892 |
db->buflen = buflen; |
| 1893 |
memcpy(db->buf, buf, buflen); |
| 1894 |
chan->outgoing_len += buflen; |
| 1895 |
|
| 1896 |
|
| 1897 |
reason = "remote window size too small"; |
| 1898 |
if (sftp_sess_state & SFTP_SESS_STATE_REKEYING) { |
| 1899 |
reason = "rekeying"; |
| 1900 |
} |
| 1901 |
|
| 1902 |
pr_trace_msg(trace_channel, 8, "buffering %lu remaining bytes of " |
| 1903 |
"outgoing data (%s)", (unsigned long) buflen, reason); |
| 1904 |
} |
| 1905 |
|
| 1906 |
return 0; |
| 1907 |
} |
| 1908 |
|
| 1909 |
int sftp_channel_write_data(pool *p, uint32_t channel_id, |
| 1910 |
unsigned char *buf, uint32_t buflen) { |
| 1911 |
return channel_write_data(p, channel_id, buf, buflen, |
| 1912 |
SFTP_SSH2_MSG_CHANNEL_DATA, 0); |
| 1913 |
} |
| 1914 |
|
| 1915 |
int sftp_channel_write_ext_data_stderr(pool *p, uint32_t channel_id, |
| 1916 |
unsigned char *buf, uint32_t buflen) { |
| 1917 |
return channel_write_data(p, channel_id, buf, buflen, |
| 1918 |
SFTP_SSH2_MSG_CHANNEL_EXTENDED_DATA, |
| 1919 |
SFTP_SSH2_MSG_CHANNEL_EXTENDED_DATA_TYPE_STDERR); |
| 1920 |
} |
| 1921 |
|
| 1922 |
|
| 1923 |
unsigned int sftp_channel_opened(uint32_t *remote_channel_id) { |
| 1924 |
register unsigned int i; |
| 1925 |
struct ssh2_channel **chans; |
| 1926 |
|
| 1927 |
if (channel_count == 0 || |
| 1928 |
channel_list == NULL) { |
| 1929 |
return 0; |
| 1930 |
} |
| 1931 |
|
| 1932 |
if (channel_list == NULL) { |
| 1933 |
errno = EACCES; |
| 1934 |
return 0; |
| 1935 |
} |
| 1936 |
|
| 1937 |
chans = channel_list->elts; |
| 1938 |
for (i = 0; i < channel_list->nelts; i++) { |
| 1939 |
if (chans[i] != NULL) { |
| 1940 |
if (remote_channel_id != NULL) { |
| 1941 |
*remote_channel_id = chans[i]->remote_channel_id; |
| 1942 |
} |
| 1943 |
} |
| 1944 |
} |
| 1945 |
|
| 1946 |
return channel_count; |
| 1947 |
} |
| 1948 |
|
| 1949 |
int sftp_channel_register_exec_handler(module *m, const char *command, |
| 1950 |
int (*set_params)(pool *, uint32_t, array_header *), |
| 1951 |
int (*prepare)(uint32_t), |
| 1952 |
int (*postopen)(uint32_t), |
| 1953 |
int (*handle_packet)(pool *, void *, uint32_t, unsigned char *, uint32_t), |
| 1954 |
int (*finish)(uint32_t), |
| 1955 |
int (**write_data)(pool *, uint32_t, unsigned char *, uint32_t)) { |
| 1956 |
struct ssh2_channel_exec_handler *handler; |
| 1957 |
|
| 1958 |
if (m == NULL || |
| 1959 |
command == NULL || |
| 1960 |
set_params == NULL || |
| 1961 |
prepare == NULL || |
| 1962 |
handle_packet == NULL || |
| 1963 |
finish == NULL || |
| 1964 |
write_data == NULL) { |
| 1965 |
errno = EINVAL; |
| 1966 |
return -1; |
| 1967 |
} |
| 1968 |
|
| 1969 |
if (channel_pool == NULL) { |
| 1970 |
channel_pool = make_sub_pool(sftp_pool); |
| 1971 |
pr_pool_tag(channel_pool, "SSH2 Channel Pool"); |
| 1972 |
} |
| 1973 |
|
| 1974 |
if (channel_exec_handlers == NULL) { |
| 1975 |
|
| 1976 |
channel_exec_handlers = make_array(channel_pool, 1, |
| 1977 |
sizeof(struct ssh2_channel_exec_handler *)); |
| 1978 |
|
| 1979 |
} else { |
| 1980 |
register unsigned int i; |
| 1981 |
struct ssh2_channel_exec_handler **handlers; |
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
handlers = channel_exec_handlers->elts; |
| 1987 |
for (i = 0; i < channel_exec_handlers->nelts; i++) { |
| 1988 |
handler = handlers[i]; |
| 1989 |
|
| 1990 |
if (strcmp(handler->command, command) == 0) { |
| 1991 |
errno = EEXIST; |
| 1992 |
return -1; |
| 1993 |
} |
| 1994 |
} |
| 1995 |
} |
| 1996 |
|
| 1997 |
handler = pcalloc(channel_pool, sizeof(struct ssh2_channel_exec_handler)); |
| 1998 |
|
| 1999 |
handler->m = m; |
| 2000 |
handler->command = pstrdup(channel_pool, command); |
| 2001 |
handler->set_params = set_params; |
| 2002 |
handler->prepare = prepare; |
| 2003 |
handler->postopen = postopen; |
| 2004 |
handler->handle_packet = handle_packet; |
| 2005 |
handler->finish = finish; |
| 2006 |
|
| 2007 |
*((struct ssh2_channel_exec_handler **) push_array(channel_exec_handlers)) = |
| 2008 |
handler; |
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
*write_data = sftp_channel_write_data; |
| 2018 |
|
| 2019 |
return 0; |
| 2020 |
} |