s3: libsmb: s3: libsmb: Plumb in additional_flags2 = FLAGS2_REPARSE_PATH to cli_mkdir...
[Samba.git] / source3 / libsmb / clifile.c
blobfaabaeba59a4a619f5ca72e67f2483ae90d97c40
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;
176 uint16_t additional_flags2 = 0;
178 req = tevent_req_create(mem_ctx, &state,
179 struct cli_setpathinfo_state);
180 if (req == NULL) {
181 return NULL;
184 /* Setup setup word. */
185 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
187 /* Setup param array. */
188 state->param = talloc_zero_array(state, uint8_t, 6);
189 if (tevent_req_nomem(state->param, req)) {
190 return tevent_req_post(req, ev);
192 SSVAL(state->param, 0, level);
194 state->param = trans2_bytes_push_str(
195 state->param, smbXcli_conn_use_unicode(cli->conn), path, strlen(path)+1, NULL);
196 if (tevent_req_nomem(state->param, req)) {
197 return tevent_req_post(req, ev);
200 if (clistr_is_previous_version_path(path) &&
201 !INFO_LEVEL_IS_UNIX(level)) {
202 additional_flags2 = FLAGS2_REPARSE_PATH;
205 subreq = cli_trans_send(
206 state, /* mem ctx. */
207 ev, /* event ctx. */
208 cli, /* cli_state. */
209 additional_flags2, /* additional_flags2 */
210 SMBtrans2, /* cmd. */
211 NULL, /* pipe name. */
212 -1, /* fid. */
213 0, /* function. */
214 0, /* flags. */
215 &state->setup, /* setup. */
216 1, /* num setup uint16_t words. */
217 0, /* max returned setup. */
218 state->param, /* param. */
219 talloc_get_size(state->param), /* num param. */
220 2, /* max returned param. */
221 data, /* data. */
222 data_len, /* num data. */
223 0); /* max returned data. */
225 if (tevent_req_nomem(subreq, req)) {
226 return tevent_req_post(req, ev);
228 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
229 return req;
232 static void cli_setpathinfo_done(struct tevent_req *subreq)
234 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
235 NULL, 0, NULL, NULL, 0, NULL);
236 tevent_req_simple_finish_ntstatus(subreq, status);
239 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
241 return tevent_req_simple_recv_ntstatus(req);
244 NTSTATUS cli_setpathinfo(struct cli_state *cli,
245 uint16_t level,
246 const char *path,
247 uint8_t *data,
248 size_t data_len)
250 TALLOC_CTX *frame = talloc_stackframe();
251 struct tevent_context *ev;
252 struct tevent_req *req;
253 NTSTATUS status = NT_STATUS_NO_MEMORY;
255 if (smbXcli_conn_has_async_calls(cli->conn)) {
257 * Can't use sync call while an async call is in flight
259 status = NT_STATUS_INVALID_PARAMETER;
260 goto fail;
262 ev = samba_tevent_context_init(frame);
263 if (ev == NULL) {
264 goto fail;
266 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
267 if (req == NULL) {
268 goto fail;
270 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
271 goto fail;
273 status = cli_setpathinfo_recv(req);
274 fail:
275 TALLOC_FREE(frame);
276 return status;
279 /****************************************************************************
280 Hard/Symlink a file (UNIX extensions).
281 Creates new name (sym)linked to oldname.
282 ****************************************************************************/
284 struct cli_posix_link_internal_state {
285 uint8_t *data;
288 static void cli_posix_link_internal_done(struct tevent_req *subreq);
290 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
291 struct tevent_context *ev,
292 struct cli_state *cli,
293 uint16_t level,
294 const char *oldname,
295 const char *newname)
297 struct tevent_req *req = NULL, *subreq = NULL;
298 struct cli_posix_link_internal_state *state = NULL;
300 req = tevent_req_create(mem_ctx, &state,
301 struct cli_posix_link_internal_state);
302 if (req == NULL) {
303 return NULL;
306 /* Setup data array. */
307 state->data = talloc_array(state, uint8_t, 0);
308 if (tevent_req_nomem(state->data, req)) {
309 return tevent_req_post(req, ev);
311 state->data = trans2_bytes_push_str(
312 state->data, smbXcli_conn_use_unicode(cli->conn), oldname, strlen(oldname)+1, NULL);
314 subreq = cli_setpathinfo_send(
315 state, ev, cli, level, newname,
316 state->data, talloc_get_size(state->data));
317 if (tevent_req_nomem(subreq, req)) {
318 return tevent_req_post(req, ev);
320 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
321 return req;
324 static void cli_posix_link_internal_done(struct tevent_req *subreq)
326 NTSTATUS status = cli_setpathinfo_recv(subreq);
327 tevent_req_simple_finish_ntstatus(subreq, status);
330 /****************************************************************************
331 Symlink a file (UNIX extensions).
332 ****************************************************************************/
334 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
335 struct tevent_context *ev,
336 struct cli_state *cli,
337 const char *oldname,
338 const char *newname)
340 return cli_posix_link_internal_send(
341 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
344 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
346 return tevent_req_simple_recv_ntstatus(req);
349 NTSTATUS cli_posix_symlink(struct cli_state *cli,
350 const char *oldname,
351 const char *newname)
353 TALLOC_CTX *frame = talloc_stackframe();
354 struct tevent_context *ev = NULL;
355 struct tevent_req *req = NULL;
356 NTSTATUS status = NT_STATUS_OK;
358 if (smbXcli_conn_has_async_calls(cli->conn)) {
360 * Can't use sync call while an async call is in flight
362 status = NT_STATUS_INVALID_PARAMETER;
363 goto fail;
366 ev = samba_tevent_context_init(frame);
367 if (ev == NULL) {
368 status = NT_STATUS_NO_MEMORY;
369 goto fail;
372 req = cli_posix_symlink_send(frame,
374 cli,
375 oldname,
376 newname);
377 if (req == NULL) {
378 status = NT_STATUS_NO_MEMORY;
379 goto fail;
382 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
383 goto fail;
386 status = cli_posix_symlink_recv(req);
388 fail:
389 TALLOC_FREE(frame);
390 return status;
393 /****************************************************************************
394 Read a POSIX symlink.
395 ****************************************************************************/
397 struct readlink_state {
398 uint8_t *data;
399 uint32_t num_data;
402 static void cli_posix_readlink_done(struct tevent_req *subreq);
404 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
405 struct tevent_context *ev,
406 struct cli_state *cli,
407 const char *fname,
408 size_t len)
410 struct tevent_req *req = NULL, *subreq = NULL;
411 struct readlink_state *state = NULL;
412 uint32_t maxbytelen = (uint32_t)(smbXcli_conn_use_unicode(cli->conn) ? len*3 : len);
414 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
415 if (req == NULL) {
416 return NULL;
420 * Len is in bytes, we need it in UCS2 units.
422 if ((2*len < len) || (maxbytelen < len)) {
423 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
424 return tevent_req_post(req, ev);
427 subreq = cli_qpathinfo_send(state, ev, cli, fname,
428 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
429 if (tevent_req_nomem(subreq, req)) {
430 return tevent_req_post(req, ev);
432 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
433 return req;
436 static void cli_posix_readlink_done(struct tevent_req *subreq)
438 struct tevent_req *req = tevent_req_callback_data(
439 subreq, struct tevent_req);
440 struct readlink_state *state = tevent_req_data(
441 req, struct readlink_state);
442 NTSTATUS status;
444 status = cli_qpathinfo_recv(subreq, state, &state->data,
445 &state->num_data);
446 TALLOC_FREE(subreq);
447 if (tevent_req_nterror(req, status)) {
448 return;
451 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
453 if (state->data[state->num_data-1] != '\0') {
454 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
455 return;
457 tevent_req_done(req);
460 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
461 char *retpath, size_t len)
463 NTSTATUS status;
464 char *converted = NULL;
465 size_t converted_size = 0;
466 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
468 if (tevent_req_is_nterror(req, &status)) {
469 return status;
471 /* The returned data is a pushed string, not raw data. */
472 if (!convert_string_talloc(state,
473 smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
474 CH_UNIX,
475 state->data,
476 state->num_data,
477 &converted,
478 &converted_size)) {
479 return NT_STATUS_NO_MEMORY;
482 len = MIN(len,converted_size);
483 if (len == 0) {
484 return NT_STATUS_DATA_ERROR;
486 memcpy(retpath, converted, len);
487 return NT_STATUS_OK;
490 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
491 char *linkpath, size_t len)
493 TALLOC_CTX *frame = talloc_stackframe();
494 struct tevent_context *ev = NULL;
495 struct tevent_req *req = NULL;
496 NTSTATUS status = NT_STATUS_OK;
498 if (smbXcli_conn_has_async_calls(cli->conn)) {
500 * Can't use sync call while an async call is in flight
502 status = NT_STATUS_INVALID_PARAMETER;
503 goto fail;
506 ev = samba_tevent_context_init(frame);
507 if (ev == NULL) {
508 status = NT_STATUS_NO_MEMORY;
509 goto fail;
512 req = cli_posix_readlink_send(frame,
514 cli,
515 fname,
516 len);
517 if (req == NULL) {
518 status = NT_STATUS_NO_MEMORY;
519 goto fail;
522 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
523 goto fail;
526 status = cli_posix_readlink_recv(req, cli, linkpath, len);
528 fail:
529 TALLOC_FREE(frame);
530 return status;
533 /****************************************************************************
534 Hard link a file (UNIX extensions).
535 ****************************************************************************/
537 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
538 struct tevent_context *ev,
539 struct cli_state *cli,
540 const char *oldname,
541 const char *newname)
543 return cli_posix_link_internal_send(
544 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
547 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
549 return tevent_req_simple_recv_ntstatus(req);
552 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
553 const char *oldname,
554 const char *newname)
556 TALLOC_CTX *frame = talloc_stackframe();
557 struct tevent_context *ev = NULL;
558 struct tevent_req *req = NULL;
559 NTSTATUS status = NT_STATUS_OK;
561 if (smbXcli_conn_has_async_calls(cli->conn)) {
563 * Can't use sync call while an async call is in flight
565 status = NT_STATUS_INVALID_PARAMETER;
566 goto fail;
569 ev = samba_tevent_context_init(frame);
570 if (ev == NULL) {
571 status = NT_STATUS_NO_MEMORY;
572 goto fail;
575 req = cli_posix_hardlink_send(frame,
577 cli,
578 oldname,
579 newname);
580 if (req == NULL) {
581 status = NT_STATUS_NO_MEMORY;
582 goto fail;
585 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
586 goto fail;
589 status = cli_posix_hardlink_recv(req);
591 fail:
592 TALLOC_FREE(frame);
593 return status;
596 /****************************************************************************
597 Do a POSIX getacl - pathname based ACL get (UNIX extensions).
598 ****************************************************************************/
600 struct getacl_state {
601 uint32_t num_data;
602 uint8_t *data;
605 static void cli_posix_getacl_done(struct tevent_req *subreq);
607 struct tevent_req *cli_posix_getacl_send(TALLOC_CTX *mem_ctx,
608 struct tevent_context *ev,
609 struct cli_state *cli,
610 const char *fname)
612 struct tevent_req *req = NULL, *subreq = NULL;
613 struct getacl_state *state = NULL;
615 req = tevent_req_create(mem_ctx, &state, struct getacl_state);
616 if (req == NULL) {
617 return NULL;
619 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
620 0, CLI_BUFFER_SIZE);
621 if (tevent_req_nomem(subreq, req)) {
622 return tevent_req_post(req, ev);
624 tevent_req_set_callback(subreq, cli_posix_getacl_done, req);
625 return req;
628 static void cli_posix_getacl_done(struct tevent_req *subreq)
630 struct tevent_req *req = tevent_req_callback_data(
631 subreq, struct tevent_req);
632 struct getacl_state *state = tevent_req_data(
633 req, struct getacl_state);
634 NTSTATUS status;
636 status = cli_qpathinfo_recv(subreq, state, &state->data,
637 &state->num_data);
638 TALLOC_FREE(subreq);
639 if (tevent_req_nterror(req, status)) {
640 return;
642 tevent_req_done(req);
645 NTSTATUS cli_posix_getacl_recv(struct tevent_req *req,
646 TALLOC_CTX *mem_ctx,
647 size_t *prb_size,
648 char **retbuf)
650 struct getacl_state *state = tevent_req_data(req, struct getacl_state);
651 NTSTATUS status;
653 if (tevent_req_is_nterror(req, &status)) {
654 return status;
656 *prb_size = (size_t)state->num_data;
657 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
658 return NT_STATUS_OK;
661 NTSTATUS cli_posix_getacl(struct cli_state *cli,
662 const char *fname,
663 TALLOC_CTX *mem_ctx,
664 size_t *prb_size,
665 char **retbuf)
667 TALLOC_CTX *frame = talloc_stackframe();
668 struct tevent_context *ev = NULL;
669 struct tevent_req *req = NULL;
670 NTSTATUS status = NT_STATUS_OK;
672 if (smbXcli_conn_has_async_calls(cli->conn)) {
674 * Can't use sync call while an async call is in flight
676 status = NT_STATUS_INVALID_PARAMETER;
677 goto fail;
680 ev = samba_tevent_context_init(frame);
681 if (ev == NULL) {
682 status = NT_STATUS_NO_MEMORY;
683 goto fail;
686 req = cli_posix_getacl_send(frame,
688 cli,
689 fname);
690 if (req == NULL) {
691 status = NT_STATUS_NO_MEMORY;
692 goto fail;
695 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
696 goto fail;
699 status = cli_posix_getacl_recv(req, mem_ctx, prb_size, retbuf);
701 fail:
702 TALLOC_FREE(frame);
703 return status;
706 /****************************************************************************
707 Do a POSIX setacl - pathname based ACL set (UNIX extensions).
708 ****************************************************************************/
710 struct setacl_state {
711 uint8_t *data;
714 static void cli_posix_setacl_done(struct tevent_req *subreq);
716 struct tevent_req *cli_posix_setacl_send(TALLOC_CTX *mem_ctx,
717 struct tevent_context *ev,
718 struct cli_state *cli,
719 const char *fname,
720 const void *data,
721 size_t num_data)
723 struct tevent_req *req = NULL, *subreq = NULL;
724 struct setacl_state *state = NULL;
726 req = tevent_req_create(mem_ctx, &state, struct setacl_state);
727 if (req == NULL) {
728 return NULL;
730 state->data = talloc_memdup(state, data, num_data);
731 if (tevent_req_nomem(state->data, req)) {
732 return tevent_req_post(req, ev);
735 subreq = cli_setpathinfo_send(state,
737 cli,
738 SMB_SET_POSIX_ACL,
739 fname,
740 state->data,
741 num_data);
742 if (tevent_req_nomem(subreq, req)) {
743 return tevent_req_post(req, ev);
745 tevent_req_set_callback(subreq, cli_posix_setacl_done, req);
746 return req;
749 static void cli_posix_setacl_done(struct tevent_req *subreq)
751 NTSTATUS status = cli_setpathinfo_recv(subreq);
752 tevent_req_simple_finish_ntstatus(subreq, status);
755 NTSTATUS cli_posix_setacl_recv(struct tevent_req *req)
757 return tevent_req_simple_recv_ntstatus(req);
760 NTSTATUS cli_posix_setacl(struct cli_state *cli,
761 const char *fname,
762 const void *acl_buf,
763 size_t acl_buf_size)
765 TALLOC_CTX *frame = talloc_stackframe();
766 struct tevent_context *ev = NULL;
767 struct tevent_req *req = NULL;
768 NTSTATUS status = NT_STATUS_OK;
770 if (smbXcli_conn_has_async_calls(cli->conn)) {
772 * Can't use sync call while an async call is in flight
774 status = NT_STATUS_INVALID_PARAMETER;
775 goto fail;
778 ev = samba_tevent_context_init(frame);
779 if (ev == NULL) {
780 status = NT_STATUS_NO_MEMORY;
781 goto fail;
784 req = cli_posix_setacl_send(frame,
786 cli,
787 fname,
788 acl_buf,
789 acl_buf_size);
790 if (req == NULL) {
791 status = NT_STATUS_NO_MEMORY;
792 goto fail;
795 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
796 goto fail;
799 status = cli_posix_setacl_recv(req);
801 fail:
802 TALLOC_FREE(frame);
803 return status;
806 /****************************************************************************
807 Stat a file (UNIX extensions).
808 ****************************************************************************/
810 struct stat_state {
811 uint32_t num_data;
812 uint8_t *data;
815 static void cli_posix_stat_done(struct tevent_req *subreq);
817 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
818 struct tevent_context *ev,
819 struct cli_state *cli,
820 const char *fname)
822 struct tevent_req *req = NULL, *subreq = NULL;
823 struct stat_state *state = NULL;
825 req = tevent_req_create(mem_ctx, &state, struct stat_state);
826 if (req == NULL) {
827 return NULL;
829 subreq = cli_qpathinfo_send(state, ev, cli, fname,
830 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
831 if (tevent_req_nomem(subreq, req)) {
832 return tevent_req_post(req, ev);
834 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
835 return req;
838 static void cli_posix_stat_done(struct tevent_req *subreq)
840 struct tevent_req *req = tevent_req_callback_data(
841 subreq, struct tevent_req);
842 struct stat_state *state = tevent_req_data(req, struct stat_state);
843 NTSTATUS status;
845 status = cli_qpathinfo_recv(subreq, state, &state->data,
846 &state->num_data);
847 TALLOC_FREE(subreq);
848 if (tevent_req_nterror(req, status)) {
849 return;
851 tevent_req_done(req);
854 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
855 SMB_STRUCT_STAT *sbuf)
857 struct stat_state *state = tevent_req_data(req, struct stat_state);
858 NTSTATUS status;
860 if (tevent_req_is_nterror(req, &status)) {
861 return status;
864 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
865 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
866 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
867 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
868 #else
869 /* assume 512 byte blocks */
870 sbuf->st_ex_blocks /= 512;
871 #endif
872 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
873 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
874 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
876 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
877 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
878 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
879 #if defined(HAVE_MAKEDEV)
881 uint32_t dev_major = IVAL(state->data,60);
882 uint32_t dev_minor = IVAL(state->data,68);
883 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
885 #endif
886 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
887 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
888 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
890 return NT_STATUS_OK;
893 NTSTATUS cli_posix_stat(struct cli_state *cli,
894 const char *fname,
895 SMB_STRUCT_STAT *sbuf)
897 TALLOC_CTX *frame = talloc_stackframe();
898 struct tevent_context *ev = NULL;
899 struct tevent_req *req = NULL;
900 NTSTATUS status = NT_STATUS_OK;
902 if (smbXcli_conn_has_async_calls(cli->conn)) {
904 * Can't use sync call while an async call is in flight
906 status = NT_STATUS_INVALID_PARAMETER;
907 goto fail;
910 ev = samba_tevent_context_init(frame);
911 if (ev == NULL) {
912 status = NT_STATUS_NO_MEMORY;
913 goto fail;
916 req = cli_posix_stat_send(frame,
918 cli,
919 fname);
920 if (req == NULL) {
921 status = NT_STATUS_NO_MEMORY;
922 goto fail;
925 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
926 goto fail;
929 status = cli_posix_stat_recv(req, sbuf);
931 fail:
932 TALLOC_FREE(frame);
933 return status;
936 /****************************************************************************
937 Chmod or chown a file internal (UNIX extensions).
938 ****************************************************************************/
940 struct cli_posix_chown_chmod_internal_state {
941 uint8_t data[100];
944 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
946 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
947 struct tevent_context *ev,
948 struct cli_state *cli,
949 const char *fname,
950 uint32_t mode,
951 uint32_t uid,
952 uint32_t gid)
954 struct tevent_req *req = NULL, *subreq = NULL;
955 struct cli_posix_chown_chmod_internal_state *state = NULL;
957 req = tevent_req_create(mem_ctx, &state,
958 struct cli_posix_chown_chmod_internal_state);
959 if (req == NULL) {
960 return NULL;
963 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
964 memset(&state->data[40], '\0', 60);
965 SIVAL(state->data,40,uid);
966 SIVAL(state->data,48,gid);
967 SIVAL(state->data,84,mode);
969 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
970 fname, state->data, sizeof(state->data));
971 if (tevent_req_nomem(subreq, req)) {
972 return tevent_req_post(req, ev);
974 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
975 req);
976 return req;
979 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
981 NTSTATUS status = cli_setpathinfo_recv(subreq);
982 tevent_req_simple_finish_ntstatus(subreq, status);
985 /****************************************************************************
986 chmod a file (UNIX extensions).
987 ****************************************************************************/
989 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
990 struct tevent_context *ev,
991 struct cli_state *cli,
992 const char *fname,
993 mode_t mode)
995 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
996 fname,
997 unix_perms_to_wire(mode),
998 SMB_UID_NO_CHANGE,
999 SMB_GID_NO_CHANGE);
1002 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
1004 return tevent_req_simple_recv_ntstatus(req);
1007 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
1009 TALLOC_CTX *frame = talloc_stackframe();
1010 struct tevent_context *ev = NULL;
1011 struct tevent_req *req = NULL;
1012 NTSTATUS status = NT_STATUS_OK;
1014 if (smbXcli_conn_has_async_calls(cli->conn)) {
1016 * Can't use sync call while an async call is in flight
1018 status = NT_STATUS_INVALID_PARAMETER;
1019 goto fail;
1022 ev = samba_tevent_context_init(frame);
1023 if (ev == NULL) {
1024 status = NT_STATUS_NO_MEMORY;
1025 goto fail;
1028 req = cli_posix_chmod_send(frame,
1030 cli,
1031 fname,
1032 mode);
1033 if (req == NULL) {
1034 status = NT_STATUS_NO_MEMORY;
1035 goto fail;
1038 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1039 goto fail;
1042 status = cli_posix_chmod_recv(req);
1044 fail:
1045 TALLOC_FREE(frame);
1046 return status;
1049 /****************************************************************************
1050 chown a file (UNIX extensions).
1051 ****************************************************************************/
1053 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1054 struct tevent_context *ev,
1055 struct cli_state *cli,
1056 const char *fname,
1057 uid_t uid,
1058 gid_t gid)
1060 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1061 fname,
1062 SMB_MODE_NO_CHANGE,
1063 (uint32_t)uid,
1064 (uint32_t)gid);
1067 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1069 return tevent_req_simple_recv_ntstatus(req);
1072 NTSTATUS cli_posix_chown(struct cli_state *cli,
1073 const char *fname,
1074 uid_t uid,
1075 gid_t gid)
1077 TALLOC_CTX *frame = talloc_stackframe();
1078 struct tevent_context *ev = NULL;
1079 struct tevent_req *req = NULL;
1080 NTSTATUS status = NT_STATUS_OK;
1082 if (smbXcli_conn_has_async_calls(cli->conn)) {
1084 * Can't use sync call while an async call is in flight
1086 status = NT_STATUS_INVALID_PARAMETER;
1087 goto fail;
1090 ev = samba_tevent_context_init(frame);
1091 if (ev == NULL) {
1092 status = NT_STATUS_NO_MEMORY;
1093 goto fail;
1096 req = cli_posix_chown_send(frame,
1098 cli,
1099 fname,
1100 uid,
1101 gid);
1102 if (req == NULL) {
1103 status = NT_STATUS_NO_MEMORY;
1104 goto fail;
1107 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1108 goto fail;
1111 status = cli_posix_chown_recv(req);
1113 fail:
1114 TALLOC_FREE(frame);
1115 return status;
1118 /****************************************************************************
1119 Rename a file.
1120 ****************************************************************************/
1122 static void cli_rename_done(struct tevent_req *subreq);
1124 struct cli_rename_state {
1125 uint16_t vwv[1];
1128 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1129 struct tevent_context *ev,
1130 struct cli_state *cli,
1131 const char *fname_src,
1132 const char *fname_dst)
1134 struct tevent_req *req = NULL, *subreq = NULL;
1135 struct cli_rename_state *state = NULL;
1136 uint8_t additional_flags = 0;
1137 uint16_t additional_flags2 = 0;
1138 uint8_t *bytes = NULL;
1140 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1141 if (req == NULL) {
1142 return NULL;
1145 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1147 bytes = talloc_array(state, uint8_t, 1);
1148 if (tevent_req_nomem(bytes, req)) {
1149 return tevent_req_post(req, ev);
1151 bytes[0] = 4;
1152 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1153 strlen(fname_src)+1, NULL);
1154 if (tevent_req_nomem(bytes, req)) {
1155 return tevent_req_post(req, ev);
1158 if (clistr_is_previous_version_path(fname_src)) {
1159 additional_flags2 = FLAGS2_REPARSE_PATH;
1162 bytes = talloc_realloc(state, bytes, uint8_t,
1163 talloc_get_size(bytes)+1);
1164 if (tevent_req_nomem(bytes, req)) {
1165 return tevent_req_post(req, ev);
1168 bytes[talloc_get_size(bytes)-1] = 4;
1169 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1170 strlen(fname_dst)+1, NULL);
1171 if (tevent_req_nomem(bytes, req)) {
1172 return tevent_req_post(req, ev);
1175 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1176 additional_flags2,
1177 1, state->vwv, talloc_get_size(bytes), bytes);
1178 if (tevent_req_nomem(subreq, req)) {
1179 return tevent_req_post(req, ev);
1181 tevent_req_set_callback(subreq, cli_rename_done, req);
1182 return req;
1185 static void cli_rename_done(struct tevent_req *subreq)
1187 struct tevent_req *req = tevent_req_callback_data(
1188 subreq, struct tevent_req);
1189 NTSTATUS status;
1191 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1192 TALLOC_FREE(subreq);
1193 if (tevent_req_nterror(req, status)) {
1194 return;
1196 tevent_req_done(req);
1199 NTSTATUS cli_rename_recv(struct tevent_req *req)
1201 return tevent_req_simple_recv_ntstatus(req);
1204 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1206 TALLOC_CTX *frame = NULL;
1207 struct tevent_context *ev;
1208 struct tevent_req *req;
1209 NTSTATUS status = NT_STATUS_OK;
1211 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1212 return cli_smb2_rename(cli,
1213 fname_src,
1214 fname_dst);
1217 frame = talloc_stackframe();
1219 if (smbXcli_conn_has_async_calls(cli->conn)) {
1221 * Can't use sync call while an async call is in flight
1223 status = NT_STATUS_INVALID_PARAMETER;
1224 goto fail;
1227 ev = samba_tevent_context_init(frame);
1228 if (ev == NULL) {
1229 status = NT_STATUS_NO_MEMORY;
1230 goto fail;
1233 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1234 if (req == NULL) {
1235 status = NT_STATUS_NO_MEMORY;
1236 goto fail;
1239 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1240 goto fail;
1243 status = cli_rename_recv(req);
1245 fail:
1246 TALLOC_FREE(frame);
1247 return status;
1250 /****************************************************************************
1251 NT Rename a file.
1252 ****************************************************************************/
1254 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1256 struct cli_ntrename_internal_state {
1257 uint16_t vwv[4];
1260 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1261 struct tevent_context *ev,
1262 struct cli_state *cli,
1263 const char *fname_src,
1264 const char *fname_dst,
1265 uint16_t rename_flag)
1267 struct tevent_req *req = NULL, *subreq = NULL;
1268 struct cli_ntrename_internal_state *state = NULL;
1269 uint8_t additional_flags = 0;
1270 uint16_t additional_flags2 = 0;
1271 uint8_t *bytes = NULL;
1273 req = tevent_req_create(mem_ctx, &state,
1274 struct cli_ntrename_internal_state);
1275 if (req == NULL) {
1276 return NULL;
1279 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1280 SSVAL(state->vwv+1, 0, rename_flag);
1282 bytes = talloc_array(state, uint8_t, 1);
1283 if (tevent_req_nomem(bytes, req)) {
1284 return tevent_req_post(req, ev);
1286 bytes[0] = 4;
1287 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_src,
1288 strlen(fname_src)+1, NULL);
1289 if (tevent_req_nomem(bytes, req)) {
1290 return tevent_req_post(req, ev);
1293 if (clistr_is_previous_version_path(fname_src)) {
1294 additional_flags2 = FLAGS2_REPARSE_PATH;
1297 bytes = talloc_realloc(state, bytes, uint8_t,
1298 talloc_get_size(bytes)+1);
1299 if (tevent_req_nomem(bytes, req)) {
1300 return tevent_req_post(req, ev);
1303 bytes[talloc_get_size(bytes)-1] = 4;
1304 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname_dst,
1305 strlen(fname_dst)+1, NULL);
1306 if (tevent_req_nomem(bytes, req)) {
1307 return tevent_req_post(req, ev);
1310 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1311 additional_flags2,
1312 4, state->vwv, talloc_get_size(bytes), bytes);
1313 if (tevent_req_nomem(subreq, req)) {
1314 return tevent_req_post(req, ev);
1316 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1317 return req;
1320 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1322 struct tevent_req *req = tevent_req_callback_data(
1323 subreq, struct tevent_req);
1324 NTSTATUS status;
1326 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1327 TALLOC_FREE(subreq);
1328 if (tevent_req_nterror(req, status)) {
1329 return;
1331 tevent_req_done(req);
1334 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1336 return tevent_req_simple_recv_ntstatus(req);
1339 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1340 struct tevent_context *ev,
1341 struct cli_state *cli,
1342 const char *fname_src,
1343 const char *fname_dst)
1345 return cli_ntrename_internal_send(mem_ctx,
1347 cli,
1348 fname_src,
1349 fname_dst,
1350 RENAME_FLAG_RENAME);
1353 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1355 return cli_ntrename_internal_recv(req);
1358 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1360 TALLOC_CTX *frame = talloc_stackframe();
1361 struct tevent_context *ev;
1362 struct tevent_req *req;
1363 NTSTATUS status = NT_STATUS_OK;
1365 if (smbXcli_conn_has_async_calls(cli->conn)) {
1367 * Can't use sync call while an async call is in flight
1369 status = NT_STATUS_INVALID_PARAMETER;
1370 goto fail;
1373 ev = samba_tevent_context_init(frame);
1374 if (ev == NULL) {
1375 status = NT_STATUS_NO_MEMORY;
1376 goto fail;
1379 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1380 if (req == NULL) {
1381 status = NT_STATUS_NO_MEMORY;
1382 goto fail;
1385 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1386 goto fail;
1389 status = cli_ntrename_recv(req);
1391 fail:
1392 TALLOC_FREE(frame);
1393 return status;
1396 /****************************************************************************
1397 NT hardlink a file.
1398 ****************************************************************************/
1400 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1401 struct tevent_context *ev,
1402 struct cli_state *cli,
1403 const char *fname_src,
1404 const char *fname_dst)
1406 return cli_ntrename_internal_send(mem_ctx,
1408 cli,
1409 fname_src,
1410 fname_dst,
1411 RENAME_FLAG_HARD_LINK);
1414 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1416 return cli_ntrename_internal_recv(req);
1419 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1421 TALLOC_CTX *frame = talloc_stackframe();
1422 struct tevent_context *ev;
1423 struct tevent_req *req;
1424 NTSTATUS status = NT_STATUS_OK;
1426 if (smbXcli_conn_has_async_calls(cli->conn)) {
1428 * Can't use sync call while an async call is in flight
1430 status = NT_STATUS_INVALID_PARAMETER;
1431 goto fail;
1434 ev = samba_tevent_context_init(frame);
1435 if (ev == NULL) {
1436 status = NT_STATUS_NO_MEMORY;
1437 goto fail;
1440 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1441 if (req == NULL) {
1442 status = NT_STATUS_NO_MEMORY;
1443 goto fail;
1446 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1447 goto fail;
1450 status = cli_nt_hardlink_recv(req);
1452 fail:
1453 TALLOC_FREE(frame);
1454 return status;
1457 /****************************************************************************
1458 Delete a file.
1459 ****************************************************************************/
1461 static void cli_unlink_done(struct tevent_req *subreq);
1463 struct cli_unlink_state {
1464 uint16_t vwv[1];
1467 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1468 struct tevent_context *ev,
1469 struct cli_state *cli,
1470 const char *fname,
1471 uint16_t mayhave_attrs)
1473 struct tevent_req *req = NULL, *subreq = NULL;
1474 struct cli_unlink_state *state = NULL;
1475 uint8_t additional_flags = 0;
1476 uint16_t additional_flags2 = 0;
1477 uint8_t *bytes = NULL;
1479 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1480 if (req == NULL) {
1481 return NULL;
1484 SSVAL(state->vwv+0, 0, mayhave_attrs);
1486 bytes = talloc_array(state, uint8_t, 1);
1487 if (tevent_req_nomem(bytes, req)) {
1488 return tevent_req_post(req, ev);
1490 bytes[0] = 4;
1491 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
1492 strlen(fname)+1, NULL);
1494 if (tevent_req_nomem(bytes, req)) {
1495 return tevent_req_post(req, ev);
1498 if (clistr_is_previous_version_path(fname)) {
1499 additional_flags2 = FLAGS2_REPARSE_PATH;
1502 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1503 additional_flags2,
1504 1, state->vwv, talloc_get_size(bytes), bytes);
1505 if (tevent_req_nomem(subreq, req)) {
1506 return tevent_req_post(req, ev);
1508 tevent_req_set_callback(subreq, cli_unlink_done, req);
1509 return req;
1512 static void cli_unlink_done(struct tevent_req *subreq)
1514 struct tevent_req *req = tevent_req_callback_data(
1515 subreq, struct tevent_req);
1516 NTSTATUS status;
1518 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1519 TALLOC_FREE(subreq);
1520 if (tevent_req_nterror(req, status)) {
1521 return;
1523 tevent_req_done(req);
1526 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1528 return tevent_req_simple_recv_ntstatus(req);
1531 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1533 TALLOC_CTX *frame = NULL;
1534 struct tevent_context *ev;
1535 struct tevent_req *req;
1536 NTSTATUS status = NT_STATUS_OK;
1538 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1539 return cli_smb2_unlink(cli, fname);
1542 frame = talloc_stackframe();
1544 if (smbXcli_conn_has_async_calls(cli->conn)) {
1546 * Can't use sync call while an async call is in flight
1548 status = NT_STATUS_INVALID_PARAMETER;
1549 goto fail;
1552 ev = samba_tevent_context_init(frame);
1553 if (ev == NULL) {
1554 status = NT_STATUS_NO_MEMORY;
1555 goto fail;
1558 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1559 if (req == NULL) {
1560 status = NT_STATUS_NO_MEMORY;
1561 goto fail;
1564 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1565 goto fail;
1568 status = cli_unlink_recv(req);
1570 fail:
1571 TALLOC_FREE(frame);
1572 return status;
1575 /****************************************************************************
1576 Create a directory.
1577 ****************************************************************************/
1579 static void cli_mkdir_done(struct tevent_req *subreq);
1581 struct cli_mkdir_state {
1582 int dummy;
1585 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1586 struct tevent_context *ev,
1587 struct cli_state *cli,
1588 const char *dname)
1590 struct tevent_req *req = NULL, *subreq = NULL;
1591 struct cli_mkdir_state *state = NULL;
1592 uint8_t additional_flags = 0;
1593 uint16_t additional_flags2 = 0;
1594 uint8_t *bytes = NULL;
1596 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1597 if (req == NULL) {
1598 return NULL;
1601 bytes = talloc_array(state, uint8_t, 1);
1602 if (tevent_req_nomem(bytes, req)) {
1603 return tevent_req_post(req, ev);
1605 bytes[0] = 4;
1606 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1607 strlen(dname)+1, NULL);
1609 if (tevent_req_nomem(bytes, req)) {
1610 return tevent_req_post(req, ev);
1613 if (clistr_is_previous_version_path(dname)) {
1614 additional_flags2 = FLAGS2_REPARSE_PATH;
1617 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1618 additional_flags2,
1619 0, NULL, talloc_get_size(bytes), bytes);
1620 if (tevent_req_nomem(subreq, req)) {
1621 return tevent_req_post(req, ev);
1623 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1624 return req;
1627 static void cli_mkdir_done(struct tevent_req *subreq)
1629 struct tevent_req *req = tevent_req_callback_data(
1630 subreq, struct tevent_req);
1631 NTSTATUS status;
1633 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1634 TALLOC_FREE(subreq);
1635 if (tevent_req_nterror(req, status)) {
1636 return;
1638 tevent_req_done(req);
1641 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1643 return tevent_req_simple_recv_ntstatus(req);
1646 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1648 TALLOC_CTX *frame = NULL;
1649 struct tevent_context *ev;
1650 struct tevent_req *req;
1651 NTSTATUS status = NT_STATUS_OK;
1653 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1654 return cli_smb2_mkdir(cli, dname);
1657 frame = talloc_stackframe();
1659 if (smbXcli_conn_has_async_calls(cli->conn)) {
1661 * Can't use sync call while an async call is in flight
1663 status = NT_STATUS_INVALID_PARAMETER;
1664 goto fail;
1667 ev = samba_tevent_context_init(frame);
1668 if (ev == NULL) {
1669 status = NT_STATUS_NO_MEMORY;
1670 goto fail;
1673 req = cli_mkdir_send(frame, ev, cli, dname);
1674 if (req == NULL) {
1675 status = NT_STATUS_NO_MEMORY;
1676 goto fail;
1679 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1680 goto fail;
1683 status = cli_mkdir_recv(req);
1685 fail:
1686 TALLOC_FREE(frame);
1687 return status;
1690 /****************************************************************************
1691 Remove a directory.
1692 ****************************************************************************/
1694 static void cli_rmdir_done(struct tevent_req *subreq);
1696 struct cli_rmdir_state {
1697 int dummy;
1700 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1701 struct tevent_context *ev,
1702 struct cli_state *cli,
1703 const char *dname)
1705 struct tevent_req *req = NULL, *subreq = NULL;
1706 struct cli_rmdir_state *state = NULL;
1707 uint8_t additional_flags = 0;
1708 uint8_t *bytes = NULL;
1710 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1711 if (req == NULL) {
1712 return NULL;
1715 bytes = talloc_array(state, uint8_t, 1);
1716 if (tevent_req_nomem(bytes, req)) {
1717 return tevent_req_post(req, ev);
1719 bytes[0] = 4;
1720 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), dname,
1721 strlen(dname)+1, NULL);
1723 if (tevent_req_nomem(bytes, req)) {
1724 return tevent_req_post(req, ev);
1727 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags, 0,
1728 0, NULL, talloc_get_size(bytes), bytes);
1729 if (tevent_req_nomem(subreq, req)) {
1730 return tevent_req_post(req, ev);
1732 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1733 return req;
1736 static void cli_rmdir_done(struct tevent_req *subreq)
1738 struct tevent_req *req = tevent_req_callback_data(
1739 subreq, struct tevent_req);
1740 NTSTATUS status;
1742 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1743 TALLOC_FREE(subreq);
1744 if (tevent_req_nterror(req, status)) {
1745 return;
1747 tevent_req_done(req);
1750 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1752 return tevent_req_simple_recv_ntstatus(req);
1755 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1757 TALLOC_CTX *frame = NULL;
1758 struct tevent_context *ev;
1759 struct tevent_req *req;
1760 NTSTATUS status = NT_STATUS_OK;
1762 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1763 return cli_smb2_rmdir(cli, dname);
1766 frame = talloc_stackframe();
1768 if (smbXcli_conn_has_async_calls(cli->conn)) {
1770 * Can't use sync call while an async call is in flight
1772 status = NT_STATUS_INVALID_PARAMETER;
1773 goto fail;
1776 ev = samba_tevent_context_init(frame);
1777 if (ev == NULL) {
1778 status = NT_STATUS_NO_MEMORY;
1779 goto fail;
1782 req = cli_rmdir_send(frame, ev, cli, dname);
1783 if (req == NULL) {
1784 status = NT_STATUS_NO_MEMORY;
1785 goto fail;
1788 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1789 goto fail;
1792 status = cli_rmdir_recv(req);
1794 fail:
1795 TALLOC_FREE(frame);
1796 return status;
1799 /****************************************************************************
1800 Set or clear the delete on close flag.
1801 ****************************************************************************/
1803 struct doc_state {
1804 uint16_t setup;
1805 uint8_t param[6];
1806 uint8_t data[1];
1809 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1811 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1812 NULL, 0, NULL, NULL, 0, NULL);
1813 tevent_req_simple_finish_ntstatus(subreq, status);
1816 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1817 struct tevent_context *ev,
1818 struct cli_state *cli,
1819 uint16_t fnum,
1820 bool flag)
1822 struct tevent_req *req = NULL, *subreq = NULL;
1823 struct doc_state *state = NULL;
1825 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1826 if (req == NULL) {
1827 return NULL;
1830 /* Setup setup word. */
1831 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1833 /* Setup param array. */
1834 SSVAL(state->param,0,fnum);
1835 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1837 /* Setup data array. */
1838 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1840 subreq = cli_trans_send(state, /* mem ctx. */
1841 ev, /* event ctx. */
1842 cli, /* cli_state. */
1843 0, /* additional_flags2 */
1844 SMBtrans2, /* cmd. */
1845 NULL, /* pipe name. */
1846 -1, /* fid. */
1847 0, /* function. */
1848 0, /* flags. */
1849 &state->setup, /* setup. */
1850 1, /* num setup uint16_t words. */
1851 0, /* max returned setup. */
1852 state->param, /* param. */
1853 6, /* num param. */
1854 2, /* max returned param. */
1855 state->data, /* data. */
1856 1, /* num data. */
1857 0); /* max returned data. */
1859 if (tevent_req_nomem(subreq, req)) {
1860 return tevent_req_post(req, ev);
1862 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1863 return req;
1866 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1868 return tevent_req_simple_recv_ntstatus(req);
1871 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1873 TALLOC_CTX *frame = talloc_stackframe();
1874 struct tevent_context *ev = NULL;
1875 struct tevent_req *req = NULL;
1876 NTSTATUS status = NT_STATUS_OK;
1878 if (smbXcli_conn_has_async_calls(cli->conn)) {
1880 * Can't use sync call while an async call is in flight
1882 status = NT_STATUS_INVALID_PARAMETER;
1883 goto fail;
1886 ev = samba_tevent_context_init(frame);
1887 if (ev == NULL) {
1888 status = NT_STATUS_NO_MEMORY;
1889 goto fail;
1892 req = cli_nt_delete_on_close_send(frame,
1894 cli,
1895 fnum,
1896 flag);
1897 if (req == NULL) {
1898 status = NT_STATUS_NO_MEMORY;
1899 goto fail;
1902 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
1903 goto fail;
1906 status = cli_nt_delete_on_close_recv(req);
1908 fail:
1909 TALLOC_FREE(frame);
1910 return status;
1913 struct cli_ntcreate1_state {
1914 uint16_t vwv[24];
1915 uint16_t fnum;
1916 struct smb_create_returns cr;
1917 struct tevent_req *subreq;
1920 static void cli_ntcreate1_done(struct tevent_req *subreq);
1921 static bool cli_ntcreate1_cancel(struct tevent_req *req);
1923 static struct tevent_req *cli_ntcreate1_send(TALLOC_CTX *mem_ctx,
1924 struct tevent_context *ev,
1925 struct cli_state *cli,
1926 const char *fname,
1927 uint32_t CreatFlags,
1928 uint32_t DesiredAccess,
1929 uint32_t FileAttributes,
1930 uint32_t ShareAccess,
1931 uint32_t CreateDisposition,
1932 uint32_t CreateOptions,
1933 uint8_t SecurityFlags)
1935 struct tevent_req *req, *subreq;
1936 struct cli_ntcreate1_state *state;
1937 uint16_t *vwv;
1938 uint8_t *bytes;
1939 size_t converted_len;
1941 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate1_state);
1942 if (req == NULL) {
1943 return NULL;
1946 vwv = state->vwv;
1948 SCVAL(vwv+0, 0, 0xFF);
1949 SCVAL(vwv+0, 1, 0);
1950 SSVAL(vwv+1, 0, 0);
1951 SCVAL(vwv+2, 0, 0);
1953 if (cli->use_oplocks) {
1954 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1956 SIVAL(vwv+3, 1, CreatFlags);
1957 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1958 SIVAL(vwv+7, 1, DesiredAccess);
1959 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1960 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1961 SIVAL(vwv+13, 1, FileAttributes);
1962 SIVAL(vwv+15, 1, ShareAccess);
1963 SIVAL(vwv+17, 1, CreateDisposition);
1964 SIVAL(vwv+19, 1, CreateOptions |
1965 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1966 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1967 SCVAL(vwv+23, 1, SecurityFlags);
1969 bytes = talloc_array(state, uint8_t, 0);
1970 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn),
1971 fname, strlen(fname)+1,
1972 &converted_len);
1974 /* sigh. this copes with broken netapp filer behaviour */
1975 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "", 1, NULL);
1977 if (tevent_req_nomem(bytes, req)) {
1978 return tevent_req_post(req, ev);
1981 SSVAL(vwv+2, 1, converted_len);
1983 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 0, 24, vwv,
1984 talloc_get_size(bytes), bytes);
1985 if (tevent_req_nomem(subreq, req)) {
1986 return tevent_req_post(req, ev);
1988 tevent_req_set_callback(subreq, cli_ntcreate1_done, req);
1990 state->subreq = subreq;
1991 tevent_req_set_cancel_fn(req, cli_ntcreate1_cancel);
1993 return req;
1996 static void cli_ntcreate1_done(struct tevent_req *subreq)
1998 struct tevent_req *req = tevent_req_callback_data(
1999 subreq, struct tevent_req);
2000 struct cli_ntcreate1_state *state = tevent_req_data(
2001 req, struct cli_ntcreate1_state);
2002 uint8_t wct;
2003 uint16_t *vwv;
2004 uint32_t num_bytes;
2005 uint8_t *bytes;
2006 NTSTATUS status;
2008 status = cli_smb_recv(subreq, state, NULL, 34, &wct, &vwv,
2009 &num_bytes, &bytes);
2010 TALLOC_FREE(subreq);
2011 if (tevent_req_nterror(req, status)) {
2012 return;
2014 state->cr.oplock_level = CVAL(vwv+2, 0);
2015 state->fnum = SVAL(vwv+2, 1);
2016 state->cr.create_action = IVAL(vwv+3, 1);
2017 state->cr.creation_time = BVAL(vwv+5, 1);
2018 state->cr.last_access_time = BVAL(vwv+9, 1);
2019 state->cr.last_write_time = BVAL(vwv+13, 1);
2020 state->cr.change_time = BVAL(vwv+17, 1);
2021 state->cr.file_attributes = IVAL(vwv+21, 1);
2022 state->cr.allocation_size = BVAL(vwv+23, 1);
2023 state->cr.end_of_file = BVAL(vwv+27, 1);
2025 tevent_req_done(req);
2028 static bool cli_ntcreate1_cancel(struct tevent_req *req)
2030 struct cli_ntcreate1_state *state = tevent_req_data(
2031 req, struct cli_ntcreate1_state);
2032 return tevent_req_cancel(state->subreq);
2035 static NTSTATUS cli_ntcreate1_recv(struct tevent_req *req,
2036 uint16_t *pfnum,
2037 struct smb_create_returns *cr)
2039 struct cli_ntcreate1_state *state = tevent_req_data(
2040 req, struct cli_ntcreate1_state);
2041 NTSTATUS status;
2043 if (tevent_req_is_nterror(req, &status)) {
2044 return status;
2046 *pfnum = state->fnum;
2047 if (cr != NULL) {
2048 *cr = state->cr;
2050 return NT_STATUS_OK;
2053 struct cli_ntcreate_state {
2054 NTSTATUS (*recv)(struct tevent_req *req, uint16_t *fnum,
2055 struct smb_create_returns *cr);
2056 struct smb_create_returns cr;
2057 uint16_t fnum;
2058 struct tevent_req *subreq;
2061 static void cli_ntcreate_done(struct tevent_req *subreq);
2062 static bool cli_ntcreate_cancel(struct tevent_req *req);
2064 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
2065 struct tevent_context *ev,
2066 struct cli_state *cli,
2067 const char *fname,
2068 uint32_t create_flags,
2069 uint32_t desired_access,
2070 uint32_t file_attributes,
2071 uint32_t share_access,
2072 uint32_t create_disposition,
2073 uint32_t create_options,
2074 uint8_t security_flags)
2076 struct tevent_req *req, *subreq;
2077 struct cli_ntcreate_state *state;
2079 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2080 if (req == NULL) {
2081 return NULL;
2084 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2085 state->recv = cli_smb2_create_fnum_recv;
2087 if (cli->use_oplocks) {
2088 create_flags |= REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK;
2091 subreq = cli_smb2_create_fnum_send(
2092 state, ev, cli, fname, create_flags, desired_access,
2093 file_attributes, share_access, create_disposition,
2094 create_options);
2095 } else {
2096 state->recv = cli_ntcreate1_recv;
2097 subreq = cli_ntcreate1_send(
2098 state, ev, cli, fname, create_flags, desired_access,
2099 file_attributes, share_access, create_disposition,
2100 create_options, security_flags);
2102 if (tevent_req_nomem(subreq, req)) {
2103 return tevent_req_post(req, ev);
2105 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
2107 state->subreq = subreq;
2108 tevent_req_set_cancel_fn(req, cli_ntcreate_cancel);
2110 return req;
2113 static void cli_ntcreate_done(struct tevent_req *subreq)
2115 struct tevent_req *req = tevent_req_callback_data(
2116 subreq, struct tevent_req);
2117 struct cli_ntcreate_state *state = tevent_req_data(
2118 req, struct cli_ntcreate_state);
2119 NTSTATUS status;
2121 status = state->recv(subreq, &state->fnum, &state->cr);
2122 TALLOC_FREE(subreq);
2123 if (tevent_req_nterror(req, status)) {
2124 return;
2126 tevent_req_done(req);
2129 static bool cli_ntcreate_cancel(struct tevent_req *req)
2131 struct cli_ntcreate_state *state = tevent_req_data(
2132 req, struct cli_ntcreate_state);
2133 return tevent_req_cancel(state->subreq);
2136 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *fnum,
2137 struct smb_create_returns *cr)
2139 struct cli_ntcreate_state *state = tevent_req_data(
2140 req, struct cli_ntcreate_state);
2141 NTSTATUS status;
2143 if (tevent_req_is_nterror(req, &status)) {
2144 return status;
2146 if (fnum != NULL) {
2147 *fnum = state->fnum;
2149 if (cr != NULL) {
2150 *cr = state->cr;
2152 return NT_STATUS_OK;
2155 NTSTATUS cli_ntcreate(struct cli_state *cli,
2156 const char *fname,
2157 uint32_t CreatFlags,
2158 uint32_t DesiredAccess,
2159 uint32_t FileAttributes,
2160 uint32_t ShareAccess,
2161 uint32_t CreateDisposition,
2162 uint32_t CreateOptions,
2163 uint8_t SecurityFlags,
2164 uint16_t *pfid,
2165 struct smb_create_returns *cr)
2167 TALLOC_CTX *frame = talloc_stackframe();
2168 struct tevent_context *ev;
2169 struct tevent_req *req;
2170 NTSTATUS status = NT_STATUS_NO_MEMORY;
2172 if (smbXcli_conn_has_async_calls(cli->conn)) {
2174 * Can't use sync call while an async call is in flight
2176 status = NT_STATUS_INVALID_PARAMETER;
2177 goto fail;
2180 ev = samba_tevent_context_init(frame);
2181 if (ev == NULL) {
2182 goto fail;
2185 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2186 DesiredAccess, FileAttributes, ShareAccess,
2187 CreateDisposition, CreateOptions,
2188 SecurityFlags);
2189 if (req == NULL) {
2190 goto fail;
2193 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2194 goto fail;
2197 status = cli_ntcreate_recv(req, pfid, cr);
2198 fail:
2199 TALLOC_FREE(frame);
2200 return status;
2203 struct cli_nttrans_create_state {
2204 uint16_t fnum;
2205 struct smb_create_returns cr;
2208 static void cli_nttrans_create_done(struct tevent_req *subreq);
2210 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
2211 struct tevent_context *ev,
2212 struct cli_state *cli,
2213 const char *fname,
2214 uint32_t CreatFlags,
2215 uint32_t DesiredAccess,
2216 uint32_t FileAttributes,
2217 uint32_t ShareAccess,
2218 uint32_t CreateDisposition,
2219 uint32_t CreateOptions,
2220 uint8_t SecurityFlags,
2221 struct security_descriptor *secdesc,
2222 struct ea_struct *eas,
2223 int num_eas)
2225 struct tevent_req *req, *subreq;
2226 struct cli_nttrans_create_state *state;
2227 uint8_t *param;
2228 uint8_t *secdesc_buf;
2229 size_t secdesc_len;
2230 NTSTATUS status;
2231 size_t converted_len;
2233 req = tevent_req_create(mem_ctx,
2234 &state, struct cli_nttrans_create_state);
2235 if (req == NULL) {
2236 return NULL;
2239 if (secdesc != NULL) {
2240 status = marshall_sec_desc(talloc_tos(), secdesc,
2241 &secdesc_buf, &secdesc_len);
2242 if (tevent_req_nterror(req, status)) {
2243 DEBUG(10, ("marshall_sec_desc failed: %s\n",
2244 nt_errstr(status)));
2245 return tevent_req_post(req, ev);
2247 } else {
2248 secdesc_buf = NULL;
2249 secdesc_len = 0;
2252 if (num_eas != 0) {
2254 * TODO ;-)
2256 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
2257 return tevent_req_post(req, ev);
2260 param = talloc_array(state, uint8_t, 53);
2261 if (tevent_req_nomem(param, req)) {
2262 return tevent_req_post(req, ev);
2265 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
2266 fname, strlen(fname),
2267 &converted_len);
2268 if (tevent_req_nomem(param, req)) {
2269 return tevent_req_post(req, ev);
2272 SIVAL(param, 0, CreatFlags);
2273 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2274 SIVAL(param, 8, DesiredAccess);
2275 SIVAL(param, 12, 0x0); /* AllocationSize */
2276 SIVAL(param, 16, 0x0); /* AllocationSize */
2277 SIVAL(param, 20, FileAttributes);
2278 SIVAL(param, 24, ShareAccess);
2279 SIVAL(param, 28, CreateDisposition);
2280 SIVAL(param, 32, CreateOptions |
2281 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2282 SIVAL(param, 36, secdesc_len);
2283 SIVAL(param, 40, 0); /* EA length*/
2284 SIVAL(param, 44, converted_len);
2285 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2286 SCVAL(param, 52, SecurityFlags);
2288 subreq = cli_trans_send(state, ev, cli,
2289 0, /* additional_flags2 */
2290 SMBnttrans,
2291 NULL, -1, /* name, fid */
2292 NT_TRANSACT_CREATE, 0,
2293 NULL, 0, 0, /* setup */
2294 param, talloc_get_size(param), 128, /* param */
2295 secdesc_buf, secdesc_len, 0); /* data */
2296 if (tevent_req_nomem(subreq, req)) {
2297 return tevent_req_post(req, ev);
2299 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2300 return req;
2303 static void cli_nttrans_create_done(struct tevent_req *subreq)
2305 struct tevent_req *req = tevent_req_callback_data(
2306 subreq, struct tevent_req);
2307 struct cli_nttrans_create_state *state = tevent_req_data(
2308 req, struct cli_nttrans_create_state);
2309 uint8_t *param;
2310 uint32_t num_param;
2311 NTSTATUS status;
2313 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2314 NULL, 0, NULL, /* rsetup */
2315 &param, 69, &num_param,
2316 NULL, 0, NULL);
2317 if (tevent_req_nterror(req, status)) {
2318 return;
2320 state->cr.oplock_level = CVAL(param, 0);
2321 state->fnum = SVAL(param, 2);
2322 state->cr.create_action = IVAL(param, 4);
2323 state->cr.creation_time = BVAL(param, 12);
2324 state->cr.last_access_time = BVAL(param, 20);
2325 state->cr.last_write_time = BVAL(param, 28);
2326 state->cr.change_time = BVAL(param, 36);
2327 state->cr.file_attributes = IVAL(param, 44);
2328 state->cr.allocation_size = BVAL(param, 48);
2329 state->cr.end_of_file = BVAL(param, 56);
2331 TALLOC_FREE(param);
2332 tevent_req_done(req);
2335 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req,
2336 uint16_t *fnum,
2337 struct smb_create_returns *cr)
2339 struct cli_nttrans_create_state *state = tevent_req_data(
2340 req, struct cli_nttrans_create_state);
2341 NTSTATUS status;
2343 if (tevent_req_is_nterror(req, &status)) {
2344 return status;
2346 *fnum = state->fnum;
2347 if (cr != NULL) {
2348 *cr = state->cr;
2350 return NT_STATUS_OK;
2353 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2354 const char *fname,
2355 uint32_t CreatFlags,
2356 uint32_t DesiredAccess,
2357 uint32_t FileAttributes,
2358 uint32_t ShareAccess,
2359 uint32_t CreateDisposition,
2360 uint32_t CreateOptions,
2361 uint8_t SecurityFlags,
2362 struct security_descriptor *secdesc,
2363 struct ea_struct *eas,
2364 int num_eas,
2365 uint16_t *pfid,
2366 struct smb_create_returns *cr)
2368 TALLOC_CTX *frame = talloc_stackframe();
2369 struct tevent_context *ev;
2370 struct tevent_req *req;
2371 NTSTATUS status = NT_STATUS_NO_MEMORY;
2373 if (smbXcli_conn_has_async_calls(cli->conn)) {
2375 * Can't use sync call while an async call is in flight
2377 status = NT_STATUS_INVALID_PARAMETER;
2378 goto fail;
2380 ev = samba_tevent_context_init(frame);
2381 if (ev == NULL) {
2382 goto fail;
2384 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2385 DesiredAccess, FileAttributes,
2386 ShareAccess, CreateDisposition,
2387 CreateOptions, SecurityFlags,
2388 secdesc, eas, num_eas);
2389 if (req == NULL) {
2390 goto fail;
2392 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2393 goto fail;
2395 status = cli_nttrans_create_recv(req, pfid, cr);
2396 fail:
2397 TALLOC_FREE(frame);
2398 return status;
2401 /****************************************************************************
2402 Open a file
2403 WARNING: if you open with O_WRONLY then getattrE won't work!
2404 ****************************************************************************/
2406 struct cli_openx_state {
2407 const char *fname;
2408 uint16_t vwv[15];
2409 uint16_t fnum;
2410 struct iovec bytes;
2413 static void cli_openx_done(struct tevent_req *subreq);
2415 struct tevent_req *cli_openx_create(TALLOC_CTX *mem_ctx,
2416 struct tevent_context *ev,
2417 struct cli_state *cli, const char *fname,
2418 int flags, int share_mode,
2419 struct tevent_req **psmbreq)
2421 struct tevent_req *req, *subreq;
2422 struct cli_openx_state *state;
2423 unsigned openfn;
2424 unsigned accessmode;
2425 uint8_t additional_flags;
2426 uint8_t *bytes;
2428 req = tevent_req_create(mem_ctx, &state, struct cli_openx_state);
2429 if (req == NULL) {
2430 return NULL;
2433 openfn = 0;
2434 if (flags & O_CREAT) {
2435 openfn |= (1<<4);
2437 if (!(flags & O_EXCL)) {
2438 if (flags & O_TRUNC)
2439 openfn |= (1<<1);
2440 else
2441 openfn |= (1<<0);
2444 accessmode = (share_mode<<4);
2446 if ((flags & O_ACCMODE) == O_RDWR) {
2447 accessmode |= 2;
2448 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2449 accessmode |= 1;
2452 #if defined(O_SYNC)
2453 if ((flags & O_SYNC) == O_SYNC) {
2454 accessmode |= (1<<14);
2456 #endif /* O_SYNC */
2458 if (share_mode == DENY_FCB) {
2459 accessmode = 0xFF;
2462 SCVAL(state->vwv + 0, 0, 0xFF);
2463 SCVAL(state->vwv + 0, 1, 0);
2464 SSVAL(state->vwv + 1, 0, 0);
2465 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2466 SSVAL(state->vwv + 3, 0, accessmode);
2467 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2468 SSVAL(state->vwv + 5, 0, 0);
2469 SIVAL(state->vwv + 6, 0, 0);
2470 SSVAL(state->vwv + 8, 0, openfn);
2471 SIVAL(state->vwv + 9, 0, 0);
2472 SIVAL(state->vwv + 11, 0, 0);
2473 SIVAL(state->vwv + 13, 0, 0);
2475 additional_flags = 0;
2477 if (cli->use_oplocks) {
2478 /* if using oplocks then ask for a batch oplock via
2479 core and extended methods */
2480 additional_flags =
2481 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2482 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2485 bytes = talloc_array(state, uint8_t, 0);
2486 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
2487 strlen(fname)+1, NULL);
2489 if (tevent_req_nomem(bytes, req)) {
2490 return tevent_req_post(req, ev);
2493 state->bytes.iov_base = (void *)bytes;
2494 state->bytes.iov_len = talloc_get_size(bytes);
2496 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2497 0, 15, state->vwv, 1, &state->bytes);
2498 if (subreq == NULL) {
2499 TALLOC_FREE(req);
2500 return NULL;
2502 tevent_req_set_callback(subreq, cli_openx_done, req);
2503 *psmbreq = subreq;
2504 return req;
2507 struct tevent_req *cli_openx_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2508 struct cli_state *cli, const char *fname,
2509 int flags, int share_mode)
2511 struct tevent_req *req, *subreq;
2512 NTSTATUS status;
2514 req = cli_openx_create(mem_ctx, ev, cli, fname, flags, share_mode,
2515 &subreq);
2516 if (req == NULL) {
2517 return NULL;
2520 status = smb1cli_req_chain_submit(&subreq, 1);
2521 if (tevent_req_nterror(req, status)) {
2522 return tevent_req_post(req, ev);
2524 return req;
2527 static void cli_openx_done(struct tevent_req *subreq)
2529 struct tevent_req *req = tevent_req_callback_data(
2530 subreq, struct tevent_req);
2531 struct cli_openx_state *state = tevent_req_data(
2532 req, struct cli_openx_state);
2533 uint8_t wct;
2534 uint16_t *vwv;
2535 NTSTATUS status;
2537 status = cli_smb_recv(subreq, state, NULL, 3, &wct, &vwv, NULL,
2538 NULL);
2539 TALLOC_FREE(subreq);
2540 if (tevent_req_nterror(req, status)) {
2541 return;
2543 state->fnum = SVAL(vwv+2, 0);
2544 tevent_req_done(req);
2547 NTSTATUS cli_openx_recv(struct tevent_req *req, uint16_t *pfnum)
2549 struct cli_openx_state *state = tevent_req_data(
2550 req, struct cli_openx_state);
2551 NTSTATUS status;
2553 if (tevent_req_is_nterror(req, &status)) {
2554 return status;
2556 *pfnum = state->fnum;
2557 return NT_STATUS_OK;
2560 NTSTATUS cli_openx(struct cli_state *cli, const char *fname, int flags,
2561 int share_mode, uint16_t *pfnum)
2563 TALLOC_CTX *frame = talloc_stackframe();
2564 struct tevent_context *ev;
2565 struct tevent_req *req;
2566 NTSTATUS status = NT_STATUS_NO_MEMORY;
2568 if (smbXcli_conn_has_async_calls(cli->conn)) {
2570 * Can't use sync call while an async call is in flight
2572 status = NT_STATUS_INVALID_PARAMETER;
2573 goto fail;
2576 ev = samba_tevent_context_init(frame);
2577 if (ev == NULL) {
2578 goto fail;
2581 req = cli_openx_send(frame, ev, cli, fname, flags, share_mode);
2582 if (req == NULL) {
2583 goto fail;
2586 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2587 goto fail;
2590 status = cli_openx_recv(req, pfnum);
2591 fail:
2592 TALLOC_FREE(frame);
2593 return status;
2595 /****************************************************************************
2596 Synchronous wrapper function that does an NtCreateX open by preference
2597 and falls back to openX if this fails.
2598 ****************************************************************************/
2600 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2601 int share_mode_in, uint16_t *pfnum)
2603 NTSTATUS status;
2604 unsigned int openfn = 0;
2605 unsigned int dos_deny = 0;
2606 uint32_t access_mask, share_mode, create_disposition, create_options;
2607 struct smb_create_returns cr;
2609 /* Do the initial mapping into OpenX parameters. */
2610 if (flags & O_CREAT) {
2611 openfn |= (1<<4);
2613 if (!(flags & O_EXCL)) {
2614 if (flags & O_TRUNC)
2615 openfn |= (1<<1);
2616 else
2617 openfn |= (1<<0);
2620 dos_deny = (share_mode_in<<4);
2622 if ((flags & O_ACCMODE) == O_RDWR) {
2623 dos_deny |= 2;
2624 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2625 dos_deny |= 1;
2628 #if defined(O_SYNC)
2629 if ((flags & O_SYNC) == O_SYNC) {
2630 dos_deny |= (1<<14);
2632 #endif /* O_SYNC */
2634 if (share_mode_in == DENY_FCB) {
2635 dos_deny = 0xFF;
2638 #if 0
2639 /* Hmmm. This is what I think the above code
2640 should look like if it's using the constants
2641 we #define. JRA. */
2643 if (flags & O_CREAT) {
2644 openfn |= OPENX_FILE_CREATE_IF_NOT_EXIST;
2646 if (!(flags & O_EXCL)) {
2647 if (flags & O_TRUNC)
2648 openfn |= OPENX_FILE_EXISTS_TRUNCATE;
2649 else
2650 openfn |= OPENX_FILE_EXISTS_OPEN;
2653 dos_deny = SET_DENY_MODE(share_mode_in);
2655 if ((flags & O_ACCMODE) == O_RDWR) {
2656 dos_deny |= DOS_OPEN_RDWR;
2657 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2658 dos_deny |= DOS_OPEN_WRONLY;
2661 #if defined(O_SYNC)
2662 if ((flags & O_SYNC) == O_SYNC) {
2663 dos_deny |= FILE_SYNC_OPENMODE;
2665 #endif /* O_SYNC */
2667 if (share_mode_in == DENY_FCB) {
2668 dos_deny = 0xFF;
2670 #endif
2672 if (!map_open_params_to_ntcreate(fname, dos_deny,
2673 openfn, &access_mask,
2674 &share_mode, &create_disposition,
2675 &create_options, NULL)) {
2676 goto try_openx;
2679 status = cli_ntcreate(cli,
2680 fname,
2682 access_mask,
2684 share_mode,
2685 create_disposition,
2686 create_options,
2688 pfnum,
2689 &cr);
2691 /* Try and cope will all varients of "we don't do this call"
2692 and fall back to openX. */
2694 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
2695 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
2696 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
2697 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
2698 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
2699 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
2700 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
2701 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
2702 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
2703 goto try_openx;
2706 if (NT_STATUS_IS_OK(status) &&
2707 (create_options & FILE_NON_DIRECTORY_FILE) &&
2708 (cr.file_attributes & FILE_ATTRIBUTE_DIRECTORY))
2711 * Some (broken) servers return a valid handle
2712 * for directories even if FILE_NON_DIRECTORY_FILE
2713 * is set. Just close the handle and set the
2714 * error explicitly to NT_STATUS_FILE_IS_A_DIRECTORY.
2716 status = cli_close(cli, *pfnum);
2717 if (!NT_STATUS_IS_OK(status)) {
2718 return status;
2720 status = NT_STATUS_FILE_IS_A_DIRECTORY;
2721 /* Set this so libsmbclient can retrieve it. */
2722 cli->raw_status = status;
2725 return status;
2727 try_openx:
2729 return cli_openx(cli, fname, flags, share_mode_in, pfnum);
2732 /****************************************************************************
2733 Close a file.
2734 ****************************************************************************/
2736 struct cli_close_state {
2737 uint16_t vwv[3];
2740 static void cli_close_done(struct tevent_req *subreq);
2742 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2743 struct tevent_context *ev,
2744 struct cli_state *cli,
2745 uint16_t fnum,
2746 struct tevent_req **psubreq)
2748 struct tevent_req *req, *subreq;
2749 struct cli_close_state *state;
2751 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2752 if (req == NULL) {
2753 return NULL;
2756 SSVAL(state->vwv+0, 0, fnum);
2757 SIVALS(state->vwv+1, 0, -1);
2759 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 0,
2760 3, state->vwv, 0, NULL);
2761 if (subreq == NULL) {
2762 TALLOC_FREE(req);
2763 return NULL;
2765 tevent_req_set_callback(subreq, cli_close_done, req);
2766 *psubreq = subreq;
2767 return req;
2770 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2771 struct tevent_context *ev,
2772 struct cli_state *cli,
2773 uint16_t fnum)
2775 struct tevent_req *req, *subreq;
2776 NTSTATUS status;
2778 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2779 if (req == NULL) {
2780 return NULL;
2783 status = smb1cli_req_chain_submit(&subreq, 1);
2784 if (tevent_req_nterror(req, status)) {
2785 return tevent_req_post(req, ev);
2787 return req;
2790 static void cli_close_done(struct tevent_req *subreq)
2792 struct tevent_req *req = tevent_req_callback_data(
2793 subreq, struct tevent_req);
2794 NTSTATUS status;
2796 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2797 TALLOC_FREE(subreq);
2798 if (tevent_req_nterror(req, status)) {
2799 return;
2801 tevent_req_done(req);
2804 NTSTATUS cli_close_recv(struct tevent_req *req)
2806 return tevent_req_simple_recv_ntstatus(req);
2809 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2811 TALLOC_CTX *frame = NULL;
2812 struct tevent_context *ev;
2813 struct tevent_req *req;
2814 NTSTATUS status = NT_STATUS_OK;
2816 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
2817 return cli_smb2_close_fnum(cli, fnum);
2820 frame = talloc_stackframe();
2822 if (smbXcli_conn_has_async_calls(cli->conn)) {
2824 * Can't use sync call while an async call is in flight
2826 status = NT_STATUS_INVALID_PARAMETER;
2827 goto fail;
2830 ev = samba_tevent_context_init(frame);
2831 if (ev == NULL) {
2832 status = NT_STATUS_NO_MEMORY;
2833 goto fail;
2836 req = cli_close_send(frame, ev, cli, fnum);
2837 if (req == NULL) {
2838 status = NT_STATUS_NO_MEMORY;
2839 goto fail;
2842 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2843 goto fail;
2846 status = cli_close_recv(req);
2847 fail:
2848 TALLOC_FREE(frame);
2849 return status;
2852 /****************************************************************************
2853 Truncate a file to a specified size
2854 ****************************************************************************/
2856 struct ftrunc_state {
2857 uint16_t setup;
2858 uint8_t param[6];
2859 uint8_t data[8];
2862 static void cli_ftruncate_done(struct tevent_req *subreq)
2864 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2865 NULL, 0, NULL, NULL, 0, NULL);
2866 tevent_req_simple_finish_ntstatus(subreq, status);
2869 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2870 struct tevent_context *ev,
2871 struct cli_state *cli,
2872 uint16_t fnum,
2873 uint64_t size)
2875 struct tevent_req *req = NULL, *subreq = NULL;
2876 struct ftrunc_state *state = NULL;
2878 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2879 if (req == NULL) {
2880 return NULL;
2883 /* Setup setup word. */
2884 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2886 /* Setup param array. */
2887 SSVAL(state->param,0,fnum);
2888 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2889 SSVAL(state->param,4,0);
2891 /* Setup data array. */
2892 SBVAL(state->data, 0, size);
2894 subreq = cli_trans_send(state, /* mem ctx. */
2895 ev, /* event ctx. */
2896 cli, /* cli_state. */
2897 0, /* additional_flags2 */
2898 SMBtrans2, /* cmd. */
2899 NULL, /* pipe name. */
2900 -1, /* fid. */
2901 0, /* function. */
2902 0, /* flags. */
2903 &state->setup, /* setup. */
2904 1, /* num setup uint16_t words. */
2905 0, /* max returned setup. */
2906 state->param, /* param. */
2907 6, /* num param. */
2908 2, /* max returned param. */
2909 state->data, /* data. */
2910 8, /* num data. */
2911 0); /* max returned data. */
2913 if (tevent_req_nomem(subreq, req)) {
2914 return tevent_req_post(req, ev);
2916 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2917 return req;
2920 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2922 return tevent_req_simple_recv_ntstatus(req);
2925 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2927 TALLOC_CTX *frame = talloc_stackframe();
2928 struct tevent_context *ev = NULL;
2929 struct tevent_req *req = NULL;
2930 NTSTATUS status = NT_STATUS_OK;
2932 if (smbXcli_conn_has_async_calls(cli->conn)) {
2934 * Can't use sync call while an async call is in flight
2936 status = NT_STATUS_INVALID_PARAMETER;
2937 goto fail;
2940 ev = samba_tevent_context_init(frame);
2941 if (ev == NULL) {
2942 status = NT_STATUS_NO_MEMORY;
2943 goto fail;
2946 req = cli_ftruncate_send(frame,
2948 cli,
2949 fnum,
2950 size);
2951 if (req == NULL) {
2952 status = NT_STATUS_NO_MEMORY;
2953 goto fail;
2956 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2957 goto fail;
2960 status = cli_ftruncate_recv(req);
2962 fail:
2963 TALLOC_FREE(frame);
2964 return status;
2967 /****************************************************************************
2968 send a lock with a specified locktype
2969 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2970 ****************************************************************************/
2972 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2973 uint32_t offset, uint32_t len,
2974 int timeout, unsigned char locktype)
2976 uint16_t vwv[8];
2977 uint8_t bytes[10];
2978 NTSTATUS status;
2979 unsigned int set_timeout = 0;
2980 unsigned int saved_timeout = 0;
2982 SCVAL(vwv + 0, 0, 0xff);
2983 SCVAL(vwv + 0, 1, 0);
2984 SSVAL(vwv + 1, 0, 0);
2985 SSVAL(vwv + 2, 0, fnum);
2986 SCVAL(vwv + 3, 0, locktype);
2987 SCVAL(vwv + 3, 1, 0);
2988 SIVALS(vwv + 4, 0, timeout);
2989 SSVAL(vwv + 6, 0, 0);
2990 SSVAL(vwv + 7, 0, 1);
2992 SSVAL(bytes, 0, cli_getpid(cli));
2993 SIVAL(bytes, 2, offset);
2994 SIVAL(bytes, 6, len);
2996 if (timeout != 0) {
2997 if (timeout == -1) {
2998 set_timeout = 0x7FFFFFFF;
2999 } else {
3000 set_timeout = timeout + 2*1000;
3002 saved_timeout = cli_set_timeout(cli, set_timeout);
3005 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3006 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3008 if (saved_timeout != 0) {
3009 cli_set_timeout(cli, saved_timeout);
3012 return status;
3015 /****************************************************************************
3016 Lock a file.
3017 note that timeout is in units of 2 milliseconds
3018 ****************************************************************************/
3020 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
3021 uint32_t offset, uint32_t len, int timeout,
3022 enum brl_type lock_type)
3024 NTSTATUS status;
3026 status = cli_locktype(cli, fnum, offset, len, timeout,
3027 (lock_type == READ_LOCK? 1 : 0));
3028 return status;
3031 /****************************************************************************
3032 Unlock a file.
3033 ****************************************************************************/
3035 struct cli_unlock_state {
3036 uint16_t vwv[8];
3037 uint8_t data[10];
3040 static void cli_unlock_done(struct tevent_req *subreq);
3042 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
3043 struct tevent_context *ev,
3044 struct cli_state *cli,
3045 uint16_t fnum,
3046 uint64_t offset,
3047 uint64_t len)
3050 struct tevent_req *req = NULL, *subreq = NULL;
3051 struct cli_unlock_state *state = NULL;
3052 uint8_t additional_flags = 0;
3054 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
3055 if (req == NULL) {
3056 return NULL;
3059 SCVAL(state->vwv+0, 0, 0xFF);
3060 SSVAL(state->vwv+2, 0, fnum);
3061 SCVAL(state->vwv+3, 0, 0);
3062 SIVALS(state->vwv+4, 0, 0);
3063 SSVAL(state->vwv+6, 0, 1);
3064 SSVAL(state->vwv+7, 0, 0);
3066 SSVAL(state->data, 0, cli_getpid(cli));
3067 SIVAL(state->data, 2, offset);
3068 SIVAL(state->data, 6, len);
3070 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3071 8, state->vwv, 10, state->data);
3072 if (tevent_req_nomem(subreq, req)) {
3073 return tevent_req_post(req, ev);
3075 tevent_req_set_callback(subreq, cli_unlock_done, req);
3076 return req;
3079 static void cli_unlock_done(struct tevent_req *subreq)
3081 struct tevent_req *req = tevent_req_callback_data(
3082 subreq, struct tevent_req);
3083 NTSTATUS status;
3085 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3086 TALLOC_FREE(subreq);
3087 if (tevent_req_nterror(req, status)) {
3088 return;
3090 tevent_req_done(req);
3093 NTSTATUS cli_unlock_recv(struct tevent_req *req)
3095 return tevent_req_simple_recv_ntstatus(req);
3098 NTSTATUS cli_unlock(struct cli_state *cli,
3099 uint16_t fnum,
3100 uint32_t offset,
3101 uint32_t len)
3103 TALLOC_CTX *frame = talloc_stackframe();
3104 struct tevent_context *ev;
3105 struct tevent_req *req;
3106 NTSTATUS status = NT_STATUS_OK;
3108 if (smbXcli_conn_has_async_calls(cli->conn)) {
3110 * Can't use sync call while an async call is in flight
3112 status = NT_STATUS_INVALID_PARAMETER;
3113 goto fail;
3116 ev = samba_tevent_context_init(frame);
3117 if (ev == NULL) {
3118 status = NT_STATUS_NO_MEMORY;
3119 goto fail;
3122 req = cli_unlock_send(frame, ev, cli,
3123 fnum, offset, len);
3124 if (req == NULL) {
3125 status = NT_STATUS_NO_MEMORY;
3126 goto fail;
3129 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3130 goto fail;
3133 status = cli_unlock_recv(req);
3135 fail:
3136 TALLOC_FREE(frame);
3137 return status;
3140 /****************************************************************************
3141 Lock a file with 64 bit offsets.
3142 ****************************************************************************/
3144 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
3145 uint64_t offset, uint64_t len, int timeout,
3146 enum brl_type lock_type)
3148 uint16_t vwv[8];
3149 uint8_t bytes[20];
3150 unsigned int set_timeout = 0;
3151 unsigned int saved_timeout = 0;
3152 int ltype;
3153 NTSTATUS status;
3155 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3156 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
3159 ltype = (lock_type == READ_LOCK? 1 : 0);
3160 ltype |= LOCKING_ANDX_LARGE_FILES;
3162 SCVAL(vwv + 0, 0, 0xff);
3163 SCVAL(vwv + 0, 1, 0);
3164 SSVAL(vwv + 1, 0, 0);
3165 SSVAL(vwv + 2, 0, fnum);
3166 SCVAL(vwv + 3, 0, ltype);
3167 SCVAL(vwv + 3, 1, 0);
3168 SIVALS(vwv + 4, 0, timeout);
3169 SSVAL(vwv + 6, 0, 0);
3170 SSVAL(vwv + 7, 0, 1);
3172 SIVAL(bytes, 0, cli_getpid(cli));
3173 SOFF_T_R(bytes, 4, offset);
3174 SOFF_T_R(bytes, 12, len);
3176 if (timeout != 0) {
3177 if (timeout == -1) {
3178 set_timeout = 0x7FFFFFFF;
3179 } else {
3180 set_timeout = timeout + 2*1000;
3182 saved_timeout = cli_set_timeout(cli, set_timeout);
3185 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
3186 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
3188 if (saved_timeout != 0) {
3189 cli_set_timeout(cli, saved_timeout);
3192 return status;
3195 /****************************************************************************
3196 Unlock a file with 64 bit offsets.
3197 ****************************************************************************/
3199 struct cli_unlock64_state {
3200 uint16_t vwv[8];
3201 uint8_t data[20];
3204 static void cli_unlock64_done(struct tevent_req *subreq);
3206 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
3207 struct tevent_context *ev,
3208 struct cli_state *cli,
3209 uint16_t fnum,
3210 uint64_t offset,
3211 uint64_t len)
3214 struct tevent_req *req = NULL, *subreq = NULL;
3215 struct cli_unlock64_state *state = NULL;
3216 uint8_t additional_flags = 0;
3218 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
3219 if (req == NULL) {
3220 return NULL;
3223 SCVAL(state->vwv+0, 0, 0xff);
3224 SSVAL(state->vwv+2, 0, fnum);
3225 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
3226 SIVALS(state->vwv+4, 0, 0);
3227 SSVAL(state->vwv+6, 0, 1);
3228 SSVAL(state->vwv+7, 0, 0);
3230 SIVAL(state->data, 0, cli_getpid(cli));
3231 SOFF_T_R(state->data, 4, offset);
3232 SOFF_T_R(state->data, 12, len);
3234 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags, 0,
3235 8, state->vwv, 20, state->data);
3236 if (tevent_req_nomem(subreq, req)) {
3237 return tevent_req_post(req, ev);
3239 tevent_req_set_callback(subreq, cli_unlock64_done, req);
3240 return req;
3243 static void cli_unlock64_done(struct tevent_req *subreq)
3245 struct tevent_req *req = tevent_req_callback_data(
3246 subreq, struct tevent_req);
3247 NTSTATUS status;
3249 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3250 TALLOC_FREE(subreq);
3251 if (tevent_req_nterror(req, status)) {
3252 return;
3254 tevent_req_done(req);
3257 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
3259 return tevent_req_simple_recv_ntstatus(req);
3262 NTSTATUS cli_unlock64(struct cli_state *cli,
3263 uint16_t fnum,
3264 uint64_t offset,
3265 uint64_t len)
3267 TALLOC_CTX *frame = talloc_stackframe();
3268 struct tevent_context *ev;
3269 struct tevent_req *req;
3270 NTSTATUS status = NT_STATUS_OK;
3272 if (! (smb1cli_conn_capabilities(cli->conn) & CAP_LARGE_FILES)) {
3273 return cli_unlock(cli, fnum, offset, len);
3276 if (smbXcli_conn_has_async_calls(cli->conn)) {
3278 * Can't use sync call while an async call is in flight
3280 status = NT_STATUS_INVALID_PARAMETER;
3281 goto fail;
3284 ev = samba_tevent_context_init(frame);
3285 if (ev == NULL) {
3286 status = NT_STATUS_NO_MEMORY;
3287 goto fail;
3290 req = cli_unlock64_send(frame, ev, cli,
3291 fnum, offset, len);
3292 if (req == NULL) {
3293 status = NT_STATUS_NO_MEMORY;
3294 goto fail;
3297 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3298 goto fail;
3301 status = cli_unlock64_recv(req);
3303 fail:
3304 TALLOC_FREE(frame);
3305 return status;
3308 /****************************************************************************
3309 Get/unlock a POSIX lock on a file - internal function.
3310 ****************************************************************************/
3312 struct posix_lock_state {
3313 uint16_t setup;
3314 uint8_t param[4];
3315 uint8_t data[POSIX_LOCK_DATA_SIZE];
3318 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
3320 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
3321 NULL, 0, NULL, NULL, 0, NULL);
3322 tevent_req_simple_finish_ntstatus(subreq, status);
3325 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
3326 struct tevent_context *ev,
3327 struct cli_state *cli,
3328 uint16_t fnum,
3329 uint64_t offset,
3330 uint64_t len,
3331 bool wait_lock,
3332 enum brl_type lock_type)
3334 struct tevent_req *req = NULL, *subreq = NULL;
3335 struct posix_lock_state *state = NULL;
3337 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
3338 if (req == NULL) {
3339 return NULL;
3342 /* Setup setup word. */
3343 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
3345 /* Setup param array. */
3346 SSVAL(&state->param, 0, fnum);
3347 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3349 /* Setup data array. */
3350 switch (lock_type) {
3351 case READ_LOCK:
3352 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3353 POSIX_LOCK_TYPE_READ);
3354 break;
3355 case WRITE_LOCK:
3356 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3357 POSIX_LOCK_TYPE_WRITE);
3358 break;
3359 case UNLOCK_LOCK:
3360 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3361 POSIX_LOCK_TYPE_UNLOCK);
3362 break;
3363 default:
3364 return NULL;
3367 if (wait_lock) {
3368 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3369 POSIX_LOCK_FLAG_WAIT);
3370 } else {
3371 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3372 POSIX_LOCK_FLAG_NOWAIT);
3375 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
3376 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3377 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3379 subreq = cli_trans_send(state, /* mem ctx. */
3380 ev, /* event ctx. */
3381 cli, /* cli_state. */
3382 0, /* additional_flags2 */
3383 SMBtrans2, /* cmd. */
3384 NULL, /* pipe name. */
3385 -1, /* fid. */
3386 0, /* function. */
3387 0, /* flags. */
3388 &state->setup, /* setup. */
3389 1, /* num setup uint16_t words. */
3390 0, /* max returned setup. */
3391 state->param, /* param. */
3392 4, /* num param. */
3393 2, /* max returned param. */
3394 state->data, /* data. */
3395 POSIX_LOCK_DATA_SIZE, /* num data. */
3396 0); /* max returned data. */
3398 if (tevent_req_nomem(subreq, req)) {
3399 return tevent_req_post(req, ev);
3401 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3402 return req;
3405 /****************************************************************************
3406 POSIX Lock a file.
3407 ****************************************************************************/
3409 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3410 struct tevent_context *ev,
3411 struct cli_state *cli,
3412 uint16_t fnum,
3413 uint64_t offset,
3414 uint64_t len,
3415 bool wait_lock,
3416 enum brl_type lock_type)
3418 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3419 wait_lock, lock_type);
3422 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3424 return tevent_req_simple_recv_ntstatus(req);
3427 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3428 uint64_t offset, uint64_t len,
3429 bool wait_lock, enum brl_type lock_type)
3431 TALLOC_CTX *frame = talloc_stackframe();
3432 struct tevent_context *ev = NULL;
3433 struct tevent_req *req = NULL;
3434 NTSTATUS status = NT_STATUS_OK;
3436 if (smbXcli_conn_has_async_calls(cli->conn)) {
3438 * Can't use sync call while an async call is in flight
3440 status = NT_STATUS_INVALID_PARAMETER;
3441 goto fail;
3444 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3445 status = NT_STATUS_INVALID_PARAMETER;
3446 goto fail;
3449 ev = samba_tevent_context_init(frame);
3450 if (ev == NULL) {
3451 status = NT_STATUS_NO_MEMORY;
3452 goto fail;
3455 req = cli_posix_lock_send(frame,
3457 cli,
3458 fnum,
3459 offset,
3460 len,
3461 wait_lock,
3462 lock_type);
3463 if (req == NULL) {
3464 status = NT_STATUS_NO_MEMORY;
3465 goto fail;
3468 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3469 goto fail;
3472 status = cli_posix_lock_recv(req);
3474 fail:
3475 TALLOC_FREE(frame);
3476 return status;
3479 /****************************************************************************
3480 POSIX Unlock a file.
3481 ****************************************************************************/
3483 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3484 struct tevent_context *ev,
3485 struct cli_state *cli,
3486 uint16_t fnum,
3487 uint64_t offset,
3488 uint64_t len)
3490 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3491 false, UNLOCK_LOCK);
3494 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3496 return tevent_req_simple_recv_ntstatus(req);
3499 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3501 TALLOC_CTX *frame = talloc_stackframe();
3502 struct tevent_context *ev = NULL;
3503 struct tevent_req *req = NULL;
3504 NTSTATUS status = NT_STATUS_OK;
3506 if (smbXcli_conn_has_async_calls(cli->conn)) {
3508 * Can't use sync call while an async call is in flight
3510 status = NT_STATUS_INVALID_PARAMETER;
3511 goto fail;
3514 ev = samba_tevent_context_init(frame);
3515 if (ev == NULL) {
3516 status = NT_STATUS_NO_MEMORY;
3517 goto fail;
3520 req = cli_posix_unlock_send(frame,
3522 cli,
3523 fnum,
3524 offset,
3525 len);
3526 if (req == NULL) {
3527 status = NT_STATUS_NO_MEMORY;
3528 goto fail;
3531 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3532 goto fail;
3535 status = cli_posix_unlock_recv(req);
3537 fail:
3538 TALLOC_FREE(frame);
3539 return status;
3542 /****************************************************************************
3543 Do a SMBgetattrE call.
3544 ****************************************************************************/
3546 static void cli_getattrE_done(struct tevent_req *subreq);
3548 struct cli_getattrE_state {
3549 uint16_t vwv[1];
3550 int zone_offset;
3551 uint16_t attr;
3552 off_t size;
3553 time_t change_time;
3554 time_t access_time;
3555 time_t write_time;
3558 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3559 struct tevent_context *ev,
3560 struct cli_state *cli,
3561 uint16_t fnum)
3563 struct tevent_req *req = NULL, *subreq = NULL;
3564 struct cli_getattrE_state *state = NULL;
3565 uint8_t additional_flags = 0;
3567 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3568 if (req == NULL) {
3569 return NULL;
3572 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3573 SSVAL(state->vwv+0,0,fnum);
3575 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags, 0,
3576 1, state->vwv, 0, NULL);
3577 if (tevent_req_nomem(subreq, req)) {
3578 return tevent_req_post(req, ev);
3580 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3581 return req;
3584 static void cli_getattrE_done(struct tevent_req *subreq)
3586 struct tevent_req *req = tevent_req_callback_data(
3587 subreq, struct tevent_req);
3588 struct cli_getattrE_state *state = tevent_req_data(
3589 req, struct cli_getattrE_state);
3590 uint8_t wct;
3591 uint16_t *vwv = NULL;
3592 NTSTATUS status;
3594 status = cli_smb_recv(subreq, state, NULL, 11, &wct, &vwv,
3595 NULL, NULL);
3596 TALLOC_FREE(subreq);
3597 if (tevent_req_nterror(req, status)) {
3598 return;
3601 state->size = (off_t)IVAL(vwv+6,0);
3602 state->attr = SVAL(vwv+10,0);
3603 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3604 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3605 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3607 tevent_req_done(req);
3610 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3611 uint16_t *attr,
3612 off_t *size,
3613 time_t *change_time,
3614 time_t *access_time,
3615 time_t *write_time)
3617 struct cli_getattrE_state *state = tevent_req_data(
3618 req, struct cli_getattrE_state);
3619 NTSTATUS status;
3621 if (tevent_req_is_nterror(req, &status)) {
3622 return status;
3624 if (attr) {
3625 *attr = state->attr;
3627 if (size) {
3628 *size = state->size;
3630 if (change_time) {
3631 *change_time = state->change_time;
3633 if (access_time) {
3634 *access_time = state->access_time;
3636 if (write_time) {
3637 *write_time = state->write_time;
3639 return NT_STATUS_OK;
3642 NTSTATUS cli_getattrE(struct cli_state *cli,
3643 uint16_t fnum,
3644 uint16_t *attr,
3645 off_t *size,
3646 time_t *change_time,
3647 time_t *access_time,
3648 time_t *write_time)
3650 TALLOC_CTX *frame = NULL;
3651 struct tevent_context *ev = NULL;
3652 struct tevent_req *req = NULL;
3653 NTSTATUS status = NT_STATUS_OK;
3655 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3656 return cli_smb2_getattrE(cli,
3657 fnum,
3658 attr,
3659 size,
3660 change_time,
3661 access_time,
3662 write_time);
3665 frame = talloc_stackframe();
3667 if (smbXcli_conn_has_async_calls(cli->conn)) {
3669 * Can't use sync call while an async call is in flight
3671 status = NT_STATUS_INVALID_PARAMETER;
3672 goto fail;
3675 ev = samba_tevent_context_init(frame);
3676 if (ev == NULL) {
3677 status = NT_STATUS_NO_MEMORY;
3678 goto fail;
3681 req = cli_getattrE_send(frame, ev, cli, fnum);
3682 if (req == NULL) {
3683 status = NT_STATUS_NO_MEMORY;
3684 goto fail;
3687 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3688 goto fail;
3691 status = cli_getattrE_recv(req,
3692 attr,
3693 size,
3694 change_time,
3695 access_time,
3696 write_time);
3698 fail:
3699 TALLOC_FREE(frame);
3700 return status;
3703 /****************************************************************************
3704 Do a SMBgetatr call
3705 ****************************************************************************/
3707 static void cli_getatr_done(struct tevent_req *subreq);
3709 struct cli_getatr_state {
3710 int zone_offset;
3711 uint16_t attr;
3712 off_t size;
3713 time_t write_time;
3716 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3717 struct tevent_context *ev,
3718 struct cli_state *cli,
3719 const char *fname)
3721 struct tevent_req *req = NULL, *subreq = NULL;
3722 struct cli_getatr_state *state = NULL;
3723 uint8_t additional_flags = 0;
3724 uint8_t *bytes = NULL;
3726 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3727 if (req == NULL) {
3728 return NULL;
3731 state->zone_offset = smb1cli_conn_server_time_zone(cli->conn);
3733 bytes = talloc_array(state, uint8_t, 1);
3734 if (tevent_req_nomem(bytes, req)) {
3735 return tevent_req_post(req, ev);
3737 bytes[0] = 4;
3738 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
3739 strlen(fname)+1, NULL);
3741 if (tevent_req_nomem(bytes, req)) {
3742 return tevent_req_post(req, ev);
3745 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags, 0,
3746 0, NULL, talloc_get_size(bytes), bytes);
3747 if (tevent_req_nomem(subreq, req)) {
3748 return tevent_req_post(req, ev);
3750 tevent_req_set_callback(subreq, cli_getatr_done, req);
3751 return req;
3754 static void cli_getatr_done(struct tevent_req *subreq)
3756 struct tevent_req *req = tevent_req_callback_data(
3757 subreq, struct tevent_req);
3758 struct cli_getatr_state *state = tevent_req_data(
3759 req, struct cli_getatr_state);
3760 uint8_t wct;
3761 uint16_t *vwv = NULL;
3762 NTSTATUS status;
3764 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
3765 NULL);
3766 TALLOC_FREE(subreq);
3767 if (tevent_req_nterror(req, status)) {
3768 return;
3771 state->attr = SVAL(vwv+0,0);
3772 state->size = (off_t)IVAL(vwv+3,0);
3773 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3775 tevent_req_done(req);
3778 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3779 uint16_t *attr,
3780 off_t *size,
3781 time_t *write_time)
3783 struct cli_getatr_state *state = tevent_req_data(
3784 req, struct cli_getatr_state);
3785 NTSTATUS status;
3787 if (tevent_req_is_nterror(req, &status)) {
3788 return status;
3790 if (attr) {
3791 *attr = state->attr;
3793 if (size) {
3794 *size = state->size;
3796 if (write_time) {
3797 *write_time = state->write_time;
3799 return NT_STATUS_OK;
3802 NTSTATUS cli_getatr(struct cli_state *cli,
3803 const char *fname,
3804 uint16_t *attr,
3805 off_t *size,
3806 time_t *write_time)
3808 TALLOC_CTX *frame = NULL;
3809 struct tevent_context *ev = NULL;
3810 struct tevent_req *req = NULL;
3811 NTSTATUS status = NT_STATUS_OK;
3813 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3814 return cli_smb2_getatr(cli,
3815 fname,
3816 attr,
3817 size,
3818 write_time);
3821 frame = talloc_stackframe();
3823 if (smbXcli_conn_has_async_calls(cli->conn)) {
3825 * Can't use sync call while an async call is in flight
3827 status = NT_STATUS_INVALID_PARAMETER;
3828 goto fail;
3831 ev = samba_tevent_context_init(frame);
3832 if (ev == NULL) {
3833 status = NT_STATUS_NO_MEMORY;
3834 goto fail;
3837 req = cli_getatr_send(frame, ev, cli, fname);
3838 if (req == NULL) {
3839 status = NT_STATUS_NO_MEMORY;
3840 goto fail;
3843 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3844 goto fail;
3847 status = cli_getatr_recv(req,
3848 attr,
3849 size,
3850 write_time);
3852 fail:
3853 TALLOC_FREE(frame);
3854 return status;
3857 /****************************************************************************
3858 Do a SMBsetattrE call.
3859 ****************************************************************************/
3861 static void cli_setattrE_done(struct tevent_req *subreq);
3863 struct cli_setattrE_state {
3864 uint16_t vwv[7];
3867 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3868 struct tevent_context *ev,
3869 struct cli_state *cli,
3870 uint16_t fnum,
3871 time_t change_time,
3872 time_t access_time,
3873 time_t write_time)
3875 struct tevent_req *req = NULL, *subreq = NULL;
3876 struct cli_setattrE_state *state = NULL;
3877 uint8_t additional_flags = 0;
3879 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3880 if (req == NULL) {
3881 return NULL;
3884 SSVAL(state->vwv+0, 0, fnum);
3885 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3886 smb1cli_conn_server_time_zone(cli->conn));
3887 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3888 smb1cli_conn_server_time_zone(cli->conn));
3889 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3890 smb1cli_conn_server_time_zone(cli->conn));
3892 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags, 0,
3893 7, state->vwv, 0, NULL);
3894 if (tevent_req_nomem(subreq, req)) {
3895 return tevent_req_post(req, ev);
3897 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3898 return req;
3901 static void cli_setattrE_done(struct tevent_req *subreq)
3903 struct tevent_req *req = tevent_req_callback_data(
3904 subreq, struct tevent_req);
3905 NTSTATUS status;
3907 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3908 TALLOC_FREE(subreq);
3909 if (tevent_req_nterror(req, status)) {
3910 return;
3912 tevent_req_done(req);
3915 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3917 return tevent_req_simple_recv_ntstatus(req);
3920 NTSTATUS cli_setattrE(struct cli_state *cli,
3921 uint16_t fnum,
3922 time_t change_time,
3923 time_t access_time,
3924 time_t write_time)
3926 TALLOC_CTX *frame = NULL;
3927 struct tevent_context *ev = NULL;
3928 struct tevent_req *req = NULL;
3929 NTSTATUS status = NT_STATUS_OK;
3931 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
3932 return cli_smb2_setattrE(cli,
3933 fnum,
3934 change_time,
3935 access_time,
3936 write_time);
3939 frame = talloc_stackframe();
3941 if (smbXcli_conn_has_async_calls(cli->conn)) {
3943 * Can't use sync call while an async call is in flight
3945 status = NT_STATUS_INVALID_PARAMETER;
3946 goto fail;
3949 ev = samba_tevent_context_init(frame);
3950 if (ev == NULL) {
3951 status = NT_STATUS_NO_MEMORY;
3952 goto fail;
3955 req = cli_setattrE_send(frame, ev,
3956 cli,
3957 fnum,
3958 change_time,
3959 access_time,
3960 write_time);
3962 if (req == NULL) {
3963 status = NT_STATUS_NO_MEMORY;
3964 goto fail;
3967 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
3968 goto fail;
3971 status = cli_setattrE_recv(req);
3973 fail:
3974 TALLOC_FREE(frame);
3975 return status;
3978 /****************************************************************************
3979 Do a SMBsetatr call.
3980 ****************************************************************************/
3982 static void cli_setatr_done(struct tevent_req *subreq);
3984 struct cli_setatr_state {
3985 uint16_t vwv[8];
3988 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3989 struct tevent_context *ev,
3990 struct cli_state *cli,
3991 const char *fname,
3992 uint16_t attr,
3993 time_t mtime)
3995 struct tevent_req *req = NULL, *subreq = NULL;
3996 struct cli_setatr_state *state = NULL;
3997 uint8_t additional_flags = 0;
3998 uint8_t *bytes = NULL;
4000 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
4001 if (req == NULL) {
4002 return NULL;
4005 SSVAL(state->vwv+0, 0, attr);
4006 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, smb1cli_conn_server_time_zone(cli->conn));
4008 bytes = talloc_array(state, uint8_t, 1);
4009 if (tevent_req_nomem(bytes, req)) {
4010 return tevent_req_post(req, ev);
4012 bytes[0] = 4;
4013 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4014 strlen(fname)+1, NULL);
4015 if (tevent_req_nomem(bytes, req)) {
4016 return tevent_req_post(req, ev);
4018 bytes = talloc_realloc(state, bytes, uint8_t,
4019 talloc_get_size(bytes)+1);
4020 if (tevent_req_nomem(bytes, req)) {
4021 return tevent_req_post(req, ev);
4024 bytes[talloc_get_size(bytes)-1] = 4;
4025 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), "",
4026 1, NULL);
4027 if (tevent_req_nomem(bytes, req)) {
4028 return tevent_req_post(req, ev);
4031 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags, 0,
4032 8, state->vwv, talloc_get_size(bytes), bytes);
4033 if (tevent_req_nomem(subreq, req)) {
4034 return tevent_req_post(req, ev);
4036 tevent_req_set_callback(subreq, cli_setatr_done, req);
4037 return req;
4040 static void cli_setatr_done(struct tevent_req *subreq)
4042 struct tevent_req *req = tevent_req_callback_data(
4043 subreq, struct tevent_req);
4044 NTSTATUS status;
4046 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4047 TALLOC_FREE(subreq);
4048 if (tevent_req_nterror(req, status)) {
4049 return;
4051 tevent_req_done(req);
4054 NTSTATUS cli_setatr_recv(struct tevent_req *req)
4056 return tevent_req_simple_recv_ntstatus(req);
4059 NTSTATUS cli_setatr(struct cli_state *cli,
4060 const char *fname,
4061 uint16_t attr,
4062 time_t mtime)
4064 TALLOC_CTX *frame = NULL;
4065 struct tevent_context *ev = NULL;
4066 struct tevent_req *req = NULL;
4067 NTSTATUS status = NT_STATUS_OK;
4069 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4070 return cli_smb2_setatr(cli,
4071 fname,
4072 attr,
4073 mtime);
4076 frame = talloc_stackframe();
4078 if (smbXcli_conn_has_async_calls(cli->conn)) {
4080 * Can't use sync call while an async call is in flight
4082 status = NT_STATUS_INVALID_PARAMETER;
4083 goto fail;
4086 ev = samba_tevent_context_init(frame);
4087 if (ev == NULL) {
4088 status = NT_STATUS_NO_MEMORY;
4089 goto fail;
4092 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
4093 if (req == NULL) {
4094 status = NT_STATUS_NO_MEMORY;
4095 goto fail;
4098 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4099 goto fail;
4102 status = cli_setatr_recv(req);
4104 fail:
4105 TALLOC_FREE(frame);
4106 return status;
4109 /****************************************************************************
4110 Check for existance of a dir.
4111 ****************************************************************************/
4113 static void cli_chkpath_done(struct tevent_req *subreq);
4115 struct cli_chkpath_state {
4116 int dummy;
4119 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
4120 struct tevent_context *ev,
4121 struct cli_state *cli,
4122 const char *fname)
4124 struct tevent_req *req = NULL, *subreq = NULL;
4125 struct cli_chkpath_state *state = NULL;
4126 uint8_t additional_flags = 0;
4127 uint8_t *bytes = NULL;
4129 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
4130 if (req == NULL) {
4131 return NULL;
4134 bytes = talloc_array(state, uint8_t, 1);
4135 if (tevent_req_nomem(bytes, req)) {
4136 return tevent_req_post(req, ev);
4138 bytes[0] = 4;
4139 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), fname,
4140 strlen(fname)+1, NULL);
4142 if (tevent_req_nomem(bytes, req)) {
4143 return tevent_req_post(req, ev);
4146 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags, 0,
4147 0, NULL, talloc_get_size(bytes), bytes);
4148 if (tevent_req_nomem(subreq, req)) {
4149 return tevent_req_post(req, ev);
4151 tevent_req_set_callback(subreq, cli_chkpath_done, req);
4152 return req;
4155 static void cli_chkpath_done(struct tevent_req *subreq)
4157 struct tevent_req *req = tevent_req_callback_data(
4158 subreq, struct tevent_req);
4159 NTSTATUS status;
4161 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
4162 TALLOC_FREE(subreq);
4163 if (tevent_req_nterror(req, status)) {
4164 return;
4166 tevent_req_done(req);
4169 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
4171 return tevent_req_simple_recv_ntstatus(req);
4174 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
4176 TALLOC_CTX *frame = talloc_stackframe();
4177 struct tevent_context *ev = NULL;
4178 struct tevent_req *req = NULL;
4179 char *path2 = NULL;
4180 NTSTATUS status = NT_STATUS_OK;
4182 if (smbXcli_conn_has_async_calls(cli->conn)) {
4184 * Can't use sync call while an async call is in flight
4186 status = NT_STATUS_INVALID_PARAMETER;
4187 goto fail;
4190 path2 = talloc_strdup(frame, path);
4191 if (!path2) {
4192 status = NT_STATUS_NO_MEMORY;
4193 goto fail;
4195 trim_char(path2,'\0','\\');
4196 if (!*path2) {
4197 path2 = talloc_strdup(frame, "\\");
4198 if (!path2) {
4199 status = NT_STATUS_NO_MEMORY;
4200 goto fail;
4204 ev = samba_tevent_context_init(frame);
4205 if (ev == NULL) {
4206 status = NT_STATUS_NO_MEMORY;
4207 goto fail;
4210 req = cli_chkpath_send(frame, ev, cli, path2);
4211 if (req == NULL) {
4212 status = NT_STATUS_NO_MEMORY;
4213 goto fail;
4216 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4217 goto fail;
4220 status = cli_chkpath_recv(req);
4222 fail:
4223 TALLOC_FREE(frame);
4224 return status;
4227 /****************************************************************************
4228 Query disk space.
4229 ****************************************************************************/
4231 static void cli_dskattr_done(struct tevent_req *subreq);
4233 struct cli_dskattr_state {
4234 int bsize;
4235 int total;
4236 int avail;
4239 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
4240 struct tevent_context *ev,
4241 struct cli_state *cli)
4243 struct tevent_req *req = NULL, *subreq = NULL;
4244 struct cli_dskattr_state *state = NULL;
4245 uint8_t additional_flags = 0;
4247 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
4248 if (req == NULL) {
4249 return NULL;
4252 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags, 0,
4253 0, NULL, 0, NULL);
4254 if (tevent_req_nomem(subreq, req)) {
4255 return tevent_req_post(req, ev);
4257 tevent_req_set_callback(subreq, cli_dskattr_done, req);
4258 return req;
4261 static void cli_dskattr_done(struct tevent_req *subreq)
4263 struct tevent_req *req = tevent_req_callback_data(
4264 subreq, struct tevent_req);
4265 struct cli_dskattr_state *state = tevent_req_data(
4266 req, struct cli_dskattr_state);
4267 uint8_t wct;
4268 uint16_t *vwv = NULL;
4269 NTSTATUS status;
4271 status = cli_smb_recv(subreq, state, NULL, 4, &wct, &vwv, NULL,
4272 NULL);
4273 TALLOC_FREE(subreq);
4274 if (tevent_req_nterror(req, status)) {
4275 return;
4277 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
4278 state->total = SVAL(vwv+0, 0);
4279 state->avail = SVAL(vwv+3, 0);
4280 tevent_req_done(req);
4283 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
4285 struct cli_dskattr_state *state = tevent_req_data(
4286 req, struct cli_dskattr_state);
4287 NTSTATUS status;
4289 if (tevent_req_is_nterror(req, &status)) {
4290 return status;
4292 *bsize = state->bsize;
4293 *total = state->total;
4294 *avail = state->avail;
4295 return NT_STATUS_OK;
4298 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
4300 TALLOC_CTX *frame = NULL;
4301 struct tevent_context *ev = NULL;
4302 struct tevent_req *req = NULL;
4303 NTSTATUS status = NT_STATUS_OK;
4305 frame = talloc_stackframe();
4307 if (smbXcli_conn_has_async_calls(cli->conn)) {
4309 * Can't use sync call while an async call is in flight
4311 status = NT_STATUS_INVALID_PARAMETER;
4312 goto fail;
4315 ev = samba_tevent_context_init(frame);
4316 if (ev == NULL) {
4317 status = NT_STATUS_NO_MEMORY;
4318 goto fail;
4321 req = cli_dskattr_send(frame, ev, cli);
4322 if (req == NULL) {
4323 status = NT_STATUS_NO_MEMORY;
4324 goto fail;
4327 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4328 goto fail;
4331 status = cli_dskattr_recv(req, bsize, total, avail);
4333 fail:
4334 TALLOC_FREE(frame);
4335 return status;
4338 NTSTATUS cli_disk_size(struct cli_state *cli, const char *path, uint64_t *bsize,
4339 uint64_t *total, uint64_t *avail)
4341 uint64_t sectors_per_block;
4342 uint64_t bytes_per_sector;
4343 int old_bsize, old_total, old_avail;
4344 NTSTATUS status;
4346 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4347 return cli_smb2_dskattr(cli, path, bsize, total, avail);
4351 * Try the trans2 disk full size info call first.
4352 * We already use this in SMBC_fstatvfs_ctx().
4353 * Ignore 'actual_available_units' as we only
4354 * care about the quota for the caller.
4357 status = cli_get_fs_full_size_info(cli,
4358 total,
4359 avail,
4360 NULL,
4361 &sectors_per_block,
4362 &bytes_per_sector);
4364 /* Try and cope will all varients of "we don't do this call"
4365 and fall back to cli_dskattr. */
4367 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_IMPLEMENTED) ||
4368 NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED) ||
4369 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_INFO_CLASS) ||
4370 NT_STATUS_EQUAL(status,NT_STATUS_PROCEDURE_NOT_FOUND) ||
4371 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_LEVEL) ||
4372 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER) ||
4373 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_REQUEST) ||
4374 NT_STATUS_EQUAL(status,NT_STATUS_INVALID_DEVICE_STATE) ||
4375 NT_STATUS_EQUAL(status,NT_STATUS_CTL_FILE_NOT_SUPPORTED) ||
4376 NT_STATUS_EQUAL(status,NT_STATUS_UNSUCCESSFUL)) {
4377 goto try_dskattr;
4380 if (!NT_STATUS_IS_OK(status)) {
4381 return status;
4384 if (bsize) {
4385 *bsize = sectors_per_block *
4386 bytes_per_sector;
4389 return NT_STATUS_OK;
4391 try_dskattr:
4393 /* Old SMB1 core protocol fallback. */
4394 status = cli_dskattr(cli, &old_bsize, &old_total, &old_avail);
4395 if (!NT_STATUS_IS_OK(status)) {
4396 return status;
4398 if (bsize) {
4399 *bsize = (uint64_t)old_bsize;
4401 if (total) {
4402 *total = (uint64_t)old_total;
4404 if (avail) {
4405 *avail = (uint64_t)old_avail;
4407 return NT_STATUS_OK;
4410 /****************************************************************************
4411 Create and open a temporary file.
4412 ****************************************************************************/
4414 static void cli_ctemp_done(struct tevent_req *subreq);
4416 struct ctemp_state {
4417 uint16_t vwv[3];
4418 char *ret_path;
4419 uint16_t fnum;
4422 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4423 struct tevent_context *ev,
4424 struct cli_state *cli,
4425 const char *path)
4427 struct tevent_req *req = NULL, *subreq = NULL;
4428 struct ctemp_state *state = NULL;
4429 uint8_t additional_flags = 0;
4430 uint8_t *bytes = NULL;
4432 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4433 if (req == NULL) {
4434 return NULL;
4437 SSVAL(state->vwv,0,0);
4438 SIVALS(state->vwv+1,0,-1);
4440 bytes = talloc_array(state, uint8_t, 1);
4441 if (tevent_req_nomem(bytes, req)) {
4442 return tevent_req_post(req, ev);
4444 bytes[0] = 4;
4445 bytes = smb_bytes_push_str(bytes, smbXcli_conn_use_unicode(cli->conn), path,
4446 strlen(path)+1, NULL);
4447 if (tevent_req_nomem(bytes, req)) {
4448 return tevent_req_post(req, ev);
4451 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags, 0,
4452 3, state->vwv, talloc_get_size(bytes), bytes);
4453 if (tevent_req_nomem(subreq, req)) {
4454 return tevent_req_post(req, ev);
4456 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4457 return req;
4460 static void cli_ctemp_done(struct tevent_req *subreq)
4462 struct tevent_req *req = tevent_req_callback_data(
4463 subreq, struct tevent_req);
4464 struct ctemp_state *state = tevent_req_data(
4465 req, struct ctemp_state);
4466 NTSTATUS status;
4467 uint8_t wcnt;
4468 uint16_t *vwv;
4469 uint32_t num_bytes = 0;
4470 uint8_t *bytes = NULL;
4472 status = cli_smb_recv(subreq, state, NULL, 1, &wcnt, &vwv,
4473 &num_bytes, &bytes);
4474 TALLOC_FREE(subreq);
4475 if (tevent_req_nterror(req, status)) {
4476 return;
4479 state->fnum = SVAL(vwv+0, 0);
4481 /* From W2K3, the result is just the ASCII name */
4482 if (num_bytes < 2) {
4483 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4484 return;
4487 if (pull_string_talloc(state,
4488 NULL,
4490 &state->ret_path,
4491 bytes,
4492 num_bytes,
4493 STR_ASCII) == 0) {
4494 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4495 return;
4497 tevent_req_done(req);
4500 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4501 TALLOC_CTX *ctx,
4502 uint16_t *pfnum,
4503 char **outfile)
4505 struct ctemp_state *state = tevent_req_data(req,
4506 struct ctemp_state);
4507 NTSTATUS status;
4509 if (tevent_req_is_nterror(req, &status)) {
4510 return status;
4512 *pfnum = state->fnum;
4513 *outfile = talloc_strdup(ctx, state->ret_path);
4514 if (!*outfile) {
4515 return NT_STATUS_NO_MEMORY;
4517 return NT_STATUS_OK;
4520 NTSTATUS cli_ctemp(struct cli_state *cli,
4521 TALLOC_CTX *ctx,
4522 const char *path,
4523 uint16_t *pfnum,
4524 char **out_path)
4526 TALLOC_CTX *frame = talloc_stackframe();
4527 struct tevent_context *ev;
4528 struct tevent_req *req;
4529 NTSTATUS status = NT_STATUS_OK;
4531 if (smbXcli_conn_has_async_calls(cli->conn)) {
4533 * Can't use sync call while an async call is in flight
4535 status = NT_STATUS_INVALID_PARAMETER;
4536 goto fail;
4539 ev = samba_tevent_context_init(frame);
4540 if (ev == NULL) {
4541 status = NT_STATUS_NO_MEMORY;
4542 goto fail;
4545 req = cli_ctemp_send(frame, ev, cli, path);
4546 if (req == NULL) {
4547 status = NT_STATUS_NO_MEMORY;
4548 goto fail;
4551 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4552 goto fail;
4555 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4557 fail:
4558 TALLOC_FREE(frame);
4559 return status;
4563 send a raw ioctl - used by the torture code
4565 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4567 uint16_t vwv[3];
4568 NTSTATUS status;
4570 SSVAL(vwv+0, 0, fnum);
4571 SSVAL(vwv+1, 0, code>>16);
4572 SSVAL(vwv+2, 0, (code&0xFFFF));
4574 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4575 NULL, 0, NULL, NULL, NULL, NULL);
4576 if (!NT_STATUS_IS_OK(status)) {
4577 return status;
4579 *blob = data_blob_null;
4580 return NT_STATUS_OK;
4583 /*********************************************************
4584 Set an extended attribute utility fn.
4585 *********************************************************/
4587 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4588 uint8_t *param, unsigned int param_len,
4589 const char *ea_name,
4590 const char *ea_val, size_t ea_len)
4592 uint16_t setup[1];
4593 unsigned int data_len = 0;
4594 uint8_t *data = NULL;
4595 char *p;
4596 size_t ea_namelen = strlen(ea_name);
4597 NTSTATUS status;
4599 SSVAL(setup, 0, setup_val);
4601 if (ea_namelen == 0 && ea_len == 0) {
4602 data_len = 4;
4603 data = talloc_array(talloc_tos(),
4604 uint8_t,
4605 data_len);
4606 if (!data) {
4607 return NT_STATUS_NO_MEMORY;
4609 p = (char *)data;
4610 SIVAL(p,0,data_len);
4611 } else {
4612 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4613 data = talloc_array(talloc_tos(),
4614 uint8_t,
4615 data_len);
4616 if (!data) {
4617 return NT_STATUS_NO_MEMORY;
4619 p = (char *)data;
4620 SIVAL(p,0,data_len);
4621 p += 4;
4622 SCVAL(p, 0, 0); /* EA flags. */
4623 SCVAL(p, 1, ea_namelen);
4624 SSVAL(p, 2, ea_len);
4625 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4626 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4629 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4630 setup, 1, 0,
4631 param, param_len, 2,
4632 data, data_len, 0,
4633 NULL,
4634 NULL, 0, NULL, /* rsetup */
4635 NULL, 0, NULL, /* rparam */
4636 NULL, 0, NULL); /* rdata */
4637 talloc_free(data);
4638 return status;
4641 /*********************************************************
4642 Set an extended attribute on a pathname.
4643 *********************************************************/
4645 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4646 const char *ea_name, const char *ea_val,
4647 size_t ea_len)
4649 unsigned int param_len = 0;
4650 uint8_t *param;
4651 NTSTATUS status;
4652 TALLOC_CTX *frame = NULL;
4654 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4655 return cli_smb2_set_ea_path(cli,
4656 path,
4657 ea_name,
4658 ea_val,
4659 ea_len);
4662 frame = talloc_stackframe();
4664 param = talloc_array(frame, uint8_t, 6);
4665 if (!param) {
4666 status = NT_STATUS_NO_MEMORY;
4667 goto fail;
4669 SSVAL(param,0,SMB_INFO_SET_EA);
4670 SSVAL(param,2,0);
4671 SSVAL(param,4,0);
4673 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
4674 path, strlen(path)+1,
4675 NULL);
4676 param_len = talloc_get_size(param);
4678 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4679 ea_name, ea_val, ea_len);
4681 fail:
4683 TALLOC_FREE(frame);
4684 return status;
4687 /*********************************************************
4688 Set an extended attribute on an fnum.
4689 *********************************************************/
4691 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4692 const char *ea_name, const char *ea_val,
4693 size_t ea_len)
4695 uint8_t param[6];
4697 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4698 return cli_smb2_set_ea_fnum(cli,
4699 fnum,
4700 ea_name,
4701 ea_val,
4702 ea_len);
4705 memset(param, 0, 6);
4706 SSVAL(param,0,fnum);
4707 SSVAL(param,2,SMB_INFO_SET_EA);
4709 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4710 ea_name, ea_val, ea_len);
4713 /*********************************************************
4714 Get an extended attribute list utility fn.
4715 *********************************************************/
4717 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4718 size_t rdata_len,
4719 size_t *pnum_eas, struct ea_struct **pea_list)
4721 struct ea_struct *ea_list = NULL;
4722 size_t num_eas;
4723 size_t ea_size;
4724 const uint8_t *p;
4726 if (rdata_len < 4) {
4727 return false;
4730 ea_size = (size_t)IVAL(rdata,0);
4731 if (ea_size > rdata_len) {
4732 return false;
4735 if (ea_size == 0) {
4736 /* No EA's present. */
4737 *pnum_eas = 0;
4738 *pea_list = NULL;
4739 return true;
4742 p = rdata + 4;
4743 ea_size -= 4;
4745 /* Validate the EA list and count it. */
4746 for (num_eas = 0; ea_size >= 4; num_eas++) {
4747 unsigned int ea_namelen = CVAL(p,1);
4748 unsigned int ea_valuelen = SVAL(p,2);
4749 if (ea_namelen == 0) {
4750 return false;
4752 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4753 return false;
4755 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4756 p += 4 + ea_namelen + 1 + ea_valuelen;
4759 if (num_eas == 0) {
4760 *pnum_eas = 0;
4761 *pea_list = NULL;
4762 return true;
4765 *pnum_eas = num_eas;
4766 if (!pea_list) {
4767 /* Caller only wants number of EA's. */
4768 return true;
4771 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4772 if (!ea_list) {
4773 return false;
4776 ea_size = (size_t)IVAL(rdata,0);
4777 p = rdata + 4;
4779 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4780 struct ea_struct *ea = &ea_list[num_eas];
4781 fstring unix_ea_name;
4782 unsigned int ea_namelen = CVAL(p,1);
4783 unsigned int ea_valuelen = SVAL(p,2);
4785 ea->flags = CVAL(p,0);
4786 unix_ea_name[0] = '\0';
4787 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4788 ea->name = talloc_strdup(ea_list, unix_ea_name);
4789 if (!ea->name) {
4790 goto fail;
4792 /* Ensure the value is null terminated (in case it's a string). */
4793 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4794 if (!ea->value.data) {
4795 goto fail;
4797 if (ea_valuelen) {
4798 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4800 ea->value.data[ea_valuelen] = 0;
4801 ea->value.length--;
4802 p += 4 + ea_namelen + 1 + ea_valuelen;
4805 *pea_list = ea_list;
4806 return true;
4808 fail:
4809 TALLOC_FREE(ea_list);
4810 return false;
4813 /*********************************************************
4814 Get an extended attribute list from a pathname.
4815 *********************************************************/
4817 struct cli_get_ea_list_path_state {
4818 uint32_t num_data;
4819 uint8_t *data;
4822 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4824 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4825 struct tevent_context *ev,
4826 struct cli_state *cli,
4827 const char *fname)
4829 struct tevent_req *req, *subreq;
4830 struct cli_get_ea_list_path_state *state;
4832 req = tevent_req_create(mem_ctx, &state,
4833 struct cli_get_ea_list_path_state);
4834 if (req == NULL) {
4835 return NULL;
4837 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4838 SMB_INFO_QUERY_ALL_EAS, 4,
4839 CLI_BUFFER_SIZE);
4840 if (tevent_req_nomem(subreq, req)) {
4841 return tevent_req_post(req, ev);
4843 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4844 return req;
4847 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4849 struct tevent_req *req = tevent_req_callback_data(
4850 subreq, struct tevent_req);
4851 struct cli_get_ea_list_path_state *state = tevent_req_data(
4852 req, struct cli_get_ea_list_path_state);
4853 NTSTATUS status;
4855 status = cli_qpathinfo_recv(subreq, state, &state->data,
4856 &state->num_data);
4857 TALLOC_FREE(subreq);
4858 if (tevent_req_nterror(req, status)) {
4859 return;
4861 tevent_req_done(req);
4864 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4865 size_t *pnum_eas, struct ea_struct **peas)
4867 struct cli_get_ea_list_path_state *state = tevent_req_data(
4868 req, struct cli_get_ea_list_path_state);
4869 NTSTATUS status;
4871 if (tevent_req_is_nterror(req, &status)) {
4872 return status;
4874 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4875 pnum_eas, peas)) {
4876 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4878 return NT_STATUS_OK;
4881 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4882 TALLOC_CTX *ctx,
4883 size_t *pnum_eas,
4884 struct ea_struct **pea_list)
4886 TALLOC_CTX *frame = NULL;
4887 struct tevent_context *ev = NULL;
4888 struct tevent_req *req = NULL;
4889 NTSTATUS status = NT_STATUS_NO_MEMORY;
4891 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
4892 return cli_smb2_get_ea_list_path(cli,
4893 path,
4894 ctx,
4895 pnum_eas,
4896 pea_list);
4899 frame = talloc_stackframe();
4901 if (smbXcli_conn_has_async_calls(cli->conn)) {
4903 * Can't use sync call while an async call is in flight
4905 status = NT_STATUS_INVALID_PARAMETER;
4906 goto fail;
4908 ev = samba_tevent_context_init(frame);
4909 if (ev == NULL) {
4910 goto fail;
4912 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4913 if (req == NULL) {
4914 goto fail;
4916 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4917 goto fail;
4919 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4920 fail:
4921 TALLOC_FREE(frame);
4922 return status;
4925 /****************************************************************************
4926 Convert open "flags" arg to uint32_t on wire.
4927 ****************************************************************************/
4929 static uint32_t open_flags_to_wire(int flags)
4931 int open_mode = flags & O_ACCMODE;
4932 uint32_t ret = 0;
4934 switch (open_mode) {
4935 case O_WRONLY:
4936 ret |= SMB_O_WRONLY;
4937 break;
4938 case O_RDWR:
4939 ret |= SMB_O_RDWR;
4940 break;
4941 default:
4942 case O_RDONLY:
4943 ret |= SMB_O_RDONLY;
4944 break;
4947 if (flags & O_CREAT) {
4948 ret |= SMB_O_CREAT;
4950 if (flags & O_EXCL) {
4951 ret |= SMB_O_EXCL;
4953 if (flags & O_TRUNC) {
4954 ret |= SMB_O_TRUNC;
4956 #if defined(O_SYNC)
4957 if (flags & O_SYNC) {
4958 ret |= SMB_O_SYNC;
4960 #endif /* O_SYNC */
4961 if (flags & O_APPEND) {
4962 ret |= SMB_O_APPEND;
4964 #if defined(O_DIRECT)
4965 if (flags & O_DIRECT) {
4966 ret |= SMB_O_DIRECT;
4968 #endif
4969 #if defined(O_DIRECTORY)
4970 if (flags & O_DIRECTORY) {
4971 ret |= SMB_O_DIRECTORY;
4973 #endif
4974 return ret;
4977 /****************************************************************************
4978 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4979 ****************************************************************************/
4981 struct posix_open_state {
4982 uint16_t setup;
4983 uint8_t *param;
4984 uint8_t data[18];
4985 uint16_t fnum; /* Out */
4988 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4990 struct tevent_req *req = tevent_req_callback_data(
4991 subreq, struct tevent_req);
4992 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4993 NTSTATUS status;
4994 uint8_t *data;
4995 uint32_t num_data;
4997 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4998 NULL, 0, NULL, &data, 12, &num_data);
4999 TALLOC_FREE(subreq);
5000 if (tevent_req_nterror(req, status)) {
5001 return;
5003 state->fnum = SVAL(data,2);
5004 tevent_req_done(req);
5007 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
5008 struct tevent_context *ev,
5009 struct cli_state *cli,
5010 const char *fname,
5011 int flags,
5012 mode_t mode,
5013 bool is_dir)
5015 struct tevent_req *req = NULL, *subreq = NULL;
5016 struct posix_open_state *state = NULL;
5017 uint32_t wire_flags = open_flags_to_wire(flags);
5019 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
5020 if (req == NULL) {
5021 return NULL;
5024 /* Setup setup word. */
5025 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
5027 /* Setup param array. */
5028 state->param = talloc_array(state, uint8_t, 6);
5029 if (tevent_req_nomem(state->param, req)) {
5030 return tevent_req_post(req, ev);
5032 memset(state->param, '\0', 6);
5033 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
5035 state->param = trans2_bytes_push_str(state->param, smbXcli_conn_use_unicode(cli->conn), fname,
5036 strlen(fname)+1, NULL);
5038 if (tevent_req_nomem(state->param, req)) {
5039 return tevent_req_post(req, ev);
5042 /* Setup data words. */
5043 if (is_dir) {
5044 wire_flags |= SMB_O_DIRECTORY;
5047 SIVAL(state->data,0,0); /* No oplock. */
5048 SIVAL(state->data,4,wire_flags);
5049 SIVAL(state->data,8,unix_perms_to_wire(mode));
5050 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
5051 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
5053 subreq = cli_trans_send(state, /* mem ctx. */
5054 ev, /* event ctx. */
5055 cli, /* cli_state. */
5056 0, /* additional_flags2 */
5057 SMBtrans2, /* cmd. */
5058 NULL, /* pipe name. */
5059 -1, /* fid. */
5060 0, /* function. */
5061 0, /* flags. */
5062 &state->setup, /* setup. */
5063 1, /* num setup uint16_t words. */
5064 0, /* max returned setup. */
5065 state->param, /* param. */
5066 talloc_get_size(state->param),/* num param. */
5067 2, /* max returned param. */
5068 state->data, /* data. */
5069 18, /* num data. */
5070 12); /* max returned data. */
5072 if (tevent_req_nomem(subreq, req)) {
5073 return tevent_req_post(req, ev);
5075 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
5076 return req;
5079 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
5080 struct tevent_context *ev,
5081 struct cli_state *cli,
5082 const char *fname,
5083 int flags,
5084 mode_t mode)
5086 return cli_posix_open_internal_send(mem_ctx, ev,
5087 cli, fname, flags, mode, false);
5090 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
5092 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
5093 NTSTATUS status;
5095 if (tevent_req_is_nterror(req, &status)) {
5096 return status;
5098 *pfnum = state->fnum;
5099 return NT_STATUS_OK;
5102 /****************************************************************************
5103 Open - POSIX semantics. Doesn't request oplock.
5104 ****************************************************************************/
5106 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
5107 int flags, mode_t mode, uint16_t *pfnum)
5110 TALLOC_CTX *frame = talloc_stackframe();
5111 struct tevent_context *ev = NULL;
5112 struct tevent_req *req = NULL;
5113 NTSTATUS status = NT_STATUS_OK;
5115 if (smbXcli_conn_has_async_calls(cli->conn)) {
5117 * Can't use sync call while an async call is in flight
5119 status = NT_STATUS_INVALID_PARAMETER;
5120 goto fail;
5123 ev = samba_tevent_context_init(frame);
5124 if (ev == NULL) {
5125 status = NT_STATUS_NO_MEMORY;
5126 goto fail;
5129 req = cli_posix_open_send(frame,
5131 cli,
5132 fname,
5133 flags,
5134 mode);
5135 if (req == NULL) {
5136 status = NT_STATUS_NO_MEMORY;
5137 goto fail;
5140 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5141 goto fail;
5144 status = cli_posix_open_recv(req, pfnum);
5146 fail:
5147 TALLOC_FREE(frame);
5148 return status;
5151 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
5152 struct tevent_context *ev,
5153 struct cli_state *cli,
5154 const char *fname,
5155 mode_t mode)
5157 return cli_posix_open_internal_send(mem_ctx, ev,
5158 cli, fname, O_CREAT, mode, true);
5161 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
5163 return tevent_req_simple_recv_ntstatus(req);
5166 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
5168 TALLOC_CTX *frame = talloc_stackframe();
5169 struct tevent_context *ev = NULL;
5170 struct tevent_req *req = NULL;
5171 NTSTATUS status = NT_STATUS_OK;
5173 if (smbXcli_conn_has_async_calls(cli->conn)) {
5175 * Can't use sync call while an async call is in flight
5177 status = NT_STATUS_INVALID_PARAMETER;
5178 goto fail;
5181 ev = samba_tevent_context_init(frame);
5182 if (ev == NULL) {
5183 status = NT_STATUS_NO_MEMORY;
5184 goto fail;
5187 req = cli_posix_mkdir_send(frame,
5189 cli,
5190 fname,
5191 mode);
5192 if (req == NULL) {
5193 status = NT_STATUS_NO_MEMORY;
5194 goto fail;
5197 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5198 goto fail;
5201 status = cli_posix_mkdir_recv(req);
5203 fail:
5204 TALLOC_FREE(frame);
5205 return status;
5208 /****************************************************************************
5209 unlink or rmdir - POSIX semantics.
5210 ****************************************************************************/
5212 struct cli_posix_unlink_internal_state {
5213 uint8_t data[2];
5216 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
5218 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
5219 struct tevent_context *ev,
5220 struct cli_state *cli,
5221 const char *fname,
5222 uint16_t level)
5224 struct tevent_req *req = NULL, *subreq = NULL;
5225 struct cli_posix_unlink_internal_state *state = NULL;
5227 req = tevent_req_create(mem_ctx, &state,
5228 struct cli_posix_unlink_internal_state);
5229 if (req == NULL) {
5230 return NULL;
5233 /* Setup data word. */
5234 SSVAL(state->data, 0, level);
5236 subreq = cli_setpathinfo_send(state, ev, cli,
5237 SMB_POSIX_PATH_UNLINK,
5238 fname,
5239 state->data, sizeof(state->data));
5240 if (tevent_req_nomem(subreq, req)) {
5241 return tevent_req_post(req, ev);
5243 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
5244 return req;
5247 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
5249 NTSTATUS status = cli_setpathinfo_recv(subreq);
5250 tevent_req_simple_finish_ntstatus(subreq, status);
5253 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
5254 struct tevent_context *ev,
5255 struct cli_state *cli,
5256 const char *fname)
5258 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
5259 SMB_POSIX_UNLINK_FILE_TARGET);
5262 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
5264 return tevent_req_simple_recv_ntstatus(req);
5267 /****************************************************************************
5268 unlink - POSIX semantics.
5269 ****************************************************************************/
5271 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
5273 TALLOC_CTX *frame = talloc_stackframe();
5274 struct tevent_context *ev = NULL;
5275 struct tevent_req *req = NULL;
5276 NTSTATUS status = NT_STATUS_OK;
5278 if (smbXcli_conn_has_async_calls(cli->conn)) {
5280 * Can't use sync call while an async call is in flight
5282 status = NT_STATUS_INVALID_PARAMETER;
5283 goto fail;
5286 ev = samba_tevent_context_init(frame);
5287 if (ev == NULL) {
5288 status = NT_STATUS_NO_MEMORY;
5289 goto fail;
5292 req = cli_posix_unlink_send(frame,
5294 cli,
5295 fname);
5296 if (req == NULL) {
5297 status = NT_STATUS_NO_MEMORY;
5298 goto fail;
5301 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5302 goto fail;
5305 status = cli_posix_unlink_recv(req);
5307 fail:
5308 TALLOC_FREE(frame);
5309 return status;
5312 /****************************************************************************
5313 rmdir - POSIX semantics.
5314 ****************************************************************************/
5316 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
5317 struct tevent_context *ev,
5318 struct cli_state *cli,
5319 const char *fname)
5321 return cli_posix_unlink_internal_send(
5322 mem_ctx, ev, cli, fname,
5323 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
5326 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
5328 return tevent_req_simple_recv_ntstatus(req);
5331 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
5333 TALLOC_CTX *frame = talloc_stackframe();
5334 struct tevent_context *ev = NULL;
5335 struct tevent_req *req = NULL;
5336 NTSTATUS status = NT_STATUS_OK;
5338 if (smbXcli_conn_has_async_calls(cli->conn)) {
5340 * Can't use sync call while an async call is in flight
5342 status = NT_STATUS_INVALID_PARAMETER;
5343 goto fail;
5346 ev = samba_tevent_context_init(frame);
5347 if (ev == NULL) {
5348 status = NT_STATUS_NO_MEMORY;
5349 goto fail;
5352 req = cli_posix_rmdir_send(frame,
5354 cli,
5355 fname);
5356 if (req == NULL) {
5357 status = NT_STATUS_NO_MEMORY;
5358 goto fail;
5361 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5362 goto fail;
5365 status = cli_posix_rmdir_recv(req, frame);
5367 fail:
5368 TALLOC_FREE(frame);
5369 return status;
5372 /****************************************************************************
5373 filechangenotify
5374 ****************************************************************************/
5376 struct cli_notify_state {
5377 uint8_t setup[8];
5378 uint32_t num_changes;
5379 struct notify_change *changes;
5382 static void cli_notify_done(struct tevent_req *subreq);
5384 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5385 struct tevent_context *ev,
5386 struct cli_state *cli, uint16_t fnum,
5387 uint32_t buffer_size,
5388 uint32_t completion_filter, bool recursive)
5390 struct tevent_req *req, *subreq;
5391 struct cli_notify_state *state;
5392 unsigned old_timeout;
5394 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5395 if (req == NULL) {
5396 return NULL;
5399 SIVAL(state->setup, 0, completion_filter);
5400 SSVAL(state->setup, 4, fnum);
5401 SSVAL(state->setup, 6, recursive);
5404 * Notifies should not time out
5406 old_timeout = cli_set_timeout(cli, 0);
5408 subreq = cli_trans_send(
5409 state, /* mem ctx. */
5410 ev, /* event ctx. */
5411 cli, /* cli_state. */
5412 0, /* additional_flags2 */
5413 SMBnttrans, /* cmd. */
5414 NULL, /* pipe name. */
5415 -1, /* fid. */
5416 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5417 0, /* flags. */
5418 (uint16_t *)state->setup, /* setup. */
5419 4, /* num setup uint16_t words. */
5420 0, /* max returned setup. */
5421 NULL, /* param. */
5422 0, /* num param. */
5423 buffer_size, /* max returned param. */
5424 NULL, /* data. */
5425 0, /* num data. */
5426 0); /* max returned data. */
5428 cli_set_timeout(cli, old_timeout);
5430 if (tevent_req_nomem(subreq, req)) {
5431 return tevent_req_post(req, ev);
5433 tevent_req_set_callback(subreq, cli_notify_done, req);
5434 return req;
5437 static void cli_notify_done(struct tevent_req *subreq)
5439 struct tevent_req *req = tevent_req_callback_data(
5440 subreq, struct tevent_req);
5441 struct cli_notify_state *state = tevent_req_data(
5442 req, struct cli_notify_state);
5443 NTSTATUS status;
5444 uint8_t *params;
5445 uint32_t i, ofs, num_params;
5446 uint16_t flags2;
5448 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5449 &params, 0, &num_params, NULL, 0, NULL);
5450 TALLOC_FREE(subreq);
5451 if (tevent_req_nterror(req, status)) {
5452 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5453 return;
5456 state->num_changes = 0;
5457 ofs = 0;
5459 while (num_params - ofs > 12) {
5460 uint32_t next = IVAL(params, ofs);
5461 state->num_changes += 1;
5463 if ((next == 0) || (ofs+next >= num_params)) {
5464 break;
5466 ofs += next;
5469 state->changes = talloc_array(state, struct notify_change,
5470 state->num_changes);
5471 if (tevent_req_nomem(state->changes, req)) {
5472 TALLOC_FREE(params);
5473 return;
5476 ofs = 0;
5478 for (i=0; i<state->num_changes; i++) {
5479 uint32_t next = IVAL(params, ofs);
5480 uint32_t len = IVAL(params, ofs+8);
5481 ssize_t ret;
5482 char *name;
5484 if (trans_oob(num_params, ofs + 12, len)) {
5485 TALLOC_FREE(params);
5486 tevent_req_nterror(
5487 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5488 return;
5491 state->changes[i].action = IVAL(params, ofs+4);
5492 ret = clistr_pull_talloc(state->changes, (char *)params, flags2,
5493 &name, params+ofs+12, len,
5494 STR_TERMINATE|STR_UNICODE);
5495 if (ret == -1) {
5496 TALLOC_FREE(params);
5497 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5498 return;
5500 state->changes[i].name = name;
5501 ofs += next;
5504 TALLOC_FREE(params);
5505 tevent_req_done(req);
5508 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5509 uint32_t *pnum_changes,
5510 struct notify_change **pchanges)
5512 struct cli_notify_state *state = tevent_req_data(
5513 req, struct cli_notify_state);
5514 NTSTATUS status;
5516 if (tevent_req_is_nterror(req, &status)) {
5517 return status;
5520 *pnum_changes = state->num_changes;
5521 *pchanges = talloc_move(mem_ctx, &state->changes);
5522 return NT_STATUS_OK;
5525 NTSTATUS cli_notify(struct cli_state *cli, uint16_t fnum, uint32_t buffer_size,
5526 uint32_t completion_filter, bool recursive,
5527 TALLOC_CTX *mem_ctx, uint32_t *pnum_changes,
5528 struct notify_change **pchanges)
5530 TALLOC_CTX *frame = talloc_stackframe();
5531 struct tevent_context *ev;
5532 struct tevent_req *req;
5533 NTSTATUS status = NT_STATUS_NO_MEMORY;
5535 if (smbXcli_conn_has_async_calls(cli->conn)) {
5537 * Can't use sync call while an async call is in flight
5539 status = NT_STATUS_INVALID_PARAMETER;
5540 goto fail;
5542 ev = samba_tevent_context_init(frame);
5543 if (ev == NULL) {
5544 goto fail;
5546 req = cli_notify_send(ev, ev, cli, fnum, buffer_size,
5547 completion_filter, recursive);
5548 if (req == NULL) {
5549 goto fail;
5551 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5552 goto fail;
5554 status = cli_notify_recv(req, mem_ctx, pnum_changes, pchanges);
5555 fail:
5556 TALLOC_FREE(frame);
5557 return status;
5560 struct cli_qpathinfo_state {
5561 uint8_t *param;
5562 uint8_t *data;
5563 uint16_t setup[1];
5564 uint32_t min_rdata;
5565 uint8_t *rdata;
5566 uint32_t num_rdata;
5569 static void cli_qpathinfo_done(struct tevent_req *subreq);
5571 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5572 struct tevent_context *ev,
5573 struct cli_state *cli, const char *fname,
5574 uint16_t level, uint32_t min_rdata,
5575 uint32_t max_rdata)
5577 struct tevent_req *req, *subreq;
5578 struct cli_qpathinfo_state *state;
5579 uint16_t additional_flags2 = 0;
5581 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5582 if (req == NULL) {
5583 return NULL;
5585 state->min_rdata = min_rdata;
5586 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5588 state->param = talloc_zero_array(state, uint8_t, 6);
5589 if (tevent_req_nomem(state->param, req)) {
5590 return tevent_req_post(req, ev);
5592 SSVAL(state->param, 0, level);
5593 state->param = trans2_bytes_push_str(
5594 state->param, smbXcli_conn_use_unicode(cli->conn), fname, strlen(fname)+1, NULL);
5595 if (tevent_req_nomem(state->param, req)) {
5596 return tevent_req_post(req, ev);
5599 if (clistr_is_previous_version_path(fname) &&
5600 !INFO_LEVEL_IS_UNIX(level)) {
5601 additional_flags2 = FLAGS2_REPARSE_PATH;
5604 subreq = cli_trans_send(
5605 state, /* mem ctx. */
5606 ev, /* event ctx. */
5607 cli, /* cli_state. */
5608 additional_flags2, /* additional_flags2 */
5609 SMBtrans2, /* cmd. */
5610 NULL, /* pipe name. */
5611 -1, /* fid. */
5612 0, /* function. */
5613 0, /* flags. */
5614 state->setup, /* setup. */
5615 1, /* num setup uint16_t words. */
5616 0, /* max returned setup. */
5617 state->param, /* param. */
5618 talloc_get_size(state->param), /* num param. */
5619 2, /* max returned param. */
5620 NULL, /* data. */
5621 0, /* num data. */
5622 max_rdata); /* max returned data. */
5624 if (tevent_req_nomem(subreq, req)) {
5625 return tevent_req_post(req, ev);
5627 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5628 return req;
5631 static void cli_qpathinfo_done(struct tevent_req *subreq)
5633 struct tevent_req *req = tevent_req_callback_data(
5634 subreq, struct tevent_req);
5635 struct cli_qpathinfo_state *state = tevent_req_data(
5636 req, struct cli_qpathinfo_state);
5637 NTSTATUS status;
5639 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5640 NULL, 0, NULL,
5641 &state->rdata, state->min_rdata,
5642 &state->num_rdata);
5643 if (tevent_req_nterror(req, status)) {
5644 return;
5646 tevent_req_done(req);
5649 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5650 uint8_t **rdata, uint32_t *num_rdata)
5652 struct cli_qpathinfo_state *state = tevent_req_data(
5653 req, struct cli_qpathinfo_state);
5654 NTSTATUS status;
5656 if (tevent_req_is_nterror(req, &status)) {
5657 return status;
5659 if (rdata != NULL) {
5660 *rdata = talloc_move(mem_ctx, &state->rdata);
5661 } else {
5662 TALLOC_FREE(state->rdata);
5664 if (num_rdata != NULL) {
5665 *num_rdata = state->num_rdata;
5667 return NT_STATUS_OK;
5670 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5671 const char *fname, uint16_t level, uint32_t min_rdata,
5672 uint32_t max_rdata,
5673 uint8_t **rdata, uint32_t *num_rdata)
5675 TALLOC_CTX *frame = talloc_stackframe();
5676 struct tevent_context *ev;
5677 struct tevent_req *req;
5678 NTSTATUS status = NT_STATUS_NO_MEMORY;
5680 if (smbXcli_conn_has_async_calls(cli->conn)) {
5682 * Can't use sync call while an async call is in flight
5684 status = NT_STATUS_INVALID_PARAMETER;
5685 goto fail;
5687 ev = samba_tevent_context_init(frame);
5688 if (ev == NULL) {
5689 goto fail;
5691 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5692 max_rdata);
5693 if (req == NULL) {
5694 goto fail;
5696 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5697 goto fail;
5699 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5700 fail:
5701 TALLOC_FREE(frame);
5702 return status;
5705 struct cli_qfileinfo_state {
5706 uint16_t setup[1];
5707 uint8_t param[4];
5708 uint8_t *data;
5709 uint16_t recv_flags2;
5710 uint32_t min_rdata;
5711 uint8_t *rdata;
5712 uint32_t num_rdata;
5715 static void cli_qfileinfo_done(struct tevent_req *subreq);
5717 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5718 struct tevent_context *ev,
5719 struct cli_state *cli, uint16_t fnum,
5720 uint16_t level, uint32_t min_rdata,
5721 uint32_t max_rdata)
5723 struct tevent_req *req, *subreq;
5724 struct cli_qfileinfo_state *state;
5726 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5727 if (req == NULL) {
5728 return NULL;
5730 state->min_rdata = min_rdata;
5731 SSVAL(state->param, 0, fnum);
5732 SSVAL(state->param, 2, level);
5733 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5735 subreq = cli_trans_send(
5736 state, /* mem ctx. */
5737 ev, /* event ctx. */
5738 cli, /* cli_state. */
5739 0, /* additional_flags2 */
5740 SMBtrans2, /* cmd. */
5741 NULL, /* pipe name. */
5742 -1, /* fid. */
5743 0, /* function. */
5744 0, /* flags. */
5745 state->setup, /* setup. */
5746 1, /* num setup uint16_t words. */
5747 0, /* max returned setup. */
5748 state->param, /* param. */
5749 sizeof(state->param), /* num param. */
5750 2, /* max returned param. */
5751 NULL, /* data. */
5752 0, /* num data. */
5753 max_rdata); /* max returned data. */
5755 if (tevent_req_nomem(subreq, req)) {
5756 return tevent_req_post(req, ev);
5758 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5759 return req;
5762 static void cli_qfileinfo_done(struct tevent_req *subreq)
5764 struct tevent_req *req = tevent_req_callback_data(
5765 subreq, struct tevent_req);
5766 struct cli_qfileinfo_state *state = tevent_req_data(
5767 req, struct cli_qfileinfo_state);
5768 NTSTATUS status;
5770 status = cli_trans_recv(subreq, state,
5771 &state->recv_flags2,
5772 NULL, 0, NULL,
5773 NULL, 0, NULL,
5774 &state->rdata, state->min_rdata,
5775 &state->num_rdata);
5776 if (tevent_req_nterror(req, status)) {
5777 return;
5779 tevent_req_done(req);
5782 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5783 uint16_t *recv_flags2,
5784 uint8_t **rdata, uint32_t *num_rdata)
5786 struct cli_qfileinfo_state *state = tevent_req_data(
5787 req, struct cli_qfileinfo_state);
5788 NTSTATUS status;
5790 if (tevent_req_is_nterror(req, &status)) {
5791 return status;
5794 if (recv_flags2 != NULL) {
5795 *recv_flags2 = state->recv_flags2;
5797 if (rdata != NULL) {
5798 *rdata = talloc_move(mem_ctx, &state->rdata);
5799 } else {
5800 TALLOC_FREE(state->rdata);
5802 if (num_rdata != NULL) {
5803 *num_rdata = state->num_rdata;
5805 return NT_STATUS_OK;
5808 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5809 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5810 uint32_t max_rdata, uint16_t *recv_flags2,
5811 uint8_t **rdata, uint32_t *num_rdata)
5813 TALLOC_CTX *frame = talloc_stackframe();
5814 struct tevent_context *ev;
5815 struct tevent_req *req;
5816 NTSTATUS status = NT_STATUS_NO_MEMORY;
5818 if (smbXcli_conn_has_async_calls(cli->conn)) {
5820 * Can't use sync call while an async call is in flight
5822 status = NT_STATUS_INVALID_PARAMETER;
5823 goto fail;
5825 ev = samba_tevent_context_init(frame);
5826 if (ev == NULL) {
5827 goto fail;
5829 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5830 max_rdata);
5831 if (req == NULL) {
5832 goto fail;
5834 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5835 goto fail;
5837 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5838 fail:
5839 TALLOC_FREE(frame);
5840 return status;
5843 struct cli_flush_state {
5844 uint16_t vwv[1];
5847 static void cli_flush_done(struct tevent_req *subreq);
5849 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5850 struct tevent_context *ev,
5851 struct cli_state *cli,
5852 uint16_t fnum)
5854 struct tevent_req *req, *subreq;
5855 struct cli_flush_state *state;
5857 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5858 if (req == NULL) {
5859 return NULL;
5861 SSVAL(state->vwv + 0, 0, fnum);
5863 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 0, 1, state->vwv,
5864 0, NULL);
5865 if (tevent_req_nomem(subreq, req)) {
5866 return tevent_req_post(req, ev);
5868 tevent_req_set_callback(subreq, cli_flush_done, req);
5869 return req;
5872 static void cli_flush_done(struct tevent_req *subreq)
5874 struct tevent_req *req = tevent_req_callback_data(
5875 subreq, struct tevent_req);
5876 NTSTATUS status;
5878 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5879 TALLOC_FREE(subreq);
5880 if (tevent_req_nterror(req, status)) {
5881 return;
5883 tevent_req_done(req);
5886 NTSTATUS cli_flush_recv(struct tevent_req *req)
5888 return tevent_req_simple_recv_ntstatus(req);
5891 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5893 TALLOC_CTX *frame = talloc_stackframe();
5894 struct tevent_context *ev;
5895 struct tevent_req *req;
5896 NTSTATUS status = NT_STATUS_NO_MEMORY;
5898 if (smbXcli_conn_has_async_calls(cli->conn)) {
5900 * Can't use sync call while an async call is in flight
5902 status = NT_STATUS_INVALID_PARAMETER;
5903 goto fail;
5905 ev = samba_tevent_context_init(frame);
5906 if (ev == NULL) {
5907 goto fail;
5909 req = cli_flush_send(frame, ev, cli, fnum);
5910 if (req == NULL) {
5911 goto fail;
5913 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5914 goto fail;
5916 status = cli_flush_recv(req);
5917 fail:
5918 TALLOC_FREE(frame);
5919 return status;
5922 struct cli_shadow_copy_data_state {
5923 uint16_t setup[4];
5924 uint8_t *data;
5925 uint32_t num_data;
5926 bool get_names;
5929 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5931 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5932 struct tevent_context *ev,
5933 struct cli_state *cli,
5934 uint16_t fnum,
5935 bool get_names)
5937 struct tevent_req *req, *subreq;
5938 struct cli_shadow_copy_data_state *state;
5939 uint32_t ret_size;
5941 req = tevent_req_create(mem_ctx, &state,
5942 struct cli_shadow_copy_data_state);
5943 if (req == NULL) {
5944 return NULL;
5946 state->get_names = get_names;
5947 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5949 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5950 SSVAL(state->setup + 2, 0, fnum);
5951 SCVAL(state->setup + 3, 0, 1); /* isFsctl */
5952 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5954 subreq = cli_trans_send(
5955 state, ev, cli, 0, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5956 state->setup, ARRAY_SIZE(state->setup), 0,
5957 NULL, 0, 0,
5958 NULL, 0, ret_size);
5959 if (tevent_req_nomem(subreq, req)) {
5960 return tevent_req_post(req, ev);
5962 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5963 return req;
5966 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5968 struct tevent_req *req = tevent_req_callback_data(
5969 subreq, struct tevent_req);
5970 struct cli_shadow_copy_data_state *state = tevent_req_data(
5971 req, struct cli_shadow_copy_data_state);
5972 NTSTATUS status;
5974 status = cli_trans_recv(subreq, state, NULL,
5975 NULL, 0, NULL, /* setup */
5976 NULL, 0, NULL, /* param */
5977 &state->data, 12, &state->num_data);
5978 TALLOC_FREE(subreq);
5979 if (tevent_req_nterror(req, status)) {
5980 return;
5982 tevent_req_done(req);
5985 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5986 char ***pnames, int *pnum_names)
5988 struct cli_shadow_copy_data_state *state = tevent_req_data(
5989 req, struct cli_shadow_copy_data_state);
5990 char **names;
5991 int i, num_names;
5992 uint32_t dlength;
5993 NTSTATUS status;
5995 if (tevent_req_is_nterror(req, &status)) {
5996 return status;
5998 num_names = IVAL(state->data, 4);
5999 dlength = IVAL(state->data, 8);
6001 if (!state->get_names) {
6002 *pnum_names = num_names;
6003 return NT_STATUS_OK;
6006 if (dlength+12 > state->num_data) {
6007 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6009 names = talloc_array(mem_ctx, char *, num_names);
6010 if (names == NULL) {
6011 return NT_STATUS_NO_MEMORY;
6014 for (i=0; i<num_names; i++) {
6015 bool ret;
6016 uint8_t *src;
6017 size_t converted_size;
6019 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
6020 ret = convert_string_talloc(
6021 names, CH_UTF16LE, CH_UNIX,
6022 src, 2 * sizeof(SHADOW_COPY_LABEL),
6023 &names[i], &converted_size);
6024 if (!ret) {
6025 TALLOC_FREE(names);
6026 return NT_STATUS_INVALID_NETWORK_RESPONSE;
6029 *pnum_names = num_names;
6030 *pnames = names;
6031 return NT_STATUS_OK;
6034 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
6035 uint16_t fnum, bool get_names,
6036 char ***pnames, int *pnum_names)
6038 TALLOC_CTX *frame = talloc_stackframe();
6039 struct tevent_context *ev;
6040 struct tevent_req *req;
6041 NTSTATUS status = NT_STATUS_NO_MEMORY;
6043 if (smbXcli_conn_has_async_calls(cli->conn)) {
6045 * Can't use sync call while an async call is in flight
6047 status = NT_STATUS_INVALID_PARAMETER;
6048 goto fail;
6050 ev = samba_tevent_context_init(frame);
6051 if (ev == NULL) {
6052 goto fail;
6054 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
6055 if (req == NULL) {
6056 goto fail;
6058 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
6059 goto fail;
6061 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
6062 fail:
6063 TALLOC_FREE(frame);
6064 return status;