s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / libsmb / clifile.c
blob6503e234e5e1fc4ccdf0876980a7f7680eb33c44
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
27 #include "trans2.h"
28 #include "ntioctl.h"
29 #include "libcli/security/secdesc.h"
31 /***********************************************************
32 Common function for pushing stings, used by smb_bytes_push_str()
33 and trans_bytes_push_str(). Only difference is the align_odd
34 parameter setting.
35 ***********************************************************/
37 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
38 const char *str, size_t str_len,
39 bool align_odd,
40 size_t *pconverted_size)
42 size_t buflen;
43 char *converted;
44 size_t converted_size;
46 if (buf == NULL) {
47 return NULL;
50 buflen = talloc_get_size(buf);
52 if (ucs2 &&
53 ((align_odd && (buflen % 2 == 0)) ||
54 (!align_odd && (buflen % 2 == 1)))) {
56 * We're pushing into an SMB buffer, align odd
58 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
59 if (buf == NULL) {
60 return NULL;
62 buf[buflen] = '\0';
63 buflen += 1;
66 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
67 ucs2 ? CH_UTF16LE : CH_DOS,
68 str, str_len, &converted,
69 &converted_size)) {
70 return NULL;
73 buf = talloc_realloc(NULL, buf, uint8_t,
74 buflen + converted_size);
75 if (buf == NULL) {
76 TALLOC_FREE(converted);
77 return NULL;
80 memcpy(buf + buflen, converted, converted_size);
82 TALLOC_FREE(converted);
84 if (pconverted_size) {
85 *pconverted_size = converted_size;
88 return buf;
91 /***********************************************************
92 Push a string into an SMB buffer, with odd byte alignment
93 if it's a UCS2 string.
94 ***********************************************************/
96 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
97 const char *str, size_t str_len,
98 size_t *pconverted_size)
100 return internal_bytes_push_str(buf, ucs2, str, str_len,
101 true, pconverted_size);
104 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
105 const uint8_t *bytes, size_t num_bytes)
107 size_t buflen;
109 if (buf == NULL) {
110 return NULL;
112 buflen = talloc_get_size(buf);
114 buf = talloc_realloc(NULL, buf, uint8_t,
115 buflen + 1 + num_bytes);
116 if (buf == NULL) {
117 return NULL;
119 buf[buflen] = prefix;
120 memcpy(&buf[buflen+1], bytes, num_bytes);
121 return buf;
124 /***********************************************************
125 Same as smb_bytes_push_str(), but without the odd byte
126 align for ucs2 (we're pushing into a param or data block).
127 static for now, although this will probably change when
128 other modules use async trans calls.
129 ***********************************************************/
131 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
132 const char *str, size_t str_len,
133 size_t *pconverted_size)
135 return internal_bytes_push_str(buf, ucs2, str, str_len,
136 false, pconverted_size);
139 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
140 const uint8_t *bytes, size_t num_bytes)
142 size_t buflen;
144 if (buf == NULL) {
145 return NULL;
147 buflen = talloc_get_size(buf);
149 buf = talloc_realloc(NULL, buf, uint8_t,
150 buflen + num_bytes);
151 if (buf == NULL) {
152 return NULL;
154 memcpy(&buf[buflen], bytes, num_bytes);
155 return buf;
158 struct cli_setpathinfo_state {
159 uint16_t setup;
160 uint8_t *param;
163 static void cli_setpathinfo_done(struct tevent_req *subreq);
165 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
166 struct tevent_context *ev,
167 struct cli_state *cli,
168 uint16_t level,
169 const char *path,
170 uint8_t *data,
171 size_t data_len)
173 struct tevent_req *req, *subreq;
174 struct cli_setpathinfo_state *state;
176 req = tevent_req_create(mem_ctx, &state,
177 struct cli_setpathinfo_state);
178 if (req == NULL) {
179 return NULL;
182 /* Setup setup word. */
183 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
185 /* Setup param array. */
186 state->param = talloc_zero_array(state, uint8_t, 6);
187 if (tevent_req_nomem(state->param, req)) {
188 return tevent_req_post(req, ev);
190 SSVAL(state->param, 0, level);
192 state->param = trans2_bytes_push_str(
193 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
194 if (tevent_req_nomem(state->param, req)) {
195 return tevent_req_post(req, ev);
198 subreq = cli_trans_send(
199 state, /* mem ctx. */
200 ev, /* event ctx. */
201 cli, /* cli_state. */
202 SMBtrans2, /* cmd. */
203 NULL, /* pipe name. */
204 -1, /* fid. */
205 0, /* function. */
206 0, /* flags. */
207 &state->setup, /* setup. */
208 1, /* num setup uint16_t words. */
209 0, /* max returned setup. */
210 state->param, /* param. */
211 talloc_get_size(state->param), /* num param. */
212 2, /* max returned param. */
213 data, /* data. */
214 data_len, /* num data. */
215 0); /* max returned data. */
217 if (tevent_req_nomem(subreq, req)) {
218 return tevent_req_post(req, ev);
220 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
221 return req;
224 static void cli_setpathinfo_done(struct tevent_req *subreq)
226 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
227 NULL, 0, NULL, NULL, 0, NULL);
228 tevent_req_simple_finish_ntstatus(subreq, status);
231 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
233 return tevent_req_simple_recv_ntstatus(req);
236 NTSTATUS cli_setpathinfo(struct cli_state *cli,
237 uint16_t level,
238 const char *path,
239 uint8_t *data,
240 size_t data_len)
242 TALLOC_CTX *frame = talloc_stackframe();
243 struct tevent_context *ev;
244 struct tevent_req *req;
245 NTSTATUS status = NT_STATUS_NO_MEMORY;
247 if (cli_has_async_calls(cli)) {
249 * Can't use sync call while an async call is in flight
251 status = NT_STATUS_INVALID_PARAMETER;
252 goto fail;
254 ev = tevent_context_init(frame);
255 if (ev == NULL) {
256 goto fail;
258 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
259 if (req == NULL) {
260 goto fail;
262 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
263 goto fail;
265 status = cli_setpathinfo_recv(req);
266 fail:
267 TALLOC_FREE(frame);
268 return status;
271 /****************************************************************************
272 Hard/Symlink a file (UNIX extensions).
273 Creates new name (sym)linked to oldname.
274 ****************************************************************************/
276 struct cli_posix_link_internal_state {
277 uint8_t *data;
280 static void cli_posix_link_internal_done(struct tevent_req *subreq);
282 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
283 struct event_context *ev,
284 struct cli_state *cli,
285 uint16_t level,
286 const char *oldname,
287 const char *newname)
289 struct tevent_req *req = NULL, *subreq = NULL;
290 struct cli_posix_link_internal_state *state = NULL;
292 req = tevent_req_create(mem_ctx, &state,
293 struct cli_posix_link_internal_state);
294 if (req == NULL) {
295 return NULL;
298 /* Setup data array. */
299 state->data = talloc_array(state, uint8_t, 0);
300 if (tevent_req_nomem(state->data, req)) {
301 return tevent_req_post(req, ev);
303 state->data = trans2_bytes_push_str(
304 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
306 subreq = cli_setpathinfo_send(
307 state, ev, cli, level, newname,
308 state->data, talloc_get_size(state->data));
309 if (tevent_req_nomem(subreq, req)) {
310 return tevent_req_post(req, ev);
312 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
313 return req;
316 static void cli_posix_link_internal_done(struct tevent_req *subreq)
318 NTSTATUS status = cli_setpathinfo_recv(subreq);
319 tevent_req_simple_finish_ntstatus(subreq, status);
322 /****************************************************************************
323 Symlink a file (UNIX extensions).
324 ****************************************************************************/
326 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
327 struct event_context *ev,
328 struct cli_state *cli,
329 const char *oldname,
330 const char *newname)
332 return cli_posix_link_internal_send(
333 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
336 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
338 return tevent_req_simple_recv_ntstatus(req);
341 NTSTATUS cli_posix_symlink(struct cli_state *cli,
342 const char *oldname,
343 const char *newname)
345 TALLOC_CTX *frame = talloc_stackframe();
346 struct event_context *ev = NULL;
347 struct tevent_req *req = NULL;
348 NTSTATUS status = NT_STATUS_OK;
350 if (cli_has_async_calls(cli)) {
352 * Can't use sync call while an async call is in flight
354 status = NT_STATUS_INVALID_PARAMETER;
355 goto fail;
358 ev = event_context_init(frame);
359 if (ev == NULL) {
360 status = NT_STATUS_NO_MEMORY;
361 goto fail;
364 req = cli_posix_symlink_send(frame,
366 cli,
367 oldname,
368 newname);
369 if (req == NULL) {
370 status = NT_STATUS_NO_MEMORY;
371 goto fail;
374 if (!tevent_req_poll(req, ev)) {
375 status = map_nt_error_from_unix(errno);
376 goto fail;
379 status = cli_posix_symlink_recv(req);
381 fail:
382 TALLOC_FREE(frame);
383 return status;
386 /****************************************************************************
387 Read a POSIX symlink.
388 ****************************************************************************/
390 struct readlink_state {
391 uint8_t *data;
392 uint32_t num_data;
395 static void cli_posix_readlink_done(struct tevent_req *subreq);
397 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
398 struct event_context *ev,
399 struct cli_state *cli,
400 const char *fname,
401 size_t len)
403 struct tevent_req *req = NULL, *subreq = NULL;
404 struct readlink_state *state = NULL;
405 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
407 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
408 if (req == NULL) {
409 return NULL;
413 * Len is in bytes, we need it in UCS2 units.
415 if ((2*len < len) || (maxbytelen < len)) {
416 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
417 return tevent_req_post(req, ev);
420 subreq = cli_qpathinfo_send(state, ev, cli, fname,
421 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
422 if (tevent_req_nomem(subreq, req)) {
423 return tevent_req_post(req, ev);
425 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
426 return req;
429 static void cli_posix_readlink_done(struct tevent_req *subreq)
431 struct tevent_req *req = tevent_req_callback_data(
432 subreq, struct tevent_req);
433 struct readlink_state *state = tevent_req_data(
434 req, struct readlink_state);
435 NTSTATUS status;
437 status = cli_qpathinfo_recv(subreq, state, &state->data,
438 &state->num_data);
439 TALLOC_FREE(subreq);
440 if (tevent_req_nterror(req, status)) {
441 return;
444 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
446 if (state->data[state->num_data-1] != '\0') {
447 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
448 return;
450 tevent_req_done(req);
453 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
454 char *retpath, size_t len)
456 NTSTATUS status;
457 char *converted = NULL;
458 size_t converted_size = 0;
459 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
461 if (tevent_req_is_nterror(req, &status)) {
462 return status;
464 /* The returned data is a pushed string, not raw data. */
465 if (!convert_string_talloc(state,
466 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
467 CH_UNIX,
468 state->data,
469 state->num_data,
470 &converted,
471 &converted_size)) {
472 return NT_STATUS_NO_MEMORY;
475 len = MIN(len,converted_size);
476 if (len == 0) {
477 return NT_STATUS_DATA_ERROR;
479 memcpy(retpath, converted, len);
480 return NT_STATUS_OK;
483 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
484 char *linkpath, size_t len)
486 TALLOC_CTX *frame = talloc_stackframe();
487 struct event_context *ev = NULL;
488 struct tevent_req *req = NULL;
489 NTSTATUS status = NT_STATUS_OK;
491 if (cli_has_async_calls(cli)) {
493 * Can't use sync call while an async call is in flight
495 status = NT_STATUS_INVALID_PARAMETER;
496 goto fail;
499 ev = event_context_init(frame);
500 if (ev == NULL) {
501 status = NT_STATUS_NO_MEMORY;
502 goto fail;
505 req = cli_posix_readlink_send(frame,
507 cli,
508 fname,
509 len);
510 if (req == NULL) {
511 status = NT_STATUS_NO_MEMORY;
512 goto fail;
515 if (!tevent_req_poll(req, ev)) {
516 status = map_nt_error_from_unix(errno);
517 goto fail;
520 status = cli_posix_readlink_recv(req, cli, linkpath, len);
522 fail:
523 TALLOC_FREE(frame);
524 return status;
527 /****************************************************************************
528 Hard link a file (UNIX extensions).
529 ****************************************************************************/
531 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
532 struct event_context *ev,
533 struct cli_state *cli,
534 const char *oldname,
535 const char *newname)
537 return cli_posix_link_internal_send(
538 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
541 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
543 return tevent_req_simple_recv_ntstatus(req);
546 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
547 const char *oldname,
548 const char *newname)
550 TALLOC_CTX *frame = talloc_stackframe();
551 struct event_context *ev = NULL;
552 struct tevent_req *req = NULL;
553 NTSTATUS status = NT_STATUS_OK;
555 if (cli_has_async_calls(cli)) {
557 * Can't use sync call while an async call is in flight
559 status = NT_STATUS_INVALID_PARAMETER;
560 goto fail;
563 ev = event_context_init(frame);
564 if (ev == NULL) {
565 status = NT_STATUS_NO_MEMORY;
566 goto fail;
569 req = cli_posix_hardlink_send(frame,
571 cli,
572 oldname,
573 newname);
574 if (req == NULL) {
575 status = NT_STATUS_NO_MEMORY;
576 goto fail;
579 if (!tevent_req_poll(req, ev)) {
580 status = map_nt_error_from_unix(errno);
581 goto fail;
584 status = cli_posix_hardlink_recv(req);
586 fail:
587 TALLOC_FREE(frame);
588 return status;
591 /****************************************************************************
592 Do a POSIX getfacl (UNIX extensions).
593 ****************************************************************************/
595 struct getfacl_state {
596 uint32_t num_data;
597 uint8_t *data;
600 static void cli_posix_getfacl_done(struct tevent_req *subreq);
602 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
603 struct event_context *ev,
604 struct cli_state *cli,
605 const char *fname)
607 struct tevent_req *req = NULL, *subreq = NULL;
608 struct getfacl_state *state = NULL;
610 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
611 if (req == NULL) {
612 return NULL;
614 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
615 0, cli->max_xmit);
616 if (tevent_req_nomem(subreq, req)) {
617 return tevent_req_post(req, ev);
619 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
620 return req;
623 static void cli_posix_getfacl_done(struct tevent_req *subreq)
625 struct tevent_req *req = tevent_req_callback_data(
626 subreq, struct tevent_req);
627 struct getfacl_state *state = tevent_req_data(
628 req, struct getfacl_state);
629 NTSTATUS status;
631 status = cli_qpathinfo_recv(subreq, state, &state->data,
632 &state->num_data);
633 TALLOC_FREE(subreq);
634 if (tevent_req_nterror(req, status)) {
635 return;
637 tevent_req_done(req);
640 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
641 TALLOC_CTX *mem_ctx,
642 size_t *prb_size,
643 char **retbuf)
645 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
646 NTSTATUS status;
648 if (tevent_req_is_nterror(req, &status)) {
649 return status;
651 *prb_size = (size_t)state->num_data;
652 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
653 return NT_STATUS_OK;
656 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
657 const char *fname,
658 TALLOC_CTX *mem_ctx,
659 size_t *prb_size,
660 char **retbuf)
662 TALLOC_CTX *frame = talloc_stackframe();
663 struct event_context *ev = NULL;
664 struct tevent_req *req = NULL;
665 NTSTATUS status = NT_STATUS_OK;
667 if (cli_has_async_calls(cli)) {
669 * Can't use sync call while an async call is in flight
671 status = NT_STATUS_INVALID_PARAMETER;
672 goto fail;
675 ev = event_context_init(frame);
676 if (ev == NULL) {
677 status = NT_STATUS_NO_MEMORY;
678 goto fail;
681 req = cli_posix_getfacl_send(frame,
683 cli,
684 fname);
685 if (req == NULL) {
686 status = NT_STATUS_NO_MEMORY;
687 goto fail;
690 if (!tevent_req_poll(req, ev)) {
691 status = map_nt_error_from_unix(errno);
692 goto fail;
695 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
697 fail:
698 TALLOC_FREE(frame);
699 return status;
702 /****************************************************************************
703 Stat a file (UNIX extensions).
704 ****************************************************************************/
706 struct stat_state {
707 uint32_t num_data;
708 uint8_t *data;
711 static void cli_posix_stat_done(struct tevent_req *subreq);
713 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
714 struct event_context *ev,
715 struct cli_state *cli,
716 const char *fname)
718 struct tevent_req *req = NULL, *subreq = NULL;
719 struct stat_state *state = NULL;
721 req = tevent_req_create(mem_ctx, &state, struct stat_state);
722 if (req == NULL) {
723 return NULL;
725 subreq = cli_qpathinfo_send(state, ev, cli, fname,
726 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
727 if (tevent_req_nomem(subreq, req)) {
728 return tevent_req_post(req, ev);
730 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
731 return req;
734 static void cli_posix_stat_done(struct tevent_req *subreq)
736 struct tevent_req *req = tevent_req_callback_data(
737 subreq, struct tevent_req);
738 struct stat_state *state = tevent_req_data(req, struct stat_state);
739 NTSTATUS status;
741 status = cli_qpathinfo_recv(subreq, state, &state->data,
742 &state->num_data);
743 TALLOC_FREE(subreq);
744 if (tevent_req_nterror(req, status)) {
745 return;
747 tevent_req_done(req);
750 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
751 SMB_STRUCT_STAT *sbuf)
753 struct stat_state *state = tevent_req_data(req, struct stat_state);
754 NTSTATUS status;
756 if (tevent_req_is_nterror(req, &status)) {
757 return status;
760 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
761 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
762 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
763 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
764 #else
765 /* assume 512 byte blocks */
766 sbuf->st_ex_blocks /= 512;
767 #endif
768 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
769 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
770 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
772 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
773 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
774 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
775 #if defined(HAVE_MAKEDEV)
777 uint32_t dev_major = IVAL(state->data,60);
778 uint32_t dev_minor = IVAL(state->data,68);
779 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
781 #endif
782 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
783 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
784 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
786 return NT_STATUS_OK;
789 NTSTATUS cli_posix_stat(struct cli_state *cli,
790 const char *fname,
791 SMB_STRUCT_STAT *sbuf)
793 TALLOC_CTX *frame = talloc_stackframe();
794 struct event_context *ev = NULL;
795 struct tevent_req *req = NULL;
796 NTSTATUS status = NT_STATUS_OK;
798 if (cli_has_async_calls(cli)) {
800 * Can't use sync call while an async call is in flight
802 status = NT_STATUS_INVALID_PARAMETER;
803 goto fail;
806 ev = event_context_init(frame);
807 if (ev == NULL) {
808 status = NT_STATUS_NO_MEMORY;
809 goto fail;
812 req = cli_posix_stat_send(frame,
814 cli,
815 fname);
816 if (req == NULL) {
817 status = NT_STATUS_NO_MEMORY;
818 goto fail;
821 if (!tevent_req_poll(req, ev)) {
822 status = map_nt_error_from_unix(errno);
823 goto fail;
826 status = cli_posix_stat_recv(req, sbuf);
828 fail:
829 TALLOC_FREE(frame);
830 return status;
833 /****************************************************************************
834 Chmod or chown a file internal (UNIX extensions).
835 ****************************************************************************/
837 struct cli_posix_chown_chmod_internal_state {
838 uint8_t data[100];
841 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
843 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
844 struct event_context *ev,
845 struct cli_state *cli,
846 const char *fname,
847 uint32_t mode,
848 uint32_t uid,
849 uint32_t gid)
851 struct tevent_req *req = NULL, *subreq = NULL;
852 struct cli_posix_chown_chmod_internal_state *state = NULL;
854 req = tevent_req_create(mem_ctx, &state,
855 struct cli_posix_chown_chmod_internal_state);
856 if (req == NULL) {
857 return NULL;
860 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
861 memset(&state->data[40], '\0', 60);
862 SIVAL(state->data,40,uid);
863 SIVAL(state->data,48,gid);
864 SIVAL(state->data,84,mode);
866 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
867 fname, state->data, sizeof(state->data));
868 if (tevent_req_nomem(subreq, req)) {
869 return tevent_req_post(req, ev);
871 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
872 req);
873 return req;
876 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
878 NTSTATUS status = cli_setpathinfo_recv(subreq);
879 tevent_req_simple_finish_ntstatus(subreq, status);
882 /****************************************************************************
883 chmod a file (UNIX extensions).
884 ****************************************************************************/
886 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
887 struct event_context *ev,
888 struct cli_state *cli,
889 const char *fname,
890 mode_t mode)
892 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
893 fname,
894 unix_perms_to_wire(mode),
895 SMB_UID_NO_CHANGE,
896 SMB_GID_NO_CHANGE);
899 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
901 return tevent_req_simple_recv_ntstatus(req);
904 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
906 TALLOC_CTX *frame = talloc_stackframe();
907 struct event_context *ev = NULL;
908 struct tevent_req *req = NULL;
909 NTSTATUS status = NT_STATUS_OK;
911 if (cli_has_async_calls(cli)) {
913 * Can't use sync call while an async call is in flight
915 status = NT_STATUS_INVALID_PARAMETER;
916 goto fail;
919 ev = event_context_init(frame);
920 if (ev == NULL) {
921 status = NT_STATUS_NO_MEMORY;
922 goto fail;
925 req = cli_posix_chmod_send(frame,
927 cli,
928 fname,
929 mode);
930 if (req == NULL) {
931 status = NT_STATUS_NO_MEMORY;
932 goto fail;
935 if (!tevent_req_poll(req, ev)) {
936 status = map_nt_error_from_unix(errno);
937 goto fail;
940 status = cli_posix_chmod_recv(req);
942 fail:
943 TALLOC_FREE(frame);
944 return status;
947 /****************************************************************************
948 chown a file (UNIX extensions).
949 ****************************************************************************/
951 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
952 struct event_context *ev,
953 struct cli_state *cli,
954 const char *fname,
955 uid_t uid,
956 gid_t gid)
958 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
959 fname,
960 SMB_MODE_NO_CHANGE,
961 (uint32_t)uid,
962 (uint32_t)gid);
965 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
967 return tevent_req_simple_recv_ntstatus(req);
970 NTSTATUS cli_posix_chown(struct cli_state *cli,
971 const char *fname,
972 uid_t uid,
973 gid_t gid)
975 TALLOC_CTX *frame = talloc_stackframe();
976 struct event_context *ev = NULL;
977 struct tevent_req *req = NULL;
978 NTSTATUS status = NT_STATUS_OK;
980 if (cli_has_async_calls(cli)) {
982 * Can't use sync call while an async call is in flight
984 status = NT_STATUS_INVALID_PARAMETER;
985 goto fail;
988 ev = event_context_init(frame);
989 if (ev == NULL) {
990 status = NT_STATUS_NO_MEMORY;
991 goto fail;
994 req = cli_posix_chown_send(frame,
996 cli,
997 fname,
998 uid,
999 gid);
1000 if (req == NULL) {
1001 status = NT_STATUS_NO_MEMORY;
1002 goto fail;
1005 if (!tevent_req_poll(req, ev)) {
1006 status = map_nt_error_from_unix(errno);
1007 goto fail;
1010 status = cli_posix_chown_recv(req);
1012 fail:
1013 TALLOC_FREE(frame);
1014 return status;
1017 /****************************************************************************
1018 Rename a file.
1019 ****************************************************************************/
1021 static void cli_rename_done(struct tevent_req *subreq);
1023 struct cli_rename_state {
1024 uint16_t vwv[1];
1027 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1028 struct event_context *ev,
1029 struct cli_state *cli,
1030 const char *fname_src,
1031 const char *fname_dst)
1033 struct tevent_req *req = NULL, *subreq = NULL;
1034 struct cli_rename_state *state = NULL;
1035 uint8_t additional_flags = 0;
1036 uint8_t *bytes = NULL;
1038 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1039 if (req == NULL) {
1040 return NULL;
1043 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1045 bytes = talloc_array(state, uint8_t, 1);
1046 if (tevent_req_nomem(bytes, req)) {
1047 return tevent_req_post(req, ev);
1049 bytes[0] = 4;
1050 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1051 strlen(fname_src)+1, NULL);
1052 if (tevent_req_nomem(bytes, req)) {
1053 return tevent_req_post(req, ev);
1056 bytes = talloc_realloc(state, bytes, uint8_t,
1057 talloc_get_size(bytes)+1);
1058 if (tevent_req_nomem(bytes, req)) {
1059 return tevent_req_post(req, ev);
1062 bytes[talloc_get_size(bytes)-1] = 4;
1063 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1064 strlen(fname_dst)+1, NULL);
1065 if (tevent_req_nomem(bytes, req)) {
1066 return tevent_req_post(req, ev);
1069 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1070 1, state->vwv, talloc_get_size(bytes), bytes);
1071 if (tevent_req_nomem(subreq, req)) {
1072 return tevent_req_post(req, ev);
1074 tevent_req_set_callback(subreq, cli_rename_done, req);
1075 return req;
1078 static void cli_rename_done(struct tevent_req *subreq)
1080 struct tevent_req *req = tevent_req_callback_data(
1081 subreq, struct tevent_req);
1082 NTSTATUS status;
1084 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1085 TALLOC_FREE(subreq);
1086 if (tevent_req_nterror(req, status)) {
1087 return;
1089 tevent_req_done(req);
1092 NTSTATUS cli_rename_recv(struct tevent_req *req)
1094 return tevent_req_simple_recv_ntstatus(req);
1097 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1099 TALLOC_CTX *frame = talloc_stackframe();
1100 struct event_context *ev;
1101 struct tevent_req *req;
1102 NTSTATUS status = NT_STATUS_OK;
1104 if (cli_has_async_calls(cli)) {
1106 * Can't use sync call while an async call is in flight
1108 status = NT_STATUS_INVALID_PARAMETER;
1109 goto fail;
1112 ev = event_context_init(frame);
1113 if (ev == NULL) {
1114 status = NT_STATUS_NO_MEMORY;
1115 goto fail;
1118 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1119 if (req == NULL) {
1120 status = NT_STATUS_NO_MEMORY;
1121 goto fail;
1124 if (!tevent_req_poll(req, ev)) {
1125 status = map_nt_error_from_unix(errno);
1126 goto fail;
1129 status = cli_rename_recv(req);
1131 fail:
1132 TALLOC_FREE(frame);
1133 return status;
1136 /****************************************************************************
1137 NT Rename a file.
1138 ****************************************************************************/
1140 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1142 struct cli_ntrename_internal_state {
1143 uint16_t vwv[4];
1146 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1147 struct event_context *ev,
1148 struct cli_state *cli,
1149 const char *fname_src,
1150 const char *fname_dst,
1151 uint16_t rename_flag)
1153 struct tevent_req *req = NULL, *subreq = NULL;
1154 struct cli_ntrename_internal_state *state = NULL;
1155 uint8_t additional_flags = 0;
1156 uint8_t *bytes = NULL;
1158 req = tevent_req_create(mem_ctx, &state,
1159 struct cli_ntrename_internal_state);
1160 if (req == NULL) {
1161 return NULL;
1164 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1165 SSVAL(state->vwv+1, 0, rename_flag);
1167 bytes = talloc_array(state, uint8_t, 1);
1168 if (tevent_req_nomem(bytes, req)) {
1169 return tevent_req_post(req, ev);
1171 bytes[0] = 4;
1172 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1173 strlen(fname_src)+1, NULL);
1174 if (tevent_req_nomem(bytes, req)) {
1175 return tevent_req_post(req, ev);
1178 bytes = talloc_realloc(state, bytes, uint8_t,
1179 talloc_get_size(bytes)+1);
1180 if (tevent_req_nomem(bytes, req)) {
1181 return tevent_req_post(req, ev);
1184 bytes[talloc_get_size(bytes)-1] = 4;
1185 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1186 strlen(fname_dst)+1, NULL);
1187 if (tevent_req_nomem(bytes, req)) {
1188 return tevent_req_post(req, ev);
1191 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1192 4, state->vwv, talloc_get_size(bytes), bytes);
1193 if (tevent_req_nomem(subreq, req)) {
1194 return tevent_req_post(req, ev);
1196 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1197 return req;
1200 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1202 struct tevent_req *req = tevent_req_callback_data(
1203 subreq, struct tevent_req);
1204 NTSTATUS status;
1206 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1207 TALLOC_FREE(subreq);
1208 if (tevent_req_nterror(req, status)) {
1209 return;
1211 tevent_req_done(req);
1214 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1216 return tevent_req_simple_recv_ntstatus(req);
1219 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1220 struct event_context *ev,
1221 struct cli_state *cli,
1222 const char *fname_src,
1223 const char *fname_dst)
1225 return cli_ntrename_internal_send(mem_ctx,
1227 cli,
1228 fname_src,
1229 fname_dst,
1230 RENAME_FLAG_RENAME);
1233 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1235 return cli_ntrename_internal_recv(req);
1238 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1240 TALLOC_CTX *frame = talloc_stackframe();
1241 struct event_context *ev;
1242 struct tevent_req *req;
1243 NTSTATUS status = NT_STATUS_OK;
1245 if (cli_has_async_calls(cli)) {
1247 * Can't use sync call while an async call is in flight
1249 status = NT_STATUS_INVALID_PARAMETER;
1250 goto fail;
1253 ev = event_context_init(frame);
1254 if (ev == NULL) {
1255 status = NT_STATUS_NO_MEMORY;
1256 goto fail;
1259 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1260 if (req == NULL) {
1261 status = NT_STATUS_NO_MEMORY;
1262 goto fail;
1265 if (!tevent_req_poll(req, ev)) {
1266 status = map_nt_error_from_unix(errno);
1267 goto fail;
1270 status = cli_ntrename_recv(req);
1272 fail:
1273 TALLOC_FREE(frame);
1274 return status;
1277 /****************************************************************************
1278 NT hardlink a file.
1279 ****************************************************************************/
1281 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1282 struct event_context *ev,
1283 struct cli_state *cli,
1284 const char *fname_src,
1285 const char *fname_dst)
1287 return cli_ntrename_internal_send(mem_ctx,
1289 cli,
1290 fname_src,
1291 fname_dst,
1292 RENAME_FLAG_HARD_LINK);
1295 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1297 return cli_ntrename_internal_recv(req);
1300 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1302 TALLOC_CTX *frame = talloc_stackframe();
1303 struct event_context *ev;
1304 struct tevent_req *req;
1305 NTSTATUS status = NT_STATUS_OK;
1307 if (cli_has_async_calls(cli)) {
1309 * Can't use sync call while an async call is in flight
1311 status = NT_STATUS_INVALID_PARAMETER;
1312 goto fail;
1315 ev = event_context_init(frame);
1316 if (ev == NULL) {
1317 status = NT_STATUS_NO_MEMORY;
1318 goto fail;
1321 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1322 if (req == NULL) {
1323 status = NT_STATUS_NO_MEMORY;
1324 goto fail;
1327 if (!tevent_req_poll(req, ev)) {
1328 status = map_nt_error_from_unix(errno);
1329 goto fail;
1332 status = cli_nt_hardlink_recv(req);
1334 fail:
1335 TALLOC_FREE(frame);
1336 return status;
1339 /****************************************************************************
1340 Delete a file.
1341 ****************************************************************************/
1343 static void cli_unlink_done(struct tevent_req *subreq);
1345 struct cli_unlink_state {
1346 uint16_t vwv[1];
1349 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1350 struct event_context *ev,
1351 struct cli_state *cli,
1352 const char *fname,
1353 uint16_t mayhave_attrs)
1355 struct tevent_req *req = NULL, *subreq = NULL;
1356 struct cli_unlink_state *state = NULL;
1357 uint8_t additional_flags = 0;
1358 uint8_t *bytes = NULL;
1360 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1361 if (req == NULL) {
1362 return NULL;
1365 SSVAL(state->vwv+0, 0, mayhave_attrs);
1367 bytes = talloc_array(state, uint8_t, 1);
1368 if (tevent_req_nomem(bytes, req)) {
1369 return tevent_req_post(req, ev);
1371 bytes[0] = 4;
1372 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1373 strlen(fname)+1, NULL);
1375 if (tevent_req_nomem(bytes, req)) {
1376 return tevent_req_post(req, ev);
1379 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1380 1, state->vwv, talloc_get_size(bytes), bytes);
1381 if (tevent_req_nomem(subreq, req)) {
1382 return tevent_req_post(req, ev);
1384 tevent_req_set_callback(subreq, cli_unlink_done, req);
1385 return req;
1388 static void cli_unlink_done(struct tevent_req *subreq)
1390 struct tevent_req *req = tevent_req_callback_data(
1391 subreq, struct tevent_req);
1392 NTSTATUS status;
1394 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1395 TALLOC_FREE(subreq);
1396 if (tevent_req_nterror(req, status)) {
1397 return;
1399 tevent_req_done(req);
1402 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1404 return tevent_req_simple_recv_ntstatus(req);
1407 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1409 TALLOC_CTX *frame = talloc_stackframe();
1410 struct event_context *ev;
1411 struct tevent_req *req;
1412 NTSTATUS status = NT_STATUS_OK;
1414 if (cli_has_async_calls(cli)) {
1416 * Can't use sync call while an async call is in flight
1418 status = NT_STATUS_INVALID_PARAMETER;
1419 goto fail;
1422 ev = event_context_init(frame);
1423 if (ev == NULL) {
1424 status = NT_STATUS_NO_MEMORY;
1425 goto fail;
1428 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1429 if (req == NULL) {
1430 status = NT_STATUS_NO_MEMORY;
1431 goto fail;
1434 if (!tevent_req_poll(req, ev)) {
1435 status = map_nt_error_from_unix(errno);
1436 goto fail;
1439 status = cli_unlink_recv(req);
1441 fail:
1442 TALLOC_FREE(frame);
1443 return status;
1446 /****************************************************************************
1447 Create a directory.
1448 ****************************************************************************/
1450 static void cli_mkdir_done(struct tevent_req *subreq);
1452 struct cli_mkdir_state {
1453 int dummy;
1456 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1457 struct event_context *ev,
1458 struct cli_state *cli,
1459 const char *dname)
1461 struct tevent_req *req = NULL, *subreq = NULL;
1462 struct cli_mkdir_state *state = NULL;
1463 uint8_t additional_flags = 0;
1464 uint8_t *bytes = NULL;
1466 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1467 if (req == NULL) {
1468 return NULL;
1471 bytes = talloc_array(state, uint8_t, 1);
1472 if (tevent_req_nomem(bytes, req)) {
1473 return tevent_req_post(req, ev);
1475 bytes[0] = 4;
1476 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1477 strlen(dname)+1, NULL);
1479 if (tevent_req_nomem(bytes, req)) {
1480 return tevent_req_post(req, ev);
1483 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1484 0, NULL, talloc_get_size(bytes), bytes);
1485 if (tevent_req_nomem(subreq, req)) {
1486 return tevent_req_post(req, ev);
1488 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1489 return req;
1492 static void cli_mkdir_done(struct tevent_req *subreq)
1494 struct tevent_req *req = tevent_req_callback_data(
1495 subreq, struct tevent_req);
1496 NTSTATUS status;
1498 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1499 TALLOC_FREE(subreq);
1500 if (tevent_req_nterror(req, status)) {
1501 return;
1503 tevent_req_done(req);
1506 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1508 return tevent_req_simple_recv_ntstatus(req);
1511 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1513 TALLOC_CTX *frame = talloc_stackframe();
1514 struct event_context *ev;
1515 struct tevent_req *req;
1516 NTSTATUS status = NT_STATUS_OK;
1518 if (cli_has_async_calls(cli)) {
1520 * Can't use sync call while an async call is in flight
1522 status = NT_STATUS_INVALID_PARAMETER;
1523 goto fail;
1526 ev = event_context_init(frame);
1527 if (ev == NULL) {
1528 status = NT_STATUS_NO_MEMORY;
1529 goto fail;
1532 req = cli_mkdir_send(frame, ev, cli, dname);
1533 if (req == NULL) {
1534 status = NT_STATUS_NO_MEMORY;
1535 goto fail;
1538 if (!tevent_req_poll(req, ev)) {
1539 status = map_nt_error_from_unix(errno);
1540 goto fail;
1543 status = cli_mkdir_recv(req);
1545 fail:
1546 TALLOC_FREE(frame);
1547 return status;
1550 /****************************************************************************
1551 Remove a directory.
1552 ****************************************************************************/
1554 static void cli_rmdir_done(struct tevent_req *subreq);
1556 struct cli_rmdir_state {
1557 int dummy;
1560 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1561 struct event_context *ev,
1562 struct cli_state *cli,
1563 const char *dname)
1565 struct tevent_req *req = NULL, *subreq = NULL;
1566 struct cli_rmdir_state *state = NULL;
1567 uint8_t additional_flags = 0;
1568 uint8_t *bytes = NULL;
1570 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1571 if (req == NULL) {
1572 return NULL;
1575 bytes = talloc_array(state, uint8_t, 1);
1576 if (tevent_req_nomem(bytes, req)) {
1577 return tevent_req_post(req, ev);
1579 bytes[0] = 4;
1580 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1581 strlen(dname)+1, NULL);
1583 if (tevent_req_nomem(bytes, req)) {
1584 return tevent_req_post(req, ev);
1587 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1588 0, NULL, talloc_get_size(bytes), bytes);
1589 if (tevent_req_nomem(subreq, req)) {
1590 return tevent_req_post(req, ev);
1592 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1593 return req;
1596 static void cli_rmdir_done(struct tevent_req *subreq)
1598 struct tevent_req *req = tevent_req_callback_data(
1599 subreq, struct tevent_req);
1600 NTSTATUS status;
1602 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1603 TALLOC_FREE(subreq);
1604 if (tevent_req_nterror(req, status)) {
1605 return;
1607 tevent_req_done(req);
1610 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1612 return tevent_req_simple_recv_ntstatus(req);
1615 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1617 TALLOC_CTX *frame = talloc_stackframe();
1618 struct event_context *ev;
1619 struct tevent_req *req;
1620 NTSTATUS status = NT_STATUS_OK;
1622 if (cli_has_async_calls(cli)) {
1624 * Can't use sync call while an async call is in flight
1626 status = NT_STATUS_INVALID_PARAMETER;
1627 goto fail;
1630 ev = event_context_init(frame);
1631 if (ev == NULL) {
1632 status = NT_STATUS_NO_MEMORY;
1633 goto fail;
1636 req = cli_rmdir_send(frame, ev, cli, dname);
1637 if (req == NULL) {
1638 status = NT_STATUS_NO_MEMORY;
1639 goto fail;
1642 if (!tevent_req_poll(req, ev)) {
1643 status = map_nt_error_from_unix(errno);
1644 goto fail;
1647 status = cli_rmdir_recv(req);
1649 fail:
1650 TALLOC_FREE(frame);
1651 return status;
1654 /****************************************************************************
1655 Set or clear the delete on close flag.
1656 ****************************************************************************/
1658 struct doc_state {
1659 uint16_t setup;
1660 uint8_t param[6];
1661 uint8_t data[1];
1664 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1666 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1667 NULL, 0, NULL, NULL, 0, NULL);
1668 tevent_req_simple_finish_ntstatus(subreq, status);
1671 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1672 struct event_context *ev,
1673 struct cli_state *cli,
1674 uint16_t fnum,
1675 bool flag)
1677 struct tevent_req *req = NULL, *subreq = NULL;
1678 struct doc_state *state = NULL;
1680 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1681 if (req == NULL) {
1682 return NULL;
1685 /* Setup setup word. */
1686 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1688 /* Setup param array. */
1689 SSVAL(state->param,0,fnum);
1690 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1692 /* Setup data array. */
1693 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1695 subreq = cli_trans_send(state, /* mem ctx. */
1696 ev, /* event ctx. */
1697 cli, /* cli_state. */
1698 SMBtrans2, /* cmd. */
1699 NULL, /* pipe name. */
1700 -1, /* fid. */
1701 0, /* function. */
1702 0, /* flags. */
1703 &state->setup, /* setup. */
1704 1, /* num setup uint16_t words. */
1705 0, /* max returned setup. */
1706 state->param, /* param. */
1707 6, /* num param. */
1708 2, /* max returned param. */
1709 state->data, /* data. */
1710 1, /* num data. */
1711 0); /* max returned data. */
1713 if (tevent_req_nomem(subreq, req)) {
1714 return tevent_req_post(req, ev);
1716 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1717 return req;
1720 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1722 return tevent_req_simple_recv_ntstatus(req);
1725 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1727 TALLOC_CTX *frame = talloc_stackframe();
1728 struct event_context *ev = NULL;
1729 struct tevent_req *req = NULL;
1730 NTSTATUS status = NT_STATUS_OK;
1732 if (cli_has_async_calls(cli)) {
1734 * Can't use sync call while an async call is in flight
1736 status = NT_STATUS_INVALID_PARAMETER;
1737 goto fail;
1740 ev = event_context_init(frame);
1741 if (ev == NULL) {
1742 status = NT_STATUS_NO_MEMORY;
1743 goto fail;
1746 req = cli_nt_delete_on_close_send(frame,
1748 cli,
1749 fnum,
1750 flag);
1751 if (req == NULL) {
1752 status = NT_STATUS_NO_MEMORY;
1753 goto fail;
1756 if (!tevent_req_poll(req, ev)) {
1757 status = map_nt_error_from_unix(errno);
1758 goto fail;
1761 status = cli_nt_delete_on_close_recv(req);
1763 fail:
1764 TALLOC_FREE(frame);
1765 return status;
1768 struct cli_ntcreate_state {
1769 uint16_t vwv[24];
1770 uint16_t fnum;
1773 static void cli_ntcreate_done(struct tevent_req *subreq);
1775 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1776 struct event_context *ev,
1777 struct cli_state *cli,
1778 const char *fname,
1779 uint32_t CreatFlags,
1780 uint32_t DesiredAccess,
1781 uint32_t FileAttributes,
1782 uint32_t ShareAccess,
1783 uint32_t CreateDisposition,
1784 uint32_t CreateOptions,
1785 uint8_t SecurityFlags)
1787 struct tevent_req *req, *subreq;
1788 struct cli_ntcreate_state *state;
1789 uint16_t *vwv;
1790 uint8_t *bytes;
1791 size_t converted_len;
1793 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1794 if (req == NULL) {
1795 return NULL;
1798 vwv = state->vwv;
1800 SCVAL(vwv+0, 0, 0xFF);
1801 SCVAL(vwv+0, 1, 0);
1802 SSVAL(vwv+1, 0, 0);
1803 SCVAL(vwv+2, 0, 0);
1805 if (cli->use_oplocks) {
1806 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1808 SIVAL(vwv+3, 1, CreatFlags);
1809 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1810 SIVAL(vwv+7, 1, DesiredAccess);
1811 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1812 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1813 SIVAL(vwv+13, 1, FileAttributes);
1814 SIVAL(vwv+15, 1, ShareAccess);
1815 SIVAL(vwv+17, 1, CreateDisposition);
1816 SIVAL(vwv+19, 1, CreateOptions);
1817 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1818 SCVAL(vwv+23, 1, SecurityFlags);
1820 bytes = talloc_array(state, uint8_t, 0);
1821 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1822 fname, strlen(fname)+1,
1823 &converted_len);
1825 /* sigh. this copes with broken netapp filer behaviour */
1826 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1828 if (tevent_req_nomem(bytes, req)) {
1829 return tevent_req_post(req, ev);
1832 SSVAL(vwv+2, 1, converted_len);
1834 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1835 talloc_get_size(bytes), bytes);
1836 if (tevent_req_nomem(subreq, req)) {
1837 return tevent_req_post(req, ev);
1839 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1840 return req;
1843 static void cli_ntcreate_done(struct tevent_req *subreq)
1845 struct tevent_req *req = tevent_req_callback_data(
1846 subreq, struct tevent_req);
1847 struct cli_ntcreate_state *state = tevent_req_data(
1848 req, struct cli_ntcreate_state);
1849 uint8_t wct;
1850 uint16_t *vwv;
1851 uint32_t num_bytes;
1852 uint8_t *bytes;
1853 uint8_t *inbuf;
1854 NTSTATUS status;
1856 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1857 &num_bytes, &bytes);
1858 TALLOC_FREE(subreq);
1859 if (tevent_req_nterror(req, status)) {
1860 return;
1862 state->fnum = SVAL(vwv+2, 1);
1863 tevent_req_done(req);
1866 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1868 struct cli_ntcreate_state *state = tevent_req_data(
1869 req, struct cli_ntcreate_state);
1870 NTSTATUS status;
1872 if (tevent_req_is_nterror(req, &status)) {
1873 return status;
1875 *pfnum = state->fnum;
1876 return NT_STATUS_OK;
1879 NTSTATUS cli_ntcreate(struct cli_state *cli,
1880 const char *fname,
1881 uint32_t CreatFlags,
1882 uint32_t DesiredAccess,
1883 uint32_t FileAttributes,
1884 uint32_t ShareAccess,
1885 uint32_t CreateDisposition,
1886 uint32_t CreateOptions,
1887 uint8_t SecurityFlags,
1888 uint16_t *pfid)
1890 TALLOC_CTX *frame = talloc_stackframe();
1891 struct event_context *ev;
1892 struct tevent_req *req;
1893 NTSTATUS status = NT_STATUS_OK;
1895 if (cli_has_async_calls(cli)) {
1897 * Can't use sync call while an async call is in flight
1899 status = NT_STATUS_INVALID_PARAMETER;
1900 goto fail;
1903 ev = event_context_init(frame);
1904 if (ev == NULL) {
1905 status = NT_STATUS_NO_MEMORY;
1906 goto fail;
1909 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1910 DesiredAccess, FileAttributes, ShareAccess,
1911 CreateDisposition, CreateOptions,
1912 SecurityFlags);
1913 if (req == NULL) {
1914 status = NT_STATUS_NO_MEMORY;
1915 goto fail;
1918 if (!tevent_req_poll(req, ev)) {
1919 status = map_nt_error_from_unix(errno);
1920 goto fail;
1923 status = cli_ntcreate_recv(req, pfid);
1924 fail:
1925 TALLOC_FREE(frame);
1926 return status;
1929 struct cli_nttrans_create_state {
1930 uint16_t fnum;
1933 static void cli_nttrans_create_done(struct tevent_req *subreq);
1935 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
1936 struct event_context *ev,
1937 struct cli_state *cli,
1938 const char *fname,
1939 uint32_t CreatFlags,
1940 uint32_t DesiredAccess,
1941 uint32_t FileAttributes,
1942 uint32_t ShareAccess,
1943 uint32_t CreateDisposition,
1944 uint32_t CreateOptions,
1945 uint8_t SecurityFlags,
1946 struct security_descriptor *secdesc,
1947 struct ea_struct *eas,
1948 int num_eas)
1950 struct tevent_req *req, *subreq;
1951 struct cli_nttrans_create_state *state;
1952 uint8_t *param;
1953 uint8_t *secdesc_buf;
1954 size_t secdesc_len;
1955 NTSTATUS status;
1956 size_t converted_len;
1958 req = tevent_req_create(mem_ctx,
1959 &state, struct cli_nttrans_create_state);
1960 if (req == NULL) {
1961 return NULL;
1964 if (secdesc != NULL) {
1965 status = marshall_sec_desc(talloc_tos(), secdesc,
1966 &secdesc_buf, &secdesc_len);
1967 if (tevent_req_nterror(req, status)) {
1968 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1969 nt_errstr(status)));
1970 return tevent_req_post(req, ev);
1972 } else {
1973 secdesc_buf = NULL;
1974 secdesc_len = 0;
1977 if (num_eas != 0) {
1979 * TODO ;-)
1981 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
1982 return tevent_req_post(req, ev);
1985 param = talloc_array(state, uint8_t, 53);
1986 if (tevent_req_nomem(param, req)) {
1987 return tevent_req_post(req, ev);
1990 param = trans2_bytes_push_str(param, cli_ucs2(cli),
1991 fname, strlen(fname),
1992 &converted_len);
1993 if (tevent_req_nomem(param, req)) {
1994 return tevent_req_post(req, ev);
1997 SIVAL(param, 0, CreatFlags);
1998 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
1999 SIVAL(param, 8, DesiredAccess);
2000 SIVAL(param, 12, 0x0); /* AllocationSize */
2001 SIVAL(param, 16, 0x0); /* AllocationSize */
2002 SIVAL(param, 20, FileAttributes);
2003 SIVAL(param, 24, ShareAccess);
2004 SIVAL(param, 28, CreateDisposition);
2005 SIVAL(param, 32, CreateOptions);
2006 SIVAL(param, 36, secdesc_len);
2007 SIVAL(param, 40, 0); /* EA length*/
2008 SIVAL(param, 44, converted_len);
2009 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2010 SCVAL(param, 52, SecurityFlags);
2012 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2013 NULL, -1, /* name, fid */
2014 NT_TRANSACT_CREATE, 0,
2015 NULL, 0, 0, /* setup */
2016 param, talloc_get_size(param), 128, /* param */
2017 secdesc_buf, secdesc_len, 0); /* data */
2018 if (tevent_req_nomem(subreq, req)) {
2019 return tevent_req_post(req, ev);
2021 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2022 return req;
2025 static void cli_nttrans_create_done(struct tevent_req *subreq)
2027 struct tevent_req *req = tevent_req_callback_data(
2028 subreq, struct tevent_req);
2029 struct cli_nttrans_create_state *state = tevent_req_data(
2030 req, struct cli_nttrans_create_state);
2031 uint8_t *param;
2032 uint32_t num_param;
2033 NTSTATUS status;
2035 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2036 NULL, 0, NULL, /* rsetup */
2037 &param, 69, &num_param,
2038 NULL, 0, NULL);
2039 if (tevent_req_nterror(req, status)) {
2040 return;
2042 state->fnum = SVAL(param, 2);
2043 TALLOC_FREE(param);
2044 tevent_req_done(req);
2047 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
2049 struct cli_nttrans_create_state *state = tevent_req_data(
2050 req, struct cli_nttrans_create_state);
2051 NTSTATUS status;
2053 if (tevent_req_is_nterror(req, &status)) {
2054 return status;
2056 *fnum = state->fnum;
2057 return NT_STATUS_OK;
2060 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2061 const char *fname,
2062 uint32_t CreatFlags,
2063 uint32_t DesiredAccess,
2064 uint32_t FileAttributes,
2065 uint32_t ShareAccess,
2066 uint32_t CreateDisposition,
2067 uint32_t CreateOptions,
2068 uint8_t SecurityFlags,
2069 struct security_descriptor *secdesc,
2070 struct ea_struct *eas,
2071 int num_eas,
2072 uint16_t *pfid)
2074 TALLOC_CTX *frame = talloc_stackframe();
2075 struct event_context *ev;
2076 struct tevent_req *req;
2077 NTSTATUS status = NT_STATUS_NO_MEMORY;
2079 if (cli_has_async_calls(cli)) {
2081 * Can't use sync call while an async call is in flight
2083 status = NT_STATUS_INVALID_PARAMETER;
2084 goto fail;
2086 ev = event_context_init(frame);
2087 if (ev == NULL) {
2088 goto fail;
2090 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2091 DesiredAccess, FileAttributes,
2092 ShareAccess, CreateDisposition,
2093 CreateOptions, SecurityFlags,
2094 secdesc, eas, num_eas);
2095 if (req == NULL) {
2096 goto fail;
2098 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2099 goto fail;
2101 status = cli_nttrans_create_recv(req, pfid);
2102 fail:
2103 TALLOC_FREE(frame);
2104 return status;
2107 /****************************************************************************
2108 Open a file
2109 WARNING: if you open with O_WRONLY then getattrE won't work!
2110 ****************************************************************************/
2112 struct cli_open_state {
2113 uint16_t vwv[15];
2114 uint16_t fnum;
2115 struct iovec bytes;
2118 static void cli_open_done(struct tevent_req *subreq);
2120 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2121 struct event_context *ev,
2122 struct cli_state *cli, const char *fname,
2123 int flags, int share_mode,
2124 struct tevent_req **psmbreq)
2126 struct tevent_req *req, *subreq;
2127 struct cli_open_state *state;
2128 unsigned openfn;
2129 unsigned accessmode;
2130 uint8_t additional_flags;
2131 uint8_t *bytes;
2133 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2134 if (req == NULL) {
2135 return NULL;
2138 openfn = 0;
2139 if (flags & O_CREAT) {
2140 openfn |= (1<<4);
2142 if (!(flags & O_EXCL)) {
2143 if (flags & O_TRUNC)
2144 openfn |= (1<<1);
2145 else
2146 openfn |= (1<<0);
2149 accessmode = (share_mode<<4);
2151 if ((flags & O_ACCMODE) == O_RDWR) {
2152 accessmode |= 2;
2153 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2154 accessmode |= 1;
2157 #if defined(O_SYNC)
2158 if ((flags & O_SYNC) == O_SYNC) {
2159 accessmode |= (1<<14);
2161 #endif /* O_SYNC */
2163 if (share_mode == DENY_FCB) {
2164 accessmode = 0xFF;
2167 SCVAL(state->vwv + 0, 0, 0xFF);
2168 SCVAL(state->vwv + 0, 1, 0);
2169 SSVAL(state->vwv + 1, 0, 0);
2170 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2171 SSVAL(state->vwv + 3, 0, accessmode);
2172 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2173 SSVAL(state->vwv + 5, 0, 0);
2174 SIVAL(state->vwv + 6, 0, 0);
2175 SSVAL(state->vwv + 8, 0, openfn);
2176 SIVAL(state->vwv + 9, 0, 0);
2177 SIVAL(state->vwv + 11, 0, 0);
2178 SIVAL(state->vwv + 13, 0, 0);
2180 additional_flags = 0;
2182 if (cli->use_oplocks) {
2183 /* if using oplocks then ask for a batch oplock via
2184 core and extended methods */
2185 additional_flags =
2186 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2187 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2190 bytes = talloc_array(state, uint8_t, 0);
2191 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2192 strlen(fname)+1, NULL);
2194 if (tevent_req_nomem(bytes, req)) {
2195 return tevent_req_post(req, ev);
2198 state->bytes.iov_base = (void *)bytes;
2199 state->bytes.iov_len = talloc_get_size(bytes);
2201 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2202 15, state->vwv, 1, &state->bytes);
2203 if (subreq == NULL) {
2204 TALLOC_FREE(req);
2205 return NULL;
2207 tevent_req_set_callback(subreq, cli_open_done, req);
2208 *psmbreq = subreq;
2209 return req;
2212 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2213 struct cli_state *cli, const char *fname,
2214 int flags, int share_mode)
2216 struct tevent_req *req, *subreq;
2217 NTSTATUS status;
2219 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2220 &subreq);
2221 if (req == NULL) {
2222 return NULL;
2225 status = cli_smb_req_send(subreq);
2226 if (tevent_req_nterror(req, status)) {
2227 return tevent_req_post(req, ev);
2229 return req;
2232 static void cli_open_done(struct tevent_req *subreq)
2234 struct tevent_req *req = tevent_req_callback_data(
2235 subreq, struct tevent_req);
2236 struct cli_open_state *state = tevent_req_data(
2237 req, struct cli_open_state);
2238 uint8_t wct;
2239 uint16_t *vwv;
2240 uint8_t *inbuf;
2241 NTSTATUS status;
2243 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2244 NULL);
2245 TALLOC_FREE(subreq);
2246 if (tevent_req_nterror(req, status)) {
2247 return;
2249 state->fnum = SVAL(vwv+2, 0);
2250 tevent_req_done(req);
2253 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2255 struct cli_open_state *state = tevent_req_data(
2256 req, struct cli_open_state);
2257 NTSTATUS status;
2259 if (tevent_req_is_nterror(req, &status)) {
2260 return status;
2262 *pfnum = state->fnum;
2263 return NT_STATUS_OK;
2266 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2267 int share_mode, uint16_t *pfnum)
2269 TALLOC_CTX *frame = talloc_stackframe();
2270 struct event_context *ev;
2271 struct tevent_req *req;
2272 NTSTATUS status = NT_STATUS_OK;
2274 if (cli_has_async_calls(cli)) {
2276 * Can't use sync call while an async call is in flight
2278 status = NT_STATUS_INVALID_PARAMETER;
2279 goto fail;
2282 ev = event_context_init(frame);
2283 if (ev == NULL) {
2284 status = NT_STATUS_NO_MEMORY;
2285 goto fail;
2288 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2289 if (req == NULL) {
2290 status = NT_STATUS_NO_MEMORY;
2291 goto fail;
2294 if (!tevent_req_poll(req, ev)) {
2295 status = map_nt_error_from_unix(errno);
2296 goto fail;
2299 status = cli_open_recv(req, pfnum);
2300 fail:
2301 TALLOC_FREE(frame);
2302 return status;
2305 /****************************************************************************
2306 Close a file.
2307 ****************************************************************************/
2309 struct cli_close_state {
2310 uint16_t vwv[3];
2313 static void cli_close_done(struct tevent_req *subreq);
2315 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2316 struct event_context *ev,
2317 struct cli_state *cli,
2318 uint16_t fnum,
2319 struct tevent_req **psubreq)
2321 struct tevent_req *req, *subreq;
2322 struct cli_close_state *state;
2324 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2325 if (req == NULL) {
2326 return NULL;
2329 SSVAL(state->vwv+0, 0, fnum);
2330 SIVALS(state->vwv+1, 0, -1);
2332 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2333 0, NULL);
2334 if (subreq == NULL) {
2335 TALLOC_FREE(req);
2336 return NULL;
2338 tevent_req_set_callback(subreq, cli_close_done, req);
2339 *psubreq = subreq;
2340 return req;
2343 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2344 struct event_context *ev,
2345 struct cli_state *cli,
2346 uint16_t fnum)
2348 struct tevent_req *req, *subreq;
2349 NTSTATUS status;
2351 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2352 if (req == NULL) {
2353 return NULL;
2356 status = cli_smb_req_send(subreq);
2357 if (tevent_req_nterror(req, status)) {
2358 return tevent_req_post(req, ev);
2360 return req;
2363 static void cli_close_done(struct tevent_req *subreq)
2365 struct tevent_req *req = tevent_req_callback_data(
2366 subreq, struct tevent_req);
2367 NTSTATUS status;
2369 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2370 TALLOC_FREE(subreq);
2371 if (tevent_req_nterror(req, status)) {
2372 return;
2374 tevent_req_done(req);
2377 NTSTATUS cli_close_recv(struct tevent_req *req)
2379 return tevent_req_simple_recv_ntstatus(req);
2382 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2384 TALLOC_CTX *frame = talloc_stackframe();
2385 struct event_context *ev;
2386 struct tevent_req *req;
2387 NTSTATUS status = NT_STATUS_OK;
2389 if (cli_has_async_calls(cli)) {
2391 * Can't use sync call while an async call is in flight
2393 status = NT_STATUS_INVALID_PARAMETER;
2394 goto fail;
2397 ev = event_context_init(frame);
2398 if (ev == NULL) {
2399 status = NT_STATUS_NO_MEMORY;
2400 goto fail;
2403 req = cli_close_send(frame, ev, cli, fnum);
2404 if (req == NULL) {
2405 status = NT_STATUS_NO_MEMORY;
2406 goto fail;
2409 if (!tevent_req_poll(req, ev)) {
2410 status = map_nt_error_from_unix(errno);
2411 goto fail;
2414 status = cli_close_recv(req);
2415 fail:
2416 TALLOC_FREE(frame);
2417 return status;
2420 /****************************************************************************
2421 Truncate a file to a specified size
2422 ****************************************************************************/
2424 struct ftrunc_state {
2425 uint16_t setup;
2426 uint8_t param[6];
2427 uint8_t data[8];
2430 static void cli_ftruncate_done(struct tevent_req *subreq)
2432 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2433 NULL, 0, NULL, NULL, 0, NULL);
2434 tevent_req_simple_finish_ntstatus(subreq, status);
2437 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2438 struct event_context *ev,
2439 struct cli_state *cli,
2440 uint16_t fnum,
2441 uint64_t size)
2443 struct tevent_req *req = NULL, *subreq = NULL;
2444 struct ftrunc_state *state = NULL;
2446 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2447 if (req == NULL) {
2448 return NULL;
2451 /* Setup setup word. */
2452 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2454 /* Setup param array. */
2455 SSVAL(state->param,0,fnum);
2456 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2457 SSVAL(state->param,4,0);
2459 /* Setup data array. */
2460 SBVAL(state->data, 0, size);
2462 subreq = cli_trans_send(state, /* mem ctx. */
2463 ev, /* event ctx. */
2464 cli, /* cli_state. */
2465 SMBtrans2, /* cmd. */
2466 NULL, /* pipe name. */
2467 -1, /* fid. */
2468 0, /* function. */
2469 0, /* flags. */
2470 &state->setup, /* setup. */
2471 1, /* num setup uint16_t words. */
2472 0, /* max returned setup. */
2473 state->param, /* param. */
2474 6, /* num param. */
2475 2, /* max returned param. */
2476 state->data, /* data. */
2477 8, /* num data. */
2478 0); /* max returned data. */
2480 if (tevent_req_nomem(subreq, req)) {
2481 return tevent_req_post(req, ev);
2483 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2484 return req;
2487 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2489 return tevent_req_simple_recv_ntstatus(req);
2492 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2494 TALLOC_CTX *frame = talloc_stackframe();
2495 struct event_context *ev = NULL;
2496 struct tevent_req *req = NULL;
2497 NTSTATUS status = NT_STATUS_OK;
2499 if (cli_has_async_calls(cli)) {
2501 * Can't use sync call while an async call is in flight
2503 status = NT_STATUS_INVALID_PARAMETER;
2504 goto fail;
2507 ev = event_context_init(frame);
2508 if (ev == NULL) {
2509 status = NT_STATUS_NO_MEMORY;
2510 goto fail;
2513 req = cli_ftruncate_send(frame,
2515 cli,
2516 fnum,
2517 size);
2518 if (req == NULL) {
2519 status = NT_STATUS_NO_MEMORY;
2520 goto fail;
2523 if (!tevent_req_poll(req, ev)) {
2524 status = map_nt_error_from_unix(errno);
2525 goto fail;
2528 status = cli_ftruncate_recv(req);
2530 fail:
2531 TALLOC_FREE(frame);
2532 return status;
2535 /****************************************************************************
2536 send a lock with a specified locktype
2537 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2538 ****************************************************************************/
2540 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2541 uint32_t offset, uint32_t len,
2542 int timeout, unsigned char locktype)
2544 uint16_t vwv[8];
2545 uint8_t bytes[10];
2546 NTSTATUS status;
2547 int saved_timeout;
2549 SCVAL(vwv + 0, 0, 0xff);
2550 SCVAL(vwv + 0, 1, 0);
2551 SSVAL(vwv + 1, 0, 0);
2552 SSVAL(vwv + 2, 0, fnum);
2553 SCVAL(vwv + 3, 0, locktype);
2554 SCVAL(vwv + 3, 1, 0);
2555 SIVALS(vwv + 4, 0, timeout);
2556 SSVAL(vwv + 6, 0, 0);
2557 SSVAL(vwv + 7, 0, 1);
2559 SSVAL(bytes, 0, cli_getpid(cli));
2560 SIVAL(bytes, 2, offset);
2561 SIVAL(bytes, 6, len);
2563 saved_timeout = cli->timeout;
2565 if (timeout != 0) {
2566 cli->timeout = (timeout == -1)
2567 ? 0x7FFFFFFF : (timeout + 2*1000);
2570 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2571 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2573 cli->timeout = saved_timeout;
2575 return status;
2578 /****************************************************************************
2579 Lock a file.
2580 note that timeout is in units of 2 milliseconds
2581 ****************************************************************************/
2583 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2584 uint32_t offset, uint32_t len, int timeout,
2585 enum brl_type lock_type)
2587 NTSTATUS status;
2589 status = cli_locktype(cli, fnum, offset, len, timeout,
2590 (lock_type == READ_LOCK? 1 : 0));
2591 return status;
2594 /****************************************************************************
2595 Unlock a file.
2596 ****************************************************************************/
2598 struct cli_unlock_state {
2599 uint16_t vwv[8];
2600 uint8_t data[10];
2603 static void cli_unlock_done(struct tevent_req *subreq);
2605 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2606 struct event_context *ev,
2607 struct cli_state *cli,
2608 uint16_t fnum,
2609 uint64_t offset,
2610 uint64_t len)
2613 struct tevent_req *req = NULL, *subreq = NULL;
2614 struct cli_unlock_state *state = NULL;
2615 uint8_t additional_flags = 0;
2617 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2618 if (req == NULL) {
2619 return NULL;
2622 SCVAL(state->vwv+0, 0, 0xFF);
2623 SSVAL(state->vwv+2, 0, fnum);
2624 SCVAL(state->vwv+3, 0, 0);
2625 SIVALS(state->vwv+4, 0, 0);
2626 SSVAL(state->vwv+6, 0, 1);
2627 SSVAL(state->vwv+7, 0, 0);
2629 SSVAL(state->data, 0, cli_getpid(cli));
2630 SIVAL(state->data, 2, offset);
2631 SIVAL(state->data, 6, len);
2633 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2634 8, state->vwv, 10, state->data);
2635 if (tevent_req_nomem(subreq, req)) {
2636 return tevent_req_post(req, ev);
2638 tevent_req_set_callback(subreq, cli_unlock_done, req);
2639 return req;
2642 static void cli_unlock_done(struct tevent_req *subreq)
2644 struct tevent_req *req = tevent_req_callback_data(
2645 subreq, struct tevent_req);
2646 NTSTATUS status;
2648 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2649 TALLOC_FREE(subreq);
2650 if (tevent_req_nterror(req, status)) {
2651 return;
2653 tevent_req_done(req);
2656 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2658 return tevent_req_simple_recv_ntstatus(req);
2661 NTSTATUS cli_unlock(struct cli_state *cli,
2662 uint16_t fnum,
2663 uint32_t offset,
2664 uint32_t len)
2666 TALLOC_CTX *frame = talloc_stackframe();
2667 struct event_context *ev;
2668 struct tevent_req *req;
2669 NTSTATUS status = NT_STATUS_OK;
2671 if (cli_has_async_calls(cli)) {
2673 * Can't use sync call while an async call is in flight
2675 status = NT_STATUS_INVALID_PARAMETER;
2676 goto fail;
2679 ev = event_context_init(frame);
2680 if (ev == NULL) {
2681 status = NT_STATUS_NO_MEMORY;
2682 goto fail;
2685 req = cli_unlock_send(frame, ev, cli,
2686 fnum, offset, len);
2687 if (req == NULL) {
2688 status = NT_STATUS_NO_MEMORY;
2689 goto fail;
2692 if (!tevent_req_poll(req, ev)) {
2693 status = map_nt_error_from_unix(errno);
2694 goto fail;
2697 status = cli_unlock_recv(req);
2699 fail:
2700 TALLOC_FREE(frame);
2701 return status;
2704 /****************************************************************************
2705 Lock a file with 64 bit offsets.
2706 ****************************************************************************/
2708 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
2709 uint64_t offset, uint64_t len, int timeout,
2710 enum brl_type lock_type)
2712 uint16_t vwv[8];
2713 uint8_t bytes[20];
2714 int saved_timeout = cli->timeout;
2715 int ltype;
2716 NTSTATUS status;
2718 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2719 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
2722 ltype = (lock_type == READ_LOCK? 1 : 0);
2723 ltype |= LOCKING_ANDX_LARGE_FILES;
2725 SCVAL(vwv + 0, 0, 0xff);
2726 SCVAL(vwv + 0, 1, 0);
2727 SSVAL(vwv + 1, 0, 0);
2728 SSVAL(vwv + 2, 0, fnum);
2729 SCVAL(vwv + 3, 0, ltype);
2730 SCVAL(vwv + 3, 1, 0);
2731 SIVALS(vwv + 4, 0, timeout);
2732 SSVAL(vwv + 6, 0, 0);
2733 SSVAL(vwv + 7, 0, 1);
2735 SIVAL(bytes, 0, cli_getpid(cli));
2736 SOFF_T_R(bytes, 4, offset);
2737 SOFF_T_R(bytes, 12, len);
2739 saved_timeout = cli->timeout;
2741 if (timeout != 0) {
2742 cli->timeout = (timeout == -1)
2743 ? 0x7FFFFFFF : (timeout + 2*1000);
2746 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2747 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2749 cli->timeout = saved_timeout;
2751 return status;
2754 /****************************************************************************
2755 Unlock a file with 64 bit offsets.
2756 ****************************************************************************/
2758 struct cli_unlock64_state {
2759 uint16_t vwv[8];
2760 uint8_t data[20];
2763 static void cli_unlock64_done(struct tevent_req *subreq);
2765 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2766 struct event_context *ev,
2767 struct cli_state *cli,
2768 uint16_t fnum,
2769 uint64_t offset,
2770 uint64_t len)
2773 struct tevent_req *req = NULL, *subreq = NULL;
2774 struct cli_unlock64_state *state = NULL;
2775 uint8_t additional_flags = 0;
2777 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2778 if (req == NULL) {
2779 return NULL;
2782 SCVAL(state->vwv+0, 0, 0xff);
2783 SSVAL(state->vwv+2, 0, fnum);
2784 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2785 SIVALS(state->vwv+4, 0, 0);
2786 SSVAL(state->vwv+6, 0, 1);
2787 SSVAL(state->vwv+7, 0, 0);
2789 SIVAL(state->data, 0, cli_getpid(cli));
2790 SOFF_T_R(state->data, 4, offset);
2791 SOFF_T_R(state->data, 12, len);
2793 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2794 8, state->vwv, 20, state->data);
2795 if (tevent_req_nomem(subreq, req)) {
2796 return tevent_req_post(req, ev);
2798 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2799 return req;
2802 static void cli_unlock64_done(struct tevent_req *subreq)
2804 struct tevent_req *req = tevent_req_callback_data(
2805 subreq, struct tevent_req);
2806 NTSTATUS status;
2808 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2809 TALLOC_FREE(subreq);
2810 if (tevent_req_nterror(req, status)) {
2811 return;
2813 tevent_req_done(req);
2816 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2818 return tevent_req_simple_recv_ntstatus(req);
2821 NTSTATUS cli_unlock64(struct cli_state *cli,
2822 uint16_t fnum,
2823 uint64_t offset,
2824 uint64_t len)
2826 TALLOC_CTX *frame = talloc_stackframe();
2827 struct event_context *ev;
2828 struct tevent_req *req;
2829 NTSTATUS status = NT_STATUS_OK;
2831 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2832 return cli_unlock(cli, fnum, offset, len);
2835 if (cli_has_async_calls(cli)) {
2837 * Can't use sync call while an async call is in flight
2839 status = NT_STATUS_INVALID_PARAMETER;
2840 goto fail;
2843 ev = event_context_init(frame);
2844 if (ev == NULL) {
2845 status = NT_STATUS_NO_MEMORY;
2846 goto fail;
2849 req = cli_unlock64_send(frame, ev, cli,
2850 fnum, offset, len);
2851 if (req == NULL) {
2852 status = NT_STATUS_NO_MEMORY;
2853 goto fail;
2856 if (!tevent_req_poll(req, ev)) {
2857 status = map_nt_error_from_unix(errno);
2858 goto fail;
2861 status = cli_unlock64_recv(req);
2863 fail:
2864 TALLOC_FREE(frame);
2865 return status;
2868 /****************************************************************************
2869 Get/unlock a POSIX lock on a file - internal function.
2870 ****************************************************************************/
2872 struct posix_lock_state {
2873 uint16_t setup;
2874 uint8_t param[4];
2875 uint8_t data[POSIX_LOCK_DATA_SIZE];
2878 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2880 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2881 NULL, 0, NULL, NULL, 0, NULL);
2882 tevent_req_simple_finish_ntstatus(subreq, status);
2885 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2886 struct event_context *ev,
2887 struct cli_state *cli,
2888 uint16_t fnum,
2889 uint64_t offset,
2890 uint64_t len,
2891 bool wait_lock,
2892 enum brl_type lock_type)
2894 struct tevent_req *req = NULL, *subreq = NULL;
2895 struct posix_lock_state *state = NULL;
2897 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2898 if (req == NULL) {
2899 return NULL;
2902 /* Setup setup word. */
2903 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2905 /* Setup param array. */
2906 SSVAL(&state->param, 0, fnum);
2907 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2909 /* Setup data array. */
2910 switch (lock_type) {
2911 case READ_LOCK:
2912 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2913 POSIX_LOCK_TYPE_READ);
2914 break;
2915 case WRITE_LOCK:
2916 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2917 POSIX_LOCK_TYPE_WRITE);
2918 break;
2919 case UNLOCK_LOCK:
2920 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2921 POSIX_LOCK_TYPE_UNLOCK);
2922 break;
2923 default:
2924 return NULL;
2927 if (wait_lock) {
2928 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2929 POSIX_LOCK_FLAG_WAIT);
2930 } else {
2931 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2932 POSIX_LOCK_FLAG_NOWAIT);
2935 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
2936 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2937 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2939 subreq = cli_trans_send(state, /* mem ctx. */
2940 ev, /* event ctx. */
2941 cli, /* cli_state. */
2942 SMBtrans2, /* cmd. */
2943 NULL, /* pipe name. */
2944 -1, /* fid. */
2945 0, /* function. */
2946 0, /* flags. */
2947 &state->setup, /* setup. */
2948 1, /* num setup uint16_t words. */
2949 0, /* max returned setup. */
2950 state->param, /* param. */
2951 4, /* num param. */
2952 2, /* max returned param. */
2953 state->data, /* data. */
2954 POSIX_LOCK_DATA_SIZE, /* num data. */
2955 0); /* max returned data. */
2957 if (tevent_req_nomem(subreq, req)) {
2958 return tevent_req_post(req, ev);
2960 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2961 return req;
2964 /****************************************************************************
2965 POSIX Lock a file.
2966 ****************************************************************************/
2968 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2969 struct event_context *ev,
2970 struct cli_state *cli,
2971 uint16_t fnum,
2972 uint64_t offset,
2973 uint64_t len,
2974 bool wait_lock,
2975 enum brl_type lock_type)
2977 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2978 wait_lock, lock_type);
2981 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2983 return tevent_req_simple_recv_ntstatus(req);
2986 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2987 uint64_t offset, uint64_t len,
2988 bool wait_lock, enum brl_type lock_type)
2990 TALLOC_CTX *frame = talloc_stackframe();
2991 struct event_context *ev = NULL;
2992 struct tevent_req *req = NULL;
2993 NTSTATUS status = NT_STATUS_OK;
2995 if (cli_has_async_calls(cli)) {
2997 * Can't use sync call while an async call is in flight
2999 status = NT_STATUS_INVALID_PARAMETER;
3000 goto fail;
3003 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3004 status = NT_STATUS_INVALID_PARAMETER;
3005 goto fail;
3008 ev = event_context_init(frame);
3009 if (ev == NULL) {
3010 status = NT_STATUS_NO_MEMORY;
3011 goto fail;
3014 req = cli_posix_lock_send(frame,
3016 cli,
3017 fnum,
3018 offset,
3019 len,
3020 wait_lock,
3021 lock_type);
3022 if (req == NULL) {
3023 status = NT_STATUS_NO_MEMORY;
3024 goto fail;
3027 if (!tevent_req_poll(req, ev)) {
3028 status = map_nt_error_from_unix(errno);
3029 goto fail;
3032 status = cli_posix_lock_recv(req);
3034 fail:
3035 TALLOC_FREE(frame);
3036 return status;
3039 /****************************************************************************
3040 POSIX Unlock a file.
3041 ****************************************************************************/
3043 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3044 struct event_context *ev,
3045 struct cli_state *cli,
3046 uint16_t fnum,
3047 uint64_t offset,
3048 uint64_t len)
3050 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3051 false, UNLOCK_LOCK);
3054 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3056 return tevent_req_simple_recv_ntstatus(req);
3059 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3061 TALLOC_CTX *frame = talloc_stackframe();
3062 struct event_context *ev = NULL;
3063 struct tevent_req *req = NULL;
3064 NTSTATUS status = NT_STATUS_OK;
3066 if (cli_has_async_calls(cli)) {
3068 * Can't use sync call while an async call is in flight
3070 status = NT_STATUS_INVALID_PARAMETER;
3071 goto fail;
3074 ev = event_context_init(frame);
3075 if (ev == NULL) {
3076 status = NT_STATUS_NO_MEMORY;
3077 goto fail;
3080 req = cli_posix_unlock_send(frame,
3082 cli,
3083 fnum,
3084 offset,
3085 len);
3086 if (req == NULL) {
3087 status = NT_STATUS_NO_MEMORY;
3088 goto fail;
3091 if (!tevent_req_poll(req, ev)) {
3092 status = map_nt_error_from_unix(errno);
3093 goto fail;
3096 status = cli_posix_unlock_recv(req);
3098 fail:
3099 TALLOC_FREE(frame);
3100 return status;
3103 /****************************************************************************
3104 Do a SMBgetattrE call.
3105 ****************************************************************************/
3107 static void cli_getattrE_done(struct tevent_req *subreq);
3109 struct cli_getattrE_state {
3110 uint16_t vwv[1];
3111 int zone_offset;
3112 uint16_t attr;
3113 SMB_OFF_T size;
3114 time_t change_time;
3115 time_t access_time;
3116 time_t write_time;
3119 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3120 struct event_context *ev,
3121 struct cli_state *cli,
3122 uint16_t fnum)
3124 struct tevent_req *req = NULL, *subreq = NULL;
3125 struct cli_getattrE_state *state = NULL;
3126 uint8_t additional_flags = 0;
3128 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3129 if (req == NULL) {
3130 return NULL;
3133 state->zone_offset = cli->serverzone;
3134 SSVAL(state->vwv+0,0,fnum);
3136 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3137 1, state->vwv, 0, NULL);
3138 if (tevent_req_nomem(subreq, req)) {
3139 return tevent_req_post(req, ev);
3141 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3142 return req;
3145 static void cli_getattrE_done(struct tevent_req *subreq)
3147 struct tevent_req *req = tevent_req_callback_data(
3148 subreq, struct tevent_req);
3149 struct cli_getattrE_state *state = tevent_req_data(
3150 req, struct cli_getattrE_state);
3151 uint8_t wct;
3152 uint16_t *vwv = NULL;
3153 uint8_t *inbuf;
3154 NTSTATUS status;
3156 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3157 NULL, NULL);
3158 TALLOC_FREE(subreq);
3159 if (tevent_req_nterror(req, status)) {
3160 return;
3163 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3164 state->attr = SVAL(vwv+10,0);
3165 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3166 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3167 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3169 tevent_req_done(req);
3172 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3173 uint16_t *attr,
3174 SMB_OFF_T *size,
3175 time_t *change_time,
3176 time_t *access_time,
3177 time_t *write_time)
3179 struct cli_getattrE_state *state = tevent_req_data(
3180 req, struct cli_getattrE_state);
3181 NTSTATUS status;
3183 if (tevent_req_is_nterror(req, &status)) {
3184 return status;
3186 if (attr) {
3187 *attr = state->attr;
3189 if (size) {
3190 *size = state->size;
3192 if (change_time) {
3193 *change_time = state->change_time;
3195 if (access_time) {
3196 *access_time = state->access_time;
3198 if (write_time) {
3199 *write_time = state->write_time;
3201 return NT_STATUS_OK;
3204 NTSTATUS cli_getattrE(struct cli_state *cli,
3205 uint16_t fnum,
3206 uint16_t *attr,
3207 SMB_OFF_T *size,
3208 time_t *change_time,
3209 time_t *access_time,
3210 time_t *write_time)
3212 TALLOC_CTX *frame = talloc_stackframe();
3213 struct event_context *ev = NULL;
3214 struct tevent_req *req = NULL;
3215 NTSTATUS status = NT_STATUS_OK;
3217 if (cli_has_async_calls(cli)) {
3219 * Can't use sync call while an async call is in flight
3221 status = NT_STATUS_INVALID_PARAMETER;
3222 goto fail;
3225 ev = event_context_init(frame);
3226 if (ev == NULL) {
3227 status = NT_STATUS_NO_MEMORY;
3228 goto fail;
3231 req = cli_getattrE_send(frame, ev, cli, fnum);
3232 if (req == NULL) {
3233 status = NT_STATUS_NO_MEMORY;
3234 goto fail;
3237 if (!tevent_req_poll(req, ev)) {
3238 status = map_nt_error_from_unix(errno);
3239 goto fail;
3242 status = cli_getattrE_recv(req,
3243 attr,
3244 size,
3245 change_time,
3246 access_time,
3247 write_time);
3249 fail:
3250 TALLOC_FREE(frame);
3251 return status;
3254 /****************************************************************************
3255 Do a SMBgetatr call
3256 ****************************************************************************/
3258 static void cli_getatr_done(struct tevent_req *subreq);
3260 struct cli_getatr_state {
3261 int zone_offset;
3262 uint16_t attr;
3263 SMB_OFF_T size;
3264 time_t write_time;
3267 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3268 struct event_context *ev,
3269 struct cli_state *cli,
3270 const char *fname)
3272 struct tevent_req *req = NULL, *subreq = NULL;
3273 struct cli_getatr_state *state = NULL;
3274 uint8_t additional_flags = 0;
3275 uint8_t *bytes = NULL;
3277 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3278 if (req == NULL) {
3279 return NULL;
3282 state->zone_offset = cli->serverzone;
3284 bytes = talloc_array(state, uint8_t, 1);
3285 if (tevent_req_nomem(bytes, req)) {
3286 return tevent_req_post(req, ev);
3288 bytes[0] = 4;
3289 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3290 strlen(fname)+1, NULL);
3292 if (tevent_req_nomem(bytes, req)) {
3293 return tevent_req_post(req, ev);
3296 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3297 0, NULL, talloc_get_size(bytes), bytes);
3298 if (tevent_req_nomem(subreq, req)) {
3299 return tevent_req_post(req, ev);
3301 tevent_req_set_callback(subreq, cli_getatr_done, req);
3302 return req;
3305 static void cli_getatr_done(struct tevent_req *subreq)
3307 struct tevent_req *req = tevent_req_callback_data(
3308 subreq, struct tevent_req);
3309 struct cli_getatr_state *state = tevent_req_data(
3310 req, struct cli_getatr_state);
3311 uint8_t wct;
3312 uint16_t *vwv = NULL;
3313 uint8_t *inbuf;
3314 NTSTATUS status;
3316 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3317 NULL);
3318 TALLOC_FREE(subreq);
3319 if (tevent_req_nterror(req, status)) {
3320 return;
3323 state->attr = SVAL(vwv+0,0);
3324 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3325 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3327 tevent_req_done(req);
3330 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3331 uint16_t *attr,
3332 SMB_OFF_T *size,
3333 time_t *write_time)
3335 struct cli_getatr_state *state = tevent_req_data(
3336 req, struct cli_getatr_state);
3337 NTSTATUS status;
3339 if (tevent_req_is_nterror(req, &status)) {
3340 return status;
3342 if (attr) {
3343 *attr = state->attr;
3345 if (size) {
3346 *size = state->size;
3348 if (write_time) {
3349 *write_time = state->write_time;
3351 return NT_STATUS_OK;
3354 NTSTATUS cli_getatr(struct cli_state *cli,
3355 const char *fname,
3356 uint16_t *attr,
3357 SMB_OFF_T *size,
3358 time_t *write_time)
3360 TALLOC_CTX *frame = talloc_stackframe();
3361 struct event_context *ev = NULL;
3362 struct tevent_req *req = NULL;
3363 NTSTATUS status = NT_STATUS_OK;
3365 if (cli_has_async_calls(cli)) {
3367 * Can't use sync call while an async call is in flight
3369 status = NT_STATUS_INVALID_PARAMETER;
3370 goto fail;
3373 ev = event_context_init(frame);
3374 if (ev == NULL) {
3375 status = NT_STATUS_NO_MEMORY;
3376 goto fail;
3379 req = cli_getatr_send(frame, ev, cli, fname);
3380 if (req == NULL) {
3381 status = NT_STATUS_NO_MEMORY;
3382 goto fail;
3385 if (!tevent_req_poll(req, ev)) {
3386 status = map_nt_error_from_unix(errno);
3387 goto fail;
3390 status = cli_getatr_recv(req,
3391 attr,
3392 size,
3393 write_time);
3395 fail:
3396 TALLOC_FREE(frame);
3397 return status;
3400 /****************************************************************************
3401 Do a SMBsetattrE call.
3402 ****************************************************************************/
3404 static void cli_setattrE_done(struct tevent_req *subreq);
3406 struct cli_setattrE_state {
3407 uint16_t vwv[7];
3410 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3411 struct event_context *ev,
3412 struct cli_state *cli,
3413 uint16_t fnum,
3414 time_t change_time,
3415 time_t access_time,
3416 time_t write_time)
3418 struct tevent_req *req = NULL, *subreq = NULL;
3419 struct cli_setattrE_state *state = NULL;
3420 uint8_t additional_flags = 0;
3422 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3423 if (req == NULL) {
3424 return NULL;
3427 SSVAL(state->vwv+0, 0, fnum);
3428 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3429 cli->serverzone);
3430 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3431 cli->serverzone);
3432 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3433 cli->serverzone);
3435 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3436 7, state->vwv, 0, NULL);
3437 if (tevent_req_nomem(subreq, req)) {
3438 return tevent_req_post(req, ev);
3440 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3441 return req;
3444 static void cli_setattrE_done(struct tevent_req *subreq)
3446 struct tevent_req *req = tevent_req_callback_data(
3447 subreq, struct tevent_req);
3448 NTSTATUS status;
3450 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3451 TALLOC_FREE(subreq);
3452 if (tevent_req_nterror(req, status)) {
3453 return;
3455 tevent_req_done(req);
3458 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3460 return tevent_req_simple_recv_ntstatus(req);
3463 NTSTATUS cli_setattrE(struct cli_state *cli,
3464 uint16_t fnum,
3465 time_t change_time,
3466 time_t access_time,
3467 time_t write_time)
3469 TALLOC_CTX *frame = talloc_stackframe();
3470 struct event_context *ev = NULL;
3471 struct tevent_req *req = NULL;
3472 NTSTATUS status = NT_STATUS_OK;
3474 if (cli_has_async_calls(cli)) {
3476 * Can't use sync call while an async call is in flight
3478 status = NT_STATUS_INVALID_PARAMETER;
3479 goto fail;
3482 ev = event_context_init(frame);
3483 if (ev == NULL) {
3484 status = NT_STATUS_NO_MEMORY;
3485 goto fail;
3488 req = cli_setattrE_send(frame, ev,
3489 cli,
3490 fnum,
3491 change_time,
3492 access_time,
3493 write_time);
3495 if (req == NULL) {
3496 status = NT_STATUS_NO_MEMORY;
3497 goto fail;
3500 if (!tevent_req_poll(req, ev)) {
3501 status = map_nt_error_from_unix(errno);
3502 goto fail;
3505 status = cli_setattrE_recv(req);
3507 fail:
3508 TALLOC_FREE(frame);
3509 return status;
3512 /****************************************************************************
3513 Do a SMBsetatr call.
3514 ****************************************************************************/
3516 static void cli_setatr_done(struct tevent_req *subreq);
3518 struct cli_setatr_state {
3519 uint16_t vwv[8];
3522 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3523 struct event_context *ev,
3524 struct cli_state *cli,
3525 const char *fname,
3526 uint16_t attr,
3527 time_t mtime)
3529 struct tevent_req *req = NULL, *subreq = NULL;
3530 struct cli_setatr_state *state = NULL;
3531 uint8_t additional_flags = 0;
3532 uint8_t *bytes = NULL;
3534 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3535 if (req == NULL) {
3536 return NULL;
3539 SSVAL(state->vwv+0, 0, attr);
3540 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3542 bytes = talloc_array(state, uint8_t, 1);
3543 if (tevent_req_nomem(bytes, req)) {
3544 return tevent_req_post(req, ev);
3546 bytes[0] = 4;
3547 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3548 strlen(fname)+1, NULL);
3549 if (tevent_req_nomem(bytes, req)) {
3550 return tevent_req_post(req, ev);
3552 bytes = talloc_realloc(state, bytes, uint8_t,
3553 talloc_get_size(bytes)+1);
3554 if (tevent_req_nomem(bytes, req)) {
3555 return tevent_req_post(req, ev);
3558 bytes[talloc_get_size(bytes)-1] = 4;
3559 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3560 1, NULL);
3561 if (tevent_req_nomem(bytes, req)) {
3562 return tevent_req_post(req, ev);
3565 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3566 8, state->vwv, talloc_get_size(bytes), bytes);
3567 if (tevent_req_nomem(subreq, req)) {
3568 return tevent_req_post(req, ev);
3570 tevent_req_set_callback(subreq, cli_setatr_done, req);
3571 return req;
3574 static void cli_setatr_done(struct tevent_req *subreq)
3576 struct tevent_req *req = tevent_req_callback_data(
3577 subreq, struct tevent_req);
3578 NTSTATUS status;
3580 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3581 TALLOC_FREE(subreq);
3582 if (tevent_req_nterror(req, status)) {
3583 return;
3585 tevent_req_done(req);
3588 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3590 return tevent_req_simple_recv_ntstatus(req);
3593 NTSTATUS cli_setatr(struct cli_state *cli,
3594 const char *fname,
3595 uint16_t attr,
3596 time_t mtime)
3598 TALLOC_CTX *frame = talloc_stackframe();
3599 struct event_context *ev = NULL;
3600 struct tevent_req *req = NULL;
3601 NTSTATUS status = NT_STATUS_OK;
3603 if (cli_has_async_calls(cli)) {
3605 * Can't use sync call while an async call is in flight
3607 status = NT_STATUS_INVALID_PARAMETER;
3608 goto fail;
3611 ev = event_context_init(frame);
3612 if (ev == NULL) {
3613 status = NT_STATUS_NO_MEMORY;
3614 goto fail;
3617 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3618 if (req == NULL) {
3619 status = NT_STATUS_NO_MEMORY;
3620 goto fail;
3623 if (!tevent_req_poll(req, ev)) {
3624 status = map_nt_error_from_unix(errno);
3625 goto fail;
3628 status = cli_setatr_recv(req);
3630 fail:
3631 TALLOC_FREE(frame);
3632 return status;
3635 /****************************************************************************
3636 Check for existance of a dir.
3637 ****************************************************************************/
3639 static void cli_chkpath_done(struct tevent_req *subreq);
3641 struct cli_chkpath_state {
3642 int dummy;
3645 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3646 struct event_context *ev,
3647 struct cli_state *cli,
3648 const char *fname)
3650 struct tevent_req *req = NULL, *subreq = NULL;
3651 struct cli_chkpath_state *state = NULL;
3652 uint8_t additional_flags = 0;
3653 uint8_t *bytes = NULL;
3655 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3656 if (req == NULL) {
3657 return NULL;
3660 bytes = talloc_array(state, uint8_t, 1);
3661 if (tevent_req_nomem(bytes, req)) {
3662 return tevent_req_post(req, ev);
3664 bytes[0] = 4;
3665 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3666 strlen(fname)+1, NULL);
3668 if (tevent_req_nomem(bytes, req)) {
3669 return tevent_req_post(req, ev);
3672 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3673 0, NULL, talloc_get_size(bytes), bytes);
3674 if (tevent_req_nomem(subreq, req)) {
3675 return tevent_req_post(req, ev);
3677 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3678 return req;
3681 static void cli_chkpath_done(struct tevent_req *subreq)
3683 struct tevent_req *req = tevent_req_callback_data(
3684 subreq, struct tevent_req);
3685 NTSTATUS status;
3687 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3688 TALLOC_FREE(subreq);
3689 if (tevent_req_nterror(req, status)) {
3690 return;
3692 tevent_req_done(req);
3695 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3697 return tevent_req_simple_recv_ntstatus(req);
3700 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3702 TALLOC_CTX *frame = talloc_stackframe();
3703 struct event_context *ev = NULL;
3704 struct tevent_req *req = NULL;
3705 char *path2 = NULL;
3706 NTSTATUS status = NT_STATUS_OK;
3708 if (cli_has_async_calls(cli)) {
3710 * Can't use sync call while an async call is in flight
3712 status = NT_STATUS_INVALID_PARAMETER;
3713 goto fail;
3716 path2 = talloc_strdup(frame, path);
3717 if (!path2) {
3718 status = NT_STATUS_NO_MEMORY;
3719 goto fail;
3721 trim_char(path2,'\0','\\');
3722 if (!*path2) {
3723 path2 = talloc_strdup(frame, "\\");
3724 if (!path2) {
3725 status = NT_STATUS_NO_MEMORY;
3726 goto fail;
3730 ev = event_context_init(frame);
3731 if (ev == NULL) {
3732 status = NT_STATUS_NO_MEMORY;
3733 goto fail;
3736 req = cli_chkpath_send(frame, ev, cli, path2);
3737 if (req == NULL) {
3738 status = NT_STATUS_NO_MEMORY;
3739 goto fail;
3742 if (!tevent_req_poll(req, ev)) {
3743 status = map_nt_error_from_unix(errno);
3744 goto fail;
3747 status = cli_chkpath_recv(req);
3749 fail:
3750 TALLOC_FREE(frame);
3751 return status;
3754 /****************************************************************************
3755 Query disk space.
3756 ****************************************************************************/
3758 static void cli_dskattr_done(struct tevent_req *subreq);
3760 struct cli_dskattr_state {
3761 int bsize;
3762 int total;
3763 int avail;
3766 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3767 struct event_context *ev,
3768 struct cli_state *cli)
3770 struct tevent_req *req = NULL, *subreq = NULL;
3771 struct cli_dskattr_state *state = NULL;
3772 uint8_t additional_flags = 0;
3774 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3775 if (req == NULL) {
3776 return NULL;
3779 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3780 0, NULL, 0, NULL);
3781 if (tevent_req_nomem(subreq, req)) {
3782 return tevent_req_post(req, ev);
3784 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3785 return req;
3788 static void cli_dskattr_done(struct tevent_req *subreq)
3790 struct tevent_req *req = tevent_req_callback_data(
3791 subreq, struct tevent_req);
3792 struct cli_dskattr_state *state = tevent_req_data(
3793 req, struct cli_dskattr_state);
3794 uint8_t wct;
3795 uint16_t *vwv = NULL;
3796 uint8_t *inbuf;
3797 NTSTATUS status;
3799 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3800 NULL);
3801 TALLOC_FREE(subreq);
3802 if (tevent_req_nterror(req, status)) {
3803 return;
3805 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3806 state->total = SVAL(vwv+0, 0);
3807 state->avail = SVAL(vwv+3, 0);
3808 tevent_req_done(req);
3811 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3813 struct cli_dskattr_state *state = tevent_req_data(
3814 req, struct cli_dskattr_state);
3815 NTSTATUS status;
3817 if (tevent_req_is_nterror(req, &status)) {
3818 return status;
3820 *bsize = state->bsize;
3821 *total = state->total;
3822 *avail = state->avail;
3823 return NT_STATUS_OK;
3826 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3828 TALLOC_CTX *frame = talloc_stackframe();
3829 struct event_context *ev = NULL;
3830 struct tevent_req *req = NULL;
3831 NTSTATUS status = NT_STATUS_OK;
3833 if (cli_has_async_calls(cli)) {
3835 * Can't use sync call while an async call is in flight
3837 status = NT_STATUS_INVALID_PARAMETER;
3838 goto fail;
3841 ev = event_context_init(frame);
3842 if (ev == NULL) {
3843 status = NT_STATUS_NO_MEMORY;
3844 goto fail;
3847 req = cli_dskattr_send(frame, ev, cli);
3848 if (req == NULL) {
3849 status = NT_STATUS_NO_MEMORY;
3850 goto fail;
3853 if (!tevent_req_poll(req, ev)) {
3854 status = map_nt_error_from_unix(errno);
3855 goto fail;
3858 status = cli_dskattr_recv(req, bsize, total, avail);
3860 fail:
3861 TALLOC_FREE(frame);
3862 return status;
3865 /****************************************************************************
3866 Create and open a temporary file.
3867 ****************************************************************************/
3869 static void cli_ctemp_done(struct tevent_req *subreq);
3871 struct ctemp_state {
3872 uint16_t vwv[3];
3873 char *ret_path;
3874 uint16_t fnum;
3877 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3878 struct event_context *ev,
3879 struct cli_state *cli,
3880 const char *path)
3882 struct tevent_req *req = NULL, *subreq = NULL;
3883 struct ctemp_state *state = NULL;
3884 uint8_t additional_flags = 0;
3885 uint8_t *bytes = NULL;
3887 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3888 if (req == NULL) {
3889 return NULL;
3892 SSVAL(state->vwv,0,0);
3893 SIVALS(state->vwv+1,0,-1);
3895 bytes = talloc_array(state, uint8_t, 1);
3896 if (tevent_req_nomem(bytes, req)) {
3897 return tevent_req_post(req, ev);
3899 bytes[0] = 4;
3900 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3901 strlen(path)+1, NULL);
3902 if (tevent_req_nomem(bytes, req)) {
3903 return tevent_req_post(req, ev);
3906 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3907 3, state->vwv, talloc_get_size(bytes), bytes);
3908 if (tevent_req_nomem(subreq, req)) {
3909 return tevent_req_post(req, ev);
3911 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3912 return req;
3915 static void cli_ctemp_done(struct tevent_req *subreq)
3917 struct tevent_req *req = tevent_req_callback_data(
3918 subreq, struct tevent_req);
3919 struct ctemp_state *state = tevent_req_data(
3920 req, struct ctemp_state);
3921 NTSTATUS status;
3922 uint8_t wcnt;
3923 uint16_t *vwv;
3924 uint32_t num_bytes = 0;
3925 uint8_t *bytes = NULL;
3926 uint8_t *inbuf;
3928 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3929 &num_bytes, &bytes);
3930 TALLOC_FREE(subreq);
3931 if (tevent_req_nterror(req, status)) {
3932 return;
3935 state->fnum = SVAL(vwv+0, 0);
3937 /* From W2K3, the result is just the ASCII name */
3938 if (num_bytes < 2) {
3939 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3940 return;
3943 if (pull_string_talloc(state,
3944 NULL,
3946 &state->ret_path,
3947 bytes,
3948 num_bytes,
3949 STR_ASCII) == 0) {
3950 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3951 return;
3953 tevent_req_done(req);
3956 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3957 TALLOC_CTX *ctx,
3958 uint16_t *pfnum,
3959 char **outfile)
3961 struct ctemp_state *state = tevent_req_data(req,
3962 struct ctemp_state);
3963 NTSTATUS status;
3965 if (tevent_req_is_nterror(req, &status)) {
3966 return status;
3968 *pfnum = state->fnum;
3969 *outfile = talloc_strdup(ctx, state->ret_path);
3970 if (!*outfile) {
3971 return NT_STATUS_NO_MEMORY;
3973 return NT_STATUS_OK;
3976 NTSTATUS cli_ctemp(struct cli_state *cli,
3977 TALLOC_CTX *ctx,
3978 const char *path,
3979 uint16_t *pfnum,
3980 char **out_path)
3982 TALLOC_CTX *frame = talloc_stackframe();
3983 struct event_context *ev;
3984 struct tevent_req *req;
3985 NTSTATUS status = NT_STATUS_OK;
3987 if (cli_has_async_calls(cli)) {
3989 * Can't use sync call while an async call is in flight
3991 status = NT_STATUS_INVALID_PARAMETER;
3992 goto fail;
3995 ev = event_context_init(frame);
3996 if (ev == NULL) {
3997 status = NT_STATUS_NO_MEMORY;
3998 goto fail;
4001 req = cli_ctemp_send(frame, ev, cli, path);
4002 if (req == NULL) {
4003 status = NT_STATUS_NO_MEMORY;
4004 goto fail;
4007 if (!tevent_req_poll(req, ev)) {
4008 status = map_nt_error_from_unix(errno);
4009 goto fail;
4012 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4014 fail:
4015 TALLOC_FREE(frame);
4016 return status;
4020 send a raw ioctl - used by the torture code
4022 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4024 uint16_t vwv[3];
4025 NTSTATUS status;
4027 SSVAL(vwv+0, 0, fnum);
4028 SSVAL(vwv+1, 0, code>>16);
4029 SSVAL(vwv+2, 0, (code&0xFFFF));
4031 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4032 NULL, 0, NULL, NULL, NULL, NULL);
4033 if (!NT_STATUS_IS_OK(status)) {
4034 return status;
4036 *blob = data_blob_null;
4037 return NT_STATUS_OK;
4040 /*********************************************************
4041 Set an extended attribute utility fn.
4042 *********************************************************/
4044 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4045 uint8_t *param, unsigned int param_len,
4046 const char *ea_name,
4047 const char *ea_val, size_t ea_len)
4049 uint16_t setup[1];
4050 unsigned int data_len = 0;
4051 uint8_t *data = NULL;
4052 char *p;
4053 size_t ea_namelen = strlen(ea_name);
4054 NTSTATUS status;
4056 SSVAL(setup, 0, setup_val);
4058 if (ea_namelen == 0 && ea_len == 0) {
4059 data_len = 4;
4060 data = (uint8_t *)SMB_MALLOC(data_len);
4061 if (!data) {
4062 return NT_STATUS_NO_MEMORY;
4064 p = (char *)data;
4065 SIVAL(p,0,data_len);
4066 } else {
4067 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4068 data = (uint8_t *)SMB_MALLOC(data_len);
4069 if (!data) {
4070 return NT_STATUS_NO_MEMORY;
4072 p = (char *)data;
4073 SIVAL(p,0,data_len);
4074 p += 4;
4075 SCVAL(p, 0, 0); /* EA flags. */
4076 SCVAL(p, 1, ea_namelen);
4077 SSVAL(p, 2, ea_len);
4078 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4079 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4082 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4083 setup, 1, 0,
4084 param, param_len, 2,
4085 data, data_len, cli->max_xmit,
4086 NULL,
4087 NULL, 0, NULL, /* rsetup */
4088 NULL, 0, NULL, /* rparam */
4089 NULL, 0, NULL); /* rdata */
4090 SAFE_FREE(data);
4091 return status;
4094 /*********************************************************
4095 Set an extended attribute on a pathname.
4096 *********************************************************/
4098 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4099 const char *ea_name, const char *ea_val,
4100 size_t ea_len)
4102 unsigned int param_len = 0;
4103 uint8_t *param;
4104 NTSTATUS status;
4105 TALLOC_CTX *frame = talloc_stackframe();
4107 param = talloc_array(talloc_tos(), uint8_t, 6);
4108 if (!param) {
4109 return NT_STATUS_NO_MEMORY;
4111 SSVAL(param,0,SMB_INFO_SET_EA);
4112 SSVAL(param,2,0);
4113 SSVAL(param,4,0);
4115 param = trans2_bytes_push_str(param, cli_ucs2(cli),
4116 path, strlen(path)+1,
4117 NULL);
4118 param_len = talloc_get_size(param);
4120 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4121 ea_name, ea_val, ea_len);
4122 SAFE_FREE(frame);
4123 return status;
4126 /*********************************************************
4127 Set an extended attribute on an fnum.
4128 *********************************************************/
4130 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4131 const char *ea_name, const char *ea_val,
4132 size_t ea_len)
4134 uint8_t param[6];
4136 memset(param, 0, 6);
4137 SSVAL(param,0,fnum);
4138 SSVAL(param,2,SMB_INFO_SET_EA);
4140 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4141 ea_name, ea_val, ea_len);
4144 /*********************************************************
4145 Get an extended attribute list utility fn.
4146 *********************************************************/
4148 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4149 size_t rdata_len,
4150 size_t *pnum_eas, struct ea_struct **pea_list)
4152 struct ea_struct *ea_list = NULL;
4153 size_t num_eas;
4154 size_t ea_size;
4155 const uint8_t *p;
4157 if (rdata_len < 4) {
4158 return false;
4161 ea_size = (size_t)IVAL(rdata,0);
4162 if (ea_size > rdata_len) {
4163 return false;
4166 if (ea_size == 0) {
4167 /* No EA's present. */
4168 *pnum_eas = 0;
4169 *pea_list = NULL;
4170 return true;
4173 p = rdata + 4;
4174 ea_size -= 4;
4176 /* Validate the EA list and count it. */
4177 for (num_eas = 0; ea_size >= 4; num_eas++) {
4178 unsigned int ea_namelen = CVAL(p,1);
4179 unsigned int ea_valuelen = SVAL(p,2);
4180 if (ea_namelen == 0) {
4181 return false;
4183 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4184 return false;
4186 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4187 p += 4 + ea_namelen + 1 + ea_valuelen;
4190 if (num_eas == 0) {
4191 *pnum_eas = 0;
4192 *pea_list = NULL;
4193 return true;
4196 *pnum_eas = num_eas;
4197 if (!pea_list) {
4198 /* Caller only wants number of EA's. */
4199 return true;
4202 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4203 if (!ea_list) {
4204 return false;
4207 ea_size = (size_t)IVAL(rdata,0);
4208 p = rdata + 4;
4210 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4211 struct ea_struct *ea = &ea_list[num_eas];
4212 fstring unix_ea_name;
4213 unsigned int ea_namelen = CVAL(p,1);
4214 unsigned int ea_valuelen = SVAL(p,2);
4216 ea->flags = CVAL(p,0);
4217 unix_ea_name[0] = '\0';
4218 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4219 ea->name = talloc_strdup(ea_list, unix_ea_name);
4220 if (!ea->name) {
4221 goto fail;
4223 /* Ensure the value is null terminated (in case it's a string). */
4224 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4225 if (!ea->value.data) {
4226 goto fail;
4228 if (ea_valuelen) {
4229 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4231 ea->value.data[ea_valuelen] = 0;
4232 ea->value.length--;
4233 p += 4 + ea_namelen + 1 + ea_valuelen;
4236 *pea_list = ea_list;
4237 return true;
4239 fail:
4240 TALLOC_FREE(ea_list);
4241 return false;
4244 /*********************************************************
4245 Get an extended attribute list from a pathname.
4246 *********************************************************/
4248 struct cli_get_ea_list_path_state {
4249 uint32_t num_data;
4250 uint8_t *data;
4253 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4255 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4256 struct tevent_context *ev,
4257 struct cli_state *cli,
4258 const char *fname)
4260 struct tevent_req *req, *subreq;
4261 struct cli_get_ea_list_path_state *state;
4263 req = tevent_req_create(mem_ctx, &state,
4264 struct cli_get_ea_list_path_state);
4265 if (req == NULL) {
4266 return NULL;
4268 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4269 SMB_INFO_QUERY_ALL_EAS, 4,
4270 cli->max_xmit);
4271 if (tevent_req_nomem(subreq, req)) {
4272 return tevent_req_post(req, ev);
4274 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4275 return req;
4278 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4280 struct tevent_req *req = tevent_req_callback_data(
4281 subreq, struct tevent_req);
4282 struct cli_get_ea_list_path_state *state = tevent_req_data(
4283 req, struct cli_get_ea_list_path_state);
4284 NTSTATUS status;
4286 status = cli_qpathinfo_recv(subreq, state, &state->data,
4287 &state->num_data);
4288 TALLOC_FREE(subreq);
4289 if (tevent_req_nterror(req, status)) {
4290 return;
4292 tevent_req_done(req);
4295 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4296 size_t *pnum_eas, struct ea_struct **peas)
4298 struct cli_get_ea_list_path_state *state = tevent_req_data(
4299 req, struct cli_get_ea_list_path_state);
4300 NTSTATUS status;
4302 if (tevent_req_is_nterror(req, &status)) {
4303 return status;
4305 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4306 pnum_eas, peas)) {
4307 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4309 return NT_STATUS_OK;
4312 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4313 TALLOC_CTX *ctx,
4314 size_t *pnum_eas,
4315 struct ea_struct **pea_list)
4317 TALLOC_CTX *frame = talloc_stackframe();
4318 struct event_context *ev = NULL;
4319 struct tevent_req *req = NULL;
4320 NTSTATUS status = NT_STATUS_NO_MEMORY;
4322 if (cli_has_async_calls(cli)) {
4324 * Can't use sync call while an async call is in flight
4326 status = NT_STATUS_INVALID_PARAMETER;
4327 goto fail;
4329 ev = event_context_init(frame);
4330 if (ev == NULL) {
4331 goto fail;
4333 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4334 if (req == NULL) {
4335 goto fail;
4337 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4338 goto fail;
4340 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4341 fail:
4342 TALLOC_FREE(frame);
4343 return status;
4346 /****************************************************************************
4347 Convert open "flags" arg to uint32_t on wire.
4348 ****************************************************************************/
4350 static uint32_t open_flags_to_wire(int flags)
4352 int open_mode = flags & O_ACCMODE;
4353 uint32_t ret = 0;
4355 switch (open_mode) {
4356 case O_WRONLY:
4357 ret |= SMB_O_WRONLY;
4358 break;
4359 case O_RDWR:
4360 ret |= SMB_O_RDWR;
4361 break;
4362 default:
4363 case O_RDONLY:
4364 ret |= SMB_O_RDONLY;
4365 break;
4368 if (flags & O_CREAT) {
4369 ret |= SMB_O_CREAT;
4371 if (flags & O_EXCL) {
4372 ret |= SMB_O_EXCL;
4374 if (flags & O_TRUNC) {
4375 ret |= SMB_O_TRUNC;
4377 #if defined(O_SYNC)
4378 if (flags & O_SYNC) {
4379 ret |= SMB_O_SYNC;
4381 #endif /* O_SYNC */
4382 if (flags & O_APPEND) {
4383 ret |= SMB_O_APPEND;
4385 #if defined(O_DIRECT)
4386 if (flags & O_DIRECT) {
4387 ret |= SMB_O_DIRECT;
4389 #endif
4390 #if defined(O_DIRECTORY)
4391 if (flags & O_DIRECTORY) {
4392 ret |= SMB_O_DIRECTORY;
4394 #endif
4395 return ret;
4398 /****************************************************************************
4399 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4400 ****************************************************************************/
4402 struct posix_open_state {
4403 uint16_t setup;
4404 uint8_t *param;
4405 uint8_t data[18];
4406 uint16_t fnum; /* Out */
4409 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4411 struct tevent_req *req = tevent_req_callback_data(
4412 subreq, struct tevent_req);
4413 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4414 NTSTATUS status;
4415 uint8_t *data;
4416 uint32_t num_data;
4418 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4419 NULL, 0, NULL, &data, 12, &num_data);
4420 TALLOC_FREE(subreq);
4421 if (tevent_req_nterror(req, status)) {
4422 return;
4424 state->fnum = SVAL(data,2);
4425 tevent_req_done(req);
4428 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4429 struct event_context *ev,
4430 struct cli_state *cli,
4431 const char *fname,
4432 int flags,
4433 mode_t mode,
4434 bool is_dir)
4436 struct tevent_req *req = NULL, *subreq = NULL;
4437 struct posix_open_state *state = NULL;
4438 uint32_t wire_flags = open_flags_to_wire(flags);
4440 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4441 if (req == NULL) {
4442 return NULL;
4445 /* Setup setup word. */
4446 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4448 /* Setup param array. */
4449 state->param = talloc_array(state, uint8_t, 6);
4450 if (tevent_req_nomem(state->param, req)) {
4451 return tevent_req_post(req, ev);
4453 memset(state->param, '\0', 6);
4454 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4456 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4457 strlen(fname)+1, NULL);
4459 if (tevent_req_nomem(state->param, req)) {
4460 return tevent_req_post(req, ev);
4463 /* Setup data words. */
4464 if (is_dir) {
4465 wire_flags |= SMB_O_DIRECTORY;
4468 SIVAL(state->data,0,0); /* No oplock. */
4469 SIVAL(state->data,4,wire_flags);
4470 SIVAL(state->data,8,unix_perms_to_wire(mode));
4471 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4472 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4474 subreq = cli_trans_send(state, /* mem ctx. */
4475 ev, /* event ctx. */
4476 cli, /* cli_state. */
4477 SMBtrans2, /* cmd. */
4478 NULL, /* pipe name. */
4479 -1, /* fid. */
4480 0, /* function. */
4481 0, /* flags. */
4482 &state->setup, /* setup. */
4483 1, /* num setup uint16_t words. */
4484 0, /* max returned setup. */
4485 state->param, /* param. */
4486 talloc_get_size(state->param),/* num param. */
4487 2, /* max returned param. */
4488 state->data, /* data. */
4489 18, /* num data. */
4490 12); /* max returned data. */
4492 if (tevent_req_nomem(subreq, req)) {
4493 return tevent_req_post(req, ev);
4495 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4496 return req;
4499 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4500 struct event_context *ev,
4501 struct cli_state *cli,
4502 const char *fname,
4503 int flags,
4504 mode_t mode)
4506 return cli_posix_open_internal_send(mem_ctx, ev,
4507 cli, fname, flags, mode, false);
4510 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4512 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4513 NTSTATUS status;
4515 if (tevent_req_is_nterror(req, &status)) {
4516 return status;
4518 *pfnum = state->fnum;
4519 return NT_STATUS_OK;
4522 /****************************************************************************
4523 Open - POSIX semantics. Doesn't request oplock.
4524 ****************************************************************************/
4526 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4527 int flags, mode_t mode, uint16_t *pfnum)
4530 TALLOC_CTX *frame = talloc_stackframe();
4531 struct event_context *ev = NULL;
4532 struct tevent_req *req = NULL;
4533 NTSTATUS status = NT_STATUS_OK;
4535 if (cli_has_async_calls(cli)) {
4537 * Can't use sync call while an async call is in flight
4539 status = NT_STATUS_INVALID_PARAMETER;
4540 goto fail;
4543 ev = event_context_init(frame);
4544 if (ev == NULL) {
4545 status = NT_STATUS_NO_MEMORY;
4546 goto fail;
4549 req = cli_posix_open_send(frame,
4551 cli,
4552 fname,
4553 flags,
4554 mode);
4555 if (req == NULL) {
4556 status = NT_STATUS_NO_MEMORY;
4557 goto fail;
4560 if (!tevent_req_poll(req, ev)) {
4561 status = map_nt_error_from_unix(errno);
4562 goto fail;
4565 status = cli_posix_open_recv(req, pfnum);
4567 fail:
4568 TALLOC_FREE(frame);
4569 return status;
4572 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4573 struct event_context *ev,
4574 struct cli_state *cli,
4575 const char *fname,
4576 mode_t mode)
4578 return cli_posix_open_internal_send(mem_ctx, ev,
4579 cli, fname, O_CREAT, mode, true);
4582 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4584 return tevent_req_simple_recv_ntstatus(req);
4587 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4589 TALLOC_CTX *frame = talloc_stackframe();
4590 struct event_context *ev = NULL;
4591 struct tevent_req *req = NULL;
4592 NTSTATUS status = NT_STATUS_OK;
4594 if (cli_has_async_calls(cli)) {
4596 * Can't use sync call while an async call is in flight
4598 status = NT_STATUS_INVALID_PARAMETER;
4599 goto fail;
4602 ev = event_context_init(frame);
4603 if (ev == NULL) {
4604 status = NT_STATUS_NO_MEMORY;
4605 goto fail;
4608 req = cli_posix_mkdir_send(frame,
4610 cli,
4611 fname,
4612 mode);
4613 if (req == NULL) {
4614 status = NT_STATUS_NO_MEMORY;
4615 goto fail;
4618 if (!tevent_req_poll(req, ev)) {
4619 status = map_nt_error_from_unix(errno);
4620 goto fail;
4623 status = cli_posix_mkdir_recv(req);
4625 fail:
4626 TALLOC_FREE(frame);
4627 return status;
4630 /****************************************************************************
4631 unlink or rmdir - POSIX semantics.
4632 ****************************************************************************/
4634 struct cli_posix_unlink_internal_state {
4635 uint8_t data[2];
4638 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4640 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4641 struct event_context *ev,
4642 struct cli_state *cli,
4643 const char *fname,
4644 uint16_t level)
4646 struct tevent_req *req = NULL, *subreq = NULL;
4647 struct cli_posix_unlink_internal_state *state = NULL;
4649 req = tevent_req_create(mem_ctx, &state,
4650 struct cli_posix_unlink_internal_state);
4651 if (req == NULL) {
4652 return NULL;
4655 /* Setup data word. */
4656 SSVAL(state->data, 0, level);
4658 subreq = cli_setpathinfo_send(state, ev, cli,
4659 SMB_POSIX_PATH_UNLINK,
4660 fname,
4661 state->data, sizeof(state->data));
4662 if (tevent_req_nomem(subreq, req)) {
4663 return tevent_req_post(req, ev);
4665 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4666 return req;
4669 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4671 NTSTATUS status = cli_setpathinfo_recv(subreq);
4672 tevent_req_simple_finish_ntstatus(subreq, status);
4675 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4676 struct event_context *ev,
4677 struct cli_state *cli,
4678 const char *fname)
4680 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4681 SMB_POSIX_UNLINK_FILE_TARGET);
4684 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4686 return tevent_req_simple_recv_ntstatus(req);
4689 /****************************************************************************
4690 unlink - POSIX semantics.
4691 ****************************************************************************/
4693 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4695 TALLOC_CTX *frame = talloc_stackframe();
4696 struct event_context *ev = NULL;
4697 struct tevent_req *req = NULL;
4698 NTSTATUS status = NT_STATUS_OK;
4700 if (cli_has_async_calls(cli)) {
4702 * Can't use sync call while an async call is in flight
4704 status = NT_STATUS_INVALID_PARAMETER;
4705 goto fail;
4708 ev = event_context_init(frame);
4709 if (ev == NULL) {
4710 status = NT_STATUS_NO_MEMORY;
4711 goto fail;
4714 req = cli_posix_unlink_send(frame,
4716 cli,
4717 fname);
4718 if (req == NULL) {
4719 status = NT_STATUS_NO_MEMORY;
4720 goto fail;
4723 if (!tevent_req_poll(req, ev)) {
4724 status = map_nt_error_from_unix(errno);
4725 goto fail;
4728 status = cli_posix_unlink_recv(req);
4730 fail:
4731 TALLOC_FREE(frame);
4732 return status;
4735 /****************************************************************************
4736 rmdir - POSIX semantics.
4737 ****************************************************************************/
4739 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4740 struct event_context *ev,
4741 struct cli_state *cli,
4742 const char *fname)
4744 return cli_posix_unlink_internal_send(
4745 mem_ctx, ev, cli, fname,
4746 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4749 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4751 return tevent_req_simple_recv_ntstatus(req);
4754 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4756 TALLOC_CTX *frame = talloc_stackframe();
4757 struct event_context *ev = NULL;
4758 struct tevent_req *req = NULL;
4759 NTSTATUS status = NT_STATUS_OK;
4761 if (cli_has_async_calls(cli)) {
4763 * Can't use sync call while an async call is in flight
4765 status = NT_STATUS_INVALID_PARAMETER;
4766 goto fail;
4769 ev = event_context_init(frame);
4770 if (ev == NULL) {
4771 status = NT_STATUS_NO_MEMORY;
4772 goto fail;
4775 req = cli_posix_rmdir_send(frame,
4777 cli,
4778 fname);
4779 if (req == NULL) {
4780 status = NT_STATUS_NO_MEMORY;
4781 goto fail;
4784 if (!tevent_req_poll(req, ev)) {
4785 status = map_nt_error_from_unix(errno);
4786 goto fail;
4789 status = cli_posix_rmdir_recv(req, frame);
4791 fail:
4792 TALLOC_FREE(frame);
4793 return status;
4796 /****************************************************************************
4797 filechangenotify
4798 ****************************************************************************/
4800 struct cli_notify_state {
4801 uint8_t setup[8];
4802 uint32_t num_changes;
4803 struct notify_change *changes;
4806 static void cli_notify_done(struct tevent_req *subreq);
4808 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4809 struct tevent_context *ev,
4810 struct cli_state *cli, uint16_t fnum,
4811 uint32_t buffer_size,
4812 uint32_t completion_filter, bool recursive)
4814 struct tevent_req *req, *subreq;
4815 struct cli_notify_state *state;
4817 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4818 if (req == NULL) {
4819 return NULL;
4822 SIVAL(state->setup, 0, completion_filter);
4823 SSVAL(state->setup, 4, fnum);
4824 SSVAL(state->setup, 6, recursive);
4826 subreq = cli_trans_send(
4827 state, /* mem ctx. */
4828 ev, /* event ctx. */
4829 cli, /* cli_state. */
4830 SMBnttrans, /* cmd. */
4831 NULL, /* pipe name. */
4832 -1, /* fid. */
4833 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4834 0, /* flags. */
4835 (uint16_t *)state->setup, /* setup. */
4836 4, /* num setup uint16_t words. */
4837 0, /* max returned setup. */
4838 NULL, /* param. */
4839 0, /* num param. */
4840 buffer_size, /* max returned param. */
4841 NULL, /* data. */
4842 0, /* num data. */
4843 0); /* max returned data. */
4845 if (tevent_req_nomem(subreq, req)) {
4846 return tevent_req_post(req, ev);
4848 tevent_req_set_callback(subreq, cli_notify_done, req);
4849 return req;
4852 static void cli_notify_done(struct tevent_req *subreq)
4854 struct tevent_req *req = tevent_req_callback_data(
4855 subreq, struct tevent_req);
4856 struct cli_notify_state *state = tevent_req_data(
4857 req, struct cli_notify_state);
4858 NTSTATUS status;
4859 uint8_t *params;
4860 uint32_t i, ofs, num_params;
4861 uint16_t flags2;
4863 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4864 &params, 0, &num_params, NULL, 0, NULL);
4865 TALLOC_FREE(subreq);
4866 if (tevent_req_nterror(req, status)) {
4867 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4868 return;
4871 state->num_changes = 0;
4872 ofs = 0;
4874 while (num_params - ofs > 12) {
4875 uint32_t len = IVAL(params, ofs);
4876 state->num_changes += 1;
4878 if ((len == 0) || (ofs+len >= num_params)) {
4879 break;
4881 ofs += len;
4884 state->changes = talloc_array(state, struct notify_change,
4885 state->num_changes);
4886 if (tevent_req_nomem(state->changes, req)) {
4887 TALLOC_FREE(params);
4888 return;
4891 ofs = 0;
4893 for (i=0; i<state->num_changes; i++) {
4894 uint32_t next = IVAL(params, ofs);
4895 uint32_t len = IVAL(params, ofs+8);
4896 ssize_t ret;
4897 char *name;
4899 if ((next != 0) && (len+12 != next)) {
4900 TALLOC_FREE(params);
4901 tevent_req_nterror(
4902 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4903 return;
4906 state->changes[i].action = IVAL(params, ofs+4);
4907 ret = clistr_pull_talloc(params, (char *)params, flags2,
4908 &name, params+ofs+12, len,
4909 STR_TERMINATE|STR_UNICODE);
4910 if (ret == -1) {
4911 TALLOC_FREE(params);
4912 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4913 return;
4915 state->changes[i].name = name;
4916 ofs += next;
4919 TALLOC_FREE(params);
4920 tevent_req_done(req);
4923 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4924 uint32_t *pnum_changes,
4925 struct notify_change **pchanges)
4927 struct cli_notify_state *state = tevent_req_data(
4928 req, struct cli_notify_state);
4929 NTSTATUS status;
4931 if (tevent_req_is_nterror(req, &status)) {
4932 return status;
4935 *pnum_changes = state->num_changes;
4936 *pchanges = talloc_move(mem_ctx, &state->changes);
4937 return NT_STATUS_OK;
4940 struct cli_qpathinfo_state {
4941 uint8_t *param;
4942 uint8_t *data;
4943 uint16_t setup[1];
4944 uint32_t min_rdata;
4945 uint8_t *rdata;
4946 uint32_t num_rdata;
4949 static void cli_qpathinfo_done(struct tevent_req *subreq);
4951 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4952 struct tevent_context *ev,
4953 struct cli_state *cli, const char *fname,
4954 uint16_t level, uint32_t min_rdata,
4955 uint32_t max_rdata)
4957 struct tevent_req *req, *subreq;
4958 struct cli_qpathinfo_state *state;
4960 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4961 if (req == NULL) {
4962 return NULL;
4964 state->min_rdata = min_rdata;
4965 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4967 state->param = talloc_zero_array(state, uint8_t, 6);
4968 if (tevent_req_nomem(state->param, req)) {
4969 return tevent_req_post(req, ev);
4971 SSVAL(state->param, 0, level);
4972 state->param = trans2_bytes_push_str(
4973 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4974 if (tevent_req_nomem(state->param, req)) {
4975 return tevent_req_post(req, ev);
4978 subreq = cli_trans_send(
4979 state, /* mem ctx. */
4980 ev, /* event ctx. */
4981 cli, /* cli_state. */
4982 SMBtrans2, /* cmd. */
4983 NULL, /* pipe name. */
4984 -1, /* fid. */
4985 0, /* function. */
4986 0, /* flags. */
4987 state->setup, /* setup. */
4988 1, /* num setup uint16_t words. */
4989 0, /* max returned setup. */
4990 state->param, /* param. */
4991 talloc_get_size(state->param), /* num param. */
4992 2, /* max returned param. */
4993 NULL, /* data. */
4994 0, /* num data. */
4995 max_rdata); /* max returned data. */
4997 if (tevent_req_nomem(subreq, req)) {
4998 return tevent_req_post(req, ev);
5000 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5001 return req;
5004 static void cli_qpathinfo_done(struct tevent_req *subreq)
5006 struct tevent_req *req = tevent_req_callback_data(
5007 subreq, struct tevent_req);
5008 struct cli_qpathinfo_state *state = tevent_req_data(
5009 req, struct cli_qpathinfo_state);
5010 NTSTATUS status;
5012 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5013 NULL, 0, NULL,
5014 &state->rdata, state->min_rdata,
5015 &state->num_rdata);
5016 if (tevent_req_nterror(req, status)) {
5017 return;
5019 tevent_req_done(req);
5022 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5023 uint8_t **rdata, uint32_t *num_rdata)
5025 struct cli_qpathinfo_state *state = tevent_req_data(
5026 req, struct cli_qpathinfo_state);
5027 NTSTATUS status;
5029 if (tevent_req_is_nterror(req, &status)) {
5030 return status;
5032 if (rdata != NULL) {
5033 *rdata = talloc_move(mem_ctx, &state->rdata);
5034 } else {
5035 TALLOC_FREE(state->rdata);
5037 if (num_rdata != NULL) {
5038 *num_rdata = state->num_rdata;
5040 return NT_STATUS_OK;
5043 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5044 const char *fname, uint16_t level, uint32_t min_rdata,
5045 uint32_t max_rdata,
5046 uint8_t **rdata, uint32_t *num_rdata)
5048 TALLOC_CTX *frame = talloc_stackframe();
5049 struct event_context *ev;
5050 struct tevent_req *req;
5051 NTSTATUS status = NT_STATUS_NO_MEMORY;
5053 if (cli_has_async_calls(cli)) {
5055 * Can't use sync call while an async call is in flight
5057 status = NT_STATUS_INVALID_PARAMETER;
5058 goto fail;
5060 ev = event_context_init(frame);
5061 if (ev == NULL) {
5062 goto fail;
5064 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5065 max_rdata);
5066 if (req == NULL) {
5067 goto fail;
5069 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5070 goto fail;
5072 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5073 fail:
5074 TALLOC_FREE(frame);
5075 return status;
5078 struct cli_qfileinfo_state {
5079 uint16_t setup[1];
5080 uint8_t param[4];
5081 uint8_t *data;
5082 uint16_t recv_flags2;
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,
5143 &state->recv_flags2,
5144 NULL, 0, NULL,
5145 NULL, 0, NULL,
5146 &state->rdata, state->min_rdata,
5147 &state->num_rdata);
5148 if (tevent_req_nterror(req, status)) {
5149 return;
5151 tevent_req_done(req);
5154 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5155 uint16_t *recv_flags2,
5156 uint8_t **rdata, uint32_t *num_rdata)
5158 struct cli_qfileinfo_state *state = tevent_req_data(
5159 req, struct cli_qfileinfo_state);
5160 NTSTATUS status;
5162 if (tevent_req_is_nterror(req, &status)) {
5163 return status;
5166 if (recv_flags2 != NULL) {
5167 *recv_flags2 = state->recv_flags2;
5169 if (rdata != NULL) {
5170 *rdata = talloc_move(mem_ctx, &state->rdata);
5171 } else {
5172 TALLOC_FREE(state->rdata);
5174 if (num_rdata != NULL) {
5175 *num_rdata = state->num_rdata;
5177 return NT_STATUS_OK;
5180 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5181 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5182 uint32_t max_rdata, uint16_t *recv_flags2,
5183 uint8_t **rdata, uint32_t *num_rdata)
5185 TALLOC_CTX *frame = talloc_stackframe();
5186 struct event_context *ev;
5187 struct tevent_req *req;
5188 NTSTATUS status = NT_STATUS_NO_MEMORY;
5190 if (cli_has_async_calls(cli)) {
5192 * Can't use sync call while an async call is in flight
5194 status = NT_STATUS_INVALID_PARAMETER;
5195 goto fail;
5197 ev = event_context_init(frame);
5198 if (ev == NULL) {
5199 goto fail;
5201 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5202 max_rdata);
5203 if (req == NULL) {
5204 goto fail;
5206 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5207 goto fail;
5209 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5210 fail:
5211 TALLOC_FREE(frame);
5212 return status;
5215 struct cli_flush_state {
5216 uint16_t vwv[1];
5219 static void cli_flush_done(struct tevent_req *subreq);
5221 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5222 struct event_context *ev,
5223 struct cli_state *cli,
5224 uint16_t fnum)
5226 struct tevent_req *req, *subreq;
5227 struct cli_flush_state *state;
5229 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5230 if (req == NULL) {
5231 return NULL;
5233 SSVAL(state->vwv + 0, 0, fnum);
5235 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5236 0, NULL);
5237 if (tevent_req_nomem(subreq, req)) {
5238 return tevent_req_post(req, ev);
5240 tevent_req_set_callback(subreq, cli_flush_done, req);
5241 return req;
5244 static void cli_flush_done(struct tevent_req *subreq)
5246 struct tevent_req *req = tevent_req_callback_data(
5247 subreq, struct tevent_req);
5248 NTSTATUS status;
5250 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5251 TALLOC_FREE(subreq);
5252 if (tevent_req_nterror(req, status)) {
5253 return;
5255 tevent_req_done(req);
5258 NTSTATUS cli_flush_recv(struct tevent_req *req)
5260 return tevent_req_simple_recv_ntstatus(req);
5263 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5265 TALLOC_CTX *frame = talloc_stackframe();
5266 struct event_context *ev;
5267 struct tevent_req *req;
5268 NTSTATUS status = NT_STATUS_NO_MEMORY;
5270 if (cli_has_async_calls(cli)) {
5272 * Can't use sync call while an async call is in flight
5274 status = NT_STATUS_INVALID_PARAMETER;
5275 goto fail;
5277 ev = event_context_init(frame);
5278 if (ev == NULL) {
5279 goto fail;
5281 req = cli_flush_send(frame, ev, cli, fnum);
5282 if (req == NULL) {
5283 goto fail;
5285 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5286 goto fail;
5288 status = cli_flush_recv(req);
5289 fail:
5290 TALLOC_FREE(frame);
5291 return status;
5294 struct cli_shadow_copy_data_state {
5295 uint16_t setup[4];
5296 uint8_t *data;
5297 uint32_t num_data;
5298 bool get_names;
5301 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5303 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5304 struct tevent_context *ev,
5305 struct cli_state *cli,
5306 uint16_t fnum,
5307 bool get_names)
5309 struct tevent_req *req, *subreq;
5310 struct cli_shadow_copy_data_state *state;
5311 uint32_t ret_size;
5313 req = tevent_req_create(mem_ctx, &state,
5314 struct cli_shadow_copy_data_state);
5315 if (req == NULL) {
5316 return NULL;
5318 state->get_names = get_names;
5319 ret_size = get_names ? cli->max_xmit : 16;
5321 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5322 SSVAL(state->setup + 2, 0, fnum);
5323 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5324 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5326 subreq = cli_trans_send(
5327 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5328 state->setup, ARRAY_SIZE(state->setup), 0,
5329 NULL, 0, 0,
5330 NULL, 0, ret_size);
5331 if (tevent_req_nomem(subreq, req)) {
5332 return tevent_req_post(req, ev);
5334 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5335 return req;
5338 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5340 struct tevent_req *req = tevent_req_callback_data(
5341 subreq, struct tevent_req);
5342 struct cli_shadow_copy_data_state *state = tevent_req_data(
5343 req, struct cli_shadow_copy_data_state);
5344 NTSTATUS status;
5346 status = cli_trans_recv(subreq, state, NULL,
5347 NULL, 0, NULL, /* setup */
5348 NULL, 0, NULL, /* param */
5349 &state->data, 12, &state->num_data);
5350 TALLOC_FREE(subreq);
5351 if (tevent_req_nterror(req, status)) {
5352 return;
5354 tevent_req_done(req);
5357 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5358 char ***pnames, int *pnum_names)
5360 struct cli_shadow_copy_data_state *state = tevent_req_data(
5361 req, struct cli_shadow_copy_data_state);
5362 char **names;
5363 int i, num_names;
5364 uint32_t dlength;
5365 NTSTATUS status;
5367 if (tevent_req_is_nterror(req, &status)) {
5368 return status;
5370 num_names = IVAL(state->data, 4);
5371 dlength = IVAL(state->data, 8);
5373 if (!state->get_names) {
5374 *pnum_names = num_names;
5375 return NT_STATUS_OK;
5378 if (dlength+12 > state->num_data) {
5379 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5381 names = talloc_array(mem_ctx, char *, num_names);
5382 if (names == NULL) {
5383 return NT_STATUS_NO_MEMORY;
5386 for (i=0; i<num_names; i++) {
5387 bool ret;
5388 uint8_t *src;
5389 size_t converted_size;
5391 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5392 ret = convert_string_talloc(
5393 names, CH_UTF16LE, CH_UNIX,
5394 src, 2 * sizeof(SHADOW_COPY_LABEL),
5395 &names[i], &converted_size);
5396 if (!ret) {
5397 TALLOC_FREE(names);
5398 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5401 *pnum_names = num_names;
5402 *pnames = names;
5403 return NT_STATUS_OK;
5406 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5407 uint16_t fnum, bool get_names,
5408 char ***pnames, int *pnum_names)
5410 TALLOC_CTX *frame = talloc_stackframe();
5411 struct event_context *ev;
5412 struct tevent_req *req;
5413 NTSTATUS status = NT_STATUS_NO_MEMORY;
5415 if (cli_has_async_calls(cli)) {
5417 * Can't use sync call while an async call is in flight
5419 status = NT_STATUS_INVALID_PARAMETER;
5420 goto fail;
5422 ev = event_context_init(frame);
5423 if (ev == NULL) {
5424 goto fail;
5426 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5427 if (req == NULL) {
5428 goto fail;
5430 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5431 goto fail;
5433 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5434 fail:
5435 TALLOC_FREE(frame);
5436 return status;