s3: Correctly align even
[Samba/bjacke.git] / source3 / libsmb / clifile.c
blob5f93ced6af62851f5c10c8e338c50513a89a37be
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
27 #include "trans2.h"
28 #include "ntioctl.h"
30 /***********************************************************
31 Common function for pushing stings, used by smb_bytes_push_str()
32 and trans_bytes_push_str(). Only difference is the align_odd
33 parameter setting.
34 ***********************************************************/
36 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
37 const char *str, size_t str_len,
38 bool align_odd,
39 size_t *pconverted_size)
41 size_t buflen;
42 char *converted;
43 size_t converted_size;
45 if (buf == NULL) {
46 return NULL;
49 buflen = talloc_get_size(buf);
51 if (ucs2 &&
52 ((align_odd && (buflen % 2 == 0)) ||
53 (!align_odd && (buflen % 2 == 1)))) {
55 * We're pushing into an SMB buffer, align odd
57 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
58 if (buf == NULL) {
59 return NULL;
61 buf[buflen] = '\0';
62 buflen += 1;
65 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
66 ucs2 ? CH_UTF16LE : CH_DOS,
67 str, str_len, &converted,
68 &converted_size)) {
69 return NULL;
72 buf = talloc_realloc(NULL, buf, uint8_t,
73 buflen + converted_size);
74 if (buf == NULL) {
75 TALLOC_FREE(converted);
76 return NULL;
79 memcpy(buf + buflen, converted, converted_size);
81 TALLOC_FREE(converted);
83 if (pconverted_size) {
84 *pconverted_size = converted_size;
87 return buf;
90 /***********************************************************
91 Push a string into an SMB buffer, with odd byte alignment
92 if it's a UCS2 string.
93 ***********************************************************/
95 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
96 const char *str, size_t str_len,
97 size_t *pconverted_size)
99 return internal_bytes_push_str(buf, ucs2, str, str_len,
100 true, pconverted_size);
103 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
104 const uint8_t *bytes, size_t num_bytes)
106 size_t buflen;
108 if (buf == NULL) {
109 return NULL;
111 buflen = talloc_get_size(buf);
113 buf = talloc_realloc(NULL, buf, uint8_t,
114 buflen + 1 + num_bytes);
115 if (buf == NULL) {
116 return NULL;
118 buf[buflen] = prefix;
119 memcpy(&buf[buflen+1], bytes, num_bytes);
120 return buf;
123 /***********************************************************
124 Same as smb_bytes_push_str(), but without the odd byte
125 align for ucs2 (we're pushing into a param or data block).
126 static for now, although this will probably change when
127 other modules use async trans calls.
128 ***********************************************************/
130 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
131 const char *str, size_t str_len,
132 size_t *pconverted_size)
134 return internal_bytes_push_str(buf, ucs2, str, str_len,
135 false, pconverted_size);
138 struct cli_setpathinfo_state {
139 uint16_t setup;
140 uint8_t *param;
143 static void cli_setpathinfo_done(struct tevent_req *subreq);
145 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
146 struct tevent_context *ev,
147 struct cli_state *cli,
148 uint16_t level,
149 const char *path,
150 uint8_t *data,
151 size_t data_len)
153 struct tevent_req *req, *subreq;
154 struct cli_setpathinfo_state *state;
156 req = tevent_req_create(mem_ctx, &state,
157 struct cli_setpathinfo_state);
158 if (req == NULL) {
159 return NULL;
162 /* Setup setup word. */
163 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
165 /* Setup param array. */
166 state->param = talloc_zero_array(state, uint8_t, 6);
167 if (tevent_req_nomem(state->param, req)) {
168 return tevent_req_post(req, ev);
170 SSVAL(state->param, 0, level);
172 state->param = trans2_bytes_push_str(
173 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
174 if (tevent_req_nomem(state->param, req)) {
175 return tevent_req_post(req, ev);
178 subreq = cli_trans_send(
179 state, /* mem ctx. */
180 ev, /* event ctx. */
181 cli, /* cli_state. */
182 SMBtrans2, /* cmd. */
183 NULL, /* pipe name. */
184 -1, /* fid. */
185 0, /* function. */
186 0, /* flags. */
187 &state->setup, /* setup. */
188 1, /* num setup uint16_t words. */
189 0, /* max returned setup. */
190 state->param, /* param. */
191 talloc_get_size(state->param), /* num param. */
192 2, /* max returned param. */
193 data, /* data. */
194 data_len, /* num data. */
195 0); /* max returned data. */
197 if (tevent_req_nomem(subreq, req)) {
198 return tevent_req_post(req, ev);
200 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
201 return req;
204 static void cli_setpathinfo_done(struct tevent_req *subreq)
206 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
207 NULL, 0, NULL, NULL, 0, NULL);
208 tevent_req_simple_finish_ntstatus(subreq, status);
211 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
213 return tevent_req_simple_recv_ntstatus(req);
216 NTSTATUS cli_setpathinfo(struct cli_state *cli,
217 uint16_t level,
218 const char *path,
219 uint8_t *data,
220 size_t data_len)
222 TALLOC_CTX *frame = talloc_stackframe();
223 struct tevent_context *ev;
224 struct tevent_req *req;
225 NTSTATUS status = NT_STATUS_NO_MEMORY;
227 if (cli_has_async_calls(cli)) {
229 * Can't use sync call while an async call is in flight
231 status = NT_STATUS_INVALID_PARAMETER;
232 goto fail;
234 ev = tevent_context_init(frame);
235 if (ev == NULL) {
236 goto fail;
238 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
239 if (req == NULL) {
240 goto fail;
242 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
243 goto fail;
245 status = cli_setpathinfo_recv(req);
246 fail:
247 TALLOC_FREE(frame);
248 return status;
251 /****************************************************************************
252 Hard/Symlink a file (UNIX extensions).
253 Creates new name (sym)linked to oldname.
254 ****************************************************************************/
256 struct cli_posix_link_internal_state {
257 uint8_t *data;
260 static void cli_posix_link_internal_done(struct tevent_req *subreq);
262 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
263 struct event_context *ev,
264 struct cli_state *cli,
265 uint16_t level,
266 const char *oldname,
267 const char *newname)
269 struct tevent_req *req = NULL, *subreq = NULL;
270 struct cli_posix_link_internal_state *state = NULL;
272 req = tevent_req_create(mem_ctx, &state,
273 struct cli_posix_link_internal_state);
274 if (req == NULL) {
275 return NULL;
278 /* Setup data array. */
279 state->data = talloc_array(state, uint8_t, 0);
280 if (tevent_req_nomem(state->data, req)) {
281 return tevent_req_post(req, ev);
283 state->data = trans2_bytes_push_str(
284 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
286 subreq = cli_setpathinfo_send(
287 state, ev, cli, level, newname,
288 state->data, talloc_get_size(state->data));
289 if (tevent_req_nomem(subreq, req)) {
290 return tevent_req_post(req, ev);
292 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
293 return req;
296 static void cli_posix_link_internal_done(struct tevent_req *subreq)
298 NTSTATUS status = cli_setpathinfo_recv(subreq);
299 tevent_req_simple_finish_ntstatus(subreq, status);
302 /****************************************************************************
303 Symlink a file (UNIX extensions).
304 ****************************************************************************/
306 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
307 struct event_context *ev,
308 struct cli_state *cli,
309 const char *oldname,
310 const char *newname)
312 return cli_posix_link_internal_send(
313 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
316 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
318 return tevent_req_simple_recv_ntstatus(req);
321 NTSTATUS cli_posix_symlink(struct cli_state *cli,
322 const char *oldname,
323 const char *newname)
325 TALLOC_CTX *frame = talloc_stackframe();
326 struct event_context *ev = NULL;
327 struct tevent_req *req = NULL;
328 NTSTATUS status = NT_STATUS_OK;
330 if (cli_has_async_calls(cli)) {
332 * Can't use sync call while an async call is in flight
334 status = NT_STATUS_INVALID_PARAMETER;
335 goto fail;
338 ev = event_context_init(frame);
339 if (ev == NULL) {
340 status = NT_STATUS_NO_MEMORY;
341 goto fail;
344 req = cli_posix_symlink_send(frame,
346 cli,
347 oldname,
348 newname);
349 if (req == NULL) {
350 status = NT_STATUS_NO_MEMORY;
351 goto fail;
354 if (!tevent_req_poll(req, ev)) {
355 status = map_nt_error_from_unix(errno);
356 goto fail;
359 status = cli_posix_symlink_recv(req);
361 fail:
362 TALLOC_FREE(frame);
363 return status;
366 /****************************************************************************
367 Read a POSIX symlink.
368 ****************************************************************************/
370 struct readlink_state {
371 uint8_t *data;
372 uint32_t num_data;
375 static void cli_posix_readlink_done(struct tevent_req *subreq);
377 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
378 struct event_context *ev,
379 struct cli_state *cli,
380 const char *fname,
381 size_t len)
383 struct tevent_req *req = NULL, *subreq = NULL;
384 struct readlink_state *state = NULL;
385 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
387 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
388 if (req == NULL) {
389 return NULL;
393 * Len is in bytes, we need it in UCS2 units.
395 if ((2*len < len) || (maxbytelen < len)) {
396 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
397 return tevent_req_post(req, ev);
400 subreq = cli_qpathinfo_send(state, ev, cli, fname,
401 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
402 if (tevent_req_nomem(subreq, req)) {
403 return tevent_req_post(req, ev);
405 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
406 return req;
409 static void cli_posix_readlink_done(struct tevent_req *subreq)
411 struct tevent_req *req = tevent_req_callback_data(
412 subreq, struct tevent_req);
413 struct readlink_state *state = tevent_req_data(
414 req, struct readlink_state);
415 NTSTATUS status;
417 status = cli_qpathinfo_recv(subreq, state, &state->data,
418 &state->num_data);
419 TALLOC_FREE(subreq);
420 if (tevent_req_nterror(req, status)) {
421 return;
424 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
426 if (state->data[state->num_data-1] != '\0') {
427 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
428 return;
430 tevent_req_done(req);
433 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
434 char *retpath, size_t len)
436 NTSTATUS status;
437 char *converted = NULL;
438 size_t converted_size = 0;
439 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
441 if (tevent_req_is_nterror(req, &status)) {
442 return status;
444 /* The returned data is a pushed string, not raw data. */
445 if (!convert_string_talloc(state,
446 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
447 CH_UNIX,
448 state->data,
449 state->num_data,
450 &converted,
451 &converted_size)) {
452 return NT_STATUS_NO_MEMORY;
455 len = MIN(len,converted_size);
456 if (len == 0) {
457 return NT_STATUS_DATA_ERROR;
459 memcpy(retpath, converted, len);
460 return NT_STATUS_OK;
463 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
464 char *linkpath, size_t len)
466 TALLOC_CTX *frame = talloc_stackframe();
467 struct event_context *ev = NULL;
468 struct tevent_req *req = NULL;
469 NTSTATUS status = NT_STATUS_OK;
471 if (cli_has_async_calls(cli)) {
473 * Can't use sync call while an async call is in flight
475 status = NT_STATUS_INVALID_PARAMETER;
476 goto fail;
479 ev = event_context_init(frame);
480 if (ev == NULL) {
481 status = NT_STATUS_NO_MEMORY;
482 goto fail;
485 req = cli_posix_readlink_send(frame,
487 cli,
488 fname,
489 len);
490 if (req == NULL) {
491 status = NT_STATUS_NO_MEMORY;
492 goto fail;
495 if (!tevent_req_poll(req, ev)) {
496 status = map_nt_error_from_unix(errno);
497 goto fail;
500 status = cli_posix_readlink_recv(req, cli, linkpath, len);
502 fail:
503 TALLOC_FREE(frame);
504 return status;
507 /****************************************************************************
508 Hard link a file (UNIX extensions).
509 ****************************************************************************/
511 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
512 struct event_context *ev,
513 struct cli_state *cli,
514 const char *oldname,
515 const char *newname)
517 return cli_posix_link_internal_send(
518 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
521 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
523 return tevent_req_simple_recv_ntstatus(req);
526 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
527 const char *oldname,
528 const char *newname)
530 TALLOC_CTX *frame = talloc_stackframe();
531 struct event_context *ev = NULL;
532 struct tevent_req *req = NULL;
533 NTSTATUS status = NT_STATUS_OK;
535 if (cli_has_async_calls(cli)) {
537 * Can't use sync call while an async call is in flight
539 status = NT_STATUS_INVALID_PARAMETER;
540 goto fail;
543 ev = event_context_init(frame);
544 if (ev == NULL) {
545 status = NT_STATUS_NO_MEMORY;
546 goto fail;
549 req = cli_posix_hardlink_send(frame,
551 cli,
552 oldname,
553 newname);
554 if (req == NULL) {
555 status = NT_STATUS_NO_MEMORY;
556 goto fail;
559 if (!tevent_req_poll(req, ev)) {
560 status = map_nt_error_from_unix(errno);
561 goto fail;
564 status = cli_posix_hardlink_recv(req);
566 fail:
567 TALLOC_FREE(frame);
568 return status;
571 /****************************************************************************
572 Do a POSIX getfacl (UNIX extensions).
573 ****************************************************************************/
575 struct getfacl_state {
576 uint32_t num_data;
577 uint8_t *data;
580 static void cli_posix_getfacl_done(struct tevent_req *subreq);
582 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
583 struct event_context *ev,
584 struct cli_state *cli,
585 const char *fname)
587 struct tevent_req *req = NULL, *subreq = NULL;
588 struct getfacl_state *state = NULL;
590 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
591 if (req == NULL) {
592 return NULL;
594 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
595 0, cli->max_xmit);
596 if (tevent_req_nomem(subreq, req)) {
597 return tevent_req_post(req, ev);
599 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
600 return req;
603 static void cli_posix_getfacl_done(struct tevent_req *subreq)
605 struct tevent_req *req = tevent_req_callback_data(
606 subreq, struct tevent_req);
607 struct getfacl_state *state = tevent_req_data(
608 req, struct getfacl_state);
609 NTSTATUS status;
611 status = cli_qpathinfo_recv(subreq, state, &state->data,
612 &state->num_data);
613 TALLOC_FREE(subreq);
614 if (tevent_req_nterror(req, status)) {
615 return;
617 tevent_req_done(req);
620 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
621 TALLOC_CTX *mem_ctx,
622 size_t *prb_size,
623 char **retbuf)
625 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
626 NTSTATUS status;
628 if (tevent_req_is_nterror(req, &status)) {
629 return status;
631 *prb_size = (size_t)state->num_data;
632 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
633 return NT_STATUS_OK;
636 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
637 const char *fname,
638 TALLOC_CTX *mem_ctx,
639 size_t *prb_size,
640 char **retbuf)
642 TALLOC_CTX *frame = talloc_stackframe();
643 struct event_context *ev = NULL;
644 struct tevent_req *req = NULL;
645 NTSTATUS status = NT_STATUS_OK;
647 if (cli_has_async_calls(cli)) {
649 * Can't use sync call while an async call is in flight
651 status = NT_STATUS_INVALID_PARAMETER;
652 goto fail;
655 ev = event_context_init(frame);
656 if (ev == NULL) {
657 status = NT_STATUS_NO_MEMORY;
658 goto fail;
661 req = cli_posix_getfacl_send(frame,
663 cli,
664 fname);
665 if (req == NULL) {
666 status = NT_STATUS_NO_MEMORY;
667 goto fail;
670 if (!tevent_req_poll(req, ev)) {
671 status = map_nt_error_from_unix(errno);
672 goto fail;
675 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
677 fail:
678 TALLOC_FREE(frame);
679 return status;
682 /****************************************************************************
683 Stat a file (UNIX extensions).
684 ****************************************************************************/
686 struct stat_state {
687 uint32_t num_data;
688 uint8_t *data;
691 static void cli_posix_stat_done(struct tevent_req *subreq);
693 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
694 struct event_context *ev,
695 struct cli_state *cli,
696 const char *fname)
698 struct tevent_req *req = NULL, *subreq = NULL;
699 struct stat_state *state = NULL;
701 req = tevent_req_create(mem_ctx, &state, struct stat_state);
702 if (req == NULL) {
703 return NULL;
705 subreq = cli_qpathinfo_send(state, ev, cli, fname,
706 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
707 if (tevent_req_nomem(subreq, req)) {
708 return tevent_req_post(req, ev);
710 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
711 return req;
714 static void cli_posix_stat_done(struct tevent_req *subreq)
716 struct tevent_req *req = tevent_req_callback_data(
717 subreq, struct tevent_req);
718 struct stat_state *state = tevent_req_data(req, struct stat_state);
719 NTSTATUS status;
721 status = cli_qpathinfo_recv(subreq, state, &state->data,
722 &state->num_data);
723 TALLOC_FREE(subreq);
724 if (tevent_req_nterror(req, status)) {
725 return;
727 tevent_req_done(req);
730 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
731 SMB_STRUCT_STAT *sbuf)
733 struct stat_state *state = tevent_req_data(req, struct stat_state);
734 NTSTATUS status;
736 if (tevent_req_is_nterror(req, &status)) {
737 return status;
740 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
741 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
742 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
743 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
744 #else
745 /* assume 512 byte blocks */
746 sbuf->st_ex_blocks /= 512;
747 #endif
748 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
749 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
750 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
752 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
753 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
754 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
755 #if defined(HAVE_MAKEDEV)
757 uint32_t dev_major = IVAL(state->data,60);
758 uint32_t dev_minor = IVAL(state->data,68);
759 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
761 #endif
762 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
763 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
764 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
766 return NT_STATUS_OK;
769 NTSTATUS cli_posix_stat(struct cli_state *cli,
770 const char *fname,
771 SMB_STRUCT_STAT *sbuf)
773 TALLOC_CTX *frame = talloc_stackframe();
774 struct event_context *ev = NULL;
775 struct tevent_req *req = NULL;
776 NTSTATUS status = NT_STATUS_OK;
778 if (cli_has_async_calls(cli)) {
780 * Can't use sync call while an async call is in flight
782 status = NT_STATUS_INVALID_PARAMETER;
783 goto fail;
786 ev = event_context_init(frame);
787 if (ev == NULL) {
788 status = NT_STATUS_NO_MEMORY;
789 goto fail;
792 req = cli_posix_stat_send(frame,
794 cli,
795 fname);
796 if (req == NULL) {
797 status = NT_STATUS_NO_MEMORY;
798 goto fail;
801 if (!tevent_req_poll(req, ev)) {
802 status = map_nt_error_from_unix(errno);
803 goto fail;
806 status = cli_posix_stat_recv(req, sbuf);
808 fail:
809 TALLOC_FREE(frame);
810 return status;
813 /****************************************************************************
814 Chmod or chown a file internal (UNIX extensions).
815 ****************************************************************************/
817 struct cli_posix_chown_chmod_internal_state {
818 uint8_t data[100];
821 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
823 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
824 struct event_context *ev,
825 struct cli_state *cli,
826 const char *fname,
827 uint32_t mode,
828 uint32_t uid,
829 uint32_t gid)
831 struct tevent_req *req = NULL, *subreq = NULL;
832 struct cli_posix_chown_chmod_internal_state *state = NULL;
834 req = tevent_req_create(mem_ctx, &state,
835 struct cli_posix_chown_chmod_internal_state);
836 if (req == NULL) {
837 return NULL;
840 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
841 memset(&state->data[40], '\0', 60);
842 SIVAL(state->data,40,uid);
843 SIVAL(state->data,48,gid);
844 SIVAL(state->data,84,mode);
846 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
847 fname, state->data, sizeof(state->data));
848 if (tevent_req_nomem(subreq, req)) {
849 return tevent_req_post(req, ev);
851 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
852 req);
853 return req;
856 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
858 NTSTATUS status = cli_setpathinfo_recv(subreq);
859 tevent_req_simple_finish_ntstatus(subreq, status);
862 /****************************************************************************
863 chmod a file (UNIX extensions).
864 ****************************************************************************/
866 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
867 struct event_context *ev,
868 struct cli_state *cli,
869 const char *fname,
870 mode_t mode)
872 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
873 fname,
874 unix_perms_to_wire(mode),
875 SMB_UID_NO_CHANGE,
876 SMB_GID_NO_CHANGE);
879 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
881 return tevent_req_simple_recv_ntstatus(req);
884 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
886 TALLOC_CTX *frame = talloc_stackframe();
887 struct event_context *ev = NULL;
888 struct tevent_req *req = NULL;
889 NTSTATUS status = NT_STATUS_OK;
891 if (cli_has_async_calls(cli)) {
893 * Can't use sync call while an async call is in flight
895 status = NT_STATUS_INVALID_PARAMETER;
896 goto fail;
899 ev = event_context_init(frame);
900 if (ev == NULL) {
901 status = NT_STATUS_NO_MEMORY;
902 goto fail;
905 req = cli_posix_chmod_send(frame,
907 cli,
908 fname,
909 mode);
910 if (req == NULL) {
911 status = NT_STATUS_NO_MEMORY;
912 goto fail;
915 if (!tevent_req_poll(req, ev)) {
916 status = map_nt_error_from_unix(errno);
917 goto fail;
920 status = cli_posix_chmod_recv(req);
922 fail:
923 TALLOC_FREE(frame);
924 return status;
927 /****************************************************************************
928 chown a file (UNIX extensions).
929 ****************************************************************************/
931 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
932 struct event_context *ev,
933 struct cli_state *cli,
934 const char *fname,
935 uid_t uid,
936 gid_t gid)
938 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
939 fname,
940 SMB_MODE_NO_CHANGE,
941 (uint32_t)uid,
942 (uint32_t)gid);
945 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
947 return tevent_req_simple_recv_ntstatus(req);
950 NTSTATUS cli_posix_chown(struct cli_state *cli,
951 const char *fname,
952 uid_t uid,
953 gid_t gid)
955 TALLOC_CTX *frame = talloc_stackframe();
956 struct event_context *ev = NULL;
957 struct tevent_req *req = NULL;
958 NTSTATUS status = NT_STATUS_OK;
960 if (cli_has_async_calls(cli)) {
962 * Can't use sync call while an async call is in flight
964 status = NT_STATUS_INVALID_PARAMETER;
965 goto fail;
968 ev = event_context_init(frame);
969 if (ev == NULL) {
970 status = NT_STATUS_NO_MEMORY;
971 goto fail;
974 req = cli_posix_chown_send(frame,
976 cli,
977 fname,
978 uid,
979 gid);
980 if (req == NULL) {
981 status = NT_STATUS_NO_MEMORY;
982 goto fail;
985 if (!tevent_req_poll(req, ev)) {
986 status = map_nt_error_from_unix(errno);
987 goto fail;
990 status = cli_posix_chown_recv(req);
992 fail:
993 TALLOC_FREE(frame);
994 return status;
997 /****************************************************************************
998 Rename a file.
999 ****************************************************************************/
1001 static void cli_rename_done(struct tevent_req *subreq);
1003 struct cli_rename_state {
1004 uint16_t vwv[1];
1007 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1008 struct event_context *ev,
1009 struct cli_state *cli,
1010 const char *fname_src,
1011 const char *fname_dst)
1013 struct tevent_req *req = NULL, *subreq = NULL;
1014 struct cli_rename_state *state = NULL;
1015 uint8_t additional_flags = 0;
1016 uint8_t *bytes = NULL;
1018 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1019 if (req == NULL) {
1020 return NULL;
1023 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1025 bytes = talloc_array(state, uint8_t, 1);
1026 if (tevent_req_nomem(bytes, req)) {
1027 return tevent_req_post(req, ev);
1029 bytes[0] = 4;
1030 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1031 strlen(fname_src)+1, NULL);
1032 if (tevent_req_nomem(bytes, req)) {
1033 return tevent_req_post(req, ev);
1036 bytes = talloc_realloc(state, bytes, uint8_t,
1037 talloc_get_size(bytes)+1);
1038 if (tevent_req_nomem(bytes, req)) {
1039 return tevent_req_post(req, ev);
1042 bytes[talloc_get_size(bytes)-1] = 4;
1043 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1044 strlen(fname_dst)+1, NULL);
1045 if (tevent_req_nomem(bytes, req)) {
1046 return tevent_req_post(req, ev);
1049 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1050 1, state->vwv, talloc_get_size(bytes), bytes);
1051 if (tevent_req_nomem(subreq, req)) {
1052 return tevent_req_post(req, ev);
1054 tevent_req_set_callback(subreq, cli_rename_done, req);
1055 return req;
1058 static void cli_rename_done(struct tevent_req *subreq)
1060 struct tevent_req *req = tevent_req_callback_data(
1061 subreq, struct tevent_req);
1062 NTSTATUS status;
1064 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1065 TALLOC_FREE(subreq);
1066 if (tevent_req_nterror(req, status)) {
1067 return;
1069 tevent_req_done(req);
1072 NTSTATUS cli_rename_recv(struct tevent_req *req)
1074 return tevent_req_simple_recv_ntstatus(req);
1077 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1079 TALLOC_CTX *frame = talloc_stackframe();
1080 struct event_context *ev;
1081 struct tevent_req *req;
1082 NTSTATUS status = NT_STATUS_OK;
1084 if (cli_has_async_calls(cli)) {
1086 * Can't use sync call while an async call is in flight
1088 status = NT_STATUS_INVALID_PARAMETER;
1089 goto fail;
1092 ev = event_context_init(frame);
1093 if (ev == NULL) {
1094 status = NT_STATUS_NO_MEMORY;
1095 goto fail;
1098 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1099 if (req == NULL) {
1100 status = NT_STATUS_NO_MEMORY;
1101 goto fail;
1104 if (!tevent_req_poll(req, ev)) {
1105 status = map_nt_error_from_unix(errno);
1106 goto fail;
1109 status = cli_rename_recv(req);
1111 fail:
1112 TALLOC_FREE(frame);
1113 return status;
1116 /****************************************************************************
1117 NT Rename a file.
1118 ****************************************************************************/
1120 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1122 struct cli_ntrename_internal_state {
1123 uint16_t vwv[4];
1126 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1127 struct event_context *ev,
1128 struct cli_state *cli,
1129 const char *fname_src,
1130 const char *fname_dst,
1131 uint16_t rename_flag)
1133 struct tevent_req *req = NULL, *subreq = NULL;
1134 struct cli_ntrename_internal_state *state = NULL;
1135 uint8_t additional_flags = 0;
1136 uint8_t *bytes = NULL;
1138 req = tevent_req_create(mem_ctx, &state,
1139 struct cli_ntrename_internal_state);
1140 if (req == NULL) {
1141 return NULL;
1144 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1145 SSVAL(state->vwv+1, 0, rename_flag);
1147 bytes = talloc_array(state, uint8_t, 1);
1148 if (tevent_req_nomem(bytes, req)) {
1149 return tevent_req_post(req, ev);
1151 bytes[0] = 4;
1152 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1153 strlen(fname_src)+1, NULL);
1154 if (tevent_req_nomem(bytes, req)) {
1155 return tevent_req_post(req, ev);
1158 bytes = talloc_realloc(state, bytes, uint8_t,
1159 talloc_get_size(bytes)+1);
1160 if (tevent_req_nomem(bytes, req)) {
1161 return tevent_req_post(req, ev);
1164 bytes[talloc_get_size(bytes)-1] = 4;
1165 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1166 strlen(fname_dst)+1, NULL);
1167 if (tevent_req_nomem(bytes, req)) {
1168 return tevent_req_post(req, ev);
1171 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1172 4, state->vwv, talloc_get_size(bytes), bytes);
1173 if (tevent_req_nomem(subreq, req)) {
1174 return tevent_req_post(req, ev);
1176 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1177 return req;
1180 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1182 struct tevent_req *req = tevent_req_callback_data(
1183 subreq, struct tevent_req);
1184 NTSTATUS status;
1186 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1187 TALLOC_FREE(subreq);
1188 if (tevent_req_nterror(req, status)) {
1189 return;
1191 tevent_req_done(req);
1194 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1196 return tevent_req_simple_recv_ntstatus(req);
1199 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1200 struct event_context *ev,
1201 struct cli_state *cli,
1202 const char *fname_src,
1203 const char *fname_dst)
1205 return cli_ntrename_internal_send(mem_ctx,
1207 cli,
1208 fname_src,
1209 fname_dst,
1210 RENAME_FLAG_RENAME);
1213 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1215 return cli_ntrename_internal_recv(req);
1218 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1220 TALLOC_CTX *frame = talloc_stackframe();
1221 struct event_context *ev;
1222 struct tevent_req *req;
1223 NTSTATUS status = NT_STATUS_OK;
1225 if (cli_has_async_calls(cli)) {
1227 * Can't use sync call while an async call is in flight
1229 status = NT_STATUS_INVALID_PARAMETER;
1230 goto fail;
1233 ev = event_context_init(frame);
1234 if (ev == NULL) {
1235 status = NT_STATUS_NO_MEMORY;
1236 goto fail;
1239 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1240 if (req == NULL) {
1241 status = NT_STATUS_NO_MEMORY;
1242 goto fail;
1245 if (!tevent_req_poll(req, ev)) {
1246 status = map_nt_error_from_unix(errno);
1247 goto fail;
1250 status = cli_ntrename_recv(req);
1252 fail:
1253 TALLOC_FREE(frame);
1254 return status;
1257 /****************************************************************************
1258 NT hardlink a file.
1259 ****************************************************************************/
1261 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1262 struct event_context *ev,
1263 struct cli_state *cli,
1264 const char *fname_src,
1265 const char *fname_dst)
1267 return cli_ntrename_internal_send(mem_ctx,
1269 cli,
1270 fname_src,
1271 fname_dst,
1272 RENAME_FLAG_HARD_LINK);
1275 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1277 return cli_ntrename_internal_recv(req);
1280 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1282 TALLOC_CTX *frame = talloc_stackframe();
1283 struct event_context *ev;
1284 struct tevent_req *req;
1285 NTSTATUS status = NT_STATUS_OK;
1287 if (cli_has_async_calls(cli)) {
1289 * Can't use sync call while an async call is in flight
1291 status = NT_STATUS_INVALID_PARAMETER;
1292 goto fail;
1295 ev = event_context_init(frame);
1296 if (ev == NULL) {
1297 status = NT_STATUS_NO_MEMORY;
1298 goto fail;
1301 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1302 if (req == NULL) {
1303 status = NT_STATUS_NO_MEMORY;
1304 goto fail;
1307 if (!tevent_req_poll(req, ev)) {
1308 status = map_nt_error_from_unix(errno);
1309 goto fail;
1312 status = cli_nt_hardlink_recv(req);
1314 fail:
1315 TALLOC_FREE(frame);
1316 return status;
1319 /****************************************************************************
1320 Delete a file.
1321 ****************************************************************************/
1323 static void cli_unlink_done(struct tevent_req *subreq);
1325 struct cli_unlink_state {
1326 uint16_t vwv[1];
1329 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1330 struct event_context *ev,
1331 struct cli_state *cli,
1332 const char *fname,
1333 uint16_t mayhave_attrs)
1335 struct tevent_req *req = NULL, *subreq = NULL;
1336 struct cli_unlink_state *state = NULL;
1337 uint8_t additional_flags = 0;
1338 uint8_t *bytes = NULL;
1340 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1341 if (req == NULL) {
1342 return NULL;
1345 SSVAL(state->vwv+0, 0, mayhave_attrs);
1347 bytes = talloc_array(state, uint8_t, 1);
1348 if (tevent_req_nomem(bytes, req)) {
1349 return tevent_req_post(req, ev);
1351 bytes[0] = 4;
1352 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1353 strlen(fname)+1, NULL);
1355 if (tevent_req_nomem(bytes, req)) {
1356 return tevent_req_post(req, ev);
1359 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1360 1, state->vwv, talloc_get_size(bytes), bytes);
1361 if (tevent_req_nomem(subreq, req)) {
1362 return tevent_req_post(req, ev);
1364 tevent_req_set_callback(subreq, cli_unlink_done, req);
1365 return req;
1368 static void cli_unlink_done(struct tevent_req *subreq)
1370 struct tevent_req *req = tevent_req_callback_data(
1371 subreq, struct tevent_req);
1372 NTSTATUS status;
1374 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1375 TALLOC_FREE(subreq);
1376 if (tevent_req_nterror(req, status)) {
1377 return;
1379 tevent_req_done(req);
1382 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1384 return tevent_req_simple_recv_ntstatus(req);
1387 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1389 TALLOC_CTX *frame = talloc_stackframe();
1390 struct event_context *ev;
1391 struct tevent_req *req;
1392 NTSTATUS status = NT_STATUS_OK;
1394 if (cli_has_async_calls(cli)) {
1396 * Can't use sync call while an async call is in flight
1398 status = NT_STATUS_INVALID_PARAMETER;
1399 goto fail;
1402 ev = event_context_init(frame);
1403 if (ev == NULL) {
1404 status = NT_STATUS_NO_MEMORY;
1405 goto fail;
1408 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1409 if (req == NULL) {
1410 status = NT_STATUS_NO_MEMORY;
1411 goto fail;
1414 if (!tevent_req_poll(req, ev)) {
1415 status = map_nt_error_from_unix(errno);
1416 goto fail;
1419 status = cli_unlink_recv(req);
1421 fail:
1422 TALLOC_FREE(frame);
1423 return status;
1426 /****************************************************************************
1427 Create a directory.
1428 ****************************************************************************/
1430 static void cli_mkdir_done(struct tevent_req *subreq);
1432 struct cli_mkdir_state {
1433 int dummy;
1436 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1437 struct event_context *ev,
1438 struct cli_state *cli,
1439 const char *dname)
1441 struct tevent_req *req = NULL, *subreq = NULL;
1442 struct cli_mkdir_state *state = NULL;
1443 uint8_t additional_flags = 0;
1444 uint8_t *bytes = NULL;
1446 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1447 if (req == NULL) {
1448 return NULL;
1451 bytes = talloc_array(state, uint8_t, 1);
1452 if (tevent_req_nomem(bytes, req)) {
1453 return tevent_req_post(req, ev);
1455 bytes[0] = 4;
1456 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1457 strlen(dname)+1, NULL);
1459 if (tevent_req_nomem(bytes, req)) {
1460 return tevent_req_post(req, ev);
1463 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1464 0, NULL, talloc_get_size(bytes), bytes);
1465 if (tevent_req_nomem(subreq, req)) {
1466 return tevent_req_post(req, ev);
1468 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1469 return req;
1472 static void cli_mkdir_done(struct tevent_req *subreq)
1474 struct tevent_req *req = tevent_req_callback_data(
1475 subreq, struct tevent_req);
1476 NTSTATUS status;
1478 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1479 TALLOC_FREE(subreq);
1480 if (tevent_req_nterror(req, status)) {
1481 return;
1483 tevent_req_done(req);
1486 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1488 return tevent_req_simple_recv_ntstatus(req);
1491 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1493 TALLOC_CTX *frame = talloc_stackframe();
1494 struct event_context *ev;
1495 struct tevent_req *req;
1496 NTSTATUS status = NT_STATUS_OK;
1498 if (cli_has_async_calls(cli)) {
1500 * Can't use sync call while an async call is in flight
1502 status = NT_STATUS_INVALID_PARAMETER;
1503 goto fail;
1506 ev = event_context_init(frame);
1507 if (ev == NULL) {
1508 status = NT_STATUS_NO_MEMORY;
1509 goto fail;
1512 req = cli_mkdir_send(frame, ev, cli, dname);
1513 if (req == NULL) {
1514 status = NT_STATUS_NO_MEMORY;
1515 goto fail;
1518 if (!tevent_req_poll(req, ev)) {
1519 status = map_nt_error_from_unix(errno);
1520 goto fail;
1523 status = cli_mkdir_recv(req);
1525 fail:
1526 TALLOC_FREE(frame);
1527 return status;
1530 /****************************************************************************
1531 Remove a directory.
1532 ****************************************************************************/
1534 static void cli_rmdir_done(struct tevent_req *subreq);
1536 struct cli_rmdir_state {
1537 int dummy;
1540 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1541 struct event_context *ev,
1542 struct cli_state *cli,
1543 const char *dname)
1545 struct tevent_req *req = NULL, *subreq = NULL;
1546 struct cli_rmdir_state *state = NULL;
1547 uint8_t additional_flags = 0;
1548 uint8_t *bytes = NULL;
1550 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1551 if (req == NULL) {
1552 return NULL;
1555 bytes = talloc_array(state, uint8_t, 1);
1556 if (tevent_req_nomem(bytes, req)) {
1557 return tevent_req_post(req, ev);
1559 bytes[0] = 4;
1560 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1561 strlen(dname)+1, NULL);
1563 if (tevent_req_nomem(bytes, req)) {
1564 return tevent_req_post(req, ev);
1567 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1568 0, NULL, talloc_get_size(bytes), bytes);
1569 if (tevent_req_nomem(subreq, req)) {
1570 return tevent_req_post(req, ev);
1572 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1573 return req;
1576 static void cli_rmdir_done(struct tevent_req *subreq)
1578 struct tevent_req *req = tevent_req_callback_data(
1579 subreq, struct tevent_req);
1580 NTSTATUS status;
1582 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1583 TALLOC_FREE(subreq);
1584 if (tevent_req_nterror(req, status)) {
1585 return;
1587 tevent_req_done(req);
1590 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1592 return tevent_req_simple_recv_ntstatus(req);
1595 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1597 TALLOC_CTX *frame = talloc_stackframe();
1598 struct event_context *ev;
1599 struct tevent_req *req;
1600 NTSTATUS status = NT_STATUS_OK;
1602 if (cli_has_async_calls(cli)) {
1604 * Can't use sync call while an async call is in flight
1606 status = NT_STATUS_INVALID_PARAMETER;
1607 goto fail;
1610 ev = event_context_init(frame);
1611 if (ev == NULL) {
1612 status = NT_STATUS_NO_MEMORY;
1613 goto fail;
1616 req = cli_rmdir_send(frame, ev, cli, dname);
1617 if (req == NULL) {
1618 status = NT_STATUS_NO_MEMORY;
1619 goto fail;
1622 if (!tevent_req_poll(req, ev)) {
1623 status = map_nt_error_from_unix(errno);
1624 goto fail;
1627 status = cli_rmdir_recv(req);
1629 fail:
1630 TALLOC_FREE(frame);
1631 return status;
1634 /****************************************************************************
1635 Set or clear the delete on close flag.
1636 ****************************************************************************/
1638 struct doc_state {
1639 uint16_t setup;
1640 uint8_t param[6];
1641 uint8_t data[1];
1644 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1646 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1647 NULL, 0, NULL, NULL, 0, NULL);
1648 tevent_req_simple_finish_ntstatus(subreq, status);
1651 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1652 struct event_context *ev,
1653 struct cli_state *cli,
1654 uint16_t fnum,
1655 bool flag)
1657 struct tevent_req *req = NULL, *subreq = NULL;
1658 struct doc_state *state = NULL;
1660 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1661 if (req == NULL) {
1662 return NULL;
1665 /* Setup setup word. */
1666 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1668 /* Setup param array. */
1669 SSVAL(state->param,0,fnum);
1670 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1672 /* Setup data array. */
1673 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1675 subreq = cli_trans_send(state, /* mem ctx. */
1676 ev, /* event ctx. */
1677 cli, /* cli_state. */
1678 SMBtrans2, /* cmd. */
1679 NULL, /* pipe name. */
1680 -1, /* fid. */
1681 0, /* function. */
1682 0, /* flags. */
1683 &state->setup, /* setup. */
1684 1, /* num setup uint16_t words. */
1685 0, /* max returned setup. */
1686 state->param, /* param. */
1687 6, /* num param. */
1688 2, /* max returned param. */
1689 state->data, /* data. */
1690 1, /* num data. */
1691 0); /* max returned data. */
1693 if (tevent_req_nomem(subreq, req)) {
1694 return tevent_req_post(req, ev);
1696 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1697 return req;
1700 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1702 return tevent_req_simple_recv_ntstatus(req);
1705 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1707 TALLOC_CTX *frame = talloc_stackframe();
1708 struct event_context *ev = NULL;
1709 struct tevent_req *req = NULL;
1710 NTSTATUS status = NT_STATUS_OK;
1712 if (cli_has_async_calls(cli)) {
1714 * Can't use sync call while an async call is in flight
1716 status = NT_STATUS_INVALID_PARAMETER;
1717 goto fail;
1720 ev = event_context_init(frame);
1721 if (ev == NULL) {
1722 status = NT_STATUS_NO_MEMORY;
1723 goto fail;
1726 req = cli_nt_delete_on_close_send(frame,
1728 cli,
1729 fnum,
1730 flag);
1731 if (req == NULL) {
1732 status = NT_STATUS_NO_MEMORY;
1733 goto fail;
1736 if (!tevent_req_poll(req, ev)) {
1737 status = map_nt_error_from_unix(errno);
1738 goto fail;
1741 status = cli_nt_delete_on_close_recv(req);
1743 fail:
1744 TALLOC_FREE(frame);
1745 return status;
1748 struct cli_ntcreate_state {
1749 uint16_t vwv[24];
1750 uint16_t fnum;
1753 static void cli_ntcreate_done(struct tevent_req *subreq);
1755 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1756 struct event_context *ev,
1757 struct cli_state *cli,
1758 const char *fname,
1759 uint32_t CreatFlags,
1760 uint32_t DesiredAccess,
1761 uint32_t FileAttributes,
1762 uint32_t ShareAccess,
1763 uint32_t CreateDisposition,
1764 uint32_t CreateOptions,
1765 uint8_t SecurityFlags)
1767 struct tevent_req *req, *subreq;
1768 struct cli_ntcreate_state *state;
1769 uint16_t *vwv;
1770 uint8_t *bytes;
1771 size_t converted_len;
1773 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1774 if (req == NULL) {
1775 return NULL;
1778 vwv = state->vwv;
1780 SCVAL(vwv+0, 0, 0xFF);
1781 SCVAL(vwv+0, 1, 0);
1782 SSVAL(vwv+1, 0, 0);
1783 SCVAL(vwv+2, 0, 0);
1785 if (cli->use_oplocks) {
1786 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1788 SIVAL(vwv+3, 1, CreatFlags);
1789 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1790 SIVAL(vwv+7, 1, DesiredAccess);
1791 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1792 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1793 SIVAL(vwv+13, 1, FileAttributes);
1794 SIVAL(vwv+15, 1, ShareAccess);
1795 SIVAL(vwv+17, 1, CreateDisposition);
1796 SIVAL(vwv+19, 1, CreateOptions);
1797 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1798 SCVAL(vwv+23, 1, SecurityFlags);
1800 bytes = talloc_array(state, uint8_t, 0);
1801 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1802 fname, strlen(fname)+1,
1803 &converted_len);
1805 /* sigh. this copes with broken netapp filer behaviour */
1806 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1808 if (tevent_req_nomem(bytes, req)) {
1809 return tevent_req_post(req, ev);
1812 SSVAL(vwv+2, 1, converted_len);
1814 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1815 talloc_get_size(bytes), bytes);
1816 if (tevent_req_nomem(subreq, req)) {
1817 return tevent_req_post(req, ev);
1819 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1820 return req;
1823 static void cli_ntcreate_done(struct tevent_req *subreq)
1825 struct tevent_req *req = tevent_req_callback_data(
1826 subreq, struct tevent_req);
1827 struct cli_ntcreate_state *state = tevent_req_data(
1828 req, struct cli_ntcreate_state);
1829 uint8_t wct;
1830 uint16_t *vwv;
1831 uint32_t num_bytes;
1832 uint8_t *bytes;
1833 uint8_t *inbuf;
1834 NTSTATUS status;
1836 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1837 &num_bytes, &bytes);
1838 TALLOC_FREE(subreq);
1839 if (tevent_req_nterror(req, status)) {
1840 return;
1842 state->fnum = SVAL(vwv+2, 1);
1843 tevent_req_done(req);
1846 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1848 struct cli_ntcreate_state *state = tevent_req_data(
1849 req, struct cli_ntcreate_state);
1850 NTSTATUS status;
1852 if (tevent_req_is_nterror(req, &status)) {
1853 return status;
1855 *pfnum = state->fnum;
1856 return NT_STATUS_OK;
1859 NTSTATUS cli_ntcreate(struct cli_state *cli,
1860 const char *fname,
1861 uint32_t CreatFlags,
1862 uint32_t DesiredAccess,
1863 uint32_t FileAttributes,
1864 uint32_t ShareAccess,
1865 uint32_t CreateDisposition,
1866 uint32_t CreateOptions,
1867 uint8_t SecurityFlags,
1868 uint16_t *pfid)
1870 TALLOC_CTX *frame = talloc_stackframe();
1871 struct event_context *ev;
1872 struct tevent_req *req;
1873 NTSTATUS status = NT_STATUS_OK;
1875 if (cli_has_async_calls(cli)) {
1877 * Can't use sync call while an async call is in flight
1879 status = NT_STATUS_INVALID_PARAMETER;
1880 goto fail;
1883 ev = event_context_init(frame);
1884 if (ev == NULL) {
1885 status = NT_STATUS_NO_MEMORY;
1886 goto fail;
1889 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1890 DesiredAccess, FileAttributes, ShareAccess,
1891 CreateDisposition, CreateOptions,
1892 SecurityFlags);
1893 if (req == NULL) {
1894 status = NT_STATUS_NO_MEMORY;
1895 goto fail;
1898 if (!tevent_req_poll(req, ev)) {
1899 status = map_nt_error_from_unix(errno);
1900 goto fail;
1903 status = cli_ntcreate_recv(req, pfid);
1904 fail:
1905 TALLOC_FREE(frame);
1906 return status;
1909 /****************************************************************************
1910 Open a file
1911 WARNING: if you open with O_WRONLY then getattrE won't work!
1912 ****************************************************************************/
1914 struct cli_open_state {
1915 uint16_t vwv[15];
1916 uint16_t fnum;
1917 struct iovec bytes;
1920 static void cli_open_done(struct tevent_req *subreq);
1922 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1923 struct event_context *ev,
1924 struct cli_state *cli, const char *fname,
1925 int flags, int share_mode,
1926 struct tevent_req **psmbreq)
1928 struct tevent_req *req, *subreq;
1929 struct cli_open_state *state;
1930 unsigned openfn;
1931 unsigned accessmode;
1932 uint8_t additional_flags;
1933 uint8_t *bytes;
1935 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1936 if (req == NULL) {
1937 return NULL;
1940 openfn = 0;
1941 if (flags & O_CREAT) {
1942 openfn |= (1<<4);
1944 if (!(flags & O_EXCL)) {
1945 if (flags & O_TRUNC)
1946 openfn |= (1<<1);
1947 else
1948 openfn |= (1<<0);
1951 accessmode = (share_mode<<4);
1953 if ((flags & O_ACCMODE) == O_RDWR) {
1954 accessmode |= 2;
1955 } else if ((flags & O_ACCMODE) == O_WRONLY) {
1956 accessmode |= 1;
1959 #if defined(O_SYNC)
1960 if ((flags & O_SYNC) == O_SYNC) {
1961 accessmode |= (1<<14);
1963 #endif /* O_SYNC */
1965 if (share_mode == DENY_FCB) {
1966 accessmode = 0xFF;
1969 SCVAL(state->vwv + 0, 0, 0xFF);
1970 SCVAL(state->vwv + 0, 1, 0);
1971 SSVAL(state->vwv + 1, 0, 0);
1972 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
1973 SSVAL(state->vwv + 3, 0, accessmode);
1974 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
1975 SSVAL(state->vwv + 5, 0, 0);
1976 SIVAL(state->vwv + 6, 0, 0);
1977 SSVAL(state->vwv + 8, 0, openfn);
1978 SIVAL(state->vwv + 9, 0, 0);
1979 SIVAL(state->vwv + 11, 0, 0);
1980 SIVAL(state->vwv + 13, 0, 0);
1982 additional_flags = 0;
1984 if (cli->use_oplocks) {
1985 /* if using oplocks then ask for a batch oplock via
1986 core and extended methods */
1987 additional_flags =
1988 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1989 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1992 bytes = talloc_array(state, uint8_t, 0);
1993 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1994 strlen(fname)+1, NULL);
1996 if (tevent_req_nomem(bytes, req)) {
1997 return tevent_req_post(req, ev);
2000 state->bytes.iov_base = (void *)bytes;
2001 state->bytes.iov_len = talloc_get_size(bytes);
2003 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2004 15, state->vwv, 1, &state->bytes);
2005 if (subreq == NULL) {
2006 TALLOC_FREE(req);
2007 return NULL;
2009 tevent_req_set_callback(subreq, cli_open_done, req);
2010 *psmbreq = subreq;
2011 return req;
2014 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2015 struct cli_state *cli, const char *fname,
2016 int flags, int share_mode)
2018 struct tevent_req *req, *subreq;
2019 NTSTATUS status;
2021 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2022 &subreq);
2023 if (req == NULL) {
2024 return NULL;
2027 status = cli_smb_req_send(subreq);
2028 if (tevent_req_nterror(req, status)) {
2029 return tevent_req_post(req, ev);
2031 return req;
2034 static void cli_open_done(struct tevent_req *subreq)
2036 struct tevent_req *req = tevent_req_callback_data(
2037 subreq, struct tevent_req);
2038 struct cli_open_state *state = tevent_req_data(
2039 req, struct cli_open_state);
2040 uint8_t wct;
2041 uint16_t *vwv;
2042 uint8_t *inbuf;
2043 NTSTATUS status;
2045 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2046 NULL);
2047 TALLOC_FREE(subreq);
2048 if (tevent_req_nterror(req, status)) {
2049 return;
2051 state->fnum = SVAL(vwv+2, 0);
2052 tevent_req_done(req);
2055 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2057 struct cli_open_state *state = tevent_req_data(
2058 req, struct cli_open_state);
2059 NTSTATUS status;
2061 if (tevent_req_is_nterror(req, &status)) {
2062 return status;
2064 *pfnum = state->fnum;
2065 return NT_STATUS_OK;
2068 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2069 int share_mode, uint16_t *pfnum)
2071 TALLOC_CTX *frame = talloc_stackframe();
2072 struct event_context *ev;
2073 struct tevent_req *req;
2074 NTSTATUS status = NT_STATUS_OK;
2076 if (cli_has_async_calls(cli)) {
2078 * Can't use sync call while an async call is in flight
2080 status = NT_STATUS_INVALID_PARAMETER;
2081 goto fail;
2084 ev = event_context_init(frame);
2085 if (ev == NULL) {
2086 status = NT_STATUS_NO_MEMORY;
2087 goto fail;
2090 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2091 if (req == NULL) {
2092 status = NT_STATUS_NO_MEMORY;
2093 goto fail;
2096 if (!tevent_req_poll(req, ev)) {
2097 status = map_nt_error_from_unix(errno);
2098 goto fail;
2101 status = cli_open_recv(req, pfnum);
2102 fail:
2103 TALLOC_FREE(frame);
2104 return status;
2107 /****************************************************************************
2108 Close a file.
2109 ****************************************************************************/
2111 struct cli_close_state {
2112 uint16_t vwv[3];
2115 static void cli_close_done(struct tevent_req *subreq);
2117 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2118 struct event_context *ev,
2119 struct cli_state *cli,
2120 uint16_t fnum,
2121 struct tevent_req **psubreq)
2123 struct tevent_req *req, *subreq;
2124 struct cli_close_state *state;
2126 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2127 if (req == NULL) {
2128 return NULL;
2131 SSVAL(state->vwv+0, 0, fnum);
2132 SIVALS(state->vwv+1, 0, -1);
2134 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2135 0, NULL);
2136 if (subreq == NULL) {
2137 TALLOC_FREE(req);
2138 return NULL;
2140 tevent_req_set_callback(subreq, cli_close_done, req);
2141 *psubreq = subreq;
2142 return req;
2145 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2146 struct event_context *ev,
2147 struct cli_state *cli,
2148 uint16_t fnum)
2150 struct tevent_req *req, *subreq;
2151 NTSTATUS status;
2153 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2154 if (req == NULL) {
2155 return NULL;
2158 status = cli_smb_req_send(subreq);
2159 if (tevent_req_nterror(req, status)) {
2160 return tevent_req_post(req, ev);
2162 return req;
2165 static void cli_close_done(struct tevent_req *subreq)
2167 struct tevent_req *req = tevent_req_callback_data(
2168 subreq, struct tevent_req);
2169 NTSTATUS status;
2171 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2172 TALLOC_FREE(subreq);
2173 if (tevent_req_nterror(req, status)) {
2174 return;
2176 tevent_req_done(req);
2179 NTSTATUS cli_close_recv(struct tevent_req *req)
2181 return tevent_req_simple_recv_ntstatus(req);
2184 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2186 TALLOC_CTX *frame = talloc_stackframe();
2187 struct event_context *ev;
2188 struct tevent_req *req;
2189 NTSTATUS status = NT_STATUS_OK;
2191 if (cli_has_async_calls(cli)) {
2193 * Can't use sync call while an async call is in flight
2195 status = NT_STATUS_INVALID_PARAMETER;
2196 goto fail;
2199 ev = event_context_init(frame);
2200 if (ev == NULL) {
2201 status = NT_STATUS_NO_MEMORY;
2202 goto fail;
2205 req = cli_close_send(frame, ev, cli, fnum);
2206 if (req == NULL) {
2207 status = NT_STATUS_NO_MEMORY;
2208 goto fail;
2211 if (!tevent_req_poll(req, ev)) {
2212 status = map_nt_error_from_unix(errno);
2213 goto fail;
2216 status = cli_close_recv(req);
2217 fail:
2218 TALLOC_FREE(frame);
2219 return status;
2222 /****************************************************************************
2223 Truncate a file to a specified size
2224 ****************************************************************************/
2226 struct ftrunc_state {
2227 uint16_t setup;
2228 uint8_t param[6];
2229 uint8_t data[8];
2232 static void cli_ftruncate_done(struct tevent_req *subreq)
2234 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2235 NULL, 0, NULL, NULL, 0, NULL);
2236 tevent_req_simple_finish_ntstatus(subreq, status);
2239 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2240 struct event_context *ev,
2241 struct cli_state *cli,
2242 uint16_t fnum,
2243 uint64_t size)
2245 struct tevent_req *req = NULL, *subreq = NULL;
2246 struct ftrunc_state *state = NULL;
2248 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2249 if (req == NULL) {
2250 return NULL;
2253 /* Setup setup word. */
2254 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2256 /* Setup param array. */
2257 SSVAL(state->param,0,fnum);
2258 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2259 SSVAL(state->param,4,0);
2261 /* Setup data array. */
2262 SBVAL(state->data, 0, size);
2264 subreq = cli_trans_send(state, /* mem ctx. */
2265 ev, /* event ctx. */
2266 cli, /* cli_state. */
2267 SMBtrans2, /* cmd. */
2268 NULL, /* pipe name. */
2269 -1, /* fid. */
2270 0, /* function. */
2271 0, /* flags. */
2272 &state->setup, /* setup. */
2273 1, /* num setup uint16_t words. */
2274 0, /* max returned setup. */
2275 state->param, /* param. */
2276 6, /* num param. */
2277 2, /* max returned param. */
2278 state->data, /* data. */
2279 8, /* num data. */
2280 0); /* max returned data. */
2282 if (tevent_req_nomem(subreq, req)) {
2283 return tevent_req_post(req, ev);
2285 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2286 return req;
2289 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2291 return tevent_req_simple_recv_ntstatus(req);
2294 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2296 TALLOC_CTX *frame = talloc_stackframe();
2297 struct event_context *ev = NULL;
2298 struct tevent_req *req = NULL;
2299 NTSTATUS status = NT_STATUS_OK;
2301 if (cli_has_async_calls(cli)) {
2303 * Can't use sync call while an async call is in flight
2305 status = NT_STATUS_INVALID_PARAMETER;
2306 goto fail;
2309 ev = event_context_init(frame);
2310 if (ev == NULL) {
2311 status = NT_STATUS_NO_MEMORY;
2312 goto fail;
2315 req = cli_ftruncate_send(frame,
2317 cli,
2318 fnum,
2319 size);
2320 if (req == NULL) {
2321 status = NT_STATUS_NO_MEMORY;
2322 goto fail;
2325 if (!tevent_req_poll(req, ev)) {
2326 status = map_nt_error_from_unix(errno);
2327 goto fail;
2330 status = cli_ftruncate_recv(req);
2332 fail:
2333 TALLOC_FREE(frame);
2334 return status;
2337 /****************************************************************************
2338 send a lock with a specified locktype
2339 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2340 ****************************************************************************/
2342 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2343 uint32_t offset, uint32_t len,
2344 int timeout, unsigned char locktype)
2346 uint16_t vwv[8];
2347 uint8_t bytes[10];
2348 NTSTATUS status;
2349 int saved_timeout;
2351 SCVAL(vwv + 0, 0, 0xff);
2352 SCVAL(vwv + 0, 1, 0);
2353 SSVAL(vwv + 1, 0, 0);
2354 SSVAL(vwv + 2, 0, fnum);
2355 SCVAL(vwv + 3, 0, locktype);
2356 SCVAL(vwv + 3, 1, 0);
2357 SIVALS(vwv + 4, 0, timeout);
2358 SSVAL(vwv + 6, 0, 0);
2359 SSVAL(vwv + 7, 0, 1);
2361 SSVAL(bytes, 0, cli->pid);
2362 SIVAL(bytes, 2, offset);
2363 SIVAL(bytes, 6, len);
2365 saved_timeout = cli->timeout;
2367 if (timeout != 0) {
2368 cli->timeout = (timeout == -1)
2369 ? 0x7FFFFFFF : (timeout + 2*1000);
2372 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2373 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2375 cli->timeout = saved_timeout;
2377 return status;
2380 /****************************************************************************
2381 Lock a file.
2382 note that timeout is in units of 2 milliseconds
2383 ****************************************************************************/
2385 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2386 uint32_t offset, uint32_t len, int timeout,
2387 enum brl_type lock_type)
2389 NTSTATUS status;
2391 status = cli_locktype(cli, fnum, offset, len, timeout,
2392 (lock_type == READ_LOCK? 1 : 0));
2393 return NT_STATUS_IS_OK(status);
2396 /****************************************************************************
2397 Unlock a file.
2398 ****************************************************************************/
2400 struct cli_unlock_state {
2401 uint16_t vwv[8];
2402 uint8_t data[10];
2405 static void cli_unlock_done(struct tevent_req *subreq);
2407 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2408 struct event_context *ev,
2409 struct cli_state *cli,
2410 uint16_t fnum,
2411 uint64_t offset,
2412 uint64_t len)
2415 struct tevent_req *req = NULL, *subreq = NULL;
2416 struct cli_unlock_state *state = NULL;
2417 uint8_t additional_flags = 0;
2419 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2420 if (req == NULL) {
2421 return NULL;
2424 SCVAL(state->vwv+0, 0, 0xFF);
2425 SSVAL(state->vwv+2, 0, fnum);
2426 SCVAL(state->vwv+3, 0, 0);
2427 SIVALS(state->vwv+4, 0, 0);
2428 SSVAL(state->vwv+6, 0, 1);
2429 SSVAL(state->vwv+7, 0, 0);
2431 SSVAL(state->data, 0, cli->pid);
2432 SIVAL(state->data, 2, offset);
2433 SIVAL(state->data, 6, len);
2435 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2436 8, state->vwv, 10, state->data);
2437 if (tevent_req_nomem(subreq, req)) {
2438 return tevent_req_post(req, ev);
2440 tevent_req_set_callback(subreq, cli_unlock_done, req);
2441 return req;
2444 static void cli_unlock_done(struct tevent_req *subreq)
2446 struct tevent_req *req = tevent_req_callback_data(
2447 subreq, struct tevent_req);
2448 NTSTATUS status;
2450 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2451 TALLOC_FREE(subreq);
2452 if (tevent_req_nterror(req, status)) {
2453 return;
2455 tevent_req_done(req);
2458 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2460 return tevent_req_simple_recv_ntstatus(req);
2463 NTSTATUS cli_unlock(struct cli_state *cli,
2464 uint16_t fnum,
2465 uint32_t offset,
2466 uint32_t len)
2468 TALLOC_CTX *frame = talloc_stackframe();
2469 struct event_context *ev;
2470 struct tevent_req *req;
2471 NTSTATUS status = NT_STATUS_OK;
2473 if (cli_has_async_calls(cli)) {
2475 * Can't use sync call while an async call is in flight
2477 status = NT_STATUS_INVALID_PARAMETER;
2478 goto fail;
2481 ev = event_context_init(frame);
2482 if (ev == NULL) {
2483 status = NT_STATUS_NO_MEMORY;
2484 goto fail;
2487 req = cli_unlock_send(frame, ev, cli,
2488 fnum, offset, len);
2489 if (req == NULL) {
2490 status = NT_STATUS_NO_MEMORY;
2491 goto fail;
2494 if (!tevent_req_poll(req, ev)) {
2495 status = map_nt_error_from_unix(errno);
2496 goto fail;
2499 status = cli_unlock_recv(req);
2501 fail:
2502 TALLOC_FREE(frame);
2503 return status;
2506 /****************************************************************************
2507 Lock a file with 64 bit offsets.
2508 ****************************************************************************/
2510 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2511 uint64_t offset, uint64_t len, int timeout,
2512 enum brl_type lock_type)
2514 uint16_t vwv[8];
2515 uint8_t bytes[20];
2516 int saved_timeout = cli->timeout;
2517 int ltype;
2518 NTSTATUS status;
2520 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2521 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2524 ltype = (lock_type == READ_LOCK? 1 : 0);
2525 ltype |= LOCKING_ANDX_LARGE_FILES;
2527 SCVAL(vwv + 0, 0, 0xff);
2528 SCVAL(vwv + 0, 1, 0);
2529 SSVAL(vwv + 1, 0, 0);
2530 SSVAL(vwv + 2, 0, fnum);
2531 SCVAL(vwv + 3, 0, ltype);
2532 SCVAL(vwv + 3, 1, 0);
2533 SIVALS(vwv + 4, 0, timeout);
2534 SSVAL(vwv + 6, 0, 0);
2535 SSVAL(vwv + 7, 0, 1);
2537 SIVAL(bytes, 0, cli->pid);
2538 SOFF_T_R(bytes, 4, offset);
2539 SOFF_T_R(bytes, 12, len);
2541 saved_timeout = cli->timeout;
2543 if (timeout != 0) {
2544 cli->timeout = (timeout == -1)
2545 ? 0x7FFFFFFF : (timeout + 2*1000);
2548 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2549 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2551 cli->timeout = saved_timeout;
2553 return NT_STATUS_IS_OK(status);
2556 /****************************************************************************
2557 Unlock a file with 64 bit offsets.
2558 ****************************************************************************/
2560 struct cli_unlock64_state {
2561 uint16_t vwv[8];
2562 uint8_t data[20];
2565 static void cli_unlock64_done(struct tevent_req *subreq);
2567 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2568 struct event_context *ev,
2569 struct cli_state *cli,
2570 uint16_t fnum,
2571 uint64_t offset,
2572 uint64_t len)
2575 struct tevent_req *req = NULL, *subreq = NULL;
2576 struct cli_unlock64_state *state = NULL;
2577 uint8_t additional_flags = 0;
2579 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2580 if (req == NULL) {
2581 return NULL;
2584 SCVAL(state->vwv+0, 0, 0xff);
2585 SSVAL(state->vwv+2, 0, fnum);
2586 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2587 SIVALS(state->vwv+4, 0, 0);
2588 SSVAL(state->vwv+6, 0, 1);
2589 SSVAL(state->vwv+7, 0, 0);
2591 SIVAL(state->data, 0, cli->pid);
2592 SOFF_T_R(state->data, 4, offset);
2593 SOFF_T_R(state->data, 12, len);
2595 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2596 8, state->vwv, 20, state->data);
2597 if (tevent_req_nomem(subreq, req)) {
2598 return tevent_req_post(req, ev);
2600 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2601 return req;
2604 static void cli_unlock64_done(struct tevent_req *subreq)
2606 struct tevent_req *req = tevent_req_callback_data(
2607 subreq, struct tevent_req);
2608 NTSTATUS status;
2610 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2611 TALLOC_FREE(subreq);
2612 if (tevent_req_nterror(req, status)) {
2613 return;
2615 tevent_req_done(req);
2618 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2620 return tevent_req_simple_recv_ntstatus(req);
2623 NTSTATUS cli_unlock64(struct cli_state *cli,
2624 uint16_t fnum,
2625 uint64_t offset,
2626 uint64_t len)
2628 TALLOC_CTX *frame = talloc_stackframe();
2629 struct event_context *ev;
2630 struct tevent_req *req;
2631 NTSTATUS status = NT_STATUS_OK;
2633 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2634 return cli_unlock(cli, fnum, offset, len);
2637 if (cli_has_async_calls(cli)) {
2639 * Can't use sync call while an async call is in flight
2641 status = NT_STATUS_INVALID_PARAMETER;
2642 goto fail;
2645 ev = event_context_init(frame);
2646 if (ev == NULL) {
2647 status = NT_STATUS_NO_MEMORY;
2648 goto fail;
2651 req = cli_unlock64_send(frame, ev, cli,
2652 fnum, offset, len);
2653 if (req == NULL) {
2654 status = NT_STATUS_NO_MEMORY;
2655 goto fail;
2658 if (!tevent_req_poll(req, ev)) {
2659 status = map_nt_error_from_unix(errno);
2660 goto fail;
2663 status = cli_unlock64_recv(req);
2665 fail:
2666 TALLOC_FREE(frame);
2667 return status;
2670 /****************************************************************************
2671 Get/unlock a POSIX lock on a file - internal function.
2672 ****************************************************************************/
2674 struct posix_lock_state {
2675 uint16_t setup;
2676 uint8_t param[4];
2677 uint8_t data[POSIX_LOCK_DATA_SIZE];
2680 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2682 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2683 NULL, 0, NULL, NULL, 0, NULL);
2684 tevent_req_simple_finish_ntstatus(subreq, status);
2687 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2688 struct event_context *ev,
2689 struct cli_state *cli,
2690 uint16_t fnum,
2691 uint64_t offset,
2692 uint64_t len,
2693 bool wait_lock,
2694 enum brl_type lock_type)
2696 struct tevent_req *req = NULL, *subreq = NULL;
2697 struct posix_lock_state *state = NULL;
2699 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2700 if (req == NULL) {
2701 return NULL;
2704 /* Setup setup word. */
2705 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2707 /* Setup param array. */
2708 SSVAL(&state->param, 0, fnum);
2709 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2711 /* Setup data array. */
2712 switch (lock_type) {
2713 case READ_LOCK:
2714 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2715 POSIX_LOCK_TYPE_READ);
2716 break;
2717 case WRITE_LOCK:
2718 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2719 POSIX_LOCK_TYPE_WRITE);
2720 break;
2721 case UNLOCK_LOCK:
2722 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2723 POSIX_LOCK_TYPE_UNLOCK);
2724 break;
2725 default:
2726 return NULL;
2729 if (wait_lock) {
2730 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2731 POSIX_LOCK_FLAG_WAIT);
2732 } else {
2733 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2734 POSIX_LOCK_FLAG_NOWAIT);
2737 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2738 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2739 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2741 subreq = cli_trans_send(state, /* mem ctx. */
2742 ev, /* event ctx. */
2743 cli, /* cli_state. */
2744 SMBtrans2, /* cmd. */
2745 NULL, /* pipe name. */
2746 -1, /* fid. */
2747 0, /* function. */
2748 0, /* flags. */
2749 &state->setup, /* setup. */
2750 1, /* num setup uint16_t words. */
2751 0, /* max returned setup. */
2752 state->param, /* param. */
2753 4, /* num param. */
2754 2, /* max returned param. */
2755 state->data, /* data. */
2756 POSIX_LOCK_DATA_SIZE, /* num data. */
2757 0); /* max returned data. */
2759 if (tevent_req_nomem(subreq, req)) {
2760 return tevent_req_post(req, ev);
2762 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2763 return req;
2766 /****************************************************************************
2767 POSIX Lock a file.
2768 ****************************************************************************/
2770 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2771 struct event_context *ev,
2772 struct cli_state *cli,
2773 uint16_t fnum,
2774 uint64_t offset,
2775 uint64_t len,
2776 bool wait_lock,
2777 enum brl_type lock_type)
2779 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2780 wait_lock, lock_type);
2783 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2785 return tevent_req_simple_recv_ntstatus(req);
2788 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2789 uint64_t offset, uint64_t len,
2790 bool wait_lock, enum brl_type lock_type)
2792 TALLOC_CTX *frame = talloc_stackframe();
2793 struct event_context *ev = NULL;
2794 struct tevent_req *req = NULL;
2795 NTSTATUS status = NT_STATUS_OK;
2797 if (cli_has_async_calls(cli)) {
2799 * Can't use sync call while an async call is in flight
2801 status = NT_STATUS_INVALID_PARAMETER;
2802 goto fail;
2805 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2806 status = NT_STATUS_INVALID_PARAMETER;
2807 goto fail;
2810 ev = event_context_init(frame);
2811 if (ev == NULL) {
2812 status = NT_STATUS_NO_MEMORY;
2813 goto fail;
2816 req = cli_posix_lock_send(frame,
2818 cli,
2819 fnum,
2820 offset,
2821 len,
2822 wait_lock,
2823 lock_type);
2824 if (req == NULL) {
2825 status = NT_STATUS_NO_MEMORY;
2826 goto fail;
2829 if (!tevent_req_poll(req, ev)) {
2830 status = map_nt_error_from_unix(errno);
2831 goto fail;
2834 status = cli_posix_lock_recv(req);
2836 fail:
2837 TALLOC_FREE(frame);
2838 return status;
2841 /****************************************************************************
2842 POSIX Unlock a file.
2843 ****************************************************************************/
2845 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
2846 struct event_context *ev,
2847 struct cli_state *cli,
2848 uint16_t fnum,
2849 uint64_t offset,
2850 uint64_t len)
2852 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2853 false, UNLOCK_LOCK);
2856 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
2858 return tevent_req_simple_recv_ntstatus(req);
2861 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2863 TALLOC_CTX *frame = talloc_stackframe();
2864 struct event_context *ev = NULL;
2865 struct tevent_req *req = NULL;
2866 NTSTATUS status = NT_STATUS_OK;
2868 if (cli_has_async_calls(cli)) {
2870 * Can't use sync call while an async call is in flight
2872 status = NT_STATUS_INVALID_PARAMETER;
2873 goto fail;
2876 ev = event_context_init(frame);
2877 if (ev == NULL) {
2878 status = NT_STATUS_NO_MEMORY;
2879 goto fail;
2882 req = cli_posix_unlock_send(frame,
2884 cli,
2885 fnum,
2886 offset,
2887 len);
2888 if (req == NULL) {
2889 status = NT_STATUS_NO_MEMORY;
2890 goto fail;
2893 if (!tevent_req_poll(req, ev)) {
2894 status = map_nt_error_from_unix(errno);
2895 goto fail;
2898 status = cli_posix_unlock_recv(req);
2900 fail:
2901 TALLOC_FREE(frame);
2902 return status;
2905 /****************************************************************************
2906 Do a SMBgetattrE call.
2907 ****************************************************************************/
2909 static void cli_getattrE_done(struct tevent_req *subreq);
2911 struct cli_getattrE_state {
2912 uint16_t vwv[1];
2913 int zone_offset;
2914 uint16_t attr;
2915 SMB_OFF_T size;
2916 time_t change_time;
2917 time_t access_time;
2918 time_t write_time;
2921 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
2922 struct event_context *ev,
2923 struct cli_state *cli,
2924 uint16_t fnum)
2926 struct tevent_req *req = NULL, *subreq = NULL;
2927 struct cli_getattrE_state *state = NULL;
2928 uint8_t additional_flags = 0;
2930 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
2931 if (req == NULL) {
2932 return NULL;
2935 state->zone_offset = cli->serverzone;
2936 SSVAL(state->vwv+0,0,fnum);
2938 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
2939 1, state->vwv, 0, NULL);
2940 if (tevent_req_nomem(subreq, req)) {
2941 return tevent_req_post(req, ev);
2943 tevent_req_set_callback(subreq, cli_getattrE_done, req);
2944 return req;
2947 static void cli_getattrE_done(struct tevent_req *subreq)
2949 struct tevent_req *req = tevent_req_callback_data(
2950 subreq, struct tevent_req);
2951 struct cli_getattrE_state *state = tevent_req_data(
2952 req, struct cli_getattrE_state);
2953 uint8_t wct;
2954 uint16_t *vwv = NULL;
2955 uint8_t *inbuf;
2956 NTSTATUS status;
2958 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
2959 NULL, NULL);
2960 TALLOC_FREE(subreq);
2961 if (tevent_req_nterror(req, status)) {
2962 return;
2965 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
2966 state->attr = SVAL(vwv+10,0);
2967 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
2968 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
2969 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
2971 tevent_req_done(req);
2974 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
2975 uint16_t *attr,
2976 SMB_OFF_T *size,
2977 time_t *change_time,
2978 time_t *access_time,
2979 time_t *write_time)
2981 struct cli_getattrE_state *state = tevent_req_data(
2982 req, struct cli_getattrE_state);
2983 NTSTATUS status;
2985 if (tevent_req_is_nterror(req, &status)) {
2986 return status;
2988 if (attr) {
2989 *attr = state->attr;
2991 if (size) {
2992 *size = state->size;
2994 if (change_time) {
2995 *change_time = state->change_time;
2997 if (access_time) {
2998 *access_time = state->access_time;
3000 if (write_time) {
3001 *write_time = state->write_time;
3003 return NT_STATUS_OK;
3006 NTSTATUS cli_getattrE(struct cli_state *cli,
3007 uint16_t fnum,
3008 uint16_t *attr,
3009 SMB_OFF_T *size,
3010 time_t *change_time,
3011 time_t *access_time,
3012 time_t *write_time)
3014 TALLOC_CTX *frame = talloc_stackframe();
3015 struct event_context *ev = NULL;
3016 struct tevent_req *req = NULL;
3017 NTSTATUS status = NT_STATUS_OK;
3019 if (cli_has_async_calls(cli)) {
3021 * Can't use sync call while an async call is in flight
3023 status = NT_STATUS_INVALID_PARAMETER;
3024 goto fail;
3027 ev = event_context_init(frame);
3028 if (ev == NULL) {
3029 status = NT_STATUS_NO_MEMORY;
3030 goto fail;
3033 req = cli_getattrE_send(frame, ev, cli, fnum);
3034 if (req == NULL) {
3035 status = NT_STATUS_NO_MEMORY;
3036 goto fail;
3039 if (!tevent_req_poll(req, ev)) {
3040 status = map_nt_error_from_unix(errno);
3041 goto fail;
3044 status = cli_getattrE_recv(req,
3045 attr,
3046 size,
3047 change_time,
3048 access_time,
3049 write_time);
3051 fail:
3052 TALLOC_FREE(frame);
3053 return status;
3056 /****************************************************************************
3057 Do a SMBgetatr call
3058 ****************************************************************************/
3060 static void cli_getatr_done(struct tevent_req *subreq);
3062 struct cli_getatr_state {
3063 int zone_offset;
3064 uint16_t attr;
3065 SMB_OFF_T size;
3066 time_t write_time;
3069 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3070 struct event_context *ev,
3071 struct cli_state *cli,
3072 const char *fname)
3074 struct tevent_req *req = NULL, *subreq = NULL;
3075 struct cli_getatr_state *state = NULL;
3076 uint8_t additional_flags = 0;
3077 uint8_t *bytes = NULL;
3079 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3080 if (req == NULL) {
3081 return NULL;
3084 state->zone_offset = cli->serverzone;
3086 bytes = talloc_array(state, uint8_t, 1);
3087 if (tevent_req_nomem(bytes, req)) {
3088 return tevent_req_post(req, ev);
3090 bytes[0] = 4;
3091 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3092 strlen(fname)+1, NULL);
3094 if (tevent_req_nomem(bytes, req)) {
3095 return tevent_req_post(req, ev);
3098 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3099 0, NULL, talloc_get_size(bytes), bytes);
3100 if (tevent_req_nomem(subreq, req)) {
3101 return tevent_req_post(req, ev);
3103 tevent_req_set_callback(subreq, cli_getatr_done, req);
3104 return req;
3107 static void cli_getatr_done(struct tevent_req *subreq)
3109 struct tevent_req *req = tevent_req_callback_data(
3110 subreq, struct tevent_req);
3111 struct cli_getatr_state *state = tevent_req_data(
3112 req, struct cli_getatr_state);
3113 uint8_t wct;
3114 uint16_t *vwv = NULL;
3115 uint8_t *inbuf;
3116 NTSTATUS status;
3118 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3119 NULL);
3120 TALLOC_FREE(subreq);
3121 if (tevent_req_nterror(req, status)) {
3122 return;
3125 state->attr = SVAL(vwv+0,0);
3126 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3127 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3129 tevent_req_done(req);
3132 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3133 uint16_t *attr,
3134 SMB_OFF_T *size,
3135 time_t *write_time)
3137 struct cli_getatr_state *state = tevent_req_data(
3138 req, struct cli_getatr_state);
3139 NTSTATUS status;
3141 if (tevent_req_is_nterror(req, &status)) {
3142 return status;
3144 if (attr) {
3145 *attr = state->attr;
3147 if (size) {
3148 *size = state->size;
3150 if (write_time) {
3151 *write_time = state->write_time;
3153 return NT_STATUS_OK;
3156 NTSTATUS cli_getatr(struct cli_state *cli,
3157 const char *fname,
3158 uint16_t *attr,
3159 SMB_OFF_T *size,
3160 time_t *write_time)
3162 TALLOC_CTX *frame = talloc_stackframe();
3163 struct event_context *ev = NULL;
3164 struct tevent_req *req = NULL;
3165 NTSTATUS status = NT_STATUS_OK;
3167 if (cli_has_async_calls(cli)) {
3169 * Can't use sync call while an async call is in flight
3171 status = NT_STATUS_INVALID_PARAMETER;
3172 goto fail;
3175 ev = event_context_init(frame);
3176 if (ev == NULL) {
3177 status = NT_STATUS_NO_MEMORY;
3178 goto fail;
3181 req = cli_getatr_send(frame, ev, cli, fname);
3182 if (req == NULL) {
3183 status = NT_STATUS_NO_MEMORY;
3184 goto fail;
3187 if (!tevent_req_poll(req, ev)) {
3188 status = map_nt_error_from_unix(errno);
3189 goto fail;
3192 status = cli_getatr_recv(req,
3193 attr,
3194 size,
3195 write_time);
3197 fail:
3198 TALLOC_FREE(frame);
3199 return status;
3202 /****************************************************************************
3203 Do a SMBsetattrE call.
3204 ****************************************************************************/
3206 static void cli_setattrE_done(struct tevent_req *subreq);
3208 struct cli_setattrE_state {
3209 uint16_t vwv[7];
3212 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3213 struct event_context *ev,
3214 struct cli_state *cli,
3215 uint16_t fnum,
3216 time_t change_time,
3217 time_t access_time,
3218 time_t write_time)
3220 struct tevent_req *req = NULL, *subreq = NULL;
3221 struct cli_setattrE_state *state = NULL;
3222 uint8_t additional_flags = 0;
3224 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3225 if (req == NULL) {
3226 return NULL;
3229 SSVAL(state->vwv+0, 0, fnum);
3230 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3231 cli->serverzone);
3232 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3233 cli->serverzone);
3234 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3235 cli->serverzone);
3237 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3238 7, state->vwv, 0, NULL);
3239 if (tevent_req_nomem(subreq, req)) {
3240 return tevent_req_post(req, ev);
3242 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3243 return req;
3246 static void cli_setattrE_done(struct tevent_req *subreq)
3248 struct tevent_req *req = tevent_req_callback_data(
3249 subreq, struct tevent_req);
3250 NTSTATUS status;
3252 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3253 TALLOC_FREE(subreq);
3254 if (tevent_req_nterror(req, status)) {
3255 return;
3257 tevent_req_done(req);
3260 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3262 return tevent_req_simple_recv_ntstatus(req);
3265 NTSTATUS cli_setattrE(struct cli_state *cli,
3266 uint16_t fnum,
3267 time_t change_time,
3268 time_t access_time,
3269 time_t write_time)
3271 TALLOC_CTX *frame = talloc_stackframe();
3272 struct event_context *ev = NULL;
3273 struct tevent_req *req = NULL;
3274 NTSTATUS status = NT_STATUS_OK;
3276 if (cli_has_async_calls(cli)) {
3278 * Can't use sync call while an async call is in flight
3280 status = NT_STATUS_INVALID_PARAMETER;
3281 goto fail;
3284 ev = event_context_init(frame);
3285 if (ev == NULL) {
3286 status = NT_STATUS_NO_MEMORY;
3287 goto fail;
3290 req = cli_setattrE_send(frame, ev,
3291 cli,
3292 fnum,
3293 change_time,
3294 access_time,
3295 write_time);
3297 if (req == NULL) {
3298 status = NT_STATUS_NO_MEMORY;
3299 goto fail;
3302 if (!tevent_req_poll(req, ev)) {
3303 status = map_nt_error_from_unix(errno);
3304 goto fail;
3307 status = cli_setattrE_recv(req);
3309 fail:
3310 TALLOC_FREE(frame);
3311 return status;
3314 /****************************************************************************
3315 Do a SMBsetatr call.
3316 ****************************************************************************/
3318 static void cli_setatr_done(struct tevent_req *subreq);
3320 struct cli_setatr_state {
3321 uint16_t vwv[8];
3324 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3325 struct event_context *ev,
3326 struct cli_state *cli,
3327 const char *fname,
3328 uint16_t attr,
3329 time_t mtime)
3331 struct tevent_req *req = NULL, *subreq = NULL;
3332 struct cli_setatr_state *state = NULL;
3333 uint8_t additional_flags = 0;
3334 uint8_t *bytes = NULL;
3336 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3337 if (req == NULL) {
3338 return NULL;
3341 SSVAL(state->vwv+0, 0, attr);
3342 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3344 bytes = talloc_array(state, uint8_t, 1);
3345 if (tevent_req_nomem(bytes, req)) {
3346 return tevent_req_post(req, ev);
3348 bytes[0] = 4;
3349 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3350 strlen(fname)+1, NULL);
3351 if (tevent_req_nomem(bytes, req)) {
3352 return tevent_req_post(req, ev);
3354 bytes = talloc_realloc(state, bytes, uint8_t,
3355 talloc_get_size(bytes)+1);
3356 if (tevent_req_nomem(bytes, req)) {
3357 return tevent_req_post(req, ev);
3360 bytes[talloc_get_size(bytes)-1] = 4;
3361 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3362 1, NULL);
3363 if (tevent_req_nomem(bytes, req)) {
3364 return tevent_req_post(req, ev);
3367 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3368 8, state->vwv, talloc_get_size(bytes), bytes);
3369 if (tevent_req_nomem(subreq, req)) {
3370 return tevent_req_post(req, ev);
3372 tevent_req_set_callback(subreq, cli_setatr_done, req);
3373 return req;
3376 static void cli_setatr_done(struct tevent_req *subreq)
3378 struct tevent_req *req = tevent_req_callback_data(
3379 subreq, struct tevent_req);
3380 NTSTATUS status;
3382 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3383 TALLOC_FREE(subreq);
3384 if (tevent_req_nterror(req, status)) {
3385 return;
3387 tevent_req_done(req);
3390 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3392 return tevent_req_simple_recv_ntstatus(req);
3395 NTSTATUS cli_setatr(struct cli_state *cli,
3396 const char *fname,
3397 uint16_t attr,
3398 time_t mtime)
3400 TALLOC_CTX *frame = talloc_stackframe();
3401 struct event_context *ev = NULL;
3402 struct tevent_req *req = NULL;
3403 NTSTATUS status = NT_STATUS_OK;
3405 if (cli_has_async_calls(cli)) {
3407 * Can't use sync call while an async call is in flight
3409 status = NT_STATUS_INVALID_PARAMETER;
3410 goto fail;
3413 ev = event_context_init(frame);
3414 if (ev == NULL) {
3415 status = NT_STATUS_NO_MEMORY;
3416 goto fail;
3419 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3420 if (req == NULL) {
3421 status = NT_STATUS_NO_MEMORY;
3422 goto fail;
3425 if (!tevent_req_poll(req, ev)) {
3426 status = map_nt_error_from_unix(errno);
3427 goto fail;
3430 status = cli_setatr_recv(req);
3432 fail:
3433 TALLOC_FREE(frame);
3434 return status;
3437 /****************************************************************************
3438 Check for existance of a dir.
3439 ****************************************************************************/
3441 static void cli_chkpath_done(struct tevent_req *subreq);
3443 struct cli_chkpath_state {
3444 int dummy;
3447 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3448 struct event_context *ev,
3449 struct cli_state *cli,
3450 const char *fname)
3452 struct tevent_req *req = NULL, *subreq = NULL;
3453 struct cli_chkpath_state *state = NULL;
3454 uint8_t additional_flags = 0;
3455 uint8_t *bytes = NULL;
3457 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3458 if (req == NULL) {
3459 return NULL;
3462 bytes = talloc_array(state, uint8_t, 1);
3463 if (tevent_req_nomem(bytes, req)) {
3464 return tevent_req_post(req, ev);
3466 bytes[0] = 4;
3467 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3468 strlen(fname)+1, NULL);
3470 if (tevent_req_nomem(bytes, req)) {
3471 return tevent_req_post(req, ev);
3474 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3475 0, NULL, talloc_get_size(bytes), bytes);
3476 if (tevent_req_nomem(subreq, req)) {
3477 return tevent_req_post(req, ev);
3479 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3480 return req;
3483 static void cli_chkpath_done(struct tevent_req *subreq)
3485 struct tevent_req *req = tevent_req_callback_data(
3486 subreq, struct tevent_req);
3487 NTSTATUS status;
3489 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3490 TALLOC_FREE(subreq);
3491 if (tevent_req_nterror(req, status)) {
3492 return;
3494 tevent_req_done(req);
3497 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3499 return tevent_req_simple_recv_ntstatus(req);
3502 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3504 TALLOC_CTX *frame = talloc_stackframe();
3505 struct event_context *ev = NULL;
3506 struct tevent_req *req = NULL;
3507 char *path2 = NULL;
3508 NTSTATUS status = NT_STATUS_OK;
3510 if (cli_has_async_calls(cli)) {
3512 * Can't use sync call while an async call is in flight
3514 status = NT_STATUS_INVALID_PARAMETER;
3515 goto fail;
3518 path2 = talloc_strdup(frame, path);
3519 if (!path2) {
3520 status = NT_STATUS_NO_MEMORY;
3521 goto fail;
3523 trim_char(path2,'\0','\\');
3524 if (!*path2) {
3525 path2 = talloc_strdup(frame, "\\");
3526 if (!path2) {
3527 status = NT_STATUS_NO_MEMORY;
3528 goto fail;
3532 ev = event_context_init(frame);
3533 if (ev == NULL) {
3534 status = NT_STATUS_NO_MEMORY;
3535 goto fail;
3538 req = cli_chkpath_send(frame, ev, cli, path2);
3539 if (req == NULL) {
3540 status = NT_STATUS_NO_MEMORY;
3541 goto fail;
3544 if (!tevent_req_poll(req, ev)) {
3545 status = map_nt_error_from_unix(errno);
3546 goto fail;
3549 status = cli_chkpath_recv(req);
3551 fail:
3552 TALLOC_FREE(frame);
3553 return status;
3556 /****************************************************************************
3557 Query disk space.
3558 ****************************************************************************/
3560 static void cli_dskattr_done(struct tevent_req *subreq);
3562 struct cli_dskattr_state {
3563 int bsize;
3564 int total;
3565 int avail;
3568 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3569 struct event_context *ev,
3570 struct cli_state *cli)
3572 struct tevent_req *req = NULL, *subreq = NULL;
3573 struct cli_dskattr_state *state = NULL;
3574 uint8_t additional_flags = 0;
3576 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3577 if (req == NULL) {
3578 return NULL;
3581 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3582 0, NULL, 0, NULL);
3583 if (tevent_req_nomem(subreq, req)) {
3584 return tevent_req_post(req, ev);
3586 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3587 return req;
3590 static void cli_dskattr_done(struct tevent_req *subreq)
3592 struct tevent_req *req = tevent_req_callback_data(
3593 subreq, struct tevent_req);
3594 struct cli_dskattr_state *state = tevent_req_data(
3595 req, struct cli_dskattr_state);
3596 uint8_t wct;
3597 uint16_t *vwv = NULL;
3598 uint8_t *inbuf;
3599 NTSTATUS status;
3601 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3602 NULL);
3603 TALLOC_FREE(subreq);
3604 if (tevent_req_nterror(req, status)) {
3605 return;
3607 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3608 state->total = SVAL(vwv+0, 0);
3609 state->avail = SVAL(vwv+3, 0);
3610 tevent_req_done(req);
3613 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3615 struct cli_dskattr_state *state = tevent_req_data(
3616 req, struct cli_dskattr_state);
3617 NTSTATUS status;
3619 if (tevent_req_is_nterror(req, &status)) {
3620 return status;
3622 *bsize = state->bsize;
3623 *total = state->total;
3624 *avail = state->avail;
3625 return NT_STATUS_OK;
3628 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3630 TALLOC_CTX *frame = talloc_stackframe();
3631 struct event_context *ev = NULL;
3632 struct tevent_req *req = NULL;
3633 NTSTATUS status = NT_STATUS_OK;
3635 if (cli_has_async_calls(cli)) {
3637 * Can't use sync call while an async call is in flight
3639 status = NT_STATUS_INVALID_PARAMETER;
3640 goto fail;
3643 ev = event_context_init(frame);
3644 if (ev == NULL) {
3645 status = NT_STATUS_NO_MEMORY;
3646 goto fail;
3649 req = cli_dskattr_send(frame, ev, cli);
3650 if (req == NULL) {
3651 status = NT_STATUS_NO_MEMORY;
3652 goto fail;
3655 if (!tevent_req_poll(req, ev)) {
3656 status = map_nt_error_from_unix(errno);
3657 goto fail;
3660 status = cli_dskattr_recv(req, bsize, total, avail);
3662 fail:
3663 TALLOC_FREE(frame);
3664 return status;
3667 /****************************************************************************
3668 Create and open a temporary file.
3669 ****************************************************************************/
3671 static void cli_ctemp_done(struct tevent_req *subreq);
3673 struct ctemp_state {
3674 uint16_t vwv[3];
3675 char *ret_path;
3676 uint16_t fnum;
3679 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3680 struct event_context *ev,
3681 struct cli_state *cli,
3682 const char *path)
3684 struct tevent_req *req = NULL, *subreq = NULL;
3685 struct ctemp_state *state = NULL;
3686 uint8_t additional_flags = 0;
3687 uint8_t *bytes = NULL;
3689 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3690 if (req == NULL) {
3691 return NULL;
3694 SSVAL(state->vwv,0,0);
3695 SIVALS(state->vwv+1,0,-1);
3697 bytes = talloc_array(state, uint8_t, 1);
3698 if (tevent_req_nomem(bytes, req)) {
3699 return tevent_req_post(req, ev);
3701 bytes[0] = 4;
3702 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3703 strlen(path)+1, NULL);
3704 if (tevent_req_nomem(bytes, req)) {
3705 return tevent_req_post(req, ev);
3708 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3709 3, state->vwv, talloc_get_size(bytes), bytes);
3710 if (tevent_req_nomem(subreq, req)) {
3711 return tevent_req_post(req, ev);
3713 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3714 return req;
3717 static void cli_ctemp_done(struct tevent_req *subreq)
3719 struct tevent_req *req = tevent_req_callback_data(
3720 subreq, struct tevent_req);
3721 struct ctemp_state *state = tevent_req_data(
3722 req, struct ctemp_state);
3723 NTSTATUS status;
3724 uint8_t wcnt;
3725 uint16_t *vwv;
3726 uint32_t num_bytes = 0;
3727 uint8_t *bytes = NULL;
3728 uint8_t *inbuf;
3730 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3731 &num_bytes, &bytes);
3732 TALLOC_FREE(subreq);
3733 if (tevent_req_nterror(req, status)) {
3734 return;
3737 state->fnum = SVAL(vwv+0, 0);
3739 /* From W2K3, the result is just the ASCII name */
3740 if (num_bytes < 2) {
3741 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3742 return;
3745 if (pull_string_talloc(state,
3746 NULL,
3748 &state->ret_path,
3749 bytes,
3750 num_bytes,
3751 STR_ASCII) == 0) {
3752 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3753 return;
3755 tevent_req_done(req);
3758 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3759 TALLOC_CTX *ctx,
3760 uint16_t *pfnum,
3761 char **outfile)
3763 struct ctemp_state *state = tevent_req_data(req,
3764 struct ctemp_state);
3765 NTSTATUS status;
3767 if (tevent_req_is_nterror(req, &status)) {
3768 return status;
3770 *pfnum = state->fnum;
3771 *outfile = talloc_strdup(ctx, state->ret_path);
3772 if (!*outfile) {
3773 return NT_STATUS_NO_MEMORY;
3775 return NT_STATUS_OK;
3778 NTSTATUS cli_ctemp(struct cli_state *cli,
3779 TALLOC_CTX *ctx,
3780 const char *path,
3781 uint16_t *pfnum,
3782 char **out_path)
3784 TALLOC_CTX *frame = talloc_stackframe();
3785 struct event_context *ev;
3786 struct tevent_req *req;
3787 NTSTATUS status = NT_STATUS_OK;
3789 if (cli_has_async_calls(cli)) {
3791 * Can't use sync call while an async call is in flight
3793 status = NT_STATUS_INVALID_PARAMETER;
3794 goto fail;
3797 ev = event_context_init(frame);
3798 if (ev == NULL) {
3799 status = NT_STATUS_NO_MEMORY;
3800 goto fail;
3803 req = cli_ctemp_send(frame, ev, cli, path);
3804 if (req == NULL) {
3805 status = NT_STATUS_NO_MEMORY;
3806 goto fail;
3809 if (!tevent_req_poll(req, ev)) {
3810 status = map_nt_error_from_unix(errno);
3811 goto fail;
3814 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
3816 fail:
3817 TALLOC_FREE(frame);
3818 return status;
3822 send a raw ioctl - used by the torture code
3824 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
3826 uint16_t vwv[3];
3827 NTSTATUS status;
3829 SSVAL(vwv+0, 0, fnum);
3830 SSVAL(vwv+1, 0, code>>16);
3831 SSVAL(vwv+2, 0, (code&0xFFFF));
3833 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
3834 NULL, 0, NULL, NULL, NULL, NULL);
3835 if (!NT_STATUS_IS_OK(status)) {
3836 return status;
3838 *blob = data_blob_null;
3839 return NT_STATUS_OK;
3842 /*********************************************************
3843 Set an extended attribute utility fn.
3844 *********************************************************/
3846 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
3847 uint8_t *param, unsigned int param_len,
3848 const char *ea_name,
3849 const char *ea_val, size_t ea_len)
3851 uint16_t setup[1];
3852 unsigned int data_len = 0;
3853 uint8_t *data = NULL;
3854 char *p;
3855 size_t ea_namelen = strlen(ea_name);
3856 NTSTATUS status;
3858 SSVAL(setup, 0, setup_val);
3860 if (ea_namelen == 0 && ea_len == 0) {
3861 data_len = 4;
3862 data = (uint8_t *)SMB_MALLOC(data_len);
3863 if (!data) {
3864 return NT_STATUS_NO_MEMORY;
3866 p = (char *)data;
3867 SIVAL(p,0,data_len);
3868 } else {
3869 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
3870 data = (uint8_t *)SMB_MALLOC(data_len);
3871 if (!data) {
3872 return NT_STATUS_NO_MEMORY;
3874 p = (char *)data;
3875 SIVAL(p,0,data_len);
3876 p += 4;
3877 SCVAL(p, 0, 0); /* EA flags. */
3878 SCVAL(p, 1, ea_namelen);
3879 SSVAL(p, 2, ea_len);
3880 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
3881 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
3884 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
3885 setup, 1, 0,
3886 param, param_len, 2,
3887 data, data_len, cli->max_xmit,
3888 NULL,
3889 NULL, 0, NULL, /* rsetup */
3890 NULL, 0, NULL, /* rparam */
3891 NULL, 0, NULL); /* rdata */
3892 SAFE_FREE(data);
3893 return status;
3896 /*********************************************************
3897 Set an extended attribute on a pathname.
3898 *********************************************************/
3900 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
3901 const char *ea_name, const char *ea_val,
3902 size_t ea_len)
3904 unsigned int param_len = 0;
3905 uint8_t *param;
3906 size_t srclen = 2*(strlen(path)+1);
3907 char *p;
3908 NTSTATUS status;
3910 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
3911 if (!param) {
3912 return NT_STATUS_NO_MEMORY;
3914 memset(param, '\0', 6);
3915 SSVAL(param,0,SMB_INFO_SET_EA);
3916 p = (char *)(&param[6]);
3918 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3919 param_len = PTR_DIFF(p, param);
3921 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
3922 ea_name, ea_val, ea_len);
3923 SAFE_FREE(param);
3924 return status;
3927 /*********************************************************
3928 Set an extended attribute on an fnum.
3929 *********************************************************/
3931 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
3932 const char *ea_name, const char *ea_val,
3933 size_t ea_len)
3935 uint8_t param[6];
3937 memset(param, 0, 6);
3938 SSVAL(param,0,fnum);
3939 SSVAL(param,2,SMB_INFO_SET_EA);
3941 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
3942 ea_name, ea_val, ea_len);
3945 /*********************************************************
3946 Get an extended attribute list utility fn.
3947 *********************************************************/
3949 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
3950 size_t rdata_len,
3951 size_t *pnum_eas, struct ea_struct **pea_list)
3953 struct ea_struct *ea_list = NULL;
3954 size_t num_eas;
3955 size_t ea_size;
3956 const uint8_t *p;
3958 if (rdata_len < 4) {
3959 return false;
3962 ea_size = (size_t)IVAL(rdata,0);
3963 if (ea_size > rdata_len) {
3964 return false;
3967 if (ea_size == 0) {
3968 /* No EA's present. */
3969 *pnum_eas = 0;
3970 *pea_list = NULL;
3971 return true;
3974 p = rdata + 4;
3975 ea_size -= 4;
3977 /* Validate the EA list and count it. */
3978 for (num_eas = 0; ea_size >= 4; num_eas++) {
3979 unsigned int ea_namelen = CVAL(p,1);
3980 unsigned int ea_valuelen = SVAL(p,2);
3981 if (ea_namelen == 0) {
3982 return false;
3984 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
3985 return false;
3987 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
3988 p += 4 + ea_namelen + 1 + ea_valuelen;
3991 if (num_eas == 0) {
3992 *pnum_eas = 0;
3993 *pea_list = NULL;
3994 return true;
3997 *pnum_eas = num_eas;
3998 if (!pea_list) {
3999 /* Caller only wants number of EA's. */
4000 return true;
4003 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4004 if (!ea_list) {
4005 return false;
4008 ea_size = (size_t)IVAL(rdata,0);
4009 p = rdata + 4;
4011 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4012 struct ea_struct *ea = &ea_list[num_eas];
4013 fstring unix_ea_name;
4014 unsigned int ea_namelen = CVAL(p,1);
4015 unsigned int ea_valuelen = SVAL(p,2);
4017 ea->flags = CVAL(p,0);
4018 unix_ea_name[0] = '\0';
4019 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4020 ea->name = talloc_strdup(ea_list, unix_ea_name);
4021 if (!ea->name) {
4022 goto fail;
4024 /* Ensure the value is null terminated (in case it's a string). */
4025 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4026 if (!ea->value.data) {
4027 goto fail;
4029 if (ea_valuelen) {
4030 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4032 ea->value.data[ea_valuelen] = 0;
4033 ea->value.length--;
4034 p += 4 + ea_namelen + 1 + ea_valuelen;
4037 *pea_list = ea_list;
4038 return true;
4040 fail:
4041 TALLOC_FREE(ea_list);
4042 return false;
4045 /*********************************************************
4046 Get an extended attribute list from a pathname.
4047 *********************************************************/
4049 struct cli_get_ea_list_path_state {
4050 uint32_t num_data;
4051 uint8_t *data;
4054 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4056 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4057 struct tevent_context *ev,
4058 struct cli_state *cli,
4059 const char *fname)
4061 struct tevent_req *req, *subreq;
4062 struct cli_get_ea_list_path_state *state;
4064 req = tevent_req_create(mem_ctx, &state,
4065 struct cli_get_ea_list_path_state);
4066 if (req == NULL) {
4067 return NULL;
4069 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4070 SMB_INFO_QUERY_ALL_EAS, 4,
4071 cli->max_xmit);
4072 if (tevent_req_nomem(subreq, req)) {
4073 return tevent_req_post(req, ev);
4075 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4076 return req;
4079 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4081 struct tevent_req *req = tevent_req_callback_data(
4082 subreq, struct tevent_req);
4083 struct cli_get_ea_list_path_state *state = tevent_req_data(
4084 req, struct cli_get_ea_list_path_state);
4085 NTSTATUS status;
4087 status = cli_qpathinfo_recv(subreq, state, &state->data,
4088 &state->num_data);
4089 TALLOC_FREE(subreq);
4090 if (tevent_req_nterror(req, status)) {
4091 return;
4093 tevent_req_done(req);
4096 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4097 size_t *pnum_eas, struct ea_struct **peas)
4099 struct cli_get_ea_list_path_state *state = tevent_req_data(
4100 req, struct cli_get_ea_list_path_state);
4101 NTSTATUS status;
4103 if (tevent_req_is_nterror(req, &status)) {
4104 return status;
4106 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4107 pnum_eas, peas)) {
4108 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4110 return NT_STATUS_OK;
4113 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4114 TALLOC_CTX *ctx,
4115 size_t *pnum_eas,
4116 struct ea_struct **pea_list)
4118 TALLOC_CTX *frame = talloc_stackframe();
4119 struct event_context *ev = NULL;
4120 struct tevent_req *req = NULL;
4121 NTSTATUS status = NT_STATUS_NO_MEMORY;
4123 if (cli_has_async_calls(cli)) {
4125 * Can't use sync call while an async call is in flight
4127 status = NT_STATUS_INVALID_PARAMETER;
4128 goto fail;
4130 ev = event_context_init(frame);
4131 if (ev == NULL) {
4132 goto fail;
4134 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4135 if (req == NULL) {
4136 goto fail;
4138 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4139 goto fail;
4141 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4142 fail:
4143 TALLOC_FREE(frame);
4144 return status;
4147 /****************************************************************************
4148 Convert open "flags" arg to uint32_t on wire.
4149 ****************************************************************************/
4151 static uint32_t open_flags_to_wire(int flags)
4153 int open_mode = flags & O_ACCMODE;
4154 uint32_t ret = 0;
4156 switch (open_mode) {
4157 case O_WRONLY:
4158 ret |= SMB_O_WRONLY;
4159 break;
4160 case O_RDWR:
4161 ret |= SMB_O_RDWR;
4162 break;
4163 default:
4164 case O_RDONLY:
4165 ret |= SMB_O_RDONLY;
4166 break;
4169 if (flags & O_CREAT) {
4170 ret |= SMB_O_CREAT;
4172 if (flags & O_EXCL) {
4173 ret |= SMB_O_EXCL;
4175 if (flags & O_TRUNC) {
4176 ret |= SMB_O_TRUNC;
4178 #if defined(O_SYNC)
4179 if (flags & O_SYNC) {
4180 ret |= SMB_O_SYNC;
4182 #endif /* O_SYNC */
4183 if (flags & O_APPEND) {
4184 ret |= SMB_O_APPEND;
4186 #if defined(O_DIRECT)
4187 if (flags & O_DIRECT) {
4188 ret |= SMB_O_DIRECT;
4190 #endif
4191 #if defined(O_DIRECTORY)
4192 if (flags & O_DIRECTORY) {
4193 ret |= SMB_O_DIRECTORY;
4195 #endif
4196 return ret;
4199 /****************************************************************************
4200 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4201 ****************************************************************************/
4203 struct posix_open_state {
4204 uint16_t setup;
4205 uint8_t *param;
4206 uint8_t data[18];
4207 uint16_t fnum; /* Out */
4210 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4212 struct tevent_req *req = tevent_req_callback_data(
4213 subreq, struct tevent_req);
4214 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4215 NTSTATUS status;
4216 uint8_t *data;
4217 uint32_t num_data;
4219 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4220 NULL, 0, NULL, &data, 12, &num_data);
4221 TALLOC_FREE(subreq);
4222 if (tevent_req_nterror(req, status)) {
4223 return;
4225 state->fnum = SVAL(data,2);
4226 tevent_req_done(req);
4229 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4230 struct event_context *ev,
4231 struct cli_state *cli,
4232 const char *fname,
4233 int flags,
4234 mode_t mode,
4235 bool is_dir)
4237 struct tevent_req *req = NULL, *subreq = NULL;
4238 struct posix_open_state *state = NULL;
4239 uint32_t wire_flags = open_flags_to_wire(flags);
4241 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4242 if (req == NULL) {
4243 return NULL;
4246 /* Setup setup word. */
4247 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4249 /* Setup param array. */
4250 state->param = talloc_array(state, uint8_t, 6);
4251 if (tevent_req_nomem(state->param, req)) {
4252 return tevent_req_post(req, ev);
4254 memset(state->param, '\0', 6);
4255 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4257 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4258 strlen(fname)+1, NULL);
4260 if (tevent_req_nomem(state->param, req)) {
4261 return tevent_req_post(req, ev);
4264 /* Setup data words. */
4265 if (is_dir) {
4266 wire_flags |= SMB_O_DIRECTORY;
4269 SIVAL(state->data,0,0); /* No oplock. */
4270 SIVAL(state->data,4,wire_flags);
4271 SIVAL(state->data,8,unix_perms_to_wire(mode));
4272 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4273 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4275 subreq = cli_trans_send(state, /* mem ctx. */
4276 ev, /* event ctx. */
4277 cli, /* cli_state. */
4278 SMBtrans2, /* cmd. */
4279 NULL, /* pipe name. */
4280 -1, /* fid. */
4281 0, /* function. */
4282 0, /* flags. */
4283 &state->setup, /* setup. */
4284 1, /* num setup uint16_t words. */
4285 0, /* max returned setup. */
4286 state->param, /* param. */
4287 talloc_get_size(state->param),/* num param. */
4288 2, /* max returned param. */
4289 state->data, /* data. */
4290 18, /* num data. */
4291 12); /* max returned data. */
4293 if (tevent_req_nomem(subreq, req)) {
4294 return tevent_req_post(req, ev);
4296 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4297 return req;
4300 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4301 struct event_context *ev,
4302 struct cli_state *cli,
4303 const char *fname,
4304 int flags,
4305 mode_t mode)
4307 return cli_posix_open_internal_send(mem_ctx, ev,
4308 cli, fname, flags, mode, false);
4311 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4313 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4314 NTSTATUS status;
4316 if (tevent_req_is_nterror(req, &status)) {
4317 return status;
4319 *pfnum = state->fnum;
4320 return NT_STATUS_OK;
4323 /****************************************************************************
4324 Open - POSIX semantics. Doesn't request oplock.
4325 ****************************************************************************/
4327 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4328 int flags, mode_t mode, uint16_t *pfnum)
4331 TALLOC_CTX *frame = talloc_stackframe();
4332 struct event_context *ev = NULL;
4333 struct tevent_req *req = NULL;
4334 NTSTATUS status = NT_STATUS_OK;
4336 if (cli_has_async_calls(cli)) {
4338 * Can't use sync call while an async call is in flight
4340 status = NT_STATUS_INVALID_PARAMETER;
4341 goto fail;
4344 ev = event_context_init(frame);
4345 if (ev == NULL) {
4346 status = NT_STATUS_NO_MEMORY;
4347 goto fail;
4350 req = cli_posix_open_send(frame,
4352 cli,
4353 fname,
4354 flags,
4355 mode);
4356 if (req == NULL) {
4357 status = NT_STATUS_NO_MEMORY;
4358 goto fail;
4361 if (!tevent_req_poll(req, ev)) {
4362 status = map_nt_error_from_unix(errno);
4363 goto fail;
4366 status = cli_posix_open_recv(req, pfnum);
4368 fail:
4369 TALLOC_FREE(frame);
4370 return status;
4373 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4374 struct event_context *ev,
4375 struct cli_state *cli,
4376 const char *fname,
4377 mode_t mode)
4379 return cli_posix_open_internal_send(mem_ctx, ev,
4380 cli, fname, O_CREAT, mode, true);
4383 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4385 return tevent_req_simple_recv_ntstatus(req);
4388 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4390 TALLOC_CTX *frame = talloc_stackframe();
4391 struct event_context *ev = NULL;
4392 struct tevent_req *req = NULL;
4393 NTSTATUS status = NT_STATUS_OK;
4395 if (cli_has_async_calls(cli)) {
4397 * Can't use sync call while an async call is in flight
4399 status = NT_STATUS_INVALID_PARAMETER;
4400 goto fail;
4403 ev = event_context_init(frame);
4404 if (ev == NULL) {
4405 status = NT_STATUS_NO_MEMORY;
4406 goto fail;
4409 req = cli_posix_mkdir_send(frame,
4411 cli,
4412 fname,
4413 mode);
4414 if (req == NULL) {
4415 status = NT_STATUS_NO_MEMORY;
4416 goto fail;
4419 if (!tevent_req_poll(req, ev)) {
4420 status = map_nt_error_from_unix(errno);
4421 goto fail;
4424 status = cli_posix_mkdir_recv(req);
4426 fail:
4427 TALLOC_FREE(frame);
4428 return status;
4431 /****************************************************************************
4432 unlink or rmdir - POSIX semantics.
4433 ****************************************************************************/
4435 struct cli_posix_unlink_internal_state {
4436 uint8_t data[2];
4439 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4441 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4442 struct event_context *ev,
4443 struct cli_state *cli,
4444 const char *fname,
4445 uint16_t level)
4447 struct tevent_req *req = NULL, *subreq = NULL;
4448 struct cli_posix_unlink_internal_state *state = NULL;
4450 req = tevent_req_create(mem_ctx, &state,
4451 struct cli_posix_unlink_internal_state);
4452 if (req == NULL) {
4453 return NULL;
4456 /* Setup data word. */
4457 SSVAL(state->data, 0, level);
4459 subreq = cli_setpathinfo_send(state, ev, cli,
4460 SMB_POSIX_PATH_UNLINK,
4461 fname,
4462 state->data, sizeof(state->data));
4463 if (tevent_req_nomem(subreq, req)) {
4464 return tevent_req_post(req, ev);
4466 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4467 return req;
4470 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4472 NTSTATUS status = cli_setpathinfo_recv(subreq);
4473 tevent_req_simple_finish_ntstatus(subreq, status);
4476 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4477 struct event_context *ev,
4478 struct cli_state *cli,
4479 const char *fname)
4481 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4482 SMB_POSIX_UNLINK_FILE_TARGET);
4485 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4487 return tevent_req_simple_recv_ntstatus(req);
4490 /****************************************************************************
4491 unlink - POSIX semantics.
4492 ****************************************************************************/
4494 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4496 TALLOC_CTX *frame = talloc_stackframe();
4497 struct event_context *ev = NULL;
4498 struct tevent_req *req = NULL;
4499 NTSTATUS status = NT_STATUS_OK;
4501 if (cli_has_async_calls(cli)) {
4503 * Can't use sync call while an async call is in flight
4505 status = NT_STATUS_INVALID_PARAMETER;
4506 goto fail;
4509 ev = event_context_init(frame);
4510 if (ev == NULL) {
4511 status = NT_STATUS_NO_MEMORY;
4512 goto fail;
4515 req = cli_posix_unlink_send(frame,
4517 cli,
4518 fname);
4519 if (req == NULL) {
4520 status = NT_STATUS_NO_MEMORY;
4521 goto fail;
4524 if (!tevent_req_poll(req, ev)) {
4525 status = map_nt_error_from_unix(errno);
4526 goto fail;
4529 status = cli_posix_unlink_recv(req);
4531 fail:
4532 TALLOC_FREE(frame);
4533 return status;
4536 /****************************************************************************
4537 rmdir - POSIX semantics.
4538 ****************************************************************************/
4540 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4541 struct event_context *ev,
4542 struct cli_state *cli,
4543 const char *fname)
4545 return cli_posix_unlink_internal_send(
4546 mem_ctx, ev, cli, fname,
4547 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4550 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4552 return tevent_req_simple_recv_ntstatus(req);
4555 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4557 TALLOC_CTX *frame = talloc_stackframe();
4558 struct event_context *ev = NULL;
4559 struct tevent_req *req = NULL;
4560 NTSTATUS status = NT_STATUS_OK;
4562 if (cli_has_async_calls(cli)) {
4564 * Can't use sync call while an async call is in flight
4566 status = NT_STATUS_INVALID_PARAMETER;
4567 goto fail;
4570 ev = event_context_init(frame);
4571 if (ev == NULL) {
4572 status = NT_STATUS_NO_MEMORY;
4573 goto fail;
4576 req = cli_posix_rmdir_send(frame,
4578 cli,
4579 fname);
4580 if (req == NULL) {
4581 status = NT_STATUS_NO_MEMORY;
4582 goto fail;
4585 if (!tevent_req_poll(req, ev)) {
4586 status = map_nt_error_from_unix(errno);
4587 goto fail;
4590 status = cli_posix_rmdir_recv(req, frame);
4592 fail:
4593 TALLOC_FREE(frame);
4594 return status;
4597 /****************************************************************************
4598 filechangenotify
4599 ****************************************************************************/
4601 struct cli_notify_state {
4602 uint8_t setup[8];
4603 uint32_t num_changes;
4604 struct notify_change *changes;
4607 static void cli_notify_done(struct tevent_req *subreq);
4609 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4610 struct tevent_context *ev,
4611 struct cli_state *cli, uint16_t fnum,
4612 uint32_t buffer_size,
4613 uint32_t completion_filter, bool recursive)
4615 struct tevent_req *req, *subreq;
4616 struct cli_notify_state *state;
4618 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4619 if (req == NULL) {
4620 return NULL;
4623 SIVAL(state->setup, 0, completion_filter);
4624 SSVAL(state->setup, 4, fnum);
4625 SSVAL(state->setup, 6, recursive);
4627 subreq = cli_trans_send(
4628 state, /* mem ctx. */
4629 ev, /* event ctx. */
4630 cli, /* cli_state. */
4631 SMBnttrans, /* cmd. */
4632 NULL, /* pipe name. */
4633 -1, /* fid. */
4634 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4635 0, /* flags. */
4636 (uint16_t *)state->setup, /* setup. */
4637 4, /* num setup uint16_t words. */
4638 0, /* max returned setup. */
4639 NULL, /* param. */
4640 0, /* num param. */
4641 buffer_size, /* max returned param. */
4642 NULL, /* data. */
4643 0, /* num data. */
4644 0); /* max returned data. */
4646 if (tevent_req_nomem(subreq, req)) {
4647 return tevent_req_post(req, ev);
4649 tevent_req_set_callback(subreq, cli_notify_done, req);
4650 return req;
4653 static void cli_notify_done(struct tevent_req *subreq)
4655 struct tevent_req *req = tevent_req_callback_data(
4656 subreq, struct tevent_req);
4657 struct cli_notify_state *state = tevent_req_data(
4658 req, struct cli_notify_state);
4659 NTSTATUS status;
4660 uint8_t *params;
4661 uint32_t i, ofs, num_params;
4662 uint16_t flags2;
4664 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4665 &params, 0, &num_params, NULL, 0, NULL);
4666 TALLOC_FREE(subreq);
4667 if (tevent_req_nterror(req, status)) {
4668 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4669 return;
4672 state->num_changes = 0;
4673 ofs = 0;
4675 while (num_params - ofs > 12) {
4676 uint32_t len = IVAL(params, ofs);
4677 state->num_changes += 1;
4679 if ((len == 0) || (ofs+len >= num_params)) {
4680 break;
4682 ofs += len;
4685 state->changes = talloc_array(state, struct notify_change,
4686 state->num_changes);
4687 if (tevent_req_nomem(state->changes, req)) {
4688 TALLOC_FREE(params);
4689 return;
4692 ofs = 0;
4694 for (i=0; i<state->num_changes; i++) {
4695 uint32_t next = IVAL(params, ofs);
4696 uint32_t len = IVAL(params, ofs+8);
4697 ssize_t ret;
4698 char *name;
4700 if ((next != 0) && (len+12 != next)) {
4701 TALLOC_FREE(params);
4702 tevent_req_nterror(
4703 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4704 return;
4707 state->changes[i].action = IVAL(params, ofs+4);
4708 ret = clistr_pull_talloc(params, (char *)params, flags2,
4709 &name, params+ofs+12, len,
4710 STR_TERMINATE|STR_UNICODE);
4711 if (ret == -1) {
4712 TALLOC_FREE(params);
4713 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4714 return;
4716 state->changes[i].name = name;
4717 ofs += next;
4720 TALLOC_FREE(params);
4721 tevent_req_done(req);
4724 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4725 uint32_t *pnum_changes,
4726 struct notify_change **pchanges)
4728 struct cli_notify_state *state = tevent_req_data(
4729 req, struct cli_notify_state);
4730 NTSTATUS status;
4732 if (tevent_req_is_nterror(req, &status)) {
4733 return status;
4736 *pnum_changes = state->num_changes;
4737 *pchanges = talloc_move(mem_ctx, &state->changes);
4738 return NT_STATUS_OK;
4741 struct cli_qpathinfo_state {
4742 uint8_t *param;
4743 uint8_t *data;
4744 uint16_t setup[1];
4745 uint32_t min_rdata;
4746 uint8_t *rdata;
4747 uint32_t num_rdata;
4750 static void cli_qpathinfo_done(struct tevent_req *subreq);
4752 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4753 struct tevent_context *ev,
4754 struct cli_state *cli, const char *fname,
4755 uint16_t level, uint32_t min_rdata,
4756 uint32_t max_rdata)
4758 struct tevent_req *req, *subreq;
4759 struct cli_qpathinfo_state *state;
4761 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4762 if (req == NULL) {
4763 return NULL;
4765 state->min_rdata = min_rdata;
4766 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4768 state->param = talloc_zero_array(state, uint8_t, 6);
4769 if (tevent_req_nomem(state->param, req)) {
4770 return tevent_req_post(req, ev);
4772 SSVAL(state->param, 0, level);
4773 state->param = trans2_bytes_push_str(
4774 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4775 if (tevent_req_nomem(state->param, req)) {
4776 return tevent_req_post(req, ev);
4779 subreq = cli_trans_send(
4780 state, /* mem ctx. */
4781 ev, /* event ctx. */
4782 cli, /* cli_state. */
4783 SMBtrans2, /* cmd. */
4784 NULL, /* pipe name. */
4785 -1, /* fid. */
4786 0, /* function. */
4787 0, /* flags. */
4788 state->setup, /* setup. */
4789 1, /* num setup uint16_t words. */
4790 0, /* max returned setup. */
4791 state->param, /* param. */
4792 talloc_get_size(state->param), /* num param. */
4793 2, /* max returned param. */
4794 NULL, /* data. */
4795 0, /* num data. */
4796 max_rdata); /* max returned data. */
4798 if (tevent_req_nomem(subreq, req)) {
4799 return tevent_req_post(req, ev);
4801 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
4802 return req;
4805 static void cli_qpathinfo_done(struct tevent_req *subreq)
4807 struct tevent_req *req = tevent_req_callback_data(
4808 subreq, struct tevent_req);
4809 struct cli_qpathinfo_state *state = tevent_req_data(
4810 req, struct cli_qpathinfo_state);
4811 NTSTATUS status;
4813 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4814 NULL, 0, NULL,
4815 &state->rdata, state->min_rdata,
4816 &state->num_rdata);
4817 if (tevent_req_nterror(req, status)) {
4818 return;
4820 tevent_req_done(req);
4823 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4824 uint8_t **rdata, uint32_t *num_rdata)
4826 struct cli_qpathinfo_state *state = tevent_req_data(
4827 req, struct cli_qpathinfo_state);
4828 NTSTATUS status;
4830 if (tevent_req_is_nterror(req, &status)) {
4831 return status;
4833 if (rdata != NULL) {
4834 *rdata = talloc_move(mem_ctx, &state->rdata);
4835 } else {
4836 TALLOC_FREE(state->rdata);
4838 if (num_rdata != NULL) {
4839 *num_rdata = state->num_rdata;
4841 return NT_STATUS_OK;
4844 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
4845 const char *fname, uint16_t level, uint32_t min_rdata,
4846 uint32_t max_rdata,
4847 uint8_t **rdata, uint32_t *num_rdata)
4849 TALLOC_CTX *frame = talloc_stackframe();
4850 struct event_context *ev;
4851 struct tevent_req *req;
4852 NTSTATUS status = NT_STATUS_NO_MEMORY;
4854 if (cli_has_async_calls(cli)) {
4856 * Can't use sync call while an async call is in flight
4858 status = NT_STATUS_INVALID_PARAMETER;
4859 goto fail;
4861 ev = event_context_init(frame);
4862 if (ev == NULL) {
4863 goto fail;
4865 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
4866 max_rdata);
4867 if (req == NULL) {
4868 goto fail;
4870 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4871 goto fail;
4873 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
4874 fail:
4875 TALLOC_FREE(frame);
4876 return status;
4879 struct cli_qfileinfo_state {
4880 uint16_t setup[1];
4881 uint8_t param[4];
4882 uint8_t *data;
4883 uint32_t min_rdata;
4884 uint8_t *rdata;
4885 uint32_t num_rdata;
4888 static void cli_qfileinfo_done(struct tevent_req *subreq);
4890 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
4891 struct tevent_context *ev,
4892 struct cli_state *cli, uint16_t fnum,
4893 uint16_t level, uint32_t min_rdata,
4894 uint32_t max_rdata)
4896 struct tevent_req *req, *subreq;
4897 struct cli_qfileinfo_state *state;
4899 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
4900 if (req == NULL) {
4901 return NULL;
4903 state->min_rdata = min_rdata;
4904 SSVAL(state->param, 0, fnum);
4905 SSVAL(state->param, 2, level);
4906 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
4908 subreq = cli_trans_send(
4909 state, /* mem ctx. */
4910 ev, /* event ctx. */
4911 cli, /* cli_state. */
4912 SMBtrans2, /* cmd. */
4913 NULL, /* pipe name. */
4914 -1, /* fid. */
4915 0, /* function. */
4916 0, /* flags. */
4917 state->setup, /* setup. */
4918 1, /* num setup uint16_t words. */
4919 0, /* max returned setup. */
4920 state->param, /* param. */
4921 sizeof(state->param), /* num param. */
4922 2, /* max returned param. */
4923 NULL, /* data. */
4924 0, /* num data. */
4925 max_rdata); /* max returned data. */
4927 if (tevent_req_nomem(subreq, req)) {
4928 return tevent_req_post(req, ev);
4930 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
4931 return req;
4934 static void cli_qfileinfo_done(struct tevent_req *subreq)
4936 struct tevent_req *req = tevent_req_callback_data(
4937 subreq, struct tevent_req);
4938 struct cli_qfileinfo_state *state = tevent_req_data(
4939 req, struct cli_qfileinfo_state);
4940 NTSTATUS status;
4942 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4943 NULL, 0, NULL,
4944 &state->rdata, state->min_rdata,
4945 &state->num_rdata);
4946 if (tevent_req_nterror(req, status)) {
4947 return;
4949 tevent_req_done(req);
4952 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4953 uint8_t **rdata, uint32_t *num_rdata)
4955 struct cli_qfileinfo_state *state = tevent_req_data(
4956 req, struct cli_qfileinfo_state);
4957 NTSTATUS status;
4959 if (tevent_req_is_nterror(req, &status)) {
4960 return status;
4962 if (rdata != NULL) {
4963 *rdata = talloc_move(mem_ctx, &state->rdata);
4964 } else {
4965 TALLOC_FREE(state->rdata);
4967 if (num_rdata != NULL) {
4968 *num_rdata = state->num_rdata;
4970 return NT_STATUS_OK;
4973 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
4974 uint16_t fnum, uint16_t level, uint32_t min_rdata,
4975 uint32_t max_rdata,
4976 uint8_t **rdata, uint32_t *num_rdata)
4978 TALLOC_CTX *frame = talloc_stackframe();
4979 struct event_context *ev;
4980 struct tevent_req *req;
4981 NTSTATUS status = NT_STATUS_NO_MEMORY;
4983 if (cli_has_async_calls(cli)) {
4985 * Can't use sync call while an async call is in flight
4987 status = NT_STATUS_INVALID_PARAMETER;
4988 goto fail;
4990 ev = event_context_init(frame);
4991 if (ev == NULL) {
4992 goto fail;
4994 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
4995 max_rdata);
4996 if (req == NULL) {
4997 goto fail;
4999 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5000 goto fail;
5002 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5003 fail:
5004 TALLOC_FREE(frame);
5005 return status;
5008 struct cli_flush_state {
5009 uint16_t vwv[1];
5012 static void cli_flush_done(struct tevent_req *subreq);
5014 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5015 struct event_context *ev,
5016 struct cli_state *cli,
5017 uint16_t fnum)
5019 struct tevent_req *req, *subreq;
5020 struct cli_flush_state *state;
5022 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5023 if (req == NULL) {
5024 return NULL;
5026 SSVAL(state->vwv + 0, 0, fnum);
5028 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5029 0, NULL);
5030 if (tevent_req_nomem(subreq, req)) {
5031 return tevent_req_post(req, ev);
5033 tevent_req_set_callback(subreq, cli_flush_done, req);
5034 return req;
5037 static void cli_flush_done(struct tevent_req *subreq)
5039 struct tevent_req *req = tevent_req_callback_data(
5040 subreq, struct tevent_req);
5041 NTSTATUS status;
5043 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5044 TALLOC_FREE(subreq);
5045 if (tevent_req_nterror(req, status)) {
5046 return;
5048 tevent_req_done(req);
5051 NTSTATUS cli_flush_recv(struct tevent_req *req)
5053 return tevent_req_simple_recv_ntstatus(req);
5056 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5058 TALLOC_CTX *frame = talloc_stackframe();
5059 struct event_context *ev;
5060 struct tevent_req *req;
5061 NTSTATUS status = NT_STATUS_NO_MEMORY;
5063 if (cli_has_async_calls(cli)) {
5065 * Can't use sync call while an async call is in flight
5067 status = NT_STATUS_INVALID_PARAMETER;
5068 goto fail;
5070 ev = event_context_init(frame);
5071 if (ev == NULL) {
5072 goto fail;
5074 req = cli_flush_send(frame, ev, cli, fnum);
5075 if (req == NULL) {
5076 goto fail;
5078 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5079 goto fail;
5081 status = cli_flush_recv(req);
5082 fail:
5083 TALLOC_FREE(frame);
5084 return status;
5087 struct cli_shadow_copy_data_state {
5088 uint16_t setup[4];
5089 uint8_t *data;
5090 uint32_t num_data;
5091 bool get_names;
5094 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5096 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5097 struct tevent_context *ev,
5098 struct cli_state *cli,
5099 uint16_t fnum,
5100 bool get_names)
5102 struct tevent_req *req, *subreq;
5103 struct cli_shadow_copy_data_state *state;
5104 uint32_t ret_size;
5106 req = tevent_req_create(mem_ctx, &state,
5107 struct cli_shadow_copy_data_state);
5108 if (req == NULL) {
5109 return NULL;
5111 state->get_names = get_names;
5112 ret_size = get_names ? cli->max_xmit : 16;
5114 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5115 SSVAL(state->setup + 2, 0, fnum);
5116 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5117 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5119 subreq = cli_trans_send(
5120 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5121 state->setup, ARRAY_SIZE(state->setup), 0,
5122 NULL, 0, 0,
5123 NULL, 0, ret_size);
5124 if (tevent_req_nomem(subreq, req)) {
5125 return tevent_req_post(req, ev);
5127 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5128 return req;
5131 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5133 struct tevent_req *req = tevent_req_callback_data(
5134 subreq, struct tevent_req);
5135 struct cli_shadow_copy_data_state *state = tevent_req_data(
5136 req, struct cli_shadow_copy_data_state);
5137 NTSTATUS status;
5139 status = cli_trans_recv(subreq, state, NULL,
5140 NULL, 0, NULL, /* setup */
5141 NULL, 0, NULL, /* param */
5142 &state->data, 12, &state->num_data);
5143 TALLOC_FREE(subreq);
5144 if (tevent_req_nterror(req, status)) {
5145 return;
5147 tevent_req_done(req);
5150 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5151 char ***pnames, int *pnum_names)
5153 struct cli_shadow_copy_data_state *state = tevent_req_data(
5154 req, struct cli_shadow_copy_data_state);
5155 char **names;
5156 int i, num_names;
5157 uint32_t dlength;
5158 NTSTATUS status;
5160 if (tevent_req_is_nterror(req, &status)) {
5161 return status;
5163 num_names = IVAL(state->data, 4);
5164 dlength = IVAL(state->data, 8);
5166 if (!state->get_names) {
5167 *pnum_names = num_names;
5168 return NT_STATUS_OK;
5171 if (dlength+12 > state->num_data) {
5172 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5174 names = talloc_array(mem_ctx, char *, num_names);
5175 if (names == NULL) {
5176 return NT_STATUS_NO_MEMORY;
5179 for (i=0; i<num_names; i++) {
5180 bool ret;
5181 uint8_t *src;
5182 size_t converted_size;
5184 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5185 ret = convert_string_talloc(
5186 names, CH_UTF16LE, CH_UNIX,
5187 src, 2 * sizeof(SHADOW_COPY_LABEL),
5188 &names[i], &converted_size);
5189 if (!ret) {
5190 TALLOC_FREE(names);
5191 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5194 *pnum_names = num_names;
5195 *pnames = names;
5196 return NT_STATUS_OK;
5199 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5200 uint16_t fnum, bool get_names,
5201 char ***pnames, int *pnum_names)
5203 TALLOC_CTX *frame = talloc_stackframe();
5204 struct event_context *ev;
5205 struct tevent_req *req;
5206 NTSTATUS status = NT_STATUS_NO_MEMORY;
5208 if (cli_has_async_calls(cli)) {
5210 * Can't use sync call while an async call is in flight
5212 status = NT_STATUS_INVALID_PARAMETER;
5213 goto fail;
5215 ev = event_context_init(frame);
5216 if (ev == NULL) {
5217 goto fail;
5219 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5220 if (req == NULL) {
5221 goto fail;
5223 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5224 goto fail;
5226 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5227 fail:
5228 TALLOC_FREE(frame);
5229 return status;