2010-07-16 15:19:07 +04:00
/*
2004-03-31 10:45:39 +04:00
ldb database library
Copyright ( C ) Andrew Tridgell 2004
* * NOTE ! The following LGPL license applies to the ldb
* * library . This does NOT imply that all of Samba is released
* * under the LGPL
2010-07-16 15:19:07 +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
*/
/*
* Name : ldb
*
* Component : ldbadd
*
* Description : utility to add records - modelled on ldapadd
*
* Author : Andrew Tridgell
*/
2012-06-11 00:59:00 +04:00
# include "replace.h"
2009-01-30 02:39:30 +03:00
# include "ldb.h"
2007-05-05 22:59:52 +04:00
# include "tools/cmdline.h"
2009-12-22 20:44:19 +03:00
# include "ldbutil.h"
2012-04-04 09:17:32 +04:00
# include "include/ldb_private.h"
2004-03-31 10:45:39 +04:00
2009-11-20 03:34:24 +03:00
static struct ldb_cmdline * options ;
2004-03-31 10:45:39 +04:00
2010-11-01 10:45:25 +03:00
static void usage ( struct ldb_context * ldb )
2004-04-11 00:18:22 +04:00
{
2010-07-16 15:19:07 +04:00
printf ( " Usage: ldbadd <options> <ldif...> \n " ) ;
2004-04-11 00:18:22 +04:00
printf ( " Adds records to a ldb, reading ldif the specified list of files \n \n " ) ;
2010-11-01 10:45:25 +03:00
ldb_cmdline_help ( ldb , " ldbadd " , stdout ) ;
2011-02-01 22:34:44 +03:00
exit ( LDB_ERR_OPERATIONS_ERROR ) ;
2004-04-11 00:18:22 +04:00
}
2004-03-31 10:45:39 +04:00
2004-04-11 00:18:22 +04:00
/*
add records from an opened file
*/
2009-11-06 20:35:17 +03:00
static int process_file ( struct ldb_context * ldb , FILE * f , unsigned int * count )
2004-04-11 00:18:22 +04:00
{
struct ldb_ldif * ldif ;
2011-04-04 14:59:30 +04:00
int fun_ret = LDB_SUCCESS , ret ;
2009-12-22 20:44:19 +03:00
struct ldb_control * * req_ctrls = ldb_parse_control_strings ( ldb , ldb , ( const char * * ) options - > controls ) ;
2012-04-04 09:17:32 +04:00
struct ldif_read_file_state state = {
. f = f
} ;
2009-12-22 20:44:19 +03:00
if ( options - > controls ! = NULL & & req_ctrls = = NULL ) {
printf ( " parsing controls failed: %s \n " , ldb_errstring ( ldb ) ) ;
2011-01-14 14:34:08 +03:00
return LDB_ERR_OPERATIONS_ERROR ;
2009-12-22 20:44:19 +03:00
}
2012-04-04 09:17:32 +04:00
fun_ret = ldb_transaction_start ( ldb ) ;
if ( fun_ret ! = LDB_SUCCESS ) {
fprintf ( stderr , " ERR: (%s) on transaction start \n " ,
ldb_errstring ( ldb ) ) ;
return fun_ret ;
}
2004-04-03 16:29:21 +04:00
2012-04-04 09:17:32 +04:00
while ( ( ldif = ldb_ldif_read_file_state ( ldb , & state ) ) ) {
2004-04-03 16:29:21 +04:00
if ( ldif - > changetype ! = LDB_CHANGETYPE_ADD & &
ldif - > changetype ! = LDB_CHANGETYPE_NONE ) {
fprintf ( stderr , " Only CHANGETYPE_ADD records allowed \n " ) ;
break ;
}
2010-07-16 15:03:53 +04:00
ret = ldb_msg_normalize ( ldb , ldif , ldif - > msg , & ldif - > msg ) ;
if ( ret ! = LDB_SUCCESS ) {
fprintf ( stderr ,
" ERR: Message canonicalize failed - %s \n " ,
ldb_strerror ( ret ) ) ;
2011-04-04 14:59:30 +04:00
fun_ret = ret ;
2010-07-16 15:03:53 +04:00
ldb_ldif_read_free ( ldb , ldif ) ;
continue ;
}
2005-06-24 05:58:40 +04:00
2012-04-04 06:51:00 +04:00
ret = ldb_add_ctrl ( ldb , ldif - > msg , req_ctrls ) ;
2005-09-24 19:42:15 +04:00
if ( ret ! = LDB_SUCCESS ) {
2012-04-04 09:17:32 +04:00
fprintf ( stderr , " ERR: %s : \" %s \" on DN %s at block before line %llu \n " ,
2009-12-22 04:31:42 +03:00
ldb_strerror ( ret ) , ldb_errstring ( ldb ) ,
2012-04-04 09:17:32 +04:00
ldb_dn_get_linearized ( ldif - > msg - > dn ) ,
( unsigned long long ) state . line_no ) ;
2011-04-04 14:59:30 +04:00
fun_ret = ret ;
2004-03-31 10:45:39 +04:00
} else {
2006-10-14 08:43:51 +04:00
( * count ) + + ;
2009-11-20 03:34:24 +03:00
if ( options - > verbose ) {
printf ( " Added %s \n " , ldb_dn_get_linearized ( ldif - > msg - > dn ) ) ;
}
2004-03-31 10:45:39 +04:00
}
2004-05-20 17:25:06 +04:00
ldb_ldif_read_free ( ldb , ldif ) ;
2012-04-04 09:17:32 +04:00
if ( ret ) {
break ;
}
}
if ( fun_ret = = LDB_SUCCESS & & ! feof ( f ) ) {
fprintf ( stderr , " Failed to parse ldif \n " ) ;
fun_ret = LDB_ERR_OPERATIONS_ERROR ;
}
if ( fun_ret = = LDB_SUCCESS ) {
fun_ret = ldb_transaction_commit ( ldb ) ;
if ( fun_ret ! = LDB_SUCCESS ) {
fprintf ( stderr , " ERR: (%s) on transaction commit \n " ,
ldb_errstring ( ldb ) ) ;
}
} else {
ldb_transaction_cancel ( ldb ) ;
2004-03-31 10:45:39 +04:00
}
2011-04-04 14:59:30 +04:00
return fun_ret ;
2004-04-11 00:18:22 +04:00
}
2006-03-02 19:32:53 +03:00
int main ( int argc , const char * * argv )
2004-04-11 00:18:22 +04:00
{
struct ldb_context * ldb ;
2009-11-06 20:35:17 +03:00
unsigned int i , count = 0 ;
2011-01-14 14:34:08 +03:00
int ret = LDB_SUCCESS ;
2010-05-02 17:53:14 +04:00
TALLOC_CTX * mem_ctx = talloc_new ( NULL ) ;
2004-04-11 00:18:22 +04:00
2010-05-02 17:53:14 +04:00
ldb = ldb_init ( mem_ctx , NULL ) ;
2011-02-01 22:26:12 +03:00
if ( ldb = = NULL ) {
return LDB_ERR_OPERATIONS_ERROR ;
}
2004-04-11 00:18:22 +04:00
2005-06-18 11:42:21 +04:00
options = ldb_cmdline_process ( ldb , argc , argv , usage ) ;
2004-04-11 00:18:22 +04:00
2011-02-01 22:37:58 +03:00
ret = ldb_transaction_start ( ldb ) ;
if ( ret ! = LDB_SUCCESS ) {
2008-12-18 06:30:11 +03:00
printf ( " Failed to start transaction: %s \n " , ldb_errstring ( ldb ) ) ;
2011-02-01 22:37:58 +03:00
return ret ;
2008-12-16 06:39:42 +03:00
}
2005-06-18 11:42:21 +04:00
if ( options - > argc = = 0 ) {
2006-10-14 08:43:51 +04:00
ret = process_file ( ldb , stdin , & count ) ;
2005-06-18 11:42:21 +04:00
} else {
for ( i = 0 ; i < options - > argc ; i + + ) {
const char * fname = options - > argv [ i ] ;
FILE * f ;
f = fopen ( fname , " r " ) ;
if ( ! f ) {
perror ( fname ) ;
2011-02-01 22:34:44 +03:00
return LDB_ERR_OPERATIONS_ERROR ;
2005-06-18 11:42:21 +04:00
}
2006-10-14 08:43:51 +04:00
ret = process_file ( ldb , f , & count ) ;
2004-04-11 00:18:22 +04:00
fclose ( f ) ;
}
}
2009-09-04 07:59:44 +04:00
if ( count ! = 0 ) {
2011-02-01 22:37:58 +03:00
ret = ldb_transaction_commit ( ldb ) ;
if ( ret ! = LDB_SUCCESS ) {
2009-09-04 07:59:44 +04:00
printf ( " Failed to commit transaction: %s \n " , ldb_errstring ( ldb ) ) ;
2011-02-01 22:37:58 +03:00
return ret ;
2009-09-04 07:59:44 +04:00
}
} else {
ldb_transaction_cancel ( ldb ) ;
2008-12-16 06:39:42 +03:00
}
2010-05-02 17:53:14 +04:00
talloc_free ( mem_ctx ) ;
2004-03-31 10:45:39 +04:00
2012-04-04 09:17:32 +04:00
if ( ret ) {
printf ( " Add failed after processing %u records \n " , count ) ;
} else {
printf ( " Added %u records successfully \n " , count ) ;
}
2010-07-16 15:19:07 +04:00
2006-10-14 08:43:51 +04:00
return ret ;
2004-03-31 10:45:39 +04:00
}