2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
test suite for session setup operations
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-11-01 04:03:22 +03:00
# include "libcli/raw/libcliraw.h"
2004-11-30 07:33:27 +03:00
# include "librpc/gen_ndr/ndr_security.h"
2005-01-16 14:15:08 +03:00
# include "libcli/composite/composite.h"
2003-08-13 05:53:07 +04:00
# define BASEDIR "\\rawcontext"
# define CHECK_STATUS(status, correct) do { \
if ( ! NT_STATUS_EQUAL ( status , correct ) ) { \
2004-10-25 06:36:12 +04:00
printf ( " (%s) Incorrect status %s - should be %s \n " , \
__location__ , nt_errstr ( status ) , nt_errstr ( correct ) ) ; \
2003-08-13 05:53:07 +04:00
ret = False ; \
goto done ; \
} } while ( 0 )
# define CHECK_VALUE(v, correct) do { \
if ( ( v ) ! = ( correct ) ) { \
2004-10-25 06:36:12 +04:00
printf ( " (%s) Incorrect value %s=%d - should be %d \n " , \
__location__ , # v , v , correct ) ; \
2003-08-13 05:53:07 +04:00
ret = False ; \
goto done ; \
} } while ( 0 )
2004-08-12 11:29:49 +04:00
# define CHECK_NOT_VALUE(v, correct) do { \
if ( ( v ) = = ( correct ) ) { \
2004-10-25 06:36:12 +04:00
printf ( " (%s) Incorrect value %s=%d - should not be %d \n " , \
__location__ , # v , v , correct ) ; \
2004-08-12 11:29:49 +04:00
ret = False ; \
goto done ; \
} } while ( 0 )
2003-08-13 05:53:07 +04:00
/*
test session ops
*/
2004-08-04 17:23:35 +04:00
static BOOL test_session ( struct smbcli_state * cli , TALLOC_CTX * mem_ctx )
2003-08-13 05:53:07 +04:00
{
NTSTATUS status ;
BOOL ret = True ;
2004-08-23 11:22:31 +04:00
const char * username , * domain , * password ;
2004-08-04 17:23:35 +04:00
struct smbcli_session * session ;
2004-08-12 11:29:49 +04:00
struct smbcli_session * session2 ;
struct smbcli_session * session3 ;
2004-08-04 17:23:35 +04:00
struct smbcli_tree * tree ;
2005-01-16 14:15:08 +03:00
struct smb_composite_sesssetup setup ;
2003-08-13 05:53:07 +04:00
union smb_open io ;
union smb_write wr ;
union smb_close cl ;
int fnum ;
const char * fname = BASEDIR " \\ test.txt " ;
2004-12-04 16:56:25 +03:00
uint8_t c = 1 ;
2003-08-13 05:53:07 +04:00
printf ( " TESTING SESSION HANDLING \n " ) ;
2004-11-12 12:37:59 +03:00
if ( ! torture_setup_dir ( cli , BASEDIR ) ) {
2003-08-13 05:53:07 +04:00
return False ;
}
username = lp_parm_string ( - 1 , " torture " , " username " ) ;
password = lp_parm_string ( - 1 , " torture " , " password " ) ;
2004-08-25 18:31:59 +04:00
domain = lp_parm_string ( - 1 , " torture " , " userdomain " ) ;
2003-08-13 05:53:07 +04:00
printf ( " create a second security context on the same transport \n " ) ;
2005-01-24 03:57:14 +03:00
session = smbcli_session_init ( cli - > transport , mem_ctx , False ) ;
2004-09-26 15:30:20 +04:00
2005-01-16 14:15:08 +03:00
setup . in . sesskey = cli - > transport - > negotiate . sesskey ;
setup . in . capabilities = cli - > transport - > negotiate . capabilities ; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
setup . in . password = password ;
setup . in . user = username ;
setup . in . domain = domain ;
2003-08-13 05:53:07 +04:00
2005-01-16 14:15:08 +03:00
status = smb_composite_sesssetup ( session , & setup ) ;
2003-08-13 05:53:07 +04:00
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2005-01-16 14:15:08 +03:00
session - > vuid = setup . out . vuid ;
2003-08-13 05:53:07 +04:00
2004-08-12 11:29:49 +04:00
printf ( " create a third security context on the same transport, with vuid set \n " ) ;
2005-01-24 03:57:14 +03:00
session2 = smbcli_session_init ( cli - > transport , mem_ctx , False ) ;
2004-09-26 15:30:20 +04:00
2004-08-12 11:29:49 +04:00
session2 - > vuid = session - > vuid ;
2005-01-16 14:15:08 +03:00
setup . in . sesskey = cli - > transport - > negotiate . sesskey ;
setup . in . capabilities = cli - > transport - > negotiate . capabilities ; /* ignored in secondary session setup, except by our libs, which care about the extended security bit */
setup . in . password = password ;
setup . in . user = username ;
setup . in . domain = domain ;
status = smb_composite_sesssetup ( session2 , & setup ) ;
2004-08-12 11:29:49 +04:00
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2005-01-16 14:15:08 +03:00
session2 - > vuid = setup . out . vuid ;
2004-08-12 11:29:49 +04:00
printf ( " vuid1=%d vuid2=%d vuid3=%d \n " , cli - > session - > vuid , session - > vuid , session2 - > vuid ) ;
CHECK_NOT_VALUE ( session - > vuid , session2 - > vuid ) ;
2004-09-26 15:30:20 +04:00
talloc_free ( session2 ) ;
2004-08-12 11:29:49 +04:00
if ( cli - > transport - > negotiate . capabilities & CAP_EXTENDED_SECURITY ) {
printf ( " create a fourth security context on the same transport, without extended security \n " ) ;
2005-01-24 03:57:14 +03:00
session3 = smbcli_session_init ( cli - > transport , mem_ctx , False ) ;
2004-09-26 15:30:20 +04:00
2004-08-12 11:29:49 +04:00
session3 - > vuid = session - > vuid ;
2005-01-16 14:15:08 +03:00
setup . in . sesskey = cli - > transport - > negotiate . sesskey ;
setup . in . capabilities = 0 ; /* force a non extended security login (should fail) */
setup . in . password = password ;
setup . in . user = username ;
setup . in . domain = domain ;
status = smb_composite_sesssetup ( session3 , & setup ) ;
2004-08-12 11:29:49 +04:00
CHECK_STATUS ( status , NT_STATUS_ACCESS_DENIED ) ;
2004-09-26 15:30:20 +04:00
talloc_free ( session3 ) ;
2004-08-12 11:29:49 +04:00
}
2003-08-13 05:53:07 +04:00
printf ( " use the same tree as the existing connection \n " ) ;
2005-01-24 03:57:14 +03:00
tree = smbcli_tree_init ( session , mem_ctx , False ) ;
2003-08-13 05:53:07 +04:00
tree - > tid = cli - > tree - > tid ;
printf ( " create a file using the new vuid \n " ) ;
io . generic . level = RAW_OPEN_NTCREATEX ;
io . ntcreatex . in . root_fid = 0 ;
io . ntcreatex . in . flags = 0 ;
2004-12-02 07:37:36 +03:00
io . ntcreatex . in . access_mask = SEC_FLAG_MAXIMUM_ALLOWED ;
2003-08-13 05:53:07 +04:00
io . ntcreatex . in . create_options = 0 ;
io . ntcreatex . in . file_attr = FILE_ATTRIBUTE_NORMAL ;
io . ntcreatex . in . share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE ;
io . ntcreatex . in . alloc_size = 0 ;
io . ntcreatex . in . open_disposition = NTCREATEX_DISP_CREATE ;
io . ntcreatex . in . impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS ;
io . ntcreatex . in . security_flags = 0 ;
io . ntcreatex . in . fname = fname ;
status = smb_raw_open ( tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
fnum = io . ntcreatex . out . fnum ;
printf ( " write using the old vuid \n " ) ;
wr . generic . level = RAW_WRITE_WRITEX ;
wr . writex . in . fnum = fnum ;
wr . writex . in . offset = 0 ;
wr . writex . in . wmode = 0 ;
wr . writex . in . remaining = 0 ;
wr . writex . in . count = 1 ;
wr . writex . in . data = & c ;
status = smb_raw_write ( cli - > tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_INVALID_HANDLE ) ;
printf ( " write with the new vuid \n " ) ;
status = smb_raw_write ( tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
CHECK_VALUE ( wr . writex . out . nwritten , 1 ) ;
printf ( " logoff the new vuid \n " ) ;
status = smb_raw_ulogoff ( session ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2004-09-29 01:42:49 +04:00
talloc_free ( session ) ;
2003-08-13 05:53:07 +04:00
printf ( " the new vuid should not now be accessible \n " ) ;
status = smb_raw_write ( tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_INVALID_HANDLE ) ;
printf ( " the fnum should have been auto-closed \n " ) ;
cl . close . level = RAW_CLOSE_CLOSE ;
cl . close . in . fnum = fnum ;
cl . close . in . write_time = 0 ;
status = smb_raw_close ( cli - > tree , & cl ) ;
CHECK_STATUS ( status , NT_STATUS_INVALID_HANDLE ) ;
2004-09-26 15:30:20 +04:00
talloc_free ( tree ) ;
2003-08-13 05:53:07 +04:00
done :
return ret ;
}
/*
test tree ops
*/
2004-08-04 17:23:35 +04:00
static BOOL test_tree ( struct smbcli_state * cli , TALLOC_CTX * mem_ctx )
2003-08-13 05:53:07 +04:00
{
NTSTATUS status ;
BOOL ret = True ;
2004-08-23 11:22:31 +04:00
const char * share ;
2004-08-04 17:23:35 +04:00
struct smbcli_tree * tree ;
2003-08-13 05:53:07 +04:00
union smb_tcon tcon ;
union smb_open io ;
union smb_write wr ;
union smb_close cl ;
int fnum ;
const char * fname = BASEDIR " \\ test.txt " ;
2004-12-04 16:56:25 +03:00
uint8_t c = 1 ;
2003-08-13 05:53:07 +04:00
printf ( " TESTING TREE HANDLING \n " ) ;
2004-11-12 12:37:59 +03:00
if ( ! torture_setup_dir ( cli , BASEDIR ) ) {
2003-08-13 05:53:07 +04:00
return False ;
}
share = lp_parm_string ( - 1 , " torture " , " share " ) ;
2004-09-26 15:30:20 +04:00
2003-08-13 05:53:07 +04:00
printf ( " create a second tree context on the same session \n " ) ;
2005-01-24 03:57:14 +03:00
tree = smbcli_tree_init ( cli - > session , mem_ctx , False ) ;
2003-08-13 05:53:07 +04:00
tcon . generic . level = RAW_TCON_TCONX ;
tcon . tconx . in . flags = 0 ;
tcon . tconx . in . password = data_blob ( NULL , 0 ) ;
tcon . tconx . in . path = share ;
tcon . tconx . in . device = " A: " ;
status = smb_tree_connect ( tree , mem_ctx , & tcon ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2005-01-13 21:49:10 +03:00
tree - > tid = tcon . tconx . out . tid ;
2003-08-13 05:53:07 +04:00
printf ( " tid1=%d tid2=%d \n " , cli - > tree - > tid , tree - > tid ) ;
printf ( " try a tconx with a bad device type \n " ) ;
tcon . tconx . in . device = " FOO " ;
status = smb_tree_connect ( tree , mem_ctx , & tcon ) ;
CHECK_STATUS ( status , NT_STATUS_BAD_DEVICE_TYPE ) ;
printf ( " create a file using the new tid \n " ) ;
io . generic . level = RAW_OPEN_NTCREATEX ;
io . ntcreatex . in . root_fid = 0 ;
io . ntcreatex . in . flags = 0 ;
2004-12-02 07:37:36 +03:00
io . ntcreatex . in . access_mask = SEC_FLAG_MAXIMUM_ALLOWED ;
2003-08-13 05:53:07 +04:00
io . ntcreatex . in . create_options = 0 ;
io . ntcreatex . in . file_attr = FILE_ATTRIBUTE_NORMAL ;
io . ntcreatex . in . share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE ;
io . ntcreatex . in . alloc_size = 0 ;
io . ntcreatex . in . open_disposition = NTCREATEX_DISP_CREATE ;
io . ntcreatex . in . impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS ;
io . ntcreatex . in . security_flags = 0 ;
io . ntcreatex . in . fname = fname ;
status = smb_raw_open ( tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
fnum = io . ntcreatex . out . fnum ;
printf ( " write using the old tid \n " ) ;
wr . generic . level = RAW_WRITE_WRITEX ;
wr . writex . in . fnum = fnum ;
wr . writex . in . offset = 0 ;
wr . writex . in . wmode = 0 ;
wr . writex . in . remaining = 0 ;
wr . writex . in . count = 1 ;
wr . writex . in . data = & c ;
status = smb_raw_write ( cli - > tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_INVALID_HANDLE ) ;
printf ( " write with the new tid \n " ) ;
status = smb_raw_write ( tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
CHECK_VALUE ( wr . writex . out . nwritten , 1 ) ;
printf ( " disconnect the new tid \n " ) ;
status = smb_tree_disconnect ( tree ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
printf ( " the new tid should not now be accessible \n " ) ;
status = smb_raw_write ( tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_INVALID_HANDLE ) ;
printf ( " the fnum should have been auto-closed \n " ) ;
cl . close . level = RAW_CLOSE_CLOSE ;
cl . close . in . fnum = fnum ;
cl . close . in . write_time = 0 ;
status = smb_raw_close ( cli - > tree , & cl ) ;
CHECK_STATUS ( status , NT_STATUS_INVALID_HANDLE ) ;
/* close down the new tree */
2004-09-26 15:30:20 +04:00
talloc_free ( tree ) ;
2003-08-13 05:53:07 +04:00
done :
return ret ;
}
/*
test pid ops
*/
2004-08-04 17:23:35 +04:00
static BOOL test_pid ( struct smbcli_state * cli , TALLOC_CTX * mem_ctx )
2003-08-13 05:53:07 +04:00
{
NTSTATUS status ;
BOOL ret = True ;
union smb_open io ;
union smb_write wr ;
union smb_close cl ;
int fnum ;
const char * fname = BASEDIR " \\ test.txt " ;
2004-12-04 16:56:25 +03:00
uint8_t c = 1 ;
2004-05-25 21:24:24 +04:00
uint16_t pid1 , pid2 ;
2003-08-13 05:53:07 +04:00
printf ( " TESTING PID HANDLING \n " ) ;
2004-11-12 12:37:59 +03:00
if ( ! torture_setup_dir ( cli , BASEDIR ) ) {
2003-08-13 05:53:07 +04:00
return False ;
}
printf ( " create a second pid \n " ) ;
pid1 = cli - > session - > pid ;
pid2 = pid1 + 1 ;
printf ( " pid1=%d pid2=%d \n " , pid1 , pid2 ) ;
printf ( " create a file using the new pid \n " ) ;
cli - > session - > pid = pid2 ;
io . generic . level = RAW_OPEN_NTCREATEX ;
io . ntcreatex . in . root_fid = 0 ;
io . ntcreatex . in . flags = 0 ;
2004-12-02 07:37:36 +03:00
io . ntcreatex . in . access_mask = SEC_FLAG_MAXIMUM_ALLOWED ;
2003-08-13 05:53:07 +04:00
io . ntcreatex . in . create_options = 0 ;
io . ntcreatex . in . file_attr = FILE_ATTRIBUTE_NORMAL ;
io . ntcreatex . in . share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE ;
io . ntcreatex . in . alloc_size = 0 ;
io . ntcreatex . in . open_disposition = NTCREATEX_DISP_CREATE ;
io . ntcreatex . in . impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS ;
io . ntcreatex . in . security_flags = 0 ;
io . ntcreatex . in . fname = fname ;
status = smb_raw_open ( cli - > tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
fnum = io . ntcreatex . out . fnum ;
printf ( " write using the old pid \n " ) ;
cli - > session - > pid = pid1 ;
wr . generic . level = RAW_WRITE_WRITEX ;
wr . writex . in . fnum = fnum ;
wr . writex . in . offset = 0 ;
wr . writex . in . wmode = 0 ;
wr . writex . in . remaining = 0 ;
wr . writex . in . count = 1 ;
wr . writex . in . data = & c ;
status = smb_raw_write ( cli - > tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
CHECK_VALUE ( wr . writex . out . nwritten , 1 ) ;
printf ( " write with the new pid \n " ) ;
cli - > session - > pid = pid2 ;
status = smb_raw_write ( cli - > tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
CHECK_VALUE ( wr . writex . out . nwritten , 1 ) ;
printf ( " exit the old pid \n " ) ;
cli - > session - > pid = pid1 ;
status = smb_raw_exit ( cli - > session ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
printf ( " the fnum should still be accessible \n " ) ;
cli - > session - > pid = pid1 ;
status = smb_raw_write ( cli - > tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
CHECK_VALUE ( wr . writex . out . nwritten , 1 ) ;
printf ( " exit the new pid \n " ) ;
cli - > session - > pid = pid2 ;
status = smb_raw_exit ( cli - > session ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
printf ( " the fnum should not now be accessible \n " ) ;
cli - > session - > pid = pid1 ;
status = smb_raw_write ( cli - > tree , & wr ) ;
CHECK_STATUS ( status , NT_STATUS_INVALID_HANDLE ) ;
printf ( " the fnum should have been auto-closed \n " ) ;
cl . close . level = RAW_CLOSE_CLOSE ;
cl . close . in . fnum = fnum ;
cl . close . in . write_time = 0 ;
status = smb_raw_close ( cli - > tree , & cl ) ;
CHECK_STATUS ( status , NT_STATUS_INVALID_HANDLE ) ;
done :
return ret ;
}
/*
basic testing of session / tree context calls
*/
2004-10-28 17:40:50 +04:00
BOOL torture_raw_context ( 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
BOOL ret = True ;
TALLOC_CTX * mem_ctx ;
2004-10-25 11:56:48 +04:00
lp_set_cmdline ( " use spnego " , " False " ) ;
2003-08-13 05:53:07 +04:00
if ( ! torture_open_connection ( & cli ) ) {
return False ;
}
mem_ctx = talloc_init ( " torture_raw_context " ) ;
if ( ! test_session ( cli , mem_ctx ) ) {
ret = False ;
}
if ( ! test_tree ( cli , mem_ctx ) ) {
ret = False ;
}
if ( ! test_pid ( cli , mem_ctx ) ) {
ret = False ;
}
smb_raw_exit ( cli - > session ) ;
2004-08-04 17:23:35 +04:00
smbcli_deltree ( cli - > tree , BASEDIR ) ;
2003-08-13 05:53:07 +04:00
torture_close_connection ( cli ) ;
2005-01-27 10:08:20 +03:00
talloc_free ( mem_ctx ) ;
2004-09-26 15:30:20 +04:00
2003-08-13 05:53:07 +04:00
return ret ;
}