libsmb: Replace async cli_ntcreate by cli_create
[Samba.git] / source3 / libsmb / clifile.c
blob40c41d9eb3b88933e20830142200fa4fb2b541eb
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/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 /***********************************************************
33 Common function for pushing stings, used by smb_bytes_push_str()
34 and trans_bytes_push_str(). Only difference is the align_odd
35 parameter setting.
36 ***********************************************************/
38 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
39 const char *str, size_t str_len,
40 bool align_odd,
41 size_t *pconverted_size)
43 size_t buflen;
44 char *converted;
45 size_t converted_size;
47 if (buf == NULL) {
48 return NULL;
51 buflen = talloc_get_size(buf);
53 if (ucs2 &&
54 ((align_odd && (buflen % 2 == 0)) ||
55 (!align_odd && (buflen % 2 == 1)))) {
57 * We're pushing into an SMB buffer, align odd
59 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
60 if (buf == NULL) {
61 return NULL;
63 buf[buflen] = '\0';
64 buflen += 1;
67 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
68 ucs2 ? CH_UTF16LE : CH_DOS,
69 str, str_len, &converted,
70 &converted_size)) {
71 return NULL;
74 buf = talloc_realloc(NULL, buf, uint8_t,
75 buflen + converted_size);
76 if (buf == NULL) {
77 TALLOC_FREE(converted);
78 return NULL;
81 memcpy(buf + buflen, converted, converted_size);
83 TALLOC_FREE(converted);
85 if (pconverted_size) {
86 *pconverted_size = converted_size;
89 return buf;
92 /***********************************************************
93 Push a string into an SMB buffer, with odd byte alignment
94 if it's a UCS2 string.
95 ***********************************************************/
97 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
98 const char *str, size_t str_len,
99 size_t *pconverted_size)
101 return internal_bytes_push_str(buf, ucs2, str, str_len,
102 true, pconverted_size);
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
106 const uint8_t *bytes, size_t num_bytes)
108 size_t buflen;
110 if (buf == NULL) {
111 return NULL;
113 buflen = talloc_get_size(buf);
115 buf = talloc_realloc(NULL, buf, uint8_t,
116 buflen + 1 + num_bytes);
117 if (buf == NULL) {
118 return NULL;
120 buf[buflen] = prefix;
121 memcpy(&buf[buflen+1], bytes, num_bytes);
122 return buf;
125 /***********************************************************
126 Same as smb_bytes_push_str(), but without the odd byte
127 align for ucs2 (we're pushing into a param or data block).
128 static for now, although this will probably change when
129 other modules use async trans calls.
130 ***********************************************************/
132 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
133 const char *str, size_t str_len,
134 size_t *pconverted_size)
136 return internal_bytes_push_str(buf, ucs2, str, str_len,
137 false, pconverted_size);
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
141 const uint8_t *bytes, size_t num_bytes)
143 size_t buflen;
145 if (buf == NULL) {
146 return NULL;
148 buflen = talloc_get_size(buf);
150 buf = talloc_realloc(NULL, buf, uint8_t,
151 buflen + num_bytes);
152 if (buf == NULL) {
153 return NULL;
155 memcpy(&buf[buflen], bytes, num_bytes);
156 return buf;
159 struct cli_setpathinfo_state {
160 uint16_t setup;
161 uint8_t *param;
164 static void cli_setpathinfo_done(struct tevent_req *subreq);
166 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
167 struct tevent_context *ev,
168 struct cli_state *cli,
169 uint16_t level,
170 const char *path,
171 uint8_t *data,
172 size_t data_len)
174 struct tevent_req *req, *subreq;
175 struct cli_setpathinfo_state *state;
177 req = tevent_req_create(mem_ctx, &state,
178 struct cli_setpathinfo_state);
179 if (req == NULL) {
180 return NULL;
183 /* Setup setup word. */
184 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
186 /* Setup param array. */
187 state->param = talloc_zero_array(state, uint8_t, 6);
188 if (tevent_req_nomem(state->param, req)) {
189 return tevent_req_post(req, ev);
191 SSVAL(state->param, 0, level);
193 state->param = trans2_bytes_push_str(
194 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
195 if (tevent_req_nomem(state->param, req)) {
196 return tevent_req_post(req, ev);
199 subreq = cli_trans_send(
200 state, /* mem ctx. */
201 ev, /* event ctx. */
202 cli, /* cli_state. */
203 SMBtrans2, /* cmd. */
204 NULL, /* pipe name. */
205 -1, /* fid. */
206 0, /* function. */
207 0, /* flags. */
208 &state->setup, /* setup. */
209 1, /* num setup uint16_t words. */
210 0, /* max returned setup. */
211 state->param, /* param. */
212 talloc_get_size(state->param), /* num param. */
213 2, /* max returned param. */
214 data, /* data. */
215 data_len, /* num data. */
216 0); /* max returned data. */
218 if (tevent_req_nomem(subreq, req)) {
219 return tevent_req_post(req, ev);
221 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
222 return req;
225 static void cli_setpathinfo_done(struct tevent_req *subreq)
227 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
228 NULL, 0, NULL, NULL, 0, NULL);
229 tevent_req_simple_finish_ntstatus(subreq, status);
232 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
234 return tevent_req_simple_recv_ntstatus(req);
237 NTSTATUS cli_setpathinfo(struct cli_state *cli,
238 uint16_t level,
239 const char *path,
240 uint8_t *data,
241 size_t data_len)
243 TALLOC_CTX *frame = talloc_stackframe();
244 struct tevent_context *ev;
245 struct tevent_req *req;
246 NTSTATUS status = NT_STATUS_NO_MEMORY;
248 if (smbXcli_conn_has_async_calls(cli->conn)) {
250 * Can't use sync call while an async call is in flight
252 status = NT_STATUS_INVALID_PARAMETER;
253 goto fail;
255 ev = samba_tevent_context_init(frame);
256 if (ev == NULL) {
257 goto fail;
259 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
260 if (req == NULL) {
261 goto fail;
263 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
264 goto fail;
266 status = cli_setpathinfo_recv(req);
267 fail:
268 TALLOC_FREE(frame);
269 return status;
272 /****************************************************************************
273 Hard/Symlink a file (UNIX extensions).
274 Creates new name (sym)linked to oldname.
275 ****************************************************************************/
277 struct cli_posix_link_internal_state {
278 uint8_t *data;
281 static void cli_posix_link_internal_done(struct tevent_req *subreq);
283 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
284 struct tevent_context *ev,
285 struct cli_state *cli,
286 uint16_t level,
287 const char *oldname,
288 const char *newname)
290 struct tevent_req *req = NULL, *subreq = NULL;
291 struct cli_posix_link_internal_state *state = NULL;
293 req = tevent_req_create(mem_ctx, &state,
294 struct cli_posix_link_internal_state);
295 if (req == NULL) {
296 return NULL;
299 /* Setup data array. */
300 state->data = talloc_array(state, uint8_t, 0);
301 if (tevent_req_nomem(state->data, req)) {
302 return tevent_req_post(req, ev);
304 state->data = trans2_bytes_push_str(
305 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
307 subreq = cli_setpathinfo_send(
308 state, ev, cli, level, newname,
309 state->data, talloc_get_size(state->data));
310 if (tevent_req_nomem(subreq, req)) {
311 return tevent_req_post(req, ev);
313 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
314 return req;
317 static void cli_posix_link_internal_done(struct tevent_req *subreq)
319 NTSTATUS status = cli_setpathinfo_recv(subreq);
320 tevent_req_simple_finish_ntstatus(subreq, status);
323 /****************************************************************************
324 Symlink a file (UNIX extensions).
325 ****************************************************************************/
327 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
328 struct tevent_context *ev,
329 struct cli_state *cli,
330 const char *oldname,
331 const char *newname)
333 return cli_posix_link_internal_send(
334 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
337 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
339 return tevent_req_simple_recv_ntstatus(req);
342 NTSTATUS cli_posix_symlink(struct cli_state *cli,
343 const char *oldname,
344 const char *newname)
346 TALLOC_CTX *frame = talloc_stackframe();
347 struct tevent_context *ev = NULL;
348 struct tevent_req *req = NULL;
349 NTSTATUS status = NT_STATUS_OK;
351 if (smbXcli_conn_has_async_calls(cli->conn)) {
353 * Can't use sync call while an async call is in flight
355 status = NT_STATUS_INVALID_PARAMETER;
356 goto fail;
359 ev = samba_tevent_context_init(frame);
360 if (ev == NULL) {
361 status = NT_STATUS_NO_MEMORY;
362 goto fail;
365 req = cli_posix_symlink_send(frame,
367 cli,
368 oldname,
369 newname);
370 if (req == NULL) {
371 status = NT_STATUS_NO_MEMORY;
372 goto fail;
375 if (!tevent_req_poll(req, ev)) {
376 status = map_nt_error_from_unix(errno);
377 goto fail;
380 status = cli_posix_symlink_recv(req);
382 fail:
383 TALLOC_FREE(frame);
384 return status;
387 /****************************************************************************
388 Read a POSIX symlink.
389 ****************************************************************************/
391 struct readlink_state {
392 uint8_t *data;
393 uint32_t num_data;
396 static void cli_posix_readlink_done(struct tevent_req *subreq);
398 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
399 struct tevent_context *ev,
400 struct cli_state *cli,
401 const char *fname,
402 size_t len)
404 struct tevent_req *req = NULL, *subreq = NULL;
405 struct readlink_state *state = NULL;
406 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
408 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
409 if (req == NULL) {
410 return NULL;
414 * Len is in bytes, we need it in UCS2 units.
416 if ((2*len < len) || (maxbytelen < len)) {
417 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
418 return tevent_req_post(req, ev);
421 subreq = cli_qpathinfo_send(state, ev, cli, fname,
422 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
423 if (tevent_req_nomem(subreq, req)) {
424 return tevent_req_post(req, ev);
426 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
427 return req;
430 static void cli_posix_readlink_done(struct tevent_req *subreq)
432 struct tevent_req *req = tevent_req_callback_data(
433 subreq, struct tevent_req);
434 struct readlink_state *state = tevent_req_data(
435 req, struct readlink_state);
436 NTSTATUS status;
438 status = cli_qpathinfo_recv(subreq, state, &state->data,
439 &state->num_data);
440 TALLOC_FREE(subreq);
441 if (tevent_req_nterror(req, status)) {
442 return;
445 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
447 if (state->data[state->num_data-1] != '\0') {
448 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
449 return;
451 tevent_req_done(req);
454 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
455 char *retpath, size_t len)
457 NTSTATUS status;
458 char *converted = NULL;
459 size_t converted_size = 0;
460 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
462 if (tevent_req_is_nterror(req, &status)) {
463 return status;
465 /* The returned data is a pushed string, not raw data. */
466 if (!convert_string_talloc(state,
467 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
468 CH_UNIX,
469 state->data,
470 state->num_data,
471 &converted,
472 &converted_size)) {
473 return NT_STATUS_NO_MEMORY;
476 len = MIN(len,converted_size);
477 if (len == 0) {
478 return NT_STATUS_DATA_ERROR;
480 memcpy(retpath, converted, len);
481 return NT_STATUS_OK;
484 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
485 char *linkpath, size_t len)
487 TALLOC_CTX *frame = talloc_stackframe();
488 struct tevent_context *ev = NULL;
489 struct tevent_req *req = NULL;
490 NTSTATUS status = NT_STATUS_OK;
492 if (smbXcli_conn_has_async_calls(cli->conn)) {
494 * Can't use sync call while an async call is in flight
496 status = NT_STATUS_INVALID_PARAMETER;
497 goto fail;
500 ev = samba_tevent_context_init(frame);
501 if (ev == NULL) {
502 status = NT_STATUS_NO_MEMORY;
503 goto fail;
506 req = cli_posix_readlink_send(frame,
508 cli,
509 fname,
510 len);
511 if (req == NULL) {
512 status = NT_STATUS_NO_MEMORY;
513 goto fail;
516 if (!tevent_req_poll(req, ev)) {
517 status = map_nt_error_from_unix(errno);
518 goto fail;
521 status = cli_posix_readlink_recv(req, cli, linkpath, len);
523 fail:
524 TALLOC_FREE(frame);
525 return status;
528 /****************************************************************************
529 Hard link a file (UNIX extensions).
530 ****************************************************************************/
532 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
533 struct tevent_context *ev,
534 struct cli_state *cli,
535 const char *oldname,
536 const char *newname)
538 return cli_posix_link_internal_send(
539 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
542 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
544 return tevent_req_simple_recv_ntstatus(req);
547 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
548 const char *oldname,
549 const char *newname)
551 TALLOC_CTX *frame = talloc_stackframe();
552 struct tevent_context *ev = NULL;
553 struct tevent_req *req = NULL;
554 NTSTATUS status = NT_STATUS_OK;
556 if (smbXcli_conn_has_async_calls(cli->conn)) {
558 * Can't use sync call while an async call is in flight
560 status = NT_STATUS_INVALID_PARAMETER;
561 goto fail;
564 ev = samba_tevent_context_init(frame);
565 if (ev == NULL) {
566 status = NT_STATUS_NO_MEMORY;
567 goto fail;
570 req = cli_posix_hardlink_send(frame,
572 cli,
573 oldname,
574 newname);
575 if (req == NULL) {
576 status = NT_STATUS_NO_MEMORY;
577 goto fail;
580 if (!tevent_req_poll(req, ev)) {
581 status = map_nt_error_from_unix(errno);
582 goto fail;
585 status = cli_posix_hardlink_recv(req);
587 fail:
588 TALLOC_FREE(frame);
589 return status;
592 /****************************************************************************
593 Do a POSIX getfacl (UNIX extensions).
594 ****************************************************************************/
596 struct getfacl_state {
597 uint32_t num_data;
598 uint8_t *data;
601 static void cli_posix_getfacl_done(struct tevent_req *subreq);
603 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
604 struct tevent_context *ev,
605 struct cli_state *cli,
606 const char *fname)
608 struct tevent_req *req = NULL, *subreq = NULL;
609 struct getfacl_state *state = NULL;
611 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
612 if (req == NULL) {
613 return NULL;
615 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
616 0, CLI_BUFFER_SIZE);
617 if (tevent_req_nomem(subreq, req)) {
618 return tevent_req_post(req, ev);
620 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
621 return req;
624 static void cli_posix_getfacl_done(struct tevent_req *subreq)
626 struct tevent_req *req = tevent_req_callback_data(
627 subreq, struct tevent_req);
628 struct getfacl_state *state = tevent_req_data(
629 req, struct getfacl_state);
630 NTSTATUS status;
632 status = cli_qpathinfo_recv(subreq, state, &state->data,
633 &state->num_data);
634 TALLOC_FREE(subreq);
635 if (tevent_req_nterror(req, status)) {
636 return;
638 tevent_req_done(req);
641 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
642 TALLOC_CTX *mem_ctx,
643 size_t *prb_size,
644 char **retbuf)
646 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
647 NTSTATUS status;
649 if (tevent_req_is_nterror(req, &status)) {
650 return status;
652 *prb_size = (size_t)state->num_data;
653 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
654 return NT_STATUS_OK;
657 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
658 const char *fname,
659 TALLOC_CTX *mem_ctx,
660 size_t *prb_size,
661 char **retbuf)
663 TALLOC_CTX *frame = talloc_stackframe();
664 struct tevent_context *ev = NULL;
665 struct tevent_req *req = NULL;
666 NTSTATUS status = NT_STATUS_OK;
668 if (smbXcli_conn_has_async_calls(cli->conn)) {
670 * Can't use sync call while an async call is in flight
672 status = NT_STATUS_INVALID_PARAMETER;
673 goto fail;
676 ev = samba_tevent_context_init(frame);
677 if (ev == NULL) {
678 status = NT_STATUS_NO_MEMORY;
679 goto fail;
682 req = cli_posix_getfacl_send(frame,
684 cli,
685 fname);
686 if (req == NULL) {
687 status = NT_STATUS_NO_MEMORY;
688 goto fail;
691 if (!tevent_req_poll(req, ev)) {
692 status = map_nt_error_from_unix(errno);
693 goto fail;
696 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
698 fail:
699 TALLOC_FREE(frame);
700 return status;
703 /****************************************************************************
704 Stat a file (UNIX extensions).
705 ****************************************************************************/
707 struct stat_state {
708 uint32_t num_data;
709 uint8_t *data;
712 static void cli_posix_stat_done(struct tevent_req *subreq);
714 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
715 struct tevent_context *ev,
716 struct cli_state *cli,
717 const char *fname)
719 struct tevent_req *req = NULL, *subreq = NULL;
720 struct stat_state *state = NULL;
722 req = tevent_req_create(mem_ctx, &state, struct stat_state);
723 if (req == NULL) {
724 return NULL;
726 subreq = cli_qpathinfo_send(state, ev, cli, fname,
727 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
728 if (tevent_req_nomem(subreq, req)) {
729 return tevent_req_post(req, ev);
731 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
732 return req;
735 static void cli_posix_stat_done(struct tevent_req *subreq)
737 struct tevent_req *req = tevent_req_callback_data(
738 subreq, struct tevent_req);
739 struct stat_state *state = tevent_req_data(req, struct stat_state);
740 NTSTATUS status;
742 status = cli_qpathinfo_recv(subreq, state, &state->data,
743 &state->num_data);
744 TALLOC_FREE(subreq);
745 if (tevent_req_nterror(req, status)) {
746 return;
748 tevent_req_done(req);
751 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
752 SMB_STRUCT_STAT *sbuf)
754 struct stat_state *state = tevent_req_data(req, struct stat_state);
755 NTSTATUS status;
757 if (tevent_req_is_nterror(req, &status)) {
758 return status;
761 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
762 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
763 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
764 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
765 #else
766 /* assume 512 byte blocks */
767 sbuf->st_ex_blocks /= 512;
768 #endif
769 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
770 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
771 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
773 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
774 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
775 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
776 #if defined(HAVE_MAKEDEV)
778 uint32_t dev_major = IVAL(state->data,60);
779 uint32_t dev_minor = IVAL(state->data,68);
780 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
782 #endif
783 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
784 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
785 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
787 return NT_STATUS_OK;
790 NTSTATUS cli_posix_stat(struct cli_state *cli,
791 const char *fname,
792 SMB_STRUCT_STAT *sbuf)
794 TALLOC_CTX *frame = talloc_stackframe();
795 struct tevent_context *ev = NULL;
796 struct tevent_req *req = NULL;
797 NTSTATUS status = NT_STATUS_OK;
799 if (smbXcli_conn_has_async_calls(cli->conn)) {
801 * Can't use sync call while an async call is in flight
803 status = NT_STATUS_INVALID_PARAMETER;
804 goto fail;
807 ev = samba_tevent_context_init(frame);
808 if (ev == NULL) {
809 status = NT_STATUS_NO_MEMORY;
810 goto fail;
813 req = cli_posix_stat_send(frame,
815 cli,
816 fname);
817 if (req == NULL) {
818 status = NT_STATUS_NO_MEMORY;
819 goto fail;
822 if (!tevent_req_poll(req, ev)) {
823 status = map_nt_error_from_unix(errno);
824 goto fail;
827 status = cli_posix_stat_recv(req, sbuf);
829 fail:
830 TALLOC_FREE(frame);
831 return status;
834 /****************************************************************************
835 Chmod or chown a file internal (UNIX extensions).
836 ****************************************************************************/
838 struct cli_posix_chown_chmod_internal_state {
839 uint8_t data[100];
842 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
844 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
845 struct tevent_context *ev,
846 struct cli_state *cli,
847 const char *fname,
848 uint32_t mode,
849 uint32_t uid,
850 uint32_t gid)
852 struct tevent_req *req = NULL, *subreq = NULL;
853 struct cli_posix_chown_chmod_internal_state *state = NULL;
855 req = tevent_req_create(mem_ctx, &state,
856 struct cli_posix_chown_chmod_internal_state);
857 if (req == NULL) {
858 return NULL;
861 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
862 memset(&state->data[40], '\0', 60);
863 SIVAL(state->data,40,uid);
864 SIVAL(state->data,48,gid);
865 SIVAL(state->data,84,mode);
867 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
868 fname, state->data, sizeof(state->data));
869 if (tevent_req_nomem(subreq, req)) {
870 return tevent_req_post(req, ev);
872 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
873 req);
874 return req;
877 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
879 NTSTATUS status = cli_setpathinfo_recv(subreq);
880 tevent_req_simple_finish_ntstatus(subreq, status);
883 /****************************************************************************
884 chmod a file (UNIX extensions).
885 ****************************************************************************/
887 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
888 struct tevent_context *ev,
889 struct cli_state *cli,
890 const char *fname,
891 mode_t mode)
893 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
894 fname,
895 unix_perms_to_wire(mode),
896 SMB_UID_NO_CHANGE,
897 SMB_GID_NO_CHANGE);
900 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
902 return tevent_req_simple_recv_ntstatus(req);
905 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
907 TALLOC_CTX *frame = talloc_stackframe();
908 struct tevent_context *ev = NULL;
909 struct tevent_req *req = NULL;
910 NTSTATUS status = NT_STATUS_OK;
912 if (smbXcli_conn_has_async_calls(cli->conn)) {
914 * Can't use sync call while an async call is in flight
916 status = NT_STATUS_INVALID_PARAMETER;
917 goto fail;
920 ev = samba_tevent_context_init(frame);
921 if (ev == NULL) {
922 status = NT_STATUS_NO_MEMORY;
923 goto fail;
926 req = cli_posix_chmod_send(frame,
928 cli,
929 fname,
930 mode);
931 if (req == NULL) {
932 status = NT_STATUS_NO_MEMORY;
933 goto fail;
936 if (!tevent_req_poll(req, ev)) {
937 status = map_nt_error_from_unix(errno);
938 goto fail;
941 status = cli_posix_chmod_recv(req);
943 fail:
944 TALLOC_FREE(frame);
945 return status;
948 /****************************************************************************
949 chown a file (UNIX extensions).
950 ****************************************************************************/
952 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
953 struct tevent_context *ev,
954 struct cli_state *cli,
955 const char *fname,
956 uid_t uid,
957 gid_t gid)
959 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
960 fname,
961 SMB_MODE_NO_CHANGE,
962 (uint32_t)uid,
963 (uint32_t)gid);
966 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
968 return tevent_req_simple_recv_ntstatus(req);
971 NTSTATUS cli_posix_chown(struct cli_state *cli,
972 const char *fname,
973 uid_t uid,
974 gid_t gid)
976 TALLOC_CTX *frame = talloc_stackframe();
977 struct tevent_context *ev = NULL;
978 struct tevent_req *req = NULL;
979 NTSTATUS status = NT_STATUS_OK;
981 if (smbXcli_conn_has_async_calls(cli->conn)) {
983 * Can't use sync call while an async call is in flight
985 status = NT_STATUS_INVALID_PARAMETER;
986 goto fail;
989 ev = samba_tevent_context_init(frame);
990 if (ev == NULL) {
991 status = NT_STATUS_NO_MEMORY;
992 goto fail;
995 req = cli_posix_chown_send(frame,
997 cli,
998 fname,
999 uid,
1000 gid);
1001 if (req == NULL) {
1002 status = NT_STATUS_NO_MEMORY;
1003 goto fail;
1006 if (!tevent_req_poll(req, ev)) {
1007 status = map_nt_error_from_unix(errno);
1008 goto fail;
1011 status = cli_posix_chown_recv(req);
1013 fail:
1014 TALLOC_FREE(frame);
1015 return status;
1018 /****************************************************************************
1019 Rename a file.
1020 ****************************************************************************/
1022 static void cli_rename_done(struct tevent_req *subreq);
1024 struct cli_rename_state {
1025 uint16_t vwv[1];
1028 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1029 struct tevent_context *ev,
1030 struct cli_state *cli,
1031 const char *fname_src,
1032 const char *fname_dst)
1034 struct tevent_req *req = NULL, *subreq = NULL;
1035 struct cli_rename_state *state = NULL;
1036 uint8_t additional_flags = 0;
1037 uint8_t *bytes = NULL;
1039 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1040 if (req == NULL) {
1041 return NULL;
1044 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1046 bytes = talloc_array(state, uint8_t, 1);
1047 if (tevent_req_nomem(bytes, req)) {
1048 return tevent_req_post(req, ev);
1050 bytes[0] = 4;
1051 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1052 strlen(fname_src)+1, NULL);
1053 if (tevent_req_nomem(bytes, req)) {
1054 return tevent_req_post(req, ev);
1057 bytes = talloc_realloc(state, bytes, uint8_t,
1058 talloc_get_size(bytes)+1);
1059 if (tevent_req_nomem(bytes, req)) {
1060 return tevent_req_post(req, ev);
1063 bytes[talloc_get_size(bytes)-1] = 4;
1064 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1065 strlen(fname_dst)+1, NULL);
1066 if (tevent_req_nomem(bytes, req)) {
1067 return tevent_req_post(req, ev);
1070 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1071 1, state->vwv, talloc_get_size(bytes), bytes);
1072 if (tevent_req_nomem(subreq, req)) {
1073 return tevent_req_post(req, ev);
1075 tevent_req_set_callback(subreq, cli_rename_done, req);
1076 return req;
1079 static void cli_rename_done(struct tevent_req *subreq)
1081 struct tevent_req *req = tevent_req_callback_data(
1082 subreq, struct tevent_req);
1083 NTSTATUS status;
1085 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1086 TALLOC_FREE(subreq);
1087 if (tevent_req_nterror(req, status)) {
1088 return;
1090 tevent_req_done(req);
1093 NTSTATUS cli_rename_recv(struct tevent_req *req)
1095 return tevent_req_simple_recv_ntstatus(req);
1098 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1100 TALLOC_CTX *frame = NULL;
1101 struct tevent_context *ev;
1102 struct tevent_req *req;
1103 NTSTATUS status = NT_STATUS_OK;
1105 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1106 return cli_smb2_rename(cli,
1107 fname_src,
1108 fname_dst);
1111 frame = talloc_stackframe();
1113 if (smbXcli_conn_has_async_calls(cli->conn)) {
1115 * Can't use sync call while an async call is in flight
1117 status = NT_STATUS_INVALID_PARAMETER;
1118 goto fail;
1121 ev = samba_tevent_context_init(frame);
1122 if (ev == NULL) {
1123 status = NT_STATUS_NO_MEMORY;
1124 goto fail;
1127 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1128 if (req == NULL) {
1129 status = NT_STATUS_NO_MEMORY;
1130 goto fail;
1133 if (!tevent_req_poll(req, ev)) {
1134 status = map_nt_error_from_unix(errno);
1135 goto fail;
1138 status = cli_rename_recv(req);
1140 fail:
1141 TALLOC_FREE(frame);
1142 return status;
1145 /****************************************************************************
1146 NT Rename a file.
1147 ****************************************************************************/
1149 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1151 struct cli_ntrename_internal_state {
1152 uint16_t vwv[4];
1155 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1156 struct tevent_context *ev,
1157 struct cli_state *cli,
1158 const char *fname_src,
1159 const char *fname_dst,
1160 uint16_t rename_flag)
1162 struct tevent_req *req = NULL, *subreq = NULL;
1163 struct cli_ntrename_internal_state *state = NULL;
1164 uint8_t additional_flags = 0;
1165 uint8_t *bytes = NULL;
1167 req = tevent_req_create(mem_ctx, &state,
1168 struct cli_ntrename_internal_state);
1169 if (req == NULL) {
1170 return NULL;
1173 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1174 SSVAL(state->vwv+1, 0, rename_flag);
1176 bytes = talloc_array(state, uint8_t, 1);
1177 if (tevent_req_nomem(bytes, req)) {
1178 return tevent_req_post(req, ev);
1180 bytes[0] = 4;
1181 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1182 strlen(fname_src)+1, NULL);
1183 if (tevent_req_nomem(bytes, req)) {
1184 return tevent_req_post(req, ev);
1187 bytes = talloc_realloc(state, bytes, uint8_t,
1188 talloc_get_size(bytes)+1);
1189 if (tevent_req_nomem(bytes, req)) {
1190 return tevent_req_post(req, ev);
1193 bytes[talloc_get_size(bytes)-1] = 4;
1194 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1195 strlen(fname_dst)+1, NULL);
1196 if (tevent_req_nomem(bytes, req)) {
1197 return tevent_req_post(req, ev);
1200 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1201 4, state->vwv, talloc_get_size(bytes), bytes);
1202 if (tevent_req_nomem(subreq, req)) {
1203 return tevent_req_post(req, ev);
1205 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1206 return req;
1209 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1211 struct tevent_req *req = tevent_req_callback_data(
1212 subreq, struct tevent_req);
1213 NTSTATUS status;
1215 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1216 TALLOC_FREE(subreq);
1217 if (tevent_req_nterror(req, status)) {
1218 return;
1220 tevent_req_done(req);
1223 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1225 return tevent_req_simple_recv_ntstatus(req);
1228 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1229 struct tevent_context *ev,
1230 struct cli_state *cli,
1231 const char *fname_src,
1232 const char *fname_dst)
1234 return cli_ntrename_internal_send(mem_ctx,
1236 cli,
1237 fname_src,
1238 fname_dst,
1239 RENAME_FLAG_RENAME);
1242 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1244 return cli_ntrename_internal_recv(req);
1247 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1249 TALLOC_CTX *frame = talloc_stackframe();
1250 struct tevent_context *ev;
1251 struct tevent_req *req;
1252 NTSTATUS status = NT_STATUS_OK;
1254 if (smbXcli_conn_has_async_calls(cli->conn)) {
1256 * Can't use sync call while an async call is in flight
1258 status = NT_STATUS_INVALID_PARAMETER;
1259 goto fail;
1262 ev = samba_tevent_context_init(frame);
1263 if (ev == NULL) {
1264 status = NT_STATUS_NO_MEMORY;
1265 goto fail;
1268 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1269 if (req == NULL) {
1270 status = NT_STATUS_NO_MEMORY;
1271 goto fail;
1274 if (!tevent_req_poll(req, ev)) {
1275 status = map_nt_error_from_unix(errno);
1276 goto fail;
1279 status = cli_ntrename_recv(req);
1281 fail:
1282 TALLOC_FREE(frame);
1283 return status;
1286 /****************************************************************************
1287 NT hardlink a file.
1288 ****************************************************************************/
1290 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1291 struct tevent_context *ev,
1292 struct cli_state *cli,
1293 const char *fname_src,
1294 const char *fname_dst)
1296 return cli_ntrename_internal_send(mem_ctx,
1298 cli,
1299 fname_src,
1300 fname_dst,
1301 RENAME_FLAG_HARD_LINK);
1304 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1306 return cli_ntrename_internal_recv(req);
1309 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1311 TALLOC_CTX *frame = talloc_stackframe();
1312 struct tevent_context *ev;
1313 struct tevent_req *req;
1314 NTSTATUS status = NT_STATUS_OK;
1316 if (smbXcli_conn_has_async_calls(cli->conn)) {
1318 * Can't use sync call while an async call is in flight
1320 status = NT_STATUS_INVALID_PARAMETER;
1321 goto fail;
1324 ev = samba_tevent_context_init(frame);
1325 if (ev == NULL) {
1326 status = NT_STATUS_NO_MEMORY;
1327 goto fail;
1330 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1331 if (req == NULL) {
1332 status = NT_STATUS_NO_MEMORY;
1333 goto fail;
1336 if (!tevent_req_poll(req, ev)) {
1337 status = map_nt_error_from_unix(errno);
1338 goto fail;
1341 status = cli_nt_hardlink_recv(req);
1343 fail:
1344 TALLOC_FREE(frame);
1345 return status;
1348 /****************************************************************************
1349 Delete a file.
1350 ****************************************************************************/
1352 static void cli_unlink_done(struct tevent_req *subreq);
1354 struct cli_unlink_state {
1355 uint16_t vwv[1];
1358 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1359 struct tevent_context *ev,
1360 struct cli_state *cli,
1361 const char *fname,
1362 uint16_t mayhave_attrs)
1364 struct tevent_req *req = NULL, *subreq = NULL;
1365 struct cli_unlink_state *state = NULL;
1366 uint8_t additional_flags = 0;
1367 uint8_t *bytes = NULL;
1369 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1370 if (req == NULL) {
1371 return NULL;
1374 SSVAL(state->vwv+0, 0, mayhave_attrs);
1376 bytes = talloc_array(state, uint8_t, 1);
1377 if (tevent_req_nomem(bytes, req)) {
1378 return tevent_req_post(req, ev);
1380 bytes[0] = 4;
1381 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1382 strlen(fname)+1, NULL);
1384 if (tevent_req_nomem(bytes, req)) {
1385 return tevent_req_post(req, ev);
1388 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1389 1, state->vwv, talloc_get_size(bytes), bytes);
1390 if (tevent_req_nomem(subreq, req)) {
1391 return tevent_req_post(req, ev);
1393 tevent_req_set_callback(subreq, cli_unlink_done, req);
1394 return req;
1397 static void cli_unlink_done(struct tevent_req *subreq)
1399 struct tevent_req *req = tevent_req_callback_data(
1400 subreq, struct tevent_req);
1401 NTSTATUS status;
1403 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1404 TALLOC_FREE(subreq);
1405 if (tevent_req_nterror(req, status)) {
1406 return;
1408 tevent_req_done(req);
1411 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1413 return tevent_req_simple_recv_ntstatus(req);
1416 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1418 TALLOC_CTX *frame = NULL;
1419 struct tevent_context *ev;
1420 struct tevent_req *req;
1421 NTSTATUS status = NT_STATUS_OK;
1423 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1424 return cli_smb2_unlink(cli, fname);
1427 frame = talloc_stackframe();
1429 if (smbXcli_conn_has_async_calls(cli->conn)) {
1431 * Can't use sync call while an async call is in flight
1433 status = NT_STATUS_INVALID_PARAMETER;
1434 goto fail;
1437 ev = samba_tevent_context_init(frame);
1438 if (ev == NULL) {
1439 status = NT_STATUS_NO_MEMORY;
1440 goto fail;
1443 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1444 if (req == NULL) {
1445 status = NT_STATUS_NO_MEMORY;
1446 goto fail;
1449 if (!tevent_req_poll(req, ev)) {
1450 status = map_nt_error_from_unix(errno);
1451 goto fail;
1454 status = cli_unlink_recv(req);
1456 fail:
1457 TALLOC_FREE(frame);
1458 return status;
1461 /****************************************************************************
1462 Create a directory.
1463 ****************************************************************************/
1465 static void cli_mkdir_done(struct tevent_req *subreq);
1467 struct cli_mkdir_state {
1468 int dummy;
1471 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1472 struct tevent_context *ev,
1473 struct cli_state *cli,
1474 const char *dname)
1476 struct tevent_req *req = NULL, *subreq = NULL;
1477 struct cli_mkdir_state *state = NULL;
1478 uint8_t additional_flags = 0;
1479 uint8_t *bytes = NULL;
1481 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1482 if (req == NULL) {
1483 return NULL;
1486 bytes = talloc_array(state, uint8_t, 1);
1487 if (tevent_req_nomem(bytes, req)) {
1488 return tevent_req_post(req, ev);
1490 bytes[0] = 4;
1491 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1492 strlen(dname)+1, NULL);
1494 if (tevent_req_nomem(bytes, req)) {
1495 return tevent_req_post(req, ev);
1498 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1499 0, NULL, talloc_get_size(bytes), bytes);
1500 if (tevent_req_nomem(subreq, req)) {
1501 return tevent_req_post(req, ev);
1503 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1504 return req;
1507 static void cli_mkdir_done(struct tevent_req *subreq)
1509 struct tevent_req *req = tevent_req_callback_data(
1510 subreq, struct tevent_req);
1511 NTSTATUS status;
1513 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1514 TALLOC_FREE(subreq);
1515 if (tevent_req_nterror(req, status)) {
1516 return;
1518 tevent_req_done(req);
1521 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1523 return tevent_req_simple_recv_ntstatus(req);
1526 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1528 TALLOC_CTX *frame = NULL;
1529 struct tevent_context *ev;
1530 struct tevent_req *req;
1531 NTSTATUS status = NT_STATUS_OK;
1533 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1534 return cli_smb2_mkdir(cli, dname);
1537 frame = talloc_stackframe();
1539 if (smbXcli_conn_has_async_calls(cli->conn)) {
1541 * Can't use sync call while an async call is in flight
1543 status = NT_STATUS_INVALID_PARAMETER;
1544 goto fail;
1547 ev = samba_tevent_context_init(frame);
1548 if (ev == NULL) {
1549 status = NT_STATUS_NO_MEMORY;
1550 goto fail;
1553 req = cli_mkdir_send(frame, ev, cli, dname);
1554 if (req == NULL) {
1555 status = NT_STATUS_NO_MEMORY;
1556 goto fail;
1559 if (!tevent_req_poll(req, ev)) {
1560 status = map_nt_error_from_unix(errno);
1561 goto fail;
1564 status = cli_mkdir_recv(req);
1566 fail:
1567 TALLOC_FREE(frame);
1568 return status;
1571 /****************************************************************************
1572 Remove a directory.
1573 ****************************************************************************/
1575 static void cli_rmdir_done(struct tevent_req *subreq);
1577 struct cli_rmdir_state {
1578 int dummy;
1581 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1582 struct tevent_context *ev,
1583 struct cli_state *cli,
1584 const char *dname)
1586 struct tevent_req *req = NULL, *subreq = NULL;
1587 struct cli_rmdir_state *state = NULL;
1588 uint8_t additional_flags = 0;
1589 uint8_t *bytes = NULL;
1591 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1592 if (req == NULL) {
1593 return NULL;
1596 bytes = talloc_array(state, uint8_t, 1);
1597 if (tevent_req_nomem(bytes, req)) {
1598 return tevent_req_post(req, ev);
1600 bytes[0] = 4;
1601 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1602 strlen(dname)+1, NULL);
1604 if (tevent_req_nomem(bytes, req)) {
1605 return tevent_req_post(req, ev);
1608 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1609 0, NULL, talloc_get_size(bytes), bytes);
1610 if (tevent_req_nomem(subreq, req)) {
1611 return tevent_req_post(req, ev);
1613 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1614 return req;
1617 static void cli_rmdir_done(struct tevent_req *subreq)
1619 struct tevent_req *req = tevent_req_callback_data(
1620 subreq, struct tevent_req);
1621 NTSTATUS status;
1623 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1624 TALLOC_FREE(subreq);
1625 if (tevent_req_nterror(req, status)) {
1626 return;
1628 tevent_req_done(req);
1631 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1633 return tevent_req_simple_recv_ntstatus(req);
1636 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1638 TALLOC_CTX *frame = NULL;
1639 struct tevent_context *ev;
1640 struct tevent_req *req;
1641 NTSTATUS status = NT_STATUS_OK;
1643 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1644 return cli_smb2_rmdir(cli, dname);
1647 frame = talloc_stackframe();
1649 if (smbXcli_conn_has_async_calls(cli->conn)) {
1651 * Can't use sync call while an async call is in flight
1653 status = NT_STATUS_INVALID_PARAMETER;
1654 goto fail;
1657 ev = samba_tevent_context_init(frame);
1658 if (ev == NULL) {
1659 status = NT_STATUS_NO_MEMORY;
1660 goto fail;
1663 req = cli_rmdir_send(frame, ev, cli, dname);
1664 if (req == NULL) {
1665 status = NT_STATUS_NO_MEMORY;
1666 goto fail;
1669 if (!tevent_req_poll(req, ev)) {
1670 status = map_nt_error_from_unix(errno);
1671 goto fail;
1674 status = cli_rmdir_recv(req);
1676 fail:
1677 TALLOC_FREE(frame);
1678 return status;
1681 /****************************************************************************
1682 Set or clear the delete on close flag.
1683 ****************************************************************************/
1685 struct doc_state {
1686 uint16_t setup;
1687 uint8_t param[6];
1688 uint8_t data[1];
1691 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1693 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1694 NULL, 0, NULL, NULL, 0, NULL);
1695 tevent_req_simple_finish_ntstatus(subreq, status);
1698 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1699 struct tevent_context *ev,
1700 struct cli_state *cli,
1701 uint16_t fnum,
1702 bool flag)
1704 struct tevent_req *req = NULL, *subreq = NULL;
1705 struct doc_state *state = NULL;
1707 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1708 if (req == NULL) {
1709 return NULL;
1712 /* Setup setup word. */
1713 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1715 /* Setup param array. */
1716 SSVAL(state->param,0,fnum);
1717 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1719 /* Setup data array. */
1720 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1722 subreq = cli_trans_send(state, /* mem ctx. */
1723 ev, /* event ctx. */
1724 cli, /* cli_state. */
1725 SMBtrans2, /* cmd. */
1726 NULL, /* pipe name. */
1727 -1, /* fid. */
1728 0, /* function. */
1729 0, /* flags. */
1730 &state->setup, /* setup. */
1731 1, /* num setup uint16_t words. */
1732 0, /* max returned setup. */
1733 state->param, /* param. */
1734 6, /* num param. */
1735 2, /* max returned param. */
1736 state->data, /* data. */
1737 1, /* num data. */
1738 0); /* max returned data. */
1740 if (tevent_req_nomem(subreq, req)) {
1741 return tevent_req_post(req, ev);
1743 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1744 return req;
1747 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1749 return tevent_req_simple_recv_ntstatus(req);
1752 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1754 TALLOC_CTX *frame = talloc_stackframe();
1755 struct tevent_context *ev = NULL;
1756 struct tevent_req *req = NULL;
1757 NTSTATUS status = NT_STATUS_OK;
1759 if (smbXcli_conn_has_async_calls(cli->conn)) {
1761 * Can't use sync call while an async call is in flight
1763 status = NT_STATUS_INVALID_PARAMETER;
1764 goto fail;
1767 ev = samba_tevent_context_init(frame);
1768 if (ev == NULL) {
1769 status = NT_STATUS_NO_MEMORY;
1770 goto fail;
1773 req = cli_nt_delete_on_close_send(frame,
1775 cli,
1776 fnum,
1777 flag);
1778 if (req == NULL) {
1779 status = NT_STATUS_NO_MEMORY;
1780 goto fail;
1783 if (!tevent_req_poll(req, ev)) {
1784 status = map_nt_error_from_unix(errno);
1785 goto fail;
1788 status = cli_nt_delete_on_close_recv(req);
1790 fail:
1791 TALLOC_FREE(frame);
1792 return status;
1795 struct cli_ntcreate1_state {
1796 uint16_t vwv[24];
1797 uint16_t fnum;
1798 struct smb_create_returns cr;
1801 static void cli_ntcreate1_done(struct tevent_req *subreq);
1803 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
1804 struct tevent_context *ev,
1805 struct cli_state *cli,
1806 const char *fname,
1807 uint32_t CreatFlags,
1808 uint32_t DesiredAccess,
1809 uint32_t FileAttributes,
1810 uint32_t ShareAccess,
1811 uint32_t CreateDisposition,
1812 uint32_t CreateOptions,
1813 uint8_t SecurityFlags)
1815 struct tevent_req *req, *subreq;
1816 struct cli_ntcreate1_state *state;
1817 uint16_t *vwv;
1818 uint8_t *bytes;
1819 size_t converted_len;
1821 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1822 if (req == NULL) {
1823 return NULL;
1826 vwv = state->vwv;
1828 SCVAL(vwv+0, 0, 0xFF);
1829 SCVAL(vwv+0, 1, 0);
1830 SSVAL(vwv+1, 0, 0);
1831 SCVAL(vwv+2, 0, 0);
1833 if (cli->use_oplocks) {
1834 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1836 SIVAL(vwv+3, 1, CreatFlags);
1837 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1838 SIVAL(vwv+7, 1, DesiredAccess);
1839 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1840 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1841 SIVAL(vwv+13, 1, FileAttributes);
1842 SIVAL(vwv+15, 1, ShareAccess);
1843 SIVAL(vwv+17, 1, CreateDisposition);
1844 SIVAL(vwv+19, 1, CreateOptions |
1845 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1846 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1847 SCVAL(vwv+23, 1, SecurityFlags);
1849 bytes = talloc_array(state, uint8_t, 0);
1850 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1851 fname, strlen(fname)+1,
1852 &converted_len);
1854 /* sigh. this copes with broken netapp filer behaviour */
1855 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1857 if (tevent_req_nomem(bytes, req)) {
1858 return tevent_req_post(req, ev);
1861 SSVAL(vwv+2, 1, converted_len);
1863 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1864 talloc_get_size(bytes), bytes);
1865 if (tevent_req_nomem(subreq, req)) {
1866 return tevent_req_post(req, ev);
1868 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
1869 return req;
1872 static void cli_ntcreate1_done(struct tevent_req *subreq)
1874 struct tevent_req *req = tevent_req_callback_data(
1875 subreq, struct tevent_req);
1876 struct cli_ntcreate1_state *state = tevent_req_data(
1877 req, struct cli_ntcreate1_state);
1878 uint8_t wct;
1879 uint16_t *vwv;
1880 uint32_t num_bytes;
1881 uint8_t *bytes;
1882 NTSTATUS status;
1884 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
1885 &num_bytes, &bytes);
1886 TALLOC_FREE(subreq);
1887 if (tevent_req_nterror(req, status)) {
1888 return;
1890 state->cr.oplock_level = CVAL(vwv+2, 0);
1891 state->fnum = SVAL(vwv+2, 1);
1892 state->cr.create_action = IVAL(vwv+3, 1);
1893 state->cr.creation_time = BVAL(vwv+5, 1);
1894 state->cr.last_access_time = BVAL(vwv+9, 1);
1895 state->cr.last_write_time = BVAL(vwv+13, 1);
1896 state->cr.change_time = BVAL(vwv+17, 1);
1897 state->cr.file_attributes = IVAL(vwv+21, 1);
1898 state->cr.allocation_size = BVAL(vwv+23, 1);
1899 state->cr.end_of_file = BVAL(vwv+27, 1);
1901 tevent_req_done(req);
1904 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
1905 uint16_t *pfnum,
1906 struct smb_create_returns *cr)
1908 struct cli_ntcreate1_state *state = tevent_req_data(
1909 req, struct cli_ntcreate1_state);
1910 NTSTATUS status;
1912 if (tevent_req_is_nterror(req, &status)) {
1913 return status;
1915 *pfnum = state->fnum;
1916 if (cr != NULL) {
1917 *cr = state->cr;
1919 return NT_STATUS_OK;
1922 struct cli_ntcreate_state {
1923 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
1924 struct smb_create_returns *cr);
1925 struct smb_create_returns cr;
1926 uint16_t fnum;
1929 static void cli_ntcreate_done(struct tevent_req *subreq);
1931 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1932 struct tevent_context *ev,
1933 struct cli_state *cli,
1934 const char *fname,
1935 uint32_t create_flags,
1936 uint32_t desired_access,
1937 uint32_t file_attributes,
1938 uint32_t share_access,
1939 uint32_t create_disposition,
1940 uint32_t create_options,
1941 uint8_t security_flags)
1943 struct tevent_req *req, *subreq;
1944 struct cli_ntcreate_state *state;
1946 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1947 if (req == NULL) {
1948 return NULL;
1951 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1952 state->recv = cli_smb2_create_fnum_recv;
1953 subreq = cli_smb2_create_fnum_send(
1954 state, ev, cli, fname, create_flags, desired_access,
1955 file_attributes, share_access, create_disposition,
1956 create_options);
1957 } else {
1958 state->recv = cli_ntcreate1_recv;
1959 subreq = cli_ntcreate1_send(
1960 state, ev, cli, fname, create_flags, desired_access,
1961 file_attributes, share_access, create_disposition,
1962 create_options, security_flags);
1964 if (tevent_req_nomem(subreq, req)) {
1965 return tevent_req_post(req, ev);
1967 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1968 return req;
1971 static void cli_ntcreate_done(struct tevent_req *subreq)
1973 struct tevent_req *req = tevent_req_callback_data(
1974 subreq, struct tevent_req);
1975 struct cli_ntcreate_state *state = tevent_req_data(
1976 req, struct cli_ntcreate_state);
1977 NTSTATUS status;
1979 status = state->recv(subreq, &state->fnum, &state->cr);
1980 TALLOC_FREE(subreq);
1981 if (tevent_req_nterror(req, status)) {
1982 return;
1984 tevent_req_done(req);
1987 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
1988 struct smb_create_returns *cr)
1990 struct cli_ntcreate_state *state = tevent_req_data(
1991 req, struct cli_ntcreate_state);
1992 NTSTATUS status;
1994 if (tevent_req_is_nterror(req, &status)) {
1995 return status;
1997 if (fnum != NULL) {
1998 *fnum = state->fnum;
2000 if (cr != NULL) {
2001 *cr = state->cr;
2003 return NT_STATUS_OK;
2006 NTSTATUS cli_ntcreate(struct cli_state *cli,
2007 const char *fname,
2008 uint32_t CreatFlags,
2009 uint32_t DesiredAccess,
2010 uint32_t FileAttributes,
2011 uint32_t ShareAccess,
2012 uint32_t CreateDisposition,
2013 uint32_t CreateOptions,
2014 uint8_t SecurityFlags,
2015 uint16_t *pfid,
2016 struct smb_create_returns *cr)
2018 TALLOC_CTX *frame = NULL;
2019 struct tevent_context *ev;
2020 struct tevent_req *req;
2021 NTSTATUS status = NT_STATUS_OK;
2023 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2024 return cli_smb2_create_fnum(cli,
2025 fname,
2026 CreatFlags,
2027 DesiredAccess,
2028 FileAttributes,
2029 ShareAccess,
2030 CreateDisposition,
2031 CreateOptions,
2032 pfid,
2033 cr);
2036 frame = talloc_stackframe();
2038 if (smbXcli_conn_has_async_calls(cli->conn)) {
2040 * Can't use sync call while an async call is in flight
2042 status = NT_STATUS_INVALID_PARAMETER;
2043 goto fail;
2046 ev = samba_tevent_context_init(frame);
2047 if (ev == NULL) {
2048 status = NT_STATUS_NO_MEMORY;
2049 goto fail;
2052 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2053 DesiredAccess, FileAttributes, ShareAccess,
2054 CreateDisposition, CreateOptions,
2055 SecurityFlags);
2056 if (req == NULL) {
2057 status = NT_STATUS_NO_MEMORY;
2058 goto fail;
2061 if (!tevent_req_poll(req, ev)) {
2062 status = map_nt_error_from_unix(errno);
2063 goto fail;
2066 status = cli_ntcreate_recv(req, pfid, cr);
2067 fail:
2068 TALLOC_FREE(frame);
2069 return status;
2072 struct cli_nttrans_create_state {
2073 uint16_t fnum;
2074 struct smb_create_returns cr;
2077 static void cli_nttrans_create_done(struct tevent_req *subreq);
2079 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2080 struct tevent_context *ev,
2081 struct cli_state *cli,
2082 const char *fname,
2083 uint32_t CreatFlags,
2084 uint32_t DesiredAccess,
2085 uint32_t FileAttributes,
2086 uint32_t ShareAccess,
2087 uint32_t CreateDisposition,
2088 uint32_t CreateOptions,
2089 uint8_t SecurityFlags,
2090 struct security_descriptor *secdesc,
2091 struct ea_struct *eas,
2092 int num_eas)
2094 struct tevent_req *req, *subreq;
2095 struct cli_nttrans_create_state *state;
2096 uint8_t *param;
2097 uint8_t *secdesc_buf;
2098 size_t secdesc_len;
2099 NTSTATUS status;
2100 size_t converted_len;
2102 req = tevent_req_create(mem_ctx,
2103 &state, struct cli_nttrans_create_state);
2104 if (req == NULL) {
2105 return NULL;
2108 if (secdesc != NULL) {
2109 status = marshall_sec_desc(talloc_tos(), secdesc,
2110 &secdesc_buf, &secdesc_len);
2111 if (tevent_req_nterror(req, status)) {
2112 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2113 nt_errstr(status)));
2114 return tevent_req_post(req, ev);
2116 } else {
2117 secdesc_buf = NULL;
2118 secdesc_len = 0;
2121 if (num_eas != 0) {
2123 * TODO ;-)
2125 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2126 return tevent_req_post(req, ev);
2129 param = talloc_array(state, uint8_t, 53);
2130 if (tevent_req_nomem(param, req)) {
2131 return tevent_req_post(req, ev);
2134 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2135 fname, strlen(fname),
2136 &converted_len);
2137 if (tevent_req_nomem(param, req)) {
2138 return tevent_req_post(req, ev);
2141 SIVAL(param, 0, CreatFlags);
2142 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2143 SIVAL(param, 8, DesiredAccess);
2144 SIVAL(param, 12, 0x0); /* AllocationSize */
2145 SIVAL(param, 16, 0x0); /* AllocationSize */
2146 SIVAL(param, 20, FileAttributes);
2147 SIVAL(param, 24, ShareAccess);
2148 SIVAL(param, 28, CreateDisposition);
2149 SIVAL(param, 32, CreateOptions |
2150 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2151 SIVAL(param, 36, secdesc_len);
2152 SIVAL(param, 40, 0); /* EA length*/
2153 SIVAL(param, 44, converted_len);
2154 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2155 SCVAL(param, 52, SecurityFlags);
2157 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2158 NULL, -1, /* name, fid */
2159 NT_TRANSACT_CREATE, 0,
2160 NULL, 0, 0, /* setup */
2161 param, talloc_get_size(param), 128, /* param */
2162 secdesc_buf, secdesc_len, 0); /* data */
2163 if (tevent_req_nomem(subreq, req)) {
2164 return tevent_req_post(req, ev);
2166 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2167 return req;
2170 static void cli_nttrans_create_done(struct tevent_req *subreq)
2172 struct tevent_req *req = tevent_req_callback_data(
2173 subreq, struct tevent_req);
2174 struct cli_nttrans_create_state *state = tevent_req_data(
2175 req, struct cli_nttrans_create_state);
2176 uint8_t *param;
2177 uint32_t num_param;
2178 NTSTATUS status;
2180 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2181 NULL, 0, NULL, /* rsetup */
2182 &param, 69, &num_param,
2183 NULL, 0, NULL);
2184 if (tevent_req_nterror(req, status)) {
2185 return;
2187 state->cr.oplock_level = CVAL(param, 0);
2188 state->fnum = SVAL(param, 2);
2189 state->cr.create_action = IVAL(param, 4);
2190 state->cr.creation_time = BVAL(param, 12);
2191 state->cr.last_access_time = BVAL(param, 20);
2192 state->cr.last_write_time = BVAL(param, 28);
2193 state->cr.change_time = BVAL(param, 36);
2194 state->cr.file_attributes = IVAL(param, 44);
2195 state->cr.allocation_size = BVAL(param, 48);
2196 state->cr.end_of_file = BVAL(param, 56);
2198 TALLOC_FREE(param);
2199 tevent_req_done(req);
2202 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2203 uint16_t *fnum,
2204 struct smb_create_returns *cr)
2206 struct cli_nttrans_create_state *state = tevent_req_data(
2207 req, struct cli_nttrans_create_state);
2208 NTSTATUS status;
2210 if (tevent_req_is_nterror(req, &status)) {
2211 return status;
2213 *fnum = state->fnum;
2214 if (cr != NULL) {
2215 *cr = state->cr;
2217 return NT_STATUS_OK;
2220 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2221 const char *fname,
2222 uint32_t CreatFlags,
2223 uint32_t DesiredAccess,
2224 uint32_t FileAttributes,
2225 uint32_t ShareAccess,
2226 uint32_t CreateDisposition,
2227 uint32_t CreateOptions,
2228 uint8_t SecurityFlags,
2229 struct security_descriptor *secdesc,
2230 struct ea_struct *eas,
2231 int num_eas,
2232 uint16_t *pfid,
2233 struct smb_create_returns *cr)
2235 TALLOC_CTX *frame = talloc_stackframe();
2236 struct tevent_context *ev;
2237 struct tevent_req *req;
2238 NTSTATUS status = NT_STATUS_NO_MEMORY;
2240 if (smbXcli_conn_has_async_calls(cli->conn)) {
2242 * Can't use sync call while an async call is in flight
2244 status = NT_STATUS_INVALID_PARAMETER;
2245 goto fail;
2247 ev = samba_tevent_context_init(frame);
2248 if (ev == NULL) {
2249 goto fail;
2251 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2252 DesiredAccess, FileAttributes,
2253 ShareAccess, CreateDisposition,
2254 CreateOptions, SecurityFlags,
2255 secdesc, eas, num_eas);
2256 if (req == NULL) {
2257 goto fail;
2259 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2260 goto fail;
2262 status = cli_nttrans_create_recv(req, pfid, cr);
2263 fail:
2264 TALLOC_FREE(frame);
2265 return status;
2268 /****************************************************************************
2269 Open a file
2270 WARNING: if you open with O_WRONLY then getattrE won't work!
2271 ****************************************************************************/
2273 struct cli_openx_state {
2274 const char *fname;
2275 uint16_t vwv[15];
2276 uint16_t fnum;
2277 struct iovec bytes;
2280 static void cli_openx_done(struct tevent_req *subreq);
2282 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2283 struct tevent_context *ev,
2284 struct cli_state *cli, const char *fname,
2285 int flags, int share_mode,
2286 struct tevent_req **psmbreq)
2288 struct tevent_req *req, *subreq;
2289 struct cli_openx_state *state;
2290 unsigned openfn;
2291 unsigned accessmode;
2292 uint8_t additional_flags;
2293 uint8_t *bytes;
2295 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2296 if (req == NULL) {
2297 return NULL;
2300 openfn = 0;
2301 if (flags & O_CREAT) {
2302 openfn |= (1<<4);
2304 if (!(flags & O_EXCL)) {
2305 if (flags & O_TRUNC)
2306 openfn |= (1<<1);
2307 else
2308 openfn |= (1<<0);
2311 accessmode = (share_mode<<4);
2313 if ((flags & O_ACCMODE) == O_RDWR) {
2314 accessmode |= 2;
2315 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2316 accessmode |= 1;
2319 #if defined(O_SYNC)
2320 if ((flags & O_SYNC) == O_SYNC) {
2321 accessmode |= (1<<14);
2323 #endif /* O_SYNC */
2325 if (share_mode == DENY_FCB) {
2326 accessmode = 0xFF;
2329 SCVAL(state->vwv + 0, 0, 0xFF);
2330 SCVAL(state->vwv + 0, 1, 0);
2331 SSVAL(state->vwv + 1, 0, 0);
2332 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2333 SSVAL(state->vwv + 3, 0, accessmode);
2334 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2335 SSVAL(state->vwv + 5, 0, 0);
2336 SIVAL(state->vwv + 6, 0, 0);
2337 SSVAL(state->vwv + 8, 0, openfn);
2338 SIVAL(state->vwv + 9, 0, 0);
2339 SIVAL(state->vwv + 11, 0, 0);
2340 SIVAL(state->vwv + 13, 0, 0);
2342 additional_flags = 0;
2344 if (cli->use_oplocks) {
2345 /* if using oplocks then ask for a batch oplock via
2346 core and extended methods */
2347 additional_flags =
2348 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2349 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2352 bytes = talloc_array(state, uint8_t, 0);
2353 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2354 strlen(fname)+1, NULL);
2356 if (tevent_req_nomem(bytes, req)) {
2357 return tevent_req_post(req, ev);
2360 state->bytes.iov_base = (void *)bytes;
2361 state->bytes.iov_len = talloc_get_size(bytes);
2363 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2364 15, state->vwv, 1, &state->bytes);
2365 if (subreq == NULL) {
2366 TALLOC_FREE(req);
2367 return NULL;
2369 tevent_req_set_callback(subreq, cli_openx_done, req);
2370 *psmbreq = subreq;
2371 return req;
2374 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2375 struct cli_state *cli, const char *fname,
2376 int flags, int share_mode)
2378 struct tevent_req *req, *subreq;
2379 NTSTATUS status;
2381 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2382 &subreq);
2383 if (req == NULL) {
2384 return NULL;
2387 status = smb1cli_req_chain_submit(&subreq, 1);
2388 if (tevent_req_nterror(req, status)) {
2389 return tevent_req_post(req, ev);
2391 return req;
2394 static void cli_openx_done(struct tevent_req *subreq)
2396 struct tevent_req *req = tevent_req_callback_data(
2397 subreq, struct tevent_req);
2398 struct cli_openx_state *state = tevent_req_data(
2399 req, struct cli_openx_state);
2400 uint8_t wct;
2401 uint16_t *vwv;
2402 NTSTATUS status;
2404 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2405 NULL);
2406 TALLOC_FREE(subreq);
2407 if (tevent_req_nterror(req, status)) {
2408 return;
2410 state->fnum = SVAL(vwv+2, 0);
2411 tevent_req_done(req);
2414 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2416 struct cli_openx_state *state = tevent_req_data(
2417 req, struct cli_openx_state);
2418 NTSTATUS status;
2420 if (tevent_req_is_nterror(req, &status)) {
2421 return status;
2423 *pfnum = state->fnum;
2424 return NT_STATUS_OK;
2427 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2428 int share_mode, uint16_t *pfnum)
2430 TALLOC_CTX *frame = talloc_stackframe();
2431 struct tevent_context *ev;
2432 struct tevent_req *req;
2433 NTSTATUS status = NT_STATUS_NO_MEMORY;
2435 if (smbXcli_conn_has_async_calls(cli->conn)) {
2437 * Can't use sync call while an async call is in flight
2439 status = NT_STATUS_INVALID_PARAMETER;
2440 goto fail;
2443 ev = samba_tevent_context_init(frame);
2444 if (ev == NULL) {
2445 goto fail;
2448 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2449 if (req == NULL) {
2450 goto fail;
2453 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2454 goto fail;
2457 status = cli_openx_recv(req, pfnum);
2458 fail:
2459 TALLOC_FREE(frame);
2460 return status;
2462 /****************************************************************************
2463 Synchronous wrapper function that does an NtCreateX open by preference
2464 and falls back to openX if this fails.
2465 ****************************************************************************/
2467 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2468 int share_mode_in, uint16_t *pfnum)
2470 NTSTATUS status;
2471 unsigned int openfn = 0;
2472 unsigned int dos_deny = 0;
2473 uint32_t access_mask, share_mode, create_disposition, create_options;
2474 struct smb_create_returns cr;
2476 /* Do the initial mapping into OpenX parameters. */
2477 if (flags & O_CREAT) {
2478 openfn |= (1<<4);
2480 if (!(flags & O_EXCL)) {
2481 if (flags & O_TRUNC)
2482 openfn |= (1<<1);
2483 else
2484 openfn |= (1<<0);
2487 dos_deny = (share_mode_in<<4);
2489 if ((flags & O_ACCMODE) == O_RDWR) {
2490 dos_deny |= 2;
2491 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2492 dos_deny |= 1;
2495 #if defined(O_SYNC)
2496 if ((flags & O_SYNC) == O_SYNC) {
2497 dos_deny |= (1<<14);
2499 #endif /* O_SYNC */
2501 if (share_mode_in == DENY_FCB) {
2502 dos_deny = 0xFF;
2505 #if 0
2506 /* Hmmm. This is what I think the above code
2507 should look like if it's using the constants
2508 we #define. JRA. */
2510 if (flags & O_CREAT) {
2511 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2513 if (!(flags & O_EXCL)) {
2514 if (flags & O_TRUNC)
2515 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2516 else
2517 openfn |= OPENX_FILE_EXISTS_OPEN;
2520 dos_deny = SET_DENY_MODE(share_mode_in);
2522 if ((flags & O_ACCMODE) == O_RDWR) {
2523 dos_deny |= DOS_OPEN_RDWR;
2524 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2525 dos_deny |= DOS_OPEN_WRONLY;
2528 #if defined(O_SYNC)
2529 if ((flags & O_SYNC) == O_SYNC) {
2530 dos_deny |= FILE_SYNC_OPENMODE;
2532 #endif /* O_SYNC */
2534 if (share_mode_in == DENY_FCB) {
2535 dos_deny = 0xFF;
2537 #endif
2539 if (!map_open_params_to_ntcreate(fname, dos_deny,
2540 openfn, &access_mask,
2541 &share_mode, &create_disposition,
2542 &create_options, NULL)) {
2543 goto try_openx;
2546 status = cli_ntcreate(cli,
2547 fname,
2549 access_mask,
2551 share_mode,
2552 create_disposition,
2553 create_options,
2555 pfnum,
2556 &cr);
2558 /* Try and cope will all varients of "we don't do this call"
2559 and fall back to openX. */
2561 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2562 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2563 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2564 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2565 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2566 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2567 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2568 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2569 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2570 goto try_openx;
2573 if (NT_STATUS_IS_OK(status) &&
2574 (create_options & FILE_NON_DIRECTORY_FILE) &&
2575 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2578 * Some (broken) servers return a valid handle
2579 * for directories even if FILE_NON_DIRECTORY_FILE
2580 * is set. Just close the handle and set the
2581 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2583 status = cli_close(cli, *pfnum);
2584 if (!NT_STATUS_IS_OK(status)) {
2585 return status;
2587 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2588 /* Set this so libsmbclient can retrieve it. */
2589 cli->raw_status = status;
2592 return status;
2594 try_openx:
2596 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2599 /****************************************************************************
2600 Close a file.
2601 ****************************************************************************/
2603 struct cli_close_state {
2604 uint16_t vwv[3];
2607 static void cli_close_done(struct tevent_req *subreq);
2609 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2610 struct tevent_context *ev,
2611 struct cli_state *cli,
2612 uint16_t fnum,
2613 struct tevent_req **psubreq)
2615 struct tevent_req *req, *subreq;
2616 struct cli_close_state *state;
2618 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2619 if (req == NULL) {
2620 return NULL;
2623 SSVAL(state->vwv+0, 0, fnum);
2624 SIVALS(state->vwv+1, 0, -1);
2626 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2627 0, NULL);
2628 if (subreq == NULL) {
2629 TALLOC_FREE(req);
2630 return NULL;
2632 tevent_req_set_callback(subreq, cli_close_done, req);
2633 *psubreq = subreq;
2634 return req;
2637 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2638 struct tevent_context *ev,
2639 struct cli_state *cli,
2640 uint16_t fnum)
2642 struct tevent_req *req, *subreq;
2643 NTSTATUS status;
2645 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2646 if (req == NULL) {
2647 return NULL;
2650 status = smb1cli_req_chain_submit(&subreq, 1);
2651 if (tevent_req_nterror(req, status)) {
2652 return tevent_req_post(req, ev);
2654 return req;
2657 static void cli_close_done(struct tevent_req *subreq)
2659 struct tevent_req *req = tevent_req_callback_data(
2660 subreq, struct tevent_req);
2661 NTSTATUS status;
2663 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2664 TALLOC_FREE(subreq);
2665 if (tevent_req_nterror(req, status)) {
2666 return;
2668 tevent_req_done(req);
2671 NTSTATUS cli_close_recv(struct tevent_req *req)
2673 return tevent_req_simple_recv_ntstatus(req);
2676 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2678 TALLOC_CTX *frame = NULL;
2679 struct tevent_context *ev;
2680 struct tevent_req *req;
2681 NTSTATUS status = NT_STATUS_OK;
2683 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2684 return cli_smb2_close_fnum(cli, fnum);
2687 frame = talloc_stackframe();
2689 if (smbXcli_conn_has_async_calls(cli->conn)) {
2691 * Can't use sync call while an async call is in flight
2693 status = NT_STATUS_INVALID_PARAMETER;
2694 goto fail;
2697 ev = samba_tevent_context_init(frame);
2698 if (ev == NULL) {
2699 status = NT_STATUS_NO_MEMORY;
2700 goto fail;
2703 req = cli_close_send(frame, ev, cli, fnum);
2704 if (req == NULL) {
2705 status = NT_STATUS_NO_MEMORY;
2706 goto fail;
2709 if (!tevent_req_poll(req, ev)) {
2710 status = map_nt_error_from_unix(errno);
2711 goto fail;
2714 status = cli_close_recv(req);
2715 fail:
2716 TALLOC_FREE(frame);
2717 return status;
2720 /****************************************************************************
2721 Truncate a file to a specified size
2722 ****************************************************************************/
2724 struct ftrunc_state {
2725 uint16_t setup;
2726 uint8_t param[6];
2727 uint8_t data[8];
2730 static void cli_ftruncate_done(struct tevent_req *subreq)
2732 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2733 NULL, 0, NULL, NULL, 0, NULL);
2734 tevent_req_simple_finish_ntstatus(subreq, status);
2737 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2738 struct tevent_context *ev,
2739 struct cli_state *cli,
2740 uint16_t fnum,
2741 uint64_t size)
2743 struct tevent_req *req = NULL, *subreq = NULL;
2744 struct ftrunc_state *state = NULL;
2746 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2747 if (req == NULL) {
2748 return NULL;
2751 /* Setup setup word. */
2752 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2754 /* Setup param array. */
2755 SSVAL(state->param,0,fnum);
2756 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2757 SSVAL(state->param,4,0);
2759 /* Setup data array. */
2760 SBVAL(state->data, 0, size);
2762 subreq = cli_trans_send(state, /* mem ctx. */
2763 ev, /* event ctx. */
2764 cli, /* cli_state. */
2765 SMBtrans2, /* cmd. */
2766 NULL, /* pipe name. */
2767 -1, /* fid. */
2768 0, /* function. */
2769 0, /* flags. */
2770 &state->setup, /* setup. */
2771 1, /* num setup uint16_t words. */
2772 0, /* max returned setup. */
2773 state->param, /* param. */
2774 6, /* num param. */
2775 2, /* max returned param. */
2776 state->data, /* data. */
2777 8, /* num data. */
2778 0); /* max returned data. */
2780 if (tevent_req_nomem(subreq, req)) {
2781 return tevent_req_post(req, ev);
2783 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2784 return req;
2787 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2789 return tevent_req_simple_recv_ntstatus(req);
2792 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2794 TALLOC_CTX *frame = talloc_stackframe();
2795 struct tevent_context *ev = NULL;
2796 struct tevent_req *req = NULL;
2797 NTSTATUS status = NT_STATUS_OK;
2799 if (smbXcli_conn_has_async_calls(cli->conn)) {
2801 * Can't use sync call while an async call is in flight
2803 status = NT_STATUS_INVALID_PARAMETER;
2804 goto fail;
2807 ev = samba_tevent_context_init(frame);
2808 if (ev == NULL) {
2809 status = NT_STATUS_NO_MEMORY;
2810 goto fail;
2813 req = cli_ftruncate_send(frame,
2815 cli,
2816 fnum,
2817 size);
2818 if (req == NULL) {
2819 status = NT_STATUS_NO_MEMORY;
2820 goto fail;
2823 if (!tevent_req_poll(req, ev)) {
2824 status = map_nt_error_from_unix(errno);
2825 goto fail;
2828 status = cli_ftruncate_recv(req);
2830 fail:
2831 TALLOC_FREE(frame);
2832 return status;
2835 /****************************************************************************
2836 send a lock with a specified locktype
2837 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2838 ****************************************************************************/
2840 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2841 uint32_t offset, uint32_t len,
2842 int timeout, unsigned char locktype)
2844 uint16_t vwv[8];
2845 uint8_t bytes[10];
2846 NTSTATUS status;
2847 unsigned int set_timeout = 0;
2848 unsigned int saved_timeout = 0;
2850 SCVAL(vwv + 0, 0, 0xff);
2851 SCVAL(vwv + 0, 1, 0);
2852 SSVAL(vwv + 1, 0, 0);
2853 SSVAL(vwv + 2, 0, fnum);
2854 SCVAL(vwv + 3, 0, locktype);
2855 SCVAL(vwv + 3, 1, 0);
2856 SIVALS(vwv + 4, 0, timeout);
2857 SSVAL(vwv + 6, 0, 0);
2858 SSVAL(vwv + 7, 0, 1);
2860 SSVAL(bytes, 0, cli_getpid(cli));
2861 SIVAL(bytes, 2, offset);
2862 SIVAL(bytes, 6, len);
2864 if (timeout != 0) {
2865 if (timeout == -1) {
2866 set_timeout = 0x7FFFFFFF;
2867 } else {
2868 set_timeout = timeout + 2*1000;
2870 saved_timeout = cli_set_timeout(cli, set_timeout);
2873 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2874 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2876 if (saved_timeout != 0) {
2877 cli_set_timeout(cli, saved_timeout);
2880 return status;
2883 /****************************************************************************
2884 Lock a file.
2885 note that timeout is in units of 2 milliseconds
2886 ****************************************************************************/
2888 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2889 uint32_t offset, uint32_t len, int timeout,
2890 enum brl_type lock_type)
2892 NTSTATUS status;
2894 status = cli_locktype(cli, fnum, offset, len, timeout,
2895 (lock_type == READ_LOCK? 1 : 0));
2896 return status;
2899 /****************************************************************************
2900 Unlock a file.
2901 ****************************************************************************/
2903 struct cli_unlock_state {
2904 uint16_t vwv[8];
2905 uint8_t data[10];
2908 static void cli_unlock_done(struct tevent_req *subreq);
2910 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2911 struct tevent_context *ev,
2912 struct cli_state *cli,
2913 uint16_t fnum,
2914 uint64_t offset,
2915 uint64_t len)
2918 struct tevent_req *req = NULL, *subreq = NULL;
2919 struct cli_unlock_state *state = NULL;
2920 uint8_t additional_flags = 0;
2922 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2923 if (req == NULL) {
2924 return NULL;
2927 SCVAL(state->vwv+0, 0, 0xFF);
2928 SSVAL(state->vwv+2, 0, fnum);
2929 SCVAL(state->vwv+3, 0, 0);
2930 SIVALS(state->vwv+4, 0, 0);
2931 SSVAL(state->vwv+6, 0, 1);
2932 SSVAL(state->vwv+7, 0, 0);
2934 SSVAL(state->data, 0, cli_getpid(cli));
2935 SIVAL(state->data, 2, offset);
2936 SIVAL(state->data, 6, len);
2938 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2939 8, state->vwv, 10, state->data);
2940 if (tevent_req_nomem(subreq, req)) {
2941 return tevent_req_post(req, ev);
2943 tevent_req_set_callback(subreq, cli_unlock_done, req);
2944 return req;
2947 static void cli_unlock_done(struct tevent_req *subreq)
2949 struct tevent_req *req = tevent_req_callback_data(
2950 subreq, struct tevent_req);
2951 NTSTATUS status;
2953 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2954 TALLOC_FREE(subreq);
2955 if (tevent_req_nterror(req, status)) {
2956 return;
2958 tevent_req_done(req);
2961 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2963 return tevent_req_simple_recv_ntstatus(req);
2966 NTSTATUS cli_unlock(struct cli_state *cli,
2967 uint16_t fnum,
2968 uint32_t offset,
2969 uint32_t len)
2971 TALLOC_CTX *frame = talloc_stackframe();
2972 struct tevent_context *ev;
2973 struct tevent_req *req;
2974 NTSTATUS status = NT_STATUS_OK;
2976 if (smbXcli_conn_has_async_calls(cli->conn)) {
2978 * Can't use sync call while an async call is in flight
2980 status = NT_STATUS_INVALID_PARAMETER;
2981 goto fail;
2984 ev = samba_tevent_context_init(frame);
2985 if (ev == NULL) {
2986 status = NT_STATUS_NO_MEMORY;
2987 goto fail;
2990 req = cli_unlock_send(frame, ev, cli,
2991 fnum, offset, len);
2992 if (req == NULL) {
2993 status = NT_STATUS_NO_MEMORY;
2994 goto fail;
2997 if (!tevent_req_poll(req, ev)) {
2998 status = map_nt_error_from_unix(errno);
2999 goto fail;
3002 status = cli_unlock_recv(req);
3004 fail:
3005 TALLOC_FREE(frame);
3006 return status;
3009 /****************************************************************************
3010 Lock a file with 64 bit offsets.
3011 ****************************************************************************/
3013 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3014 uint64_t offset, uint64_t len, int timeout,
3015 enum brl_type lock_type)
3017 uint16_t vwv[8];
3018 uint8_t bytes[20];
3019 unsigned int set_timeout = 0;
3020 unsigned int saved_timeout = 0;
3021 int ltype;
3022 NTSTATUS status;
3024 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3025 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3028 ltype = (lock_type == READ_LOCK? 1 : 0);
3029 ltype |= LOCKING_ANDX_LARGE_FILES;
3031 SCVAL(vwv + 0, 0, 0xff);
3032 SCVAL(vwv + 0, 1, 0);
3033 SSVAL(vwv + 1, 0, 0);
3034 SSVAL(vwv + 2, 0, fnum);
3035 SCVAL(vwv + 3, 0, ltype);
3036 SCVAL(vwv + 3, 1, 0);
3037 SIVALS(vwv + 4, 0, timeout);
3038 SSVAL(vwv + 6, 0, 0);
3039 SSVAL(vwv + 7, 0, 1);
3041 SIVAL(bytes, 0, cli_getpid(cli));
3042 SOFF_T_R(bytes, 4, offset);
3043 SOFF_T_R(bytes, 12, len);
3045 if (timeout != 0) {
3046 if (timeout == -1) {
3047 set_timeout = 0x7FFFFFFF;
3048 } else {
3049 set_timeout = timeout + 2*1000;
3051 saved_timeout = cli_set_timeout(cli, set_timeout);
3054 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3055 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3057 if (saved_timeout != 0) {
3058 cli_set_timeout(cli, saved_timeout);
3061 return status;
3064 /****************************************************************************
3065 Unlock a file with 64 bit offsets.
3066 ****************************************************************************/
3068 struct cli_unlock64_state {
3069 uint16_t vwv[8];
3070 uint8_t data[20];
3073 static void cli_unlock64_done(struct tevent_req *subreq);
3075 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3076 struct tevent_context *ev,
3077 struct cli_state *cli,
3078 uint16_t fnum,
3079 uint64_t offset,
3080 uint64_t len)
3083 struct tevent_req *req = NULL, *subreq = NULL;
3084 struct cli_unlock64_state *state = NULL;
3085 uint8_t additional_flags = 0;
3087 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3088 if (req == NULL) {
3089 return NULL;
3092 SCVAL(state->vwv+0, 0, 0xff);
3093 SSVAL(state->vwv+2, 0, fnum);
3094 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3095 SIVALS(state->vwv+4, 0, 0);
3096 SSVAL(state->vwv+6, 0, 1);
3097 SSVAL(state->vwv+7, 0, 0);
3099 SIVAL(state->data, 0, cli_getpid(cli));
3100 SOFF_T_R(state->data, 4, offset);
3101 SOFF_T_R(state->data, 12, len);
3103 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
3104 8, state->vwv, 20, state->data);
3105 if (tevent_req_nomem(subreq, req)) {
3106 return tevent_req_post(req, ev);
3108 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3109 return req;
3112 static void cli_unlock64_done(struct tevent_req *subreq)
3114 struct tevent_req *req = tevent_req_callback_data(
3115 subreq, struct tevent_req);
3116 NTSTATUS status;
3118 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3119 TALLOC_FREE(subreq);
3120 if (tevent_req_nterror(req, status)) {
3121 return;
3123 tevent_req_done(req);
3126 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3128 return tevent_req_simple_recv_ntstatus(req);
3131 NTSTATUS cli_unlock64(struct cli_state *cli,
3132 uint16_t fnum,
3133 uint64_t offset,
3134 uint64_t len)
3136 TALLOC_CTX *frame = talloc_stackframe();
3137 struct tevent_context *ev;
3138 struct tevent_req *req;
3139 NTSTATUS status = NT_STATUS_OK;
3141 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3142 return cli_unlock(cli, fnum, offset, len);
3145 if (smbXcli_conn_has_async_calls(cli->conn)) {
3147 * Can't use sync call while an async call is in flight
3149 status = NT_STATUS_INVALID_PARAMETER;
3150 goto fail;
3153 ev = samba_tevent_context_init(frame);
3154 if (ev == NULL) {
3155 status = NT_STATUS_NO_MEMORY;
3156 goto fail;
3159 req = cli_unlock64_send(frame, ev, cli,
3160 fnum, offset, len);
3161 if (req == NULL) {
3162 status = NT_STATUS_NO_MEMORY;
3163 goto fail;
3166 if (!tevent_req_poll(req, ev)) {
3167 status = map_nt_error_from_unix(errno);
3168 goto fail;
3171 status = cli_unlock64_recv(req);
3173 fail:
3174 TALLOC_FREE(frame);
3175 return status;
3178 /****************************************************************************
3179 Get/unlock a POSIX lock on a file - internal function.
3180 ****************************************************************************/
3182 struct posix_lock_state {
3183 uint16_t setup;
3184 uint8_t param[4];
3185 uint8_t data[POSIX_LOCK_DATA_SIZE];
3188 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3190 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3191 NULL, 0, NULL, NULL, 0, NULL);
3192 tevent_req_simple_finish_ntstatus(subreq, status);
3195 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3196 struct tevent_context *ev,
3197 struct cli_state *cli,
3198 uint16_t fnum,
3199 uint64_t offset,
3200 uint64_t len,
3201 bool wait_lock,
3202 enum brl_type lock_type)
3204 struct tevent_req *req = NULL, *subreq = NULL;
3205 struct posix_lock_state *state = NULL;
3207 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3208 if (req == NULL) {
3209 return NULL;
3212 /* Setup setup word. */
3213 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3215 /* Setup param array. */
3216 SSVAL(&state->param, 0, fnum);
3217 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3219 /* Setup data array. */
3220 switch (lock_type) {
3221 case READ_LOCK:
3222 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3223 POSIX_LOCK_TYPE_READ);
3224 break;
3225 case WRITE_LOCK:
3226 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3227 POSIX_LOCK_TYPE_WRITE);
3228 break;
3229 case UNLOCK_LOCK:
3230 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3231 POSIX_LOCK_TYPE_UNLOCK);
3232 break;
3233 default:
3234 return NULL;
3237 if (wait_lock) {
3238 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3239 POSIX_LOCK_FLAG_WAIT);
3240 } else {
3241 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3242 POSIX_LOCK_FLAG_NOWAIT);
3245 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3246 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3247 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3249 subreq = cli_trans_send(state, /* mem ctx. */
3250 ev, /* event ctx. */
3251 cli, /* cli_state. */
3252 SMBtrans2, /* cmd. */
3253 NULL, /* pipe name. */
3254 -1, /* fid. */
3255 0, /* function. */
3256 0, /* flags. */
3257 &state->setup, /* setup. */
3258 1, /* num setup uint16_t words. */
3259 0, /* max returned setup. */
3260 state->param, /* param. */
3261 4, /* num param. */
3262 2, /* max returned param. */
3263 state->data, /* data. */
3264 POSIX_LOCK_DATA_SIZE, /* num data. */
3265 0); /* max returned data. */
3267 if (tevent_req_nomem(subreq, req)) {
3268 return tevent_req_post(req, ev);
3270 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3271 return req;
3274 /****************************************************************************
3275 POSIX Lock a file.
3276 ****************************************************************************/
3278 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3279 struct tevent_context *ev,
3280 struct cli_state *cli,
3281 uint16_t fnum,
3282 uint64_t offset,
3283 uint64_t len,
3284 bool wait_lock,
3285 enum brl_type lock_type)
3287 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3288 wait_lock, lock_type);
3291 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3293 return tevent_req_simple_recv_ntstatus(req);
3296 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3297 uint64_t offset, uint64_t len,
3298 bool wait_lock, enum brl_type lock_type)
3300 TALLOC_CTX *frame = talloc_stackframe();
3301 struct tevent_context *ev = NULL;
3302 struct tevent_req *req = NULL;
3303 NTSTATUS status = NT_STATUS_OK;
3305 if (smbXcli_conn_has_async_calls(cli->conn)) {
3307 * Can't use sync call while an async call is in flight
3309 status = NT_STATUS_INVALID_PARAMETER;
3310 goto fail;
3313 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3314 status = NT_STATUS_INVALID_PARAMETER;
3315 goto fail;
3318 ev = samba_tevent_context_init(frame);
3319 if (ev == NULL) {
3320 status = NT_STATUS_NO_MEMORY;
3321 goto fail;
3324 req = cli_posix_lock_send(frame,
3326 cli,
3327 fnum,
3328 offset,
3329 len,
3330 wait_lock,
3331 lock_type);
3332 if (req == NULL) {
3333 status = NT_STATUS_NO_MEMORY;
3334 goto fail;
3337 if (!tevent_req_poll(req, ev)) {
3338 status = map_nt_error_from_unix(errno);
3339 goto fail;
3342 status = cli_posix_lock_recv(req);
3344 fail:
3345 TALLOC_FREE(frame);
3346 return status;
3349 /****************************************************************************
3350 POSIX Unlock a file.
3351 ****************************************************************************/
3353 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3354 struct tevent_context *ev,
3355 struct cli_state *cli,
3356 uint16_t fnum,
3357 uint64_t offset,
3358 uint64_t len)
3360 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3361 false, UNLOCK_LOCK);
3364 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3366 return tevent_req_simple_recv_ntstatus(req);
3369 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3371 TALLOC_CTX *frame = talloc_stackframe();
3372 struct tevent_context *ev = NULL;
3373 struct tevent_req *req = NULL;
3374 NTSTATUS status = NT_STATUS_OK;
3376 if (smbXcli_conn_has_async_calls(cli->conn)) {
3378 * Can't use sync call while an async call is in flight
3380 status = NT_STATUS_INVALID_PARAMETER;
3381 goto fail;
3384 ev = samba_tevent_context_init(frame);
3385 if (ev == NULL) {
3386 status = NT_STATUS_NO_MEMORY;
3387 goto fail;
3390 req = cli_posix_unlock_send(frame,
3392 cli,
3393 fnum,
3394 offset,
3395 len);
3396 if (req == NULL) {
3397 status = NT_STATUS_NO_MEMORY;
3398 goto fail;
3401 if (!tevent_req_poll(req, ev)) {
3402 status = map_nt_error_from_unix(errno);
3403 goto fail;
3406 status = cli_posix_unlock_recv(req);
3408 fail:
3409 TALLOC_FREE(frame);
3410 return status;
3413 /****************************************************************************
3414 Do a SMBgetattrE call.
3415 ****************************************************************************/
3417 static void cli_getattrE_done(struct tevent_req *subreq);
3419 struct cli_getattrE_state {
3420 uint16_t vwv[1];
3421 int zone_offset;
3422 uint16_t attr;
3423 off_t size;
3424 time_t change_time;
3425 time_t access_time;
3426 time_t write_time;
3429 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3430 struct tevent_context *ev,
3431 struct cli_state *cli,
3432 uint16_t fnum)
3434 struct tevent_req *req = NULL, *subreq = NULL;
3435 struct cli_getattrE_state *state = NULL;
3436 uint8_t additional_flags = 0;
3438 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3439 if (req == NULL) {
3440 return NULL;
3443 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3444 SSVAL(state->vwv+0,0,fnum);
3446 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3447 1, state->vwv, 0, NULL);
3448 if (tevent_req_nomem(subreq, req)) {
3449 return tevent_req_post(req, ev);
3451 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3452 return req;
3455 static void cli_getattrE_done(struct tevent_req *subreq)
3457 struct tevent_req *req = tevent_req_callback_data(
3458 subreq, struct tevent_req);
3459 struct cli_getattrE_state *state = tevent_req_data(
3460 req, struct cli_getattrE_state);
3461 uint8_t wct;
3462 uint16_t *vwv = NULL;
3463 NTSTATUS status;
3465 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3466 NULL, NULL);
3467 TALLOC_FREE(subreq);
3468 if (tevent_req_nterror(req, status)) {
3469 return;
3472 state->size = (off_t)IVAL(vwv+6,0);
3473 state->attr = SVAL(vwv+10,0);
3474 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3475 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3476 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3478 tevent_req_done(req);
3481 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3482 uint16_t *attr,
3483 off_t *size,
3484 time_t *change_time,
3485 time_t *access_time,
3486 time_t *write_time)
3488 struct cli_getattrE_state *state = tevent_req_data(
3489 req, struct cli_getattrE_state);
3490 NTSTATUS status;
3492 if (tevent_req_is_nterror(req, &status)) {
3493 return status;
3495 if (attr) {
3496 *attr = state->attr;
3498 if (size) {
3499 *size = state->size;
3501 if (change_time) {
3502 *change_time = state->change_time;
3504 if (access_time) {
3505 *access_time = state->access_time;
3507 if (write_time) {
3508 *write_time = state->write_time;
3510 return NT_STATUS_OK;
3513 NTSTATUS cli_getattrE(struct cli_state *cli,
3514 uint16_t fnum,
3515 uint16_t *attr,
3516 off_t *size,
3517 time_t *change_time,
3518 time_t *access_time,
3519 time_t *write_time)
3521 TALLOC_CTX *frame = NULL;
3522 struct tevent_context *ev = NULL;
3523 struct tevent_req *req = NULL;
3524 NTSTATUS status = NT_STATUS_OK;
3526 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3527 return cli_smb2_getattrE(cli,
3528 fnum,
3529 attr,
3530 size,
3531 change_time,
3532 access_time,
3533 write_time);
3536 frame = talloc_stackframe();
3538 if (smbXcli_conn_has_async_calls(cli->conn)) {
3540 * Can't use sync call while an async call is in flight
3542 status = NT_STATUS_INVALID_PARAMETER;
3543 goto fail;
3546 ev = samba_tevent_context_init(frame);
3547 if (ev == NULL) {
3548 status = NT_STATUS_NO_MEMORY;
3549 goto fail;
3552 req = cli_getattrE_send(frame, ev, cli, fnum);
3553 if (req == NULL) {
3554 status = NT_STATUS_NO_MEMORY;
3555 goto fail;
3558 if (!tevent_req_poll(req, ev)) {
3559 status = map_nt_error_from_unix(errno);
3560 goto fail;
3563 status = cli_getattrE_recv(req,
3564 attr,
3565 size,
3566 change_time,
3567 access_time,
3568 write_time);
3570 fail:
3571 TALLOC_FREE(frame);
3572 return status;
3575 /****************************************************************************
3576 Do a SMBgetatr call
3577 ****************************************************************************/
3579 static void cli_getatr_done(struct tevent_req *subreq);
3581 struct cli_getatr_state {
3582 int zone_offset;
3583 uint16_t attr;
3584 off_t size;
3585 time_t write_time;
3588 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3589 struct tevent_context *ev,
3590 struct cli_state *cli,
3591 const char *fname)
3593 struct tevent_req *req = NULL, *subreq = NULL;
3594 struct cli_getatr_state *state = NULL;
3595 uint8_t additional_flags = 0;
3596 uint8_t *bytes = NULL;
3598 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3599 if (req == NULL) {
3600 return NULL;
3603 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3605 bytes = talloc_array(state, uint8_t, 1);
3606 if (tevent_req_nomem(bytes, req)) {
3607 return tevent_req_post(req, ev);
3609 bytes[0] = 4;
3610 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3611 strlen(fname)+1, NULL);
3613 if (tevent_req_nomem(bytes, req)) {
3614 return tevent_req_post(req, ev);
3617 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3618 0, NULL, talloc_get_size(bytes), bytes);
3619 if (tevent_req_nomem(subreq, req)) {
3620 return tevent_req_post(req, ev);
3622 tevent_req_set_callback(subreq, cli_getatr_done, req);
3623 return req;
3626 static void cli_getatr_done(struct tevent_req *subreq)
3628 struct tevent_req *req = tevent_req_callback_data(
3629 subreq, struct tevent_req);
3630 struct cli_getatr_state *state = tevent_req_data(
3631 req, struct cli_getatr_state);
3632 uint8_t wct;
3633 uint16_t *vwv = NULL;
3634 NTSTATUS status;
3636 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3637 NULL);
3638 TALLOC_FREE(subreq);
3639 if (tevent_req_nterror(req, status)) {
3640 return;
3643 state->attr = SVAL(vwv+0,0);
3644 state->size = (off_t)IVAL(vwv+3,0);
3645 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3647 tevent_req_done(req);
3650 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3651 uint16_t *attr,
3652 off_t *size,
3653 time_t *write_time)
3655 struct cli_getatr_state *state = tevent_req_data(
3656 req, struct cli_getatr_state);
3657 NTSTATUS status;
3659 if (tevent_req_is_nterror(req, &status)) {
3660 return status;
3662 if (attr) {
3663 *attr = state->attr;
3665 if (size) {
3666 *size = state->size;
3668 if (write_time) {
3669 *write_time = state->write_time;
3671 return NT_STATUS_OK;
3674 NTSTATUS cli_getatr(struct cli_state *cli,
3675 const char *fname,
3676 uint16_t *attr,
3677 off_t *size,
3678 time_t *write_time)
3680 TALLOC_CTX *frame = NULL;
3681 struct tevent_context *ev = NULL;
3682 struct tevent_req *req = NULL;
3683 NTSTATUS status = NT_STATUS_OK;
3685 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3686 return cli_smb2_getatr(cli,
3687 fname,
3688 attr,
3689 size,
3690 write_time);
3693 frame = talloc_stackframe();
3695 if (smbXcli_conn_has_async_calls(cli->conn)) {
3697 * Can't use sync call while an async call is in flight
3699 status = NT_STATUS_INVALID_PARAMETER;
3700 goto fail;
3703 ev = samba_tevent_context_init(frame);
3704 if (ev == NULL) {
3705 status = NT_STATUS_NO_MEMORY;
3706 goto fail;
3709 req = cli_getatr_send(frame, ev, cli, fname);
3710 if (req == NULL) {
3711 status = NT_STATUS_NO_MEMORY;
3712 goto fail;
3715 if (!tevent_req_poll(req, ev)) {
3716 status = map_nt_error_from_unix(errno);
3717 goto fail;
3720 status = cli_getatr_recv(req,
3721 attr,
3722 size,
3723 write_time);
3725 fail:
3726 TALLOC_FREE(frame);
3727 return status;
3730 /****************************************************************************
3731 Do a SMBsetattrE call.
3732 ****************************************************************************/
3734 static void cli_setattrE_done(struct tevent_req *subreq);
3736 struct cli_setattrE_state {
3737 uint16_t vwv[7];
3740 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3741 struct tevent_context *ev,
3742 struct cli_state *cli,
3743 uint16_t fnum,
3744 time_t change_time,
3745 time_t access_time,
3746 time_t write_time)
3748 struct tevent_req *req = NULL, *subreq = NULL;
3749 struct cli_setattrE_state *state = NULL;
3750 uint8_t additional_flags = 0;
3752 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3753 if (req == NULL) {
3754 return NULL;
3757 SSVAL(state->vwv+0, 0, fnum);
3758 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3759 smb1cli_conn_server_time_zone(cli->conn));
3760 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3761 smb1cli_conn_server_time_zone(cli->conn));
3762 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3763 smb1cli_conn_server_time_zone(cli->conn));
3765 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3766 7, state->vwv, 0, NULL);
3767 if (tevent_req_nomem(subreq, req)) {
3768 return tevent_req_post(req, ev);
3770 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3771 return req;
3774 static void cli_setattrE_done(struct tevent_req *subreq)
3776 struct tevent_req *req = tevent_req_callback_data(
3777 subreq, struct tevent_req);
3778 NTSTATUS status;
3780 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3781 TALLOC_FREE(subreq);
3782 if (tevent_req_nterror(req, status)) {
3783 return;
3785 tevent_req_done(req);
3788 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3790 return tevent_req_simple_recv_ntstatus(req);
3793 NTSTATUS cli_setattrE(struct cli_state *cli,
3794 uint16_t fnum,
3795 time_t change_time,
3796 time_t access_time,
3797 time_t write_time)
3799 TALLOC_CTX *frame = NULL;
3800 struct tevent_context *ev = NULL;
3801 struct tevent_req *req = NULL;
3802 NTSTATUS status = NT_STATUS_OK;
3804 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3805 return cli_smb2_setattrE(cli,
3806 fnum,
3807 change_time,
3808 access_time,
3809 write_time);
3812 frame = talloc_stackframe();
3814 if (smbXcli_conn_has_async_calls(cli->conn)) {
3816 * Can't use sync call while an async call is in flight
3818 status = NT_STATUS_INVALID_PARAMETER;
3819 goto fail;
3822 ev = samba_tevent_context_init(frame);
3823 if (ev == NULL) {
3824 status = NT_STATUS_NO_MEMORY;
3825 goto fail;
3828 req = cli_setattrE_send(frame, ev,
3829 cli,
3830 fnum,
3831 change_time,
3832 access_time,
3833 write_time);
3835 if (req == NULL) {
3836 status = NT_STATUS_NO_MEMORY;
3837 goto fail;
3840 if (!tevent_req_poll(req, ev)) {
3841 status = map_nt_error_from_unix(errno);
3842 goto fail;
3845 status = cli_setattrE_recv(req);
3847 fail:
3848 TALLOC_FREE(frame);
3849 return status;
3852 /****************************************************************************
3853 Do a SMBsetatr call.
3854 ****************************************************************************/
3856 static void cli_setatr_done(struct tevent_req *subreq);
3858 struct cli_setatr_state {
3859 uint16_t vwv[8];
3862 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3863 struct tevent_context *ev,
3864 struct cli_state *cli,
3865 const char *fname,
3866 uint16_t attr,
3867 time_t mtime)
3869 struct tevent_req *req = NULL, *subreq = NULL;
3870 struct cli_setatr_state *state = NULL;
3871 uint8_t additional_flags = 0;
3872 uint8_t *bytes = NULL;
3874 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3875 if (req == NULL) {
3876 return NULL;
3879 SSVAL(state->vwv+0, 0, attr);
3880 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3882 bytes = talloc_array(state, uint8_t, 1);
3883 if (tevent_req_nomem(bytes, req)) {
3884 return tevent_req_post(req, ev);
3886 bytes[0] = 4;
3887 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3888 strlen(fname)+1, NULL);
3889 if (tevent_req_nomem(bytes, req)) {
3890 return tevent_req_post(req, ev);
3892 bytes = talloc_realloc(state, bytes, uint8_t,
3893 talloc_get_size(bytes)+1);
3894 if (tevent_req_nomem(bytes, req)) {
3895 return tevent_req_post(req, ev);
3898 bytes[talloc_get_size(bytes)-1] = 4;
3899 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3900 1, NULL);
3901 if (tevent_req_nomem(bytes, req)) {
3902 return tevent_req_post(req, ev);
3905 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3906 8, state->vwv, talloc_get_size(bytes), bytes);
3907 if (tevent_req_nomem(subreq, req)) {
3908 return tevent_req_post(req, ev);
3910 tevent_req_set_callback(subreq, cli_setatr_done, req);
3911 return req;
3914 static void cli_setatr_done(struct tevent_req *subreq)
3916 struct tevent_req *req = tevent_req_callback_data(
3917 subreq, struct tevent_req);
3918 NTSTATUS status;
3920 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3921 TALLOC_FREE(subreq);
3922 if (tevent_req_nterror(req, status)) {
3923 return;
3925 tevent_req_done(req);
3928 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3930 return tevent_req_simple_recv_ntstatus(req);
3933 NTSTATUS cli_setatr(struct cli_state *cli,
3934 const char *fname,
3935 uint16_t attr,
3936 time_t mtime)
3938 TALLOC_CTX *frame = NULL;
3939 struct tevent_context *ev = NULL;
3940 struct tevent_req *req = NULL;
3941 NTSTATUS status = NT_STATUS_OK;
3943 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3944 return cli_smb2_setatr(cli,
3945 fname,
3946 attr,
3947 mtime);
3950 frame = talloc_stackframe();
3952 if (smbXcli_conn_has_async_calls(cli->conn)) {
3954 * Can't use sync call while an async call is in flight
3956 status = NT_STATUS_INVALID_PARAMETER;
3957 goto fail;
3960 ev = samba_tevent_context_init(frame);
3961 if (ev == NULL) {
3962 status = NT_STATUS_NO_MEMORY;
3963 goto fail;
3966 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3967 if (req == NULL) {
3968 status = NT_STATUS_NO_MEMORY;
3969 goto fail;
3972 if (!tevent_req_poll(req, ev)) {
3973 status = map_nt_error_from_unix(errno);
3974 goto fail;
3977 status = cli_setatr_recv(req);
3979 fail:
3980 TALLOC_FREE(frame);
3981 return status;
3984 /****************************************************************************
3985 Check for existance of a dir.
3986 ****************************************************************************/
3988 static void cli_chkpath_done(struct tevent_req *subreq);
3990 struct cli_chkpath_state {
3991 int dummy;
3994 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3995 struct tevent_context *ev,
3996 struct cli_state *cli,
3997 const char *fname)
3999 struct tevent_req *req = NULL, *subreq = NULL;
4000 struct cli_chkpath_state *state = NULL;
4001 uint8_t additional_flags = 0;
4002 uint8_t *bytes = NULL;
4004 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4005 if (req == NULL) {
4006 return NULL;
4009 bytes = talloc_array(state, uint8_t, 1);
4010 if (tevent_req_nomem(bytes, req)) {
4011 return tevent_req_post(req, ev);
4013 bytes[0] = 4;
4014 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4015 strlen(fname)+1, NULL);
4017 if (tevent_req_nomem(bytes, req)) {
4018 return tevent_req_post(req, ev);
4021 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
4022 0, NULL, talloc_get_size(bytes), bytes);
4023 if (tevent_req_nomem(subreq, req)) {
4024 return tevent_req_post(req, ev);
4026 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4027 return req;
4030 static void cli_chkpath_done(struct tevent_req *subreq)
4032 struct tevent_req *req = tevent_req_callback_data(
4033 subreq, struct tevent_req);
4034 NTSTATUS status;
4036 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4037 TALLOC_FREE(subreq);
4038 if (tevent_req_nterror(req, status)) {
4039 return;
4041 tevent_req_done(req);
4044 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4046 return tevent_req_simple_recv_ntstatus(req);
4049 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4051 TALLOC_CTX *frame = talloc_stackframe();
4052 struct tevent_context *ev = NULL;
4053 struct tevent_req *req = NULL;
4054 char *path2 = NULL;
4055 NTSTATUS status = NT_STATUS_OK;
4057 if (smbXcli_conn_has_async_calls(cli->conn)) {
4059 * Can't use sync call while an async call is in flight
4061 status = NT_STATUS_INVALID_PARAMETER;
4062 goto fail;
4065 path2 = talloc_strdup(frame, path);
4066 if (!path2) {
4067 status = NT_STATUS_NO_MEMORY;
4068 goto fail;
4070 trim_char(path2,'\0','\\');
4071 if (!*path2) {
4072 path2 = talloc_strdup(frame, "\\");
4073 if (!path2) {
4074 status = NT_STATUS_NO_MEMORY;
4075 goto fail;
4079 ev = samba_tevent_context_init(frame);
4080 if (ev == NULL) {
4081 status = NT_STATUS_NO_MEMORY;
4082 goto fail;
4085 req = cli_chkpath_send(frame, ev, cli, path2);
4086 if (req == NULL) {
4087 status = NT_STATUS_NO_MEMORY;
4088 goto fail;
4091 if (!tevent_req_poll(req, ev)) {
4092 status = map_nt_error_from_unix(errno);
4093 goto fail;
4096 status = cli_chkpath_recv(req);
4098 fail:
4099 TALLOC_FREE(frame);
4100 return status;
4103 /****************************************************************************
4104 Query disk space.
4105 ****************************************************************************/
4107 static void cli_dskattr_done(struct tevent_req *subreq);
4109 struct cli_dskattr_state {
4110 int bsize;
4111 int total;
4112 int avail;
4115 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4116 struct tevent_context *ev,
4117 struct cli_state *cli)
4119 struct tevent_req *req = NULL, *subreq = NULL;
4120 struct cli_dskattr_state *state = NULL;
4121 uint8_t additional_flags = 0;
4123 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4124 if (req == NULL) {
4125 return NULL;
4128 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
4129 0, NULL, 0, NULL);
4130 if (tevent_req_nomem(subreq, req)) {
4131 return tevent_req_post(req, ev);
4133 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4134 return req;
4137 static void cli_dskattr_done(struct tevent_req *subreq)
4139 struct tevent_req *req = tevent_req_callback_data(
4140 subreq, struct tevent_req);
4141 struct cli_dskattr_state *state = tevent_req_data(
4142 req, struct cli_dskattr_state);
4143 uint8_t wct;
4144 uint16_t *vwv = NULL;
4145 NTSTATUS status;
4147 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4148 NULL);
4149 TALLOC_FREE(subreq);
4150 if (tevent_req_nterror(req, status)) {
4151 return;
4153 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4154 state->total = SVAL(vwv+0, 0);
4155 state->avail = SVAL(vwv+3, 0);
4156 tevent_req_done(req);
4159 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4161 struct cli_dskattr_state *state = tevent_req_data(
4162 req, struct cli_dskattr_state);
4163 NTSTATUS status;
4165 if (tevent_req_is_nterror(req, &status)) {
4166 return status;
4168 *bsize = state->bsize;
4169 *total = state->total;
4170 *avail = state->avail;
4171 return NT_STATUS_OK;
4174 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4176 TALLOC_CTX *frame = NULL;
4177 struct tevent_context *ev = NULL;
4178 struct tevent_req *req = NULL;
4179 NTSTATUS status = NT_STATUS_OK;
4181 frame = talloc_stackframe();
4183 if (smbXcli_conn_has_async_calls(cli->conn)) {
4185 * Can't use sync call while an async call is in flight
4187 status = NT_STATUS_INVALID_PARAMETER;
4188 goto fail;
4191 ev = samba_tevent_context_init(frame);
4192 if (ev == NULL) {
4193 status = NT_STATUS_NO_MEMORY;
4194 goto fail;
4197 req = cli_dskattr_send(frame, ev, cli);
4198 if (req == NULL) {
4199 status = NT_STATUS_NO_MEMORY;
4200 goto fail;
4203 if (!tevent_req_poll(req, ev)) {
4204 status = map_nt_error_from_unix(errno);
4205 goto fail;
4208 status = cli_dskattr_recv(req, bsize, total, avail);
4210 fail:
4211 TALLOC_FREE(frame);
4212 return status;
4215 NTSTATUS cli_disk_size(struct cli_state *cli, uint64_t *bsize, uint64_t *total, uint64_t *avail)
4217 uint64_t sectors_per_block;
4218 uint64_t bytes_per_sector;
4219 int old_bsize, old_total, old_avail;
4220 NTSTATUS status;
4222 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4223 return cli_smb2_dskattr(cli, bsize, total, avail);
4227 * Try the trans2 disk full size info call first.
4228 * We already use this in SMBC_fstatvfs_ctx().
4229 * Ignore 'actual_available_units' as we only
4230 * care about the quota for the caller.
4233 status = cli_get_fs_full_size_info(cli,
4234 total,
4235 avail,
4236 NULL,
4237 &sectors_per_block,
4238 &bytes_per_sector);
4240 /* Try and cope will all varients of "we don't do this call"
4241 and fall back to cli_dskattr. */
4243 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4244 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4245 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4246 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4247 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4248 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4249 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4250 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4251 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4252 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4253 goto try_dskattr;
4256 if (!NT_STATUS_IS_OK(status)) {
4257 return status;
4260 if (bsize) {
4261 *bsize = sectors_per_block *
4262 bytes_per_sector;
4265 return NT_STATUS_OK;
4267 try_dskattr:
4269 /* Old SMB1 core protocol fallback. */
4270 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4271 if (!NT_STATUS_IS_OK(status)) {
4272 return status;
4274 if (bsize) {
4275 *bsize = (uint64_t)old_bsize;
4277 if (total) {
4278 *total = (uint64_t)old_total;
4280 if (avail) {
4281 *avail = (uint64_t)old_avail;
4283 return NT_STATUS_OK;
4286 /****************************************************************************
4287 Create and open a temporary file.
4288 ****************************************************************************/
4290 static void cli_ctemp_done(struct tevent_req *subreq);
4292 struct ctemp_state {
4293 uint16_t vwv[3];
4294 char *ret_path;
4295 uint16_t fnum;
4298 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4299 struct tevent_context *ev,
4300 struct cli_state *cli,
4301 const char *path)
4303 struct tevent_req *req = NULL, *subreq = NULL;
4304 struct ctemp_state *state = NULL;
4305 uint8_t additional_flags = 0;
4306 uint8_t *bytes = NULL;
4308 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4309 if (req == NULL) {
4310 return NULL;
4313 SSVAL(state->vwv,0,0);
4314 SIVALS(state->vwv+1,0,-1);
4316 bytes = talloc_array(state, uint8_t, 1);
4317 if (tevent_req_nomem(bytes, req)) {
4318 return tevent_req_post(req, ev);
4320 bytes[0] = 4;
4321 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4322 strlen(path)+1, NULL);
4323 if (tevent_req_nomem(bytes, req)) {
4324 return tevent_req_post(req, ev);
4327 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4328 3, state->vwv, talloc_get_size(bytes), bytes);
4329 if (tevent_req_nomem(subreq, req)) {
4330 return tevent_req_post(req, ev);
4332 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4333 return req;
4336 static void cli_ctemp_done(struct tevent_req *subreq)
4338 struct tevent_req *req = tevent_req_callback_data(
4339 subreq, struct tevent_req);
4340 struct ctemp_state *state = tevent_req_data(
4341 req, struct ctemp_state);
4342 NTSTATUS status;
4343 uint8_t wcnt;
4344 uint16_t *vwv;
4345 uint32_t num_bytes = 0;
4346 uint8_t *bytes = NULL;
4348 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4349 &num_bytes, &bytes);
4350 TALLOC_FREE(subreq);
4351 if (tevent_req_nterror(req, status)) {
4352 return;
4355 state->fnum = SVAL(vwv+0, 0);
4357 /* From W2K3, the result is just the ASCII name */
4358 if (num_bytes < 2) {
4359 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4360 return;
4363 if (pull_string_talloc(state,
4364 NULL,
4366 &state->ret_path,
4367 bytes,
4368 num_bytes,
4369 STR_ASCII) == 0) {
4370 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4371 return;
4373 tevent_req_done(req);
4376 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4377 TALLOC_CTX *ctx,
4378 uint16_t *pfnum,
4379 char **outfile)
4381 struct ctemp_state *state = tevent_req_data(req,
4382 struct ctemp_state);
4383 NTSTATUS status;
4385 if (tevent_req_is_nterror(req, &status)) {
4386 return status;
4388 *pfnum = state->fnum;
4389 *outfile = talloc_strdup(ctx, state->ret_path);
4390 if (!*outfile) {
4391 return NT_STATUS_NO_MEMORY;
4393 return NT_STATUS_OK;
4396 NTSTATUS cli_ctemp(struct cli_state *cli,
4397 TALLOC_CTX *ctx,
4398 const char *path,
4399 uint16_t *pfnum,
4400 char **out_path)
4402 TALLOC_CTX *frame = talloc_stackframe();
4403 struct tevent_context *ev;
4404 struct tevent_req *req;
4405 NTSTATUS status = NT_STATUS_OK;
4407 if (smbXcli_conn_has_async_calls(cli->conn)) {
4409 * Can't use sync call while an async call is in flight
4411 status = NT_STATUS_INVALID_PARAMETER;
4412 goto fail;
4415 ev = samba_tevent_context_init(frame);
4416 if (ev == NULL) {
4417 status = NT_STATUS_NO_MEMORY;
4418 goto fail;
4421 req = cli_ctemp_send(frame, ev, cli, path);
4422 if (req == NULL) {
4423 status = NT_STATUS_NO_MEMORY;
4424 goto fail;
4427 if (!tevent_req_poll(req, ev)) {
4428 status = map_nt_error_from_unix(errno);
4429 goto fail;
4432 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4434 fail:
4435 TALLOC_FREE(frame);
4436 return status;
4440 send a raw ioctl - used by the torture code
4442 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4444 uint16_t vwv[3];
4445 NTSTATUS status;
4447 SSVAL(vwv+0, 0, fnum);
4448 SSVAL(vwv+1, 0, code>>16);
4449 SSVAL(vwv+2, 0, (code&0xFFFF));
4451 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4452 NULL, 0, NULL, NULL, NULL, NULL);
4453 if (!NT_STATUS_IS_OK(status)) {
4454 return status;
4456 *blob = data_blob_null;
4457 return NT_STATUS_OK;
4460 /*********************************************************
4461 Set an extended attribute utility fn.
4462 *********************************************************/
4464 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4465 uint8_t *param, unsigned int param_len,
4466 const char *ea_name,
4467 const char *ea_val, size_t ea_len)
4469 uint16_t setup[1];
4470 unsigned int data_len = 0;
4471 uint8_t *data = NULL;
4472 char *p;
4473 size_t ea_namelen = strlen(ea_name);
4474 NTSTATUS status;
4476 SSVAL(setup, 0, setup_val);
4478 if (ea_namelen == 0 && ea_len == 0) {
4479 data_len = 4;
4480 data = talloc_array(talloc_tos(),
4481 uint8_t,
4482 data_len);
4483 if (!data) {
4484 return NT_STATUS_NO_MEMORY;
4486 p = (char *)data;
4487 SIVAL(p,0,data_len);
4488 } else {
4489 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4490 data = talloc_array(talloc_tos(),
4491 uint8_t,
4492 data_len);
4493 if (!data) {
4494 return NT_STATUS_NO_MEMORY;
4496 p = (char *)data;
4497 SIVAL(p,0,data_len);
4498 p += 4;
4499 SCVAL(p, 0, 0); /* EA flags. */
4500 SCVAL(p, 1, ea_namelen);
4501 SSVAL(p, 2, ea_len);
4502 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4503 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4506 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4507 setup, 1, 0,
4508 param, param_len, 2,
4509 data, data_len, CLI_BUFFER_SIZE,
4510 NULL,
4511 NULL, 0, NULL, /* rsetup */
4512 NULL, 0, NULL, /* rparam */
4513 NULL, 0, NULL); /* rdata */
4514 talloc_free(data);
4515 return status;
4518 /*********************************************************
4519 Set an extended attribute on a pathname.
4520 *********************************************************/
4522 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4523 const char *ea_name, const char *ea_val,
4524 size_t ea_len)
4526 unsigned int param_len = 0;
4527 uint8_t *param;
4528 NTSTATUS status;
4529 TALLOC_CTX *frame = NULL;
4531 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4532 return cli_smb2_set_ea_path(cli,
4533 path,
4534 ea_name,
4535 ea_val,
4536 ea_len);
4539 frame = talloc_stackframe();
4541 param = talloc_array(frame, uint8_t, 6);
4542 if (!param) {
4543 status = NT_STATUS_NO_MEMORY;
4544 goto fail;
4546 SSVAL(param,0,SMB_INFO_SET_EA);
4547 SSVAL(param,2,0);
4548 SSVAL(param,4,0);
4550 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4551 path, strlen(path)+1,
4552 NULL);
4553 param_len = talloc_get_size(param);
4555 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4556 ea_name, ea_val, ea_len);
4558 fail:
4560 TALLOC_FREE(frame);
4561 return status;
4564 /*********************************************************
4565 Set an extended attribute on an fnum.
4566 *********************************************************/
4568 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4569 const char *ea_name, const char *ea_val,
4570 size_t ea_len)
4572 uint8_t param[6];
4574 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4575 return cli_smb2_set_ea_fnum(cli,
4576 fnum,
4577 ea_name,
4578 ea_val,
4579 ea_len);
4582 memset(param, 0, 6);
4583 SSVAL(param,0,fnum);
4584 SSVAL(param,2,SMB_INFO_SET_EA);
4586 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4587 ea_name, ea_val, ea_len);
4590 /*********************************************************
4591 Get an extended attribute list utility fn.
4592 *********************************************************/
4594 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4595 size_t rdata_len,
4596 size_t *pnum_eas, struct ea_struct **pea_list)
4598 struct ea_struct *ea_list = NULL;
4599 size_t num_eas;
4600 size_t ea_size;
4601 const uint8_t *p;
4603 if (rdata_len < 4) {
4604 return false;
4607 ea_size = (size_t)IVAL(rdata,0);
4608 if (ea_size > rdata_len) {
4609 return false;
4612 if (ea_size == 0) {
4613 /* No EA's present. */
4614 *pnum_eas = 0;
4615 *pea_list = NULL;
4616 return true;
4619 p = rdata + 4;
4620 ea_size -= 4;
4622 /* Validate the EA list and count it. */
4623 for (num_eas = 0; ea_size >= 4; num_eas++) {
4624 unsigned int ea_namelen = CVAL(p,1);
4625 unsigned int ea_valuelen = SVAL(p,2);
4626 if (ea_namelen == 0) {
4627 return false;
4629 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4630 return false;
4632 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4633 p += 4 + ea_namelen + 1 + ea_valuelen;
4636 if (num_eas == 0) {
4637 *pnum_eas = 0;
4638 *pea_list = NULL;
4639 return true;
4642 *pnum_eas = num_eas;
4643 if (!pea_list) {
4644 /* Caller only wants number of EA's. */
4645 return true;
4648 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4649 if (!ea_list) {
4650 return false;
4653 ea_size = (size_t)IVAL(rdata,0);
4654 p = rdata + 4;
4656 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4657 struct ea_struct *ea = &ea_list[num_eas];
4658 fstring unix_ea_name;
4659 unsigned int ea_namelen = CVAL(p,1);
4660 unsigned int ea_valuelen = SVAL(p,2);
4662 ea->flags = CVAL(p,0);
4663 unix_ea_name[0] = '\0';
4664 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4665 ea->name = talloc_strdup(ea_list, unix_ea_name);
4666 if (!ea->name) {
4667 goto fail;
4669 /* Ensure the value is null terminated (in case it's a string). */
4670 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4671 if (!ea->value.data) {
4672 goto fail;
4674 if (ea_valuelen) {
4675 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4677 ea->value.data[ea_valuelen] = 0;
4678 ea->value.length--;
4679 p += 4 + ea_namelen + 1 + ea_valuelen;
4682 *pea_list = ea_list;
4683 return true;
4685 fail:
4686 TALLOC_FREE(ea_list);
4687 return false;
4690 /*********************************************************
4691 Get an extended attribute list from a pathname.
4692 *********************************************************/
4694 struct cli_get_ea_list_path_state {
4695 uint32_t num_data;
4696 uint8_t *data;
4699 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4701 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4702 struct tevent_context *ev,
4703 struct cli_state *cli,
4704 const char *fname)
4706 struct tevent_req *req, *subreq;
4707 struct cli_get_ea_list_path_state *state;
4709 req = tevent_req_create(mem_ctx, &state,
4710 struct cli_get_ea_list_path_state);
4711 if (req == NULL) {
4712 return NULL;
4714 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4715 SMB_INFO_QUERY_ALL_EAS, 4,
4716 CLI_BUFFER_SIZE);
4717 if (tevent_req_nomem(subreq, req)) {
4718 return tevent_req_post(req, ev);
4720 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4721 return req;
4724 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4726 struct tevent_req *req = tevent_req_callback_data(
4727 subreq, struct tevent_req);
4728 struct cli_get_ea_list_path_state *state = tevent_req_data(
4729 req, struct cli_get_ea_list_path_state);
4730 NTSTATUS status;
4732 status = cli_qpathinfo_recv(subreq, state, &state->data,
4733 &state->num_data);
4734 TALLOC_FREE(subreq);
4735 if (tevent_req_nterror(req, status)) {
4736 return;
4738 tevent_req_done(req);
4741 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4742 size_t *pnum_eas, struct ea_struct **peas)
4744 struct cli_get_ea_list_path_state *state = tevent_req_data(
4745 req, struct cli_get_ea_list_path_state);
4746 NTSTATUS status;
4748 if (tevent_req_is_nterror(req, &status)) {
4749 return status;
4751 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4752 pnum_eas, peas)) {
4753 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4755 return NT_STATUS_OK;
4758 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4759 TALLOC_CTX *ctx,
4760 size_t *pnum_eas,
4761 struct ea_struct **pea_list)
4763 TALLOC_CTX *frame = NULL;
4764 struct tevent_context *ev = NULL;
4765 struct tevent_req *req = NULL;
4766 NTSTATUS status = NT_STATUS_NO_MEMORY;
4768 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4769 return cli_smb2_get_ea_list_path(cli,
4770 path,
4771 ctx,
4772 pnum_eas,
4773 pea_list);
4776 frame = talloc_stackframe();
4778 if (smbXcli_conn_has_async_calls(cli->conn)) {
4780 * Can't use sync call while an async call is in flight
4782 status = NT_STATUS_INVALID_PARAMETER;
4783 goto fail;
4785 ev = samba_tevent_context_init(frame);
4786 if (ev == NULL) {
4787 goto fail;
4789 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4790 if (req == NULL) {
4791 goto fail;
4793 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4794 goto fail;
4796 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4797 fail:
4798 TALLOC_FREE(frame);
4799 return status;
4802 /****************************************************************************
4803 Convert open "flags" arg to uint32_t on wire.
4804 ****************************************************************************/
4806 static uint32_t open_flags_to_wire(int flags)
4808 int open_mode = flags & O_ACCMODE;
4809 uint32_t ret = 0;
4811 switch (open_mode) {
4812 case O_WRONLY:
4813 ret |= SMB_O_WRONLY;
4814 break;
4815 case O_RDWR:
4816 ret |= SMB_O_RDWR;
4817 break;
4818 default:
4819 case O_RDONLY:
4820 ret |= SMB_O_RDONLY;
4821 break;
4824 if (flags & O_CREAT) {
4825 ret |= SMB_O_CREAT;
4827 if (flags & O_EXCL) {
4828 ret |= SMB_O_EXCL;
4830 if (flags & O_TRUNC) {
4831 ret |= SMB_O_TRUNC;
4833 #if defined(O_SYNC)
4834 if (flags & O_SYNC) {
4835 ret |= SMB_O_SYNC;
4837 #endif /* O_SYNC */
4838 if (flags & O_APPEND) {
4839 ret |= SMB_O_APPEND;
4841 #if defined(O_DIRECT)
4842 if (flags & O_DIRECT) {
4843 ret |= SMB_O_DIRECT;
4845 #endif
4846 #if defined(O_DIRECTORY)
4847 if (flags & O_DIRECTORY) {
4848 ret |= SMB_O_DIRECTORY;
4850 #endif
4851 return ret;
4854 /****************************************************************************
4855 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4856 ****************************************************************************/
4858 struct posix_open_state {
4859 uint16_t setup;
4860 uint8_t *param;
4861 uint8_t data[18];
4862 uint16_t fnum; /* Out */
4865 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4867 struct tevent_req *req = tevent_req_callback_data(
4868 subreq, struct tevent_req);
4869 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4870 NTSTATUS status;
4871 uint8_t *data;
4872 uint32_t num_data;
4874 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4875 NULL, 0, NULL, &data, 12, &num_data);
4876 TALLOC_FREE(subreq);
4877 if (tevent_req_nterror(req, status)) {
4878 return;
4880 state->fnum = SVAL(data,2);
4881 tevent_req_done(req);
4884 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4885 struct tevent_context *ev,
4886 struct cli_state *cli,
4887 const char *fname,
4888 int flags,
4889 mode_t mode,
4890 bool is_dir)
4892 struct tevent_req *req = NULL, *subreq = NULL;
4893 struct posix_open_state *state = NULL;
4894 uint32_t wire_flags = open_flags_to_wire(flags);
4896 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4897 if (req == NULL) {
4898 return NULL;
4901 /* Setup setup word. */
4902 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4904 /* Setup param array. */
4905 state->param = talloc_array(state, uint8_t, 6);
4906 if (tevent_req_nomem(state->param, req)) {
4907 return tevent_req_post(req, ev);
4909 memset(state->param, '\0', 6);
4910 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4912 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4913 strlen(fname)+1, NULL);
4915 if (tevent_req_nomem(state->param, req)) {
4916 return tevent_req_post(req, ev);
4919 /* Setup data words. */
4920 if (is_dir) {
4921 wire_flags |= SMB_O_DIRECTORY;
4924 SIVAL(state->data,0,0); /* No oplock. */
4925 SIVAL(state->data,4,wire_flags);
4926 SIVAL(state->data,8,unix_perms_to_wire(mode));
4927 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4928 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4930 subreq = cli_trans_send(state, /* mem ctx. */
4931 ev, /* event ctx. */
4932 cli, /* cli_state. */
4933 SMBtrans2, /* cmd. */
4934 NULL, /* pipe name. */
4935 -1, /* fid. */
4936 0, /* function. */
4937 0, /* flags. */
4938 &state->setup, /* setup. */
4939 1, /* num setup uint16_t words. */
4940 0, /* max returned setup. */
4941 state->param, /* param. */
4942 talloc_get_size(state->param),/* num param. */
4943 2, /* max returned param. */
4944 state->data, /* data. */
4945 18, /* num data. */
4946 12); /* max returned data. */
4948 if (tevent_req_nomem(subreq, req)) {
4949 return tevent_req_post(req, ev);
4951 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4952 return req;
4955 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4956 struct tevent_context *ev,
4957 struct cli_state *cli,
4958 const char *fname,
4959 int flags,
4960 mode_t mode)
4962 return cli_posix_open_internal_send(mem_ctx, ev,
4963 cli, fname, flags, mode, false);
4966 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4968 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4969 NTSTATUS status;
4971 if (tevent_req_is_nterror(req, &status)) {
4972 return status;
4974 *pfnum = state->fnum;
4975 return NT_STATUS_OK;
4978 /****************************************************************************
4979 Open - POSIX semantics. Doesn't request oplock.
4980 ****************************************************************************/
4982 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4983 int flags, mode_t mode, uint16_t *pfnum)
4986 TALLOC_CTX *frame = talloc_stackframe();
4987 struct tevent_context *ev = NULL;
4988 struct tevent_req *req = NULL;
4989 NTSTATUS status = NT_STATUS_OK;
4991 if (smbXcli_conn_has_async_calls(cli->conn)) {
4993 * Can't use sync call while an async call is in flight
4995 status = NT_STATUS_INVALID_PARAMETER;
4996 goto fail;
4999 ev = samba_tevent_context_init(frame);
5000 if (ev == NULL) {
5001 status = NT_STATUS_NO_MEMORY;
5002 goto fail;
5005 req = cli_posix_open_send(frame,
5007 cli,
5008 fname,
5009 flags,
5010 mode);
5011 if (req == NULL) {
5012 status = NT_STATUS_NO_MEMORY;
5013 goto fail;
5016 if (!tevent_req_poll(req, ev)) {
5017 status = map_nt_error_from_unix(errno);
5018 goto fail;
5021 status = cli_posix_open_recv(req, pfnum);
5023 fail:
5024 TALLOC_FREE(frame);
5025 return status;
5028 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5029 struct tevent_context *ev,
5030 struct cli_state *cli,
5031 const char *fname,
5032 mode_t mode)
5034 return cli_posix_open_internal_send(mem_ctx, ev,
5035 cli, fname, O_CREAT, mode, true);
5038 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5040 return tevent_req_simple_recv_ntstatus(req);
5043 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5045 TALLOC_CTX *frame = talloc_stackframe();
5046 struct tevent_context *ev = NULL;
5047 struct tevent_req *req = NULL;
5048 NTSTATUS status = NT_STATUS_OK;
5050 if (smbXcli_conn_has_async_calls(cli->conn)) {
5052 * Can't use sync call while an async call is in flight
5054 status = NT_STATUS_INVALID_PARAMETER;
5055 goto fail;
5058 ev = samba_tevent_context_init(frame);
5059 if (ev == NULL) {
5060 status = NT_STATUS_NO_MEMORY;
5061 goto fail;
5064 req = cli_posix_mkdir_send(frame,
5066 cli,
5067 fname,
5068 mode);
5069 if (req == NULL) {
5070 status = NT_STATUS_NO_MEMORY;
5071 goto fail;
5074 if (!tevent_req_poll(req, ev)) {
5075 status = map_nt_error_from_unix(errno);
5076 goto fail;
5079 status = cli_posix_mkdir_recv(req);
5081 fail:
5082 TALLOC_FREE(frame);
5083 return status;
5086 /****************************************************************************
5087 unlink or rmdir - POSIX semantics.
5088 ****************************************************************************/
5090 struct cli_posix_unlink_internal_state {
5091 uint8_t data[2];
5094 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5096 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5097 struct tevent_context *ev,
5098 struct cli_state *cli,
5099 const char *fname,
5100 uint16_t level)
5102 struct tevent_req *req = NULL, *subreq = NULL;
5103 struct cli_posix_unlink_internal_state *state = NULL;
5105 req = tevent_req_create(mem_ctx, &state,
5106 struct cli_posix_unlink_internal_state);
5107 if (req == NULL) {
5108 return NULL;
5111 /* Setup data word. */
5112 SSVAL(state->data, 0, level);
5114 subreq = cli_setpathinfo_send(state, ev, cli,
5115 SMB_POSIX_PATH_UNLINK,
5116 fname,
5117 state->data, sizeof(state->data));
5118 if (tevent_req_nomem(subreq, req)) {
5119 return tevent_req_post(req, ev);
5121 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5122 return req;
5125 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5127 NTSTATUS status = cli_setpathinfo_recv(subreq);
5128 tevent_req_simple_finish_ntstatus(subreq, status);
5131 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5132 struct tevent_context *ev,
5133 struct cli_state *cli,
5134 const char *fname)
5136 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5137 SMB_POSIX_UNLINK_FILE_TARGET);
5140 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5142 return tevent_req_simple_recv_ntstatus(req);
5145 /****************************************************************************
5146 unlink - POSIX semantics.
5147 ****************************************************************************/
5149 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5151 TALLOC_CTX *frame = talloc_stackframe();
5152 struct tevent_context *ev = NULL;
5153 struct tevent_req *req = NULL;
5154 NTSTATUS status = NT_STATUS_OK;
5156 if (smbXcli_conn_has_async_calls(cli->conn)) {
5158 * Can't use sync call while an async call is in flight
5160 status = NT_STATUS_INVALID_PARAMETER;
5161 goto fail;
5164 ev = samba_tevent_context_init(frame);
5165 if (ev == NULL) {
5166 status = NT_STATUS_NO_MEMORY;
5167 goto fail;
5170 req = cli_posix_unlink_send(frame,
5172 cli,
5173 fname);
5174 if (req == NULL) {
5175 status = NT_STATUS_NO_MEMORY;
5176 goto fail;
5179 if (!tevent_req_poll(req, ev)) {
5180 status = map_nt_error_from_unix(errno);
5181 goto fail;
5184 status = cli_posix_unlink_recv(req);
5186 fail:
5187 TALLOC_FREE(frame);
5188 return status;
5191 /****************************************************************************
5192 rmdir - POSIX semantics.
5193 ****************************************************************************/
5195 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5196 struct tevent_context *ev,
5197 struct cli_state *cli,
5198 const char *fname)
5200 return cli_posix_unlink_internal_send(
5201 mem_ctx, ev, cli, fname,
5202 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5205 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5207 return tevent_req_simple_recv_ntstatus(req);
5210 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5212 TALLOC_CTX *frame = talloc_stackframe();
5213 struct tevent_context *ev = NULL;
5214 struct tevent_req *req = NULL;
5215 NTSTATUS status = NT_STATUS_OK;
5217 if (smbXcli_conn_has_async_calls(cli->conn)) {
5219 * Can't use sync call while an async call is in flight
5221 status = NT_STATUS_INVALID_PARAMETER;
5222 goto fail;
5225 ev = samba_tevent_context_init(frame);
5226 if (ev == NULL) {
5227 status = NT_STATUS_NO_MEMORY;
5228 goto fail;
5231 req = cli_posix_rmdir_send(frame,
5233 cli,
5234 fname);
5235 if (req == NULL) {
5236 status = NT_STATUS_NO_MEMORY;
5237 goto fail;
5240 if (!tevent_req_poll(req, ev)) {
5241 status = map_nt_error_from_unix(errno);
5242 goto fail;
5245 status = cli_posix_rmdir_recv(req, frame);
5247 fail:
5248 TALLOC_FREE(frame);
5249 return status;
5252 /****************************************************************************
5253 filechangenotify
5254 ****************************************************************************/
5256 struct cli_notify_state {
5257 uint8_t setup[8];
5258 uint32_t num_changes;
5259 struct notify_change *changes;
5262 static void cli_notify_done(struct tevent_req *subreq);
5264 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5265 struct tevent_context *ev,
5266 struct cli_state *cli, uint16_t fnum,
5267 uint32_t buffer_size,
5268 uint32_t completion_filter, bool recursive)
5270 struct tevent_req *req, *subreq;
5271 struct cli_notify_state *state;
5272 unsigned old_timeout;
5274 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5275 if (req == NULL) {
5276 return NULL;
5279 SIVAL(state->setup, 0, completion_filter);
5280 SSVAL(state->setup, 4, fnum);
5281 SSVAL(state->setup, 6, recursive);
5284 * Notifies should not time out
5286 old_timeout = cli_set_timeout(cli, 0);
5288 subreq = cli_trans_send(
5289 state, /* mem ctx. */
5290 ev, /* event ctx. */
5291 cli, /* cli_state. */
5292 SMBnttrans, /* cmd. */
5293 NULL, /* pipe name. */
5294 -1, /* fid. */
5295 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5296 0, /* flags. */
5297 (uint16_t *)state->setup, /* setup. */
5298 4, /* num setup uint16_t words. */
5299 0, /* max returned setup. */
5300 NULL, /* param. */
5301 0, /* num param. */
5302 buffer_size, /* max returned param. */
5303 NULL, /* data. */
5304 0, /* num data. */
5305 0); /* max returned data. */
5307 cli_set_timeout(cli, old_timeout);
5309 if (tevent_req_nomem(subreq, req)) {
5310 return tevent_req_post(req, ev);
5312 tevent_req_set_callback(subreq, cli_notify_done, req);
5313 return req;
5316 static void cli_notify_done(struct tevent_req *subreq)
5318 struct tevent_req *req = tevent_req_callback_data(
5319 subreq, struct tevent_req);
5320 struct cli_notify_state *state = tevent_req_data(
5321 req, struct cli_notify_state);
5322 NTSTATUS status;
5323 uint8_t *params;
5324 uint32_t i, ofs, num_params;
5325 uint16_t flags2;
5327 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5328 &params, 0, &num_params, NULL, 0, NULL);
5329 TALLOC_FREE(subreq);
5330 if (tevent_req_nterror(req, status)) {
5331 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5332 return;
5335 state->num_changes = 0;
5336 ofs = 0;
5338 while (num_params - ofs > 12) {
5339 uint32_t next = IVAL(params, ofs);
5340 state->num_changes += 1;
5342 if ((next == 0) || (ofs+next >= num_params)) {
5343 break;
5345 ofs += next;
5348 state->changes = talloc_array(state, struct notify_change,
5349 state->num_changes);
5350 if (tevent_req_nomem(state->changes, req)) {
5351 TALLOC_FREE(params);
5352 return;
5355 ofs = 0;
5357 for (i=0; i<state->num_changes; i++) {
5358 uint32_t next = IVAL(params, ofs);
5359 uint32_t len = IVAL(params, ofs+8);
5360 ssize_t ret;
5361 char *name;
5363 if (trans_oob(num_params, ofs + 12, len)) {
5364 TALLOC_FREE(params);
5365 tevent_req_nterror(
5366 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5367 return;
5370 state->changes[i].action = IVAL(params, ofs+4);
5371 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5372 &name, params+ofs+12, len,
5373 STR_TERMINATE|STR_UNICODE);
5374 if (ret == -1) {
5375 TALLOC_FREE(params);
5376 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5377 return;
5379 state->changes[i].name = name;
5380 ofs += next;
5383 TALLOC_FREE(params);
5384 tevent_req_done(req);
5387 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5388 uint32_t *pnum_changes,
5389 struct notify_change **pchanges)
5391 struct cli_notify_state *state = tevent_req_data(
5392 req, struct cli_notify_state);
5393 NTSTATUS status;
5395 if (tevent_req_is_nterror(req, &status)) {
5396 return status;
5399 *pnum_changes = state->num_changes;
5400 *pchanges = talloc_move(mem_ctx, &state->changes);
5401 return NT_STATUS_OK;
5404 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5405 uint32_t completion_filter, bool recursive,
5406 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5407 struct notify_change **pchanges)
5409 TALLOC_CTX *frame = talloc_stackframe();
5410 struct tevent_context *ev;
5411 struct tevent_req *req;
5412 NTSTATUS status = NT_STATUS_NO_MEMORY;
5414 if (smbXcli_conn_has_async_calls(cli->conn)) {
5416 * Can't use sync call while an async call is in flight
5418 status = NT_STATUS_INVALID_PARAMETER;
5419 goto fail;
5421 ev = samba_tevent_context_init(frame);
5422 if (ev == NULL) {
5423 goto fail;
5425 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5426 completion_filter, recursive);
5427 if (req == NULL) {
5428 goto fail;
5430 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5431 goto fail;
5433 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5434 fail:
5435 TALLOC_FREE(frame);
5436 return status;
5439 struct cli_qpathinfo_state {
5440 uint8_t *param;
5441 uint8_t *data;
5442 uint16_t setup[1];
5443 uint32_t min_rdata;
5444 uint8_t *rdata;
5445 uint32_t num_rdata;
5448 static void cli_qpathinfo_done(struct tevent_req *subreq);
5450 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5451 struct tevent_context *ev,
5452 struct cli_state *cli, const char *fname,
5453 uint16_t level, uint32_t min_rdata,
5454 uint32_t max_rdata)
5456 struct tevent_req *req, *subreq;
5457 struct cli_qpathinfo_state *state;
5459 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5460 if (req == NULL) {
5461 return NULL;
5463 state->min_rdata = min_rdata;
5464 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5466 state->param = talloc_zero_array(state, uint8_t, 6);
5467 if (tevent_req_nomem(state->param, req)) {
5468 return tevent_req_post(req, ev);
5470 SSVAL(state->param, 0, level);
5471 state->param = trans2_bytes_push_str(
5472 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5473 if (tevent_req_nomem(state->param, req)) {
5474 return tevent_req_post(req, ev);
5477 subreq = cli_trans_send(
5478 state, /* mem ctx. */
5479 ev, /* event ctx. */
5480 cli, /* cli_state. */
5481 SMBtrans2, /* cmd. */
5482 NULL, /* pipe name. */
5483 -1, /* fid. */
5484 0, /* function. */
5485 0, /* flags. */
5486 state->setup, /* setup. */
5487 1, /* num setup uint16_t words. */
5488 0, /* max returned setup. */
5489 state->param, /* param. */
5490 talloc_get_size(state->param), /* num param. */
5491 2, /* max returned param. */
5492 NULL, /* data. */
5493 0, /* num data. */
5494 max_rdata); /* max returned data. */
5496 if (tevent_req_nomem(subreq, req)) {
5497 return tevent_req_post(req, ev);
5499 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5500 return req;
5503 static void cli_qpathinfo_done(struct tevent_req *subreq)
5505 struct tevent_req *req = tevent_req_callback_data(
5506 subreq, struct tevent_req);
5507 struct cli_qpathinfo_state *state = tevent_req_data(
5508 req, struct cli_qpathinfo_state);
5509 NTSTATUS status;
5511 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5512 NULL, 0, NULL,
5513 &state->rdata, state->min_rdata,
5514 &state->num_rdata);
5515 if (tevent_req_nterror(req, status)) {
5516 return;
5518 tevent_req_done(req);
5521 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5522 uint8_t **rdata, uint32_t *num_rdata)
5524 struct cli_qpathinfo_state *state = tevent_req_data(
5525 req, struct cli_qpathinfo_state);
5526 NTSTATUS status;
5528 if (tevent_req_is_nterror(req, &status)) {
5529 return status;
5531 if (rdata != NULL) {
5532 *rdata = talloc_move(mem_ctx, &state->rdata);
5533 } else {
5534 TALLOC_FREE(state->rdata);
5536 if (num_rdata != NULL) {
5537 *num_rdata = state->num_rdata;
5539 return NT_STATUS_OK;
5542 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5543 const char *fname, uint16_t level, uint32_t min_rdata,
5544 uint32_t max_rdata,
5545 uint8_t **rdata, uint32_t *num_rdata)
5547 TALLOC_CTX *frame = talloc_stackframe();
5548 struct tevent_context *ev;
5549 struct tevent_req *req;
5550 NTSTATUS status = NT_STATUS_NO_MEMORY;
5552 if (smbXcli_conn_has_async_calls(cli->conn)) {
5554 * Can't use sync call while an async call is in flight
5556 status = NT_STATUS_INVALID_PARAMETER;
5557 goto fail;
5559 ev = samba_tevent_context_init(frame);
5560 if (ev == NULL) {
5561 goto fail;
5563 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5564 max_rdata);
5565 if (req == NULL) {
5566 goto fail;
5568 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5569 goto fail;
5571 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5572 fail:
5573 TALLOC_FREE(frame);
5574 return status;
5577 struct cli_qfileinfo_state {
5578 uint16_t setup[1];
5579 uint8_t param[4];
5580 uint8_t *data;
5581 uint16_t recv_flags2;
5582 uint32_t min_rdata;
5583 uint8_t *rdata;
5584 uint32_t num_rdata;
5587 static void cli_qfileinfo_done(struct tevent_req *subreq);
5589 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5590 struct tevent_context *ev,
5591 struct cli_state *cli, uint16_t fnum,
5592 uint16_t level, uint32_t min_rdata,
5593 uint32_t max_rdata)
5595 struct tevent_req *req, *subreq;
5596 struct cli_qfileinfo_state *state;
5598 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5599 if (req == NULL) {
5600 return NULL;
5602 state->min_rdata = min_rdata;
5603 SSVAL(state->param, 0, fnum);
5604 SSVAL(state->param, 2, level);
5605 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5607 subreq = cli_trans_send(
5608 state, /* mem ctx. */
5609 ev, /* event ctx. */
5610 cli, /* cli_state. */
5611 SMBtrans2, /* cmd. */
5612 NULL, /* pipe name. */
5613 -1, /* fid. */
5614 0, /* function. */
5615 0, /* flags. */
5616 state->setup, /* setup. */
5617 1, /* num setup uint16_t words. */
5618 0, /* max returned setup. */
5619 state->param, /* param. */
5620 sizeof(state->param), /* num param. */
5621 2, /* max returned param. */
5622 NULL, /* data. */
5623 0, /* num data. */
5624 max_rdata); /* max returned data. */
5626 if (tevent_req_nomem(subreq, req)) {
5627 return tevent_req_post(req, ev);
5629 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5630 return req;
5633 static void cli_qfileinfo_done(struct tevent_req *subreq)
5635 struct tevent_req *req = tevent_req_callback_data(
5636 subreq, struct tevent_req);
5637 struct cli_qfileinfo_state *state = tevent_req_data(
5638 req, struct cli_qfileinfo_state);
5639 NTSTATUS status;
5641 status = cli_trans_recv(subreq, state,
5642 &state->recv_flags2,
5643 NULL, 0, NULL,
5644 NULL, 0, NULL,
5645 &state->rdata, state->min_rdata,
5646 &state->num_rdata);
5647 if (tevent_req_nterror(req, status)) {
5648 return;
5650 tevent_req_done(req);
5653 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5654 uint16_t *recv_flags2,
5655 uint8_t **rdata, uint32_t *num_rdata)
5657 struct cli_qfileinfo_state *state = tevent_req_data(
5658 req, struct cli_qfileinfo_state);
5659 NTSTATUS status;
5661 if (tevent_req_is_nterror(req, &status)) {
5662 return status;
5665 if (recv_flags2 != NULL) {
5666 *recv_flags2 = state->recv_flags2;
5668 if (rdata != NULL) {
5669 *rdata = talloc_move(mem_ctx, &state->rdata);
5670 } else {
5671 TALLOC_FREE(state->rdata);
5673 if (num_rdata != NULL) {
5674 *num_rdata = state->num_rdata;
5676 return NT_STATUS_OK;
5679 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5680 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5681 uint32_t max_rdata, uint16_t *recv_flags2,
5682 uint8_t **rdata, uint32_t *num_rdata)
5684 TALLOC_CTX *frame = talloc_stackframe();
5685 struct tevent_context *ev;
5686 struct tevent_req *req;
5687 NTSTATUS status = NT_STATUS_NO_MEMORY;
5689 if (smbXcli_conn_has_async_calls(cli->conn)) {
5691 * Can't use sync call while an async call is in flight
5693 status = NT_STATUS_INVALID_PARAMETER;
5694 goto fail;
5696 ev = samba_tevent_context_init(frame);
5697 if (ev == NULL) {
5698 goto fail;
5700 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5701 max_rdata);
5702 if (req == NULL) {
5703 goto fail;
5705 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5706 goto fail;
5708 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5709 fail:
5710 TALLOC_FREE(frame);
5711 return status;
5714 struct cli_flush_state {
5715 uint16_t vwv[1];
5718 static void cli_flush_done(struct tevent_req *subreq);
5720 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5721 struct tevent_context *ev,
5722 struct cli_state *cli,
5723 uint16_t fnum)
5725 struct tevent_req *req, *subreq;
5726 struct cli_flush_state *state;
5728 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5729 if (req == NULL) {
5730 return NULL;
5732 SSVAL(state->vwv + 0, 0, fnum);
5734 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5735 0, NULL);
5736 if (tevent_req_nomem(subreq, req)) {
5737 return tevent_req_post(req, ev);
5739 tevent_req_set_callback(subreq, cli_flush_done, req);
5740 return req;
5743 static void cli_flush_done(struct tevent_req *subreq)
5745 struct tevent_req *req = tevent_req_callback_data(
5746 subreq, struct tevent_req);
5747 NTSTATUS status;
5749 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5750 TALLOC_FREE(subreq);
5751 if (tevent_req_nterror(req, status)) {
5752 return;
5754 tevent_req_done(req);
5757 NTSTATUS cli_flush_recv(struct tevent_req *req)
5759 return tevent_req_simple_recv_ntstatus(req);
5762 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5764 TALLOC_CTX *frame = talloc_stackframe();
5765 struct tevent_context *ev;
5766 struct tevent_req *req;
5767 NTSTATUS status = NT_STATUS_NO_MEMORY;
5769 if (smbXcli_conn_has_async_calls(cli->conn)) {
5771 * Can't use sync call while an async call is in flight
5773 status = NT_STATUS_INVALID_PARAMETER;
5774 goto fail;
5776 ev = samba_tevent_context_init(frame);
5777 if (ev == NULL) {
5778 goto fail;
5780 req = cli_flush_send(frame, ev, cli, fnum);
5781 if (req == NULL) {
5782 goto fail;
5784 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5785 goto fail;
5787 status = cli_flush_recv(req);
5788 fail:
5789 TALLOC_FREE(frame);
5790 return status;
5793 struct cli_shadow_copy_data_state {
5794 uint16_t setup[4];
5795 uint8_t *data;
5796 uint32_t num_data;
5797 bool get_names;
5800 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5802 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5803 struct tevent_context *ev,
5804 struct cli_state *cli,
5805 uint16_t fnum,
5806 bool get_names)
5808 struct tevent_req *req, *subreq;
5809 struct cli_shadow_copy_data_state *state;
5810 uint32_t ret_size;
5812 req = tevent_req_create(mem_ctx, &state,
5813 struct cli_shadow_copy_data_state);
5814 if (req == NULL) {
5815 return NULL;
5817 state->get_names = get_names;
5818 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5820 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5821 SSVAL(state->setup + 2, 0, fnum);
5822 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5823 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5825 subreq = cli_trans_send(
5826 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5827 state->setup, ARRAY_SIZE(state->setup), 0,
5828 NULL, 0, 0,
5829 NULL, 0, ret_size);
5830 if (tevent_req_nomem(subreq, req)) {
5831 return tevent_req_post(req, ev);
5833 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5834 return req;
5837 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5839 struct tevent_req *req = tevent_req_callback_data(
5840 subreq, struct tevent_req);
5841 struct cli_shadow_copy_data_state *state = tevent_req_data(
5842 req, struct cli_shadow_copy_data_state);
5843 NTSTATUS status;
5845 status = cli_trans_recv(subreq, state, NULL,
5846 NULL, 0, NULL, /* setup */
5847 NULL, 0, NULL, /* param */
5848 &state->data, 12, &state->num_data);
5849 TALLOC_FREE(subreq);
5850 if (tevent_req_nterror(req, status)) {
5851 return;
5853 tevent_req_done(req);
5856 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5857 char ***pnames, int *pnum_names)
5859 struct cli_shadow_copy_data_state *state = tevent_req_data(
5860 req, struct cli_shadow_copy_data_state);
5861 char **names;
5862 int i, num_names;
5863 uint32_t dlength;
5864 NTSTATUS status;
5866 if (tevent_req_is_nterror(req, &status)) {
5867 return status;
5869 num_names = IVAL(state->data, 4);
5870 dlength = IVAL(state->data, 8);
5872 if (!state->get_names) {
5873 *pnum_names = num_names;
5874 return NT_STATUS_OK;
5877 if (dlength+12 > state->num_data) {
5878 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5880 names = talloc_array(mem_ctx, char *, num_names);
5881 if (names == NULL) {
5882 return NT_STATUS_NO_MEMORY;
5885 for (i=0; i<num_names; i++) {
5886 bool ret;
5887 uint8_t *src;
5888 size_t converted_size;
5890 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5891 ret = convert_string_talloc(
5892 names, CH_UTF16LE, CH_UNIX,
5893 src, 2 * sizeof(SHADOW_COPY_LABEL),
5894 &names[i], &converted_size);
5895 if (!ret) {
5896 TALLOC_FREE(names);
5897 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5900 *pnum_names = num_names;
5901 *pnames = names;
5902 return NT_STATUS_OK;
5905 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5906 uint16_t fnum, bool get_names,
5907 char ***pnames, int *pnum_names)
5909 TALLOC_CTX *frame = talloc_stackframe();
5910 struct tevent_context *ev;
5911 struct tevent_req *req;
5912 NTSTATUS status = NT_STATUS_NO_MEMORY;
5914 if (smbXcli_conn_has_async_calls(cli->conn)) {
5916 * Can't use sync call while an async call is in flight
5918 status = NT_STATUS_INVALID_PARAMETER;
5919 goto fail;
5921 ev = samba_tevent_context_init(frame);
5922 if (ev == NULL) {
5923 goto fail;
5925 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5926 if (req == NULL) {
5927 goto fail;
5929 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5930 goto fail;
5932 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5933 fail:
5934 TALLOC_FREE(frame);
5935 return status;