2007-01-14 17:04:15 +00:00
/*
Unix SMB / CIFS mplementation .
2009-10-22 15:54:57 +11:00
The module that handles the Schema checkings and dynamic attributes
2007-01-14 17:04:15 +00:00
2007-03-14 19:10:21 +00:00
Copyright ( C ) Stefan Metzmacher < metze @ samba . org > 2007
2009-10-10 09:10:03 +11:00
Copyright ( C ) Andrew Bartlett < abartlet @ samba . org > 2009
2007-01-14 17:04:15 +00: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-10 02:07:03 +00:00
the Free Software Foundation ; either version 3 of the License , or
2007-01-14 17:04:15 +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/>.
2007-01-14 17:04:15 +00:00
*/
# include "includes.h"
2009-01-30 16:31:19 -05:00
# include "ldb_module.h"
2007-01-14 17:04:15 +00:00
# include "dsdb/samdb/samdb.h"
# include "librpc/gen_ndr/ndr_misc.h"
# include "librpc/gen_ndr/ndr_drsuapi.h"
# include "librpc/gen_ndr/ndr_drsblobs.h"
Make Samba4 pass the NET-API-BECOMEDC test against Win2k3 (again).
To make Samba4, using the python provision system, pass this test
required some major rework. Untested code is broken code, and some of
the refactoring for a seperate provision test (which also now passes)
broke things.
Similarly, the iconv work has compiled, but these codepaths have never
been run (NULL pointer de-reference).
In working to use a local, rather than global, loadparm context, and
to support using a target directory, a few things needed to be
reworked, particularly around path handling.
Andrew Bartlett
(This used to be commit 1169e8d7bee20477b0efbfea3534ac63c83fb3d6)
2008-03-06 21:55:26 +11:00
# include "param/param.h"
2009-10-10 09:10:03 +11:00
# include "dsdb/samdb/ldb_modules/util.h"
2007-01-14 17:04:15 +00:00
2008-08-15 20:40:57 +10:00
static int generate_objectClasses ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema ) ;
static int generate_attributeTypes ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema ) ;
static int generate_dITContentRules ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema ) ;
2008-11-14 09:33:08 +01:00
static int generate_extendedAttributeInfo ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema ) ;
static int generate_extendedClassInfo ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema ) ;
2009-04-09 13:45:04 +10:00
static int generate_possibleInferiors ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema ) ;
2008-08-15 20:40:57 +10:00
static const struct {
const char * attr ;
int ( * fn ) ( struct ldb_context * , struct ldb_message * , const struct dsdb_schema * ) ;
2009-04-09 13:45:04 +10:00
bool aggregate ;
2008-08-15 20:40:57 +10:00
} generated_attrs [ ] = {
{
. attr = " objectClasses " ,
2009-04-09 13:45:04 +10:00
. fn = generate_objectClasses ,
. aggregate = true ,
2008-08-15 20:40:57 +10:00
} ,
{
. attr = " attributeTypes " ,
2009-04-09 13:45:04 +10:00
. fn = generate_attributeTypes ,
. aggregate = true ,
2008-08-15 20:40:57 +10:00
} ,
{
. attr = " dITContentRules " ,
2009-04-09 13:45:04 +10:00
. fn = generate_dITContentRules ,
. aggregate = true ,
2008-11-14 09:33:08 +01:00
} ,
{
. attr = " extendedAttributeInfo " ,
2009-04-09 13:45:04 +10:00
. fn = generate_extendedAttributeInfo ,
. aggregate = true ,
2008-11-14 09:33:08 +01:00
} ,
{
. attr = " extendedClassInfo " ,
2009-04-09 13:45:04 +10:00
. fn = generate_extendedClassInfo ,
. aggregate = true ,
} ,
{
. attr = " possibleInferiors " ,
. fn = generate_possibleInferiors ,
. aggregate = false ,
2008-08-15 20:40:57 +10:00
}
} ;
2009-10-22 15:54:57 +11:00
struct schema_data_private_data {
2008-08-15 20:40:57 +10:00
struct ldb_dn * aggregate_dn ;
2009-10-22 15:54:57 +11:00
struct ldb_dn * schema_dn ;
2008-08-15 20:40:57 +10:00
} ;
2009-10-22 15:54:57 +11:00
struct schema_data_search_data {
2008-09-11 18:36:28 -04:00
struct ldb_module * module ;
struct ldb_request * req ;
const struct dsdb_schema * schema ;
2008-08-15 20:40:57 +10:00
} ;
2009-10-22 15:54:57 +11:00
static int schema_data_init ( struct ldb_module * module )
2007-01-14 17:04:15 +00:00
{
2009-01-30 16:31:19 -05:00
struct ldb_context * ldb ;
2007-01-14 17:04:15 +00:00
struct ldb_dn * schema_dn ;
int ret ;
2009-10-22 15:54:57 +11:00
struct schema_data_private_data * data ;
ret = ldb_next_init ( module ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
2007-06-21 10:18:20 +00:00
2009-01-30 16:31:19 -05:00
ldb = ldb_module_get_ctx ( module ) ;
schema_dn = samdb_schema_dn ( ldb ) ;
2007-01-14 17:04:15 +00:00
if ( ! schema_dn ) {
2009-01-30 16:31:19 -05:00
ldb_reset_err_string ( ldb ) ;
ldb_debug ( ldb , LDB_DEBUG_WARNING ,
2009-10-22 15:54:57 +11:00
" schema_data_init: no schema dn present: (skip schema loading) \n " ) ;
return LDB_SUCCESS ;
2007-01-14 17:04:15 +00:00
}
2009-10-22 15:54:57 +11:00
data = talloc ( module , struct schema_data_private_data ) ;
2008-08-15 20:40:57 +10:00
if ( data = = NULL ) {
2009-01-30 16:31:19 -05:00
ldb_oom ( ldb ) ;
2008-08-15 20:40:57 +10:00
return LDB_ERR_OPERATIONS_ERROR ;
}
2009-11-24 10:18:02 +11:00
data - > schema_dn = schema_dn ;
/* Used to check to see if this is a result on the CN=Aggregate schema */
data - > aggregate_dn = samdb_aggregate_schema_dn ( ldb , data ) ;
if ( ! data - > aggregate_dn ) {
ldb_set_errstring ( ldb , " Could not build aggregate schema DN " ) ;
2008-08-15 20:40:57 +10:00
return LDB_ERR_OPERATIONS_ERROR ;
}
2009-10-22 15:54:57 +11:00
ldb_module_set_private ( module , data ) ;
return LDB_SUCCESS ;
2007-01-14 17:04:15 +00:00
}
2009-10-22 15:54:57 +11:00
static int schema_data_add ( struct ldb_module * module , struct ldb_request * req )
2008-06-30 17:17:24 +02:00
{
2009-01-30 16:31:19 -05:00
struct ldb_context * ldb ;
2008-06-30 17:17:24 +02:00
struct dsdb_schema * schema ;
2009-08-24 20:22:18 +10:00
const struct ldb_val * attributeID = NULL ;
const struct ldb_val * governsID = NULL ;
2008-06-30 17:17:24 +02:00
const char * oid_attr = NULL ;
const char * oid = NULL ;
WERROR status ;
2009-01-30 16:31:19 -05:00
ldb = ldb_module_get_ctx ( module ) ;
2008-09-27 02:27:54 +02:00
/* special objects should always go through */
if ( ldb_dn_is_special ( req - > op . add . message - > dn ) ) {
return ldb_next_request ( module , req ) ;
}
/* replicated update should always go through */
if ( ldb_request_get_control ( req , DSDB_CONTROL_REPLICATED_UPDATE_OID ) ) {
return ldb_next_request ( module , req ) ;
}
2009-01-30 16:31:19 -05:00
schema = dsdb_get_schema ( ldb ) ;
2008-06-30 17:17:24 +02:00
if ( ! schema ) {
return ldb_next_request ( module , req ) ;
}
if ( ! schema - > fsmo . we_are_master ) {
2009-01-30 16:31:19 -05:00
ldb_debug_set ( ldb , LDB_DEBUG_ERROR ,
2009-10-22 15:54:57 +11:00
" schema_data_add: we are not master: reject request \n " ) ;
2008-06-30 17:17:24 +02:00
return LDB_ERR_UNWILLING_TO_PERFORM ;
}
2009-08-24 20:22:18 +10:00
attributeID = ldb_msg_find_ldb_val ( req - > op . add . message , " attributeID " ) ;
governsID = ldb_msg_find_ldb_val ( req - > op . add . message , " governsID " ) ;
2008-06-30 17:17:24 +02:00
if ( attributeID ) {
oid_attr = " attributeID " ;
2009-08-24 20:22:18 +10:00
oid = talloc_strndup ( req , ( const char * ) attributeID - > data , attributeID - > length ) ;
2008-06-30 17:17:24 +02:00
} else if ( governsID ) {
oid_attr = " governsID " ;
2009-08-24 20:22:18 +10:00
oid = talloc_strndup ( req , ( const char * ) governsID - > data , governsID - > length ) ;
} else {
return ldb_next_request ( module , req ) ;
2008-06-30 17:17:24 +02:00
}
if ( ! oid ) {
2009-08-24 20:22:18 +10:00
ldb_oom ( ldb ) ;
return LDB_ERR_OPERATIONS_ERROR ;
2008-06-30 17:17:24 +02:00
}
2009-08-24 20:22:18 +10:00
2009-12-18 03:53:13 +02:00
status = dsdb_schema_pfm_find_oid ( schema - > prefixmap , oid , NULL ) ;
2008-06-30 17:17:24 +02:00
if ( W_ERROR_IS_OK ( status ) ) {
return ldb_next_request ( module , req ) ;
} else if ( ! W_ERROR_EQUAL ( WERR_DS_NO_MSDS_INTID , status ) ) {
2009-01-30 16:31:19 -05:00
ldb_debug_set ( ldb , LDB_DEBUG_ERROR ,
2009-10-22 15:54:57 +11:00
" schema_data_add: failed to map %s[%s]: %s \n " ,
2008-06-30 17:17:24 +02:00
oid_attr , oid , win_errstr ( status ) ) ;
return LDB_ERR_UNWILLING_TO_PERFORM ;
}
2009-01-30 16:31:19 -05:00
status = dsdb_create_prefix_mapping ( ldb , schema , oid ) ;
2008-06-30 17:17:24 +02:00
if ( ! W_ERROR_IS_OK ( status ) ) {
2009-01-30 16:31:19 -05:00
ldb_debug_set ( ldb , LDB_DEBUG_ERROR ,
2009-10-22 15:54:57 +11:00
" schema_data_add: failed to create prefix mapping for %s[%s]: %s \n " ,
2008-06-30 17:17:24 +02:00
oid_attr , oid , win_errstr ( status ) ) ;
return LDB_ERR_UNWILLING_TO_PERFORM ;
}
return ldb_next_request ( module , req ) ;
}
2008-08-15 20:40:57 +10:00
static int generate_objectClasses ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema )
{
2009-02-02 11:25:11 +01:00
const struct dsdb_class * sclass ;
2008-08-15 20:40:57 +10:00
int ret ;
2009-02-02 11:25:11 +01:00
for ( sclass = schema - > classes ; sclass ; sclass = sclass - > next ) {
ret = ldb_msg_add_string ( msg , " objectClasses " , schema_class_to_description ( msg , sclass ) ) ;
2008-08-15 20:40:57 +10:00
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
}
return LDB_SUCCESS ;
}
static int generate_attributeTypes ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema )
{
const struct dsdb_attribute * attribute ;
int ret ;
for ( attribute = schema - > attributes ; attribute ; attribute = attribute - > next ) {
ret = ldb_msg_add_string ( msg , " attributeTypes " , schema_attribute_to_description ( msg , attribute ) ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
}
return LDB_SUCCESS ;
}
static int generate_dITContentRules ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema )
{
2009-02-02 11:25:11 +01:00
const struct dsdb_class * sclass ;
2008-08-15 20:40:57 +10:00
int ret ;
2009-02-02 11:25:11 +01:00
for ( sclass = schema - > classes ; sclass ; sclass = sclass - > next ) {
if ( sclass - > auxiliaryClass | | sclass - > systemAuxiliaryClass ) {
char * ditcontentrule = schema_class_to_dITContentRule ( msg , sclass , schema ) ;
2008-08-15 20:40:57 +10:00
if ( ! ditcontentrule ) {
ldb_oom ( ldb ) ;
return LDB_ERR_OPERATIONS_ERROR ;
}
ret = ldb_msg_add_steal_string ( msg , " dITContentRules " , ditcontentrule ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
}
}
2008-09-09 15:06:13 +02:00
return LDB_SUCCESS ;
2008-08-15 20:40:57 +10:00
}
2008-11-14 09:33:08 +01:00
static int generate_extendedAttributeInfo ( struct ldb_context * ldb ,
struct ldb_message * msg ,
const struct dsdb_schema * schema )
{
const struct dsdb_attribute * attribute ;
int ret ;
for ( attribute = schema - > attributes ; attribute ; attribute = attribute - > next ) {
char * val = schema_attribute_to_extendedInfo ( msg , attribute ) ;
if ( ! val ) {
ldb_oom ( ldb ) ;
return LDB_ERR_OPERATIONS_ERROR ;
}
ret = ldb_msg_add_string ( msg , " extendedAttributeInfo " , val ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
}
return LDB_SUCCESS ;
}
static int generate_extendedClassInfo ( struct ldb_context * ldb ,
struct ldb_message * msg ,
const struct dsdb_schema * schema )
{
const struct dsdb_class * sclass ;
int ret ;
for ( sclass = schema - > classes ; sclass ; sclass = sclass - > next ) {
char * val = schema_class_to_extendedInfo ( msg , sclass ) ;
if ( ! val ) {
ldb_oom ( ldb ) ;
return LDB_ERR_OPERATIONS_ERROR ;
}
2008-08-15 20:40:57 +10:00
2008-11-14 09:33:08 +01:00
ret = ldb_msg_add_string ( msg , " extendedClassInfo " , val ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
}
return LDB_SUCCESS ;
}
2008-08-15 20:40:57 +10:00
2009-04-09 13:45:04 +10:00
static int generate_possibleInferiors ( struct ldb_context * ldb , struct ldb_message * msg ,
const struct dsdb_schema * schema )
{
struct ldb_dn * dn = msg - > dn ;
int ret , i ;
const char * first_component_name = ldb_dn_get_component_name ( dn , 0 ) ;
const struct ldb_val * first_component_val ;
const struct dsdb_class * schema_class ;
const char * * possibleInferiors ;
if ( strcasecmp ( first_component_name , " cn " ) ! = 0 ) {
return LDB_SUCCESS ;
}
first_component_val = ldb_dn_get_component_val ( dn , 0 ) ;
2009-08-05 08:53:11 +10:00
schema_class = dsdb_class_by_cn_ldb_val ( schema , first_component_val ) ;
2009-04-09 13:45:04 +10:00
if ( schema_class = = NULL ) {
return LDB_SUCCESS ;
}
possibleInferiors = schema_class - > possibleInferiors ;
if ( possibleInferiors = = NULL ) {
return LDB_SUCCESS ;
}
for ( i = 0 ; possibleInferiors [ i ] ; i + + ) {
ret = ldb_msg_add_string ( msg , " possibleInferiors " , possibleInferiors [ i ] ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
}
return LDB_SUCCESS ;
}
2008-08-15 20:40:57 +10:00
/* Add objectClasses, attributeTypes and dITContentRules from the
schema object ( they are not stored in the database )
*/
2009-10-22 15:54:57 +11:00
static int schema_data_search_callback ( struct ldb_request * req , struct ldb_reply * ares )
2008-08-15 20:40:57 +10:00
{
2009-01-30 16:31:19 -05:00
struct ldb_context * ldb ;
2009-10-22 15:54:57 +11:00
struct schema_data_search_data * ac ;
struct schema_data_private_data * mc ;
2008-08-15 20:40:57 +10:00
int i , ret ;
2009-10-22 15:54:57 +11:00
ac = talloc_get_type ( req - > context , struct schema_data_search_data ) ;
mc = talloc_get_type ( ldb_module_get_private ( ac - > module ) , struct schema_data_private_data ) ;
2009-01-30 16:31:19 -05:00
ldb = ldb_module_get_ctx ( ac - > module ) ;
2008-08-15 20:40:57 +10:00
2008-09-11 18:36:28 -04:00
if ( ! ares ) {
return ldb_module_done ( ac - > req , NULL , NULL ,
LDB_ERR_OPERATIONS_ERROR ) ;
2008-08-15 20:40:57 +10:00
}
2008-09-11 18:36:28 -04:00
if ( ares - > error ! = LDB_SUCCESS ) {
return ldb_module_done ( ac - > req , ares - > controls ,
ares - > response , ares - > error ) ;
2008-08-15 20:40:57 +10:00
}
2008-09-11 18:36:28 -04:00
/* Only entries are interesting, and we handle the case of the parent seperatly */
2008-08-15 20:40:57 +10:00
2008-09-11 18:36:28 -04:00
switch ( ares - > type ) {
case LDB_REPLY_ENTRY :
2009-04-09 13:45:04 +10:00
if ( ldb_dn_compare ( ares - > message - > dn , mc - > aggregate_dn ) = = 0 ) {
for ( i = 0 ; i < ARRAY_SIZE ( generated_attrs ) ; i + + ) {
if ( generated_attrs [ i ] . aggregate & &
ldb_attr_in_list ( ac - > req - > op . search . attrs , generated_attrs [ i ] . attr ) ) {
ret = generated_attrs [ i ] . fn ( ldb , ares - > message , ac - > schema ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
}
}
2009-10-22 15:54:57 +11:00
} else if ( ( ldb_dn_compare_base ( mc - > schema_dn , ares - > message - > dn ) = = 0 )
& & ( ldb_dn_compare ( mc - > schema_dn , ares - > message - > dn ) ! = 0 ) ) {
2009-04-09 13:45:04 +10:00
for ( i = 0 ; i < ARRAY_SIZE ( generated_attrs ) ; i + + ) {
if ( ! generated_attrs [ i ] . aggregate & &
ldb_attr_in_list ( ac - > req - > op . search . attrs , generated_attrs [ i ] . attr ) ) {
ret = generated_attrs [ i ] . fn ( ldb , ares - > message , ac - > schema ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
2008-09-11 18:36:28 -04:00
}
2008-08-15 20:40:57 +10:00
}
}
2008-09-11 18:36:28 -04:00
2009-04-09 13:45:04 +10:00
2008-12-16 08:59:05 +01:00
return ldb_module_send_entry ( ac - > req , ares - > message , ares - > controls ) ;
2008-09-11 18:36:28 -04:00
case LDB_REPLY_REFERRAL :
return ldb_module_send_referral ( ac - > req , ares - > referral ) ;
case LDB_REPLY_DONE :
return ldb_module_done ( ac - > req , ares - > controls ,
ares - > response , ares - > error ) ;
2008-08-15 20:40:57 +10:00
}
2008-09-11 18:36:28 -04:00
return LDB_SUCCESS ;
2008-08-15 20:40:57 +10:00
}
/* search */
2009-10-22 15:54:57 +11:00
static int schema_data_search ( struct ldb_module * module , struct ldb_request * req )
2008-08-15 20:40:57 +10:00
{
2009-01-30 16:31:19 -05:00
struct ldb_context * ldb = ldb_module_get_ctx ( module ) ;
2008-08-15 20:40:57 +10:00
int i , ret ;
2009-10-22 15:54:57 +11:00
struct schema_data_search_data * search_context ;
2008-08-15 20:40:57 +10:00
struct ldb_request * down_req ;
2009-01-30 16:31:19 -05:00
struct dsdb_schema * schema = dsdb_get_schema ( ldb ) ;
2008-08-15 20:40:57 +10:00
2009-01-30 16:31:19 -05:00
if ( ! schema | | ! ldb_module_get_private ( module ) ) {
2008-08-15 20:40:57 +10:00
/* If there is no schema, there is little we can do */
return ldb_next_request ( module , req ) ;
}
for ( i = 0 ; i < ARRAY_SIZE ( generated_attrs ) ; i + + ) {
if ( ldb_attr_in_list ( req - > op . search . attrs , generated_attrs [ i ] . attr ) ) {
break ;
}
}
if ( i = = ARRAY_SIZE ( generated_attrs ) ) {
/* No request for a generated attr found, nothing to
* see here , move along . . . */
return ldb_next_request ( module , req ) ;
}
2009-10-22 15:54:57 +11:00
search_context = talloc ( req , struct schema_data_search_data ) ;
2008-08-15 20:40:57 +10:00
if ( ! search_context ) {
2009-01-30 16:31:19 -05:00
ldb_oom ( ldb ) ;
2008-08-15 20:40:57 +10:00
return LDB_ERR_OPERATIONS_ERROR ;
}
2008-09-11 18:36:28 -04:00
search_context - > module = module ;
search_context - > req = req ;
search_context - > schema = schema ;
2008-08-15 20:40:57 +10:00
2009-01-30 16:31:19 -05:00
ret = ldb_build_search_req_ex ( & down_req , ldb , search_context ,
2008-09-11 18:36:28 -04:00
req - > op . search . base ,
req - > op . search . scope ,
req - > op . search . tree ,
req - > op . search . attrs ,
req - > controls ,
2009-10-22 15:54:57 +11:00
search_context , schema_data_search_callback ,
2008-09-11 18:36:28 -04:00
req ) ;
if ( ret ! = LDB_SUCCESS ) {
return LDB_ERR_OPERATIONS_ERROR ;
2008-08-15 20:40:57 +10:00
}
2008-09-11 18:36:28 -04:00
return ldb_next_request ( module , down_req ) ;
2008-08-15 20:40:57 +10:00
}
2009-10-22 15:54:57 +11:00
_PUBLIC_ const struct ldb_module_ops ldb_schema_data_module_ops = {
. name = " schema_data " ,
. init_context = schema_data_init ,
. add = schema_data_add ,
. search = schema_data_search
2007-01-14 17:04:15 +00:00
} ;