2008-06-15 04:37:40 +04:00
/*
2004-03-31 10:45:39 +04:00
ldb database library
2009-10-06 11:30:53 +04:00
Copyright ( C ) Andrew Tridgell 2004
Copyright ( C ) Stefan Metzmacher 2004
Copyright ( C ) Simo Sorce 2006 - 2008
2010-10-18 22:07:49 +04:00
Copyright ( C ) Matthias Dieter Wallnöfer 2009 - 2010
2004-03-31 10:45:39 +04:00
* * NOTE ! The following LGPL license applies to the ldb
* * library . This does NOT imply that all of Samba is released
* * under the LGPL
2008-06-15 04:37:40 +04:00
2004-03-31 10:45:39 +04:00
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
2007-07-10 06:46:15 +04:00
version 3 of the License , or ( at your option ) any later version .
2004-03-31 10:45:39 +04:00
This library 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
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
2007-07-10 07:42:26 +04:00
License along with this library ; if not , see < http : //www.gnu.org/licenses/>.
2004-03-31 10:45:39 +04:00
*/
/*
2006-03-03 20:44:03 +03:00
* Name : ldb_tdb
2004-03-31 10:45:39 +04:00
*
* Component : ldb tdb backend
*
* Description : core functions for tdb backend
*
* Author : Andrew Tridgell
2004-10-20 23:28:02 +04:00
* Author : Stefan Metzmacher
2006-03-03 20:44:03 +03:00
*
* Modifications :
*
2010-02-21 09:22:45 +03:00
* - description : make the module use asynchronous calls
2006-03-03 20:44:03 +03:00
* date : Feb 2006
* Author : Simo Sorce
2008-09-12 02:34:11 +04:00
*
* - description : make it possible to use event contexts
* date : Jan 2008
* Author : Simo Sorce
2009-10-06 11:30:53 +04:00
*
* - description : fix up memory leaks and small bugs
* date : Oct 2009
* Author : Matthias Dieter Wallnöfer
2004-03-31 10:45:39 +04:00
*/
2007-05-05 22:50:56 +04:00
# include "ldb_tdb.h"
2011-06-20 13:10:31 +04:00
# include <lib/tdb_compat/tdb_compat.h>
2004-03-31 10:45:39 +04:00
2005-10-28 07:43:39 +04:00
/*
map a tdb error code to a ldb error code
*/
2009-10-22 04:06:33 +04:00
int ltdb_err_map ( enum TDB_ERROR tdb_code )
2005-10-28 07:43:39 +04:00
{
switch ( tdb_code ) {
case TDB_SUCCESS :
return LDB_SUCCESS ;
case TDB_ERR_CORRUPT :
case TDB_ERR_OOM :
case TDB_ERR_EINVAL :
return LDB_ERR_OPERATIONS_ERROR ;
case TDB_ERR_IO :
return LDB_ERR_PROTOCOL_ERROR ;
case TDB_ERR_LOCK :
case TDB_ERR_NOLOCK :
return LDB_ERR_BUSY ;
case TDB_ERR_LOCK_TIMEOUT :
return LDB_ERR_TIME_LIMIT_EXCEEDED ;
case TDB_ERR_EXISTS :
return LDB_ERR_ENTRY_ALREADY_EXISTS ;
case TDB_ERR_NOEXIST :
return LDB_ERR_NO_SUCH_OBJECT ;
case TDB_ERR_RDONLY :
return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS ;
2010-05-30 23:58:11 +04:00
default :
break ;
2005-10-28 07:43:39 +04:00
}
return LDB_ERR_OTHER ;
}
2008-10-15 22:03:20 +04:00
/*
lock the database for read - use by ltdb_search and ltdb_sequence_number
*/
int ltdb_lock_read ( struct ldb_module * module )
{
2009-01-30 02:39:30 +03:00
void * data = ldb_module_get_private ( module ) ;
struct ltdb_private * ltdb = talloc_get_type ( data , struct ltdb_private ) ;
2009-12-09 06:37:26 +03:00
int ret = 0 ;
if ( ltdb - > in_transaction = = 0 & &
ltdb - > read_lock_count = = 0 ) {
ret = tdb_lockall_read ( ltdb - > tdb ) ;
2008-10-15 22:03:20 +04:00
}
2009-12-09 06:37:26 +03:00
if ( ret = = 0 ) {
ltdb - > read_lock_count + + ;
}
return ret ;
2008-10-15 22:03:20 +04:00
}
/*
unlock the database after a ltdb_lock_read ( )
*/
int ltdb_unlock_read ( struct ldb_module * module )
{
2009-01-30 02:39:30 +03:00
void * data = ldb_module_get_private ( module ) ;
struct ltdb_private * ltdb = talloc_get_type ( data , struct ltdb_private ) ;
2009-12-09 06:37:26 +03:00
if ( ltdb - > in_transaction = = 0 & & ltdb - > read_lock_count = = 1 ) {
2008-10-15 22:03:20 +04:00
return tdb_unlockall_read ( ltdb - > tdb ) ;
}
2009-12-09 06:37:26 +03:00
ltdb - > read_lock_count - - ;
2008-10-15 22:03:20 +04:00
return 0 ;
}
2005-10-28 07:43:39 +04:00
2004-03-31 10:45:39 +04:00
/*
form a TDB_DATA for a record key
caller frees
2004-05-01 13:45:56 +04:00
2008-06-15 04:37:40 +04:00
note that the key for a record can depend on whether the
2004-05-01 13:45:56 +04:00
dn refers to a case sensitive index record or not
2004-03-31 10:45:39 +04:00
*/
2009-10-02 16:39:19 +04:00
struct TDB_DATA ltdb_key ( struct ldb_module * module , struct ldb_dn * dn )
2004-03-31 10:45:39 +04:00
{
2009-10-02 16:39:19 +04:00
struct ldb_context * ldb = ldb_module_get_ctx ( module ) ;
2004-03-31 10:45:39 +04:00
TDB_DATA key ;
2009-10-02 16:39:19 +04:00
char * key_str = NULL ;
const char * dn_folded = NULL ;
2004-05-01 13:45:56 +04:00
/*
most DNs are case insensitive . The exception is index DNs for
case sensitive attributes
2004-05-04 09:58:22 +04:00
there are 3 cases dealt with in this code :
r8037: a fairly major update to the internals of ldb. Changes are:
- moved the knowledge of attribute types out of ldb_tdb and into the
generic ldb code. This allows the ldb_match() message match logic
to be generic, so it can be used by other backend
- added the generic ability to load attribute handlers, for
canonicalisation, compare, ldif read and ldif write. In the future
this will be used by the schema module to allow us to correctly
obey the attributetype schema elements
- added attribute handlers for some of the core ldap attribute types,
Integer, DirectoryString, DN, ObjectClass etc
- added automatic registration of attribute handlers for well-known
attribute names 'cn', 'dc', 'dn', 'ou' and 'objectClass'
- converted the objectSid special handlers for Samba to the new system
- added more correct handling of indexing in tdb backend based on the
attribute canonicalisation function
- added generic support for subclasses, moving it out of the tdb
backend. This will be used in future by the schema module
- fixed several bugs in the dn_explode code. It still needs more
work, but doesn't corrupt ldb dbs any more.
(This used to be commit 944c5844ab441b96d8e5d7b2d151982139d1fab9)
2005-07-01 10:21:26 +04:00
1 ) if the dn doesn ' t start with @ then uppercase the attribute
2005-02-13 15:27:57 +03:00
names and the attributes values of case insensitive attributes
2008-06-15 04:37:40 +04:00
2 ) if the dn starts with @ then leave it alone -
the indexing code handles the rest
2004-05-01 13:45:56 +04:00
*/
2005-06-27 03:59:22 +04:00
2009-10-02 16:39:19 +04:00
dn_folded = ldb_dn_get_casefold ( dn ) ;
if ( ! dn_folded ) {
goto failed ;
}
key_str = talloc_strdup ( ldb , " DN= " ) ;
if ( ! key_str ) {
goto failed ;
2006-11-27 08:32:35 +03:00
}
2005-08-18 19:02:01 +04:00
2009-10-02 16:39:19 +04:00
key_str = talloc_strdup_append_buffer ( key_str , dn_folded ) ;
if ( ! key_str ) {
goto failed ;
}
key . dptr = ( uint8_t * ) key_str ;
key . dsize = strlen ( key_str ) + 1 ;
return key ;
failed :
errno = ENOMEM ;
key . dptr = NULL ;
key . dsize = 0 ;
return key ;
2004-03-31 10:45:39 +04:00
}
2005-05-18 01:43:47 +04:00
/*
check special dn ' s have valid attributes
currently only @ ATTRIBUTES is checked
*/
2008-10-20 20:59:51 +04:00
static int ltdb_check_special_dn ( struct ldb_module * module ,
2010-11-06 21:46:42 +03:00
const struct ldb_message * msg )
2005-05-18 01:43:47 +04:00
{
2009-01-30 02:39:30 +03:00
struct ldb_context * ldb = ldb_module_get_ctx ( module ) ;
2009-11-06 20:35:17 +03:00
unsigned int i , j ;
2008-06-15 04:37:40 +04:00
2005-08-18 19:02:01 +04:00
if ( ! ldb_dn_is_special ( msg - > dn ) | |
! ldb_dn_check_special ( msg - > dn , LTDB_ATTRIBUTES ) ) {
2009-10-06 11:30:53 +04:00
return LDB_SUCCESS ;
2005-05-18 01:43:47 +04:00
}
/* we have @ATTRIBUTES, let's check attributes are fine */
/* should we check that we deny multivalued attributes ? */
for ( i = 0 ; i < msg - > num_elements ; i + + ) {
2009-10-10 02:35:39 +04:00
if ( ldb_attr_cmp ( msg - > elements [ i ] . name , " distinguishedName " ) = = 0 ) continue ;
2005-05-18 01:43:47 +04:00
for ( j = 0 ; j < msg - > elements [ i ] . num_values ; j + + ) {
if ( ltdb_check_at_attributes_values ( & msg - > elements [ i ] . values [ j ] ) ! = 0 ) {
2009-01-30 02:39:30 +03:00
ldb_set_errstring ( ldb , " Invalid attribute value in an @ATTRIBUTES entry " ) ;
2005-09-18 22:49:06 +04:00
return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX ;
2005-05-18 01:43:47 +04:00
}
}
}
2009-10-06 11:30:53 +04:00
return LDB_SUCCESS ;
2005-05-18 01:43:47 +04:00
}
2005-05-01 16:34:12 +04:00
2004-05-01 13:45:56 +04:00
/*
2008-06-15 04:37:40 +04:00
we ' ve made a modification to a dn - possibly reindex and
2004-05-01 13:45:56 +04:00
update sequence number
*/
2006-11-22 03:59:34 +03:00
static int ltdb_modified ( struct ldb_module * module , struct ldb_dn * dn )
2004-05-01 13:45:56 +04:00
{
2007-04-23 04:36:49 +04:00
int ret = LDB_SUCCESS ;
2009-10-23 15:43:24 +04:00
struct ltdb_private * ltdb = talloc_get_type ( ldb_module_get_private ( module ) , struct ltdb_private ) ;
/* only allow modifies inside a transaction, otherwise the
* ldb is unsafe */
if ( ltdb - > in_transaction = = 0 ) {
ldb_set_errstring ( ldb_module_get_ctx ( module ) , " ltdb modify without transaction " ) ;
return LDB_ERR_OPERATIONS_ERROR ;
}
2004-05-01 13:45:56 +04:00
2005-08-18 19:02:01 +04:00
if ( ldb_dn_is_special ( dn ) & &
( ldb_dn_check_special ( dn , LTDB_INDEXLIST ) | |
ldb_dn_check_special ( dn , LTDB_ATTRIBUTES ) ) ) {
2004-11-15 14:40:27 +03:00
ret = ltdb_reindex ( module ) ;
2004-05-01 13:45:56 +04:00
}
2009-10-10 02:14:37 +04:00
/* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
2007-04-23 04:36:49 +04:00
if ( ret = = LDB_SUCCESS & &
2005-08-18 19:02:01 +04:00
! ( ldb_dn_is_special ( dn ) & &
ldb_dn_check_special ( dn , LTDB_BASEINFO ) ) ) {
2004-11-15 14:40:27 +03:00
ret = ltdb_increase_sequence_number ( module ) ;
2004-05-01 13:45:56 +04:00
}
2009-10-10 02:14:37 +04:00
/* If the modify was to @OPTIONS, reload the cache */
2010-01-07 02:11:10 +03:00
if ( ret = = LDB_SUCCESS & &
ldb_dn_is_special ( dn ) & &
2009-10-10 02:14:37 +04:00
( ldb_dn_check_special ( dn , LTDB_OPTIONS ) ) ) {
ret = ltdb_cache_reload ( module ) ;
}
2004-05-01 13:45:56 +04:00
return ret ;
}
2004-03-31 10:45:39 +04:00
/*
store a record into the db
*/
2009-10-02 16:39:19 +04:00
int ltdb_store ( struct ldb_module * module , const struct ldb_message * msg , int flgs )
2004-03-31 10:45:39 +04:00
{
2009-01-30 02:39:30 +03:00
void * data = ldb_module_get_private ( module ) ;
struct ltdb_private * ltdb = talloc_get_type ( data , struct ltdb_private ) ;
2004-03-31 10:45:39 +04:00
TDB_DATA tdb_key , tdb_data ;
2009-10-06 11:30:53 +04:00
int ret = LDB_SUCCESS ;
2004-03-31 10:45:39 +04:00
2009-10-02 16:39:19 +04:00
tdb_key = ltdb_key ( module , msg - > dn ) ;
2009-10-13 01:39:40 +04:00
if ( tdb_key . dptr = = NULL ) {
2005-09-18 22:49:06 +04:00
return LDB_ERR_OTHER ;
2004-03-31 10:45:39 +04:00
}
2004-11-16 12:00:52 +03:00
ret = ltdb_pack_data ( module , msg , & tdb_data ) ;
2008-09-25 01:59:59 +04:00
if ( ret = = - 1 ) {
2005-01-02 10:49:29 +03:00
talloc_free ( tdb_key . dptr ) ;
2005-09-18 22:49:06 +04:00
return LDB_ERR_OTHER ;
2004-03-31 10:45:39 +04:00
}
ret = tdb_store ( ltdb - > tdb , tdb_key , tdb_data , flgs ) ;
2011-06-20 13:10:31 +04:00
if ( ret ! = 0 ) {
2005-10-28 07:43:39 +04:00
ret = ltdb_err_map ( tdb_error ( ltdb - > tdb ) ) ;
2004-03-31 10:45:39 +04:00
goto done ;
}
2008-06-15 04:37:40 +04:00
2004-03-31 10:45:39 +04:00
done :
2005-01-02 10:49:29 +03:00
talloc_free ( tdb_key . dptr ) ;
talloc_free ( tdb_data . dptr ) ;
2004-03-31 10:45:39 +04:00
return ret ;
}
2011-02-14 02:07:21 +03:00
/*
check if a attribute is a single valued , for a given element
*/
static bool ldb_tdb_single_valued ( const struct ldb_schema_attribute * a ,
struct ldb_message_element * el )
{
if ( ! a ) return false ;
if ( el ! = NULL ) {
2011-02-14 02:08:24 +03:00
if ( el - > flags & LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK ) {
/* override from a ldb module, for example
used for the description field , which is
marked multi - valued in the schema but which
should not actually accept multiple
values */
return true ;
}
2011-02-14 02:07:21 +03:00
if ( el - > flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK ) {
/* override from a ldb module, for example used for
deleted linked attribute entries */
return false ;
}
}
if ( a - > flags & LDB_ATTR_FLAG_SINGLE_VALUE ) {
return true ;
}
return false ;
}
2008-06-15 04:37:40 +04:00
static int ltdb_add_internal ( struct ldb_module * module ,
const struct ldb_message * msg )
2006-05-30 03:46:43 +04:00
{
2009-01-30 02:39:30 +03:00
struct ldb_context * ldb = ldb_module_get_ctx ( module ) ;
2009-11-06 20:35:17 +03:00
int ret = LDB_SUCCESS ;
unsigned int i ;
2008-06-15 04:37:40 +04:00
2009-09-22 06:26:59 +04:00
for ( i = 0 ; i < msg - > num_elements ; i + + ) {
struct ldb_message_element * el = & msg - > elements [ i ] ;
2010-11-04 12:02:16 +03:00
const struct ldb_schema_attribute * a = ldb_schema_attribute_by_name ( ldb , el - > name ) ;
2009-09-22 06:26:59 +04:00
if ( el - > num_values = = 0 ) {
2010-10-18 22:07:49 +04:00
ldb_asprintf_errstring ( ldb , " attribute '%s' on '%s' specified, but with 0 values (illegal) " ,
2009-09-22 06:26:59 +04:00
el - > name , ldb_dn_get_linearized ( msg - > dn ) ) ;
2009-10-13 01:39:40 +04:00
return LDB_ERR_CONSTRAINT_VIOLATION ;
2009-09-22 06:26:59 +04:00
}
2011-02-14 02:07:21 +03:00
if ( el - > num_values > 1 & & ldb_tdb_single_valued ( a , el ) ) {
ldb_asprintf_errstring ( ldb , " SINGLE-VALUE attribute %s on %s specified more than once " ,
el - > name , ldb_dn_get_linearized ( msg - > dn ) ) ;
return LDB_ERR_CONSTRAINT_VIOLATION ;
2010-11-04 12:02:16 +03:00
}
2009-09-22 06:26:59 +04:00
}
2009-10-02 16:39:19 +04:00
ret = ltdb_store ( module , msg , TDB_INSERT ) ;
2009-10-06 11:30:53 +04:00
if ( ret ! = LDB_SUCCESS ) {
if ( ret = = LDB_ERR_ENTRY_ALREADY_EXISTS ) {
ldb_asprintf_errstring ( ldb ,
" Entry %s already exists " ,
ldb_dn_get_linearized ( msg - > dn ) ) ;
}
2009-10-13 01:39:40 +04:00
return ret ;
2006-08-01 07:22:02 +04:00
}
2008-06-15 04:37:40 +04:00
2009-10-21 15:21:26 +04:00
ret = ltdb_index_add_new ( module , msg ) ;
2009-10-06 11:30:53 +04:00
if ( ret ! = LDB_SUCCESS ) {
2009-10-13 01:39:40 +04:00
return ret ;
2009-10-06 11:30:53 +04:00
}
2006-12-11 18:49:39 +03:00
2009-10-06 11:30:53 +04:00
ret = ltdb_modified ( module , msg - > dn ) ;
2006-08-01 07:22:02 +04:00
2006-08-01 06:25:05 +04:00
return ret ;
2006-05-30 03:46:43 +04:00
}
2004-03-31 10:45:39 +04:00
/*
add a record to the database
*/
2008-09-12 02:34:11 +04:00
static int ltdb_add ( struct ltdb_context * ctx )
2004-03-31 10:45:39 +04:00
{
2008-09-12 02:34:11 +04:00
struct ldb_module * module = ctx - > module ;
struct ldb_request * req = ctx - > req ;
2009-10-06 11:30:53 +04:00
int ret = LDB_SUCCESS ;
2008-06-15 04:37:40 +04:00
2010-11-06 21:38:47 +03:00
ret = ltdb_check_special_dn ( module , req - > op . add . message ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
2009-01-30 02:39:30 +03:00
ldb_request_set_state ( req , LDB_ASYNC_PENDING ) ;
2004-04-03 16:29:21 +04:00
2009-10-06 11:30:53 +04:00
if ( ltdb_cache_load ( module ) ! = 0 ) {
return LDB_ERR_OPERATIONS_ERROR ;
2005-05-18 01:43:47 +04:00
}
2008-06-15 04:37:40 +04:00
2009-10-06 11:30:53 +04:00
ret = ltdb_add_internal ( module , req - > op . add . message ) ;
return ret ;
2004-03-31 10:45:39 +04:00
}
/*
delete a record from the database , not updating indexes ( used for deleting
index records )
*/
2009-10-22 04:06:33 +04:00
int ltdb_delete_noindex ( struct ldb_module * module , struct ldb_dn * dn )
2004-03-31 10:45:39 +04:00
{
2009-01-30 02:39:30 +03:00
void * data = ldb_module_get_private ( module ) ;
struct ltdb_private * ltdb = talloc_get_type ( data , struct ltdb_private ) ;
2004-03-31 10:45:39 +04:00
TDB_DATA tdb_key ;
int ret ;
2009-10-02 16:39:19 +04:00
tdb_key = ltdb_key ( module , dn ) ;
2004-03-31 10:45:39 +04:00
if ( ! tdb_key . dptr ) {
2005-09-18 22:49:06 +04:00
return LDB_ERR_OTHER ;
2004-03-31 10:45:39 +04:00
}
ret = tdb_delete ( ltdb - > tdb , tdb_key ) ;
2005-01-02 10:49:29 +03:00
talloc_free ( tdb_key . dptr ) ;
2004-03-31 10:45:39 +04:00
2005-10-28 07:43:39 +04:00
if ( ret ! = 0 ) {
ret = ltdb_err_map ( tdb_error ( ltdb - > tdb ) ) ;
}
2005-09-18 22:49:06 +04:00
2004-03-31 10:45:39 +04:00
return ret ;
}
2009-10-02 16:39:19 +04:00
static int ltdb_delete_internal ( struct ldb_module * module , struct ldb_dn * dn )
2006-05-30 03:46:43 +04:00
{
struct ldb_message * msg ;
2009-10-06 11:30:53 +04:00
int ret = LDB_SUCCESS ;
2006-05-30 03:46:43 +04:00
2010-10-20 15:53:14 +04:00
msg = ldb_msg_new ( module ) ;
2006-05-30 03:46:43 +04:00
if ( msg = = NULL ) {
return LDB_ERR_OPERATIONS_ERROR ;
}
/* in case any attribute of the message was indexed, we need
to fetch the old record */
ret = ltdb_search_dn1 ( module , dn , msg ) ;
2007-04-23 04:36:49 +04:00
if ( ret ! = LDB_SUCCESS ) {
2006-05-30 03:46:43 +04:00
/* not finding the old record is an error */
2007-04-23 04:36:49 +04:00
goto done ;
2006-05-30 03:46:43 +04:00
}
2009-10-02 16:39:19 +04:00
ret = ltdb_delete_noindex ( module , dn ) ;
2006-05-30 03:46:43 +04:00
if ( ret ! = LDB_SUCCESS ) {
2007-04-23 04:36:49 +04:00
goto done ;
2006-05-30 03:46:43 +04:00
}
/* remove any indexed attributes */
2009-10-21 15:21:26 +04:00
ret = ltdb_index_delete ( module , msg ) ;
2006-05-30 03:46:43 +04:00
if ( ret ! = LDB_SUCCESS ) {
2007-04-23 04:36:49 +04:00
goto done ;
2006-05-30 03:46:43 +04:00
}
2006-05-30 05:46:14 +04:00
ret = ltdb_modified ( module , dn ) ;
if ( ret ! = LDB_SUCCESS ) {
2007-04-23 04:36:49 +04:00
goto done ;
2006-05-30 05:46:14 +04:00
}
2006-05-30 03:46:43 +04:00
2007-04-23 04:36:49 +04:00
done :
2006-05-30 03:46:43 +04:00
talloc_free ( msg ) ;
2007-04-23 04:36:49 +04:00
return ret ;
2006-05-30 03:46:43 +04:00
}
2004-03-31 10:45:39 +04:00
/*
delete a record from the database
*/
2008-09-12 02:34:11 +04:00
static int ltdb_delete ( struct ltdb_context * ctx )
2004-03-31 10:45:39 +04:00
{
2008-09-12 02:34:11 +04:00
struct ldb_module * module = ctx - > module ;
struct ldb_request * req = ctx - > req ;
2009-10-06 11:30:53 +04:00
int ret = LDB_SUCCESS ;
2008-06-15 04:37:40 +04:00
2009-01-30 02:39:30 +03:00
ldb_request_set_state ( req , LDB_ASYNC_PENDING ) ;
2004-05-06 08:40:15 +04:00
2004-11-15 14:40:27 +03:00
if ( ltdb_cache_load ( module ) ! = 0 ) {
2006-05-28 06:10:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2005-01-02 10:49:29 +03:00
}
2009-10-06 11:30:53 +04:00
ret = ltdb_delete_internal ( module , req - > op . del . dn ) ;
2004-04-23 17:05:27 +04:00
2009-10-06 11:30:53 +04:00
return ret ;
2006-03-03 20:44:03 +03:00
}
2004-04-03 16:29:21 +04:00
/*
2008-06-15 04:37:40 +04:00
find an element by attribute name . At the moment this does a linear search ,
it should be re - coded to use a binary search once all places that modify
records guarantee sorted order
2004-04-03 16:29:21 +04:00
return the index of the first matching element if found , otherwise - 1
*/
static int find_element ( const struct ldb_message * msg , const char * name )
{
2004-07-07 05:02:54 +04:00
unsigned int i ;
2004-04-03 16:29:21 +04:00
for ( i = 0 ; i < msg - > num_elements ; i + + ) {
2004-05-01 13:45:56 +04:00
if ( ldb_attr_cmp ( msg - > elements [ i ] . name , name ) = = 0 ) {
2004-04-03 16:29:21 +04:00
return i ;
}
}
return - 1 ;
}
/*
add an element to an existing record . Assumes a elements array that we
2008-06-15 04:37:40 +04:00
can call re - alloc on , and assumed that we can re - use the data pointers from
the passed in additional values . Use with care !
2004-04-03 16:29:21 +04:00
returns 0 on success , - 1 on failure ( and sets errno )
*/
2009-10-21 15:21:26 +04:00
static int ltdb_msg_add_element ( struct ldb_context * ldb ,
struct ldb_message * msg ,
struct ldb_message_element * el )
2004-04-03 16:29:21 +04:00
{
struct ldb_message_element * e2 ;
2004-07-07 05:02:54 +04:00
unsigned int i ;
2004-04-03 16:29:21 +04:00
2009-10-06 11:30:53 +04:00
if ( el - > num_values = = 0 ) {
/* nothing to do here - we don't add empty elements */
return 0 ;
}
2008-06-15 04:37:40 +04:00
e2 = talloc_realloc ( msg , msg - > elements , struct ldb_message_element ,
2005-01-02 10:49:29 +03:00
msg - > num_elements + 1 ) ;
2004-04-03 16:29:21 +04:00
if ( ! e2 ) {
errno = ENOMEM ;
return - 1 ;
}
msg - > elements = e2 ;
e2 = & msg - > elements [ msg - > num_elements ] ;
e2 - > name = el - > name ;
e2 - > flags = el - > flags ;
2009-10-06 11:30:53 +04:00
e2 - > values = talloc_array ( msg - > elements ,
struct ldb_val , el - > num_values ) ;
if ( ! e2 - > values ) {
errno = ENOMEM ;
return - 1 ;
2004-04-03 16:29:21 +04:00
}
for ( i = 0 ; i < el - > num_values ; i + + ) {
e2 - > values [ i ] = el - > values [ i ] ;
}
e2 - > num_values = el - > num_values ;
2009-10-06 11:30:53 +04:00
+ + msg - > num_elements ;
2004-04-03 16:29:21 +04:00
return 0 ;
}
/*
delete all elements having a specified attribute name
*/
2004-12-19 13:56:29 +03:00
static int msg_delete_attribute ( struct ldb_module * module ,
2009-10-02 16:39:19 +04:00
struct ldb_context * ldb ,
2004-05-06 08:40:15 +04:00
struct ldb_message * msg , const char * name )
2004-04-03 16:29:21 +04:00
{
2009-10-21 15:21:26 +04:00
unsigned int i ;
int ret ;
struct ldb_message_element * el ;
2004-04-03 16:29:21 +04:00
2009-10-21 15:21:26 +04:00
el = ldb_msg_find_element ( msg , name ) ;
if ( el = = NULL ) {
2010-01-30 08:33:22 +03:00
return LDB_ERR_NO_SUCH_ATTRIBUTE ;
2009-10-21 15:21:26 +04:00
}
i = el - msg - > elements ;
2009-10-06 11:30:53 +04:00
2009-11-18 12:44:56 +03:00
ret = ltdb_index_del_element ( module , msg - > dn , el ) ;
2009-10-21 15:21:26 +04:00
if ( ret ! = LDB_SUCCESS ) {
return ret ;
2004-04-03 16:29:21 +04:00
}
2009-10-21 15:21:26 +04:00
talloc_free ( el - > values ) ;
if ( msg - > num_elements > ( i + 1 ) ) {
memmove ( el , el + 1 , sizeof ( * el ) * ( msg - > num_elements - ( i + 1 ) ) ) ;
}
msg - > num_elements - - ;
msg - > elements = talloc_realloc ( msg , msg - > elements ,
struct ldb_message_element ,
msg - > num_elements ) ;
2010-01-30 08:33:22 +03:00
return LDB_SUCCESS ;
2004-03-31 10:45:39 +04:00
}
2004-04-03 16:29:21 +04:00
/*
2008-06-15 04:37:40 +04:00
delete all elements matching an attribute name / value
2004-04-03 16:29:21 +04:00
2010-01-30 08:33:22 +03:00
return LDB Error on failure
2004-04-03 16:29:21 +04:00
*/
2004-11-15 14:40:27 +03:00
static int msg_delete_element ( struct ldb_module * module ,
2008-06-15 04:37:40 +04:00
struct ldb_message * msg ,
2004-04-03 16:29:21 +04:00
const char * name ,
const struct ldb_val * val )
{
2009-01-30 02:39:30 +03:00
struct ldb_context * ldb = ldb_module_get_ctx ( module ) ;
2004-07-07 05:02:54 +04:00
unsigned int i ;
2009-10-21 15:21:26 +04:00
int found , ret ;
2004-04-03 16:29:21 +04:00
struct ldb_message_element * el ;
2006-12-15 16:08:57 +03:00
const struct ldb_schema_attribute * a ;
2004-04-03 16:29:21 +04:00
2004-07-07 05:02:54 +04:00
found = find_element ( msg , name ) ;
if ( found = = - 1 ) {
2010-01-30 08:33:22 +03:00
return LDB_ERR_NO_SUCH_ATTRIBUTE ;
2004-04-03 16:29:21 +04:00
}
2010-10-18 22:10:17 +04:00
i = ( unsigned int ) found ;
el = & ( msg - > elements [ i ] ) ;
2004-04-03 16:29:21 +04:00
2006-12-15 16:08:57 +03:00
a = ldb_schema_attribute_by_name ( ldb , el - > name ) ;
r8037: a fairly major update to the internals of ldb. Changes are:
- moved the knowledge of attribute types out of ldb_tdb and into the
generic ldb code. This allows the ldb_match() message match logic
to be generic, so it can be used by other backend
- added the generic ability to load attribute handlers, for
canonicalisation, compare, ldif read and ldif write. In the future
this will be used by the schema module to allow us to correctly
obey the attributetype schema elements
- added attribute handlers for some of the core ldap attribute types,
Integer, DirectoryString, DN, ObjectClass etc
- added automatic registration of attribute handlers for well-known
attribute names 'cn', 'dc', 'dn', 'ou' and 'objectClass'
- converted the objectSid special handlers for Samba to the new system
- added more correct handling of indexing in tdb backend based on the
attribute canonicalisation function
- added generic support for subclasses, moving it out of the tdb
backend. This will be used in future by the schema module
- fixed several bugs in the dn_explode code. It still needs more
work, but doesn't corrupt ldb dbs any more.
(This used to be commit 944c5844ab441b96d8e5d7b2d151982139d1fab9)
2005-07-01 10:21:26 +04:00
2004-04-03 16:29:21 +04:00
for ( i = 0 ; i < el - > num_values ; i + + ) {
2010-10-13 13:02:18 +04:00
bool matched ;
if ( a - > syntax - > operator_fn ) {
ret = a - > syntax - > operator_fn ( ldb , LDB_OP_EQUALITY , a ,
& el - > values [ i ] , val , & matched ) ;
if ( ret ! = LDB_SUCCESS ) return ret ;
} else {
matched = ( a - > syntax - > comparison_fn ( ldb , ldb ,
& el - > values [ i ] , val ) = = 0 ) ;
}
if ( matched ) {
2009-10-21 15:21:26 +04:00
if ( el - > num_values = = 1 ) {
return msg_delete_attribute ( module , ldb , msg , name ) ;
}
2009-11-18 12:44:56 +03:00
ret = ltdb_index_del_value ( module , msg - > dn , el , i ) ;
2009-10-21 15:21:26 +04:00
if ( ret ! = LDB_SUCCESS ) {
2010-01-30 08:33:22 +03:00
return ret ;
2009-10-21 15:21:26 +04:00
}
2004-04-03 16:29:21 +04:00
if ( i < el - > num_values - 1 ) {
memmove ( & el - > values [ i ] , & el - > values [ i + 1 ] ,
2008-06-15 04:37:40 +04:00
sizeof ( el - > values [ i ] ) *
( el - > num_values - ( i + 1 ) ) ) ;
2004-04-03 16:29:21 +04:00
}
el - > num_values - - ;
2009-10-06 11:30:53 +04:00
/* per definition we find in a canonicalised message an
attribute value only once . So we are finished here */
2010-01-30 08:33:22 +03:00
return LDB_SUCCESS ;
2004-04-03 16:29:21 +04:00
}
}
2004-05-08 03:54:41 +04:00
2009-10-06 11:30:53 +04:00
/* Not found */
2010-01-30 08:33:22 +03:00
return LDB_ERR_NO_SUCH_ATTRIBUTE ;
2004-04-03 16:29:21 +04:00
}
2004-03-31 10:45:39 +04:00
2004-05-01 13:45:56 +04:00
2004-03-31 10:45:39 +04:00
/*
2004-05-01 13:45:56 +04:00
modify a record - internal interface
2004-04-03 16:29:21 +04:00
yuck - this is O ( n ^ 2 ) . Luckily n is usually small so we probably
2008-06-15 04:37:40 +04:00
get away with it , but if we ever have really large attribute lists
2004-04-03 16:29:21 +04:00
then we ' ll need to look at this again
2009-12-19 12:55:11 +03:00
' req ' is optional , and is used to specify controls if supplied
2004-03-31 10:45:39 +04:00
*/
2008-06-15 04:37:40 +04:00
int ltdb_modify_internal ( struct ldb_module * module ,
2009-12-19 12:55:11 +03:00
const struct ldb_message * msg ,
struct ldb_request * req )
2004-03-31 10:45:39 +04:00
{
2009-01-30 02:39:30 +03:00
struct ldb_context * ldb = ldb_module_get_ctx ( module ) ;
void * data = ldb_module_get_private ( module ) ;
struct ltdb_private * ltdb = talloc_get_type ( data , struct ltdb_private ) ;
2004-03-31 10:45:39 +04:00
TDB_DATA tdb_key , tdb_data ;
2005-01-02 10:49:29 +03:00
struct ldb_message * msg2 ;
2010-10-18 22:10:17 +04:00
unsigned int i , j , k ;
2009-10-06 11:30:53 +04:00
int ret = LDB_SUCCESS , idx ;
2010-01-30 08:33:22 +03:00
struct ldb_control * control_permissive = NULL ;
if ( req ) {
control_permissive = ldb_request_get_control ( req ,
LDB_CONTROL_PERMISSIVE_MODIFY_OID ) ;
}
2009-10-02 16:39:19 +04:00
tdb_key = ltdb_key ( module , msg - > dn ) ;
2004-03-31 10:45:39 +04:00
if ( ! tdb_key . dptr ) {
2005-09-18 22:49:06 +04:00
return LDB_ERR_OTHER ;
2004-03-31 10:45:39 +04:00
}
2011-06-20 13:10:31 +04:00
tdb_data = tdb_fetch_compat ( ltdb - > tdb , tdb_key ) ;
2004-03-31 10:45:39 +04:00
if ( ! tdb_data . dptr ) {
2009-10-02 16:39:19 +04:00
talloc_free ( tdb_key . dptr ) ;
2005-10-28 07:43:39 +04:00
return ltdb_err_map ( tdb_error ( ltdb - > tdb ) ) ;
2005-01-02 10:49:29 +03:00
}
2010-10-20 15:53:14 +04:00
msg2 = ldb_msg_new ( tdb_key . dptr ) ;
2005-01-02 10:49:29 +03:00
if ( msg2 = = NULL ) {
2009-10-03 17:37:25 +04:00
free ( tdb_data . dptr ) ;
2009-10-06 11:30:53 +04:00
ret = LDB_ERR_OTHER ;
goto done ;
2004-03-31 10:45:39 +04:00
}
2005-01-02 10:49:29 +03:00
ret = ltdb_unpack_data ( module , & tdb_data , msg2 ) ;
2009-10-03 17:37:25 +04:00
free ( tdb_data . dptr ) ;
2008-09-25 01:59:59 +04:00
if ( ret = = - 1 ) {
2009-10-02 16:39:19 +04:00
ret = LDB_ERR_OTHER ;
2009-10-06 11:30:53 +04:00
goto done ;
2004-03-31 10:45:39 +04:00
}
2005-01-02 10:49:29 +03:00
if ( ! msg2 - > dn ) {
msg2 - > dn = msg - > dn ;
2004-05-01 13:45:56 +04:00
}
2004-04-03 16:29:21 +04:00
2009-10-06 11:30:53 +04:00
for ( i = 0 ; i < msg - > num_elements ; i + + ) {
struct ldb_message_element * el = & msg - > elements [ i ] , * el2 ;
2004-12-26 20:30:27 +03:00
struct ldb_val * vals ;
2010-11-04 12:02:16 +03:00
const struct ldb_schema_attribute * a = ldb_schema_attribute_by_name ( ldb , el - > name ) ;
2009-10-06 11:30:53 +04:00
const char * dn ;
2004-04-03 16:29:21 +04:00
2009-09-25 04:20:55 +04:00
switch ( msg - > elements [ i ] . flags & LDB_FLAG_MOD_MASK ) {
2004-03-31 10:45:39 +04:00
case LDB_FLAG_MOD_ADD :
2010-01-30 08:33:22 +03:00
2010-01-30 10:57:33 +03:00
if ( el - > num_values = = 0 ) {
2010-10-18 22:07:49 +04:00
ldb_asprintf_errstring ( ldb ,
" attribute '%s': attribute on '%s' specified, but with 0 values (illegal) " ,
2010-01-30 10:57:33 +03:00
el - > name , ldb_dn_get_linearized ( msg2 - > dn ) ) ;
ret = LDB_ERR_CONSTRAINT_VIOLATION ;
goto done ;
}
2010-01-30 08:33:22 +03:00
/* make a copy of the array so that a permissive
* control can remove duplicates without changing the
* original values , but do not copy data as we do not
* need to keep it around once the operation is
* finished */
if ( control_permissive ) {
el = talloc ( msg2 , struct ldb_message_element ) ;
if ( ! el ) {
ret = LDB_ERR_OTHER ;
goto done ;
}
2011-02-14 05:08:44 +03:00
* el = msg - > elements [ i ] ;
2010-01-30 08:33:22 +03:00
el - > values = talloc_array ( el , struct ldb_val , el - > num_values ) ;
2010-01-30 10:57:33 +03:00
if ( el - > values = = NULL ) {
ret = LDB_ERR_OTHER ;
goto done ;
}
2010-01-30 08:33:22 +03:00
for ( j = 0 ; j < el - > num_values ; j + + ) {
el - > values [ j ] = msg - > elements [ i ] . values [ j ] ;
}
2004-12-26 20:30:27 +03:00
}
2011-02-14 02:07:21 +03:00
if ( el - > num_values > 1 & & ldb_tdb_single_valued ( a , el ) ) {
ldb_asprintf_errstring ( ldb , " SINGLE-VALUE attribute %s on %s specified more than once " ,
el - > name , ldb_dn_get_linearized ( msg2 - > dn ) ) ;
ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS ;
goto done ;
2010-11-04 12:02:16 +03:00
}
2009-10-06 11:30:53 +04:00
/* Checks if element already exists */
idx = find_element ( msg2 , el - > name ) ;
if ( idx = = - 1 ) {
2009-10-21 15:21:26 +04:00
if ( ltdb_msg_add_element ( ldb , msg2 , el ) ! = 0 ) {
2009-10-06 11:30:53 +04:00
ret = LDB_ERR_OTHER ;
goto done ;
2007-07-07 08:34:36 +04:00
}
2010-10-18 22:07:49 +04:00
ret = ltdb_index_add_element ( module , msg2 - > dn ,
el ) ;
2009-10-21 15:21:26 +04:00
if ( ret ! = LDB_SUCCESS ) {
goto done ;
}
2009-10-06 11:30:53 +04:00
} else {
2010-10-18 22:10:17 +04:00
j = ( unsigned int ) idx ;
el2 = & ( msg2 - > elements [ j ] ) ;
2004-12-26 20:30:27 +03:00
2010-11-04 12:02:16 +03:00
/* We cannot add another value on a existing one
if the attribute is single - valued */
2011-02-14 02:07:21 +03:00
if ( ldb_tdb_single_valued ( a , el ) ) {
2010-11-04 12:02:16 +03:00
ldb_asprintf_errstring ( ldb , " SINGLE-VALUE attribute %s on %s specified more than once " ,
el - > name , ldb_dn_get_linearized ( msg2 - > dn ) ) ;
ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS ;
goto done ;
}
2009-10-06 11:30:53 +04:00
/* Check that values don't exist yet on multi-
valued attributes or aren ' t provided twice */
2010-01-30 08:33:22 +03:00
for ( j = 0 ; j < el - > num_values ; j + + ) {
2009-10-06 11:30:53 +04:00
if ( ldb_msg_find_val ( el2 , & el - > values [ j ] ) ! = NULL ) {
2010-01-30 08:33:22 +03:00
if ( control_permissive ) {
/* remove this one as if it was never added */
el - > num_values - - ;
for ( k = j ; k < el - > num_values ; k + + ) {
el - > values [ k ] = el - > values [ k + 1 ] ;
}
j - - ; /* rewind */
continue ;
}
2010-10-18 22:07:49 +04:00
ldb_asprintf_errstring ( ldb ,
" attribute '%s': value #%u on '%s' already exists " ,
el - > name , j , ldb_dn_get_linearized ( msg2 - > dn ) ) ;
2009-10-06 11:30:53 +04:00
ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS ;
goto done ;
}
if ( ldb_msg_find_val ( el , & el - > values [ j ] ) ! = & el - > values [ j ] ) {
2010-10-18 22:07:49 +04:00
ldb_asprintf_errstring ( ldb ,
" attribute '%s': value #%u on '%s' provided more than once " ,
el - > name , j , ldb_dn_get_linearized ( msg2 - > dn ) ) ;
2009-10-06 11:30:53 +04:00
ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS ;
goto done ;
}
}
2004-12-26 20:30:27 +03:00
2009-10-06 11:30:53 +04:00
/* Now combine existing and new values to a new
attribute record */
vals = talloc_realloc ( msg2 - > elements ,
2009-10-21 15:21:26 +04:00
el2 - > values , struct ldb_val ,
el2 - > num_values + el - > num_values ) ;
2009-10-06 11:30:53 +04:00
if ( vals = = NULL ) {
ldb_oom ( ldb ) ;
ret = LDB_ERR_OTHER ;
goto done ;
}
2004-12-26 20:30:27 +03:00
2009-10-06 11:30:53 +04:00
for ( j = 0 ; j < el - > num_values ; j + + ) {
vals [ el2 - > num_values + j ] =
ldb_val_dup ( vals , & el - > values [ j ] ) ;
}
el2 - > values = vals ;
el2 - > num_values + = el - > num_values ;
2009-10-21 15:21:26 +04:00
2009-11-18 13:56:24 +03:00
ret = ltdb_index_add_element ( module , msg2 - > dn , el ) ;
2009-10-21 15:21:26 +04:00
if ( ret ! = LDB_SUCCESS ) {
goto done ;
}
2009-10-06 11:30:53 +04:00
}
2004-12-26 20:30:27 +03:00
2004-04-03 16:29:21 +04:00
break ;
case LDB_FLAG_MOD_REPLACE :
2010-01-30 08:33:22 +03:00
2011-02-14 02:07:21 +03:00
if ( el - > num_values > 1 & & ldb_tdb_single_valued ( a , el ) ) {
ldb_asprintf_errstring ( ldb , " SINGLE-VALUE attribute %s on %s specified more than once " ,
el - > name , ldb_dn_get_linearized ( msg2 - > dn ) ) ;
ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS ;
goto done ;
2010-11-04 12:02:16 +03:00
}
2009-10-22 04:06:33 +04:00
/* TODO: This is O(n^2) - replace with more efficient check */
2009-10-06 11:30:53 +04:00
for ( j = 0 ; j < el - > num_values ; j + + ) {
2007-07-07 08:34:36 +04:00
if ( ldb_msg_find_val ( el , & el - > values [ j ] ) ! = & el - > values [ j ] ) {
2010-10-18 22:07:49 +04:00
ldb_asprintf_errstring ( ldb ,
" attribute '%s': value #%u on '%s' provided more than once " ,
el - > name , j , ldb_dn_get_linearized ( msg2 - > dn ) ) ;
2007-07-07 08:34:36 +04:00
ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS ;
2009-10-06 11:30:53 +04:00
goto done ;
2007-07-07 08:34:36 +04:00
}
}
2004-05-22 04:52:04 +04:00
2010-10-18 22:07:49 +04:00
/* Checks if element already exists */
2009-10-21 15:21:26 +04:00
idx = find_element ( msg2 , el - > name ) ;
if ( idx ! = - 1 ) {
2010-10-18 22:10:17 +04:00
j = ( unsigned int ) idx ;
el2 = & ( msg2 - > elements [ j ] ) ;
2009-10-21 15:21:26 +04:00
if ( ldb_msg_element_compare ( el , el2 ) = = 0 ) {
/* we are replacing with the same values */
continue ;
}
/* Delete the attribute if it exists in the DB */
2010-10-18 22:07:49 +04:00
if ( msg_delete_attribute ( module , ldb , msg2 ,
el - > name ) ! = 0 ) {
2009-10-27 21:40:57 +03:00
ret = LDB_ERR_OTHER ;
2009-10-21 15:21:26 +04:00
goto done ;
}
}
2009-10-06 11:30:53 +04:00
/* Recreate it with the new values */
2009-10-21 15:21:26 +04:00
if ( ltdb_msg_add_element ( ldb , msg2 , el ) ! = 0 ) {
2006-05-31 14:36:48 +04:00
ret = LDB_ERR_OTHER ;
2009-10-06 11:30:53 +04:00
goto done ;
2004-04-03 16:29:21 +04:00
}
2009-10-06 11:30:53 +04:00
2009-11-18 13:56:24 +03:00
ret = ltdb_index_add_element ( module , msg2 - > dn , el ) ;
2009-10-21 15:21:26 +04:00
if ( ret ! = LDB_SUCCESS ) {
goto done ;
}
2004-04-03 16:29:21 +04:00
break ;
case LDB_FLAG_MOD_DELETE :
2009-11-18 13:56:24 +03:00
dn = ldb_dn_get_linearized ( msg2 - > dn ) ;
2009-10-02 16:39:19 +04:00
if ( dn = = NULL ) {
ret = LDB_ERR_OTHER ;
2009-10-06 11:30:53 +04:00
goto done ;
2009-10-02 16:39:19 +04:00
}
2004-04-03 16:29:21 +04:00
if ( msg - > elements [ i ] . num_values = = 0 ) {
2009-10-06 11:30:53 +04:00
/* Delete the whole attribute */
2010-01-30 08:33:22 +03:00
ret = msg_delete_attribute ( module , ldb , msg2 ,
msg - > elements [ i ] . name ) ;
if ( ret = = LDB_ERR_NO_SUCH_ATTRIBUTE & &
control_permissive ) {
ret = LDB_SUCCESS ;
} else {
2010-10-18 22:07:49 +04:00
ldb_asprintf_errstring ( ldb ,
" attribute '%s': no such attribute for delete on '%s' " ,
2009-10-06 11:30:53 +04:00
msg - > elements [ i ] . name , dn ) ;
2010-01-30 08:33:22 +03:00
}
if ( ret ! = LDB_SUCCESS ) {
2009-10-06 11:30:53 +04:00
goto done ;
2004-04-03 16:29:21 +04:00
}
2009-10-06 11:30:53 +04:00
} else {
/* Delete specified values from an attribute */
for ( j = 0 ; j < msg - > elements [ i ] . num_values ; j + + ) {
2010-01-30 08:33:22 +03:00
ret = msg_delete_element ( module ,
msg2 ,
msg - > elements [ i ] . name ,
& msg - > elements [ i ] . values [ j ] ) ;
if ( ret = = LDB_ERR_NO_SUCH_ATTRIBUTE & &
control_permissive ) {
ret = LDB_SUCCESS ;
} else {
2010-10-18 22:07:49 +04:00
ldb_asprintf_errstring ( ldb ,
" attribute '%s': no matching attribute value while deleting attribute on '%s' " ,
2009-10-06 11:30:53 +04:00
msg - > elements [ i ] . name , dn ) ;
2010-01-30 08:33:22 +03:00
}
if ( ret ! = LDB_SUCCESS ) {
2009-10-06 11:30:53 +04:00
goto done ;
}
2004-12-19 13:56:29 +03:00
}
2004-04-03 16:29:21 +04:00
}
break ;
2005-06-20 12:50:53 +04:00
default :
2009-01-30 02:39:30 +03:00
ldb_asprintf_errstring ( ldb ,
2010-10-18 22:07:49 +04:00
" attribute '%s': invalid modify flags on '%s': 0x%x " ,
msg - > elements [ i ] . name , ldb_dn_get_linearized ( msg - > dn ) ,
msg - > elements [ i ] . flags & LDB_FLAG_MOD_MASK ) ;
2005-09-18 22:49:06 +04:00
ret = LDB_ERR_PROTOCOL_ERROR ;
2009-10-06 11:30:53 +04:00
goto done ;
2004-03-31 10:45:39 +04:00
}
}
2009-10-02 16:39:19 +04:00
ret = ltdb_store ( module , msg2 , TDB_MODIFY ) ;
2006-05-30 05:46:14 +04:00
if ( ret ! = LDB_SUCCESS ) {
2009-10-06 11:30:53 +04:00
goto done ;
2006-05-30 05:46:14 +04:00
}
2009-11-18 13:56:24 +03:00
ret = ltdb_modified ( module , msg2 - > dn ) ;
2007-04-23 04:36:49 +04:00
if ( ret ! = LDB_SUCCESS ) {
2009-10-06 11:30:53 +04:00
goto done ;
2006-05-30 05:46:14 +04:00
}
2004-04-03 16:29:21 +04:00
2009-10-06 11:30:53 +04:00
done :
2009-10-02 16:39:19 +04:00
talloc_free ( tdb_key . dptr ) ;
2005-09-18 22:49:06 +04:00
return ret ;
2004-05-01 13:45:56 +04:00
}
/*
modify a record
*/
2008-09-12 02:34:11 +04:00
static int ltdb_modify ( struct ltdb_context * ctx )
2004-05-01 13:45:56 +04:00
{
2008-09-12 02:34:11 +04:00
struct ldb_module * module = ctx - > module ;
struct ldb_request * req = ctx - > req ;
2009-10-06 11:30:53 +04:00
int ret = LDB_SUCCESS ;
2004-05-01 13:45:56 +04:00
2009-10-06 11:30:53 +04:00
ret = ltdb_check_special_dn ( module , req - > op . mod . message ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
2005-05-18 01:43:47 +04:00
}
2008-06-15 04:37:40 +04:00
2009-10-06 11:30:53 +04:00
ldb_request_set_state ( req , LDB_ASYNC_PENDING ) ;
2004-11-15 14:40:27 +03:00
if ( ltdb_cache_load ( module ) ! = 0 ) {
2008-09-12 02:34:11 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2004-05-01 13:45:56 +04:00
}
2009-12-19 12:55:11 +03:00
ret = ltdb_modify_internal ( module , req - > op . mod . message , req ) ;
2004-04-03 16:29:21 +04:00
2009-10-06 11:30:53 +04:00
return ret ;
2006-03-03 20:44:03 +03:00
}
2004-10-20 23:28:02 +04:00
/*
rename a record
*/
2008-09-12 02:34:11 +04:00
static int ltdb_rename ( struct ltdb_context * ctx )
2004-10-20 23:28:02 +04:00
{
2008-09-12 02:34:11 +04:00
struct ldb_module * module = ctx - > module ;
struct ldb_request * req = ctx - > req ;
2005-01-02 10:49:29 +03:00
struct ldb_message * msg ;
2009-10-06 11:30:53 +04:00
int ret = LDB_SUCCESS ;
2004-10-20 23:28:02 +04:00
2009-01-30 02:39:30 +03:00
ldb_request_set_state ( req , LDB_ASYNC_PENDING ) ;
2005-06-18 11:42:21 +04:00
2008-09-12 02:34:11 +04:00
if ( ltdb_cache_load ( ctx - > module ) ! = 0 ) {
2006-05-28 06:10:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2006-03-03 20:44:03 +03:00
}
2010-10-20 15:53:14 +04:00
msg = ldb_msg_new ( ctx ) ;
2005-01-02 10:49:29 +03:00
if ( msg = = NULL ) {
2009-10-13 01:39:40 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2005-01-02 10:49:29 +03:00
}
2004-10-20 23:28:02 +04:00
/* in case any attribute of the message was indexed, we need
to fetch the old record */
2009-10-06 11:30:53 +04:00
ret = ltdb_search_dn1 ( module , req - > op . rename . olddn , msg ) ;
if ( ret ! = LDB_SUCCESS ) {
2004-10-20 23:28:02 +04:00
/* not finding the old record is an error */
2009-10-13 01:39:40 +04:00
return ret ;
2004-10-20 23:28:02 +04:00
}
2009-06-04 07:52:40 +04:00
/* Always delete first then add, to avoid conflicts with
* unique indexes . We rely on the transaction to make this
* atomic
*/
2009-11-18 13:56:24 +03:00
ret = ltdb_delete_internal ( module , msg - > dn ) ;
2009-10-06 11:30:53 +04:00
if ( ret ! = LDB_SUCCESS ) {
2009-10-13 01:39:40 +04:00
return ret ;
2009-06-04 07:52:40 +04:00
}
2007-09-18 10:36:07 +04:00
2009-11-18 13:56:24 +03:00
msg - > dn = ldb_dn_copy ( msg , req - > op . rename . newdn ) ;
if ( msg - > dn = = NULL ) {
return LDB_ERR_OPERATIONS_ERROR ;
}
2009-10-06 11:30:53 +04:00
ret = ltdb_add_internal ( module , msg ) ;
2004-10-20 23:28:02 +04:00
2009-10-06 11:30:53 +04:00
return ret ;
2006-03-03 20:44:03 +03:00
}
2005-01-02 10:49:29 +03:00
2005-09-17 23:25:50 +04:00
static int ltdb_start_trans ( struct ldb_module * module )
{
2009-01-30 02:39:30 +03:00
void * data = ldb_module_get_private ( module ) ;
struct ltdb_private * ltdb = talloc_get_type ( data , struct ltdb_private ) ;
2005-09-22 07:56:41 +04:00
if ( tdb_transaction_start ( ltdb - > tdb ) ! = 0 ) {
2005-10-28 07:43:39 +04:00
return ltdb_err_map ( tdb_error ( ltdb - > tdb ) ) ;
2005-09-22 07:56:41 +04:00
}
2005-09-17 23:25:50 +04:00
2007-10-18 04:03:21 +04:00
ltdb - > in_transaction + + ;
2008-12-16 10:45:28 +03:00
ltdb_index_transaction_start ( module ) ;
2005-09-24 19:42:15 +04:00
return LDB_SUCCESS ;
2005-09-17 23:25:50 +04:00
}
2009-03-31 08:07:54 +04:00
static int ltdb_prepare_commit ( struct ldb_module * module )
2005-09-17 23:25:50 +04:00
{
2009-01-30 02:39:30 +03:00
void * data = ldb_module_get_private ( module ) ;
struct ltdb_private * ltdb = talloc_get_type ( data , struct ltdb_private ) ;
2005-09-22 07:56:41 +04:00
2009-03-31 08:07:54 +04:00
if ( ltdb - > in_transaction ! = 1 ) {
return LDB_SUCCESS ;
}
2007-10-18 04:03:21 +04:00
2009-10-02 16:39:19 +04:00
if ( ltdb_index_transaction_commit ( module ) ! = 0 ) {
2009-02-22 09:50:49 +03:00
tdb_transaction_cancel ( ltdb - > tdb ) ;
2009-03-31 08:07:54 +04:00
ltdb - > in_transaction - - ;
return ltdb_err_map ( tdb_error ( ltdb - > tdb ) ) ;
}
if ( tdb_transaction_prepare_commit ( ltdb - > tdb ) ! = 0 ) {
ltdb - > in_transaction - - ;
2008-12-16 10:45:28 +03:00
return ltdb_err_map ( tdb_error ( ltdb - > tdb ) ) ;
}
2009-03-31 08:07:54 +04:00
ltdb - > prepared_commit = true ;
return LDB_SUCCESS ;
}
static int ltdb_end_trans ( struct ldb_module * module )
{
void * data = ldb_module_get_private ( module ) ;
struct ltdb_private * ltdb = talloc_get_type ( data , struct ltdb_private ) ;
if ( ! ltdb - > prepared_commit ) {
int ret = ltdb_prepare_commit ( module ) ;
if ( ret ! = LDB_SUCCESS ) {
return ret ;
}
}
ltdb - > in_transaction - - ;
ltdb - > prepared_commit = false ;
2005-09-24 19:42:15 +04:00
if ( tdb_transaction_commit ( ltdb - > tdb ) ! = 0 ) {
2005-10-28 07:43:39 +04:00
return ltdb_err_map ( tdb_error ( ltdb - > tdb ) ) ;
2005-09-24 19:42:15 +04:00
}
return LDB_SUCCESS ;
}
static int ltdb_del_trans ( struct ldb_module * module )
{
2009-01-30 02:39:30 +03:00
void * data = ldb_module_get_private ( module ) ;
struct ltdb_private * ltdb = talloc_get_type ( data , struct ltdb_private ) ;
2005-09-24 19:42:15 +04:00
2007-10-18 04:03:21 +04:00
ltdb - > in_transaction - - ;
2008-12-16 10:45:28 +03:00
if ( ltdb_index_transaction_cancel ( module ) ! = 0 ) {
2009-02-22 09:50:49 +03:00
tdb_transaction_cancel ( ltdb - > tdb ) ;
2008-12-16 10:45:28 +03:00
return ltdb_err_map ( tdb_error ( ltdb - > tdb ) ) ;
}
2005-09-24 19:42:15 +04:00
if ( tdb_transaction_cancel ( ltdb - > tdb ) ! = 0 ) {
2005-10-28 07:43:39 +04:00
return ltdb_err_map ( tdb_error ( ltdb - > tdb ) ) ;
2005-09-22 07:56:41 +04:00
}
2005-09-17 23:25:50 +04:00
2005-09-24 19:42:15 +04:00
return LDB_SUCCESS ;
2005-09-17 23:25:50 +04:00
}
2004-03-31 10:45:39 +04:00
2006-02-27 03:39:26 +03:00
/*
return sequenceNumber from @ BASEINFO
*/
2008-10-15 22:03:20 +04:00
static int ltdb_sequence_number ( struct ltdb_context * ctx ,
struct ldb_extended * * ext )
2006-02-27 03:39:26 +03:00
{
2009-01-30 02:39:30 +03:00
struct ldb_context * ldb ;
2008-10-15 22:03:20 +04:00
struct ldb_module * module = ctx - > module ;
struct ldb_request * req = ctx - > req ;
2011-01-09 00:04:32 +03:00
TALLOC_CTX * tmp_ctx = NULL ;
2008-10-15 22:03:20 +04:00
struct ldb_seqnum_request * seq ;
struct ldb_seqnum_result * res ;
2006-06-08 01:03:38 +04:00
struct ldb_message * msg = NULL ;
2007-12-24 07:03:31 +03:00
struct ldb_dn * dn ;
const char * date ;
2009-10-06 11:30:53 +04:00
int ret = LDB_SUCCESS ;
2006-06-08 01:03:38 +04:00
2009-01-30 02:39:30 +03:00
ldb = ldb_module_get_ctx ( module ) ;
2008-10-15 22:03:20 +04:00
seq = talloc_get_type ( req - > op . extended . data ,
struct ldb_seqnum_request ) ;
if ( seq = = NULL ) {
return LDB_ERR_OPERATIONS_ERROR ;
}
2009-01-30 02:39:30 +03:00
ldb_request_set_state ( req , LDB_ASYNC_PENDING ) ;
2008-10-15 22:03:20 +04:00
if ( ltdb_lock_read ( module ) ! = 0 ) {
return LDB_ERR_OPERATIONS_ERROR ;
}
res = talloc_zero ( req , struct ldb_seqnum_result ) ;
if ( res = = NULL ) {
ret = LDB_ERR_OPERATIONS_ERROR ;
goto done ;
}
2011-01-09 00:04:32 +03:00
2007-12-24 07:03:31 +03:00
tmp_ctx = talloc_new ( req ) ;
2006-06-08 01:03:38 +04:00
if ( tmp_ctx = = NULL ) {
2008-10-15 22:03:20 +04:00
ret = LDB_ERR_OPERATIONS_ERROR ;
goto done ;
2006-06-08 01:03:38 +04:00
}
2009-01-30 02:39:30 +03:00
dn = ldb_dn_new ( tmp_ctx , ldb , LTDB_BASEINFO ) ;
2011-01-09 00:07:30 +03:00
if ( dn = = NULL ) {
ret = LDB_ERR_OPERATIONS_ERROR ;
goto done ;
}
2007-12-24 07:03:31 +03:00
2010-10-20 15:53:14 +04:00
msg = ldb_msg_new ( tmp_ctx ) ;
2006-06-08 01:03:38 +04:00
if ( msg = = NULL ) {
2008-10-15 22:03:20 +04:00
ret = LDB_ERR_OPERATIONS_ERROR ;
goto done ;
2006-06-08 01:03:38 +04:00
}
2006-02-27 03:39:26 +03:00
2008-10-15 22:03:20 +04:00
ret = ltdb_search_dn1 ( module , dn , msg ) ;
if ( ret ! = LDB_SUCCESS ) {
goto done ;
2006-02-27 03:39:26 +03:00
}
2008-10-15 22:03:20 +04:00
switch ( seq - > type ) {
2006-09-21 10:44:12 +04:00
case LDB_SEQ_HIGHEST_SEQ :
2008-10-15 22:03:20 +04:00
res - > seq_num = ldb_msg_find_attr_as_uint64 ( msg , LTDB_SEQUENCE_NUMBER , 0 ) ;
2006-09-21 10:44:12 +04:00
break ;
case LDB_SEQ_NEXT :
2008-10-15 22:03:20 +04:00
res - > seq_num = ldb_msg_find_attr_as_uint64 ( msg , LTDB_SEQUENCE_NUMBER , 0 ) ;
res - > seq_num + + ;
2006-09-21 10:44:12 +04:00
break ;
case LDB_SEQ_HIGHEST_TIMESTAMP :
2007-12-24 07:03:31 +03:00
date = ldb_msg_find_attr_as_string ( msg , LTDB_MOD_TIMESTAMP , NULL ) ;
2006-09-21 10:44:12 +04:00
if ( date ) {
2008-10-15 22:03:20 +04:00
res - > seq_num = ldb_string_to_time ( date ) ;
2006-09-21 10:44:12 +04:00
} else {
2008-10-15 22:03:20 +04:00
res - > seq_num = 0 ;
2006-09-21 10:44:12 +04:00
/* zero is as good as anything when we don't know */
}
break ;
}
2008-09-12 02:34:11 +04:00
2008-10-15 22:03:20 +04:00
* ext = talloc_zero ( req , struct ldb_extended ) ;
if ( * ext = = NULL ) {
ret = LDB_ERR_OPERATIONS_ERROR ;
goto done ;
}
( * ext ) - > oid = LDB_EXTENDED_SEQUENCE_NUMBER ;
( * ext ) - > data = talloc_steal ( * ext , res ) ;
2008-09-12 02:34:11 +04:00
2008-10-15 22:03:20 +04:00
done :
talloc_free ( tmp_ctx ) ;
ltdb_unlock_read ( module ) ;
return ret ;
2008-09-12 02:34:11 +04:00
}
2009-01-30 02:39:30 +03:00
static void ltdb_request_done ( struct ltdb_context * ctx , int error )
2008-09-12 02:34:11 +04:00
{
2009-01-30 02:39:30 +03:00
struct ldb_context * ldb ;
struct ldb_request * req ;
2008-09-12 02:34:11 +04:00
struct ldb_reply * ares ;
2009-01-30 02:39:30 +03:00
ldb = ldb_module_get_ctx ( ctx - > module ) ;
req = ctx - > req ;
2008-09-12 02:34:11 +04:00
/* if we already returned an error just return */
2009-01-30 02:39:30 +03:00
if ( ldb_request_get_status ( req ) ! = LDB_SUCCESS ) {
2008-09-12 02:34:11 +04:00
return ;
}
ares = talloc_zero ( req , struct ldb_reply ) ;
if ( ! ares ) {
2009-01-30 02:39:30 +03:00
ldb_oom ( ldb ) ;
2008-09-12 02:34:11 +04:00
req - > callback ( req , NULL ) ;
return ;
}
ares - > type = LDB_REPLY_DONE ;
ares - > error = error ;
req - > callback ( req , ares ) ;
}
2008-12-29 22:24:57 +03:00
static void ltdb_timeout ( struct tevent_context * ev ,
struct tevent_timer * te ,
2008-09-12 02:34:11 +04:00
struct timeval t ,
void * private_data )
{
struct ltdb_context * ctx ;
ctx = talloc_get_type ( private_data , struct ltdb_context ) ;
2009-03-10 01:04:38 +03:00
if ( ! ctx - > request_terminated ) {
/* request is done now */
ltdb_request_done ( ctx , LDB_ERR_TIME_LIMIT_EXCEEDED ) ;
}
2009-03-10 17:05:52 +03:00
if ( ! ctx - > request_terminated ) {
/* neutralize the spy */
ctx - > spy - > ctx = NULL ;
}
2009-03-10 01:04:38 +03:00
talloc_free ( ctx ) ;
2008-09-12 02:34:11 +04:00
}
2009-01-30 02:39:30 +03:00
static void ltdb_request_extended_done ( struct ltdb_context * ctx ,
2008-10-15 22:03:20 +04:00
struct ldb_extended * ext ,
int error )
{
2009-01-30 02:39:30 +03:00
struct ldb_context * ldb ;
struct ldb_request * req ;
2008-10-15 22:03:20 +04:00
struct ldb_reply * ares ;
2009-01-30 02:39:30 +03:00
ldb = ldb_module_get_ctx ( ctx - > module ) ;
req = ctx - > req ;
2008-10-15 22:03:20 +04:00
/* if we already returned an error just return */
2009-01-30 02:39:30 +03:00
if ( ldb_request_get_status ( req ) ! = LDB_SUCCESS ) {
2008-10-15 22:03:20 +04:00
return ;
}
ares = talloc_zero ( req , struct ldb_reply ) ;
if ( ! ares ) {
2009-01-30 02:39:30 +03:00
ldb_oom ( ldb ) ;
2008-10-15 22:03:20 +04:00
req - > callback ( req , NULL ) ;
return ;
}
ares - > type = LDB_REPLY_DONE ;
ares - > response = ext ;
ares - > error = error ;
req - > callback ( req , ares ) ;
}
static void ltdb_handle_extended ( struct ltdb_context * ctx )
{
struct ldb_extended * ext = NULL ;
int ret ;
if ( strcmp ( ctx - > req - > op . extended . oid ,
LDB_EXTENDED_SEQUENCE_NUMBER ) = = 0 ) {
/* get sequence number */
ret = ltdb_sequence_number ( ctx , & ext ) ;
} else {
/* not recognized */
ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION ;
}
2009-01-30 02:39:30 +03:00
ltdb_request_extended_done ( ctx , ext , ret ) ;
2008-10-15 22:03:20 +04:00
}
2008-12-29 22:24:57 +03:00
static void ltdb_callback ( struct tevent_context * ev ,
struct tevent_timer * te ,
2008-09-12 02:34:11 +04:00
struct timeval t ,
void * private_data )
{
struct ltdb_context * ctx ;
int ret ;
ctx = talloc_get_type ( private_data , struct ltdb_context ) ;
2009-03-10 17:05:52 +03:00
if ( ctx - > request_terminated ) {
goto done ;
}
2009-03-10 01:04:38 +03:00
2008-09-12 02:34:11 +04:00
switch ( ctx - > req - > operation ) {
case LDB_SEARCH :
ret = ltdb_search ( ctx ) ;
break ;
case LDB_ADD :
ret = ltdb_add ( ctx ) ;
break ;
case LDB_MODIFY :
ret = ltdb_modify ( ctx ) ;
break ;
case LDB_DELETE :
ret = ltdb_delete ( ctx ) ;
break ;
case LDB_RENAME :
ret = ltdb_rename ( ctx ) ;
break ;
2008-10-15 22:03:20 +04:00
case LDB_EXTENDED :
ltdb_handle_extended ( ctx ) ;
2009-03-10 17:05:52 +03:00
goto done ;
2008-09-12 02:34:11 +04:00
default :
/* no other op supported */
2010-10-18 23:21:45 +04:00
ret = LDB_ERR_PROTOCOL_ERROR ;
2008-09-12 02:34:11 +04:00
}
2009-03-10 01:04:38 +03:00
if ( ! ctx - > request_terminated ) {
/* request is done now */
2009-01-30 02:39:30 +03:00
ltdb_request_done ( ctx , ret ) ;
2008-09-12 02:34:11 +04:00
}
2009-03-10 01:04:38 +03:00
done :
2009-03-10 17:05:52 +03:00
if ( ! ctx - > request_terminated ) {
/* neutralize the spy */
ctx - > spy - > ctx = NULL ;
}
2009-03-10 01:04:38 +03:00
talloc_free ( ctx ) ;
}
static int ltdb_request_destructor ( void * ptr )
{
struct ltdb_req_spy * spy = talloc_get_type ( ptr , struct ltdb_req_spy ) ;
if ( spy - > ctx ! = NULL ) {
spy - > ctx - > request_terminated = true ;
}
return 0 ;
2008-09-12 02:34:11 +04:00
}
static int ltdb_handle_request ( struct ldb_module * module ,
2008-11-13 06:07:29 +03:00
struct ldb_request * req )
2008-09-12 02:34:11 +04:00
{
2010-01-30 08:33:22 +03:00
struct ldb_control * control_permissive ;
2009-01-30 02:39:30 +03:00
struct ldb_context * ldb ;
2008-12-29 22:24:57 +03:00
struct tevent_context * ev ;
2008-09-12 02:34:11 +04:00
struct ltdb_context * ac ;
2008-12-29 22:24:57 +03:00
struct tevent_timer * te ;
2008-09-12 02:34:11 +04:00
struct timeval tv ;
2010-10-18 22:10:17 +04:00
unsigned int i ;
2008-09-12 02:34:11 +04:00
2009-01-30 02:39:30 +03:00
ldb = ldb_module_get_ctx ( module ) ;
2010-01-30 08:33:22 +03:00
control_permissive = ldb_request_get_control ( req ,
LDB_CONTROL_PERMISSIVE_MODIFY_OID ) ;
2009-12-16 09:13:19 +03:00
for ( i = 0 ; req - > controls & & req - > controls [ i ] ; i + + ) {
2010-01-30 08:33:22 +03:00
if ( req - > controls [ i ] - > critical & &
req - > controls [ i ] ! = control_permissive ) {
2009-12-16 09:13:19 +03:00
ldb_asprintf_errstring ( ldb , " Unsupported critical extension %s " ,
req - > controls [ i ] - > oid ) ;
return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION ;
}
}
2008-09-12 02:34:11 +04:00
if ( req - > starttime = = 0 | | req - > timeout = = 0 ) {
2009-01-30 02:39:30 +03:00
ldb_set_errstring ( ldb , " Invalid timeout settings " ) ;
2008-09-12 02:34:11 +04:00
return LDB_ERR_TIME_LIMIT_EXCEEDED ;
}
2009-01-30 02:39:30 +03:00
ev = ldb_get_event_context ( ldb ) ;
2008-09-12 02:34:11 +04:00
2009-03-10 01:04:38 +03:00
ac = talloc_zero ( ldb , struct ltdb_context ) ;
2008-09-12 02:34:11 +04:00
if ( ac = = NULL ) {
2009-10-06 11:30:53 +04:00
ldb_oom ( ldb ) ;
2008-09-12 02:34:11 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
}
ac - > module = module ;
ac - > req = req ;
tv . tv_sec = 0 ;
tv . tv_usec = 0 ;
2009-01-21 11:16:45 +03:00
te = tevent_add_timer ( ev , ac , tv , ltdb_callback , ac ) ;
2008-09-12 02:34:11 +04:00
if ( NULL = = te ) {
2009-03-10 01:04:38 +03:00
talloc_free ( ac ) ;
2008-09-12 02:34:11 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
}
tv . tv_sec = req - > starttime + req - > timeout ;
2009-01-21 11:16:45 +03:00
ac - > timeout_event = tevent_add_timer ( ev , ac , tv , ltdb_timeout , ac ) ;
2008-11-13 06:07:29 +03:00
if ( NULL = = ac - > timeout_event ) {
2009-03-10 01:04:38 +03:00
talloc_free ( ac ) ;
2008-09-12 02:34:11 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
}
2009-03-10 01:04:38 +03:00
/* set a spy so that we do not try to use the request context
* if it is freed before ltdb_callback fires */
ac - > spy = talloc ( req , struct ltdb_req_spy ) ;
if ( NULL = = ac - > spy ) {
talloc_free ( ac ) ;
return LDB_ERR_OPERATIONS_ERROR ;
}
ac - > spy - > ctx = ac ;
talloc_set_destructor ( ( TALLOC_CTX * ) ac - > spy , ltdb_request_destructor ) ;
2006-06-08 01:03:38 +04:00
return LDB_SUCCESS ;
2006-02-27 03:39:26 +03:00
}
2010-01-30 08:33:22 +03:00
static int ltdb_init_rootdse ( struct ldb_module * module )
{
struct ldb_context * ldb ;
int ret ;
ldb = ldb_module_get_ctx ( module ) ;
ret = ldb_mod_register_control ( module ,
LDB_CONTROL_PERMISSIVE_MODIFY_OID ) ;
2010-09-28 21:46:03 +04:00
/* ignore errors on this - we expect it for non-sam databases */
2010-01-30 08:33:22 +03:00
/* there can be no module beyond the backend, just return */
return LDB_SUCCESS ;
}
2004-11-15 14:40:27 +03:00
static const struct ldb_module_ops ltdb_ops = {
2005-09-17 23:25:50 +04:00
. name = " tdb " ,
2010-01-30 08:33:22 +03:00
. init_context = ltdb_init_rootdse ,
2008-09-12 02:34:11 +04:00
. search = ltdb_handle_request ,
. add = ltdb_handle_request ,
. modify = ltdb_handle_request ,
. del = ltdb_handle_request ,
. rename = ltdb_handle_request ,
2008-10-15 22:03:20 +04:00
. extended = ltdb_handle_request ,
2005-09-17 23:25:50 +04:00
. start_transaction = ltdb_start_trans ,
2005-09-24 19:42:15 +04:00
. end_transaction = ltdb_end_trans ,
2009-03-31 08:07:54 +04:00
. prepare_commit = ltdb_prepare_commit ,
2006-03-03 23:01:19 +03:00
. del_transaction = ltdb_del_trans ,
2004-03-31 10:45:39 +04:00
} ;
/*
connect to the database
*/
2008-06-15 04:37:40 +04:00
static int ltdb_connect ( struct ldb_context * ldb , const char * url ,
2006-06-08 01:03:38 +04:00
unsigned int flags , const char * options [ ] ,
2009-01-30 02:39:30 +03:00
struct ldb_module * * _module )
2004-03-31 10:45:39 +04:00
{
2009-01-30 02:39:30 +03:00
struct ldb_module * module ;
2004-03-31 10:45:39 +04:00
const char * path ;
int tdb_flags , open_flags ;
struct ltdb_private * ltdb ;
2004-05-06 08:40:15 +04:00
2004-03-31 10:45:39 +04:00
/* parse the url */
2004-05-03 13:34:18 +04:00
if ( strchr ( url , ' : ' ) ) {
if ( strncmp ( url , " tdb:// " , 6 ) ! = 0 ) {
2008-06-15 04:37:40 +04:00
ldb_debug ( ldb , LDB_DEBUG_ERROR ,
" Invalid tdb URL '%s' " , url ) ;
2009-10-23 19:23:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2004-05-03 13:34:18 +04:00
}
path = url + 6 ;
} else {
path = url ;
2004-03-31 10:45:39 +04:00
}
2006-10-19 01:45:46 +04:00
tdb_flags = TDB_DEFAULT | TDB_SEQNUM ;
2004-03-31 10:45:39 +04:00
2005-09-22 08:16:46 +04:00
/* check for the 'nosync' option */
if ( flags & LDB_FLG_NOSYNC ) {
tdb_flags | = TDB_NOSYNC ;
}
2005-09-22 07:56:41 +04:00
2007-06-06 16:44:04 +04:00
/* and nommap option */
if ( flags & LDB_FLG_NOMMAP ) {
tdb_flags | = TDB_NOMMAP ;
}
2004-03-31 10:45:39 +04:00
if ( flags & LDB_FLG_RDONLY ) {
open_flags = O_RDONLY ;
} else {
open_flags = O_CREAT | O_RDWR ;
}
2005-01-12 19:00:01 +03:00
ltdb = talloc_zero ( ldb , struct ltdb_private ) ;
2004-03-31 10:45:39 +04:00
if ( ! ltdb ) {
2005-06-18 11:42:21 +04:00
ldb_oom ( ldb ) ;
2009-10-23 19:23:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2004-03-31 10:45:39 +04:00
}
2005-07-20 04:59:38 +04:00
/* note that we use quite a large default hash size */
2008-06-15 04:37:40 +04:00
ltdb - > tdb = ltdb_wrap_open ( ltdb , path , 10000 ,
tdb_flags , open_flags ,
2009-01-30 02:39:30 +03:00
ldb_get_create_perms ( ldb ) , ldb ) ;
2005-07-20 04:59:38 +04:00
if ( ! ltdb - > tdb ) {
2008-06-15 04:37:40 +04:00
ldb_debug ( ldb , LDB_DEBUG_ERROR ,
2009-07-11 00:44:27 +04:00
" Unable to open tdb '%s' " , path ) ;
2005-07-20 04:59:38 +04:00
talloc_free ( ltdb ) ;
2009-10-23 19:23:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2005-07-20 04:59:38 +04:00
}
2004-05-01 13:45:56 +04:00
2010-11-23 12:19:49 +03:00
if ( getenv ( " LDB_WARN_UNINDEXED " ) ) {
ltdb - > warn_unindexed = true ;
}
2005-07-20 04:59:38 +04:00
ltdb - > sequence_number = 0 ;
2004-03-31 10:45:39 +04:00
2009-01-30 02:39:30 +03:00
module = ldb_module_new ( ldb , ldb , " ldb_tdb backend " , & ltdb_ops ) ;
if ( ! module ) {
2005-06-18 11:42:21 +04:00
talloc_free ( ltdb ) ;
2009-10-23 19:23:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2004-11-15 14:40:27 +03:00
}
2009-01-30 02:39:30 +03:00
ldb_module_set_private ( module , ltdb ) ;
2009-10-21 09:08:24 +04:00
talloc_steal ( module , ltdb ) ;
2004-03-31 10:45:39 +04:00
2009-01-30 02:39:30 +03:00
if ( ltdb_cache_load ( module ) ! = 0 ) {
talloc_free ( module ) ;
2006-10-17 05:21:02 +04:00
talloc_free ( ltdb ) ;
2009-10-23 19:23:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2006-10-17 05:21:02 +04:00
}
2009-01-30 02:39:30 +03:00
* _module = module ;
2009-10-23 19:23:44 +04:00
return LDB_SUCCESS ;
2004-03-31 10:45:39 +04:00
}
2006-03-05 19:05:26 +03:00
2010-11-01 06:59:28 +03:00
int ldb_tdb_init ( const char * version )
{
2010-11-01 14:30:23 +03:00
LDB_MODULE_CHECK_VERSION ( version ) ;
2010-11-01 08:42:21 +03:00
return ldb_register_backend ( " tdb " , ltdb_connect , false ) ;
2010-11-01 06:59:28 +03:00
}