lib: Remove unused parmlist code
[Samba.git] / source3 / libsmb / clifile.c
blob9e1975b10610ffbd359afbdf019248032be8025e
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_ntstatus(req, ev, &status)) {
376 goto fail;
379 status = cli_posix_symlink_recv(req);
381 fail:
382 TALLOC_FREE(frame);
383 return status;
386 /****************************************************************************
387 Read a POSIX symlink.
388 ****************************************************************************/
390 struct readlink_state {
391 uint8_t *data;
392 uint32_t num_data;
395 static void cli_posix_readlink_done(struct tevent_req *subreq);
397 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
398 struct tevent_context *ev,
399 struct cli_state *cli,
400 const char *fname,
401 size_t len)
403 struct tevent_req *req = NULL, *subreq = NULL;
404 struct readlink_state *state = NULL;
405 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
407 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
408 if (req == NULL) {
409 return NULL;
413 * Len is in bytes, we need it in UCS2 units.
415 if ((2*len < len) || (maxbytelen < len)) {
416 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
417 return tevent_req_post(req, ev);
420 subreq = cli_qpathinfo_send(state, ev, cli, fname,
421 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
422 if (tevent_req_nomem(subreq, req)) {
423 return tevent_req_post(req, ev);
425 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
426 return req;
429 static void cli_posix_readlink_done(struct tevent_req *subreq)
431 struct tevent_req *req = tevent_req_callback_data(
432 subreq, struct tevent_req);
433 struct readlink_state *state = tevent_req_data(
434 req, struct readlink_state);
435 NTSTATUS status;
437 status = cli_qpathinfo_recv(subreq, state, &state->data,
438 &state->num_data);
439 TALLOC_FREE(subreq);
440 if (tevent_req_nterror(req, status)) {
441 return;
444 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
446 if (state->data[state->num_data-1] != '\0') {
447 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
448 return;
450 tevent_req_done(req);
453 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
454 char *retpath, size_t len)
456 NTSTATUS status;
457 char *converted = NULL;
458 size_t converted_size = 0;
459 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
461 if (tevent_req_is_nterror(req, &status)) {
462 return status;
464 /* The returned data is a pushed string, not raw data. */
465 if (!convert_string_talloc(state,
466 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
467 CH_UNIX,
468 state->data,
469 state->num_data,
470 &converted,
471 &converted_size)) {
472 return NT_STATUS_NO_MEMORY;
475 len = MIN(len,converted_size);
476 if (len == 0) {
477 return NT_STATUS_DATA_ERROR;
479 memcpy(retpath, converted, len);
480 return NT_STATUS_OK;
483 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
484 char *linkpath, size_t len)
486 TALLOC_CTX *frame = talloc_stackframe();
487 struct tevent_context *ev = NULL;
488 struct tevent_req *req = NULL;
489 NTSTATUS status = NT_STATUS_OK;
491 if (smbXcli_conn_has_async_calls(cli->conn)) {
493 * Can't use sync call while an async call is in flight
495 status = NT_STATUS_INVALID_PARAMETER;
496 goto fail;
499 ev = samba_tevent_context_init(frame);
500 if (ev == NULL) {
501 status = NT_STATUS_NO_MEMORY;
502 goto fail;
505 req = cli_posix_readlink_send(frame,
507 cli,
508 fname,
509 len);
510 if (req == NULL) {
511 status = NT_STATUS_NO_MEMORY;
512 goto fail;
515 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
516 goto fail;
519 status = cli_posix_readlink_recv(req, cli, linkpath, len);
521 fail:
522 TALLOC_FREE(frame);
523 return status;
526 /****************************************************************************
527 Hard link a file (UNIX extensions).
528 ****************************************************************************/
530 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
531 struct tevent_context *ev,
532 struct cli_state *cli,
533 const char *oldname,
534 const char *newname)
536 return cli_posix_link_internal_send(
537 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
540 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
542 return tevent_req_simple_recv_ntstatus(req);
545 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
546 const char *oldname,
547 const char *newname)
549 TALLOC_CTX *frame = talloc_stackframe();
550 struct tevent_context *ev = NULL;
551 struct tevent_req *req = NULL;
552 NTSTATUS status = NT_STATUS_OK;
554 if (smbXcli_conn_has_async_calls(cli->conn)) {
556 * Can't use sync call while an async call is in flight
558 status = NT_STATUS_INVALID_PARAMETER;
559 goto fail;
562 ev = samba_tevent_context_init(frame);
563 if (ev == NULL) {
564 status = NT_STATUS_NO_MEMORY;
565 goto fail;
568 req = cli_posix_hardlink_send(frame,
570 cli,
571 oldname,
572 newname);
573 if (req == NULL) {
574 status = NT_STATUS_NO_MEMORY;
575 goto fail;
578 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
579 goto fail;
582 status = cli_posix_hardlink_recv(req);
584 fail:
585 TALLOC_FREE(frame);
586 return status;
589 /****************************************************************************
590 Do a POSIX getfacl (UNIX extensions).
591 ****************************************************************************/
593 struct getfacl_state {
594 uint32_t num_data;
595 uint8_t *data;
598 static void cli_posix_getfacl_done(struct tevent_req *subreq);
600 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
601 struct tevent_context *ev,
602 struct cli_state *cli,
603 const char *fname)
605 struct tevent_req *req = NULL, *subreq = NULL;
606 struct getfacl_state *state = NULL;
608 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
609 if (req == NULL) {
610 return NULL;
612 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
613 0, CLI_BUFFER_SIZE);
614 if (tevent_req_nomem(subreq, req)) {
615 return tevent_req_post(req, ev);
617 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
618 return req;
621 static void cli_posix_getfacl_done(struct tevent_req *subreq)
623 struct tevent_req *req = tevent_req_callback_data(
624 subreq, struct tevent_req);
625 struct getfacl_state *state = tevent_req_data(
626 req, struct getfacl_state);
627 NTSTATUS status;
629 status = cli_qpathinfo_recv(subreq, state, &state->data,
630 &state->num_data);
631 TALLOC_FREE(subreq);
632 if (tevent_req_nterror(req, status)) {
633 return;
635 tevent_req_done(req);
638 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
639 TALLOC_CTX *mem_ctx,
640 size_t *prb_size,
641 char **retbuf)
643 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
644 NTSTATUS status;
646 if (tevent_req_is_nterror(req, &status)) {
647 return status;
649 *prb_size = (size_t)state->num_data;
650 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
651 return NT_STATUS_OK;
654 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
655 const char *fname,
656 TALLOC_CTX *mem_ctx,
657 size_t *prb_size,
658 char **retbuf)
660 TALLOC_CTX *frame = talloc_stackframe();
661 struct tevent_context *ev = NULL;
662 struct tevent_req *req = NULL;
663 NTSTATUS status = NT_STATUS_OK;
665 if (smbXcli_conn_has_async_calls(cli->conn)) {
667 * Can't use sync call while an async call is in flight
669 status = NT_STATUS_INVALID_PARAMETER;
670 goto fail;
673 ev = samba_tevent_context_init(frame);
674 if (ev == NULL) {
675 status = NT_STATUS_NO_MEMORY;
676 goto fail;
679 req = cli_posix_getfacl_send(frame,
681 cli,
682 fname);
683 if (req == NULL) {
684 status = NT_STATUS_NO_MEMORY;
685 goto fail;
688 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
689 goto fail;
692 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
694 fail:
695 TALLOC_FREE(frame);
696 return status;
699 /****************************************************************************
700 Stat a file (UNIX extensions).
701 ****************************************************************************/
703 struct stat_state {
704 uint32_t num_data;
705 uint8_t *data;
708 static void cli_posix_stat_done(struct tevent_req *subreq);
710 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
711 struct tevent_context *ev,
712 struct cli_state *cli,
713 const char *fname)
715 struct tevent_req *req = NULL, *subreq = NULL;
716 struct stat_state *state = NULL;
718 req = tevent_req_create(mem_ctx, &state, struct stat_state);
719 if (req == NULL) {
720 return NULL;
722 subreq = cli_qpathinfo_send(state, ev, cli, fname,
723 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
724 if (tevent_req_nomem(subreq, req)) {
725 return tevent_req_post(req, ev);
727 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
728 return req;
731 static void cli_posix_stat_done(struct tevent_req *subreq)
733 struct tevent_req *req = tevent_req_callback_data(
734 subreq, struct tevent_req);
735 struct stat_state *state = tevent_req_data(req, struct stat_state);
736 NTSTATUS status;
738 status = cli_qpathinfo_recv(subreq, state, &state->data,
739 &state->num_data);
740 TALLOC_FREE(subreq);
741 if (tevent_req_nterror(req, status)) {
742 return;
744 tevent_req_done(req);
747 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
748 SMB_STRUCT_STAT *sbuf)
750 struct stat_state *state = tevent_req_data(req, struct stat_state);
751 NTSTATUS status;
753 if (tevent_req_is_nterror(req, &status)) {
754 return status;
757 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
758 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
759 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
760 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
761 #else
762 /* assume 512 byte blocks */
763 sbuf->st_ex_blocks /= 512;
764 #endif
765 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
766 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
767 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
769 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
770 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
771 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
772 #if defined(HAVE_MAKEDEV)
774 uint32_t dev_major = IVAL(state->data,60);
775 uint32_t dev_minor = IVAL(state->data,68);
776 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
778 #endif
779 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
780 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
781 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
783 return NT_STATUS_OK;
786 NTSTATUS cli_posix_stat(struct cli_state *cli,
787 const char *fname,
788 SMB_STRUCT_STAT *sbuf)
790 TALLOC_CTX *frame = talloc_stackframe();
791 struct tevent_context *ev = NULL;
792 struct tevent_req *req = NULL;
793 NTSTATUS status = NT_STATUS_OK;
795 if (smbXcli_conn_has_async_calls(cli->conn)) {
797 * Can't use sync call while an async call is in flight
799 status = NT_STATUS_INVALID_PARAMETER;
800 goto fail;
803 ev = samba_tevent_context_init(frame);
804 if (ev == NULL) {
805 status = NT_STATUS_NO_MEMORY;
806 goto fail;
809 req = cli_posix_stat_send(frame,
811 cli,
812 fname);
813 if (req == NULL) {
814 status = NT_STATUS_NO_MEMORY;
815 goto fail;
818 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
819 goto fail;
822 status = cli_posix_stat_recv(req, sbuf);
824 fail:
825 TALLOC_FREE(frame);
826 return status;
829 /****************************************************************************
830 Chmod or chown a file internal (UNIX extensions).
831 ****************************************************************************/
833 struct cli_posix_chown_chmod_internal_state {
834 uint8_t data[100];
837 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
839 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
840 struct tevent_context *ev,
841 struct cli_state *cli,
842 const char *fname,
843 uint32_t mode,
844 uint32_t uid,
845 uint32_t gid)
847 struct tevent_req *req = NULL, *subreq = NULL;
848 struct cli_posix_chown_chmod_internal_state *state = NULL;
850 req = tevent_req_create(mem_ctx, &state,
851 struct cli_posix_chown_chmod_internal_state);
852 if (req == NULL) {
853 return NULL;
856 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
857 memset(&state->data[40], '\0', 60);
858 SIVAL(state->data,40,uid);
859 SIVAL(state->data,48,gid);
860 SIVAL(state->data,84,mode);
862 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
863 fname, state->data, sizeof(state->data));
864 if (tevent_req_nomem(subreq, req)) {
865 return tevent_req_post(req, ev);
867 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
868 req);
869 return req;
872 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
874 NTSTATUS status = cli_setpathinfo_recv(subreq);
875 tevent_req_simple_finish_ntstatus(subreq, status);
878 /****************************************************************************
879 chmod a file (UNIX extensions).
880 ****************************************************************************/
882 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
883 struct tevent_context *ev,
884 struct cli_state *cli,
885 const char *fname,
886 mode_t mode)
888 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
889 fname,
890 unix_perms_to_wire(mode),
891 SMB_UID_NO_CHANGE,
892 SMB_GID_NO_CHANGE);
895 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
897 return tevent_req_simple_recv_ntstatus(req);
900 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
902 TALLOC_CTX *frame = talloc_stackframe();
903 struct tevent_context *ev = NULL;
904 struct tevent_req *req = NULL;
905 NTSTATUS status = NT_STATUS_OK;
907 if (smbXcli_conn_has_async_calls(cli->conn)) {
909 * Can't use sync call while an async call is in flight
911 status = NT_STATUS_INVALID_PARAMETER;
912 goto fail;
915 ev = samba_tevent_context_init(frame);
916 if (ev == NULL) {
917 status = NT_STATUS_NO_MEMORY;
918 goto fail;
921 req = cli_posix_chmod_send(frame,
923 cli,
924 fname,
925 mode);
926 if (req == NULL) {
927 status = NT_STATUS_NO_MEMORY;
928 goto fail;
931 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
932 goto fail;
935 status = cli_posix_chmod_recv(req);
937 fail:
938 TALLOC_FREE(frame);
939 return status;
942 /****************************************************************************
943 chown a file (UNIX extensions).
944 ****************************************************************************/
946 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
947 struct tevent_context *ev,
948 struct cli_state *cli,
949 const char *fname,
950 uid_t uid,
951 gid_t gid)
953 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
954 fname,
955 SMB_MODE_NO_CHANGE,
956 (uint32_t)uid,
957 (uint32_t)gid);
960 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
962 return tevent_req_simple_recv_ntstatus(req);
965 NTSTATUS cli_posix_chown(struct cli_state *cli,
966 const char *fname,
967 uid_t uid,
968 gid_t gid)
970 TALLOC_CTX *frame = talloc_stackframe();
971 struct tevent_context *ev = NULL;
972 struct tevent_req *req = NULL;
973 NTSTATUS status = NT_STATUS_OK;
975 if (smbXcli_conn_has_async_calls(cli->conn)) {
977 * Can't use sync call while an async call is in flight
979 status = NT_STATUS_INVALID_PARAMETER;
980 goto fail;
983 ev = samba_tevent_context_init(frame);
984 if (ev == NULL) {
985 status = NT_STATUS_NO_MEMORY;
986 goto fail;
989 req = cli_posix_chown_send(frame,
991 cli,
992 fname,
993 uid,
994 gid);
995 if (req == NULL) {
996 status = NT_STATUS_NO_MEMORY;
997 goto fail;
1000 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1001 goto fail;
1004 status = cli_posix_chown_recv(req);
1006 fail:
1007 TALLOC_FREE(frame);
1008 return status;
1011 /****************************************************************************
1012 Rename a file.
1013 ****************************************************************************/
1015 static void cli_rename_done(struct tevent_req *subreq);
1017 struct cli_rename_state {
1018 uint16_t vwv[1];
1021 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1022 struct tevent_context *ev,
1023 struct cli_state *cli,
1024 const char *fname_src,
1025 const char *fname_dst)
1027 struct tevent_req *req = NULL, *subreq = NULL;
1028 struct cli_rename_state *state = NULL;
1029 uint8_t additional_flags = 0;
1030 uint8_t *bytes = NULL;
1032 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1033 if (req == NULL) {
1034 return NULL;
1037 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1039 bytes = talloc_array(state, uint8_t, 1);
1040 if (tevent_req_nomem(bytes, req)) {
1041 return tevent_req_post(req, ev);
1043 bytes[0] = 4;
1044 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1045 strlen(fname_src)+1, NULL);
1046 if (tevent_req_nomem(bytes, req)) {
1047 return tevent_req_post(req, ev);
1050 bytes = talloc_realloc(state, bytes, uint8_t,
1051 talloc_get_size(bytes)+1);
1052 if (tevent_req_nomem(bytes, req)) {
1053 return tevent_req_post(req, ev);
1056 bytes[talloc_get_size(bytes)-1] = 4;
1057 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1058 strlen(fname_dst)+1, NULL);
1059 if (tevent_req_nomem(bytes, req)) {
1060 return tevent_req_post(req, ev);
1063 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1064 1, state->vwv, talloc_get_size(bytes), bytes);
1065 if (tevent_req_nomem(subreq, req)) {
1066 return tevent_req_post(req, ev);
1068 tevent_req_set_callback(subreq, cli_rename_done, req);
1069 return req;
1072 static void cli_rename_done(struct tevent_req *subreq)
1074 struct tevent_req *req = tevent_req_callback_data(
1075 subreq, struct tevent_req);
1076 NTSTATUS status;
1078 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1079 TALLOC_FREE(subreq);
1080 if (tevent_req_nterror(req, status)) {
1081 return;
1083 tevent_req_done(req);
1086 NTSTATUS cli_rename_recv(struct tevent_req *req)
1088 return tevent_req_simple_recv_ntstatus(req);
1091 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1093 TALLOC_CTX *frame = NULL;
1094 struct tevent_context *ev;
1095 struct tevent_req *req;
1096 NTSTATUS status = NT_STATUS_OK;
1098 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1099 return cli_smb2_rename(cli,
1100 fname_src,
1101 fname_dst);
1104 frame = talloc_stackframe();
1106 if (smbXcli_conn_has_async_calls(cli->conn)) {
1108 * Can't use sync call while an async call is in flight
1110 status = NT_STATUS_INVALID_PARAMETER;
1111 goto fail;
1114 ev = samba_tevent_context_init(frame);
1115 if (ev == NULL) {
1116 status = NT_STATUS_NO_MEMORY;
1117 goto fail;
1120 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1121 if (req == NULL) {
1122 status = NT_STATUS_NO_MEMORY;
1123 goto fail;
1126 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1127 goto fail;
1130 status = cli_rename_recv(req);
1132 fail:
1133 TALLOC_FREE(frame);
1134 return status;
1137 /****************************************************************************
1138 NT Rename a file.
1139 ****************************************************************************/
1141 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1143 struct cli_ntrename_internal_state {
1144 uint16_t vwv[4];
1147 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1148 struct tevent_context *ev,
1149 struct cli_state *cli,
1150 const char *fname_src,
1151 const char *fname_dst,
1152 uint16_t rename_flag)
1154 struct tevent_req *req = NULL, *subreq = NULL;
1155 struct cli_ntrename_internal_state *state = NULL;
1156 uint8_t additional_flags = 0;
1157 uint8_t *bytes = NULL;
1159 req = tevent_req_create(mem_ctx, &state,
1160 struct cli_ntrename_internal_state);
1161 if (req == NULL) {
1162 return NULL;
1165 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1166 SSVAL(state->vwv+1, 0, rename_flag);
1168 bytes = talloc_array(state, uint8_t, 1);
1169 if (tevent_req_nomem(bytes, req)) {
1170 return tevent_req_post(req, ev);
1172 bytes[0] = 4;
1173 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1174 strlen(fname_src)+1, NULL);
1175 if (tevent_req_nomem(bytes, req)) {
1176 return tevent_req_post(req, ev);
1179 bytes = talloc_realloc(state, bytes, uint8_t,
1180 talloc_get_size(bytes)+1);
1181 if (tevent_req_nomem(bytes, req)) {
1182 return tevent_req_post(req, ev);
1185 bytes[talloc_get_size(bytes)-1] = 4;
1186 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1187 strlen(fname_dst)+1, NULL);
1188 if (tevent_req_nomem(bytes, req)) {
1189 return tevent_req_post(req, ev);
1192 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1193 4, state->vwv, talloc_get_size(bytes), bytes);
1194 if (tevent_req_nomem(subreq, req)) {
1195 return tevent_req_post(req, ev);
1197 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1198 return req;
1201 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1203 struct tevent_req *req = tevent_req_callback_data(
1204 subreq, struct tevent_req);
1205 NTSTATUS status;
1207 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1208 TALLOC_FREE(subreq);
1209 if (tevent_req_nterror(req, status)) {
1210 return;
1212 tevent_req_done(req);
1215 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1217 return tevent_req_simple_recv_ntstatus(req);
1220 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1221 struct tevent_context *ev,
1222 struct cli_state *cli,
1223 const char *fname_src,
1224 const char *fname_dst)
1226 return cli_ntrename_internal_send(mem_ctx,
1228 cli,
1229 fname_src,
1230 fname_dst,
1231 RENAME_FLAG_RENAME);
1234 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1236 return cli_ntrename_internal_recv(req);
1239 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1241 TALLOC_CTX *frame = talloc_stackframe();
1242 struct tevent_context *ev;
1243 struct tevent_req *req;
1244 NTSTATUS status = NT_STATUS_OK;
1246 if (smbXcli_conn_has_async_calls(cli->conn)) {
1248 * Can't use sync call while an async call is in flight
1250 status = NT_STATUS_INVALID_PARAMETER;
1251 goto fail;
1254 ev = samba_tevent_context_init(frame);
1255 if (ev == NULL) {
1256 status = NT_STATUS_NO_MEMORY;
1257 goto fail;
1260 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1261 if (req == NULL) {
1262 status = NT_STATUS_NO_MEMORY;
1263 goto fail;
1266 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1267 goto fail;
1270 status = cli_ntrename_recv(req);
1272 fail:
1273 TALLOC_FREE(frame);
1274 return status;
1277 /****************************************************************************
1278 NT hardlink a file.
1279 ****************************************************************************/
1281 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1282 struct tevent_context *ev,
1283 struct cli_state *cli,
1284 const char *fname_src,
1285 const char *fname_dst)
1287 return cli_ntrename_internal_send(mem_ctx,
1289 cli,
1290 fname_src,
1291 fname_dst,
1292 RENAME_FLAG_HARD_LINK);
1295 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1297 return cli_ntrename_internal_recv(req);
1300 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1302 TALLOC_CTX *frame = talloc_stackframe();
1303 struct tevent_context *ev;
1304 struct tevent_req *req;
1305 NTSTATUS status = NT_STATUS_OK;
1307 if (smbXcli_conn_has_async_calls(cli->conn)) {
1309 * Can't use sync call while an async call is in flight
1311 status = NT_STATUS_INVALID_PARAMETER;
1312 goto fail;
1315 ev = samba_tevent_context_init(frame);
1316 if (ev == NULL) {
1317 status = NT_STATUS_NO_MEMORY;
1318 goto fail;
1321 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1322 if (req == NULL) {
1323 status = NT_STATUS_NO_MEMORY;
1324 goto fail;
1327 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1328 goto fail;
1331 status = cli_nt_hardlink_recv(req);
1333 fail:
1334 TALLOC_FREE(frame);
1335 return status;
1338 /****************************************************************************
1339 Delete a file.
1340 ****************************************************************************/
1342 static void cli_unlink_done(struct tevent_req *subreq);
1344 struct cli_unlink_state {
1345 uint16_t vwv[1];
1348 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1349 struct tevent_context *ev,
1350 struct cli_state *cli,
1351 const char *fname,
1352 uint16_t mayhave_attrs)
1354 struct tevent_req *req = NULL, *subreq = NULL;
1355 struct cli_unlink_state *state = NULL;
1356 uint8_t additional_flags = 0;
1357 uint8_t *bytes = NULL;
1359 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1360 if (req == NULL) {
1361 return NULL;
1364 SSVAL(state->vwv+0, 0, mayhave_attrs);
1366 bytes = talloc_array(state, uint8_t, 1);
1367 if (tevent_req_nomem(bytes, req)) {
1368 return tevent_req_post(req, ev);
1370 bytes[0] = 4;
1371 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1372 strlen(fname)+1, NULL);
1374 if (tevent_req_nomem(bytes, req)) {
1375 return tevent_req_post(req, ev);
1378 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1379 1, state->vwv, talloc_get_size(bytes), bytes);
1380 if (tevent_req_nomem(subreq, req)) {
1381 return tevent_req_post(req, ev);
1383 tevent_req_set_callback(subreq, cli_unlink_done, req);
1384 return req;
1387 static void cli_unlink_done(struct tevent_req *subreq)
1389 struct tevent_req *req = tevent_req_callback_data(
1390 subreq, struct tevent_req);
1391 NTSTATUS status;
1393 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1394 TALLOC_FREE(subreq);
1395 if (tevent_req_nterror(req, status)) {
1396 return;
1398 tevent_req_done(req);
1401 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1403 return tevent_req_simple_recv_ntstatus(req);
1406 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1408 TALLOC_CTX *frame = NULL;
1409 struct tevent_context *ev;
1410 struct tevent_req *req;
1411 NTSTATUS status = NT_STATUS_OK;
1413 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1414 return cli_smb2_unlink(cli, fname);
1417 frame = talloc_stackframe();
1419 if (smbXcli_conn_has_async_calls(cli->conn)) {
1421 * Can't use sync call while an async call is in flight
1423 status = NT_STATUS_INVALID_PARAMETER;
1424 goto fail;
1427 ev = samba_tevent_context_init(frame);
1428 if (ev == NULL) {
1429 status = NT_STATUS_NO_MEMORY;
1430 goto fail;
1433 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1434 if (req == NULL) {
1435 status = NT_STATUS_NO_MEMORY;
1436 goto fail;
1439 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1440 goto fail;
1443 status = cli_unlink_recv(req);
1445 fail:
1446 TALLOC_FREE(frame);
1447 return status;
1450 /****************************************************************************
1451 Create a directory.
1452 ****************************************************************************/
1454 static void cli_mkdir_done(struct tevent_req *subreq);
1456 struct cli_mkdir_state {
1457 int dummy;
1460 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1461 struct tevent_context *ev,
1462 struct cli_state *cli,
1463 const char *dname)
1465 struct tevent_req *req = NULL, *subreq = NULL;
1466 struct cli_mkdir_state *state = NULL;
1467 uint8_t additional_flags = 0;
1468 uint8_t *bytes = NULL;
1470 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1471 if (req == NULL) {
1472 return NULL;
1475 bytes = talloc_array(state, uint8_t, 1);
1476 if (tevent_req_nomem(bytes, req)) {
1477 return tevent_req_post(req, ev);
1479 bytes[0] = 4;
1480 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1481 strlen(dname)+1, NULL);
1483 if (tevent_req_nomem(bytes, req)) {
1484 return tevent_req_post(req, ev);
1487 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1488 0, NULL, talloc_get_size(bytes), bytes);
1489 if (tevent_req_nomem(subreq, req)) {
1490 return tevent_req_post(req, ev);
1492 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1493 return req;
1496 static void cli_mkdir_done(struct tevent_req *subreq)
1498 struct tevent_req *req = tevent_req_callback_data(
1499 subreq, struct tevent_req);
1500 NTSTATUS status;
1502 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1503 TALLOC_FREE(subreq);
1504 if (tevent_req_nterror(req, status)) {
1505 return;
1507 tevent_req_done(req);
1510 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1512 return tevent_req_simple_recv_ntstatus(req);
1515 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1517 TALLOC_CTX *frame = NULL;
1518 struct tevent_context *ev;
1519 struct tevent_req *req;
1520 NTSTATUS status = NT_STATUS_OK;
1522 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1523 return cli_smb2_mkdir(cli, dname);
1526 frame = talloc_stackframe();
1528 if (smbXcli_conn_has_async_calls(cli->conn)) {
1530 * Can't use sync call while an async call is in flight
1532 status = NT_STATUS_INVALID_PARAMETER;
1533 goto fail;
1536 ev = samba_tevent_context_init(frame);
1537 if (ev == NULL) {
1538 status = NT_STATUS_NO_MEMORY;
1539 goto fail;
1542 req = cli_mkdir_send(frame, ev, cli, dname);
1543 if (req == NULL) {
1544 status = NT_STATUS_NO_MEMORY;
1545 goto fail;
1548 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1549 goto fail;
1552 status = cli_mkdir_recv(req);
1554 fail:
1555 TALLOC_FREE(frame);
1556 return status;
1559 /****************************************************************************
1560 Remove a directory.
1561 ****************************************************************************/
1563 static void cli_rmdir_done(struct tevent_req *subreq);
1565 struct cli_rmdir_state {
1566 int dummy;
1569 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1570 struct tevent_context *ev,
1571 struct cli_state *cli,
1572 const char *dname)
1574 struct tevent_req *req = NULL, *subreq = NULL;
1575 struct cli_rmdir_state *state = NULL;
1576 uint8_t additional_flags = 0;
1577 uint8_t *bytes = NULL;
1579 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1580 if (req == NULL) {
1581 return NULL;
1584 bytes = talloc_array(state, uint8_t, 1);
1585 if (tevent_req_nomem(bytes, req)) {
1586 return tevent_req_post(req, ev);
1588 bytes[0] = 4;
1589 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1590 strlen(dname)+1, NULL);
1592 if (tevent_req_nomem(bytes, req)) {
1593 return tevent_req_post(req, ev);
1596 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1597 0, NULL, talloc_get_size(bytes), bytes);
1598 if (tevent_req_nomem(subreq, req)) {
1599 return tevent_req_post(req, ev);
1601 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1602 return req;
1605 static void cli_rmdir_done(struct tevent_req *subreq)
1607 struct tevent_req *req = tevent_req_callback_data(
1608 subreq, struct tevent_req);
1609 NTSTATUS status;
1611 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1612 TALLOC_FREE(subreq);
1613 if (tevent_req_nterror(req, status)) {
1614 return;
1616 tevent_req_done(req);
1619 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1621 return tevent_req_simple_recv_ntstatus(req);
1624 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1626 TALLOC_CTX *frame = NULL;
1627 struct tevent_context *ev;
1628 struct tevent_req *req;
1629 NTSTATUS status = NT_STATUS_OK;
1631 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1632 return cli_smb2_rmdir(cli, dname);
1635 frame = talloc_stackframe();
1637 if (smbXcli_conn_has_async_calls(cli->conn)) {
1639 * Can't use sync call while an async call is in flight
1641 status = NT_STATUS_INVALID_PARAMETER;
1642 goto fail;
1645 ev = samba_tevent_context_init(frame);
1646 if (ev == NULL) {
1647 status = NT_STATUS_NO_MEMORY;
1648 goto fail;
1651 req = cli_rmdir_send(frame, ev, cli, dname);
1652 if (req == NULL) {
1653 status = NT_STATUS_NO_MEMORY;
1654 goto fail;
1657 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1658 goto fail;
1661 status = cli_rmdir_recv(req);
1663 fail:
1664 TALLOC_FREE(frame);
1665 return status;
1668 /****************************************************************************
1669 Set or clear the delete on close flag.
1670 ****************************************************************************/
1672 struct doc_state {
1673 uint16_t setup;
1674 uint8_t param[6];
1675 uint8_t data[1];
1678 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1680 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1681 NULL, 0, NULL, NULL, 0, NULL);
1682 tevent_req_simple_finish_ntstatus(subreq, status);
1685 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1686 struct tevent_context *ev,
1687 struct cli_state *cli,
1688 uint16_t fnum,
1689 bool flag)
1691 struct tevent_req *req = NULL, *subreq = NULL;
1692 struct doc_state *state = NULL;
1694 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1695 if (req == NULL) {
1696 return NULL;
1699 /* Setup setup word. */
1700 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1702 /* Setup param array. */
1703 SSVAL(state->param,0,fnum);
1704 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1706 /* Setup data array. */
1707 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1709 subreq = cli_trans_send(state, /* mem ctx. */
1710 ev, /* event ctx. */
1711 cli, /* cli_state. */
1712 SMBtrans2, /* cmd. */
1713 NULL, /* pipe name. */
1714 -1, /* fid. */
1715 0, /* function. */
1716 0, /* flags. */
1717 &state->setup, /* setup. */
1718 1, /* num setup uint16_t words. */
1719 0, /* max returned setup. */
1720 state->param, /* param. */
1721 6, /* num param. */
1722 2, /* max returned param. */
1723 state->data, /* data. */
1724 1, /* num data. */
1725 0); /* max returned data. */
1727 if (tevent_req_nomem(subreq, req)) {
1728 return tevent_req_post(req, ev);
1730 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1731 return req;
1734 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1736 return tevent_req_simple_recv_ntstatus(req);
1739 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1741 TALLOC_CTX *frame = talloc_stackframe();
1742 struct tevent_context *ev = NULL;
1743 struct tevent_req *req = NULL;
1744 NTSTATUS status = NT_STATUS_OK;
1746 if (smbXcli_conn_has_async_calls(cli->conn)) {
1748 * Can't use sync call while an async call is in flight
1750 status = NT_STATUS_INVALID_PARAMETER;
1751 goto fail;
1754 ev = samba_tevent_context_init(frame);
1755 if (ev == NULL) {
1756 status = NT_STATUS_NO_MEMORY;
1757 goto fail;
1760 req = cli_nt_delete_on_close_send(frame,
1762 cli,
1763 fnum,
1764 flag);
1765 if (req == NULL) {
1766 status = NT_STATUS_NO_MEMORY;
1767 goto fail;
1770 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1771 goto fail;
1774 status = cli_nt_delete_on_close_recv(req);
1776 fail:
1777 TALLOC_FREE(frame);
1778 return status;
1781 struct cli_ntcreate1_state {
1782 uint16_t vwv[24];
1783 uint16_t fnum;
1784 struct smb_create_returns cr;
1785 struct tevent_req *subreq;
1788 static void cli_ntcreate1_done(struct tevent_req *subreq);
1789 static bool cli_ntcreate1_cancel(struct tevent_req *req);
1791 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
1792 struct tevent_context *ev,
1793 struct cli_state *cli,
1794 const char *fname,
1795 uint32_t CreatFlags,
1796 uint32_t DesiredAccess,
1797 uint32_t FileAttributes,
1798 uint32_t ShareAccess,
1799 uint32_t CreateDisposition,
1800 uint32_t CreateOptions,
1801 uint8_t SecurityFlags)
1803 struct tevent_req *req, *subreq;
1804 struct cli_ntcreate1_state *state;
1805 uint16_t *vwv;
1806 uint8_t *bytes;
1807 size_t converted_len;
1809 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1810 if (req == NULL) {
1811 return NULL;
1814 vwv = state->vwv;
1816 SCVAL(vwv+0, 0, 0xFF);
1817 SCVAL(vwv+0, 1, 0);
1818 SSVAL(vwv+1, 0, 0);
1819 SCVAL(vwv+2, 0, 0);
1821 if (cli->use_oplocks) {
1822 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1824 SIVAL(vwv+3, 1, CreatFlags);
1825 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1826 SIVAL(vwv+7, 1, DesiredAccess);
1827 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1828 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1829 SIVAL(vwv+13, 1, FileAttributes);
1830 SIVAL(vwv+15, 1, ShareAccess);
1831 SIVAL(vwv+17, 1, CreateDisposition);
1832 SIVAL(vwv+19, 1, CreateOptions |
1833 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1834 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1835 SCVAL(vwv+23, 1, SecurityFlags);
1837 bytes = talloc_array(state, uint8_t, 0);
1838 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1839 fname, strlen(fname)+1,
1840 &converted_len);
1842 /* sigh. this copes with broken netapp filer behaviour */
1843 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1845 if (tevent_req_nomem(bytes, req)) {
1846 return tevent_req_post(req, ev);
1849 SSVAL(vwv+2, 1, converted_len);
1851 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1852 talloc_get_size(bytes), bytes);
1853 if (tevent_req_nomem(subreq, req)) {
1854 return tevent_req_post(req, ev);
1856 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
1858 state->subreq = subreq;
1859 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
1861 return req;
1864 static void cli_ntcreate1_done(struct tevent_req *subreq)
1866 struct tevent_req *req = tevent_req_callback_data(
1867 subreq, struct tevent_req);
1868 struct cli_ntcreate1_state *state = tevent_req_data(
1869 req, struct cli_ntcreate1_state);
1870 uint8_t wct;
1871 uint16_t *vwv;
1872 uint32_t num_bytes;
1873 uint8_t *bytes;
1874 NTSTATUS status;
1876 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
1877 &num_bytes, &bytes);
1878 TALLOC_FREE(subreq);
1879 if (tevent_req_nterror(req, status)) {
1880 return;
1882 state->cr.oplock_level = CVAL(vwv+2, 0);
1883 state->fnum = SVAL(vwv+2, 1);
1884 state->cr.create_action = IVAL(vwv+3, 1);
1885 state->cr.creation_time = BVAL(vwv+5, 1);
1886 state->cr.last_access_time = BVAL(vwv+9, 1);
1887 state->cr.last_write_time = BVAL(vwv+13, 1);
1888 state->cr.change_time = BVAL(vwv+17, 1);
1889 state->cr.file_attributes = IVAL(vwv+21, 1);
1890 state->cr.allocation_size = BVAL(vwv+23, 1);
1891 state->cr.end_of_file = BVAL(vwv+27, 1);
1893 tevent_req_done(req);
1896 static bool cli_ntcreate1_cancel(struct tevent_req *req)
1898 struct cli_ntcreate1_state *state = tevent_req_data(
1899 req, struct cli_ntcreate1_state);
1900 return tevent_req_cancel(state->subreq);
1903 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
1904 uint16_t *pfnum,
1905 struct smb_create_returns *cr)
1907 struct cli_ntcreate1_state *state = tevent_req_data(
1908 req, struct cli_ntcreate1_state);
1909 NTSTATUS status;
1911 if (tevent_req_is_nterror(req, &status)) {
1912 return status;
1914 *pfnum = state->fnum;
1915 if (cr != NULL) {
1916 *cr = state->cr;
1918 return NT_STATUS_OK;
1921 struct cli_ntcreate_state {
1922 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
1923 struct smb_create_returns *cr);
1924 struct smb_create_returns cr;
1925 uint16_t fnum;
1926 struct tevent_req *subreq;
1929 static void cli_ntcreate_done(struct tevent_req *subreq);
1930 static bool cli_ntcreate_cancel(struct tevent_req *req);
1932 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1933 struct tevent_context *ev,
1934 struct cli_state *cli,
1935 const char *fname,
1936 uint32_t create_flags,
1937 uint32_t desired_access,
1938 uint32_t file_attributes,
1939 uint32_t share_access,
1940 uint32_t create_disposition,
1941 uint32_t create_options,
1942 uint8_t security_flags)
1944 struct tevent_req *req, *subreq;
1945 struct cli_ntcreate_state *state;
1947 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1948 if (req == NULL) {
1949 return NULL;
1952 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1953 state->recv = cli_smb2_create_fnum_recv;
1955 if (cli->use_oplocks) {
1956 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
1959 subreq = cli_smb2_create_fnum_send(
1960 state, ev, cli, fname, create_flags, desired_access,
1961 file_attributes, share_access, create_disposition,
1962 create_options);
1963 } else {
1964 state->recv = cli_ntcreate1_recv;
1965 subreq = cli_ntcreate1_send(
1966 state, ev, cli, fname, create_flags, desired_access,
1967 file_attributes, share_access, create_disposition,
1968 create_options, security_flags);
1970 if (tevent_req_nomem(subreq, req)) {
1971 return tevent_req_post(req, ev);
1973 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1975 state->subreq = subreq;
1976 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
1978 return req;
1981 static void cli_ntcreate_done(struct tevent_req *subreq)
1983 struct tevent_req *req = tevent_req_callback_data(
1984 subreq, struct tevent_req);
1985 struct cli_ntcreate_state *state = tevent_req_data(
1986 req, struct cli_ntcreate_state);
1987 NTSTATUS status;
1989 status = state->recv(subreq, &state->fnum, &state->cr);
1990 TALLOC_FREE(subreq);
1991 if (tevent_req_nterror(req, status)) {
1992 return;
1994 tevent_req_done(req);
1997 static bool cli_ntcreate_cancel(struct tevent_req *req)
1999 struct cli_ntcreate_state *state = tevent_req_data(
2000 req, struct cli_ntcreate_state);
2001 return tevent_req_cancel(state->subreq);
2004 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2005 struct smb_create_returns *cr)
2007 struct cli_ntcreate_state *state = tevent_req_data(
2008 req, struct cli_ntcreate_state);
2009 NTSTATUS status;
2011 if (tevent_req_is_nterror(req, &status)) {
2012 return status;
2014 if (fnum != NULL) {
2015 *fnum = state->fnum;
2017 if (cr != NULL) {
2018 *cr = state->cr;
2020 return NT_STATUS_OK;
2023 NTSTATUS cli_ntcreate(struct cli_state *cli,
2024 const char *fname,
2025 uint32_t CreatFlags,
2026 uint32_t DesiredAccess,
2027 uint32_t FileAttributes,
2028 uint32_t ShareAccess,
2029 uint32_t CreateDisposition,
2030 uint32_t CreateOptions,
2031 uint8_t SecurityFlags,
2032 uint16_t *pfid,
2033 struct smb_create_returns *cr)
2035 TALLOC_CTX *frame = talloc_stackframe();
2036 struct tevent_context *ev;
2037 struct tevent_req *req;
2038 NTSTATUS status = NT_STATUS_NO_MEMORY;
2040 if (smbXcli_conn_has_async_calls(cli->conn)) {
2042 * Can't use sync call while an async call is in flight
2044 status = NT_STATUS_INVALID_PARAMETER;
2045 goto fail;
2048 ev = samba_tevent_context_init(frame);
2049 if (ev == NULL) {
2050 goto fail;
2053 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2054 DesiredAccess, FileAttributes, ShareAccess,
2055 CreateDisposition, CreateOptions,
2056 SecurityFlags);
2057 if (req == NULL) {
2058 goto fail;
2061 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2062 goto fail;
2065 status = cli_ntcreate_recv(req, pfid, cr);
2066 fail:
2067 TALLOC_FREE(frame);
2068 return status;
2071 struct cli_nttrans_create_state {
2072 uint16_t fnum;
2073 struct smb_create_returns cr;
2076 static void cli_nttrans_create_done(struct tevent_req *subreq);
2078 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2079 struct tevent_context *ev,
2080 struct cli_state *cli,
2081 const char *fname,
2082 uint32_t CreatFlags,
2083 uint32_t DesiredAccess,
2084 uint32_t FileAttributes,
2085 uint32_t ShareAccess,
2086 uint32_t CreateDisposition,
2087 uint32_t CreateOptions,
2088 uint8_t SecurityFlags,
2089 struct security_descriptor *secdesc,
2090 struct ea_struct *eas,
2091 int num_eas)
2093 struct tevent_req *req, *subreq;
2094 struct cli_nttrans_create_state *state;
2095 uint8_t *param;
2096 uint8_t *secdesc_buf;
2097 size_t secdesc_len;
2098 NTSTATUS status;
2099 size_t converted_len;
2101 req = tevent_req_create(mem_ctx,
2102 &state, struct cli_nttrans_create_state);
2103 if (req == NULL) {
2104 return NULL;
2107 if (secdesc != NULL) {
2108 status = marshall_sec_desc(talloc_tos(), secdesc,
2109 &secdesc_buf, &secdesc_len);
2110 if (tevent_req_nterror(req, status)) {
2111 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2112 nt_errstr(status)));
2113 return tevent_req_post(req, ev);
2115 } else {
2116 secdesc_buf = NULL;
2117 secdesc_len = 0;
2120 if (num_eas != 0) {
2122 * TODO ;-)
2124 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2125 return tevent_req_post(req, ev);
2128 param = talloc_array(state, uint8_t, 53);
2129 if (tevent_req_nomem(param, req)) {
2130 return tevent_req_post(req, ev);
2133 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2134 fname, strlen(fname),
2135 &converted_len);
2136 if (tevent_req_nomem(param, req)) {
2137 return tevent_req_post(req, ev);
2140 SIVAL(param, 0, CreatFlags);
2141 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2142 SIVAL(param, 8, DesiredAccess);
2143 SIVAL(param, 12, 0x0); /* AllocationSize */
2144 SIVAL(param, 16, 0x0); /* AllocationSize */
2145 SIVAL(param, 20, FileAttributes);
2146 SIVAL(param, 24, ShareAccess);
2147 SIVAL(param, 28, CreateDisposition);
2148 SIVAL(param, 32, CreateOptions |
2149 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2150 SIVAL(param, 36, secdesc_len);
2151 SIVAL(param, 40, 0); /* EA length*/
2152 SIVAL(param, 44, converted_len);
2153 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2154 SCVAL(param, 52, SecurityFlags);
2156 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2157 NULL, -1, /* name, fid */
2158 NT_TRANSACT_CREATE, 0,
2159 NULL, 0, 0, /* setup */
2160 param, talloc_get_size(param), 128, /* param */
2161 secdesc_buf, secdesc_len, 0); /* data */
2162 if (tevent_req_nomem(subreq, req)) {
2163 return tevent_req_post(req, ev);
2165 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2166 return req;
2169 static void cli_nttrans_create_done(struct tevent_req *subreq)
2171 struct tevent_req *req = tevent_req_callback_data(
2172 subreq, struct tevent_req);
2173 struct cli_nttrans_create_state *state = tevent_req_data(
2174 req, struct cli_nttrans_create_state);
2175 uint8_t *param;
2176 uint32_t num_param;
2177 NTSTATUS status;
2179 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2180 NULL, 0, NULL, /* rsetup */
2181 &param, 69, &num_param,
2182 NULL, 0, NULL);
2183 if (tevent_req_nterror(req, status)) {
2184 return;
2186 state->cr.oplock_level = CVAL(param, 0);
2187 state->fnum = SVAL(param, 2);
2188 state->cr.create_action = IVAL(param, 4);
2189 state->cr.creation_time = BVAL(param, 12);
2190 state->cr.last_access_time = BVAL(param, 20);
2191 state->cr.last_write_time = BVAL(param, 28);
2192 state->cr.change_time = BVAL(param, 36);
2193 state->cr.file_attributes = IVAL(param, 44);
2194 state->cr.allocation_size = BVAL(param, 48);
2195 state->cr.end_of_file = BVAL(param, 56);
2197 TALLOC_FREE(param);
2198 tevent_req_done(req);
2201 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2202 uint16_t *fnum,
2203 struct smb_create_returns *cr)
2205 struct cli_nttrans_create_state *state = tevent_req_data(
2206 req, struct cli_nttrans_create_state);
2207 NTSTATUS status;
2209 if (tevent_req_is_nterror(req, &status)) {
2210 return status;
2212 *fnum = state->fnum;
2213 if (cr != NULL) {
2214 *cr = state->cr;
2216 return NT_STATUS_OK;
2219 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2220 const char *fname,
2221 uint32_t CreatFlags,
2222 uint32_t DesiredAccess,
2223 uint32_t FileAttributes,
2224 uint32_t ShareAccess,
2225 uint32_t CreateDisposition,
2226 uint32_t CreateOptions,
2227 uint8_t SecurityFlags,
2228 struct security_descriptor *secdesc,
2229 struct ea_struct *eas,
2230 int num_eas,
2231 uint16_t *pfid,
2232 struct smb_create_returns *cr)
2234 TALLOC_CTX *frame = talloc_stackframe();
2235 struct tevent_context *ev;
2236 struct tevent_req *req;
2237 NTSTATUS status = NT_STATUS_NO_MEMORY;
2239 if (smbXcli_conn_has_async_calls(cli->conn)) {
2241 * Can't use sync call while an async call is in flight
2243 status = NT_STATUS_INVALID_PARAMETER;
2244 goto fail;
2246 ev = samba_tevent_context_init(frame);
2247 if (ev == NULL) {
2248 goto fail;
2250 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2251 DesiredAccess, FileAttributes,
2252 ShareAccess, CreateDisposition,
2253 CreateOptions, SecurityFlags,
2254 secdesc, eas, num_eas);
2255 if (req == NULL) {
2256 goto fail;
2258 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2259 goto fail;
2261 status = cli_nttrans_create_recv(req, pfid, cr);
2262 fail:
2263 TALLOC_FREE(frame);
2264 return status;
2267 /****************************************************************************
2268 Open a file
2269 WARNING: if you open with O_WRONLY then getattrE won't work!
2270 ****************************************************************************/
2272 struct cli_openx_state {
2273 const char *fname;
2274 uint16_t vwv[15];
2275 uint16_t fnum;
2276 struct iovec bytes;
2279 static void cli_openx_done(struct tevent_req *subreq);
2281 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2282 struct tevent_context *ev,
2283 struct cli_state *cli, const char *fname,
2284 int flags, int share_mode,
2285 struct tevent_req **psmbreq)
2287 struct tevent_req *req, *subreq;
2288 struct cli_openx_state *state;
2289 unsigned openfn;
2290 unsigned accessmode;
2291 uint8_t additional_flags;
2292 uint8_t *bytes;
2294 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2295 if (req == NULL) {
2296 return NULL;
2299 openfn = 0;
2300 if (flags & O_CREAT) {
2301 openfn |= (1<<4);
2303 if (!(flags & O_EXCL)) {
2304 if (flags & O_TRUNC)
2305 openfn |= (1<<1);
2306 else
2307 openfn |= (1<<0);
2310 accessmode = (share_mode<<4);
2312 if ((flags & O_ACCMODE) == O_RDWR) {
2313 accessmode |= 2;
2314 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2315 accessmode |= 1;
2318 #if defined(O_SYNC)
2319 if ((flags & O_SYNC) == O_SYNC) {
2320 accessmode |= (1<<14);
2322 #endif /* O_SYNC */
2324 if (share_mode == DENY_FCB) {
2325 accessmode = 0xFF;
2328 SCVAL(state->vwv + 0, 0, 0xFF);
2329 SCVAL(state->vwv + 0, 1, 0);
2330 SSVAL(state->vwv + 1, 0, 0);
2331 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2332 SSVAL(state->vwv + 3, 0, accessmode);
2333 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2334 SSVAL(state->vwv + 5, 0, 0);
2335 SIVAL(state->vwv + 6, 0, 0);
2336 SSVAL(state->vwv + 8, 0, openfn);
2337 SIVAL(state->vwv + 9, 0, 0);
2338 SIVAL(state->vwv + 11, 0, 0);
2339 SIVAL(state->vwv + 13, 0, 0);
2341 additional_flags = 0;
2343 if (cli->use_oplocks) {
2344 /* if using oplocks then ask for a batch oplock via
2345 core and extended methods */
2346 additional_flags =
2347 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2348 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2351 bytes = talloc_array(state, uint8_t, 0);
2352 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2353 strlen(fname)+1, NULL);
2355 if (tevent_req_nomem(bytes, req)) {
2356 return tevent_req_post(req, ev);
2359 state->bytes.iov_base = (void *)bytes;
2360 state->bytes.iov_len = talloc_get_size(bytes);
2362 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2363 15, state->vwv, 1, &state->bytes);
2364 if (subreq == NULL) {
2365 TALLOC_FREE(req);
2366 return NULL;
2368 tevent_req_set_callback(subreq, cli_openx_done, req);
2369 *psmbreq = subreq;
2370 return req;
2373 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2374 struct cli_state *cli, const char *fname,
2375 int flags, int share_mode)
2377 struct tevent_req *req, *subreq;
2378 NTSTATUS status;
2380 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2381 &subreq);
2382 if (req == NULL) {
2383 return NULL;
2386 status = smb1cli_req_chain_submit(&subreq, 1);
2387 if (tevent_req_nterror(req, status)) {
2388 return tevent_req_post(req, ev);
2390 return req;
2393 static void cli_openx_done(struct tevent_req *subreq)
2395 struct tevent_req *req = tevent_req_callback_data(
2396 subreq, struct tevent_req);
2397 struct cli_openx_state *state = tevent_req_data(
2398 req, struct cli_openx_state);
2399 uint8_t wct;
2400 uint16_t *vwv;
2401 NTSTATUS status;
2403 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2404 NULL);
2405 TALLOC_FREE(subreq);
2406 if (tevent_req_nterror(req, status)) {
2407 return;
2409 state->fnum = SVAL(vwv+2, 0);
2410 tevent_req_done(req);
2413 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2415 struct cli_openx_state *state = tevent_req_data(
2416 req, struct cli_openx_state);
2417 NTSTATUS status;
2419 if (tevent_req_is_nterror(req, &status)) {
2420 return status;
2422 *pfnum = state->fnum;
2423 return NT_STATUS_OK;
2426 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2427 int share_mode, uint16_t *pfnum)
2429 TALLOC_CTX *frame = talloc_stackframe();
2430 struct tevent_context *ev;
2431 struct tevent_req *req;
2432 NTSTATUS status = NT_STATUS_NO_MEMORY;
2434 if (smbXcli_conn_has_async_calls(cli->conn)) {
2436 * Can't use sync call while an async call is in flight
2438 status = NT_STATUS_INVALID_PARAMETER;
2439 goto fail;
2442 ev = samba_tevent_context_init(frame);
2443 if (ev == NULL) {
2444 goto fail;
2447 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2448 if (req == NULL) {
2449 goto fail;
2452 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2453 goto fail;
2456 status = cli_openx_recv(req, pfnum);
2457 fail:
2458 TALLOC_FREE(frame);
2459 return status;
2461 /****************************************************************************
2462 Synchronous wrapper function that does an NtCreateX open by preference
2463 and falls back to openX if this fails.
2464 ****************************************************************************/
2466 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2467 int share_mode_in, uint16_t *pfnum)
2469 NTSTATUS status;
2470 unsigned int openfn = 0;
2471 unsigned int dos_deny = 0;
2472 uint32_t access_mask, share_mode, create_disposition, create_options;
2473 struct smb_create_returns cr;
2475 /* Do the initial mapping into OpenX parameters. */
2476 if (flags & O_CREAT) {
2477 openfn |= (1<<4);
2479 if (!(flags & O_EXCL)) {
2480 if (flags & O_TRUNC)
2481 openfn |= (1<<1);
2482 else
2483 openfn |= (1<<0);
2486 dos_deny = (share_mode_in<<4);
2488 if ((flags & O_ACCMODE) == O_RDWR) {
2489 dos_deny |= 2;
2490 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2491 dos_deny |= 1;
2494 #if defined(O_SYNC)
2495 if ((flags & O_SYNC) == O_SYNC) {
2496 dos_deny |= (1<<14);
2498 #endif /* O_SYNC */
2500 if (share_mode_in == DENY_FCB) {
2501 dos_deny = 0xFF;
2504 #if 0
2505 /* Hmmm. This is what I think the above code
2506 should look like if it's using the constants
2507 we #define. JRA. */
2509 if (flags & O_CREAT) {
2510 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2512 if (!(flags & O_EXCL)) {
2513 if (flags & O_TRUNC)
2514 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2515 else
2516 openfn |= OPENX_FILE_EXISTS_OPEN;
2519 dos_deny = SET_DENY_MODE(share_mode_in);
2521 if ((flags & O_ACCMODE) == O_RDWR) {
2522 dos_deny |= DOS_OPEN_RDWR;
2523 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2524 dos_deny |= DOS_OPEN_WRONLY;
2527 #if defined(O_SYNC)
2528 if ((flags & O_SYNC) == O_SYNC) {
2529 dos_deny |= FILE_SYNC_OPENMODE;
2531 #endif /* O_SYNC */
2533 if (share_mode_in == DENY_FCB) {
2534 dos_deny = 0xFF;
2536 #endif
2538 if (!map_open_params_to_ntcreate(fname, dos_deny,
2539 openfn, &access_mask,
2540 &share_mode, &create_disposition,
2541 &create_options, NULL)) {
2542 goto try_openx;
2545 status = cli_ntcreate(cli,
2546 fname,
2548 access_mask,
2550 share_mode,
2551 create_disposition,
2552 create_options,
2554 pfnum,
2555 &cr);
2557 /* Try and cope will all varients of "we don't do this call"
2558 and fall back to openX. */
2560 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2561 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2562 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2563 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2564 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2565 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2566 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2567 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2568 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2569 goto try_openx;
2572 if (NT_STATUS_IS_OK(status) &&
2573 (create_options & FILE_NON_DIRECTORY_FILE) &&
2574 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2577 * Some (broken) servers return a valid handle
2578 * for directories even if FILE_NON_DIRECTORY_FILE
2579 * is set. Just close the handle and set the
2580 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2582 status = cli_close(cli, *pfnum);
2583 if (!NT_STATUS_IS_OK(status)) {
2584 return status;
2586 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2587 /* Set this so libsmbclient can retrieve it. */
2588 cli->raw_status = status;
2591 return status;
2593 try_openx:
2595 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2598 /****************************************************************************
2599 Close a file.
2600 ****************************************************************************/
2602 struct cli_close_state {
2603 uint16_t vwv[3];
2606 static void cli_close_done(struct tevent_req *subreq);
2608 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2609 struct tevent_context *ev,
2610 struct cli_state *cli,
2611 uint16_t fnum,
2612 struct tevent_req **psubreq)
2614 struct tevent_req *req, *subreq;
2615 struct cli_close_state *state;
2617 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2618 if (req == NULL) {
2619 return NULL;
2622 SSVAL(state->vwv+0, 0, fnum);
2623 SIVALS(state->vwv+1, 0, -1);
2625 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2626 0, NULL);
2627 if (subreq == NULL) {
2628 TALLOC_FREE(req);
2629 return NULL;
2631 tevent_req_set_callback(subreq, cli_close_done, req);
2632 *psubreq = subreq;
2633 return req;
2636 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2637 struct tevent_context *ev,
2638 struct cli_state *cli,
2639 uint16_t fnum)
2641 struct tevent_req *req, *subreq;
2642 NTSTATUS status;
2644 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2645 if (req == NULL) {
2646 return NULL;
2649 status = smb1cli_req_chain_submit(&subreq, 1);
2650 if (tevent_req_nterror(req, status)) {
2651 return tevent_req_post(req, ev);
2653 return req;
2656 static void cli_close_done(struct tevent_req *subreq)
2658 struct tevent_req *req = tevent_req_callback_data(
2659 subreq, struct tevent_req);
2660 NTSTATUS status;
2662 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2663 TALLOC_FREE(subreq);
2664 if (tevent_req_nterror(req, status)) {
2665 return;
2667 tevent_req_done(req);
2670 NTSTATUS cli_close_recv(struct tevent_req *req)
2672 return tevent_req_simple_recv_ntstatus(req);
2675 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2677 TALLOC_CTX *frame = NULL;
2678 struct tevent_context *ev;
2679 struct tevent_req *req;
2680 NTSTATUS status = NT_STATUS_OK;
2682 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2683 return cli_smb2_close_fnum(cli, fnum);
2686 frame = talloc_stackframe();
2688 if (smbXcli_conn_has_async_calls(cli->conn)) {
2690 * Can't use sync call while an async call is in flight
2692 status = NT_STATUS_INVALID_PARAMETER;
2693 goto fail;
2696 ev = samba_tevent_context_init(frame);
2697 if (ev == NULL) {
2698 status = NT_STATUS_NO_MEMORY;
2699 goto fail;
2702 req = cli_close_send(frame, ev, cli, fnum);
2703 if (req == NULL) {
2704 status = NT_STATUS_NO_MEMORY;
2705 goto fail;
2708 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2709 goto fail;
2712 status = cli_close_recv(req);
2713 fail:
2714 TALLOC_FREE(frame);
2715 return status;
2718 /****************************************************************************
2719 Truncate a file to a specified size
2720 ****************************************************************************/
2722 struct ftrunc_state {
2723 uint16_t setup;
2724 uint8_t param[6];
2725 uint8_t data[8];
2728 static void cli_ftruncate_done(struct tevent_req *subreq)
2730 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2731 NULL, 0, NULL, NULL, 0, NULL);
2732 tevent_req_simple_finish_ntstatus(subreq, status);
2735 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2736 struct tevent_context *ev,
2737 struct cli_state *cli,
2738 uint16_t fnum,
2739 uint64_t size)
2741 struct tevent_req *req = NULL, *subreq = NULL;
2742 struct ftrunc_state *state = NULL;
2744 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2745 if (req == NULL) {
2746 return NULL;
2749 /* Setup setup word. */
2750 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2752 /* Setup param array. */
2753 SSVAL(state->param,0,fnum);
2754 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2755 SSVAL(state->param,4,0);
2757 /* Setup data array. */
2758 SBVAL(state->data, 0, size);
2760 subreq = cli_trans_send(state, /* mem ctx. */
2761 ev, /* event ctx. */
2762 cli, /* cli_state. */
2763 SMBtrans2, /* cmd. */
2764 NULL, /* pipe name. */
2765 -1, /* fid. */
2766 0, /* function. */
2767 0, /* flags. */
2768 &state->setup, /* setup. */
2769 1, /* num setup uint16_t words. */
2770 0, /* max returned setup. */
2771 state->param, /* param. */
2772 6, /* num param. */
2773 2, /* max returned param. */
2774 state->data, /* data. */
2775 8, /* num data. */
2776 0); /* max returned data. */
2778 if (tevent_req_nomem(subreq, req)) {
2779 return tevent_req_post(req, ev);
2781 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2782 return req;
2785 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2787 return tevent_req_simple_recv_ntstatus(req);
2790 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2792 TALLOC_CTX *frame = talloc_stackframe();
2793 struct tevent_context *ev = NULL;
2794 struct tevent_req *req = NULL;
2795 NTSTATUS status = NT_STATUS_OK;
2797 if (smbXcli_conn_has_async_calls(cli->conn)) {
2799 * Can't use sync call while an async call is in flight
2801 status = NT_STATUS_INVALID_PARAMETER;
2802 goto fail;
2805 ev = samba_tevent_context_init(frame);
2806 if (ev == NULL) {
2807 status = NT_STATUS_NO_MEMORY;
2808 goto fail;
2811 req = cli_ftruncate_send(frame,
2813 cli,
2814 fnum,
2815 size);
2816 if (req == NULL) {
2817 status = NT_STATUS_NO_MEMORY;
2818 goto fail;
2821 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2822 goto fail;
2825 status = cli_ftruncate_recv(req);
2827 fail:
2828 TALLOC_FREE(frame);
2829 return status;
2832 /****************************************************************************
2833 send a lock with a specified locktype
2834 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2835 ****************************************************************************/
2837 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2838 uint32_t offset, uint32_t len,
2839 int timeout, unsigned char locktype)
2841 uint16_t vwv[8];
2842 uint8_t bytes[10];
2843 NTSTATUS status;
2844 unsigned int set_timeout = 0;
2845 unsigned int saved_timeout = 0;
2847 SCVAL(vwv + 0, 0, 0xff);
2848 SCVAL(vwv + 0, 1, 0);
2849 SSVAL(vwv + 1, 0, 0);
2850 SSVAL(vwv + 2, 0, fnum);
2851 SCVAL(vwv + 3, 0, locktype);
2852 SCVAL(vwv + 3, 1, 0);
2853 SIVALS(vwv + 4, 0, timeout);
2854 SSVAL(vwv + 6, 0, 0);
2855 SSVAL(vwv + 7, 0, 1);
2857 SSVAL(bytes, 0, cli_getpid(cli));
2858 SIVAL(bytes, 2, offset);
2859 SIVAL(bytes, 6, len);
2861 if (timeout != 0) {
2862 if (timeout == -1) {
2863 set_timeout = 0x7FFFFFFF;
2864 } else {
2865 set_timeout = timeout + 2*1000;
2867 saved_timeout = cli_set_timeout(cli, set_timeout);
2870 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2871 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2873 if (saved_timeout != 0) {
2874 cli_set_timeout(cli, saved_timeout);
2877 return status;
2880 /****************************************************************************
2881 Lock a file.
2882 note that timeout is in units of 2 milliseconds
2883 ****************************************************************************/
2885 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2886 uint32_t offset, uint32_t len, int timeout,
2887 enum brl_type lock_type)
2889 NTSTATUS status;
2891 status = cli_locktype(cli, fnum, offset, len, timeout,
2892 (lock_type == READ_LOCK? 1 : 0));
2893 return status;
2896 /****************************************************************************
2897 Unlock a file.
2898 ****************************************************************************/
2900 struct cli_unlock_state {
2901 uint16_t vwv[8];
2902 uint8_t data[10];
2905 static void cli_unlock_done(struct tevent_req *subreq);
2907 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2908 struct tevent_context *ev,
2909 struct cli_state *cli,
2910 uint16_t fnum,
2911 uint64_t offset,
2912 uint64_t len)
2915 struct tevent_req *req = NULL, *subreq = NULL;
2916 struct cli_unlock_state *state = NULL;
2917 uint8_t additional_flags = 0;
2919 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2920 if (req == NULL) {
2921 return NULL;
2924 SCVAL(state->vwv+0, 0, 0xFF);
2925 SSVAL(state->vwv+2, 0, fnum);
2926 SCVAL(state->vwv+3, 0, 0);
2927 SIVALS(state->vwv+4, 0, 0);
2928 SSVAL(state->vwv+6, 0, 1);
2929 SSVAL(state->vwv+7, 0, 0);
2931 SSVAL(state->data, 0, cli_getpid(cli));
2932 SIVAL(state->data, 2, offset);
2933 SIVAL(state->data, 6, len);
2935 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2936 8, state->vwv, 10, state->data);
2937 if (tevent_req_nomem(subreq, req)) {
2938 return tevent_req_post(req, ev);
2940 tevent_req_set_callback(subreq, cli_unlock_done, req);
2941 return req;
2944 static void cli_unlock_done(struct tevent_req *subreq)
2946 struct tevent_req *req = tevent_req_callback_data(
2947 subreq, struct tevent_req);
2948 NTSTATUS status;
2950 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2951 TALLOC_FREE(subreq);
2952 if (tevent_req_nterror(req, status)) {
2953 return;
2955 tevent_req_done(req);
2958 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2960 return tevent_req_simple_recv_ntstatus(req);
2963 NTSTATUS cli_unlock(struct cli_state *cli,
2964 uint16_t fnum,
2965 uint32_t offset,
2966 uint32_t len)
2968 TALLOC_CTX *frame = talloc_stackframe();
2969 struct tevent_context *ev;
2970 struct tevent_req *req;
2971 NTSTATUS status = NT_STATUS_OK;
2973 if (smbXcli_conn_has_async_calls(cli->conn)) {
2975 * Can't use sync call while an async call is in flight
2977 status = NT_STATUS_INVALID_PARAMETER;
2978 goto fail;
2981 ev = samba_tevent_context_init(frame);
2982 if (ev == NULL) {
2983 status = NT_STATUS_NO_MEMORY;
2984 goto fail;
2987 req = cli_unlock_send(frame, ev, cli,
2988 fnum, offset, len);
2989 if (req == NULL) {
2990 status = NT_STATUS_NO_MEMORY;
2991 goto fail;
2994 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2995 goto fail;
2998 status = cli_unlock_recv(req);
3000 fail:
3001 TALLOC_FREE(frame);
3002 return status;
3005 /****************************************************************************
3006 Lock a file with 64 bit offsets.
3007 ****************************************************************************/
3009 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3010 uint64_t offset, uint64_t len, int timeout,
3011 enum brl_type lock_type)
3013 uint16_t vwv[8];
3014 uint8_t bytes[20];
3015 unsigned int set_timeout = 0;
3016 unsigned int saved_timeout = 0;
3017 int ltype;
3018 NTSTATUS status;
3020 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3021 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3024 ltype = (lock_type == READ_LOCK? 1 : 0);
3025 ltype |= LOCKING_ANDX_LARGE_FILES;
3027 SCVAL(vwv + 0, 0, 0xff);
3028 SCVAL(vwv + 0, 1, 0);
3029 SSVAL(vwv + 1, 0, 0);
3030 SSVAL(vwv + 2, 0, fnum);
3031 SCVAL(vwv + 3, 0, ltype);
3032 SCVAL(vwv + 3, 1, 0);
3033 SIVALS(vwv + 4, 0, timeout);
3034 SSVAL(vwv + 6, 0, 0);
3035 SSVAL(vwv + 7, 0, 1);
3037 SIVAL(bytes, 0, cli_getpid(cli));
3038 SOFF_T_R(bytes, 4, offset);
3039 SOFF_T_R(bytes, 12, len);
3041 if (timeout != 0) {
3042 if (timeout == -1) {
3043 set_timeout = 0x7FFFFFFF;
3044 } else {
3045 set_timeout = timeout + 2*1000;
3047 saved_timeout = cli_set_timeout(cli, set_timeout);
3050 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3051 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3053 if (saved_timeout != 0) {
3054 cli_set_timeout(cli, saved_timeout);
3057 return status;
3060 /****************************************************************************
3061 Unlock a file with 64 bit offsets.
3062 ****************************************************************************/
3064 struct cli_unlock64_state {
3065 uint16_t vwv[8];
3066 uint8_t data[20];
3069 static void cli_unlock64_done(struct tevent_req *subreq);
3071 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3072 struct tevent_context *ev,
3073 struct cli_state *cli,
3074 uint16_t fnum,
3075 uint64_t offset,
3076 uint64_t len)
3079 struct tevent_req *req = NULL, *subreq = NULL;
3080 struct cli_unlock64_state *state = NULL;
3081 uint8_t additional_flags = 0;
3083 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3084 if (req == NULL) {
3085 return NULL;
3088 SCVAL(state->vwv+0, 0, 0xff);
3089 SSVAL(state->vwv+2, 0, fnum);
3090 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3091 SIVALS(state->vwv+4, 0, 0);
3092 SSVAL(state->vwv+6, 0, 1);
3093 SSVAL(state->vwv+7, 0, 0);
3095 SIVAL(state->data, 0, cli_getpid(cli));
3096 SOFF_T_R(state->data, 4, offset);
3097 SOFF_T_R(state->data, 12, len);
3099 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
3100 8, state->vwv, 20, state->data);
3101 if (tevent_req_nomem(subreq, req)) {
3102 return tevent_req_post(req, ev);
3104 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3105 return req;
3108 static void cli_unlock64_done(struct tevent_req *subreq)
3110 struct tevent_req *req = tevent_req_callback_data(
3111 subreq, struct tevent_req);
3112 NTSTATUS status;
3114 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3115 TALLOC_FREE(subreq);
3116 if (tevent_req_nterror(req, status)) {
3117 return;
3119 tevent_req_done(req);
3122 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3124 return tevent_req_simple_recv_ntstatus(req);
3127 NTSTATUS cli_unlock64(struct cli_state *cli,
3128 uint16_t fnum,
3129 uint64_t offset,
3130 uint64_t len)
3132 TALLOC_CTX *frame = talloc_stackframe();
3133 struct tevent_context *ev;
3134 struct tevent_req *req;
3135 NTSTATUS status = NT_STATUS_OK;
3137 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3138 return cli_unlock(cli, fnum, offset, len);
3141 if (smbXcli_conn_has_async_calls(cli->conn)) {
3143 * Can't use sync call while an async call is in flight
3145 status = NT_STATUS_INVALID_PARAMETER;
3146 goto fail;
3149 ev = samba_tevent_context_init(frame);
3150 if (ev == NULL) {
3151 status = NT_STATUS_NO_MEMORY;
3152 goto fail;
3155 req = cli_unlock64_send(frame, ev, cli,
3156 fnum, offset, len);
3157 if (req == NULL) {
3158 status = NT_STATUS_NO_MEMORY;
3159 goto fail;
3162 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3163 goto fail;
3166 status = cli_unlock64_recv(req);
3168 fail:
3169 TALLOC_FREE(frame);
3170 return status;
3173 /****************************************************************************
3174 Get/unlock a POSIX lock on a file - internal function.
3175 ****************************************************************************/
3177 struct posix_lock_state {
3178 uint16_t setup;
3179 uint8_t param[4];
3180 uint8_t data[POSIX_LOCK_DATA_SIZE];
3183 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3185 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3186 NULL, 0, NULL, NULL, 0, NULL);
3187 tevent_req_simple_finish_ntstatus(subreq, status);
3190 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3191 struct tevent_context *ev,
3192 struct cli_state *cli,
3193 uint16_t fnum,
3194 uint64_t offset,
3195 uint64_t len,
3196 bool wait_lock,
3197 enum brl_type lock_type)
3199 struct tevent_req *req = NULL, *subreq = NULL;
3200 struct posix_lock_state *state = NULL;
3202 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3203 if (req == NULL) {
3204 return NULL;
3207 /* Setup setup word. */
3208 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3210 /* Setup param array. */
3211 SSVAL(&state->param, 0, fnum);
3212 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3214 /* Setup data array. */
3215 switch (lock_type) {
3216 case READ_LOCK:
3217 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3218 POSIX_LOCK_TYPE_READ);
3219 break;
3220 case WRITE_LOCK:
3221 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3222 POSIX_LOCK_TYPE_WRITE);
3223 break;
3224 case UNLOCK_LOCK:
3225 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3226 POSIX_LOCK_TYPE_UNLOCK);
3227 break;
3228 default:
3229 return NULL;
3232 if (wait_lock) {
3233 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3234 POSIX_LOCK_FLAG_WAIT);
3235 } else {
3236 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3237 POSIX_LOCK_FLAG_NOWAIT);
3240 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3241 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3242 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3244 subreq = cli_trans_send(state, /* mem ctx. */
3245 ev, /* event ctx. */
3246 cli, /* cli_state. */
3247 SMBtrans2, /* cmd. */
3248 NULL, /* pipe name. */
3249 -1, /* fid. */
3250 0, /* function. */
3251 0, /* flags. */
3252 &state->setup, /* setup. */
3253 1, /* num setup uint16_t words. */
3254 0, /* max returned setup. */
3255 state->param, /* param. */
3256 4, /* num param. */
3257 2, /* max returned param. */
3258 state->data, /* data. */
3259 POSIX_LOCK_DATA_SIZE, /* num data. */
3260 0); /* max returned data. */
3262 if (tevent_req_nomem(subreq, req)) {
3263 return tevent_req_post(req, ev);
3265 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3266 return req;
3269 /****************************************************************************
3270 POSIX Lock a file.
3271 ****************************************************************************/
3273 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3274 struct tevent_context *ev,
3275 struct cli_state *cli,
3276 uint16_t fnum,
3277 uint64_t offset,
3278 uint64_t len,
3279 bool wait_lock,
3280 enum brl_type lock_type)
3282 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3283 wait_lock, lock_type);
3286 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3288 return tevent_req_simple_recv_ntstatus(req);
3291 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3292 uint64_t offset, uint64_t len,
3293 bool wait_lock, enum brl_type lock_type)
3295 TALLOC_CTX *frame = talloc_stackframe();
3296 struct tevent_context *ev = NULL;
3297 struct tevent_req *req = NULL;
3298 NTSTATUS status = NT_STATUS_OK;
3300 if (smbXcli_conn_has_async_calls(cli->conn)) {
3302 * Can't use sync call while an async call is in flight
3304 status = NT_STATUS_INVALID_PARAMETER;
3305 goto fail;
3308 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3309 status = NT_STATUS_INVALID_PARAMETER;
3310 goto fail;
3313 ev = samba_tevent_context_init(frame);
3314 if (ev == NULL) {
3315 status = NT_STATUS_NO_MEMORY;
3316 goto fail;
3319 req = cli_posix_lock_send(frame,
3321 cli,
3322 fnum,
3323 offset,
3324 len,
3325 wait_lock,
3326 lock_type);
3327 if (req == NULL) {
3328 status = NT_STATUS_NO_MEMORY;
3329 goto fail;
3332 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3333 goto fail;
3336 status = cli_posix_lock_recv(req);
3338 fail:
3339 TALLOC_FREE(frame);
3340 return status;
3343 /****************************************************************************
3344 POSIX Unlock a file.
3345 ****************************************************************************/
3347 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3348 struct tevent_context *ev,
3349 struct cli_state *cli,
3350 uint16_t fnum,
3351 uint64_t offset,
3352 uint64_t len)
3354 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3355 false, UNLOCK_LOCK);
3358 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3360 return tevent_req_simple_recv_ntstatus(req);
3363 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3365 TALLOC_CTX *frame = talloc_stackframe();
3366 struct tevent_context *ev = NULL;
3367 struct tevent_req *req = NULL;
3368 NTSTATUS status = NT_STATUS_OK;
3370 if (smbXcli_conn_has_async_calls(cli->conn)) {
3372 * Can't use sync call while an async call is in flight
3374 status = NT_STATUS_INVALID_PARAMETER;
3375 goto fail;
3378 ev = samba_tevent_context_init(frame);
3379 if (ev == NULL) {
3380 status = NT_STATUS_NO_MEMORY;
3381 goto fail;
3384 req = cli_posix_unlock_send(frame,
3386 cli,
3387 fnum,
3388 offset,
3389 len);
3390 if (req == NULL) {
3391 status = NT_STATUS_NO_MEMORY;
3392 goto fail;
3395 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3396 goto fail;
3399 status = cli_posix_unlock_recv(req);
3401 fail:
3402 TALLOC_FREE(frame);
3403 return status;
3406 /****************************************************************************
3407 Do a SMBgetattrE call.
3408 ****************************************************************************/
3410 static void cli_getattrE_done(struct tevent_req *subreq);
3412 struct cli_getattrE_state {
3413 uint16_t vwv[1];
3414 int zone_offset;
3415 uint16_t attr;
3416 off_t size;
3417 time_t change_time;
3418 time_t access_time;
3419 time_t write_time;
3422 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3423 struct tevent_context *ev,
3424 struct cli_state *cli,
3425 uint16_t fnum)
3427 struct tevent_req *req = NULL, *subreq = NULL;
3428 struct cli_getattrE_state *state = NULL;
3429 uint8_t additional_flags = 0;
3431 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3432 if (req == NULL) {
3433 return NULL;
3436 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3437 SSVAL(state->vwv+0,0,fnum);
3439 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3440 1, state->vwv, 0, NULL);
3441 if (tevent_req_nomem(subreq, req)) {
3442 return tevent_req_post(req, ev);
3444 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3445 return req;
3448 static void cli_getattrE_done(struct tevent_req *subreq)
3450 struct tevent_req *req = tevent_req_callback_data(
3451 subreq, struct tevent_req);
3452 struct cli_getattrE_state *state = tevent_req_data(
3453 req, struct cli_getattrE_state);
3454 uint8_t wct;
3455 uint16_t *vwv = NULL;
3456 NTSTATUS status;
3458 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3459 NULL, NULL);
3460 TALLOC_FREE(subreq);
3461 if (tevent_req_nterror(req, status)) {
3462 return;
3465 state->size = (off_t)IVAL(vwv+6,0);
3466 state->attr = SVAL(vwv+10,0);
3467 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3468 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3469 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3471 tevent_req_done(req);
3474 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3475 uint16_t *attr,
3476 off_t *size,
3477 time_t *change_time,
3478 time_t *access_time,
3479 time_t *write_time)
3481 struct cli_getattrE_state *state = tevent_req_data(
3482 req, struct cli_getattrE_state);
3483 NTSTATUS status;
3485 if (tevent_req_is_nterror(req, &status)) {
3486 return status;
3488 if (attr) {
3489 *attr = state->attr;
3491 if (size) {
3492 *size = state->size;
3494 if (change_time) {
3495 *change_time = state->change_time;
3497 if (access_time) {
3498 *access_time = state->access_time;
3500 if (write_time) {
3501 *write_time = state->write_time;
3503 return NT_STATUS_OK;
3506 NTSTATUS cli_getattrE(struct cli_state *cli,
3507 uint16_t fnum,
3508 uint16_t *attr,
3509 off_t *size,
3510 time_t *change_time,
3511 time_t *access_time,
3512 time_t *write_time)
3514 TALLOC_CTX *frame = NULL;
3515 struct tevent_context *ev = NULL;
3516 struct tevent_req *req = NULL;
3517 NTSTATUS status = NT_STATUS_OK;
3519 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3520 return cli_smb2_getattrE(cli,
3521 fnum,
3522 attr,
3523 size,
3524 change_time,
3525 access_time,
3526 write_time);
3529 frame = talloc_stackframe();
3531 if (smbXcli_conn_has_async_calls(cli->conn)) {
3533 * Can't use sync call while an async call is in flight
3535 status = NT_STATUS_INVALID_PARAMETER;
3536 goto fail;
3539 ev = samba_tevent_context_init(frame);
3540 if (ev == NULL) {
3541 status = NT_STATUS_NO_MEMORY;
3542 goto fail;
3545 req = cli_getattrE_send(frame, ev, cli, fnum);
3546 if (req == NULL) {
3547 status = NT_STATUS_NO_MEMORY;
3548 goto fail;
3551 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3552 goto fail;
3555 status = cli_getattrE_recv(req,
3556 attr,
3557 size,
3558 change_time,
3559 access_time,
3560 write_time);
3562 fail:
3563 TALLOC_FREE(frame);
3564 return status;
3567 /****************************************************************************
3568 Do a SMBgetatr call
3569 ****************************************************************************/
3571 static void cli_getatr_done(struct tevent_req *subreq);
3573 struct cli_getatr_state {
3574 int zone_offset;
3575 uint16_t attr;
3576 off_t size;
3577 time_t write_time;
3580 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3581 struct tevent_context *ev,
3582 struct cli_state *cli,
3583 const char *fname)
3585 struct tevent_req *req = NULL, *subreq = NULL;
3586 struct cli_getatr_state *state = NULL;
3587 uint8_t additional_flags = 0;
3588 uint8_t *bytes = NULL;
3590 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3591 if (req == NULL) {
3592 return NULL;
3595 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3597 bytes = talloc_array(state, uint8_t, 1);
3598 if (tevent_req_nomem(bytes, req)) {
3599 return tevent_req_post(req, ev);
3601 bytes[0] = 4;
3602 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3603 strlen(fname)+1, NULL);
3605 if (tevent_req_nomem(bytes, req)) {
3606 return tevent_req_post(req, ev);
3609 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3610 0, NULL, talloc_get_size(bytes), bytes);
3611 if (tevent_req_nomem(subreq, req)) {
3612 return tevent_req_post(req, ev);
3614 tevent_req_set_callback(subreq, cli_getatr_done, req);
3615 return req;
3618 static void cli_getatr_done(struct tevent_req *subreq)
3620 struct tevent_req *req = tevent_req_callback_data(
3621 subreq, struct tevent_req);
3622 struct cli_getatr_state *state = tevent_req_data(
3623 req, struct cli_getatr_state);
3624 uint8_t wct;
3625 uint16_t *vwv = NULL;
3626 NTSTATUS status;
3628 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3629 NULL);
3630 TALLOC_FREE(subreq);
3631 if (tevent_req_nterror(req, status)) {
3632 return;
3635 state->attr = SVAL(vwv+0,0);
3636 state->size = (off_t)IVAL(vwv+3,0);
3637 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3639 tevent_req_done(req);
3642 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3643 uint16_t *attr,
3644 off_t *size,
3645 time_t *write_time)
3647 struct cli_getatr_state *state = tevent_req_data(
3648 req, struct cli_getatr_state);
3649 NTSTATUS status;
3651 if (tevent_req_is_nterror(req, &status)) {
3652 return status;
3654 if (attr) {
3655 *attr = state->attr;
3657 if (size) {
3658 *size = state->size;
3660 if (write_time) {
3661 *write_time = state->write_time;
3663 return NT_STATUS_OK;
3666 NTSTATUS cli_getatr(struct cli_state *cli,
3667 const char *fname,
3668 uint16_t *attr,
3669 off_t *size,
3670 time_t *write_time)
3672 TALLOC_CTX *frame = NULL;
3673 struct tevent_context *ev = NULL;
3674 struct tevent_req *req = NULL;
3675 NTSTATUS status = NT_STATUS_OK;
3677 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3678 return cli_smb2_getatr(cli,
3679 fname,
3680 attr,
3681 size,
3682 write_time);
3685 frame = talloc_stackframe();
3687 if (smbXcli_conn_has_async_calls(cli->conn)) {
3689 * Can't use sync call while an async call is in flight
3691 status = NT_STATUS_INVALID_PARAMETER;
3692 goto fail;
3695 ev = samba_tevent_context_init(frame);
3696 if (ev == NULL) {
3697 status = NT_STATUS_NO_MEMORY;
3698 goto fail;
3701 req = cli_getatr_send(frame, ev, cli, fname);
3702 if (req == NULL) {
3703 status = NT_STATUS_NO_MEMORY;
3704 goto fail;
3707 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3708 goto fail;
3711 status = cli_getatr_recv(req,
3712 attr,
3713 size,
3714 write_time);
3716 fail:
3717 TALLOC_FREE(frame);
3718 return status;
3721 /****************************************************************************
3722 Do a SMBsetattrE call.
3723 ****************************************************************************/
3725 static void cli_setattrE_done(struct tevent_req *subreq);
3727 struct cli_setattrE_state {
3728 uint16_t vwv[7];
3731 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3732 struct tevent_context *ev,
3733 struct cli_state *cli,
3734 uint16_t fnum,
3735 time_t change_time,
3736 time_t access_time,
3737 time_t write_time)
3739 struct tevent_req *req = NULL, *subreq = NULL;
3740 struct cli_setattrE_state *state = NULL;
3741 uint8_t additional_flags = 0;
3743 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3744 if (req == NULL) {
3745 return NULL;
3748 SSVAL(state->vwv+0, 0, fnum);
3749 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3750 smb1cli_conn_server_time_zone(cli->conn));
3751 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3752 smb1cli_conn_server_time_zone(cli->conn));
3753 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3754 smb1cli_conn_server_time_zone(cli->conn));
3756 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3757 7, state->vwv, 0, NULL);
3758 if (tevent_req_nomem(subreq, req)) {
3759 return tevent_req_post(req, ev);
3761 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3762 return req;
3765 static void cli_setattrE_done(struct tevent_req *subreq)
3767 struct tevent_req *req = tevent_req_callback_data(
3768 subreq, struct tevent_req);
3769 NTSTATUS status;
3771 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3772 TALLOC_FREE(subreq);
3773 if (tevent_req_nterror(req, status)) {
3774 return;
3776 tevent_req_done(req);
3779 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3781 return tevent_req_simple_recv_ntstatus(req);
3784 NTSTATUS cli_setattrE(struct cli_state *cli,
3785 uint16_t fnum,
3786 time_t change_time,
3787 time_t access_time,
3788 time_t write_time)
3790 TALLOC_CTX *frame = NULL;
3791 struct tevent_context *ev = NULL;
3792 struct tevent_req *req = NULL;
3793 NTSTATUS status = NT_STATUS_OK;
3795 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3796 return cli_smb2_setattrE(cli,
3797 fnum,
3798 change_time,
3799 access_time,
3800 write_time);
3803 frame = talloc_stackframe();
3805 if (smbXcli_conn_has_async_calls(cli->conn)) {
3807 * Can't use sync call while an async call is in flight
3809 status = NT_STATUS_INVALID_PARAMETER;
3810 goto fail;
3813 ev = samba_tevent_context_init(frame);
3814 if (ev == NULL) {
3815 status = NT_STATUS_NO_MEMORY;
3816 goto fail;
3819 req = cli_setattrE_send(frame, ev,
3820 cli,
3821 fnum,
3822 change_time,
3823 access_time,
3824 write_time);
3826 if (req == NULL) {
3827 status = NT_STATUS_NO_MEMORY;
3828 goto fail;
3831 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3832 goto fail;
3835 status = cli_setattrE_recv(req);
3837 fail:
3838 TALLOC_FREE(frame);
3839 return status;
3842 /****************************************************************************
3843 Do a SMBsetatr call.
3844 ****************************************************************************/
3846 static void cli_setatr_done(struct tevent_req *subreq);
3848 struct cli_setatr_state {
3849 uint16_t vwv[8];
3852 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3853 struct tevent_context *ev,
3854 struct cli_state *cli,
3855 const char *fname,
3856 uint16_t attr,
3857 time_t mtime)
3859 struct tevent_req *req = NULL, *subreq = NULL;
3860 struct cli_setatr_state *state = NULL;
3861 uint8_t additional_flags = 0;
3862 uint8_t *bytes = NULL;
3864 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3865 if (req == NULL) {
3866 return NULL;
3869 SSVAL(state->vwv+0, 0, attr);
3870 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
3872 bytes = talloc_array(state, uint8_t, 1);
3873 if (tevent_req_nomem(bytes, req)) {
3874 return tevent_req_post(req, ev);
3876 bytes[0] = 4;
3877 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3878 strlen(fname)+1, NULL);
3879 if (tevent_req_nomem(bytes, req)) {
3880 return tevent_req_post(req, ev);
3882 bytes = talloc_realloc(state, bytes, uint8_t,
3883 talloc_get_size(bytes)+1);
3884 if (tevent_req_nomem(bytes, req)) {
3885 return tevent_req_post(req, ev);
3888 bytes[talloc_get_size(bytes)-1] = 4;
3889 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
3890 1, NULL);
3891 if (tevent_req_nomem(bytes, req)) {
3892 return tevent_req_post(req, ev);
3895 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3896 8, state->vwv, talloc_get_size(bytes), bytes);
3897 if (tevent_req_nomem(subreq, req)) {
3898 return tevent_req_post(req, ev);
3900 tevent_req_set_callback(subreq, cli_setatr_done, req);
3901 return req;
3904 static void cli_setatr_done(struct tevent_req *subreq)
3906 struct tevent_req *req = tevent_req_callback_data(
3907 subreq, struct tevent_req);
3908 NTSTATUS status;
3910 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3911 TALLOC_FREE(subreq);
3912 if (tevent_req_nterror(req, status)) {
3913 return;
3915 tevent_req_done(req);
3918 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3920 return tevent_req_simple_recv_ntstatus(req);
3923 NTSTATUS cli_setatr(struct cli_state *cli,
3924 const char *fname,
3925 uint16_t attr,
3926 time_t mtime)
3928 TALLOC_CTX *frame = NULL;
3929 struct tevent_context *ev = NULL;
3930 struct tevent_req *req = NULL;
3931 NTSTATUS status = NT_STATUS_OK;
3933 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3934 return cli_smb2_setatr(cli,
3935 fname,
3936 attr,
3937 mtime);
3940 frame = talloc_stackframe();
3942 if (smbXcli_conn_has_async_calls(cli->conn)) {
3944 * Can't use sync call while an async call is in flight
3946 status = NT_STATUS_INVALID_PARAMETER;
3947 goto fail;
3950 ev = samba_tevent_context_init(frame);
3951 if (ev == NULL) {
3952 status = NT_STATUS_NO_MEMORY;
3953 goto fail;
3956 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3957 if (req == NULL) {
3958 status = NT_STATUS_NO_MEMORY;
3959 goto fail;
3962 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3963 goto fail;
3966 status = cli_setatr_recv(req);
3968 fail:
3969 TALLOC_FREE(frame);
3970 return status;
3973 /****************************************************************************
3974 Check for existance of a dir.
3975 ****************************************************************************/
3977 static void cli_chkpath_done(struct tevent_req *subreq);
3979 struct cli_chkpath_state {
3980 int dummy;
3983 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3984 struct tevent_context *ev,
3985 struct cli_state *cli,
3986 const char *fname)
3988 struct tevent_req *req = NULL, *subreq = NULL;
3989 struct cli_chkpath_state *state = NULL;
3990 uint8_t additional_flags = 0;
3991 uint8_t *bytes = NULL;
3993 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3994 if (req == NULL) {
3995 return NULL;
3998 bytes = talloc_array(state, uint8_t, 1);
3999 if (tevent_req_nomem(bytes, req)) {
4000 return tevent_req_post(req, ev);
4002 bytes[0] = 4;
4003 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4004 strlen(fname)+1, NULL);
4006 if (tevent_req_nomem(bytes, req)) {
4007 return tevent_req_post(req, ev);
4010 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
4011 0, NULL, talloc_get_size(bytes), bytes);
4012 if (tevent_req_nomem(subreq, req)) {
4013 return tevent_req_post(req, ev);
4015 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4016 return req;
4019 static void cli_chkpath_done(struct tevent_req *subreq)
4021 struct tevent_req *req = tevent_req_callback_data(
4022 subreq, struct tevent_req);
4023 NTSTATUS status;
4025 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4026 TALLOC_FREE(subreq);
4027 if (tevent_req_nterror(req, status)) {
4028 return;
4030 tevent_req_done(req);
4033 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4035 return tevent_req_simple_recv_ntstatus(req);
4038 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4040 TALLOC_CTX *frame = talloc_stackframe();
4041 struct tevent_context *ev = NULL;
4042 struct tevent_req *req = NULL;
4043 char *path2 = NULL;
4044 NTSTATUS status = NT_STATUS_OK;
4046 if (smbXcli_conn_has_async_calls(cli->conn)) {
4048 * Can't use sync call while an async call is in flight
4050 status = NT_STATUS_INVALID_PARAMETER;
4051 goto fail;
4054 path2 = talloc_strdup(frame, path);
4055 if (!path2) {
4056 status = NT_STATUS_NO_MEMORY;
4057 goto fail;
4059 trim_char(path2,'\0','\\');
4060 if (!*path2) {
4061 path2 = talloc_strdup(frame, "\\");
4062 if (!path2) {
4063 status = NT_STATUS_NO_MEMORY;
4064 goto fail;
4068 ev = samba_tevent_context_init(frame);
4069 if (ev == NULL) {
4070 status = NT_STATUS_NO_MEMORY;
4071 goto fail;
4074 req = cli_chkpath_send(frame, ev, cli, path2);
4075 if (req == NULL) {
4076 status = NT_STATUS_NO_MEMORY;
4077 goto fail;
4080 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4081 goto fail;
4084 status = cli_chkpath_recv(req);
4086 fail:
4087 TALLOC_FREE(frame);
4088 return status;
4091 /****************************************************************************
4092 Query disk space.
4093 ****************************************************************************/
4095 static void cli_dskattr_done(struct tevent_req *subreq);
4097 struct cli_dskattr_state {
4098 int bsize;
4099 int total;
4100 int avail;
4103 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4104 struct tevent_context *ev,
4105 struct cli_state *cli)
4107 struct tevent_req *req = NULL, *subreq = NULL;
4108 struct cli_dskattr_state *state = NULL;
4109 uint8_t additional_flags = 0;
4111 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4112 if (req == NULL) {
4113 return NULL;
4116 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
4117 0, NULL, 0, NULL);
4118 if (tevent_req_nomem(subreq, req)) {
4119 return tevent_req_post(req, ev);
4121 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4122 return req;
4125 static void cli_dskattr_done(struct tevent_req *subreq)
4127 struct tevent_req *req = tevent_req_callback_data(
4128 subreq, struct tevent_req);
4129 struct cli_dskattr_state *state = tevent_req_data(
4130 req, struct cli_dskattr_state);
4131 uint8_t wct;
4132 uint16_t *vwv = NULL;
4133 NTSTATUS status;
4135 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4136 NULL);
4137 TALLOC_FREE(subreq);
4138 if (tevent_req_nterror(req, status)) {
4139 return;
4141 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4142 state->total = SVAL(vwv+0, 0);
4143 state->avail = SVAL(vwv+3, 0);
4144 tevent_req_done(req);
4147 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4149 struct cli_dskattr_state *state = tevent_req_data(
4150 req, struct cli_dskattr_state);
4151 NTSTATUS status;
4153 if (tevent_req_is_nterror(req, &status)) {
4154 return status;
4156 *bsize = state->bsize;
4157 *total = state->total;
4158 *avail = state->avail;
4159 return NT_STATUS_OK;
4162 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4164 TALLOC_CTX *frame = NULL;
4165 struct tevent_context *ev = NULL;
4166 struct tevent_req *req = NULL;
4167 NTSTATUS status = NT_STATUS_OK;
4169 frame = talloc_stackframe();
4171 if (smbXcli_conn_has_async_calls(cli->conn)) {
4173 * Can't use sync call while an async call is in flight
4175 status = NT_STATUS_INVALID_PARAMETER;
4176 goto fail;
4179 ev = samba_tevent_context_init(frame);
4180 if (ev == NULL) {
4181 status = NT_STATUS_NO_MEMORY;
4182 goto fail;
4185 req = cli_dskattr_send(frame, ev, cli);
4186 if (req == NULL) {
4187 status = NT_STATUS_NO_MEMORY;
4188 goto fail;
4191 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
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_ntstatus(req, ev, &status)) {
4415 goto fail;
4418 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4420 fail:
4421 TALLOC_FREE(frame);
4422 return status;
4426 send a raw ioctl - used by the torture code
4428 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4430 uint16_t vwv[3];
4431 NTSTATUS status;
4433 SSVAL(vwv+0, 0, fnum);
4434 SSVAL(vwv+1, 0, code>>16);
4435 SSVAL(vwv+2, 0, (code&0xFFFF));
4437 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4438 NULL, 0, NULL, NULL, NULL, NULL);
4439 if (!NT_STATUS_IS_OK(status)) {
4440 return status;
4442 *blob = data_blob_null;
4443 return NT_STATUS_OK;
4446 /*********************************************************
4447 Set an extended attribute utility fn.
4448 *********************************************************/
4450 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4451 uint8_t *param, unsigned int param_len,
4452 const char *ea_name,
4453 const char *ea_val, size_t ea_len)
4455 uint16_t setup[1];
4456 unsigned int data_len = 0;
4457 uint8_t *data = NULL;
4458 char *p;
4459 size_t ea_namelen = strlen(ea_name);
4460 NTSTATUS status;
4462 SSVAL(setup, 0, setup_val);
4464 if (ea_namelen == 0 && ea_len == 0) {
4465 data_len = 4;
4466 data = talloc_array(talloc_tos(),
4467 uint8_t,
4468 data_len);
4469 if (!data) {
4470 return NT_STATUS_NO_MEMORY;
4472 p = (char *)data;
4473 SIVAL(p,0,data_len);
4474 } else {
4475 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4476 data = talloc_array(talloc_tos(),
4477 uint8_t,
4478 data_len);
4479 if (!data) {
4480 return NT_STATUS_NO_MEMORY;
4482 p = (char *)data;
4483 SIVAL(p,0,data_len);
4484 p += 4;
4485 SCVAL(p, 0, 0); /* EA flags. */
4486 SCVAL(p, 1, ea_namelen);
4487 SSVAL(p, 2, ea_len);
4488 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4489 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4492 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4493 setup, 1, 0,
4494 param, param_len, 2,
4495 data, data_len, CLI_BUFFER_SIZE,
4496 NULL,
4497 NULL, 0, NULL, /* rsetup */
4498 NULL, 0, NULL, /* rparam */
4499 NULL, 0, NULL); /* rdata */
4500 talloc_free(data);
4501 return status;
4504 /*********************************************************
4505 Set an extended attribute on a pathname.
4506 *********************************************************/
4508 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4509 const char *ea_name, const char *ea_val,
4510 size_t ea_len)
4512 unsigned int param_len = 0;
4513 uint8_t *param;
4514 NTSTATUS status;
4515 TALLOC_CTX *frame = NULL;
4517 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4518 return cli_smb2_set_ea_path(cli,
4519 path,
4520 ea_name,
4521 ea_val,
4522 ea_len);
4525 frame = talloc_stackframe();
4527 param = talloc_array(frame, uint8_t, 6);
4528 if (!param) {
4529 status = NT_STATUS_NO_MEMORY;
4530 goto fail;
4532 SSVAL(param,0,SMB_INFO_SET_EA);
4533 SSVAL(param,2,0);
4534 SSVAL(param,4,0);
4536 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4537 path, strlen(path)+1,
4538 NULL);
4539 param_len = talloc_get_size(param);
4541 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4542 ea_name, ea_val, ea_len);
4544 fail:
4546 TALLOC_FREE(frame);
4547 return status;
4550 /*********************************************************
4551 Set an extended attribute on an fnum.
4552 *********************************************************/
4554 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4555 const char *ea_name, const char *ea_val,
4556 size_t ea_len)
4558 uint8_t param[6];
4560 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4561 return cli_smb2_set_ea_fnum(cli,
4562 fnum,
4563 ea_name,
4564 ea_val,
4565 ea_len);
4568 memset(param, 0, 6);
4569 SSVAL(param,0,fnum);
4570 SSVAL(param,2,SMB_INFO_SET_EA);
4572 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4573 ea_name, ea_val, ea_len);
4576 /*********************************************************
4577 Get an extended attribute list utility fn.
4578 *********************************************************/
4580 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4581 size_t rdata_len,
4582 size_t *pnum_eas, struct ea_struct **pea_list)
4584 struct ea_struct *ea_list = NULL;
4585 size_t num_eas;
4586 size_t ea_size;
4587 const uint8_t *p;
4589 if (rdata_len < 4) {
4590 return false;
4593 ea_size = (size_t)IVAL(rdata,0);
4594 if (ea_size > rdata_len) {
4595 return false;
4598 if (ea_size == 0) {
4599 /* No EA's present. */
4600 *pnum_eas = 0;
4601 *pea_list = NULL;
4602 return true;
4605 p = rdata + 4;
4606 ea_size -= 4;
4608 /* Validate the EA list and count it. */
4609 for (num_eas = 0; ea_size >= 4; num_eas++) {
4610 unsigned int ea_namelen = CVAL(p,1);
4611 unsigned int ea_valuelen = SVAL(p,2);
4612 if (ea_namelen == 0) {
4613 return false;
4615 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4616 return false;
4618 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4619 p += 4 + ea_namelen + 1 + ea_valuelen;
4622 if (num_eas == 0) {
4623 *pnum_eas = 0;
4624 *pea_list = NULL;
4625 return true;
4628 *pnum_eas = num_eas;
4629 if (!pea_list) {
4630 /* Caller only wants number of EA's. */
4631 return true;
4634 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4635 if (!ea_list) {
4636 return false;
4639 ea_size = (size_t)IVAL(rdata,0);
4640 p = rdata + 4;
4642 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4643 struct ea_struct *ea = &ea_list[num_eas];
4644 fstring unix_ea_name;
4645 unsigned int ea_namelen = CVAL(p,1);
4646 unsigned int ea_valuelen = SVAL(p,2);
4648 ea->flags = CVAL(p,0);
4649 unix_ea_name[0] = '\0';
4650 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4651 ea->name = talloc_strdup(ea_list, unix_ea_name);
4652 if (!ea->name) {
4653 goto fail;
4655 /* Ensure the value is null terminated (in case it's a string). */
4656 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4657 if (!ea->value.data) {
4658 goto fail;
4660 if (ea_valuelen) {
4661 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4663 ea->value.data[ea_valuelen] = 0;
4664 ea->value.length--;
4665 p += 4 + ea_namelen + 1 + ea_valuelen;
4668 *pea_list = ea_list;
4669 return true;
4671 fail:
4672 TALLOC_FREE(ea_list);
4673 return false;
4676 /*********************************************************
4677 Get an extended attribute list from a pathname.
4678 *********************************************************/
4680 struct cli_get_ea_list_path_state {
4681 uint32_t num_data;
4682 uint8_t *data;
4685 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4687 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4688 struct tevent_context *ev,
4689 struct cli_state *cli,
4690 const char *fname)
4692 struct tevent_req *req, *subreq;
4693 struct cli_get_ea_list_path_state *state;
4695 req = tevent_req_create(mem_ctx, &state,
4696 struct cli_get_ea_list_path_state);
4697 if (req == NULL) {
4698 return NULL;
4700 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4701 SMB_INFO_QUERY_ALL_EAS, 4,
4702 CLI_BUFFER_SIZE);
4703 if (tevent_req_nomem(subreq, req)) {
4704 return tevent_req_post(req, ev);
4706 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4707 return req;
4710 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4712 struct tevent_req *req = tevent_req_callback_data(
4713 subreq, struct tevent_req);
4714 struct cli_get_ea_list_path_state *state = tevent_req_data(
4715 req, struct cli_get_ea_list_path_state);
4716 NTSTATUS status;
4718 status = cli_qpathinfo_recv(subreq, state, &state->data,
4719 &state->num_data);
4720 TALLOC_FREE(subreq);
4721 if (tevent_req_nterror(req, status)) {
4722 return;
4724 tevent_req_done(req);
4727 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4728 size_t *pnum_eas, struct ea_struct **peas)
4730 struct cli_get_ea_list_path_state *state = tevent_req_data(
4731 req, struct cli_get_ea_list_path_state);
4732 NTSTATUS status;
4734 if (tevent_req_is_nterror(req, &status)) {
4735 return status;
4737 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4738 pnum_eas, peas)) {
4739 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4741 return NT_STATUS_OK;
4744 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4745 TALLOC_CTX *ctx,
4746 size_t *pnum_eas,
4747 struct ea_struct **pea_list)
4749 TALLOC_CTX *frame = NULL;
4750 struct tevent_context *ev = NULL;
4751 struct tevent_req *req = NULL;
4752 NTSTATUS status = NT_STATUS_NO_MEMORY;
4754 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4755 return cli_smb2_get_ea_list_path(cli,
4756 path,
4757 ctx,
4758 pnum_eas,
4759 pea_list);
4762 frame = talloc_stackframe();
4764 if (smbXcli_conn_has_async_calls(cli->conn)) {
4766 * Can't use sync call while an async call is in flight
4768 status = NT_STATUS_INVALID_PARAMETER;
4769 goto fail;
4771 ev = samba_tevent_context_init(frame);
4772 if (ev == NULL) {
4773 goto fail;
4775 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4776 if (req == NULL) {
4777 goto fail;
4779 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4780 goto fail;
4782 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4783 fail:
4784 TALLOC_FREE(frame);
4785 return status;
4788 /****************************************************************************
4789 Convert open "flags" arg to uint32_t on wire.
4790 ****************************************************************************/
4792 static uint32_t open_flags_to_wire(int flags)
4794 int open_mode = flags & O_ACCMODE;
4795 uint32_t ret = 0;
4797 switch (open_mode) {
4798 case O_WRONLY:
4799 ret |= SMB_O_WRONLY;
4800 break;
4801 case O_RDWR:
4802 ret |= SMB_O_RDWR;
4803 break;
4804 default:
4805 case O_RDONLY:
4806 ret |= SMB_O_RDONLY;
4807 break;
4810 if (flags & O_CREAT) {
4811 ret |= SMB_O_CREAT;
4813 if (flags & O_EXCL) {
4814 ret |= SMB_O_EXCL;
4816 if (flags & O_TRUNC) {
4817 ret |= SMB_O_TRUNC;
4819 #if defined(O_SYNC)
4820 if (flags & O_SYNC) {
4821 ret |= SMB_O_SYNC;
4823 #endif /* O_SYNC */
4824 if (flags & O_APPEND) {
4825 ret |= SMB_O_APPEND;
4827 #if defined(O_DIRECT)
4828 if (flags & O_DIRECT) {
4829 ret |= SMB_O_DIRECT;
4831 #endif
4832 #if defined(O_DIRECTORY)
4833 if (flags & O_DIRECTORY) {
4834 ret |= SMB_O_DIRECTORY;
4836 #endif
4837 return ret;
4840 /****************************************************************************
4841 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4842 ****************************************************************************/
4844 struct posix_open_state {
4845 uint16_t setup;
4846 uint8_t *param;
4847 uint8_t data[18];
4848 uint16_t fnum; /* Out */
4851 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4853 struct tevent_req *req = tevent_req_callback_data(
4854 subreq, struct tevent_req);
4855 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4856 NTSTATUS status;
4857 uint8_t *data;
4858 uint32_t num_data;
4860 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4861 NULL, 0, NULL, &data, 12, &num_data);
4862 TALLOC_FREE(subreq);
4863 if (tevent_req_nterror(req, status)) {
4864 return;
4866 state->fnum = SVAL(data,2);
4867 tevent_req_done(req);
4870 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4871 struct tevent_context *ev,
4872 struct cli_state *cli,
4873 const char *fname,
4874 int flags,
4875 mode_t mode,
4876 bool is_dir)
4878 struct tevent_req *req = NULL, *subreq = NULL;
4879 struct posix_open_state *state = NULL;
4880 uint32_t wire_flags = open_flags_to_wire(flags);
4882 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4883 if (req == NULL) {
4884 return NULL;
4887 /* Setup setup word. */
4888 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4890 /* Setup param array. */
4891 state->param = talloc_array(state, uint8_t, 6);
4892 if (tevent_req_nomem(state->param, req)) {
4893 return tevent_req_post(req, ev);
4895 memset(state->param, '\0', 6);
4896 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4898 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
4899 strlen(fname)+1, NULL);
4901 if (tevent_req_nomem(state->param, req)) {
4902 return tevent_req_post(req, ev);
4905 /* Setup data words. */
4906 if (is_dir) {
4907 wire_flags |= SMB_O_DIRECTORY;
4910 SIVAL(state->data,0,0); /* No oplock. */
4911 SIVAL(state->data,4,wire_flags);
4912 SIVAL(state->data,8,unix_perms_to_wire(mode));
4913 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4914 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4916 subreq = cli_trans_send(state, /* mem ctx. */
4917 ev, /* event ctx. */
4918 cli, /* cli_state. */
4919 SMBtrans2, /* cmd. */
4920 NULL, /* pipe name. */
4921 -1, /* fid. */
4922 0, /* function. */
4923 0, /* flags. */
4924 &state->setup, /* setup. */
4925 1, /* num setup uint16_t words. */
4926 0, /* max returned setup. */
4927 state->param, /* param. */
4928 talloc_get_size(state->param),/* num param. */
4929 2, /* max returned param. */
4930 state->data, /* data. */
4931 18, /* num data. */
4932 12); /* max returned data. */
4934 if (tevent_req_nomem(subreq, req)) {
4935 return tevent_req_post(req, ev);
4937 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4938 return req;
4941 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4942 struct tevent_context *ev,
4943 struct cli_state *cli,
4944 const char *fname,
4945 int flags,
4946 mode_t mode)
4948 return cli_posix_open_internal_send(mem_ctx, ev,
4949 cli, fname, flags, mode, false);
4952 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4954 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4955 NTSTATUS status;
4957 if (tevent_req_is_nterror(req, &status)) {
4958 return status;
4960 *pfnum = state->fnum;
4961 return NT_STATUS_OK;
4964 /****************************************************************************
4965 Open - POSIX semantics. Doesn't request oplock.
4966 ****************************************************************************/
4968 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4969 int flags, mode_t mode, uint16_t *pfnum)
4972 TALLOC_CTX *frame = talloc_stackframe();
4973 struct tevent_context *ev = NULL;
4974 struct tevent_req *req = NULL;
4975 NTSTATUS status = NT_STATUS_OK;
4977 if (smbXcli_conn_has_async_calls(cli->conn)) {
4979 * Can't use sync call while an async call is in flight
4981 status = NT_STATUS_INVALID_PARAMETER;
4982 goto fail;
4985 ev = samba_tevent_context_init(frame);
4986 if (ev == NULL) {
4987 status = NT_STATUS_NO_MEMORY;
4988 goto fail;
4991 req = cli_posix_open_send(frame,
4993 cli,
4994 fname,
4995 flags,
4996 mode);
4997 if (req == NULL) {
4998 status = NT_STATUS_NO_MEMORY;
4999 goto fail;
5002 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5003 goto fail;
5006 status = cli_posix_open_recv(req, pfnum);
5008 fail:
5009 TALLOC_FREE(frame);
5010 return status;
5013 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5014 struct tevent_context *ev,
5015 struct cli_state *cli,
5016 const char *fname,
5017 mode_t mode)
5019 return cli_posix_open_internal_send(mem_ctx, ev,
5020 cli, fname, O_CREAT, mode, true);
5023 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5025 return tevent_req_simple_recv_ntstatus(req);
5028 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5030 TALLOC_CTX *frame = talloc_stackframe();
5031 struct tevent_context *ev = NULL;
5032 struct tevent_req *req = NULL;
5033 NTSTATUS status = NT_STATUS_OK;
5035 if (smbXcli_conn_has_async_calls(cli->conn)) {
5037 * Can't use sync call while an async call is in flight
5039 status = NT_STATUS_INVALID_PARAMETER;
5040 goto fail;
5043 ev = samba_tevent_context_init(frame);
5044 if (ev == NULL) {
5045 status = NT_STATUS_NO_MEMORY;
5046 goto fail;
5049 req = cli_posix_mkdir_send(frame,
5051 cli,
5052 fname,
5053 mode);
5054 if (req == NULL) {
5055 status = NT_STATUS_NO_MEMORY;
5056 goto fail;
5059 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5060 goto fail;
5063 status = cli_posix_mkdir_recv(req);
5065 fail:
5066 TALLOC_FREE(frame);
5067 return status;
5070 /****************************************************************************
5071 unlink or rmdir - POSIX semantics.
5072 ****************************************************************************/
5074 struct cli_posix_unlink_internal_state {
5075 uint8_t data[2];
5078 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5080 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5081 struct tevent_context *ev,
5082 struct cli_state *cli,
5083 const char *fname,
5084 uint16_t level)
5086 struct tevent_req *req = NULL, *subreq = NULL;
5087 struct cli_posix_unlink_internal_state *state = NULL;
5089 req = tevent_req_create(mem_ctx, &state,
5090 struct cli_posix_unlink_internal_state);
5091 if (req == NULL) {
5092 return NULL;
5095 /* Setup data word. */
5096 SSVAL(state->data, 0, level);
5098 subreq = cli_setpathinfo_send(state, ev, cli,
5099 SMB_POSIX_PATH_UNLINK,
5100 fname,
5101 state->data, sizeof(state->data));
5102 if (tevent_req_nomem(subreq, req)) {
5103 return tevent_req_post(req, ev);
5105 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5106 return req;
5109 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5111 NTSTATUS status = cli_setpathinfo_recv(subreq);
5112 tevent_req_simple_finish_ntstatus(subreq, status);
5115 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5116 struct tevent_context *ev,
5117 struct cli_state *cli,
5118 const char *fname)
5120 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5121 SMB_POSIX_UNLINK_FILE_TARGET);
5124 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5126 return tevent_req_simple_recv_ntstatus(req);
5129 /****************************************************************************
5130 unlink - POSIX semantics.
5131 ****************************************************************************/
5133 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5135 TALLOC_CTX *frame = talloc_stackframe();
5136 struct tevent_context *ev = NULL;
5137 struct tevent_req *req = NULL;
5138 NTSTATUS status = NT_STATUS_OK;
5140 if (smbXcli_conn_has_async_calls(cli->conn)) {
5142 * Can't use sync call while an async call is in flight
5144 status = NT_STATUS_INVALID_PARAMETER;
5145 goto fail;
5148 ev = samba_tevent_context_init(frame);
5149 if (ev == NULL) {
5150 status = NT_STATUS_NO_MEMORY;
5151 goto fail;
5154 req = cli_posix_unlink_send(frame,
5156 cli,
5157 fname);
5158 if (req == NULL) {
5159 status = NT_STATUS_NO_MEMORY;
5160 goto fail;
5163 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5164 goto fail;
5167 status = cli_posix_unlink_recv(req);
5169 fail:
5170 TALLOC_FREE(frame);
5171 return status;
5174 /****************************************************************************
5175 rmdir - POSIX semantics.
5176 ****************************************************************************/
5178 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5179 struct tevent_context *ev,
5180 struct cli_state *cli,
5181 const char *fname)
5183 return cli_posix_unlink_internal_send(
5184 mem_ctx, ev, cli, fname,
5185 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5188 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5190 return tevent_req_simple_recv_ntstatus(req);
5193 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5195 TALLOC_CTX *frame = talloc_stackframe();
5196 struct tevent_context *ev = NULL;
5197 struct tevent_req *req = NULL;
5198 NTSTATUS status = NT_STATUS_OK;
5200 if (smbXcli_conn_has_async_calls(cli->conn)) {
5202 * Can't use sync call while an async call is in flight
5204 status = NT_STATUS_INVALID_PARAMETER;
5205 goto fail;
5208 ev = samba_tevent_context_init(frame);
5209 if (ev == NULL) {
5210 status = NT_STATUS_NO_MEMORY;
5211 goto fail;
5214 req = cli_posix_rmdir_send(frame,
5216 cli,
5217 fname);
5218 if (req == NULL) {
5219 status = NT_STATUS_NO_MEMORY;
5220 goto fail;
5223 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5224 goto fail;
5227 status = cli_posix_rmdir_recv(req, frame);
5229 fail:
5230 TALLOC_FREE(frame);
5231 return status;
5234 /****************************************************************************
5235 filechangenotify
5236 ****************************************************************************/
5238 struct cli_notify_state {
5239 uint8_t setup[8];
5240 uint32_t num_changes;
5241 struct notify_change *changes;
5244 static void cli_notify_done(struct tevent_req *subreq);
5246 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5247 struct tevent_context *ev,
5248 struct cli_state *cli, uint16_t fnum,
5249 uint32_t buffer_size,
5250 uint32_t completion_filter, bool recursive)
5252 struct tevent_req *req, *subreq;
5253 struct cli_notify_state *state;
5254 unsigned old_timeout;
5256 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5257 if (req == NULL) {
5258 return NULL;
5261 SIVAL(state->setup, 0, completion_filter);
5262 SSVAL(state->setup, 4, fnum);
5263 SSVAL(state->setup, 6, recursive);
5266 * Notifies should not time out
5268 old_timeout = cli_set_timeout(cli, 0);
5270 subreq = cli_trans_send(
5271 state, /* mem ctx. */
5272 ev, /* event ctx. */
5273 cli, /* cli_state. */
5274 SMBnttrans, /* cmd. */
5275 NULL, /* pipe name. */
5276 -1, /* fid. */
5277 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5278 0, /* flags. */
5279 (uint16_t *)state->setup, /* setup. */
5280 4, /* num setup uint16_t words. */
5281 0, /* max returned setup. */
5282 NULL, /* param. */
5283 0, /* num param. */
5284 buffer_size, /* max returned param. */
5285 NULL, /* data. */
5286 0, /* num data. */
5287 0); /* max returned data. */
5289 cli_set_timeout(cli, old_timeout);
5291 if (tevent_req_nomem(subreq, req)) {
5292 return tevent_req_post(req, ev);
5294 tevent_req_set_callback(subreq, cli_notify_done, req);
5295 return req;
5298 static void cli_notify_done(struct tevent_req *subreq)
5300 struct tevent_req *req = tevent_req_callback_data(
5301 subreq, struct tevent_req);
5302 struct cli_notify_state *state = tevent_req_data(
5303 req, struct cli_notify_state);
5304 NTSTATUS status;
5305 uint8_t *params;
5306 uint32_t i, ofs, num_params;
5307 uint16_t flags2;
5309 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5310 &params, 0, &num_params, NULL, 0, NULL);
5311 TALLOC_FREE(subreq);
5312 if (tevent_req_nterror(req, status)) {
5313 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5314 return;
5317 state->num_changes = 0;
5318 ofs = 0;
5320 while (num_params - ofs > 12) {
5321 uint32_t next = IVAL(params, ofs);
5322 state->num_changes += 1;
5324 if ((next == 0) || (ofs+next >= num_params)) {
5325 break;
5327 ofs += next;
5330 state->changes = talloc_array(state, struct notify_change,
5331 state->num_changes);
5332 if (tevent_req_nomem(state->changes, req)) {
5333 TALLOC_FREE(params);
5334 return;
5337 ofs = 0;
5339 for (i=0; i<state->num_changes; i++) {
5340 uint32_t next = IVAL(params, ofs);
5341 uint32_t len = IVAL(params, ofs+8);
5342 ssize_t ret;
5343 char *name;
5345 if (trans_oob(num_params, ofs + 12, len)) {
5346 TALLOC_FREE(params);
5347 tevent_req_nterror(
5348 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5349 return;
5352 state->changes[i].action = IVAL(params, ofs+4);
5353 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5354 &name, params+ofs+12, len,
5355 STR_TERMINATE|STR_UNICODE);
5356 if (ret == -1) {
5357 TALLOC_FREE(params);
5358 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5359 return;
5361 state->changes[i].name = name;
5362 ofs += next;
5365 TALLOC_FREE(params);
5366 tevent_req_done(req);
5369 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5370 uint32_t *pnum_changes,
5371 struct notify_change **pchanges)
5373 struct cli_notify_state *state = tevent_req_data(
5374 req, struct cli_notify_state);
5375 NTSTATUS status;
5377 if (tevent_req_is_nterror(req, &status)) {
5378 return status;
5381 *pnum_changes = state->num_changes;
5382 *pchanges = talloc_move(mem_ctx, &state->changes);
5383 return NT_STATUS_OK;
5386 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5387 uint32_t completion_filter, bool recursive,
5388 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5389 struct notify_change **pchanges)
5391 TALLOC_CTX *frame = talloc_stackframe();
5392 struct tevent_context *ev;
5393 struct tevent_req *req;
5394 NTSTATUS status = NT_STATUS_NO_MEMORY;
5396 if (smbXcli_conn_has_async_calls(cli->conn)) {
5398 * Can't use sync call while an async call is in flight
5400 status = NT_STATUS_INVALID_PARAMETER;
5401 goto fail;
5403 ev = samba_tevent_context_init(frame);
5404 if (ev == NULL) {
5405 goto fail;
5407 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5408 completion_filter, recursive);
5409 if (req == NULL) {
5410 goto fail;
5412 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5413 goto fail;
5415 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5416 fail:
5417 TALLOC_FREE(frame);
5418 return status;
5421 struct cli_qpathinfo_state {
5422 uint8_t *param;
5423 uint8_t *data;
5424 uint16_t setup[1];
5425 uint32_t min_rdata;
5426 uint8_t *rdata;
5427 uint32_t num_rdata;
5430 static void cli_qpathinfo_done(struct tevent_req *subreq);
5432 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5433 struct tevent_context *ev,
5434 struct cli_state *cli, const char *fname,
5435 uint16_t level, uint32_t min_rdata,
5436 uint32_t max_rdata)
5438 struct tevent_req *req, *subreq;
5439 struct cli_qpathinfo_state *state;
5441 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5442 if (req == NULL) {
5443 return NULL;
5445 state->min_rdata = min_rdata;
5446 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5448 state->param = talloc_zero_array(state, uint8_t, 6);
5449 if (tevent_req_nomem(state->param, req)) {
5450 return tevent_req_post(req, ev);
5452 SSVAL(state->param, 0, level);
5453 state->param = trans2_bytes_push_str(
5454 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5455 if (tevent_req_nomem(state->param, req)) {
5456 return tevent_req_post(req, ev);
5459 subreq = cli_trans_send(
5460 state, /* mem ctx. */
5461 ev, /* event ctx. */
5462 cli, /* cli_state. */
5463 SMBtrans2, /* cmd. */
5464 NULL, /* pipe name. */
5465 -1, /* fid. */
5466 0, /* function. */
5467 0, /* flags. */
5468 state->setup, /* setup. */
5469 1, /* num setup uint16_t words. */
5470 0, /* max returned setup. */
5471 state->param, /* param. */
5472 talloc_get_size(state->param), /* num param. */
5473 2, /* max returned param. */
5474 NULL, /* data. */
5475 0, /* num data. */
5476 max_rdata); /* max returned data. */
5478 if (tevent_req_nomem(subreq, req)) {
5479 return tevent_req_post(req, ev);
5481 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5482 return req;
5485 static void cli_qpathinfo_done(struct tevent_req *subreq)
5487 struct tevent_req *req = tevent_req_callback_data(
5488 subreq, struct tevent_req);
5489 struct cli_qpathinfo_state *state = tevent_req_data(
5490 req, struct cli_qpathinfo_state);
5491 NTSTATUS status;
5493 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5494 NULL, 0, NULL,
5495 &state->rdata, state->min_rdata,
5496 &state->num_rdata);
5497 if (tevent_req_nterror(req, status)) {
5498 return;
5500 tevent_req_done(req);
5503 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5504 uint8_t **rdata, uint32_t *num_rdata)
5506 struct cli_qpathinfo_state *state = tevent_req_data(
5507 req, struct cli_qpathinfo_state);
5508 NTSTATUS status;
5510 if (tevent_req_is_nterror(req, &status)) {
5511 return status;
5513 if (rdata != NULL) {
5514 *rdata = talloc_move(mem_ctx, &state->rdata);
5515 } else {
5516 TALLOC_FREE(state->rdata);
5518 if (num_rdata != NULL) {
5519 *num_rdata = state->num_rdata;
5521 return NT_STATUS_OK;
5524 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5525 const char *fname, uint16_t level, uint32_t min_rdata,
5526 uint32_t max_rdata,
5527 uint8_t **rdata, uint32_t *num_rdata)
5529 TALLOC_CTX *frame = talloc_stackframe();
5530 struct tevent_context *ev;
5531 struct tevent_req *req;
5532 NTSTATUS status = NT_STATUS_NO_MEMORY;
5534 if (smbXcli_conn_has_async_calls(cli->conn)) {
5536 * Can't use sync call while an async call is in flight
5538 status = NT_STATUS_INVALID_PARAMETER;
5539 goto fail;
5541 ev = samba_tevent_context_init(frame);
5542 if (ev == NULL) {
5543 goto fail;
5545 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5546 max_rdata);
5547 if (req == NULL) {
5548 goto fail;
5550 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5551 goto fail;
5553 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5554 fail:
5555 TALLOC_FREE(frame);
5556 return status;
5559 struct cli_qfileinfo_state {
5560 uint16_t setup[1];
5561 uint8_t param[4];
5562 uint8_t *data;
5563 uint16_t recv_flags2;
5564 uint32_t min_rdata;
5565 uint8_t *rdata;
5566 uint32_t num_rdata;
5569 static void cli_qfileinfo_done(struct tevent_req *subreq);
5571 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5572 struct tevent_context *ev,
5573 struct cli_state *cli, uint16_t fnum,
5574 uint16_t level, uint32_t min_rdata,
5575 uint32_t max_rdata)
5577 struct tevent_req *req, *subreq;
5578 struct cli_qfileinfo_state *state;
5580 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5581 if (req == NULL) {
5582 return NULL;
5584 state->min_rdata = min_rdata;
5585 SSVAL(state->param, 0, fnum);
5586 SSVAL(state->param, 2, level);
5587 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5589 subreq = cli_trans_send(
5590 state, /* mem ctx. */
5591 ev, /* event ctx. */
5592 cli, /* cli_state. */
5593 SMBtrans2, /* cmd. */
5594 NULL, /* pipe name. */
5595 -1, /* fid. */
5596 0, /* function. */
5597 0, /* flags. */
5598 state->setup, /* setup. */
5599 1, /* num setup uint16_t words. */
5600 0, /* max returned setup. */
5601 state->param, /* param. */
5602 sizeof(state->param), /* num param. */
5603 2, /* max returned param. */
5604 NULL, /* data. */
5605 0, /* num data. */
5606 max_rdata); /* max returned data. */
5608 if (tevent_req_nomem(subreq, req)) {
5609 return tevent_req_post(req, ev);
5611 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5612 return req;
5615 static void cli_qfileinfo_done(struct tevent_req *subreq)
5617 struct tevent_req *req = tevent_req_callback_data(
5618 subreq, struct tevent_req);
5619 struct cli_qfileinfo_state *state = tevent_req_data(
5620 req, struct cli_qfileinfo_state);
5621 NTSTATUS status;
5623 status = cli_trans_recv(subreq, state,
5624 &state->recv_flags2,
5625 NULL, 0, NULL,
5626 NULL, 0, NULL,
5627 &state->rdata, state->min_rdata,
5628 &state->num_rdata);
5629 if (tevent_req_nterror(req, status)) {
5630 return;
5632 tevent_req_done(req);
5635 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5636 uint16_t *recv_flags2,
5637 uint8_t **rdata, uint32_t *num_rdata)
5639 struct cli_qfileinfo_state *state = tevent_req_data(
5640 req, struct cli_qfileinfo_state);
5641 NTSTATUS status;
5643 if (tevent_req_is_nterror(req, &status)) {
5644 return status;
5647 if (recv_flags2 != NULL) {
5648 *recv_flags2 = state->recv_flags2;
5650 if (rdata != NULL) {
5651 *rdata = talloc_move(mem_ctx, &state->rdata);
5652 } else {
5653 TALLOC_FREE(state->rdata);
5655 if (num_rdata != NULL) {
5656 *num_rdata = state->num_rdata;
5658 return NT_STATUS_OK;
5661 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5662 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5663 uint32_t max_rdata, uint16_t *recv_flags2,
5664 uint8_t **rdata, uint32_t *num_rdata)
5666 TALLOC_CTX *frame = talloc_stackframe();
5667 struct tevent_context *ev;
5668 struct tevent_req *req;
5669 NTSTATUS status = NT_STATUS_NO_MEMORY;
5671 if (smbXcli_conn_has_async_calls(cli->conn)) {
5673 * Can't use sync call while an async call is in flight
5675 status = NT_STATUS_INVALID_PARAMETER;
5676 goto fail;
5678 ev = samba_tevent_context_init(frame);
5679 if (ev == NULL) {
5680 goto fail;
5682 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5683 max_rdata);
5684 if (req == NULL) {
5685 goto fail;
5687 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5688 goto fail;
5690 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5691 fail:
5692 TALLOC_FREE(frame);
5693 return status;
5696 struct cli_flush_state {
5697 uint16_t vwv[1];
5700 static void cli_flush_done(struct tevent_req *subreq);
5702 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5703 struct tevent_context *ev,
5704 struct cli_state *cli,
5705 uint16_t fnum)
5707 struct tevent_req *req, *subreq;
5708 struct cli_flush_state *state;
5710 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5711 if (req == NULL) {
5712 return NULL;
5714 SSVAL(state->vwv + 0, 0, fnum);
5716 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5717 0, NULL);
5718 if (tevent_req_nomem(subreq, req)) {
5719 return tevent_req_post(req, ev);
5721 tevent_req_set_callback(subreq, cli_flush_done, req);
5722 return req;
5725 static void cli_flush_done(struct tevent_req *subreq)
5727 struct tevent_req *req = tevent_req_callback_data(
5728 subreq, struct tevent_req);
5729 NTSTATUS status;
5731 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5732 TALLOC_FREE(subreq);
5733 if (tevent_req_nterror(req, status)) {
5734 return;
5736 tevent_req_done(req);
5739 NTSTATUS cli_flush_recv(struct tevent_req *req)
5741 return tevent_req_simple_recv_ntstatus(req);
5744 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5746 TALLOC_CTX *frame = talloc_stackframe();
5747 struct tevent_context *ev;
5748 struct tevent_req *req;
5749 NTSTATUS status = NT_STATUS_NO_MEMORY;
5751 if (smbXcli_conn_has_async_calls(cli->conn)) {
5753 * Can't use sync call while an async call is in flight
5755 status = NT_STATUS_INVALID_PARAMETER;
5756 goto fail;
5758 ev = samba_tevent_context_init(frame);
5759 if (ev == NULL) {
5760 goto fail;
5762 req = cli_flush_send(frame, ev, cli, fnum);
5763 if (req == NULL) {
5764 goto fail;
5766 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5767 goto fail;
5769 status = cli_flush_recv(req);
5770 fail:
5771 TALLOC_FREE(frame);
5772 return status;
5775 struct cli_shadow_copy_data_state {
5776 uint16_t setup[4];
5777 uint8_t *data;
5778 uint32_t num_data;
5779 bool get_names;
5782 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5784 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5785 struct tevent_context *ev,
5786 struct cli_state *cli,
5787 uint16_t fnum,
5788 bool get_names)
5790 struct tevent_req *req, *subreq;
5791 struct cli_shadow_copy_data_state *state;
5792 uint32_t ret_size;
5794 req = tevent_req_create(mem_ctx, &state,
5795 struct cli_shadow_copy_data_state);
5796 if (req == NULL) {
5797 return NULL;
5799 state->get_names = get_names;
5800 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5802 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5803 SSVAL(state->setup + 2, 0, fnum);
5804 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5805 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5807 subreq = cli_trans_send(
5808 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5809 state->setup, ARRAY_SIZE(state->setup), 0,
5810 NULL, 0, 0,
5811 NULL, 0, ret_size);
5812 if (tevent_req_nomem(subreq, req)) {
5813 return tevent_req_post(req, ev);
5815 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5816 return req;
5819 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5821 struct tevent_req *req = tevent_req_callback_data(
5822 subreq, struct tevent_req);
5823 struct cli_shadow_copy_data_state *state = tevent_req_data(
5824 req, struct cli_shadow_copy_data_state);
5825 NTSTATUS status;
5827 status = cli_trans_recv(subreq, state, NULL,
5828 NULL, 0, NULL, /* setup */
5829 NULL, 0, NULL, /* param */
5830 &state->data, 12, &state->num_data);
5831 TALLOC_FREE(subreq);
5832 if (tevent_req_nterror(req, status)) {
5833 return;
5835 tevent_req_done(req);
5838 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5839 char ***pnames, int *pnum_names)
5841 struct cli_shadow_copy_data_state *state = tevent_req_data(
5842 req, struct cli_shadow_copy_data_state);
5843 char **names;
5844 int i, num_names;
5845 uint32_t dlength;
5846 NTSTATUS status;
5848 if (tevent_req_is_nterror(req, &status)) {
5849 return status;
5851 num_names = IVAL(state->data, 4);
5852 dlength = IVAL(state->data, 8);
5854 if (!state->get_names) {
5855 *pnum_names = num_names;
5856 return NT_STATUS_OK;
5859 if (dlength+12 > state->num_data) {
5860 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5862 names = talloc_array(mem_ctx, char *, num_names);
5863 if (names == NULL) {
5864 return NT_STATUS_NO_MEMORY;
5867 for (i=0; i<num_names; i++) {
5868 bool ret;
5869 uint8_t *src;
5870 size_t converted_size;
5872 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5873 ret = convert_string_talloc(
5874 names, CH_UTF16LE, CH_UNIX,
5875 src, 2 * sizeof(SHADOW_COPY_LABEL),
5876 &names[i], &converted_size);
5877 if (!ret) {
5878 TALLOC_FREE(names);
5879 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5882 *pnum_names = num_names;
5883 *pnames = names;
5884 return NT_STATUS_OK;
5887 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5888 uint16_t fnum, bool get_names,
5889 char ***pnames, int *pnum_names)
5891 TALLOC_CTX *frame = talloc_stackframe();
5892 struct tevent_context *ev;
5893 struct tevent_req *req;
5894 NTSTATUS status = NT_STATUS_NO_MEMORY;
5896 if (smbXcli_conn_has_async_calls(cli->conn)) {
5898 * Can't use sync call while an async call is in flight
5900 status = NT_STATUS_INVALID_PARAMETER;
5901 goto fail;
5903 ev = samba_tevent_context_init(frame);
5904 if (ev == NULL) {
5905 goto fail;
5907 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5908 if (req == NULL) {
5909 goto fail;
5911 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5912 goto fail;
5914 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5915 fail:
5916 TALLOC_FREE(frame);
5917 return status;