2008-06-15 04:37:40 +04:00
/*
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
2008-09-12 02:34:11 +04:00
Copyright ( C ) Simo Sorce 2006 - 2008
2008-06-15 04:37:40 +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
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 :
*
* - description : make the module use asyncronous calls
* 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
2004-03-31 10:45:39 +04:00
*/
2007-05-05 22:50:56 +04:00
# include "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 ;
}
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 ) ;
2008-10-15 22:03:20 +04:00
if ( ltdb - > in_transaction = = 0 ) {
return tdb_lockall_read ( ltdb - > tdb ) ;
}
return 0 ;
}
/*
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 ) ;
2008-10-15 22:03:20 +04:00
if ( ltdb - > in_transaction = = 0 ) {
return tdb_unlockall_read ( ltdb - > tdb ) ;
}
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
*/
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
{
2009-01-30 02:39:30 +03:00
struct ldb_context * ldb = ldb_module_get_ctx ( module ) ;
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
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
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
}
2006-11-27 08:32:35 +03:00
key_str = talloc_strdup ( ldb , " DN= " ) ;
if ( ! key_str ) {
goto failed ;
}
2005-08-18 19:02:01 +04:00
2007-09-18 17:41:50 +04:00
key_str = talloc_strdup_append_buffer ( key_str , dn_folded ) ;
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
*/
2008-10-20 20:59:51 +04:00
static int ltdb_check_special_dn ( struct ldb_module * module ,
2008-06-15 04:37:40 +04: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 ) ;
2005-05-18 01:43:47 +04:00
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 ) ) {
2008-09-25 01:59:59 +04:00
return 0 ;
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 + + ) {
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
}
}
}
2008-09-25 01:59:59 +04:00
return 0 ;
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 ;
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
}
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
}
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
{
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 ;
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 ) ;
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 ) ;
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 ;
}
2008-06-15 04:37:40 +04:00
2004-11-15 14:40:27 +03:00
ret = ltdb_index_add ( module , msg ) ;
2007-04-23 04:36:49 +04:00
if ( ret ! = LDB_SUCCESS ) {
2004-03-31 10:45:39 +04:00
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 ;
}
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 ) ;
2006-05-30 03:46:43 +04:00
int ret ;
2008-06-15 04:37:40 +04:00
2006-05-30 03:46:43 +04:00
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
}
2008-06-15 04:37:40 +04:00
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 ) {
2009-01-30 02:39:30 +03:00
ldb_asprintf_errstring ( ldb ,
2008-06-15 04:37:40 +04:00
" Entry %s already exists " ,
ldb_dn_get_linearized ( msg - > dn ) ) ;
2006-08-01 07:22:02 +04:00
return ret ;
}
2008-06-15 04:37:40 +04:00
2006-08-01 07:22:02 +04:00
if ( ret = = LDB_SUCCESS ) {
2006-12-11 18:49:39 +03:00
ret = ltdb_index_one ( module , msg , 1 ) ;
if ( ret ! = LDB_SUCCESS ) {
2007-04-23 04:36:49 +04:00
return ret ;
2006-12-11 18:49:39 +03:00
}
2006-08-01 06:25:05 +04:00
ret = ltdb_modified ( module , msg - > dn ) ;
if ( ret ! = LDB_SUCCESS ) {
2007-04-23 04:36:49 +04:00
return ret ;
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
*/
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 ;
int tret ;
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-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 ) {
2008-09-12 02:34:11 +04:00
return tret ;
2005-05-18 01:43:47 +04:00
}
2008-06-15 04:37:40 +04:00
2008-09-12 02:34:11 +04:00
return LDB_SUCCESS ;
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
{
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 ;
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 ) ;
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
}
ret = ltdb_delete_noindex ( module , dn ) ;
if ( ret ! = LDB_SUCCESS ) {
2007-04-23 04:36:49 +04:00
goto done ;
2006-05-30 03:46:43 +04:00
}
2006-12-11 18:49:39 +03:00
/* remove one level attribute */
ret = ltdb_index_one ( module , msg , 0 ) ;
if ( ret ! = LDB_SUCCESS ) {
2007-04-23 04:36:49 +04:00
goto done ;
2006-12-11 18:49:39 +03:00
}
2006-05-30 03:46:43 +04:00
/* remove any indexed attributes */
ret = ltdb_index_del ( module , msg ) ;
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 ;
int tret ;
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
}
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 ) {
2008-09-12 02:34:11 +04:00
return tret ;
2006-03-03 20:44:03 +03:00
}
2004-04-23 17:05:27 +04:00
2008-09-12 02:34:11 +04:00
return LDB_SUCCESS ;
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 )
*/
2004-05-06 08:40:15 +04:00
static int msg_add_element ( struct ldb_context * ldb ,
2008-06-15 04:37:40 +04:00
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
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 ;
e2 - > values = NULL ;
if ( el - > num_values ! = 0 ) {
2008-06-15 04:37:40 +04: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 + + ) {
2008-06-15 04:37:40 +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 ) ) {
2008-06-15 04:37:40 +04:00
memmove ( & msg - > elements [ i ] ,
& msg - > elements [ i + 1 ] ,
2005-01-02 10:49:29 +03:00
sizeof ( struct ldb_message_element ) *
( msg - > num_elements - ( i + 1 ) ) ) ;
}
msg - > num_elements - - ;
i - - ;
2008-06-15 04:37:40 +04:00
msg - > elements = talloc_realloc ( msg , msg - > elements ,
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
/*
2008-06-15 04:37:40 +04:00
delete all elements matching an attribute name / value
2004-04-03 16:29:21 +04:00
return 0 on success , - 1 on failure
*/
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 ;
int found ;
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 ) {
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
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 + + ) {
2008-06-15 04:37:40 +04:00
if ( a - > syntax - > 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 ] ,
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 - - ;
2004-05-08 03:54:41 +04:00
if ( el - > num_values = = 0 ) {
2008-06-15 04:37:40 +04: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
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
2004-03-31 10:45:39 +04:00
*/
2008-06-15 04:37:40 +04:00
int ltdb_modify_internal ( struct ldb_module * module ,
const struct ldb_message * msg )
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 ;
2004-07-07 05:02:54 +04:00
unsigned i , j ;
2007-07-07 08:34:36 +04:00
int ret , idx ;
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 ) ;
2008-09-25 01:59:59 +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 */
2007-07-07 08:34:36 +04:00
idx = find_element ( msg2 , el - > name ) ;
2004-12-26 20:30:27 +03:00
2007-07-07 08:34:36 +04:00
if ( idx = = - 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 ;
}
2007-07-07 08:34:36 +04:00
el2 = & msg2 - > elements [ idx ] ;
2004-12-26 20:30:27 +03:00
2007-07-07 08:34:36 +04:00
/* An attribute with this name already exists,
* add all values if they don ' t already exist
* ( check both the other elements to be added ,
* and those already in the db ) . */
2004-12-26 20:30:27 +03:00
for ( j = 0 ; j < el - > num_values ; j + + ) {
if ( ldb_msg_find_val ( el2 , & el - > values [ j ] ) ) {
2009-01-30 02:39:30 +03:00
ldb_asprintf_errstring ( ldb , " %s: value #%d already exists " , el - > name , j ) ;
2007-07-07 08:34:36 +04:00
ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS ;
goto failed ;
}
if ( ldb_msg_find_val ( el , & el - > values [ j ] ) ! = & el - > values [ j ] ) {
2009-01-30 02:39:30 +03:00
ldb_asprintf_errstring ( ldb , " %s: value #%d provided more than once " , el - > name , j ) ;
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 */
2007-07-07 08:34:36 +04:00
msg_delete_attribute ( module , ldb , msg2 , el - > name ) ;
for ( j = 0 ; j < el - > num_values ; j + + ) {
if ( ldb_msg_find_val ( el , & el - > values [ j ] ) ! = & el - > values [ j ] ) {
2009-01-30 02:39:30 +03:00
ldb_asprintf_errstring ( ldb , " %s: value #%d provided more than once " , el - > name , j ) ;
2007-07-07 08:34:36 +04:00
ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS ;
goto failed ;
}
}
2004-05-22 04:52:04 +04:00
/* add the replacement element, if not empty */
2007-07-07 08:34:36 +04:00
if ( el - > num_values ! = 0 & &
msg_add_element ( ldb , msg2 , el ) ! = 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 ) {
2009-01-30 02:39:30 +03:00
ldb_asprintf_errstring ( 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 ) {
2009-01-30 02:39:30 +03:00
ldb_asprintf_errstring ( 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 ;
}
2007-04-23 04:36:49 +04:00
ret = ltdb_index_del_value ( module , dn , & msg - > elements [ i ] , j ) ;
if ( ret ! = LDB_SUCCESS ) {
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 :
2009-01-30 02:39:30 +03:00
ldb_asprintf_errstring ( ldb ,
2008-06-15 04:37:40 +04:00
" 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
}
}
2008-06-15 04:37:40 +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 ;
}
2007-04-23 04:36:49 +04:00
ret = ltdb_modified ( module , msg - > dn ) ;
if ( ret ! = LDB_SUCCESS ) {
2006-05-30 05:46:14 +04:00
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
*/
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 ;
int tret ;
2006-03-03 20:44:03 +03:00
2009-01-30 02:39:30 +03:00
ldb_request_set_state ( req , LDB_ASYNC_PENDING ) ;
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 ) {
2008-09-12 02:34:11 +04:00
return tret ;
2005-05-18 01:43:47 +04:00
}
2008-06-15 04:37:40 +04:00
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
}
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 ) {
2008-09-12 02:34:11 +04:00
return tret ;
2004-05-01 13:45:56 +04:00
}
2004-04-03 16:29:21 +04:00
2008-09-12 02:34:11 +04:00
return LDB_SUCCESS ;
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 ;
2008-09-12 02:34:11 +04:00
int tret ;
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
}
2008-09-12 02:34:11 +04:00
msg = talloc ( ctx , struct ldb_message ) ;
2005-01-02 10:49:29 +03:00
if ( msg = = NULL ) {
2008-09-12 02:34:11 +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 */
2006-05-29 05:30:02 +04:00
tret = ltdb_search_dn1 ( module , req - > op . rename . olddn , msg ) ;
2007-04-23 04:36:49 +04:00
if ( tret ! = LDB_SUCCESS ) {
2004-10-20 23:28:02 +04:00
/* not finding the old record is an error */
2008-09-12 02:34:11 +04:00
return tret ;
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 ) {
2008-09-12 02:34:11 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2004-10-20 23:28:02 +04:00
}
2007-09-18 10:36:07 +04:00
if ( ldb_dn_compare ( req - > op . rename . olddn , req - > op . rename . newdn ) = = 0 ) {
/* The rename operation is apparently only changing case -
the DNs are the same . Delete the old DN before adding
the new one to avoid a TDB_ERR_EXISTS error .
The only drawback to this is that if the delete
succeeds but the add fails , we rely on the
transaction to roll this all back . */
2008-09-12 02:34:11 +04:00
tret = ltdb_delete_internal ( module , req - > op . rename . olddn ) ;
if ( tret ! = LDB_SUCCESS ) {
return tret ;
2007-09-18 10:36:07 +04:00
}
2004-10-20 23:28:02 +04:00
2008-09-12 02:34:11 +04:00
tret = ltdb_add_internal ( module , msg ) ;
if ( tret ! = LDB_SUCCESS ) {
return tret ;
2007-09-18 10:36:07 +04:00
}
} else {
/* The rename operation is changing DNs. Try to add the new
DN first to avoid clobbering another DN not related to
this rename operation . */
2008-09-12 02:34:11 +04:00
tret = ltdb_add_internal ( module , msg ) ;
if ( tret ! = LDB_SUCCESS ) {
return tret ;
2007-09-18 10:36:07 +04:00
}
tret = ltdb_delete_internal ( module , req - > op . rename . olddn ) ;
if ( tret ! = LDB_SUCCESS ) {
ltdb_delete_internal ( module , req - > op . rename . newdn ) ;
2008-09-12 02:34:11 +04:00
return LDB_ERR_OPERATIONS_ERROR ;
2007-09-18 10:36:07 +04:00
}
2004-10-20 23:28:02 +04:00
}
2008-09-12 02:34:11 +04:00
return LDB_SUCCESS ;
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
2008-12-16 10:45:28 +03: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 ;
2007-12-24 07:03:31 +03:00
TALLOC_CTX * tmp_ctx ;
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 ;
2008-10-15 22:03:20 +04:00
int ret ;
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 ;
}
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 ) ;
2007-12-24 07:03:31 +03:00
2006-06-08 01:03:38 +04:00
msg = talloc ( tmp_ctx , struct ldb_message ) ;
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
ret = LDB_SUCCESS ;
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 */
ret = LDB_ERR_UNWILLING_TO_PERFORM ;
}
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
{
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 ;
if ( check_critical_controls ( req - > controls ) ) {
return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION ;
}
2009-01-30 02:39:30 +03:00
ldb = ldb_module_get_ctx ( module ) ;
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-01-30 02:39:30 +03:00
ldb_set_errstring ( ldb , " Out of Memory " ) ;
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
}
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 " ,
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 ) ;
2008-09-25 01:59:59 +04:00
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
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 ) ;
2008-09-25 01:59:59 +04:00
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 */
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 ,
" Unable to open tdb '%s' \n " , path ) ;
2005-07-20 04:59:38 +04:00
talloc_free ( ltdb ) ;
2008-09-25 01:59:59 +04:00
return - 1 ;
2005-07-20 04:59:38 +04:00
}
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
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 ) ;
2008-09-25 01:59:59 +04:00
return - 1 ;
2004-11-15 14:40:27 +03:00
}
2009-01-30 02:39:30 +03:00
ldb_module_set_private ( 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 ) ;
2008-09-25 01:59:59 +04:00
return - 1 ;
2006-10-17 05:21:02 +04:00
}
2009-01-30 02:39:30 +03:00
* _module = module ;
2008-09-25 01:59:59 +04:00
return 0 ;
2004-03-31 10:45:39 +04:00
}
2006-03-05 19:05:26 +03:00
2008-02-20 04:57:07 +03:00
const struct ldb_backend_ops ldb_tdb_backend_ops = {
. name = " tdb " ,
. connect_fn = ltdb_connect
} ;