2004-12-11 13:19:41 +00:00
/*
Unix SMB / CIFS implementation .
manipulate privilege records in samdb
Copyright ( C ) Andrew Tridgell 2004
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-10 02:07:03 +00:00
the Free Software Foundation ; either version 3 of the License , or
2004-12-11 13:19:41 +00: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 02:07:03 +00:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2004-12-11 13:19:41 +00:00
*/
# include "includes.h"
2007-12-06 21:39:49 +01:00
# include "libcli/ldap/ldap_ndr.h"
2005-12-28 15:38:36 +00:00
# include "dsdb/samdb/samdb.h"
# include "auth/auth.h"
2006-04-02 12:02:01 +00:00
# include "libcli/security/security.h"
2010-10-17 14:27:18 +02:00
# include "../lib/util/util_ldb.h"
2007-12-02 17:09:52 +01:00
# include "param/param.h"
2009-10-16 17:05:27 +11:00
# include "ldb_wrap.h"
/* connect to the privilege database */
struct ldb_context * privilege_connect ( TALLOC_CTX * mem_ctx ,
struct loadparm_context * lp_ctx )
{
2010-12-20 21:26:35 +11:00
return ldb_wrap_connect ( mem_ctx , NULL , lp_ctx , " privilege.ldb " ,
2009-10-25 17:19:03 +11:00
NULL , NULL , 0 ) ;
2009-10-16 17:05:27 +11:00
}
2004-12-11 13:19:41 +00:00
/*
add privilege bits for one sid to a security_token
*/
2009-10-16 17:05:27 +11:00
static NTSTATUS samdb_privilege_setup_sid ( struct ldb_context * pdb , TALLOC_CTX * mem_ctx ,
2006-04-03 14:39:46 +00:00
struct security_token * token ,
const struct dom_sid * sid )
2004-12-11 13:19:41 +00:00
{
const char * const attrs [ ] = { " privilege " , NULL } ;
struct ldb_message * * res = NULL ;
struct ldb_message_element * el ;
2009-11-06 20:14:41 +01:00
unsigned int i ;
int ret ;
2005-08-11 13:12:45 +00:00
char * sidstr ;
2004-12-11 13:19:41 +00:00
2005-06-24 00:18:20 +00:00
sidstr = ldap_encode_ndr_dom_sid ( mem_ctx , sid ) ;
NT_STATUS_HAVE_NO_MEMORY ( sidstr ) ;
2004-12-11 13:19:41 +00:00
2009-10-16 17:05:27 +11:00
ret = gendb_search ( pdb , mem_ctx , NULL , & res , attrs , " objectSid=%s " , sidstr ) ;
2005-06-24 00:18:20 +00:00
talloc_free ( sidstr ) ;
2004-12-11 13:19:41 +00:00
if ( ret ! = 1 ) {
/* not an error to not match */
return NT_STATUS_OK ;
}
el = ldb_msg_find_element ( res [ 0 ] , " privilege " ) ;
if ( el = = NULL ) {
return NT_STATUS_OK ;
}
for ( i = 0 ; i < el - > num_values ; i + + ) {
2005-08-11 13:12:45 +00:00
const char * priv_str = ( const char * ) el - > values [ i ] . data ;
2006-04-03 14:39:46 +00:00
enum sec_privilege privilege = sec_privilege_id ( priv_str ) ;
2010-08-30 14:00:50 +10:00
if ( privilege = = SEC_PRIV_INVALID ) {
2010-09-11 16:58:45 +10:00
uint32_t right_bit = sec_right_bit ( priv_str ) ;
security_token_set_right_bit ( token , right_bit ) ;
if ( right_bit = = 0 ) {
DEBUG ( 1 , ( " Unknown privilege '%s' in samdb \n " ,
priv_str ) ) ;
}
2004-12-11 13:19:41 +00:00
continue ;
}
2006-04-03 15:18:12 +00:00
security_token_set_privilege ( token , privilege ) ;
2004-12-11 13:19:41 +00:00
}
return NT_STATUS_OK ;
}
/*
setup the privilege mask for this security token based on our
local SAM
*/
2010-12-20 21:26:35 +11:00
NTSTATUS samdb_privilege_setup ( struct loadparm_context * lp_ctx , struct security_token * token )
2004-12-11 13:19:41 +00:00
{
2009-10-16 17:05:27 +11:00
struct ldb_context * pdb ;
2005-10-07 11:31:45 +00:00
TALLOC_CTX * mem_ctx ;
2009-11-06 20:14:41 +01:00
unsigned int i ;
2004-12-11 13:19:41 +00:00
NTSTATUS status ;
2005-10-07 11:31:45 +00:00
/* Shortcuts to prevent recursion and avoid lookups */
2010-08-14 13:30:51 +10:00
if ( token - > sids = = NULL ) {
2007-07-19 07:48:26 +00:00
token - > privilege_mask = 0 ;
return NT_STATUS_OK ;
}
2006-03-31 11:05:33 +00:00
if ( security_token_is_system ( token ) ) {
2005-10-07 11:31:45 +00:00
token - > privilege_mask = ~ 0 ;
return NT_STATUS_OK ;
}
2006-03-31 11:05:33 +00:00
if ( security_token_is_anonymous ( token ) ) {
2005-10-07 11:31:45 +00:00
token - > privilege_mask = 0 ;
return NT_STATUS_OK ;
}
mem_ctx = talloc_new ( token ) ;
2010-12-20 21:26:35 +11:00
pdb = privilege_connect ( mem_ctx , lp_ctx ) ;
2009-10-16 17:05:27 +11:00
if ( pdb = = NULL ) {
2004-12-11 13:19:41 +00:00
talloc_free ( mem_ctx ) ;
return NT_STATUS_INTERNAL_DB_CORRUPTION ;
}
token - > privilege_mask = 0 ;
for ( i = 0 ; i < token - > num_sids ; i + + ) {
2009-10-16 17:05:27 +11:00
status = samdb_privilege_setup_sid ( pdb , mem_ctx ,
2010-08-20 12:15:15 +10:00
token , & token - > sids [ i ] ) ;
2004-12-11 13:19:41 +00:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
talloc_free ( mem_ctx ) ;
return status ;
}
}
talloc_free ( mem_ctx ) ;
return NT_STATUS_OK ;
}