2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
client connect / disconnect routines
Copyright ( C ) Andrew Tridgell 2003
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"
/*
2004-08-04 17:23:35 +04:00
wrapper around smbcli_sock_connect ( )
2003-08-13 05:53:07 +04:00
*/
2004-08-04 17:23:35 +04:00
BOOL smbcli_socket_connect ( struct smbcli_state * cli , const char * server , struct in_addr * ip )
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
2004-08-04 17:23:35 +04:00
sock = smbcli_sock_init ( ) ;
2003-08-13 05:53:07 +04:00
if ( ! sock ) return False ;
2004-08-04 17:23:35 +04:00
if ( ! smbcli_sock_connect_byname ( sock , server , 0 ) ) {
smbcli_sock_close ( sock ) ;
2003-08-13 05:53:07 +04:00
return False ;
}
2004-08-04 17:23:35 +04:00
cli - > transport = smbcli_transport_init ( sock ) ;
2003-08-13 05:53:07 +04:00
if ( ! cli - > transport ) {
2004-08-04 17:23:35 +04:00
smbcli_sock_close ( sock ) ;
2003-08-13 05:53:07 +04:00
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 ,
2003-08-13 05:53:07 +04:00
struct nmb_name * calling ,
struct nmb_name * called )
{
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
{
2004-02-10 14:33:35 +03:00
return smb_raw_negotiate ( cli - > transport ) ;
2003-08-13 05:53:07 +04:00
}
/* wrapper around smb_raw_session_setup() */
2004-08-04 17:23:35 +04:00
NTSTATUS smbcli_session_setup ( struct smbcli_state * cli ,
2004-02-10 14:33:35 +03:00
const char * user ,
const char * password ,
const char * domain )
2003-08-13 05:53:07 +04:00
{
union smb_sesssetup setup ;
NTSTATUS status ;
TALLOC_CTX * mem_ctx ;
2004-08-04 17:23:35 +04:00
cli - > session = smbcli_session_init ( cli - > transport ) ;
2004-02-10 14:33:35 +03:00
if ( ! cli - > session ) return NT_STATUS_UNSUCCESSFUL ;
2003-08-13 05:53:07 +04:00
2004-08-04 17:23:35 +04:00
mem_ctx = talloc_init ( " smbcli_session_setup " ) ;
2004-02-10 14:33:35 +03:00
if ( ! mem_ctx ) return NT_STATUS_NO_MEMORY ;
2003-08-13 05:53:07 +04:00
setup . generic . level = RAW_SESSSETUP_GENERIC ;
setup . generic . in . sesskey = cli - > transport - > negotiate . sesskey ;
2004-07-10 14:24:58 +04:00
setup . generic . in . capabilities = cli - > transport - > negotiate . capabilities ;
if ( ! user | | ! user [ 0 ] ) {
setup . generic . in . password = NULL ;
setup . generic . in . user = " " ;
setup . generic . in . domain = " " ;
setup . generic . in . capabilities & = ~ CAP_EXTENDED_SECURITY ;
} else {
setup . generic . in . password = password ;
setup . generic . in . user = user ;
setup . generic . in . domain = domain ;
}
2003-08-13 05:53:07 +04:00
status = smb_raw_session_setup ( cli - > session , mem_ctx , & setup ) ;
cli - > session - > vuid = setup . generic . out . vuid ;
talloc_destroy ( mem_ctx ) ;
2004-02-10 14:33:35 +03:00
return status ;
2003-08-13 05:53:07 +04:00
}
/* wrapper around smb_tree_connect() */
2004-08-04 17:23:35 +04:00
NTSTATUS smbcli_send_tconX ( struct smbcli_state * cli , const char * sharename ,
2004-02-10 14:33:35 +03:00
const char * devtype , const char * password )
2003-08-13 05:53:07 +04:00
{
union smb_tcon tcon ;
TALLOC_CTX * mem_ctx ;
NTSTATUS status ;
2004-08-04 17:23:35 +04:00
cli - > tree = smbcli_tree_init ( cli - > session ) ;
2004-02-10 14:33:35 +03:00
if ( ! cli - > tree ) return NT_STATUS_UNSUCCESSFUL ;
2003-08-13 05:53:07 +04:00
cli - > tree - > reference_count + + ;
/* setup a tree connect */
tcon . generic . level = RAW_TCON_TCONX ;
tcon . tconx . in . flags = 0 ;
tcon . tconx . in . password = data_blob ( password , strlen ( password ) + 1 ) ;
tcon . tconx . in . path = sharename ;
tcon . tconx . in . device = devtype ;
mem_ctx = talloc_init ( " tcon " ) ;
2004-02-10 14:33:35 +03:00
if ( ! mem_ctx )
return NT_STATUS_NO_MEMORY ;
2003-08-13 05:53:07 +04:00
status = smb_tree_connect ( cli - > tree , mem_ctx , & tcon ) ;
cli - > tree - > tid = tcon . tconx . out . cnum ;
talloc_destroy ( mem_ctx ) ;
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-08-04 17:23:35 +04:00
NTSTATUS smbcli_full_connection ( struct smbcli_state * * ret_cli ,
2003-08-13 05:53:07 +04:00
const char * myname ,
const char * host ,
struct in_addr * ip ,
const char * sharename ,
const char * devtype ,
const char * username ,
const char * domain ,
const char * password ,
uint_t flags ,
BOOL * retry )
{
2004-08-04 17:23:35 +04:00
struct smbcli_tree * tree ;
2003-08-13 05:53:07 +04:00
NTSTATUS status ;
2003-08-13 20:04:21 +04:00
char * p ;
TALLOC_CTX * mem_ctx ;
2004-08-04 17:23:35 +04:00
mem_ctx = talloc_init ( " smbcli_full_connection " ) ;
2003-08-13 05:53:07 +04:00
* ret_cli = NULL ;
2003-08-13 20:04:21 +04:00
/* if the username is of the form DOMAIN\username then split out the domain */
p = strpbrk ( username , " \\ / " ) ;
if ( p ) {
domain = talloc_strndup ( mem_ctx , username , PTR_DIFF ( p , username ) ) ;
username = talloc_strdup ( mem_ctx , p + 1 ) ;
}
2004-08-04 17:23:35 +04:00
status = smbcli_tree_full_connection ( & tree , myname , host , 0 , sharename , devtype ,
2003-08-13 05:53:07 +04:00
username , domain , password ) ;
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-08-04 17:23:35 +04:00
( * ret_cli ) = smbcli_state_init ( ) ;
2003-08-13 05:53:07 +04:00
( * ret_cli ) - > tree = tree ;
( * ret_cli ) - > session = tree - > session ;
( * ret_cli ) - > transport = tree - > session - > transport ;
tree - > reference_count + + ;
2003-08-13 20:04:21 +04:00
done :
talloc_destroy ( mem_ctx ) ;
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-08-04 17:23:35 +04:00
struct smbcli_state * smbcli_state_init ( void )
2003-08-13 05:53:07 +04:00
{
2004-08-04 17:23:35 +04:00
struct smbcli_state * cli ;
2003-08-13 05:53:07 +04:00
TALLOC_CTX * mem_ctx ;
2004-08-04 17:23:35 +04:00
mem_ctx = talloc_init ( " smbcli_state " ) ;
2003-08-13 05:53:07 +04:00
if ( ! mem_ctx ) return NULL ;
cli = talloc_zero ( mem_ctx , sizeof ( * cli ) ) ;
cli - > mem_ctx = mem_ctx ;
return cli ;
}
/****************************************************************************
Shutdown a client structure .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-08-04 17:23:35 +04:00
void smbcli_shutdown ( struct smbcli_state * cli )
2003-08-13 05:53:07 +04:00
{
2003-08-16 03:57:05 +04:00
if ( ! cli ) return ;
if ( cli - > tree ) {
cli - > tree - > reference_count + + ;
2004-08-04 17:23:35 +04:00
smbcli_tree_close ( cli - > tree ) ;
2003-08-16 03:57:05 +04:00
}
2003-08-13 05:53:07 +04:00
if ( cli - > mem_ctx ) {
talloc_destroy ( cli - > mem_ctx ) ;
}
}