| 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 "packet.h" |
| 28 |
#include "msg.h" |
| 29 |
#include "channel.h" |
| 30 |
#include "scp.h" |
| 31 |
#include "misc.h" |
| 32 |
#include "disconnect.h" |
| 33 |
|
| 34 |
#define SFTP_SCP_ST_MODE_MASK (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#define SFTP_SCP_MAX_CTL_LEN (PR_TUNABLE_PATH_MAX + 256) |
| 40 |
|
| 41 |
extern pr_response_t *resp_list, *resp_err_list; |
| 42 |
|
| 43 |
struct scp_path { |
| 44 |
char *path; |
| 45 |
|
| 46 |
|
| 47 |
const char *orig_path; |
| 48 |
|
| 49 |
pr_fh_t *fh; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
struct scp_path *parent_dir; |
| 61 |
|
| 62 |
|
| 63 |
int checked_errors; |
| 64 |
|
| 65 |
int have_mode; |
| 66 |
mode_t st_mode; |
| 67 |
|
| 68 |
struct timeval times[2]; |
| 69 |
int recvd_timeinfo; |
| 70 |
|
| 71 |
mode_t perms; |
| 72 |
off_t filesz; |
| 73 |
const char *filename; |
| 74 |
const char *best_path; |
| 75 |
int recvd_finfo; |
| 76 |
int recvd_data; |
| 77 |
|
| 78 |
|
| 79 |
pool *ctl_pool; |
| 80 |
unsigned char *ctl_data; |
| 81 |
uint32_t ctl_datalen; |
| 82 |
|
| 83 |
|
| 84 |
off_t recvlen; |
| 85 |
|
| 86 |
int wrote_errors; |
| 87 |
|
| 88 |
|
| 89 |
int sent_timeinfo; |
| 90 |
int sent_dirinfo; |
| 91 |
int sent_finfo; |
| 92 |
int sent_data; |
| 93 |
|
| 94 |
|
| 95 |
off_t sentlen; |
| 96 |
|
| 97 |
|
| 98 |
void *dirh; |
| 99 |
struct scp_path *dir_spi; |
| 100 |
|
| 101 |
|
| 102 |
int hiddenstore; |
| 103 |
|
| 104 |
|
| 105 |
int file_existed; |
| 106 |
}; |
| 107 |
|
| 108 |
static pool *scp_pool = NULL; |
| 109 |
|
| 110 |
|
| 111 |
struct scp_session { |
| 112 |
struct scp_session *next, *prev; |
| 113 |
|
| 114 |
pool *pool; |
| 115 |
uint32_t channel_id; |
| 116 |
array_header *paths; |
| 117 |
unsigned int path_idx; |
| 118 |
}; |
| 119 |
|
| 120 |
static struct scp_session *scp_session = NULL, *scp_sessions = NULL; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
struct scp_paths { |
| 129 |
struct scp_paths *next, *prev; |
| 130 |
|
| 131 |
pool *pool; |
| 132 |
uint32_t channel_id; |
| 133 |
array_header *paths; |
| 134 |
unsigned int path_idx; |
| 135 |
}; |
| 136 |
|
| 137 |
static struct scp_paths *scp_paths = NULL; |
| 138 |
|
| 139 |
static unsigned int scp_opts = 0; |
| 140 |
#define SFTP_SCP_OPT_ISSRC 0x0001 |
| 141 |
#define SFTP_SCP_OPT_ISDST 0x0002 |
| 142 |
#define SFTP_SCP_OPT_DIR 0x0004 |
| 143 |
#define SFTP_SCP_OPT_VERBOSE 0x0008 |
| 144 |
#define SFTP_SCP_OPT_PRESERVE 0x0010 |
| 145 |
#define SFTP_SCP_OPT_RECURSE 0x0020 |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static int need_confirm = FALSE; |
| 151 |
|
| 152 |
static const char *trace_channel = "scp"; |
| 153 |
|
| 154 |
static int send_path(pool *, uint32_t, struct scp_path *); |
| 155 |
|
| 156 |
static int scp_timeout_stalled_cb(CALLBACK_FRAME) { |
| 157 |
pr_event_generate("core.timeout-stalled", NULL); |
| 158 |
|
| 159 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 160 |
"SCP data transfer stalled timeout (%d secs) reached", |
| 161 |
pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED)); |
| 162 |
SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, |
| 163 |
"data stalled timeout reached"); |
| 164 |
|
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
static cmd_rec *scp_cmd_alloc(pool *p, const char *name, const char *arg) { |
| 169 |
cmd_rec *cmd; |
| 170 |
|
| 171 |
cmd = pr_cmd_alloc(p, 2, pstrdup(p, name), arg ? arg : ""); |
| 172 |
cmd->arg = (char *) arg; |
| 173 |
|
| 174 |
return cmd; |
| 175 |
} |
| 176 |
|
| 177 |
static int scp_destroy_paths(struct scp_paths *paths) { |
| 178 |
if (paths == NULL) { |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
if (paths->next) |
| 183 |
paths->next->prev = paths->prev; |
| 184 |
|
| 185 |
if (paths->prev) { |
| 186 |
paths->prev->next = paths->next; |
| 187 |
|
| 188 |
} else { |
| 189 |
scp_paths = paths->next; |
| 190 |
} |
| 191 |
|
| 192 |
destroy_pool(paths->pool); |
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
static struct scp_paths *scp_new_paths(uint32_t channel_id) { |
| 197 |
pool *sub_pool; |
| 198 |
struct scp_paths *paths, *last; |
| 199 |
|
| 200 |
|
| 201 |
paths = last = scp_paths; |
| 202 |
while (paths) { |
| 203 |
pr_signals_handle(); |
| 204 |
|
| 205 |
if (paths->channel_id == channel_id) { |
| 206 |
errno = EEXIST; |
| 207 |
return NULL; |
| 208 |
} |
| 209 |
|
| 210 |
if (paths->next == NULL) { |
| 211 |
|
| 212 |
last = paths; |
| 213 |
} |
| 214 |
|
| 215 |
paths = paths->next; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
sub_pool = make_sub_pool(scp_pool); |
| 220 |
pr_pool_tag(sub_pool, "SCP paths pool"); |
| 221 |
|
| 222 |
paths = pcalloc(sub_pool, sizeof(struct scp_paths)); |
| 223 |
paths->pool = sub_pool; |
| 224 |
paths->channel_id = channel_id; |
| 225 |
|
| 226 |
if (last != NULL) { |
| 227 |
last->next = paths; |
| 228 |
paths->prev = last; |
| 229 |
|
| 230 |
} else { |
| 231 |
scp_paths = paths; |
| 232 |
} |
| 233 |
|
| 234 |
return paths; |
| 235 |
} |
| 236 |
|
| 237 |
static struct scp_paths *scp_get_paths(uint32_t channel_id) { |
| 238 |
struct scp_paths *paths; |
| 239 |
|
| 240 |
paths = scp_paths; |
| 241 |
while (paths) { |
| 242 |
pr_signals_handle(); |
| 243 |
|
| 244 |
if (paths->channel_id == channel_id) { |
| 245 |
return paths; |
| 246 |
} |
| 247 |
|
| 248 |
paths = paths->next; |
| 249 |
} |
| 250 |
|
| 251 |
errno = ENOENT; |
| 252 |
return NULL; |
| 253 |
} |
| 254 |
|
| 255 |
static struct scp_session *scp_get_session(uint32_t channel_id) { |
| 256 |
struct scp_session *sess; |
| 257 |
|
| 258 |
sess = scp_sessions; |
| 259 |
while (sess != NULL) { |
| 260 |
pr_signals_handle(); |
| 261 |
|
| 262 |
if (sess->channel_id == channel_id) { |
| 263 |
return sess; |
| 264 |
} |
| 265 |
|
| 266 |
sess = sess->next; |
| 267 |
} |
| 268 |
|
| 269 |
errno = ENOENT; |
| 270 |
return NULL; |
| 271 |
} |
| 272 |
|
| 273 |
static void reset_path(struct scp_path *sp) { |
| 274 |
if (sp->fh) { |
| 275 |
pr_fsio_close(sp->fh); |
| 276 |
sp->fh = NULL; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
sp->checked_errors = FALSE; |
| 284 |
|
| 285 |
sp->st_mode = 0; |
| 286 |
sp->have_mode = FALSE; |
| 287 |
sp->recvd_timeinfo = FALSE; |
| 288 |
|
| 289 |
sp->perms = 0; |
| 290 |
sp->filesz = 0; |
| 291 |
sp->filename = NULL; |
| 292 |
sp->best_path = NULL; |
| 293 |
sp->recvd_finfo = FALSE; |
| 294 |
sp->recvd_data = FALSE; |
| 295 |
|
| 296 |
sp->recvlen = 0; |
| 297 |
sp->hiddenstore = FALSE; |
| 298 |
sp->file_existed = FALSE; |
| 299 |
|
| 300 |
sp->wrote_errors = FALSE; |
| 301 |
} |
| 302 |
|
| 303 |
static int read_confirm(struct ssh2_packet *pkt, unsigned char **buf, |
| 304 |
uint32_t *buflen) { |
| 305 |
char code; |
| 306 |
|
| 307 |
code = sftp_msg_read_byte(pkt->pool, buf, buflen); |
| 308 |
pr_trace_msg(trace_channel, 9, "recvd confirmation/error code = %d", code); |
| 309 |
|
| 310 |
switch (code) { |
| 311 |
case 0: |
| 312 |
break; |
| 313 |
|
| 314 |
case 1: { |
| 315 |
register unsigned int i; |
| 316 |
char *msg; |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
msg = pcalloc(pkt->pool, *buflen + 1); |
| 323 |
for (i = 0; *buflen; ) { |
| 324 |
char c; |
| 325 |
|
| 326 |
c = sftp_msg_read_byte(pkt->pool, buf, buflen); |
| 327 |
if (c == '\n') { |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
msg[i++] = c; |
| 332 |
} |
| 333 |
|
| 334 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 335 |
"error from client: %s", msg); |
| 336 |
return -1; |
| 337 |
} |
| 338 |
|
| 339 |
case 2: |
| 340 |
|
| 341 |
return -1; |
| 342 |
} |
| 343 |
|
| 344 |
need_confirm = FALSE; |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
static int write_confirm(pool *p, uint32_t channel_id, int code, |
| 349 |
const char *msg) { |
| 350 |
unsigned char *buf, *ptr; |
| 351 |
uint32_t buflen, bufsz; |
| 352 |
|
| 353 |
|
| 354 |
buflen = bufsz = 128; |
| 355 |
buf = ptr = palloc(p, bufsz); |
| 356 |
|
| 357 |
if (code == 0) { |
| 358 |
pr_trace_msg(trace_channel, 9, "sending confirmation/error code = %d", |
| 359 |
code); |
| 360 |
sftp_msg_write_byte(&buf, &buflen, code); |
| 361 |
|
| 362 |
} else { |
| 363 |
char *errstr; |
| 364 |
size_t errlen; |
| 365 |
|
| 366 |
pr_trace_msg(trace_channel, 9, "sending confirmation/error code = %d (%s)", |
| 367 |
code, msg ? msg : "null"); |
| 368 |
|
| 369 |
errstr = pstrcat(p, msg, "\n", NULL); |
| 370 |
errlen = strlen(errstr); |
| 371 |
|
| 372 |
sftp_msg_write_byte(&buf, &buflen, code); |
| 373 |
sftp_msg_write_data(&buf, &buflen, (const unsigned char *) errstr, errlen, |
| 374 |
FALSE); |
| 375 |
} |
| 376 |
|
| 377 |
return sftp_channel_write_data(p, channel_id, ptr, (bufsz - buflen)); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
static int recv_ctl(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 383 |
unsigned char *data, uint32_t datalen, |
| 384 |
unsigned char **ctl_data, uint32_t *ctl_datalen) { |
| 385 |
register int i; |
| 386 |
int have_newline = FALSE; |
| 387 |
char *tmp; |
| 388 |
uint32_t tmplen; |
| 389 |
|
| 390 |
for (i = datalen-1; i >= 0; i--) { |
| 391 |
if (data[i] == '\n') { |
| 392 |
have_newline = TRUE; |
| 393 |
break; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if (sp->ctl_data == NULL) { |
| 398 |
if (have_newline == TRUE) { |
| 399 |
*ctl_data = data; |
| 400 |
*ctl_datalen = datalen; |
| 401 |
|
| 402 |
return 1; |
| 403 |
} |
| 404 |
|
| 405 |
sp->ctl_pool = pr_pool_create_sz(scp_session->pool, 128); |
| 406 |
sp->ctl_datalen = datalen; |
| 407 |
sp->ctl_data = palloc(sp->ctl_pool, sp->ctl_datalen); |
| 408 |
memmove(sp->ctl_data, data, datalen); |
| 409 |
|
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
tmplen = sp->ctl_datalen + datalen; |
| 415 |
tmp = palloc(sp->ctl_pool, tmplen); |
| 416 |
memmove(tmp, sp->ctl_data, sp->ctl_datalen); |
| 417 |
memmove(tmp + sp->ctl_datalen, data, datalen); |
| 418 |
|
| 419 |
sp->ctl_data = (unsigned char *) tmp; |
| 420 |
sp->ctl_datalen = tmplen; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
if (have_newline == TRUE) { |
| 426 |
*ctl_datalen = sp->ctl_datalen; |
| 427 |
*ctl_data = palloc(p, *ctl_datalen); |
| 428 |
memcpy(*ctl_data, sp->ctl_data, sp->ctl_datalen); |
| 429 |
|
| 430 |
sp->ctl_data = NULL; |
| 431 |
sp->ctl_datalen = 0; |
| 432 |
destroy_pool(sp->ctl_pool); |
| 433 |
sp->ctl_pool = NULL; |
| 434 |
|
| 435 |
return 1; |
| 436 |
} |
| 437 |
|
| 438 |
if (sp->ctl_datalen >= SFTP_SCP_MAX_CTL_LEN) { |
| 439 |
write_confirm(sp->ctl_pool, channel_id, 1, |
| 440 |
"max control message size exceeded"); |
| 441 |
sp->wrote_errors = TRUE; |
| 442 |
return 1; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
return 0; |
| 447 |
} |
| 448 |
|
| 449 |
static int recv_errors(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 450 |
unsigned char *data, uint32_t datalen) { |
| 451 |
|
| 452 |
|
| 453 |
if (data[0] == '\01') { |
| 454 |
register unsigned int i; |
| 455 |
char *msg; |
| 456 |
|
| 457 |
for (i = 1; i < datalen; i++) { |
| 458 |
if (data[i] == '\n') { |
| 459 |
break; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
if (i < datalen) { |
| 464 |
msg = pstrndup(p, (char *) &(data[1]), i + 1); |
| 465 |
|
| 466 |
} else { |
| 467 |
msg = pcalloc(p, i + 1); |
| 468 |
memcpy(msg, &(data[1]), i); |
| 469 |
} |
| 470 |
|
| 471 |
pr_trace_msg(trace_channel, 3, |
| 472 |
"received error '%s' from client while receiving path '%s', skipping", |
| 473 |
msg, sp->path); |
| 474 |
|
| 475 |
sp->checked_errors = TRUE; |
| 476 |
return 1; |
| 477 |
} |
| 478 |
|
| 479 |
if (data[0] == '\02') { |
| 480 |
pr_trace_msg(trace_channel, 3, |
| 481 |
"received fatal error from client while receiving path '%s', skipping", |
| 482 |
sp->path); |
| 483 |
|
| 484 |
sp->checked_errors = TRUE; |
| 485 |
return 1; |
| 486 |
} |
| 487 |
|
| 488 |
sp->checked_errors = TRUE; |
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
static int recv_timeinfo(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 493 |
unsigned char *buf, uint32_t buflen, unsigned char **remain, |
| 494 |
uint32_t *remainlen) { |
| 495 |
register unsigned int i; |
| 496 |
unsigned char *data = NULL, *msg, *ptr = NULL; |
| 497 |
uint32_t datalen = 0; |
| 498 |
char *tmp = NULL; |
| 499 |
int res; |
| 500 |
|
| 501 |
res = recv_ctl(p, channel_id, sp, buf, buflen, &data, &datalen); |
| 502 |
if (res != 1) { |
| 503 |
return res; |
| 504 |
} |
| 505 |
|
| 506 |
if (data[0] != 'T') { |
| 507 |
|
| 508 |
*remain = data; |
| 509 |
*remainlen = datalen; |
| 510 |
|
| 511 |
errno = EINVAL; |
| 512 |
return -1; |
| 513 |
} |
| 514 |
|
| 515 |
for (i = 1; i < datalen; i++) { |
| 516 |
if (data[i] == '\n') { |
| 517 |
ptr = &data[i++]; |
| 518 |
break; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
msg = data + 1; |
| 523 |
|
| 524 |
if (ptr) |
| 525 |
*ptr = '\0'; |
| 526 |
|
| 527 |
pr_trace_msg(trace_channel, 5, "'%s' control message: T%s", sp->path, msg); |
| 528 |
|
| 529 |
sp->times[1].tv_sec = strtoul((char *) msg, &tmp, 10); |
| 530 |
if (tmp == NULL || |
| 531 |
*tmp != ' ') { |
| 532 |
write_confirm(p, channel_id, 1, "mtime secs not delimited"); |
| 533 |
sp->wrote_errors = TRUE; |
| 534 |
return 1; |
| 535 |
} |
| 536 |
|
| 537 |
msg = ((unsigned char *) tmp) + 1; |
| 538 |
sp->times[1].tv_usec = strtoul((char *) msg, &tmp, 10); |
| 539 |
if (tmp == NULL || |
| 540 |
*tmp != ' ') { |
| 541 |
write_confirm(p, channel_id, 1, "mtime usecs not delimited"); |
| 542 |
sp->wrote_errors = TRUE; |
| 543 |
return 1; |
| 544 |
} |
| 545 |
|
| 546 |
msg = ((unsigned char *) tmp) + 1; |
| 547 |
sp->times[0].tv_sec = strtoul((char *) msg, &tmp, 10); |
| 548 |
if (tmp == NULL || |
| 549 |
*tmp != ' ') { |
| 550 |
write_confirm(p, channel_id, 1, "atime secs not delimited"); |
| 551 |
sp->wrote_errors = TRUE; |
| 552 |
return 1; |
| 553 |
} |
| 554 |
|
| 555 |
msg = ((unsigned char *) tmp) + 1; |
| 556 |
sp->times[0].tv_usec = strtoul((char *) msg, &tmp, 10); |
| 557 |
if (tmp == NULL || |
| 558 |
*tmp != '\0') { |
| 559 |
write_confirm(p, channel_id, 1, "atime usecs not delimited"); |
| 560 |
sp->wrote_errors = TRUE; |
| 561 |
return 1; |
| 562 |
} |
| 563 |
|
| 564 |
sp->recvd_timeinfo = TRUE; |
| 565 |
write_confirm(p, channel_id, 0, NULL); |
| 566 |
return 0; |
| 567 |
} |
| 568 |
|
| 569 |
static int recv_perms(pool *p, uint32_t channel_id, char *mode_str, |
| 570 |
mode_t *perms) { |
| 571 |
register unsigned int i; |
| 572 |
|
| 573 |
if (strlen(mode_str) < 5) { |
| 574 |
|
| 575 |
pr_trace_msg(trace_channel, 2, "mode string too short: '%s'", mode_str); |
| 576 |
write_confirm(p, channel_id, 1, "bad mode"); |
| 577 |
return -1; |
| 578 |
} |
| 579 |
|
| 580 |
for (i = 0; i < 4; i++) { |
| 581 |
|
| 582 |
if (mode_str[i] < '0' || |
| 583 |
mode_str[i] > '7') { |
| 584 |
pr_trace_msg(trace_channel, 2, "non-octal mode character in '%s'", |
| 585 |
mode_str); |
| 586 |
*perms = 0; |
| 587 |
write_confirm(p, channel_id, 1, "bad mode"); |
| 588 |
return -1; |
| 589 |
} |
| 590 |
|
| 591 |
*perms = (*perms << 3) | (mode_str[i] - '0'); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
if (mode_str[i] != ' ') { |
| 596 |
pr_trace_msg(trace_channel, 2, "mode not followed by space delimiter"); |
| 597 |
write_confirm(p, channel_id, 1, "mode not delimited"); |
| 598 |
return -1; |
| 599 |
} |
| 600 |
|
| 601 |
pr_trace_msg(trace_channel, 8, "client sent file perms: %04o", |
| 602 |
(unsigned int) *perms); |
| 603 |
return 0; |
| 604 |
} |
| 605 |
|
| 606 |
static int recv_filesz(pool *p, uint32_t channel_id, char *size_str, |
| 607 |
off_t *filesz) { |
| 608 |
char *endp = NULL; |
| 609 |
unsigned long long sz; |
| 610 |
*filesz = 0; |
| 611 |
|
| 612 |
#if defined(HAVE_STROULL) |
| 613 |
sz = strtoull(size_str, &endp, 10); |
| 614 |
#else |
| 615 |
sz = strtoul(size_str, &endp, 10); |
| 616 |
#endif |
| 617 |
|
| 618 |
*filesz = (off_t) sz; |
| 619 |
|
| 620 |
|
| 621 |
if (*filesz < 0) { |
| 622 |
pr_trace_msg(trace_channel, 2, "file size out of range"); |
| 623 |
write_confirm(p, channel_id, 1, "file size out of range"); |
| 624 |
return -1; |
| 625 |
} |
| 626 |
|
| 627 |
if (endp == NULL || |
| 628 |
*endp != ' ') { |
| 629 |
pr_trace_msg(trace_channel, 2, "file size not followed by space delimiter"); |
| 630 |
write_confirm(p, channel_id, 1, "file size not delimited"); |
| 631 |
return -1; |
| 632 |
} |
| 633 |
|
| 634 |
pr_trace_msg(trace_channel, 8, "client sent file size: %" PR_LU " bytes", |
| 635 |
(pr_off_t) *filesz); |
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
static int recv_filename(pool *p, uint32_t channel_id, char *name_str, |
| 640 |
struct scp_path *sp) { |
| 641 |
|
| 642 |
if (strchr(name_str, '/') != NULL || |
| 643 |
strncmp(name_str, "..", 3) == 0) { |
| 644 |
pr_trace_msg(trace_channel, 2, "bad filename: '%s'", name_str); |
| 645 |
write_confirm(p, channel_id, 1, |
| 646 |
pstrcat(p, "unexpected filename: ", name_str, NULL)); |
| 647 |
return -1; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
if (sp->parent_dir == NULL) { |
| 709 |
if (!S_ISDIR(sp->st_mode)) { |
| 710 |
|
| 711 |
sp->filename = pstrdup(scp_pool, sp->path); |
| 712 |
|
| 713 |
} else { |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
sp->filename = pdircat(scp_pool, sp->path, name_str, NULL); |
| 718 |
} |
| 719 |
|
| 720 |
} else { |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
sp->filename = pdircat(scp_pool, sp->path, name_str, NULL); |
| 725 |
} |
| 726 |
|
| 727 |
if (sp->filename != NULL) { |
| 728 |
struct stat st; |
| 729 |
|
| 730 |
sp->best_path = dir_canonical_vpath(scp_pool, sp->filename); |
| 731 |
|
| 732 |
pr_fs_clear_cache2(sp->best_path); |
| 733 |
if (pr_fsio_lstat(sp->best_path, &st) == 0) { |
| 734 |
if (S_ISLNK(st.st_mode)) { |
| 735 |
char link_path[PR_TUNABLE_PATH_MAX]; |
| 736 |
int len; |
| 737 |
|
| 738 |
memset(link_path, '\0', sizeof(link_path)); |
| 739 |
len = dir_readlink(scp_pool, sp->best_path, link_path, |
| 740 |
sizeof(link_path)-1, PR_DIR_READLINK_FL_HANDLE_REL_PATH); |
| 741 |
if (len > 0) { |
| 742 |
link_path[len] = '\0'; |
| 743 |
sp->best_path = pstrdup(scp_pool, link_path); |
| 744 |
} |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
session.xfer.path = pstrdup(session.xfer.p, sp->best_path); |
| 750 |
} |
| 751 |
|
| 752 |
pr_trace_msg(trace_channel, 8, |
| 753 |
"client sent filename '%s' (path '%s')", name_str, sp->best_path); |
| 754 |
return 0; |
| 755 |
} |
| 756 |
|
| 757 |
static int recv_finfo(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 758 |
unsigned char *buf, uint32_t buflen) { |
| 759 |
register unsigned int i; |
| 760 |
const char *hiddenstore_path = NULL; |
| 761 |
struct stat st; |
| 762 |
unsigned char *data = NULL, *msg; |
| 763 |
uint32_t datalen = 0; |
| 764 |
char *ptr = NULL; |
| 765 |
int have_dir = FALSE, res; |
| 766 |
cmd_rec *cmd = NULL; |
| 767 |
|
| 768 |
res = recv_ctl(p, channel_id, sp, buf, buflen, &data, &datalen); |
| 769 |
if (res != 1) { |
| 770 |
return res; |
| 771 |
} |
| 772 |
|
| 773 |
switch (data[0]) { |
| 774 |
case 'C': |
| 775 |
break; |
| 776 |
|
| 777 |
case 'D': |
| 778 |
if (!(scp_opts & SFTP_SCP_OPT_RECURSE)) { |
| 779 |
pr_trace_msg(trace_channel, 3, |
| 780 |
"received D control message for '%s' without RECURSE set, " |
| 781 |
"rejecting", sp->path); |
| 782 |
write_confirm(p, channel_id, 1, |
| 783 |
pstrcat(p, sp->path, ": cannot use directory (no -r option)", NULL)); |
| 784 |
sp->wrote_errors = TRUE; |
| 785 |
return 1; |
| 786 |
} |
| 787 |
|
| 788 |
have_dir = TRUE; |
| 789 |
break; |
| 790 |
|
| 791 |
default: |
| 792 |
pr_trace_msg(trace_channel, 3, |
| 793 |
"expected file info control message for '%s', got '%c'", |
| 794 |
sp->path, data[0]); |
| 795 |
write_confirm(p, channel_id, 1, |
| 796 |
pstrcat(p, sp->path, ": expected control message", NULL)); |
| 797 |
sp->wrote_errors = TRUE; |
| 798 |
return 1; |
| 799 |
} |
| 800 |
|
| 801 |
for (i = 1; i < datalen; i++) { |
| 802 |
if (data[i] == '\n') { |
| 803 |
ptr = (char *) &data[i++]; |
| 804 |
break; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
msg = data + 1; |
| 809 |
if (ptr != NULL) { |
| 810 |
*ptr = '\0'; |
| 811 |
} |
| 812 |
|
| 813 |
pr_trace_msg(trace_channel, 5, "'%s' control message: %c%s", sp->path, |
| 814 |
!have_dir ? 'C' : 'D', msg); |
| 815 |
|
| 816 |
ptr = (char *) msg; |
| 817 |
if (recv_perms(p, channel_id, ptr, &sp->perms) < 0) { |
| 818 |
sp->wrote_errors = TRUE; |
| 819 |
return 1; |
| 820 |
} |
| 821 |
|
| 822 |
ptr = strchr(ptr, ' '); |
| 823 |
if (ptr == NULL) { |
| 824 |
pr_trace_msg(trace_channel, 3, |
| 825 |
"bad control message (undelimited mode)"); |
| 826 |
write_confirm(p, channel_id, 1, |
| 827 |
pstrcat(p, sp->path, ": bad control message (undelimited mode)", NULL)); |
| 828 |
sp->wrote_errors = TRUE; |
| 829 |
return 1; |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
ptr++; |
| 834 |
if (recv_filesz(p, channel_id, ptr, &sp->filesz) < 0) { |
| 835 |
sp->wrote_errors = TRUE; |
| 836 |
return 1; |
| 837 |
} |
| 838 |
|
| 839 |
ptr = strchr(ptr, ' '); |
| 840 |
if (ptr == NULL) { |
| 841 |
pr_trace_msg(trace_channel, 3, |
| 842 |
"bad control message (undelimited file size)"); |
| 843 |
write_confirm(p, channel_id, 1, |
| 844 |
pstrcat(p, sp->path, ": bad control message (undelimited file size)", |
| 845 |
NULL)); |
| 846 |
sp->wrote_errors = TRUE; |
| 847 |
return 1; |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
ptr++; |
| 852 |
if (recv_filename(p, channel_id, ptr, sp) < 0) { |
| 853 |
sp->wrote_errors = TRUE; |
| 854 |
return 1; |
| 855 |
} |
| 856 |
|
| 857 |
sp->recvd_finfo = TRUE; |
| 858 |
|
| 859 |
if (have_dir) { |
| 860 |
struct scp_path *parent_sp; |
| 861 |
|
| 862 |
pr_fs_clear_cache2(sp->filename); |
| 863 |
if (pr_fsio_stat(sp->filename, &st) < 0) { |
| 864 |
int xerrno = errno; |
| 865 |
|
| 866 |
|
| 867 |
if (xerrno == ENOENT) { |
| 868 |
pr_trace_msg(trace_channel, 5, "creating directory '%s'", sp->filename); |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
pr_fs_clear_cache2(sp->filename); |
| 875 |
if (pr_fsio_smkdir(p, sp->filename, 0777, (uid_t) -1, (gid_t) -1) < 0) { |
| 876 |
xerrno = errno; |
| 877 |
|
| 878 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 879 |
"scp: error creating directory '%s': %s", sp->filename, |
| 880 |
strerror(xerrno)); |
| 881 |
write_confirm(p, channel_id, 1, |
| 882 |
pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL)); |
| 883 |
sp->wrote_errors = TRUE; |
| 884 |
|
| 885 |
errno = xerrno; |
| 886 |
return 1; |
| 887 |
} |
| 888 |
|
| 889 |
sftp_misc_chown_path(p, sp->filename); |
| 890 |
|
| 891 |
} else { |
| 892 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 893 |
"scp: error checking directory '%s': %s", sp->filename, |
| 894 |
strerror(xerrno)); |
| 895 |
write_confirm(p, channel_id, 1, |
| 896 |
pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL)); |
| 897 |
sp->wrote_errors = TRUE; |
| 898 |
|
| 899 |
errno = xerrno; |
| 900 |
return 1; |
| 901 |
} |
| 902 |
|
| 903 |
} else { |
| 904 |
|
| 905 |
if (!S_ISDIR(st.st_mode)) { |
| 906 |
pr_trace_msg(trace_channel, 2, "error handling '%s': %s", |
| 907 |
sp->best_path, strerror(ENOTDIR)); |
| 908 |
write_confirm(p, channel_id, 1, |
| 909 |
pstrcat(p, sp->filename, ": ", strerror(ENOTDIR), NULL)); |
| 910 |
sp->wrote_errors = TRUE; |
| 911 |
return 1; |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
parent_sp = pcalloc(scp_pool, sizeof(struct scp_path)); |
| 925 |
parent_sp->orig_path = pstrdup(scp_pool, sp->orig_path); |
| 926 |
parent_sp->path = pstrdup(scp_pool, sp->filename); |
| 927 |
parent_sp->filename = pstrdup(scp_pool, sp->filename); |
| 928 |
parent_sp->best_path = pstrdup(scp_pool, sp->best_path); |
| 929 |
|
| 930 |
|
| 931 |
parent_sp->times[0].tv_sec = sp->times[0].tv_sec; |
| 932 |
parent_sp->times[0].tv_usec = sp->times[0].tv_usec; |
| 933 |
parent_sp->times[1].tv_sec = sp->times[1].tv_sec; |
| 934 |
parent_sp->times[1].tv_usec = sp->times[1].tv_usec; |
| 935 |
parent_sp->recvd_timeinfo = sp->recvd_timeinfo; |
| 936 |
|
| 937 |
|
| 938 |
parent_sp->perms = sp->perms; |
| 939 |
parent_sp->parent_dir = sp->parent_dir; |
| 940 |
|
| 941 |
|
| 942 |
reset_path(sp); |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
sp->path = pstrdup(scp_pool, parent_sp->filename); |
| 948 |
sp->parent_dir = parent_sp; |
| 949 |
|
| 950 |
write_confirm(p, channel_id, 0, NULL); |
| 951 |
return 0; |
| 952 |
} |
| 953 |
|
| 954 |
pr_scoreboard_entry_update(session.pid, |
| 955 |
PR_SCORE_CMD, "%s", "scp upload", NULL, NULL); |
| 956 |
pr_scoreboard_entry_update(session.pid, |
| 957 |
PR_SCORE_CMD_ARG, "%s", sp->best_path, NULL, NULL); |
| 958 |
|
| 959 |
cmd = scp_cmd_alloc(p, C_STOR, sp->best_path); |
| 960 |
|
| 961 |
pr_fs_clear_cache2(sp->best_path); |
| 962 |
if (exists2(p, sp->best_path)) { |
| 963 |
if (pr_table_add(cmd->notes, "mod_xfer.file-modified", |
| 964 |
pstrdup(cmd->pool, "true"), 0) < 0) { |
| 965 |
if (errno != EEXIST) { |
| 966 |
pr_log_pri(PR_LOG_NOTICE, |
| 967 |
"notice: error adding 'mod_xfer.file-modified' note: %s", |
| 968 |
strerror(errno)); |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
sp->file_existed = TRUE; |
| 973 |
} |
| 974 |
|
| 975 |
if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) { |
| 976 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 977 |
"scp upload to '%s' blocked by '%s' handler", sp->path, |
| 978 |
(char *) cmd->argv[0]); |
| 979 |
|
| 980 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 981 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 982 |
|
| 983 |
write_confirm(p, channel_id, 1, |
| 984 |
pstrcat(p, sp->filename, ": ", strerror(EACCES), NULL)); |
| 985 |
sp->wrote_errors = TRUE; |
| 986 |
|
| 987 |
return 1; |
| 988 |
} |
| 989 |
|
| 990 |
if (strcmp(sp->filename, cmd->arg) != 0) { |
| 991 |
sp->filename = cmd->arg; |
| 992 |
sp->best_path = dir_canonical_vpath(scp_pool, sp->filename); |
| 993 |
} |
| 994 |
|
| 995 |
if (session.xfer.xfer_type == STOR_HIDDEN) { |
| 996 |
hiddenstore_path = pr_table_get(cmd->notes, "mod_xfer.store-hidden-path", |
| 997 |
NULL); |
| 998 |
} |
| 999 |
|
| 1000 |
if (!dir_check(p, cmd, G_WRITE, (char *) sp->best_path, NULL)) { |
| 1001 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1002 |
"scp upload to '%s' blocked by <Limit> configuration", sp->best_path); |
| 1003 |
|
| 1004 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1005 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1006 |
|
| 1007 |
write_confirm(p, channel_id, 1, |
| 1008 |
pstrcat(p, sp->filename, ": ", strerror(EACCES), NULL)); |
| 1009 |
sp->wrote_errors = TRUE; |
| 1010 |
|
| 1011 |
return 1; |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
sp->fh = pr_fsio_open(hiddenstore_path ? hiddenstore_path : sp->best_path, |
| 1021 |
O_WRONLY|O_CREAT|O_NONBLOCK|O_TRUNC); |
| 1022 |
if (sp->fh == NULL) { |
| 1023 |
int xerrno = errno; |
| 1024 |
|
| 1025 |
(void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): " |
| 1026 |
"error opening '%s': %s", "scp upload", session.user, |
| 1027 |
pr_uid2str(cmd->tmp_pool, session.uid), pr_gid2str(NULL, session.gid), |
| 1028 |
hiddenstore_path ? hiddenstore_path : sp->best_path, strerror(xerrno)); |
| 1029 |
|
| 1030 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1031 |
"scp: error opening '%s': %s", |
| 1032 |
hiddenstore_path ? hiddenstore_path : sp->best_path, strerror(xerrno)); |
| 1033 |
|
| 1034 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1035 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1036 |
|
| 1037 |
write_confirm(p, channel_id, 1, |
| 1038 |
pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL)); |
| 1039 |
sp->wrote_errors = TRUE; |
| 1040 |
|
| 1041 |
errno = xerrno; |
| 1042 |
return 1; |
| 1043 |
|
| 1044 |
} else { |
| 1045 |
off_t curr_offset; |
| 1046 |
|
| 1047 |
|
| 1048 |
curr_offset = pr_fsio_lseek(sp->fh, (off_t) 0, SEEK_CUR); |
| 1049 |
if (curr_offset != (off_t) -1) { |
| 1050 |
off_t *file_offset; |
| 1051 |
|
| 1052 |
file_offset = palloc(cmd->pool, sizeof(off_t)); |
| 1053 |
*file_offset = (off_t) curr_offset; |
| 1054 |
(void) pr_table_add(cmd->notes, "mod_xfer.file-offset", file_offset, |
| 1055 |
sizeof(off_t)); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
if (hiddenstore_path) { |
| 1060 |
sp->hiddenstore = TRUE; |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (pr_fsio_fstat(sp->fh, &st) < 0) { |
| 1064 |
pr_trace_msg(trace_channel, 3, |
| 1065 |
"fstat(2) error on '%s': %s", sp->fh->fh_path, strerror(errno)); |
| 1066 |
|
| 1067 |
} else { |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
#ifdef S_ISFIFO |
| 1073 |
if (S_ISFIFO(st.st_mode)) { |
| 1074 |
if (sftp_opts & SFTP_OPT_IGNORE_FIFOS) { |
| 1075 |
int xerrno = EPERM; |
| 1076 |
|
| 1077 |
(void) pr_fsio_close(sp->fh); |
| 1078 |
sp->fh = NULL; |
| 1079 |
|
| 1080 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1081 |
"scp: error using FIFO '%s': %s (IgnoreFIFOs SFTPOption in effect)", |
| 1082 |
hiddenstore_path ? hiddenstore_path : sp->best_path, |
| 1083 |
strerror(xerrno)); |
| 1084 |
|
| 1085 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1086 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1087 |
|
| 1088 |
write_confirm(p, channel_id, 1, |
| 1089 |
pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL)); |
| 1090 |
sp->wrote_errors = TRUE; |
| 1091 |
|
| 1092 |
errno = xerrno; |
| 1093 |
return 1; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
#endif |
| 1097 |
} |
| 1098 |
|
| 1099 |
if (pr_fsio_set_block(sp->fh) < 0) { |
| 1100 |
pr_trace_msg(trace_channel, 3, |
| 1101 |
"error setting fd %d (file '%s') as blocking: %s", sp->fh->fh_fd, |
| 1102 |
sp->fh->fh_path, strerror(errno)); |
| 1103 |
} |
| 1104 |
|
| 1105 |
sftp_misc_chown_file(p, sp->fh); |
| 1106 |
|
| 1107 |
write_confirm(p, channel_id, 0, NULL); |
| 1108 |
return 0; |
| 1109 |
} |
| 1110 |
|
| 1111 |
static int recv_data(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 1112 |
unsigned char *data, uint32_t datalen) { |
| 1113 |
uint32_t writelen; |
| 1114 |
config_rec *c; |
| 1115 |
off_t nbytes_max_store = 0; |
| 1116 |
|
| 1117 |
|
| 1118 |
c = find_config(get_dir_ctxt(p, sp->fh->fh_path), CONF_PARAM, |
| 1119 |
"MaxStoreFileSize", FALSE); |
| 1120 |
if (c != NULL) { |
| 1121 |
nbytes_max_store = *((off_t *) c->argv[0]); |
| 1122 |
} |
| 1123 |
|
| 1124 |
writelen = datalen; |
| 1125 |
if (writelen > (sp->filesz - sp->recvlen)) { |
| 1126 |
writelen = (uint32_t) (sp->filesz - sp->recvlen); |
| 1127 |
} |
| 1128 |
|
| 1129 |
if (nbytes_max_store > 0) { |
| 1130 |
if (sp->recvlen > nbytes_max_store) { |
| 1131 |
#if defined(EFBIG) |
| 1132 |
int xerrno = EFBIG; |
| 1133 |
#elif defined(ENOSPC) |
| 1134 |
int xerrno = ENOSPC; |
| 1135 |
#else |
| 1136 |
int xerrno = EIO; |
| 1137 |
#endif |
| 1138 |
|
| 1139 |
pr_log_pri(PR_LOG_NOTICE, "MaxStoreFileSize (%" PR_LU " %s) reached: " |
| 1140 |
"aborting transfer of '%s'", (pr_off_t) nbytes_max_store, |
| 1141 |
nbytes_max_store != 1 ? "bytes" : "byte", sp->fh->fh_path); |
| 1142 |
|
| 1143 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1144 |
"error writing %lu bytes to '%s': %s " |
| 1145 |
"(MaxStoreFileSize %" PR_LU " exceeded)", (unsigned long) writelen, |
| 1146 |
sp->fh->fh_path, strerror(xerrno), (pr_off_t) nbytes_max_store); |
| 1147 |
|
| 1148 |
write_confirm(p, channel_id, 1, |
| 1149 |
pstrcat(p, sp->filename, ": write error: ", strerror(xerrno), NULL)); |
| 1150 |
sp->wrote_errors = TRUE; |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
errno = xerrno; |
| 1157 |
return 1; |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
if (writelen > 0) { |
| 1162 |
while (TRUE) { |
| 1163 |
int res; |
| 1164 |
|
| 1165 |
|
| 1166 |
res = pr_fsio_write(sp->fh, (char *) data, writelen); |
| 1167 |
if ((uint32_t) res != writelen) { |
| 1168 |
int xerrno = errno; |
| 1169 |
|
| 1170 |
if (xerrno == EINTR || |
| 1171 |
xerrno == EAGAIN) { |
| 1172 |
pr_signals_handle(); |
| 1173 |
continue; |
| 1174 |
} |
| 1175 |
|
| 1176 |
pr_trace_msg(trace_channel, 2, "error writing to '%s': %s", |
| 1177 |
sp->best_path, strerror(xerrno)); |
| 1178 |
write_confirm(p, channel_id, 1, |
| 1179 |
pstrcat(p, sp->filename, ": write error: ", strerror(xerrno), NULL)); |
| 1180 |
sp->wrote_errors = TRUE; |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
errno = xerrno; |
| 1187 |
return 1; |
| 1188 |
} |
| 1189 |
|
| 1190 |
break; |
| 1191 |
} |
| 1192 |
|
| 1193 |
sp->recvlen += writelen; |
| 1194 |
|
| 1195 |
session.xfer.total_bytes += writelen; |
| 1196 |
session.total_bytes += writelen; |
| 1197 |
|
| 1198 |
if (writelen < datalen) { |
| 1199 |
if (data[writelen] != '\0') { |
| 1200 |
pr_trace_msg(trace_channel, 2, "expected end-of-data marker when " |
| 1201 |
"receiving file data, received '%c'", data[writelen]); |
| 1202 |
} |
| 1203 |
|
| 1204 |
pr_throttle_pause(sp->recvlen, TRUE, 0); |
| 1205 |
|
| 1206 |
sp->recvd_data = TRUE; |
| 1207 |
return 1; |
| 1208 |
} |
| 1209 |
|
| 1210 |
pr_throttle_pause(sp->recvlen, FALSE, 0); |
| 1211 |
|
| 1212 |
} else { |
| 1213 |
|
| 1214 |
if (data[writelen] != '\0') { |
| 1215 |
pr_trace_msg(trace_channel, 2, "expected end-of-data marker when " |
| 1216 |
"receiving file data, received '%c'", data[writelen]); |
| 1217 |
} |
| 1218 |
|
| 1219 |
pr_throttle_pause(sp->recvlen, TRUE, 0); |
| 1220 |
|
| 1221 |
sp->recvd_data = TRUE; |
| 1222 |
return 1; |
| 1223 |
} |
| 1224 |
|
| 1225 |
return 0; |
| 1226 |
} |
| 1227 |
|
| 1228 |
static int recv_eod(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 1229 |
unsigned char *buf, uint32_t buflen, unsigned char **remain, |
| 1230 |
uint32_t *remainlen) { |
| 1231 |
struct scp_path *parent_sp; |
| 1232 |
unsigned char *data = NULL; |
| 1233 |
uint32_t datalen = 0; |
| 1234 |
int ok = TRUE, res; |
| 1235 |
|
| 1236 |
res = recv_ctl(p, channel_id, sp, buf, buflen, &data, &datalen); |
| 1237 |
if (res != 1) { |
| 1238 |
return res; |
| 1239 |
} |
| 1240 |
|
| 1241 |
if (data[0] != 'E') { |
| 1242 |
|
| 1243 |
*remain = data; |
| 1244 |
*remainlen = datalen; |
| 1245 |
|
| 1246 |
errno = EINVAL; |
| 1247 |
return -1; |
| 1248 |
} |
| 1249 |
|
| 1250 |
pr_trace_msg(trace_channel, 5, "'%s' control message: E", sp->path); |
| 1251 |
|
| 1252 |
parent_sp = sp->parent_dir; |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
if (!(sftp_opts & SFTP_OPT_IGNORE_SCP_UPLOAD_PERMS)) { |
| 1258 |
pr_trace_msg(trace_channel, 9, "setting perms %04o on directory '%s'", |
| 1259 |
(unsigned int) parent_sp->perms, parent_sp->path); |
| 1260 |
if (pr_fsio_chmod(parent_sp->path, parent_sp->perms) < 0) { |
| 1261 |
int xerrno = errno; |
| 1262 |
|
| 1263 |
pr_trace_msg(trace_channel, 2, "error setting mode %04o on '%s': %s", |
| 1264 |
(unsigned int) parent_sp->perms, parent_sp->path, strerror(xerrno)); |
| 1265 |
write_confirm(p, channel_id, 1, |
| 1266 |
pstrcat(p, parent_sp->path, ": error setting mode: ", strerror(xerrno), |
| 1267 |
NULL)); |
| 1268 |
parent_sp->wrote_errors = TRUE; |
| 1269 |
ok = FALSE; |
| 1270 |
} |
| 1271 |
|
| 1272 |
} else { |
| 1273 |
pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSCPUploadPerms' " |
| 1274 |
"configured, ignoring perms sent by client"); |
| 1275 |
} |
| 1276 |
|
| 1277 |
if (parent_sp->recvd_timeinfo) { |
| 1278 |
pr_trace_msg(trace_channel, 9, "setting times on directory '%s'", |
| 1279 |
parent_sp->filename); |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
if (!(sftp_opts & SFTP_OPT_IGNORE_SCP_UPLOAD_TIMES)) { |
| 1285 |
if (pr_fsio_utimes(parent_sp->filename, parent_sp->times) < 0) { |
| 1286 |
int xerrno = errno; |
| 1287 |
|
| 1288 |
pr_trace_msg(trace_channel, 2, |
| 1289 |
"error setting atime %lu, mtime %lu on '%s': %s", |
| 1290 |
(unsigned long) sp->times[0].tv_sec, |
| 1291 |
(unsigned long) sp->times[1].tv_sec, parent_sp->filename, |
| 1292 |
strerror(xerrno)); |
| 1293 |
|
| 1294 |
write_confirm(p, channel_id, 1, |
| 1295 |
pstrcat(p, parent_sp->filename, ": error setting times: ", |
| 1296 |
strerror(xerrno), NULL)); |
| 1297 |
parent_sp->wrote_errors = TRUE; |
| 1298 |
ok = FALSE; |
| 1299 |
} |
| 1300 |
|
| 1301 |
} else { |
| 1302 |
pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSCPUploadTimes' " |
| 1303 |
"configured, ignoring times sent by client"); |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
if (ok) { |
| 1308 |
write_confirm(p, channel_id, 0, NULL); |
| 1309 |
} |
| 1310 |
|
| 1311 |
return 1; |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
static int recv_path(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 1319 |
unsigned char *data, uint32_t datalen) { |
| 1320 |
int res; |
| 1321 |
cmd_rec *cmd = NULL; |
| 1322 |
char *curr_path = NULL; |
| 1323 |
|
| 1324 |
if (!sp->checked_errors) { |
| 1325 |
res = recv_errors(p, channel_id, sp, data, datalen); |
| 1326 |
if (res == 1) { |
| 1327 |
return 1; |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
if (!sp->have_mode) { |
| 1332 |
struct stat st; |
| 1333 |
|
| 1334 |
pr_fs_clear_cache2(sp->path); |
| 1335 |
res = pr_fsio_stat(sp->path, &st); |
| 1336 |
if (res == 0) { |
| 1337 |
sp->st_mode = st.st_mode; |
| 1338 |
sp->have_mode = TRUE; |
| 1339 |
} |
| 1340 |
|
| 1341 |
if (scp_opts & SFTP_SCP_OPT_DIR) { |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
if (res == 0) { |
| 1346 |
if (!S_ISDIR(st.st_mode)) { |
| 1347 |
write_confirm(p, channel_id, 1, |
| 1348 |
pstrcat(p, sp->path, ": ", strerror(ENOTDIR), NULL)); |
| 1349 |
sp->wrote_errors = TRUE; |
| 1350 |
return 1; |
| 1351 |
} |
| 1352 |
|
| 1353 |
} else { |
| 1354 |
write_confirm(p, channel_id, 1, |
| 1355 |
pstrcat(p, sp->path, ": ", strerror(errno), NULL)); |
| 1356 |
sp->wrote_errors = TRUE; |
| 1357 |
return 1; |
| 1358 |
} |
| 1359 |
|
| 1360 |
} else { |
| 1361 |
char *ptr; |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
ptr = strrchr(sp->path, '/'); |
| 1367 |
if (ptr != NULL) { |
| 1368 |
*ptr = '\0'; |
| 1369 |
|
| 1370 |
pr_fs_clear_cache2(sp->path); |
| 1371 |
res = pr_fsio_stat(sp->path, &st); |
| 1372 |
*ptr = '/'; |
| 1373 |
|
| 1374 |
if (res < 0) { |
| 1375 |
write_confirm(p, channel_id, 1, |
| 1376 |
pstrcat(p, sp->path, ": ", strerror(errno), NULL)); |
| 1377 |
sp->wrote_errors = TRUE; |
| 1378 |
return 1; |
| 1379 |
} |
| 1380 |
} |
| 1381 |
} |
| 1382 |
} |
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
if (sp->parent_dir != NULL && |
| 1393 |
sp->recvd_finfo == FALSE && |
| 1394 |
(sp->recvlen == 0 || sp->recvd_data)) { |
| 1395 |
unsigned char *remain = NULL; |
| 1396 |
uint32_t remainlen = 0; |
| 1397 |
|
| 1398 |
res = recv_eod(p, channel_id, sp, data, datalen, &remain, &remainlen); |
| 1399 |
if (res == 0) { |
| 1400 |
return res; |
| 1401 |
} |
| 1402 |
|
| 1403 |
if (res == 1) { |
| 1404 |
struct scp_path *parent_dir = NULL; |
| 1405 |
|
| 1406 |
if (sp->parent_dir != NULL) { |
| 1407 |
parent_dir = sp->parent_dir->parent_dir; |
| 1408 |
} |
| 1409 |
|
| 1410 |
if (parent_dir != NULL) { |
| 1411 |
pr_trace_msg(trace_channel, 18, |
| 1412 |
"received EOD, resetting path from '%s' to '%s'", sp->path, |
| 1413 |
parent_dir->path); |
| 1414 |
sp->path = parent_dir->path; |
| 1415 |
|
| 1416 |
} else { |
| 1417 |
if (sp->orig_path != NULL) { |
| 1418 |
sp->path = pstrdup(scp_pool, sp->orig_path); |
| 1419 |
} |
| 1420 |
|
| 1421 |
pr_trace_msg(trace_channel, 18, |
| 1422 |
"received EOD, no parent found for '%s'", sp->path); |
| 1423 |
} |
| 1424 |
|
| 1425 |
sp->parent_dir = parent_dir; |
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
return 1; |
| 1432 |
} |
| 1433 |
|
| 1434 |
data = remain; |
| 1435 |
datalen = remainlen; |
| 1436 |
} |
| 1437 |
|
| 1438 |
if ((scp_opts & SFTP_SCP_OPT_PRESERVE) && |
| 1439 |
!sp->recvd_timeinfo && |
| 1440 |
!sp->recvd_finfo) { |
| 1441 |
unsigned char *remain = NULL; |
| 1442 |
uint32_t remainlen = 0; |
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
res = recv_timeinfo(p, channel_id, sp, data, datalen, &remain, &remainlen); |
| 1450 |
if (res < 0) { |
| 1451 |
data = remain; |
| 1452 |
datalen = remainlen; |
| 1453 |
|
| 1454 |
} else { |
| 1455 |
return res; |
| 1456 |
} |
| 1457 |
} |
| 1458 |
|
| 1459 |
if (!sp->recvd_finfo) { |
| 1460 |
return recv_finfo(p, channel_id, sp, data, datalen); |
| 1461 |
} |
| 1462 |
|
| 1463 |
if (!sp->recvd_data && |
| 1464 |
sp->recvlen != sp->filesz) { |
| 1465 |
if (cmd == NULL) { |
| 1466 |
cmd = scp_cmd_alloc(p, C_STOR, sp->best_path); |
| 1467 |
|
| 1468 |
if (pr_table_add(cmd->notes, "mod_xfer.store-path", |
| 1469 |
pstrdup(p, sp->best_path), 0) < 0) { |
| 1470 |
if (errno != EEXIST) { |
| 1471 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1472 |
"error adding 'mod_xfer.store-path for SCP upload: %s", |
| 1473 |
strerror(errno)); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
pr_throttle_init(cmd); |
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
res = recv_data(p, channel_id, sp, data, datalen); |
| 1485 |
if (res != 1) { |
| 1486 |
return res; |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
if (sp->wrote_errors == FALSE) { |
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
if (S_ISREG(sp->st_mode)) { |
| 1495 |
pr_trace_msg(trace_channel, 9, "truncating file '%s' to %" PR_LU " bytes", |
| 1496 |
sp->fh->fh_path, (pr_off_t) sp->filesz); |
| 1497 |
|
| 1498 |
if (pr_fsio_ftruncate(sp->fh, sp->filesz) < 0) { |
| 1499 |
int xerrno = errno; |
| 1500 |
|
| 1501 |
pr_trace_msg(trace_channel, 2, "error truncating '%s' to %" PR_LU |
| 1502 |
" bytes: %s", sp->best_path, (pr_off_t) sp->filesz, strerror(xerrno)); |
| 1503 |
|
| 1504 |
write_confirm(p, channel_id, 1, |
| 1505 |
pstrcat(p, sp->filename, ": error truncating file: ", |
| 1506 |
strerror(xerrno), NULL)); |
| 1507 |
sp->wrote_errors = TRUE; |
| 1508 |
} |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
if (sp->wrote_errors == FALSE) { |
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
if (!(sftp_opts & SFTP_OPT_IGNORE_SCP_UPLOAD_PERMS)) { |
| 1517 |
pr_trace_msg(trace_channel, 9, "setting perms %04o on file '%s'", |
| 1518 |
(unsigned int) sp->perms, sp->fh->fh_path); |
| 1519 |
|
| 1520 |
if (pr_fsio_fchmod(sp->fh, sp->perms) < 0) { |
| 1521 |
int xerrno = errno; |
| 1522 |
|
| 1523 |
pr_trace_msg(trace_channel, 2, "error setting mode %04o on '%s': %s", |
| 1524 |
(unsigned int) sp->perms, sp->best_path, strerror(xerrno)); |
| 1525 |
|
| 1526 |
write_confirm(p, channel_id, 1, |
| 1527 |
pstrcat(p, sp->filename, ": error setting mode: ", strerror(xerrno), |
| 1528 |
NULL)); |
| 1529 |
sp->wrote_errors = TRUE; |
| 1530 |
} |
| 1531 |
|
| 1532 |
} else { |
| 1533 |
pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSCPUploadPerms' " |
| 1534 |
"configured, ignoring perms sent by client"); |
| 1535 |
} |
| 1536 |
} |
| 1537 |
|
| 1538 |
if (sp->fh) { |
| 1539 |
curr_path = pstrdup(scp_pool, sp->fh->fh_path); |
| 1540 |
|
| 1541 |
|
| 1542 |
session.curr_cmd = C_STOR; |
| 1543 |
|
| 1544 |
res = pr_fsio_close(sp->fh); |
| 1545 |
if (res < 0) { |
| 1546 |
int xerrno = errno; |
| 1547 |
|
| 1548 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1549 |
"scp: error closing '%s': %s", sp->best_path, strerror(xerrno)); |
| 1550 |
|
| 1551 |
write_confirm(p, channel_id, 1, |
| 1552 |
pstrcat(p, sp->filename, ": ", strerror(xerrno), NULL)); |
| 1553 |
sp->wrote_errors = TRUE; |
| 1554 |
} |
| 1555 |
|
| 1556 |
sp->fh = NULL; |
| 1557 |
} |
| 1558 |
|
| 1559 |
if (sp->hiddenstore == TRUE && |
| 1560 |
curr_path != NULL) { |
| 1561 |
if (sp->wrote_errors == TRUE) { |
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
pr_trace_msg(trace_channel, 8, "deleting HiddenStores path '%s'", |
| 1566 |
curr_path); |
| 1567 |
|
| 1568 |
if (pr_fsio_unlink(curr_path) < 0) { |
| 1569 |
if (errno != ENOENT) { |
| 1570 |
pr_log_debug(DEBUG0, MOD_SFTP_VERSION |
| 1571 |
": error deleting HiddenStores file '%s': %s", curr_path, |
| 1572 |
strerror(errno)); |
| 1573 |
} |
| 1574 |
} |
| 1575 |
|
| 1576 |
} else { |
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
pr_trace_msg(trace_channel, 8, |
| 1581 |
"renaming HiddenStores path '%s' to '%s'", curr_path, sp->best_path); |
| 1582 |
|
| 1583 |
res = pr_fsio_rename(curr_path, sp->best_path); |
| 1584 |
if (res < 0) { |
| 1585 |
int xerrno = errno; |
| 1586 |
|
| 1587 |
pr_log_pri(PR_LOG_WARNING, "Rename of %s to %s failed: %s", |
| 1588 |
curr_path, sp->best_path, strerror(xerrno)); |
| 1589 |
|
| 1590 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1591 |
"renaming of HiddenStore path '%s' to '%s' failed: %s", |
| 1592 |
curr_path, sp->best_path, strerror(xerrno)); |
| 1593 |
|
| 1594 |
if (pr_fsio_unlink(curr_path) < 0) { |
| 1595 |
pr_trace_msg(trace_channel, 1, |
| 1596 |
"error deleting HiddenStores file '%s': %s", curr_path, |
| 1597 |
strerror(errno)); |
| 1598 |
} |
| 1599 |
} |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
if (sp->recvd_timeinfo) { |
| 1607 |
pr_trace_msg(trace_channel, 9, "setting times on file '%s'", sp->filename); |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
if (!(sftp_opts & SFTP_OPT_IGNORE_SCP_UPLOAD_TIMES)) { |
| 1613 |
if (pr_fsio_utimes(sp->filename, sp->times) < 0) { |
| 1614 |
int xerrno = errno; |
| 1615 |
|
| 1616 |
pr_trace_msg(trace_channel, 2, |
| 1617 |
"error setting atime %lu, mtime %lu on '%s': %s", |
| 1618 |
(unsigned long) sp->times[0].tv_sec, |
| 1619 |
(unsigned long) sp->times[1].tv_sec, sp->best_path, strerror(xerrno)); |
| 1620 |
|
| 1621 |
write_confirm(p, channel_id, 1, |
| 1622 |
pstrcat(p, sp->filename, ": error setting times: ", strerror(xerrno), |
| 1623 |
NULL)); |
| 1624 |
sp->wrote_errors = TRUE; |
| 1625 |
} |
| 1626 |
|
| 1627 |
} else { |
| 1628 |
pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSCPUploadTimes' " |
| 1629 |
"configured, ignoring times sent by client"); |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
if (!sp->wrote_errors) { |
| 1634 |
|
| 1635 |
write_confirm(p, channel_id, 0, NULL); |
| 1636 |
|
| 1637 |
if (cmd == NULL) { |
| 1638 |
cmd = scp_cmd_alloc(p, C_STOR, sp->best_path); |
| 1639 |
|
| 1640 |
if (pr_table_add(cmd->notes, "mod_xfer.store-path", |
| 1641 |
pstrdup(p, sp->best_path), 0) < 0) { |
| 1642 |
if (errno != EEXIST) { |
| 1643 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1644 |
"error adding 'mod_xfer.store-path: %s", strerror(errno)); |
| 1645 |
} |
| 1646 |
} |
| 1647 |
} |
| 1648 |
|
| 1649 |
if (sp->file_existed) { |
| 1650 |
if (pr_table_add(cmd->notes, "mod_xfer.file-modified", |
| 1651 |
pstrdup(cmd->pool, "true"), 0) < 0) { |
| 1652 |
if (errno != EEXIST) { |
| 1653 |
pr_log_pri(PR_LOG_NOTICE, |
| 1654 |
"notice: error adding 'mod_xfer.file-modified' note: %s", |
| 1655 |
strerror(errno)); |
| 1656 |
} |
| 1657 |
} |
| 1658 |
} |
| 1659 |
|
| 1660 |
session.xfer.path = sftp_misc_vroot_abs_path(session.xfer.p, |
| 1661 |
session.xfer.path, FALSE); |
| 1662 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 1663 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 1664 |
|
| 1665 |
} else { |
| 1666 |
if (cmd == NULL) { |
| 1667 |
cmd = scp_cmd_alloc(p, C_STOR, sp->best_path); |
| 1668 |
|
| 1669 |
if (pr_table_add(cmd->notes, "mod_xfer.store-path", |
| 1670 |
pstrdup(p, sp->best_path), 0) < 0) { |
| 1671 |
if (errno != EEXIST) { |
| 1672 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1673 |
"error adding 'mod_xfer.store-path: %s", strerror(errno)); |
| 1674 |
} |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
if (sp->file_existed) { |
| 1679 |
if (pr_table_add(cmd->notes, "mod_xfer.file-modified", |
| 1680 |
pstrdup(cmd->pool, "true"), 0) < 0) { |
| 1681 |
pr_log_pri(PR_LOG_NOTICE, |
| 1682 |
"notice: error adding 'mod_xfer.file-modified' note: %s", |
| 1683 |
strerror(errno)); |
| 1684 |
} |
| 1685 |
} |
| 1686 |
|
| 1687 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 1688 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 1689 |
} |
| 1690 |
|
| 1691 |
return 1; |
| 1692 |
} |
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
static int send_timeinfo(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 1697 |
struct stat *st) { |
| 1698 |
int res; |
| 1699 |
unsigned char ctrl_msg[64]; |
| 1700 |
size_t ctrl_msglen; |
| 1701 |
|
| 1702 |
memset(ctrl_msg, '\0', sizeof(ctrl_msg)); |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
pr_snprintf((char *) ctrl_msg, sizeof(ctrl_msg), "T%lu 0 %lu 0", |
| 1714 |
(unsigned long) (st->st_mtime > 0 ? st->st_mtime : 0), |
| 1715 |
(unsigned long) (st->st_atime > 0 ? st->st_atime : 0)); |
| 1716 |
|
| 1717 |
pr_trace_msg(trace_channel, 3, "sending '%s' T (timestamps): %s", sp->path, |
| 1718 |
ctrl_msg); |
| 1719 |
|
| 1720 |
ctrl_msg[strlen((char *) ctrl_msg)] = '\n'; |
| 1721 |
ctrl_msglen = strlen((char *) ctrl_msg); |
| 1722 |
|
| 1723 |
need_confirm = TRUE; |
| 1724 |
|
| 1725 |
res = sftp_channel_write_data(p, channel_id, ctrl_msg, ctrl_msglen); |
| 1726 |
if (res < 0) |
| 1727 |
return -1; |
| 1728 |
|
| 1729 |
sp->sent_timeinfo = TRUE; |
| 1730 |
return 0; |
| 1731 |
} |
| 1732 |
|
| 1733 |
static int send_dirinfo(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 1734 |
struct stat *st) { |
| 1735 |
int res; |
| 1736 |
unsigned char ctrl_msg[1536]; |
| 1737 |
size_t ctrl_msglen; |
| 1738 |
char *tmp; |
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
tmp = strrchr(sp->path, '/'); |
| 1744 |
if (tmp == NULL) { |
| 1745 |
tmp = sp->path; |
| 1746 |
|
| 1747 |
} else { |
| 1748 |
tmp++; |
| 1749 |
} |
| 1750 |
|
| 1751 |
memset(ctrl_msg, '\0', sizeof(ctrl_msg)); |
| 1752 |
pr_snprintf((char *) ctrl_msg, sizeof(ctrl_msg), "D%04o 0 %.1024s", |
| 1753 |
(unsigned int) (st->st_mode & SFTP_SCP_ST_MODE_MASK), tmp); |
| 1754 |
|
| 1755 |
pr_trace_msg(trace_channel, 3, "sending '%s' D (directory): %s", sp->path, |
| 1756 |
ctrl_msg); |
| 1757 |
|
| 1758 |
ctrl_msg[strlen((char *) ctrl_msg)] = '\n'; |
| 1759 |
ctrl_msglen = strlen((char *) ctrl_msg); |
| 1760 |
|
| 1761 |
need_confirm = TRUE; |
| 1762 |
|
| 1763 |
res = sftp_channel_write_data(p, channel_id, ctrl_msg, ctrl_msglen); |
| 1764 |
if (res < 0) { |
| 1765 |
return -1; |
| 1766 |
} |
| 1767 |
|
| 1768 |
sp->sent_dirinfo = TRUE; |
| 1769 |
return 0; |
| 1770 |
} |
| 1771 |
|
| 1772 |
static int send_finfo(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 1773 |
struct stat *st) { |
| 1774 |
int res; |
| 1775 |
unsigned char ctrl_msg[1536]; |
| 1776 |
size_t ctrl_msglen; |
| 1777 |
char *tmp; |
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
tmp = strrchr(sp->path, '/'); |
| 1783 |
if (tmp == NULL) { |
| 1784 |
tmp = sp->path; |
| 1785 |
|
| 1786 |
} else { |
| 1787 |
tmp++; |
| 1788 |
} |
| 1789 |
|
| 1790 |
memset(ctrl_msg, '\0', sizeof(ctrl_msg)); |
| 1791 |
pr_snprintf((char *) ctrl_msg, sizeof(ctrl_msg), "C%04o %" PR_LU " %.1024s", |
| 1792 |
(unsigned int) (st->st_mode & SFTP_SCP_ST_MODE_MASK), |
| 1793 |
(pr_off_t) st->st_size, tmp); |
| 1794 |
|
| 1795 |
pr_trace_msg(trace_channel, 3, "sending '%s' C (info): %s", sp->path, |
| 1796 |
ctrl_msg); |
| 1797 |
|
| 1798 |
ctrl_msg[strlen((char *) ctrl_msg)] = '\n'; |
| 1799 |
ctrl_msglen = strlen((char *) ctrl_msg); |
| 1800 |
|
| 1801 |
need_confirm = TRUE; |
| 1802 |
|
| 1803 |
res = sftp_channel_write_data(p, channel_id, ctrl_msg, ctrl_msglen); |
| 1804 |
if (res < 0) |
| 1805 |
return -1; |
| 1806 |
|
| 1807 |
sp->sent_finfo = TRUE; |
| 1808 |
return 0; |
| 1809 |
} |
| 1810 |
|
| 1811 |
static int send_data(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 1812 |
struct stat *st) { |
| 1813 |
unsigned char *chunk; |
| 1814 |
size_t chunksz; |
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
chunksz = pr_config_get_server_xfer_bufsz(PR_NETIO_IO_WR) + 1; |
| 1820 |
chunk = palloc(p, chunksz); |
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
while (TRUE) { |
| 1826 |
int res, chunklen; |
| 1827 |
|
| 1828 |
pr_signals_handle(); |
| 1829 |
|
| 1830 |
if (S_ISREG(st->st_mode)) { |
| 1831 |
|
| 1832 |
if (pr_fsio_lseek(sp->fh, sp->sentlen, SEEK_SET) < 0) { |
| 1833 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1834 |
"error seeking to offset %" PR_LU " in '%s': %s", |
| 1835 |
(pr_off_t) sp->sentlen, sp->path, strerror(errno)); |
| 1836 |
return 1; |
| 1837 |
} |
| 1838 |
|
| 1839 |
pr_trace_msg(trace_channel, 15, "at %.2f%% (%" PR_LU " of %" PR_LU |
| 1840 |
" bytes) of '%s'", |
| 1841 |
(float) (((float) sp->sentlen / (float) st->st_size) * 100), |
| 1842 |
(pr_off_t) sp->sentlen, (pr_off_t) st->st_size, sp->path); |
| 1843 |
} |
| 1844 |
|
| 1845 |
chunklen = pr_fsio_read(sp->fh, (char *) chunk, chunksz - 1); |
| 1846 |
if (chunklen < 0) { |
| 1847 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1848 |
"error reading from '%s': %s", sp->path, strerror(errno)); |
| 1849 |
return 1; |
| 1850 |
} |
| 1851 |
|
| 1852 |
session.xfer.total_bytes += chunklen; |
| 1853 |
session.total_bytes += chunklen; |
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
if (sp->sentlen + chunklen == st->st_size) { |
| 1859 |
chunk[chunklen++] = '\0'; |
| 1860 |
need_confirm = TRUE; |
| 1861 |
|
| 1862 |
pr_throttle_pause(sp->sentlen, TRUE, 0); |
| 1863 |
|
| 1864 |
} else { |
| 1865 |
pr_throttle_pause(sp->sentlen, FALSE, 0); |
| 1866 |
} |
| 1867 |
|
| 1868 |
pr_trace_msg(trace_channel, 3, "sending '%s' data (%lu bytes)", sp->path, |
| 1869 |
need_confirm ? (unsigned long) (chunklen - 1) : (unsigned long) chunklen); |
| 1870 |
|
| 1871 |
res = sftp_channel_write_data(p, channel_id, chunk, chunklen); |
| 1872 |
if (res < 0) { |
| 1873 |
return 1; |
| 1874 |
} |
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
if (pr_data_get_timeout(PR_DATA_TIMEOUT_NO_TRANSFER) > 0) { |
| 1882 |
pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE); |
| 1883 |
} |
| 1884 |
|
| 1885 |
if (pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED) > 0) { |
| 1886 |
pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE); |
| 1887 |
} |
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
while ((sftp_sess_state & SFTP_SESS_STATE_REKEYING) || |
| 1902 |
sftp_channel_get_windowsz(channel_id) == 0) { |
| 1903 |
pr_signals_handle(); |
| 1904 |
|
| 1905 |
if (sftp_ssh2_packet_process(sftp_pool) < 0) { |
| 1906 |
return 1; |
| 1907 |
} |
| 1908 |
} |
| 1909 |
|
| 1910 |
sp->sentlen += chunklen; |
| 1911 |
if (sp->sentlen >= st->st_size) { |
| 1912 |
sp->sent_data = TRUE; |
| 1913 |
break; |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
return 0; |
| 1918 |
} |
| 1919 |
|
| 1920 |
static int send_dir(pool *p, uint32_t channel_id, struct scp_path *sp, |
| 1921 |
struct stat *st) { |
| 1922 |
struct dirent *dent; |
| 1923 |
struct stat link_st; |
| 1924 |
int res = 0; |
| 1925 |
|
| 1926 |
if (sp->dirh == NULL) { |
| 1927 |
sp->dirh = pr_fsio_opendir(sp->path); |
| 1928 |
if (sp->dirh == NULL) { |
| 1929 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 1930 |
"error reading directory '%s': %s", sp->path, strerror(errno)); |
| 1931 |
return -1; |
| 1932 |
} |
| 1933 |
|
| 1934 |
|
| 1935 |
if (!sp->sent_dirinfo) { |
| 1936 |
return send_dirinfo(p, channel_id, sp, st); |
| 1937 |
} |
| 1938 |
} |
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
if (sp->dir_spi) { |
| 1946 |
res = send_path(p, channel_id, sp->dir_spi); |
| 1947 |
if (res <= 0) { |
| 1948 |
return res; |
| 1949 |
} |
| 1950 |
|
| 1951 |
|
| 1952 |
if (session.xfer.p != NULL) { |
| 1953 |
destroy_pool(session.xfer.p); |
| 1954 |
} |
| 1955 |
memset(&session.xfer, 0, sizeof(session.xfer)); |
| 1956 |
|
| 1957 |
sp->dir_spi = NULL; |
| 1958 |
return 0; |
| 1959 |
} |
| 1960 |
|
| 1961 |
while ((dent = pr_fsio_readdir(sp->dirh)) != NULL) { |
| 1962 |
struct scp_path *spi; |
| 1963 |
size_t pathlen; |
| 1964 |
|
| 1965 |
pr_signals_handle(); |
| 1966 |
|
| 1967 |
|
| 1968 |
if (strncmp(dent->d_name, ".", 2) == 0 || |
| 1969 |
strncmp(dent->d_name, "..", 3) == 0) { |
| 1970 |
continue; |
| 1971 |
} |
| 1972 |
|
| 1973 |
|
| 1974 |
spi = pcalloc(scp_pool, sizeof(struct scp_path)); |
| 1975 |
spi->path = pdircat(scp_pool, sp->path, dent->d_name, NULL); |
| 1976 |
pathlen = strlen(spi->path); |
| 1977 |
|
| 1978 |
|
| 1979 |
while (pathlen > 1 && |
| 1980 |
spi->path[pathlen-1] == '/') { |
| 1981 |
pr_signals_handle(); |
| 1982 |
spi->path[pathlen-1] = '\0'; |
| 1983 |
pathlen--; |
| 1984 |
} |
| 1985 |
|
| 1986 |
spi->best_path = dir_canonical_vpath(scp_pool, spi->path); |
| 1987 |
|
| 1988 |
pr_fs_clear_cache2(spi->best_path); |
| 1989 |
if (pr_fsio_lstat(spi->best_path, &link_st) == 0) { |
| 1990 |
if (S_ISLNK(link_st.st_mode)) { |
| 1991 |
char link_path[PR_TUNABLE_PATH_MAX]; |
| 1992 |
int len; |
| 1993 |
|
| 1994 |
memset(link_path, '\0', sizeof(link_path)); |
| 1995 |
len = dir_readlink(scp_pool, spi->best_path, link_path, |
| 1996 |
sizeof(link_path)-1, PR_DIR_READLINK_FL_HANDLE_REL_PATH); |
| 1997 |
if (len > 0) { |
| 1998 |
link_path[len] = '\0'; |
| 1999 |
spi->best_path = pstrdup(scp_pool, link_path); |
| 2000 |
} |
| 2001 |
} |
| 2002 |
} |
| 2003 |
|
| 2004 |
if (pathlen > 0) { |
| 2005 |
sp->dir_spi = spi; |
| 2006 |
|
| 2007 |
res = send_path(p, channel_id, spi); |
| 2008 |
if (res == 1) { |
| 2009 |
|
| 2010 |
if (session.xfer.p != NULL) { |
| 2011 |
destroy_pool(session.xfer.p); |
| 2012 |
} |
| 2013 |
|
| 2014 |
memset(&session.xfer, 0, sizeof(session.xfer)); |
| 2015 |
} |
| 2016 |
|
| 2017 |
return res; |
| 2018 |
} |
| 2019 |
} |
| 2020 |
|
| 2021 |
if (sp->dirh != NULL) { |
| 2022 |
pr_fsio_closedir(sp->dirh); |
| 2023 |
sp->dirh = NULL; |
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
need_confirm = TRUE; |
| 2028 |
res = sftp_channel_write_data(p, channel_id, (unsigned char *) "E\n", 2); |
| 2029 |
if (res < 0) { |
| 2030 |
return res; |
| 2031 |
} |
| 2032 |
} |
| 2033 |
|
| 2034 |
return 1; |
| 2035 |
} |
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
static int send_path(pool *p, uint32_t channel_id, struct scp_path *sp) { |
| 2042 |
int res, is_file = FALSE; |
| 2043 |
struct stat st; |
| 2044 |
cmd_rec *cmd = NULL; |
| 2045 |
|
| 2046 |
if (sp->sent_data == TRUE) { |
| 2047 |
|
| 2048 |
return 1; |
| 2049 |
} |
| 2050 |
|
| 2051 |
pr_scoreboard_entry_update(session.pid, |
| 2052 |
PR_SCORE_CMD, "%s", "scp download", NULL, NULL); |
| 2053 |
pr_scoreboard_entry_update(session.pid, |
| 2054 |
PR_SCORE_CMD_ARG, "%s", sp->path, NULL, NULL); |
| 2055 |
|
| 2056 |
cmd = scp_cmd_alloc(p, C_RETR, sp->path); |
| 2057 |
session.curr_cmd_rec = cmd; |
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
|
| 2062 |
if (sp->fh == NULL) { |
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) { |
| 2068 |
int xerrno = errno; |
| 2069 |
|
| 2070 |
if (xerrno != EISDIR) { |
| 2071 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2072 |
"scp download of '%s' blocked by '%s' handler", sp->path, |
| 2073 |
(char *) cmd->argv[0]); |
| 2074 |
|
| 2075 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 2076 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 2077 |
|
| 2078 |
destroy_pool(cmd->pool); |
| 2079 |
session.curr_cmd_rec = NULL; |
| 2080 |
|
| 2081 |
write_confirm(p, channel_id, 1, |
| 2082 |
pstrcat(p, sp->path, ": ", strerror(xerrno), NULL)); |
| 2083 |
sp->wrote_errors = TRUE; |
| 2084 |
return 1; |
| 2085 |
} |
| 2086 |
} |
| 2087 |
|
| 2088 |
if (strcmp(sp->path, cmd->arg) != 0) { |
| 2089 |
sp->path = pstrdup(scp_session->pool, cmd->arg); |
| 2090 |
} |
| 2091 |
} |
| 2092 |
|
| 2093 |
if (pr_table_add(cmd->notes, "mod_xfer.retr-path", |
| 2094 |
pstrdup(cmd->pool, sp->path), 0) < 0) { |
| 2095 |
if (errno != EEXIST) { |
| 2096 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2097 |
"error adding 'mod_xfer.retr-path' for SCP download: %s", |
| 2098 |
strerror(errno)); |
| 2099 |
} |
| 2100 |
} |
| 2101 |
|
| 2102 |
pr_fs_clear_cache2(sp->path); |
| 2103 |
if (pr_fsio_lstat(sp->path, &st) == 0) { |
| 2104 |
if (S_ISLNK(st.st_mode)) { |
| 2105 |
char link_path[PR_TUNABLE_PATH_MAX]; |
| 2106 |
int len; |
| 2107 |
|
| 2108 |
memset(link_path, '\0', sizeof(link_path)); |
| 2109 |
len = dir_readlink(scp_pool, sp->path, link_path, sizeof(link_path)-1, |
| 2110 |
PR_DIR_READLINK_FL_HANDLE_REL_PATH); |
| 2111 |
if (len > 0) { |
| 2112 |
link_path[len] = '\0'; |
| 2113 |
sp->path = pstrdup(scp_pool, link_path); |
| 2114 |
} |
| 2115 |
} |
| 2116 |
} |
| 2117 |
|
| 2118 |
if (pr_fsio_stat(sp->path, &st) < 0) { |
| 2119 |
int xerrno = errno; |
| 2120 |
|
| 2121 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2122 |
"error stat'ing '%s': %s", sp->path, strerror(xerrno)); |
| 2123 |
|
| 2124 |
if (sp->fh != NULL) { |
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
session.curr_cmd = C_RETR; |
| 2129 |
|
| 2130 |
pr_fsio_close(sp->fh); |
| 2131 |
sp->fh = NULL; |
| 2132 |
|
| 2133 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 2134 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 2135 |
} |
| 2136 |
|
| 2137 |
destroy_pool(cmd->pool); |
| 2138 |
session.curr_cmd_rec = NULL; |
| 2139 |
|
| 2140 |
write_confirm(p, channel_id, 1, |
| 2141 |
pstrcat(p, sp->path, ": ", strerror(xerrno), NULL)); |
| 2142 |
sp->wrote_errors = TRUE; |
| 2143 |
return 1; |
| 2144 |
} |
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
if (S_ISREG(st.st_mode)) { |
| 2151 |
is_file = TRUE; |
| 2152 |
|
| 2153 |
} else { |
| 2154 |
#ifdef S_ISFIFO |
| 2155 |
if (S_ISFIFO(st.st_mode)) { |
| 2156 |
is_file = TRUE; |
| 2157 |
|
| 2158 |
if (sftp_opts & SFTP_OPT_IGNORE_FIFOS) { |
| 2159 |
is_file = FALSE; |
| 2160 |
} |
| 2161 |
} |
| 2162 |
#endif |
| 2163 |
} |
| 2164 |
|
| 2165 |
if (is_file == FALSE) { |
| 2166 |
if (S_ISDIR(st.st_mode)) { |
| 2167 |
if (scp_opts & SFTP_SCP_OPT_RECURSE) { |
| 2168 |
res = send_dir(p, channel_id, sp, &st); |
| 2169 |
destroy_pool(cmd->pool); |
| 2170 |
session.curr_cmd_rec = NULL; |
| 2171 |
return res; |
| 2172 |
} |
| 2173 |
|
| 2174 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2175 |
"cannot send directory '%s' (no -r option)", sp->path); |
| 2176 |
|
| 2177 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 2178 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 2179 |
|
| 2180 |
destroy_pool(cmd->pool); |
| 2181 |
session.curr_cmd_rec = NULL; |
| 2182 |
|
| 2183 |
write_confirm(p, channel_id, 1, |
| 2184 |
pstrcat(p, sp->path, ": ", strerror(EPERM), NULL)); |
| 2185 |
sp->wrote_errors = TRUE; |
| 2186 |
return 1; |
| 2187 |
} |
| 2188 |
|
| 2189 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2190 |
"cannot send '%s': Not a regular file", sp->path); |
| 2191 |
|
| 2192 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 2193 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 2194 |
|
| 2195 |
destroy_pool(cmd->pool); |
| 2196 |
session.curr_cmd_rec = NULL; |
| 2197 |
|
| 2198 |
write_confirm(p, channel_id, 1, |
| 2199 |
pstrcat(p, sp->path, ": ", strerror(EPERM), NULL)); |
| 2200 |
sp->wrote_errors = TRUE; |
| 2201 |
return 1; |
| 2202 |
} |
| 2203 |
|
| 2204 |
if (sp->fh == NULL) { |
| 2205 |
sp->best_path = dir_canonical_vpath(scp_pool, sp->path); |
| 2206 |
|
| 2207 |
if (!dir_check(p, cmd, G_READ, sp->best_path, NULL)) { |
| 2208 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2209 |
"scp download of '%s' blocked by <Limit> configuration", sp->best_path); |
| 2210 |
|
| 2211 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 2212 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 2213 |
|
| 2214 |
destroy_pool(cmd->pool); |
| 2215 |
session.curr_cmd_rec = NULL; |
| 2216 |
|
| 2217 |
write_confirm(p, channel_id, 1, |
| 2218 |
pstrcat(p, sp->path, ": ", strerror(EACCES), NULL)); |
| 2219 |
sp->wrote_errors = TRUE; |
| 2220 |
return 1; |
| 2221 |
} |
| 2222 |
|
| 2223 |
sp->fh = pr_fsio_open(sp->best_path, O_RDONLY|O_NONBLOCK); |
| 2224 |
if (sp->fh == NULL) { |
| 2225 |
int xerrno = errno; |
| 2226 |
|
| 2227 |
(void) pr_trace_msg("fileperms", 1, "%s, user '%s' (UID %s, GID %s): " |
| 2228 |
"error opening '%s': %s", "scp download", session.user, |
| 2229 |
pr_uid2str(cmd->tmp_pool, session.uid), pr_gid2str(NULL, session.gid), |
| 2230 |
sp->best_path, strerror(xerrno)); |
| 2231 |
|
| 2232 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2233 |
"error reading '%s': %s", sp->best_path, strerror(xerrno)); |
| 2234 |
|
| 2235 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 2236 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 2237 |
|
| 2238 |
destroy_pool(cmd->pool); |
| 2239 |
session.curr_cmd_rec = NULL; |
| 2240 |
|
| 2241 |
write_confirm(p, channel_id, 1, |
| 2242 |
pstrcat(p, sp->path, ": ", strerror(xerrno), NULL)); |
| 2243 |
sp->wrote_errors = TRUE; |
| 2244 |
|
| 2245 |
errno = xerrno; |
| 2246 |
return 1; |
| 2247 |
|
| 2248 |
} else { |
| 2249 |
off_t curr_offset; |
| 2250 |
|
| 2251 |
|
| 2252 |
curr_offset = pr_fsio_lseek(sp->fh, (off_t) 0, SEEK_CUR); |
| 2253 |
if (curr_offset != (off_t) -1) { |
| 2254 |
off_t *file_offset; |
| 2255 |
|
| 2256 |
file_offset = palloc(cmd->pool, sizeof(off_t)); |
| 2257 |
*file_offset = (off_t) curr_offset; |
| 2258 |
(void) pr_table_add(cmd->notes, "mod_xfer.file-offset", file_offset, |
| 2259 |
sizeof(off_t)); |
| 2260 |
} |
| 2261 |
} |
| 2262 |
} |
| 2263 |
|
| 2264 |
if (pr_fsio_set_block(sp->fh) < 0) { |
| 2265 |
pr_trace_msg(trace_channel, 3, |
| 2266 |
"error setting fd %d (file '%s') as blocking: %s", sp->fh->fh_fd, |
| 2267 |
sp->fh->fh_path, strerror(errno)); |
| 2268 |
} |
| 2269 |
|
| 2270 |
if (session.xfer.p == NULL) { |
| 2271 |
session.xfer.p = pr_pool_create_sz(scp_pool, 64); |
| 2272 |
session.xfer.path = pstrdup(session.xfer.p, sp->best_path); |
| 2273 |
memset(&session.xfer.start_time, 0, sizeof(session.xfer.start_time)); |
| 2274 |
gettimeofday(&session.xfer.start_time, NULL); |
| 2275 |
session.xfer.direction = PR_NETIO_IO_WR; |
| 2276 |
} |
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
if ((scp_opts & SFTP_SCP_OPT_PRESERVE) && |
| 2282 |
sp->sent_timeinfo == FALSE) { |
| 2283 |
res = send_timeinfo(p, channel_id, sp, &st); |
| 2284 |
if (res == 1) { |
| 2285 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 2286 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 2287 |
} |
| 2288 |
|
| 2289 |
destroy_pool(cmd->pool); |
| 2290 |
session.curr_cmd_rec = NULL; |
| 2291 |
return res; |
| 2292 |
} |
| 2293 |
|
| 2294 |
if (sp->sent_finfo == FALSE) { |
| 2295 |
res = send_finfo(p, channel_id, sp, &st); |
| 2296 |
if (res == 1) { |
| 2297 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 2298 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 2299 |
} |
| 2300 |
|
| 2301 |
destroy_pool(cmd->pool); |
| 2302 |
session.curr_cmd_rec = NULL; |
| 2303 |
return res; |
| 2304 |
} |
| 2305 |
|
| 2306 |
if (sp->sent_data == FALSE) { |
| 2307 |
pr_throttle_init(cmd); |
| 2308 |
|
| 2309 |
res = send_data(p, channel_id, sp, &st); |
| 2310 |
if (res == 1) { |
| 2311 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0); |
| 2312 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0); |
| 2313 |
|
| 2314 |
destroy_pool(cmd->pool); |
| 2315 |
session.curr_cmd_rec = NULL; |
| 2316 |
|
| 2317 |
return res; |
| 2318 |
} |
| 2319 |
} |
| 2320 |
|
| 2321 |
pr_fsio_close(sp->fh); |
| 2322 |
sp->fh = NULL; |
| 2323 |
|
| 2324 |
session.xfer.path = sftp_misc_vroot_abs_path(session.xfer.p, |
| 2325 |
session.xfer.path, FALSE); |
| 2326 |
(void) pr_cmd_dispatch_phase(cmd, POST_CMD, 0); |
| 2327 |
(void) pr_cmd_dispatch_phase(cmd, LOG_CMD, 0); |
| 2328 |
|
| 2329 |
destroy_pool(cmd->pool); |
| 2330 |
session.curr_cmd_rec = NULL; |
| 2331 |
|
| 2332 |
return 1; |
| 2333 |
} |
| 2334 |
|
| 2335 |
|
| 2336 |
int sftp_scp_handle_packet(pool *p, void *ssh2, uint32_t channel_id, |
| 2337 |
unsigned char *data, uint32_t datalen) { |
| 2338 |
int res = -1; |
| 2339 |
struct ssh2_packet *pkt; |
| 2340 |
|
| 2341 |
scp_session = scp_get_session(channel_id); |
| 2342 |
if (scp_session == NULL) { |
| 2343 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2344 |
"no existing SCP session for channel ID %lu, rejecting request", |
| 2345 |
(unsigned long) channel_id); |
| 2346 |
return -1; |
| 2347 |
} |
| 2348 |
|
| 2349 |
pkt = ssh2; |
| 2350 |
|
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
session.curr_phase = PRE_CMD; |
| 2358 |
|
| 2359 |
if (pr_data_get_timeout(PR_DATA_TIMEOUT_NO_TRANSFER) > 0) { |
| 2360 |
pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE); |
| 2361 |
} |
| 2362 |
|
| 2363 |
if (pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED) > 0) { |
| 2364 |
pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE); |
| 2365 |
} |
| 2366 |
|
| 2367 |
pr_response_set_pool(pkt->pool); |
| 2368 |
|
| 2369 |
if (need_confirm == TRUE) { |
| 2370 |
|
| 2371 |
if (read_confirm(pkt, &data, &datalen) < 0) { |
| 2372 |
return 1; |
| 2373 |
} |
| 2374 |
} |
| 2375 |
|
| 2376 |
if (scp_opts & SFTP_SCP_OPT_ISSRC) { |
| 2377 |
struct scp_path **paths; |
| 2378 |
|
| 2379 |
pr_proctitle_set("%s - %s: scp download", session.user, |
| 2380 |
session.proc_prefix); |
| 2381 |
|
| 2382 |
if (scp_session->path_idx == scp_session->paths->nelts) { |
| 2383 |
|
| 2384 |
|
| 2385 |
|
| 2386 |
return 1; |
| 2387 |
} |
| 2388 |
|
| 2389 |
paths = scp_session->paths->elts; |
| 2390 |
|
| 2391 |
if (scp_session->path_idx < scp_session->paths->nelts) { |
| 2392 |
pr_signals_handle(); |
| 2393 |
|
| 2394 |
res = send_path(pkt->pool, channel_id, paths[scp_session->path_idx]); |
| 2395 |
if (res < 0) { |
| 2396 |
return -1; |
| 2397 |
} |
| 2398 |
|
| 2399 |
if (res == 1) { |
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
scp_session->path_idx++; |
| 2404 |
|
| 2405 |
|
| 2406 |
if (session.xfer.p) { |
| 2407 |
destroy_pool(session.xfer.p); |
| 2408 |
} |
| 2409 |
memset(&session.xfer, 0, sizeof(session.xfer)); |
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
|
| 2414 |
pr_response_clear(&resp_list); |
| 2415 |
pr_response_clear(&resp_err_list); |
| 2416 |
} |
| 2417 |
} |
| 2418 |
|
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
|
| 2429 |
|
| 2430 |
if (res == 1) { |
| 2431 |
if (paths[scp_session->path_idx-1]->wrote_errors == TRUE) { |
| 2432 |
return 1; |
| 2433 |
} |
| 2434 |
} |
| 2435 |
|
| 2436 |
return 0; |
| 2437 |
|
| 2438 |
} else if (scp_opts & SFTP_SCP_OPT_ISDST) { |
| 2439 |
struct scp_path **paths; |
| 2440 |
|
| 2441 |
pr_proctitle_set("%s - %s: scp upload", session.user, |
| 2442 |
session.proc_prefix); |
| 2443 |
|
| 2444 |
paths = scp_session->paths->elts; |
| 2445 |
|
| 2446 |
if (session.xfer.p == NULL) { |
| 2447 |
session.xfer.p = pr_pool_create_sz(scp_pool, 64); |
| 2448 |
session.xfer.path = pstrdup(session.xfer.p, |
| 2449 |
paths[scp_session->path_idx]->path); |
| 2450 |
memset(&session.xfer.start_time, 0, sizeof(session.xfer.start_time)); |
| 2451 |
gettimeofday(&session.xfer.start_time, NULL); |
| 2452 |
session.xfer.direction = PR_NETIO_IO_RD; |
| 2453 |
} |
| 2454 |
|
| 2455 |
res = recv_path(pkt->pool, channel_id, paths[scp_session->path_idx], data, |
| 2456 |
datalen); |
| 2457 |
if (res < 0) { |
| 2458 |
return -1; |
| 2459 |
} |
| 2460 |
|
| 2461 |
if (res == 1) { |
| 2462 |
|
| 2463 |
if (session.xfer.p) { |
| 2464 |
destroy_pool(session.xfer.p); |
| 2465 |
} |
| 2466 |
memset(&session.xfer, 0, sizeof(session.xfer)); |
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 2471 |
pr_response_clear(&resp_list); |
| 2472 |
pr_response_clear(&resp_err_list); |
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
|
| 2477 |
|
| 2478 |
|
| 2479 |
|
| 2480 |
reset_path(paths[scp_session->path_idx]); |
| 2481 |
} |
| 2482 |
} |
| 2483 |
|
| 2484 |
return 0; |
| 2485 |
} |
| 2486 |
|
| 2487 |
static char *scp_canonicalize_target(pool *p, char *target) { |
| 2488 |
size_t target_len; |
| 2489 |
|
| 2490 |
target_len = strlen(target); |
| 2491 |
|
| 2492 |
|
| 2493 |
|
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
if ((target[0] == '\'' && |
| 2499 |
target[target_len-1] == '\'') || |
| 2500 |
(target[0] == '"' && |
| 2501 |
target[target_len-1] == '"')) { |
| 2502 |
target[target_len-1] = '\0'; |
| 2503 |
return pstrdup(p, target + 1); |
| 2504 |
} |
| 2505 |
|
| 2506 |
return target; |
| 2507 |
} |
| 2508 |
|
| 2509 |
int sftp_scp_set_params(pool *p, uint32_t channel_id, array_header *req) { |
| 2510 |
register unsigned int i; |
| 2511 |
int optc, use_glob = TRUE; |
| 2512 |
char **reqargv, *target_path = NULL; |
| 2513 |
const char *opts = "dfprtv"; |
| 2514 |
config_rec *c; |
| 2515 |
struct scp_paths *paths; |
| 2516 |
struct scp_path *sp; |
| 2517 |
|
| 2518 |
if (!(sftp_services & SFTP_SERVICE_FL_SCP)) { |
| 2519 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2520 |
"%s", "'scp' exec request denied by Protocols config"); |
| 2521 |
errno = EPERM; |
| 2522 |
return -1; |
| 2523 |
} |
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
|
| 2534 |
|
| 2535 |
pr_getopt_reset(); |
| 2536 |
|
| 2537 |
reqargv = (char **) req->elts; |
| 2538 |
for (i = 0; i < req->nelts; i++) { |
| 2539 |
if (reqargv[i] != NULL) { |
| 2540 |
pr_trace_msg(trace_channel, 5, "reqargv[%u] = '%s'", i, reqargv[i]); |
| 2541 |
} |
| 2542 |
} |
| 2543 |
|
| 2544 |
c = find_config(main_server->conf, CONF_PARAM, "UseGlobbing", FALSE); |
| 2545 |
if (c != NULL) { |
| 2546 |
use_glob = *((unsigned char *) c->argv[0]); |
| 2547 |
} |
| 2548 |
|
| 2549 |
need_confirm = FALSE; |
| 2550 |
scp_pool = make_sub_pool(sftp_pool); |
| 2551 |
pr_pool_tag(scp_pool, "SSH2 SCP Pool"); |
| 2552 |
|
| 2553 |
while ((optc = getopt(req->nelts-1, reqargv, opts)) != -1) { |
| 2554 |
switch (optc) { |
| 2555 |
case 'd': |
| 2556 |
scp_opts |= SFTP_SCP_OPT_DIR; |
| 2557 |
break; |
| 2558 |
|
| 2559 |
case 'f': |
| 2560 |
scp_opts |= SFTP_SCP_OPT_ISSRC; |
| 2561 |
need_confirm = TRUE; |
| 2562 |
break; |
| 2563 |
|
| 2564 |
case 'p': |
| 2565 |
scp_opts |= SFTP_SCP_OPT_PRESERVE; |
| 2566 |
break; |
| 2567 |
|
| 2568 |
case 'r': |
| 2569 |
scp_opts |= SFTP_SCP_OPT_RECURSE; |
| 2570 |
break; |
| 2571 |
|
| 2572 |
case 't': |
| 2573 |
scp_opts |= SFTP_SCP_OPT_ISDST; |
| 2574 |
write_confirm(p, channel_id, 0, NULL); |
| 2575 |
break; |
| 2576 |
|
| 2577 |
case 'v': |
| 2578 |
scp_opts |= SFTP_SCP_OPT_VERBOSE; |
| 2579 |
break; |
| 2580 |
} |
| 2581 |
} |
| 2582 |
|
| 2583 |
|
| 2584 |
if (reqargv[optind] == NULL) { |
| 2585 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2586 |
"'scp' request provided no paths, ignoring"); |
| 2587 |
errno = EINVAL; |
| 2588 |
return -1; |
| 2589 |
} |
| 2590 |
|
| 2591 |
paths = scp_new_paths(channel_id); |
| 2592 |
if (paths == NULL) { |
| 2593 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2594 |
"unable to handle paths for 'scp' request: %s", strerror(errno)); |
| 2595 |
return -1; |
| 2596 |
} |
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
paths->paths = make_array(paths->pool, 1, sizeof(struct scp_path *)); |
| 2604 |
paths->path_idx = 0; |
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
for (i = optind; i < req->nelts; i++) { |
| 2610 |
pr_signals_handle(); |
| 2611 |
|
| 2612 |
if (reqargv[i] != NULL) { |
| 2613 |
size_t reqarg_len; |
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
|
| 2618 |
|
| 2619 |
|
| 2620 |
reqarg_len = strlen(reqargv[i]); |
| 2621 |
if (reqarg_len == 0) { |
| 2622 |
continue; |
| 2623 |
} |
| 2624 |
|
| 2625 |
if (target_path != NULL) { |
| 2626 |
target_path = pstrcat(p, target_path, " ", reqargv[i], NULL); |
| 2627 |
|
| 2628 |
} else { |
| 2629 |
target_path = pstrdup(p, reqargv[i]); |
| 2630 |
} |
| 2631 |
} |
| 2632 |
} |
| 2633 |
|
| 2634 |
target_path = scp_canonicalize_target(p, target_path); |
| 2635 |
|
| 2636 |
if (use_glob == TRUE && |
| 2637 |
(scp_opts & SFTP_SCP_OPT_ISSRC) && |
| 2638 |
strpbrk(target_path, "{[*?") != NULL) { |
| 2639 |
int res, xerrno; |
| 2640 |
char *glob_path; |
| 2641 |
size_t path_len; |
| 2642 |
glob_t gl; |
| 2643 |
|
| 2644 |
|
| 2645 |
|
| 2646 |
|
| 2647 |
|
| 2648 |
memset(&gl, 0, sizeof(gl)); |
| 2649 |
|
| 2650 |
glob_path = pstrdup(paths->pool, target_path); |
| 2651 |
path_len = strlen(glob_path); |
| 2652 |
|
| 2653 |
res = pr_fs_glob(glob_path, GLOB_NOSORT|GLOB_BRACE, NULL, &gl); |
| 2654 |
switch (res) { |
| 2655 |
case 0: { |
| 2656 |
register unsigned int j; |
| 2657 |
|
| 2658 |
for (j = 0; j < gl.gl_pathc; j++) { |
| 2659 |
pr_signals_handle(); |
| 2660 |
|
| 2661 |
sp = pcalloc(paths->pool, sizeof(struct scp_path)); |
| 2662 |
sp->path = pstrdup(paths->pool, gl.gl_pathv[j]); |
| 2663 |
path_len = strlen(sp->path); |
| 2664 |
|
| 2665 |
|
| 2666 |
while (path_len > 1 && |
| 2667 |
sp->path[path_len-1] == '/') { |
| 2668 |
pr_signals_handle(); |
| 2669 |
sp->path[--path_len] = '\0'; |
| 2670 |
} |
| 2671 |
|
| 2672 |
sp->orig_path = pstrdup(paths->pool, sp->path); |
| 2673 |
|
| 2674 |
if (path_len > 0) { |
| 2675 |
*((struct scp_path **) push_array(paths->paths)) = sp; |
| 2676 |
} |
| 2677 |
} |
| 2678 |
|
| 2679 |
break; |
| 2680 |
} |
| 2681 |
|
| 2682 |
case GLOB_NOSPACE: |
| 2683 |
xerrno = errno; |
| 2684 |
pr_trace_msg(trace_channel, 1, "error globbing '%s': Not " |
| 2685 |
"enough memory (%s)", reqargv[i], strerror(xerrno)); |
| 2686 |
write_confirm(p, channel_id, 1, pstrcat(p, target_path, ": ", |
| 2687 |
strerror(xerrno), NULL)); |
| 2688 |
errno = xerrno; |
| 2689 |
return 0; |
| 2690 |
|
| 2691 |
case GLOB_NOMATCH: |
| 2692 |
xerrno = ENOENT; |
| 2693 |
pr_trace_msg(trace_channel, 1, "error globbing '%s': No " |
| 2694 |
"matches found (%s)", reqargv[i], strerror(xerrno)); |
| 2695 |
write_confirm(p, channel_id, 1, pstrcat(p, target_path, ": ", |
| 2696 |
strerror(xerrno), NULL)); |
| 2697 |
errno = xerrno; |
| 2698 |
return 0; |
| 2699 |
} |
| 2700 |
|
| 2701 |
pr_fs_globfree(&gl); |
| 2702 |
|
| 2703 |
} else { |
| 2704 |
size_t path_len; |
| 2705 |
|
| 2706 |
sp = pcalloc(paths->pool, sizeof(struct scp_path)); |
| 2707 |
sp->path = pstrdup(paths->pool, target_path); |
| 2708 |
|
| 2709 |
path_len = strlen(sp->path); |
| 2710 |
|
| 2711 |
|
| 2712 |
while (path_len > 1 && |
| 2713 |
sp->path[path_len-1] == '/') { |
| 2714 |
pr_signals_handle(); |
| 2715 |
sp->path[--path_len] = '\0'; |
| 2716 |
} |
| 2717 |
|
| 2718 |
sp->orig_path = pstrdup(paths->pool, sp->path); |
| 2719 |
|
| 2720 |
if (path_len > 0) { |
| 2721 |
*((struct scp_path **) push_array(paths->paths)) = sp; |
| 2722 |
} |
| 2723 |
} |
| 2724 |
|
| 2725 |
|
| 2726 |
|
| 2727 |
|
| 2728 |
|
| 2729 |
if ((scp_opts & SFTP_SCP_OPT_ISDST) && |
| 2730 |
paths->paths->nelts != 1) { |
| 2731 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2732 |
"'scp' request provided more than one destination path, ignoring"); |
| 2733 |
errno = EINVAL; |
| 2734 |
return -1; |
| 2735 |
} |
| 2736 |
|
| 2737 |
for (i = 0; i < paths->paths->nelts; i++) { |
| 2738 |
struct scp_path *spi; |
| 2739 |
|
| 2740 |
spi = ((struct scp_path **) paths->paths->elts)[i]; |
| 2741 |
if (spi != NULL) { |
| 2742 |
pr_trace_msg(trace_channel, 5, "scp path[%u] = '%s'", i, spi->path); |
| 2743 |
} |
| 2744 |
} |
| 2745 |
|
| 2746 |
return 0; |
| 2747 |
} |
| 2748 |
|
| 2749 |
int sftp_scp_open_session(uint32_t channel_id) { |
| 2750 |
register unsigned int i; |
| 2751 |
pool *sub_pool; |
| 2752 |
struct scp_paths *paths; |
| 2753 |
struct scp_session *sess, *last; |
| 2754 |
int timeout_stalled; |
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
sess = last = scp_sessions; |
| 2760 |
while (sess != NULL) { |
| 2761 |
pr_signals_handle(); |
| 2762 |
|
| 2763 |
if (sess->channel_id == channel_id) { |
| 2764 |
errno = EEXIST; |
| 2765 |
return -1; |
| 2766 |
} |
| 2767 |
|
| 2768 |
if (sess->next == NULL) { |
| 2769 |
|
| 2770 |
last = sess; |
| 2771 |
} |
| 2772 |
|
| 2773 |
sess = sess->next; |
| 2774 |
} |
| 2775 |
|
| 2776 |
paths = scp_get_paths(channel_id); |
| 2777 |
if (paths == NULL) { |
| 2778 |
pr_trace_msg(trace_channel, 1, "missing paths for SCP channel ID %lu", |
| 2779 |
(unsigned long) channel_id); |
| 2780 |
errno = EACCES; |
| 2781 |
return -1; |
| 2782 |
} |
| 2783 |
|
| 2784 |
|
| 2785 |
sub_pool = make_sub_pool(scp_pool); |
| 2786 |
pr_pool_tag(sub_pool, "SCP session pool"); |
| 2787 |
|
| 2788 |
sess = pcalloc(sub_pool, sizeof(struct scp_session)); |
| 2789 |
sess->pool = sub_pool; |
| 2790 |
sess->channel_id = channel_id; |
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
sess->paths = make_array(sess->pool, paths->paths->nelts, |
| 2797 |
sizeof(struct scp_path *)); |
| 2798 |
|
| 2799 |
for (i = 0; i < paths->paths->nelts; i++) { |
| 2800 |
struct scp_path *src_sp, *dst_sp; |
| 2801 |
|
| 2802 |
src_sp = ((struct scp_path **) paths->paths->elts)[i]; |
| 2803 |
|
| 2804 |
dst_sp = pcalloc(sess->pool, sizeof(struct scp_path)); |
| 2805 |
dst_sp->orig_path = pstrdup(sess->pool, src_sp->orig_path); |
| 2806 |
dst_sp->path = pstrdup(sess->pool, src_sp->path); |
| 2807 |
|
| 2808 |
*((struct scp_path **) push_array(sess->paths)) = dst_sp; |
| 2809 |
} |
| 2810 |
|
| 2811 |
sess->path_idx = paths->path_idx; |
| 2812 |
|
| 2813 |
scp_destroy_paths(paths); |
| 2814 |
|
| 2815 |
if (last != NULL) { |
| 2816 |
last->next = sess; |
| 2817 |
sess->prev = last; |
| 2818 |
|
| 2819 |
} else { |
| 2820 |
scp_sessions = sess; |
| 2821 |
} |
| 2822 |
|
| 2823 |
pr_event_generate("mod_sftp.scp.session-opened", NULL); |
| 2824 |
|
| 2825 |
pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE); |
| 2826 |
|
| 2827 |
timeout_stalled = pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED); |
| 2828 |
if (timeout_stalled > 0) { |
| 2829 |
pr_timer_add(timeout_stalled, PR_TIMER_STALLED, NULL, |
| 2830 |
scp_timeout_stalled_cb, "TimeoutStalled"); |
| 2831 |
} |
| 2832 |
|
| 2833 |
pr_session_set_protocol("scp"); |
| 2834 |
|
| 2835 |
|
| 2836 |
session.sf_flags &= ~SF_ASCII; |
| 2837 |
|
| 2838 |
return 0; |
| 2839 |
} |
| 2840 |
|
| 2841 |
int sftp_scp_close_session(uint32_t channel_id) { |
| 2842 |
struct scp_session *sess; |
| 2843 |
|
| 2844 |
|
| 2845 |
sess = scp_sessions; |
| 2846 |
while (sess != NULL) { |
| 2847 |
pr_signals_handle(); |
| 2848 |
|
| 2849 |
if (sess->channel_id == channel_id) { |
| 2850 |
pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE); |
| 2851 |
|
| 2852 |
if (sess->next != NULL) { |
| 2853 |
sess->next->prev = sess->prev; |
| 2854 |
} |
| 2855 |
|
| 2856 |
if (sess->prev != NULL) { |
| 2857 |
sess->prev->next = sess->next; |
| 2858 |
|
| 2859 |
} else { |
| 2860 |
|
| 2861 |
scp_sessions = sess->next; |
| 2862 |
} |
| 2863 |
|
| 2864 |
|
| 2865 |
|
| 2866 |
if (sess->paths != NULL) { |
| 2867 |
if (sess->paths != NULL && |
| 2868 |
sess->paths->nelts > 0) { |
| 2869 |
register unsigned int i; |
| 2870 |
int count = 0; |
| 2871 |
struct scp_path **elts; |
| 2872 |
|
| 2873 |
elts = sess->paths->elts; |
| 2874 |
for (i = 0; i < sess->paths->nelts; i++) { |
| 2875 |
struct scp_path *elt = elts[i]; |
| 2876 |
|
| 2877 |
if (elt->fh != NULL) { |
| 2878 |
count++; |
| 2879 |
} |
| 2880 |
} |
| 2881 |
|
| 2882 |
if (count > 0) { |
| 2883 |
config_rec *c; |
| 2884 |
unsigned char delete_aborted_stores = FALSE; |
| 2885 |
|
| 2886 |
c = find_config(main_server->conf, CONF_PARAM, |
| 2887 |
"DeleteAbortedStores", FALSE); |
| 2888 |
if (c) { |
| 2889 |
delete_aborted_stores = *((unsigned char *) c->argv[0]); |
| 2890 |
} |
| 2891 |
|
| 2892 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2893 |
"aborting %d unclosed file %s", count, |
| 2894 |
count != 1 ? "handles" : "handle"); |
| 2895 |
|
| 2896 |
for (i = 0; i < sess->paths->nelts; i++) { |
| 2897 |
struct scp_path *elt = elts[i]; |
| 2898 |
|
| 2899 |
if (elt->fh != NULL) { |
| 2900 |
char *abs_path, *curr_path; |
| 2901 |
|
| 2902 |
curr_path = pstrdup(scp_pool, elt->fh->fh_path); |
| 2903 |
|
| 2904 |
|
| 2905 |
abs_path = sftp_misc_vroot_abs_path(scp_pool, elt->best_path, |
| 2906 |
TRUE); |
| 2907 |
|
| 2908 |
if (scp_opts & SFTP_SCP_OPT_ISDST) { |
| 2909 |
xferlog_write(0, pr_netaddr_get_sess_remote_name(), |
| 2910 |
elt->recvlen, abs_path, 'b', 'i', 'r', session.user, 'i', |
| 2911 |
"_"); |
| 2912 |
|
| 2913 |
} else { |
| 2914 |
xferlog_write(0, pr_netaddr_get_sess_remote_name(), |
| 2915 |
elt->sentlen, abs_path, 'b', 'o', 'r', session.user, 'i', |
| 2916 |
"_"); |
| 2917 |
} |
| 2918 |
|
| 2919 |
if (pr_fsio_close(elt->fh) < 0) { |
| 2920 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2921 |
"error writing aborted file '%s': %s", elt->best_path, |
| 2922 |
strerror(errno)); |
| 2923 |
} |
| 2924 |
|
| 2925 |
elt->fh = NULL; |
| 2926 |
|
| 2927 |
if (delete_aborted_stores == TRUE && |
| 2928 |
scp_opts & SFTP_SCP_OPT_ISDST) { |
| 2929 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2930 |
"removing aborted uploaded file '%s'", curr_path); |
| 2931 |
|
| 2932 |
if (pr_fsio_unlink(curr_path) < 0) { |
| 2933 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 2934 |
"error unlinking file '%s': %s", curr_path, |
| 2935 |
strerror(errno)); |
| 2936 |
} |
| 2937 |
} |
| 2938 |
} |
| 2939 |
} |
| 2940 |
} |
| 2941 |
} |
| 2942 |
} |
| 2943 |
|
| 2944 |
sess->paths = NULL; |
| 2945 |
destroy_pool(sess->pool); |
| 2946 |
|
| 2947 |
pr_session_set_protocol("ssh2"); |
| 2948 |
|
| 2949 |
pr_event_generate("mod_sftp.scp.session-closed", NULL); |
| 2950 |
return 0; |
| 2951 |
} |
| 2952 |
|
| 2953 |
sess = sess->next; |
| 2954 |
} |
| 2955 |
|
| 2956 |
errno = ENOENT; |
| 2957 |
return -1; |
| 2958 |
} |