2007-08-21 06:04:24 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
Password and authentication handling
1998-01-22 16:27:43 +03:00
Copyright ( C ) Andrew Tridgell 1992 - 1998
2007-08-21 06:04:24 +04:00
Copyright ( C ) Jeremy Allison 2007.
1996-05-04 11:50:46 +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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1996-05-04 11:50:46 +04:00
( at your option ) any later version .
2007-08-21 06:04:24 +04:00
1996-05-04 11:50:46 +04:00
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 .
2007-08-21 06:04:24 +04:00
1996-05-04 11:50:46 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1996-05-04 11:50:46 +04:00
*/
# include "includes.h"
2011-02-25 19:14:22 +03:00
# include "system/passwd.h"
2011-03-22 18:57:01 +03:00
# include "smbd/smbd.h"
2009-01-08 14:03:45 +03:00
# include "smbd/globals.h"
2010-08-05 17:14:04 +04:00
# include "../librpc/gen_ndr/netlogon.h"
2011-03-24 15:46:20 +03:00
# include "auth.h"
2011-07-19 05:57:05 +04:00
# include "../libcli/security/security.h"
1999-12-13 16:27:58 +03:00
2010-06-15 08:52:42 +04:00
/* Fix up prototypes for OSX 10.4, where they're missing */
# ifndef HAVE_SETNETGRENT_PROTOTYPE
extern int setnetgrent ( const char * netgroup ) ;
# endif
# ifndef HAVE_GETNETGRENT_PROTOTYPE
extern int getnetgrent ( char * * host , char * * user , char * * domain ) ;
# endif
# ifndef HAVE_ENDNETGRENT_PROTOTYPE
extern void endnetgrent ( void ) ;
# endif
2007-08-21 05:43:22 +04:00
enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES ,
SERVER_ALLOCATED_REQUIRED_NO ,
SERVER_ALLOCATED_REQUIRED_ANY } ;
2003-07-17 22:55:40 +04:00
2009-05-26 18:38:45 +04:00
static user_struct * get_valid_user_struct_internal (
struct smbd_server_connection * sconn ,
uint16 vuid ,
2007-08-21 05:43:22 +04:00
enum server_allocated_state server_allocated )
1999-12-13 16:27:58 +03:00
{
2000-11-29 01:17:44 +03:00
user_struct * usp ;
int count = 0 ;
if ( vuid = = UID_FIELD_INVALID )
return NULL ;
2012-03-03 08:41:43 +04:00
usp = sconn - > users ;
2009-05-26 18:38:45 +04:00
for ( ; usp ; usp = usp - > next , count + + ) {
2007-08-21 05:43:22 +04:00
if ( vuid = = usp - > vuid ) {
switch ( server_allocated ) {
case SERVER_ALLOCATED_REQUIRED_YES :
2011-02-21 12:25:52 +03:00
if ( usp - > session_info = = NULL ) {
2007-08-21 05:43:22 +04:00
continue ;
}
break ;
case SERVER_ALLOCATED_REQUIRED_NO :
2011-02-21 12:25:52 +03:00
if ( usp - > session_info ! = NULL ) {
2007-08-21 05:43:22 +04:00
continue ;
}
case SERVER_ALLOCATED_REQUIRED_ANY :
break ;
}
2005-07-14 18:39:27 +04:00
if ( count > 10 ) {
2012-03-03 08:41:43 +04:00
DLIST_PROMOTE ( sconn - > users , usp ) ;
2005-07-14 18:39:27 +04:00
}
return usp ;
}
}
return NULL ;
}
/****************************************************************************
2007-08-21 05:43:22 +04:00
Check if a uid has been validated , and return an pointer to the user_struct
if it has . NULL if not . vuid is biased by an offset . This allows us to
tell random client vuid ' s ( normally zero ) from valid vuids .
2005-07-14 18:39:27 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-05-26 18:38:45 +04:00
user_struct * get_valid_user_struct ( struct smbd_server_connection * sconn ,
uint16 vuid )
2005-07-14 18:39:27 +04:00
{
2009-05-26 18:38:45 +04:00
return get_valid_user_struct_internal ( sconn , vuid ,
2007-08-21 06:04:24 +04:00
SERVER_ALLOCATED_REQUIRED_YES ) ;
2007-08-21 05:43:22 +04:00
}
2005-07-14 18:39:27 +04:00
2009-05-26 18:38:45 +04:00
bool is_partial_auth_vuid ( struct smbd_server_connection * sconn , uint16 vuid )
2007-08-21 05:43:22 +04:00
{
2009-05-26 18:38:45 +04:00
return ( get_partial_auth_user_struct ( sconn , vuid ) ! = NULL ) ;
2007-08-21 05:43:22 +04:00
}
2000-11-29 01:17:44 +03:00
2007-08-21 05:43:22 +04:00
/****************************************************************************
Get the user struct of a partial NTLMSSP login
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-05-26 18:38:45 +04:00
user_struct * get_partial_auth_user_struct ( struct smbd_server_connection * sconn ,
uint16 vuid )
2007-08-21 05:43:22 +04:00
{
2009-05-26 18:38:45 +04:00
return get_valid_user_struct_internal ( sconn , vuid ,
2007-08-21 05:43:22 +04:00
SERVER_ALLOCATED_REQUIRED_NO ) ;
1999-12-13 16:27:58 +03:00
}
/****************************************************************************
2003-07-17 22:55:40 +04:00
Invalidate a uid .
1999-12-13 16:27:58 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-07-17 22:55:40 +04:00
2009-05-26 18:38:45 +04:00
void invalidate_vuid ( struct smbd_server_connection * sconn , uint16 vuid )
1999-12-13 16:27:58 +03:00
{
2007-08-21 05:43:22 +04:00
user_struct * vuser = NULL ;
2009-05-26 18:38:45 +04:00
vuser = get_valid_user_struct_internal ( sconn , vuid ,
2007-08-21 05:43:22 +04:00
SERVER_ALLOCATED_REQUIRED_ANY ) ;
if ( vuser = = NULL ) {
2000-06-09 22:45:31 +04:00
return ;
2007-08-21 05:43:22 +04:00
}
2001-11-09 01:19:01 +03:00
session_yield ( vuser ) ;
2003-02-24 05:35:54 +03:00
2011-12-26 07:23:15 +04:00
if ( vuser - > gensec_security ) {
TALLOC_FREE ( vuser - > gensec_security ) ;
2008-02-15 05:06:16 +03:00
}
2012-03-03 08:41:43 +04:00
DLIST_REMOVE ( sconn - > users , vuser ) ;
SMB_ASSERT ( sconn - > num_users > 0 ) ;
sconn - > num_users - - ;
2000-06-09 07:30:54 +04:00
2002-09-25 19:19:00 +04:00
/* clear the vuid from the 'cache' on each connection, and
from the vuid ' owner ' of connections */
2009-05-27 13:15:44 +04:00
conn_clear_vuid_caches ( sconn , vuid ) ;
2002-09-25 19:19:00 +04:00
2007-04-02 07:46:13 +04:00
TALLOC_FREE ( vuser ) ;
1999-12-13 16:27:58 +03:00
}
2001-04-18 20:41:04 +04:00
/****************************************************************************
2003-07-17 22:55:40 +04:00
Invalidate all vuid entries for this process .
2001-04-18 20:41:04 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-07-17 22:55:40 +04:00
2009-05-26 18:38:45 +04:00
void invalidate_all_vuids ( struct smbd_server_connection * sconn )
2001-04-18 20:41:04 +04:00
{
2010-06-10 06:12:02 +04:00
if ( sconn - > using_smb2 ) {
2009-08-11 20:08:26 +04:00
return ;
}
2012-03-03 08:41:43 +04:00
while ( sconn - > users ! = NULL ) {
invalidate_vuid ( sconn , sconn - > users - > vuid ) ;
2001-04-18 20:41:04 +04:00
}
}
2008-12-30 00:06:08 +03:00
static void increment_next_vuid ( uint16_t * vuid )
{
* vuid + = 1 ;
/* Check for vuid wrap. */
if ( * vuid = = UID_FIELD_INVALID ) {
* vuid = VUID_OFFSET ;
}
}
2007-08-21 05:43:22 +04:00
/****************************************************
Create a new partial auth user struct .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-08-02 06:11:55 +04:00
2009-05-26 18:38:45 +04:00
int register_initial_vuid ( struct smbd_server_connection * sconn )
1999-12-13 16:27:58 +03:00
{
2007-04-02 07:46:13 +04:00
user_struct * vuser ;
2000-11-29 01:17:44 +03:00
/* Limit allowed vuids to 16bits - VUID_OFFSET. */
2012-03-03 08:41:43 +04:00
if ( sconn - > num_users > = 0xFFFF - VUID_OFFSET ) {
2000-11-29 01:17:44 +03:00
return UID_FIELD_INVALID ;
Changes all over the shop, but all towards:
- NTLM2 support in the server
- KEY_EXCH support in the server
- variable length session keys.
In detail:
- NTLM2 is an extension of NTLMv1, that is compatible with existing
domain controllers (unlike NTLMv2, which requires a DC upgrade).
* This is known as 'NTLMv2 session security' *
(This is not yet implemented on the RPC pipes however, so there may
well still be issues for PDC setups, particuarly around password
changes. We do not fully understand the sign/seal implications of
NTLM2 on RPC pipes.)
This requires modifications to our authentication subsystem, as we
must handle the 'challege' input into the challenge-response algorithm
being changed. This also needs to be turned off for
'security=server', which does not support this.
- KEY_EXCH is another 'security' mechanism, whereby the session key
actually used by the server is sent by the client, rather than being
the shared-secret directly or indirectly.
- As both these methods change the session key, the auth subsystem
needed to be changed, to 'override' session keys provided by the
backend.
- There has also been a major overhaul of the NTLMSSP subsystem, to merge the 'client' and 'server' functions, so they both operate on a single structure. This should help the SPNEGO implementation.
- The 'names blob' in NTLMSSP is always in unicode - never in ascii.
Don't make an ascii version ever.
- The other big change is to allow variable length session keys. We
have always assumed that session keys are 16 bytes long - and padded
to this length if shorter. However, Kerberos session keys are 8 bytes
long, when the krb5 login uses DES.
* This fix allows SMB signging on machines not yet running MIT KRB5 1.3.1. *
- Add better DEBUG() messages to ntlm_auth, warning administrators of
misconfigurations that prevent access to the privileged pipe. This
should help reduce some of the 'it just doesn't work' issues.
- Fix data_blob_talloc() to behave the same way data_blob() does when
passed a NULL data pointer. (just allocate)
REMEMBER to make clean after this commit - I have changed plenty of data structures...
(This used to be commit f3bbc87b0dac63426cda6fac7a295d3aad810ecc)
2003-11-22 16:19:38 +03:00
}
2000-11-29 01:17:44 +03:00
2007-08-21 05:43:22 +04:00
if ( ( vuser = talloc_zero ( NULL , user_struct ) ) = = NULL ) {
DEBUG ( 0 , ( " register_initial_vuid: "
" Failed to talloc users struct! \n " ) ) ;
2000-11-29 01:17:44 +03:00
return UID_FIELD_INVALID ;
}
2007-08-21 05:43:22 +04:00
/* Allocate a free vuid. Yes this is a linear search... */
2009-05-26 18:38:45 +04:00
while ( get_valid_user_struct_internal ( sconn ,
sconn - > smb1 . sessions . next_vuid ,
2007-08-21 05:43:22 +04:00
SERVER_ALLOCATED_REQUIRED_ANY ) ! = NULL ) {
2009-05-26 18:38:45 +04:00
increment_next_vuid ( & sconn - > smb1 . sessions . next_vuid ) ;
2000-11-29 01:17:44 +03:00
}
2007-08-21 05:43:22 +04:00
DEBUG ( 10 , ( " register_initial_vuid: allocated vuid = %u \n " ,
2009-05-26 18:38:45 +04:00
( unsigned int ) sconn - > smb1 . sessions . next_vuid ) ) ;
2000-11-29 01:17:44 +03:00
2009-05-26 18:38:45 +04:00
vuser - > vuid = sconn - > smb1 . sessions . next_vuid ;
2002-09-25 19:19:00 +04:00
2007-08-21 05:43:22 +04:00
/*
* This happens in an unfinished NTLMSSP session setup . We
* need to allocate a vuid between the first and second calls
* to NTLMSSP .
*/
2009-05-26 18:38:45 +04:00
increment_next_vuid ( & sconn - > smb1 . sessions . next_vuid ) ;
2007-08-21 05:43:22 +04:00
2012-03-03 08:41:43 +04:00
sconn - > num_users + + ;
DLIST_ADD ( sconn - > users , vuser ) ;
2007-08-21 05:43:22 +04:00
return vuser - > vuid ;
}
2010-05-18 05:22:19 +04:00
int register_homes_share ( const char * username )
2008-04-29 15:23:47 +04:00
{
int result ;
struct passwd * pwd ;
result = lp_servicenumber ( username ) ;
if ( result ! = - 1 ) {
DEBUG ( 3 , ( " Using static (or previously created) service for "
" user '%s'; path = '%s' \n " , username ,
lp_pathname ( result ) ) ) ;
return result ;
}
2010-10-20 19:16:23 +04:00
pwd = Get_Pwnam_alloc ( talloc_tos ( ) , username ) ;
2008-04-29 15:23:47 +04:00
if ( ( pwd = = NULL ) | | ( pwd - > pw_dir [ 0 ] = = ' \0 ' ) ) {
DEBUG ( 3 , ( " No home directory defined for user '%s' \n " ,
username ) ) ;
TALLOC_FREE ( pwd ) ;
return - 1 ;
}
DEBUG ( 3 , ( " Adding homes service for user '%s' using home directory: "
" '%s' \n " , username , pwd - > pw_dir ) ) ;
result = add_home_service ( username , username , pwd - > pw_dir ) ;
TALLOC_FREE ( pwd ) ;
return result ;
}
2007-08-21 05:43:22 +04:00
/**
* register that a valid login has been performed , establish ' session ' .
2011-02-21 12:25:52 +03:00
* @ param session_info The token returned from the authentication process .
2007-08-21 05:43:22 +04:00
* ( now ' owned ' by register_existing_vuid )
*
* @ param session_key The User session key for the login session ( now also
* ' owned ' by register_existing_vuid )
*
* @ param respose_blob The NT challenge - response , if available . ( May be
* freed after this call )
*
* @ param smb_name The untranslated name of the user
*
* @ return Newly allocated vuid , biased by an offset . ( This allows us to
* tell random client vuid ' s ( normally zero ) from valid vuids . )
*
*/
2009-05-26 18:38:45 +04:00
int register_existing_vuid ( struct smbd_server_connection * sconn ,
uint16 vuid ,
2011-07-18 07:06:47 +04:00
struct auth_session_info * session_info ,
2011-07-26 07:37:36 +04:00
DATA_BLOB response_blob )
2007-08-21 05:43:22 +04:00
{
2008-04-30 19:42:39 +04:00
user_struct * vuser ;
2011-07-19 05:57:05 +04:00
bool guest = security_session_user_level ( session_info , NULL ) < SECURITY_USER ;
2008-04-30 19:42:39 +04:00
2009-05-26 18:38:45 +04:00
vuser = get_partial_auth_user_struct ( sconn , vuid ) ;
2007-08-21 05:43:22 +04:00
if ( ! vuser ) {
goto fail ;
2005-07-14 18:39:27 +04:00
}
2007-08-21 05:43:22 +04:00
/* Use this to keep tabs on all our info from the authentication */
2011-02-21 12:25:52 +03:00
vuser - > session_info = talloc_move ( vuser , & session_info ) ;
2003-02-24 05:35:54 +03:00
2011-07-15 09:55:31 +04:00
/* Make clear that we require the optional unix_token and unix_info in the source3 code */
2011-07-15 08:59:14 +04:00
SMB_ASSERT ( vuser - > session_info - > unix_token ) ;
2011-07-15 09:55:31 +04:00
SMB_ASSERT ( vuser - > session_info - > unix_info ) ;
2011-07-15 08:59:14 +04:00
2007-08-21 05:43:22 +04:00
DEBUG ( 10 , ( " register_existing_vuid: (%u,%u) %s %s %s guest=%d \n " ,
2011-07-15 08:59:14 +04:00
( unsigned int ) vuser - > session_info - > unix_token - > uid ,
( unsigned int ) vuser - > session_info - > unix_token - > gid ,
2011-07-15 09:55:31 +04:00
vuser - > session_info - > unix_info - > unix_name ,
vuser - > session_info - > unix_info - > sanitized_username ,
2011-07-18 06:58:25 +04:00
vuser - > session_info - > info - > domain_name ,
2011-07-19 05:57:05 +04:00
guest ) ) ;
2001-10-31 13:46:25 +03:00
2007-08-21 05:43:22 +04:00
DEBUG ( 3 , ( " register_existing_vuid: User name: %s \t "
2011-07-15 09:55:31 +04:00
" Real name: %s \n " , vuser - > session_info - > unix_info - > unix_name ,
2011-07-18 06:58:25 +04:00
vuser - > session_info - > info - > full_name ) ) ;
2000-11-29 01:17:44 +03:00
2011-02-21 12:25:52 +03:00
if ( ! vuser - > session_info - > security_token ) {
DEBUG ( 1 , ( " register_existing_vuid: session_info does not "
2007-08-21 05:43:22 +04:00
" contain a user_token - cannot continue \n " ) ) ;
goto fail ;
2002-09-25 19:19:00 +04:00
}
2000-11-29 01:17:44 +03:00
2011-07-15 08:59:14 +04:00
/* Make clear that we require the optional unix_token in the source3 code */
SMB_ASSERT ( vuser - > session_info - > unix_token ) ;
2007-08-21 05:43:22 +04:00
DEBUG ( 3 , ( " register_existing_vuid: UNIX uid %d is UNIX user %s, "
2011-07-15 08:59:14 +04:00
" and will be vuid %u \n " , ( int ) vuser - > session_info - > unix_token - > uid ,
2011-07-15 09:55:31 +04:00
vuser - > session_info - > unix_info - > unix_name , vuser - > vuid ) ) ;
2001-08-17 11:47:10 +04:00
2010-08-16 10:00:48 +04:00
if ( ! session_claim ( sconn , vuser ) ) {
2007-08-21 05:43:22 +04:00
DEBUG ( 1 , ( " register_existing_vuid: Failed to claim session "
" for vuid=%d \n " ,
vuser - > vuid ) ) ;
goto fail ;
2000-11-29 01:17:44 +03:00
}
1999-12-13 16:27:58 +03:00
2007-08-21 05:43:22 +04:00
/* Register a home dir service for this user if
( a ) This is not a guest connection ,
( b ) we have a home directory defined
( c ) there s not an existing static share by that name
If a share exists by this name ( autoloaded or not ) reuse it . */
2004-08-27 00:47:58 +04:00
vuser - > homes_snum = - 1 ;
2008-04-29 15:23:47 +04:00
2011-07-19 05:57:05 +04:00
if ( ! guest ) {
2008-04-29 15:23:47 +04:00
vuser - > homes_snum = register_homes_share (
2011-07-15 09:55:31 +04:00
vuser - > session_info - > unix_info - > unix_name ) ;
2007-08-21 05:43:22 +04:00
}
2010-06-12 14:02:47 +04:00
if ( srv_is_signing_negotiated ( sconn ) & &
2011-07-19 05:57:05 +04:00
! guest ) {
2006-02-04 01:19:41 +03:00
/* Try and turn on server signing on the first non-guest
* sessionsetup . */
2010-06-12 14:02:47 +04:00
srv_set_signing ( sconn ,
2011-02-14 03:35:21 +03:00
vuser - > session_info - > session_key ,
2009-03-09 11:47:59 +03:00
response_blob ) ;
2003-07-17 22:55:40 +04:00
}
2007-08-21 05:43:22 +04:00
2004-03-20 01:06:54 +03:00
/* fill in the current_user_info struct */
2008-04-30 19:42:39 +04:00
set_current_user_info (
2011-07-15 09:55:31 +04:00
vuser - > session_info - > unix_info - > sanitized_username ,
vuser - > session_info - > unix_info - > unix_name ,
2011-07-18 06:58:25 +04:00
vuser - > session_info - > info - > domain_name ) ;
2008-04-30 19:42:39 +04:00
2007-08-21 05:43:22 +04:00
return vuser - > vuid ;
2004-03-20 01:06:54 +03:00
2007-08-21 05:43:22 +04:00
fail :
2003-07-17 22:55:40 +04:00
2007-08-21 05:43:22 +04:00
if ( vuser ) {
2009-05-26 18:38:45 +04:00
invalidate_vuid ( sconn , vuid ) ;
2007-08-21 05:43:22 +04:00
}
return UID_FIELD_INVALID ;
1999-12-13 16:27:58 +03:00
}