libsmb: remove smb2 switch from cli_ntcreate
[Samba.git] / source3 / libsmb / clifile.c
blobc8a36104c29e006b74618c2c568fce3490506211
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
27 #include "trans2.h"
28 #include "ntioctl.h"
29 #include "libcli/security/secdesc.h"
30 #include "../libcli/smb/smbXcli_base.h"
32 /***********************************************************
33 Common function for pushing stings, used by smb_bytes_push_str()
34 and trans_bytes_push_str(). Only difference is the align_odd
35 parameter setting.
36 ***********************************************************/
38 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
39 const char *str, size_t str_len,
40 bool align_odd,
41 size_t *pconverted_size)
43 size_t buflen;
44 char *converted;
45 size_t converted_size;
47 if (buf == NULL) {
48 return NULL;
51 buflen = talloc_get_size(buf);
53 if (ucs2 &&
54 ((align_odd && (buflen % 2 == 0)) ||
55 (!align_odd && (buflen % 2 == 1)))) {
57 * We're pushing into an SMB buffer, align odd
59 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
60 if (buf == NULL) {
61 return NULL;
63 buf[buflen] = '\0';
64 buflen += 1;
67 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
68 ucs2 ? CH_UTF16LE : CH_DOS,
69 str, str_len, &converted,
70 &converted_size)) {
71 return NULL;
74 buf = talloc_realloc(NULL, buf, uint8_t,
75 buflen + converted_size);
76 if (buf == NULL) {
77 TALLOC_FREE(converted);
78 return NULL;
81 memcpy(buf + buflen, converted, converted_size);
83 TALLOC_FREE(converted);
85 if (pconverted_size) {
86 *pconverted_size = converted_size;
89 return buf;
92 /***********************************************************
93 Push a string into an SMB buffer, with odd byte alignment
94 if it's a UCS2 string.
95 ***********************************************************/
97 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
98 const char *str, size_t str_len,
99 size_t *pconverted_size)
101 return internal_bytes_push_str(buf, ucs2, str, str_len,
102 true, pconverted_size);
105 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
106 const uint8_t *bytes, size_t num_bytes)
108 size_t buflen;
110 if (buf == NULL) {
111 return NULL;
113 buflen = talloc_get_size(buf);
115 buf = talloc_realloc(NULL, buf, uint8_t,
116 buflen + 1 + num_bytes);
117 if (buf == NULL) {
118 return NULL;
120 buf[buflen] = prefix;
121 memcpy(&buf[buflen+1], bytes, num_bytes);
122 return buf;
125 /***********************************************************
126 Same as smb_bytes_push_str(), but without the odd byte
127 align for ucs2 (we're pushing into a param or data block).
128 static for now, although this will probably change when
129 other modules use async trans calls.
130 ***********************************************************/
132 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
133 const char *str, size_t str_len,
134 size_t *pconverted_size)
136 return internal_bytes_push_str(buf, ucs2, str, str_len,
137 false, pconverted_size);
140 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
141 const uint8_t *bytes, size_t num_bytes)
143 size_t buflen;
145 if (buf == NULL) {
146 return NULL;
148 buflen = talloc_get_size(buf);
150 buf = talloc_realloc(NULL, buf, uint8_t,
151 buflen + num_bytes);
152 if (buf == NULL) {
153 return NULL;
155 memcpy(&buf[buflen], bytes, num_bytes);
156 return buf;
159 struct cli_setpathinfo_state {
160 uint16_t setup;
161 uint8_t *param;
164 static void cli_setpathinfo_done(struct tevent_req *subreq);
166 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
167 struct tevent_context *ev,
168 struct cli_state *cli,
169 uint16_t level,
170 const char *path,
171 uint8_t *data,
172 size_t data_len)
174 struct tevent_req *req, *subreq;
175 struct cli_setpathinfo_state *state;
177 req = tevent_req_create(mem_ctx, &state,
178 struct cli_setpathinfo_state);
179 if (req == NULL) {
180 return NULL;
183 /* Setup setup word. */
184 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
186 /* Setup param array. */
187 state->param = talloc_zero_array(state, uint8_t, 6);
188 if (tevent_req_nomem(state->param, req)) {
189 return tevent_req_post(req, ev);
191 SSVAL(state->param, 0, level);
193 state->param = trans2_bytes_push_str(
194 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
195 if (tevent_req_nomem(state->param, req)) {
196 return tevent_req_post(req, ev);
199 subreq = cli_trans_send(
200 state, /* mem ctx. */
201 ev, /* event ctx. */
202 cli, /* cli_state. */
203 SMBtrans2, /* cmd. */
204 NULL, /* pipe name. */
205 -1, /* fid. */
206 0, /* function. */
207 0, /* flags. */
208 &state->setup, /* setup. */
209 1, /* num setup uint16_t words. */
210 0, /* max returned setup. */
211 state->param, /* param. */
212 talloc_get_size(state->param), /* num param. */
213 2, /* max returned param. */
214 data, /* data. */
215 data_len, /* num data. */
216 0); /* max returned data. */
218 if (tevent_req_nomem(subreq, req)) {
219 return tevent_req_post(req, ev);
221 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
222 return req;
225 static void cli_setpathinfo_done(struct tevent_req *subreq)
227 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
228 NULL, 0, NULL, NULL, 0, NULL);
229 tevent_req_simple_finish_ntstatus(subreq, status);
232 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
234 return tevent_req_simple_recv_ntstatus(req);
237 NTSTATUS cli_setpathinfo(struct cli_state *cli,
238 uint16_t level,
239 const char *path,
240 uint8_t *data,
241 size_t data_len)
243 TALLOC_CTX *frame = talloc_stackframe();
244 struct tevent_context *ev;
245 struct tevent_req *req;
246 NTSTATUS status = NT_STATUS_NO_MEMORY;
248 if (smbXcli_conn_has_async_calls(cli->conn)) {
250 * Can't use sync call while an async call is in flight
252 status = NT_STATUS_INVALID_PARAMETER;
253 goto fail;
255 ev = samba_tevent_context_init(frame);
256 if (ev == NULL) {
257 goto fail;
259 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
260 if (req == NULL) {
261 goto fail;
263 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
264 goto fail;
266 status = cli_setpathinfo_recv(req);
267 fail:
268 TALLOC_FREE(frame);
269 return status;
272 /****************************************************************************
273 Hard/Symlink a file (UNIX extensions).
274 Creates new name (sym)linked to oldname.
275 ****************************************************************************/
277 struct cli_posix_link_internal_state {
278 uint8_t *data;
281 static void cli_posix_link_internal_done(struct tevent_req *subreq);
283 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
284 struct tevent_context *ev,
285 struct cli_state *cli,
286 uint16_t level,
287 const char *oldname,
288 const char *newname)
290 struct tevent_req *req = NULL, *subreq = NULL;
291 struct cli_posix_link_internal_state *state = NULL;
293 req = tevent_req_create(mem_ctx, &state,
294 struct cli_posix_link_internal_state);
295 if (req == NULL) {
296 return NULL;
299 /* Setup data array. */
300 state->data = talloc_array(state, uint8_t, 0);
301 if (tevent_req_nomem(state->data, req)) {
302 return tevent_req_post(req, ev);
304 state->data = trans2_bytes_push_str(
305 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
307 subreq = cli_setpathinfo_send(
308 state, ev, cli, level, newname,
309 state->data, talloc_get_size(state->data));
310 if (tevent_req_nomem(subreq, req)) {
311 return tevent_req_post(req, ev);
313 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
314 return req;
317 static void cli_posix_link_internal_done(struct tevent_req *subreq)
319 NTSTATUS status = cli_setpathinfo_recv(subreq);
320 tevent_req_simple_finish_ntstatus(subreq, status);
323 /****************************************************************************
324 Symlink a file (UNIX extensions).
325 ****************************************************************************/
327 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
328 struct tevent_context *ev,
329 struct cli_state *cli,
330 const char *oldname,
331 const char *newname)
333 return cli_posix_link_internal_send(
334 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
337 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
339 return tevent_req_simple_recv_ntstatus(req);
342 NTSTATUS cli_posix_symlink(struct cli_state *cli,
343 const char *oldname,
344 const char *newname)
346 TALLOC_CTX *frame = talloc_stackframe();
347 struct tevent_context *ev = NULL;
348 struct tevent_req *req = NULL;
349 NTSTATUS status = NT_STATUS_OK;
351 if (smbXcli_conn_has_async_calls(cli->conn)) {
353 * Can't use sync call while an async call is in flight
355 status = NT_STATUS_INVALID_PARAMETER;
356 goto fail;
359 ev = samba_tevent_context_init(frame);
360 if (ev == NULL) {
361 status = NT_STATUS_NO_MEMORY;
362 goto fail;
365 req = cli_posix_symlink_send(frame,
367 cli,
368 oldname,
369 newname);
370 if (req == NULL) {
371 status = NT_STATUS_NO_MEMORY;
372 goto fail;
375 if (!tevent_req_poll(req, ev)) {
376 status = map_nt_error_from_unix(errno);
377 goto fail;
380 status = cli_posix_symlink_recv(req);
382 fail:
383 TALLOC_FREE(frame);
384 return status;
387 /****************************************************************************
388 Read a POSIX symlink.
389 ****************************************************************************/
391 struct readlink_state {
392 uint8_t *data;
393 uint32_t num_data;
396 static void cli_posix_readlink_done(struct tevent_req *subreq);
398 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
399 struct tevent_context *ev,
400 struct cli_state *cli,
401 const char *fname,
402 size_t len)
404 struct tevent_req *req = NULL, *subreq = NULL;
405 struct readlink_state *state = NULL;
406 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
408 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
409 if (req == NULL) {
410 return NULL;
414 * Len is in bytes, we need it in UCS2 units.
416 if ((2*len < len) || (maxbytelen < len)) {
417 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
418 return tevent_req_post(req, ev);
421 subreq = cli_qpathinfo_send(state, ev, cli, fname,
422 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
423 if (tevent_req_nomem(subreq, req)) {
424 return tevent_req_post(req, ev);
426 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
427 return req;
430 static void cli_posix_readlink_done(struct tevent_req *subreq)
432 struct tevent_req *req = tevent_req_callback_data(
433 subreq, struct tevent_req);
434 struct readlink_state *state = tevent_req_data(
435 req, struct readlink_state);
436 NTSTATUS status;
438 status = cli_qpathinfo_recv(subreq, state, &state->data,
439 &state->num_data);
440 TALLOC_FREE(subreq);
441 if (tevent_req_nterror(req, status)) {
442 return;
445 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
447 if (state->data[state->num_data-1] != '\0') {
448 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
449 return;
451 tevent_req_done(req);
454 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
455 char *retpath, size_t len)
457 NTSTATUS status;
458 char *converted = NULL;
459 size_t converted_size = 0;
460 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
462 if (tevent_req_is_nterror(req, &status)) {
463 return status;
465 /* The returned data is a pushed string, not raw data. */
466 if (!convert_string_talloc(state,
467 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
468 CH_UNIX,
469 state->data,
470 state->num_data,
471 &converted,
472 &converted_size)) {
473 return NT_STATUS_NO_MEMORY;
476 len = MIN(len,converted_size);
477 if (len == 0) {
478 return NT_STATUS_DATA_ERROR;
480 memcpy(retpath, converted, len);
481 return NT_STATUS_OK;
484 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
485 char *linkpath, size_t len)
487 TALLOC_CTX *frame = talloc_stackframe();
488 struct tevent_context *ev = NULL;
489 struct tevent_req *req = NULL;
490 NTSTATUS status = NT_STATUS_OK;
492 if (smbXcli_conn_has_async_calls(cli->conn)) {
494 * Can't use sync call while an async call is in flight
496 status = NT_STATUS_INVALID_PARAMETER;
497 goto fail;
500 ev = samba_tevent_context_init(frame);
501 if (ev == NULL) {
502 status = NT_STATUS_NO_MEMORY;
503 goto fail;
506 req = cli_posix_readlink_send(frame,
508 cli,
509 fname,
510 len);
511 if (req == NULL) {
512 status = NT_STATUS_NO_MEMORY;
513 goto fail;
516 if (!tevent_req_poll(req, ev)) {
517 status = map_nt_error_from_unix(errno);
518 goto fail;
521 status = cli_posix_readlink_recv(req, cli, linkpath, len);
523 fail:
524 TALLOC_FREE(frame);
525 return status;
528 /****************************************************************************
529 Hard link a file (UNIX extensions).
530 ****************************************************************************/
532 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
533 struct tevent_context *ev,
534 struct cli_state *cli,
535 const char *oldname,
536 const char *newname)
538 return cli_posix_link_internal_send(
539 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
542 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
544 return tevent_req_simple_recv_ntstatus(req);
547 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
548 const char *oldname,
549 const char *newname)
551 TALLOC_CTX *frame = talloc_stackframe();
552 struct tevent_context *ev = NULL;
553 struct tevent_req *req = NULL;
554 NTSTATUS status = NT_STATUS_OK;
556 if (smbXcli_conn_has_async_calls(cli->conn)) {
558 * Can't use sync call while an async call is in flight
560 status = NT_STATUS_INVALID_PARAMETER;
561 goto fail;
564 ev = samba_tevent_context_init(frame);
565 if (ev == NULL) {
566 status = NT_STATUS_NO_MEMORY;
567 goto fail;
570 req = cli_posix_hardlink_send(frame,
572 cli,
573 oldname,
574 newname);
575 if (req == NULL) {
576 status = NT_STATUS_NO_MEMORY;
577 goto fail;
580 if (!tevent_req_poll(req, ev)) {
581 status = map_nt_error_from_unix(errno);
582 goto fail;
585 status = cli_posix_hardlink_recv(req);
587 fail:
588 TALLOC_FREE(frame);
589 return status;
592 /****************************************************************************
593 Do a POSIX getfacl (UNIX extensions).
594 ****************************************************************************/
596 struct getfacl_state {
597 uint32_t num_data;
598 uint8_t *data;
601 static void cli_posix_getfacl_done(struct tevent_req *subreq);
603 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
604 struct tevent_context *ev,
605 struct cli_state *cli,
606 const char *fname)
608 struct tevent_req *req = NULL, *subreq = NULL;
609 struct getfacl_state *state = NULL;
611 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
612 if (req == NULL) {
613 return NULL;
615 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
616 0, CLI_BUFFER_SIZE);
617 if (tevent_req_nomem(subreq, req)) {
618 return tevent_req_post(req, ev);
620 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
621 return req;
624 static void cli_posix_getfacl_done(struct tevent_req *subreq)
626 struct tevent_req *req = tevent_req_callback_data(
627 subreq, struct tevent_req);
628 struct getfacl_state *state = tevent_req_data(
629 req, struct getfacl_state);
630 NTSTATUS status;
632 status = cli_qpathinfo_recv(subreq, state, &state->data,
633 &state->num_data);
634 TALLOC_FREE(subreq);
635 if (tevent_req_nterror(req, status)) {
636 return;
638 tevent_req_done(req);
641 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
642 TALLOC_CTX *mem_ctx,
643 size_t *prb_size,
644 char **retbuf)
646 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
647 NTSTATUS status;
649 if (tevent_req_is_nterror(req, &status)) {
650 return status;
652 *prb_size = (size_t)state->num_data;
653 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
654 return NT_STATUS_OK;
657 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
658 const char *fname,
659 TALLOC_CTX *mem_ctx,
660 size_t *prb_size,
661 char **retbuf)
663 TALLOC_CTX *frame = talloc_stackframe();
664 struct tevent_context *ev = NULL;
665 struct tevent_req *req = NULL;
666 NTSTATUS status = NT_STATUS_OK;
668 if (smbXcli_conn_has_async_calls(cli->conn)) {
670 * Can't use sync call while an async call is in flight
672 status = NT_STATUS_INVALID_PARAMETER;
673 goto fail;
676 ev = samba_tevent_context_init(frame);
677 if (ev == NULL) {
678 status = NT_STATUS_NO_MEMORY;
679 goto fail;
682 req = cli_posix_getfacl_send(frame,
684 cli,
685 fname);
686 if (req == NULL) {
687 status = NT_STATUS_NO_MEMORY;
688 goto fail;
691 if (!tevent_req_poll(req, ev)) {
692 status = map_nt_error_from_unix(errno);
693 goto fail;
696 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
698 fail:
699 TALLOC_FREE(frame);
700 return status;
703 /****************************************************************************
704 Stat a file (UNIX extensions).
705 ****************************************************************************/
707 struct stat_state {
708 uint32_t num_data;
709 uint8_t *data;
712 static void cli_posix_stat_done(struct tevent_req *subreq);
714 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
715 struct tevent_context *ev,
716 struct cli_state *cli,
717 const char *fname)
719 struct tevent_req *req = NULL, *subreq = NULL;
720 struct stat_state *state = NULL;
722 req = tevent_req_create(mem_ctx, &state, struct stat_state);
723 if (req == NULL) {
724 return NULL;
726 subreq = cli_qpathinfo_send(state, ev, cli, fname,
727 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
728 if (tevent_req_nomem(subreq, req)) {
729 return tevent_req_post(req, ev);
731 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
732 return req;
735 static void cli_posix_stat_done(struct tevent_req *subreq)
737 struct tevent_req *req = tevent_req_callback_data(
738 subreq, struct tevent_req);
739 struct stat_state *state = tevent_req_data(req, struct stat_state);
740 NTSTATUS status;
742 status = cli_qpathinfo_recv(subreq, state, &state->data,
743 &state->num_data);
744 TALLOC_FREE(subreq);
745 if (tevent_req_nterror(req, status)) {
746 return;
748 tevent_req_done(req);
751 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
752 SMB_STRUCT_STAT *sbuf)
754 struct stat_state *state = tevent_req_data(req, struct stat_state);
755 NTSTATUS status;
757 if (tevent_req_is_nterror(req, &status)) {
758 return status;
761 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
762 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
763 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
764 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
765 #else
766 /* assume 512 byte blocks */
767 sbuf->st_ex_blocks /= 512;
768 #endif
769 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
770 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
771 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
773 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
774 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
775 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
776 #if defined(HAVE_MAKEDEV)
778 uint32_t dev_major = IVAL(state->data,60);
779 uint32_t dev_minor = IVAL(state->data,68);
780 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
782 #endif
783 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
784 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
785 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
787 return NT_STATUS_OK;
790 NTSTATUS cli_posix_stat(struct cli_state *cli,
791 const char *fname,
792 SMB_STRUCT_STAT *sbuf)
794 TALLOC_CTX *frame = talloc_stackframe();
795 struct tevent_context *ev = NULL;
796 struct tevent_req *req = NULL;
797 NTSTATUS status = NT_STATUS_OK;
799 if (smbXcli_conn_has_async_calls(cli->conn)) {
801 * Can't use sync call while an async call is in flight
803 status = NT_STATUS_INVALID_PARAMETER;
804 goto fail;
807 ev = samba_tevent_context_init(frame);
808 if (ev == NULL) {
809 status = NT_STATUS_NO_MEMORY;
810 goto fail;
813 req = cli_posix_stat_send(frame,
815 cli,
816 fname);
817 if (req == NULL) {
818 status = NT_STATUS_NO_MEMORY;
819 goto fail;
822 if (!tevent_req_poll(req, ev)) {
823 status = map_nt_error_from_unix(errno);
824 goto fail;
827 status = cli_posix_stat_recv(req, sbuf);
829 fail:
830 TALLOC_FREE(frame);
831 return status;
834 /****************************************************************************
835 Chmod or chown a file internal (UNIX extensions).
836 ****************************************************************************/
838 struct cli_posix_chown_chmod_internal_state {
839 uint8_t data[100];
842 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
844 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
845 struct tevent_context *ev,
846 struct cli_state *cli,
847 const char *fname,
848 uint32_t mode,
849 uint32_t uid,
850 uint32_t gid)
852 struct tevent_req *req = NULL, *subreq = NULL;
853 struct cli_posix_chown_chmod_internal_state *state = NULL;
855 req = tevent_req_create(mem_ctx, &state,
856 struct cli_posix_chown_chmod_internal_state);
857 if (req == NULL) {
858 return NULL;
861 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
862 memset(&state->data[40], '\0', 60);
863 SIVAL(state->data,40,uid);
864 SIVAL(state->data,48,gid);
865 SIVAL(state->data,84,mode);
867 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
868 fname, state->data, sizeof(state->data));
869 if (tevent_req_nomem(subreq, req)) {
870 return tevent_req_post(req, ev);
872 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
873 req);
874 return req;
877 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
879 NTSTATUS status = cli_setpathinfo_recv(subreq);
880 tevent_req_simple_finish_ntstatus(subreq, status);
883 /****************************************************************************
884 chmod a file (UNIX extensions).
885 ****************************************************************************/
887 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
888 struct tevent_context *ev,
889 struct cli_state *cli,
890 const char *fname,
891 mode_t mode)
893 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
894 fname,
895 unix_perms_to_wire(mode),
896 SMB_UID_NO_CHANGE,
897 SMB_GID_NO_CHANGE);
900 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
902 return tevent_req_simple_recv_ntstatus(req);
905 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
907 TALLOC_CTX *frame = talloc_stackframe();
908 struct tevent_context *ev = NULL;
909 struct tevent_req *req = NULL;
910 NTSTATUS status = NT_STATUS_OK;
912 if (smbXcli_conn_has_async_calls(cli->conn)) {
914 * Can't use sync call while an async call is in flight
916 status = NT_STATUS_INVALID_PARAMETER;
917 goto fail;
920 ev = samba_tevent_context_init(frame);
921 if (ev == NULL) {
922 status = NT_STATUS_NO_MEMORY;
923 goto fail;
926 req = cli_posix_chmod_send(frame,
928 cli,
929 fname,
930 mode);
931 if (req == NULL) {
932 status = NT_STATUS_NO_MEMORY;
933 goto fail;
936 if (!tevent_req_poll(req, ev)) {
937 status = map_nt_error_from_unix(errno);
938 goto fail;
941 status = cli_posix_chmod_recv(req);
943 fail:
944 TALLOC_FREE(frame);
945 return status;
948 /****************************************************************************
949 chown a file (UNIX extensions).
950 ****************************************************************************/
952 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
953 struct tevent_context *ev,
954 struct cli_state *cli,
955 const char *fname,
956 uid_t uid,
957 gid_t gid)
959 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
960 fname,
961 SMB_MODE_NO_CHANGE,
962 (uint32_t)uid,
963 (uint32_t)gid);
966 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
968 return tevent_req_simple_recv_ntstatus(req);
971 NTSTATUS cli_posix_chown(struct cli_state *cli,
972 const char *fname,
973 uid_t uid,
974 gid_t gid)
976 TALLOC_CTX *frame = talloc_stackframe();
977 struct tevent_context *ev = NULL;
978 struct tevent_req *req = NULL;
979 NTSTATUS status = NT_STATUS_OK;
981 if (smbXcli_conn_has_async_calls(cli->conn)) {
983 * Can't use sync call while an async call is in flight
985 status = NT_STATUS_INVALID_PARAMETER;
986 goto fail;
989 ev = samba_tevent_context_init(frame);
990 if (ev == NULL) {
991 status = NT_STATUS_NO_MEMORY;
992 goto fail;
995 req = cli_posix_chown_send(frame,
997 cli,
998 fname,
999 uid,
1000 gid);
1001 if (req == NULL) {
1002 status = NT_STATUS_NO_MEMORY;
1003 goto fail;
1006 if (!tevent_req_poll(req, ev)) {
1007 status = map_nt_error_from_unix(errno);
1008 goto fail;
1011 status = cli_posix_chown_recv(req);
1013 fail:
1014 TALLOC_FREE(frame);
1015 return status;
1018 /****************************************************************************
1019 Rename a file.
1020 ****************************************************************************/
1022 static void cli_rename_done(struct tevent_req *subreq);
1024 struct cli_rename_state {
1025 uint16_t vwv[1];
1028 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1029 struct tevent_context *ev,
1030 struct cli_state *cli,
1031 const char *fname_src,
1032 const char *fname_dst)
1034 struct tevent_req *req = NULL, *subreq = NULL;
1035 struct cli_rename_state *state = NULL;
1036 uint8_t additional_flags = 0;
1037 uint8_t *bytes = NULL;
1039 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1040 if (req == NULL) {
1041 return NULL;
1044 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1046 bytes = talloc_array(state, uint8_t, 1);
1047 if (tevent_req_nomem(bytes, req)) {
1048 return tevent_req_post(req, ev);
1050 bytes[0] = 4;
1051 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1052 strlen(fname_src)+1, NULL);
1053 if (tevent_req_nomem(bytes, req)) {
1054 return tevent_req_post(req, ev);
1057 bytes = talloc_realloc(state, bytes, uint8_t,
1058 talloc_get_size(bytes)+1);
1059 if (tevent_req_nomem(bytes, req)) {
1060 return tevent_req_post(req, ev);
1063 bytes[talloc_get_size(bytes)-1] = 4;
1064 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1065 strlen(fname_dst)+1, NULL);
1066 if (tevent_req_nomem(bytes, req)) {
1067 return tevent_req_post(req, ev);
1070 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1071 1, state->vwv, talloc_get_size(bytes), bytes);
1072 if (tevent_req_nomem(subreq, req)) {
1073 return tevent_req_post(req, ev);
1075 tevent_req_set_callback(subreq, cli_rename_done, req);
1076 return req;
1079 static void cli_rename_done(struct tevent_req *subreq)
1081 struct tevent_req *req = tevent_req_callback_data(
1082 subreq, struct tevent_req);
1083 NTSTATUS status;
1085 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1086 TALLOC_FREE(subreq);
1087 if (tevent_req_nterror(req, status)) {
1088 return;
1090 tevent_req_done(req);
1093 NTSTATUS cli_rename_recv(struct tevent_req *req)
1095 return tevent_req_simple_recv_ntstatus(req);
1098 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1100 TALLOC_CTX *frame = NULL;
1101 struct tevent_context *ev;
1102 struct tevent_req *req;
1103 NTSTATUS status = NT_STATUS_OK;
1105 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1106 return cli_smb2_rename(cli,
1107 fname_src,
1108 fname_dst);
1111 frame = talloc_stackframe();
1113 if (smbXcli_conn_has_async_calls(cli->conn)) {
1115 * Can't use sync call while an async call is in flight
1117 status = NT_STATUS_INVALID_PARAMETER;
1118 goto fail;
1121 ev = samba_tevent_context_init(frame);
1122 if (ev == NULL) {
1123 status = NT_STATUS_NO_MEMORY;
1124 goto fail;
1127 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1128 if (req == NULL) {
1129 status = NT_STATUS_NO_MEMORY;
1130 goto fail;
1133 if (!tevent_req_poll(req, ev)) {
1134 status = map_nt_error_from_unix(errno);
1135 goto fail;
1138 status = cli_rename_recv(req);
1140 fail:
1141 TALLOC_FREE(frame);
1142 return status;
1145 /****************************************************************************
1146 NT Rename a file.
1147 ****************************************************************************/
1149 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1151 struct cli_ntrename_internal_state {
1152 uint16_t vwv[4];
1155 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1156 struct tevent_context *ev,
1157 struct cli_state *cli,
1158 const char *fname_src,
1159 const char *fname_dst,
1160 uint16_t rename_flag)
1162 struct tevent_req *req = NULL, *subreq = NULL;
1163 struct cli_ntrename_internal_state *state = NULL;
1164 uint8_t additional_flags = 0;
1165 uint8_t *bytes = NULL;
1167 req = tevent_req_create(mem_ctx, &state,
1168 struct cli_ntrename_internal_state);
1169 if (req == NULL) {
1170 return NULL;
1173 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1174 SSVAL(state->vwv+1, 0, rename_flag);
1176 bytes = talloc_array(state, uint8_t, 1);
1177 if (tevent_req_nomem(bytes, req)) {
1178 return tevent_req_post(req, ev);
1180 bytes[0] = 4;
1181 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1182 strlen(fname_src)+1, NULL);
1183 if (tevent_req_nomem(bytes, req)) {
1184 return tevent_req_post(req, ev);
1187 bytes = talloc_realloc(state, bytes, uint8_t,
1188 talloc_get_size(bytes)+1);
1189 if (tevent_req_nomem(bytes, req)) {
1190 return tevent_req_post(req, ev);
1193 bytes[talloc_get_size(bytes)-1] = 4;
1194 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1195 strlen(fname_dst)+1, NULL);
1196 if (tevent_req_nomem(bytes, req)) {
1197 return tevent_req_post(req, ev);
1200 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1201 4, state->vwv, talloc_get_size(bytes), bytes);
1202 if (tevent_req_nomem(subreq, req)) {
1203 return tevent_req_post(req, ev);
1205 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1206 return req;
1209 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1211 struct tevent_req *req = tevent_req_callback_data(
1212 subreq, struct tevent_req);
1213 NTSTATUS status;
1215 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1216 TALLOC_FREE(subreq);
1217 if (tevent_req_nterror(req, status)) {
1218 return;
1220 tevent_req_done(req);
1223 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1225 return tevent_req_simple_recv_ntstatus(req);
1228 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1229 struct tevent_context *ev,
1230 struct cli_state *cli,
1231 const char *fname_src,
1232 const char *fname_dst)
1234 return cli_ntrename_internal_send(mem_ctx,
1236 cli,
1237 fname_src,
1238 fname_dst,
1239 RENAME_FLAG_RENAME);
1242 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1244 return cli_ntrename_internal_recv(req);
1247 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1249 TALLOC_CTX *frame = talloc_stackframe();
1250 struct tevent_context *ev;
1251 struct tevent_req *req;
1252 NTSTATUS status = NT_STATUS_OK;
1254 if (smbXcli_conn_has_async_calls(cli->conn)) {
1256 * Can't use sync call while an async call is in flight
1258 status = NT_STATUS_INVALID_PARAMETER;
1259 goto fail;
1262 ev = samba_tevent_context_init(frame);
1263 if (ev == NULL) {
1264 status = NT_STATUS_NO_MEMORY;
1265 goto fail;
1268 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1269 if (req == NULL) {
1270 status = NT_STATUS_NO_MEMORY;
1271 goto fail;
1274 if (!tevent_req_poll(req, ev)) {
1275 status = map_nt_error_from_unix(errno);
1276 goto fail;
1279 status = cli_ntrename_recv(req);
1281 fail:
1282 TALLOC_FREE(frame);
1283 return status;
1286 /****************************************************************************
1287 NT hardlink a file.
1288 ****************************************************************************/
1290 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1291 struct tevent_context *ev,
1292 struct cli_state *cli,
1293 const char *fname_src,
1294 const char *fname_dst)
1296 return cli_ntrename_internal_send(mem_ctx,
1298 cli,
1299 fname_src,
1300 fname_dst,
1301 RENAME_FLAG_HARD_LINK);
1304 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1306 return cli_ntrename_internal_recv(req);
1309 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1311 TALLOC_CTX *frame = talloc_stackframe();
1312 struct tevent_context *ev;
1313 struct tevent_req *req;
1314 NTSTATUS status = NT_STATUS_OK;
1316 if (smbXcli_conn_has_async_calls(cli->conn)) {
1318 * Can't use sync call while an async call is in flight
1320 status = NT_STATUS_INVALID_PARAMETER;
1321 goto fail;
1324 ev = samba_tevent_context_init(frame);
1325 if (ev == NULL) {
1326 status = NT_STATUS_NO_MEMORY;
1327 goto fail;
1330 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1331 if (req == NULL) {
1332 status = NT_STATUS_NO_MEMORY;
1333 goto fail;
1336 if (!tevent_req_poll(req, ev)) {
1337 status = map_nt_error_from_unix(errno);
1338 goto fail;
1341 status = cli_nt_hardlink_recv(req);
1343 fail:
1344 TALLOC_FREE(frame);
1345 return status;
1348 /****************************************************************************
1349 Delete a file.
1350 ****************************************************************************/
1352 static void cli_unlink_done(struct tevent_req *subreq);
1354 struct cli_unlink_state {
1355 uint16_t vwv[1];
1358 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1359 struct tevent_context *ev,
1360 struct cli_state *cli,
1361 const char *fname,
1362 uint16_t mayhave_attrs)
1364 struct tevent_req *req = NULL, *subreq = NULL;
1365 struct cli_unlink_state *state = NULL;
1366 uint8_t additional_flags = 0;
1367 uint8_t *bytes = NULL;
1369 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1370 if (req == NULL) {
1371 return NULL;
1374 SSVAL(state->vwv+0, 0, mayhave_attrs);
1376 bytes = talloc_array(state, uint8_t, 1);
1377 if (tevent_req_nomem(bytes, req)) {
1378 return tevent_req_post(req, ev);
1380 bytes[0] = 4;
1381 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1382 strlen(fname)+1, NULL);
1384 if (tevent_req_nomem(bytes, req)) {
1385 return tevent_req_post(req, ev);
1388 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1389 1, state->vwv, talloc_get_size(bytes), bytes);
1390 if (tevent_req_nomem(subreq, req)) {
1391 return tevent_req_post(req, ev);
1393 tevent_req_set_callback(subreq, cli_unlink_done, req);
1394 return req;
1397 static void cli_unlink_done(struct tevent_req *subreq)
1399 struct tevent_req *req = tevent_req_callback_data(
1400 subreq, struct tevent_req);
1401 NTSTATUS status;
1403 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1404 TALLOC_FREE(subreq);
1405 if (tevent_req_nterror(req, status)) {
1406 return;
1408 tevent_req_done(req);
1411 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1413 return tevent_req_simple_recv_ntstatus(req);
1416 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1418 TALLOC_CTX *frame = NULL;
1419 struct tevent_context *ev;
1420 struct tevent_req *req;
1421 NTSTATUS status = NT_STATUS_OK;
1423 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1424 return cli_smb2_unlink(cli, fname);
1427 frame = talloc_stackframe();
1429 if (smbXcli_conn_has_async_calls(cli->conn)) {
1431 * Can't use sync call while an async call is in flight
1433 status = NT_STATUS_INVALID_PARAMETER;
1434 goto fail;
1437 ev = samba_tevent_context_init(frame);
1438 if (ev == NULL) {
1439 status = NT_STATUS_NO_MEMORY;
1440 goto fail;
1443 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1444 if (req == NULL) {
1445 status = NT_STATUS_NO_MEMORY;
1446 goto fail;
1449 if (!tevent_req_poll(req, ev)) {
1450 status = map_nt_error_from_unix(errno);
1451 goto fail;
1454 status = cli_unlink_recv(req);
1456 fail:
1457 TALLOC_FREE(frame);
1458 return status;
1461 /****************************************************************************
1462 Create a directory.
1463 ****************************************************************************/
1465 static void cli_mkdir_done(struct tevent_req *subreq);
1467 struct cli_mkdir_state {
1468 int dummy;
1471 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1472 struct tevent_context *ev,
1473 struct cli_state *cli,
1474 const char *dname)
1476 struct tevent_req *req = NULL, *subreq = NULL;
1477 struct cli_mkdir_state *state = NULL;
1478 uint8_t additional_flags = 0;
1479 uint8_t *bytes = NULL;
1481 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1482 if (req == NULL) {
1483 return NULL;
1486 bytes = talloc_array(state, uint8_t, 1);
1487 if (tevent_req_nomem(bytes, req)) {
1488 return tevent_req_post(req, ev);
1490 bytes[0] = 4;
1491 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1492 strlen(dname)+1, NULL);
1494 if (tevent_req_nomem(bytes, req)) {
1495 return tevent_req_post(req, ev);
1498 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1499 0, NULL, talloc_get_size(bytes), bytes);
1500 if (tevent_req_nomem(subreq, req)) {
1501 return tevent_req_post(req, ev);
1503 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1504 return req;
1507 static void cli_mkdir_done(struct tevent_req *subreq)
1509 struct tevent_req *req = tevent_req_callback_data(
1510 subreq, struct tevent_req);
1511 NTSTATUS status;
1513 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1514 TALLOC_FREE(subreq);
1515 if (tevent_req_nterror(req, status)) {
1516 return;
1518 tevent_req_done(req);
1521 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1523 return tevent_req_simple_recv_ntstatus(req);
1526 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1528 TALLOC_CTX *frame = NULL;
1529 struct tevent_context *ev;
1530 struct tevent_req *req;
1531 NTSTATUS status = NT_STATUS_OK;
1533 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1534 return cli_smb2_mkdir(cli, dname);
1537 frame = talloc_stackframe();
1539 if (smbXcli_conn_has_async_calls(cli->conn)) {
1541 * Can't use sync call while an async call is in flight
1543 status = NT_STATUS_INVALID_PARAMETER;
1544 goto fail;
1547 ev = samba_tevent_context_init(frame);
1548 if (ev == NULL) {
1549 status = NT_STATUS_NO_MEMORY;
1550 goto fail;
1553 req = cli_mkdir_send(frame, ev, cli, dname);
1554 if (req == NULL) {
1555 status = NT_STATUS_NO_MEMORY;
1556 goto fail;
1559 if (!tevent_req_poll(req, ev)) {
1560 status = map_nt_error_from_unix(errno);
1561 goto fail;
1564 status = cli_mkdir_recv(req);
1566 fail:
1567 TALLOC_FREE(frame);
1568 return status;
1571 /****************************************************************************
1572 Remove a directory.
1573 ****************************************************************************/
1575 static void cli_rmdir_done(struct tevent_req *subreq);
1577 struct cli_rmdir_state {
1578 int dummy;
1581 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1582 struct tevent_context *ev,
1583 struct cli_state *cli,
1584 const char *dname)
1586 struct tevent_req *req = NULL, *subreq = NULL;
1587 struct cli_rmdir_state *state = NULL;
1588 uint8_t additional_flags = 0;
1589 uint8_t *bytes = NULL;
1591 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1592 if (req == NULL) {
1593 return NULL;
1596 bytes = talloc_array(state, uint8_t, 1);
1597 if (tevent_req_nomem(bytes, req)) {
1598 return tevent_req_post(req, ev);
1600 bytes[0] = 4;
1601 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1602 strlen(dname)+1, NULL);
1604 if (tevent_req_nomem(bytes, req)) {
1605 return tevent_req_post(req, ev);
1608 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1609 0, NULL, talloc_get_size(bytes), bytes);
1610 if (tevent_req_nomem(subreq, req)) {
1611 return tevent_req_post(req, ev);
1613 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1614 return req;
1617 static void cli_rmdir_done(struct tevent_req *subreq)
1619 struct tevent_req *req = tevent_req_callback_data(
1620 subreq, struct tevent_req);
1621 NTSTATUS status;
1623 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1624 TALLOC_FREE(subreq);
1625 if (tevent_req_nterror(req, status)) {
1626 return;
1628 tevent_req_done(req);
1631 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1633 return tevent_req_simple_recv_ntstatus(req);
1636 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1638 TALLOC_CTX *frame = NULL;
1639 struct tevent_context *ev;
1640 struct tevent_req *req;
1641 NTSTATUS status = NT_STATUS_OK;
1643 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1644 return cli_smb2_rmdir(cli, dname);
1647 frame = talloc_stackframe();
1649 if (smbXcli_conn_has_async_calls(cli->conn)) {
1651 * Can't use sync call while an async call is in flight
1653 status = NT_STATUS_INVALID_PARAMETER;
1654 goto fail;
1657 ev = samba_tevent_context_init(frame);
1658 if (ev == NULL) {
1659 status = NT_STATUS_NO_MEMORY;
1660 goto fail;
1663 req = cli_rmdir_send(frame, ev, cli, dname);
1664 if (req == NULL) {
1665 status = NT_STATUS_NO_MEMORY;
1666 goto fail;
1669 if (!tevent_req_poll(req, ev)) {
1670 status = map_nt_error_from_unix(errno);
1671 goto fail;
1674 status = cli_rmdir_recv(req);
1676 fail:
1677 TALLOC_FREE(frame);
1678 return status;
1681 /****************************************************************************
1682 Set or clear the delete on close flag.
1683 ****************************************************************************/
1685 struct doc_state {
1686 uint16_t setup;
1687 uint8_t param[6];
1688 uint8_t data[1];
1691 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1693 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1694 NULL, 0, NULL, NULL, 0, NULL);
1695 tevent_req_simple_finish_ntstatus(subreq, status);
1698 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1699 struct tevent_context *ev,
1700 struct cli_state *cli,
1701 uint16_t fnum,
1702 bool flag)
1704 struct tevent_req *req = NULL, *subreq = NULL;
1705 struct doc_state *state = NULL;
1707 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1708 if (req == NULL) {
1709 return NULL;
1712 /* Setup setup word. */
1713 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1715 /* Setup param array. */
1716 SSVAL(state->param,0,fnum);
1717 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1719 /* Setup data array. */
1720 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1722 subreq = cli_trans_send(state, /* mem ctx. */
1723 ev, /* event ctx. */
1724 cli, /* cli_state. */
1725 SMBtrans2, /* cmd. */
1726 NULL, /* pipe name. */
1727 -1, /* fid. */
1728 0, /* function. */
1729 0, /* flags. */
1730 &state->setup, /* setup. */
1731 1, /* num setup uint16_t words. */
1732 0, /* max returned setup. */
1733 state->param, /* param. */
1734 6, /* num param. */
1735 2, /* max returned param. */
1736 state->data, /* data. */
1737 1, /* num data. */
1738 0); /* max returned data. */
1740 if (tevent_req_nomem(subreq, req)) {
1741 return tevent_req_post(req, ev);
1743 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1744 return req;
1747 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1749 return tevent_req_simple_recv_ntstatus(req);
1752 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1754 TALLOC_CTX *frame = talloc_stackframe();
1755 struct tevent_context *ev = NULL;
1756 struct tevent_req *req = NULL;
1757 NTSTATUS status = NT_STATUS_OK;
1759 if (smbXcli_conn_has_async_calls(cli->conn)) {
1761 * Can't use sync call while an async call is in flight
1763 status = NT_STATUS_INVALID_PARAMETER;
1764 goto fail;
1767 ev = samba_tevent_context_init(frame);
1768 if (ev == NULL) {
1769 status = NT_STATUS_NO_MEMORY;
1770 goto fail;
1773 req = cli_nt_delete_on_close_send(frame,
1775 cli,
1776 fnum,
1777 flag);
1778 if (req == NULL) {
1779 status = NT_STATUS_NO_MEMORY;
1780 goto fail;
1783 if (!tevent_req_poll(req, ev)) {
1784 status = map_nt_error_from_unix(errno);
1785 goto fail;
1788 status = cli_nt_delete_on_close_recv(req);
1790 fail:
1791 TALLOC_FREE(frame);
1792 return status;
1795 struct cli_ntcreate1_state {
1796 uint16_t vwv[24];
1797 uint16_t fnum;
1798 struct smb_create_returns cr;
1801 static void cli_ntcreate1_done(struct tevent_req *subreq);
1803 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
1804 struct tevent_context *ev,
1805 struct cli_state *cli,
1806 const char *fname,
1807 uint32_t CreatFlags,
1808 uint32_t DesiredAccess,
1809 uint32_t FileAttributes,
1810 uint32_t ShareAccess,
1811 uint32_t CreateDisposition,
1812 uint32_t CreateOptions,
1813 uint8_t SecurityFlags)
1815 struct tevent_req *req, *subreq;
1816 struct cli_ntcreate1_state *state;
1817 uint16_t *vwv;
1818 uint8_t *bytes;
1819 size_t converted_len;
1821 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1822 if (req == NULL) {
1823 return NULL;
1826 vwv = state->vwv;
1828 SCVAL(vwv+0, 0, 0xFF);
1829 SCVAL(vwv+0, 1, 0);
1830 SSVAL(vwv+1, 0, 0);
1831 SCVAL(vwv+2, 0, 0);
1833 if (cli->use_oplocks) {
1834 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1836 SIVAL(vwv+3, 1, CreatFlags);
1837 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1838 SIVAL(vwv+7, 1, DesiredAccess);
1839 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1840 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1841 SIVAL(vwv+13, 1, FileAttributes);
1842 SIVAL(vwv+15, 1, ShareAccess);
1843 SIVAL(vwv+17, 1, CreateDisposition);
1844 SIVAL(vwv+19, 1, CreateOptions |
1845 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1846 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1847 SCVAL(vwv+23, 1, SecurityFlags);
1849 bytes = talloc_array(state, uint8_t, 0);
1850 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1851 fname, strlen(fname)+1,
1852 &converted_len);
1854 /* sigh. this copes with broken netapp filer behaviour */
1855 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1857 if (tevent_req_nomem(bytes, req)) {
1858 return tevent_req_post(req, ev);
1861 SSVAL(vwv+2, 1, converted_len);
1863 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1864 talloc_get_size(bytes), bytes);
1865 if (tevent_req_nomem(subreq, req)) {
1866 return tevent_req_post(req, ev);
1868 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
1869 return req;
1872 static void cli_ntcreate1_done(struct tevent_req *subreq)
1874 struct tevent_req *req = tevent_req_callback_data(
1875 subreq, struct tevent_req);
1876 struct cli_ntcreate1_state *state = tevent_req_data(
1877 req, struct cli_ntcreate1_state);
1878 uint8_t wct;
1879 uint16_t *vwv;
1880 uint32_t num_bytes;
1881 uint8_t *bytes;
1882 NTSTATUS status;
1884 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
1885 &num_bytes, &bytes);
1886 TALLOC_FREE(subreq);
1887 if (tevent_req_nterror(req, status)) {
1888 return;
1890 state->cr.oplock_level = CVAL(vwv+2, 0);
1891 state->fnum = SVAL(vwv+2, 1);
1892 state->cr.create_action = IVAL(vwv+3, 1);
1893 state->cr.creation_time = BVAL(vwv+5, 1);
1894 state->cr.last_access_time = BVAL(vwv+9, 1);
1895 state->cr.last_write_time = BVAL(vwv+13, 1);
1896 state->cr.change_time = BVAL(vwv+17, 1);
1897 state->cr.file_attributes = IVAL(vwv+21, 1);
1898 state->cr.allocation_size = BVAL(vwv+23, 1);
1899 state->cr.end_of_file = BVAL(vwv+27, 1);
1901 tevent_req_done(req);
1904 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
1905 uint16_t *pfnum,
1906 struct smb_create_returns *cr)
1908 struct cli_ntcreate1_state *state = tevent_req_data(
1909 req, struct cli_ntcreate1_state);
1910 NTSTATUS status;
1912 if (tevent_req_is_nterror(req, &status)) {
1913 return status;
1915 *pfnum = state->fnum;
1916 if (cr != NULL) {
1917 *cr = state->cr;
1919 return NT_STATUS_OK;
1922 struct cli_ntcreate_state {
1923 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
1924 struct smb_create_returns *cr);
1925 struct smb_create_returns cr;
1926 uint16_t fnum;
1929 static void cli_ntcreate_done(struct tevent_req *subreq);
1931 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1932 struct tevent_context *ev,
1933 struct cli_state *cli,
1934 const char *fname,
1935 uint32_t create_flags,
1936 uint32_t desired_access,
1937 uint32_t file_attributes,
1938 uint32_t share_access,
1939 uint32_t create_disposition,
1940 uint32_t create_options,
1941 uint8_t security_flags)
1943 struct tevent_req *req, *subreq;
1944 struct cli_ntcreate_state *state;
1946 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1947 if (req == NULL) {
1948 return NULL;
1951 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1952 state->recv = cli_smb2_create_fnum_recv;
1953 subreq = cli_smb2_create_fnum_send(
1954 state, ev, cli, fname, create_flags, desired_access,
1955 file_attributes, share_access, create_disposition,
1956 create_options);
1957 } else {
1958 state->recv = cli_ntcreate1_recv;
1959 subreq = cli_ntcreate1_send(
1960 state, ev, cli, fname, create_flags, desired_access,
1961 file_attributes, share_access, create_disposition,
1962 create_options, security_flags);
1964 if (tevent_req_nomem(subreq, req)) {
1965 return tevent_req_post(req, ev);
1967 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1968 return req;
1971 static void cli_ntcreate_done(struct tevent_req *subreq)
1973 struct tevent_req *req = tevent_req_callback_data(
1974 subreq, struct tevent_req);
1975 struct cli_ntcreate_state *state = tevent_req_data(
1976 req, struct cli_ntcreate_state);
1977 NTSTATUS status;
1979 status = state->recv(subreq, &state->fnum, &state->cr);
1980 TALLOC_FREE(subreq);
1981 if (tevent_req_nterror(req, status)) {
1982 return;
1984 tevent_req_done(req);
1987 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
1988 struct smb_create_returns *cr)
1990 struct cli_ntcreate_state *state = tevent_req_data(
1991 req, struct cli_ntcreate_state);
1992 NTSTATUS status;
1994 if (tevent_req_is_nterror(req, &status)) {
1995 return status;
1997 if (fnum != NULL) {
1998 *fnum = state->fnum;
2000 if (cr != NULL) {
2001 *cr = state->cr;
2003 return NT_STATUS_OK;
2006 NTSTATUS cli_ntcreate(struct cli_state *cli,
2007 const char *fname,
2008 uint32_t CreatFlags,
2009 uint32_t DesiredAccess,
2010 uint32_t FileAttributes,
2011 uint32_t ShareAccess,
2012 uint32_t CreateDisposition,
2013 uint32_t CreateOptions,
2014 uint8_t SecurityFlags,
2015 uint16_t *pfid,
2016 struct smb_create_returns *cr)
2018 TALLOC_CTX *frame = NULL;
2019 struct tevent_context *ev;
2020 struct tevent_req *req;
2021 NTSTATUS status = NT_STATUS_OK;
2023 frame = talloc_stackframe();
2025 if (smbXcli_conn_has_async_calls(cli->conn)) {
2027 * Can't use sync call while an async call is in flight
2029 status = NT_STATUS_INVALID_PARAMETER;
2030 goto fail;
2033 ev = samba_tevent_context_init(frame);
2034 if (ev == NULL) {
2035 status = NT_STATUS_NO_MEMORY;
2036 goto fail;
2039 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2040 DesiredAccess, FileAttributes, ShareAccess,
2041 CreateDisposition, CreateOptions,
2042 SecurityFlags);
2043 if (req == NULL) {
2044 status = NT_STATUS_NO_MEMORY;
2045 goto fail;
2048 if (!tevent_req_poll(req, ev)) {
2049 status = map_nt_error_from_unix(errno);
2050 goto fail;
2053 status = cli_ntcreate_recv(req, pfid, cr);
2054 fail:
2055 TALLOC_FREE(frame);
2056 return status;
2059 struct cli_nttrans_create_state {
2060 uint16_t fnum;
2061 struct smb_create_returns cr;
2064 static void cli_nttrans_create_done(struct tevent_req *subreq);
2066 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2067 struct tevent_context *ev,
2068 struct cli_state *cli,
2069 const char *fname,
2070 uint32_t CreatFlags,
2071 uint32_t DesiredAccess,
2072 uint32_t FileAttributes,
2073 uint32_t ShareAccess,
2074 uint32_t CreateDisposition,
2075 uint32_t CreateOptions,
2076 uint8_t SecurityFlags,
2077 struct security_descriptor *secdesc,
2078 struct ea_struct *eas,
2079 int num_eas)
2081 struct tevent_req *req, *subreq;
2082 struct cli_nttrans_create_state *state;
2083 uint8_t *param;
2084 uint8_t *secdesc_buf;
2085 size_t secdesc_len;
2086 NTSTATUS status;
2087 size_t converted_len;
2089 req = tevent_req_create(mem_ctx,
2090 &state, struct cli_nttrans_create_state);
2091 if (req == NULL) {
2092 return NULL;
2095 if (secdesc != NULL) {
2096 status = marshall_sec_desc(talloc_tos(), secdesc,
2097 &secdesc_buf, &secdesc_len);
2098 if (tevent_req_nterror(req, status)) {
2099 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2100 nt_errstr(status)));
2101 return tevent_req_post(req, ev);
2103 } else {
2104 secdesc_buf = NULL;
2105 secdesc_len = 0;
2108 if (num_eas != 0) {
2110 * TODO ;-)
2112 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2113 return tevent_req_post(req, ev);
2116 param = talloc_array(state, uint8_t, 53);
2117 if (tevent_req_nomem(param, req)) {
2118 return tevent_req_post(req, ev);
2121 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2122 fname, strlen(fname),
2123 &converted_len);
2124 if (tevent_req_nomem(param, req)) {
2125 return tevent_req_post(req, ev);
2128 SIVAL(param, 0, CreatFlags);
2129 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2130 SIVAL(param, 8, DesiredAccess);
2131 SIVAL(param, 12, 0x0); /* AllocationSize */
2132 SIVAL(param, 16, 0x0); /* AllocationSize */
2133 SIVAL(param, 20, FileAttributes);
2134 SIVAL(param, 24, ShareAccess);
2135 SIVAL(param, 28, CreateDisposition);
2136 SIVAL(param, 32, CreateOptions |
2137 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2138 SIVAL(param, 36, secdesc_len);
2139 SIVAL(param, 40, 0); /* EA length*/
2140 SIVAL(param, 44, converted_len);
2141 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2142 SCVAL(param, 52, SecurityFlags);
2144 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2145 NULL, -1, /* name, fid */
2146 NT_TRANSACT_CREATE, 0,
2147 NULL, 0, 0, /* setup */
2148 param, talloc_get_size(param), 128, /* param */
2149 secdesc_buf, secdesc_len, 0); /* data */
2150 if (tevent_req_nomem(subreq, req)) {
2151 return tevent_req_post(req, ev);
2153 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2154 return req;
2157 static void cli_nttrans_create_done(struct tevent_req *subreq)
2159 struct tevent_req *req = tevent_req_callback_data(
2160 subreq, struct tevent_req);
2161 struct cli_nttrans_create_state *state = tevent_req_data(
2162 req, struct cli_nttrans_create_state);
2163 uint8_t *param;
2164 uint32_t num_param;
2165 NTSTATUS status;
2167 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2168 NULL, 0, NULL, /* rsetup */
2169 &param, 69, &num_param,
2170 NULL, 0, NULL);
2171 if (tevent_req_nterror(req, status)) {
2172 return;
2174 state->cr.oplock_level = CVAL(param, 0);
2175 state->fnum = SVAL(param, 2);
2176 state->cr.create_action = IVAL(param, 4);
2177 state->cr.creation_time = BVAL(param, 12);
2178 state->cr.last_access_time = BVAL(param, 20);
2179 state->cr.last_write_time = BVAL(param, 28);
2180 state->cr.change_time = BVAL(param, 36);
2181 state->cr.file_attributes = IVAL(param, 44);
2182 state->cr.allocation_size = BVAL(param, 48);
2183 state->cr.end_of_file = BVAL(param, 56);
2185 TALLOC_FREE(param);
2186 tevent_req_done(req);
2189 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2190 uint16_t *fnum,
2191 struct smb_create_returns *cr)
2193 struct cli_nttrans_create_state *state = tevent_req_data(
2194 req, struct cli_nttrans_create_state);
2195 NTSTATUS status;
2197 if (tevent_req_is_nterror(req, &status)) {
2198 return status;
2200 *fnum = state->fnum;
2201 if (cr != NULL) {
2202 *cr = state->cr;
2204 return NT_STATUS_OK;
2207 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2208 const char *fname,
2209 uint32_t CreatFlags,
2210 uint32_t DesiredAccess,
2211 uint32_t FileAttributes,
2212 uint32_t ShareAccess,
2213 uint32_t CreateDisposition,
2214 uint32_t CreateOptions,
2215 uint8_t SecurityFlags,
2216 struct security_descriptor *secdesc,
2217 struct ea_struct *eas,
2218 int num_eas,
2219 uint16_t *pfid,
2220 struct smb_create_returns *cr)
2222 TALLOC_CTX *frame = talloc_stackframe();
2223 struct tevent_context *ev;
2224 struct tevent_req *req;
2225 NTSTATUS status = NT_STATUS_NO_MEMORY;
2227 if (smbXcli_conn_has_async_calls(cli->conn)) {
2229 * Can't use sync call while an async call is in flight
2231 status = NT_STATUS_INVALID_PARAMETER;
2232 goto fail;
2234 ev = samba_tevent_context_init(frame);
2235 if (ev == NULL) {
2236 goto fail;
2238 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2239 DesiredAccess, FileAttributes,
2240 ShareAccess, CreateDisposition,
2241 CreateOptions, SecurityFlags,
2242 secdesc, eas, num_eas);
2243 if (req == NULL) {
2244 goto fail;
2246 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2247 goto fail;
2249 status = cli_nttrans_create_recv(req, pfid, cr);
2250 fail:
2251 TALLOC_FREE(frame);
2252 return status;
2255 /****************************************************************************
2256 Open a file
2257 WARNING: if you open with O_WRONLY then getattrE won't work!
2258 ****************************************************************************/
2260 struct cli_openx_state {
2261 const char *fname;
2262 uint16_t vwv[15];
2263 uint16_t fnum;
2264 struct iovec bytes;
2267 static void cli_openx_done(struct tevent_req *subreq);
2269 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2270 struct tevent_context *ev,
2271 struct cli_state *cli, const char *fname,
2272 int flags, int share_mode,
2273 struct tevent_req **psmbreq)
2275 struct tevent_req *req, *subreq;
2276 struct cli_openx_state *state;
2277 unsigned openfn;
2278 unsigned accessmode;
2279 uint8_t additional_flags;
2280 uint8_t *bytes;
2282 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2283 if (req == NULL) {
2284 return NULL;
2287 openfn = 0;
2288 if (flags & O_CREAT) {
2289 openfn |= (1<<4);
2291 if (!(flags & O_EXCL)) {
2292 if (flags & O_TRUNC)
2293 openfn |= (1<<1);
2294 else
2295 openfn |= (1<<0);
2298 accessmode = (share_mode<<4);
2300 if ((flags & O_ACCMODE) == O_RDWR) {
2301 accessmode |= 2;
2302 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2303 accessmode |= 1;
2306 #if defined(O_SYNC)
2307 if ((flags & O_SYNC) == O_SYNC) {
2308 accessmode |= (1<<14);
2310 #endif /* O_SYNC */
2312 if (share_mode == DENY_FCB) {
2313 accessmode = 0xFF;
2316 SCVAL(state->vwv + 0, 0, 0xFF);
2317 SCVAL(state->vwv + 0, 1, 0);
2318 SSVAL(state->vwv + 1, 0, 0);
2319 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2320 SSVAL(state->vwv + 3, 0, accessmode);
2321 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2322 SSVAL(state->vwv + 5, 0, 0);
2323 SIVAL(state->vwv + 6, 0, 0);
2324 SSVAL(state->vwv + 8, 0, openfn);
2325 SIVAL(state->vwv + 9, 0, 0);
2326 SIVAL(state->vwv + 11, 0, 0);
2327 SIVAL(state->vwv + 13, 0, 0);
2329 additional_flags = 0;
2331 if (cli->use_oplocks) {
2332 /* if using oplocks then ask for a batch oplock via
2333 core and extended methods */
2334 additional_flags =
2335 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2336 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2339 bytes = talloc_array(state, uint8_t, 0);
2340 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2341 strlen(fname)+1, NULL);
2343 if (tevent_req_nomem(bytes, req)) {
2344 return tevent_req_post(req, ev);
2347 state->bytes.iov_base = (void *)bytes;
2348 state->bytes.iov_len = talloc_get_size(bytes);
2350 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2351 15, state->vwv, 1, &state->bytes);
2352 if (subreq == NULL) {
2353 TALLOC_FREE(req);
2354 return NULL;
2356 tevent_req_set_callback(subreq, cli_openx_done, req);
2357 *psmbreq = subreq;
2358 return req;
2361 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2362 struct cli_state *cli, const char *fname,
2363 int flags, int share_mode)
2365 struct tevent_req *req, *subreq;
2366 NTSTATUS status;
2368 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2369 &subreq);
2370 if (req == NULL) {
2371 return NULL;
2374 status = smb1cli_req_chain_submit(&subreq, 1);
2375 if (tevent_req_nterror(req, status)) {
2376 return tevent_req_post(req, ev);
2378 return req;
2381 static void cli_openx_done(struct tevent_req *subreq)
2383 struct tevent_req *req = tevent_req_callback_data(
2384 subreq, struct tevent_req);
2385 struct cli_openx_state *state = tevent_req_data(
2386 req, struct cli_openx_state);
2387 uint8_t wct;
2388 uint16_t *vwv;
2389 NTSTATUS status;
2391 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2392 NULL);
2393 TALLOC_FREE(subreq);
2394 if (tevent_req_nterror(req, status)) {
2395 return;
2397 state->fnum = SVAL(vwv+2, 0);
2398 tevent_req_done(req);
2401 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2403 struct cli_openx_state *state = tevent_req_data(
2404 req, struct cli_openx_state);
2405 NTSTATUS status;
2407 if (tevent_req_is_nterror(req, &status)) {
2408 return status;
2410 *pfnum = state->fnum;
2411 return NT_STATUS_OK;
2414 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2415 int share_mode, uint16_t *pfnum)
2417 TALLOC_CTX *frame = talloc_stackframe();
2418 struct tevent_context *ev;
2419 struct tevent_req *req;
2420 NTSTATUS status = NT_STATUS_NO_MEMORY;
2422 if (smbXcli_conn_has_async_calls(cli->conn)) {
2424 * Can't use sync call while an async call is in flight
2426 status = NT_STATUS_INVALID_PARAMETER;
2427 goto fail;
2430 ev = samba_tevent_context_init(frame);
2431 if (ev == NULL) {
2432 goto fail;
2435 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2436 if (req == NULL) {
2437 goto fail;
2440 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2441 goto fail;
2444 status = cli_openx_recv(req, pfnum);
2445 fail:
2446 TALLOC_FREE(frame);
2447 return status;
2449 /****************************************************************************
2450 Synchronous wrapper function that does an NtCreateX open by preference
2451 and falls back to openX if this fails.
2452 ****************************************************************************/
2454 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2455 int share_mode_in, uint16_t *pfnum)
2457 NTSTATUS status;
2458 unsigned int openfn = 0;
2459 unsigned int dos_deny = 0;
2460 uint32_t access_mask, share_mode, create_disposition, create_options;
2461 struct smb_create_returns cr;
2463 /* Do the initial mapping into OpenX parameters. */
2464 if (flags & O_CREAT) {
2465 openfn |= (1<<4);
2467 if (!(flags & O_EXCL)) {
2468 if (flags & O_TRUNC)
2469 openfn |= (1<<1);
2470 else
2471 openfn |= (1<<0);
2474 dos_deny = (share_mode_in<<4);
2476 if ((flags & O_ACCMODE) == O_RDWR) {
2477 dos_deny |= 2;
2478 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2479 dos_deny |= 1;
2482 #if defined(O_SYNC)
2483 if ((flags & O_SYNC) == O_SYNC) {
2484 dos_deny |= (1<<14);
2486 #endif /* O_SYNC */
2488 if (share_mode_in == DENY_FCB) {
2489 dos_deny = 0xFF;
2492 #if 0
2493 /* Hmmm. This is what I think the above code
2494 should look like if it's using the constants
2495 we #define. JRA. */
2497 if (flags & O_CREAT) {
2498 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2500 if (!(flags & O_EXCL)) {
2501 if (flags & O_TRUNC)
2502 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2503 else
2504 openfn |= OPENX_FILE_EXISTS_OPEN;
2507 dos_deny = SET_DENY_MODE(share_mode_in);
2509 if ((flags & O_ACCMODE) == O_RDWR) {
2510 dos_deny |= DOS_OPEN_RDWR;
2511 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2512 dos_deny |= DOS_OPEN_WRONLY;
2515 #if defined(O_SYNC)
2516 if ((flags & O_SYNC) == O_SYNC) {
2517 dos_deny |= FILE_SYNC_OPENMODE;
2519 #endif /* O_SYNC */
2521 if (share_mode_in == DENY_FCB) {
2522 dos_deny = 0xFF;
2524 #endif
2526 if (!map_open_params_to_ntcreate(fname, dos_deny,
2527 openfn, &access_mask,
2528 &share_mode, &create_disposition,
2529 &create_options, NULL)) {
2530 goto try_openx;
2533 status = cli_ntcreate(cli,
2534 fname,
2536 access_mask,
2538 share_mode,
2539 create_disposition,
2540 create_options,
2542 pfnum,
2543 &cr);
2545 /* Try and cope will all varients of "we don't do this call"
2546 and fall back to openX. */
2548 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2549 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2550 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2551 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2552 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2553 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2554 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2555 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2556 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2557 goto try_openx;
2560 if (NT_STATUS_IS_OK(status) &&
2561 (create_options & FILE_NON_DIRECTORY_FILE) &&
2562 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2565 * Some (broken) servers return a valid handle
2566 * for directories even if FILE_NON_DIRECTORY_FILE
2567 * is set. Just close the handle and set the
2568 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2570 status = cli_close(cli, *pfnum);
2571 if (!NT_STATUS_IS_OK(status)) {
2572 return status;
2574 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2575 /* Set this so libsmbclient can retrieve it. */
2576 cli->raw_status = status;
2579 return status;
2581 try_openx:
2583 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2586 /****************************************************************************
2587 Close a file.
2588 ****************************************************************************/
2590 struct cli_close_state {
2591 uint16_t vwv[3];
2594 static void cli_close_done(struct tevent_req *subreq);
2596 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2597 struct tevent_context *ev,
2598 struct cli_state *cli,
2599 uint16_t fnum,
2600 struct tevent_req **psubreq)
2602 struct tevent_req *req, *subreq;
2603 struct cli_close_state *state;
2605 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2606 if (req == NULL) {
2607 return NULL;
2610 SSVAL(state->vwv+0, 0, fnum);
2611 SIVALS(state->vwv+1, 0, -1);
2613 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2614 0, NULL);
2615 if (subreq == NULL) {
2616 TALLOC_FREE(req);
2617 return NULL;
2619 tevent_req_set_callback(subreq, cli_close_done, req);
2620 *psubreq = subreq;
2621 return req;
2624 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2625 struct tevent_context *ev,
2626 struct cli_state *cli,
2627 uint16_t fnum)
2629 struct tevent_req *req, *subreq;
2630 NTSTATUS status;
2632 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2633 if (req == NULL) {
2634 return NULL;
2637 status = smb1cli_req_chain_submit(&subreq, 1);
2638 if (tevent_req_nterror(req, status)) {
2639 return tevent_req_post(req, ev);
2641 return req;
2644 static void cli_close_done(struct tevent_req *subreq)
2646 struct tevent_req *req = tevent_req_callback_data(
2647 subreq, struct tevent_req);
2648 NTSTATUS status;
2650 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2651 TALLOC_FREE(subreq);
2652 if (tevent_req_nterror(req, status)) {
2653 return;
2655 tevent_req_done(req);
2658 NTSTATUS cli_close_recv(struct tevent_req *req)
2660 return tevent_req_simple_recv_ntstatus(req);
2663 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2665 TALLOC_CTX *frame = NULL;
2666 struct tevent_context *ev;
2667 struct tevent_req *req;
2668 NTSTATUS status = NT_STATUS_OK;
2670 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2671 return cli_smb2_close_fnum(cli, fnum);
2674 frame = talloc_stackframe();
2676 if (smbXcli_conn_has_async_calls(cli->conn)) {
2678 * Can't use sync call while an async call is in flight
2680 status = NT_STATUS_INVALID_PARAMETER;
2681 goto fail;
2684 ev = samba_tevent_context_init(frame);
2685 if (ev == NULL) {
2686 status = NT_STATUS_NO_MEMORY;
2687 goto fail;
2690 req = cli_close_send(frame, ev, cli, fnum);
2691 if (req == NULL) {
2692 status = NT_STATUS_NO_MEMORY;
2693 goto fail;
2696 if (!tevent_req_poll(req, ev)) {
2697 status = map_nt_error_from_unix(errno);
2698 goto fail;
2701 status = cli_close_recv(req);
2702 fail:
2703 TALLOC_FREE(frame);
2704 return status;
2707 /****************************************************************************
2708 Truncate a file to a specified size
2709 ****************************************************************************/
2711 struct ftrunc_state {
2712 uint16_t setup;
2713 uint8_t param[6];
2714 uint8_t data[8];
2717 static void cli_ftruncate_done(struct tevent_req *subreq)
2719 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2720 NULL, 0, NULL, NULL, 0, NULL);
2721 tevent_req_simple_finish_ntstatus(subreq, status);
2724 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2725 struct tevent_context *ev,
2726 struct cli_state *cli,
2727 uint16_t fnum,
2728 uint64_t size)
2730 struct tevent_req *req = NULL, *subreq = NULL;
2731 struct ftrunc_state *state = NULL;
2733 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2734 if (req == NULL) {
2735 return NULL;
2738 /* Setup setup word. */
2739 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2741 /* Setup param array. */
2742 SSVAL(state->param,0,fnum);
2743 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2744 SSVAL(state->param,4,0);
2746 /* Setup data array. */
2747 SBVAL(state->data, 0, size);
2749 subreq = cli_trans_send(state, /* mem ctx. */
2750 ev, /* event ctx. */
2751 cli, /* cli_state. */
2752 SMBtrans2, /* cmd. */
2753 NULL, /* pipe name. */
2754 -1, /* fid. */
2755 0, /* function. */
2756 0, /* flags. */
2757 &state->setup, /* setup. */
2758 1, /* num setup uint16_t words. */
2759 0, /* max returned setup. */
2760 state->param, /* param. */
2761 6, /* num param. */
2762 2, /* max returned param. */
2763 state->data, /* data. */
2764 8, /* num data. */
2765 0); /* max returned data. */
2767 if (tevent_req_nomem(subreq, req)) {
2768 return tevent_req_post(req, ev);
2770 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2771 return req;
2774 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2776 return tevent_req_simple_recv_ntstatus(req);
2779 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2781 TALLOC_CTX *frame = talloc_stackframe();
2782 struct tevent_context *ev = NULL;
2783 struct tevent_req *req = NULL;
2784 NTSTATUS status = NT_STATUS_OK;
2786 if (smbXcli_conn_has_async_calls(cli->conn)) {
2788 * Can't use sync call while an async call is in flight
2790 status = NT_STATUS_INVALID_PARAMETER;
2791 goto fail;
2794 ev = samba_tevent_context_init(frame);
2795 if (ev == NULL) {
2796 status = NT_STATUS_NO_MEMORY;
2797 goto fail;
2800 req = cli_ftruncate_send(frame,
2802 cli,
2803 fnum,
2804 size);
2805 if (req == NULL) {
2806 status = NT_STATUS_NO_MEMORY;
2807 goto fail;
2810 if (!tevent_req_poll(req, ev)) {
2811 status = map_nt_error_from_unix(errno);
2812 goto fail;
2815 status = cli_ftruncate_recv(req);
2817 fail:
2818 TALLOC_FREE(frame);
2819 return status;
2822 /****************************************************************************
2823 send a lock with a specified locktype
2824 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2825 ****************************************************************************/
2827 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2828 uint32_t offset, uint32_t len,
2829 int timeout, unsigned char locktype)
2831 uint16_t vwv[8];
2832 uint8_t bytes[10];
2833 NTSTATUS status;
2834 unsigned int set_timeout = 0;
2835 unsigned int saved_timeout = 0;
2837 SCVAL(vwv + 0, 0, 0xff);
2838 SCVAL(vwv + 0, 1, 0);
2839 SSVAL(vwv + 1, 0, 0);
2840 SSVAL(vwv + 2, 0, fnum);
2841 SCVAL(vwv + 3, 0, locktype);
2842 SCVAL(vwv + 3, 1, 0);
2843 SIVALS(vwv + 4, 0, timeout);
2844 SSVAL(vwv + 6, 0, 0);
2845 SSVAL(vwv + 7, 0, 1);
2847 SSVAL(bytes, 0, cli_getpid(cli));
2848 SIVAL(bytes, 2, offset);
2849 SIVAL(bytes, 6, len);
2851 if (timeout != 0) {
2852 if (timeout == -1) {
2853 set_timeout = 0x7FFFFFFF;
2854 } else {
2855 set_timeout = timeout + 2*1000;
2857 saved_timeout = cli_set_timeout(cli, set_timeout);
2860 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2861 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2863 if (saved_timeout != 0) {
2864 cli_set_timeout(cli, saved_timeout);
2867 return status;
2870 /****************************************************************************
2871 Lock a file.
2872 note that timeout is in units of 2 milliseconds
2873 ****************************************************************************/
2875 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2876 uint32_t offset, uint32_t len, int timeout,
2877 enum brl_type lock_type)
2879 NTSTATUS status;
2881 status = cli_locktype(cli, fnum, offset, len, timeout,
2882 (lock_type == READ_LOCK? 1 : 0));
2883 return status;
2886 /****************************************************************************
2887 Unlock a file.
2888 ****************************************************************************/
2890 struct cli_unlock_state {
2891 uint16_t vwv[8];
2892 uint8_t data[10];
2895 static void cli_unlock_done(struct tevent_req *subreq);
2897 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2898 struct tevent_context *ev,
2899 struct cli_state *cli,
2900 uint16_t fnum,
2901 uint64_t offset,
2902 uint64_t len)
2905 struct tevent_req *req = NULL, *subreq = NULL;
2906 struct cli_unlock_state *state = NULL;
2907 uint8_t additional_flags = 0;
2909 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2910 if (req == NULL) {
2911 return NULL;
2914 SCVAL(state->vwv+0, 0, 0xFF);
2915 SSVAL(state->vwv+2, 0, fnum);
2916 SCVAL(state->vwv+3, 0, 0);
2917 SIVALS(state->vwv+4, 0, 0);
2918 SSVAL(state->vwv+6, 0, 1);
2919 SSVAL(state->vwv+7, 0, 0);
2921 SSVAL(state->data, 0, cli_getpid(cli));
2922 SIVAL(state->data, 2, offset);
2923 SIVAL(state->data, 6, len);
2925 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2926 8, state->vwv, 10, state->data);
2927 if (tevent_req_nomem(subreq, req)) {
2928 return tevent_req_post(req, ev);
2930 tevent_req_set_callback(subreq, cli_unlock_done, req);
2931 return req;
2934 static void cli_unlock_done(struct tevent_req *subreq)
2936 struct tevent_req *req = tevent_req_callback_data(
2937 subreq, struct tevent_req);
2938 NTSTATUS status;
2940 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2941 TALLOC_FREE(subreq);
2942 if (tevent_req_nterror(req, status)) {
2943 return;
2945 tevent_req_done(req);
2948 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2950 return tevent_req_simple_recv_ntstatus(req);
2953 NTSTATUS cli_unlock(struct cli_state *cli,
2954 uint16_t fnum,
2955 uint32_t offset,
2956 uint32_t len)
2958 TALLOC_CTX *frame = talloc_stackframe();
2959 struct tevent_context *ev;
2960 struct tevent_req *req;
2961 NTSTATUS status = NT_STATUS_OK;
2963 if (smbXcli_conn_has_async_calls(cli->conn)) {
2965 * Can't use sync call while an async call is in flight
2967 status = NT_STATUS_INVALID_PARAMETER;
2968 goto fail;
2971 ev = samba_tevent_context_init(frame);
2972 if (ev == NULL) {
2973 status = NT_STATUS_NO_MEMORY;
2974 goto fail;
2977 req = cli_unlock_send(frame, ev, cli,
2978 fnum, offset, len);
2979 if (req == NULL) {
2980 status = NT_STATUS_NO_MEMORY;
2981 goto fail;
2984 if (!tevent_req_poll(req, ev)) {
2985 status = map_nt_error_from_unix(errno);
2986 goto fail;
2989 status = cli_unlock_recv(req);
2991 fail:
2992 TALLOC_FREE(frame);
2993 return status;
2996 /****************************************************************************
2997 Lock a file with 64 bit offsets.
2998 ****************************************************************************/
3000 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3001 uint64_t offset, uint64_t len, int timeout,
3002 enum brl_type lock_type)
3004 uint16_t vwv[8];
3005 uint8_t bytes[20];
3006 unsigned int set_timeout = 0;
3007 unsigned int saved_timeout = 0;
3008 int ltype;
3009 NTSTATUS status;
3011 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3012 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3015 ltype = (lock_type == READ_LOCK? 1 : 0);
3016 ltype |= LOCKING_ANDX_LARGE_FILES;
3018 SCVAL(vwv + 0, 0, 0xff);
3019 SCVAL(vwv + 0, 1, 0);
3020 SSVAL(vwv + 1, 0, 0);
3021 SSVAL(vwv + 2, 0, fnum);
3022 SCVAL(vwv + 3, 0, ltype);
3023 SCVAL(vwv + 3, 1, 0);
3024 SIVALS(vwv + 4, 0, timeout);
3025 SSVAL(vwv + 6, 0, 0);
3026 SSVAL(vwv + 7, 0, 1);
3028 SIVAL(bytes, 0, cli_getpid(cli));
3029 SOFF_T_R(bytes, 4, offset);
3030 SOFF_T_R(bytes, 12, len);
3032 if (timeout != 0) {
3033 if (timeout == -1) {
3034 set_timeout = 0x7FFFFFFF;
3035 } else {
3036 set_timeout = timeout + 2*1000;
3038 saved_timeout = cli_set_timeout(cli, set_timeout);
3041 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3042 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3044 if (saved_timeout != 0) {
3045 cli_set_timeout(cli, saved_timeout);
3048 return status;
3051 /****************************************************************************
3052 Unlock a file with 64 bit offsets.
3053 ****************************************************************************/
3055 struct cli_unlock64_state {
3056 uint16_t vwv[8];
3057 uint8_t data[20];
3060 static void cli_unlock64_done(struct tevent_req *subreq);
3062 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3063 struct tevent_context *ev,
3064 struct cli_state *cli,
3065 uint16_t fnum,
3066 uint64_t offset,
3067 uint64_t len)
3070 struct tevent_req *req = NULL, *subreq = NULL;
3071 struct cli_unlock64_state *state = NULL;
3072 uint8_t additional_flags = 0;
3074 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3075 if (req == NULL) {
3076 return NULL;
3079 SCVAL(state->vwv+0, 0, 0xff);
3080 SSVAL(state->vwv+2, 0, fnum);
3081 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3082 SIVALS(state->vwv+4, 0, 0);
3083 SSVAL(state->vwv+6, 0, 1);
3084 SSVAL(state->vwv+7, 0, 0);
3086 SIVAL(state->data, 0, cli_getpid(cli));
3087 SOFF_T_R(state->data, 4, offset);
3088 SOFF_T_R(state->data, 12, len);
3090 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
3091 8, state->vwv, 20, state->data);
3092 if (tevent_req_nomem(subreq, req)) {
3093 return tevent_req_post(req, ev);
3095 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3096 return req;
3099 static void cli_unlock64_done(struct tevent_req *subreq)
3101 struct tevent_req *req = tevent_req_callback_data(
3102 subreq, struct tevent_req);
3103 NTSTATUS status;
3105 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3106 TALLOC_FREE(subreq);
3107 if (tevent_req_nterror(req, status)) {
3108 return;
3110 tevent_req_done(req);
3113 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3115 return tevent_req_simple_recv_ntstatus(req);
3118 NTSTATUS cli_unlock64(struct cli_state *cli,
3119 uint16_t fnum,
3120 uint64_t offset,
3121 uint64_t len)
3123 TALLOC_CTX *frame = talloc_stackframe();
3124 struct tevent_context *ev;
3125 struct tevent_req *req;
3126 NTSTATUS status = NT_STATUS_OK;
3128 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3129 return cli_unlock(cli, fnum, offset, len);
3132 if (smbXcli_conn_has_async_calls(cli->conn)) {
3134 * Can't use sync call while an async call is in flight
3136 status = NT_STATUS_INVALID_PARAMETER;
3137 goto fail;
3140 ev = samba_tevent_context_init(frame);
3141 if (ev == NULL) {
3142 status = NT_STATUS_NO_MEMORY;
3143 goto fail;
3146 req = cli_unlock64_send(frame, ev, cli,
3147 fnum, offset, len);
3148 if (req == NULL) {
3149 status = NT_STATUS_NO_MEMORY;
3150 goto fail;
3153 if (!tevent_req_poll(req, ev)) {
3154 status = map_nt_error_from_unix(errno);
3155 goto fail;
3158 status = cli_unlock64_recv(req);
3160 fail:
3161 TALLOC_FREE(frame);
3162 return status;
3165 /****************************************************************************
3166 Get/unlock a POSIX lock on a file - internal function.
3167 ****************************************************************************/
3169 struct posix_lock_state {
3170 uint16_t setup;
3171 uint8_t param[4];
3172 uint8_t data[POSIX_LOCK_DATA_SIZE];
3175 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3177 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3178 NULL, 0, NULL, NULL, 0, NULL);
3179 tevent_req_simple_finish_ntstatus(subreq, status);
3182 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3183 struct tevent_context *ev,
3184 struct cli_state *cli,
3185 uint16_t fnum,
3186 uint64_t offset,
3187 uint64_t len,
3188 bool wait_lock,
3189 enum brl_type lock_type)
3191 struct tevent_req *req = NULL, *subreq = NULL;
3192 struct posix_lock_state *state = NULL;
3194 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3195 if (req == NULL) {
3196 return NULL;
3199 /* Setup setup word. */
3200 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3202 /* Setup param array. */
3203 SSVAL(&state->param, 0, fnum);
3204 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3206 /* Setup data array. */
3207 switch (lock_type) {
3208 case READ_LOCK:
3209 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3210 POSIX_LOCK_TYPE_READ);
3211 break;
3212 case WRITE_LOCK:
3213 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3214 POSIX_LOCK_TYPE_WRITE);
3215 break;
3216 case UNLOCK_LOCK:
3217 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3218 POSIX_LOCK_TYPE_UNLOCK);
3219 break;
3220 default:
3221 return NULL;
3224 if (wait_lock) {
3225 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3226 POSIX_LOCK_FLAG_WAIT);
3227 } else {
3228 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3229 POSIX_LOCK_FLAG_NOWAIT);
3232 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3233 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3234 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3236 subreq = cli_trans_send(state, /* mem ctx. */
3237 ev, /* event ctx. */
3238 cli, /* cli_state. */
3239 SMBtrans2, /* cmd. */
3240 NULL, /* pipe name. */
3241 -1, /* fid. */
3242 0, /* function. */
3243 0, /* flags. */
3244 &state->setup, /* setup. */
3245 1, /* num setup uint16_t words. */
3246 0, /* max returned setup. */
3247 state->param, /* param. */
3248 4, /* num param. */
3249 2, /* max returned param. */
3250 state->data, /* data. */
3251 POSIX_LOCK_DATA_SIZE, /* num data. */
3252 0); /* max returned data. */
3254 if (tevent_req_nomem(subreq, req)) {
3255 return tevent_req_post(req, ev);
3257 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3258 return req;
3261 /****************************************************************************
3262 POSIX Lock a file.
3263 ****************************************************************************/
3265 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3266 struct tevent_context *ev,
3267 struct cli_state *cli,
3268 uint16_t fnum,
3269 uint64_t offset,
3270 uint64_t len,
3271 bool wait_lock,
3272 enum brl_type lock_type)
3274 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3275 wait_lock, lock_type);
3278 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3280 return tevent_req_simple_recv_ntstatus(req);
3283 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3284 uint64_t offset, uint64_t len,
3285 bool wait_lock, enum brl_type lock_type)
3287 TALLOC_CTX *frame = talloc_stackframe();
3288 struct tevent_context *ev = NULL;
3289 struct tevent_req *req = NULL;
3290 NTSTATUS status = NT_STATUS_OK;
3292 if (smbXcli_conn_has_async_calls(cli->conn)) {
3294 * Can't use sync call while an async call is in flight
3296 status = NT_STATUS_INVALID_PARAMETER;
3297 goto fail;
3300 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3301 status = NT_STATUS_INVALID_PARAMETER;
3302 goto fail;
3305 ev = samba_tevent_context_init(frame);
3306 if (ev == NULL) {
3307 status = NT_STATUS_NO_MEMORY;
3308 goto fail;
3311 req = cli_posix_lock_send(frame,
3313 cli,
3314 fnum,
3315 offset,
3316 len,
3317 wait_lock,
3318 lock_type);
3319 if (req == NULL) {
3320 status = NT_STATUS_NO_MEMORY;
3321 goto fail;
3324 if (!tevent_req_poll(req, ev)) {
3325 status = map_nt_error_from_unix(errno);
3326 goto fail;
3329 status = cli_posix_lock_recv(req);
3331 fail:
3332 TALLOC_FREE(frame);
3333 return status;
3336 /****************************************************************************
3337 POSIX Unlock a file.
3338 ****************************************************************************/
3340 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3341 struct tevent_context *ev,
3342 struct cli_state *cli,
3343 uint16_t fnum,
3344 uint64_t offset,
3345 uint64_t len)
3347 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3348 false, UNLOCK_LOCK);
3351 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3353 return tevent_req_simple_recv_ntstatus(req);
3356 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3358 TALLOC_CTX *frame = talloc_stackframe();
3359 struct tevent_context *ev = NULL;
3360 struct tevent_req *req = NULL;
3361 NTSTATUS status = NT_STATUS_OK;
3363 if (smbXcli_conn_has_async_calls(cli->conn)) {
3365 * Can't use sync call while an async call is in flight
3367 status = NT_STATUS_INVALID_PARAMETER;
3368 goto fail;
3371 ev = samba_tevent_context_init(frame);
3372 if (ev == NULL) {
3373 status = NT_STATUS_NO_MEMORY;
3374 goto fail;
3377 req = cli_posix_unlock_send(frame,
3379 cli,
3380 fnum,
3381 offset,
3382 len);
3383 if (req == NULL) {
3384 status = NT_STATUS_NO_MEMORY;
3385 goto fail;
3388 if (!tevent_req_poll(req, ev)) {
3389 status = map_nt_error_from_unix(errno);
3390 goto fail;
3393 status = cli_posix_unlock_recv(req);
3395 fail:
3396 TALLOC_FREE(frame);
3397 return status;
3400 /****************************************************************************
3401 Do a SMBgetattrE call.
3402 ****************************************************************************/
3404 static void cli_getattrE_done(struct tevent_req *subreq);
3406 struct cli_getattrE_state {
3407 uint16_t vwv[1];
3408 int zone_offset;
3409 uint16_t attr;
3410 off_t size;
3411 time_t change_time;
3412 time_t access_time;
3413 time_t write_time;
3416 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3417 struct tevent_context *ev,
3418 struct cli_state *cli,
3419 uint16_t fnum)
3421 struct tevent_req *req = NULL, *subreq = NULL;
3422 struct cli_getattrE_state *state = NULL;
3423 uint8_t additional_flags = 0;
3425 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3426 if (req == NULL) {
3427 return NULL;
3430 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3431 SSVAL(state->vwv+0,0,fnum);
3433 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3434 1, state->vwv, 0, NULL);
3435 if (tevent_req_nomem(subreq, req)) {
3436 return tevent_req_post(req, ev);
3438 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3439 return req;
3442 static void cli_getattrE_done(struct tevent_req *subreq)
3444 struct tevent_req *req = tevent_req_callback_data(
3445 subreq, struct tevent_req);
3446 struct cli_getattrE_state *state = tevent_req_data(
3447 req, struct cli_getattrE_state);
3448 uint8_t wct;
3449 uint16_t *vwv = NULL;
3450 NTSTATUS status;
3452 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3453 NULL, NULL);
3454 TALLOC_FREE(subreq);
3455 if (tevent_req_nterror(req, status)) {
3456 return;
3459 state->size = (off_t)IVAL(vwv+6,0);
3460 state->attr = SVAL(vwv+10,0);
3461 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3462 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3463 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3465 tevent_req_done(req);
3468 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3469 uint16_t *attr,
3470 off_t *size,
3471 time_t *change_time,
3472 time_t *access_time,
3473 time_t *write_time)
3475 struct cli_getattrE_state *state = tevent_req_data(
3476 req, struct cli_getattrE_state);
3477 NTSTATUS status;
3479 if (tevent_req_is_nterror(req, &status)) {
3480 return status;
3482 if (attr) {
3483 *attr = state->attr;
3485 if (size) {
3486 *size = state->size;
3488 if (change_time) {
3489 *change_time = state->change_time;
3491 if (access_time) {
3492 *access_time = state->access_time;
3494 if (write_time) {
3495 *write_time = state->write_time;
3497 return NT_STATUS_OK;
3500 NTSTATUS cli_getattrE(struct cli_state *cli,
3501 uint16_t fnum,
3502 uint16_t *attr,
3503 off_t *size,
3504 time_t *change_time,
3505 time_t *access_time,
3506 time_t *write_time)
3508 TALLOC_CTX *frame = NULL;
3509 struct tevent_context *ev = NULL;
3510 struct tevent_req *req = NULL;
3511 NTSTATUS status = NT_STATUS_OK;
3513 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3514 return cli_smb2_getattrE(cli,
3515 fnum,
3516 attr,
3517 size,
3518 change_time,
3519 access_time,
3520 write_time);
3523 frame = talloc_stackframe();
3525 if (smbXcli_conn_has_async_calls(cli->conn)) {
3527 * Can't use sync call while an async call is in flight
3529 status = NT_STATUS_INVALID_PARAMETER;
3530 goto fail;
3533 ev = samba_tevent_context_init(frame);
3534 if (ev == NULL) {
3535 status = NT_STATUS_NO_MEMORY;
3536 goto fail;
3539 req = cli_getattrE_send(frame, ev, cli, fnum);
3540 if (req == NULL) {
3541 status = NT_STATUS_NO_MEMORY;
3542 goto fail;
3545 if (!tevent_req_poll(req, ev)) {
3546 status = map_nt_error_from_unix(errno);
3547 goto fail;
3550 status = cli_getattrE_recv(req,
3551 attr,
3552 size,
3553 change_time,
3554 access_time,
3555 write_time);
3557 fail:
3558 TALLOC_FREE(frame);
3559 return status;
3562 /****************************************************************************
3563 Do a SMBgetatr call
3564 ****************************************************************************/
3566 static void cli_getatr_done(struct tevent_req *subreq);
3568 struct cli_getatr_state {
3569 int zone_offset;
3570 uint16_t attr;
3571 off_t size;
3572 time_t write_time;
3575 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3576 struct tevent_context *ev,
3577 struct cli_state *cli,
3578 const char *fname)
3580 struct tevent_req *req = NULL, *subreq = NULL;
3581 struct cli_getatr_state *state = NULL;
3582 uint8_t additional_flags = 0;
3583 uint8_t *bytes = NULL;
3585 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3586 if (req == NULL) {
3587 return NULL;
3590 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3592 bytes = talloc_array(state, uint8_t, 1);
3593 if (tevent_req_nomem(bytes, req)) {
3594 return tevent_req_post(req, ev);
3596 bytes[0] = 4;
3597 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3598 strlen(fname)+1, NULL);
3600 if (tevent_req_nomem(bytes, req)) {
3601 return tevent_req_post(req, ev);
3604 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3605 0, NULL, talloc_get_size(bytes), bytes);
3606 if (tevent_req_nomem(subreq, req)) {
3607 return tevent_req_post(req, ev);
3609 tevent_req_set_callback(subreq, cli_getatr_done, req);
3610 return req;
3613 static void cli_getatr_done(struct tevent_req *subreq)
3615 struct tevent_req *req = tevent_req_callback_data(
3616 subreq, struct tevent_req);
3617 struct cli_getatr_state *state = tevent_req_data(
3618 req, struct cli_getatr_state);
3619 uint8_t wct;
3620 uint16_t *vwv = NULL;
3621 NTSTATUS status;
3623 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3624 NULL);
3625 TALLOC_FREE(subreq);
3626 if (tevent_req_nterror(req, status)) {
3627 return;
3630 state->attr = SVAL(vwv+0,0);
3631 state->size = (off_t)IVAL(vwv+3,0);
3632 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3634 tevent_req_done(req);
3637 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3638 uint16_t *attr,
3639 off_t *size,
3640 time_t *write_time)
3642 struct cli_getatr_state *state = tevent_req_data(
3643 req, struct cli_getatr_state);
3644 NTSTATUS status;
3646 if (tevent_req_is_nterror(req, &status)) {
3647 return status;
3649 if (attr) {
3650 *attr = state->attr;
3652 if (size) {
3653 *size = state->size;
3655 if (write_time) {
3656 *write_time = state->write_time;
3658 return NT_STATUS_OK;
3661 NTSTATUS cli_getatr(struct cli_state *cli,
3662 const char *fname,
3663 uint16_t *attr,
3664 off_t *size,
3665 time_t *write_time)
3667 TALLOC_CTX *frame = NULL;
3668 struct tevent_context *ev = NULL;
3669 struct tevent_req *req = NULL;
3670 NTSTATUS status = NT_STATUS_OK;
3672 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3673 return cli_smb2_getatr(cli,
3674 fname,
3675 attr,
3676 size,
3677 write_time);
3680 frame = talloc_stackframe();
3682 if (smbXcli_conn_has_async_calls(cli->conn)) {
3684 * Can't use sync call while an async call is in flight
3686 status = NT_STATUS_INVALID_PARAMETER;
3687 goto fail;
3690 ev = samba_tevent_context_init(frame);
3691 if (ev == NULL) {
3692 status = NT_STATUS_NO_MEMORY;
3693 goto fail;
3696 req = cli_getatr_send(frame, ev, cli, fname);
3697 if (req == NULL) {
3698 status = NT_STATUS_NO_MEMORY;
3699 goto fail;
3702 if (!tevent_req_poll(req, ev)) {
3703 status = map_nt_error_from_unix(errno);
3704 goto fail;
3707 status = cli_getatr_recv(req,
3708 attr,
3709 size,
3710 write_time);
3712 fail:
3713 TALLOC_FREE(frame);
3714 return status;
3717 /****************************************************************************
3718 Do a SMBsetattrE call.
3719 ****************************************************************************/
3721 static void cli_setattrE_done(struct tevent_req *subreq);
3723 struct cli_setattrE_state {
3724 uint16_t vwv[7];
3727 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3728 struct tevent_context *ev,
3729 struct cli_state *cli,
3730 uint16_t fnum,
3731 time_t change_time,
3732 time_t access_time,
3733 time_t write_time)
3735 struct tevent_req *req = NULL, *subreq = NULL;
3736 struct cli_setattrE_state *state = NULL;
3737 uint8_t additional_flags = 0;
3739 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3740 if (req == NULL) {
3741 return NULL;
3744 SSVAL(state->vwv+0, 0, fnum);
3745 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3746 smb1cli_conn_server_time_zone(cli->conn));
3747 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3748 smb1cli_conn_server_time_zone(cli->conn));
3749 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3750 smb1cli_conn_server_time_zone(cli->conn));
3752 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3753 7, state->vwv, 0, NULL);
3754 if (tevent_req_nomem(subreq, req)) {
3755 return tevent_req_post(req, ev);
3757 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3758 return req;
3761 static void cli_setattrE_done(struct tevent_req *subreq)
3763 struct tevent_req *req = tevent_req_callback_data(
3764 subreq, struct tevent_req);
3765 NTSTATUS status;
3767 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3768 TALLOC_FREE(subreq);
3769 if (tevent_req_nterror(req, status)) {
3770 return;
3772 tevent_req_done(req);
3775 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3777 return tevent_req_simple_recv_ntstatus(req);
3780 NTSTATUS cli_setattrE(struct cli_state *cli,
3781 uint16_t fnum,
3782 time_t change_time,
3783 time_t access_time,
3784 time_t write_time)
3786 TALLOC_CTX *frame = NULL;
3787 struct tevent_context *ev = NULL;
3788 struct tevent_req *req = NULL;
3789 NTSTATUS status = NT_STATUS_OK;
3791 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3792 return cli_smb2_setattrE(cli,
3793 fnum,
3794 change_time,
3795 access_time,
3796 write_time);
3799 frame = talloc_stackframe();
3801 if (smbXcli_conn_has_async_calls(cli->conn)) {
3803 * Can't use sync call while an async call is in flight
3805 status = NT_STATUS_INVALID_PARAMETER;
3806 goto fail;
3809 ev = samba_tevent_context_init(frame);
3810 if (ev == NULL) {
3811 status = NT_STATUS_NO_MEMORY;
3812 goto fail;
3815 req = cli_setattrE_send(frame, ev,
3816 cli,
3817 fnum,
3818 change_time,
3819 access_time,
3820 write_time);
3822 if (req == NULL) {
3823 status = NT_STATUS_NO_MEMORY;
3824 goto fail;
3827 if (!tevent_req_poll(req, ev)) {
3828 status = map_nt_error_from_unix(errno);
3829 goto fail;
3832 status = cli_setattrE_recv(req);
3834 fail:
3835 TALLOC_FREE(frame);
3836 return status;
3839 /****************************************************************************
3840 Do a SMBsetatr call.
3841 ****************************************************************************/
3843 static void cli_setatr_done(struct tevent_req *subreq);
3845 struct cli_setatr_state {
3846 uint16_t vwv[8];
3849 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3850 struct tevent_context *ev,
3851 struct cli_state *cli,
3852 const char *fname,
3853 uint16_t attr,
3854 time_t mtime)
3856 struct tevent_req *req = NULL, *subreq = NULL;
3857 struct cli_setatr_state *state = NULL;
3858 uint8_t additional_flags = 0;
3859 uint8_t *bytes = NULL;
3861 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3862 if (req == NULL) {
3863 return NULL;
3866 SSVAL(state->vwv+0, 0, attr);
3867 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3869 bytes = talloc_array(state, uint8_t, 1);
3870 if (tevent_req_nomem(bytes, req)) {
3871 return tevent_req_post(req, ev);
3873 bytes[0] = 4;
3874 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3875 strlen(fname)+1, NULL);
3876 if (tevent_req_nomem(bytes, req)) {
3877 return tevent_req_post(req, ev);
3879 bytes = talloc_realloc(state, bytes, uint8_t,
3880 talloc_get_size(bytes)+1);
3881 if (tevent_req_nomem(bytes, req)) {
3882 return tevent_req_post(req, ev);
3885 bytes[talloc_get_size(bytes)-1] = 4;
3886 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3887 1, NULL);
3888 if (tevent_req_nomem(bytes, req)) {
3889 return tevent_req_post(req, ev);
3892 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3893 8, state->vwv, talloc_get_size(bytes), bytes);
3894 if (tevent_req_nomem(subreq, req)) {
3895 return tevent_req_post(req, ev);
3897 tevent_req_set_callback(subreq, cli_setatr_done, req);
3898 return req;
3901 static void cli_setatr_done(struct tevent_req *subreq)
3903 struct tevent_req *req = tevent_req_callback_data(
3904 subreq, struct tevent_req);
3905 NTSTATUS status;
3907 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3908 TALLOC_FREE(subreq);
3909 if (tevent_req_nterror(req, status)) {
3910 return;
3912 tevent_req_done(req);
3915 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3917 return tevent_req_simple_recv_ntstatus(req);
3920 NTSTATUS cli_setatr(struct cli_state *cli,
3921 const char *fname,
3922 uint16_t attr,
3923 time_t mtime)
3925 TALLOC_CTX *frame = NULL;
3926 struct tevent_context *ev = NULL;
3927 struct tevent_req *req = NULL;
3928 NTSTATUS status = NT_STATUS_OK;
3930 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3931 return cli_smb2_setatr(cli,
3932 fname,
3933 attr,
3934 mtime);
3937 frame = talloc_stackframe();
3939 if (smbXcli_conn_has_async_calls(cli->conn)) {
3941 * Can't use sync call while an async call is in flight
3943 status = NT_STATUS_INVALID_PARAMETER;
3944 goto fail;
3947 ev = samba_tevent_context_init(frame);
3948 if (ev == NULL) {
3949 status = NT_STATUS_NO_MEMORY;
3950 goto fail;
3953 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3954 if (req == NULL) {
3955 status = NT_STATUS_NO_MEMORY;
3956 goto fail;
3959 if (!tevent_req_poll(req, ev)) {
3960 status = map_nt_error_from_unix(errno);
3961 goto fail;
3964 status = cli_setatr_recv(req);
3966 fail:
3967 TALLOC_FREE(frame);
3968 return status;
3971 /****************************************************************************
3972 Check for existance of a dir.
3973 ****************************************************************************/
3975 static void cli_chkpath_done(struct tevent_req *subreq);
3977 struct cli_chkpath_state {
3978 int dummy;
3981 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3982 struct tevent_context *ev,
3983 struct cli_state *cli,
3984 const char *fname)
3986 struct tevent_req *req = NULL, *subreq = NULL;
3987 struct cli_chkpath_state *state = NULL;
3988 uint8_t additional_flags = 0;
3989 uint8_t *bytes = NULL;
3991 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3992 if (req == NULL) {
3993 return NULL;
3996 bytes = talloc_array(state, uint8_t, 1);
3997 if (tevent_req_nomem(bytes, req)) {
3998 return tevent_req_post(req, ev);
4000 bytes[0] = 4;
4001 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4002 strlen(fname)+1, NULL);
4004 if (tevent_req_nomem(bytes, req)) {
4005 return tevent_req_post(req, ev);
4008 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
4009 0, NULL, talloc_get_size(bytes), bytes);
4010 if (tevent_req_nomem(subreq, req)) {
4011 return tevent_req_post(req, ev);
4013 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4014 return req;
4017 static void cli_chkpath_done(struct tevent_req *subreq)
4019 struct tevent_req *req = tevent_req_callback_data(
4020 subreq, struct tevent_req);
4021 NTSTATUS status;
4023 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4024 TALLOC_FREE(subreq);
4025 if (tevent_req_nterror(req, status)) {
4026 return;
4028 tevent_req_done(req);
4031 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4033 return tevent_req_simple_recv_ntstatus(req);
4036 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4038 TALLOC_CTX *frame = talloc_stackframe();
4039 struct tevent_context *ev = NULL;
4040 struct tevent_req *req = NULL;
4041 char *path2 = NULL;
4042 NTSTATUS status = NT_STATUS_OK;
4044 if (smbXcli_conn_has_async_calls(cli->conn)) {
4046 * Can't use sync call while an async call is in flight
4048 status = NT_STATUS_INVALID_PARAMETER;
4049 goto fail;
4052 path2 = talloc_strdup(frame, path);
4053 if (!path2) {
4054 status = NT_STATUS_NO_MEMORY;
4055 goto fail;
4057 trim_char(path2,'\0','\\');
4058 if (!*path2) {
4059 path2 = talloc_strdup(frame, "\\");
4060 if (!path2) {
4061 status = NT_STATUS_NO_MEMORY;
4062 goto fail;
4066 ev = samba_tevent_context_init(frame);
4067 if (ev == NULL) {
4068 status = NT_STATUS_NO_MEMORY;
4069 goto fail;
4072 req = cli_chkpath_send(frame, ev, cli, path2);
4073 if (req == NULL) {
4074 status = NT_STATUS_NO_MEMORY;
4075 goto fail;
4078 if (!tevent_req_poll(req, ev)) {
4079 status = map_nt_error_from_unix(errno);
4080 goto fail;
4083 status = cli_chkpath_recv(req);
4085 fail:
4086 TALLOC_FREE(frame);
4087 return status;
4090 /****************************************************************************
4091 Query disk space.
4092 ****************************************************************************/
4094 static void cli_dskattr_done(struct tevent_req *subreq);
4096 struct cli_dskattr_state {
4097 int bsize;
4098 int total;
4099 int avail;
4102 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4103 struct tevent_context *ev,
4104 struct cli_state *cli)
4106 struct tevent_req *req = NULL, *subreq = NULL;
4107 struct cli_dskattr_state *state = NULL;
4108 uint8_t additional_flags = 0;
4110 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4111 if (req == NULL) {
4112 return NULL;
4115 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
4116 0, NULL, 0, NULL);
4117 if (tevent_req_nomem(subreq, req)) {
4118 return tevent_req_post(req, ev);
4120 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4121 return req;
4124 static void cli_dskattr_done(struct tevent_req *subreq)
4126 struct tevent_req *req = tevent_req_callback_data(
4127 subreq, struct tevent_req);
4128 struct cli_dskattr_state *state = tevent_req_data(
4129 req, struct cli_dskattr_state);
4130 uint8_t wct;
4131 uint16_t *vwv = NULL;
4132 NTSTATUS status;
4134 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4135 NULL);
4136 TALLOC_FREE(subreq);
4137 if (tevent_req_nterror(req, status)) {
4138 return;
4140 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4141 state->total = SVAL(vwv+0, 0);
4142 state->avail = SVAL(vwv+3, 0);
4143 tevent_req_done(req);
4146 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4148 struct cli_dskattr_state *state = tevent_req_data(
4149 req, struct cli_dskattr_state);
4150 NTSTATUS status;
4152 if (tevent_req_is_nterror(req, &status)) {
4153 return status;
4155 *bsize = state->bsize;
4156 *total = state->total;
4157 *avail = state->avail;
4158 return NT_STATUS_OK;
4161 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4163 TALLOC_CTX *frame = NULL;
4164 struct tevent_context *ev = NULL;
4165 struct tevent_req *req = NULL;
4166 NTSTATUS status = NT_STATUS_OK;
4168 frame = talloc_stackframe();
4170 if (smbXcli_conn_has_async_calls(cli->conn)) {
4172 * Can't use sync call while an async call is in flight
4174 status = NT_STATUS_INVALID_PARAMETER;
4175 goto fail;
4178 ev = samba_tevent_context_init(frame);
4179 if (ev == NULL) {
4180 status = NT_STATUS_NO_MEMORY;
4181 goto fail;
4184 req = cli_dskattr_send(frame, ev, cli);
4185 if (req == NULL) {
4186 status = NT_STATUS_NO_MEMORY;
4187 goto fail;
4190 if (!tevent_req_poll(req, ev)) {
4191 status = map_nt_error_from_unix(errno);
4192 goto fail;
4195 status = cli_dskattr_recv(req, bsize, total, avail);
4197 fail:
4198 TALLOC_FREE(frame);
4199 return status;
4202 NTSTATUS cli_disk_size(struct cli_state *cli, uint64_t *bsize, uint64_t *total, uint64_t *avail)
4204 uint64_t sectors_per_block;
4205 uint64_t bytes_per_sector;
4206 int old_bsize, old_total, old_avail;
4207 NTSTATUS status;
4209 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4210 return cli_smb2_dskattr(cli, bsize, total, avail);
4214 * Try the trans2 disk full size info call first.
4215 * We already use this in SMBC_fstatvfs_ctx().
4216 * Ignore 'actual_available_units' as we only
4217 * care about the quota for the caller.
4220 status = cli_get_fs_full_size_info(cli,
4221 total,
4222 avail,
4223 NULL,
4224 &sectors_per_block,
4225 &bytes_per_sector);
4227 /* Try and cope will all varients of "we don't do this call"
4228 and fall back to cli_dskattr. */
4230 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4231 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4232 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4233 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4234 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4235 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4236 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4237 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4238 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4239 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4240 goto try_dskattr;
4243 if (!NT_STATUS_IS_OK(status)) {
4244 return status;
4247 if (bsize) {
4248 *bsize = sectors_per_block *
4249 bytes_per_sector;
4252 return NT_STATUS_OK;
4254 try_dskattr:
4256 /* Old SMB1 core protocol fallback. */
4257 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4258 if (!NT_STATUS_IS_OK(status)) {
4259 return status;
4261 if (bsize) {
4262 *bsize = (uint64_t)old_bsize;
4264 if (total) {
4265 *total = (uint64_t)old_total;
4267 if (avail) {
4268 *avail = (uint64_t)old_avail;
4270 return NT_STATUS_OK;
4273 /****************************************************************************
4274 Create and open a temporary file.
4275 ****************************************************************************/
4277 static void cli_ctemp_done(struct tevent_req *subreq);
4279 struct ctemp_state {
4280 uint16_t vwv[3];
4281 char *ret_path;
4282 uint16_t fnum;
4285 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4286 struct tevent_context *ev,
4287 struct cli_state *cli,
4288 const char *path)
4290 struct tevent_req *req = NULL, *subreq = NULL;
4291 struct ctemp_state *state = NULL;
4292 uint8_t additional_flags = 0;
4293 uint8_t *bytes = NULL;
4295 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4296 if (req == NULL) {
4297 return NULL;
4300 SSVAL(state->vwv,0,0);
4301 SIVALS(state->vwv+1,0,-1);
4303 bytes = talloc_array(state, uint8_t, 1);
4304 if (tevent_req_nomem(bytes, req)) {
4305 return tevent_req_post(req, ev);
4307 bytes[0] = 4;
4308 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4309 strlen(path)+1, NULL);
4310 if (tevent_req_nomem(bytes, req)) {
4311 return tevent_req_post(req, ev);
4314 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4315 3, state->vwv, talloc_get_size(bytes), bytes);
4316 if (tevent_req_nomem(subreq, req)) {
4317 return tevent_req_post(req, ev);
4319 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4320 return req;
4323 static void cli_ctemp_done(struct tevent_req *subreq)
4325 struct tevent_req *req = tevent_req_callback_data(
4326 subreq, struct tevent_req);
4327 struct ctemp_state *state = tevent_req_data(
4328 req, struct ctemp_state);
4329 NTSTATUS status;
4330 uint8_t wcnt;
4331 uint16_t *vwv;
4332 uint32_t num_bytes = 0;
4333 uint8_t *bytes = NULL;
4335 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4336 &num_bytes, &bytes);
4337 TALLOC_FREE(subreq);
4338 if (tevent_req_nterror(req, status)) {
4339 return;
4342 state->fnum = SVAL(vwv+0, 0);
4344 /* From W2K3, the result is just the ASCII name */
4345 if (num_bytes < 2) {
4346 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4347 return;
4350 if (pull_string_talloc(state,
4351 NULL,
4353 &state->ret_path,
4354 bytes,
4355 num_bytes,
4356 STR_ASCII) == 0) {
4357 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4358 return;
4360 tevent_req_done(req);
4363 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4364 TALLOC_CTX *ctx,
4365 uint16_t *pfnum,
4366 char **outfile)
4368 struct ctemp_state *state = tevent_req_data(req,
4369 struct ctemp_state);
4370 NTSTATUS status;
4372 if (tevent_req_is_nterror(req, &status)) {
4373 return status;
4375 *pfnum = state->fnum;
4376 *outfile = talloc_strdup(ctx, state->ret_path);
4377 if (!*outfile) {
4378 return NT_STATUS_NO_MEMORY;
4380 return NT_STATUS_OK;
4383 NTSTATUS cli_ctemp(struct cli_state *cli,
4384 TALLOC_CTX *ctx,
4385 const char *path,
4386 uint16_t *pfnum,
4387 char **out_path)
4389 TALLOC_CTX *frame = talloc_stackframe();
4390 struct tevent_context *ev;
4391 struct tevent_req *req;
4392 NTSTATUS status = NT_STATUS_OK;
4394 if (smbXcli_conn_has_async_calls(cli->conn)) {
4396 * Can't use sync call while an async call is in flight
4398 status = NT_STATUS_INVALID_PARAMETER;
4399 goto fail;
4402 ev = samba_tevent_context_init(frame);
4403 if (ev == NULL) {
4404 status = NT_STATUS_NO_MEMORY;
4405 goto fail;
4408 req = cli_ctemp_send(frame, ev, cli, path);
4409 if (req == NULL) {
4410 status = NT_STATUS_NO_MEMORY;
4411 goto fail;
4414 if (!tevent_req_poll(req, ev)) {
4415 status = map_nt_error_from_unix(errno);
4416 goto fail;
4419 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4421 fail:
4422 TALLOC_FREE(frame);
4423 return status;
4427 send a raw ioctl - used by the torture code
4429 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4431 uint16_t vwv[3];
4432 NTSTATUS status;
4434 SSVAL(vwv+0, 0, fnum);
4435 SSVAL(vwv+1, 0, code>>16);
4436 SSVAL(vwv+2, 0, (code&0xFFFF));
4438 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4439 NULL, 0, NULL, NULL, NULL, NULL);
4440 if (!NT_STATUS_IS_OK(status)) {
4441 return status;
4443 *blob = data_blob_null;
4444 return NT_STATUS_OK;
4447 /*********************************************************
4448 Set an extended attribute utility fn.
4449 *********************************************************/
4451 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4452 uint8_t *param, unsigned int param_len,
4453 const char *ea_name,
4454 const char *ea_val, size_t ea_len)
4456 uint16_t setup[1];
4457 unsigned int data_len = 0;
4458 uint8_t *data = NULL;
4459 char *p;
4460 size_t ea_namelen = strlen(ea_name);
4461 NTSTATUS status;
4463 SSVAL(setup, 0, setup_val);
4465 if (ea_namelen == 0 && ea_len == 0) {
4466 data_len = 4;
4467 data = talloc_array(talloc_tos(),
4468 uint8_t,
4469 data_len);
4470 if (!data) {
4471 return NT_STATUS_NO_MEMORY;
4473 p = (char *)data;
4474 SIVAL(p,0,data_len);
4475 } else {
4476 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4477 data = talloc_array(talloc_tos(),
4478 uint8_t,
4479 data_len);
4480 if (!data) {
4481 return NT_STATUS_NO_MEMORY;
4483 p = (char *)data;
4484 SIVAL(p,0,data_len);
4485 p += 4;
4486 SCVAL(p, 0, 0); /* EA flags. */
4487 SCVAL(p, 1, ea_namelen);
4488 SSVAL(p, 2, ea_len);
4489 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4490 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4493 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4494 setup, 1, 0,
4495 param, param_len, 2,
4496 data, data_len, CLI_BUFFER_SIZE,
4497 NULL,
4498 NULL, 0, NULL, /* rsetup */
4499 NULL, 0, NULL, /* rparam */
4500 NULL, 0, NULL); /* rdata */
4501 talloc_free(data);
4502 return status;
4505 /*********************************************************
4506 Set an extended attribute on a pathname.
4507 *********************************************************/
4509 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4510 const char *ea_name, const char *ea_val,
4511 size_t ea_len)
4513 unsigned int param_len = 0;
4514 uint8_t *param;
4515 NTSTATUS status;
4516 TALLOC_CTX *frame = NULL;
4518 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4519 return cli_smb2_set_ea_path(cli,
4520 path,
4521 ea_name,
4522 ea_val,
4523 ea_len);
4526 frame = talloc_stackframe();
4528 param = talloc_array(frame, uint8_t, 6);
4529 if (!param) {
4530 status = NT_STATUS_NO_MEMORY;
4531 goto fail;
4533 SSVAL(param,0,SMB_INFO_SET_EA);
4534 SSVAL(param,2,0);
4535 SSVAL(param,4,0);
4537 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4538 path, strlen(path)+1,
4539 NULL);
4540 param_len = talloc_get_size(param);
4542 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4543 ea_name, ea_val, ea_len);
4545 fail:
4547 TALLOC_FREE(frame);
4548 return status;
4551 /*********************************************************
4552 Set an extended attribute on an fnum.
4553 *********************************************************/
4555 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4556 const char *ea_name, const char *ea_val,
4557 size_t ea_len)
4559 uint8_t param[6];
4561 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4562 return cli_smb2_set_ea_fnum(cli,
4563 fnum,
4564 ea_name,
4565 ea_val,
4566 ea_len);
4569 memset(param, 0, 6);
4570 SSVAL(param,0,fnum);
4571 SSVAL(param,2,SMB_INFO_SET_EA);
4573 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4574 ea_name, ea_val, ea_len);
4577 /*********************************************************
4578 Get an extended attribute list utility fn.
4579 *********************************************************/
4581 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4582 size_t rdata_len,
4583 size_t *pnum_eas, struct ea_struct **pea_list)
4585 struct ea_struct *ea_list = NULL;
4586 size_t num_eas;
4587 size_t ea_size;
4588 const uint8_t *p;
4590 if (rdata_len < 4) {
4591 return false;
4594 ea_size = (size_t)IVAL(rdata,0);
4595 if (ea_size > rdata_len) {
4596 return false;
4599 if (ea_size == 0) {
4600 /* No EA's present. */
4601 *pnum_eas = 0;
4602 *pea_list = NULL;
4603 return true;
4606 p = rdata + 4;
4607 ea_size -= 4;
4609 /* Validate the EA list and count it. */
4610 for (num_eas = 0; ea_size >= 4; num_eas++) {
4611 unsigned int ea_namelen = CVAL(p,1);
4612 unsigned int ea_valuelen = SVAL(p,2);
4613 if (ea_namelen == 0) {
4614 return false;
4616 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4617 return false;
4619 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4620 p += 4 + ea_namelen + 1 + ea_valuelen;
4623 if (num_eas == 0) {
4624 *pnum_eas = 0;
4625 *pea_list = NULL;
4626 return true;
4629 *pnum_eas = num_eas;
4630 if (!pea_list) {
4631 /* Caller only wants number of EA's. */
4632 return true;
4635 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4636 if (!ea_list) {
4637 return false;
4640 ea_size = (size_t)IVAL(rdata,0);
4641 p = rdata + 4;
4643 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4644 struct ea_struct *ea = &ea_list[num_eas];
4645 fstring unix_ea_name;
4646 unsigned int ea_namelen = CVAL(p,1);
4647 unsigned int ea_valuelen = SVAL(p,2);
4649 ea->flags = CVAL(p,0);
4650 unix_ea_name[0] = '\0';
4651 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4652 ea->name = talloc_strdup(ea_list, unix_ea_name);
4653 if (!ea->name) {
4654 goto fail;
4656 /* Ensure the value is null terminated (in case it's a string). */
4657 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4658 if (!ea->value.data) {
4659 goto fail;
4661 if (ea_valuelen) {
4662 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4664 ea->value.data[ea_valuelen] = 0;
4665 ea->value.length--;
4666 p += 4 + ea_namelen + 1 + ea_valuelen;
4669 *pea_list = ea_list;
4670 return true;
4672 fail:
4673 TALLOC_FREE(ea_list);
4674 return false;
4677 /*********************************************************
4678 Get an extended attribute list from a pathname.
4679 *********************************************************/
4681 struct cli_get_ea_list_path_state {
4682 uint32_t num_data;
4683 uint8_t *data;
4686 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4688 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4689 struct tevent_context *ev,
4690 struct cli_state *cli,
4691 const char *fname)
4693 struct tevent_req *req, *subreq;
4694 struct cli_get_ea_list_path_state *state;
4696 req = tevent_req_create(mem_ctx, &state,
4697 struct cli_get_ea_list_path_state);
4698 if (req == NULL) {
4699 return NULL;
4701 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4702 SMB_INFO_QUERY_ALL_EAS, 4,
4703 CLI_BUFFER_SIZE);
4704 if (tevent_req_nomem(subreq, req)) {
4705 return tevent_req_post(req, ev);
4707 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4708 return req;
4711 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4713 struct tevent_req *req = tevent_req_callback_data(
4714 subreq, struct tevent_req);
4715 struct cli_get_ea_list_path_state *state = tevent_req_data(
4716 req, struct cli_get_ea_list_path_state);
4717 NTSTATUS status;
4719 status = cli_qpathinfo_recv(subreq, state, &state->data,
4720 &state->num_data);
4721 TALLOC_FREE(subreq);
4722 if (tevent_req_nterror(req, status)) {
4723 return;
4725 tevent_req_done(req);
4728 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4729 size_t *pnum_eas, struct ea_struct **peas)
4731 struct cli_get_ea_list_path_state *state = tevent_req_data(
4732 req, struct cli_get_ea_list_path_state);
4733 NTSTATUS status;
4735 if (tevent_req_is_nterror(req, &status)) {
4736 return status;
4738 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4739 pnum_eas, peas)) {
4740 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4742 return NT_STATUS_OK;
4745 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4746 TALLOC_CTX *ctx,
4747 size_t *pnum_eas,
4748 struct ea_struct **pea_list)
4750 TALLOC_CTX *frame = NULL;
4751 struct tevent_context *ev = NULL;
4752 struct tevent_req *req = NULL;
4753 NTSTATUS status = NT_STATUS_NO_MEMORY;
4755 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4756 return cli_smb2_get_ea_list_path(cli,
4757 path,
4758 ctx,
4759 pnum_eas,
4760 pea_list);
4763 frame = talloc_stackframe();
4765 if (smbXcli_conn_has_async_calls(cli->conn)) {
4767 * Can't use sync call while an async call is in flight
4769 status = NT_STATUS_INVALID_PARAMETER;
4770 goto fail;
4772 ev = samba_tevent_context_init(frame);
4773 if (ev == NULL) {
4774 goto fail;
4776 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4777 if (req == NULL) {
4778 goto fail;
4780 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4781 goto fail;
4783 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4784 fail:
4785 TALLOC_FREE(frame);
4786 return status;
4789 /****************************************************************************
4790 Convert open "flags" arg to uint32_t on wire.
4791 ****************************************************************************/
4793 static uint32_t open_flags_to_wire(int flags)
4795 int open_mode = flags & O_ACCMODE;
4796 uint32_t ret = 0;
4798 switch (open_mode) {
4799 case O_WRONLY:
4800 ret |= SMB_O_WRONLY;
4801 break;
4802 case O_RDWR:
4803 ret |= SMB_O_RDWR;
4804 break;
4805 default:
4806 case O_RDONLY:
4807 ret |= SMB_O_RDONLY;
4808 break;
4811 if (flags & O_CREAT) {
4812 ret |= SMB_O_CREAT;
4814 if (flags & O_EXCL) {
4815 ret |= SMB_O_EXCL;
4817 if (flags & O_TRUNC) {
4818 ret |= SMB_O_TRUNC;
4820 #if defined(O_SYNC)
4821 if (flags & O_SYNC) {
4822 ret |= SMB_O_SYNC;
4824 #endif /* O_SYNC */
4825 if (flags & O_APPEND) {
4826 ret |= SMB_O_APPEND;
4828 #if defined(O_DIRECT)
4829 if (flags & O_DIRECT) {
4830 ret |= SMB_O_DIRECT;
4832 #endif
4833 #if defined(O_DIRECTORY)
4834 if (flags & O_DIRECTORY) {
4835 ret |= SMB_O_DIRECTORY;
4837 #endif
4838 return ret;
4841 /****************************************************************************
4842 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4843 ****************************************************************************/
4845 struct posix_open_state {
4846 uint16_t setup;
4847 uint8_t *param;
4848 uint8_t data[18];
4849 uint16_t fnum; /* Out */
4852 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4854 struct tevent_req *req = tevent_req_callback_data(
4855 subreq, struct tevent_req);
4856 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4857 NTSTATUS status;
4858 uint8_t *data;
4859 uint32_t num_data;
4861 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4862 NULL, 0, NULL, &data, 12, &num_data);
4863 TALLOC_FREE(subreq);
4864 if (tevent_req_nterror(req, status)) {
4865 return;
4867 state->fnum = SVAL(data,2);
4868 tevent_req_done(req);
4871 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4872 struct tevent_context *ev,
4873 struct cli_state *cli,
4874 const char *fname,
4875 int flags,
4876 mode_t mode,
4877 bool is_dir)
4879 struct tevent_req *req = NULL, *subreq = NULL;
4880 struct posix_open_state *state = NULL;
4881 uint32_t wire_flags = open_flags_to_wire(flags);
4883 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4884 if (req == NULL) {
4885 return NULL;
4888 /* Setup setup word. */
4889 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4891 /* Setup param array. */
4892 state->param = talloc_array(state, uint8_t, 6);
4893 if (tevent_req_nomem(state->param, req)) {
4894 return tevent_req_post(req, ev);
4896 memset(state->param, '\0', 6);
4897 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4899 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4900 strlen(fname)+1, NULL);
4902 if (tevent_req_nomem(state->param, req)) {
4903 return tevent_req_post(req, ev);
4906 /* Setup data words. */
4907 if (is_dir) {
4908 wire_flags |= SMB_O_DIRECTORY;
4911 SIVAL(state->data,0,0); /* No oplock. */
4912 SIVAL(state->data,4,wire_flags);
4913 SIVAL(state->data,8,unix_perms_to_wire(mode));
4914 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4915 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4917 subreq = cli_trans_send(state, /* mem ctx. */
4918 ev, /* event ctx. */
4919 cli, /* cli_state. */
4920 SMBtrans2, /* cmd. */
4921 NULL, /* pipe name. */
4922 -1, /* fid. */
4923 0, /* function. */
4924 0, /* flags. */
4925 &state->setup, /* setup. */
4926 1, /* num setup uint16_t words. */
4927 0, /* max returned setup. */
4928 state->param, /* param. */
4929 talloc_get_size(state->param),/* num param. */
4930 2, /* max returned param. */
4931 state->data, /* data. */
4932 18, /* num data. */
4933 12); /* max returned data. */
4935 if (tevent_req_nomem(subreq, req)) {
4936 return tevent_req_post(req, ev);
4938 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4939 return req;
4942 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4943 struct tevent_context *ev,
4944 struct cli_state *cli,
4945 const char *fname,
4946 int flags,
4947 mode_t mode)
4949 return cli_posix_open_internal_send(mem_ctx, ev,
4950 cli, fname, flags, mode, false);
4953 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4955 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4956 NTSTATUS status;
4958 if (tevent_req_is_nterror(req, &status)) {
4959 return status;
4961 *pfnum = state->fnum;
4962 return NT_STATUS_OK;
4965 /****************************************************************************
4966 Open - POSIX semantics. Doesn't request oplock.
4967 ****************************************************************************/
4969 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4970 int flags, mode_t mode, uint16_t *pfnum)
4973 TALLOC_CTX *frame = talloc_stackframe();
4974 struct tevent_context *ev = NULL;
4975 struct tevent_req *req = NULL;
4976 NTSTATUS status = NT_STATUS_OK;
4978 if (smbXcli_conn_has_async_calls(cli->conn)) {
4980 * Can't use sync call while an async call is in flight
4982 status = NT_STATUS_INVALID_PARAMETER;
4983 goto fail;
4986 ev = samba_tevent_context_init(frame);
4987 if (ev == NULL) {
4988 status = NT_STATUS_NO_MEMORY;
4989 goto fail;
4992 req = cli_posix_open_send(frame,
4994 cli,
4995 fname,
4996 flags,
4997 mode);
4998 if (req == NULL) {
4999 status = NT_STATUS_NO_MEMORY;
5000 goto fail;
5003 if (!tevent_req_poll(req, ev)) {
5004 status = map_nt_error_from_unix(errno);
5005 goto fail;
5008 status = cli_posix_open_recv(req, pfnum);
5010 fail:
5011 TALLOC_FREE(frame);
5012 return status;
5015 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5016 struct tevent_context *ev,
5017 struct cli_state *cli,
5018 const char *fname,
5019 mode_t mode)
5021 return cli_posix_open_internal_send(mem_ctx, ev,
5022 cli, fname, O_CREAT, mode, true);
5025 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5027 return tevent_req_simple_recv_ntstatus(req);
5030 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5032 TALLOC_CTX *frame = talloc_stackframe();
5033 struct tevent_context *ev = NULL;
5034 struct tevent_req *req = NULL;
5035 NTSTATUS status = NT_STATUS_OK;
5037 if (smbXcli_conn_has_async_calls(cli->conn)) {
5039 * Can't use sync call while an async call is in flight
5041 status = NT_STATUS_INVALID_PARAMETER;
5042 goto fail;
5045 ev = samba_tevent_context_init(frame);
5046 if (ev == NULL) {
5047 status = NT_STATUS_NO_MEMORY;
5048 goto fail;
5051 req = cli_posix_mkdir_send(frame,
5053 cli,
5054 fname,
5055 mode);
5056 if (req == NULL) {
5057 status = NT_STATUS_NO_MEMORY;
5058 goto fail;
5061 if (!tevent_req_poll(req, ev)) {
5062 status = map_nt_error_from_unix(errno);
5063 goto fail;
5066 status = cli_posix_mkdir_recv(req);
5068 fail:
5069 TALLOC_FREE(frame);
5070 return status;
5073 /****************************************************************************
5074 unlink or rmdir - POSIX semantics.
5075 ****************************************************************************/
5077 struct cli_posix_unlink_internal_state {
5078 uint8_t data[2];
5081 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5083 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5084 struct tevent_context *ev,
5085 struct cli_state *cli,
5086 const char *fname,
5087 uint16_t level)
5089 struct tevent_req *req = NULL, *subreq = NULL;
5090 struct cli_posix_unlink_internal_state *state = NULL;
5092 req = tevent_req_create(mem_ctx, &state,
5093 struct cli_posix_unlink_internal_state);
5094 if (req == NULL) {
5095 return NULL;
5098 /* Setup data word. */
5099 SSVAL(state->data, 0, level);
5101 subreq = cli_setpathinfo_send(state, ev, cli,
5102 SMB_POSIX_PATH_UNLINK,
5103 fname,
5104 state->data, sizeof(state->data));
5105 if (tevent_req_nomem(subreq, req)) {
5106 return tevent_req_post(req, ev);
5108 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5109 return req;
5112 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5114 NTSTATUS status = cli_setpathinfo_recv(subreq);
5115 tevent_req_simple_finish_ntstatus(subreq, status);
5118 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5119 struct tevent_context *ev,
5120 struct cli_state *cli,
5121 const char *fname)
5123 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5124 SMB_POSIX_UNLINK_FILE_TARGET);
5127 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5129 return tevent_req_simple_recv_ntstatus(req);
5132 /****************************************************************************
5133 unlink - POSIX semantics.
5134 ****************************************************************************/
5136 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5138 TALLOC_CTX *frame = talloc_stackframe();
5139 struct tevent_context *ev = NULL;
5140 struct tevent_req *req = NULL;
5141 NTSTATUS status = NT_STATUS_OK;
5143 if (smbXcli_conn_has_async_calls(cli->conn)) {
5145 * Can't use sync call while an async call is in flight
5147 status = NT_STATUS_INVALID_PARAMETER;
5148 goto fail;
5151 ev = samba_tevent_context_init(frame);
5152 if (ev == NULL) {
5153 status = NT_STATUS_NO_MEMORY;
5154 goto fail;
5157 req = cli_posix_unlink_send(frame,
5159 cli,
5160 fname);
5161 if (req == NULL) {
5162 status = NT_STATUS_NO_MEMORY;
5163 goto fail;
5166 if (!tevent_req_poll(req, ev)) {
5167 status = map_nt_error_from_unix(errno);
5168 goto fail;
5171 status = cli_posix_unlink_recv(req);
5173 fail:
5174 TALLOC_FREE(frame);
5175 return status;
5178 /****************************************************************************
5179 rmdir - POSIX semantics.
5180 ****************************************************************************/
5182 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5183 struct tevent_context *ev,
5184 struct cli_state *cli,
5185 const char *fname)
5187 return cli_posix_unlink_internal_send(
5188 mem_ctx, ev, cli, fname,
5189 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5192 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5194 return tevent_req_simple_recv_ntstatus(req);
5197 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5199 TALLOC_CTX *frame = talloc_stackframe();
5200 struct tevent_context *ev = NULL;
5201 struct tevent_req *req = NULL;
5202 NTSTATUS status = NT_STATUS_OK;
5204 if (smbXcli_conn_has_async_calls(cli->conn)) {
5206 * Can't use sync call while an async call is in flight
5208 status = NT_STATUS_INVALID_PARAMETER;
5209 goto fail;
5212 ev = samba_tevent_context_init(frame);
5213 if (ev == NULL) {
5214 status = NT_STATUS_NO_MEMORY;
5215 goto fail;
5218 req = cli_posix_rmdir_send(frame,
5220 cli,
5221 fname);
5222 if (req == NULL) {
5223 status = NT_STATUS_NO_MEMORY;
5224 goto fail;
5227 if (!tevent_req_poll(req, ev)) {
5228 status = map_nt_error_from_unix(errno);
5229 goto fail;
5232 status = cli_posix_rmdir_recv(req, frame);
5234 fail:
5235 TALLOC_FREE(frame);
5236 return status;
5239 /****************************************************************************
5240 filechangenotify
5241 ****************************************************************************/
5243 struct cli_notify_state {
5244 uint8_t setup[8];
5245 uint32_t num_changes;
5246 struct notify_change *changes;
5249 static void cli_notify_done(struct tevent_req *subreq);
5251 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5252 struct tevent_context *ev,
5253 struct cli_state *cli, uint16_t fnum,
5254 uint32_t buffer_size,
5255 uint32_t completion_filter, bool recursive)
5257 struct tevent_req *req, *subreq;
5258 struct cli_notify_state *state;
5259 unsigned old_timeout;
5261 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5262 if (req == NULL) {
5263 return NULL;
5266 SIVAL(state->setup, 0, completion_filter);
5267 SSVAL(state->setup, 4, fnum);
5268 SSVAL(state->setup, 6, recursive);
5271 * Notifies should not time out
5273 old_timeout = cli_set_timeout(cli, 0);
5275 subreq = cli_trans_send(
5276 state, /* mem ctx. */
5277 ev, /* event ctx. */
5278 cli, /* cli_state. */
5279 SMBnttrans, /* cmd. */
5280 NULL, /* pipe name. */
5281 -1, /* fid. */
5282 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5283 0, /* flags. */
5284 (uint16_t *)state->setup, /* setup. */
5285 4, /* num setup uint16_t words. */
5286 0, /* max returned setup. */
5287 NULL, /* param. */
5288 0, /* num param. */
5289 buffer_size, /* max returned param. */
5290 NULL, /* data. */
5291 0, /* num data. */
5292 0); /* max returned data. */
5294 cli_set_timeout(cli, old_timeout);
5296 if (tevent_req_nomem(subreq, req)) {
5297 return tevent_req_post(req, ev);
5299 tevent_req_set_callback(subreq, cli_notify_done, req);
5300 return req;
5303 static void cli_notify_done(struct tevent_req *subreq)
5305 struct tevent_req *req = tevent_req_callback_data(
5306 subreq, struct tevent_req);
5307 struct cli_notify_state *state = tevent_req_data(
5308 req, struct cli_notify_state);
5309 NTSTATUS status;
5310 uint8_t *params;
5311 uint32_t i, ofs, num_params;
5312 uint16_t flags2;
5314 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5315 &params, 0, &num_params, NULL, 0, NULL);
5316 TALLOC_FREE(subreq);
5317 if (tevent_req_nterror(req, status)) {
5318 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5319 return;
5322 state->num_changes = 0;
5323 ofs = 0;
5325 while (num_params - ofs > 12) {
5326 uint32_t next = IVAL(params, ofs);
5327 state->num_changes += 1;
5329 if ((next == 0) || (ofs+next >= num_params)) {
5330 break;
5332 ofs += next;
5335 state->changes = talloc_array(state, struct notify_change,
5336 state->num_changes);
5337 if (tevent_req_nomem(state->changes, req)) {
5338 TALLOC_FREE(params);
5339 return;
5342 ofs = 0;
5344 for (i=0; i<state->num_changes; i++) {
5345 uint32_t next = IVAL(params, ofs);
5346 uint32_t len = IVAL(params, ofs+8);
5347 ssize_t ret;
5348 char *name;
5350 if (trans_oob(num_params, ofs + 12, len)) {
5351 TALLOC_FREE(params);
5352 tevent_req_nterror(
5353 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5354 return;
5357 state->changes[i].action = IVAL(params, ofs+4);
5358 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5359 &name, params+ofs+12, len,
5360 STR_TERMINATE|STR_UNICODE);
5361 if (ret == -1) {
5362 TALLOC_FREE(params);
5363 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5364 return;
5366 state->changes[i].name = name;
5367 ofs += next;
5370 TALLOC_FREE(params);
5371 tevent_req_done(req);
5374 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5375 uint32_t *pnum_changes,
5376 struct notify_change **pchanges)
5378 struct cli_notify_state *state = tevent_req_data(
5379 req, struct cli_notify_state);
5380 NTSTATUS status;
5382 if (tevent_req_is_nterror(req, &status)) {
5383 return status;
5386 *pnum_changes = state->num_changes;
5387 *pchanges = talloc_move(mem_ctx, &state->changes);
5388 return NT_STATUS_OK;
5391 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5392 uint32_t completion_filter, bool recursive,
5393 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5394 struct notify_change **pchanges)
5396 TALLOC_CTX *frame = talloc_stackframe();
5397 struct tevent_context *ev;
5398 struct tevent_req *req;
5399 NTSTATUS status = NT_STATUS_NO_MEMORY;
5401 if (smbXcli_conn_has_async_calls(cli->conn)) {
5403 * Can't use sync call while an async call is in flight
5405 status = NT_STATUS_INVALID_PARAMETER;
5406 goto fail;
5408 ev = samba_tevent_context_init(frame);
5409 if (ev == NULL) {
5410 goto fail;
5412 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5413 completion_filter, recursive);
5414 if (req == NULL) {
5415 goto fail;
5417 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5418 goto fail;
5420 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5421 fail:
5422 TALLOC_FREE(frame);
5423 return status;
5426 struct cli_qpathinfo_state {
5427 uint8_t *param;
5428 uint8_t *data;
5429 uint16_t setup[1];
5430 uint32_t min_rdata;
5431 uint8_t *rdata;
5432 uint32_t num_rdata;
5435 static void cli_qpathinfo_done(struct tevent_req *subreq);
5437 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5438 struct tevent_context *ev,
5439 struct cli_state *cli, const char *fname,
5440 uint16_t level, uint32_t min_rdata,
5441 uint32_t max_rdata)
5443 struct tevent_req *req, *subreq;
5444 struct cli_qpathinfo_state *state;
5446 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5447 if (req == NULL) {
5448 return NULL;
5450 state->min_rdata = min_rdata;
5451 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5453 state->param = talloc_zero_array(state, uint8_t, 6);
5454 if (tevent_req_nomem(state->param, req)) {
5455 return tevent_req_post(req, ev);
5457 SSVAL(state->param, 0, level);
5458 state->param = trans2_bytes_push_str(
5459 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5460 if (tevent_req_nomem(state->param, req)) {
5461 return tevent_req_post(req, ev);
5464 subreq = cli_trans_send(
5465 state, /* mem ctx. */
5466 ev, /* event ctx. */
5467 cli, /* cli_state. */
5468 SMBtrans2, /* cmd. */
5469 NULL, /* pipe name. */
5470 -1, /* fid. */
5471 0, /* function. */
5472 0, /* flags. */
5473 state->setup, /* setup. */
5474 1, /* num setup uint16_t words. */
5475 0, /* max returned setup. */
5476 state->param, /* param. */
5477 talloc_get_size(state->param), /* num param. */
5478 2, /* max returned param. */
5479 NULL, /* data. */
5480 0, /* num data. */
5481 max_rdata); /* max returned data. */
5483 if (tevent_req_nomem(subreq, req)) {
5484 return tevent_req_post(req, ev);
5486 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5487 return req;
5490 static void cli_qpathinfo_done(struct tevent_req *subreq)
5492 struct tevent_req *req = tevent_req_callback_data(
5493 subreq, struct tevent_req);
5494 struct cli_qpathinfo_state *state = tevent_req_data(
5495 req, struct cli_qpathinfo_state);
5496 NTSTATUS status;
5498 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5499 NULL, 0, NULL,
5500 &state->rdata, state->min_rdata,
5501 &state->num_rdata);
5502 if (tevent_req_nterror(req, status)) {
5503 return;
5505 tevent_req_done(req);
5508 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5509 uint8_t **rdata, uint32_t *num_rdata)
5511 struct cli_qpathinfo_state *state = tevent_req_data(
5512 req, struct cli_qpathinfo_state);
5513 NTSTATUS status;
5515 if (tevent_req_is_nterror(req, &status)) {
5516 return status;
5518 if (rdata != NULL) {
5519 *rdata = talloc_move(mem_ctx, &state->rdata);
5520 } else {
5521 TALLOC_FREE(state->rdata);
5523 if (num_rdata != NULL) {
5524 *num_rdata = state->num_rdata;
5526 return NT_STATUS_OK;
5529 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5530 const char *fname, uint16_t level, uint32_t min_rdata,
5531 uint32_t max_rdata,
5532 uint8_t **rdata, uint32_t *num_rdata)
5534 TALLOC_CTX *frame = talloc_stackframe();
5535 struct tevent_context *ev;
5536 struct tevent_req *req;
5537 NTSTATUS status = NT_STATUS_NO_MEMORY;
5539 if (smbXcli_conn_has_async_calls(cli->conn)) {
5541 * Can't use sync call while an async call is in flight
5543 status = NT_STATUS_INVALID_PARAMETER;
5544 goto fail;
5546 ev = samba_tevent_context_init(frame);
5547 if (ev == NULL) {
5548 goto fail;
5550 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5551 max_rdata);
5552 if (req == NULL) {
5553 goto fail;
5555 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5556 goto fail;
5558 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5559 fail:
5560 TALLOC_FREE(frame);
5561 return status;
5564 struct cli_qfileinfo_state {
5565 uint16_t setup[1];
5566 uint8_t param[4];
5567 uint8_t *data;
5568 uint16_t recv_flags2;
5569 uint32_t min_rdata;
5570 uint8_t *rdata;
5571 uint32_t num_rdata;
5574 static void cli_qfileinfo_done(struct tevent_req *subreq);
5576 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5577 struct tevent_context *ev,
5578 struct cli_state *cli, uint16_t fnum,
5579 uint16_t level, uint32_t min_rdata,
5580 uint32_t max_rdata)
5582 struct tevent_req *req, *subreq;
5583 struct cli_qfileinfo_state *state;
5585 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5586 if (req == NULL) {
5587 return NULL;
5589 state->min_rdata = min_rdata;
5590 SSVAL(state->param, 0, fnum);
5591 SSVAL(state->param, 2, level);
5592 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5594 subreq = cli_trans_send(
5595 state, /* mem ctx. */
5596 ev, /* event ctx. */
5597 cli, /* cli_state. */
5598 SMBtrans2, /* cmd. */
5599 NULL, /* pipe name. */
5600 -1, /* fid. */
5601 0, /* function. */
5602 0, /* flags. */
5603 state->setup, /* setup. */
5604 1, /* num setup uint16_t words. */
5605 0, /* max returned setup. */
5606 state->param, /* param. */
5607 sizeof(state->param), /* num param. */
5608 2, /* max returned param. */
5609 NULL, /* data. */
5610 0, /* num data. */
5611 max_rdata); /* max returned data. */
5613 if (tevent_req_nomem(subreq, req)) {
5614 return tevent_req_post(req, ev);
5616 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5617 return req;
5620 static void cli_qfileinfo_done(struct tevent_req *subreq)
5622 struct tevent_req *req = tevent_req_callback_data(
5623 subreq, struct tevent_req);
5624 struct cli_qfileinfo_state *state = tevent_req_data(
5625 req, struct cli_qfileinfo_state);
5626 NTSTATUS status;
5628 status = cli_trans_recv(subreq, state,
5629 &state->recv_flags2,
5630 NULL, 0, NULL,
5631 NULL, 0, NULL,
5632 &state->rdata, state->min_rdata,
5633 &state->num_rdata);
5634 if (tevent_req_nterror(req, status)) {
5635 return;
5637 tevent_req_done(req);
5640 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5641 uint16_t *recv_flags2,
5642 uint8_t **rdata, uint32_t *num_rdata)
5644 struct cli_qfileinfo_state *state = tevent_req_data(
5645 req, struct cli_qfileinfo_state);
5646 NTSTATUS status;
5648 if (tevent_req_is_nterror(req, &status)) {
5649 return status;
5652 if (recv_flags2 != NULL) {
5653 *recv_flags2 = state->recv_flags2;
5655 if (rdata != NULL) {
5656 *rdata = talloc_move(mem_ctx, &state->rdata);
5657 } else {
5658 TALLOC_FREE(state->rdata);
5660 if (num_rdata != NULL) {
5661 *num_rdata = state->num_rdata;
5663 return NT_STATUS_OK;
5666 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5667 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5668 uint32_t max_rdata, uint16_t *recv_flags2,
5669 uint8_t **rdata, uint32_t *num_rdata)
5671 TALLOC_CTX *frame = talloc_stackframe();
5672 struct tevent_context *ev;
5673 struct tevent_req *req;
5674 NTSTATUS status = NT_STATUS_NO_MEMORY;
5676 if (smbXcli_conn_has_async_calls(cli->conn)) {
5678 * Can't use sync call while an async call is in flight
5680 status = NT_STATUS_INVALID_PARAMETER;
5681 goto fail;
5683 ev = samba_tevent_context_init(frame);
5684 if (ev == NULL) {
5685 goto fail;
5687 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5688 max_rdata);
5689 if (req == NULL) {
5690 goto fail;
5692 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5693 goto fail;
5695 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5696 fail:
5697 TALLOC_FREE(frame);
5698 return status;
5701 struct cli_flush_state {
5702 uint16_t vwv[1];
5705 static void cli_flush_done(struct tevent_req *subreq);
5707 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5708 struct tevent_context *ev,
5709 struct cli_state *cli,
5710 uint16_t fnum)
5712 struct tevent_req *req, *subreq;
5713 struct cli_flush_state *state;
5715 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5716 if (req == NULL) {
5717 return NULL;
5719 SSVAL(state->vwv + 0, 0, fnum);
5721 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5722 0, NULL);
5723 if (tevent_req_nomem(subreq, req)) {
5724 return tevent_req_post(req, ev);
5726 tevent_req_set_callback(subreq, cli_flush_done, req);
5727 return req;
5730 static void cli_flush_done(struct tevent_req *subreq)
5732 struct tevent_req *req = tevent_req_callback_data(
5733 subreq, struct tevent_req);
5734 NTSTATUS status;
5736 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5737 TALLOC_FREE(subreq);
5738 if (tevent_req_nterror(req, status)) {
5739 return;
5741 tevent_req_done(req);
5744 NTSTATUS cli_flush_recv(struct tevent_req *req)
5746 return tevent_req_simple_recv_ntstatus(req);
5749 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5751 TALLOC_CTX *frame = talloc_stackframe();
5752 struct tevent_context *ev;
5753 struct tevent_req *req;
5754 NTSTATUS status = NT_STATUS_NO_MEMORY;
5756 if (smbXcli_conn_has_async_calls(cli->conn)) {
5758 * Can't use sync call while an async call is in flight
5760 status = NT_STATUS_INVALID_PARAMETER;
5761 goto fail;
5763 ev = samba_tevent_context_init(frame);
5764 if (ev == NULL) {
5765 goto fail;
5767 req = cli_flush_send(frame, ev, cli, fnum);
5768 if (req == NULL) {
5769 goto fail;
5771 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5772 goto fail;
5774 status = cli_flush_recv(req);
5775 fail:
5776 TALLOC_FREE(frame);
5777 return status;
5780 struct cli_shadow_copy_data_state {
5781 uint16_t setup[4];
5782 uint8_t *data;
5783 uint32_t num_data;
5784 bool get_names;
5787 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5789 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5790 struct tevent_context *ev,
5791 struct cli_state *cli,
5792 uint16_t fnum,
5793 bool get_names)
5795 struct tevent_req *req, *subreq;
5796 struct cli_shadow_copy_data_state *state;
5797 uint32_t ret_size;
5799 req = tevent_req_create(mem_ctx, &state,
5800 struct cli_shadow_copy_data_state);
5801 if (req == NULL) {
5802 return NULL;
5804 state->get_names = get_names;
5805 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5807 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5808 SSVAL(state->setup + 2, 0, fnum);
5809 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5810 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5812 subreq = cli_trans_send(
5813 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5814 state->setup, ARRAY_SIZE(state->setup), 0,
5815 NULL, 0, 0,
5816 NULL, 0, ret_size);
5817 if (tevent_req_nomem(subreq, req)) {
5818 return tevent_req_post(req, ev);
5820 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5821 return req;
5824 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5826 struct tevent_req *req = tevent_req_callback_data(
5827 subreq, struct tevent_req);
5828 struct cli_shadow_copy_data_state *state = tevent_req_data(
5829 req, struct cli_shadow_copy_data_state);
5830 NTSTATUS status;
5832 status = cli_trans_recv(subreq, state, NULL,
5833 NULL, 0, NULL, /* setup */
5834 NULL, 0, NULL, /* param */
5835 &state->data, 12, &state->num_data);
5836 TALLOC_FREE(subreq);
5837 if (tevent_req_nterror(req, status)) {
5838 return;
5840 tevent_req_done(req);
5843 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5844 char ***pnames, int *pnum_names)
5846 struct cli_shadow_copy_data_state *state = tevent_req_data(
5847 req, struct cli_shadow_copy_data_state);
5848 char **names;
5849 int i, num_names;
5850 uint32_t dlength;
5851 NTSTATUS status;
5853 if (tevent_req_is_nterror(req, &status)) {
5854 return status;
5856 num_names = IVAL(state->data, 4);
5857 dlength = IVAL(state->data, 8);
5859 if (!state->get_names) {
5860 *pnum_names = num_names;
5861 return NT_STATUS_OK;
5864 if (dlength+12 > state->num_data) {
5865 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5867 names = talloc_array(mem_ctx, char *, num_names);
5868 if (names == NULL) {
5869 return NT_STATUS_NO_MEMORY;
5872 for (i=0; i<num_names; i++) {
5873 bool ret;
5874 uint8_t *src;
5875 size_t converted_size;
5877 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5878 ret = convert_string_talloc(
5879 names, CH_UTF16LE, CH_UNIX,
5880 src, 2 * sizeof(SHADOW_COPY_LABEL),
5881 &names[i], &converted_size);
5882 if (!ret) {
5883 TALLOC_FREE(names);
5884 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5887 *pnum_names = num_names;
5888 *pnames = names;
5889 return NT_STATUS_OK;
5892 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5893 uint16_t fnum, bool get_names,
5894 char ***pnames, int *pnum_names)
5896 TALLOC_CTX *frame = talloc_stackframe();
5897 struct tevent_context *ev;
5898 struct tevent_req *req;
5899 NTSTATUS status = NT_STATUS_NO_MEMORY;
5901 if (smbXcli_conn_has_async_calls(cli->conn)) {
5903 * Can't use sync call while an async call is in flight
5905 status = NT_STATUS_INVALID_PARAMETER;
5906 goto fail;
5908 ev = samba_tevent_context_init(frame);
5909 if (ev == NULL) {
5910 goto fail;
5912 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5913 if (req == NULL) {
5914 goto fail;
5916 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5917 goto fail;
5919 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5920 fail:
5921 TALLOC_FREE(frame);
5922 return status;