2023-12-18 23:13:56 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-04-25 18:04:06 +04:00
client connect / disconnect routines
Copyright ( C ) Andrew Tridgell 1994 - 1998
2003-02-24 05:55:00 +03:00
Copyright ( C ) Andrew Bartlett 2001 - 2003
2011-04-27 03:31:28 +04:00
Copyright ( C ) Volker Lendecke 2011
Copyright ( C ) Jeremy Allison 2011
2010-01-02 20:25:13 +03:00
2000-04-25 18:04:06 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2000-04-25 18:04:06 +04:00
( at your option ) any later version .
2010-01-02 20:25:13 +03:00
2000-04-25 18:04:06 +04:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2010-01-02 20:25:13 +03:00
2000-04-25 18:04:06 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2000-04-25 18:04:06 +04:00
*/
# include "includes.h"
2011-05-06 13:47:43 +04:00
# include "libsmb/libsmb.h"
2018-03-10 17:31:11 +03:00
# include "libsmb/namequery.h"
2009-03-17 12:08:31 +03:00
# include "../libcli/auth/libcli_auth.h"
2015-11-26 16:34:46 +03:00
# include "auth/credentials/credentials.h"
# include "auth/gensec/gensec.h"
# include "auth/ntlmssp/ntlmssp.h"
# include "auth_generic.h"
2011-04-28 19:38:09 +04:00
# include "../lib/util/tevent_ntstatus.h"
2010-08-26 11:58:09 +04:00
# include "async_smb.h"
2011-03-23 16:18:59 +03:00
# include "libsmb/nmblib.h"
2011-09-13 23:34:35 +04:00
# include "librpc/ndr/libndr.h"
2011-09-22 23:09:00 +04:00
# include "../libcli/smb/smbXcli_base.h"
2016-11-01 01:02:27 +03:00
# include "../libcli/smb/smb_seal.h"
2016-09-13 15:40:36 +03:00
# include "lib/param/param.h"
2017-02-27 18:14:39 +03:00
# include "../libcli/smb/smb2_negotiate_context.h"
2000-04-25 18:04:06 +04:00
2016-10-22 12:00:39 +03:00
static char * cli_session_setup_get_account ( TALLOC_CTX * mem_ctx ,
const char * principal ) ;
2016-10-28 13:09:05 +03:00
struct cli_credentials * cli_session_creds_init ( TALLOC_CTX * mem_ctx ,
const char * username ,
const char * domain ,
const char * realm ,
const char * password ,
bool use_kerberos ,
bool fallback_after_kerberos ,
bool use_ccache ,
bool password_is_nt_hash )
2016-09-13 15:40:36 +03:00
{
struct loadparm_context * lp_ctx = NULL ;
struct cli_credentials * creds = NULL ;
2016-10-22 12:00:39 +03:00
const char * principal = NULL ;
2024-05-28 13:23:35 +03:00
enum credentials_use_kerberos creds_use_krb ;
2016-10-26 13:41:10 +03:00
char * tmp = NULL ;
char * p = NULL ;
2016-09-13 15:40:36 +03:00
bool ok ;
creds = cli_credentials_init ( mem_ctx ) ;
if ( creds = = NULL ) {
return NULL ;
}
lp_ctx = loadparm_init_s3 ( creds , loadparm_s3_helpers ( ) ) ;
if ( lp_ctx = = NULL ) {
goto fail ;
}
2021-05-04 12:06:23 +03:00
ok = cli_credentials_set_conf ( creds , lp_ctx ) ;
if ( ! ok ) {
goto fail ;
}
2016-09-13 15:40:36 +03:00
if ( username = = NULL ) {
username = " " ;
}
if ( strlen ( username ) = = 0 ) {
if ( password ! = NULL & & strlen ( password ) = = 0 ) {
/*
* some callers pass " " as no password
*
* gensec only handles NULL as no password .
*/
password = NULL ;
}
if ( password = = NULL ) {
cli_credentials_set_anonymous ( creds ) ;
return creds ;
}
}
2016-10-26 13:41:10 +03:00
tmp = talloc_strdup ( creds , username ) ;
if ( tmp = = NULL ) {
goto fail ;
}
username = tmp ;
/* allow for workgroups as part of the username */
if ( ( p = strchr_m ( tmp , ' \\ ' ) ) | |
( p = strchr_m ( tmp , ' / ' ) ) | |
( p = strchr_m ( tmp , * lp_winbind_separator ( ) ) ) ) {
* p = 0 ;
username = p + 1 ;
domain = tmp ;
}
2016-10-22 12:00:39 +03:00
principal = username ;
username = cli_session_setup_get_account ( creds , principal ) ;
if ( username = = NULL ) {
goto fail ;
}
ok = strequal ( username , principal ) ;
if ( ok ) {
/*
* Ok still the same , so it ' s not a principal
*/
principal = NULL ;
}
2024-05-28 13:23:35 +03:00
if ( use_kerberos ) {
if ( fallback_after_kerberos ) {
/*
* Keep what we learned from the
* " client use kerberos " option .
*/
creds_use_krb = cli_credentials_get_kerberos_state (
creds ) ;
} else {
creds_use_krb = CRED_USE_KERBEROS_REQUIRED ;
}
2016-09-13 15:40:36 +03:00
} else {
2024-05-28 13:23:35 +03:00
creds_use_krb = CRED_USE_KERBEROS_DISABLED ;
2016-09-13 15:40:36 +03:00
}
2024-05-28 13:23:35 +03:00
cli_credentials_set_kerberos_state ( creds ,
creds_use_krb ,
CRED_SPECIFIED ) ;
2016-09-13 15:40:36 +03:00
if ( use_ccache ) {
2024-05-28 13:38:18 +03:00
cli_credentials_add_gensec_features ( creds ,
GENSEC_FEATURE_NTLM_CCACHE ,
2020-08-20 11:50:30 +03:00
CRED_SPECIFIED ) ;
2016-09-13 15:40:36 +03:00
if ( password ! = NULL & & strlen ( password ) = = 0 ) {
/*
* some callers pass " " as no password
*
* GENSEC_FEATURE_NTLM_CCACHE only handles
* NULL as no password .
*/
password = NULL ;
}
}
ok = cli_credentials_set_username ( creds ,
username ,
CRED_SPECIFIED ) ;
if ( ! ok ) {
goto fail ;
}
2016-12-16 03:26:29 +03:00
if ( domain ! = NULL ) {
ok = cli_credentials_set_domain ( creds ,
domain ,
CRED_SPECIFIED ) ;
if ( ! ok ) {
goto fail ;
}
2016-09-13 15:40:36 +03:00
}
2016-10-22 12:00:39 +03:00
if ( principal ! = NULL ) {
ok = cli_credentials_set_principal ( creds ,
principal ,
CRED_SPECIFIED ) ;
if ( ! ok ) {
goto fail ;
}
}
2016-09-13 15:40:36 +03:00
if ( realm ! = NULL ) {
ok = cli_credentials_set_realm ( creds ,
realm ,
CRED_SPECIFIED ) ;
if ( ! ok ) {
goto fail ;
}
}
if ( password ! = NULL & & strlen ( password ) > 0 ) {
if ( password_is_nt_hash ) {
struct samr_Password nt_hash ;
size_t converted ;
converted = strhex_to_str ( ( char * ) nt_hash . hash ,
sizeof ( nt_hash . hash ) ,
password ,
strlen ( password ) ) ;
if ( converted ! = sizeof ( nt_hash . hash ) ) {
goto fail ;
}
ok = cli_credentials_set_nt_hash ( creds ,
& nt_hash ,
CRED_SPECIFIED ) ;
if ( ! ok ) {
goto fail ;
}
} else {
ok = cli_credentials_set_password ( creds ,
password ,
CRED_SPECIFIED ) ;
if ( ! ok ) {
goto fail ;
}
}
2024-03-07 01:05:00 +03:00
} else if ( use_kerberos & & ! fallback_after_kerberos ) {
const char * error_string = NULL ;
int rc ;
rc = cli_credentials_set_ccache ( creds ,
lp_ctx ,
NULL ,
CRED_SPECIFIED ,
& error_string ) ;
if ( rc ! = 0 ) {
fprintf ( stderr ,
" Warning reading default "
" krb5 credentials cache: %s \n " ,
error_string ) ;
}
2016-09-13 15:40:36 +03:00
}
return creds ;
fail :
TALLOC_FREE ( creds ) ;
return NULL ;
}
2017-03-15 20:04:44 +03:00
static NTSTATUS cli_state_update_after_sesssetup ( struct cli_state * cli ,
const char * native_os ,
const char * native_lm ,
const char * primary_domain )
{
# define _VALID_STR(p) ((p) != NULL && (p)[0] != '\0')
if ( ! _VALID_STR ( cli - > server_os ) & & _VALID_STR ( native_os ) ) {
cli - > server_os = talloc_strdup ( cli , native_os ) ;
if ( cli - > server_os = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
}
if ( ! _VALID_STR ( cli - > server_type ) & & _VALID_STR ( native_lm ) ) {
cli - > server_type = talloc_strdup ( cli , native_lm ) ;
if ( cli - > server_type = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
}
if ( ! _VALID_STR ( cli - > server_domain ) & & _VALID_STR ( primary_domain ) ) {
cli - > server_domain = talloc_strdup ( cli , primary_domain ) ;
if ( cli - > server_domain = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
}
# undef _VALID_STRING
return NT_STATUS_OK ;
}
2011-04-27 23:06:25 +04:00
/********************************************************
Utility function to ensure we always return at least
a valid char * pointer to an empty string for the
cli - > server_os , cli - > server_type and cli - > server_domain
strings .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-07-19 18:02:31 +04:00
static NTSTATUS smb_bytes_talloc_string ( TALLOC_CTX * mem_ctx ,
2012-06-04 18:26:27 +04:00
const uint8_t * hdr ,
2011-04-27 23:06:25 +04:00
char * * dest ,
uint8_t * src ,
size_t srclen ,
ssize_t * destlen )
{
2020-05-17 13:57:32 +03:00
* destlen = pull_string_talloc ( mem_ctx ,
( const char * ) hdr ,
SVAL ( hdr , HDR_FLG2 ) ,
dest ,
( char * ) src ,
srclen ,
STR_TERMINATE ) ;
2011-04-27 23:06:25 +04:00
if ( * destlen = = - 1 ) {
return NT_STATUS_NO_MEMORY ;
}
if ( * dest = = NULL ) {
2011-07-19 18:02:31 +04:00
* dest = talloc_strdup ( mem_ctx , " " ) ;
2011-04-27 23:06:25 +04:00
if ( * dest = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
}
return NT_STATUS_OK ;
}
2001-10-11 11:42:52 +04:00
/****************************************************************************
2002-07-15 14:35:28 +04:00
Work out suitable capabilities to offer the server .
2001-10-11 11:42:52 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
2011-09-08 18:39:14 +04:00
static uint32_t cli_session_setup_capabilities ( struct cli_state * cli ,
uint32_t sesssetup_capabilities )
2001-10-11 11:42:52 +04:00
{
2012-05-19 20:23:40 +04:00
uint32_t client_capabilities = smb1cli_conn_capabilities ( cli - > conn ) ;
2001-10-11 11:42:52 +04:00
2011-09-08 18:39:14 +04:00
/*
* We only send capabilities based on the mask for :
* - client only flags
* - flags used in both directions
*
2013-03-19 12:03:38 +04:00
* We do not echo the server only flags , except some legacy flags .
*
* SMB_CAP_LEGACY_CLIENT_MASK contains CAP_LARGE_READX and
* CAP_LARGE_WRITEX in order to allow us to do large reads
* against old Samba releases ( < = 3.6 . x ) .
2011-09-08 18:39:14 +04:00
*/
2013-03-19 12:03:38 +04:00
client_capabilities & = ( SMB_CAP_BOTH_MASK | SMB_CAP_LEGACY_CLIENT_MASK ) ;
2001-10-11 11:42:52 +04:00
2011-09-08 18:39:14 +04:00
/*
* Session Setup specific flags CAP_DYNAMIC_REAUTH
* and CAP_EXTENDED_SECURITY are passed by the caller .
* We need that in order to do guest logins even if
* CAP_EXTENDED_SECURITY is negotiated .
*/
client_capabilities & = ~ ( CAP_DYNAMIC_REAUTH | CAP_EXTENDED_SECURITY ) ;
sesssetup_capabilities & = ( CAP_DYNAMIC_REAUTH | CAP_EXTENDED_SECURITY ) ;
client_capabilities | = sesssetup_capabilities ;
2001-10-11 11:42:52 +04:00
2011-09-08 18:39:14 +04:00
return client_capabilities ;
2001-10-11 11:42:52 +04:00
}
2003-08-19 21:47:44 +04:00
/****************************************************************************
Do a NT1 guest session setup .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-03-29 19:42:13 +04:00
struct cli_session_setup_guest_state {
struct cli_state * cli ;
2011-04-03 01:53:27 +04:00
uint16_t vwv [ 13 ] ;
2009-05-07 18:23:27 +04:00
struct iovec bytes ;
2009-03-29 19:42:13 +04:00
} ;
static void cli_session_setup_guest_done ( struct tevent_req * subreq ) ;
2009-05-07 18:23:27 +04:00
struct tevent_req * cli_session_setup_guest_create ( TALLOC_CTX * mem_ctx ,
2013-02-18 12:59:58 +04:00
struct tevent_context * ev ,
2009-05-07 18:23:27 +04:00
struct cli_state * cli ,
struct tevent_req * * psmbreq )
2003-08-19 21:47:44 +04:00
{
2009-03-29 19:42:13 +04:00
struct tevent_req * req , * subreq ;
struct cli_session_setup_guest_state * state ;
uint16_t * vwv ;
2009-01-25 23:54:55 +03:00
uint8_t * bytes ;
2009-03-29 19:42:13 +04:00
req = tevent_req_create ( mem_ctx , & state ,
struct cli_session_setup_guest_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > cli = cli ;
vwv = state - > vwv ;
2009-01-25 23:54:55 +03:00
SCVAL ( vwv + 0 , 0 , 0xFF ) ;
SCVAL ( vwv + 0 , 1 , 0 ) ;
SSVAL ( vwv + 1 , 0 , 0 ) ;
SSVAL ( vwv + 2 , 0 , CLI_BUFFER_SIZE ) ;
SSVAL ( vwv + 3 , 0 , 2 ) ;
2011-07-19 14:52:10 +04:00
SSVAL ( vwv + 4 , 0 , cli_state_get_vc_num ( cli ) ) ;
2012-05-19 20:32:51 +04:00
SIVAL ( vwv + 5 , 0 , smb1cli_conn_server_session_key ( cli - > conn ) ) ;
2009-01-25 23:54:55 +03:00
SSVAL ( vwv + 7 , 0 , 0 ) ;
SSVAL ( vwv + 8 , 0 , 0 ) ;
SSVAL ( vwv + 9 , 0 , 0 ) ;
SSVAL ( vwv + 10 , 0 , 0 ) ;
2011-09-08 18:39:14 +04:00
SIVAL ( vwv + 11 , 0 , cli_session_setup_capabilities ( cli , 0 ) ) ;
2009-01-25 23:54:55 +03:00
2009-03-29 19:42:13 +04:00
bytes = talloc_array ( state , uint8_t , 0 ) ;
2009-01-25 23:54:55 +03:00
2012-05-26 14:14:51 +04:00
bytes = smb_bytes_push_str ( bytes , smbXcli_conn_use_unicode ( cli - > conn ) , " " , 1 , /* username */
2009-01-25 23:54:55 +03:00
NULL ) ;
2012-05-26 14:14:51 +04:00
bytes = smb_bytes_push_str ( bytes , smbXcli_conn_use_unicode ( cli - > conn ) , " " , 1 , /* workgroup */
2009-01-25 23:54:55 +03:00
NULL ) ;
2012-05-26 14:14:51 +04:00
bytes = smb_bytes_push_str ( bytes , smbXcli_conn_use_unicode ( cli - > conn ) , " Unix " , 5 , NULL ) ;
bytes = smb_bytes_push_str ( bytes , smbXcli_conn_use_unicode ( cli - > conn ) , " Samba " , 6 , NULL ) ;
2009-01-25 23:54:55 +03:00
2009-05-07 18:23:27 +04:00
if ( bytes = = NULL ) {
TALLOC_FREE ( req ) ;
return NULL ;
2009-01-25 23:54:55 +03:00
}
2003-08-19 21:47:44 +04:00
2009-05-12 22:45:37 +04:00
state - > bytes . iov_base = ( void * ) bytes ;
2009-05-07 18:23:27 +04:00
state - > bytes . iov_len = talloc_get_size ( bytes ) ;
2016-08-18 23:45:35 +03:00
subreq = cli_smb_req_create ( state , ev , cli , SMBsesssetupX , 0 , 0 , 13 ,
vwv , 1 , & state - > bytes ) ;
2009-05-07 18:23:27 +04:00
if ( subreq = = NULL ) {
TALLOC_FREE ( req ) ;
return NULL ;
2009-03-29 19:42:13 +04:00
}
tevent_req_set_callback ( subreq , cli_session_setup_guest_done , req ) ;
2009-05-07 18:23:27 +04:00
* psmbreq = subreq ;
return req ;
}
struct tevent_req * cli_session_setup_guest_send ( TALLOC_CTX * mem_ctx ,
2013-02-18 12:59:58 +04:00
struct tevent_context * ev ,
2009-05-07 18:23:27 +04:00
struct cli_state * cli )
{
struct tevent_req * req , * subreq ;
2009-05-14 06:13:12 +04:00
NTSTATUS status ;
2009-05-07 18:23:27 +04:00
req = cli_session_setup_guest_create ( mem_ctx , ev , cli , & subreq ) ;
2009-05-14 06:13:12 +04:00
if ( req = = NULL ) {
2009-05-07 18:23:27 +04:00
return NULL ;
}
2009-05-14 06:13:12 +04:00
2012-05-26 13:58:34 +04:00
status = smb1cli_req_chain_submit ( & subreq , 1 ) ;
2013-04-03 15:24:53 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-05-14 06:13:12 +04:00
tevent_req_nterror ( req , status ) ;
return tevent_req_post ( req , ev ) ;
}
2009-03-29 19:42:13 +04:00
return req ;
2009-01-25 23:54:55 +03:00
}
2003-08-19 21:47:44 +04:00
2009-03-29 19:42:13 +04:00
static void cli_session_setup_guest_done ( struct tevent_req * subreq )
2009-01-25 23:54:55 +03:00
{
2009-03-29 19:42:13 +04:00
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_session_setup_guest_state * state = tevent_req_data (
req , struct cli_session_setup_guest_state ) ;
struct cli_state * cli = state - > cli ;
uint32_t num_bytes ;
2010-02-20 17:14:43 +03:00
uint8_t * in ;
2012-06-04 18:26:27 +04:00
uint8_t * inhdr ;
2009-01-25 23:54:55 +03:00
uint8_t * bytes ;
uint8_t * p ;
NTSTATUS status ;
2011-04-27 23:06:25 +04:00
ssize_t ret ;
2011-05-29 12:58:05 +04:00
uint8_t wct ;
uint16_t * vwv ;
2009-01-25 23:54:55 +03:00
2011-05-29 12:58:05 +04:00
status = cli_smb_recv ( subreq , state , & in , 3 , & wct , & vwv ,
2010-02-20 11:53:58 +03:00
& num_bytes , & bytes ) ;
2010-02-20 17:14:43 +03:00
TALLOC_FREE ( subreq ) ;
2009-01-25 23:54:55 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2009-03-29 19:42:13 +04:00
tevent_req_nterror ( req , status ) ;
return ;
2006-08-17 14:01:48 +04:00
}
2003-08-19 21:47:44 +04:00
2012-06-04 18:26:27 +04:00
inhdr = in + NBT_HDR_SIZE ;
2009-01-25 23:54:55 +03:00
p = bytes ;
2003-08-19 21:47:44 +04:00
2012-06-04 18:26:27 +04:00
cli_state_set_uid ( state - > cli , SVAL ( inhdr , HDR_UID ) ) ;
2016-04-19 08:19:19 +03:00
smb1cli_session_set_action ( cli - > smb1 . session , SVAL ( vwv + 2 , 0 ) ) ;
2009-01-25 23:54:55 +03:00
2011-04-27 23:06:25 +04:00
status = smb_bytes_talloc_string ( cli ,
2012-06-04 18:26:27 +04:00
inhdr ,
2011-04-27 23:06:25 +04:00
& cli - > server_os ,
p ,
bytes + num_bytes - p ,
& ret ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
p + = ret ;
status = smb_bytes_talloc_string ( cli ,
2012-06-04 18:26:27 +04:00
inhdr ,
2011-04-27 23:06:25 +04:00
& cli - > server_type ,
p ,
bytes + num_bytes - p ,
& ret ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
p + = ret ;
status = smb_bytes_talloc_string ( cli ,
2012-06-04 18:26:27 +04:00
inhdr ,
2011-04-27 23:06:25 +04:00
& cli - > server_domain ,
p ,
bytes + num_bytes - p ,
& ret ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
2003-08-19 21:47:44 +04:00
2009-03-29 19:42:13 +04:00
tevent_req_done ( req ) ;
}
2003-08-19 21:47:44 +04:00
2009-03-29 19:42:13 +04:00
NTSTATUS cli_session_setup_guest_recv ( struct tevent_req * req )
{
return tevent_req_simple_recv_ntstatus ( req ) ;
2003-08-19 21:47:44 +04:00
}
2007-02-28 03:35:58 +03:00
/* The following is calculated from :
* ( smb_size - 4 ) = 35
* ( smb_wcnt * 2 ) = 24 ( smb_wcnt = = 12 in cli_session_setup_blob_send ( ) )
* ( strlen ( " Unix " ) + 1 + strlen ( " Samba " ) + 1 ) * 2 = 22 ( unicode strings at
* end of packet .
*/
# define BASE_SESSSETUP_BLOB_PACKET_SIZE (35 + 24 + 22)
2009-12-20 16:47:09 +03:00
struct cli_sesssetup_blob_state {
struct tevent_context * ev ;
struct cli_state * cli ;
DATA_BLOB blob ;
uint16_t max_blob_size ;
2016-10-25 22:50:44 +03:00
DATA_BLOB this_blob ;
2011-09-20 07:07:27 +04:00
struct iovec * recv_iov ;
2009-12-20 16:47:09 +03:00
NTSTATUS status ;
2016-10-25 22:50:44 +03:00
const uint8_t * inbuf ;
2009-12-20 16:47:09 +03:00
DATA_BLOB ret_blob ;
2016-10-25 22:50:44 +03:00
char * out_native_os ;
char * out_native_lm ;
2009-12-20 16:47:09 +03:00
} ;
static bool cli_sesssetup_blob_next ( struct cli_sesssetup_blob_state * state ,
struct tevent_req * * psubreq ) ;
static void cli_sesssetup_blob_done ( struct tevent_req * subreq ) ;
static struct tevent_req * cli_sesssetup_blob_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct cli_state * cli ,
DATA_BLOB blob )
2003-03-18 15:01:47 +03:00
{
2009-12-20 16:47:09 +03:00
struct tevent_req * req , * subreq ;
struct cli_sesssetup_blob_state * state ;
2011-09-13 13:35:53 +04:00
uint32_t usable_space ;
2009-12-20 16:47:09 +03:00
req = tevent_req_create ( mem_ctx , & state ,
struct cli_sesssetup_blob_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > ev = ev ;
state - > blob = blob ;
state - > cli = cli ;
2007-02-28 03:35:58 +03:00
2012-05-19 20:09:30 +04:00
if ( smbXcli_conn_protocol ( cli - > conn ) > = PROTOCOL_SMB2_02 ) {
2011-09-20 07:07:27 +04:00
usable_space = UINT16_MAX ;
} else {
usable_space = cli_state_available_size ( cli ,
2011-09-13 13:35:53 +04:00
BASE_SESSSETUP_BLOB_PACKET_SIZE ) ;
2011-09-20 07:07:27 +04:00
}
2011-09-13 13:35:53 +04:00
if ( usable_space = = 0 ) {
2009-12-20 16:47:09 +03:00
DEBUG ( 1 , ( " cli_session_setup_blob: cli->max_xmit too small "
2011-09-13 13:35:53 +04:00
" (not possible to send %u bytes) \n " ,
BASE_SESSSETUP_BLOB_PACKET_SIZE + 1 ) ) ;
2009-12-20 16:47:09 +03:00
tevent_req_nterror ( req , NT_STATUS_INVALID_PARAMETER ) ;
return tevent_req_post ( req , ev ) ;
2003-03-18 15:01:47 +03:00
}
2011-09-13 13:35:53 +04:00
state - > max_blob_size = MIN ( usable_space , 0xFFFF ) ;
2007-02-28 03:35:58 +03:00
2009-12-20 16:47:09 +03:00
if ( ! cli_sesssetup_blob_next ( state , & subreq ) ) {
2011-03-26 15:29:37 +03:00
tevent_req_nterror ( req , NT_STATUS_NO_MEMORY ) ;
2009-12-20 16:47:09 +03:00
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_sesssetup_blob_done , req ) ;
return req ;
}
2007-02-28 03:35:58 +03:00
2009-12-20 16:47:09 +03:00
static bool cli_sesssetup_blob_next ( struct cli_sesssetup_blob_state * state ,
struct tevent_req * * psubreq )
{
struct tevent_req * subreq ;
uint16_t thistime ;
2011-09-20 07:07:27 +04:00
thistime = MIN ( state - > blob . length , state - > max_blob_size ) ;
2016-10-25 22:50:44 +03:00
state - > this_blob . data = state - > blob . data ;
state - > this_blob . length = thistime ;
2011-09-20 07:07:27 +04:00
2016-10-25 22:50:44 +03:00
state - > blob . data + = thistime ;
state - > blob . length - = thistime ;
2011-09-20 07:07:27 +04:00
2016-10-25 22:50:44 +03:00
if ( smbXcli_conn_protocol ( state - > cli - > conn ) > = PROTOCOL_SMB2_02 ) {
2011-09-20 07:07:27 +04:00
subreq = smb2cli_session_setup_send ( state , state - > ev ,
state - > cli - > conn ,
state - > cli - > timeout ,
state - > cli - > smb2 . session ,
0 , /* in_flags */
SMB2_CAP_DFS , /* in_capabilities */
0 , /* in_channel */
2012-02-26 20:35:28 +04:00
0 , /* in_previous_session_id */
2016-10-25 22:50:44 +03:00
& state - > this_blob ) ;
2011-09-20 07:07:27 +04:00
if ( subreq = = NULL ) {
return false ;
}
2016-10-25 22:50:44 +03:00
} else {
uint16_t in_buf_size = 0 ;
uint16_t in_mpx_max = 0 ;
uint16_t in_vc_num = 0 ;
uint32_t in_sess_key = 0 ;
uint32_t in_capabilities = 0 ;
const char * in_native_os = NULL ;
const char * in_native_lm = NULL ;
in_buf_size = CLI_BUFFER_SIZE ;
in_mpx_max = smbXcli_conn_max_requests ( state - > cli - > conn ) ;
in_vc_num = cli_state_get_vc_num ( state - > cli ) ;
in_sess_key = smb1cli_conn_server_session_key ( state - > cli - > conn ) ;
in_capabilities = cli_session_setup_capabilities ( state - > cli ,
CAP_EXTENDED_SECURITY ) ;
in_native_os = " Unix " ;
in_native_lm = " Samba " ;
2009-12-20 16:47:09 +03:00
2016-10-25 22:50:44 +03:00
/*
* For now we keep the same values as before ,
* we may remove these in a separate commit later .
*/
in_mpx_max = 2 ;
in_vc_num = 1 ;
in_sess_key = 0 ;
subreq = smb1cli_session_setup_ext_send ( state , state - > ev ,
state - > cli - > conn ,
state - > cli - > timeout ,
state - > cli - > smb1 . pid ,
state - > cli - > smb1 . session ,
in_buf_size ,
in_mpx_max ,
in_vc_num ,
in_sess_key ,
state - > this_blob ,
in_capabilities ,
in_native_os ,
in_native_lm ) ;
if ( subreq = = NULL ) {
return false ;
}
2009-12-20 16:47:09 +03:00
}
* psubreq = subreq ;
return true ;
}
2007-02-28 03:35:58 +03:00
2009-12-20 16:47:09 +03:00
static void cli_sesssetup_blob_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_sesssetup_blob_state * state = tevent_req_data (
req , struct cli_sesssetup_blob_state ) ;
NTSTATUS status ;
2007-02-28 03:35:58 +03:00
2012-05-19 20:09:30 +04:00
if ( smbXcli_conn_protocol ( state - > cli - > conn ) > = PROTOCOL_SMB2_02 ) {
2011-09-20 07:07:27 +04:00
status = smb2cli_session_setup_recv ( subreq , state ,
& state - > recv_iov ,
& state - > ret_blob ) ;
} else {
2016-10-25 22:50:44 +03:00
status = smb1cli_session_setup_ext_recv ( subreq , state ,
& state - > recv_iov ,
& state - > inbuf ,
& state - > ret_blob ,
& state - > out_native_os ,
& state - > out_native_lm ) ;
2011-09-20 07:07:27 +04:00
}
2010-02-20 17:15:28 +03:00
TALLOC_FREE ( subreq ) ;
2009-12-20 16:47:09 +03:00
if ( ! NT_STATUS_IS_OK ( status )
& & ! NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) ) {
tevent_req_nterror ( req , status ) ;
return ;
}
2007-02-28 03:35:58 +03:00
2016-10-25 22:50:44 +03:00
state - > status = status ;
2009-12-20 16:47:09 +03:00
2017-03-15 20:04:44 +03:00
status = cli_state_update_after_sesssetup ( state - > cli ,
state - > out_native_os ,
state - > out_native_lm ,
NULL ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2009-12-20 16:47:09 +03:00
if ( state - > blob . length ! = 0 ) {
/*
* More to send
*/
if ( ! cli_sesssetup_blob_next ( state , & subreq ) ) {
2011-06-19 23:10:01 +04:00
tevent_req_oom ( req ) ;
2009-12-20 16:47:09 +03:00
return ;
2007-02-28 03:35:58 +03:00
}
2009-12-20 16:47:09 +03:00
tevent_req_set_callback ( subreq , cli_sesssetup_blob_done , req ) ;
return ;
2007-02-28 03:35:58 +03:00
}
2009-12-20 16:47:09 +03:00
tevent_req_done ( req ) ;
}
2007-02-28 03:35:58 +03:00
2009-12-20 16:47:09 +03:00
static NTSTATUS cli_sesssetup_blob_recv ( struct tevent_req * req ,
TALLOC_CTX * mem_ctx ,
DATA_BLOB * pblob ,
2016-10-25 22:50:44 +03:00
const uint8_t * * pinbuf ,
2011-09-20 07:07:27 +04:00
struct iovec * * precv_iov )
2009-12-20 16:47:09 +03:00
{
struct cli_sesssetup_blob_state * state = tevent_req_data (
req , struct cli_sesssetup_blob_state ) ;
NTSTATUS status ;
2011-09-20 07:07:27 +04:00
struct iovec * recv_iov ;
2009-12-20 16:47:09 +03:00
if ( tevent_req_is_nterror ( req , & status ) ) {
2011-09-20 07:07:27 +04:00
TALLOC_FREE ( state - > cli - > smb2 . session ) ;
2011-07-21 11:20:43 +04:00
cli_state_set_uid ( state - > cli , UID_FIELD_INVALID ) ;
2016-10-25 22:50:44 +03:00
tevent_req_received ( req ) ;
2009-12-20 16:47:09 +03:00
return status ;
}
2011-09-20 07:07:27 +04:00
recv_iov = talloc_move ( mem_ctx , & state - > recv_iov ) ;
2009-12-20 16:47:09 +03:00
if ( pblob ! = NULL ) {
* pblob = state - > ret_blob ;
}
if ( pinbuf ! = NULL ) {
2016-10-25 22:50:44 +03:00
* pinbuf = state - > inbuf ;
2009-12-20 16:47:09 +03:00
}
2011-09-20 07:07:27 +04:00
if ( precv_iov ! = NULL ) {
* precv_iov = recv_iov ;
}
2009-12-20 16:47:09 +03:00
/* could be NT_STATUS_MORE_PROCESSING_REQUIRED */
2016-10-25 22:50:44 +03:00
status = state - > status ;
tevent_req_received ( req ) ;
return status ;
2003-03-18 15:01:47 +03:00
}
2001-10-12 08:49:42 +04:00
/****************************************************************************
2002-07-15 14:35:28 +04:00
Do a spnego / NTLMSSP encrypted session setup .
2001-10-12 08:49:42 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state {
2009-12-23 01:31:20 +03:00
struct tevent_context * ev ;
struct cli_state * cli ;
2015-11-26 16:34:46 +03:00
struct auth_generic_state * auth_generic ;
bool is_anonymous ;
DATA_BLOB blob_in ;
2016-10-25 22:50:44 +03:00
const uint8_t * inbuf ;
2016-03-01 20:31:50 +03:00
struct iovec * recv_iov ;
2009-12-23 01:31:20 +03:00
DATA_BLOB blob_out ;
2016-03-01 20:31:50 +03:00
bool local_ready ;
bool remote_ready ;
2015-11-26 16:34:46 +03:00
DATA_BLOB session_key ;
2009-12-23 01:31:20 +03:00
} ;
2001-10-12 08:49:42 +04:00
2016-03-01 17:47:11 +03:00
static int cli_session_setup_gensec_state_destructor (
struct cli_session_setup_gensec_state * state )
2009-12-23 01:31:20 +03:00
{
2015-11-26 16:34:46 +03:00
TALLOC_FREE ( state - > auth_generic ) ;
data_blob_clear_free ( & state - > session_key ) ;
2009-12-23 01:31:20 +03:00
return 0 ;
}
2003-03-18 15:01:47 +03:00
2016-03-01 17:47:11 +03:00
static void cli_session_setup_gensec_local_next ( struct tevent_req * req ) ;
static void cli_session_setup_gensec_local_done ( struct tevent_req * subreq ) ;
static void cli_session_setup_gensec_remote_next ( struct tevent_req * req ) ;
static void cli_session_setup_gensec_remote_done ( struct tevent_req * subreq ) ;
static void cli_session_setup_gensec_ready ( struct tevent_req * req ) ;
2009-12-23 01:31:20 +03:00
2016-03-01 17:47:11 +03:00
static struct tevent_req * cli_session_setup_gensec_send (
2009-12-23 01:31:20 +03:00
TALLOC_CTX * mem_ctx , struct tevent_context * ev , struct cli_state * cli ,
2016-10-21 21:35:01 +03:00
struct cli_credentials * creds ,
2016-03-01 17:47:11 +03:00
const char * target_service ,
2016-10-27 16:03:07 +03:00
const char * target_hostname )
2009-12-23 01:31:20 +03:00
{
2016-03-01 20:31:50 +03:00
struct tevent_req * req ;
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state * state ;
2009-12-23 01:31:20 +03:00
NTSTATUS status ;
2016-10-27 16:00:47 +03:00
const DATA_BLOB * b = NULL ;
2009-12-23 01:31:20 +03:00
req = tevent_req_create ( mem_ctx , & state ,
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state ) ;
2009-12-23 01:31:20 +03:00
if ( req = = NULL ) {
return NULL ;
2003-02-24 05:55:00 +03:00
}
2009-12-23 01:31:20 +03:00
state - > ev = ev ;
state - > cli = cli ;
2001-10-12 08:49:42 +04:00
2009-12-23 01:31:20 +03:00
talloc_set_destructor (
2016-03-01 17:47:11 +03:00
state , cli_session_setup_gensec_state_destructor ) ;
2009-12-23 01:31:20 +03:00
2016-09-13 15:40:36 +03:00
status = auth_generic_client_prepare ( state , & state - > auth_generic ) ;
2015-11-26 16:34:46 +03:00
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
2002-09-25 19:19:00 +04:00
}
2015-11-26 16:34:46 +03:00
2016-09-13 15:40:36 +03:00
status = auth_generic_set_creds ( state - > auth_generic , creds ) ;
2015-11-26 16:34:46 +03:00
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
2009-12-23 01:31:20 +03:00
}
2015-11-26 16:34:46 +03:00
2016-09-13 15:40:36 +03:00
gensec_want_feature ( state - > auth_generic - > gensec_security ,
GENSEC_FEATURE_SESSION_KEY ) ;
2016-03-01 17:47:11 +03:00
if ( target_service ! = NULL ) {
status = gensec_set_target_service (
state - > auth_generic - > gensec_security ,
target_service ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
}
if ( target_hostname ! = NULL ) {
status = gensec_set_target_hostname (
state - > auth_generic - > gensec_security ,
target_hostname ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
}
2016-10-27 16:00:47 +03:00
b = smbXcli_conn_server_gss_blob ( cli - > conn ) ;
if ( b ! = NULL ) {
state - > blob_in = * b ;
2016-03-01 17:47:11 +03:00
}
2015-11-26 16:34:46 +03:00
state - > is_anonymous = cli_credentials_is_anonymous ( state - > auth_generic - > credentials ) ;
status = auth_generic_client_start ( state - > auth_generic ,
GENSEC_OID_SPNEGO ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
2009-12-23 01:31:20 +03:00
}
2015-11-26 16:34:46 +03:00
2012-05-19 20:09:30 +04:00
if ( smbXcli_conn_protocol ( cli - > conn ) > = PROTOCOL_SMB2_02 ) {
2011-09-20 07:07:27 +04:00
state - > cli - > smb2 . session = smbXcli_session_create ( cli ,
cli - > conn ) ;
if ( tevent_req_nomem ( state - > cli - > smb2 . session , req ) ) {
return tevent_req_post ( req , ev ) ;
}
}
2016-03-01 17:47:11 +03:00
cli_session_setup_gensec_local_next ( req ) ;
2016-03-01 20:31:50 +03:00
if ( ! tevent_req_is_in_progress ( req ) ) {
2009-12-23 01:31:20 +03:00
return tevent_req_post ( req , ev ) ;
}
2016-03-01 20:31:50 +03:00
2009-12-23 01:31:20 +03:00
return req ;
}
2001-10-12 08:49:42 +04:00
2016-03-01 17:47:11 +03:00
static void cli_session_setup_gensec_local_next ( struct tevent_req * req )
2009-12-23 01:31:20 +03:00
{
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state * state =
2016-03-01 20:31:50 +03:00
tevent_req_data ( req ,
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state ) ;
2016-03-01 20:31:50 +03:00
struct tevent_req * subreq = NULL ;
if ( state - > local_ready ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_NETWORK_RESPONSE ) ;
return ;
}
subreq = gensec_update_send ( state , state - > ev ,
state - > auth_generic - > gensec_security ,
state - > blob_in ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
2016-03-01 17:47:11 +03:00
tevent_req_set_callback ( subreq , cli_session_setup_gensec_local_done , req ) ;
2016-03-01 20:31:50 +03:00
}
2016-03-01 17:47:11 +03:00
static void cli_session_setup_gensec_local_done ( struct tevent_req * subreq )
2016-03-01 20:31:50 +03:00
{
struct tevent_req * req =
tevent_req_callback_data ( subreq ,
struct tevent_req ) ;
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state * state =
2016-03-01 20:31:50 +03:00
tevent_req_data ( req ,
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state ) ;
2009-12-23 01:31:20 +03:00
NTSTATUS status ;
2007-03-08 01:29:21 +03:00
2016-03-01 20:31:50 +03:00
status = gensec_update_recv ( subreq , state , & state - > blob_out ) ;
2009-12-23 01:31:20 +03:00
TALLOC_FREE ( subreq ) ;
2016-03-01 20:31:50 +03:00
state - > blob_in = data_blob_null ;
if ( ! NT_STATUS_IS_OK ( status ) & &
! NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) )
{
tevent_req_nterror ( req , status ) ;
return ;
}
2003-12-27 13:11:26 +03:00
2009-12-23 01:31:20 +03:00
if ( NT_STATUS_IS_OK ( status ) ) {
2016-03-01 20:31:50 +03:00
state - > local_ready = true ;
}
2015-11-26 16:34:46 +03:00
2016-03-01 20:31:50 +03:00
if ( state - > local_ready & & state - > remote_ready ) {
2016-03-01 17:47:11 +03:00
cli_session_setup_gensec_ready ( req ) ;
2016-03-01 20:31:50 +03:00
return ;
}
2015-11-26 16:34:46 +03:00
2016-03-01 17:47:11 +03:00
cli_session_setup_gensec_remote_next ( req ) ;
2016-03-01 20:31:50 +03:00
}
2003-12-27 13:11:26 +03:00
2016-03-01 17:47:11 +03:00
static void cli_session_setup_gensec_remote_next ( struct tevent_req * req )
2016-03-01 20:31:50 +03:00
{
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state * state =
2016-03-01 20:31:50 +03:00
tevent_req_data ( req ,
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state ) ;
2016-03-01 20:31:50 +03:00
struct tevent_req * subreq = NULL ;
2015-11-26 16:34:46 +03:00
2016-03-01 20:31:50 +03:00
if ( state - > remote_ready ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_NETWORK_RESPONSE ) ;
return ;
}
2012-04-16 14:14:07 +04:00
2016-03-01 20:31:50 +03:00
subreq = cli_sesssetup_blob_send ( state , state - > ev ,
state - > cli , state - > blob_out ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq ,
2016-03-01 17:47:11 +03:00
cli_session_setup_gensec_remote_done ,
2016-03-01 20:31:50 +03:00
req ) ;
}
2012-04-16 14:14:07 +04:00
2016-03-01 17:47:11 +03:00
static void cli_session_setup_gensec_remote_done ( struct tevent_req * subreq )
2016-03-01 20:31:50 +03:00
{
struct tevent_req * req =
tevent_req_callback_data ( subreq ,
struct tevent_req ) ;
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state * state =
2016-03-01 20:31:50 +03:00
tevent_req_data ( req ,
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state ) ;
2016-03-01 20:31:50 +03:00
NTSTATUS status ;
2012-08-01 10:54:51 +04:00
2016-10-25 22:50:44 +03:00
state - > inbuf = NULL ;
2016-03-01 20:31:50 +03:00
TALLOC_FREE ( state - > recv_iov ) ;
2012-08-01 10:54:51 +04:00
2016-03-01 20:31:50 +03:00
status = cli_sesssetup_blob_recv ( subreq , state , & state - > blob_in ,
& state - > inbuf , & state - > recv_iov ) ;
TALLOC_FREE ( subreq ) ;
data_blob_free ( & state - > blob_out ) ;
if ( ! NT_STATUS_IS_OK ( status ) & &
! NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) )
{
tevent_req_nterror ( req , status ) ;
2009-12-23 01:31:20 +03:00
return ;
}
2016-03-01 20:31:50 +03:00
if ( NT_STATUS_IS_OK ( status ) ) {
2016-04-19 08:20:28 +03:00
struct smbXcli_session * session = NULL ;
bool is_guest = false ;
if ( smbXcli_conn_protocol ( state - > cli - > conn ) > = PROTOCOL_SMB2_02 ) {
session = state - > cli - > smb2 . session ;
} else {
session = state - > cli - > smb1 . session ;
}
is_guest = smbXcli_session_is_guest ( session ) ;
if ( is_guest ) {
/*
* We can ' t finish the gensec handshake , we don ' t
* have a negotiated session key .
*
2017-05-10 17:17:48 +03:00
* So just pretend we are completely done ,
* we need to continue as anonymous from this point ,
* as we can ' t get a session key .
2016-04-28 03:36:35 +03:00
*
* Note that smbXcli_session_is_guest ( )
* always returns false if we require signing .
2016-04-19 08:20:28 +03:00
*/
state - > blob_in = data_blob_null ;
state - > local_ready = true ;
2017-05-10 17:17:48 +03:00
state - > is_anonymous = true ;
2016-04-19 08:20:28 +03:00
}
2016-03-01 20:31:50 +03:00
state - > remote_ready = true ;
}
if ( state - > local_ready & & state - > remote_ready ) {
2016-03-01 17:47:11 +03:00
cli_session_setup_gensec_ready ( req ) ;
2009-12-23 01:31:20 +03:00
return ;
}
2008-11-30 14:25:17 +03:00
2016-03-01 17:47:11 +03:00
cli_session_setup_gensec_local_next ( req ) ;
2016-03-01 20:31:50 +03:00
}
libsmb,s3/smbd: dump SMB3+ session keys if debug parm is set
Use of previously added smb.conf global param.
Sample usage:
$ smbclient //localhost/scratch --option='debugencryption=yes' \
-e -mSMB3 -U aaptel%aaptel -c quit
debug encryption: dumping generated session keys
Session Id [0000] 26 48 BF FD 00 00 00 00 &H......
Session Key [0000] 63 D6 CA BC 08 C8 4A D2 45 F6 AE 35 AB 4A B3 3B c.....J. E..5.J.;
Signing Key [0000] 4E FE 35 92 AC 13 14 FC C9 17 62 B1 82 20 A4 12 N.5..... ..b.. ..
App Key [0000] A5 0F F4 8B 2F FB 0D FF F2 BF EE 39 E6 6D F5 0A ..../... ...9.m..
ServerIn Key [0000] 2A 02 7E E1 D3 58 D8 12 4C 63 76 AE 59 17 5A E4 *.~..X.. Lcv.Y.Z.
ServerOut Key [0000] 59 F2 5B 7F 66 8F 31 A0 A5 E4 A8 D8 2F BA 00 38 Y.[.f.1. ..../..8
We can now simply pass -ouat:smb2_seskey_list:<sesid>,<seskey> to
wireshark or tshark:
$ tshark -ouat:smb2_seskey_list:2648BFFD00000000,63D6CABC08C84AD245F6AE35AB4AB33B \
-Y smb2 -r capture.pcap -Tfields -e _ws.col.Info
Negotiate Protocol Response
Negotiate Protocol Request
Negotiate Protocol Response
Session Setup Request, NTLMSSP_NEGOTIATE
Session Setup Response, Error: STATUS_MORE_PROCESSING_REQUIRED, NTLMSSP_CHALLENGE
Session Setup Request, NTLMSSP_AUTH, User: WORKGROUP\aaptel
Session Setup Response
Tree Connect Request Tree: \\localhost\IPC$
Tree Connect Response
Decrypted SMB3;Ioctl Request FSCTL_DFS_GET_REFERRALS, File: \localhost\scratch
Decrypted SMB3;Ioctl Response, Error: STATUS_NOT_FOUND
Decrypted SMB3;Tree Disconnect Request
Decrypted SMB3;Tree Disconnect Response
Decrypted SMB3;Tree Connect Request Tree: \\localhost\scratch
Decrypted SMB3;Tree Connect Response
Decrypted SMB3;Tree Disconnect Request
Decrypted SMB3;Tree Disconnect Response
For more info on Wireshark decryption support see
https://wiki.samba.org/index.php/Wireshark_Decryption
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Reviewed-by: Noel Power <npower@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
Reviewed-by: David Disseldorp <ddiss@samba.org>
Autobuild-User(master): David Disseldorp <ddiss@samba.org>
Autobuild-Date(master): Sat Feb 9 21:43:25 CET 2019 on sn-devel-144
2019-02-08 14:13:25 +03:00
static void cli_session_dump_keys ( TALLOC_CTX * mem_ctx ,
struct smbXcli_session * session ,
DATA_BLOB session_key )
{
NTSTATUS status ;
DATA_BLOB sig = data_blob_null ;
DATA_BLOB app = data_blob_null ;
DATA_BLOB enc = data_blob_null ;
DATA_BLOB dec = data_blob_null ;
uint64_t sid = smb2cli_session_current_id ( session ) ;
status = smb2cli_session_signing_key ( session , mem_ctx , & sig ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto out ;
}
status = smbXcli_session_application_key ( session , mem_ctx , & app ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto out ;
}
status = smb2cli_session_encryption_key ( session , mem_ctx , & enc ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto out ;
}
status = smb2cli_session_decryption_key ( session , mem_ctx , & dec ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto out ;
}
DEBUG ( 0 , ( " debug encryption: dumping generated session keys \n " ) ) ;
DEBUGADD ( 0 , ( " Session Id " ) ) ;
dump_data ( 0 , ( uint8_t * ) & sid , sizeof ( sid ) ) ;
DEBUGADD ( 0 , ( " Session Key " ) ) ;
dump_data ( 0 , session_key . data , session_key . length ) ;
DEBUGADD ( 0 , ( " Signing Key " ) ) ;
dump_data ( 0 , sig . data , sig . length ) ;
DEBUGADD ( 0 , ( " App Key " ) ) ;
dump_data ( 0 , app . data , app . length ) ;
/* In client code, ServerIn is the encryption key */
DEBUGADD ( 0 , ( " ServerIn Key " ) ) ;
dump_data ( 0 , enc . data , enc . length ) ;
DEBUGADD ( 0 , ( " ServerOut Key " ) ) ;
dump_data ( 0 , dec . data , dec . length ) ;
out :
data_blob_clear_free ( & sig ) ;
data_blob_clear_free ( & app ) ;
data_blob_clear_free ( & enc ) ;
data_blob_clear_free ( & dec ) ;
}
2016-03-01 17:47:11 +03:00
static void cli_session_setup_gensec_ready ( struct tevent_req * req )
2016-03-01 20:31:50 +03:00
{
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state * state =
2016-03-01 20:31:50 +03:00
tevent_req_data ( req ,
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state ) ;
2016-03-01 20:31:50 +03:00
const char * server_domain = NULL ;
NTSTATUS status ;
if ( state - > blob_in . length ! = 0 ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_NETWORK_RESPONSE ) ;
2009-12-23 01:31:20 +03:00
return ;
}
2016-03-01 20:31:50 +03:00
if ( state - > blob_out . length ! = 0 ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_NETWORK_RESPONSE ) ;
2009-12-23 01:31:20 +03:00
return ;
}
2003-03-18 15:01:47 +03:00
2016-03-01 20:31:50 +03:00
/*
2016-03-01 17:47:11 +03:00
* gensec_ntlmssp_server_domain ( ) returns NULL
* if NTLMSSP is not used .
*
2016-03-01 20:31:50 +03:00
* We can remove this later
* and leave the server domain empty for SMB2 and above
* in future releases .
*/
server_domain = gensec_ntlmssp_server_domain (
state - > auth_generic - > gensec_security ) ;
2017-06-08 17:08:54 +03:00
if ( state - > cli - > server_domain [ 0 ] = = ' \0 ' & & server_domain ! = NULL ) {
2016-03-01 20:31:50 +03:00
TALLOC_FREE ( state - > cli - > server_domain ) ;
state - > cli - > server_domain = talloc_strdup ( state - > cli ,
server_domain ) ;
if ( state - > cli - > server_domain = = NULL ) {
tevent_req_nterror ( req , NT_STATUS_NO_MEMORY ) ;
return ;
}
}
2016-04-19 08:33:03 +03:00
if ( state - > is_anonymous ) {
/*
* Windows server does not set the
* SMB2_SESSION_FLAG_IS_NULL flag .
*
* This fix makes sure we do not try
* to verify a signature on the final
* session setup response .
*/
tevent_req_done ( req ) ;
return ;
}
2016-03-01 20:31:50 +03:00
status = gensec_session_key ( state - > auth_generic - > gensec_security ,
state , & state - > session_key ) ;
if ( tevent_req_nterror ( req , status ) ) {
2009-12-23 01:31:20 +03:00
return ;
2007-08-09 03:56:55 +04:00
}
2016-03-01 20:31:50 +03:00
if ( smbXcli_conn_protocol ( state - > cli - > conn ) > = PROTOCOL_SMB2_02 ) {
struct smbXcli_session * session = state - > cli - > smb2 . session ;
status = smb2cli_session_set_session_key ( session ,
state - > session_key ,
state - > recv_iov ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
libsmb,s3/smbd: dump SMB3+ session keys if debug parm is set
Use of previously added smb.conf global param.
Sample usage:
$ smbclient //localhost/scratch --option='debugencryption=yes' \
-e -mSMB3 -U aaptel%aaptel -c quit
debug encryption: dumping generated session keys
Session Id [0000] 26 48 BF FD 00 00 00 00 &H......
Session Key [0000] 63 D6 CA BC 08 C8 4A D2 45 F6 AE 35 AB 4A B3 3B c.....J. E..5.J.;
Signing Key [0000] 4E FE 35 92 AC 13 14 FC C9 17 62 B1 82 20 A4 12 N.5..... ..b.. ..
App Key [0000] A5 0F F4 8B 2F FB 0D FF F2 BF EE 39 E6 6D F5 0A ..../... ...9.m..
ServerIn Key [0000] 2A 02 7E E1 D3 58 D8 12 4C 63 76 AE 59 17 5A E4 *.~..X.. Lcv.Y.Z.
ServerOut Key [0000] 59 F2 5B 7F 66 8F 31 A0 A5 E4 A8 D8 2F BA 00 38 Y.[.f.1. ..../..8
We can now simply pass -ouat:smb2_seskey_list:<sesid>,<seskey> to
wireshark or tshark:
$ tshark -ouat:smb2_seskey_list:2648BFFD00000000,63D6CABC08C84AD245F6AE35AB4AB33B \
-Y smb2 -r capture.pcap -Tfields -e _ws.col.Info
Negotiate Protocol Response
Negotiate Protocol Request
Negotiate Protocol Response
Session Setup Request, NTLMSSP_NEGOTIATE
Session Setup Response, Error: STATUS_MORE_PROCESSING_REQUIRED, NTLMSSP_CHALLENGE
Session Setup Request, NTLMSSP_AUTH, User: WORKGROUP\aaptel
Session Setup Response
Tree Connect Request Tree: \\localhost\IPC$
Tree Connect Response
Decrypted SMB3;Ioctl Request FSCTL_DFS_GET_REFERRALS, File: \localhost\scratch
Decrypted SMB3;Ioctl Response, Error: STATUS_NOT_FOUND
Decrypted SMB3;Tree Disconnect Request
Decrypted SMB3;Tree Disconnect Response
Decrypted SMB3;Tree Connect Request Tree: \\localhost\scratch
Decrypted SMB3;Tree Connect Response
Decrypted SMB3;Tree Disconnect Request
Decrypted SMB3;Tree Disconnect Response
For more info on Wireshark decryption support see
https://wiki.samba.org/index.php/Wireshark_Decryption
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
Reviewed-by: Noel Power <npower@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
Reviewed-by: David Disseldorp <ddiss@samba.org>
Autobuild-User(master): David Disseldorp <ddiss@samba.org>
Autobuild-Date(master): Sat Feb 9 21:43:25 CET 2019 on sn-devel-144
2019-02-08 14:13:25 +03:00
if ( smbXcli_conn_protocol ( state - > cli - > conn ) > = PROTOCOL_SMB3_00
& & lp_debug_encryption ( ) )
{
cli_session_dump_keys ( state , session , state - > session_key ) ;
}
2016-03-01 20:31:50 +03:00
} else {
struct smbXcli_session * session = state - > cli - > smb1 . session ;
bool active ;
status = smb1cli_session_set_session_key ( session ,
state - > session_key ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
active = smb1cli_conn_activate_signing ( state - > cli - > conn ,
state - > session_key ,
data_blob_null ) ;
if ( active ) {
bool ok ;
ok = smb1cli_conn_check_signing ( state - > cli - > conn ,
state - > inbuf , 1 ) ;
if ( ! ok ) {
tevent_req_nterror ( req , NT_STATUS_ACCESS_DENIED ) ;
return ;
}
}
}
tevent_req_done ( req ) ;
2009-12-23 01:31:20 +03:00
}
2016-03-01 17:47:11 +03:00
static NTSTATUS cli_session_setup_gensec_recv ( struct tevent_req * req )
2009-12-23 01:31:20 +03:00
{
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state * state =
2016-03-01 20:31:50 +03:00
tevent_req_data ( req ,
2016-03-01 17:47:11 +03:00
struct cli_session_setup_gensec_state ) ;
2009-12-23 01:31:20 +03:00
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
2011-07-21 11:20:43 +04:00
cli_state_set_uid ( state - > cli , UID_FIELD_INVALID ) ;
2009-12-23 01:31:20 +03:00
return status ;
}
return NT_STATUS_OK ;
}
2013-03-09 12:53:42 +04:00
static char * cli_session_setup_get_account ( TALLOC_CTX * mem_ctx ,
const char * principal )
{
char * account , * p ;
account = talloc_strdup ( mem_ctx , principal ) ;
if ( account = = NULL ) {
return NULL ;
}
p = strchr_m ( account , ' @ ' ) ;
if ( p ! = NULL ) {
* p = ' \0 ' ;
}
return account ;
}
2001-10-12 08:49:42 +04:00
/****************************************************************************
2002-07-15 14:35:28 +04:00
Do a spnego encrypted session setup .
2008-05-24 01:01:45 +04:00
user_domain : The shortname of the domain the user / machine is a member of .
dest_realm : The realm we ' re connecting to , if NULL we use our default realm .
2001-10-12 08:49:42 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
2013-03-09 16:07:57 +04:00
struct cli_session_setup_spnego_state {
ADS_STATUS result ;
} ;
2016-10-22 10:52:48 +03:00
static void cli_session_setup_spnego_done ( struct tevent_req * subreq ) ;
2013-03-09 16:07:57 +04:00
static struct tevent_req * cli_session_setup_spnego_send (
TALLOC_CTX * mem_ctx , struct tevent_context * ev , struct cli_state * cli ,
2016-10-22 12:24:48 +03:00
struct cli_credentials * creds )
2001-10-12 08:49:42 +04:00
{
2013-03-09 16:07:57 +04:00
struct tevent_req * req , * subreq ;
struct cli_session_setup_spnego_state * state ;
2016-12-08 14:11:45 +03:00
const char * target_service = NULL ;
2016-10-22 10:52:48 +03:00
const char * target_hostname = NULL ;
2002-07-15 14:35:28 +04:00
2013-03-09 16:07:57 +04:00
req = tevent_req_create ( mem_ctx , & state ,
struct cli_session_setup_spnego_state ) ;
if ( req = = NULL ) {
return NULL ;
}
2016-12-08 14:11:45 +03:00
target_service = " cifs " ;
2016-10-22 10:52:48 +03:00
target_hostname = smbXcli_conn_remote_name ( cli - > conn ) ;
2019-03-27 18:45:39 +03:00
DBG_INFO ( " Connect to %s as %s using SPNEGO \n " ,
target_hostname ,
cli_credentials_get_principal ( creds , talloc_tos ( ) ) ) ;
2016-10-21 21:35:01 +03:00
subreq = cli_session_setup_gensec_send ( state , ev , cli , creds ,
2016-12-08 14:11:45 +03:00
target_service , target_hostname ) ;
2013-03-09 16:07:57 +04:00
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback (
2016-10-22 10:52:48 +03:00
subreq , cli_session_setup_spnego_done , req ) ;
2013-03-09 16:07:57 +04:00
return req ;
}
2016-10-22 10:52:48 +03:00
static void cli_session_setup_spnego_done ( struct tevent_req * subreq )
2013-03-09 16:07:57 +04:00
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
2016-03-02 16:58:30 +03:00
NTSTATUS status ;
2001-10-14 09:42:28 +04:00
2016-03-02 16:58:30 +03:00
status = cli_session_setup_gensec_recv ( subreq ) ;
2013-03-09 16:07:57 +04:00
TALLOC_FREE ( subreq ) ;
2016-10-22 10:52:48 +03:00
if ( tevent_req_nterror ( req , status ) ) {
2013-03-09 16:07:57 +04:00
return ;
2008-05-06 11:48:16 +04:00
}
2013-03-09 16:07:57 +04:00
tevent_req_done ( req ) ;
}
static ADS_STATUS cli_session_setup_spnego_recv ( struct tevent_req * req )
{
struct cli_session_setup_spnego_state * state = tevent_req_data (
req , struct cli_session_setup_spnego_state ) ;
2016-10-22 10:52:48 +03:00
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
state - > result = ADS_ERROR_NT ( status ) ;
}
2013-03-09 16:07:57 +04:00
return state - > result ;
}
2016-10-26 14:17:59 +03:00
struct cli_session_setup_creds_state {
2016-10-26 13:01:45 +03:00
struct cli_state * cli ;
DATA_BLOB apassword_blob ;
DATA_BLOB upassword_blob ;
DATA_BLOB lm_session_key ;
DATA_BLOB session_key ;
char * out_native_os ;
char * out_native_lm ;
char * out_primary_domain ;
2013-04-02 15:37:30 +04:00
} ;
2016-10-26 14:17:59 +03:00
static void cli_session_setup_creds_cleanup ( struct tevent_req * req ,
enum tevent_req_state req_state )
2016-10-26 13:01:45 +03:00
{
2016-10-26 14:17:59 +03:00
struct cli_session_setup_creds_state * state = tevent_req_data (
req , struct cli_session_setup_creds_state ) ;
2016-10-26 13:01:45 +03:00
if ( req_state ! = TEVENT_REQ_RECEIVED ) {
return ;
}
/*
* We only call data_blob_clear ( ) as
* some of the blobs point to the same memory .
*
2019-08-29 22:50:45 +03:00
* We let the talloc hierarchy free the memory .
2016-10-26 13:01:45 +03:00
*/
data_blob_clear ( & state - > apassword_blob ) ;
data_blob_clear ( & state - > upassword_blob ) ;
data_blob_clear ( & state - > lm_session_key ) ;
data_blob_clear ( & state - > session_key ) ;
ZERO_STRUCTP ( state ) ;
}
2016-10-26 14:17:59 +03:00
static void cli_session_setup_creds_done_spnego ( struct tevent_req * subreq ) ;
static void cli_session_setup_creds_done_nt1 ( struct tevent_req * subreq ) ;
static void cli_session_setup_creds_done_lm21 ( struct tevent_req * subreq ) ;
2013-04-02 15:37:30 +04:00
2000-04-25 18:04:06 +04:00
/****************************************************************************
2001-02-14 08:34:50 +03:00
Send a session setup . The username and workgroup is in UNIX character
format and must be converted to DOS codepage format before sending . If the
password is in plaintext , the same should be done .
2000-04-25 18:04:06 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
2016-10-26 14:17:59 +03:00
struct tevent_req * cli_session_setup_creds_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct cli_state * cli ,
struct cli_credentials * creds )
2000-04-25 18:04:06 +04:00
{
2013-04-02 15:37:30 +04:00
struct tevent_req * req , * subreq ;
2016-10-26 14:17:59 +03:00
struct cli_session_setup_creds_state * state ;
2012-05-19 20:47:31 +04:00
uint16_t sec_mode = smb1cli_conn_server_security_mode ( cli - > conn ) ;
2016-11-07 17:06:09 +03:00
bool use_spnego = false ;
2016-10-26 13:01:45 +03:00
int flags = 0 ;
2016-10-26 13:01:45 +03:00
const char * username = " " ;
const char * domain = " " ;
DATA_BLOB target_info = data_blob_null ;
DATA_BLOB challenge = data_blob_null ;
uint16_t in_buf_size = 0 ;
uint16_t in_mpx_max = 0 ;
uint16_t in_vc_num = 0 ;
uint32_t in_sess_key = 0 ;
const char * in_native_os = NULL ;
const char * in_native_lm = NULL ;
2016-10-27 11:40:28 +03:00
enum credentials_use_kerberos krb5_state =
cli_credentials_get_kerberos_state ( creds ) ;
2016-10-26 13:01:45 +03:00
NTSTATUS status ;
2000-12-04 07:26:22 +03:00
2013-04-02 15:37:30 +04:00
req = tevent_req_create ( mem_ctx , & state ,
2016-10-26 14:17:59 +03:00
struct cli_session_setup_creds_state ) ;
2013-04-02 15:37:30 +04:00
if ( req = = NULL ) {
return NULL ;
}
2016-10-26 13:01:45 +03:00
state - > cli = cli ;
2016-10-26 14:17:59 +03:00
tevent_req_set_cleanup_fn ( req , cli_session_setup_creds_cleanup ) ;
2013-04-02 15:37:30 +04:00
2016-10-26 03:03:17 +03:00
/*
* Now work out what sort of session setup we are going to
* do . I have split this into separate functions to make the flow a bit
* easier to understand ( tridge ) .
*/
2016-11-07 17:06:09 +03:00
if ( smbXcli_conn_protocol ( cli - > conn ) < PROTOCOL_NT1 ) {
use_spnego = false ;
} else if ( smbXcli_conn_protocol ( cli - > conn ) > = PROTOCOL_SMB2_02 ) {
use_spnego = true ;
} else if ( smb1cli_conn_capabilities ( cli - > conn ) & CAP_EXTENDED_SECURITY ) {
/*
* if the server supports extended security then use SPNEGO
* even for anonymous connections .
*/
use_spnego = true ;
} else {
use_spnego = false ;
}
if ( use_spnego ) {
subreq = cli_session_setup_spnego_send (
2016-10-22 12:24:48 +03:00
state , ev , cli , creds ) ;
2016-11-07 17:06:09 +03:00
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
2016-10-26 14:17:59 +03:00
tevent_req_set_callback ( subreq , cli_session_setup_creds_done_spnego ,
2016-11-07 17:06:09 +03:00
req ) ;
return req ;
}
2016-10-27 11:40:28 +03:00
if ( krb5_state = = CRED_USE_KERBEROS_REQUIRED ) {
DBG_WARNING ( " Kerberos authentication requested, but "
" the server does not support SPNEGO authentication \n " ) ;
tevent_req_nterror ( req , NT_STATUS_NETWORK_CREDENTIAL_CONFLICT ) ;
return tevent_req_post ( req , ev ) ;
}
2012-05-19 20:09:30 +04:00
if ( smbXcli_conn_protocol ( cli - > conn ) < PROTOCOL_LANMAN1 ) {
2016-10-26 03:03:17 +03:00
/*
* SessionSetupAndX was introduced by LANMAN 1.0 . So we skip
* this step against older servers .
*/
2013-04-02 15:37:30 +04:00
tevent_req_done ( req ) ;
return tevent_req_post ( req , ev ) ;
2006-08-16 21:14:16 +04:00
}
2000-04-25 18:04:06 +04:00
2016-10-26 13:01:45 +03:00
if ( cli_credentials_is_anonymous ( creds ) ) {
2016-10-26 13:01:45 +03:00
/*
* Do an anonymous session setup
*/
goto non_spnego_creds_done ;
}
2002-09-25 19:19:00 +04:00
2016-10-26 13:01:45 +03:00
if ( ( sec_mode & NEGOTIATE_SECURITY_USER_LEVEL ) = = 0 ) {
/*
* Do an anonymous session setup ,
* the password is passed via the tree connect .
*/
goto non_spnego_creds_done ;
}
2016-10-26 13:01:45 +03:00
cli_credentials_get_ntlm_username_domain ( creds , state ,
& username ,
& domain ) ;
if ( tevent_req_nomem ( username , req ) ) {
return tevent_req_post ( req , ev ) ;
}
if ( tevent_req_nomem ( domain , req ) ) {
return tevent_req_post ( req , ev ) ;
2016-10-26 13:01:45 +03:00
}
2019-03-27 18:45:39 +03:00
DBG_INFO ( " Connect to %s as %s using NTLM \n " , domain , username ) ;
2016-10-26 13:01:45 +03:00
if ( ( sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE ) = = 0 ) {
bool use_unicode = smbXcli_conn_use_unicode ( cli - > conn ) ;
uint8_t * bytes = NULL ;
size_t bytes_len = 0 ;
2016-10-26 13:01:45 +03:00
const char * pw = cli_credentials_get_password ( creds ) ;
2016-10-26 13:01:45 +03:00
size_t pw_len = 0 ;
if ( pw = = NULL ) {
pw = " " ;
2003-06-08 16:49:31 +04:00
}
2016-10-26 13:01:45 +03:00
pw_len = strlen ( pw ) + 1 ;
2003-06-08 16:49:31 +04:00
2016-10-26 13:01:45 +03:00
if ( ! lp_client_plaintext_auth ( ) ) {
DEBUG ( 1 , ( " Server requested PLAINTEXT password but "
" 'client plaintext auth = no' \n " ) ) ;
2013-04-02 15:37:30 +04:00
tevent_req_nterror ( req , NT_STATUS_ACCESS_DENIED ) ;
return tevent_req_post ( req , ev ) ;
2003-06-08 16:49:31 +04:00
}
2016-10-26 13:01:45 +03:00
bytes = talloc_array ( state , uint8_t , 0 ) ;
bytes = trans2_bytes_push_str ( bytes , use_unicode ,
pw , pw_len , & bytes_len ) ;
if ( tevent_req_nomem ( bytes , req ) ) {
2013-04-02 15:37:30 +04:00
return tevent_req_post ( req , ev ) ;
}
2003-08-19 21:47:44 +04:00
2016-10-26 13:01:45 +03:00
if ( use_unicode ) {
/*
* CAP_UNICODE , can only be negotiated by NT1 .
*/
state - > upassword_blob = data_blob_const ( bytes ,
bytes_len ) ;
} else {
state - > apassword_blob = data_blob_const ( bytes ,
bytes_len ) ;
2013-04-02 15:37:30 +04:00
}
2016-10-26 13:01:45 +03:00
goto non_spnego_creds_done ;
2013-04-02 15:37:30 +04:00
}
2003-08-19 21:47:44 +04:00
2016-10-26 13:01:45 +03:00
challenge = data_blob_const ( smb1cli_conn_server_challenge ( cli - > conn ) , 8 ) ;
2002-09-25 19:19:00 +04:00
2016-10-26 13:01:45 +03:00
if ( smbXcli_conn_protocol ( cli - > conn ) = = PROTOCOL_NT1 ) {
if ( lp_client_ntlmv2_auth ( ) & & lp_client_use_spnego ( ) ) {
/*
* Don ' t send an NTLMv2 response without NTLMSSP if we
* want to use spnego support .
*/
DEBUG ( 1 , ( " Server does not support EXTENDED_SECURITY "
" but 'client use spnego = yes' "
" and 'client ntlmv2 auth = yes' is set \n " ) ) ;
tevent_req_nterror ( req , NT_STATUS_ACCESS_DENIED ) ;
2013-04-02 15:37:30 +04:00
return tevent_req_post ( req , ev ) ;
}
2000-04-25 18:04:06 +04:00
2016-10-26 13:01:45 +03:00
if ( lp_client_ntlmv2_auth ( ) ) {
2016-10-26 13:01:45 +03:00
flags | = CLI_CRED_NTLMv2_AUTH ;
2002-09-25 19:19:00 +04:00
2016-10-26 13:01:45 +03:00
/*
* note that the ' domain ' here is a best
* guess - we don ' t know the server ' s domain
* at this point . Windows clients also don ' t
* use hostname . . .
*/
target_info = NTLMv2_generate_names_blob ( state ,
NULL ,
domain ) ;
if ( tevent_req_nomem ( target_info . data , req ) ) {
return tevent_req_post ( req , ev ) ;
}
} else {
2016-10-26 13:01:45 +03:00
flags | = CLI_CRED_NTLM_AUTH ;
2016-10-26 13:01:45 +03:00
if ( lp_client_lanman_auth ( ) ) {
2016-10-26 13:01:45 +03:00
flags | = CLI_CRED_LANMAN_AUTH ;
2016-10-26 13:01:45 +03:00
}
}
} else {
if ( ! lp_client_lanman_auth ( ) ) {
2016-12-07 19:18:01 +03:00
DEBUG ( 1 , ( " Server requested user level LM password but "
" 'client lanman auth = no' is set. \n " ) ) ;
2013-04-02 15:37:30 +04:00
tevent_req_nterror ( req , NT_STATUS_ACCESS_DENIED ) ;
return tevent_req_post ( req , ev ) ;
}
2016-10-26 13:01:45 +03:00
2016-10-26 13:01:45 +03:00
flags | = CLI_CRED_LANMAN_AUTH ;
2016-10-26 13:01:45 +03:00
}
2016-10-26 13:01:45 +03:00
status = cli_credentials_get_ntlm_response ( creds , state , & flags ,
challenge , NULL ,
target_info ,
& state - > apassword_blob ,
& state - > upassword_blob ,
& state - > lm_session_key ,
& state - > session_key ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
2016-10-26 13:01:45 +03:00
}
non_spnego_creds_done :
in_buf_size = CLI_BUFFER_SIZE ;
in_mpx_max = smbXcli_conn_max_requests ( cli - > conn ) ;
in_vc_num = cli_state_get_vc_num ( cli ) ;
in_sess_key = smb1cli_conn_server_session_key ( cli - > conn ) ;
in_native_os = " Unix " ;
in_native_lm = " Samba " ;
if ( smbXcli_conn_protocol ( cli - > conn ) = = PROTOCOL_NT1 ) {
uint32_t in_capabilities = 0 ;
in_capabilities = cli_session_setup_capabilities ( cli , 0 ) ;
/*
* For now we keep the same values as before ,
* we may remove these in a separate commit later .
*/
in_mpx_max = 2 ;
subreq = smb1cli_session_setup_nt1_send ( state , ev ,
cli - > conn ,
cli - > timeout ,
cli - > smb1 . pid ,
cli - > smb1 . session ,
in_buf_size ,
in_mpx_max ,
in_vc_num ,
in_sess_key ,
username ,
domain ,
state - > apassword_blob ,
state - > upassword_blob ,
in_capabilities ,
in_native_os ,
in_native_lm ) ;
2013-04-02 15:37:30 +04:00
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
2006-02-04 00:19:24 +03:00
}
2016-10-26 14:17:59 +03:00
tevent_req_set_callback ( subreq , cli_session_setup_creds_done_nt1 ,
2013-04-02 15:37:30 +04:00
req ) ;
return req ;
Changes all over the shop, but all towards:
- NTLM2 support in the server
- KEY_EXCH support in the server
- variable length session keys.
In detail:
- NTLM2 is an extension of NTLMv1, that is compatible with existing
domain controllers (unlike NTLMv2, which requires a DC upgrade).
* This is known as 'NTLMv2 session security' *
(This is not yet implemented on the RPC pipes however, so there may
well still be issues for PDC setups, particuarly around password
changes. We do not fully understand the sign/seal implications of
NTLM2 on RPC pipes.)
This requires modifications to our authentication subsystem, as we
must handle the 'challege' input into the challenge-response algorithm
being changed. This also needs to be turned off for
'security=server', which does not support this.
- KEY_EXCH is another 'security' mechanism, whereby the session key
actually used by the server is sent by the client, rather than being
the shared-secret directly or indirectly.
- As both these methods change the session key, the auth subsystem
needed to be changed, to 'override' session keys provided by the
backend.
- There has also been a major overhaul of the NTLMSSP subsystem, to merge the 'client' and 'server' functions, so they both operate on a single structure. This should help the SPNEGO implementation.
- The 'names blob' in NTLMSSP is always in unicode - never in ascii.
Don't make an ascii version ever.
- The other big change is to allow variable length session keys. We
have always assumed that session keys are 16 bytes long - and padded
to this length if shorter. However, Kerberos session keys are 8 bytes
long, when the krb5 login uses DES.
* This fix allows SMB signging on machines not yet running MIT KRB5 1.3.1. *
- Add better DEBUG() messages to ntlm_auth, warning administrators of
misconfigurations that prevent access to the privileged pipe. This
should help reduce some of the 'it just doesn't work' issues.
- Fix data_blob_talloc() to behave the same way data_blob() does when
passed a NULL data pointer. (just allocate)
REMEMBER to make clean after this commit - I have changed plenty of data structures...
(This used to be commit f3bbc87b0dac63426cda6fac7a295d3aad810ecc)
2003-11-22 16:19:38 +03:00
}
2000-04-25 18:04:06 +04:00
2016-10-26 13:01:45 +03:00
/*
* For now we keep the same values as before ,
* we may remove these in a separate commit later .
*/
in_mpx_max = 2 ;
in_vc_num = 1 ;
subreq = smb1cli_session_setup_lm21_send ( state , ev ,
cli - > conn ,
cli - > timeout ,
cli - > smb1 . pid ,
cli - > smb1 . session ,
in_buf_size ,
in_mpx_max ,
in_vc_num ,
in_sess_key ,
username ,
domain ,
state - > apassword_blob ,
in_native_os ,
in_native_lm ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
2013-04-02 15:37:30 +04:00
}
2016-10-26 14:17:59 +03:00
tevent_req_set_callback ( subreq , cli_session_setup_creds_done_lm21 ,
2016-10-26 13:01:45 +03:00
req ) ;
return req ;
2013-04-02 15:37:30 +04:00
}
2016-10-26 14:17:59 +03:00
static void cli_session_setup_creds_done_spnego ( struct tevent_req * subreq )
2013-04-02 15:37:30 +04:00
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
ADS_STATUS status ;
status = cli_session_setup_spnego_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( ! ADS_ERR_OK ( status ) ) {
DEBUG ( 3 , ( " SPNEGO login failed: %s \n " , ads_errstr ( status ) ) ) ;
tevent_req_nterror ( req , ads_ntstatus ( status ) ) ;
return ;
}
tevent_req_done ( req ) ;
}
2016-10-26 14:17:59 +03:00
static void cli_session_setup_creds_done_nt1 ( struct tevent_req * subreq )
2013-04-02 15:37:30 +04:00
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
2016-10-26 14:17:59 +03:00
struct cli_session_setup_creds_state * state = tevent_req_data (
req , struct cli_session_setup_creds_state ) ;
2016-10-26 13:01:45 +03:00
struct cli_state * cli = state - > cli ;
2013-04-02 15:37:30 +04:00
NTSTATUS status ;
2016-10-26 13:01:45 +03:00
struct iovec * recv_iov = NULL ;
const uint8_t * inbuf = NULL ;
bool ok ;
status = smb1cli_session_setup_nt1_recv ( subreq , state ,
& recv_iov ,
& inbuf ,
& state - > out_native_os ,
& state - > out_native_lm ,
& state - > out_primary_domain ) ;
2013-04-02 15:37:30 +04:00
TALLOC_FREE ( subreq ) ;
2023-08-16 12:59:03 +03:00
if ( tevent_req_nterror ( req , status ) ) {
2016-10-26 13:01:45 +03:00
DEBUG ( 3 , ( " NT1 login failed: %s \n " , nt_errstr ( status ) ) ) ;
2013-04-02 15:37:30 +04:00
return ;
}
2017-03-15 20:04:44 +03:00
status = cli_state_update_after_sesssetup ( state - > cli ,
state - > out_native_os ,
state - > out_native_lm ,
state - > out_primary_domain ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
2016-10-26 13:01:45 +03:00
}
2013-04-02 15:37:30 +04:00
2016-10-26 13:01:45 +03:00
ok = smb1cli_conn_activate_signing ( cli - > conn ,
state - > session_key ,
state - > upassword_blob ) ;
if ( ok ) {
ok = smb1cli_conn_check_signing ( cli - > conn , inbuf , 1 ) ;
if ( ! ok ) {
tevent_req_nterror ( req , NT_STATUS_ACCESS_DENIED ) ;
return ;
}
}
if ( state - > session_key . data ) {
struct smbXcli_session * session = cli - > smb1 . session ;
status = smb1cli_session_set_session_key ( session ,
state - > session_key ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2013-04-02 15:37:30 +04:00
}
2016-10-26 13:01:45 +03:00
2013-04-02 15:37:30 +04:00
tevent_req_done ( req ) ;
}
2016-10-26 14:17:59 +03:00
static void cli_session_setup_creds_done_lm21 ( struct tevent_req * subreq )
2013-04-02 15:37:30 +04:00
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
2016-10-26 14:17:59 +03:00
struct cli_session_setup_creds_state * state = tevent_req_data (
req , struct cli_session_setup_creds_state ) ;
2013-04-02 15:37:30 +04:00
NTSTATUS status ;
2016-10-26 13:01:45 +03:00
status = smb1cli_session_setup_lm21_recv ( subreq , state ,
& state - > out_native_os ,
& state - > out_native_lm ) ;
2013-04-02 15:37:30 +04:00
TALLOC_FREE ( subreq ) ;
2023-08-16 12:59:03 +03:00
if ( tevent_req_nterror ( req , status ) ) {
2016-10-26 13:01:45 +03:00
DEBUG ( 3 , ( " LM21 login failed: %s \n " , nt_errstr ( status ) ) ) ;
2013-04-02 15:37:30 +04:00
return ;
}
2016-10-26 13:01:45 +03:00
2017-03-15 20:04:44 +03:00
status = cli_state_update_after_sesssetup ( state - > cli ,
state - > out_native_os ,
state - > out_native_lm ,
NULL ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
2016-10-26 13:01:45 +03:00
}
2013-04-02 15:37:30 +04:00
tevent_req_done ( req ) ;
}
2016-10-26 14:17:59 +03:00
NTSTATUS cli_session_setup_creds_recv ( struct tevent_req * req )
2013-04-02 15:37:30 +04:00
{
return tevent_req_simple_recv_ntstatus ( req ) ;
}
2016-10-26 14:17:59 +03:00
NTSTATUS cli_session_setup_creds ( struct cli_state * cli ,
struct cli_credentials * creds )
2013-04-02 15:37:30 +04:00
{
struct tevent_context * ev ;
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_NO_MEMORY ;
if ( smbXcli_conn_has_async_calls ( cli - > conn ) ) {
return NT_STATUS_INVALID_PARAMETER ;
}
ev = samba_tevent_context_init ( talloc_tos ( ) ) ;
if ( ev = = NULL ) {
goto fail ;
}
2016-10-26 14:17:59 +03:00
req = cli_session_setup_creds_send ( ev , ev , cli , creds ) ;
2013-04-02 15:37:30 +04:00
if ( req = = NULL ) {
goto fail ;
}
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
goto fail ;
}
2016-10-26 14:17:59 +03:00
status = cli_session_setup_creds_recv ( req ) ;
2013-04-02 15:37:30 +04:00
fail :
TALLOC_FREE ( ev ) ;
return status ;
2000-04-25 18:04:06 +04:00
}
2016-10-28 13:01:50 +03:00
NTSTATUS cli_session_setup_anon ( struct cli_state * cli )
{
2019-08-15 16:50:33 +03:00
NTSTATUS status ;
2016-10-28 13:01:50 +03:00
struct cli_credentials * creds = NULL ;
creds = cli_credentials_init_anon ( cli ) ;
if ( creds = = NULL ) {
return NT_STATUS_NO_MEMORY ;
}
status = cli_session_setup_creds ( cli , creds ) ;
TALLOC_FREE ( creds ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
return NT_STATUS_OK ;
}
2000-04-25 18:04:06 +04:00
/****************************************************************************
Send a uloggoff .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-01-03 15:03:42 +03:00
struct cli_ulogoff_state {
struct cli_state * cli ;
2010-09-17 12:33:08 +04:00
uint16_t vwv [ 3 ] ;
2010-01-03 15:03:42 +03:00
} ;
static void cli_ulogoff_done ( struct tevent_req * subreq ) ;
2013-08-14 13:48:40 +04:00
static struct tevent_req * cli_ulogoff_send ( TALLOC_CTX * mem_ctx ,
2010-01-03 15:03:42 +03:00
struct tevent_context * ev ,
struct cli_state * cli )
2000-04-25 18:04:06 +04:00
{
2010-01-03 15:03:42 +03:00
struct tevent_req * req , * subreq ;
struct cli_ulogoff_state * state ;
2000-04-25 18:04:06 +04:00
2010-01-03 15:03:42 +03:00
req = tevent_req_create ( mem_ctx , & state , struct cli_ulogoff_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > cli = cli ;
2000-04-25 18:04:06 +04:00
2010-01-03 15:03:42 +03:00
SCVAL ( state - > vwv + 0 , 0 , 0xFF ) ;
SCVAL ( state - > vwv + 1 , 0 , 0 ) ;
SSVAL ( state - > vwv + 2 , 0 , 0 ) ;
2016-08-18 22:20:25 +03:00
subreq = cli_smb_send ( state , ev , cli , SMBulogoffX , 0 , 0 , 2 , state - > vwv ,
2010-01-03 15:03:42 +03:00
0 , NULL ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_ulogoff_done , req ) ;
return req ;
}
static void cli_ulogoff_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_ulogoff_state * state = tevent_req_data (
req , struct cli_ulogoff_state ) ;
NTSTATUS status ;
2010-02-20 11:53:58 +03:00
status = cli_smb_recv ( subreq , NULL , NULL , 0 , NULL , NULL , NULL , NULL ) ;
2023-08-16 12:59:03 +03:00
if ( tevent_req_nterror ( req , status ) ) {
2010-01-03 15:03:42 +03:00
return ;
2005-03-10 23:14:24 +03:00
}
2011-07-21 11:20:43 +04:00
cli_state_set_uid ( state - > cli , UID_FIELD_INVALID ) ;
2010-01-03 15:03:42 +03:00
tevent_req_done ( req ) ;
}
2013-08-14 13:48:40 +04:00
static NTSTATUS cli_ulogoff_recv ( struct tevent_req * req )
2010-01-03 15:03:42 +03:00
{
return tevent_req_simple_recv_ntstatus ( req ) ;
}
2005-03-10 23:14:24 +03:00
2010-01-03 15:03:42 +03:00
NTSTATUS cli_ulogoff ( struct cli_state * cli )
{
struct tevent_context * ev ;
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_NO_MEMORY ;
2013-08-15 02:18:47 +04:00
if ( smbXcli_conn_protocol ( cli - > conn ) > = PROTOCOL_SMB2_02 ) {
status = smb2cli_logoff ( cli - > conn ,
cli - > timeout ,
cli - > smb2 . session ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
smb2cli_session_set_id_and_flags ( cli - > smb2 . session ,
UINT64_MAX , 0 ) ;
return NT_STATUS_OK ;
}
2012-05-26 13:45:09 +04:00
if ( smbXcli_conn_has_async_calls ( cli - > conn ) ) {
2010-01-03 15:03:42 +03:00
return NT_STATUS_INVALID_PARAMETER ;
}
2013-02-18 12:08:19 +04:00
ev = samba_tevent_context_init ( talloc_tos ( ) ) ;
2010-01-03 15:03:42 +03:00
if ( ev = = NULL ) {
goto fail ;
}
req = cli_ulogoff_send ( ev , ev , cli ) ;
if ( req = = NULL ) {
goto fail ;
}
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
goto fail ;
}
status = cli_ulogoff_recv ( req ) ;
fail :
TALLOC_FREE ( ev ) ;
return status ;
2000-04-25 18:04:06 +04:00
}
/****************************************************************************
2002-07-15 14:35:28 +04:00
Send a tconX .
2000-04-25 18:04:06 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-05-15 08:51:46 +04:00
2009-03-29 21:47:18 +04:00
struct cli_tcon_andx_state {
struct cli_state * cli ;
uint16_t vwv [ 4 ] ;
2009-05-07 18:24:04 +04:00
struct iovec bytes ;
2009-03-29 21:47:18 +04:00
} ;
static void cli_tcon_andx_done ( struct tevent_req * subreq ) ;
2009-05-07 18:24:04 +04:00
struct tevent_req * cli_tcon_andx_create ( TALLOC_CTX * mem_ctx ,
2013-02-18 12:59:58 +04:00
struct tevent_context * ev ,
2009-05-07 18:24:04 +04:00
struct cli_state * cli ,
const char * share , const char * dev ,
const char * pass , int passlen ,
struct tevent_req * * psmbreq )
2000-04-25 18:04:06 +04:00
{
2009-03-29 21:47:18 +04:00
struct tevent_req * req , * subreq ;
struct cli_tcon_andx_state * state ;
2011-04-28 04:16:36 +04:00
uint8_t p24 [ 24 ] ;
2009-03-29 21:47:18 +04:00
uint16_t * vwv ;
2009-01-26 10:37:13 +03:00
char * tmp = NULL ;
uint8_t * bytes ;
2012-05-19 20:47:31 +04:00
uint16_t sec_mode = smb1cli_conn_server_security_mode ( cli - > conn ) ;
2012-08-01 13:45:36 +04:00
uint16_t tcon_flags = 0 ;
2000-04-25 18:04:06 +04:00
2010-01-04 23:57:34 +03:00
* psmbreq = NULL ;
2009-03-29 21:47:18 +04:00
req = tevent_req_create ( mem_ctx , & state , struct cli_tcon_andx_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > cli = cli ;
vwv = state - > vwv ;
2017-06-14 02:37:39 +03:00
TALLOC_FREE ( cli - > smb1 . tcon ) ;
cli - > smb1 . tcon = smbXcli_tcon_create ( cli ) ;
if ( tevent_req_nomem ( cli - > smb1 . tcon , req ) ) {
return tevent_req_post ( req , ev ) ;
}
smb1cli_tcon_set_id ( cli - > smb1 . tcon , UINT16_MAX ) ;
2011-04-27 23:06:25 +04:00
cli - > share = talloc_strdup ( cli , share ) ;
if ( ! cli - > share ) {
return NULL ;
}
2000-04-25 18:04:06 +04:00
/* in user level security don't send a password now */
2011-09-13 18:47:24 +04:00
if ( sec_mode & NEGOTIATE_SECURITY_USER_LEVEL ) {
2000-04-25 18:04:06 +04:00
passlen = 1 ;
pass = " " ;
2009-01-26 10:37:13 +03:00
} else if ( pass = = NULL ) {
DEBUG ( 1 , ( " Server not using user level security and no "
" password supplied. \n " ) ) ;
goto access_denied ;
2000-04-25 18:04:06 +04:00
}
2011-09-13 18:47:24 +04:00
if ( ( sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE ) & &
2006-05-15 08:51:46 +04:00
* pass & & passlen ! = 24 ) {
2003-06-08 16:49:31 +04:00
if ( ! lp_client_lanman_auth ( ) ) {
2008-04-07 12:18:36 +04:00
DEBUG ( 1 , ( " Server requested LANMAN password "
" (share-level security) but "
2010-12-13 04:40:25 +03:00
" 'client lanman auth = no' or 'client ntlmv2 auth = yes' \n " ) ) ;
2009-01-26 10:37:13 +03:00
goto access_denied ;
2003-06-08 16:49:31 +04:00
}
2000-04-25 18:04:06 +04:00
/*
2009-01-26 10:37:13 +03:00
* Non - encrypted passwords - convert to DOS codepage before
* encryption .
2000-04-25 18:04:06 +04:00
*/
2012-05-19 20:42:56 +04:00
SMBencrypt ( pass , smb1cli_conn_server_challenge ( cli - > conn ) , p24 ) ;
2000-04-25 18:04:06 +04:00
passlen = 24 ;
2011-04-28 04:16:36 +04:00
pass = ( const char * ) p24 ;
2000-04-25 18:04:06 +04:00
} else {
2011-09-13 18:47:24 +04:00
if ( ( sec_mode & ( NEGOTIATE_SECURITY_USER_LEVEL
2009-01-26 10:37:13 +03:00
| NEGOTIATE_SECURITY_CHALLENGE_RESPONSE ) )
= = 0 ) {
2011-07-07 12:55:00 +04:00
uint8_t * tmp_pass ;
2011-04-28 04:16:36 +04:00
2003-06-08 16:49:31 +04:00
if ( ! lp_client_plaintext_auth ( ) & & ( * pass ) ) {
2012-06-06 10:22:50 +04:00
DEBUG ( 1 , ( " Server requested PLAINTEXT "
2010-12-13 04:40:25 +03:00
" password but "
2012-06-06 10:22:50 +04:00
" 'client plaintext auth = no' or 'client ntlmv2 auth = yes' \n " ) ) ;
2009-01-26 10:37:13 +03:00
goto access_denied ;
2003-06-08 16:49:31 +04:00
}
2000-04-25 18:04:06 +04:00
/*
2009-01-26 10:37:13 +03:00
* Non - encrypted passwords - convert to DOS codepage
* before using .
2000-04-25 18:04:06 +04:00
*/
2015-05-10 02:59:45 +03:00
tmp_pass = talloc_array ( talloc_tos ( ) , uint8_t , 0 ) ;
2011-07-07 12:55:00 +04:00
if ( tevent_req_nomem ( tmp_pass , req ) ) {
2011-04-28 04:16:36 +04:00
return tevent_req_post ( req , ev ) ;
2009-02-03 12:21:51 +03:00
}
2011-07-07 12:55:00 +04:00
tmp_pass = trans2_bytes_push_str ( tmp_pass ,
false , /* always DOS */
pass ,
passlen ,
NULL ) ;
if ( tevent_req_nomem ( tmp_pass , req ) ) {
2011-04-28 04:16:36 +04:00
return tevent_req_post ( req , ev ) ;
2006-05-15 08:51:46 +04:00
}
2011-07-07 12:55:00 +04:00
pass = ( const char * ) tmp_pass ;
passlen = talloc_get_size ( tmp_pass ) ;
2000-04-25 18:04:06 +04:00
}
}
2012-08-01 13:45:36 +04:00
tcon_flags | = TCONX_FLAG_EXTENDED_RESPONSE ;
2012-08-01 13:47:07 +04:00
tcon_flags | = TCONX_FLAG_EXTENDED_SIGNATURES ;
2012-08-01 13:45:36 +04:00
2009-01-26 10:37:13 +03:00
SCVAL ( vwv + 0 , 0 , 0xFF ) ;
SCVAL ( vwv + 0 , 1 , 0 ) ;
SSVAL ( vwv + 1 , 0 , 0 ) ;
2012-08-01 13:45:36 +04:00
SSVAL ( vwv + 2 , 0 , tcon_flags ) ;
2009-01-26 10:37:13 +03:00
SSVAL ( vwv + 3 , 0 , passlen ) ;
2000-04-25 18:04:06 +04:00
2011-04-28 04:16:36 +04:00
if ( passlen & & pass ) {
bytes = ( uint8_t * ) talloc_memdup ( state , pass , passlen ) ;
2009-01-26 10:37:13 +03:00
} else {
2009-03-29 21:47:18 +04:00
bytes = talloc_array ( state , uint8_t , 0 ) ;
2009-01-26 10:37:13 +03:00
}
2000-04-25 18:04:06 +04:00
2009-01-26 10:37:13 +03:00
/*
* Add the sharename
*/
tmp = talloc_asprintf_strupper_m ( talloc_tos ( ) , " \\ \\ %s \\ %s " ,
2012-05-19 19:31:50 +04:00
smbXcli_conn_remote_name ( cli - > conn ) , share ) ;
2009-05-07 18:24:04 +04:00
if ( tmp = = NULL ) {
TALLOC_FREE ( req ) ;
return NULL ;
2009-01-26 10:37:13 +03:00
}
2012-05-26 14:14:51 +04:00
bytes = smb_bytes_push_str ( bytes , smbXcli_conn_use_unicode ( cli - > conn ) , tmp , strlen ( tmp ) + 1 ,
2009-01-26 10:37:13 +03:00
NULL ) ;
TALLOC_FREE ( tmp ) ;
/*
* Add the devicetype
*/
tmp = talloc_strdup_upper ( talloc_tos ( ) , dev ) ;
2009-05-07 18:24:04 +04:00
if ( tmp = = NULL ) {
TALLOC_FREE ( req ) ;
return NULL ;
2009-01-26 10:37:13 +03:00
}
bytes = smb_bytes_push_str ( bytes , false , tmp , strlen ( tmp ) + 1 , NULL ) ;
TALLOC_FREE ( tmp ) ;
2000-04-25 18:04:06 +04:00
2009-05-07 18:24:04 +04:00
if ( bytes = = NULL ) {
TALLOC_FREE ( req ) ;
return NULL ;
2006-05-15 08:51:46 +04:00
}
2001-02-20 11:09:06 +03:00
2009-05-12 22:45:37 +04:00
state - > bytes . iov_base = ( void * ) bytes ;
2009-05-07 18:24:04 +04:00
state - > bytes . iov_len = talloc_get_size ( bytes ) ;
2016-08-18 23:45:35 +03:00
subreq = cli_smb_req_create ( state , ev , cli , SMBtconX , 0 , 0 , 4 , vwv ,
2009-05-07 18:24:04 +04:00
1 , & state - > bytes ) ;
if ( subreq = = NULL ) {
TALLOC_FREE ( req ) ;
return NULL ;
2009-03-29 21:47:18 +04:00
}
tevent_req_set_callback ( subreq , cli_tcon_andx_done , req ) ;
2009-05-07 18:24:04 +04:00
* psmbreq = subreq ;
2009-03-29 21:47:18 +04:00
return req ;
2000-04-25 18:04:06 +04:00
2009-01-26 10:37:13 +03:00
access_denied :
2009-03-29 21:47:18 +04:00
tevent_req_nterror ( req , NT_STATUS_ACCESS_DENIED ) ;
return tevent_req_post ( req , ev ) ;
2009-01-26 10:37:13 +03:00
}
2000-04-25 18:04:06 +04:00
2009-05-07 18:24:04 +04:00
struct tevent_req * cli_tcon_andx_send ( TALLOC_CTX * mem_ctx ,
2013-02-18 12:59:58 +04:00
struct tevent_context * ev ,
2009-05-07 18:24:04 +04:00
struct cli_state * cli ,
const char * share , const char * dev ,
const char * pass , int passlen )
{
struct tevent_req * req , * subreq ;
2009-05-14 06:13:12 +04:00
NTSTATUS status ;
2009-05-07 18:24:04 +04:00
req = cli_tcon_andx_create ( mem_ctx , ev , cli , share , dev , pass , passlen ,
& subreq ) ;
2009-05-14 06:13:12 +04:00
if ( req = = NULL ) {
2009-05-07 18:24:04 +04:00
return NULL ;
}
2010-01-04 23:57:34 +03:00
if ( subreq = = NULL ) {
return req ;
}
2012-05-26 13:58:34 +04:00
status = smb1cli_req_chain_submit ( & subreq , 1 ) ;
2023-08-16 12:59:03 +03:00
if ( tevent_req_nterror ( req , status ) ) {
2009-05-14 06:13:12 +04:00
return tevent_req_post ( req , ev ) ;
}
2009-05-07 18:24:04 +04:00
return req ;
}
2009-03-29 21:47:18 +04:00
static void cli_tcon_andx_done ( struct tevent_req * subreq )
2009-01-26 10:37:13 +03:00
{
2009-03-29 21:47:18 +04:00
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_tcon_andx_state * state = tevent_req_data (
req , struct cli_tcon_andx_state ) ;
struct cli_state * cli = state - > cli ;
2010-02-20 17:16:55 +03:00
uint8_t * in ;
2012-06-04 18:26:27 +04:00
uint8_t * inhdr ;
2009-01-26 10:37:13 +03:00
uint8_t wct ;
uint16_t * vwv ;
2009-03-29 21:47:18 +04:00
uint32_t num_bytes ;
2009-01-26 10:37:13 +03:00
uint8_t * bytes ;
NTSTATUS status ;
2012-08-01 13:46:22 +04:00
uint16_t optional_support = 0 ;
2009-01-26 10:37:13 +03:00
2010-02-20 17:16:55 +03:00
status = cli_smb_recv ( subreq , state , & in , 0 , & wct , & vwv ,
2010-02-20 11:53:58 +03:00
& num_bytes , & bytes ) ;
2010-02-20 17:16:55 +03:00
TALLOC_FREE ( subreq ) ;
2023-08-16 12:59:03 +03:00
if ( tevent_req_nterror ( req , status ) ) {
2009-03-29 21:47:18 +04:00
return ;
2009-01-26 10:37:13 +03:00
}
2000-04-25 18:04:06 +04:00
2012-06-04 18:26:27 +04:00
inhdr = in + NBT_HDR_SIZE ;
2010-02-20 17:16:55 +03:00
2011-04-27 23:06:25 +04:00
if ( num_bytes ) {
2020-05-17 13:57:32 +03:00
if ( pull_string_talloc ( cli ,
( const char * ) inhdr ,
SVAL ( inhdr , HDR_FLG2 ) ,
& cli - > dev ,
bytes ,
num_bytes ,
STR_TERMINATE | STR_ASCII ) = = - 1 ) {
2011-04-27 23:06:25 +04:00
tevent_req_nterror ( req , NT_STATUS_NO_MEMORY ) ;
return ;
}
} else {
cli - > dev = talloc_strdup ( cli , " " ) ;
if ( cli - > dev = = NULL ) {
tevent_req_nterror ( req , NT_STATUS_NO_MEMORY ) ;
return ;
}
}
2000-04-25 18:04:06 +04:00
2012-05-19 20:09:30 +04:00
if ( ( smbXcli_conn_protocol ( cli - > conn ) > = PROTOCOL_NT1 ) & & ( num_bytes = = 3 ) ) {
2000-04-25 18:04:06 +04:00
/* almost certainly win95 - enable bug fixes */
cli - > win95 = True ;
}
2008-11-30 14:25:17 +03:00
2009-01-26 10:37:13 +03:00
/*
* Make sure that we have the optional support 16 - bit field . WCT > 2.
* Avoids issues when connecting to Win9x boxes sharing files
*/
2005-03-23 18:36:48 +03:00
2012-05-19 20:09:30 +04:00
if ( ( wct > 2 ) & & ( smbXcli_conn_protocol ( cli - > conn ) > = PROTOCOL_LANMAN2 ) ) {
2012-08-01 13:46:22 +04:00
optional_support = SVAL ( vwv + 2 , 0 ) ;
}
2012-08-01 13:47:07 +04:00
if ( optional_support & SMB_EXTENDED_SIGNATURES ) {
smb1cli_session_protect_session_key ( cli - > smb1 . session ) ;
}
2013-09-13 11:10:42 +04:00
smb1cli_tcon_set_values ( state - > cli - > smb1 . tcon ,
SVAL ( inhdr , HDR_TID ) ,
optional_support ,
0 , /* maximal_access */
0 , /* guest_maximal_access */
NULL , /* service */
NULL ) ; /* fs_type */
2009-03-29 21:47:18 +04:00
tevent_req_done ( req ) ;
}
NTSTATUS cli_tcon_andx_recv ( struct tevent_req * req )
{
return tevent_req_simple_recv_ntstatus ( req ) ;
2009-01-26 10:37:13 +03:00
}
NTSTATUS cli_tcon_andx ( struct cli_state * cli , const char * share ,
const char * dev , const char * pass , int passlen )
{
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2013-02-18 12:59:58 +04:00
struct tevent_context * ev ;
2009-03-29 21:47:18 +04:00
struct tevent_req * req ;
2013-04-05 17:02:51 +04:00
NTSTATUS status = NT_STATUS_NO_MEMORY ;
2009-01-26 10:37:13 +03:00
2012-05-26 13:45:09 +04:00
if ( smbXcli_conn_has_async_calls ( cli - > conn ) ) {
2009-01-26 10:37:13 +03:00
/*
* Can ' t use sync call while an async call is in flight
*/
status = NT_STATUS_INVALID_PARAMETER ;
goto fail ;
}
2013-02-18 12:08:19 +04:00
ev = samba_tevent_context_init ( frame ) ;
2009-01-26 10:37:13 +03:00
if ( ev = = NULL ) {
goto fail ;
}
req = cli_tcon_andx_send ( frame , ev , cli , share , dev , pass , passlen ) ;
if ( req = = NULL ) {
goto fail ;
}
2013-04-05 16:58:02 +04:00
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
2009-03-29 21:47:18 +04:00
goto fail ;
2009-01-26 10:37:13 +03:00
}
status = cli_tcon_andx_recv ( req ) ;
fail :
TALLOC_FREE ( frame ) ;
return status ;
2000-04-25 18:04:06 +04:00
}
2013-04-06 12:57:41 +04:00
struct cli_tree_connect_state {
struct cli_state * cli ;
} ;
static struct tevent_req * cli_raw_tcon_send (
TALLOC_CTX * mem_ctx , struct tevent_context * ev , struct cli_state * cli ,
const char * service , const char * pass , const char * dev ) ;
static NTSTATUS cli_raw_tcon_recv ( struct tevent_req * req ,
2015-05-10 02:59:45 +03:00
uint16_t * max_xmit , uint16_t * tid ) ;
2013-04-06 12:57:41 +04:00
static void cli_tree_connect_smb2_done ( struct tevent_req * subreq ) ;
static void cli_tree_connect_andx_done ( struct tevent_req * subreq ) ;
static void cli_tree_connect_raw_done ( struct tevent_req * subreq ) ;
static struct tevent_req * cli_tree_connect_send (
TALLOC_CTX * mem_ctx , struct tevent_context * ev , struct cli_state * cli ,
2016-12-08 09:13:57 +03:00
const char * share , const char * dev , const char * pass )
2011-09-20 08:57:37 +04:00
{
2013-04-06 12:57:41 +04:00
struct tevent_req * req , * subreq ;
struct cli_tree_connect_state * state ;
2016-12-08 09:13:57 +03:00
int passlen ;
if ( pass = = NULL ) {
pass = " " ;
}
passlen = strlen ( pass ) + 1 ;
2013-04-06 12:57:41 +04:00
req = tevent_req_create ( mem_ctx , & state ,
struct cli_tree_connect_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > cli = cli ;
2012-05-30 12:07:21 +04:00
2011-09-20 08:57:37 +04:00
cli - > share = talloc_strdup ( cli , share ) ;
2013-04-06 12:57:41 +04:00
if ( tevent_req_nomem ( cli - > share , req ) ) {
return tevent_req_post ( req , ev ) ;
2011-09-20 08:57:37 +04:00
}
2012-05-19 20:09:30 +04:00
if ( smbXcli_conn_protocol ( cli - > conn ) > = PROTOCOL_SMB2_02 ) {
2014-09-25 05:28:37 +04:00
char * unc ;
2017-06-14 02:37:39 +03:00
TALLOC_FREE ( cli - > smb2 . tcon ) ;
2014-09-25 05:28:37 +04:00
cli - > smb2 . tcon = smbXcli_tcon_create ( cli ) ;
if ( tevent_req_nomem ( cli - > smb2 . tcon , req ) ) {
return tevent_req_post ( req , ev ) ;
}
unc = talloc_asprintf ( state , " \\ \\ %s \\ %s " ,
smbXcli_conn_remote_name ( cli - > conn ) ,
share ) ;
if ( tevent_req_nomem ( unc , req ) ) {
return tevent_req_post ( req , ev ) ;
}
subreq = smb2cli_tcon_send ( state , ev , cli - > conn , cli - > timeout ,
cli - > smb2 . session , cli - > smb2 . tcon ,
0 , /* flags */
unc ) ;
2013-04-06 12:57:41 +04:00
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_tree_connect_smb2_done ,
req ) ;
return req ;
2011-09-20 08:57:37 +04:00
}
2012-05-30 12:07:21 +04:00
if ( smbXcli_conn_protocol ( cli - > conn ) > = PROTOCOL_LANMAN1 ) {
2013-04-06 12:57:41 +04:00
subreq = cli_tcon_andx_send ( state , ev , cli , share , dev ,
pass , passlen ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_tree_connect_andx_done ,
req ) ;
return req ;
2012-05-30 12:07:21 +04:00
}
2013-04-06 12:57:41 +04:00
subreq = cli_raw_tcon_send ( state , ev , cli , share , pass , dev ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
2012-05-30 12:07:21 +04:00
}
2013-04-06 12:57:41 +04:00
tevent_req_set_callback ( subreq , cli_tree_connect_raw_done , req ) ;
return req ;
}
2012-07-23 22:38:33 +04:00
2013-04-06 12:57:41 +04:00
static void cli_tree_connect_smb2_done ( struct tevent_req * subreq )
{
2022-08-25 11:43:54 +03:00
NTSTATUS status = smb2cli_tcon_recv ( subreq ) ;
tevent_req_simple_finish_ntstatus ( subreq , status ) ;
2013-04-06 12:57:41 +04:00
}
2012-05-30 12:07:21 +04:00
2013-04-06 12:57:41 +04:00
static void cli_tree_connect_andx_done ( struct tevent_req * subreq )
{
2022-08-25 11:43:54 +03:00
NTSTATUS status = cli_tcon_andx_recv ( subreq ) ;
tevent_req_simple_finish_ntstatus ( subreq , status ) ;
2013-04-06 12:57:41 +04:00
}
static void cli_tree_connect_raw_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_tree_connect_state * state = tevent_req_data (
req , struct cli_tree_connect_state ) ;
NTSTATUS status ;
uint16_t max_xmit = 0 ;
uint16_t tid = 0 ;
status = cli_raw_tcon_recv ( subreq , & max_xmit , & tid ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2013-09-13 11:10:42 +04:00
smb1cli_tcon_set_values ( state - > cli - > smb1 . tcon ,
tid ,
0 , /* optional_support */
0 , /* maximal_access */
0 , /* guest_maximal_access */
NULL , /* service */
NULL ) ; /* fs_type */
2013-04-06 12:57:41 +04:00
tevent_req_done ( req ) ;
}
static NTSTATUS cli_tree_connect_recv ( struct tevent_req * req )
{
return tevent_req_simple_recv_ntstatus ( req ) ;
}
NTSTATUS cli_tree_connect ( struct cli_state * cli , const char * share ,
2016-12-08 09:13:57 +03:00
const char * dev , const char * pass )
2013-04-06 12:57:41 +04:00
{
struct tevent_context * ev ;
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_NO_MEMORY ;
if ( smbXcli_conn_has_async_calls ( cli - > conn ) ) {
return NT_STATUS_INVALID_PARAMETER ;
}
ev = samba_tevent_context_init ( talloc_tos ( ) ) ;
if ( ev = = NULL ) {
goto fail ;
}
2016-12-08 09:13:57 +03:00
req = cli_tree_connect_send ( ev , ev , cli , share , dev , pass ) ;
2013-04-06 12:57:41 +04:00
if ( req = = NULL ) {
goto fail ;
}
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
goto fail ;
}
status = cli_tree_connect_recv ( req ) ;
fail :
TALLOC_FREE ( ev ) ;
return status ;
2011-09-20 08:57:37 +04:00
}
2016-12-07 19:32:58 +03:00
NTSTATUS cli_tree_connect_creds ( struct cli_state * cli ,
const char * share , const char * dev ,
struct cli_credentials * creds )
{
2022-04-14 16:36:51 +03:00
bool need_pass = false ;
2016-12-07 19:32:58 +03:00
const char * pw = NULL ;
2022-04-14 16:36:51 +03:00
/*
* We should work out if the protocol
* will make use of a password for share level
* authentication before we may cause
* the password prompt to be called .
*/
if ( smbXcli_conn_protocol ( cli - > conn ) < PROTOCOL_SMB2_02 ) {
uint16_t sec_mode = smb1cli_conn_server_security_mode ( cli - > conn ) ;
/* in user level security don't send a password now */
if ( ! ( sec_mode & NEGOTIATE_SECURITY_USER_LEVEL ) ) {
need_pass = true ;
}
}
if ( need_pass & & creds ! = NULL ) {
2016-12-07 19:32:58 +03:00
pw = cli_credentials_get_password ( creds ) ;
}
2016-12-08 09:13:57 +03:00
return cli_tree_connect ( cli , share , dev , pw ) ;
2016-12-07 19:32:58 +03:00
}
2000-04-25 18:04:06 +04:00
/****************************************************************************
2002-07-15 14:35:28 +04:00
Send a tree disconnect .
2000-04-25 18:04:06 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
2010-01-03 20:46:57 +03:00
struct cli_tdis_state {
struct cli_state * cli ;
} ;
static void cli_tdis_done ( struct tevent_req * subreq ) ;
2013-08-14 13:38:22 +04:00
static struct tevent_req * cli_tdis_send ( TALLOC_CTX * mem_ctx ,
2010-01-03 20:46:57 +03:00
struct tevent_context * ev ,
struct cli_state * cli )
2000-04-25 18:04:06 +04:00
{
2010-01-03 20:46:57 +03:00
struct tevent_req * req , * subreq ;
struct cli_tdis_state * state ;
2008-11-30 14:25:17 +03:00
2010-01-03 20:46:57 +03:00
req = tevent_req_create ( mem_ctx , & state , struct cli_tdis_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > cli = cli ;
2008-11-30 14:25:17 +03:00
2016-08-18 22:20:25 +03:00
subreq = cli_smb_send ( state , ev , cli , SMBtdis , 0 , 0 , 0 , NULL , 0 , NULL ) ;
2010-01-03 20:46:57 +03:00
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
2005-01-22 04:22:39 +03:00
}
2010-01-03 20:46:57 +03:00
tevent_req_set_callback ( subreq , cli_tdis_done , req ) ;
return req ;
}
2005-01-22 04:22:39 +03:00
2010-01-03 20:46:57 +03:00
static void cli_tdis_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_tdis_state * state = tevent_req_data (
req , struct cli_tdis_state ) ;
NTSTATUS status ;
2010-02-20 11:53:58 +03:00
status = cli_smb_recv ( subreq , NULL , NULL , 0 , NULL , NULL , NULL , NULL ) ;
2010-01-03 20:46:57 +03:00
TALLOC_FREE ( subreq ) ;
2023-08-16 12:59:03 +03:00
if ( tevent_req_nterror ( req , status ) ) {
2010-01-03 20:46:57 +03:00
return ;
}
2017-06-14 02:37:39 +03:00
TALLOC_FREE ( state - > cli - > smb1 . tcon ) ;
2010-01-03 20:46:57 +03:00
tevent_req_done ( req ) ;
}
2013-08-14 13:38:22 +04:00
static NTSTATUS cli_tdis_recv ( struct tevent_req * req )
2010-01-03 20:46:57 +03:00
{
return tevent_req_simple_recv_ntstatus ( req ) ;
}
NTSTATUS cli_tdis ( struct cli_state * cli )
{
struct tevent_context * ev ;
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_NO_MEMORY ;
2013-08-14 13:44:58 +04:00
if ( smbXcli_conn_protocol ( cli - > conn ) > = PROTOCOL_SMB2_02 ) {
2017-06-14 02:37:39 +03:00
status = smb2cli_tdis ( cli - > conn ,
2014-09-25 05:28:37 +04:00
cli - > timeout ,
cli - > smb2 . session ,
cli - > smb2 . tcon ) ;
2017-06-14 02:37:39 +03:00
if ( NT_STATUS_IS_OK ( status ) ) {
TALLOC_FREE ( cli - > smb2 . tcon ) ;
}
return status ;
2013-08-14 13:44:58 +04:00
}
2012-05-26 13:45:09 +04:00
if ( smbXcli_conn_has_async_calls ( cli - > conn ) ) {
2010-01-03 20:46:57 +03:00
return NT_STATUS_INVALID_PARAMETER ;
}
2013-02-18 12:08:19 +04:00
ev = samba_tevent_context_init ( talloc_tos ( ) ) ;
2010-01-03 20:46:57 +03:00
if ( ev = = NULL ) {
goto fail ;
}
req = cli_tdis_send ( ev , ev , cli ) ;
if ( req = = NULL ) {
goto fail ;
}
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
goto fail ;
}
status = cli_tdis_recv ( req ) ;
fail :
TALLOC_FREE ( ev ) ;
return status ;
2000-04-25 18:04:06 +04:00
}
2013-04-04 16:36:51 +04:00
struct cli_connect_sock_state {
2011-05-27 00:00:07 +04:00
const char * * called_names ;
const char * * calling_names ;
int * called_types ;
int fd ;
2013-04-04 16:36:51 +04:00
uint16_t port ;
} ;
static void cli_connect_sock_done ( struct tevent_req * subreq ) ;
/*
* Async only if we don ' t have to look up the name , i . e . " pss " is set with a
* nonzero address .
*/
static struct tevent_req * cli_connect_sock_send (
TALLOC_CTX * mem_ctx , struct tevent_context * ev ,
const char * host , int name_type , const struct sockaddr_storage * pss ,
const char * myname , uint16_t port )
{
struct tevent_req * req , * subreq ;
struct cli_connect_sock_state * state ;
2020-09-04 05:03:52 +03:00
struct sockaddr_storage * addrs = NULL ;
unsigned i ;
unsigned num_addrs = 0 ;
2013-04-04 16:36:51 +04:00
NTSTATUS status ;
req = tevent_req_create ( mem_ctx , & state ,
struct cli_connect_sock_state ) ;
if ( req = = NULL ) {
return NULL ;
}
2011-05-27 00:00:07 +04:00
if ( ( pss = = NULL ) | | is_zero_addr ( pss ) ) {
2013-04-04 16:36:51 +04:00
/*
* Here we cheat . resolve_name_list is not async at all . So
* this call will only be really async if the name lookup has
* been done externally .
*/
status = resolve_name_list ( state , host , name_type ,
2011-05-27 00:00:07 +04:00
& addrs , & num_addrs ) ;
2023-08-16 12:59:03 +03:00
if ( tevent_req_nterror ( req , status ) ) {
2013-04-04 16:36:51 +04:00
return tevent_req_post ( req , ev ) ;
2011-05-27 00:00:07 +04:00
}
} else {
2013-11-12 00:37:48 +04:00
addrs = talloc_array ( state , struct sockaddr_storage , 1 ) ;
if ( tevent_req_nomem ( addrs , req ) ) {
return tevent_req_post ( req , ev ) ;
}
addrs [ 0 ] = * pss ;
2011-05-27 00:00:07 +04:00
num_addrs = 1 ;
}
2013-04-04 16:36:51 +04:00
state - > called_names = talloc_array ( state , const char * , num_addrs ) ;
if ( tevent_req_nomem ( state - > called_names , req ) ) {
return tevent_req_post ( req , ev ) ;
2011-05-27 00:00:07 +04:00
}
2013-04-04 16:36:51 +04:00
state - > called_types = talloc_array ( state , int , num_addrs ) ;
if ( tevent_req_nomem ( state - > called_types , req ) ) {
return tevent_req_post ( req , ev ) ;
}
state - > calling_names = talloc_array ( state , const char * , num_addrs ) ;
if ( tevent_req_nomem ( state - > calling_names , req ) ) {
return tevent_req_post ( req , ev ) ;
}
for ( i = 0 ; i < num_addrs ; i + + ) {
state - > called_names [ i ] = host ;
state - > called_types [ i ] = name_type ;
state - > calling_names [ i ] = myname ;
}
subreq = smbsock_any_connect_send (
2013-11-12 00:37:48 +04:00
state , ev , addrs , state - > called_names , state - > called_types ,
2013-04-04 16:36:51 +04:00
state - > calling_names , NULL , num_addrs , port ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_connect_sock_done , req ) ;
return req ;
}
static void cli_connect_sock_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_connect_sock_state * state = tevent_req_data (
req , struct cli_connect_sock_state ) ;
NTSTATUS status ;
status = smbsock_any_connect_recv ( subreq , & state - > fd , NULL ,
& state - > port ) ;
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
set_socket_options ( state - > fd , lp_socket_options ( ) ) ;
tevent_req_done ( req ) ;
}
static NTSTATUS cli_connect_sock_recv ( struct tevent_req * req ,
int * pfd , uint16_t * pport )
{
struct cli_connect_sock_state * state = tevent_req_data (
req , struct cli_connect_sock_state ) ;
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
return status ;
}
* pfd = state - > fd ;
* pport = state - > port ;
return NT_STATUS_OK ;
}
2013-04-04 18:55:48 +04:00
struct cli_connect_nb_state {
const char * desthost ;
2020-05-28 18:59:19 +03:00
enum smb_signing_setting signing_state ;
2013-04-04 18:55:48 +04:00
int flags ;
2011-05-27 00:00:07 +04:00
struct cli_state * cli ;
2013-04-04 18:55:48 +04:00
} ;
static void cli_connect_nb_done ( struct tevent_req * subreq ) ;
static struct tevent_req * cli_connect_nb_send (
TALLOC_CTX * mem_ctx , struct tevent_context * ev ,
const char * host , const struct sockaddr_storage * dest_ss ,
uint16_t port , int name_type , const char * myname ,
2020-05-28 18:59:19 +03:00
enum smb_signing_setting signing_state , int flags )
2013-04-04 18:55:48 +04:00
{
struct tevent_req * req , * subreq ;
struct cli_connect_nb_state * state ;
2011-05-27 00:00:07 +04:00
2013-04-04 18:55:48 +04:00
req = tevent_req_create ( mem_ctx , & state , struct cli_connect_nb_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > signing_state = signing_state ;
state - > flags = flags ;
2011-05-27 00:00:07 +04:00
2015-01-16 18:18:45 +03:00
if ( host ! = NULL ) {
char * p = strchr ( host , ' # ' ) ;
if ( p ! = NULL ) {
name_type = strtol ( p + 1 , NULL , 16 ) ;
host = talloc_strndup ( state , host , p - host ) ;
if ( tevent_req_nomem ( host , req ) ) {
return tevent_req_post ( req , ev ) ;
}
}
state - > desthost = host ;
2016-08-09 02:53:21 +03:00
} else if ( dest_ss ! = NULL ) {
2015-01-16 18:18:45 +03:00
state - > desthost = print_canonical_sockaddr ( state , dest_ss ) ;
if ( tevent_req_nomem ( state - > desthost , req ) ) {
2013-04-04 18:55:48 +04:00
return tevent_req_post ( req , ev ) ;
2011-05-27 00:00:07 +04:00
}
2016-08-09 02:53:21 +03:00
} else {
/* No host or dest_ss given. Error out. */
s3:libsmb: Fix panic in cliconnect.c
This command line panics:
$ bin/rpcclient ncacn_np: -c epmlookup
0 __pthread_kill_implementation (threadid=<optimized out>, signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44
1 0x00007ffff64ae8a3 in __pthread_kill_internal (signo=6, threadid=<optimized out>) at pthread_kill.c:78
2 0x00007ffff645c8ee in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
3 0x00007ffff64448ff in __GI_abort () at abort.c:79
4 0x00007ffff7b01524 in tevent_req_is_nterror (req=0x555555728610, status=0x7fffffff9bb4) at ../../lib/util/tevent_ntstatus.c:66
5 0x00007ffff7b9fd91 in cli_connect_nb_recv (req=0x555555728610, pcli=0x5555556fcb18) at ../../source3/libsmb/cliconnect.c:2731
6 0x00007ffff7ba02a8 in cli_start_connection_connected (subreq=0x555555728610) at ../../source3/libsmb/cliconnect.c:2882
7 0x00007ffff7aecb1a in _tevent_req_notify_callback (req=0x555555728610, location=0x7ffff7bde0e0 "../../source3/libsmb/cliconnect.c:2686") at ../../lib/tevent/tevent_req.c:177
8 0x00007ffff7aeccab in tevent_req_finish (req=0x555555728610, state=TEVENT_REQ_USER_ERROR, location=0x7ffff7bde0e0 "../../source3/libsmb/cliconnect.c:2686") at ../../lib/tevent/tevent_req.c:234
9 0x00007ffff7aecdda in tevent_req_trigger (ev=0x5555557182e0, im=0x555555728720, private_data=0x555555728610) at ../../lib/tevent/tevent_req.c:291
10 0x00007ffff7aeb513 in tevent_common_invoke_immediate_handler (im=0x555555728720, removed=0x0) at ../../lib/tevent/tevent_immediate.c:190
11 0x00007ffff7aeb685 in tevent_common_loop_immediate (ev=0x5555557182e0) at ../../lib/tevent/tevent_immediate.c:236
12 0x00007ffff7af7502 in epoll_event_loop_once (ev=0x5555557182e0, location=0x7ffff7af8ac0 "../../lib/tevent/tevent_req.c:342") at ../../lib/tevent/tevent_epoll.c:905
13 0x00007ffff7af2d22 in std_event_loop_once (ev=0x5555557182e0, location=0x7ffff7af8ac0 "../../lib/tevent/tevent_req.c:342") at ../../lib/tevent/tevent_standard.c:110
14 0x00007ffff7ae93ab in _tevent_loop_once (ev=0x5555557182e0, location=0x7ffff7af8ac0 "../../lib/tevent/tevent_req.c:342") at ../../lib/tevent/tevent.c:820
15 0x00007ffff7aecf9e in tevent_req_poll (req=0x555555728290, ev=0x5555557182e0) at ../../lib/tevent/tevent_req.c:342
16 0x00007ffff7b01647 in tevent_req_poll_ntstatus (req=0x555555728290, ev=0x5555557182e0, status=0x7fffffff9f4c) at ../../lib/util/tevent_ntstatus.c:109
17 0x00007ffff7ba246a in cli_full_connection_creds (output_cli=0x7fffffffa220, my_name=0x555555701990 "CLUSTEREDMEMBER", dest_host=0x0, dest_ss=0x0, port=0, service=0x5555556981d1 "IPC$", service_type=0x5555556981d6 "IPC", creds=0x5555556fa410, flags=4096) at ../../source3/libsmb/cliconnect.c:3807
18 0x0000555555619ae9 in main (argc=4, argv=0x7fffffffa3e8) at ../../source3/rpcclient/rpcclient.c:1308
tevent_req_is_nterror() expects error set by tevent_req_nterror()
- to have TEVENT_NTERROR_MAGIC, otherwise it calls abort().
Signed-off-by: Pavel Filipenský <pfilipensky@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Pavel Filipensky <pfilipensky@samba.org>
Autobuild-Date(master): Mon Apr 15 14:55:21 UTC 2024 on atb-devel-224
2024-04-12 15:57:11 +03:00
tevent_req_nterror ( req , NT_STATUS_INVALID_PARAMETER_MIX ) ;
2016-08-09 02:53:21 +03:00
return tevent_req_post ( req , ev ) ;
2011-05-27 00:00:07 +04:00
}
2013-04-04 18:55:48 +04:00
subreq = cli_connect_sock_send ( state , ev , host , name_type , dest_ss ,
myname , port ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
2011-05-27 00:00:07 +04:00
}
2013-04-04 18:55:48 +04:00
tevent_req_set_callback ( subreq , cli_connect_nb_done , req ) ;
return req ;
}
2011-05-27 00:00:07 +04:00
2013-04-04 18:55:48 +04:00
static void cli_connect_nb_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_connect_nb_state * state = tevent_req_data (
req , struct cli_connect_nb_state ) ;
NTSTATUS status ;
2013-12-10 20:48:59 +04:00
int fd = 0 ;
2013-04-04 18:55:48 +04:00
uint16_t port ;
status = cli_connect_sock_recv ( subreq , & fd , & port ) ;
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2016-12-08 16:06:39 +03:00
state - > cli = cli_state_create ( state , fd , state - > desthost ,
2013-04-04 18:55:48 +04:00
state - > signing_state , state - > flags ) ;
if ( tevent_req_nomem ( state - > cli , req ) ) {
2011-11-21 21:03:07 +04:00
close ( fd ) ;
2013-04-04 18:55:48 +04:00
return ;
2011-05-27 00:00:07 +04:00
}
2013-04-04 18:55:48 +04:00
tevent_req_done ( req ) ;
}
static NTSTATUS cli_connect_nb_recv ( struct tevent_req * req ,
2023-10-31 16:58:49 +03:00
TALLOC_CTX * mem_ctx ,
2013-04-04 18:55:48 +04:00
struct cli_state * * pcli )
{
struct cli_connect_nb_state * state = tevent_req_data (
req , struct cli_connect_nb_state ) ;
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
return status ;
}
2023-10-31 16:58:49 +03:00
* pcli = talloc_move ( mem_ctx , & state - > cli ) ;
2013-04-04 18:55:48 +04:00
return NT_STATUS_OK ;
}
2023-10-31 17:13:04 +03:00
NTSTATUS cli_connect_nb ( TALLOC_CTX * mem_ctx ,
const char * host ,
const struct sockaddr_storage * dest_ss ,
uint16_t port ,
int name_type ,
const char * myname ,
enum smb_signing_setting signing_state ,
int flags ,
struct cli_state * * pcli )
2013-04-04 18:55:48 +04:00
{
struct tevent_context * ev ;
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_NO_MEMORY ;
2011-05-27 00:00:07 +04:00
2023-10-31 17:13:04 +03:00
ev = samba_tevent_context_init ( mem_ctx ) ;
2013-04-04 18:55:48 +04:00
if ( ev = = NULL ) {
goto fail ;
}
req = cli_connect_nb_send ( ev , ev , host , dest_ss , port , name_type ,
myname , signing_state , flags ) ;
if ( req = = NULL ) {
goto fail ;
}
if ( ! tevent_req_set_endtime ( req , ev , timeval_current_ofs ( 20 , 0 ) ) ) {
goto fail ;
}
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
goto fail ;
}
2023-10-31 17:13:04 +03:00
status = cli_connect_nb_recv ( req , mem_ctx , pcli ) ;
2011-05-27 00:00:07 +04:00
fail :
2013-04-04 18:55:48 +04:00
TALLOC_FREE ( ev ) ;
2011-05-27 00:00:07 +04:00
return status ;
}
2013-04-05 14:42:01 +04:00
struct cli_start_connection_state {
struct tevent_context * ev ;
struct cli_state * cli ;
2015-12-18 19:16:04 +03:00
int min_protocol ;
int max_protocol ;
2022-08-25 13:20:26 +03:00
struct smb2_negotiate_contexts * negotiate_contexts ;
2013-04-05 14:42:01 +04:00
} ;
static void cli_start_connection_connected ( struct tevent_req * subreq ) ;
static void cli_start_connection_done ( struct tevent_req * subreq ) ;
2002-07-15 14:35:28 +04:00
/**
2023-12-18 23:13:56 +03:00
establishes a connection to after the negprot .
2002-07-15 14:35:28 +04:00
@ param output_cli A fully initialised cli structure , non - null only on success
@ param dest_host The netbios name of the remote host
2023-01-17 14:29:28 +03:00
@ param dest_ss ( optional ) The destination IP , NULL for name based lookup
2002-07-15 14:35:28 +04:00
@ param port ( optional ) The destination port ( 0 for default )
2003-08-19 21:47:44 +04:00
*/
2013-04-05 14:42:01 +04:00
static struct tevent_req * cli_start_connection_send (
TALLOC_CTX * mem_ctx , struct tevent_context * ev ,
const char * my_name , const char * dest_host ,
const struct sockaddr_storage * dest_ss , int port ,
2022-08-26 15:17:26 +03:00
enum smb_signing_setting signing_state , int flags ,
struct smb2_negotiate_contexts * negotiate_contexts )
2013-04-05 14:42:01 +04:00
{
struct tevent_req * req , * subreq ;
struct cli_start_connection_state * state ;
req = tevent_req_create ( mem_ctx , & state ,
struct cli_start_connection_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > ev = ev ;
2020-05-28 18:22:12 +03:00
if ( flags & CLI_FULL_CONNECTION_IPC ) {
2015-12-18 19:16:04 +03:00
state - > min_protocol = lp_client_ipc_min_protocol ( ) ;
state - > max_protocol = lp_client_ipc_max_protocol ( ) ;
} else {
state - > min_protocol = lp_client_min_protocol ( ) ;
state - > max_protocol = lp_client_max_protocol ( ) ;
}
2017-06-19 09:16:57 +03:00
if ( flags & CLI_FULL_CONNECTION_FORCE_SMB1 ) {
2019-08-28 15:11:02 +03:00
state - > max_protocol = MIN ( state - > max_protocol ,
PROTOCOL_NT1 ) ;
state - > min_protocol = MIN ( state - > min_protocol ,
state - > max_protocol ) ;
2017-06-19 09:16:57 +03:00
}
2017-06-19 10:57:28 +03:00
if ( flags & CLI_FULL_CONNECTION_DISABLE_SMB1 ) {
2019-08-29 13:13:50 +03:00
state - > min_protocol = MAX ( state - > min_protocol ,
PROTOCOL_SMB2_02 ) ;
state - > max_protocol = MAX ( state - > max_protocol ,
state - > min_protocol ) ;
2017-06-19 10:57:28 +03:00
}
2022-08-25 13:20:26 +03:00
state - > negotiate_contexts = talloc_zero (
state , struct smb2_negotiate_contexts ) ;
if ( tevent_req_nomem ( state - > negotiate_contexts , req ) ) {
return tevent_req_post ( req , ev ) ;
}
if ( flags & CLI_FULL_CONNECTION_REQUEST_POSIX ) {
NTSTATUS status ;
status = smb2_negotiate_context_add (
state - > negotiate_contexts ,
state - > negotiate_contexts ,
SMB2_POSIX_EXTENSIONS_AVAILABLE ,
( const uint8_t * ) SMB2_CREATE_TAG_POSIX ,
strlen ( SMB2_CREATE_TAG_POSIX ) ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
}
2022-08-26 15:17:26 +03:00
if ( negotiate_contexts ! = NULL ) {
uint16_t i ;
for ( i = 0 ; i < negotiate_contexts - > num_contexts ; i + + ) {
struct smb2_negotiate_context * ctx =
& negotiate_contexts - > contexts [ i ] ;
NTSTATUS status ;
status = smb2_negotiate_context_add (
state - > negotiate_contexts ,
state - > negotiate_contexts ,
ctx - > type ,
ctx - > data . data ,
ctx - > data . length ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
}
}
2013-04-05 14:42:01 +04:00
subreq = cli_connect_nb_send ( state , ev , dest_host , dest_ss , port ,
0x20 , my_name , signing_state , flags ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_start_connection_connected , req ) ;
return req ;
}
static void cli_start_connection_connected ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_start_connection_state * state = tevent_req_data (
req , struct cli_start_connection_state ) ;
NTSTATUS status ;
2023-10-31 16:58:49 +03:00
status = cli_connect_nb_recv ( subreq , state , & state - > cli ) ;
2013-04-05 14:42:01 +04:00
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2022-08-25 10:54:52 +03:00
subreq = smbXcli_negprot_send (
state ,
state - > ev ,
state - > cli - > conn ,
state - > cli - > timeout ,
state - > min_protocol ,
state - > max_protocol ,
WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK ,
2022-08-25 13:20:26 +03:00
state - > negotiate_contexts ) ;
2013-04-05 14:42:01 +04:00
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq , cli_start_connection_done , req ) ;
}
static void cli_start_connection_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
2013-08-09 22:15:48 +04:00
struct cli_start_connection_state * state = tevent_req_data (
req , struct cli_start_connection_state ) ;
2013-04-05 14:42:01 +04:00
NTSTATUS status ;
2022-08-25 10:54:52 +03:00
status = smbXcli_negprot_recv ( subreq , NULL , NULL ) ;
2013-04-05 14:42:01 +04:00
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2013-08-09 22:15:48 +04:00
if ( smbXcli_conn_protocol ( state - > cli - > conn ) > = PROTOCOL_SMB2_02 ) {
/* Ensure we ask for some initial credits. */
smb2cli_conn_set_max_credits ( state - > cli - > conn ,
DEFAULT_SMB2_MAX_CREDITS ) ;
}
2013-04-05 14:42:01 +04:00
tevent_req_done ( req ) ;
}
static NTSTATUS cli_start_connection_recv ( struct tevent_req * req ,
2023-10-31 17:05:35 +03:00
TALLOC_CTX * mem_ctx ,
2013-04-05 14:42:01 +04:00
struct cli_state * * output_cli )
{
struct cli_start_connection_state * state = tevent_req_data (
req , struct cli_start_connection_state ) ;
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
return status ;
}
2023-10-31 17:05:35 +03:00
* output_cli = talloc_move ( mem_ctx , & state - > cli ) ;
2013-08-09 22:15:48 +04:00
2013-04-05 14:42:01 +04:00
return NT_STATUS_OK ;
}
2023-11-23 16:51:48 +03:00
NTSTATUS cli_start_connection ( TALLOC_CTX * mem_ctx ,
struct cli_state * * output_cli ,
2023-12-18 23:13:56 +03:00
const char * my_name ,
const char * dest_host ,
2011-07-22 14:01:27 +04:00
const struct sockaddr_storage * dest_ss , int port ,
2020-05-28 18:59:19 +03:00
enum smb_signing_setting signing_state , int flags )
2001-12-03 10:42:18 +03:00
{
2013-04-05 14:42:01 +04:00
struct tevent_context * ev ;
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_NO_MEMORY ;
2002-07-15 14:35:28 +04:00
2023-11-23 16:51:48 +03:00
ev = samba_tevent_context_init ( mem_ctx ) ;
2013-04-05 14:42:01 +04:00
if ( ev = = NULL ) {
goto fail ;
2001-12-03 10:42:18 +03:00
}
2013-04-05 14:42:01 +04:00
req = cli_start_connection_send ( ev , ev , my_name , dest_host , dest_ss ,
2022-08-26 15:17:26 +03:00
port , signing_state , flags , NULL ) ;
2013-04-05 14:42:01 +04:00
if ( req = = NULL ) {
goto fail ;
2001-12-03 10:42:18 +03:00
}
2013-04-05 14:42:01 +04:00
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
goto fail ;
}
2023-11-23 16:51:48 +03:00
status = cli_start_connection_recv ( req , mem_ctx , output_cli ) ;
2013-04-05 14:42:01 +04:00
fail :
TALLOC_FREE ( ev ) ;
return status ;
2003-08-19 21:47:44 +04:00
}
2016-11-01 01:02:27 +03:00
struct cli_smb1_setup_encryption_blob_state {
uint16_t setup [ 1 ] ;
uint8_t param [ 4 ] ;
NTSTATUS status ;
DATA_BLOB out ;
uint16_t enc_ctx_id ;
} ;
static void cli_smb1_setup_encryption_blob_done ( struct tevent_req * subreq ) ;
static struct tevent_req * cli_smb1_setup_encryption_blob_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct cli_state * cli ,
const DATA_BLOB in )
{
struct tevent_req * req = NULL ;
struct cli_smb1_setup_encryption_blob_state * state = NULL ;
struct tevent_req * subreq = NULL ;
req = tevent_req_create ( mem_ctx , & state ,
struct cli_smb1_setup_encryption_blob_state ) ;
if ( req = = NULL ) {
return NULL ;
}
if ( in . length > CLI_BUFFER_SIZE ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_PARAMETER_MIX ) ;
return tevent_req_post ( req , ev ) ;
}
SSVAL ( state - > setup + 0 , 0 , TRANSACT2_SETFSINFO ) ;
SSVAL ( state - > param , 0 , 0 ) ;
SSVAL ( state - > param , 2 , SMB_REQUEST_TRANSPORT_ENCRYPTION ) ;
subreq = smb1cli_trans_send ( state , ev , cli - > conn ,
SMBtrans2 ,
0 , 0 , /* _flags */
0 , 0 , /* _flags2 */
cli - > timeout ,
cli - > smb1 . pid ,
cli - > smb1 . tcon ,
cli - > smb1 . session ,
NULL , /* pipe_name */
0 , /* fid */
0 , /* function */
0 , /* flags */
state - > setup , 1 , 0 ,
state - > param , 4 , 2 ,
in . data , in . length , CLI_BUFFER_SIZE ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq ,
cli_smb1_setup_encryption_blob_done ,
req ) ;
return req ;
}
static void cli_smb1_setup_encryption_blob_done ( struct tevent_req * subreq )
{
struct tevent_req * req =
tevent_req_callback_data ( subreq ,
struct tevent_req ) ;
struct cli_smb1_setup_encryption_blob_state * state =
tevent_req_data ( req ,
struct cli_smb1_setup_encryption_blob_state ) ;
uint8_t * rparam = NULL , * rdata = NULL ;
uint32_t num_rparam , num_rdata ;
NTSTATUS status ;
status = smb1cli_trans_recv ( subreq , state ,
NULL , /* recv_flags */
NULL , 0 , NULL , /* rsetup */
& rparam , 0 , & num_rparam ,
& rdata , 0 , & num_rdata ) ;
TALLOC_FREE ( subreq ) ;
state - > status = status ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) ) {
status = NT_STATUS_OK ;
}
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
if ( num_rparam = = 2 ) {
state - > enc_ctx_id = SVAL ( rparam , 0 ) ;
}
TALLOC_FREE ( rparam ) ;
state - > out = data_blob_const ( rdata , num_rdata ) ;
tevent_req_done ( req ) ;
}
static NTSTATUS cli_smb1_setup_encryption_blob_recv ( struct tevent_req * req ,
TALLOC_CTX * mem_ctx ,
DATA_BLOB * out ,
uint16_t * enc_ctx_id )
{
struct cli_smb1_setup_encryption_blob_state * state =
tevent_req_data ( req ,
struct cli_smb1_setup_encryption_blob_state ) ;
NTSTATUS status ;
if ( tevent_req_is_nterror ( req , & status ) ) {
tevent_req_received ( req ) ;
return status ;
}
status = state - > status ;
* out = state - > out ;
talloc_steal ( mem_ctx , out - > data ) ;
* enc_ctx_id = state - > enc_ctx_id ;
tevent_req_received ( req ) ;
return status ;
}
struct cli_smb1_setup_encryption_state {
struct tevent_context * ev ;
struct cli_state * cli ;
struct smb_trans_enc_state * es ;
DATA_BLOB blob_in ;
DATA_BLOB blob_out ;
bool local_ready ;
bool remote_ready ;
} ;
static void cli_smb1_setup_encryption_local_next ( struct tevent_req * req ) ;
static void cli_smb1_setup_encryption_local_done ( struct tevent_req * subreq ) ;
static void cli_smb1_setup_encryption_remote_next ( struct tevent_req * req ) ;
static void cli_smb1_setup_encryption_remote_done ( struct tevent_req * subreq ) ;
static void cli_smb1_setup_encryption_ready ( struct tevent_req * req ) ;
static struct tevent_req * cli_smb1_setup_encryption_send ( TALLOC_CTX * mem_ctx ,
struct tevent_context * ev ,
struct cli_state * cli ,
struct cli_credentials * creds )
{
struct tevent_req * req = NULL ;
struct cli_smb1_setup_encryption_state * state = NULL ;
struct auth_generic_state * ags = NULL ;
const DATA_BLOB * b = NULL ;
bool auth_requested = false ;
const char * target_service = NULL ;
const char * target_hostname = NULL ;
NTSTATUS status ;
req = tevent_req_create ( mem_ctx , & state ,
struct cli_smb1_setup_encryption_state ) ;
if ( req = = NULL ) {
return NULL ;
}
state - > ev = ev ;
state - > cli = cli ;
auth_requested = cli_credentials_authentication_requested ( creds ) ;
if ( ! auth_requested ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_PARAMETER_MIX ) ;
return tevent_req_post ( req , ev ) ;
}
target_service = " cifs " ;
target_hostname = smbXcli_conn_remote_name ( cli - > conn ) ;
state - > es = talloc_zero ( state , struct smb_trans_enc_state ) ;
if ( tevent_req_nomem ( state - > es , req ) ) {
return tevent_req_post ( req , ev ) ;
}
status = auth_generic_client_prepare ( state - > es , & ags ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
gensec_want_feature ( ags - > gensec_security ,
GENSEC_FEATURE_SIGN ) ;
gensec_want_feature ( ags - > gensec_security ,
GENSEC_FEATURE_SEAL ) ;
status = auth_generic_set_creds ( ags , creds ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
if ( target_service ! = NULL ) {
status = gensec_set_target_service ( ags - > gensec_security ,
target_service ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
}
if ( target_hostname ! = NULL ) {
status = gensec_set_target_hostname ( ags - > gensec_security ,
target_hostname ) ;
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
}
gensec_set_max_update_size ( ags - > gensec_security ,
CLI_BUFFER_SIZE ) ;
2016-12-08 14:25:22 +03:00
b = smbXcli_conn_server_gss_blob ( state - > cli - > conn ) ;
if ( b ! = NULL ) {
state - > blob_in = * b ;
}
status = auth_generic_client_start ( ags , GENSEC_OID_SPNEGO ) ;
2016-11-01 01:02:27 +03:00
if ( tevent_req_nterror ( req , status ) ) {
return tevent_req_post ( req , ev ) ;
}
/*
* We only need the gensec_security part from here .
*/
state - > es - > gensec_security = talloc_move ( state - > es ,
& ags - > gensec_security ) ;
TALLOC_FREE ( ags ) ;
cli_smb1_setup_encryption_local_next ( req ) ;
if ( ! tevent_req_is_in_progress ( req ) ) {
return tevent_req_post ( req , ev ) ;
}
return req ;
}
static void cli_smb1_setup_encryption_local_next ( struct tevent_req * req )
{
struct cli_smb1_setup_encryption_state * state =
tevent_req_data ( req ,
struct cli_smb1_setup_encryption_state ) ;
struct tevent_req * subreq = NULL ;
if ( state - > local_ready ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_NETWORK_RESPONSE ) ;
return ;
}
subreq = gensec_update_send ( state , state - > ev ,
state - > es - > gensec_security ,
state - > blob_in ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq , cli_smb1_setup_encryption_local_done , req ) ;
}
static void cli_smb1_setup_encryption_local_done ( struct tevent_req * subreq )
{
struct tevent_req * req =
tevent_req_callback_data ( subreq ,
struct tevent_req ) ;
struct cli_smb1_setup_encryption_state * state =
tevent_req_data ( req ,
struct cli_smb1_setup_encryption_state ) ;
NTSTATUS status ;
status = gensec_update_recv ( subreq , state , & state - > blob_out ) ;
TALLOC_FREE ( subreq ) ;
state - > blob_in = data_blob_null ;
if ( ! NT_STATUS_IS_OK ( status ) & &
! NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) )
{
tevent_req_nterror ( req , status ) ;
return ;
}
if ( NT_STATUS_IS_OK ( status ) ) {
state - > local_ready = true ;
}
/*
* We always get NT_STATUS_OK from the server even if it is not ready .
* So guess the server is ready when we are ready and already sent
* our last blob to the server .
*/
if ( state - > local_ready & & state - > blob_out . length = = 0 ) {
state - > remote_ready = true ;
}
if ( state - > local_ready & & state - > remote_ready ) {
cli_smb1_setup_encryption_ready ( req ) ;
return ;
}
cli_smb1_setup_encryption_remote_next ( req ) ;
}
static void cli_smb1_setup_encryption_remote_next ( struct tevent_req * req )
{
struct cli_smb1_setup_encryption_state * state =
tevent_req_data ( req ,
struct cli_smb1_setup_encryption_state ) ;
struct tevent_req * subreq = NULL ;
if ( state - > remote_ready ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_NETWORK_RESPONSE ) ;
return ;
}
subreq = cli_smb1_setup_encryption_blob_send ( state , state - > ev ,
state - > cli , state - > blob_out ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq ,
cli_smb1_setup_encryption_remote_done ,
req ) ;
}
static void cli_smb1_setup_encryption_remote_done ( struct tevent_req * subreq )
{
struct tevent_req * req =
tevent_req_callback_data ( subreq ,
struct tevent_req ) ;
struct cli_smb1_setup_encryption_state * state =
tevent_req_data ( req ,
struct cli_smb1_setup_encryption_state ) ;
NTSTATUS status ;
status = cli_smb1_setup_encryption_blob_recv ( subreq , state ,
& state - > blob_in ,
& state - > es - > enc_ctx_num ) ;
TALLOC_FREE ( subreq ) ;
data_blob_free ( & state - > blob_out ) ;
if ( ! NT_STATUS_IS_OK ( status ) & &
! NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) )
{
tevent_req_nterror ( req , status ) ;
return ;
}
/*
* We always get NT_STATUS_OK even if the server is not ready .
* So guess the server is ready when we are ready and sent
* our last blob to the server .
*/
if ( state - > local_ready ) {
state - > remote_ready = true ;
}
if ( state - > local_ready & & state - > remote_ready ) {
cli_smb1_setup_encryption_ready ( req ) ;
return ;
}
cli_smb1_setup_encryption_local_next ( req ) ;
}
static void cli_smb1_setup_encryption_ready ( struct tevent_req * req )
{
struct cli_smb1_setup_encryption_state * state =
tevent_req_data ( req ,
struct cli_smb1_setup_encryption_state ) ;
struct smb_trans_enc_state * es = NULL ;
if ( state - > blob_in . length ! = 0 ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_NETWORK_RESPONSE ) ;
return ;
}
if ( state - > blob_out . length ! = 0 ) {
tevent_req_nterror ( req , NT_STATUS_INVALID_NETWORK_RESPONSE ) ;
return ;
}
es = talloc_move ( state - > cli - > conn , & state - > es ) ;
es - > enc_on = true ;
smb1cli_conn_set_encryption ( state - > cli - > conn , es ) ;
es = NULL ;
tevent_req_done ( req ) ;
}
static NTSTATUS cli_smb1_setup_encryption_recv ( struct tevent_req * req )
{
return tevent_req_simple_recv_ntstatus ( req ) ;
}
NTSTATUS cli_smb1_setup_encryption ( struct cli_state * cli ,
struct cli_credentials * creds )
{
struct tevent_context * ev = NULL ;
struct tevent_req * req = NULL ;
NTSTATUS status = NT_STATUS_NO_MEMORY ;
ev = samba_tevent_context_init ( talloc_tos ( ) ) ;
if ( ev = = NULL ) {
goto fail ;
}
req = cli_smb1_setup_encryption_send ( ev , ev , cli , creds ) ;
if ( req = = NULL ) {
goto fail ;
}
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
goto fail ;
}
status = cli_smb1_setup_encryption_recv ( req ) ;
fail :
TALLOC_FREE ( ev ) ;
return status ;
}
2003-08-19 21:47:44 +04:00
/**
establishes a connection right up to doing tconX , password specified .
@ param output_cli A fully initialised cli structure , non - null only on success
@ param dest_host The netbios name of the remote host
@ param dest_ip ( optional ) The the destination IP , NULL for name based lookup
@ param port ( optional ) The destination port ( 0 for default )
@ param service ( optional ) The share to make the connection to . Should be ' unqualified ' in any way .
2023-12-18 23:13:56 +03:00
@ param service_type The ' type ' of service .
2016-10-26 14:48:58 +03:00
@ param creds The used user credentials
2003-08-19 21:47:44 +04:00
*/
2016-10-26 14:48:58 +03:00
struct cli_full_connection_creds_state {
2013-04-05 16:55:26 +04:00
struct tevent_context * ev ;
const char * service ;
const char * service_type ;
2016-10-26 14:48:58 +03:00
struct cli_credentials * creds ;
2013-04-05 16:55:26 +04:00
int flags ;
struct cli_state * cli ;
} ;
2016-10-26 14:48:58 +03:00
static int cli_full_connection_creds_state_destructor (
2016-11-04 13:34:02 +03:00
struct cli_full_connection_creds_state * s )
{
if ( s - > cli ! = NULL ) {
cli_shutdown ( s - > cli ) ;
s - > cli = NULL ;
}
return 0 ;
}
static void cli_full_connection_creds_conn_done ( struct tevent_req * subreq ) ;
static void cli_full_connection_creds_sess_done ( struct tevent_req * subreq ) ;
2020-06-08 09:04:24 +03:00
static void cli_full_connection_creds_enc_start ( struct tevent_req * req ) ;
static void cli_full_connection_creds_enc_tcon ( struct tevent_req * subreq ) ;
static void cli_full_connection_creds_enc_ver ( struct tevent_req * subreq ) ;
static void cli_full_connection_creds_enc_done ( struct tevent_req * subreq ) ;
2021-08-11 16:30:12 +03:00
static void cli_full_connection_creds_enc_tdis ( struct tevent_req * req ) ;
static void cli_full_connection_creds_enc_finished ( struct tevent_req * subreq ) ;
2016-11-04 13:34:02 +03:00
static void cli_full_connection_creds_tcon_start ( struct tevent_req * req ) ;
static void cli_full_connection_creds_tcon_done ( struct tevent_req * subreq ) ;
2013-04-05 16:55:26 +04:00
2016-10-26 14:48:58 +03:00
struct tevent_req * cli_full_connection_creds_send (
2013-04-05 16:55:26 +04:00
TALLOC_CTX * mem_ctx , struct tevent_context * ev ,
const char * my_name , const char * dest_host ,
const struct sockaddr_storage * dest_ss , int port ,
const char * service , const char * service_type ,
2016-10-26 14:48:58 +03:00
struct cli_credentials * creds ,
2022-08-26 15:17:26 +03:00
int flags ,
struct smb2_negotiate_contexts * negotiate_contexts )
2003-08-19 21:47:44 +04:00
{
2013-04-05 16:55:26 +04:00
struct tevent_req * req , * subreq ;
2016-10-26 14:48:58 +03:00
struct cli_full_connection_creds_state * state ;
2020-05-28 19:20:02 +03:00
enum smb_signing_setting signing_state ;
2020-06-08 09:04:24 +03:00
enum smb_encryption_setting encryption_state =
cli_credentials_get_smb_encryption ( creds ) ;
2013-04-05 16:55:26 +04:00
req = tevent_req_create ( mem_ctx , & state ,
2016-10-26 14:48:58 +03:00
struct cli_full_connection_creds_state ) ;
2013-04-05 16:55:26 +04:00
if ( req = = NULL ) {
return NULL ;
}
2016-10-26 14:48:58 +03:00
talloc_set_destructor ( state , cli_full_connection_creds_state_destructor ) ;
2013-04-05 16:55:26 +04:00
state - > ev = ev ;
state - > service = service ;
state - > service_type = service_type ;
2016-10-26 14:48:58 +03:00
state - > creds = creds ;
2013-04-05 16:55:26 +04:00
state - > flags = flags ;
2003-08-19 21:47:44 +04:00
2020-05-28 19:20:02 +03:00
if ( flags & CLI_FULL_CONNECTION_IPC ) {
signing_state = cli_credentials_get_smb_ipc_signing ( creds ) ;
} else {
signing_state = cli_credentials_get_smb_signing ( creds ) ;
}
2020-06-08 09:04:24 +03:00
if ( encryption_state = = SMB_ENCRYPTION_REQUIRED ) {
if ( flags & CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK ) {
encryption_state = SMB_ENCRYPTION_DESIRED ;
}
}
if ( encryption_state > = SMB_ENCRYPTION_DESIRED ) {
signing_state = SMB_SIGNING_REQUIRED ;
}
2013-04-05 16:55:26 +04:00
subreq = cli_start_connection_send (
state , ev , my_name , dest_host , dest_ss , port ,
2022-08-26 15:17:26 +03:00
signing_state , flags ,
negotiate_contexts ) ;
2013-04-05 16:55:26 +04:00
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
2016-11-04 13:34:02 +03:00
tevent_req_set_callback ( subreq ,
cli_full_connection_creds_conn_done ,
req ) ;
2013-04-05 16:55:26 +04:00
return req ;
}
2006-07-11 22:01:26 +04:00
2016-11-04 13:34:02 +03:00
static void cli_full_connection_creds_conn_done ( struct tevent_req * subreq )
2013-04-05 16:55:26 +04:00
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
2016-10-26 14:48:58 +03:00
struct cli_full_connection_creds_state * state = tevent_req_data (
req , struct cli_full_connection_creds_state ) ;
2013-04-05 16:55:26 +04:00
NTSTATUS status ;
2007-10-25 01:16:54 +04:00
2023-10-31 17:05:35 +03:00
status = cli_start_connection_recv ( subreq , state , & state - > cli ) ;
2013-04-05 16:55:26 +04:00
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
2003-08-19 21:47:44 +04:00
}
2016-10-26 14:17:59 +03:00
subreq = cli_session_setup_creds_send (
2016-10-26 14:48:58 +03:00
state , state - > ev , state - > cli , state - > creds ) ;
2013-04-05 16:55:26 +04:00
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
2016-11-04 13:34:02 +03:00
tevent_req_set_callback ( subreq ,
cli_full_connection_creds_sess_done ,
req ) ;
2013-04-05 16:55:26 +04:00
}
2003-08-19 21:47:44 +04:00
2016-11-04 13:34:02 +03:00
static void cli_full_connection_creds_sess_done ( struct tevent_req * subreq )
2013-04-05 16:55:26 +04:00
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
2016-10-26 14:48:58 +03:00
struct cli_full_connection_creds_state * state = tevent_req_data (
req , struct cli_full_connection_creds_state ) ;
2013-04-05 16:55:26 +04:00
NTSTATUS status ;
2006-08-16 21:14:16 +04:00
2016-10-26 14:17:59 +03:00
status = cli_session_setup_creds_recv ( subreq ) ;
2013-04-05 16:55:26 +04:00
TALLOC_FREE ( subreq ) ;
if ( ! NT_STATUS_IS_OK ( status ) & &
( state - > flags & CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK ) ) {
2001-12-03 10:42:18 +03:00
2013-04-05 16:55:26 +04:00
state - > flags & = ~ CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK ;
2016-10-26 14:48:58 +03:00
state - > creds = cli_credentials_init_anon ( state ) ;
if ( tevent_req_nomem ( state - > creds , req ) ) {
2016-10-26 14:17:59 +03:00
return ;
}
2022-08-25 11:31:42 +03:00
subreq = cli_session_setup_creds_send (
state , state - > ev , state - > cli , state - > creds ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq ,
cli_full_connection_creds_sess_done ,
req ) ;
2013-04-05 16:55:26 +04:00
return ;
2006-08-16 21:14:16 +04:00
}
2007-10-25 01:16:54 +04:00
2013-04-05 16:55:26 +04:00
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2020-06-08 09:04:24 +03:00
cli_full_connection_creds_enc_start ( req ) ;
}
static void cli_full_connection_creds_enc_start ( struct tevent_req * req )
{
struct cli_full_connection_creds_state * state = tevent_req_data (
req , struct cli_full_connection_creds_state ) ;
enum smb_encryption_setting encryption_state =
cli_credentials_get_smb_encryption ( state - > creds ) ;
struct tevent_req * subreq = NULL ;
NTSTATUS status ;
if ( encryption_state < SMB_ENCRYPTION_DESIRED ) {
cli_full_connection_creds_tcon_start ( req ) ;
return ;
}
if ( smbXcli_conn_protocol ( state - > cli - > conn ) > = PROTOCOL_SMB2_02 ) {
status = smb2cli_session_encryption_on ( state - > cli - > smb2 . session ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_NOT_SUPPORTED ) ) {
if ( encryption_state < SMB_ENCRYPTION_REQUIRED ) {
cli_full_connection_creds_tcon_start ( req ) ;
return ;
}
d_printf ( " Encryption required and "
" server doesn't support "
" SMB3 encryption - failing connect \n " ) ;
tevent_req_nterror ( req , status ) ;
return ;
} else if ( ! NT_STATUS_IS_OK ( status ) ) {
d_printf ( " Encryption required and "
" setup failed with error %s. \n " ,
nt_errstr ( status ) ) ;
tevent_req_nterror ( req , status ) ;
return ;
}
cli_full_connection_creds_tcon_start ( req ) ;
return ;
}
if ( ! SERVER_HAS_UNIX_CIFS ( state - > cli ) ) {
if ( encryption_state < SMB_ENCRYPTION_REQUIRED ) {
cli_full_connection_creds_tcon_start ( req ) ;
return ;
}
status = NT_STATUS_NOT_SUPPORTED ;
d_printf ( " Encryption required and "
" server doesn't support "
" SMB1 Unix Extensions - failing connect \n " ) ;
tevent_req_nterror ( req , status ) ;
return ;
}
/*
* We do a tcon on IPC $ just to setup the encryption ,
* the real tcon will be encrypted then .
*/
subreq = cli_tree_connect_send ( state , state - > ev , state - > cli ,
" IPC$ " , " IPC " , NULL ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq , cli_full_connection_creds_enc_tcon , req ) ;
}
static void cli_full_connection_creds_enc_tcon ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_full_connection_creds_state * state = tevent_req_data (
req , struct cli_full_connection_creds_state ) ;
NTSTATUS status ;
status = cli_tree_connect_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
subreq = cli_unix_extensions_version_send ( state , state - > ev , state - > cli ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq , cli_full_connection_creds_enc_ver , req ) ;
}
static void cli_full_connection_creds_enc_ver ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_full_connection_creds_state * state = tevent_req_data (
req , struct cli_full_connection_creds_state ) ;
enum smb_encryption_setting encryption_state =
cli_credentials_get_smb_encryption ( state - > creds ) ;
uint16_t major , minor ;
uint32_t caplow , caphigh ;
NTSTATUS status ;
status = cli_unix_extensions_version_recv ( subreq ,
& major , & minor ,
& caplow ,
& caphigh ) ;
TALLOC_FREE ( subreq ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
if ( encryption_state < SMB_ENCRYPTION_REQUIRED ) {
2021-08-11 16:30:12 +03:00
/* disconnect ipc$ followed by the real tree connect */
cli_full_connection_creds_enc_tdis ( req ) ;
2020-06-08 09:04:24 +03:00
return ;
}
DEBUG ( 10 , ( " %s: cli_unix_extensions_version "
" returned %s \n " , __func__ , nt_errstr ( status ) ) ) ;
tevent_req_nterror ( req , NT_STATUS_UNKNOWN_REVISION ) ;
return ;
}
if ( ! ( caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP ) ) {
if ( encryption_state < SMB_ENCRYPTION_REQUIRED ) {
2021-08-11 16:30:12 +03:00
/* disconnect ipc$ followed by the real tree connect */
cli_full_connection_creds_enc_tdis ( req ) ;
2020-06-08 09:04:24 +03:00
return ;
}
DEBUG ( 10 , ( " %s: CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP "
" not supported \n " , __func__ ) ) ;
tevent_req_nterror ( req , NT_STATUS_UNSUPPORTED_COMPRESSION ) ;
return ;
}
subreq = cli_smb1_setup_encryption_send ( state , state - > ev ,
state - > cli ,
state - > creds ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq ,
cli_full_connection_creds_enc_done ,
req ) ;
}
static void cli_full_connection_creds_enc_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
NTSTATUS status ;
status = cli_smb1_setup_encryption_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2021-08-11 16:30:12 +03:00
/* disconnect ipc$ followed by the real tree connect */
cli_full_connection_creds_enc_tdis ( req ) ;
}
static void cli_full_connection_creds_enc_tdis ( struct tevent_req * req )
{
struct cli_full_connection_creds_state * state = tevent_req_data (
req , struct cli_full_connection_creds_state ) ;
struct tevent_req * subreq = NULL ;
subreq = cli_tdis_send ( state , state - > ev , state - > cli ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq ,
cli_full_connection_creds_enc_finished ,
req ) ;
}
static void cli_full_connection_creds_enc_finished ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
NTSTATUS status ;
status = cli_tdis_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2016-11-04 13:34:02 +03:00
cli_full_connection_creds_tcon_start ( req ) ;
}
2016-10-26 14:48:58 +03:00
2016-11-04 13:34:02 +03:00
static void cli_full_connection_creds_tcon_start ( struct tevent_req * req )
{
struct cli_full_connection_creds_state * state = tevent_req_data (
req , struct cli_full_connection_creds_state ) ;
struct tevent_req * subreq = NULL ;
const char * password = NULL ;
2016-10-26 14:48:58 +03:00
2016-11-04 13:34:02 +03:00
if ( state - > service = = NULL ) {
tevent_req_done ( req ) ;
2013-04-05 16:55:26 +04:00
return ;
2001-12-03 10:42:18 +03:00
}
2016-11-04 13:34:02 +03:00
password = cli_credentials_get_password ( state - > creds ) ;
subreq = cli_tree_connect_send ( state , state - > ev ,
state - > cli ,
state - > service ,
state - > service_type ,
2016-12-08 09:13:57 +03:00
password ) ;
2016-11-04 13:34:02 +03:00
if ( tevent_req_nomem ( subreq , req ) ) {
return ;
}
tevent_req_set_callback ( subreq ,
cli_full_connection_creds_tcon_done ,
req ) ;
2013-04-05 16:55:26 +04:00
}
2016-11-04 13:34:02 +03:00
static void cli_full_connection_creds_tcon_done ( struct tevent_req * subreq )
2013-04-05 16:55:26 +04:00
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
NTSTATUS status ;
status = cli_tree_connect_recv ( subreq ) ;
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
}
2014-09-26 05:35:30 +04:00
2013-04-05 16:55:26 +04:00
tevent_req_done ( req ) ;
}
2016-10-26 14:48:58 +03:00
NTSTATUS cli_full_connection_creds_recv ( struct tevent_req * req ,
2023-11-23 16:57:42 +03:00
TALLOC_CTX * mem_ctx ,
struct cli_state * * output_cli )
2013-04-05 16:55:26 +04:00
{
2016-10-26 14:48:58 +03:00
struct cli_full_connection_creds_state * state = tevent_req_data (
req , struct cli_full_connection_creds_state ) ;
2013-04-05 16:55:26 +04:00
NTSTATUS status ;
2001-12-03 10:42:18 +03:00
2013-04-05 16:55:26 +04:00
if ( tevent_req_is_nterror ( req , & status ) ) {
return status ;
}
2023-11-23 16:57:42 +03:00
* output_cli = talloc_move ( mem_ctx , & state - > cli ) ;
2013-04-05 16:55:26 +04:00
talloc_set_destructor ( state , NULL ) ;
2001-12-03 10:42:18 +03:00
return NT_STATUS_OK ;
}
2000-04-25 18:04:06 +04:00
2023-11-23 17:07:53 +03:00
NTSTATUS cli_full_connection_creds ( TALLOC_CTX * mem_ctx ,
struct cli_state * * output_cli ,
2016-10-26 14:48:58 +03:00
const char * my_name ,
const char * dest_host ,
const struct sockaddr_storage * dest_ss , int port ,
const char * service , const char * service_type ,
struct cli_credentials * creds ,
2020-06-04 15:59:14 +03:00
int flags )
2013-04-05 16:55:26 +04:00
{
struct tevent_context * ev ;
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_NO_MEMORY ;
2023-11-23 17:07:53 +03:00
ev = samba_tevent_context_init ( mem_ctx ) ;
2013-04-05 16:55:26 +04:00
if ( ev = = NULL ) {
goto fail ;
}
2016-10-26 14:48:58 +03:00
req = cli_full_connection_creds_send (
2013-04-05 16:55:26 +04:00
ev , ev , my_name , dest_host , dest_ss , port , service ,
2022-08-26 15:17:26 +03:00
service_type , creds , flags ,
NULL ) ;
2013-04-05 16:55:26 +04:00
if ( req = = NULL ) {
goto fail ;
}
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
goto fail ;
}
2023-11-23 17:07:53 +03:00
status = cli_full_connection_creds_recv ( req , mem_ctx , output_cli ) ;
2013-04-05 16:55:26 +04:00
fail :
TALLOC_FREE ( ev ) ;
return status ;
}
2003-04-14 08:15:24 +04:00
/****************************************************************************
Send an old style tcon .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2013-04-05 17:29:14 +04:00
struct cli_raw_tcon_state {
2011-03-27 20:03:24 +04:00
uint16_t * ret_vwv ;
2013-04-05 17:29:14 +04:00
} ;
static void cli_raw_tcon_done ( struct tevent_req * subreq ) ;
static struct tevent_req * cli_raw_tcon_send (
TALLOC_CTX * mem_ctx , struct tevent_context * ev , struct cli_state * cli ,
const char * service , const char * pass , const char * dev )
{
struct tevent_req * req , * subreq ;
struct cli_raw_tcon_state * state ;
2011-03-27 20:03:24 +04:00
uint8_t * bytes ;
2013-04-05 17:29:14 +04:00
req = tevent_req_create ( mem_ctx , & state , struct cli_raw_tcon_state ) ;
if ( req = = NULL ) {
return NULL ;
}
2003-04-14 08:15:24 +04:00
2003-06-08 16:49:31 +04:00
if ( ! lp_client_plaintext_auth ( ) & & ( * pass ) ) {
2012-06-06 10:22:50 +04:00
DEBUG ( 1 , ( " Server requested PLAINTEXT password but 'client plaintext auth = no' "
" or 'client ntlmv2 auth = yes' \n " ) ) ;
2013-04-05 17:29:14 +04:00
tevent_req_nterror ( req , NT_STATUS_ACCESS_DENIED ) ;
return tevent_req_post ( req , ev ) ;
2003-06-08 16:49:31 +04:00
}
2017-06-14 02:37:39 +03:00
TALLOC_FREE ( cli - > smb1 . tcon ) ;
cli - > smb1 . tcon = smbXcli_tcon_create ( cli ) ;
if ( tevent_req_nomem ( cli - > smb1 . tcon , req ) ) {
return tevent_req_post ( req , ev ) ;
}
smb1cli_tcon_set_id ( cli - > smb1 . tcon , UINT16_MAX ) ;
2013-04-05 17:29:14 +04:00
bytes = talloc_array ( state , uint8_t , 0 ) ;
2011-03-27 20:03:24 +04:00
bytes = smb_bytes_push_bytes ( bytes , 4 , NULL , 0 ) ;
2012-05-26 14:14:51 +04:00
bytes = smb_bytes_push_str ( bytes , smbXcli_conn_use_unicode ( cli - > conn ) ,
2011-03-27 20:03:24 +04:00
service , strlen ( service ) + 1 , NULL ) ;
bytes = smb_bytes_push_bytes ( bytes , 4 , NULL , 0 ) ;
2012-05-26 14:14:51 +04:00
bytes = smb_bytes_push_str ( bytes , smbXcli_conn_use_unicode ( cli - > conn ) ,
2011-03-27 20:03:24 +04:00
pass , strlen ( pass ) + 1 , NULL ) ;
bytes = smb_bytes_push_bytes ( bytes , 4 , NULL , 0 ) ;
2012-05-26 14:14:51 +04:00
bytes = smb_bytes_push_str ( bytes , smbXcli_conn_use_unicode ( cli - > conn ) ,
2011-03-27 20:03:24 +04:00
dev , strlen ( dev ) + 1 , NULL ) ;
2013-04-05 17:29:14 +04:00
if ( tevent_req_nomem ( bytes , req ) ) {
return tevent_req_post ( req , ev ) ;
}
2016-08-18 22:20:25 +03:00
subreq = cli_smb_send ( state , ev , cli , SMBtcon , 0 , 0 , 0 , NULL ,
2013-04-05 17:29:14 +04:00
talloc_get_size ( bytes ) , bytes ) ;
if ( tevent_req_nomem ( subreq , req ) ) {
return tevent_req_post ( req , ev ) ;
}
tevent_req_set_callback ( subreq , cli_raw_tcon_done , req ) ;
return req ;
}
static void cli_raw_tcon_done ( struct tevent_req * subreq )
{
struct tevent_req * req = tevent_req_callback_data (
subreq , struct tevent_req ) ;
struct cli_raw_tcon_state * state = tevent_req_data (
req , struct cli_raw_tcon_state ) ;
NTSTATUS status ;
status = cli_smb_recv ( subreq , state , NULL , 2 , NULL , & state - > ret_vwv ,
NULL , NULL ) ;
TALLOC_FREE ( subreq ) ;
if ( tevent_req_nterror ( req , status ) ) {
return ;
2003-04-14 08:15:24 +04:00
}
2013-04-05 17:29:14 +04:00
tevent_req_done ( req ) ;
}
2003-04-14 08:15:24 +04:00
2013-04-05 17:29:14 +04:00
static NTSTATUS cli_raw_tcon_recv ( struct tevent_req * req ,
2015-05-10 02:59:45 +03:00
uint16_t * max_xmit , uint16_t * tid )
2013-04-05 17:29:14 +04:00
{
struct cli_raw_tcon_state * state = tevent_req_data (
req , struct cli_raw_tcon_state ) ;
NTSTATUS status ;
2003-04-14 08:15:24 +04:00
2013-04-05 17:29:14 +04:00
if ( tevent_req_is_nterror ( req , & status ) ) {
return status ;
}
* max_xmit = SVAL ( state - > ret_vwv + 0 , 0 ) ;
* tid = SVAL ( state - > ret_vwv + 1 , 0 ) ;
2003-04-14 08:15:24 +04:00
return NT_STATUS_OK ;
}
2013-04-05 17:29:14 +04:00
NTSTATUS cli_raw_tcon ( struct cli_state * cli ,
const char * service , const char * pass , const char * dev ,
2015-05-10 02:59:45 +03:00
uint16_t * max_xmit , uint16_t * tid )
2013-04-05 17:29:14 +04:00
{
struct tevent_context * ev ;
struct tevent_req * req ;
NTSTATUS status = NT_STATUS_NO_MEMORY ;
ev = samba_tevent_context_init ( talloc_tos ( ) ) ;
if ( ev = = NULL ) {
goto fail ;
}
req = cli_raw_tcon_send ( ev , ev , cli , service , pass , dev ) ;
if ( req = = NULL ) {
goto fail ;
}
if ( ! tevent_req_poll_ntstatus ( req , ev , & status ) ) {
goto fail ;
}
status = cli_raw_tcon_recv ( req , max_xmit , tid ) ;
fail :
TALLOC_FREE ( ev ) ;
return status ;
}
2003-04-14 08:15:24 +04:00
/* Return a cli_state pointing at the IPC$ share for the given server */
2023-11-23 17:53:29 +03:00
static struct cli_state * get_ipc_connect ( TALLOC_CTX * mem_ctx ,
char * server ,
2023-11-23 17:52:05 +03:00
struct sockaddr_storage * server_ss ,
struct cli_credentials * creds )
2003-04-14 08:15:24 +04:00
{
struct cli_state * cli ;
NTSTATUS nt_status ;
2008-05-05 18:58:24 +04:00
uint32_t flags = CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK ;
2017-07-08 01:57:59 +03:00
flags | = CLI_FULL_CONNECTION_FORCE_SMB1 ;
2020-05-28 18:22:12 +03:00
flags | = CLI_FULL_CONNECTION_IPC ;
2017-07-08 01:57:59 +03:00
2023-11-23 17:53:29 +03:00
nt_status = cli_full_connection_creds ( mem_ctx ,
2023-11-23 17:07:53 +03:00
& cli ,
NULL ,
server ,
server_ss ,
0 ,
" IPC$ " ,
" IPC " ,
2021-01-13 17:35:11 +03:00
creds ,
2023-11-23 17:07:53 +03:00
flags ) ;
2003-04-14 08:15:24 +04:00
if ( NT_STATUS_IS_OK ( nt_status ) ) {
return cli ;
2020-04-13 10:23:45 +03:00
}
if ( is_ipaddress ( server ) ) {
2003-04-14 08:15:24 +04:00
/* windows 9* needs a correct NMB name for connections */
fstring remote_name ;
2007-10-25 01:16:54 +04:00
if ( name_status_find ( " * " , 0 , 0 , server_ss , remote_name ) ) {
2023-11-23 17:53:29 +03:00
cli = get_ipc_connect ( mem_ctx , remote_name , server_ss , creds ) ;
2003-04-14 08:15:24 +04:00
if ( cli )
return cli ;
}
}
return NULL ;
}
2004-03-19 19:22:47 +03:00
/*
* Given the IP address of a master browser on the network , return its
* workgroup and connect to it .
*
* This function is provided to allow additional processing beyond what
* get_ipc_connect_master_ip_bcast ( ) does , e . g . to retrieve the list of master
* browsers and obtain each master browsers ' list of domains ( in case the
* first master browser is recently on the network and has not yet
* synchronized with other master browsers and therefore does not yet have the
* entire network browse list )
*/
2003-04-14 08:15:24 +04:00
2007-11-30 00:24:54 +03:00
struct cli_state * get_ipc_connect_master_ip ( TALLOC_CTX * ctx ,
2011-04-30 19:10:35 +04:00
struct sockaddr_storage * mb_ip ,
2021-01-13 17:40:08 +03:00
struct cli_credentials * creds ,
2007-11-30 00:24:54 +03:00
char * * pp_workgroup_out )
2003-04-14 08:15:24 +04:00
{
2007-10-25 01:16:54 +04:00
char addr [ INET6_ADDRSTRLEN ] ;
2007-11-11 17:38:50 +03:00
fstring name ;
2003-04-14 08:15:24 +04:00
struct cli_state * cli ;
2007-10-25 01:16:54 +04:00
struct sockaddr_storage server_ss ;
2003-04-14 08:15:24 +04:00
2007-11-30 00:24:54 +03:00
* pp_workgroup_out = NULL ;
2011-04-30 19:10:35 +04:00
print_sockaddr ( addr , sizeof ( addr ) , mb_ip ) ;
2004-03-19 19:22:47 +03:00
DEBUG ( 99 , ( " Looking up name of master browser %s \n " ,
2007-10-25 01:16:54 +04:00
addr ) ) ;
2004-03-19 19:22:47 +03:00
/*
* Do a name status query to find out the name of the master browser .
* We use < 01 > < 02 > __MSBROWSE__ < 02 > # 01 if * # 00 fails because a domain
* master browser will not respond to a wildcard query ( or , at least ,
* an NT4 server acting as the domain master browser will not ) .
*
* We might be able to use ONLY the query on MSBROWSE , but that ' s not
* yet been tested with all Windows versions , so until it is , leave
* the original wildcard query as the first choice and fall back to
* MSBROWSE if the wildcard query fails .
*/
2011-04-30 19:10:35 +04:00
if ( ! name_status_find ( " * " , 0 , 0x1d , mb_ip , name ) & &
! name_status_find ( MSBROWSE , 1 , 0x1d , mb_ip , name ) ) {
2003-04-14 08:15:24 +04:00
2004-03-19 19:22:47 +03:00
DEBUG ( 99 , ( " Could not retrieve name status for %s \n " ,
2007-10-25 01:16:54 +04:00
addr ) ) ;
2004-03-19 19:22:47 +03:00
return NULL ;
2003-04-14 08:15:24 +04:00
}
2007-10-25 01:16:54 +04:00
if ( ! find_master_ip ( name , & server_ss ) ) {
2004-03-19 19:22:47 +03:00
DEBUG ( 99 , ( " Could not find master ip for %s \n " , name ) ) ;
return NULL ;
}
2003-04-14 08:15:24 +04:00
2007-11-30 00:24:54 +03:00
* pp_workgroup_out = talloc_strdup ( ctx , name ) ;
2003-04-14 08:15:24 +04:00
2007-10-25 01:16:54 +04:00
DEBUG ( 4 , ( " found master browser %s, %s \n " , name , addr ) ) ;
2003-04-14 08:15:24 +04:00
2007-10-25 01:16:54 +04:00
print_sockaddr ( addr , sizeof ( addr ) , & server_ss ) ;
2023-11-23 17:53:29 +03:00
cli = get_ipc_connect ( ctx , addr , & server_ss , creds ) ;
2003-04-14 08:15:24 +04:00
2007-10-25 01:16:54 +04:00
return cli ;
2004-03-19 19:22:47 +03:00
}