2 Unix SMB/CIFS implementation.
4 Copyright (C) Jeremy Allison 2013
5 Copyright (C) Volker Lendecke 2013
6 Copyright (C) Stefan Metzmacher 2013
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 This code is a thin wrapper around the existing
24 cli_smb2_XXXX() functions in libcli/smb/smb2cli_XXXXX.c,
25 but allows the handles to be mapped to uint16_t fnums,
26 which are easier for smbclient to use.
31 #include "async_smb.h"
32 #include "../libcli/smb/smbXcli_base.h"
33 #include "cli_smb2_fnum.h"
36 #include "../libcli/smb/smb2_create_blob.h"
37 #include "libsmb/proto.h"
38 #include "lib/util/tevent_ntstatus.h"
39 #include "../libcli/security/security.h"
40 #include "lib/util_ea.h"
41 #include "librpc/gen_ndr/ndr_ioctl.h"
44 uint64_t fid_persistent;
45 uint64_t fid_volatile;
49 * Handle mapping code.
52 /***************************************************************
53 Allocate a new fnum between 1 and 0xFFFE from an smb2_hnd.
54 Ensures handle is owned by cli struct.
55 ***************************************************************/
57 static NTSTATUS map_smb2_handle_to_fnum(struct cli_state *cli,
58 const struct smb2_hnd *ph, /* In */
59 uint16_t *pfnum) /* Out */
62 struct idr_context *idp = cli->smb2.open_handles;
63 struct smb2_hnd *owned_h = talloc_memdup(cli,
65 sizeof(struct smb2_hnd));
67 if (owned_h == NULL) {
68 return NT_STATUS_NO_MEMORY;
73 cli->smb2.open_handles = idr_init(cli);
74 if (cli->smb2.open_handles == NULL) {
76 return NT_STATUS_NO_MEMORY;
78 idp = cli->smb2.open_handles;
81 ret = idr_get_new_above(idp, owned_h, 1, 0xFFFE);
84 return NT_STATUS_NO_MEMORY;
87 *pfnum = (uint16_t)ret;
91 /***************************************************************
92 Return the smb2_hnd pointer associated with the given fnum.
93 ***************************************************************/
95 static NTSTATUS map_fnum_to_smb2_handle(struct cli_state *cli,
96 uint16_t fnum, /* In */
97 struct smb2_hnd **pph) /* Out */
99 struct idr_context *idp = cli->smb2.open_handles;
102 return NT_STATUS_INVALID_PARAMETER;
104 *pph = (struct smb2_hnd *)idr_find(idp, fnum);
106 return NT_STATUS_INVALID_HANDLE;
111 /***************************************************************
112 Delete the fnum to smb2_hnd mapping. Zeros out handle on
114 ***************************************************************/
116 static NTSTATUS delete_smb2_handle_mapping(struct cli_state *cli,
117 struct smb2_hnd **pph, /* In */
118 uint16_t fnum) /* In */
120 struct idr_context *idp = cli->smb2.open_handles;
124 return NT_STATUS_INVALID_PARAMETER;
127 ph = (struct smb2_hnd *)idr_find(idp, fnum);
129 return NT_STATUS_INVALID_PARAMETER;
131 idr_remove(idp, fnum);
136 /***************************************************************
138 ***************************************************************/
140 static uint8_t flags_to_smb2_oplock(uint32_t create_flags)
142 if (create_flags & REQUEST_BATCH_OPLOCK) {
143 return SMB2_OPLOCK_LEVEL_BATCH;
144 } else if (create_flags & REQUEST_OPLOCK) {
145 return SMB2_OPLOCK_LEVEL_EXCLUSIVE;
148 /* create_flags doesn't do a level2 request. */
149 return SMB2_OPLOCK_LEVEL_NONE;
152 /***************************************************************
153 Small wrapper that allows SMB2 create to return a uint16_t fnum.
154 ***************************************************************/
156 struct cli_smb2_create_fnum_state {
157 struct cli_state *cli;
158 struct smb_create_returns cr;
160 struct tevent_req *subreq;
163 static void cli_smb2_create_fnum_done(struct tevent_req *subreq);
164 static bool cli_smb2_create_fnum_cancel(struct tevent_req *req);
166 struct tevent_req *cli_smb2_create_fnum_send(TALLOC_CTX *mem_ctx,
167 struct tevent_context *ev,
168 struct cli_state *cli,
170 uint32_t create_flags,
171 uint32_t desired_access,
172 uint32_t file_attributes,
173 uint32_t share_access,
174 uint32_t create_disposition,
175 uint32_t create_options)
177 struct tevent_req *req, *subreq;
178 struct cli_smb2_create_fnum_state *state;
180 req = tevent_req_create(mem_ctx, &state,
181 struct cli_smb2_create_fnum_state);
187 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
188 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
189 return tevent_req_post(req, ev);
192 if (cli->backup_intent) {
193 create_options |= FILE_OPEN_FOR_BACKUP_INTENT;
196 /* SMB2 is pickier about pathnames. Ensure it doesn't
198 if (*fname == '\\') {
202 subreq = smb2cli_create_send(state, ev,
208 flags_to_smb2_oplock(create_flags),
209 SMB2_IMPERSONATION_IMPERSONATION,
216 if (tevent_req_nomem(subreq, req)) {
217 return tevent_req_post(req, ev);
219 tevent_req_set_callback(subreq, cli_smb2_create_fnum_done, req);
221 state->subreq = subreq;
222 tevent_req_set_cancel_fn(req, cli_smb2_create_fnum_cancel);
227 static void cli_smb2_create_fnum_done(struct tevent_req *subreq)
229 struct tevent_req *req = tevent_req_callback_data(
230 subreq, struct tevent_req);
231 struct cli_smb2_create_fnum_state *state = tevent_req_data(
232 req, struct cli_smb2_create_fnum_state);
236 status = smb2cli_create_recv(subreq, &h.fid_persistent,
237 &h.fid_volatile, &state->cr, NULL, NULL);
239 if (tevent_req_nterror(req, status)) {
243 status = map_smb2_handle_to_fnum(state->cli, &h, &state->fnum);
244 if (tevent_req_nterror(req, status)) {
247 tevent_req_done(req);
250 static bool cli_smb2_create_fnum_cancel(struct tevent_req *req)
252 struct cli_smb2_create_fnum_state *state = tevent_req_data(
253 req, struct cli_smb2_create_fnum_state);
254 return tevent_req_cancel(state->subreq);
257 NTSTATUS cli_smb2_create_fnum_recv(struct tevent_req *req, uint16_t *pfnum,
258 struct smb_create_returns *cr)
260 struct cli_smb2_create_fnum_state *state = tevent_req_data(
261 req, struct cli_smb2_create_fnum_state);
264 if (tevent_req_is_nterror(req, &status)) {
268 *pfnum = state->fnum;
276 NTSTATUS cli_smb2_create_fnum(struct cli_state *cli,
278 uint32_t create_flags,
279 uint32_t desired_access,
280 uint32_t file_attributes,
281 uint32_t share_access,
282 uint32_t create_disposition,
283 uint32_t create_options,
285 struct smb_create_returns *cr)
287 TALLOC_CTX *frame = talloc_stackframe();
288 struct tevent_context *ev;
289 struct tevent_req *req;
290 NTSTATUS status = NT_STATUS_NO_MEMORY;
292 if (smbXcli_conn_has_async_calls(cli->conn)) {
294 * Can't use sync call while an async call is in flight
296 status = NT_STATUS_INVALID_PARAMETER;
299 ev = samba_tevent_context_init(frame);
303 req = cli_smb2_create_fnum_send(frame, ev, cli, fname, create_flags,
304 desired_access, file_attributes,
305 share_access, create_disposition,
310 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
313 status = cli_smb2_create_fnum_recv(req, pfid, cr);
319 /***************************************************************
320 Small wrapper that allows SMB2 close to use a uint16_t fnum.
321 ***************************************************************/
323 struct cli_smb2_close_fnum_state {
324 struct cli_state *cli;
329 static void cli_smb2_close_fnum_done(struct tevent_req *subreq);
331 struct tevent_req *cli_smb2_close_fnum_send(TALLOC_CTX *mem_ctx,
332 struct tevent_context *ev,
333 struct cli_state *cli,
336 struct tevent_req *req, *subreq;
337 struct cli_smb2_close_fnum_state *state;
340 req = tevent_req_create(mem_ctx, &state,
341 struct cli_smb2_close_fnum_state);
348 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
349 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
350 return tevent_req_post(req, ev);
353 status = map_fnum_to_smb2_handle(cli, fnum, &state->ph);
354 if (tevent_req_nterror(req, status)) {
355 return tevent_req_post(req, ev);
358 subreq = smb2cli_close_send(state, ev, cli->conn, cli->timeout,
359 cli->smb2.session, cli->smb2.tcon,
360 0, state->ph->fid_persistent,
361 state->ph->fid_volatile);
362 if (tevent_req_nomem(subreq, req)) {
363 return tevent_req_post(req, ev);
365 tevent_req_set_callback(subreq, cli_smb2_close_fnum_done, req);
369 static void cli_smb2_close_fnum_done(struct tevent_req *subreq)
371 struct tevent_req *req = tevent_req_callback_data(
372 subreq, struct tevent_req);
373 struct cli_smb2_close_fnum_state *state = tevent_req_data(
374 req, struct cli_smb2_close_fnum_state);
377 status = smb2cli_close_recv(subreq);
378 if (tevent_req_nterror(req, status)) {
382 /* Delete the fnum -> handle mapping. */
383 status = delete_smb2_handle_mapping(state->cli, &state->ph,
385 if (tevent_req_nterror(req, status)) {
388 tevent_req_done(req);
391 NTSTATUS cli_smb2_close_fnum_recv(struct tevent_req *req)
393 return tevent_req_simple_recv_ntstatus(req);
396 NTSTATUS cli_smb2_close_fnum(struct cli_state *cli, uint16_t fnum)
398 TALLOC_CTX *frame = talloc_stackframe();
399 struct tevent_context *ev;
400 struct tevent_req *req;
401 NTSTATUS status = NT_STATUS_NO_MEMORY;
403 if (smbXcli_conn_has_async_calls(cli->conn)) {
405 * Can't use sync call while an async call is in flight
407 status = NT_STATUS_INVALID_PARAMETER;
410 ev = samba_tevent_context_init(frame);
414 req = cli_smb2_close_fnum_send(frame, ev, cli, fnum);
418 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
421 status = cli_smb2_close_fnum_recv(req);
427 /***************************************************************
428 Small wrapper that allows SMB2 to create a directory
430 ***************************************************************/
432 NTSTATUS cli_smb2_mkdir(struct cli_state *cli, const char *dname)
437 if (smbXcli_conn_has_async_calls(cli->conn)) {
439 * Can't use sync call while an async call is in flight
441 return NT_STATUS_INVALID_PARAMETER;
444 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
445 return NT_STATUS_INVALID_PARAMETER;
448 status = cli_smb2_create_fnum(cli,
450 0, /* create_flags */
451 FILE_READ_ATTRIBUTES, /* desired_access */
452 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
453 FILE_SHARE_READ|FILE_SHARE_WRITE, /* share_access */
454 FILE_CREATE, /* create_disposition */
455 FILE_DIRECTORY_FILE, /* create_options */
459 if (!NT_STATUS_IS_OK(status)) {
462 return cli_smb2_close_fnum(cli, fnum);
465 /***************************************************************
466 Small wrapper that allows SMB2 to delete a directory
468 ***************************************************************/
470 NTSTATUS cli_smb2_rmdir(struct cli_state *cli, const char *dname)
475 if (smbXcli_conn_has_async_calls(cli->conn)) {
477 * Can't use sync call while an async call is in flight
479 return NT_STATUS_INVALID_PARAMETER;
482 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
483 return NT_STATUS_INVALID_PARAMETER;
486 status = cli_smb2_create_fnum(cli,
488 0, /* create_flags */
489 DELETE_ACCESS, /* desired_access */
490 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
491 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access */
492 FILE_OPEN, /* create_disposition */
493 FILE_DIRECTORY_FILE|FILE_DELETE_ON_CLOSE, /* create_options */
497 if (!NT_STATUS_IS_OK(status)) {
500 return cli_smb2_close_fnum(cli, fnum);
503 /***************************************************************
504 Small wrapper that allows SMB2 to unlink a pathname.
506 ***************************************************************/
508 NTSTATUS cli_smb2_unlink(struct cli_state *cli, const char *fname)
513 if (smbXcli_conn_has_async_calls(cli->conn)) {
515 * Can't use sync call while an async call is in flight
517 return NT_STATUS_INVALID_PARAMETER;
520 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
521 return NT_STATUS_INVALID_PARAMETER;
524 status = cli_smb2_create_fnum(cli,
526 0, /* create_flags */
527 DELETE_ACCESS, /* desired_access */
528 FILE_ATTRIBUTE_NORMAL, /* file attributes */
529 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access */
530 FILE_OPEN, /* create_disposition */
531 FILE_DELETE_ON_CLOSE, /* create_options */
535 if (!NT_STATUS_IS_OK(status)) {
538 return cli_smb2_close_fnum(cli, fnum);
541 /***************************************************************
542 Utility function to parse a SMB2_FIND_ID_BOTH_DIRECTORY_INFO reply.
543 ***************************************************************/
545 static NTSTATUS parse_finfo_id_both_directory_info(uint8_t *dir_data,
546 uint32_t dir_data_length,
547 struct file_info *finfo,
548 uint32_t *next_offset)
554 if (dir_data_length < 4) {
555 return NT_STATUS_INFO_LENGTH_MISMATCH;
558 *next_offset = IVAL(dir_data, 0);
560 if (*next_offset > dir_data_length) {
561 return NT_STATUS_INFO_LENGTH_MISMATCH;
564 if (*next_offset != 0) {
565 /* Ensure we only read what in this record. */
566 dir_data_length = *next_offset;
569 if (dir_data_length < 105) {
570 return NT_STATUS_INFO_LENGTH_MISMATCH;
573 finfo->atime_ts = interpret_long_date((const char *)dir_data + 16);
574 finfo->mtime_ts = interpret_long_date((const char *)dir_data + 24);
575 finfo->ctime_ts = interpret_long_date((const char *)dir_data + 32);
576 finfo->size = IVAL2_TO_SMB_BIG_UINT(dir_data + 40, 0);
577 finfo->mode = CVAL(dir_data + 56, 0);
578 namelen = IVAL(dir_data + 60,0);
579 if (namelen > (dir_data_length - 104)) {
580 return NT_STATUS_INFO_LENGTH_MISMATCH;
582 slen = CVAL(dir_data + 68, 0);
584 return NT_STATUS_INFO_LENGTH_MISMATCH;
586 ret = pull_string_talloc(finfo,
588 FLAGS2_UNICODE_STRINGS,
593 if (ret == (size_t)-1) {
594 /* Bad conversion. */
595 return NT_STATUS_INVALID_NETWORK_RESPONSE;
598 ret = pull_string_talloc(finfo,
600 FLAGS2_UNICODE_STRINGS,
605 if (ret == (size_t)-1) {
606 /* Bad conversion. */
607 return NT_STATUS_INVALID_NETWORK_RESPONSE;
612 /*******************************************************************
613 Given a filename - get its directory name
614 ********************************************************************/
616 static bool windows_parent_dirname(TALLOC_CTX *mem_ctx,
624 p = strrchr_m(dir, '\\'); /* Find final '\\', if any */
627 if (!(*parent = talloc_strdup(mem_ctx, "\\"))) {
638 if (!(*parent = (char *)talloc_memdup(mem_ctx, dir, len+1))) {
641 (*parent)[len] = '\0';
649 /***************************************************************
650 Wrapper that allows SMB2 to list a directory.
652 ***************************************************************/
654 NTSTATUS cli_smb2_list(struct cli_state *cli,
655 const char *pathname,
657 NTSTATUS (*fn)(const char *,
664 uint16_t fnum = 0xffff;
665 char *parent_dir = NULL;
666 const char *mask = NULL;
667 struct smb2_hnd *ph = NULL;
668 bool processed_file = false;
669 TALLOC_CTX *frame = talloc_stackframe();
670 TALLOC_CTX *subframe = NULL;
673 if (smbXcli_conn_has_async_calls(cli->conn)) {
675 * Can't use sync call while an async call is in flight
677 status = NT_STATUS_INVALID_PARAMETER;
681 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
682 status = NT_STATUS_INVALID_PARAMETER;
686 /* Get the directory name. */
687 if (!windows_parent_dirname(frame,
691 status = NT_STATUS_NO_MEMORY;
695 mask_has_wild = ms_has_wild(mask);
697 status = cli_smb2_create_fnum(cli,
699 0, /* create_flags */
700 SEC_DIR_LIST|SEC_DIR_READ_ATTRIBUTE,/* desired_access */
701 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
702 FILE_SHARE_READ|FILE_SHARE_WRITE, /* share_access */
703 FILE_OPEN, /* create_disposition */
704 FILE_DIRECTORY_FILE, /* create_options */
708 if (!NT_STATUS_IS_OK(status)) {
712 status = map_fnum_to_smb2_handle(cli,
715 if (!NT_STATUS_IS_OK(status)) {
720 uint8_t *dir_data = NULL;
721 uint32_t dir_data_length = 0;
722 uint32_t next_offset = 0;
723 subframe = talloc_stackframe();
725 status = smb2cli_query_directory(cli->conn,
729 SMB2_FIND_ID_BOTH_DIRECTORY_INFO,
740 if (!NT_STATUS_IS_OK(status)) {
741 if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES)) {
748 struct file_info *finfo = talloc_zero(subframe,
752 status = NT_STATUS_NO_MEMORY;
756 status = parse_finfo_id_both_directory_info(dir_data,
761 if (!NT_STATUS_IS_OK(status)) {
765 if (dir_check_ftype((uint32_t)finfo->mode,
766 (uint32_t)attribute)) {
768 * Only process if attributes match.
769 * On SMB1 server does this, so on
770 * SMB2 we need to emulate in the
773 * https://bugzilla.samba.org/show_bug.cgi?id=10260
775 processed_file = true;
777 status = fn(cli->dfs_mountpoint,
782 if (!NT_STATUS_IS_OK(status)) {
789 /* Move to next entry. */
791 dir_data += next_offset;
792 dir_data_length -= next_offset;
794 } while (next_offset != 0);
796 TALLOC_FREE(subframe);
798 if (!mask_has_wild) {
800 * MacOSX 10 doesn't set STATUS_NO_MORE_FILES
801 * when handed a non-wildcard path. Do it
802 * for the server (with a non-wildcard path
803 * there should only ever be one file returned.
805 status = STATUS_NO_MORE_FILES;
809 } while (NT_STATUS_IS_OK(status));
811 if (NT_STATUS_EQUAL(status, STATUS_NO_MORE_FILES)) {
812 status = NT_STATUS_OK;
815 if (NT_STATUS_IS_OK(status) && !processed_file) {
817 * In SMB1 findfirst returns NT_STATUS_NO_SUCH_FILE
818 * if no files match. Emulate this in the client.
820 status = NT_STATUS_NO_SUCH_FILE;
825 if (fnum != 0xffff) {
826 cli_smb2_close_fnum(cli, fnum);
828 TALLOC_FREE(subframe);
833 /***************************************************************
834 Wrapper that allows SMB2 to query a path info (basic level).
836 ***************************************************************/
838 NTSTATUS cli_smb2_qpathinfo_basic(struct cli_state *cli,
840 SMB_STRUCT_STAT *sbuf,
841 uint32_t *attributes)
844 struct smb_create_returns cr;
845 uint16_t fnum = 0xffff;
846 size_t namelen = strlen(name);
848 if (smbXcli_conn_has_async_calls(cli->conn)) {
850 * Can't use sync call while an async call is in flight
852 return NT_STATUS_INVALID_PARAMETER;
855 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
856 return NT_STATUS_INVALID_PARAMETER;
859 /* SMB2 is pickier about pathnames. Ensure it doesn't
861 if (namelen > 0 && name[namelen-1] == '\\') {
862 char *modname = talloc_strdup(talloc_tos(), name);
863 modname[namelen-1] = '\0';
867 /* This is commonly used as a 'cd'. Try qpathinfo on
868 a directory handle first. */
870 status = cli_smb2_create_fnum(cli,
872 0, /* create_flags */
873 FILE_READ_ATTRIBUTES, /* desired_access */
874 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
875 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access */
876 FILE_OPEN, /* create_disposition */
877 FILE_DIRECTORY_FILE, /* create_options */
881 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_A_DIRECTORY)) {
883 status = cli_smb2_create_fnum(cli,
885 0, /* create_flags */
886 FILE_READ_ATTRIBUTES, /* desired_access */
887 0, /* file attributes */
888 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access */
889 FILE_OPEN, /* create_disposition */
890 0, /* create_options */
895 if (!NT_STATUS_IS_OK(status)) {
899 cli_smb2_close_fnum(cli, fnum);
903 sbuf->st_ex_atime = nt_time_to_unix_timespec(cr.last_access_time);
904 sbuf->st_ex_mtime = nt_time_to_unix_timespec(cr.last_write_time);
905 sbuf->st_ex_ctime = nt_time_to_unix_timespec(cr.change_time);
906 sbuf->st_ex_size = cr.end_of_file;
907 *attributes = cr.file_attributes;
912 /***************************************************************
913 Helper function for pathname operations.
914 ***************************************************************/
916 static NTSTATUS get_fnum_from_path(struct cli_state *cli,
918 uint32_t desired_access,
922 size_t namelen = strlen(name);
923 TALLOC_CTX *frame = talloc_stackframe();
925 /* SMB2 is pickier about pathnames. Ensure it doesn't
927 if (namelen > 0 && name[namelen-1] == '\\') {
928 char *modname = talloc_strdup(frame, name);
929 if (modname == NULL) {
930 status = NT_STATUS_NO_MEMORY;
933 modname[namelen-1] = '\0';
937 /* Try to open a file handle first. */
938 status = cli_smb2_create_fnum(cli,
940 0, /* create_flags */
942 0, /* file attributes */
943 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access */
944 FILE_OPEN, /* create_disposition */
945 0, /* create_options */
949 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
950 status = cli_smb2_create_fnum(cli,
952 0, /* create_flags */
954 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
955 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access */
956 FILE_OPEN, /* create_disposition */
957 FILE_DIRECTORY_FILE, /* create_options */
968 /***************************************************************
969 Wrapper that allows SMB2 to query a path info (ALTNAME level).
971 ***************************************************************/
973 NTSTATUS cli_smb2_qpathinfo_alt_name(struct cli_state *cli,
978 DATA_BLOB outbuf = data_blob_null;
979 uint16_t fnum = 0xffff;
980 struct smb2_hnd *ph = NULL;
981 uint32_t altnamelen = 0;
982 TALLOC_CTX *frame = talloc_stackframe();
984 if (smbXcli_conn_has_async_calls(cli->conn)) {
986 * Can't use sync call while an async call is in flight
988 status = NT_STATUS_INVALID_PARAMETER;
992 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
993 status = NT_STATUS_INVALID_PARAMETER;
997 status = get_fnum_from_path(cli,
999 FILE_READ_ATTRIBUTES,
1002 if (!NT_STATUS_IS_OK(status)) {
1006 status = map_fnum_to_smb2_handle(cli,
1009 if (!NT_STATUS_IS_OK(status)) {
1013 /* getinfo on the handle with info_type SMB2_GETINFO_FILE (1),
1014 level SMB_FILE_ALTERNATE_NAME_INFORMATION (1021) == SMB2 21 */
1016 status = smb2cli_query_info(cli->conn,
1020 1, /* in_info_type */
1021 (SMB_FILE_ALTERNATE_NAME_INFORMATION - 1000), /* in_file_info_class */
1022 0xFFFF, /* in_max_output_length */
1023 NULL, /* in_input_buffer */
1024 0, /* in_additional_info */
1031 if (!NT_STATUS_IS_OK(status)) {
1035 /* Parse the reply. */
1036 if (outbuf.length < 4) {
1037 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1041 altnamelen = IVAL(outbuf.data, 0);
1042 if (altnamelen > outbuf.length - 4) {
1043 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1047 if (altnamelen > 0) {
1049 char *short_name = NULL;
1050 ret = pull_string_talloc(frame,
1052 FLAGS2_UNICODE_STRINGS,
1057 if (ret == (size_t)-1) {
1058 /* Bad conversion. */
1059 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1063 fstrcpy(alt_name, short_name);
1068 status = NT_STATUS_OK;
1072 if (fnum != 0xffff) {
1073 cli_smb2_close_fnum(cli, fnum);
1080 /***************************************************************
1081 Wrapper that allows SMB2 to query a fnum info (basic level).
1083 ***************************************************************/
1085 NTSTATUS cli_smb2_qfileinfo_basic(struct cli_state *cli,
1089 struct timespec *create_time,
1090 struct timespec *access_time,
1091 struct timespec *write_time,
1092 struct timespec *change_time,
1096 DATA_BLOB outbuf = data_blob_null;
1097 struct smb2_hnd *ph = NULL;
1098 TALLOC_CTX *frame = talloc_stackframe();
1100 if (smbXcli_conn_has_async_calls(cli->conn)) {
1102 * Can't use sync call while an async call is in flight
1104 status = NT_STATUS_INVALID_PARAMETER;
1108 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1109 status = NT_STATUS_INVALID_PARAMETER;
1113 status = map_fnum_to_smb2_handle(cli,
1116 if (!NT_STATUS_IS_OK(status)) {
1120 /* getinfo on the handle with info_type SMB2_GETINFO_FILE (1),
1121 level 0x12 (SMB2_FILE_ALL_INFORMATION). */
1123 status = smb2cli_query_info(cli->conn,
1127 1, /* in_info_type */
1128 (SMB_FILE_ALL_INFORMATION - 1000), /* in_file_info_class */
1129 0xFFFF, /* in_max_output_length */
1130 NULL, /* in_input_buffer */
1131 0, /* in_additional_info */
1137 if (!NT_STATUS_IS_OK(status)) {
1141 /* Parse the reply. */
1142 if (outbuf.length < 0x60) {
1143 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1148 *create_time = interpret_long_date((const char *)outbuf.data + 0x0);
1151 *access_time = interpret_long_date((const char *)outbuf.data + 0x8);
1154 *write_time = interpret_long_date((const char *)outbuf.data + 0x10);
1157 *change_time = interpret_long_date((const char *)outbuf.data + 0x18);
1160 uint32_t attr = IVAL(outbuf.data, 0x20);
1161 *mode = (uint16_t)attr;
1164 uint64_t file_size = BVAL(outbuf.data, 0x30);
1165 *size = (off_t)file_size;
1168 uint64_t file_index = BVAL(outbuf.data, 0x40);
1169 *ino = (SMB_INO_T)file_index;
1178 /***************************************************************
1179 Wrapper that allows SMB2 to query an fnum.
1180 Implement on top of cli_smb2_qfileinfo_basic().
1182 ***************************************************************/
1184 NTSTATUS cli_smb2_getattrE(struct cli_state *cli,
1188 time_t *change_time,
1189 time_t *access_time,
1192 struct timespec access_time_ts;
1193 struct timespec write_time_ts;
1194 struct timespec change_time_ts;
1195 NTSTATUS status = cli_smb2_qfileinfo_basic(cli,
1205 if (!NT_STATUS_IS_OK(status)) {
1210 *change_time = change_time_ts.tv_sec;
1213 *access_time = access_time_ts.tv_sec;
1216 *write_time = write_time_ts.tv_sec;
1218 return NT_STATUS_OK;
1221 /***************************************************************
1222 Wrapper that allows SMB2 to get pathname attributes.
1224 ***************************************************************/
1226 NTSTATUS cli_smb2_getatr(struct cli_state *cli,
1233 uint16_t fnum = 0xffff;
1234 struct smb2_hnd *ph = NULL;
1235 TALLOC_CTX *frame = talloc_stackframe();
1237 if (smbXcli_conn_has_async_calls(cli->conn)) {
1239 * Can't use sync call while an async call is in flight
1241 status = NT_STATUS_INVALID_PARAMETER;
1245 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1246 status = NT_STATUS_INVALID_PARAMETER;
1250 status = get_fnum_from_path(cli,
1252 FILE_READ_ATTRIBUTES,
1255 if (!NT_STATUS_IS_OK(status)) {
1259 status = map_fnum_to_smb2_handle(cli,
1262 if (!NT_STATUS_IS_OK(status)) {
1265 status = cli_smb2_getattrE(cli,
1272 if (!NT_STATUS_IS_OK(status)) {
1278 if (fnum != 0xffff) {
1279 cli_smb2_close_fnum(cli, fnum);
1286 /***************************************************************
1287 Wrapper that allows SMB2 to query a pathname info (basic level).
1288 Implement on top of cli_smb2_qfileinfo_basic().
1290 ***************************************************************/
1292 NTSTATUS cli_smb2_qpathinfo2(struct cli_state *cli,
1294 struct timespec *create_time,
1295 struct timespec *access_time,
1296 struct timespec *write_time,
1297 struct timespec *change_time,
1303 struct smb2_hnd *ph = NULL;
1304 uint16_t fnum = 0xffff;
1305 TALLOC_CTX *frame = talloc_stackframe();
1307 if (smbXcli_conn_has_async_calls(cli->conn)) {
1309 * Can't use sync call while an async call is in flight
1311 status = NT_STATUS_INVALID_PARAMETER;
1315 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1316 status = NT_STATUS_INVALID_PARAMETER;
1320 status = get_fnum_from_path(cli,
1322 FILE_READ_ATTRIBUTES,
1325 if (!NT_STATUS_IS_OK(status)) {
1329 status = map_fnum_to_smb2_handle(cli,
1332 if (!NT_STATUS_IS_OK(status)) {
1336 status = cli_smb2_qfileinfo_basic(cli,
1348 if (fnum != 0xffff) {
1349 cli_smb2_close_fnum(cli, fnum);
1356 /***************************************************************
1357 Wrapper that allows SMB2 to query pathname streams.
1359 ***************************************************************/
1361 NTSTATUS cli_smb2_qpathinfo_streams(struct cli_state *cli,
1363 TALLOC_CTX *mem_ctx,
1364 unsigned int *pnum_streams,
1365 struct stream_struct **pstreams)
1368 struct smb2_hnd *ph = NULL;
1369 uint16_t fnum = 0xffff;
1370 DATA_BLOB outbuf = data_blob_null;
1371 TALLOC_CTX *frame = talloc_stackframe();
1373 if (smbXcli_conn_has_async_calls(cli->conn)) {
1375 * Can't use sync call while an async call is in flight
1377 status = NT_STATUS_INVALID_PARAMETER;
1381 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1382 status = NT_STATUS_INVALID_PARAMETER;
1386 status = get_fnum_from_path(cli,
1388 FILE_READ_ATTRIBUTES,
1391 if (!NT_STATUS_IS_OK(status)) {
1395 status = map_fnum_to_smb2_handle(cli,
1398 if (!NT_STATUS_IS_OK(status)) {
1402 /* getinfo on the handle with info_type SMB2_GETINFO_FILE (1),
1403 level 22 (SMB2_FILE_STREAM_INFORMATION). */
1405 status = smb2cli_query_info(cli->conn,
1409 1, /* in_info_type */
1410 (SMB_FILE_STREAM_INFORMATION - 1000), /* in_file_info_class */
1411 0xFFFF, /* in_max_output_length */
1412 NULL, /* in_input_buffer */
1413 0, /* in_additional_info */
1420 if (!NT_STATUS_IS_OK(status)) {
1424 /* Parse the reply. */
1425 if (!parse_streams_blob(mem_ctx,
1430 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1436 if (fnum != 0xffff) {
1437 cli_smb2_close_fnum(cli, fnum);
1444 /***************************************************************
1445 Wrapper that allows SMB2 to set pathname attributes.
1447 ***************************************************************/
1449 NTSTATUS cli_smb2_setatr(struct cli_state *cli,
1455 uint16_t fnum = 0xffff;
1456 struct smb2_hnd *ph = NULL;
1457 uint8_t inbuf_store[40];
1458 DATA_BLOB inbuf = data_blob_null;
1459 TALLOC_CTX *frame = talloc_stackframe();
1461 if (smbXcli_conn_has_async_calls(cli->conn)) {
1463 * Can't use sync call while an async call is in flight
1465 status = NT_STATUS_INVALID_PARAMETER;
1469 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1470 status = NT_STATUS_INVALID_PARAMETER;
1474 status = get_fnum_from_path(cli,
1476 FILE_WRITE_ATTRIBUTES,
1479 if (!NT_STATUS_IS_OK(status)) {
1483 status = map_fnum_to_smb2_handle(cli,
1486 if (!NT_STATUS_IS_OK(status)) {
1490 /* setinfo on the handle with info_type SMB2_SETINFO_FILE (1),
1491 level 4 (SMB_FILE_BASIC_INFORMATION - 1000). */
1493 inbuf.data = inbuf_store;
1494 inbuf.length = sizeof(inbuf_store);
1495 data_blob_clear(&inbuf);
1497 SSVAL(inbuf.data, 32, attr);
1499 put_long_date((char *)inbuf.data + 16,mtime);
1501 /* Set all the other times to -1. */
1502 SBVAL(inbuf.data, 0, 0xFFFFFFFFFFFFFFFFLL);
1503 SBVAL(inbuf.data, 8, 0xFFFFFFFFFFFFFFFFLL);
1504 SBVAL(inbuf.data, 24, 0xFFFFFFFFFFFFFFFFLL);
1506 status = smb2cli_set_info(cli->conn,
1510 1, /* in_info_type */
1511 SMB_FILE_BASIC_INFORMATION - 1000, /* in_file_info_class */
1512 &inbuf, /* in_input_buffer */
1513 0, /* in_additional_info */
1518 if (fnum != 0xffff) {
1519 cli_smb2_close_fnum(cli, fnum);
1526 /***************************************************************
1527 Wrapper that allows SMB2 to set file handle times.
1529 ***************************************************************/
1531 NTSTATUS cli_smb2_setattrE(struct cli_state *cli,
1538 struct smb2_hnd *ph = NULL;
1539 uint8_t inbuf_store[40];
1540 DATA_BLOB inbuf = data_blob_null;
1542 if (smbXcli_conn_has_async_calls(cli->conn)) {
1544 * Can't use sync call while an async call is in flight
1546 return NT_STATUS_INVALID_PARAMETER;
1549 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1550 return NT_STATUS_INVALID_PARAMETER;
1553 status = map_fnum_to_smb2_handle(cli,
1556 if (!NT_STATUS_IS_OK(status)) {
1560 /* setinfo on the handle with info_type SMB2_SETINFO_FILE (1),
1561 level 4 (SMB_FILE_BASIC_INFORMATION - 1000). */
1563 inbuf.data = inbuf_store;
1564 inbuf.length = sizeof(inbuf_store);
1565 data_blob_clear(&inbuf);
1567 SBVAL(inbuf.data, 0, 0xFFFFFFFFFFFFFFFFLL);
1568 if (change_time != 0) {
1569 put_long_date((char *)inbuf.data + 24, change_time);
1571 if (access_time != 0) {
1572 put_long_date((char *)inbuf.data + 8, access_time);
1574 if (write_time != 0) {
1575 put_long_date((char *)inbuf.data + 16, write_time);
1578 return smb2cli_set_info(cli->conn,
1582 1, /* in_info_type */
1583 SMB_FILE_BASIC_INFORMATION - 1000, /* in_file_info_class */
1584 &inbuf, /* in_input_buffer */
1585 0, /* in_additional_info */
1590 /***************************************************************
1591 Wrapper that allows SMB2 to query disk attributes (size).
1593 ***************************************************************/
1595 NTSTATUS cli_smb2_dskattr(struct cli_state *cli, const char *path,
1596 uint64_t *bsize, uint64_t *total, uint64_t *avail)
1599 uint16_t fnum = 0xffff;
1600 DATA_BLOB outbuf = data_blob_null;
1601 struct smb2_hnd *ph = NULL;
1602 uint32_t sectors_per_unit = 0;
1603 uint32_t bytes_per_sector = 0;
1604 uint64_t total_size = 0;
1605 uint64_t size_free = 0;
1606 TALLOC_CTX *frame = talloc_stackframe();
1608 if (smbXcli_conn_has_async_calls(cli->conn)) {
1610 * Can't use sync call while an async call is in flight
1612 status = NT_STATUS_INVALID_PARAMETER;
1616 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1617 status = NT_STATUS_INVALID_PARAMETER;
1621 /* First open the top level directory. */
1622 status = cli_smb2_create_fnum(cli,
1624 0, /* create_flags */
1625 FILE_READ_ATTRIBUTES, /* desired_access */
1626 FILE_ATTRIBUTE_DIRECTORY, /* file attributes */
1627 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, /* share_access */
1628 FILE_OPEN, /* create_disposition */
1629 FILE_DIRECTORY_FILE, /* create_options */
1633 if (!NT_STATUS_IS_OK(status)) {
1637 status = map_fnum_to_smb2_handle(cli,
1640 if (!NT_STATUS_IS_OK(status)) {
1644 /* getinfo on the returned handle with info_type SMB2_GETINFO_FS (2),
1645 level 3 (SMB_FS_SIZE_INFORMATION). */
1647 status = smb2cli_query_info(cli->conn,
1651 2, /* in_info_type */
1652 3, /* in_file_info_class */
1653 0xFFFF, /* in_max_output_length */
1654 NULL, /* in_input_buffer */
1655 0, /* in_additional_info */
1661 if (!NT_STATUS_IS_OK(status)) {
1665 /* Parse the reply. */
1666 if (outbuf.length != 24) {
1667 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
1671 total_size = BVAL(outbuf.data, 0);
1672 size_free = BVAL(outbuf.data, 8);
1673 sectors_per_unit = IVAL(outbuf.data, 16);
1674 bytes_per_sector = IVAL(outbuf.data, 20);
1677 *bsize = (uint64_t)sectors_per_unit * (uint64_t)bytes_per_sector;
1680 *total = total_size;
1686 status = NT_STATUS_OK;
1690 if (fnum != 0xffff) {
1691 cli_smb2_close_fnum(cli, fnum);
1698 /***************************************************************
1699 Wrapper that allows SMB2 to query a security descriptor.
1701 ***************************************************************/
1703 NTSTATUS cli_smb2_query_security_descriptor(struct cli_state *cli,
1706 TALLOC_CTX *mem_ctx,
1707 struct security_descriptor **ppsd)
1710 DATA_BLOB outbuf = data_blob_null;
1711 struct smb2_hnd *ph = NULL;
1712 struct security_descriptor *lsd = NULL;
1713 TALLOC_CTX *frame = talloc_stackframe();
1715 if (smbXcli_conn_has_async_calls(cli->conn)) {
1717 * Can't use sync call while an async call is in flight
1719 status = NT_STATUS_INVALID_PARAMETER;
1723 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1724 status = NT_STATUS_INVALID_PARAMETER;
1728 status = map_fnum_to_smb2_handle(cli,
1731 if (!NT_STATUS_IS_OK(status)) {
1735 /* getinfo on the returned handle with info_type SMB2_GETINFO_SEC (3) */
1737 status = smb2cli_query_info(cli->conn,
1741 3, /* in_info_type */
1742 0, /* in_file_info_class */
1743 0xFFFF, /* in_max_output_length */
1744 NULL, /* in_input_buffer */
1745 sec_info, /* in_additional_info */
1752 if (!NT_STATUS_IS_OK(status)) {
1756 /* Parse the reply. */
1757 status = unmarshall_sec_desc(mem_ctx,
1762 if (!NT_STATUS_IS_OK(status)) {
1778 /***************************************************************
1779 Wrapper that allows SMB2 to set a security descriptor.
1781 ***************************************************************/
1783 NTSTATUS cli_smb2_set_security_descriptor(struct cli_state *cli,
1786 const struct security_descriptor *sd)
1789 DATA_BLOB inbuf = data_blob_null;
1790 struct smb2_hnd *ph = NULL;
1791 TALLOC_CTX *frame = talloc_stackframe();
1793 if (smbXcli_conn_has_async_calls(cli->conn)) {
1795 * Can't use sync call while an async call is in flight
1797 status = NT_STATUS_INVALID_PARAMETER;
1801 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1802 status = NT_STATUS_INVALID_PARAMETER;
1806 status = map_fnum_to_smb2_handle(cli,
1809 if (!NT_STATUS_IS_OK(status)) {
1813 status = marshall_sec_desc(frame,
1818 if (!NT_STATUS_IS_OK(status)) {
1822 /* setinfo on the returned handle with info_type SMB2_SETINFO_SEC (3) */
1824 status = smb2cli_set_info(cli->conn,
1828 3, /* in_info_type */
1829 0, /* in_file_info_class */
1830 &inbuf, /* in_input_buffer */
1831 sec_info, /* in_additional_info */
1841 /***************************************************************
1842 Wrapper that allows SMB2 to rename a file.
1844 ***************************************************************/
1846 NTSTATUS cli_smb2_rename(struct cli_state *cli,
1847 const char *fname_src,
1848 const char *fname_dst)
1851 DATA_BLOB inbuf = data_blob_null;
1852 uint16_t fnum = 0xffff;
1853 struct smb2_hnd *ph = NULL;
1854 smb_ucs2_t *converted_str = NULL;
1855 size_t converted_size_bytes = 0;
1857 TALLOC_CTX *frame = talloc_stackframe();
1859 if (smbXcli_conn_has_async_calls(cli->conn)) {
1861 * Can't use sync call while an async call is in flight
1863 status = NT_STATUS_INVALID_PARAMETER;
1867 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1868 status = NT_STATUS_INVALID_PARAMETER;
1872 status = get_fnum_from_path(cli,
1877 if (!NT_STATUS_IS_OK(status)) {
1881 status = map_fnum_to_smb2_handle(cli,
1884 if (!NT_STATUS_IS_OK(status)) {
1888 /* SMB2 is pickier about pathnames. Ensure it doesn't
1890 if (*fname_dst == '\\') {
1894 /* SMB2 is pickier about pathnames. Ensure it doesn't
1896 namelen = strlen(fname_dst);
1897 if (namelen > 0 && fname_dst[namelen-1] == '\\') {
1898 char *modname = talloc_strdup(frame, fname_dst);
1899 modname[namelen-1] = '\0';
1900 fname_dst = modname;
1903 if (!push_ucs2_talloc(frame,
1906 &converted_size_bytes)) {
1907 status = NT_STATUS_INVALID_PARAMETER;
1911 /* W2K8 insists the dest name is not null
1912 terminated. Remove the last 2 zero bytes
1913 and reduce the name length. */
1915 if (converted_size_bytes < 2) {
1916 status = NT_STATUS_INVALID_PARAMETER;
1919 converted_size_bytes -= 2;
1921 inbuf = data_blob_talloc_zero(frame,
1922 20 + converted_size_bytes);
1923 if (inbuf.data == NULL) {
1924 status = NT_STATUS_NO_MEMORY;
1928 SIVAL(inbuf.data, 16, converted_size_bytes);
1929 memcpy(inbuf.data + 20, converted_str, converted_size_bytes);
1931 /* setinfo on the returned handle with info_type SMB2_GETINFO_FILE (1),
1932 level SMB2_FILE_RENAME_INFORMATION (SMB_FILE_RENAME_INFORMATION - 1000) */
1934 status = smb2cli_set_info(cli->conn,
1938 1, /* in_info_type */
1939 SMB_FILE_RENAME_INFORMATION - 1000, /* in_file_info_class */
1940 &inbuf, /* in_input_buffer */
1941 0, /* in_additional_info */
1947 if (fnum != 0xffff) {
1948 cli_smb2_close_fnum(cli, fnum);
1955 /***************************************************************
1956 Wrapper that allows SMB2 to set an EA on a fnum.
1958 ***************************************************************/
1960 NTSTATUS cli_smb2_set_ea_fnum(struct cli_state *cli,
1962 const char *ea_name,
1967 DATA_BLOB inbuf = data_blob_null;
1969 char *ea_name_ascii = NULL;
1971 struct smb2_hnd *ph = NULL;
1972 TALLOC_CTX *frame = talloc_stackframe();
1974 if (smbXcli_conn_has_async_calls(cli->conn)) {
1976 * Can't use sync call while an async call is in flight
1978 status = NT_STATUS_INVALID_PARAMETER;
1982 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
1983 status = NT_STATUS_INVALID_PARAMETER;
1987 status = map_fnum_to_smb2_handle(cli,
1990 if (!NT_STATUS_IS_OK(status)) {
1994 /* Marshall the SMB2 EA data. */
1995 if (ea_len > 0xFFFF) {
1996 status = NT_STATUS_INVALID_PARAMETER;
2000 if (!push_ascii_talloc(frame,
2004 status = NT_STATUS_INVALID_PARAMETER;
2008 if (namelen < 2 || namelen > 0xFF) {
2009 status = NT_STATUS_INVALID_PARAMETER;
2013 bloblen = 8 + ea_len + namelen;
2014 /* Round up to a 4 byte boundary. */
2015 bloblen = ((bloblen + 3)&~3);
2017 inbuf = data_blob_talloc_zero(frame, bloblen);
2018 if (inbuf.data == NULL) {
2019 status = NT_STATUS_NO_MEMORY;
2022 /* namelen doesn't include the NULL byte. */
2023 SCVAL(inbuf.data, 5, namelen - 1);
2024 SSVAL(inbuf.data, 6, ea_len);
2025 memcpy(inbuf.data + 8, ea_name_ascii, namelen);
2026 memcpy(inbuf.data + 8 + namelen, ea_val, ea_len);
2028 /* setinfo on the handle with info_type SMB2_SETINFO_FILE (1),
2029 level 15 (SMB_FILE_FULL_EA_INFORMATION - 1000). */
2031 status = smb2cli_set_info(cli->conn,
2035 1, /* in_info_type */
2036 SMB_FILE_FULL_EA_INFORMATION - 1000, /* in_file_info_class */
2037 &inbuf, /* in_input_buffer */
2038 0, /* in_additional_info */
2048 /***************************************************************
2049 Wrapper that allows SMB2 to set an EA on a pathname.
2051 ***************************************************************/
2053 NTSTATUS cli_smb2_set_ea_path(struct cli_state *cli,
2055 const char *ea_name,
2060 uint16_t fnum = 0xffff;
2062 if (smbXcli_conn_has_async_calls(cli->conn)) {
2064 * Can't use sync call while an async call is in flight
2066 status = NT_STATUS_INVALID_PARAMETER;
2070 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
2071 status = NT_STATUS_INVALID_PARAMETER;
2075 status = get_fnum_from_path(cli,
2080 if (!NT_STATUS_IS_OK(status)) {
2084 status = cli_set_ea_fnum(cli,
2089 if (!NT_STATUS_IS_OK(status)) {
2095 if (fnum != 0xffff) {
2096 cli_smb2_close_fnum(cli, fnum);
2102 /***************************************************************
2103 Wrapper that allows SMB2 to get an EA list on a pathname.
2105 ***************************************************************/
2107 NTSTATUS cli_smb2_get_ea_list_path(struct cli_state *cli,
2111 struct ea_struct **pea_array)
2114 uint16_t fnum = 0xffff;
2115 DATA_BLOB outbuf = data_blob_null;
2116 struct smb2_hnd *ph = NULL;
2117 struct ea_list *ea_list = NULL;
2118 struct ea_list *eal = NULL;
2119 size_t ea_count = 0;
2120 TALLOC_CTX *frame = talloc_stackframe();
2125 if (smbXcli_conn_has_async_calls(cli->conn)) {
2127 * Can't use sync call while an async call is in flight
2129 status = NT_STATUS_INVALID_PARAMETER;
2133 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
2134 status = NT_STATUS_INVALID_PARAMETER;
2138 status = get_fnum_from_path(cli,
2143 if (!NT_STATUS_IS_OK(status)) {
2147 status = map_fnum_to_smb2_handle(cli,
2150 if (!NT_STATUS_IS_OK(status)) {
2154 /* getinfo on the handle with info_type SMB2_GETINFO_FILE (1),
2155 level 15 (SMB_FILE_FULL_EA_INFORMATION - 1000). */
2157 status = smb2cli_query_info(cli->conn,
2161 1, /* in_info_type */
2162 SMB_FILE_FULL_EA_INFORMATION - 1000, /* in_file_info_class */
2163 0xFFFF, /* in_max_output_length */
2164 NULL, /* in_input_buffer */
2165 0, /* in_additional_info */
2172 if (!NT_STATUS_IS_OK(status)) {
2176 /* Parse the reply. */
2177 ea_list = read_nttrans_ea_list(ctx,
2178 (const char *)outbuf.data,
2180 if (ea_list == NULL) {
2181 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
2185 /* Convert to an array. */
2186 for (eal = ea_list; eal; eal = eal->next) {
2191 *pea_array = talloc_array(ctx, struct ea_struct, ea_count);
2192 if (*pea_array == NULL) {
2193 status = NT_STATUS_NO_MEMORY;
2197 for (eal = ea_list; eal; eal = eal->next) {
2198 (*pea_array)[ea_count++] = eal->ea;
2200 *pnum_eas = ea_count;
2205 if (fnum != 0xffff) {
2206 cli_smb2_close_fnum(cli, fnum);
2213 struct cli_smb2_read_state {
2214 struct tevent_context *ev;
2215 struct cli_state *cli;
2216 struct smb2_hnd *ph;
2217 uint64_t start_offset;
2223 static void cli_smb2_read_done(struct tevent_req *subreq);
2225 struct tevent_req *cli_smb2_read_send(TALLOC_CTX *mem_ctx,
2226 struct tevent_context *ev,
2227 struct cli_state *cli,
2233 struct tevent_req *req, *subreq;
2234 struct cli_smb2_read_state *state;
2236 req = tevent_req_create(mem_ctx, &state, struct cli_smb2_read_state);
2242 state->start_offset = (uint64_t)offset;
2243 state->size = (uint32_t)size;
2244 state->received = 0;
2247 status = map_fnum_to_smb2_handle(cli,
2250 if (tevent_req_nterror(req, status)) {
2251 return tevent_req_post(req, ev);
2254 subreq = smb2cli_read_send(state,
2257 state->cli->timeout,
2258 state->cli->smb2.session,
2259 state->cli->smb2.tcon,
2261 state->start_offset,
2262 state->ph->fid_persistent,
2263 state->ph->fid_volatile,
2264 0, /* minimum_count */
2265 0); /* remaining_bytes */
2267 if (tevent_req_nomem(subreq, req)) {
2268 return tevent_req_post(req, ev);
2270 tevent_req_set_callback(subreq, cli_smb2_read_done, req);
2274 static void cli_smb2_read_done(struct tevent_req *subreq)
2276 struct tevent_req *req = tevent_req_callback_data(
2277 subreq, struct tevent_req);
2278 struct cli_smb2_read_state *state = tevent_req_data(
2279 req, struct cli_smb2_read_state);
2282 status = smb2cli_read_recv(subreq, state,
2283 &state->buf, &state->received);
2284 if (tevent_req_nterror(req, status)) {
2288 if (state->received > state->size) {
2289 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
2293 tevent_req_done(req);
2296 NTSTATUS cli_smb2_read_recv(struct tevent_req *req,
2301 struct cli_smb2_read_state *state = tevent_req_data(
2302 req, struct cli_smb2_read_state);
2304 if (tevent_req_is_nterror(req, &status)) {
2308 * As in cli_read_andx_recv() rcvbuf is talloced from the request, so
2309 * better make sure that you copy it away before you talloc_free(req).
2310 * "rcvbuf" is NOT a talloc_ctx of its own, so do not talloc_move it!
2312 *received = (ssize_t)state->received;
2313 *rcvbuf = state->buf;
2314 return NT_STATUS_OK;
2317 struct cli_smb2_write_state {
2318 struct tevent_context *ev;
2319 struct cli_state *cli;
2320 struct smb2_hnd *ph;
2328 static void cli_smb2_write_written(struct tevent_req *req);
2330 struct tevent_req *cli_smb2_write_send(TALLOC_CTX *mem_ctx,
2331 struct tevent_context *ev,
2332 struct cli_state *cli,
2340 struct tevent_req *req, *subreq = NULL;
2341 struct cli_smb2_write_state *state = NULL;
2343 req = tevent_req_create(mem_ctx, &state, struct cli_smb2_write_state);
2349 /* Both SMB1 and SMB2 use 1 in the following meaning write-through. */
2350 state->flags = (uint32_t)mode;
2352 state->offset = (uint64_t)offset;
2353 state->size = (uint32_t)size;
2356 status = map_fnum_to_smb2_handle(cli,
2359 if (tevent_req_nterror(req, status)) {
2360 return tevent_req_post(req, ev);
2363 subreq = smb2cli_write_send(state,
2366 state->cli->timeout,
2367 state->cli->smb2.session,
2368 state->cli->smb2.tcon,
2371 state->ph->fid_persistent,
2372 state->ph->fid_volatile,
2373 0, /* remaining_bytes */
2374 state->flags, /* flags */
2377 if (tevent_req_nomem(subreq, req)) {
2378 return tevent_req_post(req, ev);
2380 tevent_req_set_callback(subreq, cli_smb2_write_written, req);
2384 static void cli_smb2_write_written(struct tevent_req *subreq)
2386 struct tevent_req *req = tevent_req_callback_data(
2387 subreq, struct tevent_req);
2388 struct cli_smb2_write_state *state = tevent_req_data(
2389 req, struct cli_smb2_write_state);
2393 status = smb2cli_write_recv(subreq, &written);
2394 TALLOC_FREE(subreq);
2395 if (tevent_req_nterror(req, status)) {
2399 state->written = written;
2401 tevent_req_done(req);
2404 NTSTATUS cli_smb2_write_recv(struct tevent_req *req,
2407 struct cli_smb2_write_state *state = tevent_req_data(
2408 req, struct cli_smb2_write_state);
2411 if (tevent_req_is_nterror(req, &status)) {
2412 tevent_req_received(req);
2416 if (pwritten != NULL) {
2417 *pwritten = (size_t)state->written;
2419 tevent_req_received(req);
2420 return NT_STATUS_OK;
2423 /***************************************************************
2424 Wrapper that allows SMB2 async write using an fnum.
2425 This is mostly cut-and-paste from Volker's code inside
2426 source3/libsmb/clireadwrite.c, adapted for SMB2.
2428 Done this way so I can reuse all the logic inside cli_push()
2430 ***************************************************************/
2432 struct cli_smb2_writeall_state {
2433 struct tevent_context *ev;
2434 struct cli_state *cli;
2435 struct smb2_hnd *ph;
2443 static void cli_smb2_writeall_written(struct tevent_req *req);
2445 struct tevent_req *cli_smb2_writeall_send(TALLOC_CTX *mem_ctx,
2446 struct tevent_context *ev,
2447 struct cli_state *cli,
2455 struct tevent_req *req, *subreq = NULL;
2456 struct cli_smb2_writeall_state *state = NULL;
2461 req = tevent_req_create(mem_ctx, &state, struct cli_smb2_writeall_state);
2467 /* Both SMB1 and SMB2 use 1 in the following meaning write-through. */
2468 state->flags = (uint32_t)mode;
2470 state->offset = (uint64_t)offset;
2471 state->size = (uint32_t)size;
2474 status = map_fnum_to_smb2_handle(cli,
2477 if (tevent_req_nterror(req, status)) {
2478 return tevent_req_post(req, ev);
2481 to_write = state->size;
2482 max_size = smb2cli_conn_max_write_size(state->cli->conn);
2483 to_write = MIN(max_size, to_write);
2484 ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
2486 to_write = MIN(max_size, to_write);
2489 subreq = smb2cli_write_send(state,
2492 state->cli->timeout,
2493 state->cli->smb2.session,
2494 state->cli->smb2.tcon,
2497 state->ph->fid_persistent,
2498 state->ph->fid_volatile,
2499 0, /* remaining_bytes */
2500 state->flags, /* flags */
2501 state->buf + state->written);
2503 if (tevent_req_nomem(subreq, req)) {
2504 return tevent_req_post(req, ev);
2506 tevent_req_set_callback(subreq, cli_smb2_writeall_written, req);
2510 static void cli_smb2_writeall_written(struct tevent_req *subreq)
2512 struct tevent_req *req = tevent_req_callback_data(
2513 subreq, struct tevent_req);
2514 struct cli_smb2_writeall_state *state = tevent_req_data(
2515 req, struct cli_smb2_writeall_state);
2517 uint32_t written, to_write;
2521 status = smb2cli_write_recv(subreq, &written);
2522 TALLOC_FREE(subreq);
2523 if (tevent_req_nterror(req, status)) {
2527 state->written += written;
2529 if (state->written > state->size) {
2530 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
2534 to_write = state->size - state->written;
2536 if (to_write == 0) {
2537 tevent_req_done(req);
2541 max_size = smb2cli_conn_max_write_size(state->cli->conn);
2542 to_write = MIN(max_size, to_write);
2543 ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
2545 to_write = MIN(max_size, to_write);
2548 subreq = smb2cli_write_send(state,
2551 state->cli->timeout,
2552 state->cli->smb2.session,
2553 state->cli->smb2.tcon,
2555 state->offset + state->written,
2556 state->ph->fid_persistent,
2557 state->ph->fid_volatile,
2558 0, /* remaining_bytes */
2559 state->flags, /* flags */
2560 state->buf + state->written);
2562 if (tevent_req_nomem(subreq, req)) {
2565 tevent_req_set_callback(subreq, cli_smb2_writeall_written, req);
2568 NTSTATUS cli_smb2_writeall_recv(struct tevent_req *req,
2571 struct cli_smb2_writeall_state *state = tevent_req_data(
2572 req, struct cli_smb2_writeall_state);
2575 if (tevent_req_is_nterror(req, &status)) {
2578 if (pwritten != NULL) {
2579 *pwritten = (size_t)state->written;
2581 return NT_STATUS_OK;
2584 struct cli_smb2_splice_state {
2585 struct tevent_context *ev;
2586 struct cli_state *cli;
2587 struct smb2_hnd *src_ph;
2588 struct smb2_hnd *dst_ph;
2589 int (*splice_cb)(off_t n, void *priv);
2596 struct req_resume_key_rsp resume_rsp;
2597 struct srv_copychunk_copy cc_copy;
2600 static void cli_splice_copychunk_send(struct cli_smb2_splice_state *state,
2601 struct tevent_req *req);
2603 static void cli_splice_copychunk_done(struct tevent_req *subreq)
2605 struct tevent_req *req = tevent_req_callback_data(
2606 subreq, struct tevent_req);
2607 struct cli_smb2_splice_state *state =
2608 tevent_req_data(req,
2609 struct cli_smb2_splice_state);
2610 struct smbXcli_conn *conn = state->cli->conn;
2611 DATA_BLOB out_input_buffer = data_blob_null;
2612 DATA_BLOB out_output_buffer = data_blob_null;
2613 struct srv_copychunk_rsp cc_copy_rsp;
2614 enum ndr_err_code ndr_ret;
2617 status = smb2cli_ioctl_recv(subreq, state,
2619 &out_output_buffer);
2620 TALLOC_FREE(subreq);
2621 if ((!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) ||
2622 state->resized) && tevent_req_nterror(req, status)) {
2626 ndr_ret = ndr_pull_struct_blob(&out_output_buffer, state, &cc_copy_rsp,
2627 (ndr_pull_flags_fn_t)ndr_pull_srv_copychunk_rsp);
2628 if (ndr_ret != NDR_ERR_SUCCESS) {
2629 DEBUG(0, ("failed to unmarshall copy chunk rsp\n"));
2630 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
2634 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2635 uint32_t max_chunks = MIN(cc_copy_rsp.chunks_written,
2636 cc_copy_rsp.total_bytes_written / cc_copy_rsp.chunk_bytes_written);
2637 if ((cc_copy_rsp.chunk_bytes_written > smb2cli_conn_cc_chunk_len(conn) ||
2638 max_chunks > smb2cli_conn_cc_max_chunks(conn)) &&
2639 tevent_req_nterror(req, status)) {
2643 state->resized = true;
2644 smb2cli_conn_set_cc_chunk_len(conn, cc_copy_rsp.chunk_bytes_written);
2645 smb2cli_conn_set_cc_max_chunks(conn, max_chunks);
2647 if ((state->src_offset > INT64_MAX - cc_copy_rsp.total_bytes_written) ||
2648 (state->dst_offset > INT64_MAX - cc_copy_rsp.total_bytes_written) ||
2649 (state->written > INT64_MAX - cc_copy_rsp.total_bytes_written)) {
2650 tevent_req_nterror(req, NT_STATUS_FILE_TOO_LARGE);
2653 state->src_offset += cc_copy_rsp.total_bytes_written;
2654 state->dst_offset += cc_copy_rsp.total_bytes_written;
2655 state->written += cc_copy_rsp.total_bytes_written;
2656 if (!state->splice_cb(state->written, state->priv)) {
2657 tevent_req_nterror(req, NT_STATUS_CANCELLED);
2662 cli_splice_copychunk_send(state, req);
2665 static void cli_splice_copychunk_send(struct cli_smb2_splice_state *state,
2666 struct tevent_req *req)
2668 struct tevent_req *subreq;
2669 enum ndr_err_code ndr_ret;
2670 struct smbXcli_conn *conn = state->cli->conn;
2671 struct srv_copychunk_copy *cc_copy = &state->cc_copy;
2672 off_t src_offset = state->src_offset;
2673 off_t dst_offset = state->dst_offset;
2674 uint32_t req_len = MIN(smb2cli_conn_cc_chunk_len(conn) * smb2cli_conn_cc_max_chunks(conn),
2675 state->size - state->written);
2676 DATA_BLOB in_input_buffer = data_blob_null;
2677 DATA_BLOB in_output_buffer = data_blob_null;
2679 if (state->size - state->written == 0) {
2680 tevent_req_done(req);
2684 cc_copy->chunk_count = 0;
2686 cc_copy->chunks[cc_copy->chunk_count].source_off = src_offset;
2687 cc_copy->chunks[cc_copy->chunk_count].target_off = dst_offset;
2688 cc_copy->chunks[cc_copy->chunk_count].length = MIN(req_len,
2689 smb2cli_conn_cc_chunk_len(conn));
2690 if (req_len < cc_copy->chunks[cc_copy->chunk_count].length) {
2691 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
2694 req_len -= cc_copy->chunks[cc_copy->chunk_count].length;
2695 if ((src_offset > INT64_MAX - cc_copy->chunks[cc_copy->chunk_count].length) ||
2696 (dst_offset > INT64_MAX - cc_copy->chunks[cc_copy->chunk_count].length)) {
2697 tevent_req_nterror(req, NT_STATUS_FILE_TOO_LARGE);
2700 src_offset += cc_copy->chunks[cc_copy->chunk_count].length;
2701 dst_offset += cc_copy->chunks[cc_copy->chunk_count].length;
2702 cc_copy->chunk_count++;
2705 ndr_ret = ndr_push_struct_blob(&in_input_buffer, state, cc_copy,
2706 (ndr_push_flags_fn_t)ndr_push_srv_copychunk_copy);
2707 if (ndr_ret != NDR_ERR_SUCCESS) {
2708 DEBUG(0, ("failed to marshall copy chunk req\n"));
2709 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
2713 subreq = smb2cli_ioctl_send(state, state->ev, state->cli->conn,
2714 state->cli->timeout,
2715 state->cli->smb2.session,
2716 state->cli->smb2.tcon,
2717 state->dst_ph->fid_persistent, /* in_fid_persistent */
2718 state->dst_ph->fid_volatile, /* in_fid_volatile */
2719 FSCTL_SRV_COPYCHUNK_WRITE,
2720 0, /* in_max_input_length */
2722 12, /* in_max_output_length */
2724 SMB2_IOCTL_FLAG_IS_FSCTL);
2725 if (tevent_req_nomem(subreq, req)) {
2728 tevent_req_set_callback(subreq,
2729 cli_splice_copychunk_done,
2733 static void cli_splice_key_done(struct tevent_req *subreq)
2735 struct tevent_req *req = tevent_req_callback_data(
2736 subreq, struct tevent_req);
2737 struct cli_smb2_splice_state *state =
2738 tevent_req_data(req,
2739 struct cli_smb2_splice_state);
2740 enum ndr_err_code ndr_ret;
2743 DATA_BLOB out_input_buffer = data_blob_null;
2744 DATA_BLOB out_output_buffer = data_blob_null;
2746 status = smb2cli_ioctl_recv(subreq, state,
2748 &out_output_buffer);
2749 TALLOC_FREE(subreq);
2750 if (tevent_req_nterror(req, status)) {
2754 ndr_ret = ndr_pull_struct_blob(&out_output_buffer,
2755 state, &state->resume_rsp,
2756 (ndr_pull_flags_fn_t)ndr_pull_req_resume_key_rsp);
2757 if (ndr_ret != NDR_ERR_SUCCESS) {
2758 DEBUG(0, ("failed to unmarshall resume key rsp\n"));
2759 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
2763 memcpy(&state->cc_copy.source_key,
2764 &state->resume_rsp.resume_key,
2765 sizeof state->resume_rsp.resume_key);
2767 cli_splice_copychunk_send(state, req);
2770 struct tevent_req *cli_smb2_splice_send(TALLOC_CTX *mem_ctx,
2771 struct tevent_context *ev,
2772 struct cli_state *cli,
2773 uint16_t src_fnum, uint16_t dst_fnum,
2774 off_t size, off_t src_offset, off_t dst_offset,
2775 int (*splice_cb)(off_t n, void *priv),
2778 struct tevent_req *req;
2779 struct tevent_req *subreq;
2780 struct cli_smb2_splice_state *state;
2782 DATA_BLOB in_input_buffer = data_blob_null;
2783 DATA_BLOB in_output_buffer = data_blob_null;
2785 req = tevent_req_create(mem_ctx, &state, struct cli_smb2_splice_state);
2791 state->splice_cb = splice_cb;
2795 state->src_offset = src_offset;
2796 state->dst_offset = dst_offset;
2797 state->cc_copy.chunks = talloc_array(state,
2798 struct srv_copychunk,
2799 smb2cli_conn_cc_max_chunks(cli->conn));
2800 if (state->cc_copy.chunks == NULL) {
2804 status = map_fnum_to_smb2_handle(cli, src_fnum, &state->src_ph);
2805 if (tevent_req_nterror(req, status))
2806 return tevent_req_post(req, ev);
2808 status = map_fnum_to_smb2_handle(cli, dst_fnum, &state->dst_ph);
2809 if (tevent_req_nterror(req, status))
2810 return tevent_req_post(req, ev);
2812 subreq = smb2cli_ioctl_send(state, ev, cli->conn,
2816 state->src_ph->fid_persistent, /* in_fid_persistent */
2817 state->src_ph->fid_volatile, /* in_fid_volatile */
2818 FSCTL_SRV_REQUEST_RESUME_KEY,
2819 0, /* in_max_input_length */
2821 32, /* in_max_output_length */
2823 SMB2_IOCTL_FLAG_IS_FSCTL);
2824 if (tevent_req_nomem(subreq, req)) {
2827 tevent_req_set_callback(subreq,
2828 cli_splice_key_done,
2834 NTSTATUS cli_smb2_splice_recv(struct tevent_req *req, off_t *written)
2836 struct cli_smb2_splice_state *state = tevent_req_data(
2837 req, struct cli_smb2_splice_state);
2840 if (tevent_req_is_nterror(req, &status)) {
2841 tevent_req_received(req);
2844 if (written != NULL) {
2845 *written = state->written;
2847 tevent_req_received(req);
2848 return NT_STATUS_OK;