/*
Unix SMB / CIFS implementation .
handle SMBsessionsetup
Copyright ( C ) Andrew Tridgell 1998 - 2001
Copyright ( C ) Andrew Bartlett 2001
Copyright ( C ) Jim McDonough 2002
Copyright ( C ) Luke Howard 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"
2005-01-18 10:10:35 +00:00
# include "version.h"
2004-11-02 02:57:18 +00:00
# include "auth/auth.h"
2004-11-02 07:18:24 +00:00
# include "smb_server/smb_server.h"
2005-01-30 00:54:57 +00:00
# include "smbd/service_stream.h"
2004-11-02 07:18:24 +00:00
/*
setup the OS , Lanman and domain portions of a session setup reply
*/
2004-06-28 08:39:00 +00:00
static void sesssetup_common_strings ( struct smbsrv_request * req ,
char * * os , char * * lanman , char * * domain )
{
2004-09-08 05:39:06 +00:00
( * os ) = talloc_asprintf ( req , " Unix " ) ;
( * lanman ) = talloc_asprintf ( req , " Samba %s " , SAMBA_VERSION_STRING ) ;
( * domain ) = talloc_asprintf ( req , " %s " , lp_workgroup ( ) ) ;
}
/*
handler for old style session setup
*/
2004-06-28 08:39:00 +00:00
static NTSTATUS sesssetup_old ( struct smbsrv_request * req , union smb_sesssetup * sess )
{
NTSTATUS status ;
2004-06-05 01:39:08 +00:00
struct auth_usersupplied_info * user_info = NULL ;
struct auth_serversupplied_info * server_info = NULL ;
struct auth_session_info * session_info ;
2004-09-22 23:50:28 +00:00
char * remote_machine ;
2005-01-09 12:55:25 +00:00
TALLOC_CTX * mem_ctx ;
mem_ctx = talloc_named ( req , 0 , " OLD session setup " ) ;
NT_STATUS_HAVE_NO_MEMORY ( mem_ctx ) ;
2004-06-29 07:40:14 +00:00
if ( ! req - > smb_conn - > negotiate . done_sesssetup ) {
req - > smb_conn - > negotiate . max_send = sess - > old . in . bufsize ;
}
2004-09-22 23:50:28 +00:00
remote_machine = socket_get_peer_addr ( req - > smb_conn - > connection - > socket , mem_ctx ) ;
2005-01-09 12:55:25 +00:00
status = make_user_info_for_reply_enc ( req - > smb_conn ,
sess - > old . in . user , sess - > old . in . domain ,
2004-09-22 23:50:28 +00:00
remote_machine ,
sess - > old . in . password ,
2005-01-09 12:55:25 +00:00
data_blob ( NULL , 0 ) ,
& user_info ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2004-10-29 09:15:41 +00:00
talloc_free ( mem_ctx ) ;
return NT_STATUS_ACCESS_DENIED ;
}
2005-01-09 12:55:25 +00:00
status = auth_check_password ( req - > smb_conn - > negotiate . auth_context ,
mem_ctx , user_info , & server_info ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2004-10-29 09:15:41 +00:00
talloc_free ( mem_ctx ) ;
2005-01-09 12:55:25 +00:00
return auth_nt_status_squash ( status ) ;
2004-05-02 08:45:00 +00:00
}
2004-10-25 04:19:02 +00:00
/* This references server_info into session_info */
2005-01-09 12:55:25 +00:00
status = auth_generate_session_info ( req , server_info , & session_info ) ;
2004-10-29 09:15:41 +00:00
talloc_free ( mem_ctx ) ;
2004-06-05 01:39:08 +00:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2005-01-09 12:55:25 +00:00
return auth_nt_status_squash ( status ) ;
}
sess - > old . out . action = 0 ;
2004-07-14 12:44:31 +00:00
sess - > old . out . vuid = smbsrv_register_session ( req - > smb_conn , session_info , NULL ) ;
2004-07-05 07:29:14 +00:00
if ( sess - > old . out . vuid = = UID_FIELD_INVALID ) {
return NT_STATUS_ACCESS_DENIED ;
}
sesssetup_common_strings ( req ,
& sess - > old . out . os ,
& sess - > old . out . lanman ,
& sess - > old . out . domain ) ;
2004-07-15 02:11:03 +00:00
req - > session = smbsrv_session_find ( req - > smb_conn , sess - > old . out . vuid ) ;
return NT_STATUS_OK ;
}
/*
handler for NT1 style session setup
*/
2004-06-28 08:39:00 +00:00
static NTSTATUS sesssetup_nt1 ( struct smbsrv_request * req , union smb_sesssetup * sess )
{
NTSTATUS status ;
2004-06-05 01:39:08 +00:00
struct auth_usersupplied_info * user_info = NULL ;
struct auth_serversupplied_info * server_info = NULL ;
struct auth_session_info * session_info ;
2005-01-09 12:55:25 +00:00
TALLOC_CTX * mem_ctx ;
2004-10-29 09:15:41 +00:00
2005-01-09 12:55:25 +00:00
mem_ctx = talloc_named ( req , 0 , " NT1 session setup " ) ;
NT_STATUS_HAVE_NO_MEMORY ( mem_ctx ) ;
2004-06-29 07:40:14 +00: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 ;
}
2004-08-11 18:09:40 +00:00
if ( req - > smb_conn - > negotiate . spnego_negotiated ) {
struct auth_context * auth_context ;
2004-08-12 06:58:10 +00:00
if ( sess - > nt1 . in . user & & * sess - > nt1 . in . user ) {
return NT_STATUS_ACCESS_DENIED ;
}
2004-08-11 18:09:40 +00:00
2005-01-09 12:55:25 +00:00
status = make_user_info_anonymous ( req - > smb_conn , & user_info ) ;
2004-08-11 18:09:40 +00:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2004-10-29 09:15:41 +00:00
talloc_free ( mem_ctx ) ;
2004-08-11 18:09:40 +00:00
return status ;
}
2005-01-09 12:55:25 +00:00
/* TODO: should we use just "anonymous" here? */
status = auth_context_create ( req - > smb_conn , lp_auth_methods ( ) , & auth_context ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
talloc_free ( mem_ctx ) ;
return status ;
}
status = auth_check_password ( auth_context , mem_ctx ,
user_info , & server_info ) ;
2004-08-11 18:09:40 +00:00
} else {
2004-09-22 23:50:28 +00:00
char * remote_machine ;
2005-01-09 12:55:25 +00:00
2004-09-22 23:50:28 +00:00
remote_machine = socket_get_peer_addr ( req - > smb_conn - > connection - > socket , mem_ctx ) ;
2005-01-09 12:55:25 +00:00
status = make_user_info_for_reply_enc ( req - > smb_conn ,
2004-08-11 18:09:40 +00:00
sess - > nt1 . in . user , sess - > nt1 . in . domain ,
2004-09-22 23:50:28 +00:00
remote_machine ,
2004-08-11 18:09:40 +00:00
sess - > nt1 . in . password1 ,
2005-01-09 12:55:25 +00:00
sess - > nt1 . in . password2 ,
& user_info ) ;
2004-08-11 18:09:40 +00:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2004-10-29 09:15:41 +00:00
talloc_free ( mem_ctx ) ;
2004-08-11 18:09:40 +00:00
return NT_STATUS_ACCESS_DENIED ;
}
2005-01-09 12:55:25 +00:00
status = auth_check_password ( req - > smb_conn - > negotiate . auth_context ,
req , user_info , & server_info ) ;
}
if ( ! NT_STATUS_IS_OK ( status ) ) {
2004-10-29 09:15:41 +00:00
talloc_free ( mem_ctx ) ;
2005-01-09 12:55:25 +00:00
return auth_nt_status_squash ( status ) ;
2004-05-02 08:45:00 +00:00
}
2004-10-25 04:19:02 +00:00
/* This references server_info into session_info */
2005-01-09 12:55:25 +00:00
status = auth_generate_session_info ( mem_ctx , server_info , & session_info ) ;
2004-06-05 01:39:08 +00:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2004-10-29 09:15:41 +00:00
talloc_free ( mem_ctx ) ;
2005-01-09 12:55:25 +00:00
return auth_nt_status_squash ( status ) ;
}
2004-10-03 07:32:08 +00:00
sess - > nt1 . out . action = 0 ;
2004-07-14 12:44:31 +00:00
sess - > nt1 . out . vuid = smbsrv_register_session ( req - > smb_conn , session_info , NULL ) ;
2004-10-29 09:15:41 +00:00
talloc_free ( mem_ctx ) ;
2004-06-05 01:39:08 +00:00
if ( sess - > nt1 . out . vuid = = UID_FIELD_INVALID ) {
return NT_STATUS_ACCESS_DENIED ;
}
sesssetup_common_strings ( req ,
& sess - > nt1 . out . os ,
& sess - > nt1 . out . lanman ,
& sess - > nt1 . out . domain ) ;
2004-06-05 01:39:08 +00:00
2004-07-15 02:11:03 +00:00
req - > session = smbsrv_session_find ( req - > smb_conn , sess - > nt1 . out . vuid ) ;
2005-01-09 12:55:25 +00:00
if ( ! session_info - > server_info - > authenticated ) {
2004-08-13 00:16:57 +00:00
return NT_STATUS_OK ;
2004-08-10 04:56:44 +00:00
}
2005-01-09 12:55:25 +00:00
2004-08-13 00:16:57 +00:00
if ( ! srv_setup_signing ( req - > smb_conn , & session_info - > session_key , & sess - > nt1 . in . password2 ) ) {
/* Already signing, or disabled */
return NT_STATUS_OK ;
}
2005-01-09 12:55:25 +00:00
2004-08-13 00:16:57 +00:00
/* Force check of the request packet, now we know the session key */
req_signing_check_incoming ( req ) ;
srv_signing_restart ( req - > smb_conn , & session_info - > session_key , & sess - > nt1 . in . password2 ) ;
2004-08-10 04:56:44 +00:00
return NT_STATUS_OK ;
}
/*
handler for SPNEGO style session setup
*/
2004-06-28 08:39:00 +00:00
static NTSTATUS sesssetup_spnego ( struct smbsrv_request * req , union smb_sesssetup * sess )
{
2004-08-11 18:09:40 +00:00
NTSTATUS status = NT_STATUS_ACCESS_DENIED ;
struct smbsrv_session * smb_sess ;
struct gensec_security * gensec_ctx = NULL ;
struct auth_session_info * session_info = NULL ;
uint16_t vuid ;
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 ) ;
smb_sess = smbsrv_session_find ( req - > smb_conn , vuid ) ;
2004-09-26 11:24:39 +00:00
if ( smb_sess & & ! smb_sess - > session_info ) {
2004-08-11 18:09:40 +00:00
if ( ! smb_sess - > gensec_ctx ) {
return NT_STATUS_INVALID_HANDLE ;
}
2004-09-08 05:39:06 +00:00
status = gensec_update ( smb_sess - > gensec_ctx , req , sess - > spnego . in . secblob , & sess - > spnego . out . secblob ) ;
2004-08-11 18:09:40 +00:00
} else {
2004-09-26 11:24:39 +00:00
smb_sess = NULL ;
2004-09-25 12:08:57 +00:00
status = gensec_server_start ( req - > smb_conn , & gensec_ctx ) ;
2004-08-11 18:09:40 +00:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 1 , ( " Failed to start GENSEC server code: %s \n " , nt_errstr ( status ) ) ) ;
return status ;
}
2004-12-24 09:54:23 +00:00
gensec_set_target_service ( gensec_ctx , " cifs " ) ;
2004-12-06 15:44:17 +00:00
gensec_want_feature ( gensec_ctx , GENSEC_FEATURE_SESSION_KEY ) ;
2004-08-25 02:25:20 +00:00
2004-12-05 16:29:27 +00:00
status = gensec_start_mech_by_oid ( gensec_ctx , GENSEC_OID_SPNEGO ) ;
2004-08-11 18:09:40 +00:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 1 , ( " Failed to start GENSEC SPNEGO server code: %s \n " , nt_errstr ( status ) ) ) ;
return status ;
}
2004-09-08 05:39:06 +00:00
status = gensec_update ( gensec_ctx , req , sess - > spnego . in . secblob , & sess - > spnego . out . secblob ) ;
2004-08-11 18:09:40 +00:00
}
2004-08-12 03:23:19 +00:00
if ( ! smb_sess ) {
2004-10-25 04:19:02 +00:00
vuid = smbsrv_register_session ( req - > smb_conn ,
session_info , gensec_ctx ) ;
2004-08-12 03:23:19 +00:00
if ( vuid = = UID_FIELD_INVALID ) {
return NT_STATUS_ACCESS_DENIED ;
}
smb_sess = smbsrv_session_find ( req - > smb_conn , vuid ) ;
if ( ! smb_sess ) {
return NT_STATUS_FOOBAR ;
}
}
2004-08-11 18:09:40 +00:00
if ( NT_STATUS_IS_OK ( status ) ) {
DATA_BLOB session_key ;
2004-08-12 03:23:19 +00:00
2004-08-11 18:09:40 +00:00
status = gensec_session_info ( smb_sess - > gensec_ctx , & smb_sess - > session_info ) ;
2004-08-12 03:23:19 +00:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
status = gensec_session_key ( smb_sess - > gensec_ctx ,
2004-08-13 00:16:57 +00:00
& session_key ) ;
if ( NT_STATUS_IS_OK ( status )
2005-01-09 12:55:25 +00:00
& & smb_sess - > session_info - > server_info - > authenticated
2004-08-13 00:16:57 +00:00
& & srv_setup_signing ( req - > smb_conn , & session_key , NULL ) ) {
/* Force check of the request packet, now we know the session key */
req_signing_check_incoming ( req ) ;
srv_signing_restart ( req - > smb_conn , & session_key , NULL ) ;
2004-08-11 18:09:40 +00:00
}
2004-08-13 00:16:57 +00:00
} else {
2005-01-09 12:55:25 +00:00
status = auth_nt_status_squash ( status ) ;
2004-09-24 07:08:02 +00:00
if ( smb_sess - > gensec_ctx & &
! NT_STATUS_EQUAL ( status , NT_STATUS_MORE_PROCESSING_REQUIRED ) ) {
2004-12-24 23:02:39 +00:00
talloc_free ( smb_sess - > gensec_ctx ) ;
smb_sess - > gensec_ctx = NULL ;
2004-09-24 07:08:02 +00:00
}
2004-08-11 18:09:40 +00:00
}
sess - > spnego . out . action = 0 ;
sess - > spnego . out . vuid = vuid ;
sesssetup_common_strings ( req ,
& sess - > spnego . out . os ,
& sess - > spnego . out . lanman ,
& sess - > spnego . out . domain ) ;
return status ;
}
/*
2004-06-27 11:06:10 +00:00
backend for sessionsetup call - this takes all 3 variants of the call
*/
2004-06-28 08:39:00 +00:00
NTSTATUS sesssetup_backend ( struct smbsrv_request * req ,
union smb_sesssetup * sess )
{
2004-07-15 02:11:03 +00:00
NTSTATUS status = NT_STATUS_INVALID_LEVEL ;
2005-01-16 11:15:08 +00:00
switch ( sess - > old . level ) {
case RAW_SESSSETUP_OLD :
2004-07-15 02:11:03 +00:00
status = sesssetup_old ( req , sess ) ;
break ;
case RAW_SESSSETUP_NT1 :
2004-07-15 02:11:03 +00:00
status = sesssetup_nt1 ( req , sess ) ;
break ;
case RAW_SESSSETUP_SPNEGO :
2004-07-15 02:11:03 +00:00
status = sesssetup_spnego ( req , sess ) ;
break ;
}
2004-07-15 02:11:03 +00:00
if ( NT_STATUS_IS_OK ( status ) ) {
req - > smb_conn - > negotiate . done_sesssetup = True ;
}
2004-07-15 02:11:03 +00:00
return status ;
}