2 Unix SMB/CIFS implementation.
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/>.
23 /***********************************************************
24 Common function for pushing stings, used by smb_bytes_push_str()
25 and trans_bytes_push_str(). Only difference is the align_odd
27 ***********************************************************/
29 static uint8_t *internal_bytes_push_str(uint8_t *buf
, bool ucs2
,
30 const char *str
, size_t str_len
,
32 size_t *pconverted_size
)
36 size_t converted_size
;
42 buflen
= talloc_get_size(buf
);
44 if (align_odd
&& ucs2
&& (buflen
% 2 == 0)) {
46 * We're pushing into an SMB buffer, align odd
48 buf
= TALLOC_REALLOC_ARRAY(NULL
, buf
, uint8_t, buflen
+ 1);
56 if (!convert_string_talloc(talloc_tos(), CH_UNIX
,
57 ucs2
? CH_UTF16LE
: CH_DOS
,
58 str
, str_len
, &converted
,
59 &converted_size
, true)) {
63 buf
= TALLOC_REALLOC_ARRAY(NULL
, buf
, uint8_t,
64 buflen
+ converted_size
);
66 TALLOC_FREE(converted
);
70 memcpy(buf
+ buflen
, converted
, converted_size
);
72 TALLOC_FREE(converted
);
74 if (pconverted_size
) {
75 *pconverted_size
= converted_size
;
81 /***********************************************************
82 Push a string into an SMB buffer, with odd byte alignment
83 if it's a UCS2 string.
84 ***********************************************************/
86 uint8_t *smb_bytes_push_str(uint8_t *buf
, bool ucs2
,
87 const char *str
, size_t str_len
,
88 size_t *pconverted_size
)
90 return internal_bytes_push_str(buf
, ucs2
, str
, str_len
,
91 true, pconverted_size
);
94 /***********************************************************
95 Same as smb_bytes_push_str(), but without the odd byte
96 align for ucs2 (we're pushing into a param or data block).
97 static for now, although this will probably change when
98 other modules use async trans calls.
99 ***********************************************************/
101 static uint8_t *trans2_bytes_push_str(uint8_t *buf
, bool ucs2
,
102 const char *str
, size_t str_len
,
103 size_t *pconverted_size
)
105 return internal_bytes_push_str(buf
, ucs2
, str
, str_len
,
106 false, pconverted_size
);
109 /****************************************************************************
110 Hard/Symlink a file (UNIX extensions).
111 Creates new name (sym)linked to oldname.
112 ****************************************************************************/
120 static void cli_posix_link_internal_done(struct tevent_req
*subreq
)
122 struct tevent_req
*req
= tevent_req_callback_data(
123 subreq
, struct tevent_req
);
124 struct link_state
*state
= tevent_req_data(req
, struct link_state
);
127 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
129 if (!NT_STATUS_IS_OK(status
)) {
130 tevent_req_nterror(req
, status
);
133 tevent_req_done(req
);
136 static struct tevent_req
*cli_posix_link_internal_send(TALLOC_CTX
*mem_ctx
,
137 struct event_context
*ev
,
138 struct cli_state
*cli
,
143 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
144 struct link_state
*state
= NULL
;
146 req
= tevent_req_create(mem_ctx
, &state
, struct link_state
);
151 /* Setup setup word. */
152 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
154 /* Setup param array. */
155 state
->param
= talloc_array(state
, uint8_t, 6);
156 if (tevent_req_nomem(state
->param
, req
)) {
157 return tevent_req_post(req
, ev
);
159 memset(state
->param
, '\0', 6);
160 SSVAL(state
->param
,0,hardlink
? SMB_SET_FILE_UNIX_HLINK
: SMB_SET_FILE_UNIX_LINK
);
162 state
->param
= trans2_bytes_push_str(state
->param
, cli_ucs2(cli
), newname
,
163 strlen(newname
)+1, NULL
);
165 if (tevent_req_nomem(state
->param
, req
)) {
166 return tevent_req_post(req
, ev
);
169 /* Setup data array. */
170 state
->data
= talloc_array(state
, uint8_t, 0);
171 if (tevent_req_nomem(state
->data
, req
)) {
172 return tevent_req_post(req
, ev
);
174 state
->data
= trans2_bytes_push_str(state
->data
, cli_ucs2(cli
), oldname
,
175 strlen(oldname
)+1, NULL
);
177 subreq
= cli_trans_send(state
, /* mem ctx. */
179 cli
, /* cli_state. */
180 SMBtrans2
, /* cmd. */
181 NULL
, /* pipe name. */
185 &state
->setup
, /* setup. */
186 1, /* num setup uint16_t words. */
187 0, /* max returned setup. */
188 state
->param
, /* param. */
189 talloc_get_size(state
->param
), /* num param. */
190 2, /* max returned param. */
191 state
->data
, /* data. */
192 talloc_get_size(state
->data
), /* num data. */
193 0); /* max returned data. */
195 if (tevent_req_nomem(subreq
, req
)) {
196 return tevent_req_post(req
, ev
);
198 tevent_req_set_callback(subreq
, cli_posix_link_internal_done
, req
);
202 /****************************************************************************
203 Symlink a file (UNIX extensions).
204 ****************************************************************************/
206 struct tevent_req
*cli_posix_symlink_send(TALLOC_CTX
*mem_ctx
,
207 struct event_context
*ev
,
208 struct cli_state
*cli
,
212 return cli_posix_link_internal_send(mem_ctx
, ev
, cli
,
213 oldname
, newname
, false);
216 NTSTATUS
cli_posix_symlink_recv(struct tevent_req
*req
)
220 if (tevent_req_is_nterror(req
, &status
)) {
226 NTSTATUS
cli_posix_symlink(struct cli_state
*cli
,
230 TALLOC_CTX
*frame
= talloc_stackframe();
231 struct event_context
*ev
= NULL
;
232 struct tevent_req
*req
= NULL
;
233 NTSTATUS status
= NT_STATUS_OK
;
235 if (cli_has_async_calls(cli
)) {
237 * Can't use sync call while an async call is in flight
239 status
= NT_STATUS_INVALID_PARAMETER
;
243 ev
= event_context_init(frame
);
245 status
= NT_STATUS_NO_MEMORY
;
249 req
= cli_posix_symlink_send(frame
,
255 status
= NT_STATUS_NO_MEMORY
;
259 if (!tevent_req_poll(req
, ev
)) {
260 status
= map_nt_error_from_unix(errno
);
264 status
= cli_posix_symlink_recv(req
);
268 if (!NT_STATUS_IS_OK(status
)) {
269 cli_set_error(cli
, status
);
274 /****************************************************************************
275 Read a POSIX symlink.
276 ****************************************************************************/
278 struct readlink_state
{
285 static void cli_posix_readlink_done(struct tevent_req
*subreq
)
287 struct tevent_req
*req
= tevent_req_callback_data(
288 subreq
, struct tevent_req
);
289 struct readlink_state
*state
= tevent_req_data(req
, struct readlink_state
);
292 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
,
293 &state
->data
, &state
->num_data
);
295 if (!NT_STATUS_IS_OK(status
)) {
296 tevent_req_nterror(req
, status
);
299 if (state
->num_data
== 0) {
300 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
303 if (state
->data
[state
->num_data
-1] != '\0') {
304 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
307 tevent_req_done(req
);
310 struct tevent_req
*cli_posix_readlink_send(TALLOC_CTX
*mem_ctx
,
311 struct event_context
*ev
,
312 struct cli_state
*cli
,
316 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
317 struct readlink_state
*state
= NULL
;
318 uint32_t maxbytelen
= (uint32_t)(cli_ucs2(cli
) ? len
*3 : len
);
320 if (maxbytelen
< len
) {
324 req
= tevent_req_create(mem_ctx
, &state
, struct readlink_state
);
329 /* Setup setup word. */
330 SSVAL(&state
->setup
, 0, TRANSACT2_QPATHINFO
);
332 /* Setup param array. */
333 state
->param
= talloc_array(state
, uint8_t, 6);
334 if (tevent_req_nomem(state
->param
, req
)) {
335 return tevent_req_post(req
, ev
);
337 memset(state
->param
, '\0', 6);
338 SSVAL(state
->param
,0,SMB_QUERY_FILE_UNIX_LINK
);
340 state
->param
= trans2_bytes_push_str(state
->param
, cli_ucs2(cli
), fname
,
341 strlen(fname
)+1, NULL
);
343 if (tevent_req_nomem(state
->param
, req
)) {
344 return tevent_req_post(req
, ev
);
347 subreq
= cli_trans_send(state
, /* mem ctx. */
349 cli
, /* cli_state. */
350 SMBtrans2
, /* cmd. */
351 NULL
, /* pipe name. */
355 &state
->setup
, /* setup. */
356 1, /* num setup uint16_t words. */
357 0, /* max returned setup. */
358 state
->param
, /* param. */
359 talloc_get_size(state
->param
), /* num param. */
360 2, /* max returned param. */
363 maxbytelen
); /* max returned data. */
365 if (tevent_req_nomem(subreq
, req
)) {
366 return tevent_req_post(req
, ev
);
368 tevent_req_set_callback(subreq
, cli_posix_readlink_done
, req
);
372 NTSTATUS
cli_posix_readlink_recv(struct tevent_req
*req
, struct cli_state
*cli
,
373 char *retpath
, size_t len
)
376 char *converted
= NULL
;
377 size_t converted_size
= 0;
378 struct readlink_state
*state
= tevent_req_data(req
, struct readlink_state
);
380 if (tevent_req_is_nterror(req
, &status
)) {
383 /* The returned data is a pushed string, not raw data. */
384 if (!convert_string_talloc(state
,
385 cli_ucs2(cli
) ? CH_UTF16LE
: CH_DOS
,
392 return NT_STATUS_NO_MEMORY
;
395 len
= MIN(len
,converted_size
);
397 return NT_STATUS_DATA_ERROR
;
399 memcpy(retpath
, converted
, len
);
403 NTSTATUS
cli_posix_readlink(struct cli_state
*cli
, const char *fname
,
404 char *linkpath
, size_t len
)
406 TALLOC_CTX
*frame
= talloc_stackframe();
407 struct event_context
*ev
= NULL
;
408 struct tevent_req
*req
= NULL
;
409 NTSTATUS status
= NT_STATUS_OK
;
411 if (cli_has_async_calls(cli
)) {
413 * Can't use sync call while an async call is in flight
415 status
= NT_STATUS_INVALID_PARAMETER
;
419 ev
= event_context_init(frame
);
421 status
= NT_STATUS_NO_MEMORY
;
425 /* Len is in bytes, we need it in UCS2 units. */
427 status
= NT_STATUS_INVALID_PARAMETER
;
431 req
= cli_posix_readlink_send(frame
,
437 status
= NT_STATUS_NO_MEMORY
;
441 if (!tevent_req_poll(req
, ev
)) {
442 status
= map_nt_error_from_unix(errno
);
446 status
= cli_posix_readlink_recv(req
, cli
, linkpath
, len
);
450 if (!NT_STATUS_IS_OK(status
)) {
451 cli_set_error(cli
, status
);
456 /****************************************************************************
457 Hard link a file (UNIX extensions).
458 ****************************************************************************/
460 struct tevent_req
*cli_posix_hardlink_send(TALLOC_CTX
*mem_ctx
,
461 struct event_context
*ev
,
462 struct cli_state
*cli
,
466 return cli_posix_link_internal_send(mem_ctx
, ev
, cli
,
467 oldname
, newname
, true);
470 NTSTATUS
cli_posix_hardlink_recv(struct tevent_req
*req
)
474 if (tevent_req_is_nterror(req
, &status
)) {
480 NTSTATUS
cli_posix_hardlink(struct cli_state
*cli
,
484 TALLOC_CTX
*frame
= talloc_stackframe();
485 struct event_context
*ev
= NULL
;
486 struct tevent_req
*req
= NULL
;
487 NTSTATUS status
= NT_STATUS_OK
;
489 if (cli_has_async_calls(cli
)) {
491 * Can't use sync call while an async call is in flight
493 status
= NT_STATUS_INVALID_PARAMETER
;
497 ev
= event_context_init(frame
);
499 status
= NT_STATUS_NO_MEMORY
;
503 req
= cli_posix_hardlink_send(frame
,
509 status
= NT_STATUS_NO_MEMORY
;
513 if (!tevent_req_poll(req
, ev
)) {
514 status
= map_nt_error_from_unix(errno
);
518 status
= cli_posix_hardlink_recv(req
);
522 if (!NT_STATUS_IS_OK(status
)) {
523 cli_set_error(cli
, status
);
528 /****************************************************************************
529 Map standard UNIX permissions onto wire representations.
530 ****************************************************************************/
532 uint32_t unix_perms_to_wire(mode_t perms
)
534 unsigned int ret
= 0;
536 ret
|= ((perms
& S_IXOTH
) ? UNIX_X_OTH
: 0);
537 ret
|= ((perms
& S_IWOTH
) ? UNIX_W_OTH
: 0);
538 ret
|= ((perms
& S_IROTH
) ? UNIX_R_OTH
: 0);
539 ret
|= ((perms
& S_IXGRP
) ? UNIX_X_GRP
: 0);
540 ret
|= ((perms
& S_IWGRP
) ? UNIX_W_GRP
: 0);
541 ret
|= ((perms
& S_IRGRP
) ? UNIX_R_GRP
: 0);
542 ret
|= ((perms
& S_IXUSR
) ? UNIX_X_USR
: 0);
543 ret
|= ((perms
& S_IWUSR
) ? UNIX_W_USR
: 0);
544 ret
|= ((perms
& S_IRUSR
) ? UNIX_R_USR
: 0);
546 ret
|= ((perms
& S_ISVTX
) ? UNIX_STICKY
: 0);
549 ret
|= ((perms
& S_ISGID
) ? UNIX_SET_GID
: 0);
552 ret
|= ((perms
& S_ISUID
) ? UNIX_SET_UID
: 0);
557 /****************************************************************************
558 Map wire permissions to standard UNIX.
559 ****************************************************************************/
561 mode_t
wire_perms_to_unix(uint32_t perms
)
563 mode_t ret
= (mode_t
)0;
565 ret
|= ((perms
& UNIX_X_OTH
) ? S_IXOTH
: 0);
566 ret
|= ((perms
& UNIX_W_OTH
) ? S_IWOTH
: 0);
567 ret
|= ((perms
& UNIX_R_OTH
) ? S_IROTH
: 0);
568 ret
|= ((perms
& UNIX_X_GRP
) ? S_IXGRP
: 0);
569 ret
|= ((perms
& UNIX_W_GRP
) ? S_IWGRP
: 0);
570 ret
|= ((perms
& UNIX_R_GRP
) ? S_IRGRP
: 0);
571 ret
|= ((perms
& UNIX_X_USR
) ? S_IXUSR
: 0);
572 ret
|= ((perms
& UNIX_W_USR
) ? S_IWUSR
: 0);
573 ret
|= ((perms
& UNIX_R_USR
) ? S_IRUSR
: 0);
575 ret
|= ((perms
& UNIX_STICKY
) ? S_ISVTX
: 0);
578 ret
|= ((perms
& UNIX_SET_GID
) ? S_ISGID
: 0);
581 ret
|= ((perms
& UNIX_SET_UID
) ? S_ISUID
: 0);
586 /****************************************************************************
587 Return the file type from the wire filetype for UNIX extensions.
588 ****************************************************************************/
590 static mode_t
unix_filetype_from_wire(uint32_t wire_type
)
598 case UNIX_TYPE_SYMLINK
:
602 case UNIX_TYPE_CHARDEV
:
606 case UNIX_TYPE_BLKDEV
:
614 case UNIX_TYPE_SOCKET
:
622 /****************************************************************************
623 Do a POSIX getfacl (UNIX extensions).
624 ****************************************************************************/
626 struct getfacl_state
{
633 static void cli_posix_getfacl_done(struct tevent_req
*subreq
)
635 struct tevent_req
*req
= tevent_req_callback_data(
636 subreq
, struct tevent_req
);
637 struct getfacl_state
*state
= tevent_req_data(req
, struct getfacl_state
);
640 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
,
641 &state
->data
, &state
->num_data
);
643 if (!NT_STATUS_IS_OK(status
)) {
644 tevent_req_nterror(req
, status
);
647 tevent_req_done(req
);
650 struct tevent_req
*cli_posix_getfacl_send(TALLOC_CTX
*mem_ctx
,
651 struct event_context
*ev
,
652 struct cli_state
*cli
,
655 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
656 struct link_state
*state
= NULL
;
658 req
= tevent_req_create(mem_ctx
, &state
, struct getfacl_state
);
663 /* Setup setup word. */
664 SSVAL(&state
->setup
, 0, TRANSACT2_QPATHINFO
);
666 /* Setup param array. */
667 state
->param
= talloc_array(state
, uint8_t, 6);
668 if (tevent_req_nomem(state
->param
, req
)) {
669 return tevent_req_post(req
, ev
);
671 memset(state
->param
, '\0', 6);
672 SSVAL(state
->param
, 0, SMB_QUERY_POSIX_ACL
);
674 state
->param
= trans2_bytes_push_str(state
->param
, cli_ucs2(cli
), fname
,
675 strlen(fname
)+1, NULL
);
677 if (tevent_req_nomem(state
->param
, req
)) {
678 return tevent_req_post(req
, ev
);
681 subreq
= cli_trans_send(state
, /* mem ctx. */
683 cli
, /* cli_state. */
684 SMBtrans2
, /* cmd. */
685 NULL
, /* pipe name. */
689 &state
->setup
, /* setup. */
690 1, /* num setup uint16_t words. */
691 0, /* max returned setup. */
692 state
->param
, /* param. */
693 talloc_get_size(state
->param
), /* num param. */
694 2, /* max returned param. */
697 cli
->max_xmit
); /* max returned data. */
699 if (tevent_req_nomem(subreq
, req
)) {
700 return tevent_req_post(req
, ev
);
702 tevent_req_set_callback(subreq
, cli_posix_getfacl_done
, req
);
706 NTSTATUS
cli_posix_getfacl_recv(struct tevent_req
*req
,
711 struct getfacl_state
*state
= tevent_req_data(req
, struct getfacl_state
);
714 if (tevent_req_is_nterror(req
, &status
)) {
717 *prb_size
= (size_t)state
->num_data
;
718 *retbuf
= (char *)talloc_move(mem_ctx
, &state
->data
);
722 NTSTATUS
cli_posix_getfacl(struct cli_state
*cli
,
728 TALLOC_CTX
*frame
= talloc_stackframe();
729 struct event_context
*ev
= NULL
;
730 struct tevent_req
*req
= NULL
;
731 NTSTATUS status
= NT_STATUS_OK
;
733 if (cli_has_async_calls(cli
)) {
735 * Can't use sync call while an async call is in flight
737 status
= NT_STATUS_INVALID_PARAMETER
;
741 ev
= event_context_init(frame
);
743 status
= NT_STATUS_NO_MEMORY
;
747 req
= cli_posix_getfacl_send(frame
,
752 status
= NT_STATUS_NO_MEMORY
;
756 if (!tevent_req_poll(req
, ev
)) {
757 status
= map_nt_error_from_unix(errno
);
761 status
= cli_posix_getfacl_recv(req
, mem_ctx
, prb_size
, retbuf
);
765 if (!NT_STATUS_IS_OK(status
)) {
766 cli_set_error(cli
, status
);
771 /****************************************************************************
772 Stat a file (UNIX extensions).
773 ****************************************************************************/
782 static void cli_posix_stat_done(struct tevent_req
*subreq
)
784 struct tevent_req
*req
= tevent_req_callback_data(
785 subreq
, struct tevent_req
);
786 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
789 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
,
790 &state
->data
, &state
->num_data
);
792 if (!NT_STATUS_IS_OK(status
)) {
793 tevent_req_nterror(req
, status
);
796 tevent_req_done(req
);
799 struct tevent_req
*cli_posix_stat_send(TALLOC_CTX
*mem_ctx
,
800 struct event_context
*ev
,
801 struct cli_state
*cli
,
804 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
805 struct stat_state
*state
= NULL
;
807 req
= tevent_req_create(mem_ctx
, &state
, struct stat_state
);
812 /* Setup setup word. */
813 SSVAL(&state
->setup
, 0, TRANSACT2_QPATHINFO
);
815 /* Setup param array. */
816 state
->param
= talloc_array(state
, uint8_t, 6);
817 if (tevent_req_nomem(state
->param
, req
)) {
818 return tevent_req_post(req
, ev
);
820 memset(state
->param
, '\0', 6);
821 SSVAL(state
->param
, 0, SMB_QUERY_FILE_UNIX_BASIC
);
823 state
->param
= trans2_bytes_push_str(state
->param
, cli_ucs2(cli
), fname
,
824 strlen(fname
)+1, NULL
);
826 if (tevent_req_nomem(state
->param
, req
)) {
827 return tevent_req_post(req
, ev
);
830 subreq
= cli_trans_send(state
, /* mem ctx. */
832 cli
, /* cli_state. */
833 SMBtrans2
, /* cmd. */
834 NULL
, /* pipe name. */
838 &state
->setup
, /* setup. */
839 1, /* num setup uint16_t words. */
840 0, /* max returned setup. */
841 state
->param
, /* param. */
842 talloc_get_size(state
->param
), /* num param. */
843 2, /* max returned param. */
846 96); /* max returned data. */
848 if (tevent_req_nomem(subreq
, req
)) {
849 return tevent_req_post(req
, ev
);
851 tevent_req_set_callback(subreq
, cli_posix_stat_done
, req
);
855 NTSTATUS
cli_posix_stat_recv(struct tevent_req
*req
,
856 SMB_STRUCT_STAT
*sbuf
)
858 struct stat_state
*state
= tevent_req_data(req
, struct stat_state
);
861 if (tevent_req_is_nterror(req
, &status
)) {
865 if (state
->num_data
!= 96) {
866 return NT_STATUS_DATA_ERROR
;
869 sbuf
->st_ex_size
= IVAL2_TO_SMB_BIG_UINT(state
->data
,0); /* total size, in bytes */
870 sbuf
->st_ex_blocks
= IVAL2_TO_SMB_BIG_UINT(state
->data
,8); /* number of blocks allocated */
871 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
872 sbuf
->st_ex_blocks
/= STAT_ST_BLOCKSIZE
;
874 /* assume 512 byte blocks */
875 sbuf
->st_ex_blocks
/= 512;
877 sbuf
->st_ex_ctime
= interpret_long_date((char *)(state
->data
+ 16)); /* time of last change */
878 sbuf
->st_ex_atime
= interpret_long_date((char *)(state
->data
+ 24)); /* time of last access */
879 sbuf
->st_ex_mtime
= interpret_long_date((char *)(state
->data
+ 32)); /* time of last modification */
881 sbuf
->st_ex_uid
= (uid_t
) IVAL(state
->data
,40); /* user ID of owner */
882 sbuf
->st_ex_gid
= (gid_t
) IVAL(state
->data
,48); /* group ID of owner */
883 sbuf
->st_ex_mode
= unix_filetype_from_wire(IVAL(state
->data
, 56));
884 #if defined(HAVE_MAKEDEV)
886 uint32_t dev_major
= IVAL(state
->data
,60);
887 uint32_t dev_minor
= IVAL(state
->data
,68);
888 sbuf
->st_ex_rdev
= makedev(dev_major
, dev_minor
);
891 sbuf
->st_ex_ino
= (SMB_INO_T
)IVAL2_TO_SMB_BIG_UINT(state
->data
,76); /* inode */
892 sbuf
->st_ex_mode
|= wire_perms_to_unix(IVAL(state
->data
,84)); /* protection */
893 sbuf
->st_ex_nlink
= IVAL(state
->data
,92); /* number of hard links */
898 NTSTATUS
cli_posix_stat(struct cli_state
*cli
,
900 SMB_STRUCT_STAT
*sbuf
)
902 TALLOC_CTX
*frame
= talloc_stackframe();
903 struct event_context
*ev
= NULL
;
904 struct tevent_req
*req
= NULL
;
905 NTSTATUS status
= NT_STATUS_OK
;
907 if (cli_has_async_calls(cli
)) {
909 * Can't use sync call while an async call is in flight
911 status
= NT_STATUS_INVALID_PARAMETER
;
915 ev
= event_context_init(frame
);
917 status
= NT_STATUS_NO_MEMORY
;
921 req
= cli_posix_stat_send(frame
,
926 status
= NT_STATUS_NO_MEMORY
;
930 if (!tevent_req_poll(req
, ev
)) {
931 status
= map_nt_error_from_unix(errno
);
935 status
= cli_posix_stat_recv(req
, sbuf
);
939 if (!NT_STATUS_IS_OK(status
)) {
940 cli_set_error(cli
, status
);
945 /****************************************************************************
946 Chmod or chown a file internal (UNIX extensions).
947 ****************************************************************************/
955 static void cli_posix_chown_chmod_internal_done(struct tevent_req
*subreq
)
957 struct tevent_req
*req
= tevent_req_callback_data(
958 subreq
, struct tevent_req
);
959 struct ch_state
*state
= tevent_req_data(req
, struct ch_state
);
962 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
964 if (!NT_STATUS_IS_OK(status
)) {
965 tevent_req_nterror(req
, status
);
968 tevent_req_done(req
);
971 static struct tevent_req
*cli_posix_chown_chmod_internal_send(TALLOC_CTX
*mem_ctx
,
972 struct event_context
*ev
,
973 struct cli_state
*cli
,
979 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
980 struct ch_state
*state
= NULL
;
982 req
= tevent_req_create(mem_ctx
, &state
, struct ch_state
);
987 /* Setup setup word. */
988 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
990 /* Setup param array. */
991 state
->param
= talloc_array(state
, uint8_t, 6);
992 if (tevent_req_nomem(state
->param
, req
)) {
993 return tevent_req_post(req
, ev
);
995 memset(state
->param
, '\0', 6);
996 SSVAL(state
->param
,0,SMB_SET_FILE_UNIX_BASIC
);
998 state
->param
= trans2_bytes_push_str(state
->param
, cli_ucs2(cli
), fname
,
999 strlen(fname
)+1, NULL
);
1001 if (tevent_req_nomem(state
->param
, req
)) {
1002 return tevent_req_post(req
, ev
);
1005 /* Setup data array. */
1006 state
->data
= talloc_array(state
, uint8_t, 100);
1007 if (tevent_req_nomem(state
->data
, req
)) {
1008 return tevent_req_post(req
, ev
);
1010 memset(state
->data
, 0xff, 40); /* Set all sizes/times to no change. */
1011 memset(&state
->data
[40], '\0', 60);
1012 SIVAL(state
->data
,40,uid
);
1013 SIVAL(state
->data
,48,gid
);
1014 SIVAL(state
->data
,84,mode
);
1016 subreq
= cli_trans_send(state
, /* mem ctx. */
1017 ev
, /* event ctx. */
1018 cli
, /* cli_state. */
1019 SMBtrans2
, /* cmd. */
1020 NULL
, /* pipe name. */
1024 &state
->setup
, /* setup. */
1025 1, /* num setup uint16_t words. */
1026 0, /* max returned setup. */
1027 state
->param
, /* param. */
1028 talloc_get_size(state
->param
), /* num param. */
1029 2, /* max returned param. */
1030 state
->data
, /* data. */
1031 talloc_get_size(state
->data
), /* num data. */
1032 0); /* max returned data. */
1034 if (tevent_req_nomem(subreq
, req
)) {
1035 return tevent_req_post(req
, ev
);
1037 tevent_req_set_callback(subreq
, cli_posix_chown_chmod_internal_done
, req
);
1041 /****************************************************************************
1042 chmod a file (UNIX extensions).
1043 ****************************************************************************/
1045 struct tevent_req
*cli_posix_chmod_send(TALLOC_CTX
*mem_ctx
,
1046 struct event_context
*ev
,
1047 struct cli_state
*cli
,
1051 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
1053 unix_perms_to_wire(mode
),
1058 NTSTATUS
cli_posix_chmod_recv(struct tevent_req
*req
)
1062 if (tevent_req_is_nterror(req
, &status
)) {
1065 return NT_STATUS_OK
;
1068 NTSTATUS
cli_posix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
1070 TALLOC_CTX
*frame
= talloc_stackframe();
1071 struct event_context
*ev
= NULL
;
1072 struct tevent_req
*req
= NULL
;
1073 NTSTATUS status
= NT_STATUS_OK
;
1075 if (cli_has_async_calls(cli
)) {
1077 * Can't use sync call while an async call is in flight
1079 status
= NT_STATUS_INVALID_PARAMETER
;
1083 ev
= event_context_init(frame
);
1085 status
= NT_STATUS_NO_MEMORY
;
1089 req
= cli_posix_chmod_send(frame
,
1095 status
= NT_STATUS_NO_MEMORY
;
1099 if (!tevent_req_poll(req
, ev
)) {
1100 status
= map_nt_error_from_unix(errno
);
1104 status
= cli_posix_chmod_recv(req
);
1108 if (!NT_STATUS_IS_OK(status
)) {
1109 cli_set_error(cli
, status
);
1114 /****************************************************************************
1115 chown a file (UNIX extensions).
1116 ****************************************************************************/
1118 struct tevent_req
*cli_posix_chown_send(TALLOC_CTX
*mem_ctx
,
1119 struct event_context
*ev
,
1120 struct cli_state
*cli
,
1125 return cli_posix_chown_chmod_internal_send(mem_ctx
, ev
, cli
,
1132 NTSTATUS
cli_posix_chown_recv(struct tevent_req
*req
)
1136 if (tevent_req_is_nterror(req
, &status
)) {
1139 return NT_STATUS_OK
;
1142 NTSTATUS
cli_posix_chown(struct cli_state
*cli
,
1147 TALLOC_CTX
*frame
= talloc_stackframe();
1148 struct event_context
*ev
= NULL
;
1149 struct tevent_req
*req
= NULL
;
1150 NTSTATUS status
= NT_STATUS_OK
;
1152 if (cli_has_async_calls(cli
)) {
1154 * Can't use sync call while an async call is in flight
1156 status
= NT_STATUS_INVALID_PARAMETER
;
1160 ev
= event_context_init(frame
);
1162 status
= NT_STATUS_NO_MEMORY
;
1166 req
= cli_posix_chown_send(frame
,
1173 status
= NT_STATUS_NO_MEMORY
;
1177 if (!tevent_req_poll(req
, ev
)) {
1178 status
= map_nt_error_from_unix(errno
);
1182 status
= cli_posix_chown_recv(req
);
1186 if (!NT_STATUS_IS_OK(status
)) {
1187 cli_set_error(cli
, status
);
1192 /****************************************************************************
1194 ****************************************************************************/
1196 static void cli_rename_done(struct tevent_req
*subreq
);
1198 struct cli_rename_state
{
1202 struct tevent_req
*cli_rename_send(TALLOC_CTX
*mem_ctx
,
1203 struct event_context
*ev
,
1204 struct cli_state
*cli
,
1205 const char *fname_src
,
1206 const char *fname_dst
)
1208 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1209 struct cli_rename_state
*state
= NULL
;
1210 uint8_t additional_flags
= 0;
1211 uint8_t *bytes
= NULL
;
1213 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rename_state
);
1218 SSVAL(state
->vwv
+0, 0, aSYSTEM
| aHIDDEN
| aDIR
);
1220 bytes
= talloc_array(state
, uint8_t, 1);
1221 if (tevent_req_nomem(bytes
, req
)) {
1222 return tevent_req_post(req
, ev
);
1225 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_src
,
1226 strlen(fname_src
)+1, NULL
);
1227 if (tevent_req_nomem(bytes
, req
)) {
1228 return tevent_req_post(req
, ev
);
1231 bytes
= TALLOC_REALLOC_ARRAY(state
, bytes
, uint8_t,
1232 talloc_get_size(bytes
)+1);
1233 if (tevent_req_nomem(bytes
, req
)) {
1234 return tevent_req_post(req
, ev
);
1237 bytes
[talloc_get_size(bytes
)-1] = 4;
1238 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_dst
,
1239 strlen(fname_dst
)+1, NULL
);
1240 if (tevent_req_nomem(bytes
, req
)) {
1241 return tevent_req_post(req
, ev
);
1244 subreq
= cli_smb_send(state
, ev
, cli
, SMBmv
, additional_flags
,
1245 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1246 if (tevent_req_nomem(subreq
, req
)) {
1247 return tevent_req_post(req
, ev
);
1249 tevent_req_set_callback(subreq
, cli_rename_done
, req
);
1253 static void cli_rename_done(struct tevent_req
*subreq
)
1255 struct tevent_req
*req
= tevent_req_callback_data(
1256 subreq
, struct tevent_req
);
1259 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
1260 TALLOC_FREE(subreq
);
1261 if (!NT_STATUS_IS_OK(status
)) {
1262 tevent_req_nterror(req
, status
);
1265 tevent_req_done(req
);
1268 NTSTATUS
cli_rename_recv(struct tevent_req
*req
)
1270 return tevent_req_simple_recv_ntstatus(req
);
1273 NTSTATUS
cli_rename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1275 TALLOC_CTX
*frame
= talloc_stackframe();
1276 struct event_context
*ev
;
1277 struct tevent_req
*req
;
1278 NTSTATUS status
= NT_STATUS_OK
;
1280 if (cli_has_async_calls(cli
)) {
1282 * Can't use sync call while an async call is in flight
1284 status
= NT_STATUS_INVALID_PARAMETER
;
1288 ev
= event_context_init(frame
);
1290 status
= NT_STATUS_NO_MEMORY
;
1294 req
= cli_rename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1296 status
= NT_STATUS_NO_MEMORY
;
1300 if (!tevent_req_poll(req
, ev
)) {
1301 status
= map_nt_error_from_unix(errno
);
1305 status
= cli_rename_recv(req
);
1309 if (!NT_STATUS_IS_OK(status
)) {
1310 cli_set_error(cli
, status
);
1315 /****************************************************************************
1317 ****************************************************************************/
1319 static void cli_ntrename_internal_done(struct tevent_req
*subreq
);
1321 struct cli_ntrename_internal_state
{
1325 static struct tevent_req
*cli_ntrename_internal_send(TALLOC_CTX
*mem_ctx
,
1326 struct event_context
*ev
,
1327 struct cli_state
*cli
,
1328 const char *fname_src
,
1329 const char *fname_dst
,
1330 uint16_t rename_flag
)
1332 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1333 struct cli_ntrename_internal_state
*state
= NULL
;
1334 uint8_t additional_flags
= 0;
1335 uint8_t *bytes
= NULL
;
1337 req
= tevent_req_create(mem_ctx
, &state
,
1338 struct cli_ntrename_internal_state
);
1343 SSVAL(state
->vwv
+0, 0 ,aSYSTEM
| aHIDDEN
| aDIR
);
1344 SSVAL(state
->vwv
+1, 0, rename_flag
);
1346 bytes
= talloc_array(state
, uint8_t, 1);
1347 if (tevent_req_nomem(bytes
, req
)) {
1348 return tevent_req_post(req
, ev
);
1351 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_src
,
1352 strlen(fname_src
)+1, NULL
);
1353 if (tevent_req_nomem(bytes
, req
)) {
1354 return tevent_req_post(req
, ev
);
1357 bytes
= TALLOC_REALLOC_ARRAY(state
, bytes
, uint8_t,
1358 talloc_get_size(bytes
)+1);
1359 if (tevent_req_nomem(bytes
, req
)) {
1360 return tevent_req_post(req
, ev
);
1363 bytes
[talloc_get_size(bytes
)-1] = 4;
1364 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_dst
,
1365 strlen(fname_dst
)+1, NULL
);
1366 if (tevent_req_nomem(bytes
, req
)) {
1367 return tevent_req_post(req
, ev
);
1370 subreq
= cli_smb_send(state
, ev
, cli
, SMBntrename
, additional_flags
,
1371 4, state
->vwv
, talloc_get_size(bytes
), bytes
);
1372 if (tevent_req_nomem(subreq
, req
)) {
1373 return tevent_req_post(req
, ev
);
1375 tevent_req_set_callback(subreq
, cli_ntrename_internal_done
, req
);
1379 static void cli_ntrename_internal_done(struct tevent_req
*subreq
)
1381 struct tevent_req
*req
= tevent_req_callback_data(
1382 subreq
, struct tevent_req
);
1385 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
1386 TALLOC_FREE(subreq
);
1387 if (!NT_STATUS_IS_OK(status
)) {
1388 tevent_req_nterror(req
, status
);
1391 tevent_req_done(req
);
1394 static NTSTATUS
cli_ntrename_internal_recv(struct tevent_req
*req
)
1396 return tevent_req_simple_recv_ntstatus(req
);
1399 struct tevent_req
*cli_ntrename_send(TALLOC_CTX
*mem_ctx
,
1400 struct event_context
*ev
,
1401 struct cli_state
*cli
,
1402 const char *fname_src
,
1403 const char *fname_dst
)
1405 return cli_ntrename_internal_send(mem_ctx
,
1410 RENAME_FLAG_RENAME
);
1413 NTSTATUS
cli_ntrename_recv(struct tevent_req
*req
)
1415 return cli_ntrename_internal_recv(req
);
1418 NTSTATUS
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1420 TALLOC_CTX
*frame
= talloc_stackframe();
1421 struct event_context
*ev
;
1422 struct tevent_req
*req
;
1423 NTSTATUS status
= NT_STATUS_OK
;
1425 if (cli_has_async_calls(cli
)) {
1427 * Can't use sync call while an async call is in flight
1429 status
= NT_STATUS_INVALID_PARAMETER
;
1433 ev
= event_context_init(frame
);
1435 status
= NT_STATUS_NO_MEMORY
;
1439 req
= cli_ntrename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1441 status
= NT_STATUS_NO_MEMORY
;
1445 if (!tevent_req_poll(req
, ev
)) {
1446 status
= map_nt_error_from_unix(errno
);
1450 status
= cli_ntrename_recv(req
);
1454 if (!NT_STATUS_IS_OK(status
)) {
1455 cli_set_error(cli
, status
);
1460 /****************************************************************************
1462 ****************************************************************************/
1464 struct tevent_req
*cli_nt_hardlink_send(TALLOC_CTX
*mem_ctx
,
1465 struct event_context
*ev
,
1466 struct cli_state
*cli
,
1467 const char *fname_src
,
1468 const char *fname_dst
)
1470 return cli_ntrename_internal_send(mem_ctx
,
1475 RENAME_FLAG_HARD_LINK
);
1478 NTSTATUS
cli_nt_hardlink_recv(struct tevent_req
*req
)
1480 return cli_ntrename_internal_recv(req
);
1483 NTSTATUS
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
1485 TALLOC_CTX
*frame
= talloc_stackframe();
1486 struct event_context
*ev
;
1487 struct tevent_req
*req
;
1488 NTSTATUS status
= NT_STATUS_OK
;
1490 if (cli_has_async_calls(cli
)) {
1492 * Can't use sync call while an async call is in flight
1494 status
= NT_STATUS_INVALID_PARAMETER
;
1498 ev
= event_context_init(frame
);
1500 status
= NT_STATUS_NO_MEMORY
;
1504 req
= cli_nt_hardlink_send(frame
, ev
, cli
, fname_src
, fname_dst
);
1506 status
= NT_STATUS_NO_MEMORY
;
1510 if (!tevent_req_poll(req
, ev
)) {
1511 status
= map_nt_error_from_unix(errno
);
1515 status
= cli_nt_hardlink_recv(req
);
1519 if (!NT_STATUS_IS_OK(status
)) {
1520 cli_set_error(cli
, status
);
1525 /****************************************************************************
1527 ****************************************************************************/
1529 static void cli_unlink_done(struct tevent_req
*subreq
);
1531 struct cli_unlink_state
{
1535 struct tevent_req
*cli_unlink_send(TALLOC_CTX
*mem_ctx
,
1536 struct event_context
*ev
,
1537 struct cli_state
*cli
,
1539 uint16_t mayhave_attrs
)
1541 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1542 struct cli_unlink_state
*state
= NULL
;
1543 uint8_t additional_flags
= 0;
1544 uint8_t *bytes
= NULL
;
1546 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlink_state
);
1551 SSVAL(state
->vwv
+0, 0, mayhave_attrs
);
1553 bytes
= talloc_array(state
, uint8_t, 1);
1554 if (tevent_req_nomem(bytes
, req
)) {
1555 return tevent_req_post(req
, ev
);
1558 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
1559 strlen(fname
)+1, NULL
);
1561 if (tevent_req_nomem(bytes
, req
)) {
1562 return tevent_req_post(req
, ev
);
1565 subreq
= cli_smb_send(state
, ev
, cli
, SMBunlink
, additional_flags
,
1566 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
1567 if (tevent_req_nomem(subreq
, req
)) {
1568 return tevent_req_post(req
, ev
);
1570 tevent_req_set_callback(subreq
, cli_unlink_done
, req
);
1574 static void cli_unlink_done(struct tevent_req
*subreq
)
1576 struct tevent_req
*req
= tevent_req_callback_data(
1577 subreq
, struct tevent_req
);
1580 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
1581 TALLOC_FREE(subreq
);
1582 if (!NT_STATUS_IS_OK(status
)) {
1583 tevent_req_nterror(req
, status
);
1586 tevent_req_done(req
);
1589 NTSTATUS
cli_unlink_recv(struct tevent_req
*req
)
1591 return tevent_req_simple_recv_ntstatus(req
);
1594 NTSTATUS
cli_unlink(struct cli_state
*cli
, const char *fname
, uint16_t mayhave_attrs
)
1596 TALLOC_CTX
*frame
= talloc_stackframe();
1597 struct event_context
*ev
;
1598 struct tevent_req
*req
;
1599 NTSTATUS status
= NT_STATUS_OK
;
1601 if (cli_has_async_calls(cli
)) {
1603 * Can't use sync call while an async call is in flight
1605 status
= NT_STATUS_INVALID_PARAMETER
;
1609 ev
= event_context_init(frame
);
1611 status
= NT_STATUS_NO_MEMORY
;
1615 req
= cli_unlink_send(frame
, ev
, cli
, fname
, mayhave_attrs
);
1617 status
= NT_STATUS_NO_MEMORY
;
1621 if (!tevent_req_poll(req
, ev
)) {
1622 status
= map_nt_error_from_unix(errno
);
1626 status
= cli_unlink_recv(req
);
1630 if (!NT_STATUS_IS_OK(status
)) {
1631 cli_set_error(cli
, status
);
1636 /****************************************************************************
1638 ****************************************************************************/
1640 static void cli_mkdir_done(struct tevent_req
*subreq
);
1642 struct cli_mkdir_state
{
1646 struct tevent_req
*cli_mkdir_send(TALLOC_CTX
*mem_ctx
,
1647 struct event_context
*ev
,
1648 struct cli_state
*cli
,
1651 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1652 struct cli_mkdir_state
*state
= NULL
;
1653 uint8_t additional_flags
= 0;
1654 uint8_t *bytes
= NULL
;
1656 req
= tevent_req_create(mem_ctx
, &state
, struct cli_mkdir_state
);
1661 bytes
= talloc_array(state
, uint8_t, 1);
1662 if (tevent_req_nomem(bytes
, req
)) {
1663 return tevent_req_post(req
, ev
);
1666 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), dname
,
1667 strlen(dname
)+1, NULL
);
1669 if (tevent_req_nomem(bytes
, req
)) {
1670 return tevent_req_post(req
, ev
);
1673 subreq
= cli_smb_send(state
, ev
, cli
, SMBmkdir
, additional_flags
,
1674 0, NULL
, talloc_get_size(bytes
), bytes
);
1675 if (tevent_req_nomem(subreq
, req
)) {
1676 return tevent_req_post(req
, ev
);
1678 tevent_req_set_callback(subreq
, cli_mkdir_done
, req
);
1682 static void cli_mkdir_done(struct tevent_req
*subreq
)
1684 struct tevent_req
*req
= tevent_req_callback_data(
1685 subreq
, struct tevent_req
);
1688 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
1689 TALLOC_FREE(subreq
);
1690 if (!NT_STATUS_IS_OK(status
)) {
1691 tevent_req_nterror(req
, status
);
1694 tevent_req_done(req
);
1697 NTSTATUS
cli_mkdir_recv(struct tevent_req
*req
)
1699 return tevent_req_simple_recv_ntstatus(req
);
1702 NTSTATUS
cli_mkdir(struct cli_state
*cli
, const char *dname
)
1704 TALLOC_CTX
*frame
= talloc_stackframe();
1705 struct event_context
*ev
;
1706 struct tevent_req
*req
;
1707 NTSTATUS status
= NT_STATUS_OK
;
1709 if (cli_has_async_calls(cli
)) {
1711 * Can't use sync call while an async call is in flight
1713 status
= NT_STATUS_INVALID_PARAMETER
;
1717 ev
= event_context_init(frame
);
1719 status
= NT_STATUS_NO_MEMORY
;
1723 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
1725 status
= NT_STATUS_NO_MEMORY
;
1729 if (!tevent_req_poll(req
, ev
)) {
1730 status
= map_nt_error_from_unix(errno
);
1734 status
= cli_mkdir_recv(req
);
1738 if (!NT_STATUS_IS_OK(status
)) {
1739 cli_set_error(cli
, status
);
1744 /****************************************************************************
1746 ****************************************************************************/
1748 static void cli_rmdir_done(struct tevent_req
*subreq
);
1750 struct cli_rmdir_state
{
1754 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
1755 struct event_context
*ev
,
1756 struct cli_state
*cli
,
1759 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1760 struct cli_rmdir_state
*state
= NULL
;
1761 uint8_t additional_flags
= 0;
1762 uint8_t *bytes
= NULL
;
1764 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
1769 bytes
= talloc_array(state
, uint8_t, 1);
1770 if (tevent_req_nomem(bytes
, req
)) {
1771 return tevent_req_post(req
, ev
);
1774 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), dname
,
1775 strlen(dname
)+1, NULL
);
1777 if (tevent_req_nomem(bytes
, req
)) {
1778 return tevent_req_post(req
, ev
);
1781 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
1782 0, NULL
, talloc_get_size(bytes
), bytes
);
1783 if (tevent_req_nomem(subreq
, req
)) {
1784 return tevent_req_post(req
, ev
);
1786 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
1790 static void cli_rmdir_done(struct tevent_req
*subreq
)
1792 struct tevent_req
*req
= tevent_req_callback_data(
1793 subreq
, struct tevent_req
);
1796 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
1797 TALLOC_FREE(subreq
);
1798 if (!NT_STATUS_IS_OK(status
)) {
1799 tevent_req_nterror(req
, status
);
1802 tevent_req_done(req
);
1805 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
1807 return tevent_req_simple_recv_ntstatus(req
);
1810 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
1812 TALLOC_CTX
*frame
= talloc_stackframe();
1813 struct event_context
*ev
;
1814 struct tevent_req
*req
;
1815 NTSTATUS status
= NT_STATUS_OK
;
1817 if (cli_has_async_calls(cli
)) {
1819 * Can't use sync call while an async call is in flight
1821 status
= NT_STATUS_INVALID_PARAMETER
;
1825 ev
= event_context_init(frame
);
1827 status
= NT_STATUS_NO_MEMORY
;
1831 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
1833 status
= NT_STATUS_NO_MEMORY
;
1837 if (!tevent_req_poll(req
, ev
)) {
1838 status
= map_nt_error_from_unix(errno
);
1842 status
= cli_rmdir_recv(req
);
1846 if (!NT_STATUS_IS_OK(status
)) {
1847 cli_set_error(cli
, status
);
1852 /****************************************************************************
1853 Set or clear the delete on close flag.
1854 ****************************************************************************/
1862 static void cli_nt_delete_on_close_done(struct tevent_req
*subreq
)
1864 struct tevent_req
*req
= tevent_req_callback_data(
1865 subreq
, struct tevent_req
);
1866 struct doc_state
*state
= tevent_req_data(req
, struct doc_state
);
1869 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
1870 TALLOC_FREE(subreq
);
1871 if (!NT_STATUS_IS_OK(status
)) {
1872 tevent_req_nterror(req
, status
);
1875 tevent_req_done(req
);
1878 struct tevent_req
*cli_nt_delete_on_close_send(TALLOC_CTX
*mem_ctx
,
1879 struct event_context
*ev
,
1880 struct cli_state
*cli
,
1884 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
1885 struct doc_state
*state
= NULL
;
1887 req
= tevent_req_create(mem_ctx
, &state
, struct doc_state
);
1892 /* Setup setup word. */
1893 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
1895 /* Setup param array. */
1896 SSVAL(state
->param
,0,fnum
);
1897 SSVAL(state
->param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1899 /* Setup data array. */
1900 SCVAL(&state
->data
[0], 0, flag
? 1 : 0);
1902 subreq
= cli_trans_send(state
, /* mem ctx. */
1903 ev
, /* event ctx. */
1904 cli
, /* cli_state. */
1905 SMBtrans2
, /* cmd. */
1906 NULL
, /* pipe name. */
1910 &state
->setup
, /* setup. */
1911 1, /* num setup uint16_t words. */
1912 0, /* max returned setup. */
1913 state
->param
, /* param. */
1915 2, /* max returned param. */
1916 state
->data
, /* data. */
1918 0); /* max returned data. */
1920 if (tevent_req_nomem(subreq
, req
)) {
1921 return tevent_req_post(req
, ev
);
1923 tevent_req_set_callback(subreq
, cli_nt_delete_on_close_done
, req
);
1927 NTSTATUS
cli_nt_delete_on_close_recv(struct tevent_req
*req
)
1931 if (tevent_req_is_nterror(req
, &status
)) {
1934 return NT_STATUS_OK
;
1937 NTSTATUS
cli_nt_delete_on_close(struct cli_state
*cli
, uint16_t fnum
, bool flag
)
1939 TALLOC_CTX
*frame
= talloc_stackframe();
1940 struct event_context
*ev
= NULL
;
1941 struct tevent_req
*req
= NULL
;
1942 NTSTATUS status
= NT_STATUS_OK
;
1944 if (cli_has_async_calls(cli
)) {
1946 * Can't use sync call while an async call is in flight
1948 status
= NT_STATUS_INVALID_PARAMETER
;
1952 ev
= event_context_init(frame
);
1954 status
= NT_STATUS_NO_MEMORY
;
1958 req
= cli_nt_delete_on_close_send(frame
,
1964 status
= NT_STATUS_NO_MEMORY
;
1968 if (!tevent_req_poll(req
, ev
)) {
1969 status
= map_nt_error_from_unix(errno
);
1973 status
= cli_nt_delete_on_close_recv(req
);
1977 if (!NT_STATUS_IS_OK(status
)) {
1978 cli_set_error(cli
, status
);
1983 struct cli_ntcreate_state
{
1988 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1990 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1991 struct event_context
*ev
,
1992 struct cli_state
*cli
,
1994 uint32_t CreatFlags
,
1995 uint32_t DesiredAccess
,
1996 uint32_t FileAttributes
,
1997 uint32_t ShareAccess
,
1998 uint32_t CreateDisposition
,
1999 uint32_t CreateOptions
,
2000 uint8_t SecurityFlags
)
2002 struct tevent_req
*req
, *subreq
;
2003 struct cli_ntcreate_state
*state
;
2006 size_t converted_len
;
2008 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
2015 SCVAL(vwv
+0, 0, 0xFF);
2020 if (cli
->use_oplocks
) {
2021 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
2023 SIVAL(vwv
+3, 1, CreatFlags
);
2024 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
2025 SIVAL(vwv
+7, 1, DesiredAccess
);
2026 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
2027 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
2028 SIVAL(vwv
+13, 1, FileAttributes
);
2029 SIVAL(vwv
+15, 1, ShareAccess
);
2030 SIVAL(vwv
+17, 1, CreateDisposition
);
2031 SIVAL(vwv
+19, 1, CreateOptions
);
2032 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
2033 SCVAL(vwv
+23, 1, SecurityFlags
);
2035 bytes
= talloc_array(state
, uint8_t, 0);
2036 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
),
2037 fname
, strlen(fname
)+1,
2040 /* sigh. this copes with broken netapp filer behaviour */
2041 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "", 1, NULL
);
2043 if (tevent_req_nomem(bytes
, req
)) {
2044 return tevent_req_post(req
, ev
);
2047 SSVAL(vwv
+2, 1, converted_len
);
2049 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
2050 talloc_get_size(bytes
), bytes
);
2051 if (tevent_req_nomem(subreq
, req
)) {
2052 return tevent_req_post(req
, ev
);
2054 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
2058 static void cli_ntcreate_done(struct tevent_req
*subreq
)
2060 struct tevent_req
*req
= tevent_req_callback_data(
2061 subreq
, struct tevent_req
);
2062 struct cli_ntcreate_state
*state
= tevent_req_data(
2063 req
, struct cli_ntcreate_state
);
2070 status
= cli_smb_recv(subreq
, 3, &wct
, &vwv
, &num_bytes
, &bytes
);
2071 if (!NT_STATUS_IS_OK(status
)) {
2072 TALLOC_FREE(subreq
);
2073 tevent_req_nterror(req
, status
);
2076 state
->fnum
= SVAL(vwv
+2, 1);
2077 tevent_req_done(req
);
2080 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2082 struct cli_ntcreate_state
*state
= tevent_req_data(
2083 req
, struct cli_ntcreate_state
);
2086 if (tevent_req_is_nterror(req
, &status
)) {
2089 *pfnum
= state
->fnum
;
2090 return NT_STATUS_OK
;
2093 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
2095 uint32_t CreatFlags
,
2096 uint32_t DesiredAccess
,
2097 uint32_t FileAttributes
,
2098 uint32_t ShareAccess
,
2099 uint32_t CreateDisposition
,
2100 uint32_t CreateOptions
,
2101 uint8_t SecurityFlags
,
2104 TALLOC_CTX
*frame
= talloc_stackframe();
2105 struct event_context
*ev
;
2106 struct tevent_req
*req
;
2107 NTSTATUS status
= NT_STATUS_OK
;
2109 if (cli_has_async_calls(cli
)) {
2111 * Can't use sync call while an async call is in flight
2113 status
= NT_STATUS_INVALID_PARAMETER
;
2117 ev
= event_context_init(frame
);
2119 status
= NT_STATUS_NO_MEMORY
;
2123 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
2124 DesiredAccess
, FileAttributes
, ShareAccess
,
2125 CreateDisposition
, CreateOptions
,
2128 status
= NT_STATUS_NO_MEMORY
;
2132 if (!tevent_req_poll(req
, ev
)) {
2133 status
= map_nt_error_from_unix(errno
);
2137 status
= cli_ntcreate_recv(req
, pfid
);
2140 if (!NT_STATUS_IS_OK(status
)) {
2141 cli_set_error(cli
, status
);
2146 /****************************************************************************
2148 WARNING: if you open with O_WRONLY then getattrE won't work!
2149 ****************************************************************************/
2151 struct cli_open_state
{
2157 static void cli_open_done(struct tevent_req
*subreq
);
2159 struct tevent_req
*cli_open_create(TALLOC_CTX
*mem_ctx
,
2160 struct event_context
*ev
,
2161 struct cli_state
*cli
, const char *fname
,
2162 int flags
, int share_mode
,
2163 struct tevent_req
**psmbreq
)
2165 struct tevent_req
*req
, *subreq
;
2166 struct cli_open_state
*state
;
2168 unsigned accessmode
;
2169 uint8_t additional_flags
;
2172 req
= tevent_req_create(mem_ctx
, &state
, struct cli_open_state
);
2178 if (flags
& O_CREAT
) {
2181 if (!(flags
& O_EXCL
)) {
2182 if (flags
& O_TRUNC
)
2188 accessmode
= (share_mode
<<4);
2190 if ((flags
& O_ACCMODE
) == O_RDWR
) {
2192 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
2197 if ((flags
& O_SYNC
) == O_SYNC
) {
2198 accessmode
|= (1<<14);
2202 if (share_mode
== DENY_FCB
) {
2206 SCVAL(state
->vwv
+ 0, 0, 0xFF);
2207 SCVAL(state
->vwv
+ 0, 1, 0);
2208 SSVAL(state
->vwv
+ 1, 0, 0);
2209 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
2210 SSVAL(state
->vwv
+ 3, 0, accessmode
);
2211 SSVAL(state
->vwv
+ 4, 0, aSYSTEM
| aHIDDEN
);
2212 SSVAL(state
->vwv
+ 5, 0, 0);
2213 SIVAL(state
->vwv
+ 6, 0, 0);
2214 SSVAL(state
->vwv
+ 8, 0, openfn
);
2215 SIVAL(state
->vwv
+ 9, 0, 0);
2216 SIVAL(state
->vwv
+ 11, 0, 0);
2217 SIVAL(state
->vwv
+ 13, 0, 0);
2219 additional_flags
= 0;
2221 if (cli
->use_oplocks
) {
2222 /* if using oplocks then ask for a batch oplock via
2223 core and extended methods */
2225 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
2226 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
2229 bytes
= talloc_array(state
, uint8_t, 0);
2230 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
2231 strlen(fname
)+1, NULL
);
2233 if (tevent_req_nomem(bytes
, req
)) {
2234 return tevent_req_post(req
, ev
);
2237 state
->bytes
.iov_base
= (void *)bytes
;
2238 state
->bytes
.iov_len
= talloc_get_size(bytes
);
2240 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
2241 15, state
->vwv
, 1, &state
->bytes
);
2242 if (subreq
== NULL
) {
2246 tevent_req_set_callback(subreq
, cli_open_done
, req
);
2251 struct tevent_req
*cli_open_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
2252 struct cli_state
*cli
, const char *fname
,
2253 int flags
, int share_mode
)
2255 struct tevent_req
*req
, *subreq
;
2258 req
= cli_open_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
2264 status
= cli_smb_req_send(subreq
);
2265 if (!NT_STATUS_IS_OK(status
)) {
2266 tevent_req_nterror(req
, status
);
2267 return tevent_req_post(req
, ev
);
2272 static void cli_open_done(struct tevent_req
*subreq
)
2274 struct tevent_req
*req
= tevent_req_callback_data(
2275 subreq
, struct tevent_req
);
2276 struct cli_open_state
*state
= tevent_req_data(
2277 req
, struct cli_open_state
);
2282 status
= cli_smb_recv(subreq
, 3, &wct
, &vwv
, NULL
, NULL
);
2283 if (!NT_STATUS_IS_OK(status
)) {
2284 TALLOC_FREE(subreq
);
2285 tevent_req_nterror(req
, status
);
2288 state
->fnum
= SVAL(vwv
+2, 0);
2289 tevent_req_done(req
);
2292 NTSTATUS
cli_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
2294 struct cli_open_state
*state
= tevent_req_data(
2295 req
, struct cli_open_state
);
2298 if (tevent_req_is_nterror(req
, &status
)) {
2301 *pfnum
= state
->fnum
;
2302 return NT_STATUS_OK
;
2305 NTSTATUS
cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
2306 int share_mode
, uint16_t *pfnum
)
2308 TALLOC_CTX
*frame
= talloc_stackframe();
2309 struct event_context
*ev
;
2310 struct tevent_req
*req
;
2311 NTSTATUS status
= NT_STATUS_OK
;
2313 if (cli_has_async_calls(cli
)) {
2315 * Can't use sync call while an async call is in flight
2317 status
= NT_STATUS_INVALID_PARAMETER
;
2321 ev
= event_context_init(frame
);
2323 status
= NT_STATUS_NO_MEMORY
;
2327 req
= cli_open_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
2329 status
= NT_STATUS_NO_MEMORY
;
2333 if (!tevent_req_poll(req
, ev
)) {
2334 status
= map_nt_error_from_unix(errno
);
2338 status
= cli_open_recv(req
, pfnum
);
2341 if (!NT_STATUS_IS_OK(status
)) {
2342 cli_set_error(cli
, status
);
2347 /****************************************************************************
2349 ****************************************************************************/
2351 struct cli_close_state
{
2355 static void cli_close_done(struct tevent_req
*subreq
);
2357 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
2358 struct event_context
*ev
,
2359 struct cli_state
*cli
,
2361 struct tevent_req
**psubreq
)
2363 struct tevent_req
*req
, *subreq
;
2364 struct cli_close_state
*state
;
2366 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
2371 SSVAL(state
->vwv
+0, 0, fnum
);
2372 SIVALS(state
->vwv
+1, 0, -1);
2374 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
2376 if (subreq
== NULL
) {
2380 tevent_req_set_callback(subreq
, cli_close_done
, req
);
2385 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
2386 struct event_context
*ev
,
2387 struct cli_state
*cli
,
2390 struct tevent_req
*req
, *subreq
;
2393 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
2398 status
= cli_smb_req_send(subreq
);
2399 if (!NT_STATUS_IS_OK(status
)) {
2400 tevent_req_nterror(req
, status
);
2401 return tevent_req_post(req
, ev
);
2406 static void cli_close_done(struct tevent_req
*subreq
)
2408 struct tevent_req
*req
= tevent_req_callback_data(
2409 subreq
, struct tevent_req
);
2412 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
2413 TALLOC_FREE(subreq
);
2414 if (!NT_STATUS_IS_OK(status
)) {
2415 tevent_req_nterror(req
, status
);
2418 tevent_req_done(req
);
2421 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
2423 return tevent_req_simple_recv_ntstatus(req
);
2426 NTSTATUS
cli_close(struct cli_state
*cli
, uint16_t fnum
)
2428 TALLOC_CTX
*frame
= talloc_stackframe();
2429 struct event_context
*ev
;
2430 struct tevent_req
*req
;
2431 NTSTATUS status
= NT_STATUS_OK
;
2433 if (cli_has_async_calls(cli
)) {
2435 * Can't use sync call while an async call is in flight
2437 status
= NT_STATUS_INVALID_PARAMETER
;
2441 ev
= event_context_init(frame
);
2443 status
= NT_STATUS_NO_MEMORY
;
2447 req
= cli_close_send(frame
, ev
, cli
, fnum
);
2449 status
= NT_STATUS_NO_MEMORY
;
2453 if (!tevent_req_poll(req
, ev
)) {
2454 status
= map_nt_error_from_unix(errno
);
2458 status
= cli_close_recv(req
);
2461 if (!NT_STATUS_IS_OK(status
)) {
2462 cli_set_error(cli
, status
);
2467 /****************************************************************************
2468 Truncate a file to a specified size
2469 ****************************************************************************/
2471 struct ftrunc_state
{
2477 static void cli_ftruncate_done(struct tevent_req
*subreq
)
2479 struct tevent_req
*req
= tevent_req_callback_data(
2480 subreq
, struct tevent_req
);
2481 struct ftrunc_state
*state
= tevent_req_data(req
, struct ftrunc_state
);
2484 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
2485 TALLOC_FREE(subreq
);
2486 if (!NT_STATUS_IS_OK(status
)) {
2487 tevent_req_nterror(req
, status
);
2490 tevent_req_done(req
);
2493 struct tevent_req
*cli_ftruncate_send(TALLOC_CTX
*mem_ctx
,
2494 struct event_context
*ev
,
2495 struct cli_state
*cli
,
2499 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2500 struct ftrunc_state
*state
= NULL
;
2502 req
= tevent_req_create(mem_ctx
, &state
, struct ftrunc_state
);
2507 /* Setup setup word. */
2508 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
2510 /* Setup param array. */
2511 SSVAL(state
->param
,0,fnum
);
2512 SSVAL(state
->param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
2513 SSVAL(state
->param
,4,0);
2515 /* Setup data array. */
2516 SBVAL(state
->data
, 0, size
);
2518 subreq
= cli_trans_send(state
, /* mem ctx. */
2519 ev
, /* event ctx. */
2520 cli
, /* cli_state. */
2521 SMBtrans2
, /* cmd. */
2522 NULL
, /* pipe name. */
2526 &state
->setup
, /* setup. */
2527 1, /* num setup uint16_t words. */
2528 0, /* max returned setup. */
2529 state
->param
, /* param. */
2531 2, /* max returned param. */
2532 state
->data
, /* data. */
2534 0); /* max returned data. */
2536 if (tevent_req_nomem(subreq
, req
)) {
2537 return tevent_req_post(req
, ev
);
2539 tevent_req_set_callback(subreq
, cli_ftruncate_done
, req
);
2543 NTSTATUS
cli_ftruncate_recv(struct tevent_req
*req
)
2547 if (tevent_req_is_nterror(req
, &status
)) {
2550 return NT_STATUS_OK
;
2553 NTSTATUS
cli_ftruncate(struct cli_state
*cli
, uint16_t fnum
, uint64_t size
)
2555 TALLOC_CTX
*frame
= talloc_stackframe();
2556 struct event_context
*ev
= NULL
;
2557 struct tevent_req
*req
= NULL
;
2558 NTSTATUS status
= NT_STATUS_OK
;
2560 if (cli_has_async_calls(cli
)) {
2562 * Can't use sync call while an async call is in flight
2564 status
= NT_STATUS_INVALID_PARAMETER
;
2568 ev
= event_context_init(frame
);
2570 status
= NT_STATUS_NO_MEMORY
;
2574 req
= cli_ftruncate_send(frame
,
2580 status
= NT_STATUS_NO_MEMORY
;
2584 if (!tevent_req_poll(req
, ev
)) {
2585 status
= map_nt_error_from_unix(errno
);
2589 status
= cli_ftruncate_recv(req
);
2593 if (!NT_STATUS_IS_OK(status
)) {
2594 cli_set_error(cli
, status
);
2599 /****************************************************************************
2600 send a lock with a specified locktype
2601 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2602 ****************************************************************************/
2604 NTSTATUS
cli_locktype(struct cli_state
*cli
, uint16_t fnum
,
2605 uint32_t offset
, uint32_t len
,
2606 int timeout
, unsigned char locktype
)
2609 int saved_timeout
= cli
->timeout
;
2611 memset(cli
->outbuf
,'\0',smb_size
);
2612 memset(cli
->inbuf
,'\0', smb_size
);
2614 cli_set_message(cli
->outbuf
,8,0,True
);
2616 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
2617 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
2618 cli_setup_packet(cli
);
2620 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
2621 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
2622 SCVAL(cli
->outbuf
,smb_vwv3
,locktype
);
2623 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
2624 SSVAL(cli
->outbuf
,smb_vwv6
,0);
2625 SSVAL(cli
->outbuf
,smb_vwv7
,1);
2627 p
= smb_buf(cli
->outbuf
);
2628 SSVAL(p
, 0, cli
->pid
);
2629 SIVAL(p
, 2, offset
);
2634 cli_setup_bcc(cli
, p
);
2639 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 2*1000);
2642 if (!cli_receive_smb(cli
)) {
2643 cli
->timeout
= saved_timeout
;
2644 return NT_STATUS_UNSUCCESSFUL
;
2647 cli
->timeout
= saved_timeout
;
2649 return cli_nt_error(cli
);
2652 /****************************************************************************
2654 note that timeout is in units of 2 milliseconds
2655 ****************************************************************************/
2657 bool cli_lock(struct cli_state
*cli
, uint16_t fnum
,
2658 uint32_t offset
, uint32_t len
, int timeout
, enum brl_type lock_type
)
2661 int saved_timeout
= cli
->timeout
;
2663 memset(cli
->outbuf
,'\0',smb_size
);
2664 memset(cli
->inbuf
,'\0', smb_size
);
2666 cli_set_message(cli
->outbuf
,8,0,True
);
2668 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
2669 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
2670 cli_setup_packet(cli
);
2672 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
2673 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
2674 SCVAL(cli
->outbuf
,smb_vwv3
,(lock_type
== READ_LOCK
? 1 : 0));
2675 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
2676 SSVAL(cli
->outbuf
,smb_vwv6
,0);
2677 SSVAL(cli
->outbuf
,smb_vwv7
,1);
2679 p
= smb_buf(cli
->outbuf
);
2680 SSVAL(p
, 0, cli
->pid
);
2681 SIVAL(p
, 2, offset
);
2686 cli_setup_bcc(cli
, p
);
2691 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
*2 + 5*1000);
2694 if (!cli_receive_smb(cli
)) {
2695 cli
->timeout
= saved_timeout
;
2699 cli
->timeout
= saved_timeout
;
2701 if (cli_is_error(cli
)) {
2708 /****************************************************************************
2710 ****************************************************************************/
2712 struct cli_unlock_state
{
2717 static void cli_unlock_done(struct tevent_req
*subreq
);
2719 struct tevent_req
*cli_unlock_send(TALLOC_CTX
*mem_ctx
,
2720 struct event_context
*ev
,
2721 struct cli_state
*cli
,
2727 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2728 struct cli_unlock_state
*state
= NULL
;
2729 uint8_t additional_flags
= 0;
2731 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock_state
);
2736 SCVAL(state
->vwv
+0, 0, 0xFF);
2737 SSVAL(state
->vwv
+2, 0, fnum
);
2738 SCVAL(state
->vwv
+3, 0, 0);
2739 SIVALS(state
->vwv
+4, 0, 0);
2740 SSVAL(state
->vwv
+6, 0, 1);
2741 SSVAL(state
->vwv
+7, 0, 0);
2743 SSVAL(state
->data
, 0, cli
->pid
);
2744 SIVAL(state
->data
, 2, offset
);
2745 SIVAL(state
->data
, 6, len
);
2747 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2748 8, state
->vwv
, 10, state
->data
);
2749 if (tevent_req_nomem(subreq
, req
)) {
2750 return tevent_req_post(req
, ev
);
2752 tevent_req_set_callback(subreq
, cli_unlock_done
, req
);
2756 static void cli_unlock_done(struct tevent_req
*subreq
)
2758 struct tevent_req
*req
= tevent_req_callback_data(
2759 subreq
, struct tevent_req
);
2762 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
2763 TALLOC_FREE(subreq
);
2764 if (!NT_STATUS_IS_OK(status
)) {
2765 tevent_req_nterror(req
, status
);
2768 tevent_req_done(req
);
2771 NTSTATUS
cli_unlock_recv(struct tevent_req
*req
)
2773 return tevent_req_simple_recv_ntstatus(req
);
2776 NTSTATUS
cli_unlock(struct cli_state
*cli
,
2781 TALLOC_CTX
*frame
= talloc_stackframe();
2782 struct event_context
*ev
;
2783 struct tevent_req
*req
;
2784 NTSTATUS status
= NT_STATUS_OK
;
2786 if (cli_has_async_calls(cli
)) {
2788 * Can't use sync call while an async call is in flight
2790 status
= NT_STATUS_INVALID_PARAMETER
;
2794 ev
= event_context_init(frame
);
2796 status
= NT_STATUS_NO_MEMORY
;
2800 req
= cli_unlock_send(frame
, ev
, cli
,
2803 status
= NT_STATUS_NO_MEMORY
;
2807 if (!tevent_req_poll(req
, ev
)) {
2808 status
= map_nt_error_from_unix(errno
);
2812 status
= cli_unlock_recv(req
);
2816 if (!NT_STATUS_IS_OK(status
)) {
2817 cli_set_error(cli
, status
);
2822 /****************************************************************************
2823 Lock a file with 64 bit offsets.
2824 ****************************************************************************/
2826 bool cli_lock64(struct cli_state
*cli
, uint16_t fnum
,
2827 uint64_t offset
, uint64_t len
, int timeout
, enum brl_type lock_type
)
2830 int saved_timeout
= cli
->timeout
;
2833 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
2834 return cli_lock(cli
, fnum
, offset
, len
, timeout
, lock_type
);
2837 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
2838 ltype
|= LOCKING_ANDX_LARGE_FILES
;
2840 memset(cli
->outbuf
,'\0',smb_size
);
2841 memset(cli
->inbuf
,'\0', smb_size
);
2843 cli_set_message(cli
->outbuf
,8,0,True
);
2845 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
2846 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
2847 cli_setup_packet(cli
);
2849 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
2850 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
2851 SCVAL(cli
->outbuf
,smb_vwv3
,ltype
);
2852 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
2853 SSVAL(cli
->outbuf
,smb_vwv6
,0);
2854 SSVAL(cli
->outbuf
,smb_vwv7
,1);
2856 p
= smb_buf(cli
->outbuf
);
2857 SIVAL(p
, 0, cli
->pid
);
2858 SOFF_T_R(p
, 4, offset
);
2859 SOFF_T_R(p
, 12, len
);
2862 cli_setup_bcc(cli
, p
);
2866 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 5*1000);
2869 if (!cli_receive_smb(cli
)) {
2870 cli
->timeout
= saved_timeout
;
2874 cli
->timeout
= saved_timeout
;
2876 if (cli_is_error(cli
)) {
2883 /****************************************************************************
2884 Unlock a file with 64 bit offsets.
2885 ****************************************************************************/
2887 struct cli_unlock64_state
{
2892 static void cli_unlock64_done(struct tevent_req
*subreq
);
2894 struct tevent_req
*cli_unlock64_send(TALLOC_CTX
*mem_ctx
,
2895 struct event_context
*ev
,
2896 struct cli_state
*cli
,
2902 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2903 struct cli_unlock64_state
*state
= NULL
;
2904 uint8_t additional_flags
= 0;
2906 req
= tevent_req_create(mem_ctx
, &state
, struct cli_unlock64_state
);
2911 SCVAL(state
->vwv
+0, 0, 0xff);
2912 SSVAL(state
->vwv
+2, 0, fnum
);
2913 SCVAL(state
->vwv
+3, 0,LOCKING_ANDX_LARGE_FILES
);
2914 SIVALS(state
->vwv
+4, 0, 0);
2915 SSVAL(state
->vwv
+6, 0, 1);
2916 SSVAL(state
->vwv
+7, 0, 0);
2918 SIVAL(state
->data
, 0, cli
->pid
);
2919 SOFF_T_R(state
->data
, 4, offset
);
2920 SOFF_T_R(state
->data
, 12, len
);
2922 subreq
= cli_smb_send(state
, ev
, cli
, SMBlockingX
, additional_flags
,
2923 8, state
->vwv
, 20, state
->data
);
2924 if (tevent_req_nomem(subreq
, req
)) {
2925 return tevent_req_post(req
, ev
);
2927 tevent_req_set_callback(subreq
, cli_unlock64_done
, req
);
2931 static void cli_unlock64_done(struct tevent_req
*subreq
)
2933 struct tevent_req
*req
= tevent_req_callback_data(
2934 subreq
, struct tevent_req
);
2937 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
2938 TALLOC_FREE(subreq
);
2939 if (!NT_STATUS_IS_OK(status
)) {
2940 tevent_req_nterror(req
, status
);
2943 tevent_req_done(req
);
2946 NTSTATUS
cli_unlock64_recv(struct tevent_req
*req
)
2948 return tevent_req_simple_recv_ntstatus(req
);
2951 NTSTATUS
cli_unlock64(struct cli_state
*cli
,
2956 TALLOC_CTX
*frame
= talloc_stackframe();
2957 struct event_context
*ev
;
2958 struct tevent_req
*req
;
2959 NTSTATUS status
= NT_STATUS_OK
;
2961 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
2962 return cli_unlock(cli
, fnum
, offset
, len
);
2965 if (cli_has_async_calls(cli
)) {
2967 * Can't use sync call while an async call is in flight
2969 status
= NT_STATUS_INVALID_PARAMETER
;
2973 ev
= event_context_init(frame
);
2975 status
= NT_STATUS_NO_MEMORY
;
2979 req
= cli_unlock64_send(frame
, ev
, cli
,
2982 status
= NT_STATUS_NO_MEMORY
;
2986 if (!tevent_req_poll(req
, ev
)) {
2987 status
= map_nt_error_from_unix(errno
);
2991 status
= cli_unlock64_recv(req
);
2995 if (!NT_STATUS_IS_OK(status
)) {
2996 cli_set_error(cli
, status
);
3001 /****************************************************************************
3002 Get/unlock a POSIX lock on a file - internal function.
3003 ****************************************************************************/
3005 struct posix_lock_state
{
3008 uint8_t data
[POSIX_LOCK_DATA_SIZE
];
3011 static void cli_posix_unlock_internal_done(struct tevent_req
*subreq
)
3013 struct tevent_req
*req
= tevent_req_callback_data(
3014 subreq
, struct tevent_req
);
3015 struct posix_lock_state
*state
= tevent_req_data(req
, struct posix_lock_state
);
3018 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
3019 TALLOC_FREE(subreq
);
3020 if (!NT_STATUS_IS_OK(status
)) {
3021 tevent_req_nterror(req
, status
);
3024 tevent_req_done(req
);
3027 static struct tevent_req
*cli_posix_lock_internal_send(TALLOC_CTX
*mem_ctx
,
3028 struct event_context
*ev
,
3029 struct cli_state
*cli
,
3034 enum brl_type lock_type
)
3036 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3037 struct posix_lock_state
*state
= NULL
;
3039 req
= tevent_req_create(mem_ctx
, &state
, struct posix_lock_state
);
3044 /* Setup setup word. */
3045 SSVAL(&state
->setup
, 0, TRANSACT2_SETFILEINFO
);
3047 /* Setup param array. */
3048 SSVAL(&state
->param
, 0, fnum
);
3049 SSVAL(&state
->param
, 2, SMB_SET_POSIX_LOCK
);
3051 /* Setup data array. */
3052 switch (lock_type
) {
3054 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3055 POSIX_LOCK_TYPE_READ
);
3058 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3059 POSIX_LOCK_TYPE_WRITE
);
3062 SSVAL(&state
->data
, POSIX_LOCK_TYPE_OFFSET
,
3063 POSIX_LOCK_TYPE_UNLOCK
);
3070 SSVAL(&state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3071 POSIX_LOCK_FLAG_WAIT
);
3073 SSVAL(state
->data
, POSIX_LOCK_FLAGS_OFFSET
,
3074 POSIX_LOCK_FLAG_NOWAIT
);
3077 SIVAL(&state
->data
, POSIX_LOCK_PID_OFFSET
, cli
->pid
);
3078 SOFF_T(&state
->data
, POSIX_LOCK_START_OFFSET
, offset
);
3079 SOFF_T(&state
->data
, POSIX_LOCK_LEN_OFFSET
, len
);
3081 subreq
= cli_trans_send(state
, /* mem ctx. */
3082 ev
, /* event ctx. */
3083 cli
, /* cli_state. */
3084 SMBtrans2
, /* cmd. */
3085 NULL
, /* pipe name. */
3089 &state
->setup
, /* setup. */
3090 1, /* num setup uint16_t words. */
3091 0, /* max returned setup. */
3092 state
->param
, /* param. */
3094 2, /* max returned param. */
3095 state
->data
, /* data. */
3096 POSIX_LOCK_DATA_SIZE
, /* num data. */
3097 0); /* max returned data. */
3099 if (tevent_req_nomem(subreq
, req
)) {
3100 return tevent_req_post(req
, ev
);
3102 tevent_req_set_callback(subreq
, cli_posix_unlock_internal_done
, req
);
3106 /****************************************************************************
3108 ****************************************************************************/
3110 struct tevent_req
*cli_posix_lock_send(TALLOC_CTX
*mem_ctx
,
3111 struct event_context
*ev
,
3112 struct cli_state
*cli
,
3117 enum brl_type lock_type
)
3119 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3120 wait_lock
, lock_type
);
3123 NTSTATUS
cli_posix_lock_recv(struct tevent_req
*req
)
3127 if (tevent_req_is_nterror(req
, &status
)) {
3130 return NT_STATUS_OK
;
3133 NTSTATUS
cli_posix_lock(struct cli_state
*cli
, uint16_t fnum
,
3134 uint64_t offset
, uint64_t len
,
3135 bool wait_lock
, enum brl_type lock_type
)
3137 TALLOC_CTX
*frame
= talloc_stackframe();
3138 struct event_context
*ev
= NULL
;
3139 struct tevent_req
*req
= NULL
;
3140 NTSTATUS status
= NT_STATUS_OK
;
3142 if (cli_has_async_calls(cli
)) {
3144 * Can't use sync call while an async call is in flight
3146 status
= NT_STATUS_INVALID_PARAMETER
;
3150 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
3151 status
= NT_STATUS_INVALID_PARAMETER
;
3155 ev
= event_context_init(frame
);
3157 status
= NT_STATUS_NO_MEMORY
;
3161 req
= cli_posix_lock_send(frame
,
3170 status
= NT_STATUS_NO_MEMORY
;
3174 if (!tevent_req_poll(req
, ev
)) {
3175 status
= map_nt_error_from_unix(errno
);
3179 status
= cli_posix_lock_recv(req
);
3183 if (!NT_STATUS_IS_OK(status
)) {
3184 cli_set_error(cli
, status
);
3189 /****************************************************************************
3190 POSIX Unlock a file.
3191 ****************************************************************************/
3193 struct tevent_req
*cli_posix_unlock_send(TALLOC_CTX
*mem_ctx
,
3194 struct event_context
*ev
,
3195 struct cli_state
*cli
,
3200 return cli_posix_lock_internal_send(mem_ctx
, ev
, cli
, fnum
, offset
, len
,
3201 false, UNLOCK_LOCK
);
3204 NTSTATUS
cli_posix_unlock_recv(struct tevent_req
*req
)
3208 if (tevent_req_is_nterror(req
, &status
)) {
3211 return NT_STATUS_OK
;
3214 NTSTATUS
cli_posix_unlock(struct cli_state
*cli
, uint16_t fnum
, uint64_t offset
, uint64_t len
)
3216 TALLOC_CTX
*frame
= talloc_stackframe();
3217 struct event_context
*ev
= NULL
;
3218 struct tevent_req
*req
= NULL
;
3219 NTSTATUS status
= NT_STATUS_OK
;
3221 if (cli_has_async_calls(cli
)) {
3223 * Can't use sync call while an async call is in flight
3225 status
= NT_STATUS_INVALID_PARAMETER
;
3229 ev
= event_context_init(frame
);
3231 status
= NT_STATUS_NO_MEMORY
;
3235 req
= cli_posix_unlock_send(frame
,
3242 status
= NT_STATUS_NO_MEMORY
;
3246 if (!tevent_req_poll(req
, ev
)) {
3247 status
= map_nt_error_from_unix(errno
);
3251 status
= cli_posix_unlock_recv(req
);
3255 if (!NT_STATUS_IS_OK(status
)) {
3256 cli_set_error(cli
, status
);
3261 /****************************************************************************
3262 Do a SMBgetattrE call.
3263 ****************************************************************************/
3265 static void cli_getattrE_done(struct tevent_req
*subreq
);
3267 struct cli_getattrE_state
{
3277 struct tevent_req
*cli_getattrE_send(TALLOC_CTX
*mem_ctx
,
3278 struct event_context
*ev
,
3279 struct cli_state
*cli
,
3282 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3283 struct cli_getattrE_state
*state
= NULL
;
3284 uint8_t additional_flags
= 0;
3286 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getattrE_state
);
3291 state
->zone_offset
= cli
->serverzone
;
3292 SSVAL(state
->vwv
+0,0,fnum
);
3294 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetattrE
, additional_flags
,
3295 1, state
->vwv
, 0, NULL
);
3296 if (tevent_req_nomem(subreq
, req
)) {
3297 return tevent_req_post(req
, ev
);
3299 tevent_req_set_callback(subreq
, cli_getattrE_done
, req
);
3303 static void cli_getattrE_done(struct tevent_req
*subreq
)
3305 struct tevent_req
*req
= tevent_req_callback_data(
3306 subreq
, struct tevent_req
);
3307 struct cli_getattrE_state
*state
= tevent_req_data(
3308 req
, struct cli_getattrE_state
);
3310 uint16_t *vwv
= NULL
;
3313 status
= cli_smb_recv(subreq
, 11, &wct
, &vwv
, NULL
, NULL
);
3314 if (!NT_STATUS_IS_OK(status
)) {
3315 tevent_req_nterror(req
, status
);
3319 state
->size
= (SMB_OFF_T
)IVAL(vwv
+6,0);
3320 state
->attr
= SVAL(vwv
+10,0);
3321 state
->change_time
= make_unix_date2(vwv
+0, state
->zone_offset
);
3322 state
->access_time
= make_unix_date2(vwv
+2, state
->zone_offset
);
3323 state
->write_time
= make_unix_date2(vwv
+4, state
->zone_offset
);
3325 TALLOC_FREE(subreq
);
3326 tevent_req_done(req
);
3329 NTSTATUS
cli_getattrE_recv(struct tevent_req
*req
,
3332 time_t *change_time
,
3333 time_t *access_time
,
3336 struct cli_getattrE_state
*state
= tevent_req_data(
3337 req
, struct cli_getattrE_state
);
3340 if (tevent_req_is_nterror(req
, &status
)) {
3344 *attr
= state
->attr
;
3347 *size
= state
->size
;
3350 *change_time
= state
->change_time
;
3353 *access_time
= state
->access_time
;
3356 *write_time
= state
->write_time
;
3358 return NT_STATUS_OK
;
3361 NTSTATUS
cli_getattrE(struct cli_state
*cli
,
3365 time_t *change_time
,
3366 time_t *access_time
,
3369 TALLOC_CTX
*frame
= talloc_stackframe();
3370 struct event_context
*ev
= NULL
;
3371 struct tevent_req
*req
= NULL
;
3372 NTSTATUS status
= NT_STATUS_OK
;
3374 if (cli_has_async_calls(cli
)) {
3376 * Can't use sync call while an async call is in flight
3378 status
= NT_STATUS_INVALID_PARAMETER
;
3382 ev
= event_context_init(frame
);
3384 status
= NT_STATUS_NO_MEMORY
;
3388 req
= cli_getattrE_send(frame
, ev
, cli
, fnum
);
3390 status
= NT_STATUS_NO_MEMORY
;
3394 if (!tevent_req_poll(req
, ev
)) {
3395 status
= map_nt_error_from_unix(errno
);
3399 status
= cli_getattrE_recv(req
,
3408 if (!NT_STATUS_IS_OK(status
)) {
3409 cli_set_error(cli
, status
);
3414 /****************************************************************************
3416 ****************************************************************************/
3418 static void cli_getatr_done(struct tevent_req
*subreq
);
3420 struct cli_getatr_state
{
3427 struct tevent_req
*cli_getatr_send(TALLOC_CTX
*mem_ctx
,
3428 struct event_context
*ev
,
3429 struct cli_state
*cli
,
3432 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3433 struct cli_getatr_state
*state
= NULL
;
3434 uint8_t additional_flags
= 0;
3435 uint8_t *bytes
= NULL
;
3437 req
= tevent_req_create(mem_ctx
, &state
, struct cli_getatr_state
);
3442 state
->zone_offset
= cli
->serverzone
;
3444 bytes
= talloc_array(state
, uint8_t, 1);
3445 if (tevent_req_nomem(bytes
, req
)) {
3446 return tevent_req_post(req
, ev
);
3449 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
3450 strlen(fname
)+1, NULL
);
3452 if (tevent_req_nomem(bytes
, req
)) {
3453 return tevent_req_post(req
, ev
);
3456 subreq
= cli_smb_send(state
, ev
, cli
, SMBgetatr
, additional_flags
,
3457 0, NULL
, talloc_get_size(bytes
), bytes
);
3458 if (tevent_req_nomem(subreq
, req
)) {
3459 return tevent_req_post(req
, ev
);
3461 tevent_req_set_callback(subreq
, cli_getatr_done
, req
);
3465 static void cli_getatr_done(struct tevent_req
*subreq
)
3467 struct tevent_req
*req
= tevent_req_callback_data(
3468 subreq
, struct tevent_req
);
3469 struct cli_getatr_state
*state
= tevent_req_data(
3470 req
, struct cli_getatr_state
);
3472 uint16_t *vwv
= NULL
;
3475 status
= cli_smb_recv(subreq
, 4, &wct
, &vwv
, NULL
, NULL
);
3476 if (!NT_STATUS_IS_OK(status
)) {
3477 tevent_req_nterror(req
, status
);
3481 state
->attr
= SVAL(vwv
+0,0);
3482 state
->size
= (SMB_OFF_T
)IVAL(vwv
+3,0);
3483 state
->write_time
= make_unix_date3(vwv
+1, state
->zone_offset
);
3485 TALLOC_FREE(subreq
);
3486 tevent_req_done(req
);
3489 NTSTATUS
cli_getatr_recv(struct tevent_req
*req
,
3494 struct cli_getatr_state
*state
= tevent_req_data(
3495 req
, struct cli_getatr_state
);
3498 if (tevent_req_is_nterror(req
, &status
)) {
3502 *attr
= state
->attr
;
3505 *size
= state
->size
;
3508 *write_time
= state
->write_time
;
3510 return NT_STATUS_OK
;
3513 NTSTATUS
cli_getatr(struct cli_state
*cli
,
3519 TALLOC_CTX
*frame
= talloc_stackframe();
3520 struct event_context
*ev
= NULL
;
3521 struct tevent_req
*req
= NULL
;
3522 NTSTATUS status
= NT_STATUS_OK
;
3524 if (cli_has_async_calls(cli
)) {
3526 * Can't use sync call while an async call is in flight
3528 status
= NT_STATUS_INVALID_PARAMETER
;
3532 ev
= event_context_init(frame
);
3534 status
= NT_STATUS_NO_MEMORY
;
3538 req
= cli_getatr_send(frame
, ev
, cli
, fname
);
3540 status
= NT_STATUS_NO_MEMORY
;
3544 if (!tevent_req_poll(req
, ev
)) {
3545 status
= map_nt_error_from_unix(errno
);
3549 status
= cli_getatr_recv(req
,
3556 if (!NT_STATUS_IS_OK(status
)) {
3557 cli_set_error(cli
, status
);
3562 /****************************************************************************
3563 Do a SMBsetattrE call.
3564 ****************************************************************************/
3566 static void cli_setattrE_done(struct tevent_req
*subreq
);
3568 struct cli_setattrE_state
{
3572 struct tevent_req
*cli_setattrE_send(TALLOC_CTX
*mem_ctx
,
3573 struct event_context
*ev
,
3574 struct cli_state
*cli
,
3580 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3581 struct cli_setattrE_state
*state
= NULL
;
3582 uint8_t additional_flags
= 0;
3584 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setattrE_state
);
3589 SSVAL(state
->vwv
+0, 0, fnum
);
3590 cli_put_dos_date2(cli
, (char *)&state
->vwv
[1], 0, change_time
);
3591 cli_put_dos_date2(cli
, (char *)&state
->vwv
[3], 0, access_time
);
3592 cli_put_dos_date2(cli
, (char *)&state
->vwv
[5], 0, write_time
);
3594 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetattrE
, additional_flags
,
3595 7, state
->vwv
, 0, NULL
);
3596 if (tevent_req_nomem(subreq
, req
)) {
3597 return tevent_req_post(req
, ev
);
3599 tevent_req_set_callback(subreq
, cli_setattrE_done
, req
);
3603 static void cli_setattrE_done(struct tevent_req
*subreq
)
3605 struct tevent_req
*req
= tevent_req_callback_data(
3606 subreq
, struct tevent_req
);
3609 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
3610 TALLOC_FREE(subreq
);
3611 if (!NT_STATUS_IS_OK(status
)) {
3612 tevent_req_nterror(req
, status
);
3615 tevent_req_done(req
);
3618 NTSTATUS
cli_setattrE_recv(struct tevent_req
*req
)
3620 return tevent_req_simple_recv_ntstatus(req
);
3623 NTSTATUS
cli_setattrE(struct cli_state
*cli
,
3629 TALLOC_CTX
*frame
= talloc_stackframe();
3630 struct event_context
*ev
= NULL
;
3631 struct tevent_req
*req
= NULL
;
3632 NTSTATUS status
= NT_STATUS_OK
;
3634 if (cli_has_async_calls(cli
)) {
3636 * Can't use sync call while an async call is in flight
3638 status
= NT_STATUS_INVALID_PARAMETER
;
3642 ev
= event_context_init(frame
);
3644 status
= NT_STATUS_NO_MEMORY
;
3648 req
= cli_setattrE_send(frame
, ev
,
3656 status
= NT_STATUS_NO_MEMORY
;
3660 if (!tevent_req_poll(req
, ev
)) {
3661 status
= map_nt_error_from_unix(errno
);
3665 status
= cli_setattrE_recv(req
);
3669 if (!NT_STATUS_IS_OK(status
)) {
3670 cli_set_error(cli
, status
);
3675 /****************************************************************************
3676 Do a SMBsetatr call.
3677 ****************************************************************************/
3679 static void cli_setatr_done(struct tevent_req
*subreq
);
3681 struct cli_setatr_state
{
3685 struct tevent_req
*cli_setatr_send(TALLOC_CTX
*mem_ctx
,
3686 struct event_context
*ev
,
3687 struct cli_state
*cli
,
3692 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3693 struct cli_setatr_state
*state
= NULL
;
3694 uint8_t additional_flags
= 0;
3695 uint8_t *bytes
= NULL
;
3697 req
= tevent_req_create(mem_ctx
, &state
, struct cli_setatr_state
);
3702 SSVAL(state
->vwv
+0, 0, attr
);
3703 cli_put_dos_date3(cli
, (char *)&state
->vwv
[1], 0, mtime
);
3705 bytes
= talloc_array(state
, uint8_t, 1);
3706 if (tevent_req_nomem(bytes
, req
)) {
3707 return tevent_req_post(req
, ev
);
3710 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
3711 strlen(fname
)+1, NULL
);
3712 if (tevent_req_nomem(bytes
, req
)) {
3713 return tevent_req_post(req
, ev
);
3715 bytes
= TALLOC_REALLOC_ARRAY(state
, bytes
, uint8_t,
3716 talloc_get_size(bytes
)+1);
3717 if (tevent_req_nomem(bytes
, req
)) {
3718 return tevent_req_post(req
, ev
);
3721 bytes
[talloc_get_size(bytes
)-1] = 4;
3722 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "",
3724 if (tevent_req_nomem(bytes
, req
)) {
3725 return tevent_req_post(req
, ev
);
3728 subreq
= cli_smb_send(state
, ev
, cli
, SMBsetatr
, additional_flags
,
3729 8, state
->vwv
, talloc_get_size(bytes
), bytes
);
3730 if (tevent_req_nomem(subreq
, req
)) {
3731 return tevent_req_post(req
, ev
);
3733 tevent_req_set_callback(subreq
, cli_setatr_done
, req
);
3737 static void cli_setatr_done(struct tevent_req
*subreq
)
3739 struct tevent_req
*req
= tevent_req_callback_data(
3740 subreq
, struct tevent_req
);
3743 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
3744 TALLOC_FREE(subreq
);
3745 if (!NT_STATUS_IS_OK(status
)) {
3746 tevent_req_nterror(req
, status
);
3749 tevent_req_done(req
);
3752 NTSTATUS
cli_setatr_recv(struct tevent_req
*req
)
3754 return tevent_req_simple_recv_ntstatus(req
);
3757 NTSTATUS
cli_setatr(struct cli_state
*cli
,
3762 TALLOC_CTX
*frame
= talloc_stackframe();
3763 struct event_context
*ev
= NULL
;
3764 struct tevent_req
*req
= NULL
;
3765 NTSTATUS status
= NT_STATUS_OK
;
3767 if (cli_has_async_calls(cli
)) {
3769 * Can't use sync call while an async call is in flight
3771 status
= NT_STATUS_INVALID_PARAMETER
;
3775 ev
= event_context_init(frame
);
3777 status
= NT_STATUS_NO_MEMORY
;
3781 req
= cli_setatr_send(frame
, ev
, cli
, fname
, attr
, mtime
);
3783 status
= NT_STATUS_NO_MEMORY
;
3787 if (!tevent_req_poll(req
, ev
)) {
3788 status
= map_nt_error_from_unix(errno
);
3792 status
= cli_setatr_recv(req
);
3796 if (!NT_STATUS_IS_OK(status
)) {
3797 cli_set_error(cli
, status
);
3802 /****************************************************************************
3803 Check for existance of a dir.
3804 ****************************************************************************/
3806 static void cli_chkpath_done(struct tevent_req
*subreq
);
3808 struct cli_chkpath_state
{
3812 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
3813 struct event_context
*ev
,
3814 struct cli_state
*cli
,
3817 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3818 struct cli_chkpath_state
*state
= NULL
;
3819 uint8_t additional_flags
= 0;
3820 uint8_t *bytes
= NULL
;
3822 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
3827 bytes
= talloc_array(state
, uint8_t, 1);
3828 if (tevent_req_nomem(bytes
, req
)) {
3829 return tevent_req_post(req
, ev
);
3832 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
3833 strlen(fname
)+1, NULL
);
3835 if (tevent_req_nomem(bytes
, req
)) {
3836 return tevent_req_post(req
, ev
);
3839 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
3840 0, NULL
, talloc_get_size(bytes
), bytes
);
3841 if (tevent_req_nomem(subreq
, req
)) {
3842 return tevent_req_post(req
, ev
);
3844 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
3848 static void cli_chkpath_done(struct tevent_req
*subreq
)
3850 struct tevent_req
*req
= tevent_req_callback_data(
3851 subreq
, struct tevent_req
);
3854 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
3855 TALLOC_FREE(subreq
);
3856 if (!NT_STATUS_IS_OK(status
)) {
3857 tevent_req_nterror(req
, status
);
3860 tevent_req_done(req
);
3863 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
3865 return tevent_req_simple_recv_ntstatus(req
);
3868 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
3870 TALLOC_CTX
*frame
= talloc_stackframe();
3871 struct event_context
*ev
= NULL
;
3872 struct tevent_req
*req
= NULL
;
3874 NTSTATUS status
= NT_STATUS_OK
;
3876 if (cli_has_async_calls(cli
)) {
3878 * Can't use sync call while an async call is in flight
3880 status
= NT_STATUS_INVALID_PARAMETER
;
3884 path2
= talloc_strdup(frame
, path
);
3886 status
= NT_STATUS_NO_MEMORY
;
3889 trim_char(path2
,'\0','\\');
3891 path2
= talloc_strdup(frame
, "\\");
3893 status
= NT_STATUS_NO_MEMORY
;
3898 ev
= event_context_init(frame
);
3900 status
= NT_STATUS_NO_MEMORY
;
3904 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
3906 status
= NT_STATUS_NO_MEMORY
;
3910 if (!tevent_req_poll(req
, ev
)) {
3911 status
= map_nt_error_from_unix(errno
);
3915 status
= cli_chkpath_recv(req
);
3919 if (!NT_STATUS_IS_OK(status
)) {
3920 cli_set_error(cli
, status
);
3925 /****************************************************************************
3927 ****************************************************************************/
3929 static void cli_dskattr_done(struct tevent_req
*subreq
);
3931 struct cli_dskattr_state
{
3937 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
3938 struct event_context
*ev
,
3939 struct cli_state
*cli
)
3941 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3942 struct cli_dskattr_state
*state
= NULL
;
3943 uint8_t additional_flags
= 0;
3945 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
3950 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
3952 if (tevent_req_nomem(subreq
, req
)) {
3953 return tevent_req_post(req
, ev
);
3955 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
3959 static void cli_dskattr_done(struct tevent_req
*subreq
)
3961 struct tevent_req
*req
= tevent_req_callback_data(
3962 subreq
, struct tevent_req
);
3963 struct cli_dskattr_state
*state
= tevent_req_data(
3964 req
, struct cli_dskattr_state
);
3966 uint16_t *vwv
= NULL
;
3969 status
= cli_smb_recv(subreq
, 4, &wct
, &vwv
, NULL
, NULL
);
3970 if (!NT_STATUS_IS_OK(status
)) {
3971 tevent_req_nterror(req
, status
);
3974 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
3975 state
->total
= SVAL(vwv
+0, 0);
3976 state
->avail
= SVAL(vwv
+3, 0);
3977 TALLOC_FREE(subreq
);
3978 tevent_req_done(req
);
3981 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
3983 struct cli_dskattr_state
*state
= tevent_req_data(
3984 req
, struct cli_dskattr_state
);
3987 if (tevent_req_is_nterror(req
, &status
)) {
3990 *bsize
= state
->bsize
;
3991 *total
= state
->total
;
3992 *avail
= state
->avail
;
3993 return NT_STATUS_OK
;
3996 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
3998 TALLOC_CTX
*frame
= talloc_stackframe();
3999 struct event_context
*ev
= NULL
;
4000 struct tevent_req
*req
= NULL
;
4001 NTSTATUS status
= NT_STATUS_OK
;
4003 if (cli_has_async_calls(cli
)) {
4005 * Can't use sync call while an async call is in flight
4007 status
= NT_STATUS_INVALID_PARAMETER
;
4011 ev
= event_context_init(frame
);
4013 status
= NT_STATUS_NO_MEMORY
;
4017 req
= cli_dskattr_send(frame
, ev
, cli
);
4019 status
= NT_STATUS_NO_MEMORY
;
4023 if (!tevent_req_poll(req
, ev
)) {
4024 status
= map_nt_error_from_unix(errno
);
4028 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
4032 if (!NT_STATUS_IS_OK(status
)) {
4033 cli_set_error(cli
, status
);
4038 /****************************************************************************
4039 Create and open a temporary file.
4040 ****************************************************************************/
4042 static void cli_ctemp_done(struct tevent_req
*subreq
);
4044 struct ctemp_state
{
4050 struct tevent_req
*cli_ctemp_send(TALLOC_CTX
*mem_ctx
,
4051 struct event_context
*ev
,
4052 struct cli_state
*cli
,
4055 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4056 struct ctemp_state
*state
= NULL
;
4057 uint8_t additional_flags
= 0;
4058 uint8_t *bytes
= NULL
;
4060 req
= tevent_req_create(mem_ctx
, &state
, struct ctemp_state
);
4065 SSVAL(state
->vwv
,0,0);
4066 SIVALS(state
->vwv
+1,0,-1);
4068 bytes
= talloc_array(state
, uint8_t, 1);
4069 if (tevent_req_nomem(bytes
, req
)) {
4070 return tevent_req_post(req
, ev
);
4073 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), path
,
4074 strlen(path
)+1, NULL
);
4075 if (tevent_req_nomem(bytes
, req
)) {
4076 return tevent_req_post(req
, ev
);
4079 subreq
= cli_smb_send(state
, ev
, cli
, SMBctemp
, additional_flags
,
4080 3, state
->vwv
, talloc_get_size(bytes
), bytes
);
4081 if (tevent_req_nomem(subreq
, req
)) {
4082 return tevent_req_post(req
, ev
);
4084 tevent_req_set_callback(subreq
, cli_ctemp_done
, req
);
4088 static void cli_ctemp_done(struct tevent_req
*subreq
)
4090 struct tevent_req
*req
= tevent_req_callback_data(
4091 subreq
, struct tevent_req
);
4092 struct ctemp_state
*state
= tevent_req_data(
4093 req
, struct ctemp_state
);
4097 uint32_t num_bytes
= 0;
4098 uint8_t *bytes
= NULL
;
4100 status
= cli_smb_recv(subreq
, 1, &wcnt
, &vwv
, &num_bytes
, &bytes
);
4101 if (!NT_STATUS_IS_OK(status
)) {
4102 TALLOC_FREE(subreq
);
4103 tevent_req_nterror(req
, status
);
4107 state
->fnum
= SVAL(vwv
+0, 0);
4109 TALLOC_FREE(subreq
);
4111 /* From W2K3, the result is just the ASCII name */
4112 if (num_bytes
< 2) {
4113 tevent_req_nterror(req
, NT_STATUS_DATA_ERROR
);
4117 if (pull_string_talloc(state
,
4124 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
4127 tevent_req_done(req
);
4130 NTSTATUS
cli_ctemp_recv(struct tevent_req
*req
,
4135 struct ctemp_state
*state
= tevent_req_data(req
,
4136 struct ctemp_state
);
4139 if (tevent_req_is_nterror(req
, &status
)) {
4142 *pfnum
= state
->fnum
;
4143 *outfile
= talloc_strdup(ctx
, state
->ret_path
);
4145 return NT_STATUS_NO_MEMORY
;
4147 return NT_STATUS_OK
;
4150 NTSTATUS
cli_ctemp(struct cli_state
*cli
,
4156 TALLOC_CTX
*frame
= talloc_stackframe();
4157 struct event_context
*ev
;
4158 struct tevent_req
*req
;
4159 NTSTATUS status
= NT_STATUS_OK
;
4161 if (cli_has_async_calls(cli
)) {
4163 * Can't use sync call while an async call is in flight
4165 status
= NT_STATUS_INVALID_PARAMETER
;
4169 ev
= event_context_init(frame
);
4171 status
= NT_STATUS_NO_MEMORY
;
4175 req
= cli_ctemp_send(frame
, ev
, cli
, path
);
4177 status
= NT_STATUS_NO_MEMORY
;
4181 if (!tevent_req_poll(req
, ev
)) {
4182 status
= map_nt_error_from_unix(errno
);
4186 status
= cli_ctemp_recv(req
, ctx
, pfnum
, out_path
);
4190 if (!NT_STATUS_IS_OK(status
)) {
4191 cli_set_error(cli
, status
);
4197 send a raw ioctl - used by the torture code
4199 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, uint16_t fnum
, uint32_t code
, DATA_BLOB
*blob
)
4201 memset(cli
->outbuf
,'\0',smb_size
);
4202 memset(cli
->inbuf
,'\0',smb_size
);
4204 cli_set_message(cli
->outbuf
, 3, 0, True
);
4205 SCVAL(cli
->outbuf
,smb_com
,SMBioctl
);
4206 cli_setup_packet(cli
);
4208 SSVAL(cli
->outbuf
, smb_vwv0
, fnum
);
4209 SSVAL(cli
->outbuf
, smb_vwv1
, code
>>16);
4210 SSVAL(cli
->outbuf
, smb_vwv2
, (code
&0xFFFF));
4213 if (!cli_receive_smb(cli
)) {
4214 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
4217 if (cli_is_error(cli
)) {
4218 return cli_nt_error(cli
);
4221 *blob
= data_blob_null
;
4223 return NT_STATUS_OK
;
4226 /*********************************************************
4227 Set an extended attribute utility fn.
4228 *********************************************************/
4230 static bool cli_set_ea(struct cli_state
*cli
, uint16_t setup
, char *param
, unsigned int param_len
,
4231 const char *ea_name
, const char *ea_val
, size_t ea_len
)
4233 unsigned int data_len
= 0;
4235 char *rparam
=NULL
, *rdata
=NULL
;
4237 size_t ea_namelen
= strlen(ea_name
);
4239 if (ea_namelen
== 0 && ea_len
== 0) {
4241 data
= (char *)SMB_MALLOC(data_len
);
4246 SIVAL(p
,0,data_len
);
4248 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
4249 data
= (char *)SMB_MALLOC(data_len
);
4254 SIVAL(p
,0,data_len
);
4256 SCVAL(p
, 0, 0); /* EA flags. */
4257 SCVAL(p
, 1, ea_namelen
);
4258 SSVAL(p
, 2, ea_len
);
4259 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
4260 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
4263 if (!cli_send_trans(cli
, SMBtrans2
,
4265 -1, 0, /* fid, flags */
4266 &setup
, 1, 0, /* setup, length, max */
4267 param
, param_len
, 2, /* param, length, max */
4268 data
, data_len
, cli
->max_xmit
/* data, length, max */
4274 if (!cli_receive_trans(cli
, SMBtrans2
,
4275 &rparam
, ¶m_len
,
4276 &rdata
, &data_len
)) {
4288 /*********************************************************
4289 Set an extended attribute on a pathname.
4290 *********************************************************/
4292 bool cli_set_ea_path(struct cli_state
*cli
, const char *path
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
4294 uint16_t setup
= TRANSACT2_SETPATHINFO
;
4295 unsigned int param_len
= 0;
4297 size_t srclen
= 2*(strlen(path
)+1);
4301 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
4305 memset(param
, '\0', 6);
4306 SSVAL(param
,0,SMB_INFO_SET_EA
);
4309 p
+= clistr_push(cli
, p
, path
, srclen
, STR_TERMINATE
);
4310 param_len
= PTR_DIFF(p
, param
);
4312 ret
= cli_set_ea(cli
, setup
, param
, param_len
, ea_name
, ea_val
, ea_len
);
4317 /*********************************************************
4318 Set an extended attribute on an fnum.
4319 *********************************************************/
4321 bool cli_set_ea_fnum(struct cli_state
*cli
, uint16_t fnum
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
4324 uint16_t setup
= TRANSACT2_SETFILEINFO
;
4326 memset(param
, 0, 6);
4327 SSVAL(param
,0,fnum
);
4328 SSVAL(param
,2,SMB_INFO_SET_EA
);
4330 return cli_set_ea(cli
, setup
, param
, 6, ea_name
, ea_val
, ea_len
);
4333 /*********************************************************
4334 Get an extended attribute list utility fn.
4335 *********************************************************/
4337 static bool cli_get_ea_list(struct cli_state
*cli
,
4338 uint16_t setup
, char *param
, unsigned int param_len
,
4341 struct ea_struct
**pea_list
)
4343 unsigned int data_len
= 0;
4344 unsigned int rparam_len
, rdata_len
;
4345 char *rparam
=NULL
, *rdata
=NULL
;
4350 struct ea_struct
*ea_list
;
4357 if (!cli_send_trans(cli
, SMBtrans2
,
4359 -1, 0, /* fid, flags */
4360 &setup
, 1, 0, /* setup, length, max */
4361 param
, param_len
, 10, /* param, length, max */
4362 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
4367 if (!cli_receive_trans(cli
, SMBtrans2
,
4368 &rparam
, &rparam_len
,
4369 &rdata
, &rdata_len
)) {
4373 if (!rdata
|| rdata_len
< 4) {
4377 ea_size
= (size_t)IVAL(rdata
,0);
4378 if (ea_size
> rdata_len
) {
4383 /* No EA's present. */
4391 /* Validate the EA list and count it. */
4392 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
4393 unsigned int ea_namelen
= CVAL(p
,1);
4394 unsigned int ea_valuelen
= SVAL(p
,2);
4395 if (ea_namelen
== 0) {
4398 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
4401 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
4402 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4410 *pnum_eas
= num_eas
;
4412 /* Caller only wants number of EA's. */
4417 ea_list
= TALLOC_ARRAY(ctx
, struct ea_struct
, num_eas
);
4422 ea_size
= (size_t)IVAL(rdata
,0);
4425 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
4426 struct ea_struct
*ea
= &ea_list
[num_eas
];
4427 fstring unix_ea_name
;
4428 unsigned int ea_namelen
= CVAL(p
,1);
4429 unsigned int ea_valuelen
= SVAL(p
,2);
4431 ea
->flags
= CVAL(p
,0);
4432 unix_ea_name
[0] = '\0';
4433 pull_ascii_fstring(unix_ea_name
, p
+ 4);
4434 ea
->name
= talloc_strdup(ctx
, unix_ea_name
);
4435 /* Ensure the value is null terminated (in case it's a string). */
4436 ea
->value
= data_blob_talloc(ctx
, NULL
, ea_valuelen
+ 1);
4437 if (!ea
->value
.data
) {
4441 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
4443 ea
->value
.data
[ea_valuelen
] = 0;
4445 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
4448 *pea_list
= ea_list
;
4458 /*********************************************************
4459 Get an extended attribute list from a pathname.
4460 *********************************************************/
4462 bool cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
4465 struct ea_struct
**pea_list
)
4467 uint16_t setup
= TRANSACT2_QPATHINFO
;
4468 unsigned int param_len
= 0;
4471 size_t srclen
= 2*(strlen(path
)+1);
4474 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
4480 SSVAL(p
, 0, SMB_INFO_QUERY_ALL_EAS
);
4482 p
+= clistr_push(cli
, p
, path
, srclen
, STR_TERMINATE
);
4483 param_len
= PTR_DIFF(p
, param
);
4485 ret
= cli_get_ea_list(cli
, setup
, param
, param_len
, ctx
, pnum_eas
, pea_list
);
4490 /*********************************************************
4491 Get an extended attribute list from an fnum.
4492 *********************************************************/
4494 bool cli_get_ea_list_fnum(struct cli_state
*cli
, uint16_t fnum
,
4497 struct ea_struct
**pea_list
)
4499 uint16_t setup
= TRANSACT2_QFILEINFO
;
4502 memset(param
, 0, 6);
4503 SSVAL(param
,0,fnum
);
4504 SSVAL(param
,2,SMB_INFO_SET_EA
);
4506 return cli_get_ea_list(cli
, setup
, param
, 6, ctx
, pnum_eas
, pea_list
);
4509 /****************************************************************************
4510 Convert open "flags" arg to uint32_t on wire.
4511 ****************************************************************************/
4513 static uint32_t open_flags_to_wire(int flags
)
4515 int open_mode
= flags
& O_ACCMODE
;
4518 switch (open_mode
) {
4520 ret
|= SMB_O_WRONLY
;
4527 ret
|= SMB_O_RDONLY
;
4531 if (flags
& O_CREAT
) {
4534 if (flags
& O_EXCL
) {
4537 if (flags
& O_TRUNC
) {
4541 if (flags
& O_SYNC
) {
4545 if (flags
& O_APPEND
) {
4546 ret
|= SMB_O_APPEND
;
4548 #if defined(O_DIRECT)
4549 if (flags
& O_DIRECT
) {
4550 ret
|= SMB_O_DIRECT
;
4553 #if defined(O_DIRECTORY)
4554 if (flags
& O_DIRECTORY
) {
4555 ret
&= ~(SMB_O_RDONLY
|SMB_O_RDWR
|SMB_O_WRONLY
);
4556 ret
|= SMB_O_DIRECTORY
;
4562 /****************************************************************************
4563 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4564 ****************************************************************************/
4566 struct posix_open_state
{
4570 uint16_t fnum
; /* Out */
4573 static void cli_posix_open_internal_done(struct tevent_req
*subreq
)
4575 struct tevent_req
*req
= tevent_req_callback_data(
4576 subreq
, struct tevent_req
);
4577 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4582 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
, &data
, &num_data
);
4583 TALLOC_FREE(subreq
);
4584 if (!NT_STATUS_IS_OK(status
)) {
4585 tevent_req_nterror(req
, status
);
4588 if (num_data
< 12) {
4589 tevent_req_nterror(req
, status
);
4592 state
->fnum
= SVAL(data
,2);
4593 tevent_req_done(req
);
4596 static struct tevent_req
*cli_posix_open_internal_send(TALLOC_CTX
*mem_ctx
,
4597 struct event_context
*ev
,
4598 struct cli_state
*cli
,
4604 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4605 struct posix_open_state
*state
= NULL
;
4606 uint32_t wire_flags
= open_flags_to_wire(flags
);
4608 req
= tevent_req_create(mem_ctx
, &state
, struct posix_open_state
);
4613 /* Setup setup word. */
4614 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4616 /* Setup param array. */
4617 state
->param
= talloc_array(state
, uint8_t, 6);
4618 if (tevent_req_nomem(state
->param
, req
)) {
4619 return tevent_req_post(req
, ev
);
4621 memset(state
->param
, '\0', 6);
4622 SSVAL(state
->param
, 0, SMB_POSIX_PATH_OPEN
);
4624 state
->param
= trans2_bytes_push_str(state
->param
, cli_ucs2(cli
), fname
,
4625 strlen(fname
)+1, NULL
);
4627 if (tevent_req_nomem(state
->param
, req
)) {
4628 return tevent_req_post(req
, ev
);
4631 /* Setup data words. */
4633 wire_flags
&= ~(SMB_O_RDONLY
|SMB_O_RDWR
|SMB_O_WRONLY
);
4634 wire_flags
|= SMB_O_DIRECTORY
;
4637 SIVAL(state
->data
,0,0); /* No oplock. */
4638 SIVAL(state
->data
,4,wire_flags
);
4639 SIVAL(state
->data
,8,unix_perms_to_wire(mode
));
4640 SIVAL(state
->data
,12,0); /* Top bits of perms currently undefined. */
4641 SSVAL(state
->data
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
4643 subreq
= cli_trans_send(state
, /* mem ctx. */
4644 ev
, /* event ctx. */
4645 cli
, /* cli_state. */
4646 SMBtrans2
, /* cmd. */
4647 NULL
, /* pipe name. */
4651 &state
->setup
, /* setup. */
4652 1, /* num setup uint16_t words. */
4653 0, /* max returned setup. */
4654 state
->param
, /* param. */
4655 talloc_get_size(state
->param
),/* num param. */
4656 2, /* max returned param. */
4657 state
->data
, /* data. */
4659 12); /* max returned data. */
4661 if (tevent_req_nomem(subreq
, req
)) {
4662 return tevent_req_post(req
, ev
);
4664 tevent_req_set_callback(subreq
, cli_posix_open_internal_done
, req
);
4668 struct tevent_req
*cli_posix_open_send(TALLOC_CTX
*mem_ctx
,
4669 struct event_context
*ev
,
4670 struct cli_state
*cli
,
4675 return cli_posix_open_internal_send(mem_ctx
, ev
,
4676 cli
, fname
, flags
, mode
, false);
4679 NTSTATUS
cli_posix_open_recv(struct tevent_req
*req
, uint16_t *pfnum
)
4681 struct posix_open_state
*state
= tevent_req_data(req
, struct posix_open_state
);
4684 if (tevent_req_is_nterror(req
, &status
)) {
4687 *pfnum
= state
->fnum
;
4688 return NT_STATUS_OK
;
4691 /****************************************************************************
4692 Open - POSIX semantics. Doesn't request oplock.
4693 ****************************************************************************/
4695 NTSTATUS
cli_posix_open(struct cli_state
*cli
, const char *fname
,
4696 int flags
, mode_t mode
, uint16_t *pfnum
)
4699 TALLOC_CTX
*frame
= talloc_stackframe();
4700 struct event_context
*ev
= NULL
;
4701 struct tevent_req
*req
= NULL
;
4702 NTSTATUS status
= NT_STATUS_OK
;
4704 if (cli_has_async_calls(cli
)) {
4706 * Can't use sync call while an async call is in flight
4708 status
= NT_STATUS_INVALID_PARAMETER
;
4712 ev
= event_context_init(frame
);
4714 status
= NT_STATUS_NO_MEMORY
;
4718 req
= cli_posix_open_send(frame
,
4725 status
= NT_STATUS_NO_MEMORY
;
4729 if (!tevent_req_poll(req
, ev
)) {
4730 status
= map_nt_error_from_unix(errno
);
4734 status
= cli_posix_open_recv(req
, pfnum
);
4738 if (!NT_STATUS_IS_OK(status
)) {
4739 cli_set_error(cli
, status
);
4744 struct tevent_req
*cli_posix_mkdir_send(TALLOC_CTX
*mem_ctx
,
4745 struct event_context
*ev
,
4746 struct cli_state
*cli
,
4750 return cli_posix_open_internal_send(mem_ctx
, ev
,
4751 cli
, fname
, O_CREAT
, mode
, true);
4754 NTSTATUS
cli_posix_mkdir_recv(struct tevent_req
*req
)
4758 if (tevent_req_is_nterror(req
, &status
)) {
4761 return NT_STATUS_OK
;
4764 NTSTATUS
cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
4766 TALLOC_CTX
*frame
= talloc_stackframe();
4767 struct event_context
*ev
= NULL
;
4768 struct tevent_req
*req
= NULL
;
4769 NTSTATUS status
= NT_STATUS_OK
;
4771 if (cli_has_async_calls(cli
)) {
4773 * Can't use sync call while an async call is in flight
4775 status
= NT_STATUS_INVALID_PARAMETER
;
4779 ev
= event_context_init(frame
);
4781 status
= NT_STATUS_NO_MEMORY
;
4785 req
= cli_posix_mkdir_send(frame
,
4791 status
= NT_STATUS_NO_MEMORY
;
4795 if (!tevent_req_poll(req
, ev
)) {
4796 status
= map_nt_error_from_unix(errno
);
4800 status
= cli_posix_mkdir_recv(req
);
4804 if (!NT_STATUS_IS_OK(status
)) {
4805 cli_set_error(cli
, status
);
4810 /****************************************************************************
4811 unlink or rmdir - POSIX semantics.
4812 ****************************************************************************/
4814 struct unlink_state
{
4819 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
4821 struct tevent_req
*req
= tevent_req_callback_data(
4822 subreq
, struct tevent_req
);
4823 struct unlink_state
*state
= tevent_req_data(req
, struct unlink_state
);
4826 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
4827 TALLOC_FREE(subreq
);
4828 if (!NT_STATUS_IS_OK(status
)) {
4829 tevent_req_nterror(req
, status
);
4832 tevent_req_done(req
);
4835 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
4836 struct event_context
*ev
,
4837 struct cli_state
*cli
,
4841 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
4842 struct unlink_state
*state
= NULL
;
4843 uint8_t *param
= NULL
;
4845 req
= tevent_req_create(mem_ctx
, &state
, struct unlink_state
);
4850 /* Setup setup word. */
4851 SSVAL(&state
->setup
, 0, TRANSACT2_SETPATHINFO
);
4853 /* Setup param array. */
4854 param
= talloc_array(state
, uint8_t, 6);
4855 if (tevent_req_nomem(param
, req
)) {
4856 return tevent_req_post(req
, ev
);
4858 memset(param
, '\0', 6);
4859 SSVAL(param
, 0, SMB_POSIX_PATH_UNLINK
);
4861 param
= trans2_bytes_push_str(param
, cli_ucs2(cli
), fname
,
4862 strlen(fname
)+1, NULL
);
4864 if (tevent_req_nomem(param
, req
)) {
4865 return tevent_req_post(req
, ev
);
4868 /* Setup data word. */
4869 SSVAL(state
->data
, 0, is_dir
? SMB_POSIX_UNLINK_DIRECTORY_TARGET
:
4870 SMB_POSIX_UNLINK_FILE_TARGET
);
4872 subreq
= cli_trans_send(state
, /* mem ctx. */
4873 ev
, /* event ctx. */
4874 cli
, /* cli_state. */
4875 SMBtrans2
, /* cmd. */
4876 NULL
, /* pipe name. */
4880 &state
->setup
, /* setup. */
4881 1, /* num setup uint16_t words. */
4882 0, /* max returned setup. */
4884 talloc_get_size(param
), /* num param. */
4885 2, /* max returned param. */
4886 state
->data
, /* data. */
4888 0); /* max returned data. */
4890 if (tevent_req_nomem(subreq
, req
)) {
4891 return tevent_req_post(req
, ev
);
4893 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
4897 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
4898 struct event_context
*ev
,
4899 struct cli_state
*cli
,
4902 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
, false);
4905 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
)
4909 if (tevent_req_is_nterror(req
, &status
)) {
4912 return NT_STATUS_OK
;
4915 /****************************************************************************
4916 unlink - POSIX semantics.
4917 ****************************************************************************/
4919 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
4921 TALLOC_CTX
*frame
= talloc_stackframe();
4922 struct event_context
*ev
= NULL
;
4923 struct tevent_req
*req
= NULL
;
4924 NTSTATUS status
= NT_STATUS_OK
;
4926 if (cli_has_async_calls(cli
)) {
4928 * Can't use sync call while an async call is in flight
4930 status
= NT_STATUS_INVALID_PARAMETER
;
4934 ev
= event_context_init(frame
);
4936 status
= NT_STATUS_NO_MEMORY
;
4940 req
= cli_posix_unlink_send(frame
,
4945 status
= NT_STATUS_NO_MEMORY
;
4949 if (!tevent_req_poll(req
, ev
)) {
4950 status
= map_nt_error_from_unix(errno
);
4954 status
= cli_posix_unlink_recv(req
);
4958 if (!NT_STATUS_IS_OK(status
)) {
4959 cli_set_error(cli
, status
);
4964 /****************************************************************************
4965 rmdir - POSIX semantics.
4966 ****************************************************************************/
4968 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
4969 struct event_context
*ev
,
4970 struct cli_state
*cli
,
4973 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
, true);
4976 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
4980 if (tevent_req_is_nterror(req
, &status
)) {
4983 return NT_STATUS_OK
;
4986 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
4988 TALLOC_CTX
*frame
= talloc_stackframe();
4989 struct event_context
*ev
= NULL
;
4990 struct tevent_req
*req
= NULL
;
4991 NTSTATUS status
= NT_STATUS_OK
;
4993 if (cli_has_async_calls(cli
)) {
4995 * Can't use sync call while an async call is in flight
4997 status
= NT_STATUS_INVALID_PARAMETER
;
5001 ev
= event_context_init(frame
);
5003 status
= NT_STATUS_NO_MEMORY
;
5007 req
= cli_posix_rmdir_send(frame
,
5012 status
= NT_STATUS_NO_MEMORY
;
5016 if (!tevent_req_poll(req
, ev
)) {
5017 status
= map_nt_error_from_unix(errno
);
5021 status
= cli_posix_rmdir_recv(req
, frame
);
5025 if (!NT_STATUS_IS_OK(status
)) {
5026 cli_set_error(cli
, status
);
5031 /****************************************************************************
5033 ****************************************************************************/
5035 struct cli_notify_state
{
5037 uint32_t num_changes
;
5038 struct notify_change
*changes
;
5041 static void cli_notify_done(struct tevent_req
*subreq
);
5043 struct tevent_req
*cli_notify_send(TALLOC_CTX
*mem_ctx
,
5044 struct tevent_context
*ev
,
5045 struct cli_state
*cli
, uint16_t fnum
,
5046 uint32_t buffer_size
,
5047 uint32_t completion_filter
, bool recursive
)
5049 struct tevent_req
*req
, *subreq
;
5050 struct cli_notify_state
*state
;
5052 req
= tevent_req_create(mem_ctx
, &state
, struct cli_notify_state
);
5057 SIVAL(state
->setup
, 0, completion_filter
);
5058 SSVAL(state
->setup
, 4, fnum
);
5059 SSVAL(state
->setup
, 6, recursive
);
5061 subreq
= cli_trans_send(
5062 state
, /* mem ctx. */
5063 ev
, /* event ctx. */
5064 cli
, /* cli_state. */
5065 SMBnttrans
, /* cmd. */
5066 NULL
, /* pipe name. */
5068 NT_TRANSACT_NOTIFY_CHANGE
, /* function. */
5070 (uint16_t *)state
->setup
, /* setup. */
5071 4, /* num setup uint16_t words. */
5072 0, /* max returned setup. */
5075 buffer_size
, /* max returned param. */
5078 0); /* max returned data. */
5080 if (tevent_req_nomem(subreq
, req
)) {
5081 return tevent_req_post(req
, ev
);
5083 tevent_req_set_callback(subreq
, cli_notify_done
, req
);
5087 static void cli_notify_done(struct tevent_req
*subreq
)
5089 struct tevent_req
*req
= tevent_req_callback_data(
5090 subreq
, struct tevent_req
);
5091 struct cli_notify_state
*state
= tevent_req_data(
5092 req
, struct cli_notify_state
);
5095 uint32_t i
, ofs
, num_params
;
5097 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
, NULL
,
5098 ¶ms
, &num_params
, NULL
, NULL
);
5099 TALLOC_FREE(subreq
);
5100 if (!NT_STATUS_IS_OK(status
)) {
5101 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status
)));
5102 tevent_req_nterror(req
, status
);
5106 state
->num_changes
= 0;
5109 while (num_params
- ofs
> 12) {
5110 uint32_t len
= IVAL(params
, ofs
);
5111 state
->num_changes
+= 1;
5113 if ((len
== 0) || (ofs
+len
>= num_params
)) {
5119 state
->changes
= talloc_array(state
, struct notify_change
,
5120 state
->num_changes
);
5121 if (tevent_req_nomem(state
->changes
, req
)) {
5122 TALLOC_FREE(params
);
5128 for (i
=0; i
<state
->num_changes
; i
++) {
5129 uint32_t next
= IVAL(params
, ofs
);
5130 uint32_t len
= IVAL(params
, ofs
+8);
5134 if ((next
!= 0) && (len
+12 != next
)) {
5135 TALLOC_FREE(params
);
5137 req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5141 state
->changes
[i
].action
= IVAL(params
, ofs
+4);
5142 ret
= clistr_pull_talloc(params
, (char *)params
, &name
,
5144 STR_TERMINATE
|STR_UNICODE
);
5146 TALLOC_FREE(params
);
5147 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5150 state
->changes
[i
].name
= name
;
5154 TALLOC_FREE(params
);
5155 tevent_req_done(req
);
5158 NTSTATUS
cli_notify_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
5159 uint32_t *pnum_changes
,
5160 struct notify_change
**pchanges
)
5162 struct cli_notify_state
*state
= tevent_req_data(
5163 req
, struct cli_notify_state
);
5166 if (tevent_req_is_nterror(req
, &status
)) {
5170 *pnum_changes
= state
->num_changes
;
5171 *pchanges
= talloc_move(mem_ctx
, &state
->changes
);
5172 return NT_STATUS_OK
;