1998-03-12 00:11:04 +03:00
/*
2002-01-30 09:08:46 +03:00
* Unix SMB / CIFS implementation .
1998-03-12 00:11:04 +03:00
* RPC Pipe client / server routines
* Copyright ( C ) Andrew Tridgell 1992 - 1997 ,
* Copyright ( C ) Luke Kenneth Casson Leighton 1996 - 1997 ,
2001-03-11 03:32:10 +03:00
* Copyright ( C ) Jeremy Allison 2001.
1998-03-12 00:11:04 +03: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
1998-03-12 00:11:04 +03:00
* ( 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
2007-07-10 09:23:25 +04:00
* along with this program ; if not , see < http : //www.gnu.org/licenses/>.
1998-03-12 00:11:04 +03:00
*/
# include "includes.h"
2002-07-15 14:35:28 +04:00
# undef DBGC_CLASS
# define DBGC_CLASS DBGC_RPC_SRV
2001-03-13 23:18:45 +03:00
/* This is the max handles across all instances of a pipe name. */
1998-03-12 00:11:04 +03:00
# ifndef MAX_OPEN_POLS
2001-03-13 23:18:45 +03:00
# define MAX_OPEN_POLS 1024
1998-03-12 00:11:04 +03:00
# endif
2002-03-30 03:45:26 +03:00
/****************************************************************************
Hack as handles need to be persisant over lsa pipe closes so long as a samr
pipe is open . JRA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
static bool is_samr_lsa_pipe ( const char * pipe_name )
2002-03-30 03:45:26 +03:00
{
return ( strstr ( pipe_name , " samr " ) | | strstr ( pipe_name , " lsa " ) ) ;
}
1998-03-12 00:11:04 +03:00
/****************************************************************************
2001-03-13 23:18:45 +03:00
Initialise a policy handle list on a pipe . Handle list is shared between all
pipes of the same name .
1998-03-12 00:11:04 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-03-11 03:32:10 +03:00
2007-10-19 04:40:25 +04:00
bool init_pipe_handle_list ( pipes_struct * p , const char * pipe_name )
1998-03-12 00:11:04 +03:00
{
2009-01-07 19:36:54 +03:00
pipes_struct * plist ;
struct handle_list * hl ;
2001-03-13 23:18:45 +03:00
2009-01-07 19:36:54 +03:00
for ( plist = get_first_internal_pipe ( ) ;
plist ;
plist = get_next_internal_pipe ( plist ) ) {
if ( strequal ( plist - > name , pipe_name ) ) {
break ;
}
if ( is_samr_lsa_pipe ( plist - > name )
& & is_samr_lsa_pipe ( pipe_name ) ) {
/*
* samr and lsa share a handle space ( same process
* under Windows ? )
*/
2001-03-13 23:18:45 +03:00
break ;
}
}
2009-01-07 19:36:54 +03:00
if ( plist ! = NULL ) {
hl = plist - > pipe_handles ;
if ( hl = = NULL ) {
return false ;
}
} else {
2001-03-13 23:18:45 +03:00
/*
2009-01-07 19:36:54 +03:00
* First open , we have to create the handle list
2001-03-13 23:18:45 +03:00
*/
2009-01-07 19:36:54 +03:00
hl = SMB_MALLOC_P ( struct handle_list ) ;
if ( hl = = NULL ) {
return false ;
}
2001-03-13 23:18:45 +03:00
ZERO_STRUCTP ( hl ) ;
DEBUG ( 10 , ( " init_pipe_handles: created handle list for pipe %s \n " , pipe_name ) ) ;
}
/*
* One more pipe is using this list .
*/
hl - > pipe_ref_count + + ;
/*
* Point this pipe at this list .
*/
p - > pipe_handles = hl ;
2003-11-03 17:34:25 +03:00
DEBUG ( 10 , ( " init_pipe_handles: pipe_handles ref count = %lu for pipe %s \n " ,
( unsigned long ) p - > pipe_handles - > pipe_ref_count , pipe_name ) ) ;
2001-03-13 23:18:45 +03:00
return True ;
1998-03-12 00:11:04 +03:00
}
/****************************************************************************
find first available policy slot . creates a policy handle for you .
2009-01-07 20:44:52 +03:00
If " data_ptr " is given , this must be a talloc ' ed object , create_policy_hnd
talloc_moves this into the handle . If the policy_hnd is closed ,
data_ptr is TALLOC_FREE ( ) ' ed
1998-03-12 00:11:04 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-03-11 03:32:10 +03:00
2009-01-07 20:44:52 +03:00
bool create_policy_hnd ( pipes_struct * p , POLICY_HND * hnd , void * data_ptr )
1998-03-12 00:11:04 +03:00
{
2001-03-11 03:32:10 +03:00
static uint32 pol_hnd_low = 0 ;
static uint32 pol_hnd_high = 0 ;
2006-09-12 02:02:34 +04:00
time_t t = time ( NULL ) ;
1998-03-12 00:11:04 +03:00
2001-03-11 03:32:10 +03:00
struct policy * pol ;
1998-03-12 00:11:04 +03:00
2001-03-13 23:18:45 +03:00
if ( p - > pipe_handles - > count > MAX_OPEN_POLS ) {
DEBUG ( 0 , ( " create_policy_hnd: ERROR: too many handles (%d) on this pipe. \n " ,
( int ) p - > pipe_handles - > count ) ) ;
1998-08-17 08:54:06 +04:00
return False ;
}
1998-03-12 00:11:04 +03:00
2009-01-07 20:44:52 +03:00
pol = TALLOC_ZERO_P ( NULL , struct policy ) ;
2001-03-11 03:32:10 +03:00
if ( ! pol ) {
DEBUG ( 0 , ( " create_policy_hnd: ERROR: out of memory! \n " ) ) ;
1998-08-17 08:54:06 +04:00
return False ;
1998-03-12 00:11:04 +03:00
}
2009-01-07 20:44:52 +03:00
if ( data_ptr ! = NULL ) {
pol - > data_ptr = talloc_move ( pol , & data_ptr ) ;
}
1998-03-12 00:11:04 +03:00
2002-01-22 22:45:17 +03:00
pol_hnd_low + + ;
if ( pol_hnd_low = = 0 )
( pol_hnd_high ) + + ;
2006-09-12 02:02:34 +04:00
SIVAL ( & pol - > pol_hnd . handle_type , 0 , 0 ) ; /* first bit must be null */
SIVAL ( & pol - > pol_hnd . uuid . time_low , 0 , pol_hnd_low ) ; /* second bit is incrementing */
SSVAL ( & pol - > pol_hnd . uuid . time_mid , 0 , pol_hnd_high ) ; /* second bit is incrementing */
SSVAL ( & pol - > pol_hnd . uuid . time_hi_and_version , 0 , ( pol_hnd_high > > 16 ) ) ; /* second bit is incrementing */
/* split the current time into two 16 bit values */
SSVAL ( pol - > pol_hnd . uuid . clock_seq , 0 , ( t > > 16 ) ) ; /* something random */
SSVAL ( pol - > pol_hnd . uuid . node , 0 , t ) ; /* something random */
SIVAL ( pol - > pol_hnd . uuid . node , 2 , sys_getpid ( ) ) ; /* something more random */
1998-08-17 08:54:06 +04:00
2001-03-13 23:18:45 +03:00
DLIST_ADD ( p - > pipe_handles - > Policy , pol ) ;
p - > pipe_handles - > count + + ;
1998-08-17 08:54:06 +04:00
2001-03-11 03:32:10 +03:00
* hnd = pol - > pol_hnd ;
1998-08-17 08:54:06 +04:00
2001-03-13 23:18:45 +03:00
DEBUG ( 4 , ( " Opened policy hnd[%d] " , ( int ) p - > pipe_handles - > count ) ) ;
2007-03-28 17:34:59 +04:00
dump_data ( 4 , ( uint8 * ) hnd , sizeof ( * hnd ) ) ;
1998-08-17 08:54:06 +04:00
return True ;
1998-03-12 00:11:04 +03:00
}
/****************************************************************************
2001-03-11 03:32:10 +03:00
find policy by handle - internal version .
1998-03-12 00:11:04 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-03-11 03:32:10 +03:00
static struct policy * find_policy_by_hnd_internal ( pipes_struct * p , POLICY_HND * hnd , void * * data_p )
{
struct policy * pol ;
size_t i ;
if ( data_p )
* data_p = NULL ;
2001-03-13 23:18:45 +03:00
for ( i = 0 , pol = p - > pipe_handles - > Policy ; pol ; pol = pol - > next , i + + ) {
2001-03-11 03:32:10 +03:00
if ( memcmp ( & pol - > pol_hnd , hnd , sizeof ( * hnd ) ) = = 0 ) {
DEBUG ( 4 , ( " Found policy hnd[%d] " , ( int ) i ) ) ;
2007-03-28 17:34:59 +04:00
dump_data ( 4 , ( uint8 * ) hnd , sizeof ( * hnd ) ) ;
2001-03-11 03:32:10 +03:00
if ( data_p )
* data_p = pol - > data_ptr ;
return pol ;
1998-03-12 00:11:04 +03:00
}
}
DEBUG ( 4 , ( " Policy not found: " ) ) ;
2007-03-28 17:34:59 +04:00
dump_data ( 4 , ( uint8 * ) hnd , sizeof ( * hnd ) ) ;
1998-03-12 00:11:04 +03:00
2002-01-25 08:28:37 +03:00
p - > bad_handle_fault_state = True ;
1998-08-17 08:54:06 +04:00
return NULL ;
}
/****************************************************************************
2001-03-11 03:32:10 +03:00
find policy by handle
1998-08-17 08:54:06 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool find_policy_by_hnd ( pipes_struct * p , POLICY_HND * hnd , void * * data_p )
1998-03-12 00:11:04 +03:00
{
2001-03-11 03:32:10 +03:00
return find_policy_by_hnd_internal ( p , hnd , data_p ) = = NULL ? False : True ;
1998-03-12 00:11:04 +03:00
}
/****************************************************************************
2001-03-11 03:32:10 +03:00
Close a policy .
1998-03-12 00:11:04 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-08-17 08:54:06 +04:00
2007-10-19 04:40:25 +04:00
bool close_policy_hnd ( pipes_struct * p , POLICY_HND * hnd )
1998-10-22 02:36:26 +04:00
{
2001-03-11 03:32:10 +03:00
struct policy * pol = find_policy_by_hnd_internal ( p , hnd , NULL ) ;
1998-10-22 02:36:26 +04:00
2001-03-11 03:32:10 +03:00
if ( ! pol ) {
DEBUG ( 3 , ( " Error closing policy \n " ) ) ;
return False ;
1998-10-22 02:36:26 +04:00
}
2001-03-11 03:32:10 +03:00
DEBUG ( 3 , ( " Closed policy \n " ) ) ;
1998-10-22 02:36:26 +04:00
2001-04-03 02:27:40 +04:00
p - > pipe_handles - > count - - ;
1998-03-12 00:11:04 +03:00
2001-04-03 02:27:40 +04:00
DLIST_REMOVE ( p - > pipe_handles - > Policy , pol ) ;
1998-08-17 08:54:06 +04:00
2009-01-07 20:44:52 +03:00
TALLOC_FREE ( pol ) ;
1999-11-19 01:03:47 +03:00
2001-03-11 03:32:10 +03:00
return True ;
1999-11-24 21:09:33 +03:00
}
1998-03-12 00:11:04 +03:00
/****************************************************************************
2001-03-13 23:18:45 +03:00
Close a pipe - free the handle list if it was the last pipe reference .
1998-03-12 00:11:04 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-03-11 03:32:10 +03:00
void close_policy_by_pipe ( pipes_struct * p )
{
2001-03-13 23:18:45 +03:00
p - > pipe_handles - > pipe_ref_count - - ;
if ( p - > pipe_handles - > pipe_ref_count = = 0 ) {
/*
* Last pipe open on this list - free the list .
*/
while ( p - > pipe_handles - > Policy )
close_policy_hnd ( p , & p - > pipe_handles - > Policy - > pol_hnd ) ;
1998-08-17 08:54:06 +04:00
2001-03-13 23:18:45 +03:00
p - > pipe_handles - > Policy = NULL ;
p - > pipe_handles - > count = 0 ;
2001-09-17 14:26:23 +04:00
SAFE_FREE ( p - > pipe_handles ) ;
2001-03-13 23:18:45 +03:00
DEBUG ( 10 , ( " close_policy_by_pipe: deleted handle list for pipe %s \n " , p - > name ) ) ;
}
1998-03-12 00:11:04 +03:00
}
2002-07-15 14:35:28 +04:00
/*******************************************************************
Shall we allow access to this rpc ? Currently this function
implements the ' restrict anonymous ' setting by denying access to
anonymous users if the restrict anonymous level is > 0. Further work
will be checking a security descriptor to determine whether a user
token has enough access to access the pipe .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool pipe_access_check ( pipes_struct * p )
2002-07-15 14:35:28 +04:00
{
/* Don't let anonymous users access this RPC if restrict
anonymous > 0 */
if ( lp_restrict_anonymous ( ) > 0 ) {
2005-02-19 14:09:52 +03:00
/* schannel, so we must be ok */
2005-09-30 21:13:37 +04:00
if ( p - > pipe_bound & & ( p - > auth . auth_type = = PIPE_AUTH_TYPE_SCHANNEL ) ) {
2005-02-19 14:09:52 +03:00
return True ;
2005-09-30 21:13:37 +04:00
}
2005-02-19 14:09:52 +03:00
2008-06-24 17:24:08 +04:00
if ( p - > server_info - > guest ) {
2002-07-15 14:35:28 +04:00
return False ;
2005-09-30 21:13:37 +04:00
}
2002-07-15 14:35:28 +04:00
}
return True ;
}