s3:libsmb: Plumb cli_smb2_rmdir() inside cli_rmdir().
[Samba/wip.git] / source3 / libsmb / clifile.c
blobd63734d3f7463f8edb88e44e4ca242a7b7029465
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_ntcreate_state {
1796 uint16_t vwv[24];
1797 uint16_t fnum;
1800 static void cli_ntcreate_done(struct tevent_req *subreq);
1802 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1803 struct tevent_context *ev,
1804 struct cli_state *cli,
1805 const char *fname,
1806 uint32_t CreatFlags,
1807 uint32_t DesiredAccess,
1808 uint32_t FileAttributes,
1809 uint32_t ShareAccess,
1810 uint32_t CreateDisposition,
1811 uint32_t CreateOptions,
1812 uint8_t SecurityFlags)
1814 struct tevent_req *req, *subreq;
1815 struct cli_ntcreate_state *state;
1816 uint16_t *vwv;
1817 uint8_t *bytes;
1818 size_t converted_len;
1820 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1821 if (req == NULL) {
1822 return NULL;
1825 vwv = state->vwv;
1827 SCVAL(vwv+0, 0, 0xFF);
1828 SCVAL(vwv+0, 1, 0);
1829 SSVAL(vwv+1, 0, 0);
1830 SCVAL(vwv+2, 0, 0);
1832 if (cli->use_oplocks) {
1833 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1835 SIVAL(vwv+3, 1, CreatFlags);
1836 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1837 SIVAL(vwv+7, 1, DesiredAccess);
1838 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1839 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1840 SIVAL(vwv+13, 1, FileAttributes);
1841 SIVAL(vwv+15, 1, ShareAccess);
1842 SIVAL(vwv+17, 1, CreateDisposition);
1843 SIVAL(vwv+19, 1, CreateOptions |
1844 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1845 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1846 SCVAL(vwv+23, 1, SecurityFlags);
1848 bytes = talloc_array(state, uint8_t, 0);
1849 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1850 fname, strlen(fname)+1,
1851 &converted_len);
1853 /* sigh. this copes with broken netapp filer behaviour */
1854 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1856 if (tevent_req_nomem(bytes, req)) {
1857 return tevent_req_post(req, ev);
1860 SSVAL(vwv+2, 1, converted_len);
1862 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1863 talloc_get_size(bytes), bytes);
1864 if (tevent_req_nomem(subreq, req)) {
1865 return tevent_req_post(req, ev);
1867 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1868 return req;
1871 static void cli_ntcreate_done(struct tevent_req *subreq)
1873 struct tevent_req *req = tevent_req_callback_data(
1874 subreq, struct tevent_req);
1875 struct cli_ntcreate_state *state = tevent_req_data(
1876 req, struct cli_ntcreate_state);
1877 uint8_t wct;
1878 uint16_t *vwv;
1879 uint32_t num_bytes;
1880 uint8_t *bytes;
1881 NTSTATUS status;
1883 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv,
1884 &num_bytes, &bytes);
1885 TALLOC_FREE(subreq);
1886 if (tevent_req_nterror(req, status)) {
1887 return;
1889 state->fnum = SVAL(vwv+2, 1);
1890 tevent_req_done(req);
1893 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1895 struct cli_ntcreate_state *state = tevent_req_data(
1896 req, struct cli_ntcreate_state);
1897 NTSTATUS status;
1899 if (tevent_req_is_nterror(req, &status)) {
1900 return status;
1902 *pfnum = state->fnum;
1903 return NT_STATUS_OK;
1906 NTSTATUS cli_ntcreate(struct cli_state *cli,
1907 const char *fname,
1908 uint32_t CreatFlags,
1909 uint32_t DesiredAccess,
1910 uint32_t FileAttributes,
1911 uint32_t ShareAccess,
1912 uint32_t CreateDisposition,
1913 uint32_t CreateOptions,
1914 uint8_t SecurityFlags,
1915 uint16_t *pfid)
1917 TALLOC_CTX *frame = talloc_stackframe();
1918 struct tevent_context *ev;
1919 struct tevent_req *req;
1920 NTSTATUS status = NT_STATUS_OK;
1922 if (smbXcli_conn_has_async_calls(cli->conn)) {
1924 * Can't use sync call while an async call is in flight
1926 status = NT_STATUS_INVALID_PARAMETER;
1927 goto fail;
1930 ev = samba_tevent_context_init(frame);
1931 if (ev == NULL) {
1932 status = NT_STATUS_NO_MEMORY;
1933 goto fail;
1936 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1937 DesiredAccess, FileAttributes, ShareAccess,
1938 CreateDisposition, CreateOptions,
1939 SecurityFlags);
1940 if (req == NULL) {
1941 status = NT_STATUS_NO_MEMORY;
1942 goto fail;
1945 if (!tevent_req_poll(req, ev)) {
1946 status = map_nt_error_from_unix(errno);
1947 goto fail;
1950 status = cli_ntcreate_recv(req, pfid);
1951 fail:
1952 TALLOC_FREE(frame);
1953 return status;
1956 struct cli_nttrans_create_state {
1957 uint16_t fnum;
1960 static void cli_nttrans_create_done(struct tevent_req *subreq);
1962 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
1963 struct tevent_context *ev,
1964 struct cli_state *cli,
1965 const char *fname,
1966 uint32_t CreatFlags,
1967 uint32_t DesiredAccess,
1968 uint32_t FileAttributes,
1969 uint32_t ShareAccess,
1970 uint32_t CreateDisposition,
1971 uint32_t CreateOptions,
1972 uint8_t SecurityFlags,
1973 struct security_descriptor *secdesc,
1974 struct ea_struct *eas,
1975 int num_eas)
1977 struct tevent_req *req, *subreq;
1978 struct cli_nttrans_create_state *state;
1979 uint8_t *param;
1980 uint8_t *secdesc_buf;
1981 size_t secdesc_len;
1982 NTSTATUS status;
1983 size_t converted_len;
1985 req = tevent_req_create(mem_ctx,
1986 &state, struct cli_nttrans_create_state);
1987 if (req == NULL) {
1988 return NULL;
1991 if (secdesc != NULL) {
1992 status = marshall_sec_desc(talloc_tos(), secdesc,
1993 &secdesc_buf, &secdesc_len);
1994 if (tevent_req_nterror(req, status)) {
1995 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1996 nt_errstr(status)));
1997 return tevent_req_post(req, ev);
1999 } else {
2000 secdesc_buf = NULL;
2001 secdesc_len = 0;
2004 if (num_eas != 0) {
2006 * TODO ;-)
2008 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2009 return tevent_req_post(req, ev);
2012 param = talloc_array(state, uint8_t, 53);
2013 if (tevent_req_nomem(param, req)) {
2014 return tevent_req_post(req, ev);
2017 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2018 fname, strlen(fname),
2019 &converted_len);
2020 if (tevent_req_nomem(param, req)) {
2021 return tevent_req_post(req, ev);
2024 SIVAL(param, 0, CreatFlags);
2025 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2026 SIVAL(param, 8, DesiredAccess);
2027 SIVAL(param, 12, 0x0); /* AllocationSize */
2028 SIVAL(param, 16, 0x0); /* AllocationSize */
2029 SIVAL(param, 20, FileAttributes);
2030 SIVAL(param, 24, ShareAccess);
2031 SIVAL(param, 28, CreateDisposition);
2032 SIVAL(param, 32, CreateOptions |
2033 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2034 SIVAL(param, 36, secdesc_len);
2035 SIVAL(param, 40, 0); /* EA length*/
2036 SIVAL(param, 44, converted_len);
2037 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2038 SCVAL(param, 52, SecurityFlags);
2040 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2041 NULL, -1, /* name, fid */
2042 NT_TRANSACT_CREATE, 0,
2043 NULL, 0, 0, /* setup */
2044 param, talloc_get_size(param), 128, /* param */
2045 secdesc_buf, secdesc_len, 0); /* data */
2046 if (tevent_req_nomem(subreq, req)) {
2047 return tevent_req_post(req, ev);
2049 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2050 return req;
2053 static void cli_nttrans_create_done(struct tevent_req *subreq)
2055 struct tevent_req *req = tevent_req_callback_data(
2056 subreq, struct tevent_req);
2057 struct cli_nttrans_create_state *state = tevent_req_data(
2058 req, struct cli_nttrans_create_state);
2059 uint8_t *param;
2060 uint32_t num_param;
2061 NTSTATUS status;
2063 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2064 NULL, 0, NULL, /* rsetup */
2065 &param, 69, &num_param,
2066 NULL, 0, NULL);
2067 if (tevent_req_nterror(req, status)) {
2068 return;
2070 state->fnum = SVAL(param, 2);
2071 TALLOC_FREE(param);
2072 tevent_req_done(req);
2075 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
2077 struct cli_nttrans_create_state *state = tevent_req_data(
2078 req, struct cli_nttrans_create_state);
2079 NTSTATUS status;
2081 if (tevent_req_is_nterror(req, &status)) {
2082 return status;
2084 *fnum = state->fnum;
2085 return NT_STATUS_OK;
2088 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2089 const char *fname,
2090 uint32_t CreatFlags,
2091 uint32_t DesiredAccess,
2092 uint32_t FileAttributes,
2093 uint32_t ShareAccess,
2094 uint32_t CreateDisposition,
2095 uint32_t CreateOptions,
2096 uint8_t SecurityFlags,
2097 struct security_descriptor *secdesc,
2098 struct ea_struct *eas,
2099 int num_eas,
2100 uint16_t *pfid)
2102 TALLOC_CTX *frame = talloc_stackframe();
2103 struct tevent_context *ev;
2104 struct tevent_req *req;
2105 NTSTATUS status = NT_STATUS_NO_MEMORY;
2107 if (smbXcli_conn_has_async_calls(cli->conn)) {
2109 * Can't use sync call while an async call is in flight
2111 status = NT_STATUS_INVALID_PARAMETER;
2112 goto fail;
2114 ev = samba_tevent_context_init(frame);
2115 if (ev == NULL) {
2116 goto fail;
2118 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2119 DesiredAccess, FileAttributes,
2120 ShareAccess, CreateDisposition,
2121 CreateOptions, SecurityFlags,
2122 secdesc, eas, num_eas);
2123 if (req == NULL) {
2124 goto fail;
2126 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2127 goto fail;
2129 status = cli_nttrans_create_recv(req, pfid);
2130 fail:
2131 TALLOC_FREE(frame);
2132 return status;
2135 /****************************************************************************
2136 Open a file
2137 WARNING: if you open with O_WRONLY then getattrE won't work!
2138 ****************************************************************************/
2140 struct cli_openx_state {
2141 const char *fname;
2142 uint16_t vwv[15];
2143 uint16_t fnum;
2144 struct iovec bytes;
2147 static void cli_openx_done(struct tevent_req *subreq);
2149 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2150 struct tevent_context *ev,
2151 struct cli_state *cli, const char *fname,
2152 int flags, int share_mode,
2153 struct tevent_req **psmbreq)
2155 struct tevent_req *req, *subreq;
2156 struct cli_openx_state *state;
2157 unsigned openfn;
2158 unsigned accessmode;
2159 uint8_t additional_flags;
2160 uint8_t *bytes;
2162 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2163 if (req == NULL) {
2164 return NULL;
2167 openfn = 0;
2168 if (flags & O_CREAT) {
2169 openfn |= (1<<4);
2171 if (!(flags & O_EXCL)) {
2172 if (flags & O_TRUNC)
2173 openfn |= (1<<1);
2174 else
2175 openfn |= (1<<0);
2178 accessmode = (share_mode<<4);
2180 if ((flags & O_ACCMODE) == O_RDWR) {
2181 accessmode |= 2;
2182 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2183 accessmode |= 1;
2186 #if defined(O_SYNC)
2187 if ((flags & O_SYNC) == O_SYNC) {
2188 accessmode |= (1<<14);
2190 #endif /* O_SYNC */
2192 if (share_mode == DENY_FCB) {
2193 accessmode = 0xFF;
2196 SCVAL(state->vwv + 0, 0, 0xFF);
2197 SCVAL(state->vwv + 0, 1, 0);
2198 SSVAL(state->vwv + 1, 0, 0);
2199 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2200 SSVAL(state->vwv + 3, 0, accessmode);
2201 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2202 SSVAL(state->vwv + 5, 0, 0);
2203 SIVAL(state->vwv + 6, 0, 0);
2204 SSVAL(state->vwv + 8, 0, openfn);
2205 SIVAL(state->vwv + 9, 0, 0);
2206 SIVAL(state->vwv + 11, 0, 0);
2207 SIVAL(state->vwv + 13, 0, 0);
2209 additional_flags = 0;
2211 if (cli->use_oplocks) {
2212 /* if using oplocks then ask for a batch oplock via
2213 core and extended methods */
2214 additional_flags =
2215 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2216 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2219 bytes = talloc_array(state, uint8_t, 0);
2220 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2221 strlen(fname)+1, NULL);
2223 if (tevent_req_nomem(bytes, req)) {
2224 return tevent_req_post(req, ev);
2227 state->bytes.iov_base = (void *)bytes;
2228 state->bytes.iov_len = talloc_get_size(bytes);
2230 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2231 15, state->vwv, 1, &state->bytes);
2232 if (subreq == NULL) {
2233 TALLOC_FREE(req);
2234 return NULL;
2236 tevent_req_set_callback(subreq, cli_openx_done, req);
2237 *psmbreq = subreq;
2238 return req;
2241 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2242 struct cli_state *cli, const char *fname,
2243 int flags, int share_mode)
2245 struct tevent_req *req, *subreq;
2246 NTSTATUS status;
2248 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2249 &subreq);
2250 if (req == NULL) {
2251 return NULL;
2254 status = smb1cli_req_chain_submit(&subreq, 1);
2255 if (tevent_req_nterror(req, status)) {
2256 return tevent_req_post(req, ev);
2258 return req;
2261 static void cli_openx_done(struct tevent_req *subreq)
2263 struct tevent_req *req = tevent_req_callback_data(
2264 subreq, struct tevent_req);
2265 struct cli_openx_state *state = tevent_req_data(
2266 req, struct cli_openx_state);
2267 uint8_t wct;
2268 uint16_t *vwv;
2269 NTSTATUS status;
2271 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2272 NULL);
2273 TALLOC_FREE(subreq);
2274 if (tevent_req_nterror(req, status)) {
2275 return;
2277 state->fnum = SVAL(vwv+2, 0);
2278 tevent_req_done(req);
2281 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2283 struct cli_openx_state *state = tevent_req_data(
2284 req, struct cli_openx_state);
2285 NTSTATUS status;
2287 if (tevent_req_is_nterror(req, &status)) {
2288 return status;
2290 *pfnum = state->fnum;
2291 return NT_STATUS_OK;
2294 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2295 int share_mode, uint16_t *pfnum)
2297 TALLOC_CTX *frame = talloc_stackframe();
2298 struct tevent_context *ev;
2299 struct tevent_req *req;
2300 NTSTATUS status = NT_STATUS_NO_MEMORY;
2302 if (smbXcli_conn_has_async_calls(cli->conn)) {
2304 * Can't use sync call while an async call is in flight
2306 status = NT_STATUS_INVALID_PARAMETER;
2307 goto fail;
2310 ev = samba_tevent_context_init(frame);
2311 if (ev == NULL) {
2312 goto fail;
2315 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2316 if (req == NULL) {
2317 goto fail;
2320 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2321 goto fail;
2324 status = cli_openx_recv(req, pfnum);
2325 fail:
2326 TALLOC_FREE(frame);
2327 return status;
2329 /****************************************************************************
2330 Synchronous wrapper function that does an NtCreateX open by preference
2331 and falls back to openX if this fails.
2332 ****************************************************************************/
2334 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2335 int share_mode_in, uint16_t *pfnum)
2337 NTSTATUS status;
2338 unsigned int openfn = 0;
2339 unsigned int dos_deny = 0;
2340 uint32_t access_mask, share_mode, create_disposition, create_options;
2342 /* Do the initial mapping into OpenX parameters. */
2343 if (flags & O_CREAT) {
2344 openfn |= (1<<4);
2346 if (!(flags & O_EXCL)) {
2347 if (flags & O_TRUNC)
2348 openfn |= (1<<1);
2349 else
2350 openfn |= (1<<0);
2353 dos_deny = (share_mode_in<<4);
2355 if ((flags & O_ACCMODE) == O_RDWR) {
2356 dos_deny |= 2;
2357 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2358 dos_deny |= 1;
2361 #if defined(O_SYNC)
2362 if ((flags & O_SYNC) == O_SYNC) {
2363 dos_deny |= (1<<14);
2365 #endif /* O_SYNC */
2367 if (share_mode_in == DENY_FCB) {
2368 dos_deny = 0xFF;
2371 #if 0
2372 /* Hmmm. This is what I think the above code
2373 should look like if it's using the constants
2374 we #define. JRA. */
2376 if (flags & O_CREAT) {
2377 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2379 if (!(flags & O_EXCL)) {
2380 if (flags & O_TRUNC)
2381 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2382 else
2383 openfn |= OPENX_FILE_EXISTS_OPEN;
2386 dos_deny = SET_DENY_MODE(share_mode_in);
2388 if ((flags & O_ACCMODE) == O_RDWR) {
2389 dos_deny |= DOS_OPEN_RDWR;
2390 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2391 dos_deny |= DOS_OPEN_WRONLY;
2394 #if defined(O_SYNC)
2395 if ((flags & O_SYNC) == O_SYNC) {
2396 dos_deny |= FILE_SYNC_OPENMODE;
2398 #endif /* O_SYNC */
2400 if (share_mode_in == DENY_FCB) {
2401 dos_deny = 0xFF;
2403 #endif
2405 if (!map_open_params_to_ntcreate(fname, dos_deny,
2406 openfn, &access_mask,
2407 &share_mode, &create_disposition,
2408 &create_options, NULL)) {
2409 goto try_openx;
2412 status = cli_ntcreate(cli,
2413 fname,
2415 access_mask,
2417 share_mode,
2418 create_disposition,
2419 create_options,
2421 pfnum);
2423 /* Try and cope will all varients of "we don't do this call"
2424 and fall back to openX. */
2426 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2427 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2428 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2429 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2430 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2431 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2432 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2433 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2434 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2435 goto try_openx;
2438 return status;
2440 try_openx:
2442 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2445 /****************************************************************************
2446 Close a file.
2447 ****************************************************************************/
2449 struct cli_close_state {
2450 uint16_t vwv[3];
2453 static void cli_close_done(struct tevent_req *subreq);
2455 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2456 struct tevent_context *ev,
2457 struct cli_state *cli,
2458 uint16_t fnum,
2459 struct tevent_req **psubreq)
2461 struct tevent_req *req, *subreq;
2462 struct cli_close_state *state;
2464 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2465 if (req == NULL) {
2466 return NULL;
2469 SSVAL(state->vwv+0, 0, fnum);
2470 SIVALS(state->vwv+1, 0, -1);
2472 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2473 0, NULL);
2474 if (subreq == NULL) {
2475 TALLOC_FREE(req);
2476 return NULL;
2478 tevent_req_set_callback(subreq, cli_close_done, req);
2479 *psubreq = subreq;
2480 return req;
2483 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2484 struct tevent_context *ev,
2485 struct cli_state *cli,
2486 uint16_t fnum)
2488 struct tevent_req *req, *subreq;
2489 NTSTATUS status;
2491 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2492 if (req == NULL) {
2493 return NULL;
2496 status = smb1cli_req_chain_submit(&subreq, 1);
2497 if (tevent_req_nterror(req, status)) {
2498 return tevent_req_post(req, ev);
2500 return req;
2503 static void cli_close_done(struct tevent_req *subreq)
2505 struct tevent_req *req = tevent_req_callback_data(
2506 subreq, struct tevent_req);
2507 NTSTATUS status;
2509 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2510 TALLOC_FREE(subreq);
2511 if (tevent_req_nterror(req, status)) {
2512 return;
2514 tevent_req_done(req);
2517 NTSTATUS cli_close_recv(struct tevent_req *req)
2519 return tevent_req_simple_recv_ntstatus(req);
2522 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2524 TALLOC_CTX *frame = talloc_stackframe();
2525 struct tevent_context *ev;
2526 struct tevent_req *req;
2527 NTSTATUS status = NT_STATUS_OK;
2529 if (smbXcli_conn_has_async_calls(cli->conn)) {
2531 * Can't use sync call while an async call is in flight
2533 status = NT_STATUS_INVALID_PARAMETER;
2534 goto fail;
2537 ev = samba_tevent_context_init(frame);
2538 if (ev == NULL) {
2539 status = NT_STATUS_NO_MEMORY;
2540 goto fail;
2543 req = cli_close_send(frame, ev, cli, fnum);
2544 if (req == NULL) {
2545 status = NT_STATUS_NO_MEMORY;
2546 goto fail;
2549 if (!tevent_req_poll(req, ev)) {
2550 status = map_nt_error_from_unix(errno);
2551 goto fail;
2554 status = cli_close_recv(req);
2555 fail:
2556 TALLOC_FREE(frame);
2557 return status;
2560 /****************************************************************************
2561 Truncate a file to a specified size
2562 ****************************************************************************/
2564 struct ftrunc_state {
2565 uint16_t setup;
2566 uint8_t param[6];
2567 uint8_t data[8];
2570 static void cli_ftruncate_done(struct tevent_req *subreq)
2572 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2573 NULL, 0, NULL, NULL, 0, NULL);
2574 tevent_req_simple_finish_ntstatus(subreq, status);
2577 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2578 struct tevent_context *ev,
2579 struct cli_state *cli,
2580 uint16_t fnum,
2581 uint64_t size)
2583 struct tevent_req *req = NULL, *subreq = NULL;
2584 struct ftrunc_state *state = NULL;
2586 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2587 if (req == NULL) {
2588 return NULL;
2591 /* Setup setup word. */
2592 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2594 /* Setup param array. */
2595 SSVAL(state->param,0,fnum);
2596 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2597 SSVAL(state->param,4,0);
2599 /* Setup data array. */
2600 SBVAL(state->data, 0, size);
2602 subreq = cli_trans_send(state, /* mem ctx. */
2603 ev, /* event ctx. */
2604 cli, /* cli_state. */
2605 SMBtrans2, /* cmd. */
2606 NULL, /* pipe name. */
2607 -1, /* fid. */
2608 0, /* function. */
2609 0, /* flags. */
2610 &state->setup, /* setup. */
2611 1, /* num setup uint16_t words. */
2612 0, /* max returned setup. */
2613 state->param, /* param. */
2614 6, /* num param. */
2615 2, /* max returned param. */
2616 state->data, /* data. */
2617 8, /* num data. */
2618 0); /* max returned data. */
2620 if (tevent_req_nomem(subreq, req)) {
2621 return tevent_req_post(req, ev);
2623 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2624 return req;
2627 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2629 return tevent_req_simple_recv_ntstatus(req);
2632 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2634 TALLOC_CTX *frame = talloc_stackframe();
2635 struct tevent_context *ev = NULL;
2636 struct tevent_req *req = NULL;
2637 NTSTATUS status = NT_STATUS_OK;
2639 if (smbXcli_conn_has_async_calls(cli->conn)) {
2641 * Can't use sync call while an async call is in flight
2643 status = NT_STATUS_INVALID_PARAMETER;
2644 goto fail;
2647 ev = samba_tevent_context_init(frame);
2648 if (ev == NULL) {
2649 status = NT_STATUS_NO_MEMORY;
2650 goto fail;
2653 req = cli_ftruncate_send(frame,
2655 cli,
2656 fnum,
2657 size);
2658 if (req == NULL) {
2659 status = NT_STATUS_NO_MEMORY;
2660 goto fail;
2663 if (!tevent_req_poll(req, ev)) {
2664 status = map_nt_error_from_unix(errno);
2665 goto fail;
2668 status = cli_ftruncate_recv(req);
2670 fail:
2671 TALLOC_FREE(frame);
2672 return status;
2675 /****************************************************************************
2676 send a lock with a specified locktype
2677 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2678 ****************************************************************************/
2680 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2681 uint32_t offset, uint32_t len,
2682 int timeout, unsigned char locktype)
2684 uint16_t vwv[8];
2685 uint8_t bytes[10];
2686 NTSTATUS status;
2687 unsigned int set_timeout = 0;
2688 unsigned int saved_timeout = 0;
2690 SCVAL(vwv + 0, 0, 0xff);
2691 SCVAL(vwv + 0, 1, 0);
2692 SSVAL(vwv + 1, 0, 0);
2693 SSVAL(vwv + 2, 0, fnum);
2694 SCVAL(vwv + 3, 0, locktype);
2695 SCVAL(vwv + 3, 1, 0);
2696 SIVALS(vwv + 4, 0, timeout);
2697 SSVAL(vwv + 6, 0, 0);
2698 SSVAL(vwv + 7, 0, 1);
2700 SSVAL(bytes, 0, cli_getpid(cli));
2701 SIVAL(bytes, 2, offset);
2702 SIVAL(bytes, 6, len);
2704 if (timeout != 0) {
2705 if (timeout == -1) {
2706 set_timeout = 0x7FFFFFFF;
2707 } else {
2708 set_timeout = timeout + 2*1000;
2710 saved_timeout = cli_set_timeout(cli, set_timeout);
2713 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2714 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2716 if (saved_timeout != 0) {
2717 cli_set_timeout(cli, saved_timeout);
2720 return status;
2723 /****************************************************************************
2724 Lock a file.
2725 note that timeout is in units of 2 milliseconds
2726 ****************************************************************************/
2728 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2729 uint32_t offset, uint32_t len, int timeout,
2730 enum brl_type lock_type)
2732 NTSTATUS status;
2734 status = cli_locktype(cli, fnum, offset, len, timeout,
2735 (lock_type == READ_LOCK? 1 : 0));
2736 return status;
2739 /****************************************************************************
2740 Unlock a file.
2741 ****************************************************************************/
2743 struct cli_unlock_state {
2744 uint16_t vwv[8];
2745 uint8_t data[10];
2748 static void cli_unlock_done(struct tevent_req *subreq);
2750 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2751 struct tevent_context *ev,
2752 struct cli_state *cli,
2753 uint16_t fnum,
2754 uint64_t offset,
2755 uint64_t len)
2758 struct tevent_req *req = NULL, *subreq = NULL;
2759 struct cli_unlock_state *state = NULL;
2760 uint8_t additional_flags = 0;
2762 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2763 if (req == NULL) {
2764 return NULL;
2767 SCVAL(state->vwv+0, 0, 0xFF);
2768 SSVAL(state->vwv+2, 0, fnum);
2769 SCVAL(state->vwv+3, 0, 0);
2770 SIVALS(state->vwv+4, 0, 0);
2771 SSVAL(state->vwv+6, 0, 1);
2772 SSVAL(state->vwv+7, 0, 0);
2774 SSVAL(state->data, 0, cli_getpid(cli));
2775 SIVAL(state->data, 2, offset);
2776 SIVAL(state->data, 6, len);
2778 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2779 8, state->vwv, 10, state->data);
2780 if (tevent_req_nomem(subreq, req)) {
2781 return tevent_req_post(req, ev);
2783 tevent_req_set_callback(subreq, cli_unlock_done, req);
2784 return req;
2787 static void cli_unlock_done(struct tevent_req *subreq)
2789 struct tevent_req *req = tevent_req_callback_data(
2790 subreq, struct tevent_req);
2791 NTSTATUS status;
2793 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2794 TALLOC_FREE(subreq);
2795 if (tevent_req_nterror(req, status)) {
2796 return;
2798 tevent_req_done(req);
2801 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2803 return tevent_req_simple_recv_ntstatus(req);
2806 NTSTATUS cli_unlock(struct cli_state *cli,
2807 uint16_t fnum,
2808 uint32_t offset,
2809 uint32_t len)
2811 TALLOC_CTX *frame = talloc_stackframe();
2812 struct tevent_context *ev;
2813 struct tevent_req *req;
2814 NTSTATUS status = NT_STATUS_OK;
2816 if (smbXcli_conn_has_async_calls(cli->conn)) {
2818 * Can't use sync call while an async call is in flight
2820 status = NT_STATUS_INVALID_PARAMETER;
2821 goto fail;
2824 ev = samba_tevent_context_init(frame);
2825 if (ev == NULL) {
2826 status = NT_STATUS_NO_MEMORY;
2827 goto fail;
2830 req = cli_unlock_send(frame, ev, cli,
2831 fnum, offset, len);
2832 if (req == NULL) {
2833 status = NT_STATUS_NO_MEMORY;
2834 goto fail;
2837 if (!tevent_req_poll(req, ev)) {
2838 status = map_nt_error_from_unix(errno);
2839 goto fail;
2842 status = cli_unlock_recv(req);
2844 fail:
2845 TALLOC_FREE(frame);
2846 return status;
2849 /****************************************************************************
2850 Lock a file with 64 bit offsets.
2851 ****************************************************************************/
2853 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
2854 uint64_t offset, uint64_t len, int timeout,
2855 enum brl_type lock_type)
2857 uint16_t vwv[8];
2858 uint8_t bytes[20];
2859 unsigned int set_timeout = 0;
2860 unsigned int saved_timeout = 0;
2861 int ltype;
2862 NTSTATUS status;
2864 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
2865 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
2868 ltype = (lock_type == READ_LOCK? 1 : 0);
2869 ltype |= LOCKING_ANDX_LARGE_FILES;
2871 SCVAL(vwv + 0, 0, 0xff);
2872 SCVAL(vwv + 0, 1, 0);
2873 SSVAL(vwv + 1, 0, 0);
2874 SSVAL(vwv + 2, 0, fnum);
2875 SCVAL(vwv + 3, 0, ltype);
2876 SCVAL(vwv + 3, 1, 0);
2877 SIVALS(vwv + 4, 0, timeout);
2878 SSVAL(vwv + 6, 0, 0);
2879 SSVAL(vwv + 7, 0, 1);
2881 SIVAL(bytes, 0, cli_getpid(cli));
2882 SOFF_T_R(bytes, 4, offset);
2883 SOFF_T_R(bytes, 12, len);
2885 if (timeout != 0) {
2886 if (timeout == -1) {
2887 set_timeout = 0x7FFFFFFF;
2888 } else {
2889 set_timeout = timeout + 2*1000;
2891 saved_timeout = cli_set_timeout(cli, set_timeout);
2894 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2895 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2897 if (saved_timeout != 0) {
2898 cli_set_timeout(cli, saved_timeout);
2901 return status;
2904 /****************************************************************************
2905 Unlock a file with 64 bit offsets.
2906 ****************************************************************************/
2908 struct cli_unlock64_state {
2909 uint16_t vwv[8];
2910 uint8_t data[20];
2913 static void cli_unlock64_done(struct tevent_req *subreq);
2915 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2916 struct tevent_context *ev,
2917 struct cli_state *cli,
2918 uint16_t fnum,
2919 uint64_t offset,
2920 uint64_t len)
2923 struct tevent_req *req = NULL, *subreq = NULL;
2924 struct cli_unlock64_state *state = NULL;
2925 uint8_t additional_flags = 0;
2927 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2928 if (req == NULL) {
2929 return NULL;
2932 SCVAL(state->vwv+0, 0, 0xff);
2933 SSVAL(state->vwv+2, 0, fnum);
2934 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2935 SIVALS(state->vwv+4, 0, 0);
2936 SSVAL(state->vwv+6, 0, 1);
2937 SSVAL(state->vwv+7, 0, 0);
2939 SIVAL(state->data, 0, cli_getpid(cli));
2940 SOFF_T_R(state->data, 4, offset);
2941 SOFF_T_R(state->data, 12, len);
2943 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2944 8, state->vwv, 20, state->data);
2945 if (tevent_req_nomem(subreq, req)) {
2946 return tevent_req_post(req, ev);
2948 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2949 return req;
2952 static void cli_unlock64_done(struct tevent_req *subreq)
2954 struct tevent_req *req = tevent_req_callback_data(
2955 subreq, struct tevent_req);
2956 NTSTATUS status;
2958 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2959 TALLOC_FREE(subreq);
2960 if (tevent_req_nterror(req, status)) {
2961 return;
2963 tevent_req_done(req);
2966 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2968 return tevent_req_simple_recv_ntstatus(req);
2971 NTSTATUS cli_unlock64(struct cli_state *cli,
2972 uint16_t fnum,
2973 uint64_t offset,
2974 uint64_t len)
2976 TALLOC_CTX *frame = talloc_stackframe();
2977 struct tevent_context *ev;
2978 struct tevent_req *req;
2979 NTSTATUS status = NT_STATUS_OK;
2981 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
2982 return cli_unlock(cli, fnum, offset, len);
2985 if (smbXcli_conn_has_async_calls(cli->conn)) {
2987 * Can't use sync call while an async call is in flight
2989 status = NT_STATUS_INVALID_PARAMETER;
2990 goto fail;
2993 ev = samba_tevent_context_init(frame);
2994 if (ev == NULL) {
2995 status = NT_STATUS_NO_MEMORY;
2996 goto fail;
2999 req = cli_unlock64_send(frame, ev, cli,
3000 fnum, offset, len);
3001 if (req == NULL) {
3002 status = NT_STATUS_NO_MEMORY;
3003 goto fail;
3006 if (!tevent_req_poll(req, ev)) {
3007 status = map_nt_error_from_unix(errno);
3008 goto fail;
3011 status = cli_unlock64_recv(req);
3013 fail:
3014 TALLOC_FREE(frame);
3015 return status;
3018 /****************************************************************************
3019 Get/unlock a POSIX lock on a file - internal function.
3020 ****************************************************************************/
3022 struct posix_lock_state {
3023 uint16_t setup;
3024 uint8_t param[4];
3025 uint8_t data[POSIX_LOCK_DATA_SIZE];
3028 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3030 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3031 NULL, 0, NULL, NULL, 0, NULL);
3032 tevent_req_simple_finish_ntstatus(subreq, status);
3035 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3036 struct tevent_context *ev,
3037 struct cli_state *cli,
3038 uint16_t fnum,
3039 uint64_t offset,
3040 uint64_t len,
3041 bool wait_lock,
3042 enum brl_type lock_type)
3044 struct tevent_req *req = NULL, *subreq = NULL;
3045 struct posix_lock_state *state = NULL;
3047 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3048 if (req == NULL) {
3049 return NULL;
3052 /* Setup setup word. */
3053 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3055 /* Setup param array. */
3056 SSVAL(&state->param, 0, fnum);
3057 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3059 /* Setup data array. */
3060 switch (lock_type) {
3061 case READ_LOCK:
3062 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3063 POSIX_LOCK_TYPE_READ);
3064 break;
3065 case WRITE_LOCK:
3066 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3067 POSIX_LOCK_TYPE_WRITE);
3068 break;
3069 case UNLOCK_LOCK:
3070 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3071 POSIX_LOCK_TYPE_UNLOCK);
3072 break;
3073 default:
3074 return NULL;
3077 if (wait_lock) {
3078 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3079 POSIX_LOCK_FLAG_WAIT);
3080 } else {
3081 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3082 POSIX_LOCK_FLAG_NOWAIT);
3085 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3086 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3087 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3089 subreq = cli_trans_send(state, /* mem ctx. */
3090 ev, /* event ctx. */
3091 cli, /* cli_state. */
3092 SMBtrans2, /* cmd. */
3093 NULL, /* pipe name. */
3094 -1, /* fid. */
3095 0, /* function. */
3096 0, /* flags. */
3097 &state->setup, /* setup. */
3098 1, /* num setup uint16_t words. */
3099 0, /* max returned setup. */
3100 state->param, /* param. */
3101 4, /* num param. */
3102 2, /* max returned param. */
3103 state->data, /* data. */
3104 POSIX_LOCK_DATA_SIZE, /* num data. */
3105 0); /* max returned data. */
3107 if (tevent_req_nomem(subreq, req)) {
3108 return tevent_req_post(req, ev);
3110 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3111 return req;
3114 /****************************************************************************
3115 POSIX Lock a file.
3116 ****************************************************************************/
3118 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3119 struct tevent_context *ev,
3120 struct cli_state *cli,
3121 uint16_t fnum,
3122 uint64_t offset,
3123 uint64_t len,
3124 bool wait_lock,
3125 enum brl_type lock_type)
3127 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3128 wait_lock, lock_type);
3131 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3133 return tevent_req_simple_recv_ntstatus(req);
3136 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3137 uint64_t offset, uint64_t len,
3138 bool wait_lock, enum brl_type lock_type)
3140 TALLOC_CTX *frame = talloc_stackframe();
3141 struct tevent_context *ev = NULL;
3142 struct tevent_req *req = NULL;
3143 NTSTATUS status = NT_STATUS_OK;
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 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3154 status = NT_STATUS_INVALID_PARAMETER;
3155 goto fail;
3158 ev = samba_tevent_context_init(frame);
3159 if (ev == NULL) {
3160 status = NT_STATUS_NO_MEMORY;
3161 goto fail;
3164 req = cli_posix_lock_send(frame,
3166 cli,
3167 fnum,
3168 offset,
3169 len,
3170 wait_lock,
3171 lock_type);
3172 if (req == NULL) {
3173 status = NT_STATUS_NO_MEMORY;
3174 goto fail;
3177 if (!tevent_req_poll(req, ev)) {
3178 status = map_nt_error_from_unix(errno);
3179 goto fail;
3182 status = cli_posix_lock_recv(req);
3184 fail:
3185 TALLOC_FREE(frame);
3186 return status;
3189 /****************************************************************************
3190 POSIX Unlock a file.
3191 ****************************************************************************/
3193 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3194 struct tevent_context *ev,
3195 struct cli_state *cli,
3196 uint16_t fnum,
3197 uint64_t offset,
3198 uint64_t len)
3200 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3201 false, UNLOCK_LOCK);
3204 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3206 return tevent_req_simple_recv_ntstatus(req);
3209 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3211 TALLOC_CTX *frame = talloc_stackframe();
3212 struct tevent_context *ev = NULL;
3213 struct tevent_req *req = NULL;
3214 NTSTATUS status = NT_STATUS_OK;
3216 if (smbXcli_conn_has_async_calls(cli->conn)) {
3218 * Can't use sync call while an async call is in flight
3220 status = NT_STATUS_INVALID_PARAMETER;
3221 goto fail;
3224 ev = samba_tevent_context_init(frame);
3225 if (ev == NULL) {
3226 status = NT_STATUS_NO_MEMORY;
3227 goto fail;
3230 req = cli_posix_unlock_send(frame,
3232 cli,
3233 fnum,
3234 offset,
3235 len);
3236 if (req == NULL) {
3237 status = NT_STATUS_NO_MEMORY;
3238 goto fail;
3241 if (!tevent_req_poll(req, ev)) {
3242 status = map_nt_error_from_unix(errno);
3243 goto fail;
3246 status = cli_posix_unlock_recv(req);
3248 fail:
3249 TALLOC_FREE(frame);
3250 return status;
3253 /****************************************************************************
3254 Do a SMBgetattrE call.
3255 ****************************************************************************/
3257 static void cli_getattrE_done(struct tevent_req *subreq);
3259 struct cli_getattrE_state {
3260 uint16_t vwv[1];
3261 int zone_offset;
3262 uint16_t attr;
3263 off_t size;
3264 time_t change_time;
3265 time_t access_time;
3266 time_t write_time;
3269 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3270 struct tevent_context *ev,
3271 struct cli_state *cli,
3272 uint16_t fnum)
3274 struct tevent_req *req = NULL, *subreq = NULL;
3275 struct cli_getattrE_state *state = NULL;
3276 uint8_t additional_flags = 0;
3278 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3279 if (req == NULL) {
3280 return NULL;
3283 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3284 SSVAL(state->vwv+0,0,fnum);
3286 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3287 1, state->vwv, 0, NULL);
3288 if (tevent_req_nomem(subreq, req)) {
3289 return tevent_req_post(req, ev);
3291 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3292 return req;
3295 static void cli_getattrE_done(struct tevent_req *subreq)
3297 struct tevent_req *req = tevent_req_callback_data(
3298 subreq, struct tevent_req);
3299 struct cli_getattrE_state *state = tevent_req_data(
3300 req, struct cli_getattrE_state);
3301 uint8_t wct;
3302 uint16_t *vwv = NULL;
3303 NTSTATUS status;
3305 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3306 NULL, NULL);
3307 TALLOC_FREE(subreq);
3308 if (tevent_req_nterror(req, status)) {
3309 return;
3312 state->size = (off_t)IVAL(vwv+6,0);
3313 state->attr = SVAL(vwv+10,0);
3314 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3315 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3316 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3318 tevent_req_done(req);
3321 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3322 uint16_t *attr,
3323 off_t *size,
3324 time_t *change_time,
3325 time_t *access_time,
3326 time_t *write_time)
3328 struct cli_getattrE_state *state = tevent_req_data(
3329 req, struct cli_getattrE_state);
3330 NTSTATUS status;
3332 if (tevent_req_is_nterror(req, &status)) {
3333 return status;
3335 if (attr) {
3336 *attr = state->attr;
3338 if (size) {
3339 *size = state->size;
3341 if (change_time) {
3342 *change_time = state->change_time;
3344 if (access_time) {
3345 *access_time = state->access_time;
3347 if (write_time) {
3348 *write_time = state->write_time;
3350 return NT_STATUS_OK;
3353 NTSTATUS cli_getattrE(struct cli_state *cli,
3354 uint16_t fnum,
3355 uint16_t *attr,
3356 off_t *size,
3357 time_t *change_time,
3358 time_t *access_time,
3359 time_t *write_time)
3361 TALLOC_CTX *frame = talloc_stackframe();
3362 struct tevent_context *ev = NULL;
3363 struct tevent_req *req = NULL;
3364 NTSTATUS status = NT_STATUS_OK;
3366 if (smbXcli_conn_has_async_calls(cli->conn)) {
3368 * Can't use sync call while an async call is in flight
3370 status = NT_STATUS_INVALID_PARAMETER;
3371 goto fail;
3374 ev = samba_tevent_context_init(frame);
3375 if (ev == NULL) {
3376 status = NT_STATUS_NO_MEMORY;
3377 goto fail;
3380 req = cli_getattrE_send(frame, ev, cli, fnum);
3381 if (req == NULL) {
3382 status = NT_STATUS_NO_MEMORY;
3383 goto fail;
3386 if (!tevent_req_poll(req, ev)) {
3387 status = map_nt_error_from_unix(errno);
3388 goto fail;
3391 status = cli_getattrE_recv(req,
3392 attr,
3393 size,
3394 change_time,
3395 access_time,
3396 write_time);
3398 fail:
3399 TALLOC_FREE(frame);
3400 return status;
3403 /****************************************************************************
3404 Do a SMBgetatr call
3405 ****************************************************************************/
3407 static void cli_getatr_done(struct tevent_req *subreq);
3409 struct cli_getatr_state {
3410 int zone_offset;
3411 uint16_t attr;
3412 off_t size;
3413 time_t write_time;
3416 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3417 struct tevent_context *ev,
3418 struct cli_state *cli,
3419 const char *fname)
3421 struct tevent_req *req = NULL, *subreq = NULL;
3422 struct cli_getatr_state *state = NULL;
3423 uint8_t additional_flags = 0;
3424 uint8_t *bytes = NULL;
3426 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3427 if (req == NULL) {
3428 return NULL;
3431 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3433 bytes = talloc_array(state, uint8_t, 1);
3434 if (tevent_req_nomem(bytes, req)) {
3435 return tevent_req_post(req, ev);
3437 bytes[0] = 4;
3438 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3439 strlen(fname)+1, NULL);
3441 if (tevent_req_nomem(bytes, req)) {
3442 return tevent_req_post(req, ev);
3445 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3446 0, NULL, talloc_get_size(bytes), bytes);
3447 if (tevent_req_nomem(subreq, req)) {
3448 return tevent_req_post(req, ev);
3450 tevent_req_set_callback(subreq, cli_getatr_done, req);
3451 return req;
3454 static void cli_getatr_done(struct tevent_req *subreq)
3456 struct tevent_req *req = tevent_req_callback_data(
3457 subreq, struct tevent_req);
3458 struct cli_getatr_state *state = tevent_req_data(
3459 req, struct cli_getatr_state);
3460 uint8_t wct;
3461 uint16_t *vwv = NULL;
3462 NTSTATUS status;
3464 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3465 NULL);
3466 TALLOC_FREE(subreq);
3467 if (tevent_req_nterror(req, status)) {
3468 return;
3471 state->attr = SVAL(vwv+0,0);
3472 state->size = (off_t)IVAL(vwv+3,0);
3473 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3475 tevent_req_done(req);
3478 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3479 uint16_t *attr,
3480 off_t *size,
3481 time_t *write_time)
3483 struct cli_getatr_state *state = tevent_req_data(
3484 req, struct cli_getatr_state);
3485 NTSTATUS status;
3487 if (tevent_req_is_nterror(req, &status)) {
3488 return status;
3490 if (attr) {
3491 *attr = state->attr;
3493 if (size) {
3494 *size = state->size;
3496 if (write_time) {
3497 *write_time = state->write_time;
3499 return NT_STATUS_OK;
3502 NTSTATUS cli_getatr(struct cli_state *cli,
3503 const char *fname,
3504 uint16_t *attr,
3505 off_t *size,
3506 time_t *write_time)
3508 TALLOC_CTX *frame = talloc_stackframe();
3509 struct tevent_context *ev = NULL;
3510 struct tevent_req *req = NULL;
3511 NTSTATUS status = NT_STATUS_OK;
3513 if (smbXcli_conn_has_async_calls(cli->conn)) {
3515 * Can't use sync call while an async call is in flight
3517 status = NT_STATUS_INVALID_PARAMETER;
3518 goto fail;
3521 ev = samba_tevent_context_init(frame);
3522 if (ev == NULL) {
3523 status = NT_STATUS_NO_MEMORY;
3524 goto fail;
3527 req = cli_getatr_send(frame, ev, cli, fname);
3528 if (req == NULL) {
3529 status = NT_STATUS_NO_MEMORY;
3530 goto fail;
3533 if (!tevent_req_poll(req, ev)) {
3534 status = map_nt_error_from_unix(errno);
3535 goto fail;
3538 status = cli_getatr_recv(req,
3539 attr,
3540 size,
3541 write_time);
3543 fail:
3544 TALLOC_FREE(frame);
3545 return status;
3548 /****************************************************************************
3549 Do a SMBsetattrE call.
3550 ****************************************************************************/
3552 static void cli_setattrE_done(struct tevent_req *subreq);
3554 struct cli_setattrE_state {
3555 uint16_t vwv[7];
3558 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3559 struct tevent_context *ev,
3560 struct cli_state *cli,
3561 uint16_t fnum,
3562 time_t change_time,
3563 time_t access_time,
3564 time_t write_time)
3566 struct tevent_req *req = NULL, *subreq = NULL;
3567 struct cli_setattrE_state *state = NULL;
3568 uint8_t additional_flags = 0;
3570 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3571 if (req == NULL) {
3572 return NULL;
3575 SSVAL(state->vwv+0, 0, fnum);
3576 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3577 smb1cli_conn_server_time_zone(cli->conn));
3578 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3579 smb1cli_conn_server_time_zone(cli->conn));
3580 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3581 smb1cli_conn_server_time_zone(cli->conn));
3583 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3584 7, state->vwv, 0, NULL);
3585 if (tevent_req_nomem(subreq, req)) {
3586 return tevent_req_post(req, ev);
3588 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3589 return req;
3592 static void cli_setattrE_done(struct tevent_req *subreq)
3594 struct tevent_req *req = tevent_req_callback_data(
3595 subreq, struct tevent_req);
3596 NTSTATUS status;
3598 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3599 TALLOC_FREE(subreq);
3600 if (tevent_req_nterror(req, status)) {
3601 return;
3603 tevent_req_done(req);
3606 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3608 return tevent_req_simple_recv_ntstatus(req);
3611 NTSTATUS cli_setattrE(struct cli_state *cli,
3612 uint16_t fnum,
3613 time_t change_time,
3614 time_t access_time,
3615 time_t write_time)
3617 TALLOC_CTX *frame = talloc_stackframe();
3618 struct tevent_context *ev = NULL;
3619 struct tevent_req *req = NULL;
3620 NTSTATUS status = NT_STATUS_OK;
3622 if (smbXcli_conn_has_async_calls(cli->conn)) {
3624 * Can't use sync call while an async call is in flight
3626 status = NT_STATUS_INVALID_PARAMETER;
3627 goto fail;
3630 ev = samba_tevent_context_init(frame);
3631 if (ev == NULL) {
3632 status = NT_STATUS_NO_MEMORY;
3633 goto fail;
3636 req = cli_setattrE_send(frame, ev,
3637 cli,
3638 fnum,
3639 change_time,
3640 access_time,
3641 write_time);
3643 if (req == NULL) {
3644 status = NT_STATUS_NO_MEMORY;
3645 goto fail;
3648 if (!tevent_req_poll(req, ev)) {
3649 status = map_nt_error_from_unix(errno);
3650 goto fail;
3653 status = cli_setattrE_recv(req);
3655 fail:
3656 TALLOC_FREE(frame);
3657 return status;
3660 /****************************************************************************
3661 Do a SMBsetatr call.
3662 ****************************************************************************/
3664 static void cli_setatr_done(struct tevent_req *subreq);
3666 struct cli_setatr_state {
3667 uint16_t vwv[8];
3670 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3671 struct tevent_context *ev,
3672 struct cli_state *cli,
3673 const char *fname,
3674 uint16_t attr,
3675 time_t mtime)
3677 struct tevent_req *req = NULL, *subreq = NULL;
3678 struct cli_setatr_state *state = NULL;
3679 uint8_t additional_flags = 0;
3680 uint8_t *bytes = NULL;
3682 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3683 if (req == NULL) {
3684 return NULL;
3687 SSVAL(state->vwv+0, 0, attr);
3688 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3690 bytes = talloc_array(state, uint8_t, 1);
3691 if (tevent_req_nomem(bytes, req)) {
3692 return tevent_req_post(req, ev);
3694 bytes[0] = 4;
3695 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3696 strlen(fname)+1, NULL);
3697 if (tevent_req_nomem(bytes, req)) {
3698 return tevent_req_post(req, ev);
3700 bytes = talloc_realloc(state, bytes, uint8_t,
3701 talloc_get_size(bytes)+1);
3702 if (tevent_req_nomem(bytes, req)) {
3703 return tevent_req_post(req, ev);
3706 bytes[talloc_get_size(bytes)-1] = 4;
3707 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3708 1, NULL);
3709 if (tevent_req_nomem(bytes, req)) {
3710 return tevent_req_post(req, ev);
3713 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3714 8, state->vwv, talloc_get_size(bytes), bytes);
3715 if (tevent_req_nomem(subreq, req)) {
3716 return tevent_req_post(req, ev);
3718 tevent_req_set_callback(subreq, cli_setatr_done, req);
3719 return req;
3722 static void cli_setatr_done(struct tevent_req *subreq)
3724 struct tevent_req *req = tevent_req_callback_data(
3725 subreq, struct tevent_req);
3726 NTSTATUS status;
3728 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3729 TALLOC_FREE(subreq);
3730 if (tevent_req_nterror(req, status)) {
3731 return;
3733 tevent_req_done(req);
3736 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3738 return tevent_req_simple_recv_ntstatus(req);
3741 NTSTATUS cli_setatr(struct cli_state *cli,
3742 const char *fname,
3743 uint16_t attr,
3744 time_t mtime)
3746 TALLOC_CTX *frame = talloc_stackframe();
3747 struct tevent_context *ev = NULL;
3748 struct tevent_req *req = NULL;
3749 NTSTATUS status = NT_STATUS_OK;
3751 if (smbXcli_conn_has_async_calls(cli->conn)) {
3753 * Can't use sync call while an async call is in flight
3755 status = NT_STATUS_INVALID_PARAMETER;
3756 goto fail;
3759 ev = samba_tevent_context_init(frame);
3760 if (ev == NULL) {
3761 status = NT_STATUS_NO_MEMORY;
3762 goto fail;
3765 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3766 if (req == NULL) {
3767 status = NT_STATUS_NO_MEMORY;
3768 goto fail;
3771 if (!tevent_req_poll(req, ev)) {
3772 status = map_nt_error_from_unix(errno);
3773 goto fail;
3776 status = cli_setatr_recv(req);
3778 fail:
3779 TALLOC_FREE(frame);
3780 return status;
3783 /****************************************************************************
3784 Check for existance of a dir.
3785 ****************************************************************************/
3787 static void cli_chkpath_done(struct tevent_req *subreq);
3789 struct cli_chkpath_state {
3790 int dummy;
3793 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3794 struct tevent_context *ev,
3795 struct cli_state *cli,
3796 const char *fname)
3798 struct tevent_req *req = NULL, *subreq = NULL;
3799 struct cli_chkpath_state *state = NULL;
3800 uint8_t additional_flags = 0;
3801 uint8_t *bytes = NULL;
3803 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3804 if (req == NULL) {
3805 return NULL;
3808 bytes = talloc_array(state, uint8_t, 1);
3809 if (tevent_req_nomem(bytes, req)) {
3810 return tevent_req_post(req, ev);
3812 bytes[0] = 4;
3813 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3814 strlen(fname)+1, NULL);
3816 if (tevent_req_nomem(bytes, req)) {
3817 return tevent_req_post(req, ev);
3820 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3821 0, NULL, talloc_get_size(bytes), bytes);
3822 if (tevent_req_nomem(subreq, req)) {
3823 return tevent_req_post(req, ev);
3825 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3826 return req;
3829 static void cli_chkpath_done(struct tevent_req *subreq)
3831 struct tevent_req *req = tevent_req_callback_data(
3832 subreq, struct tevent_req);
3833 NTSTATUS status;
3835 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3836 TALLOC_FREE(subreq);
3837 if (tevent_req_nterror(req, status)) {
3838 return;
3840 tevent_req_done(req);
3843 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3845 return tevent_req_simple_recv_ntstatus(req);
3848 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3850 TALLOC_CTX *frame = talloc_stackframe();
3851 struct tevent_context *ev = NULL;
3852 struct tevent_req *req = NULL;
3853 char *path2 = NULL;
3854 NTSTATUS status = NT_STATUS_OK;
3856 if (smbXcli_conn_has_async_calls(cli->conn)) {
3858 * Can't use sync call while an async call is in flight
3860 status = NT_STATUS_INVALID_PARAMETER;
3861 goto fail;
3864 path2 = talloc_strdup(frame, path);
3865 if (!path2) {
3866 status = NT_STATUS_NO_MEMORY;
3867 goto fail;
3869 trim_char(path2,'\0','\\');
3870 if (!*path2) {
3871 path2 = talloc_strdup(frame, "\\");
3872 if (!path2) {
3873 status = NT_STATUS_NO_MEMORY;
3874 goto fail;
3878 ev = samba_tevent_context_init(frame);
3879 if (ev == NULL) {
3880 status = NT_STATUS_NO_MEMORY;
3881 goto fail;
3884 req = cli_chkpath_send(frame, ev, cli, path2);
3885 if (req == NULL) {
3886 status = NT_STATUS_NO_MEMORY;
3887 goto fail;
3890 if (!tevent_req_poll(req, ev)) {
3891 status = map_nt_error_from_unix(errno);
3892 goto fail;
3895 status = cli_chkpath_recv(req);
3897 fail:
3898 TALLOC_FREE(frame);
3899 return status;
3902 /****************************************************************************
3903 Query disk space.
3904 ****************************************************************************/
3906 static void cli_dskattr_done(struct tevent_req *subreq);
3908 struct cli_dskattr_state {
3909 int bsize;
3910 int total;
3911 int avail;
3914 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3915 struct tevent_context *ev,
3916 struct cli_state *cli)
3918 struct tevent_req *req = NULL, *subreq = NULL;
3919 struct cli_dskattr_state *state = NULL;
3920 uint8_t additional_flags = 0;
3922 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3923 if (req == NULL) {
3924 return NULL;
3927 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3928 0, NULL, 0, NULL);
3929 if (tevent_req_nomem(subreq, req)) {
3930 return tevent_req_post(req, ev);
3932 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3933 return req;
3936 static void cli_dskattr_done(struct tevent_req *subreq)
3938 struct tevent_req *req = tevent_req_callback_data(
3939 subreq, struct tevent_req);
3940 struct cli_dskattr_state *state = tevent_req_data(
3941 req, struct cli_dskattr_state);
3942 uint8_t wct;
3943 uint16_t *vwv = NULL;
3944 NTSTATUS status;
3946 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3947 NULL);
3948 TALLOC_FREE(subreq);
3949 if (tevent_req_nterror(req, status)) {
3950 return;
3952 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3953 state->total = SVAL(vwv+0, 0);
3954 state->avail = SVAL(vwv+3, 0);
3955 tevent_req_done(req);
3958 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3960 struct cli_dskattr_state *state = tevent_req_data(
3961 req, struct cli_dskattr_state);
3962 NTSTATUS status;
3964 if (tevent_req_is_nterror(req, &status)) {
3965 return status;
3967 *bsize = state->bsize;
3968 *total = state->total;
3969 *avail = state->avail;
3970 return NT_STATUS_OK;
3973 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3975 TALLOC_CTX *frame = talloc_stackframe();
3976 struct tevent_context *ev = NULL;
3977 struct tevent_req *req = NULL;
3978 NTSTATUS status = NT_STATUS_OK;
3980 if (smbXcli_conn_has_async_calls(cli->conn)) {
3982 * Can't use sync call while an async call is in flight
3984 status = NT_STATUS_INVALID_PARAMETER;
3985 goto fail;
3988 ev = samba_tevent_context_init(frame);
3989 if (ev == NULL) {
3990 status = NT_STATUS_NO_MEMORY;
3991 goto fail;
3994 req = cli_dskattr_send(frame, ev, cli);
3995 if (req == NULL) {
3996 status = NT_STATUS_NO_MEMORY;
3997 goto fail;
4000 if (!tevent_req_poll(req, ev)) {
4001 status = map_nt_error_from_unix(errno);
4002 goto fail;
4005 status = cli_dskattr_recv(req, bsize, total, avail);
4007 fail:
4008 TALLOC_FREE(frame);
4009 return status;
4012 /****************************************************************************
4013 Create and open a temporary file.
4014 ****************************************************************************/
4016 static void cli_ctemp_done(struct tevent_req *subreq);
4018 struct ctemp_state {
4019 uint16_t vwv[3];
4020 char *ret_path;
4021 uint16_t fnum;
4024 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4025 struct tevent_context *ev,
4026 struct cli_state *cli,
4027 const char *path)
4029 struct tevent_req *req = NULL, *subreq = NULL;
4030 struct ctemp_state *state = NULL;
4031 uint8_t additional_flags = 0;
4032 uint8_t *bytes = NULL;
4034 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4035 if (req == NULL) {
4036 return NULL;
4039 SSVAL(state->vwv,0,0);
4040 SIVALS(state->vwv+1,0,-1);
4042 bytes = talloc_array(state, uint8_t, 1);
4043 if (tevent_req_nomem(bytes, req)) {
4044 return tevent_req_post(req, ev);
4046 bytes[0] = 4;
4047 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4048 strlen(path)+1, NULL);
4049 if (tevent_req_nomem(bytes, req)) {
4050 return tevent_req_post(req, ev);
4053 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4054 3, state->vwv, talloc_get_size(bytes), bytes);
4055 if (tevent_req_nomem(subreq, req)) {
4056 return tevent_req_post(req, ev);
4058 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4059 return req;
4062 static void cli_ctemp_done(struct tevent_req *subreq)
4064 struct tevent_req *req = tevent_req_callback_data(
4065 subreq, struct tevent_req);
4066 struct ctemp_state *state = tevent_req_data(
4067 req, struct ctemp_state);
4068 NTSTATUS status;
4069 uint8_t wcnt;
4070 uint16_t *vwv;
4071 uint32_t num_bytes = 0;
4072 uint8_t *bytes = NULL;
4074 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4075 &num_bytes, &bytes);
4076 TALLOC_FREE(subreq);
4077 if (tevent_req_nterror(req, status)) {
4078 return;
4081 state->fnum = SVAL(vwv+0, 0);
4083 /* From W2K3, the result is just the ASCII name */
4084 if (num_bytes < 2) {
4085 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4086 return;
4089 if (pull_string_talloc(state,
4090 NULL,
4092 &state->ret_path,
4093 bytes,
4094 num_bytes,
4095 STR_ASCII) == 0) {
4096 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4097 return;
4099 tevent_req_done(req);
4102 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4103 TALLOC_CTX *ctx,
4104 uint16_t *pfnum,
4105 char **outfile)
4107 struct ctemp_state *state = tevent_req_data(req,
4108 struct ctemp_state);
4109 NTSTATUS status;
4111 if (tevent_req_is_nterror(req, &status)) {
4112 return status;
4114 *pfnum = state->fnum;
4115 *outfile = talloc_strdup(ctx, state->ret_path);
4116 if (!*outfile) {
4117 return NT_STATUS_NO_MEMORY;
4119 return NT_STATUS_OK;
4122 NTSTATUS cli_ctemp(struct cli_state *cli,
4123 TALLOC_CTX *ctx,
4124 const char *path,
4125 uint16_t *pfnum,
4126 char **out_path)
4128 TALLOC_CTX *frame = talloc_stackframe();
4129 struct tevent_context *ev;
4130 struct tevent_req *req;
4131 NTSTATUS status = NT_STATUS_OK;
4133 if (smbXcli_conn_has_async_calls(cli->conn)) {
4135 * Can't use sync call while an async call is in flight
4137 status = NT_STATUS_INVALID_PARAMETER;
4138 goto fail;
4141 ev = samba_tevent_context_init(frame);
4142 if (ev == NULL) {
4143 status = NT_STATUS_NO_MEMORY;
4144 goto fail;
4147 req = cli_ctemp_send(frame, ev, cli, path);
4148 if (req == NULL) {
4149 status = NT_STATUS_NO_MEMORY;
4150 goto fail;
4153 if (!tevent_req_poll(req, ev)) {
4154 status = map_nt_error_from_unix(errno);
4155 goto fail;
4158 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4160 fail:
4161 TALLOC_FREE(frame);
4162 return status;
4166 send a raw ioctl - used by the torture code
4168 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4170 uint16_t vwv[3];
4171 NTSTATUS status;
4173 SSVAL(vwv+0, 0, fnum);
4174 SSVAL(vwv+1, 0, code>>16);
4175 SSVAL(vwv+2, 0, (code&0xFFFF));
4177 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4178 NULL, 0, NULL, NULL, NULL, NULL);
4179 if (!NT_STATUS_IS_OK(status)) {
4180 return status;
4182 *blob = data_blob_null;
4183 return NT_STATUS_OK;
4186 /*********************************************************
4187 Set an extended attribute utility fn.
4188 *********************************************************/
4190 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4191 uint8_t *param, unsigned int param_len,
4192 const char *ea_name,
4193 const char *ea_val, size_t ea_len)
4195 uint16_t setup[1];
4196 unsigned int data_len = 0;
4197 uint8_t *data = NULL;
4198 char *p;
4199 size_t ea_namelen = strlen(ea_name);
4200 NTSTATUS status;
4202 SSVAL(setup, 0, setup_val);
4204 if (ea_namelen == 0 && ea_len == 0) {
4205 data_len = 4;
4206 data = talloc_array(talloc_tos(),
4207 uint8_t,
4208 data_len);
4209 if (!data) {
4210 return NT_STATUS_NO_MEMORY;
4212 p = (char *)data;
4213 SIVAL(p,0,data_len);
4214 } else {
4215 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4216 data = talloc_array(talloc_tos(),
4217 uint8_t,
4218 data_len);
4219 if (!data) {
4220 return NT_STATUS_NO_MEMORY;
4222 p = (char *)data;
4223 SIVAL(p,0,data_len);
4224 p += 4;
4225 SCVAL(p, 0, 0); /* EA flags. */
4226 SCVAL(p, 1, ea_namelen);
4227 SSVAL(p, 2, ea_len);
4228 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4229 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4232 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4233 setup, 1, 0,
4234 param, param_len, 2,
4235 data, data_len, CLI_BUFFER_SIZE,
4236 NULL,
4237 NULL, 0, NULL, /* rsetup */
4238 NULL, 0, NULL, /* rparam */
4239 NULL, 0, NULL); /* rdata */
4240 talloc_free(data);
4241 return status;
4244 /*********************************************************
4245 Set an extended attribute on a pathname.
4246 *********************************************************/
4248 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4249 const char *ea_name, const char *ea_val,
4250 size_t ea_len)
4252 unsigned int param_len = 0;
4253 uint8_t *param;
4254 NTSTATUS status;
4255 TALLOC_CTX *frame = talloc_stackframe();
4257 param = talloc_array(talloc_tos(), uint8_t, 6);
4258 if (!param) {
4259 return NT_STATUS_NO_MEMORY;
4261 SSVAL(param,0,SMB_INFO_SET_EA);
4262 SSVAL(param,2,0);
4263 SSVAL(param,4,0);
4265 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4266 path, strlen(path)+1,
4267 NULL);
4268 param_len = talloc_get_size(param);
4270 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4271 ea_name, ea_val, ea_len);
4272 talloc_free(frame);
4273 return status;
4276 /*********************************************************
4277 Set an extended attribute on an fnum.
4278 *********************************************************/
4280 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4281 const char *ea_name, const char *ea_val,
4282 size_t ea_len)
4284 uint8_t param[6];
4286 memset(param, 0, 6);
4287 SSVAL(param,0,fnum);
4288 SSVAL(param,2,SMB_INFO_SET_EA);
4290 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4291 ea_name, ea_val, ea_len);
4294 /*********************************************************
4295 Get an extended attribute list utility fn.
4296 *********************************************************/
4298 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4299 size_t rdata_len,
4300 size_t *pnum_eas, struct ea_struct **pea_list)
4302 struct ea_struct *ea_list = NULL;
4303 size_t num_eas;
4304 size_t ea_size;
4305 const uint8_t *p;
4307 if (rdata_len < 4) {
4308 return false;
4311 ea_size = (size_t)IVAL(rdata,0);
4312 if (ea_size > rdata_len) {
4313 return false;
4316 if (ea_size == 0) {
4317 /* No EA's present. */
4318 *pnum_eas = 0;
4319 *pea_list = NULL;
4320 return true;
4323 p = rdata + 4;
4324 ea_size -= 4;
4326 /* Validate the EA list and count it. */
4327 for (num_eas = 0; ea_size >= 4; num_eas++) {
4328 unsigned int ea_namelen = CVAL(p,1);
4329 unsigned int ea_valuelen = SVAL(p,2);
4330 if (ea_namelen == 0) {
4331 return false;
4333 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4334 return false;
4336 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4337 p += 4 + ea_namelen + 1 + ea_valuelen;
4340 if (num_eas == 0) {
4341 *pnum_eas = 0;
4342 *pea_list = NULL;
4343 return true;
4346 *pnum_eas = num_eas;
4347 if (!pea_list) {
4348 /* Caller only wants number of EA's. */
4349 return true;
4352 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4353 if (!ea_list) {
4354 return false;
4357 ea_size = (size_t)IVAL(rdata,0);
4358 p = rdata + 4;
4360 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4361 struct ea_struct *ea = &ea_list[num_eas];
4362 fstring unix_ea_name;
4363 unsigned int ea_namelen = CVAL(p,1);
4364 unsigned int ea_valuelen = SVAL(p,2);
4366 ea->flags = CVAL(p,0);
4367 unix_ea_name[0] = '\0';
4368 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4369 ea->name = talloc_strdup(ea_list, unix_ea_name);
4370 if (!ea->name) {
4371 goto fail;
4373 /* Ensure the value is null terminated (in case it's a string). */
4374 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4375 if (!ea->value.data) {
4376 goto fail;
4378 if (ea_valuelen) {
4379 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4381 ea->value.data[ea_valuelen] = 0;
4382 ea->value.length--;
4383 p += 4 + ea_namelen + 1 + ea_valuelen;
4386 *pea_list = ea_list;
4387 return true;
4389 fail:
4390 TALLOC_FREE(ea_list);
4391 return false;
4394 /*********************************************************
4395 Get an extended attribute list from a pathname.
4396 *********************************************************/
4398 struct cli_get_ea_list_path_state {
4399 uint32_t num_data;
4400 uint8_t *data;
4403 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4405 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4406 struct tevent_context *ev,
4407 struct cli_state *cli,
4408 const char *fname)
4410 struct tevent_req *req, *subreq;
4411 struct cli_get_ea_list_path_state *state;
4413 req = tevent_req_create(mem_ctx, &state,
4414 struct cli_get_ea_list_path_state);
4415 if (req == NULL) {
4416 return NULL;
4418 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4419 SMB_INFO_QUERY_ALL_EAS, 4,
4420 CLI_BUFFER_SIZE);
4421 if (tevent_req_nomem(subreq, req)) {
4422 return tevent_req_post(req, ev);
4424 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4425 return req;
4428 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4430 struct tevent_req *req = tevent_req_callback_data(
4431 subreq, struct tevent_req);
4432 struct cli_get_ea_list_path_state *state = tevent_req_data(
4433 req, struct cli_get_ea_list_path_state);
4434 NTSTATUS status;
4436 status = cli_qpathinfo_recv(subreq, state, &state->data,
4437 &state->num_data);
4438 TALLOC_FREE(subreq);
4439 if (tevent_req_nterror(req, status)) {
4440 return;
4442 tevent_req_done(req);
4445 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4446 size_t *pnum_eas, struct ea_struct **peas)
4448 struct cli_get_ea_list_path_state *state = tevent_req_data(
4449 req, struct cli_get_ea_list_path_state);
4450 NTSTATUS status;
4452 if (tevent_req_is_nterror(req, &status)) {
4453 return status;
4455 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4456 pnum_eas, peas)) {
4457 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4459 return NT_STATUS_OK;
4462 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4463 TALLOC_CTX *ctx,
4464 size_t *pnum_eas,
4465 struct ea_struct **pea_list)
4467 TALLOC_CTX *frame = talloc_stackframe();
4468 struct tevent_context *ev = NULL;
4469 struct tevent_req *req = NULL;
4470 NTSTATUS status = NT_STATUS_NO_MEMORY;
4472 if (smbXcli_conn_has_async_calls(cli->conn)) {
4474 * Can't use sync call while an async call is in flight
4476 status = NT_STATUS_INVALID_PARAMETER;
4477 goto fail;
4479 ev = samba_tevent_context_init(frame);
4480 if (ev == NULL) {
4481 goto fail;
4483 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4484 if (req == NULL) {
4485 goto fail;
4487 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4488 goto fail;
4490 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4491 fail:
4492 TALLOC_FREE(frame);
4493 return status;
4496 /****************************************************************************
4497 Convert open "flags" arg to uint32_t on wire.
4498 ****************************************************************************/
4500 static uint32_t open_flags_to_wire(int flags)
4502 int open_mode = flags & O_ACCMODE;
4503 uint32_t ret = 0;
4505 switch (open_mode) {
4506 case O_WRONLY:
4507 ret |= SMB_O_WRONLY;
4508 break;
4509 case O_RDWR:
4510 ret |= SMB_O_RDWR;
4511 break;
4512 default:
4513 case O_RDONLY:
4514 ret |= SMB_O_RDONLY;
4515 break;
4518 if (flags & O_CREAT) {
4519 ret |= SMB_O_CREAT;
4521 if (flags & O_EXCL) {
4522 ret |= SMB_O_EXCL;
4524 if (flags & O_TRUNC) {
4525 ret |= SMB_O_TRUNC;
4527 #if defined(O_SYNC)
4528 if (flags & O_SYNC) {
4529 ret |= SMB_O_SYNC;
4531 #endif /* O_SYNC */
4532 if (flags & O_APPEND) {
4533 ret |= SMB_O_APPEND;
4535 #if defined(O_DIRECT)
4536 if (flags & O_DIRECT) {
4537 ret |= SMB_O_DIRECT;
4539 #endif
4540 #if defined(O_DIRECTORY)
4541 if (flags & O_DIRECTORY) {
4542 ret |= SMB_O_DIRECTORY;
4544 #endif
4545 return ret;
4548 /****************************************************************************
4549 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4550 ****************************************************************************/
4552 struct posix_open_state {
4553 uint16_t setup;
4554 uint8_t *param;
4555 uint8_t data[18];
4556 uint16_t fnum; /* Out */
4559 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4561 struct tevent_req *req = tevent_req_callback_data(
4562 subreq, struct tevent_req);
4563 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4564 NTSTATUS status;
4565 uint8_t *data;
4566 uint32_t num_data;
4568 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4569 NULL, 0, NULL, &data, 12, &num_data);
4570 TALLOC_FREE(subreq);
4571 if (tevent_req_nterror(req, status)) {
4572 return;
4574 state->fnum = SVAL(data,2);
4575 tevent_req_done(req);
4578 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4579 struct tevent_context *ev,
4580 struct cli_state *cli,
4581 const char *fname,
4582 int flags,
4583 mode_t mode,
4584 bool is_dir)
4586 struct tevent_req *req = NULL, *subreq = NULL;
4587 struct posix_open_state *state = NULL;
4588 uint32_t wire_flags = open_flags_to_wire(flags);
4590 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4591 if (req == NULL) {
4592 return NULL;
4595 /* Setup setup word. */
4596 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4598 /* Setup param array. */
4599 state->param = talloc_array(state, uint8_t, 6);
4600 if (tevent_req_nomem(state->param, req)) {
4601 return tevent_req_post(req, ev);
4603 memset(state->param, '\0', 6);
4604 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4606 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4607 strlen(fname)+1, NULL);
4609 if (tevent_req_nomem(state->param, req)) {
4610 return tevent_req_post(req, ev);
4613 /* Setup data words. */
4614 if (is_dir) {
4615 wire_flags |= SMB_O_DIRECTORY;
4618 SIVAL(state->data,0,0); /* No oplock. */
4619 SIVAL(state->data,4,wire_flags);
4620 SIVAL(state->data,8,unix_perms_to_wire(mode));
4621 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4622 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4624 subreq = cli_trans_send(state, /* mem ctx. */
4625 ev, /* event ctx. */
4626 cli, /* cli_state. */
4627 SMBtrans2, /* cmd. */
4628 NULL, /* pipe name. */
4629 -1, /* fid. */
4630 0, /* function. */
4631 0, /* flags. */
4632 &state->setup, /* setup. */
4633 1, /* num setup uint16_t words. */
4634 0, /* max returned setup. */
4635 state->param, /* param. */
4636 talloc_get_size(state->param),/* num param. */
4637 2, /* max returned param. */
4638 state->data, /* data. */
4639 18, /* num data. */
4640 12); /* max returned data. */
4642 if (tevent_req_nomem(subreq, req)) {
4643 return tevent_req_post(req, ev);
4645 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4646 return req;
4649 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4650 struct tevent_context *ev,
4651 struct cli_state *cli,
4652 const char *fname,
4653 int flags,
4654 mode_t mode)
4656 return cli_posix_open_internal_send(mem_ctx, ev,
4657 cli, fname, flags, mode, false);
4660 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4662 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4663 NTSTATUS status;
4665 if (tevent_req_is_nterror(req, &status)) {
4666 return status;
4668 *pfnum = state->fnum;
4669 return NT_STATUS_OK;
4672 /****************************************************************************
4673 Open - POSIX semantics. Doesn't request oplock.
4674 ****************************************************************************/
4676 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4677 int flags, mode_t mode, uint16_t *pfnum)
4680 TALLOC_CTX *frame = talloc_stackframe();
4681 struct tevent_context *ev = NULL;
4682 struct tevent_req *req = NULL;
4683 NTSTATUS status = NT_STATUS_OK;
4685 if (smbXcli_conn_has_async_calls(cli->conn)) {
4687 * Can't use sync call while an async call is in flight
4689 status = NT_STATUS_INVALID_PARAMETER;
4690 goto fail;
4693 ev = samba_tevent_context_init(frame);
4694 if (ev == NULL) {
4695 status = NT_STATUS_NO_MEMORY;
4696 goto fail;
4699 req = cli_posix_open_send(frame,
4701 cli,
4702 fname,
4703 flags,
4704 mode);
4705 if (req == NULL) {
4706 status = NT_STATUS_NO_MEMORY;
4707 goto fail;
4710 if (!tevent_req_poll(req, ev)) {
4711 status = map_nt_error_from_unix(errno);
4712 goto fail;
4715 status = cli_posix_open_recv(req, pfnum);
4717 fail:
4718 TALLOC_FREE(frame);
4719 return status;
4722 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4723 struct tevent_context *ev,
4724 struct cli_state *cli,
4725 const char *fname,
4726 mode_t mode)
4728 return cli_posix_open_internal_send(mem_ctx, ev,
4729 cli, fname, O_CREAT, mode, true);
4732 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4734 return tevent_req_simple_recv_ntstatus(req);
4737 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4739 TALLOC_CTX *frame = talloc_stackframe();
4740 struct tevent_context *ev = NULL;
4741 struct tevent_req *req = NULL;
4742 NTSTATUS status = NT_STATUS_OK;
4744 if (smbXcli_conn_has_async_calls(cli->conn)) {
4746 * Can't use sync call while an async call is in flight
4748 status = NT_STATUS_INVALID_PARAMETER;
4749 goto fail;
4752 ev = samba_tevent_context_init(frame);
4753 if (ev == NULL) {
4754 status = NT_STATUS_NO_MEMORY;
4755 goto fail;
4758 req = cli_posix_mkdir_send(frame,
4760 cli,
4761 fname,
4762 mode);
4763 if (req == NULL) {
4764 status = NT_STATUS_NO_MEMORY;
4765 goto fail;
4768 if (!tevent_req_poll(req, ev)) {
4769 status = map_nt_error_from_unix(errno);
4770 goto fail;
4773 status = cli_posix_mkdir_recv(req);
4775 fail:
4776 TALLOC_FREE(frame);
4777 return status;
4780 /****************************************************************************
4781 unlink or rmdir - POSIX semantics.
4782 ****************************************************************************/
4784 struct cli_posix_unlink_internal_state {
4785 uint8_t data[2];
4788 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4790 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4791 struct tevent_context *ev,
4792 struct cli_state *cli,
4793 const char *fname,
4794 uint16_t level)
4796 struct tevent_req *req = NULL, *subreq = NULL;
4797 struct cli_posix_unlink_internal_state *state = NULL;
4799 req = tevent_req_create(mem_ctx, &state,
4800 struct cli_posix_unlink_internal_state);
4801 if (req == NULL) {
4802 return NULL;
4805 /* Setup data word. */
4806 SSVAL(state->data, 0, level);
4808 subreq = cli_setpathinfo_send(state, ev, cli,
4809 SMB_POSIX_PATH_UNLINK,
4810 fname,
4811 state->data, sizeof(state->data));
4812 if (tevent_req_nomem(subreq, req)) {
4813 return tevent_req_post(req, ev);
4815 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4816 return req;
4819 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4821 NTSTATUS status = cli_setpathinfo_recv(subreq);
4822 tevent_req_simple_finish_ntstatus(subreq, status);
4825 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4826 struct tevent_context *ev,
4827 struct cli_state *cli,
4828 const char *fname)
4830 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4831 SMB_POSIX_UNLINK_FILE_TARGET);
4834 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4836 return tevent_req_simple_recv_ntstatus(req);
4839 /****************************************************************************
4840 unlink - POSIX semantics.
4841 ****************************************************************************/
4843 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4845 TALLOC_CTX *frame = talloc_stackframe();
4846 struct tevent_context *ev = NULL;
4847 struct tevent_req *req = NULL;
4848 NTSTATUS status = NT_STATUS_OK;
4850 if (smbXcli_conn_has_async_calls(cli->conn)) {
4852 * Can't use sync call while an async call is in flight
4854 status = NT_STATUS_INVALID_PARAMETER;
4855 goto fail;
4858 ev = samba_tevent_context_init(frame);
4859 if (ev == NULL) {
4860 status = NT_STATUS_NO_MEMORY;
4861 goto fail;
4864 req = cli_posix_unlink_send(frame,
4866 cli,
4867 fname);
4868 if (req == NULL) {
4869 status = NT_STATUS_NO_MEMORY;
4870 goto fail;
4873 if (!tevent_req_poll(req, ev)) {
4874 status = map_nt_error_from_unix(errno);
4875 goto fail;
4878 status = cli_posix_unlink_recv(req);
4880 fail:
4881 TALLOC_FREE(frame);
4882 return status;
4885 /****************************************************************************
4886 rmdir - POSIX semantics.
4887 ****************************************************************************/
4889 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4890 struct tevent_context *ev,
4891 struct cli_state *cli,
4892 const char *fname)
4894 return cli_posix_unlink_internal_send(
4895 mem_ctx, ev, cli, fname,
4896 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4899 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4901 return tevent_req_simple_recv_ntstatus(req);
4904 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4906 TALLOC_CTX *frame = talloc_stackframe();
4907 struct tevent_context *ev = NULL;
4908 struct tevent_req *req = NULL;
4909 NTSTATUS status = NT_STATUS_OK;
4911 if (smbXcli_conn_has_async_calls(cli->conn)) {
4913 * Can't use sync call while an async call is in flight
4915 status = NT_STATUS_INVALID_PARAMETER;
4916 goto fail;
4919 ev = samba_tevent_context_init(frame);
4920 if (ev == NULL) {
4921 status = NT_STATUS_NO_MEMORY;
4922 goto fail;
4925 req = cli_posix_rmdir_send(frame,
4927 cli,
4928 fname);
4929 if (req == NULL) {
4930 status = NT_STATUS_NO_MEMORY;
4931 goto fail;
4934 if (!tevent_req_poll(req, ev)) {
4935 status = map_nt_error_from_unix(errno);
4936 goto fail;
4939 status = cli_posix_rmdir_recv(req, frame);
4941 fail:
4942 TALLOC_FREE(frame);
4943 return status;
4946 /****************************************************************************
4947 filechangenotify
4948 ****************************************************************************/
4950 struct cli_notify_state {
4951 uint8_t setup[8];
4952 uint32_t num_changes;
4953 struct notify_change *changes;
4956 static void cli_notify_done(struct tevent_req *subreq);
4958 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4959 struct tevent_context *ev,
4960 struct cli_state *cli, uint16_t fnum,
4961 uint32_t buffer_size,
4962 uint32_t completion_filter, bool recursive)
4964 struct tevent_req *req, *subreq;
4965 struct cli_notify_state *state;
4966 unsigned old_timeout;
4968 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4969 if (req == NULL) {
4970 return NULL;
4973 SIVAL(state->setup, 0, completion_filter);
4974 SSVAL(state->setup, 4, fnum);
4975 SSVAL(state->setup, 6, recursive);
4978 * Notifies should not time out
4980 old_timeout = cli_set_timeout(cli, 0);
4982 subreq = cli_trans_send(
4983 state, /* mem ctx. */
4984 ev, /* event ctx. */
4985 cli, /* cli_state. */
4986 SMBnttrans, /* cmd. */
4987 NULL, /* pipe name. */
4988 -1, /* fid. */
4989 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4990 0, /* flags. */
4991 (uint16_t *)state->setup, /* setup. */
4992 4, /* num setup uint16_t words. */
4993 0, /* max returned setup. */
4994 NULL, /* param. */
4995 0, /* num param. */
4996 buffer_size, /* max returned param. */
4997 NULL, /* data. */
4998 0, /* num data. */
4999 0); /* max returned data. */
5001 cli_set_timeout(cli, old_timeout);
5003 if (tevent_req_nomem(subreq, req)) {
5004 return tevent_req_post(req, ev);
5006 tevent_req_set_callback(subreq, cli_notify_done, req);
5007 return req;
5010 static void cli_notify_done(struct tevent_req *subreq)
5012 struct tevent_req *req = tevent_req_callback_data(
5013 subreq, struct tevent_req);
5014 struct cli_notify_state *state = tevent_req_data(
5015 req, struct cli_notify_state);
5016 NTSTATUS status;
5017 uint8_t *params;
5018 uint32_t i, ofs, num_params;
5019 uint16_t flags2;
5021 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5022 &params, 0, &num_params, NULL, 0, NULL);
5023 TALLOC_FREE(subreq);
5024 if (tevent_req_nterror(req, status)) {
5025 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5026 return;
5029 state->num_changes = 0;
5030 ofs = 0;
5032 while (num_params - ofs > 12) {
5033 uint32_t next = IVAL(params, ofs);
5034 state->num_changes += 1;
5036 if ((next == 0) || (ofs+next >= num_params)) {
5037 break;
5039 ofs += next;
5042 state->changes = talloc_array(state, struct notify_change,
5043 state->num_changes);
5044 if (tevent_req_nomem(state->changes, req)) {
5045 TALLOC_FREE(params);
5046 return;
5049 ofs = 0;
5051 for (i=0; i<state->num_changes; i++) {
5052 uint32_t next = IVAL(params, ofs);
5053 uint32_t len = IVAL(params, ofs+8);
5054 ssize_t ret;
5055 char *name;
5057 if (trans_oob(num_params, ofs + 12, len)) {
5058 TALLOC_FREE(params);
5059 tevent_req_nterror(
5060 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5061 return;
5064 state->changes[i].action = IVAL(params, ofs+4);
5065 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5066 &name, params+ofs+12, len,
5067 STR_TERMINATE|STR_UNICODE);
5068 if (ret == -1) {
5069 TALLOC_FREE(params);
5070 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5071 return;
5073 state->changes[i].name = name;
5074 ofs += next;
5077 TALLOC_FREE(params);
5078 tevent_req_done(req);
5081 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5082 uint32_t *pnum_changes,
5083 struct notify_change **pchanges)
5085 struct cli_notify_state *state = tevent_req_data(
5086 req, struct cli_notify_state);
5087 NTSTATUS status;
5089 if (tevent_req_is_nterror(req, &status)) {
5090 return status;
5093 *pnum_changes = state->num_changes;
5094 *pchanges = talloc_move(mem_ctx, &state->changes);
5095 return NT_STATUS_OK;
5098 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5099 uint32_t completion_filter, bool recursive,
5100 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5101 struct notify_change **pchanges)
5103 TALLOC_CTX *frame = talloc_stackframe();
5104 struct tevent_context *ev;
5105 struct tevent_req *req;
5106 NTSTATUS status = NT_STATUS_NO_MEMORY;
5108 if (smbXcli_conn_has_async_calls(cli->conn)) {
5110 * Can't use sync call while an async call is in flight
5112 status = NT_STATUS_INVALID_PARAMETER;
5113 goto fail;
5115 ev = samba_tevent_context_init(frame);
5116 if (ev == NULL) {
5117 goto fail;
5119 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5120 completion_filter, recursive);
5121 if (req == NULL) {
5122 goto fail;
5124 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5125 goto fail;
5127 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5128 fail:
5129 TALLOC_FREE(frame);
5130 return status;
5133 struct cli_qpathinfo_state {
5134 uint8_t *param;
5135 uint8_t *data;
5136 uint16_t setup[1];
5137 uint32_t min_rdata;
5138 uint8_t *rdata;
5139 uint32_t num_rdata;
5142 static void cli_qpathinfo_done(struct tevent_req *subreq);
5144 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5145 struct tevent_context *ev,
5146 struct cli_state *cli, const char *fname,
5147 uint16_t level, uint32_t min_rdata,
5148 uint32_t max_rdata)
5150 struct tevent_req *req, *subreq;
5151 struct cli_qpathinfo_state *state;
5153 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5154 if (req == NULL) {
5155 return NULL;
5157 state->min_rdata = min_rdata;
5158 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5160 state->param = talloc_zero_array(state, uint8_t, 6);
5161 if (tevent_req_nomem(state->param, req)) {
5162 return tevent_req_post(req, ev);
5164 SSVAL(state->param, 0, level);
5165 state->param = trans2_bytes_push_str(
5166 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5167 if (tevent_req_nomem(state->param, req)) {
5168 return tevent_req_post(req, ev);
5171 subreq = cli_trans_send(
5172 state, /* mem ctx. */
5173 ev, /* event ctx. */
5174 cli, /* cli_state. */
5175 SMBtrans2, /* cmd. */
5176 NULL, /* pipe name. */
5177 -1, /* fid. */
5178 0, /* function. */
5179 0, /* flags. */
5180 state->setup, /* setup. */
5181 1, /* num setup uint16_t words. */
5182 0, /* max returned setup. */
5183 state->param, /* param. */
5184 talloc_get_size(state->param), /* num param. */
5185 2, /* max returned param. */
5186 NULL, /* data. */
5187 0, /* num data. */
5188 max_rdata); /* max returned data. */
5190 if (tevent_req_nomem(subreq, req)) {
5191 return tevent_req_post(req, ev);
5193 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5194 return req;
5197 static void cli_qpathinfo_done(struct tevent_req *subreq)
5199 struct tevent_req *req = tevent_req_callback_data(
5200 subreq, struct tevent_req);
5201 struct cli_qpathinfo_state *state = tevent_req_data(
5202 req, struct cli_qpathinfo_state);
5203 NTSTATUS status;
5205 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5206 NULL, 0, NULL,
5207 &state->rdata, state->min_rdata,
5208 &state->num_rdata);
5209 if (tevent_req_nterror(req, status)) {
5210 return;
5212 tevent_req_done(req);
5215 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5216 uint8_t **rdata, uint32_t *num_rdata)
5218 struct cli_qpathinfo_state *state = tevent_req_data(
5219 req, struct cli_qpathinfo_state);
5220 NTSTATUS status;
5222 if (tevent_req_is_nterror(req, &status)) {
5223 return status;
5225 if (rdata != NULL) {
5226 *rdata = talloc_move(mem_ctx, &state->rdata);
5227 } else {
5228 TALLOC_FREE(state->rdata);
5230 if (num_rdata != NULL) {
5231 *num_rdata = state->num_rdata;
5233 return NT_STATUS_OK;
5236 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5237 const char *fname, uint16_t level, uint32_t min_rdata,
5238 uint32_t max_rdata,
5239 uint8_t **rdata, uint32_t *num_rdata)
5241 TALLOC_CTX *frame = talloc_stackframe();
5242 struct tevent_context *ev;
5243 struct tevent_req *req;
5244 NTSTATUS status = NT_STATUS_NO_MEMORY;
5246 if (smbXcli_conn_has_async_calls(cli->conn)) {
5248 * Can't use sync call while an async call is in flight
5250 status = NT_STATUS_INVALID_PARAMETER;
5251 goto fail;
5253 ev = samba_tevent_context_init(frame);
5254 if (ev == NULL) {
5255 goto fail;
5257 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5258 max_rdata);
5259 if (req == NULL) {
5260 goto fail;
5262 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5263 goto fail;
5265 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5266 fail:
5267 TALLOC_FREE(frame);
5268 return status;
5271 struct cli_qfileinfo_state {
5272 uint16_t setup[1];
5273 uint8_t param[4];
5274 uint8_t *data;
5275 uint16_t recv_flags2;
5276 uint32_t min_rdata;
5277 uint8_t *rdata;
5278 uint32_t num_rdata;
5281 static void cli_qfileinfo_done(struct tevent_req *subreq);
5283 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5284 struct tevent_context *ev,
5285 struct cli_state *cli, uint16_t fnum,
5286 uint16_t level, uint32_t min_rdata,
5287 uint32_t max_rdata)
5289 struct tevent_req *req, *subreq;
5290 struct cli_qfileinfo_state *state;
5292 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5293 if (req == NULL) {
5294 return NULL;
5296 state->min_rdata = min_rdata;
5297 SSVAL(state->param, 0, fnum);
5298 SSVAL(state->param, 2, level);
5299 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5301 subreq = cli_trans_send(
5302 state, /* mem ctx. */
5303 ev, /* event ctx. */
5304 cli, /* cli_state. */
5305 SMBtrans2, /* cmd. */
5306 NULL, /* pipe name. */
5307 -1, /* fid. */
5308 0, /* function. */
5309 0, /* flags. */
5310 state->setup, /* setup. */
5311 1, /* num setup uint16_t words. */
5312 0, /* max returned setup. */
5313 state->param, /* param. */
5314 sizeof(state->param), /* num param. */
5315 2, /* max returned param. */
5316 NULL, /* data. */
5317 0, /* num data. */
5318 max_rdata); /* max returned data. */
5320 if (tevent_req_nomem(subreq, req)) {
5321 return tevent_req_post(req, ev);
5323 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5324 return req;
5327 static void cli_qfileinfo_done(struct tevent_req *subreq)
5329 struct tevent_req *req = tevent_req_callback_data(
5330 subreq, struct tevent_req);
5331 struct cli_qfileinfo_state *state = tevent_req_data(
5332 req, struct cli_qfileinfo_state);
5333 NTSTATUS status;
5335 status = cli_trans_recv(subreq, state,
5336 &state->recv_flags2,
5337 NULL, 0, NULL,
5338 NULL, 0, NULL,
5339 &state->rdata, state->min_rdata,
5340 &state->num_rdata);
5341 if (tevent_req_nterror(req, status)) {
5342 return;
5344 tevent_req_done(req);
5347 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5348 uint16_t *recv_flags2,
5349 uint8_t **rdata, uint32_t *num_rdata)
5351 struct cli_qfileinfo_state *state = tevent_req_data(
5352 req, struct cli_qfileinfo_state);
5353 NTSTATUS status;
5355 if (tevent_req_is_nterror(req, &status)) {
5356 return status;
5359 if (recv_flags2 != NULL) {
5360 *recv_flags2 = state->recv_flags2;
5362 if (rdata != NULL) {
5363 *rdata = talloc_move(mem_ctx, &state->rdata);
5364 } else {
5365 TALLOC_FREE(state->rdata);
5367 if (num_rdata != NULL) {
5368 *num_rdata = state->num_rdata;
5370 return NT_STATUS_OK;
5373 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5374 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5375 uint32_t max_rdata, uint16_t *recv_flags2,
5376 uint8_t **rdata, uint32_t *num_rdata)
5378 TALLOC_CTX *frame = talloc_stackframe();
5379 struct tevent_context *ev;
5380 struct tevent_req *req;
5381 NTSTATUS status = NT_STATUS_NO_MEMORY;
5383 if (smbXcli_conn_has_async_calls(cli->conn)) {
5385 * Can't use sync call while an async call is in flight
5387 status = NT_STATUS_INVALID_PARAMETER;
5388 goto fail;
5390 ev = samba_tevent_context_init(frame);
5391 if (ev == NULL) {
5392 goto fail;
5394 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5395 max_rdata);
5396 if (req == NULL) {
5397 goto fail;
5399 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5400 goto fail;
5402 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5403 fail:
5404 TALLOC_FREE(frame);
5405 return status;
5408 struct cli_flush_state {
5409 uint16_t vwv[1];
5412 static void cli_flush_done(struct tevent_req *subreq);
5414 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5415 struct tevent_context *ev,
5416 struct cli_state *cli,
5417 uint16_t fnum)
5419 struct tevent_req *req, *subreq;
5420 struct cli_flush_state *state;
5422 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5423 if (req == NULL) {
5424 return NULL;
5426 SSVAL(state->vwv + 0, 0, fnum);
5428 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5429 0, NULL);
5430 if (tevent_req_nomem(subreq, req)) {
5431 return tevent_req_post(req, ev);
5433 tevent_req_set_callback(subreq, cli_flush_done, req);
5434 return req;
5437 static void cli_flush_done(struct tevent_req *subreq)
5439 struct tevent_req *req = tevent_req_callback_data(
5440 subreq, struct tevent_req);
5441 NTSTATUS status;
5443 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5444 TALLOC_FREE(subreq);
5445 if (tevent_req_nterror(req, status)) {
5446 return;
5448 tevent_req_done(req);
5451 NTSTATUS cli_flush_recv(struct tevent_req *req)
5453 return tevent_req_simple_recv_ntstatus(req);
5456 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5458 TALLOC_CTX *frame = talloc_stackframe();
5459 struct tevent_context *ev;
5460 struct tevent_req *req;
5461 NTSTATUS status = NT_STATUS_NO_MEMORY;
5463 if (smbXcli_conn_has_async_calls(cli->conn)) {
5465 * Can't use sync call while an async call is in flight
5467 status = NT_STATUS_INVALID_PARAMETER;
5468 goto fail;
5470 ev = samba_tevent_context_init(frame);
5471 if (ev == NULL) {
5472 goto fail;
5474 req = cli_flush_send(frame, ev, cli, fnum);
5475 if (req == NULL) {
5476 goto fail;
5478 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5479 goto fail;
5481 status = cli_flush_recv(req);
5482 fail:
5483 TALLOC_FREE(frame);
5484 return status;
5487 struct cli_shadow_copy_data_state {
5488 uint16_t setup[4];
5489 uint8_t *data;
5490 uint32_t num_data;
5491 bool get_names;
5494 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5496 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5497 struct tevent_context *ev,
5498 struct cli_state *cli,
5499 uint16_t fnum,
5500 bool get_names)
5502 struct tevent_req *req, *subreq;
5503 struct cli_shadow_copy_data_state *state;
5504 uint32_t ret_size;
5506 req = tevent_req_create(mem_ctx, &state,
5507 struct cli_shadow_copy_data_state);
5508 if (req == NULL) {
5509 return NULL;
5511 state->get_names = get_names;
5512 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5514 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5515 SSVAL(state->setup + 2, 0, fnum);
5516 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5517 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5519 subreq = cli_trans_send(
5520 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5521 state->setup, ARRAY_SIZE(state->setup), 0,
5522 NULL, 0, 0,
5523 NULL, 0, ret_size);
5524 if (tevent_req_nomem(subreq, req)) {
5525 return tevent_req_post(req, ev);
5527 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5528 return req;
5531 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5533 struct tevent_req *req = tevent_req_callback_data(
5534 subreq, struct tevent_req);
5535 struct cli_shadow_copy_data_state *state = tevent_req_data(
5536 req, struct cli_shadow_copy_data_state);
5537 NTSTATUS status;
5539 status = cli_trans_recv(subreq, state, NULL,
5540 NULL, 0, NULL, /* setup */
5541 NULL, 0, NULL, /* param */
5542 &state->data, 12, &state->num_data);
5543 TALLOC_FREE(subreq);
5544 if (tevent_req_nterror(req, status)) {
5545 return;
5547 tevent_req_done(req);
5550 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5551 char ***pnames, int *pnum_names)
5553 struct cli_shadow_copy_data_state *state = tevent_req_data(
5554 req, struct cli_shadow_copy_data_state);
5555 char **names;
5556 int i, num_names;
5557 uint32_t dlength;
5558 NTSTATUS status;
5560 if (tevent_req_is_nterror(req, &status)) {
5561 return status;
5563 num_names = IVAL(state->data, 4);
5564 dlength = IVAL(state->data, 8);
5566 if (!state->get_names) {
5567 *pnum_names = num_names;
5568 return NT_STATUS_OK;
5571 if (dlength+12 > state->num_data) {
5572 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5574 names = talloc_array(mem_ctx, char *, num_names);
5575 if (names == NULL) {
5576 return NT_STATUS_NO_MEMORY;
5579 for (i=0; i<num_names; i++) {
5580 bool ret;
5581 uint8_t *src;
5582 size_t converted_size;
5584 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5585 ret = convert_string_talloc(
5586 names, CH_UTF16LE, CH_UNIX,
5587 src, 2 * sizeof(SHADOW_COPY_LABEL),
5588 &names[i], &converted_size);
5589 if (!ret) {
5590 TALLOC_FREE(names);
5591 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5594 *pnum_names = num_names;
5595 *pnames = names;
5596 return NT_STATUS_OK;
5599 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5600 uint16_t fnum, bool get_names,
5601 char ***pnames, int *pnum_names)
5603 TALLOC_CTX *frame = talloc_stackframe();
5604 struct tevent_context *ev;
5605 struct tevent_req *req;
5606 NTSTATUS status = NT_STATUS_NO_MEMORY;
5608 if (smbXcli_conn_has_async_calls(cli->conn)) {
5610 * Can't use sync call while an async call is in flight
5612 status = NT_STATUS_INVALID_PARAMETER;
5613 goto fail;
5615 ev = samba_tevent_context_init(frame);
5616 if (ev == NULL) {
5617 goto fail;
5619 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5620 if (req == NULL) {
5621 goto fail;
5623 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5624 goto fail;
5626 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5627 fail:
5628 TALLOC_FREE(frame);
5629 return status;