Debian Codegraph @

contrib/mod_sftp/fxp.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp sftp
3 * Copyright (c) 2008-2024 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 #include "mod_sftp.h"
26 #include "ssh2.h"
27 #include "msg.h"
28 #include "crypto.h"
29 #include "packet.h"
30 #include "disconnect.h"
31 #include "interop.h"
32 #include "channel.h"
33 #include "auth.h"
34 #include "display.h"
35 #include "fxp.h"
36 #include "utf8.h"
37 #include "misc.h"
38
39 /* FXP_NAME file attribute flags */
40 #define SSH2_FX_ATTR_SIZE 0x00000001
41 #define SSH2_FX_ATTR_UIDGID 0x00000002
42 #define SSH2_FX_ATTR_PERMISSIONS 0x00000004
43 #define SSH2_FX_ATTR_ACMODTIME 0x00000008
44 #define SSH2_FX_ATTR_ACCESSTIME SSH2_FX_ATTR_ACMODTIME
45 #define SSH2_FX_ATTR_CREATETIME 0x00000010
46 #define SSH2_FX_ATTR_MODIFYTIME 0x00000020
47 #define SSH2_FX_ATTR_ACL 0x00000040
48 #define SSH2_FX_ATTR_OWNERGROUP 0x00000080
49 #define SSH2_FX_ATTR_SUBSECOND_TIMES 0x00000100
50 #define SSH2_FX_ATTR_BITS 0x00000200
51
52 /* Note that these attributes were added in draft-ietf-secsh-filexfer-06,
53 * which is SFTP protocol version 6.
54 */
55 #define SSH2_FX_ATTR_ALLOCATION_SIZE 0x00000400
56 #define SSH2_FX_ATTR_TEXT_HINT 0x00000800
57 #define SSH2_FX_ATTR_MIME_TYPE 0x00001000
58 #define SSH2_FX_ATTR_LINK_COUNT 0x00002000
59 #define SSH2_FX_ATTR_UNTRANSLATED_NAME 0x00004000
60
61 #define SSH2_FX_ATTR_CTIME 0x00008000
62
63 /* The EXTENDED attribute was defined in draft-ietf-secsh-filexfer-02,
64 * which is SFTP protocol version 3.
65 */
66 #define SSH2_FX_ATTR_EXTENDED 0x80000000
67
68 /* FX_ATTR_BITS values (see draft-ietf-secsh-filexfer-13, Section 7.9) */
69 #define SSH2_FX_ATTR_BIT_FL_READONLY 0x00000001
70 #define SSH2_FX_ATTR_BIT_FL_SYSTEM 0x00000002
71 #define SSH2_FX_ATTR_BIT_FL_HIDDEN 0x00000004
72 #define SSH2_FX_ATTR_BIT_FL_CASE_INSENSITIVE 0x00000008
73 #define SSH2_FX_ATTR_BIT_FL_ARCHIVE 0x00000010
74 #define SSH2_FX_ATTR_BIT_FL_ENCRYPTED 0x00000020
75 #define SSH2_FX_ATTR_BIT_FL_COMPRESSED 0x00000040
76 #define SSH2_FX_ATTR_BIT_FL_SPARSE 0x00000080
77 #define SSH2_FX_ATTR_BIT_FL_APPEND_ONLY 0x00000100
78 #define SSH2_FX_ATTR_BIT_FL_IMMUTABLE 0x00000200
79 #define SSH2_FX_ATTR_BIT_FL_SYNC 0x00000400
80 #define SSH2_FX_ATTR_BIT_FL_TRANSLATION_ERR 0x00000800
81
82 /* FX_ATTR_TEXT_HINT values (see draft-ietf-secsh-filexfer-13, Section 7.10) */
83 #define SSH2_FX_ATTR_KNOWN_TEXT 0x00
84 #define SSH2_FX_ATTR_GUESSED_TEXT 0x01
85 #define SSH2_FX_ATTR_KNOWN_BINARY 0x02
86 #define SSH2_FX_ATTR_GUESSED_BINARY 0x03
87
88 /* FXP_ATTRS file types */
89 #define SSH2_FX_ATTR_FTYPE_REGULAR 1
90 #define SSH2_FX_ATTR_FTYPE_DIRECTORY 2
91 #define SSH2_FX_ATTR_FTYPE_SYMLINK 3
92 #define SSH2_FX_ATTR_FTYPE_SPECIAL 4
93 #define SSH2_FX_ATTR_FTYPE_UNKNOWN 5
94 #define SSH2_FX_ATTR_FTYPE_SOCKET 6
95 #define SSH2_FX_ATTR_FTYPE_CHAR_DEVICE 7
96 #define SSH2_FX_ATTR_FTYPE_BLOCK_DEVICE 8
97 #define SSH2_FX_ATTR_FTYPE_FIFO 9
98
99 /* FXP_LOCK/FXP_UNLOCK flags */
100 #define SSH2_FXL_READ 0x00000040
101 #define SSH2_FXL_WRITE 0x00000080
102 #define SSH2_FXL_DELETE 0x00000100
103
104 /* FXP_OPEN flags (prior to version 5) */
105 #define SSH2_FXF_READ 0x00000001
106 #define SSH2_FXF_WRITE 0x00000002
107 #define SSH2_FXF_APPEND 0x00000004
108 #define SSH2_FXF_CREAT 0x00000008
109 #define SSH2_FXF_TRUNC 0x00000010
110 #define SSH2_FXF_EXCL 0x00000020
111 #define SSH2_FXF_TEXT 0x00000040
112
113 /* FXP_OPEN flags (version 5 and higher) */
114 #define SSH2_FXF_WANT_READ_DATA 0x00000001
115 #define SSH2_FXF_WANT_WRITE_DATA 0x00000002
116 #define SSH2_FXF_WANT_APPEND_DATA 0x00000004
117 #define SSH2_FXF_WANT_READ_NAMED_ATTRS 0x00000008
118 #define SSH2_FXF_WANT_WRITE_NAMED_ATTRS 0x00000010
119 #define SSH2_FXF_WANT_READ_ATTRIBUTES 0x00000080
120 #define SSH2_FXF_WANT_WRITE_ATTRIBUTES 0x00000100
121 #define SSH2_FXF_WANT_READ_ACL 0x00020000
122 #define SSH2_FXF_WANT_WRITE_ACL 0x00040000
123 #define SSH2_FXF_WANT_WRITE_OWNER 0x00080000
124
125 #define SSH2_FXF_CREATE_NEW 0x00000000
126 #define SSH2_FXF_CREATE_TRUNCATE 0x00000001
127 #define SSH2_FXF_OPEN_EXISTING 0x00000002
128 #define SSH2_FXF_OPEN_OR_CREATE 0x00000003
129 #define SSH2_FXF_TRUNCATE_EXISTING 0x00000004
130 #define SSH2_FXF_ACCESS_APPEND_DATA 0x00000008
131 #define SSH2_FXF_ACCESS_APPEND_DATA_ATOMIC 0x00000010
132 #define SSH2_FXF_ACCESS_TEXT_MODE 0x00000020
133
134 /* These are the BLOCK_{READ,WRITE,DELETE} values from Section 8.1.1.3 of
135 * the SFTP Draft.
136 */
137 #define SSH2_FXF_ACCESS_READ_LOCK 0x00000040
138 #define SSH2_FXF_ACCESS_WRITE_LOCK 0x00000080
139 #define SSH2_FXF_ACCESS_DELETE_LOCK 0x00000100
140
141 /* FXP_REALPATH control values */
142 #define SSH2_FXRP_NO_CHECK 0x00000001
143 #define SSH2_FXRP_STAT_IF 0x00000002
144 #define SSH2_FXRP_STAT_ALWAYS 0x00000003
145
146 /* FXP_RENAME flags */
147 #define SSH2_FXR_OVERWRITE 0x00000001
148 #define SSH2_FXR_ATOMIC 0x00000002
149 #define SSH2_FXR_NATIVE 0x00000004
150
151 /* FXP_STATUS codes */
152 #define SSH2_FX_OK 0
153 #define SSH2_FX_EOF 1
154 #define SSH2_FX_NO_SUCH_FILE 2
155 #define SSH2_FX_PERMISSION_DENIED 3
156 #define SSH2_FX_FAILURE 4
157 #define SSH2_FX_BAD_MESSAGE 5
158 #define SSH2_FX_NO_CONNECTION 6
159 #define SSH2_FX_CONNECTION_LOST 7
160 #define SSH2_FX_OP_UNSUPPORTED 8
161 #define SSH2_FX_INVALID_HANDLE 9
162 #define SSH2_FX_NO_SUCH_PATH 10
163 #define SSH2_FX_FILE_ALREADY_EXISTS 11
164 #define SSH2_FX_WRITE_PROTECT 12
165 #define SSH2_FX_NO_MEDIA 13
166 #define SSH2_FX_NO_SPACE_ON_FILESYSTEM 14
167 #define SSH2_FX_QUOTA_EXCEEDED 15
168 #define SSH2_FX_UNKNOWN_PRINCIPAL 16
169 #define SSH2_FX_LOCK_CONFLICT 17
170 #define SSH2_FX_DIR_NOT_EMPTY 18
171 #define SSH2_FX_NOT_A_DIRECTORY 19
172 #define SSH2_FX_INVALID_FILENAME 20
173 #define SSH2_FX_LINK_LOOP 21
174 #define SSH2_FX_CANNOT_DELETE 22
175 #define SSH2_FX_INVALID_PARAMETER 23
176 #define SSH2_FX_FILE_IS_A_DIRECTORY 24
177 #define SSH2_FX_BYTE_RANGE_LOCK_CONFLICT 25
178 #define SSH2_FX_BYTE_RANGE_LOCK_REFUSED 26
179 #define SSH2_FX_DELETE_PENDING 27
180 #define SSH2_FX_FILE_CORRUPT 28
181 #define SSH2_FX_OWNER_INVALID 29
182 #define SSH2_FX_GROUP_INVALID 30
183 #define SSH2_FX_NO_MATCHING_BYTE_RANGE_LOCK 31
184
185 /* [email protected] extension flags */
186 #define SSH2_FXE_STATVFS_ST_RDONLY 0x1
187 #define SSH2_FXE_STATVFS_ST_NOSUID 0x2
188
189 /* [email protected] extension flags */
190 #define SSH2_FXE_XATTR_CREATE 0x1
191 #define SSH2_FXE_XATTR_REPLACE 0x2
192
193 extern pr_response_t *resp_list, *resp_err_list;
194
195 struct fxp_dirent {
196 const char *client_path;
197 const char *real_path;
198 struct stat *st;
199 };
200
201 struct fxp_handle {
202 pool *pool;
203 const char *name;
204
205 pr_fh_t *fh;
206 int fh_flags;
207
208 /* For indicating whether the file existed prior to being opened/created. */
209 int fh_existed;
210
211 /* For caching the initial dir_check() results for subsequent READ/WRITE
212 * requests.
213 */
214 int fh_cached_read_dir_check, fh_have_cached_read_dir_check;
215 int fh_cached_write_dir_check, fh_have_cached_write_dir_check;
216
217 /* For referencing information about the opened file; NOTE THAT THIS MAY
218 * BE STALE.
219 */
220 struct stat *fh_st;
221
222 /* This deliberately mimics the session.xfer struct, for updating
223 * session.xfer with the details of this filehandle (vs other filehandles
224 * open for the same session/channel concurrently); see Issue #1646.
225 */
226 struct {
227 int xfer_type;
228 int direction;
229 const char *filename; /* Same as requested path */
230 const char *path; /* Same as fxh->fh->fh_path */
231 const char *path_hidden; /* Used for HiddenStores if applicable */
232 struct timeval start_time;
233 off_t file_size;
234 off_t total_bytes;
235 } xfer;
236
237 void *dirh;
238 const char *dir;
239 };
240
241 struct fxp_packet {
242 pool *pool;
243 uint32_t channel_id;
244 uint32_t packet_len;
245 unsigned char request_type;
246 uint32_t request_id;
247 uint32_t payload_sz;
248 unsigned char *payload;
249 uint32_t payload_len;
250
251 unsigned int state;
252 };
253
254 struct fxp_buffer {
255 /* Pointer to the start of the buffer */
256 unsigned char *ptr;
257
258 /* Total size of the buffer */
259 uint32_t bufsz;
260
261 /* Current pointer */
262 unsigned char *buf;
263
264 /* Length of buffer remaining */
265 uint32_t buflen;
266 };
267
268 #define FXP_PACKET_HAVE_PACKET_LEN 0x0001
269 #define FXP_PACKET_HAVE_REQUEST_TYPE 0x0002
270 #define FXP_PACKET_HAVE_REQUEST_ID 0x0004
271 #define FXP_PACKET_HAVE_PAYLOAD_SIZE 0x0008
272 #define FXP_PACKET_HAVE_PAYLOAD 0x0010
273
274 /* After 64K of allocation from the scratch SFTP payload pool, destroy the
275 * pool and create a new one. This will prevent unbounded allocation
276 * from the pool.
277 */
278 #define FXP_PACKET_DATA_ALLOC_MAX_SZ (1024 * 64)
279 static size_t fxp_packet_data_allocsz = 0;
280
281 #define FXP_PACKET_DATA_DEFAULT_SZ (1024 * 16)
282 #define FXP_RESPONSE_DATA_DEFAULT_SZ 512
283
284 #if defined(PR_USE_XATTR)
285 /* Impose limits on the xattr value length we are willing to process. */
286 # define FXP_XATTR_VALUE_MAX_LEN (1024 * 64)
287 /* Allocate larger buffers for extended attributes */
288 # define FXP_RESPONSE_NAME_DEFAULT_SZ (1024 * 4)
289 #endif /* PR_USE_XATTR */
290
291 #if !defined(FXP_RESPONSE_NAME_DEFAULT_SZ)
292 # define FXP_RESPONSE_NAME_DEFAULT_SZ FXP_RESPONSE_DATA_DEFAULT_SZ
293 #endif
294
295 #define FXP_MAX_PACKET_LEN (1024 * 512)
296
297 /* Maximum number of SFTP extended attributes we accept at one time. */
298 #ifndef FXP_MAX_EXTENDED_ATTRIBUTES
299 # define FXP_MAX_EXTENDED_ATTRIBUTES 100
300 #endif
301
302 /* Maximum length of SFTP extended attribute name OR value. */
303 #ifndef FXP_MAX_EXTENDED_ATTR_LEN
304 # define FXP_MAX_EXTENDED_ATTR_LEN 1024
305 #endif
306
307 struct fxp_extpair {
308 char *ext_name;
309 uint32_t ext_datalen;
310 unsigned char *ext_data;
311 };
312
313 static pool *fxp_pool = NULL;
314 static int fxp_use_gmt = TRUE;
315
316 /* FSOptions */
317 static unsigned long fxp_fsio_opts = 0UL;
318 static unsigned int fxp_min_client_version = 1;
319 static unsigned int fxp_max_client_version = 6;
320 static unsigned int fxp_utf8_protocol_version = 4;
321 static unsigned long fxp_ext_flags = SFTP_FXP_EXT_DEFAULT;
322
323 static pr_fh_t *fxp_displaylogin_fh = NULL;
324 static int fxp_sent_display_login_file = FALSE;
325
326 /* For handling "version-select" requests properly (or rejecting them as
327 * necessary.
328 */
329 static int allow_version_select = FALSE;
330
331 /* Use a struct to maintain the per-channel FXP-specific values. */
332 struct fxp_session {
333 struct fxp_session *next, *prev;
334
335 pool *pool;
336 uint32_t channel_id;
337 uint32_t client_version;
338 pr_table_t *handle_tab;
339 };
340
341 static struct fxp_session *fxp_session = NULL, *fxp_sessions = NULL;
342
343 static const char *trace_channel = "sftp";
344
345 /* Necessary prototypes */
346 static struct fxp_handle *fxp_handle_get(const char *);
347 static struct fxp_packet *fxp_packet_create(pool *, uint32_t);
348 static int fxp_packet_write(struct fxp_packet *);
349
350 static struct fxp_session *fxp_get_session(uint32_t channel_id) {
351 struct fxp_session *sess;
352
353 sess = fxp_sessions;
354 while (sess) {
355 pr_signals_handle();
356
357 if (sess->channel_id == channel_id) {
358 return sess;
359 }
360
361 sess = sess->next;
362 }
363
364 errno = ENOENT;
365 return NULL;
366 }
367
368 static int fxp_timeout_stalled_cb(CALLBACK_FRAME) {
369 pr_event_generate("core.timeout-stalled", NULL);
370
371 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
372 "SFTP data transfer stalled timeout (%d secs) reached",
373 pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED));
374 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION,
375 "data stalled timeout reached");
376
377 return 0;
378 }
379
380 static cmd_rec *fxp_cmd_alloc(pool *p, const char *name, char *arg) {
381 cmd_rec *cmd;
382
383 cmd = pr_cmd_alloc(p, 2, pstrdup(p, name), arg ? arg : "");
384 cmd->arg = arg;
385
386 return cmd;
387 }
388
389 static const char *fxp_strerror(uint32_t status) {
390 switch (status) {
391 case SSH2_FX_OK:
392 return "OK";
393
394 case SSH2_FX_EOF:
395 return "End of file";
396
397 case SSH2_FX_NO_SUCH_FILE:
398 return "No such file";
399
400 case SSH2_FX_PERMISSION_DENIED:
401 return "Permission denied";
402
403 case SSH2_FX_BAD_MESSAGE:
404 return "Bad message";
405
406 case SSH2_FX_OP_UNSUPPORTED:
407 return "Unsupported operation";
408
409 case SSH2_FX_INVALID_HANDLE:
410 return "Invalid handle";
411
412 case SSH2_FX_NO_SUCH_PATH:
413 return "No such path";
414
415 case SSH2_FX_FILE_ALREADY_EXISTS:
416 return "File already exists";
417
418 case SSH2_FX_NO_SPACE_ON_FILESYSTEM:
419 return "Out of disk space";
420
421 case SSH2_FX_QUOTA_EXCEEDED:
422 return "Quota exceeded";
423
424 case SSH2_FX_UNKNOWN_PRINCIPAL:
425 return "Unknown principal";
426
427 case SSH2_FX_LOCK_CONFLICT:
428 return "Lock conflict";
429
430 case SSH2_FX_DIR_NOT_EMPTY:
431 return "Directory is not empty";
432
433 case SSH2_FX_NOT_A_DIRECTORY:
434 return "Not a directory";
435
436 case SSH2_FX_INVALID_FILENAME:
437 return "Invalid filename";
438
439 case SSH2_FX_LINK_LOOP:
440 return "Link loop";
441
442 case SSH2_FX_INVALID_PARAMETER:
443 return "Invalid parameter";
444
445 case SSH2_FX_FILE_IS_A_DIRECTORY:
446 return "File is a directory";
447
448 case SSH2_FX_OWNER_INVALID:
449 return "Invalid owner";
450
451 case SSH2_FX_GROUP_INVALID:
452 return "Invalid group";
453 }
454
455 return "Failure";
456 }
457
458 static uint32_t fxp_errno2status(int xerrno, const char **reason) {
459 uint32_t status_code = SSH2_FX_FAILURE;
460
461 /* Provide a default reason string; it will be overwritten below by a
462 * more appropriate string as necessary.
463 */
464 if (reason != NULL) {
465 *reason = fxp_strerror(status_code);
466 }
467
468 switch (xerrno) {
469 case 0:
470 status_code = SSH2_FX_OK;
471 if (reason) {
472 *reason = fxp_strerror(status_code);
473 }
474 break;
475
476 case EOF:
477 status_code = SSH2_FX_EOF;
478 if (reason) {
479 *reason = fxp_strerror(status_code);
480 }
481 break;
482
483 case EBADF:
484 case ENOENT:
485 #ifdef ENXIO
486 case ENXIO:
487 #endif
488 #if defined(ENODATA)
489 case ENODATA:
490 #endif
491 #if defined(ENOATTR) && defined(ENODATA) && ENOATTR != ENODATA
492 case ENOATTR:
493 #endif
494 status_code = SSH2_FX_NO_SUCH_FILE;
495 if (reason) {
496 *reason = fxp_strerror(status_code);
497 }
498 break;
499
500 case EACCES:
501 case EPERM:
502 status_code = SSH2_FX_PERMISSION_DENIED;
503 if (reason) {
504 *reason = fxp_strerror(status_code);
505 }
506 break;
507
508 case EIO:
509 case EXDEV:
510 if (reason) {
511 *reason = strerror(xerrno);
512 }
513 break;
514
515 case ENOSYS:
516 #ifdef ENOTSUP
517 case ENOTSUP:
518 #endif
519 status_code = SSH2_FX_OP_UNSUPPORTED;
520 if (reason) {
521 *reason = fxp_strerror(status_code);
522 }
523 break;
524
525 case EFAULT:
526 case EINVAL:
527 #ifdef E2BIG
528 case E2BIG:
529 #endif
530 #ifdef ERANGE
531 case ERANGE:
532 #endif
533 if (reason) {
534 *reason = fxp_strerror(SSH2_FX_INVALID_PARAMETER);
535 }
536
537 if (fxp_session->client_version > 5) {
538 status_code = SSH2_FX_INVALID_PARAMETER;
539
540 } else {
541 status_code = SSH2_FX_OP_UNSUPPORTED;
542 }
543 break;
544
545 case EEXIST:
546 if (reason) {
547 *reason = fxp_strerror(SSH2_FX_FILE_ALREADY_EXISTS);
548 }
549
550 if (fxp_session->client_version > 3) {
551 status_code = SSH2_FX_FILE_ALREADY_EXISTS;
552 }
553 break;
554
555 #ifdef EDQUOT
556 case EDQUOT:
557 if (reason) {
558 *reason = fxp_strerror(SSH2_FX_QUOTA_EXCEEDED);
559 }
560
561 if (fxp_session->client_version > 4) {
562 status_code = SSH2_FX_QUOTA_EXCEEDED;
563 }
564 break;
565 #endif
566
567 #ifdef EFBIG
568 case EFBIG:
569 #endif
570 #ifdef ENOSPC
571 case ENOSPC:
572 #endif
573 if (reason) {
574 *reason = fxp_strerror(SSH2_FX_NO_SPACE_ON_FILESYSTEM);
575 }
576
577 if (fxp_session->client_version > 4) {
578 status_code = SSH2_FX_NO_SPACE_ON_FILESYSTEM;
579 }
580 break;
581
582 case EISDIR:
583 if (reason) {
584 *reason = fxp_strerror(SSH2_FX_FILE_IS_A_DIRECTORY);
585 }
586
587 if (fxp_session->client_version > 5) {
588 status_code = SSH2_FX_FILE_IS_A_DIRECTORY;
589 }
590 break;
591
592 case ENOTDIR:
593 if (reason) {
594 *reason = fxp_strerror(SSH2_FX_NOT_A_DIRECTORY);
595 }
596
597 if (fxp_session->client_version > 5) {
598 status_code = SSH2_FX_NOT_A_DIRECTORY;
599 }
600 break;
601
602 case ELOOP:
603 if (reason) {
604 *reason = fxp_strerror(SSH2_FX_LINK_LOOP);
605 }
606
607 if (fxp_session->client_version > 5) {
608 status_code = SSH2_FX_LINK_LOOP;
609 }
610 break;
611
612 #ifdef ENAMETOOLONG
613 case ENAMETOOLONG:
614 if (reason) {
615 *reason = fxp_strerror(SSH2_FX_INVALID_FILENAME);
616 }
617
618 if (fxp_session->client_version > 5) {
619 status_code = SSH2_FX_INVALID_FILENAME;
620 }
621 break;
622 #endif
623
624 /* On AIX5, ENOTEMPTY and EEXIST are defined to be the same value.
625 * And using the same value multiple times in a switch statement
626 * causes compiler grief. See:
627 *
628 * http://forums.proftpd.org/smf/index.php/topic,3971.0.html
629 *
630 * To handle this, then, we only use ENOTEMPTY if it is defined to
631 * be a different value than EEXIST. We'll have an AIX-specific
632 * check for this particular error case in the fxp_handle_rmdir()
633 * function.
634 */
635 #if defined(ENOTEMPTY) && ENOTEMPTY != EEXIST
636 case ENOTEMPTY:
637 if (reason) {
638 *reason = fxp_strerror(SSH2_FX_DIR_NOT_EMPTY);
639 }
640
641 if (fxp_session->client_version > 5) {
642 status_code = SSH2_FX_DIR_NOT_EMPTY;
643 }
644 break;
645 #endif
646 }
647
648 return status_code;
649 }
650
651 static void fxp_set_filehandle_note(cmd_rec *cmd, struct fxp_handle *fxh) {
652 if (pr_table_add(cmd->notes, "sftp.file-handle", (char *) fxh->name, 0) < 0) {
653 int xerrno = errno;
654
655 if (xerrno != EEXIST) {
656 pr_trace_msg(trace_channel, 8,
657 "error setting 'sftp.file-handle' note: %s", strerror(xerrno));
658 }
659 }
660 }
661
662 /* Copy the filehandle-specific details into the global session.xfer struct
663 * for use by the rest of the ProFTPD machinery.
664 */
665 static void fxp_set_filehandle_sess_xfer(struct fxp_handle *fxh) {
666 if (session.xfer.p != NULL) {
667 destroy_pool(session.xfer.p);
668 }
669
670 memset(&session.xfer, 0, sizeof(session.xfer));
671
672 session.xfer.p = make_sub_pool(fxp_pool);
673 pr_pool_tag(session.xfer.p, "SFTP session transfer pool");
674
675 session.xfer.xfer_type = fxh->xfer.xfer_type;
676 session.xfer.direction = fxh->xfer.direction;
677
678 session.xfer.filename = pstrdup(session.xfer.p, fxh->xfer.filename);
679 session.xfer.path = pstrdup(session.xfer.p, fxh->xfer.path);
680 session.xfer.path_hidden = pstrdup(session.xfer.p, fxh->xfer.path_hidden);
681 if (session.xfer.path_hidden != NULL) {
682 /* If HiddenStores are in effect, this holds the actual destination path. */
683 session.xfer.path = session.xfer.path_hidden;
684 }
685
686 memcpy(&(session.xfer.start_time), &(fxh->xfer.start_time),
687 sizeof(session.xfer.start_time));
688 session.xfer.file_size = fxh->xfer.file_size;
689 session.xfer.total_bytes = fxh->xfer.total_bytes;
690 }
691
692 static void fxp_trace_v3_open_flags(pool *p, uint32_t flags) {
693 char *flags_str = "";
694 int trace_level = 15;
695
696 if (pr_trace_get_level(trace_channel) < trace_level) {
697 return;
698 }
699
700 if (flags & SSH2_FXF_READ) {
701 flags_str = pstrcat(p, flags_str, "FXF_READ", NULL);
702 }
703
704 if (flags & SSH2_FXF_WRITE) {
705 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
706 "FXF_WRITE", NULL);
707 }
708
709 if (flags & SSH2_FXF_APPEND) {
710 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
711 "FXF_APPEND", NULL);
712 }
713
714 if (flags & SSH2_FXF_CREAT) {
715 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
716 "FXF_CREAT", NULL);
717 }
718
719 if (flags & SSH2_FXF_TRUNC) {
720 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
721 "FXF_TRUNC", NULL);
722 }
723
724 if (flags & SSH2_FXF_EXCL) {
725 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
726 "FXF_EXCL", NULL);
727 }
728
729 if (flags & SSH2_FXF_TEXT) {
730 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
731 "FXF_TEXT", NULL);
732 }
733
734 pr_trace_msg(trace_channel, trace_level, "OPEN flags = %s", flags_str);
735 }
736
737 /* Map the FXP_OPEN flags to POSIX flags. */
738 static int fxp_get_v3_open_flags(uint32_t flags) {
739 int res = 0;
740
741 if (flags & SSH2_FXF_READ) {
742 if (flags & SSH2_FXF_WRITE) {
743 res = O_RDWR;
744
745 #ifdef O_APPEND
746 if (flags & SSH2_FXF_APPEND) {
747 res |= O_APPEND;
748 }
749 #endif
750
751 } else {
752 res = O_RDONLY;
753 }
754
755 } else if (flags & SSH2_FXF_WRITE) {
756 res = O_WRONLY;
757
758 #ifdef O_APPEND
759 if (flags & SSH2_FXF_APPEND) {
760 res |= O_APPEND;
761 }
762 #endif
763
764 } else if (flags & SSH2_FXF_APPEND) {
765 /* Assume FXF_WRITE, since the client didn't explicitly provide either
766 * FXF_READ or FXF_WRITE.
767 */
768 res = O_WRONLY|O_APPEND;
769 }
770
771 if (flags & SSH2_FXF_CREAT) {
772 res |= O_CREAT;
773
774 /* Since the behavior of open(2) when O_EXCL is set and O_CREAT is not
775 * set is undefined, we avoid that situation, and only check for the
776 * FXF_EXCL SSH flag if the FXF_CREAT flag is set.
777 */
778 if (flags & SSH2_FXF_EXCL) {
779 res |= O_EXCL;
780 }
781 }
782
783 if (flags & SSH2_FXF_TRUNC) {
784 res |= O_TRUNC;
785 }
786
787 return res;
788 }
789
790 static void fxp_trace_v5_bit_flags(pool *p, uint32_t attr_bits,
791 uint32_t attr_valid_bits) {
792 uint32_t flags;
793 char *flags_str = "";
794 int trace_level = 15;
795
796 if (pr_trace_get_level(trace_channel) < trace_level) {
797 return;
798 }
799
800 /* We're only interested in which, of the valid bits, are turned on/enabled
801 * in the bits value, for logging.
802 */
803
804 flags = (attr_bits & attr_valid_bits);
805
806 if (flags & SSH2_FX_ATTR_BIT_FL_READONLY) {
807 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
808 "FLAGS_READONLY", NULL);
809 }
810
811 if (flags & SSH2_FX_ATTR_BIT_FL_SYSTEM) {
812 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
813 "FLAGS_SYSTEM", NULL);
814 }
815
816 if (flags & SSH2_FX_ATTR_BIT_FL_HIDDEN) {
817 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
818 "FLAGS_HIDDEN", NULL);
819 }
820
821 if (flags & SSH2_FX_ATTR_BIT_FL_CASE_INSENSITIVE) {
822 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
823 "FLAGS_CASE_INSENSITIVE", NULL);
824 }
825
826 if (flags & SSH2_FX_ATTR_BIT_FL_ARCHIVE) {
827 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
828 "FLAGS_ARCHIVE", NULL);
829 }
830
831 if (flags & SSH2_FX_ATTR_BIT_FL_ENCRYPTED) {
832 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
833 "FLAGS_ENCRYPTED", NULL);
834 }
835
836 if (flags & SSH2_FX_ATTR_BIT_FL_COMPRESSED) {
837 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
838 "FLAGS_COMPRESSED", NULL);
839 }
840
841 if (flags & SSH2_FX_ATTR_BIT_FL_SPARSE) {
842 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
843 "FLAGS_SPARSE", NULL);
844 }
845
846 if (flags & SSH2_FX_ATTR_BIT_FL_APPEND_ONLY) {
847 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
848 "FLAGS_APPEND_ONLY", NULL);
849 }
850
851 if (flags & SSH2_FX_ATTR_BIT_FL_IMMUTABLE) {
852 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
853 "FLAGS_IMMUTABLE", NULL);
854 }
855
856 if (flags & SSH2_FX_ATTR_BIT_FL_SYNC) {
857 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
858 "FLAGS_SYNC", NULL);
859 }
860
861 if (flags & SSH2_FX_ATTR_BIT_FL_TRANSLATION_ERR) {
862 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
863 "FLAGS_TRANSLATION_ERR", NULL);
864 }
865
866 pr_trace_msg(trace_channel, 15,
867 "protocol version %lu: read BITS attribute: bits %s requested",
868 (unsigned long) fxp_session->client_version, flags_str);
869 }
870
871 static void fxp_trace_v5_open_flags(pool *p, uint32_t desired_access,
872 uint32_t flags) {
873 uint32_t base_flag;
874 char *access_str = "", *flags_str = "";
875 int trace_level = 15;
876
877 if (pr_trace_get_level(trace_channel) < trace_level) {
878 return;
879 }
880
881 /* The flags value, in the case of v5 (and later) OPEN requests, has
882 * a "base" value, along with some modifying bits masked in.
883 */
884
885 base_flag = (flags &~ (SSH2_FXF_ACCESS_APPEND_DATA|SSH2_FXF_ACCESS_APPEND_DATA_ATOMIC|SSH2_FXF_ACCESS_TEXT_MODE));
886
887 switch (base_flag) {
888 case SSH2_FXF_CREATE_NEW:
889 flags_str = pstrcat(p, flags_str, "FXF_CREATE_NEW", NULL);
890 break;
891
892 case SSH2_FXF_CREATE_TRUNCATE:
893 flags_str = pstrcat(p, flags_str, "FXF_CREATE_TRUNCATE", NULL);
894 break;
895
896 case SSH2_FXF_OPEN_EXISTING:
897 flags_str = pstrcat(p, flags_str, "FXF_OPEN_EXISTING", NULL);
898 break;
899
900 case SSH2_FXF_OPEN_OR_CREATE:
901 flags_str = pstrcat(p, flags_str, "FXF_OPEN_OR_CREATE", NULL);
902 break;
903
904 case SSH2_FXF_TRUNCATE_EXISTING:
905 flags_str = pstrcat(p, flags_str, "FXF_TRUNCATE_EXISTING", NULL);
906 break;
907
908 default:
909 flags_str = pstrcat(p, flags_str, "<unknown>", NULL);
910 }
911
912 if (flags & SSH2_FXF_ACCESS_APPEND_DATA) {
913 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
914 "FXF_ACCESS_APPEND_DATA", NULL);
915 }
916
917 if (flags & SSH2_FXF_ACCESS_APPEND_DATA_ATOMIC) {
918 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
919 "FXF_ACCESS_APPEND_DATA_ATOMIC", NULL);
920 }
921
922 if (flags & SSH2_FXF_ACCESS_TEXT_MODE) {
923 flags_str = pstrcat(p, flags_str, *flags_str ? "|" : "",
924 "FXF_ACCESS_TEXT_MODE", NULL);
925 }
926
927 if (desired_access & SSH2_FXF_WANT_READ_DATA) {
928 access_str = pstrcat(p, access_str, "FXF_WANT_READ_DATA", NULL);
929 }
930
931 if (desired_access & SSH2_FXF_WANT_WRITE_DATA) {
932 access_str = pstrcat(p, access_str, *access_str ? "|" : "",
933 "FXF_WANT_WRITE_DATA", NULL);
934 }
935
936 if (desired_access & SSH2_FXF_WANT_APPEND_DATA) {
937 access_str = pstrcat(p, access_str, *access_str ? "|" : "",
938 "FXF_WANT_APPEND_DATA", NULL);
939 }
940
941 if (desired_access & SSH2_FXF_WANT_READ_NAMED_ATTRS) {
942 access_str = pstrcat(p, access_str, *access_str ? "|" : "",
943 "FXF_WANT_READ_NAMED_ATTRS", NULL);
944 }
945
946 if (desired_access & SSH2_FXF_WANT_WRITE_NAMED_ATTRS) {
947 access_str = pstrcat(p, access_str, *access_str ? "|" : "",
948 "FXF_WANT_WRITE_NAMED_ATTRS", NULL);
949 }
950
951 if (desired_access & SSH2_FXF_WANT_WRITE_ATTRIBUTES) {
952 access_str = pstrcat(p, access_str, *access_str ? "|" : "",
953 "FXF_WANT_WRITE_ATTRS", NULL);
954 }
955
956 if (desired_access & SSH2_FXF_WANT_READ_ACL) {
957 access_str = pstrcat(p, access_str, *access_str ? "|" : "",
958 "FXF_WANT_READ_ACL", NULL);
959 }
960
961 if (desired_access & SSH2_FXF_WANT_WRITE_ACL) {
962 access_str = pstrcat(p, access_str, *access_str ? "|" : "",
963 "FXF_WANT_WRITE_ACL", NULL);
964 }
965
966 if (desired_access & SSH2_FXF_WANT_WRITE_OWNER) {
967 access_str = pstrcat(p, access_str, *access_str ? "|" : "",
968 "FXF_WANT_WRITE_OWNER", NULL);
969 }
970
971 pr_trace_msg(trace_channel, trace_level,
972 "OPEN flags = %s, desired access = %s", flags_str, access_str);
973 }
974
975 static int fxp_get_v5_open_flags(uint32_t desired_access, uint32_t flags) {
976 uint32_t base_flag;
977
978 /* Assume that the desired flag is read-only by default. */
979 int res = O_RDONLY;
980
981 /* These mappings are found in draft-ietf-secsh-filexfer-05.txt,
982 * section 6.3.1.
983 */
984
985 if ((desired_access & SSH2_FXF_WANT_READ_DATA) ||
986 (desired_access & SSH2_FXF_WANT_READ_ATTRIBUTES)) {
987
988 if ((desired_access & SSH2_FXF_WANT_WRITE_DATA) ||
989 (desired_access & SSH2_FXF_WANT_WRITE_ATTRIBUTES)) {
990 res = O_RDWR;
991
992 #ifdef O_APPEND
993 if ((desired_access & SSH2_FXF_WANT_APPEND_DATA) &&
994 ((flags & SSH2_FXF_ACCESS_APPEND_DATA) ||
995 (flags & SSH2_FXF_ACCESS_APPEND_DATA_ATOMIC))) {
996 res |= O_APPEND;
997 }
998 #endif
999
1000 } else {
1001 res = O_RDONLY;
1002 }
1003
1004 } else if ((desired_access & SSH2_FXF_WANT_WRITE_DATA) ||
1005 (desired_access & SSH2_FXF_WANT_WRITE_ATTRIBUTES)) {
1006 res = O_WRONLY;
1007
1008 #ifdef O_APPEND
1009 if ((desired_access & SSH2_FXF_WANT_APPEND_DATA) &&
1010 ((flags & SSH2_FXF_ACCESS_APPEND_DATA) ||
1011 (flags & SSH2_FXF_ACCESS_APPEND_DATA_ATOMIC))) {
1012 res |= O_APPEND;
1013 }
1014 #endif
1015 }
1016
1017 /* The flags value, in the case of v5 (and later) OPEN requests, has
1018 * a "base" value, along with some modifying bits masked in.
1019 */
1020 base_flag = (flags &~ (SSH2_FXF_ACCESS_APPEND_DATA|SSH2_FXF_ACCESS_APPEND_DATA_ATOMIC|SSH2_FXF_ACCESS_TEXT_MODE));
1021
1022 switch (base_flag) {
1023 case SSH2_FXF_CREATE_NEW:
1024 res |= O_CREAT|O_EXCL;
1025 break;
1026
1027 case SSH2_FXF_CREATE_TRUNCATE:
1028 if (res == O_RDONLY) {
1029 /* A truncate is a write. */
1030 res = O_WRONLY;
1031 }
1032 res |= O_CREAT|O_TRUNC;
1033 break;
1034
1035 case SSH2_FXF_OPEN_EXISTING:
1036 break;
1037
1038 case SSH2_FXF_OPEN_OR_CREATE:
1039 res |= O_CREAT;
1040 break;
1041
1042 case SSH2_FXF_TRUNCATE_EXISTING:
1043 if (res == O_RDONLY) {
1044 /* A truncate is a write. */
1045 res = O_WRONLY;
1046 }
1047 res |= O_TRUNC;
1048 break;
1049
1050 default:
1051 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1052 "unknown OPEN base flag value (%lu), defaulting to O_RDONLY",
1053 (unsigned long) base_flag);
1054 break;
1055 }
1056
1057 return res;
1058 }
1059
1060 static const char *fxp_strtime(pool *p, time_t t) {
1061 return pr_strtime3(p, t, TRUE);
1062 }
1063
1064 static void fxp_cmd_dispatch(cmd_rec *cmd) {
1065 pr_cmd_dispatch_phase(cmd, POST_CMD, 0);
1066 pr_cmd_dispatch_phase(cmd, LOG_CMD, 0);
1067 pr_response_clear(&resp_list);
1068 }
1069
1070 static void fxp_cmd_dispatch_err(cmd_rec *cmd) {
1071 pr_cmd_dispatch_phase(cmd, POST_CMD_ERR, 0);
1072 pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
1073 pr_response_clear(&resp_err_list);
1074 }
1075
1076 static void fxp_cmd_note_file_status(cmd_rec *cmd, const char *status) {
1077 if (pr_table_add(cmd->notes, "mod_sftp.file-status",
1078 pstrdup(cmd->pool, status), 0) < 0) {
1079 if (errno != EEXIST) {
1080 pr_trace_msg(trace_channel, 3,
1081 "error stashing file status in command notes: %s", strerror(errno));
1082 }
1083 }
1084 }
1085
1086 static const char *fxp_get_request_type_desc(unsigned char request_type) {
1087 switch (request_type) {
1088 case SFTP_SSH2_FXP_INIT:
1089 return "INIT";
1090
1091 case SFTP_SSH2_FXP_VERSION:
1092 /* XXX We should never receive this type of message from the client. */
1093 return "VERSION";
1094
1095 case SFTP_SSH2_FXP_OPEN:
1096 return "OPEN";
1097
1098 case SFTP_SSH2_FXP_CLOSE:
1099 return "CLOSE";
1100
1101 case SFTP_SSH2_FXP_READ:
1102 return "READ";
1103
1104 case SFTP_SSH2_FXP_WRITE:
1105 return "WRITE";
1106
1107 case SFTP_SSH2_FXP_LSTAT:
1108 return "LSTAT";
1109
1110 case SFTP_SSH2_FXP_FSTAT:
1111 return "FSTAT";
1112
1113 case SFTP_SSH2_FXP_SETSTAT:
1114 return "SETSTAT";
1115
1116 case SFTP_SSH2_FXP_FSETSTAT:
1117 return "FSETSTAT";
1118
1119 case SFTP_SSH2_FXP_OPENDIR:
1120 return "OPENDIR";
1121
1122 case SFTP_SSH2_FXP_READDIR:
1123 return "READDIR";
1124
1125 case SFTP_SSH2_FXP_REMOVE:
1126 return "REMOVE";
1127
1128 case SFTP_SSH2_FXP_MKDIR:
1129 return "MKDIR";
1130
1131 case SFTP_SSH2_FXP_RMDIR:
1132 return "RMDIR";
1133
1134 case SFTP_SSH2_FXP_REALPATH:
1135 return "REALPATH";
1136
1137 case SFTP_SSH2_FXP_STAT:
1138 return "STAT";
1139
1140 case SFTP_SSH2_FXP_RENAME:
1141 return "RENAME";
1142
1143 case SFTP_SSH2_FXP_READLINK:
1144 return "READLINK";
1145
1146 case SFTP_SSH2_FXP_LINK:
1147 return "LINK";
1148
1149 case SFTP_SSH2_FXP_SYMLINK:
1150 return "SYMLINK";
1151
1152 case SFTP_SSH2_FXP_LOCK:
1153 return "LOCK";
1154
1155 case SFTP_SSH2_FXP_UNLOCK:
1156 return "UNLOCK";
1157
1158 case SFTP_SSH2_FXP_STATUS:
1159 return "STATUS";
1160
1161 case SFTP_SSH2_FXP_HANDLE:
1162 return "HANDLE";
1163
1164 case SFTP_SSH2_FXP_DATA:
1165 return "DATA";
1166
1167 case SFTP_SSH2_FXP_NAME:
1168 return "NAME";
1169
1170 case SFTP_SSH2_FXP_ATTRS:
1171 return "ATTRS";
1172
1173 case SFTP_SSH2_FXP_EXTENDED:
1174 return "EXTENDED";
1175
1176 case SFTP_SSH2_FXP_EXTENDED_REPLY:
1177 return "EXTENDED_REPLY";
1178 }
1179
1180 return "(unknown)";
1181 }
1182
1183 static int fxp_path_pass_regex_filters(pool *p, const char *request,
1184 const char *path) {
1185 int res;
1186 xaset_t *set;
1187
1188 set = get_dir_ctxt(p, (char *) path);
1189
1190 res = pr_filter_allow_path(set, path);
1191 switch (res) {
1192 case 0:
1193 break;
1194
1195 case PR_FILTER_ERR_FAILS_ALLOW_FILTER:
1196 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1197 "path '%s' for %s denied by PathAllowFilter", path, request);
1198 errno = EACCES;
1199 return -1;
1200
1201 case PR_FILTER_ERR_FAILS_DENY_FILTER:
1202 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1203 "path '%s' for %s denied by PathDenyFilter", path, request);
1204 errno = EACCES;
1205 return -1;
1206 }
1207
1208 return 0;
1209 }
1210
1211 /* FXP_STATUS messages */
1212 static void fxp_status_write(pool *p, unsigned char **buf, uint32_t *buflen,
1213 uint32_t request_id, uint32_t status_code, const char *status_msg,
1214 const char *extra_data) {
1215 char num[32];
1216
1217 /* Add a fake response to the response chain, for use by mod_log's
1218 * logging, e.g. for supporting the %S/%s LogFormat variables.
1219 */
1220
1221 pr_response_clear(&resp_list);
1222 pr_response_clear(&resp_err_list);
1223
1224 memset(num, '\0', sizeof(num));
1225 pr_snprintf(num, sizeof(num)-1, "%lu", (unsigned long) status_code);
1226 num[sizeof(num)-1] = '\0';
1227 pr_response_add(pstrdup(p, num), "%s", status_msg);
1228
1229 sftp_msg_write_byte(buf, buflen, SFTP_SSH2_FXP_STATUS);
1230 sftp_msg_write_int(buf, buflen, request_id);
1231 sftp_msg_write_int(buf, buflen, status_code);
1232
1233 if (fxp_session->client_version >= 3) {
1234 sftp_msg_write_string(buf, buflen, status_msg);
1235 /* XXX localization */
1236 sftp_msg_write_string(buf, buflen, "en-US");
1237
1238 if (fxp_session->client_version >= 5 &&
1239 extra_data) {
1240 /* Used specifically for UNKNOWN_PRINCIPAL errors */
1241 sftp_msg_write_string(buf, buflen, extra_data);
1242 }
1243 }
1244 }
1245
1246 /* The SFTP subsystem Draft defines a few new data types. */
1247
1248 #if 0
1249 /* XXX Not really used for messages from clients. */
1250 static uint16_t fxp_msg_read_short(pool *p, char **buf, uint32_t *buflen) {
1251 uint16_t val;
1252
1253 (void) p;
1254
1255 if (*buflen < sizeof(uint16_t)) {
1256 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1257 "SFTP message format error: unable to read short (buflen = %lu)",
1258 (unsigned long) *buflen);
1259 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
1260 }
1261
1262 memcpy(&val, *buf, sizeof(uint16_t));
1263 (*buf) += sizeof(uint16_t);
1264 (*buflen) -= sizeof(uint16_t);
1265
1266 val = ntohs(val);
1267 return val;
1268 }
1269 #endif
1270
1271 static struct fxp_extpair *fxp_msg_read_extpair(pool *p, unsigned char **buf,
1272 uint32_t *buflen) {
1273 uint32_t namelen, datalen;
1274 unsigned char *name, *data;
1275 struct fxp_extpair *extpair;
1276
1277 namelen = sftp_msg_read_int(p, buf, buflen);
1278 if (*buflen < namelen) {
1279 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1280 "SFTP message format error: unable to read %lu bytes of extpair name "
1281 "data (buflen = %lu)", (unsigned long) namelen, (unsigned long) *buflen);
1282 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
1283 }
1284
1285 if (namelen > FXP_MAX_EXTENDED_ATTR_LEN) {
1286 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1287 "received too-long extended attribute name (%lu > max %lu), ignoring",
1288 (unsigned long) namelen, (unsigned long) FXP_MAX_EXTENDED_ATTR_LEN);
1289 errno = EINVAL;
1290 return NULL;
1291 }
1292
1293 name = palloc(p, namelen + 1);
1294 memcpy(name, *buf, namelen);
1295 (*buf) += namelen;
1296 (*buflen) -= namelen;
1297 name[namelen] = '\0';
1298
1299 datalen = sftp_msg_read_int(p, buf, buflen);
1300 if (datalen > 0) {
1301 if (datalen > FXP_MAX_EXTENDED_ATTR_LEN) {
1302 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1303 "received too-long extended attribute '%s' value (%lu > max %lu), "
1304 "ignoring", name, (unsigned long) datalen,
1305 (unsigned long) FXP_MAX_EXTENDED_ATTR_LEN);
1306 errno = EINVAL;
1307 return NULL;
1308 }
1309
1310 data = sftp_msg_read_data(p, buf, buflen, datalen);
1311
1312 } else {
1313 data = NULL;
1314 }
1315
1316 extpair = palloc(p, sizeof(struct fxp_extpair));
1317 extpair->ext_name = (char *) name;
1318 extpair->ext_datalen = datalen;
1319 extpair->ext_data = data;
1320
1321 return extpair;
1322 }
1323
1324 static uint32_t fxp_msg_write_short(unsigned char **buf, uint32_t *buflen,
1325 uint16_t val) {
1326 uint32_t len = 0;
1327
1328 if (*buflen < sizeof(uint16_t)) {
1329 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1330 "SFTP message format error: unable to write short (buflen = %lu)",
1331 (unsigned long) *buflen);
1332 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
1333 }
1334
1335 len = sizeof(uint16_t);
1336
1337 val = htons(val);
1338 memcpy(*buf, &val, len);
1339 (*buf) += len;
1340 (*buflen) -= len;
1341
1342 return len;
1343 }
1344
1345 static void fxp_msg_write_extpair(unsigned char **buf, uint32_t *buflen,
1346 struct fxp_extpair *extpair) {
1347 uint32_t len;
1348
1349 len = strlen(extpair->ext_name);
1350 sftp_msg_write_data(buf, buflen, (unsigned char *) extpair->ext_name, len,
1351 TRUE);
1352 sftp_msg_write_data(buf, buflen, extpair->ext_data, extpair->ext_datalen,
1353 TRUE);
1354 }
1355
1356 static uint32_t fxp_attrs_clear_unsupported(uint32_t attr_flags) {
1357
1358 /* Clear any unsupported/ignored flags. */
1359
1360 if (attr_flags & SSH2_FX_ATTR_ALLOCATION_SIZE) {
1361 pr_trace_msg(trace_channel, 17,
1362 "clearing unsupported ALLOCATION_SIZE attribute flag");
1363 attr_flags &= ~SSH2_FX_ATTR_ALLOCATION_SIZE;
1364 }
1365
1366 if (attr_flags & SSH2_FX_ATTR_SUBSECOND_TIMES) {
1367 pr_trace_msg(trace_channel, 17,
1368 "clearing unsupported SUBSECOND_TIMES attribute flag");
1369 attr_flags &= ~SSH2_FX_ATTR_SUBSECOND_TIMES;
1370 }
1371
1372 if (attr_flags & SSH2_FX_ATTR_CREATETIME) {
1373 pr_trace_msg(trace_channel, 17,
1374 "clearing unsupported CREATETIME attribute flag");
1375 attr_flags &= ~SSH2_FX_ATTR_CREATETIME;
1376 }
1377
1378 if (attr_flags & SSH2_FX_ATTR_ACL) {
1379 pr_trace_msg(trace_channel, 17,
1380 "clearing unsupported ACL attribute flag");
1381 attr_flags &= ~SSH2_FX_ATTR_ACL;
1382 }
1383
1384 if (attr_flags & SSH2_FX_ATTR_BITS) {
1385 pr_trace_msg(trace_channel, 17,
1386 "clearing unsupported BITS attribute flag");
1387 attr_flags &= ~SSH2_FX_ATTR_BITS;
1388 }
1389
1390 if (attr_flags & SSH2_FX_ATTR_TEXT_HINT) {
1391 pr_trace_msg(trace_channel, 17,
1392 "clearing unsupported TEXT_HINT attribute flag");
1393 attr_flags &= ~SSH2_FX_ATTR_TEXT_HINT;
1394 }
1395
1396 if (attr_flags & SSH2_FX_ATTR_MIME_TYPE) {
1397 pr_trace_msg(trace_channel, 17,
1398 "clearing unsupported MIME_TYPE attribute flag");
1399 attr_flags &= ~SSH2_FX_ATTR_MIME_TYPE;
1400 }
1401
1402 if (attr_flags & SSH2_FX_ATTR_UNTRANSLATED_NAME) {
1403 pr_trace_msg(trace_channel, 17,
1404 "clearing unsupported UNTRANSLATED_NAME attribute flag");
1405 attr_flags &= ~SSH2_FX_ATTR_UNTRANSLATED_NAME;
1406 }
1407
1408 if (attr_flags & SSH2_FX_ATTR_CTIME) {
1409 pr_trace_msg(trace_channel, 17,
1410 "clearing unsupported CTIME attribute flag");
1411 attr_flags &= ~SSH2_FX_ATTR_CTIME;
1412 }
1413
1414 return attr_flags;
1415 }
1416
1417 static int fxp_attrs_set(pr_fh_t *fh, const char *path, struct stat *attrs,
1418 uint32_t attr_flags, array_header *xattrs, unsigned char **buf,
1419 uint32_t *buflen, struct fxp_packet *fxp) {
1420 struct stat st;
1421 int res, xerrno;
1422
1423 /* Note: path is never null; it is always passed by the caller. fh MAY be
1424 * null, depending on whether the caller already has a file handle or not.
1425 */
1426
1427 if (fh != NULL) {
1428 res = pr_fsio_fstat(fh, &st);
1429 xerrno = errno;
1430
1431 } else {
1432 pr_fs_clear_cache2(path);
1433 res = pr_fsio_lstat(path, &st);
1434 xerrno = errno;
1435 }
1436
1437 if (res < 0) {
1438 uint32_t status_code;
1439 const char *reason;
1440
1441 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1442 "error checking '%s': %s", path, strerror(xerrno));
1443
1444 status_code = fxp_errno2status(xerrno, &reason);
1445
1446 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
1447 "('%s' [%d])", (unsigned long) status_code, reason,
1448 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
1449
1450 fxp_status_write(fxp->pool, buf, buflen, fxp->request_id, status_code,
1451 reason, NULL);
1452
1453 errno = xerrno;
1454 return -1;
1455 }
1456
1457 if (attr_flags & SSH2_FX_ATTR_PERMISSIONS) {
1458 if (attrs->st_mode &&
1459 (st.st_mode & ~S_IFMT) != (attrs->st_mode & ~S_IFMT)) {
1460 cmd_rec *cmd;
1461
1462 cmd = fxp_cmd_alloc(fxp->pool, "SITE_CHMOD", pstrdup(fxp->pool, path));
1463 if (!dir_check(fxp->pool, cmd, G_WRITE, (char *) path, NULL)) {
1464 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1465 "chmod of '%s' blocked by <Limit> configuration "
1466 "(see the IgnoreSFTPSetPerms/IgnoreSFTPUploadPerms SFTPOptions)",
1467 path);
1468
1469 xerrno = EACCES;
1470 res = -1;
1471
1472 } else {
1473 if (fh != NULL) {
1474 res = pr_fsio_fchmod(fh, attrs->st_mode);
1475 xerrno = errno;
1476
1477 } else {
1478 res = pr_fsio_chmod(path, attrs->st_mode);
1479 xerrno = errno;
1480 }
1481 }
1482
1483 if (res < 0) {
1484 uint32_t status_code;
1485 const char *reason;
1486
1487 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1488 "error changing permissions of '%s' to 0%o: %s", path,
1489 (unsigned int) (attrs->st_mode & ~S_IFMT), strerror(xerrno));
1490
1491 status_code = fxp_errno2status(xerrno, &reason);
1492
1493 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
1494 "('%s' [%d])", (unsigned long) status_code, reason,
1495 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
1496
1497 fxp_status_write(fxp->pool, buf, buflen, fxp->request_id, status_code,
1498 reason, NULL);
1499
1500 errno = xerrno;
1501 return -1;
1502 }
1503
1504 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1505 "client set permissions on '%s' to 0%o", path,
1506 (unsigned int) (attrs->st_mode & ~S_IFMT));
1507 }
1508 }
1509
1510 if ((attr_flags & SSH2_FX_ATTR_UIDGID) ||
1511 (attr_flags & SSH2_FX_ATTR_OWNERGROUP)) {
1512 int do_chown = FALSE;
1513 uid_t client_uid = (uid_t) -1;
1514 gid_t client_gid = (gid_t) -1;
1515
1516 if (st.st_uid != attrs->st_uid) {
1517 client_uid = attrs->st_uid;
1518 do_chown = TRUE;
1519 }
1520
1521 if (st.st_gid != attrs->st_gid) {
1522 client_gid = attrs->st_gid;
1523 do_chown = TRUE;
1524 }
1525
1526 if (do_chown == TRUE) {
1527 cmd_rec *cmd;
1528
1529 cmd = fxp_cmd_alloc(fxp->pool, "SITE_CHGRP", pstrdup(fxp->pool, path));
1530 if (!dir_check(fxp->pool, cmd, G_WRITE, (char *) path, NULL)) {
1531 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1532 "chown of '%s' blocked by <Limit> configuration", path);
1533
1534 xerrno = EACCES;
1535 res = -1;
1536
1537 } else {
1538 if (fh != NULL) {
1539 res = pr_fsio_fchown(fh, client_uid, client_gid);
1540 xerrno = errno;
1541
1542 } else {
1543 res = pr_fsio_chown(path, client_uid, client_gid);
1544 xerrno = errno;
1545 }
1546 }
1547
1548 if (res < 0) {
1549 uint32_t status_code;
1550 const char *reason;
1551
1552 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1553 "error changing ownership of '%s' to UID %s, GID %s: %s",
1554 path, pr_uid2str(fxp->pool, client_uid),
1555 pr_gid2str(fxp->pool, client_gid), strerror(xerrno));
1556
1557 status_code = fxp_errno2status(xerrno, &reason);
1558
1559 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
1560 "('%s' [%d])", (unsigned long) status_code, reason,
1561 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
1562
1563 fxp_status_write(fxp->pool, buf, buflen, fxp->request_id, status_code,
1564 reason, NULL);
1565
1566 errno = xerrno;
1567 return -1;
1568 }
1569
1570 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1571 "client set ownership of '%s' to UID %s, GID %s",
1572 path, pr_uid2str(fxp->pool, client_uid),
1573 pr_gid2str(fxp->pool, client_gid));
1574 }
1575 }
1576
1577 if (attr_flags & SSH2_FX_ATTR_SIZE) {
1578 if (st.st_size != attrs->st_size) {
1579
1580 /* If we're dealing with a FIFO, just pretend that the truncate(2)
1581 * succeeded; FIFOs don't handle truncation well. And it won't
1582 * necessarily matter to the client, right?
1583 */
1584 if (S_ISREG(st.st_mode)) {
1585 if (fh != NULL) {
1586 res = pr_fsio_ftruncate(fh, attrs->st_size);
1587 xerrno = errno;
1588
1589 } else {
1590 res = pr_fsio_truncate(path, attrs->st_size);
1591 xerrno = errno;
1592 }
1593
1594 } else {
1595 res = 0;
1596 }
1597
1598 if (res < 0) {
1599 uint32_t status_code;
1600 const char *reason;
1601
1602 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1603 "error changing size of '%s' from %" PR_LU " bytes to %" PR_LU
1604 " bytes: %s", path, (pr_off_t) st.st_size, (pr_off_t) attrs->st_size,
1605 strerror(xerrno));
1606
1607 status_code = fxp_errno2status(xerrno, &reason);
1608
1609 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
1610 "('%s' [%d])", (unsigned long) status_code, reason,
1611 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
1612
1613 fxp_status_write(fxp->pool, buf, buflen, fxp->request_id, status_code,
1614 reason, NULL);
1615
1616 errno = xerrno;
1617 return -1;
1618 }
1619
1620 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1621 "client set size of '%s' to %" PR_LU " bytes", path,
1622 (pr_off_t) attrs->st_size);
1623 }
1624 }
1625
1626 if (fxp_session->client_version <= 3 &&
1627 attr_flags & SSH2_FX_ATTR_ACMODTIME) {
1628 if (st.st_atime != attrs->st_atime ||
1629 st.st_mtime != attrs->st_mtime) {
1630 struct timeval tvs[2];
1631
1632 tvs[0].tv_sec = attrs->st_atime;
1633 tvs[0].tv_usec = 0;
1634
1635 tvs[1].tv_sec = attrs->st_mtime;
1636 tvs[1].tv_usec = 0;
1637
1638 if (fh != NULL) {
1639 res = pr_fsio_futimes(fh, tvs);
1640 xerrno = errno;
1641
1642 } else {
1643 res = pr_fsio_utimes(path, tvs);
1644 xerrno = errno;
1645 }
1646
1647 if (res < 0) {
1648 uint32_t status_code;
1649 const char *reason;
1650
1651 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1652 "error changing access/modification times '%s': %s", path,
1653 strerror(xerrno));
1654
1655 status_code = fxp_errno2status(xerrno, &reason);
1656
1657 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
1658 "('%s' [%d])", (unsigned long) status_code, reason,
1659 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
1660
1661 fxp_status_write(fxp->pool, buf, buflen, fxp->request_id, status_code,
1662 reason, NULL);
1663
1664 errno = xerrno;
1665 return -1;
1666 }
1667
1668 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1669 "client set access time of '%s' to %s, modification time to %s",
1670 path, fxp_strtime(fxp->pool, attrs->st_atime),
1671 fxp_strtime(fxp->pool, attrs->st_mtime));
1672 }
1673 }
1674
1675 if (fxp_session->client_version > 3) {
1676 /* Note: we handle the xattrs FIRST, before the timestamps, so that
1677 * setting the xattrs does not change the expected timestamps, thus
1678 * preserving the principle of least surprise.
1679 */
1680 if (attr_flags & SSH2_FX_ATTR_EXTENDED) {
1681 #if defined(PR_USE_XATTR)
1682 if (xattrs != NULL &&
1683 xattrs->nelts > 0) {
1684 register unsigned int i;
1685 struct fxp_extpair **ext_pairs;
1686
1687 ext_pairs = xattrs->elts;
1688 for (i = 0; i < xattrs->nelts; i++) {
1689 struct fxp_extpair *xattr;
1690 const char *xattr_name;
1691 void *xattr_val;
1692 size_t xattr_valsz;
1693
1694 xattr = ext_pairs[i];
1695 xattr_name = xattr->ext_name;
1696 xattr_val = xattr->ext_data;
1697 xattr_valsz = (size_t) xattr->ext_datalen;
1698
1699 if (fh != NULL) {
1700 res = pr_fsio_fsetxattr(fxp->pool, fh, xattr_name, xattr_val,
1701 xattr_valsz, 0);
1702 xerrno = errno;
1703
1704 } else {
1705 res = pr_fsio_lsetxattr(fxp->pool, path, xattr_name, xattr_val,
1706 xattr_valsz, 0);
1707 xerrno = errno;
1708 }
1709
1710 if (res < 0) {
1711 uint32_t status_code;
1712 const char *reason;
1713
1714 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1715 "error setting xattr '%s' (%lu bytes) on '%s': %s", xattr_name,
1716 (unsigned long) xattr_valsz, path, strerror(xerrno));
1717
1718 status_code = fxp_errno2status(xerrno, &reason);
1719
1720 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
1721 "('%s' [%d])", (unsigned long) status_code, reason,
1722 strerror(xerrno), xerrno);
1723
1724 fxp_status_write(fxp->pool, buf, buflen, fxp->request_id,
1725 status_code, reason, NULL);
1726
1727 errno = xerrno;
1728 return -1;
1729 }
1730 }
1731 }
1732 #else
1733 (void) xattrs;
1734 #endif /* PR_USE_XATTR */
1735 }
1736
1737 if (attr_flags & SSH2_FX_ATTR_ACCESSTIME) {
1738 if (st.st_atime != attrs->st_atime) {
1739 struct timeval tvs[2];
1740
1741 tvs[0].tv_sec = attrs->st_atime;
1742 tvs[0].tv_usec = 0;
1743
1744 tvs[1].tv_sec = st.st_mtime;
1745 tvs[1].tv_usec = 0;
1746
1747 if (fh != NULL) {
1748 res = pr_fsio_futimes(fh, tvs);
1749 xerrno = errno;
1750
1751 } else {
1752 res = pr_fsio_utimes(path, tvs);
1753 xerrno = errno;
1754 }
1755
1756 if (res < 0) {
1757 uint32_t status_code;
1758 const char *reason;
1759
1760 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1761 "error changing access time '%s': %s", path, strerror(xerrno));
1762
1763 status_code = fxp_errno2status(xerrno, &reason);
1764
1765 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
1766 "('%s' [%d])", (unsigned long) status_code, reason,
1767 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
1768
1769 fxp_status_write(fxp->pool, buf, buflen, fxp->request_id, status_code,
1770 reason, NULL);
1771
1772 errno = xerrno;
1773 return -1;
1774 }
1775
1776 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1777 "client set access time of '%s' to %s", path,
1778 fxp_strtime(fxp->pool, attrs->st_atime));
1779 }
1780 }
1781
1782 if (attr_flags & SSH2_FX_ATTR_MODIFYTIME) {
1783 if (st.st_mtime != attrs->st_mtime) {
1784 struct timeval tvs[2];
1785
1786 tvs[0].tv_sec = st.st_atime;
1787 tvs[0].tv_usec = 0;
1788
1789 tvs[1].tv_sec = attrs->st_mtime;
1790 tvs[1].tv_usec = 0;
1791
1792 if (fh != NULL) {
1793 res = pr_fsio_futimes(fh, tvs);
1794 xerrno = errno;
1795
1796 } else {
1797 res = pr_fsio_utimes(path, tvs);
1798 xerrno = errno;
1799 }
1800
1801 if (res < 0) {
1802 uint32_t status_code;
1803 const char *reason;
1804
1805 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1806 "error changing modification time '%s': %s", path,
1807 strerror(xerrno));
1808
1809 status_code = fxp_errno2status(xerrno, &reason);
1810
1811 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
1812 "('%s' [%d])", (unsigned long) status_code, reason,
1813 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
1814
1815 fxp_status_write(fxp->pool, buf, buflen, fxp->request_id, status_code,
1816 reason, NULL);
1817
1818 errno = xerrno;
1819 return -1;
1820 }
1821
1822 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
1823 "client set modification time of '%s' to %s", path,
1824 fxp_strtime(fxp->pool, attrs->st_mtime));
1825 }
1826 }
1827 }
1828
1829 return 0;
1830 }
1831
1832 /* Provide a stringified representation of attributes sent by clients. */
1833
1834 static const char *fxp_strftype(mode_t mode) {
1835 if (S_ISREG(mode)) {
1836 return "file";
1837 }
1838
1839 if (S_ISDIR(mode)) {
1840 return "dir";
1841 }
1842
1843 if (S_ISLNK(mode)) {
1844 return "symlink";
1845 }
1846
1847 if (S_ISSOCK(mode)) {
1848 return "socket";
1849 }
1850
1851 #ifdef S_ISFIFO
1852 if (S_ISFIFO(mode)) {
1853 return "fifo";
1854 }
1855 #endif
1856
1857 #ifdef S_ISCHR
1858 if (S_ISCHR(mode)) {
1859 return "dev/char";
1860 }
1861 #endif
1862
1863 #ifdef S_ISBLK
1864 if (S_ISBLK(mode)) {
1865 return "dev/block";
1866 }
1867 #endif
1868
1869 return "unknown";
1870 }
1871
1872 static char *fxp_strattrs(pool *p, struct stat *st, uint32_t *attr_flags) {
1873 char buf[PR_TUNABLE_BUFFER_SIZE], *ptr;
1874 size_t buflen = 0, bufsz;
1875 uint32_t flags = 0;
1876
1877 buflen = 0;
1878 bufsz = sizeof(buf);
1879 memset(buf, '\0', bufsz);
1880
1881 ptr = buf;
1882
1883 if (attr_flags != NULL) {
1884 flags = *attr_flags;
1885
1886 } else {
1887 if (fxp_session->client_version <= 3) {
1888 flags = SSH2_FX_ATTR_SIZE|SSH2_FX_ATTR_UIDGID|SSH2_FX_ATTR_PERMISSIONS|
1889 SSH2_FX_ATTR_ACMODTIME;
1890
1891 } else {
1892 flags = SSH2_FX_ATTR_SIZE|SSH2_FX_ATTR_PERMISSIONS|
1893 SSH2_FX_ATTR_ACCESSTIME|SSH2_FX_ATTR_MODIFYTIME|
1894 SSH2_FX_ATTR_OWNERGROUP;
1895
1896 if (fxp_session->client_version >= 6) {
1897 flags |= SSH2_FX_ATTR_LINK_COUNT;
1898 #if defined(PR_USE_XATTR)
1899 flags |= SSH2_FX_ATTR_EXTENDED;
1900 #endif /* PR_USE_XATTR */
1901 }
1902 }
1903 }
1904
1905 pr_snprintf(ptr, bufsz - buflen, "type=%s;", fxp_strftype(st->st_mode));
1906 buflen = strlen(buf);
1907 ptr = buf + buflen;
1908
1909 if (flags & SSH2_FX_ATTR_SIZE) {
1910 pr_snprintf(ptr, bufsz - buflen, "size=%" PR_LU ";",
1911 (pr_off_t) st->st_size);
1912 buflen = strlen(buf);
1913 ptr = buf + buflen;
1914 }
1915
1916 if ((flags & SSH2_FX_ATTR_UIDGID) ||
1917 (flags & SSH2_FX_ATTR_OWNERGROUP)) {
1918 pr_snprintf(ptr, bufsz - buflen, "UNIX.owner=%s;",
1919 pr_uid2str(NULL, st->st_uid));
1920 buflen = strlen(buf);
1921 ptr = buf + buflen;
1922
1923 pr_snprintf(ptr, bufsz - buflen, "UNIX.group=%s;",
1924 pr_gid2str(NULL, st->st_gid));
1925 buflen = strlen(buf);
1926 ptr = buf + buflen;
1927 }
1928
1929 if (flags & SSH2_FX_ATTR_PERMISSIONS) {
1930 pr_snprintf(ptr, bufsz - buflen, "UNIX.mode=%04o;",
1931 (unsigned int) st->st_mode & 07777);
1932 buflen = strlen(buf);
1933 ptr = buf + buflen;
1934 }
1935
1936 if (fxp_session->client_version <= 3) {
1937 if (flags & SSH2_FX_ATTR_ACMODTIME) {
1938 struct tm *tm;
1939
1940 tm = pr_gmtime(p, (const time_t *) &st->st_atime);
1941 if (tm != NULL) {
1942 pr_snprintf(ptr, bufsz - buflen, "access=%04d%02d%02d%02d%02d%02d;",
1943 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min,
1944 tm->tm_sec);
1945 buflen = strlen(buf);
1946 ptr = buf + buflen;
1947
1948 } else {
1949 pr_trace_msg(trace_channel, 1,
1950 "error obtaining st_atime GMT timestamp: %s", strerror(errno));
1951 }
1952
1953 tm = pr_gmtime(p, (const time_t *) &st->st_mtime);
1954 if (tm != NULL) {
1955 pr_snprintf(ptr, bufsz - buflen, "modify=%04d%02d%02d%02d%02d%02d;",
1956 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min,
1957 tm->tm_sec);
1958
1959 } else {
1960 pr_trace_msg(trace_channel, 1,
1961 "error obtaining st_mtime GMT timestamp: %s", strerror(errno));
1962 }
1963
1964 buflen = strlen(buf);
1965 ptr = buf + buflen;
1966 }
1967
1968 } else {
1969 if (flags & SSH2_FX_ATTR_ACCESSTIME) {
1970 struct tm *tm;
1971
1972 tm = pr_gmtime(p, (const time_t *) &st->st_atime);
1973 if (tm != NULL) {
1974 pr_snprintf(ptr, bufsz - buflen, "access=%04d%02d%02d%02d%02d%02d;",
1975 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min,
1976 tm->tm_sec);
1977
1978 } else {
1979 pr_trace_msg(trace_channel, 1,
1980 "error obtaining st_atime GMT timestamp: %s", strerror(errno));
1981 }
1982
1983 buflen = strlen(buf);
1984 ptr = buf + buflen;
1985 }
1986
1987 if (flags & SSH2_FX_ATTR_MODIFYTIME) {
1988 struct tm *tm;
1989
1990 tm = pr_gmtime(p, (const time_t *) &st->st_mtime);
1991 if (tm != NULL) {
1992 pr_snprintf(ptr, bufsz - buflen, "modify=%04d%02d%02d%02d%02d%02d;",
1993 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min,
1994 tm->tm_sec);
1995
1996 } else {
1997 pr_trace_msg(trace_channel, 1,
1998 "error obtaining st_mtime GMT timestamp: %s", strerror(errno));
1999 }
2000
2001 buflen = strlen(buf);
2002 ptr = buf + buflen;
2003 }
2004
2005 if (flags & SSH2_FX_ATTR_LINK_COUNT) {
2006 pr_snprintf(ptr, bufsz - buflen, "UNIX.nlink=%lu;",
2007 (unsigned long) st->st_nlink);
2008 buflen = strlen(buf);
2009 ptr = buf + buflen;
2010 }
2011 }
2012
2013 return pstrdup(p, buf);
2014 }
2015
2016 static char *fxp_strattrflags(pool *p, uint32_t flags) {
2017 char *str = "";
2018
2019 if (flags & SSH2_FX_ATTR_SIZE) {
2020 str = pstrcat(p, str, *str ? ";" : "", "size", NULL);
2021 }
2022
2023 if ((flags & SSH2_FX_ATTR_UIDGID) ||
2024 (flags & SSH2_FX_ATTR_OWNERGROUP)) {
2025 str = pstrcat(p, str, *str ? ";" : "", "UNIX.owner", NULL);
2026 str = pstrcat(p, str, *str ? ";" : "", "UNIX.group", NULL);
2027 }
2028
2029 if (flags & SSH2_FX_ATTR_PERMISSIONS) {
2030 str = pstrcat(p, str, *str ? ";" : "", "UNIX.mode", NULL);
2031 }
2032
2033 if (fxp_session->client_version <= 3) {
2034 if (flags & SSH2_FX_ATTR_ACMODTIME) {
2035 str = pstrcat(p, str, *str ? ";" : "", "access", NULL);
2036 str = pstrcat(p, str, *str ? ";" : "", "modify", NULL);
2037 }
2038
2039 } else {
2040 if (flags & SSH2_FX_ATTR_ACCESSTIME) {
2041 str = pstrcat(p, str, *str ? ";" : "", "access", NULL);
2042 }
2043
2044 if (flags & SSH2_FX_ATTR_MODIFYTIME) {
2045 str = pstrcat(p, str, *str ? ";" : "", "modify", NULL);
2046 }
2047
2048 if (flags & SSH2_FX_ATTR_OWNERGROUP) {
2049 str = pstrcat(p, str, *str ? ";" : "", "UNIX.owner;UNIX.group", NULL);
2050 }
2051
2052 if (flags & SSH2_FX_ATTR_LINK_COUNT) {
2053 str = pstrcat(p, str, *str ? ";" : "", "UNIX.nlink", NULL);
2054 }
2055
2056 /* Ignored/unimplemented */
2057 if (flags & SSH2_FX_ATTR_ALLOCATION_SIZE) {
2058 str = pstrcat(p, str, *str ? ";" : "", "allocation-size", NULL);
2059 }
2060
2061 if (flags & SSH2_FX_ATTR_SUBSECOND_TIMES) {
2062 str = pstrcat(p, str, *str ? ";" : "", "subsecond", NULL);
2063 }
2064
2065 if (flags & SSH2_FX_ATTR_CREATETIME) {
2066 str = pstrcat(p, str, *str ? ";" : "", "create", NULL);
2067 }
2068
2069 if (flags & SSH2_FX_ATTR_ACL) {
2070 str = pstrcat(p, str, *str ? ";" : "", "acl", NULL);
2071 }
2072
2073 if (flags & SSH2_FX_ATTR_BITS) {
2074 str = pstrcat(p, str, *str ? ";" : "", "bits", NULL);
2075 }
2076
2077 if (flags & SSH2_FX_ATTR_TEXT_HINT) {
2078 str = pstrcat(p, str, *str ? ";" : "", "text-hint", NULL);
2079 }
2080
2081 if (flags & SSH2_FX_ATTR_MIME_TYPE) {
2082 str = pstrcat(p, str, *str ? ";" : "", "mime-type", NULL);
2083 }
2084
2085 if (flags & SSH2_FX_ATTR_UNTRANSLATED_NAME) {
2086 str = pstrcat(p, str, *str ? ";" : "", "untranslated-name", NULL);
2087 }
2088
2089 if (flags & SSH2_FX_ATTR_CTIME) {
2090 str = pstrcat(p, str, *str ? ";" : "", "ctime", NULL);
2091 }
2092 }
2093
2094 return str;
2095 }
2096
2097 static char *fxp_stroflags(pool *p, int flags) {
2098 char *str = "";
2099
2100 if (flags == O_RDONLY) {
2101 str = pstrcat(p, str, "O_RDONLY", NULL);
2102
2103 } else if (flags & O_RDWR) {
2104 str = pstrcat(p, str, "O_RDWR", NULL);
2105
2106 } else if (flags & O_WRONLY) {
2107 str = pstrcat(p, str, "O_WRONLY", NULL);
2108 }
2109
2110 #ifdef O_APPEND
2111 if (flags & O_APPEND) {
2112 str = pstrcat(p, str, *str ? "|" : "", "O_APPEND", NULL);
2113 }
2114 #endif
2115
2116 if (flags & O_CREAT) {
2117 str = pstrcat(p, str, *str ? "|" : "", "O_CREAT", NULL);
2118 }
2119
2120 if (flags & O_TRUNC) {
2121 str = pstrcat(p, str, *str ? "|" : "", "O_TRUNC", NULL);
2122 }
2123
2124 if (flags & O_EXCL) {
2125 str = pstrcat(p, str, *str ? "|" : "", "O_EXCL", NULL);
2126 }
2127
2128 return str;
2129 }
2130
2131 static array_header *fxp_xattrs_read(pool *p, unsigned char **buf,
2132 uint32_t *buflen) {
2133 register unsigned int i;
2134 uint32_t extpair_count;
2135 array_header *xattrs = NULL;
2136
2137 extpair_count = sftp_msg_read_int(p, buf, buflen);
2138 pr_trace_msg(trace_channel, 15,
2139 "protocol version %lu: read EXTENDED attribute: %lu extensions",
2140 (unsigned long) fxp_session->client_version,
2141 (unsigned long) extpair_count);
2142
2143 if (extpair_count > FXP_MAX_EXTENDED_ATTRIBUTES) {
2144 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2145 "received too many EXTENDED attributes (%lu > max %lu), "
2146 "truncating to max", (unsigned long) extpair_count,
2147 (unsigned long) FXP_MAX_EXTENDED_ATTRIBUTES);
2148 extpair_count = FXP_MAX_EXTENDED_ATTRIBUTES;
2149 }
2150
2151 xattrs = make_array(p, 1, sizeof(struct fxp_extpair *));
2152
2153 for (i = 0; i < extpair_count; i++) {
2154 struct fxp_extpair *ext;
2155
2156 ext = fxp_msg_read_extpair(p, buf, buflen);
2157 if (ext != NULL) {
2158 pr_trace_msg(trace_channel, 15,
2159 "protocol version %lu: read EXTENDED attribute: "
2160 "extension '%s' (%lu bytes of data)",
2161 (unsigned long) fxp_session->client_version, ext->ext_name,
2162 (unsigned long) ext->ext_datalen);
2163
2164 *((struct fxp_extpair **) push_array(xattrs)) = ext;
2165 }
2166 }
2167
2168 return xattrs;
2169 }
2170
2171 static struct stat *fxp_attrs_read(struct fxp_packet *fxp, unsigned char **buf,
2172 uint32_t *buflen, uint32_t *flags, array_header **xattrs) {
2173 struct stat *st;
2174
2175 st = pcalloc(fxp->pool, sizeof(struct stat));
2176
2177 *flags = sftp_msg_read_int(fxp->pool, buf, buflen);
2178
2179 if (fxp_session->client_version <= 3) {
2180 if (*flags & SSH2_FX_ATTR_SIZE) {
2181 st->st_size = sftp_msg_read_long(fxp->pool, buf, buflen);
2182 }
2183
2184 if (*flags & SSH2_FX_ATTR_UIDGID) {
2185 st->st_uid = sftp_msg_read_int(fxp->pool, buf, buflen);
2186 st->st_gid = sftp_msg_read_int(fxp->pool, buf, buflen);
2187 }
2188
2189 if (*flags & SSH2_FX_ATTR_PERMISSIONS) {
2190 st->st_mode |= sftp_msg_read_int(fxp->pool, buf, buflen);
2191 }
2192
2193 if (*flags & SSH2_FX_ATTR_ACMODTIME) {
2194 st->st_atime = sftp_msg_read_int(fxp->pool, buf, buflen);
2195 st->st_mtime = sftp_msg_read_int(fxp->pool, buf, buflen);
2196 }
2197
2198 } else {
2199 char file_type;
2200
2201 /* XXX Use this to create different types of files? E.g. what if the client
2202 * wants to OPEN a socket, or a fifo?
2203 */
2204 file_type = sftp_msg_read_byte(fxp->pool, buf, buflen);
2205 switch (file_type) {
2206 case SSH2_FX_ATTR_FTYPE_REGULAR:
2207 st->st_mode |= S_IFREG;
2208 break;
2209
2210 case SSH2_FX_ATTR_FTYPE_DIRECTORY:
2211 st->st_mode |= S_IFDIR;
2212 break;
2213
2214 case SSH2_FX_ATTR_FTYPE_SYMLINK:
2215 st->st_mode |= S_IFLNK;
2216 break;
2217
2218 case SSH2_FX_ATTR_FTYPE_SPECIAL:
2219 /* Default to marking this as a character special device, rather than
2220 * block special.
2221 */
2222 st->st_mode |= S_IFCHR;
2223 break;
2224
2225 case SSH2_FX_ATTR_FTYPE_UNKNOWN:
2226 /* Nothing to do here; leave st->st_mode alone. */
2227 break;
2228
2229 case SSH2_FX_ATTR_FTYPE_SOCKET:
2230 st->st_mode |= S_IFSOCK;
2231 break;
2232
2233 case SSH2_FX_ATTR_FTYPE_CHAR_DEVICE:
2234 st->st_mode |= S_IFCHR;
2235 break;
2236
2237 case SSH2_FX_ATTR_FTYPE_BLOCK_DEVICE:
2238 st->st_mode |= S_IFBLK;
2239 break;
2240
2241 #ifdef S_IFIFO
2242 case SSH2_FX_ATTR_FTYPE_FIFO:
2243 st->st_mode |= S_IFIFO;
2244 break;
2245 #endif /* S_IFIFO */
2246
2247 default:
2248 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2249 "unrecognized file type %d requested (protocol version %d)",
2250 file_type, fxp_session->client_version);
2251 }
2252
2253 if (*flags & SSH2_FX_ATTR_SIZE) {
2254 st->st_size = sftp_msg_read_long(fxp->pool, buf, buflen);
2255 }
2256
2257 if (*flags & SSH2_FX_ATTR_ALLOCATION_SIZE) {
2258 /* Read (and ignore) any allocation size attribute. */
2259 uint64_t allosz;
2260
2261 allosz = sftp_msg_read_long(fxp->pool, buf, buflen);
2262 pr_trace_msg(trace_channel, 15,
2263 "protocol version %lu: read ALLOCATION_SIZE attribute: %" PR_LU,
2264 (unsigned long) fxp_session->client_version, (pr_off_t) allosz);
2265 }
2266
2267 if (*flags & SSH2_FX_ATTR_OWNERGROUP) {
2268 char *name;
2269 uid_t uid;
2270 gid_t gid;
2271
2272 name = sftp_msg_read_string(fxp->pool, buf, buflen);
2273 uid = pr_auth_name2uid(fxp->pool, name);
2274 if (uid == (uid_t) -1) {
2275 unsigned char *buf2, *ptr2;
2276 uint32_t buflen2, bufsz2, status_code;
2277 struct fxp_packet *resp;
2278
2279 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2280 "unable to translate user name '%s' to UID, UNKNOWN_PRINCIPAL error",
2281 name);
2282
2283 buflen2 = bufsz2 = FXP_RESPONSE_DATA_DEFAULT_SZ;
2284 buf2 = ptr2 = palloc(fxp->pool, bufsz2);
2285
2286 status_code = SSH2_FX_UNKNOWN_PRINCIPAL;
2287
2288 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
2289 (unsigned long) status_code, fxp_strerror(status_code));
2290
2291 fxp_status_write(fxp->pool, &buf2, &buflen2, fxp->request_id,
2292 status_code, fxp_strerror(status_code), name);
2293
2294 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
2295 resp->payload = ptr2;
2296 resp->payload_sz = (bufsz2 - buflen2);
2297
2298 if (fxp_packet_write(resp) < 0) {
2299 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2300 "error sending UNKNOWN_PRINCIPAL status: %s", strerror(errno));
2301 }
2302
2303 return NULL;
2304 }
2305
2306 st->st_uid = uid;
2307
2308 name = sftp_msg_read_string(fxp->pool, buf, buflen);
2309 gid = pr_auth_name2gid(fxp->pool, name);
2310 if (gid == (gid_t) -1) {
2311 unsigned char *buf2, *ptr2;
2312 uint32_t buflen2, bufsz2, status_code;
2313 struct fxp_packet *resp;
2314
2315 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2316 "unable to translate group name '%s' to GID, UNKNOWN_PRINCIPAL error",
2317 name);
2318
2319 buflen2 = bufsz2 = FXP_RESPONSE_DATA_DEFAULT_SZ;
2320 buf2 = ptr2 = palloc(fxp->pool, bufsz2);
2321
2322 status_code = SSH2_FX_UNKNOWN_PRINCIPAL;
2323
2324 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
2325 (unsigned long) status_code, fxp_strerror(status_code));
2326
2327 fxp_status_write(fxp->pool, &buf2, &buflen2, fxp->request_id,
2328 status_code, fxp_strerror(status_code), name);
2329
2330 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
2331 resp->payload = ptr2;
2332 resp->payload_sz = (bufsz2 - buflen2);
2333
2334 if (fxp_packet_write(resp) < 0) {
2335 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2336 "error sending UNKNOWN_PRINCIPAL status: %s", strerror(errno));
2337 }
2338
2339 return NULL;
2340 }
2341
2342 st->st_gid = gid;
2343 }
2344
2345 if (*flags & SSH2_FX_ATTR_PERMISSIONS) {
2346 st->st_mode |= sftp_msg_read_int(fxp->pool, buf, buflen);
2347 }
2348
2349 if (*flags & SSH2_FX_ATTR_ACCESSTIME) {
2350 st->st_atime = sftp_msg_read_long(fxp->pool, buf, buflen);
2351
2352 if (*flags & SSH2_FX_ATTR_SUBSECOND_TIMES) {
2353 /* Read (and ignore) the nanoseconds field. */
2354 uint32_t nanosecs;
2355
2356 nanosecs = sftp_msg_read_int(fxp->pool, buf, buflen);
2357 pr_trace_msg(trace_channel, 15,
2358 "protocol version %lu: read ACCESSTIME SUBSECOND attribute: %lu",
2359 (unsigned long) fxp_session->client_version,
2360 (unsigned long) nanosecs);
2361 }
2362 }
2363
2364 if (*flags & SSH2_FX_ATTR_CREATETIME) {
2365 /* Read (and ignore) the create time attribute. */
2366 uint64_t create_time;
2367
2368 create_time = sftp_msg_read_long(fxp->pool, buf, buflen);
2369 pr_trace_msg(trace_channel, 15,
2370 "protocol version %lu: read CREATETIME attribute: %" PR_LU,
2371 (unsigned long) fxp_session->client_version, (pr_off_t) create_time);
2372
2373 if (*flags & SSH2_FX_ATTR_SUBSECOND_TIMES) {
2374 /* Read (and ignore) the nanoseconds field. */
2375 uint32_t nanosecs;
2376
2377 nanosecs = sftp_msg_read_int(fxp->pool, buf, buflen);
2378 pr_trace_msg(trace_channel, 15,
2379 "protocol version %lu: read CREATETIME SUBSECOND attribute: %lu",
2380 (unsigned long) fxp_session->client_version,
2381 (unsigned long) nanosecs);
2382 }
2383 }
2384
2385 if (*flags & SSH2_FX_ATTR_MODIFYTIME) {
2386 st->st_mtime = sftp_msg_read_long(fxp->pool, buf, buflen);
2387
2388 if (*flags & SSH2_FX_ATTR_SUBSECOND_TIMES) {
2389 /* Read (and ignore) the nanoseconds field. */
2390 uint32_t nanosecs;
2391
2392 nanosecs = sftp_msg_read_int(fxp->pool, buf, buflen);
2393 pr_trace_msg(trace_channel, 15,
2394 "protocol version %lu: read MOTIFYTIME SUBSECOND attribute: %lu",
2395 (unsigned long) fxp_session->client_version,
2396 (unsigned long) nanosecs);
2397 }
2398 }
2399
2400 if (*flags & SSH2_FX_ATTR_CTIME) {
2401 /* Read (and ignore) the ctime attribute. */
2402 uint64_t change_time;
2403
2404 change_time = sftp_msg_read_long(fxp->pool, buf, buflen);
2405 pr_trace_msg(trace_channel, 15,
2406 "protocol version %lu: read CTIME attribute: %" PR_LU,
2407 (unsigned long) fxp_session->client_version, (pr_off_t) change_time);
2408
2409 if (*flags & SSH2_FX_ATTR_SUBSECOND_TIMES) {
2410 /* Read (and ignore) the nanoseconds field. */
2411 uint32_t nanosecs;
2412
2413 nanosecs = sftp_msg_read_int(fxp->pool, buf, buflen);
2414 pr_trace_msg(trace_channel, 15,
2415 "protocol version %lu: read CTIME SUBSECOND attribute: %lu",
2416 (unsigned long) fxp_session->client_version,
2417 (unsigned long) nanosecs);
2418 }
2419 }
2420
2421 if (*flags & SSH2_FX_ATTR_ACL) {
2422 /* Read (and ignore) the ACL attribute. */
2423 char *acl;
2424
2425 acl = sftp_msg_read_string(fxp->pool, buf, buflen);
2426 pr_trace_msg(trace_channel, 15,
2427 "protocol version %lu: read ACL attribute: '%s'",
2428 (unsigned long) fxp_session->client_version, acl ? acl : "(nil)");
2429 }
2430
2431 if (*flags & SSH2_FX_ATTR_BITS) {
2432 /* Read (and ignore) the BITS attributes. */
2433 uint32_t attr_bits, attr_valid_bits;
2434
2435 attr_bits = sftp_msg_read_int(fxp->pool, buf, buflen);
2436 attr_valid_bits = sftp_msg_read_int(fxp->pool, buf, buflen);
2437
2438 fxp_trace_v5_bit_flags(fxp->pool, attr_bits, attr_valid_bits);
2439 }
2440
2441 if (*flags & SSH2_FX_ATTR_TEXT_HINT) {
2442 /* Read (and ignore) the TEXT_HINT attribute. */
2443 char hint, *hint_type;
2444
2445 hint = sftp_msg_read_byte(fxp->pool, buf, buflen);
2446 switch (hint) {
2447 case SSH2_FX_ATTR_KNOWN_TEXT:
2448 hint_type = "KNOWN_TEXT";
2449 break;
2450
2451 case SSH2_FX_ATTR_GUESSED_TEXT:
2452 hint_type = "GUESSED_TEXT";
2453 break;
2454
2455 case SSH2_FX_ATTR_KNOWN_BINARY:
2456 hint_type = "KNOWN_BINARY";
2457 break;
2458
2459 case SSH2_FX_ATTR_GUESSED_BINARY:
2460 hint_type = "GUESSED_BINARY";
2461 break;
2462
2463 default:
2464 hint_type = "(unknown)";
2465 break;
2466 }
2467
2468 pr_trace_msg(trace_channel, 15,
2469 "protocol version %lu: read TEXT_HINT attribute: '%s'",
2470 (unsigned long) fxp_session->client_version, hint_type);
2471 }
2472
2473 if (*flags & SSH2_FX_ATTR_MIME_TYPE) {
2474 /* Read (and ignore) the MIME_TYPE attribute. */
2475 char *mime_type;
2476
2477 mime_type = sftp_msg_read_string(fxp->pool, buf, buflen);
2478 pr_trace_msg(trace_channel, 15,
2479 "protocol version %lu: read MIME_TYPE attribute: '%s'",
2480 (unsigned long) fxp_session->client_version,
2481 mime_type ? mime_type : "(nil)");
2482 }
2483
2484 if (*flags & SSH2_FX_ATTR_LINK_COUNT) {
2485 uint32_t link_count;
2486
2487 link_count = sftp_msg_read_int(fxp->pool, buf, buflen);
2488 pr_trace_msg(trace_channel, 15,
2489 "protocol version %lu: read LINK_COUNT attribute: %lu",
2490 (unsigned long) fxp_session->client_version,
2491 (unsigned long) link_count);
2492 }
2493
2494 if (*flags & SSH2_FX_ATTR_UNTRANSLATED_NAME) {
2495 /* Read (and ignore) the UNTRANSLATED_NAME attribute. */
2496 char *untranslated;
2497
2498 untranslated = sftp_msg_read_string(fxp->pool, buf, buflen);
2499 pr_trace_msg(trace_channel, 15,
2500 "protocol version %lu: read UNTRANSLATED_NAME attribute: '%s'",
2501 (unsigned long) fxp_session->client_version,
2502 untranslated ? untranslated : "(nil)");
2503 }
2504 }
2505
2506 if (*flags & SSH2_FX_ATTR_EXTENDED) {
2507 array_header *ext_attrs;
2508
2509 /* Read the EXTENDED attribute. */
2510 ext_attrs = fxp_xattrs_read(fxp->pool, buf, buflen);
2511 if (xattrs != NULL) {
2512 *xattrs = ext_attrs;
2513 }
2514 }
2515
2516 return st;
2517 }
2518
2519 static char fxp_get_file_type(mode_t mode) {
2520 if (S_ISREG(mode)) {
2521 return SSH2_FX_ATTR_FTYPE_REGULAR;
2522 }
2523
2524 if (S_ISDIR(mode)) {
2525 return SSH2_FX_ATTR_FTYPE_DIRECTORY;
2526 }
2527
2528 if (S_ISLNK(mode)) {
2529 return SSH2_FX_ATTR_FTYPE_SYMLINK;
2530 }
2531
2532 if (S_ISSOCK(mode)) {
2533 if (fxp_session->client_version <= 4) {
2534 return SSH2_FX_ATTR_FTYPE_SPECIAL;
2535 }
2536
2537 return SSH2_FX_ATTR_FTYPE_SOCKET;
2538 }
2539
2540 #ifdef S_ISFIFO
2541 if (S_ISFIFO(mode)) {
2542 if (fxp_session->client_version <= 4) {
2543 return SSH2_FX_ATTR_FTYPE_SPECIAL;
2544 }
2545
2546 return SSH2_FX_ATTR_FTYPE_FIFO;
2547 }
2548 #endif
2549
2550 #ifdef S_ISCHR
2551 if (S_ISCHR(mode)) {
2552 if (fxp_session->client_version <= 4) {
2553 return SSH2_FX_ATTR_FTYPE_SPECIAL;
2554 }
2555
2556 return SSH2_FX_ATTR_FTYPE_CHAR_DEVICE;
2557 }
2558 #endif
2559
2560 #ifdef S_ISBLK
2561 if (S_ISBLK(mode)) {
2562 if (fxp_session->client_version <= 4) {
2563 return SSH2_FX_ATTR_FTYPE_SPECIAL;
2564 }
2565
2566 return SSH2_FX_ATTR_FTYPE_BLOCK_DEVICE;
2567 }
2568 #endif
2569
2570 return SSH2_FX_ATTR_FTYPE_UNKNOWN;
2571 }
2572
2573 static uint32_t fxp_xattrs_write(pool *p, struct fxp_buffer *fxb,
2574 const char *path) {
2575 uint32_t len = 0;
2576
2577 #if defined(PR_USE_XATTR)
2578 int res;
2579 array_header *names = NULL;
2580
2581 res = pr_fsio_llistxattr(p, path, &names);
2582 if (res > 0) {
2583 register unsigned int i;
2584 pool *sub_pool;
2585 uint32_t xattrsz = 0;
2586 array_header *vals;
2587
2588 sub_pool = make_sub_pool(p);
2589 pr_pool_tag(sub_pool, "listxattr pool");
2590
2591 vals = make_array(sub_pool, names->nelts, sizeof(pr_buffer_t *));
2592 xattrsz = sizeof(uint32_t);
2593
2594 for (i = 0; i < names->nelts; i++) {
2595 const char *name;
2596 pr_buffer_t *val;
2597 ssize_t valsz;
2598
2599 name = ((const char **) names->elts)[i];
2600 xattrsz += (sizeof(uint32_t) + strlen(name));
2601
2602 val = pcalloc(sub_pool, sizeof(pr_buffer_t));
2603
2604 valsz = pr_fsio_lgetxattr(p, path, name, NULL, 0);
2605 if (valsz > 0) {
2606 xattrsz += (sizeof(uint32_t) + valsz);
2607
2608 val->buflen = valsz;
2609 val->buf = palloc(sub_pool, valsz);
2610
2611 valsz = pr_fsio_lgetxattr(p, path, name, val->buf, valsz);
2612 if (valsz > 0) {
2613 *((pr_buffer_t **) push_array(vals)) = val;
2614 }
2615
2616 } else {
2617 /* Push the empty buffer into the list, so that the vals list
2618 * lines up with the names list.
2619 */
2620 *((pr_buffer_t **) push_array(vals)) = val;
2621 }
2622 }
2623
2624 if (fxb->buflen < xattrsz) {
2625 unsigned char *ptr;
2626 uint32_t bufsz, resp_len;
2627
2628 resp_len = fxb->bufsz - fxb->buflen;
2629
2630 /* Allocate a buffer large enough for the xattrs */
2631 pr_trace_msg(trace_channel, 3,
2632 "allocating larger response buffer (have %lu bytes, need %lu bytes)",
2633 (unsigned long) fxb->bufsz, (unsigned long) fxb->bufsz + xattrsz);
2634
2635 bufsz = fxb->bufsz + xattrsz;
2636 ptr = palloc(p, bufsz);
2637
2638 /* Copy over our existing response data into the new buffer. */
2639 memcpy(ptr, fxb->ptr, resp_len);
2640 fxb->ptr = ptr;
2641 fxb->bufsz = bufsz;
2642 fxb->buf = ptr + resp_len;
2643 fxb->buflen = bufsz - resp_len;
2644 }
2645
2646 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), names->nelts);
2647 for (i = 0; i < names->nelts; i++) {
2648 const char *name;
2649 pr_buffer_t *val;
2650
2651 name = ((const char **) names->elts)[i];
2652 val = ((pr_buffer_t **) vals->elts)[i];
2653
2654 len += sftp_msg_write_string(&(fxb->buf), &(fxb->buflen), name);
2655 len += sftp_msg_write_data(&(fxb->buf), &(fxb->buflen),
2656 (const unsigned char *) val->buf, (size_t) val->buflen, TRUE);
2657 }
2658
2659 destroy_pool(sub_pool);
2660
2661 } else {
2662 /* Have to write an extended count of zero. */
2663 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), 0);
2664 }
2665 #endif /* PR_USE_XATTR */
2666
2667 return len;
2668 }
2669
2670 static uint32_t fxp_attrs_write(pool *p, struct fxp_buffer *fxb,
2671 const char *path, struct stat *st, uint32_t flags,
2672 const char *user_owner, const char *group_owner) {
2673 uint32_t len = 0;
2674 mode_t perms;
2675
2676 if (fxp_session->client_version <= 3) {
2677 perms = st->st_mode;
2678
2679 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), flags);
2680
2681 if (flags & SSH2_FX_ATTR_SIZE) {
2682 len += sftp_msg_write_long(&(fxb->buf), &(fxb->buflen), st->st_size);
2683 }
2684
2685 if (flags & SSH2_FX_ATTR_UIDGID) {
2686 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), st->st_uid);
2687 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), st->st_gid);
2688 }
2689
2690 if (flags & SSH2_FX_ATTR_PERMISSIONS) {
2691 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), perms);
2692 }
2693
2694 if (flags & SSH2_FX_ATTR_ACMODTIME) {
2695 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), st->st_atime);
2696 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), st->st_mtime);
2697 }
2698
2699 if (flags & SSH2_FX_ATTR_EXTENDED) {
2700 len += fxp_xattrs_write(p, fxb, path);
2701 }
2702
2703 } else {
2704 char file_type;
2705
2706 perms = st->st_mode;
2707
2708 /* Make sure that we do not include the file type bits when sending the
2709 * permission bits of the st_mode field.
2710 */
2711 perms &= ~S_IFMT;
2712
2713 file_type = fxp_get_file_type(st->st_mode);
2714
2715 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), flags);
2716 len += sftp_msg_write_byte(&(fxb->buf), &(fxb->buflen), file_type);
2717
2718 if (flags & SSH2_FX_ATTR_SIZE) {
2719 len += sftp_msg_write_long(&(fxb->buf), &(fxb->buflen), st->st_size);
2720 }
2721
2722 if (flags & SSH2_FX_ATTR_OWNERGROUP) {
2723 const char *user_name, *group_name;
2724
2725 if (user_owner == NULL) {
2726 user_name = pr_auth_uid2name(p, st->st_uid);
2727
2728 } else {
2729 user_name = user_owner;
2730 }
2731
2732 if (group_owner == NULL) {
2733 group_name = pr_auth_gid2name(p, st->st_gid);
2734
2735 } else {
2736 group_name = group_owner;
2737 }
2738
2739 len += sftp_msg_write_string(&(fxb->buf), &(fxb->buflen), user_name);
2740 len += sftp_msg_write_string(&(fxb->buf), &(fxb->buflen), group_name);
2741 }
2742
2743 if (flags & SSH2_FX_ATTR_PERMISSIONS) {
2744 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), perms);
2745 }
2746
2747 if (flags & SSH2_FX_ATTR_ACCESSTIME) {
2748 len += sftp_msg_write_long(&(fxb->buf), &(fxb->buflen), st->st_atime);
2749 }
2750
2751 if (flags & SSH2_FX_ATTR_MODIFYTIME) {
2752 len += sftp_msg_write_long(&(fxb->buf), &(fxb->buflen), st->st_mtime);
2753 }
2754
2755 if (flags & SSH2_FX_ATTR_LINK_COUNT) {
2756 len += sftp_msg_write_int(&(fxb->buf), &(fxb->buflen), st->st_nlink);
2757 }
2758
2759 if (flags & SSH2_FX_ATTR_EXTENDED) {
2760 len += fxp_xattrs_write(p, fxb, path);
2761 }
2762 }
2763
2764 return len;
2765 }
2766
2767 /* The strmode(3) function appears in some BSDs, but is not portable. */
2768 static char *fxp_strmode(pool *p, mode_t mode) {
2769 char mode_str[12];
2770
2771 memset(mode_str, '\0', sizeof(mode_str));
2772 sstrncpy(mode_str, "?--------- ", sizeof(mode_str));
2773
2774 switch (mode & S_IFMT) {
2775 case S_IFREG:
2776 mode_str[0] = '-';
2777 break;
2778
2779 case S_IFDIR:
2780 mode_str[0] = 'd';
2781 break;
2782
2783 case S_IFLNK:
2784 mode_str[0] = 'l';
2785 break;
2786
2787 #ifdef S_IFSOCK
2788 case S_IFSOCK:
2789 mode_str[0] = 's';
2790 break;
2791 #endif
2792
2793 case S_IFIFO:
2794 mode_str[0] = 'p';
2795 break;
2796
2797 case S_IFBLK:
2798 mode_str[0] = 'b';
2799 break;
2800
2801 case S_IFCHR:
2802 mode_str[0] = 'c';
2803 break;
2804 }
2805
2806 if (mode_str[0] != '?') {
2807 /* User perms */
2808 mode_str[1] = (mode & S_IRUSR) ? 'r' : '-';
2809 mode_str[2] = (mode & S_IWUSR) ? 'w' : '-';
2810 mode_str[3] = (mode & S_IXUSR) ?
2811 ((mode & S_ISUID) ? 's' : 'x') : ((mode & S_ISUID) ? 'S' : '-');
2812
2813 /* Group perms */
2814 mode_str[4] = (mode & S_IRGRP) ? 'r' : '-';
2815 mode_str[5] = (mode & S_IWGRP) ? 'w' : '-';
2816 mode_str[6] = (mode & S_IXGRP) ?
2817 ((mode & S_ISGID) ? 's' : 'x') : ((mode & S_ISGID) ? 'S' : '-');
2818
2819 /* World perms */
2820 mode_str[7] = (mode & S_IROTH) ? 'r' : '-';
2821 mode_str[8] = (mode & S_IWOTH) ? 'w' : '-';
2822 mode_str[9] = (mode & S_IXOTH) ?
2823 ((mode & S_ISVTX) ? 't' : 'x') : ((mode & S_ISVTX) ? 'T' : '-');
2824 }
2825
2826 return pstrdup(p, mode_str);
2827 }
2828
2829 static char *fxp_get_path_listing(pool *p, const char *path, struct stat *st,
2830 const char *user_owner, const char *group_owner) {
2831 const char *user, *group;
2832 char listing[1024], *mode_str, time_str[64];
2833 struct tm *t;
2834 int user_len, group_len;
2835 size_t time_strlen;
2836 time_t now = time(NULL);
2837
2838 memset(listing, '\0', sizeof(listing));
2839 memset(time_str, '\0', sizeof(time_str));
2840
2841 mode_str = fxp_strmode(p, st->st_mode);
2842
2843 if (fxp_use_gmt) {
2844 t = pr_gmtime(p, (const time_t *) &st->st_mtime);
2845
2846 } else {
2847 t = pr_localtime(p, (const time_t *) &st->st_mtime);
2848 }
2849
2850 /* Use strftime(3) to format the time entry for us. Seems some SFTP clients
2851 * are *very* particular about this formatting. Understandable, since
2852 * the SFTP Drafts for protocol version 3 did not actually define a format;
2853 * now most clients conform to the format used by OpenSSH.
2854 */
2855
2856 if ((now - st->st_mtime) > (180 * 24 * 60 * 60)) {
2857 time_strlen = strftime(time_str, sizeof(time_str), "%b %e %Y", t);
2858
2859 } else {
2860 time_strlen = strftime(time_str, sizeof(time_str), "%b %e %H:%M", t);
2861 }
2862
2863 if (time_strlen == 0) {
2864 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "%s",
2865 "warning: strftime(3) returned 0");
2866 }
2867
2868 if (user_owner == NULL) {
2869 user = pr_auth_uid2name(p, st->st_uid);
2870
2871 } else {
2872 user = user_owner;
2873 }
2874
2875 user_len = MAX(strlen(user), 8);
2876
2877 if (group_owner == NULL) {
2878 group = pr_auth_gid2name(p, st->st_gid);
2879
2880 } else {
2881 group = group_owner;
2882 }
2883
2884 group_len = MAX(strlen(group), 8);
2885
2886 pr_snprintf(listing, sizeof(listing)-1,
2887 "%s %3u %-*s %-*s %8" PR_LU " %s %s", mode_str,
2888 (unsigned int) st->st_nlink, user_len, user, group_len, group,
2889 (pr_off_t) st->st_size, time_str, path);
2890 return pstrdup(p, listing);
2891 }
2892
2893 static struct fxp_dirent *fxp_get_dirent(pool *p, cmd_rec *cmd,
2894 const char *real_path, mode_t *fake_mode) {
2895 struct fxp_dirent *fxd;
2896 struct stat st;
2897 int hidden = 0, res;
2898
2899 pr_fs_clear_cache2(real_path);
2900 if (pr_fsio_lstat(real_path, &st) < 0) {
2901 return NULL;
2902 }
2903
2904 res = dir_check(p, cmd, G_DIRS, real_path, &hidden);
2905 if (res == 0 ||
2906 hidden == TRUE) {
2907 errno = EACCES;
2908 return NULL;
2909 }
2910
2911 if (fake_mode != NULL) {
2912 mode_t mode;
2913
2914 mode = *fake_mode;
2915 mode |= (st.st_mode & S_IFMT);
2916
2917 if (S_ISDIR(st.st_mode)) {
2918 if (st.st_mode & S_IROTH) {
2919 mode |= S_IXOTH;
2920 }
2921
2922 if (st.st_mode & S_IRGRP) {
2923 mode |= S_IXGRP;
2924 }
2925
2926 if (st.st_mode & S_IRUSR) {
2927 mode |= S_IXUSR;
2928 }
2929 }
2930
2931 st.st_mode = mode;
2932 }
2933
2934 fxd = pcalloc(p, sizeof(struct fxp_dirent));
2935 fxd->real_path = real_path;
2936 fxd->st = pcalloc(p, sizeof(struct stat));
2937 memcpy(fxd->st, &st, sizeof(struct stat));
2938
2939 return fxd;
2940 }
2941
2942 static uint32_t fxp_name_write(pool *p, struct fxp_buffer *fxb,
2943 const char *path, struct stat *st, uint32_t attr_flags,
2944 const char *user_owner, const char *group_owner) {
2945 uint32_t len = 0;
2946 const char *encoded_path;
2947
2948 encoded_path = path;
2949 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
2950 encoded_path = sftp_utf8_encode_str(p, encoded_path);
2951 }
2952
2953 len += sftp_msg_write_string(&(fxb->buf), &(fxb->buflen), encoded_path);
2954
2955 if (fxp_session->client_version <= 3) {
2956 char *path_desc;
2957
2958 path_desc = fxp_get_path_listing(p, path, st, user_owner, group_owner);
2959 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
2960 path_desc = sftp_utf8_encode_str(p, path_desc);
2961 }
2962
2963 len += sftp_msg_write_string(&(fxb->buf), &(fxb->buflen), path_desc);
2964 }
2965
2966 len += fxp_attrs_write(p, fxb, path, st, attr_flags, user_owner, group_owner);
2967 return len;
2968 }
2969
2970 /* FX Handle Mgmt */
2971
2972 static int fxp_handle_add(uint32_t channel_id, struct fxp_handle *fxh) {
2973 int res;
2974
2975 if (fxp_session->handle_tab == NULL) {
2976 fxp_session->handle_tab = pr_table_alloc(fxp_session->pool, 0);
2977 }
2978
2979 res = pr_table_add(fxp_session->handle_tab, fxh->name, fxh, sizeof(void *));
2980 if (res < 0) {
2981 if (errno != EEXIST) {
2982 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2983 "error stashing handle: %s", strerror(errno));
2984 }
2985 }
2986
2987 return res;
2988 }
2989
2990 static struct fxp_handle *fxp_handle_create(pool *p) {
2991 unsigned char *data;
2992 char *handle;
2993 size_t data_len;
2994 pool *sub_pool;
2995 struct fxp_handle *fxh;
2996
2997 sub_pool = make_sub_pool(p);
2998 pr_pool_tag(sub_pool, "SFTP file handle pool");
2999 fxh = pcalloc(sub_pool, sizeof(struct fxp_handle));
3000 fxh->pool = sub_pool;
3001
3002 /* Use 8 random bytes for our handle, which means 16 bytes as hex-encoded
3003 * characters.
3004 */
3005 data_len = 8;
3006 data = palloc(p, data_len);
3007
3008 while (TRUE) {
3009 /* Keep trying until mktemp(3) returns a string that we haven't used
3010 * yet. We need to avoid collisions.
3011 */
3012 pr_signals_handle();
3013
3014 RAND_bytes(data, data_len);
3015
3016 /* Encode the data as hex to create the handle ID. */
3017 handle = pr_str_bin2hex(fxh->pool, data, data_len, PR_STR_FL_HEX_USE_LC);
3018
3019 if (fxp_handle_get(handle) == NULL) {
3020 fxh->name = handle;
3021 fxh->fh_st = pcalloc(fxh->pool, sizeof(struct stat));
3022 break;
3023 }
3024
3025 pr_trace_msg(trace_channel, 4,
3026 "handle '%s' already used, generating another", handle);
3027 }
3028
3029 return fxh;
3030 }
3031
3032 /* NOTE: this function is ONLY called when the session is closed, for
3033 * "aborting" any file handles still left open by the client.
3034 */
3035 static int fxp_handle_abort(const void *key_data, size_t key_datasz,
3036 const void *value_data, size_t value_datasz, void *user_data) {
3037 struct fxp_handle *fxh;
3038 const char *real_path = NULL;
3039 char *abs_path, *curr_path = NULL;
3040 char direction;
3041 unsigned char *delete_aborted_stores = NULL;
3042 cmd_rec *cmd = NULL;
3043
3044 fxh = (struct fxp_handle *) value_data;
3045 delete_aborted_stores = user_data;
3046
3047 /* Is this a file or a directory handle? */
3048 if (fxh->dirh != NULL) {
3049 cmd = fxp_cmd_alloc(fxh->pool, C_MLSD, (char *) fxh->dir);
3050 cmd->cmd_class = CL_DIRS;
3051 cmd->cmd_id = pr_cmd_get_id(C_MLSD);
3052
3053 if (pr_fsio_closedir(fxh->dirh) < 0) {
3054 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3055 "error closing aborted directory '%s': %s", fxh->dir, strerror(errno));
3056 }
3057
3058 pr_response_clear(&resp_list);
3059 pr_response_clear(&resp_err_list);
3060 fxp_cmd_dispatch_err(cmd);
3061
3062 fxh->dirh = NULL;
3063 return 0;
3064 }
3065
3066 /* This filehandle may already have been closed. If so, just move on to
3067 * the next one.
3068 */
3069 if (fxh->fh == NULL) {
3070 return 0;
3071 }
3072
3073 curr_path = pstrdup(fxh->pool, fxh->fh->fh_path);
3074 real_path = curr_path;
3075 if (fxh->xfer.path_hidden != NULL) {
3076 real_path = fxh->xfer.path_hidden;
3077 }
3078
3079 /* Write an 'incomplete' TransferLog entry for this. */
3080 abs_path = sftp_misc_vroot_abs_path(fxh->pool, real_path, TRUE);
3081
3082 if (fxh->fh_flags == O_RDONLY) {
3083 direction = 'o';
3084
3085 } else {
3086 direction = 'i';
3087 }
3088
3089 if (fxh->fh_flags & O_APPEND) {
3090 cmd = fxp_cmd_alloc(fxh->pool, C_APPE, pstrdup(fxh->pool, curr_path));
3091 cmd->cmd_class = CL_WRITE;
3092 session.curr_cmd = C_APPE;
3093
3094 if (pr_table_add(cmd->notes, "mod_xfer.store-path",
3095 pstrdup(fxh->pool, curr_path), 0) < 0) {
3096 if (errno != EEXIST) {
3097 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3098 "error adding 'mod_xfer.store-path' note: %s", strerror(errno));
3099 }
3100 }
3101
3102 } else if ((fxh->fh_flags & O_WRONLY) ||
3103 (fxh->fh_flags & O_RDWR)) {
3104 cmd = fxp_cmd_alloc(fxh->pool, C_STOR, pstrdup(fxh->pool, curr_path));
3105 cmd->cmd_class = CL_WRITE;
3106 session.curr_cmd = C_STOR;
3107
3108 if (pr_table_add(cmd->notes, "mod_xfer.store-path",
3109 pstrdup(fxh->pool, curr_path), 0) < 0) {
3110 if (errno != EEXIST) {
3111 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3112 "error adding 'mod_xfer.store-path' note: %s", strerror(errno));
3113 }
3114 }
3115
3116 } else if (fxh->fh_flags == O_RDONLY) {
3117 cmd = fxp_cmd_alloc(fxh->pool, C_RETR, pstrdup(fxh->pool, curr_path));
3118 cmd->cmd_class = CL_READ;
3119 session.curr_cmd = C_RETR;
3120
3121 if (pr_table_add(cmd->notes, "mod_xfer.retr-path",
3122 pstrdup(fxh->pool, curr_path), 0) < 0) {
3123 if (errno != EEXIST) {
3124 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3125 "error adding 'mod_xfer.retr-path' note: %s", strerror(errno));
3126 }
3127 }
3128 }
3129
3130 if (cmd != NULL) {
3131 /* Add a note indicating that this is a failed transfer. */
3132 fxp_cmd_note_file_status(cmd, "failed");
3133 }
3134
3135 /* Populate the session.xfer struct with filehandle-specific details
3136 * (Issue #1646)
3137 */
3138 fxp_set_filehandle_sess_xfer(fxh);
3139
3140 xferlog_write(0, pr_netaddr_get_sess_remote_name(), fxh->xfer.total_bytes,
3141 abs_path, 'b', direction, 'r', session.user, 'i', "_");
3142
3143 if (cmd != NULL) {
3144 pr_response_clear(&resp_list);
3145 pr_response_clear(&resp_err_list);
3146
3147 pr_response_add_err(R_451, "%s: %s", cmd->arg, strerror(ECONNRESET));
3148 fxp_cmd_dispatch_err(cmd);
3149 }
3150
3151 if (pr_fsio_close(fxh->fh) < 0) {
3152 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3153 "error writing aborted file '%s': %s", fxh->fh->fh_path, strerror(errno));
3154 }
3155
3156 fxh->fh = NULL;
3157
3158 if (fxh->fh_flags != O_RDONLY) {
3159 if (fxh->xfer.path_hidden != NULL) {
3160 /* This is a HiddenStores file. */
3161 if (delete_aborted_stores == NULL ||
3162 *delete_aborted_stores == TRUE) {
3163 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3164 "removing aborted uploaded file '%s'", curr_path);
3165
3166 if (pr_fsio_unlink(curr_path) < 0) {
3167 if (errno != ENOENT) {
3168 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3169 "error unlinking file '%s': %s", curr_path,
3170 strerror(errno));
3171 }
3172 }
3173 }
3174 }
3175 }
3176
3177 return 0;
3178 }
3179
3180 static int fxp_handle_delete(struct fxp_handle *fxh) {
3181 if (fxp_session->handle_tab == NULL) {
3182 errno = EPERM;
3183 return -1;
3184 }
3185
3186 (void) pr_table_remove(fxp_session->handle_tab, fxh->name, NULL);
3187 return 0;
3188 }
3189
3190 static struct fxp_handle *fxp_handle_get(const char *handle) {
3191 struct fxp_handle *fxh;
3192
3193 if (fxp_session->handle_tab == NULL) {
3194 errno = EPERM;
3195 return NULL;
3196 }
3197
3198 fxh = (struct fxp_handle *) pr_table_get(fxp_session->handle_tab, handle,
3199 NULL);
3200 return fxh;
3201 }
3202
3203 /* FX Message I/O */
3204
3205 static struct fxp_packet *fxp_packet_create(pool *p, uint32_t channel_id) {
3206 pool *sub_pool;
3207 struct fxp_packet *fxp;
3208
3209 sub_pool = make_sub_pool(p);
3210 pr_pool_tag(sub_pool, "SFTP packet pool");
3211 fxp = pcalloc(sub_pool, sizeof(struct fxp_packet));
3212 fxp->pool = sub_pool;
3213 fxp->channel_id = channel_id;
3214
3215 return fxp;
3216 }
3217
3218 static pool *curr_buf_pool = NULL;
3219 static unsigned char *curr_buf = NULL;
3220 static uint32_t curr_buflen = 0, curr_bufsz = 0;
3221 static struct fxp_packet *curr_pkt = NULL;
3222
3223 static struct fxp_packet *fxp_packet_get_packet(uint32_t channel_id) {
3224 struct fxp_packet *fxp;
3225
3226 if (curr_pkt) {
3227 return curr_pkt;
3228 }
3229
3230 fxp = fxp_packet_create(fxp_pool, channel_id);
3231 return fxp;
3232 }
3233
3234 static void fxp_packet_set_packet(struct fxp_packet *pkt) {
3235 curr_pkt = pkt;
3236 }
3237
3238 static void fxp_packet_clear_cache_data(void) {
3239 curr_buflen = 0;
3240 }
3241
3242 static uint32_t fxp_packet_get_cache_data(unsigned char **data) {
3243 *data = curr_buf;
3244 return curr_buflen;
3245 }
3246
3247 static uint32_t fxp_packet_have_cache_data(void) {
3248 return curr_buflen;
3249 }
3250
3251 static void fxp_packet_add_cache_data(unsigned char *data, uint32_t datalen) {
3252 if (curr_buf_pool == NULL) {
3253 curr_buf_pool = make_sub_pool(fxp_pool);
3254 pr_pool_tag(curr_buf_pool, "SFTP packet buffer pool");
3255
3256 curr_buf = palloc(curr_buf_pool, FXP_PACKET_DATA_DEFAULT_SZ);
3257 curr_bufsz = fxp_packet_data_allocsz = FXP_PACKET_DATA_DEFAULT_SZ;
3258 }
3259
3260 if (data == NULL ||
3261 datalen == 0) {
3262 return;
3263 }
3264
3265 if (curr_buflen == 0) {
3266 if (curr_bufsz >= datalen) {
3267 /* We already have a buffer with enough space. Nice. */
3268
3269 } else {
3270 /* We need a larger buffer. Round up to the nearest 1K size. */
3271 size_t sz;
3272
3273 sz = sftp_crypto_get_size(datalen+1, 1024);
3274
3275 if (fxp_packet_data_allocsz > FXP_PACKET_DATA_ALLOC_MAX_SZ) {
3276 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3277 "renewing SFTP packet data pool");
3278 destroy_pool(curr_buf_pool);
3279
3280 curr_buf_pool = make_sub_pool(fxp_pool);
3281 pr_pool_tag(curr_buf_pool, "SFTP packet buffer pool");
3282
3283 curr_bufsz = 0;
3284 }
3285
3286 curr_bufsz = sz;
3287 curr_buf = palloc(curr_buf_pool, curr_bufsz);
3288 fxp_packet_data_allocsz += sz;
3289 }
3290
3291 /* We explicitly want to use memmove(3) here rather than memcpy(3),
3292 * since it is possible (and likely) that after reading data out
3293 * of this buffer, there will be leftover data which is put back into
3294 * the buffer, only at a different offset. This means that the
3295 * source and destination pointers CAN overlap; using memcpy(3) would
3296 * lead to subtle memory copy issue (e.g. Bug#3743).
3297 *
3298 * This manifested as hard-to-reproduce SFTP upload/download stalls,
3299 * segfaults, etc, due to corrupted memory being read out as
3300 * packet lengths and such.
3301 */
3302 memmove(curr_buf, data, datalen);
3303 curr_buflen = datalen;
3304
3305 return;
3306 }
3307
3308 if (curr_buflen > 0) {
3309 if (curr_bufsz >= (curr_buflen + datalen)) {
3310 /* We already have a buffer with enough space. Nice. */
3311
3312 } else {
3313 /* We need a larger buffer. Round up to the nearest 1K size. */
3314 pool *tmp_pool;
3315 char *cached_data;
3316 uint32_t cached_datalen;
3317 size_t sz;
3318
3319 if (curr_buflen + datalen > FXP_MAX_PACKET_LEN) {
3320 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3321 "received excessive SFTP data (len %lu > max %lu bytes), rejecting",
3322 (unsigned long) curr_buflen + datalen,
3323 (unsigned long) FXP_MAX_PACKET_LEN);
3324 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
3325 }
3326
3327 /* Get the existing cached data before allocating a larger buffer. */
3328 tmp_pool = make_sub_pool(fxp_pool);
3329
3330 cached_datalen = curr_buflen;
3331 cached_data = palloc(tmp_pool, cached_datalen);
3332 memcpy(cached_data, curr_buf, cached_datalen);
3333
3334 if (fxp_packet_data_allocsz > FXP_PACKET_DATA_ALLOC_MAX_SZ) {
3335 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3336 "renewing SFTP packet data pool");
3337
3338 destroy_pool(curr_buf_pool);
3339
3340 curr_buf_pool = make_sub_pool(fxp_pool);
3341 pr_pool_tag(curr_buf_pool, "SFTP packet buffer pool");
3342 }
3343
3344 sz = sftp_crypto_get_size(curr_buflen + datalen + 1, 1024);
3345 curr_bufsz = sz;
3346 curr_buf = palloc(curr_buf_pool, curr_bufsz);
3347 fxp_packet_data_allocsz += sz;
3348
3349 memcpy(curr_buf, cached_data, cached_datalen);
3350 curr_buflen = cached_datalen;
3351
3352 destroy_pool(tmp_pool);
3353 }
3354
3355 /* Append the SSH2 data to the current unconsumed buffer.
3356 *
3357 * We explicitly want to use memmove(3) here rather than memcpy(3),
3358 * since it is possible (and likely) that after reading data out
3359 * of this buffer, there will be leftover data which is put back into
3360 * the buffer, only at a different offset. This means that the
3361 * source and destination pointers CAN overlap; using memcpy(3) would
3362 * lead to subtle memory copy issue (e.g. Bug#3743).
3363 *
3364 * This manifested as hard-to-reproduce SFTP upload/download stalls,
3365 * segfaults, etc, due to corrupted memory being read out as
3366 * packet lengths and such.
3367 */
3368 memmove(curr_buf + curr_buflen, data, datalen);
3369 curr_buflen += datalen;
3370 }
3371 }
3372
3373 static struct fxp_packet *fxp_packet_read(uint32_t channel_id,
3374 unsigned char **data, uint32_t *datalen) {
3375 struct fxp_packet *fxp;
3376 unsigned char *buf;
3377 uint32_t buflen;
3378
3379 if (datalen != NULL) {
3380 pr_trace_msg(trace_channel, 9,
3381 "reading SFTP data from SSH2 packet buffer (%lu bytes)",
3382 (unsigned long) *datalen);
3383
3384 /* If we have previously cached data, we need to ensure that we append
3385 * our current data to that cached data first.
3386 */
3387 if (fxp_packet_have_cache_data() > 0) {
3388 fxp_packet_add_cache_data(*data, *datalen);
3389 buflen = fxp_packet_get_cache_data(&buf);
3390
3391 } else {
3392 buflen = *datalen;
3393 buf = *data;
3394 }
3395
3396 } else {
3397 buflen = fxp_packet_get_cache_data(&buf);
3398 }
3399
3400 pr_trace_msg(trace_channel, 19,
3401 "using %lu bytes of SSH2 packet buffer data", (unsigned long) buflen);
3402
3403 fxp = fxp_packet_get_packet(channel_id);
3404
3405 if (!(fxp->state & FXP_PACKET_HAVE_PACKET_LEN)) {
3406 /* Make sure we have enough data in the buffer to cover the packet len. */
3407 if (buflen < sizeof(uint32_t)) {
3408 fxp_packet_set_packet(fxp);
3409 fxp_packet_clear_cache_data();
3410 fxp_packet_add_cache_data(buf, buflen);
3411
3412 return NULL;
3413 }
3414
3415 fxp->packet_len = sftp_msg_read_int(fxp->pool, &buf, &buflen);
3416 fxp->state |= FXP_PACKET_HAVE_PACKET_LEN;
3417
3418 pr_trace_msg(trace_channel, 19,
3419 "read SFTP request packet len %lu from SSH2 packet buffer "
3420 "(%lu bytes remaining in buffer)", (unsigned long) fxp->packet_len,
3421 (unsigned long) buflen);
3422
3423 /* We require 5 bytes of SFTP request data at a minimum: 1 byte for the
3424 * request type, and 4 bytes for the payload length (Issue #2115).
3425 */
3426 if (fxp->packet_len < 5) {
3427 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3428 "illegal SFTP request length (%lu bytes, require at least 5 bytes), "
3429 "rejecting", (unsigned long) fxp->packet_len);
3430 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
3431 }
3432
3433 if (fxp->packet_len > FXP_MAX_PACKET_LEN) {
3434 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
3435 "received excessive SFTP packet (len %lu > max %lu bytes), rejecting",
3436 (unsigned long) fxp->packet_len, (unsigned long) FXP_MAX_PACKET_LEN);
3437 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
3438 }
3439
3440 if (buflen == 0) {
3441 fxp_packet_set_packet(fxp);
3442 fxp_packet_clear_cache_data();
3443
3444 return NULL;
3445 }
3446
3447 } else {
3448 pr_trace_msg(trace_channel, 19,
3449 "already have SFTP request packet len %lu from previous buffer data",
3450 (unsigned long) fxp->packet_len);
3451 }
3452
3453 if (!(fxp->state & FXP_PACKET_HAVE_REQUEST_TYPE)) {
3454 /* Make sure we have enough data in the buffer to cover the request type. */
3455 if (buflen < sizeof(char)) {
3456 fxp_packet_set_packet(fxp);
3457 fxp_packet_clear_cache_data();
3458 fxp_packet_add_cache_data(buf, buflen);
3459
3460 return NULL;
3461 }
3462
3463 fxp->request_type = sftp_msg_read_byte(fxp->pool, &buf, &buflen);
3464 fxp->state |= FXP_PACKET_HAVE_REQUEST_TYPE;
3465
3466 pr_trace_msg(trace_channel, 19,
3467 "read SFTP request type %s (%d) from SSH2 packet buffer "
3468 "(%lu bytes remaining in buffer)",
3469 fxp_get_request_type_desc(fxp->request_type), (int) fxp->request_type,
3470 (unsigned long) buflen);
3471
3472 if (buflen == 0) {
3473 fxp_packet_set_packet(fxp);
3474 fxp_packet_clear_cache_data();
3475
3476 return NULL;
3477 }
3478
3479 } else {
3480 pr_trace_msg(trace_channel, 19,
3481 "already have SFTP request type %s (%d) from previous buffer data",
3482 fxp_get_request_type_desc(fxp->request_type), fxp->request_type);
3483 }
3484
3485 if (!(fxp->state & FXP_PACKET_HAVE_PAYLOAD_SIZE)) {
3486 /* And take back one byte for whose request_type this is... */
3487 fxp->payload_sz = fxp->packet_len - 1;
3488 fxp->state |= FXP_PACKET_HAVE_PAYLOAD_SIZE;
3489
3490 pr_trace_msg(trace_channel, 19,
3491 "read SFTP request payload size %lu from SSH2 packet buffer "
3492 "(%lu bytes remaining in buffer)", (unsigned long) fxp->payload_sz,
3493 (unsigned long) buflen);
3494
3495 } else {
3496 pr_trace_msg(trace_channel, 19,
3497 "already have SFTP request payload size %lu from previous buffer data",
3498 (unsigned long) fxp->payload_sz);
3499 }
3500
3501 if (!(fxp->state & FXP_PACKET_HAVE_REQUEST_ID)) {
3502 if (fxp->request_type != SFTP_SSH2_FXP_INIT) {
3503 /* Make sure we have enough data in the buffer to cover the request ID. */
3504 if (buflen < sizeof(uint32_t)) {
3505 fxp_packet_set_packet(fxp);
3506 fxp_packet_clear_cache_data();
3507 fxp_packet_add_cache_data(buf, buflen);
3508
3509 return NULL;
3510 }
3511
3512 /* The INIT and VERSION requests do not use request IDs. */
3513 fxp->request_id = sftp_msg_read_int(fxp->pool, &buf, &buflen);
3514 fxp->payload_sz -= sizeof(uint32_t);
3515
3516 pr_trace_msg(trace_channel, 19,
3517 "read SFTP request ID %lu from SSH2 packet buffer "
3518 "(%lu bytes remaining in buffer)", (unsigned long) fxp->request_id,
3519 (unsigned long) buflen);
3520 }
3521
3522 fxp->state |= FXP_PACKET_HAVE_REQUEST_ID;
3523
3524 if (buflen == 0) {
3525 fxp_packet_set_packet(fxp);
3526 fxp_packet_clear_cache_data();
3527
3528 return NULL;
3529 }
3530
3531 } else {
3532 pr_trace_msg(trace_channel, 19,
3533 "already have SFTP request ID %lu from previous buffer data",
3534 (unsigned long) fxp->request_id);
3535 }
3536
3537 if (!(fxp->state & FXP_PACKET_HAVE_PAYLOAD)) {
3538 uint32_t payload_remaining;
3539
3540 /* The first question is: do we have any existing payload data?
3541 *
3542 * 1. Have no payload data:
3543 * a. Packet buffer is exactly size of needed payload data.
3544 * This means that we will get the full payload, and there will be
3545 * no data left over in the packet buffer.
3546 *
3547 * b. Packet buffer is larger than size of needed payload data.
3548 * This means that we will get the full payload, and there will be
3549 * data left over in the packet buffer.
3550 *
3551 * c. Packet buffer is smaller than size of needed payload data.
3552 * This means that we will get only a partial payload, and there will
3553 * be no data left over in the packet buffer.
3554 *
3555 * 2. Have existing payload data:
3556 * a. Packet buffer is exactly size of remaining payload data.
3557 * This means that we will get the full payload, and there will be
3558 * no data left over in the packet buffer.
3559 *
3560 * b. Packet buffer is larger than size of remaining payload data.
3561 * This means that we will get the full payload, and there will be
3562 * data left over in the packet buffer.
3563 *
3564 * c. Packet buffer is smaller than size of remaining payload data.
3565 * This means that we will get only a partial payload, and there will
3566 * be no data left over in the packet buffer.
3567 *
3568 * To simplify the code, we can say that if we have no payload data,
3569 * it can be handled the same as a partial payload of length zero.
3570 */
3571
3572 if (fxp->payload == NULL) {
3573 /* Make sure we have a payload buffer allocated. */
3574 fxp->payload = palloc(fxp->pool, fxp->payload_sz);
3575 fxp->payload_len = 0;
3576 }
3577
3578 /* Now determine the amount of bytes remaining before we have the full
3579 * payload.
3580 */
3581 payload_remaining = fxp->payload_sz - fxp->payload_len;
3582
3583 /* First case: the packet buffer is exactly the size of the remaining
3584 * payload data.
3585 */
3586 if (buflen == payload_remaining) {
3587 pr_trace_msg(trace_channel, 19,
3588 "filling SFTP request payload (%lu remaining of %lu total bytes) "
3589 "from SSH2 packet buffer (%lu bytes in buffer)",
3590 (unsigned long) payload_remaining, (unsigned long) fxp->payload_sz,
3591 (unsigned long) buflen);
3592
3593 memcpy(fxp->payload + fxp->payload_len, buf, buflen);
3594 fxp->payload_len = buflen;
3595 fxp->state |= FXP_PACKET_HAVE_PAYLOAD;
3596
3597 fxp_packet_set_packet(NULL);
3598 fxp_packet_clear_cache_data();
3599
3600 pr_trace_msg(trace_channel, 19, "completely filled payload of %lu bytes "
3601 "(0 bytes remaining in buffer)", (unsigned long) fxp->payload_sz);
3602 return fxp;
3603 }
3604
3605 /* Second case: the packet buffer is larger than the size of the remaining
3606 * payload data.
3607 */
3608 if (buflen > payload_remaining) {
3609 pr_trace_msg(trace_channel, 19,
3610 "filling SFTP request payload (%lu remaining of %lu total bytes) "
3611 "from SSH2 packet buffer (%lu bytes in buffer)",
3612 (unsigned long) payload_remaining, (unsigned long) fxp->payload_sz,
3613 (unsigned long) buflen);
3614
3615 memcpy(fxp->payload + fxp->payload_len, buf, payload_remaining);
3616 fxp->payload_len += payload_remaining;
3617 fxp->state |= FXP_PACKET_HAVE_PAYLOAD;
3618
3619 buflen -= payload_remaining;
3620 buf += payload_remaining;
3621
3622 fxp_packet_set_packet(NULL);
3623 fxp_packet_clear_cache_data();
3624 fxp_packet_add_cache_data(buf, buflen);
3625
3626 pr_trace_msg(trace_channel, 19, "completely filled payload of %lu bytes "
3627 "(%lu bytes remaining in buffer)", (unsigned long) fxp->payload_sz,
3628 (unsigned long) buflen);
3629 return fxp;
3630 }
3631
3632 /* Third (and remaining) case: the packet buffer is smaller than the size
3633 * of the remaining payload data.
3634 */
3635 pr_trace_msg(trace_channel, 19,
3636 "filling SFTP request payload (%lu remaining of %lu total bytes) "
3637 "from SSH2 packet buffer (%lu bytes in buffer)",
3638 (unsigned long) payload_remaining, (unsigned long) fxp->payload_sz,
3639 (unsigned long) buflen);
3640
3641 memcpy(fxp->payload + fxp->payload_len, buf, buflen);
3642 fxp->payload_len += buflen;
3643
3644 fxp_packet_set_packet(fxp);
3645 fxp_packet_clear_cache_data();
3646
3647 } else {
3648 pr_trace_msg(trace_channel, 19,
3649 "already have SFTP payload (%lu bytes) from previous buffer data",
3650 (unsigned long) fxp->payload_sz);
3651 }
3652
3653 return NULL;
3654 }
3655
3656 static int fxp_packet_write(struct fxp_packet *fxp) {
3657 unsigned char *buf, *ptr;
3658 uint32_t buflen, bufsz;
3659 int res;
3660
3661 /* Use a buffer that's a little larger than the FX packet size */
3662 buflen = bufsz = fxp->payload_sz + 32;
3663 buf = ptr = palloc(fxp->pool, bufsz);
3664
3665 sftp_msg_write_data(&buf, &buflen, fxp->payload, fxp->payload_sz, TRUE);
3666
3667 res = sftp_channel_write_data(fxp->pool, fxp->channel_id, ptr,
3668 (bufsz - buflen));
3669 return res;
3670 }
3671
3672 /* Miscellaneous */
3673
3674 static void fxp_version_add_vendor_id_ext(pool *p, unsigned char **buf,
3675 uint32_t *buflen) {
3676 unsigned char *buf2, *ptr2;
3677 const char *vendor_name, *product_name, *product_version;
3678 uint32_t bufsz2, buflen2;
3679 uint64_t build_number;
3680 struct fxp_extpair ext;
3681
3682 bufsz2 = buflen2 = 512;
3683 ptr2 = buf2 = sftp_msg_getbuf(p, bufsz2);
3684
3685 vendor_name = "ProFTPD Project";
3686 product_name = "mod_sftp";
3687 product_version = MOD_SFTP_VERSION;
3688 build_number = pr_version_get_number();
3689
3690 sftp_msg_write_string(&buf2, &buflen2, vendor_name);
3691 sftp_msg_write_string(&buf2, &buflen2, product_name);
3692 sftp_msg_write_string(&buf2, &buflen2, product_version);
3693 sftp_msg_write_long(&buf2, &buflen2, build_number);
3694
3695 ext.ext_name = "vendor-id";
3696 ext.ext_data = ptr2;
3697 ext.ext_datalen = (bufsz2 - buflen2);
3698
3699 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = "
3700 "{ vendorName = '%s', productName = '%s', productVersion = '%s', "
3701 "buildNumber = %" PR_LU " }", ext.ext_name, vendor_name, product_name,
3702 product_version, (pr_off_t) build_number);
3703
3704 fxp_msg_write_extpair(buf, buflen, &ext);
3705 }
3706
3707 static void fxp_version_add_version_ext(pool *p, unsigned char **buf,
3708 uint32_t *buflen) {
3709 register unsigned int i;
3710 struct fxp_extpair ext;
3711 char *versions_str = "";
3712
3713 if (!(fxp_ext_flags & SFTP_FXP_EXT_VERSION_SELECT)) {
3714 return;
3715 }
3716
3717 ext.ext_name = "versions";
3718
3719 /* The versions we report to the client depend on the min/max client
3720 * versions, which may have been configured differently via SFTPClientMatch.
3721 */
3722
3723 for (i = fxp_min_client_version; i <= fxp_max_client_version; i++) {
3724 switch (i) {
3725 case 1:
3726 /* Skip version 1; it is not in the list of version strings defined
3727 * in Section 4.6 of the SFTP Draft.
3728 */
3729 break;
3730
3731 case 2:
3732 versions_str = pstrcat(p, versions_str, *versions_str ? "," : "",
3733 "2", NULL);
3734 break;
3735
3736 case 3:
3737 versions_str = pstrcat(p, versions_str, *versions_str ? "," : "",
3738 "3", NULL);
3739 break;
3740
3741 #if defined(PR_USE_NLS)
3742 /* We can only advertise support for these protocol versions if
3743 * --enable-nls has been used, as they require UTF8 support.
3744 */
3745 case 4:
3746 versions_str = pstrcat(p, versions_str, *versions_str ? "," : "",
3747 "4", NULL);
3748 break;
3749
3750 case 5:
3751 versions_str = pstrcat(p, versions_str, *versions_str ? "," : "",
3752 "5", NULL);
3753 break;
3754
3755 case 6:
3756 versions_str = pstrcat(p, versions_str, *versions_str ? "," : "",
3757 "6", NULL);
3758 break;
3759 #endif /* PR_USE_NLS */
3760 }
3761 }
3762
3763 ext.ext_data = (unsigned char *) versions_str;
3764 ext.ext_datalen = strlen(versions_str);
3765
3766 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3767 ext.ext_data);
3768 fxp_msg_write_extpair(buf, buflen, &ext);
3769
3770 /* The sending of this extension is necessary in order to support any
3771 * 'version-select' requests from the client, as per Section 4.6 of the
3772 * SFTP Draft. That is, if we don't send the 'versions' extension and the
3773 * client tries to send us a 'version-select', then we MUST close the
3774 * connection.
3775 */
3776 allow_version_select = TRUE;
3777 }
3778
3779 /* The difference between "add_std_exts" and "add_std_ext_names" is that the
3780 * latter only includes the text names and NO VALUES, intended for use in the
3781 * "supported" and "supported2" extension lists. Yes, this leads to redundant
3782 * and similar (but not quite duplicated) advertising of these same extensions.
3783 */
3784
3785 static void fxp_version_add_std_ext_names(pool *p, unsigned char **buf,
3786 uint32_t *buflen) {
3787 unsigned int ext_count = 0;
3788
3789 /* These are the "standard" SFTP extensions.
3790 * See draft-ietf-secsh-filexfer-extensions-00.
3791 *
3792 * The possible extensions to advertise here are:
3793 * check-file
3794 * copy-file
3795 * home-directory
3796 * space-available
3797 * vendor-id
3798 */
3799
3800 if (fxp_ext_flags & SFTP_FXP_EXT_CHECK_FILE) {
3801 ext_count++;
3802 }
3803
3804 if (fxp_ext_flags & SFTP_FXP_EXT_COPY_FILE) {
3805 ext_count++;
3806 }
3807
3808 if (fxp_ext_flags & SFTP_FXP_EXT_HOMEDIR) {
3809 ext_count++;
3810 }
3811
3812 if (fxp_ext_flags & SFTP_FXP_EXT_SPACE_AVAIL) {
3813 ext_count++;
3814 }
3815
3816 if (fxp_ext_flags & SFTP_FXP_EXT_VENDOR_ID) {
3817 ext_count++;
3818 }
3819
3820 /* Extension names */
3821 sftp_msg_write_int(buf, buflen, ext_count);
3822
3823 if (fxp_ext_flags & SFTP_FXP_EXT_CHECK_FILE) {
3824 pr_trace_msg(trace_channel, 11, "%s", "+ SFTP extension name: check-file");
3825 sftp_msg_write_string(buf, buflen, "check-file");
3826 }
3827
3828 if (fxp_ext_flags & SFTP_FXP_EXT_COPY_FILE) {
3829 pr_trace_msg(trace_channel, 11, "%s", "+ SFTP extension name: copy-file");
3830 sftp_msg_write_string(buf, buflen, "copy-file");
3831 }
3832
3833 if (fxp_ext_flags & SFTP_FXP_EXT_HOMEDIR) {
3834 pr_trace_msg(trace_channel, 11, "%s",
3835 "+ SFTP extension name: home-directory");
3836 sftp_msg_write_string(buf, buflen, "home-directory");
3837 }
3838
3839 if (fxp_ext_flags & SFTP_FXP_EXT_SPACE_AVAIL) {
3840 pr_trace_msg(trace_channel, 11, "%s",
3841 "+ SFTP extension name: space-available");
3842 sftp_msg_write_string(buf, buflen, "space-available");
3843 }
3844
3845 if (fxp_ext_flags & SFTP_FXP_EXT_VENDOR_ID) {
3846 pr_trace_msg(trace_channel, 11, "%s", "+ SFTP extension name: vendor-id");
3847 sftp_msg_write_string(buf, buflen, "vendor-id");
3848 }
3849 }
3850
3851 static void fxp_version_add_std_exts(pool *p, unsigned char **buf,
3852 uint32_t *buflen) {
3853
3854 /* These are the "standard" SFTP extensions.
3855 * See draft-ietf-secsh-filexfer-extensions-00.
3856 *
3857 * The possible extensions to advertise here are:
3858 * check-file
3859 * copy-file
3860 * home-directory
3861 * space-available
3862 * vendor-id
3863 */
3864
3865 if (fxp_ext_flags & SFTP_FXP_EXT_CHECK_FILE) {
3866 struct fxp_extpair ext;
3867
3868 ext.ext_name = "check-file";
3869 ext.ext_data = (unsigned char *) "1";
3870 ext.ext_datalen = 1;
3871
3872 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3873 ext.ext_data);
3874 fxp_msg_write_extpair(buf, buflen, &ext);
3875 }
3876
3877 if (fxp_ext_flags & SFTP_FXP_EXT_COPY_FILE) {
3878 struct fxp_extpair ext;
3879
3880 ext.ext_name = "copy-file";
3881 ext.ext_data = (unsigned char *) "1";
3882 ext.ext_datalen = 1;
3883
3884 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3885 ext.ext_data);
3886 fxp_msg_write_extpair(buf, buflen, &ext);
3887 }
3888
3889 if (fxp_ext_flags & SFTP_FXP_EXT_HOMEDIR) {
3890 struct fxp_extpair ext;
3891
3892 ext.ext_name = "home-directory";
3893 ext.ext_data = (unsigned char *) "1";
3894 ext.ext_datalen = 1;
3895
3896 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3897 ext.ext_data);
3898 fxp_msg_write_extpair(buf, buflen, &ext);
3899 }
3900
3901 if (fxp_ext_flags & SFTP_FXP_EXT_SPACE_AVAIL) {
3902 struct fxp_extpair ext;
3903
3904 ext.ext_name = "space-available";
3905 ext.ext_data = (unsigned char *) "1";
3906 ext.ext_datalen = 1;
3907
3908 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3909 ext.ext_data);
3910 fxp_msg_write_extpair(buf, buflen, &ext);
3911 }
3912
3913 if (fxp_ext_flags & SFTP_FXP_EXT_VENDOR_ID) {
3914 fxp_version_add_vendor_id_ext(p, buf, buflen);
3915 }
3916 }
3917
3918 static void fxp_version_add_openssh_exts(pool *p, unsigned char **buf,
3919 uint32_t *buflen) {
3920 (void) p;
3921
3922 /* These are OpenSSH-specific SFTP extensions. */
3923
3924 if (fxp_ext_flags & SFTP_FXP_EXT_FSYNC) {
3925 struct fxp_extpair ext;
3926
3927 ext.ext_name = "[email protected]";
3928 ext.ext_data = (unsigned char *) "1";
3929 ext.ext_datalen = 1;
3930
3931 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3932 ext.ext_data);
3933 fxp_msg_write_extpair(buf, buflen, &ext);
3934 }
3935
3936 if (fxp_ext_flags & SFTP_FXP_EXT_POSIX_RENAME) {
3937 struct fxp_extpair ext;
3938
3939 ext.ext_name = "[email protected]";
3940 ext.ext_data = (unsigned char *) "1";
3941 ext.ext_datalen = 1;
3942
3943 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3944 ext.ext_data);
3945 fxp_msg_write_extpair(buf, buflen, &ext);
3946 }
3947
3948 #if defined(HAVE_SYS_STATVFS_H)
3949 if (fxp_ext_flags & SFTP_FXP_EXT_STATVFS) {
3950 struct fxp_extpair ext;
3951
3952 ext.ext_name = "[email protected]";
3953 ext.ext_data = (unsigned char *) "2";
3954 ext.ext_datalen = 1;
3955
3956 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3957 ext.ext_data);
3958 fxp_msg_write_extpair(buf, buflen, &ext);
3959
3960 ext.ext_name = "[email protected]";
3961 ext.ext_data = (unsigned char *) "2";
3962 ext.ext_datalen = 1;
3963
3964 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'",
3965 ext.ext_name, ext.ext_data);
3966 fxp_msg_write_extpair(buf, buflen, &ext);
3967 }
3968 #endif /* HAVE_SYS_STATVFS_H */
3969
3970 if (fxp_ext_flags & SFTP_FXP_EXT_HARDLINK) {
3971 struct fxp_extpair ext;
3972
3973 ext.ext_name = "[email protected]";
3974 ext.ext_data = (unsigned char *) "1";
3975 ext.ext_datalen = 1;
3976
3977 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3978 ext.ext_data);
3979 fxp_msg_write_extpair(buf, buflen, &ext);
3980 }
3981
3982 if (fxp_ext_flags & SFTP_FXP_EXT_LIMITS) {
3983 struct fxp_extpair ext;
3984
3985 ext.ext_name = "[email protected]";
3986 ext.ext_data = (unsigned char *) "1";
3987 ext.ext_datalen = 1;
3988
3989 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
3990 ext.ext_data);
3991 fxp_msg_write_extpair(buf, buflen, &ext);
3992 }
3993
3994 if (fxp_ext_flags & SFTP_FXP_EXT_USERGROUPNAMES) {
3995 struct fxp_extpair ext;
3996
3997 ext.ext_name = "[email protected]";
3998 ext.ext_data = (unsigned char *) "1";
3999 ext.ext_datalen = 1;
4000
4001 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
4002 ext.ext_data);
4003 fxp_msg_write_extpair(buf, buflen, &ext);
4004 }
4005
4006 if (fxp_ext_flags & SFTP_FXP_EXT_XATTR) {
4007 struct fxp_extpair ext;
4008
4009 ext.ext_name = "[email protected]";
4010 ext.ext_data = (unsigned char *) "1";
4011 ext.ext_datalen = 1;
4012
4013 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '%s'", ext.ext_name,
4014 ext.ext_data);
4015 fxp_msg_write_extpair(buf, buflen, &ext);
4016 }
4017 }
4018
4019 static void fxp_version_add_newline_ext(pool *p, unsigned char **buf,
4020 uint32_t *buflen) {
4021 struct fxp_extpair ext;
4022
4023 (void) p;
4024
4025 ext.ext_name = "newline";
4026 ext.ext_data = (unsigned char *) "\n";
4027 ext.ext_datalen = 1;
4028
4029 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s = '\n'", ext.ext_name);
4030 fxp_msg_write_extpair(buf, buflen, &ext);
4031 }
4032
4033 static void fxp_version_add_supported_ext(pool *p, unsigned char **buf,
4034 uint32_t *buflen) {
4035 struct fxp_extpair ext;
4036 uint32_t attrs_len, attrs_sz;
4037 unsigned char *attrs_buf, *attrs_ptr;
4038 uint32_t file_mask, bits_mask, open_mask, access_mask, max_read_size;
4039
4040 ext.ext_name = "supported";
4041
4042 attrs_sz = attrs_len = 1024;
4043 attrs_ptr = attrs_buf = sftp_msg_getbuf(p, attrs_sz);
4044
4045 file_mask = SSH2_FX_ATTR_SIZE|SSH2_FX_ATTR_PERMISSIONS|
4046 SSH2_FX_ATTR_ACCESSTIME|SSH2_FX_ATTR_MODIFYTIME|SSH2_FX_ATTR_OWNERGROUP;
4047
4048 bits_mask = 0;
4049
4050 open_mask = SSH2_FXF_WANT_READ_DATA|SSH2_FXF_WANT_WRITE_DATA|
4051 SSH2_FXF_WANT_APPEND_DATA|SSH2_FXF_WANT_READ_ATTRIBUTES|
4052 SSH2_FXF_WANT_WRITE_ATTRIBUTES;
4053
4054 access_mask = SSH2_FXF_CREATE_NEW|SSH2_FXF_CREATE_TRUNCATE|
4055 SSH2_FXF_OPEN_EXISTING|SSH2_FXF_OPEN_OR_CREATE|
4056 SSH2_FXF_TRUNCATE_EXISTING|SSH2_FXF_ACCESS_APPEND_DATA|
4057 SSH2_FXF_ACCESS_APPEND_DATA_ATOMIC;
4058
4059 max_read_size = 0;
4060
4061 sftp_msg_write_int(&attrs_buf, &attrs_len, file_mask);
4062 sftp_msg_write_int(&attrs_buf, &attrs_len, bits_mask);
4063 sftp_msg_write_int(&attrs_buf, &attrs_len, open_mask);
4064 sftp_msg_write_int(&attrs_buf, &attrs_len, access_mask);
4065 sftp_msg_write_int(&attrs_buf, &attrs_len, max_read_size);
4066
4067 /* Attribute extensions */
4068 sftp_msg_write_int(&attrs_buf, &attrs_len, 0);
4069
4070 /* Protocol extensions */
4071 fxp_version_add_std_ext_names(p, &attrs_buf, &attrs_len);
4072
4073 ext.ext_data = attrs_ptr;
4074 ext.ext_datalen = (attrs_sz - attrs_len);
4075
4076 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s", ext.ext_name);
4077 fxp_msg_write_extpair(buf, buflen, &ext);
4078 }
4079
4080 static void fxp_version_add_supported2_ext(pool *p, unsigned char **buf,
4081 uint32_t *buflen) {
4082 struct fxp_extpair ext;
4083 uint32_t attrs_len, attrs_sz;
4084 unsigned char *attrs_buf, *attrs_ptr;
4085 uint32_t file_mask, bits_mask, open_mask, access_mask, max_read_size;
4086 uint16_t open_lock_mask, lock_mask;
4087
4088 ext.ext_name = "supported2";
4089
4090 attrs_sz = attrs_len = 1024;
4091 attrs_ptr = attrs_buf = sftp_msg_getbuf(p, attrs_sz);
4092
4093 file_mask = SSH2_FX_ATTR_SIZE|SSH2_FX_ATTR_PERMISSIONS|
4094 SSH2_FX_ATTR_ACCESSTIME|SSH2_FX_ATTR_MODIFYTIME|SSH2_FX_ATTR_OWNERGROUP;
4095 #if defined(PR_USE_XATTR)
4096 file_mask |= SSH2_FX_ATTR_EXTENDED;
4097 #endif /* PR_USE_XATTR */
4098
4099 bits_mask = 0;
4100
4101 open_mask = SSH2_FXF_WANT_READ_DATA|SSH2_FXF_WANT_WRITE_DATA|
4102 SSH2_FXF_WANT_APPEND_DATA|SSH2_FXF_WANT_READ_ATTRIBUTES|
4103 SSH2_FXF_WANT_WRITE_ATTRIBUTES;
4104
4105 access_mask = SSH2_FXF_CREATE_NEW|SSH2_FXF_CREATE_TRUNCATE|
4106 SSH2_FXF_OPEN_EXISTING|SSH2_FXF_OPEN_OR_CREATE|
4107 SSH2_FXF_TRUNCATE_EXISTING|SSH2_FXF_ACCESS_APPEND_DATA|
4108 SSH2_FXF_ACCESS_APPEND_DATA_ATOMIC;
4109
4110 max_read_size = 0;
4111
4112 /* Set only one bit, to indicate that locking is not supported for
4113 * OPEN commands.
4114 */
4115 open_lock_mask = 0x0001;
4116
4117 /* Indicate that we support the classic locks: READ+WRITE+ADVISORY and
4118 * WRITE+ADVISORY. Note that we do not need to include DELETE, since this
4119 * mask is only for LOCK commands, not UNLOCK commands.
4120 */
4121 lock_mask = 0x0c01;
4122
4123 sftp_msg_write_int(&attrs_buf, &attrs_len, file_mask);
4124 sftp_msg_write_int(&attrs_buf, &attrs_len, bits_mask);
4125 sftp_msg_write_int(&attrs_buf, &attrs_len, open_mask);
4126 sftp_msg_write_int(&attrs_buf, &attrs_len, access_mask);
4127 sftp_msg_write_int(&attrs_buf, &attrs_len, max_read_size);
4128 fxp_msg_write_short(&attrs_buf, &attrs_len, open_lock_mask);
4129 fxp_msg_write_short(&attrs_buf, &attrs_len, lock_mask);
4130
4131 /* Attribute extensions */
4132 sftp_msg_write_int(&attrs_buf, &attrs_len, 0);
4133
4134 /* Protocol extensions */
4135 fxp_version_add_std_ext_names(p, &attrs_buf, &attrs_len);
4136
4137 ext.ext_data = attrs_ptr;
4138 ext.ext_datalen = (attrs_sz - attrs_len);
4139
4140 pr_trace_msg(trace_channel, 11, "+ SFTP extension: %s", ext.ext_name);
4141 fxp_msg_write_extpair(buf, buflen, &ext);
4142 }
4143
4144 /* SFTP Extension handlers */
4145
4146 /* Returns -1 on error, 0 on EOF, and 1 on successful block read. */
4147 static int read_file_block(pr_fh_t *fh, EVP_MD_CTX *pctx, size_t blocksz,
4148 void *buf, size_t bufsz) {
4149 size_t len, total_len;
4150
4151 total_len = blocksz;
4152
4153 len = bufsz;
4154 if (blocksz < bufsz) {
4155 len = blocksz;
4156 }
4157
4158 while (total_len != 0) {
4159 ssize_t nread;
4160 int xerrno;
4161
4162 nread = pr_fsio_read(fh, buf, len);
4163 xerrno = errno;
4164
4165 if (nread < 0) {
4166 if (xerrno == EINTR) {
4167 pr_signals_handle();
4168 continue;
4169 }
4170
4171 return -1;
4172 }
4173
4174 if (nread == 0) {
4175 /* EOF */
4176 return 0;
4177 }
4178
4179 EVP_DigestUpdate(pctx, buf, nread);
4180 total_len -= nread;
4181 }
4182
4183 return 1;
4184 }
4185
4186 static int fxp_handle_ext_check_file(struct fxp_packet *fxp, char *digest_list,
4187 char *path, off_t offset, off_t len, uint32_t blocksz) {
4188 register unsigned int i;
4189 unsigned char *buf, *ptr;
4190 char *supported_digests;
4191 const char *digest_name, *reason;
4192 uint32_t buflen, bufsz, expected_buflen, status_code;
4193 struct fxp_packet *resp;
4194 int res, xerrno = 0;
4195 struct stat st;
4196 pr_fh_t *fh;
4197 cmd_rec *cmd;
4198 unsigned long block_count;
4199 off_t range_len;
4200 void *data;
4201 size_t datasz;
4202 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4203 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
4204 EVP_MD_CTX md_ctx;
4205 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4206 EVP_MD_CTX *pctx;
4207 const EVP_MD *md;
4208
4209 pr_trace_msg(trace_channel, 8, "client sent check-file request: "
4210 "path = '%s', digests = '%s', offset = %" PR_LU ", len = %" PR_LU
4211 ", block size = %lu", path, digest_list, (pr_off_t) offset, (pr_off_t) len,
4212 (unsigned long) blocksz);
4213
4214 /* We could end up with lots of digests to write, if the file is large
4215 * and/or the block size is small. Be prepared.
4216 */
4217 buflen = bufsz = (FXP_RESPONSE_DATA_DEFAULT_SZ * 2);
4218 buf = ptr = palloc(fxp->pool, bufsz);
4219
4220 /* The minimum block size required by this extension is 256 bytes. */
4221 if (blocksz != 0 &&
4222 blocksz < 256) {
4223 xerrno = EINVAL;
4224
4225 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4226 "SFTP client check-file request sent invalid block size "
4227 "(%lu bytes <= 256)", (unsigned long) blocksz);
4228
4229 status_code = fxp_errno2status(xerrno, &reason);
4230
4231 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4232 (unsigned long) status_code, reason);
4233
4234 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4235 reason, NULL);
4236
4237 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4238 resp->payload = ptr;
4239 resp->payload_sz = (bufsz - buflen);
4240
4241 return fxp_packet_write(resp);
4242 }
4243
4244 pr_fs_clear_cache2(path);
4245 if (pr_fsio_lstat(path, &st) == 0) {
4246 if (S_ISLNK(st.st_mode)) {
4247 char link_path[PR_TUNABLE_PATH_MAX];
4248 int link_len;
4249
4250 memset(link_path, '\0', sizeof(link_path));
4251 link_len = dir_readlink(fxp->pool, path, link_path, sizeof(link_path)-1,
4252 PR_DIR_READLINK_FL_HANDLE_REL_PATH);
4253 if (link_len > 0) {
4254 link_path[link_len] = '\0';
4255 path = pstrdup(fxp->pool, link_path);
4256 }
4257 }
4258 }
4259
4260 pr_fs_clear_cache2(path);
4261 res = pr_fsio_lstat(path, &st);
4262 if (res < 0) {
4263 xerrno = errno;
4264
4265 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4266 "unable to lstat path '%s': %s", path, strerror(xerrno));
4267
4268 status_code = fxp_errno2status(xerrno, &reason);
4269
4270 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
4271 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
4272 xerrno);
4273
4274 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4275 reason, NULL);
4276
4277 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4278 resp->payload = ptr;
4279 resp->payload_sz = (bufsz - buflen);
4280
4281 return fxp_packet_write(resp);
4282 }
4283
4284 if (S_ISDIR(st.st_mode)) {
4285 xerrno = EISDIR;
4286
4287 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4288 "SFTP client check-file requested on a directory, denying");
4289
4290 status_code = fxp_errno2status(xerrno, &reason);
4291
4292 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
4293 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
4294 xerrno);
4295
4296 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4297 reason, NULL);
4298
4299 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4300 resp->payload = ptr;
4301 resp->payload_sz = (bufsz - buflen);
4302
4303 return fxp_packet_write(resp);
4304 }
4305
4306 if (!S_ISREG(st.st_mode) &&
4307 !S_ISLNK(st.st_mode)) {
4308 xerrno = EINVAL;
4309
4310 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4311 "SFTP client check-file request not for file or symlink, denying");
4312
4313 status_code = fxp_errno2status(xerrno, &reason);
4314
4315 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4316 (unsigned long) status_code, reason);
4317
4318 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4319 reason, NULL);
4320
4321 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4322 resp->payload = ptr;
4323 resp->payload_sz = (bufsz - buflen);
4324
4325 return fxp_packet_write(resp);
4326 }
4327
4328 if (offset >= st.st_size) {
4329 xerrno = EINVAL;
4330
4331 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4332 "client check-file request sent invalid offset (%" PR_LU
4333 " >= %" PR_LU " file size)", (pr_off_t) offset, (pr_off_t) st.st_size);
4334
4335 status_code = fxp_errno2status(xerrno, &reason);
4336
4337 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4338 (unsigned long) status_code, reason);
4339
4340 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4341 reason, NULL);
4342
4343 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4344 resp->payload = ptr;
4345 resp->payload_sz = (bufsz - buflen);
4346
4347 return fxp_packet_write(resp);
4348 }
4349
4350 cmd = fxp_cmd_alloc(fxp->pool, "SITE_DIGEST", pstrdup(fxp->pool, path));
4351 if (!dir_check(fxp->pool, cmd, "READ", path, NULL)) {
4352 xerrno = EACCES;
4353
4354 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4355 "'check-file' of '%s' blocked by <Limit> configuration", path);
4356
4357 status_code = fxp_errno2status(xerrno, &reason);
4358
4359 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
4360 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
4361 xerrno);
4362
4363 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4364 reason, NULL);
4365
4366 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4367 resp->payload = ptr;
4368 resp->payload_sz = (bufsz - buflen);
4369
4370 return fxp_packet_write(resp);
4371 }
4372
4373 supported_digests = "sha1,md5";
4374 #if defined(HAVE_SHA256_OPENSSL)
4375 supported_digests = pstrcat(fxp->pool, "sha256,sha224,", supported_digests,
4376 NULL);
4377 #endif /* HAVE_SHA256_OPENSSL */
4378 #if defined(HAVE_SHA512_OPENSSL)
4379 supported_digests = pstrcat(fxp->pool, "sha512,sha384,", supported_digests,
4380 NULL);
4381 #endif /* HAVE_SHA512_OPENSSL */
4382
4383 digest_name = sftp_misc_namelist_shared(fxp->pool, digest_list,
4384 supported_digests);
4385 if (digest_name == NULL) {
4386 xerrno = EINVAL;
4387
4388 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4389 "no supported digests in client check-file request "
4390 "(client sent '%s', server supports '%s')", digest_list,
4391 supported_digests);
4392
4393 status_code = fxp_errno2status(xerrno, &reason);
4394
4395 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4396 (unsigned long) status_code, reason);
4397
4398 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4399 reason, NULL);
4400
4401 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4402 resp->payload = ptr;
4403 resp->payload_sz = (bufsz - buflen);
4404
4405 return fxp_packet_write(resp);
4406 }
4407
4408 if (len != 0) {
4409 range_len = len;
4410
4411 } else {
4412 range_len = st.st_size - offset;
4413 }
4414
4415 if (blocksz == 0) {
4416 block_count = 1;
4417 blocksz = range_len;
4418
4419 } else {
4420 block_count = (unsigned long) (range_len / blocksz);
4421 if (range_len % blocksz != 0) {
4422 block_count++;
4423 }
4424 }
4425
4426 pr_trace_msg(trace_channel, 15, "for check-file request on '%s', "
4427 "calculate %s digest of %lu %s", path, digest_name, block_count,
4428 block_count == 1 ? "block/checksum" : "blocks/checksums");
4429
4430 fh = pr_fsio_open(path, O_RDONLY);
4431 if (fh == NULL) {
4432 xerrno = errno;
4433
4434 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4435 "unable to open path '%s': %s", path, strerror(xerrno));
4436
4437 status_code = fxp_errno2status(xerrno, &reason);
4438
4439 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
4440 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
4441 xerrno);
4442
4443 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4444 reason, NULL);
4445
4446 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4447 resp->payload = ptr;
4448 resp->payload_sz = (bufsz - buflen);
4449
4450 return fxp_packet_write(resp);
4451 }
4452
4453 if (pr_fsio_set_block(fh) < 0) {
4454 pr_trace_msg(trace_channel, 3,
4455 "error setting fd %d (file '%s') as blocking: %s", fh->fh_fd,
4456 fh->fh_path, strerror(errno));
4457 }
4458
4459 if (pr_fsio_lseek(fh, offset, SEEK_SET) < 0) {
4460 xerrno = errno;
4461
4462 pr_fsio_close(fh);
4463
4464 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4465 "unable to seek to offset %" PR_LU " in '%s': %s", (pr_off_t) offset,
4466 path, strerror(xerrno));
4467
4468 status_code = fxp_errno2status(xerrno, &reason);
4469
4470 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
4471 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
4472 xerrno);
4473
4474 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4475 reason, NULL);
4476
4477 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4478 resp->payload = ptr;
4479 resp->payload_sz = (bufsz - buflen);
4480
4481 return fxp_packet_write(resp);
4482 }
4483
4484 md = EVP_get_digestbyname(digest_name);
4485 if (md == NULL) {
4486 xerrno = EINVAL;
4487
4488 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4489 "unable to support %s digests: %s", digest_name,
4490 sftp_crypto_get_errors());
4491
4492 pr_fsio_close(fh);
4493
4494 status_code = fxp_errno2status(xerrno, &reason);
4495
4496 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4497 (unsigned long) status_code, reason);
4498
4499 /* Since we already started writing the EXTENDED_REPLY, we have
4500 * to reset the pointers and overwrite the existing message.
4501 */
4502 buf = ptr;
4503 buflen = bufsz;
4504
4505 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4506 reason, NULL);
4507
4508 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4509 resp->payload = ptr;
4510 resp->payload_sz = (bufsz - buflen);
4511
4512 return fxp_packet_write(resp);
4513 }
4514
4515 /* Calculate the size of the response buffer, based on the number of blocks.
4516 * Our already-allocated response buffer might be too small (see Issue #576).
4517 *
4518 * Each block needs at most EVP_MAX_MD_SIZE bytes, plus 4 bytes for the
4519 * length prefix.
4520 */
4521 expected_buflen = FXP_RESPONSE_DATA_DEFAULT_SZ +
4522 (block_count * (EVP_MAX_MD_SIZE + 4));
4523 if (buflen < expected_buflen) {
4524 pr_trace_msg(trace_channel, 15, "allocated larger buffer (%lu bytes) for "
4525 "check-file request on '%s', %s digest, %lu %s",
4526 (unsigned long) expected_buflen, path, digest_name, block_count,
4527 block_count == 1 ? "block/checksum" : "blocks/checksums");
4528
4529 buflen = bufsz = expected_buflen;
4530 buf = ptr = palloc(fxp->pool, bufsz);
4531 }
4532
4533 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4534 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
4535 pctx = &md_ctx;
4536 EVP_MD_CTX_init(pctx);
4537 #else
4538 pctx = EVP_MD_CTX_new();
4539 #endif /* prior to OpenSSL-1.1.0/LibreSSL-3.5.0 */
4540
4541 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_EXTENDED_REPLY);
4542 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
4543 sftp_msg_write_string(&buf, &buflen, digest_name);
4544
4545 pr_trace_msg(trace_channel, 8,
4546 "sending response: EXTENDED_REPLY %s digest of %lu %s", digest_name,
4547 block_count, block_count == 1 ? "block" : "blocks");
4548
4549 datasz = st.st_blksize * 4;
4550 data = palloc(fxp->pool, datasz);
4551
4552 for (i = 0; i < block_count; i++) {
4553 unsigned char digest[EVP_MAX_MD_SIZE];
4554 unsigned int digest_len = 0;
4555
4556 pr_signals_handle();
4557 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4558 (defined(HAVE_LIBRESSL) && LIBRESSL_VERSION_NUMBER < 0x3050000L)
4559 EVP_MD_CTX_cleanup(pctx);
4560 EVP_MD_CTX_init(pctx);
4561 #else
4562 EVP_MD_CTX_reset(pctx);
4563 #endif /* OpenSSL-1.1.0/LibreSSL-3.5.0 and later */
4564 EVP_DigestInit(pctx, md);
4565
4566 pr_trace_msg(trace_channel, 19,
4567 "reading block %lu (block size %" PR_LU ") from '%s'", block_count,
4568 (pr_off_t) blocksz, path);
4569 res = read_file_block(fh, pctx, blocksz, data, datasz);
4570 xerrno = errno;
4571
4572 if (res < 0) {
4573 pr_fsio_close(fh);
4574
4575 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4576 "error reading from '%s': %s", path, strerror(xerrno));
4577
4578 status_code = fxp_errno2status(xerrno, &reason);
4579
4580 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
4581 "('%s' [%d])", (unsigned long) status_code, reason,
4582 strerror(xerrno), xerrno);
4583
4584 /* Since we already started writing the EXTENDED_REPLY, we have
4585 * to reset the pointers and overwrite the existing message.
4586 */
4587 buf = ptr;
4588 buflen = bufsz;
4589
4590 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4591 reason, NULL);
4592
4593 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4594 resp->payload = ptr;
4595 resp->payload_sz = (bufsz - buflen);
4596
4597 /* Cleanup. */
4598 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4599 defined(HAVE_LIBRESSL)
4600 EVP_MD_CTX_cleanup(pctx);
4601 #else
4602 EVP_MD_CTX_free(pctx);
4603 #endif /* prior to OpenSSL-1.1.0 */
4604
4605 return fxp_packet_write(resp);
4606 }
4607
4608 EVP_DigestFinal(pctx, digest, &digest_len);
4609 sftp_msg_write_data(&buf, &buflen, digest, digest_len, FALSE);
4610 pr_trace_msg(trace_channel, 19,
4611 "completed block %lu (block size %" PR_LU" ) of '%s'", block_count,
4612 (pr_off_t) blocksz, path);
4613 }
4614
4615 /* Cleanup. */
4616 pr_fsio_close(fh);
4617 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
4618 defined(HAVE_LIBRESSL)
4619 EVP_MD_CTX_cleanup(pctx);
4620 #else
4621 EVP_MD_CTX_free(pctx);
4622 #endif /* prior to OpenSSL-1.1.0 */
4623
4624 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4625 resp->payload = ptr;
4626 resp->payload_sz = (bufsz - buflen);
4627
4628 return fxp_packet_write(resp);
4629 }
4630
4631 static int fxp_handle_ext_copy_file(struct fxp_packet *fxp, char *src,
4632 char *dst, int overwrite) {
4633 char *abs_path, *args, *tmp;
4634 unsigned char *buf, *ptr;
4635 const char *reason;
4636 uint32_t buflen, bufsz, status_code;
4637 struct fxp_packet *resp;
4638 cmd_rec *cmd, *cmd2;
4639 int res, xerrno;
4640 struct stat st;
4641
4642 args = pstrcat(fxp->pool, src, " ", dst, NULL);
4643
4644 /* We need to provide an actual argv in this COPY cmd_rec, so we can't
4645 * use fxp_cmd_alloc(); we have to allocate the cmd_rec ourselves.
4646 */
4647 cmd = pr_cmd_alloc(fxp->pool, 4, pstrdup(fxp->pool, "SITE"),
4648 pstrdup(fxp->pool, "COPY"), src, dst);
4649 cmd->arg = pstrcat(fxp->pool, "COPY ", src, " ", dst, NULL);
4650 cmd->cmd_class = CL_MISC|CL_WRITE;
4651
4652 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
4653 buf = ptr = palloc(fxp->pool, bufsz);
4654
4655 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
4656 status_code = SSH2_FX_PERMISSION_DENIED;
4657
4658 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4659 "COPY of '%s' to '%s' blocked by '%s' handler: %s", src, dst,
4660 (char *) cmd->argv[0], strerror(errno));
4661
4662 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4663 (unsigned long) status_code, fxp_strerror(status_code));
4664
4665 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4666 fxp_strerror(status_code), NULL);
4667
4668 fxp_cmd_dispatch_err(cmd);
4669
4670 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4671 resp->payload = ptr;
4672 resp->payload_sz = (bufsz - buflen);
4673
4674 return fxp_packet_write(resp);
4675 }
4676
4677 tmp = src;
4678 src = dir_best_path(fxp->pool, tmp);
4679 if (src == NULL) {
4680 status_code = SSH2_FX_PERMISSION_DENIED;
4681
4682 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4683 "COPY request denied: unable to access path '%s'", tmp);
4684
4685 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4686 (unsigned long) status_code, fxp_strerror(status_code));
4687
4688 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4689 fxp_strerror(status_code), NULL);
4690
4691 fxp_cmd_dispatch_err(cmd);
4692
4693 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4694 resp->payload = ptr;
4695 resp->payload_sz = (bufsz - buflen);
4696
4697 return fxp_packet_write(resp);
4698 }
4699
4700 tmp = dst;
4701 dst = dir_best_path(fxp->pool, tmp);
4702 if (dst == NULL) {
4703 status_code = SSH2_FX_PERMISSION_DENIED;
4704
4705 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4706 "COPY request denied: unable to access path '%s'", tmp);
4707
4708 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4709 (unsigned long) status_code, fxp_strerror(status_code));
4710
4711 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4712 fxp_strerror(status_code), NULL);
4713
4714 fxp_cmd_dispatch_err(cmd);
4715
4716 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4717 resp->payload = ptr;
4718 resp->payload_sz = (bufsz - buflen);
4719
4720 return fxp_packet_write(resp);
4721 }
4722
4723 if (strcmp(src, dst) == 0) {
4724 xerrno = EEXIST;
4725
4726 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4727 "COPY of '%s' to same path '%s', rejecting", src, dst);
4728
4729 status_code = fxp_errno2status(xerrno, &reason);
4730
4731 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
4732 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
4733 xerrno);
4734
4735 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4736 reason, NULL);
4737
4738 fxp_cmd_dispatch_err(cmd);
4739
4740 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4741 resp->payload = ptr;
4742 resp->payload_sz = (bufsz - buflen);
4743
4744 return fxp_packet_write(resp);
4745 }
4746
4747 pr_fs_clear_cache2(dst);
4748 res = pr_fsio_stat(dst, &st);
4749 if (res == 0) {
4750 unsigned char *allow_overwrite = NULL;
4751 int limit_allow;
4752
4753 allow_overwrite = get_param_ptr(get_dir_ctxt(fxp->pool, dst),
4754 "AllowOverwrite", FALSE);
4755
4756 cmd2 = pr_cmd_alloc(fxp->pool, 3, "SITE_COPY", src, dst);
4757 cmd2->arg = pstrdup(fxp->pool, args);
4758 limit_allow = dir_check(fxp->pool, cmd2, "WRITE", dst, NULL);
4759
4760 if (!overwrite ||
4761 (allow_overwrite == NULL ||
4762 *allow_overwrite == FALSE) ||
4763 !limit_allow) {
4764 xerrno = EACCES;
4765
4766 if (!overwrite) {
4767 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4768 "'%s' exists and client did not request COPY overwrites", dst);
4769
4770 } else if (!limit_allow) {
4771 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4772 "COPY to '%s' blocked by <Limit> configuration", dst);
4773
4774 } else {
4775 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4776 "AllowOverwrite permission denied for '%s'", dst);
4777 }
4778
4779 status_code = fxp_errno2status(xerrno, &reason);
4780
4781 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4782 (unsigned long) status_code, reason);
4783
4784 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4785 reason, NULL);
4786
4787 fxp_cmd_dispatch_err(cmd);
4788
4789 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4790 resp->payload = ptr;
4791 resp->payload_sz = (bufsz - buflen);
4792
4793 return fxp_packet_write(resp);
4794 }
4795 }
4796
4797 if (fxp_path_pass_regex_filters(fxp->pool, "COPY", src) < 0 ||
4798 fxp_path_pass_regex_filters(fxp->pool, "COPY", dst) < 0) {
4799 xerrno = errno;
4800
4801 status_code = fxp_errno2status(xerrno, &reason);
4802
4803 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4804 (unsigned long) status_code, reason);
4805
4806 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4807 reason, NULL);
4808
4809 fxp_cmd_dispatch_err(cmd);
4810
4811 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4812 resp->payload = ptr;
4813 resp->payload_sz = (bufsz - buflen);
4814
4815 return fxp_packet_write(resp);
4816 }
4817
4818 cmd2 = pr_cmd_alloc(fxp->pool, 3, "SITE_COPY", src, dst);
4819 cmd2->arg = pstrdup(fxp->pool, args);
4820 if (!dir_check(fxp->pool, cmd2, "READ", src, NULL)) {
4821 xerrno = EACCES;
4822
4823 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4824 "COPY of '%s' blocked by <Limit> configuration", src);
4825
4826 status_code = fxp_errno2status(xerrno, &reason);
4827
4828 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
4829 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
4830 xerrno);
4831
4832 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4833 reason, NULL);
4834
4835 fxp_cmd_dispatch_err(cmd);
4836
4837 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4838 resp->payload = ptr;
4839 resp->payload_sz = (bufsz - buflen);
4840
4841 return fxp_packet_write(resp);
4842 }
4843
4844 res = pr_fs_copy_file2(src, dst, 0, NULL);
4845 if (res < 0) {
4846 xerrno = errno;
4847
4848 status_code = fxp_errno2status(xerrno, &reason);
4849
4850 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4851 "error copying '%s' to '%s': %s", src, dst, strerror(xerrno));
4852
4853 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4854 (unsigned long) status_code, reason);
4855
4856 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4857 reason, NULL);
4858
4859 fxp_cmd_dispatch_err(cmd);
4860
4861 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4862 resp->payload = ptr;
4863 resp->payload_sz = (bufsz - buflen);
4864
4865 return fxp_packet_write(resp);
4866 }
4867
4868 /* No errors. */
4869 xerrno = errno = 0;
4870
4871 pr_fs_clear_cache2(dst);
4872 pr_fsio_stat(dst, &st);
4873
4874 fxp_cmd_dispatch(cmd);
4875
4876 /* Write a TransferLog entry as well. */
4877 abs_path = sftp_misc_vroot_abs_path(fxp->pool, dst, TRUE);
4878 xferlog_write(0, session.c->remote_name, st.st_size, abs_path, 'b', 'i',
4879 'r', session.user, 'c', "_");
4880
4881 status_code = fxp_errno2status(xerrno, &reason);
4882
4883 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4884 (unsigned long) status_code, reason);
4885
4886 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4887 reason, NULL);
4888
4889 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4890 resp->payload = ptr;
4891 resp->payload_sz = (bufsz - buflen);
4892
4893 return fxp_packet_write(resp);
4894 }
4895
4896 static int fxp_handle_ext_fsync(struct fxp_packet *fxp,
4897 struct fxp_handle *fxh) {
4898 unsigned char *buf, *ptr;
4899 char *args;
4900 const char *path, *reason;
4901 uint32_t buflen, bufsz, status_code;
4902 struct fxp_packet *resp;
4903 cmd_rec *cmd;
4904 int res, xerrno;
4905
4906 path = fxh->fh->fh_path;
4907 args = pstrdup(fxp->pool, path);
4908
4909 cmd = fxp_cmd_alloc(fxp->pool, "FSYNC", args);
4910 cmd->cmd_class = CL_MISC|CL_SFTP;
4911 cmd->cmd_id = SFTP_CMD_ID;
4912 pr_cmd_dispatch_phase(cmd, PRE_CMD, 0);
4913
4914 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
4915 buf = ptr = palloc(fxp->pool, bufsz);
4916
4917 res = fsync(PR_FH_FD(fxh->fh));
4918 if (res < 0) {
4919 xerrno = errno;
4920
4921 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4922 "error calling fsync(2) on '%s': %s", path, strerror(xerrno));
4923
4924 errno = xerrno;
4925
4926 } else {
4927 /* No errors. */
4928 xerrno = errno = 0;
4929 }
4930
4931 status_code = fxp_errno2status(xerrno, &reason);
4932
4933 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
4934 "('%s' [%d])", (unsigned long) status_code, reason,
4935 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
4936
4937 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4938 reason, NULL);
4939
4940 if (xerrno == 0) {
4941 fxp_cmd_dispatch(cmd);
4942
4943 } else {
4944 fxp_cmd_dispatch_err(cmd);
4945 }
4946
4947 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4948 resp->payload = ptr;
4949 resp->payload_sz = (bufsz - buflen);
4950
4951 return fxp_packet_write(resp);
4952 }
4953
4954 static int fxp_handle_ext_hardlink(struct fxp_packet *fxp, char *src,
4955 char *dst) {
4956 unsigned char *buf, *ptr;
4957 char *args, *path;
4958 const char *reason;
4959 uint32_t buflen, bufsz, status_code;
4960 struct fxp_packet *resp;
4961 cmd_rec *cmd = NULL;
4962 int res, xerrno = 0;
4963
4964 args = pstrcat(fxp->pool, src, " ", dst, NULL);
4965
4966 pr_scoreboard_entry_update(session.pid,
4967 PR_SCORE_CMD, "%s", "HARDLINK", NULL, NULL);
4968 pr_scoreboard_entry_update(session.pid,
4969 PR_SCORE_CMD_ARG, "%s", args, NULL, NULL);
4970
4971 pr_proctitle_set("%s - %s: HARDLINK %s %s", session.user, session.proc_prefix,
4972 src, dst);
4973
4974 cmd = fxp_cmd_alloc(fxp->pool, "HARDLINK", args);
4975 cmd->cmd_class = CL_MISC|CL_SFTP;
4976 cmd->cmd_id = SFTP_CMD_ID;
4977
4978 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
4979 buf = ptr = palloc(fxp->pool, bufsz);
4980
4981 path = dir_best_path(fxp->pool, src);
4982 if (path == NULL) {
4983 status_code = SSH2_FX_PERMISSION_DENIED;
4984
4985 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
4986 "hardlink request denied: unable to access path '%s'", src);
4987
4988 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
4989 (unsigned long) status_code, fxp_strerror(status_code));
4990
4991 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
4992 fxp_strerror(status_code), NULL);
4993
4994 fxp_cmd_dispatch_err(cmd);
4995
4996 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
4997 resp->payload = ptr;
4998 resp->payload_sz = (bufsz - buflen);
4999
5000 return fxp_packet_write(resp);
5001 }
5002 src = path;
5003
5004 path = dir_best_path(fxp->pool, dst);
5005 if (path == NULL) {
5006 status_code = SSH2_FX_PERMISSION_DENIED;
5007
5008 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5009 "hardlink request denied: unable to access path '%s'", dst);
5010
5011 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5012 (unsigned long) status_code, fxp_strerror(status_code));
5013
5014 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5015 fxp_strerror(status_code), NULL);
5016
5017 fxp_cmd_dispatch_err(cmd);
5018
5019 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5020 resp->payload = ptr;
5021 resp->payload_sz = (bufsz - buflen);
5022
5023 return fxp_packet_write(resp);
5024 }
5025 dst = path;
5026
5027 if (!dir_check(fxp->pool, cmd, G_DIRS, src, NULL) ||
5028 !dir_check(fxp->pool, cmd, G_WRITE, dst, NULL)) {
5029 status_code = SSH2_FX_PERMISSION_DENIED;
5030
5031 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5032 "HARDLINK of '%s' to '%s' blocked by <Limit> configuration",
5033 src, dst);
5034
5035 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5036 (unsigned long) status_code, fxp_strerror(status_code));
5037
5038 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5039 fxp_strerror(status_code), NULL);
5040
5041 fxp_cmd_dispatch_err(cmd);
5042
5043 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5044 resp->payload = ptr;
5045 resp->payload_sz = (bufsz - buflen);
5046
5047 return fxp_packet_write(resp);
5048 }
5049
5050 if (strcmp(src, dst) == 0) {
5051 xerrno = EEXIST;
5052
5053 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5054 "HARDLINK of '%s' to same path '%s', rejecting", src, dst);
5055
5056 status_code = fxp_errno2status(xerrno, &reason);
5057
5058 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
5059 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
5060 xerrno);
5061
5062 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5063 reason, NULL);
5064
5065 fxp_cmd_dispatch_err(cmd);
5066
5067 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5068 resp->payload = ptr;
5069 resp->payload_sz = (bufsz - buflen);
5070
5071 return fxp_packet_write(resp);
5072 }
5073
5074 if (fxp_path_pass_regex_filters(fxp->pool, "HARDLINK", src) < 0 ||
5075 fxp_path_pass_regex_filters(fxp->pool, "HARDLINK", dst) < 0) {
5076 xerrno = errno;
5077
5078 status_code = fxp_errno2status(xerrno, &reason);
5079
5080 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5081 (unsigned long) status_code, fxp_strerror(status_code));
5082
5083 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5084 fxp_strerror(status_code), NULL);
5085
5086 fxp_cmd_dispatch_err(cmd);
5087
5088 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5089 resp->payload = ptr;
5090 resp->payload_sz = (bufsz - buflen);
5091
5092 return fxp_packet_write(resp);
5093 }
5094
5095 res = pr_fsio_link(src, dst);
5096 if (res < 0) {
5097 xerrno = errno;
5098
5099 (void) pr_trace_msg("fileperms", 1, "HARDLINK, user '%s' (UID %s, "
5100 "GID %s): error hardlinking '%s' to '%s': %s", session.user,
5101 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
5102 src, dst, strerror(xerrno));
5103
5104 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5105 "error hardlinking '%s' to '%s': %s", src, dst, strerror(xerrno));
5106
5107 errno = xerrno;
5108
5109 } else {
5110 /* No errors. */
5111 xerrno = errno = 0;
5112 }
5113
5114 status_code = fxp_errno2status(xerrno, &reason);
5115
5116 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
5117 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
5118 xerrno);
5119
5120 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5121 reason, NULL);
5122
5123 if (xerrno == 0) {
5124 fxp_cmd_dispatch(cmd);
5125
5126 } else {
5127 fxp_cmd_dispatch_err(cmd);
5128 }
5129
5130 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5131 resp->payload = ptr;
5132 resp->payload_sz = (bufsz - buflen);
5133
5134 return fxp_packet_write(resp);
5135 }
5136
5137 static int fxp_handle_ext_homedir(struct fxp_packet *fxp, const char *name) {
5138 int res, xerrno = 0;
5139 unsigned char *buf, *ptr;
5140 char *args, *path;
5141 const char *reason;
5142 uint32_t buflen, bufsz, status_code;
5143 struct passwd *pw;
5144 struct stat st;
5145 struct fxp_buffer *fxb;
5146 struct fxp_packet *resp;
5147 cmd_rec *cmd = NULL;
5148
5149 args = pstrdup(fxp->pool, name);
5150
5151 pr_scoreboard_entry_update(session.pid,
5152 PR_SCORE_CMD, "%s", "HOMEDIR", NULL, NULL);
5153 pr_scoreboard_entry_update(session.pid,
5154 PR_SCORE_CMD_ARG, "%s", args, NULL, NULL);
5155
5156 pr_proctitle_set("%s - %s: HOMEDIR %s", session.user, session.proc_prefix,
5157 name);
5158
5159 cmd = fxp_cmd_alloc(fxp->pool, "HOMEDIR", args);
5160 cmd->cmd_class = CL_MISC|CL_SFTP;
5161 cmd->cmd_id = SFTP_CMD_ID;
5162
5163 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
5164 buf = ptr = palloc(fxp->pool, bufsz);
5165
5166 pw = pr_auth_getpwnam(fxp->pool, name);
5167 xerrno = errno;
5168
5169 if (pw == NULL) {
5170 status_code = fxp_errno2status(xerrno, &reason);
5171
5172 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5173 "home-directory request failed: unable to determine home for '%s': %s",
5174 name, strerror(xerrno));
5175
5176 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5177 (unsigned long) status_code, reason);
5178
5179 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5180 reason, NULL);
5181
5182 fxp_cmd_dispatch_err(cmd);
5183
5184 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5185 resp->payload = ptr;
5186 resp->payload_sz = (bufsz - buflen);
5187
5188 return fxp_packet_write(resp);
5189 }
5190
5191 path = pw->pw_dir;
5192
5193 pr_fs_clear_cache2(path);
5194 res = pr_fsio_stat(path, &st);
5195 xerrno = errno;
5196
5197 if (res < 0) {
5198 status_code = fxp_errno2status(xerrno, &reason);
5199
5200 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5201 "home-directory request failed: unable to stat '%s': %s", path,
5202 strerror(xerrno));
5203
5204 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5205 (unsigned long) status_code, reason);
5206
5207 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5208 reason, NULL);
5209
5210 fxp_cmd_dispatch_err(cmd);
5211
5212 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5213 resp->payload = ptr;
5214 resp->payload_sz = (bufsz - buflen);
5215
5216 return fxp_packet_write(resp);
5217 }
5218
5219 /* If we are chrooted, and the chroot path matches the home directory, then
5220 * we should tell the client that the home directory is just `/`.
5221 *
5222 * Then the question is: what if we are chrooted, but the chroot path does
5223 * NOT match the user's home directory?
5224 */
5225 if (session.chroot_path != NULL &&
5226 strcmp(session.chroot_path, path) == 0) {
5227 path = pstrdup(fxp->pool, "/");
5228 }
5229
5230 pr_trace_msg(trace_channel, 8, "sending response: NAME 1 %s %s",
5231 path, fxp_strattrs(fxp->pool, &st, NULL));
5232
5233 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_NAME);
5234 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
5235 sftp_msg_write_int(&buf, &buflen, 1);
5236
5237 fxb = pcalloc(fxp->pool, sizeof(struct fxp_buffer));
5238 fxb->bufsz = buflen = FXP_RESPONSE_NAME_DEFAULT_SZ;
5239 fxb->ptr = buf = palloc(fxp->pool, fxb->bufsz);
5240
5241 fxb->buf = buf;
5242 fxb->buflen = buflen;
5243 fxp_name_write(fxp->pool, fxb, path, &st, 0, session.user, session.group);
5244
5245 buf = fxb->buf;
5246 buflen = fxb->buflen;
5247
5248 fxp_cmd_dispatch(cmd);
5249 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5250 resp->payload = fxb->ptr;
5251 resp->payload_sz = (fxb->bufsz - buflen);
5252
5253 return fxp_packet_write(resp);
5254 }
5255
5256 static int fxp_handle_ext_limits(struct fxp_packet *fxp) {
5257 int res, xerrno = 0;
5258 unsigned char *buf, *ptr;
5259 uint32_t buflen, bufsz;
5260 struct fxp_packet *resp;
5261 cmd_rec *cmd = NULL;
5262 uint32_t max_packet_len, max_read_len, max_write_len, max_open_fds = 0;
5263 rlim_t curr_open_fds;
5264
5265 pr_scoreboard_entry_update(session.pid,
5266 PR_SCORE_CMD, "%s", "LIMITS", NULL, NULL);
5267 pr_scoreboard_entry_update(session.pid,
5268 PR_SCORE_CMD_ARG, "", NULL, NULL);
5269
5270 pr_proctitle_set("%s - %s: LIMITS", session.user, session.proc_prefix);
5271
5272 cmd = fxp_cmd_alloc(fxp->pool, "LIMITS", "");
5273 cmd->cmd_class = CL_MISC|CL_SFTP;
5274 cmd->cmd_id = SFTP_CMD_ID;
5275
5276 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
5277 buf = ptr = palloc(fxp->pool, bufsz);
5278
5279 max_packet_len = FXP_MAX_PACKET_LEN;
5280 max_read_len = max_write_len = FXP_MAX_PACKET_LEN - 1024;
5281
5282 res = pr_rlimit_get_files(&curr_open_fds, NULL);
5283 if (res < 0) {
5284 xerrno = errno;
5285
5286 (void) pr_trace_msg(trace_channel, 29,
5287 "error obtaining open fds rlimit: %s", strerror(xerrno));
5288 max_open_fds = 0;
5289
5290 } else {
5291 /* Allow for the Big Three (stdio), logging, and spares. */
5292 if (curr_open_fds > 8) {
5293 max_open_fds = (uint32_t) curr_open_fds - 8;
5294 }
5295 }
5296
5297 /* Older versions of OpenSSH just accepted our (larger) limits as-is,
5298 * but then ran into client-side policies which forbade the use of such
5299 * larger SFTP requests; it was a bug that was fixed in OpenSSH 9.2p1 and
5300 * later.
5301 *
5302 * Unfortunately, this means that we need to be sensitive to those older
5303 * OpenSSH versions, and to send shorter lengths to them. Otherwise, users
5304 * with older OpenSSH packages will be unfairly punished when their clients
5305 * use this extension, then inexplicably fail with
5306 * "Outbound message too long" errors"; see Issue #1288.
5307 */
5308
5309 if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_USE_FULL_FXP_LIMITS) == FALSE) {
5310 /* In these older OpenSSH versions, their max packet is 256K. */
5311 pr_trace_msg(trace_channel, 8,
5312 "using shorter lengths for older OpenSSH client");
5313 max_packet_len = (256 * 1024);
5314 max_read_len = max_write_len = max_packet_len - 1024;
5315 }
5316
5317 pr_trace_msg(trace_channel, 8,
5318 "sending response: EXTENDED_REPLY [email protected]: "
5319 "max-packet-len = %lu, max-read-len = %lu, max-write-len = %lu, "
5320 "max-open-handles = %lu", (unsigned long) max_packet_len,
5321 (unsigned long) max_read_len, (unsigned long) max_write_len,
5322 (unsigned long) max_open_fds);
5323
5324 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_EXTENDED_REPLY);
5325 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
5326 sftp_msg_write_long(&buf, &buflen, max_packet_len);
5327 sftp_msg_write_long(&buf, &buflen, max_read_len);
5328 sftp_msg_write_long(&buf, &buflen, max_write_len);
5329 sftp_msg_write_long(&buf, &buflen, max_open_fds);
5330
5331 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5332 resp->payload = ptr;
5333 resp->payload_sz = (bufsz - buflen);
5334
5335 return fxp_packet_write(resp);
5336 }
5337
5338 static int fxp_handle_ext_posix_rename(struct fxp_packet *fxp, char *src,
5339 char *dst) {
5340 unsigned char *buf, *ptr;
5341 char *args;
5342 const char *abs_src, *reason;
5343 uint32_t buflen, bufsz, status_code;
5344 struct fxp_packet *resp;
5345 cmd_rec *cmd = NULL, *cmd2 = NULL, *cmd3 = NULL;
5346 int res, xerrno = 0;
5347
5348 args = pstrcat(fxp->pool, src, " ", dst, NULL);
5349
5350 pr_scoreboard_entry_update(session.pid,
5351 PR_SCORE_CMD, "%s", "RENAME", NULL, NULL);
5352 pr_scoreboard_entry_update(session.pid,
5353 PR_SCORE_CMD_ARG, "%s", args, NULL, NULL);
5354
5355 pr_proctitle_set("%s - %s: RENAME %s %s", session.user, session.proc_prefix,
5356 src, dst);
5357
5358 cmd = fxp_cmd_alloc(fxp->pool, "RENAME", args);
5359 cmd->cmd_class = CL_MISC|CL_WRITE|CL_SFTP;
5360
5361 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
5362 buf = ptr = palloc(fxp->pool, bufsz);
5363
5364 cmd2 = fxp_cmd_alloc(fxp->pool, C_RNFR, src);
5365 cmd2->cmd_class = CL_MISC|CL_WRITE;
5366 if (pr_cmd_dispatch_phase(cmd2, PRE_CMD, 0) < 0) {
5367 status_code = SSH2_FX_PERMISSION_DENIED;
5368
5369 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5370 "RENAME from '%s' blocked by '%s' handler: %s", src,
5371 (char *) cmd2->argv[0], strerror(errno));
5372
5373 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5374 (unsigned long) status_code, fxp_strerror(status_code));
5375
5376 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
5377 fxp_cmd_dispatch_err(cmd2);
5378
5379 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5380 fxp_strerror(status_code), NULL);
5381
5382 fxp_cmd_dispatch_err(cmd);
5383
5384 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5385 resp->payload = ptr;
5386 resp->payload_sz = (bufsz - buflen);
5387
5388 return fxp_packet_write(resp);
5389 }
5390
5391 src = dir_best_path(fxp->pool, cmd2->arg);
5392 if (src == NULL) {
5393 status_code = SSH2_FX_PERMISSION_DENIED;
5394
5395 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5396 "posix-rename request denied: unable to access path '%s'", cmd2->arg);
5397
5398 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5399 (unsigned long) status_code, fxp_strerror(status_code));
5400
5401 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5402 fxp_strerror(status_code), NULL);
5403
5404 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
5405 fxp_cmd_dispatch_err(cmd2);
5406
5407 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5408 fxp_strerror(status_code), NULL);
5409
5410 fxp_cmd_dispatch_err(cmd);
5411
5412 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5413 resp->payload = ptr;
5414 resp->payload_sz = (bufsz - buflen);
5415
5416 return fxp_packet_write(resp);
5417 }
5418
5419 /* Make sure we store the absolute path for LogFormat %w (Issue #1808). */
5420 abs_src = dir_abs_path(fxp->pool, src, FALSE);
5421 abs_src = pr_fsio_realpath(fxp->pool, abs_src);
5422
5423 if (pr_table_add(session.notes, "mod_core.rnfr-path",
5424 pstrdup(session.pool, abs_src), 0) < 0) {
5425 if (errno != EEXIST) {
5426 pr_trace_msg(trace_channel, 8,
5427 "error setting 'mod_core.rnfr-path' note: %s", strerror(errno));
5428 }
5429 }
5430
5431 cmd3 = fxp_cmd_alloc(fxp->pool, C_RNTO, dst);
5432 cmd3->cmd_class = CL_MISC|CL_WRITE;
5433 if (pr_cmd_dispatch_phase(cmd3, PRE_CMD, 0) < 0) {
5434 status_code = SSH2_FX_PERMISSION_DENIED;
5435
5436 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5437 "RENAME to '%s' blocked by '%s' handler: %s", dst,
5438 (char *) cmd3->argv[0], strerror(errno));
5439
5440 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5441 (unsigned long) status_code, fxp_strerror(status_code));
5442
5443 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(EACCES));
5444 fxp_cmd_dispatch_err(cmd3);
5445
5446 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
5447 fxp_cmd_dispatch_err(cmd2);
5448
5449 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5450 fxp_strerror(status_code), NULL);
5451
5452 fxp_cmd_dispatch_err(cmd);
5453
5454 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5455 resp->payload = ptr;
5456 resp->payload_sz = (bufsz - buflen);
5457
5458 return fxp_packet_write(resp);
5459 }
5460
5461 dst = dir_best_path(fxp->pool, cmd3->arg);
5462 if (dst == NULL) {
5463 status_code = SSH2_FX_PERMISSION_DENIED;
5464
5465 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5466 "posix-rename request denied: unable to access path '%s'", cmd2->arg);
5467
5468 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5469 (unsigned long) status_code, fxp_strerror(status_code));
5470
5471 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5472 fxp_strerror(status_code), NULL);
5473
5474 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(EACCES));
5475 fxp_cmd_dispatch_err(cmd3);
5476
5477 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
5478 fxp_cmd_dispatch_err(cmd2);
5479
5480 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5481 fxp_strerror(status_code), NULL);
5482
5483 fxp_cmd_dispatch_err(cmd);
5484
5485 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5486 resp->payload = ptr;
5487 resp->payload_sz = (bufsz - buflen);
5488
5489 return fxp_packet_write(resp);
5490 }
5491
5492 if (!dir_check(fxp->pool, cmd2, G_DIRS, src, NULL) ||
5493 !dir_check(fxp->pool, cmd3, G_WRITE, dst, NULL)) {
5494 status_code = SSH2_FX_PERMISSION_DENIED;
5495
5496 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5497 "RENAME of '%s' to '%s' blocked by <Limit> configuration",
5498 src, dst);
5499
5500 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5501 (unsigned long) status_code, fxp_strerror(status_code));
5502
5503 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(EACCES));
5504 fxp_cmd_dispatch_err(cmd3);
5505
5506 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
5507 fxp_cmd_dispatch_err(cmd2);
5508
5509 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5510 fxp_strerror(status_code), NULL);
5511
5512 fxp_cmd_dispatch_err(cmd);
5513
5514 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5515 resp->payload = ptr;
5516 resp->payload_sz = (bufsz - buflen);
5517
5518 return fxp_packet_write(resp);
5519 }
5520
5521 if (strcmp(src, dst) == 0) {
5522 xerrno = EEXIST;
5523
5524 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5525 "RENAME of '%s' to same path '%s', rejecting", src, dst);
5526
5527 status_code = fxp_errno2status(xerrno, &reason);
5528
5529 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
5530 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
5531 xerrno);
5532
5533 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(EEXIST));
5534 fxp_cmd_dispatch_err(cmd3);
5535
5536 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(EEXIST));
5537 fxp_cmd_dispatch_err(cmd2);
5538
5539 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5540 reason, NULL);
5541
5542 fxp_cmd_dispatch_err(cmd);
5543
5544 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5545 resp->payload = ptr;
5546 resp->payload_sz = (bufsz - buflen);
5547
5548 return fxp_packet_write(resp);
5549 }
5550
5551 if (fxp_path_pass_regex_filters(fxp->pool, "RENAME", src) < 0 ||
5552 fxp_path_pass_regex_filters(fxp->pool, "RENAME", dst) < 0) {
5553 xerrno = errno;
5554
5555 status_code = fxp_errno2status(xerrno, &reason);
5556
5557 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
5558 (unsigned long) status_code, fxp_strerror(status_code));
5559
5560 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(xerrno));
5561 fxp_cmd_dispatch_err(cmd3);
5562
5563 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(xerrno));
5564 fxp_cmd_dispatch_err(cmd2);
5565
5566 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5567 fxp_strerror(status_code), NULL);
5568
5569 fxp_cmd_dispatch_err(cmd);
5570
5571 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5572 resp->payload = ptr;
5573 resp->payload_sz = (bufsz - buflen);
5574
5575 return fxp_packet_write(resp);
5576 }
5577
5578 res = pr_fsio_rename(src, dst);
5579 if (res < 0) {
5580 if (errno != EXDEV) {
5581 xerrno = errno;
5582
5583 (void) pr_trace_msg("fileperms", 1, "RENAME, user '%s' (UID %s, "
5584 "GID %s): error renaming '%s' to '%s': %s", session.user,
5585 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
5586 src, dst, strerror(xerrno));
5587
5588 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5589 "error renaming '%s' to '%s': %s", src, dst, strerror(xerrno));
5590
5591 errno = xerrno;
5592
5593 } else {
5594 /* In this case, we should manually copy the file from the source
5595 * path to the destination path.
5596 */
5597 errno = 0;
5598
5599 res = pr_fs_copy_file2(src, dst, 0, NULL);
5600 if (res < 0) {
5601 xerrno = errno;
5602
5603 (void) pr_trace_msg("fileperms", 1, "RENAME, user '%s' (UID %s, "
5604 "GID %s): error copying '%s' to '%s': %s", session.user,
5605 pr_uid2str(fxp->pool, session.uid),
5606 pr_gid2str(fxp->pool, session.gid),
5607 src, dst, strerror(xerrno));
5608
5609 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5610 "error copying '%s' to '%s': %s", src, dst, strerror(xerrno));
5611
5612 errno = xerrno;
5613
5614 } else {
5615 /* Once copied, remove the original path. */
5616 if (pr_fsio_unlink(src) < 0) {
5617 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
5618 "error deleting '%s': %s", src, strerror(errno));
5619 }
5620
5621 xerrno = errno = 0;
5622 }
5623 }
5624
5625 } else {
5626 /* No errors. */
5627 xerrno = errno = 0;
5628 }
5629
5630 status_code = fxp_errno2status(xerrno, &reason);
5631
5632 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
5633 "('%s' [%d])", (unsigned long) status_code, reason,
5634 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
5635
5636 /* Clear out any transfer-specific data. */
5637 if (session.xfer.p) {
5638 destroy_pool(session.xfer.p);
5639 }
5640 memset(&session.xfer, 0, sizeof(session.xfer));
5641
5642 /* The timing of these steps may look peculiar, but it's deliberate,
5643 * in order to get the expected log messages in an ExtendedLog.
5644 */
5645
5646 session.xfer.p = make_sub_pool(fxp_pool);
5647 pr_pool_tag(session.xfer.p, "SFTP session transfer pool");
5648 memset(&session.xfer.start_time, 0, sizeof(session.xfer.start_time));
5649 gettimeofday(&session.xfer.start_time, NULL);
5650
5651 session.xfer.path = pstrdup(session.xfer.p, src);
5652
5653 if (xerrno == 0) {
5654 pr_response_add(R_350,
5655 "File or directory exists, ready for destination name");
5656 fxp_cmd_dispatch(cmd2);
5657
5658 } else {
5659 pr_response_add_err(R_550, "%s: %s", (char *) cmd2->argv[0],
5660 strerror(xerrno));
5661 fxp_cmd_dispatch_err(cmd2);
5662 }
5663
5664 session.xfer.path = pstrdup(session.xfer.p, dst);
5665
5666 if (xerrno == 0) {
5667 pr_response_add(R_250, "Rename successful");
5668 fxp_cmd_dispatch(cmd3);
5669
5670 } else {
5671 pr_response_add_err(R_550, "%s: %s", (char *) cmd3->argv[0],
5672 strerror(xerrno));
5673 fxp_cmd_dispatch_err(cmd3);
5674 }
5675
5676 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5677 reason, NULL);
5678 if (xerrno == 0) {
5679 fxp_cmd_dispatch(cmd);
5680
5681 } else {
5682 fxp_cmd_dispatch_err(cmd);
5683 }
5684
5685 /* Clear out any transfer-specific data. */
5686 if (session.xfer.p) {
5687 destroy_pool(session.xfer.p);
5688 }
5689 memset(&session.xfer, 0, sizeof(session.xfer));
5690
5691 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5692 resp->payload = ptr;
5693 resp->payload_sz = (bufsz - buflen);
5694
5695 return fxp_packet_write(resp);
5696 }
5697
5698 #ifdef HAVE_SYS_STATVFS_H
5699
5700 static off_t get_fs_bytes_total(void *ptr) {
5701 # if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && \
5702 defined(SOLARIS2) && !defined(SOLARIS2_5_1) && !defined(SOLARIS2_6) && \
5703 !defined(SOLARIS2_7)
5704 /* Note: somewhere along the way, Sun decided that the prototype for
5705 * its statvfs64(2) function would include a statvfs64_t rather than
5706 * struct statvfs64. In 2.6 and 2.7, it's struct statvfs64, and
5707 * in 8, 9 it's statvfs64_t. This should silence compiler warnings.
5708 * (The statvfs_t will be redefined to a statvfs64_t as appropriate on
5709 * LFS systems).
5710 */
5711 statvfs_t *fs = ptr;
5712 # else
5713 struct statvfs *fs = ptr;
5714 # endif /* LFS && !Solaris 2.5.1 && !Solaris 2.6 && !Solaris 2.7 */
5715
5716 return ((off_t) fs->f_blocks * (off_t) fs->f_frsize);
5717 }
5718
5719 static off_t get_fs_bytes_unused(void *ptr) {
5720 # if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && \
5721 defined(SOLARIS2) && !defined(SOLARIS2_5_1) && !defined(SOLARIS2_6) && \
5722 !defined(SOLARIS2_7)
5723 /* Note: somewhere along the way, Sun decided that the prototype for
5724 * its statvfs64(2) function would include a statvfs64_t rather than
5725 * struct statvfs64. In 2.6 and 2.7, it's struct statvfs64, and
5726 * in 8, 9 it's statvfs64_t. This should silence compiler warnings.
5727 * (The statvfs_t will be redefined to a statvfs64_t as appropriate on
5728 * LFS systems).
5729 */
5730 statvfs_t *fs = ptr;
5731 # else
5732 struct statvfs *fs = ptr;
5733 # endif /* LFS && !Solaris 2.5.1 && !Solaris 2.6 && !Solaris 2.7 */
5734
5735 return ((off_t) fs->f_bavail * (off_t) fs->f_frsize);
5736 }
5737
5738 static off_t get_user_bytes_avail(void *ptr) {
5739 # if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && \
5740 defined(SOLARIS2) && !defined(SOLARIS2_5_1) && !defined(SOLARIS2_6) && \
5741 !defined(SOLARIS2_7)
5742 /* Note: somewhere along the way, Sun decided that the prototype for
5743 * its statvfs64(2) function would include a statvfs64_t rather than
5744 * struct statvfs64. In 2.6 and 2.7, it's struct statvfs64, and
5745 * in 8, 9 it's statvfs64_t. This should silence compiler warnings.
5746 * (The statvfs_t will be redefined to a statvfs64_t as appropriate on
5747 * LFS systems).
5748 */
5749 statvfs_t *fs = ptr;
5750 # else
5751 struct statvfs *fs = ptr;
5752 # endif /* LFS && !Solaris 2.5.1 && !Solaris 2.6 && !Solaris 2.7 */
5753
5754 /* XXX This should use mod_quotatab as well, for user-specific limits/
5755 * tallies.
5756 */
5757
5758 /* Take the total number of blocks, and subtract the difference between
5759 * the total free blocks and the non-root free blocks. That difference
5760 * provides the number of blocks reserved for root. So subtracting those
5761 * reserved blocks from the total blocks yields the user-available blocks.
5762 */
5763 return (((off_t) fs->f_blocks - ((off_t) fs->f_bfree) - (off_t) fs->f_bavail) * (off_t) fs->f_frsize);
5764 }
5765
5766 static off_t get_user_bytes_unused(void *ptr) {
5767 # if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && \
5768 defined(SOLARIS2) && !defined(SOLARIS2_5_1) && !defined(SOLARIS2_6) && \
5769 !defined(SOLARIS2_7)
5770 /* Note: somewhere along the way, Sun decided that the prototype for
5771 * its statvfs64(2) function would include a statvfs64_t rather than
5772 * struct statvfs64. In 2.6 and 2.7, it's struct statvfs64, and
5773 * in 8, 9 it's statvfs64_t. This should silence compiler warnings.
5774 * (The statvfs_t will be redefined to a statvfs64_t as appropriate on
5775 * LFS systems).
5776 */
5777 statvfs_t *fs = ptr;
5778 # else
5779 struct statvfs *fs = ptr;
5780 # endif /* LFS && !Solaris 2.5.1 && !Solaris 2.6 && !Solaris 2.7 */
5781
5782 /* XXX This should use mod_quotatab as well, for user-specific limits/
5783 * tallies.
5784 */
5785
5786 return ((off_t) fs->f_bavail * (off_t) fs->f_frsize);
5787 }
5788
5789 static int fxp_handle_ext_space_avail(struct fxp_packet *fxp,
5790 const char *path) {
5791 unsigned char *buf, *ptr;
5792 const char *real_path = NULL, *reason;
5793 uint32_t buflen, bufsz, status_code;
5794 struct fxp_packet *resp;
5795
5796 # if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && \
5797 defined(SOLARIS2) && !defined(SOLARIS2_5_1) && !defined(SOLARIS2_6) && \
5798 !defined(SOLARIS2_7)
5799 /* Note: somewhere along the way, Sun decided that the prototype for
5800 * its statvfs64(2) function would include a statvfs64_t rather than
5801 * struct statvfs64. In 2.6 and 2.7, it's struct statvfs64, and
5802 * in 8, 9 it's statvfs64_t. This should silence compiler warnings.
5803 * (The statvfs_t will be redefined to a statvfs64_t as appropriate on
5804 * LFS systems).
5805 */
5806 statvfs_t fs;
5807 # else
5808 struct statvfs fs;
5809 # endif /* LFS && !Solaris 2.5.1 && !Solaris 2.6 && !Solaris 2.7 */
5810
5811 pr_trace_msg(trace_channel, 8, "client sent space-available request: "
5812 "path = '%s'", path);
5813
5814 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
5815 buf = ptr = palloc(fxp->pool, bufsz);
5816
5817 real_path = pr_fsio_realpath(fxp->pool, path);
5818 if (real_path != NULL) {
5819 path = real_path;
5820 }
5821
5822 if (statvfs(path, &fs) < 0) {
5823 int xerrno = errno;
5824
5825 pr_trace_msg(trace_channel, 3, "statvfs() error using '%s': %s",
5826 path, strerror(xerrno));
5827
5828 status_code = fxp_errno2status(xerrno, &reason);
5829
5830 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
5831 "('%s' [%d])", (unsigned long) status_code, reason,
5832 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
5833
5834 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5835 reason, NULL);
5836
5837 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5838 resp->payload = ptr;
5839 resp->payload_sz = (bufsz - buflen);
5840
5841 return fxp_packet_write(resp);
5842 }
5843
5844 pr_trace_msg(trace_channel, 8,
5845 "sending response: EXTENDED_REPLY <space-avail data of '%s'>", path);
5846
5847 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_EXTENDED_REPLY);
5848 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
5849
5850 /* Total bytes on device */
5851 sftp_msg_write_long(&buf, &buflen, (uint64_t) get_fs_bytes_total(&fs));
5852
5853 /* Unused bytes on device. */
5854 sftp_msg_write_long(&buf, &buflen, (uint64_t) get_fs_bytes_unused(&fs));
5855
5856 /* Total bytes available to user. */
5857 sftp_msg_write_long(&buf, &buflen, (uint64_t) get_user_bytes_avail(&fs));
5858
5859 /* Unused bytes available to user. */
5860 sftp_msg_write_long(&buf, &buflen, (uint64_t) get_user_bytes_unused(&fs));
5861
5862 sftp_msg_write_int(&buf, &buflen, (uint32_t) fs.f_frsize);
5863
5864 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5865 resp->payload = ptr;
5866 resp->payload_sz = (bufsz - buflen);
5867
5868 return fxp_packet_write(resp);
5869 }
5870
5871 static int fxp_handle_ext_statvfs(struct fxp_packet *fxp, const char *path) {
5872 unsigned char *buf, *ptr;
5873 const char *real_path = NULL, *reason;
5874 uint32_t buflen, bufsz, status_code;
5875 struct fxp_packet *resp;
5876 uint64_t fs_id = 0, fs_flags = 0;
5877
5878 # if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && \
5879 defined(SOLARIS2) && !defined(SOLARIS2_5_1) && !defined(SOLARIS2_6) && \
5880 !defined(SOLARIS2_7)
5881 /* Note: somewhere along the way, Sun decided that the prototype for
5882 * its statvfs64(2) function would include a statvfs64_t rather than
5883 * struct statvfs64. In 2.6 and 2.7, it's struct statvfs64, and
5884 * in 8, 9 it's statvfs64_t. This should silence compiler warnings.
5885 * (The statvfs_t will be redefined to a statvfs64_t as appropriate on
5886 * LFS systems).
5887 */
5888 statvfs_t fs;
5889 # else
5890 struct statvfs fs;
5891 # endif /* LFS && !Solaris 2.5.1 && !Solaris 2.6 && !Solaris 2.7 */
5892
5893 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
5894 buf = ptr = palloc(fxp->pool, bufsz);
5895
5896 real_path = pr_fsio_realpath(fxp->pool, path);
5897 if (real_path != NULL) {
5898 path = real_path;
5899 }
5900
5901 if (statvfs(path, &fs) < 0) {
5902 int xerrno = errno;
5903
5904 pr_trace_msg(trace_channel, 3, "statvfs() error using '%s': %s",
5905 path, strerror(xerrno));
5906
5907 status_code = fxp_errno2status(xerrno, &reason);
5908
5909 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
5910 "('%s' [%d])", (unsigned long) status_code, reason,
5911 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
5912
5913 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
5914 reason, NULL);
5915
5916 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5917 resp->payload = ptr;
5918 resp->payload_sz = (bufsz - buflen);
5919
5920 return fxp_packet_write(resp);
5921 }
5922
5923 pr_trace_msg(trace_channel, 8,
5924 "sending response: EXTENDED_REPLY <statvfs data of '%s'>", path);
5925
5926 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_EXTENDED_REPLY);
5927 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
5928 sftp_msg_write_long(&buf, &buflen, fs.f_bsize);
5929 sftp_msg_write_long(&buf, &buflen, fs.f_frsize);
5930 sftp_msg_write_long(&buf, &buflen, fs.f_blocks);
5931 sftp_msg_write_long(&buf, &buflen, fs.f_bfree);
5932 sftp_msg_write_long(&buf, &buflen, fs.f_bavail);
5933 sftp_msg_write_long(&buf, &buflen, fs.f_files);
5934 sftp_msg_write_long(&buf, &buflen, fs.f_ffree);
5935 sftp_msg_write_long(&buf, &buflen, fs.f_favail);
5936
5937 /* AIX requires this machination because a) its statvfs struct has
5938 * non-standard data types for the fsid value:
5939 *
5940 * https://lists.dulug.duke.edu/pipermail/rpm-devel/2006-July/001236.html
5941 * https://lists.dulug.duke.edu/pipermail/rpm-devel/2006-July/001264.html
5942 * https://lists.dulug.duke.edu/pipermail/rpm-devel/2006-July/001265.html
5943 * https://lists.dulug.duke.edu/pipermail/rpm-devel/2006-July/001268.html
5944 *
5945 * and b) it does not really matter what value is written; the client is
5946 * not going to be able to do much with this value anyway. From that
5947 * perspective, I'm not sure why the OpenSSH extension even includes the
5948 * value in the response (*shrug*).
5949 */
5950 #if !defined(AIX4) && !defined(AIX5)
5951 memcpy(&fs_id, &(fs.f_fsid), sizeof(fs_id));
5952 #endif
5953 sftp_msg_write_long(&buf, &buflen, fs_id);
5954
5955 /* These flags and values are defined by OpenSSH's PROTOCOL document.
5956 *
5957 * Other platforms support more fs.f_flag values than just ST_RDONLY
5958 * and ST_NOSUID, but those are the only two flags handled by OpenSSH;
5959 * thus we cannot simply send fs.f_flag directly to the client as is.
5960 */
5961 #ifdef ST_RDONLY
5962 if (fs.f_flag & ST_RDONLY) {
5963 fs_flags |= SSH2_FXE_STATVFS_ST_RDONLY;
5964 }
5965 #endif
5966
5967 #ifdef ST_NOSUID
5968 if (fs.f_flag & ST_NOSUID) {
5969 fs_flags |= SSH2_FXE_STATVFS_ST_NOSUID;
5970 }
5971 #endif
5972
5973 sftp_msg_write_long(&buf, &buflen, fs_flags);
5974 sftp_msg_write_long(&buf, &buflen, fs.f_namemax);
5975
5976 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
5977 resp->payload = ptr;
5978 resp->payload_sz = (bufsz - buflen);
5979
5980 return fxp_packet_write(resp);
5981 }
5982 #endif /* !HAVE_SYS_STATVFS_H */
5983
5984 #if defined(PR_USE_XATTR)
5985 static int fxp_handle_ext_getxattr(struct fxp_packet *fxp, const char *path,
5986 const char *name, uint32_t valsz) {
5987 ssize_t res;
5988 void *val;
5989 unsigned char *buf, *ptr;
5990 uint32_t buflen, bufsz, status_code;
5991 const char *reason;
5992 struct fxp_packet *resp;
5993
5994 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
5995 buf = ptr = palloc(fxp->pool, bufsz);
5996
5997 if (valsz > FXP_XATTR_VALUE_MAX_LEN) {
5998 int xerrno = EINVAL;
5999
6000 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6001 "getxattr(2) on '%s' for attribute '%s' (%lu bytes) exceeds maximum "
6002 "value size (%lu bytes), denying", path, name, (unsigned long) valsz,
6003 (unsigned long) FXP_XATTR_VALUE_MAX_LEN);
6004
6005 status_code = fxp_errno2status(xerrno, &reason);
6006
6007 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6008 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
6009 xerrno);
6010
6011 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6012 reason, NULL);
6013
6014 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6015 resp->payload = ptr;
6016 resp->payload_sz = (bufsz - buflen);
6017
6018 return fxp_packet_write(resp);
6019 }
6020
6021 /* Now that the value size has been checked, we allocate a new, larger
6022 * buffer for that size.
6023 */
6024 val = pcalloc(fxp->pool, (size_t) valsz+1);
6025
6026 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ + valsz;
6027 buf = ptr = palloc(fxp->pool, bufsz);
6028
6029 res = pr_fsio_lgetxattr(fxp->pool, path, name, val, (size_t) valsz);
6030 if (res < 0) {
6031 int xerrno = errno;
6032
6033 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6034 "getxattr(2) error on '%s' for attribute '%s': %s", path, name,
6035 strerror(xerrno));
6036
6037 status_code = fxp_errno2status(xerrno, &reason);
6038
6039 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6040 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
6041 xerrno);
6042
6043 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6044 reason, NULL);
6045
6046 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6047 resp->payload = ptr;
6048 resp->payload_sz = (bufsz - buflen);
6049
6050 return fxp_packet_write(resp);
6051 }
6052
6053 pr_trace_msg(trace_channel, 8,
6054 "sending response: EXTENDED_REPLY (%lu bytes)", (unsigned long) res);
6055
6056 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_EXTENDED_REPLY);
6057 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
6058 sftp_msg_write_data(&buf, &buflen, val, res, TRUE);
6059
6060 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6061 resp->payload = ptr;
6062 resp->payload_sz = (bufsz - buflen);
6063
6064 return fxp_packet_write(resp);
6065 }
6066
6067 static int fxp_handle_ext_fgetxattr(struct fxp_packet *fxp, const char *handle,
6068 const char *name, uint32_t valsz) {
6069 ssize_t res;
6070 void *val;
6071 unsigned char *buf, *ptr;
6072 uint32_t buflen, bufsz, status_code;
6073 const char *path, *reason;
6074 struct fxp_handle *fxh;
6075 struct fxp_packet *resp;
6076
6077 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
6078 buf = ptr = palloc(fxp->pool, bufsz);
6079
6080 fxh = fxp_handle_get(handle);
6081 if (fxh == NULL) {
6082 pr_trace_msg(trace_channel, 17,
6083 "[email protected]: unable to find handle for name '%s': %s", handle,
6084 strerror(errno));
6085
6086 status_code = SSH2_FX_INVALID_HANDLE;
6087
6088 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6089 (unsigned long) status_code, fxp_strerror(status_code));
6090
6091 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6092 fxp_strerror(status_code), NULL);
6093
6094 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6095 resp->payload = ptr;
6096 resp->payload_sz = (bufsz - buflen);
6097
6098 return fxp_packet_write(resp);
6099 }
6100
6101 if (fxh->dirh != NULL) {
6102 /* Request for extended attributes on a directory handle. It's not
6103 * easy to get the file descriptor on a directory, so we'll just do
6104 * by path instead.
6105 */
6106 return fxp_handle_ext_getxattr(fxp, fxh->fh->fh_path, name, valsz);
6107 }
6108
6109 if (fxh->fh == NULL) {
6110 status_code = SSH2_FX_INVALID_HANDLE;
6111
6112 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6113 (unsigned long) status_code, fxp_strerror(status_code));
6114
6115 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6116 fxp_strerror(status_code), NULL);
6117
6118 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6119 resp->payload = ptr;
6120 resp->payload_sz = (bufsz - buflen);
6121
6122 return fxp_packet_write(resp);
6123 }
6124
6125 path = fxh->fh->fh_path;
6126
6127 if (valsz > FXP_XATTR_VALUE_MAX_LEN) {
6128 int xerrno = EINVAL;
6129
6130 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6131 "fgetxattr(2) on '%s' for attribute '%s' (%lu bytes) exceeds maximum "
6132 "value size (%lu bytes), denying", path, name, (unsigned long) valsz,
6133 (unsigned long) FXP_XATTR_VALUE_MAX_LEN);
6134
6135 status_code = fxp_errno2status(xerrno, &reason);
6136
6137 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6138 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
6139 xerrno);
6140
6141 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6142 reason, NULL);
6143
6144 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6145 resp->payload = ptr;
6146 resp->payload_sz = (bufsz - buflen);
6147
6148 return fxp_packet_write(resp);
6149 }
6150
6151 /* Now that the value size has been checked, we allocate a new, larger
6152 * buffer for that size.
6153 */
6154 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ + valsz;
6155 buf = ptr = palloc(fxp->pool, bufsz);
6156
6157 val = pcalloc(fxp->pool, (size_t) valsz+1);
6158
6159 res = pr_fsio_fgetxattr(fxp->pool, fxh->fh, name, val, (size_t) valsz);
6160 if (res < 0) {
6161 int xerrno = errno;
6162
6163 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6164 "fgetxattr(2) error on '%s' for attribute '%s': %s", path, name,
6165 strerror(xerrno));
6166
6167 status_code = fxp_errno2status(xerrno, &reason);
6168
6169 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6170 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
6171 xerrno);
6172
6173 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6174 reason, NULL);
6175
6176 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6177 resp->payload = ptr;
6178 resp->payload_sz = (bufsz - buflen);
6179
6180 return fxp_packet_write(resp);
6181 }
6182
6183 pr_trace_msg(trace_channel, 8,
6184 "sending response: EXTENDED_REPLY (%lu bytes)", (unsigned long) res);
6185
6186 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_EXTENDED_REPLY);
6187 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
6188 sftp_msg_write_data(&buf, &buflen, val, res, TRUE);
6189
6190 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6191 resp->payload = ptr;
6192 resp->payload_sz = (bufsz - buflen);
6193
6194 return fxp_packet_write(resp);
6195 }
6196
6197 static int fxp_handle_ext_listxattr(struct fxp_packet *fxp, const char *path) {
6198 register unsigned int i;
6199 int res;
6200 unsigned char *buf, *ptr;
6201 uint32_t buflen, bufsz, status_code;
6202 const char *reason;
6203 struct fxp_packet *resp;
6204 array_header *names = NULL;
6205
6206 buflen = bufsz = FXP_RESPONSE_NAME_DEFAULT_SZ;
6207 buf = ptr = palloc(fxp->pool, bufsz);
6208
6209 res = pr_fsio_llistxattr(fxp->pool, path, &names);
6210 if (res < 0) {
6211 int xerrno = errno;
6212
6213 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6214 "listxattr(2) error on '%s': %s", path, strerror(xerrno));
6215
6216 status_code = fxp_errno2status(xerrno, &reason);
6217
6218 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6219 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
6220 xerrno);
6221
6222 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6223 reason, NULL);
6224
6225 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6226 resp->payload = ptr;
6227 resp->payload_sz = (bufsz - buflen);
6228
6229 return fxp_packet_write(resp);
6230 }
6231
6232 pr_trace_msg(trace_channel, 8,
6233 "sending response: EXTENDED_REPLY (%d attribute names)", names->nelts);
6234
6235 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_EXTENDED_REPLY);
6236 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
6237 sftp_msg_write_int(&buf, &buflen, names->nelts);
6238 for (i = 0; i < names->nelts; i++) {
6239 const char *name;
6240
6241 name = ((const char **) names->elts)[i];
6242 sftp_msg_write_string(&buf, &buflen, name);
6243 }
6244
6245 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6246 resp->payload = ptr;
6247 resp->payload_sz = (bufsz - buflen);
6248
6249 return fxp_packet_write(resp);
6250 }
6251
6252 static int fxp_handle_ext_flistxattr(struct fxp_packet *fxp,
6253 const char *handle) {
6254 register unsigned int i;
6255 int res;
6256 unsigned char *buf, *ptr;
6257 uint32_t buflen, bufsz, status_code;
6258 const char *path, *reason;
6259 struct fxp_handle *fxh;
6260 struct fxp_packet *resp;
6261 array_header *names = NULL;
6262
6263 buflen = bufsz = FXP_RESPONSE_NAME_DEFAULT_SZ;
6264 buf = ptr = palloc(fxp->pool, bufsz);
6265
6266 fxh = fxp_handle_get(handle);
6267 if (fxh == NULL) {
6268 pr_trace_msg(trace_channel, 17,
6269 "[email protected]: unable to find handle for name '%s': %s", handle,
6270 strerror(errno));
6271
6272 status_code = SSH2_FX_INVALID_HANDLE;
6273
6274 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6275 (unsigned long) status_code, fxp_strerror(status_code));
6276
6277 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6278 fxp_strerror(status_code), NULL);
6279
6280 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6281 resp->payload = ptr;
6282 resp->payload_sz = (bufsz - buflen);
6283
6284 return fxp_packet_write(resp);
6285 }
6286
6287 if (fxh->dirh != NULL) {
6288 /* Request for extended attributes on a directory handle. It's not
6289 * easy to get the file descriptor on a directory, so we'll just do
6290 * by path instead.
6291 */
6292 return fxp_handle_ext_listxattr(fxp, fxh->fh->fh_path);
6293 }
6294
6295 if (fxh->fh == NULL) {
6296 status_code = SSH2_FX_INVALID_HANDLE;
6297
6298 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6299 (unsigned long) status_code, fxp_strerror(status_code));
6300
6301 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6302 fxp_strerror(status_code), NULL);
6303
6304 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6305 resp->payload = ptr;
6306 resp->payload_sz = (bufsz - buflen);
6307
6308 return fxp_packet_write(resp);
6309 }
6310
6311 path = fxh->fh->fh_path;
6312 res = pr_fsio_flistxattr(fxp->pool, fxh->fh, &names);
6313 if (res < 0) {
6314 int xerrno = errno;
6315
6316 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6317 "flistxattr(2) error on '%s': %s", path, strerror(xerrno));
6318
6319 status_code = fxp_errno2status(xerrno, &reason);
6320
6321 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6322 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
6323 xerrno);
6324
6325 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6326 reason, NULL);
6327
6328 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6329 resp->payload = ptr;
6330 resp->payload_sz = (bufsz - buflen);
6331
6332 return fxp_packet_write(resp);
6333 }
6334
6335 pr_trace_msg(trace_channel, 8,
6336 "sending response: EXTENDED_REPLY (%d attributes)", names->nelts);
6337
6338 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_EXTENDED_REPLY);
6339 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
6340 sftp_msg_write_int(&buf, &buflen, names->nelts);
6341 for (i = 0; i < names->nelts; i++) {
6342 const char *name;
6343
6344 name = ((const char **) names->elts)[i];
6345 sftp_msg_write_string(&buf, &buflen, name);
6346 }
6347
6348 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6349 resp->payload = ptr;
6350 resp->payload_sz = (bufsz - buflen);
6351
6352 return fxp_packet_write(resp);
6353 }
6354
6355 static int fxp_handle_ext_removexattr(struct fxp_packet *fxp, const char *path,
6356 const char *name) {
6357 int res;
6358 unsigned char *buf, *ptr;
6359 uint32_t buflen, bufsz, status_code;
6360 const char *reason;
6361 struct fxp_packet *resp;
6362
6363 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
6364 buf = ptr = palloc(fxp->pool, bufsz);
6365
6366 res = pr_fsio_lremovexattr(fxp->pool, path, name);
6367 if (res < 0) {
6368 int xerrno = errno;
6369
6370 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6371 "removexattr(2) error on '%s' for attribute '%s': %s", path, name,
6372 strerror(xerrno));
6373
6374 status_code = fxp_errno2status(xerrno, &reason);
6375
6376 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6377 "('%s' [%d])", (unsigned long) status_code, reason,
6378 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
6379
6380 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6381 reason, NULL);
6382
6383 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6384 resp->payload = ptr;
6385 resp->payload_sz = (bufsz - buflen);
6386
6387 return fxp_packet_write(resp);
6388 }
6389
6390 status_code = SSH2_FX_OK;
6391 reason = "OK";
6392
6393 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6394 (unsigned long) status_code, reason);
6395
6396 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6397 reason, NULL);
6398
6399 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6400 resp->payload = ptr;
6401 resp->payload_sz = (bufsz - buflen);
6402
6403 return fxp_packet_write(resp);
6404 }
6405
6406 static int fxp_handle_ext_fremovexattr(struct fxp_packet *fxp,
6407 const char *handle, const char *name) {
6408 int res;
6409 unsigned char *buf, *ptr;
6410 uint32_t buflen, bufsz, status_code;
6411 const char *path, *reason;
6412 struct fxp_handle *fxh;
6413 struct fxp_packet *resp;
6414
6415 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
6416 buf = ptr = palloc(fxp->pool, bufsz);
6417
6418 fxh = fxp_handle_get(handle);
6419 if (fxh == NULL) {
6420 pr_trace_msg(trace_channel, 17,
6421 "[email protected]: unable to find handle for name '%s': %s",
6422 handle, strerror(errno));
6423
6424 status_code = SSH2_FX_INVALID_HANDLE;
6425
6426 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6427 (unsigned long) status_code, fxp_strerror(status_code));
6428
6429 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6430 fxp_strerror(status_code), NULL);
6431
6432 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6433 resp->payload = ptr;
6434 resp->payload_sz = (bufsz - buflen);
6435
6436 return fxp_packet_write(resp);
6437 }
6438
6439 if (fxh->dirh != NULL) {
6440 /* Request for extended attributes on a directory handle. It's not
6441 * easy to get the file descriptor on a directory, so we'll just do
6442 * by path instead.
6443 */
6444 return fxp_handle_ext_removexattr(fxp, fxh->fh->fh_path, name);
6445 }
6446
6447 if (fxh->fh == NULL) {
6448 status_code = SSH2_FX_INVALID_HANDLE;
6449
6450 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6451 (unsigned long) status_code, fxp_strerror(status_code));
6452
6453 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6454 fxp_strerror(status_code), NULL);
6455
6456 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6457 resp->payload = ptr;
6458 resp->payload_sz = (bufsz - buflen);
6459
6460 return fxp_packet_write(resp);
6461 }
6462
6463 path = fxh->fh->fh_path;
6464
6465 res = pr_fsio_fremovexattr(fxp->pool, fxh->fh, name);
6466 if (res < 0) {
6467 int xerrno = errno;
6468
6469 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6470 "fremovexattr(2) error on '%s' for attribute '%s': %s", path, name,
6471 strerror(xerrno));
6472
6473 status_code = fxp_errno2status(xerrno, &reason);
6474
6475 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6476 "('%s' [%d])", (unsigned long) status_code, reason,
6477 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
6478
6479 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6480 reason, NULL);
6481
6482 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6483 resp->payload = ptr;
6484 resp->payload_sz = (bufsz - buflen);
6485
6486 return fxp_packet_write(resp);
6487 }
6488
6489 status_code = SSH2_FX_OK;
6490 reason = "OK";
6491
6492 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6493 (unsigned long) status_code, reason);
6494
6495 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6496 reason, NULL);
6497
6498 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6499 resp->payload = ptr;
6500 resp->payload_sz = (bufsz - buflen);
6501
6502 return fxp_packet_write(resp);
6503 }
6504
6505 static int fxp_handle_ext_setxattr(struct fxp_packet *fxp, const char *path,
6506 const char *name, void *val, uint32_t valsz, uint32_t pflags) {
6507 int res, flags = 0;
6508 unsigned char *buf, *ptr;
6509 uint32_t buflen, bufsz, status_code;
6510 const char *reason;
6511 struct fxp_packet *resp;
6512
6513 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
6514 buf = ptr = palloc(fxp->pool, bufsz);
6515
6516 if (pflags & SSH2_FXE_XATTR_CREATE) {
6517 flags |= PR_FSIO_XATTR_FL_CREATE;
6518 }
6519
6520 if (pflags & SSH2_FXE_XATTR_REPLACE) {
6521 flags |= PR_FSIO_XATTR_FL_REPLACE;
6522 }
6523
6524 res = pr_fsio_lsetxattr(fxp->pool, path, name, val, (size_t) valsz, flags);
6525 if (res < 0) {
6526 int xerrno = errno;
6527
6528 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6529 "setxattr(2) error on '%s' for attribute '%s': %s", path, name,
6530 strerror(xerrno));
6531
6532 status_code = fxp_errno2status(xerrno, &reason);
6533
6534 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6535 "('%s' [%d])", (unsigned long) status_code, reason,
6536 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
6537
6538 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6539 reason, NULL);
6540
6541 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6542 resp->payload = ptr;
6543 resp->payload_sz = (bufsz - buflen);
6544
6545 return fxp_packet_write(resp);
6546 }
6547
6548 status_code = SSH2_FX_OK;
6549 reason = "OK";
6550
6551 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6552 (unsigned long) status_code, reason);
6553
6554 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6555 reason, NULL);
6556
6557 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6558 resp->payload = ptr;
6559 resp->payload_sz = (bufsz - buflen);
6560
6561 return fxp_packet_write(resp);
6562 }
6563
6564 static int fxp_handle_ext_fsetxattr(struct fxp_packet *fxp, const char *handle,
6565 const char *name, void *val, uint32_t valsz, uint32_t pflags) {
6566 int res, flags = 0;
6567 unsigned char *buf, *ptr;
6568 uint32_t buflen, bufsz, status_code;
6569 const char *path, *reason;
6570 struct fxp_handle *fxh;
6571 struct fxp_packet *resp;
6572
6573 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
6574 buf = ptr = palloc(fxp->pool, bufsz);
6575
6576 fxh = fxp_handle_get(handle);
6577 if (fxh == NULL) {
6578 pr_trace_msg(trace_channel, 17,
6579 "[email protected]: unable to find handle for name '%s': %s", handle,
6580 strerror(errno));
6581
6582 status_code = SSH2_FX_INVALID_HANDLE;
6583
6584 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6585 (unsigned long) status_code, fxp_strerror(status_code));
6586
6587 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6588 fxp_strerror(status_code), NULL);
6589
6590 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6591 resp->payload = ptr;
6592 resp->payload_sz = (bufsz - buflen);
6593
6594 return fxp_packet_write(resp);
6595 }
6596
6597 if (fxh->dirh != NULL) {
6598 /* Request for extended attributes on a directory handle. It's not
6599 * easy to get the file descriptor on a directory, so we'll just do
6600 * by path instead.
6601 */
6602 return fxp_handle_ext_setxattr(fxp, fxh->fh->fh_path, name, val, valsz,
6603 pflags);
6604 }
6605
6606 if (fxh->fh == NULL) {
6607 status_code = SSH2_FX_INVALID_HANDLE;
6608
6609 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6610 (unsigned long) status_code, fxp_strerror(status_code));
6611
6612 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6613 fxp_strerror(status_code), NULL);
6614
6615 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6616 resp->payload = ptr;
6617 resp->payload_sz = (bufsz - buflen);
6618
6619 return fxp_packet_write(resp);
6620 }
6621
6622 if (pflags & SSH2_FXE_XATTR_CREATE) {
6623 flags |= PR_FSIO_XATTR_FL_CREATE;
6624 }
6625
6626 if (pflags & SSH2_FXE_XATTR_REPLACE) {
6627 flags |= PR_FSIO_XATTR_FL_REPLACE;
6628 }
6629
6630 path = fxh->fh->fh_path;
6631
6632 res = pr_fsio_fsetxattr(fxp->pool, fxh->fh, name, val, (size_t) valsz, flags);
6633 if (res < 0) {
6634 int xerrno = errno;
6635
6636 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6637 "fsetxattr(2) error on '%s' for attribute '%s': %s", path, name,
6638 strerror(xerrno));
6639
6640 status_code = fxp_errno2status(xerrno, &reason);
6641
6642 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6643 "('%s' [%d])", (unsigned long) status_code, reason,
6644 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
6645
6646 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6647 reason, NULL);
6648
6649 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6650 resp->payload = ptr;
6651 resp->payload_sz = (bufsz - buflen);
6652
6653 return fxp_packet_write(resp);
6654 }
6655
6656 status_code = SSH2_FX_OK;
6657 reason = "OK";
6658
6659 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6660 (unsigned long) status_code, reason);
6661
6662 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6663 reason, NULL);
6664
6665 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6666 resp->payload = ptr;
6667 resp->payload_sz = (bufsz - buflen);
6668
6669 return fxp_packet_write(resp);
6670 }
6671 #endif /* PR_USE_XATTR */
6672
6673 static int fxp_handle_ext_user_group_names(struct fxp_packet *fxp) {
6674 unsigned int uid_count, gid_count;
6675 uint32_t uid_datalen, user_nameslen, user_namessz;
6676 unsigned char *uid_data, *user_names, *user_names_ptr;
6677 uint32_t gid_datalen, group_nameslen, group_namessz;
6678 unsigned char *gid_data, *group_names, *group_names_ptr;
6679 uint32_t buflen, bufsz;
6680 unsigned char *buf, *ptr;
6681 struct fxp_packet *resp;
6682
6683 uid_datalen = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
6684 uid_data = sftp_msg_read_data(fxp->pool, &fxp->payload, &fxp->payload_sz,
6685 uid_datalen);
6686 uid_count = uid_datalen / sizeof(uint32_t);
6687 pr_trace_msg(trace_channel, 19, "client requested names for %u UID%s",
6688 uid_count, uid_count != 1 ? "s" : "");
6689
6690 gid_datalen = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
6691 gid_data = sftp_msg_read_data(fxp->pool, &fxp->payload, &fxp->payload_sz,
6692 gid_datalen);
6693 gid_count = gid_datalen / sizeof(uint32_t);
6694 pr_trace_msg(trace_channel, 19, "client requested names for %u GID%s",
6695 gid_count, gid_count != 1 ? "s" : "");
6696
6697 if (uid_datalen > 0) {
6698 user_namessz = user_nameslen = FXP_PACKET_DATA_DEFAULT_SZ;
6699 user_names = user_names_ptr = palloc(fxp->pool, user_namessz);
6700
6701 } else {
6702 user_namessz = user_nameslen = 0;
6703 user_names = user_names_ptr = NULL;
6704 }
6705
6706 while (uid_datalen > 0) {
6707 uint32_t uid;
6708 const char *name;
6709
6710 pr_signals_handle();
6711
6712 uid = sftp_msg_read_int(fxp->pool, &uid_data, &uid_datalen);
6713 name = pr_auth_uid2name(fxp->pool, (uid_t) uid);
6714 if (name == NULL) {
6715 pr_trace_msg(trace_channel, 9,
6716 "unable to resolve UID %lu to user name: %s", (unsigned long) uid,
6717 strerror(errno));
6718 name = pstrdup(fxp->pool, "");
6719
6720 } else {
6721 pr_trace_msg(trace_channel, 19,
6722 "resolved client-requested UID %lu to user '%s'", (unsigned long) uid,
6723 name);
6724 }
6725
6726 sftp_msg_write_data(&user_names, &user_nameslen,
6727 (const unsigned char *) name, strlen(name), TRUE);
6728 }
6729
6730 if (gid_datalen > 0) {
6731 group_namessz = group_nameslen = FXP_PACKET_DATA_DEFAULT_SZ;
6732 group_names = group_names_ptr = palloc(fxp->pool, group_namessz);
6733
6734 } else {
6735 group_namessz = group_nameslen = 0;
6736 group_names = group_names_ptr = NULL;
6737 }
6738
6739 while (gid_datalen > 0) {
6740 uint32_t gid;
6741 const char *name;
6742
6743 pr_signals_handle();
6744
6745 gid = sftp_msg_read_int(fxp->pool, &gid_data, &gid_datalen);
6746 name = pr_auth_gid2name(fxp->pool, (gid_t) gid);
6747 if (name == NULL) {
6748 pr_trace_msg(trace_channel, 9,
6749 "unable to resolve GID %lu to group name: %s", (unsigned long) gid,
6750 strerror(errno));
6751 name = pstrdup(fxp->pool, "");
6752
6753 } else {
6754 pr_trace_msg(trace_channel, 19,
6755 "resolved client-requested GID %lu to group '%s'", (unsigned long) gid,
6756 name);
6757 }
6758
6759 sftp_msg_write_data(&group_names, &group_nameslen,
6760 (const unsigned char *) name, strlen(name), TRUE);
6761 }
6762
6763 buflen = bufsz = (FXP_PACKET_DATA_DEFAULT_SZ * 2);
6764 buf = ptr = palloc(fxp->pool, bufsz);
6765
6766 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_EXTENDED_REPLY);
6767 /* Note: The current OpenSSH spec for this request is missing this
6768 * request ID in its documented response.
6769 */
6770 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
6771 sftp_msg_write_data(&buf, &buflen, user_names_ptr,
6772 user_namessz - user_nameslen, TRUE);
6773 sftp_msg_write_data(&buf, &buflen, group_names_ptr,
6774 group_namessz - group_nameslen, TRUE);
6775
6776 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6777 resp->payload = ptr;
6778 resp->payload_sz = (bufsz - buflen);
6779
6780 return fxp_packet_write(resp);
6781 }
6782
6783 static int fxp_handle_ext_vendor_id(struct fxp_packet *fxp) {
6784 unsigned char *buf, *ptr;
6785 char *vendor_name, *product_name, *product_version;
6786 uint32_t buflen, bufsz, status_code;
6787 uint64_t build_number;
6788 const char *reason;
6789 struct fxp_packet *resp;
6790
6791 vendor_name = sftp_msg_read_string(fxp->pool, &fxp->payload,
6792 &fxp->payload_sz);
6793
6794 product_name = sftp_msg_read_string(fxp->pool, &fxp->payload,
6795 &fxp->payload_sz);
6796
6797 product_version = sftp_msg_read_string(fxp->pool, &fxp->payload,
6798 &fxp->payload_sz);
6799
6800 build_number = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
6801
6802 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
6803 vendor_name = sftp_utf8_decode_str(fxp->pool, vendor_name);
6804 product_name = sftp_utf8_decode_str(fxp->pool, product_name);
6805 product_version = sftp_utf8_decode_str(fxp->pool, product_version);
6806 }
6807
6808 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6809 "client sent 'vendor-id' extension: { vendorName = '%s', "
6810 "productName = '%s', productVersion = '%s', buildNumber = %" PR_LU " }",
6811 vendor_name, product_name, product_version, (pr_off_t) build_number);
6812
6813 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
6814 buf = ptr = palloc(fxp->pool, bufsz);
6815
6816 status_code = SSH2_FX_OK;
6817 reason = "OK";
6818
6819 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6820 (unsigned long) status_code, reason);
6821
6822 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6823 reason, NULL);
6824
6825 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6826 resp->payload = ptr;
6827 resp->payload_sz = (bufsz - buflen);
6828
6829 return fxp_packet_write(resp);
6830 }
6831
6832 static int fxp_handle_ext_version_select(struct fxp_packet *fxp,
6833 char *version_str) {
6834 unsigned char *buf, *ptr;
6835 uint32_t buflen, bufsz, status_code;
6836 const char *reason;
6837 struct fxp_packet *resp;
6838 int res = 0, val = 0;
6839 unsigned int version = 0;
6840
6841 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
6842 buf = ptr = palloc(fxp->pool, bufsz);
6843
6844 if (!allow_version_select) {
6845 int xerrno = EACCES;
6846
6847 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6848 "client sent 'version-select' request at inappropriate time, rejecting");
6849
6850 status_code = SSH2_FX_FAILURE;
6851 reason = "Failure";
6852
6853 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6854 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
6855 xerrno);
6856
6857 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6858 reason, NULL);
6859
6860 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6861 resp->payload = ptr;
6862 resp->payload_sz = (bufsz - buflen);
6863
6864 (void) fxp_packet_write(resp);
6865
6866 errno = EINVAL;
6867 return -1;
6868 }
6869
6870 val = atoi(version_str);
6871 if (val < 0) {
6872 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6873 "client requested invalid SFTP protocol version %d via 'version-select'",
6874 val);
6875 res = -1;
6876 }
6877
6878 version = val;
6879
6880 if (res == 0 &&
6881 version > fxp_max_client_version) {
6882 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6883 "client requested SFTP protocol version %u via 'version-select', "
6884 "which exceeds SFTPClientMatch max SFTP protocol version %u, rejecting",
6885 version, fxp_max_client_version);
6886 res = -1;
6887 }
6888
6889 if (res == 0 &&
6890 version < fxp_min_client_version) {
6891 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6892 "client requested SFTP protocol version %u via 'version-select', "
6893 "which is less than SFTPClientMatch min SFTP protocol version %u, "
6894 "rejecting", version, fxp_min_client_version);
6895 res = -1;
6896 }
6897
6898 #ifndef PR_USE_NLS
6899 /* If NLS supported was enabled in the proftpd build, then we can support
6900 * UTF8, and thus every other version of SFTP. Otherwise, we can only
6901 * support up to version 3.
6902 */
6903 if (res == 0 &&
6904 version > 3) {
6905 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6906 "client requested SFTP protocol version %u via 'version-select', "
6907 "but we can only support protocol version 3 due to lack of "
6908 "UTF8 support (requires --enable-nls)", version);
6909 res = -1;
6910 }
6911 #endif
6912
6913 if (res < 0) {
6914 int xerrno = EINVAL;
6915
6916 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
6917 "client sent 'version-select' request at inappropriate time, rejecting");
6918
6919 status_code = fxp_errno2status(xerrno, &reason);
6920
6921 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
6922 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
6923 xerrno);
6924
6925 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6926 reason, NULL);
6927
6928 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6929 resp->payload = ptr;
6930 resp->payload_sz = (bufsz - buflen);
6931
6932 (void) fxp_packet_write(resp);
6933
6934 errno = EINVAL;
6935 return -1;
6936 }
6937
6938 pr_trace_msg(trace_channel, 7, "client requested switch to SFTP protocol "
6939 "version %u via 'version-select'", version);
6940 fxp_session->client_version = (unsigned long) version;
6941
6942 status_code = SSH2_FX_OK;
6943 reason = "OK";
6944
6945 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
6946 (unsigned long) status_code, reason);
6947
6948 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
6949 reason, NULL);
6950
6951 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
6952 resp->payload = ptr;
6953 resp->payload_sz = (bufsz - buflen);
6954
6955 allow_version_select = FALSE;
6956 return fxp_packet_write(resp);
6957 }
6958
6959 /* Request handlers */
6960
6961 static int fxp_handle_close(struct fxp_packet *fxp) {
6962 int xerrno = 0, res = 0;
6963 unsigned char *buf, *ptr;
6964 char *name;
6965 const char *reason;
6966 uint32_t buflen, bufsz, status_code;
6967 struct fxp_handle *fxh;
6968 struct fxp_packet *resp;
6969 cmd_rec *cmd;
6970
6971 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
6972 cmd = fxp_cmd_alloc(fxp->pool, "CLOSE", name);
6973
6974 /* Set the command class to MISC for now; we'll change it later to
6975 * READ or WRITE once we know which it is.
6976 */
6977 cmd->cmd_class = CL_MISC|CL_SFTP;
6978 cmd->cmd_id = SFTP_CMD_ID;
6979
6980 pr_scoreboard_entry_update(session.pid,
6981 PR_SCORE_CMD, "%s", "CLOSE", NULL, NULL);
6982 pr_scoreboard_entry_update(session.pid,
6983 PR_SCORE_CMD_ARG, "%s", name, NULL, NULL);
6984
6985 pr_proctitle_set("%s - %s: CLOSE %s", session.user, session.proc_prefix,
6986 name);
6987
6988 pr_trace_msg(trace_channel, 7, "received request: CLOSE %s", name);
6989
6990 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
6991 buf = ptr = palloc(fxp->pool, bufsz);
6992
6993 fxh = fxp_handle_get(name);
6994 if (fxh == NULL) {
6995 pr_trace_msg(trace_channel, 17,
6996 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
6997 name, strerror(errno));
6998
6999 status_code = SSH2_FX_INVALID_HANDLE;
7000
7001 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7002 (unsigned long) status_code, fxp_strerror(status_code));
7003
7004 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7005 fxp_strerror(status_code), NULL);
7006
7007 fxp_cmd_dispatch_err(cmd);
7008
7009 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7010 resp->payload = ptr;
7011 resp->payload_sz = (bufsz - buflen);
7012
7013 return fxp_packet_write(resp);
7014 }
7015
7016 if (fxh->dirh == NULL &&
7017 fxh->fh == NULL) {
7018 status_code = SSH2_FX_INVALID_HANDLE;
7019
7020 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7021 (unsigned long) status_code, fxp_strerror(status_code));
7022
7023 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7024 fxp_strerror(status_code), NULL);
7025
7026 fxp_handle_delete(fxh);
7027 destroy_pool(fxh->pool);
7028
7029 fxp_cmd_dispatch_err(cmd);
7030
7031 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7032 resp->payload = ptr;
7033 resp->payload_sz = (bufsz - buflen);
7034
7035 return fxp_packet_write(resp);
7036 }
7037
7038 /* Add a note containing the file handle for logging (Bug#3707). */
7039 fxp_set_filehandle_note(cmd, fxh);
7040
7041 pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);
7042
7043 if (fxh->fh != NULL) {
7044 const char *real_path = NULL;
7045 char *curr_path = NULL;
7046 cmd_rec *cmd2 = NULL;
7047
7048 curr_path = pstrdup(fxp->pool, fxh->fh->fh_path);
7049 real_path = curr_path;
7050
7051 if (fxh->xfer.path_hidden != NULL) {
7052 real_path = fxh->xfer.path_hidden;
7053 }
7054
7055 /* Set session.curr_cmd appropriately here, for any FSIO callbacks. */
7056 if (fxh->fh_flags & O_APPEND) {
7057 cmd->cmd_class &= ~CL_MISC;
7058 cmd->cmd_class |= CL_WRITE;
7059 session.curr_cmd = C_APPE;
7060
7061 } else if ((fxh->fh_flags & O_WRONLY) ||
7062 (fxh->fh_flags & O_RDWR)) {
7063 cmd->cmd_class &= ~CL_MISC;
7064 cmd->cmd_class |= CL_WRITE;
7065 session.curr_cmd = C_STOR;
7066
7067 } else if (fxh->fh_flags == O_RDONLY) {
7068 cmd->cmd_class &= ~CL_MISC;
7069 cmd->cmd_class |= CL_READ;
7070 session.curr_cmd = C_RETR;
7071 }
7072
7073 res = pr_fsio_close(fxh->fh);
7074 xerrno = errno;
7075
7076 session.curr_cmd = "CLOSE";
7077
7078 pr_scoreboard_entry_update(session.pid,
7079 PR_SCORE_CMD_ARG, "%s", real_path, NULL, NULL);
7080
7081 if (fxh->xfer.path_hidden != NULL &&
7082 res == 0) {
7083 /* This is a HiddenStores file, and needs to be renamed to the real
7084 * path.
7085 */
7086
7087 pr_trace_msg(trace_channel, 8, "renaming HiddenStores path '%s' to '%s'",
7088 curr_path, real_path);
7089
7090 res = pr_fsio_rename(curr_path, real_path);
7091 if (res < 0) {
7092 xerrno = errno;
7093
7094 pr_log_pri(PR_LOG_WARNING, "Rename of %s to %s failed: %s",
7095 curr_path, real_path, strerror(xerrno));
7096
7097 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
7098 "renaming of HiddenStore path '%s' to '%s' failed: %s",
7099 curr_path, real_path, strerror(xerrno));
7100
7101 pr_fsio_unlink(curr_path);
7102 }
7103 }
7104
7105 if (fxh->fh_flags & O_APPEND) {
7106 cmd2 = fxp_cmd_alloc(fxp->pool, C_APPE, pstrdup(fxp->pool, real_path));
7107 cmd2->cmd_id = pr_cmd_get_id(C_APPE);
7108
7109 if (pr_table_add(cmd2->notes, "mod_xfer.store-path",
7110 pstrdup(fxp->pool, real_path), 0) < 0) {
7111 if (errno != EEXIST) {
7112 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
7113 "error adding 'mod_xfer.store-path' note: %s", strerror(errno));
7114 }
7115 }
7116
7117 } else if ((fxh->fh_flags & O_WRONLY) ||
7118 (fxh->fh_flags & O_RDWR)) {
7119 cmd2 = fxp_cmd_alloc(fxp->pool, C_STOR, pstrdup(fxp->pool, real_path));
7120 cmd2->cmd_id = pr_cmd_get_id(C_STOR);
7121
7122 if (pr_table_add(cmd2->notes, "mod_xfer.store-path",
7123 pstrdup(fxp->pool, real_path), 0) < 0) {
7124 if (errno != EEXIST) {
7125 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
7126 "error adding 'mod_xfer.store-path' note: %s", strerror(errno));
7127 }
7128 }
7129
7130 } else if (fxh->fh_flags == O_RDONLY) {
7131 cmd2 = fxp_cmd_alloc(fxp->pool, C_RETR, pstrdup(fxp->pool, real_path));
7132 cmd2->cmd_id = pr_cmd_get_id(C_RETR);
7133
7134 if (pr_table_add(cmd2->notes, "mod_xfer.retr-path",
7135 pstrdup(fxp->pool, real_path), 0) < 0) {
7136 if (errno != EEXIST) {
7137 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
7138 "error adding 'mod_xfer.retr-path' note: %s", strerror(errno));
7139 }
7140 }
7141 }
7142
7143 fxh->fh = NULL;
7144
7145 /* Before we dispatch to the RETR/STOR handlers, make a copy of the
7146 * session.xfer.path variable (and others). The RETR/STOR handlers will
7147 * clear out the session.xfer.p pool, but we will want to put that path
7148 * back, after the RETR/STOR handlers, for the sake of logging e.g. a %f
7149 * LogFormat variable for the CLOSE request.
7150 */
7151
7152 fxp_set_filehandle_sess_xfer(fxh);
7153
7154 if (cmd2 != NULL) {
7155 if (fxh->fh_existed &&
7156 (pr_cmd_cmp(cmd2, PR_CMD_STOR_ID) == 0 ||
7157 pr_cmd_cmp(cmd2, PR_CMD_APPE_ID) == 0)) {
7158
7159 /* Clear any existing key in the notes. */
7160 (void) pr_table_remove(cmd->notes, "mod_xfer.file-modified", NULL);
7161
7162 if (pr_table_add(cmd->notes, "mod_xfer.file-modified",
7163 pstrdup(cmd->pool, "true"), 0) < 0) {
7164 if (errno != EEXIST) {
7165 pr_log_pri(PR_LOG_NOTICE,
7166 "notice: error adding 'mod_xfer.file-modified' note: %s",
7167 strerror(errno));
7168 }
7169 }
7170
7171 /* Clear any existing key in the notes. */
7172 (void) pr_table_remove(cmd2->notes, "mod_xfer.file-modified", NULL);
7173
7174 if (pr_table_add(cmd2->notes, "mod_xfer.file-modified",
7175 pstrdup(cmd2->pool, "true"), 0) < 0) {
7176 if (errno != EEXIST) {
7177 pr_log_pri(PR_LOG_NOTICE,
7178 "notice: error adding 'mod_xfer.file-modified' note: %s",
7179 strerror(errno));
7180 }
7181 }
7182 }
7183
7184 if (res < 0 &&
7185 xerrno != EOF) {
7186 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
7187 fxp_cmd_dispatch_err(cmd2);
7188
7189 } else {
7190 pr_response_add(R_226, "%s", "Transfer complete");
7191 fxp_cmd_dispatch(cmd2);
7192 }
7193 }
7194
7195 } else if (fxh->dirh != NULL) {
7196 cmd_rec *cmd2;
7197
7198 cmd2 = fxp_cmd_alloc(fxp->pool, C_MLSD, (char *) fxh->dir);
7199 cmd2->cmd_class = CL_DIRS;
7200 cmd2->cmd_id = pr_cmd_get_id(C_MLSD);
7201
7202 pr_scoreboard_entry_update(session.pid,
7203 PR_SCORE_CMD_ARG, "%s", fxh->dir, NULL, NULL);
7204
7205 res = pr_fsio_closedir(fxh->dirh);
7206 if (res < 0) {
7207 xerrno = errno;
7208
7209 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
7210 "error closing directory '%s': %s", fxh->dir, strerror(xerrno));
7211 fxp_cmd_dispatch_err(cmd2);
7212
7213 } else {
7214 fxp_cmd_dispatch(cmd2);
7215 }
7216
7217 fxh->dirh = NULL;
7218 }
7219
7220 if (res < 0) {
7221 status_code = fxp_errno2status(xerrno, &reason);
7222
7223 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
7224 "('%s' [%d])", (unsigned long) status_code, reason,
7225 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
7226
7227 } else {
7228 errno = 0;
7229 status_code = fxp_errno2status(0, &reason);
7230
7231 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7232 (unsigned long) status_code, reason);
7233 }
7234
7235 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7236 reason, NULL);
7237
7238 /* Now re-populate the session.xfer struct, for mod_log's handling of
7239 * the CLOSE request.
7240 */
7241 fxp_set_filehandle_sess_xfer(fxh);
7242
7243 if (res < 0) {
7244 fxp_cmd_dispatch_err(cmd);
7245
7246 } else {
7247 fxp_cmd_dispatch(cmd);
7248 }
7249
7250 fxp_handle_delete(fxh);
7251 destroy_pool(fxh->pool);
7252
7253 /* Clear out session.xfer again. */
7254 if (session.xfer.p != NULL) {
7255 destroy_pool(session.xfer.p);
7256 }
7257 memset(&session.xfer, 0, sizeof(session.xfer));
7258
7259 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7260 resp->payload = ptr;
7261 resp->payload_sz = (bufsz - buflen);
7262
7263 return fxp_packet_write(resp);
7264 }
7265
7266 static int fxp_handle_extended(struct fxp_packet *fxp) {
7267 int res;
7268 unsigned char *buf, *ptr;
7269 char *ext_request_name;
7270 uint32_t buflen, bufsz, status_code;
7271 struct fxp_packet *resp;
7272 cmd_rec *cmd;
7273
7274 ext_request_name = sftp_msg_read_string(fxp->pool, &fxp->payload,
7275 &fxp->payload_sz);
7276
7277 cmd = fxp_cmd_alloc(fxp->pool, "EXTENDED", ext_request_name);
7278 cmd->cmd_class = CL_MISC|CL_SFTP;
7279 cmd->cmd_id = SFTP_CMD_ID;
7280
7281 pr_scoreboard_entry_update(session.pid,
7282 PR_SCORE_CMD, "%s", "EXTENDED", NULL, NULL);
7283 pr_scoreboard_entry_update(session.pid,
7284 PR_SCORE_CMD_ARG, "%s", ext_request_name, NULL, NULL);
7285
7286 pr_proctitle_set("%s - %s: EXTENDED %s", session.user, session.proc_prefix,
7287 ext_request_name);
7288
7289 pr_trace_msg(trace_channel, 7, "received request: EXTENDED %s",
7290 ext_request_name);
7291
7292 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
7293 buf = ptr = palloc(fxp->pool, bufsz);
7294
7295 /* We always handle an EXTENDED vendor-id request from the client; the
7296 * client is telling us its vendor information; it is not requesting that
7297 * we send our vendor information.
7298 */
7299 if (strcmp(ext_request_name, "vendor-id") == 0) {
7300 res = fxp_handle_ext_vendor_id(fxp);
7301 if (res == 0) {
7302 fxp_cmd_dispatch(cmd);
7303
7304 } else {
7305 fxp_cmd_dispatch_err(cmd);
7306 }
7307
7308 return res;
7309 }
7310
7311 if ((fxp_ext_flags & SFTP_FXP_EXT_VERSION_SELECT) &&
7312 strcmp(ext_request_name, "version-select") == 0) {
7313 char *version_str;
7314
7315 version_str = sftp_msg_read_string(fxp->pool, &fxp->payload,
7316 &fxp->payload_sz);
7317
7318 res = fxp_handle_ext_version_select(fxp, version_str);
7319 if (res == 0) {
7320 fxp_cmd_dispatch(cmd);
7321
7322 } else {
7323 fxp_cmd_dispatch_err(cmd);
7324 }
7325
7326 return res;
7327 }
7328
7329 if ((fxp_ext_flags & SFTP_FXP_EXT_CHECK_FILE) &&
7330 strcmp(ext_request_name, "check-file-name") == 0) {
7331 char *path, *digest_list;
7332 off_t offset, len;
7333 uint32_t blocksz;
7334
7335 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7336 digest_list = sftp_msg_read_string(fxp->pool, &fxp->payload,
7337 &fxp->payload_sz);
7338 offset = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
7339 len = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
7340 blocksz = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
7341
7342 res = fxp_handle_ext_check_file(fxp, digest_list, path, offset, len,
7343 blocksz);
7344 if (res == 0) {
7345 fxp_cmd_dispatch(cmd);
7346
7347 } else {
7348 fxp_cmd_dispatch_err(cmd);
7349 }
7350
7351 return res;
7352 }
7353
7354 if ((fxp_ext_flags & SFTP_FXP_EXT_CHECK_FILE) &&
7355 strcmp(ext_request_name, "check-file-handle") == 0) {
7356 char *handle, *path, *digest_list;
7357 off_t offset, len;
7358 uint32_t blocksz;
7359 struct fxp_handle *fxh;
7360
7361 handle = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7362
7363 fxh = fxp_handle_get(handle);
7364 if (fxh == NULL ||
7365 fxh->dirh != NULL) {
7366 status_code = SSH2_FX_INVALID_HANDLE;
7367
7368 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7369 (unsigned long) status_code, fxp_strerror(status_code));
7370
7371 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7372 fxp_strerror(status_code), NULL);
7373
7374 fxp_cmd_dispatch_err(cmd);
7375
7376 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7377 resp->payload = ptr;
7378 resp->payload_sz = (bufsz - buflen);
7379
7380 return fxp_packet_write(resp);
7381 }
7382
7383 /* Make sure the file was opened with read permissions; if it was opened
7384 * write-only, for example, we need to return EACCES.
7385 */
7386 if (fxh->fh_flags & O_WRONLY) {
7387 status_code = SSH2_FX_PERMISSION_DENIED;
7388
7389 pr_trace_msg(trace_channel, 9, "file %s opened write-only, "
7390 "unable to obtain file checksum (%s)", fxh->fh->fh_path,
7391 strerror(EACCES));
7392
7393 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7394 (unsigned long) status_code, fxp_strerror(status_code));
7395
7396 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7397 fxp_strerror(status_code), NULL);
7398
7399 fxp_cmd_dispatch_err(cmd);
7400
7401 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7402 resp->payload = ptr;
7403 resp->payload_sz = (bufsz - buflen);
7404
7405 return fxp_packet_write(resp);
7406 }
7407
7408 path = fxh->fh->fh_path;
7409
7410 digest_list = sftp_msg_read_string(fxp->pool, &fxp->payload,
7411 &fxp->payload_sz);
7412 offset = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
7413 len = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
7414 blocksz = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
7415
7416 res = fxp_handle_ext_check_file(fxp, digest_list, path, offset, len,
7417 blocksz);
7418 if (res == 0) {
7419 fxp_cmd_dispatch(cmd);
7420
7421 } else {
7422 fxp_cmd_dispatch_err(cmd);
7423 }
7424
7425 return res;
7426 }
7427
7428 if ((fxp_ext_flags & SFTP_FXP_EXT_COPY_FILE) &&
7429 strcmp(ext_request_name, "copy-file") == 0) {
7430 char *src, *dst;
7431 int overwrite;
7432
7433 src = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7434 dst = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7435 overwrite = sftp_msg_read_bool(fxp->pool, &fxp->payload, &fxp->payload_sz);
7436
7437 res = fxp_handle_ext_copy_file(fxp, src, dst, overwrite);
7438 if (res == 0) {
7439 fxp_cmd_dispatch(cmd);
7440
7441 } else {
7442 fxp_cmd_dispatch_err(cmd);
7443 }
7444
7445 return res;
7446 }
7447
7448 if ((fxp_ext_flags & SFTP_FXP_EXT_FSYNC) &&
7449 strcmp(ext_request_name, "[email protected]") == 0) {
7450 const char *handle;
7451 struct fxp_handle *fxh;
7452
7453 handle = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7454
7455 fxh = fxp_handle_get(handle);
7456 if (fxh == NULL) {
7457 pr_trace_msg(trace_channel, 17,
7458 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
7459 handle, strerror(errno));
7460
7461 status_code = SSH2_FX_INVALID_HANDLE;
7462
7463 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7464 (unsigned long) status_code, fxp_strerror(status_code));
7465
7466 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7467 fxp_strerror(status_code), NULL);
7468
7469 fxp_cmd_dispatch_err(cmd);
7470
7471 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7472 resp->payload = ptr;
7473 resp->payload_sz = (bufsz - buflen);
7474
7475 return fxp_packet_write(resp);
7476 }
7477
7478 if (fxh->fh == NULL) {
7479 errno = EISDIR;
7480
7481 pr_trace_msg(trace_channel, 17,
7482 "%s: handle '%s': %s", (char *) cmd->argv[0], handle, strerror(errno));
7483
7484 status_code = SSH2_FX_INVALID_HANDLE;
7485
7486 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7487 (unsigned long) status_code, fxp_strerror(status_code));
7488
7489 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7490 fxp_strerror(status_code), NULL);
7491
7492 fxp_cmd_dispatch_err(cmd);
7493
7494 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7495 resp->payload = ptr;
7496 resp->payload_sz = (bufsz - buflen);
7497
7498 return fxp_packet_write(resp);
7499 }
7500
7501 res = fxp_handle_ext_fsync(fxp, fxh);
7502 if (res == 0) {
7503 fxp_cmd_dispatch(cmd);
7504
7505 } else {
7506 fxp_cmd_dispatch_err(cmd);
7507 }
7508
7509 return res;
7510 }
7511
7512 if ((fxp_ext_flags & SFTP_FXP_EXT_HARDLINK) &&
7513 strcmp(ext_request_name, "[email protected]") == 0) {
7514 char *src, *dst;
7515
7516 src = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7517 dst = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7518
7519 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
7520 src = sftp_utf8_decode_str(fxp->pool, src);
7521 dst = sftp_utf8_decode_str(fxp->pool, dst);
7522 }
7523
7524 res = fxp_handle_ext_hardlink(fxp, src, dst);
7525 if (res == 0) {
7526 fxp_cmd_dispatch(cmd);
7527
7528 } else {
7529 fxp_cmd_dispatch_err(cmd);
7530 }
7531
7532 return res;
7533 }
7534
7535 if ((fxp_ext_flags & SFTP_FXP_EXT_HOMEDIR) &&
7536 strcmp(ext_request_name, "home-directory") == 0) {
7537 const char *name;
7538
7539 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7540 if (strcmp(name, "") == 0) {
7541 name = session.user;
7542 }
7543
7544 res = fxp_handle_ext_homedir(fxp, name);
7545 if (res == 0) {
7546 fxp_cmd_dispatch(cmd);
7547
7548 } else {
7549 fxp_cmd_dispatch_err(cmd);
7550 }
7551
7552 return res;
7553 }
7554
7555 if ((fxp_ext_flags & SFTP_FXP_EXT_LIMITS) &&
7556 strcmp(ext_request_name, "[email protected]") == 0) {
7557 res = fxp_handle_ext_limits(fxp);
7558 if (res == 0) {
7559 fxp_cmd_dispatch(cmd);
7560
7561 } else {
7562 fxp_cmd_dispatch_err(cmd);
7563 }
7564
7565 return res;
7566 }
7567
7568 if ((fxp_ext_flags & SFTP_FXP_EXT_POSIX_RENAME) &&
7569 strcmp(ext_request_name, "[email protected]") == 0) {
7570 char *src, *dst;
7571
7572 src = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7573 dst = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7574
7575 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
7576 src = sftp_utf8_decode_str(fxp->pool, src);
7577 dst = sftp_utf8_decode_str(fxp->pool, dst);
7578 }
7579
7580 res = fxp_handle_ext_posix_rename(fxp, src, dst);
7581 if (res == 0) {
7582 fxp_cmd_dispatch(cmd);
7583
7584 } else {
7585 fxp_cmd_dispatch_err(cmd);
7586 }
7587
7588 return res;
7589 }
7590
7591 #if defined(HAVE_SYS_STATVFS_H)
7592 if ((fxp_ext_flags & SFTP_FXP_EXT_SPACE_AVAIL) &&
7593 strcmp(ext_request_name, "space-available") == 0) {
7594 const char *path;
7595
7596 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7597
7598 res = fxp_handle_ext_space_avail(fxp, path);
7599 if (res == 0) {
7600 fxp_cmd_dispatch(cmd);
7601
7602 } else {
7603 fxp_cmd_dispatch_err(cmd);
7604 }
7605
7606 return res;
7607 }
7608
7609 if ((fxp_ext_flags & SFTP_FXP_EXT_STATVFS) &&
7610 strcmp(ext_request_name, "[email protected]") == 0) {
7611 const char *path;
7612
7613 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7614
7615 res = fxp_handle_ext_statvfs(fxp, path);
7616 if (res == 0) {
7617 fxp_cmd_dispatch(cmd);
7618
7619 } else {
7620 fxp_cmd_dispatch_err(cmd);
7621 }
7622
7623 return res;
7624 }
7625
7626 if ((fxp_ext_flags & SFTP_FXP_EXT_STATVFS) &&
7627 strcmp(ext_request_name, "[email protected]") == 0) {
7628 const char *handle, *path;
7629 struct fxp_handle *fxh;
7630
7631 handle = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7632
7633 fxh = fxp_handle_get(handle);
7634 if (fxh == NULL) {
7635 pr_trace_msg(trace_channel, 17,
7636 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
7637 handle, strerror(errno));
7638
7639 status_code = SSH2_FX_INVALID_HANDLE;
7640
7641 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7642 (unsigned long) status_code, fxp_strerror(status_code));
7643
7644 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7645 fxp_strerror(status_code), NULL);
7646
7647 fxp_cmd_dispatch_err(cmd);
7648
7649 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7650 resp->payload = ptr;
7651 resp->payload_sz = (bufsz - buflen);
7652
7653 return fxp_packet_write(resp);
7654 }
7655
7656 path = fxh->fh ? fxh->fh->fh_path : fxh->dir;
7657
7658 res = fxp_handle_ext_statvfs(fxp, path);
7659 if (res == 0) {
7660 fxp_cmd_dispatch(cmd);
7661
7662 } else {
7663 fxp_cmd_dispatch_err(cmd);
7664 }
7665
7666 return res;
7667 }
7668 #endif /* HAVE_SYS_STATVFS_H */
7669
7670 if ((fxp_ext_flags & SFTP_FXP_EXT_USERGROUPNAMES) &&
7671 strcmp(ext_request_name, "[email protected]") == 0) {
7672 res = fxp_handle_ext_user_group_names(fxp);
7673 if (res == 0) {
7674 fxp_cmd_dispatch(cmd);
7675
7676 } else {
7677 fxp_cmd_dispatch_err(cmd);
7678 }
7679
7680 return res;
7681 }
7682
7683 #if defined(PR_USE_XATTR)
7684 if (fxp_ext_flags & SFTP_FXP_EXT_XATTR) {
7685 if (strcmp(ext_request_name, "[email protected]") == 0) {
7686 const char *handle, *name;
7687 uint32_t valsz;
7688
7689 handle = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7690 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7691 valsz = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
7692
7693 res = fxp_handle_ext_fgetxattr(fxp, handle, name, valsz);
7694 if (res == 0) {
7695 fxp_cmd_dispatch(cmd);
7696
7697 } else {
7698 fxp_cmd_dispatch_err(cmd);
7699 }
7700
7701 return res;
7702 }
7703
7704 if (strcmp(ext_request_name, "[email protected]") == 0) {
7705 const char *handle;
7706
7707 handle = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7708
7709 res = fxp_handle_ext_flistxattr(fxp, handle);
7710 if (res == 0) {
7711 fxp_cmd_dispatch(cmd);
7712
7713 } else {
7714 fxp_cmd_dispatch_err(cmd);
7715 }
7716
7717 return res;
7718 }
7719
7720 if (strcmp(ext_request_name, "[email protected]") == 0) {
7721 const char *handle, *name;
7722
7723 handle = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7724 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7725
7726 res = fxp_handle_ext_fremovexattr(fxp, handle, name);
7727 if (res == 0) {
7728 fxp_cmd_dispatch(cmd);
7729
7730 } else {
7731 fxp_cmd_dispatch_err(cmd);
7732 }
7733
7734 return res;
7735 }
7736
7737 if (strcmp(ext_request_name, "[email protected]") == 0) {
7738 const char *handle, *name;
7739 void *val;
7740 uint32_t pflags, valsz;
7741
7742 handle = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7743 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7744 valsz = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
7745 val = (void *) sftp_msg_read_data(fxp->pool, &fxp->payload,
7746 &fxp->payload_sz, valsz);
7747 pflags = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
7748
7749 res = fxp_handle_ext_fsetxattr(fxp, handle, name, val, valsz, pflags);
7750 if (res == 0) {
7751 fxp_cmd_dispatch(cmd);
7752
7753 } else {
7754 fxp_cmd_dispatch_err(cmd);
7755 }
7756
7757 return res;
7758 }
7759
7760 if (strcmp(ext_request_name, "[email protected]") == 0) {
7761 const char *path, *name;
7762 uint32_t valsz;
7763
7764 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7765 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7766 valsz = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
7767
7768 res = fxp_handle_ext_getxattr(fxp, path, name, valsz);
7769 if (res == 0) {
7770 fxp_cmd_dispatch(cmd);
7771
7772 } else {
7773 fxp_cmd_dispatch_err(cmd);
7774 }
7775
7776 return res;
7777 }
7778
7779 if (strcmp(ext_request_name, "[email protected]") == 0) {
7780 const char *path;
7781
7782 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7783
7784 res = fxp_handle_ext_listxattr(fxp, path);
7785 if (res == 0) {
7786 fxp_cmd_dispatch(cmd);
7787
7788 } else {
7789 fxp_cmd_dispatch_err(cmd);
7790 }
7791
7792 return res;
7793 }
7794
7795 if (strcmp(ext_request_name, "[email protected]") == 0) {
7796 const char *path, *name;
7797
7798 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7799 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7800
7801 res = fxp_handle_ext_removexattr(fxp, path, name);
7802 if (res == 0) {
7803 fxp_cmd_dispatch(cmd);
7804
7805 } else {
7806 fxp_cmd_dispatch_err(cmd);
7807 }
7808
7809 return res;
7810 }
7811
7812 if (strcmp(ext_request_name, "[email protected]") == 0) {
7813 const char *path, *name;
7814 void *val;
7815 uint32_t pflags, valsz;
7816
7817 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7818 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7819 valsz = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
7820 val = (void *) sftp_msg_read_data(fxp->pool, &fxp->payload,
7821 &fxp->payload_sz, valsz);
7822 pflags = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
7823
7824 res = fxp_handle_ext_setxattr(fxp, path, name, val, valsz, pflags);
7825 if (res == 0) {
7826 fxp_cmd_dispatch(cmd);
7827
7828 } else {
7829 fxp_cmd_dispatch_err(cmd);
7830 }
7831
7832 return res;
7833 }
7834 }
7835 #endif /* PR_USE_XATTR */
7836
7837 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
7838 "client requested '%s' extension, rejecting", ext_request_name);
7839 status_code = SSH2_FX_OP_UNSUPPORTED;
7840
7841 fxp_cmd_dispatch_err(cmd);
7842
7843 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7844 (unsigned long) status_code, fxp_strerror(status_code));
7845
7846 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7847 fxp_strerror(status_code), NULL);
7848
7849 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7850 resp->payload = ptr;
7851 resp->payload_sz = (bufsz - buflen);
7852
7853 return fxp_packet_write(resp);
7854 }
7855
7856 static int fxp_handle_fsetstat(struct fxp_packet *fxp) {
7857 unsigned char *buf, *ptr;
7858 char *attrs_str, *cmd_name, *name, *path;
7859 const char *reason;
7860 uint32_t attr_flags, buflen, bufsz, status_code;
7861 int have_error = FALSE, res;
7862 struct stat *attrs;
7863 struct fxp_handle *fxh;
7864 struct fxp_packet *resp;
7865 cmd_rec *cmd;
7866 array_header *xattrs = NULL;
7867
7868 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
7869
7870 cmd = fxp_cmd_alloc(fxp->pool, "FSETSTAT", name);
7871 cmd->cmd_class = CL_WRITE|CL_SFTP;
7872 cmd->cmd_id = SFTP_CMD_ID;
7873
7874 pr_scoreboard_entry_update(session.pid,
7875 PR_SCORE_CMD, "%s", "FSETSTAT", NULL, NULL);
7876 pr_scoreboard_entry_update(session.pid,
7877 PR_SCORE_CMD_ARG, "%s", name, NULL, NULL);
7878
7879 attrs = fxp_attrs_read(fxp, &fxp->payload, &fxp->payload_sz, &attr_flags,
7880 &xattrs);
7881 if (attrs == NULL) {
7882 fxp_cmd_dispatch_err(cmd);
7883
7884 /* XXX TODO: Provide a response to the client here! */
7885 return 0;
7886 }
7887
7888 attrs_str = fxp_strattrs(fxp->pool, attrs, &attr_flags);
7889
7890 pr_proctitle_set("%s - %s: FSETSTAT %s %s", session.user, session.proc_prefix,
7891 name, attrs_str);
7892
7893 pr_trace_msg(trace_channel, 7, "received request: FSETSTAT %s %s", name,
7894 attrs_str);
7895
7896 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
7897 buf = ptr = palloc(fxp->pool, bufsz);
7898
7899 fxh = fxp_handle_get(name);
7900 if (fxh == NULL) {
7901 pr_trace_msg(trace_channel, 17,
7902 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
7903 name, strerror(errno));
7904
7905 status_code = SSH2_FX_INVALID_HANDLE;
7906
7907 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7908 (unsigned long) status_code, fxp_strerror(status_code));
7909
7910 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7911 fxp_strerror(status_code), NULL);
7912
7913 fxp_cmd_dispatch_err(cmd);
7914
7915 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7916 resp->payload = ptr;
7917 resp->payload_sz = (bufsz - buflen);
7918
7919 return fxp_packet_write(resp);
7920 }
7921
7922 /* Add a note containing the file handle for logging (Bug#3707). */
7923 fxp_set_filehandle_note(cmd, fxh);
7924
7925 cmd->arg = pstrdup(cmd->pool, (fxh->fh ? fxh->fh->fh_path : fxh->dir));
7926
7927 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
7928 status_code = SSH2_FX_PERMISSION_DENIED;
7929
7930 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
7931 "FSETSTAT of '%s' blocked by '%s' handler: %s", cmd->arg,
7932 (char *) cmd->argv[0], strerror(errno));
7933
7934 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7935 (unsigned long) status_code, fxp_strerror(status_code));
7936
7937 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7938 fxp_strerror(status_code), NULL);
7939
7940 fxp_cmd_dispatch_err(cmd);
7941
7942 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7943 resp->payload = ptr;
7944 resp->payload_sz = (bufsz - buflen);
7945
7946 return fxp_packet_write(resp);
7947 }
7948
7949 /* The path may have been changed by any PRE_CMD handlers. */
7950 path = dir_best_path(fxp->pool, cmd->arg);
7951 if (path == NULL) {
7952 status_code = SSH2_FX_PERMISSION_DENIED;
7953
7954 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
7955 "FSETSTAT request denied: unable to access path '%s'", cmd->arg);
7956
7957 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7958 (unsigned long) status_code, fxp_strerror(status_code));
7959
7960 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
7961 fxp_strerror(status_code), NULL);
7962
7963 fxp_cmd_dispatch_err(cmd);
7964
7965 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
7966 resp->payload = ptr;
7967 resp->payload_sz = (bufsz - buflen);
7968
7969 return fxp_packet_write(resp);
7970 }
7971
7972 cmd_name = cmd->argv[0];
7973
7974 pr_cmd_set_name(cmd, "FSETSTAT");
7975 if (dir_check(fxp->pool, cmd, G_WRITE, path, NULL) > 0) {
7976 /* Explicitly allowed by <Limit FSETSTAT>. */
7977 have_error = FALSE;
7978
7979 } else {
7980 pr_cmd_set_name(cmd, "SETSTAT");
7981
7982 if (!dir_check(fxp->pool, cmd, G_WRITE, path, NULL)) {
7983 have_error = TRUE;
7984 }
7985 }
7986
7987 if (have_error) {
7988 status_code = SSH2_FX_PERMISSION_DENIED;
7989
7990 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
7991 "FSETSTAT of '%s' blocked by <Limit %s> configuration", path,
7992 (char *) cmd->argv[0]);
7993
7994 pr_cmd_set_name(cmd, cmd_name);
7995
7996 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
7997 (unsigned long) status_code, fxp_strerror(status_code));
7998
7999 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8000 fxp_strerror(status_code), NULL);
8001
8002 fxp_cmd_dispatch_err(cmd);
8003
8004 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8005 resp->payload = ptr;
8006 resp->payload_sz = (bufsz - buflen);
8007
8008 return fxp_packet_write(resp);
8009 }
8010 pr_cmd_set_name(cmd, cmd_name);
8011
8012 attr_flags = fxp_attrs_clear_unsupported(attr_flags);
8013
8014 /* If the SFTPOption for ignoring the owners for SFTP setstat requests is set,
8015 * handle it by clearing the SSH2_FX_ATTR_UIDGID and SSH2_FX_ATTR_OWNERGROUP
8016 * flags.
8017 */
8018 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_SET_OWNERS) &&
8019 ((attr_flags & SSH2_FX_ATTR_UIDGID) ||
8020 (attr_flags & SSH2_FX_ATTR_OWNERGROUP))) {
8021 pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSFTPSetOwners' "
8022 "configured, ignoring ownership sent by client");
8023 attr_flags &= ~SSH2_FX_ATTR_UIDGID;
8024 attr_flags &= ~SSH2_FX_ATTR_OWNERGROUP;
8025 }
8026
8027 /* If the SFTPOption for ignoring the xattrs for SFTP setstat requests is set,
8028 * handle it by clearing the SSH2_FX_ATTR_EXTENDED flag.
8029 */
8030 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_SET_XATTRS) &&
8031 (attr_flags & SSH2_FX_ATTR_EXTENDED)) {
8032 pr_trace_msg(trace_channel, 7,
8033 "SFTPOption 'IgnoreSFTPSetExtendedAttributes' configured, ignoring "
8034 "xattrs sent by client");
8035 attr_flags &= ~SSH2_FX_ATTR_EXTENDED;
8036 }
8037
8038 /* If the SFTPOption for ignoring the perms for SFTP setstat requests is set,
8039 * handle it by clearing the SSH2_FX_ATTR_PERMISSIONS flag.
8040 */
8041 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_SET_PERMS) &&
8042 (attr_flags & SSH2_FX_ATTR_PERMISSIONS)) {
8043 pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSFTPSetPerms' "
8044 "configured, ignoring perms sent by client");
8045 attr_flags &= ~SSH2_FX_ATTR_PERMISSIONS;
8046 }
8047
8048 /* If the SFTPOption for ignoring the times for SFTP setstat requests is set,
8049 * handle it by clearing the time-related SSH2_FX_ATTR flags.
8050 */
8051 if (sftp_opts & SFTP_OPT_IGNORE_SFTP_SET_TIMES) {
8052 if ((attr_flags & SSH2_FX_ATTR_ACCESSTIME) ||
8053 (attr_flags & SSH2_FX_ATTR_MODIFYTIME)) {
8054 pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSFTPSetTimes' "
8055 "configured, ignoring times sent by client");
8056 attr_flags &= ~SSH2_FX_ATTR_ACCESSTIME;
8057 attr_flags &= ~SSH2_FX_ATTR_MODIFYTIME;
8058 }
8059 }
8060
8061 if (fxh->fh != NULL) {
8062 res = fxp_attrs_set(fxh->fh, fxh->fh->fh_path, attrs, attr_flags, xattrs,
8063 &buf, &buflen, fxp);
8064
8065 } else {
8066 res = fxp_attrs_set(NULL, fxh->dir, attrs, attr_flags, xattrs, &buf,
8067 &buflen, fxp);
8068 }
8069
8070 if (res < 0) {
8071 int xerrno = errno;
8072
8073 status_code = fxp_errno2status(xerrno, &reason);
8074
8075 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
8076 "('%s' [%d])", (unsigned long) status_code, reason,
8077 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
8078
8079 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8080 reason, NULL);
8081
8082 fxp_cmd_dispatch_err(cmd);
8083
8084 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8085 resp->payload = ptr;
8086 resp->payload_sz = (bufsz - buflen);
8087
8088 return fxp_packet_write(resp);
8089 }
8090
8091 status_code = fxp_errno2status(0, &reason);
8092
8093 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8094 (unsigned long) status_code, reason);
8095
8096 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8097 reason, NULL);
8098
8099 fxp_cmd_dispatch(cmd);
8100
8101 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8102 resp->payload = ptr;
8103 resp->payload_sz = (bufsz - buflen);
8104
8105 return fxp_packet_write(resp);
8106 }
8107
8108 static int fxp_handle_fstat(struct fxp_packet *fxp) {
8109 unsigned char *buf;
8110 char *cmd_name, *name;
8111 uint32_t attr_flags, buflen;
8112 struct stat st;
8113 struct fxp_buffer *fxb;
8114 struct fxp_handle *fxh;
8115 struct fxp_packet *resp;
8116 cmd_rec *cmd;
8117 const char *fake_user = NULL, *fake_group = NULL;
8118
8119 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
8120
8121 cmd = fxp_cmd_alloc(fxp->pool, "FSTAT", name);
8122 cmd->cmd_class = CL_READ|CL_SFTP;
8123 cmd->cmd_id = SFTP_CMD_ID;
8124
8125 pr_scoreboard_entry_update(session.pid,
8126 PR_SCORE_CMD, "%s", "FSTAT", NULL, NULL);
8127 pr_scoreboard_entry_update(session.pid,
8128 PR_SCORE_CMD_ARG, "%s", name, NULL, NULL);
8129
8130 pr_proctitle_set("%s - %s: FSTAT %s", session.user, session.proc_prefix,
8131 name);
8132
8133 if (fxp_session->client_version > 3) {
8134 attr_flags = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
8135
8136 pr_trace_msg(trace_channel, 7, "received request: FSTAT %s %s", name,
8137 fxp_strattrflags(fxp->pool, attr_flags));
8138
8139 } else {
8140 pr_trace_msg(trace_channel, 7, "received request: FSTAT %s", name);
8141 attr_flags = SSH2_FX_ATTR_SIZE|SSH2_FX_ATTR_UIDGID|SSH2_FX_ATTR_PERMISSIONS|
8142 SSH2_FX_ATTR_ACMODTIME;
8143 #if defined(PR_USE_XATTR)
8144 if (!(fxp_fsio_opts & PR_FSIO_OPT_IGNORE_XATTR)) {
8145 attr_flags |= SSH2_FX_ATTR_EXTENDED;
8146 }
8147 #endif /* PR_USE_XATTR */
8148 }
8149
8150 fxb = pcalloc(fxp->pool, sizeof(struct fxp_buffer));
8151 fxb->bufsz = buflen = FXP_RESPONSE_NAME_DEFAULT_SZ;
8152 fxb->ptr = buf = palloc(fxp->pool, fxb->bufsz);
8153
8154 fxh = fxp_handle_get(name);
8155 if (fxh == NULL) {
8156 uint32_t status_code;
8157
8158 pr_trace_msg(trace_channel, 17,
8159 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
8160 name, strerror(errno));
8161
8162 status_code = SSH2_FX_INVALID_HANDLE;
8163
8164 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8165 (unsigned long) status_code, fxp_strerror(status_code));
8166
8167 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8168 fxp_strerror(status_code), NULL);
8169
8170 fxp_cmd_dispatch_err(cmd);
8171
8172 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8173 resp->payload = fxb->ptr;
8174 resp->payload_sz = (fxb->bufsz - buflen);
8175
8176 return fxp_packet_write(resp);
8177 }
8178
8179 if (fxh->fh == NULL) {
8180 uint32_t status_code = SSH2_FX_INVALID_HANDLE;
8181
8182 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8183 (unsigned long) status_code, fxp_strerror(status_code));
8184
8185 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8186 fxp_strerror(status_code), NULL);
8187
8188 fxp_cmd_dispatch_err(cmd);
8189
8190 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8191 resp->payload = fxb->ptr;
8192 resp->payload_sz = (fxb->bufsz - buflen);
8193
8194 return fxp_packet_write(resp);
8195 }
8196
8197 /* Add a note containing the file handle for logging (Bug#3707). */
8198 fxp_set_filehandle_note(cmd, fxh);
8199
8200 cmd_name = cmd->argv[0];
8201 pr_cmd_set_name(cmd, "FSTAT");
8202
8203 if (!dir_check(fxp->pool, cmd, G_NONE, fxh->fh->fh_path, NULL)) {
8204 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
8205
8206 pr_cmd_set_name(cmd, cmd_name);
8207
8208 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8209 "FSTAT of '%s' blocked by <Limit> configuration", fxh->fh->fh_path);
8210
8211 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8212 (unsigned long) status_code, fxp_strerror(status_code));
8213
8214 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8215 fxp_strerror(status_code), NULL);
8216
8217 fxp_cmd_dispatch_err(cmd);
8218
8219 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8220 resp->payload = fxb->ptr;
8221 resp->payload_sz = (fxb->bufsz - buflen);
8222
8223 return fxp_packet_write(resp);
8224 }
8225 pr_cmd_set_name(cmd, cmd_name);
8226
8227 if (pr_fsio_fstat(fxh->fh, &st) < 0) {
8228 uint32_t status_code;
8229 const char *reason;
8230 int xerrno = errno;
8231
8232 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8233 "error checking '%s' for FSTAT: %s", fxh->fh->fh_path, strerror(xerrno));
8234
8235 status_code = fxp_errno2status(xerrno, &reason);
8236
8237 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
8238 "('%s' [%d])", (unsigned long) status_code, reason,
8239 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
8240
8241 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8242 reason, NULL);
8243
8244 fxp_cmd_dispatch_err(cmd);
8245
8246 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8247 resp->payload = fxb->ptr;
8248 resp->payload_sz = (fxb->bufsz - buflen);
8249
8250 return fxp_packet_write(resp);
8251 }
8252
8253 pr_trace_msg(trace_channel, 8, "sending response: ATTRS %s",
8254 fxp_strattrs(fxp->pool, &st, NULL));
8255
8256 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_ATTRS);
8257 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
8258
8259 fake_user = get_param_ptr(get_dir_ctxt(fxp->pool, fxh->fh->fh_path),
8260 "DirFakeUser", FALSE);
8261 if (fake_user != NULL &&
8262 strcmp(fake_user, "~") == 0) {
8263 fake_user = session.user;
8264 }
8265
8266 fake_group = get_param_ptr(get_dir_ctxt(fxp->pool, fxh->fh->fh_path),
8267 "DirFakeGroup", FALSE);
8268 if (fake_group != NULL &&
8269 strcmp(fake_group, "~") == 0) {
8270 fake_group = session.group;
8271 }
8272
8273 fxb->buf = buf;
8274 fxb->buflen = buflen;
8275
8276 attr_flags = fxp_attrs_clear_unsupported(attr_flags);
8277 if (fxp_session->client_version > 3 &&
8278 sftp_opts & SFTP_OPT_INCLUDE_SFTP_TIMES) {
8279 pr_trace_msg(trace_channel, 17,
8280 "SFTPOption IncludeSFTPTimes in effect; assuring presence of "
8281 "ACCESSTIME/MODIFYTIME attributes");
8282 attr_flags |= SSH2_FX_ATTR_ACCESSTIME;
8283 attr_flags |= SSH2_FX_ATTR_MODIFYTIME;
8284 }
8285
8286 fxp_attrs_write(fxp->pool, fxb, fxh->fh->fh_path, &st, attr_flags, fake_user,
8287 fake_group);
8288
8289 /* fxp_attrs_write will have changed the buf/buflen values in the buffer. */
8290 buf = fxb->buf;
8291 buflen = fxb->buflen;
8292
8293 fxp_cmd_dispatch(cmd);
8294
8295 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8296 resp->payload = fxb->ptr;
8297 resp->payload_sz = (fxb->bufsz - buflen);
8298
8299 return fxp_packet_write(resp);
8300 }
8301
8302 static int fxp_handle_init(struct fxp_packet *fxp) {
8303 char version_str[16];
8304 unsigned char *buf, *ptr;
8305 uint32_t buflen, bufsz, client_version;
8306 struct fxp_packet *resp;
8307 cmd_rec *cmd;
8308 config_rec *c;
8309
8310 client_version = fxp_session->client_version = sftp_msg_read_int(fxp->pool,
8311 &fxp->payload, &fxp->payload_sz);
8312
8313 memset(version_str, '\0', sizeof(version_str));
8314 pr_snprintf(version_str, sizeof(version_str)-1, "%lu",
8315 (unsigned long) fxp_session->client_version);
8316
8317 cmd = fxp_cmd_alloc(fxp->pool, "INIT", version_str);
8318 cmd->cmd_class = CL_MISC|CL_SFTP;
8319 cmd->cmd_id = SFTP_CMD_ID;
8320
8321 pr_scoreboard_entry_update(session.pid,
8322 PR_SCORE_CMD, "%s", "INIT", NULL, NULL);
8323 pr_scoreboard_entry_update(session.pid,
8324 PR_SCORE_CMD_ARG, "%s", version_str, NULL, NULL);
8325
8326 pr_proctitle_set("%s - %s: INIT %s", session.user, session.proc_prefix,
8327 version_str);
8328
8329 pr_trace_msg(trace_channel, 7, "received request: INIT %lu",
8330 (unsigned long) fxp_session->client_version);
8331
8332 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
8333 buf = ptr = palloc(fxp->pool, bufsz);
8334
8335 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_VERSION);
8336
8337 if (fxp_session->client_version > fxp_max_client_version) {
8338 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8339 "client requested SFTP protocol version %lu, which exceeds "
8340 "SFTPClientMatch max SFTP protocol version %u, using protocol version %u",
8341 (unsigned long) fxp_session->client_version, fxp_max_client_version,
8342 fxp_max_client_version);
8343 fxp_session->client_version = fxp_max_client_version;
8344 }
8345
8346 if (fxp_session->client_version < fxp_min_client_version) {
8347 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8348 "client requested SFTP protocol version %lu, which is less than "
8349 "SFTPClientMatch min SFTP protocol version %u, using protocol version %u",
8350 (unsigned long) fxp_session->client_version, fxp_min_client_version,
8351 fxp_min_client_version);
8352 fxp_session->client_version = fxp_min_client_version;
8353 }
8354
8355 #if !defined(PR_USE_NLS)
8356 /* If NLS supported was enabled in the proftpd build, then we can support
8357 * UTF8, and thus every other version of SFTP. Otherwise, we can only
8358 * support up to version 3.
8359 */
8360 if (fxp_session->client_version > 3) {
8361 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8362 "client requested SFTP protocol version %lu, but we can only support "
8363 "protocol version 3 due to lack of UTF8 support (requires --enable-nls)",
8364 (unsigned long) fxp_session->client_version);
8365 fxp_session->client_version = 3;
8366 }
8367 #endif /* PR_USE_NLS */
8368
8369 /* If the lowest protocol version we want to support is still higher than
8370 * the client-requested protocol version, then we will -- sadly -- abruptly
8371 * disconnect the client. See Issue #1200 for the reasons why.
8372 */
8373 if (fxp_session->client_version > client_version) {
8374 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8375 "client-requested SFTP protocol version (%lu) is lower than we can "
8376 "support (%lu), disconnecting client", (unsigned long) client_version,
8377 (unsigned long) fxp_session->client_version);
8378 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED,
8379 "Unable to use requested SFTP protocol version");
8380 }
8381
8382 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8383 "using SFTP protocol version %lu for this session (channel ID %lu)",
8384 (unsigned long) fxp_session->client_version,
8385 (unsigned long) fxp->channel_id);
8386
8387 pr_trace_msg(trace_channel, 8, "sending response: VERSION %lu",
8388 (unsigned long) fxp_session->client_version);
8389
8390 sftp_msg_write_int(&buf, &buflen, fxp_session->client_version);
8391
8392 fxp_version_add_version_ext(fxp->pool, &buf, &buflen);
8393
8394 if (fxp_session->client_version >= 4) {
8395 fxp_version_add_newline_ext(fxp->pool, &buf, &buflen);
8396 }
8397
8398 if (fxp_session->client_version == 5) {
8399 fxp_version_add_supported_ext(fxp->pool, &buf, &buflen);
8400 }
8401
8402 if (fxp_session->client_version >= 6) {
8403 fxp_version_add_supported2_ext(fxp->pool, &buf, &buflen);
8404 }
8405
8406 fxp_version_add_std_exts(fxp->pool, &buf, &buflen);
8407 fxp_version_add_openssh_exts(fxp->pool, &buf, &buflen);
8408
8409 /* Look up the FSOptions here, for use later (Issue #593). We do not need
8410 * set these for the FSIO API; that is already done by mod_core. Instead,
8411 * we look them up for ourselves, for our own consumption/use.
8412 */
8413 c = find_config(main_server->conf, CONF_PARAM, "FSOptions", FALSE);
8414 while (c != NULL) {
8415 unsigned long opts = 0;
8416
8417 pr_signals_handle();
8418
8419 opts = *((unsigned long *) c->argv[0]);
8420 fxp_fsio_opts |= opts;
8421
8422 c = find_config_next(c, c->next, CONF_PARAM, "FSOptions", FALSE);
8423 }
8424
8425 pr_event_generate("mod_sftp.sftp.protocol-version",
8426 &(fxp_session->client_version));
8427
8428 fxp_cmd_dispatch(cmd);
8429
8430 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8431 resp->payload = ptr;
8432 resp->payload_sz = (bufsz - buflen);
8433
8434 return fxp_packet_write(resp);
8435 }
8436
8437 static int fxp_handle_link(struct fxp_packet *fxp) {
8438 unsigned char *buf, *ptr;
8439 char *args, *cmd_name, *link_path, *target_path;
8440 const char *reason;
8441 char is_symlink;
8442 int have_error = FALSE, res;
8443 uint32_t buflen, bufsz, status_code;
8444 struct fxp_packet *resp;
8445 cmd_rec *cmd;
8446
8447 link_path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
8448 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
8449 link_path = sftp_utf8_decode_str(fxp->pool, link_path);
8450 }
8451
8452 target_path = sftp_msg_read_string(fxp->pool, &fxp->payload,
8453 &fxp->payload_sz);
8454 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
8455 target_path = sftp_utf8_decode_str(fxp->pool, target_path);
8456 }
8457
8458 args = pstrcat(fxp->pool, link_path, " ", target_path, NULL);
8459
8460 cmd = fxp_cmd_alloc(fxp->pool, "LINK", args);
8461 cmd->cmd_class = CL_WRITE|CL_SFTP;
8462 cmd->cmd_id = SFTP_CMD_ID;
8463
8464 pr_scoreboard_entry_update(session.pid,
8465 PR_SCORE_CMD, "%s", "LINK", NULL, NULL);
8466 pr_scoreboard_entry_update(session.pid,
8467 PR_SCORE_CMD_ARG, "%s", args, NULL, NULL);
8468
8469 is_symlink = sftp_msg_read_byte(fxp->pool, &fxp->payload, &fxp->payload_sz);
8470
8471 pr_proctitle_set("%s - %s: LINK %s %s %s", session.user, session.proc_prefix,
8472 link_path, target_path, is_symlink ? "true" : "false");
8473
8474 pr_trace_msg(trace_channel, 7, "received request: LINK %s %s %s", link_path,
8475 target_path, is_symlink ? "true" : "false");
8476
8477 if (strlen(link_path) == 0) {
8478 /* Use the default directory if the path is empty. */
8479 link_path = sftp_auth_get_default_dir();
8480
8481 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8482 "empty link path given in LINK request, using '%s'", link_path);
8483 }
8484
8485 if (strlen(target_path) == 0) {
8486 /* Use the default directory if the path is empty. */
8487 target_path = sftp_auth_get_default_dir();
8488
8489 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8490 "empty target path given in LINK request, using '%s'", target_path);
8491 }
8492
8493 /* Make sure we use the full paths. */
8494 link_path = dir_canonical_vpath(fxp->pool, link_path);
8495 target_path = dir_canonical_vpath(fxp->pool, target_path);
8496
8497 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
8498 buf = ptr = palloc(fxp->pool, bufsz);
8499
8500 cmd_name = cmd->argv[0];
8501 pr_cmd_set_name(cmd, "LINK");
8502
8503 if (!dir_check(fxp->pool, cmd, G_READ, target_path, NULL)) {
8504 have_error = TRUE;
8505 }
8506
8507 if (!have_error) {
8508 if (!dir_check(fxp->pool, cmd, G_WRITE, link_path, NULL)) {
8509 have_error = TRUE;
8510 }
8511 }
8512
8513 if (is_symlink) {
8514 if (!have_error) {
8515 pr_cmd_set_name(cmd, "SYMLINK");
8516
8517 if (!dir_check(fxp->pool, cmd, G_READ, target_path, NULL)) {
8518 have_error = TRUE;
8519 }
8520
8521 if (!have_error) {
8522 if (!dir_check(fxp->pool, cmd, G_WRITE, link_path, NULL)) {
8523 have_error = TRUE;
8524 }
8525 }
8526 }
8527 }
8528
8529 if (have_error) {
8530 status_code = SSH2_FX_PERMISSION_DENIED;
8531
8532 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8533 "LINK of '%s' to '%s' blocked by <Limit %s> configuration",
8534 target_path, link_path, (char *) cmd->argv[0]);
8535
8536 pr_cmd_set_name(cmd, cmd_name);
8537
8538 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8539 (unsigned long) status_code, fxp_strerror(status_code));
8540
8541 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8542 fxp_strerror(status_code), NULL);
8543
8544 fxp_cmd_dispatch_err(cmd);
8545
8546 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8547 resp->payload = ptr;
8548 resp->payload_sz = (bufsz - buflen);
8549
8550 return fxp_packet_write(resp);
8551 }
8552
8553 pr_cmd_set_name(cmd, cmd_name);
8554
8555 if (is_symlink) {
8556 res = pr_fsio_symlink(target_path, link_path);
8557
8558 } else {
8559 res = pr_fsio_link(target_path, link_path);
8560 }
8561
8562 if (res < 0) {
8563 int xerrno = errno;
8564
8565 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8566 "error %s '%s' to '%s': %s", is_symlink ? "symlinking" : "linking",
8567 target_path, link_path, strerror(xerrno));
8568
8569 status_code = fxp_errno2status(xerrno, &reason);
8570
8571 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
8572 "('%s' [%d])", (unsigned long) status_code, reason,
8573 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
8574
8575 fxp_cmd_dispatch_err(cmd);
8576
8577 } else {
8578 errno = 0;
8579 status_code = fxp_errno2status(0, &reason);
8580
8581 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8582 (unsigned long) status_code, reason);
8583
8584 fxp_cmd_dispatch(cmd);
8585 }
8586
8587 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8588 reason, NULL);
8589
8590 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8591 resp->payload = ptr;
8592 resp->payload_sz = (bufsz - buflen);
8593
8594 return fxp_packet_write(resp);
8595 }
8596
8597 static int fxp_handle_lock(struct fxp_packet *fxp) {
8598 unsigned char *buf, *ptr;
8599 char *name;
8600 const char *lock_type_str = NULL;
8601 uint32_t buflen, bufsz, lock_flags, status_code;
8602 uint64_t offset, lock_len;
8603 struct flock lock;
8604 struct fxp_handle *fxh;
8605 struct fxp_packet *resp;
8606 cmd_rec *cmd;
8607
8608 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
8609 offset = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
8610 lock_len = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
8611 lock_flags = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
8612
8613 cmd = fxp_cmd_alloc(fxp->pool, "LOCK", name);
8614 cmd->cmd_class = CL_WRITE|CL_SFTP;
8615 cmd->cmd_id = SFTP_CMD_ID;
8616
8617 pr_scoreboard_entry_update(session.pid,
8618 PR_SCORE_CMD, "%s", "LOCK", NULL, NULL);
8619 pr_scoreboard_entry_update(session.pid,
8620 PR_SCORE_CMD_ARG, "%s", name, NULL, NULL);
8621
8622 pr_proctitle_set("%s - %s: LOCK %s", session.user, session.proc_prefix, name);
8623
8624 pr_trace_msg(trace_channel, 7,
8625 "received request: LOCK %s %" PR_LU " %" PR_LU " %lu",
8626 name, (pr_off_t) offset, (pr_off_t) lock_len, (unsigned long) lock_flags);
8627
8628 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
8629 buf = ptr = palloc(fxp->pool, bufsz);
8630
8631 fxh = fxp_handle_get(name);
8632 if (fxh == NULL) {
8633 pr_trace_msg(trace_channel, 17,
8634 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
8635 name, strerror(errno));
8636
8637 status_code = SSH2_FX_INVALID_HANDLE;
8638
8639 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8640 (unsigned long) status_code, fxp_strerror(status_code));
8641
8642 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8643 fxp_strerror(status_code), NULL);
8644
8645 fxp_cmd_dispatch_err(cmd);
8646
8647 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8648 resp->payload = ptr;
8649 resp->payload_sz = (bufsz - buflen);
8650
8651 return fxp_packet_write(resp);
8652 }
8653
8654 if (fxh->fh == NULL) {
8655 /* We do not support locking of directory handles, only files. */
8656 status_code = SSH2_FX_OP_UNSUPPORTED;
8657
8658 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8659 "client requested unsupported LOCK of a directory, rejecting");
8660
8661 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8662 (unsigned long) status_code, fxp_strerror(status_code));
8663
8664 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8665 fxp_strerror(status_code), NULL);
8666
8667 fxp_cmd_dispatch_err(cmd);
8668
8669 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8670 resp->payload = ptr;
8671 resp->payload_sz = (bufsz - buflen);
8672
8673 return fxp_packet_write(resp);
8674 }
8675
8676 if (!dir_check(fxp->pool, cmd, G_WRITE, fxh->fh->fh_path, NULL)) {
8677 status_code = SSH2_FX_PERMISSION_DENIED;
8678
8679 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8680 "LOCK of '%s' blocked by <Limit> configuration", fxh->fh->fh_path);
8681
8682 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8683 (unsigned long) status_code, fxp_strerror(status_code));
8684
8685 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8686 fxp_strerror(status_code), NULL);
8687
8688 fxp_cmd_dispatch_err(cmd);
8689
8690 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8691 resp->payload = ptr;
8692 resp->payload_sz = (bufsz - buflen);
8693
8694 return fxp_packet_write(resp);
8695 }
8696
8697 pr_scoreboard_entry_update(session.pid,
8698 PR_SCORE_CMD_ARG, "%s", fxh->fh->fh_path, NULL, NULL);
8699
8700 if (lock_flags & SSH2_FXL_DELETE) {
8701 /* The UNLOCK command is used for removing locks, not LOCK. */
8702 status_code = SSH2_FX_OP_UNSUPPORTED;
8703
8704 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8705 "client requested lock removal using LOCK, rejecting");
8706
8707 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8708 (unsigned long) status_code, fxp_strerror(status_code));
8709
8710 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8711 fxp_strerror(status_code), NULL);
8712
8713 fxp_cmd_dispatch_err(cmd);
8714
8715 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8716 resp->payload = ptr;
8717 resp->payload_sz = (bufsz - buflen);
8718
8719 return fxp_packet_write(resp);
8720
8721 } else {
8722 if ((lock_flags & SSH2_FXL_WRITE) &&
8723 (lock_flags & SSH2_FXL_READ)) {
8724 /* We do not support simultaneous read and write locking. */
8725 status_code = SSH2_FX_OP_UNSUPPORTED;
8726
8727 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8728 "client requested unsupported simultaneous read/write LOCK, "
8729 "rejecting");
8730
8731 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8732 (unsigned long) status_code, fxp_strerror(status_code));
8733
8734 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8735 fxp_strerror(status_code), NULL);
8736
8737 fxp_cmd_dispatch_err(cmd);
8738
8739 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8740 resp->payload = ptr;
8741 resp->payload_sz = (bufsz - buflen);
8742
8743 return fxp_packet_write(resp);
8744 }
8745
8746 if (lock_flags & SSH2_FXL_READ) {
8747 lock.l_type = F_RDLCK;
8748 lock_type_str = "read";
8749 }
8750
8751 if (lock_flags & SSH2_FXL_WRITE) {
8752 lock.l_type = F_WRLCK;
8753 lock_type_str = "write";
8754 }
8755 }
8756
8757 lock.l_whence = SEEK_SET;
8758 lock.l_start = offset;
8759 lock.l_len = lock_len;
8760
8761 if (lock_len > 0) {
8762 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8763 "client requested %s locking of '%s' from %" PR_LU " for %" PR_LU
8764 " bytes", lock_type_str, fxh->fh->fh_path, (pr_off_t) offset,
8765 (pr_off_t) lock_len);
8766
8767 } else {
8768 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8769 "client requested %s locking of '%s' from %" PR_LU " to end-of-file",
8770 lock_type_str, fxh->fh->fh_path, (pr_off_t) offset);
8771 }
8772
8773 pr_trace_msg("lock", 9, "attempting to %s lock file '%s'", lock_type_str,
8774 fxh->fh->fh_path);
8775
8776 while (fcntl(fxh->fh->fh_fd, F_SETLKW, &lock) < 0) {
8777 int xerrno;
8778 const char *reason;
8779
8780 if (errno == EINTR) {
8781 pr_signals_handle();
8782 continue;
8783 }
8784
8785 xerrno = errno;
8786 pr_trace_msg("lock", 3, "%s-lock of '%s' failed: %s", lock_type_str,
8787 fxh->fh->fh_path, strerror(errno));
8788
8789 if (errno == EACCES) {
8790 /* Get the PID of the process blocking this lock. */
8791 if (fcntl(fxh->fh->fh_fd, F_GETLK, &lock) == 0) {
8792 pr_trace_msg("lock", 3, "process ID %lu has blocking %s lock on '%s'",
8793 (unsigned long) lock.l_pid, lock.l_type == F_RDLCK ? "read" : "write",
8794 fxh->fh->fh_path);
8795 }
8796
8797 status_code = SSH2_FX_LOCK_CONFLICT;
8798 reason = fxp_strerror(status_code);
8799
8800 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8801 (unsigned long) status_code, reason);
8802
8803 } else {
8804 status_code = fxp_errno2status(xerrno, &reason);
8805
8806 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
8807 "('%s' [%d])", (unsigned long) status_code, reason,
8808 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
8809 }
8810
8811 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8812 reason, NULL);
8813
8814 fxp_cmd_dispatch_err(cmd);
8815
8816 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8817 resp->payload = ptr;
8818 resp->payload_sz = (bufsz - buflen);
8819
8820 return fxp_packet_write(resp);
8821 }
8822
8823 pr_trace_msg("lock", 9, "%s lock of file '%s' successful", lock_type_str,
8824 fxh->fh->fh_path);
8825
8826 status_code = SSH2_FX_OK;
8827
8828 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8829 (unsigned long) status_code, fxp_strerror(status_code));
8830
8831 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8832 fxp_strerror(status_code), NULL);
8833
8834 fxp_cmd_dispatch(cmd);
8835
8836 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8837 resp->payload = ptr;
8838 resp->payload_sz = (bufsz - buflen);
8839
8840 return fxp_packet_write(resp);
8841 }
8842
8843 static int fxp_handle_lstat(struct fxp_packet *fxp) {
8844 unsigned char *buf;
8845 char *cmd_name, *path;
8846 uint32_t attr_flags, buflen;
8847 struct stat st;
8848 struct fxp_buffer *fxb;
8849 struct fxp_packet *resp;
8850 cmd_rec *cmd;
8851 const char *fake_user = NULL, *fake_group = NULL;
8852
8853 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
8854 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
8855 path = sftp_utf8_decode_str(fxp->pool, path);
8856 }
8857
8858 pr_scoreboard_entry_update(session.pid,
8859 PR_SCORE_CMD, "%s", "LSTAT", NULL, NULL);
8860 pr_scoreboard_entry_update(session.pid,
8861 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
8862
8863 pr_proctitle_set("%s - %s: LSTAT %s", session.user, session.proc_prefix,
8864 path);
8865
8866 if (fxp_session->client_version > 3) {
8867 attr_flags = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
8868
8869 pr_trace_msg(trace_channel, 7, "received request: LSTAT %s %s", path,
8870 fxp_strattrflags(fxp->pool, attr_flags));
8871
8872 } else {
8873 pr_trace_msg(trace_channel, 7, "received request: LSTAT %s", path);
8874 attr_flags = SSH2_FX_ATTR_SIZE|SSH2_FX_ATTR_UIDGID|SSH2_FX_ATTR_PERMISSIONS|
8875 SSH2_FX_ATTR_ACMODTIME;
8876 #if defined(PR_USE_XATTR)
8877 if (!(fxp_fsio_opts & PR_FSIO_OPT_IGNORE_XATTR)) {
8878 attr_flags |= SSH2_FX_ATTR_EXTENDED;
8879 }
8880 #endif /* PR_USE_XATTR */
8881 }
8882
8883 if (strlen(path) == 0) {
8884 /* Use the default directory if the path is empty. */
8885 path = sftp_auth_get_default_dir();
8886
8887 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8888 "empty path given in LSTAT request, using '%s'", path);
8889 }
8890
8891 cmd = fxp_cmd_alloc(fxp->pool, "LSTAT", path);
8892 cmd->cmd_class = CL_READ|CL_SFTP;
8893 cmd->cmd_id = SFTP_CMD_ID;
8894
8895 fxb = pcalloc(fxp->pool, sizeof(struct fxp_buffer));
8896 fxb->bufsz = buflen = FXP_RESPONSE_NAME_DEFAULT_SZ;
8897 fxb->ptr = buf = palloc(fxp->pool, fxb->bufsz);
8898
8899 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
8900 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
8901
8902 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8903 "LSTAT of '%s' blocked by '%s' handler: %s", path,
8904 (char *) cmd->argv[0], strerror(errno));
8905
8906 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8907 (unsigned long) status_code, fxp_strerror(status_code));
8908
8909 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8910 fxp_strerror(status_code), NULL);
8911
8912 fxp_cmd_dispatch_err(cmd);
8913
8914 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8915 resp->payload = fxb->ptr;
8916 resp->payload_sz = (fxb->bufsz - buflen);
8917
8918 return fxp_packet_write(resp);
8919 }
8920
8921 /* The path may have been changed by any PRE_CMD handlers. */
8922 path = dir_best_path(fxp->pool, cmd->arg);
8923 if (path == NULL) {
8924 int xerrno = EACCES;
8925 const char *reason;
8926 uint32_t status_code;
8927
8928 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8929 "LSTAT request denied: unable to access path '%s'", cmd->arg);
8930
8931 status_code = fxp_errno2status(xerrno, &reason);
8932
8933 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
8934 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
8935 xerrno);
8936
8937 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8938 reason, NULL);
8939
8940 fxp_cmd_dispatch_err(cmd);
8941
8942 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8943 resp->payload = fxb->ptr;
8944 resp->payload_sz = (fxb->bufsz - buflen);
8945
8946 return fxp_packet_write(resp);
8947 }
8948
8949 cmd_name = cmd->argv[0];
8950 pr_cmd_set_name(cmd, "LSTAT");
8951
8952 if (!dir_check(fxp->pool, cmd, G_NONE, path, NULL)) {
8953 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
8954
8955 pr_cmd_set_name(cmd, cmd_name);
8956
8957 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8958 "LSTAT of '%s' blocked by <Limit> configuration", path);
8959
8960 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
8961 (unsigned long) status_code, fxp_strerror(status_code));
8962
8963 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8964 fxp_strerror(status_code), NULL);
8965
8966 fxp_cmd_dispatch_err(cmd);
8967
8968 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8969 resp->payload = fxb->ptr;
8970 resp->payload_sz = (fxb->bufsz - buflen);
8971
8972 return fxp_packet_write(resp);
8973 }
8974 pr_cmd_set_name(cmd, cmd_name);
8975
8976 pr_fs_clear_cache2(path);
8977 if (pr_fsio_lstat(path, &st) < 0) {
8978 uint32_t status_code;
8979 const char *reason;
8980 int xerrno = errno;
8981
8982 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
8983 "error checking '%s' for LSTAT: %s", path, strerror(xerrno));
8984
8985 status_code = fxp_errno2status(xerrno, &reason);
8986
8987 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
8988 "('%s' [%d])", (unsigned long) status_code, reason,
8989 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
8990
8991 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
8992 reason, NULL);
8993
8994 fxp_cmd_dispatch_err(cmd);
8995
8996 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
8997 resp->payload = fxb->ptr;
8998 resp->payload_sz = (fxb->bufsz - buflen);
8999
9000 return fxp_packet_write(resp);
9001 }
9002
9003 pr_trace_msg(trace_channel, 8, "sending response: ATTRS %s",
9004 fxp_strattrs(fxp->pool, &st, NULL));
9005
9006 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_ATTRS);
9007 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
9008
9009 fake_user = get_param_ptr(get_dir_ctxt(fxp->pool, path), "DirFakeUser",
9010 FALSE);
9011 if (fake_user != NULL &&
9012 strncmp(fake_user, "~", 2) == 0) {
9013 fake_user = session.user;
9014 }
9015
9016 fake_group = get_param_ptr(get_dir_ctxt(fxp->pool, path), "DirFakeGroup",
9017 FALSE);
9018 if (fake_group != NULL &&
9019 strncmp(fake_group, "~", 2) == 0) {
9020 fake_group = session.group;
9021 }
9022
9023 fxb->buf = buf;
9024 fxb->buflen = buflen;
9025
9026 attr_flags = fxp_attrs_clear_unsupported(attr_flags);
9027 if (fxp_session->client_version > 3 &&
9028 sftp_opts & SFTP_OPT_INCLUDE_SFTP_TIMES) {
9029 pr_trace_msg(trace_channel, 17,
9030 "SFTPOption IncludeSFTPTimes in effect; assuring presence of "
9031 "ACCESSTIME/MODIFYTIME attributes");
9032 attr_flags |= SSH2_FX_ATTR_ACCESSTIME;
9033 attr_flags |= SSH2_FX_ATTR_MODIFYTIME;
9034 }
9035
9036 fxp_attrs_write(fxp->pool, fxb, path, &st, attr_flags, fake_user, fake_group);
9037
9038 /* fxp_attrs_write will have changed the buf/buflen fields in the buffer. */
9039 buf = fxb->buf;
9040 buflen = fxb->buflen;
9041
9042 fxp_cmd_dispatch(cmd);
9043
9044 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9045 resp->payload = fxb->ptr;
9046 resp->payload_sz = (fxb->bufsz - buflen);
9047
9048 return fxp_packet_write(resp);
9049 }
9050
9051 static int fxp_handle_mkdir(struct fxp_packet *fxp) {
9052 unsigned char *buf, *ptr;
9053 char *attrs_str, *cmd_name, *path;
9054 struct stat *attrs;
9055 int have_error = FALSE, res = 0;
9056 mode_t dir_mode;
9057 uint32_t attr_flags, buflen, bufsz, status_code;
9058 struct fxp_packet *resp;
9059 cmd_rec *cmd, *cmd2;
9060 array_header *xattrs = NULL;
9061
9062 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
9063 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
9064 path = sftp_utf8_decode_str(fxp->pool, path);
9065 }
9066
9067 pr_scoreboard_entry_update(session.pid,
9068 PR_SCORE_CMD, "%s", "MKDIR", NULL, NULL);
9069 pr_scoreboard_entry_update(session.pid,
9070 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
9071
9072 attrs = fxp_attrs_read(fxp, &fxp->payload, &fxp->payload_sz, &attr_flags,
9073 &xattrs);
9074 if (attrs == NULL) {
9075 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9076 "MKDIR request missing required attributes, ignoring");
9077 return 0;
9078 }
9079
9080 /* If the SFTPOption for ignoring perms for SFTP uploads is set, handle
9081 * it by clearing the SSH2_FX_ATTR_PERMISSIONS flag.
9082 */
9083 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_UPLOAD_PERMS) &&
9084 (attr_flags & SSH2_FX_ATTR_PERMISSIONS)) {
9085 pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSFTPUploadPerms' "
9086 "configured, ignoring perms sent by client");
9087 attr_flags &= ~SSH2_FX_ATTR_PERMISSIONS;
9088 }
9089
9090 /* If the SFTPOption for ignoring xattrs for SFTP uploads is set, handle it
9091 * by clearing the SSH2_FX_ATTR_EXTENDED flag.
9092 */
9093 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_UPLOAD_XATTRS) &&
9094 (attr_flags & SSH2_FX_ATTR_EXTENDED)) {
9095 pr_trace_msg(trace_channel, 7,
9096 "SFTPOption 'IgnoreSFTPUploadExtendedAttributes' configured, "
9097 "ignoring xattrs sent by client");
9098 attr_flags &= ~SSH2_FX_ATTR_EXTENDED;
9099 }
9100
9101 attrs_str = fxp_strattrs(fxp->pool, attrs, &attr_flags);
9102
9103 pr_proctitle_set("%s - %s: MKDIR %s %s", session.user, session.proc_prefix,
9104 path, attrs_str);
9105
9106 pr_trace_msg(trace_channel, 7, "received request: MKDIR %s %s", path,
9107 attrs_str);
9108
9109 if (strlen(path) == 0) {
9110 /* Use the default directory if the path is empty. */
9111 path = sftp_auth_get_default_dir();
9112
9113 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9114 "empty path given in MKDIR request, using '%s'", path);
9115 }
9116
9117 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
9118 buf = ptr = palloc(fxp->pool, bufsz);
9119
9120 cmd = fxp_cmd_alloc(fxp->pool, "MKDIR", path);
9121 cmd->cmd_class = CL_DIRS|CL_WRITE|CL_SFTP;
9122 cmd->cmd_id = SFTP_CMD_ID;
9123
9124 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
9125 status_code = SSH2_FX_PERMISSION_DENIED;
9126
9127 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9128 "MKDIR of '%s' blocked by '%s' handler: %s", path,
9129 (char *) cmd->argv[0], strerror(errno));
9130
9131 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
9132 (unsigned long) status_code, fxp_strerror(status_code));
9133
9134 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9135 fxp_strerror(status_code), NULL);
9136
9137 fxp_cmd_dispatch_err(cmd);
9138
9139 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9140 resp->payload = ptr;
9141 resp->payload_sz = (bufsz - buflen);
9142
9143 return fxp_packet_write(resp);
9144 }
9145
9146 path = cmd->arg;
9147
9148 cmd2 = fxp_cmd_alloc(fxp->pool, C_MKD, path);
9149 if (pr_cmd_dispatch_phase(cmd2, PRE_CMD, 0) == -1) {
9150 status_code = SSH2_FX_PERMISSION_DENIED;
9151
9152 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9153 "MKDIR of '%s' blocked by '%s' handler: %s", path,
9154 (char *) cmd2->argv[0], strerror(errno));
9155
9156 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
9157 (unsigned long) status_code, fxp_strerror(status_code));
9158
9159 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
9160 fxp_cmd_dispatch_err(cmd2);
9161
9162 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9163 fxp_strerror(status_code), NULL);
9164
9165 fxp_cmd_dispatch_err(cmd);
9166
9167 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9168 resp->payload = ptr;
9169 resp->payload_sz = (bufsz - buflen);
9170
9171 return fxp_packet_write(resp);
9172 }
9173
9174 /* The path may have been changed by any PRE_CMD handlers. */
9175 path = cmd2->arg;
9176
9177 path = dir_canonical_path(fxp->pool, path);
9178 if (path == NULL) {
9179 status_code = fxp_errno2status(EINVAL, NULL);
9180
9181 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
9182 (unsigned long) status_code, fxp_strerror(status_code));
9183
9184 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
9185 fxp_cmd_dispatch_err(cmd2);
9186
9187 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9188 fxp_strerror(status_code), NULL);
9189
9190 fxp_cmd_dispatch_err(cmd);
9191
9192 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9193 resp->payload = ptr;
9194 resp->payload_sz = (bufsz - buflen);
9195
9196 return fxp_packet_write(resp);
9197 }
9198
9199 cmd_name = cmd->argv[0];
9200 pr_cmd_set_name(cmd, C_XMKD);
9201
9202 if (!dir_check_canon(fxp->pool, cmd, G_WRITE, path, NULL)) {
9203 have_error = TRUE;
9204 }
9205
9206 if (have_error) {
9207 status_code = SSH2_FX_PERMISSION_DENIED;
9208
9209 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9210 "MKDIR of '%s' blocked by <Limit %s> configuration", path,
9211 (char *) cmd->argv[0]);
9212
9213 pr_cmd_set_name(cmd, cmd_name);
9214
9215 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
9216 (unsigned long) status_code, fxp_strerror(status_code));
9217
9218 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
9219 fxp_cmd_dispatch_err(cmd2);
9220
9221 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9222 fxp_strerror(status_code), NULL);
9223
9224 fxp_cmd_dispatch_err(cmd);
9225
9226 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9227 resp->payload = ptr;
9228 resp->payload_sz = (bufsz - buflen);
9229
9230 return fxp_packet_write(resp);
9231 }
9232
9233 pr_cmd_set_name(cmd, cmd_name);
9234
9235 if (fxp_path_pass_regex_filters(fxp->pool, "MKDIR", path) < 0) {
9236 int xerrno = errno;
9237
9238 status_code = fxp_errno2status(xerrno, NULL);
9239
9240 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
9241 (unsigned long) status_code, fxp_strerror(status_code));
9242
9243 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(xerrno));
9244 fxp_cmd_dispatch_err(cmd2);
9245
9246 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9247 fxp_strerror(status_code), NULL);
9248
9249 fxp_cmd_dispatch_err(cmd);
9250
9251 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9252 resp->payload = ptr;
9253 resp->payload_sz = (bufsz - buflen);
9254
9255 return fxp_packet_write(resp);
9256 }
9257
9258 dir_mode = (attr_flags & SSH2_FX_ATTR_PERMISSIONS) ? attrs->st_mode : 0777;
9259
9260 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9261 "creating directory '%s' with mode 0%o", path, (unsigned int) dir_mode);
9262
9263 res = pr_fsio_smkdir(fxp->pool, path, dir_mode, (uid_t) -1, (gid_t) -1);
9264 if (res < 0 &&
9265 errno != EEXIST) {
9266 const char *reason;
9267 int xerrno = errno;
9268
9269 (void) pr_trace_msg("fileperms", 1, "MKDIR, user '%s' (UID %s, GID %s): "
9270 "error making directory '%s': %s", session.user,
9271 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
9272 path, strerror(xerrno));
9273
9274 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9275 "MKDIR of '%s' failed: %s", path, strerror(xerrno));
9276
9277 status_code = fxp_errno2status(xerrno, &reason);
9278
9279 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
9280 "('%s' [%d])", (unsigned long) status_code, reason,
9281 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
9282
9283 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(xerrno));
9284 fxp_cmd_dispatch_err(cmd2);
9285
9286 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9287 reason, NULL);
9288
9289 fxp_cmd_dispatch_err(cmd);
9290
9291 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9292 resp->payload = ptr;
9293 resp->payload_sz = (bufsz - buflen);
9294
9295 return fxp_packet_write(resp);
9296 }
9297
9298 /* Handle any possible UserOwner/GroupOwner directives for created
9299 * directories.
9300 */
9301 if (sftp_misc_chown_path(fxp->pool, path) < 0) {
9302 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9303 "error changing ownership on path '%s': %s", path, strerror(errno));
9304 }
9305
9306 status_code = SSH2_FX_OK;
9307
9308 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
9309 (unsigned long) status_code, fxp_strerror(status_code));
9310
9311 fxp_cmd_dispatch(cmd2);
9312
9313 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9314 fxp_strerror(status_code), NULL);
9315
9316 pr_response_add(R_257, "\"%s\" - Directory successfully created",
9317 quote_dir(cmd->tmp_pool, path));
9318 fxp_cmd_dispatch(cmd);
9319
9320 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9321 resp->payload = ptr;
9322 resp->payload_sz = (bufsz - buflen);
9323
9324 return fxp_packet_write(resp);
9325 }
9326
9327 static int fxp_handle_open(struct fxp_packet *fxp) {
9328 unsigned char *buf, *ptr;
9329 const char *hiddenstore_path = NULL;
9330 char *path, *orig_path;
9331 uint32_t attr_flags, buflen, bufsz, desired_access = 0, flags;
9332 int file_existed = FALSE, open_flags, res, timeout_stalled;
9333 pr_fh_t *fh;
9334 struct stat *attrs, st;
9335 struct fxp_handle *fxh;
9336 struct fxp_packet *resp;
9337 cmd_rec *cmd, *cmd2 = NULL;
9338 array_header *xattrs = NULL;
9339
9340 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
9341 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
9342 path = sftp_utf8_decode_str(fxp->pool, path);
9343 }
9344
9345 orig_path = path;
9346 cmd = fxp_cmd_alloc(fxp->pool, "OPEN", path);
9347
9348 /* Set the command class to MISC for now; we'll change it later to
9349 * READ or WRITE once we know which it is.
9350 */
9351 cmd->cmd_class = CL_MISC|CL_SFTP;
9352 cmd->cmd_id = SFTP_CMD_ID;
9353
9354 pr_scoreboard_entry_update(session.pid,
9355 PR_SCORE_CMD, "%s", "OPEN", NULL, NULL);
9356 pr_scoreboard_entry_update(session.pid,
9357 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
9358
9359 pr_proctitle_set("%s - %s: OPEN %s", session.user, session.proc_prefix, path);
9360
9361 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
9362 buf = ptr = palloc(fxp->pool, bufsz);
9363
9364 if (fxp_session->client_version > 4) {
9365 desired_access = sftp_msg_read_int(fxp->pool, &fxp->payload,
9366 &fxp->payload_sz);
9367
9368 /* Check for unsupported flags. */
9369 if ((desired_access & SSH2_FXF_WANT_READ_NAMED_ATTRS) ||
9370 (desired_access & SSH2_FXF_WANT_READ_ACL) ||
9371 (desired_access & SSH2_FXF_WANT_WRITE_NAMED_ATTRS) ||
9372 (desired_access & SSH2_FXF_WANT_WRITE_ACL) ||
9373 (desired_access & SSH2_FXF_WANT_WRITE_OWNER)) {
9374 uint32_t status_code;
9375 const char *unsupported_str = "";
9376
9377 if (desired_access & SSH2_FXF_WANT_READ_NAMED_ATTRS) {
9378 unsupported_str = pstrcat(fxp->pool, unsupported_str,
9379 *unsupported_str ? "|" : "", "WANT_READ_NAMED_ATTRS", NULL);
9380 }
9381
9382 if (desired_access & SSH2_FXF_WANT_READ_ACL) {
9383 unsupported_str = pstrcat(fxp->pool, unsupported_str,
9384 *unsupported_str ? "|" : "", "WANT_READ_ACL", NULL);
9385 }
9386
9387 if (desired_access & SSH2_FXF_WANT_WRITE_NAMED_ATTRS) {
9388 unsupported_str = pstrcat(fxp->pool, unsupported_str,
9389 *unsupported_str ? "|" : "", "WANT_WRITE_NAMED_ATTRS", NULL);
9390 }
9391
9392 if (desired_access & SSH2_FXF_WANT_WRITE_ACL) {
9393 unsupported_str = pstrcat(fxp->pool, unsupported_str,
9394 *unsupported_str ? "|" : "", "WANT_WRITE_ACL", NULL);
9395 }
9396
9397 if (desired_access & SSH2_FXF_WANT_WRITE_OWNER) {
9398 unsupported_str = pstrcat(fxp->pool, unsupported_str,
9399 *unsupported_str ? "|" : "", "WANT_WRITE_OWNER", NULL);
9400 }
9401
9402 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9403 "client requested unsupported access '%s' in OPEN command, rejecting",
9404 unsupported_str);
9405
9406 status_code = SSH2_FX_OP_UNSUPPORTED;
9407
9408 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
9409 (unsigned long) status_code, fxp_strerror(status_code));
9410
9411 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9412 fxp_strerror(status_code), NULL);
9413
9414 fxp_cmd_note_file_status(cmd, "failed");
9415 fxp_cmd_dispatch_err(cmd);
9416
9417 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9418 resp->payload = ptr;
9419 resp->payload_sz = (bufsz - buflen);
9420
9421 return fxp_packet_write(resp);
9422 }
9423 }
9424
9425 flags = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
9426
9427 /* Check for any unsupported flags. */
9428 if (fxp_session->client_version > 4) {
9429 /* XXX If O_SHLOCK and O_EXLOCK are defined, as they are on OSX, the
9430 * ACCESS_READ_LOCK and ACCESS_WRITE_LOCK flags should be supported.
9431 *
9432 * Note that IF we support these LOCK flags, we will need to report
9433 * this support in the VERSION response as well.
9434 */
9435
9436 if ((flags & SSH2_FXF_ACCESS_READ_LOCK) ||
9437 (flags & SSH2_FXF_ACCESS_WRITE_LOCK) ||
9438 (flags & SSH2_FXF_ACCESS_DELETE_LOCK)) {
9439 uint32_t status_code;
9440 const char *unsupported_str = "";
9441
9442 if (flags & SSH2_FXF_ACCESS_READ_LOCK) {
9443 unsupported_str = pstrcat(fxp->pool, unsupported_str,
9444 *unsupported_str ? "|" : "", "ACCESS_READ_LOCK", NULL);
9445 }
9446
9447 if (flags & SSH2_FXF_ACCESS_WRITE_LOCK) {
9448 unsupported_str = pstrcat(fxp->pool, unsupported_str,
9449 *unsupported_str ? "|" : "", "ACCESS_WRITE_LOCK", NULL);
9450 }
9451
9452 if (flags & SSH2_FXF_ACCESS_DELETE_LOCK) {
9453 unsupported_str = pstrcat(fxp->pool, unsupported_str,
9454 *unsupported_str ? "|" : "", "ACCESS_DELETE_LOCK", NULL);
9455 }
9456
9457 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9458 "client requested unsupported flag '%s' in OPEN command, rejecting",
9459 unsupported_str);
9460
9461 status_code = SSH2_FX_OP_UNSUPPORTED;
9462
9463 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
9464 (unsigned long) status_code, fxp_strerror(status_code));
9465
9466 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9467 fxp_strerror(status_code), NULL);
9468
9469 fxp_cmd_note_file_status(cmd, "failed");
9470 fxp_cmd_dispatch_err(cmd);
9471
9472 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9473 resp->payload = ptr;
9474 resp->payload_sz = (bufsz - buflen);
9475
9476 return fxp_packet_write(resp);
9477 }
9478
9479 /* Make sure the requested path exists. */
9480 pr_fs_clear_cache2(path);
9481 if (((flags & SSH2_FXF_OPEN_EXISTING) &&
9482 !(flags & SSH2_FXF_CREATE_TRUNCATE)) &&
9483 !exists2(fxp->pool, path)) {
9484 uint32_t status_code;
9485
9486 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9487 "client requested OPEN_EXISTING flag in OPEN command and '%s' does "
9488 "not exist", path);
9489
9490 status_code = SSH2_FX_NO_SUCH_FILE;
9491
9492 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
9493 (unsigned long) status_code, fxp_strerror(status_code));
9494
9495 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9496 fxp_strerror(status_code), NULL);
9497
9498 fxp_cmd_note_file_status(cmd, "failed");
9499 fxp_cmd_dispatch_err(cmd);
9500
9501 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9502 resp->payload = ptr;
9503 resp->payload_sz = (bufsz - buflen);
9504
9505 return fxp_packet_write(resp);
9506 }
9507 }
9508
9509 if (fxp_session->client_version < 5) {
9510 fxp_trace_v3_open_flags(fxp->pool, flags);
9511 open_flags = fxp_get_v3_open_flags(flags);
9512
9513 } else {
9514 fxp_trace_v5_open_flags(fxp->pool, desired_access, flags);
9515 open_flags = fxp_get_v5_open_flags(desired_access, flags);
9516 }
9517
9518 attrs = fxp_attrs_read(fxp, &fxp->payload, &fxp->payload_sz, &attr_flags,
9519 &xattrs);
9520 if (attrs == NULL) {
9521 fxp_cmd_note_file_status(cmd, "failed");
9522 fxp_cmd_dispatch_err(cmd);
9523
9524 /* XXX TODO: Provide a response to the client here */
9525 return 0;
9526 }
9527
9528 pr_trace_msg(trace_channel, 7, "received request: OPEN %s %s (%s)",
9529 path, fxp_strattrs(fxp->pool, attrs, &attr_flags),
9530 fxp_stroflags(fxp->pool, open_flags));
9531
9532 if (open_flags & O_APPEND) {
9533 cmd->cmd_class &= ~CL_MISC;
9534 cmd->cmd_class |= CL_WRITE;
9535 cmd2 = fxp_cmd_alloc(fxp->pool, C_APPE, path);
9536 cmd2->cmd_id = pr_cmd_get_id(C_APPE);
9537 session.curr_cmd = C_APPE;
9538
9539 if (pr_table_add(cmd2->notes, "mod_xfer.store-path",
9540 pstrdup(fxp->pool, path), 0) < 0) {
9541 if (errno != EEXIST) {
9542 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9543 "error adding 'mod_xfer.store-path' note: %s", strerror(errno));
9544 }
9545 }
9546
9547 } else if ((open_flags & O_WRONLY) ||
9548 (open_flags & O_RDWR)) {
9549 cmd2 = fxp_cmd_alloc(fxp->pool, C_STOR, path);
9550 cmd2->cmd_id = pr_cmd_get_id(C_STOR);
9551
9552 if (open_flags & O_WRONLY) {
9553 cmd->cmd_class &= ~CL_MISC;
9554 cmd->cmd_class |= CL_WRITE;
9555
9556 } else if (open_flags & O_RDWR) {
9557 cmd->cmd_class &= ~CL_MISC;
9558 cmd->cmd_class |= (CL_READ|CL_WRITE);
9559 }
9560
9561 session.curr_cmd = C_STOR;
9562
9563 if (pr_table_add(cmd2->notes, "mod_xfer.store-path",
9564 pstrdup(fxp->pool, path), 0) < 0) {
9565 if (errno != EEXIST) {
9566 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9567 "error adding 'mod_xfer.store-path' note: %s", strerror(errno));
9568 }
9569 }
9570
9571 } else if (open_flags == O_RDONLY) {
9572 cmd->cmd_class &= ~CL_MISC;
9573 cmd->cmd_class |= CL_READ;
9574 cmd2 = fxp_cmd_alloc(fxp->pool, C_RETR, path);
9575 cmd2->cmd_id = pr_cmd_get_id(C_RETR);
9576 session.curr_cmd = C_RETR;
9577
9578 /* We ignore any perms sent by the client for read-only requests.
9579 *
9580 * This happens because we explicitly call chown(2)/chmod(2) after
9581 * open(2) in order to handle UserOwner/GroupOwner directive. But this
9582 * breaks the semantics of open(2), which does not change the mode of
9583 * an existing file if the flags are O_RDONLY.
9584 */
9585 if (attr_flags & SSH2_FX_ATTR_PERMISSIONS) {
9586 pr_trace_msg(trace_channel, 15,
9587 "read-only OPEN request, ignoring perms sent by client");
9588 attr_flags &= ~SSH2_FX_ATTR_PERMISSIONS;
9589 }
9590
9591 if (pr_table_add(cmd2->notes, "mod_xfer.retr-path",
9592 pstrdup(fxp->pool, path), 0) < 0) {
9593 if (errno != EEXIST) {
9594 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9595 "error adding 'mod_xfer.retr-path' note: %s", strerror(errno));
9596 }
9597 }
9598 }
9599
9600 if (cmd2 != NULL) {
9601 if (pr_cmd_dispatch_phase(cmd2, PRE_CMD, 0) < 0) {
9602 int xerrno = errno;
9603 const char *reason;
9604 uint32_t status_code;
9605
9606 /* One of the PRE_CMD phase handlers rejected the command. */
9607
9608 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9609 "OPEN command for '%s' blocked by '%s' handler: %s", path,
9610 (char *) cmd2->argv[0], strerror(xerrno));
9611
9612 /* Hopefully the command handlers set an appropriate errno value. If
9613 * they didn't, however, we need to be prepared with a fallback.
9614 */
9615 if (xerrno != ENOENT &&
9616 xerrno != EPERM &&
9617 #if defined(EDQUOT)
9618 xerrno != EDQUOT &&
9619 #endif /* EDQUOT */
9620 #if defined(EFBIG)
9621 xerrno != EFBIG &&
9622 #endif /* EFBIG */
9623 #if defined(ENOSPC)
9624 xerrno != ENOSPC &&
9625 #endif /* ENOSPC */
9626 xerrno != EINVAL) {
9627 xerrno = EACCES;
9628 }
9629
9630 status_code = fxp_errno2status(xerrno, &reason);
9631
9632 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
9633 "('%s' [%d])", (unsigned long) status_code, reason, strerror(errno),
9634 xerrno);
9635
9636 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
9637 fxp_cmd_note_file_status(cmd2, "failed");
9638 fxp_cmd_dispatch_err(cmd2);
9639
9640 fxp_cmd_note_file_status(cmd, "failed");
9641 fxp_cmd_dispatch_err(cmd);
9642
9643 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9644 reason, NULL);
9645
9646 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9647 resp->payload = ptr;
9648 resp->payload_sz = (bufsz - buflen);
9649
9650 return fxp_packet_write(resp);
9651 }
9652
9653 path = cmd2->arg;
9654
9655 /* Note that session.xfer.xfer_type will have been set by the PRE_CMD
9656 * dispatch above, by mod_xfer.
9657 */
9658 if (session.xfer.xfer_type == STOR_HIDDEN) {
9659 const void *nfs;
9660
9661 hiddenstore_path = pr_table_get(cmd2->notes,
9662 "mod_xfer.store-hidden-path", NULL);
9663
9664 nfs = pr_table_get(cmd2->notes, "mod_xfer.store-hidden-nfs", NULL);
9665 if (nfs == NULL) {
9666 open_flags |= O_EXCL;
9667 }
9668
9669 } else {
9670 pr_fs_clear_cache2(path);
9671 if (pr_fsio_lstat(path, &st) == 0) {
9672 if (S_ISLNK(st.st_mode)) {
9673 char link_path[PR_TUNABLE_PATH_MAX];
9674 int len;
9675
9676 memset(link_path, '\0', sizeof(link_path));
9677 len = dir_readlink(fxp->pool, path, link_path, sizeof(link_path)-1,
9678 PR_DIR_READLINK_FL_HANDLE_REL_PATH);
9679 if (len > 0) {
9680 link_path[len] = '\0';
9681 path = pstrdup(fxp->pool, link_path);
9682 } else {
9683 path = dir_best_path(fxp->pool, path);
9684 }
9685
9686 } else {
9687 path = dir_best_path(fxp->pool, path);
9688 }
9689
9690 } else {
9691 path = dir_best_path(fxp->pool, path);
9692 }
9693 }
9694
9695 if (hiddenstore_path != NULL) {
9696 pr_fs_clear_cache2(hiddenstore_path);
9697 }
9698
9699 file_existed = exists2(fxp->pool,
9700 hiddenstore_path ? hiddenstore_path : path);
9701
9702 if (file_existed &&
9703 (pr_cmd_cmp(cmd2, PR_CMD_STOR_ID) == 0 ||
9704 pr_cmd_cmp(cmd2, PR_CMD_APPE_ID) == 0)) {
9705
9706 /* Clear any existing key in the notes. */
9707 (void) pr_table_remove(cmd->notes, "mod_xfer.file-modified", NULL);
9708
9709 if (pr_table_add(cmd->notes, "mod_xfer.file-modified",
9710 pstrdup(cmd->pool, "true"), 0) < 0) {
9711 if (errno != EEXIST) {
9712 pr_log_pri(PR_LOG_NOTICE,
9713 "notice: error adding 'mod_xfer.file-modified' note: %s",
9714 strerror(errno));
9715 }
9716 }
9717
9718 /* Clear any existing key in the notes. */
9719 (void) pr_table_remove(cmd2->notes, "mod_xfer.file-modified", NULL);
9720
9721 if (pr_table_add(cmd2->notes, "mod_xfer.file-modified",
9722 pstrdup(cmd2->pool, "true"), 0) < 0) {
9723 if (errno != EEXIST) {
9724 pr_log_pri(PR_LOG_NOTICE,
9725 "notice: error adding 'mod_xfer.file-modified' note: %s",
9726 strerror(errno));
9727 }
9728 }
9729 }
9730 }
9731
9732 pr_fs_clear_cache2(path);
9733 if (exists2(fxp->pool, path)) {
9734 /* draft-ietf-secsh-filexfer-06.txt, section 7.1.1 specifically
9735 * states that any attributes in a OPEN request are ignored if the
9736 * file already exists.
9737 */
9738 if (attr_flags & SSH2_FX_ATTR_PERMISSIONS) {
9739 pr_trace_msg(trace_channel, 15,
9740 "OPEN request for existing path, ignoring perms sent by client");
9741 attr_flags &= ~SSH2_FX_ATTR_PERMISSIONS;
9742 }
9743
9744 if ((attr_flags & SSH2_FX_ATTR_UIDGID) ||
9745 (attr_flags & SSH2_FX_ATTR_OWNERGROUP)) {
9746 pr_trace_msg(trace_channel, 15,
9747 "OPEN request for existing path, ignoring ownership sent by client");
9748 attr_flags &= ~SSH2_FX_ATTR_UIDGID;
9749 attr_flags &= ~SSH2_FX_ATTR_OWNERGROUP;
9750 }
9751 }
9752
9753 /* We automatically add the O_NONBLOCK flag to the set of open() flags
9754 * in order to deal with writing to a FIFO whose other end may not be
9755 * open. Then, after a successful open, we return the file to blocking
9756 * mode.
9757 */
9758 fh = pr_fsio_open(hiddenstore_path ? hiddenstore_path : path,
9759 open_flags|O_NONBLOCK);
9760 if (fh == NULL) {
9761 uint32_t status_code;
9762 const char *reason;
9763 int xerrno = errno;
9764
9765 (void) pr_trace_msg("fileperms", 1, "OPEN, user '%s' (UID %s, GID %s): "
9766 "error opening '%s': %s", session.user,
9767 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
9768 hiddenstore_path ? hiddenstore_path : path, strerror(xerrno));
9769
9770 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9771 "error opening '%s': %s", hiddenstore_path ? hiddenstore_path : path,
9772 strerror(xerrno));
9773
9774 status_code = fxp_errno2status(xerrno, &reason);
9775
9776 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
9777 "('%s' [%d])", (unsigned long) status_code, reason,
9778 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
9779
9780 if (cmd2 != NULL) {
9781 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
9782 fxp_cmd_note_file_status(cmd2, "failed");
9783 fxp_cmd_dispatch_err(cmd2);
9784 }
9785
9786 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9787 reason, NULL);
9788
9789 fxp_cmd_note_file_status(cmd, "failed");
9790 fxp_cmd_dispatch_err(cmd);
9791
9792 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9793 resp->payload = ptr;
9794 resp->payload_sz = (bufsz - buflen);
9795
9796 return fxp_packet_write(resp);
9797 }
9798
9799 memset(&st, 0, sizeof(st));
9800 if (pr_fsio_fstat(fh, &st) < 0) {
9801 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9802 "fstat error on '%s' (fd %d): %s", path, fh->fh_fd, strerror(errno));
9803 }
9804
9805 #ifdef S_ISFIFO
9806 /* The path in question might be a FIFO. The FIFO case requires some special
9807 * handling, modulo any IgnoreFIFOs SFTPOption that might be in effect.
9808 */
9809 if (S_ISFIFO(st.st_mode) &&
9810 (sftp_opts & SFTP_OPT_IGNORE_FIFOS)) {
9811 uint32_t status_code;
9812 const char *reason;
9813 int xerrno = EPERM;
9814
9815 (void) pr_fsio_close(fh);
9816
9817 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9818 "error opening FIFO '%s': %s (IgnoreFIFOs SFTPOption in effect)",
9819 hiddenstore_path ? hiddenstore_path : path, strerror(xerrno));
9820
9821 status_code = fxp_errno2status(xerrno, &reason);
9822
9823 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
9824 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
9825 xerrno);
9826
9827 if (cmd2 != NULL) {
9828 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
9829 fxp_cmd_note_file_status(cmd2, "failed");
9830 fxp_cmd_dispatch_err(cmd2);
9831 }
9832
9833 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9834 reason, NULL);
9835
9836 fxp_cmd_note_file_status(cmd, "failed");
9837 fxp_cmd_dispatch_err(cmd);
9838
9839 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9840 resp->payload = ptr;
9841 resp->payload_sz = (bufsz - buflen);
9842
9843 return fxp_packet_write(resp);
9844 }
9845 #endif /* S_ISFIFO */
9846
9847 if (pr_fsio_set_block(fh) < 0) {
9848 pr_trace_msg(trace_channel, 3,
9849 "error setting fd %d (file '%s') as blocking: %s", fh->fh_fd,
9850 fh->fh_path, strerror(errno));
9851 }
9852
9853 attr_flags = fxp_attrs_clear_unsupported(attr_flags);
9854
9855 /* If the SFTPOption for ignoring perms for SFTP uploads is set, handle
9856 * it by clearing the SSH2_FX_ATTR_PERMISSIONS flag.
9857 */
9858 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_UPLOAD_PERMS) &&
9859 (attr_flags & SSH2_FX_ATTR_PERMISSIONS)) {
9860 pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSFTPUploadPerms' "
9861 "configured, ignoring perms sent by client");
9862 attr_flags &= ~SSH2_FX_ATTR_PERMISSIONS;
9863 }
9864
9865 /* If the SFTPOption for ignoring xattrs for SFTP uploads is set, handle it
9866 * by clearing the SSH2_FX_ATTR_EXTENDED flag.
9867 */
9868 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_UPLOAD_XATTRS) &&
9869 (attr_flags & SSH2_FX_ATTR_EXTENDED)) {
9870 pr_trace_msg(trace_channel, 7,
9871 "SFTPOption 'IgnoreSFTPUploadExtendedAttributes' configured, "
9872 "ignoring xattrs sent by client");
9873 attr_flags &= ~SSH2_FX_ATTR_EXTENDED;
9874 }
9875
9876 /* If the client provided a suggested size in the OPEN, ignore it.
9877 * Trying to honor the suggested size by truncating the file here can
9878 * cause problems, as when the client is resuming a transfer and the
9879 * resumption fails; the file would then be worse off than before due to the
9880 * truncation. See:
9881 *
9882 * http://winscp.net/tracker/show_bug.cgi?id=351
9883 *
9884 * The truncation isn't really needed anyway, since the ensuing READ/WRITE
9885 * requests will contain the offsets into the file at which to begin
9886 * reading/write the file contents.
9887 *
9888 * However, if the size is provided, we should at least record it in the
9889 * handle structure. In the case of an upload, we can compare the size of
9890 * file, at CLOSE time, with the size that was provided here. If the size
9891 * of the file at CLOSE is less than the size sent here, we could log it
9892 * as an incomplete upload. Not all clients will provide the size attribute,
9893 * for those that do, it can be useful.
9894 */
9895
9896 attr_flags &= ~SSH2_FX_ATTR_SIZE;
9897
9898 res = fxp_attrs_set(fh, fh->fh_path, attrs, attr_flags, xattrs, &buf,
9899 &buflen, fxp);
9900 if (res < 0) {
9901 int xerrno = errno;
9902
9903 pr_fsio_close(fh);
9904
9905 if (cmd2 != NULL) {
9906 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
9907 fxp_cmd_note_file_status(cmd2, "failed");
9908 fxp_cmd_dispatch_err(cmd2);
9909 }
9910
9911 fxp_cmd_note_file_status(cmd, "failed");
9912 fxp_cmd_dispatch_err(cmd);
9913
9914 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9915 resp->payload = ptr;
9916 resp->payload_sz = (bufsz - buflen);
9917
9918 return fxp_packet_write(resp);
9919 }
9920
9921 if ((open_flags & O_WRONLY) ||
9922 (open_flags & O_RDWR)) {
9923 /* Handle any possible UserOwner/GroupOwner directives for uploaded
9924 * files.
9925 */
9926 if (sftp_misc_chown_file(fxp->pool, fh) < 0) {
9927 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9928 "error changing ownership on file '%s': %s", fh->fh_path,
9929 strerror(errno));
9930 }
9931 }
9932
9933 fxh = fxp_handle_create(fxp_pool);
9934 if (fxh == NULL) {
9935 uint32_t status_code;
9936 const char *reason;
9937 int xerrno = errno;
9938
9939 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
9940 "error creating SFTP handle for '%s': %s", fh->fh_path, strerror(xerrno));
9941
9942 status_code = fxp_errno2status(xerrno, &reason);
9943
9944 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
9945 "('%s' [%d])", (unsigned long) status_code, reason,
9946 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
9947
9948 if (cmd2 != NULL) {
9949 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
9950 fxp_cmd_note_file_status(cmd2, "failed");
9951 fxp_cmd_dispatch_err(cmd2);
9952 }
9953
9954 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
9955 reason, NULL);
9956
9957 fxp_cmd_note_file_status(cmd, "failed");
9958 fxp_cmd_dispatch_err(cmd);
9959
9960 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
9961 resp->payload = ptr;
9962 resp->payload_sz = (bufsz - buflen);
9963
9964 return fxp_packet_write(resp);
9965 }
9966
9967 /* Populate the filehandle fields, included the start-of-transfer details. */
9968 fxh->fh = fh;
9969 fxh->fh_flags = open_flags;
9970 fxh->fh_existed = file_existed;
9971 memcpy(fxh->fh_st, &st, sizeof(struct stat));
9972
9973 fxh->xfer.xfer_type = session.xfer.xfer_type;
9974 fxh->xfer.filename = pstrdup(fxh->pool, orig_path);
9975 fxh->xfer.path = pstrdup(fxh->pool, fxh->fh->fh_path);
9976
9977 if (hiddenstore_path != NULL) {
9978 fxh->xfer.path_hidden = pstrdup(fxh->pool, path);
9979 }
9980
9981 memset(&(fxh->xfer.start_time), 0, sizeof(struct timeval));
9982 gettimeofday(&(fxh->xfer.start_time), NULL);
9983
9984 if (file_existed == TRUE) {
9985 fxh->xfer.file_size = st.st_size;
9986
9987 } else {
9988 fxh->xfer.file_size = 0;
9989 }
9990
9991 fxh->xfer.total_bytes = 0;
9992
9993 if (fxp_handle_add(fxp->channel_id, fxh) < 0) {
9994 uint32_t status_code;
9995 const char *reason;
9996 int xerrno = errno;
9997
9998 buf = ptr;
9999 buflen = bufsz;
10000
10001 status_code = fxp_errno2status(xerrno, &reason);
10002
10003 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
10004 "('%s' [%d])", (unsigned long) status_code, reason,
10005 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
10006
10007 pr_fsio_close(fh);
10008 destroy_pool(fxh->pool);
10009
10010 if (cmd2 != NULL) {
10011 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
10012 fxp_cmd_note_file_status(cmd2, "failed");
10013 fxp_cmd_dispatch_err(cmd2);
10014 }
10015
10016 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10017 reason, NULL);
10018
10019 fxp_cmd_note_file_status(cmd, "failed");
10020 fxp_cmd_dispatch_err(cmd);
10021
10022 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10023 resp->payload = ptr;
10024 resp->payload_sz = (bufsz - buflen);
10025
10026 return fxp_packet_write(resp);
10027 }
10028
10029 pr_trace_msg(trace_channel, 8, "sending response: HANDLE %s", fxh->name);
10030
10031 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_HANDLE);
10032 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
10033 sftp_msg_write_string(&buf, &buflen, fxh->name);
10034
10035 if ((open_flags & O_APPEND) ||
10036 (open_flags & O_WRONLY) ||
10037 (open_flags & O_RDWR)) {
10038
10039 /* Advise the platform that we will be only writing this file. */
10040 pr_fs_fadvise(PR_FH_FD(fxh->fh), 0, 0, PR_FS_FADVISE_DONTNEED);
10041
10042 fxh->xfer.direction = PR_NETIO_IO_RD;
10043
10044 } else if (open_flags == O_RDONLY) {
10045 /* Advise the platform that we will be only reading this file, and that
10046 * we will be accessing the data soon.
10047 */
10048 pr_fs_fadvise(PR_FH_FD(fxh->fh), 0, 0, PR_FS_FADVISE_SEQUENTIAL);
10049 pr_fs_fadvise(PR_FH_FD(fxh->fh), 0, 0, PR_FS_FADVISE_WILLNEED);
10050
10051 fxh->xfer.direction = PR_NETIO_IO_WR;
10052 }
10053
10054 pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);
10055
10056 timeout_stalled = pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED);
10057 if (timeout_stalled > 0) {
10058 pr_timer_add(timeout_stalled, PR_TIMER_STALLED, NULL,
10059 fxp_timeout_stalled_cb, "TimeoutStalled");
10060 }
10061
10062 /* Add a note containing the file handle for logging (Bug#3707). */
10063 fxp_set_filehandle_note(cmd, fxh);
10064
10065 /* Populate the session.xfer struct with filehandle-specific details
10066 * (Issue #1646)
10067 */
10068 fxp_set_filehandle_sess_xfer(fxh);
10069
10070 fxp_cmd_dispatch(cmd);
10071
10072 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10073 resp->payload = ptr;
10074 resp->payload_sz = (bufsz - buflen);
10075
10076 return fxp_packet_write(resp);
10077 }
10078
10079 static int fxp_handle_opendir(struct fxp_packet *fxp) {
10080 unsigned char *buf, *ptr;
10081 char *path, *vpath;
10082 uint32_t buflen, bufsz;
10083 int timeout_stalled;
10084 void *dirh;
10085 struct fxp_handle *fxh;
10086 struct fxp_packet *resp;
10087 cmd_rec *cmd, *cmd2;
10088 struct stat st;
10089
10090 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
10091 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
10092 path = sftp_utf8_decode_str(fxp->pool, path);
10093 }
10094
10095 pr_scoreboard_entry_update(session.pid,
10096 PR_SCORE_CMD, "%s", "OPENDIR", NULL, NULL);
10097 pr_scoreboard_entry_update(session.pid,
10098 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
10099
10100 pr_proctitle_set("%s - %s: OPENDIR %s", session.user, session.proc_prefix,
10101 path);
10102
10103 pr_trace_msg(trace_channel, 7, "received request: OPENDIR %s", path);
10104
10105 if (strlen(path) == 0) {
10106 /* Use the default directory if the path is empty. */
10107 path = sftp_auth_get_default_dir();
10108
10109 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10110 "empty path given in OPENDIR request, using '%s'", path);
10111 }
10112
10113 cmd = fxp_cmd_alloc(fxp->pool, "OPENDIR", path);
10114 cmd->cmd_class = CL_DIRS|CL_SFTP;
10115 cmd->cmd_id = SFTP_CMD_ID;
10116
10117 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
10118 buf = ptr = palloc(fxp->pool, bufsz);
10119
10120 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
10121 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
10122
10123 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10124 "OPENDIR of '%s' blocked by '%s' handler: %s", path,
10125 (char *) cmd->argv[0], strerror(errno));
10126
10127 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10128 (unsigned long) status_code, fxp_strerror(status_code));
10129
10130 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10131 fxp_strerror(status_code), NULL);
10132
10133 fxp_cmd_dispatch_err(cmd);
10134
10135 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10136 resp->payload = ptr;
10137 resp->payload_sz = (bufsz - buflen);
10138
10139 return fxp_packet_write(resp);
10140 }
10141
10142 /* The path may have been changed by any PRE_CMD handlers. */
10143 path = cmd->arg;
10144
10145 pr_fs_clear_cache2(path);
10146 if (pr_fsio_lstat(path, &st) == 0) {
10147 if (S_ISLNK(st.st_mode)) {
10148 char link_path[PR_TUNABLE_PATH_MAX];
10149 int len;
10150
10151 memset(link_path, '\0', sizeof(link_path));
10152 len = dir_readlink(fxp->pool, path, link_path, sizeof(link_path)-1,
10153 PR_DIR_READLINK_FL_HANDLE_REL_PATH);
10154 if (len > 0) {
10155 link_path[len] = '\0';
10156 path = pstrdup(fxp->pool, link_path);
10157 }
10158 }
10159 }
10160
10161 path = dir_best_path(fxp->pool, path);
10162 if (path == NULL) {
10163 int xerrno = EACCES;
10164 const char *reason;
10165 uint32_t status_code;
10166
10167 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10168 "OPENDIR request denied: unable to access path '%s'", cmd->arg);
10169
10170 status_code = fxp_errno2status(xerrno, &reason);
10171
10172 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
10173 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
10174 xerrno);
10175
10176 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10177 reason, NULL);
10178
10179 fxp_cmd_dispatch_err(cmd);
10180
10181 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10182 resp->payload = ptr;
10183 resp->payload_sz = (bufsz - buflen);
10184
10185 return fxp_packet_write(resp);
10186 }
10187
10188 if (!dir_check(fxp->pool, cmd, G_DIRS, path, NULL)) {
10189 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
10190
10191 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10192 "OPENDIR of '%s' blocked by <Limit> configuration", path);
10193
10194 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10195 (unsigned long) status_code, fxp_strerror(status_code));
10196
10197 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10198 fxp_strerror(status_code), NULL);
10199
10200 fxp_cmd_dispatch_err(cmd);
10201
10202 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10203 resp->payload = ptr;
10204 resp->payload_sz = (bufsz - buflen);
10205
10206 return fxp_packet_write(resp);
10207 }
10208
10209 cmd2 = fxp_cmd_alloc(fxp->pool, C_MLSD, path);
10210 cmd2->cmd_class = CL_DIRS;
10211 cmd2->cmd_id = pr_cmd_get_id(C_MLSD);
10212
10213 if (pr_cmd_dispatch_phase(cmd2, PRE_CMD, 0) < 0) {
10214 int xerrno = errno;
10215 const char *reason;
10216 uint32_t status_code;
10217
10218 /* One of the PRE_CMD phase handlers rejected the command. */
10219 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10220 "OPENDIR command for '%s' blocked by '%s' handler: %s", path,
10221 (char *) cmd2->argv[0], strerror(xerrno));
10222
10223 /* Hopefully the command handlers set an appropriate errno value. If
10224 * they didn't, however, we need to be prepared with a fallback.
10225 */
10226 if (xerrno != ENOENT &&
10227 xerrno != EACCES &&
10228 xerrno != EPERM &&
10229 xerrno != EINVAL) {
10230 xerrno = EACCES;
10231 }
10232
10233 status_code = fxp_errno2status(xerrno, &reason);
10234
10235 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
10236 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
10237 xerrno);
10238
10239 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
10240 fxp_cmd_dispatch_err(cmd2);
10241
10242 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10243 reason, NULL);
10244
10245 fxp_cmd_dispatch_err(cmd);
10246
10247 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10248 resp->payload = ptr;
10249 resp->payload_sz = (bufsz - buflen);
10250
10251 return fxp_packet_write(resp);
10252 }
10253
10254 /* The path may have been changed by any PRE_CMD handlers. */
10255 path = cmd2->arg;
10256
10257 vpath = dir_canonical_vpath(fxp->pool, path);
10258 if (vpath == NULL) {
10259 uint32_t status_code;
10260 const char *reason;
10261 int xerrno = errno;
10262
10263 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10264 "error resolving '%s': %s", path, strerror(xerrno));
10265
10266 status_code = fxp_errno2status(xerrno, &reason);
10267
10268 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
10269 "('%s' [%d])", (unsigned long) status_code, reason,
10270 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
10271
10272 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
10273 fxp_cmd_dispatch_err(cmd2);
10274
10275 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10276 reason, NULL);
10277
10278 fxp_cmd_dispatch_err(cmd);
10279
10280 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10281 resp->payload = ptr;
10282 resp->payload_sz = (bufsz - buflen);
10283
10284 return fxp_packet_write(resp);
10285 }
10286
10287 path = vpath;
10288
10289 dirh = pr_fsio_opendir(path);
10290 if (dirh == NULL) {
10291 uint32_t status_code;
10292 const char *reason;
10293 int xerrno = errno;
10294
10295 (void) pr_trace_msg("fileperms", 1, "OPENDIR, user '%s' (UID %s, "
10296 "GID %s): error opening '%s': %s", session.user,
10297 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
10298 path, strerror(xerrno));
10299
10300 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10301 "error opening '%s': %s", path, strerror(xerrno));
10302
10303 status_code = fxp_errno2status(xerrno, &reason);
10304
10305 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
10306 "('%s' [%d])", (unsigned long) status_code, reason,
10307 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
10308
10309 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
10310 fxp_cmd_dispatch_err(cmd2);
10311
10312 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10313 reason, NULL);
10314
10315 fxp_cmd_dispatch_err(cmd);
10316
10317 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10318 resp->payload = ptr;
10319 resp->payload_sz = (bufsz - buflen);
10320
10321 return fxp_packet_write(resp);
10322 }
10323
10324 fxh = fxp_handle_create(fxp_pool);
10325 if (fxh == NULL) {
10326 uint32_t status_code;
10327 const char *reason;
10328 int xerrno = errno;
10329
10330 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10331 "error creating SFTP handle for '%s': %s", path, strerror(xerrno));
10332
10333 status_code = fxp_errno2status(xerrno, &reason);
10334
10335 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
10336 "('%s' [%d])", (unsigned long) status_code, reason,
10337 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
10338
10339 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
10340 fxp_cmd_dispatch_err(cmd2);
10341
10342 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10343 reason, NULL);
10344
10345 fxp_cmd_dispatch_err(cmd);
10346
10347 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10348 resp->payload = ptr;
10349 resp->payload_sz = (bufsz - buflen);
10350
10351 return fxp_packet_write(resp);
10352 }
10353
10354 fxh->dirh = dirh;
10355 fxh->dir = pstrdup(fxh->pool, path);
10356
10357 if (fxp_handle_add(fxp->channel_id, fxh) < 0) {
10358 uint32_t status_code;
10359 const char *reason;
10360 int xerrno = errno;
10361
10362 buf = ptr;
10363 buflen = bufsz;
10364
10365 status_code = fxp_errno2status(xerrno, &reason);
10366
10367 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
10368 "('%s' [%d])", (unsigned long) status_code, reason,
10369 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
10370
10371 if (pr_fsio_closedir(dirh) < 0) {
10372 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10373 "error closing directory '%s': %s", fxh->dir, strerror(xerrno));
10374 }
10375
10376 destroy_pool(fxh->pool);
10377
10378 pr_response_add_err(R_451, "%s: %s", cmd2->arg, strerror(xerrno));
10379 fxp_cmd_dispatch_err(cmd2);
10380
10381 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10382 reason, NULL);
10383
10384 fxp_cmd_dispatch_err(cmd);
10385
10386 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10387 resp->payload = ptr;
10388 resp->payload_sz = (bufsz - buflen);
10389
10390 return fxp_packet_write(resp);
10391 }
10392
10393 pr_trace_msg(trace_channel, 8, "sending response: HANDLE %s",
10394 fxh->name);
10395
10396 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_HANDLE);
10397 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
10398 sftp_msg_write_string(&buf, &buflen, fxh->name);
10399
10400 /* If there is any existing transfer-specific data, leave it alone.
10401 *
10402 * Unlike FTP, SFTP allows for file downloads whilst in the middle of
10403 * a directory listing. Thus this OPENDIR could arrive while a file
10404 * is being read/written. Assume that the per-file stats are more
10405 * important.
10406 */
10407 if (session.xfer.p == NULL) {
10408 memset(&session.xfer, 0, sizeof(session.xfer));
10409
10410 session.xfer.p = make_sub_pool(fxp_pool);
10411 pr_pool_tag(session.xfer.p, "SFTP session transfer pool");
10412 memset(&session.xfer.start_time, 0, sizeof(session.xfer.start_time));
10413 gettimeofday(&session.xfer.start_time, NULL);
10414 session.xfer.direction = PR_NETIO_IO_WR;
10415 }
10416
10417 pr_timer_remove(PR_TIMER_STALLED, ANY_MODULE);
10418
10419 timeout_stalled = pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED);
10420 if (timeout_stalled > 0) {
10421 pr_timer_add(timeout_stalled, PR_TIMER_STALLED, NULL,
10422 fxp_timeout_stalled_cb, "TimeoutStalled");
10423 }
10424
10425 fxp_cmd_dispatch(cmd);
10426
10427 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10428 resp->payload = ptr;
10429 resp->payload_sz = (bufsz - buflen);
10430
10431 return fxp_packet_write(resp);
10432 }
10433
10434 static int fxp_handle_read(struct fxp_packet *fxp) {
10435 unsigned char *buf, *data = NULL, *ptr;
10436 char *file, *name, *ptr2;
10437 ssize_t res;
10438 uint32_t buflen, bufsz, datalen, max_readsz;
10439 uint64_t offset;
10440 struct fxp_handle *fxh;
10441 struct fxp_packet *resp;
10442 cmd_rec *cmd, *cmd2;
10443 pr_buffer_t *pbuf;
10444
10445 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
10446 offset = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
10447 datalen = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
10448
10449 /* We tell clients that request the "[email protected]" extension what
10450 * the maximum allowed READ length is; we should enforce that here.
10451 */
10452 max_readsz = FXP_MAX_PACKET_LEN - 1024;
10453
10454 if (datalen > max_readsz) {
10455 pr_trace_msg(trace_channel, 8,
10456 "READ requested len %lu exceeds max (%lu), truncating",
10457 (unsigned long) datalen, (unsigned long) max_readsz);
10458 datalen = max_readsz;
10459 }
10460
10461 cmd = fxp_cmd_alloc(fxp->pool, "READ", name);
10462 cmd->cmd_class = CL_READ|CL_SFTP;
10463 cmd->cmd_id = SFTP_CMD_ID;
10464
10465 pr_scoreboard_entry_update(session.pid,
10466 PR_SCORE_CMD, "%s", "READ", NULL, NULL);
10467 pr_scoreboard_entry_update(session.pid,
10468 PR_SCORE_CMD_ARG, "%s", name, NULL, NULL);
10469
10470 pr_proctitle_set("%s - %s: READ %s %" PR_LU " %lu", session.user,
10471 session.proc_prefix, name, (pr_off_t) offset, (unsigned long) datalen);
10472
10473 pr_trace_msg(trace_channel, 7, "received request: READ %s %" PR_LU " %lu",
10474 name, (pr_off_t) offset, (unsigned long) datalen);
10475
10476 buflen = bufsz = datalen + 64;
10477 buf = ptr = palloc(fxp->pool, bufsz);
10478
10479 fxh = fxp_handle_get(name);
10480 if (fxh == NULL) {
10481 uint32_t status_code;
10482
10483 pr_trace_msg(trace_channel, 17,
10484 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
10485 name, strerror(errno));
10486
10487 status_code = SSH2_FX_INVALID_HANDLE;
10488
10489 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10490 (unsigned long) status_code, fxp_strerror(status_code));
10491
10492 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10493 fxp_strerror(status_code), NULL);
10494
10495 fxp_cmd_dispatch_err(cmd);
10496
10497 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10498 resp->payload = ptr;
10499 resp->payload_sz = (bufsz - buflen);
10500
10501 return fxp_packet_write(resp);
10502 }
10503
10504 if (fxh->fh == NULL) {
10505 uint32_t status_code = SSH2_FX_INVALID_HANDLE;
10506
10507 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10508 (unsigned long) status_code, fxp_strerror(status_code));
10509
10510 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10511 fxp_strerror(status_code), NULL);
10512
10513 fxp_cmd_dispatch_err(cmd);
10514
10515 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10516 resp->payload = ptr;
10517 resp->payload_sz = (bufsz - buflen);
10518
10519 return fxp_packet_write(resp);
10520 }
10521
10522 /* Add a note containing the file handle for logging (Bug#3707). */
10523 fxp_set_filehandle_note(cmd, fxh);
10524
10525 pr_scoreboard_entry_update(session.pid,
10526 PR_SCORE_CMD_ARG, "%s", fxh->fh->fh_path, NULL, NULL);
10527
10528 if ((off_t) offset > fxh->fh_st->st_size) {
10529 uint32_t status_code;
10530 const char *reason;
10531 int xerrno = EOF;
10532
10533 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10534 "requested read offset (%" PR_LU " bytes) greater than size of "
10535 "'%s' (%" PR_LU " bytes)", (pr_off_t) offset, fxh->fh->fh_path,
10536 (pr_off_t) fxh->fh_st->st_size);
10537
10538 status_code = fxp_errno2status(xerrno, &reason);
10539
10540 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
10541 "('%s' [%d])", (unsigned long) status_code, reason, "End of file",
10542 xerrno);
10543
10544 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10545 reason, NULL);
10546
10547 fxp_cmd_dispatch_err(cmd);
10548
10549 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10550 resp->payload = ptr;
10551 resp->payload_sz = (bufsz - buflen);
10552
10553 return fxp_packet_write(resp);
10554 }
10555
10556 pr_scoreboard_entry_update(session.pid,
10557 PR_SCORE_XFER_SIZE, fxh->fh_st->st_size,
10558 PR_SCORE_XFER_DONE, (off_t) offset,
10559 NULL);
10560
10561 /* Trim the full path to just the filename, for our RETR command. */
10562 ptr2 = strrchr(fxh->fh->fh_path, '/');
10563 if (ptr2 != NULL &&
10564 ptr2 != fxh->fh->fh_path) {
10565 file = pstrdup(fxp->pool, ptr2 + 1);
10566
10567 } else {
10568 file = fxh->fh->fh_path;
10569 }
10570
10571 cmd2 = fxp_cmd_alloc(fxp->pool, C_RETR, file);
10572 cmd2->cmd_class = CL_READ;
10573
10574 if (fxh->fh_have_cached_read_dir_check == FALSE) {
10575 fxh->fh_cached_read_dir_check = dir_check(fxp->pool, cmd2, G_READ,
10576 fxh->fh->fh_path, NULL);
10577 fxh->fh_have_cached_read_dir_check = TRUE;
10578 }
10579
10580 if (!(fxh->fh_cached_read_dir_check)) {
10581 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
10582
10583 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10584 "READ of '%s' blocked by <Limit> configuration", fxh->fh->fh_path);
10585
10586 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10587 (unsigned long) status_code, fxp_strerror(status_code));
10588
10589 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10590 fxp_strerror(status_code), NULL);
10591
10592 fxp_cmd_dispatch_err(cmd);
10593
10594 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10595 resp->payload = ptr;
10596 resp->payload_sz = (bufsz - buflen);
10597
10598 return fxp_packet_write(resp);
10599 }
10600
10601 /* XXX Check MaxRetrieveFileSize */
10602
10603 if (fxp_path_pass_regex_filters(fxp->pool, "READ", fxh->fh->fh_path) < 0) {
10604 uint32_t status_code;
10605 const char *reason;
10606
10607 status_code = fxp_errno2status(errno, &reason);
10608
10609 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10610 (unsigned long) status_code, reason);
10611
10612 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10613 reason, NULL);
10614
10615 fxp_cmd_dispatch_err(cmd);
10616
10617 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10618 resp->payload = ptr;
10619 resp->payload_sz = (bufsz - buflen);
10620
10621 return fxp_packet_write(resp);
10622 }
10623
10624 if (S_ISREG(fxh->fh_st->st_mode)) {
10625 off_t *file_offset;
10626
10627 /* Stash the offset at which we're reading from this file. */
10628 file_offset = palloc(cmd->pool, sizeof(off_t));
10629 *file_offset = (off_t) offset;
10630 (void) pr_table_add(cmd->notes, "mod_xfer.file-offset", file_offset,
10631 sizeof(off_t));
10632 }
10633
10634 cmd2 = fxp_cmd_alloc(fxp->pool, C_RETR, NULL);
10635 pr_throttle_init(cmd2);
10636
10637 if (datalen > 0) {
10638 data = palloc(fxp->pool, datalen);
10639 res = pr_fsio_pread(fxh->fh, data, datalen, offset);
10640
10641 } else {
10642 res = 0;
10643 }
10644
10645 if (pr_data_get_timeout(PR_DATA_TIMEOUT_NO_TRANSFER) > 0) {
10646 pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
10647 }
10648
10649 if (pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED) > 0) {
10650 pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
10651 }
10652
10653 if (res <= 0) {
10654 uint32_t status_code;
10655 const char *reason;
10656 int xerrno;
10657
10658 if (res < 0) {
10659 xerrno = errno;
10660
10661 (void) pr_trace_msg("fileperms", 1, "READ, user '%s' (UID %s, GID %s): "
10662 "error reading from '%s': %s", session.user,
10663 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
10664 fxh->fh->fh_path, strerror(xerrno));
10665
10666 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10667 "error reading from '%s': %s", fxh->fh->fh_path, strerror(xerrno));
10668
10669 errno = xerrno;
10670
10671 } else {
10672 /* Assume EOF */
10673 pr_throttle_pause(offset, TRUE, 0);
10674 xerrno = EOF;
10675 }
10676
10677 status_code = fxp_errno2status(xerrno, &reason);
10678
10679 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
10680 "('%s' [%d])", (unsigned long) status_code, reason,
10681 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
10682
10683 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10684 reason, NULL);
10685
10686 if (xerrno != EOF) {
10687 fxp_cmd_dispatch_err(cmd);
10688
10689 } else {
10690 fxp_cmd_dispatch(cmd);
10691 }
10692
10693 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10694 resp->payload = ptr;
10695 resp->payload_sz = (bufsz - buflen);
10696
10697 return fxp_packet_write(resp);
10698 }
10699
10700 pr_throttle_pause(offset, FALSE, 0);
10701
10702 pr_trace_msg(trace_channel, 8, "sending response: DATA (%lu bytes)",
10703 (unsigned long) res);
10704
10705 pbuf = pcalloc(fxp->pool, sizeof(pr_buffer_t));
10706 pbuf->buf = (char *) data;
10707 pbuf->buflen = res;
10708 pbuf->current = pbuf->buf;
10709 pbuf->remaining = 0;
10710 pr_event_generate("mod_sftp.sftp.data-write", pbuf);
10711
10712 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_DATA);
10713 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
10714 sftp_msg_write_data(&buf, &buflen, data, res, TRUE);
10715
10716 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10717 resp->payload = ptr;
10718 resp->payload_sz = (bufsz - buflen);
10719
10720 fxh->xfer.total_bytes += res;
10721 session.total_bytes += res;
10722
10723 fxp_set_filehandle_sess_xfer(fxh);
10724 fxp_cmd_dispatch(cmd);
10725
10726 res = fxp_packet_write(resp);
10727 return res;
10728 }
10729
10730 static int fxp_handle_readdir(struct fxp_packet *fxp) {
10731 register unsigned int i;
10732 unsigned char *buf;
10733 char *cmd_name, *name;
10734 uint32_t attr_flags, buflen, curr_packet_pathsz = 0, max_packetsz;
10735 struct dirent *dent;
10736 struct fxp_buffer *fxb;
10737 struct fxp_dirent **paths;
10738 struct fxp_handle *fxh;
10739 struct fxp_packet *resp;
10740 array_header *path_list;
10741 cmd_rec *cmd;
10742 int have_error = FALSE, have_eod = TRUE, res;
10743 mode_t *fake_mode = NULL;
10744 const char *fake_user = NULL, *fake_group = NULL, *vwd = NULL;
10745
10746 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
10747
10748 cmd = fxp_cmd_alloc(fxp->pool, "READDIR", name);
10749 cmd->cmd_class = CL_DIRS|CL_SFTP;
10750 cmd->cmd_id = SFTP_CMD_ID;
10751 cmd->group = G_DIRS;
10752
10753 pr_scoreboard_entry_update(session.pid,
10754 PR_SCORE_CMD, "%s", "READDIR", NULL, NULL);
10755 pr_scoreboard_entry_update(session.pid,
10756 PR_SCORE_CMD_ARG, "%s", name, NULL, NULL);
10757
10758 pr_proctitle_set("%s - %s: READDIR %s", session.user, session.proc_prefix,
10759 name);
10760
10761 pr_trace_msg(trace_channel, 7, "received request: READDIR %s", name);
10762
10763 /* XXX What's a good size here? */
10764
10765 fxb = pcalloc(fxp->pool, sizeof(struct fxp_buffer));
10766
10767 max_packetsz = sftp_channel_get_max_packetsz();
10768 fxb->bufsz = buflen = max_packetsz;
10769 fxb->ptr = buf = palloc(fxp->pool, fxb->bufsz);
10770
10771 fxh = fxp_handle_get(name);
10772 if (fxh == NULL) {
10773 uint32_t status_code;
10774
10775 pr_trace_msg(trace_channel, 17,
10776 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
10777 name, strerror(errno));
10778
10779 status_code = SSH2_FX_INVALID_HANDLE;
10780
10781 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10782 (unsigned long) status_code, fxp_strerror(status_code));
10783
10784 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10785 fxp_strerror(status_code), NULL);
10786
10787 fxp_cmd_dispatch_err(cmd);
10788
10789 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10790 resp->payload = fxb->ptr;
10791 resp->payload_sz = (fxb->bufsz - buflen);
10792
10793 return fxp_packet_write(resp);
10794 }
10795
10796 if (fxh->dirh == NULL) {
10797 uint32_t status_code = SSH2_FX_INVALID_HANDLE;
10798
10799 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10800 (unsigned long) status_code, fxp_strerror(status_code));
10801
10802 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10803 fxp_strerror(status_code), NULL);
10804
10805 fxp_cmd_dispatch_err(cmd);
10806
10807 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10808 resp->payload = fxb->ptr;
10809 resp->payload_sz = (fxb->bufsz - buflen);
10810
10811 return fxp_packet_write(resp);
10812 }
10813
10814 pr_scoreboard_entry_update(session.pid,
10815 PR_SCORE_CMD_ARG, "%s", fxh->dir, NULL, NULL);
10816
10817 path_list = make_array(fxp->pool, 5, sizeof(struct fxp_dirent *));
10818
10819 cmd_name = cmd->argv[0];
10820
10821 /* If blocked by <Limit LIST>/<Limit NLST>, return EOF immediately. */
10822 pr_cmd_set_name(cmd, C_LIST);
10823 res = dir_check(fxp->pool, cmd, cmd->group, (char *) fxh->dir, NULL);
10824 if (res == 0) {
10825 have_error = TRUE;
10826 }
10827
10828 if (!have_error) {
10829 pr_cmd_set_name(cmd, C_NLST);
10830 res = dir_check(fxp->pool, cmd, cmd->group, (char *) fxh->dir, NULL);
10831 if (res == 0) {
10832 have_error = TRUE;
10833 }
10834 }
10835
10836 pr_cmd_set_name(cmd, "READDIR");
10837 res = dir_check(fxp->pool, cmd, cmd->group, (char *) fxh->dir, NULL);
10838 if (res == 2) {
10839 /* Explicitly allowed by <Limit READDIR> configuration. */
10840 have_error = FALSE;
10841
10842 } else if (res == 0) {
10843 have_error = TRUE;
10844 }
10845
10846 if (have_error == TRUE) {
10847 uint32_t status_code = SSH2_FX_EOF;
10848
10849 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10850 "READDIR of '%s' blocked by <Limit %s> configuration", fxh->dir,
10851 (char *) cmd->argv[0]);
10852
10853 pr_cmd_set_name(cmd, cmd_name);
10854
10855 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10856 (unsigned long) status_code, fxp_strerror(status_code));
10857
10858 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10859 fxp_strerror(status_code), NULL);
10860
10861 fxp_cmd_dispatch_err(cmd);
10862
10863 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10864 resp->payload = fxb->ptr;
10865 resp->payload_sz = (fxb->bufsz - buflen);
10866
10867 return fxp_packet_write(resp);
10868 }
10869
10870 pr_cmd_set_name(cmd, cmd_name);
10871
10872 /* Change into the directory being read, so that ".", "..", and relative
10873 * paths (e.g. for symlinks) get resolved properly.
10874 *
10875 * We need to dup the string returned by pr_fs_getvwd(), since it returns
10876 * a pointer to a static string which is changed by the call we make
10877 * to pr_fsio_chdir().
10878 */
10879 vwd = pstrdup(fxp->pool, pr_fs_getvwd());
10880
10881 res = pr_fsio_chdir(fxh->dir, FALSE);
10882 if (res < 0) {
10883 uint32_t status_code;
10884 const char *reason;
10885 int xerrno = errno;
10886
10887 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
10888 "unable to chdir to '%s': %s", (char *) fxh->dir, strerror(xerrno));
10889
10890 status_code = fxp_errno2status(xerrno, &reason);
10891
10892 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
10893 (unsigned long) status_code, reason);
10894
10895 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
10896 fxp_strerror(status_code), NULL);
10897
10898 fxp_cmd_dispatch_err(cmd);
10899
10900 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
10901 resp->payload = fxb->ptr;
10902 resp->payload_sz = (fxb->bufsz - buflen);
10903
10904 return fxp_packet_write(resp);
10905 }
10906
10907 fake_mode = get_param_ptr(get_dir_ctxt(fxp->pool, (char *) fxh->dir),
10908 "DirFakeMode", FALSE);
10909
10910 fake_user = get_param_ptr(get_dir_ctxt(fxp->pool, (char *) fxh->dir),
10911 "DirFakeUser", FALSE);
10912 if (fake_user != NULL &&
10913 strncmp(fake_user, "~", 2) == 0) {
10914 fake_user = session.user;
10915 }
10916
10917 fake_group = get_param_ptr(get_dir_ctxt(fxp->pool, (char *) fxh->dir),
10918 "DirFakeGroup", FALSE);
10919 if (fake_group != NULL &&
10920 strncmp(fake_group, "~", 2) == 0) {
10921 fake_group = session.group;
10922 }
10923
10924 while ((dent = pr_fsio_readdir(fxh->dirh)) != NULL) {
10925 char *real_path;
10926 struct fxp_dirent *fxd;
10927 uint32_t curr_packetsz, max_entry_metadata, max_entrysz;
10928 size_t dent_len;
10929
10930 pr_signals_handle();
10931
10932 /* How much non-path data do we expect to be associated with this entry? */
10933 #if defined(PR_USE_XATTR)
10934 /* Note that the "extra space" to allocate for extended attributes is
10935 * currently a bit of a guess. Initially, this was 4K; that was causing
10936 * slower directory listings due to the need for more READDIR requests,
10937 * since we were sending fewer entries back (limited by the max packet
10938 * size) per READDIR request.
10939 *
10940 * Now, we are trying 1K, and will see how that does.
10941 */
10942 max_entry_metadata = 1024;
10943 #else
10944 max_entry_metadata = 256;
10945 #endif /* PR_USE_XATTR */
10946
10947 max_entrysz = (PR_TUNABLE_PATH_MAX + 1 + max_entry_metadata);
10948
10949 /* Do not expand/resolve dot directories; it will be handled automatically
10950 * lower down in the ACL-checking code. Plus, this allows regex filters
10951 * that rely on the dot directory name to work properly.
10952 */
10953 if (!is_dotdir(dent->d_name)) {
10954 real_path = pdircat(fxp->pool, fxh->dir, dent->d_name, NULL);
10955
10956 } else {
10957 real_path = pstrdup(fxp->pool, dent->d_name);
10958 }
10959
10960 fxd = fxp_get_dirent(fxp->pool, cmd, real_path, fake_mode);
10961 if (fxd == NULL) {
10962 int xerrno = errno;
10963
10964 pr_trace_msg(trace_channel, 3,
10965 "unable to obtain directory listing for '%s': %s", real_path,
10966 strerror(xerrno));
10967
10968 continue;
10969 }
10970
10971 dent_len = strlen(dent->d_name);
10972 fxd->client_path = pstrndup(fxp->pool, dent->d_name, dent_len);
10973 curr_packet_pathsz += (dent_len + 1);
10974
10975 *((struct fxp_dirent **) push_array(path_list)) = fxd;
10976
10977 /* We determine the number of entries to send in this packet based on
10978 * the maximum packet size and the max entry size.
10979 *
10980 * We assume that each entry will need up to PR_TUNABLE_PATH_MAX+1 bytes for
10981 * the filename, and max_entry_metadata bytes of associated data.
10982 *
10983 * We have the total number of entries for this message when there is less
10984 * than enough space for one more maximum-sized entry.
10985 */
10986
10987 curr_packetsz = curr_packet_pathsz +
10988 (path_list->nelts * max_entry_metadata);
10989 if ((max_packetsz - curr_packetsz) <= max_entrysz) {
10990 have_eod = FALSE;
10991 break;
10992 }
10993 }
10994
10995 if (pr_data_get_timeout(PR_DATA_TIMEOUT_NO_TRANSFER) > 0) {
10996 pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
10997 }
10998
10999 if (pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED) > 0) {
11000 pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
11001 }
11002
11003 /* Now make sure we switch back to the directory where we were. */
11004 res = pr_fsio_chdir(vwd, FALSE);
11005 if (res < 0) {
11006 uint32_t status_code;
11007 const char *reason;
11008 int xerrno = errno;
11009
11010 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11011 "unable to chdir to '%s': %s", vwd, strerror(xerrno));
11012
11013 status_code = fxp_errno2status(xerrno, &reason);
11014
11015 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
11016 (unsigned long) status_code, reason);
11017
11018 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11019 fxp_strerror(status_code), NULL);
11020
11021 fxp_cmd_dispatch_err(cmd);
11022
11023 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11024 resp->payload = fxb->ptr;
11025 resp->payload_sz = (fxb->bufsz - buflen);
11026
11027 return fxp_packet_write(resp);
11028 }
11029
11030 if (path_list->nelts == 0) {
11031 /* We have reached the end of the directory entries; send an EOF. */
11032 uint32_t status_code = SSH2_FX_EOF;
11033
11034 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
11035 (unsigned long) status_code, fxp_strerror(status_code));
11036
11037 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11038 fxp_strerror(status_code), NULL);
11039
11040 fxp_cmd_dispatch(cmd);
11041
11042 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11043 resp->payload = fxb->ptr;
11044 resp->payload_sz = (fxb->bufsz - buflen);
11045
11046 return fxp_packet_write(resp);
11047 }
11048
11049 pr_trace_msg(trace_channel, 8, "sending response: NAME (%lu count)",
11050 (unsigned long) path_list->nelts);
11051
11052 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_NAME);
11053 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
11054 sftp_msg_write_int(&buf, &buflen, path_list->nelts);
11055
11056 fxb->buf = buf;
11057 fxb->buflen = buflen;
11058 paths = path_list->elts;
11059
11060 /* For READDIR requests, since they do NOT contain a flags field for clients
11061 * to express which attributes they want, we ASSUME some standard fields.
11062 */
11063
11064 if (fxp_session->client_version <= 3) {
11065 attr_flags = SSH2_FX_ATTR_SIZE|SSH2_FX_ATTR_UIDGID|SSH2_FX_ATTR_PERMISSIONS|
11066 SSH2_FX_ATTR_ACMODTIME;
11067
11068 } else {
11069 attr_flags = SSH2_FX_ATTR_SIZE|SSH2_FX_ATTR_PERMISSIONS|
11070 SSH2_FX_ATTR_ACCESSTIME|SSH2_FX_ATTR_MODIFYTIME|SSH2_FX_ATTR_OWNERGROUP;
11071 }
11072
11073 /* The FX_ATTR_LINK_COUNT attribute was defined in
11074 * draft-ietf-secsh-filexfer-06, which is SFTP protocol version 6.
11075 */
11076 if (fxp_session->client_version >= 6) {
11077 attr_flags |= SSH2_FX_ATTR_LINK_COUNT;
11078
11079 /* The FX_ATTR_EXTENDED attribute was defined in
11080 * draft-ietf-secsh-filexfer-02, which is SFTP protocol version 3.
11081 * However, many SFTP clients may not be prepared for handling these.
11082 * Thus we CHOOSE to only provide these extended attributes, if supported,
11083 * to protocol version 6 clients.
11084 */
11085 #if defined(PR_USE_XATTR)
11086 if (!(fxp_fsio_opts & PR_FSIO_OPT_IGNORE_XATTR)) {
11087 attr_flags |= SSH2_FX_ATTR_EXTENDED;
11088 }
11089 #endif /* PR_USE_XATTR */
11090 }
11091
11092 for (i = 0; i < path_list->nelts; i++) {
11093 uint32_t name_len = 0;
11094
11095 name_len = fxp_name_write(fxp->pool, fxb, paths[i]->client_path,
11096 paths[i]->st, attr_flags, fake_user, fake_group);
11097
11098 pr_trace_msg(trace_channel, 19, "READDIR: FXP_NAME entry size: %lu bytes",
11099 (unsigned long) name_len);
11100 }
11101
11102 /* fxp_name_write will have changed the values stashed in the buffer. */
11103 buf = fxb->buf;
11104 buflen = fxb->buflen;
11105
11106 if (fxp_session->client_version > 5) {
11107 sftp_msg_write_bool(&buf, &buflen, have_eod ? TRUE : FALSE);
11108 }
11109
11110 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11111 resp->payload = fxb->ptr;
11112 resp->payload_sz = (fxb->bufsz - buflen);
11113
11114 session.xfer.total_bytes += resp->payload_sz;
11115 session.total_bytes += resp->payload_sz;
11116
11117 fxp_cmd_dispatch(cmd);
11118
11119 return fxp_packet_write(resp);
11120 }
11121
11122 static int fxp_handle_readlink(struct fxp_packet *fxp) {
11123 char data[PR_TUNABLE_PATH_MAX + 1];
11124 unsigned char *buf;
11125 char *path, *resolved_path;
11126 int res;
11127 uint32_t buflen;
11128 struct fxp_buffer *fxb;
11129 struct fxp_packet *resp;
11130 cmd_rec *cmd;
11131
11132 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
11133 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
11134 path = sftp_utf8_decode_str(fxp->pool, path);
11135 }
11136
11137 pr_scoreboard_entry_update(session.pid,
11138 PR_SCORE_CMD, "%s", "READLINK", NULL, NULL);
11139 pr_scoreboard_entry_update(session.pid,
11140 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
11141
11142 pr_proctitle_set("%s - %s: READLINK %s", session.user, session.proc_prefix,
11143 path);
11144
11145 pr_trace_msg(trace_channel, 7, "received request: READLINK %s", path);
11146
11147 if (strlen(path) == 0) {
11148 /* Use the default directory if the path is empty. */
11149 path = sftp_auth_get_default_dir();
11150
11151 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11152 "empty path given in READLINK request, using '%s'", path);
11153 }
11154
11155 cmd = fxp_cmd_alloc(fxp->pool, "READLINK", path);
11156 cmd->cmd_class = CL_READ|CL_SFTP;
11157 cmd->cmd_id = SFTP_CMD_ID;
11158
11159 fxb = pcalloc(fxp->pool, sizeof(struct fxp_buffer));
11160 fxb->bufsz = buflen = FXP_RESPONSE_NAME_DEFAULT_SZ;
11161 fxb->ptr = buf = palloc(fxp->pool, fxb->bufsz);
11162
11163 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
11164 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
11165
11166 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11167 "READLINK of '%s' blocked by '%s' handler: %s", path,
11168 (char *) cmd->argv[0], strerror(errno));
11169
11170 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
11171 (unsigned long) status_code, fxp_strerror(status_code));
11172
11173 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11174 fxp_strerror(status_code), NULL);
11175
11176 fxp_cmd_dispatch_err(cmd);
11177
11178 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11179 resp->payload = fxb->ptr;
11180 resp->payload_sz = (fxb->bufsz - buflen);
11181
11182 return fxp_packet_write(resp);
11183 }
11184
11185 /* The path may have been changed by any PRE_CMD handlers. */
11186 path = cmd->arg;
11187 pr_fs_clear_cache2(path);
11188
11189 resolved_path = dir_best_path(fxp->pool, path);
11190 if (resolved_path == NULL) {
11191 int xerrno = EACCES;
11192 const char *reason;
11193 uint32_t status_code;
11194
11195 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11196 "READLINK request denied: unable to access path '%s'", cmd->arg);
11197
11198 status_code = fxp_errno2status(xerrno, &reason);
11199
11200 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
11201 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
11202 xerrno);
11203
11204 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11205 reason, NULL);
11206
11207 fxp_cmd_dispatch_err(cmd);
11208
11209 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11210 resp->payload = fxb->ptr;
11211 resp->payload_sz = (fxb->bufsz - buflen);
11212
11213 return fxp_packet_write(resp);
11214 }
11215
11216 if (!dir_check(fxp->pool, cmd, G_READ, resolved_path, NULL)) {
11217 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
11218
11219 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11220 "READLINK of '%s' (resolved to '%s') blocked by <Limit %s> configuration",
11221 path, resolved_path, (char *) cmd->argv[0]);
11222
11223 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
11224 (unsigned long) status_code, fxp_strerror(status_code));
11225
11226 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11227 fxp_strerror(status_code), NULL);
11228
11229 fxp_cmd_dispatch_err(cmd);
11230
11231 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11232 resp->payload = fxb->ptr;
11233 resp->payload_sz = (fxb->bufsz - buflen);
11234
11235 return fxp_packet_write(resp);
11236 }
11237
11238 memset(data, '\0', sizeof(data));
11239
11240 /* Note: do NOT use the resolved_path variable here, as it will have
11241 * resolved by following any symlinks; readlink(2) would then return EINVAL
11242 * for reading a non-symlink path.
11243 */
11244 res = dir_readlink(fxp->pool, path, data, sizeof(data) - 1,
11245 PR_DIR_READLINK_FL_HANDLE_REL_PATH);
11246 if (res < 0) {
11247 uint32_t status_code;
11248 const char *reason;
11249 int xerrno = errno;
11250
11251 buf = fxb->ptr;
11252 buflen = fxb->bufsz;
11253
11254 status_code = fxp_errno2status(xerrno, &reason);
11255
11256 (void) pr_trace_msg("fileperms", 1, "READLINK, user '%s' (UID %s, "
11257 "GID %s): error using readlink() on '%s': %s", session.user,
11258 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
11259 path, strerror(xerrno));
11260
11261 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
11262 "('%s' [%d])", (unsigned long) status_code, reason,
11263 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
11264
11265 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11266 reason, NULL);
11267
11268 fxp_cmd_dispatch_err(cmd);
11269
11270 } else {
11271 struct stat st;
11272 const char *fake_user = NULL, *fake_group = NULL;
11273
11274 memset(&st, 0, sizeof(struct stat));
11275
11276 data[res] = '\0';
11277
11278 pr_trace_msg(trace_channel, 8, "sending response: NAME 1 %s %s",
11279 data, fxp_strattrs(fxp->pool, &st, NULL));
11280
11281 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_NAME);
11282 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
11283 sftp_msg_write_int(&buf, &buflen, 1);
11284
11285 fake_user = get_param_ptr(get_dir_ctxt(fxp->pool, path), "DirFakeUser",
11286 FALSE);
11287 if (fake_user != NULL &&
11288 strncmp(fake_user, "~", 2) == 0) {
11289 fake_user = session.user;
11290 }
11291
11292 fake_group = get_param_ptr(get_dir_ctxt(fxp->pool, path), "DirFakeGroup",
11293 FALSE);
11294 if (fake_group != NULL &&
11295 strncmp(fake_group, "~", 2) == 0) {
11296 fake_group = session.group;
11297 }
11298
11299 fxb->buf = buf;
11300 fxb->buflen = buflen;
11301
11302 fxp_name_write(fxp->pool, fxb, data, &st, 0, fake_user, fake_group);
11303
11304 buf = fxb->buf;
11305 buflen = fxb->buflen;
11306
11307 fxp_cmd_dispatch(cmd);
11308 }
11309
11310 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11311 resp->payload = fxb->ptr;
11312 resp->payload_sz = (fxb->bufsz - buflen);
11313
11314 return fxp_packet_write(resp);
11315 }
11316
11317 static void fxp_trace_v6_realpath_flags(pool *p, unsigned char flags,
11318 int client_sent) {
11319 char *flags_str = "";
11320 int trace_level = 15;
11321
11322 if (pr_trace_get_level(trace_channel) < trace_level) {
11323 return;
11324 }
11325
11326 switch (flags) {
11327 case SSH2_FXRP_NO_CHECK:
11328 flags_str = "FX_REALPATH_NO_CHECK";
11329 break;
11330
11331 case SSH2_FXRP_STAT_IF:
11332 flags_str = "FX_REALPATH_STAT_IF";
11333 break;
11334
11335 case SSH2_FXRP_STAT_ALWAYS:
11336 flags_str = "FX_REALPATH_STAT_ALWAYS";
11337 break;
11338 }
11339
11340 pr_trace_msg(trace_channel, trace_level, "REALPATH flags = %s (%s)",
11341 flags_str, client_sent == TRUE ? "explicit" : "default");
11342 }
11343
11344 static int fxp_handle_realpath(struct fxp_packet *fxp) {
11345 int res, xerrno;
11346 unsigned char *buf, realpath_flags = SSH2_FXRP_NO_CHECK;
11347 char *path;
11348 uint32_t buflen;
11349 struct stat st;
11350 struct fxp_buffer *fxb;
11351 struct fxp_packet *resp;
11352 cmd_rec *cmd;
11353
11354 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
11355 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
11356 path = sftp_utf8_decode_str(fxp->pool, path);
11357 }
11358
11359 pr_scoreboard_entry_update(session.pid,
11360 PR_SCORE_CMD, "%s", "REALPATH", NULL, NULL);
11361 pr_scoreboard_entry_update(session.pid,
11362 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
11363
11364 pr_proctitle_set("%s - %s: REALPATH %s", session.user, session.proc_prefix,
11365 path);
11366
11367 pr_trace_msg(trace_channel, 7, "received request: REALPATH %s", path);
11368
11369 if (strlen(path) == 0) {
11370 /* Use the default directory if the path is empty. */
11371 path = sftp_auth_get_default_dir();
11372
11373 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11374 "empty path given in REALPATH request, using '%s'", path);
11375 }
11376
11377 cmd = fxp_cmd_alloc(fxp->pool, "REALPATH", path);
11378 cmd->cmd_class = CL_INFO|CL_DIRS|CL_SFTP;
11379 cmd->cmd_id = SFTP_CMD_ID;
11380
11381 if (fxp_session->client_version >= 6) {
11382 /* See Section 8.9 of:
11383 *
11384 * http://tools.ietf.org/id/draft-ietf-secsh-filexfer-13.txt
11385 *
11386 * for the semantics and defaults of these crazy flags.
11387 */
11388
11389 if (fxp->payload_sz >= sizeof(char)) {
11390 char *composite_path = NULL;
11391
11392 realpath_flags = sftp_msg_read_byte(fxp->pool, &fxp->payload,
11393 &fxp->payload_sz);
11394 fxp_trace_v6_realpath_flags(fxp->pool, realpath_flags, TRUE);
11395
11396 if (fxp->payload_sz > 0) {
11397 composite_path = sftp_msg_read_string(fxp->pool, &fxp->payload,
11398 &fxp->payload_sz);
11399
11400 /* XXX One problem with the most recent SFTP Draft is that it does NOT
11401 * include a count of the number of composite-paths that the client
11402 * may send. The format of the REALPATH request, currently, only allows
11403 * for one composite-path element; the description of this feature
11404 * implies that multiple such composite-path elements could be supplied.
11405 * Sigh. Maybe it's meant to a blob of strings? Or we keep reading
11406 * a string until the remaining payload size is zero?
11407 */
11408 pr_trace_msg(trace_channel, 13,
11409 "REALPATH request set composite-path: '%s'", composite_path);
11410 }
11411
11412 } else {
11413 fxp_trace_v6_realpath_flags(fxp->pool, realpath_flags, FALSE);
11414 }
11415 }
11416
11417 fxb = pcalloc(fxp->pool, sizeof(struct fxp_buffer));
11418 fxb->bufsz = buflen = FXP_RESPONSE_NAME_DEFAULT_SZ;
11419 fxb->ptr = buf = palloc(fxp->pool, fxb->bufsz);
11420
11421 res = pr_cmd_dispatch_phase(cmd, PRE_CMD, 0);
11422 if (res < 0) {
11423 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
11424
11425 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11426 "REALPATH of '%s' blocked by '%s' handler: %s", path,
11427 (char *) cmd->argv[0], strerror(errno));
11428
11429 if (fxp_session->client_version <= 5 ||
11430 (fxp_session->client_version >= 6 &&
11431 realpath_flags != SSH2_FXRP_NO_CHECK)) {
11432 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
11433 (unsigned long) status_code, fxp_strerror(status_code));
11434
11435 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11436 fxp_strerror(status_code), NULL);
11437
11438 } else {
11439 uint32_t attr_flags = 0;
11440
11441 memset(&st, 0, sizeof(st));
11442 st.st_uid = (uid_t) -1;
11443 st.st_gid = (gid_t) -1;
11444
11445 pr_trace_msg(trace_channel, 8, "sending response: NAME 1 %s %s",
11446 path, fxp_strattrs(fxp->pool, &st, &attr_flags));
11447
11448 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_NAME);
11449 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
11450 sftp_msg_write_int(&buf, &buflen, 1);
11451
11452 fxb->buf = buf;
11453 fxb->buflen = buflen;
11454
11455 fxp_name_write(fxp->pool, fxb, path, &st, 0, "nobody", "nobody");
11456
11457 buf = fxb->buf;
11458 buflen = fxb->buflen;
11459 }
11460
11461 fxp_cmd_dispatch_err(cmd);
11462
11463 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11464 resp->payload = fxb->ptr;
11465 resp->payload_sz = (fxb->bufsz - buflen);
11466
11467 return fxp_packet_write(resp);
11468 }
11469
11470 /* The path may have been changed by any PRE_CMD handlers. */
11471 path = cmd->arg;
11472
11473 if (strncmp(path, ".", 2) == 0) {
11474 /* The client is asking about the current working directory. Easy. */
11475 path = (char *) pr_fs_getvwd();
11476
11477 } else {
11478 char *vpath;
11479
11480 vpath = dir_realpath(fxp->pool, path);
11481 if (vpath == NULL) {
11482 uint32_t status_code;
11483 const char *reason;
11484
11485 xerrno = errno;
11486
11487 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11488 "error resolving '%s': %s", path, strerror(xerrno));
11489
11490 status_code = fxp_errno2status(xerrno, &reason);
11491
11492 if (fxp_session->client_version <= 5 ||
11493 (fxp_session->client_version >= 6 &&
11494 realpath_flags != SSH2_FXRP_NO_CHECK)) {
11495 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
11496 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
11497 xerrno);
11498
11499 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11500 reason, NULL);
11501
11502 } else {
11503 uint32_t attr_flags = 0;
11504
11505 memset(&st, 0, sizeof(st));
11506 st.st_uid = (uid_t) -1;
11507 st.st_gid = (gid_t) -1;
11508
11509 pr_trace_msg(trace_channel, 8, "sending response: NAME 1 %s %s",
11510 path, fxp_strattrs(fxp->pool, &st, &attr_flags));
11511
11512 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_NAME);
11513 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
11514 sftp_msg_write_int(&buf, &buflen, 1);
11515
11516 fxb->buf = buf;
11517 fxb->buflen = buflen;
11518
11519 fxp_name_write(fxp->pool, fxb, path, &st, 0, "nobody", "nobody");
11520
11521 buf = fxb->buf;
11522 buflen = fxb->buflen;
11523 }
11524
11525 fxp_cmd_dispatch_err(cmd);
11526
11527 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11528 resp->payload = fxb->ptr;
11529 resp->payload_sz = (fxb->bufsz - buflen);
11530
11531 return fxp_packet_write(resp);
11532 }
11533
11534 pr_trace_msg(trace_channel, 15,
11535 "resolved client-sent path '%s' to local path '%s'", path, vpath);
11536 path = vpath;
11537 }
11538
11539 /* Force a full lookup. */
11540 pr_fs_clear_cache2(path);
11541 if (!dir_check_full(fxp->pool, cmd, G_DIRS, path, NULL)) {
11542 uint32_t status_code;
11543 const char *reason;
11544
11545 xerrno = errno;
11546
11547 status_code = SSH2_FX_PERMISSION_DENIED;
11548
11549 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11550 "REALPATH of '%s' blocked by <Limit> configuration", path);
11551
11552 buf = fxb->ptr;
11553 buflen = fxb->bufsz;
11554
11555 status_code = fxp_errno2status(xerrno, &reason);
11556
11557 if (fxp_session->client_version <= 5 ||
11558 (fxp_session->client_version >= 6 &&
11559 realpath_flags != SSH2_FXRP_NO_CHECK)) {
11560
11561 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
11562 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
11563 xerrno);
11564
11565 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11566 reason, NULL);
11567
11568 } else {
11569 uint32_t attr_flags = 0;
11570
11571 memset(&st, 0, sizeof(st));
11572 st.st_uid = (uid_t) -1;
11573 st.st_gid = (gid_t) -1;
11574
11575 pr_trace_msg(trace_channel, 8, "sending response: NAME 1 %s %s",
11576 path, fxp_strattrs(fxp->pool, &st, &attr_flags));
11577
11578 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_NAME);
11579 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
11580 sftp_msg_write_int(&buf, &buflen, 1);
11581
11582 fxb->buf = buf;
11583 fxb->buflen = buflen;
11584
11585 fxp_name_write(fxp->pool, fxb, path, &st, 0, "nobody", "nobody");
11586
11587 buf = fxb->buf;
11588 buflen = fxb->buflen;
11589 }
11590
11591 fxp_cmd_dispatch_err(cmd);
11592
11593 } else {
11594 /* draft-ietf-secsh-filexfer-13 says:
11595 *
11596 * SSH_FXP_REALPATH_NO_CHECK:
11597 * NOT resolve symbolic links (thus use lstat(2))
11598 *
11599 * SSH_FXP_REALPATH_STAT_IF:
11600 * stat(2) the file, but if the stat(2) fails, do NOT fail the request,
11601 * but send a NAME with type UNKNOWN.
11602 *
11603 * SSH_FXP_REALPATH_STAT_ALWAYS:
11604 * stat(2) the file, and return any error.
11605 */
11606
11607 pr_fs_clear_cache2(path);
11608 switch (realpath_flags) {
11609 case SSH2_FXRP_NO_CHECK:
11610 res = pr_fsio_lstat(path, &st);
11611 xerrno = errno;
11612 break;
11613
11614 case SSH2_FXRP_STAT_IF:
11615 case SSH2_FXRP_STAT_ALWAYS:
11616 res = pr_fsio_stat(path, &st);
11617 xerrno = errno;
11618 break;
11619 }
11620
11621 if (res < 0) {
11622 uint32_t status_code;
11623 const char *reason;
11624
11625 xerrno = errno;
11626
11627 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11628 "error checking '%s' for REALPATH: %s", path, strerror(xerrno));
11629
11630 buf = fxb->ptr;
11631 buflen = fxb->bufsz;
11632
11633 status_code = fxp_errno2status(xerrno, &reason);
11634
11635 if (fxp_session->client_version <= 5 ||
11636 (fxp_session->client_version >= 6 &&
11637 realpath_flags == SSH2_FXRP_STAT_ALWAYS)) {
11638
11639 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
11640 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
11641 xerrno);
11642
11643 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11644 reason, NULL);
11645
11646 } else {
11647 uint32_t attr_flags = 0;
11648
11649 memset(&st, 0, sizeof(st));
11650 st.st_uid = (uid_t) -1;
11651 st.st_gid = (gid_t) -1;
11652
11653 pr_trace_msg(trace_channel, 8, "sending response: NAME 1 %s %s",
11654 path, fxp_strattrs(fxp->pool, &st, &attr_flags));
11655
11656 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_NAME);
11657 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
11658 sftp_msg_write_int(&buf, &buflen, 1);
11659
11660 fxb->buf = buf;
11661 fxb->buflen = buflen;
11662
11663 fxp_name_write(fxp->pool, fxb, path, &st, 0, "nobody", "nobody");
11664
11665 buf = fxb->buf;
11666 buflen = fxb->buflen;
11667 }
11668
11669 fxp_cmd_dispatch_err(cmd);
11670
11671 } else {
11672 const char *fake_user = NULL, *fake_group = NULL;
11673
11674 pr_trace_msg(trace_channel, 8, "sending response: NAME 1 %s %s",
11675 path, fxp_strattrs(fxp->pool, &st, NULL));
11676
11677 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_NAME);
11678 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
11679 sftp_msg_write_int(&buf, &buflen, 1);
11680
11681 fake_user = get_param_ptr(get_dir_ctxt(fxp->pool, path), "DirFakeUser",
11682 FALSE);
11683 if (fake_user != NULL &&
11684 strncmp(fake_user, "~", 2) == 0) {
11685 fake_user = session.user;
11686 }
11687
11688 fake_group = get_param_ptr(get_dir_ctxt(fxp->pool, path), "DirFakeGroup",
11689 FALSE);
11690 if (fake_group != NULL &&
11691 strncmp(fake_group, "~", 2) == 0) {
11692 fake_group = session.group;
11693 }
11694
11695 fxb->buf = buf;
11696 fxb->buflen = buflen;
11697
11698 fxp_name_write(fxp->pool, fxb, path, &st, 0, fake_user, fake_group);
11699
11700 buf = fxb->buf;
11701 buflen = fxb->buflen;
11702
11703 fxp_cmd_dispatch(cmd);
11704 }
11705 }
11706
11707 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11708 resp->payload = fxb->ptr;
11709 resp->payload_sz = (fxb->bufsz - buflen);
11710
11711 return fxp_packet_write(resp);
11712 }
11713
11714 static int fxp_handle_remove(struct fxp_packet *fxp) {
11715 unsigned char *buf, *ptr;
11716 char *cmd_name, *path, *real_path;
11717 const char *reason;
11718 uint32_t buflen, bufsz, status_code;
11719 struct stat st;
11720 struct fxp_packet *resp;
11721 cmd_rec *cmd, *cmd2;
11722 int res;
11723
11724 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
11725 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
11726 path = sftp_utf8_decode_str(fxp->pool, path);
11727 }
11728
11729 pr_scoreboard_entry_update(session.pid,
11730 PR_SCORE_CMD, "%s", "REMOVE", NULL, NULL);
11731 pr_scoreboard_entry_update(session.pid,
11732 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
11733
11734 pr_proctitle_set("%s - %s: REMOVE %s", session.user, session.proc_prefix,
11735 path);
11736
11737 pr_trace_msg(trace_channel, 7, "received request: REMOVE %s", path);
11738
11739 if (strlen(path) == 0) {
11740 /* Use the default directory if the path is empty. */
11741 path = sftp_auth_get_default_dir();
11742
11743 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11744 "empty path given in REMOVE request, using '%s'", path);
11745 }
11746
11747 cmd = fxp_cmd_alloc(fxp->pool, "REMOVE", path);
11748 cmd->cmd_class = CL_WRITE|CL_SFTP;
11749 cmd->cmd_id = SFTP_CMD_ID;
11750
11751 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
11752 buf = ptr = palloc(fxp->pool, bufsz);
11753
11754 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
11755 status_code = SSH2_FX_PERMISSION_DENIED;
11756
11757 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11758 "REMOVE of '%s' blocked by '%s' handler: %s", path,
11759 (char *) cmd->argv[0], strerror(errno));
11760
11761 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
11762 (unsigned long) status_code, fxp_strerror(status_code));
11763
11764 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11765 fxp_strerror(status_code), NULL);
11766
11767 fxp_cmd_dispatch_err(cmd);
11768
11769 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11770 resp->payload = ptr;
11771 resp->payload_sz = (bufsz - buflen);
11772
11773 return fxp_packet_write(resp);
11774 }
11775
11776 path = cmd->arg;
11777
11778 cmd2 = fxp_cmd_alloc(fxp->pool, C_DELE, path);
11779 if (pr_cmd_dispatch_phase(cmd2, PRE_CMD, 0) < 0) {
11780 status_code = SSH2_FX_PERMISSION_DENIED;
11781
11782 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11783 "DELE of '%s' blocked by '%s' handler: %s", path,
11784 (char *) cmd2->argv[0], strerror(errno));
11785
11786 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
11787 (unsigned long) status_code, fxp_strerror(status_code));
11788
11789 pr_response_add_err(R_550, "%s: %s", path, strerror(EPERM));
11790 fxp_cmd_dispatch_err(cmd2);
11791
11792 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11793 fxp_strerror(status_code), NULL);
11794
11795 fxp_cmd_dispatch_err(cmd);
11796
11797 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11798 resp->payload = ptr;
11799 resp->payload_sz = (bufsz - buflen);
11800
11801 return fxp_packet_write(resp);
11802 }
11803
11804 /* The path may have been changed by any PRE_CMD handlers. */
11805 path = cmd2->arg;
11806
11807 cmd_name = cmd->argv[0];
11808 pr_cmd_set_name(cmd, C_DELE);
11809
11810 if (!dir_check_canon(fxp->pool, cmd, G_WRITE, path, NULL)) {
11811 status_code = SSH2_FX_PERMISSION_DENIED;
11812
11813 pr_cmd_set_name(cmd, cmd_name);
11814
11815 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11816 "REMOVE of '%s' blocked by <Limit> configuration", path);
11817
11818 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
11819 (unsigned long) status_code, fxp_strerror(status_code));
11820
11821 pr_response_add_err(R_550, "%s: %s", path, strerror(EPERM));
11822 fxp_cmd_dispatch_err(cmd2);
11823
11824 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11825 fxp_strerror(status_code), NULL);
11826
11827 fxp_cmd_dispatch_err(cmd);
11828
11829 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11830 resp->payload = ptr;
11831 resp->payload_sz = (bufsz - buflen);
11832
11833 return fxp_packet_write(resp);
11834 }
11835
11836 pr_cmd_set_name(cmd, cmd_name);
11837
11838 if (fxp_path_pass_regex_filters(fxp->pool, "REMOVE", path) < 0) {
11839 int xerrno = errno;
11840
11841 status_code = fxp_errno2status(errno, NULL);
11842
11843 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
11844 (unsigned long) status_code, fxp_strerror(status_code));
11845
11846 pr_response_add_err(R_550, "%s: %s", path, strerror(xerrno));
11847 fxp_cmd_dispatch_err(cmd2);
11848
11849 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11850 fxp_strerror(status_code), NULL);
11851
11852 fxp_cmd_dispatch_err(cmd);
11853
11854 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11855 resp->payload = ptr;
11856 resp->payload_sz = (bufsz - buflen);
11857
11858 return fxp_packet_write(resp);
11859 }
11860
11861 real_path = dir_canonical_path(fxp->pool, path);
11862 if (real_path == NULL) {
11863 int xerrno = errno;
11864
11865 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11866 "error resolving '%s': %s", path, strerror(xerrno));
11867
11868 status_code = fxp_errno2status(xerrno, &reason);
11869
11870 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
11871 "('%s' [%d])", (unsigned long) status_code, reason,
11872 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
11873
11874 pr_response_add_err(R_550, "%s: %s", path, strerror(xerrno));
11875 fxp_cmd_dispatch_err(cmd2);
11876
11877 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11878 reason, NULL);
11879
11880 fxp_cmd_dispatch_err(cmd);
11881
11882 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11883 resp->payload = ptr;
11884 resp->payload_sz = (bufsz - buflen);
11885
11886 return fxp_packet_write(resp);
11887 }
11888
11889 pr_fs_clear_cache2(real_path);
11890 res = pr_fsio_lstat(real_path, &st);
11891 if (res < 0) {
11892 int xerrno = errno;
11893
11894 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11895 "unable to check '%s': %s", real_path, strerror(xerrno));
11896
11897 status_code = fxp_errno2status(xerrno, &reason);
11898
11899 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
11900 "('%s' [%d])", (unsigned long) status_code, reason,
11901 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
11902
11903 pr_response_add_err(R_550, "%s: %s", path, strerror(xerrno));
11904 fxp_cmd_dispatch_err(cmd2);
11905
11906 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11907 reason, NULL);
11908
11909 fxp_cmd_dispatch_err(cmd);
11910
11911 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11912 resp->payload = ptr;
11913 resp->payload_sz = (bufsz - buflen);
11914
11915 return fxp_packet_write(resp);
11916 }
11917
11918 if (S_ISDIR(st.st_mode)) {
11919 int xerrno = EISDIR;
11920
11921 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11922 "unable to remove '%s': %s", real_path, strerror(xerrno));
11923
11924 status_code = fxp_errno2status(xerrno, &reason);
11925
11926 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
11927 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
11928 xerrno);
11929
11930 pr_response_add_err(R_550, "%s: %s", path, strerror(xerrno));
11931 fxp_cmd_dispatch_err(cmd2);
11932
11933 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11934 reason, NULL);
11935
11936 fxp_cmd_dispatch_err(cmd);
11937
11938 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11939 resp->payload = ptr;
11940 resp->payload_sz = (bufsz - buflen);
11941
11942 return fxp_packet_write(resp);
11943 }
11944
11945 res = pr_fsio_unlink(real_path);
11946 if (res < 0) {
11947 int xerrno = errno;
11948
11949 (void) pr_trace_msg("fileperms", 1, "REMOVE, user '%s' (UID %s, GID %s): "
11950 "error deleting '%s': %s", session.user,
11951 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
11952 real_path, strerror(xerrno));
11953
11954 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
11955 "error unlinking '%s': %s", real_path, strerror(xerrno));
11956
11957 pr_response_add_err(R_550, "%s: %s", path, strerror(xerrno));
11958 fxp_cmd_dispatch_err(cmd2);
11959
11960 errno = xerrno;
11961
11962 } else {
11963 char *abs_path;
11964
11965 /* The TransferLog format wants the full path to the deleted file,
11966 * regardless of a chroot.
11967 */
11968 abs_path = sftp_misc_vroot_abs_path(fxp->pool, path, TRUE);
11969
11970 xferlog_write(0, session.c->remote_name, st.st_size, abs_path,
11971 'b', 'd', 'r', session.user, 'c', "_");
11972
11973 pr_response_add(R_250, "%s command successful", (char *) cmd2->argv[0]);
11974 fxp_cmd_dispatch(cmd2);
11975
11976 errno = 0;
11977 }
11978
11979 status_code = fxp_errno2status(errno, &reason);
11980
11981 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
11982 "('%s' [%d])", (unsigned long) status_code, reason,
11983 errno != EOF ? strerror(errno) : "End of file", errno);
11984
11985 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
11986 reason, NULL);
11987
11988 if (res == 0) {
11989 fxp_cmd_dispatch(cmd);
11990
11991 } else {
11992 fxp_cmd_dispatch_err(cmd);
11993 }
11994
11995 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
11996 resp->payload = ptr;
11997 resp->payload_sz = (bufsz - buflen);
11998
11999 return fxp_packet_write(resp);
12000 }
12001
12002 static int fxp_handle_rename(struct fxp_packet *fxp) {
12003 unsigned char *buf, *ptr;
12004 char *args, *old_path, *new_path;
12005 const char *abs_old_path, *reason;
12006 uint32_t buflen, bufsz, flags, status_code;
12007 struct fxp_packet *resp;
12008 cmd_rec *cmd = NULL, *cmd2 = NULL, *cmd3 = NULL;
12009 int xerrno = 0;
12010
12011 old_path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
12012 new_path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
12013
12014 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
12015 old_path = sftp_utf8_decode_str(fxp->pool, old_path);
12016 new_path = sftp_utf8_decode_str(fxp->pool, new_path);
12017 }
12018
12019 /* In protocol version 5 and later, there is a flags int which follows.
12020 * However, this flags argument is usually used to indicate to servers
12021 * that they can use the POSIX rename(2) semantics, i.e. that overwriting
12022 * a file at the new/destination path is OK.
12023 *
12024 * At the moment, since we use rename(2) anyway, even for the older protocol
12025 * versions, we don't read in the flags value. This does mean, however,
12026 * that mod_sftp will not properly return an "file already exists" error
12027 * if the specified new/destination path already exists.
12028 */
12029
12030 args = pstrcat(fxp->pool, old_path, " ", new_path, NULL);
12031
12032 pr_trace_msg(trace_channel, 7, "received request: RENAME %s %s", old_path,
12033 new_path);
12034
12035 pr_scoreboard_entry_update(session.pid,
12036 PR_SCORE_CMD, "%s", "RENAME", NULL, NULL);
12037 pr_scoreboard_entry_update(session.pid,
12038 PR_SCORE_CMD_ARG, "%s", args, NULL, NULL);
12039
12040 pr_proctitle_set("%s - %s: RENAME %s %s", session.user, session.proc_prefix,
12041 old_path, new_path);
12042
12043 if (strlen(old_path) == 0) {
12044 /* Use the default directory if the path is empty. */
12045 old_path = sftp_auth_get_default_dir();
12046
12047 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12048 "empty old path given in RENAME request, using '%s'", old_path);
12049 }
12050
12051 if (strlen(new_path) == 0) {
12052 /* Use the default directory if the path is empty. */
12053 new_path = sftp_auth_get_default_dir();
12054
12055 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12056 "empty new path given in RENAME request, using '%s'", new_path);
12057 }
12058
12059 if (fxp_session->client_version > 4) {
12060 flags = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
12061
12062 if (flags & SSH2_FXR_ATOMIC) {
12063 /* The ATOMIC flag implies OVERWRITE. */
12064 flags |= SSH2_FXR_OVERWRITE;
12065 }
12066
12067 } else {
12068 flags = 0;
12069 }
12070
12071 cmd = fxp_cmd_alloc(fxp->pool, "RENAME", args);
12072 cmd->cmd_class = CL_MISC|CL_WRITE|CL_SFTP;
12073 cmd->cmd_id = SFTP_CMD_ID;
12074
12075 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
12076 buf = ptr = palloc(fxp->pool, bufsz);
12077
12078 cmd2 = fxp_cmd_alloc(fxp->pool, C_RNFR, old_path);
12079 cmd2->cmd_class = CL_MISC|CL_WRITE;
12080 if (pr_cmd_dispatch_phase(cmd2, PRE_CMD, 0) < 0) {
12081 status_code = SSH2_FX_PERMISSION_DENIED;
12082
12083 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12084 "RENAME from '%s' blocked by '%s' handler: %s", old_path,
12085 (char *) cmd2->argv[0], strerror(errno));
12086
12087 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12088 (unsigned long) status_code, fxp_strerror(status_code));
12089
12090 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
12091 fxp_cmd_dispatch_err(cmd2);
12092
12093 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12094 fxp_strerror(status_code), NULL);
12095
12096 fxp_cmd_dispatch_err(cmd);
12097
12098 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12099 resp->payload = ptr;
12100 resp->payload_sz = (bufsz - buflen);
12101
12102 return fxp_packet_write(resp);
12103 }
12104
12105 old_path = dir_best_path(fxp->pool, cmd2->arg);
12106 if (old_path == NULL) {
12107 status_code = SSH2_FX_PERMISSION_DENIED;
12108
12109 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12110 "RENAME request denied: unable to access path '%s'", cmd2->arg);
12111
12112 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12113 (unsigned long) status_code, fxp_strerror(status_code));
12114
12115 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
12116 fxp_cmd_dispatch_err(cmd2);
12117
12118 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12119 fxp_strerror(status_code), NULL);
12120
12121 fxp_cmd_dispatch_err(cmd);
12122
12123 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12124 resp->payload = ptr;
12125 resp->payload_sz = (bufsz - buflen);
12126
12127 return fxp_packet_write(resp);
12128 }
12129
12130 /* Make sure we store the absolute path for LogFormat %w (Issue #1808). */
12131 abs_old_path = dir_abs_path(fxp->pool, old_path, FALSE);
12132 abs_old_path = pr_fsio_realpath(fxp->pool, abs_old_path);
12133
12134 if (pr_table_add(session.notes, "mod_core.rnfr-path",
12135 pstrdup(session.pool, abs_old_path), 0) < 0) {
12136 if (errno != EEXIST) {
12137 pr_trace_msg(trace_channel, 8,
12138 "error setting 'mod_core.rnfr-path' note: %s", strerror(errno));
12139 }
12140 }
12141
12142 cmd3 = fxp_cmd_alloc(fxp->pool, C_RNTO, new_path);
12143 cmd3->cmd_class = CL_MISC|CL_WRITE;
12144 if (pr_cmd_dispatch_phase(cmd3, PRE_CMD, 0) < 0) {
12145 status_code = SSH2_FX_PERMISSION_DENIED;
12146
12147 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12148 "RENAME to '%s' blocked by '%s' handler: %s", new_path,
12149 (char *) cmd3->argv[0], strerror(errno));
12150
12151 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12152 (unsigned long) status_code, fxp_strerror(status_code));
12153
12154 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
12155 fxp_cmd_dispatch_err(cmd2);
12156
12157 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(EACCES));
12158 fxp_cmd_dispatch_err(cmd3);
12159
12160 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12161 fxp_strerror(status_code), NULL);
12162
12163 fxp_cmd_dispatch_err(cmd);
12164
12165 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12166 resp->payload = ptr;
12167 resp->payload_sz = (bufsz - buflen);
12168
12169 return fxp_packet_write(resp);
12170 }
12171
12172 new_path = dir_best_path(fxp->pool, cmd3->arg);
12173 if (new_path == NULL) {
12174 status_code = SSH2_FX_PERMISSION_DENIED;
12175
12176 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12177 "RENAME request denied: unable to access path '%s'", cmd3->arg);
12178
12179 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12180 (unsigned long) status_code, fxp_strerror(status_code));
12181
12182 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(EACCES));
12183 fxp_cmd_dispatch_err(cmd3);
12184
12185 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
12186 fxp_cmd_dispatch_err(cmd2);
12187
12188 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12189 fxp_strerror(status_code), NULL);
12190
12191 fxp_cmd_dispatch_err(cmd);
12192
12193 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12194 resp->payload = ptr;
12195 resp->payload_sz = (bufsz - buflen);
12196
12197 return fxp_packet_write(resp);
12198 }
12199
12200 if (!dir_check(fxp->pool, cmd2, G_DIRS, old_path, NULL) ||
12201 !dir_check(fxp->pool, cmd3, G_WRITE, new_path, NULL)) {
12202 status_code = SSH2_FX_PERMISSION_DENIED;
12203
12204 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12205 "RENAME of '%s' to '%s' blocked by <Limit> configuration",
12206 old_path, new_path);
12207
12208 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12209 (unsigned long) status_code, fxp_strerror(status_code));
12210
12211 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
12212 fxp_cmd_dispatch_err(cmd2);
12213
12214 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
12215 fxp_cmd_dispatch_err(cmd3);
12216
12217 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12218 fxp_strerror(status_code), NULL);
12219
12220 fxp_cmd_dispatch_err(cmd);
12221
12222 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12223 resp->payload = ptr;
12224 resp->payload_sz = (bufsz - buflen);
12225
12226 return fxp_packet_write(resp);
12227 }
12228
12229 if (strcmp(old_path, new_path) == 0) {
12230 xerrno = EEXIST;
12231
12232 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12233 "RENAME of '%s' to same path '%s', rejecting", old_path, new_path);
12234
12235 status_code = fxp_errno2status(xerrno, &reason);
12236
12237 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
12238 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
12239 xerrno);
12240
12241 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(xerrno));
12242 fxp_cmd_dispatch_err(cmd2);
12243
12244 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(xerrno));
12245 fxp_cmd_dispatch_err(cmd3);
12246
12247 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12248 reason, NULL);
12249
12250 fxp_cmd_dispatch_err(cmd);
12251
12252 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12253 resp->payload = ptr;
12254 resp->payload_sz = (bufsz - buflen);
12255
12256 return fxp_packet_write(resp);
12257 }
12258
12259 if (!(flags & SSH2_FXR_OVERWRITE) &&
12260 exists2(fxp->pool, new_path)) {
12261 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12262 "denying RENAME of '%s' to '%s': '%s' already exists and client did not "
12263 "specify OVERWRITE flag", old_path, new_path, new_path);
12264
12265 status_code = SSH2_FX_FILE_ALREADY_EXISTS;
12266
12267 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12268 (unsigned long) status_code, fxp_strerror(status_code));
12269
12270 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(xerrno));
12271 fxp_cmd_dispatch_err(cmd2);
12272
12273 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(xerrno));
12274 fxp_cmd_dispatch_err(cmd3);
12275
12276 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12277 fxp_strerror(status_code), NULL);
12278
12279 fxp_cmd_dispatch_err(cmd);
12280
12281 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12282 resp->payload = ptr;
12283 resp->payload_sz = (bufsz - buflen);
12284
12285 return fxp_packet_write(resp);
12286 }
12287
12288 if (fxp_path_pass_regex_filters(fxp->pool, "RENAME", old_path) < 0 ||
12289 fxp_path_pass_regex_filters(fxp->pool, "RENAME", new_path) < 0) {
12290 xerrno = errno;
12291
12292 status_code = fxp_errno2status(xerrno, NULL);
12293
12294 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12295 (unsigned long) status_code, fxp_strerror(status_code));
12296
12297 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(xerrno));
12298 fxp_cmd_dispatch_err(cmd2);
12299
12300 pr_response_add_err(R_550, "%s: %s", cmd3->arg, strerror(xerrno));
12301 fxp_cmd_dispatch_err(cmd3);
12302
12303 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12304 fxp_strerror(status_code), NULL);
12305
12306 fxp_cmd_dispatch_err(cmd);
12307
12308 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12309 resp->payload = ptr;
12310 resp->payload_sz = (bufsz - buflen);
12311
12312 return fxp_packet_write(resp);
12313 }
12314
12315 if (pr_fsio_rename(old_path, new_path) < 0) {
12316 if (errno != EXDEV) {
12317 xerrno = errno;
12318
12319 (void) pr_trace_msg("fileperms", 1, "RENAME, user '%s' (UID %s, "
12320 "GID %s): error renaming '%s' to '%s': %s", session.user,
12321 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
12322 old_path, new_path, strerror(xerrno));
12323
12324 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12325 "error renaming '%s' to '%s': %s", old_path, new_path,
12326 strerror(xerrno));
12327
12328 errno = xerrno;
12329
12330 } else {
12331 /* In this case, we should manually copy the file from the source
12332 * path to the destination path.
12333 */
12334 errno = 0;
12335 if (pr_fs_copy_file2(old_path, new_path, 0, NULL) < 0) {
12336 xerrno = errno;
12337
12338 (void) pr_trace_msg("fileperms", 1, "RENAME, user '%s' (UID %s, "
12339 "GID %s): error copying '%s' to '%s': %s", session.user,
12340 pr_uid2str(fxp->pool, session.uid),
12341 pr_gid2str(fxp->pool, session.gid), old_path, new_path,
12342 strerror(xerrno));
12343
12344 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12345 "error copying '%s' to '%s': %s", old_path, new_path,
12346 strerror(xerrno));
12347
12348 errno = xerrno;
12349
12350 } else {
12351 /* Once copied, remove the original path. */
12352 if (pr_fsio_unlink(old_path) < 0) {
12353 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12354 "error deleting '%s': %s", old_path, strerror(errno));
12355 }
12356
12357 xerrno = errno = 0;
12358 }
12359 }
12360
12361 } else {
12362 /* No errors. */
12363 xerrno = errno = 0;
12364 }
12365
12366 status_code = fxp_errno2status(xerrno, &reason);
12367
12368 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
12369 "('%s' [%d])", (unsigned long) status_code, reason,
12370 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
12371
12372 /* Clear out any transfer-specific data. */
12373 if (session.xfer.p) {
12374 destroy_pool(session.xfer.p);
12375 }
12376 memset(&session.xfer, 0, sizeof(session.xfer));
12377
12378 /* The timing of these steps may look peculiar, but it's deliberate,
12379 * in order to get the expected log messages in an ExtendedLog.
12380 */
12381
12382 session.xfer.p = make_sub_pool(fxp_pool);
12383 pr_pool_tag(session.xfer.p, "SFTP session transfer pool");
12384 memset(&session.xfer.start_time, 0, sizeof(session.xfer.start_time));
12385 gettimeofday(&session.xfer.start_time, NULL);
12386
12387 session.xfer.path = pstrdup(session.xfer.p, old_path);
12388
12389 if (xerrno == 0) {
12390 pr_response_add(R_350,
12391 "File or directory exists, ready for destination name");
12392 fxp_cmd_dispatch(cmd2);
12393
12394 } else {
12395 pr_response_add_err(R_550, "%s: %s", (char *) cmd2->argv[0],
12396 strerror(xerrno));
12397 fxp_cmd_dispatch_err(cmd2);
12398 }
12399
12400 session.xfer.path = pstrdup(session.xfer.p, new_path);
12401
12402 if (xerrno == 0) {
12403 pr_response_add(R_250, "Rename successful");
12404 fxp_cmd_dispatch(cmd3);
12405
12406 } else {
12407 pr_response_add_err(R_550, "%s: %s", (char *) cmd3->argv[0],
12408 strerror(xerrno));
12409 fxp_cmd_dispatch_err(cmd3);
12410 }
12411
12412 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12413 reason, NULL);
12414 if (xerrno == 0) {
12415 fxp_cmd_dispatch(cmd);
12416
12417 } else {
12418 fxp_cmd_dispatch_err(cmd);
12419 }
12420
12421 /* Clear out any transfer-specific data. */
12422 if (session.xfer.p) {
12423 destroy_pool(session.xfer.p);
12424 }
12425 memset(&session.xfer, 0, sizeof(session.xfer));
12426
12427 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12428 resp->payload = ptr;
12429 resp->payload_sz = (bufsz - buflen);
12430
12431 return fxp_packet_write(resp);
12432 }
12433
12434 static int fxp_handle_rmdir(struct fxp_packet *fxp) {
12435 unsigned char *buf, *ptr;
12436 char *cmd_name, *path;
12437 const char *reason;
12438 uint32_t buflen, bufsz, status_code;
12439 struct fxp_packet *resp;
12440 cmd_rec *cmd, *cmd2;
12441 int have_error = FALSE, res = 0;
12442 struct stat st;
12443
12444 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
12445 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
12446 path = sftp_utf8_decode_str(fxp->pool, path);
12447 }
12448
12449 pr_scoreboard_entry_update(session.pid,
12450 PR_SCORE_CMD, "%s", "RMDIR", NULL, NULL);
12451 pr_scoreboard_entry_update(session.pid,
12452 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
12453
12454 pr_proctitle_set("%s - %s: RMDIR %s", session.user, session.proc_prefix,
12455 path);
12456
12457 pr_trace_msg(trace_channel, 7, "received request: RMDIR %s", path);
12458
12459 if (strlen(path) == 0) {
12460 /* Use the default directory if the path is empty. */
12461 path = sftp_auth_get_default_dir();
12462
12463 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12464 "empty path given in RMDIR request, using '%s'", path);
12465 }
12466
12467 cmd = fxp_cmd_alloc(fxp->pool, "RMDIR", path);
12468 cmd->cmd_class = CL_DIRS|CL_WRITE|CL_SFTP;
12469 cmd->cmd_id = SFTP_CMD_ID;
12470
12471 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
12472 buf = ptr = palloc(fxp->pool, bufsz);
12473
12474 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
12475 status_code = SSH2_FX_PERMISSION_DENIED;
12476
12477 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12478 "RMDIR of '%s' blocked by '%s' handler: %s", path,
12479 (char *) cmd->argv[0], strerror(errno));
12480
12481 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12482 (unsigned long) status_code, fxp_strerror(status_code));
12483
12484 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12485 fxp_strerror(status_code), NULL);
12486
12487 fxp_cmd_dispatch_err(cmd);
12488
12489 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12490 resp->payload = ptr;
12491 resp->payload_sz = (bufsz - buflen);
12492
12493 return fxp_packet_write(resp);
12494 }
12495
12496 /* The path may have been changed by any PRE_CMD handlers. */
12497 path = cmd->arg;
12498
12499 pr_fs_clear_cache2(path);
12500 if (pr_fsio_lstat(path, &st) == 0) {
12501 if (S_ISLNK(st.st_mode)) {
12502 char link_path[PR_TUNABLE_PATH_MAX];
12503 int len;
12504
12505 memset(link_path, '\0', sizeof(link_path));
12506 len = dir_readlink(fxp->pool, path, link_path, sizeof(link_path)-1,
12507 PR_DIR_READLINK_FL_HANDLE_REL_PATH);
12508 if (len > 0) {
12509 link_path[len] = '\0';
12510 path = pstrdup(fxp->pool, link_path);
12511 }
12512 }
12513 }
12514
12515 path = dir_best_path(fxp->pool, path);
12516 if (path == NULL) {
12517 status_code = SSH2_FX_PERMISSION_DENIED;
12518
12519 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12520 "RMDIR request denied: unable to access path '%s'", cmd->arg);
12521
12522 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12523 (unsigned long) status_code, fxp_strerror(status_code));
12524
12525 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12526 fxp_strerror(status_code), NULL);
12527
12528 fxp_cmd_dispatch_err(cmd);
12529
12530 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12531 resp->payload = ptr;
12532 resp->payload_sz = (bufsz - buflen);
12533
12534 return fxp_packet_write(resp);
12535 }
12536
12537 cmd2 = fxp_cmd_alloc(fxp->pool, C_RMD, path);
12538 if (pr_cmd_dispatch_phase(cmd2, PRE_CMD, 0) == -1) {
12539 status_code = SSH2_FX_PERMISSION_DENIED;
12540
12541 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12542 "RMDIR of '%s' blocked by '%s' handler: %s", path,
12543 (char *) cmd2->argv[0], strerror(errno));
12544
12545 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12546 (unsigned long) status_code, fxp_strerror(status_code));
12547
12548 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
12549 fxp_cmd_dispatch_err(cmd2);
12550
12551 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12552 fxp_strerror(status_code), NULL);
12553
12554 fxp_cmd_dispatch_err(cmd);
12555
12556 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12557 resp->payload = ptr;
12558 resp->payload_sz = (bufsz - buflen);
12559
12560 return fxp_packet_write(resp);
12561 }
12562
12563 /* The path may have been changed by any PRE_CMD handlers. */
12564 path = cmd2->arg;
12565
12566 cmd_name = cmd->argv[0];
12567 pr_cmd_set_name(cmd, C_RMD);
12568
12569 if (!dir_check(fxp->pool, cmd, G_WRITE, path, NULL)) {
12570 have_error = TRUE;
12571 }
12572
12573 pr_cmd_set_name(cmd, C_XRMD);
12574
12575 if (!have_error &&
12576 !dir_check(fxp->pool, cmd, G_WRITE, path, NULL)) {
12577 have_error = TRUE;
12578 }
12579
12580 pr_cmd_set_name(cmd, cmd_name);
12581
12582 if (have_error) {
12583 status_code = SSH2_FX_PERMISSION_DENIED;
12584
12585 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12586 "RMDIR of '%s' blocked by <Limit> configuration", path);
12587
12588 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12589 (unsigned long) status_code, fxp_strerror(status_code));
12590
12591 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
12592 fxp_cmd_dispatch_err(cmd2);
12593
12594 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12595 fxp_strerror(status_code), NULL);
12596
12597 fxp_cmd_dispatch_err(cmd);
12598
12599 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12600 resp->payload = ptr;
12601 resp->payload_sz = (bufsz - buflen);
12602
12603 return fxp_packet_write(resp);
12604 }
12605
12606 if (fxp_path_pass_regex_filters(fxp->pool, "RMDIR", path) < 0) {
12607 int xerrno = errno;
12608
12609 status_code = fxp_errno2status(xerrno, NULL);
12610
12611 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12612 (unsigned long) status_code, fxp_strerror(status_code));
12613
12614 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(xerrno));
12615 fxp_cmd_dispatch_err(cmd2);
12616
12617 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12618 fxp_strerror(status_code), NULL);
12619
12620 fxp_cmd_dispatch_err(cmd);
12621
12622 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12623 resp->payload = ptr;
12624 resp->payload_sz = (bufsz - buflen);
12625
12626 return fxp_packet_write(resp);
12627 }
12628
12629 res = pr_fsio_rmdir(path);
12630 if (res < 0) {
12631 int xerrno = errno;
12632
12633 (void) pr_trace_msg("fileperms", 1, "RMDIR, user '%s' (UID %s, GID %s): "
12634 "error removing directory '%s': %s", session.user,
12635 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
12636 path, strerror(xerrno));
12637
12638 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12639 "error removing directory '%s': %s", path, strerror(xerrno));
12640
12641 #if defined(ENOTEMPTY) && ENOTEMPTY != EEXIST
12642 status_code = fxp_errno2status(xerrno, &reason);
12643
12644 #else
12645 /* On AIX5, ENOTEMPTY and EEXIST are defined to the same value. See:
12646 *
12647 * http://forums.proftpd.org/smf/index.php/topic,3971.0.html
12648 *
12649 * We still want to send the proper SFTP error code/string if we see
12650 * these values, though. The fix for handling this case in
12651 * fxp_errno2status() means that we need to do the errno lookup a little
12652 * more manually here.
12653 */
12654
12655 if (xerrno != ENOTEMPTY) {
12656 status_code = fxp_errno2status(xerrno, &reason);
12657
12658 } else {
12659 /* Generic failure code, works for all protocol versions. */
12660 status_code = SSH2_FX_FAILURE;
12661
12662 if (fxp_session->client_version > 3) {
12663 status_code = SSH2_FX_FILE_ALREADY_EXISTS;
12664 }
12665
12666 if (fxp_session->client_version > 5) {
12667 status_code = SSH2_FX_DIR_NOT_EMPTY;
12668 }
12669
12670 reason = fxp_strerror(status_code);
12671 }
12672 #endif
12673
12674 errno = xerrno;
12675
12676 } else {
12677 /* No error. */
12678 errno = 0;
12679 status_code = SSH2_FX_OK;
12680 reason = fxp_strerror(status_code);
12681 }
12682
12683 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
12684 "('%s' [%d])", (unsigned long) status_code, reason,
12685 errno != EOF ? strerror(errno) : "End of file", errno);
12686
12687 if (res == 0) {
12688 fxp_cmd_dispatch(cmd2);
12689
12690 } else {
12691 fxp_cmd_dispatch_err(cmd2);
12692 }
12693
12694 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12695 reason, NULL);
12696
12697 if (res == 0) {
12698 fxp_cmd_dispatch(cmd);
12699
12700 } else {
12701 fxp_cmd_dispatch_err(cmd);
12702 }
12703
12704 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12705 resp->payload = ptr;
12706 resp->payload_sz = (bufsz - buflen);
12707
12708 return fxp_packet_write(resp);
12709 }
12710
12711 static int fxp_handle_setstat(struct fxp_packet *fxp) {
12712 unsigned char *buf, *ptr;
12713 char *attrs_str, *cmd_name, *path;
12714 const char *reason;
12715 uint32_t attr_flags, buflen, bufsz, status_code;
12716 int res;
12717 struct stat *attrs;
12718 struct fxp_packet *resp;
12719 cmd_rec *cmd;
12720 struct stat st;
12721 array_header *xattrs = NULL;
12722
12723 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
12724 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
12725 path = sftp_utf8_decode_str(fxp->pool, path);
12726 }
12727
12728 pr_scoreboard_entry_update(session.pid,
12729 PR_SCORE_CMD, "%s", "SETSTAT", NULL, NULL);
12730 pr_scoreboard_entry_update(session.pid,
12731 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
12732
12733 attrs = fxp_attrs_read(fxp, &fxp->payload, &fxp->payload_sz, &attr_flags,
12734 &xattrs);
12735 if (attrs == NULL) {
12736 return 0;
12737 }
12738
12739 attrs_str = fxp_strattrs(fxp->pool, attrs, &attr_flags);
12740
12741 pr_proctitle_set("%s - %s: SETSTAT %s %s", session.user, session.proc_prefix,
12742 path, attrs_str);
12743
12744 pr_trace_msg(trace_channel, 7, "received request: SETSTAT %s %s", path,
12745 attrs_str);
12746
12747 if (strlen(path) == 0) {
12748 /* Use the default directory if the path is empty. */
12749 path = sftp_auth_get_default_dir();
12750
12751 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12752 "empty path given in SETSTAT request, using '%s'", path);
12753 }
12754
12755 cmd = fxp_cmd_alloc(fxp->pool, "SETSTAT", path);
12756 cmd->cmd_class = CL_WRITE|CL_SFTP;
12757 cmd->cmd_id = SFTP_CMD_ID;
12758
12759 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
12760 buf = ptr = palloc(fxp->pool, bufsz);
12761
12762 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
12763 status_code = SSH2_FX_PERMISSION_DENIED;
12764
12765 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12766 "SETSTAT of '%s' blocked by '%s' handler: %s", path,
12767 (char *) cmd->argv[0], strerror(errno));
12768
12769 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12770 (unsigned long) status_code, fxp_strerror(status_code));
12771
12772 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12773 fxp_strerror(status_code), NULL);
12774
12775 fxp_cmd_dispatch_err(cmd);
12776
12777 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12778 resp->payload = ptr;
12779 resp->payload_sz = (bufsz - buflen);
12780
12781 return fxp_packet_write(resp);
12782 }
12783
12784 /* The path may have been changed by any PRE_CMD handlers. */
12785 path = cmd->arg;
12786
12787 pr_fs_clear_cache2(path);
12788 if (pr_fsio_lstat(path, &st) == 0) {
12789 if (S_ISLNK(st.st_mode)) {
12790 char link_path[PR_TUNABLE_PATH_MAX];
12791 int len;
12792
12793 memset(link_path, '\0', sizeof(link_path));
12794 len = dir_readlink(fxp->pool, path, link_path, sizeof(link_path)-1,
12795 PR_DIR_READLINK_FL_HANDLE_REL_PATH);
12796 if (len > 0) {
12797 link_path[len] = '\0';
12798 path = pstrdup(fxp->pool, link_path);
12799 }
12800 }
12801 }
12802
12803 path = dir_best_path(fxp->pool, path);
12804 if (path == NULL) {
12805 status_code = SSH2_FX_PERMISSION_DENIED;
12806
12807 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12808 "SETSTAT request denied: unable to access path '%s'", cmd->arg);
12809
12810 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12811 (unsigned long) status_code, fxp_strerror(status_code));
12812
12813 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12814 fxp_strerror(status_code), NULL);
12815
12816 fxp_cmd_dispatch_err(cmd);
12817
12818 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12819 resp->payload = ptr;
12820 resp->payload_sz = (bufsz - buflen);
12821
12822 return fxp_packet_write(resp);
12823 }
12824
12825 cmd_name = cmd->argv[0];
12826 pr_cmd_set_name(cmd, "SETSTAT");
12827
12828 if (!dir_check(fxp->pool, cmd, G_WRITE, path, NULL)) {
12829 status_code = SSH2_FX_PERMISSION_DENIED;
12830
12831 pr_cmd_set_name(cmd, cmd_name);
12832
12833 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12834 "SETSTAT of '%s' blocked by <Limit> configuration", path);
12835
12836 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12837 (unsigned long) status_code, fxp_strerror(status_code));
12838
12839 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12840 fxp_strerror(status_code), NULL);
12841
12842 fxp_cmd_dispatch_err(cmd);
12843
12844 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12845 resp->payload = ptr;
12846 resp->payload_sz = (bufsz - buflen);
12847
12848 return fxp_packet_write(resp);
12849 }
12850 pr_cmd_set_name(cmd, cmd_name);
12851
12852 attr_flags = fxp_attrs_clear_unsupported(attr_flags);
12853
12854 /* If the SFTPOption for ignoring the owners for SFTP setstat requests is set,
12855 * handle it by clearing the SSH2_FX_ATTR_UIDGID and SSH2_FX_ATTR_OWNERGROUP
12856 * flags.
12857 */
12858 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_SET_OWNERS) &&
12859 ((attr_flags & SSH2_FX_ATTR_UIDGID) ||
12860 (attr_flags & SSH2_FX_ATTR_OWNERGROUP))) {
12861 pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSFTPSetOwners' "
12862 "configured, ignoring ownership sent by client");
12863 attr_flags &= ~SSH2_FX_ATTR_UIDGID;
12864 attr_flags &= ~SSH2_FX_ATTR_OWNERGROUP;
12865 }
12866
12867 /* If the SFTPOption for ignoring the xattrs for SFTP setstat requests is set,
12868 * handle it by clearing the SSH2_FX_ATTR_EXTENDED flag.
12869 */
12870 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_SET_XATTRS) &&
12871 (attr_flags & SSH2_FX_ATTR_EXTENDED)) {
12872 pr_trace_msg(trace_channel, 7,
12873 "SFTPOption 'IgnoreSFTPSetExtendedAttributes' configured, ignoring "
12874 "xattrs sent by client");
12875 attr_flags &= ~SSH2_FX_ATTR_EXTENDED;
12876 }
12877
12878 /* If the SFTPOption for ignoring the perms for SFTP setstat requests is set,
12879 * handle it by clearing the SSH2_FX_ATTR_PERMISSIONS flag.
12880 */
12881 if ((sftp_opts & SFTP_OPT_IGNORE_SFTP_SET_PERMS) &&
12882 (attr_flags & SSH2_FX_ATTR_PERMISSIONS)) {
12883 pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSFTPSetPerms' "
12884 "configured, ignoring perms sent by client");
12885 attr_flags &= ~SSH2_FX_ATTR_PERMISSIONS;
12886 }
12887
12888 /* If the SFTPOption for ignoring the times for SFTP setstat requests is set,
12889 * handle it by clearing the time-related SSH2_FX_ATTR flags.
12890 */
12891 if (sftp_opts & SFTP_OPT_IGNORE_SFTP_SET_TIMES) {
12892 if ((attr_flags & SSH2_FX_ATTR_ACCESSTIME) ||
12893 (attr_flags & SSH2_FX_ATTR_MODIFYTIME)) {
12894 pr_trace_msg(trace_channel, 7, "SFTPOption 'IgnoreSFTPSetTimes' "
12895 "configured, ignoring times sent by client");
12896 attr_flags &= ~SSH2_FX_ATTR_ACCESSTIME;
12897 attr_flags &= ~SSH2_FX_ATTR_MODIFYTIME;
12898 }
12899 }
12900
12901 res = fxp_attrs_set(NULL, path, attrs, attr_flags, xattrs, &buf, &buflen,
12902 fxp);
12903 if (res < 0) {
12904 fxp_cmd_dispatch_err(cmd);
12905
12906 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12907 resp->payload = ptr;
12908 resp->payload_sz = (bufsz - buflen);
12909
12910 return fxp_packet_write(resp);
12911 }
12912
12913 status_code = fxp_errno2status(0, &reason);
12914
12915 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12916 (unsigned long) status_code, reason);
12917
12918 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12919 reason, NULL);
12920
12921 fxp_cmd_dispatch(cmd);
12922
12923 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
12924 resp->payload = ptr;
12925 resp->payload_sz = (bufsz - buflen);
12926
12927 return fxp_packet_write(resp);
12928 }
12929
12930 static int fxp_handle_stat(struct fxp_packet *fxp) {
12931 unsigned char *buf;
12932 char *cmd_name, *path;
12933 uint32_t attr_flags, buflen;
12934 struct stat st;
12935 struct fxp_buffer *fxb;
12936 struct fxp_packet *resp;
12937 cmd_rec *cmd;
12938 const char *fake_user = NULL, *fake_group = NULL;
12939
12940 path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
12941 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
12942 path = sftp_utf8_decode_str(fxp->pool, path);
12943 }
12944
12945 pr_scoreboard_entry_update(session.pid,
12946 PR_SCORE_CMD, "%s", "STAT", NULL, NULL);
12947 pr_scoreboard_entry_update(session.pid,
12948 PR_SCORE_CMD_ARG, "%s", path, NULL, NULL);
12949
12950 pr_proctitle_set("%s - %s: STAT %s", session.user, session.proc_prefix, path);
12951
12952 if (fxp_session->client_version > 3) {
12953 attr_flags = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
12954
12955 pr_trace_msg(trace_channel, 7, "received request: STAT %s %s", path,
12956 fxp_strattrflags(fxp->pool, attr_flags));
12957
12958 } else {
12959 pr_trace_msg(trace_channel, 7, "received request: STAT %s", path);
12960 attr_flags = SSH2_FX_ATTR_SIZE|SSH2_FX_ATTR_UIDGID|SSH2_FX_ATTR_PERMISSIONS|
12961 SSH2_FX_ATTR_ACMODTIME;
12962 #if defined(PR_USE_XATTR)
12963 if (!(fxp_fsio_opts & PR_FSIO_OPT_IGNORE_XATTR)) {
12964 attr_flags |= SSH2_FX_ATTR_EXTENDED;
12965 }
12966 #endif /* PR_USE_XATTR */
12967 }
12968
12969 if (strlen(path) == 0) {
12970 /* Use the default directory if the path is empty. */
12971 path = sftp_auth_get_default_dir();
12972
12973 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12974 "empty path given in STAT request, using '%s'", path);
12975 }
12976
12977 cmd = fxp_cmd_alloc(fxp->pool, "STAT", path);
12978 cmd->cmd_class = CL_READ|CL_SFTP;
12979 cmd->cmd_id = SFTP_CMD_ID;
12980
12981 fxb = pcalloc(fxp->pool, sizeof(struct fxp_buffer));
12982 fxb->bufsz = buflen = FXP_RESPONSE_NAME_DEFAULT_SZ;
12983 fxb->ptr = buf = palloc(fxp->pool, fxb->bufsz);
12984
12985 if (pr_cmd_dispatch_phase(cmd, PRE_CMD, 0) < 0) {
12986 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
12987
12988 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
12989 "STAT of '%s' blocked by '%s' handler: %s", path,
12990 (char *) cmd->argv[0], strerror(errno));
12991
12992 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
12993 (unsigned long) status_code, fxp_strerror(status_code));
12994
12995 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
12996 fxp_strerror(status_code), NULL);
12997
12998 fxp_cmd_dispatch_err(cmd);
12999
13000 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13001 resp->payload = fxb->ptr;
13002 resp->payload_sz = (fxb->bufsz - buflen);
13003
13004 return fxp_packet_write(resp);
13005 }
13006
13007 /* The path may have been changed by any PRE_CMD handlers. */
13008 path = cmd->arg;
13009
13010 pr_fs_clear_cache2(path);
13011 if (pr_fsio_lstat(path, &st) == 0) {
13012 if (S_ISLNK(st.st_mode)) {
13013 char link_path[PR_TUNABLE_PATH_MAX];
13014 int len;
13015
13016 memset(link_path, '\0', sizeof(link_path));
13017 len = dir_readlink(fxp->pool, path, link_path, sizeof(link_path)-1,
13018 PR_DIR_READLINK_FL_HANDLE_REL_PATH);
13019 if (len > 0) {
13020 link_path[len] = '\0';
13021 path = pstrdup(fxp->pool, link_path);
13022 }
13023 }
13024 }
13025
13026 path = dir_best_path(fxp->pool, path);
13027 if (path == NULL) {
13028 int xerrno = EACCES;
13029 const char *reason;
13030 uint32_t status_code;
13031
13032 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13033 "STAT request denied: unable to access path '%s'", cmd->arg);
13034
13035 status_code = fxp_errno2status(xerrno, &reason);
13036
13037 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
13038 "('%s' [%d])", (unsigned long) status_code, reason, strerror(xerrno),
13039 xerrno);
13040
13041 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13042 reason, NULL);
13043
13044 fxp_cmd_dispatch_err(cmd);
13045
13046 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13047 resp->payload = fxb->ptr;
13048 resp->payload_sz = (fxb->bufsz - buflen);
13049
13050 return fxp_packet_write(resp);
13051 }
13052
13053 cmd_name = cmd->argv[0];
13054 pr_cmd_set_name(cmd, "STAT");
13055
13056 if (!dir_check(fxp->pool, cmd, G_READ, path, NULL)) {
13057 uint32_t status_code = SSH2_FX_PERMISSION_DENIED;
13058
13059 pr_cmd_set_name(cmd, cmd_name);
13060
13061 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13062 "STAT of '%s' blocked by <Limit> configuration", path);
13063
13064 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13065 (unsigned long) status_code, fxp_strerror(status_code));
13066
13067 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13068 fxp_strerror(status_code), NULL);
13069
13070 fxp_cmd_dispatch_err(cmd);
13071
13072 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13073 resp->payload = fxb->ptr;
13074 resp->payload_sz = (fxb->bufsz - buflen);
13075
13076 return fxp_packet_write(resp);
13077 }
13078 pr_cmd_set_name(cmd, cmd_name);
13079
13080 pr_fs_clear_cache2(path);
13081 if (pr_fsio_stat(path, &st) < 0) {
13082 uint32_t status_code;
13083 const char *reason;
13084 int xerrno = errno;
13085
13086 if (xerrno != ENOENT) {
13087 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13088 "error checking '%s' for STAT: %s", path, strerror(xerrno));
13089 }
13090
13091 status_code = fxp_errno2status(xerrno, &reason);
13092
13093 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
13094 "('%s' [%d])", (unsigned long) status_code, reason,
13095 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
13096
13097 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13098 reason, NULL);
13099
13100 fxp_cmd_dispatch_err(cmd);
13101
13102 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13103 resp->payload = fxb->ptr;
13104 resp->payload_sz = (fxb->bufsz - buflen);
13105
13106 return fxp_packet_write(resp);
13107 }
13108
13109 pr_trace_msg(trace_channel, 8, "sending response: ATTRS %s",
13110 fxp_strattrs(fxp->pool, &st, &attr_flags));
13111
13112 sftp_msg_write_byte(&buf, &buflen, SFTP_SSH2_FXP_ATTRS);
13113 sftp_msg_write_int(&buf, &buflen, fxp->request_id);
13114
13115 fake_user = get_param_ptr(get_dir_ctxt(fxp->pool, path), "DirFakeUser",
13116 FALSE);
13117 if (fake_user != NULL &&
13118 strncmp(fake_user, "~", 2) == 0) {
13119 fake_user = session.user;
13120 }
13121
13122 fake_group = get_param_ptr(get_dir_ctxt(fxp->pool, path), "DirFakeGroup",
13123 FALSE);
13124 if (fake_group != NULL &&
13125 strncmp(fake_group, "~", 2) == 0) {
13126 fake_group = session.group;
13127 }
13128
13129 fxb->buf = buf;
13130 fxb->buflen = buflen;
13131
13132 attr_flags = fxp_attrs_clear_unsupported(attr_flags);
13133 if (fxp_session->client_version > 3 &&
13134 sftp_opts & SFTP_OPT_INCLUDE_SFTP_TIMES) {
13135 pr_trace_msg(trace_channel, 17,
13136 "SFTPOption IncludeSFTPTimes in effect; assuring presence of "
13137 "ACCESSTIME/MODIFYTIME attributes");
13138 attr_flags |= SSH2_FX_ATTR_ACCESSTIME;
13139 attr_flags |= SSH2_FX_ATTR_MODIFYTIME;
13140 }
13141
13142 fxp_attrs_write(fxp->pool, fxb, path, &st, attr_flags, fake_user, fake_group);
13143
13144 buf = fxb->buf;
13145 buflen = fxb->buflen;
13146
13147 fxp_cmd_dispatch(cmd);
13148
13149 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13150 resp->payload = fxb->ptr;
13151 resp->payload_sz = (fxb->bufsz - buflen);
13152
13153 return fxp_packet_write(resp);
13154 }
13155
13156 static int fxp_handle_symlink(struct fxp_packet *fxp) {
13157 unsigned char *buf, *ptr;
13158 char *args, *args2, *cmd_name, *link_path, *link_vpath, *target_path,
13159 *target_vpath, *vpath;
13160 const char *reason;
13161 int have_error = FALSE, res;
13162 uint32_t buflen, bufsz, status_code;
13163 struct fxp_packet *resp;
13164 cmd_rec *cmd, *cmd2;
13165
13166 /* Note: The ietf-secsh-filexfer drafts define the arguments for SYMLINK
13167 * as "linkpath" (the file being created), followed by "targetpath" (the
13168 * target of the link). The following code reads the arguments in the
13169 * opposite (thus wrong) order. This is done deliberately, to match
13170 * the behavior that OpenSSH uses; see:
13171 *
13172 * https://bugzilla.mindrot.org/show_bug.cgi?id=861
13173 */
13174
13175 target_path = sftp_msg_read_string(fxp->pool, &fxp->payload,
13176 &fxp->payload_sz);
13177 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
13178 target_path = sftp_utf8_decode_str(fxp->pool, target_path);
13179 }
13180
13181 link_path = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
13182 if (fxp_session->client_version >= fxp_utf8_protocol_version) {
13183 link_path = sftp_utf8_decode_str(fxp->pool, link_path);
13184 }
13185
13186 args = pstrcat(fxp->pool, target_path, " ", link_path, NULL);
13187
13188 cmd = fxp_cmd_alloc(fxp->pool, "SYMLINK", args);
13189 cmd->cmd_class = CL_WRITE|CL_SFTP;
13190 cmd->cmd_id = SFTP_CMD_ID;
13191
13192 pr_scoreboard_entry_update(session.pid,
13193 PR_SCORE_CMD, "%s", "SYMLINK", NULL, NULL);
13194 pr_scoreboard_entry_update(session.pid,
13195 PR_SCORE_CMD_ARG, "%s", args, NULL, NULL);
13196
13197 pr_proctitle_set("%s - %s: SYMLINK %s %s", session.user, session.proc_prefix,
13198 link_path, target_path);
13199
13200 pr_trace_msg(trace_channel, 7, "received request: SYMLINK %s %s", target_path,
13201 link_path);
13202
13203 if (strlen(target_path) == 0) {
13204 /* Use the default directory if the path is empty. */
13205 target_path = sftp_auth_get_default_dir();
13206
13207 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13208 "empty target path given in SYMLINK request, using '%s'", target_path);
13209 }
13210
13211 if (strlen(link_path) == 0) {
13212 /* Use the default directory if the path is empty. */
13213 link_path = sftp_auth_get_default_dir();
13214
13215 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13216 "empty link path given in SYMLINK request, using '%s'", link_path);
13217 }
13218
13219 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
13220 buf = ptr = palloc(fxp->pool, bufsz);
13221
13222 /* Make sure we use the full paths. */
13223 vpath = dir_canonical_vpath(fxp->pool, target_path);
13224 if (vpath == NULL) {
13225 int xerrno = errno;
13226
13227 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13228 "error resolving '%s': %s", target_path, strerror(xerrno));
13229
13230 status_code = fxp_errno2status(xerrno, &reason);
13231
13232 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
13233 "('%s' [%d])", (unsigned long) status_code, reason,
13234 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
13235
13236 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13237 reason, NULL);
13238
13239 fxp_cmd_dispatch_err(cmd);
13240
13241 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13242 resp->payload = ptr;
13243 resp->payload_sz = (bufsz - buflen);
13244
13245 return fxp_packet_write(resp);
13246 }
13247 target_vpath = vpath;
13248
13249 vpath = dir_canonical_vpath(fxp->pool, link_path);
13250 if (vpath == NULL) {
13251 int xerrno = errno;
13252
13253 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13254 "error resolving '%s': %s", link_path, strerror(xerrno));
13255
13256 status_code = fxp_errno2status(xerrno, &reason);
13257
13258 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
13259 "('%s' [%d])", (unsigned long) status_code, reason,
13260 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
13261
13262 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13263 reason, NULL);
13264
13265 fxp_cmd_dispatch_err(cmd);
13266
13267 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13268 resp->payload = ptr;
13269 resp->payload_sz = (bufsz - buflen);
13270
13271 return fxp_packet_write(resp);
13272 }
13273 link_vpath = vpath;
13274
13275 /* We use a slightly different cmd_rec here, for the benefit of PRE_CMD
13276 * handlers such as mod_rewrite. It is impossible for a client to
13277 * send a tab ('\t') in SFTP, so we use that as our delimiter in the
13278 * single-string args argument in the cmd_rec.
13279 *
13280 * If the PRE_CMD dispatch is successful, we can then check to see
13281 * if the args string changed, and if so, parse back out the individual
13282 * paths.
13283 */
13284
13285 args2 = pstrcat(fxp->pool, target_vpath, "\t", link_vpath, NULL);
13286 cmd2 = fxp_cmd_alloc(fxp->pool, "SYMLINK", args2);
13287 cmd2->cmd_class = CL_WRITE;
13288 cmd2->cmd_id = SFTP_CMD_ID;
13289
13290 if (pr_cmd_dispatch_phase(cmd2, PRE_CMD, 0) < 0) {
13291 status_code = SSH2_FX_PERMISSION_DENIED;
13292
13293 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13294 "SYMLINK of '%s' to '%s' blocked by '%s' handler: %sl", target_path,
13295 link_path, (char *) cmd2->argv[0], strerror(errno));
13296
13297 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13298 (unsigned long) status_code, fxp_strerror(status_code));
13299
13300 pr_response_add_err(R_550, "%s: %s", cmd2->arg, strerror(EACCES));
13301 fxp_cmd_dispatch_err(cmd2);
13302
13303 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13304 fxp_strerror(status_code), NULL);
13305
13306 fxp_cmd_dispatch_err(cmd);
13307
13308 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13309 resp->payload = ptr;
13310 resp->payload_sz = (bufsz - buflen);
13311
13312 return fxp_packet_write(resp);
13313 }
13314
13315 /* The paths may have been changed by any PRE_CMD handlers. */
13316 if (strcmp(args2, cmd2->arg) != 0) {
13317 char *ptr2;
13318
13319 ptr2 = strchr(cmd2->arg, '\t');
13320 if (ptr2) {
13321 *ptr2 = '\0';
13322 target_path = cmd2->arg;
13323 link_path = ptr2 + 1;
13324 }
13325 }
13326
13327 cmd_name = cmd->argv[0];
13328 pr_cmd_set_name(cmd, "SYMLINK");
13329
13330 if (!dir_check(fxp->pool, cmd, G_READ, target_vpath, NULL)) {
13331 pr_cmd_set_name(cmd, cmd_name);
13332 have_error = TRUE;
13333 }
13334
13335 if (!have_error &&
13336 !dir_check(fxp->pool, cmd, G_WRITE, link_vpath, NULL)) {
13337 pr_cmd_set_name(cmd, cmd_name);
13338 have_error = TRUE;
13339 }
13340
13341 pr_cmd_set_name(cmd, cmd_name);
13342
13343 if (have_error) {
13344 status_code = SSH2_FX_PERMISSION_DENIED;
13345
13346 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13347 "SYMLINK of '%s' to '%s' blocked by <Limit> configuration",
13348 target_path, link_path);
13349
13350 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13351 (unsigned long) status_code, fxp_strerror(status_code));
13352
13353 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13354 fxp_strerror(status_code), NULL);
13355
13356 fxp_cmd_dispatch_err(cmd);
13357
13358 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13359 resp->payload = ptr;
13360 resp->payload_sz = (bufsz - buflen);
13361
13362 return fxp_packet_write(resp);
13363 }
13364
13365 res = pr_fsio_symlink(target_path, link_path);
13366
13367 if (res < 0) {
13368 int xerrno = errno;
13369
13370 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13371 "error symlinking '%s' to '%s': %s", target_path, link_path,
13372 strerror(xerrno));
13373
13374 status_code = fxp_errno2status(xerrno, &reason);
13375
13376 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
13377 "('%s' [%d])", (unsigned long) status_code, reason,
13378 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
13379
13380 fxp_cmd_dispatch_err(cmd);
13381
13382 } else {
13383 errno = 0;
13384 status_code = fxp_errno2status(0, &reason);
13385
13386 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13387 (unsigned long) status_code, reason);
13388
13389 fxp_cmd_dispatch(cmd);
13390 }
13391
13392 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13393 reason, NULL);
13394
13395 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13396 resp->payload = ptr;
13397 resp->payload_sz = (bufsz - buflen);
13398
13399 return fxp_packet_write(resp);
13400 }
13401
13402 /* Similar to mod_xfer's find_max_nbytes() function. */
13403 static off_t find_max_store_nbytes(void) {
13404 config_rec *c = NULL;
13405 unsigned int ctxt_precedence = 0;
13406 unsigned char have_user_limit, have_group_limit, have_class_limit,
13407 have_all_limit;
13408 off_t max_nbytes = 0UL;
13409
13410 have_user_limit = have_group_limit = have_class_limit =
13411 have_all_limit = FALSE;
13412
13413 c = find_config(CURRENT_CONF, CONF_PARAM, "MaxStoreFileSize", FALSE);
13414 while (c != NULL) {
13415 pr_signals_handle();
13416
13417 /* This check is for more than three arguments: one argument is the
13418 * classifier (i.e. "user", "group", or "class"), one argument is
13419 * the precedence, one is the number of bytes; the remaining arguments
13420 * are the individual items in the configured expression.
13421 */
13422
13423 if (c->argc > 3) {
13424 if (strcasecmp(c->argv[2], "user") == 0) {
13425 if (pr_expr_eval_user_or((char **) &c->argv[3]) == TRUE) {
13426 if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
13427
13428 /* Set the context precedence */
13429 ctxt_precedence = *((unsigned int *) c->argv[1]);
13430
13431 max_nbytes = *((off_t *) c->argv[0]);
13432
13433 have_group_limit = have_class_limit = have_all_limit = FALSE;
13434 have_user_limit = TRUE;
13435 }
13436 }
13437
13438 } else if (strcasecmp(c->argv[2], "group") == 0) {
13439 if (pr_expr_eval_group_or((char **) &c->argv[3]) == TRUE) {
13440 if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
13441
13442 /* Set the context precedence */
13443 ctxt_precedence = *((unsigned int *) c->argv[1]);
13444
13445 max_nbytes = *((off_t *) c->argv[0]);
13446
13447 have_user_limit = have_class_limit = have_all_limit = FALSE;
13448 have_group_limit = TRUE;
13449 }
13450 }
13451
13452 } else if (strcasecmp(c->argv[2], "class") == 0) {
13453 if (pr_expr_eval_class_or((char **) &c->argv[3]) == TRUE) {
13454 if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
13455
13456 /* Set the context precedence */
13457 ctxt_precedence = *((unsigned int *) c->argv[1]);
13458
13459 max_nbytes = *((off_t *) c->argv[0]);
13460
13461 have_user_limit = have_group_limit = have_all_limit = FALSE;
13462 have_class_limit = TRUE;
13463 }
13464 }
13465 }
13466
13467 } else {
13468 if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
13469
13470 /* Set the context precedence. */
13471 ctxt_precedence = *((unsigned int *) c->argv[1]);
13472
13473 max_nbytes = *((off_t *) c->argv[0]);
13474
13475 have_user_limit = have_group_limit = have_class_limit = FALSE;
13476 have_all_limit = TRUE;
13477 }
13478 }
13479
13480 c = find_config_next(c, c->next, CONF_PARAM, "MaxStoreFileSize", FALSE);
13481 }
13482
13483 /* Print out some nice debugging information. */
13484 if (max_nbytes > 0 &&
13485 (have_user_limit ||
13486 have_group_limit ||
13487 have_class_limit ||
13488 have_all_limit)) {
13489 pr_log_debug(DEBUG5, "MaxStoreFileSize (%" PR_LU " bytes) in effect for %s",
13490 (pr_off_t) max_nbytes,
13491 have_user_limit ? "user " : have_group_limit ? "group " :
13492 have_class_limit ? "class " : "all");
13493 }
13494
13495 return max_nbytes;
13496 }
13497
13498 static int fxp_handle_write(struct fxp_packet *fxp) {
13499 unsigned char *buf, *data, *ptr;
13500 char cmd_arg[256], *file, *name, *ptr2;
13501 ssize_t res;
13502 int xerrno = 0;
13503 uint32_t buflen, bufsz, datalen, status_code;
13504 uint64_t offset;
13505 struct fxp_handle *fxh;
13506 struct fxp_packet *resp;
13507 cmd_rec *cmd, *cmd2;
13508 pr_buffer_t *pbuf;
13509
13510 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
13511 offset = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
13512 datalen = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
13513 data = sftp_msg_read_data_direct(fxp->pool, &fxp->payload, &fxp->payload_sz,
13514 datalen);
13515
13516 memset(cmd_arg, '\0', sizeof(cmd_arg));
13517 pr_snprintf(cmd_arg, sizeof(cmd_arg)-1, "%s %" PR_LU " %lu", name,
13518 (pr_off_t) offset, (unsigned long) datalen);
13519 cmd = fxp_cmd_alloc(fxp->pool, "WRITE", cmd_arg);
13520 cmd->cmd_class = CL_WRITE|CL_SFTP;
13521 cmd->cmd_id = SFTP_CMD_ID;
13522
13523 pr_scoreboard_entry_update(session.pid,
13524 PR_SCORE_CMD, "%s", "WRITE", NULL, NULL);
13525 pr_scoreboard_entry_update(session.pid,
13526 PR_SCORE_CMD_ARG, "%s", name, NULL, NULL);
13527
13528 pr_proctitle_set("%s - %s: WRITE %s %" PR_LU " %lu", session.user,
13529 session.proc_prefix, name, (pr_off_t) offset, (unsigned long) datalen);
13530
13531 pr_trace_msg(trace_channel, 7, "received request: WRITE %s %" PR_LU " %lu",
13532 name, (pr_off_t) offset, (unsigned long) datalen);
13533
13534 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
13535 buf = ptr = palloc(fxp->pool, bufsz);
13536
13537 fxh = fxp_handle_get(name);
13538 if (fxh == NULL) {
13539 pr_trace_msg(trace_channel, 17,
13540 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
13541 name, strerror(errno));
13542
13543 status_code = SSH2_FX_INVALID_HANDLE;
13544
13545 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13546 (unsigned long) status_code, fxp_strerror(status_code));
13547
13548 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13549 fxp_strerror(status_code), NULL);
13550
13551 fxp_cmd_dispatch_err(cmd);
13552
13553 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13554 resp->payload = ptr;
13555 resp->payload_sz = (bufsz - buflen);
13556
13557 return fxp_packet_write(resp);
13558 }
13559
13560 if (fxh->fh == NULL) {
13561 status_code = SSH2_FX_INVALID_HANDLE;
13562
13563 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13564 (unsigned long) status_code, fxp_strerror(status_code));
13565
13566 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13567 fxp_strerror(status_code), NULL);
13568
13569 fxp_cmd_dispatch_err(cmd);
13570
13571 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13572 resp->payload = ptr;
13573 resp->payload_sz = (bufsz - buflen);
13574
13575 return fxp_packet_write(resp);
13576 }
13577
13578 /* Add a note containing the file handle for logging (Bug#3707). */
13579 fxp_set_filehandle_note(cmd, fxh);
13580
13581 pr_scoreboard_entry_update(session.pid,
13582 PR_SCORE_CMD_ARG, "%s", fxh->fh->fh_path, NULL, NULL);
13583 fxh->xfer.total_bytes += datalen;
13584
13585 /* It would be nice to check the requested offset against the size of
13586 * the file. However, the protocol specifically allows for sparse files,
13587 * where the requested offset is far beyond the end of the file.
13588 *
13589 * XXX Perhaps this should be configurable?
13590 */
13591 #if 0
13592 if (offset > st.st_size) {
13593 const char *reason;
13594
13595 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13596 "requested write offset (%" PR_LU " bytes) greater than size of "
13597 "'%s' (%" PR_LU " bytes)", (pr_off_t) offset, fxh->fh->fh_path,
13598 (pr_off_t) st.st_size);
13599
13600 status_code = fxp_errno2status(EINVAL, &reason);
13601
13602 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
13603 "('%s' [%d])", (unsigned long) status_code, reason, strerror(EINVAL),
13604 EINVAL);
13605
13606 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13607 reason, NULL);
13608
13609 fxp_cmd_dispatch_err(cmd);
13610
13611 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13612 resp->payload = ptr;
13613 resp->payload_sz = (bufsz - buflen);
13614
13615 return fxp_packet_write(resp);
13616 }
13617 #endif
13618
13619 /* Trim the full path to just the filename, for our STOR command. */
13620 ptr2 = strrchr(fxh->fh->fh_path, '/');
13621 if (ptr2 != NULL &&
13622 ptr2 != fxh->fh->fh_path) {
13623 file = pstrdup(fxp->pool, ptr2 + 1);
13624
13625 } else {
13626 file = fxh->fh->fh_path;
13627 }
13628
13629 cmd2 = fxp_cmd_alloc(fxp->pool, C_STOR, file);
13630 cmd2->cmd_class = CL_WRITE;
13631
13632 if (fxh->fh_have_cached_write_dir_check == FALSE) {
13633 fxh->fh_cached_write_dir_check = dir_check(fxp->pool, cmd2, G_WRITE,
13634 fxh->fh->fh_path, NULL);
13635 fxh->fh_have_cached_write_dir_check = TRUE;
13636 }
13637
13638 if (!(fxh->fh_cached_write_dir_check)) {
13639 status_code = SSH2_FX_PERMISSION_DENIED;
13640
13641 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13642 "WRITE of '%s' blocked by <Limit> configuration", fxh->fh->fh_path);
13643
13644 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13645 (unsigned long) status_code, fxp_strerror(status_code));
13646
13647 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13648 fxp_strerror(status_code), NULL);
13649
13650 fxp_cmd_dispatch_err(cmd);
13651
13652 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13653 resp->payload = ptr;
13654 resp->payload_sz = (bufsz - buflen);
13655
13656 return fxp_packet_write(resp);
13657 }
13658
13659 if (fxp_path_pass_regex_filters(fxp->pool, "WRITE", fxh->fh->fh_path) < 0) {
13660 status_code = fxp_errno2status(errno, NULL);
13661
13662 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13663 (unsigned long) status_code, fxp_strerror(status_code));
13664
13665 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13666 fxp_strerror(status_code), NULL);
13667
13668 fxp_cmd_dispatch_err(cmd);
13669
13670 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13671 resp->payload = ptr;
13672 resp->payload_sz = (bufsz - buflen);
13673
13674 return fxp_packet_write(resp);
13675 }
13676
13677 /* If the open flags have O_APPEND, treat this as an APPE command, rather
13678 * than a STOR command.
13679 *
13680 * Note that this means that the offset provided by the client in this
13681 * WRITE requested is ignored, per SFTP Draft:
13682 *
13683 * SSH_FXF_APPEND_DATA
13684 * Data is always written at the end of the file. The offset field
13685 * of SSH_FXP_WRITE requests is ignored.
13686 *
13687 * Instead, we override the offset to the current size of the file.
13688 */
13689 if (fxh->fh_flags & O_APPEND) {
13690 pr_trace_msg(trace_channel, 5, "ignoring WRITE offset %" PR_LU " for file "
13691 "opened for appending, using file size %" PR_LU " as offset instead",
13692 (pr_off_t) offset, (pr_off_t) fxh->fh_st->st_size);
13693 offset = fxh->fh_st->st_size;
13694
13695 cmd2 = fxp_cmd_alloc(fxp->pool, C_APPE, NULL);
13696
13697 } else {
13698 cmd2 = fxp_cmd_alloc(fxp->pool, C_STOR, NULL);
13699 }
13700
13701 if (S_ISREG(fxh->fh_st->st_mode)) {
13702 off_t *file_offset;
13703
13704 /* Stash the offset at which we're writing to this file. */
13705 file_offset = palloc(cmd->pool, sizeof(off_t));
13706 *file_offset = (off_t) offset;
13707 (void) pr_table_add(cmd->notes, "mod_xfer.file-offset", file_offset,
13708 sizeof(off_t));
13709 }
13710
13711 pbuf = pcalloc(fxp->pool, sizeof(pr_buffer_t));
13712 pbuf->buf = (char *) data;
13713 pbuf->buflen = datalen;
13714 pbuf->current = pbuf->buf;
13715 pbuf->remaining = 0;
13716 pr_event_generate("mod_sftp.sftp.data-read", pbuf);
13717
13718 pr_throttle_init(cmd2);
13719
13720 /* Handle zero-length writes as a special case; see Bug#4398. */
13721 if (datalen > 0) {
13722 res = pr_fsio_pwrite(fxh->fh, data, datalen, offset);
13723
13724 } else {
13725 res = 0;
13726 }
13727
13728 xerrno = errno;
13729
13730 /* Increment the "on-disk" file size with the number of bytes written.
13731 * We do this, rather than using fstat(2), to avoid performance penalties
13732 * associated with fstat(2) on network filesystems such as NFS. And we
13733 * want to track the on-disk size for enforcing limits such as
13734 * MaxStoreFileSize.
13735 *
13736 * Note that we only want to increment the file size if the chunk we
13737 * just wrote is PAST the current end of the file; we could be just
13738 * overwriting a chunk of the file.
13739 */
13740 if (res > 0) {
13741 size_t new_size;
13742
13743 new_size = offset + res;
13744 if ((off_t) new_size > fxh->fh_st->st_size) {
13745 fxh->fh_st->st_size = fxh->xfer.file_size = new_size;
13746 }
13747
13748 session.total_bytes += datalen;
13749 }
13750
13751 if (pr_data_get_timeout(PR_DATA_TIMEOUT_NO_TRANSFER) > 0) {
13752 pr_timer_reset(PR_TIMER_NOXFER, ANY_MODULE);
13753 }
13754
13755 if (pr_data_get_timeout(PR_DATA_TIMEOUT_STALLED) > 0) {
13756 pr_timer_reset(PR_TIMER_STALLED, ANY_MODULE);
13757 }
13758
13759 pr_throttle_pause(offset, FALSE, 0);
13760
13761 if (res < 0) {
13762 const char *reason;
13763
13764 (void) pr_trace_msg("fileperms", 1, "WRITE, user '%s' (UID %s, GID %s): "
13765 "error writing to '%s': %s", session.user,
13766 pr_uid2str(fxp->pool, session.uid), pr_gid2str(fxp->pool, session.gid),
13767 fxh->fh->fh_path, strerror(xerrno));
13768
13769 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13770 "error writing to '%s': %s", fxh->fh->fh_path, strerror(xerrno));
13771
13772 status_code = fxp_errno2status(xerrno, &reason);
13773
13774 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
13775 "('%s' [%d])", (unsigned long) status_code, reason,
13776 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
13777
13778 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13779 reason, NULL);
13780
13781 fxp_cmd_dispatch_err(cmd);
13782
13783 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13784 resp->payload = ptr;
13785 resp->payload_sz = (bufsz - buflen);
13786
13787 return fxp_packet_write(resp);
13788 }
13789
13790 if (fxh->fh_st->st_size > 0) {
13791 off_t nbytes_max_store = 0;
13792
13793 nbytes_max_store = find_max_store_nbytes();
13794 if (nbytes_max_store > 0) {
13795 if (fxh->fh_st->st_size > nbytes_max_store) {
13796 const char *reason;
13797 #if defined(EFBIG)
13798 xerrno = EFBIG;
13799 #elif defined(ENOSPC)
13800 xerrno = ENOSPC;
13801 #else
13802 xerrno = EIO;
13803 #endif
13804
13805 pr_log_pri(PR_LOG_NOTICE, "MaxStoreFileSize (%" PR_LU " %s) reached: "
13806 "aborting transfer of '%s'", (pr_off_t) nbytes_max_store,
13807 nbytes_max_store != 1 ? "bytes" : "byte", fxh->fh->fh_path);
13808
13809 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13810 "error writing %" PR_LU " bytes to '%s': %s "
13811 "(MaxStoreFileSize %" PR_LU " exceeded)", (pr_off_t) datalen,
13812 fxh->fh->fh_path, strerror(xerrno), (pr_off_t) nbytes_max_store);
13813
13814 status_code = fxp_errno2status(xerrno, &reason);
13815
13816 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
13817 "('%s' [%d])", (unsigned long) status_code, reason,
13818 strerror(xerrno), xerrno);
13819
13820 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13821 reason, NULL);
13822
13823 fxp_cmd_dispatch_err(cmd);
13824
13825 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13826 resp->payload = ptr;
13827 resp->payload_sz = (bufsz - buflen);
13828
13829 return fxp_packet_write(resp);
13830 }
13831 }
13832 }
13833
13834 status_code = SSH2_FX_OK;
13835
13836 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13837 (unsigned long) status_code, fxp_strerror(status_code));
13838
13839 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13840 fxp_strerror(status_code), NULL);
13841
13842 fxp_set_filehandle_sess_xfer(fxh);
13843 fxp_cmd_dispatch(cmd);
13844
13845 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13846 resp->payload = ptr;
13847 resp->payload_sz = (bufsz - buflen);
13848
13849 return fxp_packet_write(resp);
13850 }
13851
13852 static int fxp_handle_unlock(struct fxp_packet *fxp) {
13853 unsigned char *buf, *ptr;
13854 char *cmd_name, *name;
13855 uint32_t buflen, bufsz, lock_flags, status_code;
13856 uint64_t offset, lock_len;
13857 struct flock lock;
13858 struct fxp_handle *fxh;
13859 struct fxp_packet *resp;
13860 cmd_rec *cmd;
13861
13862 name = sftp_msg_read_string(fxp->pool, &fxp->payload, &fxp->payload_sz);
13863 offset = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
13864 lock_len = sftp_msg_read_long(fxp->pool, &fxp->payload, &fxp->payload_sz);
13865 lock_flags = sftp_msg_read_int(fxp->pool, &fxp->payload, &fxp->payload_sz);
13866
13867 cmd = fxp_cmd_alloc(fxp->pool, "UNLOCK", name);
13868 cmd->cmd_class = CL_WRITE|CL_SFTP;
13869 cmd->cmd_id = SFTP_CMD_ID;
13870
13871 pr_scoreboard_entry_update(session.pid,
13872 PR_SCORE_CMD, "%s", "UNLOCK", NULL, NULL);
13873 pr_scoreboard_entry_update(session.pid,
13874 PR_SCORE_CMD_ARG, "%s", name, NULL, NULL);
13875
13876 pr_proctitle_set("%s - %s: UNLOCK %s", session.user, session.proc_prefix,
13877 name);
13878
13879 pr_trace_msg(trace_channel, 7,
13880 "received request: UNLOCK %s %" PR_LU " %" PR_LU " %lu", name,
13881 (pr_off_t) offset, (pr_off_t) lock_len, (unsigned long) lock_flags);
13882
13883 buflen = bufsz = FXP_RESPONSE_DATA_DEFAULT_SZ;
13884 buf = ptr = palloc(fxp->pool, bufsz);
13885
13886 fxh = fxp_handle_get(name);
13887 if (fxh == NULL) {
13888 pr_trace_msg(trace_channel, 17,
13889 "%s: unable to find handle for name '%s': %s", (char *) cmd->argv[0],
13890 name, strerror(errno));
13891
13892 status_code = SSH2_FX_INVALID_HANDLE;
13893
13894 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13895 (unsigned long) status_code, fxp_strerror(status_code));
13896
13897 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13898 fxp_strerror(status_code), NULL);
13899
13900 fxp_cmd_dispatch_err(cmd);
13901
13902 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13903 resp->payload = ptr;
13904 resp->payload_sz = (bufsz - buflen);
13905
13906 return fxp_packet_write(resp);
13907 }
13908
13909 if (fxh->fh == NULL) {
13910 /* We do not support locking of directory handles, only files. */
13911 status_code = SSH2_FX_OP_UNSUPPORTED;
13912
13913 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13914 "client requested unsupported UNLOCK of a directory, rejecting");
13915
13916 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13917 (unsigned long) status_code, fxp_strerror(status_code));
13918
13919 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13920 fxp_strerror(status_code), NULL);
13921
13922 fxp_cmd_dispatch_err(cmd);
13923
13924 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13925 resp->payload = ptr;
13926 resp->payload_sz = (bufsz - buflen);
13927
13928 return fxp_packet_write(resp);
13929 }
13930
13931 cmd_name = cmd->argv[0];
13932 pr_cmd_set_name(cmd, "LOCK");
13933
13934 if (!dir_check(fxp->pool, cmd, G_WRITE, fxh->fh->fh_path, NULL)) {
13935 status_code = SSH2_FX_PERMISSION_DENIED;
13936
13937 pr_cmd_set_name(cmd, cmd_name);
13938
13939 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13940 "UNLOCK of '%s' blocked by <Limit> configuration", fxh->fh->fh_path);
13941
13942 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13943 (unsigned long) status_code, fxp_strerror(status_code));
13944
13945 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13946 fxp_strerror(status_code), NULL);
13947
13948 fxp_cmd_dispatch_err(cmd);
13949
13950 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13951 resp->payload = ptr;
13952 resp->payload_sz = (bufsz - buflen);
13953
13954 return fxp_packet_write(resp);
13955 }
13956 pr_cmd_set_name(cmd, cmd_name);
13957
13958 pr_scoreboard_entry_update(session.pid,
13959 PR_SCORE_CMD_ARG, "%s", fxh->fh->fh_path, NULL, NULL);
13960
13961 if (lock_flags & SSH2_FXL_DELETE) {
13962 lock.l_type = F_UNLCK;
13963
13964 } else {
13965 /* The LOCK command is used for adding locks, not UNLOCK. */
13966 status_code = SSH2_FX_OP_UNSUPPORTED;
13967
13968 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13969 "client requested locking using UNLOCK, rejecting");
13970
13971 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
13972 (unsigned long) status_code, fxp_strerror(status_code));
13973
13974 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
13975 fxp_strerror(status_code), NULL);
13976
13977 fxp_cmd_dispatch_err(cmd);
13978
13979 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
13980 resp->payload = ptr;
13981 resp->payload_sz = (bufsz - buflen);
13982
13983 return fxp_packet_write(resp);
13984 }
13985
13986 lock.l_whence = SEEK_SET;
13987 lock.l_start = offset;
13988 lock.l_len = lock_len;
13989
13990 if (lock_len > 0) {
13991 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13992 "client requested unlocking of '%s' from %" PR_LU " for %" PR_LU
13993 " bytes", fxh->fh->fh_path, (pr_off_t) offset, (pr_off_t) lock_len);
13994
13995 } else {
13996 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
13997 "client requested unlocking of '%s' from %" PR_LU " to end-of-file",
13998 fxh->fh->fh_path, (pr_off_t) offset);
13999 }
14000
14001 pr_trace_msg("lock", 9, "attempting to unlock file '%s'", fxh->fh->fh_path);
14002
14003 while (fcntl(fxh->fh->fh_fd, F_SETLK, &lock) < 0) {
14004 int xerrno;
14005 const char *reason;
14006
14007 if (errno == EINTR) {
14008 pr_signals_handle();
14009 continue;
14010 }
14011
14012 xerrno = errno;
14013 pr_trace_msg("lock", 3, "unlock of '%s' failed: %s", fxh->fh->fh_path,
14014 strerror(errno));
14015
14016 status_code = fxp_errno2status(xerrno, &reason);
14017
14018 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s' "
14019 "('%s' [%d])", (unsigned long) status_code, reason,
14020 xerrno != EOF ? strerror(xerrno) : "End of file", xerrno);
14021
14022 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
14023 reason, NULL);
14024
14025 fxp_cmd_dispatch_err(cmd);
14026
14027 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
14028 resp->payload = ptr;
14029 resp->payload_sz = (bufsz - buflen);
14030
14031 return fxp_packet_write(resp);
14032 }
14033
14034 pr_trace_msg("lock", 9, "unlock of file '%s' successful", fxh->fh->fh_path);
14035
14036 status_code = SSH2_FX_OK;
14037
14038 pr_trace_msg(trace_channel, 8, "sending response: STATUS %lu '%s'",
14039 (unsigned long) status_code, fxp_strerror(status_code));
14040
14041 fxp_status_write(fxp->pool, &buf, &buflen, fxp->request_id, status_code,
14042 fxp_strerror(status_code), NULL);
14043
14044 fxp_cmd_dispatch(cmd);
14045
14046 resp = fxp_packet_create(fxp->pool, fxp->channel_id);
14047 resp->payload = ptr;
14048 resp->payload_sz = (bufsz - buflen);
14049
14050 return fxp_packet_write(resp);
14051 }
14052
14053 static int fxp_send_display_login_file(uint32_t channel_id) {
14054 const char *msg;
14055 int res, xerrno;
14056 pool *sub_pool;
14057
14058 if (fxp_sent_display_login_file) {
14059 /* Already sent the file; no need to do it again. */
14060 return 0;
14061 }
14062
14063 if (fxp_displaylogin_fh == NULL) {
14064 /* No DisplayLogin file found. */
14065 return 0;
14066 }
14067
14068 if (fxp_pool == NULL) {
14069 fxp_pool = make_sub_pool(sftp_pool);
14070 pr_pool_tag(fxp_pool, "SFTP Pool");
14071 }
14072
14073 sub_pool = make_sub_pool(fxp_pool);
14074 pr_pool_tag(sub_pool, "SFTP DisplayLogin pool");
14075
14076 msg = sftp_display_fh_get_msg(sub_pool, fxp_displaylogin_fh);
14077 pr_fsio_close(fxp_displaylogin_fh);
14078
14079 if (msg == NULL) {
14080 destroy_pool(sub_pool);
14081 fxp_displaylogin_fh = NULL;
14082 return -1;
14083 }
14084
14085 pr_trace_msg(trace_channel, 3,
14086 "sending data from DisplayLogin file '%s'", fxp_displaylogin_fh->fh_path);
14087 fxp_displaylogin_fh = NULL;
14088
14089 res = sftp_channel_write_ext_data_stderr(sub_pool, channel_id,
14090 (unsigned char *) msg, strlen(msg));
14091 xerrno = errno;
14092
14093 if (res == 0) {
14094 fxp_sent_display_login_file = TRUE;
14095 }
14096
14097 destroy_pool(sub_pool);
14098 errno = xerrno;
14099 return res;
14100 }
14101
14102 /* Main entry point */
14103 int sftp_fxp_handle_packet(pool *p, void *ssh2, uint32_t channel_id,
14104 unsigned char *data, uint32_t datalen) {
14105 struct fxp_packet *fxp;
14106 int res;
14107
14108 /* Unused parameter; we read the SFTP request out of the provided buffer. */
14109 (void) ssh2;
14110
14111 if (fxp_pool == NULL) {
14112 fxp_pool = make_sub_pool(sftp_pool);
14113 pr_pool_tag(fxp_pool, "SFTP Pool");
14114 }
14115
14116 fxp = fxp_packet_read(channel_id, &data, &datalen);
14117 while (fxp != NULL) {
14118 pr_signals_handle();
14119
14120 /* This is a bit of a hack, for playing along better with mod_vroot,
14121 * which pays attention to the session.curr_phase value.
14122 *
14123 * I'm not sure which is better here, PRE_CMD vs CMD. Let's go with
14124 * PRE_CMD for now.
14125 */
14126 session.curr_phase = PRE_CMD;
14127
14128 if (fxp->request_id) {
14129 pr_trace_msg(trace_channel, 6,
14130 "received %s (%d) SFTP request (request ID %lu, channel ID %lu)",
14131 fxp_get_request_type_desc(fxp->request_type), fxp->request_type,
14132 (unsigned long) fxp->request_id, (unsigned long) channel_id);
14133
14134 } else {
14135 pr_trace_msg(trace_channel, 6,
14136 "received %s (%d) SFTP request (channel ID %lu)",
14137 fxp_get_request_type_desc(fxp->request_type), fxp->request_type,
14138 (unsigned long) channel_id);
14139 }
14140
14141 fxp_session = fxp_get_session(channel_id);
14142 if (fxp_session == NULL) {
14143 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
14144 "no existing SFTP session for channel ID %lu, rejecting request",
14145 (unsigned long) channel_id);
14146 destroy_pool(fxp->pool);
14147 errno = EPERM;
14148 return -1;
14149 }
14150
14151 pr_response_set_pool(fxp->pool);
14152
14153 /* Make sure to clear the response lists of any cruft from previous
14154 * requests.
14155 */
14156 pr_response_clear(&resp_list);
14157 pr_response_clear(&resp_err_list);
14158
14159 switch (fxp->request_type) {
14160 case SFTP_SSH2_FXP_INIT:
14161 /* If we already know the version, then the client has sent
14162 * FXP_INIT before, and should NOT be sending it again.
14163 *
14164 * However, per Bug#4227, there ARE clients which do send INIT
14165 * multiple times; I don't know why. And since OpenSSH handles
14166 * these repeated INITs without disconnecting clients, that is the
14167 * de facto expected behavior. We will do the same, but at least
14168 * log about it.
14169 */
14170 if (fxp_session->client_version > 0) {
14171 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
14172 "already received SFTP INIT %u request from client",
14173 (unsigned int) fxp_session->client_version);
14174 }
14175
14176 res = fxp_handle_init(fxp);
14177 break;
14178
14179 case SFTP_SSH2_FXP_CLOSE:
14180 allow_version_select = FALSE;
14181 res = fxp_handle_close(fxp);
14182 break;
14183
14184 case SFTP_SSH2_FXP_EXTENDED:
14185 res = fxp_handle_extended(fxp);
14186 break;
14187
14188 case SFTP_SSH2_FXP_FSETSTAT:
14189 allow_version_select = FALSE;
14190 res = fxp_handle_fsetstat(fxp);
14191 break;
14192
14193 case SFTP_SSH2_FXP_FSTAT:
14194 allow_version_select = FALSE;
14195 res = fxp_handle_fstat(fxp);
14196 break;
14197
14198 case SFTP_SSH2_FXP_LINK:
14199 allow_version_select = FALSE;
14200 res = fxp_handle_link(fxp);
14201 break;
14202
14203 case SFTP_SSH2_FXP_LOCK:
14204 allow_version_select = FALSE;
14205 res = fxp_handle_lock(fxp);
14206 break;
14207
14208 case SFTP_SSH2_FXP_LSTAT:
14209 allow_version_select = FALSE;
14210 res = fxp_handle_lstat(fxp);
14211 break;
14212
14213 case SFTP_SSH2_FXP_MKDIR:
14214 allow_version_select = FALSE;
14215 res = fxp_handle_mkdir(fxp);
14216 break;
14217
14218 case SFTP_SSH2_FXP_OPEN:
14219 allow_version_select = FALSE;
14220 res = fxp_handle_open(fxp);
14221 break;
14222
14223 case SFTP_SSH2_FXP_OPENDIR:
14224 allow_version_select = FALSE;
14225 res = fxp_handle_opendir(fxp);
14226 break;
14227
14228 case SFTP_SSH2_FXP_READ:
14229 allow_version_select = FALSE;
14230 res = fxp_handle_read(fxp);
14231 break;
14232
14233 case SFTP_SSH2_FXP_READDIR:
14234 allow_version_select = FALSE;
14235 res = fxp_handle_readdir(fxp);
14236 break;
14237
14238 case SFTP_SSH2_FXP_READLINK:
14239 allow_version_select = FALSE;
14240 res = fxp_handle_readlink(fxp);
14241 break;
14242
14243 case SFTP_SSH2_FXP_REALPATH:
14244 allow_version_select = FALSE;
14245 res = fxp_handle_realpath(fxp);
14246 break;
14247
14248 case SFTP_SSH2_FXP_REMOVE:
14249 allow_version_select = FALSE;
14250 res = fxp_handle_remove(fxp);
14251 break;
14252
14253 case SFTP_SSH2_FXP_RENAME:
14254 allow_version_select = FALSE;
14255 res = fxp_handle_rename(fxp);
14256 break;
14257
14258 case SFTP_SSH2_FXP_RMDIR:
14259 allow_version_select = FALSE;
14260 res = fxp_handle_rmdir(fxp);
14261 break;
14262
14263 case SFTP_SSH2_FXP_SETSTAT:
14264 allow_version_select = FALSE;
14265 res = fxp_handle_setstat(fxp);
14266 break;
14267
14268 case SFTP_SSH2_FXP_STAT:
14269 allow_version_select = FALSE;
14270 res = fxp_handle_stat(fxp);
14271 break;
14272
14273 case SFTP_SSH2_FXP_SYMLINK:
14274 allow_version_select = FALSE;
14275 res = fxp_handle_symlink(fxp);
14276 break;
14277
14278 case SFTP_SSH2_FXP_WRITE:
14279 allow_version_select = FALSE;
14280 res = fxp_handle_write(fxp);
14281 break;
14282
14283 case SFTP_SSH2_FXP_UNLOCK:
14284 allow_version_select = FALSE;
14285 res = fxp_handle_unlock(fxp);
14286 break;
14287
14288 default:
14289 pr_event_generate("sftp.invalid-request", fxp);
14290
14291 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
14292 "unhandled SFTP request type %d", fxp->request_type);
14293 destroy_pool(fxp->pool);
14294 fxp_packet_set_packet(NULL);
14295 fxp_session = NULL;
14296 return -1;
14297 }
14298
14299 destroy_pool(fxp->pool);
14300 fxp_packet_set_packet(NULL);
14301
14302 /* Clear any possibly stale pointers. */
14303 session.curr_cmd_rec = NULL;
14304
14305 if (res < 0) {
14306 fxp_session = NULL;
14307 return res;
14308 }
14309
14310 if (fxp_packet_have_cache_data() > 0) {
14311 fxp = fxp_packet_read(channel_id, NULL, NULL);
14312 continue;
14313 }
14314
14315 fxp_session = NULL;
14316 return res;
14317 }
14318
14319 fxp_session = NULL;
14320 return 0;
14321 }
14322
14323 int sftp_fxp_set_displaylogin(const char *path) {
14324 pr_fh_t *fh;
14325
14326 if (path == NULL) {
14327 errno = EINVAL;
14328 return -1;
14329 }
14330
14331 /* Support "DisplayLogin none", in case we need to disable support for
14332 * DisplayLogin files inherited from <Global> configurations.
14333 */
14334 if (strncasecmp(path, "none", 5) == 0) {
14335 return 0;
14336 }
14337
14338 fh = pr_fsio_open(path, O_RDONLY);
14339 if (fh == NULL)
14340 return -1;
14341
14342 fxp_displaylogin_fh = fh;
14343 return 0;
14344 }
14345
14346 int sftp_fxp_set_extensions(unsigned long ext_flags) {
14347 fxp_ext_flags = ext_flags;
14348 return 0;
14349 }
14350
14351 int sftp_fxp_set_protocol_version(unsigned int min_version,
14352 unsigned int max_version) {
14353 if ((min_version < 1 || min_version > 6) ||
14354 (max_version < 1 || max_version > 6)) {
14355 errno = EINVAL;
14356 return -1;
14357 }
14358
14359 if (min_version > max_version) {
14360 errno = EINVAL;
14361 return -1;
14362 }
14363
14364 fxp_min_client_version = min_version;
14365 fxp_max_client_version = max_version;
14366 return 0;
14367 }
14368
14369 int sftp_fxp_set_utf8_protocol_version(unsigned int version) {
14370 if (version < 1 || version > 6) {
14371 errno = EINVAL;
14372 return -1;
14373 }
14374
14375 fxp_utf8_protocol_version = version;
14376 return 0;
14377 }
14378
14379 void sftp_fxp_use_gmt(int use_gmt) {
14380 fxp_use_gmt = use_gmt;
14381 }
14382
14383 int sftp_fxp_open_session(uint32_t channel_id) {
14384 pool *sub_pool;
14385 struct fxp_session *sess, *last;
14386
14387 /* Check to see if we already have an SFTP session opened for the given
14388 * channel ID.
14389 */
14390 sess = last = fxp_sessions;
14391 while (sess) {
14392 pr_signals_handle();
14393
14394 if (sess->channel_id == channel_id) {
14395 errno = EEXIST;
14396 return -1;
14397 }
14398
14399 if (sess->next == NULL) {
14400 /* This is the last item in the list. */
14401 last = sess;
14402 }
14403
14404 sess = sess->next;
14405 }
14406
14407 /* Looks like we get to allocate a new one. */
14408 sub_pool = make_sub_pool(fxp_pool);
14409 pr_pool_tag(sub_pool, "SFTP session pool");
14410
14411 sess = pcalloc(sub_pool, sizeof(struct fxp_session));
14412 sess->pool = sub_pool;
14413 sess->channel_id = channel_id;
14414
14415 if (last) {
14416 last->next = sess;
14417 sess->prev = last;
14418
14419 } else {
14420 fxp_sessions = sess;
14421 }
14422
14423 pr_event_generate("mod_sftp.sftp.session-opened", NULL);
14424
14425 /* XXX Ignore any return value, for now. */
14426 (void) fxp_send_display_login_file(channel_id);
14427
14428 pr_session_set_protocol("sftp");
14429
14430 /* Clear any ASCII flags (set by default for FTP sessions. */
14431 session.sf_flags &= ~SF_ASCII;
14432
14433 return 0;
14434 }
14435
14436 int sftp_fxp_close_session(uint32_t channel_id) {
14437 struct fxp_session *sess;
14438
14439 /* Check to see if we have an SFTP session opened for the given channel ID.
14440 */
14441 sess = fxp_sessions;
14442 while (sess) {
14443 pr_signals_handle();
14444
14445 if (sess->channel_id == channel_id) {
14446 if (sess->next)
14447 sess->next->prev = sess->prev;
14448
14449 if (sess->prev) {
14450 sess->prev->next = sess->next;
14451
14452 } else {
14453 /* This is the start of the session list. */
14454 fxp_sessions = sess->next;
14455 }
14456
14457 if (sess->handle_tab != NULL) {
14458 int count;
14459
14460 count = pr_table_count(sess->handle_tab);
14461 if (count > 0) {
14462 int res;
14463 config_rec *c;
14464 void *callback_data = NULL;
14465
14466 c = find_config(main_server->conf, CONF_PARAM, "DeleteAbortedStores",
14467 FALSE);
14468 if (c) {
14469 callback_data = c->argv[0];
14470 }
14471
14472 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
14473 "aborting %d unclosed file %s", count,
14474 count != 1 ? "handles" : "handle");
14475
14476 /* Make sure that any abort processing has a valid response pool to
14477 * work with.
14478 */
14479 pr_response_set_pool(sess->pool);
14480
14481 res = pr_table_do(sess->handle_tab, fxp_handle_abort, callback_data,
14482 PR_TABLE_DO_FL_ALL);
14483 if (res < 0) {
14484 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
14485 "error doing session filehandle table: %s", strerror(errno));
14486 }
14487 }
14488
14489 (void) pr_table_empty(sess->handle_tab);
14490 (void) pr_table_free(sess->handle_tab);
14491 sess->handle_tab = NULL;
14492 }
14493
14494 destroy_pool(sess->pool);
14495 pr_session_set_protocol("ssh2");
14496
14497 pr_event_generate("mod_sftp.sftp.session-closed", NULL);
14498 return 0;
14499 }
14500
14501 sess = sess->next;
14502 }
14503
14504 errno = ENOENT;
14505 return -1;
14506 }