ldb_kv: Use ldb_msg_add_steal_value() in msg_add_distinguished_name()
[Samba.git] / source3 / libsmb / clifile.c
blobb0d023952736324080d96f639927912c88871e6c
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
27 #include "trans2.h"
28 #include "ntioctl.h"
29 #include "libcli/security/security.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 struct cli_setpathinfo_state {
33 uint16_t setup;
34 uint8_t *param;
37 static void cli_setpathinfo_done(struct tevent_req *subreq);
39 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
40 struct tevent_context *ev,
41 struct cli_state *cli,
42 uint16_t level,
43 const char *path,
44 uint8_t *data,
45 size_t data_len)
47 struct tevent_req *req, *subreq;
48 struct cli_setpathinfo_state *state;
49 uint16_t additional_flags2 = 0;
51 req = tevent_req_create(mem_ctx, &state,
52 struct cli_setpathinfo_state);
53 if (req == NULL) {
54 return NULL;
57 /* Setup setup word. */
58 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
60 /* Setup param array. */
61 state->param = talloc_zero_array(state, uint8_t, 6);
62 if (tevent_req_nomem(state->param, req)) {
63 return tevent_req_post(req, ev);
65 SSVAL(state->param, 0, level);
67 state->param = trans2_bytes_push_str(
68 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
69 if (tevent_req_nomem(state->param, req)) {
70 return tevent_req_post(req, ev);
73 if (clistr_is_previous_version_path(path, NULL, NULL, NULL) &&
74 !INFO_LEVEL_IS_UNIX(level)) {
75 additional_flags2 = FLAGS2_REPARSE_PATH;
78 subreq = cli_trans_send(
79 state, /* mem ctx. */
80 ev, /* event ctx. */
81 cli, /* cli_state. */
82 additional_flags2, /* additional_flags2 */
83 SMBtrans2, /* cmd. */
84 NULL, /* pipe name. */
85 -1, /* fid. */
86 0, /* function. */
87 0, /* flags. */
88 &state->setup, /* setup. */
89 1, /* num setup uint16_t words. */
90 0, /* max returned setup. */
91 state->param, /* param. */
92 talloc_get_size(state->param), /* num param. */
93 2, /* max returned param. */
94 data, /* data. */
95 data_len, /* num data. */
96 0); /* max returned data. */
98 if (tevent_req_nomem(subreq, req)) {
99 return tevent_req_post(req, ev);
101 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
102 return req;
105 static void cli_setpathinfo_done(struct tevent_req *subreq)
107 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
108 NULL, 0, NULL, NULL, 0, NULL);
109 tevent_req_simple_finish_ntstatus(subreq, status);
112 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
114 return tevent_req_simple_recv_ntstatus(req);
117 NTSTATUS cli_setpathinfo(struct cli_state *cli,
118 uint16_t level,
119 const char *path,
120 uint8_t *data,
121 size_t data_len)
123 TALLOC_CTX *frame = talloc_stackframe();
124 struct tevent_context *ev;
125 struct tevent_req *req;
126 NTSTATUS status = NT_STATUS_NO_MEMORY;
128 if (smbXcli_conn_has_async_calls(cli->conn)) {
130 * Can't use sync call while an async call is in flight
132 status = NT_STATUS_INVALID_PARAMETER;
133 goto fail;
135 ev = samba_tevent_context_init(frame);
136 if (ev == NULL) {
137 goto fail;
139 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
140 if (req == NULL) {
141 goto fail;
143 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
144 goto fail;
146 status = cli_setpathinfo_recv(req);
147 fail:
148 TALLOC_FREE(frame);
149 return status;
152 /****************************************************************************
153 Hard/Symlink a file (UNIX extensions).
154 Creates new name (sym)linked to link_target.
155 ****************************************************************************/
157 struct cli_posix_link_internal_state {
158 uint8_t *data;
161 static void cli_posix_link_internal_done(struct tevent_req *subreq);
163 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
164 struct tevent_context *ev,
165 struct cli_state *cli,
166 uint16_t level,
167 const char *link_target,
168 const char *newname)
170 struct tevent_req *req = NULL, *subreq = NULL;
171 struct cli_posix_link_internal_state *state = NULL;
173 req = tevent_req_create(mem_ctx, &state,
174 struct cli_posix_link_internal_state);
175 if (req == NULL) {
176 return NULL;
179 /* Setup data array. */
180 state->data = talloc_array(state, uint8_t, 0);
181 if (tevent_req_nomem(state->data, req)) {
182 return tevent_req_post(req, ev);
184 state->data = trans2_bytes_push_str(
185 state->data, smbXcli_conn_use_unicode(cli->conn),
186 link_target, strlen(link_target)+1, NULL);
188 subreq = cli_setpathinfo_send(
189 state, ev, cli, level, newname,
190 state->data, talloc_get_size(state->data));
191 if (tevent_req_nomem(subreq, req)) {
192 return tevent_req_post(req, ev);
194 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
195 return req;
198 static void cli_posix_link_internal_done(struct tevent_req *subreq)
200 NTSTATUS status = cli_setpathinfo_recv(subreq);
201 tevent_req_simple_finish_ntstatus(subreq, status);
204 static NTSTATUS cli_posix_link_internal_recv(struct tevent_req *req)
206 return tevent_req_simple_recv_ntstatus(req);
209 /****************************************************************************
210 Symlink a file (UNIX extensions).
211 ****************************************************************************/
213 struct cli_posix_symlink_state {
214 uint8_t dummy;
217 static void cli_posix_symlink_done(struct tevent_req *subreq);
219 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
220 struct tevent_context *ev,
221 struct cli_state *cli,
222 const char *link_target,
223 const char *newname)
225 struct tevent_req *req = NULL, *subreq = NULL;
226 struct cli_posix_symlink_state *state = NULL;
228 req = tevent_req_create(
229 mem_ctx, &state, struct cli_posix_symlink_state);
230 if (req == NULL) {
231 return NULL;
234 subreq = cli_posix_link_internal_send(
235 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, link_target, newname);
236 if (tevent_req_nomem(subreq, req)) {
237 return tevent_req_post(req, ev);
239 tevent_req_set_callback(subreq, cli_posix_symlink_done, req);
240 return req;
243 static void cli_posix_symlink_done(struct tevent_req *subreq)
245 NTSTATUS status = cli_posix_link_internal_recv(subreq);
246 tevent_req_simple_finish_ntstatus(subreq, status);
249 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
251 return tevent_req_simple_recv_ntstatus(req);
254 NTSTATUS cli_posix_symlink(struct cli_state *cli,
255 const char *link_target,
256 const char *newname)
258 TALLOC_CTX *frame = talloc_stackframe();
259 struct tevent_context *ev = NULL;
260 struct tevent_req *req = NULL;
261 NTSTATUS status = NT_STATUS_OK;
263 if (smbXcli_conn_has_async_calls(cli->conn)) {
265 * Can't use sync call while an async call is in flight
267 status = NT_STATUS_INVALID_PARAMETER;
268 goto fail;
271 ev = samba_tevent_context_init(frame);
272 if (ev == NULL) {
273 status = NT_STATUS_NO_MEMORY;
274 goto fail;
277 req = cli_posix_symlink_send(frame,
279 cli,
280 link_target,
281 newname);
282 if (req == NULL) {
283 status = NT_STATUS_NO_MEMORY;
284 goto fail;
287 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
288 goto fail;
291 status = cli_posix_symlink_recv(req);
293 fail:
294 TALLOC_FREE(frame);
295 return status;
298 /****************************************************************************
299 Read a POSIX symlink.
300 ****************************************************************************/
302 struct cli_posix_readlink_state {
303 struct cli_state *cli;
304 char *converted;
307 static void cli_posix_readlink_done(struct tevent_req *subreq);
309 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
310 struct tevent_context *ev,
311 struct cli_state *cli,
312 const char *fname)
314 struct tevent_req *req = NULL, *subreq = NULL;
315 struct cli_posix_readlink_state *state = NULL;
317 req = tevent_req_create(
318 mem_ctx, &state, struct cli_posix_readlink_state);
319 if (req == NULL) {
320 return NULL;
322 state->cli = cli;
324 subreq = cli_qpathinfo_send(
325 state,
327 cli,
328 fname,
329 SMB_QUERY_FILE_UNIX_LINK,
331 UINT16_MAX);
332 if (tevent_req_nomem(subreq, req)) {
333 return tevent_req_post(req, ev);
335 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
336 return req;
339 static void cli_posix_readlink_done(struct tevent_req *subreq)
341 struct tevent_req *req = tevent_req_callback_data(
342 subreq, struct tevent_req);
343 struct cli_posix_readlink_state *state = tevent_req_data(
344 req, struct cli_posix_readlink_state);
345 NTSTATUS status;
346 uint8_t *data = NULL;
347 uint32_t num_data;
348 charset_t charset;
349 size_t converted_size;
350 bool ok;
352 status = cli_qpathinfo_recv(subreq, state, &data, &num_data);
353 TALLOC_FREE(subreq);
354 if (tevent_req_nterror(req, status)) {
355 return;
358 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
360 if (data[num_data-1] != '\0') {
361 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
362 return;
365 charset = smbXcli_conn_use_unicode(state->cli->conn) ?
366 CH_UTF16LE : CH_DOS;
368 /* The returned data is a pushed string, not raw data. */
369 ok = convert_string_talloc(
370 state,
371 charset,
372 CH_UNIX,
373 data,
374 num_data,
375 &state->converted,
376 &converted_size);
377 if (!ok) {
378 tevent_req_oom(req);
379 return;
381 tevent_req_done(req);
384 NTSTATUS cli_posix_readlink_recv(
385 struct tevent_req *req, TALLOC_CTX *mem_ctx, char **target)
387 struct cli_posix_readlink_state *state = tevent_req_data(
388 req, struct cli_posix_readlink_state);
389 NTSTATUS status;
391 if (tevent_req_is_nterror(req, &status)) {
392 return status;
394 *target = talloc_move(mem_ctx, &state->converted);
395 return NT_STATUS_OK;
398 NTSTATUS cli_posix_readlink(
399 struct cli_state *cli,
400 const char *fname,
401 TALLOC_CTX *mem_ctx,
402 char **target)
404 TALLOC_CTX *frame = talloc_stackframe();
405 struct tevent_context *ev = NULL;
406 struct tevent_req *req = NULL;
407 NTSTATUS status = NT_STATUS_OK;
409 if (smbXcli_conn_has_async_calls(cli->conn)) {
411 * Can't use sync call while an async call is in flight
413 status = NT_STATUS_INVALID_PARAMETER;
414 goto fail;
417 ev = samba_tevent_context_init(frame);
418 if (ev == NULL) {
419 status = NT_STATUS_NO_MEMORY;
420 goto fail;
423 req = cli_posix_readlink_send(frame, ev, cli, fname);
424 if (req == NULL) {
425 status = NT_STATUS_NO_MEMORY;
426 goto fail;
429 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
430 goto fail;
433 status = cli_posix_readlink_recv(req, mem_ctx, target);
435 fail:
436 TALLOC_FREE(frame);
437 return status;
440 /****************************************************************************
441 Hard link a file (UNIX extensions).
442 ****************************************************************************/
444 struct cli_posix_hardlink_state {
445 uint8_t dummy;
448 static void cli_posix_hardlink_done(struct tevent_req *subreq);
450 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
451 struct tevent_context *ev,
452 struct cli_state *cli,
453 const char *oldname,
454 const char *newname)
456 struct tevent_req *req = NULL, *subreq = NULL;
457 struct cli_posix_hardlink_state *state = NULL;
459 req = tevent_req_create(
460 mem_ctx, &state, struct cli_posix_hardlink_state);
461 if (req == NULL) {
462 return NULL;
465 subreq = cli_posix_link_internal_send(
466 state, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
467 if (tevent_req_nomem(subreq, req)) {
468 return tevent_req_post(req, ev);
470 tevent_req_set_callback(subreq, cli_posix_hardlink_done, req);
471 return req;
474 static void cli_posix_hardlink_done(struct tevent_req *subreq)
476 NTSTATUS status = cli_posix_link_internal_recv(subreq);
477 tevent_req_simple_finish_ntstatus(subreq, status);
480 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
482 return tevent_req_simple_recv_ntstatus(req);
485 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
486 const char *oldname,
487 const char *newname)
489 TALLOC_CTX *frame = talloc_stackframe();
490 struct tevent_context *ev = NULL;
491 struct tevent_req *req = NULL;
492 NTSTATUS status = NT_STATUS_OK;
494 if (smbXcli_conn_has_async_calls(cli->conn)) {
496 * Can't use sync call while an async call is in flight
498 status = NT_STATUS_INVALID_PARAMETER;
499 goto fail;
502 ev = samba_tevent_context_init(frame);
503 if (ev == NULL) {
504 status = NT_STATUS_NO_MEMORY;
505 goto fail;
508 req = cli_posix_hardlink_send(frame,
510 cli,
511 oldname,
512 newname);
513 if (req == NULL) {
514 status = NT_STATUS_NO_MEMORY;
515 goto fail;
518 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
519 goto fail;
522 status = cli_posix_hardlink_recv(req);
524 fail:
525 TALLOC_FREE(frame);
526 return status;
529 /****************************************************************************
530 Do a POSIX getacl - pathname based ACL get (UNIX extensions).
531 ****************************************************************************/
533 struct getacl_state {
534 uint32_t num_data;
535 uint8_t *data;
538 static void cli_posix_getacl_done(struct tevent_req *subreq);
540 struct tevent_req *cli_posix_getacl_send(TALLOC_CTX *mem_ctx,
541 struct tevent_context *ev,
542 struct cli_state *cli,
543 const char *fname)
545 struct tevent_req *req = NULL, *subreq = NULL;
546 struct getacl_state *state = NULL;
548 req = tevent_req_create(mem_ctx, &state, struct getacl_state);
549 if (req == NULL) {
550 return NULL;
552 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
553 0, CLI_BUFFER_SIZE);
554 if (tevent_req_nomem(subreq, req)) {
555 return tevent_req_post(req, ev);
557 tevent_req_set_callback(subreq, cli_posix_getacl_done, req);
558 return req;
561 static void cli_posix_getacl_done(struct tevent_req *subreq)
563 struct tevent_req *req = tevent_req_callback_data(
564 subreq, struct tevent_req);
565 struct getacl_state *state = tevent_req_data(
566 req, struct getacl_state);
567 NTSTATUS status;
569 status = cli_qpathinfo_recv(subreq, state, &state->data,
570 &state->num_data);
571 TALLOC_FREE(subreq);
572 if (tevent_req_nterror(req, status)) {
573 return;
575 tevent_req_done(req);
578 NTSTATUS cli_posix_getacl_recv(struct tevent_req *req,
579 TALLOC_CTX *mem_ctx,
580 size_t *prb_size,
581 char **retbuf)
583 struct getacl_state *state = tevent_req_data(req, struct getacl_state);
584 NTSTATUS status;
586 if (tevent_req_is_nterror(req, &status)) {
587 return status;
589 *prb_size = (size_t)state->num_data;
590 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
591 return NT_STATUS_OK;
594 NTSTATUS cli_posix_getacl(struct cli_state *cli,
595 const char *fname,
596 TALLOC_CTX *mem_ctx,
597 size_t *prb_size,
598 char **retbuf)
600 TALLOC_CTX *frame = talloc_stackframe();
601 struct tevent_context *ev = NULL;
602 struct tevent_req *req = NULL;
603 NTSTATUS status = NT_STATUS_OK;
605 if (smbXcli_conn_has_async_calls(cli->conn)) {
607 * Can't use sync call while an async call is in flight
609 status = NT_STATUS_INVALID_PARAMETER;
610 goto fail;
613 ev = samba_tevent_context_init(frame);
614 if (ev == NULL) {
615 status = NT_STATUS_NO_MEMORY;
616 goto fail;
619 req = cli_posix_getacl_send(frame,
621 cli,
622 fname);
623 if (req == NULL) {
624 status = NT_STATUS_NO_MEMORY;
625 goto fail;
628 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
629 goto fail;
632 status = cli_posix_getacl_recv(req, mem_ctx, prb_size, retbuf);
634 fail:
635 TALLOC_FREE(frame);
636 return status;
639 /****************************************************************************
640 Do a POSIX setacl - pathname based ACL set (UNIX extensions).
641 ****************************************************************************/
643 struct setacl_state {
644 uint8_t *data;
647 static void cli_posix_setacl_done(struct tevent_req *subreq);
649 struct tevent_req *cli_posix_setacl_send(TALLOC_CTX *mem_ctx,
650 struct tevent_context *ev,
651 struct cli_state *cli,
652 const char *fname,
653 const void *data,
654 size_t num_data)
656 struct tevent_req *req = NULL, *subreq = NULL;
657 struct setacl_state *state = NULL;
659 req = tevent_req_create(mem_ctx, &state, struct setacl_state);
660 if (req == NULL) {
661 return NULL;
663 state->data = talloc_memdup(state, data, num_data);
664 if (tevent_req_nomem(state->data, req)) {
665 return tevent_req_post(req, ev);
668 subreq = cli_setpathinfo_send(state,
670 cli,
671 SMB_SET_POSIX_ACL,
672 fname,
673 state->data,
674 num_data);
675 if (tevent_req_nomem(subreq, req)) {
676 return tevent_req_post(req, ev);
678 tevent_req_set_callback(subreq, cli_posix_setacl_done, req);
679 return req;
682 static void cli_posix_setacl_done(struct tevent_req *subreq)
684 NTSTATUS status = cli_setpathinfo_recv(subreq);
685 tevent_req_simple_finish_ntstatus(subreq, status);
688 NTSTATUS cli_posix_setacl_recv(struct tevent_req *req)
690 return tevent_req_simple_recv_ntstatus(req);
693 NTSTATUS cli_posix_setacl(struct cli_state *cli,
694 const char *fname,
695 const void *acl_buf,
696 size_t acl_buf_size)
698 TALLOC_CTX *frame = talloc_stackframe();
699 struct tevent_context *ev = NULL;
700 struct tevent_req *req = NULL;
701 NTSTATUS status = NT_STATUS_OK;
703 if (smbXcli_conn_has_async_calls(cli->conn)) {
705 * Can't use sync call while an async call is in flight
707 status = NT_STATUS_INVALID_PARAMETER;
708 goto fail;
711 ev = samba_tevent_context_init(frame);
712 if (ev == NULL) {
713 status = NT_STATUS_NO_MEMORY;
714 goto fail;
717 req = cli_posix_setacl_send(frame,
719 cli,
720 fname,
721 acl_buf,
722 acl_buf_size);
723 if (req == NULL) {
724 status = NT_STATUS_NO_MEMORY;
725 goto fail;
728 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
729 goto fail;
732 status = cli_posix_setacl_recv(req);
734 fail:
735 TALLOC_FREE(frame);
736 return status;
739 /****************************************************************************
740 Stat a file (UNIX extensions).
741 ****************************************************************************/
743 struct stat_state {
744 SMB_STRUCT_STAT *sbuf;
747 static void cli_posix_stat_done(struct tevent_req *subreq);
749 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
750 struct tevent_context *ev,
751 struct cli_state *cli,
752 const char *fname,
753 SMB_STRUCT_STAT *sbuf)
755 struct tevent_req *req = NULL, *subreq = NULL;
756 struct stat_state *state = NULL;
758 req = tevent_req_create(mem_ctx, &state, struct stat_state);
759 if (req == NULL) {
760 return NULL;
762 state->sbuf = sbuf;
764 subreq = cli_qpathinfo_send(state, ev, cli, fname,
765 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
766 if (tevent_req_nomem(subreq, req)) {
767 return tevent_req_post(req, ev);
769 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
770 return req;
773 static void cli_posix_stat_done(struct tevent_req *subreq)
775 struct tevent_req *req = tevent_req_callback_data(
776 subreq, struct tevent_req);
777 struct stat_state *state = tevent_req_data(req, struct stat_state);
778 SMB_STRUCT_STAT *sbuf = state->sbuf;
779 uint8_t *data;
780 uint32_t num_data;
781 NTSTATUS status;
783 status = cli_qpathinfo_recv(subreq, state, &data, &num_data);
784 TALLOC_FREE(subreq);
785 if (tevent_req_nterror(req, status)) {
786 return;
789 if (num_data != 100) {
791 * Paranoia, cli_qpathinfo should have guaranteed
792 * this, but you never know...
794 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
795 return;
798 *sbuf = (SMB_STRUCT_STAT) { 0 };
800 /* total size, in bytes */
801 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(data, 0);
803 /* number of blocks allocated */
804 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(data,8);
805 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
806 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
807 #else
808 /* assume 512 byte blocks */
809 sbuf->st_ex_blocks /= 512;
810 #endif
811 /* time of last change */
812 sbuf->st_ex_ctime = interpret_long_date((char *)(data + 16));
814 /* time of last access */
815 sbuf->st_ex_atime = interpret_long_date((char *)(data + 24));
817 /* time of last modification */
818 sbuf->st_ex_mtime = interpret_long_date((char *)(data + 32));
820 sbuf->st_ex_uid = (uid_t) IVAL(data, 40); /* user ID of owner */
821 sbuf->st_ex_gid = (gid_t) IVAL(data, 48); /* group ID of owner */
822 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(data, 56));
824 #if defined(HAVE_MAKEDEV)
826 uint32_t dev_major = IVAL(data,60);
827 uint32_t dev_minor = IVAL(data,68);
828 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
830 #endif
831 /* inode */
832 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(data, 76);
834 /* protection */
835 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(data, 84));
837 /* number of hard links */
838 sbuf->st_ex_nlink = BIG_UINT(data, 92);
840 tevent_req_done(req);
843 NTSTATUS cli_posix_stat_recv(struct tevent_req *req)
845 return tevent_req_simple_recv_ntstatus(req);
848 NTSTATUS cli_posix_stat(struct cli_state *cli,
849 const char *fname,
850 SMB_STRUCT_STAT *sbuf)
852 TALLOC_CTX *frame = talloc_stackframe();
853 struct tevent_context *ev = NULL;
854 struct tevent_req *req = NULL;
855 NTSTATUS status = NT_STATUS_OK;
857 if (smbXcli_conn_has_async_calls(cli->conn)) {
859 * Can't use sync call while an async call is in flight
861 status = NT_STATUS_INVALID_PARAMETER;
862 goto fail;
865 ev = samba_tevent_context_init(frame);
866 if (ev == NULL) {
867 status = NT_STATUS_NO_MEMORY;
868 goto fail;
871 req = cli_posix_stat_send(frame, ev, cli, fname, sbuf);
872 if (req == NULL) {
873 status = NT_STATUS_NO_MEMORY;
874 goto fail;
877 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
878 goto fail;
881 status = cli_posix_stat_recv(req);
883 fail:
884 TALLOC_FREE(frame);
885 return status;
888 /****************************************************************************
889 Chmod or chown a file internal (UNIX extensions).
890 ****************************************************************************/
892 struct cli_posix_chown_chmod_internal_state {
893 uint8_t data[100];
896 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
898 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
899 struct tevent_context *ev,
900 struct cli_state *cli,
901 const char *fname,
902 uint32_t mode,
903 uint32_t uid,
904 uint32_t gid)
906 struct tevent_req *req = NULL, *subreq = NULL;
907 struct cli_posix_chown_chmod_internal_state *state = NULL;
909 req = tevent_req_create(mem_ctx, &state,
910 struct cli_posix_chown_chmod_internal_state);
911 if (req == NULL) {
912 return NULL;
915 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
916 memset(&state->data[40], '\0', 60);
917 SIVAL(state->data,40,uid);
918 SIVAL(state->data,48,gid);
919 SIVAL(state->data,84,mode);
921 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
922 fname, state->data, sizeof(state->data));
923 if (tevent_req_nomem(subreq, req)) {
924 return tevent_req_post(req, ev);
926 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
927 req);
928 return req;
931 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
933 NTSTATUS status = cli_setpathinfo_recv(subreq);
934 tevent_req_simple_finish_ntstatus(subreq, status);
937 static NTSTATUS cli_posix_chown_chmod_internal_recv(struct tevent_req *req)
939 return tevent_req_simple_recv_ntstatus(req);
942 /****************************************************************************
943 chmod a file (UNIX extensions).
944 ****************************************************************************/
946 struct cli_posix_chmod_state {
947 uint8_t dummy;
950 static void cli_posix_chmod_done(struct tevent_req *subreq);
952 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
953 struct tevent_context *ev,
954 struct cli_state *cli,
955 const char *fname,
956 mode_t mode)
958 struct tevent_req *req = NULL, *subreq = NULL;
959 struct cli_posix_chmod_state *state = NULL;
961 req = tevent_req_create(mem_ctx, &state, struct cli_posix_chmod_state);
962 if (req == NULL) {
963 return NULL;
966 subreq = cli_posix_chown_chmod_internal_send(
967 state,
969 cli,
970 fname,
971 unix_perms_to_wire(mode),
972 SMB_UID_NO_CHANGE,
973 SMB_GID_NO_CHANGE);
974 if (tevent_req_nomem(subreq, req)) {
975 return tevent_req_post(req, ev);
977 tevent_req_set_callback(subreq, cli_posix_chmod_done, req);
978 return req;
981 static void cli_posix_chmod_done(struct tevent_req *subreq)
983 NTSTATUS status = cli_posix_chown_chmod_internal_recv(subreq);
984 tevent_req_simple_finish_ntstatus(subreq, status);
987 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
989 return tevent_req_simple_recv_ntstatus(req);
992 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
994 TALLOC_CTX *frame = talloc_stackframe();
995 struct tevent_context *ev = NULL;
996 struct tevent_req *req = NULL;
997 NTSTATUS status = NT_STATUS_OK;
999 if (smbXcli_conn_has_async_calls(cli->conn)) {
1001 * Can't use sync call while an async call is in flight
1003 status = NT_STATUS_INVALID_PARAMETER;
1004 goto fail;
1007 ev = samba_tevent_context_init(frame);
1008 if (ev == NULL) {
1009 status = NT_STATUS_NO_MEMORY;
1010 goto fail;
1013 req = cli_posix_chmod_send(frame,
1015 cli,
1016 fname,
1017 mode);
1018 if (req == NULL) {
1019 status = NT_STATUS_NO_MEMORY;
1020 goto fail;
1023 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1024 goto fail;
1027 status = cli_posix_chmod_recv(req);
1029 fail:
1030 TALLOC_FREE(frame);
1031 return status;
1034 /****************************************************************************
1035 chown a file (UNIX extensions).
1036 ****************************************************************************/
1038 struct cli_posix_chown_state {
1039 uint8_t dummy;
1042 static void cli_posix_chown_done(struct tevent_req *subreq);
1044 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1045 struct tevent_context *ev,
1046 struct cli_state *cli,
1047 const char *fname,
1048 uid_t uid,
1049 gid_t gid)
1051 struct tevent_req *req = NULL, *subreq = NULL;
1052 struct cli_posix_chown_state *state = NULL;
1054 req = tevent_req_create(
1055 mem_ctx, &state, struct cli_posix_chown_state);
1056 if (req == NULL) {
1057 return NULL;
1060 subreq = cli_posix_chown_chmod_internal_send(
1061 state,
1063 cli,
1064 fname,
1065 SMB_MODE_NO_CHANGE,
1066 (uint32_t)uid,
1067 (uint32_t)gid);
1068 if (tevent_req_nomem(subreq, req)) {
1069 return tevent_req_post(req, ev);
1071 tevent_req_set_callback(subreq, cli_posix_chown_done, req);
1072 return req;
1075 static void cli_posix_chown_done(struct tevent_req *subreq)
1077 NTSTATUS status = cli_posix_chown_chmod_internal_recv(subreq);
1078 tevent_req_simple_finish_ntstatus(subreq, status);
1081 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1083 return tevent_req_simple_recv_ntstatus(req);
1086 NTSTATUS cli_posix_chown(struct cli_state *cli,
1087 const char *fname,
1088 uid_t uid,
1089 gid_t gid)
1091 TALLOC_CTX *frame = talloc_stackframe();
1092 struct tevent_context *ev = NULL;
1093 struct tevent_req *req = NULL;
1094 NTSTATUS status = NT_STATUS_OK;
1096 if (smbXcli_conn_has_async_calls(cli->conn)) {
1098 * Can't use sync call while an async call is in flight
1100 status = NT_STATUS_INVALID_PARAMETER;
1101 goto fail;
1104 ev = samba_tevent_context_init(frame);
1105 if (ev == NULL) {
1106 status = NT_STATUS_NO_MEMORY;
1107 goto fail;
1110 req = cli_posix_chown_send(frame,
1112 cli,
1113 fname,
1114 uid,
1115 gid);
1116 if (req == NULL) {
1117 status = NT_STATUS_NO_MEMORY;
1118 goto fail;
1121 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1122 goto fail;
1125 status = cli_posix_chown_recv(req);
1127 fail:
1128 TALLOC_FREE(frame);
1129 return status;
1132 /****************************************************************************
1133 Rename a file.
1134 ****************************************************************************/
1136 static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
1137 struct tevent_context *ev,
1138 struct cli_state *cli,
1139 const char *fname_src,
1140 const char *fname_dst,
1141 bool replace);
1143 static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
1144 struct tevent_context *ev,
1145 struct cli_state *cli,
1146 const char *fname_src,
1147 const char *fname_dst,
1148 bool replace);
1150 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1151 struct tevent_context *ev,
1152 struct cli_state *cli,
1153 const char *fname_src,
1154 const char *fname_dst,
1155 bool replace)
1157 if (replace && smbXcli_conn_support_passthrough(cli->conn)) {
1158 return cli_smb1_rename_send(mem_ctx, ev, cli, fname_src,
1159 fname_dst, replace);
1160 } else {
1161 return cli_cifs_rename_send(mem_ctx, ev, cli, fname_src,
1162 fname_dst, replace);
1166 struct cli_smb1_rename_state {
1167 uint8_t *data;
1170 static void cli_smb1_rename_done(struct tevent_req *subreq);
1172 static struct tevent_req *cli_smb1_rename_send(TALLOC_CTX *mem_ctx,
1173 struct tevent_context *ev,
1174 struct cli_state *cli,
1175 const char *fname_src,
1176 const char *fname_dst,
1177 bool replace)
1179 NTSTATUS status;
1180 struct tevent_req *req = NULL, *subreq = NULL;
1181 struct cli_smb1_rename_state *state = NULL;
1182 smb_ucs2_t *converted_str = NULL;
1183 size_t converted_size_bytes = 0;
1185 req = tevent_req_create(mem_ctx, &state, struct cli_smb1_rename_state);
1186 if (req == NULL) {
1187 return NULL;
1190 if (!push_ucs2_talloc(talloc_tos(), &converted_str, fname_dst,
1191 &converted_size_bytes)) {
1192 status = NT_STATUS_INVALID_PARAMETER;
1193 goto fail;
1196 /* W2K8 insists the dest name is not null
1197 terminated. Remove the last 2 zero bytes
1198 and reduce the name length. */
1200 if (converted_size_bytes < 2) {
1201 status = NT_STATUS_INVALID_PARAMETER;
1202 goto fail;
1204 converted_size_bytes -= 2;
1206 state->data =
1207 talloc_zero_array(state, uint8_t, 12 + converted_size_bytes);
1208 if (state->data == NULL) {
1209 status = NT_STATUS_NO_MEMORY;
1210 goto fail;
1213 if (replace) {
1214 SCVAL(state->data, 0, 1);
1217 SIVAL(state->data, 8, converted_size_bytes);
1218 memcpy(state->data + 12, converted_str, converted_size_bytes);
1220 TALLOC_FREE(converted_str);
1222 subreq = cli_setpathinfo_send(
1223 state, ev, cli, SMB_FILE_RENAME_INFORMATION, fname_src, state->data,
1224 talloc_get_size(state->data));
1225 if (tevent_req_nomem(subreq, req)) {
1226 status = NT_STATUS_NO_MEMORY;
1227 goto fail;
1229 tevent_req_set_callback(subreq, cli_smb1_rename_done, req);
1230 return req;
1232 fail:
1233 TALLOC_FREE(converted_str);
1234 tevent_req_nterror(req, status);
1235 return tevent_req_post(req, ev);
1238 static void cli_smb1_rename_done(struct tevent_req *subreq)
1240 NTSTATUS status = cli_setpathinfo_recv(subreq);
1241 tevent_req_simple_finish_ntstatus(subreq, status);
1244 static void cli_cifs_rename_done(struct tevent_req *subreq);
1246 struct cli_cifs_rename_state {
1247 uint16_t vwv[1];
1250 static struct tevent_req *cli_cifs_rename_send(TALLOC_CTX *mem_ctx,
1251 struct tevent_context *ev,
1252 struct cli_state *cli,
1253 const char *fname_src,
1254 const char *fname_dst,
1255 bool replace)
1257 struct tevent_req *req = NULL, *subreq = NULL;
1258 struct cli_cifs_rename_state *state = NULL;
1259 uint8_t additional_flags = 0;
1260 uint16_t additional_flags2 = 0;
1261 uint8_t *bytes = NULL;
1263 req = tevent_req_create(mem_ctx, &state, struct cli_cifs_rename_state);
1264 if (req == NULL) {
1265 return NULL;
1268 if (replace) {
1270 * CIFS doesn't support replace
1272 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1273 return tevent_req_post(req, ev);
1276 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1278 bytes = talloc_array(state, uint8_t, 1);
1279 if (tevent_req_nomem(bytes, req)) {
1280 return tevent_req_post(req, ev);
1282 bytes[0] = 4;
1283 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1284 strlen(fname_src)+1, NULL);
1285 if (tevent_req_nomem(bytes, req)) {
1286 return tevent_req_post(req, ev);
1289 if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
1290 additional_flags2 = FLAGS2_REPARSE_PATH;
1293 bytes = talloc_realloc(state, bytes, uint8_t,
1294 talloc_get_size(bytes)+1);
1295 if (tevent_req_nomem(bytes, req)) {
1296 return tevent_req_post(req, ev);
1299 bytes[talloc_get_size(bytes)-1] = 4;
1300 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1301 strlen(fname_dst)+1, NULL);
1302 if (tevent_req_nomem(bytes, req)) {
1303 return tevent_req_post(req, ev);
1306 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1307 additional_flags2,
1308 1, state->vwv, talloc_get_size(bytes), bytes);
1309 if (tevent_req_nomem(subreq, req)) {
1310 return tevent_req_post(req, ev);
1312 tevent_req_set_callback(subreq, cli_cifs_rename_done, req);
1313 return req;
1316 static void cli_cifs_rename_done(struct tevent_req *subreq)
1318 struct tevent_req *req = tevent_req_callback_data(
1319 subreq, struct tevent_req);
1320 NTSTATUS status;
1322 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1323 TALLOC_FREE(subreq);
1324 if (tevent_req_nterror(req, status)) {
1325 return;
1327 tevent_req_done(req);
1330 NTSTATUS cli_rename_recv(struct tevent_req *req)
1332 return tevent_req_simple_recv_ntstatus(req);
1335 NTSTATUS cli_rename(struct cli_state *cli,
1336 const char *fname_src,
1337 const char *fname_dst,
1338 bool replace)
1340 TALLOC_CTX *frame = NULL;
1341 struct tevent_context *ev;
1342 struct tevent_req *req;
1343 NTSTATUS status = NT_STATUS_OK;
1345 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1346 return cli_smb2_rename(cli, fname_src, fname_dst, replace);
1349 frame = talloc_stackframe();
1351 if (smbXcli_conn_has_async_calls(cli->conn)) {
1353 * Can't use sync call while an async call is in flight
1355 status = NT_STATUS_INVALID_PARAMETER;
1356 goto fail;
1359 ev = samba_tevent_context_init(frame);
1360 if (ev == NULL) {
1361 status = NT_STATUS_NO_MEMORY;
1362 goto fail;
1365 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst, replace);
1366 if (req == NULL) {
1367 status = NT_STATUS_NO_MEMORY;
1368 goto fail;
1371 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1372 goto fail;
1375 status = cli_rename_recv(req);
1377 fail:
1378 TALLOC_FREE(frame);
1379 return status;
1382 /****************************************************************************
1383 NT Rename a file.
1384 ****************************************************************************/
1386 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1388 struct cli_ntrename_internal_state {
1389 uint16_t vwv[4];
1392 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1393 struct tevent_context *ev,
1394 struct cli_state *cli,
1395 const char *fname_src,
1396 const char *fname_dst,
1397 uint16_t rename_flag)
1399 struct tevent_req *req = NULL, *subreq = NULL;
1400 struct cli_ntrename_internal_state *state = NULL;
1401 uint8_t additional_flags = 0;
1402 uint16_t additional_flags2 = 0;
1403 uint8_t *bytes = NULL;
1405 req = tevent_req_create(mem_ctx, &state,
1406 struct cli_ntrename_internal_state);
1407 if (req == NULL) {
1408 return NULL;
1411 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1412 SSVAL(state->vwv+1, 0, rename_flag);
1414 bytes = talloc_array(state, uint8_t, 1);
1415 if (tevent_req_nomem(bytes, req)) {
1416 return tevent_req_post(req, ev);
1418 bytes[0] = 4;
1419 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1420 strlen(fname_src)+1, NULL);
1421 if (tevent_req_nomem(bytes, req)) {
1422 return tevent_req_post(req, ev);
1425 if (clistr_is_previous_version_path(fname_src, NULL, NULL, NULL)) {
1426 additional_flags2 = FLAGS2_REPARSE_PATH;
1429 bytes = talloc_realloc(state, bytes, uint8_t,
1430 talloc_get_size(bytes)+1);
1431 if (tevent_req_nomem(bytes, req)) {
1432 return tevent_req_post(req, ev);
1435 bytes[talloc_get_size(bytes)-1] = 4;
1436 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1437 strlen(fname_dst)+1, NULL);
1438 if (tevent_req_nomem(bytes, req)) {
1439 return tevent_req_post(req, ev);
1442 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1443 additional_flags2,
1444 4, state->vwv, talloc_get_size(bytes), bytes);
1445 if (tevent_req_nomem(subreq, req)) {
1446 return tevent_req_post(req, ev);
1448 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1449 return req;
1452 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1454 NTSTATUS status = cli_smb_recv(
1455 subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1456 tevent_req_simple_finish_ntstatus(subreq, status);
1459 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1461 return tevent_req_simple_recv_ntstatus(req);
1464 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1465 struct tevent_context *ev,
1466 struct cli_state *cli,
1467 const char *fname_src,
1468 const char *fname_dst)
1470 return cli_ntrename_internal_send(mem_ctx,
1472 cli,
1473 fname_src,
1474 fname_dst,
1475 RENAME_FLAG_RENAME);
1478 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1480 return cli_ntrename_internal_recv(req);
1483 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1485 TALLOC_CTX *frame = talloc_stackframe();
1486 struct tevent_context *ev;
1487 struct tevent_req *req;
1488 NTSTATUS status = NT_STATUS_OK;
1490 if (smbXcli_conn_has_async_calls(cli->conn)) {
1492 * Can't use sync call while an async call is in flight
1494 status = NT_STATUS_INVALID_PARAMETER;
1495 goto fail;
1498 ev = samba_tevent_context_init(frame);
1499 if (ev == NULL) {
1500 status = NT_STATUS_NO_MEMORY;
1501 goto fail;
1504 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1505 if (req == NULL) {
1506 status = NT_STATUS_NO_MEMORY;
1507 goto fail;
1510 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1511 goto fail;
1514 status = cli_ntrename_recv(req);
1516 fail:
1517 TALLOC_FREE(frame);
1518 return status;
1521 /****************************************************************************
1522 NT hardlink a file.
1523 ****************************************************************************/
1525 static struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1526 struct tevent_context *ev,
1527 struct cli_state *cli,
1528 const char *fname_src,
1529 const char *fname_dst)
1531 return cli_ntrename_internal_send(mem_ctx,
1533 cli,
1534 fname_src,
1535 fname_dst,
1536 RENAME_FLAG_HARD_LINK);
1539 static NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1541 return cli_ntrename_internal_recv(req);
1544 struct cli_smb2_hardlink_state {
1545 struct tevent_context *ev;
1546 struct cli_state *cli;
1547 uint16_t fnum_src;
1548 const char *fname_dst;
1549 bool overwrite;
1550 NTSTATUS status;
1553 static void cli_smb2_hardlink_opened(struct tevent_req *subreq);
1554 static void cli_smb2_hardlink_info_set(struct tevent_req *subreq);
1555 static void cli_smb2_hardlink_closed(struct tevent_req *subreq);
1557 static struct tevent_req *cli_smb2_hardlink_send(
1558 TALLOC_CTX *mem_ctx,
1559 struct tevent_context *ev,
1560 struct cli_state *cli,
1561 const char *fname_src,
1562 const char *fname_dst,
1563 bool overwrite,
1564 struct smb2_create_blobs *in_cblobs)
1566 struct tevent_req *req = NULL, *subreq = NULL;
1567 struct cli_smb2_hardlink_state *state = NULL;
1569 req = tevent_req_create(
1570 mem_ctx, &state, struct cli_smb2_hardlink_state);
1571 if (req == NULL) {
1572 return NULL;
1574 state->ev = ev;
1575 state->cli = cli;
1576 state->fname_dst = fname_dst;
1577 state->overwrite = overwrite;
1579 subreq = cli_smb2_create_fnum_send(
1580 state,
1582 cli,
1583 fname_src,
1584 0, /* create_flags */
1585 SMB2_IMPERSONATION_IMPERSONATION,
1586 FILE_WRITE_ATTRIBUTES,
1587 0, /* file attributes */
1588 FILE_SHARE_READ|
1589 FILE_SHARE_WRITE|
1590 FILE_SHARE_DELETE, /* share_access */
1591 FILE_OPEN, /* create_disposition */
1592 FILE_NON_DIRECTORY_FILE, /* no hardlinks on directories */
1593 in_cblobs);
1594 if (tevent_req_nomem(subreq, req)) {
1595 return tevent_req_post(req, ev);
1597 tevent_req_set_callback(subreq, cli_smb2_hardlink_opened, req);
1598 return req;
1601 static void cli_smb2_hardlink_opened(struct tevent_req *subreq)
1603 struct tevent_req *req = tevent_req_callback_data(
1604 subreq, struct tevent_req);
1605 struct cli_smb2_hardlink_state *state = tevent_req_data(
1606 req, struct cli_smb2_hardlink_state);
1607 NTSTATUS status;
1608 smb_ucs2_t *ucs2_dst;
1609 size_t ucs2_len;
1610 DATA_BLOB inbuf;
1611 bool ok;
1613 status = cli_smb2_create_fnum_recv(
1614 subreq, &state->fnum_src, NULL, NULL, NULL);
1615 TALLOC_FREE(subreq);
1616 if (tevent_req_nterror(req, status)) {
1617 return;
1620 ok = push_ucs2_talloc(state, &ucs2_dst, state->fname_dst, &ucs2_len);
1621 if (!ok || (ucs2_len < 2)) {
1622 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
1623 return;
1625 /* Don't 0-terminate the name */
1626 ucs2_len -= 2;
1628 inbuf = data_blob_talloc_zero(state, ucs2_len + 20);
1629 if (tevent_req_nomem(inbuf.data, req)) {
1630 return;
1633 if (state->overwrite) {
1634 SCVAL(inbuf.data, 0, 1);
1636 SIVAL(inbuf.data, 16, ucs2_len);
1637 memcpy(inbuf.data + 20, ucs2_dst, ucs2_len);
1638 TALLOC_FREE(ucs2_dst);
1640 subreq = cli_smb2_set_info_fnum_send(
1641 state,
1642 state->ev,
1643 state->cli,
1644 state->fnum_src,
1645 1, /* in_info_type */
1646 SMB_FILE_LINK_INFORMATION - 1000, /* in_file_info_class */
1647 &inbuf,
1648 0); /* in_additional_info */
1649 if (tevent_req_nomem(subreq, req)) {
1650 return;
1652 tevent_req_set_callback(subreq, cli_smb2_hardlink_info_set, req);
1655 static void cli_smb2_hardlink_info_set(struct tevent_req *subreq)
1657 struct tevent_req *req = tevent_req_callback_data(
1658 subreq, struct tevent_req);
1659 struct cli_smb2_hardlink_state *state = tevent_req_data(
1660 req, struct cli_smb2_hardlink_state);
1662 state->status = cli_smb2_set_info_fnum_recv(subreq);
1663 TALLOC_FREE(subreq);
1665 /* ignore error here, we need to close the file */
1667 subreq = cli_smb2_close_fnum_send(
1668 state, state->ev, state->cli, state->fnum_src);
1669 if (tevent_req_nomem(subreq, req)) {
1670 return;
1672 tevent_req_set_callback(subreq, cli_smb2_hardlink_closed, req);
1675 static void cli_smb2_hardlink_closed(struct tevent_req *subreq)
1677 NTSTATUS status = cli_smb2_close_fnum_recv(subreq);
1678 tevent_req_simple_finish_ntstatus(subreq, status);
1681 static NTSTATUS cli_smb2_hardlink_recv(struct tevent_req *req)
1683 struct cli_smb2_hardlink_state *state = tevent_req_data(
1684 req, struct cli_smb2_hardlink_state);
1685 NTSTATUS status;
1687 if (tevent_req_is_nterror(req, &status)) {
1688 return status;
1690 return state->status;
1693 struct cli_hardlink_state {
1694 uint8_t dummy;
1697 static void cli_hardlink_done(struct tevent_req *subreq);
1698 static void cli_hardlink_done2(struct tevent_req *subreq);
1700 struct tevent_req *cli_hardlink_send(
1701 TALLOC_CTX *mem_ctx,
1702 struct tevent_context *ev,
1703 struct cli_state *cli,
1704 const char *fname_src,
1705 const char *fname_dst)
1707 struct tevent_req *req = NULL, *subreq = NULL;
1708 struct cli_hardlink_state *state;
1710 req = tevent_req_create(mem_ctx, &state, struct cli_hardlink_state);
1711 if (req == NULL) {
1712 return NULL;
1715 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1716 subreq = cli_smb2_hardlink_send(
1717 state, ev, cli, fname_src, fname_dst, false, NULL);
1718 if (tevent_req_nomem(subreq, req)) {
1719 return tevent_req_post(req, ev);
1721 tevent_req_set_callback(subreq, cli_hardlink_done2, req);
1722 return req;
1725 subreq = cli_nt_hardlink_send(state, ev, cli, fname_src, fname_dst);
1726 if (tevent_req_nomem(subreq, req)) {
1727 return tevent_req_post(req, ev);
1729 tevent_req_set_callback(subreq, cli_hardlink_done, req);
1730 return req;
1733 static void cli_hardlink_done(struct tevent_req *subreq)
1735 NTSTATUS status = cli_nt_hardlink_recv(subreq);
1736 tevent_req_simple_finish_ntstatus(subreq, status);
1739 static void cli_hardlink_done2(struct tevent_req *subreq)
1741 NTSTATUS status = cli_smb2_hardlink_recv(subreq);
1742 tevent_req_simple_finish_ntstatus(subreq, status);
1745 NTSTATUS cli_hardlink_recv(struct tevent_req *req)
1747 return tevent_req_simple_recv_ntstatus(req);
1750 NTSTATUS cli_hardlink(
1751 struct cli_state *cli, const char *fname_src, const char *fname_dst)
1753 TALLOC_CTX *frame = talloc_stackframe();
1754 struct tevent_context *ev = NULL;
1755 struct tevent_req *req = NULL;
1756 NTSTATUS status = NT_STATUS_NO_MEMORY;
1758 if (smbXcli_conn_has_async_calls(cli->conn)) {
1759 status = NT_STATUS_INVALID_PARAMETER;
1760 goto fail;
1762 ev = samba_tevent_context_init(frame);
1763 if (ev == NULL) {
1764 goto fail;
1766 req = cli_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1767 if (req == NULL) {
1768 goto fail;
1770 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1771 goto fail;
1773 status = cli_hardlink_recv(req);
1774 fail:
1775 TALLOC_FREE(frame);
1776 return status;
1779 /****************************************************************************
1780 Delete a file.
1781 ****************************************************************************/
1783 static void cli_unlink_done(struct tevent_req *subreq);
1785 struct cli_unlink_state {
1786 uint16_t vwv[1];
1789 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1790 struct tevent_context *ev,
1791 struct cli_state *cli,
1792 const char *fname,
1793 uint16_t mayhave_attrs)
1795 struct tevent_req *req = NULL, *subreq = NULL;
1796 struct cli_unlink_state *state = NULL;
1797 uint8_t additional_flags = 0;
1798 uint16_t additional_flags2 = 0;
1799 uint8_t *bytes = NULL;
1801 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1802 if (req == NULL) {
1803 return NULL;
1806 SSVAL(state->vwv+0, 0, mayhave_attrs);
1808 bytes = talloc_array(state, uint8_t, 1);
1809 if (tevent_req_nomem(bytes, req)) {
1810 return tevent_req_post(req, ev);
1812 bytes[0] = 4;
1813 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1814 strlen(fname)+1, NULL);
1816 if (tevent_req_nomem(bytes, req)) {
1817 return tevent_req_post(req, ev);
1820 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
1821 additional_flags2 = FLAGS2_REPARSE_PATH;
1824 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1825 additional_flags2,
1826 1, state->vwv, talloc_get_size(bytes), bytes);
1827 if (tevent_req_nomem(subreq, req)) {
1828 return tevent_req_post(req, ev);
1830 tevent_req_set_callback(subreq, cli_unlink_done, req);
1831 return req;
1834 static void cli_unlink_done(struct tevent_req *subreq)
1836 struct tevent_req *req = tevent_req_callback_data(
1837 subreq, struct tevent_req);
1838 NTSTATUS status;
1840 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1841 TALLOC_FREE(subreq);
1842 if (tevent_req_nterror(req, status)) {
1843 return;
1845 tevent_req_done(req);
1848 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1850 return tevent_req_simple_recv_ntstatus(req);
1853 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1855 TALLOC_CTX *frame = NULL;
1856 struct tevent_context *ev;
1857 struct tevent_req *req;
1858 NTSTATUS status = NT_STATUS_OK;
1860 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1861 return cli_smb2_unlink(cli, fname, NULL);
1864 frame = talloc_stackframe();
1866 if (smbXcli_conn_has_async_calls(cli->conn)) {
1868 * Can't use sync call while an async call is in flight
1870 status = NT_STATUS_INVALID_PARAMETER;
1871 goto fail;
1874 ev = samba_tevent_context_init(frame);
1875 if (ev == NULL) {
1876 status = NT_STATUS_NO_MEMORY;
1877 goto fail;
1880 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1881 if (req == NULL) {
1882 status = NT_STATUS_NO_MEMORY;
1883 goto fail;
1886 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1887 goto fail;
1890 status = cli_unlink_recv(req);
1892 fail:
1893 TALLOC_FREE(frame);
1894 return status;
1897 /****************************************************************************
1898 Create a directory.
1899 ****************************************************************************/
1901 static void cli_mkdir_done(struct tevent_req *subreq);
1903 struct cli_mkdir_state {
1904 int dummy;
1907 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1908 struct tevent_context *ev,
1909 struct cli_state *cli,
1910 const char *dname)
1912 struct tevent_req *req = NULL, *subreq = NULL;
1913 struct cli_mkdir_state *state = NULL;
1914 uint8_t additional_flags = 0;
1915 uint16_t additional_flags2 = 0;
1916 uint8_t *bytes = NULL;
1918 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1919 if (req == NULL) {
1920 return NULL;
1923 bytes = talloc_array(state, uint8_t, 1);
1924 if (tevent_req_nomem(bytes, req)) {
1925 return tevent_req_post(req, ev);
1927 bytes[0] = 4;
1928 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1929 strlen(dname)+1, NULL);
1931 if (tevent_req_nomem(bytes, req)) {
1932 return tevent_req_post(req, ev);
1935 if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
1936 additional_flags2 = FLAGS2_REPARSE_PATH;
1939 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1940 additional_flags2,
1941 0, NULL, talloc_get_size(bytes), bytes);
1942 if (tevent_req_nomem(subreq, req)) {
1943 return tevent_req_post(req, ev);
1945 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1946 return req;
1949 static void cli_mkdir_done(struct tevent_req *subreq)
1951 struct tevent_req *req = tevent_req_callback_data(
1952 subreq, struct tevent_req);
1953 NTSTATUS status;
1955 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1956 TALLOC_FREE(subreq);
1957 if (tevent_req_nterror(req, status)) {
1958 return;
1960 tevent_req_done(req);
1963 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1965 return tevent_req_simple_recv_ntstatus(req);
1968 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1970 TALLOC_CTX *frame = NULL;
1971 struct tevent_context *ev;
1972 struct tevent_req *req;
1973 NTSTATUS status = NT_STATUS_OK;
1975 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1976 return cli_smb2_mkdir(cli, dname);
1979 frame = talloc_stackframe();
1981 if (smbXcli_conn_has_async_calls(cli->conn)) {
1983 * Can't use sync call while an async call is in flight
1985 status = NT_STATUS_INVALID_PARAMETER;
1986 goto fail;
1989 ev = samba_tevent_context_init(frame);
1990 if (ev == NULL) {
1991 status = NT_STATUS_NO_MEMORY;
1992 goto fail;
1995 req = cli_mkdir_send(frame, ev, cli, dname);
1996 if (req == NULL) {
1997 status = NT_STATUS_NO_MEMORY;
1998 goto fail;
2001 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2002 goto fail;
2005 status = cli_mkdir_recv(req);
2007 fail:
2008 TALLOC_FREE(frame);
2009 return status;
2012 /****************************************************************************
2013 Remove a directory.
2014 ****************************************************************************/
2016 static void cli_rmdir_done(struct tevent_req *subreq);
2018 struct cli_rmdir_state {
2019 int dummy;
2022 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
2023 struct tevent_context *ev,
2024 struct cli_state *cli,
2025 const char *dname)
2027 struct tevent_req *req = NULL, *subreq = NULL;
2028 struct cli_rmdir_state *state = NULL;
2029 uint8_t additional_flags = 0;
2030 uint16_t additional_flags2 = 0;
2031 uint8_t *bytes = NULL;
2033 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
2034 if (req == NULL) {
2035 return NULL;
2038 bytes = talloc_array(state, uint8_t, 1);
2039 if (tevent_req_nomem(bytes, req)) {
2040 return tevent_req_post(req, ev);
2042 bytes[0] = 4;
2043 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
2044 strlen(dname)+1, NULL);
2046 if (tevent_req_nomem(bytes, req)) {
2047 return tevent_req_post(req, ev);
2050 if (clistr_is_previous_version_path(dname, NULL, NULL, NULL)) {
2051 additional_flags2 = FLAGS2_REPARSE_PATH;
2054 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
2055 additional_flags2,
2056 0, NULL, talloc_get_size(bytes), bytes);
2057 if (tevent_req_nomem(subreq, req)) {
2058 return tevent_req_post(req, ev);
2060 tevent_req_set_callback(subreq, cli_rmdir_done, req);
2061 return req;
2064 static void cli_rmdir_done(struct tevent_req *subreq)
2066 struct tevent_req *req = tevent_req_callback_data(
2067 subreq, struct tevent_req);
2068 NTSTATUS status;
2070 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2071 TALLOC_FREE(subreq);
2072 if (tevent_req_nterror(req, status)) {
2073 return;
2075 tevent_req_done(req);
2078 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
2080 return tevent_req_simple_recv_ntstatus(req);
2083 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
2085 TALLOC_CTX *frame = NULL;
2086 struct tevent_context *ev;
2087 struct tevent_req *req;
2088 NTSTATUS status = NT_STATUS_OK;
2090 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2091 return cli_smb2_rmdir(cli, dname, NULL);
2094 frame = talloc_stackframe();
2096 if (smbXcli_conn_has_async_calls(cli->conn)) {
2098 * Can't use sync call while an async call is in flight
2100 status = NT_STATUS_INVALID_PARAMETER;
2101 goto fail;
2104 ev = samba_tevent_context_init(frame);
2105 if (ev == NULL) {
2106 status = NT_STATUS_NO_MEMORY;
2107 goto fail;
2110 req = cli_rmdir_send(frame, ev, cli, dname);
2111 if (req == NULL) {
2112 status = NT_STATUS_NO_MEMORY;
2113 goto fail;
2116 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2117 goto fail;
2120 status = cli_rmdir_recv(req);
2122 fail:
2123 TALLOC_FREE(frame);
2124 return status;
2127 /****************************************************************************
2128 Set or clear the delete on close flag.
2129 ****************************************************************************/
2131 struct doc_state {
2132 uint16_t setup;
2133 uint8_t param[6];
2134 uint8_t data[1];
2137 static void cli_nt_delete_on_close_smb1_done(struct tevent_req *subreq);
2138 static void cli_nt_delete_on_close_smb2_done(struct tevent_req *subreq);
2140 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
2141 struct tevent_context *ev,
2142 struct cli_state *cli,
2143 uint16_t fnum,
2144 bool flag)
2146 struct tevent_req *req = NULL, *subreq = NULL;
2147 struct doc_state *state = NULL;
2149 req = tevent_req_create(mem_ctx, &state, struct doc_state);
2150 if (req == NULL) {
2151 return NULL;
2154 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2155 subreq = cli_smb2_delete_on_close_send(state, ev, cli,
2156 fnum, flag);
2157 if (tevent_req_nomem(subreq, req)) {
2158 return tevent_req_post(req, ev);
2160 tevent_req_set_callback(subreq,
2161 cli_nt_delete_on_close_smb2_done,
2162 req);
2163 return req;
2166 /* Setup setup word. */
2167 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2169 /* Setup param array. */
2170 SSVAL(state->param,0,fnum);
2171 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
2173 /* Setup data array. */
2174 SCVAL(&state->data[0], 0, flag ? 1 : 0);
2176 subreq = cli_trans_send(state, /* mem ctx. */
2177 ev, /* event ctx. */
2178 cli, /* cli_state. */
2179 0, /* additional_flags2 */
2180 SMBtrans2, /* cmd. */
2181 NULL, /* pipe name. */
2182 -1, /* fid. */
2183 0, /* function. */
2184 0, /* flags. */
2185 &state->setup, /* setup. */
2186 1, /* num setup uint16_t words. */
2187 0, /* max returned setup. */
2188 state->param, /* param. */
2189 6, /* num param. */
2190 2, /* max returned param. */
2191 state->data, /* data. */
2192 1, /* num data. */
2193 0); /* max returned data. */
2195 if (tevent_req_nomem(subreq, req)) {
2196 return tevent_req_post(req, ev);
2198 tevent_req_set_callback(subreq,
2199 cli_nt_delete_on_close_smb1_done,
2200 req);
2201 return req;
2204 static void cli_nt_delete_on_close_smb1_done(struct tevent_req *subreq)
2206 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2207 NULL, 0, NULL, NULL, 0, NULL);
2208 tevent_req_simple_finish_ntstatus(subreq, status);
2211 static void cli_nt_delete_on_close_smb2_done(struct tevent_req *subreq)
2213 NTSTATUS status = cli_smb2_delete_on_close_recv(subreq);
2214 tevent_req_simple_finish_ntstatus(subreq, status);
2217 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
2219 return tevent_req_simple_recv_ntstatus(req);
2222 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
2224 TALLOC_CTX *frame = talloc_stackframe();
2225 struct tevent_context *ev = NULL;
2226 struct tevent_req *req = NULL;
2227 NTSTATUS status = NT_STATUS_OK;
2229 if (smbXcli_conn_has_async_calls(cli->conn)) {
2231 * Can't use sync call while an async call is in flight
2233 status = NT_STATUS_INVALID_PARAMETER;
2234 goto fail;
2237 ev = samba_tevent_context_init(frame);
2238 if (ev == NULL) {
2239 status = NT_STATUS_NO_MEMORY;
2240 goto fail;
2243 req = cli_nt_delete_on_close_send(frame,
2245 cli,
2246 fnum,
2247 flag);
2248 if (req == NULL) {
2249 status = NT_STATUS_NO_MEMORY;
2250 goto fail;
2253 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2254 goto fail;
2257 status = cli_nt_delete_on_close_recv(req);
2259 fail:
2260 TALLOC_FREE(frame);
2261 return status;
2264 struct cli_ntcreate1_state {
2265 uint16_t vwv[24];
2266 uint16_t fnum;
2267 struct smb_create_returns cr;
2268 struct tevent_req *subreq;
2271 static void cli_ntcreate1_done(struct tevent_req *subreq);
2272 static bool cli_ntcreate1_cancel(struct tevent_req *req);
2274 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
2275 struct tevent_context *ev,
2276 struct cli_state *cli,
2277 const char *fname,
2278 uint32_t CreatFlags,
2279 uint32_t DesiredAccess,
2280 uint32_t FileAttributes,
2281 uint32_t ShareAccess,
2282 uint32_t CreateDisposition,
2283 uint32_t CreateOptions,
2284 uint32_t ImpersonationLevel,
2285 uint8_t SecurityFlags)
2287 struct tevent_req *req, *subreq;
2288 struct cli_ntcreate1_state *state;
2289 uint16_t *vwv;
2290 uint8_t *bytes;
2291 size_t converted_len;
2292 uint16_t additional_flags2 = 0;
2294 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
2295 if (req == NULL) {
2296 return NULL;
2299 vwv = state->vwv;
2301 SCVAL(vwv+0, 0, 0xFF);
2302 SCVAL(vwv+0, 1, 0);
2303 SSVAL(vwv+1, 0, 0);
2304 SCVAL(vwv+2, 0, 0);
2306 if (cli->use_oplocks) {
2307 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
2309 SIVAL(vwv+3, 1, CreatFlags);
2310 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
2311 SIVAL(vwv+7, 1, DesiredAccess);
2312 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
2313 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
2314 SIVAL(vwv+13, 1, FileAttributes);
2315 SIVAL(vwv+15, 1, ShareAccess);
2316 SIVAL(vwv+17, 1, CreateDisposition);
2317 SIVAL(vwv+19, 1, CreateOptions |
2318 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2319 SIVAL(vwv+21, 1, ImpersonationLevel);
2320 SCVAL(vwv+23, 1, SecurityFlags);
2322 bytes = talloc_array(state, uint8_t, 0);
2323 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
2324 fname, strlen(fname)+1,
2325 &converted_len);
2327 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2328 additional_flags2 = FLAGS2_REPARSE_PATH;
2331 /* sigh. this copes with broken netapp filer behaviour */
2332 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
2334 if (tevent_req_nomem(bytes, req)) {
2335 return tevent_req_post(req, ev);
2338 SSVAL(vwv+2, 1, converted_len);
2340 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0,
2341 additional_flags2, 24, vwv,
2342 talloc_get_size(bytes), bytes);
2343 if (tevent_req_nomem(subreq, req)) {
2344 return tevent_req_post(req, ev);
2346 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
2348 state->subreq = subreq;
2349 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
2351 return req;
2354 static void cli_ntcreate1_done(struct tevent_req *subreq)
2356 struct tevent_req *req = tevent_req_callback_data(
2357 subreq, struct tevent_req);
2358 struct cli_ntcreate1_state *state = tevent_req_data(
2359 req, struct cli_ntcreate1_state);
2360 uint8_t wct;
2361 uint16_t *vwv;
2362 uint32_t num_bytes;
2363 uint8_t *bytes;
2364 NTSTATUS status;
2366 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
2367 &num_bytes, &bytes);
2368 TALLOC_FREE(subreq);
2369 if (tevent_req_nterror(req, status)) {
2370 return;
2372 state->cr.oplock_level = CVAL(vwv+2, 0);
2373 state->fnum = SVAL(vwv+2, 1);
2374 state->cr.create_action = IVAL(vwv+3, 1);
2375 state->cr.creation_time = BVAL(vwv+5, 1);
2376 state->cr.last_access_time = BVAL(vwv+9, 1);
2377 state->cr.last_write_time = BVAL(vwv+13, 1);
2378 state->cr.change_time = BVAL(vwv+17, 1);
2379 state->cr.file_attributes = IVAL(vwv+21, 1);
2380 state->cr.allocation_size = BVAL(vwv+23, 1);
2381 state->cr.end_of_file = BVAL(vwv+27, 1);
2383 tevent_req_done(req);
2386 static bool cli_ntcreate1_cancel(struct tevent_req *req)
2388 struct cli_ntcreate1_state *state = tevent_req_data(
2389 req, struct cli_ntcreate1_state);
2390 return tevent_req_cancel(state->subreq);
2393 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
2394 uint16_t *pfnum,
2395 struct smb_create_returns *cr)
2397 struct cli_ntcreate1_state *state = tevent_req_data(
2398 req, struct cli_ntcreate1_state);
2399 NTSTATUS status;
2401 if (tevent_req_is_nterror(req, &status)) {
2402 return status;
2404 *pfnum = state->fnum;
2405 if (cr != NULL) {
2406 *cr = state->cr;
2408 return NT_STATUS_OK;
2411 struct cli_ntcreate_state {
2412 struct smb_create_returns cr;
2413 uint16_t fnum;
2414 struct tevent_req *subreq;
2417 static void cli_ntcreate_done_nt1(struct tevent_req *subreq);
2418 static void cli_ntcreate_done_smb2(struct tevent_req *subreq);
2419 static bool cli_ntcreate_cancel(struct tevent_req *req);
2421 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
2422 struct tevent_context *ev,
2423 struct cli_state *cli,
2424 const char *fname,
2425 uint32_t create_flags,
2426 uint32_t desired_access,
2427 uint32_t file_attributes,
2428 uint32_t share_access,
2429 uint32_t create_disposition,
2430 uint32_t create_options,
2431 uint32_t impersonation_level,
2432 uint8_t security_flags)
2434 struct tevent_req *req, *subreq;
2435 struct cli_ntcreate_state *state;
2437 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2438 if (req == NULL) {
2439 return NULL;
2442 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2443 if (cli->use_oplocks) {
2444 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
2447 subreq = cli_smb2_create_fnum_send(
2448 state,
2450 cli,
2451 fname,
2452 create_flags,
2453 impersonation_level,
2454 desired_access,
2455 file_attributes,
2456 share_access,
2457 create_disposition,
2458 create_options,
2459 NULL);
2460 if (tevent_req_nomem(subreq, req)) {
2461 return tevent_req_post(req, ev);
2463 tevent_req_set_callback(subreq, cli_ntcreate_done_smb2, req);
2464 } else {
2465 subreq = cli_ntcreate1_send(
2466 state, ev, cli, fname, create_flags, desired_access,
2467 file_attributes, share_access, create_disposition,
2468 create_options, impersonation_level, security_flags);
2469 if (tevent_req_nomem(subreq, req)) {
2470 return tevent_req_post(req, ev);
2472 tevent_req_set_callback(subreq, cli_ntcreate_done_nt1, req);
2475 state->subreq = subreq;
2476 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
2478 return req;
2481 static void cli_ntcreate_done_nt1(struct tevent_req *subreq)
2483 struct tevent_req *req = tevent_req_callback_data(
2484 subreq, struct tevent_req);
2485 struct cli_ntcreate_state *state = tevent_req_data(
2486 req, struct cli_ntcreate_state);
2487 NTSTATUS status;
2489 status = cli_ntcreate1_recv(subreq, &state->fnum, &state->cr);
2490 TALLOC_FREE(subreq);
2491 if (tevent_req_nterror(req, status)) {
2492 return;
2494 tevent_req_done(req);
2497 static void cli_ntcreate_done_smb2(struct tevent_req *subreq)
2499 struct tevent_req *req = tevent_req_callback_data(
2500 subreq, struct tevent_req);
2501 struct cli_ntcreate_state *state = tevent_req_data(
2502 req, struct cli_ntcreate_state);
2503 NTSTATUS status;
2505 status = cli_smb2_create_fnum_recv(
2506 subreq,
2507 &state->fnum,
2508 &state->cr,
2509 NULL,
2510 NULL);
2511 TALLOC_FREE(subreq);
2512 if (tevent_req_nterror(req, status)) {
2513 return;
2515 tevent_req_done(req);
2518 static bool cli_ntcreate_cancel(struct tevent_req *req)
2520 struct cli_ntcreate_state *state = tevent_req_data(
2521 req, struct cli_ntcreate_state);
2522 return tevent_req_cancel(state->subreq);
2525 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2526 struct smb_create_returns *cr)
2528 struct cli_ntcreate_state *state = tevent_req_data(
2529 req, struct cli_ntcreate_state);
2530 NTSTATUS status;
2532 if (tevent_req_is_nterror(req, &status)) {
2533 return status;
2535 if (fnum != NULL) {
2536 *fnum = state->fnum;
2538 if (cr != NULL) {
2539 *cr = state->cr;
2541 return NT_STATUS_OK;
2544 NTSTATUS cli_ntcreate(struct cli_state *cli,
2545 const char *fname,
2546 uint32_t CreatFlags,
2547 uint32_t DesiredAccess,
2548 uint32_t FileAttributes,
2549 uint32_t ShareAccess,
2550 uint32_t CreateDisposition,
2551 uint32_t CreateOptions,
2552 uint8_t SecurityFlags,
2553 uint16_t *pfid,
2554 struct smb_create_returns *cr)
2556 TALLOC_CTX *frame = talloc_stackframe();
2557 struct tevent_context *ev;
2558 struct tevent_req *req;
2559 uint32_t ImpersonationLevel = SMB2_IMPERSONATION_IMPERSONATION;
2560 NTSTATUS status = NT_STATUS_NO_MEMORY;
2562 if (smbXcli_conn_has_async_calls(cli->conn)) {
2564 * Can't use sync call while an async call is in flight
2566 status = NT_STATUS_INVALID_PARAMETER;
2567 goto fail;
2570 ev = samba_tevent_context_init(frame);
2571 if (ev == NULL) {
2572 goto fail;
2575 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2576 DesiredAccess, FileAttributes, ShareAccess,
2577 CreateDisposition, CreateOptions,
2578 ImpersonationLevel, SecurityFlags);
2579 if (req == NULL) {
2580 goto fail;
2583 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2584 goto fail;
2587 status = cli_ntcreate_recv(req, pfid, cr);
2588 fail:
2589 TALLOC_FREE(frame);
2590 return status;
2593 struct cli_nttrans_create_state {
2594 uint16_t fnum;
2595 struct smb_create_returns cr;
2598 static void cli_nttrans_create_done(struct tevent_req *subreq);
2600 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2601 struct tevent_context *ev,
2602 struct cli_state *cli,
2603 const char *fname,
2604 uint32_t CreatFlags,
2605 uint32_t DesiredAccess,
2606 uint32_t FileAttributes,
2607 uint32_t ShareAccess,
2608 uint32_t CreateDisposition,
2609 uint32_t CreateOptions,
2610 uint8_t SecurityFlags,
2611 struct security_descriptor *secdesc,
2612 struct ea_struct *eas,
2613 int num_eas)
2615 struct tevent_req *req, *subreq;
2616 struct cli_nttrans_create_state *state;
2617 uint8_t *param;
2618 uint8_t *secdesc_buf;
2619 size_t secdesc_len;
2620 NTSTATUS status;
2621 size_t converted_len;
2622 uint16_t additional_flags2 = 0;
2624 req = tevent_req_create(mem_ctx,
2625 &state, struct cli_nttrans_create_state);
2626 if (req == NULL) {
2627 return NULL;
2630 if (secdesc != NULL) {
2631 status = marshall_sec_desc(talloc_tos(), secdesc,
2632 &secdesc_buf, &secdesc_len);
2633 if (tevent_req_nterror(req, status)) {
2634 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2635 nt_errstr(status)));
2636 return tevent_req_post(req, ev);
2638 } else {
2639 secdesc_buf = NULL;
2640 secdesc_len = 0;
2643 if (num_eas != 0) {
2645 * TODO ;-)
2647 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2648 return tevent_req_post(req, ev);
2651 param = talloc_array(state, uint8_t, 53);
2652 if (tevent_req_nomem(param, req)) {
2653 return tevent_req_post(req, ev);
2656 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2657 fname, strlen(fname),
2658 &converted_len);
2659 if (tevent_req_nomem(param, req)) {
2660 return tevent_req_post(req, ev);
2663 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2664 additional_flags2 = FLAGS2_REPARSE_PATH;
2667 SIVAL(param, 0, CreatFlags);
2668 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2669 SIVAL(param, 8, DesiredAccess);
2670 SIVAL(param, 12, 0x0); /* AllocationSize */
2671 SIVAL(param, 16, 0x0); /* AllocationSize */
2672 SIVAL(param, 20, FileAttributes);
2673 SIVAL(param, 24, ShareAccess);
2674 SIVAL(param, 28, CreateDisposition);
2675 SIVAL(param, 32, CreateOptions |
2676 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2677 SIVAL(param, 36, secdesc_len);
2678 SIVAL(param, 40, 0); /* EA length*/
2679 SIVAL(param, 44, converted_len);
2680 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2681 SCVAL(param, 52, SecurityFlags);
2683 subreq = cli_trans_send(state, ev, cli,
2684 additional_flags2, /* additional_flags2 */
2685 SMBnttrans,
2686 NULL, -1, /* name, fid */
2687 NT_TRANSACT_CREATE, 0,
2688 NULL, 0, 0, /* setup */
2689 param, talloc_get_size(param), 128, /* param */
2690 secdesc_buf, secdesc_len, 0); /* data */
2691 if (tevent_req_nomem(subreq, req)) {
2692 return tevent_req_post(req, ev);
2694 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2695 return req;
2698 static void cli_nttrans_create_done(struct tevent_req *subreq)
2700 struct tevent_req *req = tevent_req_callback_data(
2701 subreq, struct tevent_req);
2702 struct cli_nttrans_create_state *state = tevent_req_data(
2703 req, struct cli_nttrans_create_state);
2704 uint8_t *param;
2705 uint32_t num_param;
2706 NTSTATUS status;
2708 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2709 NULL, 0, NULL, /* rsetup */
2710 &param, 69, &num_param,
2711 NULL, 0, NULL);
2712 if (tevent_req_nterror(req, status)) {
2713 return;
2715 state->cr.oplock_level = CVAL(param, 0);
2716 state->fnum = SVAL(param, 2);
2717 state->cr.create_action = IVAL(param, 4);
2718 state->cr.creation_time = BVAL(param, 12);
2719 state->cr.last_access_time = BVAL(param, 20);
2720 state->cr.last_write_time = BVAL(param, 28);
2721 state->cr.change_time = BVAL(param, 36);
2722 state->cr.file_attributes = IVAL(param, 44);
2723 state->cr.allocation_size = BVAL(param, 48);
2724 state->cr.end_of_file = BVAL(param, 56);
2726 TALLOC_FREE(param);
2727 tevent_req_done(req);
2730 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2731 uint16_t *fnum,
2732 struct smb_create_returns *cr)
2734 struct cli_nttrans_create_state *state = tevent_req_data(
2735 req, struct cli_nttrans_create_state);
2736 NTSTATUS status;
2738 if (tevent_req_is_nterror(req, &status)) {
2739 return status;
2741 *fnum = state->fnum;
2742 if (cr != NULL) {
2743 *cr = state->cr;
2745 return NT_STATUS_OK;
2748 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2749 const char *fname,
2750 uint32_t CreatFlags,
2751 uint32_t DesiredAccess,
2752 uint32_t FileAttributes,
2753 uint32_t ShareAccess,
2754 uint32_t CreateDisposition,
2755 uint32_t CreateOptions,
2756 uint8_t SecurityFlags,
2757 struct security_descriptor *secdesc,
2758 struct ea_struct *eas,
2759 int num_eas,
2760 uint16_t *pfid,
2761 struct smb_create_returns *cr)
2763 TALLOC_CTX *frame = talloc_stackframe();
2764 struct tevent_context *ev;
2765 struct tevent_req *req;
2766 NTSTATUS status = NT_STATUS_NO_MEMORY;
2768 if (smbXcli_conn_has_async_calls(cli->conn)) {
2770 * Can't use sync call while an async call is in flight
2772 status = NT_STATUS_INVALID_PARAMETER;
2773 goto fail;
2775 ev = samba_tevent_context_init(frame);
2776 if (ev == NULL) {
2777 goto fail;
2779 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2780 DesiredAccess, FileAttributes,
2781 ShareAccess, CreateDisposition,
2782 CreateOptions, SecurityFlags,
2783 secdesc, eas, num_eas);
2784 if (req == NULL) {
2785 goto fail;
2787 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2788 goto fail;
2790 status = cli_nttrans_create_recv(req, pfid, cr);
2791 fail:
2792 TALLOC_FREE(frame);
2793 return status;
2796 /****************************************************************************
2797 Open a file
2798 WARNING: if you open with O_WRONLY then getattrE won't work!
2799 ****************************************************************************/
2801 struct cli_openx_state {
2802 const char *fname;
2803 uint16_t vwv[15];
2804 uint16_t fnum;
2805 struct iovec bytes;
2808 static void cli_openx_done(struct tevent_req *subreq);
2810 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2811 struct tevent_context *ev,
2812 struct cli_state *cli, const char *fname,
2813 int flags, int share_mode,
2814 struct tevent_req **psmbreq)
2816 struct tevent_req *req, *subreq;
2817 struct cli_openx_state *state;
2818 unsigned openfn;
2819 unsigned accessmode;
2820 uint8_t additional_flags;
2821 uint16_t additional_flags2 = 0;
2822 uint8_t *bytes;
2824 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2825 if (req == NULL) {
2826 return NULL;
2829 openfn = 0;
2830 if (flags & O_CREAT) {
2831 openfn |= (1<<4);
2833 if (!(flags & O_EXCL)) {
2834 if (flags & O_TRUNC)
2835 openfn |= (1<<1);
2836 else
2837 openfn |= (1<<0);
2840 accessmode = (share_mode<<4);
2842 if ((flags & O_ACCMODE) == O_RDWR) {
2843 accessmode |= 2;
2844 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2845 accessmode |= 1;
2848 #if defined(O_SYNC)
2849 if ((flags & O_SYNC) == O_SYNC) {
2850 accessmode |= (1<<14);
2852 #endif /* O_SYNC */
2854 if (share_mode == DENY_FCB) {
2855 accessmode = 0xFF;
2858 SCVAL(state->vwv + 0, 0, 0xFF);
2859 SCVAL(state->vwv + 0, 1, 0);
2860 SSVAL(state->vwv + 1, 0, 0);
2861 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2862 SSVAL(state->vwv + 3, 0, accessmode);
2863 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2864 SSVAL(state->vwv + 5, 0, 0);
2865 SIVAL(state->vwv + 6, 0, 0);
2866 SSVAL(state->vwv + 8, 0, openfn);
2867 SIVAL(state->vwv + 9, 0, 0);
2868 SIVAL(state->vwv + 11, 0, 0);
2869 SIVAL(state->vwv + 13, 0, 0);
2871 additional_flags = 0;
2873 if (cli->use_oplocks) {
2874 /* if using oplocks then ask for a batch oplock via
2875 core and extended methods */
2876 additional_flags =
2877 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2878 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2881 bytes = talloc_array(state, uint8_t, 0);
2882 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2883 strlen(fname)+1, NULL);
2885 if (tevent_req_nomem(bytes, req)) {
2886 return tevent_req_post(req, ev);
2889 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
2890 additional_flags2 = FLAGS2_REPARSE_PATH;
2893 state->bytes.iov_base = (void *)bytes;
2894 state->bytes.iov_len = talloc_get_size(bytes);
2896 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2897 additional_flags2, 15, state->vwv, 1, &state->bytes);
2898 if (subreq == NULL) {
2899 TALLOC_FREE(req);
2900 return NULL;
2902 tevent_req_set_callback(subreq, cli_openx_done, req);
2903 *psmbreq = subreq;
2904 return req;
2907 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2908 struct cli_state *cli, const char *fname,
2909 int flags, int share_mode)
2911 struct tevent_req *req, *subreq;
2912 NTSTATUS status;
2914 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2915 &subreq);
2916 if (req == NULL) {
2917 return NULL;
2920 status = smb1cli_req_chain_submit(&subreq, 1);
2921 if (tevent_req_nterror(req, status)) {
2922 return tevent_req_post(req, ev);
2924 return req;
2927 static void cli_openx_done(struct tevent_req *subreq)
2929 struct tevent_req *req = tevent_req_callback_data(
2930 subreq, struct tevent_req);
2931 struct cli_openx_state *state = tevent_req_data(
2932 req, struct cli_openx_state);
2933 uint8_t wct;
2934 uint16_t *vwv;
2935 NTSTATUS status;
2937 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2938 NULL);
2939 TALLOC_FREE(subreq);
2940 if (tevent_req_nterror(req, status)) {
2941 return;
2943 state->fnum = SVAL(vwv+2, 0);
2944 tevent_req_done(req);
2947 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2949 struct cli_openx_state *state = tevent_req_data(
2950 req, struct cli_openx_state);
2951 NTSTATUS status;
2953 if (tevent_req_is_nterror(req, &status)) {
2954 return status;
2956 *pfnum = state->fnum;
2957 return NT_STATUS_OK;
2960 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2961 int share_mode, uint16_t *pfnum)
2963 TALLOC_CTX *frame = talloc_stackframe();
2964 struct tevent_context *ev;
2965 struct tevent_req *req;
2966 NTSTATUS status = NT_STATUS_NO_MEMORY;
2968 if (smbXcli_conn_has_async_calls(cli->conn)) {
2970 * Can't use sync call while an async call is in flight
2972 status = NT_STATUS_INVALID_PARAMETER;
2973 goto fail;
2976 ev = samba_tevent_context_init(frame);
2977 if (ev == NULL) {
2978 goto fail;
2981 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2982 if (req == NULL) {
2983 goto fail;
2986 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2987 goto fail;
2990 status = cli_openx_recv(req, pfnum);
2991 fail:
2992 TALLOC_FREE(frame);
2993 return status;
2995 /****************************************************************************
2996 Synchronous wrapper function that does an NtCreateX open by preference
2997 and falls back to openX if this fails.
2998 ****************************************************************************/
3000 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
3001 int share_mode_in, uint16_t *pfnum)
3003 NTSTATUS status;
3004 unsigned int openfn = 0;
3005 unsigned int dos_deny = 0;
3006 uint32_t access_mask, share_mode, create_disposition, create_options;
3007 struct smb_create_returns cr;
3009 /* Do the initial mapping into OpenX parameters. */
3010 if (flags & O_CREAT) {
3011 openfn |= (1<<4);
3013 if (!(flags & O_EXCL)) {
3014 if (flags & O_TRUNC)
3015 openfn |= (1<<1);
3016 else
3017 openfn |= (1<<0);
3020 dos_deny = (share_mode_in<<4);
3022 if ((flags & O_ACCMODE) == O_RDWR) {
3023 dos_deny |= 2;
3024 } else if ((flags & O_ACCMODE) == O_WRONLY) {
3025 dos_deny |= 1;
3028 #if defined(O_SYNC)
3029 if ((flags & O_SYNC) == O_SYNC) {
3030 dos_deny |= (1<<14);
3032 #endif /* O_SYNC */
3034 if (share_mode_in == DENY_FCB) {
3035 dos_deny = 0xFF;
3038 #if 0
3039 /* Hmmm. This is what I think the above code
3040 should look like if it's using the constants
3041 we #define. JRA. */
3043 if (flags & O_CREAT) {
3044 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
3046 if (!(flags & O_EXCL)) {
3047 if (flags & O_TRUNC)
3048 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
3049 else
3050 openfn |= OPENX_FILE_EXISTS_OPEN;
3053 dos_deny = SET_DENY_MODE(share_mode_in);
3055 if ((flags & O_ACCMODE) == O_RDWR) {
3056 dos_deny |= DOS_OPEN_RDWR;
3057 } else if ((flags & O_ACCMODE) == O_WRONLY) {
3058 dos_deny |= DOS_OPEN_WRONLY;
3061 #if defined(O_SYNC)
3062 if ((flags & O_SYNC) == O_SYNC) {
3063 dos_deny |= FILE_SYNC_OPENMODE;
3065 #endif /* O_SYNC */
3067 if (share_mode_in == DENY_FCB) {
3068 dos_deny = 0xFF;
3070 #endif
3072 if (!map_open_params_to_ntcreate(fname, dos_deny,
3073 openfn, &access_mask,
3074 &share_mode, &create_disposition,
3075 &create_options, NULL)) {
3076 goto try_openx;
3079 status = cli_ntcreate(cli,
3080 fname,
3082 access_mask,
3084 share_mode,
3085 create_disposition,
3086 create_options,
3088 pfnum,
3089 &cr);
3091 /* Try and cope will all varients of "we don't do this call"
3092 and fall back to openX. */
3094 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
3095 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
3096 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
3097 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
3098 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
3099 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
3100 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
3101 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
3102 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
3103 goto try_openx;
3106 if (NT_STATUS_IS_OK(status) &&
3107 (create_options & FILE_NON_DIRECTORY_FILE) &&
3108 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
3111 * Some (broken) servers return a valid handle
3112 * for directories even if FILE_NON_DIRECTORY_FILE
3113 * is set. Just close the handle and set the
3114 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
3116 status = cli_close(cli, *pfnum);
3117 if (!NT_STATUS_IS_OK(status)) {
3118 return status;
3120 status = NT_STATUS_FILE_IS_A_DIRECTORY;
3121 /* Set this so libsmbclient can retrieve it. */
3122 cli->raw_status = status;
3125 return status;
3127 try_openx:
3129 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
3132 /****************************************************************************
3133 Close a file.
3134 ****************************************************************************/
3136 struct cli_smb1_close_state {
3137 uint16_t vwv[3];
3140 static void cli_smb1_close_done(struct tevent_req *subreq);
3142 struct tevent_req *cli_smb1_close_create(TALLOC_CTX *mem_ctx,
3143 struct tevent_context *ev,
3144 struct cli_state *cli,
3145 uint16_t fnum,
3146 struct tevent_req **psubreq)
3148 struct tevent_req *req, *subreq;
3149 struct cli_smb1_close_state *state;
3151 req = tevent_req_create(mem_ctx, &state, struct cli_smb1_close_state);
3152 if (req == NULL) {
3153 return NULL;
3156 SSVAL(state->vwv+0, 0, fnum);
3157 SIVALS(state->vwv+1, 0, -1);
3159 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 0,
3160 3, state->vwv, 0, NULL);
3161 if (subreq == NULL) {
3162 TALLOC_FREE(req);
3163 return NULL;
3165 tevent_req_set_callback(subreq, cli_smb1_close_done, req);
3166 *psubreq = subreq;
3167 return req;
3170 static void cli_smb1_close_done(struct tevent_req *subreq)
3172 struct tevent_req *req = tevent_req_callback_data(
3173 subreq, struct tevent_req);
3174 NTSTATUS status;
3176 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3177 TALLOC_FREE(subreq);
3178 if (tevent_req_nterror(req, status)) {
3179 return;
3181 tevent_req_done(req);
3184 struct cli_close_state {
3185 int dummy;
3188 static void cli_close_done(struct tevent_req *subreq);
3190 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
3191 struct tevent_context *ev,
3192 struct cli_state *cli,
3193 uint16_t fnum)
3195 struct tevent_req *req, *subreq;
3196 struct cli_close_state *state;
3197 NTSTATUS status;
3199 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
3200 if (req == NULL) {
3201 return NULL;
3204 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3205 subreq = cli_smb2_close_fnum_send(state,
3207 cli,
3208 fnum);
3209 if (tevent_req_nomem(subreq, req)) {
3210 return tevent_req_post(req, ev);
3212 } else {
3213 struct tevent_req *ch_req = NULL;
3214 subreq = cli_smb1_close_create(state, ev, cli, fnum, &ch_req);
3215 if (tevent_req_nomem(subreq, req)) {
3216 return tevent_req_post(req, ev);
3218 status = smb1cli_req_chain_submit(&ch_req, 1);
3219 if (tevent_req_nterror(req, status)) {
3220 return tevent_req_post(req, ev);
3224 tevent_req_set_callback(subreq, cli_close_done, req);
3225 return req;
3228 static void cli_close_done(struct tevent_req *subreq)
3230 struct tevent_req *req = tevent_req_callback_data(
3231 subreq, struct tevent_req);
3232 NTSTATUS status = NT_STATUS_OK;
3233 bool err = tevent_req_is_nterror(subreq, &status);
3235 TALLOC_FREE(subreq);
3236 if (err) {
3237 tevent_req_nterror(req, status);
3238 return;
3240 tevent_req_done(req);
3243 NTSTATUS cli_close_recv(struct tevent_req *req)
3245 return tevent_req_simple_recv_ntstatus(req);
3248 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
3250 TALLOC_CTX *frame = NULL;
3251 struct tevent_context *ev;
3252 struct tevent_req *req;
3253 NTSTATUS status = NT_STATUS_OK;
3255 frame = talloc_stackframe();
3257 if (smbXcli_conn_has_async_calls(cli->conn)) {
3259 * Can't use sync call while an async call is in flight
3261 status = NT_STATUS_INVALID_PARAMETER;
3262 goto fail;
3265 ev = samba_tevent_context_init(frame);
3266 if (ev == NULL) {
3267 status = NT_STATUS_NO_MEMORY;
3268 goto fail;
3271 req = cli_close_send(frame, ev, cli, fnum);
3272 if (req == NULL) {
3273 status = NT_STATUS_NO_MEMORY;
3274 goto fail;
3277 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3278 goto fail;
3281 status = cli_close_recv(req);
3282 fail:
3283 TALLOC_FREE(frame);
3284 return status;
3287 /****************************************************************************
3288 Truncate a file to a specified size
3289 ****************************************************************************/
3291 struct ftrunc_state {
3292 uint16_t setup;
3293 uint8_t param[6];
3294 uint8_t data[8];
3297 static void cli_ftruncate_done(struct tevent_req *subreq)
3299 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3300 NULL, 0, NULL, NULL, 0, NULL);
3301 tevent_req_simple_finish_ntstatus(subreq, status);
3304 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
3305 struct tevent_context *ev,
3306 struct cli_state *cli,
3307 uint16_t fnum,
3308 uint64_t size)
3310 struct tevent_req *req = NULL, *subreq = NULL;
3311 struct ftrunc_state *state = NULL;
3313 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
3314 if (req == NULL) {
3315 return NULL;
3318 /* Setup setup word. */
3319 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3321 /* Setup param array. */
3322 SSVAL(state->param,0,fnum);
3323 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
3324 SSVAL(state->param,4,0);
3326 /* Setup data array. */
3327 SBVAL(state->data, 0, size);
3329 subreq = cli_trans_send(state, /* mem ctx. */
3330 ev, /* event ctx. */
3331 cli, /* cli_state. */
3332 0, /* additional_flags2 */
3333 SMBtrans2, /* cmd. */
3334 NULL, /* pipe name. */
3335 -1, /* fid. */
3336 0, /* function. */
3337 0, /* flags. */
3338 &state->setup, /* setup. */
3339 1, /* num setup uint16_t words. */
3340 0, /* max returned setup. */
3341 state->param, /* param. */
3342 6, /* num param. */
3343 2, /* max returned param. */
3344 state->data, /* data. */
3345 8, /* num data. */
3346 0); /* max returned data. */
3348 if (tevent_req_nomem(subreq, req)) {
3349 return tevent_req_post(req, ev);
3351 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
3352 return req;
3355 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
3357 return tevent_req_simple_recv_ntstatus(req);
3360 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
3362 TALLOC_CTX *frame = NULL;
3363 struct tevent_context *ev = NULL;
3364 struct tevent_req *req = NULL;
3365 NTSTATUS status = NT_STATUS_OK;
3367 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3368 return cli_smb2_ftruncate(cli, fnum, size);
3371 frame = talloc_stackframe();
3373 if (smbXcli_conn_has_async_calls(cli->conn)) {
3375 * Can't use sync call while an async call is in flight
3377 status = NT_STATUS_INVALID_PARAMETER;
3378 goto fail;
3381 ev = samba_tevent_context_init(frame);
3382 if (ev == NULL) {
3383 status = NT_STATUS_NO_MEMORY;
3384 goto fail;
3387 req = cli_ftruncate_send(frame,
3389 cli,
3390 fnum,
3391 size);
3392 if (req == NULL) {
3393 status = NT_STATUS_NO_MEMORY;
3394 goto fail;
3397 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3398 goto fail;
3401 status = cli_ftruncate_recv(req);
3403 fail:
3404 TALLOC_FREE(frame);
3405 return status;
3408 /****************************************************************************
3409 send a lock with a specified locktype
3410 this is used for testing LOCKING_ANDX_CANCEL_LOCK
3411 ****************************************************************************/
3413 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
3414 uint32_t offset, uint32_t len,
3415 int timeout, unsigned char locktype)
3417 uint16_t vwv[8];
3418 uint8_t bytes[10];
3419 NTSTATUS status;
3420 unsigned int set_timeout = 0;
3421 unsigned int saved_timeout = 0;
3423 SCVAL(vwv + 0, 0, 0xff);
3424 SCVAL(vwv + 0, 1, 0);
3425 SSVAL(vwv + 1, 0, 0);
3426 SSVAL(vwv + 2, 0, fnum);
3427 SCVAL(vwv + 3, 0, locktype);
3428 SCVAL(vwv + 3, 1, 0);
3429 SIVALS(vwv + 4, 0, timeout);
3430 SSVAL(vwv + 6, 0, 0);
3431 SSVAL(vwv + 7, 0, 1);
3433 SSVAL(bytes, 0, cli_getpid(cli));
3434 SIVAL(bytes, 2, offset);
3435 SIVAL(bytes, 6, len);
3437 if (timeout != 0) {
3438 if (timeout == -1) {
3439 set_timeout = 0x7FFFFFFF;
3440 } else {
3441 set_timeout = timeout + 2*1000;
3443 saved_timeout = cli_set_timeout(cli, set_timeout);
3446 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3447 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3449 if (saved_timeout != 0) {
3450 cli_set_timeout(cli, saved_timeout);
3453 return status;
3456 /****************************************************************************
3457 Lock a file.
3458 note that timeout is in units of 2 milliseconds
3459 ****************************************************************************/
3461 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
3462 uint32_t offset, uint32_t len, int timeout,
3463 enum brl_type lock_type)
3465 NTSTATUS status;
3467 status = cli_locktype(cli, fnum, offset, len, timeout,
3468 (lock_type == READ_LOCK? 1 : 0));
3469 return status;
3472 /****************************************************************************
3473 Unlock a file.
3474 ****************************************************************************/
3476 struct cli_unlock_state {
3477 uint16_t vwv[8];
3478 uint8_t data[10];
3481 static void cli_unlock_done(struct tevent_req *subreq);
3483 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
3484 struct tevent_context *ev,
3485 struct cli_state *cli,
3486 uint16_t fnum,
3487 uint64_t offset,
3488 uint64_t len)
3491 struct tevent_req *req = NULL, *subreq = NULL;
3492 struct cli_unlock_state *state = NULL;
3493 uint8_t additional_flags = 0;
3495 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
3496 if (req == NULL) {
3497 return NULL;
3500 SCVAL(state->vwv+0, 0, 0xFF);
3501 SSVAL(state->vwv+2, 0, fnum);
3502 SCVAL(state->vwv+3, 0, 0);
3503 SIVALS(state->vwv+4, 0, 0);
3504 SSVAL(state->vwv+6, 0, 1);
3505 SSVAL(state->vwv+7, 0, 0);
3507 SSVAL(state->data, 0, cli_getpid(cli));
3508 SIVAL(state->data, 2, offset);
3509 SIVAL(state->data, 6, len);
3511 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3512 8, state->vwv, 10, state->data);
3513 if (tevent_req_nomem(subreq, req)) {
3514 return tevent_req_post(req, ev);
3516 tevent_req_set_callback(subreq, cli_unlock_done, req);
3517 return req;
3520 static void cli_unlock_done(struct tevent_req *subreq)
3522 struct tevent_req *req = tevent_req_callback_data(
3523 subreq, struct tevent_req);
3524 NTSTATUS status;
3526 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3527 TALLOC_FREE(subreq);
3528 if (tevent_req_nterror(req, status)) {
3529 return;
3531 tevent_req_done(req);
3534 NTSTATUS cli_unlock_recv(struct tevent_req *req)
3536 return tevent_req_simple_recv_ntstatus(req);
3539 NTSTATUS cli_unlock(struct cli_state *cli,
3540 uint16_t fnum,
3541 uint32_t offset,
3542 uint32_t len)
3544 TALLOC_CTX *frame = talloc_stackframe();
3545 struct tevent_context *ev;
3546 struct tevent_req *req;
3547 NTSTATUS status = NT_STATUS_OK;
3549 if (smbXcli_conn_has_async_calls(cli->conn)) {
3551 * Can't use sync call while an async call is in flight
3553 status = NT_STATUS_INVALID_PARAMETER;
3554 goto fail;
3557 ev = samba_tevent_context_init(frame);
3558 if (ev == NULL) {
3559 status = NT_STATUS_NO_MEMORY;
3560 goto fail;
3563 req = cli_unlock_send(frame, ev, cli,
3564 fnum, offset, len);
3565 if (req == NULL) {
3566 status = NT_STATUS_NO_MEMORY;
3567 goto fail;
3570 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3571 goto fail;
3574 status = cli_unlock_recv(req);
3576 fail:
3577 TALLOC_FREE(frame);
3578 return status;
3581 /****************************************************************************
3582 Lock a file with 64 bit offsets.
3583 ****************************************************************************/
3585 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3586 uint64_t offset, uint64_t len, int timeout,
3587 enum brl_type lock_type)
3589 uint16_t vwv[8];
3590 uint8_t bytes[20];
3591 unsigned int set_timeout = 0;
3592 unsigned int saved_timeout = 0;
3593 int ltype;
3594 NTSTATUS status;
3596 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3597 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3600 ltype = (lock_type == READ_LOCK? 1 : 0);
3601 ltype |= LOCKING_ANDX_LARGE_FILES;
3603 SCVAL(vwv + 0, 0, 0xff);
3604 SCVAL(vwv + 0, 1, 0);
3605 SSVAL(vwv + 1, 0, 0);
3606 SSVAL(vwv + 2, 0, fnum);
3607 SCVAL(vwv + 3, 0, ltype);
3608 SCVAL(vwv + 3, 1, 0);
3609 SIVALS(vwv + 4, 0, timeout);
3610 SSVAL(vwv + 6, 0, 0);
3611 SSVAL(vwv + 7, 0, 1);
3613 SIVAL(bytes, 0, cli_getpid(cli));
3614 SOFF_T_R(bytes, 4, offset);
3615 SOFF_T_R(bytes, 12, len);
3617 if (timeout != 0) {
3618 if (timeout == -1) {
3619 set_timeout = 0x7FFFFFFF;
3620 } else {
3621 set_timeout = timeout + 2*1000;
3623 saved_timeout = cli_set_timeout(cli, set_timeout);
3626 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3627 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3629 if (saved_timeout != 0) {
3630 cli_set_timeout(cli, saved_timeout);
3633 return status;
3636 /****************************************************************************
3637 Unlock a file with 64 bit offsets.
3638 ****************************************************************************/
3640 struct cli_unlock64_state {
3641 uint16_t vwv[8];
3642 uint8_t data[20];
3645 static void cli_unlock64_done(struct tevent_req *subreq);
3647 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3648 struct tevent_context *ev,
3649 struct cli_state *cli,
3650 uint16_t fnum,
3651 uint64_t offset,
3652 uint64_t len)
3655 struct tevent_req *req = NULL, *subreq = NULL;
3656 struct cli_unlock64_state *state = NULL;
3657 uint8_t additional_flags = 0;
3659 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3660 if (req == NULL) {
3661 return NULL;
3664 SCVAL(state->vwv+0, 0, 0xff);
3665 SSVAL(state->vwv+2, 0, fnum);
3666 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3667 SIVALS(state->vwv+4, 0, 0);
3668 SSVAL(state->vwv+6, 0, 1);
3669 SSVAL(state->vwv+7, 0, 0);
3671 SIVAL(state->data, 0, cli_getpid(cli));
3672 SOFF_T_R(state->data, 4, offset);
3673 SOFF_T_R(state->data, 12, len);
3675 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3676 8, state->vwv, 20, state->data);
3677 if (tevent_req_nomem(subreq, req)) {
3678 return tevent_req_post(req, ev);
3680 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3681 return req;
3684 static void cli_unlock64_done(struct tevent_req *subreq)
3686 struct tevent_req *req = tevent_req_callback_data(
3687 subreq, struct tevent_req);
3688 NTSTATUS status;
3690 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3691 TALLOC_FREE(subreq);
3692 if (tevent_req_nterror(req, status)) {
3693 return;
3695 tevent_req_done(req);
3698 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3700 return tevent_req_simple_recv_ntstatus(req);
3703 NTSTATUS cli_unlock64(struct cli_state *cli,
3704 uint16_t fnum,
3705 uint64_t offset,
3706 uint64_t len)
3708 TALLOC_CTX *frame = talloc_stackframe();
3709 struct tevent_context *ev;
3710 struct tevent_req *req;
3711 NTSTATUS status = NT_STATUS_OK;
3713 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3714 return cli_unlock(cli, fnum, offset, len);
3717 if (smbXcli_conn_has_async_calls(cli->conn)) {
3719 * Can't use sync call while an async call is in flight
3721 status = NT_STATUS_INVALID_PARAMETER;
3722 goto fail;
3725 ev = samba_tevent_context_init(frame);
3726 if (ev == NULL) {
3727 status = NT_STATUS_NO_MEMORY;
3728 goto fail;
3731 req = cli_unlock64_send(frame, ev, cli,
3732 fnum, offset, len);
3733 if (req == NULL) {
3734 status = NT_STATUS_NO_MEMORY;
3735 goto fail;
3738 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3739 goto fail;
3742 status = cli_unlock64_recv(req);
3744 fail:
3745 TALLOC_FREE(frame);
3746 return status;
3749 /****************************************************************************
3750 Get/unlock a POSIX lock on a file - internal function.
3751 ****************************************************************************/
3753 struct posix_lock_state {
3754 uint16_t setup;
3755 uint8_t param[4];
3756 uint8_t data[POSIX_LOCK_DATA_SIZE];
3759 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3761 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3762 NULL, 0, NULL, NULL, 0, NULL);
3763 tevent_req_simple_finish_ntstatus(subreq, status);
3766 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3767 struct tevent_context *ev,
3768 struct cli_state *cli,
3769 uint16_t fnum,
3770 uint64_t offset,
3771 uint64_t len,
3772 bool wait_lock,
3773 enum brl_type lock_type)
3775 struct tevent_req *req = NULL, *subreq = NULL;
3776 struct posix_lock_state *state = NULL;
3778 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3779 if (req == NULL) {
3780 return NULL;
3783 /* Setup setup word. */
3784 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3786 /* Setup param array. */
3787 SSVAL(&state->param, 0, fnum);
3788 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3790 /* Setup data array. */
3791 switch (lock_type) {
3792 case READ_LOCK:
3793 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3794 POSIX_LOCK_TYPE_READ);
3795 break;
3796 case WRITE_LOCK:
3797 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3798 POSIX_LOCK_TYPE_WRITE);
3799 break;
3800 case UNLOCK_LOCK:
3801 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3802 POSIX_LOCK_TYPE_UNLOCK);
3803 break;
3804 default:
3805 return NULL;
3808 if (wait_lock) {
3809 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3810 POSIX_LOCK_FLAG_WAIT);
3811 } else {
3812 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3813 POSIX_LOCK_FLAG_NOWAIT);
3816 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3817 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3818 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3820 subreq = cli_trans_send(state, /* mem ctx. */
3821 ev, /* event ctx. */
3822 cli, /* cli_state. */
3823 0, /* additional_flags2 */
3824 SMBtrans2, /* cmd. */
3825 NULL, /* pipe name. */
3826 -1, /* fid. */
3827 0, /* function. */
3828 0, /* flags. */
3829 &state->setup, /* setup. */
3830 1, /* num setup uint16_t words. */
3831 0, /* max returned setup. */
3832 state->param, /* param. */
3833 4, /* num param. */
3834 2, /* max returned param. */
3835 state->data, /* data. */
3836 POSIX_LOCK_DATA_SIZE, /* num data. */
3837 0); /* max returned data. */
3839 if (tevent_req_nomem(subreq, req)) {
3840 return tevent_req_post(req, ev);
3842 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3843 return req;
3846 /****************************************************************************
3847 POSIX Lock a file.
3848 ****************************************************************************/
3850 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3851 struct tevent_context *ev,
3852 struct cli_state *cli,
3853 uint16_t fnum,
3854 uint64_t offset,
3855 uint64_t len,
3856 bool wait_lock,
3857 enum brl_type lock_type)
3859 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3860 wait_lock, lock_type);
3863 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3865 return tevent_req_simple_recv_ntstatus(req);
3868 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3869 uint64_t offset, uint64_t len,
3870 bool wait_lock, enum brl_type lock_type)
3872 TALLOC_CTX *frame = talloc_stackframe();
3873 struct tevent_context *ev = NULL;
3874 struct tevent_req *req = NULL;
3875 NTSTATUS status = NT_STATUS_OK;
3877 if (smbXcli_conn_has_async_calls(cli->conn)) {
3879 * Can't use sync call while an async call is in flight
3881 status = NT_STATUS_INVALID_PARAMETER;
3882 goto fail;
3885 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3886 status = NT_STATUS_INVALID_PARAMETER;
3887 goto fail;
3890 ev = samba_tevent_context_init(frame);
3891 if (ev == NULL) {
3892 status = NT_STATUS_NO_MEMORY;
3893 goto fail;
3896 req = cli_posix_lock_send(frame,
3898 cli,
3899 fnum,
3900 offset,
3901 len,
3902 wait_lock,
3903 lock_type);
3904 if (req == NULL) {
3905 status = NT_STATUS_NO_MEMORY;
3906 goto fail;
3909 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3910 goto fail;
3913 status = cli_posix_lock_recv(req);
3915 fail:
3916 TALLOC_FREE(frame);
3917 return status;
3920 /****************************************************************************
3921 POSIX Unlock a file.
3922 ****************************************************************************/
3924 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3925 struct tevent_context *ev,
3926 struct cli_state *cli,
3927 uint16_t fnum,
3928 uint64_t offset,
3929 uint64_t len)
3931 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3932 false, UNLOCK_LOCK);
3935 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3937 return tevent_req_simple_recv_ntstatus(req);
3940 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3942 TALLOC_CTX *frame = talloc_stackframe();
3943 struct tevent_context *ev = NULL;
3944 struct tevent_req *req = NULL;
3945 NTSTATUS status = NT_STATUS_OK;
3947 if (smbXcli_conn_has_async_calls(cli->conn)) {
3949 * Can't use sync call while an async call is in flight
3951 status = NT_STATUS_INVALID_PARAMETER;
3952 goto fail;
3955 ev = samba_tevent_context_init(frame);
3956 if (ev == NULL) {
3957 status = NT_STATUS_NO_MEMORY;
3958 goto fail;
3961 req = cli_posix_unlock_send(frame,
3963 cli,
3964 fnum,
3965 offset,
3966 len);
3967 if (req == NULL) {
3968 status = NT_STATUS_NO_MEMORY;
3969 goto fail;
3972 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3973 goto fail;
3976 status = cli_posix_unlock_recv(req);
3978 fail:
3979 TALLOC_FREE(frame);
3980 return status;
3983 /****************************************************************************
3984 Do a SMBgetattrE call.
3985 ****************************************************************************/
3987 static void cli_getattrE_done(struct tevent_req *subreq);
3989 struct cli_getattrE_state {
3990 uint16_t vwv[1];
3991 int zone_offset;
3992 uint16_t attr;
3993 off_t size;
3994 time_t change_time;
3995 time_t access_time;
3996 time_t write_time;
3999 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
4000 struct tevent_context *ev,
4001 struct cli_state *cli,
4002 uint16_t fnum)
4004 struct tevent_req *req = NULL, *subreq = NULL;
4005 struct cli_getattrE_state *state = NULL;
4006 uint8_t additional_flags = 0;
4008 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
4009 if (req == NULL) {
4010 return NULL;
4013 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
4014 SSVAL(state->vwv+0,0,fnum);
4016 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags, 0,
4017 1, state->vwv, 0, NULL);
4018 if (tevent_req_nomem(subreq, req)) {
4019 return tevent_req_post(req, ev);
4021 tevent_req_set_callback(subreq, cli_getattrE_done, req);
4022 return req;
4025 static void cli_getattrE_done(struct tevent_req *subreq)
4027 struct tevent_req *req = tevent_req_callback_data(
4028 subreq, struct tevent_req);
4029 struct cli_getattrE_state *state = tevent_req_data(
4030 req, struct cli_getattrE_state);
4031 uint8_t wct;
4032 uint16_t *vwv = NULL;
4033 NTSTATUS status;
4035 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
4036 NULL, NULL);
4037 TALLOC_FREE(subreq);
4038 if (tevent_req_nterror(req, status)) {
4039 return;
4042 state->size = (off_t)IVAL(vwv+6,0);
4043 state->attr = SVAL(vwv+10,0);
4044 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
4045 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
4046 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
4048 tevent_req_done(req);
4051 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
4052 uint16_t *attr,
4053 off_t *size,
4054 time_t *change_time,
4055 time_t *access_time,
4056 time_t *write_time)
4058 struct cli_getattrE_state *state = tevent_req_data(
4059 req, struct cli_getattrE_state);
4060 NTSTATUS status;
4062 if (tevent_req_is_nterror(req, &status)) {
4063 return status;
4065 if (attr) {
4066 *attr = state->attr;
4068 if (size) {
4069 *size = state->size;
4071 if (change_time) {
4072 *change_time = state->change_time;
4074 if (access_time) {
4075 *access_time = state->access_time;
4077 if (write_time) {
4078 *write_time = state->write_time;
4080 return NT_STATUS_OK;
4083 NTSTATUS cli_getattrE(struct cli_state *cli,
4084 uint16_t fnum,
4085 uint16_t *attr,
4086 off_t *size,
4087 time_t *change_time,
4088 time_t *access_time,
4089 time_t *write_time)
4091 TALLOC_CTX *frame = NULL;
4092 struct tevent_context *ev = NULL;
4093 struct tevent_req *req = NULL;
4094 NTSTATUS status = NT_STATUS_OK;
4096 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4097 return cli_smb2_getattrE(cli,
4098 fnum,
4099 attr,
4100 size,
4101 change_time,
4102 access_time,
4103 write_time);
4106 frame = talloc_stackframe();
4108 if (smbXcli_conn_has_async_calls(cli->conn)) {
4110 * Can't use sync call while an async call is in flight
4112 status = NT_STATUS_INVALID_PARAMETER;
4113 goto fail;
4116 ev = samba_tevent_context_init(frame);
4117 if (ev == NULL) {
4118 status = NT_STATUS_NO_MEMORY;
4119 goto fail;
4122 req = cli_getattrE_send(frame, ev, cli, fnum);
4123 if (req == NULL) {
4124 status = NT_STATUS_NO_MEMORY;
4125 goto fail;
4128 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4129 goto fail;
4132 status = cli_getattrE_recv(req,
4133 attr,
4134 size,
4135 change_time,
4136 access_time,
4137 write_time);
4139 fail:
4140 TALLOC_FREE(frame);
4141 return status;
4144 /****************************************************************************
4145 Do a SMBgetatr call
4146 ****************************************************************************/
4148 static void cli_getatr_done(struct tevent_req *subreq);
4150 struct cli_getatr_state {
4151 int zone_offset;
4152 uint16_t attr;
4153 off_t size;
4154 time_t write_time;
4157 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
4158 struct tevent_context *ev,
4159 struct cli_state *cli,
4160 const char *fname)
4162 struct tevent_req *req = NULL, *subreq = NULL;
4163 struct cli_getatr_state *state = NULL;
4164 uint8_t additional_flags = 0;
4165 uint16_t additional_flags2 = 0;
4166 uint8_t *bytes = NULL;
4168 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
4169 if (req == NULL) {
4170 return NULL;
4173 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
4175 bytes = talloc_array(state, uint8_t, 1);
4176 if (tevent_req_nomem(bytes, req)) {
4177 return tevent_req_post(req, ev);
4179 bytes[0] = 4;
4180 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4181 strlen(fname)+1, NULL);
4183 if (tevent_req_nomem(bytes, req)) {
4184 return tevent_req_post(req, ev);
4187 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4188 additional_flags2 = FLAGS2_REPARSE_PATH;
4191 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
4192 additional_flags2,
4193 0, NULL, talloc_get_size(bytes), bytes);
4194 if (tevent_req_nomem(subreq, req)) {
4195 return tevent_req_post(req, ev);
4197 tevent_req_set_callback(subreq, cli_getatr_done, req);
4198 return req;
4201 static void cli_getatr_done(struct tevent_req *subreq)
4203 struct tevent_req *req = tevent_req_callback_data(
4204 subreq, struct tevent_req);
4205 struct cli_getatr_state *state = tevent_req_data(
4206 req, struct cli_getatr_state);
4207 uint8_t wct;
4208 uint16_t *vwv = NULL;
4209 NTSTATUS status;
4211 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4212 NULL);
4213 TALLOC_FREE(subreq);
4214 if (tevent_req_nterror(req, status)) {
4215 return;
4218 state->attr = SVAL(vwv+0,0);
4219 state->size = (off_t)IVAL(vwv+3,0);
4220 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
4222 tevent_req_done(req);
4225 NTSTATUS cli_getatr_recv(struct tevent_req *req,
4226 uint16_t *attr,
4227 off_t *size,
4228 time_t *write_time)
4230 struct cli_getatr_state *state = tevent_req_data(
4231 req, struct cli_getatr_state);
4232 NTSTATUS status;
4234 if (tevent_req_is_nterror(req, &status)) {
4235 return status;
4237 if (attr) {
4238 *attr = state->attr;
4240 if (size) {
4241 *size = state->size;
4243 if (write_time) {
4244 *write_time = state->write_time;
4246 return NT_STATUS_OK;
4249 NTSTATUS cli_getatr(struct cli_state *cli,
4250 const char *fname,
4251 uint16_t *attr,
4252 off_t *size,
4253 time_t *write_time)
4255 TALLOC_CTX *frame = NULL;
4256 struct tevent_context *ev = NULL;
4257 struct tevent_req *req = NULL;
4258 NTSTATUS status = NT_STATUS_OK;
4260 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4261 return cli_smb2_getatr(cli,
4262 fname,
4263 attr,
4264 size,
4265 write_time);
4268 frame = talloc_stackframe();
4270 if (smbXcli_conn_has_async_calls(cli->conn)) {
4272 * Can't use sync call while an async call is in flight
4274 status = NT_STATUS_INVALID_PARAMETER;
4275 goto fail;
4278 ev = samba_tevent_context_init(frame);
4279 if (ev == NULL) {
4280 status = NT_STATUS_NO_MEMORY;
4281 goto fail;
4284 req = cli_getatr_send(frame, ev, cli, fname);
4285 if (req == NULL) {
4286 status = NT_STATUS_NO_MEMORY;
4287 goto fail;
4290 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4291 goto fail;
4294 status = cli_getatr_recv(req,
4295 attr,
4296 size,
4297 write_time);
4299 fail:
4300 TALLOC_FREE(frame);
4301 return status;
4304 /****************************************************************************
4305 Do a SMBsetattrE call.
4306 ****************************************************************************/
4308 static void cli_setattrE_done(struct tevent_req *subreq);
4310 struct cli_setattrE_state {
4311 uint16_t vwv[7];
4314 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
4315 struct tevent_context *ev,
4316 struct cli_state *cli,
4317 uint16_t fnum,
4318 time_t change_time,
4319 time_t access_time,
4320 time_t write_time)
4322 struct tevent_req *req = NULL, *subreq = NULL;
4323 struct cli_setattrE_state *state = NULL;
4324 uint8_t additional_flags = 0;
4326 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
4327 if (req == NULL) {
4328 return NULL;
4331 SSVAL(state->vwv+0, 0, fnum);
4332 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
4333 smb1cli_conn_server_time_zone(cli->conn));
4334 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
4335 smb1cli_conn_server_time_zone(cli->conn));
4336 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
4337 smb1cli_conn_server_time_zone(cli->conn));
4339 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
4340 7, state->vwv, 0, NULL);
4341 if (tevent_req_nomem(subreq, req)) {
4342 return tevent_req_post(req, ev);
4344 tevent_req_set_callback(subreq, cli_setattrE_done, req);
4345 return req;
4348 static void cli_setattrE_done(struct tevent_req *subreq)
4350 struct tevent_req *req = tevent_req_callback_data(
4351 subreq, struct tevent_req);
4352 NTSTATUS status;
4354 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4355 TALLOC_FREE(subreq);
4356 if (tevent_req_nterror(req, status)) {
4357 return;
4359 tevent_req_done(req);
4362 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
4364 return tevent_req_simple_recv_ntstatus(req);
4367 NTSTATUS cli_setattrE(struct cli_state *cli,
4368 uint16_t fnum,
4369 time_t change_time,
4370 time_t access_time,
4371 time_t write_time)
4373 TALLOC_CTX *frame = NULL;
4374 struct tevent_context *ev = NULL;
4375 struct tevent_req *req = NULL;
4376 NTSTATUS status = NT_STATUS_OK;
4378 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4379 return cli_smb2_setattrE(cli,
4380 fnum,
4381 change_time,
4382 access_time,
4383 write_time);
4386 frame = talloc_stackframe();
4388 if (smbXcli_conn_has_async_calls(cli->conn)) {
4390 * Can't use sync call while an async call is in flight
4392 status = NT_STATUS_INVALID_PARAMETER;
4393 goto fail;
4396 ev = samba_tevent_context_init(frame);
4397 if (ev == NULL) {
4398 status = NT_STATUS_NO_MEMORY;
4399 goto fail;
4402 req = cli_setattrE_send(frame, ev,
4403 cli,
4404 fnum,
4405 change_time,
4406 access_time,
4407 write_time);
4409 if (req == NULL) {
4410 status = NT_STATUS_NO_MEMORY;
4411 goto fail;
4414 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4415 goto fail;
4418 status = cli_setattrE_recv(req);
4420 fail:
4421 TALLOC_FREE(frame);
4422 return status;
4425 /****************************************************************************
4426 Do a SMBsetatr call.
4427 ****************************************************************************/
4429 static void cli_setatr_done(struct tevent_req *subreq);
4431 struct cli_setatr_state {
4432 uint16_t vwv[8];
4435 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
4436 struct tevent_context *ev,
4437 struct cli_state *cli,
4438 const char *fname,
4439 uint16_t attr,
4440 time_t mtime)
4442 struct tevent_req *req = NULL, *subreq = NULL;
4443 struct cli_setatr_state *state = NULL;
4444 uint8_t additional_flags = 0;
4445 uint16_t additional_flags2 = 0;
4446 uint8_t *bytes = NULL;
4448 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
4449 if (req == NULL) {
4450 return NULL;
4453 SSVAL(state->vwv+0, 0, attr);
4454 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
4456 bytes = talloc_array(state, uint8_t, 1);
4457 if (tevent_req_nomem(bytes, req)) {
4458 return tevent_req_post(req, ev);
4460 bytes[0] = 4;
4461 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4462 strlen(fname)+1, NULL);
4463 if (tevent_req_nomem(bytes, req)) {
4464 return tevent_req_post(req, ev);
4466 bytes = talloc_realloc(state, bytes, uint8_t,
4467 talloc_get_size(bytes)+1);
4468 if (tevent_req_nomem(bytes, req)) {
4469 return tevent_req_post(req, ev);
4472 bytes[talloc_get_size(bytes)-1] = 4;
4473 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
4474 1, NULL);
4475 if (tevent_req_nomem(bytes, req)) {
4476 return tevent_req_post(req, ev);
4479 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4480 additional_flags2 = FLAGS2_REPARSE_PATH;
4483 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
4484 additional_flags2,
4485 8, state->vwv, talloc_get_size(bytes), bytes);
4486 if (tevent_req_nomem(subreq, req)) {
4487 return tevent_req_post(req, ev);
4489 tevent_req_set_callback(subreq, cli_setatr_done, req);
4490 return req;
4493 static void cli_setatr_done(struct tevent_req *subreq)
4495 struct tevent_req *req = tevent_req_callback_data(
4496 subreq, struct tevent_req);
4497 NTSTATUS status;
4499 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4500 TALLOC_FREE(subreq);
4501 if (tevent_req_nterror(req, status)) {
4502 return;
4504 tevent_req_done(req);
4507 NTSTATUS cli_setatr_recv(struct tevent_req *req)
4509 return tevent_req_simple_recv_ntstatus(req);
4512 NTSTATUS cli_setatr(struct cli_state *cli,
4513 const char *fname,
4514 uint16_t attr,
4515 time_t mtime)
4517 TALLOC_CTX *frame = NULL;
4518 struct tevent_context *ev = NULL;
4519 struct tevent_req *req = NULL;
4520 NTSTATUS status = NT_STATUS_OK;
4522 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4523 return cli_smb2_setatr(cli,
4524 fname,
4525 attr,
4526 mtime);
4529 frame = talloc_stackframe();
4531 if (smbXcli_conn_has_async_calls(cli->conn)) {
4533 * Can't use sync call while an async call is in flight
4535 status = NT_STATUS_INVALID_PARAMETER;
4536 goto fail;
4539 ev = samba_tevent_context_init(frame);
4540 if (ev == NULL) {
4541 status = NT_STATUS_NO_MEMORY;
4542 goto fail;
4545 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
4546 if (req == NULL) {
4547 status = NT_STATUS_NO_MEMORY;
4548 goto fail;
4551 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4552 goto fail;
4555 status = cli_setatr_recv(req);
4557 fail:
4558 TALLOC_FREE(frame);
4559 return status;
4562 /****************************************************************************
4563 Check for existence of a dir.
4564 ****************************************************************************/
4566 static void cli_chkpath_done(struct tevent_req *subreq);
4568 struct cli_chkpath_state {
4569 int dummy;
4572 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
4573 struct tevent_context *ev,
4574 struct cli_state *cli,
4575 const char *fname)
4577 struct tevent_req *req = NULL, *subreq = NULL;
4578 struct cli_chkpath_state *state = NULL;
4579 uint8_t additional_flags = 0;
4580 uint16_t additional_flags2 = 0;
4581 uint8_t *bytes = NULL;
4583 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4584 if (req == NULL) {
4585 return NULL;
4588 bytes = talloc_array(state, uint8_t, 1);
4589 if (tevent_req_nomem(bytes, req)) {
4590 return tevent_req_post(req, ev);
4592 bytes[0] = 4;
4593 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4594 strlen(fname)+1, NULL);
4596 if (tevent_req_nomem(bytes, req)) {
4597 return tevent_req_post(req, ev);
4600 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL)) {
4601 additional_flags2 = FLAGS2_REPARSE_PATH;
4604 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
4605 additional_flags2,
4606 0, NULL, talloc_get_size(bytes), bytes);
4607 if (tevent_req_nomem(subreq, req)) {
4608 return tevent_req_post(req, ev);
4610 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4611 return req;
4614 static void cli_chkpath_done(struct tevent_req *subreq)
4616 struct tevent_req *req = tevent_req_callback_data(
4617 subreq, struct tevent_req);
4618 NTSTATUS status;
4620 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4621 TALLOC_FREE(subreq);
4622 if (tevent_req_nterror(req, status)) {
4623 return;
4625 tevent_req_done(req);
4628 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4630 return tevent_req_simple_recv_ntstatus(req);
4633 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4635 TALLOC_CTX *frame = NULL;
4636 struct tevent_context *ev = NULL;
4637 struct tevent_req *req = NULL;
4638 char *path2 = NULL;
4639 NTSTATUS status = NT_STATUS_OK;
4641 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4642 return cli_smb2_chkpath(cli, path);
4645 frame = talloc_stackframe();
4647 if (smbXcli_conn_has_async_calls(cli->conn)) {
4649 * Can't use sync call while an async call is in flight
4651 status = NT_STATUS_INVALID_PARAMETER;
4652 goto fail;
4655 path2 = talloc_strdup(frame, path);
4656 if (!path2) {
4657 status = NT_STATUS_NO_MEMORY;
4658 goto fail;
4660 trim_char(path2,'\0','\\');
4661 if (!*path2) {
4662 path2 = talloc_strdup(frame, "\\");
4663 if (!path2) {
4664 status = NT_STATUS_NO_MEMORY;
4665 goto fail;
4669 ev = samba_tevent_context_init(frame);
4670 if (ev == NULL) {
4671 status = NT_STATUS_NO_MEMORY;
4672 goto fail;
4675 req = cli_chkpath_send(frame, ev, cli, path2);
4676 if (req == NULL) {
4677 status = NT_STATUS_NO_MEMORY;
4678 goto fail;
4681 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4682 goto fail;
4685 status = cli_chkpath_recv(req);
4687 fail:
4688 TALLOC_FREE(frame);
4689 return status;
4692 /****************************************************************************
4693 Query disk space.
4694 ****************************************************************************/
4696 static void cli_dskattr_done(struct tevent_req *subreq);
4698 struct cli_dskattr_state {
4699 int bsize;
4700 int total;
4701 int avail;
4704 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4705 struct tevent_context *ev,
4706 struct cli_state *cli)
4708 struct tevent_req *req = NULL, *subreq = NULL;
4709 struct cli_dskattr_state *state = NULL;
4710 uint8_t additional_flags = 0;
4712 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4713 if (req == NULL) {
4714 return NULL;
4717 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
4718 0, NULL, 0, NULL);
4719 if (tevent_req_nomem(subreq, req)) {
4720 return tevent_req_post(req, ev);
4722 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4723 return req;
4726 static void cli_dskattr_done(struct tevent_req *subreq)
4728 struct tevent_req *req = tevent_req_callback_data(
4729 subreq, struct tevent_req);
4730 struct cli_dskattr_state *state = tevent_req_data(
4731 req, struct cli_dskattr_state);
4732 uint8_t wct;
4733 uint16_t *vwv = NULL;
4734 NTSTATUS status;
4736 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4737 NULL);
4738 TALLOC_FREE(subreq);
4739 if (tevent_req_nterror(req, status)) {
4740 return;
4742 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4743 state->total = SVAL(vwv+0, 0);
4744 state->avail = SVAL(vwv+3, 0);
4745 tevent_req_done(req);
4748 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4750 struct cli_dskattr_state *state = tevent_req_data(
4751 req, struct cli_dskattr_state);
4752 NTSTATUS status;
4754 if (tevent_req_is_nterror(req, &status)) {
4755 return status;
4757 *bsize = state->bsize;
4758 *total = state->total;
4759 *avail = state->avail;
4760 return NT_STATUS_OK;
4763 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4765 TALLOC_CTX *frame = NULL;
4766 struct tevent_context *ev = NULL;
4767 struct tevent_req *req = NULL;
4768 NTSTATUS status = NT_STATUS_OK;
4770 frame = talloc_stackframe();
4772 if (smbXcli_conn_has_async_calls(cli->conn)) {
4774 * Can't use sync call while an async call is in flight
4776 status = NT_STATUS_INVALID_PARAMETER;
4777 goto fail;
4780 ev = samba_tevent_context_init(frame);
4781 if (ev == NULL) {
4782 status = NT_STATUS_NO_MEMORY;
4783 goto fail;
4786 req = cli_dskattr_send(frame, ev, cli);
4787 if (req == NULL) {
4788 status = NT_STATUS_NO_MEMORY;
4789 goto fail;
4792 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4793 goto fail;
4796 status = cli_dskattr_recv(req, bsize, total, avail);
4798 fail:
4799 TALLOC_FREE(frame);
4800 return status;
4803 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4804 uint64_t *total, uint64_t *avail)
4806 uint64_t sectors_per_block;
4807 uint64_t bytes_per_sector;
4808 int old_bsize, old_total, old_avail;
4809 NTSTATUS status;
4811 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4812 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4816 * Try the trans2 disk full size info call first.
4817 * We already use this in SMBC_fstatvfs_ctx().
4818 * Ignore 'actual_available_units' as we only
4819 * care about the quota for the caller.
4822 status = cli_get_fs_full_size_info(cli,
4823 total,
4824 avail,
4825 NULL,
4826 &sectors_per_block,
4827 &bytes_per_sector);
4829 /* Try and cope will all varients of "we don't do this call"
4830 and fall back to cli_dskattr. */
4832 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4833 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4834 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4835 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4836 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4837 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4838 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4839 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4840 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4841 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4842 goto try_dskattr;
4845 if (!NT_STATUS_IS_OK(status)) {
4846 return status;
4849 if (bsize) {
4850 *bsize = sectors_per_block *
4851 bytes_per_sector;
4854 return NT_STATUS_OK;
4856 try_dskattr:
4858 /* Old SMB1 core protocol fallback. */
4859 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4860 if (!NT_STATUS_IS_OK(status)) {
4861 return status;
4863 if (bsize) {
4864 *bsize = (uint64_t)old_bsize;
4866 if (total) {
4867 *total = (uint64_t)old_total;
4869 if (avail) {
4870 *avail = (uint64_t)old_avail;
4872 return NT_STATUS_OK;
4875 /****************************************************************************
4876 Create and open a temporary file.
4877 ****************************************************************************/
4879 static void cli_ctemp_done(struct tevent_req *subreq);
4881 struct ctemp_state {
4882 uint16_t vwv[3];
4883 char *ret_path;
4884 uint16_t fnum;
4887 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4888 struct tevent_context *ev,
4889 struct cli_state *cli,
4890 const char *path)
4892 struct tevent_req *req = NULL, *subreq = NULL;
4893 struct ctemp_state *state = NULL;
4894 uint8_t additional_flags = 0;
4895 uint16_t additional_flags2 = 0;
4896 uint8_t *bytes = NULL;
4898 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4899 if (req == NULL) {
4900 return NULL;
4903 SSVAL(state->vwv,0,0);
4904 SIVALS(state->vwv+1,0,-1);
4906 bytes = talloc_array(state, uint8_t, 1);
4907 if (tevent_req_nomem(bytes, req)) {
4908 return tevent_req_post(req, ev);
4910 bytes[0] = 4;
4911 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4912 strlen(path)+1, NULL);
4913 if (tevent_req_nomem(bytes, req)) {
4914 return tevent_req_post(req, ev);
4917 if (clistr_is_previous_version_path(path, NULL, NULL, NULL)) {
4918 additional_flags2 = FLAGS2_REPARSE_PATH;
4921 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4922 additional_flags2,
4923 3, state->vwv, talloc_get_size(bytes), bytes);
4924 if (tevent_req_nomem(subreq, req)) {
4925 return tevent_req_post(req, ev);
4927 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4928 return req;
4931 static void cli_ctemp_done(struct tevent_req *subreq)
4933 struct tevent_req *req = tevent_req_callback_data(
4934 subreq, struct tevent_req);
4935 struct ctemp_state *state = tevent_req_data(
4936 req, struct ctemp_state);
4937 NTSTATUS status;
4938 uint8_t wcnt;
4939 uint16_t *vwv;
4940 uint32_t num_bytes = 0;
4941 uint8_t *bytes = NULL;
4943 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4944 &num_bytes, &bytes);
4945 TALLOC_FREE(subreq);
4946 if (tevent_req_nterror(req, status)) {
4947 return;
4950 state->fnum = SVAL(vwv+0, 0);
4952 /* From W2K3, the result is just the ASCII name */
4953 if (num_bytes < 2) {
4954 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4955 return;
4958 if (pull_string_talloc(state,
4959 NULL,
4961 &state->ret_path,
4962 bytes,
4963 num_bytes,
4964 STR_ASCII) == 0) {
4965 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4966 return;
4968 tevent_req_done(req);
4971 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4972 TALLOC_CTX *ctx,
4973 uint16_t *pfnum,
4974 char **outfile)
4976 struct ctemp_state *state = tevent_req_data(req,
4977 struct ctemp_state);
4978 NTSTATUS status;
4980 if (tevent_req_is_nterror(req, &status)) {
4981 return status;
4983 *pfnum = state->fnum;
4984 *outfile = talloc_strdup(ctx, state->ret_path);
4985 if (!*outfile) {
4986 return NT_STATUS_NO_MEMORY;
4988 return NT_STATUS_OK;
4991 NTSTATUS cli_ctemp(struct cli_state *cli,
4992 TALLOC_CTX *ctx,
4993 const char *path,
4994 uint16_t *pfnum,
4995 char **out_path)
4997 TALLOC_CTX *frame = talloc_stackframe();
4998 struct tevent_context *ev;
4999 struct tevent_req *req;
5000 NTSTATUS status = NT_STATUS_OK;
5002 if (smbXcli_conn_has_async_calls(cli->conn)) {
5004 * Can't use sync call while an async call is in flight
5006 status = NT_STATUS_INVALID_PARAMETER;
5007 goto fail;
5010 ev = samba_tevent_context_init(frame);
5011 if (ev == NULL) {
5012 status = NT_STATUS_NO_MEMORY;
5013 goto fail;
5016 req = cli_ctemp_send(frame, ev, cli, path);
5017 if (req == NULL) {
5018 status = NT_STATUS_NO_MEMORY;
5019 goto fail;
5022 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5023 goto fail;
5026 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
5028 fail:
5029 TALLOC_FREE(frame);
5030 return status;
5034 send a raw ioctl - used by the torture code
5036 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
5038 uint16_t vwv[3];
5039 NTSTATUS status;
5041 SSVAL(vwv+0, 0, fnum);
5042 SSVAL(vwv+1, 0, code>>16);
5043 SSVAL(vwv+2, 0, (code&0xFFFF));
5045 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
5046 NULL, 0, NULL, NULL, NULL, NULL);
5047 if (!NT_STATUS_IS_OK(status)) {
5048 return status;
5050 *blob = data_blob_null;
5051 return NT_STATUS_OK;
5054 /*********************************************************
5055 Set an extended attribute utility fn.
5056 *********************************************************/
5058 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
5059 uint8_t *param, unsigned int param_len,
5060 const char *ea_name,
5061 const char *ea_val, size_t ea_len)
5063 uint16_t setup[1];
5064 unsigned int data_len = 0;
5065 uint8_t *data = NULL;
5066 char *p;
5067 size_t ea_namelen = strlen(ea_name);
5068 NTSTATUS status;
5070 SSVAL(setup, 0, setup_val);
5072 if (ea_namelen == 0 && ea_len == 0) {
5073 data_len = 4;
5074 data = talloc_array(talloc_tos(),
5075 uint8_t,
5076 data_len);
5077 if (!data) {
5078 return NT_STATUS_NO_MEMORY;
5080 p = (char *)data;
5081 SIVAL(p,0,data_len);
5082 } else {
5083 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
5084 data = talloc_array(talloc_tos(),
5085 uint8_t,
5086 data_len);
5087 if (!data) {
5088 return NT_STATUS_NO_MEMORY;
5090 p = (char *)data;
5091 SIVAL(p,0,data_len);
5092 p += 4;
5093 SCVAL(p, 0, 0); /* EA flags. */
5094 SCVAL(p, 1, ea_namelen);
5095 SSVAL(p, 2, ea_len);
5096 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
5097 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
5101 * FIXME - if we want to do previous version path
5102 * processing on an EA set call we need to turn this
5103 * into calls to cli_trans_send()/cli_trans_recv()
5104 * with a temporary event context, as cli_trans_send()
5105 * have access to the additional_flags2 needed to
5106 * send @GMT- paths. JRA.
5109 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
5110 setup, 1, 0,
5111 param, param_len, 2,
5112 data, data_len, 0,
5113 NULL,
5114 NULL, 0, NULL, /* rsetup */
5115 NULL, 0, NULL, /* rparam */
5116 NULL, 0, NULL); /* rdata */
5117 talloc_free(data);
5118 return status;
5121 /*********************************************************
5122 Set an extended attribute on a pathname.
5123 *********************************************************/
5125 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
5126 const char *ea_name, const char *ea_val,
5127 size_t ea_len)
5129 unsigned int param_len = 0;
5130 uint8_t *param;
5131 NTSTATUS status;
5132 TALLOC_CTX *frame = NULL;
5134 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
5135 return cli_smb2_set_ea_path(cli,
5136 path,
5137 ea_name,
5138 ea_val,
5139 ea_len);
5142 frame = talloc_stackframe();
5144 param = talloc_array(frame, uint8_t, 6);
5145 if (!param) {
5146 status = NT_STATUS_NO_MEMORY;
5147 goto fail;
5149 SSVAL(param,0,SMB_INFO_SET_EA);
5150 SSVAL(param,2,0);
5151 SSVAL(param,4,0);
5153 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
5154 path, strlen(path)+1,
5155 NULL);
5156 param_len = talloc_get_size(param);
5158 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
5159 ea_name, ea_val, ea_len);
5161 fail:
5163 TALLOC_FREE(frame);
5164 return status;
5167 /*********************************************************
5168 Set an extended attribute on an fnum.
5169 *********************************************************/
5171 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
5172 const char *ea_name, const char *ea_val,
5173 size_t ea_len)
5175 uint8_t param[6];
5177 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
5178 return cli_smb2_set_ea_fnum(cli,
5179 fnum,
5180 ea_name,
5181 ea_val,
5182 ea_len);
5185 memset(param, 0, 6);
5186 SSVAL(param,0,fnum);
5187 SSVAL(param,2,SMB_INFO_SET_EA);
5189 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
5190 ea_name, ea_val, ea_len);
5193 /*********************************************************
5194 Get an extended attribute list utility fn.
5195 *********************************************************/
5197 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
5198 size_t rdata_len,
5199 size_t *pnum_eas, struct ea_struct **pea_list)
5201 struct ea_struct *ea_list = NULL;
5202 size_t num_eas;
5203 size_t ea_size;
5204 const uint8_t *p;
5206 if (rdata_len < 4) {
5207 return false;
5210 ea_size = (size_t)IVAL(rdata,0);
5211 if (ea_size > rdata_len) {
5212 return false;
5215 if (ea_size == 0) {
5216 /* No EA's present. */
5217 *pnum_eas = 0;
5218 *pea_list = NULL;
5219 return true;
5222 p = rdata + 4;
5223 ea_size -= 4;
5225 /* Validate the EA list and count it. */
5226 for (num_eas = 0; ea_size >= 4; num_eas++) {
5227 unsigned int ea_namelen = CVAL(p,1);
5228 unsigned int ea_valuelen = SVAL(p,2);
5229 if (ea_namelen == 0) {
5230 return false;
5232 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
5233 return false;
5235 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
5236 p += 4 + ea_namelen + 1 + ea_valuelen;
5239 if (num_eas == 0) {
5240 *pnum_eas = 0;
5241 *pea_list = NULL;
5242 return true;
5245 *pnum_eas = num_eas;
5246 if (!pea_list) {
5247 /* Caller only wants number of EA's. */
5248 return true;
5251 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
5252 if (!ea_list) {
5253 return false;
5256 ea_size = (size_t)IVAL(rdata,0);
5257 p = rdata + 4;
5259 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
5260 struct ea_struct *ea = &ea_list[num_eas];
5261 fstring unix_ea_name;
5262 unsigned int ea_namelen = CVAL(p,1);
5263 unsigned int ea_valuelen = SVAL(p,2);
5265 ea->flags = CVAL(p,0);
5266 unix_ea_name[0] = '\0';
5267 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
5268 ea->name = talloc_strdup(ea_list, unix_ea_name);
5269 if (!ea->name) {
5270 goto fail;
5272 /* Ensure the value is null terminated (in case it's a string). */
5273 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
5274 if (!ea->value.data) {
5275 goto fail;
5277 if (ea_valuelen) {
5278 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
5280 ea->value.data[ea_valuelen] = 0;
5281 ea->value.length--;
5282 p += 4 + ea_namelen + 1 + ea_valuelen;
5285 *pea_list = ea_list;
5286 return true;
5288 fail:
5289 TALLOC_FREE(ea_list);
5290 return false;
5293 /*********************************************************
5294 Get an extended attribute list from a pathname.
5295 *********************************************************/
5297 struct cli_get_ea_list_path_state {
5298 uint32_t num_data;
5299 uint8_t *data;
5302 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
5304 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
5305 struct tevent_context *ev,
5306 struct cli_state *cli,
5307 const char *fname)
5309 struct tevent_req *req, *subreq;
5310 struct cli_get_ea_list_path_state *state;
5312 req = tevent_req_create(mem_ctx, &state,
5313 struct cli_get_ea_list_path_state);
5314 if (req == NULL) {
5315 return NULL;
5317 subreq = cli_qpathinfo_send(state, ev, cli, fname,
5318 SMB_INFO_QUERY_ALL_EAS, 4,
5319 CLI_BUFFER_SIZE);
5320 if (tevent_req_nomem(subreq, req)) {
5321 return tevent_req_post(req, ev);
5323 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
5324 return req;
5327 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
5329 struct tevent_req *req = tevent_req_callback_data(
5330 subreq, struct tevent_req);
5331 struct cli_get_ea_list_path_state *state = tevent_req_data(
5332 req, struct cli_get_ea_list_path_state);
5333 NTSTATUS status;
5335 status = cli_qpathinfo_recv(subreq, state, &state->data,
5336 &state->num_data);
5337 TALLOC_FREE(subreq);
5338 if (tevent_req_nterror(req, status)) {
5339 return;
5341 tevent_req_done(req);
5344 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5345 size_t *pnum_eas, struct ea_struct **peas)
5347 struct cli_get_ea_list_path_state *state = tevent_req_data(
5348 req, struct cli_get_ea_list_path_state);
5349 NTSTATUS status;
5351 if (tevent_req_is_nterror(req, &status)) {
5352 return status;
5354 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
5355 pnum_eas, peas)) {
5356 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5358 return NT_STATUS_OK;
5361 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
5362 TALLOC_CTX *ctx,
5363 size_t *pnum_eas,
5364 struct ea_struct **pea_list)
5366 TALLOC_CTX *frame = NULL;
5367 struct tevent_context *ev = NULL;
5368 struct tevent_req *req = NULL;
5369 NTSTATUS status = NT_STATUS_NO_MEMORY;
5371 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
5372 return cli_smb2_get_ea_list_path(cli,
5373 path,
5374 ctx,
5375 pnum_eas,
5376 pea_list);
5379 frame = talloc_stackframe();
5381 if (smbXcli_conn_has_async_calls(cli->conn)) {
5383 * Can't use sync call while an async call is in flight
5385 status = NT_STATUS_INVALID_PARAMETER;
5386 goto fail;
5388 ev = samba_tevent_context_init(frame);
5389 if (ev == NULL) {
5390 goto fail;
5392 req = cli_get_ea_list_path_send(frame, ev, cli, path);
5393 if (req == NULL) {
5394 goto fail;
5396 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5397 goto fail;
5399 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
5400 fail:
5401 TALLOC_FREE(frame);
5402 return status;
5405 /****************************************************************************
5406 Convert open "flags" arg to uint32_t on wire.
5407 ****************************************************************************/
5409 static uint32_t open_flags_to_wire(int flags)
5411 int open_mode = flags & O_ACCMODE;
5412 uint32_t ret = 0;
5414 switch (open_mode) {
5415 case O_WRONLY:
5416 ret |= SMB_O_WRONLY;
5417 break;
5418 case O_RDWR:
5419 ret |= SMB_O_RDWR;
5420 break;
5421 default:
5422 case O_RDONLY:
5423 ret |= SMB_O_RDONLY;
5424 break;
5427 if (flags & O_CREAT) {
5428 ret |= SMB_O_CREAT;
5430 if (flags & O_EXCL) {
5431 ret |= SMB_O_EXCL;
5433 if (flags & O_TRUNC) {
5434 ret |= SMB_O_TRUNC;
5436 #if defined(O_SYNC)
5437 if (flags & O_SYNC) {
5438 ret |= SMB_O_SYNC;
5440 #endif /* O_SYNC */
5441 if (flags & O_APPEND) {
5442 ret |= SMB_O_APPEND;
5444 #if defined(O_DIRECT)
5445 if (flags & O_DIRECT) {
5446 ret |= SMB_O_DIRECT;
5448 #endif
5449 #if defined(O_DIRECTORY)
5450 if (flags & O_DIRECTORY) {
5451 ret |= SMB_O_DIRECTORY;
5453 #endif
5454 return ret;
5457 /****************************************************************************
5458 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
5459 ****************************************************************************/
5461 struct cli_posix_open_internal_state {
5462 uint16_t setup;
5463 uint8_t *param;
5464 uint8_t data[18];
5465 uint16_t fnum; /* Out */
5468 static void cli_posix_open_internal_done(struct tevent_req *subreq);
5470 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
5471 struct tevent_context *ev,
5472 struct cli_state *cli,
5473 const char *fname,
5474 uint32_t wire_flags,
5475 mode_t mode)
5477 struct tevent_req *req = NULL, *subreq = NULL;
5478 struct cli_posix_open_internal_state *state = NULL;
5480 req = tevent_req_create(
5481 mem_ctx, &state, struct cli_posix_open_internal_state);
5482 if (req == NULL) {
5483 return NULL;
5486 /* Setup setup word. */
5487 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
5489 /* Setup param array. */
5490 state->param = talloc_zero_array(state, uint8_t, 6);
5491 if (tevent_req_nomem(state->param, req)) {
5492 return tevent_req_post(req, ev);
5494 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
5496 state->param = trans2_bytes_push_str(
5497 state->param,
5498 smbXcli_conn_use_unicode(cli->conn),
5499 fname,
5500 strlen(fname)+1,
5501 NULL);
5503 if (tevent_req_nomem(state->param, req)) {
5504 return tevent_req_post(req, ev);
5507 SIVAL(state->data,0,0); /* No oplock. */
5508 SIVAL(state->data,4,wire_flags);
5509 SIVAL(state->data,8,unix_perms_to_wire(mode));
5510 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
5511 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
5513 subreq = cli_trans_send(state, /* mem ctx. */
5514 ev, /* event ctx. */
5515 cli, /* cli_state. */
5516 0, /* additional_flags2 */
5517 SMBtrans2, /* cmd. */
5518 NULL, /* pipe name. */
5519 -1, /* fid. */
5520 0, /* function. */
5521 0, /* flags. */
5522 &state->setup, /* setup. */
5523 1, /* num setup uint16_t words. */
5524 0, /* max returned setup. */
5525 state->param, /* param. */
5526 talloc_get_size(state->param),/* num param. */
5527 2, /* max returned param. */
5528 state->data, /* data. */
5529 18, /* num data. */
5530 12); /* max returned data. */
5532 if (tevent_req_nomem(subreq, req)) {
5533 return tevent_req_post(req, ev);
5535 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
5536 return req;
5539 static void cli_posix_open_internal_done(struct tevent_req *subreq)
5541 struct tevent_req *req = tevent_req_callback_data(
5542 subreq, struct tevent_req);
5543 struct cli_posix_open_internal_state *state = tevent_req_data(
5544 req, struct cli_posix_open_internal_state);
5545 NTSTATUS status;
5546 uint8_t *data;
5547 uint32_t num_data;
5549 status = cli_trans_recv(
5550 subreq,
5551 state,
5552 NULL,
5553 NULL,
5555 NULL,
5556 NULL,
5558 NULL,
5559 &data,
5561 &num_data);
5562 TALLOC_FREE(subreq);
5563 if (tevent_req_nterror(req, status)) {
5564 return;
5566 state->fnum = SVAL(data,2);
5567 tevent_req_done(req);
5570 static NTSTATUS cli_posix_open_internal_recv(struct tevent_req *req,
5571 uint16_t *pfnum)
5573 struct cli_posix_open_internal_state *state = tevent_req_data(
5574 req, struct cli_posix_open_internal_state);
5575 NTSTATUS status;
5577 if (tevent_req_is_nterror(req, &status)) {
5578 return status;
5580 *pfnum = state->fnum;
5581 return NT_STATUS_OK;
5584 struct cli_posix_open_state {
5585 uint16_t fnum;
5588 static void cli_posix_open_done(struct tevent_req *subreq);
5590 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
5591 struct tevent_context *ev,
5592 struct cli_state *cli,
5593 const char *fname,
5594 int flags,
5595 mode_t mode)
5597 struct tevent_req *req = NULL, *subreq = NULL;
5598 struct cli_posix_open_state *state = NULL;
5599 uint32_t wire_flags;
5601 req = tevent_req_create(mem_ctx, &state,
5602 struct cli_posix_open_state);
5603 if (req == NULL) {
5604 return NULL;
5607 wire_flags = open_flags_to_wire(flags);
5609 subreq = cli_posix_open_internal_send(
5610 mem_ctx, ev, cli, fname, wire_flags, mode);
5611 if (tevent_req_nomem(subreq, req)) {
5612 return tevent_req_post(req, ev);
5614 tevent_req_set_callback(subreq, cli_posix_open_done, req);
5615 return req;
5618 static void cli_posix_open_done(struct tevent_req *subreq)
5620 struct tevent_req *req = tevent_req_callback_data(
5621 subreq, struct tevent_req);
5622 struct cli_posix_open_state *state = tevent_req_data(
5623 req, struct cli_posix_open_state);
5624 NTSTATUS status;
5626 status = cli_posix_open_internal_recv(subreq, &state->fnum);
5627 tevent_req_simple_finish_ntstatus(subreq, status);
5630 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
5632 struct cli_posix_open_state *state = tevent_req_data(
5633 req, struct cli_posix_open_state);
5634 NTSTATUS status;
5636 if (tevent_req_is_nterror(req, &status)) {
5637 return status;
5639 *pfnum = state->fnum;
5640 return NT_STATUS_OK;
5643 /****************************************************************************
5644 Open - POSIX semantics. Doesn't request oplock.
5645 ****************************************************************************/
5647 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
5648 int flags, mode_t mode, uint16_t *pfnum)
5651 TALLOC_CTX *frame = talloc_stackframe();
5652 struct tevent_context *ev = NULL;
5653 struct tevent_req *req = NULL;
5654 NTSTATUS status = NT_STATUS_NO_MEMORY;
5656 if (smbXcli_conn_has_async_calls(cli->conn)) {
5658 * Can't use sync call while an async call is in flight
5660 status = NT_STATUS_INVALID_PARAMETER;
5661 goto fail;
5663 ev = samba_tevent_context_init(frame);
5664 if (ev == NULL) {
5665 goto fail;
5667 req = cli_posix_open_send(
5668 frame, ev, cli, fname, flags, mode);
5669 if (req == NULL) {
5670 goto fail;
5672 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5673 goto fail;
5675 status = cli_posix_open_recv(req, pfnum);
5676 fail:
5677 TALLOC_FREE(frame);
5678 return status;
5681 struct cli_posix_mkdir_state {
5682 struct tevent_context *ev;
5683 struct cli_state *cli;
5686 static void cli_posix_mkdir_done(struct tevent_req *subreq);
5688 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5689 struct tevent_context *ev,
5690 struct cli_state *cli,
5691 const char *fname,
5692 mode_t mode)
5694 struct tevent_req *req = NULL, *subreq = NULL;
5695 struct cli_posix_mkdir_state *state = NULL;
5696 uint32_t wire_flags;
5698 req = tevent_req_create(
5699 mem_ctx, &state, struct cli_posix_mkdir_state);
5700 if (req == NULL) {
5701 return NULL;
5703 state->ev = ev;
5704 state->cli = cli;
5706 wire_flags = SMB_O_CREAT | SMB_O_DIRECTORY;
5708 subreq = cli_posix_open_internal_send(
5709 mem_ctx, ev, cli, fname, wire_flags, mode);
5710 if (tevent_req_nomem(subreq, req)) {
5711 return tevent_req_post(req, ev);
5713 tevent_req_set_callback(subreq, cli_posix_mkdir_done, req);
5714 return req;
5717 static void cli_posix_mkdir_done(struct tevent_req *subreq)
5719 struct tevent_req *req = tevent_req_callback_data(
5720 subreq, struct tevent_req);
5721 NTSTATUS status;
5722 uint16_t fnum;
5724 status = cli_posix_open_internal_recv(subreq, &fnum);
5725 TALLOC_FREE(subreq);
5726 if (tevent_req_nterror(req, status)) {
5727 return;
5729 tevent_req_done(req);
5732 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5734 return tevent_req_simple_recv_ntstatus(req);
5737 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5739 TALLOC_CTX *frame = talloc_stackframe();
5740 struct tevent_context *ev = NULL;
5741 struct tevent_req *req = NULL;
5742 NTSTATUS status = NT_STATUS_NO_MEMORY;
5744 if (smbXcli_conn_has_async_calls(cli->conn)) {
5746 * Can't use sync call while an async call is in flight
5748 status = NT_STATUS_INVALID_PARAMETER;
5749 goto fail;
5752 ev = samba_tevent_context_init(frame);
5753 if (ev == NULL) {
5754 goto fail;
5756 req = cli_posix_mkdir_send(
5757 frame, ev, cli, fname, mode);
5758 if (req == NULL) {
5759 goto fail;
5761 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5762 goto fail;
5764 status = cli_posix_mkdir_recv(req);
5765 fail:
5766 TALLOC_FREE(frame);
5767 return status;
5770 /****************************************************************************
5771 unlink or rmdir - POSIX semantics.
5772 ****************************************************************************/
5774 struct cli_posix_unlink_internal_state {
5775 uint8_t data[2];
5778 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5780 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5781 struct tevent_context *ev,
5782 struct cli_state *cli,
5783 const char *fname,
5784 uint16_t level)
5786 struct tevent_req *req = NULL, *subreq = NULL;
5787 struct cli_posix_unlink_internal_state *state = NULL;
5789 req = tevent_req_create(mem_ctx, &state,
5790 struct cli_posix_unlink_internal_state);
5791 if (req == NULL) {
5792 return NULL;
5795 /* Setup data word. */
5796 SSVAL(state->data, 0, level);
5798 subreq = cli_setpathinfo_send(state, ev, cli,
5799 SMB_POSIX_PATH_UNLINK,
5800 fname,
5801 state->data, sizeof(state->data));
5802 if (tevent_req_nomem(subreq, req)) {
5803 return tevent_req_post(req, ev);
5805 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5806 return req;
5809 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5811 NTSTATUS status = cli_setpathinfo_recv(subreq);
5812 tevent_req_simple_finish_ntstatus(subreq, status);
5815 static NTSTATUS cli_posix_unlink_internal_recv(struct tevent_req *req)
5817 return tevent_req_simple_recv_ntstatus(req);
5820 struct cli_posix_unlink_state {
5821 uint8_t dummy;
5824 static void cli_posix_unlink_done(struct tevent_req *subreq);
5826 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5827 struct tevent_context *ev,
5828 struct cli_state *cli,
5829 const char *fname)
5831 struct tevent_req *req = NULL, *subreq = NULL;
5832 struct cli_posix_unlink_state *state;
5834 req = tevent_req_create(
5835 mem_ctx, &state, struct cli_posix_unlink_state);
5836 if (req == NULL) {
5837 return NULL;
5839 subreq = cli_posix_unlink_internal_send(
5840 mem_ctx, ev, cli, fname, SMB_POSIX_UNLINK_FILE_TARGET);
5841 if (tevent_req_nomem(subreq, req)) {
5842 return tevent_req_post(req, ev);
5844 tevent_req_set_callback(subreq, cli_posix_unlink_done, req);
5845 return req;
5848 static void cli_posix_unlink_done(struct tevent_req *subreq)
5850 NTSTATUS status = cli_posix_unlink_internal_recv(subreq);
5851 tevent_req_simple_finish_ntstatus(subreq, status);
5854 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5856 return tevent_req_simple_recv_ntstatus(req);
5859 /****************************************************************************
5860 unlink - POSIX semantics.
5861 ****************************************************************************/
5863 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5865 TALLOC_CTX *frame = talloc_stackframe();
5866 struct tevent_context *ev = NULL;
5867 struct tevent_req *req = NULL;
5868 NTSTATUS status = NT_STATUS_OK;
5870 if (smbXcli_conn_has_async_calls(cli->conn)) {
5872 * Can't use sync call while an async call is in flight
5874 status = NT_STATUS_INVALID_PARAMETER;
5875 goto fail;
5878 ev = samba_tevent_context_init(frame);
5879 if (ev == NULL) {
5880 status = NT_STATUS_NO_MEMORY;
5881 goto fail;
5884 req = cli_posix_unlink_send(frame,
5886 cli,
5887 fname);
5888 if (req == NULL) {
5889 status = NT_STATUS_NO_MEMORY;
5890 goto fail;
5893 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5894 goto fail;
5897 status = cli_posix_unlink_recv(req);
5899 fail:
5900 TALLOC_FREE(frame);
5901 return status;
5904 /****************************************************************************
5905 rmdir - POSIX semantics.
5906 ****************************************************************************/
5908 struct cli_posix_rmdir_state {
5909 uint8_t dummy;
5912 static void cli_posix_rmdir_done(struct tevent_req *subreq);
5914 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5915 struct tevent_context *ev,
5916 struct cli_state *cli,
5917 const char *fname)
5919 struct tevent_req *req = NULL, *subreq = NULL;
5920 struct cli_posix_rmdir_state *state;
5922 req = tevent_req_create(mem_ctx, &state, struct cli_posix_rmdir_state);
5923 if (req == NULL) {
5924 return NULL;
5926 subreq = cli_posix_unlink_internal_send(
5927 mem_ctx, ev, cli, fname, SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5928 if (tevent_req_nomem(subreq, req)) {
5929 return tevent_req_post(req, ev);
5931 tevent_req_set_callback(subreq, cli_posix_rmdir_done, req);
5932 return req;
5935 static void cli_posix_rmdir_done(struct tevent_req *subreq)
5937 NTSTATUS status = cli_posix_unlink_internal_recv(subreq);
5938 tevent_req_simple_finish_ntstatus(subreq, status);
5941 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5943 return tevent_req_simple_recv_ntstatus(req);
5946 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5948 TALLOC_CTX *frame = talloc_stackframe();
5949 struct tevent_context *ev = NULL;
5950 struct tevent_req *req = NULL;
5951 NTSTATUS status = NT_STATUS_OK;
5953 if (smbXcli_conn_has_async_calls(cli->conn)) {
5955 * Can't use sync call while an async call is in flight
5957 status = NT_STATUS_INVALID_PARAMETER;
5958 goto fail;
5961 ev = samba_tevent_context_init(frame);
5962 if (ev == NULL) {
5963 status = NT_STATUS_NO_MEMORY;
5964 goto fail;
5967 req = cli_posix_rmdir_send(frame,
5969 cli,
5970 fname);
5971 if (req == NULL) {
5972 status = NT_STATUS_NO_MEMORY;
5973 goto fail;
5976 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5977 goto fail;
5980 status = cli_posix_rmdir_recv(req, frame);
5982 fail:
5983 TALLOC_FREE(frame);
5984 return status;
5987 /****************************************************************************
5988 filechangenotify
5989 ****************************************************************************/
5991 struct cli_notify_state {
5992 struct tevent_req *subreq;
5993 uint8_t setup[8];
5994 uint32_t num_changes;
5995 struct notify_change *changes;
5998 static void cli_notify_done(struct tevent_req *subreq);
5999 static void cli_notify_done_smb2(struct tevent_req *subreq);
6000 static bool cli_notify_cancel(struct tevent_req *req);
6002 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
6003 struct tevent_context *ev,
6004 struct cli_state *cli, uint16_t fnum,
6005 uint32_t buffer_size,
6006 uint32_t completion_filter, bool recursive)
6008 struct tevent_req *req;
6009 struct cli_notify_state *state;
6010 unsigned old_timeout;
6012 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
6013 if (req == NULL) {
6014 return NULL;
6017 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
6019 * Notifies should not time out
6021 old_timeout = cli_set_timeout(cli, 0);
6023 state->subreq = cli_smb2_notify_send(
6024 state,
6026 cli,
6027 fnum,
6028 buffer_size,
6029 completion_filter,
6030 recursive);
6032 cli_set_timeout(cli, old_timeout);
6034 if (tevent_req_nomem(state->subreq, req)) {
6035 return tevent_req_post(req, ev);
6037 tevent_req_set_callback(
6038 state->subreq, cli_notify_done_smb2, req);
6039 goto done;
6042 SIVAL(state->setup, 0, completion_filter);
6043 SSVAL(state->setup, 4, fnum);
6044 SSVAL(state->setup, 6, recursive);
6047 * Notifies should not time out
6049 old_timeout = cli_set_timeout(cli, 0);
6051 state->subreq = cli_trans_send(
6052 state, /* mem ctx. */
6053 ev, /* event ctx. */
6054 cli, /* cli_state. */
6055 0, /* additional_flags2 */
6056 SMBnttrans, /* cmd. */
6057 NULL, /* pipe name. */
6058 -1, /* fid. */
6059 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
6060 0, /* flags. */
6061 (uint16_t *)state->setup, /* setup. */
6062 4, /* num setup uint16_t words. */
6063 0, /* max returned setup. */
6064 NULL, /* param. */
6065 0, /* num param. */
6066 buffer_size, /* max returned param. */
6067 NULL, /* data. */
6068 0, /* num data. */
6069 0); /* max returned data. */
6071 cli_set_timeout(cli, old_timeout);
6073 if (tevent_req_nomem(state->subreq, req)) {
6074 return tevent_req_post(req, ev);
6076 tevent_req_set_callback(state->subreq, cli_notify_done, req);
6077 done:
6078 tevent_req_set_cancel_fn(req, cli_notify_cancel);
6079 return req;
6082 static bool cli_notify_cancel(struct tevent_req *req)
6084 struct cli_notify_state *state = tevent_req_data(
6085 req, struct cli_notify_state);
6086 bool ok;
6088 ok = tevent_req_cancel(state->subreq);
6089 return ok;
6092 static void cli_notify_done(struct tevent_req *subreq)
6094 struct tevent_req *req = tevent_req_callback_data(
6095 subreq, struct tevent_req);
6096 struct cli_notify_state *state = tevent_req_data(
6097 req, struct cli_notify_state);
6098 NTSTATUS status;
6099 uint8_t *params;
6100 uint32_t i, ofs, num_params;
6101 uint16_t flags2;
6103 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
6104 &params, 0, &num_params, NULL, 0, NULL);
6105 TALLOC_FREE(subreq);
6106 state->subreq = NULL;
6107 if (tevent_req_nterror(req, status)) {
6108 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
6109 return;
6112 state->num_changes = 0;
6113 ofs = 0;
6115 while (num_params - ofs > 12) {
6116 uint32_t next = IVAL(params, ofs);
6117 state->num_changes += 1;
6119 if ((next == 0) || (ofs+next >= num_params)) {
6120 break;
6122 ofs += next;
6125 state->changes = talloc_array(state, struct notify_change,
6126 state->num_changes);
6127 if (tevent_req_nomem(state->changes, req)) {
6128 TALLOC_FREE(params);
6129 return;
6132 ofs = 0;
6134 for (i=0; i<state->num_changes; i++) {
6135 uint32_t next = IVAL(params, ofs);
6136 uint32_t len = IVAL(params, ofs+8);
6137 ssize_t ret;
6138 char *name;
6140 if (trans_oob(num_params, ofs + 12, len)) {
6141 TALLOC_FREE(params);
6142 tevent_req_nterror(
6143 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
6144 return;
6147 state->changes[i].action = IVAL(params, ofs+4);
6148 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
6149 &name, params+ofs+12, len,
6150 STR_TERMINATE|STR_UNICODE);
6151 if (ret == -1) {
6152 TALLOC_FREE(params);
6153 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
6154 return;
6156 state->changes[i].name = name;
6157 ofs += next;
6160 TALLOC_FREE(params);
6161 tevent_req_done(req);
6164 static void cli_notify_done_smb2(struct tevent_req *subreq)
6166 struct tevent_req *req = tevent_req_callback_data(
6167 subreq, struct tevent_req);
6168 struct cli_notify_state *state = tevent_req_data(
6169 req, struct cli_notify_state);
6170 NTSTATUS status;
6172 status = cli_smb2_notify_recv(
6173 subreq,
6174 state,
6175 &state->changes,
6176 &state->num_changes);
6177 TALLOC_FREE(subreq);
6178 if (tevent_req_nterror(req, status)) {
6179 return;
6181 tevent_req_done(req);
6184 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6185 uint32_t *pnum_changes,
6186 struct notify_change **pchanges)
6188 struct cli_notify_state *state = tevent_req_data(
6189 req, struct cli_notify_state);
6190 NTSTATUS status;
6192 if (tevent_req_is_nterror(req, &status)) {
6193 return status;
6196 *pnum_changes = state->num_changes;
6197 *pchanges = talloc_move(mem_ctx, &state->changes);
6198 return NT_STATUS_OK;
6201 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
6202 uint32_t completion_filter, bool recursive,
6203 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
6204 struct notify_change **pchanges)
6206 TALLOC_CTX *frame;
6207 struct tevent_context *ev;
6208 struct tevent_req *req;
6209 NTSTATUS status = NT_STATUS_NO_MEMORY;
6211 frame = talloc_stackframe();
6213 if (smbXcli_conn_has_async_calls(cli->conn)) {
6215 * Can't use sync call while an async call is in flight
6217 status = NT_STATUS_INVALID_PARAMETER;
6218 goto fail;
6220 ev = samba_tevent_context_init(frame);
6221 if (ev == NULL) {
6222 goto fail;
6224 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
6225 completion_filter, recursive);
6226 if (req == NULL) {
6227 goto fail;
6229 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6230 goto fail;
6232 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
6233 fail:
6234 TALLOC_FREE(frame);
6235 return status;
6238 struct cli_qpathinfo_state {
6239 uint8_t *param;
6240 uint8_t *data;
6241 uint16_t setup[1];
6242 uint32_t min_rdata;
6243 uint8_t *rdata;
6244 uint32_t num_rdata;
6247 static void cli_qpathinfo_done(struct tevent_req *subreq);
6249 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
6250 struct tevent_context *ev,
6251 struct cli_state *cli, const char *fname,
6252 uint16_t level, uint32_t min_rdata,
6253 uint32_t max_rdata)
6255 struct tevent_req *req, *subreq;
6256 struct cli_qpathinfo_state *state;
6257 uint16_t additional_flags2 = 0;
6259 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
6260 if (req == NULL) {
6261 return NULL;
6263 state->min_rdata = min_rdata;
6264 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
6266 state->param = talloc_zero_array(state, uint8_t, 6);
6267 if (tevent_req_nomem(state->param, req)) {
6268 return tevent_req_post(req, ev);
6270 SSVAL(state->param, 0, level);
6271 state->param = trans2_bytes_push_str(
6272 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
6273 if (tevent_req_nomem(state->param, req)) {
6274 return tevent_req_post(req, ev);
6277 if (clistr_is_previous_version_path(fname, NULL, NULL, NULL) &&
6278 !INFO_LEVEL_IS_UNIX(level)) {
6279 additional_flags2 = FLAGS2_REPARSE_PATH;
6282 subreq = cli_trans_send(
6283 state, /* mem ctx. */
6284 ev, /* event ctx. */
6285 cli, /* cli_state. */
6286 additional_flags2, /* additional_flags2 */
6287 SMBtrans2, /* cmd. */
6288 NULL, /* pipe name. */
6289 -1, /* fid. */
6290 0, /* function. */
6291 0, /* flags. */
6292 state->setup, /* setup. */
6293 1, /* num setup uint16_t words. */
6294 0, /* max returned setup. */
6295 state->param, /* param. */
6296 talloc_get_size(state->param), /* num param. */
6297 2, /* max returned param. */
6298 NULL, /* data. */
6299 0, /* num data. */
6300 max_rdata); /* max returned data. */
6302 if (tevent_req_nomem(subreq, req)) {
6303 return tevent_req_post(req, ev);
6305 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
6306 return req;
6309 static void cli_qpathinfo_done(struct tevent_req *subreq)
6311 struct tevent_req *req = tevent_req_callback_data(
6312 subreq, struct tevent_req);
6313 struct cli_qpathinfo_state *state = tevent_req_data(
6314 req, struct cli_qpathinfo_state);
6315 NTSTATUS status;
6317 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
6318 NULL, 0, NULL,
6319 &state->rdata, state->min_rdata,
6320 &state->num_rdata);
6321 if (tevent_req_nterror(req, status)) {
6322 return;
6324 tevent_req_done(req);
6327 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6328 uint8_t **rdata, uint32_t *num_rdata)
6330 struct cli_qpathinfo_state *state = tevent_req_data(
6331 req, struct cli_qpathinfo_state);
6332 NTSTATUS status;
6334 if (tevent_req_is_nterror(req, &status)) {
6335 return status;
6337 if (rdata != NULL) {
6338 *rdata = talloc_move(mem_ctx, &state->rdata);
6339 } else {
6340 TALLOC_FREE(state->rdata);
6342 if (num_rdata != NULL) {
6343 *num_rdata = state->num_rdata;
6345 return NT_STATUS_OK;
6348 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6349 const char *fname, uint16_t level, uint32_t min_rdata,
6350 uint32_t max_rdata,
6351 uint8_t **rdata, uint32_t *num_rdata)
6353 TALLOC_CTX *frame = talloc_stackframe();
6354 struct tevent_context *ev;
6355 struct tevent_req *req;
6356 NTSTATUS status = NT_STATUS_NO_MEMORY;
6358 if (smbXcli_conn_has_async_calls(cli->conn)) {
6360 * Can't use sync call while an async call is in flight
6362 status = NT_STATUS_INVALID_PARAMETER;
6363 goto fail;
6365 ev = samba_tevent_context_init(frame);
6366 if (ev == NULL) {
6367 goto fail;
6369 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
6370 max_rdata);
6371 if (req == NULL) {
6372 goto fail;
6374 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6375 goto fail;
6377 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
6378 fail:
6379 TALLOC_FREE(frame);
6380 return status;
6383 struct cli_qfileinfo_state {
6384 uint16_t setup[1];
6385 uint8_t param[4];
6386 uint8_t *data;
6387 uint16_t recv_flags2;
6388 uint32_t min_rdata;
6389 uint8_t *rdata;
6390 uint32_t num_rdata;
6393 static void cli_qfileinfo_done(struct tevent_req *subreq);
6395 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
6396 struct tevent_context *ev,
6397 struct cli_state *cli, uint16_t fnum,
6398 uint16_t level, uint32_t min_rdata,
6399 uint32_t max_rdata)
6401 struct tevent_req *req, *subreq;
6402 struct cli_qfileinfo_state *state;
6404 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
6405 if (req == NULL) {
6406 return NULL;
6408 state->min_rdata = min_rdata;
6409 SSVAL(state->param, 0, fnum);
6410 SSVAL(state->param, 2, level);
6411 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
6413 subreq = cli_trans_send(
6414 state, /* mem ctx. */
6415 ev, /* event ctx. */
6416 cli, /* cli_state. */
6417 0, /* additional_flags2 */
6418 SMBtrans2, /* cmd. */
6419 NULL, /* pipe name. */
6420 -1, /* fid. */
6421 0, /* function. */
6422 0, /* flags. */
6423 state->setup, /* setup. */
6424 1, /* num setup uint16_t words. */
6425 0, /* max returned setup. */
6426 state->param, /* param. */
6427 sizeof(state->param), /* num param. */
6428 2, /* max returned param. */
6429 NULL, /* data. */
6430 0, /* num data. */
6431 max_rdata); /* max returned data. */
6433 if (tevent_req_nomem(subreq, req)) {
6434 return tevent_req_post(req, ev);
6436 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
6437 return req;
6440 static void cli_qfileinfo_done(struct tevent_req *subreq)
6442 struct tevent_req *req = tevent_req_callback_data(
6443 subreq, struct tevent_req);
6444 struct cli_qfileinfo_state *state = tevent_req_data(
6445 req, struct cli_qfileinfo_state);
6446 NTSTATUS status;
6448 status = cli_trans_recv(subreq, state,
6449 &state->recv_flags2,
6450 NULL, 0, NULL,
6451 NULL, 0, NULL,
6452 &state->rdata, state->min_rdata,
6453 &state->num_rdata);
6454 if (tevent_req_nterror(req, status)) {
6455 return;
6457 tevent_req_done(req);
6460 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6461 uint16_t *recv_flags2,
6462 uint8_t **rdata, uint32_t *num_rdata)
6464 struct cli_qfileinfo_state *state = tevent_req_data(
6465 req, struct cli_qfileinfo_state);
6466 NTSTATUS status;
6468 if (tevent_req_is_nterror(req, &status)) {
6469 return status;
6472 if (recv_flags2 != NULL) {
6473 *recv_flags2 = state->recv_flags2;
6475 if (rdata != NULL) {
6476 *rdata = talloc_move(mem_ctx, &state->rdata);
6477 } else {
6478 TALLOC_FREE(state->rdata);
6480 if (num_rdata != NULL) {
6481 *num_rdata = state->num_rdata;
6483 return NT_STATUS_OK;
6486 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6487 uint16_t fnum, uint16_t level, uint32_t min_rdata,
6488 uint32_t max_rdata, uint16_t *recv_flags2,
6489 uint8_t **rdata, uint32_t *num_rdata)
6491 TALLOC_CTX *frame = talloc_stackframe();
6492 struct tevent_context *ev;
6493 struct tevent_req *req;
6494 NTSTATUS status = NT_STATUS_NO_MEMORY;
6496 if (smbXcli_conn_has_async_calls(cli->conn)) {
6498 * Can't use sync call while an async call is in flight
6500 status = NT_STATUS_INVALID_PARAMETER;
6501 goto fail;
6503 ev = samba_tevent_context_init(frame);
6504 if (ev == NULL) {
6505 goto fail;
6507 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
6508 max_rdata);
6509 if (req == NULL) {
6510 goto fail;
6512 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6513 goto fail;
6515 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
6516 fail:
6517 TALLOC_FREE(frame);
6518 return status;
6521 struct cli_flush_state {
6522 uint16_t vwv[1];
6525 static void cli_flush_done(struct tevent_req *subreq);
6527 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
6528 struct tevent_context *ev,
6529 struct cli_state *cli,
6530 uint16_t fnum)
6532 struct tevent_req *req, *subreq;
6533 struct cli_flush_state *state;
6535 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
6536 if (req == NULL) {
6537 return NULL;
6539 SSVAL(state->vwv + 0, 0, fnum);
6541 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
6542 0, NULL);
6543 if (tevent_req_nomem(subreq, req)) {
6544 return tevent_req_post(req, ev);
6546 tevent_req_set_callback(subreq, cli_flush_done, req);
6547 return req;
6550 static void cli_flush_done(struct tevent_req *subreq)
6552 struct tevent_req *req = tevent_req_callback_data(
6553 subreq, struct tevent_req);
6554 NTSTATUS status;
6556 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
6557 TALLOC_FREE(subreq);
6558 if (tevent_req_nterror(req, status)) {
6559 return;
6561 tevent_req_done(req);
6564 NTSTATUS cli_flush_recv(struct tevent_req *req)
6566 return tevent_req_simple_recv_ntstatus(req);
6569 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
6571 TALLOC_CTX *frame = talloc_stackframe();
6572 struct tevent_context *ev;
6573 struct tevent_req *req;
6574 NTSTATUS status = NT_STATUS_NO_MEMORY;
6576 if (smbXcli_conn_has_async_calls(cli->conn)) {
6578 * Can't use sync call while an async call is in flight
6580 status = NT_STATUS_INVALID_PARAMETER;
6581 goto fail;
6583 ev = samba_tevent_context_init(frame);
6584 if (ev == NULL) {
6585 goto fail;
6587 req = cli_flush_send(frame, ev, cli, fnum);
6588 if (req == NULL) {
6589 goto fail;
6591 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6592 goto fail;
6594 status = cli_flush_recv(req);
6595 fail:
6596 TALLOC_FREE(frame);
6597 return status;
6600 struct cli_shadow_copy_data_state {
6601 uint16_t setup[4];
6602 uint8_t *data;
6603 uint32_t num_data;
6604 bool get_names;
6607 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
6609 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
6610 struct tevent_context *ev,
6611 struct cli_state *cli,
6612 uint16_t fnum,
6613 bool get_names)
6615 struct tevent_req *req, *subreq;
6616 struct cli_shadow_copy_data_state *state;
6617 uint32_t ret_size;
6619 req = tevent_req_create(mem_ctx, &state,
6620 struct cli_shadow_copy_data_state);
6621 if (req == NULL) {
6622 return NULL;
6624 state->get_names = get_names;
6625 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
6627 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
6628 SSVAL(state->setup + 2, 0, fnum);
6629 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
6630 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
6632 subreq = cli_trans_send(
6633 state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
6634 state->setup, ARRAY_SIZE(state->setup),
6635 ARRAY_SIZE(state->setup),
6636 NULL, 0, 0,
6637 NULL, 0, ret_size);
6638 if (tevent_req_nomem(subreq, req)) {
6639 return tevent_req_post(req, ev);
6641 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
6642 return req;
6645 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
6647 struct tevent_req *req = tevent_req_callback_data(
6648 subreq, struct tevent_req);
6649 struct cli_shadow_copy_data_state *state = tevent_req_data(
6650 req, struct cli_shadow_copy_data_state);
6651 NTSTATUS status;
6653 status = cli_trans_recv(subreq, state, NULL,
6654 NULL, 0, NULL, /* setup */
6655 NULL, 0, NULL, /* param */
6656 &state->data, 12, &state->num_data);
6657 TALLOC_FREE(subreq);
6658 if (tevent_req_nterror(req, status)) {
6659 return;
6661 tevent_req_done(req);
6664 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
6665 char ***pnames, int *pnum_names)
6667 struct cli_shadow_copy_data_state *state = tevent_req_data(
6668 req, struct cli_shadow_copy_data_state);
6669 char **names = NULL;
6670 uint32_t i, num_names;
6671 uint32_t dlength;
6672 uint8_t *endp = NULL;
6673 NTSTATUS status;
6675 if (tevent_req_is_nterror(req, &status)) {
6676 return status;
6679 if (state->num_data < 16) {
6680 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6683 num_names = IVAL(state->data, 4);
6684 dlength = IVAL(state->data, 8);
6686 if (num_names > 0x7FFFFFFF) {
6687 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6690 if (!state->get_names) {
6691 *pnum_names = (int)num_names;
6692 return NT_STATUS_OK;
6695 if (dlength + 12 < 12) {
6696 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6698 if (dlength + 12 > state->num_data) {
6699 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6701 if (state->num_data + (2 * sizeof(SHADOW_COPY_LABEL)) <
6702 state->num_data) {
6703 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6706 names = talloc_array(mem_ctx, char *, num_names);
6707 if (names == NULL) {
6708 return NT_STATUS_NO_MEMORY;
6711 endp = state->data + state->num_data;
6713 for (i=0; i<num_names; i++) {
6714 bool ret;
6715 uint8_t *src;
6716 size_t converted_size;
6718 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
6720 if (src + (2 * sizeof(SHADOW_COPY_LABEL)) > endp) {
6721 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6724 ret = convert_string_talloc(
6725 names, CH_UTF16LE, CH_UNIX,
6726 src, 2 * sizeof(SHADOW_COPY_LABEL),
6727 &names[i], &converted_size);
6728 if (!ret) {
6729 TALLOC_FREE(names);
6730 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6733 *pnum_names = (int)num_names;
6734 *pnames = names;
6735 return NT_STATUS_OK;
6738 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6739 uint16_t fnum, bool get_names,
6740 char ***pnames, int *pnum_names)
6742 TALLOC_CTX *frame = NULL;
6743 struct tevent_context *ev;
6744 struct tevent_req *req;
6745 NTSTATUS status = NT_STATUS_NO_MEMORY;
6747 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
6748 return cli_smb2_shadow_copy_data(mem_ctx,
6749 cli,
6750 fnum,
6751 get_names,
6752 pnames,
6753 pnum_names);
6756 frame = talloc_stackframe();
6758 if (smbXcli_conn_has_async_calls(cli->conn)) {
6760 * Can't use sync call while an async call is in flight
6762 status = NT_STATUS_INVALID_PARAMETER;
6763 goto fail;
6765 ev = samba_tevent_context_init(frame);
6766 if (ev == NULL) {
6767 goto fail;
6769 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
6770 if (req == NULL) {
6771 goto fail;
6773 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6774 goto fail;
6776 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
6777 fail:
6778 TALLOC_FREE(frame);
6779 return status;