2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan (metze) Metzmacher 2003
5 Copyright (C) Jeremy Allison 2007
6 Copyright (C) Andrew Bartlett 2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
27 #include "auth_generic.h"
28 #include "auth/gensec/gensec.h"
29 #include "../libcli/smb/smbXcli_base.h"
30 #include "auth/credentials/credentials.h"
31 #include "../librpc/gen_ndr/ndr_security.h"
33 /****************************************************************************
34 Get UNIX extensions version info.
35 ****************************************************************************/
37 struct cli_unix_extensions_version_state
{
38 struct cli_state
*cli
;
41 uint16_t major
, minor
;
42 uint32_t caplow
, caphigh
;
45 static void cli_unix_extensions_version_done(struct tevent_req
*subreq
);
47 struct tevent_req
*cli_unix_extensions_version_send(TALLOC_CTX
*mem_ctx
,
48 struct tevent_context
*ev
,
49 struct cli_state
*cli
)
51 struct tevent_req
*req
, *subreq
;
52 struct cli_unix_extensions_version_state
*state
;
54 req
= tevent_req_create(mem_ctx
, &state
,
55 struct cli_unix_extensions_version_state
);
60 SSVAL(state
->setup
, 0, TRANSACT2_QFSINFO
);
61 SSVAL(state
->param
, 0, SMB_QUERY_CIFS_UNIX_INFO
);
63 subreq
= cli_trans_send(state
, ev
, cli
, 0, SMBtrans2
,
68 if (tevent_req_nomem(subreq
, req
)) {
69 return tevent_req_post(req
, ev
);
71 tevent_req_set_callback(subreq
, cli_unix_extensions_version_done
, req
);
75 static void cli_unix_extensions_version_done(struct tevent_req
*subreq
)
77 struct tevent_req
*req
= tevent_req_callback_data(
78 subreq
, struct tevent_req
);
79 struct cli_unix_extensions_version_state
*state
= tevent_req_data(
80 req
, struct cli_unix_extensions_version_state
);
85 status
= cli_trans_recv(subreq
, state
, NULL
, NULL
, 0, NULL
,
86 NULL
, 0, NULL
, &data
, 12, &num_data
);
88 if (tevent_req_nterror(req
, status
)) {
92 state
->major
= SVAL(data
, 0);
93 state
->minor
= SVAL(data
, 2);
94 state
->caplow
= IVAL(data
, 4);
95 state
->caphigh
= IVAL(data
, 8);
100 NTSTATUS
cli_unix_extensions_version_recv(struct tevent_req
*req
,
101 uint16_t *pmajor
, uint16_t *pminor
,
105 struct cli_unix_extensions_version_state
*state
= tevent_req_data(
106 req
, struct cli_unix_extensions_version_state
);
109 if (tevent_req_is_nterror(req
, &status
)) {
112 *pmajor
= state
->major
;
113 *pminor
= state
->minor
;
114 *pcaplow
= state
->caplow
;
115 *pcaphigh
= state
->caphigh
;
116 state
->cli
->server_posix_capabilities
= *pcaplow
;
120 NTSTATUS
cli_unix_extensions_version(struct cli_state
*cli
, uint16_t *pmajor
,
121 uint16_t *pminor
, uint32_t *pcaplow
,
124 TALLOC_CTX
*frame
= talloc_stackframe();
125 struct tevent_context
*ev
;
126 struct tevent_req
*req
;
127 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
129 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
131 * Can't use sync call while an async call is in flight
133 status
= NT_STATUS_INVALID_PARAMETER
;
136 ev
= samba_tevent_context_init(frame
);
140 req
= cli_unix_extensions_version_send(frame
, ev
, cli
);
144 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
147 status
= cli_unix_extensions_version_recv(req
, pmajor
, pminor
, pcaplow
,
154 /****************************************************************************
155 Set UNIX extensions capabilities.
156 ****************************************************************************/
158 struct cli_set_unix_extensions_capabilities_state
{
159 struct cli_state
*cli
;
165 static void cli_set_unix_extensions_capabilities_done(
166 struct tevent_req
*subreq
);
168 struct tevent_req
*cli_set_unix_extensions_capabilities_send(
169 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
, struct cli_state
*cli
,
170 uint16_t major
, uint16_t minor
, uint32_t caplow
, uint32_t caphigh
)
172 struct tevent_req
*req
, *subreq
;
173 struct cli_set_unix_extensions_capabilities_state
*state
;
175 req
= tevent_req_create(
177 struct cli_set_unix_extensions_capabilities_state
);
183 SSVAL(state
->setup
+0, 0, TRANSACT2_SETFSINFO
);
185 SSVAL(state
->param
, 0, 0);
186 SSVAL(state
->param
, 2, SMB_SET_CIFS_UNIX_INFO
);
188 SSVAL(state
->data
, 0, major
);
189 SSVAL(state
->data
, 2, minor
);
190 SIVAL(state
->data
, 4, caplow
);
191 SIVAL(state
->data
, 8, caphigh
);
193 subreq
= cli_trans_send(state
, ev
, cli
, 0, SMBtrans2
,
197 state
->data
, 12, 560);
198 if (tevent_req_nomem(subreq
, req
)) {
199 return tevent_req_post(req
, ev
);
201 tevent_req_set_callback(
202 subreq
, cli_set_unix_extensions_capabilities_done
, req
);
206 static void cli_set_unix_extensions_capabilities_done(
207 struct tevent_req
*subreq
)
209 struct tevent_req
*req
= tevent_req_callback_data(
210 subreq
, struct tevent_req
);
211 struct cli_set_unix_extensions_capabilities_state
*state
= tevent_req_data(
212 req
, struct cli_set_unix_extensions_capabilities_state
);
214 NTSTATUS status
= cli_trans_recv(subreq
, NULL
, NULL
, NULL
, 0, NULL
,
215 NULL
, 0, NULL
, NULL
, 0, NULL
);
216 if (NT_STATUS_IS_OK(status
)) {
217 state
->cli
->requested_posix_capabilities
= IVAL(state
->data
, 4);
219 tevent_req_simple_finish_ntstatus(subreq
, status
);
222 NTSTATUS
cli_set_unix_extensions_capabilities_recv(struct tevent_req
*req
)
224 return tevent_req_simple_recv_ntstatus(req
);
227 NTSTATUS
cli_set_unix_extensions_capabilities(struct cli_state
*cli
,
228 uint16_t major
, uint16_t minor
,
229 uint32_t caplow
, uint32_t caphigh
)
231 struct tevent_context
*ev
;
232 struct tevent_req
*req
;
233 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
235 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
236 return NT_STATUS_INVALID_PARAMETER
;
238 ev
= samba_tevent_context_init(talloc_tos());
242 req
= cli_set_unix_extensions_capabilities_send(
243 ev
, ev
, cli
, major
, minor
, caplow
, caphigh
);
247 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
250 status
= cli_set_unix_extensions_capabilities_recv(req
);
256 struct cli_get_fs_attr_info_state
{
262 static void cli_get_fs_attr_info_done(struct tevent_req
*subreq
);
264 struct tevent_req
*cli_get_fs_attr_info_send(TALLOC_CTX
*mem_ctx
,
265 struct tevent_context
*ev
,
266 struct cli_state
*cli
)
268 struct tevent_req
*subreq
, *req
;
269 struct cli_get_fs_attr_info_state
*state
;
271 req
= tevent_req_create(mem_ctx
, &state
,
272 struct cli_get_fs_attr_info_state
);
276 SSVAL(state
->setup
+0, 0, TRANSACT2_QFSINFO
);
277 SSVAL(state
->param
+0, 0, SMB_QUERY_FS_ATTRIBUTE_INFO
);
279 subreq
= cli_trans_send(state
, ev
, cli
, 0, SMBtrans2
,
284 if (tevent_req_nomem(subreq
, req
)) {
285 return tevent_req_post(req
, ev
);
287 tevent_req_set_callback(subreq
, cli_get_fs_attr_info_done
, req
);
291 static void cli_get_fs_attr_info_done(struct tevent_req
*subreq
)
293 struct tevent_req
*req
= tevent_req_callback_data(
294 subreq
, struct tevent_req
);
295 struct cli_get_fs_attr_info_state
*state
= tevent_req_data(
296 req
, struct cli_get_fs_attr_info_state
);
301 status
= cli_trans_recv(subreq
, talloc_tos(), NULL
, NULL
, 0, NULL
,
302 NULL
, 0, NULL
, &data
, 12, &num_data
);
304 if (tevent_req_nterror(req
, status
)) {
307 state
->fs_attr
= IVAL(data
, 0);
309 tevent_req_done(req
);
312 NTSTATUS
cli_get_fs_attr_info_recv(struct tevent_req
*req
, uint32_t *fs_attr
)
314 struct cli_get_fs_attr_info_state
*state
= tevent_req_data(
315 req
, struct cli_get_fs_attr_info_state
);
318 if (tevent_req_is_nterror(req
, &status
)) {
321 *fs_attr
= state
->fs_attr
;
325 NTSTATUS
cli_get_fs_attr_info(struct cli_state
*cli
, uint32_t *fs_attr
)
327 struct tevent_context
*ev
;
328 struct tevent_req
*req
;
329 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
331 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
332 return cli_smb2_get_fs_attr_info(cli
, fs_attr
);
335 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
336 return NT_STATUS_INVALID_PARAMETER
;
338 ev
= samba_tevent_context_init(talloc_tos());
342 req
= cli_get_fs_attr_info_send(ev
, ev
, cli
);
346 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
349 status
= cli_get_fs_attr_info_recv(req
, fs_attr
);
355 NTSTATUS
cli_get_fs_volume_info(struct cli_state
*cli
,
358 uint32_t *pserial_number
,
362 uint16_t recv_flags2
;
366 uint32_t rdata_count
;
368 char *volume_name
= NULL
;
370 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
371 return cli_smb2_get_fs_volume_info(cli
,
378 SSVAL(setup
, 0, TRANSACT2_QFSINFO
);
379 SSVAL(param
,0,SMB_QUERY_FS_VOLUME_INFO
);
381 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
,
389 &rdata
, 18, &rdata_count
);
390 if (!NT_STATUS_IS_OK(status
)) {
396 ts
= interpret_long_date(BVAL(rdata
, 0));
399 if (pserial_number
) {
400 *pserial_number
= IVAL(rdata
,8);
402 nlen
= IVAL(rdata
,12);
403 if (nlen
> (rdata_count
- 18)) {
405 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
408 pull_string_talloc(mem_ctx
,
414 if (volume_name
== NULL
) {
415 status
= map_nt_error_from_unix(errno
);
420 /* todo: but not yet needed
421 * return the other stuff
424 *_volume_name
= volume_name
;
429 NTSTATUS
cli_get_fs_full_size_info(struct cli_state
*cli
,
430 uint64_t *total_allocation_units
,
431 uint64_t *caller_allocation_units
,
432 uint64_t *actual_allocation_units
,
433 uint64_t *sectors_per_allocation_unit
,
434 uint64_t *bytes_per_sector
)
438 uint8_t *rdata
= NULL
;
439 uint32_t rdata_count
;
442 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
443 return cli_smb2_get_fs_full_size_info(cli
,
444 total_allocation_units
,
445 caller_allocation_units
,
446 actual_allocation_units
,
447 sectors_per_allocation_unit
,
451 SSVAL(setup
, 0, TRANSACT2_QFSINFO
);
452 SSVAL(param
, 0, SMB_FS_FULL_SIZE_INFORMATION
);
454 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
,
456 setup
, 1, 0, /* setup */
457 param
, 2, 0, /* param */
458 NULL
, 0, 560, /* data */
460 NULL
, 0, NULL
, /* rsetup */
461 NULL
, 0, NULL
, /* rparam */
462 &rdata
, 32, &rdata_count
); /* rdata */
463 if (!NT_STATUS_IS_OK(status
)) {
467 if (total_allocation_units
) {
468 *total_allocation_units
= BIG_UINT(rdata
, 0);
470 if (caller_allocation_units
) {
471 *caller_allocation_units
= BIG_UINT(rdata
,8);
473 if (actual_allocation_units
) {
474 *actual_allocation_units
= BIG_UINT(rdata
,16);
476 if (sectors_per_allocation_unit
) {
477 *sectors_per_allocation_unit
= IVAL(rdata
,24);
479 if (bytes_per_sector
) {
480 *bytes_per_sector
= IVAL(rdata
,28);
488 NTSTATUS
cli_get_posix_fs_info(struct cli_state
*cli
,
489 uint32_t *optimal_transfer_size
,
490 uint32_t *block_size
,
491 uint64_t *total_blocks
,
492 uint64_t *blocks_available
,
493 uint64_t *user_blocks_available
,
494 uint64_t *total_file_nodes
,
495 uint64_t *free_file_nodes
,
496 uint64_t *fs_identifier
)
500 uint8_t *rdata
= NULL
;
501 uint32_t rdata_count
;
504 SSVAL(setup
, 0, TRANSACT2_QFSINFO
);
505 SSVAL(param
,0,SMB_QUERY_POSIX_FS_INFO
);
507 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
, NULL
, 0, 0, 0,
512 NULL
, 0, NULL
, /* rsetup */
513 NULL
, 0, NULL
, /* rparam */
514 &rdata
, 56, &rdata_count
);
515 if (!NT_STATUS_IS_OK(status
)) {
519 if (optimal_transfer_size
) {
520 *optimal_transfer_size
= IVAL(rdata
, 0);
523 *block_size
= IVAL(rdata
,4);
526 *total_blocks
= BIG_UINT(rdata
,8);
528 if (blocks_available
) {
529 *blocks_available
= BIG_UINT(rdata
,16);
531 if (user_blocks_available
) {
532 *user_blocks_available
= BIG_UINT(rdata
,24);
534 if (total_file_nodes
) {
535 *total_file_nodes
= BIG_UINT(rdata
,32);
537 if (free_file_nodes
) {
538 *free_file_nodes
= BIG_UINT(rdata
,40);
541 *fs_identifier
= BIG_UINT(rdata
,48);
546 /****************************************************************************
547 Do a UNIX extensions SMB_QUERY_POSIX_WHOAMI call.
548 ****************************************************************************/
550 struct posix_whoami_state
{
560 struct dom_sid
*sids
;
563 static void cli_posix_whoami_done(struct tevent_req
*subreq
);
565 static const uint32_t posix_whoami_max_rdata
= 62*1024;
567 struct tevent_req
*cli_posix_whoami_send(TALLOC_CTX
*mem_ctx
,
568 struct tevent_context
*ev
,
569 struct cli_state
*cli
)
571 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
572 struct posix_whoami_state
*state
= NULL
;
574 req
= tevent_req_create(mem_ctx
, &state
, struct posix_whoami_state
);
579 /* Setup setup word. */
580 SSVAL(state
->setup
, 0, TRANSACT2_QFSINFO
);
581 SSVAL(state
->param
, 0, SMB_QUERY_POSIX_WHOAMI
);
583 state
->max_rdata
= posix_whoami_max_rdata
;
585 subreq
= cli_trans_send(state
, /* mem ctx. */
587 cli
, /* cli_state. */
588 0, /* additional_flags2 */
589 SMBtrans2
, /* cmd. */
590 NULL
, /* pipe name. */
594 state
->setup
, /* setup. */
595 1, /* num setup uint16_t words. */
596 0, /* max returned setup. */
597 state
->param
, /* param. */
599 0, /* max returned param. */
602 state
->max_rdata
); /* max returned data. */
604 if (tevent_req_nomem(subreq
, req
)) {
605 return tevent_req_post(req
, ev
);
607 tevent_req_set_callback(subreq
, cli_posix_whoami_done
, req
);
611 static void cli_posix_whoami_done(struct tevent_req
*subreq
)
613 struct tevent_req
*req
= tevent_req_callback_data(
614 subreq
, struct tevent_req
);
615 struct posix_whoami_state
*state
= tevent_req_data(
616 req
, struct posix_whoami_state
);
617 uint8_t *rdata
= NULL
;
619 uint32_t num_rdata
= 0;
623 status
= cli_trans_recv(subreq
,
636 if (tevent_req_nterror(req
, status
)) {
641 * Not strictly needed - cli_trans_recv()
642 * will ensure at least 40 bytes here. Added
643 * as more of a reminder to be careful when
644 * parsing network packets in C.
647 if (num_rdata
< 40 || num_rdata
> posix_whoami_max_rdata
) {
648 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
652 state
->guest
= (IVAL(rdata
, 0) & SMB_WHOAMI_GUEST
);
653 state
->uid
= BVAL(rdata
, 8);
654 state
->gid
= BVAL(rdata
, 16);
655 state
->num_gids
= IVAL(rdata
, 24);
656 state
->num_sids
= IVAL(rdata
, 28);
658 /* Ensure the gid array doesn't overflow */
659 if (state
->num_gids
> (num_rdata
- 40) / sizeof(uint64_t)) {
660 tevent_req_nterror(req
,
661 NT_STATUS_INVALID_NETWORK_RESPONSE
);
665 state
->gids
= talloc_array(state
, uint64_t, state
->num_gids
);
666 if (tevent_req_nomem(state
->gids
, req
)) {
669 state
->sids
= talloc_array(state
, struct dom_sid
, state
->num_sids
);
670 if (tevent_req_nomem(state
->sids
, req
)) {
676 for (i
= 0; i
< state
->num_gids
; i
++) {
677 state
->gids
[i
] = BVAL(p
, 0);
681 num_rdata
-= (p
- rdata
);
683 for (i
= 0; i
< state
->num_sids
; i
++) {
685 DATA_BLOB in
= data_blob_const(p
, num_rdata
);
686 enum ndr_err_code ndr_err
;
688 ndr_err
= ndr_pull_struct_blob(&in
,
691 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
692 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
693 tevent_req_nterror(req
,
694 NT_STATUS_INVALID_NETWORK_RESPONSE
);
698 sid_size
= ndr_size_dom_sid(&state
->sids
[i
], 0);
700 if (sid_size
> num_rdata
) {
701 tevent_req_nterror(req
,
702 NT_STATUS_INVALID_NETWORK_RESPONSE
);
707 num_rdata
-= sid_size
;
710 if (num_rdata
!= 0) {
711 tevent_req_nterror(req
,
712 NT_STATUS_INVALID_NETWORK_RESPONSE
);
716 tevent_req_done(req
);
719 NTSTATUS
cli_posix_whoami_recv(struct tevent_req
*req
,
726 struct dom_sid
**psids
,
730 struct posix_whoami_state
*state
= tevent_req_data(
731 req
, struct posix_whoami_state
);
733 if (tevent_req_is_nterror(req
, &status
)) {
744 *pnum_gids
= state
->num_gids
;
747 *pgids
= talloc_move(mem_ctx
, &state
->gids
);
750 *pnum_sids
= state
->num_sids
;
753 *psids
= talloc_move(mem_ctx
, &state
->sids
);
756 *pguest
= state
->guest
;
761 NTSTATUS
cli_posix_whoami(struct cli_state
*cli
,
768 struct dom_sid
**sids
,
771 TALLOC_CTX
*frame
= talloc_stackframe();
772 struct tevent_context
*ev
= NULL
;
773 struct tevent_req
*req
= NULL
;
774 NTSTATUS status
= NT_STATUS_OK
;
776 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
778 * Can't use sync call while an async call is in flight
780 status
= NT_STATUS_INVALID_PARAMETER
;
784 ev
= samba_tevent_context_init(frame
);
786 status
= NT_STATUS_NO_MEMORY
;
790 req
= cli_posix_whoami_send(frame
,
794 status
= NT_STATUS_NO_MEMORY
;
798 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
802 status
= cli_posix_whoami_recv(req
,