s3:libsmb: Plumb cli_smb2_rename() inside cli_rename().
[Samba.git] / source3 / libsmb / clifile.c
blobbfc398494a07643d90d50b8607246aaf0d36ce3b
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 = talloc_stackframe();
1419 struct tevent_context *ev;
1420 struct tevent_req *req;
1421 NTSTATUS status = NT_STATUS_OK;
1423 if (smbXcli_conn_has_async_calls(cli->conn)) {
1425 * Can't use sync call while an async call is in flight
1427 status = NT_STATUS_INVALID_PARAMETER;
1428 goto fail;
1431 ev = samba_tevent_context_init(frame);
1432 if (ev == NULL) {
1433 status = NT_STATUS_NO_MEMORY;
1434 goto fail;
1437 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1438 if (req == NULL) {
1439 status = NT_STATUS_NO_MEMORY;
1440 goto fail;
1443 if (!tevent_req_poll(req, ev)) {
1444 status = map_nt_error_from_unix(errno);
1445 goto fail;
1448 status = cli_unlink_recv(req);
1450 fail:
1451 TALLOC_FREE(frame);
1452 return status;
1455 /****************************************************************************
1456 Create a directory.
1457 ****************************************************************************/
1459 static void cli_mkdir_done(struct tevent_req *subreq);
1461 struct cli_mkdir_state {
1462 int dummy;
1465 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1466 struct tevent_context *ev,
1467 struct cli_state *cli,
1468 const char *dname)
1470 struct tevent_req *req = NULL, *subreq = NULL;
1471 struct cli_mkdir_state *state = NULL;
1472 uint8_t additional_flags = 0;
1473 uint8_t *bytes = NULL;
1475 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1476 if (req == NULL) {
1477 return NULL;
1480 bytes = talloc_array(state, uint8_t, 1);
1481 if (tevent_req_nomem(bytes, req)) {
1482 return tevent_req_post(req, ev);
1484 bytes[0] = 4;
1485 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1486 strlen(dname)+1, NULL);
1488 if (tevent_req_nomem(bytes, req)) {
1489 return tevent_req_post(req, ev);
1492 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1493 0, NULL, talloc_get_size(bytes), bytes);
1494 if (tevent_req_nomem(subreq, req)) {
1495 return tevent_req_post(req, ev);
1497 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1498 return req;
1501 static void cli_mkdir_done(struct tevent_req *subreq)
1503 struct tevent_req *req = tevent_req_callback_data(
1504 subreq, struct tevent_req);
1505 NTSTATUS status;
1507 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1508 TALLOC_FREE(subreq);
1509 if (tevent_req_nterror(req, status)) {
1510 return;
1512 tevent_req_done(req);
1515 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1517 return tevent_req_simple_recv_ntstatus(req);
1520 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1522 TALLOC_CTX *frame = talloc_stackframe();
1523 struct tevent_context *ev;
1524 struct tevent_req *req;
1525 NTSTATUS status = NT_STATUS_OK;
1527 if (smbXcli_conn_has_async_calls(cli->conn)) {
1529 * Can't use sync call while an async call is in flight
1531 status = NT_STATUS_INVALID_PARAMETER;
1532 goto fail;
1535 ev = samba_tevent_context_init(frame);
1536 if (ev == NULL) {
1537 status = NT_STATUS_NO_MEMORY;
1538 goto fail;
1541 req = cli_mkdir_send(frame, ev, cli, dname);
1542 if (req == NULL) {
1543 status = NT_STATUS_NO_MEMORY;
1544 goto fail;
1547 if (!tevent_req_poll(req, ev)) {
1548 status = map_nt_error_from_unix(errno);
1549 goto fail;
1552 status = cli_mkdir_recv(req);
1554 fail:
1555 TALLOC_FREE(frame);
1556 return status;
1559 /****************************************************************************
1560 Remove a directory.
1561 ****************************************************************************/
1563 static void cli_rmdir_done(struct tevent_req *subreq);
1565 struct cli_rmdir_state {
1566 int dummy;
1569 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1570 struct tevent_context *ev,
1571 struct cli_state *cli,
1572 const char *dname)
1574 struct tevent_req *req = NULL, *subreq = NULL;
1575 struct cli_rmdir_state *state = NULL;
1576 uint8_t additional_flags = 0;
1577 uint8_t *bytes = NULL;
1579 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1580 if (req == NULL) {
1581 return NULL;
1584 bytes = talloc_array(state, uint8_t, 1);
1585 if (tevent_req_nomem(bytes, req)) {
1586 return tevent_req_post(req, ev);
1588 bytes[0] = 4;
1589 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1590 strlen(dname)+1, NULL);
1592 if (tevent_req_nomem(bytes, req)) {
1593 return tevent_req_post(req, ev);
1596 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1597 0, NULL, talloc_get_size(bytes), bytes);
1598 if (tevent_req_nomem(subreq, req)) {
1599 return tevent_req_post(req, ev);
1601 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1602 return req;
1605 static void cli_rmdir_done(struct tevent_req *subreq)
1607 struct tevent_req *req = tevent_req_callback_data(
1608 subreq, struct tevent_req);
1609 NTSTATUS status;
1611 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1612 TALLOC_FREE(subreq);
1613 if (tevent_req_nterror(req, status)) {
1614 return;
1616 tevent_req_done(req);
1619 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1621 return tevent_req_simple_recv_ntstatus(req);
1624 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1626 TALLOC_CTX *frame = talloc_stackframe();
1627 struct tevent_context *ev;
1628 struct tevent_req *req;
1629 NTSTATUS status = NT_STATUS_OK;
1631 if (smbXcli_conn_has_async_calls(cli->conn)) {
1633 * Can't use sync call while an async call is in flight
1635 status = NT_STATUS_INVALID_PARAMETER;
1636 goto fail;
1639 ev = samba_tevent_context_init(frame);
1640 if (ev == NULL) {
1641 status = NT_STATUS_NO_MEMORY;
1642 goto fail;
1645 req = cli_rmdir_send(frame, ev, cli, dname);
1646 if (req == NULL) {
1647 status = NT_STATUS_NO_MEMORY;
1648 goto fail;
1651 if (!tevent_req_poll(req, ev)) {
1652 status = map_nt_error_from_unix(errno);
1653 goto fail;
1656 status = cli_rmdir_recv(req);
1658 fail:
1659 TALLOC_FREE(frame);
1660 return status;
1663 /****************************************************************************
1664 Set or clear the delete on close flag.
1665 ****************************************************************************/
1667 struct doc_state {
1668 uint16_t setup;
1669 uint8_t param[6];
1670 uint8_t data[1];
1673 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1675 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1676 NULL, 0, NULL, NULL, 0, NULL);
1677 tevent_req_simple_finish_ntstatus(subreq, status);
1680 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1681 struct tevent_context *ev,
1682 struct cli_state *cli,
1683 uint16_t fnum,
1684 bool flag)
1686 struct tevent_req *req = NULL, *subreq = NULL;
1687 struct doc_state *state = NULL;
1689 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1690 if (req == NULL) {
1691 return NULL;
1694 /* Setup setup word. */
1695 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1697 /* Setup param array. */
1698 SSVAL(state->param,0,fnum);
1699 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1701 /* Setup data array. */
1702 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1704 subreq = cli_trans_send(state, /* mem ctx. */
1705 ev, /* event ctx. */
1706 cli, /* cli_state. */
1707 SMBtrans2, /* cmd. */
1708 NULL, /* pipe name. */
1709 -1, /* fid. */
1710 0, /* function. */
1711 0, /* flags. */
1712 &state->setup, /* setup. */
1713 1, /* num setup uint16_t words. */
1714 0, /* max returned setup. */
1715 state->param, /* param. */
1716 6, /* num param. */
1717 2, /* max returned param. */
1718 state->data, /* data. */
1719 1, /* num data. */
1720 0); /* max returned data. */
1722 if (tevent_req_nomem(subreq, req)) {
1723 return tevent_req_post(req, ev);
1725 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1726 return req;
1729 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1731 return tevent_req_simple_recv_ntstatus(req);
1734 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1736 TALLOC_CTX *frame = talloc_stackframe();
1737 struct tevent_context *ev = NULL;
1738 struct tevent_req *req = NULL;
1739 NTSTATUS status = NT_STATUS_OK;
1741 if (smbXcli_conn_has_async_calls(cli->conn)) {
1743 * Can't use sync call while an async call is in flight
1745 status = NT_STATUS_INVALID_PARAMETER;
1746 goto fail;
1749 ev = samba_tevent_context_init(frame);
1750 if (ev == NULL) {
1751 status = NT_STATUS_NO_MEMORY;
1752 goto fail;
1755 req = cli_nt_delete_on_close_send(frame,
1757 cli,
1758 fnum,
1759 flag);
1760 if (req == NULL) {
1761 status = NT_STATUS_NO_MEMORY;
1762 goto fail;
1765 if (!tevent_req_poll(req, ev)) {
1766 status = map_nt_error_from_unix(errno);
1767 goto fail;
1770 status = cli_nt_delete_on_close_recv(req);
1772 fail:
1773 TALLOC_FREE(frame);
1774 return status;
1777 struct cli_ntcreate_state {
1778 uint16_t vwv[24];
1779 uint16_t fnum;
1782 static void cli_ntcreate_done(struct tevent_req *subreq);
1784 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1785 struct tevent_context *ev,
1786 struct cli_state *cli,
1787 const char *fname,
1788 uint32_t CreatFlags,
1789 uint32_t DesiredAccess,
1790 uint32_t FileAttributes,
1791 uint32_t ShareAccess,
1792 uint32_t CreateDisposition,
1793 uint32_t CreateOptions,
1794 uint8_t SecurityFlags)
1796 struct tevent_req *req, *subreq;
1797 struct cli_ntcreate_state *state;
1798 uint16_t *vwv;
1799 uint8_t *bytes;
1800 size_t converted_len;
1802 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1803 if (req == NULL) {
1804 return NULL;
1807 vwv = state->vwv;
1809 SCVAL(vwv+0, 0, 0xFF);
1810 SCVAL(vwv+0, 1, 0);
1811 SSVAL(vwv+1, 0, 0);
1812 SCVAL(vwv+2, 0, 0);
1814 if (cli->use_oplocks) {
1815 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1817 SIVAL(vwv+3, 1, CreatFlags);
1818 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1819 SIVAL(vwv+7, 1, DesiredAccess);
1820 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1821 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1822 SIVAL(vwv+13, 1, FileAttributes);
1823 SIVAL(vwv+15, 1, ShareAccess);
1824 SIVAL(vwv+17, 1, CreateDisposition);
1825 SIVAL(vwv+19, 1, CreateOptions |
1826 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1827 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1828 SCVAL(vwv+23, 1, SecurityFlags);
1830 bytes = talloc_array(state, uint8_t, 0);
1831 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1832 fname, strlen(fname)+1,
1833 &converted_len);
1835 /* sigh. this copes with broken netapp filer behaviour */
1836 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1838 if (tevent_req_nomem(bytes, req)) {
1839 return tevent_req_post(req, ev);
1842 SSVAL(vwv+2, 1, converted_len);
1844 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1845 talloc_get_size(bytes), bytes);
1846 if (tevent_req_nomem(subreq, req)) {
1847 return tevent_req_post(req, ev);
1849 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1850 return req;
1853 static void cli_ntcreate_done(struct tevent_req *subreq)
1855 struct tevent_req *req = tevent_req_callback_data(
1856 subreq, struct tevent_req);
1857 struct cli_ntcreate_state *state = tevent_req_data(
1858 req, struct cli_ntcreate_state);
1859 uint8_t wct;
1860 uint16_t *vwv;
1861 uint32_t num_bytes;
1862 uint8_t *bytes;
1863 NTSTATUS status;
1865 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv,
1866 &num_bytes, &bytes);
1867 TALLOC_FREE(subreq);
1868 if (tevent_req_nterror(req, status)) {
1869 return;
1871 state->fnum = SVAL(vwv+2, 1);
1872 tevent_req_done(req);
1875 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1877 struct cli_ntcreate_state *state = tevent_req_data(
1878 req, struct cli_ntcreate_state);
1879 NTSTATUS status;
1881 if (tevent_req_is_nterror(req, &status)) {
1882 return status;
1884 *pfnum = state->fnum;
1885 return NT_STATUS_OK;
1888 NTSTATUS cli_ntcreate(struct cli_state *cli,
1889 const char *fname,
1890 uint32_t CreatFlags,
1891 uint32_t DesiredAccess,
1892 uint32_t FileAttributes,
1893 uint32_t ShareAccess,
1894 uint32_t CreateDisposition,
1895 uint32_t CreateOptions,
1896 uint8_t SecurityFlags,
1897 uint16_t *pfid)
1899 TALLOC_CTX *frame = talloc_stackframe();
1900 struct tevent_context *ev;
1901 struct tevent_req *req;
1902 NTSTATUS status = NT_STATUS_OK;
1904 if (smbXcli_conn_has_async_calls(cli->conn)) {
1906 * Can't use sync call while an async call is in flight
1908 status = NT_STATUS_INVALID_PARAMETER;
1909 goto fail;
1912 ev = samba_tevent_context_init(frame);
1913 if (ev == NULL) {
1914 status = NT_STATUS_NO_MEMORY;
1915 goto fail;
1918 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1919 DesiredAccess, FileAttributes, ShareAccess,
1920 CreateDisposition, CreateOptions,
1921 SecurityFlags);
1922 if (req == NULL) {
1923 status = NT_STATUS_NO_MEMORY;
1924 goto fail;
1927 if (!tevent_req_poll(req, ev)) {
1928 status = map_nt_error_from_unix(errno);
1929 goto fail;
1932 status = cli_ntcreate_recv(req, pfid);
1933 fail:
1934 TALLOC_FREE(frame);
1935 return status;
1938 struct cli_nttrans_create_state {
1939 uint16_t fnum;
1942 static void cli_nttrans_create_done(struct tevent_req *subreq);
1944 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
1945 struct tevent_context *ev,
1946 struct cli_state *cli,
1947 const char *fname,
1948 uint32_t CreatFlags,
1949 uint32_t DesiredAccess,
1950 uint32_t FileAttributes,
1951 uint32_t ShareAccess,
1952 uint32_t CreateDisposition,
1953 uint32_t CreateOptions,
1954 uint8_t SecurityFlags,
1955 struct security_descriptor *secdesc,
1956 struct ea_struct *eas,
1957 int num_eas)
1959 struct tevent_req *req, *subreq;
1960 struct cli_nttrans_create_state *state;
1961 uint8_t *param;
1962 uint8_t *secdesc_buf;
1963 size_t secdesc_len;
1964 NTSTATUS status;
1965 size_t converted_len;
1967 req = tevent_req_create(mem_ctx,
1968 &state, struct cli_nttrans_create_state);
1969 if (req == NULL) {
1970 return NULL;
1973 if (secdesc != NULL) {
1974 status = marshall_sec_desc(talloc_tos(), secdesc,
1975 &secdesc_buf, &secdesc_len);
1976 if (tevent_req_nterror(req, status)) {
1977 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1978 nt_errstr(status)));
1979 return tevent_req_post(req, ev);
1981 } else {
1982 secdesc_buf = NULL;
1983 secdesc_len = 0;
1986 if (num_eas != 0) {
1988 * TODO ;-)
1990 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
1991 return tevent_req_post(req, ev);
1994 param = talloc_array(state, uint8_t, 53);
1995 if (tevent_req_nomem(param, req)) {
1996 return tevent_req_post(req, ev);
1999 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2000 fname, strlen(fname),
2001 &converted_len);
2002 if (tevent_req_nomem(param, req)) {
2003 return tevent_req_post(req, ev);
2006 SIVAL(param, 0, CreatFlags);
2007 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2008 SIVAL(param, 8, DesiredAccess);
2009 SIVAL(param, 12, 0x0); /* AllocationSize */
2010 SIVAL(param, 16, 0x0); /* AllocationSize */
2011 SIVAL(param, 20, FileAttributes);
2012 SIVAL(param, 24, ShareAccess);
2013 SIVAL(param, 28, CreateDisposition);
2014 SIVAL(param, 32, CreateOptions |
2015 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2016 SIVAL(param, 36, secdesc_len);
2017 SIVAL(param, 40, 0); /* EA length*/
2018 SIVAL(param, 44, converted_len);
2019 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2020 SCVAL(param, 52, SecurityFlags);
2022 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2023 NULL, -1, /* name, fid */
2024 NT_TRANSACT_CREATE, 0,
2025 NULL, 0, 0, /* setup */
2026 param, talloc_get_size(param), 128, /* param */
2027 secdesc_buf, secdesc_len, 0); /* data */
2028 if (tevent_req_nomem(subreq, req)) {
2029 return tevent_req_post(req, ev);
2031 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2032 return req;
2035 static void cli_nttrans_create_done(struct tevent_req *subreq)
2037 struct tevent_req *req = tevent_req_callback_data(
2038 subreq, struct tevent_req);
2039 struct cli_nttrans_create_state *state = tevent_req_data(
2040 req, struct cli_nttrans_create_state);
2041 uint8_t *param;
2042 uint32_t num_param;
2043 NTSTATUS status;
2045 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2046 NULL, 0, NULL, /* rsetup */
2047 &param, 69, &num_param,
2048 NULL, 0, NULL);
2049 if (tevent_req_nterror(req, status)) {
2050 return;
2052 state->fnum = SVAL(param, 2);
2053 TALLOC_FREE(param);
2054 tevent_req_done(req);
2057 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
2059 struct cli_nttrans_create_state *state = tevent_req_data(
2060 req, struct cli_nttrans_create_state);
2061 NTSTATUS status;
2063 if (tevent_req_is_nterror(req, &status)) {
2064 return status;
2066 *fnum = state->fnum;
2067 return NT_STATUS_OK;
2070 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2071 const char *fname,
2072 uint32_t CreatFlags,
2073 uint32_t DesiredAccess,
2074 uint32_t FileAttributes,
2075 uint32_t ShareAccess,
2076 uint32_t CreateDisposition,
2077 uint32_t CreateOptions,
2078 uint8_t SecurityFlags,
2079 struct security_descriptor *secdesc,
2080 struct ea_struct *eas,
2081 int num_eas,
2082 uint16_t *pfid)
2084 TALLOC_CTX *frame = talloc_stackframe();
2085 struct tevent_context *ev;
2086 struct tevent_req *req;
2087 NTSTATUS status = NT_STATUS_NO_MEMORY;
2089 if (smbXcli_conn_has_async_calls(cli->conn)) {
2091 * Can't use sync call while an async call is in flight
2093 status = NT_STATUS_INVALID_PARAMETER;
2094 goto fail;
2096 ev = samba_tevent_context_init(frame);
2097 if (ev == NULL) {
2098 goto fail;
2100 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2101 DesiredAccess, FileAttributes,
2102 ShareAccess, CreateDisposition,
2103 CreateOptions, SecurityFlags,
2104 secdesc, eas, num_eas);
2105 if (req == NULL) {
2106 goto fail;
2108 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2109 goto fail;
2111 status = cli_nttrans_create_recv(req, pfid);
2112 fail:
2113 TALLOC_FREE(frame);
2114 return status;
2117 /****************************************************************************
2118 Open a file
2119 WARNING: if you open with O_WRONLY then getattrE won't work!
2120 ****************************************************************************/
2122 struct cli_openx_state {
2123 const char *fname;
2124 uint16_t vwv[15];
2125 uint16_t fnum;
2126 struct iovec bytes;
2129 static void cli_openx_done(struct tevent_req *subreq);
2131 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2132 struct tevent_context *ev,
2133 struct cli_state *cli, const char *fname,
2134 int flags, int share_mode,
2135 struct tevent_req **psmbreq)
2137 struct tevent_req *req, *subreq;
2138 struct cli_openx_state *state;
2139 unsigned openfn;
2140 unsigned accessmode;
2141 uint8_t additional_flags;
2142 uint8_t *bytes;
2144 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2145 if (req == NULL) {
2146 return NULL;
2149 openfn = 0;
2150 if (flags & O_CREAT) {
2151 openfn |= (1<<4);
2153 if (!(flags & O_EXCL)) {
2154 if (flags & O_TRUNC)
2155 openfn |= (1<<1);
2156 else
2157 openfn |= (1<<0);
2160 accessmode = (share_mode<<4);
2162 if ((flags & O_ACCMODE) == O_RDWR) {
2163 accessmode |= 2;
2164 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2165 accessmode |= 1;
2168 #if defined(O_SYNC)
2169 if ((flags & O_SYNC) == O_SYNC) {
2170 accessmode |= (1<<14);
2172 #endif /* O_SYNC */
2174 if (share_mode == DENY_FCB) {
2175 accessmode = 0xFF;
2178 SCVAL(state->vwv + 0, 0, 0xFF);
2179 SCVAL(state->vwv + 0, 1, 0);
2180 SSVAL(state->vwv + 1, 0, 0);
2181 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2182 SSVAL(state->vwv + 3, 0, accessmode);
2183 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2184 SSVAL(state->vwv + 5, 0, 0);
2185 SIVAL(state->vwv + 6, 0, 0);
2186 SSVAL(state->vwv + 8, 0, openfn);
2187 SIVAL(state->vwv + 9, 0, 0);
2188 SIVAL(state->vwv + 11, 0, 0);
2189 SIVAL(state->vwv + 13, 0, 0);
2191 additional_flags = 0;
2193 if (cli->use_oplocks) {
2194 /* if using oplocks then ask for a batch oplock via
2195 core and extended methods */
2196 additional_flags =
2197 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2198 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2201 bytes = talloc_array(state, uint8_t, 0);
2202 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2203 strlen(fname)+1, NULL);
2205 if (tevent_req_nomem(bytes, req)) {
2206 return tevent_req_post(req, ev);
2209 state->bytes.iov_base = (void *)bytes;
2210 state->bytes.iov_len = talloc_get_size(bytes);
2212 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2213 15, state->vwv, 1, &state->bytes);
2214 if (subreq == NULL) {
2215 TALLOC_FREE(req);
2216 return NULL;
2218 tevent_req_set_callback(subreq, cli_openx_done, req);
2219 *psmbreq = subreq;
2220 return req;
2223 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2224 struct cli_state *cli, const char *fname,
2225 int flags, int share_mode)
2227 struct tevent_req *req, *subreq;
2228 NTSTATUS status;
2230 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2231 &subreq);
2232 if (req == NULL) {
2233 return NULL;
2236 status = smb1cli_req_chain_submit(&subreq, 1);
2237 if (tevent_req_nterror(req, status)) {
2238 return tevent_req_post(req, ev);
2240 return req;
2243 static void cli_openx_done(struct tevent_req *subreq)
2245 struct tevent_req *req = tevent_req_callback_data(
2246 subreq, struct tevent_req);
2247 struct cli_openx_state *state = tevent_req_data(
2248 req, struct cli_openx_state);
2249 uint8_t wct;
2250 uint16_t *vwv;
2251 NTSTATUS status;
2253 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2254 NULL);
2255 TALLOC_FREE(subreq);
2256 if (tevent_req_nterror(req, status)) {
2257 return;
2259 state->fnum = SVAL(vwv+2, 0);
2260 tevent_req_done(req);
2263 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2265 struct cli_openx_state *state = tevent_req_data(
2266 req, struct cli_openx_state);
2267 NTSTATUS status;
2269 if (tevent_req_is_nterror(req, &status)) {
2270 return status;
2272 *pfnum = state->fnum;
2273 return NT_STATUS_OK;
2276 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2277 int share_mode, uint16_t *pfnum)
2279 TALLOC_CTX *frame = talloc_stackframe();
2280 struct tevent_context *ev;
2281 struct tevent_req *req;
2282 NTSTATUS status = NT_STATUS_NO_MEMORY;
2284 if (smbXcli_conn_has_async_calls(cli->conn)) {
2286 * Can't use sync call while an async call is in flight
2288 status = NT_STATUS_INVALID_PARAMETER;
2289 goto fail;
2292 ev = samba_tevent_context_init(frame);
2293 if (ev == NULL) {
2294 goto fail;
2297 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2298 if (req == NULL) {
2299 goto fail;
2302 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2303 goto fail;
2306 status = cli_openx_recv(req, pfnum);
2307 fail:
2308 TALLOC_FREE(frame);
2309 return status;
2311 /****************************************************************************
2312 Synchronous wrapper function that does an NtCreateX open by preference
2313 and falls back to openX if this fails.
2314 ****************************************************************************/
2316 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2317 int share_mode_in, uint16_t *pfnum)
2319 NTSTATUS status;
2320 unsigned int openfn = 0;
2321 unsigned int dos_deny = 0;
2322 uint32_t access_mask, share_mode, create_disposition, create_options;
2324 /* Do the initial mapping into OpenX parameters. */
2325 if (flags & O_CREAT) {
2326 openfn |= (1<<4);
2328 if (!(flags & O_EXCL)) {
2329 if (flags & O_TRUNC)
2330 openfn |= (1<<1);
2331 else
2332 openfn |= (1<<0);
2335 dos_deny = (share_mode_in<<4);
2337 if ((flags & O_ACCMODE) == O_RDWR) {
2338 dos_deny |= 2;
2339 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2340 dos_deny |= 1;
2343 #if defined(O_SYNC)
2344 if ((flags & O_SYNC) == O_SYNC) {
2345 dos_deny |= (1<<14);
2347 #endif /* O_SYNC */
2349 if (share_mode_in == DENY_FCB) {
2350 dos_deny = 0xFF;
2353 #if 0
2354 /* Hmmm. This is what I think the above code
2355 should look like if it's using the constants
2356 we #define. JRA. */
2358 if (flags & O_CREAT) {
2359 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2361 if (!(flags & O_EXCL)) {
2362 if (flags & O_TRUNC)
2363 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2364 else
2365 openfn |= OPENX_FILE_EXISTS_OPEN;
2368 dos_deny = SET_DENY_MODE(share_mode_in);
2370 if ((flags & O_ACCMODE) == O_RDWR) {
2371 dos_deny |= DOS_OPEN_RDWR;
2372 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2373 dos_deny |= DOS_OPEN_WRONLY;
2376 #if defined(O_SYNC)
2377 if ((flags & O_SYNC) == O_SYNC) {
2378 dos_deny |= FILE_SYNC_OPENMODE;
2380 #endif /* O_SYNC */
2382 if (share_mode_in == DENY_FCB) {
2383 dos_deny = 0xFF;
2385 #endif
2387 if (!map_open_params_to_ntcreate(fname, dos_deny,
2388 openfn, &access_mask,
2389 &share_mode, &create_disposition,
2390 &create_options, NULL)) {
2391 goto try_openx;
2394 status = cli_ntcreate(cli,
2395 fname,
2397 access_mask,
2399 share_mode,
2400 create_disposition,
2401 create_options,
2403 pfnum);
2405 /* Try and cope will all varients of "we don't do this call"
2406 and fall back to openX. */
2408 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2409 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2410 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2411 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2412 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2413 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2414 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2415 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2416 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2417 goto try_openx;
2420 return status;
2422 try_openx:
2424 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2427 /****************************************************************************
2428 Close a file.
2429 ****************************************************************************/
2431 struct cli_close_state {
2432 uint16_t vwv[3];
2435 static void cli_close_done(struct tevent_req *subreq);
2437 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2438 struct tevent_context *ev,
2439 struct cli_state *cli,
2440 uint16_t fnum,
2441 struct tevent_req **psubreq)
2443 struct tevent_req *req, *subreq;
2444 struct cli_close_state *state;
2446 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2447 if (req == NULL) {
2448 return NULL;
2451 SSVAL(state->vwv+0, 0, fnum);
2452 SIVALS(state->vwv+1, 0, -1);
2454 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2455 0, NULL);
2456 if (subreq == NULL) {
2457 TALLOC_FREE(req);
2458 return NULL;
2460 tevent_req_set_callback(subreq, cli_close_done, req);
2461 *psubreq = subreq;
2462 return req;
2465 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2466 struct tevent_context *ev,
2467 struct cli_state *cli,
2468 uint16_t fnum)
2470 struct tevent_req *req, *subreq;
2471 NTSTATUS status;
2473 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2474 if (req == NULL) {
2475 return NULL;
2478 status = smb1cli_req_chain_submit(&subreq, 1);
2479 if (tevent_req_nterror(req, status)) {
2480 return tevent_req_post(req, ev);
2482 return req;
2485 static void cli_close_done(struct tevent_req *subreq)
2487 struct tevent_req *req = tevent_req_callback_data(
2488 subreq, struct tevent_req);
2489 NTSTATUS status;
2491 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2492 TALLOC_FREE(subreq);
2493 if (tevent_req_nterror(req, status)) {
2494 return;
2496 tevent_req_done(req);
2499 NTSTATUS cli_close_recv(struct tevent_req *req)
2501 return tevent_req_simple_recv_ntstatus(req);
2504 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2506 TALLOC_CTX *frame = talloc_stackframe();
2507 struct tevent_context *ev;
2508 struct tevent_req *req;
2509 NTSTATUS status = NT_STATUS_OK;
2511 if (smbXcli_conn_has_async_calls(cli->conn)) {
2513 * Can't use sync call while an async call is in flight
2515 status = NT_STATUS_INVALID_PARAMETER;
2516 goto fail;
2519 ev = samba_tevent_context_init(frame);
2520 if (ev == NULL) {
2521 status = NT_STATUS_NO_MEMORY;
2522 goto fail;
2525 req = cli_close_send(frame, ev, cli, fnum);
2526 if (req == NULL) {
2527 status = NT_STATUS_NO_MEMORY;
2528 goto fail;
2531 if (!tevent_req_poll(req, ev)) {
2532 status = map_nt_error_from_unix(errno);
2533 goto fail;
2536 status = cli_close_recv(req);
2537 fail:
2538 TALLOC_FREE(frame);
2539 return status;
2542 /****************************************************************************
2543 Truncate a file to a specified size
2544 ****************************************************************************/
2546 struct ftrunc_state {
2547 uint16_t setup;
2548 uint8_t param[6];
2549 uint8_t data[8];
2552 static void cli_ftruncate_done(struct tevent_req *subreq)
2554 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2555 NULL, 0, NULL, NULL, 0, NULL);
2556 tevent_req_simple_finish_ntstatus(subreq, status);
2559 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2560 struct tevent_context *ev,
2561 struct cli_state *cli,
2562 uint16_t fnum,
2563 uint64_t size)
2565 struct tevent_req *req = NULL, *subreq = NULL;
2566 struct ftrunc_state *state = NULL;
2568 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2569 if (req == NULL) {
2570 return NULL;
2573 /* Setup setup word. */
2574 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2576 /* Setup param array. */
2577 SSVAL(state->param,0,fnum);
2578 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2579 SSVAL(state->param,4,0);
2581 /* Setup data array. */
2582 SBVAL(state->data, 0, size);
2584 subreq = cli_trans_send(state, /* mem ctx. */
2585 ev, /* event ctx. */
2586 cli, /* cli_state. */
2587 SMBtrans2, /* cmd. */
2588 NULL, /* pipe name. */
2589 -1, /* fid. */
2590 0, /* function. */
2591 0, /* flags. */
2592 &state->setup, /* setup. */
2593 1, /* num setup uint16_t words. */
2594 0, /* max returned setup. */
2595 state->param, /* param. */
2596 6, /* num param. */
2597 2, /* max returned param. */
2598 state->data, /* data. */
2599 8, /* num data. */
2600 0); /* max returned data. */
2602 if (tevent_req_nomem(subreq, req)) {
2603 return tevent_req_post(req, ev);
2605 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2606 return req;
2609 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2611 return tevent_req_simple_recv_ntstatus(req);
2614 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2616 TALLOC_CTX *frame = talloc_stackframe();
2617 struct tevent_context *ev = NULL;
2618 struct tevent_req *req = NULL;
2619 NTSTATUS status = NT_STATUS_OK;
2621 if (smbXcli_conn_has_async_calls(cli->conn)) {
2623 * Can't use sync call while an async call is in flight
2625 status = NT_STATUS_INVALID_PARAMETER;
2626 goto fail;
2629 ev = samba_tevent_context_init(frame);
2630 if (ev == NULL) {
2631 status = NT_STATUS_NO_MEMORY;
2632 goto fail;
2635 req = cli_ftruncate_send(frame,
2637 cli,
2638 fnum,
2639 size);
2640 if (req == NULL) {
2641 status = NT_STATUS_NO_MEMORY;
2642 goto fail;
2645 if (!tevent_req_poll(req, ev)) {
2646 status = map_nt_error_from_unix(errno);
2647 goto fail;
2650 status = cli_ftruncate_recv(req);
2652 fail:
2653 TALLOC_FREE(frame);
2654 return status;
2657 /****************************************************************************
2658 send a lock with a specified locktype
2659 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2660 ****************************************************************************/
2662 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2663 uint32_t offset, uint32_t len,
2664 int timeout, unsigned char locktype)
2666 uint16_t vwv[8];
2667 uint8_t bytes[10];
2668 NTSTATUS status;
2669 unsigned int set_timeout = 0;
2670 unsigned int saved_timeout = 0;
2672 SCVAL(vwv + 0, 0, 0xff);
2673 SCVAL(vwv + 0, 1, 0);
2674 SSVAL(vwv + 1, 0, 0);
2675 SSVAL(vwv + 2, 0, fnum);
2676 SCVAL(vwv + 3, 0, locktype);
2677 SCVAL(vwv + 3, 1, 0);
2678 SIVALS(vwv + 4, 0, timeout);
2679 SSVAL(vwv + 6, 0, 0);
2680 SSVAL(vwv + 7, 0, 1);
2682 SSVAL(bytes, 0, cli_getpid(cli));
2683 SIVAL(bytes, 2, offset);
2684 SIVAL(bytes, 6, len);
2686 if (timeout != 0) {
2687 if (timeout == -1) {
2688 set_timeout = 0x7FFFFFFF;
2689 } else {
2690 set_timeout = timeout + 2*1000;
2692 saved_timeout = cli_set_timeout(cli, set_timeout);
2695 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2696 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2698 if (saved_timeout != 0) {
2699 cli_set_timeout(cli, saved_timeout);
2702 return status;
2705 /****************************************************************************
2706 Lock a file.
2707 note that timeout is in units of 2 milliseconds
2708 ****************************************************************************/
2710 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2711 uint32_t offset, uint32_t len, int timeout,
2712 enum brl_type lock_type)
2714 NTSTATUS status;
2716 status = cli_locktype(cli, fnum, offset, len, timeout,
2717 (lock_type == READ_LOCK? 1 : 0));
2718 return status;
2721 /****************************************************************************
2722 Unlock a file.
2723 ****************************************************************************/
2725 struct cli_unlock_state {
2726 uint16_t vwv[8];
2727 uint8_t data[10];
2730 static void cli_unlock_done(struct tevent_req *subreq);
2732 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2733 struct tevent_context *ev,
2734 struct cli_state *cli,
2735 uint16_t fnum,
2736 uint64_t offset,
2737 uint64_t len)
2740 struct tevent_req *req = NULL, *subreq = NULL;
2741 struct cli_unlock_state *state = NULL;
2742 uint8_t additional_flags = 0;
2744 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2745 if (req == NULL) {
2746 return NULL;
2749 SCVAL(state->vwv+0, 0, 0xFF);
2750 SSVAL(state->vwv+2, 0, fnum);
2751 SCVAL(state->vwv+3, 0, 0);
2752 SIVALS(state->vwv+4, 0, 0);
2753 SSVAL(state->vwv+6, 0, 1);
2754 SSVAL(state->vwv+7, 0, 0);
2756 SSVAL(state->data, 0, cli_getpid(cli));
2757 SIVAL(state->data, 2, offset);
2758 SIVAL(state->data, 6, len);
2760 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2761 8, state->vwv, 10, state->data);
2762 if (tevent_req_nomem(subreq, req)) {
2763 return tevent_req_post(req, ev);
2765 tevent_req_set_callback(subreq, cli_unlock_done, req);
2766 return req;
2769 static void cli_unlock_done(struct tevent_req *subreq)
2771 struct tevent_req *req = tevent_req_callback_data(
2772 subreq, struct tevent_req);
2773 NTSTATUS status;
2775 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2776 TALLOC_FREE(subreq);
2777 if (tevent_req_nterror(req, status)) {
2778 return;
2780 tevent_req_done(req);
2783 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2785 return tevent_req_simple_recv_ntstatus(req);
2788 NTSTATUS cli_unlock(struct cli_state *cli,
2789 uint16_t fnum,
2790 uint32_t offset,
2791 uint32_t len)
2793 TALLOC_CTX *frame = talloc_stackframe();
2794 struct tevent_context *ev;
2795 struct tevent_req *req;
2796 NTSTATUS status = NT_STATUS_OK;
2798 if (smbXcli_conn_has_async_calls(cli->conn)) {
2800 * Can't use sync call while an async call is in flight
2802 status = NT_STATUS_INVALID_PARAMETER;
2803 goto fail;
2806 ev = samba_tevent_context_init(frame);
2807 if (ev == NULL) {
2808 status = NT_STATUS_NO_MEMORY;
2809 goto fail;
2812 req = cli_unlock_send(frame, ev, cli,
2813 fnum, offset, len);
2814 if (req == NULL) {
2815 status = NT_STATUS_NO_MEMORY;
2816 goto fail;
2819 if (!tevent_req_poll(req, ev)) {
2820 status = map_nt_error_from_unix(errno);
2821 goto fail;
2824 status = cli_unlock_recv(req);
2826 fail:
2827 TALLOC_FREE(frame);
2828 return status;
2831 /****************************************************************************
2832 Lock a file with 64 bit offsets.
2833 ****************************************************************************/
2835 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
2836 uint64_t offset, uint64_t len, int timeout,
2837 enum brl_type lock_type)
2839 uint16_t vwv[8];
2840 uint8_t bytes[20];
2841 unsigned int set_timeout = 0;
2842 unsigned int saved_timeout = 0;
2843 int ltype;
2844 NTSTATUS status;
2846 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
2847 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
2850 ltype = (lock_type == READ_LOCK? 1 : 0);
2851 ltype |= LOCKING_ANDX_LARGE_FILES;
2853 SCVAL(vwv + 0, 0, 0xff);
2854 SCVAL(vwv + 0, 1, 0);
2855 SSVAL(vwv + 1, 0, 0);
2856 SSVAL(vwv + 2, 0, fnum);
2857 SCVAL(vwv + 3, 0, ltype);
2858 SCVAL(vwv + 3, 1, 0);
2859 SIVALS(vwv + 4, 0, timeout);
2860 SSVAL(vwv + 6, 0, 0);
2861 SSVAL(vwv + 7, 0, 1);
2863 SIVAL(bytes, 0, cli_getpid(cli));
2864 SOFF_T_R(bytes, 4, offset);
2865 SOFF_T_R(bytes, 12, len);
2867 if (timeout != 0) {
2868 if (timeout == -1) {
2869 set_timeout = 0x7FFFFFFF;
2870 } else {
2871 set_timeout = timeout + 2*1000;
2873 saved_timeout = cli_set_timeout(cli, set_timeout);
2876 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2877 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2879 if (saved_timeout != 0) {
2880 cli_set_timeout(cli, saved_timeout);
2883 return status;
2886 /****************************************************************************
2887 Unlock a file with 64 bit offsets.
2888 ****************************************************************************/
2890 struct cli_unlock64_state {
2891 uint16_t vwv[8];
2892 uint8_t data[20];
2895 static void cli_unlock64_done(struct tevent_req *subreq);
2897 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2898 struct tevent_context *ev,
2899 struct cli_state *cli,
2900 uint16_t fnum,
2901 uint64_t offset,
2902 uint64_t len)
2905 struct tevent_req *req = NULL, *subreq = NULL;
2906 struct cli_unlock64_state *state = NULL;
2907 uint8_t additional_flags = 0;
2909 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2910 if (req == NULL) {
2911 return NULL;
2914 SCVAL(state->vwv+0, 0, 0xff);
2915 SSVAL(state->vwv+2, 0, fnum);
2916 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2917 SIVALS(state->vwv+4, 0, 0);
2918 SSVAL(state->vwv+6, 0, 1);
2919 SSVAL(state->vwv+7, 0, 0);
2921 SIVAL(state->data, 0, cli_getpid(cli));
2922 SOFF_T_R(state->data, 4, offset);
2923 SOFF_T_R(state->data, 12, len);
2925 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2926 8, state->vwv, 20, state->data);
2927 if (tevent_req_nomem(subreq, req)) {
2928 return tevent_req_post(req, ev);
2930 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2931 return req;
2934 static void cli_unlock64_done(struct tevent_req *subreq)
2936 struct tevent_req *req = tevent_req_callback_data(
2937 subreq, struct tevent_req);
2938 NTSTATUS status;
2940 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2941 TALLOC_FREE(subreq);
2942 if (tevent_req_nterror(req, status)) {
2943 return;
2945 tevent_req_done(req);
2948 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2950 return tevent_req_simple_recv_ntstatus(req);
2953 NTSTATUS cli_unlock64(struct cli_state *cli,
2954 uint16_t fnum,
2955 uint64_t offset,
2956 uint64_t len)
2958 TALLOC_CTX *frame = talloc_stackframe();
2959 struct tevent_context *ev;
2960 struct tevent_req *req;
2961 NTSTATUS status = NT_STATUS_OK;
2963 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
2964 return cli_unlock(cli, fnum, offset, len);
2967 if (smbXcli_conn_has_async_calls(cli->conn)) {
2969 * Can't use sync call while an async call is in flight
2971 status = NT_STATUS_INVALID_PARAMETER;
2972 goto fail;
2975 ev = samba_tevent_context_init(frame);
2976 if (ev == NULL) {
2977 status = NT_STATUS_NO_MEMORY;
2978 goto fail;
2981 req = cli_unlock64_send(frame, ev, cli,
2982 fnum, offset, len);
2983 if (req == NULL) {
2984 status = NT_STATUS_NO_MEMORY;
2985 goto fail;
2988 if (!tevent_req_poll(req, ev)) {
2989 status = map_nt_error_from_unix(errno);
2990 goto fail;
2993 status = cli_unlock64_recv(req);
2995 fail:
2996 TALLOC_FREE(frame);
2997 return status;
3000 /****************************************************************************
3001 Get/unlock a POSIX lock on a file - internal function.
3002 ****************************************************************************/
3004 struct posix_lock_state {
3005 uint16_t setup;
3006 uint8_t param[4];
3007 uint8_t data[POSIX_LOCK_DATA_SIZE];
3010 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3012 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3013 NULL, 0, NULL, NULL, 0, NULL);
3014 tevent_req_simple_finish_ntstatus(subreq, status);
3017 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3018 struct tevent_context *ev,
3019 struct cli_state *cli,
3020 uint16_t fnum,
3021 uint64_t offset,
3022 uint64_t len,
3023 bool wait_lock,
3024 enum brl_type lock_type)
3026 struct tevent_req *req = NULL, *subreq = NULL;
3027 struct posix_lock_state *state = NULL;
3029 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3030 if (req == NULL) {
3031 return NULL;
3034 /* Setup setup word. */
3035 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3037 /* Setup param array. */
3038 SSVAL(&state->param, 0, fnum);
3039 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3041 /* Setup data array. */
3042 switch (lock_type) {
3043 case READ_LOCK:
3044 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3045 POSIX_LOCK_TYPE_READ);
3046 break;
3047 case WRITE_LOCK:
3048 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3049 POSIX_LOCK_TYPE_WRITE);
3050 break;
3051 case UNLOCK_LOCK:
3052 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3053 POSIX_LOCK_TYPE_UNLOCK);
3054 break;
3055 default:
3056 return NULL;
3059 if (wait_lock) {
3060 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3061 POSIX_LOCK_FLAG_WAIT);
3062 } else {
3063 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3064 POSIX_LOCK_FLAG_NOWAIT);
3067 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3068 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3069 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3071 subreq = cli_trans_send(state, /* mem ctx. */
3072 ev, /* event ctx. */
3073 cli, /* cli_state. */
3074 SMBtrans2, /* cmd. */
3075 NULL, /* pipe name. */
3076 -1, /* fid. */
3077 0, /* function. */
3078 0, /* flags. */
3079 &state->setup, /* setup. */
3080 1, /* num setup uint16_t words. */
3081 0, /* max returned setup. */
3082 state->param, /* param. */
3083 4, /* num param. */
3084 2, /* max returned param. */
3085 state->data, /* data. */
3086 POSIX_LOCK_DATA_SIZE, /* num data. */
3087 0); /* max returned data. */
3089 if (tevent_req_nomem(subreq, req)) {
3090 return tevent_req_post(req, ev);
3092 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3093 return req;
3096 /****************************************************************************
3097 POSIX Lock a file.
3098 ****************************************************************************/
3100 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3101 struct tevent_context *ev,
3102 struct cli_state *cli,
3103 uint16_t fnum,
3104 uint64_t offset,
3105 uint64_t len,
3106 bool wait_lock,
3107 enum brl_type lock_type)
3109 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3110 wait_lock, lock_type);
3113 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3115 return tevent_req_simple_recv_ntstatus(req);
3118 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3119 uint64_t offset, uint64_t len,
3120 bool wait_lock, enum brl_type lock_type)
3122 TALLOC_CTX *frame = talloc_stackframe();
3123 struct tevent_context *ev = NULL;
3124 struct tevent_req *req = NULL;
3125 NTSTATUS status = NT_STATUS_OK;
3127 if (smbXcli_conn_has_async_calls(cli->conn)) {
3129 * Can't use sync call while an async call is in flight
3131 status = NT_STATUS_INVALID_PARAMETER;
3132 goto fail;
3135 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3136 status = NT_STATUS_INVALID_PARAMETER;
3137 goto fail;
3140 ev = samba_tevent_context_init(frame);
3141 if (ev == NULL) {
3142 status = NT_STATUS_NO_MEMORY;
3143 goto fail;
3146 req = cli_posix_lock_send(frame,
3148 cli,
3149 fnum,
3150 offset,
3151 len,
3152 wait_lock,
3153 lock_type);
3154 if (req == NULL) {
3155 status = NT_STATUS_NO_MEMORY;
3156 goto fail;
3159 if (!tevent_req_poll(req, ev)) {
3160 status = map_nt_error_from_unix(errno);
3161 goto fail;
3164 status = cli_posix_lock_recv(req);
3166 fail:
3167 TALLOC_FREE(frame);
3168 return status;
3171 /****************************************************************************
3172 POSIX Unlock a file.
3173 ****************************************************************************/
3175 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3176 struct tevent_context *ev,
3177 struct cli_state *cli,
3178 uint16_t fnum,
3179 uint64_t offset,
3180 uint64_t len)
3182 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3183 false, UNLOCK_LOCK);
3186 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3188 return tevent_req_simple_recv_ntstatus(req);
3191 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3193 TALLOC_CTX *frame = talloc_stackframe();
3194 struct tevent_context *ev = NULL;
3195 struct tevent_req *req = NULL;
3196 NTSTATUS status = NT_STATUS_OK;
3198 if (smbXcli_conn_has_async_calls(cli->conn)) {
3200 * Can't use sync call while an async call is in flight
3202 status = NT_STATUS_INVALID_PARAMETER;
3203 goto fail;
3206 ev = samba_tevent_context_init(frame);
3207 if (ev == NULL) {
3208 status = NT_STATUS_NO_MEMORY;
3209 goto fail;
3212 req = cli_posix_unlock_send(frame,
3214 cli,
3215 fnum,
3216 offset,
3217 len);
3218 if (req == NULL) {
3219 status = NT_STATUS_NO_MEMORY;
3220 goto fail;
3223 if (!tevent_req_poll(req, ev)) {
3224 status = map_nt_error_from_unix(errno);
3225 goto fail;
3228 status = cli_posix_unlock_recv(req);
3230 fail:
3231 TALLOC_FREE(frame);
3232 return status;
3235 /****************************************************************************
3236 Do a SMBgetattrE call.
3237 ****************************************************************************/
3239 static void cli_getattrE_done(struct tevent_req *subreq);
3241 struct cli_getattrE_state {
3242 uint16_t vwv[1];
3243 int zone_offset;
3244 uint16_t attr;
3245 off_t size;
3246 time_t change_time;
3247 time_t access_time;
3248 time_t write_time;
3251 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3252 struct tevent_context *ev,
3253 struct cli_state *cli,
3254 uint16_t fnum)
3256 struct tevent_req *req = NULL, *subreq = NULL;
3257 struct cli_getattrE_state *state = NULL;
3258 uint8_t additional_flags = 0;
3260 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3261 if (req == NULL) {
3262 return NULL;
3265 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3266 SSVAL(state->vwv+0,0,fnum);
3268 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3269 1, state->vwv, 0, NULL);
3270 if (tevent_req_nomem(subreq, req)) {
3271 return tevent_req_post(req, ev);
3273 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3274 return req;
3277 static void cli_getattrE_done(struct tevent_req *subreq)
3279 struct tevent_req *req = tevent_req_callback_data(
3280 subreq, struct tevent_req);
3281 struct cli_getattrE_state *state = tevent_req_data(
3282 req, struct cli_getattrE_state);
3283 uint8_t wct;
3284 uint16_t *vwv = NULL;
3285 NTSTATUS status;
3287 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3288 NULL, NULL);
3289 TALLOC_FREE(subreq);
3290 if (tevent_req_nterror(req, status)) {
3291 return;
3294 state->size = (off_t)IVAL(vwv+6,0);
3295 state->attr = SVAL(vwv+10,0);
3296 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3297 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3298 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3300 tevent_req_done(req);
3303 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3304 uint16_t *attr,
3305 off_t *size,
3306 time_t *change_time,
3307 time_t *access_time,
3308 time_t *write_time)
3310 struct cli_getattrE_state *state = tevent_req_data(
3311 req, struct cli_getattrE_state);
3312 NTSTATUS status;
3314 if (tevent_req_is_nterror(req, &status)) {
3315 return status;
3317 if (attr) {
3318 *attr = state->attr;
3320 if (size) {
3321 *size = state->size;
3323 if (change_time) {
3324 *change_time = state->change_time;
3326 if (access_time) {
3327 *access_time = state->access_time;
3329 if (write_time) {
3330 *write_time = state->write_time;
3332 return NT_STATUS_OK;
3335 NTSTATUS cli_getattrE(struct cli_state *cli,
3336 uint16_t fnum,
3337 uint16_t *attr,
3338 off_t *size,
3339 time_t *change_time,
3340 time_t *access_time,
3341 time_t *write_time)
3343 TALLOC_CTX *frame = talloc_stackframe();
3344 struct tevent_context *ev = NULL;
3345 struct tevent_req *req = NULL;
3346 NTSTATUS status = NT_STATUS_OK;
3348 if (smbXcli_conn_has_async_calls(cli->conn)) {
3350 * Can't use sync call while an async call is in flight
3352 status = NT_STATUS_INVALID_PARAMETER;
3353 goto fail;
3356 ev = samba_tevent_context_init(frame);
3357 if (ev == NULL) {
3358 status = NT_STATUS_NO_MEMORY;
3359 goto fail;
3362 req = cli_getattrE_send(frame, ev, cli, fnum);
3363 if (req == NULL) {
3364 status = NT_STATUS_NO_MEMORY;
3365 goto fail;
3368 if (!tevent_req_poll(req, ev)) {
3369 status = map_nt_error_from_unix(errno);
3370 goto fail;
3373 status = cli_getattrE_recv(req,
3374 attr,
3375 size,
3376 change_time,
3377 access_time,
3378 write_time);
3380 fail:
3381 TALLOC_FREE(frame);
3382 return status;
3385 /****************************************************************************
3386 Do a SMBgetatr call
3387 ****************************************************************************/
3389 static void cli_getatr_done(struct tevent_req *subreq);
3391 struct cli_getatr_state {
3392 int zone_offset;
3393 uint16_t attr;
3394 off_t size;
3395 time_t write_time;
3398 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3399 struct tevent_context *ev,
3400 struct cli_state *cli,
3401 const char *fname)
3403 struct tevent_req *req = NULL, *subreq = NULL;
3404 struct cli_getatr_state *state = NULL;
3405 uint8_t additional_flags = 0;
3406 uint8_t *bytes = NULL;
3408 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3409 if (req == NULL) {
3410 return NULL;
3413 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3415 bytes = talloc_array(state, uint8_t, 1);
3416 if (tevent_req_nomem(bytes, req)) {
3417 return tevent_req_post(req, ev);
3419 bytes[0] = 4;
3420 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3421 strlen(fname)+1, NULL);
3423 if (tevent_req_nomem(bytes, req)) {
3424 return tevent_req_post(req, ev);
3427 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3428 0, NULL, talloc_get_size(bytes), bytes);
3429 if (tevent_req_nomem(subreq, req)) {
3430 return tevent_req_post(req, ev);
3432 tevent_req_set_callback(subreq, cli_getatr_done, req);
3433 return req;
3436 static void cli_getatr_done(struct tevent_req *subreq)
3438 struct tevent_req *req = tevent_req_callback_data(
3439 subreq, struct tevent_req);
3440 struct cli_getatr_state *state = tevent_req_data(
3441 req, struct cli_getatr_state);
3442 uint8_t wct;
3443 uint16_t *vwv = NULL;
3444 NTSTATUS status;
3446 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3447 NULL);
3448 TALLOC_FREE(subreq);
3449 if (tevent_req_nterror(req, status)) {
3450 return;
3453 state->attr = SVAL(vwv+0,0);
3454 state->size = (off_t)IVAL(vwv+3,0);
3455 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3457 tevent_req_done(req);
3460 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3461 uint16_t *attr,
3462 off_t *size,
3463 time_t *write_time)
3465 struct cli_getatr_state *state = tevent_req_data(
3466 req, struct cli_getatr_state);
3467 NTSTATUS status;
3469 if (tevent_req_is_nterror(req, &status)) {
3470 return status;
3472 if (attr) {
3473 *attr = state->attr;
3475 if (size) {
3476 *size = state->size;
3478 if (write_time) {
3479 *write_time = state->write_time;
3481 return NT_STATUS_OK;
3484 NTSTATUS cli_getatr(struct cli_state *cli,
3485 const char *fname,
3486 uint16_t *attr,
3487 off_t *size,
3488 time_t *write_time)
3490 TALLOC_CTX *frame = talloc_stackframe();
3491 struct tevent_context *ev = NULL;
3492 struct tevent_req *req = NULL;
3493 NTSTATUS status = NT_STATUS_OK;
3495 if (smbXcli_conn_has_async_calls(cli->conn)) {
3497 * Can't use sync call while an async call is in flight
3499 status = NT_STATUS_INVALID_PARAMETER;
3500 goto fail;
3503 ev = samba_tevent_context_init(frame);
3504 if (ev == NULL) {
3505 status = NT_STATUS_NO_MEMORY;
3506 goto fail;
3509 req = cli_getatr_send(frame, ev, cli, fname);
3510 if (req == NULL) {
3511 status = NT_STATUS_NO_MEMORY;
3512 goto fail;
3515 if (!tevent_req_poll(req, ev)) {
3516 status = map_nt_error_from_unix(errno);
3517 goto fail;
3520 status = cli_getatr_recv(req,
3521 attr,
3522 size,
3523 write_time);
3525 fail:
3526 TALLOC_FREE(frame);
3527 return status;
3530 /****************************************************************************
3531 Do a SMBsetattrE call.
3532 ****************************************************************************/
3534 static void cli_setattrE_done(struct tevent_req *subreq);
3536 struct cli_setattrE_state {
3537 uint16_t vwv[7];
3540 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3541 struct tevent_context *ev,
3542 struct cli_state *cli,
3543 uint16_t fnum,
3544 time_t change_time,
3545 time_t access_time,
3546 time_t write_time)
3548 struct tevent_req *req = NULL, *subreq = NULL;
3549 struct cli_setattrE_state *state = NULL;
3550 uint8_t additional_flags = 0;
3552 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3553 if (req == NULL) {
3554 return NULL;
3557 SSVAL(state->vwv+0, 0, fnum);
3558 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3559 smb1cli_conn_server_time_zone(cli->conn));
3560 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3561 smb1cli_conn_server_time_zone(cli->conn));
3562 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3563 smb1cli_conn_server_time_zone(cli->conn));
3565 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3566 7, state->vwv, 0, NULL);
3567 if (tevent_req_nomem(subreq, req)) {
3568 return tevent_req_post(req, ev);
3570 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3571 return req;
3574 static void cli_setattrE_done(struct tevent_req *subreq)
3576 struct tevent_req *req = tevent_req_callback_data(
3577 subreq, struct tevent_req);
3578 NTSTATUS status;
3580 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3581 TALLOC_FREE(subreq);
3582 if (tevent_req_nterror(req, status)) {
3583 return;
3585 tevent_req_done(req);
3588 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3590 return tevent_req_simple_recv_ntstatus(req);
3593 NTSTATUS cli_setattrE(struct cli_state *cli,
3594 uint16_t fnum,
3595 time_t change_time,
3596 time_t access_time,
3597 time_t write_time)
3599 TALLOC_CTX *frame = talloc_stackframe();
3600 struct tevent_context *ev = NULL;
3601 struct tevent_req *req = NULL;
3602 NTSTATUS status = NT_STATUS_OK;
3604 if (smbXcli_conn_has_async_calls(cli->conn)) {
3606 * Can't use sync call while an async call is in flight
3608 status = NT_STATUS_INVALID_PARAMETER;
3609 goto fail;
3612 ev = samba_tevent_context_init(frame);
3613 if (ev == NULL) {
3614 status = NT_STATUS_NO_MEMORY;
3615 goto fail;
3618 req = cli_setattrE_send(frame, ev,
3619 cli,
3620 fnum,
3621 change_time,
3622 access_time,
3623 write_time);
3625 if (req == NULL) {
3626 status = NT_STATUS_NO_MEMORY;
3627 goto fail;
3630 if (!tevent_req_poll(req, ev)) {
3631 status = map_nt_error_from_unix(errno);
3632 goto fail;
3635 status = cli_setattrE_recv(req);
3637 fail:
3638 TALLOC_FREE(frame);
3639 return status;
3642 /****************************************************************************
3643 Do a SMBsetatr call.
3644 ****************************************************************************/
3646 static void cli_setatr_done(struct tevent_req *subreq);
3648 struct cli_setatr_state {
3649 uint16_t vwv[8];
3652 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3653 struct tevent_context *ev,
3654 struct cli_state *cli,
3655 const char *fname,
3656 uint16_t attr,
3657 time_t mtime)
3659 struct tevent_req *req = NULL, *subreq = NULL;
3660 struct cli_setatr_state *state = NULL;
3661 uint8_t additional_flags = 0;
3662 uint8_t *bytes = NULL;
3664 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3665 if (req == NULL) {
3666 return NULL;
3669 SSVAL(state->vwv+0, 0, attr);
3670 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3672 bytes = talloc_array(state, uint8_t, 1);
3673 if (tevent_req_nomem(bytes, req)) {
3674 return tevent_req_post(req, ev);
3676 bytes[0] = 4;
3677 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3678 strlen(fname)+1, NULL);
3679 if (tevent_req_nomem(bytes, req)) {
3680 return tevent_req_post(req, ev);
3682 bytes = talloc_realloc(state, bytes, uint8_t,
3683 talloc_get_size(bytes)+1);
3684 if (tevent_req_nomem(bytes, req)) {
3685 return tevent_req_post(req, ev);
3688 bytes[talloc_get_size(bytes)-1] = 4;
3689 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3690 1, NULL);
3691 if (tevent_req_nomem(bytes, req)) {
3692 return tevent_req_post(req, ev);
3695 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3696 8, state->vwv, talloc_get_size(bytes), bytes);
3697 if (tevent_req_nomem(subreq, req)) {
3698 return tevent_req_post(req, ev);
3700 tevent_req_set_callback(subreq, cli_setatr_done, req);
3701 return req;
3704 static void cli_setatr_done(struct tevent_req *subreq)
3706 struct tevent_req *req = tevent_req_callback_data(
3707 subreq, struct tevent_req);
3708 NTSTATUS status;
3710 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3711 TALLOC_FREE(subreq);
3712 if (tevent_req_nterror(req, status)) {
3713 return;
3715 tevent_req_done(req);
3718 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3720 return tevent_req_simple_recv_ntstatus(req);
3723 NTSTATUS cli_setatr(struct cli_state *cli,
3724 const char *fname,
3725 uint16_t attr,
3726 time_t mtime)
3728 TALLOC_CTX *frame = talloc_stackframe();
3729 struct tevent_context *ev = NULL;
3730 struct tevent_req *req = NULL;
3731 NTSTATUS status = NT_STATUS_OK;
3733 if (smbXcli_conn_has_async_calls(cli->conn)) {
3735 * Can't use sync call while an async call is in flight
3737 status = NT_STATUS_INVALID_PARAMETER;
3738 goto fail;
3741 ev = samba_tevent_context_init(frame);
3742 if (ev == NULL) {
3743 status = NT_STATUS_NO_MEMORY;
3744 goto fail;
3747 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3748 if (req == NULL) {
3749 status = NT_STATUS_NO_MEMORY;
3750 goto fail;
3753 if (!tevent_req_poll(req, ev)) {
3754 status = map_nt_error_from_unix(errno);
3755 goto fail;
3758 status = cli_setatr_recv(req);
3760 fail:
3761 TALLOC_FREE(frame);
3762 return status;
3765 /****************************************************************************
3766 Check for existance of a dir.
3767 ****************************************************************************/
3769 static void cli_chkpath_done(struct tevent_req *subreq);
3771 struct cli_chkpath_state {
3772 int dummy;
3775 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3776 struct tevent_context *ev,
3777 struct cli_state *cli,
3778 const char *fname)
3780 struct tevent_req *req = NULL, *subreq = NULL;
3781 struct cli_chkpath_state *state = NULL;
3782 uint8_t additional_flags = 0;
3783 uint8_t *bytes = NULL;
3785 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3786 if (req == NULL) {
3787 return NULL;
3790 bytes = talloc_array(state, uint8_t, 1);
3791 if (tevent_req_nomem(bytes, req)) {
3792 return tevent_req_post(req, ev);
3794 bytes[0] = 4;
3795 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3796 strlen(fname)+1, NULL);
3798 if (tevent_req_nomem(bytes, req)) {
3799 return tevent_req_post(req, ev);
3802 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3803 0, NULL, talloc_get_size(bytes), bytes);
3804 if (tevent_req_nomem(subreq, req)) {
3805 return tevent_req_post(req, ev);
3807 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3808 return req;
3811 static void cli_chkpath_done(struct tevent_req *subreq)
3813 struct tevent_req *req = tevent_req_callback_data(
3814 subreq, struct tevent_req);
3815 NTSTATUS status;
3817 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3818 TALLOC_FREE(subreq);
3819 if (tevent_req_nterror(req, status)) {
3820 return;
3822 tevent_req_done(req);
3825 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3827 return tevent_req_simple_recv_ntstatus(req);
3830 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3832 TALLOC_CTX *frame = talloc_stackframe();
3833 struct tevent_context *ev = NULL;
3834 struct tevent_req *req = NULL;
3835 char *path2 = NULL;
3836 NTSTATUS status = NT_STATUS_OK;
3838 if (smbXcli_conn_has_async_calls(cli->conn)) {
3840 * Can't use sync call while an async call is in flight
3842 status = NT_STATUS_INVALID_PARAMETER;
3843 goto fail;
3846 path2 = talloc_strdup(frame, path);
3847 if (!path2) {
3848 status = NT_STATUS_NO_MEMORY;
3849 goto fail;
3851 trim_char(path2,'\0','\\');
3852 if (!*path2) {
3853 path2 = talloc_strdup(frame, "\\");
3854 if (!path2) {
3855 status = NT_STATUS_NO_MEMORY;
3856 goto fail;
3860 ev = samba_tevent_context_init(frame);
3861 if (ev == NULL) {
3862 status = NT_STATUS_NO_MEMORY;
3863 goto fail;
3866 req = cli_chkpath_send(frame, ev, cli, path2);
3867 if (req == NULL) {
3868 status = NT_STATUS_NO_MEMORY;
3869 goto fail;
3872 if (!tevent_req_poll(req, ev)) {
3873 status = map_nt_error_from_unix(errno);
3874 goto fail;
3877 status = cli_chkpath_recv(req);
3879 fail:
3880 TALLOC_FREE(frame);
3881 return status;
3884 /****************************************************************************
3885 Query disk space.
3886 ****************************************************************************/
3888 static void cli_dskattr_done(struct tevent_req *subreq);
3890 struct cli_dskattr_state {
3891 int bsize;
3892 int total;
3893 int avail;
3896 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3897 struct tevent_context *ev,
3898 struct cli_state *cli)
3900 struct tevent_req *req = NULL, *subreq = NULL;
3901 struct cli_dskattr_state *state = NULL;
3902 uint8_t additional_flags = 0;
3904 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3905 if (req == NULL) {
3906 return NULL;
3909 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3910 0, NULL, 0, NULL);
3911 if (tevent_req_nomem(subreq, req)) {
3912 return tevent_req_post(req, ev);
3914 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3915 return req;
3918 static void cli_dskattr_done(struct tevent_req *subreq)
3920 struct tevent_req *req = tevent_req_callback_data(
3921 subreq, struct tevent_req);
3922 struct cli_dskattr_state *state = tevent_req_data(
3923 req, struct cli_dskattr_state);
3924 uint8_t wct;
3925 uint16_t *vwv = NULL;
3926 NTSTATUS status;
3928 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3929 NULL);
3930 TALLOC_FREE(subreq);
3931 if (tevent_req_nterror(req, status)) {
3932 return;
3934 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3935 state->total = SVAL(vwv+0, 0);
3936 state->avail = SVAL(vwv+3, 0);
3937 tevent_req_done(req);
3940 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3942 struct cli_dskattr_state *state = tevent_req_data(
3943 req, struct cli_dskattr_state);
3944 NTSTATUS status;
3946 if (tevent_req_is_nterror(req, &status)) {
3947 return status;
3949 *bsize = state->bsize;
3950 *total = state->total;
3951 *avail = state->avail;
3952 return NT_STATUS_OK;
3955 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3957 TALLOC_CTX *frame = talloc_stackframe();
3958 struct tevent_context *ev = NULL;
3959 struct tevent_req *req = NULL;
3960 NTSTATUS status = NT_STATUS_OK;
3962 if (smbXcli_conn_has_async_calls(cli->conn)) {
3964 * Can't use sync call while an async call is in flight
3966 status = NT_STATUS_INVALID_PARAMETER;
3967 goto fail;
3970 ev = samba_tevent_context_init(frame);
3971 if (ev == NULL) {
3972 status = NT_STATUS_NO_MEMORY;
3973 goto fail;
3976 req = cli_dskattr_send(frame, ev, cli);
3977 if (req == NULL) {
3978 status = NT_STATUS_NO_MEMORY;
3979 goto fail;
3982 if (!tevent_req_poll(req, ev)) {
3983 status = map_nt_error_from_unix(errno);
3984 goto fail;
3987 status = cli_dskattr_recv(req, bsize, total, avail);
3989 fail:
3990 TALLOC_FREE(frame);
3991 return status;
3994 /****************************************************************************
3995 Create and open a temporary file.
3996 ****************************************************************************/
3998 static void cli_ctemp_done(struct tevent_req *subreq);
4000 struct ctemp_state {
4001 uint16_t vwv[3];
4002 char *ret_path;
4003 uint16_t fnum;
4006 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4007 struct tevent_context *ev,
4008 struct cli_state *cli,
4009 const char *path)
4011 struct tevent_req *req = NULL, *subreq = NULL;
4012 struct ctemp_state *state = NULL;
4013 uint8_t additional_flags = 0;
4014 uint8_t *bytes = NULL;
4016 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4017 if (req == NULL) {
4018 return NULL;
4021 SSVAL(state->vwv,0,0);
4022 SIVALS(state->vwv+1,0,-1);
4024 bytes = talloc_array(state, uint8_t, 1);
4025 if (tevent_req_nomem(bytes, req)) {
4026 return tevent_req_post(req, ev);
4028 bytes[0] = 4;
4029 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4030 strlen(path)+1, NULL);
4031 if (tevent_req_nomem(bytes, req)) {
4032 return tevent_req_post(req, ev);
4035 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4036 3, state->vwv, talloc_get_size(bytes), bytes);
4037 if (tevent_req_nomem(subreq, req)) {
4038 return tevent_req_post(req, ev);
4040 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4041 return req;
4044 static void cli_ctemp_done(struct tevent_req *subreq)
4046 struct tevent_req *req = tevent_req_callback_data(
4047 subreq, struct tevent_req);
4048 struct ctemp_state *state = tevent_req_data(
4049 req, struct ctemp_state);
4050 NTSTATUS status;
4051 uint8_t wcnt;
4052 uint16_t *vwv;
4053 uint32_t num_bytes = 0;
4054 uint8_t *bytes = NULL;
4056 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4057 &num_bytes, &bytes);
4058 TALLOC_FREE(subreq);
4059 if (tevent_req_nterror(req, status)) {
4060 return;
4063 state->fnum = SVAL(vwv+0, 0);
4065 /* From W2K3, the result is just the ASCII name */
4066 if (num_bytes < 2) {
4067 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4068 return;
4071 if (pull_string_talloc(state,
4072 NULL,
4074 &state->ret_path,
4075 bytes,
4076 num_bytes,
4077 STR_ASCII) == 0) {
4078 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4079 return;
4081 tevent_req_done(req);
4084 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4085 TALLOC_CTX *ctx,
4086 uint16_t *pfnum,
4087 char **outfile)
4089 struct ctemp_state *state = tevent_req_data(req,
4090 struct ctemp_state);
4091 NTSTATUS status;
4093 if (tevent_req_is_nterror(req, &status)) {
4094 return status;
4096 *pfnum = state->fnum;
4097 *outfile = talloc_strdup(ctx, state->ret_path);
4098 if (!*outfile) {
4099 return NT_STATUS_NO_MEMORY;
4101 return NT_STATUS_OK;
4104 NTSTATUS cli_ctemp(struct cli_state *cli,
4105 TALLOC_CTX *ctx,
4106 const char *path,
4107 uint16_t *pfnum,
4108 char **out_path)
4110 TALLOC_CTX *frame = talloc_stackframe();
4111 struct tevent_context *ev;
4112 struct tevent_req *req;
4113 NTSTATUS status = NT_STATUS_OK;
4115 if (smbXcli_conn_has_async_calls(cli->conn)) {
4117 * Can't use sync call while an async call is in flight
4119 status = NT_STATUS_INVALID_PARAMETER;
4120 goto fail;
4123 ev = samba_tevent_context_init(frame);
4124 if (ev == NULL) {
4125 status = NT_STATUS_NO_MEMORY;
4126 goto fail;
4129 req = cli_ctemp_send(frame, ev, cli, path);
4130 if (req == NULL) {
4131 status = NT_STATUS_NO_MEMORY;
4132 goto fail;
4135 if (!tevent_req_poll(req, ev)) {
4136 status = map_nt_error_from_unix(errno);
4137 goto fail;
4140 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4142 fail:
4143 TALLOC_FREE(frame);
4144 return status;
4148 send a raw ioctl - used by the torture code
4150 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4152 uint16_t vwv[3];
4153 NTSTATUS status;
4155 SSVAL(vwv+0, 0, fnum);
4156 SSVAL(vwv+1, 0, code>>16);
4157 SSVAL(vwv+2, 0, (code&0xFFFF));
4159 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4160 NULL, 0, NULL, NULL, NULL, NULL);
4161 if (!NT_STATUS_IS_OK(status)) {
4162 return status;
4164 *blob = data_blob_null;
4165 return NT_STATUS_OK;
4168 /*********************************************************
4169 Set an extended attribute utility fn.
4170 *********************************************************/
4172 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4173 uint8_t *param, unsigned int param_len,
4174 const char *ea_name,
4175 const char *ea_val, size_t ea_len)
4177 uint16_t setup[1];
4178 unsigned int data_len = 0;
4179 uint8_t *data = NULL;
4180 char *p;
4181 size_t ea_namelen = strlen(ea_name);
4182 NTSTATUS status;
4184 SSVAL(setup, 0, setup_val);
4186 if (ea_namelen == 0 && ea_len == 0) {
4187 data_len = 4;
4188 data = talloc_array(talloc_tos(),
4189 uint8_t,
4190 data_len);
4191 if (!data) {
4192 return NT_STATUS_NO_MEMORY;
4194 p = (char *)data;
4195 SIVAL(p,0,data_len);
4196 } else {
4197 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4198 data = talloc_array(talloc_tos(),
4199 uint8_t,
4200 data_len);
4201 if (!data) {
4202 return NT_STATUS_NO_MEMORY;
4204 p = (char *)data;
4205 SIVAL(p,0,data_len);
4206 p += 4;
4207 SCVAL(p, 0, 0); /* EA flags. */
4208 SCVAL(p, 1, ea_namelen);
4209 SSVAL(p, 2, ea_len);
4210 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4211 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4214 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4215 setup, 1, 0,
4216 param, param_len, 2,
4217 data, data_len, CLI_BUFFER_SIZE,
4218 NULL,
4219 NULL, 0, NULL, /* rsetup */
4220 NULL, 0, NULL, /* rparam */
4221 NULL, 0, NULL); /* rdata */
4222 talloc_free(data);
4223 return status;
4226 /*********************************************************
4227 Set an extended attribute on a pathname.
4228 *********************************************************/
4230 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4231 const char *ea_name, const char *ea_val,
4232 size_t ea_len)
4234 unsigned int param_len = 0;
4235 uint8_t *param;
4236 NTSTATUS status;
4237 TALLOC_CTX *frame = talloc_stackframe();
4239 param = talloc_array(talloc_tos(), uint8_t, 6);
4240 if (!param) {
4241 return NT_STATUS_NO_MEMORY;
4243 SSVAL(param,0,SMB_INFO_SET_EA);
4244 SSVAL(param,2,0);
4245 SSVAL(param,4,0);
4247 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4248 path, strlen(path)+1,
4249 NULL);
4250 param_len = talloc_get_size(param);
4252 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4253 ea_name, ea_val, ea_len);
4254 talloc_free(frame);
4255 return status;
4258 /*********************************************************
4259 Set an extended attribute on an fnum.
4260 *********************************************************/
4262 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4263 const char *ea_name, const char *ea_val,
4264 size_t ea_len)
4266 uint8_t param[6];
4268 memset(param, 0, 6);
4269 SSVAL(param,0,fnum);
4270 SSVAL(param,2,SMB_INFO_SET_EA);
4272 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4273 ea_name, ea_val, ea_len);
4276 /*********************************************************
4277 Get an extended attribute list utility fn.
4278 *********************************************************/
4280 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4281 size_t rdata_len,
4282 size_t *pnum_eas, struct ea_struct **pea_list)
4284 struct ea_struct *ea_list = NULL;
4285 size_t num_eas;
4286 size_t ea_size;
4287 const uint8_t *p;
4289 if (rdata_len < 4) {
4290 return false;
4293 ea_size = (size_t)IVAL(rdata,0);
4294 if (ea_size > rdata_len) {
4295 return false;
4298 if (ea_size == 0) {
4299 /* No EA's present. */
4300 *pnum_eas = 0;
4301 *pea_list = NULL;
4302 return true;
4305 p = rdata + 4;
4306 ea_size -= 4;
4308 /* Validate the EA list and count it. */
4309 for (num_eas = 0; ea_size >= 4; num_eas++) {
4310 unsigned int ea_namelen = CVAL(p,1);
4311 unsigned int ea_valuelen = SVAL(p,2);
4312 if (ea_namelen == 0) {
4313 return false;
4315 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4316 return false;
4318 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4319 p += 4 + ea_namelen + 1 + ea_valuelen;
4322 if (num_eas == 0) {
4323 *pnum_eas = 0;
4324 *pea_list = NULL;
4325 return true;
4328 *pnum_eas = num_eas;
4329 if (!pea_list) {
4330 /* Caller only wants number of EA's. */
4331 return true;
4334 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4335 if (!ea_list) {
4336 return false;
4339 ea_size = (size_t)IVAL(rdata,0);
4340 p = rdata + 4;
4342 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4343 struct ea_struct *ea = &ea_list[num_eas];
4344 fstring unix_ea_name;
4345 unsigned int ea_namelen = CVAL(p,1);
4346 unsigned int ea_valuelen = SVAL(p,2);
4348 ea->flags = CVAL(p,0);
4349 unix_ea_name[0] = '\0';
4350 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4351 ea->name = talloc_strdup(ea_list, unix_ea_name);
4352 if (!ea->name) {
4353 goto fail;
4355 /* Ensure the value is null terminated (in case it's a string). */
4356 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4357 if (!ea->value.data) {
4358 goto fail;
4360 if (ea_valuelen) {
4361 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4363 ea->value.data[ea_valuelen] = 0;
4364 ea->value.length--;
4365 p += 4 + ea_namelen + 1 + ea_valuelen;
4368 *pea_list = ea_list;
4369 return true;
4371 fail:
4372 TALLOC_FREE(ea_list);
4373 return false;
4376 /*********************************************************
4377 Get an extended attribute list from a pathname.
4378 *********************************************************/
4380 struct cli_get_ea_list_path_state {
4381 uint32_t num_data;
4382 uint8_t *data;
4385 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4387 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4388 struct tevent_context *ev,
4389 struct cli_state *cli,
4390 const char *fname)
4392 struct tevent_req *req, *subreq;
4393 struct cli_get_ea_list_path_state *state;
4395 req = tevent_req_create(mem_ctx, &state,
4396 struct cli_get_ea_list_path_state);
4397 if (req == NULL) {
4398 return NULL;
4400 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4401 SMB_INFO_QUERY_ALL_EAS, 4,
4402 CLI_BUFFER_SIZE);
4403 if (tevent_req_nomem(subreq, req)) {
4404 return tevent_req_post(req, ev);
4406 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4407 return req;
4410 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4412 struct tevent_req *req = tevent_req_callback_data(
4413 subreq, struct tevent_req);
4414 struct cli_get_ea_list_path_state *state = tevent_req_data(
4415 req, struct cli_get_ea_list_path_state);
4416 NTSTATUS status;
4418 status = cli_qpathinfo_recv(subreq, state, &state->data,
4419 &state->num_data);
4420 TALLOC_FREE(subreq);
4421 if (tevent_req_nterror(req, status)) {
4422 return;
4424 tevent_req_done(req);
4427 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4428 size_t *pnum_eas, struct ea_struct **peas)
4430 struct cli_get_ea_list_path_state *state = tevent_req_data(
4431 req, struct cli_get_ea_list_path_state);
4432 NTSTATUS status;
4434 if (tevent_req_is_nterror(req, &status)) {
4435 return status;
4437 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4438 pnum_eas, peas)) {
4439 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4441 return NT_STATUS_OK;
4444 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4445 TALLOC_CTX *ctx,
4446 size_t *pnum_eas,
4447 struct ea_struct **pea_list)
4449 TALLOC_CTX *frame = talloc_stackframe();
4450 struct tevent_context *ev = NULL;
4451 struct tevent_req *req = NULL;
4452 NTSTATUS status = NT_STATUS_NO_MEMORY;
4454 if (smbXcli_conn_has_async_calls(cli->conn)) {
4456 * Can't use sync call while an async call is in flight
4458 status = NT_STATUS_INVALID_PARAMETER;
4459 goto fail;
4461 ev = samba_tevent_context_init(frame);
4462 if (ev == NULL) {
4463 goto fail;
4465 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4466 if (req == NULL) {
4467 goto fail;
4469 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4470 goto fail;
4472 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4473 fail:
4474 TALLOC_FREE(frame);
4475 return status;
4478 /****************************************************************************
4479 Convert open "flags" arg to uint32_t on wire.
4480 ****************************************************************************/
4482 static uint32_t open_flags_to_wire(int flags)
4484 int open_mode = flags & O_ACCMODE;
4485 uint32_t ret = 0;
4487 switch (open_mode) {
4488 case O_WRONLY:
4489 ret |= SMB_O_WRONLY;
4490 break;
4491 case O_RDWR:
4492 ret |= SMB_O_RDWR;
4493 break;
4494 default:
4495 case O_RDONLY:
4496 ret |= SMB_O_RDONLY;
4497 break;
4500 if (flags & O_CREAT) {
4501 ret |= SMB_O_CREAT;
4503 if (flags & O_EXCL) {
4504 ret |= SMB_O_EXCL;
4506 if (flags & O_TRUNC) {
4507 ret |= SMB_O_TRUNC;
4509 #if defined(O_SYNC)
4510 if (flags & O_SYNC) {
4511 ret |= SMB_O_SYNC;
4513 #endif /* O_SYNC */
4514 if (flags & O_APPEND) {
4515 ret |= SMB_O_APPEND;
4517 #if defined(O_DIRECT)
4518 if (flags & O_DIRECT) {
4519 ret |= SMB_O_DIRECT;
4521 #endif
4522 #if defined(O_DIRECTORY)
4523 if (flags & O_DIRECTORY) {
4524 ret |= SMB_O_DIRECTORY;
4526 #endif
4527 return ret;
4530 /****************************************************************************
4531 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4532 ****************************************************************************/
4534 struct posix_open_state {
4535 uint16_t setup;
4536 uint8_t *param;
4537 uint8_t data[18];
4538 uint16_t fnum; /* Out */
4541 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4543 struct tevent_req *req = tevent_req_callback_data(
4544 subreq, struct tevent_req);
4545 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4546 NTSTATUS status;
4547 uint8_t *data;
4548 uint32_t num_data;
4550 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4551 NULL, 0, NULL, &data, 12, &num_data);
4552 TALLOC_FREE(subreq);
4553 if (tevent_req_nterror(req, status)) {
4554 return;
4556 state->fnum = SVAL(data,2);
4557 tevent_req_done(req);
4560 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4561 struct tevent_context *ev,
4562 struct cli_state *cli,
4563 const char *fname,
4564 int flags,
4565 mode_t mode,
4566 bool is_dir)
4568 struct tevent_req *req = NULL, *subreq = NULL;
4569 struct posix_open_state *state = NULL;
4570 uint32_t wire_flags = open_flags_to_wire(flags);
4572 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4573 if (req == NULL) {
4574 return NULL;
4577 /* Setup setup word. */
4578 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4580 /* Setup param array. */
4581 state->param = talloc_array(state, uint8_t, 6);
4582 if (tevent_req_nomem(state->param, req)) {
4583 return tevent_req_post(req, ev);
4585 memset(state->param, '\0', 6);
4586 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4588 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4589 strlen(fname)+1, NULL);
4591 if (tevent_req_nomem(state->param, req)) {
4592 return tevent_req_post(req, ev);
4595 /* Setup data words. */
4596 if (is_dir) {
4597 wire_flags |= SMB_O_DIRECTORY;
4600 SIVAL(state->data,0,0); /* No oplock. */
4601 SIVAL(state->data,4,wire_flags);
4602 SIVAL(state->data,8,unix_perms_to_wire(mode));
4603 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4604 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4606 subreq = cli_trans_send(state, /* mem ctx. */
4607 ev, /* event ctx. */
4608 cli, /* cli_state. */
4609 SMBtrans2, /* cmd. */
4610 NULL, /* pipe name. */
4611 -1, /* fid. */
4612 0, /* function. */
4613 0, /* flags. */
4614 &state->setup, /* setup. */
4615 1, /* num setup uint16_t words. */
4616 0, /* max returned setup. */
4617 state->param, /* param. */
4618 talloc_get_size(state->param),/* num param. */
4619 2, /* max returned param. */
4620 state->data, /* data. */
4621 18, /* num data. */
4622 12); /* max returned data. */
4624 if (tevent_req_nomem(subreq, req)) {
4625 return tevent_req_post(req, ev);
4627 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4628 return req;
4631 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4632 struct tevent_context *ev,
4633 struct cli_state *cli,
4634 const char *fname,
4635 int flags,
4636 mode_t mode)
4638 return cli_posix_open_internal_send(mem_ctx, ev,
4639 cli, fname, flags, mode, false);
4642 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4644 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4645 NTSTATUS status;
4647 if (tevent_req_is_nterror(req, &status)) {
4648 return status;
4650 *pfnum = state->fnum;
4651 return NT_STATUS_OK;
4654 /****************************************************************************
4655 Open - POSIX semantics. Doesn't request oplock.
4656 ****************************************************************************/
4658 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4659 int flags, mode_t mode, uint16_t *pfnum)
4662 TALLOC_CTX *frame = talloc_stackframe();
4663 struct tevent_context *ev = NULL;
4664 struct tevent_req *req = NULL;
4665 NTSTATUS status = NT_STATUS_OK;
4667 if (smbXcli_conn_has_async_calls(cli->conn)) {
4669 * Can't use sync call while an async call is in flight
4671 status = NT_STATUS_INVALID_PARAMETER;
4672 goto fail;
4675 ev = samba_tevent_context_init(frame);
4676 if (ev == NULL) {
4677 status = NT_STATUS_NO_MEMORY;
4678 goto fail;
4681 req = cli_posix_open_send(frame,
4683 cli,
4684 fname,
4685 flags,
4686 mode);
4687 if (req == NULL) {
4688 status = NT_STATUS_NO_MEMORY;
4689 goto fail;
4692 if (!tevent_req_poll(req, ev)) {
4693 status = map_nt_error_from_unix(errno);
4694 goto fail;
4697 status = cli_posix_open_recv(req, pfnum);
4699 fail:
4700 TALLOC_FREE(frame);
4701 return status;
4704 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4705 struct tevent_context *ev,
4706 struct cli_state *cli,
4707 const char *fname,
4708 mode_t mode)
4710 return cli_posix_open_internal_send(mem_ctx, ev,
4711 cli, fname, O_CREAT, mode, true);
4714 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4716 return tevent_req_simple_recv_ntstatus(req);
4719 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4721 TALLOC_CTX *frame = talloc_stackframe();
4722 struct tevent_context *ev = NULL;
4723 struct tevent_req *req = NULL;
4724 NTSTATUS status = NT_STATUS_OK;
4726 if (smbXcli_conn_has_async_calls(cli->conn)) {
4728 * Can't use sync call while an async call is in flight
4730 status = NT_STATUS_INVALID_PARAMETER;
4731 goto fail;
4734 ev = samba_tevent_context_init(frame);
4735 if (ev == NULL) {
4736 status = NT_STATUS_NO_MEMORY;
4737 goto fail;
4740 req = cli_posix_mkdir_send(frame,
4742 cli,
4743 fname,
4744 mode);
4745 if (req == NULL) {
4746 status = NT_STATUS_NO_MEMORY;
4747 goto fail;
4750 if (!tevent_req_poll(req, ev)) {
4751 status = map_nt_error_from_unix(errno);
4752 goto fail;
4755 status = cli_posix_mkdir_recv(req);
4757 fail:
4758 TALLOC_FREE(frame);
4759 return status;
4762 /****************************************************************************
4763 unlink or rmdir - POSIX semantics.
4764 ****************************************************************************/
4766 struct cli_posix_unlink_internal_state {
4767 uint8_t data[2];
4770 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4772 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4773 struct tevent_context *ev,
4774 struct cli_state *cli,
4775 const char *fname,
4776 uint16_t level)
4778 struct tevent_req *req = NULL, *subreq = NULL;
4779 struct cli_posix_unlink_internal_state *state = NULL;
4781 req = tevent_req_create(mem_ctx, &state,
4782 struct cli_posix_unlink_internal_state);
4783 if (req == NULL) {
4784 return NULL;
4787 /* Setup data word. */
4788 SSVAL(state->data, 0, level);
4790 subreq = cli_setpathinfo_send(state, ev, cli,
4791 SMB_POSIX_PATH_UNLINK,
4792 fname,
4793 state->data, sizeof(state->data));
4794 if (tevent_req_nomem(subreq, req)) {
4795 return tevent_req_post(req, ev);
4797 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4798 return req;
4801 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4803 NTSTATUS status = cli_setpathinfo_recv(subreq);
4804 tevent_req_simple_finish_ntstatus(subreq, status);
4807 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4808 struct tevent_context *ev,
4809 struct cli_state *cli,
4810 const char *fname)
4812 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4813 SMB_POSIX_UNLINK_FILE_TARGET);
4816 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4818 return tevent_req_simple_recv_ntstatus(req);
4821 /****************************************************************************
4822 unlink - POSIX semantics.
4823 ****************************************************************************/
4825 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4827 TALLOC_CTX *frame = talloc_stackframe();
4828 struct tevent_context *ev = NULL;
4829 struct tevent_req *req = NULL;
4830 NTSTATUS status = NT_STATUS_OK;
4832 if (smbXcli_conn_has_async_calls(cli->conn)) {
4834 * Can't use sync call while an async call is in flight
4836 status = NT_STATUS_INVALID_PARAMETER;
4837 goto fail;
4840 ev = samba_tevent_context_init(frame);
4841 if (ev == NULL) {
4842 status = NT_STATUS_NO_MEMORY;
4843 goto fail;
4846 req = cli_posix_unlink_send(frame,
4848 cli,
4849 fname);
4850 if (req == NULL) {
4851 status = NT_STATUS_NO_MEMORY;
4852 goto fail;
4855 if (!tevent_req_poll(req, ev)) {
4856 status = map_nt_error_from_unix(errno);
4857 goto fail;
4860 status = cli_posix_unlink_recv(req);
4862 fail:
4863 TALLOC_FREE(frame);
4864 return status;
4867 /****************************************************************************
4868 rmdir - POSIX semantics.
4869 ****************************************************************************/
4871 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4872 struct tevent_context *ev,
4873 struct cli_state *cli,
4874 const char *fname)
4876 return cli_posix_unlink_internal_send(
4877 mem_ctx, ev, cli, fname,
4878 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4881 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4883 return tevent_req_simple_recv_ntstatus(req);
4886 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4888 TALLOC_CTX *frame = talloc_stackframe();
4889 struct tevent_context *ev = NULL;
4890 struct tevent_req *req = NULL;
4891 NTSTATUS status = NT_STATUS_OK;
4893 if (smbXcli_conn_has_async_calls(cli->conn)) {
4895 * Can't use sync call while an async call is in flight
4897 status = NT_STATUS_INVALID_PARAMETER;
4898 goto fail;
4901 ev = samba_tevent_context_init(frame);
4902 if (ev == NULL) {
4903 status = NT_STATUS_NO_MEMORY;
4904 goto fail;
4907 req = cli_posix_rmdir_send(frame,
4909 cli,
4910 fname);
4911 if (req == NULL) {
4912 status = NT_STATUS_NO_MEMORY;
4913 goto fail;
4916 if (!tevent_req_poll(req, ev)) {
4917 status = map_nt_error_from_unix(errno);
4918 goto fail;
4921 status = cli_posix_rmdir_recv(req, frame);
4923 fail:
4924 TALLOC_FREE(frame);
4925 return status;
4928 /****************************************************************************
4929 filechangenotify
4930 ****************************************************************************/
4932 struct cli_notify_state {
4933 uint8_t setup[8];
4934 uint32_t num_changes;
4935 struct notify_change *changes;
4938 static void cli_notify_done(struct tevent_req *subreq);
4940 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4941 struct tevent_context *ev,
4942 struct cli_state *cli, uint16_t fnum,
4943 uint32_t buffer_size,
4944 uint32_t completion_filter, bool recursive)
4946 struct tevent_req *req, *subreq;
4947 struct cli_notify_state *state;
4948 unsigned old_timeout;
4950 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4951 if (req == NULL) {
4952 return NULL;
4955 SIVAL(state->setup, 0, completion_filter);
4956 SSVAL(state->setup, 4, fnum);
4957 SSVAL(state->setup, 6, recursive);
4960 * Notifies should not time out
4962 old_timeout = cli_set_timeout(cli, 0);
4964 subreq = cli_trans_send(
4965 state, /* mem ctx. */
4966 ev, /* event ctx. */
4967 cli, /* cli_state. */
4968 SMBnttrans, /* cmd. */
4969 NULL, /* pipe name. */
4970 -1, /* fid. */
4971 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4972 0, /* flags. */
4973 (uint16_t *)state->setup, /* setup. */
4974 4, /* num setup uint16_t words. */
4975 0, /* max returned setup. */
4976 NULL, /* param. */
4977 0, /* num param. */
4978 buffer_size, /* max returned param. */
4979 NULL, /* data. */
4980 0, /* num data. */
4981 0); /* max returned data. */
4983 cli_set_timeout(cli, old_timeout);
4985 if (tevent_req_nomem(subreq, req)) {
4986 return tevent_req_post(req, ev);
4988 tevent_req_set_callback(subreq, cli_notify_done, req);
4989 return req;
4992 static void cli_notify_done(struct tevent_req *subreq)
4994 struct tevent_req *req = tevent_req_callback_data(
4995 subreq, struct tevent_req);
4996 struct cli_notify_state *state = tevent_req_data(
4997 req, struct cli_notify_state);
4998 NTSTATUS status;
4999 uint8_t *params;
5000 uint32_t i, ofs, num_params;
5001 uint16_t flags2;
5003 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5004 &params, 0, &num_params, NULL, 0, NULL);
5005 TALLOC_FREE(subreq);
5006 if (tevent_req_nterror(req, status)) {
5007 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5008 return;
5011 state->num_changes = 0;
5012 ofs = 0;
5014 while (num_params - ofs > 12) {
5015 uint32_t next = IVAL(params, ofs);
5016 state->num_changes += 1;
5018 if ((next == 0) || (ofs+next >= num_params)) {
5019 break;
5021 ofs += next;
5024 state->changes = talloc_array(state, struct notify_change,
5025 state->num_changes);
5026 if (tevent_req_nomem(state->changes, req)) {
5027 TALLOC_FREE(params);
5028 return;
5031 ofs = 0;
5033 for (i=0; i<state->num_changes; i++) {
5034 uint32_t next = IVAL(params, ofs);
5035 uint32_t len = IVAL(params, ofs+8);
5036 ssize_t ret;
5037 char *name;
5039 if (trans_oob(num_params, ofs + 12, len)) {
5040 TALLOC_FREE(params);
5041 tevent_req_nterror(
5042 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5043 return;
5046 state->changes[i].action = IVAL(params, ofs+4);
5047 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5048 &name, params+ofs+12, len,
5049 STR_TERMINATE|STR_UNICODE);
5050 if (ret == -1) {
5051 TALLOC_FREE(params);
5052 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5053 return;
5055 state->changes[i].name = name;
5056 ofs += next;
5059 TALLOC_FREE(params);
5060 tevent_req_done(req);
5063 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5064 uint32_t *pnum_changes,
5065 struct notify_change **pchanges)
5067 struct cli_notify_state *state = tevent_req_data(
5068 req, struct cli_notify_state);
5069 NTSTATUS status;
5071 if (tevent_req_is_nterror(req, &status)) {
5072 return status;
5075 *pnum_changes = state->num_changes;
5076 *pchanges = talloc_move(mem_ctx, &state->changes);
5077 return NT_STATUS_OK;
5080 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5081 uint32_t completion_filter, bool recursive,
5082 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5083 struct notify_change **pchanges)
5085 TALLOC_CTX *frame = talloc_stackframe();
5086 struct tevent_context *ev;
5087 struct tevent_req *req;
5088 NTSTATUS status = NT_STATUS_NO_MEMORY;
5090 if (smbXcli_conn_has_async_calls(cli->conn)) {
5092 * Can't use sync call while an async call is in flight
5094 status = NT_STATUS_INVALID_PARAMETER;
5095 goto fail;
5097 ev = samba_tevent_context_init(frame);
5098 if (ev == NULL) {
5099 goto fail;
5101 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5102 completion_filter, recursive);
5103 if (req == NULL) {
5104 goto fail;
5106 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5107 goto fail;
5109 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5110 fail:
5111 TALLOC_FREE(frame);
5112 return status;
5115 struct cli_qpathinfo_state {
5116 uint8_t *param;
5117 uint8_t *data;
5118 uint16_t setup[1];
5119 uint32_t min_rdata;
5120 uint8_t *rdata;
5121 uint32_t num_rdata;
5124 static void cli_qpathinfo_done(struct tevent_req *subreq);
5126 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5127 struct tevent_context *ev,
5128 struct cli_state *cli, const char *fname,
5129 uint16_t level, uint32_t min_rdata,
5130 uint32_t max_rdata)
5132 struct tevent_req *req, *subreq;
5133 struct cli_qpathinfo_state *state;
5135 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5136 if (req == NULL) {
5137 return NULL;
5139 state->min_rdata = min_rdata;
5140 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5142 state->param = talloc_zero_array(state, uint8_t, 6);
5143 if (tevent_req_nomem(state->param, req)) {
5144 return tevent_req_post(req, ev);
5146 SSVAL(state->param, 0, level);
5147 state->param = trans2_bytes_push_str(
5148 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5149 if (tevent_req_nomem(state->param, req)) {
5150 return tevent_req_post(req, ev);
5153 subreq = cli_trans_send(
5154 state, /* mem ctx. */
5155 ev, /* event ctx. */
5156 cli, /* cli_state. */
5157 SMBtrans2, /* cmd. */
5158 NULL, /* pipe name. */
5159 -1, /* fid. */
5160 0, /* function. */
5161 0, /* flags. */
5162 state->setup, /* setup. */
5163 1, /* num setup uint16_t words. */
5164 0, /* max returned setup. */
5165 state->param, /* param. */
5166 talloc_get_size(state->param), /* num param. */
5167 2, /* max returned param. */
5168 NULL, /* data. */
5169 0, /* num data. */
5170 max_rdata); /* max returned data. */
5172 if (tevent_req_nomem(subreq, req)) {
5173 return tevent_req_post(req, ev);
5175 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5176 return req;
5179 static void cli_qpathinfo_done(struct tevent_req *subreq)
5181 struct tevent_req *req = tevent_req_callback_data(
5182 subreq, struct tevent_req);
5183 struct cli_qpathinfo_state *state = tevent_req_data(
5184 req, struct cli_qpathinfo_state);
5185 NTSTATUS status;
5187 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5188 NULL, 0, NULL,
5189 &state->rdata, state->min_rdata,
5190 &state->num_rdata);
5191 if (tevent_req_nterror(req, status)) {
5192 return;
5194 tevent_req_done(req);
5197 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5198 uint8_t **rdata, uint32_t *num_rdata)
5200 struct cli_qpathinfo_state *state = tevent_req_data(
5201 req, struct cli_qpathinfo_state);
5202 NTSTATUS status;
5204 if (tevent_req_is_nterror(req, &status)) {
5205 return status;
5207 if (rdata != NULL) {
5208 *rdata = talloc_move(mem_ctx, &state->rdata);
5209 } else {
5210 TALLOC_FREE(state->rdata);
5212 if (num_rdata != NULL) {
5213 *num_rdata = state->num_rdata;
5215 return NT_STATUS_OK;
5218 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5219 const char *fname, uint16_t level, uint32_t min_rdata,
5220 uint32_t max_rdata,
5221 uint8_t **rdata, uint32_t *num_rdata)
5223 TALLOC_CTX *frame = talloc_stackframe();
5224 struct tevent_context *ev;
5225 struct tevent_req *req;
5226 NTSTATUS status = NT_STATUS_NO_MEMORY;
5228 if (smbXcli_conn_has_async_calls(cli->conn)) {
5230 * Can't use sync call while an async call is in flight
5232 status = NT_STATUS_INVALID_PARAMETER;
5233 goto fail;
5235 ev = samba_tevent_context_init(frame);
5236 if (ev == NULL) {
5237 goto fail;
5239 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5240 max_rdata);
5241 if (req == NULL) {
5242 goto fail;
5244 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5245 goto fail;
5247 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5248 fail:
5249 TALLOC_FREE(frame);
5250 return status;
5253 struct cli_qfileinfo_state {
5254 uint16_t setup[1];
5255 uint8_t param[4];
5256 uint8_t *data;
5257 uint16_t recv_flags2;
5258 uint32_t min_rdata;
5259 uint8_t *rdata;
5260 uint32_t num_rdata;
5263 static void cli_qfileinfo_done(struct tevent_req *subreq);
5265 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5266 struct tevent_context *ev,
5267 struct cli_state *cli, uint16_t fnum,
5268 uint16_t level, uint32_t min_rdata,
5269 uint32_t max_rdata)
5271 struct tevent_req *req, *subreq;
5272 struct cli_qfileinfo_state *state;
5274 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5275 if (req == NULL) {
5276 return NULL;
5278 state->min_rdata = min_rdata;
5279 SSVAL(state->param, 0, fnum);
5280 SSVAL(state->param, 2, level);
5281 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5283 subreq = cli_trans_send(
5284 state, /* mem ctx. */
5285 ev, /* event ctx. */
5286 cli, /* cli_state. */
5287 SMBtrans2, /* cmd. */
5288 NULL, /* pipe name. */
5289 -1, /* fid. */
5290 0, /* function. */
5291 0, /* flags. */
5292 state->setup, /* setup. */
5293 1, /* num setup uint16_t words. */
5294 0, /* max returned setup. */
5295 state->param, /* param. */
5296 sizeof(state->param), /* num param. */
5297 2, /* max returned param. */
5298 NULL, /* data. */
5299 0, /* num data. */
5300 max_rdata); /* max returned data. */
5302 if (tevent_req_nomem(subreq, req)) {
5303 return tevent_req_post(req, ev);
5305 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5306 return req;
5309 static void cli_qfileinfo_done(struct tevent_req *subreq)
5311 struct tevent_req *req = tevent_req_callback_data(
5312 subreq, struct tevent_req);
5313 struct cli_qfileinfo_state *state = tevent_req_data(
5314 req, struct cli_qfileinfo_state);
5315 NTSTATUS status;
5317 status = cli_trans_recv(subreq, state,
5318 &state->recv_flags2,
5319 NULL, 0, NULL,
5320 NULL, 0, NULL,
5321 &state->rdata, state->min_rdata,
5322 &state->num_rdata);
5323 if (tevent_req_nterror(req, status)) {
5324 return;
5326 tevent_req_done(req);
5329 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5330 uint16_t *recv_flags2,
5331 uint8_t **rdata, uint32_t *num_rdata)
5333 struct cli_qfileinfo_state *state = tevent_req_data(
5334 req, struct cli_qfileinfo_state);
5335 NTSTATUS status;
5337 if (tevent_req_is_nterror(req, &status)) {
5338 return status;
5341 if (recv_flags2 != NULL) {
5342 *recv_flags2 = state->recv_flags2;
5344 if (rdata != NULL) {
5345 *rdata = talloc_move(mem_ctx, &state->rdata);
5346 } else {
5347 TALLOC_FREE(state->rdata);
5349 if (num_rdata != NULL) {
5350 *num_rdata = state->num_rdata;
5352 return NT_STATUS_OK;
5355 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5356 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5357 uint32_t max_rdata, uint16_t *recv_flags2,
5358 uint8_t **rdata, uint32_t *num_rdata)
5360 TALLOC_CTX *frame = talloc_stackframe();
5361 struct tevent_context *ev;
5362 struct tevent_req *req;
5363 NTSTATUS status = NT_STATUS_NO_MEMORY;
5365 if (smbXcli_conn_has_async_calls(cli->conn)) {
5367 * Can't use sync call while an async call is in flight
5369 status = NT_STATUS_INVALID_PARAMETER;
5370 goto fail;
5372 ev = samba_tevent_context_init(frame);
5373 if (ev == NULL) {
5374 goto fail;
5376 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5377 max_rdata);
5378 if (req == NULL) {
5379 goto fail;
5381 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5382 goto fail;
5384 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5385 fail:
5386 TALLOC_FREE(frame);
5387 return status;
5390 struct cli_flush_state {
5391 uint16_t vwv[1];
5394 static void cli_flush_done(struct tevent_req *subreq);
5396 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5397 struct tevent_context *ev,
5398 struct cli_state *cli,
5399 uint16_t fnum)
5401 struct tevent_req *req, *subreq;
5402 struct cli_flush_state *state;
5404 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5405 if (req == NULL) {
5406 return NULL;
5408 SSVAL(state->vwv + 0, 0, fnum);
5410 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5411 0, NULL);
5412 if (tevent_req_nomem(subreq, req)) {
5413 return tevent_req_post(req, ev);
5415 tevent_req_set_callback(subreq, cli_flush_done, req);
5416 return req;
5419 static void cli_flush_done(struct tevent_req *subreq)
5421 struct tevent_req *req = tevent_req_callback_data(
5422 subreq, struct tevent_req);
5423 NTSTATUS status;
5425 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5426 TALLOC_FREE(subreq);
5427 if (tevent_req_nterror(req, status)) {
5428 return;
5430 tevent_req_done(req);
5433 NTSTATUS cli_flush_recv(struct tevent_req *req)
5435 return tevent_req_simple_recv_ntstatus(req);
5438 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5440 TALLOC_CTX *frame = talloc_stackframe();
5441 struct tevent_context *ev;
5442 struct tevent_req *req;
5443 NTSTATUS status = NT_STATUS_NO_MEMORY;
5445 if (smbXcli_conn_has_async_calls(cli->conn)) {
5447 * Can't use sync call while an async call is in flight
5449 status = NT_STATUS_INVALID_PARAMETER;
5450 goto fail;
5452 ev = samba_tevent_context_init(frame);
5453 if (ev == NULL) {
5454 goto fail;
5456 req = cli_flush_send(frame, ev, cli, fnum);
5457 if (req == NULL) {
5458 goto fail;
5460 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5461 goto fail;
5463 status = cli_flush_recv(req);
5464 fail:
5465 TALLOC_FREE(frame);
5466 return status;
5469 struct cli_shadow_copy_data_state {
5470 uint16_t setup[4];
5471 uint8_t *data;
5472 uint32_t num_data;
5473 bool get_names;
5476 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5478 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5479 struct tevent_context *ev,
5480 struct cli_state *cli,
5481 uint16_t fnum,
5482 bool get_names)
5484 struct tevent_req *req, *subreq;
5485 struct cli_shadow_copy_data_state *state;
5486 uint32_t ret_size;
5488 req = tevent_req_create(mem_ctx, &state,
5489 struct cli_shadow_copy_data_state);
5490 if (req == NULL) {
5491 return NULL;
5493 state->get_names = get_names;
5494 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5496 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5497 SSVAL(state->setup + 2, 0, fnum);
5498 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5499 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5501 subreq = cli_trans_send(
5502 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5503 state->setup, ARRAY_SIZE(state->setup), 0,
5504 NULL, 0, 0,
5505 NULL, 0, ret_size);
5506 if (tevent_req_nomem(subreq, req)) {
5507 return tevent_req_post(req, ev);
5509 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5510 return req;
5513 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5515 struct tevent_req *req = tevent_req_callback_data(
5516 subreq, struct tevent_req);
5517 struct cli_shadow_copy_data_state *state = tevent_req_data(
5518 req, struct cli_shadow_copy_data_state);
5519 NTSTATUS status;
5521 status = cli_trans_recv(subreq, state, NULL,
5522 NULL, 0, NULL, /* setup */
5523 NULL, 0, NULL, /* param */
5524 &state->data, 12, &state->num_data);
5525 TALLOC_FREE(subreq);
5526 if (tevent_req_nterror(req, status)) {
5527 return;
5529 tevent_req_done(req);
5532 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5533 char ***pnames, int *pnum_names)
5535 struct cli_shadow_copy_data_state *state = tevent_req_data(
5536 req, struct cli_shadow_copy_data_state);
5537 char **names;
5538 int i, num_names;
5539 uint32_t dlength;
5540 NTSTATUS status;
5542 if (tevent_req_is_nterror(req, &status)) {
5543 return status;
5545 num_names = IVAL(state->data, 4);
5546 dlength = IVAL(state->data, 8);
5548 if (!state->get_names) {
5549 *pnum_names = num_names;
5550 return NT_STATUS_OK;
5553 if (dlength+12 > state->num_data) {
5554 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5556 names = talloc_array(mem_ctx, char *, num_names);
5557 if (names == NULL) {
5558 return NT_STATUS_NO_MEMORY;
5561 for (i=0; i<num_names; i++) {
5562 bool ret;
5563 uint8_t *src;
5564 size_t converted_size;
5566 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5567 ret = convert_string_talloc(
5568 names, CH_UTF16LE, CH_UNIX,
5569 src, 2 * sizeof(SHADOW_COPY_LABEL),
5570 &names[i], &converted_size);
5571 if (!ret) {
5572 TALLOC_FREE(names);
5573 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5576 *pnum_names = num_names;
5577 *pnames = names;
5578 return NT_STATUS_OK;
5581 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5582 uint16_t fnum, bool get_names,
5583 char ***pnames, int *pnum_names)
5585 TALLOC_CTX *frame = talloc_stackframe();
5586 struct tevent_context *ev;
5587 struct tevent_req *req;
5588 NTSTATUS status = NT_STATUS_NO_MEMORY;
5590 if (smbXcli_conn_has_async_calls(cli->conn)) {
5592 * Can't use sync call while an async call is in flight
5594 status = NT_STATUS_INVALID_PARAMETER;
5595 goto fail;
5597 ev = samba_tevent_context_init(frame);
5598 if (ev == NULL) {
5599 goto fail;
5601 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5602 if (req == NULL) {
5603 goto fail;
5605 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5606 goto fail;
5608 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5609 fail:
5610 TALLOC_FREE(frame);
5611 return status;