s3:libsmb: Plumb cli_smb2_mkdir() inside cli_mkdir().
[Samba/wip.git] / source3 / libsmb / clifile.c
blob9659fc1a8192b3fe8f30ff943ab14b8b5ef55c32
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
27 #include "trans2.h"
28 #include "ntioctl.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 /***********************************************************
33 Common function for pushing stings, used by smb_bytes_push_str()
34 and trans_bytes_push_str(). Only difference is the align_odd
35 parameter setting.
36 ***********************************************************/
38 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
39 const char *str, size_t str_len,
40 bool align_odd,
41 size_t *pconverted_size)
43 size_t buflen;
44 char *converted;
45 size_t converted_size;
47 if (buf == NULL) {
48 return NULL;
51 buflen = talloc_get_size(buf);
53 if (ucs2 &&
54 ((align_odd && (buflen % 2 == 0)) ||
55 (!align_odd && (buflen % 2 == 1)))) {
57 * We're pushing into an SMB buffer, align odd
59 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
60 if (buf == NULL) {
61 return NULL;
63 buf[buflen] = '\0';
64 buflen += 1;
67 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
68 ucs2 ? CH_UTF16LE : CH_DOS,
69 str, str_len, &converted,
70 &converted_size)) {
71 return NULL;
74 buf = talloc_realloc(NULL, buf, uint8_t,
75 buflen + converted_size);
76 if (buf == NULL) {
77 TALLOC_FREE(converted);
78 return NULL;
81 memcpy(buf + buflen, converted, converted_size);
83 TALLOC_FREE(converted);
85 if (pconverted_size) {
86 *pconverted_size = converted_size;
89 return buf;
92 /***********************************************************
93 Push a string into an SMB buffer, with odd byte alignment
94 if it's a UCS2 string.
95 ***********************************************************/
97 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
98 const char *str, size_t str_len,
99 size_t *pconverted_size)
101 return internal_bytes_push_str(buf, ucs2, str, str_len,
102 true, pconverted_size);
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
106 const uint8_t *bytes, size_t num_bytes)
108 size_t buflen;
110 if (buf == NULL) {
111 return NULL;
113 buflen = talloc_get_size(buf);
115 buf = talloc_realloc(NULL, buf, uint8_t,
116 buflen + 1 + num_bytes);
117 if (buf == NULL) {
118 return NULL;
120 buf[buflen] = prefix;
121 memcpy(&buf[buflen+1], bytes, num_bytes);
122 return buf;
125 /***********************************************************
126 Same as smb_bytes_push_str(), but without the odd byte
127 align for ucs2 (we're pushing into a param or data block).
128 static for now, although this will probably change when
129 other modules use async trans calls.
130 ***********************************************************/
132 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
133 const char *str, size_t str_len,
134 size_t *pconverted_size)
136 return internal_bytes_push_str(buf, ucs2, str, str_len,
137 false, pconverted_size);
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
141 const uint8_t *bytes, size_t num_bytes)
143 size_t buflen;
145 if (buf == NULL) {
146 return NULL;
148 buflen = talloc_get_size(buf);
150 buf = talloc_realloc(NULL, buf, uint8_t,
151 buflen + num_bytes);
152 if (buf == NULL) {
153 return NULL;
155 memcpy(&buf[buflen], bytes, num_bytes);
156 return buf;
159 struct cli_setpathinfo_state {
160 uint16_t setup;
161 uint8_t *param;
164 static void cli_setpathinfo_done(struct tevent_req *subreq);
166 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
167 struct tevent_context *ev,
168 struct cli_state *cli,
169 uint16_t level,
170 const char *path,
171 uint8_t *data,
172 size_t data_len)
174 struct tevent_req *req, *subreq;
175 struct cli_setpathinfo_state *state;
177 req = tevent_req_create(mem_ctx, &state,
178 struct cli_setpathinfo_state);
179 if (req == NULL) {
180 return NULL;
183 /* Setup setup word. */
184 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
186 /* Setup param array. */
187 state->param = talloc_zero_array(state, uint8_t, 6);
188 if (tevent_req_nomem(state->param, req)) {
189 return tevent_req_post(req, ev);
191 SSVAL(state->param, 0, level);
193 state->param = trans2_bytes_push_str(
194 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
195 if (tevent_req_nomem(state->param, req)) {
196 return tevent_req_post(req, ev);
199 subreq = cli_trans_send(
200 state, /* mem ctx. */
201 ev, /* event ctx. */
202 cli, /* cli_state. */
203 SMBtrans2, /* cmd. */
204 NULL, /* pipe name. */
205 -1, /* fid. */
206 0, /* function. */
207 0, /* flags. */
208 &state->setup, /* setup. */
209 1, /* num setup uint16_t words. */
210 0, /* max returned setup. */
211 state->param, /* param. */
212 talloc_get_size(state->param), /* num param. */
213 2, /* max returned param. */
214 data, /* data. */
215 data_len, /* num data. */
216 0); /* max returned data. */
218 if (tevent_req_nomem(subreq, req)) {
219 return tevent_req_post(req, ev);
221 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
222 return req;
225 static void cli_setpathinfo_done(struct tevent_req *subreq)
227 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
228 NULL, 0, NULL, NULL, 0, NULL);
229 tevent_req_simple_finish_ntstatus(subreq, status);
232 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
234 return tevent_req_simple_recv_ntstatus(req);
237 NTSTATUS cli_setpathinfo(struct cli_state *cli,
238 uint16_t level,
239 const char *path,
240 uint8_t *data,
241 size_t data_len)
243 TALLOC_CTX *frame = talloc_stackframe();
244 struct tevent_context *ev;
245 struct tevent_req *req;
246 NTSTATUS status = NT_STATUS_NO_MEMORY;
248 if (smbXcli_conn_has_async_calls(cli->conn)) {
250 * Can't use sync call while an async call is in flight
252 status = NT_STATUS_INVALID_PARAMETER;
253 goto fail;
255 ev = samba_tevent_context_init(frame);
256 if (ev == NULL) {
257 goto fail;
259 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
260 if (req == NULL) {
261 goto fail;
263 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
264 goto fail;
266 status = cli_setpathinfo_recv(req);
267 fail:
268 TALLOC_FREE(frame);
269 return status;
272 /****************************************************************************
273 Hard/Symlink a file (UNIX extensions).
274 Creates new name (sym)linked to oldname.
275 ****************************************************************************/
277 struct cli_posix_link_internal_state {
278 uint8_t *data;
281 static void cli_posix_link_internal_done(struct tevent_req *subreq);
283 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
284 struct tevent_context *ev,
285 struct cli_state *cli,
286 uint16_t level,
287 const char *oldname,
288 const char *newname)
290 struct tevent_req *req = NULL, *subreq = NULL;
291 struct cli_posix_link_internal_state *state = NULL;
293 req = tevent_req_create(mem_ctx, &state,
294 struct cli_posix_link_internal_state);
295 if (req == NULL) {
296 return NULL;
299 /* Setup data array. */
300 state->data = talloc_array(state, uint8_t, 0);
301 if (tevent_req_nomem(state->data, req)) {
302 return tevent_req_post(req, ev);
304 state->data = trans2_bytes_push_str(
305 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
307 subreq = cli_setpathinfo_send(
308 state, ev, cli, level, newname,
309 state->data, talloc_get_size(state->data));
310 if (tevent_req_nomem(subreq, req)) {
311 return tevent_req_post(req, ev);
313 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
314 return req;
317 static void cli_posix_link_internal_done(struct tevent_req *subreq)
319 NTSTATUS status = cli_setpathinfo_recv(subreq);
320 tevent_req_simple_finish_ntstatus(subreq, status);
323 /****************************************************************************
324 Symlink a file (UNIX extensions).
325 ****************************************************************************/
327 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
328 struct tevent_context *ev,
329 struct cli_state *cli,
330 const char *oldname,
331 const char *newname)
333 return cli_posix_link_internal_send(
334 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
337 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
339 return tevent_req_simple_recv_ntstatus(req);
342 NTSTATUS cli_posix_symlink(struct cli_state *cli,
343 const char *oldname,
344 const char *newname)
346 TALLOC_CTX *frame = talloc_stackframe();
347 struct tevent_context *ev = NULL;
348 struct tevent_req *req = NULL;
349 NTSTATUS status = NT_STATUS_OK;
351 if (smbXcli_conn_has_async_calls(cli->conn)) {
353 * Can't use sync call while an async call is in flight
355 status = NT_STATUS_INVALID_PARAMETER;
356 goto fail;
359 ev = samba_tevent_context_init(frame);
360 if (ev == NULL) {
361 status = NT_STATUS_NO_MEMORY;
362 goto fail;
365 req = cli_posix_symlink_send(frame,
367 cli,
368 oldname,
369 newname);
370 if (req == NULL) {
371 status = NT_STATUS_NO_MEMORY;
372 goto fail;
375 if (!tevent_req_poll(req, ev)) {
376 status = map_nt_error_from_unix(errno);
377 goto fail;
380 status = cli_posix_symlink_recv(req);
382 fail:
383 TALLOC_FREE(frame);
384 return status;
387 /****************************************************************************
388 Read a POSIX symlink.
389 ****************************************************************************/
391 struct readlink_state {
392 uint8_t *data;
393 uint32_t num_data;
396 static void cli_posix_readlink_done(struct tevent_req *subreq);
398 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
399 struct tevent_context *ev,
400 struct cli_state *cli,
401 const char *fname,
402 size_t len)
404 struct tevent_req *req = NULL, *subreq = NULL;
405 struct readlink_state *state = NULL;
406 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
408 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
409 if (req == NULL) {
410 return NULL;
414 * Len is in bytes, we need it in UCS2 units.
416 if ((2*len < len) || (maxbytelen < len)) {
417 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
418 return tevent_req_post(req, ev);
421 subreq = cli_qpathinfo_send(state, ev, cli, fname,
422 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
423 if (tevent_req_nomem(subreq, req)) {
424 return tevent_req_post(req, ev);
426 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
427 return req;
430 static void cli_posix_readlink_done(struct tevent_req *subreq)
432 struct tevent_req *req = tevent_req_callback_data(
433 subreq, struct tevent_req);
434 struct readlink_state *state = tevent_req_data(
435 req, struct readlink_state);
436 NTSTATUS status;
438 status = cli_qpathinfo_recv(subreq, state, &state->data,
439 &state->num_data);
440 TALLOC_FREE(subreq);
441 if (tevent_req_nterror(req, status)) {
442 return;
445 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
447 if (state->data[state->num_data-1] != '\0') {
448 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
449 return;
451 tevent_req_done(req);
454 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
455 char *retpath, size_t len)
457 NTSTATUS status;
458 char *converted = NULL;
459 size_t converted_size = 0;
460 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
462 if (tevent_req_is_nterror(req, &status)) {
463 return status;
465 /* The returned data is a pushed string, not raw data. */
466 if (!convert_string_talloc(state,
467 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
468 CH_UNIX,
469 state->data,
470 state->num_data,
471 &converted,
472 &converted_size)) {
473 return NT_STATUS_NO_MEMORY;
476 len = MIN(len,converted_size);
477 if (len == 0) {
478 return NT_STATUS_DATA_ERROR;
480 memcpy(retpath, converted, len);
481 return NT_STATUS_OK;
484 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
485 char *linkpath, size_t len)
487 TALLOC_CTX *frame = talloc_stackframe();
488 struct tevent_context *ev = NULL;
489 struct tevent_req *req = NULL;
490 NTSTATUS status = NT_STATUS_OK;
492 if (smbXcli_conn_has_async_calls(cli->conn)) {
494 * Can't use sync call while an async call is in flight
496 status = NT_STATUS_INVALID_PARAMETER;
497 goto fail;
500 ev = samba_tevent_context_init(frame);
501 if (ev == NULL) {
502 status = NT_STATUS_NO_MEMORY;
503 goto fail;
506 req = cli_posix_readlink_send(frame,
508 cli,
509 fname,
510 len);
511 if (req == NULL) {
512 status = NT_STATUS_NO_MEMORY;
513 goto fail;
516 if (!tevent_req_poll(req, ev)) {
517 status = map_nt_error_from_unix(errno);
518 goto fail;
521 status = cli_posix_readlink_recv(req, cli, linkpath, len);
523 fail:
524 TALLOC_FREE(frame);
525 return status;
528 /****************************************************************************
529 Hard link a file (UNIX extensions).
530 ****************************************************************************/
532 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
533 struct tevent_context *ev,
534 struct cli_state *cli,
535 const char *oldname,
536 const char *newname)
538 return cli_posix_link_internal_send(
539 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
542 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
544 return tevent_req_simple_recv_ntstatus(req);
547 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
548 const char *oldname,
549 const char *newname)
551 TALLOC_CTX *frame = talloc_stackframe();
552 struct tevent_context *ev = NULL;
553 struct tevent_req *req = NULL;
554 NTSTATUS status = NT_STATUS_OK;
556 if (smbXcli_conn_has_async_calls(cli->conn)) {
558 * Can't use sync call while an async call is in flight
560 status = NT_STATUS_INVALID_PARAMETER;
561 goto fail;
564 ev = samba_tevent_context_init(frame);
565 if (ev == NULL) {
566 status = NT_STATUS_NO_MEMORY;
567 goto fail;
570 req = cli_posix_hardlink_send(frame,
572 cli,
573 oldname,
574 newname);
575 if (req == NULL) {
576 status = NT_STATUS_NO_MEMORY;
577 goto fail;
580 if (!tevent_req_poll(req, ev)) {
581 status = map_nt_error_from_unix(errno);
582 goto fail;
585 status = cli_posix_hardlink_recv(req);
587 fail:
588 TALLOC_FREE(frame);
589 return status;
592 /****************************************************************************
593 Do a POSIX getfacl (UNIX extensions).
594 ****************************************************************************/
596 struct getfacl_state {
597 uint32_t num_data;
598 uint8_t *data;
601 static void cli_posix_getfacl_done(struct tevent_req *subreq);
603 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
604 struct tevent_context *ev,
605 struct cli_state *cli,
606 const char *fname)
608 struct tevent_req *req = NULL, *subreq = NULL;
609 struct getfacl_state *state = NULL;
611 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
612 if (req == NULL) {
613 return NULL;
615 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
616 0, CLI_BUFFER_SIZE);
617 if (tevent_req_nomem(subreq, req)) {
618 return tevent_req_post(req, ev);
620 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
621 return req;
624 static void cli_posix_getfacl_done(struct tevent_req *subreq)
626 struct tevent_req *req = tevent_req_callback_data(
627 subreq, struct tevent_req);
628 struct getfacl_state *state = tevent_req_data(
629 req, struct getfacl_state);
630 NTSTATUS status;
632 status = cli_qpathinfo_recv(subreq, state, &state->data,
633 &state->num_data);
634 TALLOC_FREE(subreq);
635 if (tevent_req_nterror(req, status)) {
636 return;
638 tevent_req_done(req);
641 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
642 TALLOC_CTX *mem_ctx,
643 size_t *prb_size,
644 char **retbuf)
646 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
647 NTSTATUS status;
649 if (tevent_req_is_nterror(req, &status)) {
650 return status;
652 *prb_size = (size_t)state->num_data;
653 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
654 return NT_STATUS_OK;
657 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
658 const char *fname,
659 TALLOC_CTX *mem_ctx,
660 size_t *prb_size,
661 char **retbuf)
663 TALLOC_CTX *frame = talloc_stackframe();
664 struct tevent_context *ev = NULL;
665 struct tevent_req *req = NULL;
666 NTSTATUS status = NT_STATUS_OK;
668 if (smbXcli_conn_has_async_calls(cli->conn)) {
670 * Can't use sync call while an async call is in flight
672 status = NT_STATUS_INVALID_PARAMETER;
673 goto fail;
676 ev = samba_tevent_context_init(frame);
677 if (ev == NULL) {
678 status = NT_STATUS_NO_MEMORY;
679 goto fail;
682 req = cli_posix_getfacl_send(frame,
684 cli,
685 fname);
686 if (req == NULL) {
687 status = NT_STATUS_NO_MEMORY;
688 goto fail;
691 if (!tevent_req_poll(req, ev)) {
692 status = map_nt_error_from_unix(errno);
693 goto fail;
696 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
698 fail:
699 TALLOC_FREE(frame);
700 return status;
703 /****************************************************************************
704 Stat a file (UNIX extensions).
705 ****************************************************************************/
707 struct stat_state {
708 uint32_t num_data;
709 uint8_t *data;
712 static void cli_posix_stat_done(struct tevent_req *subreq);
714 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
715 struct tevent_context *ev,
716 struct cli_state *cli,
717 const char *fname)
719 struct tevent_req *req = NULL, *subreq = NULL;
720 struct stat_state *state = NULL;
722 req = tevent_req_create(mem_ctx, &state, struct stat_state);
723 if (req == NULL) {
724 return NULL;
726 subreq = cli_qpathinfo_send(state, ev, cli, fname,
727 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
728 if (tevent_req_nomem(subreq, req)) {
729 return tevent_req_post(req, ev);
731 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
732 return req;
735 static void cli_posix_stat_done(struct tevent_req *subreq)
737 struct tevent_req *req = tevent_req_callback_data(
738 subreq, struct tevent_req);
739 struct stat_state *state = tevent_req_data(req, struct stat_state);
740 NTSTATUS status;
742 status = cli_qpathinfo_recv(subreq, state, &state->data,
743 &state->num_data);
744 TALLOC_FREE(subreq);
745 if (tevent_req_nterror(req, status)) {
746 return;
748 tevent_req_done(req);
751 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
752 SMB_STRUCT_STAT *sbuf)
754 struct stat_state *state = tevent_req_data(req, struct stat_state);
755 NTSTATUS status;
757 if (tevent_req_is_nterror(req, &status)) {
758 return status;
761 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
762 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
763 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
764 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
765 #else
766 /* assume 512 byte blocks */
767 sbuf->st_ex_blocks /= 512;
768 #endif
769 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
770 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
771 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
773 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
774 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
775 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
776 #if defined(HAVE_MAKEDEV)
778 uint32_t dev_major = IVAL(state->data,60);
779 uint32_t dev_minor = IVAL(state->data,68);
780 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
782 #endif
783 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
784 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
785 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
787 return NT_STATUS_OK;
790 NTSTATUS cli_posix_stat(struct cli_state *cli,
791 const char *fname,
792 SMB_STRUCT_STAT *sbuf)
794 TALLOC_CTX *frame = talloc_stackframe();
795 struct tevent_context *ev = NULL;
796 struct tevent_req *req = NULL;
797 NTSTATUS status = NT_STATUS_OK;
799 if (smbXcli_conn_has_async_calls(cli->conn)) {
801 * Can't use sync call while an async call is in flight
803 status = NT_STATUS_INVALID_PARAMETER;
804 goto fail;
807 ev = samba_tevent_context_init(frame);
808 if (ev == NULL) {
809 status = NT_STATUS_NO_MEMORY;
810 goto fail;
813 req = cli_posix_stat_send(frame,
815 cli,
816 fname);
817 if (req == NULL) {
818 status = NT_STATUS_NO_MEMORY;
819 goto fail;
822 if (!tevent_req_poll(req, ev)) {
823 status = map_nt_error_from_unix(errno);
824 goto fail;
827 status = cli_posix_stat_recv(req, sbuf);
829 fail:
830 TALLOC_FREE(frame);
831 return status;
834 /****************************************************************************
835 Chmod or chown a file internal (UNIX extensions).
836 ****************************************************************************/
838 struct cli_posix_chown_chmod_internal_state {
839 uint8_t data[100];
842 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
844 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
845 struct tevent_context *ev,
846 struct cli_state *cli,
847 const char *fname,
848 uint32_t mode,
849 uint32_t uid,
850 uint32_t gid)
852 struct tevent_req *req = NULL, *subreq = NULL;
853 struct cli_posix_chown_chmod_internal_state *state = NULL;
855 req = tevent_req_create(mem_ctx, &state,
856 struct cli_posix_chown_chmod_internal_state);
857 if (req == NULL) {
858 return NULL;
861 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
862 memset(&state->data[40], '\0', 60);
863 SIVAL(state->data,40,uid);
864 SIVAL(state->data,48,gid);
865 SIVAL(state->data,84,mode);
867 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
868 fname, state->data, sizeof(state->data));
869 if (tevent_req_nomem(subreq, req)) {
870 return tevent_req_post(req, ev);
872 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
873 req);
874 return req;
877 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
879 NTSTATUS status = cli_setpathinfo_recv(subreq);
880 tevent_req_simple_finish_ntstatus(subreq, status);
883 /****************************************************************************
884 chmod a file (UNIX extensions).
885 ****************************************************************************/
887 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
888 struct tevent_context *ev,
889 struct cli_state *cli,
890 const char *fname,
891 mode_t mode)
893 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
894 fname,
895 unix_perms_to_wire(mode),
896 SMB_UID_NO_CHANGE,
897 SMB_GID_NO_CHANGE);
900 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
902 return tevent_req_simple_recv_ntstatus(req);
905 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
907 TALLOC_CTX *frame = talloc_stackframe();
908 struct tevent_context *ev = NULL;
909 struct tevent_req *req = NULL;
910 NTSTATUS status = NT_STATUS_OK;
912 if (smbXcli_conn_has_async_calls(cli->conn)) {
914 * Can't use sync call while an async call is in flight
916 status = NT_STATUS_INVALID_PARAMETER;
917 goto fail;
920 ev = samba_tevent_context_init(frame);
921 if (ev == NULL) {
922 status = NT_STATUS_NO_MEMORY;
923 goto fail;
926 req = cli_posix_chmod_send(frame,
928 cli,
929 fname,
930 mode);
931 if (req == NULL) {
932 status = NT_STATUS_NO_MEMORY;
933 goto fail;
936 if (!tevent_req_poll(req, ev)) {
937 status = map_nt_error_from_unix(errno);
938 goto fail;
941 status = cli_posix_chmod_recv(req);
943 fail:
944 TALLOC_FREE(frame);
945 return status;
948 /****************************************************************************
949 chown a file (UNIX extensions).
950 ****************************************************************************/
952 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
953 struct tevent_context *ev,
954 struct cli_state *cli,
955 const char *fname,
956 uid_t uid,
957 gid_t gid)
959 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
960 fname,
961 SMB_MODE_NO_CHANGE,
962 (uint32_t)uid,
963 (uint32_t)gid);
966 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
968 return tevent_req_simple_recv_ntstatus(req);
971 NTSTATUS cli_posix_chown(struct cli_state *cli,
972 const char *fname,
973 uid_t uid,
974 gid_t gid)
976 TALLOC_CTX *frame = talloc_stackframe();
977 struct tevent_context *ev = NULL;
978 struct tevent_req *req = NULL;
979 NTSTATUS status = NT_STATUS_OK;
981 if (smbXcli_conn_has_async_calls(cli->conn)) {
983 * Can't use sync call while an async call is in flight
985 status = NT_STATUS_INVALID_PARAMETER;
986 goto fail;
989 ev = samba_tevent_context_init(frame);
990 if (ev == NULL) {
991 status = NT_STATUS_NO_MEMORY;
992 goto fail;
995 req = cli_posix_chown_send(frame,
997 cli,
998 fname,
999 uid,
1000 gid);
1001 if (req == NULL) {
1002 status = NT_STATUS_NO_MEMORY;
1003 goto fail;
1006 if (!tevent_req_poll(req, ev)) {
1007 status = map_nt_error_from_unix(errno);
1008 goto fail;
1011 status = cli_posix_chown_recv(req);
1013 fail:
1014 TALLOC_FREE(frame);
1015 return status;
1018 /****************************************************************************
1019 Rename a file.
1020 ****************************************************************************/
1022 static void cli_rename_done(struct tevent_req *subreq);
1024 struct cli_rename_state {
1025 uint16_t vwv[1];
1028 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1029 struct tevent_context *ev,
1030 struct cli_state *cli,
1031 const char *fname_src,
1032 const char *fname_dst)
1034 struct tevent_req *req = NULL, *subreq = NULL;
1035 struct cli_rename_state *state = NULL;
1036 uint8_t additional_flags = 0;
1037 uint8_t *bytes = NULL;
1039 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1040 if (req == NULL) {
1041 return NULL;
1044 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1046 bytes = talloc_array(state, uint8_t, 1);
1047 if (tevent_req_nomem(bytes, req)) {
1048 return tevent_req_post(req, ev);
1050 bytes[0] = 4;
1051 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1052 strlen(fname_src)+1, NULL);
1053 if (tevent_req_nomem(bytes, req)) {
1054 return tevent_req_post(req, ev);
1057 bytes = talloc_realloc(state, bytes, uint8_t,
1058 talloc_get_size(bytes)+1);
1059 if (tevent_req_nomem(bytes, req)) {
1060 return tevent_req_post(req, ev);
1063 bytes[talloc_get_size(bytes)-1] = 4;
1064 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1065 strlen(fname_dst)+1, NULL);
1066 if (tevent_req_nomem(bytes, req)) {
1067 return tevent_req_post(req, ev);
1070 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1071 1, state->vwv, talloc_get_size(bytes), bytes);
1072 if (tevent_req_nomem(subreq, req)) {
1073 return tevent_req_post(req, ev);
1075 tevent_req_set_callback(subreq, cli_rename_done, req);
1076 return req;
1079 static void cli_rename_done(struct tevent_req *subreq)
1081 struct tevent_req *req = tevent_req_callback_data(
1082 subreq, struct tevent_req);
1083 NTSTATUS status;
1085 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1086 TALLOC_FREE(subreq);
1087 if (tevent_req_nterror(req, status)) {
1088 return;
1090 tevent_req_done(req);
1093 NTSTATUS cli_rename_recv(struct tevent_req *req)
1095 return tevent_req_simple_recv_ntstatus(req);
1098 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1100 TALLOC_CTX *frame = NULL;
1101 struct tevent_context *ev;
1102 struct tevent_req *req;
1103 NTSTATUS status = NT_STATUS_OK;
1105 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1106 return cli_smb2_rename(cli,
1107 fname_src,
1108 fname_dst);
1111 frame = talloc_stackframe();
1113 if (smbXcli_conn_has_async_calls(cli->conn)) {
1115 * Can't use sync call while an async call is in flight
1117 status = NT_STATUS_INVALID_PARAMETER;
1118 goto fail;
1121 ev = samba_tevent_context_init(frame);
1122 if (ev == NULL) {
1123 status = NT_STATUS_NO_MEMORY;
1124 goto fail;
1127 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1128 if (req == NULL) {
1129 status = NT_STATUS_NO_MEMORY;
1130 goto fail;
1133 if (!tevent_req_poll(req, ev)) {
1134 status = map_nt_error_from_unix(errno);
1135 goto fail;
1138 status = cli_rename_recv(req);
1140 fail:
1141 TALLOC_FREE(frame);
1142 return status;
1145 /****************************************************************************
1146 NT Rename a file.
1147 ****************************************************************************/
1149 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1151 struct cli_ntrename_internal_state {
1152 uint16_t vwv[4];
1155 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1156 struct tevent_context *ev,
1157 struct cli_state *cli,
1158 const char *fname_src,
1159 const char *fname_dst,
1160 uint16_t rename_flag)
1162 struct tevent_req *req = NULL, *subreq = NULL;
1163 struct cli_ntrename_internal_state *state = NULL;
1164 uint8_t additional_flags = 0;
1165 uint8_t *bytes = NULL;
1167 req = tevent_req_create(mem_ctx, &state,
1168 struct cli_ntrename_internal_state);
1169 if (req == NULL) {
1170 return NULL;
1173 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1174 SSVAL(state->vwv+1, 0, rename_flag);
1176 bytes = talloc_array(state, uint8_t, 1);
1177 if (tevent_req_nomem(bytes, req)) {
1178 return tevent_req_post(req, ev);
1180 bytes[0] = 4;
1181 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1182 strlen(fname_src)+1, NULL);
1183 if (tevent_req_nomem(bytes, req)) {
1184 return tevent_req_post(req, ev);
1187 bytes = talloc_realloc(state, bytes, uint8_t,
1188 talloc_get_size(bytes)+1);
1189 if (tevent_req_nomem(bytes, req)) {
1190 return tevent_req_post(req, ev);
1193 bytes[talloc_get_size(bytes)-1] = 4;
1194 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1195 strlen(fname_dst)+1, NULL);
1196 if (tevent_req_nomem(bytes, req)) {
1197 return tevent_req_post(req, ev);
1200 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1201 4, state->vwv, talloc_get_size(bytes), bytes);
1202 if (tevent_req_nomem(subreq, req)) {
1203 return tevent_req_post(req, ev);
1205 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1206 return req;
1209 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1211 struct tevent_req *req = tevent_req_callback_data(
1212 subreq, struct tevent_req);
1213 NTSTATUS status;
1215 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1216 TALLOC_FREE(subreq);
1217 if (tevent_req_nterror(req, status)) {
1218 return;
1220 tevent_req_done(req);
1223 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1225 return tevent_req_simple_recv_ntstatus(req);
1228 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1229 struct tevent_context *ev,
1230 struct cli_state *cli,
1231 const char *fname_src,
1232 const char *fname_dst)
1234 return cli_ntrename_internal_send(mem_ctx,
1236 cli,
1237 fname_src,
1238 fname_dst,
1239 RENAME_FLAG_RENAME);
1242 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1244 return cli_ntrename_internal_recv(req);
1247 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1249 TALLOC_CTX *frame = talloc_stackframe();
1250 struct tevent_context *ev;
1251 struct tevent_req *req;
1252 NTSTATUS status = NT_STATUS_OK;
1254 if (smbXcli_conn_has_async_calls(cli->conn)) {
1256 * Can't use sync call while an async call is in flight
1258 status = NT_STATUS_INVALID_PARAMETER;
1259 goto fail;
1262 ev = samba_tevent_context_init(frame);
1263 if (ev == NULL) {
1264 status = NT_STATUS_NO_MEMORY;
1265 goto fail;
1268 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1269 if (req == NULL) {
1270 status = NT_STATUS_NO_MEMORY;
1271 goto fail;
1274 if (!tevent_req_poll(req, ev)) {
1275 status = map_nt_error_from_unix(errno);
1276 goto fail;
1279 status = cli_ntrename_recv(req);
1281 fail:
1282 TALLOC_FREE(frame);
1283 return status;
1286 /****************************************************************************
1287 NT hardlink a file.
1288 ****************************************************************************/
1290 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1291 struct tevent_context *ev,
1292 struct cli_state *cli,
1293 const char *fname_src,
1294 const char *fname_dst)
1296 return cli_ntrename_internal_send(mem_ctx,
1298 cli,
1299 fname_src,
1300 fname_dst,
1301 RENAME_FLAG_HARD_LINK);
1304 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1306 return cli_ntrename_internal_recv(req);
1309 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1311 TALLOC_CTX *frame = talloc_stackframe();
1312 struct tevent_context *ev;
1313 struct tevent_req *req;
1314 NTSTATUS status = NT_STATUS_OK;
1316 if (smbXcli_conn_has_async_calls(cli->conn)) {
1318 * Can't use sync call while an async call is in flight
1320 status = NT_STATUS_INVALID_PARAMETER;
1321 goto fail;
1324 ev = samba_tevent_context_init(frame);
1325 if (ev == NULL) {
1326 status = NT_STATUS_NO_MEMORY;
1327 goto fail;
1330 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1331 if (req == NULL) {
1332 status = NT_STATUS_NO_MEMORY;
1333 goto fail;
1336 if (!tevent_req_poll(req, ev)) {
1337 status = map_nt_error_from_unix(errno);
1338 goto fail;
1341 status = cli_nt_hardlink_recv(req);
1343 fail:
1344 TALLOC_FREE(frame);
1345 return status;
1348 /****************************************************************************
1349 Delete a file.
1350 ****************************************************************************/
1352 static void cli_unlink_done(struct tevent_req *subreq);
1354 struct cli_unlink_state {
1355 uint16_t vwv[1];
1358 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1359 struct tevent_context *ev,
1360 struct cli_state *cli,
1361 const char *fname,
1362 uint16_t mayhave_attrs)
1364 struct tevent_req *req = NULL, *subreq = NULL;
1365 struct cli_unlink_state *state = NULL;
1366 uint8_t additional_flags = 0;
1367 uint8_t *bytes = NULL;
1369 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1370 if (req == NULL) {
1371 return NULL;
1374 SSVAL(state->vwv+0, 0, mayhave_attrs);
1376 bytes = talloc_array(state, uint8_t, 1);
1377 if (tevent_req_nomem(bytes, req)) {
1378 return tevent_req_post(req, ev);
1380 bytes[0] = 4;
1381 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1382 strlen(fname)+1, NULL);
1384 if (tevent_req_nomem(bytes, req)) {
1385 return tevent_req_post(req, ev);
1388 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1389 1, state->vwv, talloc_get_size(bytes), bytes);
1390 if (tevent_req_nomem(subreq, req)) {
1391 return tevent_req_post(req, ev);
1393 tevent_req_set_callback(subreq, cli_unlink_done, req);
1394 return req;
1397 static void cli_unlink_done(struct tevent_req *subreq)
1399 struct tevent_req *req = tevent_req_callback_data(
1400 subreq, struct tevent_req);
1401 NTSTATUS status;
1403 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1404 TALLOC_FREE(subreq);
1405 if (tevent_req_nterror(req, status)) {
1406 return;
1408 tevent_req_done(req);
1411 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1413 return tevent_req_simple_recv_ntstatus(req);
1416 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1418 TALLOC_CTX *frame = NULL;
1419 struct tevent_context *ev;
1420 struct tevent_req *req;
1421 NTSTATUS status = NT_STATUS_OK;
1423 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1424 return cli_smb2_unlink(cli, fname);
1427 frame = talloc_stackframe();
1429 if (smbXcli_conn_has_async_calls(cli->conn)) {
1431 * Can't use sync call while an async call is in flight
1433 status = NT_STATUS_INVALID_PARAMETER;
1434 goto fail;
1437 ev = samba_tevent_context_init(frame);
1438 if (ev == NULL) {
1439 status = NT_STATUS_NO_MEMORY;
1440 goto fail;
1443 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1444 if (req == NULL) {
1445 status = NT_STATUS_NO_MEMORY;
1446 goto fail;
1449 if (!tevent_req_poll(req, ev)) {
1450 status = map_nt_error_from_unix(errno);
1451 goto fail;
1454 status = cli_unlink_recv(req);
1456 fail:
1457 TALLOC_FREE(frame);
1458 return status;
1461 /****************************************************************************
1462 Create a directory.
1463 ****************************************************************************/
1465 static void cli_mkdir_done(struct tevent_req *subreq);
1467 struct cli_mkdir_state {
1468 int dummy;
1471 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1472 struct tevent_context *ev,
1473 struct cli_state *cli,
1474 const char *dname)
1476 struct tevent_req *req = NULL, *subreq = NULL;
1477 struct cli_mkdir_state *state = NULL;
1478 uint8_t additional_flags = 0;
1479 uint8_t *bytes = NULL;
1481 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1482 if (req == NULL) {
1483 return NULL;
1486 bytes = talloc_array(state, uint8_t, 1);
1487 if (tevent_req_nomem(bytes, req)) {
1488 return tevent_req_post(req, ev);
1490 bytes[0] = 4;
1491 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1492 strlen(dname)+1, NULL);
1494 if (tevent_req_nomem(bytes, req)) {
1495 return tevent_req_post(req, ev);
1498 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1499 0, NULL, talloc_get_size(bytes), bytes);
1500 if (tevent_req_nomem(subreq, req)) {
1501 return tevent_req_post(req, ev);
1503 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1504 return req;
1507 static void cli_mkdir_done(struct tevent_req *subreq)
1509 struct tevent_req *req = tevent_req_callback_data(
1510 subreq, struct tevent_req);
1511 NTSTATUS status;
1513 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1514 TALLOC_FREE(subreq);
1515 if (tevent_req_nterror(req, status)) {
1516 return;
1518 tevent_req_done(req);
1521 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1523 return tevent_req_simple_recv_ntstatus(req);
1526 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1528 TALLOC_CTX *frame = NULL;
1529 struct tevent_context *ev;
1530 struct tevent_req *req;
1531 NTSTATUS status = NT_STATUS_OK;
1533 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1534 return cli_smb2_mkdir(cli, dname);
1537 frame = talloc_stackframe();
1539 if (smbXcli_conn_has_async_calls(cli->conn)) {
1541 * Can't use sync call while an async call is in flight
1543 status = NT_STATUS_INVALID_PARAMETER;
1544 goto fail;
1547 ev = samba_tevent_context_init(frame);
1548 if (ev == NULL) {
1549 status = NT_STATUS_NO_MEMORY;
1550 goto fail;
1553 req = cli_mkdir_send(frame, ev, cli, dname);
1554 if (req == NULL) {
1555 status = NT_STATUS_NO_MEMORY;
1556 goto fail;
1559 if (!tevent_req_poll(req, ev)) {
1560 status = map_nt_error_from_unix(errno);
1561 goto fail;
1564 status = cli_mkdir_recv(req);
1566 fail:
1567 TALLOC_FREE(frame);
1568 return status;
1571 /****************************************************************************
1572 Remove a directory.
1573 ****************************************************************************/
1575 static void cli_rmdir_done(struct tevent_req *subreq);
1577 struct cli_rmdir_state {
1578 int dummy;
1581 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1582 struct tevent_context *ev,
1583 struct cli_state *cli,
1584 const char *dname)
1586 struct tevent_req *req = NULL, *subreq = NULL;
1587 struct cli_rmdir_state *state = NULL;
1588 uint8_t additional_flags = 0;
1589 uint8_t *bytes = NULL;
1591 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1592 if (req == NULL) {
1593 return NULL;
1596 bytes = talloc_array(state, uint8_t, 1);
1597 if (tevent_req_nomem(bytes, req)) {
1598 return tevent_req_post(req, ev);
1600 bytes[0] = 4;
1601 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1602 strlen(dname)+1, NULL);
1604 if (tevent_req_nomem(bytes, req)) {
1605 return tevent_req_post(req, ev);
1608 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1609 0, NULL, talloc_get_size(bytes), bytes);
1610 if (tevent_req_nomem(subreq, req)) {
1611 return tevent_req_post(req, ev);
1613 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1614 return req;
1617 static void cli_rmdir_done(struct tevent_req *subreq)
1619 struct tevent_req *req = tevent_req_callback_data(
1620 subreq, struct tevent_req);
1621 NTSTATUS status;
1623 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1624 TALLOC_FREE(subreq);
1625 if (tevent_req_nterror(req, status)) {
1626 return;
1628 tevent_req_done(req);
1631 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1633 return tevent_req_simple_recv_ntstatus(req);
1636 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1638 TALLOC_CTX *frame = talloc_stackframe();
1639 struct tevent_context *ev;
1640 struct tevent_req *req;
1641 NTSTATUS status = NT_STATUS_OK;
1643 if (smbXcli_conn_has_async_calls(cli->conn)) {
1645 * Can't use sync call while an async call is in flight
1647 status = NT_STATUS_INVALID_PARAMETER;
1648 goto fail;
1651 ev = samba_tevent_context_init(frame);
1652 if (ev == NULL) {
1653 status = NT_STATUS_NO_MEMORY;
1654 goto fail;
1657 req = cli_rmdir_send(frame, ev, cli, dname);
1658 if (req == NULL) {
1659 status = NT_STATUS_NO_MEMORY;
1660 goto fail;
1663 if (!tevent_req_poll(req, ev)) {
1664 status = map_nt_error_from_unix(errno);
1665 goto fail;
1668 status = cli_rmdir_recv(req);
1670 fail:
1671 TALLOC_FREE(frame);
1672 return status;
1675 /****************************************************************************
1676 Set or clear the delete on close flag.
1677 ****************************************************************************/
1679 struct doc_state {
1680 uint16_t setup;
1681 uint8_t param[6];
1682 uint8_t data[1];
1685 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1687 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1688 NULL, 0, NULL, NULL, 0, NULL);
1689 tevent_req_simple_finish_ntstatus(subreq, status);
1692 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1693 struct tevent_context *ev,
1694 struct cli_state *cli,
1695 uint16_t fnum,
1696 bool flag)
1698 struct tevent_req *req = NULL, *subreq = NULL;
1699 struct doc_state *state = NULL;
1701 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1702 if (req == NULL) {
1703 return NULL;
1706 /* Setup setup word. */
1707 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1709 /* Setup param array. */
1710 SSVAL(state->param,0,fnum);
1711 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1713 /* Setup data array. */
1714 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1716 subreq = cli_trans_send(state, /* mem ctx. */
1717 ev, /* event ctx. */
1718 cli, /* cli_state. */
1719 SMBtrans2, /* cmd. */
1720 NULL, /* pipe name. */
1721 -1, /* fid. */
1722 0, /* function. */
1723 0, /* flags. */
1724 &state->setup, /* setup. */
1725 1, /* num setup uint16_t words. */
1726 0, /* max returned setup. */
1727 state->param, /* param. */
1728 6, /* num param. */
1729 2, /* max returned param. */
1730 state->data, /* data. */
1731 1, /* num data. */
1732 0); /* max returned data. */
1734 if (tevent_req_nomem(subreq, req)) {
1735 return tevent_req_post(req, ev);
1737 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1738 return req;
1741 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1743 return tevent_req_simple_recv_ntstatus(req);
1746 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1748 TALLOC_CTX *frame = talloc_stackframe();
1749 struct tevent_context *ev = NULL;
1750 struct tevent_req *req = NULL;
1751 NTSTATUS status = NT_STATUS_OK;
1753 if (smbXcli_conn_has_async_calls(cli->conn)) {
1755 * Can't use sync call while an async call is in flight
1757 status = NT_STATUS_INVALID_PARAMETER;
1758 goto fail;
1761 ev = samba_tevent_context_init(frame);
1762 if (ev == NULL) {
1763 status = NT_STATUS_NO_MEMORY;
1764 goto fail;
1767 req = cli_nt_delete_on_close_send(frame,
1769 cli,
1770 fnum,
1771 flag);
1772 if (req == NULL) {
1773 status = NT_STATUS_NO_MEMORY;
1774 goto fail;
1777 if (!tevent_req_poll(req, ev)) {
1778 status = map_nt_error_from_unix(errno);
1779 goto fail;
1782 status = cli_nt_delete_on_close_recv(req);
1784 fail:
1785 TALLOC_FREE(frame);
1786 return status;
1789 struct cli_ntcreate_state {
1790 uint16_t vwv[24];
1791 uint16_t fnum;
1794 static void cli_ntcreate_done(struct tevent_req *subreq);
1796 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1797 struct tevent_context *ev,
1798 struct cli_state *cli,
1799 const char *fname,
1800 uint32_t CreatFlags,
1801 uint32_t DesiredAccess,
1802 uint32_t FileAttributes,
1803 uint32_t ShareAccess,
1804 uint32_t CreateDisposition,
1805 uint32_t CreateOptions,
1806 uint8_t SecurityFlags)
1808 struct tevent_req *req, *subreq;
1809 struct cli_ntcreate_state *state;
1810 uint16_t *vwv;
1811 uint8_t *bytes;
1812 size_t converted_len;
1814 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1815 if (req == NULL) {
1816 return NULL;
1819 vwv = state->vwv;
1821 SCVAL(vwv+0, 0, 0xFF);
1822 SCVAL(vwv+0, 1, 0);
1823 SSVAL(vwv+1, 0, 0);
1824 SCVAL(vwv+2, 0, 0);
1826 if (cli->use_oplocks) {
1827 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1829 SIVAL(vwv+3, 1, CreatFlags);
1830 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1831 SIVAL(vwv+7, 1, DesiredAccess);
1832 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1833 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1834 SIVAL(vwv+13, 1, FileAttributes);
1835 SIVAL(vwv+15, 1, ShareAccess);
1836 SIVAL(vwv+17, 1, CreateDisposition);
1837 SIVAL(vwv+19, 1, CreateOptions |
1838 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1839 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1840 SCVAL(vwv+23, 1, SecurityFlags);
1842 bytes = talloc_array(state, uint8_t, 0);
1843 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1844 fname, strlen(fname)+1,
1845 &converted_len);
1847 /* sigh. this copes with broken netapp filer behaviour */
1848 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1850 if (tevent_req_nomem(bytes, req)) {
1851 return tevent_req_post(req, ev);
1854 SSVAL(vwv+2, 1, converted_len);
1856 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1857 talloc_get_size(bytes), bytes);
1858 if (tevent_req_nomem(subreq, req)) {
1859 return tevent_req_post(req, ev);
1861 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1862 return req;
1865 static void cli_ntcreate_done(struct tevent_req *subreq)
1867 struct tevent_req *req = tevent_req_callback_data(
1868 subreq, struct tevent_req);
1869 struct cli_ntcreate_state *state = tevent_req_data(
1870 req, struct cli_ntcreate_state);
1871 uint8_t wct;
1872 uint16_t *vwv;
1873 uint32_t num_bytes;
1874 uint8_t *bytes;
1875 NTSTATUS status;
1877 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv,
1878 &num_bytes, &bytes);
1879 TALLOC_FREE(subreq);
1880 if (tevent_req_nterror(req, status)) {
1881 return;
1883 state->fnum = SVAL(vwv+2, 1);
1884 tevent_req_done(req);
1887 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1889 struct cli_ntcreate_state *state = tevent_req_data(
1890 req, struct cli_ntcreate_state);
1891 NTSTATUS status;
1893 if (tevent_req_is_nterror(req, &status)) {
1894 return status;
1896 *pfnum = state->fnum;
1897 return NT_STATUS_OK;
1900 NTSTATUS cli_ntcreate(struct cli_state *cli,
1901 const char *fname,
1902 uint32_t CreatFlags,
1903 uint32_t DesiredAccess,
1904 uint32_t FileAttributes,
1905 uint32_t ShareAccess,
1906 uint32_t CreateDisposition,
1907 uint32_t CreateOptions,
1908 uint8_t SecurityFlags,
1909 uint16_t *pfid)
1911 TALLOC_CTX *frame = talloc_stackframe();
1912 struct tevent_context *ev;
1913 struct tevent_req *req;
1914 NTSTATUS status = NT_STATUS_OK;
1916 if (smbXcli_conn_has_async_calls(cli->conn)) {
1918 * Can't use sync call while an async call is in flight
1920 status = NT_STATUS_INVALID_PARAMETER;
1921 goto fail;
1924 ev = samba_tevent_context_init(frame);
1925 if (ev == NULL) {
1926 status = NT_STATUS_NO_MEMORY;
1927 goto fail;
1930 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1931 DesiredAccess, FileAttributes, ShareAccess,
1932 CreateDisposition, CreateOptions,
1933 SecurityFlags);
1934 if (req == NULL) {
1935 status = NT_STATUS_NO_MEMORY;
1936 goto fail;
1939 if (!tevent_req_poll(req, ev)) {
1940 status = map_nt_error_from_unix(errno);
1941 goto fail;
1944 status = cli_ntcreate_recv(req, pfid);
1945 fail:
1946 TALLOC_FREE(frame);
1947 return status;
1950 struct cli_nttrans_create_state {
1951 uint16_t fnum;
1954 static void cli_nttrans_create_done(struct tevent_req *subreq);
1956 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
1957 struct tevent_context *ev,
1958 struct cli_state *cli,
1959 const char *fname,
1960 uint32_t CreatFlags,
1961 uint32_t DesiredAccess,
1962 uint32_t FileAttributes,
1963 uint32_t ShareAccess,
1964 uint32_t CreateDisposition,
1965 uint32_t CreateOptions,
1966 uint8_t SecurityFlags,
1967 struct security_descriptor *secdesc,
1968 struct ea_struct *eas,
1969 int num_eas)
1971 struct tevent_req *req, *subreq;
1972 struct cli_nttrans_create_state *state;
1973 uint8_t *param;
1974 uint8_t *secdesc_buf;
1975 size_t secdesc_len;
1976 NTSTATUS status;
1977 size_t converted_len;
1979 req = tevent_req_create(mem_ctx,
1980 &state, struct cli_nttrans_create_state);
1981 if (req == NULL) {
1982 return NULL;
1985 if (secdesc != NULL) {
1986 status = marshall_sec_desc(talloc_tos(), secdesc,
1987 &secdesc_buf, &secdesc_len);
1988 if (tevent_req_nterror(req, status)) {
1989 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1990 nt_errstr(status)));
1991 return tevent_req_post(req, ev);
1993 } else {
1994 secdesc_buf = NULL;
1995 secdesc_len = 0;
1998 if (num_eas != 0) {
2000 * TODO ;-)
2002 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2003 return tevent_req_post(req, ev);
2006 param = talloc_array(state, uint8_t, 53);
2007 if (tevent_req_nomem(param, req)) {
2008 return tevent_req_post(req, ev);
2011 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2012 fname, strlen(fname),
2013 &converted_len);
2014 if (tevent_req_nomem(param, req)) {
2015 return tevent_req_post(req, ev);
2018 SIVAL(param, 0, CreatFlags);
2019 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2020 SIVAL(param, 8, DesiredAccess);
2021 SIVAL(param, 12, 0x0); /* AllocationSize */
2022 SIVAL(param, 16, 0x0); /* AllocationSize */
2023 SIVAL(param, 20, FileAttributes);
2024 SIVAL(param, 24, ShareAccess);
2025 SIVAL(param, 28, CreateDisposition);
2026 SIVAL(param, 32, CreateOptions |
2027 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2028 SIVAL(param, 36, secdesc_len);
2029 SIVAL(param, 40, 0); /* EA length*/
2030 SIVAL(param, 44, converted_len);
2031 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2032 SCVAL(param, 52, SecurityFlags);
2034 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2035 NULL, -1, /* name, fid */
2036 NT_TRANSACT_CREATE, 0,
2037 NULL, 0, 0, /* setup */
2038 param, talloc_get_size(param), 128, /* param */
2039 secdesc_buf, secdesc_len, 0); /* data */
2040 if (tevent_req_nomem(subreq, req)) {
2041 return tevent_req_post(req, ev);
2043 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2044 return req;
2047 static void cli_nttrans_create_done(struct tevent_req *subreq)
2049 struct tevent_req *req = tevent_req_callback_data(
2050 subreq, struct tevent_req);
2051 struct cli_nttrans_create_state *state = tevent_req_data(
2052 req, struct cli_nttrans_create_state);
2053 uint8_t *param;
2054 uint32_t num_param;
2055 NTSTATUS status;
2057 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2058 NULL, 0, NULL, /* rsetup */
2059 &param, 69, &num_param,
2060 NULL, 0, NULL);
2061 if (tevent_req_nterror(req, status)) {
2062 return;
2064 state->fnum = SVAL(param, 2);
2065 TALLOC_FREE(param);
2066 tevent_req_done(req);
2069 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
2071 struct cli_nttrans_create_state *state = tevent_req_data(
2072 req, struct cli_nttrans_create_state);
2073 NTSTATUS status;
2075 if (tevent_req_is_nterror(req, &status)) {
2076 return status;
2078 *fnum = state->fnum;
2079 return NT_STATUS_OK;
2082 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2083 const char *fname,
2084 uint32_t CreatFlags,
2085 uint32_t DesiredAccess,
2086 uint32_t FileAttributes,
2087 uint32_t ShareAccess,
2088 uint32_t CreateDisposition,
2089 uint32_t CreateOptions,
2090 uint8_t SecurityFlags,
2091 struct security_descriptor *secdesc,
2092 struct ea_struct *eas,
2093 int num_eas,
2094 uint16_t *pfid)
2096 TALLOC_CTX *frame = talloc_stackframe();
2097 struct tevent_context *ev;
2098 struct tevent_req *req;
2099 NTSTATUS status = NT_STATUS_NO_MEMORY;
2101 if (smbXcli_conn_has_async_calls(cli->conn)) {
2103 * Can't use sync call while an async call is in flight
2105 status = NT_STATUS_INVALID_PARAMETER;
2106 goto fail;
2108 ev = samba_tevent_context_init(frame);
2109 if (ev == NULL) {
2110 goto fail;
2112 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2113 DesiredAccess, FileAttributes,
2114 ShareAccess, CreateDisposition,
2115 CreateOptions, SecurityFlags,
2116 secdesc, eas, num_eas);
2117 if (req == NULL) {
2118 goto fail;
2120 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2121 goto fail;
2123 status = cli_nttrans_create_recv(req, pfid);
2124 fail:
2125 TALLOC_FREE(frame);
2126 return status;
2129 /****************************************************************************
2130 Open a file
2131 WARNING: if you open with O_WRONLY then getattrE won't work!
2132 ****************************************************************************/
2134 struct cli_openx_state {
2135 const char *fname;
2136 uint16_t vwv[15];
2137 uint16_t fnum;
2138 struct iovec bytes;
2141 static void cli_openx_done(struct tevent_req *subreq);
2143 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2144 struct tevent_context *ev,
2145 struct cli_state *cli, const char *fname,
2146 int flags, int share_mode,
2147 struct tevent_req **psmbreq)
2149 struct tevent_req *req, *subreq;
2150 struct cli_openx_state *state;
2151 unsigned openfn;
2152 unsigned accessmode;
2153 uint8_t additional_flags;
2154 uint8_t *bytes;
2156 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2157 if (req == NULL) {
2158 return NULL;
2161 openfn = 0;
2162 if (flags & O_CREAT) {
2163 openfn |= (1<<4);
2165 if (!(flags & O_EXCL)) {
2166 if (flags & O_TRUNC)
2167 openfn |= (1<<1);
2168 else
2169 openfn |= (1<<0);
2172 accessmode = (share_mode<<4);
2174 if ((flags & O_ACCMODE) == O_RDWR) {
2175 accessmode |= 2;
2176 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2177 accessmode |= 1;
2180 #if defined(O_SYNC)
2181 if ((flags & O_SYNC) == O_SYNC) {
2182 accessmode |= (1<<14);
2184 #endif /* O_SYNC */
2186 if (share_mode == DENY_FCB) {
2187 accessmode = 0xFF;
2190 SCVAL(state->vwv + 0, 0, 0xFF);
2191 SCVAL(state->vwv + 0, 1, 0);
2192 SSVAL(state->vwv + 1, 0, 0);
2193 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2194 SSVAL(state->vwv + 3, 0, accessmode);
2195 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2196 SSVAL(state->vwv + 5, 0, 0);
2197 SIVAL(state->vwv + 6, 0, 0);
2198 SSVAL(state->vwv + 8, 0, openfn);
2199 SIVAL(state->vwv + 9, 0, 0);
2200 SIVAL(state->vwv + 11, 0, 0);
2201 SIVAL(state->vwv + 13, 0, 0);
2203 additional_flags = 0;
2205 if (cli->use_oplocks) {
2206 /* if using oplocks then ask for a batch oplock via
2207 core and extended methods */
2208 additional_flags =
2209 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2210 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2213 bytes = talloc_array(state, uint8_t, 0);
2214 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2215 strlen(fname)+1, NULL);
2217 if (tevent_req_nomem(bytes, req)) {
2218 return tevent_req_post(req, ev);
2221 state->bytes.iov_base = (void *)bytes;
2222 state->bytes.iov_len = talloc_get_size(bytes);
2224 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2225 15, state->vwv, 1, &state->bytes);
2226 if (subreq == NULL) {
2227 TALLOC_FREE(req);
2228 return NULL;
2230 tevent_req_set_callback(subreq, cli_openx_done, req);
2231 *psmbreq = subreq;
2232 return req;
2235 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2236 struct cli_state *cli, const char *fname,
2237 int flags, int share_mode)
2239 struct tevent_req *req, *subreq;
2240 NTSTATUS status;
2242 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2243 &subreq);
2244 if (req == NULL) {
2245 return NULL;
2248 status = smb1cli_req_chain_submit(&subreq, 1);
2249 if (tevent_req_nterror(req, status)) {
2250 return tevent_req_post(req, ev);
2252 return req;
2255 static void cli_openx_done(struct tevent_req *subreq)
2257 struct tevent_req *req = tevent_req_callback_data(
2258 subreq, struct tevent_req);
2259 struct cli_openx_state *state = tevent_req_data(
2260 req, struct cli_openx_state);
2261 uint8_t wct;
2262 uint16_t *vwv;
2263 NTSTATUS status;
2265 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2266 NULL);
2267 TALLOC_FREE(subreq);
2268 if (tevent_req_nterror(req, status)) {
2269 return;
2271 state->fnum = SVAL(vwv+2, 0);
2272 tevent_req_done(req);
2275 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2277 struct cli_openx_state *state = tevent_req_data(
2278 req, struct cli_openx_state);
2279 NTSTATUS status;
2281 if (tevent_req_is_nterror(req, &status)) {
2282 return status;
2284 *pfnum = state->fnum;
2285 return NT_STATUS_OK;
2288 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2289 int share_mode, uint16_t *pfnum)
2291 TALLOC_CTX *frame = talloc_stackframe();
2292 struct tevent_context *ev;
2293 struct tevent_req *req;
2294 NTSTATUS status = NT_STATUS_NO_MEMORY;
2296 if (smbXcli_conn_has_async_calls(cli->conn)) {
2298 * Can't use sync call while an async call is in flight
2300 status = NT_STATUS_INVALID_PARAMETER;
2301 goto fail;
2304 ev = samba_tevent_context_init(frame);
2305 if (ev == NULL) {
2306 goto fail;
2309 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2310 if (req == NULL) {
2311 goto fail;
2314 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2315 goto fail;
2318 status = cli_openx_recv(req, pfnum);
2319 fail:
2320 TALLOC_FREE(frame);
2321 return status;
2323 /****************************************************************************
2324 Synchronous wrapper function that does an NtCreateX open by preference
2325 and falls back to openX if this fails.
2326 ****************************************************************************/
2328 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2329 int share_mode_in, uint16_t *pfnum)
2331 NTSTATUS status;
2332 unsigned int openfn = 0;
2333 unsigned int dos_deny = 0;
2334 uint32_t access_mask, share_mode, create_disposition, create_options;
2336 /* Do the initial mapping into OpenX parameters. */
2337 if (flags & O_CREAT) {
2338 openfn |= (1<<4);
2340 if (!(flags & O_EXCL)) {
2341 if (flags & O_TRUNC)
2342 openfn |= (1<<1);
2343 else
2344 openfn |= (1<<0);
2347 dos_deny = (share_mode_in<<4);
2349 if ((flags & O_ACCMODE) == O_RDWR) {
2350 dos_deny |= 2;
2351 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2352 dos_deny |= 1;
2355 #if defined(O_SYNC)
2356 if ((flags & O_SYNC) == O_SYNC) {
2357 dos_deny |= (1<<14);
2359 #endif /* O_SYNC */
2361 if (share_mode_in == DENY_FCB) {
2362 dos_deny = 0xFF;
2365 #if 0
2366 /* Hmmm. This is what I think the above code
2367 should look like if it's using the constants
2368 we #define. JRA. */
2370 if (flags & O_CREAT) {
2371 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2373 if (!(flags & O_EXCL)) {
2374 if (flags & O_TRUNC)
2375 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2376 else
2377 openfn |= OPENX_FILE_EXISTS_OPEN;
2380 dos_deny = SET_DENY_MODE(share_mode_in);
2382 if ((flags & O_ACCMODE) == O_RDWR) {
2383 dos_deny |= DOS_OPEN_RDWR;
2384 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2385 dos_deny |= DOS_OPEN_WRONLY;
2388 #if defined(O_SYNC)
2389 if ((flags & O_SYNC) == O_SYNC) {
2390 dos_deny |= FILE_SYNC_OPENMODE;
2392 #endif /* O_SYNC */
2394 if (share_mode_in == DENY_FCB) {
2395 dos_deny = 0xFF;
2397 #endif
2399 if (!map_open_params_to_ntcreate(fname, dos_deny,
2400 openfn, &access_mask,
2401 &share_mode, &create_disposition,
2402 &create_options, NULL)) {
2403 goto try_openx;
2406 status = cli_ntcreate(cli,
2407 fname,
2409 access_mask,
2411 share_mode,
2412 create_disposition,
2413 create_options,
2415 pfnum);
2417 /* Try and cope will all varients of "we don't do this call"
2418 and fall back to openX. */
2420 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2421 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2422 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2423 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2424 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2425 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2426 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2427 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2428 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2429 goto try_openx;
2432 return status;
2434 try_openx:
2436 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2439 /****************************************************************************
2440 Close a file.
2441 ****************************************************************************/
2443 struct cli_close_state {
2444 uint16_t vwv[3];
2447 static void cli_close_done(struct tevent_req *subreq);
2449 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2450 struct tevent_context *ev,
2451 struct cli_state *cli,
2452 uint16_t fnum,
2453 struct tevent_req **psubreq)
2455 struct tevent_req *req, *subreq;
2456 struct cli_close_state *state;
2458 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2459 if (req == NULL) {
2460 return NULL;
2463 SSVAL(state->vwv+0, 0, fnum);
2464 SIVALS(state->vwv+1, 0, -1);
2466 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2467 0, NULL);
2468 if (subreq == NULL) {
2469 TALLOC_FREE(req);
2470 return NULL;
2472 tevent_req_set_callback(subreq, cli_close_done, req);
2473 *psubreq = subreq;
2474 return req;
2477 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2478 struct tevent_context *ev,
2479 struct cli_state *cli,
2480 uint16_t fnum)
2482 struct tevent_req *req, *subreq;
2483 NTSTATUS status;
2485 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2486 if (req == NULL) {
2487 return NULL;
2490 status = smb1cli_req_chain_submit(&subreq, 1);
2491 if (tevent_req_nterror(req, status)) {
2492 return tevent_req_post(req, ev);
2494 return req;
2497 static void cli_close_done(struct tevent_req *subreq)
2499 struct tevent_req *req = tevent_req_callback_data(
2500 subreq, struct tevent_req);
2501 NTSTATUS status;
2503 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2504 TALLOC_FREE(subreq);
2505 if (tevent_req_nterror(req, status)) {
2506 return;
2508 tevent_req_done(req);
2511 NTSTATUS cli_close_recv(struct tevent_req *req)
2513 return tevent_req_simple_recv_ntstatus(req);
2516 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2518 TALLOC_CTX *frame = talloc_stackframe();
2519 struct tevent_context *ev;
2520 struct tevent_req *req;
2521 NTSTATUS status = NT_STATUS_OK;
2523 if (smbXcli_conn_has_async_calls(cli->conn)) {
2525 * Can't use sync call while an async call is in flight
2527 status = NT_STATUS_INVALID_PARAMETER;
2528 goto fail;
2531 ev = samba_tevent_context_init(frame);
2532 if (ev == NULL) {
2533 status = NT_STATUS_NO_MEMORY;
2534 goto fail;
2537 req = cli_close_send(frame, ev, cli, fnum);
2538 if (req == NULL) {
2539 status = NT_STATUS_NO_MEMORY;
2540 goto fail;
2543 if (!tevent_req_poll(req, ev)) {
2544 status = map_nt_error_from_unix(errno);
2545 goto fail;
2548 status = cli_close_recv(req);
2549 fail:
2550 TALLOC_FREE(frame);
2551 return status;
2554 /****************************************************************************
2555 Truncate a file to a specified size
2556 ****************************************************************************/
2558 struct ftrunc_state {
2559 uint16_t setup;
2560 uint8_t param[6];
2561 uint8_t data[8];
2564 static void cli_ftruncate_done(struct tevent_req *subreq)
2566 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2567 NULL, 0, NULL, NULL, 0, NULL);
2568 tevent_req_simple_finish_ntstatus(subreq, status);
2571 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2572 struct tevent_context *ev,
2573 struct cli_state *cli,
2574 uint16_t fnum,
2575 uint64_t size)
2577 struct tevent_req *req = NULL, *subreq = NULL;
2578 struct ftrunc_state *state = NULL;
2580 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2581 if (req == NULL) {
2582 return NULL;
2585 /* Setup setup word. */
2586 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2588 /* Setup param array. */
2589 SSVAL(state->param,0,fnum);
2590 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2591 SSVAL(state->param,4,0);
2593 /* Setup data array. */
2594 SBVAL(state->data, 0, size);
2596 subreq = cli_trans_send(state, /* mem ctx. */
2597 ev, /* event ctx. */
2598 cli, /* cli_state. */
2599 SMBtrans2, /* cmd. */
2600 NULL, /* pipe name. */
2601 -1, /* fid. */
2602 0, /* function. */
2603 0, /* flags. */
2604 &state->setup, /* setup. */
2605 1, /* num setup uint16_t words. */
2606 0, /* max returned setup. */
2607 state->param, /* param. */
2608 6, /* num param. */
2609 2, /* max returned param. */
2610 state->data, /* data. */
2611 8, /* num data. */
2612 0); /* max returned data. */
2614 if (tevent_req_nomem(subreq, req)) {
2615 return tevent_req_post(req, ev);
2617 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2618 return req;
2621 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2623 return tevent_req_simple_recv_ntstatus(req);
2626 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2628 TALLOC_CTX *frame = talloc_stackframe();
2629 struct tevent_context *ev = NULL;
2630 struct tevent_req *req = NULL;
2631 NTSTATUS status = NT_STATUS_OK;
2633 if (smbXcli_conn_has_async_calls(cli->conn)) {
2635 * Can't use sync call while an async call is in flight
2637 status = NT_STATUS_INVALID_PARAMETER;
2638 goto fail;
2641 ev = samba_tevent_context_init(frame);
2642 if (ev == NULL) {
2643 status = NT_STATUS_NO_MEMORY;
2644 goto fail;
2647 req = cli_ftruncate_send(frame,
2649 cli,
2650 fnum,
2651 size);
2652 if (req == NULL) {
2653 status = NT_STATUS_NO_MEMORY;
2654 goto fail;
2657 if (!tevent_req_poll(req, ev)) {
2658 status = map_nt_error_from_unix(errno);
2659 goto fail;
2662 status = cli_ftruncate_recv(req);
2664 fail:
2665 TALLOC_FREE(frame);
2666 return status;
2669 /****************************************************************************
2670 send a lock with a specified locktype
2671 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2672 ****************************************************************************/
2674 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2675 uint32_t offset, uint32_t len,
2676 int timeout, unsigned char locktype)
2678 uint16_t vwv[8];
2679 uint8_t bytes[10];
2680 NTSTATUS status;
2681 unsigned int set_timeout = 0;
2682 unsigned int saved_timeout = 0;
2684 SCVAL(vwv + 0, 0, 0xff);
2685 SCVAL(vwv + 0, 1, 0);
2686 SSVAL(vwv + 1, 0, 0);
2687 SSVAL(vwv + 2, 0, fnum);
2688 SCVAL(vwv + 3, 0, locktype);
2689 SCVAL(vwv + 3, 1, 0);
2690 SIVALS(vwv + 4, 0, timeout);
2691 SSVAL(vwv + 6, 0, 0);
2692 SSVAL(vwv + 7, 0, 1);
2694 SSVAL(bytes, 0, cli_getpid(cli));
2695 SIVAL(bytes, 2, offset);
2696 SIVAL(bytes, 6, len);
2698 if (timeout != 0) {
2699 if (timeout == -1) {
2700 set_timeout = 0x7FFFFFFF;
2701 } else {
2702 set_timeout = timeout + 2*1000;
2704 saved_timeout = cli_set_timeout(cli, set_timeout);
2707 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2708 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2710 if (saved_timeout != 0) {
2711 cli_set_timeout(cli, saved_timeout);
2714 return status;
2717 /****************************************************************************
2718 Lock a file.
2719 note that timeout is in units of 2 milliseconds
2720 ****************************************************************************/
2722 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2723 uint32_t offset, uint32_t len, int timeout,
2724 enum brl_type lock_type)
2726 NTSTATUS status;
2728 status = cli_locktype(cli, fnum, offset, len, timeout,
2729 (lock_type == READ_LOCK? 1 : 0));
2730 return status;
2733 /****************************************************************************
2734 Unlock a file.
2735 ****************************************************************************/
2737 struct cli_unlock_state {
2738 uint16_t vwv[8];
2739 uint8_t data[10];
2742 static void cli_unlock_done(struct tevent_req *subreq);
2744 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2745 struct tevent_context *ev,
2746 struct cli_state *cli,
2747 uint16_t fnum,
2748 uint64_t offset,
2749 uint64_t len)
2752 struct tevent_req *req = NULL, *subreq = NULL;
2753 struct cli_unlock_state *state = NULL;
2754 uint8_t additional_flags = 0;
2756 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2757 if (req == NULL) {
2758 return NULL;
2761 SCVAL(state->vwv+0, 0, 0xFF);
2762 SSVAL(state->vwv+2, 0, fnum);
2763 SCVAL(state->vwv+3, 0, 0);
2764 SIVALS(state->vwv+4, 0, 0);
2765 SSVAL(state->vwv+6, 0, 1);
2766 SSVAL(state->vwv+7, 0, 0);
2768 SSVAL(state->data, 0, cli_getpid(cli));
2769 SIVAL(state->data, 2, offset);
2770 SIVAL(state->data, 6, len);
2772 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2773 8, state->vwv, 10, state->data);
2774 if (tevent_req_nomem(subreq, req)) {
2775 return tevent_req_post(req, ev);
2777 tevent_req_set_callback(subreq, cli_unlock_done, req);
2778 return req;
2781 static void cli_unlock_done(struct tevent_req *subreq)
2783 struct tevent_req *req = tevent_req_callback_data(
2784 subreq, struct tevent_req);
2785 NTSTATUS status;
2787 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2788 TALLOC_FREE(subreq);
2789 if (tevent_req_nterror(req, status)) {
2790 return;
2792 tevent_req_done(req);
2795 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2797 return tevent_req_simple_recv_ntstatus(req);
2800 NTSTATUS cli_unlock(struct cli_state *cli,
2801 uint16_t fnum,
2802 uint32_t offset,
2803 uint32_t len)
2805 TALLOC_CTX *frame = talloc_stackframe();
2806 struct tevent_context *ev;
2807 struct tevent_req *req;
2808 NTSTATUS status = NT_STATUS_OK;
2810 if (smbXcli_conn_has_async_calls(cli->conn)) {
2812 * Can't use sync call while an async call is in flight
2814 status = NT_STATUS_INVALID_PARAMETER;
2815 goto fail;
2818 ev = samba_tevent_context_init(frame);
2819 if (ev == NULL) {
2820 status = NT_STATUS_NO_MEMORY;
2821 goto fail;
2824 req = cli_unlock_send(frame, ev, cli,
2825 fnum, offset, len);
2826 if (req == NULL) {
2827 status = NT_STATUS_NO_MEMORY;
2828 goto fail;
2831 if (!tevent_req_poll(req, ev)) {
2832 status = map_nt_error_from_unix(errno);
2833 goto fail;
2836 status = cli_unlock_recv(req);
2838 fail:
2839 TALLOC_FREE(frame);
2840 return status;
2843 /****************************************************************************
2844 Lock a file with 64 bit offsets.
2845 ****************************************************************************/
2847 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
2848 uint64_t offset, uint64_t len, int timeout,
2849 enum brl_type lock_type)
2851 uint16_t vwv[8];
2852 uint8_t bytes[20];
2853 unsigned int set_timeout = 0;
2854 unsigned int saved_timeout = 0;
2855 int ltype;
2856 NTSTATUS status;
2858 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
2859 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
2862 ltype = (lock_type == READ_LOCK? 1 : 0);
2863 ltype |= LOCKING_ANDX_LARGE_FILES;
2865 SCVAL(vwv + 0, 0, 0xff);
2866 SCVAL(vwv + 0, 1, 0);
2867 SSVAL(vwv + 1, 0, 0);
2868 SSVAL(vwv + 2, 0, fnum);
2869 SCVAL(vwv + 3, 0, ltype);
2870 SCVAL(vwv + 3, 1, 0);
2871 SIVALS(vwv + 4, 0, timeout);
2872 SSVAL(vwv + 6, 0, 0);
2873 SSVAL(vwv + 7, 0, 1);
2875 SIVAL(bytes, 0, cli_getpid(cli));
2876 SOFF_T_R(bytes, 4, offset);
2877 SOFF_T_R(bytes, 12, len);
2879 if (timeout != 0) {
2880 if (timeout == -1) {
2881 set_timeout = 0x7FFFFFFF;
2882 } else {
2883 set_timeout = timeout + 2*1000;
2885 saved_timeout = cli_set_timeout(cli, set_timeout);
2888 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2889 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2891 if (saved_timeout != 0) {
2892 cli_set_timeout(cli, saved_timeout);
2895 return status;
2898 /****************************************************************************
2899 Unlock a file with 64 bit offsets.
2900 ****************************************************************************/
2902 struct cli_unlock64_state {
2903 uint16_t vwv[8];
2904 uint8_t data[20];
2907 static void cli_unlock64_done(struct tevent_req *subreq);
2909 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2910 struct tevent_context *ev,
2911 struct cli_state *cli,
2912 uint16_t fnum,
2913 uint64_t offset,
2914 uint64_t len)
2917 struct tevent_req *req = NULL, *subreq = NULL;
2918 struct cli_unlock64_state *state = NULL;
2919 uint8_t additional_flags = 0;
2921 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2922 if (req == NULL) {
2923 return NULL;
2926 SCVAL(state->vwv+0, 0, 0xff);
2927 SSVAL(state->vwv+2, 0, fnum);
2928 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2929 SIVALS(state->vwv+4, 0, 0);
2930 SSVAL(state->vwv+6, 0, 1);
2931 SSVAL(state->vwv+7, 0, 0);
2933 SIVAL(state->data, 0, cli_getpid(cli));
2934 SOFF_T_R(state->data, 4, offset);
2935 SOFF_T_R(state->data, 12, len);
2937 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2938 8, state->vwv, 20, state->data);
2939 if (tevent_req_nomem(subreq, req)) {
2940 return tevent_req_post(req, ev);
2942 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2943 return req;
2946 static void cli_unlock64_done(struct tevent_req *subreq)
2948 struct tevent_req *req = tevent_req_callback_data(
2949 subreq, struct tevent_req);
2950 NTSTATUS status;
2952 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2953 TALLOC_FREE(subreq);
2954 if (tevent_req_nterror(req, status)) {
2955 return;
2957 tevent_req_done(req);
2960 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2962 return tevent_req_simple_recv_ntstatus(req);
2965 NTSTATUS cli_unlock64(struct cli_state *cli,
2966 uint16_t fnum,
2967 uint64_t offset,
2968 uint64_t len)
2970 TALLOC_CTX *frame = talloc_stackframe();
2971 struct tevent_context *ev;
2972 struct tevent_req *req;
2973 NTSTATUS status = NT_STATUS_OK;
2975 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
2976 return cli_unlock(cli, fnum, offset, len);
2979 if (smbXcli_conn_has_async_calls(cli->conn)) {
2981 * Can't use sync call while an async call is in flight
2983 status = NT_STATUS_INVALID_PARAMETER;
2984 goto fail;
2987 ev = samba_tevent_context_init(frame);
2988 if (ev == NULL) {
2989 status = NT_STATUS_NO_MEMORY;
2990 goto fail;
2993 req = cli_unlock64_send(frame, ev, cli,
2994 fnum, offset, len);
2995 if (req == NULL) {
2996 status = NT_STATUS_NO_MEMORY;
2997 goto fail;
3000 if (!tevent_req_poll(req, ev)) {
3001 status = map_nt_error_from_unix(errno);
3002 goto fail;
3005 status = cli_unlock64_recv(req);
3007 fail:
3008 TALLOC_FREE(frame);
3009 return status;
3012 /****************************************************************************
3013 Get/unlock a POSIX lock on a file - internal function.
3014 ****************************************************************************/
3016 struct posix_lock_state {
3017 uint16_t setup;
3018 uint8_t param[4];
3019 uint8_t data[POSIX_LOCK_DATA_SIZE];
3022 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3024 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3025 NULL, 0, NULL, NULL, 0, NULL);
3026 tevent_req_simple_finish_ntstatus(subreq, status);
3029 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3030 struct tevent_context *ev,
3031 struct cli_state *cli,
3032 uint16_t fnum,
3033 uint64_t offset,
3034 uint64_t len,
3035 bool wait_lock,
3036 enum brl_type lock_type)
3038 struct tevent_req *req = NULL, *subreq = NULL;
3039 struct posix_lock_state *state = NULL;
3041 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3042 if (req == NULL) {
3043 return NULL;
3046 /* Setup setup word. */
3047 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3049 /* Setup param array. */
3050 SSVAL(&state->param, 0, fnum);
3051 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3053 /* Setup data array. */
3054 switch (lock_type) {
3055 case READ_LOCK:
3056 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3057 POSIX_LOCK_TYPE_READ);
3058 break;
3059 case WRITE_LOCK:
3060 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3061 POSIX_LOCK_TYPE_WRITE);
3062 break;
3063 case UNLOCK_LOCK:
3064 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3065 POSIX_LOCK_TYPE_UNLOCK);
3066 break;
3067 default:
3068 return NULL;
3071 if (wait_lock) {
3072 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3073 POSIX_LOCK_FLAG_WAIT);
3074 } else {
3075 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3076 POSIX_LOCK_FLAG_NOWAIT);
3079 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3080 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3081 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3083 subreq = cli_trans_send(state, /* mem ctx. */
3084 ev, /* event ctx. */
3085 cli, /* cli_state. */
3086 SMBtrans2, /* cmd. */
3087 NULL, /* pipe name. */
3088 -1, /* fid. */
3089 0, /* function. */
3090 0, /* flags. */
3091 &state->setup, /* setup. */
3092 1, /* num setup uint16_t words. */
3093 0, /* max returned setup. */
3094 state->param, /* param. */
3095 4, /* num param. */
3096 2, /* max returned param. */
3097 state->data, /* data. */
3098 POSIX_LOCK_DATA_SIZE, /* num data. */
3099 0); /* max returned data. */
3101 if (tevent_req_nomem(subreq, req)) {
3102 return tevent_req_post(req, ev);
3104 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3105 return req;
3108 /****************************************************************************
3109 POSIX Lock a file.
3110 ****************************************************************************/
3112 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3113 struct tevent_context *ev,
3114 struct cli_state *cli,
3115 uint16_t fnum,
3116 uint64_t offset,
3117 uint64_t len,
3118 bool wait_lock,
3119 enum brl_type lock_type)
3121 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3122 wait_lock, lock_type);
3125 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3127 return tevent_req_simple_recv_ntstatus(req);
3130 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3131 uint64_t offset, uint64_t len,
3132 bool wait_lock, enum brl_type lock_type)
3134 TALLOC_CTX *frame = talloc_stackframe();
3135 struct tevent_context *ev = NULL;
3136 struct tevent_req *req = NULL;
3137 NTSTATUS status = NT_STATUS_OK;
3139 if (smbXcli_conn_has_async_calls(cli->conn)) {
3141 * Can't use sync call while an async call is in flight
3143 status = NT_STATUS_INVALID_PARAMETER;
3144 goto fail;
3147 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3148 status = NT_STATUS_INVALID_PARAMETER;
3149 goto fail;
3152 ev = samba_tevent_context_init(frame);
3153 if (ev == NULL) {
3154 status = NT_STATUS_NO_MEMORY;
3155 goto fail;
3158 req = cli_posix_lock_send(frame,
3160 cli,
3161 fnum,
3162 offset,
3163 len,
3164 wait_lock,
3165 lock_type);
3166 if (req == NULL) {
3167 status = NT_STATUS_NO_MEMORY;
3168 goto fail;
3171 if (!tevent_req_poll(req, ev)) {
3172 status = map_nt_error_from_unix(errno);
3173 goto fail;
3176 status = cli_posix_lock_recv(req);
3178 fail:
3179 TALLOC_FREE(frame);
3180 return status;
3183 /****************************************************************************
3184 POSIX Unlock a file.
3185 ****************************************************************************/
3187 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3188 struct tevent_context *ev,
3189 struct cli_state *cli,
3190 uint16_t fnum,
3191 uint64_t offset,
3192 uint64_t len)
3194 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3195 false, UNLOCK_LOCK);
3198 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3200 return tevent_req_simple_recv_ntstatus(req);
3203 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3205 TALLOC_CTX *frame = talloc_stackframe();
3206 struct tevent_context *ev = NULL;
3207 struct tevent_req *req = NULL;
3208 NTSTATUS status = NT_STATUS_OK;
3210 if (smbXcli_conn_has_async_calls(cli->conn)) {
3212 * Can't use sync call while an async call is in flight
3214 status = NT_STATUS_INVALID_PARAMETER;
3215 goto fail;
3218 ev = samba_tevent_context_init(frame);
3219 if (ev == NULL) {
3220 status = NT_STATUS_NO_MEMORY;
3221 goto fail;
3224 req = cli_posix_unlock_send(frame,
3226 cli,
3227 fnum,
3228 offset,
3229 len);
3230 if (req == NULL) {
3231 status = NT_STATUS_NO_MEMORY;
3232 goto fail;
3235 if (!tevent_req_poll(req, ev)) {
3236 status = map_nt_error_from_unix(errno);
3237 goto fail;
3240 status = cli_posix_unlock_recv(req);
3242 fail:
3243 TALLOC_FREE(frame);
3244 return status;
3247 /****************************************************************************
3248 Do a SMBgetattrE call.
3249 ****************************************************************************/
3251 static void cli_getattrE_done(struct tevent_req *subreq);
3253 struct cli_getattrE_state {
3254 uint16_t vwv[1];
3255 int zone_offset;
3256 uint16_t attr;
3257 off_t size;
3258 time_t change_time;
3259 time_t access_time;
3260 time_t write_time;
3263 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3264 struct tevent_context *ev,
3265 struct cli_state *cli,
3266 uint16_t fnum)
3268 struct tevent_req *req = NULL, *subreq = NULL;
3269 struct cli_getattrE_state *state = NULL;
3270 uint8_t additional_flags = 0;
3272 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3273 if (req == NULL) {
3274 return NULL;
3277 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3278 SSVAL(state->vwv+0,0,fnum);
3280 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3281 1, state->vwv, 0, NULL);
3282 if (tevent_req_nomem(subreq, req)) {
3283 return tevent_req_post(req, ev);
3285 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3286 return req;
3289 static void cli_getattrE_done(struct tevent_req *subreq)
3291 struct tevent_req *req = tevent_req_callback_data(
3292 subreq, struct tevent_req);
3293 struct cli_getattrE_state *state = tevent_req_data(
3294 req, struct cli_getattrE_state);
3295 uint8_t wct;
3296 uint16_t *vwv = NULL;
3297 NTSTATUS status;
3299 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3300 NULL, NULL);
3301 TALLOC_FREE(subreq);
3302 if (tevent_req_nterror(req, status)) {
3303 return;
3306 state->size = (off_t)IVAL(vwv+6,0);
3307 state->attr = SVAL(vwv+10,0);
3308 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3309 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3310 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3312 tevent_req_done(req);
3315 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3316 uint16_t *attr,
3317 off_t *size,
3318 time_t *change_time,
3319 time_t *access_time,
3320 time_t *write_time)
3322 struct cli_getattrE_state *state = tevent_req_data(
3323 req, struct cli_getattrE_state);
3324 NTSTATUS status;
3326 if (tevent_req_is_nterror(req, &status)) {
3327 return status;
3329 if (attr) {
3330 *attr = state->attr;
3332 if (size) {
3333 *size = state->size;
3335 if (change_time) {
3336 *change_time = state->change_time;
3338 if (access_time) {
3339 *access_time = state->access_time;
3341 if (write_time) {
3342 *write_time = state->write_time;
3344 return NT_STATUS_OK;
3347 NTSTATUS cli_getattrE(struct cli_state *cli,
3348 uint16_t fnum,
3349 uint16_t *attr,
3350 off_t *size,
3351 time_t *change_time,
3352 time_t *access_time,
3353 time_t *write_time)
3355 TALLOC_CTX *frame = talloc_stackframe();
3356 struct tevent_context *ev = NULL;
3357 struct tevent_req *req = NULL;
3358 NTSTATUS status = NT_STATUS_OK;
3360 if (smbXcli_conn_has_async_calls(cli->conn)) {
3362 * Can't use sync call while an async call is in flight
3364 status = NT_STATUS_INVALID_PARAMETER;
3365 goto fail;
3368 ev = samba_tevent_context_init(frame);
3369 if (ev == NULL) {
3370 status = NT_STATUS_NO_MEMORY;
3371 goto fail;
3374 req = cli_getattrE_send(frame, ev, cli, fnum);
3375 if (req == NULL) {
3376 status = NT_STATUS_NO_MEMORY;
3377 goto fail;
3380 if (!tevent_req_poll(req, ev)) {
3381 status = map_nt_error_from_unix(errno);
3382 goto fail;
3385 status = cli_getattrE_recv(req,
3386 attr,
3387 size,
3388 change_time,
3389 access_time,
3390 write_time);
3392 fail:
3393 TALLOC_FREE(frame);
3394 return status;
3397 /****************************************************************************
3398 Do a SMBgetatr call
3399 ****************************************************************************/
3401 static void cli_getatr_done(struct tevent_req *subreq);
3403 struct cli_getatr_state {
3404 int zone_offset;
3405 uint16_t attr;
3406 off_t size;
3407 time_t write_time;
3410 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3411 struct tevent_context *ev,
3412 struct cli_state *cli,
3413 const char *fname)
3415 struct tevent_req *req = NULL, *subreq = NULL;
3416 struct cli_getatr_state *state = NULL;
3417 uint8_t additional_flags = 0;
3418 uint8_t *bytes = NULL;
3420 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3421 if (req == NULL) {
3422 return NULL;
3425 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3427 bytes = talloc_array(state, uint8_t, 1);
3428 if (tevent_req_nomem(bytes, req)) {
3429 return tevent_req_post(req, ev);
3431 bytes[0] = 4;
3432 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3433 strlen(fname)+1, NULL);
3435 if (tevent_req_nomem(bytes, req)) {
3436 return tevent_req_post(req, ev);
3439 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3440 0, NULL, talloc_get_size(bytes), bytes);
3441 if (tevent_req_nomem(subreq, req)) {
3442 return tevent_req_post(req, ev);
3444 tevent_req_set_callback(subreq, cli_getatr_done, req);
3445 return req;
3448 static void cli_getatr_done(struct tevent_req *subreq)
3450 struct tevent_req *req = tevent_req_callback_data(
3451 subreq, struct tevent_req);
3452 struct cli_getatr_state *state = tevent_req_data(
3453 req, struct cli_getatr_state);
3454 uint8_t wct;
3455 uint16_t *vwv = NULL;
3456 NTSTATUS status;
3458 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3459 NULL);
3460 TALLOC_FREE(subreq);
3461 if (tevent_req_nterror(req, status)) {
3462 return;
3465 state->attr = SVAL(vwv+0,0);
3466 state->size = (off_t)IVAL(vwv+3,0);
3467 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3469 tevent_req_done(req);
3472 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3473 uint16_t *attr,
3474 off_t *size,
3475 time_t *write_time)
3477 struct cli_getatr_state *state = tevent_req_data(
3478 req, struct cli_getatr_state);
3479 NTSTATUS status;
3481 if (tevent_req_is_nterror(req, &status)) {
3482 return status;
3484 if (attr) {
3485 *attr = state->attr;
3487 if (size) {
3488 *size = state->size;
3490 if (write_time) {
3491 *write_time = state->write_time;
3493 return NT_STATUS_OK;
3496 NTSTATUS cli_getatr(struct cli_state *cli,
3497 const char *fname,
3498 uint16_t *attr,
3499 off_t *size,
3500 time_t *write_time)
3502 TALLOC_CTX *frame = talloc_stackframe();
3503 struct tevent_context *ev = NULL;
3504 struct tevent_req *req = NULL;
3505 NTSTATUS status = NT_STATUS_OK;
3507 if (smbXcli_conn_has_async_calls(cli->conn)) {
3509 * Can't use sync call while an async call is in flight
3511 status = NT_STATUS_INVALID_PARAMETER;
3512 goto fail;
3515 ev = samba_tevent_context_init(frame);
3516 if (ev == NULL) {
3517 status = NT_STATUS_NO_MEMORY;
3518 goto fail;
3521 req = cli_getatr_send(frame, ev, cli, fname);
3522 if (req == NULL) {
3523 status = NT_STATUS_NO_MEMORY;
3524 goto fail;
3527 if (!tevent_req_poll(req, ev)) {
3528 status = map_nt_error_from_unix(errno);
3529 goto fail;
3532 status = cli_getatr_recv(req,
3533 attr,
3534 size,
3535 write_time);
3537 fail:
3538 TALLOC_FREE(frame);
3539 return status;
3542 /****************************************************************************
3543 Do a SMBsetattrE call.
3544 ****************************************************************************/
3546 static void cli_setattrE_done(struct tevent_req *subreq);
3548 struct cli_setattrE_state {
3549 uint16_t vwv[7];
3552 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3553 struct tevent_context *ev,
3554 struct cli_state *cli,
3555 uint16_t fnum,
3556 time_t change_time,
3557 time_t access_time,
3558 time_t write_time)
3560 struct tevent_req *req = NULL, *subreq = NULL;
3561 struct cli_setattrE_state *state = NULL;
3562 uint8_t additional_flags = 0;
3564 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3565 if (req == NULL) {
3566 return NULL;
3569 SSVAL(state->vwv+0, 0, fnum);
3570 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3571 smb1cli_conn_server_time_zone(cli->conn));
3572 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3573 smb1cli_conn_server_time_zone(cli->conn));
3574 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3575 smb1cli_conn_server_time_zone(cli->conn));
3577 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3578 7, state->vwv, 0, NULL);
3579 if (tevent_req_nomem(subreq, req)) {
3580 return tevent_req_post(req, ev);
3582 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3583 return req;
3586 static void cli_setattrE_done(struct tevent_req *subreq)
3588 struct tevent_req *req = tevent_req_callback_data(
3589 subreq, struct tevent_req);
3590 NTSTATUS status;
3592 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3593 TALLOC_FREE(subreq);
3594 if (tevent_req_nterror(req, status)) {
3595 return;
3597 tevent_req_done(req);
3600 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3602 return tevent_req_simple_recv_ntstatus(req);
3605 NTSTATUS cli_setattrE(struct cli_state *cli,
3606 uint16_t fnum,
3607 time_t change_time,
3608 time_t access_time,
3609 time_t write_time)
3611 TALLOC_CTX *frame = talloc_stackframe();
3612 struct tevent_context *ev = NULL;
3613 struct tevent_req *req = NULL;
3614 NTSTATUS status = NT_STATUS_OK;
3616 if (smbXcli_conn_has_async_calls(cli->conn)) {
3618 * Can't use sync call while an async call is in flight
3620 status = NT_STATUS_INVALID_PARAMETER;
3621 goto fail;
3624 ev = samba_tevent_context_init(frame);
3625 if (ev == NULL) {
3626 status = NT_STATUS_NO_MEMORY;
3627 goto fail;
3630 req = cli_setattrE_send(frame, ev,
3631 cli,
3632 fnum,
3633 change_time,
3634 access_time,
3635 write_time);
3637 if (req == NULL) {
3638 status = NT_STATUS_NO_MEMORY;
3639 goto fail;
3642 if (!tevent_req_poll(req, ev)) {
3643 status = map_nt_error_from_unix(errno);
3644 goto fail;
3647 status = cli_setattrE_recv(req);
3649 fail:
3650 TALLOC_FREE(frame);
3651 return status;
3654 /****************************************************************************
3655 Do a SMBsetatr call.
3656 ****************************************************************************/
3658 static void cli_setatr_done(struct tevent_req *subreq);
3660 struct cli_setatr_state {
3661 uint16_t vwv[8];
3664 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3665 struct tevent_context *ev,
3666 struct cli_state *cli,
3667 const char *fname,
3668 uint16_t attr,
3669 time_t mtime)
3671 struct tevent_req *req = NULL, *subreq = NULL;
3672 struct cli_setatr_state *state = NULL;
3673 uint8_t additional_flags = 0;
3674 uint8_t *bytes = NULL;
3676 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3677 if (req == NULL) {
3678 return NULL;
3681 SSVAL(state->vwv+0, 0, attr);
3682 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3684 bytes = talloc_array(state, uint8_t, 1);
3685 if (tevent_req_nomem(bytes, req)) {
3686 return tevent_req_post(req, ev);
3688 bytes[0] = 4;
3689 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3690 strlen(fname)+1, NULL);
3691 if (tevent_req_nomem(bytes, req)) {
3692 return tevent_req_post(req, ev);
3694 bytes = talloc_realloc(state, bytes, uint8_t,
3695 talloc_get_size(bytes)+1);
3696 if (tevent_req_nomem(bytes, req)) {
3697 return tevent_req_post(req, ev);
3700 bytes[talloc_get_size(bytes)-1] = 4;
3701 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3702 1, NULL);
3703 if (tevent_req_nomem(bytes, req)) {
3704 return tevent_req_post(req, ev);
3707 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3708 8, state->vwv, talloc_get_size(bytes), bytes);
3709 if (tevent_req_nomem(subreq, req)) {
3710 return tevent_req_post(req, ev);
3712 tevent_req_set_callback(subreq, cli_setatr_done, req);
3713 return req;
3716 static void cli_setatr_done(struct tevent_req *subreq)
3718 struct tevent_req *req = tevent_req_callback_data(
3719 subreq, struct tevent_req);
3720 NTSTATUS status;
3722 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3723 TALLOC_FREE(subreq);
3724 if (tevent_req_nterror(req, status)) {
3725 return;
3727 tevent_req_done(req);
3730 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3732 return tevent_req_simple_recv_ntstatus(req);
3735 NTSTATUS cli_setatr(struct cli_state *cli,
3736 const char *fname,
3737 uint16_t attr,
3738 time_t mtime)
3740 TALLOC_CTX *frame = talloc_stackframe();
3741 struct tevent_context *ev = NULL;
3742 struct tevent_req *req = NULL;
3743 NTSTATUS status = NT_STATUS_OK;
3745 if (smbXcli_conn_has_async_calls(cli->conn)) {
3747 * Can't use sync call while an async call is in flight
3749 status = NT_STATUS_INVALID_PARAMETER;
3750 goto fail;
3753 ev = samba_tevent_context_init(frame);
3754 if (ev == NULL) {
3755 status = NT_STATUS_NO_MEMORY;
3756 goto fail;
3759 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3760 if (req == NULL) {
3761 status = NT_STATUS_NO_MEMORY;
3762 goto fail;
3765 if (!tevent_req_poll(req, ev)) {
3766 status = map_nt_error_from_unix(errno);
3767 goto fail;
3770 status = cli_setatr_recv(req);
3772 fail:
3773 TALLOC_FREE(frame);
3774 return status;
3777 /****************************************************************************
3778 Check for existance of a dir.
3779 ****************************************************************************/
3781 static void cli_chkpath_done(struct tevent_req *subreq);
3783 struct cli_chkpath_state {
3784 int dummy;
3787 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3788 struct tevent_context *ev,
3789 struct cli_state *cli,
3790 const char *fname)
3792 struct tevent_req *req = NULL, *subreq = NULL;
3793 struct cli_chkpath_state *state = NULL;
3794 uint8_t additional_flags = 0;
3795 uint8_t *bytes = NULL;
3797 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3798 if (req == NULL) {
3799 return NULL;
3802 bytes = talloc_array(state, uint8_t, 1);
3803 if (tevent_req_nomem(bytes, req)) {
3804 return tevent_req_post(req, ev);
3806 bytes[0] = 4;
3807 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3808 strlen(fname)+1, NULL);
3810 if (tevent_req_nomem(bytes, req)) {
3811 return tevent_req_post(req, ev);
3814 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3815 0, NULL, talloc_get_size(bytes), bytes);
3816 if (tevent_req_nomem(subreq, req)) {
3817 return tevent_req_post(req, ev);
3819 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3820 return req;
3823 static void cli_chkpath_done(struct tevent_req *subreq)
3825 struct tevent_req *req = tevent_req_callback_data(
3826 subreq, struct tevent_req);
3827 NTSTATUS status;
3829 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3830 TALLOC_FREE(subreq);
3831 if (tevent_req_nterror(req, status)) {
3832 return;
3834 tevent_req_done(req);
3837 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3839 return tevent_req_simple_recv_ntstatus(req);
3842 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3844 TALLOC_CTX *frame = talloc_stackframe();
3845 struct tevent_context *ev = NULL;
3846 struct tevent_req *req = NULL;
3847 char *path2 = NULL;
3848 NTSTATUS status = NT_STATUS_OK;
3850 if (smbXcli_conn_has_async_calls(cli->conn)) {
3852 * Can't use sync call while an async call is in flight
3854 status = NT_STATUS_INVALID_PARAMETER;
3855 goto fail;
3858 path2 = talloc_strdup(frame, path);
3859 if (!path2) {
3860 status = NT_STATUS_NO_MEMORY;
3861 goto fail;
3863 trim_char(path2,'\0','\\');
3864 if (!*path2) {
3865 path2 = talloc_strdup(frame, "\\");
3866 if (!path2) {
3867 status = NT_STATUS_NO_MEMORY;
3868 goto fail;
3872 ev = samba_tevent_context_init(frame);
3873 if (ev == NULL) {
3874 status = NT_STATUS_NO_MEMORY;
3875 goto fail;
3878 req = cli_chkpath_send(frame, ev, cli, path2);
3879 if (req == NULL) {
3880 status = NT_STATUS_NO_MEMORY;
3881 goto fail;
3884 if (!tevent_req_poll(req, ev)) {
3885 status = map_nt_error_from_unix(errno);
3886 goto fail;
3889 status = cli_chkpath_recv(req);
3891 fail:
3892 TALLOC_FREE(frame);
3893 return status;
3896 /****************************************************************************
3897 Query disk space.
3898 ****************************************************************************/
3900 static void cli_dskattr_done(struct tevent_req *subreq);
3902 struct cli_dskattr_state {
3903 int bsize;
3904 int total;
3905 int avail;
3908 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3909 struct tevent_context *ev,
3910 struct cli_state *cli)
3912 struct tevent_req *req = NULL, *subreq = NULL;
3913 struct cli_dskattr_state *state = NULL;
3914 uint8_t additional_flags = 0;
3916 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3917 if (req == NULL) {
3918 return NULL;
3921 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3922 0, NULL, 0, NULL);
3923 if (tevent_req_nomem(subreq, req)) {
3924 return tevent_req_post(req, ev);
3926 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3927 return req;
3930 static void cli_dskattr_done(struct tevent_req *subreq)
3932 struct tevent_req *req = tevent_req_callback_data(
3933 subreq, struct tevent_req);
3934 struct cli_dskattr_state *state = tevent_req_data(
3935 req, struct cli_dskattr_state);
3936 uint8_t wct;
3937 uint16_t *vwv = NULL;
3938 NTSTATUS status;
3940 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3941 NULL);
3942 TALLOC_FREE(subreq);
3943 if (tevent_req_nterror(req, status)) {
3944 return;
3946 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3947 state->total = SVAL(vwv+0, 0);
3948 state->avail = SVAL(vwv+3, 0);
3949 tevent_req_done(req);
3952 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3954 struct cli_dskattr_state *state = tevent_req_data(
3955 req, struct cli_dskattr_state);
3956 NTSTATUS status;
3958 if (tevent_req_is_nterror(req, &status)) {
3959 return status;
3961 *bsize = state->bsize;
3962 *total = state->total;
3963 *avail = state->avail;
3964 return NT_STATUS_OK;
3967 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3969 TALLOC_CTX *frame = talloc_stackframe();
3970 struct tevent_context *ev = NULL;
3971 struct tevent_req *req = NULL;
3972 NTSTATUS status = NT_STATUS_OK;
3974 if (smbXcli_conn_has_async_calls(cli->conn)) {
3976 * Can't use sync call while an async call is in flight
3978 status = NT_STATUS_INVALID_PARAMETER;
3979 goto fail;
3982 ev = samba_tevent_context_init(frame);
3983 if (ev == NULL) {
3984 status = NT_STATUS_NO_MEMORY;
3985 goto fail;
3988 req = cli_dskattr_send(frame, ev, cli);
3989 if (req == NULL) {
3990 status = NT_STATUS_NO_MEMORY;
3991 goto fail;
3994 if (!tevent_req_poll(req, ev)) {
3995 status = map_nt_error_from_unix(errno);
3996 goto fail;
3999 status = cli_dskattr_recv(req, bsize, total, avail);
4001 fail:
4002 TALLOC_FREE(frame);
4003 return status;
4006 /****************************************************************************
4007 Create and open a temporary file.
4008 ****************************************************************************/
4010 static void cli_ctemp_done(struct tevent_req *subreq);
4012 struct ctemp_state {
4013 uint16_t vwv[3];
4014 char *ret_path;
4015 uint16_t fnum;
4018 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4019 struct tevent_context *ev,
4020 struct cli_state *cli,
4021 const char *path)
4023 struct tevent_req *req = NULL, *subreq = NULL;
4024 struct ctemp_state *state = NULL;
4025 uint8_t additional_flags = 0;
4026 uint8_t *bytes = NULL;
4028 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4029 if (req == NULL) {
4030 return NULL;
4033 SSVAL(state->vwv,0,0);
4034 SIVALS(state->vwv+1,0,-1);
4036 bytes = talloc_array(state, uint8_t, 1);
4037 if (tevent_req_nomem(bytes, req)) {
4038 return tevent_req_post(req, ev);
4040 bytes[0] = 4;
4041 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4042 strlen(path)+1, NULL);
4043 if (tevent_req_nomem(bytes, req)) {
4044 return tevent_req_post(req, ev);
4047 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4048 3, state->vwv, talloc_get_size(bytes), bytes);
4049 if (tevent_req_nomem(subreq, req)) {
4050 return tevent_req_post(req, ev);
4052 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4053 return req;
4056 static void cli_ctemp_done(struct tevent_req *subreq)
4058 struct tevent_req *req = tevent_req_callback_data(
4059 subreq, struct tevent_req);
4060 struct ctemp_state *state = tevent_req_data(
4061 req, struct ctemp_state);
4062 NTSTATUS status;
4063 uint8_t wcnt;
4064 uint16_t *vwv;
4065 uint32_t num_bytes = 0;
4066 uint8_t *bytes = NULL;
4068 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4069 &num_bytes, &bytes);
4070 TALLOC_FREE(subreq);
4071 if (tevent_req_nterror(req, status)) {
4072 return;
4075 state->fnum = SVAL(vwv+0, 0);
4077 /* From W2K3, the result is just the ASCII name */
4078 if (num_bytes < 2) {
4079 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4080 return;
4083 if (pull_string_talloc(state,
4084 NULL,
4086 &state->ret_path,
4087 bytes,
4088 num_bytes,
4089 STR_ASCII) == 0) {
4090 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4091 return;
4093 tevent_req_done(req);
4096 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4097 TALLOC_CTX *ctx,
4098 uint16_t *pfnum,
4099 char **outfile)
4101 struct ctemp_state *state = tevent_req_data(req,
4102 struct ctemp_state);
4103 NTSTATUS status;
4105 if (tevent_req_is_nterror(req, &status)) {
4106 return status;
4108 *pfnum = state->fnum;
4109 *outfile = talloc_strdup(ctx, state->ret_path);
4110 if (!*outfile) {
4111 return NT_STATUS_NO_MEMORY;
4113 return NT_STATUS_OK;
4116 NTSTATUS cli_ctemp(struct cli_state *cli,
4117 TALLOC_CTX *ctx,
4118 const char *path,
4119 uint16_t *pfnum,
4120 char **out_path)
4122 TALLOC_CTX *frame = talloc_stackframe();
4123 struct tevent_context *ev;
4124 struct tevent_req *req;
4125 NTSTATUS status = NT_STATUS_OK;
4127 if (smbXcli_conn_has_async_calls(cli->conn)) {
4129 * Can't use sync call while an async call is in flight
4131 status = NT_STATUS_INVALID_PARAMETER;
4132 goto fail;
4135 ev = samba_tevent_context_init(frame);
4136 if (ev == NULL) {
4137 status = NT_STATUS_NO_MEMORY;
4138 goto fail;
4141 req = cli_ctemp_send(frame, ev, cli, path);
4142 if (req == NULL) {
4143 status = NT_STATUS_NO_MEMORY;
4144 goto fail;
4147 if (!tevent_req_poll(req, ev)) {
4148 status = map_nt_error_from_unix(errno);
4149 goto fail;
4152 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4154 fail:
4155 TALLOC_FREE(frame);
4156 return status;
4160 send a raw ioctl - used by the torture code
4162 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4164 uint16_t vwv[3];
4165 NTSTATUS status;
4167 SSVAL(vwv+0, 0, fnum);
4168 SSVAL(vwv+1, 0, code>>16);
4169 SSVAL(vwv+2, 0, (code&0xFFFF));
4171 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4172 NULL, 0, NULL, NULL, NULL, NULL);
4173 if (!NT_STATUS_IS_OK(status)) {
4174 return status;
4176 *blob = data_blob_null;
4177 return NT_STATUS_OK;
4180 /*********************************************************
4181 Set an extended attribute utility fn.
4182 *********************************************************/
4184 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4185 uint8_t *param, unsigned int param_len,
4186 const char *ea_name,
4187 const char *ea_val, size_t ea_len)
4189 uint16_t setup[1];
4190 unsigned int data_len = 0;
4191 uint8_t *data = NULL;
4192 char *p;
4193 size_t ea_namelen = strlen(ea_name);
4194 NTSTATUS status;
4196 SSVAL(setup, 0, setup_val);
4198 if (ea_namelen == 0 && ea_len == 0) {
4199 data_len = 4;
4200 data = talloc_array(talloc_tos(),
4201 uint8_t,
4202 data_len);
4203 if (!data) {
4204 return NT_STATUS_NO_MEMORY;
4206 p = (char *)data;
4207 SIVAL(p,0,data_len);
4208 } else {
4209 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4210 data = talloc_array(talloc_tos(),
4211 uint8_t,
4212 data_len);
4213 if (!data) {
4214 return NT_STATUS_NO_MEMORY;
4216 p = (char *)data;
4217 SIVAL(p,0,data_len);
4218 p += 4;
4219 SCVAL(p, 0, 0); /* EA flags. */
4220 SCVAL(p, 1, ea_namelen);
4221 SSVAL(p, 2, ea_len);
4222 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4223 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4226 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4227 setup, 1, 0,
4228 param, param_len, 2,
4229 data, data_len, CLI_BUFFER_SIZE,
4230 NULL,
4231 NULL, 0, NULL, /* rsetup */
4232 NULL, 0, NULL, /* rparam */
4233 NULL, 0, NULL); /* rdata */
4234 talloc_free(data);
4235 return status;
4238 /*********************************************************
4239 Set an extended attribute on a pathname.
4240 *********************************************************/
4242 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4243 const char *ea_name, const char *ea_val,
4244 size_t ea_len)
4246 unsigned int param_len = 0;
4247 uint8_t *param;
4248 NTSTATUS status;
4249 TALLOC_CTX *frame = talloc_stackframe();
4251 param = talloc_array(talloc_tos(), uint8_t, 6);
4252 if (!param) {
4253 return NT_STATUS_NO_MEMORY;
4255 SSVAL(param,0,SMB_INFO_SET_EA);
4256 SSVAL(param,2,0);
4257 SSVAL(param,4,0);
4259 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4260 path, strlen(path)+1,
4261 NULL);
4262 param_len = talloc_get_size(param);
4264 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4265 ea_name, ea_val, ea_len);
4266 talloc_free(frame);
4267 return status;
4270 /*********************************************************
4271 Set an extended attribute on an fnum.
4272 *********************************************************/
4274 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4275 const char *ea_name, const char *ea_val,
4276 size_t ea_len)
4278 uint8_t param[6];
4280 memset(param, 0, 6);
4281 SSVAL(param,0,fnum);
4282 SSVAL(param,2,SMB_INFO_SET_EA);
4284 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4285 ea_name, ea_val, ea_len);
4288 /*********************************************************
4289 Get an extended attribute list utility fn.
4290 *********************************************************/
4292 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4293 size_t rdata_len,
4294 size_t *pnum_eas, struct ea_struct **pea_list)
4296 struct ea_struct *ea_list = NULL;
4297 size_t num_eas;
4298 size_t ea_size;
4299 const uint8_t *p;
4301 if (rdata_len < 4) {
4302 return false;
4305 ea_size = (size_t)IVAL(rdata,0);
4306 if (ea_size > rdata_len) {
4307 return false;
4310 if (ea_size == 0) {
4311 /* No EA's present. */
4312 *pnum_eas = 0;
4313 *pea_list = NULL;
4314 return true;
4317 p = rdata + 4;
4318 ea_size -= 4;
4320 /* Validate the EA list and count it. */
4321 for (num_eas = 0; ea_size >= 4; num_eas++) {
4322 unsigned int ea_namelen = CVAL(p,1);
4323 unsigned int ea_valuelen = SVAL(p,2);
4324 if (ea_namelen == 0) {
4325 return false;
4327 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4328 return false;
4330 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4331 p += 4 + ea_namelen + 1 + ea_valuelen;
4334 if (num_eas == 0) {
4335 *pnum_eas = 0;
4336 *pea_list = NULL;
4337 return true;
4340 *pnum_eas = num_eas;
4341 if (!pea_list) {
4342 /* Caller only wants number of EA's. */
4343 return true;
4346 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4347 if (!ea_list) {
4348 return false;
4351 ea_size = (size_t)IVAL(rdata,0);
4352 p = rdata + 4;
4354 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4355 struct ea_struct *ea = &ea_list[num_eas];
4356 fstring unix_ea_name;
4357 unsigned int ea_namelen = CVAL(p,1);
4358 unsigned int ea_valuelen = SVAL(p,2);
4360 ea->flags = CVAL(p,0);
4361 unix_ea_name[0] = '\0';
4362 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4363 ea->name = talloc_strdup(ea_list, unix_ea_name);
4364 if (!ea->name) {
4365 goto fail;
4367 /* Ensure the value is null terminated (in case it's a string). */
4368 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4369 if (!ea->value.data) {
4370 goto fail;
4372 if (ea_valuelen) {
4373 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4375 ea->value.data[ea_valuelen] = 0;
4376 ea->value.length--;
4377 p += 4 + ea_namelen + 1 + ea_valuelen;
4380 *pea_list = ea_list;
4381 return true;
4383 fail:
4384 TALLOC_FREE(ea_list);
4385 return false;
4388 /*********************************************************
4389 Get an extended attribute list from a pathname.
4390 *********************************************************/
4392 struct cli_get_ea_list_path_state {
4393 uint32_t num_data;
4394 uint8_t *data;
4397 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4399 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4400 struct tevent_context *ev,
4401 struct cli_state *cli,
4402 const char *fname)
4404 struct tevent_req *req, *subreq;
4405 struct cli_get_ea_list_path_state *state;
4407 req = tevent_req_create(mem_ctx, &state,
4408 struct cli_get_ea_list_path_state);
4409 if (req == NULL) {
4410 return NULL;
4412 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4413 SMB_INFO_QUERY_ALL_EAS, 4,
4414 CLI_BUFFER_SIZE);
4415 if (tevent_req_nomem(subreq, req)) {
4416 return tevent_req_post(req, ev);
4418 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4419 return req;
4422 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4424 struct tevent_req *req = tevent_req_callback_data(
4425 subreq, struct tevent_req);
4426 struct cli_get_ea_list_path_state *state = tevent_req_data(
4427 req, struct cli_get_ea_list_path_state);
4428 NTSTATUS status;
4430 status = cli_qpathinfo_recv(subreq, state, &state->data,
4431 &state->num_data);
4432 TALLOC_FREE(subreq);
4433 if (tevent_req_nterror(req, status)) {
4434 return;
4436 tevent_req_done(req);
4439 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4440 size_t *pnum_eas, struct ea_struct **peas)
4442 struct cli_get_ea_list_path_state *state = tevent_req_data(
4443 req, struct cli_get_ea_list_path_state);
4444 NTSTATUS status;
4446 if (tevent_req_is_nterror(req, &status)) {
4447 return status;
4449 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4450 pnum_eas, peas)) {
4451 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4453 return NT_STATUS_OK;
4456 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4457 TALLOC_CTX *ctx,
4458 size_t *pnum_eas,
4459 struct ea_struct **pea_list)
4461 TALLOC_CTX *frame = talloc_stackframe();
4462 struct tevent_context *ev = NULL;
4463 struct tevent_req *req = NULL;
4464 NTSTATUS status = NT_STATUS_NO_MEMORY;
4466 if (smbXcli_conn_has_async_calls(cli->conn)) {
4468 * Can't use sync call while an async call is in flight
4470 status = NT_STATUS_INVALID_PARAMETER;
4471 goto fail;
4473 ev = samba_tevent_context_init(frame);
4474 if (ev == NULL) {
4475 goto fail;
4477 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4478 if (req == NULL) {
4479 goto fail;
4481 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4482 goto fail;
4484 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4485 fail:
4486 TALLOC_FREE(frame);
4487 return status;
4490 /****************************************************************************
4491 Convert open "flags" arg to uint32_t on wire.
4492 ****************************************************************************/
4494 static uint32_t open_flags_to_wire(int flags)
4496 int open_mode = flags & O_ACCMODE;
4497 uint32_t ret = 0;
4499 switch (open_mode) {
4500 case O_WRONLY:
4501 ret |= SMB_O_WRONLY;
4502 break;
4503 case O_RDWR:
4504 ret |= SMB_O_RDWR;
4505 break;
4506 default:
4507 case O_RDONLY:
4508 ret |= SMB_O_RDONLY;
4509 break;
4512 if (flags & O_CREAT) {
4513 ret |= SMB_O_CREAT;
4515 if (flags & O_EXCL) {
4516 ret |= SMB_O_EXCL;
4518 if (flags & O_TRUNC) {
4519 ret |= SMB_O_TRUNC;
4521 #if defined(O_SYNC)
4522 if (flags & O_SYNC) {
4523 ret |= SMB_O_SYNC;
4525 #endif /* O_SYNC */
4526 if (flags & O_APPEND) {
4527 ret |= SMB_O_APPEND;
4529 #if defined(O_DIRECT)
4530 if (flags & O_DIRECT) {
4531 ret |= SMB_O_DIRECT;
4533 #endif
4534 #if defined(O_DIRECTORY)
4535 if (flags & O_DIRECTORY) {
4536 ret |= SMB_O_DIRECTORY;
4538 #endif
4539 return ret;
4542 /****************************************************************************
4543 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4544 ****************************************************************************/
4546 struct posix_open_state {
4547 uint16_t setup;
4548 uint8_t *param;
4549 uint8_t data[18];
4550 uint16_t fnum; /* Out */
4553 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4555 struct tevent_req *req = tevent_req_callback_data(
4556 subreq, struct tevent_req);
4557 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4558 NTSTATUS status;
4559 uint8_t *data;
4560 uint32_t num_data;
4562 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4563 NULL, 0, NULL, &data, 12, &num_data);
4564 TALLOC_FREE(subreq);
4565 if (tevent_req_nterror(req, status)) {
4566 return;
4568 state->fnum = SVAL(data,2);
4569 tevent_req_done(req);
4572 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4573 struct tevent_context *ev,
4574 struct cli_state *cli,
4575 const char *fname,
4576 int flags,
4577 mode_t mode,
4578 bool is_dir)
4580 struct tevent_req *req = NULL, *subreq = NULL;
4581 struct posix_open_state *state = NULL;
4582 uint32_t wire_flags = open_flags_to_wire(flags);
4584 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4585 if (req == NULL) {
4586 return NULL;
4589 /* Setup setup word. */
4590 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4592 /* Setup param array. */
4593 state->param = talloc_array(state, uint8_t, 6);
4594 if (tevent_req_nomem(state->param, req)) {
4595 return tevent_req_post(req, ev);
4597 memset(state->param, '\0', 6);
4598 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4600 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4601 strlen(fname)+1, NULL);
4603 if (tevent_req_nomem(state->param, req)) {
4604 return tevent_req_post(req, ev);
4607 /* Setup data words. */
4608 if (is_dir) {
4609 wire_flags |= SMB_O_DIRECTORY;
4612 SIVAL(state->data,0,0); /* No oplock. */
4613 SIVAL(state->data,4,wire_flags);
4614 SIVAL(state->data,8,unix_perms_to_wire(mode));
4615 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4616 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4618 subreq = cli_trans_send(state, /* mem ctx. */
4619 ev, /* event ctx. */
4620 cli, /* cli_state. */
4621 SMBtrans2, /* cmd. */
4622 NULL, /* pipe name. */
4623 -1, /* fid. */
4624 0, /* function. */
4625 0, /* flags. */
4626 &state->setup, /* setup. */
4627 1, /* num setup uint16_t words. */
4628 0, /* max returned setup. */
4629 state->param, /* param. */
4630 talloc_get_size(state->param),/* num param. */
4631 2, /* max returned param. */
4632 state->data, /* data. */
4633 18, /* num data. */
4634 12); /* max returned data. */
4636 if (tevent_req_nomem(subreq, req)) {
4637 return tevent_req_post(req, ev);
4639 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4640 return req;
4643 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4644 struct tevent_context *ev,
4645 struct cli_state *cli,
4646 const char *fname,
4647 int flags,
4648 mode_t mode)
4650 return cli_posix_open_internal_send(mem_ctx, ev,
4651 cli, fname, flags, mode, false);
4654 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4656 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4657 NTSTATUS status;
4659 if (tevent_req_is_nterror(req, &status)) {
4660 return status;
4662 *pfnum = state->fnum;
4663 return NT_STATUS_OK;
4666 /****************************************************************************
4667 Open - POSIX semantics. Doesn't request oplock.
4668 ****************************************************************************/
4670 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4671 int flags, mode_t mode, uint16_t *pfnum)
4674 TALLOC_CTX *frame = talloc_stackframe();
4675 struct tevent_context *ev = NULL;
4676 struct tevent_req *req = NULL;
4677 NTSTATUS status = NT_STATUS_OK;
4679 if (smbXcli_conn_has_async_calls(cli->conn)) {
4681 * Can't use sync call while an async call is in flight
4683 status = NT_STATUS_INVALID_PARAMETER;
4684 goto fail;
4687 ev = samba_tevent_context_init(frame);
4688 if (ev == NULL) {
4689 status = NT_STATUS_NO_MEMORY;
4690 goto fail;
4693 req = cli_posix_open_send(frame,
4695 cli,
4696 fname,
4697 flags,
4698 mode);
4699 if (req == NULL) {
4700 status = NT_STATUS_NO_MEMORY;
4701 goto fail;
4704 if (!tevent_req_poll(req, ev)) {
4705 status = map_nt_error_from_unix(errno);
4706 goto fail;
4709 status = cli_posix_open_recv(req, pfnum);
4711 fail:
4712 TALLOC_FREE(frame);
4713 return status;
4716 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4717 struct tevent_context *ev,
4718 struct cli_state *cli,
4719 const char *fname,
4720 mode_t mode)
4722 return cli_posix_open_internal_send(mem_ctx, ev,
4723 cli, fname, O_CREAT, mode, true);
4726 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4728 return tevent_req_simple_recv_ntstatus(req);
4731 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4733 TALLOC_CTX *frame = talloc_stackframe();
4734 struct tevent_context *ev = NULL;
4735 struct tevent_req *req = NULL;
4736 NTSTATUS status = NT_STATUS_OK;
4738 if (smbXcli_conn_has_async_calls(cli->conn)) {
4740 * Can't use sync call while an async call is in flight
4742 status = NT_STATUS_INVALID_PARAMETER;
4743 goto fail;
4746 ev = samba_tevent_context_init(frame);
4747 if (ev == NULL) {
4748 status = NT_STATUS_NO_MEMORY;
4749 goto fail;
4752 req = cli_posix_mkdir_send(frame,
4754 cli,
4755 fname,
4756 mode);
4757 if (req == NULL) {
4758 status = NT_STATUS_NO_MEMORY;
4759 goto fail;
4762 if (!tevent_req_poll(req, ev)) {
4763 status = map_nt_error_from_unix(errno);
4764 goto fail;
4767 status = cli_posix_mkdir_recv(req);
4769 fail:
4770 TALLOC_FREE(frame);
4771 return status;
4774 /****************************************************************************
4775 unlink or rmdir - POSIX semantics.
4776 ****************************************************************************/
4778 struct cli_posix_unlink_internal_state {
4779 uint8_t data[2];
4782 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4784 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4785 struct tevent_context *ev,
4786 struct cli_state *cli,
4787 const char *fname,
4788 uint16_t level)
4790 struct tevent_req *req = NULL, *subreq = NULL;
4791 struct cli_posix_unlink_internal_state *state = NULL;
4793 req = tevent_req_create(mem_ctx, &state,
4794 struct cli_posix_unlink_internal_state);
4795 if (req == NULL) {
4796 return NULL;
4799 /* Setup data word. */
4800 SSVAL(state->data, 0, level);
4802 subreq = cli_setpathinfo_send(state, ev, cli,
4803 SMB_POSIX_PATH_UNLINK,
4804 fname,
4805 state->data, sizeof(state->data));
4806 if (tevent_req_nomem(subreq, req)) {
4807 return tevent_req_post(req, ev);
4809 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4810 return req;
4813 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4815 NTSTATUS status = cli_setpathinfo_recv(subreq);
4816 tevent_req_simple_finish_ntstatus(subreq, status);
4819 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4820 struct tevent_context *ev,
4821 struct cli_state *cli,
4822 const char *fname)
4824 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4825 SMB_POSIX_UNLINK_FILE_TARGET);
4828 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4830 return tevent_req_simple_recv_ntstatus(req);
4833 /****************************************************************************
4834 unlink - POSIX semantics.
4835 ****************************************************************************/
4837 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4839 TALLOC_CTX *frame = talloc_stackframe();
4840 struct tevent_context *ev = NULL;
4841 struct tevent_req *req = NULL;
4842 NTSTATUS status = NT_STATUS_OK;
4844 if (smbXcli_conn_has_async_calls(cli->conn)) {
4846 * Can't use sync call while an async call is in flight
4848 status = NT_STATUS_INVALID_PARAMETER;
4849 goto fail;
4852 ev = samba_tevent_context_init(frame);
4853 if (ev == NULL) {
4854 status = NT_STATUS_NO_MEMORY;
4855 goto fail;
4858 req = cli_posix_unlink_send(frame,
4860 cli,
4861 fname);
4862 if (req == NULL) {
4863 status = NT_STATUS_NO_MEMORY;
4864 goto fail;
4867 if (!tevent_req_poll(req, ev)) {
4868 status = map_nt_error_from_unix(errno);
4869 goto fail;
4872 status = cli_posix_unlink_recv(req);
4874 fail:
4875 TALLOC_FREE(frame);
4876 return status;
4879 /****************************************************************************
4880 rmdir - POSIX semantics.
4881 ****************************************************************************/
4883 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4884 struct tevent_context *ev,
4885 struct cli_state *cli,
4886 const char *fname)
4888 return cli_posix_unlink_internal_send(
4889 mem_ctx, ev, cli, fname,
4890 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4893 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4895 return tevent_req_simple_recv_ntstatus(req);
4898 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4900 TALLOC_CTX *frame = talloc_stackframe();
4901 struct tevent_context *ev = NULL;
4902 struct tevent_req *req = NULL;
4903 NTSTATUS status = NT_STATUS_OK;
4905 if (smbXcli_conn_has_async_calls(cli->conn)) {
4907 * Can't use sync call while an async call is in flight
4909 status = NT_STATUS_INVALID_PARAMETER;
4910 goto fail;
4913 ev = samba_tevent_context_init(frame);
4914 if (ev == NULL) {
4915 status = NT_STATUS_NO_MEMORY;
4916 goto fail;
4919 req = cli_posix_rmdir_send(frame,
4921 cli,
4922 fname);
4923 if (req == NULL) {
4924 status = NT_STATUS_NO_MEMORY;
4925 goto fail;
4928 if (!tevent_req_poll(req, ev)) {
4929 status = map_nt_error_from_unix(errno);
4930 goto fail;
4933 status = cli_posix_rmdir_recv(req, frame);
4935 fail:
4936 TALLOC_FREE(frame);
4937 return status;
4940 /****************************************************************************
4941 filechangenotify
4942 ****************************************************************************/
4944 struct cli_notify_state {
4945 uint8_t setup[8];
4946 uint32_t num_changes;
4947 struct notify_change *changes;
4950 static void cli_notify_done(struct tevent_req *subreq);
4952 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4953 struct tevent_context *ev,
4954 struct cli_state *cli, uint16_t fnum,
4955 uint32_t buffer_size,
4956 uint32_t completion_filter, bool recursive)
4958 struct tevent_req *req, *subreq;
4959 struct cli_notify_state *state;
4960 unsigned old_timeout;
4962 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4963 if (req == NULL) {
4964 return NULL;
4967 SIVAL(state->setup, 0, completion_filter);
4968 SSVAL(state->setup, 4, fnum);
4969 SSVAL(state->setup, 6, recursive);
4972 * Notifies should not time out
4974 old_timeout = cli_set_timeout(cli, 0);
4976 subreq = cli_trans_send(
4977 state, /* mem ctx. */
4978 ev, /* event ctx. */
4979 cli, /* cli_state. */
4980 SMBnttrans, /* cmd. */
4981 NULL, /* pipe name. */
4982 -1, /* fid. */
4983 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4984 0, /* flags. */
4985 (uint16_t *)state->setup, /* setup. */
4986 4, /* num setup uint16_t words. */
4987 0, /* max returned setup. */
4988 NULL, /* param. */
4989 0, /* num param. */
4990 buffer_size, /* max returned param. */
4991 NULL, /* data. */
4992 0, /* num data. */
4993 0); /* max returned data. */
4995 cli_set_timeout(cli, old_timeout);
4997 if (tevent_req_nomem(subreq, req)) {
4998 return tevent_req_post(req, ev);
5000 tevent_req_set_callback(subreq, cli_notify_done, req);
5001 return req;
5004 static void cli_notify_done(struct tevent_req *subreq)
5006 struct tevent_req *req = tevent_req_callback_data(
5007 subreq, struct tevent_req);
5008 struct cli_notify_state *state = tevent_req_data(
5009 req, struct cli_notify_state);
5010 NTSTATUS status;
5011 uint8_t *params;
5012 uint32_t i, ofs, num_params;
5013 uint16_t flags2;
5015 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5016 &params, 0, &num_params, NULL, 0, NULL);
5017 TALLOC_FREE(subreq);
5018 if (tevent_req_nterror(req, status)) {
5019 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5020 return;
5023 state->num_changes = 0;
5024 ofs = 0;
5026 while (num_params - ofs > 12) {
5027 uint32_t next = IVAL(params, ofs);
5028 state->num_changes += 1;
5030 if ((next == 0) || (ofs+next >= num_params)) {
5031 break;
5033 ofs += next;
5036 state->changes = talloc_array(state, struct notify_change,
5037 state->num_changes);
5038 if (tevent_req_nomem(state->changes, req)) {
5039 TALLOC_FREE(params);
5040 return;
5043 ofs = 0;
5045 for (i=0; i<state->num_changes; i++) {
5046 uint32_t next = IVAL(params, ofs);
5047 uint32_t len = IVAL(params, ofs+8);
5048 ssize_t ret;
5049 char *name;
5051 if (trans_oob(num_params, ofs + 12, len)) {
5052 TALLOC_FREE(params);
5053 tevent_req_nterror(
5054 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5055 return;
5058 state->changes[i].action = IVAL(params, ofs+4);
5059 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5060 &name, params+ofs+12, len,
5061 STR_TERMINATE|STR_UNICODE);
5062 if (ret == -1) {
5063 TALLOC_FREE(params);
5064 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5065 return;
5067 state->changes[i].name = name;
5068 ofs += next;
5071 TALLOC_FREE(params);
5072 tevent_req_done(req);
5075 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5076 uint32_t *pnum_changes,
5077 struct notify_change **pchanges)
5079 struct cli_notify_state *state = tevent_req_data(
5080 req, struct cli_notify_state);
5081 NTSTATUS status;
5083 if (tevent_req_is_nterror(req, &status)) {
5084 return status;
5087 *pnum_changes = state->num_changes;
5088 *pchanges = talloc_move(mem_ctx, &state->changes);
5089 return NT_STATUS_OK;
5092 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5093 uint32_t completion_filter, bool recursive,
5094 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5095 struct notify_change **pchanges)
5097 TALLOC_CTX *frame = talloc_stackframe();
5098 struct tevent_context *ev;
5099 struct tevent_req *req;
5100 NTSTATUS status = NT_STATUS_NO_MEMORY;
5102 if (smbXcli_conn_has_async_calls(cli->conn)) {
5104 * Can't use sync call while an async call is in flight
5106 status = NT_STATUS_INVALID_PARAMETER;
5107 goto fail;
5109 ev = samba_tevent_context_init(frame);
5110 if (ev == NULL) {
5111 goto fail;
5113 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5114 completion_filter, recursive);
5115 if (req == NULL) {
5116 goto fail;
5118 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5119 goto fail;
5121 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5122 fail:
5123 TALLOC_FREE(frame);
5124 return status;
5127 struct cli_qpathinfo_state {
5128 uint8_t *param;
5129 uint8_t *data;
5130 uint16_t setup[1];
5131 uint32_t min_rdata;
5132 uint8_t *rdata;
5133 uint32_t num_rdata;
5136 static void cli_qpathinfo_done(struct tevent_req *subreq);
5138 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5139 struct tevent_context *ev,
5140 struct cli_state *cli, const char *fname,
5141 uint16_t level, uint32_t min_rdata,
5142 uint32_t max_rdata)
5144 struct tevent_req *req, *subreq;
5145 struct cli_qpathinfo_state *state;
5147 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5148 if (req == NULL) {
5149 return NULL;
5151 state->min_rdata = min_rdata;
5152 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5154 state->param = talloc_zero_array(state, uint8_t, 6);
5155 if (tevent_req_nomem(state->param, req)) {
5156 return tevent_req_post(req, ev);
5158 SSVAL(state->param, 0, level);
5159 state->param = trans2_bytes_push_str(
5160 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5161 if (tevent_req_nomem(state->param, req)) {
5162 return tevent_req_post(req, ev);
5165 subreq = cli_trans_send(
5166 state, /* mem ctx. */
5167 ev, /* event ctx. */
5168 cli, /* cli_state. */
5169 SMBtrans2, /* cmd. */
5170 NULL, /* pipe name. */
5171 -1, /* fid. */
5172 0, /* function. */
5173 0, /* flags. */
5174 state->setup, /* setup. */
5175 1, /* num setup uint16_t words. */
5176 0, /* max returned setup. */
5177 state->param, /* param. */
5178 talloc_get_size(state->param), /* num param. */
5179 2, /* max returned param. */
5180 NULL, /* data. */
5181 0, /* num data. */
5182 max_rdata); /* max returned data. */
5184 if (tevent_req_nomem(subreq, req)) {
5185 return tevent_req_post(req, ev);
5187 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5188 return req;
5191 static void cli_qpathinfo_done(struct tevent_req *subreq)
5193 struct tevent_req *req = tevent_req_callback_data(
5194 subreq, struct tevent_req);
5195 struct cli_qpathinfo_state *state = tevent_req_data(
5196 req, struct cli_qpathinfo_state);
5197 NTSTATUS status;
5199 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5200 NULL, 0, NULL,
5201 &state->rdata, state->min_rdata,
5202 &state->num_rdata);
5203 if (tevent_req_nterror(req, status)) {
5204 return;
5206 tevent_req_done(req);
5209 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5210 uint8_t **rdata, uint32_t *num_rdata)
5212 struct cli_qpathinfo_state *state = tevent_req_data(
5213 req, struct cli_qpathinfo_state);
5214 NTSTATUS status;
5216 if (tevent_req_is_nterror(req, &status)) {
5217 return status;
5219 if (rdata != NULL) {
5220 *rdata = talloc_move(mem_ctx, &state->rdata);
5221 } else {
5222 TALLOC_FREE(state->rdata);
5224 if (num_rdata != NULL) {
5225 *num_rdata = state->num_rdata;
5227 return NT_STATUS_OK;
5230 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5231 const char *fname, uint16_t level, uint32_t min_rdata,
5232 uint32_t max_rdata,
5233 uint8_t **rdata, uint32_t *num_rdata)
5235 TALLOC_CTX *frame = talloc_stackframe();
5236 struct tevent_context *ev;
5237 struct tevent_req *req;
5238 NTSTATUS status = NT_STATUS_NO_MEMORY;
5240 if (smbXcli_conn_has_async_calls(cli->conn)) {
5242 * Can't use sync call while an async call is in flight
5244 status = NT_STATUS_INVALID_PARAMETER;
5245 goto fail;
5247 ev = samba_tevent_context_init(frame);
5248 if (ev == NULL) {
5249 goto fail;
5251 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5252 max_rdata);
5253 if (req == NULL) {
5254 goto fail;
5256 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5257 goto fail;
5259 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5260 fail:
5261 TALLOC_FREE(frame);
5262 return status;
5265 struct cli_qfileinfo_state {
5266 uint16_t setup[1];
5267 uint8_t param[4];
5268 uint8_t *data;
5269 uint16_t recv_flags2;
5270 uint32_t min_rdata;
5271 uint8_t *rdata;
5272 uint32_t num_rdata;
5275 static void cli_qfileinfo_done(struct tevent_req *subreq);
5277 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5278 struct tevent_context *ev,
5279 struct cli_state *cli, uint16_t fnum,
5280 uint16_t level, uint32_t min_rdata,
5281 uint32_t max_rdata)
5283 struct tevent_req *req, *subreq;
5284 struct cli_qfileinfo_state *state;
5286 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5287 if (req == NULL) {
5288 return NULL;
5290 state->min_rdata = min_rdata;
5291 SSVAL(state->param, 0, fnum);
5292 SSVAL(state->param, 2, level);
5293 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5295 subreq = cli_trans_send(
5296 state, /* mem ctx. */
5297 ev, /* event ctx. */
5298 cli, /* cli_state. */
5299 SMBtrans2, /* cmd. */
5300 NULL, /* pipe name. */
5301 -1, /* fid. */
5302 0, /* function. */
5303 0, /* flags. */
5304 state->setup, /* setup. */
5305 1, /* num setup uint16_t words. */
5306 0, /* max returned setup. */
5307 state->param, /* param. */
5308 sizeof(state->param), /* num param. */
5309 2, /* max returned param. */
5310 NULL, /* data. */
5311 0, /* num data. */
5312 max_rdata); /* max returned data. */
5314 if (tevent_req_nomem(subreq, req)) {
5315 return tevent_req_post(req, ev);
5317 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5318 return req;
5321 static void cli_qfileinfo_done(struct tevent_req *subreq)
5323 struct tevent_req *req = tevent_req_callback_data(
5324 subreq, struct tevent_req);
5325 struct cli_qfileinfo_state *state = tevent_req_data(
5326 req, struct cli_qfileinfo_state);
5327 NTSTATUS status;
5329 status = cli_trans_recv(subreq, state,
5330 &state->recv_flags2,
5331 NULL, 0, NULL,
5332 NULL, 0, NULL,
5333 &state->rdata, state->min_rdata,
5334 &state->num_rdata);
5335 if (tevent_req_nterror(req, status)) {
5336 return;
5338 tevent_req_done(req);
5341 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5342 uint16_t *recv_flags2,
5343 uint8_t **rdata, uint32_t *num_rdata)
5345 struct cli_qfileinfo_state *state = tevent_req_data(
5346 req, struct cli_qfileinfo_state);
5347 NTSTATUS status;
5349 if (tevent_req_is_nterror(req, &status)) {
5350 return status;
5353 if (recv_flags2 != NULL) {
5354 *recv_flags2 = state->recv_flags2;
5356 if (rdata != NULL) {
5357 *rdata = talloc_move(mem_ctx, &state->rdata);
5358 } else {
5359 TALLOC_FREE(state->rdata);
5361 if (num_rdata != NULL) {
5362 *num_rdata = state->num_rdata;
5364 return NT_STATUS_OK;
5367 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5368 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5369 uint32_t max_rdata, uint16_t *recv_flags2,
5370 uint8_t **rdata, uint32_t *num_rdata)
5372 TALLOC_CTX *frame = talloc_stackframe();
5373 struct tevent_context *ev;
5374 struct tevent_req *req;
5375 NTSTATUS status = NT_STATUS_NO_MEMORY;
5377 if (smbXcli_conn_has_async_calls(cli->conn)) {
5379 * Can't use sync call while an async call is in flight
5381 status = NT_STATUS_INVALID_PARAMETER;
5382 goto fail;
5384 ev = samba_tevent_context_init(frame);
5385 if (ev == NULL) {
5386 goto fail;
5388 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5389 max_rdata);
5390 if (req == NULL) {
5391 goto fail;
5393 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5394 goto fail;
5396 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5397 fail:
5398 TALLOC_FREE(frame);
5399 return status;
5402 struct cli_flush_state {
5403 uint16_t vwv[1];
5406 static void cli_flush_done(struct tevent_req *subreq);
5408 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5409 struct tevent_context *ev,
5410 struct cli_state *cli,
5411 uint16_t fnum)
5413 struct tevent_req *req, *subreq;
5414 struct cli_flush_state *state;
5416 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5417 if (req == NULL) {
5418 return NULL;
5420 SSVAL(state->vwv + 0, 0, fnum);
5422 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5423 0, NULL);
5424 if (tevent_req_nomem(subreq, req)) {
5425 return tevent_req_post(req, ev);
5427 tevent_req_set_callback(subreq, cli_flush_done, req);
5428 return req;
5431 static void cli_flush_done(struct tevent_req *subreq)
5433 struct tevent_req *req = tevent_req_callback_data(
5434 subreq, struct tevent_req);
5435 NTSTATUS status;
5437 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5438 TALLOC_FREE(subreq);
5439 if (tevent_req_nterror(req, status)) {
5440 return;
5442 tevent_req_done(req);
5445 NTSTATUS cli_flush_recv(struct tevent_req *req)
5447 return tevent_req_simple_recv_ntstatus(req);
5450 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5452 TALLOC_CTX *frame = talloc_stackframe();
5453 struct tevent_context *ev;
5454 struct tevent_req *req;
5455 NTSTATUS status = NT_STATUS_NO_MEMORY;
5457 if (smbXcli_conn_has_async_calls(cli->conn)) {
5459 * Can't use sync call while an async call is in flight
5461 status = NT_STATUS_INVALID_PARAMETER;
5462 goto fail;
5464 ev = samba_tevent_context_init(frame);
5465 if (ev == NULL) {
5466 goto fail;
5468 req = cli_flush_send(frame, ev, cli, fnum);
5469 if (req == NULL) {
5470 goto fail;
5472 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5473 goto fail;
5475 status = cli_flush_recv(req);
5476 fail:
5477 TALLOC_FREE(frame);
5478 return status;
5481 struct cli_shadow_copy_data_state {
5482 uint16_t setup[4];
5483 uint8_t *data;
5484 uint32_t num_data;
5485 bool get_names;
5488 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5490 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5491 struct tevent_context *ev,
5492 struct cli_state *cli,
5493 uint16_t fnum,
5494 bool get_names)
5496 struct tevent_req *req, *subreq;
5497 struct cli_shadow_copy_data_state *state;
5498 uint32_t ret_size;
5500 req = tevent_req_create(mem_ctx, &state,
5501 struct cli_shadow_copy_data_state);
5502 if (req == NULL) {
5503 return NULL;
5505 state->get_names = get_names;
5506 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5508 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5509 SSVAL(state->setup + 2, 0, fnum);
5510 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5511 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5513 subreq = cli_trans_send(
5514 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5515 state->setup, ARRAY_SIZE(state->setup), 0,
5516 NULL, 0, 0,
5517 NULL, 0, ret_size);
5518 if (tevent_req_nomem(subreq, req)) {
5519 return tevent_req_post(req, ev);
5521 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5522 return req;
5525 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5527 struct tevent_req *req = tevent_req_callback_data(
5528 subreq, struct tevent_req);
5529 struct cli_shadow_copy_data_state *state = tevent_req_data(
5530 req, struct cli_shadow_copy_data_state);
5531 NTSTATUS status;
5533 status = cli_trans_recv(subreq, state, NULL,
5534 NULL, 0, NULL, /* setup */
5535 NULL, 0, NULL, /* param */
5536 &state->data, 12, &state->num_data);
5537 TALLOC_FREE(subreq);
5538 if (tevent_req_nterror(req, status)) {
5539 return;
5541 tevent_req_done(req);
5544 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5545 char ***pnames, int *pnum_names)
5547 struct cli_shadow_copy_data_state *state = tevent_req_data(
5548 req, struct cli_shadow_copy_data_state);
5549 char **names;
5550 int i, num_names;
5551 uint32_t dlength;
5552 NTSTATUS status;
5554 if (tevent_req_is_nterror(req, &status)) {
5555 return status;
5557 num_names = IVAL(state->data, 4);
5558 dlength = IVAL(state->data, 8);
5560 if (!state->get_names) {
5561 *pnum_names = num_names;
5562 return NT_STATUS_OK;
5565 if (dlength+12 > state->num_data) {
5566 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5568 names = talloc_array(mem_ctx, char *, num_names);
5569 if (names == NULL) {
5570 return NT_STATUS_NO_MEMORY;
5573 for (i=0; i<num_names; i++) {
5574 bool ret;
5575 uint8_t *src;
5576 size_t converted_size;
5578 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5579 ret = convert_string_talloc(
5580 names, CH_UTF16LE, CH_UNIX,
5581 src, 2 * sizeof(SHADOW_COPY_LABEL),
5582 &names[i], &converted_size);
5583 if (!ret) {
5584 TALLOC_FREE(names);
5585 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5588 *pnum_names = num_names;
5589 *pnames = names;
5590 return NT_STATUS_OK;
5593 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5594 uint16_t fnum, bool get_names,
5595 char ***pnames, int *pnum_names)
5597 TALLOC_CTX *frame = talloc_stackframe();
5598 struct tevent_context *ev;
5599 struct tevent_req *req;
5600 NTSTATUS status = NT_STATUS_NO_MEMORY;
5602 if (smbXcli_conn_has_async_calls(cli->conn)) {
5604 * Can't use sync call while an async call is in flight
5606 status = NT_STATUS_INVALID_PARAMETER;
5607 goto fail;
5609 ev = samba_tevent_context_init(frame);
5610 if (ev == NULL) {
5611 goto fail;
5613 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5614 if (req == NULL) {
5615 goto fail;
5617 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5618 goto fail;
5620 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5621 fail:
5622 TALLOC_FREE(frame);
5623 return status;