s4-tests: Password tests now use the get_dsheuristics and set_dsheuristics from SamDB.
[Samba.git] / source3 / libsmb / clifile.c
blobb40f7d15d99ab1a70d806c17c07b3f1148d1c28b
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "async_smb.h"
24 /***********************************************************
25 Common function for pushing stings, used by smb_bytes_push_str()
26 and trans_bytes_push_str(). Only difference is the align_odd
27 parameter setting.
28 ***********************************************************/
30 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
31 const char *str, size_t str_len,
32 bool align_odd,
33 size_t *pconverted_size)
35 size_t buflen;
36 char *converted;
37 size_t converted_size;
39 if (buf == NULL) {
40 return NULL;
43 buflen = talloc_get_size(buf);
45 if (align_odd && ucs2 && (buflen % 2 == 0)) {
47 * We're pushing into an SMB buffer, align odd
49 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
50 if (buf == NULL) {
51 return NULL;
53 buf[buflen] = '\0';
54 buflen += 1;
57 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
58 ucs2 ? CH_UTF16LE : CH_DOS,
59 str, str_len, &converted,
60 &converted_size, true)) {
61 return NULL;
64 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
65 buflen + converted_size);
66 if (buf == NULL) {
67 TALLOC_FREE(converted);
68 return NULL;
71 memcpy(buf + buflen, converted, converted_size);
73 TALLOC_FREE(converted);
75 if (pconverted_size) {
76 *pconverted_size = converted_size;
79 return buf;
82 /***********************************************************
83 Push a string into an SMB buffer, with odd byte alignment
84 if it's a UCS2 string.
85 ***********************************************************/
87 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
88 const char *str, size_t str_len,
89 size_t *pconverted_size)
91 return internal_bytes_push_str(buf, ucs2, str, str_len,
92 true, pconverted_size);
95 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
96 const uint8_t *bytes, size_t num_bytes)
98 size_t buflen;
100 if (buf == NULL) {
101 return NULL;
103 buflen = talloc_get_size(buf);
105 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
106 buflen + 1 + num_bytes);
107 if (buf == NULL) {
108 return NULL;
110 buf[buflen] = prefix;
111 memcpy(&buf[buflen+1], bytes, num_bytes);
112 return buf;
115 /***********************************************************
116 Same as smb_bytes_push_str(), but without the odd byte
117 align for ucs2 (we're pushing into a param or data block).
118 static for now, although this will probably change when
119 other modules use async trans calls.
120 ***********************************************************/
122 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
123 const char *str, size_t str_len,
124 size_t *pconverted_size)
126 return internal_bytes_push_str(buf, ucs2, str, str_len,
127 false, pconverted_size);
130 struct cli_setpathinfo_state {
131 uint16_t setup;
132 uint8_t *param;
135 static void cli_setpathinfo_done(struct tevent_req *subreq);
137 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
138 struct tevent_context *ev,
139 struct cli_state *cli,
140 uint16_t level,
141 const char *path,
142 uint8_t *data,
143 size_t data_len)
145 struct tevent_req *req, *subreq;
146 struct cli_setpathinfo_state *state;
148 req = tevent_req_create(mem_ctx, &state,
149 struct cli_setpathinfo_state);
150 if (req == NULL) {
151 return NULL;
154 /* Setup setup word. */
155 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
157 /* Setup param array. */
158 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
159 if (tevent_req_nomem(state->param, req)) {
160 return tevent_req_post(req, ev);
162 SSVAL(state->param, 0, level);
164 state->param = trans2_bytes_push_str(
165 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
166 if (tevent_req_nomem(state->param, req)) {
167 return tevent_req_post(req, ev);
170 subreq = cli_trans_send(
171 state, /* mem ctx. */
172 ev, /* event ctx. */
173 cli, /* cli_state. */
174 SMBtrans2, /* cmd. */
175 NULL, /* pipe name. */
176 -1, /* fid. */
177 0, /* function. */
178 0, /* flags. */
179 &state->setup, /* setup. */
180 1, /* num setup uint16_t words. */
181 0, /* max returned setup. */
182 state->param, /* param. */
183 talloc_get_size(state->param), /* num param. */
184 2, /* max returned param. */
185 data, /* data. */
186 data_len, /* num data. */
187 0); /* max returned data. */
189 if (tevent_req_nomem(subreq, req)) {
190 return tevent_req_post(req, ev);
192 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
193 return req;
196 static void cli_setpathinfo_done(struct tevent_req *subreq)
198 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
199 NULL, 0, NULL, NULL, 0, NULL);
200 tevent_req_simple_finish_ntstatus(subreq, status);
203 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
205 return tevent_req_simple_recv_ntstatus(req);
208 /****************************************************************************
209 Hard/Symlink a file (UNIX extensions).
210 Creates new name (sym)linked to oldname.
211 ****************************************************************************/
213 struct cli_posix_link_internal_state {
214 uint8_t *data;
217 static void cli_posix_link_internal_done(struct tevent_req *subreq);
219 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
220 struct event_context *ev,
221 struct cli_state *cli,
222 uint16_t level,
223 const char *oldname,
224 const char *newname)
226 struct tevent_req *req = NULL, *subreq = NULL;
227 struct cli_posix_link_internal_state *state = NULL;
229 req = tevent_req_create(mem_ctx, &state,
230 struct cli_posix_link_internal_state);
231 if (req == NULL) {
232 return NULL;
235 /* Setup data array. */
236 state->data = talloc_array(state, uint8_t, 0);
237 if (tevent_req_nomem(state->data, req)) {
238 return tevent_req_post(req, ev);
240 state->data = trans2_bytes_push_str(
241 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
243 subreq = cli_setpathinfo_send(
244 state, ev, cli, level, newname,
245 state->data, talloc_get_size(state->data));
246 if (tevent_req_nomem(subreq, req)) {
247 return tevent_req_post(req, ev);
249 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
250 return req;
253 static void cli_posix_link_internal_done(struct tevent_req *subreq)
255 NTSTATUS status = cli_setpathinfo_recv(subreq);
256 tevent_req_simple_finish_ntstatus(subreq, status);
259 /****************************************************************************
260 Symlink a file (UNIX extensions).
261 ****************************************************************************/
263 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
264 struct event_context *ev,
265 struct cli_state *cli,
266 const char *oldname,
267 const char *newname)
269 return cli_posix_link_internal_send(
270 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
273 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
275 return tevent_req_simple_recv_ntstatus(req);
278 NTSTATUS cli_posix_symlink(struct cli_state *cli,
279 const char *oldname,
280 const char *newname)
282 TALLOC_CTX *frame = talloc_stackframe();
283 struct event_context *ev = NULL;
284 struct tevent_req *req = NULL;
285 NTSTATUS status = NT_STATUS_OK;
287 if (cli_has_async_calls(cli)) {
289 * Can't use sync call while an async call is in flight
291 status = NT_STATUS_INVALID_PARAMETER;
292 goto fail;
295 ev = event_context_init(frame);
296 if (ev == NULL) {
297 status = NT_STATUS_NO_MEMORY;
298 goto fail;
301 req = cli_posix_symlink_send(frame,
303 cli,
304 oldname,
305 newname);
306 if (req == NULL) {
307 status = NT_STATUS_NO_MEMORY;
308 goto fail;
311 if (!tevent_req_poll(req, ev)) {
312 status = map_nt_error_from_unix(errno);
313 goto fail;
316 status = cli_posix_symlink_recv(req);
318 fail:
319 TALLOC_FREE(frame);
320 if (!NT_STATUS_IS_OK(status)) {
321 cli_set_error(cli, status);
323 return status;
326 /****************************************************************************
327 Read a POSIX symlink.
328 ****************************************************************************/
330 struct readlink_state {
331 uint8_t *data;
332 uint32_t num_data;
335 static void cli_posix_readlink_done(struct tevent_req *subreq);
337 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
338 struct event_context *ev,
339 struct cli_state *cli,
340 const char *fname,
341 size_t len)
343 struct tevent_req *req = NULL, *subreq = NULL;
344 struct readlink_state *state = NULL;
345 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
347 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
348 if (req == NULL) {
349 return NULL;
353 * Len is in bytes, we need it in UCS2 units.
355 if ((2*len < len) || (maxbytelen < len)) {
356 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
357 return tevent_req_post(req, ev);
360 subreq = cli_qpathinfo_send(state, ev, cli, fname,
361 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
362 if (tevent_req_nomem(subreq, req)) {
363 return tevent_req_post(req, ev);
365 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
366 return req;
369 static void cli_posix_readlink_done(struct tevent_req *subreq)
371 struct tevent_req *req = tevent_req_callback_data(
372 subreq, struct tevent_req);
373 struct readlink_state *state = tevent_req_data(
374 req, struct readlink_state);
375 NTSTATUS status;
377 status = cli_qpathinfo_recv(subreq, state, &state->data,
378 &state->num_data);
379 TALLOC_FREE(subreq);
380 if (!NT_STATUS_IS_OK(status)) {
381 tevent_req_nterror(req, status);
382 return;
385 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
387 if (state->data[state->num_data-1] != '\0') {
388 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
389 return;
391 tevent_req_done(req);
394 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
395 char *retpath, size_t len)
397 NTSTATUS status;
398 char *converted = NULL;
399 size_t converted_size = 0;
400 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
402 if (tevent_req_is_nterror(req, &status)) {
403 return status;
405 /* The returned data is a pushed string, not raw data. */
406 if (!convert_string_talloc(state,
407 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
408 CH_UNIX,
409 state->data,
410 state->num_data,
411 &converted,
412 &converted_size,
413 true)) {
414 return NT_STATUS_NO_MEMORY;
417 len = MIN(len,converted_size);
418 if (len == 0) {
419 return NT_STATUS_DATA_ERROR;
421 memcpy(retpath, converted, len);
422 return NT_STATUS_OK;
425 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
426 char *linkpath, size_t len)
428 TALLOC_CTX *frame = talloc_stackframe();
429 struct event_context *ev = NULL;
430 struct tevent_req *req = NULL;
431 NTSTATUS status = NT_STATUS_OK;
433 if (cli_has_async_calls(cli)) {
435 * Can't use sync call while an async call is in flight
437 status = NT_STATUS_INVALID_PARAMETER;
438 goto fail;
441 ev = event_context_init(frame);
442 if (ev == NULL) {
443 status = NT_STATUS_NO_MEMORY;
444 goto fail;
447 req = cli_posix_readlink_send(frame,
449 cli,
450 fname,
451 len);
452 if (req == NULL) {
453 status = NT_STATUS_NO_MEMORY;
454 goto fail;
457 if (!tevent_req_poll(req, ev)) {
458 status = map_nt_error_from_unix(errno);
459 goto fail;
462 status = cli_posix_readlink_recv(req, cli, linkpath, len);
464 fail:
465 TALLOC_FREE(frame);
466 if (!NT_STATUS_IS_OK(status)) {
467 cli_set_error(cli, status);
469 return status;
472 /****************************************************************************
473 Hard link a file (UNIX extensions).
474 ****************************************************************************/
476 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
477 struct event_context *ev,
478 struct cli_state *cli,
479 const char *oldname,
480 const char *newname)
482 return cli_posix_link_internal_send(
483 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
486 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
488 return tevent_req_simple_recv_ntstatus(req);
491 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
492 const char *oldname,
493 const char *newname)
495 TALLOC_CTX *frame = talloc_stackframe();
496 struct event_context *ev = NULL;
497 struct tevent_req *req = NULL;
498 NTSTATUS status = NT_STATUS_OK;
500 if (cli_has_async_calls(cli)) {
502 * Can't use sync call while an async call is in flight
504 status = NT_STATUS_INVALID_PARAMETER;
505 goto fail;
508 ev = event_context_init(frame);
509 if (ev == NULL) {
510 status = NT_STATUS_NO_MEMORY;
511 goto fail;
514 req = cli_posix_hardlink_send(frame,
516 cli,
517 oldname,
518 newname);
519 if (req == NULL) {
520 status = NT_STATUS_NO_MEMORY;
521 goto fail;
524 if (!tevent_req_poll(req, ev)) {
525 status = map_nt_error_from_unix(errno);
526 goto fail;
529 status = cli_posix_hardlink_recv(req);
531 fail:
532 TALLOC_FREE(frame);
533 if (!NT_STATUS_IS_OK(status)) {
534 cli_set_error(cli, status);
536 return status;
539 /****************************************************************************
540 Map standard UNIX permissions onto wire representations.
541 ****************************************************************************/
543 uint32_t unix_perms_to_wire(mode_t perms)
545 unsigned int ret = 0;
547 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
548 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
549 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
550 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
551 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
552 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
553 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
554 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
555 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
556 #ifdef S_ISVTX
557 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
558 #endif
559 #ifdef S_ISGID
560 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
561 #endif
562 #ifdef S_ISUID
563 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
564 #endif
565 return ret;
568 /****************************************************************************
569 Map wire permissions to standard UNIX.
570 ****************************************************************************/
572 mode_t wire_perms_to_unix(uint32_t perms)
574 mode_t ret = (mode_t)0;
576 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
577 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
578 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
579 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
580 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
581 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
582 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
583 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
584 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
585 #ifdef S_ISVTX
586 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
587 #endif
588 #ifdef S_ISGID
589 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
590 #endif
591 #ifdef S_ISUID
592 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
593 #endif
594 return ret;
597 /****************************************************************************
598 Return the file type from the wire filetype for UNIX extensions.
599 ****************************************************************************/
601 static mode_t unix_filetype_from_wire(uint32_t wire_type)
603 switch (wire_type) {
604 case UNIX_TYPE_FILE:
605 return S_IFREG;
606 case UNIX_TYPE_DIR:
607 return S_IFDIR;
608 #ifdef S_IFLNK
609 case UNIX_TYPE_SYMLINK:
610 return S_IFLNK;
611 #endif
612 #ifdef S_IFCHR
613 case UNIX_TYPE_CHARDEV:
614 return S_IFCHR;
615 #endif
616 #ifdef S_IFBLK
617 case UNIX_TYPE_BLKDEV:
618 return S_IFBLK;
619 #endif
620 #ifdef S_IFIFO
621 case UNIX_TYPE_FIFO:
622 return S_IFIFO;
623 #endif
624 #ifdef S_IFSOCK
625 case UNIX_TYPE_SOCKET:
626 return S_IFSOCK;
627 #endif
628 default:
629 return (mode_t)0;
633 /****************************************************************************
634 Do a POSIX getfacl (UNIX extensions).
635 ****************************************************************************/
637 struct getfacl_state {
638 uint32_t num_data;
639 uint8_t *data;
642 static void cli_posix_getfacl_done(struct tevent_req *subreq);
644 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
645 struct event_context *ev,
646 struct cli_state *cli,
647 const char *fname)
649 struct tevent_req *req = NULL, *subreq = NULL;
650 struct getfacl_state *state = NULL;
652 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
653 if (req == NULL) {
654 return NULL;
656 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
657 0, cli->max_xmit);
658 if (tevent_req_nomem(subreq, req)) {
659 return tevent_req_post(req, ev);
661 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
662 return req;
665 static void cli_posix_getfacl_done(struct tevent_req *subreq)
667 struct tevent_req *req = tevent_req_callback_data(
668 subreq, struct tevent_req);
669 struct getfacl_state *state = tevent_req_data(
670 req, struct getfacl_state);
671 NTSTATUS status;
673 status = cli_qpathinfo_recv(subreq, state, &state->data,
674 &state->num_data);
675 TALLOC_FREE(subreq);
676 if (!NT_STATUS_IS_OK(status)) {
677 tevent_req_nterror(req, status);
678 return;
680 tevent_req_done(req);
683 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
684 TALLOC_CTX *mem_ctx,
685 size_t *prb_size,
686 char **retbuf)
688 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
689 NTSTATUS status;
691 if (tevent_req_is_nterror(req, &status)) {
692 return status;
694 *prb_size = (size_t)state->num_data;
695 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
696 return NT_STATUS_OK;
699 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
700 const char *fname,
701 TALLOC_CTX *mem_ctx,
702 size_t *prb_size,
703 char **retbuf)
705 TALLOC_CTX *frame = talloc_stackframe();
706 struct event_context *ev = NULL;
707 struct tevent_req *req = NULL;
708 NTSTATUS status = NT_STATUS_OK;
710 if (cli_has_async_calls(cli)) {
712 * Can't use sync call while an async call is in flight
714 status = NT_STATUS_INVALID_PARAMETER;
715 goto fail;
718 ev = event_context_init(frame);
719 if (ev == NULL) {
720 status = NT_STATUS_NO_MEMORY;
721 goto fail;
724 req = cli_posix_getfacl_send(frame,
726 cli,
727 fname);
728 if (req == NULL) {
729 status = NT_STATUS_NO_MEMORY;
730 goto fail;
733 if (!tevent_req_poll(req, ev)) {
734 status = map_nt_error_from_unix(errno);
735 goto fail;
738 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
740 fail:
741 TALLOC_FREE(frame);
742 if (!NT_STATUS_IS_OK(status)) {
743 cli_set_error(cli, status);
745 return status;
748 /****************************************************************************
749 Stat a file (UNIX extensions).
750 ****************************************************************************/
752 struct stat_state {
753 uint32_t num_data;
754 uint8_t *data;
757 static void cli_posix_stat_done(struct tevent_req *subreq);
759 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
760 struct event_context *ev,
761 struct cli_state *cli,
762 const char *fname)
764 struct tevent_req *req = NULL, *subreq = NULL;
765 struct stat_state *state = NULL;
767 req = tevent_req_create(mem_ctx, &state, struct stat_state);
768 if (req == NULL) {
769 return NULL;
771 subreq = cli_qpathinfo_send(state, ev, cli, fname,
772 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
773 if (tevent_req_nomem(subreq, req)) {
774 return tevent_req_post(req, ev);
776 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
777 return req;
780 static void cli_posix_stat_done(struct tevent_req *subreq)
782 struct tevent_req *req = tevent_req_callback_data(
783 subreq, struct tevent_req);
784 struct stat_state *state = tevent_req_data(req, struct stat_state);
785 NTSTATUS status;
787 status = cli_qpathinfo_recv(subreq, state, &state->data,
788 &state->num_data);
789 TALLOC_FREE(subreq);
790 if (!NT_STATUS_IS_OK(status)) {
791 tevent_req_nterror(req, status);
792 return;
794 tevent_req_done(req);
797 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
798 SMB_STRUCT_STAT *sbuf)
800 struct stat_state *state = tevent_req_data(req, struct stat_state);
801 NTSTATUS status;
803 if (tevent_req_is_nterror(req, &status)) {
804 return status;
807 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
808 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
809 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
810 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
811 #else
812 /* assume 512 byte blocks */
813 sbuf->st_ex_blocks /= 512;
814 #endif
815 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
816 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
817 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
819 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
820 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
821 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
822 #if defined(HAVE_MAKEDEV)
824 uint32_t dev_major = IVAL(state->data,60);
825 uint32_t dev_minor = IVAL(state->data,68);
826 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
828 #endif
829 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
830 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
831 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
833 return NT_STATUS_OK;
836 NTSTATUS cli_posix_stat(struct cli_state *cli,
837 const char *fname,
838 SMB_STRUCT_STAT *sbuf)
840 TALLOC_CTX *frame = talloc_stackframe();
841 struct event_context *ev = NULL;
842 struct tevent_req *req = NULL;
843 NTSTATUS status = NT_STATUS_OK;
845 if (cli_has_async_calls(cli)) {
847 * Can't use sync call while an async call is in flight
849 status = NT_STATUS_INVALID_PARAMETER;
850 goto fail;
853 ev = event_context_init(frame);
854 if (ev == NULL) {
855 status = NT_STATUS_NO_MEMORY;
856 goto fail;
859 req = cli_posix_stat_send(frame,
861 cli,
862 fname);
863 if (req == NULL) {
864 status = NT_STATUS_NO_MEMORY;
865 goto fail;
868 if (!tevent_req_poll(req, ev)) {
869 status = map_nt_error_from_unix(errno);
870 goto fail;
873 status = cli_posix_stat_recv(req, sbuf);
875 fail:
876 TALLOC_FREE(frame);
877 if (!NT_STATUS_IS_OK(status)) {
878 cli_set_error(cli, status);
880 return status;
883 /****************************************************************************
884 Chmod or chown a file internal (UNIX extensions).
885 ****************************************************************************/
887 struct cli_posix_chown_chmod_internal_state {
888 uint8_t data[100];
891 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
893 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
894 struct event_context *ev,
895 struct cli_state *cli,
896 const char *fname,
897 uint32_t mode,
898 uint32_t uid,
899 uint32_t gid)
901 struct tevent_req *req = NULL, *subreq = NULL;
902 struct cli_posix_chown_chmod_internal_state *state = NULL;
904 req = tevent_req_create(mem_ctx, &state,
905 struct cli_posix_chown_chmod_internal_state);
906 if (req == NULL) {
907 return NULL;
910 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
911 memset(&state->data[40], '\0', 60);
912 SIVAL(state->data,40,uid);
913 SIVAL(state->data,48,gid);
914 SIVAL(state->data,84,mode);
916 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
917 fname, state->data, sizeof(state->data));
918 if (tevent_req_nomem(subreq, req)) {
919 return tevent_req_post(req, ev);
921 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
922 req);
923 return req;
926 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
928 NTSTATUS status = cli_setpathinfo_recv(subreq);
929 tevent_req_simple_finish_ntstatus(subreq, status);
932 /****************************************************************************
933 chmod a file (UNIX extensions).
934 ****************************************************************************/
936 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
937 struct event_context *ev,
938 struct cli_state *cli,
939 const char *fname,
940 mode_t mode)
942 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
943 fname,
944 unix_perms_to_wire(mode),
945 SMB_UID_NO_CHANGE,
946 SMB_GID_NO_CHANGE);
949 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
951 return tevent_req_simple_recv_ntstatus(req);
954 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
956 TALLOC_CTX *frame = talloc_stackframe();
957 struct event_context *ev = NULL;
958 struct tevent_req *req = NULL;
959 NTSTATUS status = NT_STATUS_OK;
961 if (cli_has_async_calls(cli)) {
963 * Can't use sync call while an async call is in flight
965 status = NT_STATUS_INVALID_PARAMETER;
966 goto fail;
969 ev = event_context_init(frame);
970 if (ev == NULL) {
971 status = NT_STATUS_NO_MEMORY;
972 goto fail;
975 req = cli_posix_chmod_send(frame,
977 cli,
978 fname,
979 mode);
980 if (req == NULL) {
981 status = NT_STATUS_NO_MEMORY;
982 goto fail;
985 if (!tevent_req_poll(req, ev)) {
986 status = map_nt_error_from_unix(errno);
987 goto fail;
990 status = cli_posix_chmod_recv(req);
992 fail:
993 TALLOC_FREE(frame);
994 if (!NT_STATUS_IS_OK(status)) {
995 cli_set_error(cli, status);
997 return status;
1000 /****************************************************************************
1001 chown a file (UNIX extensions).
1002 ****************************************************************************/
1004 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1005 struct event_context *ev,
1006 struct cli_state *cli,
1007 const char *fname,
1008 uid_t uid,
1009 gid_t gid)
1011 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1012 fname,
1013 SMB_MODE_NO_CHANGE,
1014 (uint32_t)uid,
1015 (uint32_t)gid);
1018 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1020 return tevent_req_simple_recv_ntstatus(req);
1023 NTSTATUS cli_posix_chown(struct cli_state *cli,
1024 const char *fname,
1025 uid_t uid,
1026 gid_t gid)
1028 TALLOC_CTX *frame = talloc_stackframe();
1029 struct event_context *ev = NULL;
1030 struct tevent_req *req = NULL;
1031 NTSTATUS status = NT_STATUS_OK;
1033 if (cli_has_async_calls(cli)) {
1035 * Can't use sync call while an async call is in flight
1037 status = NT_STATUS_INVALID_PARAMETER;
1038 goto fail;
1041 ev = event_context_init(frame);
1042 if (ev == NULL) {
1043 status = NT_STATUS_NO_MEMORY;
1044 goto fail;
1047 req = cli_posix_chown_send(frame,
1049 cli,
1050 fname,
1051 uid,
1052 gid);
1053 if (req == NULL) {
1054 status = NT_STATUS_NO_MEMORY;
1055 goto fail;
1058 if (!tevent_req_poll(req, ev)) {
1059 status = map_nt_error_from_unix(errno);
1060 goto fail;
1063 status = cli_posix_chown_recv(req);
1065 fail:
1066 TALLOC_FREE(frame);
1067 if (!NT_STATUS_IS_OK(status)) {
1068 cli_set_error(cli, status);
1070 return status;
1073 /****************************************************************************
1074 Rename a file.
1075 ****************************************************************************/
1077 static void cli_rename_done(struct tevent_req *subreq);
1079 struct cli_rename_state {
1080 uint16_t vwv[1];
1083 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1084 struct event_context *ev,
1085 struct cli_state *cli,
1086 const char *fname_src,
1087 const char *fname_dst)
1089 struct tevent_req *req = NULL, *subreq = NULL;
1090 struct cli_rename_state *state = NULL;
1091 uint8_t additional_flags = 0;
1092 uint8_t *bytes = NULL;
1094 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1095 if (req == NULL) {
1096 return NULL;
1099 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1101 bytes = talloc_array(state, uint8_t, 1);
1102 if (tevent_req_nomem(bytes, req)) {
1103 return tevent_req_post(req, ev);
1105 bytes[0] = 4;
1106 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1107 strlen(fname_src)+1, NULL);
1108 if (tevent_req_nomem(bytes, req)) {
1109 return tevent_req_post(req, ev);
1112 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1113 talloc_get_size(bytes)+1);
1114 if (tevent_req_nomem(bytes, req)) {
1115 return tevent_req_post(req, ev);
1118 bytes[talloc_get_size(bytes)-1] = 4;
1119 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1120 strlen(fname_dst)+1, NULL);
1121 if (tevent_req_nomem(bytes, req)) {
1122 return tevent_req_post(req, ev);
1125 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1126 1, state->vwv, talloc_get_size(bytes), bytes);
1127 if (tevent_req_nomem(subreq, req)) {
1128 return tevent_req_post(req, ev);
1130 tevent_req_set_callback(subreq, cli_rename_done, req);
1131 return req;
1134 static void cli_rename_done(struct tevent_req *subreq)
1136 struct tevent_req *req = tevent_req_callback_data(
1137 subreq, struct tevent_req);
1138 NTSTATUS status;
1140 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1141 TALLOC_FREE(subreq);
1142 if (!NT_STATUS_IS_OK(status)) {
1143 tevent_req_nterror(req, status);
1144 return;
1146 tevent_req_done(req);
1149 NTSTATUS cli_rename_recv(struct tevent_req *req)
1151 return tevent_req_simple_recv_ntstatus(req);
1154 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1156 TALLOC_CTX *frame = talloc_stackframe();
1157 struct event_context *ev;
1158 struct tevent_req *req;
1159 NTSTATUS status = NT_STATUS_OK;
1161 if (cli_has_async_calls(cli)) {
1163 * Can't use sync call while an async call is in flight
1165 status = NT_STATUS_INVALID_PARAMETER;
1166 goto fail;
1169 ev = event_context_init(frame);
1170 if (ev == NULL) {
1171 status = NT_STATUS_NO_MEMORY;
1172 goto fail;
1175 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1176 if (req == NULL) {
1177 status = NT_STATUS_NO_MEMORY;
1178 goto fail;
1181 if (!tevent_req_poll(req, ev)) {
1182 status = map_nt_error_from_unix(errno);
1183 goto fail;
1186 status = cli_rename_recv(req);
1188 fail:
1189 TALLOC_FREE(frame);
1190 if (!NT_STATUS_IS_OK(status)) {
1191 cli_set_error(cli, status);
1193 return status;
1196 /****************************************************************************
1197 NT Rename a file.
1198 ****************************************************************************/
1200 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1202 struct cli_ntrename_internal_state {
1203 uint16_t vwv[4];
1206 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1207 struct event_context *ev,
1208 struct cli_state *cli,
1209 const char *fname_src,
1210 const char *fname_dst,
1211 uint16_t rename_flag)
1213 struct tevent_req *req = NULL, *subreq = NULL;
1214 struct cli_ntrename_internal_state *state = NULL;
1215 uint8_t additional_flags = 0;
1216 uint8_t *bytes = NULL;
1218 req = tevent_req_create(mem_ctx, &state,
1219 struct cli_ntrename_internal_state);
1220 if (req == NULL) {
1221 return NULL;
1224 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1225 SSVAL(state->vwv+1, 0, rename_flag);
1227 bytes = talloc_array(state, uint8_t, 1);
1228 if (tevent_req_nomem(bytes, req)) {
1229 return tevent_req_post(req, ev);
1231 bytes[0] = 4;
1232 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1233 strlen(fname_src)+1, NULL);
1234 if (tevent_req_nomem(bytes, req)) {
1235 return tevent_req_post(req, ev);
1238 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1239 talloc_get_size(bytes)+1);
1240 if (tevent_req_nomem(bytes, req)) {
1241 return tevent_req_post(req, ev);
1244 bytes[talloc_get_size(bytes)-1] = 4;
1245 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1246 strlen(fname_dst)+1, NULL);
1247 if (tevent_req_nomem(bytes, req)) {
1248 return tevent_req_post(req, ev);
1251 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1252 4, state->vwv, talloc_get_size(bytes), bytes);
1253 if (tevent_req_nomem(subreq, req)) {
1254 return tevent_req_post(req, ev);
1256 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1257 return req;
1260 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1262 struct tevent_req *req = tevent_req_callback_data(
1263 subreq, struct tevent_req);
1264 NTSTATUS status;
1266 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1267 TALLOC_FREE(subreq);
1268 if (!NT_STATUS_IS_OK(status)) {
1269 tevent_req_nterror(req, status);
1270 return;
1272 tevent_req_done(req);
1275 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1277 return tevent_req_simple_recv_ntstatus(req);
1280 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1281 struct event_context *ev,
1282 struct cli_state *cli,
1283 const char *fname_src,
1284 const char *fname_dst)
1286 return cli_ntrename_internal_send(mem_ctx,
1288 cli,
1289 fname_src,
1290 fname_dst,
1291 RENAME_FLAG_RENAME);
1294 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1296 return cli_ntrename_internal_recv(req);
1299 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1301 TALLOC_CTX *frame = talloc_stackframe();
1302 struct event_context *ev;
1303 struct tevent_req *req;
1304 NTSTATUS status = NT_STATUS_OK;
1306 if (cli_has_async_calls(cli)) {
1308 * Can't use sync call while an async call is in flight
1310 status = NT_STATUS_INVALID_PARAMETER;
1311 goto fail;
1314 ev = event_context_init(frame);
1315 if (ev == NULL) {
1316 status = NT_STATUS_NO_MEMORY;
1317 goto fail;
1320 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1321 if (req == NULL) {
1322 status = NT_STATUS_NO_MEMORY;
1323 goto fail;
1326 if (!tevent_req_poll(req, ev)) {
1327 status = map_nt_error_from_unix(errno);
1328 goto fail;
1331 status = cli_ntrename_recv(req);
1333 fail:
1334 TALLOC_FREE(frame);
1335 if (!NT_STATUS_IS_OK(status)) {
1336 cli_set_error(cli, status);
1338 return status;
1341 /****************************************************************************
1342 NT hardlink a file.
1343 ****************************************************************************/
1345 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1346 struct event_context *ev,
1347 struct cli_state *cli,
1348 const char *fname_src,
1349 const char *fname_dst)
1351 return cli_ntrename_internal_send(mem_ctx,
1353 cli,
1354 fname_src,
1355 fname_dst,
1356 RENAME_FLAG_HARD_LINK);
1359 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1361 return cli_ntrename_internal_recv(req);
1364 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1366 TALLOC_CTX *frame = talloc_stackframe();
1367 struct event_context *ev;
1368 struct tevent_req *req;
1369 NTSTATUS status = NT_STATUS_OK;
1371 if (cli_has_async_calls(cli)) {
1373 * Can't use sync call while an async call is in flight
1375 status = NT_STATUS_INVALID_PARAMETER;
1376 goto fail;
1379 ev = event_context_init(frame);
1380 if (ev == NULL) {
1381 status = NT_STATUS_NO_MEMORY;
1382 goto fail;
1385 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1386 if (req == NULL) {
1387 status = NT_STATUS_NO_MEMORY;
1388 goto fail;
1391 if (!tevent_req_poll(req, ev)) {
1392 status = map_nt_error_from_unix(errno);
1393 goto fail;
1396 status = cli_nt_hardlink_recv(req);
1398 fail:
1399 TALLOC_FREE(frame);
1400 if (!NT_STATUS_IS_OK(status)) {
1401 cli_set_error(cli, status);
1403 return status;
1406 /****************************************************************************
1407 Delete a file.
1408 ****************************************************************************/
1410 static void cli_unlink_done(struct tevent_req *subreq);
1412 struct cli_unlink_state {
1413 uint16_t vwv[1];
1416 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1417 struct event_context *ev,
1418 struct cli_state *cli,
1419 const char *fname,
1420 uint16_t mayhave_attrs)
1422 struct tevent_req *req = NULL, *subreq = NULL;
1423 struct cli_unlink_state *state = NULL;
1424 uint8_t additional_flags = 0;
1425 uint8_t *bytes = NULL;
1427 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1428 if (req == NULL) {
1429 return NULL;
1432 SSVAL(state->vwv+0, 0, mayhave_attrs);
1434 bytes = talloc_array(state, uint8_t, 1);
1435 if (tevent_req_nomem(bytes, req)) {
1436 return tevent_req_post(req, ev);
1438 bytes[0] = 4;
1439 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1440 strlen(fname)+1, NULL);
1442 if (tevent_req_nomem(bytes, req)) {
1443 return tevent_req_post(req, ev);
1446 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1447 1, state->vwv, talloc_get_size(bytes), bytes);
1448 if (tevent_req_nomem(subreq, req)) {
1449 return tevent_req_post(req, ev);
1451 tevent_req_set_callback(subreq, cli_unlink_done, req);
1452 return req;
1455 static void cli_unlink_done(struct tevent_req *subreq)
1457 struct tevent_req *req = tevent_req_callback_data(
1458 subreq, struct tevent_req);
1459 NTSTATUS status;
1461 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1462 TALLOC_FREE(subreq);
1463 if (!NT_STATUS_IS_OK(status)) {
1464 tevent_req_nterror(req, status);
1465 return;
1467 tevent_req_done(req);
1470 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1472 return tevent_req_simple_recv_ntstatus(req);
1475 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1477 TALLOC_CTX *frame = talloc_stackframe();
1478 struct event_context *ev;
1479 struct tevent_req *req;
1480 NTSTATUS status = NT_STATUS_OK;
1482 if (cli_has_async_calls(cli)) {
1484 * Can't use sync call while an async call is in flight
1486 status = NT_STATUS_INVALID_PARAMETER;
1487 goto fail;
1490 ev = event_context_init(frame);
1491 if (ev == NULL) {
1492 status = NT_STATUS_NO_MEMORY;
1493 goto fail;
1496 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1497 if (req == NULL) {
1498 status = NT_STATUS_NO_MEMORY;
1499 goto fail;
1502 if (!tevent_req_poll(req, ev)) {
1503 status = map_nt_error_from_unix(errno);
1504 goto fail;
1507 status = cli_unlink_recv(req);
1509 fail:
1510 TALLOC_FREE(frame);
1511 if (!NT_STATUS_IS_OK(status)) {
1512 cli_set_error(cli, status);
1514 return status;
1517 /****************************************************************************
1518 Create a directory.
1519 ****************************************************************************/
1521 static void cli_mkdir_done(struct tevent_req *subreq);
1523 struct cli_mkdir_state {
1524 int dummy;
1527 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1528 struct event_context *ev,
1529 struct cli_state *cli,
1530 const char *dname)
1532 struct tevent_req *req = NULL, *subreq = NULL;
1533 struct cli_mkdir_state *state = NULL;
1534 uint8_t additional_flags = 0;
1535 uint8_t *bytes = NULL;
1537 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1538 if (req == NULL) {
1539 return NULL;
1542 bytes = talloc_array(state, uint8_t, 1);
1543 if (tevent_req_nomem(bytes, req)) {
1544 return tevent_req_post(req, ev);
1546 bytes[0] = 4;
1547 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1548 strlen(dname)+1, NULL);
1550 if (tevent_req_nomem(bytes, req)) {
1551 return tevent_req_post(req, ev);
1554 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1555 0, NULL, talloc_get_size(bytes), bytes);
1556 if (tevent_req_nomem(subreq, req)) {
1557 return tevent_req_post(req, ev);
1559 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1560 return req;
1563 static void cli_mkdir_done(struct tevent_req *subreq)
1565 struct tevent_req *req = tevent_req_callback_data(
1566 subreq, struct tevent_req);
1567 NTSTATUS status;
1569 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1570 TALLOC_FREE(subreq);
1571 if (!NT_STATUS_IS_OK(status)) {
1572 tevent_req_nterror(req, status);
1573 return;
1575 tevent_req_done(req);
1578 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1580 return tevent_req_simple_recv_ntstatus(req);
1583 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1585 TALLOC_CTX *frame = talloc_stackframe();
1586 struct event_context *ev;
1587 struct tevent_req *req;
1588 NTSTATUS status = NT_STATUS_OK;
1590 if (cli_has_async_calls(cli)) {
1592 * Can't use sync call while an async call is in flight
1594 status = NT_STATUS_INVALID_PARAMETER;
1595 goto fail;
1598 ev = event_context_init(frame);
1599 if (ev == NULL) {
1600 status = NT_STATUS_NO_MEMORY;
1601 goto fail;
1604 req = cli_mkdir_send(frame, ev, cli, dname);
1605 if (req == NULL) {
1606 status = NT_STATUS_NO_MEMORY;
1607 goto fail;
1610 if (!tevent_req_poll(req, ev)) {
1611 status = map_nt_error_from_unix(errno);
1612 goto fail;
1615 status = cli_mkdir_recv(req);
1617 fail:
1618 TALLOC_FREE(frame);
1619 if (!NT_STATUS_IS_OK(status)) {
1620 cli_set_error(cli, status);
1622 return status;
1625 /****************************************************************************
1626 Remove a directory.
1627 ****************************************************************************/
1629 static void cli_rmdir_done(struct tevent_req *subreq);
1631 struct cli_rmdir_state {
1632 int dummy;
1635 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1636 struct event_context *ev,
1637 struct cli_state *cli,
1638 const char *dname)
1640 struct tevent_req *req = NULL, *subreq = NULL;
1641 struct cli_rmdir_state *state = NULL;
1642 uint8_t additional_flags = 0;
1643 uint8_t *bytes = NULL;
1645 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1646 if (req == NULL) {
1647 return NULL;
1650 bytes = talloc_array(state, uint8_t, 1);
1651 if (tevent_req_nomem(bytes, req)) {
1652 return tevent_req_post(req, ev);
1654 bytes[0] = 4;
1655 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1656 strlen(dname)+1, NULL);
1658 if (tevent_req_nomem(bytes, req)) {
1659 return tevent_req_post(req, ev);
1662 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1663 0, NULL, talloc_get_size(bytes), bytes);
1664 if (tevent_req_nomem(subreq, req)) {
1665 return tevent_req_post(req, ev);
1667 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1668 return req;
1671 static void cli_rmdir_done(struct tevent_req *subreq)
1673 struct tevent_req *req = tevent_req_callback_data(
1674 subreq, struct tevent_req);
1675 NTSTATUS status;
1677 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1678 TALLOC_FREE(subreq);
1679 if (!NT_STATUS_IS_OK(status)) {
1680 tevent_req_nterror(req, status);
1681 return;
1683 tevent_req_done(req);
1686 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1688 return tevent_req_simple_recv_ntstatus(req);
1691 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1693 TALLOC_CTX *frame = talloc_stackframe();
1694 struct event_context *ev;
1695 struct tevent_req *req;
1696 NTSTATUS status = NT_STATUS_OK;
1698 if (cli_has_async_calls(cli)) {
1700 * Can't use sync call while an async call is in flight
1702 status = NT_STATUS_INVALID_PARAMETER;
1703 goto fail;
1706 ev = event_context_init(frame);
1707 if (ev == NULL) {
1708 status = NT_STATUS_NO_MEMORY;
1709 goto fail;
1712 req = cli_rmdir_send(frame, ev, cli, dname);
1713 if (req == NULL) {
1714 status = NT_STATUS_NO_MEMORY;
1715 goto fail;
1718 if (!tevent_req_poll(req, ev)) {
1719 status = map_nt_error_from_unix(errno);
1720 goto fail;
1723 status = cli_rmdir_recv(req);
1725 fail:
1726 TALLOC_FREE(frame);
1727 if (!NT_STATUS_IS_OK(status)) {
1728 cli_set_error(cli, status);
1730 return status;
1733 /****************************************************************************
1734 Set or clear the delete on close flag.
1735 ****************************************************************************/
1737 struct doc_state {
1738 uint16_t setup;
1739 uint8_t param[6];
1740 uint8_t data[1];
1743 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1745 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1746 NULL, 0, NULL, NULL, 0, NULL);
1747 tevent_req_simple_finish_ntstatus(subreq, status);
1750 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1751 struct event_context *ev,
1752 struct cli_state *cli,
1753 uint16_t fnum,
1754 bool flag)
1756 struct tevent_req *req = NULL, *subreq = NULL;
1757 struct doc_state *state = NULL;
1759 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1760 if (req == NULL) {
1761 return NULL;
1764 /* Setup setup word. */
1765 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1767 /* Setup param array. */
1768 SSVAL(state->param,0,fnum);
1769 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1771 /* Setup data array. */
1772 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1774 subreq = cli_trans_send(state, /* mem ctx. */
1775 ev, /* event ctx. */
1776 cli, /* cli_state. */
1777 SMBtrans2, /* cmd. */
1778 NULL, /* pipe name. */
1779 -1, /* fid. */
1780 0, /* function. */
1781 0, /* flags. */
1782 &state->setup, /* setup. */
1783 1, /* num setup uint16_t words. */
1784 0, /* max returned setup. */
1785 state->param, /* param. */
1786 6, /* num param. */
1787 2, /* max returned param. */
1788 state->data, /* data. */
1789 1, /* num data. */
1790 0); /* max returned data. */
1792 if (tevent_req_nomem(subreq, req)) {
1793 return tevent_req_post(req, ev);
1795 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1796 return req;
1799 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1801 return tevent_req_simple_recv_ntstatus(req);
1804 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1806 TALLOC_CTX *frame = talloc_stackframe();
1807 struct event_context *ev = NULL;
1808 struct tevent_req *req = NULL;
1809 NTSTATUS status = NT_STATUS_OK;
1811 if (cli_has_async_calls(cli)) {
1813 * Can't use sync call while an async call is in flight
1815 status = NT_STATUS_INVALID_PARAMETER;
1816 goto fail;
1819 ev = event_context_init(frame);
1820 if (ev == NULL) {
1821 status = NT_STATUS_NO_MEMORY;
1822 goto fail;
1825 req = cli_nt_delete_on_close_send(frame,
1827 cli,
1828 fnum,
1829 flag);
1830 if (req == NULL) {
1831 status = NT_STATUS_NO_MEMORY;
1832 goto fail;
1835 if (!tevent_req_poll(req, ev)) {
1836 status = map_nt_error_from_unix(errno);
1837 goto fail;
1840 status = cli_nt_delete_on_close_recv(req);
1842 fail:
1843 TALLOC_FREE(frame);
1844 if (!NT_STATUS_IS_OK(status)) {
1845 cli_set_error(cli, status);
1847 return status;
1850 struct cli_ntcreate_state {
1851 uint16_t vwv[24];
1852 uint16_t fnum;
1855 static void cli_ntcreate_done(struct tevent_req *subreq);
1857 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1858 struct event_context *ev,
1859 struct cli_state *cli,
1860 const char *fname,
1861 uint32_t CreatFlags,
1862 uint32_t DesiredAccess,
1863 uint32_t FileAttributes,
1864 uint32_t ShareAccess,
1865 uint32_t CreateDisposition,
1866 uint32_t CreateOptions,
1867 uint8_t SecurityFlags)
1869 struct tevent_req *req, *subreq;
1870 struct cli_ntcreate_state *state;
1871 uint16_t *vwv;
1872 uint8_t *bytes;
1873 size_t converted_len;
1875 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1876 if (req == NULL) {
1877 return NULL;
1880 vwv = state->vwv;
1882 SCVAL(vwv+0, 0, 0xFF);
1883 SCVAL(vwv+0, 1, 0);
1884 SSVAL(vwv+1, 0, 0);
1885 SCVAL(vwv+2, 0, 0);
1887 if (cli->use_oplocks) {
1888 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1890 SIVAL(vwv+3, 1, CreatFlags);
1891 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1892 SIVAL(vwv+7, 1, DesiredAccess);
1893 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1894 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1895 SIVAL(vwv+13, 1, FileAttributes);
1896 SIVAL(vwv+15, 1, ShareAccess);
1897 SIVAL(vwv+17, 1, CreateDisposition);
1898 SIVAL(vwv+19, 1, CreateOptions);
1899 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1900 SCVAL(vwv+23, 1, SecurityFlags);
1902 bytes = talloc_array(state, uint8_t, 0);
1903 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1904 fname, strlen(fname)+1,
1905 &converted_len);
1907 /* sigh. this copes with broken netapp filer behaviour */
1908 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1910 if (tevent_req_nomem(bytes, req)) {
1911 return tevent_req_post(req, ev);
1914 SSVAL(vwv+2, 1, converted_len);
1916 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1917 talloc_get_size(bytes), bytes);
1918 if (tevent_req_nomem(subreq, req)) {
1919 return tevent_req_post(req, ev);
1921 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1922 return req;
1925 static void cli_ntcreate_done(struct tevent_req *subreq)
1927 struct tevent_req *req = tevent_req_callback_data(
1928 subreq, struct tevent_req);
1929 struct cli_ntcreate_state *state = tevent_req_data(
1930 req, struct cli_ntcreate_state);
1931 uint8_t wct;
1932 uint16_t *vwv;
1933 uint32_t num_bytes;
1934 uint8_t *bytes;
1935 uint8_t *inbuf;
1936 NTSTATUS status;
1938 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1939 &num_bytes, &bytes);
1940 TALLOC_FREE(subreq);
1941 if (!NT_STATUS_IS_OK(status)) {
1942 tevent_req_nterror(req, status);
1943 return;
1945 state->fnum = SVAL(vwv+2, 1);
1946 tevent_req_done(req);
1949 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1951 struct cli_ntcreate_state *state = tevent_req_data(
1952 req, struct cli_ntcreate_state);
1953 NTSTATUS status;
1955 if (tevent_req_is_nterror(req, &status)) {
1956 return status;
1958 *pfnum = state->fnum;
1959 return NT_STATUS_OK;
1962 NTSTATUS cli_ntcreate(struct cli_state *cli,
1963 const char *fname,
1964 uint32_t CreatFlags,
1965 uint32_t DesiredAccess,
1966 uint32_t FileAttributes,
1967 uint32_t ShareAccess,
1968 uint32_t CreateDisposition,
1969 uint32_t CreateOptions,
1970 uint8_t SecurityFlags,
1971 uint16_t *pfid)
1973 TALLOC_CTX *frame = talloc_stackframe();
1974 struct event_context *ev;
1975 struct tevent_req *req;
1976 NTSTATUS status = NT_STATUS_OK;
1978 if (cli_has_async_calls(cli)) {
1980 * Can't use sync call while an async call is in flight
1982 status = NT_STATUS_INVALID_PARAMETER;
1983 goto fail;
1986 ev = event_context_init(frame);
1987 if (ev == NULL) {
1988 status = NT_STATUS_NO_MEMORY;
1989 goto fail;
1992 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1993 DesiredAccess, FileAttributes, ShareAccess,
1994 CreateDisposition, CreateOptions,
1995 SecurityFlags);
1996 if (req == NULL) {
1997 status = NT_STATUS_NO_MEMORY;
1998 goto fail;
2001 if (!tevent_req_poll(req, ev)) {
2002 status = map_nt_error_from_unix(errno);
2003 goto fail;
2006 status = cli_ntcreate_recv(req, pfid);
2007 fail:
2008 TALLOC_FREE(frame);
2009 if (!NT_STATUS_IS_OK(status)) {
2010 cli_set_error(cli, status);
2012 return status;
2015 /****************************************************************************
2016 Open a file
2017 WARNING: if you open with O_WRONLY then getattrE won't work!
2018 ****************************************************************************/
2020 struct cli_open_state {
2021 uint16_t vwv[15];
2022 uint16_t fnum;
2023 struct iovec bytes;
2026 static void cli_open_done(struct tevent_req *subreq);
2028 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2029 struct event_context *ev,
2030 struct cli_state *cli, const char *fname,
2031 int flags, int share_mode,
2032 struct tevent_req **psmbreq)
2034 struct tevent_req *req, *subreq;
2035 struct cli_open_state *state;
2036 unsigned openfn;
2037 unsigned accessmode;
2038 uint8_t additional_flags;
2039 uint8_t *bytes;
2041 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2042 if (req == NULL) {
2043 return NULL;
2046 openfn = 0;
2047 if (flags & O_CREAT) {
2048 openfn |= (1<<4);
2050 if (!(flags & O_EXCL)) {
2051 if (flags & O_TRUNC)
2052 openfn |= (1<<1);
2053 else
2054 openfn |= (1<<0);
2057 accessmode = (share_mode<<4);
2059 if ((flags & O_ACCMODE) == O_RDWR) {
2060 accessmode |= 2;
2061 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2062 accessmode |= 1;
2065 #if defined(O_SYNC)
2066 if ((flags & O_SYNC) == O_SYNC) {
2067 accessmode |= (1<<14);
2069 #endif /* O_SYNC */
2071 if (share_mode == DENY_FCB) {
2072 accessmode = 0xFF;
2075 SCVAL(state->vwv + 0, 0, 0xFF);
2076 SCVAL(state->vwv + 0, 1, 0);
2077 SSVAL(state->vwv + 1, 0, 0);
2078 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2079 SSVAL(state->vwv + 3, 0, accessmode);
2080 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2081 SSVAL(state->vwv + 5, 0, 0);
2082 SIVAL(state->vwv + 6, 0, 0);
2083 SSVAL(state->vwv + 8, 0, openfn);
2084 SIVAL(state->vwv + 9, 0, 0);
2085 SIVAL(state->vwv + 11, 0, 0);
2086 SIVAL(state->vwv + 13, 0, 0);
2088 additional_flags = 0;
2090 if (cli->use_oplocks) {
2091 /* if using oplocks then ask for a batch oplock via
2092 core and extended methods */
2093 additional_flags =
2094 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2095 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2098 bytes = talloc_array(state, uint8_t, 0);
2099 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2100 strlen(fname)+1, NULL);
2102 if (tevent_req_nomem(bytes, req)) {
2103 return tevent_req_post(req, ev);
2106 state->bytes.iov_base = (void *)bytes;
2107 state->bytes.iov_len = talloc_get_size(bytes);
2109 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2110 15, state->vwv, 1, &state->bytes);
2111 if (subreq == NULL) {
2112 TALLOC_FREE(req);
2113 return NULL;
2115 tevent_req_set_callback(subreq, cli_open_done, req);
2116 *psmbreq = subreq;
2117 return req;
2120 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2121 struct cli_state *cli, const char *fname,
2122 int flags, int share_mode)
2124 struct tevent_req *req, *subreq;
2125 NTSTATUS status;
2127 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2128 &subreq);
2129 if (req == NULL) {
2130 return NULL;
2133 status = cli_smb_req_send(subreq);
2134 if (!NT_STATUS_IS_OK(status)) {
2135 tevent_req_nterror(req, status);
2136 return tevent_req_post(req, ev);
2138 return req;
2141 static void cli_open_done(struct tevent_req *subreq)
2143 struct tevent_req *req = tevent_req_callback_data(
2144 subreq, struct tevent_req);
2145 struct cli_open_state *state = tevent_req_data(
2146 req, struct cli_open_state);
2147 uint8_t wct;
2148 uint16_t *vwv;
2149 uint8_t *inbuf;
2150 NTSTATUS status;
2152 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2153 NULL);
2154 TALLOC_FREE(subreq);
2155 if (!NT_STATUS_IS_OK(status)) {
2156 tevent_req_nterror(req, status);
2157 return;
2159 state->fnum = SVAL(vwv+2, 0);
2160 tevent_req_done(req);
2163 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2165 struct cli_open_state *state = tevent_req_data(
2166 req, struct cli_open_state);
2167 NTSTATUS status;
2169 if (tevent_req_is_nterror(req, &status)) {
2170 return status;
2172 *pfnum = state->fnum;
2173 return NT_STATUS_OK;
2176 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2177 int share_mode, uint16_t *pfnum)
2179 TALLOC_CTX *frame = talloc_stackframe();
2180 struct event_context *ev;
2181 struct tevent_req *req;
2182 NTSTATUS status = NT_STATUS_OK;
2184 if (cli_has_async_calls(cli)) {
2186 * Can't use sync call while an async call is in flight
2188 status = NT_STATUS_INVALID_PARAMETER;
2189 goto fail;
2192 ev = event_context_init(frame);
2193 if (ev == NULL) {
2194 status = NT_STATUS_NO_MEMORY;
2195 goto fail;
2198 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2199 if (req == NULL) {
2200 status = NT_STATUS_NO_MEMORY;
2201 goto fail;
2204 if (!tevent_req_poll(req, ev)) {
2205 status = map_nt_error_from_unix(errno);
2206 goto fail;
2209 status = cli_open_recv(req, pfnum);
2210 fail:
2211 TALLOC_FREE(frame);
2212 if (!NT_STATUS_IS_OK(status)) {
2213 cli_set_error(cli, status);
2215 return status;
2218 /****************************************************************************
2219 Close a file.
2220 ****************************************************************************/
2222 struct cli_close_state {
2223 uint16_t vwv[3];
2226 static void cli_close_done(struct tevent_req *subreq);
2228 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2229 struct event_context *ev,
2230 struct cli_state *cli,
2231 uint16_t fnum,
2232 struct tevent_req **psubreq)
2234 struct tevent_req *req, *subreq;
2235 struct cli_close_state *state;
2237 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2238 if (req == NULL) {
2239 return NULL;
2242 SSVAL(state->vwv+0, 0, fnum);
2243 SIVALS(state->vwv+1, 0, -1);
2245 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2246 0, NULL);
2247 if (subreq == NULL) {
2248 TALLOC_FREE(req);
2249 return NULL;
2251 tevent_req_set_callback(subreq, cli_close_done, req);
2252 *psubreq = subreq;
2253 return req;
2256 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2257 struct event_context *ev,
2258 struct cli_state *cli,
2259 uint16_t fnum)
2261 struct tevent_req *req, *subreq;
2262 NTSTATUS status;
2264 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2265 if (req == NULL) {
2266 return NULL;
2269 status = cli_smb_req_send(subreq);
2270 if (!NT_STATUS_IS_OK(status)) {
2271 tevent_req_nterror(req, status);
2272 return tevent_req_post(req, ev);
2274 return req;
2277 static void cli_close_done(struct tevent_req *subreq)
2279 struct tevent_req *req = tevent_req_callback_data(
2280 subreq, struct tevent_req);
2281 NTSTATUS status;
2283 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2284 TALLOC_FREE(subreq);
2285 if (!NT_STATUS_IS_OK(status)) {
2286 tevent_req_nterror(req, status);
2287 return;
2289 tevent_req_done(req);
2292 NTSTATUS cli_close_recv(struct tevent_req *req)
2294 return tevent_req_simple_recv_ntstatus(req);
2297 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2299 TALLOC_CTX *frame = talloc_stackframe();
2300 struct event_context *ev;
2301 struct tevent_req *req;
2302 NTSTATUS status = NT_STATUS_OK;
2304 if (cli_has_async_calls(cli)) {
2306 * Can't use sync call while an async call is in flight
2308 status = NT_STATUS_INVALID_PARAMETER;
2309 goto fail;
2312 ev = event_context_init(frame);
2313 if (ev == NULL) {
2314 status = NT_STATUS_NO_MEMORY;
2315 goto fail;
2318 req = cli_close_send(frame, ev, cli, fnum);
2319 if (req == NULL) {
2320 status = NT_STATUS_NO_MEMORY;
2321 goto fail;
2324 if (!tevent_req_poll(req, ev)) {
2325 status = map_nt_error_from_unix(errno);
2326 goto fail;
2329 status = cli_close_recv(req);
2330 fail:
2331 TALLOC_FREE(frame);
2332 if (!NT_STATUS_IS_OK(status)) {
2333 cli_set_error(cli, status);
2335 return status;
2338 /****************************************************************************
2339 Truncate a file to a specified size
2340 ****************************************************************************/
2342 struct ftrunc_state {
2343 uint16_t setup;
2344 uint8_t param[6];
2345 uint8_t data[8];
2348 static void cli_ftruncate_done(struct tevent_req *subreq)
2350 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2351 NULL, 0, NULL, NULL, 0, NULL);
2352 tevent_req_simple_finish_ntstatus(subreq, status);
2355 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2356 struct event_context *ev,
2357 struct cli_state *cli,
2358 uint16_t fnum,
2359 uint64_t size)
2361 struct tevent_req *req = NULL, *subreq = NULL;
2362 struct ftrunc_state *state = NULL;
2364 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2365 if (req == NULL) {
2366 return NULL;
2369 /* Setup setup word. */
2370 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2372 /* Setup param array. */
2373 SSVAL(state->param,0,fnum);
2374 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2375 SSVAL(state->param,4,0);
2377 /* Setup data array. */
2378 SBVAL(state->data, 0, size);
2380 subreq = cli_trans_send(state, /* mem ctx. */
2381 ev, /* event ctx. */
2382 cli, /* cli_state. */
2383 SMBtrans2, /* cmd. */
2384 NULL, /* pipe name. */
2385 -1, /* fid. */
2386 0, /* function. */
2387 0, /* flags. */
2388 &state->setup, /* setup. */
2389 1, /* num setup uint16_t words. */
2390 0, /* max returned setup. */
2391 state->param, /* param. */
2392 6, /* num param. */
2393 2, /* max returned param. */
2394 state->data, /* data. */
2395 8, /* num data. */
2396 0); /* max returned data. */
2398 if (tevent_req_nomem(subreq, req)) {
2399 return tevent_req_post(req, ev);
2401 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2402 return req;
2405 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2407 return tevent_req_simple_recv_ntstatus(req);
2410 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2412 TALLOC_CTX *frame = talloc_stackframe();
2413 struct event_context *ev = NULL;
2414 struct tevent_req *req = NULL;
2415 NTSTATUS status = NT_STATUS_OK;
2417 if (cli_has_async_calls(cli)) {
2419 * Can't use sync call while an async call is in flight
2421 status = NT_STATUS_INVALID_PARAMETER;
2422 goto fail;
2425 ev = event_context_init(frame);
2426 if (ev == NULL) {
2427 status = NT_STATUS_NO_MEMORY;
2428 goto fail;
2431 req = cli_ftruncate_send(frame,
2433 cli,
2434 fnum,
2435 size);
2436 if (req == NULL) {
2437 status = NT_STATUS_NO_MEMORY;
2438 goto fail;
2441 if (!tevent_req_poll(req, ev)) {
2442 status = map_nt_error_from_unix(errno);
2443 goto fail;
2446 status = cli_ftruncate_recv(req);
2448 fail:
2449 TALLOC_FREE(frame);
2450 if (!NT_STATUS_IS_OK(status)) {
2451 cli_set_error(cli, status);
2453 return status;
2456 /****************************************************************************
2457 send a lock with a specified locktype
2458 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2459 ****************************************************************************/
2461 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2462 uint32_t offset, uint32_t len,
2463 int timeout, unsigned char locktype)
2465 char *p;
2466 int saved_timeout = cli->timeout;
2468 memset(cli->outbuf,'\0',smb_size);
2469 memset(cli->inbuf,'\0', smb_size);
2471 cli_set_message(cli->outbuf,8,0,True);
2473 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2474 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2475 cli_setup_packet(cli);
2477 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2478 SSVAL(cli->outbuf,smb_vwv2,fnum);
2479 SCVAL(cli->outbuf,smb_vwv3,locktype);
2480 SIVALS(cli->outbuf, smb_vwv4, timeout);
2481 SSVAL(cli->outbuf,smb_vwv6,0);
2482 SSVAL(cli->outbuf,smb_vwv7,1);
2484 p = smb_buf(cli->outbuf);
2485 SSVAL(p, 0, cli->pid);
2486 SIVAL(p, 2, offset);
2487 SIVAL(p, 6, len);
2489 p += 10;
2491 cli_setup_bcc(cli, p);
2493 cli_send_smb(cli);
2495 if (timeout != 0) {
2496 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
2499 if (!cli_receive_smb(cli)) {
2500 cli->timeout = saved_timeout;
2501 return NT_STATUS_UNSUCCESSFUL;
2504 cli->timeout = saved_timeout;
2506 return cli_nt_error(cli);
2509 /****************************************************************************
2510 Lock a file.
2511 note that timeout is in units of 2 milliseconds
2512 ****************************************************************************/
2514 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2515 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
2517 char *p;
2518 int saved_timeout = cli->timeout;
2520 memset(cli->outbuf,'\0',smb_size);
2521 memset(cli->inbuf,'\0', smb_size);
2523 cli_set_message(cli->outbuf,8,0,True);
2525 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2526 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2527 cli_setup_packet(cli);
2529 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2530 SSVAL(cli->outbuf,smb_vwv2,fnum);
2531 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
2532 SIVALS(cli->outbuf, smb_vwv4, timeout);
2533 SSVAL(cli->outbuf,smb_vwv6,0);
2534 SSVAL(cli->outbuf,smb_vwv7,1);
2536 p = smb_buf(cli->outbuf);
2537 SSVAL(p, 0, cli->pid);
2538 SIVAL(p, 2, offset);
2539 SIVAL(p, 6, len);
2541 p += 10;
2543 cli_setup_bcc(cli, p);
2545 cli_send_smb(cli);
2547 if (timeout != 0) {
2548 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
2551 if (!cli_receive_smb(cli)) {
2552 cli->timeout = saved_timeout;
2553 return False;
2556 cli->timeout = saved_timeout;
2558 if (cli_is_error(cli)) {
2559 return False;
2562 return True;
2565 /****************************************************************************
2566 Unlock a file.
2567 ****************************************************************************/
2569 struct cli_unlock_state {
2570 uint16_t vwv[8];
2571 uint8_t data[10];
2574 static void cli_unlock_done(struct tevent_req *subreq);
2576 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2577 struct event_context *ev,
2578 struct cli_state *cli,
2579 uint16_t fnum,
2580 uint64_t offset,
2581 uint64_t len)
2584 struct tevent_req *req = NULL, *subreq = NULL;
2585 struct cli_unlock_state *state = NULL;
2586 uint8_t additional_flags = 0;
2588 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2589 if (req == NULL) {
2590 return NULL;
2593 SCVAL(state->vwv+0, 0, 0xFF);
2594 SSVAL(state->vwv+2, 0, fnum);
2595 SCVAL(state->vwv+3, 0, 0);
2596 SIVALS(state->vwv+4, 0, 0);
2597 SSVAL(state->vwv+6, 0, 1);
2598 SSVAL(state->vwv+7, 0, 0);
2600 SSVAL(state->data, 0, cli->pid);
2601 SIVAL(state->data, 2, offset);
2602 SIVAL(state->data, 6, len);
2604 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2605 8, state->vwv, 10, state->data);
2606 if (tevent_req_nomem(subreq, req)) {
2607 return tevent_req_post(req, ev);
2609 tevent_req_set_callback(subreq, cli_unlock_done, req);
2610 return req;
2613 static void cli_unlock_done(struct tevent_req *subreq)
2615 struct tevent_req *req = tevent_req_callback_data(
2616 subreq, struct tevent_req);
2617 NTSTATUS status;
2619 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2620 TALLOC_FREE(subreq);
2621 if (!NT_STATUS_IS_OK(status)) {
2622 tevent_req_nterror(req, status);
2623 return;
2625 tevent_req_done(req);
2628 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2630 return tevent_req_simple_recv_ntstatus(req);
2633 NTSTATUS cli_unlock(struct cli_state *cli,
2634 uint16_t fnum,
2635 uint32_t offset,
2636 uint32_t len)
2638 TALLOC_CTX *frame = talloc_stackframe();
2639 struct event_context *ev;
2640 struct tevent_req *req;
2641 NTSTATUS status = NT_STATUS_OK;
2643 if (cli_has_async_calls(cli)) {
2645 * Can't use sync call while an async call is in flight
2647 status = NT_STATUS_INVALID_PARAMETER;
2648 goto fail;
2651 ev = event_context_init(frame);
2652 if (ev == NULL) {
2653 status = NT_STATUS_NO_MEMORY;
2654 goto fail;
2657 req = cli_unlock_send(frame, ev, cli,
2658 fnum, offset, len);
2659 if (req == NULL) {
2660 status = NT_STATUS_NO_MEMORY;
2661 goto fail;
2664 if (!tevent_req_poll(req, ev)) {
2665 status = map_nt_error_from_unix(errno);
2666 goto fail;
2669 status = cli_unlock_recv(req);
2671 fail:
2672 TALLOC_FREE(frame);
2673 if (!NT_STATUS_IS_OK(status)) {
2674 cli_set_error(cli, status);
2676 return status;
2679 /****************************************************************************
2680 Lock a file with 64 bit offsets.
2681 ****************************************************************************/
2683 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2684 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
2686 char *p;
2687 int saved_timeout = cli->timeout;
2688 int ltype;
2690 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2691 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2694 ltype = (lock_type == READ_LOCK? 1 : 0);
2695 ltype |= LOCKING_ANDX_LARGE_FILES;
2697 memset(cli->outbuf,'\0',smb_size);
2698 memset(cli->inbuf,'\0', smb_size);
2700 cli_set_message(cli->outbuf,8,0,True);
2702 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2703 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2704 cli_setup_packet(cli);
2706 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2707 SSVAL(cli->outbuf,smb_vwv2,fnum);
2708 SCVAL(cli->outbuf,smb_vwv3,ltype);
2709 SIVALS(cli->outbuf, smb_vwv4, timeout);
2710 SSVAL(cli->outbuf,smb_vwv6,0);
2711 SSVAL(cli->outbuf,smb_vwv7,1);
2713 p = smb_buf(cli->outbuf);
2714 SIVAL(p, 0, cli->pid);
2715 SOFF_T_R(p, 4, offset);
2716 SOFF_T_R(p, 12, len);
2717 p += 20;
2719 cli_setup_bcc(cli, p);
2720 cli_send_smb(cli);
2722 if (timeout != 0) {
2723 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
2726 if (!cli_receive_smb(cli)) {
2727 cli->timeout = saved_timeout;
2728 return False;
2731 cli->timeout = saved_timeout;
2733 if (cli_is_error(cli)) {
2734 return False;
2737 return True;
2740 /****************************************************************************
2741 Unlock a file with 64 bit offsets.
2742 ****************************************************************************/
2744 struct cli_unlock64_state {
2745 uint16_t vwv[8];
2746 uint8_t data[20];
2749 static void cli_unlock64_done(struct tevent_req *subreq);
2751 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2752 struct event_context *ev,
2753 struct cli_state *cli,
2754 uint16_t fnum,
2755 uint64_t offset,
2756 uint64_t len)
2759 struct tevent_req *req = NULL, *subreq = NULL;
2760 struct cli_unlock64_state *state = NULL;
2761 uint8_t additional_flags = 0;
2763 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2764 if (req == NULL) {
2765 return NULL;
2768 SCVAL(state->vwv+0, 0, 0xff);
2769 SSVAL(state->vwv+2, 0, fnum);
2770 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2771 SIVALS(state->vwv+4, 0, 0);
2772 SSVAL(state->vwv+6, 0, 1);
2773 SSVAL(state->vwv+7, 0, 0);
2775 SIVAL(state->data, 0, cli->pid);
2776 SOFF_T_R(state->data, 4, offset);
2777 SOFF_T_R(state->data, 12, len);
2779 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2780 8, state->vwv, 20, state->data);
2781 if (tevent_req_nomem(subreq, req)) {
2782 return tevent_req_post(req, ev);
2784 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2785 return req;
2788 static void cli_unlock64_done(struct tevent_req *subreq)
2790 struct tevent_req *req = tevent_req_callback_data(
2791 subreq, struct tevent_req);
2792 NTSTATUS status;
2794 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2795 TALLOC_FREE(subreq);
2796 if (!NT_STATUS_IS_OK(status)) {
2797 tevent_req_nterror(req, status);
2798 return;
2800 tevent_req_done(req);
2803 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2805 return tevent_req_simple_recv_ntstatus(req);
2808 NTSTATUS cli_unlock64(struct cli_state *cli,
2809 uint16_t fnum,
2810 uint64_t offset,
2811 uint64_t len)
2813 TALLOC_CTX *frame = talloc_stackframe();
2814 struct event_context *ev;
2815 struct tevent_req *req;
2816 NTSTATUS status = NT_STATUS_OK;
2818 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2819 return cli_unlock(cli, fnum, offset, len);
2822 if (cli_has_async_calls(cli)) {
2824 * Can't use sync call while an async call is in flight
2826 status = NT_STATUS_INVALID_PARAMETER;
2827 goto fail;
2830 ev = event_context_init(frame);
2831 if (ev == NULL) {
2832 status = NT_STATUS_NO_MEMORY;
2833 goto fail;
2836 req = cli_unlock64_send(frame, ev, cli,
2837 fnum, offset, len);
2838 if (req == NULL) {
2839 status = NT_STATUS_NO_MEMORY;
2840 goto fail;
2843 if (!tevent_req_poll(req, ev)) {
2844 status = map_nt_error_from_unix(errno);
2845 goto fail;
2848 status = cli_unlock64_recv(req);
2850 fail:
2851 TALLOC_FREE(frame);
2852 if (!NT_STATUS_IS_OK(status)) {
2853 cli_set_error(cli, status);
2855 return status;
2858 /****************************************************************************
2859 Get/unlock a POSIX lock on a file - internal function.
2860 ****************************************************************************/
2862 struct posix_lock_state {
2863 uint16_t setup;
2864 uint8_t param[4];
2865 uint8_t data[POSIX_LOCK_DATA_SIZE];
2868 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2870 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2871 NULL, 0, NULL, NULL, 0, NULL);
2872 tevent_req_simple_finish_ntstatus(subreq, status);
2875 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2876 struct event_context *ev,
2877 struct cli_state *cli,
2878 uint16_t fnum,
2879 uint64_t offset,
2880 uint64_t len,
2881 bool wait_lock,
2882 enum brl_type lock_type)
2884 struct tevent_req *req = NULL, *subreq = NULL;
2885 struct posix_lock_state *state = NULL;
2887 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2888 if (req == NULL) {
2889 return NULL;
2892 /* Setup setup word. */
2893 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2895 /* Setup param array. */
2896 SSVAL(&state->param, 0, fnum);
2897 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2899 /* Setup data array. */
2900 switch (lock_type) {
2901 case READ_LOCK:
2902 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2903 POSIX_LOCK_TYPE_READ);
2904 break;
2905 case WRITE_LOCK:
2906 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2907 POSIX_LOCK_TYPE_WRITE);
2908 break;
2909 case UNLOCK_LOCK:
2910 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2911 POSIX_LOCK_TYPE_UNLOCK);
2912 break;
2913 default:
2914 return NULL;
2917 if (wait_lock) {
2918 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2919 POSIX_LOCK_FLAG_WAIT);
2920 } else {
2921 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2922 POSIX_LOCK_FLAG_NOWAIT);
2925 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2926 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2927 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2929 subreq = cli_trans_send(state, /* mem ctx. */
2930 ev, /* event ctx. */
2931 cli, /* cli_state. */
2932 SMBtrans2, /* cmd. */
2933 NULL, /* pipe name. */
2934 -1, /* fid. */
2935 0, /* function. */
2936 0, /* flags. */
2937 &state->setup, /* setup. */
2938 1, /* num setup uint16_t words. */
2939 0, /* max returned setup. */
2940 state->param, /* param. */
2941 4, /* num param. */
2942 2, /* max returned param. */
2943 state->data, /* data. */
2944 POSIX_LOCK_DATA_SIZE, /* num data. */
2945 0); /* max returned data. */
2947 if (tevent_req_nomem(subreq, req)) {
2948 return tevent_req_post(req, ev);
2950 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2951 return req;
2954 /****************************************************************************
2955 POSIX Lock a file.
2956 ****************************************************************************/
2958 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2959 struct event_context *ev,
2960 struct cli_state *cli,
2961 uint16_t fnum,
2962 uint64_t offset,
2963 uint64_t len,
2964 bool wait_lock,
2965 enum brl_type lock_type)
2967 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2968 wait_lock, lock_type);
2971 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2973 return tevent_req_simple_recv_ntstatus(req);
2976 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2977 uint64_t offset, uint64_t len,
2978 bool wait_lock, enum brl_type lock_type)
2980 TALLOC_CTX *frame = talloc_stackframe();
2981 struct event_context *ev = NULL;
2982 struct tevent_req *req = NULL;
2983 NTSTATUS status = NT_STATUS_OK;
2985 if (cli_has_async_calls(cli)) {
2987 * Can't use sync call while an async call is in flight
2989 status = NT_STATUS_INVALID_PARAMETER;
2990 goto fail;
2993 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2994 status = NT_STATUS_INVALID_PARAMETER;
2995 goto fail;
2998 ev = event_context_init(frame);
2999 if (ev == NULL) {
3000 status = NT_STATUS_NO_MEMORY;
3001 goto fail;
3004 req = cli_posix_lock_send(frame,
3006 cli,
3007 fnum,
3008 offset,
3009 len,
3010 wait_lock,
3011 lock_type);
3012 if (req == NULL) {
3013 status = NT_STATUS_NO_MEMORY;
3014 goto fail;
3017 if (!tevent_req_poll(req, ev)) {
3018 status = map_nt_error_from_unix(errno);
3019 goto fail;
3022 status = cli_posix_lock_recv(req);
3024 fail:
3025 TALLOC_FREE(frame);
3026 if (!NT_STATUS_IS_OK(status)) {
3027 cli_set_error(cli, status);
3029 return status;
3032 /****************************************************************************
3033 POSIX Unlock a file.
3034 ****************************************************************************/
3036 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3037 struct event_context *ev,
3038 struct cli_state *cli,
3039 uint16_t fnum,
3040 uint64_t offset,
3041 uint64_t len)
3043 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3044 false, UNLOCK_LOCK);
3047 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3049 return tevent_req_simple_recv_ntstatus(req);
3052 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3054 TALLOC_CTX *frame = talloc_stackframe();
3055 struct event_context *ev = NULL;
3056 struct tevent_req *req = NULL;
3057 NTSTATUS status = NT_STATUS_OK;
3059 if (cli_has_async_calls(cli)) {
3061 * Can't use sync call while an async call is in flight
3063 status = NT_STATUS_INVALID_PARAMETER;
3064 goto fail;
3067 ev = event_context_init(frame);
3068 if (ev == NULL) {
3069 status = NT_STATUS_NO_MEMORY;
3070 goto fail;
3073 req = cli_posix_unlock_send(frame,
3075 cli,
3076 fnum,
3077 offset,
3078 len);
3079 if (req == NULL) {
3080 status = NT_STATUS_NO_MEMORY;
3081 goto fail;
3084 if (!tevent_req_poll(req, ev)) {
3085 status = map_nt_error_from_unix(errno);
3086 goto fail;
3089 status = cli_posix_unlock_recv(req);
3091 fail:
3092 TALLOC_FREE(frame);
3093 if (!NT_STATUS_IS_OK(status)) {
3094 cli_set_error(cli, status);
3096 return status;
3099 /****************************************************************************
3100 Do a SMBgetattrE call.
3101 ****************************************************************************/
3103 static void cli_getattrE_done(struct tevent_req *subreq);
3105 struct cli_getattrE_state {
3106 uint16_t vwv[1];
3107 int zone_offset;
3108 uint16_t attr;
3109 SMB_OFF_T size;
3110 time_t change_time;
3111 time_t access_time;
3112 time_t write_time;
3115 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3116 struct event_context *ev,
3117 struct cli_state *cli,
3118 uint16_t fnum)
3120 struct tevent_req *req = NULL, *subreq = NULL;
3121 struct cli_getattrE_state *state = NULL;
3122 uint8_t additional_flags = 0;
3124 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3125 if (req == NULL) {
3126 return NULL;
3129 state->zone_offset = cli->serverzone;
3130 SSVAL(state->vwv+0,0,fnum);
3132 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3133 1, state->vwv, 0, NULL);
3134 if (tevent_req_nomem(subreq, req)) {
3135 return tevent_req_post(req, ev);
3137 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3138 return req;
3141 static void cli_getattrE_done(struct tevent_req *subreq)
3143 struct tevent_req *req = tevent_req_callback_data(
3144 subreq, struct tevent_req);
3145 struct cli_getattrE_state *state = tevent_req_data(
3146 req, struct cli_getattrE_state);
3147 uint8_t wct;
3148 uint16_t *vwv = NULL;
3149 uint8_t *inbuf;
3150 NTSTATUS status;
3152 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3153 NULL, NULL);
3154 TALLOC_FREE(subreq);
3155 if (!NT_STATUS_IS_OK(status)) {
3156 tevent_req_nterror(req, status);
3157 return;
3160 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3161 state->attr = SVAL(vwv+10,0);
3162 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3163 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3164 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3166 tevent_req_done(req);
3169 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3170 uint16_t *attr,
3171 SMB_OFF_T *size,
3172 time_t *change_time,
3173 time_t *access_time,
3174 time_t *write_time)
3176 struct cli_getattrE_state *state = tevent_req_data(
3177 req, struct cli_getattrE_state);
3178 NTSTATUS status;
3180 if (tevent_req_is_nterror(req, &status)) {
3181 return status;
3183 if (attr) {
3184 *attr = state->attr;
3186 if (size) {
3187 *size = state->size;
3189 if (change_time) {
3190 *change_time = state->change_time;
3192 if (access_time) {
3193 *access_time = state->access_time;
3195 if (write_time) {
3196 *write_time = state->write_time;
3198 return NT_STATUS_OK;
3201 NTSTATUS cli_getattrE(struct cli_state *cli,
3202 uint16_t fnum,
3203 uint16_t *attr,
3204 SMB_OFF_T *size,
3205 time_t *change_time,
3206 time_t *access_time,
3207 time_t *write_time)
3209 TALLOC_CTX *frame = talloc_stackframe();
3210 struct event_context *ev = NULL;
3211 struct tevent_req *req = NULL;
3212 NTSTATUS status = NT_STATUS_OK;
3214 if (cli_has_async_calls(cli)) {
3216 * Can't use sync call while an async call is in flight
3218 status = NT_STATUS_INVALID_PARAMETER;
3219 goto fail;
3222 ev = event_context_init(frame);
3223 if (ev == NULL) {
3224 status = NT_STATUS_NO_MEMORY;
3225 goto fail;
3228 req = cli_getattrE_send(frame, ev, cli, fnum);
3229 if (req == NULL) {
3230 status = NT_STATUS_NO_MEMORY;
3231 goto fail;
3234 if (!tevent_req_poll(req, ev)) {
3235 status = map_nt_error_from_unix(errno);
3236 goto fail;
3239 status = cli_getattrE_recv(req,
3240 attr,
3241 size,
3242 change_time,
3243 access_time,
3244 write_time);
3246 fail:
3247 TALLOC_FREE(frame);
3248 if (!NT_STATUS_IS_OK(status)) {
3249 cli_set_error(cli, status);
3251 return status;
3254 /****************************************************************************
3255 Do a SMBgetatr call
3256 ****************************************************************************/
3258 static void cli_getatr_done(struct tevent_req *subreq);
3260 struct cli_getatr_state {
3261 int zone_offset;
3262 uint16_t attr;
3263 SMB_OFF_T size;
3264 time_t write_time;
3267 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3268 struct event_context *ev,
3269 struct cli_state *cli,
3270 const char *fname)
3272 struct tevent_req *req = NULL, *subreq = NULL;
3273 struct cli_getatr_state *state = NULL;
3274 uint8_t additional_flags = 0;
3275 uint8_t *bytes = NULL;
3277 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3278 if (req == NULL) {
3279 return NULL;
3282 state->zone_offset = cli->serverzone;
3284 bytes = talloc_array(state, uint8_t, 1);
3285 if (tevent_req_nomem(bytes, req)) {
3286 return tevent_req_post(req, ev);
3288 bytes[0] = 4;
3289 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3290 strlen(fname)+1, NULL);
3292 if (tevent_req_nomem(bytes, req)) {
3293 return tevent_req_post(req, ev);
3296 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3297 0, NULL, talloc_get_size(bytes), bytes);
3298 if (tevent_req_nomem(subreq, req)) {
3299 return tevent_req_post(req, ev);
3301 tevent_req_set_callback(subreq, cli_getatr_done, req);
3302 return req;
3305 static void cli_getatr_done(struct tevent_req *subreq)
3307 struct tevent_req *req = tevent_req_callback_data(
3308 subreq, struct tevent_req);
3309 struct cli_getatr_state *state = tevent_req_data(
3310 req, struct cli_getatr_state);
3311 uint8_t wct;
3312 uint16_t *vwv = NULL;
3313 uint8_t *inbuf;
3314 NTSTATUS status;
3316 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3317 NULL);
3318 TALLOC_FREE(subreq);
3319 if (!NT_STATUS_IS_OK(status)) {
3320 tevent_req_nterror(req, status);
3321 return;
3324 state->attr = SVAL(vwv+0,0);
3325 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3326 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3328 tevent_req_done(req);
3331 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3332 uint16_t *attr,
3333 SMB_OFF_T *size,
3334 time_t *write_time)
3336 struct cli_getatr_state *state = tevent_req_data(
3337 req, struct cli_getatr_state);
3338 NTSTATUS status;
3340 if (tevent_req_is_nterror(req, &status)) {
3341 return status;
3343 if (attr) {
3344 *attr = state->attr;
3346 if (size) {
3347 *size = state->size;
3349 if (write_time) {
3350 *write_time = state->write_time;
3352 return NT_STATUS_OK;
3355 NTSTATUS cli_getatr(struct cli_state *cli,
3356 const char *fname,
3357 uint16_t *attr,
3358 SMB_OFF_T *size,
3359 time_t *write_time)
3361 TALLOC_CTX *frame = talloc_stackframe();
3362 struct event_context *ev = NULL;
3363 struct tevent_req *req = NULL;
3364 NTSTATUS status = NT_STATUS_OK;
3366 if (cli_has_async_calls(cli)) {
3368 * Can't use sync call while an async call is in flight
3370 status = NT_STATUS_INVALID_PARAMETER;
3371 goto fail;
3374 ev = event_context_init(frame);
3375 if (ev == NULL) {
3376 status = NT_STATUS_NO_MEMORY;
3377 goto fail;
3380 req = cli_getatr_send(frame, ev, cli, fname);
3381 if (req == NULL) {
3382 status = NT_STATUS_NO_MEMORY;
3383 goto fail;
3386 if (!tevent_req_poll(req, ev)) {
3387 status = map_nt_error_from_unix(errno);
3388 goto fail;
3391 status = cli_getatr_recv(req,
3392 attr,
3393 size,
3394 write_time);
3396 fail:
3397 TALLOC_FREE(frame);
3398 if (!NT_STATUS_IS_OK(status)) {
3399 cli_set_error(cli, status);
3401 return status;
3404 /****************************************************************************
3405 Do a SMBsetattrE call.
3406 ****************************************************************************/
3408 static void cli_setattrE_done(struct tevent_req *subreq);
3410 struct cli_setattrE_state {
3411 uint16_t vwv[7];
3414 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3415 struct event_context *ev,
3416 struct cli_state *cli,
3417 uint16_t fnum,
3418 time_t change_time,
3419 time_t access_time,
3420 time_t write_time)
3422 struct tevent_req *req = NULL, *subreq = NULL;
3423 struct cli_setattrE_state *state = NULL;
3424 uint8_t additional_flags = 0;
3426 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3427 if (req == NULL) {
3428 return NULL;
3431 SSVAL(state->vwv+0, 0, fnum);
3432 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3433 cli->serverzone);
3434 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3435 cli->serverzone);
3436 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3437 cli->serverzone);
3439 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3440 7, state->vwv, 0, NULL);
3441 if (tevent_req_nomem(subreq, req)) {
3442 return tevent_req_post(req, ev);
3444 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3445 return req;
3448 static void cli_setattrE_done(struct tevent_req *subreq)
3450 struct tevent_req *req = tevent_req_callback_data(
3451 subreq, struct tevent_req);
3452 NTSTATUS status;
3454 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3455 TALLOC_FREE(subreq);
3456 if (!NT_STATUS_IS_OK(status)) {
3457 tevent_req_nterror(req, status);
3458 return;
3460 tevent_req_done(req);
3463 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3465 return tevent_req_simple_recv_ntstatus(req);
3468 NTSTATUS cli_setattrE(struct cli_state *cli,
3469 uint16_t fnum,
3470 time_t change_time,
3471 time_t access_time,
3472 time_t write_time)
3474 TALLOC_CTX *frame = talloc_stackframe();
3475 struct event_context *ev = NULL;
3476 struct tevent_req *req = NULL;
3477 NTSTATUS status = NT_STATUS_OK;
3479 if (cli_has_async_calls(cli)) {
3481 * Can't use sync call while an async call is in flight
3483 status = NT_STATUS_INVALID_PARAMETER;
3484 goto fail;
3487 ev = event_context_init(frame);
3488 if (ev == NULL) {
3489 status = NT_STATUS_NO_MEMORY;
3490 goto fail;
3493 req = cli_setattrE_send(frame, ev,
3494 cli,
3495 fnum,
3496 change_time,
3497 access_time,
3498 write_time);
3500 if (req == NULL) {
3501 status = NT_STATUS_NO_MEMORY;
3502 goto fail;
3505 if (!tevent_req_poll(req, ev)) {
3506 status = map_nt_error_from_unix(errno);
3507 goto fail;
3510 status = cli_setattrE_recv(req);
3512 fail:
3513 TALLOC_FREE(frame);
3514 if (!NT_STATUS_IS_OK(status)) {
3515 cli_set_error(cli, status);
3517 return status;
3520 /****************************************************************************
3521 Do a SMBsetatr call.
3522 ****************************************************************************/
3524 static void cli_setatr_done(struct tevent_req *subreq);
3526 struct cli_setatr_state {
3527 uint16_t vwv[8];
3530 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3531 struct event_context *ev,
3532 struct cli_state *cli,
3533 const char *fname,
3534 uint16_t attr,
3535 time_t mtime)
3537 struct tevent_req *req = NULL, *subreq = NULL;
3538 struct cli_setatr_state *state = NULL;
3539 uint8_t additional_flags = 0;
3540 uint8_t *bytes = NULL;
3542 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3543 if (req == NULL) {
3544 return NULL;
3547 SSVAL(state->vwv+0, 0, attr);
3548 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3550 bytes = talloc_array(state, uint8_t, 1);
3551 if (tevent_req_nomem(bytes, req)) {
3552 return tevent_req_post(req, ev);
3554 bytes[0] = 4;
3555 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3556 strlen(fname)+1, NULL);
3557 if (tevent_req_nomem(bytes, req)) {
3558 return tevent_req_post(req, ev);
3560 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3561 talloc_get_size(bytes)+1);
3562 if (tevent_req_nomem(bytes, req)) {
3563 return tevent_req_post(req, ev);
3566 bytes[talloc_get_size(bytes)-1] = 4;
3567 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3568 1, NULL);
3569 if (tevent_req_nomem(bytes, req)) {
3570 return tevent_req_post(req, ev);
3573 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3574 8, state->vwv, talloc_get_size(bytes), bytes);
3575 if (tevent_req_nomem(subreq, req)) {
3576 return tevent_req_post(req, ev);
3578 tevent_req_set_callback(subreq, cli_setatr_done, req);
3579 return req;
3582 static void cli_setatr_done(struct tevent_req *subreq)
3584 struct tevent_req *req = tevent_req_callback_data(
3585 subreq, struct tevent_req);
3586 NTSTATUS status;
3588 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3589 TALLOC_FREE(subreq);
3590 if (!NT_STATUS_IS_OK(status)) {
3591 tevent_req_nterror(req, status);
3592 return;
3594 tevent_req_done(req);
3597 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3599 return tevent_req_simple_recv_ntstatus(req);
3602 NTSTATUS cli_setatr(struct cli_state *cli,
3603 const char *fname,
3604 uint16_t attr,
3605 time_t mtime)
3607 TALLOC_CTX *frame = talloc_stackframe();
3608 struct event_context *ev = NULL;
3609 struct tevent_req *req = NULL;
3610 NTSTATUS status = NT_STATUS_OK;
3612 if (cli_has_async_calls(cli)) {
3614 * Can't use sync call while an async call is in flight
3616 status = NT_STATUS_INVALID_PARAMETER;
3617 goto fail;
3620 ev = event_context_init(frame);
3621 if (ev == NULL) {
3622 status = NT_STATUS_NO_MEMORY;
3623 goto fail;
3626 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3627 if (req == NULL) {
3628 status = NT_STATUS_NO_MEMORY;
3629 goto fail;
3632 if (!tevent_req_poll(req, ev)) {
3633 status = map_nt_error_from_unix(errno);
3634 goto fail;
3637 status = cli_setatr_recv(req);
3639 fail:
3640 TALLOC_FREE(frame);
3641 if (!NT_STATUS_IS_OK(status)) {
3642 cli_set_error(cli, status);
3644 return status;
3647 /****************************************************************************
3648 Check for existance of a dir.
3649 ****************************************************************************/
3651 static void cli_chkpath_done(struct tevent_req *subreq);
3653 struct cli_chkpath_state {
3654 int dummy;
3657 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3658 struct event_context *ev,
3659 struct cli_state *cli,
3660 const char *fname)
3662 struct tevent_req *req = NULL, *subreq = NULL;
3663 struct cli_chkpath_state *state = NULL;
3664 uint8_t additional_flags = 0;
3665 uint8_t *bytes = NULL;
3667 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3668 if (req == NULL) {
3669 return NULL;
3672 bytes = talloc_array(state, uint8_t, 1);
3673 if (tevent_req_nomem(bytes, req)) {
3674 return tevent_req_post(req, ev);
3676 bytes[0] = 4;
3677 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3678 strlen(fname)+1, NULL);
3680 if (tevent_req_nomem(bytes, req)) {
3681 return tevent_req_post(req, ev);
3684 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3685 0, NULL, talloc_get_size(bytes), bytes);
3686 if (tevent_req_nomem(subreq, req)) {
3687 return tevent_req_post(req, ev);
3689 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3690 return req;
3693 static void cli_chkpath_done(struct tevent_req *subreq)
3695 struct tevent_req *req = tevent_req_callback_data(
3696 subreq, struct tevent_req);
3697 NTSTATUS status;
3699 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3700 TALLOC_FREE(subreq);
3701 if (!NT_STATUS_IS_OK(status)) {
3702 tevent_req_nterror(req, status);
3703 return;
3705 tevent_req_done(req);
3708 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3710 return tevent_req_simple_recv_ntstatus(req);
3713 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3715 TALLOC_CTX *frame = talloc_stackframe();
3716 struct event_context *ev = NULL;
3717 struct tevent_req *req = NULL;
3718 char *path2 = NULL;
3719 NTSTATUS status = NT_STATUS_OK;
3721 if (cli_has_async_calls(cli)) {
3723 * Can't use sync call while an async call is in flight
3725 status = NT_STATUS_INVALID_PARAMETER;
3726 goto fail;
3729 path2 = talloc_strdup(frame, path);
3730 if (!path2) {
3731 status = NT_STATUS_NO_MEMORY;
3732 goto fail;
3734 trim_char(path2,'\0','\\');
3735 if (!*path2) {
3736 path2 = talloc_strdup(frame, "\\");
3737 if (!path2) {
3738 status = NT_STATUS_NO_MEMORY;
3739 goto fail;
3743 ev = event_context_init(frame);
3744 if (ev == NULL) {
3745 status = NT_STATUS_NO_MEMORY;
3746 goto fail;
3749 req = cli_chkpath_send(frame, ev, cli, path2);
3750 if (req == NULL) {
3751 status = NT_STATUS_NO_MEMORY;
3752 goto fail;
3755 if (!tevent_req_poll(req, ev)) {
3756 status = map_nt_error_from_unix(errno);
3757 goto fail;
3760 status = cli_chkpath_recv(req);
3762 fail:
3763 TALLOC_FREE(frame);
3764 if (!NT_STATUS_IS_OK(status)) {
3765 cli_set_error(cli, status);
3767 return status;
3770 /****************************************************************************
3771 Query disk space.
3772 ****************************************************************************/
3774 static void cli_dskattr_done(struct tevent_req *subreq);
3776 struct cli_dskattr_state {
3777 int bsize;
3778 int total;
3779 int avail;
3782 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3783 struct event_context *ev,
3784 struct cli_state *cli)
3786 struct tevent_req *req = NULL, *subreq = NULL;
3787 struct cli_dskattr_state *state = NULL;
3788 uint8_t additional_flags = 0;
3790 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3791 if (req == NULL) {
3792 return NULL;
3795 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3796 0, NULL, 0, NULL);
3797 if (tevent_req_nomem(subreq, req)) {
3798 return tevent_req_post(req, ev);
3800 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3801 return req;
3804 static void cli_dskattr_done(struct tevent_req *subreq)
3806 struct tevent_req *req = tevent_req_callback_data(
3807 subreq, struct tevent_req);
3808 struct cli_dskattr_state *state = tevent_req_data(
3809 req, struct cli_dskattr_state);
3810 uint8_t wct;
3811 uint16_t *vwv = NULL;
3812 uint8_t *inbuf;
3813 NTSTATUS status;
3815 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3816 NULL);
3817 TALLOC_FREE(subreq);
3818 if (!NT_STATUS_IS_OK(status)) {
3819 tevent_req_nterror(req, status);
3820 return;
3822 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3823 state->total = SVAL(vwv+0, 0);
3824 state->avail = SVAL(vwv+3, 0);
3825 tevent_req_done(req);
3828 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3830 struct cli_dskattr_state *state = tevent_req_data(
3831 req, struct cli_dskattr_state);
3832 NTSTATUS status;
3834 if (tevent_req_is_nterror(req, &status)) {
3835 return status;
3837 *bsize = state->bsize;
3838 *total = state->total;
3839 *avail = state->avail;
3840 return NT_STATUS_OK;
3843 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3845 TALLOC_CTX *frame = talloc_stackframe();
3846 struct event_context *ev = NULL;
3847 struct tevent_req *req = NULL;
3848 NTSTATUS status = NT_STATUS_OK;
3850 if (cli_has_async_calls(cli)) {
3852 * Can't use sync call while an async call is in flight
3854 status = NT_STATUS_INVALID_PARAMETER;
3855 goto fail;
3858 ev = event_context_init(frame);
3859 if (ev == NULL) {
3860 status = NT_STATUS_NO_MEMORY;
3861 goto fail;
3864 req = cli_dskattr_send(frame, ev, cli);
3865 if (req == NULL) {
3866 status = NT_STATUS_NO_MEMORY;
3867 goto fail;
3870 if (!tevent_req_poll(req, ev)) {
3871 status = map_nt_error_from_unix(errno);
3872 goto fail;
3875 status = cli_dskattr_recv(req, bsize, total, avail);
3877 fail:
3878 TALLOC_FREE(frame);
3879 if (!NT_STATUS_IS_OK(status)) {
3880 cli_set_error(cli, status);
3882 return status;
3885 /****************************************************************************
3886 Create and open a temporary file.
3887 ****************************************************************************/
3889 static void cli_ctemp_done(struct tevent_req *subreq);
3891 struct ctemp_state {
3892 uint16_t vwv[3];
3893 char *ret_path;
3894 uint16_t fnum;
3897 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3898 struct event_context *ev,
3899 struct cli_state *cli,
3900 const char *path)
3902 struct tevent_req *req = NULL, *subreq = NULL;
3903 struct ctemp_state *state = NULL;
3904 uint8_t additional_flags = 0;
3905 uint8_t *bytes = NULL;
3907 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3908 if (req == NULL) {
3909 return NULL;
3912 SSVAL(state->vwv,0,0);
3913 SIVALS(state->vwv+1,0,-1);
3915 bytes = talloc_array(state, uint8_t, 1);
3916 if (tevent_req_nomem(bytes, req)) {
3917 return tevent_req_post(req, ev);
3919 bytes[0] = 4;
3920 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3921 strlen(path)+1, NULL);
3922 if (tevent_req_nomem(bytes, req)) {
3923 return tevent_req_post(req, ev);
3926 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3927 3, state->vwv, talloc_get_size(bytes), bytes);
3928 if (tevent_req_nomem(subreq, req)) {
3929 return tevent_req_post(req, ev);
3931 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3932 return req;
3935 static void cli_ctemp_done(struct tevent_req *subreq)
3937 struct tevent_req *req = tevent_req_callback_data(
3938 subreq, struct tevent_req);
3939 struct ctemp_state *state = tevent_req_data(
3940 req, struct ctemp_state);
3941 NTSTATUS status;
3942 uint8_t wcnt;
3943 uint16_t *vwv;
3944 uint32_t num_bytes = 0;
3945 uint8_t *bytes = NULL;
3946 uint8_t *inbuf;
3948 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3949 &num_bytes, &bytes);
3950 TALLOC_FREE(subreq);
3951 if (!NT_STATUS_IS_OK(status)) {
3952 tevent_req_nterror(req, status);
3953 return;
3956 state->fnum = SVAL(vwv+0, 0);
3958 /* From W2K3, the result is just the ASCII name */
3959 if (num_bytes < 2) {
3960 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3961 return;
3964 if (pull_string_talloc(state,
3965 NULL,
3967 &state->ret_path,
3968 bytes,
3969 num_bytes,
3970 STR_ASCII) == 0) {
3971 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3972 return;
3974 tevent_req_done(req);
3977 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3978 TALLOC_CTX *ctx,
3979 uint16_t *pfnum,
3980 char **outfile)
3982 struct ctemp_state *state = tevent_req_data(req,
3983 struct ctemp_state);
3984 NTSTATUS status;
3986 if (tevent_req_is_nterror(req, &status)) {
3987 return status;
3989 *pfnum = state->fnum;
3990 *outfile = talloc_strdup(ctx, state->ret_path);
3991 if (!*outfile) {
3992 return NT_STATUS_NO_MEMORY;
3994 return NT_STATUS_OK;
3997 NTSTATUS cli_ctemp(struct cli_state *cli,
3998 TALLOC_CTX *ctx,
3999 const char *path,
4000 uint16_t *pfnum,
4001 char **out_path)
4003 TALLOC_CTX *frame = talloc_stackframe();
4004 struct event_context *ev;
4005 struct tevent_req *req;
4006 NTSTATUS status = NT_STATUS_OK;
4008 if (cli_has_async_calls(cli)) {
4010 * Can't use sync call while an async call is in flight
4012 status = NT_STATUS_INVALID_PARAMETER;
4013 goto fail;
4016 ev = event_context_init(frame);
4017 if (ev == NULL) {
4018 status = NT_STATUS_NO_MEMORY;
4019 goto fail;
4022 req = cli_ctemp_send(frame, ev, cli, path);
4023 if (req == NULL) {
4024 status = NT_STATUS_NO_MEMORY;
4025 goto fail;
4028 if (!tevent_req_poll(req, ev)) {
4029 status = map_nt_error_from_unix(errno);
4030 goto fail;
4033 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4035 fail:
4036 TALLOC_FREE(frame);
4037 if (!NT_STATUS_IS_OK(status)) {
4038 cli_set_error(cli, status);
4040 return status;
4044 send a raw ioctl - used by the torture code
4046 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4048 uint16_t vwv[3];
4049 NTSTATUS status;
4050 struct tevent_req *result_parent;
4052 SSVAL(vwv+0, 0, fnum);
4053 SSVAL(vwv+1, 0, code>>16);
4054 SSVAL(vwv+2, 0, (code&0xFFFF));
4056 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4057 &result_parent, 0, NULL, NULL, NULL, NULL);
4058 if (!NT_STATUS_IS_OK(status)) {
4059 return status;
4061 *blob = data_blob_null;
4062 return NT_STATUS_OK;
4065 /*********************************************************
4066 Set an extended attribute utility fn.
4067 *********************************************************/
4069 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4070 uint8_t *param, unsigned int param_len,
4071 const char *ea_name,
4072 const char *ea_val, size_t ea_len)
4074 uint16_t setup[1];
4075 unsigned int data_len = 0;
4076 uint8_t *data = NULL;
4077 char *p;
4078 size_t ea_namelen = strlen(ea_name);
4079 NTSTATUS status;
4081 SSVAL(setup, 0, setup_val);
4083 if (ea_namelen == 0 && ea_len == 0) {
4084 data_len = 4;
4085 data = (uint8_t *)SMB_MALLOC(data_len);
4086 if (!data) {
4087 return NT_STATUS_NO_MEMORY;
4089 p = (char *)data;
4090 SIVAL(p,0,data_len);
4091 } else {
4092 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4093 data = (uint8_t *)SMB_MALLOC(data_len);
4094 if (!data) {
4095 return NT_STATUS_NO_MEMORY;
4097 p = (char *)data;
4098 SIVAL(p,0,data_len);
4099 p += 4;
4100 SCVAL(p, 0, 0); /* EA flags. */
4101 SCVAL(p, 1, ea_namelen);
4102 SSVAL(p, 2, ea_len);
4103 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4104 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4107 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4108 setup, 1, 0,
4109 param, param_len, 2,
4110 data, data_len, cli->max_xmit,
4111 NULL,
4112 NULL, 0, NULL, /* rsetup */
4113 NULL, 0, NULL, /* rparam */
4114 NULL, 0, NULL); /* rdata */
4115 SAFE_FREE(data);
4116 return status;
4119 /*********************************************************
4120 Set an extended attribute on a pathname.
4121 *********************************************************/
4123 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4124 const char *ea_name, const char *ea_val,
4125 size_t ea_len)
4127 unsigned int param_len = 0;
4128 uint8_t *param;
4129 size_t srclen = 2*(strlen(path)+1);
4130 char *p;
4131 NTSTATUS status;
4133 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4134 if (!param) {
4135 return NT_STATUS_NO_MEMORY;
4137 memset(param, '\0', 6);
4138 SSVAL(param,0,SMB_INFO_SET_EA);
4139 p = (char *)(&param[6]);
4141 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4142 param_len = PTR_DIFF(p, param);
4144 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4145 ea_name, ea_val, ea_len);
4146 SAFE_FREE(param);
4147 return status;
4150 /*********************************************************
4151 Set an extended attribute on an fnum.
4152 *********************************************************/
4154 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4155 const char *ea_name, const char *ea_val,
4156 size_t ea_len)
4158 uint8_t param[6];
4160 memset(param, 0, 6);
4161 SSVAL(param,0,fnum);
4162 SSVAL(param,2,SMB_INFO_SET_EA);
4164 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4165 ea_name, ea_val, ea_len);
4168 /*********************************************************
4169 Get an extended attribute list utility fn.
4170 *********************************************************/
4172 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4173 size_t rdata_len,
4174 size_t *pnum_eas, struct ea_struct **pea_list)
4176 struct ea_struct *ea_list = NULL;
4177 size_t num_eas;
4178 size_t ea_size;
4179 const uint8_t *p;
4181 if (rdata_len < 4) {
4182 return false;
4185 ea_size = (size_t)IVAL(rdata,0);
4186 if (ea_size > rdata_len) {
4187 return false;
4190 if (ea_size == 0) {
4191 /* No EA's present. */
4192 *pnum_eas = 0;
4193 *pea_list = NULL;
4194 return true;
4197 p = rdata + 4;
4198 ea_size -= 4;
4200 /* Validate the EA list and count it. */
4201 for (num_eas = 0; ea_size >= 4; num_eas++) {
4202 unsigned int ea_namelen = CVAL(p,1);
4203 unsigned int ea_valuelen = SVAL(p,2);
4204 if (ea_namelen == 0) {
4205 return false;
4207 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4208 return false;
4210 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4211 p += 4 + ea_namelen + 1 + ea_valuelen;
4214 if (num_eas == 0) {
4215 *pnum_eas = 0;
4216 *pea_list = NULL;
4217 return true;
4220 *pnum_eas = num_eas;
4221 if (!pea_list) {
4222 /* Caller only wants number of EA's. */
4223 return true;
4226 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4227 if (!ea_list) {
4228 return false;
4231 ea_size = (size_t)IVAL(rdata,0);
4232 p = rdata + 4;
4234 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4235 struct ea_struct *ea = &ea_list[num_eas];
4236 fstring unix_ea_name;
4237 unsigned int ea_namelen = CVAL(p,1);
4238 unsigned int ea_valuelen = SVAL(p,2);
4240 ea->flags = CVAL(p,0);
4241 unix_ea_name[0] = '\0';
4242 pull_ascii_fstring(unix_ea_name, p + 4);
4243 ea->name = talloc_strdup(ea_list, unix_ea_name);
4244 if (!ea->name) {
4245 goto fail;
4247 /* Ensure the value is null terminated (in case it's a string). */
4248 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4249 if (!ea->value.data) {
4250 goto fail;
4252 if (ea_valuelen) {
4253 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4255 ea->value.data[ea_valuelen] = 0;
4256 ea->value.length--;
4257 p += 4 + ea_namelen + 1 + ea_valuelen;
4260 *pea_list = ea_list;
4261 return true;
4263 fail:
4264 TALLOC_FREE(ea_list);
4265 return false;
4268 /*********************************************************
4269 Get an extended attribute list from a pathname.
4270 *********************************************************/
4272 struct cli_get_ea_list_path_state {
4273 uint32_t num_data;
4274 uint8_t *data;
4277 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4279 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4280 struct tevent_context *ev,
4281 struct cli_state *cli,
4282 const char *fname)
4284 struct tevent_req *req, *subreq;
4285 struct cli_get_ea_list_path_state *state;
4287 req = tevent_req_create(mem_ctx, &state,
4288 struct cli_get_ea_list_path_state);
4289 if (req == NULL) {
4290 return NULL;
4292 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4293 SMB_INFO_QUERY_ALL_EAS, 4,
4294 cli->max_xmit);
4295 if (tevent_req_nomem(subreq, req)) {
4296 return tevent_req_post(req, ev);
4298 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4299 return req;
4302 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4304 struct tevent_req *req = tevent_req_callback_data(
4305 subreq, struct tevent_req);
4306 struct cli_get_ea_list_path_state *state = tevent_req_data(
4307 req, struct cli_get_ea_list_path_state);
4308 NTSTATUS status;
4310 status = cli_qpathinfo_recv(subreq, state, &state->data,
4311 &state->num_data);
4312 TALLOC_FREE(subreq);
4313 if (!NT_STATUS_IS_OK(status)) {
4314 tevent_req_nterror(req, status);
4315 return;
4317 tevent_req_done(req);
4320 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4321 size_t *pnum_eas, struct ea_struct **peas)
4323 struct cli_get_ea_list_path_state *state = tevent_req_data(
4324 req, struct cli_get_ea_list_path_state);
4325 NTSTATUS status;
4327 if (tevent_req_is_nterror(req, &status)) {
4328 return status;
4330 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4331 pnum_eas, peas)) {
4332 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4334 return NT_STATUS_OK;
4337 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4338 TALLOC_CTX *ctx,
4339 size_t *pnum_eas,
4340 struct ea_struct **pea_list)
4342 TALLOC_CTX *frame = talloc_stackframe();
4343 struct event_context *ev = NULL;
4344 struct tevent_req *req = NULL;
4345 NTSTATUS status = NT_STATUS_NO_MEMORY;
4347 if (cli_has_async_calls(cli)) {
4349 * Can't use sync call while an async call is in flight
4351 status = NT_STATUS_INVALID_PARAMETER;
4352 goto fail;
4354 ev = event_context_init(frame);
4355 if (ev == NULL) {
4356 goto fail;
4358 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4359 if (req == NULL) {
4360 goto fail;
4362 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4363 goto fail;
4365 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4366 fail:
4367 TALLOC_FREE(frame);
4368 if (!NT_STATUS_IS_OK(status)) {
4369 cli_set_error(cli, status);
4371 return status;
4374 /****************************************************************************
4375 Convert open "flags" arg to uint32_t on wire.
4376 ****************************************************************************/
4378 static uint32_t open_flags_to_wire(int flags)
4380 int open_mode = flags & O_ACCMODE;
4381 uint32_t ret = 0;
4383 switch (open_mode) {
4384 case O_WRONLY:
4385 ret |= SMB_O_WRONLY;
4386 break;
4387 case O_RDWR:
4388 ret |= SMB_O_RDWR;
4389 break;
4390 default:
4391 case O_RDONLY:
4392 ret |= SMB_O_RDONLY;
4393 break;
4396 if (flags & O_CREAT) {
4397 ret |= SMB_O_CREAT;
4399 if (flags & O_EXCL) {
4400 ret |= SMB_O_EXCL;
4402 if (flags & O_TRUNC) {
4403 ret |= SMB_O_TRUNC;
4405 #if defined(O_SYNC)
4406 if (flags & O_SYNC) {
4407 ret |= SMB_O_SYNC;
4409 #endif /* O_SYNC */
4410 if (flags & O_APPEND) {
4411 ret |= SMB_O_APPEND;
4413 #if defined(O_DIRECT)
4414 if (flags & O_DIRECT) {
4415 ret |= SMB_O_DIRECT;
4417 #endif
4418 #if defined(O_DIRECTORY)
4419 if (flags & O_DIRECTORY) {
4420 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4421 ret |= SMB_O_DIRECTORY;
4423 #endif
4424 return ret;
4427 /****************************************************************************
4428 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4429 ****************************************************************************/
4431 struct posix_open_state {
4432 uint16_t setup;
4433 uint8_t *param;
4434 uint8_t data[18];
4435 uint16_t fnum; /* Out */
4438 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4440 struct tevent_req *req = tevent_req_callback_data(
4441 subreq, struct tevent_req);
4442 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4443 NTSTATUS status;
4444 uint8_t *data;
4445 uint32_t num_data;
4447 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4448 NULL, 0, NULL, &data, 12, &num_data);
4449 TALLOC_FREE(subreq);
4450 if (!NT_STATUS_IS_OK(status)) {
4451 tevent_req_nterror(req, status);
4452 return;
4454 state->fnum = SVAL(data,2);
4455 tevent_req_done(req);
4458 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4459 struct event_context *ev,
4460 struct cli_state *cli,
4461 const char *fname,
4462 int flags,
4463 mode_t mode,
4464 bool is_dir)
4466 struct tevent_req *req = NULL, *subreq = NULL;
4467 struct posix_open_state *state = NULL;
4468 uint32_t wire_flags = open_flags_to_wire(flags);
4470 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4471 if (req == NULL) {
4472 return NULL;
4475 /* Setup setup word. */
4476 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4478 /* Setup param array. */
4479 state->param = talloc_array(state, uint8_t, 6);
4480 if (tevent_req_nomem(state->param, req)) {
4481 return tevent_req_post(req, ev);
4483 memset(state->param, '\0', 6);
4484 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4486 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4487 strlen(fname)+1, NULL);
4489 if (tevent_req_nomem(state->param, req)) {
4490 return tevent_req_post(req, ev);
4493 /* Setup data words. */
4494 if (is_dir) {
4495 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4496 wire_flags |= SMB_O_DIRECTORY;
4499 SIVAL(state->data,0,0); /* No oplock. */
4500 SIVAL(state->data,4,wire_flags);
4501 SIVAL(state->data,8,unix_perms_to_wire(mode));
4502 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4503 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4505 subreq = cli_trans_send(state, /* mem ctx. */
4506 ev, /* event ctx. */
4507 cli, /* cli_state. */
4508 SMBtrans2, /* cmd. */
4509 NULL, /* pipe name. */
4510 -1, /* fid. */
4511 0, /* function. */
4512 0, /* flags. */
4513 &state->setup, /* setup. */
4514 1, /* num setup uint16_t words. */
4515 0, /* max returned setup. */
4516 state->param, /* param. */
4517 talloc_get_size(state->param),/* num param. */
4518 2, /* max returned param. */
4519 state->data, /* data. */
4520 18, /* num data. */
4521 12); /* max returned data. */
4523 if (tevent_req_nomem(subreq, req)) {
4524 return tevent_req_post(req, ev);
4526 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4527 return req;
4530 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4531 struct event_context *ev,
4532 struct cli_state *cli,
4533 const char *fname,
4534 int flags,
4535 mode_t mode)
4537 return cli_posix_open_internal_send(mem_ctx, ev,
4538 cli, fname, flags, mode, false);
4541 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4543 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4544 NTSTATUS status;
4546 if (tevent_req_is_nterror(req, &status)) {
4547 return status;
4549 *pfnum = state->fnum;
4550 return NT_STATUS_OK;
4553 /****************************************************************************
4554 Open - POSIX semantics. Doesn't request oplock.
4555 ****************************************************************************/
4557 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4558 int flags, mode_t mode, uint16_t *pfnum)
4561 TALLOC_CTX *frame = talloc_stackframe();
4562 struct event_context *ev = NULL;
4563 struct tevent_req *req = NULL;
4564 NTSTATUS status = NT_STATUS_OK;
4566 if (cli_has_async_calls(cli)) {
4568 * Can't use sync call while an async call is in flight
4570 status = NT_STATUS_INVALID_PARAMETER;
4571 goto fail;
4574 ev = event_context_init(frame);
4575 if (ev == NULL) {
4576 status = NT_STATUS_NO_MEMORY;
4577 goto fail;
4580 req = cli_posix_open_send(frame,
4582 cli,
4583 fname,
4584 flags,
4585 mode);
4586 if (req == NULL) {
4587 status = NT_STATUS_NO_MEMORY;
4588 goto fail;
4591 if (!tevent_req_poll(req, ev)) {
4592 status = map_nt_error_from_unix(errno);
4593 goto fail;
4596 status = cli_posix_open_recv(req, pfnum);
4598 fail:
4599 TALLOC_FREE(frame);
4600 if (!NT_STATUS_IS_OK(status)) {
4601 cli_set_error(cli, status);
4603 return status;
4606 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4607 struct event_context *ev,
4608 struct cli_state *cli,
4609 const char *fname,
4610 mode_t mode)
4612 return cli_posix_open_internal_send(mem_ctx, ev,
4613 cli, fname, O_CREAT, mode, true);
4616 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4618 return tevent_req_simple_recv_ntstatus(req);
4621 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4623 TALLOC_CTX *frame = talloc_stackframe();
4624 struct event_context *ev = NULL;
4625 struct tevent_req *req = NULL;
4626 NTSTATUS status = NT_STATUS_OK;
4628 if (cli_has_async_calls(cli)) {
4630 * Can't use sync call while an async call is in flight
4632 status = NT_STATUS_INVALID_PARAMETER;
4633 goto fail;
4636 ev = event_context_init(frame);
4637 if (ev == NULL) {
4638 status = NT_STATUS_NO_MEMORY;
4639 goto fail;
4642 req = cli_posix_mkdir_send(frame,
4644 cli,
4645 fname,
4646 mode);
4647 if (req == NULL) {
4648 status = NT_STATUS_NO_MEMORY;
4649 goto fail;
4652 if (!tevent_req_poll(req, ev)) {
4653 status = map_nt_error_from_unix(errno);
4654 goto fail;
4657 status = cli_posix_mkdir_recv(req);
4659 fail:
4660 TALLOC_FREE(frame);
4661 if (!NT_STATUS_IS_OK(status)) {
4662 cli_set_error(cli, status);
4664 return status;
4667 /****************************************************************************
4668 unlink or rmdir - POSIX semantics.
4669 ****************************************************************************/
4671 struct cli_posix_unlink_internal_state {
4672 uint8_t data[2];
4675 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4677 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4678 struct event_context *ev,
4679 struct cli_state *cli,
4680 const char *fname,
4681 uint16_t level)
4683 struct tevent_req *req = NULL, *subreq = NULL;
4684 struct cli_posix_unlink_internal_state *state = NULL;
4686 req = tevent_req_create(mem_ctx, &state,
4687 struct cli_posix_unlink_internal_state);
4688 if (req == NULL) {
4689 return NULL;
4692 /* Setup data word. */
4693 SSVAL(state->data, 0, level);
4695 subreq = cli_setpathinfo_send(state, ev, cli,
4696 SMB_POSIX_PATH_UNLINK,
4697 fname,
4698 state->data, sizeof(state->data));
4699 if (tevent_req_nomem(subreq, req)) {
4700 return tevent_req_post(req, ev);
4702 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4703 return req;
4706 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4708 NTSTATUS status = cli_setpathinfo_recv(subreq);
4709 tevent_req_simple_finish_ntstatus(subreq, status);
4712 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4713 struct event_context *ev,
4714 struct cli_state *cli,
4715 const char *fname)
4717 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4718 SMB_POSIX_UNLINK_FILE_TARGET);
4721 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4723 return tevent_req_simple_recv_ntstatus(req);
4726 /****************************************************************************
4727 unlink - POSIX semantics.
4728 ****************************************************************************/
4730 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4732 TALLOC_CTX *frame = talloc_stackframe();
4733 struct event_context *ev = NULL;
4734 struct tevent_req *req = NULL;
4735 NTSTATUS status = NT_STATUS_OK;
4737 if (cli_has_async_calls(cli)) {
4739 * Can't use sync call while an async call is in flight
4741 status = NT_STATUS_INVALID_PARAMETER;
4742 goto fail;
4745 ev = event_context_init(frame);
4746 if (ev == NULL) {
4747 status = NT_STATUS_NO_MEMORY;
4748 goto fail;
4751 req = cli_posix_unlink_send(frame,
4753 cli,
4754 fname);
4755 if (req == NULL) {
4756 status = NT_STATUS_NO_MEMORY;
4757 goto fail;
4760 if (!tevent_req_poll(req, ev)) {
4761 status = map_nt_error_from_unix(errno);
4762 goto fail;
4765 status = cli_posix_unlink_recv(req);
4767 fail:
4768 TALLOC_FREE(frame);
4769 if (!NT_STATUS_IS_OK(status)) {
4770 cli_set_error(cli, status);
4772 return status;
4775 /****************************************************************************
4776 rmdir - POSIX semantics.
4777 ****************************************************************************/
4779 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4780 struct event_context *ev,
4781 struct cli_state *cli,
4782 const char *fname)
4784 return cli_posix_unlink_internal_send(
4785 mem_ctx, ev, cli, fname,
4786 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4789 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4791 return tevent_req_simple_recv_ntstatus(req);
4794 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4796 TALLOC_CTX *frame = talloc_stackframe();
4797 struct event_context *ev = NULL;
4798 struct tevent_req *req = NULL;
4799 NTSTATUS status = NT_STATUS_OK;
4801 if (cli_has_async_calls(cli)) {
4803 * Can't use sync call while an async call is in flight
4805 status = NT_STATUS_INVALID_PARAMETER;
4806 goto fail;
4809 ev = event_context_init(frame);
4810 if (ev == NULL) {
4811 status = NT_STATUS_NO_MEMORY;
4812 goto fail;
4815 req = cli_posix_rmdir_send(frame,
4817 cli,
4818 fname);
4819 if (req == NULL) {
4820 status = NT_STATUS_NO_MEMORY;
4821 goto fail;
4824 if (!tevent_req_poll(req, ev)) {
4825 status = map_nt_error_from_unix(errno);
4826 goto fail;
4829 status = cli_posix_rmdir_recv(req, frame);
4831 fail:
4832 TALLOC_FREE(frame);
4833 if (!NT_STATUS_IS_OK(status)) {
4834 cli_set_error(cli, status);
4836 return status;
4839 /****************************************************************************
4840 filechangenotify
4841 ****************************************************************************/
4843 struct cli_notify_state {
4844 uint8_t setup[8];
4845 uint32_t num_changes;
4846 struct notify_change *changes;
4849 static void cli_notify_done(struct tevent_req *subreq);
4851 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4852 struct tevent_context *ev,
4853 struct cli_state *cli, uint16_t fnum,
4854 uint32_t buffer_size,
4855 uint32_t completion_filter, bool recursive)
4857 struct tevent_req *req, *subreq;
4858 struct cli_notify_state *state;
4860 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4861 if (req == NULL) {
4862 return NULL;
4865 SIVAL(state->setup, 0, completion_filter);
4866 SSVAL(state->setup, 4, fnum);
4867 SSVAL(state->setup, 6, recursive);
4869 subreq = cli_trans_send(
4870 state, /* mem ctx. */
4871 ev, /* event ctx. */
4872 cli, /* cli_state. */
4873 SMBnttrans, /* cmd. */
4874 NULL, /* pipe name. */
4875 -1, /* fid. */
4876 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4877 0, /* flags. */
4878 (uint16_t *)state->setup, /* setup. */
4879 4, /* num setup uint16_t words. */
4880 0, /* max returned setup. */
4881 NULL, /* param. */
4882 0, /* num param. */
4883 buffer_size, /* max returned param. */
4884 NULL, /* data. */
4885 0, /* num data. */
4886 0); /* max returned data. */
4888 if (tevent_req_nomem(subreq, req)) {
4889 return tevent_req_post(req, ev);
4891 tevent_req_set_callback(subreq, cli_notify_done, req);
4892 return req;
4895 static void cli_notify_done(struct tevent_req *subreq)
4897 struct tevent_req *req = tevent_req_callback_data(
4898 subreq, struct tevent_req);
4899 struct cli_notify_state *state = tevent_req_data(
4900 req, struct cli_notify_state);
4901 NTSTATUS status;
4902 uint8_t *params;
4903 uint32_t i, ofs, num_params;
4904 uint16_t flags2;
4906 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4907 &params, 0, &num_params, NULL, 0, NULL);
4908 TALLOC_FREE(subreq);
4909 if (!NT_STATUS_IS_OK(status)) {
4910 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4911 tevent_req_nterror(req, status);
4912 return;
4915 state->num_changes = 0;
4916 ofs = 0;
4918 while (num_params - ofs > 12) {
4919 uint32_t len = IVAL(params, ofs);
4920 state->num_changes += 1;
4922 if ((len == 0) || (ofs+len >= num_params)) {
4923 break;
4925 ofs += len;
4928 state->changes = talloc_array(state, struct notify_change,
4929 state->num_changes);
4930 if (tevent_req_nomem(state->changes, req)) {
4931 TALLOC_FREE(params);
4932 return;
4935 ofs = 0;
4937 for (i=0; i<state->num_changes; i++) {
4938 uint32_t next = IVAL(params, ofs);
4939 uint32_t len = IVAL(params, ofs+8);
4940 ssize_t ret;
4941 char *name;
4943 if ((next != 0) && (len+12 != next)) {
4944 TALLOC_FREE(params);
4945 tevent_req_nterror(
4946 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4947 return;
4950 state->changes[i].action = IVAL(params, ofs+4);
4951 ret = clistr_pull_talloc(params, (char *)params, flags2,
4952 &name, params+ofs+12, len,
4953 STR_TERMINATE|STR_UNICODE);
4954 if (ret == -1) {
4955 TALLOC_FREE(params);
4956 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4957 return;
4959 state->changes[i].name = name;
4960 ofs += next;
4963 TALLOC_FREE(params);
4964 tevent_req_done(req);
4967 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4968 uint32_t *pnum_changes,
4969 struct notify_change **pchanges)
4971 struct cli_notify_state *state = tevent_req_data(
4972 req, struct cli_notify_state);
4973 NTSTATUS status;
4975 if (tevent_req_is_nterror(req, &status)) {
4976 return status;
4979 *pnum_changes = state->num_changes;
4980 *pchanges = talloc_move(mem_ctx, &state->changes);
4981 return NT_STATUS_OK;
4984 struct cli_qpathinfo_state {
4985 uint8_t *param;
4986 uint8_t *data;
4987 uint16_t setup[1];
4988 uint32_t min_rdata;
4989 uint8_t *rdata;
4990 uint32_t num_rdata;
4993 static void cli_qpathinfo_done(struct tevent_req *subreq);
4995 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4996 struct tevent_context *ev,
4997 struct cli_state *cli, const char *fname,
4998 uint16_t level, uint32_t min_rdata,
4999 uint32_t max_rdata)
5001 struct tevent_req *req, *subreq;
5002 struct cli_qpathinfo_state *state;
5004 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5005 if (req == NULL) {
5006 return NULL;
5008 state->min_rdata = min_rdata;
5009 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5011 state->param = talloc_zero_array(state, uint8_t, 6);
5012 if (tevent_req_nomem(state->param, req)) {
5013 return tevent_req_post(req, ev);
5015 SSVAL(state->param, 0, level);
5016 state->param = trans2_bytes_push_str(
5017 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
5018 if (tevent_req_nomem(state->param, req)) {
5019 return tevent_req_post(req, ev);
5022 subreq = cli_trans_send(
5023 state, /* mem ctx. */
5024 ev, /* event ctx. */
5025 cli, /* cli_state. */
5026 SMBtrans2, /* cmd. */
5027 NULL, /* pipe name. */
5028 -1, /* fid. */
5029 0, /* function. */
5030 0, /* flags. */
5031 state->setup, /* setup. */
5032 1, /* num setup uint16_t words. */
5033 0, /* max returned setup. */
5034 state->param, /* param. */
5035 talloc_get_size(state->param), /* num param. */
5036 2, /* max returned param. */
5037 NULL, /* data. */
5038 0, /* num data. */
5039 max_rdata); /* max returned data. */
5041 if (tevent_req_nomem(subreq, req)) {
5042 return tevent_req_post(req, ev);
5044 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5045 return req;
5048 static void cli_qpathinfo_done(struct tevent_req *subreq)
5050 struct tevent_req *req = tevent_req_callback_data(
5051 subreq, struct tevent_req);
5052 struct cli_qpathinfo_state *state = tevent_req_data(
5053 req, struct cli_qpathinfo_state);
5054 NTSTATUS status;
5056 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5057 NULL, 0, NULL,
5058 &state->rdata, state->min_rdata,
5059 &state->num_rdata);
5060 if (!NT_STATUS_IS_OK(status)) {
5061 tevent_req_nterror(req, status);
5062 return;
5064 tevent_req_done(req);
5067 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5068 uint8_t **rdata, uint32_t *num_rdata)
5070 struct cli_qpathinfo_state *state = tevent_req_data(
5071 req, struct cli_qpathinfo_state);
5072 NTSTATUS status;
5074 if (tevent_req_is_nterror(req, &status)) {
5075 return status;
5077 if (rdata != NULL) {
5078 *rdata = talloc_move(mem_ctx, &state->rdata);
5079 } else {
5080 TALLOC_FREE(state->rdata);
5082 if (num_rdata != NULL) {
5083 *num_rdata = state->num_rdata;
5085 return NT_STATUS_OK;
5088 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5089 const char *fname, uint16_t level, uint32_t min_rdata,
5090 uint32_t max_rdata,
5091 uint8_t **rdata, uint32_t *num_rdata)
5093 TALLOC_CTX *frame = talloc_stackframe();
5094 struct event_context *ev;
5095 struct tevent_req *req;
5096 NTSTATUS status = NT_STATUS_NO_MEMORY;
5098 if (cli_has_async_calls(cli)) {
5100 * Can't use sync call while an async call is in flight
5102 status = NT_STATUS_INVALID_PARAMETER;
5103 goto fail;
5105 ev = event_context_init(frame);
5106 if (ev == NULL) {
5107 goto fail;
5109 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5110 max_rdata);
5111 if (req == NULL) {
5112 goto fail;
5114 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5115 goto fail;
5117 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5118 fail:
5119 TALLOC_FREE(frame);
5120 if (!NT_STATUS_IS_OK(status)) {
5121 cli_set_error(cli, status);
5123 return status;
5126 struct cli_qfileinfo_state {
5127 uint16_t setup[1];
5128 uint8_t param[4];
5129 uint8_t *data;
5130 uint32_t min_rdata;
5131 uint8_t *rdata;
5132 uint32_t num_rdata;
5135 static void cli_qfileinfo_done(struct tevent_req *subreq);
5137 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5138 struct tevent_context *ev,
5139 struct cli_state *cli, uint16_t fnum,
5140 uint16_t level, uint32_t min_rdata,
5141 uint32_t max_rdata)
5143 struct tevent_req *req, *subreq;
5144 struct cli_qfileinfo_state *state;
5146 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5147 if (req == NULL) {
5148 return NULL;
5150 state->min_rdata = min_rdata;
5151 SSVAL(state->param, 0, fnum);
5152 SSVAL(state->param, 2, level);
5153 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5155 subreq = cli_trans_send(
5156 state, /* mem ctx. */
5157 ev, /* event ctx. */
5158 cli, /* cli_state. */
5159 SMBtrans2, /* cmd. */
5160 NULL, /* pipe name. */
5161 -1, /* fid. */
5162 0, /* function. */
5163 0, /* flags. */
5164 state->setup, /* setup. */
5165 1, /* num setup uint16_t words. */
5166 0, /* max returned setup. */
5167 state->param, /* param. */
5168 sizeof(state->param), /* num param. */
5169 2, /* max returned param. */
5170 NULL, /* data. */
5171 0, /* num data. */
5172 max_rdata); /* max returned data. */
5174 if (tevent_req_nomem(subreq, req)) {
5175 return tevent_req_post(req, ev);
5177 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5178 return req;
5181 static void cli_qfileinfo_done(struct tevent_req *subreq)
5183 struct tevent_req *req = tevent_req_callback_data(
5184 subreq, struct tevent_req);
5185 struct cli_qfileinfo_state *state = tevent_req_data(
5186 req, struct cli_qfileinfo_state);
5187 NTSTATUS status;
5189 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5190 NULL, 0, NULL,
5191 &state->rdata, state->min_rdata,
5192 &state->num_rdata);
5193 if (!NT_STATUS_IS_OK(status)) {
5194 tevent_req_nterror(req, status);
5195 return;
5197 tevent_req_done(req);
5200 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5201 uint8_t **rdata, uint32_t *num_rdata)
5203 struct cli_qfileinfo_state *state = tevent_req_data(
5204 req, struct cli_qfileinfo_state);
5205 NTSTATUS status;
5207 if (tevent_req_is_nterror(req, &status)) {
5208 return status;
5210 if (rdata != NULL) {
5211 *rdata = talloc_move(mem_ctx, &state->rdata);
5212 } else {
5213 TALLOC_FREE(state->rdata);
5215 if (num_rdata != NULL) {
5216 *num_rdata = state->num_rdata;
5218 return NT_STATUS_OK;
5221 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5222 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5223 uint32_t max_rdata,
5224 uint8_t **rdata, uint32_t *num_rdata)
5226 TALLOC_CTX *frame = talloc_stackframe();
5227 struct event_context *ev;
5228 struct tevent_req *req;
5229 NTSTATUS status = NT_STATUS_NO_MEMORY;
5231 if (cli_has_async_calls(cli)) {
5233 * Can't use sync call while an async call is in flight
5235 status = NT_STATUS_INVALID_PARAMETER;
5236 goto fail;
5238 ev = event_context_init(frame);
5239 if (ev == NULL) {
5240 goto fail;
5242 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5243 max_rdata);
5244 if (req == NULL) {
5245 goto fail;
5247 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5248 goto fail;
5250 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5251 fail:
5252 TALLOC_FREE(frame);
5253 if (!NT_STATUS_IS_OK(status)) {
5254 cli_set_error(cli, status);
5256 return status;
5259 struct cli_flush_state {
5260 uint16_t vwv[1];
5263 static void cli_flush_done(struct tevent_req *subreq);
5265 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5266 struct event_context *ev,
5267 struct cli_state *cli,
5268 uint16_t fnum)
5270 struct tevent_req *req, *subreq;
5271 struct cli_flush_state *state;
5273 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5274 if (req == NULL) {
5275 return NULL;
5277 SSVAL(state->vwv + 0, 0, fnum);
5279 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5280 0, NULL);
5281 if (tevent_req_nomem(subreq, req)) {
5282 return tevent_req_post(req, ev);
5284 tevent_req_set_callback(subreq, cli_flush_done, req);
5285 return req;
5288 static void cli_flush_done(struct tevent_req *subreq)
5290 struct tevent_req *req = tevent_req_callback_data(
5291 subreq, struct tevent_req);
5292 NTSTATUS status;
5294 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5295 TALLOC_FREE(subreq);
5296 if (!NT_STATUS_IS_OK(status)) {
5297 tevent_req_nterror(req, status);
5298 return;
5300 tevent_req_done(req);
5303 NTSTATUS cli_flush_recv(struct tevent_req *req)
5305 return tevent_req_simple_recv_ntstatus(req);
5308 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5310 TALLOC_CTX *frame = talloc_stackframe();
5311 struct event_context *ev;
5312 struct tevent_req *req;
5313 NTSTATUS status = NT_STATUS_NO_MEMORY;
5315 if (cli_has_async_calls(cli)) {
5317 * Can't use sync call while an async call is in flight
5319 status = NT_STATUS_INVALID_PARAMETER;
5320 goto fail;
5322 ev = event_context_init(frame);
5323 if (ev == NULL) {
5324 goto fail;
5326 req = cli_flush_send(frame, ev, cli, fnum);
5327 if (req == NULL) {
5328 goto fail;
5330 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5331 goto fail;
5333 status = cli_flush_recv(req);
5334 fail:
5335 TALLOC_FREE(frame);
5336 if (!NT_STATUS_IS_OK(status)) {
5337 cli_set_error(cli, status);
5339 return status;