idmap-autorid: Slightly simplify idmap_autorid_get_domainrange
[Samba.git] / source3 / libsmb / clifile.c
blob72f0e4b8eb76bb0ce1a793e0815bc39896dd1a67
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 "async_smb.h"
23 #include "libsmb/clirap.h"
25 /***********************************************************
26 Common function for pushing stings, used by smb_bytes_push_str()
27 and trans_bytes_push_str(). Only difference is the align_odd
28 parameter setting.
29 ***********************************************************/
31 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
32 const char *str, size_t str_len,
33 bool align_odd,
34 size_t *pconverted_size)
36 size_t buflen;
37 char *converted;
38 size_t converted_size;
40 if (buf == NULL) {
41 return NULL;
44 buflen = talloc_get_size(buf);
46 if (align_odd && ucs2 && (buflen % 2 == 0)) {
48 * We're pushing into an SMB buffer, align odd
50 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
51 if (buf == NULL) {
52 return NULL;
54 buf[buflen] = '\0';
55 buflen += 1;
58 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
59 ucs2 ? CH_UTF16LE : CH_DOS,
60 str, str_len, &converted,
61 &converted_size, true)) {
62 return NULL;
65 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
66 buflen + converted_size);
67 if (buf == NULL) {
68 TALLOC_FREE(converted);
69 return NULL;
72 memcpy(buf + buflen, converted, converted_size);
74 TALLOC_FREE(converted);
76 if (pconverted_size) {
77 *pconverted_size = converted_size;
80 return buf;
83 /***********************************************************
84 Push a string into an SMB buffer, with odd byte alignment
85 if it's a UCS2 string.
86 ***********************************************************/
88 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
89 const char *str, size_t str_len,
90 size_t *pconverted_size)
92 return internal_bytes_push_str(buf, ucs2, str, str_len,
93 true, pconverted_size);
96 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
97 const uint8_t *bytes, size_t num_bytes)
99 size_t buflen;
101 if (buf == NULL) {
102 return NULL;
104 buflen = talloc_get_size(buf);
106 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
107 buflen + 1 + num_bytes);
108 if (buf == NULL) {
109 return NULL;
111 buf[buflen] = prefix;
112 memcpy(&buf[buflen+1], bytes, num_bytes);
113 return buf;
116 /***********************************************************
117 Same as smb_bytes_push_str(), but without the odd byte
118 align for ucs2 (we're pushing into a param or data block).
119 static for now, although this will probably change when
120 other modules use async trans calls.
121 ***********************************************************/
123 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
124 const char *str, size_t str_len,
125 size_t *pconverted_size)
127 return internal_bytes_push_str(buf, ucs2, str, str_len,
128 false, pconverted_size);
131 struct cli_setpathinfo_state {
132 uint16_t setup;
133 uint8_t *param;
136 static void cli_setpathinfo_done(struct tevent_req *subreq);
138 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
139 struct tevent_context *ev,
140 struct cli_state *cli,
141 uint16_t level,
142 const char *path,
143 uint8_t *data,
144 size_t data_len)
146 struct tevent_req *req, *subreq;
147 struct cli_setpathinfo_state *state;
149 req = tevent_req_create(mem_ctx, &state,
150 struct cli_setpathinfo_state);
151 if (req == NULL) {
152 return NULL;
155 /* Setup setup word. */
156 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
158 /* Setup param array. */
159 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
160 if (tevent_req_nomem(state->param, req)) {
161 return tevent_req_post(req, ev);
163 SSVAL(state->param, 0, level);
165 state->param = trans2_bytes_push_str(
166 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
167 if (tevent_req_nomem(state->param, req)) {
168 return tevent_req_post(req, ev);
171 subreq = cli_trans_send(
172 state, /* mem ctx. */
173 ev, /* event ctx. */
174 cli, /* cli_state. */
175 SMBtrans2, /* cmd. */
176 NULL, /* pipe name. */
177 -1, /* fid. */
178 0, /* function. */
179 0, /* flags. */
180 &state->setup, /* setup. */
181 1, /* num setup uint16_t words. */
182 0, /* max returned setup. */
183 state->param, /* param. */
184 talloc_get_size(state->param), /* num param. */
185 2, /* max returned param. */
186 data, /* data. */
187 data_len, /* num data. */
188 0); /* max returned data. */
190 if (tevent_req_nomem(subreq, req)) {
191 return tevent_req_post(req, ev);
193 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
194 return req;
197 static void cli_setpathinfo_done(struct tevent_req *subreq)
199 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
200 NULL, 0, NULL, NULL, 0, NULL);
201 tevent_req_simple_finish_ntstatus(subreq, status);
204 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
206 return tevent_req_simple_recv_ntstatus(req);
209 NTSTATUS cli_setpathinfo(struct cli_state *cli,
210 uint16_t level,
211 const char *path,
212 uint8_t *data,
213 size_t data_len)
215 TALLOC_CTX *frame = talloc_stackframe();
216 struct tevent_context *ev;
217 struct tevent_req *req;
218 NTSTATUS status = NT_STATUS_NO_MEMORY;
220 if (cli_has_async_calls(cli)) {
222 * Can't use sync call while an async call is in flight
224 status = NT_STATUS_INVALID_PARAMETER;
225 goto fail;
227 ev = tevent_context_init(frame);
228 if (ev == NULL) {
229 goto fail;
231 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
232 if (req == NULL) {
233 goto fail;
235 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
236 goto fail;
238 status = cli_setpathinfo_recv(req);
239 fail:
240 TALLOC_FREE(frame);
241 return status;
244 /****************************************************************************
245 Hard/Symlink a file (UNIX extensions).
246 Creates new name (sym)linked to oldname.
247 ****************************************************************************/
249 struct cli_posix_link_internal_state {
250 uint8_t *data;
253 static void cli_posix_link_internal_done(struct tevent_req *subreq);
255 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
256 struct event_context *ev,
257 struct cli_state *cli,
258 uint16_t level,
259 const char *oldname,
260 const char *newname)
262 struct tevent_req *req = NULL, *subreq = NULL;
263 struct cli_posix_link_internal_state *state = NULL;
265 req = tevent_req_create(mem_ctx, &state,
266 struct cli_posix_link_internal_state);
267 if (req == NULL) {
268 return NULL;
271 /* Setup data array. */
272 state->data = talloc_array(state, uint8_t, 0);
273 if (tevent_req_nomem(state->data, req)) {
274 return tevent_req_post(req, ev);
276 state->data = trans2_bytes_push_str(
277 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
279 subreq = cli_setpathinfo_send(
280 state, ev, cli, level, newname,
281 state->data, talloc_get_size(state->data));
282 if (tevent_req_nomem(subreq, req)) {
283 return tevent_req_post(req, ev);
285 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
286 return req;
289 static void cli_posix_link_internal_done(struct tevent_req *subreq)
291 NTSTATUS status = cli_setpathinfo_recv(subreq);
292 tevent_req_simple_finish_ntstatus(subreq, status);
295 /****************************************************************************
296 Symlink a file (UNIX extensions).
297 ****************************************************************************/
299 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
300 struct event_context *ev,
301 struct cli_state *cli,
302 const char *oldname,
303 const char *newname)
305 return cli_posix_link_internal_send(
306 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
309 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
311 return tevent_req_simple_recv_ntstatus(req);
314 NTSTATUS cli_posix_symlink(struct cli_state *cli,
315 const char *oldname,
316 const char *newname)
318 TALLOC_CTX *frame = talloc_stackframe();
319 struct event_context *ev = NULL;
320 struct tevent_req *req = NULL;
321 NTSTATUS status = NT_STATUS_OK;
323 if (cli_has_async_calls(cli)) {
325 * Can't use sync call while an async call is in flight
327 status = NT_STATUS_INVALID_PARAMETER;
328 goto fail;
331 ev = event_context_init(frame);
332 if (ev == NULL) {
333 status = NT_STATUS_NO_MEMORY;
334 goto fail;
337 req = cli_posix_symlink_send(frame,
339 cli,
340 oldname,
341 newname);
342 if (req == NULL) {
343 status = NT_STATUS_NO_MEMORY;
344 goto fail;
347 if (!tevent_req_poll(req, ev)) {
348 status = map_nt_error_from_unix(errno);
349 goto fail;
352 status = cli_posix_symlink_recv(req);
354 fail:
355 TALLOC_FREE(frame);
356 if (!NT_STATUS_IS_OK(status)) {
357 cli_set_error(cli, status);
359 return status;
362 /****************************************************************************
363 Read a POSIX symlink.
364 ****************************************************************************/
366 struct readlink_state {
367 uint8_t *data;
368 uint32_t num_data;
371 static void cli_posix_readlink_done(struct tevent_req *subreq);
373 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
374 struct event_context *ev,
375 struct cli_state *cli,
376 const char *fname,
377 size_t len)
379 struct tevent_req *req = NULL, *subreq = NULL;
380 struct readlink_state *state = NULL;
381 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
383 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
384 if (req == NULL) {
385 return NULL;
389 * Len is in bytes, we need it in UCS2 units.
391 if ((2*len < len) || (maxbytelen < len)) {
392 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
393 return tevent_req_post(req, ev);
396 subreq = cli_qpathinfo_send(state, ev, cli, fname,
397 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
398 if (tevent_req_nomem(subreq, req)) {
399 return tevent_req_post(req, ev);
401 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
402 return req;
405 static void cli_posix_readlink_done(struct tevent_req *subreq)
407 struct tevent_req *req = tevent_req_callback_data(
408 subreq, struct tevent_req);
409 struct readlink_state *state = tevent_req_data(
410 req, struct readlink_state);
411 NTSTATUS status;
413 status = cli_qpathinfo_recv(subreq, state, &state->data,
414 &state->num_data);
415 TALLOC_FREE(subreq);
416 if (!NT_STATUS_IS_OK(status)) {
417 tevent_req_nterror(req, status);
418 return;
421 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
423 if (state->data[state->num_data-1] != '\0') {
424 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
425 return;
427 tevent_req_done(req);
430 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
431 char *retpath, size_t len)
433 NTSTATUS status;
434 char *converted = NULL;
435 size_t converted_size = 0;
436 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
438 if (tevent_req_is_nterror(req, &status)) {
439 return status;
441 /* The returned data is a pushed string, not raw data. */
442 if (!convert_string_talloc(state,
443 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
444 CH_UNIX,
445 state->data,
446 state->num_data,
447 &converted,
448 &converted_size,
449 true)) {
450 return NT_STATUS_NO_MEMORY;
453 len = MIN(len,converted_size);
454 if (len == 0) {
455 return NT_STATUS_DATA_ERROR;
457 memcpy(retpath, converted, len);
458 return NT_STATUS_OK;
461 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
462 char *linkpath, size_t len)
464 TALLOC_CTX *frame = talloc_stackframe();
465 struct event_context *ev = NULL;
466 struct tevent_req *req = NULL;
467 NTSTATUS status = NT_STATUS_OK;
469 if (cli_has_async_calls(cli)) {
471 * Can't use sync call while an async call is in flight
473 status = NT_STATUS_INVALID_PARAMETER;
474 goto fail;
477 ev = event_context_init(frame);
478 if (ev == NULL) {
479 status = NT_STATUS_NO_MEMORY;
480 goto fail;
483 req = cli_posix_readlink_send(frame,
485 cli,
486 fname,
487 len);
488 if (req == NULL) {
489 status = NT_STATUS_NO_MEMORY;
490 goto fail;
493 if (!tevent_req_poll(req, ev)) {
494 status = map_nt_error_from_unix(errno);
495 goto fail;
498 status = cli_posix_readlink_recv(req, cli, linkpath, len);
500 fail:
501 TALLOC_FREE(frame);
502 if (!NT_STATUS_IS_OK(status)) {
503 cli_set_error(cli, status);
505 return status;
508 /****************************************************************************
509 Hard link a file (UNIX extensions).
510 ****************************************************************************/
512 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
513 struct event_context *ev,
514 struct cli_state *cli,
515 const char *oldname,
516 const char *newname)
518 return cli_posix_link_internal_send(
519 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
522 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
524 return tevent_req_simple_recv_ntstatus(req);
527 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
528 const char *oldname,
529 const char *newname)
531 TALLOC_CTX *frame = talloc_stackframe();
532 struct event_context *ev = NULL;
533 struct tevent_req *req = NULL;
534 NTSTATUS status = NT_STATUS_OK;
536 if (cli_has_async_calls(cli)) {
538 * Can't use sync call while an async call is in flight
540 status = NT_STATUS_INVALID_PARAMETER;
541 goto fail;
544 ev = event_context_init(frame);
545 if (ev == NULL) {
546 status = NT_STATUS_NO_MEMORY;
547 goto fail;
550 req = cli_posix_hardlink_send(frame,
552 cli,
553 oldname,
554 newname);
555 if (req == NULL) {
556 status = NT_STATUS_NO_MEMORY;
557 goto fail;
560 if (!tevent_req_poll(req, ev)) {
561 status = map_nt_error_from_unix(errno);
562 goto fail;
565 status = cli_posix_hardlink_recv(req);
567 fail:
568 TALLOC_FREE(frame);
569 if (!NT_STATUS_IS_OK(status)) {
570 cli_set_error(cli, status);
572 return status;
575 /****************************************************************************
576 Map standard UNIX permissions onto wire representations.
577 ****************************************************************************/
579 uint32_t unix_perms_to_wire(mode_t perms)
581 unsigned int ret = 0;
583 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
584 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
585 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
586 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
587 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
588 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
589 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
590 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
591 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
592 #ifdef S_ISVTX
593 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
594 #endif
595 #ifdef S_ISGID
596 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
597 #endif
598 #ifdef S_ISUID
599 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
600 #endif
601 return ret;
604 /****************************************************************************
605 Map wire permissions to standard UNIX.
606 ****************************************************************************/
608 mode_t wire_perms_to_unix(uint32_t perms)
610 mode_t ret = (mode_t)0;
612 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
613 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
614 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
615 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
616 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
617 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
618 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
619 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
620 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
621 #ifdef S_ISVTX
622 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
623 #endif
624 #ifdef S_ISGID
625 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
626 #endif
627 #ifdef S_ISUID
628 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
629 #endif
630 return ret;
633 /****************************************************************************
634 Return the file type from the wire filetype for UNIX extensions.
635 ****************************************************************************/
637 static mode_t unix_filetype_from_wire(uint32_t wire_type)
639 switch (wire_type) {
640 case UNIX_TYPE_FILE:
641 return S_IFREG;
642 case UNIX_TYPE_DIR:
643 return S_IFDIR;
644 #ifdef S_IFLNK
645 case UNIX_TYPE_SYMLINK:
646 return S_IFLNK;
647 #endif
648 #ifdef S_IFCHR
649 case UNIX_TYPE_CHARDEV:
650 return S_IFCHR;
651 #endif
652 #ifdef S_IFBLK
653 case UNIX_TYPE_BLKDEV:
654 return S_IFBLK;
655 #endif
656 #ifdef S_IFIFO
657 case UNIX_TYPE_FIFO:
658 return S_IFIFO;
659 #endif
660 #ifdef S_IFSOCK
661 case UNIX_TYPE_SOCKET:
662 return S_IFSOCK;
663 #endif
664 default:
665 return (mode_t)0;
669 /****************************************************************************
670 Do a POSIX getfacl (UNIX extensions).
671 ****************************************************************************/
673 struct getfacl_state {
674 uint32_t num_data;
675 uint8_t *data;
678 static void cli_posix_getfacl_done(struct tevent_req *subreq);
680 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
681 struct event_context *ev,
682 struct cli_state *cli,
683 const char *fname)
685 struct tevent_req *req = NULL, *subreq = NULL;
686 struct getfacl_state *state = NULL;
688 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
689 if (req == NULL) {
690 return NULL;
692 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
693 0, cli->max_xmit);
694 if (tevent_req_nomem(subreq, req)) {
695 return tevent_req_post(req, ev);
697 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
698 return req;
701 static void cli_posix_getfacl_done(struct tevent_req *subreq)
703 struct tevent_req *req = tevent_req_callback_data(
704 subreq, struct tevent_req);
705 struct getfacl_state *state = tevent_req_data(
706 req, struct getfacl_state);
707 NTSTATUS status;
709 status = cli_qpathinfo_recv(subreq, state, &state->data,
710 &state->num_data);
711 TALLOC_FREE(subreq);
712 if (!NT_STATUS_IS_OK(status)) {
713 tevent_req_nterror(req, status);
714 return;
716 tevent_req_done(req);
719 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
720 TALLOC_CTX *mem_ctx,
721 size_t *prb_size,
722 char **retbuf)
724 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
725 NTSTATUS status;
727 if (tevent_req_is_nterror(req, &status)) {
728 return status;
730 *prb_size = (size_t)state->num_data;
731 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
732 return NT_STATUS_OK;
735 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
736 const char *fname,
737 TALLOC_CTX *mem_ctx,
738 size_t *prb_size,
739 char **retbuf)
741 TALLOC_CTX *frame = talloc_stackframe();
742 struct event_context *ev = NULL;
743 struct tevent_req *req = NULL;
744 NTSTATUS status = NT_STATUS_OK;
746 if (cli_has_async_calls(cli)) {
748 * Can't use sync call while an async call is in flight
750 status = NT_STATUS_INVALID_PARAMETER;
751 goto fail;
754 ev = event_context_init(frame);
755 if (ev == NULL) {
756 status = NT_STATUS_NO_MEMORY;
757 goto fail;
760 req = cli_posix_getfacl_send(frame,
762 cli,
763 fname);
764 if (req == NULL) {
765 status = NT_STATUS_NO_MEMORY;
766 goto fail;
769 if (!tevent_req_poll(req, ev)) {
770 status = map_nt_error_from_unix(errno);
771 goto fail;
774 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
776 fail:
777 TALLOC_FREE(frame);
778 if (!NT_STATUS_IS_OK(status)) {
779 cli_set_error(cli, status);
781 return status;
784 /****************************************************************************
785 Stat a file (UNIX extensions).
786 ****************************************************************************/
788 struct stat_state {
789 uint32_t num_data;
790 uint8_t *data;
793 static void cli_posix_stat_done(struct tevent_req *subreq);
795 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
796 struct event_context *ev,
797 struct cli_state *cli,
798 const char *fname)
800 struct tevent_req *req = NULL, *subreq = NULL;
801 struct stat_state *state = NULL;
803 req = tevent_req_create(mem_ctx, &state, struct stat_state);
804 if (req == NULL) {
805 return NULL;
807 subreq = cli_qpathinfo_send(state, ev, cli, fname,
808 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
809 if (tevent_req_nomem(subreq, req)) {
810 return tevent_req_post(req, ev);
812 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
813 return req;
816 static void cli_posix_stat_done(struct tevent_req *subreq)
818 struct tevent_req *req = tevent_req_callback_data(
819 subreq, struct tevent_req);
820 struct stat_state *state = tevent_req_data(req, struct stat_state);
821 NTSTATUS status;
823 status = cli_qpathinfo_recv(subreq, state, &state->data,
824 &state->num_data);
825 TALLOC_FREE(subreq);
826 if (!NT_STATUS_IS_OK(status)) {
827 tevent_req_nterror(req, status);
828 return;
830 tevent_req_done(req);
833 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
834 SMB_STRUCT_STAT *sbuf)
836 struct stat_state *state = tevent_req_data(req, struct stat_state);
837 NTSTATUS status;
839 if (tevent_req_is_nterror(req, &status)) {
840 return status;
843 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
844 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
845 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
846 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
847 #else
848 /* assume 512 byte blocks */
849 sbuf->st_ex_blocks /= 512;
850 #endif
851 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
852 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
853 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
855 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
856 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
857 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
858 #if defined(HAVE_MAKEDEV)
860 uint32_t dev_major = IVAL(state->data,60);
861 uint32_t dev_minor = IVAL(state->data,68);
862 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
864 #endif
865 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
866 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
867 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
869 return NT_STATUS_OK;
872 NTSTATUS cli_posix_stat(struct cli_state *cli,
873 const char *fname,
874 SMB_STRUCT_STAT *sbuf)
876 TALLOC_CTX *frame = talloc_stackframe();
877 struct event_context *ev = NULL;
878 struct tevent_req *req = NULL;
879 NTSTATUS status = NT_STATUS_OK;
881 if (cli_has_async_calls(cli)) {
883 * Can't use sync call while an async call is in flight
885 status = NT_STATUS_INVALID_PARAMETER;
886 goto fail;
889 ev = event_context_init(frame);
890 if (ev == NULL) {
891 status = NT_STATUS_NO_MEMORY;
892 goto fail;
895 req = cli_posix_stat_send(frame,
897 cli,
898 fname);
899 if (req == NULL) {
900 status = NT_STATUS_NO_MEMORY;
901 goto fail;
904 if (!tevent_req_poll(req, ev)) {
905 status = map_nt_error_from_unix(errno);
906 goto fail;
909 status = cli_posix_stat_recv(req, sbuf);
911 fail:
912 TALLOC_FREE(frame);
913 if (!NT_STATUS_IS_OK(status)) {
914 cli_set_error(cli, status);
916 return status;
919 /****************************************************************************
920 Chmod or chown a file internal (UNIX extensions).
921 ****************************************************************************/
923 struct cli_posix_chown_chmod_internal_state {
924 uint8_t data[100];
927 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
929 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
930 struct event_context *ev,
931 struct cli_state *cli,
932 const char *fname,
933 uint32_t mode,
934 uint32_t uid,
935 uint32_t gid)
937 struct tevent_req *req = NULL, *subreq = NULL;
938 struct cli_posix_chown_chmod_internal_state *state = NULL;
940 req = tevent_req_create(mem_ctx, &state,
941 struct cli_posix_chown_chmod_internal_state);
942 if (req == NULL) {
943 return NULL;
946 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
947 memset(&state->data[40], '\0', 60);
948 SIVAL(state->data,40,uid);
949 SIVAL(state->data,48,gid);
950 SIVAL(state->data,84,mode);
952 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
953 fname, state->data, sizeof(state->data));
954 if (tevent_req_nomem(subreq, req)) {
955 return tevent_req_post(req, ev);
957 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
958 req);
959 return req;
962 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
964 NTSTATUS status = cli_setpathinfo_recv(subreq);
965 tevent_req_simple_finish_ntstatus(subreq, status);
968 /****************************************************************************
969 chmod a file (UNIX extensions).
970 ****************************************************************************/
972 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
973 struct event_context *ev,
974 struct cli_state *cli,
975 const char *fname,
976 mode_t mode)
978 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
979 fname,
980 unix_perms_to_wire(mode),
981 SMB_UID_NO_CHANGE,
982 SMB_GID_NO_CHANGE);
985 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
987 return tevent_req_simple_recv_ntstatus(req);
990 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
992 TALLOC_CTX *frame = talloc_stackframe();
993 struct event_context *ev = NULL;
994 struct tevent_req *req = NULL;
995 NTSTATUS status = NT_STATUS_OK;
997 if (cli_has_async_calls(cli)) {
999 * Can't use sync call while an async call is in flight
1001 status = NT_STATUS_INVALID_PARAMETER;
1002 goto fail;
1005 ev = event_context_init(frame);
1006 if (ev == NULL) {
1007 status = NT_STATUS_NO_MEMORY;
1008 goto fail;
1011 req = cli_posix_chmod_send(frame,
1013 cli,
1014 fname,
1015 mode);
1016 if (req == NULL) {
1017 status = NT_STATUS_NO_MEMORY;
1018 goto fail;
1021 if (!tevent_req_poll(req, ev)) {
1022 status = map_nt_error_from_unix(errno);
1023 goto fail;
1026 status = cli_posix_chmod_recv(req);
1028 fail:
1029 TALLOC_FREE(frame);
1030 if (!NT_STATUS_IS_OK(status)) {
1031 cli_set_error(cli, status);
1033 return status;
1036 /****************************************************************************
1037 chown a file (UNIX extensions).
1038 ****************************************************************************/
1040 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1041 struct event_context *ev,
1042 struct cli_state *cli,
1043 const char *fname,
1044 uid_t uid,
1045 gid_t gid)
1047 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1048 fname,
1049 SMB_MODE_NO_CHANGE,
1050 (uint32_t)uid,
1051 (uint32_t)gid);
1054 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1056 return tevent_req_simple_recv_ntstatus(req);
1059 NTSTATUS cli_posix_chown(struct cli_state *cli,
1060 const char *fname,
1061 uid_t uid,
1062 gid_t gid)
1064 TALLOC_CTX *frame = talloc_stackframe();
1065 struct event_context *ev = NULL;
1066 struct tevent_req *req = NULL;
1067 NTSTATUS status = NT_STATUS_OK;
1069 if (cli_has_async_calls(cli)) {
1071 * Can't use sync call while an async call is in flight
1073 status = NT_STATUS_INVALID_PARAMETER;
1074 goto fail;
1077 ev = event_context_init(frame);
1078 if (ev == NULL) {
1079 status = NT_STATUS_NO_MEMORY;
1080 goto fail;
1083 req = cli_posix_chown_send(frame,
1085 cli,
1086 fname,
1087 uid,
1088 gid);
1089 if (req == NULL) {
1090 status = NT_STATUS_NO_MEMORY;
1091 goto fail;
1094 if (!tevent_req_poll(req, ev)) {
1095 status = map_nt_error_from_unix(errno);
1096 goto fail;
1099 status = cli_posix_chown_recv(req);
1101 fail:
1102 TALLOC_FREE(frame);
1103 if (!NT_STATUS_IS_OK(status)) {
1104 cli_set_error(cli, status);
1106 return status;
1109 /****************************************************************************
1110 Rename a file.
1111 ****************************************************************************/
1113 static void cli_rename_done(struct tevent_req *subreq);
1115 struct cli_rename_state {
1116 uint16_t vwv[1];
1119 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1120 struct event_context *ev,
1121 struct cli_state *cli,
1122 const char *fname_src,
1123 const char *fname_dst)
1125 struct tevent_req *req = NULL, *subreq = NULL;
1126 struct cli_rename_state *state = NULL;
1127 uint8_t additional_flags = 0;
1128 uint8_t *bytes = NULL;
1130 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1131 if (req == NULL) {
1132 return NULL;
1135 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1137 bytes = talloc_array(state, uint8_t, 1);
1138 if (tevent_req_nomem(bytes, req)) {
1139 return tevent_req_post(req, ev);
1141 bytes[0] = 4;
1142 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1143 strlen(fname_src)+1, NULL);
1144 if (tevent_req_nomem(bytes, req)) {
1145 return tevent_req_post(req, ev);
1148 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1149 talloc_get_size(bytes)+1);
1150 if (tevent_req_nomem(bytes, req)) {
1151 return tevent_req_post(req, ev);
1154 bytes[talloc_get_size(bytes)-1] = 4;
1155 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1156 strlen(fname_dst)+1, NULL);
1157 if (tevent_req_nomem(bytes, req)) {
1158 return tevent_req_post(req, ev);
1161 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1162 1, state->vwv, talloc_get_size(bytes), bytes);
1163 if (tevent_req_nomem(subreq, req)) {
1164 return tevent_req_post(req, ev);
1166 tevent_req_set_callback(subreq, cli_rename_done, req);
1167 return req;
1170 static void cli_rename_done(struct tevent_req *subreq)
1172 struct tevent_req *req = tevent_req_callback_data(
1173 subreq, struct tevent_req);
1174 NTSTATUS status;
1176 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1177 TALLOC_FREE(subreq);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 tevent_req_nterror(req, status);
1180 return;
1182 tevent_req_done(req);
1185 NTSTATUS cli_rename_recv(struct tevent_req *req)
1187 return tevent_req_simple_recv_ntstatus(req);
1190 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1192 TALLOC_CTX *frame = talloc_stackframe();
1193 struct event_context *ev;
1194 struct tevent_req *req;
1195 NTSTATUS status = NT_STATUS_OK;
1197 if (cli_has_async_calls(cli)) {
1199 * Can't use sync call while an async call is in flight
1201 status = NT_STATUS_INVALID_PARAMETER;
1202 goto fail;
1205 ev = event_context_init(frame);
1206 if (ev == NULL) {
1207 status = NT_STATUS_NO_MEMORY;
1208 goto fail;
1211 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1212 if (req == NULL) {
1213 status = NT_STATUS_NO_MEMORY;
1214 goto fail;
1217 if (!tevent_req_poll(req, ev)) {
1218 status = map_nt_error_from_unix(errno);
1219 goto fail;
1222 status = cli_rename_recv(req);
1224 fail:
1225 TALLOC_FREE(frame);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 cli_set_error(cli, status);
1229 return status;
1232 /****************************************************************************
1233 NT Rename a file.
1234 ****************************************************************************/
1236 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1238 struct cli_ntrename_internal_state {
1239 uint16_t vwv[4];
1242 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1243 struct event_context *ev,
1244 struct cli_state *cli,
1245 const char *fname_src,
1246 const char *fname_dst,
1247 uint16_t rename_flag)
1249 struct tevent_req *req = NULL, *subreq = NULL;
1250 struct cli_ntrename_internal_state *state = NULL;
1251 uint8_t additional_flags = 0;
1252 uint8_t *bytes = NULL;
1254 req = tevent_req_create(mem_ctx, &state,
1255 struct cli_ntrename_internal_state);
1256 if (req == NULL) {
1257 return NULL;
1260 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1261 SSVAL(state->vwv+1, 0, rename_flag);
1263 bytes = talloc_array(state, uint8_t, 1);
1264 if (tevent_req_nomem(bytes, req)) {
1265 return tevent_req_post(req, ev);
1267 bytes[0] = 4;
1268 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1269 strlen(fname_src)+1, NULL);
1270 if (tevent_req_nomem(bytes, req)) {
1271 return tevent_req_post(req, ev);
1274 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1275 talloc_get_size(bytes)+1);
1276 if (tevent_req_nomem(bytes, req)) {
1277 return tevent_req_post(req, ev);
1280 bytes[talloc_get_size(bytes)-1] = 4;
1281 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1282 strlen(fname_dst)+1, NULL);
1283 if (tevent_req_nomem(bytes, req)) {
1284 return tevent_req_post(req, ev);
1287 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1288 4, state->vwv, talloc_get_size(bytes), bytes);
1289 if (tevent_req_nomem(subreq, req)) {
1290 return tevent_req_post(req, ev);
1292 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1293 return req;
1296 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1298 struct tevent_req *req = tevent_req_callback_data(
1299 subreq, struct tevent_req);
1300 NTSTATUS status;
1302 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1303 TALLOC_FREE(subreq);
1304 if (!NT_STATUS_IS_OK(status)) {
1305 tevent_req_nterror(req, status);
1306 return;
1308 tevent_req_done(req);
1311 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1313 return tevent_req_simple_recv_ntstatus(req);
1316 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1317 struct event_context *ev,
1318 struct cli_state *cli,
1319 const char *fname_src,
1320 const char *fname_dst)
1322 return cli_ntrename_internal_send(mem_ctx,
1324 cli,
1325 fname_src,
1326 fname_dst,
1327 RENAME_FLAG_RENAME);
1330 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1332 return cli_ntrename_internal_recv(req);
1335 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1337 TALLOC_CTX *frame = talloc_stackframe();
1338 struct event_context *ev;
1339 struct tevent_req *req;
1340 NTSTATUS status = NT_STATUS_OK;
1342 if (cli_has_async_calls(cli)) {
1344 * Can't use sync call while an async call is in flight
1346 status = NT_STATUS_INVALID_PARAMETER;
1347 goto fail;
1350 ev = event_context_init(frame);
1351 if (ev == NULL) {
1352 status = NT_STATUS_NO_MEMORY;
1353 goto fail;
1356 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1357 if (req == NULL) {
1358 status = NT_STATUS_NO_MEMORY;
1359 goto fail;
1362 if (!tevent_req_poll(req, ev)) {
1363 status = map_nt_error_from_unix(errno);
1364 goto fail;
1367 status = cli_ntrename_recv(req);
1369 fail:
1370 TALLOC_FREE(frame);
1371 if (!NT_STATUS_IS_OK(status)) {
1372 cli_set_error(cli, status);
1374 return status;
1377 /****************************************************************************
1378 NT hardlink a file.
1379 ****************************************************************************/
1381 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1382 struct event_context *ev,
1383 struct cli_state *cli,
1384 const char *fname_src,
1385 const char *fname_dst)
1387 return cli_ntrename_internal_send(mem_ctx,
1389 cli,
1390 fname_src,
1391 fname_dst,
1392 RENAME_FLAG_HARD_LINK);
1395 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1397 return cli_ntrename_internal_recv(req);
1400 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1402 TALLOC_CTX *frame = talloc_stackframe();
1403 struct event_context *ev;
1404 struct tevent_req *req;
1405 NTSTATUS status = NT_STATUS_OK;
1407 if (cli_has_async_calls(cli)) {
1409 * Can't use sync call while an async call is in flight
1411 status = NT_STATUS_INVALID_PARAMETER;
1412 goto fail;
1415 ev = event_context_init(frame);
1416 if (ev == NULL) {
1417 status = NT_STATUS_NO_MEMORY;
1418 goto fail;
1421 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1422 if (req == NULL) {
1423 status = NT_STATUS_NO_MEMORY;
1424 goto fail;
1427 if (!tevent_req_poll(req, ev)) {
1428 status = map_nt_error_from_unix(errno);
1429 goto fail;
1432 status = cli_nt_hardlink_recv(req);
1434 fail:
1435 TALLOC_FREE(frame);
1436 if (!NT_STATUS_IS_OK(status)) {
1437 cli_set_error(cli, status);
1439 return status;
1442 /****************************************************************************
1443 Delete a file.
1444 ****************************************************************************/
1446 static void cli_unlink_done(struct tevent_req *subreq);
1448 struct cli_unlink_state {
1449 uint16_t vwv[1];
1452 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1453 struct event_context *ev,
1454 struct cli_state *cli,
1455 const char *fname,
1456 uint16_t mayhave_attrs)
1458 struct tevent_req *req = NULL, *subreq = NULL;
1459 struct cli_unlink_state *state = NULL;
1460 uint8_t additional_flags = 0;
1461 uint8_t *bytes = NULL;
1463 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1464 if (req == NULL) {
1465 return NULL;
1468 SSVAL(state->vwv+0, 0, mayhave_attrs);
1470 bytes = talloc_array(state, uint8_t, 1);
1471 if (tevent_req_nomem(bytes, req)) {
1472 return tevent_req_post(req, ev);
1474 bytes[0] = 4;
1475 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1476 strlen(fname)+1, NULL);
1478 if (tevent_req_nomem(bytes, req)) {
1479 return tevent_req_post(req, ev);
1482 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1483 1, state->vwv, talloc_get_size(bytes), bytes);
1484 if (tevent_req_nomem(subreq, req)) {
1485 return tevent_req_post(req, ev);
1487 tevent_req_set_callback(subreq, cli_unlink_done, req);
1488 return req;
1491 static void cli_unlink_done(struct tevent_req *subreq)
1493 struct tevent_req *req = tevent_req_callback_data(
1494 subreq, struct tevent_req);
1495 NTSTATUS status;
1497 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1498 TALLOC_FREE(subreq);
1499 if (!NT_STATUS_IS_OK(status)) {
1500 tevent_req_nterror(req, status);
1501 return;
1503 tevent_req_done(req);
1506 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1508 return tevent_req_simple_recv_ntstatus(req);
1511 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1513 TALLOC_CTX *frame = talloc_stackframe();
1514 struct event_context *ev;
1515 struct tevent_req *req;
1516 NTSTATUS status = NT_STATUS_OK;
1518 if (cli_has_async_calls(cli)) {
1520 * Can't use sync call while an async call is in flight
1522 status = NT_STATUS_INVALID_PARAMETER;
1523 goto fail;
1526 ev = event_context_init(frame);
1527 if (ev == NULL) {
1528 status = NT_STATUS_NO_MEMORY;
1529 goto fail;
1532 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1533 if (req == NULL) {
1534 status = NT_STATUS_NO_MEMORY;
1535 goto fail;
1538 if (!tevent_req_poll(req, ev)) {
1539 status = map_nt_error_from_unix(errno);
1540 goto fail;
1543 status = cli_unlink_recv(req);
1545 fail:
1546 TALLOC_FREE(frame);
1547 if (!NT_STATUS_IS_OK(status)) {
1548 cli_set_error(cli, status);
1550 return status;
1553 /****************************************************************************
1554 Create a directory.
1555 ****************************************************************************/
1557 static void cli_mkdir_done(struct tevent_req *subreq);
1559 struct cli_mkdir_state {
1560 int dummy;
1563 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1564 struct event_context *ev,
1565 struct cli_state *cli,
1566 const char *dname)
1568 struct tevent_req *req = NULL, *subreq = NULL;
1569 struct cli_mkdir_state *state = NULL;
1570 uint8_t additional_flags = 0;
1571 uint8_t *bytes = NULL;
1573 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1574 if (req == NULL) {
1575 return NULL;
1578 bytes = talloc_array(state, uint8_t, 1);
1579 if (tevent_req_nomem(bytes, req)) {
1580 return tevent_req_post(req, ev);
1582 bytes[0] = 4;
1583 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1584 strlen(dname)+1, NULL);
1586 if (tevent_req_nomem(bytes, req)) {
1587 return tevent_req_post(req, ev);
1590 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1591 0, NULL, talloc_get_size(bytes), bytes);
1592 if (tevent_req_nomem(subreq, req)) {
1593 return tevent_req_post(req, ev);
1595 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1596 return req;
1599 static void cli_mkdir_done(struct tevent_req *subreq)
1601 struct tevent_req *req = tevent_req_callback_data(
1602 subreq, struct tevent_req);
1603 NTSTATUS status;
1605 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1606 TALLOC_FREE(subreq);
1607 if (!NT_STATUS_IS_OK(status)) {
1608 tevent_req_nterror(req, status);
1609 return;
1611 tevent_req_done(req);
1614 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1616 return tevent_req_simple_recv_ntstatus(req);
1619 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1621 TALLOC_CTX *frame = talloc_stackframe();
1622 struct event_context *ev;
1623 struct tevent_req *req;
1624 NTSTATUS status = NT_STATUS_OK;
1626 if (cli_has_async_calls(cli)) {
1628 * Can't use sync call while an async call is in flight
1630 status = NT_STATUS_INVALID_PARAMETER;
1631 goto fail;
1634 ev = event_context_init(frame);
1635 if (ev == NULL) {
1636 status = NT_STATUS_NO_MEMORY;
1637 goto fail;
1640 req = cli_mkdir_send(frame, ev, cli, dname);
1641 if (req == NULL) {
1642 status = NT_STATUS_NO_MEMORY;
1643 goto fail;
1646 if (!tevent_req_poll(req, ev)) {
1647 status = map_nt_error_from_unix(errno);
1648 goto fail;
1651 status = cli_mkdir_recv(req);
1653 fail:
1654 TALLOC_FREE(frame);
1655 if (!NT_STATUS_IS_OK(status)) {
1656 cli_set_error(cli, status);
1658 return status;
1661 /****************************************************************************
1662 Remove a directory.
1663 ****************************************************************************/
1665 static void cli_rmdir_done(struct tevent_req *subreq);
1667 struct cli_rmdir_state {
1668 int dummy;
1671 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1672 struct event_context *ev,
1673 struct cli_state *cli,
1674 const char *dname)
1676 struct tevent_req *req = NULL, *subreq = NULL;
1677 struct cli_rmdir_state *state = NULL;
1678 uint8_t additional_flags = 0;
1679 uint8_t *bytes = NULL;
1681 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1682 if (req == NULL) {
1683 return NULL;
1686 bytes = talloc_array(state, uint8_t, 1);
1687 if (tevent_req_nomem(bytes, req)) {
1688 return tevent_req_post(req, ev);
1690 bytes[0] = 4;
1691 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1692 strlen(dname)+1, NULL);
1694 if (tevent_req_nomem(bytes, req)) {
1695 return tevent_req_post(req, ev);
1698 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1699 0, NULL, talloc_get_size(bytes), bytes);
1700 if (tevent_req_nomem(subreq, req)) {
1701 return tevent_req_post(req, ev);
1703 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1704 return req;
1707 static void cli_rmdir_done(struct tevent_req *subreq)
1709 struct tevent_req *req = tevent_req_callback_data(
1710 subreq, struct tevent_req);
1711 NTSTATUS status;
1713 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1714 TALLOC_FREE(subreq);
1715 if (!NT_STATUS_IS_OK(status)) {
1716 tevent_req_nterror(req, status);
1717 return;
1719 tevent_req_done(req);
1722 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1724 return tevent_req_simple_recv_ntstatus(req);
1727 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1729 TALLOC_CTX *frame = talloc_stackframe();
1730 struct event_context *ev;
1731 struct tevent_req *req;
1732 NTSTATUS status = NT_STATUS_OK;
1734 if (cli_has_async_calls(cli)) {
1736 * Can't use sync call while an async call is in flight
1738 status = NT_STATUS_INVALID_PARAMETER;
1739 goto fail;
1742 ev = event_context_init(frame);
1743 if (ev == NULL) {
1744 status = NT_STATUS_NO_MEMORY;
1745 goto fail;
1748 req = cli_rmdir_send(frame, ev, cli, dname);
1749 if (req == NULL) {
1750 status = NT_STATUS_NO_MEMORY;
1751 goto fail;
1754 if (!tevent_req_poll(req, ev)) {
1755 status = map_nt_error_from_unix(errno);
1756 goto fail;
1759 status = cli_rmdir_recv(req);
1761 fail:
1762 TALLOC_FREE(frame);
1763 if (!NT_STATUS_IS_OK(status)) {
1764 cli_set_error(cli, status);
1766 return status;
1769 /****************************************************************************
1770 Set or clear the delete on close flag.
1771 ****************************************************************************/
1773 struct doc_state {
1774 uint16_t setup;
1775 uint8_t param[6];
1776 uint8_t data[1];
1779 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1781 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1782 NULL, 0, NULL, NULL, 0, NULL);
1783 tevent_req_simple_finish_ntstatus(subreq, status);
1786 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1787 struct event_context *ev,
1788 struct cli_state *cli,
1789 uint16_t fnum,
1790 bool flag)
1792 struct tevent_req *req = NULL, *subreq = NULL;
1793 struct doc_state *state = NULL;
1795 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1796 if (req == NULL) {
1797 return NULL;
1800 /* Setup setup word. */
1801 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1803 /* Setup param array. */
1804 SSVAL(state->param,0,fnum);
1805 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1807 /* Setup data array. */
1808 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1810 subreq = cli_trans_send(state, /* mem ctx. */
1811 ev, /* event ctx. */
1812 cli, /* cli_state. */
1813 SMBtrans2, /* cmd. */
1814 NULL, /* pipe name. */
1815 -1, /* fid. */
1816 0, /* function. */
1817 0, /* flags. */
1818 &state->setup, /* setup. */
1819 1, /* num setup uint16_t words. */
1820 0, /* max returned setup. */
1821 state->param, /* param. */
1822 6, /* num param. */
1823 2, /* max returned param. */
1824 state->data, /* data. */
1825 1, /* num data. */
1826 0); /* max returned data. */
1828 if (tevent_req_nomem(subreq, req)) {
1829 return tevent_req_post(req, ev);
1831 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1832 return req;
1835 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1837 return tevent_req_simple_recv_ntstatus(req);
1840 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1842 TALLOC_CTX *frame = talloc_stackframe();
1843 struct event_context *ev = NULL;
1844 struct tevent_req *req = NULL;
1845 NTSTATUS status = NT_STATUS_OK;
1847 if (cli_has_async_calls(cli)) {
1849 * Can't use sync call while an async call is in flight
1851 status = NT_STATUS_INVALID_PARAMETER;
1852 goto fail;
1855 ev = event_context_init(frame);
1856 if (ev == NULL) {
1857 status = NT_STATUS_NO_MEMORY;
1858 goto fail;
1861 req = cli_nt_delete_on_close_send(frame,
1863 cli,
1864 fnum,
1865 flag);
1866 if (req == NULL) {
1867 status = NT_STATUS_NO_MEMORY;
1868 goto fail;
1871 if (!tevent_req_poll(req, ev)) {
1872 status = map_nt_error_from_unix(errno);
1873 goto fail;
1876 status = cli_nt_delete_on_close_recv(req);
1878 fail:
1879 TALLOC_FREE(frame);
1880 if (!NT_STATUS_IS_OK(status)) {
1881 cli_set_error(cli, status);
1883 return status;
1886 struct cli_ntcreate_state {
1887 uint16_t vwv[24];
1888 uint16_t fnum;
1891 static void cli_ntcreate_done(struct tevent_req *subreq);
1893 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1894 struct event_context *ev,
1895 struct cli_state *cli,
1896 const char *fname,
1897 uint32_t CreatFlags,
1898 uint32_t DesiredAccess,
1899 uint32_t FileAttributes,
1900 uint32_t ShareAccess,
1901 uint32_t CreateDisposition,
1902 uint32_t CreateOptions,
1903 uint8_t SecurityFlags)
1905 struct tevent_req *req, *subreq;
1906 struct cli_ntcreate_state *state;
1907 uint16_t *vwv;
1908 uint8_t *bytes;
1909 size_t converted_len;
1911 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1912 if (req == NULL) {
1913 return NULL;
1916 vwv = state->vwv;
1918 SCVAL(vwv+0, 0, 0xFF);
1919 SCVAL(vwv+0, 1, 0);
1920 SSVAL(vwv+1, 0, 0);
1921 SCVAL(vwv+2, 0, 0);
1923 if (cli->use_oplocks) {
1924 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1926 SIVAL(vwv+3, 1, CreatFlags);
1927 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1928 SIVAL(vwv+7, 1, DesiredAccess);
1929 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1930 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1931 SIVAL(vwv+13, 1, FileAttributes);
1932 SIVAL(vwv+15, 1, ShareAccess);
1933 SIVAL(vwv+17, 1, CreateDisposition);
1934 SIVAL(vwv+19, 1, CreateOptions);
1935 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1936 SCVAL(vwv+23, 1, SecurityFlags);
1938 bytes = talloc_array(state, uint8_t, 0);
1939 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1940 fname, strlen(fname)+1,
1941 &converted_len);
1943 /* sigh. this copes with broken netapp filer behaviour */
1944 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1946 if (tevent_req_nomem(bytes, req)) {
1947 return tevent_req_post(req, ev);
1950 SSVAL(vwv+2, 1, converted_len);
1952 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1953 talloc_get_size(bytes), bytes);
1954 if (tevent_req_nomem(subreq, req)) {
1955 return tevent_req_post(req, ev);
1957 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1958 return req;
1961 static void cli_ntcreate_done(struct tevent_req *subreq)
1963 struct tevent_req *req = tevent_req_callback_data(
1964 subreq, struct tevent_req);
1965 struct cli_ntcreate_state *state = tevent_req_data(
1966 req, struct cli_ntcreate_state);
1967 uint8_t wct;
1968 uint16_t *vwv;
1969 uint32_t num_bytes;
1970 uint8_t *bytes;
1971 uint8_t *inbuf;
1972 NTSTATUS status;
1974 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1975 &num_bytes, &bytes);
1976 TALLOC_FREE(subreq);
1977 if (!NT_STATUS_IS_OK(status)) {
1978 tevent_req_nterror(req, status);
1979 return;
1981 state->fnum = SVAL(vwv+2, 1);
1982 tevent_req_done(req);
1985 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1987 struct cli_ntcreate_state *state = tevent_req_data(
1988 req, struct cli_ntcreate_state);
1989 NTSTATUS status;
1991 if (tevent_req_is_nterror(req, &status)) {
1992 return status;
1994 *pfnum = state->fnum;
1995 return NT_STATUS_OK;
1998 NTSTATUS cli_ntcreate(struct cli_state *cli,
1999 const char *fname,
2000 uint32_t CreatFlags,
2001 uint32_t DesiredAccess,
2002 uint32_t FileAttributes,
2003 uint32_t ShareAccess,
2004 uint32_t CreateDisposition,
2005 uint32_t CreateOptions,
2006 uint8_t SecurityFlags,
2007 uint16_t *pfid)
2009 TALLOC_CTX *frame = talloc_stackframe();
2010 struct event_context *ev;
2011 struct tevent_req *req;
2012 NTSTATUS status = NT_STATUS_OK;
2014 if (cli_has_async_calls(cli)) {
2016 * Can't use sync call while an async call is in flight
2018 status = NT_STATUS_INVALID_PARAMETER;
2019 goto fail;
2022 ev = event_context_init(frame);
2023 if (ev == NULL) {
2024 status = NT_STATUS_NO_MEMORY;
2025 goto fail;
2028 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2029 DesiredAccess, FileAttributes, ShareAccess,
2030 CreateDisposition, CreateOptions,
2031 SecurityFlags);
2032 if (req == NULL) {
2033 status = NT_STATUS_NO_MEMORY;
2034 goto fail;
2037 if (!tevent_req_poll(req, ev)) {
2038 status = map_nt_error_from_unix(errno);
2039 goto fail;
2042 status = cli_ntcreate_recv(req, pfid);
2043 fail:
2044 TALLOC_FREE(frame);
2045 if (!NT_STATUS_IS_OK(status)) {
2046 cli_set_error(cli, status);
2048 return status;
2051 /****************************************************************************
2052 Open a file
2053 WARNING: if you open with O_WRONLY then getattrE won't work!
2054 ****************************************************************************/
2056 struct cli_open_state {
2057 uint16_t vwv[15];
2058 uint16_t fnum;
2059 struct iovec bytes;
2062 static void cli_open_done(struct tevent_req *subreq);
2064 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2065 struct event_context *ev,
2066 struct cli_state *cli, const char *fname,
2067 int flags, int share_mode,
2068 struct tevent_req **psmbreq)
2070 struct tevent_req *req, *subreq;
2071 struct cli_open_state *state;
2072 unsigned openfn;
2073 unsigned accessmode;
2074 uint8_t additional_flags;
2075 uint8_t *bytes;
2077 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2078 if (req == NULL) {
2079 return NULL;
2082 openfn = 0;
2083 if (flags & O_CREAT) {
2084 openfn |= (1<<4);
2086 if (!(flags & O_EXCL)) {
2087 if (flags & O_TRUNC)
2088 openfn |= (1<<1);
2089 else
2090 openfn |= (1<<0);
2093 accessmode = (share_mode<<4);
2095 if ((flags & O_ACCMODE) == O_RDWR) {
2096 accessmode |= 2;
2097 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2098 accessmode |= 1;
2101 #if defined(O_SYNC)
2102 if ((flags & O_SYNC) == O_SYNC) {
2103 accessmode |= (1<<14);
2105 #endif /* O_SYNC */
2107 if (share_mode == DENY_FCB) {
2108 accessmode = 0xFF;
2111 SCVAL(state->vwv + 0, 0, 0xFF);
2112 SCVAL(state->vwv + 0, 1, 0);
2113 SSVAL(state->vwv + 1, 0, 0);
2114 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2115 SSVAL(state->vwv + 3, 0, accessmode);
2116 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2117 SSVAL(state->vwv + 5, 0, 0);
2118 SIVAL(state->vwv + 6, 0, 0);
2119 SSVAL(state->vwv + 8, 0, openfn);
2120 SIVAL(state->vwv + 9, 0, 0);
2121 SIVAL(state->vwv + 11, 0, 0);
2122 SIVAL(state->vwv + 13, 0, 0);
2124 additional_flags = 0;
2126 if (cli->use_oplocks) {
2127 /* if using oplocks then ask for a batch oplock via
2128 core and extended methods */
2129 additional_flags =
2130 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2131 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2134 bytes = talloc_array(state, uint8_t, 0);
2135 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2136 strlen(fname)+1, NULL);
2138 if (tevent_req_nomem(bytes, req)) {
2139 return tevent_req_post(req, ev);
2142 state->bytes.iov_base = (void *)bytes;
2143 state->bytes.iov_len = talloc_get_size(bytes);
2145 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2146 15, state->vwv, 1, &state->bytes);
2147 if (subreq == NULL) {
2148 TALLOC_FREE(req);
2149 return NULL;
2151 tevent_req_set_callback(subreq, cli_open_done, req);
2152 *psmbreq = subreq;
2153 return req;
2156 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2157 struct cli_state *cli, const char *fname,
2158 int flags, int share_mode)
2160 struct tevent_req *req, *subreq;
2161 NTSTATUS status;
2163 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2164 &subreq);
2165 if (req == NULL) {
2166 return NULL;
2169 status = cli_smb_req_send(subreq);
2170 if (!NT_STATUS_IS_OK(status)) {
2171 tevent_req_nterror(req, status);
2172 return tevent_req_post(req, ev);
2174 return req;
2177 static void cli_open_done(struct tevent_req *subreq)
2179 struct tevent_req *req = tevent_req_callback_data(
2180 subreq, struct tevent_req);
2181 struct cli_open_state *state = tevent_req_data(
2182 req, struct cli_open_state);
2183 uint8_t wct;
2184 uint16_t *vwv;
2185 uint8_t *inbuf;
2186 NTSTATUS status;
2188 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2189 NULL);
2190 TALLOC_FREE(subreq);
2191 if (!NT_STATUS_IS_OK(status)) {
2192 tevent_req_nterror(req, status);
2193 return;
2195 state->fnum = SVAL(vwv+2, 0);
2196 tevent_req_done(req);
2199 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2201 struct cli_open_state *state = tevent_req_data(
2202 req, struct cli_open_state);
2203 NTSTATUS status;
2205 if (tevent_req_is_nterror(req, &status)) {
2206 return status;
2208 *pfnum = state->fnum;
2209 return NT_STATUS_OK;
2212 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2213 int share_mode, uint16_t *pfnum)
2215 TALLOC_CTX *frame = talloc_stackframe();
2216 struct event_context *ev;
2217 struct tevent_req *req;
2218 NTSTATUS status = NT_STATUS_OK;
2220 if (cli_has_async_calls(cli)) {
2222 * Can't use sync call while an async call is in flight
2224 status = NT_STATUS_INVALID_PARAMETER;
2225 goto fail;
2228 ev = event_context_init(frame);
2229 if (ev == NULL) {
2230 status = NT_STATUS_NO_MEMORY;
2231 goto fail;
2234 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2235 if (req == NULL) {
2236 status = NT_STATUS_NO_MEMORY;
2237 goto fail;
2240 if (!tevent_req_poll(req, ev)) {
2241 status = map_nt_error_from_unix(errno);
2242 goto fail;
2245 status = cli_open_recv(req, pfnum);
2246 fail:
2247 TALLOC_FREE(frame);
2248 if (!NT_STATUS_IS_OK(status)) {
2249 cli_set_error(cli, status);
2251 return status;
2254 /****************************************************************************
2255 Close a file.
2256 ****************************************************************************/
2258 struct cli_close_state {
2259 uint16_t vwv[3];
2262 static void cli_close_done(struct tevent_req *subreq);
2264 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2265 struct event_context *ev,
2266 struct cli_state *cli,
2267 uint16_t fnum,
2268 struct tevent_req **psubreq)
2270 struct tevent_req *req, *subreq;
2271 struct cli_close_state *state;
2273 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2274 if (req == NULL) {
2275 return NULL;
2278 SSVAL(state->vwv+0, 0, fnum);
2279 SIVALS(state->vwv+1, 0, -1);
2281 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2282 0, NULL);
2283 if (subreq == NULL) {
2284 TALLOC_FREE(req);
2285 return NULL;
2287 tevent_req_set_callback(subreq, cli_close_done, req);
2288 *psubreq = subreq;
2289 return req;
2292 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2293 struct event_context *ev,
2294 struct cli_state *cli,
2295 uint16_t fnum)
2297 struct tevent_req *req, *subreq;
2298 NTSTATUS status;
2300 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2301 if (req == NULL) {
2302 return NULL;
2305 status = cli_smb_req_send(subreq);
2306 if (!NT_STATUS_IS_OK(status)) {
2307 tevent_req_nterror(req, status);
2308 return tevent_req_post(req, ev);
2310 return req;
2313 static void cli_close_done(struct tevent_req *subreq)
2315 struct tevent_req *req = tevent_req_callback_data(
2316 subreq, struct tevent_req);
2317 NTSTATUS status;
2319 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2320 TALLOC_FREE(subreq);
2321 if (!NT_STATUS_IS_OK(status)) {
2322 tevent_req_nterror(req, status);
2323 return;
2325 tevent_req_done(req);
2328 NTSTATUS cli_close_recv(struct tevent_req *req)
2330 return tevent_req_simple_recv_ntstatus(req);
2333 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2335 TALLOC_CTX *frame = talloc_stackframe();
2336 struct event_context *ev;
2337 struct tevent_req *req;
2338 NTSTATUS status = NT_STATUS_OK;
2340 if (cli_has_async_calls(cli)) {
2342 * Can't use sync call while an async call is in flight
2344 status = NT_STATUS_INVALID_PARAMETER;
2345 goto fail;
2348 ev = event_context_init(frame);
2349 if (ev == NULL) {
2350 status = NT_STATUS_NO_MEMORY;
2351 goto fail;
2354 req = cli_close_send(frame, ev, cli, fnum);
2355 if (req == NULL) {
2356 status = NT_STATUS_NO_MEMORY;
2357 goto fail;
2360 if (!tevent_req_poll(req, ev)) {
2361 status = map_nt_error_from_unix(errno);
2362 goto fail;
2365 status = cli_close_recv(req);
2366 fail:
2367 TALLOC_FREE(frame);
2368 if (!NT_STATUS_IS_OK(status)) {
2369 cli_set_error(cli, status);
2371 return status;
2374 /****************************************************************************
2375 Truncate a file to a specified size
2376 ****************************************************************************/
2378 struct ftrunc_state {
2379 uint16_t setup;
2380 uint8_t param[6];
2381 uint8_t data[8];
2384 static void cli_ftruncate_done(struct tevent_req *subreq)
2386 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2387 NULL, 0, NULL, NULL, 0, NULL);
2388 tevent_req_simple_finish_ntstatus(subreq, status);
2391 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2392 struct event_context *ev,
2393 struct cli_state *cli,
2394 uint16_t fnum,
2395 uint64_t size)
2397 struct tevent_req *req = NULL, *subreq = NULL;
2398 struct ftrunc_state *state = NULL;
2400 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2401 if (req == NULL) {
2402 return NULL;
2405 /* Setup setup word. */
2406 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2408 /* Setup param array. */
2409 SSVAL(state->param,0,fnum);
2410 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2411 SSVAL(state->param,4,0);
2413 /* Setup data array. */
2414 SBVAL(state->data, 0, size);
2416 subreq = cli_trans_send(state, /* mem ctx. */
2417 ev, /* event ctx. */
2418 cli, /* cli_state. */
2419 SMBtrans2, /* cmd. */
2420 NULL, /* pipe name. */
2421 -1, /* fid. */
2422 0, /* function. */
2423 0, /* flags. */
2424 &state->setup, /* setup. */
2425 1, /* num setup uint16_t words. */
2426 0, /* max returned setup. */
2427 state->param, /* param. */
2428 6, /* num param. */
2429 2, /* max returned param. */
2430 state->data, /* data. */
2431 8, /* num data. */
2432 0); /* max returned data. */
2434 if (tevent_req_nomem(subreq, req)) {
2435 return tevent_req_post(req, ev);
2437 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2438 return req;
2441 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2443 return tevent_req_simple_recv_ntstatus(req);
2446 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2448 TALLOC_CTX *frame = talloc_stackframe();
2449 struct event_context *ev = NULL;
2450 struct tevent_req *req = NULL;
2451 NTSTATUS status = NT_STATUS_OK;
2453 if (cli_has_async_calls(cli)) {
2455 * Can't use sync call while an async call is in flight
2457 status = NT_STATUS_INVALID_PARAMETER;
2458 goto fail;
2461 ev = event_context_init(frame);
2462 if (ev == NULL) {
2463 status = NT_STATUS_NO_MEMORY;
2464 goto fail;
2467 req = cli_ftruncate_send(frame,
2469 cli,
2470 fnum,
2471 size);
2472 if (req == NULL) {
2473 status = NT_STATUS_NO_MEMORY;
2474 goto fail;
2477 if (!tevent_req_poll(req, ev)) {
2478 status = map_nt_error_from_unix(errno);
2479 goto fail;
2482 status = cli_ftruncate_recv(req);
2484 fail:
2485 TALLOC_FREE(frame);
2486 if (!NT_STATUS_IS_OK(status)) {
2487 cli_set_error(cli, status);
2489 return status;
2492 /****************************************************************************
2493 send a lock with a specified locktype
2494 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2495 ****************************************************************************/
2497 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2498 uint32_t offset, uint32_t len,
2499 int timeout, unsigned char locktype)
2501 uint16_t vwv[8];
2502 uint8_t bytes[10];
2503 NTSTATUS status;
2504 int saved_timeout;
2506 SCVAL(vwv + 0, 0, 0xff);
2507 SCVAL(vwv + 0, 1, 0);
2508 SSVAL(vwv + 1, 0, 0);
2509 SSVAL(vwv + 2, 0, fnum);
2510 SCVAL(vwv + 3, 0, locktype);
2511 SCVAL(vwv + 3, 1, 0);
2512 SIVALS(vwv + 4, 0, timeout);
2513 SSVAL(vwv + 6, 0, 0);
2514 SSVAL(vwv + 7, 0, 1);
2516 SSVAL(bytes, 0, cli->pid);
2517 SIVAL(bytes, 2, offset);
2518 SIVAL(bytes, 6, len);
2520 saved_timeout = cli->timeout;
2522 if (timeout != 0) {
2523 cli->timeout = (timeout == -1)
2524 ? 0x7FFFFFFF : (timeout + 2*1000);
2527 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2528 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2530 cli->timeout = saved_timeout;
2532 return status;
2535 /****************************************************************************
2536 Lock a file.
2537 note that timeout is in units of 2 milliseconds
2538 ****************************************************************************/
2540 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2541 uint32_t offset, uint32_t len, int timeout,
2542 enum brl_type lock_type)
2544 NTSTATUS status;
2546 status = cli_locktype(cli, fnum, offset, len, timeout,
2547 (lock_type == READ_LOCK? 1 : 0));
2548 cli_set_error(cli, status);
2549 return NT_STATUS_IS_OK(status);
2552 /****************************************************************************
2553 Unlock a file.
2554 ****************************************************************************/
2556 struct cli_unlock_state {
2557 uint16_t vwv[8];
2558 uint8_t data[10];
2561 static void cli_unlock_done(struct tevent_req *subreq);
2563 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2564 struct event_context *ev,
2565 struct cli_state *cli,
2566 uint16_t fnum,
2567 uint64_t offset,
2568 uint64_t len)
2571 struct tevent_req *req = NULL, *subreq = NULL;
2572 struct cli_unlock_state *state = NULL;
2573 uint8_t additional_flags = 0;
2575 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2576 if (req == NULL) {
2577 return NULL;
2580 SCVAL(state->vwv+0, 0, 0xFF);
2581 SSVAL(state->vwv+2, 0, fnum);
2582 SCVAL(state->vwv+3, 0, 0);
2583 SIVALS(state->vwv+4, 0, 0);
2584 SSVAL(state->vwv+6, 0, 1);
2585 SSVAL(state->vwv+7, 0, 0);
2587 SSVAL(state->data, 0, cli->pid);
2588 SIVAL(state->data, 2, offset);
2589 SIVAL(state->data, 6, len);
2591 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2592 8, state->vwv, 10, state->data);
2593 if (tevent_req_nomem(subreq, req)) {
2594 return tevent_req_post(req, ev);
2596 tevent_req_set_callback(subreq, cli_unlock_done, req);
2597 return req;
2600 static void cli_unlock_done(struct tevent_req *subreq)
2602 struct tevent_req *req = tevent_req_callback_data(
2603 subreq, struct tevent_req);
2604 NTSTATUS status;
2606 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2607 TALLOC_FREE(subreq);
2608 if (!NT_STATUS_IS_OK(status)) {
2609 tevent_req_nterror(req, status);
2610 return;
2612 tevent_req_done(req);
2615 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2617 return tevent_req_simple_recv_ntstatus(req);
2620 NTSTATUS cli_unlock(struct cli_state *cli,
2621 uint16_t fnum,
2622 uint32_t offset,
2623 uint32_t len)
2625 TALLOC_CTX *frame = talloc_stackframe();
2626 struct event_context *ev;
2627 struct tevent_req *req;
2628 NTSTATUS status = NT_STATUS_OK;
2630 if (cli_has_async_calls(cli)) {
2632 * Can't use sync call while an async call is in flight
2634 status = NT_STATUS_INVALID_PARAMETER;
2635 goto fail;
2638 ev = event_context_init(frame);
2639 if (ev == NULL) {
2640 status = NT_STATUS_NO_MEMORY;
2641 goto fail;
2644 req = cli_unlock_send(frame, ev, cli,
2645 fnum, offset, len);
2646 if (req == NULL) {
2647 status = NT_STATUS_NO_MEMORY;
2648 goto fail;
2651 if (!tevent_req_poll(req, ev)) {
2652 status = map_nt_error_from_unix(errno);
2653 goto fail;
2656 status = cli_unlock_recv(req);
2658 fail:
2659 TALLOC_FREE(frame);
2660 if (!NT_STATUS_IS_OK(status)) {
2661 cli_set_error(cli, status);
2663 return status;
2666 /****************************************************************************
2667 Lock a file with 64 bit offsets.
2668 ****************************************************************************/
2670 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2671 uint64_t offset, uint64_t len, int timeout,
2672 enum brl_type lock_type)
2674 uint16_t vwv[8];
2675 uint8_t bytes[20];
2676 int saved_timeout = cli->timeout;
2677 int ltype;
2678 NTSTATUS status;
2680 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2681 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2684 ltype = (lock_type == READ_LOCK? 1 : 0);
2685 ltype |= LOCKING_ANDX_LARGE_FILES;
2687 SCVAL(vwv + 0, 0, 0xff);
2688 SCVAL(vwv + 0, 1, 0);
2689 SSVAL(vwv + 1, 0, 0);
2690 SSVAL(vwv + 2, 0, fnum);
2691 SCVAL(vwv + 3, 0, ltype);
2692 SCVAL(vwv + 3, 1, 0);
2693 SIVALS(vwv + 4, 0, timeout);
2694 SSVAL(vwv + 6, 0, 0);
2695 SSVAL(vwv + 7, 0, 1);
2697 SIVAL(bytes, 0, cli->pid);
2698 SOFF_T_R(bytes, 4, offset);
2699 SOFF_T_R(bytes, 12, len);
2701 saved_timeout = cli->timeout;
2703 if (timeout != 0) {
2704 cli->timeout = (timeout == -1)
2705 ? 0x7FFFFFFF : (timeout + 2*1000);
2708 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2709 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2711 cli->timeout = saved_timeout;
2713 cli_set_error(cli, status);
2714 return NT_STATUS_IS_OK(status);
2717 /****************************************************************************
2718 Unlock a file with 64 bit offsets.
2719 ****************************************************************************/
2721 struct cli_unlock64_state {
2722 uint16_t vwv[8];
2723 uint8_t data[20];
2726 static void cli_unlock64_done(struct tevent_req *subreq);
2728 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2729 struct event_context *ev,
2730 struct cli_state *cli,
2731 uint16_t fnum,
2732 uint64_t offset,
2733 uint64_t len)
2736 struct tevent_req *req = NULL, *subreq = NULL;
2737 struct cli_unlock64_state *state = NULL;
2738 uint8_t additional_flags = 0;
2740 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2741 if (req == NULL) {
2742 return NULL;
2745 SCVAL(state->vwv+0, 0, 0xff);
2746 SSVAL(state->vwv+2, 0, fnum);
2747 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2748 SIVALS(state->vwv+4, 0, 0);
2749 SSVAL(state->vwv+6, 0, 1);
2750 SSVAL(state->vwv+7, 0, 0);
2752 SIVAL(state->data, 0, cli->pid);
2753 SOFF_T_R(state->data, 4, offset);
2754 SOFF_T_R(state->data, 12, len);
2756 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2757 8, state->vwv, 20, state->data);
2758 if (tevent_req_nomem(subreq, req)) {
2759 return tevent_req_post(req, ev);
2761 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2762 return req;
2765 static void cli_unlock64_done(struct tevent_req *subreq)
2767 struct tevent_req *req = tevent_req_callback_data(
2768 subreq, struct tevent_req);
2769 NTSTATUS status;
2771 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2772 TALLOC_FREE(subreq);
2773 if (!NT_STATUS_IS_OK(status)) {
2774 tevent_req_nterror(req, status);
2775 return;
2777 tevent_req_done(req);
2780 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2782 return tevent_req_simple_recv_ntstatus(req);
2785 NTSTATUS cli_unlock64(struct cli_state *cli,
2786 uint16_t fnum,
2787 uint64_t offset,
2788 uint64_t len)
2790 TALLOC_CTX *frame = talloc_stackframe();
2791 struct event_context *ev;
2792 struct tevent_req *req;
2793 NTSTATUS status = NT_STATUS_OK;
2795 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2796 return cli_unlock(cli, fnum, offset, len);
2799 if (cli_has_async_calls(cli)) {
2801 * Can't use sync call while an async call is in flight
2803 status = NT_STATUS_INVALID_PARAMETER;
2804 goto fail;
2807 ev = event_context_init(frame);
2808 if (ev == NULL) {
2809 status = NT_STATUS_NO_MEMORY;
2810 goto fail;
2813 req = cli_unlock64_send(frame, ev, cli,
2814 fnum, offset, len);
2815 if (req == NULL) {
2816 status = NT_STATUS_NO_MEMORY;
2817 goto fail;
2820 if (!tevent_req_poll(req, ev)) {
2821 status = map_nt_error_from_unix(errno);
2822 goto fail;
2825 status = cli_unlock64_recv(req);
2827 fail:
2828 TALLOC_FREE(frame);
2829 if (!NT_STATUS_IS_OK(status)) {
2830 cli_set_error(cli, status);
2832 return status;
2835 /****************************************************************************
2836 Get/unlock a POSIX lock on a file - internal function.
2837 ****************************************************************************/
2839 struct posix_lock_state {
2840 uint16_t setup;
2841 uint8_t param[4];
2842 uint8_t data[POSIX_LOCK_DATA_SIZE];
2845 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2847 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2848 NULL, 0, NULL, NULL, 0, NULL);
2849 tevent_req_simple_finish_ntstatus(subreq, status);
2852 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2853 struct event_context *ev,
2854 struct cli_state *cli,
2855 uint16_t fnum,
2856 uint64_t offset,
2857 uint64_t len,
2858 bool wait_lock,
2859 enum brl_type lock_type)
2861 struct tevent_req *req = NULL, *subreq = NULL;
2862 struct posix_lock_state *state = NULL;
2864 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2865 if (req == NULL) {
2866 return NULL;
2869 /* Setup setup word. */
2870 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2872 /* Setup param array. */
2873 SSVAL(&state->param, 0, fnum);
2874 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2876 /* Setup data array. */
2877 switch (lock_type) {
2878 case READ_LOCK:
2879 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2880 POSIX_LOCK_TYPE_READ);
2881 break;
2882 case WRITE_LOCK:
2883 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2884 POSIX_LOCK_TYPE_WRITE);
2885 break;
2886 case UNLOCK_LOCK:
2887 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2888 POSIX_LOCK_TYPE_UNLOCK);
2889 break;
2890 default:
2891 return NULL;
2894 if (wait_lock) {
2895 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2896 POSIX_LOCK_FLAG_WAIT);
2897 } else {
2898 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2899 POSIX_LOCK_FLAG_NOWAIT);
2902 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2903 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2904 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2906 subreq = cli_trans_send(state, /* mem ctx. */
2907 ev, /* event ctx. */
2908 cli, /* cli_state. */
2909 SMBtrans2, /* cmd. */
2910 NULL, /* pipe name. */
2911 -1, /* fid. */
2912 0, /* function. */
2913 0, /* flags. */
2914 &state->setup, /* setup. */
2915 1, /* num setup uint16_t words. */
2916 0, /* max returned setup. */
2917 state->param, /* param. */
2918 4, /* num param. */
2919 2, /* max returned param. */
2920 state->data, /* data. */
2921 POSIX_LOCK_DATA_SIZE, /* num data. */
2922 0); /* max returned data. */
2924 if (tevent_req_nomem(subreq, req)) {
2925 return tevent_req_post(req, ev);
2927 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2928 return req;
2931 /****************************************************************************
2932 POSIX Lock a file.
2933 ****************************************************************************/
2935 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2936 struct event_context *ev,
2937 struct cli_state *cli,
2938 uint16_t fnum,
2939 uint64_t offset,
2940 uint64_t len,
2941 bool wait_lock,
2942 enum brl_type lock_type)
2944 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2945 wait_lock, lock_type);
2948 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2950 return tevent_req_simple_recv_ntstatus(req);
2953 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2954 uint64_t offset, uint64_t len,
2955 bool wait_lock, enum brl_type lock_type)
2957 TALLOC_CTX *frame = talloc_stackframe();
2958 struct event_context *ev = NULL;
2959 struct tevent_req *req = NULL;
2960 NTSTATUS status = NT_STATUS_OK;
2962 if (cli_has_async_calls(cli)) {
2964 * Can't use sync call while an async call is in flight
2966 status = NT_STATUS_INVALID_PARAMETER;
2967 goto fail;
2970 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2971 status = NT_STATUS_INVALID_PARAMETER;
2972 goto fail;
2975 ev = event_context_init(frame);
2976 if (ev == NULL) {
2977 status = NT_STATUS_NO_MEMORY;
2978 goto fail;
2981 req = cli_posix_lock_send(frame,
2983 cli,
2984 fnum,
2985 offset,
2986 len,
2987 wait_lock,
2988 lock_type);
2989 if (req == NULL) {
2990 status = NT_STATUS_NO_MEMORY;
2991 goto fail;
2994 if (!tevent_req_poll(req, ev)) {
2995 status = map_nt_error_from_unix(errno);
2996 goto fail;
2999 status = cli_posix_lock_recv(req);
3001 fail:
3002 TALLOC_FREE(frame);
3003 if (!NT_STATUS_IS_OK(status)) {
3004 cli_set_error(cli, status);
3006 return status;
3009 /****************************************************************************
3010 POSIX Unlock a file.
3011 ****************************************************************************/
3013 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3014 struct event_context *ev,
3015 struct cli_state *cli,
3016 uint16_t fnum,
3017 uint64_t offset,
3018 uint64_t len)
3020 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3021 false, UNLOCK_LOCK);
3024 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3026 return tevent_req_simple_recv_ntstatus(req);
3029 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3031 TALLOC_CTX *frame = talloc_stackframe();
3032 struct event_context *ev = NULL;
3033 struct tevent_req *req = NULL;
3034 NTSTATUS status = NT_STATUS_OK;
3036 if (cli_has_async_calls(cli)) {
3038 * Can't use sync call while an async call is in flight
3040 status = NT_STATUS_INVALID_PARAMETER;
3041 goto fail;
3044 ev = event_context_init(frame);
3045 if (ev == NULL) {
3046 status = NT_STATUS_NO_MEMORY;
3047 goto fail;
3050 req = cli_posix_unlock_send(frame,
3052 cli,
3053 fnum,
3054 offset,
3055 len);
3056 if (req == NULL) {
3057 status = NT_STATUS_NO_MEMORY;
3058 goto fail;
3061 if (!tevent_req_poll(req, ev)) {
3062 status = map_nt_error_from_unix(errno);
3063 goto fail;
3066 status = cli_posix_unlock_recv(req);
3068 fail:
3069 TALLOC_FREE(frame);
3070 if (!NT_STATUS_IS_OK(status)) {
3071 cli_set_error(cli, status);
3073 return status;
3076 /****************************************************************************
3077 Do a SMBgetattrE call.
3078 ****************************************************************************/
3080 static void cli_getattrE_done(struct tevent_req *subreq);
3082 struct cli_getattrE_state {
3083 uint16_t vwv[1];
3084 int zone_offset;
3085 uint16_t attr;
3086 SMB_OFF_T size;
3087 time_t change_time;
3088 time_t access_time;
3089 time_t write_time;
3092 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3093 struct event_context *ev,
3094 struct cli_state *cli,
3095 uint16_t fnum)
3097 struct tevent_req *req = NULL, *subreq = NULL;
3098 struct cli_getattrE_state *state = NULL;
3099 uint8_t additional_flags = 0;
3101 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3102 if (req == NULL) {
3103 return NULL;
3106 state->zone_offset = cli->serverzone;
3107 SSVAL(state->vwv+0,0,fnum);
3109 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3110 1, state->vwv, 0, NULL);
3111 if (tevent_req_nomem(subreq, req)) {
3112 return tevent_req_post(req, ev);
3114 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3115 return req;
3118 static void cli_getattrE_done(struct tevent_req *subreq)
3120 struct tevent_req *req = tevent_req_callback_data(
3121 subreq, struct tevent_req);
3122 struct cli_getattrE_state *state = tevent_req_data(
3123 req, struct cli_getattrE_state);
3124 uint8_t wct;
3125 uint16_t *vwv = NULL;
3126 uint8_t *inbuf;
3127 NTSTATUS status;
3129 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3130 NULL, NULL);
3131 TALLOC_FREE(subreq);
3132 if (!NT_STATUS_IS_OK(status)) {
3133 tevent_req_nterror(req, status);
3134 return;
3137 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3138 state->attr = SVAL(vwv+10,0);
3139 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3140 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3141 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3143 tevent_req_done(req);
3146 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3147 uint16_t *attr,
3148 SMB_OFF_T *size,
3149 time_t *change_time,
3150 time_t *access_time,
3151 time_t *write_time)
3153 struct cli_getattrE_state *state = tevent_req_data(
3154 req, struct cli_getattrE_state);
3155 NTSTATUS status;
3157 if (tevent_req_is_nterror(req, &status)) {
3158 return status;
3160 if (attr) {
3161 *attr = state->attr;
3163 if (size) {
3164 *size = state->size;
3166 if (change_time) {
3167 *change_time = state->change_time;
3169 if (access_time) {
3170 *access_time = state->access_time;
3172 if (write_time) {
3173 *write_time = state->write_time;
3175 return NT_STATUS_OK;
3178 NTSTATUS cli_getattrE(struct cli_state *cli,
3179 uint16_t fnum,
3180 uint16_t *attr,
3181 SMB_OFF_T *size,
3182 time_t *change_time,
3183 time_t *access_time,
3184 time_t *write_time)
3186 TALLOC_CTX *frame = talloc_stackframe();
3187 struct event_context *ev = NULL;
3188 struct tevent_req *req = NULL;
3189 NTSTATUS status = NT_STATUS_OK;
3191 if (cli_has_async_calls(cli)) {
3193 * Can't use sync call while an async call is in flight
3195 status = NT_STATUS_INVALID_PARAMETER;
3196 goto fail;
3199 ev = event_context_init(frame);
3200 if (ev == NULL) {
3201 status = NT_STATUS_NO_MEMORY;
3202 goto fail;
3205 req = cli_getattrE_send(frame, ev, cli, fnum);
3206 if (req == NULL) {
3207 status = NT_STATUS_NO_MEMORY;
3208 goto fail;
3211 if (!tevent_req_poll(req, ev)) {
3212 status = map_nt_error_from_unix(errno);
3213 goto fail;
3216 status = cli_getattrE_recv(req,
3217 attr,
3218 size,
3219 change_time,
3220 access_time,
3221 write_time);
3223 fail:
3224 TALLOC_FREE(frame);
3225 if (!NT_STATUS_IS_OK(status)) {
3226 cli_set_error(cli, status);
3228 return status;
3231 /****************************************************************************
3232 Do a SMBgetatr call
3233 ****************************************************************************/
3235 static void cli_getatr_done(struct tevent_req *subreq);
3237 struct cli_getatr_state {
3238 int zone_offset;
3239 uint16_t attr;
3240 SMB_OFF_T size;
3241 time_t write_time;
3244 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3245 struct event_context *ev,
3246 struct cli_state *cli,
3247 const char *fname)
3249 struct tevent_req *req = NULL, *subreq = NULL;
3250 struct cli_getatr_state *state = NULL;
3251 uint8_t additional_flags = 0;
3252 uint8_t *bytes = NULL;
3254 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3255 if (req == NULL) {
3256 return NULL;
3259 state->zone_offset = cli->serverzone;
3261 bytes = talloc_array(state, uint8_t, 1);
3262 if (tevent_req_nomem(bytes, req)) {
3263 return tevent_req_post(req, ev);
3265 bytes[0] = 4;
3266 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3267 strlen(fname)+1, NULL);
3269 if (tevent_req_nomem(bytes, req)) {
3270 return tevent_req_post(req, ev);
3273 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3274 0, NULL, talloc_get_size(bytes), bytes);
3275 if (tevent_req_nomem(subreq, req)) {
3276 return tevent_req_post(req, ev);
3278 tevent_req_set_callback(subreq, cli_getatr_done, req);
3279 return req;
3282 static void cli_getatr_done(struct tevent_req *subreq)
3284 struct tevent_req *req = tevent_req_callback_data(
3285 subreq, struct tevent_req);
3286 struct cli_getatr_state *state = tevent_req_data(
3287 req, struct cli_getatr_state);
3288 uint8_t wct;
3289 uint16_t *vwv = NULL;
3290 uint8_t *inbuf;
3291 NTSTATUS status;
3293 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3294 NULL);
3295 TALLOC_FREE(subreq);
3296 if (!NT_STATUS_IS_OK(status)) {
3297 tevent_req_nterror(req, status);
3298 return;
3301 state->attr = SVAL(vwv+0,0);
3302 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3303 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3305 tevent_req_done(req);
3308 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3309 uint16_t *attr,
3310 SMB_OFF_T *size,
3311 time_t *write_time)
3313 struct cli_getatr_state *state = tevent_req_data(
3314 req, struct cli_getatr_state);
3315 NTSTATUS status;
3317 if (tevent_req_is_nterror(req, &status)) {
3318 return status;
3320 if (attr) {
3321 *attr = state->attr;
3323 if (size) {
3324 *size = state->size;
3326 if (write_time) {
3327 *write_time = state->write_time;
3329 return NT_STATUS_OK;
3332 NTSTATUS cli_getatr(struct cli_state *cli,
3333 const char *fname,
3334 uint16_t *attr,
3335 SMB_OFF_T *size,
3336 time_t *write_time)
3338 TALLOC_CTX *frame = talloc_stackframe();
3339 struct event_context *ev = NULL;
3340 struct tevent_req *req = NULL;
3341 NTSTATUS status = NT_STATUS_OK;
3343 if (cli_has_async_calls(cli)) {
3345 * Can't use sync call while an async call is in flight
3347 status = NT_STATUS_INVALID_PARAMETER;
3348 goto fail;
3351 ev = event_context_init(frame);
3352 if (ev == NULL) {
3353 status = NT_STATUS_NO_MEMORY;
3354 goto fail;
3357 req = cli_getatr_send(frame, ev, cli, fname);
3358 if (req == NULL) {
3359 status = NT_STATUS_NO_MEMORY;
3360 goto fail;
3363 if (!tevent_req_poll(req, ev)) {
3364 status = map_nt_error_from_unix(errno);
3365 goto fail;
3368 status = cli_getatr_recv(req,
3369 attr,
3370 size,
3371 write_time);
3373 fail:
3374 TALLOC_FREE(frame);
3375 if (!NT_STATUS_IS_OK(status)) {
3376 cli_set_error(cli, status);
3378 return status;
3381 /****************************************************************************
3382 Do a SMBsetattrE call.
3383 ****************************************************************************/
3385 static void cli_setattrE_done(struct tevent_req *subreq);
3387 struct cli_setattrE_state {
3388 uint16_t vwv[7];
3391 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3392 struct event_context *ev,
3393 struct cli_state *cli,
3394 uint16_t fnum,
3395 time_t change_time,
3396 time_t access_time,
3397 time_t write_time)
3399 struct tevent_req *req = NULL, *subreq = NULL;
3400 struct cli_setattrE_state *state = NULL;
3401 uint8_t additional_flags = 0;
3403 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3404 if (req == NULL) {
3405 return NULL;
3408 SSVAL(state->vwv+0, 0, fnum);
3409 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3410 cli->serverzone);
3411 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3412 cli->serverzone);
3413 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3414 cli->serverzone);
3416 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3417 7, state->vwv, 0, NULL);
3418 if (tevent_req_nomem(subreq, req)) {
3419 return tevent_req_post(req, ev);
3421 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3422 return req;
3425 static void cli_setattrE_done(struct tevent_req *subreq)
3427 struct tevent_req *req = tevent_req_callback_data(
3428 subreq, struct tevent_req);
3429 NTSTATUS status;
3431 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3432 TALLOC_FREE(subreq);
3433 if (!NT_STATUS_IS_OK(status)) {
3434 tevent_req_nterror(req, status);
3435 return;
3437 tevent_req_done(req);
3440 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3442 return tevent_req_simple_recv_ntstatus(req);
3445 NTSTATUS cli_setattrE(struct cli_state *cli,
3446 uint16_t fnum,
3447 time_t change_time,
3448 time_t access_time,
3449 time_t write_time)
3451 TALLOC_CTX *frame = talloc_stackframe();
3452 struct event_context *ev = NULL;
3453 struct tevent_req *req = NULL;
3454 NTSTATUS status = NT_STATUS_OK;
3456 if (cli_has_async_calls(cli)) {
3458 * Can't use sync call while an async call is in flight
3460 status = NT_STATUS_INVALID_PARAMETER;
3461 goto fail;
3464 ev = event_context_init(frame);
3465 if (ev == NULL) {
3466 status = NT_STATUS_NO_MEMORY;
3467 goto fail;
3470 req = cli_setattrE_send(frame, ev,
3471 cli,
3472 fnum,
3473 change_time,
3474 access_time,
3475 write_time);
3477 if (req == NULL) {
3478 status = NT_STATUS_NO_MEMORY;
3479 goto fail;
3482 if (!tevent_req_poll(req, ev)) {
3483 status = map_nt_error_from_unix(errno);
3484 goto fail;
3487 status = cli_setattrE_recv(req);
3489 fail:
3490 TALLOC_FREE(frame);
3491 if (!NT_STATUS_IS_OK(status)) {
3492 cli_set_error(cli, status);
3494 return status;
3497 /****************************************************************************
3498 Do a SMBsetatr call.
3499 ****************************************************************************/
3501 static void cli_setatr_done(struct tevent_req *subreq);
3503 struct cli_setatr_state {
3504 uint16_t vwv[8];
3507 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3508 struct event_context *ev,
3509 struct cli_state *cli,
3510 const char *fname,
3511 uint16_t attr,
3512 time_t mtime)
3514 struct tevent_req *req = NULL, *subreq = NULL;
3515 struct cli_setatr_state *state = NULL;
3516 uint8_t additional_flags = 0;
3517 uint8_t *bytes = NULL;
3519 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3520 if (req == NULL) {
3521 return NULL;
3524 SSVAL(state->vwv+0, 0, attr);
3525 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3527 bytes = talloc_array(state, uint8_t, 1);
3528 if (tevent_req_nomem(bytes, req)) {
3529 return tevent_req_post(req, ev);
3531 bytes[0] = 4;
3532 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3533 strlen(fname)+1, NULL);
3534 if (tevent_req_nomem(bytes, req)) {
3535 return tevent_req_post(req, ev);
3537 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3538 talloc_get_size(bytes)+1);
3539 if (tevent_req_nomem(bytes, req)) {
3540 return tevent_req_post(req, ev);
3543 bytes[talloc_get_size(bytes)-1] = 4;
3544 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3545 1, NULL);
3546 if (tevent_req_nomem(bytes, req)) {
3547 return tevent_req_post(req, ev);
3550 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3551 8, state->vwv, talloc_get_size(bytes), bytes);
3552 if (tevent_req_nomem(subreq, req)) {
3553 return tevent_req_post(req, ev);
3555 tevent_req_set_callback(subreq, cli_setatr_done, req);
3556 return req;
3559 static void cli_setatr_done(struct tevent_req *subreq)
3561 struct tevent_req *req = tevent_req_callback_data(
3562 subreq, struct tevent_req);
3563 NTSTATUS status;
3565 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3566 TALLOC_FREE(subreq);
3567 if (!NT_STATUS_IS_OK(status)) {
3568 tevent_req_nterror(req, status);
3569 return;
3571 tevent_req_done(req);
3574 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3576 return tevent_req_simple_recv_ntstatus(req);
3579 NTSTATUS cli_setatr(struct cli_state *cli,
3580 const char *fname,
3581 uint16_t attr,
3582 time_t mtime)
3584 TALLOC_CTX *frame = talloc_stackframe();
3585 struct event_context *ev = NULL;
3586 struct tevent_req *req = NULL;
3587 NTSTATUS status = NT_STATUS_OK;
3589 if (cli_has_async_calls(cli)) {
3591 * Can't use sync call while an async call is in flight
3593 status = NT_STATUS_INVALID_PARAMETER;
3594 goto fail;
3597 ev = event_context_init(frame);
3598 if (ev == NULL) {
3599 status = NT_STATUS_NO_MEMORY;
3600 goto fail;
3603 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3604 if (req == NULL) {
3605 status = NT_STATUS_NO_MEMORY;
3606 goto fail;
3609 if (!tevent_req_poll(req, ev)) {
3610 status = map_nt_error_from_unix(errno);
3611 goto fail;
3614 status = cli_setatr_recv(req);
3616 fail:
3617 TALLOC_FREE(frame);
3618 if (!NT_STATUS_IS_OK(status)) {
3619 cli_set_error(cli, status);
3621 return status;
3624 /****************************************************************************
3625 Check for existance of a dir.
3626 ****************************************************************************/
3628 static void cli_chkpath_done(struct tevent_req *subreq);
3630 struct cli_chkpath_state {
3631 int dummy;
3634 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3635 struct event_context *ev,
3636 struct cli_state *cli,
3637 const char *fname)
3639 struct tevent_req *req = NULL, *subreq = NULL;
3640 struct cli_chkpath_state *state = NULL;
3641 uint8_t additional_flags = 0;
3642 uint8_t *bytes = NULL;
3644 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3645 if (req == NULL) {
3646 return NULL;
3649 bytes = talloc_array(state, uint8_t, 1);
3650 if (tevent_req_nomem(bytes, req)) {
3651 return tevent_req_post(req, ev);
3653 bytes[0] = 4;
3654 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3655 strlen(fname)+1, NULL);
3657 if (tevent_req_nomem(bytes, req)) {
3658 return tevent_req_post(req, ev);
3661 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3662 0, NULL, talloc_get_size(bytes), bytes);
3663 if (tevent_req_nomem(subreq, req)) {
3664 return tevent_req_post(req, ev);
3666 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3667 return req;
3670 static void cli_chkpath_done(struct tevent_req *subreq)
3672 struct tevent_req *req = tevent_req_callback_data(
3673 subreq, struct tevent_req);
3674 NTSTATUS status;
3676 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3677 TALLOC_FREE(subreq);
3678 if (!NT_STATUS_IS_OK(status)) {
3679 tevent_req_nterror(req, status);
3680 return;
3682 tevent_req_done(req);
3685 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3687 return tevent_req_simple_recv_ntstatus(req);
3690 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3692 TALLOC_CTX *frame = talloc_stackframe();
3693 struct event_context *ev = NULL;
3694 struct tevent_req *req = NULL;
3695 char *path2 = NULL;
3696 NTSTATUS status = NT_STATUS_OK;
3698 if (cli_has_async_calls(cli)) {
3700 * Can't use sync call while an async call is in flight
3702 status = NT_STATUS_INVALID_PARAMETER;
3703 goto fail;
3706 path2 = talloc_strdup(frame, path);
3707 if (!path2) {
3708 status = NT_STATUS_NO_MEMORY;
3709 goto fail;
3711 trim_char(path2,'\0','\\');
3712 if (!*path2) {
3713 path2 = talloc_strdup(frame, "\\");
3714 if (!path2) {
3715 status = NT_STATUS_NO_MEMORY;
3716 goto fail;
3720 ev = event_context_init(frame);
3721 if (ev == NULL) {
3722 status = NT_STATUS_NO_MEMORY;
3723 goto fail;
3726 req = cli_chkpath_send(frame, ev, cli, path2);
3727 if (req == NULL) {
3728 status = NT_STATUS_NO_MEMORY;
3729 goto fail;
3732 if (!tevent_req_poll(req, ev)) {
3733 status = map_nt_error_from_unix(errno);
3734 goto fail;
3737 status = cli_chkpath_recv(req);
3739 fail:
3740 TALLOC_FREE(frame);
3741 if (!NT_STATUS_IS_OK(status)) {
3742 cli_set_error(cli, status);
3744 return status;
3747 /****************************************************************************
3748 Query disk space.
3749 ****************************************************************************/
3751 static void cli_dskattr_done(struct tevent_req *subreq);
3753 struct cli_dskattr_state {
3754 int bsize;
3755 int total;
3756 int avail;
3759 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3760 struct event_context *ev,
3761 struct cli_state *cli)
3763 struct tevent_req *req = NULL, *subreq = NULL;
3764 struct cli_dskattr_state *state = NULL;
3765 uint8_t additional_flags = 0;
3767 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3768 if (req == NULL) {
3769 return NULL;
3772 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3773 0, NULL, 0, NULL);
3774 if (tevent_req_nomem(subreq, req)) {
3775 return tevent_req_post(req, ev);
3777 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3778 return req;
3781 static void cli_dskattr_done(struct tevent_req *subreq)
3783 struct tevent_req *req = tevent_req_callback_data(
3784 subreq, struct tevent_req);
3785 struct cli_dskattr_state *state = tevent_req_data(
3786 req, struct cli_dskattr_state);
3787 uint8_t wct;
3788 uint16_t *vwv = NULL;
3789 uint8_t *inbuf;
3790 NTSTATUS status;
3792 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3793 NULL);
3794 TALLOC_FREE(subreq);
3795 if (!NT_STATUS_IS_OK(status)) {
3796 tevent_req_nterror(req, status);
3797 return;
3799 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3800 state->total = SVAL(vwv+0, 0);
3801 state->avail = SVAL(vwv+3, 0);
3802 tevent_req_done(req);
3805 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3807 struct cli_dskattr_state *state = tevent_req_data(
3808 req, struct cli_dskattr_state);
3809 NTSTATUS status;
3811 if (tevent_req_is_nterror(req, &status)) {
3812 return status;
3814 *bsize = state->bsize;
3815 *total = state->total;
3816 *avail = state->avail;
3817 return NT_STATUS_OK;
3820 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3822 TALLOC_CTX *frame = talloc_stackframe();
3823 struct event_context *ev = NULL;
3824 struct tevent_req *req = NULL;
3825 NTSTATUS status = NT_STATUS_OK;
3827 if (cli_has_async_calls(cli)) {
3829 * Can't use sync call while an async call is in flight
3831 status = NT_STATUS_INVALID_PARAMETER;
3832 goto fail;
3835 ev = event_context_init(frame);
3836 if (ev == NULL) {
3837 status = NT_STATUS_NO_MEMORY;
3838 goto fail;
3841 req = cli_dskattr_send(frame, ev, cli);
3842 if (req == NULL) {
3843 status = NT_STATUS_NO_MEMORY;
3844 goto fail;
3847 if (!tevent_req_poll(req, ev)) {
3848 status = map_nt_error_from_unix(errno);
3849 goto fail;
3852 status = cli_dskattr_recv(req, bsize, total, avail);
3854 fail:
3855 TALLOC_FREE(frame);
3856 if (!NT_STATUS_IS_OK(status)) {
3857 cli_set_error(cli, status);
3859 return status;
3862 /****************************************************************************
3863 Create and open a temporary file.
3864 ****************************************************************************/
3866 static void cli_ctemp_done(struct tevent_req *subreq);
3868 struct ctemp_state {
3869 uint16_t vwv[3];
3870 char *ret_path;
3871 uint16_t fnum;
3874 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3875 struct event_context *ev,
3876 struct cli_state *cli,
3877 const char *path)
3879 struct tevent_req *req = NULL, *subreq = NULL;
3880 struct ctemp_state *state = NULL;
3881 uint8_t additional_flags = 0;
3882 uint8_t *bytes = NULL;
3884 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3885 if (req == NULL) {
3886 return NULL;
3889 SSVAL(state->vwv,0,0);
3890 SIVALS(state->vwv+1,0,-1);
3892 bytes = talloc_array(state, uint8_t, 1);
3893 if (tevent_req_nomem(bytes, req)) {
3894 return tevent_req_post(req, ev);
3896 bytes[0] = 4;
3897 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3898 strlen(path)+1, NULL);
3899 if (tevent_req_nomem(bytes, req)) {
3900 return tevent_req_post(req, ev);
3903 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3904 3, state->vwv, talloc_get_size(bytes), bytes);
3905 if (tevent_req_nomem(subreq, req)) {
3906 return tevent_req_post(req, ev);
3908 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3909 return req;
3912 static void cli_ctemp_done(struct tevent_req *subreq)
3914 struct tevent_req *req = tevent_req_callback_data(
3915 subreq, struct tevent_req);
3916 struct ctemp_state *state = tevent_req_data(
3917 req, struct ctemp_state);
3918 NTSTATUS status;
3919 uint8_t wcnt;
3920 uint16_t *vwv;
3921 uint32_t num_bytes = 0;
3922 uint8_t *bytes = NULL;
3923 uint8_t *inbuf;
3925 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3926 &num_bytes, &bytes);
3927 TALLOC_FREE(subreq);
3928 if (!NT_STATUS_IS_OK(status)) {
3929 tevent_req_nterror(req, status);
3930 return;
3933 state->fnum = SVAL(vwv+0, 0);
3935 /* From W2K3, the result is just the ASCII name */
3936 if (num_bytes < 2) {
3937 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3938 return;
3941 if (pull_string_talloc(state,
3942 NULL,
3944 &state->ret_path,
3945 bytes,
3946 num_bytes,
3947 STR_ASCII) == 0) {
3948 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3949 return;
3951 tevent_req_done(req);
3954 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3955 TALLOC_CTX *ctx,
3956 uint16_t *pfnum,
3957 char **outfile)
3959 struct ctemp_state *state = tevent_req_data(req,
3960 struct ctemp_state);
3961 NTSTATUS status;
3963 if (tevent_req_is_nterror(req, &status)) {
3964 return status;
3966 *pfnum = state->fnum;
3967 *outfile = talloc_strdup(ctx, state->ret_path);
3968 if (!*outfile) {
3969 return NT_STATUS_NO_MEMORY;
3971 return NT_STATUS_OK;
3974 NTSTATUS cli_ctemp(struct cli_state *cli,
3975 TALLOC_CTX *ctx,
3976 const char *path,
3977 uint16_t *pfnum,
3978 char **out_path)
3980 TALLOC_CTX *frame = talloc_stackframe();
3981 struct event_context *ev;
3982 struct tevent_req *req;
3983 NTSTATUS status = NT_STATUS_OK;
3985 if (cli_has_async_calls(cli)) {
3987 * Can't use sync call while an async call is in flight
3989 status = NT_STATUS_INVALID_PARAMETER;
3990 goto fail;
3993 ev = event_context_init(frame);
3994 if (ev == NULL) {
3995 status = NT_STATUS_NO_MEMORY;
3996 goto fail;
3999 req = cli_ctemp_send(frame, ev, cli, path);
4000 if (req == NULL) {
4001 status = NT_STATUS_NO_MEMORY;
4002 goto fail;
4005 if (!tevent_req_poll(req, ev)) {
4006 status = map_nt_error_from_unix(errno);
4007 goto fail;
4010 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4012 fail:
4013 TALLOC_FREE(frame);
4014 if (!NT_STATUS_IS_OK(status)) {
4015 cli_set_error(cli, status);
4017 return status;
4021 send a raw ioctl - used by the torture code
4023 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4025 uint16_t vwv[3];
4026 NTSTATUS status;
4028 SSVAL(vwv+0, 0, fnum);
4029 SSVAL(vwv+1, 0, code>>16);
4030 SSVAL(vwv+2, 0, (code&0xFFFF));
4032 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4033 NULL, 0, NULL, NULL, NULL, NULL);
4034 if (!NT_STATUS_IS_OK(status)) {
4035 return status;
4037 *blob = data_blob_null;
4038 return NT_STATUS_OK;
4041 /*********************************************************
4042 Set an extended attribute utility fn.
4043 *********************************************************/
4045 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4046 uint8_t *param, unsigned int param_len,
4047 const char *ea_name,
4048 const char *ea_val, size_t ea_len)
4050 uint16_t setup[1];
4051 unsigned int data_len = 0;
4052 uint8_t *data = NULL;
4053 char *p;
4054 size_t ea_namelen = strlen(ea_name);
4055 NTSTATUS status;
4057 SSVAL(setup, 0, setup_val);
4059 if (ea_namelen == 0 && ea_len == 0) {
4060 data_len = 4;
4061 data = (uint8_t *)SMB_MALLOC(data_len);
4062 if (!data) {
4063 return NT_STATUS_NO_MEMORY;
4065 p = (char *)data;
4066 SIVAL(p,0,data_len);
4067 } else {
4068 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4069 data = (uint8_t *)SMB_MALLOC(data_len);
4070 if (!data) {
4071 return NT_STATUS_NO_MEMORY;
4073 p = (char *)data;
4074 SIVAL(p,0,data_len);
4075 p += 4;
4076 SCVAL(p, 0, 0); /* EA flags. */
4077 SCVAL(p, 1, ea_namelen);
4078 SSVAL(p, 2, ea_len);
4079 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4080 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4083 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4084 setup, 1, 0,
4085 param, param_len, 2,
4086 data, data_len, cli->max_xmit,
4087 NULL,
4088 NULL, 0, NULL, /* rsetup */
4089 NULL, 0, NULL, /* rparam */
4090 NULL, 0, NULL); /* rdata */
4091 SAFE_FREE(data);
4092 return status;
4095 /*********************************************************
4096 Set an extended attribute on a pathname.
4097 *********************************************************/
4099 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4100 const char *ea_name, const char *ea_val,
4101 size_t ea_len)
4103 unsigned int param_len = 0;
4104 uint8_t *param;
4105 size_t srclen = 2*(strlen(path)+1);
4106 char *p;
4107 NTSTATUS status;
4109 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4110 if (!param) {
4111 return NT_STATUS_NO_MEMORY;
4113 memset(param, '\0', 6);
4114 SSVAL(param,0,SMB_INFO_SET_EA);
4115 p = (char *)(&param[6]);
4117 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4118 param_len = PTR_DIFF(p, param);
4120 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4121 ea_name, ea_val, ea_len);
4122 SAFE_FREE(param);
4123 return status;
4126 /*********************************************************
4127 Set an extended attribute on an fnum.
4128 *********************************************************/
4130 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4131 const char *ea_name, const char *ea_val,
4132 size_t ea_len)
4134 uint8_t param[6];
4136 memset(param, 0, 6);
4137 SSVAL(param,0,fnum);
4138 SSVAL(param,2,SMB_INFO_SET_EA);
4140 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4141 ea_name, ea_val, ea_len);
4144 /*********************************************************
4145 Get an extended attribute list utility fn.
4146 *********************************************************/
4148 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4149 size_t rdata_len,
4150 size_t *pnum_eas, struct ea_struct **pea_list)
4152 struct ea_struct *ea_list = NULL;
4153 size_t num_eas;
4154 size_t ea_size;
4155 const uint8_t *p;
4157 if (rdata_len < 4) {
4158 return false;
4161 ea_size = (size_t)IVAL(rdata,0);
4162 if (ea_size > rdata_len) {
4163 return false;
4166 if (ea_size == 0) {
4167 /* No EA's present. */
4168 *pnum_eas = 0;
4169 *pea_list = NULL;
4170 return true;
4173 p = rdata + 4;
4174 ea_size -= 4;
4176 /* Validate the EA list and count it. */
4177 for (num_eas = 0; ea_size >= 4; num_eas++) {
4178 unsigned int ea_namelen = CVAL(p,1);
4179 unsigned int ea_valuelen = SVAL(p,2);
4180 if (ea_namelen == 0) {
4181 return false;
4183 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4184 return false;
4186 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4187 p += 4 + ea_namelen + 1 + ea_valuelen;
4190 if (num_eas == 0) {
4191 *pnum_eas = 0;
4192 *pea_list = NULL;
4193 return true;
4196 *pnum_eas = num_eas;
4197 if (!pea_list) {
4198 /* Caller only wants number of EA's. */
4199 return true;
4202 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4203 if (!ea_list) {
4204 return false;
4207 ea_size = (size_t)IVAL(rdata,0);
4208 p = rdata + 4;
4210 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4211 struct ea_struct *ea = &ea_list[num_eas];
4212 fstring unix_ea_name;
4213 unsigned int ea_namelen = CVAL(p,1);
4214 unsigned int ea_valuelen = SVAL(p,2);
4216 ea->flags = CVAL(p,0);
4217 unix_ea_name[0] = '\0';
4218 pull_ascii_fstring(unix_ea_name, p + 4);
4219 ea->name = talloc_strdup(ea_list, unix_ea_name);
4220 if (!ea->name) {
4221 goto fail;
4223 /* Ensure the value is null terminated (in case it's a string). */
4224 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4225 if (!ea->value.data) {
4226 goto fail;
4228 if (ea_valuelen) {
4229 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4231 ea->value.data[ea_valuelen] = 0;
4232 ea->value.length--;
4233 p += 4 + ea_namelen + 1 + ea_valuelen;
4236 *pea_list = ea_list;
4237 return true;
4239 fail:
4240 TALLOC_FREE(ea_list);
4241 return false;
4244 /*********************************************************
4245 Get an extended attribute list from a pathname.
4246 *********************************************************/
4248 struct cli_get_ea_list_path_state {
4249 uint32_t num_data;
4250 uint8_t *data;
4253 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4255 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4256 struct tevent_context *ev,
4257 struct cli_state *cli,
4258 const char *fname)
4260 struct tevent_req *req, *subreq;
4261 struct cli_get_ea_list_path_state *state;
4263 req = tevent_req_create(mem_ctx, &state,
4264 struct cli_get_ea_list_path_state);
4265 if (req == NULL) {
4266 return NULL;
4268 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4269 SMB_INFO_QUERY_ALL_EAS, 4,
4270 cli->max_xmit);
4271 if (tevent_req_nomem(subreq, req)) {
4272 return tevent_req_post(req, ev);
4274 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4275 return req;
4278 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4280 struct tevent_req *req = tevent_req_callback_data(
4281 subreq, struct tevent_req);
4282 struct cli_get_ea_list_path_state *state = tevent_req_data(
4283 req, struct cli_get_ea_list_path_state);
4284 NTSTATUS status;
4286 status = cli_qpathinfo_recv(subreq, state, &state->data,
4287 &state->num_data);
4288 TALLOC_FREE(subreq);
4289 if (!NT_STATUS_IS_OK(status)) {
4290 tevent_req_nterror(req, status);
4291 return;
4293 tevent_req_done(req);
4296 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4297 size_t *pnum_eas, struct ea_struct **peas)
4299 struct cli_get_ea_list_path_state *state = tevent_req_data(
4300 req, struct cli_get_ea_list_path_state);
4301 NTSTATUS status;
4303 if (tevent_req_is_nterror(req, &status)) {
4304 return status;
4306 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4307 pnum_eas, peas)) {
4308 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4310 return NT_STATUS_OK;
4313 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4314 TALLOC_CTX *ctx,
4315 size_t *pnum_eas,
4316 struct ea_struct **pea_list)
4318 TALLOC_CTX *frame = talloc_stackframe();
4319 struct event_context *ev = NULL;
4320 struct tevent_req *req = NULL;
4321 NTSTATUS status = NT_STATUS_NO_MEMORY;
4323 if (cli_has_async_calls(cli)) {
4325 * Can't use sync call while an async call is in flight
4327 status = NT_STATUS_INVALID_PARAMETER;
4328 goto fail;
4330 ev = event_context_init(frame);
4331 if (ev == NULL) {
4332 goto fail;
4334 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4335 if (req == NULL) {
4336 goto fail;
4338 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4339 goto fail;
4341 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4342 fail:
4343 TALLOC_FREE(frame);
4344 if (!NT_STATUS_IS_OK(status)) {
4345 cli_set_error(cli, status);
4347 return status;
4350 /****************************************************************************
4351 Convert open "flags" arg to uint32_t on wire.
4352 ****************************************************************************/
4354 static uint32_t open_flags_to_wire(int flags)
4356 int open_mode = flags & O_ACCMODE;
4357 uint32_t ret = 0;
4359 switch (open_mode) {
4360 case O_WRONLY:
4361 ret |= SMB_O_WRONLY;
4362 break;
4363 case O_RDWR:
4364 ret |= SMB_O_RDWR;
4365 break;
4366 default:
4367 case O_RDONLY:
4368 ret |= SMB_O_RDONLY;
4369 break;
4372 if (flags & O_CREAT) {
4373 ret |= SMB_O_CREAT;
4375 if (flags & O_EXCL) {
4376 ret |= SMB_O_EXCL;
4378 if (flags & O_TRUNC) {
4379 ret |= SMB_O_TRUNC;
4381 #if defined(O_SYNC)
4382 if (flags & O_SYNC) {
4383 ret |= SMB_O_SYNC;
4385 #endif /* O_SYNC */
4386 if (flags & O_APPEND) {
4387 ret |= SMB_O_APPEND;
4389 #if defined(O_DIRECT)
4390 if (flags & O_DIRECT) {
4391 ret |= SMB_O_DIRECT;
4393 #endif
4394 #if defined(O_DIRECTORY)
4395 if (flags & O_DIRECTORY) {
4396 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4397 ret |= SMB_O_DIRECTORY;
4399 #endif
4400 return ret;
4403 /****************************************************************************
4404 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4405 ****************************************************************************/
4407 struct posix_open_state {
4408 uint16_t setup;
4409 uint8_t *param;
4410 uint8_t data[18];
4411 uint16_t fnum; /* Out */
4414 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4416 struct tevent_req *req = tevent_req_callback_data(
4417 subreq, struct tevent_req);
4418 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4419 NTSTATUS status;
4420 uint8_t *data;
4421 uint32_t num_data;
4423 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4424 NULL, 0, NULL, &data, 12, &num_data);
4425 TALLOC_FREE(subreq);
4426 if (!NT_STATUS_IS_OK(status)) {
4427 tevent_req_nterror(req, status);
4428 return;
4430 state->fnum = SVAL(data,2);
4431 tevent_req_done(req);
4434 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4435 struct event_context *ev,
4436 struct cli_state *cli,
4437 const char *fname,
4438 int flags,
4439 mode_t mode,
4440 bool is_dir)
4442 struct tevent_req *req = NULL, *subreq = NULL;
4443 struct posix_open_state *state = NULL;
4444 uint32_t wire_flags = open_flags_to_wire(flags);
4446 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4447 if (req == NULL) {
4448 return NULL;
4451 /* Setup setup word. */
4452 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4454 /* Setup param array. */
4455 state->param = talloc_array(state, uint8_t, 6);
4456 if (tevent_req_nomem(state->param, req)) {
4457 return tevent_req_post(req, ev);
4459 memset(state->param, '\0', 6);
4460 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4462 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4463 strlen(fname)+1, NULL);
4465 if (tevent_req_nomem(state->param, req)) {
4466 return tevent_req_post(req, ev);
4469 /* Setup data words. */
4470 if (is_dir) {
4471 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4472 wire_flags |= SMB_O_DIRECTORY;
4475 SIVAL(state->data,0,0); /* No oplock. */
4476 SIVAL(state->data,4,wire_flags);
4477 SIVAL(state->data,8,unix_perms_to_wire(mode));
4478 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4479 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4481 subreq = cli_trans_send(state, /* mem ctx. */
4482 ev, /* event ctx. */
4483 cli, /* cli_state. */
4484 SMBtrans2, /* cmd. */
4485 NULL, /* pipe name. */
4486 -1, /* fid. */
4487 0, /* function. */
4488 0, /* flags. */
4489 &state->setup, /* setup. */
4490 1, /* num setup uint16_t words. */
4491 0, /* max returned setup. */
4492 state->param, /* param. */
4493 talloc_get_size(state->param),/* num param. */
4494 2, /* max returned param. */
4495 state->data, /* data. */
4496 18, /* num data. */
4497 12); /* max returned data. */
4499 if (tevent_req_nomem(subreq, req)) {
4500 return tevent_req_post(req, ev);
4502 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4503 return req;
4506 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4507 struct event_context *ev,
4508 struct cli_state *cli,
4509 const char *fname,
4510 int flags,
4511 mode_t mode)
4513 return cli_posix_open_internal_send(mem_ctx, ev,
4514 cli, fname, flags, mode, false);
4517 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4519 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4520 NTSTATUS status;
4522 if (tevent_req_is_nterror(req, &status)) {
4523 return status;
4525 *pfnum = state->fnum;
4526 return NT_STATUS_OK;
4529 /****************************************************************************
4530 Open - POSIX semantics. Doesn't request oplock.
4531 ****************************************************************************/
4533 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4534 int flags, mode_t mode, uint16_t *pfnum)
4537 TALLOC_CTX *frame = talloc_stackframe();
4538 struct event_context *ev = NULL;
4539 struct tevent_req *req = NULL;
4540 NTSTATUS status = NT_STATUS_OK;
4542 if (cli_has_async_calls(cli)) {
4544 * Can't use sync call while an async call is in flight
4546 status = NT_STATUS_INVALID_PARAMETER;
4547 goto fail;
4550 ev = event_context_init(frame);
4551 if (ev == NULL) {
4552 status = NT_STATUS_NO_MEMORY;
4553 goto fail;
4556 req = cli_posix_open_send(frame,
4558 cli,
4559 fname,
4560 flags,
4561 mode);
4562 if (req == NULL) {
4563 status = NT_STATUS_NO_MEMORY;
4564 goto fail;
4567 if (!tevent_req_poll(req, ev)) {
4568 status = map_nt_error_from_unix(errno);
4569 goto fail;
4572 status = cli_posix_open_recv(req, pfnum);
4574 fail:
4575 TALLOC_FREE(frame);
4576 if (!NT_STATUS_IS_OK(status)) {
4577 cli_set_error(cli, status);
4579 return status;
4582 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4583 struct event_context *ev,
4584 struct cli_state *cli,
4585 const char *fname,
4586 mode_t mode)
4588 return cli_posix_open_internal_send(mem_ctx, ev,
4589 cli, fname, O_CREAT, mode, true);
4592 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4594 return tevent_req_simple_recv_ntstatus(req);
4597 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4599 TALLOC_CTX *frame = talloc_stackframe();
4600 struct event_context *ev = NULL;
4601 struct tevent_req *req = NULL;
4602 NTSTATUS status = NT_STATUS_OK;
4604 if (cli_has_async_calls(cli)) {
4606 * Can't use sync call while an async call is in flight
4608 status = NT_STATUS_INVALID_PARAMETER;
4609 goto fail;
4612 ev = event_context_init(frame);
4613 if (ev == NULL) {
4614 status = NT_STATUS_NO_MEMORY;
4615 goto fail;
4618 req = cli_posix_mkdir_send(frame,
4620 cli,
4621 fname,
4622 mode);
4623 if (req == NULL) {
4624 status = NT_STATUS_NO_MEMORY;
4625 goto fail;
4628 if (!tevent_req_poll(req, ev)) {
4629 status = map_nt_error_from_unix(errno);
4630 goto fail;
4633 status = cli_posix_mkdir_recv(req);
4635 fail:
4636 TALLOC_FREE(frame);
4637 if (!NT_STATUS_IS_OK(status)) {
4638 cli_set_error(cli, status);
4640 return status;
4643 /****************************************************************************
4644 unlink or rmdir - POSIX semantics.
4645 ****************************************************************************/
4647 struct cli_posix_unlink_internal_state {
4648 uint8_t data[2];
4651 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4653 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4654 struct event_context *ev,
4655 struct cli_state *cli,
4656 const char *fname,
4657 uint16_t level)
4659 struct tevent_req *req = NULL, *subreq = NULL;
4660 struct cli_posix_unlink_internal_state *state = NULL;
4662 req = tevent_req_create(mem_ctx, &state,
4663 struct cli_posix_unlink_internal_state);
4664 if (req == NULL) {
4665 return NULL;
4668 /* Setup data word. */
4669 SSVAL(state->data, 0, level);
4671 subreq = cli_setpathinfo_send(state, ev, cli,
4672 SMB_POSIX_PATH_UNLINK,
4673 fname,
4674 state->data, sizeof(state->data));
4675 if (tevent_req_nomem(subreq, req)) {
4676 return tevent_req_post(req, ev);
4678 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4679 return req;
4682 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4684 NTSTATUS status = cli_setpathinfo_recv(subreq);
4685 tevent_req_simple_finish_ntstatus(subreq, status);
4688 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4689 struct event_context *ev,
4690 struct cli_state *cli,
4691 const char *fname)
4693 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4694 SMB_POSIX_UNLINK_FILE_TARGET);
4697 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4699 return tevent_req_simple_recv_ntstatus(req);
4702 /****************************************************************************
4703 unlink - POSIX semantics.
4704 ****************************************************************************/
4706 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4708 TALLOC_CTX *frame = talloc_stackframe();
4709 struct event_context *ev = NULL;
4710 struct tevent_req *req = NULL;
4711 NTSTATUS status = NT_STATUS_OK;
4713 if (cli_has_async_calls(cli)) {
4715 * Can't use sync call while an async call is in flight
4717 status = NT_STATUS_INVALID_PARAMETER;
4718 goto fail;
4721 ev = event_context_init(frame);
4722 if (ev == NULL) {
4723 status = NT_STATUS_NO_MEMORY;
4724 goto fail;
4727 req = cli_posix_unlink_send(frame,
4729 cli,
4730 fname);
4731 if (req == NULL) {
4732 status = NT_STATUS_NO_MEMORY;
4733 goto fail;
4736 if (!tevent_req_poll(req, ev)) {
4737 status = map_nt_error_from_unix(errno);
4738 goto fail;
4741 status = cli_posix_unlink_recv(req);
4743 fail:
4744 TALLOC_FREE(frame);
4745 if (!NT_STATUS_IS_OK(status)) {
4746 cli_set_error(cli, status);
4748 return status;
4751 /****************************************************************************
4752 rmdir - POSIX semantics.
4753 ****************************************************************************/
4755 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4756 struct event_context *ev,
4757 struct cli_state *cli,
4758 const char *fname)
4760 return cli_posix_unlink_internal_send(
4761 mem_ctx, ev, cli, fname,
4762 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4765 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4767 return tevent_req_simple_recv_ntstatus(req);
4770 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4772 TALLOC_CTX *frame = talloc_stackframe();
4773 struct event_context *ev = NULL;
4774 struct tevent_req *req = NULL;
4775 NTSTATUS status = NT_STATUS_OK;
4777 if (cli_has_async_calls(cli)) {
4779 * Can't use sync call while an async call is in flight
4781 status = NT_STATUS_INVALID_PARAMETER;
4782 goto fail;
4785 ev = event_context_init(frame);
4786 if (ev == NULL) {
4787 status = NT_STATUS_NO_MEMORY;
4788 goto fail;
4791 req = cli_posix_rmdir_send(frame,
4793 cli,
4794 fname);
4795 if (req == NULL) {
4796 status = NT_STATUS_NO_MEMORY;
4797 goto fail;
4800 if (!tevent_req_poll(req, ev)) {
4801 status = map_nt_error_from_unix(errno);
4802 goto fail;
4805 status = cli_posix_rmdir_recv(req, frame);
4807 fail:
4808 TALLOC_FREE(frame);
4809 if (!NT_STATUS_IS_OK(status)) {
4810 cli_set_error(cli, status);
4812 return status;
4815 /****************************************************************************
4816 filechangenotify
4817 ****************************************************************************/
4819 struct cli_notify_state {
4820 uint8_t setup[8];
4821 uint32_t num_changes;
4822 struct notify_change *changes;
4825 static void cli_notify_done(struct tevent_req *subreq);
4827 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4828 struct tevent_context *ev,
4829 struct cli_state *cli, uint16_t fnum,
4830 uint32_t buffer_size,
4831 uint32_t completion_filter, bool recursive)
4833 struct tevent_req *req, *subreq;
4834 struct cli_notify_state *state;
4836 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4837 if (req == NULL) {
4838 return NULL;
4841 SIVAL(state->setup, 0, completion_filter);
4842 SSVAL(state->setup, 4, fnum);
4843 SSVAL(state->setup, 6, recursive);
4845 subreq = cli_trans_send(
4846 state, /* mem ctx. */
4847 ev, /* event ctx. */
4848 cli, /* cli_state. */
4849 SMBnttrans, /* cmd. */
4850 NULL, /* pipe name. */
4851 -1, /* fid. */
4852 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4853 0, /* flags. */
4854 (uint16_t *)state->setup, /* setup. */
4855 4, /* num setup uint16_t words. */
4856 0, /* max returned setup. */
4857 NULL, /* param. */
4858 0, /* num param. */
4859 buffer_size, /* max returned param. */
4860 NULL, /* data. */
4861 0, /* num data. */
4862 0); /* max returned data. */
4864 if (tevent_req_nomem(subreq, req)) {
4865 return tevent_req_post(req, ev);
4867 tevent_req_set_callback(subreq, cli_notify_done, req);
4868 return req;
4871 static void cli_notify_done(struct tevent_req *subreq)
4873 struct tevent_req *req = tevent_req_callback_data(
4874 subreq, struct tevent_req);
4875 struct cli_notify_state *state = tevent_req_data(
4876 req, struct cli_notify_state);
4877 NTSTATUS status;
4878 uint8_t *params;
4879 uint32_t i, ofs, num_params;
4880 uint16_t flags2;
4882 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4883 &params, 0, &num_params, NULL, 0, NULL);
4884 TALLOC_FREE(subreq);
4885 if (!NT_STATUS_IS_OK(status)) {
4886 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4887 tevent_req_nterror(req, status);
4888 return;
4891 state->num_changes = 0;
4892 ofs = 0;
4894 while (num_params - ofs > 12) {
4895 uint32_t len = IVAL(params, ofs);
4896 state->num_changes += 1;
4898 if ((len == 0) || (ofs+len >= num_params)) {
4899 break;
4901 ofs += len;
4904 state->changes = talloc_array(state, struct notify_change,
4905 state->num_changes);
4906 if (tevent_req_nomem(state->changes, req)) {
4907 TALLOC_FREE(params);
4908 return;
4911 ofs = 0;
4913 for (i=0; i<state->num_changes; i++) {
4914 uint32_t next = IVAL(params, ofs);
4915 uint32_t len = IVAL(params, ofs+8);
4916 ssize_t ret;
4917 char *name;
4919 if ((next != 0) && (len+12 != next)) {
4920 TALLOC_FREE(params);
4921 tevent_req_nterror(
4922 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4923 return;
4926 state->changes[i].action = IVAL(params, ofs+4);
4927 ret = clistr_pull_talloc(params, (char *)params, flags2,
4928 &name, params+ofs+12, len,
4929 STR_TERMINATE|STR_UNICODE);
4930 if (ret == -1) {
4931 TALLOC_FREE(params);
4932 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4933 return;
4935 state->changes[i].name = name;
4936 ofs += next;
4939 TALLOC_FREE(params);
4940 tevent_req_done(req);
4943 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4944 uint32_t *pnum_changes,
4945 struct notify_change **pchanges)
4947 struct cli_notify_state *state = tevent_req_data(
4948 req, struct cli_notify_state);
4949 NTSTATUS status;
4951 if (tevent_req_is_nterror(req, &status)) {
4952 return status;
4955 *pnum_changes = state->num_changes;
4956 *pchanges = talloc_move(mem_ctx, &state->changes);
4957 return NT_STATUS_OK;
4960 struct cli_qpathinfo_state {
4961 uint8_t *param;
4962 uint8_t *data;
4963 uint16_t setup[1];
4964 uint32_t min_rdata;
4965 uint8_t *rdata;
4966 uint32_t num_rdata;
4969 static void cli_qpathinfo_done(struct tevent_req *subreq);
4971 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4972 struct tevent_context *ev,
4973 struct cli_state *cli, const char *fname,
4974 uint16_t level, uint32_t min_rdata,
4975 uint32_t max_rdata)
4977 struct tevent_req *req, *subreq;
4978 struct cli_qpathinfo_state *state;
4980 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4981 if (req == NULL) {
4982 return NULL;
4984 state->min_rdata = min_rdata;
4985 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4987 state->param = talloc_zero_array(state, uint8_t, 6);
4988 if (tevent_req_nomem(state->param, req)) {
4989 return tevent_req_post(req, ev);
4991 SSVAL(state->param, 0, level);
4992 state->param = trans2_bytes_push_str(
4993 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4994 if (tevent_req_nomem(state->param, req)) {
4995 return tevent_req_post(req, ev);
4998 subreq = cli_trans_send(
4999 state, /* mem ctx. */
5000 ev, /* event ctx. */
5001 cli, /* cli_state. */
5002 SMBtrans2, /* cmd. */
5003 NULL, /* pipe name. */
5004 -1, /* fid. */
5005 0, /* function. */
5006 0, /* flags. */
5007 state->setup, /* setup. */
5008 1, /* num setup uint16_t words. */
5009 0, /* max returned setup. */
5010 state->param, /* param. */
5011 talloc_get_size(state->param), /* num param. */
5012 2, /* max returned param. */
5013 NULL, /* data. */
5014 0, /* num data. */
5015 max_rdata); /* max returned data. */
5017 if (tevent_req_nomem(subreq, req)) {
5018 return tevent_req_post(req, ev);
5020 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5021 return req;
5024 static void cli_qpathinfo_done(struct tevent_req *subreq)
5026 struct tevent_req *req = tevent_req_callback_data(
5027 subreq, struct tevent_req);
5028 struct cli_qpathinfo_state *state = tevent_req_data(
5029 req, struct cli_qpathinfo_state);
5030 NTSTATUS status;
5032 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5033 NULL, 0, NULL,
5034 &state->rdata, state->min_rdata,
5035 &state->num_rdata);
5036 if (!NT_STATUS_IS_OK(status)) {
5037 tevent_req_nterror(req, status);
5038 return;
5040 tevent_req_done(req);
5043 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5044 uint8_t **rdata, uint32_t *num_rdata)
5046 struct cli_qpathinfo_state *state = tevent_req_data(
5047 req, struct cli_qpathinfo_state);
5048 NTSTATUS status;
5050 if (tevent_req_is_nterror(req, &status)) {
5051 return status;
5053 if (rdata != NULL) {
5054 *rdata = talloc_move(mem_ctx, &state->rdata);
5055 } else {
5056 TALLOC_FREE(state->rdata);
5058 if (num_rdata != NULL) {
5059 *num_rdata = state->num_rdata;
5061 return NT_STATUS_OK;
5064 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5065 const char *fname, uint16_t level, uint32_t min_rdata,
5066 uint32_t max_rdata,
5067 uint8_t **rdata, uint32_t *num_rdata)
5069 TALLOC_CTX *frame = talloc_stackframe();
5070 struct event_context *ev;
5071 struct tevent_req *req;
5072 NTSTATUS status = NT_STATUS_NO_MEMORY;
5074 if (cli_has_async_calls(cli)) {
5076 * Can't use sync call while an async call is in flight
5078 status = NT_STATUS_INVALID_PARAMETER;
5079 goto fail;
5081 ev = event_context_init(frame);
5082 if (ev == NULL) {
5083 goto fail;
5085 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5086 max_rdata);
5087 if (req == NULL) {
5088 goto fail;
5090 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5091 goto fail;
5093 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5094 fail:
5095 TALLOC_FREE(frame);
5096 if (!NT_STATUS_IS_OK(status)) {
5097 cli_set_error(cli, status);
5099 return status;
5102 struct cli_qfileinfo_state {
5103 uint16_t setup[1];
5104 uint8_t param[4];
5105 uint8_t *data;
5106 uint32_t min_rdata;
5107 uint8_t *rdata;
5108 uint32_t num_rdata;
5111 static void cli_qfileinfo_done(struct tevent_req *subreq);
5113 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5114 struct tevent_context *ev,
5115 struct cli_state *cli, uint16_t fnum,
5116 uint16_t level, uint32_t min_rdata,
5117 uint32_t max_rdata)
5119 struct tevent_req *req, *subreq;
5120 struct cli_qfileinfo_state *state;
5122 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5123 if (req == NULL) {
5124 return NULL;
5126 state->min_rdata = min_rdata;
5127 SSVAL(state->param, 0, fnum);
5128 SSVAL(state->param, 2, level);
5129 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5131 subreq = cli_trans_send(
5132 state, /* mem ctx. */
5133 ev, /* event ctx. */
5134 cli, /* cli_state. */
5135 SMBtrans2, /* cmd. */
5136 NULL, /* pipe name. */
5137 -1, /* fid. */
5138 0, /* function. */
5139 0, /* flags. */
5140 state->setup, /* setup. */
5141 1, /* num setup uint16_t words. */
5142 0, /* max returned setup. */
5143 state->param, /* param. */
5144 sizeof(state->param), /* num param. */
5145 2, /* max returned param. */
5146 NULL, /* data. */
5147 0, /* num data. */
5148 max_rdata); /* max returned data. */
5150 if (tevent_req_nomem(subreq, req)) {
5151 return tevent_req_post(req, ev);
5153 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5154 return req;
5157 static void cli_qfileinfo_done(struct tevent_req *subreq)
5159 struct tevent_req *req = tevent_req_callback_data(
5160 subreq, struct tevent_req);
5161 struct cli_qfileinfo_state *state = tevent_req_data(
5162 req, struct cli_qfileinfo_state);
5163 NTSTATUS status;
5165 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5166 NULL, 0, NULL,
5167 &state->rdata, state->min_rdata,
5168 &state->num_rdata);
5169 if (!NT_STATUS_IS_OK(status)) {
5170 tevent_req_nterror(req, status);
5171 return;
5173 tevent_req_done(req);
5176 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5177 uint8_t **rdata, uint32_t *num_rdata)
5179 struct cli_qfileinfo_state *state = tevent_req_data(
5180 req, struct cli_qfileinfo_state);
5181 NTSTATUS status;
5183 if (tevent_req_is_nterror(req, &status)) {
5184 return status;
5186 if (rdata != NULL) {
5187 *rdata = talloc_move(mem_ctx, &state->rdata);
5188 } else {
5189 TALLOC_FREE(state->rdata);
5191 if (num_rdata != NULL) {
5192 *num_rdata = state->num_rdata;
5194 return NT_STATUS_OK;
5197 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5198 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5199 uint32_t max_rdata,
5200 uint8_t **rdata, uint32_t *num_rdata)
5202 TALLOC_CTX *frame = talloc_stackframe();
5203 struct event_context *ev;
5204 struct tevent_req *req;
5205 NTSTATUS status = NT_STATUS_NO_MEMORY;
5207 if (cli_has_async_calls(cli)) {
5209 * Can't use sync call while an async call is in flight
5211 status = NT_STATUS_INVALID_PARAMETER;
5212 goto fail;
5214 ev = event_context_init(frame);
5215 if (ev == NULL) {
5216 goto fail;
5218 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5219 max_rdata);
5220 if (req == NULL) {
5221 goto fail;
5223 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5224 goto fail;
5226 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5227 fail:
5228 TALLOC_FREE(frame);
5229 if (!NT_STATUS_IS_OK(status)) {
5230 cli_set_error(cli, status);
5232 return status;
5235 struct cli_flush_state {
5236 uint16_t vwv[1];
5239 static void cli_flush_done(struct tevent_req *subreq);
5241 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5242 struct event_context *ev,
5243 struct cli_state *cli,
5244 uint16_t fnum)
5246 struct tevent_req *req, *subreq;
5247 struct cli_flush_state *state;
5249 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5250 if (req == NULL) {
5251 return NULL;
5253 SSVAL(state->vwv + 0, 0, fnum);
5255 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5256 0, NULL);
5257 if (tevent_req_nomem(subreq, req)) {
5258 return tevent_req_post(req, ev);
5260 tevent_req_set_callback(subreq, cli_flush_done, req);
5261 return req;
5264 static void cli_flush_done(struct tevent_req *subreq)
5266 struct tevent_req *req = tevent_req_callback_data(
5267 subreq, struct tevent_req);
5268 NTSTATUS status;
5270 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5271 TALLOC_FREE(subreq);
5272 if (!NT_STATUS_IS_OK(status)) {
5273 tevent_req_nterror(req, status);
5274 return;
5276 tevent_req_done(req);
5279 NTSTATUS cli_flush_recv(struct tevent_req *req)
5281 return tevent_req_simple_recv_ntstatus(req);
5284 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5286 TALLOC_CTX *frame = talloc_stackframe();
5287 struct event_context *ev;
5288 struct tevent_req *req;
5289 NTSTATUS status = NT_STATUS_NO_MEMORY;
5291 if (cli_has_async_calls(cli)) {
5293 * Can't use sync call while an async call is in flight
5295 status = NT_STATUS_INVALID_PARAMETER;
5296 goto fail;
5298 ev = event_context_init(frame);
5299 if (ev == NULL) {
5300 goto fail;
5302 req = cli_flush_send(frame, ev, cli, fnum);
5303 if (req == NULL) {
5304 goto fail;
5306 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5307 goto fail;
5309 status = cli_flush_recv(req);
5310 fail:
5311 TALLOC_FREE(frame);
5312 if (!NT_STATUS_IS_OK(status)) {
5313 cli_set_error(cli, status);
5315 return status;
5318 struct cli_shadow_copy_data_state {
5319 uint16_t setup[4];
5320 uint8_t *data;
5321 uint32_t num_data;
5322 bool get_names;
5325 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5327 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5328 struct tevent_context *ev,
5329 struct cli_state *cli,
5330 uint16_t fnum,
5331 bool get_names)
5333 struct tevent_req *req, *subreq;
5334 struct cli_shadow_copy_data_state *state;
5335 uint32_t ret_size;
5337 req = tevent_req_create(mem_ctx, &state,
5338 struct cli_shadow_copy_data_state);
5339 if (req == NULL) {
5340 return NULL;
5342 state->get_names = get_names;
5343 ret_size = get_names ? cli->max_xmit : 16;
5345 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5346 SSVAL(state->setup + 2, 0, fnum);
5347 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5348 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5350 subreq = cli_trans_send(
5351 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5352 state->setup, ARRAY_SIZE(state->setup), 0,
5353 NULL, 0, 0,
5354 NULL, 0, ret_size);
5355 if (tevent_req_nomem(subreq, req)) {
5356 return tevent_req_post(req, ev);
5358 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5359 return req;
5362 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5364 struct tevent_req *req = tevent_req_callback_data(
5365 subreq, struct tevent_req);
5366 struct cli_shadow_copy_data_state *state = tevent_req_data(
5367 req, struct cli_shadow_copy_data_state);
5368 NTSTATUS status;
5370 status = cli_trans_recv(subreq, state, NULL,
5371 NULL, 0, NULL, /* setup */
5372 NULL, 0, NULL, /* param */
5373 &state->data, 12, &state->num_data);
5374 TALLOC_FREE(subreq);
5375 if (!NT_STATUS_IS_OK(status)) {
5376 tevent_req_nterror(req, status);
5377 return;
5379 tevent_req_done(req);
5382 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5383 char ***pnames, int *pnum_names)
5385 struct cli_shadow_copy_data_state *state = tevent_req_data(
5386 req, struct cli_shadow_copy_data_state);
5387 char **names;
5388 int i, num_names;
5389 uint32_t dlength;
5390 NTSTATUS status;
5392 if (tevent_req_is_nterror(req, &status)) {
5393 return status;
5395 num_names = IVAL(state->data, 4);
5396 dlength = IVAL(state->data, 8);
5398 if (!state->get_names) {
5399 *pnum_names = num_names;
5400 return NT_STATUS_OK;
5403 if (dlength+12 > state->num_data) {
5404 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5406 names = talloc_array(mem_ctx, char *, num_names);
5407 if (names == NULL) {
5408 return NT_STATUS_NO_MEMORY;
5411 for (i=0; i<num_names; i++) {
5412 bool ret;
5413 uint8_t *src;
5414 size_t converted_size;
5416 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5417 ret = convert_string_talloc(
5418 names, CH_UTF16LE, CH_UNIX,
5419 src, 2 * sizeof(SHADOW_COPY_LABEL),
5420 &names[i], &converted_size, True);
5421 if (!ret) {
5422 TALLOC_FREE(names);
5423 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5426 *pnum_names = num_names;
5427 *pnames = names;
5428 return NT_STATUS_OK;
5431 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5432 uint16_t fnum, bool get_names,
5433 char ***pnames, int *pnum_names)
5435 TALLOC_CTX *frame = talloc_stackframe();
5436 struct event_context *ev;
5437 struct tevent_req *req;
5438 NTSTATUS status = NT_STATUS_NO_MEMORY;
5440 if (cli_has_async_calls(cli)) {
5442 * Can't use sync call while an async call is in flight
5444 status = NT_STATUS_INVALID_PARAMETER;
5445 goto fail;
5447 ev = event_context_init(frame);
5448 if (ev == NULL) {
5449 goto fail;
5451 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5452 if (req == NULL) {
5453 goto fail;
5455 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5456 goto fail;
5458 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5459 fail:
5460 TALLOC_FREE(frame);
5461 if (!NT_STATUS_IS_OK(status)) {
5462 cli_set_error(cli, status);
5464 return status;