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
the Free Software Foundation ; either version 2 of the License , or
( 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
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# 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"
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
*/
2004-08-19 00:13:08 +04:00
BOOL smbcli_socket_connect ( struct smbcli_state * cli , const char * server )
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
2005-10-28 15:02:42 +04:00
sock = smbcli_sock_connect_byname ( server , 0 , NULL , NULL ) ;
2003-08-13 05:53:07 +04:00
2005-10-28 15:02:42 +04:00
if ( sock = = NULL ) return False ;
2003-08-13 05:53:07 +04:00
2005-01-24 03:57:14 +03:00
cli - > transport = smbcli_transport_init ( sock , cli , True ) ;
2003-08-13 05:53:07 +04:00
if ( ! cli - > transport ) {
return False ;
}
return True ;
}
2004-08-04 17:23:35 +04:00
/* wrapper around smbcli_transport_connect() */
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() */
2004-08-04 17:23:35 +04:00
NTSTATUS smbcli_negprot ( struct smbcli_state * cli )
2003-08-13 05:53:07 +04:00
{
2005-01-15 14:58:52 +03:00
return smb_raw_negotiate ( cli - > transport , lp_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 ,
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
struct cli_credentials * credentials )
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 ;
2005-01-24 03:57:14 +03:00
cli - > session = smbcli_session_init ( cli - > transport , cli , True ) ;
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 ;
setup . in . workgroup = lp_workgroup ( ) ;
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 ;
2005-01-24 03:57:14 +03: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 ,
const char * sharename ,
const char * devtype ,
2005-06-16 15:36:09 +04:00
struct cli_credentials * credentials ,
struct event_context * ev )
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 ,
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
& tree , host , 0 , sharename , devtype ,
2005-06-16 15:36:09 +04:00
credentials , ev ) ;
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
*/
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 ;
2006-02-08 08:13:11 +03:00
* hostname = * sharename = NULL ;
2005-01-15 14:58:52 +03:00
if ( strncmp ( unc_name , " \\ \\ " , 2 ) & &
strncmp ( unc_name , " // " , 2 ) ) {
return False ;
}
* hostname = talloc_strdup ( mem_ctx , & unc_name [ 2 ] ) ;
2006-01-31 09:09:18 +03:00
p = terminate_path_at_separator ( * hostname ) ;
2006-02-08 08:13:11 +03:00
if ( p & & * p ) {
* 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 ) {
return True ;
}
talloc_free ( * hostname ) ;
talloc_free ( * sharename ) ;
* hostname = * sharename = NULL ;
return False ;
2005-01-15 14:58:52 +03:00
}