2004-03-31 10:45:39 +04:00
/*
ldb database library
Copyright ( C ) Andrew Tridgell 2004
2004-10-20 23:28:02 +04:00
Copyright ( C ) Stefan Metzmacher 2004
2006-03-03 20:44:03 +03:00
Copyright ( C ) Simo Sorce 2006
2004-10-20 23:28:02 +04:00
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
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
version 2 of the License , or ( at your option ) any later version .
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
License along with this library ; if not , write to the Free Software
Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
/*
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 :
*
* - description : make the module use asyncronous calls
* date : Feb 2006
* Author : Simo Sorce
2004-03-31 10:45:39 +04:00
*/
# include "includes.h"
2006-01-10 19:48:32 +03:00
# include "ldb/include/includes.h"
2004-04-11 00:18:22 +04:00
# include "ldb/ldb_tdb/ldb_tdb.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
*/
static int ltdb_err_map ( enum TDB_ERROR tdb_code )
{
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 ;
}
return LDB_ERR_OTHER ;
}
2006-07-22 20:56:33 +04:00
struct ldb_handle * init_ltdb_handle ( struct ltdb_private * ltdb , struct ldb_module * module ,
2006-10-16 07:15:41 +04:00
struct ldb_request * req )
2006-03-03 20:44:03 +03:00
{
2006-07-22 20:56:33 +04:00
struct ltdb_context * ac ;
struct ldb_handle * h ;
2006-03-03 20:44:03 +03:00
2006-10-16 07:15:41 +04:00
h = talloc_zero ( req , struct ldb_handle ) ;
2006-03-03 20:44:03 +03:00
if ( h = = NULL ) {
2006-08-13 11:33:57 +04:00
ldb_set_errstring ( module - > ldb , " Out of Memory " ) ;
2006-03-03 20:44:03 +03:00
return NULL ;
}
2006-03-08 00:08:09 +03:00
h - > module = module ;
2006-07-22 20:56:33 +04:00
ac = talloc_zero ( h , struct ltdb_context ) ;
2006-03-03 20:44:03 +03:00
if ( ac = = NULL ) {
2006-08-13 11:33:57 +04:00
ldb_set_errstring ( module - > ldb , " Out of Memory " ) ;
2006-03-03 20:44:03 +03:00
talloc_free ( h ) ;
return NULL ;
}
h - > private_data = ( void * ) ac ;
2006-03-10 18:27:16 +03:00
h - > state = LDB_ASYNC_INIT ;
h - > status = LDB_SUCCESS ;
2006-03-03 20:44:03 +03:00
ac - > module = module ;
2006-10-16 07:15:41 +04:00
ac - > context = req - > context ;
ac - > callback = req - > callback ;
2006-03-03 20:44:03 +03:00
return h ;
}
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
note that the key for a record can depend on whether the
dn refers to a case sensitive index record or not
2004-03-31 10:45:39 +04:00
*/
2006-11-22 03:59:34 +03:00
struct TDB_DATA ltdb_key ( struct ldb_module * module , struct ldb_dn * dn )
2004-03-31 10:45:39 +04:00
{
2004-11-15 14:40:27 +03:00
struct ldb_context * ldb = module - > ldb ;
2004-03-31 10:45:39 +04:00
TDB_DATA key ;
char * key_str = NULL ;
2006-11-22 05:05:19 +03:00
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
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
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
2006-11-22 03:59:34 +03:00
dn_folded = ldb_dn_get_casefold ( dn ) ;
2005-08-18 19:02:01 +04:00
if ( ! dn_folded ) {
goto failed ;
2004-05-01 13:45:56 +04:00
}
2005-01-02 10:49:29 +03:00
key_str = talloc_asprintf ( ldb , " DN=%s " , dn_folded ) ;
2005-08-18 19:02:01 +04:00
2004-03-31 10:45:39 +04:00
if ( ! key_str ) {
2004-05-01 13:45:56 +04:00
goto failed ;
2004-03-31 10:45:39 +04:00
}
2005-10-12 10:10:23 +04:00
key . dptr = ( uint8_t * ) key_str ;
2005-07-02 22:43:22 +04:00
key . dsize = strlen ( key_str ) + 1 ;
2004-03-31 10:45:39 +04:00
return key ;
2004-05-01 13:45:56 +04:00
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
*/
int ltdb_check_special_dn ( struct ldb_module * module , const struct ldb_message * msg )
{
int i , j ;
2005-08-18 19:02:01 +04:00
if ( ! ldb_dn_is_special ( msg - > dn ) | |
! ldb_dn_check_special ( msg - > dn , LTDB_ATTRIBUTES ) ) {
2005-05-18 01:43:47 +04:00
return 0 ;
}
/* 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 + + ) {
for ( j = 0 ; j < msg - > elements [ i ] . num_values ; j + + ) {
if ( ltdb_check_at_attributes_values ( & msg - > elements [ i ] . values [ j ] ) ! = 0 ) {
2006-08-13 11:33:57 +04:00
ldb_set_errstring ( module - > 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
}
}
}
return 0 ;
}
2005-05-01 16:34:12 +04:00
2004-05-01 13:45:56 +04:00
/*
we ' ve made a modification to a dn - possibly reindex and
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
{
int ret = 0 ;
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
}
if ( ret = = 0 & &
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
}
return ret ;
}
2004-03-31 10:45:39 +04:00
/*
store a record into the db
*/
2004-11-15 14:40:27 +03:00
int ltdb_store ( struct ldb_module * module , const struct ldb_message * msg , int flgs )
2004-03-31 10:45:39 +04:00
{
2006-10-06 19:03:41 +04:00
struct ltdb_private * ltdb =
talloc_get_type ( module - > private_data , struct ltdb_private ) ;
2004-03-31 10:45:39 +04:00
TDB_DATA tdb_key , tdb_data ;
int ret ;
2004-11-15 14:40:27 +03: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
}
2004-11-16 12:00:52 +03:00
ret = ltdb_pack_data ( module , msg , & tdb_data ) ;
2004-03-31 10:45:39 +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 ) ;
if ( ret = = - 1 ) {
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 ;
}
2004-11-15 14:40:27 +03:00
ret = ltdb_index_add ( module , msg ) ;
2004-03-31 10:45:39 +04:00
if ( ret = = - 1 ) {
tdb_delete ( ltdb - > tdb , tdb_key ) ;
}
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 ;
}
2006-05-30 05:46:14 +04:00
static int ltdb_add_internal ( struct ldb_module * module , const struct ldb_message * msg )
2006-05-30 03:46:43 +04:00
{
int ret ;
ret = ltdb_check_special_dn ( module , msg ) ;
if ( ret ! = LDB_SUCCESS ) {
2006-05-31 14:17:05 +04:00
return ret ;
2006-05-30 03:46:43 +04:00
}
if ( ltdb_cache_load ( module ) ! = 0 ) {
return LDB_ERR_OPERATIONS_ERROR ;
}
ret = ltdb_store ( module , msg , TDB_INSERT ) ;
2006-08-01 07:22:02 +04:00
if ( ret = = LDB_ERR_ENTRY_ALREADY_EXISTS ) {
2006-11-22 05:05:19 +03:00
ldb_asprintf_errstring ( module - > ldb , " Entry %s already exists " , ldb_dn_get_linearized ( msg - > dn ) ) ;
2006-08-01 07:22:02 +04:00
return ret ;
}
if ( ret = = LDB_SUCCESS ) {
2006-08-01 06:25:05 +04:00
ret = ltdb_modified ( module , msg - > dn ) ;
if ( ret ! = LDB_SUCCESS ) {
return LDB_ERR_OPERATIONS_ERROR ;
2006-07-29 07:00:16 +04:00
}
2006-08-01 06:25:05 +04:00
}
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
*/
2006-05-30 03:46:43 +04:00
static int ltdb_add ( struct ldb_module * module , struct ldb_request * req )
2004-03-31 10:45:39 +04:00
{
2006-03-03 20:44:03 +03:00
struct ltdb_private * ltdb = talloc_get_type ( module - > private_data , struct ltdb_private ) ;
2006-07-22 20:56:33 +04:00
struct ltdb_context * ltdb_ac ;
2006-05-28 06:10:44 +04:00
int tret , ret = LDB_SUCCESS ;
2006-03-03 20:44:03 +03:00
2006-05-30 03:46:43 +04:00
if ( req - > controls ! = NULL ) {
ldb_debug ( module - > ldb , LDB_DEBUG_WARNING , " Controls should not reach the ldb_tdb backend! \n " ) ;
if ( check_critical_controls ( req - > controls ) ) {
return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION ;
}
}
2006-10-16 07:15:41 +04:00
req - > handle = init_ltdb_handle ( ltdb , module , req ) ;
2006-07-22 21:21:59 +04:00
if ( req - > handle = = NULL ) {
2006-05-28 06:10:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2006-03-03 20:44:03 +03:00
}
2006-07-22 21:21:59 +04:00
ltdb_ac = talloc_get_type ( req - > handle - > private_data , struct ltdb_context ) ;
2004-04-03 16:29:21 +04:00
2006-05-30 03:46:43 +04:00
tret = ltdb_add_internal ( module , req - > op . add . message ) ;
2006-05-28 06:10:44 +04:00
if ( tret ! = LDB_SUCCESS ) {
2006-07-22 21:21:59 +04:00
req - > handle - > status = tret ;
2006-05-28 06:10:44 +04:00
goto done ;
2005-05-18 01:43:47 +04:00
}
2006-05-28 06:10:44 +04:00
if ( ltdb_ac - > callback ) {
ret = ltdb_ac - > callback ( module - > ldb , ltdb_ac - > context , NULL ) ;
}
done :
2006-07-22 21:21:59 +04:00
req - > handle - > state = LDB_ASYNC_DONE ;
2006-05-28 06:10:44 +04:00
return ret ;
2004-03-31 10:45:39 +04:00
}
/*
delete a record from the database , not updating indexes ( used for deleting
index records )
*/
2006-11-22 03:59:34 +03:00
int ltdb_delete_noindex ( struct ldb_module * module , struct ldb_dn * dn )
2004-03-31 10:45:39 +04:00
{
2006-10-06 19:03:41 +04:00
struct ltdb_private * ltdb =
talloc_get_type ( module - > private_data , struct ltdb_private ) ;
2004-03-31 10:45:39 +04:00
TDB_DATA tdb_key ;
int ret ;
2004-11-15 14:40:27 +03: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 ;
}
2006-11-22 03:59:34 +03: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 ;
int ret ;
msg = talloc ( module , struct ldb_message ) ;
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 ) ;
if ( ret ! = 1 ) {
/* not finding the old record is an error */
talloc_free ( msg ) ;
return LDB_ERR_NO_SUCH_OBJECT ;
}
ret = ltdb_delete_noindex ( module , dn ) ;
if ( ret ! = LDB_SUCCESS ) {
talloc_free ( msg ) ;
return LDB_ERR_NO_SUCH_OBJECT ;
}
/* remove any indexed attributes */
ret = ltdb_index_del ( module , msg ) ;
if ( ret ! = LDB_SUCCESS ) {
talloc_free ( msg ) ;
return LDB_ERR_OPERATIONS_ERROR ;
}
2006-05-30 05:46:14 +04:00
ret = ltdb_modified ( module , dn ) ;
if ( ret ! = LDB_SUCCESS ) {
2006-10-16 16:33:21 +04:00
talloc_free ( msg ) ;
2006-05-30 05:46:14 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
}
2006-05-30 03:46:43 +04:00
talloc_free ( msg ) ;
return LDB_SUCCESS ;
}
2004-03-31 10:45:39 +04:00
/*
delete a record from the database
*/
2006-05-30 03:46:43 +04:00
static int ltdb_delete ( struct ldb_module * module , struct ldb_request * req )
2004-03-31 10:45:39 +04:00
{
2006-03-03 20:44:03 +03:00
struct ltdb_private * ltdb = talloc_get_type ( module - > private_data , struct ltdb_private ) ;
2006-07-22 20:56:33 +04:00
struct ltdb_context * ltdb_ac ;
2006-05-28 06:10:44 +04:00
int tret , ret = LDB_SUCCESS ;
2006-03-03 20:44:03 +03:00
2006-05-30 03:46:43 +04:00
if ( req - > controls ! = NULL ) {
ldb_debug ( module - > ldb , LDB_DEBUG_WARNING , " Controls should not reach the ldb_tdb backend! \n " ) ;
if ( check_critical_controls ( req - > controls ) ) {
return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION ;
}
}
2006-07-22 21:21:59 +04:00
req - > handle = NULL ;
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
}
2006-10-16 07:15:41 +04:00
req - > handle = init_ltdb_handle ( ltdb , module , req ) ;
2006-07-22 21:21:59 +04:00
if ( req - > handle = = NULL ) {
2006-05-28 06:10:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2006-03-03 20:44:03 +03:00
}
2006-07-22 21:21:59 +04:00
ltdb_ac = talloc_get_type ( req - > handle - > private_data , struct ltdb_context ) ;
2006-03-03 20:44:03 +03:00
2006-05-30 03:46:43 +04:00
tret = ltdb_delete_internal ( module , req - > op . del . dn ) ;
2006-05-28 06:10:44 +04:00
if ( tret ! = LDB_SUCCESS ) {
2006-07-22 21:21:59 +04:00
req - > handle - > status = tret ;
2006-05-28 06:10:44 +04:00
goto done ;
2006-03-03 20:44:03 +03:00
}
2004-04-23 17:05:27 +04:00
2006-05-31 14:22:38 +04:00
if ( ltdb_ac - > callback ) {
2006-05-28 06:10:44 +04:00
ret = ltdb_ac - > callback ( module - > ldb , ltdb_ac - > context , NULL ) ;
2006-05-31 14:22:38 +04:00
}
2006-05-28 06:10:44 +04:00
done :
2006-07-22 21:21:59 +04:00
req - > handle - > state = LDB_ASYNC_DONE ;
2006-03-03 20:44:03 +03:00
return ret ;
}
2004-04-03 16:29:21 +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
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
can call re - alloc on , and assumed that we can re - use the data pointers from the
passed in additional values . Use with care !
returns 0 on success , - 1 on failure ( and sets errno )
*/
2004-05-06 08:40:15 +04:00
static int 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
2005-01-12 19:00:01 +03: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 ;
e2 - > values = NULL ;
if ( el - > num_values ! = 0 ) {
2005-01-12 19:00:01 +03:00
e2 - > values = talloc_array ( msg - > elements , struct ldb_val , el - > num_values ) ;
2004-04-03 16:29:21 +04:00
if ( ! e2 - > values ) {
errno = ENOMEM ;
return - 1 ;
}
}
for ( i = 0 ; i < el - > num_values ; i + + ) {
e2 - > values [ i ] = el - > values [ i ] ;
}
e2 - > num_values = el - > num_values ;
msg - > num_elements + + ;
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 ,
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
{
2006-11-22 05:05:19 +03:00
const char * dn ;
2005-01-02 10:49:29 +03:00
unsigned int i , j ;
2004-04-03 16:29:21 +04:00
2006-11-22 05:05:19 +03:00
dn = ldb_dn_get_linearized ( msg - > dn ) ;
2005-08-18 19:02:01 +04:00
if ( dn = = NULL ) {
return - 1 ;
}
2004-04-03 16:29:21 +04:00
for ( i = 0 ; i < msg - > num_elements ; i + + ) {
2005-01-02 10:49:29 +03:00
if ( ldb_attr_cmp ( msg - > elements [ i ] . name , name ) = = 0 ) {
2004-12-19 13:56:29 +03:00
for ( j = 0 ; j < msg - > elements [ i ] . num_values ; j + + ) {
2005-08-18 19:02:01 +04:00
ltdb_index_del_value ( module , dn , & msg - > elements [ i ] , j ) ;
2004-12-19 13:56:29 +03:00
}
2005-01-02 10:49:29 +03:00
talloc_free ( msg - > elements [ i ] . values ) ;
if ( msg - > num_elements > ( i + 1 ) ) {
memmove ( & msg - > elements [ i ] ,
& msg - > elements [ i + 1 ] ,
sizeof ( struct ldb_message_element ) *
( msg - > num_elements - ( i + 1 ) ) ) ;
}
msg - > num_elements - - ;
i - - ;
2005-01-12 19:00:01 +03:00
msg - > elements = talloc_realloc ( msg , msg - > elements ,
2005-01-02 10:49:29 +03:00
struct ldb_message_element ,
msg - > num_elements ) ;
2004-04-03 16:29:21 +04:00
}
}
return 0 ;
2004-03-31 10:45:39 +04:00
}
2004-04-03 16:29:21 +04:00
/*
delete all elements matching an attribute name / value
return 0 on success , - 1 on failure
*/
2004-11-15 14:40:27 +03:00
static int msg_delete_element ( struct ldb_module * module ,
2004-05-01 13:45:56 +04:00
struct ldb_message * msg ,
2004-04-03 16:29:21 +04:00
const char * name ,
const struct ldb_val * val )
{
2004-11-15 14:40:27 +03:00
struct ldb_context * ldb = module - > ldb ;
2004-07-07 05:02:54 +04:00
unsigned int i ;
int found ;
2004-04-03 16:29:21 +04:00
struct ldb_message_element * el ;
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
const struct ldb_attrib_handler * h ;
2004-04-03 16:29:21 +04:00
2004-07-07 05:02:54 +04:00
found = find_element ( msg , name ) ;
if ( found = = - 1 ) {
2004-04-03 16:29:21 +04:00
return - 1 ;
}
2004-07-07 05:02:54 +04:00
el = & msg - > elements [ found ] ;
2004-04-03 16:29:21 +04:00
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
h = ldb_attrib_handler ( ldb , el - > name ) ;
2004-04-03 16:29:21 +04:00
for ( i = 0 ; i < el - > num_values ; i + + ) {
2005-07-02 21:30:03 +04:00
if ( h - > comparison_fn ( ldb , ldb , & el - > values [ i ] , val ) = = 0 ) {
2004-04-03 16:29:21 +04:00
if ( i < el - > num_values - 1 ) {
memmove ( & el - > values [ i ] , & el - > values [ i + 1 ] ,
2004-05-06 08:40:15 +04:00
sizeof ( el - > values [ i ] ) * ( el - > num_values - ( i + 1 ) ) ) ;
2004-04-03 16:29:21 +04:00
}
el - > num_values - - ;
2004-05-08 03:54:41 +04:00
if ( el - > num_values = = 0 ) {
2004-12-19 13:56:29 +03:00
return msg_delete_attribute ( module , ldb , msg , name ) ;
2004-05-08 03:54:41 +04:00
}
2004-04-03 16:29:21 +04:00
return 0 ;
}
}
2004-05-08 03:54:41 +04:00
2004-04-03 16:29:21 +04:00
return - 1 ;
}
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
get away with it , but if we ever have really large attribute lists
then we ' ll need to look at this again
2004-03-31 10:45:39 +04:00
*/
2004-11-15 14:40:27 +03:00
int ltdb_modify_internal ( struct ldb_module * module , const struct ldb_message * msg )
2004-03-31 10:45:39 +04:00
{
2004-11-15 14:40:27 +03:00
struct ldb_context * ldb = module - > ldb ;
2006-10-06 19:03:41 +04:00
struct ltdb_private * ltdb =
talloc_get_type ( module - > private_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 ;
2004-07-07 05:02:54 +04:00
unsigned i , j ;
int ret ;
2004-04-03 16:29:21 +04:00
2004-11-15 14:40:27 +03: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
}
tdb_data = tdb_fetch ( ltdb - > tdb , tdb_key ) ;
if ( ! tdb_data . dptr ) {
2005-01-02 10:49:29 +03: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
}
2005-01-12 19:00:01 +03:00
msg2 = talloc ( tdb_key . dptr , struct ldb_message ) ;
2005-01-02 10:49:29 +03:00
if ( msg2 = = NULL ) {
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
}
2005-01-02 10:49:29 +03:00
ret = ltdb_unpack_data ( module , & tdb_data , msg2 ) ;
2004-03-31 10:45:39 +04:00
if ( ret = = - 1 ) {
2006-05-31 14:36:48 +04:00
ret = LDB_ERR_OTHER ;
goto failed ;
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
2004-03-31 10:45:39 +04:00
for ( i = 0 ; i < msg - > num_elements ; i + + ) {
2004-12-26 20:30:27 +03:00
struct ldb_message_element * el = & msg - > elements [ i ] ;
struct ldb_message_element * el2 ;
struct ldb_val * vals ;
2006-11-22 05:05:19 +03:00
const char * dn ;
2004-12-26 20:30:27 +03:00
2004-03-31 10:45:39 +04:00
switch ( msg - > elements [ i ] . flags & LDB_FLAG_MOD_MASK ) {
2004-04-03 16:29:21 +04:00
2004-03-31 10:45:39 +04:00
case LDB_FLAG_MOD_ADD :
2004-04-03 16:29:21 +04:00
/* add this element to the message. fail if it
already exists */
2005-01-02 10:49:29 +03:00
ret = find_element ( msg2 , el - > name ) ;
2004-12-26 20:30:27 +03:00
if ( ret = = - 1 ) {
2005-01-02 10:49:29 +03:00
if ( msg_add_element ( ldb , msg2 , el ) ! = 0 ) {
2005-09-18 22:49:06 +04:00
ret = LDB_ERR_OTHER ;
2004-12-26 20:30:27 +03:00
goto failed ;
}
continue ;
}
2005-01-02 10:49:29 +03:00
el2 = & msg2 - > elements [ ret ] ;
2004-12-26 20:30:27 +03:00
/* An attribute with this name already exists, add all
* values if they don ' t already exist . */
for ( j = 0 ; j < el - > num_values ; j + + ) {
if ( ldb_msg_find_val ( el2 , & el - > values [ j ] ) ) {
2006-08-13 11:33:57 +04:00
ldb_set_errstring ( module - > ldb , " Type or value exists " ) ;
2005-09-18 22:49:06 +04:00
ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS ;
2004-12-26 20:30:27 +03:00
goto failed ;
2004-12-06 09:45:51 +03:00
}
2004-03-31 10:45:39 +04:00
}
2004-12-26 20:30:27 +03:00
2005-01-12 19:00:01 +03:00
vals = talloc_realloc ( msg2 - > elements , el2 - > values , struct ldb_val ,
2005-01-02 10:49:29 +03:00
el2 - > num_values + el - > num_values ) ;
2004-12-26 20:30:27 +03:00
2006-05-31 14:36:48 +04:00
if ( vals = = NULL ) {
ret = LDB_ERR_OTHER ;
2004-04-03 16:29:21 +04:00
goto failed ;
2006-05-31 14:36:48 +04:00
}
2004-12-26 20:30:27 +03:00
for ( j = 0 ; j < el - > num_values ; j + + ) {
vals [ el2 - > num_values + j ] =
2005-01-02 10:49:29 +03:00
ldb_val_dup ( vals , & el - > values [ j ] ) ;
2004-04-03 16:29:21 +04:00
}
2004-12-26 20:30:27 +03:00
el2 - > values = vals ;
el2 - > num_values + = el - > num_values ;
2004-04-03 16:29:21 +04:00
break ;
case LDB_FLAG_MOD_REPLACE :
/* replace all elements of this attribute name with the elements
2004-05-22 04:52:04 +04:00
listed . The attribute not existing is not an error */
2005-01-02 10:49:29 +03:00
msg_delete_attribute ( module , ldb , msg2 , msg - > elements [ i ] . name ) ;
2004-05-22 04:52:04 +04:00
/* add the replacement element, if not empty */
if ( msg - > elements [ i ] . num_values ! = 0 & &
2005-01-02 10:49:29 +03:00
msg_add_element ( ldb , msg2 , & msg - > elements [ i ] ) ! = 0 ) {
2006-05-31 14:36:48 +04:00
ret = LDB_ERR_OTHER ;
2004-04-03 16:29:21 +04:00
goto failed ;
}
break ;
case LDB_FLAG_MOD_DELETE :
2005-08-18 19:02:01 +04:00
2006-11-22 05:05:19 +03:00
dn = ldb_dn_get_linearized ( msg - > dn ) ;
2006-05-31 14:36:48 +04:00
if ( dn = = NULL ) {
ret = LDB_ERR_OTHER ;
goto failed ;
}
2005-08-18 19:02:01 +04:00
2004-04-03 16:29:21 +04:00
/* we could be being asked to delete all
values or just some values */
if ( msg - > elements [ i ] . num_values = = 0 ) {
2005-01-02 10:49:29 +03:00
if ( msg_delete_attribute ( module , ldb , msg2 ,
2004-05-06 08:40:15 +04:00
msg - > elements [ i ] . name ) ! = 0 ) {
2006-08-13 11:33:57 +04:00
ldb_asprintf_errstring ( module - > ldb , " No such attribute: %s for delete on %s " , msg - > elements [ i ] . name , dn ) ;
2005-09-18 22:49:06 +04:00
ret = LDB_ERR_NO_SUCH_ATTRIBUTE ;
2004-04-03 16:29:21 +04:00
goto failed ;
}
break ;
}
for ( j = 0 ; j < msg - > elements [ i ] . num_values ; j + + ) {
2004-11-15 14:40:27 +03:00
if ( msg_delete_element ( module ,
2005-01-02 10:49:29 +03:00
msg2 ,
2004-05-01 13:45:56 +04:00
msg - > elements [ i ] . name ,
& msg - > elements [ i ] . values [ j ] ) ! = 0 ) {
2006-08-13 11:33:57 +04:00
ldb_asprintf_errstring ( module - > ldb , " No matching attribute value when deleting attribute: %s on %s " , msg - > elements [ i ] . name , dn ) ;
2005-09-18 22:49:06 +04:00
ret = LDB_ERR_NO_SUCH_ATTRIBUTE ;
2004-04-03 16:29:21 +04:00
goto failed ;
}
2005-08-18 19:02:01 +04:00
if ( ltdb_index_del_value ( module , dn , & msg - > elements [ i ] , j ) ! = 0 ) {
2006-05-31 14:36:48 +04:00
ret = LDB_ERR_OTHER ;
2004-12-19 13:56:29 +03:00
goto failed ;
}
2004-04-03 16:29:21 +04:00
}
break ;
2005-06-20 12:50:53 +04:00
default :
2006-08-13 11:33:57 +04:00
ldb_asprintf_errstring ( module - > ldb , " Invalid ldb_modify flags on %s: 0x%x " ,
msg - > elements [ i ] . name ,
msg - > elements [ i ] . flags & LDB_FLAG_MOD_MASK ) ;
2005-09-18 22:49:06 +04:00
ret = LDB_ERR_PROTOCOL_ERROR ;
2005-06-20 12:50:53 +04:00
goto failed ;
2004-03-31 10:45:39 +04:00
}
}
2004-04-03 16:29:21 +04:00
/* we've made all the mods - save the modified record back into the database */
2005-01-02 10:49:29 +03:00
ret = ltdb_store ( module , msg2 , TDB_MODIFY ) ;
2006-05-30 05:46:14 +04:00
if ( ret ! = LDB_SUCCESS ) {
goto failed ;
}
if ( ltdb_modified ( module , msg - > dn ) ! = LDB_SUCCESS ) {
ret = LDB_ERR_OPERATIONS_ERROR ;
goto failed ;
}
2004-04-03 16:29:21 +04:00
2005-01-02 10:49:29 +03:00
talloc_free ( tdb_key . dptr ) ;
2004-04-03 16:29:21 +04:00
free ( tdb_data . dptr ) ;
return ret ;
failed :
2005-01-02 10:49:29 +03:00
talloc_free ( tdb_key . dptr ) ;
2004-03-31 10:45:39 +04:00
free ( tdb_data . dptr ) ;
2005-09-18 22:49:06 +04:00
return ret ;
2004-05-01 13:45:56 +04:00
}
/*
modify a record
*/
2006-05-30 03:46:43 +04:00
static int ltdb_modify ( struct ldb_module * module , struct ldb_request * req )
2004-05-01 13:45:56 +04:00
{
2006-03-03 20:44:03 +03:00
struct ltdb_private * ltdb = talloc_get_type ( module - > private_data , struct ltdb_private ) ;
2006-07-22 20:56:33 +04:00
struct ltdb_context * ltdb_ac ;
2006-05-28 06:10:44 +04:00
int tret , ret = LDB_SUCCESS ;
2006-03-03 20:44:03 +03:00
2006-05-30 03:46:43 +04:00
if ( req - > controls ! = NULL ) {
ldb_debug ( module - > ldb , LDB_DEBUG_WARNING , " Controls should not reach the ldb_tdb backend! \n " ) ;
if ( check_critical_controls ( req - > controls ) ) {
return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION ;
}
}
2006-07-22 21:21:59 +04:00
req - > handle = NULL ;
2006-03-03 20:44:03 +03:00
2006-10-16 07:15:41 +04:00
req - > handle = init_ltdb_handle ( ltdb , module , req ) ;
2006-07-22 21:21:59 +04:00
if ( req - > handle = = NULL ) {
2006-05-28 06:10:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2006-03-03 20:44:03 +03:00
}
2006-07-22 21:21:59 +04:00
ltdb_ac = talloc_get_type ( req - > handle - > private_data , struct ltdb_context ) ;
2004-05-01 13:45:56 +04:00
2006-05-29 05:30:02 +04:00
tret = ltdb_check_special_dn ( module , req - > op . mod . message ) ;
2006-05-28 06:10:44 +04:00
if ( tret ! = LDB_SUCCESS ) {
2006-07-22 21:21:59 +04:00
req - > handle - > status = tret ;
2006-05-28 06:10:44 +04:00
goto done ;
2005-05-18 01:43:47 +04:00
}
2004-11-15 14:40:27 +03:00
if ( ltdb_cache_load ( module ) ! = 0 ) {
2006-05-28 06:10:44 +04:00
ret = LDB_ERR_OPERATIONS_ERROR ;
goto done ;
2004-05-01 13:45:56 +04:00
}
2006-05-29 05:30:02 +04:00
tret = ltdb_modify_internal ( module , req - > op . mod . message ) ;
2006-05-28 06:10:44 +04:00
if ( tret ! = LDB_SUCCESS ) {
2006-07-22 21:21:59 +04:00
req - > handle - > status = tret ;
2006-05-28 06:10:44 +04:00
goto done ;
2004-05-01 13:45:56 +04:00
}
2004-04-03 16:29:21 +04:00
2006-05-31 14:22:38 +04:00
if ( ltdb_ac - > callback ) {
2006-05-28 06:10:44 +04:00
ret = ltdb_ac - > callback ( module - > ldb , ltdb_ac - > context , NULL ) ;
2006-05-31 14:22:38 +04:00
}
2006-05-28 06:10:44 +04:00
done :
2006-07-22 21:21:59 +04:00
req - > handle - > state = LDB_ASYNC_DONE ;
2006-05-28 06:10:44 +04:00
return ret ;
2006-03-03 20:44:03 +03:00
}
2004-10-20 23:28:02 +04:00
/*
rename a record
*/
2006-05-30 03:46:43 +04:00
static int ltdb_rename ( struct ldb_module * module , struct ldb_request * req )
2004-10-20 23:28:02 +04:00
{
2006-03-03 20:44:03 +03:00
struct ltdb_private * ltdb = talloc_get_type ( module - > private_data , struct ltdb_private ) ;
2006-07-22 20:56:33 +04:00
struct ltdb_context * ltdb_ac ;
2005-01-02 10:49:29 +03:00
struct ldb_message * msg ;
2006-05-28 06:10:44 +04:00
int tret , ret = LDB_SUCCESS ;
2006-03-03 20:44:03 +03:00
2006-05-30 03:46:43 +04:00
if ( req - > controls ! = NULL ) {
ldb_debug ( module - > ldb , LDB_DEBUG_WARNING , " Controls should not reach the ldb_tdb backend! \n " ) ;
if ( check_critical_controls ( req - > controls ) ) {
return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION ;
}
}
2006-07-22 21:21:59 +04:00
req - > handle = NULL ;
2004-10-20 23:28:02 +04:00
2005-06-18 11:42:21 +04:00
if ( ltdb_cache_load ( module ) ! = 0 ) {
2006-05-28 06:10:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2005-06-18 11:42:21 +04:00
}
2006-10-16 07:15:41 +04:00
req - > handle = init_ltdb_handle ( ltdb , module , req ) ;
2006-07-22 21:21:59 +04:00
if ( req - > handle = = NULL ) {
2006-05-28 06:10:44 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2006-03-03 20:44:03 +03:00
}
2006-07-22 21:21:59 +04:00
ltdb_ac = talloc_get_type ( req - > handle - > private_data , struct ltdb_context ) ;
2006-03-03 20:44:03 +03:00
msg = talloc ( ltdb_ac , struct ldb_message ) ;
2005-01-02 10:49:29 +03:00
if ( msg = = NULL ) {
2006-05-28 06:10:44 +04:00
ret = LDB_ERR_OPERATIONS_ERROR ;
goto done ;
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 */
2006-05-29 05:30:02 +04:00
tret = ltdb_search_dn1 ( module , req - > op . rename . olddn , msg ) ;
2006-05-28 06:10:44 +04:00
if ( tret ! = 1 ) {
2004-10-20 23:28:02 +04:00
/* not finding the old record is an error */
2006-07-22 21:21:59 +04:00
req - > handle - > status = LDB_ERR_NO_SUCH_OBJECT ;
2006-05-28 06:10:44 +04:00
goto done ;
2004-10-20 23:28:02 +04:00
}
2006-05-29 05:30:02 +04:00
msg - > dn = ldb_dn_copy ( msg , req - > op . rename . newdn ) ;
2005-01-02 10:49:29 +03:00
if ( ! msg - > dn ) {
2006-03-03 20:44:03 +03:00
ret = LDB_ERR_OPERATIONS_ERROR ;
2006-05-28 06:10:44 +04:00
goto done ;
2004-10-20 23:28:02 +04:00
}
2006-05-30 03:46:43 +04:00
tret = ltdb_add_internal ( module , msg ) ;
2006-05-28 06:10:44 +04:00
if ( tret ! = LDB_SUCCESS ) {
ret = LDB_ERR_OPERATIONS_ERROR ;
goto done ;
2004-10-20 23:28:02 +04:00
}
2006-05-30 03:46:43 +04:00
tret = ltdb_delete_internal ( module , req - > op . rename . olddn ) ;
2006-05-28 06:10:44 +04:00
if ( tret ! = LDB_SUCCESS ) {
2006-05-30 03:46:43 +04:00
ltdb_delete_internal ( module , req - > op . rename . newdn ) ;
2006-05-28 06:10:44 +04:00
ret = LDB_ERR_OPERATIONS_ERROR ;
goto done ;
2004-10-20 23:28:02 +04:00
}
2006-05-31 14:22:38 +04:00
if ( ltdb_ac - > callback ) {
2006-05-28 06:10:44 +04:00
ret = ltdb_ac - > callback ( module - > ldb , ltdb_ac - > context , NULL ) ;
2006-05-31 14:22:38 +04:00
}
2006-05-28 06:10:44 +04:00
done :
2006-07-22 21:21:59 +04:00
req - > handle - > state = LDB_ASYNC_DONE ;
2004-10-20 23:28:02 +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 )
{
2006-10-06 19:03:41 +04:00
struct ltdb_private * ltdb =
talloc_get_type ( module - > private_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
2005-09-24 19:42:15 +04:00
return LDB_SUCCESS ;
2005-09-17 23:25:50 +04:00
}
2005-09-24 19:42:15 +04:00
static int ltdb_end_trans ( struct ldb_module * module )
2005-09-17 23:25:50 +04:00
{
2006-10-06 19:03:41 +04:00
struct ltdb_private * ltdb =
talloc_get_type ( module - > private_data , struct ltdb_private ) ;
2005-09-22 07:56:41 +04:00
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 )
{
2006-10-06 19:03:41 +04:00
struct ltdb_private * ltdb =
talloc_get_type ( module - > private_data , struct ltdb_private ) ;
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-07-22 20:56:33 +04:00
static int ltdb_wait ( struct ldb_handle * handle , enum ldb_wait_type type )
2006-03-03 20:44:03 +03:00
{
return handle - > status ;
}
2005-11-08 03:11:45 +03:00
static int ltdb_request ( struct ldb_module * module , struct ldb_request * req )
{
2006-01-06 07:01:23 +03:00
/* check for oustanding critical controls and return an error if found */
2006-03-03 20:44:03 +03:00
if ( req - > controls ! = NULL ) {
ldb_debug ( module - > ldb , LDB_DEBUG_WARNING , " Controls should not reach the ldb_tdb backend! \n " ) ;
2006-05-30 03:46:43 +04:00
if ( check_critical_controls ( req - > controls ) ) {
return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION ;
}
2006-01-06 07:01:23 +03:00
}
2006-05-30 03:46:43 +04:00
/* search, add, modify, delete, rename are handled by their own, no other op supported */
return LDB_ERR_OPERATIONS_ERROR ;
2005-11-08 03:11:45 +03:00
}
2006-02-27 03:39:26 +03:00
/*
return sequenceNumber from @ BASEINFO
*/
2006-06-08 01:03:38 +04:00
static int ltdb_sequence_number ( struct ldb_module * module , struct ldb_request * req )
2006-02-27 03:39:26 +03:00
{
2006-06-08 01:03:38 +04:00
TALLOC_CTX * tmp_ctx = talloc_new ( req ) ;
struct ldb_message * msg = NULL ;
2006-11-22 03:59:34 +03:00
struct ldb_dn * dn = ldb_dn_new ( tmp_ctx , module - > ldb , LTDB_BASEINFO ) ;
2006-06-08 01:03:38 +04:00
int tret ;
if ( tmp_ctx = = NULL ) {
talloc_free ( tmp_ctx ) ;
return LDB_ERR_OPERATIONS_ERROR ;
}
msg = talloc ( tmp_ctx , struct ldb_message ) ;
if ( msg = = NULL ) {
talloc_free ( tmp_ctx ) ;
return LDB_ERR_OPERATIONS_ERROR ;
}
2006-02-27 03:39:26 +03:00
2006-09-21 10:44:12 +04:00
req - > op . seq_num . flags = 0 ;
2006-06-08 01:03:38 +04:00
tret = ltdb_search_dn1 ( module , dn , msg ) ;
if ( tret ! = 1 ) {
2006-02-27 03:39:26 +03:00
talloc_free ( tmp_ctx ) ;
2006-06-08 01:03:38 +04:00
req - > op . seq_num . seq_num = 0 ;
2006-02-27 03:39:26 +03:00
/* zero is as good as anything when we don't know */
2006-06-08 01:03:38 +04:00
return LDB_SUCCESS ;
2006-02-27 03:39:26 +03:00
}
2006-09-21 10:44:12 +04:00
switch ( req - > op . seq_num . type ) {
case LDB_SEQ_HIGHEST_SEQ :
req - > op . seq_num . seq_num = ldb_msg_find_attr_as_uint64 ( msg , LTDB_SEQUENCE_NUMBER , 0 ) ;
break ;
case LDB_SEQ_NEXT :
req - > op . seq_num . seq_num = ldb_msg_find_attr_as_uint64 ( msg , LTDB_SEQUENCE_NUMBER , 0 ) ;
req - > op . seq_num . seq_num + + ;
break ;
case LDB_SEQ_HIGHEST_TIMESTAMP :
{
const char * date = ldb_msg_find_attr_as_string ( msg , LTDB_MOD_TIMESTAMP , NULL ) ;
if ( date ) {
req - > op . seq_num . seq_num = ldb_string_to_time ( date ) ;
} else {
req - > op . seq_num . seq_num = 0 ;
/* zero is as good as anything when we don't know */
}
break ;
}
}
2006-02-27 03:39:26 +03:00
talloc_free ( tmp_ctx ) ;
2006-06-08 01:03:38 +04:00
return LDB_SUCCESS ;
2006-02-27 03:39:26 +03:00
}
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 " ,
2006-05-30 03:46:43 +04:00
. search = ltdb_search ,
. add = ltdb_add ,
. modify = ltdb_modify ,
. del = ltdb_delete ,
. rename = ltdb_rename ,
2005-11-08 03:11:45 +03:00
. request = ltdb_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 ,
2006-03-03 23:01:19 +03:00
. del_transaction = ltdb_del_trans ,
2006-07-22 20:56:33 +04:00
. wait = ltdb_wait ,
2006-06-08 01:03:38 +04:00
. sequence_number = ltdb_sequence_number
2004-03-31 10:45:39 +04:00
} ;
/*
connect to the database
*/
2006-03-05 19:05:26 +03: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 [ ] ,
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 ) {
2005-06-18 11:42:21 +04:00
ldb_debug ( ldb , LDB_DEBUG_ERROR , " Invalid tdb URL '%s' " , url ) ;
return - 1 ;
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
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 ) ;
return - 1 ;
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 */
2006-07-03 10:40:56 +04:00
ltdb - > tdb = ltdb_wrap_open ( ltdb , path , 10000 ,
2006-09-27 09:57:41 +04:00
tdb_flags , open_flags ,
ldb - > create_perms , ldb ) ;
2005-07-20 04:59:38 +04:00
if ( ! ltdb - > tdb ) {
ldb_debug ( ldb , LDB_DEBUG_ERROR , " Unable to open tdb '%s' \n " , path ) ;
talloc_free ( ltdb ) ;
return - 1 ;
}
2004-05-01 13:45:56 +04:00
2005-07-20 04:59:38 +04:00
ltdb - > sequence_number = 0 ;
2004-03-31 10:45:39 +04:00
2006-06-08 01:03:38 +04:00
* module = talloc ( ldb , struct ldb_module ) ;
if ( ! module ) {
2005-06-18 11:42:21 +04:00
ldb_oom ( ldb ) ;
talloc_free ( ltdb ) ;
return - 1 ;
2004-11-15 14:40:27 +03:00
}
2006-10-17 05:21:02 +04:00
talloc_set_name_const ( * module , " ldb_tdb backend " ) ;
2006-06-08 01:03:38 +04:00
( * module ) - > ldb = ldb ;
2006-06-08 05:02:14 +04:00
( * module ) - > prev = ( * module ) - > next = NULL ;
2006-06-08 01:03:38 +04:00
( * module ) - > private_data = ltdb ;
( * module ) - > ops = & ltdb_ops ;
2004-03-31 10:45:39 +04:00
2006-10-17 05:21:02 +04:00
if ( ltdb_cache_load ( * module ) ! = 0 ) {
talloc_free ( * module ) ;
talloc_free ( ltdb ) ;
return - 1 ;
}
2005-06-18 11:42:21 +04:00
return 0 ;
2004-03-31 10:45:39 +04:00
}
2006-03-05 19:05:26 +03:00
int ldb_tdb_init ( void )
{
2006-03-21 00:44:59 +03:00
return ldb_register_backend ( " tdb " , ltdb_connect ) ;
2006-03-05 19:05:26 +03:00
}