2011-02-03 18:30:29 +03:00
/*
* Samba Unix / Linux SMB client library
*
* Copyright ( C ) Gregor Beck 2011
*
* 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 3 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 , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
2011-08-19 19:28:37 +04:00
# include "smbd/globals.h"
2011-03-22 18:57:01 +03:00
# include "smbd/smbd.h"
2011-02-03 18:30:29 +03:00
# include "../libcli/security/dom_sid.h"
2011-09-05 15:11:59 +04:00
# include "../libcli/security/security_token.h"
2011-02-03 18:30:29 +03:00
# include "idmap_cache.h"
2011-03-22 18:50:02 +03:00
# include "passdb/lookup_sid.h"
2011-03-24 15:46:20 +03:00
# include "auth.h"
2011-03-24 17:31:06 +03:00
# include "messages.h"
2011-08-19 19:28:37 +04:00
# include "lib/id_cache.h"
2011-02-03 18:30:29 +03:00
2011-09-05 15:02:54 +04:00
static bool uid_in_use ( const struct user_struct * user , uid_t uid )
2011-02-03 18:30:29 +03:00
{
while ( user ) {
2011-09-05 15:02:54 +04:00
if ( user - > session_info & &
( user - > session_info - > unix_token - > uid = = uid ) ) {
2011-02-03 18:30:29 +03:00
return true ;
}
user = user - > next ;
}
return false ;
}
2011-09-05 15:02:54 +04:00
static bool gid_in_use ( const struct user_struct * user , gid_t gid )
2011-02-03 18:30:29 +03:00
{
while ( user ) {
if ( user - > session_info ! = NULL ) {
int i ;
2011-09-05 15:02:54 +04:00
struct security_unix_token * utok ;
utok = user - > session_info - > unix_token ;
2011-07-15 08:59:14 +04:00
if ( utok - > gid = = gid ) {
2011-02-03 18:30:29 +03:00
return true ;
}
2011-07-15 08:59:14 +04:00
for ( i = 0 ; i < utok - > ngroups ; i + + ) {
if ( utok - > groups [ i ] = = gid ) {
2011-02-03 18:30:29 +03:00
return true ;
}
}
}
user = user - > next ;
}
return false ;
}
2011-09-05 15:02:54 +04:00
static bool sid_in_use ( const struct user_struct * user ,
const struct dom_sid * psid )
2011-02-03 18:30:29 +03:00
{
2011-09-05 15:11:59 +04:00
while ( user ) {
struct security_token * tok ;
if ( user - > session_info = = NULL ) {
continue ;
}
tok = user - > session_info - > security_token ;
if ( tok = = NULL ) {
/*
* Not sure session_info - > security_token can
* ever be NULL . This check might be not
* necessary .
*/
continue ;
}
if ( security_token_has_sid ( tok , psid ) ) {
return true ;
}
user = user - > next ;
2011-02-03 18:30:29 +03:00
}
return false ;
}
2011-09-05 15:02:54 +04:00
static bool id_in_use ( const struct user_struct * user ,
const struct id_cache_ref * id )
2011-02-03 18:30:29 +03:00
{
switch ( id - > type ) {
case UID :
2011-02-28 17:52:25 +03:00
return uid_in_use ( user , id - > id . uid ) ;
2011-02-03 18:30:29 +03:00
case GID :
2011-02-28 17:52:25 +03:00
return gid_in_use ( user , id - > id . gid ) ;
2011-02-03 18:30:29 +03:00
case SID :
2011-02-28 17:52:25 +03:00
return sid_in_use ( user , & id - > id . sid ) ;
2011-02-03 18:30:29 +03:00
default :
break ;
}
return false ;
}
2011-08-19 20:10:29 +04:00
static void id_cache_kill ( struct messaging_context * msg_ctx ,
2011-09-05 15:02:54 +04:00
void * private_data ,
uint32_t msg_type ,
struct server_id server_id ,
DATA_BLOB * data )
2011-02-03 18:30:29 +03:00
{
2011-09-05 15:02:54 +04:00
const char * msg = ( data & & data - > data )
? ( const char * ) data - > data : " <NULL> " ;
struct user_struct * validated_users =
smbd_server_conn - > smb1 . sessions . validated_users ;
2011-08-19 19:28:37 +04:00
struct id_cache_ref id ;
2011-02-03 18:30:29 +03:00
2011-08-19 19:28:37 +04:00
if ( ! id_cache_ref_parse ( msg , & id ) ) {
2011-02-03 18:30:29 +03:00
DEBUG ( 0 , ( " Invalid ?ID: %s \n " , msg ) ) ;
return ;
}
2011-08-19 19:28:37 +04:00
if ( id_in_use ( validated_users , & id ) ) {
2011-02-03 18:30:29 +03:00
exit_server_cleanly ( msg ) ;
}
}
2011-08-19 20:10:29 +04:00
void id_cache_register_kill_msg ( struct messaging_context * ctx )
2011-02-03 18:30:29 +03:00
{
2011-08-19 20:10:29 +04:00
messaging_register ( ctx , NULL , ID_CACHE_KILL , id_cache_kill ) ;
2011-02-03 18:30:29 +03:00
}