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 Hard/Symlink a file (UNIX extensions).
25 Creates new name (sym)linked to oldname.
26 ****************************************************************************/
28 static bool cli_link_internal(struct cli_state
*cli
, const char *oldname
, const char *newname
, bool hard_link
)
30 unsigned int data_len
= 0;
31 unsigned int param_len
= 0;
32 uint16_t setup
= TRANSACT2_SETPATHINFO
;
35 char *rparam
=NULL
, *rdata
=NULL
;
37 size_t oldlen
= 2*(strlen(oldname
)+1);
38 size_t newlen
= 2*(strlen(newname
)+1);
40 param
= SMB_MALLOC_ARRAY(char, 6+newlen
+2);
46 data
= SMB_MALLOC_ARRAY(char, oldlen
+2);
53 SSVAL(param
,0,hard_link
? SMB_SET_FILE_UNIX_HLINK
: SMB_SET_FILE_UNIX_LINK
);
57 p
+= clistr_push(cli
, p
, newname
, newlen
, STR_TERMINATE
);
58 param_len
= PTR_DIFF(p
, param
);
61 p
+= clistr_push(cli
, p
, oldname
, oldlen
, STR_TERMINATE
);
62 data_len
= PTR_DIFF(p
, data
);
64 if (!cli_send_trans(cli
, SMBtrans2
,
66 -1, 0, /* fid, flags */
67 &setup
, 1, 0, /* setup, length, max */
68 param
, param_len
, 2, /* param, length, max */
69 data
, data_len
, cli
->max_xmit
/* data, length, max */
79 if (!cli_receive_trans(cli
, SMBtrans2
,
93 /****************************************************************************
94 Map standard UNIX permissions onto wire representations.
95 ****************************************************************************/
97 uint32_t unix_perms_to_wire(mode_t perms
)
101 ret
|= ((perms
& S_IXOTH
) ? UNIX_X_OTH
: 0);
102 ret
|= ((perms
& S_IWOTH
) ? UNIX_W_OTH
: 0);
103 ret
|= ((perms
& S_IROTH
) ? UNIX_R_OTH
: 0);
104 ret
|= ((perms
& S_IXGRP
) ? UNIX_X_GRP
: 0);
105 ret
|= ((perms
& S_IWGRP
) ? UNIX_W_GRP
: 0);
106 ret
|= ((perms
& S_IRGRP
) ? UNIX_R_GRP
: 0);
107 ret
|= ((perms
& S_IXUSR
) ? UNIX_X_USR
: 0);
108 ret
|= ((perms
& S_IWUSR
) ? UNIX_W_USR
: 0);
109 ret
|= ((perms
& S_IRUSR
) ? UNIX_R_USR
: 0);
111 ret
|= ((perms
& S_ISVTX
) ? UNIX_STICKY
: 0);
114 ret
|= ((perms
& S_ISGID
) ? UNIX_SET_GID
: 0);
117 ret
|= ((perms
& S_ISUID
) ? UNIX_SET_UID
: 0);
122 /****************************************************************************
123 Map wire permissions to standard UNIX.
124 ****************************************************************************/
126 mode_t
wire_perms_to_unix(uint32_t perms
)
128 mode_t ret
= (mode_t
)0;
130 ret
|= ((perms
& UNIX_X_OTH
) ? S_IXOTH
: 0);
131 ret
|= ((perms
& UNIX_W_OTH
) ? S_IWOTH
: 0);
132 ret
|= ((perms
& UNIX_R_OTH
) ? S_IROTH
: 0);
133 ret
|= ((perms
& UNIX_X_GRP
) ? S_IXGRP
: 0);
134 ret
|= ((perms
& UNIX_W_GRP
) ? S_IWGRP
: 0);
135 ret
|= ((perms
& UNIX_R_GRP
) ? S_IRGRP
: 0);
136 ret
|= ((perms
& UNIX_X_USR
) ? S_IXUSR
: 0);
137 ret
|= ((perms
& UNIX_W_USR
) ? S_IWUSR
: 0);
138 ret
|= ((perms
& UNIX_R_USR
) ? S_IRUSR
: 0);
140 ret
|= ((perms
& UNIX_STICKY
) ? S_ISVTX
: 0);
143 ret
|= ((perms
& UNIX_SET_GID
) ? S_ISGID
: 0);
146 ret
|= ((perms
& UNIX_SET_UID
) ? S_ISUID
: 0);
151 /****************************************************************************
152 Return the file type from the wire filetype for UNIX extensions.
153 ****************************************************************************/
155 static mode_t
unix_filetype_from_wire(uint32_t wire_type
)
163 case UNIX_TYPE_SYMLINK
:
167 case UNIX_TYPE_CHARDEV
:
171 case UNIX_TYPE_BLKDEV
:
179 case UNIX_TYPE_SOCKET
:
187 /****************************************************************************
188 Do a POSIX getfacl (UNIX extensions).
189 ****************************************************************************/
191 bool cli_unix_getfacl(struct cli_state
*cli
, const char *name
, size_t *prb_size
, char **retbuf
)
193 unsigned int param_len
= 0;
194 unsigned int data_len
= 0;
195 uint16_t setup
= TRANSACT2_QPATHINFO
;
197 size_t nlen
= 2*(strlen(name
)+1);
198 char *rparam
=NULL
, *rdata
=NULL
;
201 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
208 SSVAL(p
, 0, SMB_QUERY_POSIX_ACL
);
210 p
+= clistr_push(cli
, p
, name
, nlen
, STR_TERMINATE
);
211 param_len
= PTR_DIFF(p
, param
);
213 if (!cli_send_trans(cli
, SMBtrans2
,
215 -1, 0, /* fid, flags */
216 &setup
, 1, 0, /* setup, length, max */
217 param
, param_len
, 2, /* param, length, max */
218 NULL
, 0, cli
->max_xmit
/* data, length, max */
226 if (!cli_receive_trans(cli
, SMBtrans2
,
228 &rdata
, &data_len
)) {
240 *prb_size
= (size_t)data_len
;
245 /****************************************************************************
246 Stat a file (UNIX extensions).
247 ****************************************************************************/
249 bool cli_unix_stat(struct cli_state
*cli
, const char *name
, SMB_STRUCT_STAT
*sbuf
)
251 unsigned int param_len
= 0;
252 unsigned int data_len
= 0;
253 uint16_t setup
= TRANSACT2_QPATHINFO
;
255 size_t nlen
= 2*(strlen(name
)+1);
256 char *rparam
=NULL
, *rdata
=NULL
;
261 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
267 SSVAL(p
, 0, SMB_QUERY_FILE_UNIX_BASIC
);
269 p
+= clistr_push(cli
, p
, name
, nlen
, STR_TERMINATE
);
270 param_len
= PTR_DIFF(p
, param
);
272 if (!cli_send_trans(cli
, SMBtrans2
,
274 -1, 0, /* fid, flags */
275 &setup
, 1, 0, /* setup, length, max */
276 param
, param_len
, 2, /* param, length, max */
277 NULL
, 0, cli
->max_xmit
/* data, length, max */
285 if (!cli_receive_trans(cli
, SMBtrans2
,
287 &rdata
, &data_len
)) {
297 sbuf
->st_size
= IVAL2_TO_SMB_BIG_UINT(rdata
,0); /* total size, in bytes */
298 sbuf
->st_blocks
= IVAL2_TO_SMB_BIG_UINT(rdata
,8); /* number of blocks allocated */
299 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
300 sbuf
->st_blocks
/= STAT_ST_BLOCKSIZE
;
302 /* assume 512 byte blocks */
303 sbuf
->st_blocks
/= 512;
305 set_ctimespec(sbuf
, interpret_long_date(rdata
+ 16)); /* time of last change */
306 set_atimespec(sbuf
, interpret_long_date(rdata
+ 24)); /* time of last access */
307 set_mtimespec(sbuf
, interpret_long_date(rdata
+ 32)); /* time of last modification */
309 sbuf
->st_uid
= (uid_t
) IVAL(rdata
,40); /* user ID of owner */
310 sbuf
->st_gid
= (gid_t
) IVAL(rdata
,48); /* group ID of owner */
311 sbuf
->st_mode
|= unix_filetype_from_wire(IVAL(rdata
, 56));
312 #if defined(HAVE_MAKEDEV)
314 uint32_t dev_major
= IVAL(rdata
,60);
315 uint32_t dev_minor
= IVAL(rdata
,68);
316 sbuf
->st_rdev
= makedev(dev_major
, dev_minor
);
319 sbuf
->st_ino
= (SMB_INO_T
)IVAL2_TO_SMB_BIG_UINT(rdata
,76); /* inode */
320 sbuf
->st_mode
|= wire_perms_to_unix(IVAL(rdata
,84)); /* protection */
321 sbuf
->st_nlink
= IVAL(rdata
,92); /* number of hard links */
329 /****************************************************************************
330 Symlink a file (UNIX extensions).
331 ****************************************************************************/
333 bool cli_unix_symlink(struct cli_state
*cli
, const char *oldname
, const char *newname
)
335 return cli_link_internal(cli
, oldname
, newname
, False
);
338 /****************************************************************************
339 Hard a file (UNIX extensions).
340 ****************************************************************************/
342 bool cli_unix_hardlink(struct cli_state
*cli
, const char *oldname
, const char *newname
)
344 return cli_link_internal(cli
, oldname
, newname
, True
);
347 /****************************************************************************
348 Chmod or chown a file internal (UNIX extensions).
349 ****************************************************************************/
351 static bool cli_unix_chmod_chown_internal(struct cli_state
*cli
, const char *fname
, uint32_t mode
, uint32_t uid
, uint32_t gid
)
353 unsigned int data_len
= 0;
354 unsigned int param_len
= 0;
355 uint16_t setup
= TRANSACT2_SETPATHINFO
;
356 size_t nlen
= 2*(strlen(fname
)+1);
359 char *rparam
=NULL
, *rdata
=NULL
;
362 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
366 memset(param
, '\0', 6);
367 memset(data
, 0, sizeof(data
));
369 SSVAL(param
,0,SMB_SET_FILE_UNIX_BASIC
);
372 p
+= clistr_push(cli
, p
, fname
, nlen
, STR_TERMINATE
);
373 param_len
= PTR_DIFF(p
, param
);
375 memset(data
, 0xff, 40); /* Set all sizes/times to no change. */
383 if (!cli_send_trans(cli
, SMBtrans2
,
385 -1, 0, /* fid, flags */
386 &setup
, 1, 0, /* setup, length, max */
387 param
, param_len
, 2, /* param, length, max */
388 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
396 if (!cli_receive_trans(cli
, SMBtrans2
,
398 &rdata
, &data_len
)) {
408 /****************************************************************************
409 chmod a file (UNIX extensions).
410 ****************************************************************************/
412 bool cli_unix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
414 return cli_unix_chmod_chown_internal(cli
, fname
,
415 unix_perms_to_wire(mode
), SMB_UID_NO_CHANGE
, SMB_GID_NO_CHANGE
);
418 /****************************************************************************
419 chown a file (UNIX extensions).
420 ****************************************************************************/
422 bool cli_unix_chown(struct cli_state
*cli
, const char *fname
, uid_t uid
, gid_t gid
)
424 return cli_unix_chmod_chown_internal(cli
, fname
,
425 SMB_MODE_NO_CHANGE
, (uint32
)uid
, (uint32
)gid
);
428 /****************************************************************************
430 ****************************************************************************/
432 static void cli_rename_done(struct tevent_req
*subreq
);
434 struct cli_rename_state
{
438 struct tevent_req
*cli_rename_send(TALLOC_CTX
*mem_ctx
,
439 struct event_context
*ev
,
440 struct cli_state
*cli
,
441 const char *fname_src
,
442 const char *fname_dst
)
444 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
445 struct cli_rename_state
*state
= NULL
;
446 uint8_t additional_flags
= 0;
447 uint8_t *bytes
= NULL
;
449 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rename_state
);
454 SSVAL(state
->vwv
+0, 0, aSYSTEM
| aHIDDEN
| aDIR
);
456 bytes
= talloc_array(state
, uint8_t, 1);
457 if (tevent_req_nomem(bytes
, req
)) {
458 return tevent_req_post(req
, ev
);
461 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_src
,
462 strlen(fname_src
)+1, NULL
);
463 if (tevent_req_nomem(bytes
, req
)) {
464 return tevent_req_post(req
, ev
);
467 bytes
= TALLOC_REALLOC_ARRAY(state
, bytes
, uint8_t,
468 talloc_get_size(bytes
)+1);
469 if (tevent_req_nomem(bytes
, req
)) {
470 return tevent_req_post(req
, ev
);
473 bytes
[talloc_get_size(bytes
)-1] = 4;
474 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_dst
,
475 strlen(fname_dst
)+1, NULL
);
476 if (tevent_req_nomem(bytes
, req
)) {
477 return tevent_req_post(req
, ev
);
480 subreq
= cli_smb_send(state
, ev
, cli
, SMBmv
, additional_flags
,
481 1, state
->vwv
, talloc_get_size(bytes
), bytes
);
482 if (tevent_req_nomem(subreq
, req
)) {
483 return tevent_req_post(req
, ev
);
485 tevent_req_set_callback(subreq
, cli_rename_done
, req
);
489 static void cli_rename_done(struct tevent_req
*subreq
)
491 struct tevent_req
*req
= tevent_req_callback_data(
492 subreq
, struct tevent_req
);
495 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
497 if (!NT_STATUS_IS_OK(status
)) {
498 tevent_req_nterror(req
, status
);
501 tevent_req_done(req
);
504 NTSTATUS
cli_rename_recv(struct tevent_req
*req
)
506 return tevent_req_simple_recv_ntstatus(req
);
509 NTSTATUS
cli_rename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
511 TALLOC_CTX
*frame
= talloc_stackframe();
512 struct event_context
*ev
;
513 struct tevent_req
*req
;
514 NTSTATUS status
= NT_STATUS_OK
;
516 if (cli_has_async_calls(cli
)) {
518 * Can't use sync call while an async call is in flight
520 status
= NT_STATUS_INVALID_PARAMETER
;
524 ev
= event_context_init(frame
);
526 status
= NT_STATUS_NO_MEMORY
;
530 req
= cli_rename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
532 status
= NT_STATUS_NO_MEMORY
;
536 if (!tevent_req_poll(req
, ev
)) {
537 status
= map_nt_error_from_unix(errno
);
541 status
= cli_rename_recv(req
);
545 if (!NT_STATUS_IS_OK(status
)) {
546 cli_set_error(cli
, status
);
551 /****************************************************************************
553 ****************************************************************************/
555 static void cli_ntrename_done(struct tevent_req
*subreq
);
557 struct cli_ntrename_state
{
561 static struct tevent_req
*cli_ntrename_send_internal(TALLOC_CTX
*mem_ctx
,
562 struct event_context
*ev
,
563 struct cli_state
*cli
,
564 const char *fname_src
,
565 const char *fname_dst
,
566 uint16_t rename_flag
)
568 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
569 struct cli_ntrename_state
*state
= NULL
;
570 uint8_t additional_flags
= 0;
571 uint8_t *bytes
= NULL
;
573 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntrename_state
);
578 SSVAL(state
->vwv
+0, 0 ,aSYSTEM
| aHIDDEN
| aDIR
);
579 SSVAL(state
->vwv
+1, 0, rename_flag
);
581 bytes
= talloc_array(state
, uint8_t, 1);
582 if (tevent_req_nomem(bytes
, req
)) {
583 return tevent_req_post(req
, ev
);
586 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_src
,
587 strlen(fname_src
)+1, NULL
);
588 if (tevent_req_nomem(bytes
, req
)) {
589 return tevent_req_post(req
, ev
);
592 bytes
= TALLOC_REALLOC_ARRAY(state
, bytes
, uint8_t,
593 talloc_get_size(bytes
)+1);
594 if (tevent_req_nomem(bytes
, req
)) {
595 return tevent_req_post(req
, ev
);
598 bytes
[talloc_get_size(bytes
)-1] = 4;
599 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname_dst
,
600 strlen(fname_dst
)+1, NULL
);
601 if (tevent_req_nomem(bytes
, req
)) {
602 return tevent_req_post(req
, ev
);
605 subreq
= cli_smb_send(state
, ev
, cli
, SMBntrename
, additional_flags
,
606 4, state
->vwv
, talloc_get_size(bytes
), bytes
);
607 if (tevent_req_nomem(subreq
, req
)) {
608 return tevent_req_post(req
, ev
);
610 tevent_req_set_callback(subreq
, cli_ntrename_done
, req
);
614 struct tevent_req
*cli_ntrename_send(TALLOC_CTX
*mem_ctx
,
615 struct event_context
*ev
,
616 struct cli_state
*cli
,
617 const char *fname_src
,
618 const char *fname_dst
)
620 return cli_ntrename_send_internal(mem_ctx
,
628 static void cli_ntrename_done(struct tevent_req
*subreq
)
630 struct tevent_req
*req
= tevent_req_callback_data(
631 subreq
, struct tevent_req
);
634 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
636 if (!NT_STATUS_IS_OK(status
)) {
637 tevent_req_nterror(req
, status
);
640 tevent_req_done(req
);
643 NTSTATUS
cli_ntrename_recv(struct tevent_req
*req
)
645 return tevent_req_simple_recv_ntstatus(req
);
648 NTSTATUS
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
650 TALLOC_CTX
*frame
= talloc_stackframe();
651 struct event_context
*ev
;
652 struct tevent_req
*req
;
653 NTSTATUS status
= NT_STATUS_OK
;
655 if (cli_has_async_calls(cli
)) {
657 * Can't use sync call while an async call is in flight
659 status
= NT_STATUS_INVALID_PARAMETER
;
663 ev
= event_context_init(frame
);
665 status
= NT_STATUS_NO_MEMORY
;
669 req
= cli_ntrename_send(frame
, ev
, cli
, fname_src
, fname_dst
);
671 status
= NT_STATUS_NO_MEMORY
;
675 if (!tevent_req_poll(req
, ev
)) {
676 status
= map_nt_error_from_unix(errno
);
680 status
= cli_ntrename_recv(req
);
684 if (!NT_STATUS_IS_OK(status
)) {
685 cli_set_error(cli
, status
);
690 /****************************************************************************
692 ****************************************************************************/
694 struct tevent_req
*cli_nt_hardlink_send(TALLOC_CTX
*mem_ctx
,
695 struct event_context
*ev
,
696 struct cli_state
*cli
,
697 const char *fname_src
,
698 const char *fname_dst
)
700 return cli_ntrename_send_internal(mem_ctx
,
705 RENAME_FLAG_HARD_LINK
);
708 NTSTATUS
cli_nt_hardlink_recv(struct tevent_req
*req
)
710 return tevent_req_simple_recv_ntstatus(req
);
713 NTSTATUS
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
715 TALLOC_CTX
*frame
= talloc_stackframe();
716 struct event_context
*ev
;
717 struct tevent_req
*req
;
718 NTSTATUS status
= NT_STATUS_OK
;
720 if (cli_has_async_calls(cli
)) {
722 * Can't use sync call while an async call is in flight
724 status
= NT_STATUS_INVALID_PARAMETER
;
728 ev
= event_context_init(frame
);
730 status
= NT_STATUS_NO_MEMORY
;
734 req
= cli_nt_hardlink_send(frame
, ev
, cli
, fname_src
, fname_dst
);
736 status
= NT_STATUS_NO_MEMORY
;
740 if (!tevent_req_poll(req
, ev
)) {
741 status
= map_nt_error_from_unix(errno
);
745 status
= cli_nt_hardlink_recv(req
);
749 if (!NT_STATUS_IS_OK(status
)) {
750 cli_set_error(cli
, status
);
755 /****************************************************************************
757 ****************************************************************************/
759 bool cli_unlink_full(struct cli_state
*cli
, const char *fname
, uint16_t attrs
)
763 memset(cli
->outbuf
,'\0',smb_size
);
764 memset(cli
->inbuf
,'\0',smb_size
);
766 cli_set_message(cli
->outbuf
,1, 0, true);
768 SCVAL(cli
->outbuf
,smb_com
,SMBunlink
);
769 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
770 cli_setup_packet(cli
);
772 SSVAL(cli
->outbuf
,smb_vwv0
, attrs
);
774 p
= smb_buf(cli
->outbuf
);
776 p
+= clistr_push(cli
, p
, fname
,
777 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
779 cli_setup_bcc(cli
, p
);
781 if (!cli_receive_smb(cli
)) {
785 if (cli_is_error(cli
)) {
792 /****************************************************************************
794 ****************************************************************************/
796 bool cli_unlink(struct cli_state
*cli
, const char *fname
)
798 return cli_unlink_full(cli
, fname
, aSYSTEM
| aHIDDEN
);
801 /****************************************************************************
803 ****************************************************************************/
805 static void cli_mkdir_done(struct tevent_req
*subreq
);
807 struct cli_mkdir_state
{
811 struct tevent_req
*cli_mkdir_send(TALLOC_CTX
*mem_ctx
,
812 struct event_context
*ev
,
813 struct cli_state
*cli
,
816 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
817 struct cli_mkdir_state
*state
= NULL
;
818 uint8_t additional_flags
= 0;
819 uint8_t *bytes
= NULL
;
821 req
= tevent_req_create(mem_ctx
, &state
, struct cli_mkdir_state
);
826 bytes
= talloc_array(state
, uint8_t, 1);
827 if (tevent_req_nomem(bytes
, req
)) {
828 return tevent_req_post(req
, ev
);
831 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), dname
,
832 strlen(dname
)+1, NULL
);
834 if (tevent_req_nomem(bytes
, req
)) {
835 return tevent_req_post(req
, ev
);
838 subreq
= cli_smb_send(state
, ev
, cli
, SMBmkdir
, additional_flags
,
839 0, NULL
, talloc_get_size(bytes
), bytes
);
840 if (tevent_req_nomem(subreq
, req
)) {
841 return tevent_req_post(req
, ev
);
843 tevent_req_set_callback(subreq
, cli_mkdir_done
, req
);
847 static void cli_mkdir_done(struct tevent_req
*subreq
)
849 struct tevent_req
*req
= tevent_req_callback_data(
850 subreq
, struct tevent_req
);
853 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
855 if (!NT_STATUS_IS_OK(status
)) {
856 tevent_req_nterror(req
, status
);
859 tevent_req_done(req
);
862 NTSTATUS
cli_mkdir_recv(struct tevent_req
*req
)
864 return tevent_req_simple_recv_ntstatus(req
);
867 NTSTATUS
cli_mkdir(struct cli_state
*cli
, const char *dname
)
869 TALLOC_CTX
*frame
= talloc_stackframe();
870 struct event_context
*ev
;
871 struct tevent_req
*req
;
872 NTSTATUS status
= NT_STATUS_OK
;
874 if (cli_has_async_calls(cli
)) {
876 * Can't use sync call while an async call is in flight
878 status
= NT_STATUS_INVALID_PARAMETER
;
882 ev
= event_context_init(frame
);
884 status
= NT_STATUS_NO_MEMORY
;
888 req
= cli_mkdir_send(frame
, ev
, cli
, dname
);
890 status
= NT_STATUS_NO_MEMORY
;
894 if (!tevent_req_poll(req
, ev
)) {
895 status
= map_nt_error_from_unix(errno
);
899 status
= cli_mkdir_recv(req
);
903 if (!NT_STATUS_IS_OK(status
)) {
904 cli_set_error(cli
, status
);
909 /****************************************************************************
911 ****************************************************************************/
913 static void cli_rmdir_done(struct tevent_req
*subreq
);
915 struct cli_rmdir_state
{
919 struct tevent_req
*cli_rmdir_send(TALLOC_CTX
*mem_ctx
,
920 struct event_context
*ev
,
921 struct cli_state
*cli
,
924 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
925 struct cli_rmdir_state
*state
= NULL
;
926 uint8_t additional_flags
= 0;
927 uint8_t *bytes
= NULL
;
929 req
= tevent_req_create(mem_ctx
, &state
, struct cli_rmdir_state
);
934 bytes
= talloc_array(state
, uint8_t, 1);
935 if (tevent_req_nomem(bytes
, req
)) {
936 return tevent_req_post(req
, ev
);
939 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), dname
,
940 strlen(dname
)+1, NULL
);
942 if (tevent_req_nomem(bytes
, req
)) {
943 return tevent_req_post(req
, ev
);
946 subreq
= cli_smb_send(state
, ev
, cli
, SMBrmdir
, additional_flags
,
947 0, NULL
, talloc_get_size(bytes
), bytes
);
948 if (tevent_req_nomem(subreq
, req
)) {
949 return tevent_req_post(req
, ev
);
951 tevent_req_set_callback(subreq
, cli_rmdir_done
, req
);
955 static void cli_rmdir_done(struct tevent_req
*subreq
)
957 struct tevent_req
*req
= tevent_req_callback_data(
958 subreq
, struct tevent_req
);
961 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
963 if (!NT_STATUS_IS_OK(status
)) {
964 tevent_req_nterror(req
, status
);
967 tevent_req_done(req
);
970 NTSTATUS
cli_rmdir_recv(struct tevent_req
*req
)
972 return tevent_req_simple_recv_ntstatus(req
);
975 NTSTATUS
cli_rmdir(struct cli_state
*cli
, const char *dname
)
977 TALLOC_CTX
*frame
= talloc_stackframe();
978 struct event_context
*ev
;
979 struct tevent_req
*req
;
980 NTSTATUS status
= NT_STATUS_OK
;
982 if (cli_has_async_calls(cli
)) {
984 * Can't use sync call while an async call is in flight
986 status
= NT_STATUS_INVALID_PARAMETER
;
990 ev
= event_context_init(frame
);
992 status
= NT_STATUS_NO_MEMORY
;
996 req
= cli_rmdir_send(frame
, ev
, cli
, dname
);
998 status
= NT_STATUS_NO_MEMORY
;
1002 if (!tevent_req_poll(req
, ev
)) {
1003 status
= map_nt_error_from_unix(errno
);
1007 status
= cli_rmdir_recv(req
);
1011 if (!NT_STATUS_IS_OK(status
)) {
1012 cli_set_error(cli
, status
);
1017 /****************************************************************************
1018 Set or clear the delete on close flag.
1019 ****************************************************************************/
1021 int cli_nt_delete_on_close(struct cli_state
*cli
, int fnum
, bool flag
)
1023 unsigned int data_len
= 1;
1024 unsigned int param_len
= 6;
1025 uint16_t setup
= TRANSACT2_SETFILEINFO
;
1028 char *rparam
=NULL
, *rdata
=NULL
;
1030 memset(param
, 0, param_len
);
1031 SSVAL(param
,0,fnum
);
1032 SSVAL(param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
1034 data
= flag
? 1 : 0;
1036 if (!cli_send_trans(cli
, SMBtrans2
,
1038 -1, 0, /* fid, flags */
1039 &setup
, 1, 0, /* setup, length, max */
1040 param
, param_len
, 2, /* param, length, max */
1041 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
1046 if (!cli_receive_trans(cli
, SMBtrans2
,
1047 &rparam
, ¶m_len
,
1048 &rdata
, &data_len
)) {
1058 /****************************************************************************
1059 Open a file - exposing the full horror of the NT API :-).
1061 ****************************************************************************/
1063 int cli_nt_create_full(struct cli_state
*cli
, const char *fname
,
1064 uint32_t CreatFlags
, uint32_t DesiredAccess
,
1065 uint32_t FileAttributes
, uint32_t ShareAccess
,
1066 uint32_t CreateDisposition
, uint32_t CreateOptions
,
1067 uint8_t SecurityFlags
)
1072 memset(cli
->outbuf
,'\0',smb_size
);
1073 memset(cli
->inbuf
,'\0',smb_size
);
1075 cli_set_message(cli
->outbuf
,24,0, true);
1077 SCVAL(cli
->outbuf
,smb_com
,SMBntcreateX
);
1078 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1079 cli_setup_packet(cli
);
1081 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1082 if (cli
->use_oplocks
)
1083 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1085 SIVAL(cli
->outbuf
,smb_ntcreate_Flags
, CreatFlags
);
1086 SIVAL(cli
->outbuf
,smb_ntcreate_RootDirectoryFid
, 0x0);
1087 SIVAL(cli
->outbuf
,smb_ntcreate_DesiredAccess
, DesiredAccess
);
1088 SIVAL(cli
->outbuf
,smb_ntcreate_FileAttributes
, FileAttributes
);
1089 SIVAL(cli
->outbuf
,smb_ntcreate_ShareAccess
, ShareAccess
);
1090 SIVAL(cli
->outbuf
,smb_ntcreate_CreateDisposition
, CreateDisposition
);
1091 SIVAL(cli
->outbuf
,smb_ntcreate_CreateOptions
, CreateOptions
);
1092 SIVAL(cli
->outbuf
,smb_ntcreate_ImpersonationLevel
, 0x02);
1093 SCVAL(cli
->outbuf
,smb_ntcreate_SecurityFlags
, SecurityFlags
);
1095 p
= smb_buf(cli
->outbuf
);
1096 /* this alignment and termination is critical for netapp filers. Don't change */
1097 p
+= clistr_align_out(cli
, p
, 0);
1098 len
= clistr_push(cli
, p
, fname
,
1099 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), 0);
1101 SSVAL(cli
->outbuf
,smb_ntcreate_NameLength
, len
);
1102 /* sigh. this copes with broken netapp filer behaviour */
1103 p
+= clistr_push(cli
, p
, "",
1104 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
1106 cli_setup_bcc(cli
, p
);
1109 if (!cli_receive_smb(cli
)) {
1113 if (cli_is_error(cli
)) {
1117 return SVAL(cli
->inbuf
,smb_vwv2
+ 1);
1120 struct cli_ntcreate_state
{
1125 static void cli_ntcreate_done(struct tevent_req
*subreq
);
1127 struct tevent_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
1128 struct event_context
*ev
,
1129 struct cli_state
*cli
,
1131 uint32_t CreatFlags
,
1132 uint32_t DesiredAccess
,
1133 uint32_t FileAttributes
,
1134 uint32_t ShareAccess
,
1135 uint32_t CreateDisposition
,
1136 uint32_t CreateOptions
,
1137 uint8_t SecurityFlags
)
1139 struct tevent_req
*req
, *subreq
;
1140 struct cli_ntcreate_state
*state
;
1143 size_t converted_len
;
1145 req
= tevent_req_create(mem_ctx
, &state
, struct cli_ntcreate_state
);
1151 SCVAL(vwv
+0, 0, 0xFF);
1156 if (cli
->use_oplocks
) {
1157 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
1159 SIVAL(vwv
+3, 1, CreatFlags
);
1160 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
1161 SIVAL(vwv
+7, 1, DesiredAccess
);
1162 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
1163 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
1164 SIVAL(vwv
+13, 1, FileAttributes
);
1165 SIVAL(vwv
+15, 1, ShareAccess
);
1166 SIVAL(vwv
+17, 1, CreateDisposition
);
1167 SIVAL(vwv
+19, 1, CreateOptions
);
1168 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
1169 SCVAL(vwv
+23, 1, SecurityFlags
);
1171 bytes
= talloc_array(state
, uint8_t, 0);
1172 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
),
1173 fname
, strlen(fname
)+1,
1176 /* sigh. this copes with broken netapp filer behaviour */
1177 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "", 1, NULL
);
1179 if (tevent_req_nomem(bytes
, req
)) {
1180 return tevent_req_post(req
, ev
);
1183 SIVAL(vwv
+2, 1, converted_len
);
1185 subreq
= cli_smb_send(state
, ev
, cli
, SMBntcreateX
, 0, 24, vwv
,
1186 talloc_get_size(bytes
), bytes
);
1187 if (tevent_req_nomem(subreq
, req
)) {
1188 return tevent_req_post(req
, ev
);
1190 tevent_req_set_callback(subreq
, cli_ntcreate_done
, req
);
1194 static void cli_ntcreate_done(struct tevent_req
*subreq
)
1196 struct tevent_req
*req
= tevent_req_callback_data(
1197 subreq
, struct tevent_req
);
1198 struct cli_ntcreate_state
*state
= tevent_req_data(
1199 req
, struct cli_ntcreate_state
);
1206 status
= cli_smb_recv(subreq
, 3, &wct
, &vwv
, &num_bytes
, &bytes
);
1207 if (!NT_STATUS_IS_OK(status
)) {
1208 TALLOC_FREE(subreq
);
1209 tevent_req_nterror(req
, status
);
1212 state
->fnum
= SVAL(vwv
+2, 1);
1213 tevent_req_done(req
);
1216 NTSTATUS
cli_ntcreate_recv(struct tevent_req
*req
, uint16_t *pfnum
)
1218 struct cli_ntcreate_state
*state
= tevent_req_data(
1219 req
, struct cli_ntcreate_state
);
1222 if (tevent_req_is_nterror(req
, &status
)) {
1225 *pfnum
= state
->fnum
;
1226 return NT_STATUS_OK
;
1229 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
1231 uint32_t CreatFlags
,
1232 uint32_t DesiredAccess
,
1233 uint32_t FileAttributes
,
1234 uint32_t ShareAccess
,
1235 uint32_t CreateDisposition
,
1236 uint32_t CreateOptions
,
1237 uint8_t SecurityFlags
,
1240 TALLOC_CTX
*frame
= talloc_stackframe();
1241 struct event_context
*ev
;
1242 struct tevent_req
*req
;
1243 NTSTATUS status
= NT_STATUS_OK
;
1245 if (cli_has_async_calls(cli
)) {
1247 * Can't use sync call while an async call is in flight
1249 status
= NT_STATUS_INVALID_PARAMETER
;
1253 ev
= event_context_init(frame
);
1255 status
= NT_STATUS_NO_MEMORY
;
1259 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
1260 DesiredAccess
, FileAttributes
, ShareAccess
,
1261 CreateDisposition
, CreateOptions
,
1264 status
= NT_STATUS_NO_MEMORY
;
1268 if (!tevent_req_poll(req
, ev
)) {
1269 status
= map_nt_error_from_unix(errno
);
1273 status
= cli_ntcreate_recv(req
, pfid
);
1276 if (!NT_STATUS_IS_OK(status
)) {
1277 cli_set_error(cli
, status
);
1282 /****************************************************************************
1284 ****************************************************************************/
1286 int cli_nt_create(struct cli_state
*cli
, const char *fname
, uint32_t DesiredAccess
)
1288 return cli_nt_create_full(cli
, fname
, 0, DesiredAccess
, 0,
1289 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0);
1292 uint8_t *smb_bytes_push_str(uint8_t *buf
, bool ucs2
,
1293 const char *str
, size_t str_len
,
1294 size_t *pconverted_size
)
1298 size_t converted_size
;
1304 buflen
= talloc_get_size(buf
);
1306 * We're pushing into an SMB buffer, align odd
1308 if (ucs2
&& (buflen
% 2 == 0)) {
1309 buf
= TALLOC_REALLOC_ARRAY(NULL
, buf
, uint8_t, buflen
+ 1);
1317 if (!convert_string_talloc(talloc_tos(), CH_UNIX
,
1318 ucs2
? CH_UTF16LE
: CH_DOS
,
1319 str
, str_len
, &converted
,
1320 &converted_size
, true)) {
1324 buf
= TALLOC_REALLOC_ARRAY(NULL
, buf
, uint8_t,
1325 buflen
+ converted_size
);
1327 TALLOC_FREE(converted
);
1331 memcpy(buf
+ buflen
, converted
, converted_size
);
1333 TALLOC_FREE(converted
);
1335 if (pconverted_size
) {
1336 *pconverted_size
= converted_size
;
1342 /****************************************************************************
1344 WARNING: if you open with O_WRONLY then getattrE won't work!
1345 ****************************************************************************/
1347 struct cli_open_state
{
1353 static void cli_open_done(struct tevent_req
*subreq
);
1355 struct tevent_req
*cli_open_create(TALLOC_CTX
*mem_ctx
,
1356 struct event_context
*ev
,
1357 struct cli_state
*cli
, const char *fname
,
1358 int flags
, int share_mode
,
1359 struct tevent_req
**psmbreq
)
1361 struct tevent_req
*req
, *subreq
;
1362 struct cli_open_state
*state
;
1364 unsigned accessmode
;
1365 uint8_t additional_flags
;
1368 req
= tevent_req_create(mem_ctx
, &state
, struct cli_open_state
);
1374 if (flags
& O_CREAT
) {
1377 if (!(flags
& O_EXCL
)) {
1378 if (flags
& O_TRUNC
)
1384 accessmode
= (share_mode
<<4);
1386 if ((flags
& O_ACCMODE
) == O_RDWR
) {
1388 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
1393 if ((flags
& O_SYNC
) == O_SYNC
) {
1394 accessmode
|= (1<<14);
1398 if (share_mode
== DENY_FCB
) {
1402 SCVAL(state
->vwv
+ 0, 0, 0xFF);
1403 SCVAL(state
->vwv
+ 0, 1, 0);
1404 SSVAL(state
->vwv
+ 1, 0, 0);
1405 SSVAL(state
->vwv
+ 2, 0, 0); /* no additional info */
1406 SSVAL(state
->vwv
+ 3, 0, accessmode
);
1407 SSVAL(state
->vwv
+ 4, 0, aSYSTEM
| aHIDDEN
);
1408 SSVAL(state
->vwv
+ 5, 0, 0);
1409 SIVAL(state
->vwv
+ 6, 0, 0);
1410 SSVAL(state
->vwv
+ 8, 0, openfn
);
1411 SIVAL(state
->vwv
+ 9, 0, 0);
1412 SIVAL(state
->vwv
+ 11, 0, 0);
1413 SIVAL(state
->vwv
+ 13, 0, 0);
1415 additional_flags
= 0;
1417 if (cli
->use_oplocks
) {
1418 /* if using oplocks then ask for a batch oplock via
1419 core and extended methods */
1421 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
1422 SSVAL(state
->vwv
+2, 0, SVAL(state
->vwv
+2, 0) | 6);
1425 bytes
= talloc_array(state
, uint8_t, 0);
1426 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
1427 strlen(fname
)+1, NULL
);
1429 if (tevent_req_nomem(bytes
, req
)) {
1430 return tevent_req_post(req
, ev
);
1433 state
->bytes
.iov_base
= bytes
;
1434 state
->bytes
.iov_len
= talloc_get_size(bytes
);
1436 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBopenX
, additional_flags
,
1437 15, state
->vwv
, 1, &state
->bytes
);
1438 if (subreq
== NULL
) {
1442 tevent_req_set_callback(subreq
, cli_open_done
, req
);
1447 struct tevent_req
*cli_open_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
1448 struct cli_state
*cli
, const char *fname
,
1449 int flags
, int share_mode
)
1451 struct tevent_req
*req
, *subreq
;
1453 req
= cli_open_create(mem_ctx
, ev
, cli
, fname
, flags
, share_mode
,
1455 if ((req
== NULL
) || !cli_smb_req_send(subreq
)) {
1462 static void cli_open_done(struct tevent_req
*subreq
)
1464 struct tevent_req
*req
= tevent_req_callback_data(
1465 subreq
, struct tevent_req
);
1466 struct cli_open_state
*state
= tevent_req_data(
1467 req
, struct cli_open_state
);
1472 status
= cli_smb_recv(subreq
, 3, &wct
, &vwv
, NULL
, NULL
);
1473 if (!NT_STATUS_IS_OK(status
)) {
1474 TALLOC_FREE(subreq
);
1475 tevent_req_nterror(req
, status
);
1478 state
->fnum
= SVAL(vwv
+2, 0);
1479 tevent_req_done(req
);
1482 NTSTATUS
cli_open_recv(struct tevent_req
*req
, int *fnum
)
1484 struct cli_open_state
*state
= tevent_req_data(
1485 req
, struct cli_open_state
);
1488 if (tevent_req_is_nterror(req
, &status
)) {
1491 *fnum
= state
->fnum
;
1492 return NT_STATUS_OK
;
1495 int cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
1498 TALLOC_CTX
*frame
= talloc_stackframe();
1499 struct event_context
*ev
;
1500 struct tevent_req
*req
;
1501 NTSTATUS status
= NT_STATUS_OK
;
1504 if (cli_has_async_calls(cli
)) {
1506 * Can't use sync call while an async call is in flight
1508 status
= NT_STATUS_INVALID_PARAMETER
;
1512 ev
= event_context_init(frame
);
1514 status
= NT_STATUS_NO_MEMORY
;
1518 req
= cli_open_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
1520 status
= NT_STATUS_NO_MEMORY
;
1524 if (!tevent_req_poll(req
, ev
)) {
1525 status
= map_nt_error_from_unix(errno
);
1529 cli_open_recv(req
, &result
);
1532 if (!NT_STATUS_IS_OK(status
)) {
1533 cli_set_error(cli
, status
);
1538 /****************************************************************************
1540 ****************************************************************************/
1542 struct cli_close_state
{
1546 static void cli_close_done(struct tevent_req
*subreq
);
1548 struct tevent_req
*cli_close_create(TALLOC_CTX
*mem_ctx
,
1549 struct event_context
*ev
,
1550 struct cli_state
*cli
, int fnum
,
1551 struct tevent_req
**psubreq
)
1553 struct tevent_req
*req
, *subreq
;
1554 struct cli_close_state
*state
;
1556 req
= tevent_req_create(mem_ctx
, &state
, struct cli_close_state
);
1560 SSVAL(state
->vwv
+0, 0, fnum
);
1561 SIVALS(state
->vwv
+1, 0, -1);
1563 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBclose
, 0, 3, state
->vwv
,
1565 if (subreq
== NULL
) {
1569 tevent_req_set_callback(subreq
, cli_close_done
, req
);
1574 struct tevent_req
*cli_close_send(TALLOC_CTX
*mem_ctx
,
1575 struct event_context
*ev
,
1576 struct cli_state
*cli
, int fnum
)
1578 struct tevent_req
*req
, *subreq
;
1580 req
= cli_close_create(mem_ctx
, ev
, cli
, fnum
, &subreq
);
1581 if ((req
== NULL
) || !cli_smb_req_send(subreq
)) {
1588 static void cli_close_done(struct tevent_req
*subreq
)
1590 struct tevent_req
*req
= tevent_req_callback_data(
1591 subreq
, struct tevent_req
);
1594 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
1595 TALLOC_FREE(subreq
);
1596 if (!NT_STATUS_IS_OK(status
)) {
1597 tevent_req_nterror(req
, status
);
1600 tevent_req_done(req
);
1603 NTSTATUS
cli_close_recv(struct tevent_req
*req
)
1605 return tevent_req_simple_recv_ntstatus(req
);
1608 bool cli_close(struct cli_state
*cli
, int fnum
)
1610 TALLOC_CTX
*frame
= talloc_stackframe();
1611 struct event_context
*ev
;
1612 struct tevent_req
*req
;
1613 NTSTATUS status
= NT_STATUS_OK
;
1614 bool result
= false;
1616 if (cli_has_async_calls(cli
)) {
1618 * Can't use sync call while an async call is in flight
1620 status
= NT_STATUS_INVALID_PARAMETER
;
1624 ev
= event_context_init(frame
);
1626 status
= NT_STATUS_NO_MEMORY
;
1630 req
= cli_close_send(frame
, ev
, cli
, fnum
);
1632 status
= NT_STATUS_NO_MEMORY
;
1636 if (!tevent_req_poll(req
, ev
)) {
1637 status
= map_nt_error_from_unix(errno
);
1641 result
= NT_STATUS_IS_OK(cli_close_recv(req
));
1644 if (!NT_STATUS_IS_OK(status
)) {
1645 cli_set_error(cli
, status
);
1650 /****************************************************************************
1651 Truncate a file to a specified size
1652 ****************************************************************************/
1654 bool cli_ftruncate(struct cli_state
*cli
, int fnum
, uint64_t size
)
1656 unsigned int param_len
= 6;
1657 unsigned int data_len
= 8;
1658 uint16_t setup
= TRANSACT2_SETFILEINFO
;
1660 unsigned char data
[8];
1661 char *rparam
=NULL
, *rdata
=NULL
;
1662 int saved_timeout
= cli
->timeout
;
1664 SSVAL(param
,0,fnum
);
1665 SSVAL(param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
1668 SBVAL(data
, 0, size
);
1670 if (!cli_send_trans(cli
, SMBtrans2
,
1672 -1, 0, /* fid, flags */
1673 &setup
, 1, 0, /* setup, length, max */
1674 param
, param_len
, 2, /* param, length, max */
1675 (char *)&data
, data_len
,/* data, length, ... */
1676 cli
->max_xmit
)) { /* ... max */
1677 cli
->timeout
= saved_timeout
;
1681 if (!cli_receive_trans(cli
, SMBtrans2
,
1682 &rparam
, ¶m_len
,
1683 &rdata
, &data_len
)) {
1684 cli
->timeout
= saved_timeout
;
1690 cli
->timeout
= saved_timeout
;
1699 /****************************************************************************
1700 send a lock with a specified locktype
1701 this is used for testing LOCKING_ANDX_CANCEL_LOCK
1702 ****************************************************************************/
1704 NTSTATUS
cli_locktype(struct cli_state
*cli
, int fnum
,
1705 uint32_t offset
, uint32_t len
,
1706 int timeout
, unsigned char locktype
)
1709 int saved_timeout
= cli
->timeout
;
1711 memset(cli
->outbuf
,'\0',smb_size
);
1712 memset(cli
->inbuf
,'\0', smb_size
);
1714 cli_set_message(cli
->outbuf
,8,0,True
);
1716 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1717 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1718 cli_setup_packet(cli
);
1720 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1721 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1722 SCVAL(cli
->outbuf
,smb_vwv3
,locktype
);
1723 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
1724 SSVAL(cli
->outbuf
,smb_vwv6
,0);
1725 SSVAL(cli
->outbuf
,smb_vwv7
,1);
1727 p
= smb_buf(cli
->outbuf
);
1728 SSVAL(p
, 0, cli
->pid
);
1729 SIVAL(p
, 2, offset
);
1734 cli_setup_bcc(cli
, p
);
1739 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 2*1000);
1742 if (!cli_receive_smb(cli
)) {
1743 cli
->timeout
= saved_timeout
;
1744 return NT_STATUS_UNSUCCESSFUL
;
1747 cli
->timeout
= saved_timeout
;
1749 return cli_nt_error(cli
);
1752 /****************************************************************************
1754 note that timeout is in units of 2 milliseconds
1755 ****************************************************************************/
1757 bool cli_lock(struct cli_state
*cli
, int fnum
,
1758 uint32_t offset
, uint32_t len
, int timeout
, enum brl_type lock_type
)
1761 int saved_timeout
= cli
->timeout
;
1763 memset(cli
->outbuf
,'\0',smb_size
);
1764 memset(cli
->inbuf
,'\0', smb_size
);
1766 cli_set_message(cli
->outbuf
,8,0,True
);
1768 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1769 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1770 cli_setup_packet(cli
);
1772 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1773 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1774 SCVAL(cli
->outbuf
,smb_vwv3
,(lock_type
== READ_LOCK
? 1 : 0));
1775 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
1776 SSVAL(cli
->outbuf
,smb_vwv6
,0);
1777 SSVAL(cli
->outbuf
,smb_vwv7
,1);
1779 p
= smb_buf(cli
->outbuf
);
1780 SSVAL(p
, 0, cli
->pid
);
1781 SIVAL(p
, 2, offset
);
1786 cli_setup_bcc(cli
, p
);
1791 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
*2 + 5*1000);
1794 if (!cli_receive_smb(cli
)) {
1795 cli
->timeout
= saved_timeout
;
1799 cli
->timeout
= saved_timeout
;
1801 if (cli_is_error(cli
)) {
1808 /****************************************************************************
1810 ****************************************************************************/
1812 bool cli_unlock(struct cli_state
*cli
, int fnum
, uint32_t offset
, uint32_t len
)
1816 memset(cli
->outbuf
,'\0',smb_size
);
1817 memset(cli
->inbuf
,'\0',smb_size
);
1819 cli_set_message(cli
->outbuf
,8,0,True
);
1821 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1822 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1823 cli_setup_packet(cli
);
1825 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1826 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1827 SCVAL(cli
->outbuf
,smb_vwv3
,0);
1828 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
1829 SSVAL(cli
->outbuf
,smb_vwv6
,1);
1830 SSVAL(cli
->outbuf
,smb_vwv7
,0);
1832 p
= smb_buf(cli
->outbuf
);
1833 SSVAL(p
, 0, cli
->pid
);
1834 SIVAL(p
, 2, offset
);
1837 cli_setup_bcc(cli
, p
);
1839 if (!cli_receive_smb(cli
)) {
1843 if (cli_is_error(cli
)) {
1850 /****************************************************************************
1851 Lock a file with 64 bit offsets.
1852 ****************************************************************************/
1854 bool cli_lock64(struct cli_state
*cli
, int fnum
,
1855 uint64_t offset
, uint64_t len
, int timeout
, enum brl_type lock_type
)
1858 int saved_timeout
= cli
->timeout
;
1861 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
1862 return cli_lock(cli
, fnum
, offset
, len
, timeout
, lock_type
);
1865 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
1866 ltype
|= LOCKING_ANDX_LARGE_FILES
;
1868 memset(cli
->outbuf
,'\0',smb_size
);
1869 memset(cli
->inbuf
,'\0', smb_size
);
1871 cli_set_message(cli
->outbuf
,8,0,True
);
1873 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1874 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1875 cli_setup_packet(cli
);
1877 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1878 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1879 SCVAL(cli
->outbuf
,smb_vwv3
,ltype
);
1880 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
1881 SSVAL(cli
->outbuf
,smb_vwv6
,0);
1882 SSVAL(cli
->outbuf
,smb_vwv7
,1);
1884 p
= smb_buf(cli
->outbuf
);
1885 SIVAL(p
, 0, cli
->pid
);
1886 SOFF_T_R(p
, 4, offset
);
1887 SOFF_T_R(p
, 12, len
);
1890 cli_setup_bcc(cli
, p
);
1894 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 5*1000);
1897 if (!cli_receive_smb(cli
)) {
1898 cli
->timeout
= saved_timeout
;
1902 cli
->timeout
= saved_timeout
;
1904 if (cli_is_error(cli
)) {
1911 /****************************************************************************
1912 Unlock a file with 64 bit offsets.
1913 ****************************************************************************/
1915 bool cli_unlock64(struct cli_state
*cli
, int fnum
, uint64_t offset
, uint64_t len
)
1919 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
1920 return cli_unlock(cli
, fnum
, offset
, len
);
1923 memset(cli
->outbuf
,'\0',smb_size
);
1924 memset(cli
->inbuf
,'\0',smb_size
);
1926 cli_set_message(cli
->outbuf
,8,0,True
);
1928 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1929 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1930 cli_setup_packet(cli
);
1932 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1933 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1934 SCVAL(cli
->outbuf
,smb_vwv3
,LOCKING_ANDX_LARGE_FILES
);
1935 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
1936 SSVAL(cli
->outbuf
,smb_vwv6
,1);
1937 SSVAL(cli
->outbuf
,smb_vwv7
,0);
1939 p
= smb_buf(cli
->outbuf
);
1940 SIVAL(p
, 0, cli
->pid
);
1941 SOFF_T_R(p
, 4, offset
);
1942 SOFF_T_R(p
, 12, len
);
1944 cli_setup_bcc(cli
, p
);
1946 if (!cli_receive_smb(cli
)) {
1950 if (cli_is_error(cli
)) {
1957 /****************************************************************************
1958 Get/unlock a POSIX lock on a file - internal function.
1959 ****************************************************************************/
1961 static bool cli_posix_lock_internal(struct cli_state
*cli
, int fnum
,
1962 uint64_t offset
, uint64_t len
, bool wait_lock
, enum brl_type lock_type
)
1964 unsigned int param_len
= 4;
1965 unsigned int data_len
= POSIX_LOCK_DATA_SIZE
;
1966 uint16_t setup
= TRANSACT2_SETFILEINFO
;
1968 unsigned char data
[POSIX_LOCK_DATA_SIZE
];
1969 char *rparam
=NULL
, *rdata
=NULL
;
1970 int saved_timeout
= cli
->timeout
;
1972 SSVAL(param
,0,fnum
);
1973 SSVAL(param
,2,SMB_SET_POSIX_LOCK
);
1975 switch (lock_type
) {
1977 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_READ
);
1980 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_WRITE
);
1983 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_UNLOCK
);
1990 SSVAL(data
, POSIX_LOCK_FLAGS_OFFSET
, POSIX_LOCK_FLAG_WAIT
);
1991 cli
->timeout
= 0x7FFFFFFF;
1993 SSVAL(data
, POSIX_LOCK_FLAGS_OFFSET
, POSIX_LOCK_FLAG_NOWAIT
);
1996 SIVAL(data
, POSIX_LOCK_PID_OFFSET
, cli
->pid
);
1997 SOFF_T(data
, POSIX_LOCK_START_OFFSET
, offset
);
1998 SOFF_T(data
, POSIX_LOCK_LEN_OFFSET
, len
);
2000 if (!cli_send_trans(cli
, SMBtrans2
,
2002 -1, 0, /* fid, flags */
2003 &setup
, 1, 0, /* setup, length, max */
2004 param
, param_len
, 2, /* param, length, max */
2005 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
2007 cli
->timeout
= saved_timeout
;
2011 if (!cli_receive_trans(cli
, SMBtrans2
,
2012 &rparam
, ¶m_len
,
2013 &rdata
, &data_len
)) {
2014 cli
->timeout
= saved_timeout
;
2020 cli
->timeout
= saved_timeout
;
2028 /****************************************************************************
2030 ****************************************************************************/
2032 bool cli_posix_lock(struct cli_state
*cli
, int fnum
,
2033 uint64_t offset
, uint64_t len
,
2034 bool wait_lock
, enum brl_type lock_type
)
2036 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
2039 return cli_posix_lock_internal(cli
, fnum
, offset
, len
, wait_lock
, lock_type
);
2042 /****************************************************************************
2043 POSIX Unlock a file.
2044 ****************************************************************************/
2046 bool cli_posix_unlock(struct cli_state
*cli
, int fnum
, uint64_t offset
, uint64_t len
)
2048 return cli_posix_lock_internal(cli
, fnum
, offset
, len
, False
, UNLOCK_LOCK
);
2051 /****************************************************************************
2052 POSIX Get any lock covering a file.
2053 ****************************************************************************/
2055 bool cli_posix_getlock(struct cli_state
*cli
, int fnum
, uint64_t *poffset
, uint64_t *plen
)
2060 /****************************************************************************
2061 Do a SMBgetattrE call.
2062 ****************************************************************************/
2064 bool cli_getattrE(struct cli_state
*cli
, int fd
,
2065 uint16_t *attr
, SMB_OFF_T
*size
,
2066 time_t *change_time
,
2067 time_t *access_time
,
2070 memset(cli
->outbuf
,'\0',smb_size
);
2071 memset(cli
->inbuf
,'\0',smb_size
);
2073 cli_set_message(cli
->outbuf
,1,0,True
);
2075 SCVAL(cli
->outbuf
,smb_com
,SMBgetattrE
);
2076 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
2077 cli_setup_packet(cli
);
2079 SSVAL(cli
->outbuf
,smb_vwv0
,fd
);
2082 if (!cli_receive_smb(cli
)) {
2086 if (cli_is_error(cli
)) {
2091 *size
= IVAL(cli
->inbuf
, smb_vwv6
);
2095 *attr
= SVAL(cli
->inbuf
,smb_vwv10
);
2099 *change_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv0
);
2103 *access_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv2
);
2107 *write_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv4
);
2113 /****************************************************************************
2115 ****************************************************************************/
2117 bool cli_getatr(struct cli_state
*cli
, const char *fname
,
2118 uint16_t *attr
, SMB_OFF_T
*size
, time_t *write_time
)
2122 memset(cli
->outbuf
,'\0',smb_size
);
2123 memset(cli
->inbuf
,'\0',smb_size
);
2125 cli_set_message(cli
->outbuf
,0,0,True
);
2127 SCVAL(cli
->outbuf
,smb_com
,SMBgetatr
);
2128 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
2129 cli_setup_packet(cli
);
2131 p
= smb_buf(cli
->outbuf
);
2133 p
+= clistr_push(cli
, p
, fname
,
2134 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
2136 cli_setup_bcc(cli
, p
);
2139 if (!cli_receive_smb(cli
)) {
2143 if (cli_is_error(cli
)) {
2148 *size
= IVAL(cli
->inbuf
, smb_vwv3
);
2152 *write_time
= cli_make_unix_date3(cli
, cli
->inbuf
+smb_vwv1
);
2156 *attr
= SVAL(cli
->inbuf
,smb_vwv0
);
2162 /****************************************************************************
2163 Do a SMBsetattrE call.
2164 ****************************************************************************/
2166 bool cli_setattrE(struct cli_state
*cli
, int fd
,
2174 memset(cli
->outbuf
,'\0',smb_size
);
2175 memset(cli
->inbuf
,'\0',smb_size
);
2177 cli_set_message(cli
->outbuf
,7,0,True
);
2179 SCVAL(cli
->outbuf
,smb_com
,SMBsetattrE
);
2180 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
2181 cli_setup_packet(cli
);
2183 SSVAL(cli
->outbuf
,smb_vwv0
, fd
);
2184 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv1
, change_time
);
2185 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv3
, access_time
);
2186 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv5
, write_time
);
2188 p
= smb_buf(cli
->outbuf
);
2191 cli_setup_bcc(cli
, p
);
2194 if (!cli_receive_smb(cli
)) {
2198 if (cli_is_error(cli
)) {
2205 /****************************************************************************
2206 Do a SMBsetatr call.
2207 ****************************************************************************/
2209 bool cli_setatr(struct cli_state
*cli
, const char *fname
, uint16_t attr
, time_t t
)
2213 memset(cli
->outbuf
,'\0',smb_size
);
2214 memset(cli
->inbuf
,'\0',smb_size
);
2216 cli_set_message(cli
->outbuf
,8,0,True
);
2218 SCVAL(cli
->outbuf
,smb_com
,SMBsetatr
);
2219 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
2220 cli_setup_packet(cli
);
2222 SSVAL(cli
->outbuf
,smb_vwv0
, attr
);
2223 cli_put_dos_date3(cli
, cli
->outbuf
,smb_vwv1
, t
);
2225 p
= smb_buf(cli
->outbuf
);
2227 p
+= clistr_push(cli
, p
, fname
,
2228 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
2231 cli_setup_bcc(cli
, p
);
2234 if (!cli_receive_smb(cli
)) {
2238 if (cli_is_error(cli
)) {
2245 /****************************************************************************
2246 Check for existance of a dir.
2247 ****************************************************************************/
2249 static void cli_chkpath_done(struct tevent_req
*subreq
);
2251 struct cli_chkpath_state
{
2255 struct tevent_req
*cli_chkpath_send(TALLOC_CTX
*mem_ctx
,
2256 struct event_context
*ev
,
2257 struct cli_state
*cli
,
2260 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2261 struct cli_chkpath_state
*state
= NULL
;
2262 uint8_t additional_flags
= 0;
2263 uint8_t *bytes
= NULL
;
2265 req
= tevent_req_create(mem_ctx
, &state
, struct cli_chkpath_state
);
2270 bytes
= talloc_array(state
, uint8_t, 1);
2271 if (tevent_req_nomem(bytes
, req
)) {
2272 return tevent_req_post(req
, ev
);
2275 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
2276 strlen(fname
)+1, NULL
);
2278 if (tevent_req_nomem(bytes
, req
)) {
2279 return tevent_req_post(req
, ev
);
2282 subreq
= cli_smb_send(state
, ev
, cli
, SMBcheckpath
, additional_flags
,
2283 0, NULL
, talloc_get_size(bytes
), bytes
);
2284 if (tevent_req_nomem(subreq
, req
)) {
2285 return tevent_req_post(req
, ev
);
2287 tevent_req_set_callback(subreq
, cli_chkpath_done
, req
);
2291 static void cli_chkpath_done(struct tevent_req
*subreq
)
2293 struct tevent_req
*req
= tevent_req_callback_data(
2294 subreq
, struct tevent_req
);
2297 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, NULL
, NULL
);
2298 TALLOC_FREE(subreq
);
2299 if (!NT_STATUS_IS_OK(status
)) {
2300 tevent_req_nterror(req
, status
);
2303 tevent_req_done(req
);
2306 NTSTATUS
cli_chkpath_recv(struct tevent_req
*req
)
2308 return tevent_req_simple_recv_ntstatus(req
);
2311 NTSTATUS
cli_chkpath(struct cli_state
*cli
, const char *path
)
2313 TALLOC_CTX
*frame
= talloc_stackframe();
2314 struct event_context
*ev
= NULL
;
2315 struct tevent_req
*req
= NULL
;
2317 NTSTATUS status
= NT_STATUS_OK
;
2319 if (cli_has_async_calls(cli
)) {
2321 * Can't use sync call while an async call is in flight
2323 status
= NT_STATUS_INVALID_PARAMETER
;
2327 path2
= talloc_strdup(frame
, path
);
2329 status
= NT_STATUS_NO_MEMORY
;
2332 trim_char(path2
,'\0','\\');
2334 path2
= talloc_strdup(frame
, "\\");
2336 status
= NT_STATUS_NO_MEMORY
;
2341 ev
= event_context_init(frame
);
2343 status
= NT_STATUS_NO_MEMORY
;
2347 req
= cli_chkpath_send(frame
, ev
, cli
, path2
);
2349 status
= NT_STATUS_NO_MEMORY
;
2353 if (!tevent_req_poll(req
, ev
)) {
2354 status
= map_nt_error_from_unix(errno
);
2358 status
= cli_chkpath_recv(req
);
2362 if (!NT_STATUS_IS_OK(status
)) {
2363 cli_set_error(cli
, status
);
2368 /****************************************************************************
2370 ****************************************************************************/
2372 static void cli_dskattr_done(struct tevent_req
*subreq
);
2374 struct cli_dskattr_state
{
2380 struct tevent_req
*cli_dskattr_send(TALLOC_CTX
*mem_ctx
,
2381 struct event_context
*ev
,
2382 struct cli_state
*cli
)
2384 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
2385 struct cli_dskattr_state
*state
= NULL
;
2386 uint8_t additional_flags
= 0;
2388 req
= tevent_req_create(mem_ctx
, &state
, struct cli_dskattr_state
);
2393 subreq
= cli_smb_send(state
, ev
, cli
, SMBdskattr
, additional_flags
,
2395 if (tevent_req_nomem(subreq
, req
)) {
2396 return tevent_req_post(req
, ev
);
2398 tevent_req_set_callback(subreq
, cli_dskattr_done
, req
);
2402 static void cli_dskattr_done(struct tevent_req
*subreq
)
2404 struct tevent_req
*req
= tevent_req_callback_data(
2405 subreq
, struct tevent_req
);
2406 struct cli_dskattr_state
*state
= tevent_req_data(
2407 req
, struct cli_dskattr_state
);
2409 uint16_t *vwv
= NULL
;
2412 status
= cli_smb_recv(subreq
, 4, &wct
, &vwv
, NULL
, NULL
);
2413 if (!NT_STATUS_IS_OK(status
)) {
2414 tevent_req_nterror(req
, status
);
2417 state
->bsize
= SVAL(vwv
+1, 0)*SVAL(vwv
+2,0);
2418 state
->total
= SVAL(vwv
+0, 0);
2419 state
->avail
= SVAL(vwv
+3, 0);
2420 TALLOC_FREE(subreq
);
2421 tevent_req_done(req
);
2424 NTSTATUS
cli_dskattr_recv(struct tevent_req
*req
, int *bsize
, int *total
, int *avail
)
2426 struct cli_dskattr_state
*state
= tevent_req_data(
2427 req
, struct cli_dskattr_state
);
2430 if (tevent_req_is_nterror(req
, &status
)) {
2433 *bsize
= state
->bsize
;
2434 *total
= state
->total
;
2435 *avail
= state
->avail
;
2436 return NT_STATUS_OK
;
2439 NTSTATUS
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
2441 TALLOC_CTX
*frame
= talloc_stackframe();
2442 struct event_context
*ev
= NULL
;
2443 struct tevent_req
*req
= NULL
;
2444 NTSTATUS status
= NT_STATUS_OK
;
2446 if (cli_has_async_calls(cli
)) {
2448 * Can't use sync call while an async call is in flight
2450 status
= NT_STATUS_INVALID_PARAMETER
;
2454 ev
= event_context_init(frame
);
2456 status
= NT_STATUS_NO_MEMORY
;
2460 req
= cli_dskattr_send(frame
, ev
, cli
);
2462 status
= NT_STATUS_NO_MEMORY
;
2466 if (!tevent_req_poll(req
, ev
)) {
2467 status
= map_nt_error_from_unix(errno
);
2471 status
= cli_dskattr_recv(req
, bsize
, total
, avail
);
2475 if (!NT_STATUS_IS_OK(status
)) {
2476 cli_set_error(cli
, status
);
2481 /****************************************************************************
2482 Create and open a temporary file.
2483 ****************************************************************************/
2485 int cli_ctemp(struct cli_state
*cli
, const char *path
, char **tmp_path
)
2490 memset(cli
->outbuf
,'\0',smb_size
);
2491 memset(cli
->inbuf
,'\0',smb_size
);
2493 cli_set_message(cli
->outbuf
,3,0,True
);
2495 SCVAL(cli
->outbuf
,smb_com
,SMBctemp
);
2496 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
2497 cli_setup_packet(cli
);
2499 SSVAL(cli
->outbuf
,smb_vwv0
,0);
2500 SIVALS(cli
->outbuf
,smb_vwv1
,-1);
2502 p
= smb_buf(cli
->outbuf
);
2504 p
+= clistr_push(cli
, p
, path
,
2505 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
2507 cli_setup_bcc(cli
, p
);
2510 if (!cli_receive_smb(cli
)) {
2514 if (cli_is_error(cli
)) {
2518 /* despite the spec, the result has a -1, followed by
2519 length, followed by name */
2520 p
= smb_buf(cli
->inbuf
);
2522 len
= smb_buflen(cli
->inbuf
) - 4;
2523 if (len
<= 0 || len
> PATH_MAX
) return -1;
2526 char *path2
= SMB_MALLOC_ARRAY(char, len
+1);
2530 clistr_pull(cli
->inbuf
, path2
, p
,
2531 len
+1, len
, STR_ASCII
);
2535 return SVAL(cli
->inbuf
,smb_vwv0
);
2539 send a raw ioctl - used by the torture code
2541 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, int fnum
, uint32_t code
, DATA_BLOB
*blob
)
2543 memset(cli
->outbuf
,'\0',smb_size
);
2544 memset(cli
->inbuf
,'\0',smb_size
);
2546 cli_set_message(cli
->outbuf
, 3, 0, True
);
2547 SCVAL(cli
->outbuf
,smb_com
,SMBioctl
);
2548 cli_setup_packet(cli
);
2550 SSVAL(cli
->outbuf
, smb_vwv0
, fnum
);
2551 SSVAL(cli
->outbuf
, smb_vwv1
, code
>>16);
2552 SSVAL(cli
->outbuf
, smb_vwv2
, (code
&0xFFFF));
2555 if (!cli_receive_smb(cli
)) {
2556 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2559 if (cli_is_error(cli
)) {
2560 return cli_nt_error(cli
);
2563 *blob
= data_blob_null
;
2565 return NT_STATUS_OK
;
2568 /*********************************************************
2569 Set an extended attribute utility fn.
2570 *********************************************************/
2572 static bool cli_set_ea(struct cli_state
*cli
, uint16_t setup
, char *param
, unsigned int param_len
,
2573 const char *ea_name
, const char *ea_val
, size_t ea_len
)
2575 unsigned int data_len
= 0;
2577 char *rparam
=NULL
, *rdata
=NULL
;
2579 size_t ea_namelen
= strlen(ea_name
);
2581 if (ea_namelen
== 0 && ea_len
== 0) {
2583 data
= (char *)SMB_MALLOC(data_len
);
2588 SIVAL(p
,0,data_len
);
2590 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
2591 data
= (char *)SMB_MALLOC(data_len
);
2596 SIVAL(p
,0,data_len
);
2598 SCVAL(p
, 0, 0); /* EA flags. */
2599 SCVAL(p
, 1, ea_namelen
);
2600 SSVAL(p
, 2, ea_len
);
2601 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
2602 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
2605 if (!cli_send_trans(cli
, SMBtrans2
,
2607 -1, 0, /* fid, flags */
2608 &setup
, 1, 0, /* setup, length, max */
2609 param
, param_len
, 2, /* param, length, max */
2610 data
, data_len
, cli
->max_xmit
/* data, length, max */
2616 if (!cli_receive_trans(cli
, SMBtrans2
,
2617 &rparam
, ¶m_len
,
2618 &rdata
, &data_len
)) {
2630 /*********************************************************
2631 Set an extended attribute on a pathname.
2632 *********************************************************/
2634 bool cli_set_ea_path(struct cli_state
*cli
, const char *path
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
2636 uint16_t setup
= TRANSACT2_SETPATHINFO
;
2637 unsigned int param_len
= 0;
2639 size_t srclen
= 2*(strlen(path
)+1);
2643 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
2647 memset(param
, '\0', 6);
2648 SSVAL(param
,0,SMB_INFO_SET_EA
);
2651 p
+= clistr_push(cli
, p
, path
, srclen
, STR_TERMINATE
);
2652 param_len
= PTR_DIFF(p
, param
);
2654 ret
= cli_set_ea(cli
, setup
, param
, param_len
, ea_name
, ea_val
, ea_len
);
2659 /*********************************************************
2660 Set an extended attribute on an fnum.
2661 *********************************************************/
2663 bool cli_set_ea_fnum(struct cli_state
*cli
, int fnum
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
2666 uint16_t setup
= TRANSACT2_SETFILEINFO
;
2668 memset(param
, 0, 6);
2669 SSVAL(param
,0,fnum
);
2670 SSVAL(param
,2,SMB_INFO_SET_EA
);
2672 return cli_set_ea(cli
, setup
, param
, 6, ea_name
, ea_val
, ea_len
);
2675 /*********************************************************
2676 Get an extended attribute list utility fn.
2677 *********************************************************/
2679 static bool cli_get_ea_list(struct cli_state
*cli
,
2680 uint16_t setup
, char *param
, unsigned int param_len
,
2683 struct ea_struct
**pea_list
)
2685 unsigned int data_len
= 0;
2686 unsigned int rparam_len
, rdata_len
;
2687 char *rparam
=NULL
, *rdata
=NULL
;
2692 struct ea_struct
*ea_list
;
2699 if (!cli_send_trans(cli
, SMBtrans2
,
2701 -1, 0, /* fid, flags */
2702 &setup
, 1, 0, /* setup, length, max */
2703 param
, param_len
, 10, /* param, length, max */
2704 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
2709 if (!cli_receive_trans(cli
, SMBtrans2
,
2710 &rparam
, &rparam_len
,
2711 &rdata
, &rdata_len
)) {
2715 if (!rdata
|| rdata_len
< 4) {
2719 ea_size
= (size_t)IVAL(rdata
,0);
2720 if (ea_size
> rdata_len
) {
2725 /* No EA's present. */
2733 /* Validate the EA list and count it. */
2734 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
2735 unsigned int ea_namelen
= CVAL(p
,1);
2736 unsigned int ea_valuelen
= SVAL(p
,2);
2737 if (ea_namelen
== 0) {
2740 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
2743 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
2744 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
2752 *pnum_eas
= num_eas
;
2754 /* Caller only wants number of EA's. */
2759 ea_list
= TALLOC_ARRAY(ctx
, struct ea_struct
, num_eas
);
2764 ea_size
= (size_t)IVAL(rdata
,0);
2767 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
2768 struct ea_struct
*ea
= &ea_list
[num_eas
];
2769 fstring unix_ea_name
;
2770 unsigned int ea_namelen
= CVAL(p
,1);
2771 unsigned int ea_valuelen
= SVAL(p
,2);
2773 ea
->flags
= CVAL(p
,0);
2774 unix_ea_name
[0] = '\0';
2775 pull_ascii_fstring(unix_ea_name
, p
+ 4);
2776 ea
->name
= talloc_strdup(ctx
, unix_ea_name
);
2777 /* Ensure the value is null terminated (in case it's a string). */
2778 ea
->value
= data_blob_talloc(ctx
, NULL
, ea_valuelen
+ 1);
2779 if (!ea
->value
.data
) {
2783 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
2785 ea
->value
.data
[ea_valuelen
] = 0;
2787 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
2790 *pea_list
= ea_list
;
2800 /*********************************************************
2801 Get an extended attribute list from a pathname.
2802 *********************************************************/
2804 bool cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
2807 struct ea_struct
**pea_list
)
2809 uint16_t setup
= TRANSACT2_QPATHINFO
;
2810 unsigned int param_len
= 0;
2813 size_t srclen
= 2*(strlen(path
)+1);
2816 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
2822 SSVAL(p
, 0, SMB_INFO_QUERY_ALL_EAS
);
2824 p
+= clistr_push(cli
, p
, path
, srclen
, STR_TERMINATE
);
2825 param_len
= PTR_DIFF(p
, param
);
2827 ret
= cli_get_ea_list(cli
, setup
, param
, param_len
, ctx
, pnum_eas
, pea_list
);
2832 /*********************************************************
2833 Get an extended attribute list from an fnum.
2834 *********************************************************/
2836 bool cli_get_ea_list_fnum(struct cli_state
*cli
, int fnum
,
2839 struct ea_struct
**pea_list
)
2841 uint16_t setup
= TRANSACT2_QFILEINFO
;
2844 memset(param
, 0, 6);
2845 SSVAL(param
,0,fnum
);
2846 SSVAL(param
,2,SMB_INFO_SET_EA
);
2848 return cli_get_ea_list(cli
, setup
, param
, 6, ctx
, pnum_eas
, pea_list
);
2851 /****************************************************************************
2852 Convert open "flags" arg to uint32_t on wire.
2853 ****************************************************************************/
2855 static uint32_t open_flags_to_wire(int flags
)
2857 int open_mode
= flags
& O_ACCMODE
;
2860 switch (open_mode
) {
2862 ret
|= SMB_O_WRONLY
;
2869 ret
|= SMB_O_RDONLY
;
2873 if (flags
& O_CREAT
) {
2876 if (flags
& O_EXCL
) {
2879 if (flags
& O_TRUNC
) {
2883 if (flags
& O_SYNC
) {
2887 if (flags
& O_APPEND
) {
2888 ret
|= SMB_O_APPEND
;
2890 #if defined(O_DIRECT)
2891 if (flags
& O_DIRECT
) {
2892 ret
|= SMB_O_DIRECT
;
2895 #if defined(O_DIRECTORY)
2896 if (flags
& O_DIRECTORY
) {
2897 ret
&= ~(SMB_O_RDONLY
|SMB_O_RDWR
|SMB_O_WRONLY
);
2898 ret
|= SMB_O_DIRECTORY
;
2904 /****************************************************************************
2905 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2906 ****************************************************************************/
2908 static int cli_posix_open_internal(struct cli_state
*cli
, const char *fname
, int flags
, mode_t mode
, bool is_dir
)
2910 unsigned int data_len
= 0;
2911 unsigned int param_len
= 0;
2912 uint16_t setup
= TRANSACT2_SETPATHINFO
;
2915 char *rparam
=NULL
, *rdata
=NULL
;
2918 uint32_t wire_flags
= open_flags_to_wire(flags
);
2919 size_t srclen
= 2*(strlen(fname
)+1);
2921 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
2925 memset(param
, '\0', 6);
2926 SSVAL(param
,0, SMB_POSIX_PATH_OPEN
);
2929 p
+= clistr_push(cli
, p
, fname
, srclen
, STR_TERMINATE
);
2930 param_len
= PTR_DIFF(p
, param
);
2933 wire_flags
&= ~(SMB_O_RDONLY
|SMB_O_RDWR
|SMB_O_WRONLY
);
2934 wire_flags
|= SMB_O_DIRECTORY
;
2938 SIVAL(p
,0,0); /* No oplock. */
2939 SIVAL(p
,4,wire_flags
);
2940 SIVAL(p
,8,unix_perms_to_wire(mode
));
2941 SIVAL(p
,12,0); /* Top bits of perms currently undefined. */
2942 SSVAL(p
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
2946 if (!cli_send_trans(cli
, SMBtrans2
,
2948 -1, 0, /* fid, flags */
2949 &setup
, 1, 0, /* setup, length, max */
2950 param
, param_len
, 2, /* param, length, max */
2951 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
2959 if (!cli_receive_trans(cli
, SMBtrans2
,
2960 &rparam
, ¶m_len
,
2961 &rdata
, &data_len
)) {
2965 fnum
= SVAL(rdata
,2);
2973 /****************************************************************************
2974 open - POSIX semantics.
2975 ****************************************************************************/
2977 int cli_posix_open(struct cli_state
*cli
, const char *fname
, int flags
, mode_t mode
)
2979 return cli_posix_open_internal(cli
, fname
, flags
, mode
, False
);
2982 /****************************************************************************
2983 mkdir - POSIX semantics.
2984 ****************************************************************************/
2986 int cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
2988 return (cli_posix_open_internal(cli
, fname
, O_CREAT
, mode
, True
) == -1) ? -1 : 0;
2991 /****************************************************************************
2992 unlink or rmdir - POSIX semantics.
2993 ****************************************************************************/
2995 struct unlink_state
{
2999 static void cli_posix_unlink_internal_done(struct tevent_req
*subreq
)
3001 struct tevent_req
*req
= tevent_req_callback_data(
3002 subreq
, struct tevent_req
);
3003 struct unlink_state
*state
= tevent_req_data(req
, struct unlink_state
);
3006 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
3007 TALLOC_FREE(subreq
);
3008 if (!NT_STATUS_IS_OK(status
)) {
3009 tevent_req_nterror(req
, status
);
3012 tevent_req_done(req
);
3015 static struct tevent_req
*cli_posix_unlink_internal_send(TALLOC_CTX
*mem_ctx
,
3016 struct event_context
*ev
,
3017 struct cli_state
*cli
,
3021 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
3022 struct unlink_state
*state
= NULL
;
3024 uint8_t *param
= NULL
;
3027 req
= tevent_req_create(mem_ctx
, &state
, struct unlink_state
);
3032 /* Setup setup word. */
3033 SSVAL(&setup
+0, 0, TRANSACT2_SETPATHINFO
);
3035 /* Setup param array. */
3036 param
= talloc_array(state
, uint8_t, 6);
3037 if (tevent_req_nomem(data
, req
)) {
3038 return tevent_req_post(req
, ev
);
3040 memset(param
, '\0', 6);
3041 SSVAL(param
, 0, SMB_POSIX_PATH_UNLINK
);
3043 param
= smb_bytes_push_str(param
, cli_ucs2(cli
), fname
,
3044 strlen(fname
)+1, NULL
);
3046 if (tevent_req_nomem(param
, req
)) {
3047 return tevent_req_post(req
, ev
);
3050 /* Setup data word. */
3051 SSVAL(data
, 0, is_dir
? SMB_POSIX_UNLINK_DIRECTORY_TARGET
:
3052 SMB_POSIX_UNLINK_FILE_TARGET
);
3054 subreq
= cli_trans_send(state
, /* mem ctx. */
3055 ev
, /* event ctx. */
3056 cli
, /* cli_state. */
3057 SMBtrans2
, /* cmd. */
3058 NULL
, /* pipe name. */
3062 &setup
, /* setup. */
3066 talloc_get_size(param
), /* num param. */
3072 if (tevent_req_nomem(subreq
, req
)) {
3073 return tevent_req_post(req
, ev
);
3075 tevent_req_set_callback(subreq
, cli_posix_unlink_internal_done
, req
);
3079 struct tevent_req
*cli_posix_unlink_send(TALLOC_CTX
*mem_ctx
,
3080 struct event_context
*ev
,
3081 struct cli_state
*cli
,
3084 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
, false);
3087 NTSTATUS
cli_posix_unlink_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
3091 if (tevent_req_is_nterror(req
, &status
)) {
3094 return NT_STATUS_OK
;
3097 /****************************************************************************
3098 unlink - POSIX semantics.
3099 ****************************************************************************/
3101 NTSTATUS
cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
3103 TALLOC_CTX
*frame
= talloc_stackframe();
3104 struct event_context
*ev
= NULL
;
3105 struct tevent_req
*req
= NULL
;
3106 NTSTATUS status
= NT_STATUS_OK
;
3108 if (cli_has_async_calls(cli
)) {
3110 * Can't use sync call while an async call is in flight
3112 status
= NT_STATUS_INVALID_PARAMETER
;
3116 ev
= event_context_init(frame
);
3118 status
= NT_STATUS_NO_MEMORY
;
3122 req
= cli_posix_unlink_send(frame
,
3127 status
= NT_STATUS_NO_MEMORY
;
3131 if (!tevent_req_poll(req
, ev
)) {
3132 status
= map_nt_error_from_unix(errno
);
3136 status
= cli_posix_unlink_recv(req
, frame
);
3140 if (!NT_STATUS_IS_OK(status
)) {
3141 cli_set_error(cli
, status
);
3146 /****************************************************************************
3147 rmdir - POSIX semantics.
3148 ****************************************************************************/
3150 struct tevent_req
*cli_posix_rmdir_send(TALLOC_CTX
*mem_ctx
,
3151 struct event_context
*ev
,
3152 struct cli_state
*cli
,
3155 return cli_posix_unlink_internal_send(mem_ctx
, ev
, cli
, fname
, true);
3158 NTSTATUS
cli_posix_rmdir_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
3162 if (tevent_req_is_nterror(req
, &status
)) {
3165 return NT_STATUS_OK
;
3168 NTSTATUS
cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
3170 TALLOC_CTX
*frame
= talloc_stackframe();
3171 struct event_context
*ev
= NULL
;
3172 struct tevent_req
*req
= NULL
;
3173 NTSTATUS status
= NT_STATUS_OK
;
3175 if (cli_has_async_calls(cli
)) {
3177 * Can't use sync call while an async call is in flight
3179 status
= NT_STATUS_INVALID_PARAMETER
;
3183 ev
= event_context_init(frame
);
3185 status
= NT_STATUS_NO_MEMORY
;
3189 req
= cli_posix_rmdir_send(frame
,
3194 status
= NT_STATUS_NO_MEMORY
;
3198 if (!tevent_req_poll(req
, ev
)) {
3199 status
= map_nt_error_from_unix(errno
);
3203 status
= cli_posix_rmdir_recv(req
, frame
);
3207 if (!NT_STATUS_IS_OK(status
)) {
3208 cli_set_error(cli
, status
);