2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
2005-01-21 14:18:56 +03:00
2003-08-13 05:53:07 +04:00
client connect / disconnect routines
2005-01-21 14:18:56 +03:00
Copyright ( C ) Andrew Tridgell 2003 - 2005
2006-01-31 09:09:18 +03:00
Copyright ( C ) James Peach 2005
2003-08-13 05:53:07 +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-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2003-08-13 05:53:07 +04:00
( at your option ) any later version .
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 .
You should have received a copy of the GNU General Public License
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2003-08-13 05:53:07 +04:00
*/
# include "includes.h"
2006-01-03 18:40:05 +03:00
# include "libcli/libcli.h"
2004-11-01 04:03:22 +03:00
# include "libcli/raw/libcliraw.h"
2008-04-02 06:53:27 +04:00
# include "libcli/raw/raw_proto.h"
2006-03-14 18:03:25 +03:00
# include "libcli/auth/libcli_auth.h"
2005-09-26 15:47:55 +04:00
# include "libcli/smb_composite/smb_composite.h"
2003-08-13 05:53:07 +04:00
/*
2004-08-04 17:23:35 +04:00
wrapper around smbcli_sock_connect ( )
2003-08-13 05:53:07 +04:00
*/
2007-12-03 23:25:06 +03:00
bool smbcli_socket_connect ( struct smbcli_state * cli , const char * server ,
2008-01-04 02:21:58 +03:00
const char * * ports ,
2008-12-29 22:24:57 +03:00
struct tevent_context * ev_ctx ,
2008-01-04 02:21:58 +03:00
struct resolve_context * resolve_ctx ,
2008-10-24 15:13:27 +04:00
struct smbcli_options * options ,
2008-11-02 00:13:47 +03:00
struct smb_iconv_convenience * iconv_convenience ,
const char * socket_options )
2003-08-13 05:53:07 +04:00
{
2004-08-04 17:23:35 +04:00
struct smbcli_socket * sock ;
2003-08-13 05:53:07 +04:00
2008-04-22 01:58:23 +04:00
sock = smbcli_sock_connect_byname ( server , ports , NULL ,
2008-11-02 00:13:47 +03:00
resolve_ctx , ev_ctx ,
socket_options ) ;
2003-08-13 05:53:07 +04:00
2007-10-07 02:28:14 +04:00
if ( sock = = NULL ) return false ;
2003-08-13 05:53:07 +04:00
2008-10-24 15:13:27 +04:00
cli - > transport = smbcli_transport_init ( sock , cli , true , options ,
iconv_convenience ) ;
2003-08-13 05:53:07 +04:00
if ( ! cli - > transport ) {
2007-10-07 02:28:14 +04:00
return false ;
2003-08-13 05:53:07 +04:00
}
2007-10-07 02:28:14 +04:00
return true ;
2003-08-13 05:53:07 +04:00
}
2004-08-04 17:23:35 +04:00
/* wrapper around smbcli_transport_connect() */
2007-10-07 02:28:14 +04:00
bool smbcli_transport_establish ( struct smbcli_state * cli ,
2005-01-21 14:18:56 +03:00
struct nbt_name * calling ,
struct nbt_name * called )
2003-08-13 05:53:07 +04:00
{
2004-08-04 17:23:35 +04:00
return smbcli_transport_connect ( cli - > transport , calling , called ) ;
2003-08-13 05:53:07 +04:00
}
/* wrapper around smb_raw_negotiate() */
2007-12-03 00:32:11 +03:00
NTSTATUS smbcli_negprot ( struct smbcli_state * cli , bool unicode , int maxprotocol )
2003-08-13 05:53:07 +04:00
{
2007-12-03 00:32:11 +03:00
return smb_raw_negotiate ( cli - > transport , unicode , maxprotocol ) ;
2003-08-13 05:53:07 +04:00
}
2005-08-09 08:11:09 +04:00
/* wrapper around smb_raw_sesssetup() */
2004-08-04 17:23:35 +04:00
NTSTATUS smbcli_session_setup ( struct smbcli_state * cli ,
2007-12-10 20:42:04 +03:00
struct cli_credentials * credentials ,
2008-09-30 03:38:51 +04:00
const char * workgroup ,
2008-11-02 04:05:48 +03:00
struct smbcli_session_options options ,
struct gensec_settings * gensec_settings )
2003-08-13 05:53:07 +04:00
{
2005-01-16 14:15:08 +03:00
struct smb_composite_sesssetup setup ;
2003-08-13 05:53:07 +04:00
NTSTATUS status ;
2008-09-30 03:29:53 +04:00
cli - > session = smbcli_session_init ( cli - > transport , cli , true ,
options ) ;
2004-02-10 14:33:35 +03:00
if ( ! cli - > session ) return NT_STATUS_UNSUCCESSFUL ;
2003-08-13 05:53:07 +04:00
2005-01-16 14:15:08 +03:00
setup . in . sesskey = cli - > transport - > negotiate . sesskey ;
setup . in . capabilities = cli - > transport - > negotiate . capabilities ;
r6028: A MAJOR update to intergrate the new credentails system fully with
GENSEC, and to pull SCHANNEL into GENSEC, by making it less 'special'.
GENSEC now no longer has it's own handling of 'set username' etc,
instead it uses cli_credentials calls.
In order to link the credentails code right though Samba, a lot of
interfaces have changed to remove 'username, domain, password'
arguments, and these have been replaced with a single 'struct
cli_credentials'.
In the session setup code, a new parameter 'workgroup' contains the
client/server current workgroup, which seems unrelated to the
authentication exchange (it was being filled in from the auth info).
This allows in particular kerberos to only call back for passwords
when it actually needs to perform the kinit.
The kerberos code has been modified not to use the SPNEGO provided
'principal name' (in the mechListMIC), but to instead use the name the
host was connected to as. This better matches Microsoft behaviour,
is more secure and allows better use of standard kerberos functions.
To achieve this, I made changes to our socket code so that the
hostname (before name resolution) is now recorded on the socket.
In schannel, most of the code from librpc/rpc/dcerpc_schannel.c is now
in libcli/auth/schannel.c, and it looks much more like a standard
GENSEC module. The actual sign/seal code moved to
libcli/auth/schannel_sign.c in a previous commit.
The schannel credentails structure is now merged with the rest of the
credentails, as many of the values (username, workstation, domain)
where already present there. This makes handling this in a generic
manner much easier, as there is no longer a custom entry-point.
The auth_domain module continues to be developed, but is now just as
functional as auth_winbind. The changes here are consequential to the
schannel changes.
The only removed function at this point is the RPC-LOGIN test
(simulating the load of a WinXP login), which needs much more work to
clean it up (it contains copies of too much code from all over the
torture suite, and I havn't been able to penetrate its 'structure').
Andrew Bartlett
(This used to be commit 2301a4b38a21aa60917973451687063d83d18d66)
2005-03-24 07:14:06 +03:00
setup . in . credentials = credentials ;
2007-12-10 20:42:04 +03:00
setup . in . workgroup = workgroup ;
2008-11-02 04:05:48 +03:00
setup . in . gensec_settings = gensec_settings ;
2003-08-13 05:53:07 +04:00
2005-01-16 14:15:08 +03:00
status = smb_composite_sesssetup ( cli - > session , & setup ) ;
2003-08-13 05:53:07 +04:00
2005-01-16 14:15:08 +03:00
cli - > session - > vuid = setup . out . vuid ;
2003-08-13 05:53:07 +04:00
2004-02-10 14:33:35 +03:00
return status ;
2003-08-13 05:53:07 +04:00
}
2005-08-09 07:09:47 +04:00
/* wrapper around smb_raw_tcon() */
2005-06-11 07:35:28 +04:00
NTSTATUS smbcli_tconX ( struct smbcli_state * cli , const char * sharename ,
const char * devtype , const char * password )
2003-08-13 05:53:07 +04:00
{
union smb_tcon tcon ;
TALLOC_CTX * mem_ctx ;
NTSTATUS status ;
2007-10-07 02:28:14 +04:00
cli - > tree = smbcli_tree_init ( cli - > session , cli , true ) ;
2004-02-10 14:33:35 +03:00
if ( ! cli - > tree ) return NT_STATUS_UNSUCCESSFUL ;
2003-08-13 05:53:07 +04:00
2004-09-21 08:41:17 +04:00
mem_ctx = talloc_init ( " tcon " ) ;
if ( ! mem_ctx ) {
return NT_STATUS_NO_MEMORY ;
}
2003-08-13 05:53:07 +04:00
/* setup a tree connect */
tcon . generic . level = RAW_TCON_TCONX ;
tcon . tconx . in . flags = 0 ;
2004-09-21 08:41:17 +04:00
if ( cli - > transport - > negotiate . sec_mode & NEGOTIATE_SECURITY_USER_LEVEL ) {
tcon . tconx . in . password = data_blob ( NULL , 0 ) ;
} else if ( cli - > transport - > negotiate . sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE ) {
2004-09-22 09:13:00 +04:00
tcon . tconx . in . password = data_blob_talloc ( mem_ctx , NULL , 24 ) ;
if ( cli - > transport - > negotiate . secblob . length < 8 ) {
return NT_STATUS_INVALID_PARAMETER ;
}
SMBencrypt ( password , cli - > transport - > negotiate . secblob . data , tcon . tconx . in . password . data ) ;
2004-09-21 08:41:17 +04:00
} else {
tcon . tconx . in . password = data_blob_talloc ( mem_ctx , password , strlen ( password ) + 1 ) ;
}
2003-08-13 05:53:07 +04:00
tcon . tconx . in . path = sharename ;
tcon . tconx . in . device = devtype ;
2005-08-09 07:09:47 +04:00
status = smb_raw_tcon ( cli - > tree , mem_ctx , & tcon ) ;
2003-08-13 05:53:07 +04:00
2005-01-13 21:49:10 +03:00
cli - > tree - > tid = tcon . tconx . out . tid ;
2003-08-13 05:53:07 +04:00
2004-09-08 04:00:56 +04:00
talloc_free ( mem_ctx ) ;
2003-08-13 05:53:07 +04:00
2004-02-10 14:33:35 +03:00
return status ;
2003-08-13 05:53:07 +04:00
}
/*
2004-08-04 17:23:35 +04:00
easy way to get to a fully connected smbcli_state in one call
2003-08-13 05:53:07 +04:00
*/
2004-09-28 09:44:59 +04:00
NTSTATUS smbcli_full_connection ( TALLOC_CTX * parent_ctx ,
struct smbcli_state * * ret_cli ,
const char * host ,
2007-12-12 04:15:29 +03:00
const char * * ports ,
2004-09-28 09:44:59 +04:00
const char * sharename ,
const char * devtype ,
2008-11-02 04:05:48 +03:00
const char * socket_options ,
2005-06-16 15:36:09 +04:00
struct cli_credentials * credentials ,
2008-01-03 03:39:01 +03:00
struct resolve_context * resolve_ctx ,
2008-12-29 22:24:57 +03:00
struct tevent_context * ev ,
2008-09-30 05:07:08 +04:00
struct smbcli_options * options ,
2008-10-24 15:13:27 +04:00
struct smbcli_session_options * session_options ,
2008-11-02 18:07:28 +03:00
struct smb_iconv_convenience * iconv_convenience ,
struct gensec_settings * gensec_settings )
2003-08-13 05:53:07 +04:00
{
2004-08-04 17:23:35 +04:00
struct smbcli_tree * tree ;
2003-08-13 05:53:07 +04:00
NTSTATUS status ;
* ret_cli = NULL ;
2004-09-28 09:44:59 +04:00
status = smbcli_tree_full_connection ( parent_ctx ,
2007-12-12 04:15:29 +03:00
& tree , host , ports ,
sharename , devtype ,
2008-11-02 04:05:48 +03:00
socket_options ,
2008-01-04 02:22:12 +03:00
credentials , resolve_ctx , ev ,
2008-09-30 05:07:08 +04:00
options ,
2008-10-24 15:13:27 +04:00
session_options ,
2008-11-02 18:07:28 +03:00
iconv_convenience ,
gensec_settings ) ;
2003-08-13 05:53:07 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2003-08-13 20:04:21 +04:00
goto done ;
2003-08-13 05:53:07 +04:00
}
2004-09-28 09:44:59 +04:00
( * ret_cli ) = smbcli_state_init ( parent_ctx ) ;
2003-08-13 05:53:07 +04:00
2004-09-28 09:44:59 +04:00
( * ret_cli ) - > tree = tree ;
2003-08-13 05:53:07 +04:00
( * ret_cli ) - > session = tree - > session ;
( * ret_cli ) - > transport = tree - > session - > transport ;
2005-07-26 11:23:57 +04:00
talloc_steal ( * ret_cli , tree ) ;
2004-09-28 09:44:59 +04:00
2003-08-13 20:04:21 +04:00
done :
2003-08-13 05:53:07 +04:00
return status ;
}
/*
disconnect the tree
*/
2004-08-04 17:23:35 +04:00
NTSTATUS smbcli_tdis ( struct smbcli_state * cli )
2003-08-13 05:53:07 +04:00
{
2004-02-10 14:33:35 +03:00
return smb_tree_disconnect ( cli - > tree ) ;
2003-08-13 05:53:07 +04:00
}
/****************************************************************************
Initialise a client state structure .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-28 09:44:59 +04:00
struct smbcli_state * smbcli_state_init ( TALLOC_CTX * mem_ctx )
2003-08-13 05:53:07 +04:00
{
2005-07-26 11:23:57 +04:00
return talloc_zero ( mem_ctx , struct smbcli_state ) ;
2003-08-13 05:53:07 +04:00
}
2006-01-31 09:09:18 +03:00
/* Insert a NULL at the first separator of the given path and return a pointer
2006-02-08 08:13:11 +03:00
* to the remainder of the string .
2006-01-31 09:09:18 +03:00
*/
static char *
terminate_path_at_separator ( char * path )
{
char * p ;
2006-02-08 08:13:11 +03:00
if ( ! path ) {
return NULL ;
}
2006-01-31 09:09:18 +03:00
if ( ( p = strchr_m ( path , ' / ' ) ) ) {
2006-02-08 08:13:11 +03:00
* p = ' \0 ' ;
return p + 1 ;
2006-01-31 09:09:18 +03:00
}
if ( ( p = strchr_m ( path , ' \\ ' ) ) ) {
2006-02-08 08:13:11 +03:00
* p = ' \0 ' ;
return p + 1 ;
2006-01-31 09:09:18 +03:00
}
2006-02-08 08:13:11 +03:00
/* No separator. */
return NULL ;
2006-01-31 09:09:18 +03:00
}
2005-01-15 14:58:52 +03:00
/*
parse a //server/share type UNC name
*/
2007-09-07 15:47:03 +04:00
bool smbcli_parse_unc ( const char * unc_name , TALLOC_CTX * mem_ctx ,
2006-01-31 09:09:18 +03:00
char * * hostname , char * * sharename )
2005-01-15 14:58:52 +03:00
{
char * p ;
if ( strncmp ( unc_name , " \\ \\ " , 2 ) & &
strncmp ( unc_name , " // " , 2 ) ) {
2007-10-07 02:28:14 +04:00
return false ;
2005-01-15 14:58:52 +03:00
}
2010-02-09 05:18:31 +03:00
* hostname = * sharename = NULL ;
2005-01-15 14:58:52 +03:00
* hostname = talloc_strdup ( mem_ctx , & unc_name [ 2 ] ) ;
2006-01-31 09:09:18 +03:00
p = terminate_path_at_separator ( * hostname ) ;
2007-09-07 15:47:03 +04:00
if ( p ! = NULL & & * p ) {
2006-02-08 08:13:11 +03:00
* sharename = talloc_strdup ( mem_ctx , p ) ;
terminate_path_at_separator ( * sharename ) ;
}
2005-01-15 14:58:52 +03:00
2006-02-08 08:13:11 +03:00
if ( * hostname & & * sharename ) {
2007-10-07 02:28:14 +04:00
return true ;
2006-02-08 08:13:11 +03:00
}
talloc_free ( * hostname ) ;
talloc_free ( * sharename ) ;
* hostname = * sharename = NULL ;
2007-10-07 02:28:14 +04:00
return false ;
2005-01-15 14:58:52 +03:00
}