WHATSNEW: mention initial support for SMB3
[Samba/gebeck_regimport.git] / source3 / libsmb / clifile.c
blob1ee7fff8b592377e56c4ef7fd063175d6ca9b6a0
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
27 #include "trans2.h"
28 #include "ntioctl.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 /***********************************************************
33 Common function for pushing stings, used by smb_bytes_push_str()
34 and trans_bytes_push_str(). Only difference is the align_odd
35 parameter setting.
36 ***********************************************************/
38 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
39 const char *str, size_t str_len,
40 bool align_odd,
41 size_t *pconverted_size)
43 size_t buflen;
44 char *converted;
45 size_t converted_size;
47 if (buf == NULL) {
48 return NULL;
51 buflen = talloc_get_size(buf);
53 if (ucs2 &&
54 ((align_odd && (buflen % 2 == 0)) ||
55 (!align_odd && (buflen % 2 == 1)))) {
57 * We're pushing into an SMB buffer, align odd
59 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
60 if (buf == NULL) {
61 return NULL;
63 buf[buflen] = '\0';
64 buflen += 1;
67 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
68 ucs2 ? CH_UTF16LE : CH_DOS,
69 str, str_len, &converted,
70 &converted_size)) {
71 return NULL;
74 buf = talloc_realloc(NULL, buf, uint8_t,
75 buflen + converted_size);
76 if (buf == NULL) {
77 TALLOC_FREE(converted);
78 return NULL;
81 memcpy(buf + buflen, converted, converted_size);
83 TALLOC_FREE(converted);
85 if (pconverted_size) {
86 *pconverted_size = converted_size;
89 return buf;
92 /***********************************************************
93 Push a string into an SMB buffer, with odd byte alignment
94 if it's a UCS2 string.
95 ***********************************************************/
97 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
98 const char *str, size_t str_len,
99 size_t *pconverted_size)
101 return internal_bytes_push_str(buf, ucs2, str, str_len,
102 true, pconverted_size);
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
106 const uint8_t *bytes, size_t num_bytes)
108 size_t buflen;
110 if (buf == NULL) {
111 return NULL;
113 buflen = talloc_get_size(buf);
115 buf = talloc_realloc(NULL, buf, uint8_t,
116 buflen + 1 + num_bytes);
117 if (buf == NULL) {
118 return NULL;
120 buf[buflen] = prefix;
121 memcpy(&buf[buflen+1], bytes, num_bytes);
122 return buf;
125 /***********************************************************
126 Same as smb_bytes_push_str(), but without the odd byte
127 align for ucs2 (we're pushing into a param or data block).
128 static for now, although this will probably change when
129 other modules use async trans calls.
130 ***********************************************************/
132 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
133 const char *str, size_t str_len,
134 size_t *pconverted_size)
136 return internal_bytes_push_str(buf, ucs2, str, str_len,
137 false, pconverted_size);
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
141 const uint8_t *bytes, size_t num_bytes)
143 size_t buflen;
145 if (buf == NULL) {
146 return NULL;
148 buflen = talloc_get_size(buf);
150 buf = talloc_realloc(NULL, buf, uint8_t,
151 buflen + num_bytes);
152 if (buf == NULL) {
153 return NULL;
155 memcpy(&buf[buflen], bytes, num_bytes);
156 return buf;
159 struct cli_setpathinfo_state {
160 uint16_t setup;
161 uint8_t *param;
164 static void cli_setpathinfo_done(struct tevent_req *subreq);
166 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
167 struct tevent_context *ev,
168 struct cli_state *cli,
169 uint16_t level,
170 const char *path,
171 uint8_t *data,
172 size_t data_len)
174 struct tevent_req *req, *subreq;
175 struct cli_setpathinfo_state *state;
177 req = tevent_req_create(mem_ctx, &state,
178 struct cli_setpathinfo_state);
179 if (req == NULL) {
180 return NULL;
183 /* Setup setup word. */
184 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
186 /* Setup param array. */
187 state->param = talloc_zero_array(state, uint8_t, 6);
188 if (tevent_req_nomem(state->param, req)) {
189 return tevent_req_post(req, ev);
191 SSVAL(state->param, 0, level);
193 state->param = trans2_bytes_push_str(
194 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
195 if (tevent_req_nomem(state->param, req)) {
196 return tevent_req_post(req, ev);
199 subreq = cli_trans_send(
200 state, /* mem ctx. */
201 ev, /* event ctx. */
202 cli, /* cli_state. */
203 SMBtrans2, /* cmd. */
204 NULL, /* pipe name. */
205 -1, /* fid. */
206 0, /* function. */
207 0, /* flags. */
208 &state->setup, /* setup. */
209 1, /* num setup uint16_t words. */
210 0, /* max returned setup. */
211 state->param, /* param. */
212 talloc_get_size(state->param), /* num param. */
213 2, /* max returned param. */
214 data, /* data. */
215 data_len, /* num data. */
216 0); /* max returned data. */
218 if (tevent_req_nomem(subreq, req)) {
219 return tevent_req_post(req, ev);
221 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
222 return req;
225 static void cli_setpathinfo_done(struct tevent_req *subreq)
227 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
228 NULL, 0, NULL, NULL, 0, NULL);
229 tevent_req_simple_finish_ntstatus(subreq, status);
232 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
234 return tevent_req_simple_recv_ntstatus(req);
237 NTSTATUS cli_setpathinfo(struct cli_state *cli,
238 uint16_t level,
239 const char *path,
240 uint8_t *data,
241 size_t data_len)
243 TALLOC_CTX *frame = talloc_stackframe();
244 struct tevent_context *ev;
245 struct tevent_req *req;
246 NTSTATUS status = NT_STATUS_NO_MEMORY;
248 if (smbXcli_conn_has_async_calls(cli->conn)) {
250 * Can't use sync call while an async call is in flight
252 status = NT_STATUS_INVALID_PARAMETER;
253 goto fail;
255 ev = tevent_context_init(frame);
256 if (ev == NULL) {
257 goto fail;
259 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
260 if (req == NULL) {
261 goto fail;
263 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
264 goto fail;
266 status = cli_setpathinfo_recv(req);
267 fail:
268 TALLOC_FREE(frame);
269 return status;
272 /****************************************************************************
273 Hard/Symlink a file (UNIX extensions).
274 Creates new name (sym)linked to oldname.
275 ****************************************************************************/
277 struct cli_posix_link_internal_state {
278 uint8_t *data;
281 static void cli_posix_link_internal_done(struct tevent_req *subreq);
283 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
284 struct event_context *ev,
285 struct cli_state *cli,
286 uint16_t level,
287 const char *oldname,
288 const char *newname)
290 struct tevent_req *req = NULL, *subreq = NULL;
291 struct cli_posix_link_internal_state *state = NULL;
293 req = tevent_req_create(mem_ctx, &state,
294 struct cli_posix_link_internal_state);
295 if (req == NULL) {
296 return NULL;
299 /* Setup data array. */
300 state->data = talloc_array(state, uint8_t, 0);
301 if (tevent_req_nomem(state->data, req)) {
302 return tevent_req_post(req, ev);
304 state->data = trans2_bytes_push_str(
305 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
307 subreq = cli_setpathinfo_send(
308 state, ev, cli, level, newname,
309 state->data, talloc_get_size(state->data));
310 if (tevent_req_nomem(subreq, req)) {
311 return tevent_req_post(req, ev);
313 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
314 return req;
317 static void cli_posix_link_internal_done(struct tevent_req *subreq)
319 NTSTATUS status = cli_setpathinfo_recv(subreq);
320 tevent_req_simple_finish_ntstatus(subreq, status);
323 /****************************************************************************
324 Symlink a file (UNIX extensions).
325 ****************************************************************************/
327 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
328 struct event_context *ev,
329 struct cli_state *cli,
330 const char *oldname,
331 const char *newname)
333 return cli_posix_link_internal_send(
334 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
337 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
339 return tevent_req_simple_recv_ntstatus(req);
342 NTSTATUS cli_posix_symlink(struct cli_state *cli,
343 const char *oldname,
344 const char *newname)
346 TALLOC_CTX *frame = talloc_stackframe();
347 struct event_context *ev = NULL;
348 struct tevent_req *req = NULL;
349 NTSTATUS status = NT_STATUS_OK;
351 if (smbXcli_conn_has_async_calls(cli->conn)) {
353 * Can't use sync call while an async call is in flight
355 status = NT_STATUS_INVALID_PARAMETER;
356 goto fail;
359 ev = event_context_init(frame);
360 if (ev == NULL) {
361 status = NT_STATUS_NO_MEMORY;
362 goto fail;
365 req = cli_posix_symlink_send(frame,
367 cli,
368 oldname,
369 newname);
370 if (req == NULL) {
371 status = NT_STATUS_NO_MEMORY;
372 goto fail;
375 if (!tevent_req_poll(req, ev)) {
376 status = map_nt_error_from_unix(errno);
377 goto fail;
380 status = cli_posix_symlink_recv(req);
382 fail:
383 TALLOC_FREE(frame);
384 return status;
387 /****************************************************************************
388 Read a POSIX symlink.
389 ****************************************************************************/
391 struct readlink_state {
392 uint8_t *data;
393 uint32_t num_data;
396 static void cli_posix_readlink_done(struct tevent_req *subreq);
398 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
399 struct event_context *ev,
400 struct cli_state *cli,
401 const char *fname,
402 size_t len)
404 struct tevent_req *req = NULL, *subreq = NULL;
405 struct readlink_state *state = NULL;
406 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
408 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
409 if (req == NULL) {
410 return NULL;
414 * Len is in bytes, we need it in UCS2 units.
416 if ((2*len < len) || (maxbytelen < len)) {
417 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
418 return tevent_req_post(req, ev);
421 subreq = cli_qpathinfo_send(state, ev, cli, fname,
422 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
423 if (tevent_req_nomem(subreq, req)) {
424 return tevent_req_post(req, ev);
426 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
427 return req;
430 static void cli_posix_readlink_done(struct tevent_req *subreq)
432 struct tevent_req *req = tevent_req_callback_data(
433 subreq, struct tevent_req);
434 struct readlink_state *state = tevent_req_data(
435 req, struct readlink_state);
436 NTSTATUS status;
438 status = cli_qpathinfo_recv(subreq, state, &state->data,
439 &state->num_data);
440 TALLOC_FREE(subreq);
441 if (tevent_req_nterror(req, status)) {
442 return;
445 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
447 if (state->data[state->num_data-1] != '\0') {
448 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
449 return;
451 tevent_req_done(req);
454 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
455 char *retpath, size_t len)
457 NTSTATUS status;
458 char *converted = NULL;
459 size_t converted_size = 0;
460 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
462 if (tevent_req_is_nterror(req, &status)) {
463 return status;
465 /* The returned data is a pushed string, not raw data. */
466 if (!convert_string_talloc(state,
467 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
468 CH_UNIX,
469 state->data,
470 state->num_data,
471 &converted,
472 &converted_size)) {
473 return NT_STATUS_NO_MEMORY;
476 len = MIN(len,converted_size);
477 if (len == 0) {
478 return NT_STATUS_DATA_ERROR;
480 memcpy(retpath, converted, len);
481 return NT_STATUS_OK;
484 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
485 char *linkpath, size_t len)
487 TALLOC_CTX *frame = talloc_stackframe();
488 struct event_context *ev = NULL;
489 struct tevent_req *req = NULL;
490 NTSTATUS status = NT_STATUS_OK;
492 if (smbXcli_conn_has_async_calls(cli->conn)) {
494 * Can't use sync call while an async call is in flight
496 status = NT_STATUS_INVALID_PARAMETER;
497 goto fail;
500 ev = event_context_init(frame);
501 if (ev == NULL) {
502 status = NT_STATUS_NO_MEMORY;
503 goto fail;
506 req = cli_posix_readlink_send(frame,
508 cli,
509 fname,
510 len);
511 if (req == NULL) {
512 status = NT_STATUS_NO_MEMORY;
513 goto fail;
516 if (!tevent_req_poll(req, ev)) {
517 status = map_nt_error_from_unix(errno);
518 goto fail;
521 status = cli_posix_readlink_recv(req, cli, linkpath, len);
523 fail:
524 TALLOC_FREE(frame);
525 return status;
528 /****************************************************************************
529 Hard link a file (UNIX extensions).
530 ****************************************************************************/
532 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
533 struct event_context *ev,
534 struct cli_state *cli,
535 const char *oldname,
536 const char *newname)
538 return cli_posix_link_internal_send(
539 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
542 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
544 return tevent_req_simple_recv_ntstatus(req);
547 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
548 const char *oldname,
549 const char *newname)
551 TALLOC_CTX *frame = talloc_stackframe();
552 struct event_context *ev = NULL;
553 struct tevent_req *req = NULL;
554 NTSTATUS status = NT_STATUS_OK;
556 if (smbXcli_conn_has_async_calls(cli->conn)) {
558 * Can't use sync call while an async call is in flight
560 status = NT_STATUS_INVALID_PARAMETER;
561 goto fail;
564 ev = event_context_init(frame);
565 if (ev == NULL) {
566 status = NT_STATUS_NO_MEMORY;
567 goto fail;
570 req = cli_posix_hardlink_send(frame,
572 cli,
573 oldname,
574 newname);
575 if (req == NULL) {
576 status = NT_STATUS_NO_MEMORY;
577 goto fail;
580 if (!tevent_req_poll(req, ev)) {
581 status = map_nt_error_from_unix(errno);
582 goto fail;
585 status = cli_posix_hardlink_recv(req);
587 fail:
588 TALLOC_FREE(frame);
589 return status;
592 /****************************************************************************
593 Do a POSIX getfacl (UNIX extensions).
594 ****************************************************************************/
596 struct getfacl_state {
597 uint32_t num_data;
598 uint8_t *data;
601 static void cli_posix_getfacl_done(struct tevent_req *subreq);
603 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
604 struct event_context *ev,
605 struct cli_state *cli,
606 const char *fname)
608 struct tevent_req *req = NULL, *subreq = NULL;
609 struct getfacl_state *state = NULL;
611 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
612 if (req == NULL) {
613 return NULL;
615 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
616 0, CLI_BUFFER_SIZE);
617 if (tevent_req_nomem(subreq, req)) {
618 return tevent_req_post(req, ev);
620 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
621 return req;
624 static void cli_posix_getfacl_done(struct tevent_req *subreq)
626 struct tevent_req *req = tevent_req_callback_data(
627 subreq, struct tevent_req);
628 struct getfacl_state *state = tevent_req_data(
629 req, struct getfacl_state);
630 NTSTATUS status;
632 status = cli_qpathinfo_recv(subreq, state, &state->data,
633 &state->num_data);
634 TALLOC_FREE(subreq);
635 if (tevent_req_nterror(req, status)) {
636 return;
638 tevent_req_done(req);
641 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
642 TALLOC_CTX *mem_ctx,
643 size_t *prb_size,
644 char **retbuf)
646 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
647 NTSTATUS status;
649 if (tevent_req_is_nterror(req, &status)) {
650 return status;
652 *prb_size = (size_t)state->num_data;
653 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
654 return NT_STATUS_OK;
657 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
658 const char *fname,
659 TALLOC_CTX *mem_ctx,
660 size_t *prb_size,
661 char **retbuf)
663 TALLOC_CTX *frame = talloc_stackframe();
664 struct event_context *ev = NULL;
665 struct tevent_req *req = NULL;
666 NTSTATUS status = NT_STATUS_OK;
668 if (smbXcli_conn_has_async_calls(cli->conn)) {
670 * Can't use sync call while an async call is in flight
672 status = NT_STATUS_INVALID_PARAMETER;
673 goto fail;
676 ev = event_context_init(frame);
677 if (ev == NULL) {
678 status = NT_STATUS_NO_MEMORY;
679 goto fail;
682 req = cli_posix_getfacl_send(frame,
684 cli,
685 fname);
686 if (req == NULL) {
687 status = NT_STATUS_NO_MEMORY;
688 goto fail;
691 if (!tevent_req_poll(req, ev)) {
692 status = map_nt_error_from_unix(errno);
693 goto fail;
696 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
698 fail:
699 TALLOC_FREE(frame);
700 return status;
703 /****************************************************************************
704 Stat a file (UNIX extensions).
705 ****************************************************************************/
707 struct stat_state {
708 uint32_t num_data;
709 uint8_t *data;
712 static void cli_posix_stat_done(struct tevent_req *subreq);
714 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
715 struct event_context *ev,
716 struct cli_state *cli,
717 const char *fname)
719 struct tevent_req *req = NULL, *subreq = NULL;
720 struct stat_state *state = NULL;
722 req = tevent_req_create(mem_ctx, &state, struct stat_state);
723 if (req == NULL) {
724 return NULL;
726 subreq = cli_qpathinfo_send(state, ev, cli, fname,
727 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
728 if (tevent_req_nomem(subreq, req)) {
729 return tevent_req_post(req, ev);
731 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
732 return req;
735 static void cli_posix_stat_done(struct tevent_req *subreq)
737 struct tevent_req *req = tevent_req_callback_data(
738 subreq, struct tevent_req);
739 struct stat_state *state = tevent_req_data(req, struct stat_state);
740 NTSTATUS status;
742 status = cli_qpathinfo_recv(subreq, state, &state->data,
743 &state->num_data);
744 TALLOC_FREE(subreq);
745 if (tevent_req_nterror(req, status)) {
746 return;
748 tevent_req_done(req);
751 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
752 SMB_STRUCT_STAT *sbuf)
754 struct stat_state *state = tevent_req_data(req, struct stat_state);
755 NTSTATUS status;
757 if (tevent_req_is_nterror(req, &status)) {
758 return status;
761 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
762 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
763 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
764 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
765 #else
766 /* assume 512 byte blocks */
767 sbuf->st_ex_blocks /= 512;
768 #endif
769 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
770 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
771 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
773 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
774 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
775 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
776 #if defined(HAVE_MAKEDEV)
778 uint32_t dev_major = IVAL(state->data,60);
779 uint32_t dev_minor = IVAL(state->data,68);
780 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
782 #endif
783 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
784 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
785 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
787 return NT_STATUS_OK;
790 NTSTATUS cli_posix_stat(struct cli_state *cli,
791 const char *fname,
792 SMB_STRUCT_STAT *sbuf)
794 TALLOC_CTX *frame = talloc_stackframe();
795 struct event_context *ev = NULL;
796 struct tevent_req *req = NULL;
797 NTSTATUS status = NT_STATUS_OK;
799 if (smbXcli_conn_has_async_calls(cli->conn)) {
801 * Can't use sync call while an async call is in flight
803 status = NT_STATUS_INVALID_PARAMETER;
804 goto fail;
807 ev = event_context_init(frame);
808 if (ev == NULL) {
809 status = NT_STATUS_NO_MEMORY;
810 goto fail;
813 req = cli_posix_stat_send(frame,
815 cli,
816 fname);
817 if (req == NULL) {
818 status = NT_STATUS_NO_MEMORY;
819 goto fail;
822 if (!tevent_req_poll(req, ev)) {
823 status = map_nt_error_from_unix(errno);
824 goto fail;
827 status = cli_posix_stat_recv(req, sbuf);
829 fail:
830 TALLOC_FREE(frame);
831 return status;
834 /****************************************************************************
835 Chmod or chown a file internal (UNIX extensions).
836 ****************************************************************************/
838 struct cli_posix_chown_chmod_internal_state {
839 uint8_t data[100];
842 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
844 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
845 struct event_context *ev,
846 struct cli_state *cli,
847 const char *fname,
848 uint32_t mode,
849 uint32_t uid,
850 uint32_t gid)
852 struct tevent_req *req = NULL, *subreq = NULL;
853 struct cli_posix_chown_chmod_internal_state *state = NULL;
855 req = tevent_req_create(mem_ctx, &state,
856 struct cli_posix_chown_chmod_internal_state);
857 if (req == NULL) {
858 return NULL;
861 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
862 memset(&state->data[40], '\0', 60);
863 SIVAL(state->data,40,uid);
864 SIVAL(state->data,48,gid);
865 SIVAL(state->data,84,mode);
867 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
868 fname, state->data, sizeof(state->data));
869 if (tevent_req_nomem(subreq, req)) {
870 return tevent_req_post(req, ev);
872 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
873 req);
874 return req;
877 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
879 NTSTATUS status = cli_setpathinfo_recv(subreq);
880 tevent_req_simple_finish_ntstatus(subreq, status);
883 /****************************************************************************
884 chmod a file (UNIX extensions).
885 ****************************************************************************/
887 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
888 struct event_context *ev,
889 struct cli_state *cli,
890 const char *fname,
891 mode_t mode)
893 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
894 fname,
895 unix_perms_to_wire(mode),
896 SMB_UID_NO_CHANGE,
897 SMB_GID_NO_CHANGE);
900 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
902 return tevent_req_simple_recv_ntstatus(req);
905 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
907 TALLOC_CTX *frame = talloc_stackframe();
908 struct event_context *ev = NULL;
909 struct tevent_req *req = NULL;
910 NTSTATUS status = NT_STATUS_OK;
912 if (smbXcli_conn_has_async_calls(cli->conn)) {
914 * Can't use sync call while an async call is in flight
916 status = NT_STATUS_INVALID_PARAMETER;
917 goto fail;
920 ev = event_context_init(frame);
921 if (ev == NULL) {
922 status = NT_STATUS_NO_MEMORY;
923 goto fail;
926 req = cli_posix_chmod_send(frame,
928 cli,
929 fname,
930 mode);
931 if (req == NULL) {
932 status = NT_STATUS_NO_MEMORY;
933 goto fail;
936 if (!tevent_req_poll(req, ev)) {
937 status = map_nt_error_from_unix(errno);
938 goto fail;
941 status = cli_posix_chmod_recv(req);
943 fail:
944 TALLOC_FREE(frame);
945 return status;
948 /****************************************************************************
949 chown a file (UNIX extensions).
950 ****************************************************************************/
952 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
953 struct event_context *ev,
954 struct cli_state *cli,
955 const char *fname,
956 uid_t uid,
957 gid_t gid)
959 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
960 fname,
961 SMB_MODE_NO_CHANGE,
962 (uint32_t)uid,
963 (uint32_t)gid);
966 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
968 return tevent_req_simple_recv_ntstatus(req);
971 NTSTATUS cli_posix_chown(struct cli_state *cli,
972 const char *fname,
973 uid_t uid,
974 gid_t gid)
976 TALLOC_CTX *frame = talloc_stackframe();
977 struct event_context *ev = NULL;
978 struct tevent_req *req = NULL;
979 NTSTATUS status = NT_STATUS_OK;
981 if (smbXcli_conn_has_async_calls(cli->conn)) {
983 * Can't use sync call while an async call is in flight
985 status = NT_STATUS_INVALID_PARAMETER;
986 goto fail;
989 ev = event_context_init(frame);
990 if (ev == NULL) {
991 status = NT_STATUS_NO_MEMORY;
992 goto fail;
995 req = cli_posix_chown_send(frame,
997 cli,
998 fname,
999 uid,
1000 gid);
1001 if (req == NULL) {
1002 status = NT_STATUS_NO_MEMORY;
1003 goto fail;
1006 if (!tevent_req_poll(req, ev)) {
1007 status = map_nt_error_from_unix(errno);
1008 goto fail;
1011 status = cli_posix_chown_recv(req);
1013 fail:
1014 TALLOC_FREE(frame);
1015 return status;
1018 /****************************************************************************
1019 Rename a file.
1020 ****************************************************************************/
1022 static void cli_rename_done(struct tevent_req *subreq);
1024 struct cli_rename_state {
1025 uint16_t vwv[1];
1028 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1029 struct event_context *ev,
1030 struct cli_state *cli,
1031 const char *fname_src,
1032 const char *fname_dst)
1034 struct tevent_req *req = NULL, *subreq = NULL;
1035 struct cli_rename_state *state = NULL;
1036 uint8_t additional_flags = 0;
1037 uint8_t *bytes = NULL;
1039 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1040 if (req == NULL) {
1041 return NULL;
1044 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1046 bytes = talloc_array(state, uint8_t, 1);
1047 if (tevent_req_nomem(bytes, req)) {
1048 return tevent_req_post(req, ev);
1050 bytes[0] = 4;
1051 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1052 strlen(fname_src)+1, NULL);
1053 if (tevent_req_nomem(bytes, req)) {
1054 return tevent_req_post(req, ev);
1057 bytes = talloc_realloc(state, bytes, uint8_t,
1058 talloc_get_size(bytes)+1);
1059 if (tevent_req_nomem(bytes, req)) {
1060 return tevent_req_post(req, ev);
1063 bytes[talloc_get_size(bytes)-1] = 4;
1064 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1065 strlen(fname_dst)+1, NULL);
1066 if (tevent_req_nomem(bytes, req)) {
1067 return tevent_req_post(req, ev);
1070 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1071 1, state->vwv, talloc_get_size(bytes), bytes);
1072 if (tevent_req_nomem(subreq, req)) {
1073 return tevent_req_post(req, ev);
1075 tevent_req_set_callback(subreq, cli_rename_done, req);
1076 return req;
1079 static void cli_rename_done(struct tevent_req *subreq)
1081 struct tevent_req *req = tevent_req_callback_data(
1082 subreq, struct tevent_req);
1083 NTSTATUS status;
1085 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1086 TALLOC_FREE(subreq);
1087 if (tevent_req_nterror(req, status)) {
1088 return;
1090 tevent_req_done(req);
1093 NTSTATUS cli_rename_recv(struct tevent_req *req)
1095 return tevent_req_simple_recv_ntstatus(req);
1098 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1100 TALLOC_CTX *frame = talloc_stackframe();
1101 struct event_context *ev;
1102 struct tevent_req *req;
1103 NTSTATUS status = NT_STATUS_OK;
1105 if (smbXcli_conn_has_async_calls(cli->conn)) {
1107 * Can't use sync call while an async call is in flight
1109 status = NT_STATUS_INVALID_PARAMETER;
1110 goto fail;
1113 ev = event_context_init(frame);
1114 if (ev == NULL) {
1115 status = NT_STATUS_NO_MEMORY;
1116 goto fail;
1119 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1120 if (req == NULL) {
1121 status = NT_STATUS_NO_MEMORY;
1122 goto fail;
1125 if (!tevent_req_poll(req, ev)) {
1126 status = map_nt_error_from_unix(errno);
1127 goto fail;
1130 status = cli_rename_recv(req);
1132 fail:
1133 TALLOC_FREE(frame);
1134 return status;
1137 /****************************************************************************
1138 NT Rename a file.
1139 ****************************************************************************/
1141 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1143 struct cli_ntrename_internal_state {
1144 uint16_t vwv[4];
1147 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1148 struct event_context *ev,
1149 struct cli_state *cli,
1150 const char *fname_src,
1151 const char *fname_dst,
1152 uint16_t rename_flag)
1154 struct tevent_req *req = NULL, *subreq = NULL;
1155 struct cli_ntrename_internal_state *state = NULL;
1156 uint8_t additional_flags = 0;
1157 uint8_t *bytes = NULL;
1159 req = tevent_req_create(mem_ctx, &state,
1160 struct cli_ntrename_internal_state);
1161 if (req == NULL) {
1162 return NULL;
1165 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1166 SSVAL(state->vwv+1, 0, rename_flag);
1168 bytes = talloc_array(state, uint8_t, 1);
1169 if (tevent_req_nomem(bytes, req)) {
1170 return tevent_req_post(req, ev);
1172 bytes[0] = 4;
1173 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1174 strlen(fname_src)+1, NULL);
1175 if (tevent_req_nomem(bytes, req)) {
1176 return tevent_req_post(req, ev);
1179 bytes = talloc_realloc(state, bytes, uint8_t,
1180 talloc_get_size(bytes)+1);
1181 if (tevent_req_nomem(bytes, req)) {
1182 return tevent_req_post(req, ev);
1185 bytes[talloc_get_size(bytes)-1] = 4;
1186 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1187 strlen(fname_dst)+1, NULL);
1188 if (tevent_req_nomem(bytes, req)) {
1189 return tevent_req_post(req, ev);
1192 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1193 4, state->vwv, talloc_get_size(bytes), bytes);
1194 if (tevent_req_nomem(subreq, req)) {
1195 return tevent_req_post(req, ev);
1197 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1198 return req;
1201 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1203 struct tevent_req *req = tevent_req_callback_data(
1204 subreq, struct tevent_req);
1205 NTSTATUS status;
1207 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1208 TALLOC_FREE(subreq);
1209 if (tevent_req_nterror(req, status)) {
1210 return;
1212 tevent_req_done(req);
1215 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1217 return tevent_req_simple_recv_ntstatus(req);
1220 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1221 struct event_context *ev,
1222 struct cli_state *cli,
1223 const char *fname_src,
1224 const char *fname_dst)
1226 return cli_ntrename_internal_send(mem_ctx,
1228 cli,
1229 fname_src,
1230 fname_dst,
1231 RENAME_FLAG_RENAME);
1234 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1236 return cli_ntrename_internal_recv(req);
1239 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1241 TALLOC_CTX *frame = talloc_stackframe();
1242 struct event_context *ev;
1243 struct tevent_req *req;
1244 NTSTATUS status = NT_STATUS_OK;
1246 if (smbXcli_conn_has_async_calls(cli->conn)) {
1248 * Can't use sync call while an async call is in flight
1250 status = NT_STATUS_INVALID_PARAMETER;
1251 goto fail;
1254 ev = event_context_init(frame);
1255 if (ev == NULL) {
1256 status = NT_STATUS_NO_MEMORY;
1257 goto fail;
1260 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1261 if (req == NULL) {
1262 status = NT_STATUS_NO_MEMORY;
1263 goto fail;
1266 if (!tevent_req_poll(req, ev)) {
1267 status = map_nt_error_from_unix(errno);
1268 goto fail;
1271 status = cli_ntrename_recv(req);
1273 fail:
1274 TALLOC_FREE(frame);
1275 return status;
1278 /****************************************************************************
1279 NT hardlink a file.
1280 ****************************************************************************/
1282 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1283 struct event_context *ev,
1284 struct cli_state *cli,
1285 const char *fname_src,
1286 const char *fname_dst)
1288 return cli_ntrename_internal_send(mem_ctx,
1290 cli,
1291 fname_src,
1292 fname_dst,
1293 RENAME_FLAG_HARD_LINK);
1296 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1298 return cli_ntrename_internal_recv(req);
1301 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1303 TALLOC_CTX *frame = talloc_stackframe();
1304 struct event_context *ev;
1305 struct tevent_req *req;
1306 NTSTATUS status = NT_STATUS_OK;
1308 if (smbXcli_conn_has_async_calls(cli->conn)) {
1310 * Can't use sync call while an async call is in flight
1312 status = NT_STATUS_INVALID_PARAMETER;
1313 goto fail;
1316 ev = event_context_init(frame);
1317 if (ev == NULL) {
1318 status = NT_STATUS_NO_MEMORY;
1319 goto fail;
1322 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1323 if (req == NULL) {
1324 status = NT_STATUS_NO_MEMORY;
1325 goto fail;
1328 if (!tevent_req_poll(req, ev)) {
1329 status = map_nt_error_from_unix(errno);
1330 goto fail;
1333 status = cli_nt_hardlink_recv(req);
1335 fail:
1336 TALLOC_FREE(frame);
1337 return status;
1340 /****************************************************************************
1341 Delete a file.
1342 ****************************************************************************/
1344 static void cli_unlink_done(struct tevent_req *subreq);
1346 struct cli_unlink_state {
1347 uint16_t vwv[1];
1350 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1351 struct event_context *ev,
1352 struct cli_state *cli,
1353 const char *fname,
1354 uint16_t mayhave_attrs)
1356 struct tevent_req *req = NULL, *subreq = NULL;
1357 struct cli_unlink_state *state = NULL;
1358 uint8_t additional_flags = 0;
1359 uint8_t *bytes = NULL;
1361 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1362 if (req == NULL) {
1363 return NULL;
1366 SSVAL(state->vwv+0, 0, mayhave_attrs);
1368 bytes = talloc_array(state, uint8_t, 1);
1369 if (tevent_req_nomem(bytes, req)) {
1370 return tevent_req_post(req, ev);
1372 bytes[0] = 4;
1373 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1374 strlen(fname)+1, NULL);
1376 if (tevent_req_nomem(bytes, req)) {
1377 return tevent_req_post(req, ev);
1380 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1381 1, state->vwv, talloc_get_size(bytes), bytes);
1382 if (tevent_req_nomem(subreq, req)) {
1383 return tevent_req_post(req, ev);
1385 tevent_req_set_callback(subreq, cli_unlink_done, req);
1386 return req;
1389 static void cli_unlink_done(struct tevent_req *subreq)
1391 struct tevent_req *req = tevent_req_callback_data(
1392 subreq, struct tevent_req);
1393 NTSTATUS status;
1395 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1396 TALLOC_FREE(subreq);
1397 if (tevent_req_nterror(req, status)) {
1398 return;
1400 tevent_req_done(req);
1403 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1405 return tevent_req_simple_recv_ntstatus(req);
1408 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1410 TALLOC_CTX *frame = talloc_stackframe();
1411 struct event_context *ev;
1412 struct tevent_req *req;
1413 NTSTATUS status = NT_STATUS_OK;
1415 if (smbXcli_conn_has_async_calls(cli->conn)) {
1417 * Can't use sync call while an async call is in flight
1419 status = NT_STATUS_INVALID_PARAMETER;
1420 goto fail;
1423 ev = event_context_init(frame);
1424 if (ev == NULL) {
1425 status = NT_STATUS_NO_MEMORY;
1426 goto fail;
1429 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1430 if (req == NULL) {
1431 status = NT_STATUS_NO_MEMORY;
1432 goto fail;
1435 if (!tevent_req_poll(req, ev)) {
1436 status = map_nt_error_from_unix(errno);
1437 goto fail;
1440 status = cli_unlink_recv(req);
1442 fail:
1443 TALLOC_FREE(frame);
1444 return status;
1447 /****************************************************************************
1448 Create a directory.
1449 ****************************************************************************/
1451 static void cli_mkdir_done(struct tevent_req *subreq);
1453 struct cli_mkdir_state {
1454 int dummy;
1457 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1458 struct event_context *ev,
1459 struct cli_state *cli,
1460 const char *dname)
1462 struct tevent_req *req = NULL, *subreq = NULL;
1463 struct cli_mkdir_state *state = NULL;
1464 uint8_t additional_flags = 0;
1465 uint8_t *bytes = NULL;
1467 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1468 if (req == NULL) {
1469 return NULL;
1472 bytes = talloc_array(state, uint8_t, 1);
1473 if (tevent_req_nomem(bytes, req)) {
1474 return tevent_req_post(req, ev);
1476 bytes[0] = 4;
1477 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1478 strlen(dname)+1, NULL);
1480 if (tevent_req_nomem(bytes, req)) {
1481 return tevent_req_post(req, ev);
1484 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1485 0, NULL, talloc_get_size(bytes), bytes);
1486 if (tevent_req_nomem(subreq, req)) {
1487 return tevent_req_post(req, ev);
1489 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1490 return req;
1493 static void cli_mkdir_done(struct tevent_req *subreq)
1495 struct tevent_req *req = tevent_req_callback_data(
1496 subreq, struct tevent_req);
1497 NTSTATUS status;
1499 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1500 TALLOC_FREE(subreq);
1501 if (tevent_req_nterror(req, status)) {
1502 return;
1504 tevent_req_done(req);
1507 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1509 return tevent_req_simple_recv_ntstatus(req);
1512 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1514 TALLOC_CTX *frame = talloc_stackframe();
1515 struct event_context *ev;
1516 struct tevent_req *req;
1517 NTSTATUS status = NT_STATUS_OK;
1519 if (smbXcli_conn_has_async_calls(cli->conn)) {
1521 * Can't use sync call while an async call is in flight
1523 status = NT_STATUS_INVALID_PARAMETER;
1524 goto fail;
1527 ev = event_context_init(frame);
1528 if (ev == NULL) {
1529 status = NT_STATUS_NO_MEMORY;
1530 goto fail;
1533 req = cli_mkdir_send(frame, ev, cli, dname);
1534 if (req == NULL) {
1535 status = NT_STATUS_NO_MEMORY;
1536 goto fail;
1539 if (!tevent_req_poll(req, ev)) {
1540 status = map_nt_error_from_unix(errno);
1541 goto fail;
1544 status = cli_mkdir_recv(req);
1546 fail:
1547 TALLOC_FREE(frame);
1548 return status;
1551 /****************************************************************************
1552 Remove a directory.
1553 ****************************************************************************/
1555 static void cli_rmdir_done(struct tevent_req *subreq);
1557 struct cli_rmdir_state {
1558 int dummy;
1561 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1562 struct event_context *ev,
1563 struct cli_state *cli,
1564 const char *dname)
1566 struct tevent_req *req = NULL, *subreq = NULL;
1567 struct cli_rmdir_state *state = NULL;
1568 uint8_t additional_flags = 0;
1569 uint8_t *bytes = NULL;
1571 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1572 if (req == NULL) {
1573 return NULL;
1576 bytes = talloc_array(state, uint8_t, 1);
1577 if (tevent_req_nomem(bytes, req)) {
1578 return tevent_req_post(req, ev);
1580 bytes[0] = 4;
1581 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1582 strlen(dname)+1, NULL);
1584 if (tevent_req_nomem(bytes, req)) {
1585 return tevent_req_post(req, ev);
1588 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1589 0, NULL, talloc_get_size(bytes), bytes);
1590 if (tevent_req_nomem(subreq, req)) {
1591 return tevent_req_post(req, ev);
1593 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1594 return req;
1597 static void cli_rmdir_done(struct tevent_req *subreq)
1599 struct tevent_req *req = tevent_req_callback_data(
1600 subreq, struct tevent_req);
1601 NTSTATUS status;
1603 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1604 TALLOC_FREE(subreq);
1605 if (tevent_req_nterror(req, status)) {
1606 return;
1608 tevent_req_done(req);
1611 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1613 return tevent_req_simple_recv_ntstatus(req);
1616 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1618 TALLOC_CTX *frame = talloc_stackframe();
1619 struct event_context *ev;
1620 struct tevent_req *req;
1621 NTSTATUS status = NT_STATUS_OK;
1623 if (smbXcli_conn_has_async_calls(cli->conn)) {
1625 * Can't use sync call while an async call is in flight
1627 status = NT_STATUS_INVALID_PARAMETER;
1628 goto fail;
1631 ev = event_context_init(frame);
1632 if (ev == NULL) {
1633 status = NT_STATUS_NO_MEMORY;
1634 goto fail;
1637 req = cli_rmdir_send(frame, ev, cli, dname);
1638 if (req == NULL) {
1639 status = NT_STATUS_NO_MEMORY;
1640 goto fail;
1643 if (!tevent_req_poll(req, ev)) {
1644 status = map_nt_error_from_unix(errno);
1645 goto fail;
1648 status = cli_rmdir_recv(req);
1650 fail:
1651 TALLOC_FREE(frame);
1652 return status;
1655 /****************************************************************************
1656 Set or clear the delete on close flag.
1657 ****************************************************************************/
1659 struct doc_state {
1660 uint16_t setup;
1661 uint8_t param[6];
1662 uint8_t data[1];
1665 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1667 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1668 NULL, 0, NULL, NULL, 0, NULL);
1669 tevent_req_simple_finish_ntstatus(subreq, status);
1672 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1673 struct event_context *ev,
1674 struct cli_state *cli,
1675 uint16_t fnum,
1676 bool flag)
1678 struct tevent_req *req = NULL, *subreq = NULL;
1679 struct doc_state *state = NULL;
1681 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1682 if (req == NULL) {
1683 return NULL;
1686 /* Setup setup word. */
1687 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1689 /* Setup param array. */
1690 SSVAL(state->param,0,fnum);
1691 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1693 /* Setup data array. */
1694 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1696 subreq = cli_trans_send(state, /* mem ctx. */
1697 ev, /* event ctx. */
1698 cli, /* cli_state. */
1699 SMBtrans2, /* cmd. */
1700 NULL, /* pipe name. */
1701 -1, /* fid. */
1702 0, /* function. */
1703 0, /* flags. */
1704 &state->setup, /* setup. */
1705 1, /* num setup uint16_t words. */
1706 0, /* max returned setup. */
1707 state->param, /* param. */
1708 6, /* num param. */
1709 2, /* max returned param. */
1710 state->data, /* data. */
1711 1, /* num data. */
1712 0); /* max returned data. */
1714 if (tevent_req_nomem(subreq, req)) {
1715 return tevent_req_post(req, ev);
1717 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1718 return req;
1721 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1723 return tevent_req_simple_recv_ntstatus(req);
1726 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1728 TALLOC_CTX *frame = talloc_stackframe();
1729 struct event_context *ev = NULL;
1730 struct tevent_req *req = NULL;
1731 NTSTATUS status = NT_STATUS_OK;
1733 if (smbXcli_conn_has_async_calls(cli->conn)) {
1735 * Can't use sync call while an async call is in flight
1737 status = NT_STATUS_INVALID_PARAMETER;
1738 goto fail;
1741 ev = event_context_init(frame);
1742 if (ev == NULL) {
1743 status = NT_STATUS_NO_MEMORY;
1744 goto fail;
1747 req = cli_nt_delete_on_close_send(frame,
1749 cli,
1750 fnum,
1751 flag);
1752 if (req == NULL) {
1753 status = NT_STATUS_NO_MEMORY;
1754 goto fail;
1757 if (!tevent_req_poll(req, ev)) {
1758 status = map_nt_error_from_unix(errno);
1759 goto fail;
1762 status = cli_nt_delete_on_close_recv(req);
1764 fail:
1765 TALLOC_FREE(frame);
1766 return status;
1769 struct cli_ntcreate_state {
1770 uint16_t vwv[24];
1771 uint16_t fnum;
1774 static void cli_ntcreate_done(struct tevent_req *subreq);
1776 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1777 struct event_context *ev,
1778 struct cli_state *cli,
1779 const char *fname,
1780 uint32_t CreatFlags,
1781 uint32_t DesiredAccess,
1782 uint32_t FileAttributes,
1783 uint32_t ShareAccess,
1784 uint32_t CreateDisposition,
1785 uint32_t CreateOptions,
1786 uint8_t SecurityFlags)
1788 struct tevent_req *req, *subreq;
1789 struct cli_ntcreate_state *state;
1790 uint16_t *vwv;
1791 uint8_t *bytes;
1792 size_t converted_len;
1794 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1795 if (req == NULL) {
1796 return NULL;
1799 vwv = state->vwv;
1801 SCVAL(vwv+0, 0, 0xFF);
1802 SCVAL(vwv+0, 1, 0);
1803 SSVAL(vwv+1, 0, 0);
1804 SCVAL(vwv+2, 0, 0);
1806 if (cli->use_oplocks) {
1807 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1809 SIVAL(vwv+3, 1, CreatFlags);
1810 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1811 SIVAL(vwv+7, 1, DesiredAccess);
1812 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1813 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1814 SIVAL(vwv+13, 1, FileAttributes);
1815 SIVAL(vwv+15, 1, ShareAccess);
1816 SIVAL(vwv+17, 1, CreateDisposition);
1817 SIVAL(vwv+19, 1, CreateOptions |
1818 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1819 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1820 SCVAL(vwv+23, 1, SecurityFlags);
1822 bytes = talloc_array(state, uint8_t, 0);
1823 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1824 fname, strlen(fname)+1,
1825 &converted_len);
1827 /* sigh. this copes with broken netapp filer behaviour */
1828 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1830 if (tevent_req_nomem(bytes, req)) {
1831 return tevent_req_post(req, ev);
1834 SSVAL(vwv+2, 1, converted_len);
1836 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1837 talloc_get_size(bytes), bytes);
1838 if (tevent_req_nomem(subreq, req)) {
1839 return tevent_req_post(req, ev);
1841 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1842 return req;
1845 static void cli_ntcreate_done(struct tevent_req *subreq)
1847 struct tevent_req *req = tevent_req_callback_data(
1848 subreq, struct tevent_req);
1849 struct cli_ntcreate_state *state = tevent_req_data(
1850 req, struct cli_ntcreate_state);
1851 uint8_t wct;
1852 uint16_t *vwv;
1853 uint32_t num_bytes;
1854 uint8_t *bytes;
1855 NTSTATUS status;
1857 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv,
1858 &num_bytes, &bytes);
1859 TALLOC_FREE(subreq);
1860 if (tevent_req_nterror(req, status)) {
1861 return;
1863 state->fnum = SVAL(vwv+2, 1);
1864 tevent_req_done(req);
1867 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1869 struct cli_ntcreate_state *state = tevent_req_data(
1870 req, struct cli_ntcreate_state);
1871 NTSTATUS status;
1873 if (tevent_req_is_nterror(req, &status)) {
1874 return status;
1876 *pfnum = state->fnum;
1877 return NT_STATUS_OK;
1880 NTSTATUS cli_ntcreate(struct cli_state *cli,
1881 const char *fname,
1882 uint32_t CreatFlags,
1883 uint32_t DesiredAccess,
1884 uint32_t FileAttributes,
1885 uint32_t ShareAccess,
1886 uint32_t CreateDisposition,
1887 uint32_t CreateOptions,
1888 uint8_t SecurityFlags,
1889 uint16_t *pfid)
1891 TALLOC_CTX *frame = talloc_stackframe();
1892 struct event_context *ev;
1893 struct tevent_req *req;
1894 NTSTATUS status = NT_STATUS_OK;
1896 if (smbXcli_conn_has_async_calls(cli->conn)) {
1898 * Can't use sync call while an async call is in flight
1900 status = NT_STATUS_INVALID_PARAMETER;
1901 goto fail;
1904 ev = event_context_init(frame);
1905 if (ev == NULL) {
1906 status = NT_STATUS_NO_MEMORY;
1907 goto fail;
1910 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1911 DesiredAccess, FileAttributes, ShareAccess,
1912 CreateDisposition, CreateOptions,
1913 SecurityFlags);
1914 if (req == NULL) {
1915 status = NT_STATUS_NO_MEMORY;
1916 goto fail;
1919 if (!tevent_req_poll(req, ev)) {
1920 status = map_nt_error_from_unix(errno);
1921 goto fail;
1924 status = cli_ntcreate_recv(req, pfid);
1925 fail:
1926 TALLOC_FREE(frame);
1927 return status;
1930 struct cli_nttrans_create_state {
1931 uint16_t fnum;
1934 static void cli_nttrans_create_done(struct tevent_req *subreq);
1936 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
1937 struct event_context *ev,
1938 struct cli_state *cli,
1939 const char *fname,
1940 uint32_t CreatFlags,
1941 uint32_t DesiredAccess,
1942 uint32_t FileAttributes,
1943 uint32_t ShareAccess,
1944 uint32_t CreateDisposition,
1945 uint32_t CreateOptions,
1946 uint8_t SecurityFlags,
1947 struct security_descriptor *secdesc,
1948 struct ea_struct *eas,
1949 int num_eas)
1951 struct tevent_req *req, *subreq;
1952 struct cli_nttrans_create_state *state;
1953 uint8_t *param;
1954 uint8_t *secdesc_buf;
1955 size_t secdesc_len;
1956 NTSTATUS status;
1957 size_t converted_len;
1959 req = tevent_req_create(mem_ctx,
1960 &state, struct cli_nttrans_create_state);
1961 if (req == NULL) {
1962 return NULL;
1965 if (secdesc != NULL) {
1966 status = marshall_sec_desc(talloc_tos(), secdesc,
1967 &secdesc_buf, &secdesc_len);
1968 if (tevent_req_nterror(req, status)) {
1969 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1970 nt_errstr(status)));
1971 return tevent_req_post(req, ev);
1973 } else {
1974 secdesc_buf = NULL;
1975 secdesc_len = 0;
1978 if (num_eas != 0) {
1980 * TODO ;-)
1982 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
1983 return tevent_req_post(req, ev);
1986 param = talloc_array(state, uint8_t, 53);
1987 if (tevent_req_nomem(param, req)) {
1988 return tevent_req_post(req, ev);
1991 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
1992 fname, strlen(fname),
1993 &converted_len);
1994 if (tevent_req_nomem(param, req)) {
1995 return tevent_req_post(req, ev);
1998 SIVAL(param, 0, CreatFlags);
1999 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2000 SIVAL(param, 8, DesiredAccess);
2001 SIVAL(param, 12, 0x0); /* AllocationSize */
2002 SIVAL(param, 16, 0x0); /* AllocationSize */
2003 SIVAL(param, 20, FileAttributes);
2004 SIVAL(param, 24, ShareAccess);
2005 SIVAL(param, 28, CreateDisposition);
2006 SIVAL(param, 32, CreateOptions |
2007 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2008 SIVAL(param, 36, secdesc_len);
2009 SIVAL(param, 40, 0); /* EA length*/
2010 SIVAL(param, 44, converted_len);
2011 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2012 SCVAL(param, 52, SecurityFlags);
2014 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2015 NULL, -1, /* name, fid */
2016 NT_TRANSACT_CREATE, 0,
2017 NULL, 0, 0, /* setup */
2018 param, talloc_get_size(param), 128, /* param */
2019 secdesc_buf, secdesc_len, 0); /* data */
2020 if (tevent_req_nomem(subreq, req)) {
2021 return tevent_req_post(req, ev);
2023 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2024 return req;
2027 static void cli_nttrans_create_done(struct tevent_req *subreq)
2029 struct tevent_req *req = tevent_req_callback_data(
2030 subreq, struct tevent_req);
2031 struct cli_nttrans_create_state *state = tevent_req_data(
2032 req, struct cli_nttrans_create_state);
2033 uint8_t *param;
2034 uint32_t num_param;
2035 NTSTATUS status;
2037 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2038 NULL, 0, NULL, /* rsetup */
2039 &param, 69, &num_param,
2040 NULL, 0, NULL);
2041 if (tevent_req_nterror(req, status)) {
2042 return;
2044 state->fnum = SVAL(param, 2);
2045 TALLOC_FREE(param);
2046 tevent_req_done(req);
2049 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
2051 struct cli_nttrans_create_state *state = tevent_req_data(
2052 req, struct cli_nttrans_create_state);
2053 NTSTATUS status;
2055 if (tevent_req_is_nterror(req, &status)) {
2056 return status;
2058 *fnum = state->fnum;
2059 return NT_STATUS_OK;
2062 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2063 const char *fname,
2064 uint32_t CreatFlags,
2065 uint32_t DesiredAccess,
2066 uint32_t FileAttributes,
2067 uint32_t ShareAccess,
2068 uint32_t CreateDisposition,
2069 uint32_t CreateOptions,
2070 uint8_t SecurityFlags,
2071 struct security_descriptor *secdesc,
2072 struct ea_struct *eas,
2073 int num_eas,
2074 uint16_t *pfid)
2076 TALLOC_CTX *frame = talloc_stackframe();
2077 struct event_context *ev;
2078 struct tevent_req *req;
2079 NTSTATUS status = NT_STATUS_NO_MEMORY;
2081 if (smbXcli_conn_has_async_calls(cli->conn)) {
2083 * Can't use sync call while an async call is in flight
2085 status = NT_STATUS_INVALID_PARAMETER;
2086 goto fail;
2088 ev = event_context_init(frame);
2089 if (ev == NULL) {
2090 goto fail;
2092 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2093 DesiredAccess, FileAttributes,
2094 ShareAccess, CreateDisposition,
2095 CreateOptions, SecurityFlags,
2096 secdesc, eas, num_eas);
2097 if (req == NULL) {
2098 goto fail;
2100 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2101 goto fail;
2103 status = cli_nttrans_create_recv(req, pfid);
2104 fail:
2105 TALLOC_FREE(frame);
2106 return status;
2109 /****************************************************************************
2110 Open a file
2111 WARNING: if you open with O_WRONLY then getattrE won't work!
2112 ****************************************************************************/
2114 struct cli_openx_state {
2115 const char *fname;
2116 uint16_t vwv[15];
2117 uint16_t fnum;
2118 struct iovec bytes;
2121 static void cli_openx_done(struct tevent_req *subreq);
2123 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2124 struct event_context *ev,
2125 struct cli_state *cli, const char *fname,
2126 int flags, int share_mode,
2127 struct tevent_req **psmbreq)
2129 struct tevent_req *req, *subreq;
2130 struct cli_openx_state *state;
2131 unsigned openfn;
2132 unsigned accessmode;
2133 uint8_t additional_flags;
2134 uint8_t *bytes;
2136 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2137 if (req == NULL) {
2138 return NULL;
2141 openfn = 0;
2142 if (flags & O_CREAT) {
2143 openfn |= (1<<4);
2145 if (!(flags & O_EXCL)) {
2146 if (flags & O_TRUNC)
2147 openfn |= (1<<1);
2148 else
2149 openfn |= (1<<0);
2152 accessmode = (share_mode<<4);
2154 if ((flags & O_ACCMODE) == O_RDWR) {
2155 accessmode |= 2;
2156 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2157 accessmode |= 1;
2160 #if defined(O_SYNC)
2161 if ((flags & O_SYNC) == O_SYNC) {
2162 accessmode |= (1<<14);
2164 #endif /* O_SYNC */
2166 if (share_mode == DENY_FCB) {
2167 accessmode = 0xFF;
2170 SCVAL(state->vwv + 0, 0, 0xFF);
2171 SCVAL(state->vwv + 0, 1, 0);
2172 SSVAL(state->vwv + 1, 0, 0);
2173 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2174 SSVAL(state->vwv + 3, 0, accessmode);
2175 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2176 SSVAL(state->vwv + 5, 0, 0);
2177 SIVAL(state->vwv + 6, 0, 0);
2178 SSVAL(state->vwv + 8, 0, openfn);
2179 SIVAL(state->vwv + 9, 0, 0);
2180 SIVAL(state->vwv + 11, 0, 0);
2181 SIVAL(state->vwv + 13, 0, 0);
2183 additional_flags = 0;
2185 if (cli->use_oplocks) {
2186 /* if using oplocks then ask for a batch oplock via
2187 core and extended methods */
2188 additional_flags =
2189 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2190 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2193 bytes = talloc_array(state, uint8_t, 0);
2194 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2195 strlen(fname)+1, NULL);
2197 if (tevent_req_nomem(bytes, req)) {
2198 return tevent_req_post(req, ev);
2201 state->bytes.iov_base = (void *)bytes;
2202 state->bytes.iov_len = talloc_get_size(bytes);
2204 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2205 15, state->vwv, 1, &state->bytes);
2206 if (subreq == NULL) {
2207 TALLOC_FREE(req);
2208 return NULL;
2210 tevent_req_set_callback(subreq, cli_openx_done, req);
2211 *psmbreq = subreq;
2212 return req;
2215 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2216 struct cli_state *cli, const char *fname,
2217 int flags, int share_mode)
2219 struct tevent_req *req, *subreq;
2220 NTSTATUS status;
2222 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2223 &subreq);
2224 if (req == NULL) {
2225 return NULL;
2228 status = smb1cli_req_chain_submit(&subreq, 1);
2229 if (tevent_req_nterror(req, status)) {
2230 return tevent_req_post(req, ev);
2232 return req;
2235 static void cli_openx_done(struct tevent_req *subreq)
2237 struct tevent_req *req = tevent_req_callback_data(
2238 subreq, struct tevent_req);
2239 struct cli_openx_state *state = tevent_req_data(
2240 req, struct cli_openx_state);
2241 uint8_t wct;
2242 uint16_t *vwv;
2243 NTSTATUS status;
2245 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2246 NULL);
2247 TALLOC_FREE(subreq);
2248 if (tevent_req_nterror(req, status)) {
2249 return;
2251 state->fnum = SVAL(vwv+2, 0);
2252 tevent_req_done(req);
2255 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2257 struct cli_openx_state *state = tevent_req_data(
2258 req, struct cli_openx_state);
2259 NTSTATUS status;
2261 if (tevent_req_is_nterror(req, &status)) {
2262 return status;
2264 *pfnum = state->fnum;
2265 return NT_STATUS_OK;
2268 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2269 int share_mode, uint16_t *pfnum)
2271 TALLOC_CTX *frame = talloc_stackframe();
2272 struct event_context *ev;
2273 struct tevent_req *req;
2274 NTSTATUS status = NT_STATUS_OK;
2276 if (smbXcli_conn_has_async_calls(cli->conn)) {
2278 * Can't use sync call while an async call is in flight
2280 status = NT_STATUS_INVALID_PARAMETER;
2281 goto fail;
2284 ev = event_context_init(frame);
2285 if (ev == NULL) {
2286 status = NT_STATUS_NO_MEMORY;
2287 goto fail;
2290 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2291 if (req == NULL) {
2292 status = NT_STATUS_NO_MEMORY;
2293 goto fail;
2296 if (!tevent_req_poll(req, ev)) {
2297 status = map_nt_error_from_unix(errno);
2298 goto fail;
2301 status = cli_openx_recv(req, pfnum);
2302 fail:
2303 TALLOC_FREE(frame);
2304 return status;
2306 /****************************************************************************
2307 Synchronous wrapper function that does an NtCreateX open by preference
2308 and falls back to openX if this fails.
2309 ****************************************************************************/
2311 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2312 int share_mode_in, uint16_t *pfnum)
2314 NTSTATUS status;
2315 unsigned int openfn = 0;
2316 unsigned int dos_deny = 0;
2317 uint32_t access_mask, share_mode, create_disposition, create_options;
2319 /* Do the initial mapping into OpenX parameters. */
2320 if (flags & O_CREAT) {
2321 openfn |= (1<<4);
2323 if (!(flags & O_EXCL)) {
2324 if (flags & O_TRUNC)
2325 openfn |= (1<<1);
2326 else
2327 openfn |= (1<<0);
2330 dos_deny = (share_mode_in<<4);
2332 if ((flags & O_ACCMODE) == O_RDWR) {
2333 dos_deny |= 2;
2334 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2335 dos_deny |= 1;
2338 #if defined(O_SYNC)
2339 if ((flags & O_SYNC) == O_SYNC) {
2340 dos_deny |= (1<<14);
2342 #endif /* O_SYNC */
2344 if (share_mode_in == DENY_FCB) {
2345 dos_deny = 0xFF;
2348 #if 0
2349 /* Hmmm. This is what I think the above code
2350 should look like if it's using the constants
2351 we #define. JRA. */
2353 if (flags & O_CREAT) {
2354 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2356 if (!(flags & O_EXCL)) {
2357 if (flags & O_TRUNC)
2358 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2359 else
2360 openfn |= OPENX_FILE_EXISTS_OPEN;
2363 dos_deny = SET_DENY_MODE(share_mode_in);
2365 if ((flags & O_ACCMODE) == O_RDWR) {
2366 dos_deny |= DOS_OPEN_RDWR;
2367 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2368 dos_deny |= DOS_OPEN_WRONLY;
2371 #if defined(O_SYNC)
2372 if ((flags & O_SYNC) == O_SYNC) {
2373 dos_deny |= FILE_SYNC_OPENMODE;
2375 #endif /* O_SYNC */
2377 if (share_mode_in == DENY_FCB) {
2378 dos_deny = 0xFF;
2380 #endif
2382 if (!map_open_params_to_ntcreate(fname, dos_deny,
2383 openfn, &access_mask,
2384 &share_mode, &create_disposition,
2385 &create_options, NULL)) {
2386 goto try_openx;
2389 status = cli_ntcreate(cli,
2390 fname,
2392 access_mask,
2394 share_mode,
2395 create_disposition,
2396 create_options,
2398 pfnum);
2400 /* Try and cope will all varients of "we don't do this call"
2401 and fall back to openX. */
2403 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2404 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2405 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2406 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2407 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2408 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2409 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2410 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2411 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2412 goto try_openx;
2415 return status;
2417 try_openx:
2419 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2422 /****************************************************************************
2423 Close a file.
2424 ****************************************************************************/
2426 struct cli_close_state {
2427 uint16_t vwv[3];
2430 static void cli_close_done(struct tevent_req *subreq);
2432 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2433 struct event_context *ev,
2434 struct cli_state *cli,
2435 uint16_t fnum,
2436 struct tevent_req **psubreq)
2438 struct tevent_req *req, *subreq;
2439 struct cli_close_state *state;
2441 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2442 if (req == NULL) {
2443 return NULL;
2446 SSVAL(state->vwv+0, 0, fnum);
2447 SIVALS(state->vwv+1, 0, -1);
2449 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2450 0, NULL);
2451 if (subreq == NULL) {
2452 TALLOC_FREE(req);
2453 return NULL;
2455 tevent_req_set_callback(subreq, cli_close_done, req);
2456 *psubreq = subreq;
2457 return req;
2460 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2461 struct event_context *ev,
2462 struct cli_state *cli,
2463 uint16_t fnum)
2465 struct tevent_req *req, *subreq;
2466 NTSTATUS status;
2468 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2469 if (req == NULL) {
2470 return NULL;
2473 status = smb1cli_req_chain_submit(&subreq, 1);
2474 if (tevent_req_nterror(req, status)) {
2475 return tevent_req_post(req, ev);
2477 return req;
2480 static void cli_close_done(struct tevent_req *subreq)
2482 struct tevent_req *req = tevent_req_callback_data(
2483 subreq, struct tevent_req);
2484 NTSTATUS status;
2486 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2487 TALLOC_FREE(subreq);
2488 if (tevent_req_nterror(req, status)) {
2489 return;
2491 tevent_req_done(req);
2494 NTSTATUS cli_close_recv(struct tevent_req *req)
2496 return tevent_req_simple_recv_ntstatus(req);
2499 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2501 TALLOC_CTX *frame = talloc_stackframe();
2502 struct event_context *ev;
2503 struct tevent_req *req;
2504 NTSTATUS status = NT_STATUS_OK;
2506 if (smbXcli_conn_has_async_calls(cli->conn)) {
2508 * Can't use sync call while an async call is in flight
2510 status = NT_STATUS_INVALID_PARAMETER;
2511 goto fail;
2514 ev = event_context_init(frame);
2515 if (ev == NULL) {
2516 status = NT_STATUS_NO_MEMORY;
2517 goto fail;
2520 req = cli_close_send(frame, ev, cli, fnum);
2521 if (req == NULL) {
2522 status = NT_STATUS_NO_MEMORY;
2523 goto fail;
2526 if (!tevent_req_poll(req, ev)) {
2527 status = map_nt_error_from_unix(errno);
2528 goto fail;
2531 status = cli_close_recv(req);
2532 fail:
2533 TALLOC_FREE(frame);
2534 return status;
2537 /****************************************************************************
2538 Truncate a file to a specified size
2539 ****************************************************************************/
2541 struct ftrunc_state {
2542 uint16_t setup;
2543 uint8_t param[6];
2544 uint8_t data[8];
2547 static void cli_ftruncate_done(struct tevent_req *subreq)
2549 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2550 NULL, 0, NULL, NULL, 0, NULL);
2551 tevent_req_simple_finish_ntstatus(subreq, status);
2554 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2555 struct event_context *ev,
2556 struct cli_state *cli,
2557 uint16_t fnum,
2558 uint64_t size)
2560 struct tevent_req *req = NULL, *subreq = NULL;
2561 struct ftrunc_state *state = NULL;
2563 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2564 if (req == NULL) {
2565 return NULL;
2568 /* Setup setup word. */
2569 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2571 /* Setup param array. */
2572 SSVAL(state->param,0,fnum);
2573 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2574 SSVAL(state->param,4,0);
2576 /* Setup data array. */
2577 SBVAL(state->data, 0, size);
2579 subreq = cli_trans_send(state, /* mem ctx. */
2580 ev, /* event ctx. */
2581 cli, /* cli_state. */
2582 SMBtrans2, /* cmd. */
2583 NULL, /* pipe name. */
2584 -1, /* fid. */
2585 0, /* function. */
2586 0, /* flags. */
2587 &state->setup, /* setup. */
2588 1, /* num setup uint16_t words. */
2589 0, /* max returned setup. */
2590 state->param, /* param. */
2591 6, /* num param. */
2592 2, /* max returned param. */
2593 state->data, /* data. */
2594 8, /* num data. */
2595 0); /* max returned data. */
2597 if (tevent_req_nomem(subreq, req)) {
2598 return tevent_req_post(req, ev);
2600 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2601 return req;
2604 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2606 return tevent_req_simple_recv_ntstatus(req);
2609 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2611 TALLOC_CTX *frame = talloc_stackframe();
2612 struct event_context *ev = NULL;
2613 struct tevent_req *req = NULL;
2614 NTSTATUS status = NT_STATUS_OK;
2616 if (smbXcli_conn_has_async_calls(cli->conn)) {
2618 * Can't use sync call while an async call is in flight
2620 status = NT_STATUS_INVALID_PARAMETER;
2621 goto fail;
2624 ev = event_context_init(frame);
2625 if (ev == NULL) {
2626 status = NT_STATUS_NO_MEMORY;
2627 goto fail;
2630 req = cli_ftruncate_send(frame,
2632 cli,
2633 fnum,
2634 size);
2635 if (req == NULL) {
2636 status = NT_STATUS_NO_MEMORY;
2637 goto fail;
2640 if (!tevent_req_poll(req, ev)) {
2641 status = map_nt_error_from_unix(errno);
2642 goto fail;
2645 status = cli_ftruncate_recv(req);
2647 fail:
2648 TALLOC_FREE(frame);
2649 return status;
2652 /****************************************************************************
2653 send a lock with a specified locktype
2654 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2655 ****************************************************************************/
2657 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2658 uint32_t offset, uint32_t len,
2659 int timeout, unsigned char locktype)
2661 uint16_t vwv[8];
2662 uint8_t bytes[10];
2663 NTSTATUS status;
2664 unsigned int set_timeout = 0;
2665 unsigned int saved_timeout = 0;
2667 SCVAL(vwv + 0, 0, 0xff);
2668 SCVAL(vwv + 0, 1, 0);
2669 SSVAL(vwv + 1, 0, 0);
2670 SSVAL(vwv + 2, 0, fnum);
2671 SCVAL(vwv + 3, 0, locktype);
2672 SCVAL(vwv + 3, 1, 0);
2673 SIVALS(vwv + 4, 0, timeout);
2674 SSVAL(vwv + 6, 0, 0);
2675 SSVAL(vwv + 7, 0, 1);
2677 SSVAL(bytes, 0, cli_getpid(cli));
2678 SIVAL(bytes, 2, offset);
2679 SIVAL(bytes, 6, len);
2681 if (timeout != 0) {
2682 if (timeout == -1) {
2683 set_timeout = 0x7FFFFFFF;
2684 } else {
2685 set_timeout = timeout + 2*1000;
2687 saved_timeout = cli_set_timeout(cli, set_timeout);
2690 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2691 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2693 if (saved_timeout != 0) {
2694 cli_set_timeout(cli, saved_timeout);
2697 return status;
2700 /****************************************************************************
2701 Lock a file.
2702 note that timeout is in units of 2 milliseconds
2703 ****************************************************************************/
2705 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2706 uint32_t offset, uint32_t len, int timeout,
2707 enum brl_type lock_type)
2709 NTSTATUS status;
2711 status = cli_locktype(cli, fnum, offset, len, timeout,
2712 (lock_type == READ_LOCK? 1 : 0));
2713 return status;
2716 /****************************************************************************
2717 Unlock a file.
2718 ****************************************************************************/
2720 struct cli_unlock_state {
2721 uint16_t vwv[8];
2722 uint8_t data[10];
2725 static void cli_unlock_done(struct tevent_req *subreq);
2727 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2728 struct event_context *ev,
2729 struct cli_state *cli,
2730 uint16_t fnum,
2731 uint64_t offset,
2732 uint64_t len)
2735 struct tevent_req *req = NULL, *subreq = NULL;
2736 struct cli_unlock_state *state = NULL;
2737 uint8_t additional_flags = 0;
2739 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2740 if (req == NULL) {
2741 return NULL;
2744 SCVAL(state->vwv+0, 0, 0xFF);
2745 SSVAL(state->vwv+2, 0, fnum);
2746 SCVAL(state->vwv+3, 0, 0);
2747 SIVALS(state->vwv+4, 0, 0);
2748 SSVAL(state->vwv+6, 0, 1);
2749 SSVAL(state->vwv+7, 0, 0);
2751 SSVAL(state->data, 0, cli_getpid(cli));
2752 SIVAL(state->data, 2, offset);
2753 SIVAL(state->data, 6, len);
2755 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2756 8, state->vwv, 10, state->data);
2757 if (tevent_req_nomem(subreq, req)) {
2758 return tevent_req_post(req, ev);
2760 tevent_req_set_callback(subreq, cli_unlock_done, req);
2761 return req;
2764 static void cli_unlock_done(struct tevent_req *subreq)
2766 struct tevent_req *req = tevent_req_callback_data(
2767 subreq, struct tevent_req);
2768 NTSTATUS status;
2770 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2771 TALLOC_FREE(subreq);
2772 if (tevent_req_nterror(req, status)) {
2773 return;
2775 tevent_req_done(req);
2778 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2780 return tevent_req_simple_recv_ntstatus(req);
2783 NTSTATUS cli_unlock(struct cli_state *cli,
2784 uint16_t fnum,
2785 uint32_t offset,
2786 uint32_t len)
2788 TALLOC_CTX *frame = talloc_stackframe();
2789 struct event_context *ev;
2790 struct tevent_req *req;
2791 NTSTATUS status = NT_STATUS_OK;
2793 if (smbXcli_conn_has_async_calls(cli->conn)) {
2795 * Can't use sync call while an async call is in flight
2797 status = NT_STATUS_INVALID_PARAMETER;
2798 goto fail;
2801 ev = event_context_init(frame);
2802 if (ev == NULL) {
2803 status = NT_STATUS_NO_MEMORY;
2804 goto fail;
2807 req = cli_unlock_send(frame, ev, cli,
2808 fnum, offset, len);
2809 if (req == NULL) {
2810 status = NT_STATUS_NO_MEMORY;
2811 goto fail;
2814 if (!tevent_req_poll(req, ev)) {
2815 status = map_nt_error_from_unix(errno);
2816 goto fail;
2819 status = cli_unlock_recv(req);
2821 fail:
2822 TALLOC_FREE(frame);
2823 return status;
2826 /****************************************************************************
2827 Lock a file with 64 bit offsets.
2828 ****************************************************************************/
2830 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
2831 uint64_t offset, uint64_t len, int timeout,
2832 enum brl_type lock_type)
2834 uint16_t vwv[8];
2835 uint8_t bytes[20];
2836 unsigned int set_timeout = 0;
2837 unsigned int saved_timeout = 0;
2838 int ltype;
2839 NTSTATUS status;
2841 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
2842 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
2845 ltype = (lock_type == READ_LOCK? 1 : 0);
2846 ltype |= LOCKING_ANDX_LARGE_FILES;
2848 SCVAL(vwv + 0, 0, 0xff);
2849 SCVAL(vwv + 0, 1, 0);
2850 SSVAL(vwv + 1, 0, 0);
2851 SSVAL(vwv + 2, 0, fnum);
2852 SCVAL(vwv + 3, 0, ltype);
2853 SCVAL(vwv + 3, 1, 0);
2854 SIVALS(vwv + 4, 0, timeout);
2855 SSVAL(vwv + 6, 0, 0);
2856 SSVAL(vwv + 7, 0, 1);
2858 SIVAL(bytes, 0, cli_getpid(cli));
2859 SOFF_T_R(bytes, 4, offset);
2860 SOFF_T_R(bytes, 12, len);
2862 if (timeout != 0) {
2863 if (timeout == -1) {
2864 set_timeout = 0x7FFFFFFF;
2865 } else {
2866 set_timeout = timeout + 2*1000;
2868 saved_timeout = cli_set_timeout(cli, set_timeout);
2871 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2872 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2874 if (saved_timeout != 0) {
2875 cli_set_timeout(cli, saved_timeout);
2878 return status;
2881 /****************************************************************************
2882 Unlock a file with 64 bit offsets.
2883 ****************************************************************************/
2885 struct cli_unlock64_state {
2886 uint16_t vwv[8];
2887 uint8_t data[20];
2890 static void cli_unlock64_done(struct tevent_req *subreq);
2892 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2893 struct event_context *ev,
2894 struct cli_state *cli,
2895 uint16_t fnum,
2896 uint64_t offset,
2897 uint64_t len)
2900 struct tevent_req *req = NULL, *subreq = NULL;
2901 struct cli_unlock64_state *state = NULL;
2902 uint8_t additional_flags = 0;
2904 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2905 if (req == NULL) {
2906 return NULL;
2909 SCVAL(state->vwv+0, 0, 0xff);
2910 SSVAL(state->vwv+2, 0, fnum);
2911 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2912 SIVALS(state->vwv+4, 0, 0);
2913 SSVAL(state->vwv+6, 0, 1);
2914 SSVAL(state->vwv+7, 0, 0);
2916 SIVAL(state->data, 0, cli_getpid(cli));
2917 SOFF_T_R(state->data, 4, offset);
2918 SOFF_T_R(state->data, 12, len);
2920 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2921 8, state->vwv, 20, state->data);
2922 if (tevent_req_nomem(subreq, req)) {
2923 return tevent_req_post(req, ev);
2925 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2926 return req;
2929 static void cli_unlock64_done(struct tevent_req *subreq)
2931 struct tevent_req *req = tevent_req_callback_data(
2932 subreq, struct tevent_req);
2933 NTSTATUS status;
2935 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2936 TALLOC_FREE(subreq);
2937 if (tevent_req_nterror(req, status)) {
2938 return;
2940 tevent_req_done(req);
2943 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2945 return tevent_req_simple_recv_ntstatus(req);
2948 NTSTATUS cli_unlock64(struct cli_state *cli,
2949 uint16_t fnum,
2950 uint64_t offset,
2951 uint64_t len)
2953 TALLOC_CTX *frame = talloc_stackframe();
2954 struct event_context *ev;
2955 struct tevent_req *req;
2956 NTSTATUS status = NT_STATUS_OK;
2958 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
2959 return cli_unlock(cli, fnum, offset, len);
2962 if (smbXcli_conn_has_async_calls(cli->conn)) {
2964 * Can't use sync call while an async call is in flight
2966 status = NT_STATUS_INVALID_PARAMETER;
2967 goto fail;
2970 ev = event_context_init(frame);
2971 if (ev == NULL) {
2972 status = NT_STATUS_NO_MEMORY;
2973 goto fail;
2976 req = cli_unlock64_send(frame, ev, cli,
2977 fnum, offset, len);
2978 if (req == NULL) {
2979 status = NT_STATUS_NO_MEMORY;
2980 goto fail;
2983 if (!tevent_req_poll(req, ev)) {
2984 status = map_nt_error_from_unix(errno);
2985 goto fail;
2988 status = cli_unlock64_recv(req);
2990 fail:
2991 TALLOC_FREE(frame);
2992 return status;
2995 /****************************************************************************
2996 Get/unlock a POSIX lock on a file - internal function.
2997 ****************************************************************************/
2999 struct posix_lock_state {
3000 uint16_t setup;
3001 uint8_t param[4];
3002 uint8_t data[POSIX_LOCK_DATA_SIZE];
3005 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3007 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3008 NULL, 0, NULL, NULL, 0, NULL);
3009 tevent_req_simple_finish_ntstatus(subreq, status);
3012 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3013 struct event_context *ev,
3014 struct cli_state *cli,
3015 uint16_t fnum,
3016 uint64_t offset,
3017 uint64_t len,
3018 bool wait_lock,
3019 enum brl_type lock_type)
3021 struct tevent_req *req = NULL, *subreq = NULL;
3022 struct posix_lock_state *state = NULL;
3024 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3025 if (req == NULL) {
3026 return NULL;
3029 /* Setup setup word. */
3030 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3032 /* Setup param array. */
3033 SSVAL(&state->param, 0, fnum);
3034 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3036 /* Setup data array. */
3037 switch (lock_type) {
3038 case READ_LOCK:
3039 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3040 POSIX_LOCK_TYPE_READ);
3041 break;
3042 case WRITE_LOCK:
3043 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3044 POSIX_LOCK_TYPE_WRITE);
3045 break;
3046 case UNLOCK_LOCK:
3047 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3048 POSIX_LOCK_TYPE_UNLOCK);
3049 break;
3050 default:
3051 return NULL;
3054 if (wait_lock) {
3055 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3056 POSIX_LOCK_FLAG_WAIT);
3057 } else {
3058 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3059 POSIX_LOCK_FLAG_NOWAIT);
3062 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3063 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3064 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3066 subreq = cli_trans_send(state, /* mem ctx. */
3067 ev, /* event ctx. */
3068 cli, /* cli_state. */
3069 SMBtrans2, /* cmd. */
3070 NULL, /* pipe name. */
3071 -1, /* fid. */
3072 0, /* function. */
3073 0, /* flags. */
3074 &state->setup, /* setup. */
3075 1, /* num setup uint16_t words. */
3076 0, /* max returned setup. */
3077 state->param, /* param. */
3078 4, /* num param. */
3079 2, /* max returned param. */
3080 state->data, /* data. */
3081 POSIX_LOCK_DATA_SIZE, /* num data. */
3082 0); /* max returned data. */
3084 if (tevent_req_nomem(subreq, req)) {
3085 return tevent_req_post(req, ev);
3087 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3088 return req;
3091 /****************************************************************************
3092 POSIX Lock a file.
3093 ****************************************************************************/
3095 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3096 struct event_context *ev,
3097 struct cli_state *cli,
3098 uint16_t fnum,
3099 uint64_t offset,
3100 uint64_t len,
3101 bool wait_lock,
3102 enum brl_type lock_type)
3104 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3105 wait_lock, lock_type);
3108 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3110 return tevent_req_simple_recv_ntstatus(req);
3113 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3114 uint64_t offset, uint64_t len,
3115 bool wait_lock, enum brl_type lock_type)
3117 TALLOC_CTX *frame = talloc_stackframe();
3118 struct event_context *ev = NULL;
3119 struct tevent_req *req = NULL;
3120 NTSTATUS status = NT_STATUS_OK;
3122 if (smbXcli_conn_has_async_calls(cli->conn)) {
3124 * Can't use sync call while an async call is in flight
3126 status = NT_STATUS_INVALID_PARAMETER;
3127 goto fail;
3130 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3131 status = NT_STATUS_INVALID_PARAMETER;
3132 goto fail;
3135 ev = event_context_init(frame);
3136 if (ev == NULL) {
3137 status = NT_STATUS_NO_MEMORY;
3138 goto fail;
3141 req = cli_posix_lock_send(frame,
3143 cli,
3144 fnum,
3145 offset,
3146 len,
3147 wait_lock,
3148 lock_type);
3149 if (req == NULL) {
3150 status = NT_STATUS_NO_MEMORY;
3151 goto fail;
3154 if (!tevent_req_poll(req, ev)) {
3155 status = map_nt_error_from_unix(errno);
3156 goto fail;
3159 status = cli_posix_lock_recv(req);
3161 fail:
3162 TALLOC_FREE(frame);
3163 return status;
3166 /****************************************************************************
3167 POSIX Unlock a file.
3168 ****************************************************************************/
3170 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3171 struct event_context *ev,
3172 struct cli_state *cli,
3173 uint16_t fnum,
3174 uint64_t offset,
3175 uint64_t len)
3177 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3178 false, UNLOCK_LOCK);
3181 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3183 return tevent_req_simple_recv_ntstatus(req);
3186 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3188 TALLOC_CTX *frame = talloc_stackframe();
3189 struct event_context *ev = NULL;
3190 struct tevent_req *req = NULL;
3191 NTSTATUS status = NT_STATUS_OK;
3193 if (smbXcli_conn_has_async_calls(cli->conn)) {
3195 * Can't use sync call while an async call is in flight
3197 status = NT_STATUS_INVALID_PARAMETER;
3198 goto fail;
3201 ev = event_context_init(frame);
3202 if (ev == NULL) {
3203 status = NT_STATUS_NO_MEMORY;
3204 goto fail;
3207 req = cli_posix_unlock_send(frame,
3209 cli,
3210 fnum,
3211 offset,
3212 len);
3213 if (req == NULL) {
3214 status = NT_STATUS_NO_MEMORY;
3215 goto fail;
3218 if (!tevent_req_poll(req, ev)) {
3219 status = map_nt_error_from_unix(errno);
3220 goto fail;
3223 status = cli_posix_unlock_recv(req);
3225 fail:
3226 TALLOC_FREE(frame);
3227 return status;
3230 /****************************************************************************
3231 Do a SMBgetattrE call.
3232 ****************************************************************************/
3234 static void cli_getattrE_done(struct tevent_req *subreq);
3236 struct cli_getattrE_state {
3237 uint16_t vwv[1];
3238 int zone_offset;
3239 uint16_t attr;
3240 off_t size;
3241 time_t change_time;
3242 time_t access_time;
3243 time_t write_time;
3246 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3247 struct event_context *ev,
3248 struct cli_state *cli,
3249 uint16_t fnum)
3251 struct tevent_req *req = NULL, *subreq = NULL;
3252 struct cli_getattrE_state *state = NULL;
3253 uint8_t additional_flags = 0;
3255 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3256 if (req == NULL) {
3257 return NULL;
3260 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3261 SSVAL(state->vwv+0,0,fnum);
3263 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3264 1, state->vwv, 0, NULL);
3265 if (tevent_req_nomem(subreq, req)) {
3266 return tevent_req_post(req, ev);
3268 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3269 return req;
3272 static void cli_getattrE_done(struct tevent_req *subreq)
3274 struct tevent_req *req = tevent_req_callback_data(
3275 subreq, struct tevent_req);
3276 struct cli_getattrE_state *state = tevent_req_data(
3277 req, struct cli_getattrE_state);
3278 uint8_t wct;
3279 uint16_t *vwv = NULL;
3280 NTSTATUS status;
3282 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3283 NULL, NULL);
3284 TALLOC_FREE(subreq);
3285 if (tevent_req_nterror(req, status)) {
3286 return;
3289 state->size = (off_t)IVAL(vwv+6,0);
3290 state->attr = SVAL(vwv+10,0);
3291 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3292 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3293 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3295 tevent_req_done(req);
3298 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3299 uint16_t *attr,
3300 off_t *size,
3301 time_t *change_time,
3302 time_t *access_time,
3303 time_t *write_time)
3305 struct cli_getattrE_state *state = tevent_req_data(
3306 req, struct cli_getattrE_state);
3307 NTSTATUS status;
3309 if (tevent_req_is_nterror(req, &status)) {
3310 return status;
3312 if (attr) {
3313 *attr = state->attr;
3315 if (size) {
3316 *size = state->size;
3318 if (change_time) {
3319 *change_time = state->change_time;
3321 if (access_time) {
3322 *access_time = state->access_time;
3324 if (write_time) {
3325 *write_time = state->write_time;
3327 return NT_STATUS_OK;
3330 NTSTATUS cli_getattrE(struct cli_state *cli,
3331 uint16_t fnum,
3332 uint16_t *attr,
3333 off_t *size,
3334 time_t *change_time,
3335 time_t *access_time,
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 (smbXcli_conn_has_async_calls(cli->conn)) {
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_getattrE_send(frame, ev, cli, fnum);
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_getattrE_recv(req,
3369 attr,
3370 size,
3371 change_time,
3372 access_time,
3373 write_time);
3375 fail:
3376 TALLOC_FREE(frame);
3377 return status;
3380 /****************************************************************************
3381 Do a SMBgetatr call
3382 ****************************************************************************/
3384 static void cli_getatr_done(struct tevent_req *subreq);
3386 struct cli_getatr_state {
3387 int zone_offset;
3388 uint16_t attr;
3389 off_t size;
3390 time_t write_time;
3393 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3394 struct event_context *ev,
3395 struct cli_state *cli,
3396 const char *fname)
3398 struct tevent_req *req = NULL, *subreq = NULL;
3399 struct cli_getatr_state *state = NULL;
3400 uint8_t additional_flags = 0;
3401 uint8_t *bytes = NULL;
3403 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3404 if (req == NULL) {
3405 return NULL;
3408 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3410 bytes = talloc_array(state, uint8_t, 1);
3411 if (tevent_req_nomem(bytes, req)) {
3412 return tevent_req_post(req, ev);
3414 bytes[0] = 4;
3415 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3416 strlen(fname)+1, NULL);
3418 if (tevent_req_nomem(bytes, req)) {
3419 return tevent_req_post(req, ev);
3422 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3423 0, NULL, talloc_get_size(bytes), bytes);
3424 if (tevent_req_nomem(subreq, req)) {
3425 return tevent_req_post(req, ev);
3427 tevent_req_set_callback(subreq, cli_getatr_done, req);
3428 return req;
3431 static void cli_getatr_done(struct tevent_req *subreq)
3433 struct tevent_req *req = tevent_req_callback_data(
3434 subreq, struct tevent_req);
3435 struct cli_getatr_state *state = tevent_req_data(
3436 req, struct cli_getatr_state);
3437 uint8_t wct;
3438 uint16_t *vwv = NULL;
3439 NTSTATUS status;
3441 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3442 NULL);
3443 TALLOC_FREE(subreq);
3444 if (tevent_req_nterror(req, status)) {
3445 return;
3448 state->attr = SVAL(vwv+0,0);
3449 state->size = (off_t)IVAL(vwv+3,0);
3450 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3452 tevent_req_done(req);
3455 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3456 uint16_t *attr,
3457 off_t *size,
3458 time_t *write_time)
3460 struct cli_getatr_state *state = tevent_req_data(
3461 req, struct cli_getatr_state);
3462 NTSTATUS status;
3464 if (tevent_req_is_nterror(req, &status)) {
3465 return status;
3467 if (attr) {
3468 *attr = state->attr;
3470 if (size) {
3471 *size = state->size;
3473 if (write_time) {
3474 *write_time = state->write_time;
3476 return NT_STATUS_OK;
3479 NTSTATUS cli_getatr(struct cli_state *cli,
3480 const char *fname,
3481 uint16_t *attr,
3482 off_t *size,
3483 time_t *write_time)
3485 TALLOC_CTX *frame = talloc_stackframe();
3486 struct event_context *ev = NULL;
3487 struct tevent_req *req = NULL;
3488 NTSTATUS status = NT_STATUS_OK;
3490 if (smbXcli_conn_has_async_calls(cli->conn)) {
3492 * Can't use sync call while an async call is in flight
3494 status = NT_STATUS_INVALID_PARAMETER;
3495 goto fail;
3498 ev = event_context_init(frame);
3499 if (ev == NULL) {
3500 status = NT_STATUS_NO_MEMORY;
3501 goto fail;
3504 req = cli_getatr_send(frame, ev, cli, fname);
3505 if (req == NULL) {
3506 status = NT_STATUS_NO_MEMORY;
3507 goto fail;
3510 if (!tevent_req_poll(req, ev)) {
3511 status = map_nt_error_from_unix(errno);
3512 goto fail;
3515 status = cli_getatr_recv(req,
3516 attr,
3517 size,
3518 write_time);
3520 fail:
3521 TALLOC_FREE(frame);
3522 return status;
3525 /****************************************************************************
3526 Do a SMBsetattrE call.
3527 ****************************************************************************/
3529 static void cli_setattrE_done(struct tevent_req *subreq);
3531 struct cli_setattrE_state {
3532 uint16_t vwv[7];
3535 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3536 struct event_context *ev,
3537 struct cli_state *cli,
3538 uint16_t fnum,
3539 time_t change_time,
3540 time_t access_time,
3541 time_t write_time)
3543 struct tevent_req *req = NULL, *subreq = NULL;
3544 struct cli_setattrE_state *state = NULL;
3545 uint8_t additional_flags = 0;
3547 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3548 if (req == NULL) {
3549 return NULL;
3552 SSVAL(state->vwv+0, 0, fnum);
3553 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3554 smb1cli_conn_server_time_zone(cli->conn));
3555 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3556 smb1cli_conn_server_time_zone(cli->conn));
3557 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3558 smb1cli_conn_server_time_zone(cli->conn));
3560 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3561 7, state->vwv, 0, NULL);
3562 if (tevent_req_nomem(subreq, req)) {
3563 return tevent_req_post(req, ev);
3565 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3566 return req;
3569 static void cli_setattrE_done(struct tevent_req *subreq)
3571 struct tevent_req *req = tevent_req_callback_data(
3572 subreq, struct tevent_req);
3573 NTSTATUS status;
3575 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3576 TALLOC_FREE(subreq);
3577 if (tevent_req_nterror(req, status)) {
3578 return;
3580 tevent_req_done(req);
3583 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3585 return tevent_req_simple_recv_ntstatus(req);
3588 NTSTATUS cli_setattrE(struct cli_state *cli,
3589 uint16_t fnum,
3590 time_t change_time,
3591 time_t access_time,
3592 time_t write_time)
3594 TALLOC_CTX *frame = talloc_stackframe();
3595 struct event_context *ev = NULL;
3596 struct tevent_req *req = NULL;
3597 NTSTATUS status = NT_STATUS_OK;
3599 if (smbXcli_conn_has_async_calls(cli->conn)) {
3601 * Can't use sync call while an async call is in flight
3603 status = NT_STATUS_INVALID_PARAMETER;
3604 goto fail;
3607 ev = event_context_init(frame);
3608 if (ev == NULL) {
3609 status = NT_STATUS_NO_MEMORY;
3610 goto fail;
3613 req = cli_setattrE_send(frame, ev,
3614 cli,
3615 fnum,
3616 change_time,
3617 access_time,
3618 write_time);
3620 if (req == NULL) {
3621 status = NT_STATUS_NO_MEMORY;
3622 goto fail;
3625 if (!tevent_req_poll(req, ev)) {
3626 status = map_nt_error_from_unix(errno);
3627 goto fail;
3630 status = cli_setattrE_recv(req);
3632 fail:
3633 TALLOC_FREE(frame);
3634 return status;
3637 /****************************************************************************
3638 Do a SMBsetatr call.
3639 ****************************************************************************/
3641 static void cli_setatr_done(struct tevent_req *subreq);
3643 struct cli_setatr_state {
3644 uint16_t vwv[8];
3647 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3648 struct event_context *ev,
3649 struct cli_state *cli,
3650 const char *fname,
3651 uint16_t attr,
3652 time_t mtime)
3654 struct tevent_req *req = NULL, *subreq = NULL;
3655 struct cli_setatr_state *state = NULL;
3656 uint8_t additional_flags = 0;
3657 uint8_t *bytes = NULL;
3659 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3660 if (req == NULL) {
3661 return NULL;
3664 SSVAL(state->vwv+0, 0, attr);
3665 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3667 bytes = talloc_array(state, uint8_t, 1);
3668 if (tevent_req_nomem(bytes, req)) {
3669 return tevent_req_post(req, ev);
3671 bytes[0] = 4;
3672 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3673 strlen(fname)+1, NULL);
3674 if (tevent_req_nomem(bytes, req)) {
3675 return tevent_req_post(req, ev);
3677 bytes = talloc_realloc(state, bytes, uint8_t,
3678 talloc_get_size(bytes)+1);
3679 if (tevent_req_nomem(bytes, req)) {
3680 return tevent_req_post(req, ev);
3683 bytes[talloc_get_size(bytes)-1] = 4;
3684 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3685 1, NULL);
3686 if (tevent_req_nomem(bytes, req)) {
3687 return tevent_req_post(req, ev);
3690 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3691 8, state->vwv, talloc_get_size(bytes), bytes);
3692 if (tevent_req_nomem(subreq, req)) {
3693 return tevent_req_post(req, ev);
3695 tevent_req_set_callback(subreq, cli_setatr_done, req);
3696 return req;
3699 static void cli_setatr_done(struct tevent_req *subreq)
3701 struct tevent_req *req = tevent_req_callback_data(
3702 subreq, struct tevent_req);
3703 NTSTATUS status;
3705 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3706 TALLOC_FREE(subreq);
3707 if (tevent_req_nterror(req, status)) {
3708 return;
3710 tevent_req_done(req);
3713 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3715 return tevent_req_simple_recv_ntstatus(req);
3718 NTSTATUS cli_setatr(struct cli_state *cli,
3719 const char *fname,
3720 uint16_t attr,
3721 time_t mtime)
3723 TALLOC_CTX *frame = talloc_stackframe();
3724 struct event_context *ev = NULL;
3725 struct tevent_req *req = NULL;
3726 NTSTATUS status = NT_STATUS_OK;
3728 if (smbXcli_conn_has_async_calls(cli->conn)) {
3730 * Can't use sync call while an async call is in flight
3732 status = NT_STATUS_INVALID_PARAMETER;
3733 goto fail;
3736 ev = event_context_init(frame);
3737 if (ev == NULL) {
3738 status = NT_STATUS_NO_MEMORY;
3739 goto fail;
3742 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3743 if (req == NULL) {
3744 status = NT_STATUS_NO_MEMORY;
3745 goto fail;
3748 if (!tevent_req_poll(req, ev)) {
3749 status = map_nt_error_from_unix(errno);
3750 goto fail;
3753 status = cli_setatr_recv(req);
3755 fail:
3756 TALLOC_FREE(frame);
3757 return status;
3760 /****************************************************************************
3761 Check for existance of a dir.
3762 ****************************************************************************/
3764 static void cli_chkpath_done(struct tevent_req *subreq);
3766 struct cli_chkpath_state {
3767 int dummy;
3770 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3771 struct event_context *ev,
3772 struct cli_state *cli,
3773 const char *fname)
3775 struct tevent_req *req = NULL, *subreq = NULL;
3776 struct cli_chkpath_state *state = NULL;
3777 uint8_t additional_flags = 0;
3778 uint8_t *bytes = NULL;
3780 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3781 if (req == NULL) {
3782 return NULL;
3785 bytes = talloc_array(state, uint8_t, 1);
3786 if (tevent_req_nomem(bytes, req)) {
3787 return tevent_req_post(req, ev);
3789 bytes[0] = 4;
3790 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3791 strlen(fname)+1, NULL);
3793 if (tevent_req_nomem(bytes, req)) {
3794 return tevent_req_post(req, ev);
3797 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3798 0, NULL, talloc_get_size(bytes), bytes);
3799 if (tevent_req_nomem(subreq, req)) {
3800 return tevent_req_post(req, ev);
3802 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3803 return req;
3806 static void cli_chkpath_done(struct tevent_req *subreq)
3808 struct tevent_req *req = tevent_req_callback_data(
3809 subreq, struct tevent_req);
3810 NTSTATUS status;
3812 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3813 TALLOC_FREE(subreq);
3814 if (tevent_req_nterror(req, status)) {
3815 return;
3817 tevent_req_done(req);
3820 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3822 return tevent_req_simple_recv_ntstatus(req);
3825 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3827 TALLOC_CTX *frame = talloc_stackframe();
3828 struct event_context *ev = NULL;
3829 struct tevent_req *req = NULL;
3830 char *path2 = NULL;
3831 NTSTATUS status = NT_STATUS_OK;
3833 if (smbXcli_conn_has_async_calls(cli->conn)) {
3835 * Can't use sync call while an async call is in flight
3837 status = NT_STATUS_INVALID_PARAMETER;
3838 goto fail;
3841 path2 = talloc_strdup(frame, path);
3842 if (!path2) {
3843 status = NT_STATUS_NO_MEMORY;
3844 goto fail;
3846 trim_char(path2,'\0','\\');
3847 if (!*path2) {
3848 path2 = talloc_strdup(frame, "\\");
3849 if (!path2) {
3850 status = NT_STATUS_NO_MEMORY;
3851 goto fail;
3855 ev = event_context_init(frame);
3856 if (ev == NULL) {
3857 status = NT_STATUS_NO_MEMORY;
3858 goto fail;
3861 req = cli_chkpath_send(frame, ev, cli, path2);
3862 if (req == NULL) {
3863 status = NT_STATUS_NO_MEMORY;
3864 goto fail;
3867 if (!tevent_req_poll(req, ev)) {
3868 status = map_nt_error_from_unix(errno);
3869 goto fail;
3872 status = cli_chkpath_recv(req);
3874 fail:
3875 TALLOC_FREE(frame);
3876 return status;
3879 /****************************************************************************
3880 Query disk space.
3881 ****************************************************************************/
3883 static void cli_dskattr_done(struct tevent_req *subreq);
3885 struct cli_dskattr_state {
3886 int bsize;
3887 int total;
3888 int avail;
3891 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3892 struct event_context *ev,
3893 struct cli_state *cli)
3895 struct tevent_req *req = NULL, *subreq = NULL;
3896 struct cli_dskattr_state *state = NULL;
3897 uint8_t additional_flags = 0;
3899 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3900 if (req == NULL) {
3901 return NULL;
3904 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3905 0, NULL, 0, NULL);
3906 if (tevent_req_nomem(subreq, req)) {
3907 return tevent_req_post(req, ev);
3909 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3910 return req;
3913 static void cli_dskattr_done(struct tevent_req *subreq)
3915 struct tevent_req *req = tevent_req_callback_data(
3916 subreq, struct tevent_req);
3917 struct cli_dskattr_state *state = tevent_req_data(
3918 req, struct cli_dskattr_state);
3919 uint8_t wct;
3920 uint16_t *vwv = NULL;
3921 NTSTATUS status;
3923 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3924 NULL);
3925 TALLOC_FREE(subreq);
3926 if (tevent_req_nterror(req, status)) {
3927 return;
3929 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3930 state->total = SVAL(vwv+0, 0);
3931 state->avail = SVAL(vwv+3, 0);
3932 tevent_req_done(req);
3935 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3937 struct cli_dskattr_state *state = tevent_req_data(
3938 req, struct cli_dskattr_state);
3939 NTSTATUS status;
3941 if (tevent_req_is_nterror(req, &status)) {
3942 return status;
3944 *bsize = state->bsize;
3945 *total = state->total;
3946 *avail = state->avail;
3947 return NT_STATUS_OK;
3950 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3952 TALLOC_CTX *frame = talloc_stackframe();
3953 struct event_context *ev = NULL;
3954 struct tevent_req *req = NULL;
3955 NTSTATUS status = NT_STATUS_OK;
3957 if (smbXcli_conn_has_async_calls(cli->conn)) {
3959 * Can't use sync call while an async call is in flight
3961 status = NT_STATUS_INVALID_PARAMETER;
3962 goto fail;
3965 ev = event_context_init(frame);
3966 if (ev == NULL) {
3967 status = NT_STATUS_NO_MEMORY;
3968 goto fail;
3971 req = cli_dskattr_send(frame, ev, cli);
3972 if (req == NULL) {
3973 status = NT_STATUS_NO_MEMORY;
3974 goto fail;
3977 if (!tevent_req_poll(req, ev)) {
3978 status = map_nt_error_from_unix(errno);
3979 goto fail;
3982 status = cli_dskattr_recv(req, bsize, total, avail);
3984 fail:
3985 TALLOC_FREE(frame);
3986 return status;
3989 /****************************************************************************
3990 Create and open a temporary file.
3991 ****************************************************************************/
3993 static void cli_ctemp_done(struct tevent_req *subreq);
3995 struct ctemp_state {
3996 uint16_t vwv[3];
3997 char *ret_path;
3998 uint16_t fnum;
4001 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4002 struct event_context *ev,
4003 struct cli_state *cli,
4004 const char *path)
4006 struct tevent_req *req = NULL, *subreq = NULL;
4007 struct ctemp_state *state = NULL;
4008 uint8_t additional_flags = 0;
4009 uint8_t *bytes = NULL;
4011 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4012 if (req == NULL) {
4013 return NULL;
4016 SSVAL(state->vwv,0,0);
4017 SIVALS(state->vwv+1,0,-1);
4019 bytes = talloc_array(state, uint8_t, 1);
4020 if (tevent_req_nomem(bytes, req)) {
4021 return tevent_req_post(req, ev);
4023 bytes[0] = 4;
4024 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4025 strlen(path)+1, NULL);
4026 if (tevent_req_nomem(bytes, req)) {
4027 return tevent_req_post(req, ev);
4030 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4031 3, state->vwv, talloc_get_size(bytes), bytes);
4032 if (tevent_req_nomem(subreq, req)) {
4033 return tevent_req_post(req, ev);
4035 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4036 return req;
4039 static void cli_ctemp_done(struct tevent_req *subreq)
4041 struct tevent_req *req = tevent_req_callback_data(
4042 subreq, struct tevent_req);
4043 struct ctemp_state *state = tevent_req_data(
4044 req, struct ctemp_state);
4045 NTSTATUS status;
4046 uint8_t wcnt;
4047 uint16_t *vwv;
4048 uint32_t num_bytes = 0;
4049 uint8_t *bytes = NULL;
4051 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4052 &num_bytes, &bytes);
4053 TALLOC_FREE(subreq);
4054 if (tevent_req_nterror(req, status)) {
4055 return;
4058 state->fnum = SVAL(vwv+0, 0);
4060 /* From W2K3, the result is just the ASCII name */
4061 if (num_bytes < 2) {
4062 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4063 return;
4066 if (pull_string_talloc(state,
4067 NULL,
4069 &state->ret_path,
4070 bytes,
4071 num_bytes,
4072 STR_ASCII) == 0) {
4073 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4074 return;
4076 tevent_req_done(req);
4079 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4080 TALLOC_CTX *ctx,
4081 uint16_t *pfnum,
4082 char **outfile)
4084 struct ctemp_state *state = tevent_req_data(req,
4085 struct ctemp_state);
4086 NTSTATUS status;
4088 if (tevent_req_is_nterror(req, &status)) {
4089 return status;
4091 *pfnum = state->fnum;
4092 *outfile = talloc_strdup(ctx, state->ret_path);
4093 if (!*outfile) {
4094 return NT_STATUS_NO_MEMORY;
4096 return NT_STATUS_OK;
4099 NTSTATUS cli_ctemp(struct cli_state *cli,
4100 TALLOC_CTX *ctx,
4101 const char *path,
4102 uint16_t *pfnum,
4103 char **out_path)
4105 TALLOC_CTX *frame = talloc_stackframe();
4106 struct event_context *ev;
4107 struct tevent_req *req;
4108 NTSTATUS status = NT_STATUS_OK;
4110 if (smbXcli_conn_has_async_calls(cli->conn)) {
4112 * Can't use sync call while an async call is in flight
4114 status = NT_STATUS_INVALID_PARAMETER;
4115 goto fail;
4118 ev = event_context_init(frame);
4119 if (ev == NULL) {
4120 status = NT_STATUS_NO_MEMORY;
4121 goto fail;
4124 req = cli_ctemp_send(frame, ev, cli, path);
4125 if (req == NULL) {
4126 status = NT_STATUS_NO_MEMORY;
4127 goto fail;
4130 if (!tevent_req_poll(req, ev)) {
4131 status = map_nt_error_from_unix(errno);
4132 goto fail;
4135 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4137 fail:
4138 TALLOC_FREE(frame);
4139 return status;
4143 send a raw ioctl - used by the torture code
4145 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4147 uint16_t vwv[3];
4148 NTSTATUS status;
4150 SSVAL(vwv+0, 0, fnum);
4151 SSVAL(vwv+1, 0, code>>16);
4152 SSVAL(vwv+2, 0, (code&0xFFFF));
4154 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4155 NULL, 0, NULL, NULL, NULL, NULL);
4156 if (!NT_STATUS_IS_OK(status)) {
4157 return status;
4159 *blob = data_blob_null;
4160 return NT_STATUS_OK;
4163 /*********************************************************
4164 Set an extended attribute utility fn.
4165 *********************************************************/
4167 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4168 uint8_t *param, unsigned int param_len,
4169 const char *ea_name,
4170 const char *ea_val, size_t ea_len)
4172 uint16_t setup[1];
4173 unsigned int data_len = 0;
4174 uint8_t *data = NULL;
4175 char *p;
4176 size_t ea_namelen = strlen(ea_name);
4177 NTSTATUS status;
4179 SSVAL(setup, 0, setup_val);
4181 if (ea_namelen == 0 && ea_len == 0) {
4182 data_len = 4;
4183 data = talloc_array(talloc_tos(),
4184 uint8_t,
4185 data_len);
4186 if (!data) {
4187 return NT_STATUS_NO_MEMORY;
4189 p = (char *)data;
4190 SIVAL(p,0,data_len);
4191 } else {
4192 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4193 data = talloc_array(talloc_tos(),
4194 uint8_t,
4195 data_len);
4196 if (!data) {
4197 return NT_STATUS_NO_MEMORY;
4199 p = (char *)data;
4200 SIVAL(p,0,data_len);
4201 p += 4;
4202 SCVAL(p, 0, 0); /* EA flags. */
4203 SCVAL(p, 1, ea_namelen);
4204 SSVAL(p, 2, ea_len);
4205 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4206 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4209 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4210 setup, 1, 0,
4211 param, param_len, 2,
4212 data, data_len, CLI_BUFFER_SIZE,
4213 NULL,
4214 NULL, 0, NULL, /* rsetup */
4215 NULL, 0, NULL, /* rparam */
4216 NULL, 0, NULL); /* rdata */
4217 talloc_free(data);
4218 return status;
4221 /*********************************************************
4222 Set an extended attribute on a pathname.
4223 *********************************************************/
4225 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4226 const char *ea_name, const char *ea_val,
4227 size_t ea_len)
4229 unsigned int param_len = 0;
4230 uint8_t *param;
4231 NTSTATUS status;
4232 TALLOC_CTX *frame = talloc_stackframe();
4234 param = talloc_array(talloc_tos(), uint8_t, 6);
4235 if (!param) {
4236 return NT_STATUS_NO_MEMORY;
4238 SSVAL(param,0,SMB_INFO_SET_EA);
4239 SSVAL(param,2,0);
4240 SSVAL(param,4,0);
4242 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4243 path, strlen(path)+1,
4244 NULL);
4245 param_len = talloc_get_size(param);
4247 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4248 ea_name, ea_val, ea_len);
4249 talloc_free(frame);
4250 return status;
4253 /*********************************************************
4254 Set an extended attribute on an fnum.
4255 *********************************************************/
4257 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4258 const char *ea_name, const char *ea_val,
4259 size_t ea_len)
4261 uint8_t param[6];
4263 memset(param, 0, 6);
4264 SSVAL(param,0,fnum);
4265 SSVAL(param,2,SMB_INFO_SET_EA);
4267 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4268 ea_name, ea_val, ea_len);
4271 /*********************************************************
4272 Get an extended attribute list utility fn.
4273 *********************************************************/
4275 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4276 size_t rdata_len,
4277 size_t *pnum_eas, struct ea_struct **pea_list)
4279 struct ea_struct *ea_list = NULL;
4280 size_t num_eas;
4281 size_t ea_size;
4282 const uint8_t *p;
4284 if (rdata_len < 4) {
4285 return false;
4288 ea_size = (size_t)IVAL(rdata,0);
4289 if (ea_size > rdata_len) {
4290 return false;
4293 if (ea_size == 0) {
4294 /* No EA's present. */
4295 *pnum_eas = 0;
4296 *pea_list = NULL;
4297 return true;
4300 p = rdata + 4;
4301 ea_size -= 4;
4303 /* Validate the EA list and count it. */
4304 for (num_eas = 0; ea_size >= 4; num_eas++) {
4305 unsigned int ea_namelen = CVAL(p,1);
4306 unsigned int ea_valuelen = SVAL(p,2);
4307 if (ea_namelen == 0) {
4308 return false;
4310 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4311 return false;
4313 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4314 p += 4 + ea_namelen + 1 + ea_valuelen;
4317 if (num_eas == 0) {
4318 *pnum_eas = 0;
4319 *pea_list = NULL;
4320 return true;
4323 *pnum_eas = num_eas;
4324 if (!pea_list) {
4325 /* Caller only wants number of EA's. */
4326 return true;
4329 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4330 if (!ea_list) {
4331 return false;
4334 ea_size = (size_t)IVAL(rdata,0);
4335 p = rdata + 4;
4337 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4338 struct ea_struct *ea = &ea_list[num_eas];
4339 fstring unix_ea_name;
4340 unsigned int ea_namelen = CVAL(p,1);
4341 unsigned int ea_valuelen = SVAL(p,2);
4343 ea->flags = CVAL(p,0);
4344 unix_ea_name[0] = '\0';
4345 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4346 ea->name = talloc_strdup(ea_list, unix_ea_name);
4347 if (!ea->name) {
4348 goto fail;
4350 /* Ensure the value is null terminated (in case it's a string). */
4351 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4352 if (!ea->value.data) {
4353 goto fail;
4355 if (ea_valuelen) {
4356 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4358 ea->value.data[ea_valuelen] = 0;
4359 ea->value.length--;
4360 p += 4 + ea_namelen + 1 + ea_valuelen;
4363 *pea_list = ea_list;
4364 return true;
4366 fail:
4367 TALLOC_FREE(ea_list);
4368 return false;
4371 /*********************************************************
4372 Get an extended attribute list from a pathname.
4373 *********************************************************/
4375 struct cli_get_ea_list_path_state {
4376 uint32_t num_data;
4377 uint8_t *data;
4380 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4382 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4383 struct tevent_context *ev,
4384 struct cli_state *cli,
4385 const char *fname)
4387 struct tevent_req *req, *subreq;
4388 struct cli_get_ea_list_path_state *state;
4390 req = tevent_req_create(mem_ctx, &state,
4391 struct cli_get_ea_list_path_state);
4392 if (req == NULL) {
4393 return NULL;
4395 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4396 SMB_INFO_QUERY_ALL_EAS, 4,
4397 CLI_BUFFER_SIZE);
4398 if (tevent_req_nomem(subreq, req)) {
4399 return tevent_req_post(req, ev);
4401 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4402 return req;
4405 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4407 struct tevent_req *req = tevent_req_callback_data(
4408 subreq, struct tevent_req);
4409 struct cli_get_ea_list_path_state *state = tevent_req_data(
4410 req, struct cli_get_ea_list_path_state);
4411 NTSTATUS status;
4413 status = cli_qpathinfo_recv(subreq, state, &state->data,
4414 &state->num_data);
4415 TALLOC_FREE(subreq);
4416 if (tevent_req_nterror(req, status)) {
4417 return;
4419 tevent_req_done(req);
4422 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4423 size_t *pnum_eas, struct ea_struct **peas)
4425 struct cli_get_ea_list_path_state *state = tevent_req_data(
4426 req, struct cli_get_ea_list_path_state);
4427 NTSTATUS status;
4429 if (tevent_req_is_nterror(req, &status)) {
4430 return status;
4432 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4433 pnum_eas, peas)) {
4434 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4436 return NT_STATUS_OK;
4439 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4440 TALLOC_CTX *ctx,
4441 size_t *pnum_eas,
4442 struct ea_struct **pea_list)
4444 TALLOC_CTX *frame = talloc_stackframe();
4445 struct event_context *ev = NULL;
4446 struct tevent_req *req = NULL;
4447 NTSTATUS status = NT_STATUS_NO_MEMORY;
4449 if (smbXcli_conn_has_async_calls(cli->conn)) {
4451 * Can't use sync call while an async call is in flight
4453 status = NT_STATUS_INVALID_PARAMETER;
4454 goto fail;
4456 ev = event_context_init(frame);
4457 if (ev == NULL) {
4458 goto fail;
4460 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4461 if (req == NULL) {
4462 goto fail;
4464 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4465 goto fail;
4467 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4468 fail:
4469 TALLOC_FREE(frame);
4470 return status;
4473 /****************************************************************************
4474 Convert open "flags" arg to uint32_t on wire.
4475 ****************************************************************************/
4477 static uint32_t open_flags_to_wire(int flags)
4479 int open_mode = flags & O_ACCMODE;
4480 uint32_t ret = 0;
4482 switch (open_mode) {
4483 case O_WRONLY:
4484 ret |= SMB_O_WRONLY;
4485 break;
4486 case O_RDWR:
4487 ret |= SMB_O_RDWR;
4488 break;
4489 default:
4490 case O_RDONLY:
4491 ret |= SMB_O_RDONLY;
4492 break;
4495 if (flags & O_CREAT) {
4496 ret |= SMB_O_CREAT;
4498 if (flags & O_EXCL) {
4499 ret |= SMB_O_EXCL;
4501 if (flags & O_TRUNC) {
4502 ret |= SMB_O_TRUNC;
4504 #if defined(O_SYNC)
4505 if (flags & O_SYNC) {
4506 ret |= SMB_O_SYNC;
4508 #endif /* O_SYNC */
4509 if (flags & O_APPEND) {
4510 ret |= SMB_O_APPEND;
4512 #if defined(O_DIRECT)
4513 if (flags & O_DIRECT) {
4514 ret |= SMB_O_DIRECT;
4516 #endif
4517 #if defined(O_DIRECTORY)
4518 if (flags & O_DIRECTORY) {
4519 ret |= SMB_O_DIRECTORY;
4521 #endif
4522 return ret;
4525 /****************************************************************************
4526 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4527 ****************************************************************************/
4529 struct posix_open_state {
4530 uint16_t setup;
4531 uint8_t *param;
4532 uint8_t data[18];
4533 uint16_t fnum; /* Out */
4536 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4538 struct tevent_req *req = tevent_req_callback_data(
4539 subreq, struct tevent_req);
4540 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4541 NTSTATUS status;
4542 uint8_t *data;
4543 uint32_t num_data;
4545 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4546 NULL, 0, NULL, &data, 12, &num_data);
4547 TALLOC_FREE(subreq);
4548 if (tevent_req_nterror(req, status)) {
4549 return;
4551 state->fnum = SVAL(data,2);
4552 tevent_req_done(req);
4555 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4556 struct event_context *ev,
4557 struct cli_state *cli,
4558 const char *fname,
4559 int flags,
4560 mode_t mode,
4561 bool is_dir)
4563 struct tevent_req *req = NULL, *subreq = NULL;
4564 struct posix_open_state *state = NULL;
4565 uint32_t wire_flags = open_flags_to_wire(flags);
4567 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4568 if (req == NULL) {
4569 return NULL;
4572 /* Setup setup word. */
4573 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4575 /* Setup param array. */
4576 state->param = talloc_array(state, uint8_t, 6);
4577 if (tevent_req_nomem(state->param, req)) {
4578 return tevent_req_post(req, ev);
4580 memset(state->param, '\0', 6);
4581 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4583 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4584 strlen(fname)+1, NULL);
4586 if (tevent_req_nomem(state->param, req)) {
4587 return tevent_req_post(req, ev);
4590 /* Setup data words. */
4591 if (is_dir) {
4592 wire_flags |= SMB_O_DIRECTORY;
4595 SIVAL(state->data,0,0); /* No oplock. */
4596 SIVAL(state->data,4,wire_flags);
4597 SIVAL(state->data,8,unix_perms_to_wire(mode));
4598 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4599 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4601 subreq = cli_trans_send(state, /* mem ctx. */
4602 ev, /* event ctx. */
4603 cli, /* cli_state. */
4604 SMBtrans2, /* cmd. */
4605 NULL, /* pipe name. */
4606 -1, /* fid. */
4607 0, /* function. */
4608 0, /* flags. */
4609 &state->setup, /* setup. */
4610 1, /* num setup uint16_t words. */
4611 0, /* max returned setup. */
4612 state->param, /* param. */
4613 talloc_get_size(state->param),/* num param. */
4614 2, /* max returned param. */
4615 state->data, /* data. */
4616 18, /* num data. */
4617 12); /* max returned data. */
4619 if (tevent_req_nomem(subreq, req)) {
4620 return tevent_req_post(req, ev);
4622 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4623 return req;
4626 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4627 struct event_context *ev,
4628 struct cli_state *cli,
4629 const char *fname,
4630 int flags,
4631 mode_t mode)
4633 return cli_posix_open_internal_send(mem_ctx, ev,
4634 cli, fname, flags, mode, false);
4637 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4639 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4640 NTSTATUS status;
4642 if (tevent_req_is_nterror(req, &status)) {
4643 return status;
4645 *pfnum = state->fnum;
4646 return NT_STATUS_OK;
4649 /****************************************************************************
4650 Open - POSIX semantics. Doesn't request oplock.
4651 ****************************************************************************/
4653 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4654 int flags, mode_t mode, uint16_t *pfnum)
4657 TALLOC_CTX *frame = talloc_stackframe();
4658 struct event_context *ev = NULL;
4659 struct tevent_req *req = NULL;
4660 NTSTATUS status = NT_STATUS_OK;
4662 if (smbXcli_conn_has_async_calls(cli->conn)) {
4664 * Can't use sync call while an async call is in flight
4666 status = NT_STATUS_INVALID_PARAMETER;
4667 goto fail;
4670 ev = event_context_init(frame);
4671 if (ev == NULL) {
4672 status = NT_STATUS_NO_MEMORY;
4673 goto fail;
4676 req = cli_posix_open_send(frame,
4678 cli,
4679 fname,
4680 flags,
4681 mode);
4682 if (req == NULL) {
4683 status = NT_STATUS_NO_MEMORY;
4684 goto fail;
4687 if (!tevent_req_poll(req, ev)) {
4688 status = map_nt_error_from_unix(errno);
4689 goto fail;
4692 status = cli_posix_open_recv(req, pfnum);
4694 fail:
4695 TALLOC_FREE(frame);
4696 return status;
4699 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4700 struct event_context *ev,
4701 struct cli_state *cli,
4702 const char *fname,
4703 mode_t mode)
4705 return cli_posix_open_internal_send(mem_ctx, ev,
4706 cli, fname, O_CREAT, mode, true);
4709 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4711 return tevent_req_simple_recv_ntstatus(req);
4714 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4716 TALLOC_CTX *frame = talloc_stackframe();
4717 struct event_context *ev = NULL;
4718 struct tevent_req *req = NULL;
4719 NTSTATUS status = NT_STATUS_OK;
4721 if (smbXcli_conn_has_async_calls(cli->conn)) {
4723 * Can't use sync call while an async call is in flight
4725 status = NT_STATUS_INVALID_PARAMETER;
4726 goto fail;
4729 ev = event_context_init(frame);
4730 if (ev == NULL) {
4731 status = NT_STATUS_NO_MEMORY;
4732 goto fail;
4735 req = cli_posix_mkdir_send(frame,
4737 cli,
4738 fname,
4739 mode);
4740 if (req == NULL) {
4741 status = NT_STATUS_NO_MEMORY;
4742 goto fail;
4745 if (!tevent_req_poll(req, ev)) {
4746 status = map_nt_error_from_unix(errno);
4747 goto fail;
4750 status = cli_posix_mkdir_recv(req);
4752 fail:
4753 TALLOC_FREE(frame);
4754 return status;
4757 /****************************************************************************
4758 unlink or rmdir - POSIX semantics.
4759 ****************************************************************************/
4761 struct cli_posix_unlink_internal_state {
4762 uint8_t data[2];
4765 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4767 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4768 struct event_context *ev,
4769 struct cli_state *cli,
4770 const char *fname,
4771 uint16_t level)
4773 struct tevent_req *req = NULL, *subreq = NULL;
4774 struct cli_posix_unlink_internal_state *state = NULL;
4776 req = tevent_req_create(mem_ctx, &state,
4777 struct cli_posix_unlink_internal_state);
4778 if (req == NULL) {
4779 return NULL;
4782 /* Setup data word. */
4783 SSVAL(state->data, 0, level);
4785 subreq = cli_setpathinfo_send(state, ev, cli,
4786 SMB_POSIX_PATH_UNLINK,
4787 fname,
4788 state->data, sizeof(state->data));
4789 if (tevent_req_nomem(subreq, req)) {
4790 return tevent_req_post(req, ev);
4792 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4793 return req;
4796 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4798 NTSTATUS status = cli_setpathinfo_recv(subreq);
4799 tevent_req_simple_finish_ntstatus(subreq, status);
4802 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4803 struct event_context *ev,
4804 struct cli_state *cli,
4805 const char *fname)
4807 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4808 SMB_POSIX_UNLINK_FILE_TARGET);
4811 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4813 return tevent_req_simple_recv_ntstatus(req);
4816 /****************************************************************************
4817 unlink - POSIX semantics.
4818 ****************************************************************************/
4820 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4822 TALLOC_CTX *frame = talloc_stackframe();
4823 struct event_context *ev = NULL;
4824 struct tevent_req *req = NULL;
4825 NTSTATUS status = NT_STATUS_OK;
4827 if (smbXcli_conn_has_async_calls(cli->conn)) {
4829 * Can't use sync call while an async call is in flight
4831 status = NT_STATUS_INVALID_PARAMETER;
4832 goto fail;
4835 ev = event_context_init(frame);
4836 if (ev == NULL) {
4837 status = NT_STATUS_NO_MEMORY;
4838 goto fail;
4841 req = cli_posix_unlink_send(frame,
4843 cli,
4844 fname);
4845 if (req == NULL) {
4846 status = NT_STATUS_NO_MEMORY;
4847 goto fail;
4850 if (!tevent_req_poll(req, ev)) {
4851 status = map_nt_error_from_unix(errno);
4852 goto fail;
4855 status = cli_posix_unlink_recv(req);
4857 fail:
4858 TALLOC_FREE(frame);
4859 return status;
4862 /****************************************************************************
4863 rmdir - POSIX semantics.
4864 ****************************************************************************/
4866 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4867 struct event_context *ev,
4868 struct cli_state *cli,
4869 const char *fname)
4871 return cli_posix_unlink_internal_send(
4872 mem_ctx, ev, cli, fname,
4873 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4876 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4878 return tevent_req_simple_recv_ntstatus(req);
4881 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4883 TALLOC_CTX *frame = talloc_stackframe();
4884 struct event_context *ev = NULL;
4885 struct tevent_req *req = NULL;
4886 NTSTATUS status = NT_STATUS_OK;
4888 if (smbXcli_conn_has_async_calls(cli->conn)) {
4890 * Can't use sync call while an async call is in flight
4892 status = NT_STATUS_INVALID_PARAMETER;
4893 goto fail;
4896 ev = event_context_init(frame);
4897 if (ev == NULL) {
4898 status = NT_STATUS_NO_MEMORY;
4899 goto fail;
4902 req = cli_posix_rmdir_send(frame,
4904 cli,
4905 fname);
4906 if (req == NULL) {
4907 status = NT_STATUS_NO_MEMORY;
4908 goto fail;
4911 if (!tevent_req_poll(req, ev)) {
4912 status = map_nt_error_from_unix(errno);
4913 goto fail;
4916 status = cli_posix_rmdir_recv(req, frame);
4918 fail:
4919 TALLOC_FREE(frame);
4920 return status;
4923 /****************************************************************************
4924 filechangenotify
4925 ****************************************************************************/
4927 struct cli_notify_state {
4928 uint8_t setup[8];
4929 uint32_t num_changes;
4930 struct notify_change *changes;
4933 static void cli_notify_done(struct tevent_req *subreq);
4935 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4936 struct tevent_context *ev,
4937 struct cli_state *cli, uint16_t fnum,
4938 uint32_t buffer_size,
4939 uint32_t completion_filter, bool recursive)
4941 struct tevent_req *req, *subreq;
4942 struct cli_notify_state *state;
4943 unsigned old_timeout;
4945 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4946 if (req == NULL) {
4947 return NULL;
4950 SIVAL(state->setup, 0, completion_filter);
4951 SSVAL(state->setup, 4, fnum);
4952 SSVAL(state->setup, 6, recursive);
4955 * Notifies should not time out
4957 old_timeout = cli_set_timeout(cli, 0);
4959 subreq = cli_trans_send(
4960 state, /* mem ctx. */
4961 ev, /* event ctx. */
4962 cli, /* cli_state. */
4963 SMBnttrans, /* cmd. */
4964 NULL, /* pipe name. */
4965 -1, /* fid. */
4966 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4967 0, /* flags. */
4968 (uint16_t *)state->setup, /* setup. */
4969 4, /* num setup uint16_t words. */
4970 0, /* max returned setup. */
4971 NULL, /* param. */
4972 0, /* num param. */
4973 buffer_size, /* max returned param. */
4974 NULL, /* data. */
4975 0, /* num data. */
4976 0); /* max returned data. */
4978 cli_set_timeout(cli, old_timeout);
4980 if (tevent_req_nomem(subreq, req)) {
4981 return tevent_req_post(req, ev);
4983 tevent_req_set_callback(subreq, cli_notify_done, req);
4984 return req;
4987 static void cli_notify_done(struct tevent_req *subreq)
4989 struct tevent_req *req = tevent_req_callback_data(
4990 subreq, struct tevent_req);
4991 struct cli_notify_state *state = tevent_req_data(
4992 req, struct cli_notify_state);
4993 NTSTATUS status;
4994 uint8_t *params;
4995 uint32_t i, ofs, num_params;
4996 uint16_t flags2;
4998 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4999 &params, 0, &num_params, NULL, 0, NULL);
5000 TALLOC_FREE(subreq);
5001 if (tevent_req_nterror(req, status)) {
5002 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5003 return;
5006 state->num_changes = 0;
5007 ofs = 0;
5009 while (num_params - ofs > 12) {
5010 uint32_t next = IVAL(params, ofs);
5011 state->num_changes += 1;
5013 if ((next == 0) || (ofs+next >= num_params)) {
5014 break;
5016 ofs += next;
5019 state->changes = talloc_array(state, struct notify_change,
5020 state->num_changes);
5021 if (tevent_req_nomem(state->changes, req)) {
5022 TALLOC_FREE(params);
5023 return;
5026 ofs = 0;
5028 for (i=0; i<state->num_changes; i++) {
5029 uint32_t next = IVAL(params, ofs);
5030 uint32_t len = IVAL(params, ofs+8);
5031 ssize_t ret;
5032 char *name;
5034 if (trans_oob(num_params, ofs + 12, len)) {
5035 TALLOC_FREE(params);
5036 tevent_req_nterror(
5037 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5038 return;
5041 state->changes[i].action = IVAL(params, ofs+4);
5042 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5043 &name, params+ofs+12, len,
5044 STR_TERMINATE|STR_UNICODE);
5045 if (ret == -1) {
5046 TALLOC_FREE(params);
5047 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5048 return;
5050 state->changes[i].name = name;
5051 ofs += next;
5054 TALLOC_FREE(params);
5055 tevent_req_done(req);
5058 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5059 uint32_t *pnum_changes,
5060 struct notify_change **pchanges)
5062 struct cli_notify_state *state = tevent_req_data(
5063 req, struct cli_notify_state);
5064 NTSTATUS status;
5066 if (tevent_req_is_nterror(req, &status)) {
5067 return status;
5070 *pnum_changes = state->num_changes;
5071 *pchanges = talloc_move(mem_ctx, &state->changes);
5072 return NT_STATUS_OK;
5075 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5076 uint32_t completion_filter, bool recursive,
5077 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5078 struct notify_change **pchanges)
5080 TALLOC_CTX *frame = talloc_stackframe();
5081 struct tevent_context *ev;
5082 struct tevent_req *req;
5083 NTSTATUS status = NT_STATUS_NO_MEMORY;
5085 if (smbXcli_conn_has_async_calls(cli->conn)) {
5087 * Can't use sync call while an async call is in flight
5089 status = NT_STATUS_INVALID_PARAMETER;
5090 goto fail;
5092 ev = tevent_context_init(frame);
5093 if (ev == NULL) {
5094 goto fail;
5096 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5097 completion_filter, recursive);
5098 if (req == NULL) {
5099 goto fail;
5101 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5102 goto fail;
5104 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5105 fail:
5106 TALLOC_FREE(frame);
5107 return status;
5110 struct cli_qpathinfo_state {
5111 uint8_t *param;
5112 uint8_t *data;
5113 uint16_t setup[1];
5114 uint32_t min_rdata;
5115 uint8_t *rdata;
5116 uint32_t num_rdata;
5119 static void cli_qpathinfo_done(struct tevent_req *subreq);
5121 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5122 struct tevent_context *ev,
5123 struct cli_state *cli, const char *fname,
5124 uint16_t level, uint32_t min_rdata,
5125 uint32_t max_rdata)
5127 struct tevent_req *req, *subreq;
5128 struct cli_qpathinfo_state *state;
5130 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5131 if (req == NULL) {
5132 return NULL;
5134 state->min_rdata = min_rdata;
5135 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5137 state->param = talloc_zero_array(state, uint8_t, 6);
5138 if (tevent_req_nomem(state->param, req)) {
5139 return tevent_req_post(req, ev);
5141 SSVAL(state->param, 0, level);
5142 state->param = trans2_bytes_push_str(
5143 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5144 if (tevent_req_nomem(state->param, req)) {
5145 return tevent_req_post(req, ev);
5148 subreq = cli_trans_send(
5149 state, /* mem ctx. */
5150 ev, /* event ctx. */
5151 cli, /* cli_state. */
5152 SMBtrans2, /* cmd. */
5153 NULL, /* pipe name. */
5154 -1, /* fid. */
5155 0, /* function. */
5156 0, /* flags. */
5157 state->setup, /* setup. */
5158 1, /* num setup uint16_t words. */
5159 0, /* max returned setup. */
5160 state->param, /* param. */
5161 talloc_get_size(state->param), /* num param. */
5162 2, /* max returned param. */
5163 NULL, /* data. */
5164 0, /* num data. */
5165 max_rdata); /* max returned data. */
5167 if (tevent_req_nomem(subreq, req)) {
5168 return tevent_req_post(req, ev);
5170 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5171 return req;
5174 static void cli_qpathinfo_done(struct tevent_req *subreq)
5176 struct tevent_req *req = tevent_req_callback_data(
5177 subreq, struct tevent_req);
5178 struct cli_qpathinfo_state *state = tevent_req_data(
5179 req, struct cli_qpathinfo_state);
5180 NTSTATUS status;
5182 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5183 NULL, 0, NULL,
5184 &state->rdata, state->min_rdata,
5185 &state->num_rdata);
5186 if (tevent_req_nterror(req, status)) {
5187 return;
5189 tevent_req_done(req);
5192 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5193 uint8_t **rdata, uint32_t *num_rdata)
5195 struct cli_qpathinfo_state *state = tevent_req_data(
5196 req, struct cli_qpathinfo_state);
5197 NTSTATUS status;
5199 if (tevent_req_is_nterror(req, &status)) {
5200 return status;
5202 if (rdata != NULL) {
5203 *rdata = talloc_move(mem_ctx, &state->rdata);
5204 } else {
5205 TALLOC_FREE(state->rdata);
5207 if (num_rdata != NULL) {
5208 *num_rdata = state->num_rdata;
5210 return NT_STATUS_OK;
5213 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5214 const char *fname, uint16_t level, uint32_t min_rdata,
5215 uint32_t max_rdata,
5216 uint8_t **rdata, uint32_t *num_rdata)
5218 TALLOC_CTX *frame = talloc_stackframe();
5219 struct event_context *ev;
5220 struct tevent_req *req;
5221 NTSTATUS status = NT_STATUS_NO_MEMORY;
5223 if (smbXcli_conn_has_async_calls(cli->conn)) {
5225 * Can't use sync call while an async call is in flight
5227 status = NT_STATUS_INVALID_PARAMETER;
5228 goto fail;
5230 ev = event_context_init(frame);
5231 if (ev == NULL) {
5232 goto fail;
5234 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5235 max_rdata);
5236 if (req == NULL) {
5237 goto fail;
5239 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5240 goto fail;
5242 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5243 fail:
5244 TALLOC_FREE(frame);
5245 return status;
5248 struct cli_qfileinfo_state {
5249 uint16_t setup[1];
5250 uint8_t param[4];
5251 uint8_t *data;
5252 uint16_t recv_flags2;
5253 uint32_t min_rdata;
5254 uint8_t *rdata;
5255 uint32_t num_rdata;
5258 static void cli_qfileinfo_done(struct tevent_req *subreq);
5260 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5261 struct tevent_context *ev,
5262 struct cli_state *cli, uint16_t fnum,
5263 uint16_t level, uint32_t min_rdata,
5264 uint32_t max_rdata)
5266 struct tevent_req *req, *subreq;
5267 struct cli_qfileinfo_state *state;
5269 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5270 if (req == NULL) {
5271 return NULL;
5273 state->min_rdata = min_rdata;
5274 SSVAL(state->param, 0, fnum);
5275 SSVAL(state->param, 2, level);
5276 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5278 subreq = cli_trans_send(
5279 state, /* mem ctx. */
5280 ev, /* event ctx. */
5281 cli, /* cli_state. */
5282 SMBtrans2, /* cmd. */
5283 NULL, /* pipe name. */
5284 -1, /* fid. */
5285 0, /* function. */
5286 0, /* flags. */
5287 state->setup, /* setup. */
5288 1, /* num setup uint16_t words. */
5289 0, /* max returned setup. */
5290 state->param, /* param. */
5291 sizeof(state->param), /* num param. */
5292 2, /* max returned param. */
5293 NULL, /* data. */
5294 0, /* num data. */
5295 max_rdata); /* max returned data. */
5297 if (tevent_req_nomem(subreq, req)) {
5298 return tevent_req_post(req, ev);
5300 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5301 return req;
5304 static void cli_qfileinfo_done(struct tevent_req *subreq)
5306 struct tevent_req *req = tevent_req_callback_data(
5307 subreq, struct tevent_req);
5308 struct cli_qfileinfo_state *state = tevent_req_data(
5309 req, struct cli_qfileinfo_state);
5310 NTSTATUS status;
5312 status = cli_trans_recv(subreq, state,
5313 &state->recv_flags2,
5314 NULL, 0, NULL,
5315 NULL, 0, NULL,
5316 &state->rdata, state->min_rdata,
5317 &state->num_rdata);
5318 if (tevent_req_nterror(req, status)) {
5319 return;
5321 tevent_req_done(req);
5324 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5325 uint16_t *recv_flags2,
5326 uint8_t **rdata, uint32_t *num_rdata)
5328 struct cli_qfileinfo_state *state = tevent_req_data(
5329 req, struct cli_qfileinfo_state);
5330 NTSTATUS status;
5332 if (tevent_req_is_nterror(req, &status)) {
5333 return status;
5336 if (recv_flags2 != NULL) {
5337 *recv_flags2 = state->recv_flags2;
5339 if (rdata != NULL) {
5340 *rdata = talloc_move(mem_ctx, &state->rdata);
5341 } else {
5342 TALLOC_FREE(state->rdata);
5344 if (num_rdata != NULL) {
5345 *num_rdata = state->num_rdata;
5347 return NT_STATUS_OK;
5350 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5351 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5352 uint32_t max_rdata, uint16_t *recv_flags2,
5353 uint8_t **rdata, uint32_t *num_rdata)
5355 TALLOC_CTX *frame = talloc_stackframe();
5356 struct event_context *ev;
5357 struct tevent_req *req;
5358 NTSTATUS status = NT_STATUS_NO_MEMORY;
5360 if (smbXcli_conn_has_async_calls(cli->conn)) {
5362 * Can't use sync call while an async call is in flight
5364 status = NT_STATUS_INVALID_PARAMETER;
5365 goto fail;
5367 ev = event_context_init(frame);
5368 if (ev == NULL) {
5369 goto fail;
5371 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5372 max_rdata);
5373 if (req == NULL) {
5374 goto fail;
5376 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5377 goto fail;
5379 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5380 fail:
5381 TALLOC_FREE(frame);
5382 return status;
5385 struct cli_flush_state {
5386 uint16_t vwv[1];
5389 static void cli_flush_done(struct tevent_req *subreq);
5391 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5392 struct event_context *ev,
5393 struct cli_state *cli,
5394 uint16_t fnum)
5396 struct tevent_req *req, *subreq;
5397 struct cli_flush_state *state;
5399 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5400 if (req == NULL) {
5401 return NULL;
5403 SSVAL(state->vwv + 0, 0, fnum);
5405 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5406 0, NULL);
5407 if (tevent_req_nomem(subreq, req)) {
5408 return tevent_req_post(req, ev);
5410 tevent_req_set_callback(subreq, cli_flush_done, req);
5411 return req;
5414 static void cli_flush_done(struct tevent_req *subreq)
5416 struct tevent_req *req = tevent_req_callback_data(
5417 subreq, struct tevent_req);
5418 NTSTATUS status;
5420 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5421 TALLOC_FREE(subreq);
5422 if (tevent_req_nterror(req, status)) {
5423 return;
5425 tevent_req_done(req);
5428 NTSTATUS cli_flush_recv(struct tevent_req *req)
5430 return tevent_req_simple_recv_ntstatus(req);
5433 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
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 (smbXcli_conn_has_async_calls(cli->conn)) {
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_flush_send(frame, ev, cli, fnum);
5452 if (req == NULL) {
5453 goto fail;
5455 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5456 goto fail;
5458 status = cli_flush_recv(req);
5459 fail:
5460 TALLOC_FREE(frame);
5461 return status;
5464 struct cli_shadow_copy_data_state {
5465 uint16_t setup[4];
5466 uint8_t *data;
5467 uint32_t num_data;
5468 bool get_names;
5471 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5473 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5474 struct tevent_context *ev,
5475 struct cli_state *cli,
5476 uint16_t fnum,
5477 bool get_names)
5479 struct tevent_req *req, *subreq;
5480 struct cli_shadow_copy_data_state *state;
5481 uint32_t ret_size;
5483 req = tevent_req_create(mem_ctx, &state,
5484 struct cli_shadow_copy_data_state);
5485 if (req == NULL) {
5486 return NULL;
5488 state->get_names = get_names;
5489 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5491 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5492 SSVAL(state->setup + 2, 0, fnum);
5493 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5494 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5496 subreq = cli_trans_send(
5497 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5498 state->setup, ARRAY_SIZE(state->setup), 0,
5499 NULL, 0, 0,
5500 NULL, 0, ret_size);
5501 if (tevent_req_nomem(subreq, req)) {
5502 return tevent_req_post(req, ev);
5504 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5505 return req;
5508 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5510 struct tevent_req *req = tevent_req_callback_data(
5511 subreq, struct tevent_req);
5512 struct cli_shadow_copy_data_state *state = tevent_req_data(
5513 req, struct cli_shadow_copy_data_state);
5514 NTSTATUS status;
5516 status = cli_trans_recv(subreq, state, NULL,
5517 NULL, 0, NULL, /* setup */
5518 NULL, 0, NULL, /* param */
5519 &state->data, 12, &state->num_data);
5520 TALLOC_FREE(subreq);
5521 if (tevent_req_nterror(req, status)) {
5522 return;
5524 tevent_req_done(req);
5527 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5528 char ***pnames, int *pnum_names)
5530 struct cli_shadow_copy_data_state *state = tevent_req_data(
5531 req, struct cli_shadow_copy_data_state);
5532 char **names;
5533 int i, num_names;
5534 uint32_t dlength;
5535 NTSTATUS status;
5537 if (tevent_req_is_nterror(req, &status)) {
5538 return status;
5540 num_names = IVAL(state->data, 4);
5541 dlength = IVAL(state->data, 8);
5543 if (!state->get_names) {
5544 *pnum_names = num_names;
5545 return NT_STATUS_OK;
5548 if (dlength+12 > state->num_data) {
5549 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5551 names = talloc_array(mem_ctx, char *, num_names);
5552 if (names == NULL) {
5553 return NT_STATUS_NO_MEMORY;
5556 for (i=0; i<num_names; i++) {
5557 bool ret;
5558 uint8_t *src;
5559 size_t converted_size;
5561 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5562 ret = convert_string_talloc(
5563 names, CH_UTF16LE, CH_UNIX,
5564 src, 2 * sizeof(SHADOW_COPY_LABEL),
5565 &names[i], &converted_size);
5566 if (!ret) {
5567 TALLOC_FREE(names);
5568 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5571 *pnum_names = num_names;
5572 *pnames = names;
5573 return NT_STATUS_OK;
5576 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5577 uint16_t fnum, bool get_names,
5578 char ***pnames, int *pnum_names)
5580 TALLOC_CTX *frame = talloc_stackframe();
5581 struct event_context *ev;
5582 struct tevent_req *req;
5583 NTSTATUS status = NT_STATUS_NO_MEMORY;
5585 if (smbXcli_conn_has_async_calls(cli->conn)) {
5587 * Can't use sync call while an async call is in flight
5589 status = NT_STATUS_INVALID_PARAMETER;
5590 goto fail;
5592 ev = event_context_init(frame);
5593 if (ev == NULL) {
5594 goto fail;
5596 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5597 if (req == NULL) {
5598 goto fail;
5600 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5601 goto fail;
5603 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5604 fail:
5605 TALLOC_FREE(frame);
5606 return status;