WHATSNEW: Update changes since rc1.
[Samba/gbeck.git] / source3 / libsmb / clifile.c
blob0de81b7e6e030799c8782c2b8f61ecb9287e6faa
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
27 #include "trans2.h"
28 #include "ntioctl.h"
30 /***********************************************************
31 Common function for pushing stings, used by smb_bytes_push_str()
32 and trans_bytes_push_str(). Only difference is the align_odd
33 parameter setting.
34 ***********************************************************/
36 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
37 const char *str, size_t str_len,
38 bool align_odd,
39 size_t *pconverted_size)
41 size_t buflen;
42 char *converted;
43 size_t converted_size;
45 if (buf == NULL) {
46 return NULL;
49 buflen = talloc_get_size(buf);
51 if (align_odd && ucs2 && (buflen % 2 == 0)) {
53 * We're pushing into an SMB buffer, align odd
55 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
56 if (buf == NULL) {
57 return NULL;
59 buf[buflen] = '\0';
60 buflen += 1;
63 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
64 ucs2 ? CH_UTF16LE : CH_DOS,
65 str, str_len, &converted,
66 &converted_size, true)) {
67 return NULL;
70 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
71 buflen + converted_size);
72 if (buf == NULL) {
73 TALLOC_FREE(converted);
74 return NULL;
77 memcpy(buf + buflen, converted, converted_size);
79 TALLOC_FREE(converted);
81 if (pconverted_size) {
82 *pconverted_size = converted_size;
85 return buf;
88 /***********************************************************
89 Push a string into an SMB buffer, with odd byte alignment
90 if it's a UCS2 string.
91 ***********************************************************/
93 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
94 const char *str, size_t str_len,
95 size_t *pconverted_size)
97 return internal_bytes_push_str(buf, ucs2, str, str_len,
98 true, pconverted_size);
101 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
102 const uint8_t *bytes, size_t num_bytes)
104 size_t buflen;
106 if (buf == NULL) {
107 return NULL;
109 buflen = talloc_get_size(buf);
111 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
112 buflen + 1 + num_bytes);
113 if (buf == NULL) {
114 return NULL;
116 buf[buflen] = prefix;
117 memcpy(&buf[buflen+1], bytes, num_bytes);
118 return buf;
121 /***********************************************************
122 Same as smb_bytes_push_str(), but without the odd byte
123 align for ucs2 (we're pushing into a param or data block).
124 static for now, although this will probably change when
125 other modules use async trans calls.
126 ***********************************************************/
128 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
129 const char *str, size_t str_len,
130 size_t *pconverted_size)
132 return internal_bytes_push_str(buf, ucs2, str, str_len,
133 false, pconverted_size);
136 struct cli_setpathinfo_state {
137 uint16_t setup;
138 uint8_t *param;
141 static void cli_setpathinfo_done(struct tevent_req *subreq);
143 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
144 struct tevent_context *ev,
145 struct cli_state *cli,
146 uint16_t level,
147 const char *path,
148 uint8_t *data,
149 size_t data_len)
151 struct tevent_req *req, *subreq;
152 struct cli_setpathinfo_state *state;
154 req = tevent_req_create(mem_ctx, &state,
155 struct cli_setpathinfo_state);
156 if (req == NULL) {
157 return NULL;
160 /* Setup setup word. */
161 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
163 /* Setup param array. */
164 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
165 if (tevent_req_nomem(state->param, req)) {
166 return tevent_req_post(req, ev);
168 SSVAL(state->param, 0, level);
170 state->param = trans2_bytes_push_str(
171 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
172 if (tevent_req_nomem(state->param, req)) {
173 return tevent_req_post(req, ev);
176 subreq = cli_trans_send(
177 state, /* mem ctx. */
178 ev, /* event ctx. */
179 cli, /* cli_state. */
180 SMBtrans2, /* cmd. */
181 NULL, /* pipe name. */
182 -1, /* fid. */
183 0, /* function. */
184 0, /* flags. */
185 &state->setup, /* setup. */
186 1, /* num setup uint16_t words. */
187 0, /* max returned setup. */
188 state->param, /* param. */
189 talloc_get_size(state->param), /* num param. */
190 2, /* max returned param. */
191 data, /* data. */
192 data_len, /* num data. */
193 0); /* max returned data. */
195 if (tevent_req_nomem(subreq, req)) {
196 return tevent_req_post(req, ev);
198 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
199 return req;
202 static void cli_setpathinfo_done(struct tevent_req *subreq)
204 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
205 NULL, 0, NULL, NULL, 0, NULL);
206 tevent_req_simple_finish_ntstatus(subreq, status);
209 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
211 return tevent_req_simple_recv_ntstatus(req);
214 NTSTATUS cli_setpathinfo(struct cli_state *cli,
215 uint16_t level,
216 const char *path,
217 uint8_t *data,
218 size_t data_len)
220 TALLOC_CTX *frame = talloc_stackframe();
221 struct tevent_context *ev;
222 struct tevent_req *req;
223 NTSTATUS status = NT_STATUS_NO_MEMORY;
225 if (cli_has_async_calls(cli)) {
227 * Can't use sync call while an async call is in flight
229 status = NT_STATUS_INVALID_PARAMETER;
230 goto fail;
232 ev = tevent_context_init(frame);
233 if (ev == NULL) {
234 goto fail;
236 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
237 if (req == NULL) {
238 goto fail;
240 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
241 goto fail;
243 status = cli_setpathinfo_recv(req);
244 fail:
245 TALLOC_FREE(frame);
246 return status;
249 /****************************************************************************
250 Hard/Symlink a file (UNIX extensions).
251 Creates new name (sym)linked to oldname.
252 ****************************************************************************/
254 struct cli_posix_link_internal_state {
255 uint8_t *data;
258 static void cli_posix_link_internal_done(struct tevent_req *subreq);
260 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
261 struct event_context *ev,
262 struct cli_state *cli,
263 uint16_t level,
264 const char *oldname,
265 const char *newname)
267 struct tevent_req *req = NULL, *subreq = NULL;
268 struct cli_posix_link_internal_state *state = NULL;
270 req = tevent_req_create(mem_ctx, &state,
271 struct cli_posix_link_internal_state);
272 if (req == NULL) {
273 return NULL;
276 /* Setup data array. */
277 state->data = talloc_array(state, uint8_t, 0);
278 if (tevent_req_nomem(state->data, req)) {
279 return tevent_req_post(req, ev);
281 state->data = trans2_bytes_push_str(
282 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
284 subreq = cli_setpathinfo_send(
285 state, ev, cli, level, newname,
286 state->data, talloc_get_size(state->data));
287 if (tevent_req_nomem(subreq, req)) {
288 return tevent_req_post(req, ev);
290 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
291 return req;
294 static void cli_posix_link_internal_done(struct tevent_req *subreq)
296 NTSTATUS status = cli_setpathinfo_recv(subreq);
297 tevent_req_simple_finish_ntstatus(subreq, status);
300 /****************************************************************************
301 Symlink a file (UNIX extensions).
302 ****************************************************************************/
304 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
305 struct event_context *ev,
306 struct cli_state *cli,
307 const char *oldname,
308 const char *newname)
310 return cli_posix_link_internal_send(
311 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
314 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
316 return tevent_req_simple_recv_ntstatus(req);
319 NTSTATUS cli_posix_symlink(struct cli_state *cli,
320 const char *oldname,
321 const char *newname)
323 TALLOC_CTX *frame = talloc_stackframe();
324 struct event_context *ev = NULL;
325 struct tevent_req *req = NULL;
326 NTSTATUS status = NT_STATUS_OK;
328 if (cli_has_async_calls(cli)) {
330 * Can't use sync call while an async call is in flight
332 status = NT_STATUS_INVALID_PARAMETER;
333 goto fail;
336 ev = event_context_init(frame);
337 if (ev == NULL) {
338 status = NT_STATUS_NO_MEMORY;
339 goto fail;
342 req = cli_posix_symlink_send(frame,
344 cli,
345 oldname,
346 newname);
347 if (req == NULL) {
348 status = NT_STATUS_NO_MEMORY;
349 goto fail;
352 if (!tevent_req_poll(req, ev)) {
353 status = map_nt_error_from_unix(errno);
354 goto fail;
357 status = cli_posix_symlink_recv(req);
359 fail:
360 TALLOC_FREE(frame);
361 if (!NT_STATUS_IS_OK(status)) {
362 cli_set_error(cli, status);
364 return status;
367 /****************************************************************************
368 Read a POSIX symlink.
369 ****************************************************************************/
371 struct readlink_state {
372 uint8_t *data;
373 uint32_t num_data;
376 static void cli_posix_readlink_done(struct tevent_req *subreq);
378 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
379 struct event_context *ev,
380 struct cli_state *cli,
381 const char *fname,
382 size_t len)
384 struct tevent_req *req = NULL, *subreq = NULL;
385 struct readlink_state *state = NULL;
386 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
388 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
389 if (req == NULL) {
390 return NULL;
394 * Len is in bytes, we need it in UCS2 units.
396 if ((2*len < len) || (maxbytelen < len)) {
397 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
398 return tevent_req_post(req, ev);
401 subreq = cli_qpathinfo_send(state, ev, cli, fname,
402 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
403 if (tevent_req_nomem(subreq, req)) {
404 return tevent_req_post(req, ev);
406 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
407 return req;
410 static void cli_posix_readlink_done(struct tevent_req *subreq)
412 struct tevent_req *req = tevent_req_callback_data(
413 subreq, struct tevent_req);
414 struct readlink_state *state = tevent_req_data(
415 req, struct readlink_state);
416 NTSTATUS status;
418 status = cli_qpathinfo_recv(subreq, state, &state->data,
419 &state->num_data);
420 TALLOC_FREE(subreq);
421 if (tevent_req_nterror(req, status)) {
422 return;
425 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
427 if (state->data[state->num_data-1] != '\0') {
428 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
429 return;
431 tevent_req_done(req);
434 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
435 char *retpath, size_t len)
437 NTSTATUS status;
438 char *converted = NULL;
439 size_t converted_size = 0;
440 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
442 if (tevent_req_is_nterror(req, &status)) {
443 return status;
445 /* The returned data is a pushed string, not raw data. */
446 if (!convert_string_talloc(state,
447 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
448 CH_UNIX,
449 state->data,
450 state->num_data,
451 &converted,
452 &converted_size,
453 true)) {
454 return NT_STATUS_NO_MEMORY;
457 len = MIN(len,converted_size);
458 if (len == 0) {
459 return NT_STATUS_DATA_ERROR;
461 memcpy(retpath, converted, len);
462 return NT_STATUS_OK;
465 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
466 char *linkpath, size_t len)
468 TALLOC_CTX *frame = talloc_stackframe();
469 struct event_context *ev = NULL;
470 struct tevent_req *req = NULL;
471 NTSTATUS status = NT_STATUS_OK;
473 if (cli_has_async_calls(cli)) {
475 * Can't use sync call while an async call is in flight
477 status = NT_STATUS_INVALID_PARAMETER;
478 goto fail;
481 ev = event_context_init(frame);
482 if (ev == NULL) {
483 status = NT_STATUS_NO_MEMORY;
484 goto fail;
487 req = cli_posix_readlink_send(frame,
489 cli,
490 fname,
491 len);
492 if (req == NULL) {
493 status = NT_STATUS_NO_MEMORY;
494 goto fail;
497 if (!tevent_req_poll(req, ev)) {
498 status = map_nt_error_from_unix(errno);
499 goto fail;
502 status = cli_posix_readlink_recv(req, cli, linkpath, len);
504 fail:
505 TALLOC_FREE(frame);
506 if (!NT_STATUS_IS_OK(status)) {
507 cli_set_error(cli, status);
509 return status;
512 /****************************************************************************
513 Hard link a file (UNIX extensions).
514 ****************************************************************************/
516 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
517 struct event_context *ev,
518 struct cli_state *cli,
519 const char *oldname,
520 const char *newname)
522 return cli_posix_link_internal_send(
523 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
526 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
528 return tevent_req_simple_recv_ntstatus(req);
531 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
532 const char *oldname,
533 const char *newname)
535 TALLOC_CTX *frame = talloc_stackframe();
536 struct event_context *ev = NULL;
537 struct tevent_req *req = NULL;
538 NTSTATUS status = NT_STATUS_OK;
540 if (cli_has_async_calls(cli)) {
542 * Can't use sync call while an async call is in flight
544 status = NT_STATUS_INVALID_PARAMETER;
545 goto fail;
548 ev = event_context_init(frame);
549 if (ev == NULL) {
550 status = NT_STATUS_NO_MEMORY;
551 goto fail;
554 req = cli_posix_hardlink_send(frame,
556 cli,
557 oldname,
558 newname);
559 if (req == NULL) {
560 status = NT_STATUS_NO_MEMORY;
561 goto fail;
564 if (!tevent_req_poll(req, ev)) {
565 status = map_nt_error_from_unix(errno);
566 goto fail;
569 status = cli_posix_hardlink_recv(req);
571 fail:
572 TALLOC_FREE(frame);
573 if (!NT_STATUS_IS_OK(status)) {
574 cli_set_error(cli, status);
576 return status;
579 /****************************************************************************
580 Map standard UNIX permissions onto wire representations.
581 ****************************************************************************/
583 uint32_t unix_perms_to_wire(mode_t perms)
585 unsigned int ret = 0;
587 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
588 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
589 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
590 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
591 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
592 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
593 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
594 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
595 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
596 #ifdef S_ISVTX
597 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
598 #endif
599 #ifdef S_ISGID
600 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
601 #endif
602 #ifdef S_ISUID
603 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
604 #endif
605 return ret;
608 /****************************************************************************
609 Map wire permissions to standard UNIX.
610 ****************************************************************************/
612 mode_t wire_perms_to_unix(uint32_t perms)
614 mode_t ret = (mode_t)0;
616 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
617 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
618 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
619 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
620 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
621 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
622 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
623 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
624 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
625 #ifdef S_ISVTX
626 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
627 #endif
628 #ifdef S_ISGID
629 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
630 #endif
631 #ifdef S_ISUID
632 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
633 #endif
634 return ret;
637 /****************************************************************************
638 Return the file type from the wire filetype for UNIX extensions.
639 ****************************************************************************/
641 static mode_t unix_filetype_from_wire(uint32_t wire_type)
643 switch (wire_type) {
644 case UNIX_TYPE_FILE:
645 return S_IFREG;
646 case UNIX_TYPE_DIR:
647 return S_IFDIR;
648 #ifdef S_IFLNK
649 case UNIX_TYPE_SYMLINK:
650 return S_IFLNK;
651 #endif
652 #ifdef S_IFCHR
653 case UNIX_TYPE_CHARDEV:
654 return S_IFCHR;
655 #endif
656 #ifdef S_IFBLK
657 case UNIX_TYPE_BLKDEV:
658 return S_IFBLK;
659 #endif
660 #ifdef S_IFIFO
661 case UNIX_TYPE_FIFO:
662 return S_IFIFO;
663 #endif
664 #ifdef S_IFSOCK
665 case UNIX_TYPE_SOCKET:
666 return S_IFSOCK;
667 #endif
668 default:
669 return (mode_t)0;
673 /****************************************************************************
674 Do a POSIX getfacl (UNIX extensions).
675 ****************************************************************************/
677 struct getfacl_state {
678 uint32_t num_data;
679 uint8_t *data;
682 static void cli_posix_getfacl_done(struct tevent_req *subreq);
684 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
685 struct event_context *ev,
686 struct cli_state *cli,
687 const char *fname)
689 struct tevent_req *req = NULL, *subreq = NULL;
690 struct getfacl_state *state = NULL;
692 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
693 if (req == NULL) {
694 return NULL;
696 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
697 0, cli->max_xmit);
698 if (tevent_req_nomem(subreq, req)) {
699 return tevent_req_post(req, ev);
701 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
702 return req;
705 static void cli_posix_getfacl_done(struct tevent_req *subreq)
707 struct tevent_req *req = tevent_req_callback_data(
708 subreq, struct tevent_req);
709 struct getfacl_state *state = tevent_req_data(
710 req, struct getfacl_state);
711 NTSTATUS status;
713 status = cli_qpathinfo_recv(subreq, state, &state->data,
714 &state->num_data);
715 TALLOC_FREE(subreq);
716 if (tevent_req_nterror(req, status)) {
717 return;
719 tevent_req_done(req);
722 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
723 TALLOC_CTX *mem_ctx,
724 size_t *prb_size,
725 char **retbuf)
727 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
728 NTSTATUS status;
730 if (tevent_req_is_nterror(req, &status)) {
731 return status;
733 *prb_size = (size_t)state->num_data;
734 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
735 return NT_STATUS_OK;
738 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
739 const char *fname,
740 TALLOC_CTX *mem_ctx,
741 size_t *prb_size,
742 char **retbuf)
744 TALLOC_CTX *frame = talloc_stackframe();
745 struct event_context *ev = NULL;
746 struct tevent_req *req = NULL;
747 NTSTATUS status = NT_STATUS_OK;
749 if (cli_has_async_calls(cli)) {
751 * Can't use sync call while an async call is in flight
753 status = NT_STATUS_INVALID_PARAMETER;
754 goto fail;
757 ev = event_context_init(frame);
758 if (ev == NULL) {
759 status = NT_STATUS_NO_MEMORY;
760 goto fail;
763 req = cli_posix_getfacl_send(frame,
765 cli,
766 fname);
767 if (req == NULL) {
768 status = NT_STATUS_NO_MEMORY;
769 goto fail;
772 if (!tevent_req_poll(req, ev)) {
773 status = map_nt_error_from_unix(errno);
774 goto fail;
777 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
779 fail:
780 TALLOC_FREE(frame);
781 if (!NT_STATUS_IS_OK(status)) {
782 cli_set_error(cli, status);
784 return status;
787 /****************************************************************************
788 Stat a file (UNIX extensions).
789 ****************************************************************************/
791 struct stat_state {
792 uint32_t num_data;
793 uint8_t *data;
796 static void cli_posix_stat_done(struct tevent_req *subreq);
798 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
799 struct event_context *ev,
800 struct cli_state *cli,
801 const char *fname)
803 struct tevent_req *req = NULL, *subreq = NULL;
804 struct stat_state *state = NULL;
806 req = tevent_req_create(mem_ctx, &state, struct stat_state);
807 if (req == NULL) {
808 return NULL;
810 subreq = cli_qpathinfo_send(state, ev, cli, fname,
811 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
812 if (tevent_req_nomem(subreq, req)) {
813 return tevent_req_post(req, ev);
815 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
816 return req;
819 static void cli_posix_stat_done(struct tevent_req *subreq)
821 struct tevent_req *req = tevent_req_callback_data(
822 subreq, struct tevent_req);
823 struct stat_state *state = tevent_req_data(req, struct stat_state);
824 NTSTATUS status;
826 status = cli_qpathinfo_recv(subreq, state, &state->data,
827 &state->num_data);
828 TALLOC_FREE(subreq);
829 if (tevent_req_nterror(req, status)) {
830 return;
832 tevent_req_done(req);
835 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
836 SMB_STRUCT_STAT *sbuf)
838 struct stat_state *state = tevent_req_data(req, struct stat_state);
839 NTSTATUS status;
841 if (tevent_req_is_nterror(req, &status)) {
842 return status;
845 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
846 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
847 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
848 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
849 #else
850 /* assume 512 byte blocks */
851 sbuf->st_ex_blocks /= 512;
852 #endif
853 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
854 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
855 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
857 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
858 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
859 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
860 #if defined(HAVE_MAKEDEV)
862 uint32_t dev_major = IVAL(state->data,60);
863 uint32_t dev_minor = IVAL(state->data,68);
864 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
866 #endif
867 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
868 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
869 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
871 return NT_STATUS_OK;
874 NTSTATUS cli_posix_stat(struct cli_state *cli,
875 const char *fname,
876 SMB_STRUCT_STAT *sbuf)
878 TALLOC_CTX *frame = talloc_stackframe();
879 struct event_context *ev = NULL;
880 struct tevent_req *req = NULL;
881 NTSTATUS status = NT_STATUS_OK;
883 if (cli_has_async_calls(cli)) {
885 * Can't use sync call while an async call is in flight
887 status = NT_STATUS_INVALID_PARAMETER;
888 goto fail;
891 ev = event_context_init(frame);
892 if (ev == NULL) {
893 status = NT_STATUS_NO_MEMORY;
894 goto fail;
897 req = cli_posix_stat_send(frame,
899 cli,
900 fname);
901 if (req == NULL) {
902 status = NT_STATUS_NO_MEMORY;
903 goto fail;
906 if (!tevent_req_poll(req, ev)) {
907 status = map_nt_error_from_unix(errno);
908 goto fail;
911 status = cli_posix_stat_recv(req, sbuf);
913 fail:
914 TALLOC_FREE(frame);
915 if (!NT_STATUS_IS_OK(status)) {
916 cli_set_error(cli, status);
918 return status;
921 /****************************************************************************
922 Chmod or chown a file internal (UNIX extensions).
923 ****************************************************************************/
925 struct cli_posix_chown_chmod_internal_state {
926 uint8_t data[100];
929 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
931 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
932 struct event_context *ev,
933 struct cli_state *cli,
934 const char *fname,
935 uint32_t mode,
936 uint32_t uid,
937 uint32_t gid)
939 struct tevent_req *req = NULL, *subreq = NULL;
940 struct cli_posix_chown_chmod_internal_state *state = NULL;
942 req = tevent_req_create(mem_ctx, &state,
943 struct cli_posix_chown_chmod_internal_state);
944 if (req == NULL) {
945 return NULL;
948 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
949 memset(&state->data[40], '\0', 60);
950 SIVAL(state->data,40,uid);
951 SIVAL(state->data,48,gid);
952 SIVAL(state->data,84,mode);
954 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
955 fname, state->data, sizeof(state->data));
956 if (tevent_req_nomem(subreq, req)) {
957 return tevent_req_post(req, ev);
959 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
960 req);
961 return req;
964 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
966 NTSTATUS status = cli_setpathinfo_recv(subreq);
967 tevent_req_simple_finish_ntstatus(subreq, status);
970 /****************************************************************************
971 chmod a file (UNIX extensions).
972 ****************************************************************************/
974 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
975 struct event_context *ev,
976 struct cli_state *cli,
977 const char *fname,
978 mode_t mode)
980 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
981 fname,
982 unix_perms_to_wire(mode),
983 SMB_UID_NO_CHANGE,
984 SMB_GID_NO_CHANGE);
987 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
989 return tevent_req_simple_recv_ntstatus(req);
992 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
994 TALLOC_CTX *frame = talloc_stackframe();
995 struct event_context *ev = NULL;
996 struct tevent_req *req = NULL;
997 NTSTATUS status = NT_STATUS_OK;
999 if (cli_has_async_calls(cli)) {
1001 * Can't use sync call while an async call is in flight
1003 status = NT_STATUS_INVALID_PARAMETER;
1004 goto fail;
1007 ev = event_context_init(frame);
1008 if (ev == NULL) {
1009 status = NT_STATUS_NO_MEMORY;
1010 goto fail;
1013 req = cli_posix_chmod_send(frame,
1015 cli,
1016 fname,
1017 mode);
1018 if (req == NULL) {
1019 status = NT_STATUS_NO_MEMORY;
1020 goto fail;
1023 if (!tevent_req_poll(req, ev)) {
1024 status = map_nt_error_from_unix(errno);
1025 goto fail;
1028 status = cli_posix_chmod_recv(req);
1030 fail:
1031 TALLOC_FREE(frame);
1032 if (!NT_STATUS_IS_OK(status)) {
1033 cli_set_error(cli, status);
1035 return status;
1038 /****************************************************************************
1039 chown a file (UNIX extensions).
1040 ****************************************************************************/
1042 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1043 struct event_context *ev,
1044 struct cli_state *cli,
1045 const char *fname,
1046 uid_t uid,
1047 gid_t gid)
1049 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1050 fname,
1051 SMB_MODE_NO_CHANGE,
1052 (uint32_t)uid,
1053 (uint32_t)gid);
1056 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1058 return tevent_req_simple_recv_ntstatus(req);
1061 NTSTATUS cli_posix_chown(struct cli_state *cli,
1062 const char *fname,
1063 uid_t uid,
1064 gid_t gid)
1066 TALLOC_CTX *frame = talloc_stackframe();
1067 struct event_context *ev = NULL;
1068 struct tevent_req *req = NULL;
1069 NTSTATUS status = NT_STATUS_OK;
1071 if (cli_has_async_calls(cli)) {
1073 * Can't use sync call while an async call is in flight
1075 status = NT_STATUS_INVALID_PARAMETER;
1076 goto fail;
1079 ev = event_context_init(frame);
1080 if (ev == NULL) {
1081 status = NT_STATUS_NO_MEMORY;
1082 goto fail;
1085 req = cli_posix_chown_send(frame,
1087 cli,
1088 fname,
1089 uid,
1090 gid);
1091 if (req == NULL) {
1092 status = NT_STATUS_NO_MEMORY;
1093 goto fail;
1096 if (!tevent_req_poll(req, ev)) {
1097 status = map_nt_error_from_unix(errno);
1098 goto fail;
1101 status = cli_posix_chown_recv(req);
1103 fail:
1104 TALLOC_FREE(frame);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 cli_set_error(cli, status);
1108 return status;
1111 /****************************************************************************
1112 Rename a file.
1113 ****************************************************************************/
1115 static void cli_rename_done(struct tevent_req *subreq);
1117 struct cli_rename_state {
1118 uint16_t vwv[1];
1121 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1122 struct event_context *ev,
1123 struct cli_state *cli,
1124 const char *fname_src,
1125 const char *fname_dst)
1127 struct tevent_req *req = NULL, *subreq = NULL;
1128 struct cli_rename_state *state = NULL;
1129 uint8_t additional_flags = 0;
1130 uint8_t *bytes = NULL;
1132 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1133 if (req == NULL) {
1134 return NULL;
1137 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1139 bytes = talloc_array(state, uint8_t, 1);
1140 if (tevent_req_nomem(bytes, req)) {
1141 return tevent_req_post(req, ev);
1143 bytes[0] = 4;
1144 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1145 strlen(fname_src)+1, NULL);
1146 if (tevent_req_nomem(bytes, req)) {
1147 return tevent_req_post(req, ev);
1150 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1151 talloc_get_size(bytes)+1);
1152 if (tevent_req_nomem(bytes, req)) {
1153 return tevent_req_post(req, ev);
1156 bytes[talloc_get_size(bytes)-1] = 4;
1157 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1158 strlen(fname_dst)+1, NULL);
1159 if (tevent_req_nomem(bytes, req)) {
1160 return tevent_req_post(req, ev);
1163 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1164 1, state->vwv, talloc_get_size(bytes), bytes);
1165 if (tevent_req_nomem(subreq, req)) {
1166 return tevent_req_post(req, ev);
1168 tevent_req_set_callback(subreq, cli_rename_done, req);
1169 return req;
1172 static void cli_rename_done(struct tevent_req *subreq)
1174 struct tevent_req *req = tevent_req_callback_data(
1175 subreq, struct tevent_req);
1176 NTSTATUS status;
1178 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1179 TALLOC_FREE(subreq);
1180 if (tevent_req_nterror(req, status)) {
1181 return;
1183 tevent_req_done(req);
1186 NTSTATUS cli_rename_recv(struct tevent_req *req)
1188 return tevent_req_simple_recv_ntstatus(req);
1191 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1193 TALLOC_CTX *frame = talloc_stackframe();
1194 struct event_context *ev;
1195 struct tevent_req *req;
1196 NTSTATUS status = NT_STATUS_OK;
1198 if (cli_has_async_calls(cli)) {
1200 * Can't use sync call while an async call is in flight
1202 status = NT_STATUS_INVALID_PARAMETER;
1203 goto fail;
1206 ev = event_context_init(frame);
1207 if (ev == NULL) {
1208 status = NT_STATUS_NO_MEMORY;
1209 goto fail;
1212 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1213 if (req == NULL) {
1214 status = NT_STATUS_NO_MEMORY;
1215 goto fail;
1218 if (!tevent_req_poll(req, ev)) {
1219 status = map_nt_error_from_unix(errno);
1220 goto fail;
1223 status = cli_rename_recv(req);
1225 fail:
1226 TALLOC_FREE(frame);
1227 if (!NT_STATUS_IS_OK(status)) {
1228 cli_set_error(cli, status);
1230 return status;
1233 /****************************************************************************
1234 NT Rename a file.
1235 ****************************************************************************/
1237 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1239 struct cli_ntrename_internal_state {
1240 uint16_t vwv[4];
1243 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1244 struct event_context *ev,
1245 struct cli_state *cli,
1246 const char *fname_src,
1247 const char *fname_dst,
1248 uint16_t rename_flag)
1250 struct tevent_req *req = NULL, *subreq = NULL;
1251 struct cli_ntrename_internal_state *state = NULL;
1252 uint8_t additional_flags = 0;
1253 uint8_t *bytes = NULL;
1255 req = tevent_req_create(mem_ctx, &state,
1256 struct cli_ntrename_internal_state);
1257 if (req == NULL) {
1258 return NULL;
1261 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1262 SSVAL(state->vwv+1, 0, rename_flag);
1264 bytes = talloc_array(state, uint8_t, 1);
1265 if (tevent_req_nomem(bytes, req)) {
1266 return tevent_req_post(req, ev);
1268 bytes[0] = 4;
1269 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1270 strlen(fname_src)+1, NULL);
1271 if (tevent_req_nomem(bytes, req)) {
1272 return tevent_req_post(req, ev);
1275 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1276 talloc_get_size(bytes)+1);
1277 if (tevent_req_nomem(bytes, req)) {
1278 return tevent_req_post(req, ev);
1281 bytes[talloc_get_size(bytes)-1] = 4;
1282 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1283 strlen(fname_dst)+1, NULL);
1284 if (tevent_req_nomem(bytes, req)) {
1285 return tevent_req_post(req, ev);
1288 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1289 4, state->vwv, talloc_get_size(bytes), bytes);
1290 if (tevent_req_nomem(subreq, req)) {
1291 return tevent_req_post(req, ev);
1293 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1294 return req;
1297 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1299 struct tevent_req *req = tevent_req_callback_data(
1300 subreq, struct tevent_req);
1301 NTSTATUS status;
1303 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1304 TALLOC_FREE(subreq);
1305 if (tevent_req_nterror(req, status)) {
1306 return;
1308 tevent_req_done(req);
1311 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1313 return tevent_req_simple_recv_ntstatus(req);
1316 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1317 struct event_context *ev,
1318 struct cli_state *cli,
1319 const char *fname_src,
1320 const char *fname_dst)
1322 return cli_ntrename_internal_send(mem_ctx,
1324 cli,
1325 fname_src,
1326 fname_dst,
1327 RENAME_FLAG_RENAME);
1330 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1332 return cli_ntrename_internal_recv(req);
1335 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1337 TALLOC_CTX *frame = talloc_stackframe();
1338 struct event_context *ev;
1339 struct tevent_req *req;
1340 NTSTATUS status = NT_STATUS_OK;
1342 if (cli_has_async_calls(cli)) {
1344 * Can't use sync call while an async call is in flight
1346 status = NT_STATUS_INVALID_PARAMETER;
1347 goto fail;
1350 ev = event_context_init(frame);
1351 if (ev == NULL) {
1352 status = NT_STATUS_NO_MEMORY;
1353 goto fail;
1356 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1357 if (req == NULL) {
1358 status = NT_STATUS_NO_MEMORY;
1359 goto fail;
1362 if (!tevent_req_poll(req, ev)) {
1363 status = map_nt_error_from_unix(errno);
1364 goto fail;
1367 status = cli_ntrename_recv(req);
1369 fail:
1370 TALLOC_FREE(frame);
1371 if (!NT_STATUS_IS_OK(status)) {
1372 cli_set_error(cli, status);
1374 return status;
1377 /****************************************************************************
1378 NT hardlink a file.
1379 ****************************************************************************/
1381 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1382 struct event_context *ev,
1383 struct cli_state *cli,
1384 const char *fname_src,
1385 const char *fname_dst)
1387 return cli_ntrename_internal_send(mem_ctx,
1389 cli,
1390 fname_src,
1391 fname_dst,
1392 RENAME_FLAG_HARD_LINK);
1395 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1397 return cli_ntrename_internal_recv(req);
1400 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1402 TALLOC_CTX *frame = talloc_stackframe();
1403 struct event_context *ev;
1404 struct tevent_req *req;
1405 NTSTATUS status = NT_STATUS_OK;
1407 if (cli_has_async_calls(cli)) {
1409 * Can't use sync call while an async call is in flight
1411 status = NT_STATUS_INVALID_PARAMETER;
1412 goto fail;
1415 ev = event_context_init(frame);
1416 if (ev == NULL) {
1417 status = NT_STATUS_NO_MEMORY;
1418 goto fail;
1421 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1422 if (req == NULL) {
1423 status = NT_STATUS_NO_MEMORY;
1424 goto fail;
1427 if (!tevent_req_poll(req, ev)) {
1428 status = map_nt_error_from_unix(errno);
1429 goto fail;
1432 status = cli_nt_hardlink_recv(req);
1434 fail:
1435 TALLOC_FREE(frame);
1436 if (!NT_STATUS_IS_OK(status)) {
1437 cli_set_error(cli, status);
1439 return status;
1442 /****************************************************************************
1443 Delete a file.
1444 ****************************************************************************/
1446 static void cli_unlink_done(struct tevent_req *subreq);
1448 struct cli_unlink_state {
1449 uint16_t vwv[1];
1452 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1453 struct event_context *ev,
1454 struct cli_state *cli,
1455 const char *fname,
1456 uint16_t mayhave_attrs)
1458 struct tevent_req *req = NULL, *subreq = NULL;
1459 struct cli_unlink_state *state = NULL;
1460 uint8_t additional_flags = 0;
1461 uint8_t *bytes = NULL;
1463 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1464 if (req == NULL) {
1465 return NULL;
1468 SSVAL(state->vwv+0, 0, mayhave_attrs);
1470 bytes = talloc_array(state, uint8_t, 1);
1471 if (tevent_req_nomem(bytes, req)) {
1472 return tevent_req_post(req, ev);
1474 bytes[0] = 4;
1475 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1476 strlen(fname)+1, NULL);
1478 if (tevent_req_nomem(bytes, req)) {
1479 return tevent_req_post(req, ev);
1482 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1483 1, state->vwv, talloc_get_size(bytes), bytes);
1484 if (tevent_req_nomem(subreq, req)) {
1485 return tevent_req_post(req, ev);
1487 tevent_req_set_callback(subreq, cli_unlink_done, req);
1488 return req;
1491 static void cli_unlink_done(struct tevent_req *subreq)
1493 struct tevent_req *req = tevent_req_callback_data(
1494 subreq, struct tevent_req);
1495 NTSTATUS status;
1497 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1498 TALLOC_FREE(subreq);
1499 if (tevent_req_nterror(req, status)) {
1500 return;
1502 tevent_req_done(req);
1505 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1507 return tevent_req_simple_recv_ntstatus(req);
1510 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1512 TALLOC_CTX *frame = talloc_stackframe();
1513 struct event_context *ev;
1514 struct tevent_req *req;
1515 NTSTATUS status = NT_STATUS_OK;
1517 if (cli_has_async_calls(cli)) {
1519 * Can't use sync call while an async call is in flight
1521 status = NT_STATUS_INVALID_PARAMETER;
1522 goto fail;
1525 ev = event_context_init(frame);
1526 if (ev == NULL) {
1527 status = NT_STATUS_NO_MEMORY;
1528 goto fail;
1531 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1532 if (req == NULL) {
1533 status = NT_STATUS_NO_MEMORY;
1534 goto fail;
1537 if (!tevent_req_poll(req, ev)) {
1538 status = map_nt_error_from_unix(errno);
1539 goto fail;
1542 status = cli_unlink_recv(req);
1544 fail:
1545 TALLOC_FREE(frame);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 cli_set_error(cli, status);
1549 return status;
1552 /****************************************************************************
1553 Create a directory.
1554 ****************************************************************************/
1556 static void cli_mkdir_done(struct tevent_req *subreq);
1558 struct cli_mkdir_state {
1559 int dummy;
1562 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1563 struct event_context *ev,
1564 struct cli_state *cli,
1565 const char *dname)
1567 struct tevent_req *req = NULL, *subreq = NULL;
1568 struct cli_mkdir_state *state = NULL;
1569 uint8_t additional_flags = 0;
1570 uint8_t *bytes = NULL;
1572 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1573 if (req == NULL) {
1574 return NULL;
1577 bytes = talloc_array(state, uint8_t, 1);
1578 if (tevent_req_nomem(bytes, req)) {
1579 return tevent_req_post(req, ev);
1581 bytes[0] = 4;
1582 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1583 strlen(dname)+1, NULL);
1585 if (tevent_req_nomem(bytes, req)) {
1586 return tevent_req_post(req, ev);
1589 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1590 0, NULL, talloc_get_size(bytes), bytes);
1591 if (tevent_req_nomem(subreq, req)) {
1592 return tevent_req_post(req, ev);
1594 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1595 return req;
1598 static void cli_mkdir_done(struct tevent_req *subreq)
1600 struct tevent_req *req = tevent_req_callback_data(
1601 subreq, struct tevent_req);
1602 NTSTATUS status;
1604 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1605 TALLOC_FREE(subreq);
1606 if (tevent_req_nterror(req, status)) {
1607 return;
1609 tevent_req_done(req);
1612 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1614 return tevent_req_simple_recv_ntstatus(req);
1617 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1619 TALLOC_CTX *frame = talloc_stackframe();
1620 struct event_context *ev;
1621 struct tevent_req *req;
1622 NTSTATUS status = NT_STATUS_OK;
1624 if (cli_has_async_calls(cli)) {
1626 * Can't use sync call while an async call is in flight
1628 status = NT_STATUS_INVALID_PARAMETER;
1629 goto fail;
1632 ev = event_context_init(frame);
1633 if (ev == NULL) {
1634 status = NT_STATUS_NO_MEMORY;
1635 goto fail;
1638 req = cli_mkdir_send(frame, ev, cli, dname);
1639 if (req == NULL) {
1640 status = NT_STATUS_NO_MEMORY;
1641 goto fail;
1644 if (!tevent_req_poll(req, ev)) {
1645 status = map_nt_error_from_unix(errno);
1646 goto fail;
1649 status = cli_mkdir_recv(req);
1651 fail:
1652 TALLOC_FREE(frame);
1653 if (!NT_STATUS_IS_OK(status)) {
1654 cli_set_error(cli, status);
1656 return status;
1659 /****************************************************************************
1660 Remove a directory.
1661 ****************************************************************************/
1663 static void cli_rmdir_done(struct tevent_req *subreq);
1665 struct cli_rmdir_state {
1666 int dummy;
1669 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1670 struct event_context *ev,
1671 struct cli_state *cli,
1672 const char *dname)
1674 struct tevent_req *req = NULL, *subreq = NULL;
1675 struct cli_rmdir_state *state = NULL;
1676 uint8_t additional_flags = 0;
1677 uint8_t *bytes = NULL;
1679 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1680 if (req == NULL) {
1681 return NULL;
1684 bytes = talloc_array(state, uint8_t, 1);
1685 if (tevent_req_nomem(bytes, req)) {
1686 return tevent_req_post(req, ev);
1688 bytes[0] = 4;
1689 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1690 strlen(dname)+1, NULL);
1692 if (tevent_req_nomem(bytes, req)) {
1693 return tevent_req_post(req, ev);
1696 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1697 0, NULL, talloc_get_size(bytes), bytes);
1698 if (tevent_req_nomem(subreq, req)) {
1699 return tevent_req_post(req, ev);
1701 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1702 return req;
1705 static void cli_rmdir_done(struct tevent_req *subreq)
1707 struct tevent_req *req = tevent_req_callback_data(
1708 subreq, struct tevent_req);
1709 NTSTATUS status;
1711 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1712 TALLOC_FREE(subreq);
1713 if (tevent_req_nterror(req, status)) {
1714 return;
1716 tevent_req_done(req);
1719 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1721 return tevent_req_simple_recv_ntstatus(req);
1724 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1726 TALLOC_CTX *frame = talloc_stackframe();
1727 struct event_context *ev;
1728 struct tevent_req *req;
1729 NTSTATUS status = NT_STATUS_OK;
1731 if (cli_has_async_calls(cli)) {
1733 * Can't use sync call while an async call is in flight
1735 status = NT_STATUS_INVALID_PARAMETER;
1736 goto fail;
1739 ev = event_context_init(frame);
1740 if (ev == NULL) {
1741 status = NT_STATUS_NO_MEMORY;
1742 goto fail;
1745 req = cli_rmdir_send(frame, ev, cli, dname);
1746 if (req == NULL) {
1747 status = NT_STATUS_NO_MEMORY;
1748 goto fail;
1751 if (!tevent_req_poll(req, ev)) {
1752 status = map_nt_error_from_unix(errno);
1753 goto fail;
1756 status = cli_rmdir_recv(req);
1758 fail:
1759 TALLOC_FREE(frame);
1760 if (!NT_STATUS_IS_OK(status)) {
1761 cli_set_error(cli, status);
1763 return status;
1766 /****************************************************************************
1767 Set or clear the delete on close flag.
1768 ****************************************************************************/
1770 struct doc_state {
1771 uint16_t setup;
1772 uint8_t param[6];
1773 uint8_t data[1];
1776 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1778 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1779 NULL, 0, NULL, NULL, 0, NULL);
1780 tevent_req_simple_finish_ntstatus(subreq, status);
1783 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1784 struct event_context *ev,
1785 struct cli_state *cli,
1786 uint16_t fnum,
1787 bool flag)
1789 struct tevent_req *req = NULL, *subreq = NULL;
1790 struct doc_state *state = NULL;
1792 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1793 if (req == NULL) {
1794 return NULL;
1797 /* Setup setup word. */
1798 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1800 /* Setup param array. */
1801 SSVAL(state->param,0,fnum);
1802 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1804 /* Setup data array. */
1805 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1807 subreq = cli_trans_send(state, /* mem ctx. */
1808 ev, /* event ctx. */
1809 cli, /* cli_state. */
1810 SMBtrans2, /* cmd. */
1811 NULL, /* pipe name. */
1812 -1, /* fid. */
1813 0, /* function. */
1814 0, /* flags. */
1815 &state->setup, /* setup. */
1816 1, /* num setup uint16_t words. */
1817 0, /* max returned setup. */
1818 state->param, /* param. */
1819 6, /* num param. */
1820 2, /* max returned param. */
1821 state->data, /* data. */
1822 1, /* num data. */
1823 0); /* max returned data. */
1825 if (tevent_req_nomem(subreq, req)) {
1826 return tevent_req_post(req, ev);
1828 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1829 return req;
1832 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1834 return tevent_req_simple_recv_ntstatus(req);
1837 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1839 TALLOC_CTX *frame = talloc_stackframe();
1840 struct event_context *ev = NULL;
1841 struct tevent_req *req = NULL;
1842 NTSTATUS status = NT_STATUS_OK;
1844 if (cli_has_async_calls(cli)) {
1846 * Can't use sync call while an async call is in flight
1848 status = NT_STATUS_INVALID_PARAMETER;
1849 goto fail;
1852 ev = event_context_init(frame);
1853 if (ev == NULL) {
1854 status = NT_STATUS_NO_MEMORY;
1855 goto fail;
1858 req = cli_nt_delete_on_close_send(frame,
1860 cli,
1861 fnum,
1862 flag);
1863 if (req == NULL) {
1864 status = NT_STATUS_NO_MEMORY;
1865 goto fail;
1868 if (!tevent_req_poll(req, ev)) {
1869 status = map_nt_error_from_unix(errno);
1870 goto fail;
1873 status = cli_nt_delete_on_close_recv(req);
1875 fail:
1876 TALLOC_FREE(frame);
1877 if (!NT_STATUS_IS_OK(status)) {
1878 cli_set_error(cli, status);
1880 return status;
1883 struct cli_ntcreate_state {
1884 uint16_t vwv[24];
1885 uint16_t fnum;
1888 static void cli_ntcreate_done(struct tevent_req *subreq);
1890 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1891 struct event_context *ev,
1892 struct cli_state *cli,
1893 const char *fname,
1894 uint32_t CreatFlags,
1895 uint32_t DesiredAccess,
1896 uint32_t FileAttributes,
1897 uint32_t ShareAccess,
1898 uint32_t CreateDisposition,
1899 uint32_t CreateOptions,
1900 uint8_t SecurityFlags)
1902 struct tevent_req *req, *subreq;
1903 struct cli_ntcreate_state *state;
1904 uint16_t *vwv;
1905 uint8_t *bytes;
1906 size_t converted_len;
1908 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1909 if (req == NULL) {
1910 return NULL;
1913 vwv = state->vwv;
1915 SCVAL(vwv+0, 0, 0xFF);
1916 SCVAL(vwv+0, 1, 0);
1917 SSVAL(vwv+1, 0, 0);
1918 SCVAL(vwv+2, 0, 0);
1920 if (cli->use_oplocks) {
1921 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1923 SIVAL(vwv+3, 1, CreatFlags);
1924 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1925 SIVAL(vwv+7, 1, DesiredAccess);
1926 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1927 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1928 SIVAL(vwv+13, 1, FileAttributes);
1929 SIVAL(vwv+15, 1, ShareAccess);
1930 SIVAL(vwv+17, 1, CreateDisposition);
1931 SIVAL(vwv+19, 1, CreateOptions);
1932 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1933 SCVAL(vwv+23, 1, SecurityFlags);
1935 bytes = talloc_array(state, uint8_t, 0);
1936 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1937 fname, strlen(fname)+1,
1938 &converted_len);
1940 /* sigh. this copes with broken netapp filer behaviour */
1941 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1943 if (tevent_req_nomem(bytes, req)) {
1944 return tevent_req_post(req, ev);
1947 SSVAL(vwv+2, 1, converted_len);
1949 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1950 talloc_get_size(bytes), bytes);
1951 if (tevent_req_nomem(subreq, req)) {
1952 return tevent_req_post(req, ev);
1954 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1955 return req;
1958 static void cli_ntcreate_done(struct tevent_req *subreq)
1960 struct tevent_req *req = tevent_req_callback_data(
1961 subreq, struct tevent_req);
1962 struct cli_ntcreate_state *state = tevent_req_data(
1963 req, struct cli_ntcreate_state);
1964 uint8_t wct;
1965 uint16_t *vwv;
1966 uint32_t num_bytes;
1967 uint8_t *bytes;
1968 uint8_t *inbuf;
1969 NTSTATUS status;
1971 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1972 &num_bytes, &bytes);
1973 TALLOC_FREE(subreq);
1974 if (tevent_req_nterror(req, status)) {
1975 return;
1977 state->fnum = SVAL(vwv+2, 1);
1978 tevent_req_done(req);
1981 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1983 struct cli_ntcreate_state *state = tevent_req_data(
1984 req, struct cli_ntcreate_state);
1985 NTSTATUS status;
1987 if (tevent_req_is_nterror(req, &status)) {
1988 return status;
1990 *pfnum = state->fnum;
1991 return NT_STATUS_OK;
1994 NTSTATUS cli_ntcreate(struct cli_state *cli,
1995 const char *fname,
1996 uint32_t CreatFlags,
1997 uint32_t DesiredAccess,
1998 uint32_t FileAttributes,
1999 uint32_t ShareAccess,
2000 uint32_t CreateDisposition,
2001 uint32_t CreateOptions,
2002 uint8_t SecurityFlags,
2003 uint16_t *pfid)
2005 TALLOC_CTX *frame = talloc_stackframe();
2006 struct event_context *ev;
2007 struct tevent_req *req;
2008 NTSTATUS status = NT_STATUS_OK;
2010 if (cli_has_async_calls(cli)) {
2012 * Can't use sync call while an async call is in flight
2014 status = NT_STATUS_INVALID_PARAMETER;
2015 goto fail;
2018 ev = event_context_init(frame);
2019 if (ev == NULL) {
2020 status = NT_STATUS_NO_MEMORY;
2021 goto fail;
2024 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2025 DesiredAccess, FileAttributes, ShareAccess,
2026 CreateDisposition, CreateOptions,
2027 SecurityFlags);
2028 if (req == NULL) {
2029 status = NT_STATUS_NO_MEMORY;
2030 goto fail;
2033 if (!tevent_req_poll(req, ev)) {
2034 status = map_nt_error_from_unix(errno);
2035 goto fail;
2038 status = cli_ntcreate_recv(req, pfid);
2039 fail:
2040 TALLOC_FREE(frame);
2041 if (!NT_STATUS_IS_OK(status)) {
2042 cli_set_error(cli, status);
2044 return status;
2047 /****************************************************************************
2048 Open a file
2049 WARNING: if you open with O_WRONLY then getattrE won't work!
2050 ****************************************************************************/
2052 struct cli_open_state {
2053 uint16_t vwv[15];
2054 uint16_t fnum;
2055 struct iovec bytes;
2058 static void cli_open_done(struct tevent_req *subreq);
2060 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2061 struct event_context *ev,
2062 struct cli_state *cli, const char *fname,
2063 int flags, int share_mode,
2064 struct tevent_req **psmbreq)
2066 struct tevent_req *req, *subreq;
2067 struct cli_open_state *state;
2068 unsigned openfn;
2069 unsigned accessmode;
2070 uint8_t additional_flags;
2071 uint8_t *bytes;
2073 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2074 if (req == NULL) {
2075 return NULL;
2078 openfn = 0;
2079 if (flags & O_CREAT) {
2080 openfn |= (1<<4);
2082 if (!(flags & O_EXCL)) {
2083 if (flags & O_TRUNC)
2084 openfn |= (1<<1);
2085 else
2086 openfn |= (1<<0);
2089 accessmode = (share_mode<<4);
2091 if ((flags & O_ACCMODE) == O_RDWR) {
2092 accessmode |= 2;
2093 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2094 accessmode |= 1;
2097 #if defined(O_SYNC)
2098 if ((flags & O_SYNC) == O_SYNC) {
2099 accessmode |= (1<<14);
2101 #endif /* O_SYNC */
2103 if (share_mode == DENY_FCB) {
2104 accessmode = 0xFF;
2107 SCVAL(state->vwv + 0, 0, 0xFF);
2108 SCVAL(state->vwv + 0, 1, 0);
2109 SSVAL(state->vwv + 1, 0, 0);
2110 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2111 SSVAL(state->vwv + 3, 0, accessmode);
2112 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2113 SSVAL(state->vwv + 5, 0, 0);
2114 SIVAL(state->vwv + 6, 0, 0);
2115 SSVAL(state->vwv + 8, 0, openfn);
2116 SIVAL(state->vwv + 9, 0, 0);
2117 SIVAL(state->vwv + 11, 0, 0);
2118 SIVAL(state->vwv + 13, 0, 0);
2120 additional_flags = 0;
2122 if (cli->use_oplocks) {
2123 /* if using oplocks then ask for a batch oplock via
2124 core and extended methods */
2125 additional_flags =
2126 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2127 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2130 bytes = talloc_array(state, uint8_t, 0);
2131 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2132 strlen(fname)+1, NULL);
2134 if (tevent_req_nomem(bytes, req)) {
2135 return tevent_req_post(req, ev);
2138 state->bytes.iov_base = (void *)bytes;
2139 state->bytes.iov_len = talloc_get_size(bytes);
2141 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2142 15, state->vwv, 1, &state->bytes);
2143 if (subreq == NULL) {
2144 TALLOC_FREE(req);
2145 return NULL;
2147 tevent_req_set_callback(subreq, cli_open_done, req);
2148 *psmbreq = subreq;
2149 return req;
2152 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2153 struct cli_state *cli, const char *fname,
2154 int flags, int share_mode)
2156 struct tevent_req *req, *subreq;
2157 NTSTATUS status;
2159 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2160 &subreq);
2161 if (req == NULL) {
2162 return NULL;
2165 status = cli_smb_req_send(subreq);
2166 if (tevent_req_nterror(req, status)) {
2167 return tevent_req_post(req, ev);
2169 return req;
2172 static void cli_open_done(struct tevent_req *subreq)
2174 struct tevent_req *req = tevent_req_callback_data(
2175 subreq, struct tevent_req);
2176 struct cli_open_state *state = tevent_req_data(
2177 req, struct cli_open_state);
2178 uint8_t wct;
2179 uint16_t *vwv;
2180 uint8_t *inbuf;
2181 NTSTATUS status;
2183 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2184 NULL);
2185 TALLOC_FREE(subreq);
2186 if (tevent_req_nterror(req, status)) {
2187 return;
2189 state->fnum = SVAL(vwv+2, 0);
2190 tevent_req_done(req);
2193 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2195 struct cli_open_state *state = tevent_req_data(
2196 req, struct cli_open_state);
2197 NTSTATUS status;
2199 if (tevent_req_is_nterror(req, &status)) {
2200 return status;
2202 *pfnum = state->fnum;
2203 return NT_STATUS_OK;
2206 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2207 int share_mode, uint16_t *pfnum)
2209 TALLOC_CTX *frame = talloc_stackframe();
2210 struct event_context *ev;
2211 struct tevent_req *req;
2212 NTSTATUS status = NT_STATUS_OK;
2214 if (cli_has_async_calls(cli)) {
2216 * Can't use sync call while an async call is in flight
2218 status = NT_STATUS_INVALID_PARAMETER;
2219 goto fail;
2222 ev = event_context_init(frame);
2223 if (ev == NULL) {
2224 status = NT_STATUS_NO_MEMORY;
2225 goto fail;
2228 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2229 if (req == NULL) {
2230 status = NT_STATUS_NO_MEMORY;
2231 goto fail;
2234 if (!tevent_req_poll(req, ev)) {
2235 status = map_nt_error_from_unix(errno);
2236 goto fail;
2239 status = cli_open_recv(req, pfnum);
2240 fail:
2241 TALLOC_FREE(frame);
2242 if (!NT_STATUS_IS_OK(status)) {
2243 cli_set_error(cli, status);
2245 return status;
2248 /****************************************************************************
2249 Close a file.
2250 ****************************************************************************/
2252 struct cli_close_state {
2253 uint16_t vwv[3];
2256 static void cli_close_done(struct tevent_req *subreq);
2258 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2259 struct event_context *ev,
2260 struct cli_state *cli,
2261 uint16_t fnum,
2262 struct tevent_req **psubreq)
2264 struct tevent_req *req, *subreq;
2265 struct cli_close_state *state;
2267 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2268 if (req == NULL) {
2269 return NULL;
2272 SSVAL(state->vwv+0, 0, fnum);
2273 SIVALS(state->vwv+1, 0, -1);
2275 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2276 0, NULL);
2277 if (subreq == NULL) {
2278 TALLOC_FREE(req);
2279 return NULL;
2281 tevent_req_set_callback(subreq, cli_close_done, req);
2282 *psubreq = subreq;
2283 return req;
2286 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2287 struct event_context *ev,
2288 struct cli_state *cli,
2289 uint16_t fnum)
2291 struct tevent_req *req, *subreq;
2292 NTSTATUS status;
2294 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2295 if (req == NULL) {
2296 return NULL;
2299 status = cli_smb_req_send(subreq);
2300 if (tevent_req_nterror(req, status)) {
2301 return tevent_req_post(req, ev);
2303 return req;
2306 static void cli_close_done(struct tevent_req *subreq)
2308 struct tevent_req *req = tevent_req_callback_data(
2309 subreq, struct tevent_req);
2310 NTSTATUS status;
2312 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2313 TALLOC_FREE(subreq);
2314 if (tevent_req_nterror(req, status)) {
2315 return;
2317 tevent_req_done(req);
2320 NTSTATUS cli_close_recv(struct tevent_req *req)
2322 return tevent_req_simple_recv_ntstatus(req);
2325 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2327 TALLOC_CTX *frame = talloc_stackframe();
2328 struct event_context *ev;
2329 struct tevent_req *req;
2330 NTSTATUS status = NT_STATUS_OK;
2332 if (cli_has_async_calls(cli)) {
2334 * Can't use sync call while an async call is in flight
2336 status = NT_STATUS_INVALID_PARAMETER;
2337 goto fail;
2340 ev = event_context_init(frame);
2341 if (ev == NULL) {
2342 status = NT_STATUS_NO_MEMORY;
2343 goto fail;
2346 req = cli_close_send(frame, ev, cli, fnum);
2347 if (req == NULL) {
2348 status = NT_STATUS_NO_MEMORY;
2349 goto fail;
2352 if (!tevent_req_poll(req, ev)) {
2353 status = map_nt_error_from_unix(errno);
2354 goto fail;
2357 status = cli_close_recv(req);
2358 fail:
2359 TALLOC_FREE(frame);
2360 if (!NT_STATUS_IS_OK(status)) {
2361 cli_set_error(cli, status);
2363 return status;
2366 /****************************************************************************
2367 Truncate a file to a specified size
2368 ****************************************************************************/
2370 struct ftrunc_state {
2371 uint16_t setup;
2372 uint8_t param[6];
2373 uint8_t data[8];
2376 static void cli_ftruncate_done(struct tevent_req *subreq)
2378 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2379 NULL, 0, NULL, NULL, 0, NULL);
2380 tevent_req_simple_finish_ntstatus(subreq, status);
2383 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2384 struct event_context *ev,
2385 struct cli_state *cli,
2386 uint16_t fnum,
2387 uint64_t size)
2389 struct tevent_req *req = NULL, *subreq = NULL;
2390 struct ftrunc_state *state = NULL;
2392 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2393 if (req == NULL) {
2394 return NULL;
2397 /* Setup setup word. */
2398 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2400 /* Setup param array. */
2401 SSVAL(state->param,0,fnum);
2402 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2403 SSVAL(state->param,4,0);
2405 /* Setup data array. */
2406 SBVAL(state->data, 0, size);
2408 subreq = cli_trans_send(state, /* mem ctx. */
2409 ev, /* event ctx. */
2410 cli, /* cli_state. */
2411 SMBtrans2, /* cmd. */
2412 NULL, /* pipe name. */
2413 -1, /* fid. */
2414 0, /* function. */
2415 0, /* flags. */
2416 &state->setup, /* setup. */
2417 1, /* num setup uint16_t words. */
2418 0, /* max returned setup. */
2419 state->param, /* param. */
2420 6, /* num param. */
2421 2, /* max returned param. */
2422 state->data, /* data. */
2423 8, /* num data. */
2424 0); /* max returned data. */
2426 if (tevent_req_nomem(subreq, req)) {
2427 return tevent_req_post(req, ev);
2429 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2430 return req;
2433 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2435 return tevent_req_simple_recv_ntstatus(req);
2438 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2440 TALLOC_CTX *frame = talloc_stackframe();
2441 struct event_context *ev = NULL;
2442 struct tevent_req *req = NULL;
2443 NTSTATUS status = NT_STATUS_OK;
2445 if (cli_has_async_calls(cli)) {
2447 * Can't use sync call while an async call is in flight
2449 status = NT_STATUS_INVALID_PARAMETER;
2450 goto fail;
2453 ev = event_context_init(frame);
2454 if (ev == NULL) {
2455 status = NT_STATUS_NO_MEMORY;
2456 goto fail;
2459 req = cli_ftruncate_send(frame,
2461 cli,
2462 fnum,
2463 size);
2464 if (req == NULL) {
2465 status = NT_STATUS_NO_MEMORY;
2466 goto fail;
2469 if (!tevent_req_poll(req, ev)) {
2470 status = map_nt_error_from_unix(errno);
2471 goto fail;
2474 status = cli_ftruncate_recv(req);
2476 fail:
2477 TALLOC_FREE(frame);
2478 if (!NT_STATUS_IS_OK(status)) {
2479 cli_set_error(cli, status);
2481 return status;
2484 /****************************************************************************
2485 send a lock with a specified locktype
2486 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2487 ****************************************************************************/
2489 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2490 uint32_t offset, uint32_t len,
2491 int timeout, unsigned char locktype)
2493 uint16_t vwv[8];
2494 uint8_t bytes[10];
2495 NTSTATUS status;
2496 int saved_timeout;
2498 SCVAL(vwv + 0, 0, 0xff);
2499 SCVAL(vwv + 0, 1, 0);
2500 SSVAL(vwv + 1, 0, 0);
2501 SSVAL(vwv + 2, 0, fnum);
2502 SCVAL(vwv + 3, 0, locktype);
2503 SCVAL(vwv + 3, 1, 0);
2504 SIVALS(vwv + 4, 0, timeout);
2505 SSVAL(vwv + 6, 0, 0);
2506 SSVAL(vwv + 7, 0, 1);
2508 SSVAL(bytes, 0, cli->pid);
2509 SIVAL(bytes, 2, offset);
2510 SIVAL(bytes, 6, len);
2512 saved_timeout = cli->timeout;
2514 if (timeout != 0) {
2515 cli->timeout = (timeout == -1)
2516 ? 0x7FFFFFFF : (timeout + 2*1000);
2519 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2520 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2522 cli->timeout = saved_timeout;
2524 return status;
2527 /****************************************************************************
2528 Lock a file.
2529 note that timeout is in units of 2 milliseconds
2530 ****************************************************************************/
2532 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2533 uint32_t offset, uint32_t len, int timeout,
2534 enum brl_type lock_type)
2536 NTSTATUS status;
2538 status = cli_locktype(cli, fnum, offset, len, timeout,
2539 (lock_type == READ_LOCK? 1 : 0));
2540 cli_set_error(cli, status);
2541 return NT_STATUS_IS_OK(status);
2544 /****************************************************************************
2545 Unlock a file.
2546 ****************************************************************************/
2548 struct cli_unlock_state {
2549 uint16_t vwv[8];
2550 uint8_t data[10];
2553 static void cli_unlock_done(struct tevent_req *subreq);
2555 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2556 struct event_context *ev,
2557 struct cli_state *cli,
2558 uint16_t fnum,
2559 uint64_t offset,
2560 uint64_t len)
2563 struct tevent_req *req = NULL, *subreq = NULL;
2564 struct cli_unlock_state *state = NULL;
2565 uint8_t additional_flags = 0;
2567 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2568 if (req == NULL) {
2569 return NULL;
2572 SCVAL(state->vwv+0, 0, 0xFF);
2573 SSVAL(state->vwv+2, 0, fnum);
2574 SCVAL(state->vwv+3, 0, 0);
2575 SIVALS(state->vwv+4, 0, 0);
2576 SSVAL(state->vwv+6, 0, 1);
2577 SSVAL(state->vwv+7, 0, 0);
2579 SSVAL(state->data, 0, cli->pid);
2580 SIVAL(state->data, 2, offset);
2581 SIVAL(state->data, 6, len);
2583 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2584 8, state->vwv, 10, state->data);
2585 if (tevent_req_nomem(subreq, req)) {
2586 return tevent_req_post(req, ev);
2588 tevent_req_set_callback(subreq, cli_unlock_done, req);
2589 return req;
2592 static void cli_unlock_done(struct tevent_req *subreq)
2594 struct tevent_req *req = tevent_req_callback_data(
2595 subreq, struct tevent_req);
2596 NTSTATUS status;
2598 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2599 TALLOC_FREE(subreq);
2600 if (tevent_req_nterror(req, status)) {
2601 return;
2603 tevent_req_done(req);
2606 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2608 return tevent_req_simple_recv_ntstatus(req);
2611 NTSTATUS cli_unlock(struct cli_state *cli,
2612 uint16_t fnum,
2613 uint32_t offset,
2614 uint32_t len)
2616 TALLOC_CTX *frame = talloc_stackframe();
2617 struct event_context *ev;
2618 struct tevent_req *req;
2619 NTSTATUS status = NT_STATUS_OK;
2621 if (cli_has_async_calls(cli)) {
2623 * Can't use sync call while an async call is in flight
2625 status = NT_STATUS_INVALID_PARAMETER;
2626 goto fail;
2629 ev = event_context_init(frame);
2630 if (ev == NULL) {
2631 status = NT_STATUS_NO_MEMORY;
2632 goto fail;
2635 req = cli_unlock_send(frame, ev, cli,
2636 fnum, offset, len);
2637 if (req == NULL) {
2638 status = NT_STATUS_NO_MEMORY;
2639 goto fail;
2642 if (!tevent_req_poll(req, ev)) {
2643 status = map_nt_error_from_unix(errno);
2644 goto fail;
2647 status = cli_unlock_recv(req);
2649 fail:
2650 TALLOC_FREE(frame);
2651 if (!NT_STATUS_IS_OK(status)) {
2652 cli_set_error(cli, status);
2654 return status;
2657 /****************************************************************************
2658 Lock a file with 64 bit offsets.
2659 ****************************************************************************/
2661 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2662 uint64_t offset, uint64_t len, int timeout,
2663 enum brl_type lock_type)
2665 uint16_t vwv[8];
2666 uint8_t bytes[20];
2667 int saved_timeout = cli->timeout;
2668 int ltype;
2669 NTSTATUS status;
2671 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2672 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2675 ltype = (lock_type == READ_LOCK? 1 : 0);
2676 ltype |= LOCKING_ANDX_LARGE_FILES;
2678 SCVAL(vwv + 0, 0, 0xff);
2679 SCVAL(vwv + 0, 1, 0);
2680 SSVAL(vwv + 1, 0, 0);
2681 SSVAL(vwv + 2, 0, fnum);
2682 SCVAL(vwv + 3, 0, ltype);
2683 SCVAL(vwv + 3, 1, 0);
2684 SIVALS(vwv + 4, 0, timeout);
2685 SSVAL(vwv + 6, 0, 0);
2686 SSVAL(vwv + 7, 0, 1);
2688 SIVAL(bytes, 0, cli->pid);
2689 SOFF_T_R(bytes, 4, offset);
2690 SOFF_T_R(bytes, 12, len);
2692 saved_timeout = cli->timeout;
2694 if (timeout != 0) {
2695 cli->timeout = (timeout == -1)
2696 ? 0x7FFFFFFF : (timeout + 2*1000);
2699 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2700 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2702 cli->timeout = saved_timeout;
2704 cli_set_error(cli, status);
2705 return NT_STATUS_IS_OK(status);
2708 /****************************************************************************
2709 Unlock a file with 64 bit offsets.
2710 ****************************************************************************/
2712 struct cli_unlock64_state {
2713 uint16_t vwv[8];
2714 uint8_t data[20];
2717 static void cli_unlock64_done(struct tevent_req *subreq);
2719 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2720 struct event_context *ev,
2721 struct cli_state *cli,
2722 uint16_t fnum,
2723 uint64_t offset,
2724 uint64_t len)
2727 struct tevent_req *req = NULL, *subreq = NULL;
2728 struct cli_unlock64_state *state = NULL;
2729 uint8_t additional_flags = 0;
2731 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2732 if (req == NULL) {
2733 return NULL;
2736 SCVAL(state->vwv+0, 0, 0xff);
2737 SSVAL(state->vwv+2, 0, fnum);
2738 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2739 SIVALS(state->vwv+4, 0, 0);
2740 SSVAL(state->vwv+6, 0, 1);
2741 SSVAL(state->vwv+7, 0, 0);
2743 SIVAL(state->data, 0, cli->pid);
2744 SOFF_T_R(state->data, 4, offset);
2745 SOFF_T_R(state->data, 12, len);
2747 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2748 8, state->vwv, 20, state->data);
2749 if (tevent_req_nomem(subreq, req)) {
2750 return tevent_req_post(req, ev);
2752 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2753 return req;
2756 static void cli_unlock64_done(struct tevent_req *subreq)
2758 struct tevent_req *req = tevent_req_callback_data(
2759 subreq, struct tevent_req);
2760 NTSTATUS status;
2762 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2763 TALLOC_FREE(subreq);
2764 if (tevent_req_nterror(req, status)) {
2765 return;
2767 tevent_req_done(req);
2770 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2772 return tevent_req_simple_recv_ntstatus(req);
2775 NTSTATUS cli_unlock64(struct cli_state *cli,
2776 uint16_t fnum,
2777 uint64_t offset,
2778 uint64_t len)
2780 TALLOC_CTX *frame = talloc_stackframe();
2781 struct event_context *ev;
2782 struct tevent_req *req;
2783 NTSTATUS status = NT_STATUS_OK;
2785 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2786 return cli_unlock(cli, fnum, offset, len);
2789 if (cli_has_async_calls(cli)) {
2791 * Can't use sync call while an async call is in flight
2793 status = NT_STATUS_INVALID_PARAMETER;
2794 goto fail;
2797 ev = event_context_init(frame);
2798 if (ev == NULL) {
2799 status = NT_STATUS_NO_MEMORY;
2800 goto fail;
2803 req = cli_unlock64_send(frame, ev, cli,
2804 fnum, offset, len);
2805 if (req == NULL) {
2806 status = NT_STATUS_NO_MEMORY;
2807 goto fail;
2810 if (!tevent_req_poll(req, ev)) {
2811 status = map_nt_error_from_unix(errno);
2812 goto fail;
2815 status = cli_unlock64_recv(req);
2817 fail:
2818 TALLOC_FREE(frame);
2819 if (!NT_STATUS_IS_OK(status)) {
2820 cli_set_error(cli, status);
2822 return status;
2825 /****************************************************************************
2826 Get/unlock a POSIX lock on a file - internal function.
2827 ****************************************************************************/
2829 struct posix_lock_state {
2830 uint16_t setup;
2831 uint8_t param[4];
2832 uint8_t data[POSIX_LOCK_DATA_SIZE];
2835 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2837 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2838 NULL, 0, NULL, NULL, 0, NULL);
2839 tevent_req_simple_finish_ntstatus(subreq, status);
2842 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2843 struct event_context *ev,
2844 struct cli_state *cli,
2845 uint16_t fnum,
2846 uint64_t offset,
2847 uint64_t len,
2848 bool wait_lock,
2849 enum brl_type lock_type)
2851 struct tevent_req *req = NULL, *subreq = NULL;
2852 struct posix_lock_state *state = NULL;
2854 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2855 if (req == NULL) {
2856 return NULL;
2859 /* Setup setup word. */
2860 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2862 /* Setup param array. */
2863 SSVAL(&state->param, 0, fnum);
2864 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2866 /* Setup data array. */
2867 switch (lock_type) {
2868 case READ_LOCK:
2869 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2870 POSIX_LOCK_TYPE_READ);
2871 break;
2872 case WRITE_LOCK:
2873 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2874 POSIX_LOCK_TYPE_WRITE);
2875 break;
2876 case UNLOCK_LOCK:
2877 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2878 POSIX_LOCK_TYPE_UNLOCK);
2879 break;
2880 default:
2881 return NULL;
2884 if (wait_lock) {
2885 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2886 POSIX_LOCK_FLAG_WAIT);
2887 } else {
2888 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2889 POSIX_LOCK_FLAG_NOWAIT);
2892 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2893 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2894 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2896 subreq = cli_trans_send(state, /* mem ctx. */
2897 ev, /* event ctx. */
2898 cli, /* cli_state. */
2899 SMBtrans2, /* cmd. */
2900 NULL, /* pipe name. */
2901 -1, /* fid. */
2902 0, /* function. */
2903 0, /* flags. */
2904 &state->setup, /* setup. */
2905 1, /* num setup uint16_t words. */
2906 0, /* max returned setup. */
2907 state->param, /* param. */
2908 4, /* num param. */
2909 2, /* max returned param. */
2910 state->data, /* data. */
2911 POSIX_LOCK_DATA_SIZE, /* num data. */
2912 0); /* max returned data. */
2914 if (tevent_req_nomem(subreq, req)) {
2915 return tevent_req_post(req, ev);
2917 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2918 return req;
2921 /****************************************************************************
2922 POSIX Lock a file.
2923 ****************************************************************************/
2925 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2926 struct event_context *ev,
2927 struct cli_state *cli,
2928 uint16_t fnum,
2929 uint64_t offset,
2930 uint64_t len,
2931 bool wait_lock,
2932 enum brl_type lock_type)
2934 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2935 wait_lock, lock_type);
2938 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2940 return tevent_req_simple_recv_ntstatus(req);
2943 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2944 uint64_t offset, uint64_t len,
2945 bool wait_lock, enum brl_type lock_type)
2947 TALLOC_CTX *frame = talloc_stackframe();
2948 struct event_context *ev = NULL;
2949 struct tevent_req *req = NULL;
2950 NTSTATUS status = NT_STATUS_OK;
2952 if (cli_has_async_calls(cli)) {
2954 * Can't use sync call while an async call is in flight
2956 status = NT_STATUS_INVALID_PARAMETER;
2957 goto fail;
2960 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2961 status = NT_STATUS_INVALID_PARAMETER;
2962 goto fail;
2965 ev = event_context_init(frame);
2966 if (ev == NULL) {
2967 status = NT_STATUS_NO_MEMORY;
2968 goto fail;
2971 req = cli_posix_lock_send(frame,
2973 cli,
2974 fnum,
2975 offset,
2976 len,
2977 wait_lock,
2978 lock_type);
2979 if (req == NULL) {
2980 status = NT_STATUS_NO_MEMORY;
2981 goto fail;
2984 if (!tevent_req_poll(req, ev)) {
2985 status = map_nt_error_from_unix(errno);
2986 goto fail;
2989 status = cli_posix_lock_recv(req);
2991 fail:
2992 TALLOC_FREE(frame);
2993 if (!NT_STATUS_IS_OK(status)) {
2994 cli_set_error(cli, status);
2996 return status;
2999 /****************************************************************************
3000 POSIX Unlock a file.
3001 ****************************************************************************/
3003 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3004 struct event_context *ev,
3005 struct cli_state *cli,
3006 uint16_t fnum,
3007 uint64_t offset,
3008 uint64_t len)
3010 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3011 false, UNLOCK_LOCK);
3014 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3016 return tevent_req_simple_recv_ntstatus(req);
3019 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3021 TALLOC_CTX *frame = talloc_stackframe();
3022 struct event_context *ev = NULL;
3023 struct tevent_req *req = NULL;
3024 NTSTATUS status = NT_STATUS_OK;
3026 if (cli_has_async_calls(cli)) {
3028 * Can't use sync call while an async call is in flight
3030 status = NT_STATUS_INVALID_PARAMETER;
3031 goto fail;
3034 ev = event_context_init(frame);
3035 if (ev == NULL) {
3036 status = NT_STATUS_NO_MEMORY;
3037 goto fail;
3040 req = cli_posix_unlock_send(frame,
3042 cli,
3043 fnum,
3044 offset,
3045 len);
3046 if (req == NULL) {
3047 status = NT_STATUS_NO_MEMORY;
3048 goto fail;
3051 if (!tevent_req_poll(req, ev)) {
3052 status = map_nt_error_from_unix(errno);
3053 goto fail;
3056 status = cli_posix_unlock_recv(req);
3058 fail:
3059 TALLOC_FREE(frame);
3060 if (!NT_STATUS_IS_OK(status)) {
3061 cli_set_error(cli, status);
3063 return status;
3066 /****************************************************************************
3067 Do a SMBgetattrE call.
3068 ****************************************************************************/
3070 static void cli_getattrE_done(struct tevent_req *subreq);
3072 struct cli_getattrE_state {
3073 uint16_t vwv[1];
3074 int zone_offset;
3075 uint16_t attr;
3076 SMB_OFF_T size;
3077 time_t change_time;
3078 time_t access_time;
3079 time_t write_time;
3082 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3083 struct event_context *ev,
3084 struct cli_state *cli,
3085 uint16_t fnum)
3087 struct tevent_req *req = NULL, *subreq = NULL;
3088 struct cli_getattrE_state *state = NULL;
3089 uint8_t additional_flags = 0;
3091 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3092 if (req == NULL) {
3093 return NULL;
3096 state->zone_offset = cli->serverzone;
3097 SSVAL(state->vwv+0,0,fnum);
3099 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3100 1, state->vwv, 0, NULL);
3101 if (tevent_req_nomem(subreq, req)) {
3102 return tevent_req_post(req, ev);
3104 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3105 return req;
3108 static void cli_getattrE_done(struct tevent_req *subreq)
3110 struct tevent_req *req = tevent_req_callback_data(
3111 subreq, struct tevent_req);
3112 struct cli_getattrE_state *state = tevent_req_data(
3113 req, struct cli_getattrE_state);
3114 uint8_t wct;
3115 uint16_t *vwv = NULL;
3116 uint8_t *inbuf;
3117 NTSTATUS status;
3119 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3120 NULL, NULL);
3121 TALLOC_FREE(subreq);
3122 if (tevent_req_nterror(req, status)) {
3123 return;
3126 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3127 state->attr = SVAL(vwv+10,0);
3128 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3129 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3130 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3132 tevent_req_done(req);
3135 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3136 uint16_t *attr,
3137 SMB_OFF_T *size,
3138 time_t *change_time,
3139 time_t *access_time,
3140 time_t *write_time)
3142 struct cli_getattrE_state *state = tevent_req_data(
3143 req, struct cli_getattrE_state);
3144 NTSTATUS status;
3146 if (tevent_req_is_nterror(req, &status)) {
3147 return status;
3149 if (attr) {
3150 *attr = state->attr;
3152 if (size) {
3153 *size = state->size;
3155 if (change_time) {
3156 *change_time = state->change_time;
3158 if (access_time) {
3159 *access_time = state->access_time;
3161 if (write_time) {
3162 *write_time = state->write_time;
3164 return NT_STATUS_OK;
3167 NTSTATUS cli_getattrE(struct cli_state *cli,
3168 uint16_t fnum,
3169 uint16_t *attr,
3170 SMB_OFF_T *size,
3171 time_t *change_time,
3172 time_t *access_time,
3173 time_t *write_time)
3175 TALLOC_CTX *frame = talloc_stackframe();
3176 struct event_context *ev = NULL;
3177 struct tevent_req *req = NULL;
3178 NTSTATUS status = NT_STATUS_OK;
3180 if (cli_has_async_calls(cli)) {
3182 * Can't use sync call while an async call is in flight
3184 status = NT_STATUS_INVALID_PARAMETER;
3185 goto fail;
3188 ev = event_context_init(frame);
3189 if (ev == NULL) {
3190 status = NT_STATUS_NO_MEMORY;
3191 goto fail;
3194 req = cli_getattrE_send(frame, ev, cli, fnum);
3195 if (req == NULL) {
3196 status = NT_STATUS_NO_MEMORY;
3197 goto fail;
3200 if (!tevent_req_poll(req, ev)) {
3201 status = map_nt_error_from_unix(errno);
3202 goto fail;
3205 status = cli_getattrE_recv(req,
3206 attr,
3207 size,
3208 change_time,
3209 access_time,
3210 write_time);
3212 fail:
3213 TALLOC_FREE(frame);
3214 if (!NT_STATUS_IS_OK(status)) {
3215 cli_set_error(cli, status);
3217 return status;
3220 /****************************************************************************
3221 Do a SMBgetatr call
3222 ****************************************************************************/
3224 static void cli_getatr_done(struct tevent_req *subreq);
3226 struct cli_getatr_state {
3227 int zone_offset;
3228 uint16_t attr;
3229 SMB_OFF_T size;
3230 time_t write_time;
3233 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3234 struct event_context *ev,
3235 struct cli_state *cli,
3236 const char *fname)
3238 struct tevent_req *req = NULL, *subreq = NULL;
3239 struct cli_getatr_state *state = NULL;
3240 uint8_t additional_flags = 0;
3241 uint8_t *bytes = NULL;
3243 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3244 if (req == NULL) {
3245 return NULL;
3248 state->zone_offset = cli->serverzone;
3250 bytes = talloc_array(state, uint8_t, 1);
3251 if (tevent_req_nomem(bytes, req)) {
3252 return tevent_req_post(req, ev);
3254 bytes[0] = 4;
3255 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3256 strlen(fname)+1, NULL);
3258 if (tevent_req_nomem(bytes, req)) {
3259 return tevent_req_post(req, ev);
3262 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3263 0, NULL, talloc_get_size(bytes), bytes);
3264 if (tevent_req_nomem(subreq, req)) {
3265 return tevent_req_post(req, ev);
3267 tevent_req_set_callback(subreq, cli_getatr_done, req);
3268 return req;
3271 static void cli_getatr_done(struct tevent_req *subreq)
3273 struct tevent_req *req = tevent_req_callback_data(
3274 subreq, struct tevent_req);
3275 struct cli_getatr_state *state = tevent_req_data(
3276 req, struct cli_getatr_state);
3277 uint8_t wct;
3278 uint16_t *vwv = NULL;
3279 uint8_t *inbuf;
3280 NTSTATUS status;
3282 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3283 NULL);
3284 TALLOC_FREE(subreq);
3285 if (tevent_req_nterror(req, status)) {
3286 return;
3289 state->attr = SVAL(vwv+0,0);
3290 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3291 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3293 tevent_req_done(req);
3296 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3297 uint16_t *attr,
3298 SMB_OFF_T *size,
3299 time_t *write_time)
3301 struct cli_getatr_state *state = tevent_req_data(
3302 req, struct cli_getatr_state);
3303 NTSTATUS status;
3305 if (tevent_req_is_nterror(req, &status)) {
3306 return status;
3308 if (attr) {
3309 *attr = state->attr;
3311 if (size) {
3312 *size = state->size;
3314 if (write_time) {
3315 *write_time = state->write_time;
3317 return NT_STATUS_OK;
3320 NTSTATUS cli_getatr(struct cli_state *cli,
3321 const char *fname,
3322 uint16_t *attr,
3323 SMB_OFF_T *size,
3324 time_t *write_time)
3326 TALLOC_CTX *frame = talloc_stackframe();
3327 struct event_context *ev = NULL;
3328 struct tevent_req *req = NULL;
3329 NTSTATUS status = NT_STATUS_OK;
3331 if (cli_has_async_calls(cli)) {
3333 * Can't use sync call while an async call is in flight
3335 status = NT_STATUS_INVALID_PARAMETER;
3336 goto fail;
3339 ev = event_context_init(frame);
3340 if (ev == NULL) {
3341 status = NT_STATUS_NO_MEMORY;
3342 goto fail;
3345 req = cli_getatr_send(frame, ev, cli, fname);
3346 if (req == NULL) {
3347 status = NT_STATUS_NO_MEMORY;
3348 goto fail;
3351 if (!tevent_req_poll(req, ev)) {
3352 status = map_nt_error_from_unix(errno);
3353 goto fail;
3356 status = cli_getatr_recv(req,
3357 attr,
3358 size,
3359 write_time);
3361 fail:
3362 TALLOC_FREE(frame);
3363 if (!NT_STATUS_IS_OK(status)) {
3364 cli_set_error(cli, status);
3366 return status;
3369 /****************************************************************************
3370 Do a SMBsetattrE call.
3371 ****************************************************************************/
3373 static void cli_setattrE_done(struct tevent_req *subreq);
3375 struct cli_setattrE_state {
3376 uint16_t vwv[7];
3379 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3380 struct event_context *ev,
3381 struct cli_state *cli,
3382 uint16_t fnum,
3383 time_t change_time,
3384 time_t access_time,
3385 time_t write_time)
3387 struct tevent_req *req = NULL, *subreq = NULL;
3388 struct cli_setattrE_state *state = NULL;
3389 uint8_t additional_flags = 0;
3391 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3392 if (req == NULL) {
3393 return NULL;
3396 SSVAL(state->vwv+0, 0, fnum);
3397 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3398 cli->serverzone);
3399 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3400 cli->serverzone);
3401 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3402 cli->serverzone);
3404 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3405 7, state->vwv, 0, NULL);
3406 if (tevent_req_nomem(subreq, req)) {
3407 return tevent_req_post(req, ev);
3409 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3410 return req;
3413 static void cli_setattrE_done(struct tevent_req *subreq)
3415 struct tevent_req *req = tevent_req_callback_data(
3416 subreq, struct tevent_req);
3417 NTSTATUS status;
3419 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3420 TALLOC_FREE(subreq);
3421 if (tevent_req_nterror(req, status)) {
3422 return;
3424 tevent_req_done(req);
3427 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3429 return tevent_req_simple_recv_ntstatus(req);
3432 NTSTATUS cli_setattrE(struct cli_state *cli,
3433 uint16_t fnum,
3434 time_t change_time,
3435 time_t access_time,
3436 time_t write_time)
3438 TALLOC_CTX *frame = talloc_stackframe();
3439 struct event_context *ev = NULL;
3440 struct tevent_req *req = NULL;
3441 NTSTATUS status = NT_STATUS_OK;
3443 if (cli_has_async_calls(cli)) {
3445 * Can't use sync call while an async call is in flight
3447 status = NT_STATUS_INVALID_PARAMETER;
3448 goto fail;
3451 ev = event_context_init(frame);
3452 if (ev == NULL) {
3453 status = NT_STATUS_NO_MEMORY;
3454 goto fail;
3457 req = cli_setattrE_send(frame, ev,
3458 cli,
3459 fnum,
3460 change_time,
3461 access_time,
3462 write_time);
3464 if (req == NULL) {
3465 status = NT_STATUS_NO_MEMORY;
3466 goto fail;
3469 if (!tevent_req_poll(req, ev)) {
3470 status = map_nt_error_from_unix(errno);
3471 goto fail;
3474 status = cli_setattrE_recv(req);
3476 fail:
3477 TALLOC_FREE(frame);
3478 if (!NT_STATUS_IS_OK(status)) {
3479 cli_set_error(cli, status);
3481 return status;
3484 /****************************************************************************
3485 Do a SMBsetatr call.
3486 ****************************************************************************/
3488 static void cli_setatr_done(struct tevent_req *subreq);
3490 struct cli_setatr_state {
3491 uint16_t vwv[8];
3494 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3495 struct event_context *ev,
3496 struct cli_state *cli,
3497 const char *fname,
3498 uint16_t attr,
3499 time_t mtime)
3501 struct tevent_req *req = NULL, *subreq = NULL;
3502 struct cli_setatr_state *state = NULL;
3503 uint8_t additional_flags = 0;
3504 uint8_t *bytes = NULL;
3506 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3507 if (req == NULL) {
3508 return NULL;
3511 SSVAL(state->vwv+0, 0, attr);
3512 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3514 bytes = talloc_array(state, uint8_t, 1);
3515 if (tevent_req_nomem(bytes, req)) {
3516 return tevent_req_post(req, ev);
3518 bytes[0] = 4;
3519 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3520 strlen(fname)+1, NULL);
3521 if (tevent_req_nomem(bytes, req)) {
3522 return tevent_req_post(req, ev);
3524 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3525 talloc_get_size(bytes)+1);
3526 if (tevent_req_nomem(bytes, req)) {
3527 return tevent_req_post(req, ev);
3530 bytes[talloc_get_size(bytes)-1] = 4;
3531 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3532 1, NULL);
3533 if (tevent_req_nomem(bytes, req)) {
3534 return tevent_req_post(req, ev);
3537 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3538 8, state->vwv, talloc_get_size(bytes), bytes);
3539 if (tevent_req_nomem(subreq, req)) {
3540 return tevent_req_post(req, ev);
3542 tevent_req_set_callback(subreq, cli_setatr_done, req);
3543 return req;
3546 static void cli_setatr_done(struct tevent_req *subreq)
3548 struct tevent_req *req = tevent_req_callback_data(
3549 subreq, struct tevent_req);
3550 NTSTATUS status;
3552 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3553 TALLOC_FREE(subreq);
3554 if (tevent_req_nterror(req, status)) {
3555 return;
3557 tevent_req_done(req);
3560 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3562 return tevent_req_simple_recv_ntstatus(req);
3565 NTSTATUS cli_setatr(struct cli_state *cli,
3566 const char *fname,
3567 uint16_t attr,
3568 time_t mtime)
3570 TALLOC_CTX *frame = talloc_stackframe();
3571 struct event_context *ev = NULL;
3572 struct tevent_req *req = NULL;
3573 NTSTATUS status = NT_STATUS_OK;
3575 if (cli_has_async_calls(cli)) {
3577 * Can't use sync call while an async call is in flight
3579 status = NT_STATUS_INVALID_PARAMETER;
3580 goto fail;
3583 ev = event_context_init(frame);
3584 if (ev == NULL) {
3585 status = NT_STATUS_NO_MEMORY;
3586 goto fail;
3589 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3590 if (req == NULL) {
3591 status = NT_STATUS_NO_MEMORY;
3592 goto fail;
3595 if (!tevent_req_poll(req, ev)) {
3596 status = map_nt_error_from_unix(errno);
3597 goto fail;
3600 status = cli_setatr_recv(req);
3602 fail:
3603 TALLOC_FREE(frame);
3604 if (!NT_STATUS_IS_OK(status)) {
3605 cli_set_error(cli, status);
3607 return status;
3610 /****************************************************************************
3611 Check for existance of a dir.
3612 ****************************************************************************/
3614 static void cli_chkpath_done(struct tevent_req *subreq);
3616 struct cli_chkpath_state {
3617 int dummy;
3620 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3621 struct event_context *ev,
3622 struct cli_state *cli,
3623 const char *fname)
3625 struct tevent_req *req = NULL, *subreq = NULL;
3626 struct cli_chkpath_state *state = NULL;
3627 uint8_t additional_flags = 0;
3628 uint8_t *bytes = NULL;
3630 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3631 if (req == NULL) {
3632 return NULL;
3635 bytes = talloc_array(state, uint8_t, 1);
3636 if (tevent_req_nomem(bytes, req)) {
3637 return tevent_req_post(req, ev);
3639 bytes[0] = 4;
3640 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3641 strlen(fname)+1, NULL);
3643 if (tevent_req_nomem(bytes, req)) {
3644 return tevent_req_post(req, ev);
3647 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3648 0, NULL, talloc_get_size(bytes), bytes);
3649 if (tevent_req_nomem(subreq, req)) {
3650 return tevent_req_post(req, ev);
3652 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3653 return req;
3656 static void cli_chkpath_done(struct tevent_req *subreq)
3658 struct tevent_req *req = tevent_req_callback_data(
3659 subreq, struct tevent_req);
3660 NTSTATUS status;
3662 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3663 TALLOC_FREE(subreq);
3664 if (tevent_req_nterror(req, status)) {
3665 return;
3667 tevent_req_done(req);
3670 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3672 return tevent_req_simple_recv_ntstatus(req);
3675 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3677 TALLOC_CTX *frame = talloc_stackframe();
3678 struct event_context *ev = NULL;
3679 struct tevent_req *req = NULL;
3680 char *path2 = NULL;
3681 NTSTATUS status = NT_STATUS_OK;
3683 if (cli_has_async_calls(cli)) {
3685 * Can't use sync call while an async call is in flight
3687 status = NT_STATUS_INVALID_PARAMETER;
3688 goto fail;
3691 path2 = talloc_strdup(frame, path);
3692 if (!path2) {
3693 status = NT_STATUS_NO_MEMORY;
3694 goto fail;
3696 trim_char(path2,'\0','\\');
3697 if (!*path2) {
3698 path2 = talloc_strdup(frame, "\\");
3699 if (!path2) {
3700 status = NT_STATUS_NO_MEMORY;
3701 goto fail;
3705 ev = event_context_init(frame);
3706 if (ev == NULL) {
3707 status = NT_STATUS_NO_MEMORY;
3708 goto fail;
3711 req = cli_chkpath_send(frame, ev, cli, path2);
3712 if (req == NULL) {
3713 status = NT_STATUS_NO_MEMORY;
3714 goto fail;
3717 if (!tevent_req_poll(req, ev)) {
3718 status = map_nt_error_from_unix(errno);
3719 goto fail;
3722 status = cli_chkpath_recv(req);
3724 fail:
3725 TALLOC_FREE(frame);
3726 if (!NT_STATUS_IS_OK(status)) {
3727 cli_set_error(cli, status);
3729 return status;
3732 /****************************************************************************
3733 Query disk space.
3734 ****************************************************************************/
3736 static void cli_dskattr_done(struct tevent_req *subreq);
3738 struct cli_dskattr_state {
3739 int bsize;
3740 int total;
3741 int avail;
3744 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3745 struct event_context *ev,
3746 struct cli_state *cli)
3748 struct tevent_req *req = NULL, *subreq = NULL;
3749 struct cli_dskattr_state *state = NULL;
3750 uint8_t additional_flags = 0;
3752 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3753 if (req == NULL) {
3754 return NULL;
3757 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3758 0, NULL, 0, NULL);
3759 if (tevent_req_nomem(subreq, req)) {
3760 return tevent_req_post(req, ev);
3762 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3763 return req;
3766 static void cli_dskattr_done(struct tevent_req *subreq)
3768 struct tevent_req *req = tevent_req_callback_data(
3769 subreq, struct tevent_req);
3770 struct cli_dskattr_state *state = tevent_req_data(
3771 req, struct cli_dskattr_state);
3772 uint8_t wct;
3773 uint16_t *vwv = NULL;
3774 uint8_t *inbuf;
3775 NTSTATUS status;
3777 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3778 NULL);
3779 TALLOC_FREE(subreq);
3780 if (tevent_req_nterror(req, status)) {
3781 return;
3783 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3784 state->total = SVAL(vwv+0, 0);
3785 state->avail = SVAL(vwv+3, 0);
3786 tevent_req_done(req);
3789 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3791 struct cli_dskattr_state *state = tevent_req_data(
3792 req, struct cli_dskattr_state);
3793 NTSTATUS status;
3795 if (tevent_req_is_nterror(req, &status)) {
3796 return status;
3798 *bsize = state->bsize;
3799 *total = state->total;
3800 *avail = state->avail;
3801 return NT_STATUS_OK;
3804 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3806 TALLOC_CTX *frame = talloc_stackframe();
3807 struct event_context *ev = NULL;
3808 struct tevent_req *req = NULL;
3809 NTSTATUS status = NT_STATUS_OK;
3811 if (cli_has_async_calls(cli)) {
3813 * Can't use sync call while an async call is in flight
3815 status = NT_STATUS_INVALID_PARAMETER;
3816 goto fail;
3819 ev = event_context_init(frame);
3820 if (ev == NULL) {
3821 status = NT_STATUS_NO_MEMORY;
3822 goto fail;
3825 req = cli_dskattr_send(frame, ev, cli);
3826 if (req == NULL) {
3827 status = NT_STATUS_NO_MEMORY;
3828 goto fail;
3831 if (!tevent_req_poll(req, ev)) {
3832 status = map_nt_error_from_unix(errno);
3833 goto fail;
3836 status = cli_dskattr_recv(req, bsize, total, avail);
3838 fail:
3839 TALLOC_FREE(frame);
3840 if (!NT_STATUS_IS_OK(status)) {
3841 cli_set_error(cli, status);
3843 return status;
3846 /****************************************************************************
3847 Create and open a temporary file.
3848 ****************************************************************************/
3850 static void cli_ctemp_done(struct tevent_req *subreq);
3852 struct ctemp_state {
3853 uint16_t vwv[3];
3854 char *ret_path;
3855 uint16_t fnum;
3858 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3859 struct event_context *ev,
3860 struct cli_state *cli,
3861 const char *path)
3863 struct tevent_req *req = NULL, *subreq = NULL;
3864 struct ctemp_state *state = NULL;
3865 uint8_t additional_flags = 0;
3866 uint8_t *bytes = NULL;
3868 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3869 if (req == NULL) {
3870 return NULL;
3873 SSVAL(state->vwv,0,0);
3874 SIVALS(state->vwv+1,0,-1);
3876 bytes = talloc_array(state, uint8_t, 1);
3877 if (tevent_req_nomem(bytes, req)) {
3878 return tevent_req_post(req, ev);
3880 bytes[0] = 4;
3881 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3882 strlen(path)+1, NULL);
3883 if (tevent_req_nomem(bytes, req)) {
3884 return tevent_req_post(req, ev);
3887 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3888 3, state->vwv, talloc_get_size(bytes), bytes);
3889 if (tevent_req_nomem(subreq, req)) {
3890 return tevent_req_post(req, ev);
3892 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3893 return req;
3896 static void cli_ctemp_done(struct tevent_req *subreq)
3898 struct tevent_req *req = tevent_req_callback_data(
3899 subreq, struct tevent_req);
3900 struct ctemp_state *state = tevent_req_data(
3901 req, struct ctemp_state);
3902 NTSTATUS status;
3903 uint8_t wcnt;
3904 uint16_t *vwv;
3905 uint32_t num_bytes = 0;
3906 uint8_t *bytes = NULL;
3907 uint8_t *inbuf;
3909 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3910 &num_bytes, &bytes);
3911 TALLOC_FREE(subreq);
3912 if (tevent_req_nterror(req, status)) {
3913 return;
3916 state->fnum = SVAL(vwv+0, 0);
3918 /* From W2K3, the result is just the ASCII name */
3919 if (num_bytes < 2) {
3920 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3921 return;
3924 if (pull_string_talloc(state,
3925 NULL,
3927 &state->ret_path,
3928 bytes,
3929 num_bytes,
3930 STR_ASCII) == 0) {
3931 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3932 return;
3934 tevent_req_done(req);
3937 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3938 TALLOC_CTX *ctx,
3939 uint16_t *pfnum,
3940 char **outfile)
3942 struct ctemp_state *state = tevent_req_data(req,
3943 struct ctemp_state);
3944 NTSTATUS status;
3946 if (tevent_req_is_nterror(req, &status)) {
3947 return status;
3949 *pfnum = state->fnum;
3950 *outfile = talloc_strdup(ctx, state->ret_path);
3951 if (!*outfile) {
3952 return NT_STATUS_NO_MEMORY;
3954 return NT_STATUS_OK;
3957 NTSTATUS cli_ctemp(struct cli_state *cli,
3958 TALLOC_CTX *ctx,
3959 const char *path,
3960 uint16_t *pfnum,
3961 char **out_path)
3963 TALLOC_CTX *frame = talloc_stackframe();
3964 struct event_context *ev;
3965 struct tevent_req *req;
3966 NTSTATUS status = NT_STATUS_OK;
3968 if (cli_has_async_calls(cli)) {
3970 * Can't use sync call while an async call is in flight
3972 status = NT_STATUS_INVALID_PARAMETER;
3973 goto fail;
3976 ev = event_context_init(frame);
3977 if (ev == NULL) {
3978 status = NT_STATUS_NO_MEMORY;
3979 goto fail;
3982 req = cli_ctemp_send(frame, ev, cli, path);
3983 if (req == NULL) {
3984 status = NT_STATUS_NO_MEMORY;
3985 goto fail;
3988 if (!tevent_req_poll(req, ev)) {
3989 status = map_nt_error_from_unix(errno);
3990 goto fail;
3993 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
3995 fail:
3996 TALLOC_FREE(frame);
3997 if (!NT_STATUS_IS_OK(status)) {
3998 cli_set_error(cli, status);
4000 return status;
4004 send a raw ioctl - used by the torture code
4006 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4008 uint16_t vwv[3];
4009 NTSTATUS status;
4011 SSVAL(vwv+0, 0, fnum);
4012 SSVAL(vwv+1, 0, code>>16);
4013 SSVAL(vwv+2, 0, (code&0xFFFF));
4015 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4016 NULL, 0, NULL, NULL, NULL, NULL);
4017 if (!NT_STATUS_IS_OK(status)) {
4018 return status;
4020 *blob = data_blob_null;
4021 return NT_STATUS_OK;
4024 /*********************************************************
4025 Set an extended attribute utility fn.
4026 *********************************************************/
4028 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4029 uint8_t *param, unsigned int param_len,
4030 const char *ea_name,
4031 const char *ea_val, size_t ea_len)
4033 uint16_t setup[1];
4034 unsigned int data_len = 0;
4035 uint8_t *data = NULL;
4036 char *p;
4037 size_t ea_namelen = strlen(ea_name);
4038 NTSTATUS status;
4040 SSVAL(setup, 0, setup_val);
4042 if (ea_namelen == 0 && ea_len == 0) {
4043 data_len = 4;
4044 data = (uint8_t *)SMB_MALLOC(data_len);
4045 if (!data) {
4046 return NT_STATUS_NO_MEMORY;
4048 p = (char *)data;
4049 SIVAL(p,0,data_len);
4050 } else {
4051 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4052 data = (uint8_t *)SMB_MALLOC(data_len);
4053 if (!data) {
4054 return NT_STATUS_NO_MEMORY;
4056 p = (char *)data;
4057 SIVAL(p,0,data_len);
4058 p += 4;
4059 SCVAL(p, 0, 0); /* EA flags. */
4060 SCVAL(p, 1, ea_namelen);
4061 SSVAL(p, 2, ea_len);
4062 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4063 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4066 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4067 setup, 1, 0,
4068 param, param_len, 2,
4069 data, data_len, cli->max_xmit,
4070 NULL,
4071 NULL, 0, NULL, /* rsetup */
4072 NULL, 0, NULL, /* rparam */
4073 NULL, 0, NULL); /* rdata */
4074 SAFE_FREE(data);
4075 return status;
4078 /*********************************************************
4079 Set an extended attribute on a pathname.
4080 *********************************************************/
4082 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4083 const char *ea_name, const char *ea_val,
4084 size_t ea_len)
4086 unsigned int param_len = 0;
4087 uint8_t *param;
4088 size_t srclen = 2*(strlen(path)+1);
4089 char *p;
4090 NTSTATUS status;
4092 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4093 if (!param) {
4094 return NT_STATUS_NO_MEMORY;
4096 memset(param, '\0', 6);
4097 SSVAL(param,0,SMB_INFO_SET_EA);
4098 p = (char *)(&param[6]);
4100 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4101 param_len = PTR_DIFF(p, param);
4103 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4104 ea_name, ea_val, ea_len);
4105 SAFE_FREE(param);
4106 return status;
4109 /*********************************************************
4110 Set an extended attribute on an fnum.
4111 *********************************************************/
4113 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4114 const char *ea_name, const char *ea_val,
4115 size_t ea_len)
4117 uint8_t param[6];
4119 memset(param, 0, 6);
4120 SSVAL(param,0,fnum);
4121 SSVAL(param,2,SMB_INFO_SET_EA);
4123 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4124 ea_name, ea_val, ea_len);
4127 /*********************************************************
4128 Get an extended attribute list utility fn.
4129 *********************************************************/
4131 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4132 size_t rdata_len,
4133 size_t *pnum_eas, struct ea_struct **pea_list)
4135 struct ea_struct *ea_list = NULL;
4136 size_t num_eas;
4137 size_t ea_size;
4138 const uint8_t *p;
4140 if (rdata_len < 4) {
4141 return false;
4144 ea_size = (size_t)IVAL(rdata,0);
4145 if (ea_size > rdata_len) {
4146 return false;
4149 if (ea_size == 0) {
4150 /* No EA's present. */
4151 *pnum_eas = 0;
4152 *pea_list = NULL;
4153 return true;
4156 p = rdata + 4;
4157 ea_size -= 4;
4159 /* Validate the EA list and count it. */
4160 for (num_eas = 0; ea_size >= 4; num_eas++) {
4161 unsigned int ea_namelen = CVAL(p,1);
4162 unsigned int ea_valuelen = SVAL(p,2);
4163 if (ea_namelen == 0) {
4164 return false;
4166 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4167 return false;
4169 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4170 p += 4 + ea_namelen + 1 + ea_valuelen;
4173 if (num_eas == 0) {
4174 *pnum_eas = 0;
4175 *pea_list = NULL;
4176 return true;
4179 *pnum_eas = num_eas;
4180 if (!pea_list) {
4181 /* Caller only wants number of EA's. */
4182 return true;
4185 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4186 if (!ea_list) {
4187 return false;
4190 ea_size = (size_t)IVAL(rdata,0);
4191 p = rdata + 4;
4193 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4194 struct ea_struct *ea = &ea_list[num_eas];
4195 fstring unix_ea_name;
4196 unsigned int ea_namelen = CVAL(p,1);
4197 unsigned int ea_valuelen = SVAL(p,2);
4199 ea->flags = CVAL(p,0);
4200 unix_ea_name[0] = '\0';
4201 pull_ascii_fstring(unix_ea_name, p + 4);
4202 ea->name = talloc_strdup(ea_list, unix_ea_name);
4203 if (!ea->name) {
4204 goto fail;
4206 /* Ensure the value is null terminated (in case it's a string). */
4207 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4208 if (!ea->value.data) {
4209 goto fail;
4211 if (ea_valuelen) {
4212 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4214 ea->value.data[ea_valuelen] = 0;
4215 ea->value.length--;
4216 p += 4 + ea_namelen + 1 + ea_valuelen;
4219 *pea_list = ea_list;
4220 return true;
4222 fail:
4223 TALLOC_FREE(ea_list);
4224 return false;
4227 /*********************************************************
4228 Get an extended attribute list from a pathname.
4229 *********************************************************/
4231 struct cli_get_ea_list_path_state {
4232 uint32_t num_data;
4233 uint8_t *data;
4236 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4238 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4239 struct tevent_context *ev,
4240 struct cli_state *cli,
4241 const char *fname)
4243 struct tevent_req *req, *subreq;
4244 struct cli_get_ea_list_path_state *state;
4246 req = tevent_req_create(mem_ctx, &state,
4247 struct cli_get_ea_list_path_state);
4248 if (req == NULL) {
4249 return NULL;
4251 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4252 SMB_INFO_QUERY_ALL_EAS, 4,
4253 cli->max_xmit);
4254 if (tevent_req_nomem(subreq, req)) {
4255 return tevent_req_post(req, ev);
4257 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4258 return req;
4261 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4263 struct tevent_req *req = tevent_req_callback_data(
4264 subreq, struct tevent_req);
4265 struct cli_get_ea_list_path_state *state = tevent_req_data(
4266 req, struct cli_get_ea_list_path_state);
4267 NTSTATUS status;
4269 status = cli_qpathinfo_recv(subreq, state, &state->data,
4270 &state->num_data);
4271 TALLOC_FREE(subreq);
4272 if (tevent_req_nterror(req, status)) {
4273 return;
4275 tevent_req_done(req);
4278 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4279 size_t *pnum_eas, struct ea_struct **peas)
4281 struct cli_get_ea_list_path_state *state = tevent_req_data(
4282 req, struct cli_get_ea_list_path_state);
4283 NTSTATUS status;
4285 if (tevent_req_is_nterror(req, &status)) {
4286 return status;
4288 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4289 pnum_eas, peas)) {
4290 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4292 return NT_STATUS_OK;
4295 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4296 TALLOC_CTX *ctx,
4297 size_t *pnum_eas,
4298 struct ea_struct **pea_list)
4300 TALLOC_CTX *frame = talloc_stackframe();
4301 struct event_context *ev = NULL;
4302 struct tevent_req *req = NULL;
4303 NTSTATUS status = NT_STATUS_NO_MEMORY;
4305 if (cli_has_async_calls(cli)) {
4307 * Can't use sync call while an async call is in flight
4309 status = NT_STATUS_INVALID_PARAMETER;
4310 goto fail;
4312 ev = event_context_init(frame);
4313 if (ev == NULL) {
4314 goto fail;
4316 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4317 if (req == NULL) {
4318 goto fail;
4320 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4321 goto fail;
4323 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4324 fail:
4325 TALLOC_FREE(frame);
4326 if (!NT_STATUS_IS_OK(status)) {
4327 cli_set_error(cli, status);
4329 return status;
4332 /****************************************************************************
4333 Convert open "flags" arg to uint32_t on wire.
4334 ****************************************************************************/
4336 static uint32_t open_flags_to_wire(int flags)
4338 int open_mode = flags & O_ACCMODE;
4339 uint32_t ret = 0;
4341 switch (open_mode) {
4342 case O_WRONLY:
4343 ret |= SMB_O_WRONLY;
4344 break;
4345 case O_RDWR:
4346 ret |= SMB_O_RDWR;
4347 break;
4348 default:
4349 case O_RDONLY:
4350 ret |= SMB_O_RDONLY;
4351 break;
4354 if (flags & O_CREAT) {
4355 ret |= SMB_O_CREAT;
4357 if (flags & O_EXCL) {
4358 ret |= SMB_O_EXCL;
4360 if (flags & O_TRUNC) {
4361 ret |= SMB_O_TRUNC;
4363 #if defined(O_SYNC)
4364 if (flags & O_SYNC) {
4365 ret |= SMB_O_SYNC;
4367 #endif /* O_SYNC */
4368 if (flags & O_APPEND) {
4369 ret |= SMB_O_APPEND;
4371 #if defined(O_DIRECT)
4372 if (flags & O_DIRECT) {
4373 ret |= SMB_O_DIRECT;
4375 #endif
4376 #if defined(O_DIRECTORY)
4377 if (flags & O_DIRECTORY) {
4378 ret |= SMB_O_DIRECTORY;
4380 #endif
4381 return ret;
4384 /****************************************************************************
4385 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4386 ****************************************************************************/
4388 struct posix_open_state {
4389 uint16_t setup;
4390 uint8_t *param;
4391 uint8_t data[18];
4392 uint16_t fnum; /* Out */
4395 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4397 struct tevent_req *req = tevent_req_callback_data(
4398 subreq, struct tevent_req);
4399 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4400 NTSTATUS status;
4401 uint8_t *data;
4402 uint32_t num_data;
4404 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4405 NULL, 0, NULL, &data, 12, &num_data);
4406 TALLOC_FREE(subreq);
4407 if (tevent_req_nterror(req, status)) {
4408 return;
4410 state->fnum = SVAL(data,2);
4411 tevent_req_done(req);
4414 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4415 struct event_context *ev,
4416 struct cli_state *cli,
4417 const char *fname,
4418 int flags,
4419 mode_t mode,
4420 bool is_dir)
4422 struct tevent_req *req = NULL, *subreq = NULL;
4423 struct posix_open_state *state = NULL;
4424 uint32_t wire_flags = open_flags_to_wire(flags);
4426 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4427 if (req == NULL) {
4428 return NULL;
4431 /* Setup setup word. */
4432 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4434 /* Setup param array. */
4435 state->param = talloc_array(state, uint8_t, 6);
4436 if (tevent_req_nomem(state->param, req)) {
4437 return tevent_req_post(req, ev);
4439 memset(state->param, '\0', 6);
4440 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4442 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4443 strlen(fname)+1, NULL);
4445 if (tevent_req_nomem(state->param, req)) {
4446 return tevent_req_post(req, ev);
4449 /* Setup data words. */
4450 if (is_dir) {
4451 wire_flags |= SMB_O_DIRECTORY;
4454 SIVAL(state->data,0,0); /* No oplock. */
4455 SIVAL(state->data,4,wire_flags);
4456 SIVAL(state->data,8,unix_perms_to_wire(mode));
4457 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4458 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4460 subreq = cli_trans_send(state, /* mem ctx. */
4461 ev, /* event ctx. */
4462 cli, /* cli_state. */
4463 SMBtrans2, /* cmd. */
4464 NULL, /* pipe name. */
4465 -1, /* fid. */
4466 0, /* function. */
4467 0, /* flags. */
4468 &state->setup, /* setup. */
4469 1, /* num setup uint16_t words. */
4470 0, /* max returned setup. */
4471 state->param, /* param. */
4472 talloc_get_size(state->param),/* num param. */
4473 2, /* max returned param. */
4474 state->data, /* data. */
4475 18, /* num data. */
4476 12); /* max returned data. */
4478 if (tevent_req_nomem(subreq, req)) {
4479 return tevent_req_post(req, ev);
4481 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4482 return req;
4485 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4486 struct event_context *ev,
4487 struct cli_state *cli,
4488 const char *fname,
4489 int flags,
4490 mode_t mode)
4492 return cli_posix_open_internal_send(mem_ctx, ev,
4493 cli, fname, flags, mode, false);
4496 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4498 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4499 NTSTATUS status;
4501 if (tevent_req_is_nterror(req, &status)) {
4502 return status;
4504 *pfnum = state->fnum;
4505 return NT_STATUS_OK;
4508 /****************************************************************************
4509 Open - POSIX semantics. Doesn't request oplock.
4510 ****************************************************************************/
4512 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4513 int flags, mode_t mode, uint16_t *pfnum)
4516 TALLOC_CTX *frame = talloc_stackframe();
4517 struct event_context *ev = NULL;
4518 struct tevent_req *req = NULL;
4519 NTSTATUS status = NT_STATUS_OK;
4521 if (cli_has_async_calls(cli)) {
4523 * Can't use sync call while an async call is in flight
4525 status = NT_STATUS_INVALID_PARAMETER;
4526 goto fail;
4529 ev = event_context_init(frame);
4530 if (ev == NULL) {
4531 status = NT_STATUS_NO_MEMORY;
4532 goto fail;
4535 req = cli_posix_open_send(frame,
4537 cli,
4538 fname,
4539 flags,
4540 mode);
4541 if (req == NULL) {
4542 status = NT_STATUS_NO_MEMORY;
4543 goto fail;
4546 if (!tevent_req_poll(req, ev)) {
4547 status = map_nt_error_from_unix(errno);
4548 goto fail;
4551 status = cli_posix_open_recv(req, pfnum);
4553 fail:
4554 TALLOC_FREE(frame);
4555 if (!NT_STATUS_IS_OK(status)) {
4556 cli_set_error(cli, status);
4558 return status;
4561 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4562 struct event_context *ev,
4563 struct cli_state *cli,
4564 const char *fname,
4565 mode_t mode)
4567 return cli_posix_open_internal_send(mem_ctx, ev,
4568 cli, fname, O_CREAT, mode, true);
4571 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4573 return tevent_req_simple_recv_ntstatus(req);
4576 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4578 TALLOC_CTX *frame = talloc_stackframe();
4579 struct event_context *ev = NULL;
4580 struct tevent_req *req = NULL;
4581 NTSTATUS status = NT_STATUS_OK;
4583 if (cli_has_async_calls(cli)) {
4585 * Can't use sync call while an async call is in flight
4587 status = NT_STATUS_INVALID_PARAMETER;
4588 goto fail;
4591 ev = event_context_init(frame);
4592 if (ev == NULL) {
4593 status = NT_STATUS_NO_MEMORY;
4594 goto fail;
4597 req = cli_posix_mkdir_send(frame,
4599 cli,
4600 fname,
4601 mode);
4602 if (req == NULL) {
4603 status = NT_STATUS_NO_MEMORY;
4604 goto fail;
4607 if (!tevent_req_poll(req, ev)) {
4608 status = map_nt_error_from_unix(errno);
4609 goto fail;
4612 status = cli_posix_mkdir_recv(req);
4614 fail:
4615 TALLOC_FREE(frame);
4616 if (!NT_STATUS_IS_OK(status)) {
4617 cli_set_error(cli, status);
4619 return status;
4622 /****************************************************************************
4623 unlink or rmdir - POSIX semantics.
4624 ****************************************************************************/
4626 struct cli_posix_unlink_internal_state {
4627 uint8_t data[2];
4630 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4632 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4633 struct event_context *ev,
4634 struct cli_state *cli,
4635 const char *fname,
4636 uint16_t level)
4638 struct tevent_req *req = NULL, *subreq = NULL;
4639 struct cli_posix_unlink_internal_state *state = NULL;
4641 req = tevent_req_create(mem_ctx, &state,
4642 struct cli_posix_unlink_internal_state);
4643 if (req == NULL) {
4644 return NULL;
4647 /* Setup data word. */
4648 SSVAL(state->data, 0, level);
4650 subreq = cli_setpathinfo_send(state, ev, cli,
4651 SMB_POSIX_PATH_UNLINK,
4652 fname,
4653 state->data, sizeof(state->data));
4654 if (tevent_req_nomem(subreq, req)) {
4655 return tevent_req_post(req, ev);
4657 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4658 return req;
4661 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4663 NTSTATUS status = cli_setpathinfo_recv(subreq);
4664 tevent_req_simple_finish_ntstatus(subreq, status);
4667 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4668 struct event_context *ev,
4669 struct cli_state *cli,
4670 const char *fname)
4672 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4673 SMB_POSIX_UNLINK_FILE_TARGET);
4676 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4678 return tevent_req_simple_recv_ntstatus(req);
4681 /****************************************************************************
4682 unlink - POSIX semantics.
4683 ****************************************************************************/
4685 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4687 TALLOC_CTX *frame = talloc_stackframe();
4688 struct event_context *ev = NULL;
4689 struct tevent_req *req = NULL;
4690 NTSTATUS status = NT_STATUS_OK;
4692 if (cli_has_async_calls(cli)) {
4694 * Can't use sync call while an async call is in flight
4696 status = NT_STATUS_INVALID_PARAMETER;
4697 goto fail;
4700 ev = event_context_init(frame);
4701 if (ev == NULL) {
4702 status = NT_STATUS_NO_MEMORY;
4703 goto fail;
4706 req = cli_posix_unlink_send(frame,
4708 cli,
4709 fname);
4710 if (req == NULL) {
4711 status = NT_STATUS_NO_MEMORY;
4712 goto fail;
4715 if (!tevent_req_poll(req, ev)) {
4716 status = map_nt_error_from_unix(errno);
4717 goto fail;
4720 status = cli_posix_unlink_recv(req);
4722 fail:
4723 TALLOC_FREE(frame);
4724 if (!NT_STATUS_IS_OK(status)) {
4725 cli_set_error(cli, status);
4727 return status;
4730 /****************************************************************************
4731 rmdir - POSIX semantics.
4732 ****************************************************************************/
4734 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4735 struct event_context *ev,
4736 struct cli_state *cli,
4737 const char *fname)
4739 return cli_posix_unlink_internal_send(
4740 mem_ctx, ev, cli, fname,
4741 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4744 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4746 return tevent_req_simple_recv_ntstatus(req);
4749 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4751 TALLOC_CTX *frame = talloc_stackframe();
4752 struct event_context *ev = NULL;
4753 struct tevent_req *req = NULL;
4754 NTSTATUS status = NT_STATUS_OK;
4756 if (cli_has_async_calls(cli)) {
4758 * Can't use sync call while an async call is in flight
4760 status = NT_STATUS_INVALID_PARAMETER;
4761 goto fail;
4764 ev = event_context_init(frame);
4765 if (ev == NULL) {
4766 status = NT_STATUS_NO_MEMORY;
4767 goto fail;
4770 req = cli_posix_rmdir_send(frame,
4772 cli,
4773 fname);
4774 if (req == NULL) {
4775 status = NT_STATUS_NO_MEMORY;
4776 goto fail;
4779 if (!tevent_req_poll(req, ev)) {
4780 status = map_nt_error_from_unix(errno);
4781 goto fail;
4784 status = cli_posix_rmdir_recv(req, frame);
4786 fail:
4787 TALLOC_FREE(frame);
4788 if (!NT_STATUS_IS_OK(status)) {
4789 cli_set_error(cli, status);
4791 return status;
4794 /****************************************************************************
4795 filechangenotify
4796 ****************************************************************************/
4798 struct cli_notify_state {
4799 uint8_t setup[8];
4800 uint32_t num_changes;
4801 struct notify_change *changes;
4804 static void cli_notify_done(struct tevent_req *subreq);
4806 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4807 struct tevent_context *ev,
4808 struct cli_state *cli, uint16_t fnum,
4809 uint32_t buffer_size,
4810 uint32_t completion_filter, bool recursive)
4812 struct tevent_req *req, *subreq;
4813 struct cli_notify_state *state;
4815 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4816 if (req == NULL) {
4817 return NULL;
4820 SIVAL(state->setup, 0, completion_filter);
4821 SSVAL(state->setup, 4, fnum);
4822 SSVAL(state->setup, 6, recursive);
4824 subreq = cli_trans_send(
4825 state, /* mem ctx. */
4826 ev, /* event ctx. */
4827 cli, /* cli_state. */
4828 SMBnttrans, /* cmd. */
4829 NULL, /* pipe name. */
4830 -1, /* fid. */
4831 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4832 0, /* flags. */
4833 (uint16_t *)state->setup, /* setup. */
4834 4, /* num setup uint16_t words. */
4835 0, /* max returned setup. */
4836 NULL, /* param. */
4837 0, /* num param. */
4838 buffer_size, /* max returned param. */
4839 NULL, /* data. */
4840 0, /* num data. */
4841 0); /* max returned data. */
4843 if (tevent_req_nomem(subreq, req)) {
4844 return tevent_req_post(req, ev);
4846 tevent_req_set_callback(subreq, cli_notify_done, req);
4847 return req;
4850 static void cli_notify_done(struct tevent_req *subreq)
4852 struct tevent_req *req = tevent_req_callback_data(
4853 subreq, struct tevent_req);
4854 struct cli_notify_state *state = tevent_req_data(
4855 req, struct cli_notify_state);
4856 NTSTATUS status;
4857 uint8_t *params;
4858 uint32_t i, ofs, num_params;
4859 uint16_t flags2;
4861 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4862 &params, 0, &num_params, NULL, 0, NULL);
4863 TALLOC_FREE(subreq);
4864 if (tevent_req_nterror(req, status)) {
4865 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4866 return;
4869 state->num_changes = 0;
4870 ofs = 0;
4872 while (num_params - ofs > 12) {
4873 uint32_t len = IVAL(params, ofs);
4874 state->num_changes += 1;
4876 if ((len == 0) || (ofs+len >= num_params)) {
4877 break;
4879 ofs += len;
4882 state->changes = talloc_array(state, struct notify_change,
4883 state->num_changes);
4884 if (tevent_req_nomem(state->changes, req)) {
4885 TALLOC_FREE(params);
4886 return;
4889 ofs = 0;
4891 for (i=0; i<state->num_changes; i++) {
4892 uint32_t next = IVAL(params, ofs);
4893 uint32_t len = IVAL(params, ofs+8);
4894 ssize_t ret;
4895 char *name;
4897 if ((next != 0) && (len+12 != next)) {
4898 TALLOC_FREE(params);
4899 tevent_req_nterror(
4900 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4901 return;
4904 state->changes[i].action = IVAL(params, ofs+4);
4905 ret = clistr_pull_talloc(params, (char *)params, flags2,
4906 &name, params+ofs+12, len,
4907 STR_TERMINATE|STR_UNICODE);
4908 if (ret == -1) {
4909 TALLOC_FREE(params);
4910 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4911 return;
4913 state->changes[i].name = name;
4914 ofs += next;
4917 TALLOC_FREE(params);
4918 tevent_req_done(req);
4921 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4922 uint32_t *pnum_changes,
4923 struct notify_change **pchanges)
4925 struct cli_notify_state *state = tevent_req_data(
4926 req, struct cli_notify_state);
4927 NTSTATUS status;
4929 if (tevent_req_is_nterror(req, &status)) {
4930 return status;
4933 *pnum_changes = state->num_changes;
4934 *pchanges = talloc_move(mem_ctx, &state->changes);
4935 return NT_STATUS_OK;
4938 struct cli_qpathinfo_state {
4939 uint8_t *param;
4940 uint8_t *data;
4941 uint16_t setup[1];
4942 uint32_t min_rdata;
4943 uint8_t *rdata;
4944 uint32_t num_rdata;
4947 static void cli_qpathinfo_done(struct tevent_req *subreq);
4949 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4950 struct tevent_context *ev,
4951 struct cli_state *cli, const char *fname,
4952 uint16_t level, uint32_t min_rdata,
4953 uint32_t max_rdata)
4955 struct tevent_req *req, *subreq;
4956 struct cli_qpathinfo_state *state;
4958 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4959 if (req == NULL) {
4960 return NULL;
4962 state->min_rdata = min_rdata;
4963 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4965 state->param = talloc_zero_array(state, uint8_t, 6);
4966 if (tevent_req_nomem(state->param, req)) {
4967 return tevent_req_post(req, ev);
4969 SSVAL(state->param, 0, level);
4970 state->param = trans2_bytes_push_str(
4971 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4972 if (tevent_req_nomem(state->param, req)) {
4973 return tevent_req_post(req, ev);
4976 subreq = cli_trans_send(
4977 state, /* mem ctx. */
4978 ev, /* event ctx. */
4979 cli, /* cli_state. */
4980 SMBtrans2, /* cmd. */
4981 NULL, /* pipe name. */
4982 -1, /* fid. */
4983 0, /* function. */
4984 0, /* flags. */
4985 state->setup, /* setup. */
4986 1, /* num setup uint16_t words. */
4987 0, /* max returned setup. */
4988 state->param, /* param. */
4989 talloc_get_size(state->param), /* num param. */
4990 2, /* max returned param. */
4991 NULL, /* data. */
4992 0, /* num data. */
4993 max_rdata); /* max returned data. */
4995 if (tevent_req_nomem(subreq, req)) {
4996 return tevent_req_post(req, ev);
4998 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
4999 return req;
5002 static void cli_qpathinfo_done(struct tevent_req *subreq)
5004 struct tevent_req *req = tevent_req_callback_data(
5005 subreq, struct tevent_req);
5006 struct cli_qpathinfo_state *state = tevent_req_data(
5007 req, struct cli_qpathinfo_state);
5008 NTSTATUS status;
5010 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5011 NULL, 0, NULL,
5012 &state->rdata, state->min_rdata,
5013 &state->num_rdata);
5014 if (tevent_req_nterror(req, status)) {
5015 return;
5017 tevent_req_done(req);
5020 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5021 uint8_t **rdata, uint32_t *num_rdata)
5023 struct cli_qpathinfo_state *state = tevent_req_data(
5024 req, struct cli_qpathinfo_state);
5025 NTSTATUS status;
5027 if (tevent_req_is_nterror(req, &status)) {
5028 return status;
5030 if (rdata != NULL) {
5031 *rdata = talloc_move(mem_ctx, &state->rdata);
5032 } else {
5033 TALLOC_FREE(state->rdata);
5035 if (num_rdata != NULL) {
5036 *num_rdata = state->num_rdata;
5038 return NT_STATUS_OK;
5041 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5042 const char *fname, uint16_t level, uint32_t min_rdata,
5043 uint32_t max_rdata,
5044 uint8_t **rdata, uint32_t *num_rdata)
5046 TALLOC_CTX *frame = talloc_stackframe();
5047 struct event_context *ev;
5048 struct tevent_req *req;
5049 NTSTATUS status = NT_STATUS_NO_MEMORY;
5051 if (cli_has_async_calls(cli)) {
5053 * Can't use sync call while an async call is in flight
5055 status = NT_STATUS_INVALID_PARAMETER;
5056 goto fail;
5058 ev = event_context_init(frame);
5059 if (ev == NULL) {
5060 goto fail;
5062 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5063 max_rdata);
5064 if (req == NULL) {
5065 goto fail;
5067 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5068 goto fail;
5070 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5071 fail:
5072 TALLOC_FREE(frame);
5073 if (!NT_STATUS_IS_OK(status)) {
5074 cli_set_error(cli, status);
5076 return status;
5079 struct cli_qfileinfo_state {
5080 uint16_t setup[1];
5081 uint8_t param[4];
5082 uint8_t *data;
5083 uint32_t min_rdata;
5084 uint8_t *rdata;
5085 uint32_t num_rdata;
5088 static void cli_qfileinfo_done(struct tevent_req *subreq);
5090 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5091 struct tevent_context *ev,
5092 struct cli_state *cli, uint16_t fnum,
5093 uint16_t level, uint32_t min_rdata,
5094 uint32_t max_rdata)
5096 struct tevent_req *req, *subreq;
5097 struct cli_qfileinfo_state *state;
5099 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5100 if (req == NULL) {
5101 return NULL;
5103 state->min_rdata = min_rdata;
5104 SSVAL(state->param, 0, fnum);
5105 SSVAL(state->param, 2, level);
5106 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5108 subreq = cli_trans_send(
5109 state, /* mem ctx. */
5110 ev, /* event ctx. */
5111 cli, /* cli_state. */
5112 SMBtrans2, /* cmd. */
5113 NULL, /* pipe name. */
5114 -1, /* fid. */
5115 0, /* function. */
5116 0, /* flags. */
5117 state->setup, /* setup. */
5118 1, /* num setup uint16_t words. */
5119 0, /* max returned setup. */
5120 state->param, /* param. */
5121 sizeof(state->param), /* num param. */
5122 2, /* max returned param. */
5123 NULL, /* data. */
5124 0, /* num data. */
5125 max_rdata); /* max returned data. */
5127 if (tevent_req_nomem(subreq, req)) {
5128 return tevent_req_post(req, ev);
5130 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5131 return req;
5134 static void cli_qfileinfo_done(struct tevent_req *subreq)
5136 struct tevent_req *req = tevent_req_callback_data(
5137 subreq, struct tevent_req);
5138 struct cli_qfileinfo_state *state = tevent_req_data(
5139 req, struct cli_qfileinfo_state);
5140 NTSTATUS status;
5142 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5143 NULL, 0, NULL,
5144 &state->rdata, state->min_rdata,
5145 &state->num_rdata);
5146 if (tevent_req_nterror(req, status)) {
5147 return;
5149 tevent_req_done(req);
5152 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5153 uint8_t **rdata, uint32_t *num_rdata)
5155 struct cli_qfileinfo_state *state = tevent_req_data(
5156 req, struct cli_qfileinfo_state);
5157 NTSTATUS status;
5159 if (tevent_req_is_nterror(req, &status)) {
5160 return status;
5162 if (rdata != NULL) {
5163 *rdata = talloc_move(mem_ctx, &state->rdata);
5164 } else {
5165 TALLOC_FREE(state->rdata);
5167 if (num_rdata != NULL) {
5168 *num_rdata = state->num_rdata;
5170 return NT_STATUS_OK;
5173 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5174 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5175 uint32_t max_rdata,
5176 uint8_t **rdata, uint32_t *num_rdata)
5178 TALLOC_CTX *frame = talloc_stackframe();
5179 struct event_context *ev;
5180 struct tevent_req *req;
5181 NTSTATUS status = NT_STATUS_NO_MEMORY;
5183 if (cli_has_async_calls(cli)) {
5185 * Can't use sync call while an async call is in flight
5187 status = NT_STATUS_INVALID_PARAMETER;
5188 goto fail;
5190 ev = event_context_init(frame);
5191 if (ev == NULL) {
5192 goto fail;
5194 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5195 max_rdata);
5196 if (req == NULL) {
5197 goto fail;
5199 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5200 goto fail;
5202 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5203 fail:
5204 TALLOC_FREE(frame);
5205 if (!NT_STATUS_IS_OK(status)) {
5206 cli_set_error(cli, status);
5208 return status;
5211 struct cli_flush_state {
5212 uint16_t vwv[1];
5215 static void cli_flush_done(struct tevent_req *subreq);
5217 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5218 struct event_context *ev,
5219 struct cli_state *cli,
5220 uint16_t fnum)
5222 struct tevent_req *req, *subreq;
5223 struct cli_flush_state *state;
5225 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5226 if (req == NULL) {
5227 return NULL;
5229 SSVAL(state->vwv + 0, 0, fnum);
5231 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5232 0, NULL);
5233 if (tevent_req_nomem(subreq, req)) {
5234 return tevent_req_post(req, ev);
5236 tevent_req_set_callback(subreq, cli_flush_done, req);
5237 return req;
5240 static void cli_flush_done(struct tevent_req *subreq)
5242 struct tevent_req *req = tevent_req_callback_data(
5243 subreq, struct tevent_req);
5244 NTSTATUS status;
5246 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5247 TALLOC_FREE(subreq);
5248 if (tevent_req_nterror(req, status)) {
5249 return;
5251 tevent_req_done(req);
5254 NTSTATUS cli_flush_recv(struct tevent_req *req)
5256 return tevent_req_simple_recv_ntstatus(req);
5259 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5261 TALLOC_CTX *frame = talloc_stackframe();
5262 struct event_context *ev;
5263 struct tevent_req *req;
5264 NTSTATUS status = NT_STATUS_NO_MEMORY;
5266 if (cli_has_async_calls(cli)) {
5268 * Can't use sync call while an async call is in flight
5270 status = NT_STATUS_INVALID_PARAMETER;
5271 goto fail;
5273 ev = event_context_init(frame);
5274 if (ev == NULL) {
5275 goto fail;
5277 req = cli_flush_send(frame, ev, cli, fnum);
5278 if (req == NULL) {
5279 goto fail;
5281 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5282 goto fail;
5284 status = cli_flush_recv(req);
5285 fail:
5286 TALLOC_FREE(frame);
5287 if (!NT_STATUS_IS_OK(status)) {
5288 cli_set_error(cli, status);
5290 return status;
5293 struct cli_shadow_copy_data_state {
5294 uint16_t setup[4];
5295 uint8_t *data;
5296 uint32_t num_data;
5297 bool get_names;
5300 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5302 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5303 struct tevent_context *ev,
5304 struct cli_state *cli,
5305 uint16_t fnum,
5306 bool get_names)
5308 struct tevent_req *req, *subreq;
5309 struct cli_shadow_copy_data_state *state;
5310 uint32_t ret_size;
5312 req = tevent_req_create(mem_ctx, &state,
5313 struct cli_shadow_copy_data_state);
5314 if (req == NULL) {
5315 return NULL;
5317 state->get_names = get_names;
5318 ret_size = get_names ? cli->max_xmit : 16;
5320 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5321 SSVAL(state->setup + 2, 0, fnum);
5322 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5323 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5325 subreq = cli_trans_send(
5326 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5327 state->setup, ARRAY_SIZE(state->setup), 0,
5328 NULL, 0, 0,
5329 NULL, 0, ret_size);
5330 if (tevent_req_nomem(subreq, req)) {
5331 return tevent_req_post(req, ev);
5333 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5334 return req;
5337 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5339 struct tevent_req *req = tevent_req_callback_data(
5340 subreq, struct tevent_req);
5341 struct cli_shadow_copy_data_state *state = tevent_req_data(
5342 req, struct cli_shadow_copy_data_state);
5343 NTSTATUS status;
5345 status = cli_trans_recv(subreq, state, NULL,
5346 NULL, 0, NULL, /* setup */
5347 NULL, 0, NULL, /* param */
5348 &state->data, 12, &state->num_data);
5349 TALLOC_FREE(subreq);
5350 if (tevent_req_nterror(req, status)) {
5351 return;
5353 tevent_req_done(req);
5356 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5357 char ***pnames, int *pnum_names)
5359 struct cli_shadow_copy_data_state *state = tevent_req_data(
5360 req, struct cli_shadow_copy_data_state);
5361 char **names;
5362 int i, num_names;
5363 uint32_t dlength;
5364 NTSTATUS status;
5366 if (tevent_req_is_nterror(req, &status)) {
5367 return status;
5369 num_names = IVAL(state->data, 4);
5370 dlength = IVAL(state->data, 8);
5372 if (!state->get_names) {
5373 *pnum_names = num_names;
5374 return NT_STATUS_OK;
5377 if (dlength+12 > state->num_data) {
5378 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5380 names = talloc_array(mem_ctx, char *, num_names);
5381 if (names == NULL) {
5382 return NT_STATUS_NO_MEMORY;
5385 for (i=0; i<num_names; i++) {
5386 bool ret;
5387 uint8_t *src;
5388 size_t converted_size;
5390 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5391 ret = convert_string_talloc(
5392 names, CH_UTF16LE, CH_UNIX,
5393 src, 2 * sizeof(SHADOW_COPY_LABEL),
5394 &names[i], &converted_size, True);
5395 if (!ret) {
5396 TALLOC_FREE(names);
5397 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5400 *pnum_names = num_names;
5401 *pnames = names;
5402 return NT_STATUS_OK;
5405 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5406 uint16_t fnum, bool get_names,
5407 char ***pnames, int *pnum_names)
5409 TALLOC_CTX *frame = talloc_stackframe();
5410 struct event_context *ev;
5411 struct tevent_req *req;
5412 NTSTATUS status = NT_STATUS_NO_MEMORY;
5414 if (cli_has_async_calls(cli)) {
5416 * Can't use sync call while an async call is in flight
5418 status = NT_STATUS_INVALID_PARAMETER;
5419 goto fail;
5421 ev = event_context_init(frame);
5422 if (ev == NULL) {
5423 goto fail;
5425 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5426 if (req == NULL) {
5427 goto fail;
5429 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5430 goto fail;
5432 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5433 fail:
5434 TALLOC_FREE(frame);
5435 if (!NT_STATUS_IS_OK(status)) {
5436 cli_set_error(cli, status);
5438 return status;