2006-01-10 01:12:53 +03:00
2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
handle SMBsessionsetup
2005-04-10 11:39:51 +04:00
Copyright ( C ) Andrew Tridgell 1998 - 2001
Copyright ( C ) Andrew Bartlett < abartlet @ samba . org > 2001 - 2005
Copyright ( C ) Jim McDonough 2002
Copyright ( C ) Luke Howard 2003
2005-11-18 15:57:48 +03:00
Copyright ( C ) Stefan Metzmacher 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"
2005-01-18 13:10:35 +03:00
# include "version.h"
2004-11-02 05:57:18 +03:00
# include "auth/auth.h"
2004-11-02 10:18:24 +03:00
# include "smb_server/smb_server.h"
2005-01-30 03:54:57 +03:00
# include "smbd/service_stream.h"
2006-03-18 18:42:57 +03:00
# include "librpc/gen_ndr/nbt.h"
2003-08-13 05:53:07 +04:00
/*
setup the OS , Lanman and domain portions of a session setup reply
*/
2004-06-28 12:39:00 +04:00
static void sesssetup_common_strings ( struct smbsrv_request * req ,
2003-08-13 05:53:07 +04:00
char * * os , char * * lanman , char * * domain )
{
2004-09-08 09:39:06 +04:00
( * os ) = talloc_asprintf ( req , " Unix " ) ;
( * lanman ) = talloc_asprintf ( req , " Samba %s " , SAMBA_VERSION_STRING ) ;
( * domain ) = talloc_asprintf ( req , " %s " , lp_workgroup ( ) ) ;
2003-08-13 05:53:07 +04:00
}
/*
handler for old style session setup
*/
2004-06-28 12:39:00 +04:00
static NTSTATUS sesssetup_old ( struct smbsrv_request * req , union smb_sesssetup * sess )
2003-08-13 05:53:07 +04:00
{
NTSTATUS status ;
2004-06-05 05:39:08 +04:00
struct auth_usersupplied_info * user_info = NULL ;
struct auth_serversupplied_info * server_info = NULL ;
struct auth_session_info * session_info ;
2005-04-10 11:39:51 +04:00
struct smbsrv_session * smb_sess ;
2006-01-10 01:12:53 +03:00
struct socket_address * remote_address ;
2005-07-22 08:10:07 +04:00
const char * remote_machine = NULL ;
2005-01-09 15:55:25 +03:00
2005-11-18 14:39:31 +03:00
sess - > old . out . vuid = 0 ;
2005-04-10 11:39:51 +04:00
sess - > old . out . action = 0 ;
2004-06-29 11:40:14 +04:00
if ( ! req - > smb_conn - > negotiate . done_sesssetup ) {
req - > smb_conn - > negotiate . max_send = sess - > old . in . bufsize ;
2003-08-13 05:53:07 +04:00
}
2004-09-23 03:50:28 +04:00
2005-10-13 02:25:51 +04:00
if ( req - > smb_conn - > negotiate . calling_name ) {
remote_machine = req - > smb_conn - > negotiate . calling_name - > name ;
2005-07-22 08:10:07 +04:00
}
2006-01-10 01:12:53 +03:00
remote_address = socket_get_peer_addr ( req - > smb_conn - > connection - > socket , req ) ;
NT_STATUS_HAVE_NO_MEMORY ( remote_address ) ;
2005-07-22 08:10:07 +04:00
if ( ! remote_machine ) {
2006-01-10 01:12:53 +03:00
remote_machine = remote_address - > addr ;
2005-07-22 08:10:07 +04:00
}
2005-11-18 14:39:31 +03:00
user_info = talloc ( req , struct auth_usersupplied_info ) ;
NT_STATUS_HAVE_NO_MEMORY ( user_info ) ;
2005-07-22 08:10:07 +04:00
user_info - > mapped_state = False ;
2005-10-28 13:14:16 +04:00
user_info - > logon_parameters = 0 ;
2005-07-22 08:10:07 +04:00
user_info - > flags = 0 ;
user_info - > client . account_name = sess - > old . in . user ;
user_info - > client . domain_name = sess - > old . in . domain ;
user_info - > workstation_name = remote_machine ;
2006-01-10 01:12:53 +03:00
user_info - > remote_host = talloc_steal ( user_info , remote_address ) ;
2005-07-22 08:10:07 +04:00
user_info - > password_state = AUTH_PASSWORD_RESPONSE ;
user_info - > password . response . lanman = sess - > old . in . password ;
user_info - > password . response . lanman . data = talloc_steal ( user_info , sess - > old . in . password . data ) ;
user_info - > password . response . nt = data_blob ( NULL , 0 ) ;
2003-08-13 05:53:07 +04:00
2005-01-09 15:55:25 +03:00
status = auth_check_password ( req - > smb_conn - > negotiate . auth_context ,
2005-11-18 14:39:31 +03:00
req , user_info , & server_info ) ;
2003-08-13 05:53:07 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2005-01-09 15:55:25 +03:00
return auth_nt_status_squash ( status ) ;
2004-05-02 12:45:00 +04:00
}
2004-10-25 08:19:02 +04:00
/* This references server_info into session_info */
2005-01-09 15:55:25 +03:00
status = auth_generate_session_info ( req , server_info , & session_info ) ;
2004-06-05 05:39:08 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2005-01-09 15:55:25 +03:00
return auth_nt_status_squash ( status ) ;
2003-08-13 05:53:07 +04:00
}
2005-11-18 15:57:48 +03:00
/* allocate a new session */
smb_sess = smbsrv_session_new ( req - > smb_conn , NULL ) ;
2005-04-10 11:39:51 +04:00
if ( ! smb_sess ) {
2004-07-05 11:29:14 +04:00
return NT_STATUS_ACCESS_DENIED ;
}
2005-04-10 11:39:51 +04:00
/* Ensure this is marked as a 'real' vuid, not one
* simply valid for the session setup leg */
2005-11-18 15:57:48 +03:00
status = smbsrv_session_sesssetup_finished ( smb_sess , session_info ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return auth_nt_status_squash ( status ) ;
}
2005-04-10 11:39:51 +04:00
/* To correctly process any AndX packet (like a tree connect)
* we need to fill in the session on the request here */
req - > session = smb_sess ;
sess - > old . out . vuid = smb_sess - > vuid ;
2003-08-13 05:53:07 +04:00
sesssetup_common_strings ( req ,
& sess - > old . out . os ,
& sess - > old . out . lanman ,
& sess - > old . out . domain ) ;
return NT_STATUS_OK ;
}
/*
handler for NT1 style session setup
*/
2004-06-28 12:39:00 +04:00
static NTSTATUS sesssetup_nt1 ( struct smbsrv_request * req , union smb_sesssetup * sess )
2003-08-13 05:53:07 +04:00
{
NTSTATUS status ;
2005-07-22 09:04:45 +04:00
struct auth_context * auth_context ;
2004-06-05 05:39:08 +04:00
struct auth_usersupplied_info * user_info = NULL ;
struct auth_serversupplied_info * server_info = NULL ;
struct auth_session_info * session_info ;
2005-11-18 14:39:31 +03:00
struct smbsrv_session * smb_sess ;
2006-01-10 01:12:53 +03:00
struct socket_address * remote_address ;
2005-11-18 14:39:31 +03:00
const char * remote_machine = NULL ;
2004-10-29 13:15:41 +04:00
2005-11-18 14:39:31 +03:00
sess - > nt1 . out . vuid = 0 ;
2005-04-10 11:39:51 +04:00
sess - > nt1 . out . action = 0 ;
2004-06-29 11:40:14 +04:00
if ( ! req - > smb_conn - > negotiate . done_sesssetup ) {
req - > smb_conn - > negotiate . max_send = sess - > nt1 . in . bufsize ;
req - > smb_conn - > negotiate . client_caps = sess - > nt1 . in . capabilities ;
2003-08-13 05:53:07 +04:00
}
2006-02-09 06:04:48 +03:00
if ( req - > smb_conn - > negotiate . oid ) {
2004-08-12 10:58:10 +04:00
if ( sess - > nt1 . in . user & & * sess - > nt1 . in . user ) {
2005-07-22 08:10:07 +04:00
/* We can't accept a normal login, because we
* don ' t have a challenge */
return NT_STATUS_LOGON_FAILURE ;
2004-08-11 22:09:40 +04:00
}
2005-07-22 09:04:45 +04:00
/* TODO: should we use just "anonymous" here? */
2005-11-18 14:39:31 +03:00
status = auth_context_create ( req , lp_auth_methods ( ) ,
2005-07-22 09:04:45 +04:00
& auth_context ,
req - > smb_conn - > connection - > event . ctx ) ;
2005-11-18 14:39:31 +03:00
NT_STATUS_NOT_OK_RETURN ( status ) ;
2005-07-22 09:04:45 +04:00
} else {
auth_context = req - > smb_conn - > negotiate . auth_context ;
2003-08-13 05:53:07 +04:00
}
2005-10-13 02:25:51 +04:00
if ( req - > smb_conn - > negotiate . calling_name ) {
remote_machine = req - > smb_conn - > negotiate . calling_name - > name ;
2005-07-22 08:10:07 +04:00
}
2005-11-18 15:57:48 +03:00
2006-01-10 01:12:53 +03:00
remote_address = socket_get_peer_addr ( req - > smb_conn - > connection - > socket , req ) ;
NT_STATUS_HAVE_NO_MEMORY ( remote_address ) ;
2005-07-22 08:10:07 +04:00
if ( ! remote_machine ) {
2006-01-10 01:12:53 +03:00
remote_machine = remote_address - > addr ;
2005-07-22 08:10:07 +04:00
}
2006-01-10 01:12:53 +03:00
2005-11-18 14:39:31 +03:00
user_info = talloc ( req , struct auth_usersupplied_info ) ;
NT_STATUS_HAVE_NO_MEMORY ( user_info ) ;
2005-07-22 08:10:07 +04:00
user_info - > mapped_state = False ;
2005-10-28 13:14:16 +04:00
user_info - > logon_parameters = 0 ;
2005-07-22 08:10:07 +04:00
user_info - > flags = 0 ;
user_info - > client . account_name = sess - > nt1 . in . user ;
user_info - > client . domain_name = sess - > nt1 . in . domain ;
user_info - > workstation_name = remote_machine ;
2006-01-10 01:12:53 +03:00
user_info - > remote_host = talloc_steal ( user_info , remote_address ) ;
2005-07-22 08:10:07 +04:00
user_info - > password_state = AUTH_PASSWORD_RESPONSE ;
user_info - > password . response . lanman = sess - > nt1 . in . password1 ;
user_info - > password . response . lanman . data = talloc_steal ( user_info , sess - > nt1 . in . password1 . data ) ;
user_info - > password . response . nt = sess - > nt1 . in . password2 ;
user_info - > password . response . nt . data = talloc_steal ( user_info , sess - > nt1 . in . password2 . data ) ;
2005-11-18 14:39:31 +03:00
status = auth_check_password ( auth_context , req , user_info , & server_info ) ;
2003-08-13 05:53:07 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2005-01-09 15:55:25 +03:00
return auth_nt_status_squash ( status ) ;
2004-05-02 12:45:00 +04:00
}
2004-10-25 08:19:02 +04:00
/* This references server_info into session_info */
2005-11-18 14:39:31 +03:00
status = auth_generate_session_info ( req , server_info , & session_info ) ;
2004-06-05 05:39:08 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2005-01-09 15:55:25 +03:00
return auth_nt_status_squash ( status ) ;
2003-08-13 05:53:07 +04:00
}
2004-10-03 11:32:08 +04:00
2005-11-18 15:57:48 +03:00
/* allocate a new session */
smb_sess = smbsrv_session_new ( req - > smb_conn , NULL ) ;
2005-04-10 11:39:51 +04:00
if ( ! smb_sess ) {
2004-06-05 05:39:08 +04:00
return NT_STATUS_ACCESS_DENIED ;
}
2005-04-10 11:39:51 +04:00
/* Ensure this is marked as a 'real' vuid, not one
* simply valid for the session setup leg */
2005-11-18 15:57:48 +03:00
status = smbsrv_session_sesssetup_finished ( smb_sess , session_info ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return auth_nt_status_squash ( status ) ;
}
2005-04-10 11:39:51 +04:00
/* To correctly process any AndX packet (like a tree connect)
* we need to fill in the session on the request here */
req - > session = smb_sess ;
sess - > nt1 . out . vuid = smb_sess - > vuid ;
2003-08-13 05:53:07 +04:00
sesssetup_common_strings ( req ,
& sess - > nt1 . out . os ,
& sess - > nt1 . out . lanman ,
& sess - > nt1 . out . domain ) ;
2005-11-18 14:39:31 +03:00
2005-01-09 15:55:25 +03:00
if ( ! session_info - > server_info - > authenticated ) {
2004-08-13 04:16:57 +04:00
return NT_STATUS_OK ;
2004-08-10 08:56:44 +04:00
}
2005-01-09 15:55:25 +03:00
2006-03-06 19:19:27 +03:00
if ( ! smbsrv_setup_signing ( req - > smb_conn , & session_info - > session_key , & sess - > nt1 . in . password2 ) ) {
2004-08-13 04:16:57 +04:00
/* Already signing, or disabled */
return NT_STATUS_OK ;
}
2005-01-09 15:55:25 +03:00
2004-08-13 04:16:57 +04:00
/* Force check of the request packet, now we know the session key */
2006-03-06 19:19:27 +03:00
smbsrv_signing_check_incoming ( req ) ;
2005-11-18 14:39:31 +03:00
/* TODO: why don't we check the result here? */
2004-08-13 04:16:57 +04:00
2005-04-10 11:39:51 +04:00
/* Unfortunetly win2k3 as a client doesn't sign the request
* packet here , so we have to force signing to start again */
2006-03-06 19:19:27 +03:00
smbsrv_signing_restart ( req - > smb_conn , & session_info - > session_key , & sess - > nt1 . in . password2 ) ;
2004-08-10 08:56:44 +04:00
2003-08-13 05:53:07 +04:00
return NT_STATUS_OK ;
}
/*
handler for SPNEGO style session setup
*/
2004-06-28 12:39:00 +04:00
static NTSTATUS sesssetup_spnego ( struct smbsrv_request * req , union smb_sesssetup * sess )
2003-08-13 05:53:07 +04:00
{
2005-12-07 11:11:50 +03:00
NTSTATUS status ;
NTSTATUS skey_status ;
struct smbsrv_session * smb_sess = NULL ;
2004-08-11 22:09:40 +04:00
struct auth_session_info * session_info = NULL ;
2005-12-07 11:11:50 +03:00
DATA_BLOB session_key ;
2004-08-11 22:09:40 +04:00
uint16_t vuid ;
2005-11-18 14:39:31 +03:00
sess - > spnego . out . vuid = 0 ;
2005-04-10 11:39:51 +04:00
sess - > spnego . out . action = 0 ;
sesssetup_common_strings ( req ,
& sess - > spnego . out . os ,
& sess - > spnego . out . lanman ,
& sess - > spnego . out . workgroup ) ;
2004-08-11 22:09:40 +04:00
if ( ! req - > smb_conn - > negotiate . done_sesssetup ) {
req - > smb_conn - > negotiate . max_send = sess - > nt1 . in . bufsize ;
req - > smb_conn - > negotiate . client_caps = sess - > nt1 . in . capabilities ;
}
vuid = SVAL ( req - > in . hdr , HDR_UID ) ;
2005-12-07 11:11:50 +03:00
/* lookup an existing session */
2005-04-10 11:39:51 +04:00
smb_sess = smbsrv_session_find_sesssetup ( req - > smb_conn , vuid ) ;
2005-12-07 11:11:50 +03:00
if ( ! smb_sess ) {
struct gensec_security * gensec_ctx ;
2005-07-20 08:19:45 +04:00
status = gensec_server_start ( req , & gensec_ctx ,
2005-06-16 15:36:09 +04:00
req - > smb_conn - > connection - > event . ctx ) ;
2004-08-11 22:09:40 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 1 , ( " Failed to start GENSEC server code: %s \n " , nt_errstr ( status ) ) ) ;
return status ;
}
2005-10-20 07:47:55 +04:00
gensec_set_credentials ( gensec_ctx , req - > smb_conn - > negotiate . server_credentials ) ;
2004-12-24 12:54:23 +03:00
gensec_set_target_service ( gensec_ctx , " cifs " ) ;
2004-12-06 18:44:17 +03:00
gensec_want_feature ( gensec_ctx , GENSEC_FEATURE_SESSION_KEY ) ;
2004-08-25 06:25:20 +04:00
2006-02-09 06:04:48 +03:00
status = gensec_start_mech_by_oid ( gensec_ctx , req - > smb_conn - > negotiate . oid ) ;
2004-08-11 22:09:40 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2006-02-09 06:04:48 +03:00
DEBUG ( 1 , ( " Failed to start GENSEC %s server code: %s \n " ,
gensec_get_name_by_oid ( req - > smb_conn - > negotiate . oid ) , nt_errstr ( status ) ) ) ;
2004-08-11 22:09:40 +04:00
return status ;
}
2005-12-07 11:11:50 +03:00
/* allocate a new session */
2005-11-18 15:57:48 +03:00
smb_sess = smbsrv_session_new ( req - > smb_conn , gensec_ctx ) ;
2004-08-12 07:23:19 +04:00
}
2005-12-07 11:11:50 +03:00
if ( ! smb_sess ) {
return NT_STATUS_ACCESS_DENIED ;
}
2005-04-10 11:39:51 +04:00
2005-12-07 11:11:50 +03:00
if ( ! smb_sess - > gensec_ctx ) {
status = NT_STATUS_INTERNAL_ERROR ;
DEBUG ( 1 , ( " Internal ERROR: no gensec_ctx on session: %s \n " , nt_errstr ( status ) ) ) ;
goto failed ;
}
status = gensec_update ( smb_sess - > gensec_ctx , req , sess - > spnego . in . secblob , & sess - > spnego . out . secblob ) ;
if ( NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) ) {
sess - > spnego . out . vuid = smb_sess - > vuid ;
2005-04-10 11:39:51 +04:00
return status ;
2005-12-07 11:11:50 +03:00
} else if ( ! NT_STATUS_IS_OK ( status ) ) {
goto failed ;
2005-04-10 11:39:51 +04:00
}
2005-12-07 11:11:50 +03:00
status = gensec_session_info ( smb_sess - > gensec_ctx , & session_info ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto failed ;
}
skey_status = gensec_session_key ( smb_sess - > gensec_ctx , & session_key ) ;
if ( NT_STATUS_IS_OK ( skey_status ) & &
session_info - > server_info - > authenticated & &
2006-03-06 19:19:27 +03:00
smbsrv_setup_signing ( req - > smb_conn , & session_key , NULL ) ) {
2005-12-07 11:11:50 +03:00
/* Force check of the request packet, now we know the session key */
2006-03-06 19:19:27 +03:00
smbsrv_signing_check_incoming ( req ) ;
2005-12-07 11:11:50 +03:00
2006-03-06 19:19:27 +03:00
smbsrv_signing_restart ( req - > smb_conn , & session_key , NULL ) ;
2005-12-07 11:11:50 +03:00
}
/* Ensure this is marked as a 'real' vuid, not one
* simply valid for the session setup leg */
status = smbsrv_session_sesssetup_finished ( smb_sess , session_info ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
goto failed ;
2005-04-10 11:39:51 +04:00
}
2005-12-07 11:11:50 +03:00
req - > session = smb_sess ;
2005-04-10 11:39:51 +04:00
sess - > spnego . out . vuid = smb_sess - > vuid ;
2005-12-07 11:11:50 +03:00
return NT_STATUS_OK ;
2004-08-11 22:09:40 +04:00
2005-12-07 11:11:50 +03:00
failed :
talloc_free ( smb_sess ) ;
return auth_nt_status_squash ( status ) ;
2003-08-13 05:53:07 +04:00
}
/*
2004-06-27 15:06:10 +04:00
backend for sessionsetup call - this takes all 3 variants of the call
2003-08-13 05:53:07 +04:00
*/
2006-03-06 18:33:25 +03:00
NTSTATUS smbsrv_sesssetup_backend ( struct smbsrv_request * req ,
union smb_sesssetup * sess )
2003-08-13 05:53:07 +04:00
{
2004-07-15 06:11:03 +04:00
NTSTATUS status = NT_STATUS_INVALID_LEVEL ;
2005-01-16 14:15:08 +03:00
switch ( sess - > old . level ) {
2003-08-13 05:53:07 +04:00
case RAW_SESSSETUP_OLD :
2004-07-15 06:11:03 +04:00
status = sesssetup_old ( req , sess ) ;
break ;
2003-08-13 05:53:07 +04:00
case RAW_SESSSETUP_NT1 :
2004-07-15 06:11:03 +04:00
status = sesssetup_nt1 ( req , sess ) ;
break ;
2003-08-13 05:53:07 +04:00
case RAW_SESSSETUP_SPNEGO :
2004-07-15 06:11:03 +04:00
status = sesssetup_spnego ( req , sess ) ;
break ;
2003-08-13 05:53:07 +04:00
}
2004-07-15 06:11:03 +04:00
if ( NT_STATUS_IS_OK ( status ) ) {
req - > smb_conn - > negotiate . done_sesssetup = True ;
}
2003-08-13 05:53:07 +04:00
2004-07-15 06:11:03 +04:00
return status ;
2003-08-13 05:53:07 +04:00
}